blob: 5792816d4fca8f40c5be6dad4945b8f6dfc29b38 [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 Masonc8b97812008-10-29 14:49:59 -0400363 total_compressed = actual_end - start;
364
365 /* we want to make sure that amount of ram required to uncompress
366 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500367 * of a compressed extent to 128k. This is a crucial number
368 * because it also controls how easily we can spread reads across
369 * cpus for decompression.
370 *
371 * We also want to make sure the amount of IO required to do
372 * a random read is reasonably small, so we limit the size of
373 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400374 */
375 total_compressed = min(total_compressed, max_uncompressed);
Chris Masondb945352007-10-15 16:15:53 -0400376 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
Chris Masonbe20aa92007-12-17 20:14:01 -0500377 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400378 disk_num_bytes = num_bytes;
379 total_in = 0;
380 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400381
Chris Mason771ed682008-11-06 22:02:51 -0500382 /*
383 * we do compression for mount -o compress and when the
384 * inode has not been flagged as nocompress. This flag can
385 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400386 */
387 if (!btrfs_test_flag(inode, NOCOMPRESS) &&
388 btrfs_test_opt(root, COMPRESS)) {
389 WARN_ON(pages);
Chris Masoncfbc2462008-10-30 13:22:14 -0400390 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
Chris Mason179e29e2007-11-01 11:28:41 -0400391
Chris Masonc8b97812008-10-29 14:49:59 -0400392 ret = btrfs_zlib_compress_pages(inode->i_mapping, start,
393 total_compressed, pages,
394 nr_pages, &nr_pages_ret,
395 &total_in,
396 &total_compressed,
397 max_compressed);
398
399 if (!ret) {
400 unsigned long offset = total_compressed &
401 (PAGE_CACHE_SIZE - 1);
402 struct page *page = pages[nr_pages_ret - 1];
403 char *kaddr;
404
405 /* zero the tail end of the last page, we might be
406 * sending it down to disk
407 */
408 if (offset) {
409 kaddr = kmap_atomic(page, KM_USER0);
410 memset(kaddr + offset, 0,
411 PAGE_CACHE_SIZE - offset);
412 kunmap_atomic(kaddr, KM_USER0);
413 }
414 will_compress = 1;
415 }
416 }
417 if (start == 0) {
Chris Mason771ed682008-11-06 22:02:51 -0500418 trans = btrfs_join_transaction(root, 1);
419 BUG_ON(!trans);
420 btrfs_set_trans_block_group(trans, inode);
421
Chris Masonc8b97812008-10-29 14:49:59 -0400422 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500423 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400424 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500425 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400426 */
427 ret = cow_file_range_inline(trans, root, inode,
428 start, end, 0, NULL);
429 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500430 /* try making a compressed inline extent */
Chris Masonc8b97812008-10-29 14:49:59 -0400431 ret = cow_file_range_inline(trans, root, inode,
432 start, end,
433 total_compressed, pages);
434 }
Chris Mason771ed682008-11-06 22:02:51 -0500435 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400436 if (ret == 0) {
Chris Mason771ed682008-11-06 22:02:51 -0500437 /*
438 * inline extent creation worked, we don't need
439 * to create any more async work items. Unlock
440 * and free up our temp pages.
441 */
Chris Masonc8b97812008-10-29 14:49:59 -0400442 extent_clear_unlock_delalloc(inode,
443 &BTRFS_I(inode)->io_tree,
Chris Mason771ed682008-11-06 22:02:51 -0500444 start, end, NULL, 1, 0,
445 0, 1, 1, 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400446 ret = 0;
447 goto free_pages_out;
448 }
449 }
450
451 if (will_compress) {
452 /*
453 * we aren't doing an inline extent round the compressed size
454 * up to a block size boundary so the allocator does sane
455 * things
456 */
457 total_compressed = (total_compressed + blocksize - 1) &
458 ~(blocksize - 1);
459
460 /*
461 * one last check to make sure the compression is really a
462 * win, compare the page count read with the blocks on disk
463 */
464 total_in = (total_in + PAGE_CACHE_SIZE - 1) &
465 ~(PAGE_CACHE_SIZE - 1);
466 if (total_compressed >= total_in) {
467 will_compress = 0;
468 } else {
469 disk_num_bytes = total_compressed;
470 num_bytes = total_in;
471 }
472 }
473 if (!will_compress && pages) {
474 /*
475 * the compression code ran but failed to make things smaller,
476 * free any pages it allocated and our page pointer array
477 */
478 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400479 WARN_ON(pages[i]->mapping);
Chris Masonc8b97812008-10-29 14:49:59 -0400480 page_cache_release(pages[i]);
481 }
482 kfree(pages);
483 pages = NULL;
484 total_compressed = 0;
485 nr_pages_ret = 0;
486
487 /* flag the file so we don't compress in the future */
488 btrfs_set_flag(inode, NOCOMPRESS);
489 }
Chris Mason771ed682008-11-06 22:02:51 -0500490 if (will_compress) {
491 *num_added += 1;
492
493 /* the async work queues will take care of doing actual
494 * allocation on disk for these compressed pages,
495 * and will submit them to the elevator.
496 */
497 add_async_extent(async_cow, start, num_bytes,
498 total_compressed, pages, nr_pages_ret);
499
Chris Mason42dc7ba2008-12-15 11:44:56 -0500500 if (start + num_bytes < end && start + num_bytes < actual_end) {
Chris Mason771ed682008-11-06 22:02:51 -0500501 start += num_bytes;
502 pages = NULL;
503 cond_resched();
504 goto again;
505 }
506 } else {
507 /*
508 * No compression, but we still need to write the pages in
509 * the file we've been given so far. redirty the locked
510 * page if it corresponds to our extent and set things up
511 * for the async work queue to run cow_file_range to do
512 * the normal delalloc dance
513 */
514 if (page_offset(locked_page) >= start &&
515 page_offset(locked_page) <= end) {
516 __set_page_dirty_nobuffers(locked_page);
517 /* unlocked later on in the async handlers */
518 }
519 add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0);
520 *num_added += 1;
521 }
522
523out:
524 return 0;
525
526free_pages_out:
527 for (i = 0; i < nr_pages_ret; i++) {
528 WARN_ON(pages[i]->mapping);
529 page_cache_release(pages[i]);
530 }
Chris Masond3977122009-01-05 21:25:51 -0500531 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500532
533 goto out;
534}
535
536/*
537 * phase two of compressed writeback. This is the ordered portion
538 * of the code, which only gets called in the order the work was
539 * queued. We walk all the async extents created by compress_file_range
540 * and send them down to the disk.
541 */
542static noinline int submit_compressed_extents(struct inode *inode,
543 struct async_cow *async_cow)
544{
545 struct async_extent *async_extent;
546 u64 alloc_hint = 0;
547 struct btrfs_trans_handle *trans;
548 struct btrfs_key ins;
549 struct extent_map *em;
550 struct btrfs_root *root = BTRFS_I(inode)->root;
551 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
552 struct extent_io_tree *io_tree;
553 int ret;
554
555 if (list_empty(&async_cow->extents))
556 return 0;
557
558 trans = btrfs_join_transaction(root, 1);
559
Chris Masond3977122009-01-05 21:25:51 -0500560 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500561 async_extent = list_entry(async_cow->extents.next,
562 struct async_extent, list);
563 list_del(&async_extent->list);
564
565 io_tree = &BTRFS_I(inode)->io_tree;
566
567 /* did the compression code fall back to uncompressed IO? */
568 if (!async_extent->pages) {
569 int page_started = 0;
570 unsigned long nr_written = 0;
571
572 lock_extent(io_tree, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500573 async_extent->start +
574 async_extent->ram_size - 1, GFP_NOFS);
Chris Mason771ed682008-11-06 22:02:51 -0500575
576 /* allocate blocks */
577 cow_file_range(inode, async_cow->locked_page,
578 async_extent->start,
579 async_extent->start +
580 async_extent->ram_size - 1,
581 &page_started, &nr_written, 0);
582
583 /*
584 * if page_started, cow_file_range inserted an
585 * inline extent and took care of all the unlocking
586 * and IO for us. Otherwise, we need to submit
587 * all those pages down to the drive.
588 */
589 if (!page_started)
590 extent_write_locked_range(io_tree,
591 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500592 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500593 async_extent->ram_size - 1,
594 btrfs_get_extent,
595 WB_SYNC_ALL);
596 kfree(async_extent);
597 cond_resched();
598 continue;
599 }
600
601 lock_extent(io_tree, async_extent->start,
602 async_extent->start + async_extent->ram_size - 1,
603 GFP_NOFS);
604 /*
605 * here we're doing allocation and writeback of the
606 * compressed pages
607 */
608 btrfs_drop_extent_cache(inode, async_extent->start,
609 async_extent->start +
610 async_extent->ram_size - 1, 0);
611
612 ret = btrfs_reserve_extent(trans, root,
613 async_extent->compressed_size,
614 async_extent->compressed_size,
615 0, alloc_hint,
616 (u64)-1, &ins, 1);
617 BUG_ON(ret);
618 em = alloc_extent_map(GFP_NOFS);
619 em->start = async_extent->start;
620 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500621 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500622
623 em->block_start = ins.objectid;
624 em->block_len = ins.offset;
625 em->bdev = root->fs_info->fs_devices->latest_bdev;
626 set_bit(EXTENT_FLAG_PINNED, &em->flags);
627 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
628
Chris Masond3977122009-01-05 21:25:51 -0500629 while (1) {
Chris Mason771ed682008-11-06 22:02:51 -0500630 spin_lock(&em_tree->lock);
631 ret = add_extent_mapping(em_tree, em);
632 spin_unlock(&em_tree->lock);
633 if (ret != -EEXIST) {
634 free_extent_map(em);
635 break;
636 }
637 btrfs_drop_extent_cache(inode, async_extent->start,
638 async_extent->start +
639 async_extent->ram_size - 1, 0);
640 }
641
642 ret = btrfs_add_ordered_extent(inode, async_extent->start,
643 ins.objectid,
644 async_extent->ram_size,
645 ins.offset,
646 BTRFS_ORDERED_COMPRESSED);
647 BUG_ON(ret);
648
649 btrfs_end_transaction(trans, root);
650
651 /*
652 * clear dirty, set writeback and unlock the pages.
653 */
654 extent_clear_unlock_delalloc(inode,
655 &BTRFS_I(inode)->io_tree,
656 async_extent->start,
657 async_extent->start +
658 async_extent->ram_size - 1,
659 NULL, 1, 1, 0, 1, 1, 0);
660
661 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500662 async_extent->start,
663 async_extent->ram_size,
664 ins.objectid,
665 ins.offset, async_extent->pages,
666 async_extent->nr_pages);
Chris Mason771ed682008-11-06 22:02:51 -0500667
668 BUG_ON(ret);
669 trans = btrfs_join_transaction(root, 1);
670 alloc_hint = ins.objectid + ins.offset;
671 kfree(async_extent);
672 cond_resched();
673 }
674
675 btrfs_end_transaction(trans, root);
676 return 0;
677}
678
679/*
680 * when extent_io.c finds a delayed allocation range in the file,
681 * the call backs end up in this code. The basic idea is to
682 * allocate extents on disk for the range, and create ordered data structs
683 * in ram to track those extents.
684 *
685 * locked_page is the page that writepage had locked already. We use
686 * it to make sure we don't do extra locks or unlocks.
687 *
688 * *page_started is set to one if we unlock locked_page and do everything
689 * required to start IO on it. It may be clean and already done with
690 * IO when we return.
691 */
692static noinline int cow_file_range(struct inode *inode,
693 struct page *locked_page,
694 u64 start, u64 end, int *page_started,
695 unsigned long *nr_written,
696 int unlock)
697{
698 struct btrfs_root *root = BTRFS_I(inode)->root;
699 struct btrfs_trans_handle *trans;
700 u64 alloc_hint = 0;
701 u64 num_bytes;
702 unsigned long ram_size;
703 u64 disk_num_bytes;
704 u64 cur_alloc_size;
705 u64 blocksize = root->sectorsize;
706 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500707 u64 isize = i_size_read(inode);
Chris Mason771ed682008-11-06 22:02:51 -0500708 struct btrfs_key ins;
709 struct extent_map *em;
710 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
711 int ret = 0;
712
713 trans = btrfs_join_transaction(root, 1);
714 BUG_ON(!trans);
715 btrfs_set_trans_block_group(trans, inode);
716
Chris Mason42dc7ba2008-12-15 11:44:56 -0500717 actual_end = min_t(u64, isize, end + 1);
Chris Mason771ed682008-11-06 22:02:51 -0500718
719 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
720 num_bytes = max(blocksize, num_bytes);
721 disk_num_bytes = num_bytes;
722 ret = 0;
723
724 if (start == 0) {
725 /* lets try to make an inline extent */
726 ret = cow_file_range_inline(trans, root, inode,
727 start, end, 0, NULL);
728 if (ret == 0) {
729 extent_clear_unlock_delalloc(inode,
730 &BTRFS_I(inode)->io_tree,
731 start, end, NULL, 1, 1,
732 1, 1, 1, 1);
733 *nr_written = *nr_written +
734 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
735 *page_started = 1;
736 ret = 0;
737 goto out;
738 }
739 }
Chris Masonc8b97812008-10-29 14:49:59 -0400740
741 BUG_ON(disk_num_bytes >
742 btrfs_super_total_bytes(&root->fs_info->super_copy));
743
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400744 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400745
Chris Masond3977122009-01-05 21:25:51 -0500746 while (disk_num_bytes > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400747 cur_alloc_size = min(disk_num_bytes, root->fs_info->max_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400748 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500749 root->sectorsize, 0, alloc_hint,
Chris Masone6dcd2d2008-07-17 12:53:50 -0400750 (u64)-1, &ins, 1);
Chris Masond3977122009-01-05 21:25:51 -0500751 BUG_ON(ret);
752
Chris Masone6dcd2d2008-07-17 12:53:50 -0400753 em = alloc_extent_map(GFP_NOFS);
754 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500755 em->orig_start = em->start;
Chris Masonc8b97812008-10-29 14:49:59 -0400756
Chris Mason771ed682008-11-06 22:02:51 -0500757 ram_size = ins.offset;
758 em->len = ins.offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400759
Chris Masone6dcd2d2008-07-17 12:53:50 -0400760 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400761 em->block_len = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400762 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400763 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -0400764
Chris Masond3977122009-01-05 21:25:51 -0500765 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -0400766 spin_lock(&em_tree->lock);
767 ret = add_extent_mapping(em_tree, em);
768 spin_unlock(&em_tree->lock);
769 if (ret != -EEXIST) {
770 free_extent_map(em);
771 break;
772 }
773 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400774 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400775 }
776
Chris Mason98d20f62008-04-14 09:46:10 -0400777 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400778 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -0500779 ram_size, cur_alloc_size, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400780 BUG_ON(ret);
Chris Masonc8b97812008-10-29 14:49:59 -0400781
Yan Zheng17d217f2008-12-12 10:03:38 -0500782 if (root->root_key.objectid ==
783 BTRFS_DATA_RELOC_TREE_OBJECTID) {
784 ret = btrfs_reloc_clone_csums(inode, start,
785 cur_alloc_size);
786 BUG_ON(ret);
787 }
788
Chris Masond3977122009-01-05 21:25:51 -0500789 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -0400790 break;
Chris Masond3977122009-01-05 21:25:51 -0500791
Chris Masonc8b97812008-10-29 14:49:59 -0400792 /* we're not doing compressed IO, don't unlock the first
793 * page (which the caller expects to stay locked), don't
794 * clear any dirty bits and don't set any writeback bits
795 */
796 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
797 start, start + ram_size - 1,
Chris Mason771ed682008-11-06 22:02:51 -0500798 locked_page, unlock, 1,
799 1, 0, 0, 0);
Chris Masonc8b97812008-10-29 14:49:59 -0400800 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -0500801 num_bytes -= cur_alloc_size;
802 alloc_hint = ins.objectid + ins.offset;
803 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400804 }
Chris Masonb888db22007-08-27 16:49:44 -0400805out:
Chris Mason771ed682008-11-06 22:02:51 -0500806 ret = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400807 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400808
Chris Masonbe20aa92007-12-17 20:14:01 -0500809 return ret;
Chris Mason771ed682008-11-06 22:02:51 -0500810}
Chris Masonc8b97812008-10-29 14:49:59 -0400811
Chris Mason771ed682008-11-06 22:02:51 -0500812/*
813 * work queue call back to started compression on a file and pages
814 */
815static noinline void async_cow_start(struct btrfs_work *work)
816{
817 struct async_cow *async_cow;
818 int num_added = 0;
819 async_cow = container_of(work, struct async_cow, work);
820
821 compress_file_range(async_cow->inode, async_cow->locked_page,
822 async_cow->start, async_cow->end, async_cow,
823 &num_added);
824 if (num_added == 0)
825 async_cow->inode = NULL;
826}
827
828/*
829 * work queue call back to submit previously compressed pages
830 */
831static noinline void async_cow_submit(struct btrfs_work *work)
832{
833 struct async_cow *async_cow;
834 struct btrfs_root *root;
835 unsigned long nr_pages;
836
837 async_cow = container_of(work, struct async_cow, work);
838
839 root = async_cow->root;
840 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
841 PAGE_CACHE_SHIFT;
842
843 atomic_sub(nr_pages, &root->fs_info->async_delalloc_pages);
844
845 if (atomic_read(&root->fs_info->async_delalloc_pages) <
846 5 * 1042 * 1024 &&
847 waitqueue_active(&root->fs_info->async_submit_wait))
848 wake_up(&root->fs_info->async_submit_wait);
849
Chris Masond3977122009-01-05 21:25:51 -0500850 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -0500851 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -0500852}
Chris Masonc8b97812008-10-29 14:49:59 -0400853
Chris Mason771ed682008-11-06 22:02:51 -0500854static noinline void async_cow_free(struct btrfs_work *work)
855{
856 struct async_cow *async_cow;
857 async_cow = container_of(work, struct async_cow, work);
858 kfree(async_cow);
859}
860
861static int cow_file_range_async(struct inode *inode, struct page *locked_page,
862 u64 start, u64 end, int *page_started,
863 unsigned long *nr_written)
864{
865 struct async_cow *async_cow;
866 struct btrfs_root *root = BTRFS_I(inode)->root;
867 unsigned long nr_pages;
868 u64 cur_end;
869 int limit = 10 * 1024 * 1042;
870
871 if (!btrfs_test_opt(root, COMPRESS)) {
872 return cow_file_range(inode, locked_page, start, end,
873 page_started, nr_written, 1);
874 }
875
876 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED |
877 EXTENT_DELALLOC, 1, 0, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -0500878 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500879 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
880 async_cow->inode = inode;
881 async_cow->root = root;
882 async_cow->locked_page = locked_page;
883 async_cow->start = start;
884
885 if (btrfs_test_flag(inode, NOCOMPRESS))
886 cur_end = end;
887 else
888 cur_end = min(end, start + 512 * 1024 - 1);
889
890 async_cow->end = cur_end;
891 INIT_LIST_HEAD(&async_cow->extents);
892
893 async_cow->work.func = async_cow_start;
894 async_cow->work.ordered_func = async_cow_submit;
895 async_cow->work.ordered_free = async_cow_free;
896 async_cow->work.flags = 0;
897
Chris Mason771ed682008-11-06 22:02:51 -0500898 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
899 PAGE_CACHE_SHIFT;
900 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
901
902 btrfs_queue_worker(&root->fs_info->delalloc_workers,
903 &async_cow->work);
904
905 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
906 wait_event(root->fs_info->async_submit_wait,
907 (atomic_read(&root->fs_info->async_delalloc_pages) <
908 limit));
909 }
910
Chris Masond3977122009-01-05 21:25:51 -0500911 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -0500912 atomic_read(&root->fs_info->async_delalloc_pages)) {
913 wait_event(root->fs_info->async_submit_wait,
914 (atomic_read(&root->fs_info->async_delalloc_pages) ==
915 0));
916 }
917
918 *nr_written += nr_pages;
919 start = cur_end + 1;
920 }
921 *page_started = 1;
922 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -0500923}
924
Chris Masond3977122009-01-05 21:25:51 -0500925static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -0500926 u64 bytenr, u64 num_bytes)
927{
928 int ret;
929 struct btrfs_ordered_sum *sums;
930 LIST_HEAD(list);
931
Yan Zheng07d400a2009-01-06 11:42:00 -0500932 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
933 bytenr + num_bytes - 1, &list);
Yan Zheng17d217f2008-12-12 10:03:38 -0500934 if (ret == 0 && list_empty(&list))
935 return 0;
936
937 while (!list_empty(&list)) {
938 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
939 list_del(&sums->list);
940 kfree(sums);
941 }
942 return 1;
943}
944
Chris Masond352ac62008-09-29 15:18:18 -0400945/*
946 * when nowcow writeback call back. This checks for snapshots or COW copies
947 * of the extents that exist in the file, and COWs the file as required.
948 *
949 * If no cow copies or snapshots exist, we write directly to the existing
950 * blocks on disk
951 */
Chris Masonc8b97812008-10-29 14:49:59 -0400952static int run_delalloc_nocow(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -0500953 u64 start, u64 end, int *page_started, int force,
954 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -0500955{
Chris Masonbe20aa92007-12-17 20:14:01 -0500956 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -0400957 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -0500958 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -0500959 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -0400960 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -0500961 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -0400962 u64 cow_start;
963 u64 cur_offset;
964 u64 extent_end;
965 u64 disk_bytenr;
966 u64 num_bytes;
967 int extent_type;
968 int ret;
Yan Zhengd899e052008-10-30 14:25:28 -0400969 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -0400970 int nocow;
971 int check_prev = 1;
Chris Masonbe20aa92007-12-17 20:14:01 -0500972
973 path = btrfs_alloc_path();
974 BUG_ON(!path);
Yan Zheng7ea394f2008-08-05 13:05:02 -0400975 trans = btrfs_join_transaction(root, 1);
976 BUG_ON(!trans);
Chris Masonbe20aa92007-12-17 20:14:01 -0500977
Yan Zheng80ff3852008-10-30 14:20:02 -0400978 cow_start = (u64)-1;
979 cur_offset = start;
980 while (1) {
981 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
982 cur_offset, 0);
983 BUG_ON(ret < 0);
984 if (ret > 0 && path->slots[0] > 0 && check_prev) {
985 leaf = path->nodes[0];
986 btrfs_item_key_to_cpu(leaf, &found_key,
987 path->slots[0] - 1);
988 if (found_key.objectid == inode->i_ino &&
989 found_key.type == BTRFS_EXTENT_DATA_KEY)
990 path->slots[0]--;
991 }
992 check_prev = 0;
993next_slot:
994 leaf = path->nodes[0];
995 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
996 ret = btrfs_next_leaf(root, path);
997 if (ret < 0)
998 BUG_ON(1);
999 if (ret > 0)
1000 break;
1001 leaf = path->nodes[0];
1002 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001003
Yan Zheng80ff3852008-10-30 14:20:02 -04001004 nocow = 0;
1005 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001006 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001007 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001008
Yan Zheng80ff3852008-10-30 14:20:02 -04001009 if (found_key.objectid > inode->i_ino ||
1010 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1011 found_key.offset > end)
1012 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001013
Yan Zheng80ff3852008-10-30 14:20:02 -04001014 if (found_key.offset > cur_offset) {
1015 extent_end = found_key.offset;
1016 goto out_check;
1017 }
Chris Masonc31f8832008-01-08 15:46:31 -05001018
Yan Zheng80ff3852008-10-30 14:20:02 -04001019 fi = btrfs_item_ptr(leaf, path->slots[0],
1020 struct btrfs_file_extent_item);
1021 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001022
Yan Zhengd899e052008-10-30 14:25:28 -04001023 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1024 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001025 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1026 extent_end = found_key.offset +
1027 btrfs_file_extent_num_bytes(leaf, fi);
1028 if (extent_end <= start) {
1029 path->slots[0]++;
1030 goto next_slot;
1031 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001032 if (disk_bytenr == 0)
1033 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001034 if (btrfs_file_extent_compression(leaf, fi) ||
1035 btrfs_file_extent_encryption(leaf, fi) ||
1036 btrfs_file_extent_other_encoding(leaf, fi))
1037 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001038 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1039 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001040 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001041 goto out_check;
Yan Zheng17d217f2008-12-12 10:03:38 -05001042 if (btrfs_cross_ref_exist(trans, root, inode->i_ino,
1043 disk_bytenr))
1044 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001045 disk_bytenr += btrfs_file_extent_offset(leaf, fi);
Yan Zheng17d217f2008-12-12 10:03:38 -05001046 disk_bytenr += cur_offset - found_key.offset;
1047 num_bytes = min(end + 1, extent_end) - cur_offset;
1048 /*
1049 * force cow if csum exists in the range.
1050 * this ensure that csum for a given extent are
1051 * either valid or do not exist.
1052 */
1053 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1054 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001055 nocow = 1;
1056 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1057 extent_end = found_key.offset +
1058 btrfs_file_extent_inline_len(leaf, fi);
1059 extent_end = ALIGN(extent_end, root->sectorsize);
1060 } else {
1061 BUG_ON(1);
1062 }
1063out_check:
1064 if (extent_end <= start) {
1065 path->slots[0]++;
1066 goto next_slot;
1067 }
1068 if (!nocow) {
1069 if (cow_start == (u64)-1)
1070 cow_start = cur_offset;
1071 cur_offset = extent_end;
1072 if (cur_offset > end)
1073 break;
1074 path->slots[0]++;
1075 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001076 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001077
Yan Zheng7ea394f2008-08-05 13:05:02 -04001078 btrfs_release_path(root, path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001079 if (cow_start != (u64)-1) {
1080 ret = cow_file_range(inode, locked_page, cow_start,
Chris Mason771ed682008-11-06 22:02:51 -05001081 found_key.offset - 1, page_started,
1082 nr_written, 1);
Yan Zheng80ff3852008-10-30 14:20:02 -04001083 BUG_ON(ret);
1084 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001085 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001086
Yan Zhengd899e052008-10-30 14:25:28 -04001087 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1088 struct extent_map *em;
1089 struct extent_map_tree *em_tree;
1090 em_tree = &BTRFS_I(inode)->extent_tree;
1091 em = alloc_extent_map(GFP_NOFS);
1092 em->start = cur_offset;
Chris Mason445a6942008-11-10 11:53:33 -05001093 em->orig_start = em->start;
Yan Zhengd899e052008-10-30 14:25:28 -04001094 em->len = num_bytes;
1095 em->block_len = num_bytes;
1096 em->block_start = disk_bytenr;
1097 em->bdev = root->fs_info->fs_devices->latest_bdev;
1098 set_bit(EXTENT_FLAG_PINNED, &em->flags);
1099 while (1) {
1100 spin_lock(&em_tree->lock);
1101 ret = add_extent_mapping(em_tree, em);
1102 spin_unlock(&em_tree->lock);
1103 if (ret != -EEXIST) {
1104 free_extent_map(em);
1105 break;
1106 }
1107 btrfs_drop_extent_cache(inode, em->start,
1108 em->start + em->len - 1, 0);
1109 }
1110 type = BTRFS_ORDERED_PREALLOC;
1111 } else {
1112 type = BTRFS_ORDERED_NOCOW;
1113 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001114
1115 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001116 num_bytes, num_bytes, type);
1117 BUG_ON(ret);
Chris Mason771ed682008-11-06 22:02:51 -05001118
Yan Zhengd899e052008-10-30 14:25:28 -04001119 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
1120 cur_offset, cur_offset + num_bytes - 1,
Chris Mason771ed682008-11-06 22:02:51 -05001121 locked_page, 1, 1, 1, 0, 0, 0);
Yan Zheng80ff3852008-10-30 14:20:02 -04001122 cur_offset = extent_end;
1123 if (cur_offset > end)
1124 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001125 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001126 btrfs_release_path(root, path);
1127
1128 if (cur_offset <= end && cow_start == (u64)-1)
1129 cow_start = cur_offset;
1130 if (cow_start != (u64)-1) {
1131 ret = cow_file_range(inode, locked_page, cow_start, end,
Chris Mason771ed682008-11-06 22:02:51 -05001132 page_started, nr_written, 1);
Yan Zheng80ff3852008-10-30 14:20:02 -04001133 BUG_ON(ret);
1134 }
1135
1136 ret = btrfs_end_transaction(trans, root);
1137 BUG_ON(ret);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001138 btrfs_free_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001139 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001140}
1141
Chris Masond352ac62008-09-29 15:18:18 -04001142/*
1143 * extent_io.c call back to do delayed allocation processing
1144 */
Chris Masonc8b97812008-10-29 14:49:59 -04001145static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001146 u64 start, u64 end, int *page_started,
1147 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001148{
Chris Masonbe20aa92007-12-17 20:14:01 -05001149 int ret;
Chris Masona2135012008-06-25 16:01:30 -04001150
Yan Zheng17d217f2008-12-12 10:03:38 -05001151 if (btrfs_test_flag(inode, NODATACOW))
Chris Masonc8b97812008-10-29 14:49:59 -04001152 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001153 page_started, 1, nr_written);
Yan Zhengd899e052008-10-30 14:25:28 -04001154 else if (btrfs_test_flag(inode, PREALLOC))
1155 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001156 page_started, 0, nr_written);
Chris Masonbe20aa92007-12-17 20:14:01 -05001157 else
Chris Mason771ed682008-11-06 22:02:51 -05001158 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001159 page_started, nr_written);
Chris Mason1832a6d2007-12-21 16:27:21 -05001160
Chris Masonb888db22007-08-27 16:49:44 -04001161 return ret;
1162}
1163
Chris Masond352ac62008-09-29 15:18:18 -04001164/*
1165 * extent_io.c set_bit_hook, used to track delayed allocation
1166 * bytes in this file, and to maintain the list of inodes that
1167 * have pending delalloc work to be done.
1168 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001169static int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -05001170 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -05001171{
Chris Mason75eff682008-12-15 15:54:40 -05001172 /*
1173 * set_bit and clear bit hooks normally require _irqsave/restore
1174 * but in this case, we are only testeing for the DELALLOC
1175 * bit, which is only set or cleared with irqs on
1176 */
Chris Masonb0c68f82008-01-31 11:05:37 -05001177 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001178 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason75eff682008-12-15 15:54:40 -05001179 spin_lock(&root->fs_info->delalloc_lock);
Chris Mason90692182008-02-08 13:49:28 -05001180 BTRFS_I(inode)->delalloc_bytes += end - start + 1;
Chris Mason291d6732008-01-29 15:55:23 -05001181 root->fs_info->delalloc_bytes += end - start + 1;
Chris Masonea8c2812008-08-04 23:17:27 -04001182 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1183 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1184 &root->fs_info->delalloc_inodes);
1185 }
Chris Mason75eff682008-12-15 15:54:40 -05001186 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001187 }
1188 return 0;
1189}
1190
Chris Masond352ac62008-09-29 15:18:18 -04001191/*
1192 * extent_io.c clear_bit_hook, see set_bit_hook for why
1193 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001194static int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -05001195 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -05001196{
Chris Mason75eff682008-12-15 15:54:40 -05001197 /*
1198 * set_bit and clear bit hooks normally require _irqsave/restore
1199 * but in this case, we are only testeing for the DELALLOC
1200 * bit, which is only set or cleared with irqs on
1201 */
Chris Masonb0c68f82008-01-31 11:05:37 -05001202 if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001203 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonbcbfce82008-04-22 13:26:47 -04001204
Chris Mason75eff682008-12-15 15:54:40 -05001205 spin_lock(&root->fs_info->delalloc_lock);
Chris Masonb0c68f82008-01-31 11:05:37 -05001206 if (end - start + 1 > root->fs_info->delalloc_bytes) {
Chris Masond3977122009-01-05 21:25:51 -05001207 printk(KERN_INFO "btrfs warning: delalloc account "
1208 "%llu %llu\n",
1209 (unsigned long long)end - start + 1,
1210 (unsigned long long)
1211 root->fs_info->delalloc_bytes);
Chris Masonb0c68f82008-01-31 11:05:37 -05001212 root->fs_info->delalloc_bytes = 0;
Chris Mason90692182008-02-08 13:49:28 -05001213 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masonb0c68f82008-01-31 11:05:37 -05001214 } else {
1215 root->fs_info->delalloc_bytes -= end - start + 1;
Chris Mason90692182008-02-08 13:49:28 -05001216 BTRFS_I(inode)->delalloc_bytes -= end - start + 1;
Chris Masonb0c68f82008-01-31 11:05:37 -05001217 }
Chris Masonea8c2812008-08-04 23:17:27 -04001218 if (BTRFS_I(inode)->delalloc_bytes == 0 &&
1219 !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1220 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1221 }
Chris Mason75eff682008-12-15 15:54:40 -05001222 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001223 }
1224 return 0;
1225}
1226
Chris Masond352ac62008-09-29 15:18:18 -04001227/*
1228 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1229 * we don't create bios that span stripes or chunks
1230 */
Chris Mason239b14b2008-03-24 15:02:07 -04001231int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001232 size_t size, struct bio *bio,
1233 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001234{
1235 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
1236 struct btrfs_mapping_tree *map_tree;
Chris Masona62b9402008-10-03 16:31:08 -04001237 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001238 u64 length = 0;
1239 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001240 int ret;
1241
Chris Mason771ed682008-11-06 22:02:51 -05001242 if (bio_flags & EXTENT_BIO_COMPRESSED)
1243 return 0;
1244
Chris Masonf2d8d742008-04-21 10:03:05 -04001245 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001246 map_tree = &root->fs_info->mapping_tree;
1247 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04001248 ret = btrfs_map_block(map_tree, READ, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001249 &map_length, NULL, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04001250
Chris Masond3977122009-01-05 21:25:51 -05001251 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001252 return 1;
Chris Mason239b14b2008-03-24 15:02:07 -04001253 return 0;
1254}
1255
Chris Masond352ac62008-09-29 15:18:18 -04001256/*
1257 * in order to insert checksums into the metadata in large chunks,
1258 * we wait until bio submission time. All the pages in the bio are
1259 * checksummed and sums are attached onto the ordered extent record.
1260 *
1261 * At IO completion time the cums attached on the ordered extent record
1262 * are inserted into the btree
1263 */
Chris Masond3977122009-01-05 21:25:51 -05001264static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1265 struct bio *bio, int mirror_num,
1266 unsigned long bio_flags)
Chris Mason065631f2008-02-20 12:07:25 -05001267{
Chris Mason065631f2008-02-20 12:07:25 -05001268 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001269 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001270
Chris Masond20f7042008-12-08 16:58:54 -05001271 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Chris Mason44b8bd72008-04-16 11:14:51 -04001272 BUG_ON(ret);
Chris Mason4a69a412008-11-06 22:03:00 -05001273 return 0;
1274}
Chris Masone0156402008-04-16 11:15:20 -04001275
Chris Mason4a69a412008-11-06 22:03:00 -05001276/*
1277 * in order to insert checksums into the metadata in large chunks,
1278 * we wait until bio submission time. All the pages in the bio are
1279 * checksummed and sums are attached onto the ordered extent record.
1280 *
1281 * At IO completion time the cums attached on the ordered extent record
1282 * are inserted into the btree
1283 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001284static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Mason4a69a412008-11-06 22:03:00 -05001285 int mirror_num, unsigned long bio_flags)
1286{
1287 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason8b712842008-06-11 16:50:36 -04001288 return btrfs_map_bio(root, rw, bio, mirror_num, 1);
Chris Mason44b8bd72008-04-16 11:14:51 -04001289}
1290
Chris Masond352ac62008-09-29 15:18:18 -04001291/*
Chris Masoncad321a2008-12-17 14:51:42 -05001292 * extent_io.c submission hook. This does the right thing for csum calculation
1293 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001294 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001295static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masonc8b97812008-10-29 14:49:59 -04001296 int mirror_num, unsigned long bio_flags)
Chris Mason44b8bd72008-04-16 11:14:51 -04001297{
1298 struct btrfs_root *root = BTRFS_I(inode)->root;
1299 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001300 int skip_sum;
Chris Mason44b8bd72008-04-16 11:14:51 -04001301
Chris Masoncad321a2008-12-17 14:51:42 -05001302 skip_sum = btrfs_test_flag(inode, NODATASUM);
1303
Chris Masone6dcd2d2008-07-17 12:53:50 -04001304 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
1305 BUG_ON(ret);
Chris Mason065631f2008-02-20 12:07:25 -05001306
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001307 if (!(rw & (1 << BIO_RW))) {
Chris Masond20f7042008-12-08 16:58:54 -05001308 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Chris Masonc8b97812008-10-29 14:49:59 -04001309 return btrfs_submit_compressed_read(inode, bio,
1310 mirror_num, bio_flags);
Chris Masond20f7042008-12-08 16:58:54 -05001311 } else if (!skip_sum)
1312 btrfs_lookup_bio_sums(root, inode, bio, NULL);
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001313 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001314 } else if (!skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001315 /* csum items have already been cloned */
1316 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1317 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001318 /* we're doing a write, do the async checksumming */
1319 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001320 inode, rw, bio, mirror_num,
Chris Mason4a69a412008-11-06 22:03:00 -05001321 bio_flags, __btrfs_submit_bio_start,
1322 __btrfs_submit_bio_done);
Chris Mason19b9bdb2008-10-30 14:23:13 -04001323 }
1324
Chris Mason0b86a832008-03-24 15:01:56 -04001325mapit:
Chris Mason8b712842008-06-11 16:50:36 -04001326 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
Chris Mason065631f2008-02-20 12:07:25 -05001327}
Chris Mason6885f302008-02-20 16:11:05 -05001328
Chris Masond352ac62008-09-29 15:18:18 -04001329/*
1330 * given a list of ordered sums record them in the inode. This happens
1331 * at IO completion time based on sums calculated at bio submission time.
1332 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001333static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001334 struct inode *inode, u64 file_offset,
1335 struct list_head *list)
1336{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001337 struct btrfs_ordered_sum *sum;
1338
1339 btrfs_set_trans_block_group(trans, inode);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001340
1341 list_for_each_entry(sum, list, list) {
Chris Masond20f7042008-12-08 16:58:54 -05001342 btrfs_csum_file_blocks(trans,
1343 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001344 }
1345 return 0;
1346}
1347
Chris Masonea8c2812008-08-04 23:17:27 -04001348int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end)
1349{
Chris Masond3977122009-01-05 21:25:51 -05001350 if ((end & (PAGE_CACHE_SIZE - 1)) == 0)
Chris Mason771ed682008-11-06 22:02:51 -05001351 WARN_ON(1);
Chris Masonea8c2812008-08-04 23:17:27 -04001352 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
1353 GFP_NOFS);
1354}
1355
Chris Masond352ac62008-09-29 15:18:18 -04001356/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001357struct btrfs_writepage_fixup {
1358 struct page *page;
1359 struct btrfs_work work;
1360};
1361
Christoph Hellwigb2950862008-12-02 09:54:17 -05001362static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001363{
1364 struct btrfs_writepage_fixup *fixup;
1365 struct btrfs_ordered_extent *ordered;
1366 struct page *page;
1367 struct inode *inode;
1368 u64 page_start;
1369 u64 page_end;
1370
1371 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1372 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001373again:
Chris Mason247e7432008-07-17 12:53:51 -04001374 lock_page(page);
1375 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1376 ClearPageChecked(page);
1377 goto out_page;
1378 }
1379
1380 inode = page->mapping->host;
1381 page_start = page_offset(page);
1382 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1383
1384 lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001385
1386 /* already ordered? We're done */
1387 if (test_range_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
1388 EXTENT_ORDERED, 0)) {
Chris Mason247e7432008-07-17 12:53:51 -04001389 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001390 }
1391
1392 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1393 if (ordered) {
1394 unlock_extent(&BTRFS_I(inode)->io_tree, page_start,
1395 page_end, GFP_NOFS);
1396 unlock_page(page);
1397 btrfs_start_ordered_extent(inode, ordered, 1);
1398 goto again;
1399 }
Chris Mason247e7432008-07-17 12:53:51 -04001400
Chris Masonea8c2812008-08-04 23:17:27 -04001401 btrfs_set_extent_delalloc(inode, page_start, page_end);
Chris Mason247e7432008-07-17 12:53:51 -04001402 ClearPageChecked(page);
1403out:
1404 unlock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
1405out_page:
1406 unlock_page(page);
1407 page_cache_release(page);
1408}
1409
1410/*
1411 * There are a few paths in the higher layers of the kernel that directly
1412 * set the page dirty bit without asking the filesystem if it is a
1413 * good idea. This causes problems because we want to make sure COW
1414 * properly happens and the data=ordered rules are followed.
1415 *
Chris Masonc8b97812008-10-29 14:49:59 -04001416 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001417 * hasn't been properly setup for IO. We kick off an async process
1418 * to fix it up. The async helper will wait for ordered extents, set
1419 * the delalloc bit and make it safe to write the page.
1420 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001421static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001422{
1423 struct inode *inode = page->mapping->host;
1424 struct btrfs_writepage_fixup *fixup;
1425 struct btrfs_root *root = BTRFS_I(inode)->root;
1426 int ret;
1427
1428 ret = test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1429 EXTENT_ORDERED, 0);
1430 if (ret)
1431 return 0;
1432
1433 if (PageChecked(page))
1434 return -EAGAIN;
1435
1436 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1437 if (!fixup)
1438 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001439
Chris Mason247e7432008-07-17 12:53:51 -04001440 SetPageChecked(page);
1441 page_cache_get(page);
1442 fixup->work.func = btrfs_writepage_fixup_worker;
1443 fixup->page = page;
1444 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
1445 return -EAGAIN;
1446}
1447
Yan Zhengd899e052008-10-30 14:25:28 -04001448static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1449 struct inode *inode, u64 file_pos,
1450 u64 disk_bytenr, u64 disk_num_bytes,
1451 u64 num_bytes, u64 ram_bytes,
1452 u8 compression, u8 encryption,
1453 u16 other_encoding, int extent_type)
1454{
1455 struct btrfs_root *root = BTRFS_I(inode)->root;
1456 struct btrfs_file_extent_item *fi;
1457 struct btrfs_path *path;
1458 struct extent_buffer *leaf;
1459 struct btrfs_key ins;
1460 u64 hint;
1461 int ret;
1462
1463 path = btrfs_alloc_path();
1464 BUG_ON(!path);
1465
1466 ret = btrfs_drop_extents(trans, root, inode, file_pos,
1467 file_pos + num_bytes, file_pos, &hint);
1468 BUG_ON(ret);
1469
1470 ins.objectid = inode->i_ino;
1471 ins.offset = file_pos;
1472 ins.type = BTRFS_EXTENT_DATA_KEY;
1473 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
1474 BUG_ON(ret);
1475 leaf = path->nodes[0];
1476 fi = btrfs_item_ptr(leaf, path->slots[0],
1477 struct btrfs_file_extent_item);
1478 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1479 btrfs_set_file_extent_type(leaf, fi, extent_type);
1480 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1481 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1482 btrfs_set_file_extent_offset(leaf, fi, 0);
1483 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1484 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1485 btrfs_set_file_extent_compression(leaf, fi, compression);
1486 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1487 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
1488 btrfs_mark_buffer_dirty(leaf);
1489
1490 inode_add_bytes(inode, num_bytes);
1491 btrfs_drop_extent_cache(inode, file_pos, file_pos + num_bytes - 1, 0);
1492
1493 ins.objectid = disk_bytenr;
1494 ins.offset = disk_num_bytes;
1495 ins.type = BTRFS_EXTENT_ITEM_KEY;
1496 ret = btrfs_alloc_reserved_extent(trans, root, leaf->start,
1497 root->root_key.objectid,
1498 trans->transid, inode->i_ino, &ins);
1499 BUG_ON(ret);
1500
1501 btrfs_free_path(path);
1502 return 0;
1503}
1504
Chris Masond352ac62008-09-29 15:18:18 -04001505/* as ordered data IO finishes, this gets called so we can finish
1506 * an ordered extent if the range of bytes in the file it covers are
1507 * fully written.
1508 */
Chris Mason211f90e2008-07-18 11:56:15 -04001509static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001510{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001511 struct btrfs_root *root = BTRFS_I(inode)->root;
1512 struct btrfs_trans_handle *trans;
1513 struct btrfs_ordered_extent *ordered_extent;
1514 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan Zhengd899e052008-10-30 14:25:28 -04001515 int compressed = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001516 int ret;
1517
1518 ret = btrfs_dec_test_ordered_pending(inode, start, end - start + 1);
Chris Masonba1da2f2008-07-17 12:54:15 -04001519 if (!ret)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001520 return 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001521
Chris Masonf9295742008-07-17 12:54:14 -04001522 trans = btrfs_join_transaction(root, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001523
1524 ordered_extent = btrfs_lookup_ordered_extent(inode, start);
1525 BUG_ON(!ordered_extent);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001526 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags))
1527 goto nocow;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001528
1529 lock_extent(io_tree, ordered_extent->file_offset,
1530 ordered_extent->file_offset + ordered_extent->len - 1,
1531 GFP_NOFS);
1532
Chris Masonc8b97812008-10-29 14:49:59 -04001533 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Yan Zhengd899e052008-10-30 14:25:28 -04001534 compressed = 1;
1535 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
1536 BUG_ON(compressed);
1537 ret = btrfs_mark_extent_written(trans, root, inode,
1538 ordered_extent->file_offset,
1539 ordered_extent->file_offset +
1540 ordered_extent->len);
1541 BUG_ON(ret);
1542 } else {
1543 ret = insert_reserved_file_extent(trans, inode,
1544 ordered_extent->file_offset,
1545 ordered_extent->start,
1546 ordered_extent->disk_len,
1547 ordered_extent->len,
1548 ordered_extent->len,
1549 compressed, 0, 0,
1550 BTRFS_FILE_EXTENT_REG);
1551 BUG_ON(ret);
1552 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04001553 unlock_extent(io_tree, ordered_extent->file_offset,
1554 ordered_extent->file_offset + ordered_extent->len - 1,
1555 GFP_NOFS);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001556nocow:
Chris Masone6dcd2d2008-07-17 12:53:50 -04001557 add_pending_csums(trans, inode, ordered_extent->file_offset,
1558 &ordered_extent->list);
1559
Chris Mason34353022008-09-23 20:19:49 -04001560 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Chris Masondbe674a2008-07-17 12:54:05 -04001561 btrfs_ordered_update_i_size(inode, ordered_extent);
Chris Masone02119d2008-09-05 16:13:11 -04001562 btrfs_update_inode(trans, root, inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001563 btrfs_remove_ordered_extent(inode, ordered_extent);
Chris Mason34353022008-09-23 20:19:49 -04001564 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
Chris Mason7f3c74f2008-07-18 12:01:11 -04001565
Chris Masone6dcd2d2008-07-17 12:53:50 -04001566 /* once for us */
1567 btrfs_put_ordered_extent(ordered_extent);
1568 /* once for the tree */
1569 btrfs_put_ordered_extent(ordered_extent);
1570
Chris Masone6dcd2d2008-07-17 12:53:50 -04001571 btrfs_end_transaction(trans, root);
1572 return 0;
1573}
1574
Christoph Hellwigb2950862008-12-02 09:54:17 -05001575static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04001576 struct extent_state *state, int uptodate)
1577{
1578 return btrfs_finish_ordered_io(page->mapping->host, start, end);
1579}
1580
Chris Masond352ac62008-09-29 15:18:18 -04001581/*
1582 * When IO fails, either with EIO or csum verification fails, we
1583 * try other mirrors that might have a good copy of the data. This
1584 * io_failure_record is used to record state as we go through all the
1585 * mirrors. If another mirror has good data, the page is set up to date
1586 * and things continue. If a good mirror can't be found, the original
1587 * bio end_io callback is called to indicate things have failed.
1588 */
Chris Mason7e383262008-04-09 16:28:12 -04001589struct io_failure_record {
1590 struct page *page;
1591 u64 start;
1592 u64 len;
1593 u64 logical;
Chris Masond20f7042008-12-08 16:58:54 -05001594 unsigned long bio_flags;
Chris Mason7e383262008-04-09 16:28:12 -04001595 int last_mirror;
1596};
1597
Christoph Hellwigb2950862008-12-02 09:54:17 -05001598static int btrfs_io_failed_hook(struct bio *failed_bio,
Chris Mason1259ab72008-05-12 13:39:03 -04001599 struct page *page, u64 start, u64 end,
1600 struct extent_state *state)
Chris Mason7e383262008-04-09 16:28:12 -04001601{
1602 struct io_failure_record *failrec = NULL;
1603 u64 private;
1604 struct extent_map *em;
1605 struct inode *inode = page->mapping->host;
1606 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Chris Mason3b951512008-04-17 11:29:12 -04001607 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason7e383262008-04-09 16:28:12 -04001608 struct bio *bio;
1609 int num_copies;
1610 int ret;
Chris Mason1259ab72008-05-12 13:39:03 -04001611 int rw;
Chris Mason7e383262008-04-09 16:28:12 -04001612 u64 logical;
1613
1614 ret = get_state_private(failure_tree, start, &private);
1615 if (ret) {
Chris Mason7e383262008-04-09 16:28:12 -04001616 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
1617 if (!failrec)
1618 return -ENOMEM;
1619 failrec->start = start;
1620 failrec->len = end - start + 1;
1621 failrec->last_mirror = 0;
Chris Masond20f7042008-12-08 16:58:54 -05001622 failrec->bio_flags = 0;
Chris Mason7e383262008-04-09 16:28:12 -04001623
Chris Mason3b951512008-04-17 11:29:12 -04001624 spin_lock(&em_tree->lock);
1625 em = lookup_extent_mapping(em_tree, start, failrec->len);
1626 if (em->start > start || em->start + em->len < start) {
1627 free_extent_map(em);
1628 em = NULL;
1629 }
1630 spin_unlock(&em_tree->lock);
Chris Mason7e383262008-04-09 16:28:12 -04001631
1632 if (!em || IS_ERR(em)) {
1633 kfree(failrec);
1634 return -EIO;
1635 }
1636 logical = start - em->start;
1637 logical = em->block_start + logical;
Chris Masond20f7042008-12-08 16:58:54 -05001638 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
1639 logical = em->block_start;
1640 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
1641 }
Chris Mason7e383262008-04-09 16:28:12 -04001642 failrec->logical = logical;
1643 free_extent_map(em);
1644 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
1645 EXTENT_DIRTY, GFP_NOFS);
Chris Mason587f7702008-04-11 12:16:46 -04001646 set_state_private(failure_tree, start,
1647 (u64)(unsigned long)failrec);
Chris Mason7e383262008-04-09 16:28:12 -04001648 } else {
Chris Mason587f7702008-04-11 12:16:46 -04001649 failrec = (struct io_failure_record *)(unsigned long)private;
Chris Mason7e383262008-04-09 16:28:12 -04001650 }
1651 num_copies = btrfs_num_copies(
1652 &BTRFS_I(inode)->root->fs_info->mapping_tree,
1653 failrec->logical, failrec->len);
1654 failrec->last_mirror++;
1655 if (!state) {
Chris Masoncad321a2008-12-17 14:51:42 -05001656 spin_lock(&BTRFS_I(inode)->io_tree.lock);
Chris Mason7e383262008-04-09 16:28:12 -04001657 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
1658 failrec->start,
1659 EXTENT_LOCKED);
1660 if (state && state->start != failrec->start)
1661 state = NULL;
Chris Masoncad321a2008-12-17 14:51:42 -05001662 spin_unlock(&BTRFS_I(inode)->io_tree.lock);
Chris Mason7e383262008-04-09 16:28:12 -04001663 }
1664 if (!state || failrec->last_mirror > num_copies) {
1665 set_state_private(failure_tree, failrec->start, 0);
1666 clear_extent_bits(failure_tree, failrec->start,
1667 failrec->start + failrec->len - 1,
1668 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
1669 kfree(failrec);
1670 return -EIO;
1671 }
1672 bio = bio_alloc(GFP_NOFS, 1);
1673 bio->bi_private = state;
1674 bio->bi_end_io = failed_bio->bi_end_io;
1675 bio->bi_sector = failrec->logical >> 9;
1676 bio->bi_bdev = failed_bio->bi_bdev;
Chris Masone1c4b742008-04-22 13:26:46 -04001677 bio->bi_size = 0;
Chris Masond20f7042008-12-08 16:58:54 -05001678
Chris Mason7e383262008-04-09 16:28:12 -04001679 bio_add_page(bio, page, failrec->len, start - page_offset(page));
Chris Mason1259ab72008-05-12 13:39:03 -04001680 if (failed_bio->bi_rw & (1 << BIO_RW))
1681 rw = WRITE;
1682 else
1683 rw = READ;
1684
1685 BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
Chris Masonc8b97812008-10-29 14:49:59 -04001686 failrec->last_mirror,
Chris Masond20f7042008-12-08 16:58:54 -05001687 failrec->bio_flags);
Chris Mason1259ab72008-05-12 13:39:03 -04001688 return 0;
1689}
1690
Chris Masond352ac62008-09-29 15:18:18 -04001691/*
1692 * each time an IO finishes, we do a fast check in the IO failure tree
1693 * to see if we need to process or clean up an io_failure_record
1694 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001695static int btrfs_clean_io_failures(struct inode *inode, u64 start)
Chris Mason1259ab72008-05-12 13:39:03 -04001696{
1697 u64 private;
1698 u64 private_failure;
1699 struct io_failure_record *failure;
1700 int ret;
1701
1702 private = 0;
1703 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
1704 (u64)-1, 1, EXTENT_DIRTY)) {
1705 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
1706 start, &private_failure);
1707 if (ret == 0) {
1708 failure = (struct io_failure_record *)(unsigned long)
1709 private_failure;
1710 set_state_private(&BTRFS_I(inode)->io_failure_tree,
1711 failure->start, 0);
1712 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
1713 failure->start,
1714 failure->start + failure->len - 1,
1715 EXTENT_DIRTY | EXTENT_LOCKED,
1716 GFP_NOFS);
1717 kfree(failure);
1718 }
1719 }
Chris Mason7e383262008-04-09 16:28:12 -04001720 return 0;
1721}
1722
Chris Masond352ac62008-09-29 15:18:18 -04001723/*
1724 * when reads are done, we need to check csums to verify the data is correct
1725 * if there's a match, we allow the bio to finish. If not, we go through
1726 * the io_failure_record routines to find good copies
1727 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001728static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason70dec802008-01-29 09:59:12 -05001729 struct extent_state *state)
Chris Mason07157aa2007-08-30 08:50:51 -04001730{
Chris Mason35ebb932007-10-30 16:56:53 -04001731 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -04001732 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05001733 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04001734 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -05001735 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -04001736 int ret;
Chris Masonff79f812007-10-15 16:22:25 -04001737 struct btrfs_root *root = BTRFS_I(inode)->root;
1738 u32 csum = ~(u32)0;
Chris Masond1310b22008-01-24 16:13:08 -05001739
Chris Masond20f7042008-12-08 16:58:54 -05001740 if (PageChecked(page)) {
1741 ClearPageChecked(page);
1742 goto good;
1743 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001744 if (btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -05001745 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05001746
Yan Zheng17d217f2008-12-12 10:03:38 -05001747 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
1748 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1)) {
1749 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
1750 GFP_NOFS);
1751 return 0;
1752 }
1753
Yanc2e639f2008-02-04 08:57:25 -05001754 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -05001755 private = state->private;
1756 ret = 0;
1757 } else {
1758 ret = get_state_private(io_tree, start, &private);
1759 }
Chris Mason9ab86c82009-01-07 09:48:51 -05001760 kaddr = kmap_atomic(page, KM_USER0);
Chris Masond3977122009-01-05 21:25:51 -05001761 if (ret)
Chris Mason07157aa2007-08-30 08:50:51 -04001762 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05001763
Chris Masonff79f812007-10-15 16:22:25 -04001764 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
1765 btrfs_csum_final(csum, (char *)&csum);
Chris Masond3977122009-01-05 21:25:51 -05001766 if (csum != private)
Chris Mason07157aa2007-08-30 08:50:51 -04001767 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05001768
Chris Mason9ab86c82009-01-07 09:48:51 -05001769 kunmap_atomic(kaddr, KM_USER0);
Chris Masond20f7042008-12-08 16:58:54 -05001770good:
Chris Mason7e383262008-04-09 16:28:12 -04001771 /* if the io failure tree for this inode is non-empty,
1772 * check to see if we've recovered from a failed IO
1773 */
Chris Mason1259ab72008-05-12 13:39:03 -04001774 btrfs_clean_io_failures(inode, start);
Chris Mason07157aa2007-08-30 08:50:51 -04001775 return 0;
1776
1777zeroit:
Chris Masond3977122009-01-05 21:25:51 -05001778 printk(KERN_INFO "btrfs csum failed ino %lu off %llu csum %u "
1779 "private %llu\n", page->mapping->host->i_ino,
1780 (unsigned long long)start, csum,
1781 (unsigned long long)private);
Chris Masondb945352007-10-15 16:15:53 -04001782 memset(kaddr + offset, 1, end - start + 1);
1783 flush_dcache_page(page);
Chris Mason9ab86c82009-01-07 09:48:51 -05001784 kunmap_atomic(kaddr, KM_USER0);
Chris Mason3b951512008-04-17 11:29:12 -04001785 if (private == 0)
1786 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04001787 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04001788}
Chris Masonb888db22007-08-27 16:49:44 -04001789
Josef Bacik7b128762008-07-24 12:17:14 -04001790/*
1791 * This creates an orphan entry for the given inode in case something goes
1792 * wrong in the middle of an unlink/truncate.
1793 */
1794int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
1795{
1796 struct btrfs_root *root = BTRFS_I(inode)->root;
1797 int ret = 0;
1798
Yanbcc63ab2008-07-30 16:29:20 -04001799 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001800
1801 /* already on the orphan list, we're good */
1802 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
Yanbcc63ab2008-07-30 16:29:20 -04001803 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001804 return 0;
1805 }
1806
1807 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
1808
Yanbcc63ab2008-07-30 16:29:20 -04001809 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001810
1811 /*
1812 * insert an orphan item to track this unlinked/truncated file
1813 */
1814 ret = btrfs_insert_orphan_item(trans, root, inode->i_ino);
1815
1816 return ret;
1817}
1818
1819/*
1820 * We have done the truncate/delete so we can go ahead and remove the orphan
1821 * item for this particular inode.
1822 */
1823int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
1824{
1825 struct btrfs_root *root = BTRFS_I(inode)->root;
1826 int ret = 0;
1827
Yanbcc63ab2008-07-30 16:29:20 -04001828 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001829
1830 if (list_empty(&BTRFS_I(inode)->i_orphan)) {
Yanbcc63ab2008-07-30 16:29:20 -04001831 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001832 return 0;
1833 }
1834
1835 list_del_init(&BTRFS_I(inode)->i_orphan);
1836 if (!trans) {
Yanbcc63ab2008-07-30 16:29:20 -04001837 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001838 return 0;
1839 }
1840
Yanbcc63ab2008-07-30 16:29:20 -04001841 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001842
1843 ret = btrfs_del_orphan_item(trans, root, inode->i_ino);
1844
1845 return ret;
1846}
1847
1848/*
1849 * this cleans up any orphans that may be left on the list from the last use
1850 * of this root.
1851 */
1852void btrfs_orphan_cleanup(struct btrfs_root *root)
1853{
1854 struct btrfs_path *path;
1855 struct extent_buffer *leaf;
1856 struct btrfs_item *item;
1857 struct btrfs_key key, found_key;
1858 struct btrfs_trans_handle *trans;
1859 struct inode *inode;
1860 int ret = 0, nr_unlink = 0, nr_truncate = 0;
1861
Josef Bacik7b128762008-07-24 12:17:14 -04001862 path = btrfs_alloc_path();
1863 if (!path)
1864 return;
1865 path->reada = -1;
1866
1867 key.objectid = BTRFS_ORPHAN_OBJECTID;
1868 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
1869 key.offset = (u64)-1;
1870
Josef Bacik7b128762008-07-24 12:17:14 -04001871
1872 while (1) {
1873 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1874 if (ret < 0) {
1875 printk(KERN_ERR "Error searching slot for orphan: %d"
1876 "\n", ret);
1877 break;
1878 }
1879
1880 /*
1881 * if ret == 0 means we found what we were searching for, which
1882 * is weird, but possible, so only screw with path if we didnt
1883 * find the key and see if we have stuff that matches
1884 */
1885 if (ret > 0) {
1886 if (path->slots[0] == 0)
1887 break;
1888 path->slots[0]--;
1889 }
1890
1891 /* pull out the item */
1892 leaf = path->nodes[0];
1893 item = btrfs_item_nr(leaf, path->slots[0]);
1894 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1895
1896 /* make sure the item matches what we want */
1897 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
1898 break;
1899 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
1900 break;
1901
1902 /* release the path since we're done with it */
1903 btrfs_release_path(root, path);
1904
1905 /*
1906 * this is where we are basically btrfs_lookup, without the
1907 * crossing root thing. we store the inode number in the
1908 * offset of the orphan item.
1909 */
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001910 inode = btrfs_iget_locked(root->fs_info->sb,
Josef Bacik7b128762008-07-24 12:17:14 -04001911 found_key.offset, root);
1912 if (!inode)
1913 break;
1914
1915 if (inode->i_state & I_NEW) {
1916 BTRFS_I(inode)->root = root;
1917
1918 /* have to set the location manually */
1919 BTRFS_I(inode)->location.objectid = inode->i_ino;
1920 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
1921 BTRFS_I(inode)->location.offset = 0;
1922
1923 btrfs_read_locked_inode(inode);
1924 unlock_new_inode(inode);
1925 }
1926
1927 /*
1928 * add this inode to the orphan list so btrfs_orphan_del does
1929 * the proper thing when we hit it
1930 */
Yanbcc63ab2008-07-30 16:29:20 -04001931 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001932 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
Yanbcc63ab2008-07-30 16:29:20 -04001933 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001934
1935 /*
1936 * if this is a bad inode, means we actually succeeded in
1937 * removing the inode, but not the orphan record, which means
1938 * we need to manually delete the orphan since iput will just
1939 * do a destroy_inode
1940 */
1941 if (is_bad_inode(inode)) {
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001942 trans = btrfs_start_transaction(root, 1);
Josef Bacik7b128762008-07-24 12:17:14 -04001943 btrfs_orphan_del(trans, inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001944 btrfs_end_transaction(trans, root);
Josef Bacik7b128762008-07-24 12:17:14 -04001945 iput(inode);
1946 continue;
1947 }
1948
1949 /* if we have links, this was a truncate, lets do that */
1950 if (inode->i_nlink) {
1951 nr_truncate++;
1952 btrfs_truncate(inode);
1953 } else {
1954 nr_unlink++;
1955 }
1956
1957 /* this will do delete_inode and everything for us */
1958 iput(inode);
1959 }
1960
1961 if (nr_unlink)
1962 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
1963 if (nr_truncate)
1964 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
1965
1966 btrfs_free_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04001967}
1968
Chris Masond352ac62008-09-29 15:18:18 -04001969/*
1970 * read an inode from the btree into the in-memory inode
1971 */
Chris Mason39279cc2007-06-12 06:35:45 -04001972void btrfs_read_locked_inode(struct inode *inode)
1973{
1974 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04001975 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001976 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04001977 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04001978 struct btrfs_root *root = BTRFS_I(inode)->root;
1979 struct btrfs_key location;
1980 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -04001981 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04001982 int ret;
1983
1984 path = btrfs_alloc_path();
1985 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -04001986 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05001987
Chris Mason39279cc2007-06-12 06:35:45 -04001988 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001989 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04001990 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04001991
Chris Mason5f39d392007-10-15 16:14:19 -04001992 leaf = path->nodes[0];
1993 inode_item = btrfs_item_ptr(leaf, path->slots[0],
1994 struct btrfs_inode_item);
1995
1996 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
1997 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
1998 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
1999 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
Chris Masondbe674a2008-07-17 12:54:05 -04002000 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04002001
2002 tspec = btrfs_inode_atime(inode_item);
2003 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2004 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2005
2006 tspec = btrfs_inode_mtime(inode_item);
2007 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2008 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2009
2010 tspec = btrfs_inode_ctime(inode_item);
2011 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2012 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2013
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002014 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04002015 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Chris Masonc3027eb2008-12-08 16:40:21 -05002016 BTRFS_I(inode)->sequence = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04002017 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04002018 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002019 rdev = btrfs_inode_rdev(leaf, inode_item);
2020
Josef Bacikaec74772008-07-24 12:12:38 -04002021 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05002022 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Josef Bacikaec74772008-07-24 12:12:38 -04002023
Chris Mason5f39d392007-10-15 16:14:19 -04002024 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002025
Yan Zhengd2fb3432008-12-11 16:30:39 -05002026 BTRFS_I(inode)->block_group = btrfs_find_block_group(root, 0,
2027 alloc_group_block, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002028 btrfs_free_path(path);
2029 inode_item = NULL;
2030
Chris Mason39279cc2007-06-12 06:35:45 -04002031 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04002032 case S_IFREG:
2033 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04002034 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05002035 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002036 inode->i_fop = &btrfs_file_operations;
2037 inode->i_op = &btrfs_file_inode_operations;
2038 break;
2039 case S_IFDIR:
2040 inode->i_fop = &btrfs_dir_file_operations;
2041 if (root == root->fs_info->tree_root)
2042 inode->i_op = &btrfs_dir_ro_inode_operations;
2043 else
2044 inode->i_op = &btrfs_dir_inode_operations;
2045 break;
2046 case S_IFLNK:
2047 inode->i_op = &btrfs_symlink_inode_operations;
2048 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04002049 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04002050 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04002051 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05002052 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04002053 init_special_inode(inode, inode->i_mode, rdev);
2054 break;
Chris Mason39279cc2007-06-12 06:35:45 -04002055 }
2056 return;
2057
2058make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04002059 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002060 make_bad_inode(inode);
2061}
2062
Chris Masond352ac62008-09-29 15:18:18 -04002063/*
2064 * given a leaf and an inode, copy the inode fields into the leaf
2065 */
Chris Masone02119d2008-09-05 16:13:11 -04002066static void fill_inode_item(struct btrfs_trans_handle *trans,
2067 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04002068 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04002069 struct inode *inode)
2070{
Chris Mason5f39d392007-10-15 16:14:19 -04002071 btrfs_set_inode_uid(leaf, item, inode->i_uid);
2072 btrfs_set_inode_gid(leaf, item, inode->i_gid);
Chris Masondbe674a2008-07-17 12:54:05 -04002073 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
Chris Mason5f39d392007-10-15 16:14:19 -04002074 btrfs_set_inode_mode(leaf, item, inode->i_mode);
2075 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
2076
2077 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
2078 inode->i_atime.tv_sec);
2079 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
2080 inode->i_atime.tv_nsec);
2081
2082 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
2083 inode->i_mtime.tv_sec);
2084 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
2085 inode->i_mtime.tv_nsec);
2086
2087 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
2088 inode->i_ctime.tv_sec);
2089 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
2090 inode->i_ctime.tv_nsec);
2091
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002092 btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode));
Chris Masone02119d2008-09-05 16:13:11 -04002093 btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
Chris Masonc3027eb2008-12-08 16:40:21 -05002094 btrfs_set_inode_sequence(leaf, item, BTRFS_I(inode)->sequence);
Chris Masone02119d2008-09-05 16:13:11 -04002095 btrfs_set_inode_transid(leaf, item, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002096 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -05002097 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002098 btrfs_set_inode_block_group(leaf, item, BTRFS_I(inode)->block_group);
Chris Mason39279cc2007-06-12 06:35:45 -04002099}
2100
Chris Masond352ac62008-09-29 15:18:18 -04002101/*
2102 * copy everything in the in-memory inode into the btree.
2103 */
Chris Masond3977122009-01-05 21:25:51 -05002104noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
2105 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002106{
2107 struct btrfs_inode_item *inode_item;
2108 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002109 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002110 int ret;
2111
2112 path = btrfs_alloc_path();
2113 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -04002114 ret = btrfs_lookup_inode(trans, root, path,
2115 &BTRFS_I(inode)->location, 1);
2116 if (ret) {
2117 if (ret > 0)
2118 ret = -ENOENT;
2119 goto failed;
2120 }
2121
Chris Masonb4ce94d2009-02-04 09:25:08 -05002122 btrfs_unlock_up_safe(path, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002123 leaf = path->nodes[0];
2124 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04002125 struct btrfs_inode_item);
2126
Chris Masone02119d2008-09-05 16:13:11 -04002127 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002128 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002129 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002130 ret = 0;
2131failed:
Chris Mason39279cc2007-06-12 06:35:45 -04002132 btrfs_free_path(path);
2133 return ret;
2134}
2135
2136
Chris Masond352ac62008-09-29 15:18:18 -04002137/*
2138 * unlink helper that gets used here in inode.c and in the tree logging
2139 * recovery code. It remove a link in a directory with a given name, and
2140 * also drops the back refs in the inode to the directory
2141 */
Chris Masone02119d2008-09-05 16:13:11 -04002142int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2143 struct btrfs_root *root,
2144 struct inode *dir, struct inode *inode,
2145 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04002146{
2147 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04002148 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002149 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002150 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04002151 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04002152 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04002153
2154 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04002155 if (!path) {
2156 ret = -ENOMEM;
2157 goto err;
2158 }
2159
Chris Mason39279cc2007-06-12 06:35:45 -04002160 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
2161 name, name_len, -1);
2162 if (IS_ERR(di)) {
2163 ret = PTR_ERR(di);
2164 goto err;
2165 }
2166 if (!di) {
2167 ret = -ENOENT;
2168 goto err;
2169 }
Chris Mason5f39d392007-10-15 16:14:19 -04002170 leaf = path->nodes[0];
2171 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04002172 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04002173 if (ret)
2174 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -04002175 btrfs_release_path(root, path);
2176
Josef Bacikaec74772008-07-24 12:12:38 -04002177 ret = btrfs_del_inode_ref(trans, root, name, name_len,
Chris Masone02119d2008-09-05 16:13:11 -04002178 inode->i_ino,
2179 dir->i_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04002180 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05002181 printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
Josef Bacikaec74772008-07-24 12:12:38 -04002182 "inode %lu parent %lu\n", name_len, name,
Chris Masone02119d2008-09-05 16:13:11 -04002183 inode->i_ino, dir->i_ino);
Josef Bacikaec74772008-07-24 12:12:38 -04002184 goto err;
2185 }
2186
Chris Mason39279cc2007-06-12 06:35:45 -04002187 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04002188 index, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -04002189 if (IS_ERR(di)) {
2190 ret = PTR_ERR(di);
2191 goto err;
2192 }
2193 if (!di) {
2194 ret = -ENOENT;
2195 goto err;
2196 }
2197 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason925baed2008-06-25 16:01:30 -04002198 btrfs_release_path(root, path);
Chris Mason39279cc2007-06-12 06:35:45 -04002199
Chris Masone02119d2008-09-05 16:13:11 -04002200 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
2201 inode, dir->i_ino);
Chris Mason49eb7e42008-09-11 15:53:12 -04002202 BUG_ON(ret != 0 && ret != -ENOENT);
2203 if (ret != -ENOENT)
2204 BTRFS_I(dir)->log_dirty_trans = trans->transid;
Chris Masone02119d2008-09-05 16:13:11 -04002205
2206 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
2207 dir, index);
2208 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04002209err:
2210 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002211 if (ret)
2212 goto out;
2213
2214 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
2215 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2216 btrfs_update_inode(trans, root, dir);
2217 btrfs_drop_nlink(inode);
2218 ret = btrfs_update_inode(trans, root, inode);
2219 dir->i_sb->s_dirt = 1;
2220out:
Chris Mason39279cc2007-06-12 06:35:45 -04002221 return ret;
2222}
2223
2224static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
2225{
2226 struct btrfs_root *root;
2227 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04002228 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002229 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -05002230 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002231
2232 root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05002233
2234 ret = btrfs_check_free_space(root, 1, 1);
2235 if (ret)
2236 goto fail;
2237
Chris Mason39279cc2007-06-12 06:35:45 -04002238 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002239
Chris Mason39279cc2007-06-12 06:35:45 -04002240 btrfs_set_trans_block_group(trans, dir);
Chris Masone02119d2008-09-05 16:13:11 -04002241 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
2242 dentry->d_name.name, dentry->d_name.len);
Josef Bacik7b128762008-07-24 12:17:14 -04002243
2244 if (inode->i_nlink == 0)
2245 ret = btrfs_orphan_add(trans, inode);
2246
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002247 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04002248
Chris Mason89ce8a62008-06-25 16:01:31 -04002249 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002250fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002251 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002252 return ret;
2253}
2254
2255static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
2256{
2257 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002258 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002259 int ret;
2260 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04002261 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -05002262 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002263
Chris Mason3394e162008-11-17 20:42:26 -05002264 /*
2265 * the FIRST_FREE_OBJECTID check makes sure we don't try to rmdir
2266 * the root of a subvolume or snapshot
2267 */
2268 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE ||
2269 inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) {
Yan134d4512007-10-25 15:49:25 -04002270 return -ENOTEMPTY;
Chris Mason925baed2008-06-25 16:01:30 -04002271 }
Yan134d4512007-10-25 15:49:25 -04002272
Chris Mason1832a6d2007-12-21 16:27:21 -05002273 ret = btrfs_check_free_space(root, 1, 1);
2274 if (ret)
2275 goto fail;
2276
Chris Mason39279cc2007-06-12 06:35:45 -04002277 trans = btrfs_start_transaction(root, 1);
2278 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -04002279
Josef Bacik7b128762008-07-24 12:17:14 -04002280 err = btrfs_orphan_add(trans, inode);
2281 if (err)
2282 goto fail_trans;
2283
Chris Mason39279cc2007-06-12 06:35:45 -04002284 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04002285 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
2286 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05002287 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04002288 btrfs_i_size_write(inode, 0);
Chris Mason39544012007-12-12 14:38:19 -05002289
Josef Bacik7b128762008-07-24 12:17:14 -04002290fail_trans:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002291 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04002292 ret = btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002293fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002294 btrfs_btree_balance_dirty(root, nr);
Chris Mason39544012007-12-12 14:38:19 -05002295
Chris Mason39279cc2007-06-12 06:35:45 -04002296 if (ret && !err)
2297 err = ret;
2298 return err;
2299}
2300
Chris Masond20f7042008-12-08 16:58:54 -05002301#if 0
Chris Mason39279cc2007-06-12 06:35:45 -04002302/*
Chris Mason323ac952008-10-01 19:05:46 -04002303 * when truncating bytes in a file, it is possible to avoid reading
2304 * the leaves that contain only checksum items. This can be the
2305 * majority of the IO required to delete a large file, but it must
2306 * be done carefully.
2307 *
2308 * The keys in the level just above the leaves are checked to make sure
2309 * the lowest key in a given leaf is a csum key, and starts at an offset
2310 * after the new size.
2311 *
2312 * Then the key for the next leaf is checked to make sure it also has
2313 * a checksum item for the same file. If it does, we know our target leaf
2314 * contains only checksum items, and it can be safely freed without reading
2315 * it.
2316 *
2317 * This is just an optimization targeted at large files. It may do
2318 * nothing. It will return 0 unless things went badly.
2319 */
2320static noinline int drop_csum_leaves(struct btrfs_trans_handle *trans,
2321 struct btrfs_root *root,
2322 struct btrfs_path *path,
2323 struct inode *inode, u64 new_size)
2324{
2325 struct btrfs_key key;
2326 int ret;
2327 int nritems;
2328 struct btrfs_key found_key;
2329 struct btrfs_key other_key;
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002330 struct btrfs_leaf_ref *ref;
2331 u64 leaf_gen;
2332 u64 leaf_start;
Chris Mason323ac952008-10-01 19:05:46 -04002333
2334 path->lowest_level = 1;
2335 key.objectid = inode->i_ino;
2336 key.type = BTRFS_CSUM_ITEM_KEY;
2337 key.offset = new_size;
2338again:
2339 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2340 if (ret < 0)
2341 goto out;
2342
2343 if (path->nodes[1] == NULL) {
2344 ret = 0;
2345 goto out;
2346 }
2347 ret = 0;
2348 btrfs_node_key_to_cpu(path->nodes[1], &found_key, path->slots[1]);
2349 nritems = btrfs_header_nritems(path->nodes[1]);
2350
2351 if (!nritems)
2352 goto out;
2353
2354 if (path->slots[1] >= nritems)
2355 goto next_node;
2356
2357 /* did we find a key greater than anything we want to delete? */
2358 if (found_key.objectid > inode->i_ino ||
2359 (found_key.objectid == inode->i_ino && found_key.type > key.type))
2360 goto out;
2361
2362 /* we check the next key in the node to make sure the leave contains
2363 * only checksum items. This comparison doesn't work if our
2364 * leaf is the last one in the node
2365 */
2366 if (path->slots[1] + 1 >= nritems) {
2367next_node:
2368 /* search forward from the last key in the node, this
2369 * will bring us into the next node in the tree
2370 */
2371 btrfs_node_key_to_cpu(path->nodes[1], &found_key, nritems - 1);
2372
2373 /* unlikely, but we inc below, so check to be safe */
2374 if (found_key.offset == (u64)-1)
2375 goto out;
2376
2377 /* search_forward needs a path with locks held, do the
2378 * search again for the original key. It is possible
2379 * this will race with a balance and return a path that
2380 * we could modify, but this drop is just an optimization
2381 * and is allowed to miss some leaves.
2382 */
2383 btrfs_release_path(root, path);
2384 found_key.offset++;
2385
2386 /* setup a max key for search_forward */
2387 other_key.offset = (u64)-1;
2388 other_key.type = key.type;
2389 other_key.objectid = key.objectid;
2390
2391 path->keep_locks = 1;
2392 ret = btrfs_search_forward(root, &found_key, &other_key,
2393 path, 0, 0);
2394 path->keep_locks = 0;
2395 if (ret || found_key.objectid != key.objectid ||
2396 found_key.type != key.type) {
2397 ret = 0;
2398 goto out;
2399 }
2400
2401 key.offset = found_key.offset;
2402 btrfs_release_path(root, path);
2403 cond_resched();
2404 goto again;
2405 }
2406
2407 /* we know there's one more slot after us in the tree,
2408 * read that key so we can verify it is also a checksum item
2409 */
2410 btrfs_node_key_to_cpu(path->nodes[1], &other_key, path->slots[1] + 1);
2411
2412 if (found_key.objectid < inode->i_ino)
2413 goto next_key;
2414
2415 if (found_key.type != key.type || found_key.offset < new_size)
2416 goto next_key;
2417
2418 /*
2419 * if the key for the next leaf isn't a csum key from this objectid,
2420 * we can't be sure there aren't good items inside this leaf.
2421 * Bail out
2422 */
2423 if (other_key.objectid != inode->i_ino || other_key.type != key.type)
2424 goto out;
2425
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002426 leaf_start = btrfs_node_blockptr(path->nodes[1], path->slots[1]);
2427 leaf_gen = btrfs_node_ptr_generation(path->nodes[1], path->slots[1]);
Chris Mason323ac952008-10-01 19:05:46 -04002428 /*
2429 * it is safe to delete this leaf, it contains only
2430 * csum items from this inode at an offset >= new_size
2431 */
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002432 ret = btrfs_del_leaf(trans, root, path, leaf_start);
Chris Mason323ac952008-10-01 19:05:46 -04002433 BUG_ON(ret);
2434
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002435 if (root->ref_cows && leaf_gen < trans->transid) {
2436 ref = btrfs_alloc_leaf_ref(root, 0);
2437 if (ref) {
2438 ref->root_gen = root->root_key.offset;
2439 ref->bytenr = leaf_start;
2440 ref->owner = 0;
2441 ref->generation = leaf_gen;
2442 ref->nritems = 0;
2443
Chris Masonbd56b302009-02-04 09:27:02 -05002444 btrfs_sort_leaf_ref(ref);
2445
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002446 ret = btrfs_add_leaf_ref(root, ref, 0);
2447 WARN_ON(ret);
2448 btrfs_free_leaf_ref(root, ref);
2449 } else {
2450 WARN_ON(1);
2451 }
2452 }
Chris Mason323ac952008-10-01 19:05:46 -04002453next_key:
2454 btrfs_release_path(root, path);
2455
2456 if (other_key.objectid == inode->i_ino &&
2457 other_key.type == key.type && other_key.offset > key.offset) {
2458 key.offset = other_key.offset;
2459 cond_resched();
2460 goto again;
2461 }
2462 ret = 0;
2463out:
2464 /* fixup any changes we've made to the path */
2465 path->lowest_level = 0;
2466 path->keep_locks = 0;
2467 btrfs_release_path(root, path);
2468 return ret;
2469}
2470
Chris Masond20f7042008-12-08 16:58:54 -05002471#endif
2472
Chris Mason323ac952008-10-01 19:05:46 -04002473/*
Chris Mason39279cc2007-06-12 06:35:45 -04002474 * this can truncate away extent items, csum items and directory items.
2475 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04002476 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04002477 *
2478 * csum items that cross the new i_size are truncated to the new size
2479 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04002480 *
2481 * min_type is the minimum key type to truncate down to. If set to 0, this
2482 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04002483 */
Chris Masone02119d2008-09-05 16:13:11 -04002484noinline int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
2485 struct btrfs_root *root,
2486 struct inode *inode,
2487 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04002488{
2489 int ret;
2490 struct btrfs_path *path;
2491 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04002492 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04002493 u32 found_type;
Chris Mason5f39d392007-10-15 16:14:19 -04002494 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002495 struct btrfs_file_extent_item *fi;
2496 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04002497 u64 extent_num_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002498 u64 item_end = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05002499 u64 root_gen = 0;
Chris Masond8d5f3e2007-12-11 12:42:00 -05002500 u64 root_owner = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002501 int found_extent;
2502 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05002503 int pending_del_nr = 0;
2504 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04002505 int extent_type = -1;
Chris Mason771ed682008-11-06 22:02:51 -05002506 int encoding;
Chris Mason3b951512008-04-17 11:29:12 -04002507 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002508
Chris Masone02119d2008-09-05 16:13:11 -04002509 if (root->ref_cows)
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002510 btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002511 path = btrfs_alloc_path();
Chris Mason3c69fae2007-08-07 15:52:22 -04002512 path->reada = -1;
Chris Mason39279cc2007-06-12 06:35:45 -04002513 BUG_ON(!path);
Chris Mason5f39d392007-10-15 16:14:19 -04002514
Chris Mason39279cc2007-06-12 06:35:45 -04002515 /* FIXME, add redo link to tree so we don't leak on crash */
2516 key.objectid = inode->i_ino;
2517 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04002518 key.type = (u8)-1;
2519
Chris Mason85e21ba2008-01-29 15:11:36 -05002520 btrfs_init_path(path);
Chris Mason323ac952008-10-01 19:05:46 -04002521
Chris Mason85e21ba2008-01-29 15:11:36 -05002522search_again:
2523 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masond3977122009-01-05 21:25:51 -05002524 if (ret < 0)
Chris Mason85e21ba2008-01-29 15:11:36 -05002525 goto error;
Chris Masond3977122009-01-05 21:25:51 -05002526
Chris Mason85e21ba2008-01-29 15:11:36 -05002527 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002528 /* there are no items in the tree for us to truncate, we're
2529 * done
2530 */
2531 if (path->slots[0] == 0) {
2532 ret = 0;
2533 goto error;
2534 }
Chris Mason85e21ba2008-01-29 15:11:36 -05002535 path->slots[0]--;
2536 }
2537
Chris Masond3977122009-01-05 21:25:51 -05002538 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04002539 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04002540 leaf = path->nodes[0];
2541 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2542 found_type = btrfs_key_type(&found_key);
Chris Mason771ed682008-11-06 22:02:51 -05002543 encoding = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002544
Chris Mason5f39d392007-10-15 16:14:19 -04002545 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -04002546 break;
Chris Mason5f39d392007-10-15 16:14:19 -04002547
Chris Mason85e21ba2008-01-29 15:11:36 -05002548 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04002549 break;
2550
Chris Mason5f39d392007-10-15 16:14:19 -04002551 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04002552 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04002553 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04002554 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04002555 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Mason771ed682008-11-06 22:02:51 -05002556 encoding = btrfs_file_extent_compression(leaf, fi);
2557 encoding |= btrfs_file_extent_encryption(leaf, fi);
2558 encoding |= btrfs_file_extent_other_encoding(leaf, fi);
2559
Chris Mason179e29e2007-11-01 11:28:41 -04002560 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04002561 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04002562 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04002563 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04002564 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04002565 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04002566 }
Yan008630c2007-11-07 13:31:09 -05002567 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04002568 }
Chris Masone02119d2008-09-05 16:13:11 -04002569 if (item_end < new_size) {
Chris Masond3977122009-01-05 21:25:51 -05002570 if (found_type == BTRFS_DIR_ITEM_KEY)
Chris Masonb888db22007-08-27 16:49:44 -04002571 found_type = BTRFS_INODE_ITEM_KEY;
Chris Masond3977122009-01-05 21:25:51 -05002572 else if (found_type == BTRFS_EXTENT_ITEM_KEY)
Chris Masond20f7042008-12-08 16:58:54 -05002573 found_type = BTRFS_EXTENT_DATA_KEY;
Chris Masond3977122009-01-05 21:25:51 -05002574 else if (found_type == BTRFS_EXTENT_DATA_KEY)
Chris Mason85e21ba2008-01-29 15:11:36 -05002575 found_type = BTRFS_XATTR_ITEM_KEY;
Chris Masond3977122009-01-05 21:25:51 -05002576 else if (found_type == BTRFS_XATTR_ITEM_KEY)
Chris Mason85e21ba2008-01-29 15:11:36 -05002577 found_type = BTRFS_INODE_REF_KEY;
Chris Masond3977122009-01-05 21:25:51 -05002578 else if (found_type)
Chris Masonb888db22007-08-27 16:49:44 -04002579 found_type--;
Chris Masond3977122009-01-05 21:25:51 -05002580 else
Chris Masonb888db22007-08-27 16:49:44 -04002581 break;
Yana61721d2007-09-17 11:08:38 -04002582 btrfs_set_key_type(&key, found_type);
Chris Mason85e21ba2008-01-29 15:11:36 -05002583 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04002584 }
Chris Masone02119d2008-09-05 16:13:11 -04002585 if (found_key.offset >= new_size)
Chris Mason39279cc2007-06-12 06:35:45 -04002586 del_item = 1;
2587 else
2588 del_item = 0;
2589 found_extent = 0;
2590
2591 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04002592 if (found_type != BTRFS_EXTENT_DATA_KEY)
2593 goto delete;
2594
2595 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04002596 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04002597 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason771ed682008-11-06 22:02:51 -05002598 if (!del_item && !encoding) {
Chris Masondb945352007-10-15 16:15:53 -04002599 u64 orig_num_bytes =
2600 btrfs_file_extent_num_bytes(leaf, fi);
Chris Masone02119d2008-09-05 16:13:11 -04002601 extent_num_bytes = new_size -
Chris Mason5f39d392007-10-15 16:14:19 -04002602 found_key.offset + root->sectorsize - 1;
Yanb1632b12008-01-30 11:54:04 -05002603 extent_num_bytes = extent_num_bytes &
2604 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04002605 btrfs_set_file_extent_num_bytes(leaf, fi,
2606 extent_num_bytes);
2607 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05002608 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04002609 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002610 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04002611 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002612 } else {
Chris Masondb945352007-10-15 16:15:53 -04002613 extent_num_bytes =
2614 btrfs_file_extent_disk_num_bytes(leaf,
2615 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04002616 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05002617 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04002618 if (extent_start != 0) {
2619 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04002620 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002621 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04002622 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002623 root_gen = btrfs_header_generation(leaf);
Chris Masond8d5f3e2007-12-11 12:42:00 -05002624 root_owner = btrfs_header_owner(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002625 }
Chris Mason90692182008-02-08 13:49:28 -05002626 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04002627 /*
2628 * we can't truncate inline items that have had
2629 * special encodings
2630 */
2631 if (!del_item &&
2632 btrfs_file_extent_compression(leaf, fi) == 0 &&
2633 btrfs_file_extent_encryption(leaf, fi) == 0 &&
2634 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002635 u32 size = new_size - found_key.offset;
2636
2637 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002638 inode_sub_bytes(inode, item_end + 1 -
2639 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04002640 }
2641 size =
2642 btrfs_file_extent_calc_inline_size(size);
Chris Mason90692182008-02-08 13:49:28 -05002643 ret = btrfs_truncate_item(trans, root, path,
Chris Masone02119d2008-09-05 16:13:11 -04002644 size, 1);
Chris Mason90692182008-02-08 13:49:28 -05002645 BUG_ON(ret);
Chris Masone02119d2008-09-05 16:13:11 -04002646 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002647 inode_sub_bytes(inode, item_end + 1 -
2648 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05002649 }
Chris Mason39279cc2007-06-12 06:35:45 -04002650 }
Chris Mason179e29e2007-11-01 11:28:41 -04002651delete:
Chris Mason39279cc2007-06-12 06:35:45 -04002652 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05002653 if (!pending_del_nr) {
2654 /* no pending yet, add ourselves */
2655 pending_del_slot = path->slots[0];
2656 pending_del_nr = 1;
2657 } else if (pending_del_nr &&
2658 path->slots[0] + 1 == pending_del_slot) {
2659 /* hop on the pending chunk */
2660 pending_del_nr++;
2661 pending_del_slot = path->slots[0];
2662 } else {
Chris Masond3977122009-01-05 21:25:51 -05002663 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05002664 }
Chris Mason39279cc2007-06-12 06:35:45 -04002665 } else {
2666 break;
2667 }
Chris Mason39279cc2007-06-12 06:35:45 -04002668 if (found_extent) {
2669 ret = btrfs_free_extent(trans, root, extent_start,
Chris Mason7bb86312007-12-11 09:25:06 -05002670 extent_num_bytes,
Zheng Yan31840ae2008-09-23 13:14:14 -04002671 leaf->start, root_owner,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002672 root_gen, inode->i_ino, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002673 BUG_ON(ret);
2674 }
Chris Mason85e21ba2008-01-29 15:11:36 -05002675next:
2676 if (path->slots[0] == 0) {
2677 if (pending_del_nr)
2678 goto del_pending;
2679 btrfs_release_path(root, path);
2680 goto search_again;
2681 }
2682
2683 path->slots[0]--;
2684 if (pending_del_nr &&
2685 path->slots[0] + 1 != pending_del_slot) {
2686 struct btrfs_key debug;
2687del_pending:
2688 btrfs_item_key_to_cpu(path->nodes[0], &debug,
2689 pending_del_slot);
2690 ret = btrfs_del_items(trans, root, path,
2691 pending_del_slot,
2692 pending_del_nr);
2693 BUG_ON(ret);
2694 pending_del_nr = 0;
2695 btrfs_release_path(root, path);
2696 goto search_again;
2697 }
Chris Mason39279cc2007-06-12 06:35:45 -04002698 }
2699 ret = 0;
2700error:
Chris Mason85e21ba2008-01-29 15:11:36 -05002701 if (pending_del_nr) {
2702 ret = btrfs_del_items(trans, root, path, pending_del_slot,
2703 pending_del_nr);
2704 }
Chris Mason39279cc2007-06-12 06:35:45 -04002705 btrfs_free_path(path);
2706 inode->i_sb->s_dirt = 1;
2707 return ret;
2708}
2709
Chris Masona52d9a82007-08-27 16:49:44 -04002710/*
2711 * taken from block_truncate_page, but does cow as it zeros out
2712 * any bytes left in the last page in the file.
2713 */
2714static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
2715{
2716 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -04002717 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002718 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2719 struct btrfs_ordered_extent *ordered;
2720 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04002721 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04002722 pgoff_t index = from >> PAGE_CACHE_SHIFT;
2723 unsigned offset = from & (PAGE_CACHE_SIZE-1);
2724 struct page *page;
2725 int ret = 0;
2726 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002727 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04002728
2729 if ((offset & (blocksize - 1)) == 0)
2730 goto out;
2731
2732 ret = -ENOMEM;
Chris Mason211c17f2008-05-15 09:13:45 -04002733again:
Chris Masona52d9a82007-08-27 16:49:44 -04002734 page = grab_cache_page(mapping, index);
2735 if (!page)
2736 goto out;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002737
2738 page_start = page_offset(page);
2739 page_end = page_start + PAGE_CACHE_SIZE - 1;
2740
Chris Masona52d9a82007-08-27 16:49:44 -04002741 if (!PageUptodate(page)) {
2742 ret = btrfs_readpage(NULL, page);
2743 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04002744 if (page->mapping != mapping) {
2745 unlock_page(page);
2746 page_cache_release(page);
2747 goto again;
2748 }
Chris Masona52d9a82007-08-27 16:49:44 -04002749 if (!PageUptodate(page)) {
2750 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04002751 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04002752 }
2753 }
Chris Mason211c17f2008-05-15 09:13:45 -04002754 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002755
2756 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
2757 set_page_extent_mapped(page);
2758
2759 ordered = btrfs_lookup_ordered_extent(inode, page_start);
2760 if (ordered) {
2761 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
2762 unlock_page(page);
2763 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04002764 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002765 btrfs_put_ordered_extent(ordered);
2766 goto again;
2767 }
2768
Chris Masonea8c2812008-08-04 23:17:27 -04002769 btrfs_set_extent_delalloc(inode, page_start, page_end);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002770 ret = 0;
2771 if (offset != PAGE_CACHE_SIZE) {
2772 kaddr = kmap(page);
2773 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
2774 flush_dcache_page(page);
2775 kunmap(page);
2776 }
Chris Mason247e7432008-07-17 12:53:51 -04002777 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002778 set_page_dirty(page);
2779 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04002780
Chris Mason89642222008-07-24 09:41:53 -04002781out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04002782 unlock_page(page);
2783 page_cache_release(page);
2784out:
2785 return ret;
2786}
2787
Yan Zheng9036c102008-10-30 14:19:41 -04002788int btrfs_cont_expand(struct inode *inode, loff_t size)
2789{
2790 struct btrfs_trans_handle *trans;
2791 struct btrfs_root *root = BTRFS_I(inode)->root;
2792 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2793 struct extent_map *em;
2794 u64 mask = root->sectorsize - 1;
2795 u64 hole_start = (inode->i_size + mask) & ~mask;
2796 u64 block_end = (size + mask) & ~mask;
2797 u64 last_byte;
2798 u64 cur_offset;
2799 u64 hole_size;
2800 int err;
2801
2802 if (size <= hole_start)
2803 return 0;
2804
2805 err = btrfs_check_free_space(root, 1, 0);
2806 if (err)
2807 return err;
2808
2809 btrfs_truncate_page(inode->i_mapping, inode->i_size);
2810
2811 while (1) {
2812 struct btrfs_ordered_extent *ordered;
2813 btrfs_wait_ordered_range(inode, hole_start,
2814 block_end - hole_start);
2815 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
2816 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
2817 if (!ordered)
2818 break;
2819 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
2820 btrfs_put_ordered_extent(ordered);
2821 }
2822
2823 trans = btrfs_start_transaction(root, 1);
2824 btrfs_set_trans_block_group(trans, inode);
2825
2826 cur_offset = hole_start;
2827 while (1) {
2828 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
2829 block_end - cur_offset, 0);
2830 BUG_ON(IS_ERR(em) || !em);
2831 last_byte = min(extent_map_end(em), block_end);
2832 last_byte = (last_byte + mask) & ~mask;
2833 if (test_bit(EXTENT_FLAG_VACANCY, &em->flags)) {
Chris Mason771ed682008-11-06 22:02:51 -05002834 u64 hint_byte = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04002835 hole_size = last_byte - cur_offset;
Chris Mason771ed682008-11-06 22:02:51 -05002836 err = btrfs_drop_extents(trans, root, inode,
2837 cur_offset,
2838 cur_offset + hole_size,
2839 cur_offset, &hint_byte);
2840 if (err)
2841 break;
Yan Zheng9036c102008-10-30 14:19:41 -04002842 err = btrfs_insert_file_extent(trans, root,
2843 inode->i_ino, cur_offset, 0,
2844 0, hole_size, 0, hole_size,
2845 0, 0, 0);
2846 btrfs_drop_extent_cache(inode, hole_start,
2847 last_byte - 1, 0);
2848 }
2849 free_extent_map(em);
2850 cur_offset = last_byte;
2851 if (err || cur_offset >= block_end)
2852 break;
2853 }
2854
2855 btrfs_end_transaction(trans, root);
2856 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
2857 return err;
2858}
2859
Chris Mason39279cc2007-06-12 06:35:45 -04002860static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
2861{
2862 struct inode *inode = dentry->d_inode;
2863 int err;
2864
2865 err = inode_change_ok(inode, attr);
2866 if (err)
2867 return err;
2868
2869 if (S_ISREG(inode->i_mode) &&
2870 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
Yan Zheng9036c102008-10-30 14:19:41 -04002871 err = btrfs_cont_expand(inode, attr->ia_size);
Chris Mason54aa1f42007-06-22 14:16:25 -04002872 if (err)
2873 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04002874 }
Yan Zheng9036c102008-10-30 14:19:41 -04002875
Chris Mason39279cc2007-06-12 06:35:45 -04002876 err = inode_setattr(inode, attr);
Josef Bacik33268ea2008-07-24 12:16:36 -04002877
2878 if (!err && ((attr->ia_valid & ATTR_MODE)))
2879 err = btrfs_acl_chmod(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002880 return err;
2881}
Chris Mason61295eb2008-01-14 16:24:38 -05002882
Chris Mason39279cc2007-06-12 06:35:45 -04002883void btrfs_delete_inode(struct inode *inode)
2884{
2885 struct btrfs_trans_handle *trans;
2886 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002887 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04002888 int ret;
2889
2890 truncate_inode_pages(&inode->i_data, 0);
2891 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04002892 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002893 goto no_delete;
2894 }
Chris Mason4a096752008-07-21 10:29:44 -04002895 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04002896
Chris Masondbe674a2008-07-17 12:54:05 -04002897 btrfs_i_size_write(inode, 0);
Yan Zheng180591b2009-01-06 09:58:06 -05002898 trans = btrfs_join_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002899
Chris Mason39279cc2007-06-12 06:35:45 -04002900 btrfs_set_trans_block_group(trans, inode);
Chris Masone02119d2008-09-05 16:13:11 -04002901 ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size, 0);
Josef Bacik7b128762008-07-24 12:17:14 -04002902 if (ret) {
2903 btrfs_orphan_del(NULL, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04002904 goto no_delete_lock;
Josef Bacik7b128762008-07-24 12:17:14 -04002905 }
2906
2907 btrfs_orphan_del(trans, inode);
Chris Mason85e21ba2008-01-29 15:11:36 -05002908
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002909 nr = trans->blocks_used;
Chris Mason85e21ba2008-01-29 15:11:36 -05002910 clear_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002911
Chris Mason39279cc2007-06-12 06:35:45 -04002912 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002913 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002914 return;
Chris Mason54aa1f42007-06-22 14:16:25 -04002915
2916no_delete_lock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002917 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04002918 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002919 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002920no_delete:
2921 clear_inode(inode);
2922}
2923
2924/*
2925 * this returns the key found in the dir entry in the location pointer.
2926 * If no dir entries were found, location->objectid is 0.
2927 */
2928static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
2929 struct btrfs_key *location)
2930{
2931 const char *name = dentry->d_name.name;
2932 int namelen = dentry->d_name.len;
2933 struct btrfs_dir_item *di;
2934 struct btrfs_path *path;
2935 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04002936 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002937
2938 path = btrfs_alloc_path();
2939 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -05002940
Chris Mason39279cc2007-06-12 06:35:45 -04002941 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
2942 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04002943 if (IS_ERR(di))
2944 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05002945
2946 if (!di || IS_ERR(di))
Chris Mason39544012007-12-12 14:38:19 -05002947 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05002948
Chris Mason5f39d392007-10-15 16:14:19 -04002949 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04002950out:
Chris Mason39279cc2007-06-12 06:35:45 -04002951 btrfs_free_path(path);
2952 return ret;
Chris Mason39544012007-12-12 14:38:19 -05002953out_err:
2954 location->objectid = 0;
2955 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04002956}
2957
2958/*
2959 * when we hit a tree root in a directory, the btrfs part of the inode
2960 * needs to be changed to reflect the root directory of the tree root. This
2961 * is kind of like crossing a mount point.
2962 */
2963static int fixup_tree_root_location(struct btrfs_root *root,
2964 struct btrfs_key *location,
Josef Bacik58176a92007-08-29 15:47:34 -04002965 struct btrfs_root **sub_root,
2966 struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04002967{
Chris Mason39279cc2007-06-12 06:35:45 -04002968 struct btrfs_root_item *ri;
2969
2970 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
2971 return 0;
2972 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
2973 return 0;
2974
Josef Bacik58176a92007-08-29 15:47:34 -04002975 *sub_root = btrfs_read_fs_root(root->fs_info, location,
2976 dentry->d_name.name,
2977 dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04002978 if (IS_ERR(*sub_root))
2979 return PTR_ERR(*sub_root);
2980
2981 ri = &(*sub_root)->root_item;
2982 location->objectid = btrfs_root_dirid(ri);
Chris Mason39279cc2007-06-12 06:35:45 -04002983 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
2984 location->offset = 0;
2985
Chris Mason39279cc2007-06-12 06:35:45 -04002986 return 0;
2987}
2988
Chris Masone02119d2008-09-05 16:13:11 -04002989static noinline void init_btrfs_i(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002990{
Chris Masone02119d2008-09-05 16:13:11 -04002991 struct btrfs_inode *bi = BTRFS_I(inode);
2992
2993 bi->i_acl = NULL;
2994 bi->i_default_acl = NULL;
2995
2996 bi->generation = 0;
Chris Masonc3027eb2008-12-08 16:40:21 -05002997 bi->sequence = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002998 bi->last_trans = 0;
2999 bi->logged_trans = 0;
3000 bi->delalloc_bytes = 0;
3001 bi->disk_i_size = 0;
3002 bi->flags = 0;
3003 bi->index_cnt = (u64)-1;
Chris Mason49eb7e42008-09-11 15:53:12 -04003004 bi->log_dirty_trans = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003005 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
3006 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04003007 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04003008 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
3009 inode->i_mapping, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04003010 INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes);
Chris Masonba1da2f2008-07-17 12:54:15 -04003011 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Masonee6e6502008-07-17 12:54:40 -04003012 mutex_init(&BTRFS_I(inode)->extent_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04003013 mutex_init(&BTRFS_I(inode)->log_mutex);
3014}
3015
3016static int btrfs_init_locked_inode(struct inode *inode, void *p)
3017{
3018 struct btrfs_iget_args *args = p;
3019 inode->i_ino = args->ino;
3020 init_btrfs_i(inode);
3021 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003022 return 0;
3023}
3024
3025static int btrfs_find_actor(struct inode *inode, void *opaque)
3026{
3027 struct btrfs_iget_args *args = opaque;
Chris Masond3977122009-01-05 21:25:51 -05003028 return args->ino == inode->i_ino &&
3029 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003030}
3031
Zheng Yan5b21f2e2008-09-26 10:05:38 -04003032struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
3033 struct btrfs_root *root, int wait)
3034{
3035 struct inode *inode;
3036 struct btrfs_iget_args args;
3037 args.ino = objectid;
3038 args.root = root;
3039
3040 if (wait) {
3041 inode = ilookup5(s, objectid, btrfs_find_actor,
3042 (void *)&args);
3043 } else {
3044 inode = ilookup5_nowait(s, objectid, btrfs_find_actor,
3045 (void *)&args);
3046 }
3047 return inode;
3048}
3049
Chris Mason39279cc2007-06-12 06:35:45 -04003050struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
3051 struct btrfs_root *root)
3052{
3053 struct inode *inode;
3054 struct btrfs_iget_args args;
3055 args.ino = objectid;
3056 args.root = root;
3057
3058 inode = iget5_locked(s, objectid, btrfs_find_actor,
3059 btrfs_init_locked_inode,
3060 (void *)&args);
3061 return inode;
3062}
3063
Balaji Rao1a54ef82008-07-21 02:01:04 +05303064/* Get an inode object given its location and corresponding root.
3065 * Returns in *is_new if the inode was read from disk
3066 */
3067struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
3068 struct btrfs_root *root, int *is_new)
3069{
3070 struct inode *inode;
3071
3072 inode = btrfs_iget_locked(s, location->objectid, root);
3073 if (!inode)
3074 return ERR_PTR(-EACCES);
3075
3076 if (inode->i_state & I_NEW) {
3077 BTRFS_I(inode)->root = root;
3078 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
3079 btrfs_read_locked_inode(inode);
3080 unlock_new_inode(inode);
3081 if (is_new)
3082 *is_new = 1;
3083 } else {
3084 if (is_new)
3085 *is_new = 0;
3086 }
3087
3088 return inode;
3089}
3090
Chris Mason3de45862008-11-17 21:02:50 -05003091struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04003092{
Chris Masond3977122009-01-05 21:25:51 -05003093 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003094 struct btrfs_inode *bi = BTRFS_I(dir);
3095 struct btrfs_root *root = bi->root;
3096 struct btrfs_root *sub_root = root;
3097 struct btrfs_key location;
Yan Zhengc146afa2008-11-12 14:34:12 -05003098 int ret, new;
Chris Mason39279cc2007-06-12 06:35:45 -04003099
3100 if (dentry->d_name.len > BTRFS_NAME_LEN)
3101 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04003102
Chris Mason39279cc2007-06-12 06:35:45 -04003103 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04003104
Chris Mason39279cc2007-06-12 06:35:45 -04003105 if (ret < 0)
3106 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04003107
Chris Mason39279cc2007-06-12 06:35:45 -04003108 inode = NULL;
3109 if (location.objectid) {
Josef Bacik58176a92007-08-29 15:47:34 -04003110 ret = fixup_tree_root_location(root, &location, &sub_root,
3111 dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04003112 if (ret < 0)
3113 return ERR_PTR(ret);
3114 if (ret > 0)
3115 return ERR_PTR(-ENOENT);
Balaji Rao1a54ef82008-07-21 02:01:04 +05303116 inode = btrfs_iget(dir->i_sb, &location, sub_root, &new);
3117 if (IS_ERR(inode))
3118 return ERR_CAST(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003119 }
Chris Mason3de45862008-11-17 21:02:50 -05003120 return inode;
3121}
3122
3123static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
3124 struct nameidata *nd)
3125{
3126 struct inode *inode;
3127
3128 if (dentry->d_name.len > BTRFS_NAME_LEN)
3129 return ERR_PTR(-ENAMETOOLONG);
3130
3131 inode = btrfs_lookup_dentry(dir, dentry);
3132 if (IS_ERR(inode))
3133 return ERR_CAST(inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003134
Chris Mason39279cc2007-06-12 06:35:45 -04003135 return d_splice_alias(inode, dentry);
3136}
3137
Chris Mason39279cc2007-06-12 06:35:45 -04003138static unsigned char btrfs_filetype_table[] = {
3139 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
3140};
3141
David Woodhousecbdf5a22008-08-06 19:42:33 +01003142static int btrfs_real_readdir(struct file *filp, void *dirent,
3143 filldir_t filldir)
Chris Mason39279cc2007-06-12 06:35:45 -04003144{
Chris Mason6da6aba2007-12-18 16:15:09 -05003145 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003146 struct btrfs_root *root = BTRFS_I(inode)->root;
3147 struct btrfs_item *item;
3148 struct btrfs_dir_item *di;
3149 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04003150 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04003151 struct btrfs_path *path;
3152 int ret;
3153 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04003154 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003155 int slot;
3156 int advance;
3157 unsigned char d_type;
3158 int over = 0;
3159 u32 di_cur;
3160 u32 di_total;
3161 u32 di_len;
3162 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04003163 char tmp_name[32];
3164 char *name_ptr;
3165 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04003166
3167 /* FIXME, use a real flag for deciding about the key type */
3168 if (root->fs_info->tree_root == root)
3169 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04003170
Chris Mason39544012007-12-12 14:38:19 -05003171 /* special case for "." */
3172 if (filp->f_pos == 0) {
3173 over = filldir(dirent, ".", 1,
3174 1, inode->i_ino,
3175 DT_DIR);
3176 if (over)
3177 return 0;
3178 filp->f_pos = 1;
3179 }
Chris Mason39544012007-12-12 14:38:19 -05003180 /* special case for .., just use the back ref */
3181 if (filp->f_pos == 1) {
David Woodhouse5ecc7e52008-08-17 15:14:48 +01003182 u64 pino = parent_ino(filp->f_path.dentry);
Chris Mason39544012007-12-12 14:38:19 -05003183 over = filldir(dirent, "..", 2,
David Woodhouse5ecc7e52008-08-17 15:14:48 +01003184 2, pino, DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05003185 if (over)
David Woodhouse49593bf2008-08-17 17:08:36 +01003186 return 0;
Chris Mason39544012007-12-12 14:38:19 -05003187 filp->f_pos = 2;
3188 }
David Woodhouse49593bf2008-08-17 17:08:36 +01003189 path = btrfs_alloc_path();
3190 path->reada = 2;
3191
Chris Mason39279cc2007-06-12 06:35:45 -04003192 btrfs_set_key_type(&key, key_type);
3193 key.offset = filp->f_pos;
David Woodhouse49593bf2008-08-17 17:08:36 +01003194 key.objectid = inode->i_ino;
Chris Mason5f39d392007-10-15 16:14:19 -04003195
Chris Mason39279cc2007-06-12 06:35:45 -04003196 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3197 if (ret < 0)
3198 goto err;
3199 advance = 0;
David Woodhouse49593bf2008-08-17 17:08:36 +01003200
3201 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04003202 leaf = path->nodes[0];
3203 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003204 slot = path->slots[0];
3205 if (advance || slot >= nritems) {
David Woodhouse49593bf2008-08-17 17:08:36 +01003206 if (slot >= nritems - 1) {
Chris Mason39279cc2007-06-12 06:35:45 -04003207 ret = btrfs_next_leaf(root, path);
3208 if (ret)
3209 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003210 leaf = path->nodes[0];
3211 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003212 slot = path->slots[0];
3213 } else {
3214 slot++;
3215 path->slots[0]++;
3216 }
3217 }
Chris Mason3de45862008-11-17 21:02:50 -05003218
Chris Mason39279cc2007-06-12 06:35:45 -04003219 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04003220 item = btrfs_item_nr(leaf, slot);
3221 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3222
3223 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04003224 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003225 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003226 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003227 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04003228 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04003229
3230 filp->f_pos = found_key.offset;
David Woodhouse49593bf2008-08-17 17:08:36 +01003231
Chris Mason39279cc2007-06-12 06:35:45 -04003232 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
3233 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003234 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01003235
3236 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04003237 struct btrfs_key location;
3238
3239 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01003240 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04003241 name_ptr = tmp_name;
3242 } else {
3243 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01003244 if (!name_ptr) {
3245 ret = -ENOMEM;
3246 goto err;
3247 }
Chris Mason5f39d392007-10-15 16:14:19 -04003248 }
3249 read_extent_buffer(leaf, name_ptr,
3250 (unsigned long)(di + 1), name_len);
3251
3252 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
3253 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05003254
3255 /* is this a reference to our own snapshot? If so
3256 * skip it
3257 */
3258 if (location.type == BTRFS_ROOT_ITEM_KEY &&
3259 location.objectid == root->root_key.objectid) {
3260 over = 0;
3261 goto skip;
3262 }
Chris Mason5f39d392007-10-15 16:14:19 -04003263 over = filldir(dirent, name_ptr, name_len,
David Woodhouse49593bf2008-08-17 17:08:36 +01003264 found_key.offset, location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04003265 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04003266
Chris Mason3de45862008-11-17 21:02:50 -05003267skip:
Chris Mason5f39d392007-10-15 16:14:19 -04003268 if (name_ptr != tmp_name)
3269 kfree(name_ptr);
3270
Chris Mason39279cc2007-06-12 06:35:45 -04003271 if (over)
3272 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05003273 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01003274 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04003275 di_cur += di_len;
3276 di = (struct btrfs_dir_item *)((char *)di + di_len);
3277 }
3278 }
David Woodhouse49593bf2008-08-17 17:08:36 +01003279
3280 /* Reached end of directory/root. Bump pos past the last item. */
Yan Zheng5e591a02008-02-19 11:41:02 -05003281 if (key_type == BTRFS_DIR_INDEX_KEY)
Chris Mason89f135d2009-01-28 15:34:27 -05003282 filp->f_pos = INT_LIMIT(off_t);
Yan Zheng5e591a02008-02-19 11:41:02 -05003283 else
3284 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04003285nopos:
3286 ret = 0;
3287err:
Chris Mason39279cc2007-06-12 06:35:45 -04003288 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003289 return ret;
3290}
3291
3292int btrfs_write_inode(struct inode *inode, int wait)
3293{
3294 struct btrfs_root *root = BTRFS_I(inode)->root;
3295 struct btrfs_trans_handle *trans;
3296 int ret = 0;
3297
Yan Zhengc146afa2008-11-12 14:34:12 -05003298 if (root->fs_info->btree_inode == inode)
Chris Mason4ca8b412008-08-05 13:30:48 -04003299 return 0;
3300
Chris Mason39279cc2007-06-12 06:35:45 -04003301 if (wait) {
Chris Masonf9295742008-07-17 12:54:14 -04003302 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003303 btrfs_set_trans_block_group(trans, inode);
3304 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04003305 }
3306 return ret;
3307}
3308
3309/*
Chris Mason54aa1f42007-06-22 14:16:25 -04003310 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04003311 * inode changes. But, it is most likely to find the inode in cache.
3312 * FIXME, needs more benchmarking...there are no reasons other than performance
3313 * to keep or drop this code.
3314 */
3315void btrfs_dirty_inode(struct inode *inode)
3316{
3317 struct btrfs_root *root = BTRFS_I(inode)->root;
3318 struct btrfs_trans_handle *trans;
3319
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 btrfs_update_inode(trans, root, inode);
3323 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04003324}
3325
Chris Masond352ac62008-09-29 15:18:18 -04003326/*
3327 * find the highest existing sequence number in a directory
3328 * and then set the in-memory index_cnt variable to reflect
3329 * free sequence numbers
3330 */
Josef Bacikaec74772008-07-24 12:12:38 -04003331static int btrfs_set_inode_index_count(struct inode *inode)
3332{
3333 struct btrfs_root *root = BTRFS_I(inode)->root;
3334 struct btrfs_key key, found_key;
3335 struct btrfs_path *path;
3336 struct extent_buffer *leaf;
3337 int ret;
3338
3339 key.objectid = inode->i_ino;
3340 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
3341 key.offset = (u64)-1;
3342
3343 path = btrfs_alloc_path();
3344 if (!path)
3345 return -ENOMEM;
3346
3347 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3348 if (ret < 0)
3349 goto out;
3350 /* FIXME: we should be able to handle this */
3351 if (ret == 0)
3352 goto out;
3353 ret = 0;
3354
3355 /*
3356 * MAGIC NUMBER EXPLANATION:
3357 * since we search a directory based on f_pos we have to start at 2
3358 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
3359 * else has to start at 2
3360 */
3361 if (path->slots[0] == 0) {
3362 BTRFS_I(inode)->index_cnt = 2;
3363 goto out;
3364 }
3365
3366 path->slots[0]--;
3367
3368 leaf = path->nodes[0];
3369 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3370
3371 if (found_key.objectid != inode->i_ino ||
3372 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
3373 BTRFS_I(inode)->index_cnt = 2;
3374 goto out;
3375 }
3376
3377 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
3378out:
3379 btrfs_free_path(path);
3380 return ret;
3381}
3382
Chris Masond352ac62008-09-29 15:18:18 -04003383/*
3384 * helper to find a free sequence number in a given directory. This current
3385 * code is very simple, later versions will do smarter things in the btree
3386 */
Chris Mason3de45862008-11-17 21:02:50 -05003387int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04003388{
3389 int ret = 0;
3390
3391 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
3392 ret = btrfs_set_inode_index_count(dir);
Chris Masond3977122009-01-05 21:25:51 -05003393 if (ret)
Josef Bacikaec74772008-07-24 12:12:38 -04003394 return ret;
3395 }
3396
Chris Mason00e4e6b2008-08-05 11:18:09 -04003397 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04003398 BTRFS_I(dir)->index_cnt++;
3399
3400 return ret;
3401}
3402
Chris Mason39279cc2007-06-12 06:35:45 -04003403static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
3404 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04003405 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05003406 const char *name, int name_len,
Yan Zhengd2fb3432008-12-11 16:30:39 -05003407 u64 ref_objectid, u64 objectid,
3408 u64 alloc_hint, int mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04003409{
3410 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04003411 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04003412 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04003413 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05003414 struct btrfs_inode_ref *ref;
3415 struct btrfs_key key[2];
3416 u32 sizes[2];
3417 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04003418 int ret;
3419 int owner;
3420
Chris Mason5f39d392007-10-15 16:14:19 -04003421 path = btrfs_alloc_path();
3422 BUG_ON(!path);
3423
Chris Mason39279cc2007-06-12 06:35:45 -04003424 inode = new_inode(root->fs_info->sb);
3425 if (!inode)
3426 return ERR_PTR(-ENOMEM);
3427
Josef Bacikaec74772008-07-24 12:12:38 -04003428 if (dir) {
Chris Mason3de45862008-11-17 21:02:50 -05003429 ret = btrfs_set_inode_index(dir, index);
Josef Bacikaec74772008-07-24 12:12:38 -04003430 if (ret)
3431 return ERR_PTR(ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003432 }
3433 /*
3434 * index_cnt is ignored for everything but a dir,
3435 * btrfs_get_inode_index_count has an explanation for the magic
3436 * number
3437 */
Chris Masone02119d2008-09-05 16:13:11 -04003438 init_btrfs_i(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04003439 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04003440 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04003441 BTRFS_I(inode)->generation = trans->transid;
Chris Masonb888db22007-08-27 16:49:44 -04003442
Chris Mason39279cc2007-06-12 06:35:45 -04003443 if (mode & S_IFDIR)
3444 owner = 0;
3445 else
3446 owner = 1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003447 BTRFS_I(inode)->block_group =
3448 btrfs_find_block_group(root, 0, alloc_hint, owner);
Yan Zheng17d217f2008-12-12 10:03:38 -05003449 if ((mode & S_IFREG)) {
3450 if (btrfs_test_opt(root, NODATASUM))
3451 btrfs_set_flag(inode, NODATASUM);
3452 if (btrfs_test_opt(root, NODATACOW))
3453 btrfs_set_flag(inode, NODATACOW);
3454 }
Chris Mason9c583092008-01-29 15:15:18 -05003455
3456 key[0].objectid = objectid;
3457 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
3458 key[0].offset = 0;
3459
3460 key[1].objectid = objectid;
3461 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
3462 key[1].offset = ref_objectid;
3463
3464 sizes[0] = sizeof(struct btrfs_inode_item);
3465 sizes[1] = name_len + sizeof(*ref);
3466
3467 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
3468 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04003469 goto fail;
3470
Chris Mason9c583092008-01-29 15:15:18 -05003471 if (objectid > root->highest_inode)
3472 root->highest_inode = objectid;
3473
Chris Mason79683f22008-11-19 22:00:53 -05003474 inode->i_uid = current_fsuid();
Chris Ball8c087b52009-02-04 09:29:54 -05003475
3476 if (dir->i_mode & S_ISGID) {
3477 inode->i_gid = dir->i_gid;
3478 if (S_ISDIR(mode))
3479 mode |= S_ISGID;
3480 } else
3481 inode->i_gid = current_fsgid();
3482
Chris Mason39279cc2007-06-12 06:35:45 -04003483 inode->i_mode = mode;
3484 inode->i_ino = objectid;
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003485 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003486 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04003487 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3488 struct btrfs_inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003489 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05003490
3491 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
3492 struct btrfs_inode_ref);
3493 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04003494 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05003495 ptr = (unsigned long)(ref + 1);
3496 write_extent_buffer(path->nodes[0], name, ptr, name_len);
3497
Chris Mason5f39d392007-10-15 16:14:19 -04003498 btrfs_mark_buffer_dirty(path->nodes[0]);
3499 btrfs_free_path(path);
3500
Chris Mason39279cc2007-06-12 06:35:45 -04003501 location = &BTRFS_I(inode)->location;
3502 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04003503 location->offset = 0;
3504 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
3505
Chris Mason39279cc2007-06-12 06:35:45 -04003506 insert_inode_hash(inode);
3507 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04003508fail:
Josef Bacikaec74772008-07-24 12:12:38 -04003509 if (dir)
3510 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04003511 btrfs_free_path(path);
3512 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003513}
3514
3515static inline u8 btrfs_inode_type(struct inode *inode)
3516{
3517 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
3518}
3519
Chris Masond352ac62008-09-29 15:18:18 -04003520/*
3521 * utility function to add 'inode' into 'parent_inode' with
3522 * a give name and a given sequence number.
3523 * if 'add_backref' is true, also insert a backref from the
3524 * inode to the parent directory.
3525 */
Chris Masone02119d2008-09-05 16:13:11 -04003526int btrfs_add_link(struct btrfs_trans_handle *trans,
3527 struct inode *parent_inode, struct inode *inode,
3528 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04003529{
3530 int ret;
3531 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04003532 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Chris Mason5f39d392007-10-15 16:14:19 -04003533
Chris Mason39279cc2007-06-12 06:35:45 -04003534 key.objectid = inode->i_ino;
Chris Mason39279cc2007-06-12 06:35:45 -04003535 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
3536 key.offset = 0;
3537
Chris Masone02119d2008-09-05 16:13:11 -04003538 ret = btrfs_insert_dir_item(trans, root, name, name_len,
3539 parent_inode->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04003540 &key, btrfs_inode_type(inode),
Chris Mason00e4e6b2008-08-05 11:18:09 -04003541 index);
Chris Mason39279cc2007-06-12 06:35:45 -04003542 if (ret == 0) {
Chris Mason9c583092008-01-29 15:15:18 -05003543 if (add_backref) {
3544 ret = btrfs_insert_inode_ref(trans, root,
Chris Masone02119d2008-09-05 16:13:11 -04003545 name, name_len,
3546 inode->i_ino,
3547 parent_inode->i_ino,
3548 index);
Chris Mason9c583092008-01-29 15:15:18 -05003549 }
Chris Masondbe674a2008-07-17 12:54:05 -04003550 btrfs_i_size_write(parent_inode, parent_inode->i_size +
Chris Masone02119d2008-09-05 16:13:11 -04003551 name_len * 2);
Chris Mason79c44582007-06-25 10:09:33 -04003552 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Masone02119d2008-09-05 16:13:11 -04003553 ret = btrfs_update_inode(trans, root, parent_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003554 }
3555 return ret;
3556}
3557
3558static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05003559 struct dentry *dentry, struct inode *inode,
Chris Mason00e4e6b2008-08-05 11:18:09 -04003560 int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04003561{
Chris Masone02119d2008-09-05 16:13:11 -04003562 int err = btrfs_add_link(trans, dentry->d_parent->d_inode,
3563 inode, dentry->d_name.name,
3564 dentry->d_name.len, backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04003565 if (!err) {
3566 d_instantiate(dentry, inode);
3567 return 0;
3568 }
3569 if (err > 0)
3570 err = -EEXIST;
3571 return err;
3572}
3573
Josef Bacik618e21d2007-07-11 10:18:17 -04003574static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
3575 int mode, dev_t rdev)
3576{
3577 struct btrfs_trans_handle *trans;
3578 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05003579 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04003580 int err;
3581 int drop_inode = 0;
3582 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05003583 unsigned long nr = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04003584 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04003585
3586 if (!new_valid_dev(rdev))
3587 return -EINVAL;
3588
Chris Mason1832a6d2007-12-21 16:27:21 -05003589 err = btrfs_check_free_space(root, 1, 0);
3590 if (err)
3591 goto fail;
3592
Josef Bacik618e21d2007-07-11 10:18:17 -04003593 trans = btrfs_start_transaction(root, 1);
3594 btrfs_set_trans_block_group(trans, dir);
3595
3596 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3597 if (err) {
3598 err = -ENOSPC;
3599 goto out_unlock;
3600 }
3601
Josef Bacikaec74772008-07-24 12:12:38 -04003602 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05003603 dentry->d_name.len,
3604 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04003605 BTRFS_I(dir)->block_group, mode, &index);
Josef Bacik618e21d2007-07-11 10:18:17 -04003606 err = PTR_ERR(inode);
3607 if (IS_ERR(inode))
3608 goto out_unlock;
3609
Jim Owens0279b4c2009-02-04 09:29:13 -05003610 err = btrfs_init_inode_security(inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04003611 if (err) {
3612 drop_inode = 1;
3613 goto out_unlock;
3614 }
3615
Josef Bacik618e21d2007-07-11 10:18:17 -04003616 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04003617 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04003618 if (err)
3619 drop_inode = 1;
3620 else {
3621 inode->i_op = &btrfs_special_inode_operations;
3622 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04003623 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04003624 }
3625 dir->i_sb->s_dirt = 1;
3626 btrfs_update_inode_block_group(trans, inode);
3627 btrfs_update_inode_block_group(trans, dir);
3628out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003629 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04003630 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003631fail:
Josef Bacik618e21d2007-07-11 10:18:17 -04003632 if (drop_inode) {
3633 inode_dec_link_count(inode);
3634 iput(inode);
3635 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003636 btrfs_btree_balance_dirty(root, nr);
Josef Bacik618e21d2007-07-11 10:18:17 -04003637 return err;
3638}
3639
Chris Mason39279cc2007-06-12 06:35:45 -04003640static int btrfs_create(struct inode *dir, struct dentry *dentry,
3641 int mode, struct nameidata *nd)
3642{
3643 struct btrfs_trans_handle *trans;
3644 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05003645 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04003646 int err;
3647 int drop_inode = 0;
Chris Mason1832a6d2007-12-21 16:27:21 -05003648 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003649 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04003650 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003651
Chris Mason1832a6d2007-12-21 16:27:21 -05003652 err = btrfs_check_free_space(root, 1, 0);
3653 if (err)
3654 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04003655 trans = btrfs_start_transaction(root, 1);
3656 btrfs_set_trans_block_group(trans, dir);
3657
3658 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3659 if (err) {
3660 err = -ENOSPC;
3661 goto out_unlock;
3662 }
3663
Josef Bacikaec74772008-07-24 12:12:38 -04003664 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05003665 dentry->d_name.len,
3666 dentry->d_parent->d_inode->i_ino,
Chris Mason00e4e6b2008-08-05 11:18:09 -04003667 objectid, BTRFS_I(dir)->block_group, mode,
3668 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04003669 err = PTR_ERR(inode);
3670 if (IS_ERR(inode))
3671 goto out_unlock;
3672
Jim Owens0279b4c2009-02-04 09:29:13 -05003673 err = btrfs_init_inode_security(inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04003674 if (err) {
3675 drop_inode = 1;
3676 goto out_unlock;
3677 }
3678
Chris Mason39279cc2007-06-12 06:35:45 -04003679 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04003680 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04003681 if (err)
3682 drop_inode = 1;
3683 else {
3684 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003685 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003686 inode->i_fop = &btrfs_file_operations;
3687 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05003688 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003689 }
3690 dir->i_sb->s_dirt = 1;
3691 btrfs_update_inode_block_group(trans, inode);
3692 btrfs_update_inode_block_group(trans, dir);
3693out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003694 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04003695 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003696fail:
Chris Mason39279cc2007-06-12 06:35:45 -04003697 if (drop_inode) {
3698 inode_dec_link_count(inode);
3699 iput(inode);
3700 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003701 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003702 return err;
3703}
3704
3705static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
3706 struct dentry *dentry)
3707{
3708 struct btrfs_trans_handle *trans;
3709 struct btrfs_root *root = BTRFS_I(dir)->root;
3710 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04003711 u64 index;
Chris Mason1832a6d2007-12-21 16:27:21 -05003712 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003713 int err;
3714 int drop_inode = 0;
3715
3716 if (inode->i_nlink == 0)
3717 return -ENOENT;
3718
Chris Masone02119d2008-09-05 16:13:11 -04003719 btrfs_inc_nlink(inode);
Chris Mason1832a6d2007-12-21 16:27:21 -05003720 err = btrfs_check_free_space(root, 1, 0);
3721 if (err)
3722 goto fail;
Chris Mason3de45862008-11-17 21:02:50 -05003723 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003724 if (err)
3725 goto fail;
3726
Chris Mason39279cc2007-06-12 06:35:45 -04003727 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003728
Chris Mason39279cc2007-06-12 06:35:45 -04003729 btrfs_set_trans_block_group(trans, dir);
3730 atomic_inc(&inode->i_count);
Josef Bacikaec74772008-07-24 12:12:38 -04003731
Chris Mason00e4e6b2008-08-05 11:18:09 -04003732 err = btrfs_add_nondir(trans, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04003733
Chris Mason39279cc2007-06-12 06:35:45 -04003734 if (err)
3735 drop_inode = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04003736
Chris Mason39279cc2007-06-12 06:35:45 -04003737 dir->i_sb->s_dirt = 1;
3738 btrfs_update_inode_block_group(trans, dir);
Chris Mason54aa1f42007-06-22 14:16:25 -04003739 err = btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003740
Chris Mason54aa1f42007-06-22 14:16:25 -04003741 if (err)
3742 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003743
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003744 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04003745 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003746fail:
Chris Mason39279cc2007-06-12 06:35:45 -04003747 if (drop_inode) {
3748 inode_dec_link_count(inode);
3749 iput(inode);
3750 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003751 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003752 return err;
3753}
3754
Chris Mason39279cc2007-06-12 06:35:45 -04003755static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
3756{
Chris Masonb9d86662008-05-02 16:13:49 -04003757 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04003758 struct btrfs_trans_handle *trans;
3759 struct btrfs_root *root = BTRFS_I(dir)->root;
3760 int err = 0;
3761 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04003762 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04003763 u64 index = 0;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003764 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003765
Chris Mason1832a6d2007-12-21 16:27:21 -05003766 err = btrfs_check_free_space(root, 1, 0);
3767 if (err)
3768 goto out_unlock;
3769
Chris Mason39279cc2007-06-12 06:35:45 -04003770 trans = btrfs_start_transaction(root, 1);
3771 btrfs_set_trans_block_group(trans, dir);
Chris Mason5f39d392007-10-15 16:14:19 -04003772
Chris Mason39279cc2007-06-12 06:35:45 -04003773 if (IS_ERR(trans)) {
3774 err = PTR_ERR(trans);
3775 goto out_unlock;
3776 }
3777
3778 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3779 if (err) {
3780 err = -ENOSPC;
3781 goto out_unlock;
3782 }
3783
Josef Bacikaec74772008-07-24 12:12:38 -04003784 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05003785 dentry->d_name.len,
3786 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04003787 BTRFS_I(dir)->block_group, S_IFDIR | mode,
3788 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04003789 if (IS_ERR(inode)) {
3790 err = PTR_ERR(inode);
3791 goto out_fail;
3792 }
Chris Mason5f39d392007-10-15 16:14:19 -04003793
Chris Mason39279cc2007-06-12 06:35:45 -04003794 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04003795
Jim Owens0279b4c2009-02-04 09:29:13 -05003796 err = btrfs_init_inode_security(inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04003797 if (err)
3798 goto out_fail;
3799
Chris Mason39279cc2007-06-12 06:35:45 -04003800 inode->i_op = &btrfs_dir_inode_operations;
3801 inode->i_fop = &btrfs_dir_file_operations;
3802 btrfs_set_trans_block_group(trans, inode);
3803
Chris Masondbe674a2008-07-17 12:54:05 -04003804 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003805 err = btrfs_update_inode(trans, root, inode);
3806 if (err)
3807 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04003808
Chris Masone02119d2008-09-05 16:13:11 -04003809 err = btrfs_add_link(trans, dentry->d_parent->d_inode,
3810 inode, dentry->d_name.name,
3811 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04003812 if (err)
3813 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04003814
Chris Mason39279cc2007-06-12 06:35:45 -04003815 d_instantiate(dentry, inode);
3816 drop_on_err = 0;
3817 dir->i_sb->s_dirt = 1;
3818 btrfs_update_inode_block_group(trans, inode);
3819 btrfs_update_inode_block_group(trans, dir);
3820
3821out_fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003822 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04003823 btrfs_end_transaction_throttle(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04003824
Chris Mason39279cc2007-06-12 06:35:45 -04003825out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04003826 if (drop_on_err)
3827 iput(inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003828 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003829 return err;
3830}
3831
Chris Masond352ac62008-09-29 15:18:18 -04003832/* helper for btfs_get_extent. Given an existing extent in the tree,
3833 * and an extent that you want to insert, deal with overlap and insert
3834 * the new extent into the tree.
3835 */
Chris Mason3b951512008-04-17 11:29:12 -04003836static int merge_extent_mapping(struct extent_map_tree *em_tree,
3837 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003838 struct extent_map *em,
3839 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04003840{
3841 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04003842
Chris Masone6dcd2d2008-07-17 12:53:50 -04003843 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
3844 start_diff = map_start - em->start;
3845 em->start = map_start;
3846 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04003847 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
3848 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04003849 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04003850 em->block_len -= start_diff;
3851 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003852 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04003853}
3854
Chris Masonc8b97812008-10-29 14:49:59 -04003855static noinline int uncompress_inline(struct btrfs_path *path,
3856 struct inode *inode, struct page *page,
3857 size_t pg_offset, u64 extent_offset,
3858 struct btrfs_file_extent_item *item)
3859{
3860 int ret;
3861 struct extent_buffer *leaf = path->nodes[0];
3862 char *tmp;
3863 size_t max_size;
3864 unsigned long inline_size;
3865 unsigned long ptr;
3866
3867 WARN_ON(pg_offset != 0);
3868 max_size = btrfs_file_extent_ram_bytes(leaf, item);
3869 inline_size = btrfs_file_extent_inline_item_len(leaf,
3870 btrfs_item_nr(leaf, path->slots[0]));
3871 tmp = kmalloc(inline_size, GFP_NOFS);
3872 ptr = btrfs_file_extent_inline_start(item);
3873
3874 read_extent_buffer(leaf, tmp, ptr, inline_size);
3875
Chris Mason5b050f02008-11-11 09:34:41 -05003876 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04003877 ret = btrfs_zlib_decompress(tmp, page, extent_offset,
3878 inline_size, max_size);
3879 if (ret) {
3880 char *kaddr = kmap_atomic(page, KM_USER0);
3881 unsigned long copy_size = min_t(u64,
3882 PAGE_CACHE_SIZE - pg_offset,
3883 max_size - extent_offset);
3884 memset(kaddr + pg_offset, 0, copy_size);
3885 kunmap_atomic(kaddr, KM_USER0);
3886 }
3887 kfree(tmp);
3888 return 0;
3889}
3890
Chris Masond352ac62008-09-29 15:18:18 -04003891/*
3892 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05003893 * the ugly parts come from merging extents from the disk with the in-ram
3894 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04003895 * where the in-ram extents might be locked pending data=ordered completion.
3896 *
3897 * This also copies inline extents directly into the page.
3898 */
Chris Masond3977122009-01-05 21:25:51 -05003899
Chris Masona52d9a82007-08-27 16:49:44 -04003900struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05003901 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04003902 int create)
3903{
3904 int ret;
3905 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04003906 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04003907 u64 extent_start = 0;
3908 u64 extent_end = 0;
3909 u64 objectid = inode->i_ino;
3910 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04003911 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04003912 struct btrfs_root *root = BTRFS_I(inode)->root;
3913 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04003914 struct extent_buffer *leaf;
3915 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04003916 struct extent_map *em = NULL;
3917 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05003918 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04003919 struct btrfs_trans_handle *trans = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04003920 int compressed;
Chris Masona52d9a82007-08-27 16:49:44 -04003921
Chris Masona52d9a82007-08-27 16:49:44 -04003922again:
Chris Masond1310b22008-01-24 16:13:08 -05003923 spin_lock(&em_tree->lock);
3924 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04003925 if (em)
3926 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05003927 spin_unlock(&em_tree->lock);
3928
Chris Masona52d9a82007-08-27 16:49:44 -04003929 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04003930 if (em->start > start || em->start + em->len <= start)
3931 free_extent_map(em);
3932 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05003933 free_extent_map(em);
3934 else
3935 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04003936 }
Chris Masond1310b22008-01-24 16:13:08 -05003937 em = alloc_extent_map(GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04003938 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05003939 err = -ENOMEM;
3940 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04003941 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003942 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05003943 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05003944 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05003945 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04003946 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04003947
3948 if (!path) {
3949 path = btrfs_alloc_path();
3950 BUG_ON(!path);
3951 }
3952
Chris Mason179e29e2007-11-01 11:28:41 -04003953 ret = btrfs_lookup_file_extent(trans, root, path,
3954 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04003955 if (ret < 0) {
3956 err = ret;
3957 goto out;
3958 }
3959
3960 if (ret != 0) {
3961 if (path->slots[0] == 0)
3962 goto not_found;
3963 path->slots[0]--;
3964 }
3965
Chris Mason5f39d392007-10-15 16:14:19 -04003966 leaf = path->nodes[0];
3967 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04003968 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04003969 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04003970 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3971 found_type = btrfs_key_type(&found_key);
3972 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04003973 found_type != BTRFS_EXTENT_DATA_KEY) {
3974 goto not_found;
3975 }
3976
Chris Mason5f39d392007-10-15 16:14:19 -04003977 found_type = btrfs_file_extent_type(leaf, item);
3978 extent_start = found_key.offset;
Chris Masonc8b97812008-10-29 14:49:59 -04003979 compressed = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04003980 if (found_type == BTRFS_FILE_EXTENT_REG ||
3981 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04003982 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04003983 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04003984 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
3985 size_t size;
3986 size = btrfs_file_extent_inline_len(leaf, item);
3987 extent_end = (extent_start + size + root->sectorsize - 1) &
3988 ~((u64)root->sectorsize - 1);
3989 }
3990
3991 if (start >= extent_end) {
3992 path->slots[0]++;
3993 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
3994 ret = btrfs_next_leaf(root, path);
3995 if (ret < 0) {
3996 err = ret;
3997 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04003998 }
Yan Zheng9036c102008-10-30 14:19:41 -04003999 if (ret > 0)
4000 goto not_found;
4001 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04004002 }
Yan Zheng9036c102008-10-30 14:19:41 -04004003 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4004 if (found_key.objectid != objectid ||
4005 found_key.type != BTRFS_EXTENT_DATA_KEY)
4006 goto not_found;
4007 if (start + len <= found_key.offset)
4008 goto not_found;
4009 em->start = start;
4010 em->len = found_key.offset - start;
4011 goto not_found_em;
4012 }
4013
Yan Zhengd899e052008-10-30 14:25:28 -04004014 if (found_type == BTRFS_FILE_EXTENT_REG ||
4015 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04004016 em->start = extent_start;
4017 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05004018 em->orig_start = extent_start -
4019 btrfs_file_extent_offset(leaf, item);
Chris Masondb945352007-10-15 16:15:53 -04004020 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
4021 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004022 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04004023 goto insert;
4024 }
Chris Masonc8b97812008-10-29 14:49:59 -04004025 if (compressed) {
4026 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
4027 em->block_start = bytenr;
4028 em->block_len = btrfs_file_extent_disk_num_bytes(leaf,
4029 item);
4030 } else {
4031 bytenr += btrfs_file_extent_offset(leaf, item);
4032 em->block_start = bytenr;
4033 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04004034 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
4035 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04004036 }
Chris Masona52d9a82007-08-27 16:49:44 -04004037 goto insert;
4038 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004039 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04004040 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04004041 size_t size;
4042 size_t extent_offset;
4043 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04004044
Chris Masona52d9a82007-08-27 16:49:44 -04004045 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04004046 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04004047 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04004048 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04004049 goto out;
4050 }
4051
Yan Zheng9036c102008-10-30 14:19:41 -04004052 size = btrfs_file_extent_inline_len(leaf, item);
4053 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05004054 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04004055 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04004056 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05004057 em->len = (copy_size + root->sectorsize - 1) &
4058 ~((u64)root->sectorsize - 1);
Yan Zhengff5b7ee2008-11-10 07:34:43 -05004059 em->orig_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04004060 if (compressed)
4061 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Yan689f9342007-10-29 11:41:07 -04004062 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04004063 if (create == 0 && !PageUptodate(page)) {
Chris Masonc8b97812008-10-29 14:49:59 -04004064 if (btrfs_file_extent_compression(leaf, item) ==
4065 BTRFS_COMPRESS_ZLIB) {
4066 ret = uncompress_inline(path, inode, page,
4067 pg_offset,
4068 extent_offset, item);
4069 BUG_ON(ret);
4070 } else {
4071 map = kmap(page);
4072 read_extent_buffer(leaf, map + pg_offset, ptr,
4073 copy_size);
4074 kunmap(page);
4075 }
Chris Mason179e29e2007-11-01 11:28:41 -04004076 flush_dcache_page(page);
4077 } else if (create && PageUptodate(page)) {
4078 if (!trans) {
4079 kunmap(page);
4080 free_extent_map(em);
4081 em = NULL;
4082 btrfs_release_path(root, path);
Chris Masonf9295742008-07-17 12:54:14 -04004083 trans = btrfs_join_transaction(root, 1);
Chris Mason179e29e2007-11-01 11:28:41 -04004084 goto again;
4085 }
Chris Masonc8b97812008-10-29 14:49:59 -04004086 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05004087 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04004088 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04004089 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04004090 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04004091 }
Chris Masond1310b22008-01-24 16:13:08 -05004092 set_extent_uptodate(io_tree, em->start,
4093 extent_map_end(em) - 1, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04004094 goto insert;
4095 } else {
Chris Masond3977122009-01-05 21:25:51 -05004096 printk(KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04004097 WARN_ON(1);
4098 }
4099not_found:
4100 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05004101 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04004102not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04004103 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04004104 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04004105insert:
4106 btrfs_release_path(root, path);
Chris Masond1310b22008-01-24 16:13:08 -05004107 if (em->start > start || extent_map_end(em) <= start) {
Chris Masond3977122009-01-05 21:25:51 -05004108 printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
4109 "[%llu %llu]\n", (unsigned long long)em->start,
4110 (unsigned long long)em->len,
4111 (unsigned long long)start,
4112 (unsigned long long)len);
Chris Masona52d9a82007-08-27 16:49:44 -04004113 err = -EIO;
4114 goto out;
4115 }
Chris Masond1310b22008-01-24 16:13:08 -05004116
4117 err = 0;
4118 spin_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04004119 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04004120 /* it is possible that someone inserted the extent into the tree
4121 * while we had the lock dropped. It is also possible that
4122 * an overlapping map exists in the tree
4123 */
Chris Masona52d9a82007-08-27 16:49:44 -04004124 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04004125 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004126
4127 ret = 0;
4128
Chris Mason3b951512008-04-17 11:29:12 -04004129 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04004130 if (existing && (existing->start > start ||
4131 existing->start + existing->len <= start)) {
4132 free_extent_map(existing);
4133 existing = NULL;
4134 }
Chris Mason3b951512008-04-17 11:29:12 -04004135 if (!existing) {
4136 existing = lookup_extent_mapping(em_tree, em->start,
4137 em->len);
4138 if (existing) {
4139 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04004140 em, start,
4141 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04004142 free_extent_map(existing);
4143 if (err) {
4144 free_extent_map(em);
4145 em = NULL;
4146 }
4147 } else {
4148 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04004149 free_extent_map(em);
4150 em = NULL;
4151 }
4152 } else {
4153 free_extent_map(em);
4154 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004155 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04004156 }
Chris Masona52d9a82007-08-27 16:49:44 -04004157 }
Chris Masond1310b22008-01-24 16:13:08 -05004158 spin_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04004159out:
Chris Masonf4219502008-07-22 11:18:09 -04004160 if (path)
4161 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04004162 if (trans) {
4163 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05004164 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04004165 err = ret;
4166 }
Chris Masona52d9a82007-08-27 16:49:44 -04004167 if (err) {
4168 free_extent_map(em);
4169 WARN_ON(1);
4170 return ERR_PTR(err);
4171 }
4172 return em;
4173}
4174
Chris Mason16432982008-04-10 10:23:21 -04004175static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
4176 const struct iovec *iov, loff_t offset,
4177 unsigned long nr_segs)
4178{
Chris Masone1c4b742008-04-22 13:26:46 -04004179 return -EINVAL;
Chris Mason16432982008-04-10 10:23:21 -04004180}
4181
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004182static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
4183 __u64 start, __u64 len)
4184{
4185 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent);
4186}
4187
Chris Mason9ebefb182007-06-15 13:50:00 -04004188int btrfs_readpage(struct file *file, struct page *page)
4189{
Chris Masond1310b22008-01-24 16:13:08 -05004190 struct extent_io_tree *tree;
4191 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04004192 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04004193}
Chris Mason1832a6d2007-12-21 16:27:21 -05004194
Chris Mason39279cc2007-06-12 06:35:45 -04004195static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
4196{
Chris Masond1310b22008-01-24 16:13:08 -05004197 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04004198
4199
4200 if (current->flags & PF_MEMALLOC) {
4201 redirty_page_for_writepage(wbc, page);
4202 unlock_page(page);
4203 return 0;
4204 }
Chris Masond1310b22008-01-24 16:13:08 -05004205 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04004206 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
4207}
Chris Mason39279cc2007-06-12 06:35:45 -04004208
Chris Masonf4219502008-07-22 11:18:09 -04004209int btrfs_writepages(struct address_space *mapping,
4210 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04004211{
Chris Masond1310b22008-01-24 16:13:08 -05004212 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05004213
Chris Masond1310b22008-01-24 16:13:08 -05004214 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04004215 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
4216}
4217
Chris Mason3ab2fb52007-11-08 10:59:22 -05004218static int
4219btrfs_readpages(struct file *file, struct address_space *mapping,
4220 struct list_head *pages, unsigned nr_pages)
4221{
Chris Masond1310b22008-01-24 16:13:08 -05004222 struct extent_io_tree *tree;
4223 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05004224 return extent_readpages(tree, mapping, pages, nr_pages,
4225 btrfs_get_extent);
4226}
Chris Masone6dcd2d2008-07-17 12:53:50 -04004227static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04004228{
Chris Masond1310b22008-01-24 16:13:08 -05004229 struct extent_io_tree *tree;
4230 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04004231 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004232
Chris Masond1310b22008-01-24 16:13:08 -05004233 tree = &BTRFS_I(page->mapping->host)->io_tree;
4234 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05004235 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04004236 if (ret == 1) {
4237 ClearPagePrivate(page);
4238 set_page_private(page, 0);
4239 page_cache_release(page);
4240 }
4241 return ret;
4242}
Chris Mason39279cc2007-06-12 06:35:45 -04004243
Chris Masone6dcd2d2008-07-17 12:53:50 -04004244static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
4245{
Chris Mason98509cf2008-09-11 15:51:43 -04004246 if (PageWriteback(page) || PageDirty(page))
4247 return 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004248 return __btrfs_releasepage(page, gfp_flags);
4249}
4250
Chris Masona52d9a82007-08-27 16:49:44 -04004251static void btrfs_invalidatepage(struct page *page, unsigned long offset)
4252{
Chris Masond1310b22008-01-24 16:13:08 -05004253 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004254 struct btrfs_ordered_extent *ordered;
4255 u64 page_start = page_offset(page);
4256 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04004257
Chris Masone6dcd2d2008-07-17 12:53:50 -04004258 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05004259 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004260 if (offset) {
4261 btrfs_releasepage(page, GFP_NOFS);
4262 return;
4263 }
4264
4265 lock_extent(tree, page_start, page_end, GFP_NOFS);
4266 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
4267 page_offset(page));
4268 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04004269 /*
4270 * IO on this page will never be started, so we need
4271 * to account for any ordered extents now
4272 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04004273 clear_extent_bit(tree, page_start, page_end,
4274 EXTENT_DIRTY | EXTENT_DELALLOC |
4275 EXTENT_LOCKED, 1, 0, GFP_NOFS);
Chris Mason211f90e2008-07-18 11:56:15 -04004276 btrfs_finish_ordered_io(page->mapping->host,
4277 page_start, page_end);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004278 btrfs_put_ordered_extent(ordered);
4279 lock_extent(tree, page_start, page_end, GFP_NOFS);
4280 }
4281 clear_extent_bit(tree, page_start, page_end,
4282 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
4283 EXTENT_ORDERED,
4284 1, 1, GFP_NOFS);
4285 __btrfs_releasepage(page, GFP_NOFS);
4286
Chris Mason4a096752008-07-21 10:29:44 -04004287 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04004288 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04004289 ClearPagePrivate(page);
4290 set_page_private(page, 0);
4291 page_cache_release(page);
4292 }
Chris Mason39279cc2007-06-12 06:35:45 -04004293}
4294
Chris Mason9ebefb182007-06-15 13:50:00 -04004295/*
4296 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
4297 * called from a page fault handler when a page is first dirtied. Hence we must
4298 * be careful to check for EOF conditions here. We set the page up correctly
4299 * for a written page which means we get ENOSPC checking when writing into
4300 * holes and correct delalloc and unwritten extent mapping on filesystems that
4301 * support these features.
4302 *
4303 * We are not allowed to take the i_mutex here so we have to play games to
4304 * protect against truncate races as the page could now be beyond EOF. Because
4305 * vmtruncate() writes the inode size before removing pages, once we have the
4306 * page lock we can determine safely if the page is beyond EOF. If it is not
4307 * beyond EOF, then the page is guaranteed safe against truncation until we
4308 * unlock the page.
4309 */
4310int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
4311{
Chris Mason6da6aba2007-12-18 16:15:09 -05004312 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05004313 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004314 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4315 struct btrfs_ordered_extent *ordered;
4316 char *kaddr;
4317 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04004318 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05004319 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04004320 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004321 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04004322
Chris Mason1832a6d2007-12-21 16:27:21 -05004323 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
Chris Mason1832a6d2007-12-21 16:27:21 -05004324 if (ret)
4325 goto out;
4326
4327 ret = -EINVAL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004328again:
Chris Mason9ebefb182007-06-15 13:50:00 -04004329 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04004330 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004331 page_start = page_offset(page);
4332 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04004333
Chris Mason9ebefb182007-06-15 13:50:00 -04004334 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04004335 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04004336 /* page got truncated out from underneath us */
4337 goto out_unlock;
4338 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004339 wait_on_page_writeback(page);
4340
4341 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
4342 set_page_extent_mapped(page);
4343
Chris Masoneb84ae02008-07-17 13:53:27 -04004344 /*
4345 * we can't set the delalloc bits if there are pending ordered
4346 * extents. Drop our locks and wait for them to finish
4347 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04004348 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4349 if (ordered) {
4350 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
4351 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004352 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004353 btrfs_put_ordered_extent(ordered);
4354 goto again;
4355 }
4356
Chris Masonea8c2812008-08-04 23:17:27 -04004357 btrfs_set_extent_delalloc(inode, page_start, page_end);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004358 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04004359
4360 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04004361 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04004362 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04004363 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04004364 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04004365
Chris Masone6dcd2d2008-07-17 12:53:50 -04004366 if (zero_start != PAGE_CACHE_SIZE) {
4367 kaddr = kmap(page);
4368 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
4369 flush_dcache_page(page);
4370 kunmap(page);
4371 }
Chris Mason247e7432008-07-17 12:53:51 -04004372 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004373 set_page_dirty(page);
4374 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04004375
4376out_unlock:
4377 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05004378out:
Chris Mason9ebefb182007-06-15 13:50:00 -04004379 return ret;
4380}
4381
Chris Mason39279cc2007-06-12 06:35:45 -04004382static void btrfs_truncate(struct inode *inode)
4383{
4384 struct btrfs_root *root = BTRFS_I(inode)->root;
4385 int ret;
4386 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004387 unsigned long nr;
Chris Masondbe674a2008-07-17 12:54:05 -04004388 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04004389
4390 if (!S_ISREG(inode->i_mode))
4391 return;
4392 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
4393 return;
4394
4395 btrfs_truncate_page(inode->i_mapping, inode->i_size);
Chris Mason4a096752008-07-21 10:29:44 -04004396 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04004397
Chris Mason39279cc2007-06-12 06:35:45 -04004398 trans = btrfs_start_transaction(root, 1);
4399 btrfs_set_trans_block_group(trans, inode);
Chris Masondbe674a2008-07-17 12:54:05 -04004400 btrfs_i_size_write(inode, inode->i_size);
Chris Mason39279cc2007-06-12 06:35:45 -04004401
Josef Bacik7b128762008-07-24 12:17:14 -04004402 ret = btrfs_orphan_add(trans, inode);
4403 if (ret)
4404 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04004405 /* FIXME, add redo link to tree so we don't leak on crash */
Chris Masone02119d2008-09-05 16:13:11 -04004406 ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size,
Chris Mason85e21ba2008-01-29 15:11:36 -05004407 BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004408 btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004409
Josef Bacik7b128762008-07-24 12:17:14 -04004410 ret = btrfs_orphan_del(trans, inode);
4411 BUG_ON(ret);
4412
4413out:
4414 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04004415 ret = btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004416 BUG_ON(ret);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004417 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004418}
4419
Sven Wegener3b963622008-06-09 21:57:42 -04004420/*
Chris Masond352ac62008-09-29 15:18:18 -04004421 * create a new subvolume directory/inode (helper for the ioctl).
4422 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05004423int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
4424 struct btrfs_root *new_root, struct dentry *dentry,
4425 u64 new_dirid, u64 alloc_hint)
Chris Mason39279cc2007-06-12 06:35:45 -04004426{
Chris Mason39279cc2007-06-12 06:35:45 -04004427 struct inode *inode;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04004428 int error;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004429 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004430
Josef Bacikaec74772008-07-24 12:12:38 -04004431 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
Yan Zhengd2fb3432008-12-11 16:30:39 -05004432 new_dirid, alloc_hint, S_IFDIR | 0700, &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04004433 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004434 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004435 inode->i_op = &btrfs_dir_inode_operations;
4436 inode->i_fop = &btrfs_dir_file_operations;
4437
Chris Mason39279cc2007-06-12 06:35:45 -04004438 inode->i_nlink = 1;
Chris Masondbe674a2008-07-17 12:54:05 -04004439 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04004440
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04004441 error = btrfs_update_inode(trans, new_root, inode);
4442 if (error)
4443 return error;
4444
4445 d_instantiate(dentry, inode);
4446 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004447}
4448
Chris Masond352ac62008-09-29 15:18:18 -04004449/* helper function for file defrag and space balancing. This
4450 * forces readahead on a given range of bytes in an inode
4451 */
Chris Masonedbd8d42007-12-21 16:27:24 -05004452unsigned long btrfs_force_ra(struct address_space *mapping,
Chris Mason86479a02007-09-10 19:58:16 -04004453 struct file_ra_state *ra, struct file *file,
4454 pgoff_t offset, pgoff_t last_index)
4455{
Chris Mason8e7bf942008-04-28 09:02:36 -04004456 pgoff_t req_size = last_index - offset + 1;
Chris Mason86479a02007-09-10 19:58:16 -04004457
Chris Mason86479a02007-09-10 19:58:16 -04004458 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
4459 return offset + req_size;
Chris Mason86479a02007-09-10 19:58:16 -04004460}
4461
Chris Mason39279cc2007-06-12 06:35:45 -04004462struct inode *btrfs_alloc_inode(struct super_block *sb)
4463{
4464 struct btrfs_inode *ei;
4465
4466 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
4467 if (!ei)
4468 return NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04004469 ei->last_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004470 ei->logged_trans = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004471 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Josef Bacik33268ea2008-07-24 12:16:36 -04004472 ei->i_acl = BTRFS_ACL_NOT_CACHED;
4473 ei->i_default_acl = BTRFS_ACL_NOT_CACHED;
Josef Bacik7b128762008-07-24 12:17:14 -04004474 INIT_LIST_HEAD(&ei->i_orphan);
Chris Mason39279cc2007-06-12 06:35:45 -04004475 return &ei->vfs_inode;
4476}
4477
4478void btrfs_destroy_inode(struct inode *inode)
4479{
Chris Masone6dcd2d2008-07-17 12:53:50 -04004480 struct btrfs_ordered_extent *ordered;
Chris Mason39279cc2007-06-12 06:35:45 -04004481 WARN_ON(!list_empty(&inode->i_dentry));
4482 WARN_ON(inode->i_data.nrpages);
4483
Josef Bacik33268ea2008-07-24 12:16:36 -04004484 if (BTRFS_I(inode)->i_acl &&
4485 BTRFS_I(inode)->i_acl != BTRFS_ACL_NOT_CACHED)
4486 posix_acl_release(BTRFS_I(inode)->i_acl);
4487 if (BTRFS_I(inode)->i_default_acl &&
4488 BTRFS_I(inode)->i_default_acl != BTRFS_ACL_NOT_CACHED)
4489 posix_acl_release(BTRFS_I(inode)->i_default_acl);
4490
Yanbcc63ab2008-07-30 16:29:20 -04004491 spin_lock(&BTRFS_I(inode)->root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04004492 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
4493 printk(KERN_ERR "BTRFS: inode %lu: inode still on the orphan"
4494 " list\n", inode->i_ino);
4495 dump_stack();
4496 }
Yanbcc63ab2008-07-30 16:29:20 -04004497 spin_unlock(&BTRFS_I(inode)->root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04004498
Chris Masond3977122009-01-05 21:25:51 -05004499 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04004500 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
4501 if (!ordered)
4502 break;
4503 else {
Chris Masond3977122009-01-05 21:25:51 -05004504 printk(KERN_ERR "btrfs found ordered "
4505 "extent %llu %llu on inode cleanup\n",
4506 (unsigned long long)ordered->file_offset,
4507 (unsigned long long)ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004508 btrfs_remove_ordered_extent(inode, ordered);
4509 btrfs_put_ordered_extent(ordered);
4510 btrfs_put_ordered_extent(ordered);
4511 }
4512 }
Zheng Yan5b21f2e2008-09-26 10:05:38 -04004513 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004514 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
4515}
4516
Sven Wegener0ee0fda2008-07-30 16:54:26 -04004517static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04004518{
4519 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
4520
4521 inode_init_once(&ei->vfs_inode);
4522}
4523
4524void btrfs_destroy_cachep(void)
4525{
4526 if (btrfs_inode_cachep)
4527 kmem_cache_destroy(btrfs_inode_cachep);
4528 if (btrfs_trans_handle_cachep)
4529 kmem_cache_destroy(btrfs_trans_handle_cachep);
4530 if (btrfs_transaction_cachep)
4531 kmem_cache_destroy(btrfs_transaction_cachep);
4532 if (btrfs_bit_radix_cachep)
4533 kmem_cache_destroy(btrfs_bit_radix_cachep);
4534 if (btrfs_path_cachep)
4535 kmem_cache_destroy(btrfs_path_cachep);
4536}
4537
Chris Mason86479a02007-09-10 19:58:16 -04004538struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
Chris Mason92fee662007-07-25 12:31:35 -04004539 unsigned long extra_flags,
Chris Mason2b1f55b2008-09-24 11:48:04 -04004540 void (*ctor)(void *))
Chris Mason92fee662007-07-25 12:31:35 -04004541{
4542 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
Chris Mason2b1f55b2008-09-24 11:48:04 -04004543 SLAB_MEM_SPREAD | extra_flags), ctor);
Chris Mason92fee662007-07-25 12:31:35 -04004544}
4545
Chris Mason39279cc2007-06-12 06:35:45 -04004546int btrfs_init_cachep(void)
4547{
Chris Mason86479a02007-09-10 19:58:16 -04004548 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
Chris Mason92fee662007-07-25 12:31:35 -04004549 sizeof(struct btrfs_inode),
4550 0, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04004551 if (!btrfs_inode_cachep)
4552 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04004553 btrfs_trans_handle_cachep =
4554 btrfs_cache_create("btrfs_trans_handle_cache",
4555 sizeof(struct btrfs_trans_handle),
4556 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004557 if (!btrfs_trans_handle_cachep)
4558 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04004559 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
Chris Mason39279cc2007-06-12 06:35:45 -04004560 sizeof(struct btrfs_transaction),
Chris Mason92fee662007-07-25 12:31:35 -04004561 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004562 if (!btrfs_transaction_cachep)
4563 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04004564 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
Yan23223582007-09-17 11:08:52 -04004565 sizeof(struct btrfs_path),
Chris Mason92fee662007-07-25 12:31:35 -04004566 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004567 if (!btrfs_path_cachep)
4568 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04004569 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
Chris Mason92fee662007-07-25 12:31:35 -04004570 SLAB_DESTROY_BY_RCU, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004571 if (!btrfs_bit_radix_cachep)
4572 goto fail;
4573 return 0;
4574fail:
4575 btrfs_destroy_cachep();
4576 return -ENOMEM;
4577}
4578
4579static int btrfs_getattr(struct vfsmount *mnt,
4580 struct dentry *dentry, struct kstat *stat)
4581{
4582 struct inode *inode = dentry->d_inode;
4583 generic_fillattr(inode, stat);
Chris Mason3394e162008-11-17 20:42:26 -05004584 stat->dev = BTRFS_I(inode)->root->anon_super.s_dev;
Chris Masond6667462008-01-03 14:51:00 -05004585 stat->blksize = PAGE_CACHE_SIZE;
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004586 stat->blocks = (inode_get_bytes(inode) +
4587 BTRFS_I(inode)->delalloc_bytes) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04004588 return 0;
4589}
4590
Chris Masond3977122009-01-05 21:25:51 -05004591static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
4592 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04004593{
4594 struct btrfs_trans_handle *trans;
4595 struct btrfs_root *root = BTRFS_I(old_dir)->root;
4596 struct inode *new_inode = new_dentry->d_inode;
4597 struct inode *old_inode = old_dentry->d_inode;
4598 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004599 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004600 int ret;
4601
Chris Mason3394e162008-11-17 20:42:26 -05004602 /* we're not allowed to rename between subvolumes */
4603 if (BTRFS_I(old_inode)->root->root_key.objectid !=
4604 BTRFS_I(new_dir)->root->root_key.objectid)
4605 return -EXDEV;
4606
Chris Mason39279cc2007-06-12 06:35:45 -04004607 if (S_ISDIR(old_inode->i_mode) && new_inode &&
4608 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
4609 return -ENOTEMPTY;
4610 }
Chris Mason5f39d392007-10-15 16:14:19 -04004611
Chris Mason0660b5a2008-11-17 20:37:39 -05004612 /* to rename a snapshot or subvolume, we need to juggle the
4613 * backrefs. This isn't coded yet
4614 */
4615 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
4616 return -EXDEV;
4617
Chris Mason1832a6d2007-12-21 16:27:21 -05004618 ret = btrfs_check_free_space(root, 1, 0);
4619 if (ret)
4620 goto out_unlock;
4621
Chris Mason39279cc2007-06-12 06:35:45 -04004622 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04004623
Chris Mason39279cc2007-06-12 06:35:45 -04004624 btrfs_set_trans_block_group(trans, new_dir);
Chris Mason39279cc2007-06-12 06:35:45 -04004625
Chris Masone02119d2008-09-05 16:13:11 -04004626 btrfs_inc_nlink(old_dentry->d_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004627 old_dir->i_ctime = old_dir->i_mtime = ctime;
4628 new_dir->i_ctime = new_dir->i_mtime = ctime;
4629 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04004630
Chris Masone02119d2008-09-05 16:13:11 -04004631 ret = btrfs_unlink_inode(trans, root, old_dir, old_dentry->d_inode,
4632 old_dentry->d_name.name,
4633 old_dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04004634 if (ret)
4635 goto out_fail;
4636
4637 if (new_inode) {
4638 new_inode->i_ctime = CURRENT_TIME;
Chris Masone02119d2008-09-05 16:13:11 -04004639 ret = btrfs_unlink_inode(trans, root, new_dir,
4640 new_dentry->d_inode,
4641 new_dentry->d_name.name,
4642 new_dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04004643 if (ret)
4644 goto out_fail;
Josef Bacik7b128762008-07-24 12:17:14 -04004645 if (new_inode->i_nlink == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004646 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Josef Bacik7b128762008-07-24 12:17:14 -04004647 if (ret)
4648 goto out_fail;
4649 }
Chris Masone02119d2008-09-05 16:13:11 -04004650
Chris Mason39279cc2007-06-12 06:35:45 -04004651 }
Chris Mason3de45862008-11-17 21:02:50 -05004652 ret = btrfs_set_inode_index(new_dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004653 if (ret)
4654 goto out_fail;
4655
Chris Masone02119d2008-09-05 16:13:11 -04004656 ret = btrfs_add_link(trans, new_dentry->d_parent->d_inode,
4657 old_inode, new_dentry->d_name.name,
4658 new_dentry->d_name.len, 1, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004659 if (ret)
4660 goto out_fail;
4661
4662out_fail:
Chris Masonab78c842008-07-29 16:15:18 -04004663 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05004664out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04004665 return ret;
4666}
4667
Chris Masond352ac62008-09-29 15:18:18 -04004668/*
4669 * some fairly slow code that needs optimization. This walks the list
4670 * of all the inodes with pending delalloc and forces them to disk.
4671 */
Chris Masonea8c2812008-08-04 23:17:27 -04004672int btrfs_start_delalloc_inodes(struct btrfs_root *root)
4673{
4674 struct list_head *head = &root->fs_info->delalloc_inodes;
4675 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04004676 struct inode *inode;
Chris Masonea8c2812008-08-04 23:17:27 -04004677
Yan Zhengc146afa2008-11-12 14:34:12 -05004678 if (root->fs_info->sb->s_flags & MS_RDONLY)
4679 return -EROFS;
4680
Chris Mason75eff682008-12-15 15:54:40 -05004681 spin_lock(&root->fs_info->delalloc_lock);
Chris Masond3977122009-01-05 21:25:51 -05004682 while (!list_empty(head)) {
Chris Masonea8c2812008-08-04 23:17:27 -04004683 binode = list_entry(head->next, struct btrfs_inode,
4684 delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04004685 inode = igrab(&binode->vfs_inode);
4686 if (!inode)
4687 list_del_init(&binode->delalloc_inodes);
Chris Mason75eff682008-12-15 15:54:40 -05004688 spin_unlock(&root->fs_info->delalloc_lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04004689 if (inode) {
Chris Mason8c8bee12008-09-29 11:19:10 -04004690 filemap_flush(inode->i_mapping);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04004691 iput(inode);
4692 }
4693 cond_resched();
Chris Mason75eff682008-12-15 15:54:40 -05004694 spin_lock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04004695 }
Chris Mason75eff682008-12-15 15:54:40 -05004696 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04004697
4698 /* the filemap_flush will queue IO into the worker threads, but
4699 * we have to make sure the IO is actually started and that
4700 * ordered extents get created before we return
4701 */
4702 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05004703 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05004704 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04004705 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05004706 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
4707 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04004708 }
4709 atomic_dec(&root->fs_info->async_submit_draining);
Chris Masonea8c2812008-08-04 23:17:27 -04004710 return 0;
4711}
4712
Chris Mason39279cc2007-06-12 06:35:45 -04004713static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
4714 const char *symname)
4715{
4716 struct btrfs_trans_handle *trans;
4717 struct btrfs_root *root = BTRFS_I(dir)->root;
4718 struct btrfs_path *path;
4719 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05004720 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04004721 int err;
4722 int drop_inode = 0;
4723 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004724 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04004725 int name_len;
4726 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04004727 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04004728 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04004729 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05004730 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004731
4732 name_len = strlen(symname) + 1;
4733 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
4734 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05004735
Chris Mason1832a6d2007-12-21 16:27:21 -05004736 err = btrfs_check_free_space(root, 1, 0);
4737 if (err)
4738 goto out_fail;
4739
Chris Mason39279cc2007-06-12 06:35:45 -04004740 trans = btrfs_start_transaction(root, 1);
4741 btrfs_set_trans_block_group(trans, dir);
4742
4743 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
4744 if (err) {
4745 err = -ENOSPC;
4746 goto out_unlock;
4747 }
4748
Josef Bacikaec74772008-07-24 12:12:38 -04004749 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05004750 dentry->d_name.len,
4751 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04004752 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO,
4753 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04004754 err = PTR_ERR(inode);
4755 if (IS_ERR(inode))
4756 goto out_unlock;
4757
Jim Owens0279b4c2009-02-04 09:29:13 -05004758 err = btrfs_init_inode_security(inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04004759 if (err) {
4760 drop_inode = 1;
4761 goto out_unlock;
4762 }
4763
Chris Mason39279cc2007-06-12 06:35:45 -04004764 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04004765 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004766 if (err)
4767 drop_inode = 1;
4768 else {
4769 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04004770 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04004771 inode->i_fop = &btrfs_file_operations;
4772 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05004773 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04004774 }
4775 dir->i_sb->s_dirt = 1;
4776 btrfs_update_inode_block_group(trans, inode);
4777 btrfs_update_inode_block_group(trans, dir);
4778 if (drop_inode)
4779 goto out_unlock;
4780
4781 path = btrfs_alloc_path();
4782 BUG_ON(!path);
4783 key.objectid = inode->i_ino;
4784 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004785 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
4786 datasize = btrfs_file_extent_calc_inline_size(name_len);
4787 err = btrfs_insert_empty_item(trans, root, path, &key,
4788 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04004789 if (err) {
4790 drop_inode = 1;
4791 goto out_unlock;
4792 }
Chris Mason5f39d392007-10-15 16:14:19 -04004793 leaf = path->nodes[0];
4794 ei = btrfs_item_ptr(leaf, path->slots[0],
4795 struct btrfs_file_extent_item);
4796 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
4797 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04004798 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04004799 btrfs_set_file_extent_encryption(leaf, ei, 0);
4800 btrfs_set_file_extent_compression(leaf, ei, 0);
4801 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
4802 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
4803
Chris Mason39279cc2007-06-12 06:35:45 -04004804 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04004805 write_extent_buffer(leaf, symname, ptr, name_len);
4806 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004807 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04004808
Chris Mason39279cc2007-06-12 06:35:45 -04004809 inode->i_op = &btrfs_symlink_inode_operations;
4810 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04004811 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04004812 inode_set_bytes(inode, name_len);
Chris Masondbe674a2008-07-17 12:54:05 -04004813 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04004814 err = btrfs_update_inode(trans, root, inode);
4815 if (err)
4816 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04004817
4818out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004819 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04004820 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05004821out_fail:
Chris Mason39279cc2007-06-12 06:35:45 -04004822 if (drop_inode) {
4823 inode_dec_link_count(inode);
4824 iput(inode);
4825 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004826 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004827 return err;
4828}
Chris Mason16432982008-04-10 10:23:21 -04004829
Yan Zhengd899e052008-10-30 14:25:28 -04004830static int prealloc_file_range(struct inode *inode, u64 start, u64 end,
4831 u64 alloc_hint, int mode)
4832{
4833 struct btrfs_trans_handle *trans;
4834 struct btrfs_root *root = BTRFS_I(inode)->root;
4835 struct btrfs_key ins;
4836 u64 alloc_size;
4837 u64 cur_offset = start;
4838 u64 num_bytes = end - start;
4839 int ret = 0;
4840
4841 trans = btrfs_join_transaction(root, 1);
4842 BUG_ON(!trans);
4843 btrfs_set_trans_block_group(trans, inode);
4844
4845 while (num_bytes > 0) {
4846 alloc_size = min(num_bytes, root->fs_info->max_extent);
4847 ret = btrfs_reserve_extent(trans, root, alloc_size,
4848 root->sectorsize, 0, alloc_hint,
4849 (u64)-1, &ins, 1);
4850 if (ret) {
4851 WARN_ON(1);
4852 goto out;
4853 }
4854 ret = insert_reserved_file_extent(trans, inode,
4855 cur_offset, ins.objectid,
4856 ins.offset, ins.offset,
4857 ins.offset, 0, 0, 0,
4858 BTRFS_FILE_EXTENT_PREALLOC);
4859 BUG_ON(ret);
4860 num_bytes -= ins.offset;
4861 cur_offset += ins.offset;
4862 alloc_hint = ins.objectid + ins.offset;
4863 }
4864out:
4865 if (cur_offset > start) {
4866 inode->i_ctime = CURRENT_TIME;
4867 btrfs_set_flag(inode, PREALLOC);
4868 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
4869 cur_offset > i_size_read(inode))
4870 btrfs_i_size_write(inode, cur_offset);
4871 ret = btrfs_update_inode(trans, root, inode);
4872 BUG_ON(ret);
4873 }
4874
4875 btrfs_end_transaction(trans, root);
4876 return ret;
4877}
4878
4879static long btrfs_fallocate(struct inode *inode, int mode,
4880 loff_t offset, loff_t len)
4881{
4882 u64 cur_offset;
4883 u64 last_byte;
4884 u64 alloc_start;
4885 u64 alloc_end;
4886 u64 alloc_hint = 0;
4887 u64 mask = BTRFS_I(inode)->root->sectorsize - 1;
4888 struct extent_map *em;
4889 int ret;
4890
4891 alloc_start = offset & ~mask;
4892 alloc_end = (offset + len + mask) & ~mask;
4893
4894 mutex_lock(&inode->i_mutex);
4895 if (alloc_start > inode->i_size) {
4896 ret = btrfs_cont_expand(inode, alloc_start);
4897 if (ret)
4898 goto out;
4899 }
4900
4901 while (1) {
4902 struct btrfs_ordered_extent *ordered;
4903 lock_extent(&BTRFS_I(inode)->io_tree, alloc_start,
4904 alloc_end - 1, GFP_NOFS);
4905 ordered = btrfs_lookup_first_ordered_extent(inode,
4906 alloc_end - 1);
4907 if (ordered &&
4908 ordered->file_offset + ordered->len > alloc_start &&
4909 ordered->file_offset < alloc_end) {
4910 btrfs_put_ordered_extent(ordered);
4911 unlock_extent(&BTRFS_I(inode)->io_tree,
4912 alloc_start, alloc_end - 1, GFP_NOFS);
4913 btrfs_wait_ordered_range(inode, alloc_start,
4914 alloc_end - alloc_start);
4915 } else {
4916 if (ordered)
4917 btrfs_put_ordered_extent(ordered);
4918 break;
4919 }
4920 }
4921
4922 cur_offset = alloc_start;
4923 while (1) {
4924 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4925 alloc_end - cur_offset, 0);
4926 BUG_ON(IS_ERR(em) || !em);
4927 last_byte = min(extent_map_end(em), alloc_end);
4928 last_byte = (last_byte + mask) & ~mask;
4929 if (em->block_start == EXTENT_MAP_HOLE) {
4930 ret = prealloc_file_range(inode, cur_offset,
4931 last_byte, alloc_hint, mode);
4932 if (ret < 0) {
4933 free_extent_map(em);
4934 break;
4935 }
4936 }
4937 if (em->block_start <= EXTENT_MAP_LAST_BYTE)
4938 alloc_hint = em->block_start;
4939 free_extent_map(em);
4940
4941 cur_offset = last_byte;
4942 if (cur_offset >= alloc_end) {
4943 ret = 0;
4944 break;
4945 }
4946 }
4947 unlock_extent(&BTRFS_I(inode)->io_tree, alloc_start, alloc_end - 1,
4948 GFP_NOFS);
4949out:
4950 mutex_unlock(&inode->i_mutex);
4951 return ret;
4952}
4953
Chris Masone6dcd2d2008-07-17 12:53:50 -04004954static int btrfs_set_page_dirty(struct page *page)
4955{
Chris Masone6dcd2d2008-07-17 12:53:50 -04004956 return __set_page_dirty_nobuffers(page);
4957}
4958
Sven Wegener0ee0fda2008-07-30 16:54:26 -04004959static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05004960{
4961 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
4962 return -EACCES;
Josef Bacik33268ea2008-07-24 12:16:36 -04004963 return generic_permission(inode, mask, btrfs_check_acl);
Yanfdebe2b2008-01-14 13:26:08 -05004964}
Chris Mason39279cc2007-06-12 06:35:45 -04004965
4966static struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05004967 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04004968 .lookup = btrfs_lookup,
4969 .create = btrfs_create,
4970 .unlink = btrfs_unlink,
4971 .link = btrfs_link,
4972 .mkdir = btrfs_mkdir,
4973 .rmdir = btrfs_rmdir,
4974 .rename = btrfs_rename,
4975 .symlink = btrfs_symlink,
4976 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04004977 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04004978 .setxattr = btrfs_setxattr,
4979 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05004980 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04004981 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05004982 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04004983};
Chris Mason39279cc2007-06-12 06:35:45 -04004984static struct inode_operations btrfs_dir_ro_inode_operations = {
4985 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05004986 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04004987};
Chris Mason39279cc2007-06-12 06:35:45 -04004988static struct file_operations btrfs_dir_file_operations = {
4989 .llseek = generic_file_llseek,
4990 .read = generic_read_dir,
David Woodhousecbdf5a22008-08-06 19:42:33 +01004991 .readdir = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04004992 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04004993#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04004994 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04004995#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04004996 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04004997 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04004998};
4999
Chris Masond1310b22008-01-24 16:13:08 -05005000static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04005001 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05005002 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04005003 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04005004 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005005 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04005006 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Mason1259ab72008-05-12 13:39:03 -04005007 .readpage_io_failed_hook = btrfs_io_failed_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05005008 .set_bit_hook = btrfs_set_bit_hook,
5009 .clear_bit_hook = btrfs_clear_bit_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04005010};
5011
Chris Mason35054392009-01-21 13:11:13 -05005012/*
5013 * btrfs doesn't support the bmap operation because swapfiles
5014 * use bmap to make a mapping of extents in the file. They assume
5015 * these extents won't change over the life of the file and they
5016 * use the bmap result to do IO directly to the drive.
5017 *
5018 * the btrfs bmap call would return logical addresses that aren't
5019 * suitable for IO and they also will change frequently as COW
5020 * operations happen. So, swapfile + btrfs == corruption.
5021 *
5022 * For now we're avoiding this by dropping bmap.
5023 */
Chris Mason39279cc2007-06-12 06:35:45 -04005024static struct address_space_operations btrfs_aops = {
5025 .readpage = btrfs_readpage,
5026 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04005027 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05005028 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04005029 .sync_page = block_sync_page,
Chris Mason16432982008-04-10 10:23:21 -04005030 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04005031 .invalidatepage = btrfs_invalidatepage,
5032 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005033 .set_page_dirty = btrfs_set_page_dirty,
Chris Mason39279cc2007-06-12 06:35:45 -04005034};
5035
5036static struct address_space_operations btrfs_symlink_aops = {
5037 .readpage = btrfs_readpage,
5038 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04005039 .invalidatepage = btrfs_invalidatepage,
5040 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04005041};
5042
5043static struct inode_operations btrfs_file_inode_operations = {
5044 .truncate = btrfs_truncate,
5045 .getattr = btrfs_getattr,
5046 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04005047 .setxattr = btrfs_setxattr,
5048 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05005049 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04005050 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05005051 .permission = btrfs_permission,
Yan Zhengd899e052008-10-30 14:25:28 -04005052 .fallocate = btrfs_fallocate,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05005053 .fiemap = btrfs_fiemap,
Chris Mason39279cc2007-06-12 06:35:45 -04005054};
Josef Bacik618e21d2007-07-11 10:18:17 -04005055static struct inode_operations btrfs_special_inode_operations = {
5056 .getattr = btrfs_getattr,
5057 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05005058 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04005059 .setxattr = btrfs_setxattr,
5060 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04005061 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04005062 .removexattr = btrfs_removexattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04005063};
Chris Mason39279cc2007-06-12 06:35:45 -04005064static struct inode_operations btrfs_symlink_inode_operations = {
5065 .readlink = generic_readlink,
5066 .follow_link = page_follow_link_light,
5067 .put_link = page_put_link,
Yanfdebe2b2008-01-14 13:26:08 -05005068 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05005069 .setxattr = btrfs_setxattr,
5070 .getxattr = btrfs_getxattr,
5071 .listxattr = btrfs_listxattr,
5072 .removexattr = btrfs_removexattr,
Chris Mason39279cc2007-06-12 06:35:45 -04005073};