blob: 7d4f948bc22a5b5c811b93b385ff36d5b49e8977 [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/*
Chris Masonc8b97812008-10-29 14:49:59 -0400105 * this does all the hard work for inserting an inline extent into
106 * the btree. The caller should have done a btrfs_drop_extents so that
107 * no overlapping inline items exist in the btree
108 */
Chris Masond3977122009-01-05 21:25:51 -0500109static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400110 struct btrfs_root *root, struct inode *inode,
111 u64 start, size_t size, size_t compressed_size,
112 struct page **compressed_pages)
113{
114 struct btrfs_key key;
115 struct btrfs_path *path;
116 struct extent_buffer *leaf;
117 struct page *page = NULL;
118 char *kaddr;
119 unsigned long ptr;
120 struct btrfs_file_extent_item *ei;
121 int err = 0;
122 int ret;
123 size_t cur_size = size;
124 size_t datasize;
125 unsigned long offset;
126 int use_compress = 0;
127
128 if (compressed_size && compressed_pages) {
129 use_compress = 1;
130 cur_size = compressed_size;
131 }
132
Chris Masond3977122009-01-05 21:25:51 -0500133 path = btrfs_alloc_path();
134 if (!path)
Chris Masonc8b97812008-10-29 14:49:59 -0400135 return -ENOMEM;
136
137 btrfs_set_trans_block_group(trans, inode);
138
139 key.objectid = inode->i_ino;
140 key.offset = start;
141 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
Chris Masonc8b97812008-10-29 14:49:59 -0400142 datasize = btrfs_file_extent_calc_inline_size(cur_size);
143
144 inode_add_bytes(inode, size);
145 ret = btrfs_insert_empty_item(trans, root, path, &key,
146 datasize);
147 BUG_ON(ret);
148 if (ret) {
149 err = ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400150 goto fail;
151 }
152 leaf = path->nodes[0];
153 ei = btrfs_item_ptr(leaf, path->slots[0],
154 struct btrfs_file_extent_item);
155 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
156 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
157 btrfs_set_file_extent_encryption(leaf, ei, 0);
158 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
159 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
160 ptr = btrfs_file_extent_inline_start(ei);
161
162 if (use_compress) {
163 struct page *cpage;
164 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500165 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400166 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500167 cur_size = min_t(unsigned long, compressed_size,
Chris Masonc8b97812008-10-29 14:49:59 -0400168 PAGE_CACHE_SIZE);
169
170 kaddr = kmap(cpage);
171 write_extent_buffer(leaf, kaddr, ptr, cur_size);
172 kunmap(cpage);
173
174 i++;
175 ptr += cur_size;
176 compressed_size -= cur_size;
177 }
178 btrfs_set_file_extent_compression(leaf, ei,
179 BTRFS_COMPRESS_ZLIB);
180 } else {
181 page = find_get_page(inode->i_mapping,
182 start >> PAGE_CACHE_SHIFT);
183 btrfs_set_file_extent_compression(leaf, ei, 0);
184 kaddr = kmap_atomic(page, KM_USER0);
185 offset = start & (PAGE_CACHE_SIZE - 1);
186 write_extent_buffer(leaf, kaddr + offset, ptr, size);
187 kunmap_atomic(kaddr, KM_USER0);
188 page_cache_release(page);
189 }
190 btrfs_mark_buffer_dirty(leaf);
191 btrfs_free_path(path);
192
193 BTRFS_I(inode)->disk_i_size = inode->i_size;
194 btrfs_update_inode(trans, root, inode);
195 return 0;
196fail:
197 btrfs_free_path(path);
198 return err;
199}
200
201
202/*
203 * conditionally insert an inline extent into the file. This
204 * does the checks required to make sure the data is small enough
205 * to fit as an inline extent.
206 */
207static int cow_file_range_inline(struct btrfs_trans_handle *trans,
208 struct btrfs_root *root,
209 struct inode *inode, u64 start, u64 end,
210 size_t compressed_size,
211 struct page **compressed_pages)
212{
213 u64 isize = i_size_read(inode);
214 u64 actual_end = min(end + 1, isize);
215 u64 inline_len = actual_end - start;
216 u64 aligned_end = (end + root->sectorsize - 1) &
217 ~((u64)root->sectorsize - 1);
218 u64 hint_byte;
219 u64 data_len = inline_len;
220 int ret;
221
222 if (compressed_size)
223 data_len = compressed_size;
224
225 if (start > 0 ||
Chris Mason70b99e62008-10-31 12:46:39 -0400226 actual_end >= PAGE_CACHE_SIZE ||
Chris Masonc8b97812008-10-29 14:49:59 -0400227 data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
228 (!compressed_size &&
229 (actual_end & (root->sectorsize - 1)) == 0) ||
230 end + 1 < isize ||
231 data_len > root->fs_info->max_inline) {
232 return 1;
233 }
234
Chris Masonc8b97812008-10-29 14:49:59 -0400235 ret = btrfs_drop_extents(trans, root, inode, start,
Chris Mason70b99e62008-10-31 12:46:39 -0400236 aligned_end, start, &hint_byte);
Chris Masonc8b97812008-10-29 14:49:59 -0400237 BUG_ON(ret);
238
239 if (isize > actual_end)
240 inline_len = min_t(u64, isize, actual_end);
241 ret = insert_inline_extent(trans, root, inode, start,
242 inline_len, compressed_size,
243 compressed_pages);
244 BUG_ON(ret);
245 btrfs_drop_extent_cache(inode, start, aligned_end, 0);
Chris Masonc8b97812008-10-29 14:49:59 -0400246 return 0;
247}
248
Chris Mason771ed682008-11-06 22:02:51 -0500249struct async_extent {
250 u64 start;
251 u64 ram_size;
252 u64 compressed_size;
253 struct page **pages;
254 unsigned long nr_pages;
255 struct list_head list;
256};
257
258struct async_cow {
259 struct inode *inode;
260 struct btrfs_root *root;
261 struct page *locked_page;
262 u64 start;
263 u64 end;
264 struct list_head extents;
265 struct btrfs_work work;
266};
267
268static noinline int add_async_extent(struct async_cow *cow,
269 u64 start, u64 ram_size,
270 u64 compressed_size,
271 struct page **pages,
272 unsigned long nr_pages)
273{
274 struct async_extent *async_extent;
275
276 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
277 async_extent->start = start;
278 async_extent->ram_size = ram_size;
279 async_extent->compressed_size = compressed_size;
280 async_extent->pages = pages;
281 async_extent->nr_pages = nr_pages;
282 list_add_tail(&async_extent->list, &cow->extents);
283 return 0;
284}
285
Chris Masonc8b97812008-10-29 14:49:59 -0400286/*
Chris Mason771ed682008-11-06 22:02:51 -0500287 * we create compressed extents in two phases. The first
288 * phase compresses a range of pages that have already been
289 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400290 *
Chris Mason771ed682008-11-06 22:02:51 -0500291 * This is done inside an ordered work queue, and the compression
292 * is spread across many cpus. The actual IO submission is step
293 * two, and the ordered work queue takes care of making sure that
294 * happens in the same order things were put onto the queue by
295 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400296 *
Chris Mason771ed682008-11-06 22:02:51 -0500297 * If this code finds it can't get good compression, it puts an
298 * entry onto the work queue to write the uncompressed bytes. This
299 * makes sure that both compressed inodes and uncompressed inodes
300 * are written in the same order that pdflush sent them down.
Chris Masond352ac62008-09-29 15:18:18 -0400301 */
Chris Mason771ed682008-11-06 22:02:51 -0500302static noinline int compress_file_range(struct inode *inode,
303 struct page *locked_page,
304 u64 start, u64 end,
305 struct async_cow *async_cow,
306 int *num_added)
Chris Masonb888db22007-08-27 16:49:44 -0400307{
308 struct btrfs_root *root = BTRFS_I(inode)->root;
309 struct btrfs_trans_handle *trans;
Chris Masondb945352007-10-15 16:15:53 -0400310 u64 num_bytes;
Chris Masonc8b97812008-10-29 14:49:59 -0400311 u64 orig_start;
312 u64 disk_num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400313 u64 blocksize = root->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400314 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500315 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400316 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400317 struct page **pages = NULL;
318 unsigned long nr_pages;
319 unsigned long nr_pages_ret = 0;
320 unsigned long total_compressed = 0;
321 unsigned long total_in = 0;
322 unsigned long max_compressed = 128 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -0500323 unsigned long max_uncompressed = 128 * 1024;
Chris Masonc8b97812008-10-29 14:49:59 -0400324 int i;
325 int will_compress;
Chris Masonb888db22007-08-27 16:49:44 -0400326
Chris Masonc8b97812008-10-29 14:49:59 -0400327 orig_start = start;
Chris Masonbe20aa92007-12-17 20:14:01 -0500328
Chris Mason42dc7ba2008-12-15 11:44:56 -0500329 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400330again:
331 will_compress = 0;
332 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
333 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
334
Chris Masonf03d9301f2009-02-04 09:31:06 -0500335 /*
336 * we don't want to send crud past the end of i_size through
337 * compression, that's just a waste of CPU time. So, if the
338 * end of the file is before the start of our current
339 * requested range of bytes, we bail out to the uncompressed
340 * cleanup code that can deal with all of this.
341 *
342 * It isn't really the fastest way to fix things, but this is a
343 * very uncommon corner.
344 */
345 if (actual_end <= start)
346 goto cleanup_and_bail_uncompressed;
347
Chris Masonc8b97812008-10-29 14:49:59 -0400348 total_compressed = actual_end - start;
349
350 /* we want to make sure that amount of ram required to uncompress
351 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500352 * of a compressed extent to 128k. This is a crucial number
353 * because it also controls how easily we can spread reads across
354 * cpus for decompression.
355 *
356 * We also want to make sure the amount of IO required to do
357 * a random read is reasonably small, so we limit the size of
358 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400359 */
360 total_compressed = min(total_compressed, max_uncompressed);
Chris Masondb945352007-10-15 16:15:53 -0400361 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
Chris Masonbe20aa92007-12-17 20:14:01 -0500362 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400363 disk_num_bytes = num_bytes;
364 total_in = 0;
365 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400366
Chris Mason771ed682008-11-06 22:02:51 -0500367 /*
368 * we do compression for mount -o compress and when the
369 * inode has not been flagged as nocompress. This flag can
370 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400371 */
372 if (!btrfs_test_flag(inode, NOCOMPRESS) &&
373 btrfs_test_opt(root, COMPRESS)) {
374 WARN_ON(pages);
Chris Masoncfbc2462008-10-30 13:22:14 -0400375 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
Chris Mason179e29e2007-11-01 11:28:41 -0400376
Chris Masonc8b97812008-10-29 14:49:59 -0400377 ret = btrfs_zlib_compress_pages(inode->i_mapping, start,
378 total_compressed, pages,
379 nr_pages, &nr_pages_ret,
380 &total_in,
381 &total_compressed,
382 max_compressed);
383
384 if (!ret) {
385 unsigned long offset = total_compressed &
386 (PAGE_CACHE_SIZE - 1);
387 struct page *page = pages[nr_pages_ret - 1];
388 char *kaddr;
389
390 /* zero the tail end of the last page, we might be
391 * sending it down to disk
392 */
393 if (offset) {
394 kaddr = kmap_atomic(page, KM_USER0);
395 memset(kaddr + offset, 0,
396 PAGE_CACHE_SIZE - offset);
397 kunmap_atomic(kaddr, KM_USER0);
398 }
399 will_compress = 1;
400 }
401 }
402 if (start == 0) {
Chris Mason771ed682008-11-06 22:02:51 -0500403 trans = btrfs_join_transaction(root, 1);
404 BUG_ON(!trans);
405 btrfs_set_trans_block_group(trans, inode);
406
Chris Masonc8b97812008-10-29 14:49:59 -0400407 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500408 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400409 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500410 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400411 */
412 ret = cow_file_range_inline(trans, root, inode,
413 start, end, 0, NULL);
414 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500415 /* try making a compressed inline extent */
Chris Masonc8b97812008-10-29 14:49:59 -0400416 ret = cow_file_range_inline(trans, root, inode,
417 start, end,
418 total_compressed, pages);
419 }
Chris Mason771ed682008-11-06 22:02:51 -0500420 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400421 if (ret == 0) {
Chris Mason771ed682008-11-06 22:02:51 -0500422 /*
423 * inline extent creation worked, we don't need
424 * to create any more async work items. Unlock
425 * and free up our temp pages.
426 */
Chris Masonc8b97812008-10-29 14:49:59 -0400427 extent_clear_unlock_delalloc(inode,
428 &BTRFS_I(inode)->io_tree,
Chris Mason771ed682008-11-06 22:02:51 -0500429 start, end, NULL, 1, 0,
430 0, 1, 1, 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400431 ret = 0;
432 goto free_pages_out;
433 }
434 }
435
436 if (will_compress) {
437 /*
438 * we aren't doing an inline extent round the compressed size
439 * up to a block size boundary so the allocator does sane
440 * things
441 */
442 total_compressed = (total_compressed + blocksize - 1) &
443 ~(blocksize - 1);
444
445 /*
446 * one last check to make sure the compression is really a
447 * win, compare the page count read with the blocks on disk
448 */
449 total_in = (total_in + PAGE_CACHE_SIZE - 1) &
450 ~(PAGE_CACHE_SIZE - 1);
451 if (total_compressed >= total_in) {
452 will_compress = 0;
453 } else {
454 disk_num_bytes = total_compressed;
455 num_bytes = total_in;
456 }
457 }
458 if (!will_compress && pages) {
459 /*
460 * the compression code ran but failed to make things smaller,
461 * free any pages it allocated and our page pointer array
462 */
463 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400464 WARN_ON(pages[i]->mapping);
Chris Masonc8b97812008-10-29 14:49:59 -0400465 page_cache_release(pages[i]);
466 }
467 kfree(pages);
468 pages = NULL;
469 total_compressed = 0;
470 nr_pages_ret = 0;
471
472 /* flag the file so we don't compress in the future */
473 btrfs_set_flag(inode, NOCOMPRESS);
474 }
Chris Mason771ed682008-11-06 22:02:51 -0500475 if (will_compress) {
476 *num_added += 1;
477
478 /* the async work queues will take care of doing actual
479 * allocation on disk for these compressed pages,
480 * and will submit them to the elevator.
481 */
482 add_async_extent(async_cow, start, num_bytes,
483 total_compressed, pages, nr_pages_ret);
484
Chris Mason42dc7ba2008-12-15 11:44:56 -0500485 if (start + num_bytes < end && start + num_bytes < actual_end) {
Chris Mason771ed682008-11-06 22:02:51 -0500486 start += num_bytes;
487 pages = NULL;
488 cond_resched();
489 goto again;
490 }
491 } else {
Chris Masonf03d9301f2009-02-04 09:31:06 -0500492cleanup_and_bail_uncompressed:
Chris Mason771ed682008-11-06 22:02:51 -0500493 /*
494 * No compression, but we still need to write the pages in
495 * the file we've been given so far. redirty the locked
496 * page if it corresponds to our extent and set things up
497 * for the async work queue to run cow_file_range to do
498 * the normal delalloc dance
499 */
500 if (page_offset(locked_page) >= start &&
501 page_offset(locked_page) <= end) {
502 __set_page_dirty_nobuffers(locked_page);
503 /* unlocked later on in the async handlers */
504 }
505 add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0);
506 *num_added += 1;
507 }
508
509out:
510 return 0;
511
512free_pages_out:
513 for (i = 0; i < nr_pages_ret; i++) {
514 WARN_ON(pages[i]->mapping);
515 page_cache_release(pages[i]);
516 }
Chris Masond3977122009-01-05 21:25:51 -0500517 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500518
519 goto out;
520}
521
522/*
523 * phase two of compressed writeback. This is the ordered portion
524 * of the code, which only gets called in the order the work was
525 * queued. We walk all the async extents created by compress_file_range
526 * and send them down to the disk.
527 */
528static noinline int submit_compressed_extents(struct inode *inode,
529 struct async_cow *async_cow)
530{
531 struct async_extent *async_extent;
532 u64 alloc_hint = 0;
533 struct btrfs_trans_handle *trans;
534 struct btrfs_key ins;
535 struct extent_map *em;
536 struct btrfs_root *root = BTRFS_I(inode)->root;
537 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
538 struct extent_io_tree *io_tree;
539 int ret;
540
541 if (list_empty(&async_cow->extents))
542 return 0;
543
544 trans = btrfs_join_transaction(root, 1);
545
Chris Masond3977122009-01-05 21:25:51 -0500546 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500547 async_extent = list_entry(async_cow->extents.next,
548 struct async_extent, list);
549 list_del(&async_extent->list);
550
551 io_tree = &BTRFS_I(inode)->io_tree;
552
553 /* did the compression code fall back to uncompressed IO? */
554 if (!async_extent->pages) {
555 int page_started = 0;
556 unsigned long nr_written = 0;
557
558 lock_extent(io_tree, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500559 async_extent->start +
560 async_extent->ram_size - 1, GFP_NOFS);
Chris Mason771ed682008-11-06 22:02:51 -0500561
562 /* allocate blocks */
563 cow_file_range(inode, async_cow->locked_page,
564 async_extent->start,
565 async_extent->start +
566 async_extent->ram_size - 1,
567 &page_started, &nr_written, 0);
568
569 /*
570 * if page_started, cow_file_range inserted an
571 * inline extent and took care of all the unlocking
572 * and IO for us. Otherwise, we need to submit
573 * all those pages down to the drive.
574 */
575 if (!page_started)
576 extent_write_locked_range(io_tree,
577 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500578 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500579 async_extent->ram_size - 1,
580 btrfs_get_extent,
581 WB_SYNC_ALL);
582 kfree(async_extent);
583 cond_resched();
584 continue;
585 }
586
587 lock_extent(io_tree, async_extent->start,
588 async_extent->start + async_extent->ram_size - 1,
589 GFP_NOFS);
590 /*
591 * here we're doing allocation and writeback of the
592 * compressed pages
593 */
594 btrfs_drop_extent_cache(inode, async_extent->start,
595 async_extent->start +
596 async_extent->ram_size - 1, 0);
597
598 ret = btrfs_reserve_extent(trans, root,
599 async_extent->compressed_size,
600 async_extent->compressed_size,
601 0, alloc_hint,
602 (u64)-1, &ins, 1);
603 BUG_ON(ret);
604 em = alloc_extent_map(GFP_NOFS);
605 em->start = async_extent->start;
606 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500607 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500608
609 em->block_start = ins.objectid;
610 em->block_len = ins.offset;
611 em->bdev = root->fs_info->fs_devices->latest_bdev;
612 set_bit(EXTENT_FLAG_PINNED, &em->flags);
613 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
614
Chris Masond3977122009-01-05 21:25:51 -0500615 while (1) {
Chris Mason771ed682008-11-06 22:02:51 -0500616 spin_lock(&em_tree->lock);
617 ret = add_extent_mapping(em_tree, em);
618 spin_unlock(&em_tree->lock);
619 if (ret != -EEXIST) {
620 free_extent_map(em);
621 break;
622 }
623 btrfs_drop_extent_cache(inode, async_extent->start,
624 async_extent->start +
625 async_extent->ram_size - 1, 0);
626 }
627
628 ret = btrfs_add_ordered_extent(inode, async_extent->start,
629 ins.objectid,
630 async_extent->ram_size,
631 ins.offset,
632 BTRFS_ORDERED_COMPRESSED);
633 BUG_ON(ret);
634
635 btrfs_end_transaction(trans, root);
636
637 /*
638 * clear dirty, set writeback and unlock the pages.
639 */
640 extent_clear_unlock_delalloc(inode,
641 &BTRFS_I(inode)->io_tree,
642 async_extent->start,
643 async_extent->start +
644 async_extent->ram_size - 1,
645 NULL, 1, 1, 0, 1, 1, 0);
646
647 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500648 async_extent->start,
649 async_extent->ram_size,
650 ins.objectid,
651 ins.offset, async_extent->pages,
652 async_extent->nr_pages);
Chris Mason771ed682008-11-06 22:02:51 -0500653
654 BUG_ON(ret);
655 trans = btrfs_join_transaction(root, 1);
656 alloc_hint = ins.objectid + ins.offset;
657 kfree(async_extent);
658 cond_resched();
659 }
660
661 btrfs_end_transaction(trans, root);
662 return 0;
663}
664
665/*
666 * when extent_io.c finds a delayed allocation range in the file,
667 * the call backs end up in this code. The basic idea is to
668 * allocate extents on disk for the range, and create ordered data structs
669 * in ram to track those extents.
670 *
671 * locked_page is the page that writepage had locked already. We use
672 * it to make sure we don't do extra locks or unlocks.
673 *
674 * *page_started is set to one if we unlock locked_page and do everything
675 * required to start IO on it. It may be clean and already done with
676 * IO when we return.
677 */
678static noinline int cow_file_range(struct inode *inode,
679 struct page *locked_page,
680 u64 start, u64 end, int *page_started,
681 unsigned long *nr_written,
682 int unlock)
683{
684 struct btrfs_root *root = BTRFS_I(inode)->root;
685 struct btrfs_trans_handle *trans;
686 u64 alloc_hint = 0;
687 u64 num_bytes;
688 unsigned long ram_size;
689 u64 disk_num_bytes;
690 u64 cur_alloc_size;
691 u64 blocksize = root->sectorsize;
692 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500693 u64 isize = i_size_read(inode);
Chris Mason771ed682008-11-06 22:02:51 -0500694 struct btrfs_key ins;
695 struct extent_map *em;
696 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
697 int ret = 0;
698
699 trans = btrfs_join_transaction(root, 1);
700 BUG_ON(!trans);
701 btrfs_set_trans_block_group(trans, inode);
702
Chris Mason42dc7ba2008-12-15 11:44:56 -0500703 actual_end = min_t(u64, isize, end + 1);
Chris Mason771ed682008-11-06 22:02:51 -0500704
705 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
706 num_bytes = max(blocksize, num_bytes);
707 disk_num_bytes = num_bytes;
708 ret = 0;
709
710 if (start == 0) {
711 /* lets try to make an inline extent */
712 ret = cow_file_range_inline(trans, root, inode,
713 start, end, 0, NULL);
714 if (ret == 0) {
715 extent_clear_unlock_delalloc(inode,
716 &BTRFS_I(inode)->io_tree,
717 start, end, NULL, 1, 1,
718 1, 1, 1, 1);
719 *nr_written = *nr_written +
720 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
721 *page_started = 1;
722 ret = 0;
723 goto out;
724 }
725 }
Chris Masonc8b97812008-10-29 14:49:59 -0400726
727 BUG_ON(disk_num_bytes >
728 btrfs_super_total_bytes(&root->fs_info->super_copy));
729
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400730 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400731
Chris Masond3977122009-01-05 21:25:51 -0500732 while (disk_num_bytes > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400733 cur_alloc_size = min(disk_num_bytes, root->fs_info->max_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400734 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500735 root->sectorsize, 0, alloc_hint,
Chris Masone6dcd2d2008-07-17 12:53:50 -0400736 (u64)-1, &ins, 1);
Chris Masond3977122009-01-05 21:25:51 -0500737 BUG_ON(ret);
738
Chris Masone6dcd2d2008-07-17 12:53:50 -0400739 em = alloc_extent_map(GFP_NOFS);
740 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500741 em->orig_start = em->start;
Chris Masonc8b97812008-10-29 14:49:59 -0400742
Chris Mason771ed682008-11-06 22:02:51 -0500743 ram_size = ins.offset;
744 em->len = ins.offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400745
Chris Masone6dcd2d2008-07-17 12:53:50 -0400746 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400747 em->block_len = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400748 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400749 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -0400750
Chris Masond3977122009-01-05 21:25:51 -0500751 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -0400752 spin_lock(&em_tree->lock);
753 ret = add_extent_mapping(em_tree, em);
754 spin_unlock(&em_tree->lock);
755 if (ret != -EEXIST) {
756 free_extent_map(em);
757 break;
758 }
759 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400760 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400761 }
762
Chris Mason98d20f62008-04-14 09:46:10 -0400763 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400764 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -0500765 ram_size, cur_alloc_size, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400766 BUG_ON(ret);
Chris Masonc8b97812008-10-29 14:49:59 -0400767
Yan Zheng17d217f2008-12-12 10:03:38 -0500768 if (root->root_key.objectid ==
769 BTRFS_DATA_RELOC_TREE_OBJECTID) {
770 ret = btrfs_reloc_clone_csums(inode, start,
771 cur_alloc_size);
772 BUG_ON(ret);
773 }
774
Chris Masond3977122009-01-05 21:25:51 -0500775 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -0400776 break;
Chris Masond3977122009-01-05 21:25:51 -0500777
Chris Masonc8b97812008-10-29 14:49:59 -0400778 /* we're not doing compressed IO, don't unlock the first
779 * page (which the caller expects to stay locked), don't
780 * clear any dirty bits and don't set any writeback bits
781 */
782 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
783 start, start + ram_size - 1,
Chris Mason771ed682008-11-06 22:02:51 -0500784 locked_page, unlock, 1,
785 1, 0, 0, 0);
Chris Masonc8b97812008-10-29 14:49:59 -0400786 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -0500787 num_bytes -= cur_alloc_size;
788 alloc_hint = ins.objectid + ins.offset;
789 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400790 }
Chris Masonb888db22007-08-27 16:49:44 -0400791out:
Chris Mason771ed682008-11-06 22:02:51 -0500792 ret = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400793 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400794
Chris Masonbe20aa92007-12-17 20:14:01 -0500795 return ret;
Chris Mason771ed682008-11-06 22:02:51 -0500796}
Chris Masonc8b97812008-10-29 14:49:59 -0400797
Chris Mason771ed682008-11-06 22:02:51 -0500798/*
799 * work queue call back to started compression on a file and pages
800 */
801static noinline void async_cow_start(struct btrfs_work *work)
802{
803 struct async_cow *async_cow;
804 int num_added = 0;
805 async_cow = container_of(work, struct async_cow, work);
806
807 compress_file_range(async_cow->inode, async_cow->locked_page,
808 async_cow->start, async_cow->end, async_cow,
809 &num_added);
810 if (num_added == 0)
811 async_cow->inode = NULL;
812}
813
814/*
815 * work queue call back to submit previously compressed pages
816 */
817static noinline void async_cow_submit(struct btrfs_work *work)
818{
819 struct async_cow *async_cow;
820 struct btrfs_root *root;
821 unsigned long nr_pages;
822
823 async_cow = container_of(work, struct async_cow, work);
824
825 root = async_cow->root;
826 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
827 PAGE_CACHE_SHIFT;
828
829 atomic_sub(nr_pages, &root->fs_info->async_delalloc_pages);
830
831 if (atomic_read(&root->fs_info->async_delalloc_pages) <
832 5 * 1042 * 1024 &&
833 waitqueue_active(&root->fs_info->async_submit_wait))
834 wake_up(&root->fs_info->async_submit_wait);
835
Chris Masond3977122009-01-05 21:25:51 -0500836 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -0500837 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -0500838}
Chris Masonc8b97812008-10-29 14:49:59 -0400839
Chris Mason771ed682008-11-06 22:02:51 -0500840static noinline void async_cow_free(struct btrfs_work *work)
841{
842 struct async_cow *async_cow;
843 async_cow = container_of(work, struct async_cow, work);
844 kfree(async_cow);
845}
846
847static int cow_file_range_async(struct inode *inode, struct page *locked_page,
848 u64 start, u64 end, int *page_started,
849 unsigned long *nr_written)
850{
851 struct async_cow *async_cow;
852 struct btrfs_root *root = BTRFS_I(inode)->root;
853 unsigned long nr_pages;
854 u64 cur_end;
855 int limit = 10 * 1024 * 1042;
856
857 if (!btrfs_test_opt(root, COMPRESS)) {
858 return cow_file_range(inode, locked_page, start, end,
859 page_started, nr_written, 1);
860 }
861
862 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED |
863 EXTENT_DELALLOC, 1, 0, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -0500864 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500865 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
866 async_cow->inode = inode;
867 async_cow->root = root;
868 async_cow->locked_page = locked_page;
869 async_cow->start = start;
870
871 if (btrfs_test_flag(inode, NOCOMPRESS))
872 cur_end = end;
873 else
874 cur_end = min(end, start + 512 * 1024 - 1);
875
876 async_cow->end = cur_end;
877 INIT_LIST_HEAD(&async_cow->extents);
878
879 async_cow->work.func = async_cow_start;
880 async_cow->work.ordered_func = async_cow_submit;
881 async_cow->work.ordered_free = async_cow_free;
882 async_cow->work.flags = 0;
883
Chris Mason771ed682008-11-06 22:02:51 -0500884 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
885 PAGE_CACHE_SHIFT;
886 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
887
888 btrfs_queue_worker(&root->fs_info->delalloc_workers,
889 &async_cow->work);
890
891 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
892 wait_event(root->fs_info->async_submit_wait,
893 (atomic_read(&root->fs_info->async_delalloc_pages) <
894 limit));
895 }
896
Chris Masond3977122009-01-05 21:25:51 -0500897 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -0500898 atomic_read(&root->fs_info->async_delalloc_pages)) {
899 wait_event(root->fs_info->async_submit_wait,
900 (atomic_read(&root->fs_info->async_delalloc_pages) ==
901 0));
902 }
903
904 *nr_written += nr_pages;
905 start = cur_end + 1;
906 }
907 *page_started = 1;
908 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -0500909}
910
Chris Masond3977122009-01-05 21:25:51 -0500911static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -0500912 u64 bytenr, u64 num_bytes)
913{
914 int ret;
915 struct btrfs_ordered_sum *sums;
916 LIST_HEAD(list);
917
Yan Zheng07d400a2009-01-06 11:42:00 -0500918 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
919 bytenr + num_bytes - 1, &list);
Yan Zheng17d217f2008-12-12 10:03:38 -0500920 if (ret == 0 && list_empty(&list))
921 return 0;
922
923 while (!list_empty(&list)) {
924 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
925 list_del(&sums->list);
926 kfree(sums);
927 }
928 return 1;
929}
930
Chris Masond352ac62008-09-29 15:18:18 -0400931/*
932 * when nowcow writeback call back. This checks for snapshots or COW copies
933 * of the extents that exist in the file, and COWs the file as required.
934 *
935 * If no cow copies or snapshots exist, we write directly to the existing
936 * blocks on disk
937 */
Chris Masonc8b97812008-10-29 14:49:59 -0400938static int run_delalloc_nocow(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -0500939 u64 start, u64 end, int *page_started, int force,
940 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -0500941{
Chris Masonbe20aa92007-12-17 20:14:01 -0500942 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -0400943 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -0500944 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -0500945 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -0400946 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -0500947 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -0400948 u64 cow_start;
949 u64 cur_offset;
950 u64 extent_end;
951 u64 disk_bytenr;
952 u64 num_bytes;
953 int extent_type;
954 int ret;
Yan Zhengd899e052008-10-30 14:25:28 -0400955 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -0400956 int nocow;
957 int check_prev = 1;
Chris Masonbe20aa92007-12-17 20:14:01 -0500958
959 path = btrfs_alloc_path();
960 BUG_ON(!path);
Yan Zheng7ea394f2008-08-05 13:05:02 -0400961 trans = btrfs_join_transaction(root, 1);
962 BUG_ON(!trans);
Chris Masonbe20aa92007-12-17 20:14:01 -0500963
Yan Zheng80ff3852008-10-30 14:20:02 -0400964 cow_start = (u64)-1;
965 cur_offset = start;
966 while (1) {
967 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
968 cur_offset, 0);
969 BUG_ON(ret < 0);
970 if (ret > 0 && path->slots[0] > 0 && check_prev) {
971 leaf = path->nodes[0];
972 btrfs_item_key_to_cpu(leaf, &found_key,
973 path->slots[0] - 1);
974 if (found_key.objectid == inode->i_ino &&
975 found_key.type == BTRFS_EXTENT_DATA_KEY)
976 path->slots[0]--;
977 }
978 check_prev = 0;
979next_slot:
980 leaf = path->nodes[0];
981 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
982 ret = btrfs_next_leaf(root, path);
983 if (ret < 0)
984 BUG_ON(1);
985 if (ret > 0)
986 break;
987 leaf = path->nodes[0];
988 }
Chris Masonbe20aa92007-12-17 20:14:01 -0500989
Yan Zheng80ff3852008-10-30 14:20:02 -0400990 nocow = 0;
991 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -0500992 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -0400993 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -0500994
Yan Zheng80ff3852008-10-30 14:20:02 -0400995 if (found_key.objectid > inode->i_ino ||
996 found_key.type > BTRFS_EXTENT_DATA_KEY ||
997 found_key.offset > end)
998 break;
Chris Masonbe20aa92007-12-17 20:14:01 -0500999
Yan Zheng80ff3852008-10-30 14:20:02 -04001000 if (found_key.offset > cur_offset) {
1001 extent_end = found_key.offset;
1002 goto out_check;
1003 }
Chris Masonc31f8832008-01-08 15:46:31 -05001004
Yan Zheng80ff3852008-10-30 14:20:02 -04001005 fi = btrfs_item_ptr(leaf, path->slots[0],
1006 struct btrfs_file_extent_item);
1007 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001008
Yan Zhengd899e052008-10-30 14:25:28 -04001009 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1010 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001011 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1012 extent_end = found_key.offset +
1013 btrfs_file_extent_num_bytes(leaf, fi);
1014 if (extent_end <= start) {
1015 path->slots[0]++;
1016 goto next_slot;
1017 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001018 if (disk_bytenr == 0)
1019 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001020 if (btrfs_file_extent_compression(leaf, fi) ||
1021 btrfs_file_extent_encryption(leaf, fi) ||
1022 btrfs_file_extent_other_encoding(leaf, fi))
1023 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001024 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1025 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001026 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001027 goto out_check;
Yan Zheng17d217f2008-12-12 10:03:38 -05001028 if (btrfs_cross_ref_exist(trans, root, inode->i_ino,
1029 disk_bytenr))
1030 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001031 disk_bytenr += btrfs_file_extent_offset(leaf, fi);
Yan Zheng17d217f2008-12-12 10:03:38 -05001032 disk_bytenr += cur_offset - found_key.offset;
1033 num_bytes = min(end + 1, extent_end) - cur_offset;
1034 /*
1035 * force cow if csum exists in the range.
1036 * this ensure that csum for a given extent are
1037 * either valid or do not exist.
1038 */
1039 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1040 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001041 nocow = 1;
1042 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1043 extent_end = found_key.offset +
1044 btrfs_file_extent_inline_len(leaf, fi);
1045 extent_end = ALIGN(extent_end, root->sectorsize);
1046 } else {
1047 BUG_ON(1);
1048 }
1049out_check:
1050 if (extent_end <= start) {
1051 path->slots[0]++;
1052 goto next_slot;
1053 }
1054 if (!nocow) {
1055 if (cow_start == (u64)-1)
1056 cow_start = cur_offset;
1057 cur_offset = extent_end;
1058 if (cur_offset > end)
1059 break;
1060 path->slots[0]++;
1061 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001062 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001063
Yan Zheng7ea394f2008-08-05 13:05:02 -04001064 btrfs_release_path(root, path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001065 if (cow_start != (u64)-1) {
1066 ret = cow_file_range(inode, locked_page, cow_start,
Chris Mason771ed682008-11-06 22:02:51 -05001067 found_key.offset - 1, page_started,
1068 nr_written, 1);
Yan Zheng80ff3852008-10-30 14:20:02 -04001069 BUG_ON(ret);
1070 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001071 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001072
Yan Zhengd899e052008-10-30 14:25:28 -04001073 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1074 struct extent_map *em;
1075 struct extent_map_tree *em_tree;
1076 em_tree = &BTRFS_I(inode)->extent_tree;
1077 em = alloc_extent_map(GFP_NOFS);
1078 em->start = cur_offset;
Chris Mason445a6942008-11-10 11:53:33 -05001079 em->orig_start = em->start;
Yan Zhengd899e052008-10-30 14:25:28 -04001080 em->len = num_bytes;
1081 em->block_len = num_bytes;
1082 em->block_start = disk_bytenr;
1083 em->bdev = root->fs_info->fs_devices->latest_bdev;
1084 set_bit(EXTENT_FLAG_PINNED, &em->flags);
1085 while (1) {
1086 spin_lock(&em_tree->lock);
1087 ret = add_extent_mapping(em_tree, em);
1088 spin_unlock(&em_tree->lock);
1089 if (ret != -EEXIST) {
1090 free_extent_map(em);
1091 break;
1092 }
1093 btrfs_drop_extent_cache(inode, em->start,
1094 em->start + em->len - 1, 0);
1095 }
1096 type = BTRFS_ORDERED_PREALLOC;
1097 } else {
1098 type = BTRFS_ORDERED_NOCOW;
1099 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001100
1101 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001102 num_bytes, num_bytes, type);
1103 BUG_ON(ret);
Chris Mason771ed682008-11-06 22:02:51 -05001104
Yan Zhengd899e052008-10-30 14:25:28 -04001105 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
1106 cur_offset, cur_offset + num_bytes - 1,
Chris Mason771ed682008-11-06 22:02:51 -05001107 locked_page, 1, 1, 1, 0, 0, 0);
Yan Zheng80ff3852008-10-30 14:20:02 -04001108 cur_offset = extent_end;
1109 if (cur_offset > end)
1110 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001111 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001112 btrfs_release_path(root, path);
1113
1114 if (cur_offset <= end && cow_start == (u64)-1)
1115 cow_start = cur_offset;
1116 if (cow_start != (u64)-1) {
1117 ret = cow_file_range(inode, locked_page, cow_start, end,
Chris Mason771ed682008-11-06 22:02:51 -05001118 page_started, nr_written, 1);
Yan Zheng80ff3852008-10-30 14:20:02 -04001119 BUG_ON(ret);
1120 }
1121
1122 ret = btrfs_end_transaction(trans, root);
1123 BUG_ON(ret);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001124 btrfs_free_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001125 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001126}
1127
Chris Masond352ac62008-09-29 15:18:18 -04001128/*
1129 * extent_io.c call back to do delayed allocation processing
1130 */
Chris Masonc8b97812008-10-29 14:49:59 -04001131static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001132 u64 start, u64 end, int *page_started,
1133 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001134{
Chris Masonbe20aa92007-12-17 20:14:01 -05001135 int ret;
Chris Masona2135012008-06-25 16:01:30 -04001136
Yan Zheng17d217f2008-12-12 10:03:38 -05001137 if (btrfs_test_flag(inode, NODATACOW))
Chris Masonc8b97812008-10-29 14:49:59 -04001138 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001139 page_started, 1, nr_written);
Yan Zhengd899e052008-10-30 14:25:28 -04001140 else if (btrfs_test_flag(inode, PREALLOC))
1141 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001142 page_started, 0, nr_written);
Chris Masonbe20aa92007-12-17 20:14:01 -05001143 else
Chris Mason771ed682008-11-06 22:02:51 -05001144 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001145 page_started, nr_written);
Chris Mason1832a6d2007-12-21 16:27:21 -05001146
Chris Masonb888db22007-08-27 16:49:44 -04001147 return ret;
1148}
1149
Chris Masond352ac62008-09-29 15:18:18 -04001150/*
1151 * extent_io.c set_bit_hook, used to track delayed allocation
1152 * bytes in this file, and to maintain the list of inodes that
1153 * have pending delalloc work to be done.
1154 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001155static int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -05001156 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -05001157{
Chris Mason75eff682008-12-15 15:54:40 -05001158 /*
1159 * set_bit and clear bit hooks normally require _irqsave/restore
1160 * but in this case, we are only testeing for the DELALLOC
1161 * bit, which is only set or cleared with irqs on
1162 */
Chris Masonb0c68f82008-01-31 11:05:37 -05001163 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001164 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik6a632092009-02-20 11:00:09 -05001165 btrfs_delalloc_reserve_space(root, inode, end - start + 1);
Chris Mason75eff682008-12-15 15:54:40 -05001166 spin_lock(&root->fs_info->delalloc_lock);
Chris Mason90692182008-02-08 13:49:28 -05001167 BTRFS_I(inode)->delalloc_bytes += end - start + 1;
Chris Mason291d6732008-01-29 15:55:23 -05001168 root->fs_info->delalloc_bytes += end - start + 1;
Chris Masonea8c2812008-08-04 23:17:27 -04001169 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1170 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1171 &root->fs_info->delalloc_inodes);
1172 }
Chris Mason75eff682008-12-15 15:54:40 -05001173 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001174 }
1175 return 0;
1176}
1177
Chris Masond352ac62008-09-29 15:18:18 -04001178/*
1179 * extent_io.c clear_bit_hook, see set_bit_hook for why
1180 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001181static int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -05001182 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -05001183{
Chris Mason75eff682008-12-15 15:54:40 -05001184 /*
1185 * set_bit and clear bit hooks normally require _irqsave/restore
1186 * but in this case, we are only testeing for the DELALLOC
1187 * bit, which is only set or cleared with irqs on
1188 */
Chris Masonb0c68f82008-01-31 11:05:37 -05001189 if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001190 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonbcbfce82008-04-22 13:26:47 -04001191
Chris Mason75eff682008-12-15 15:54:40 -05001192 spin_lock(&root->fs_info->delalloc_lock);
Chris Masonb0c68f82008-01-31 11:05:37 -05001193 if (end - start + 1 > root->fs_info->delalloc_bytes) {
Chris Masond3977122009-01-05 21:25:51 -05001194 printk(KERN_INFO "btrfs warning: delalloc account "
1195 "%llu %llu\n",
1196 (unsigned long long)end - start + 1,
1197 (unsigned long long)
1198 root->fs_info->delalloc_bytes);
Josef Bacik6a632092009-02-20 11:00:09 -05001199 btrfs_delalloc_free_space(root, inode, (u64)-1);
Chris Masonb0c68f82008-01-31 11:05:37 -05001200 root->fs_info->delalloc_bytes = 0;
Chris Mason90692182008-02-08 13:49:28 -05001201 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masonb0c68f82008-01-31 11:05:37 -05001202 } else {
Josef Bacik6a632092009-02-20 11:00:09 -05001203 btrfs_delalloc_free_space(root, inode,
1204 end - start + 1);
Chris Masonb0c68f82008-01-31 11:05:37 -05001205 root->fs_info->delalloc_bytes -= end - start + 1;
Chris Mason90692182008-02-08 13:49:28 -05001206 BTRFS_I(inode)->delalloc_bytes -= end - start + 1;
Chris Masonb0c68f82008-01-31 11:05:37 -05001207 }
Chris Masonea8c2812008-08-04 23:17:27 -04001208 if (BTRFS_I(inode)->delalloc_bytes == 0 &&
1209 !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1210 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1211 }
Chris Mason75eff682008-12-15 15:54:40 -05001212 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001213 }
1214 return 0;
1215}
1216
Chris Masond352ac62008-09-29 15:18:18 -04001217/*
1218 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1219 * we don't create bios that span stripes or chunks
1220 */
Chris Mason239b14b2008-03-24 15:02:07 -04001221int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001222 size_t size, struct bio *bio,
1223 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001224{
1225 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
1226 struct btrfs_mapping_tree *map_tree;
Chris Masona62b9402008-10-03 16:31:08 -04001227 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001228 u64 length = 0;
1229 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001230 int ret;
1231
Chris Mason771ed682008-11-06 22:02:51 -05001232 if (bio_flags & EXTENT_BIO_COMPRESSED)
1233 return 0;
1234
Chris Masonf2d8d742008-04-21 10:03:05 -04001235 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001236 map_tree = &root->fs_info->mapping_tree;
1237 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04001238 ret = btrfs_map_block(map_tree, READ, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001239 &map_length, NULL, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04001240
Chris Masond3977122009-01-05 21:25:51 -05001241 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001242 return 1;
Chris Mason239b14b2008-03-24 15:02:07 -04001243 return 0;
1244}
1245
Chris Masond352ac62008-09-29 15:18:18 -04001246/*
1247 * in order to insert checksums into the metadata in large chunks,
1248 * we wait until bio submission time. All the pages in the bio are
1249 * checksummed and sums are attached onto the ordered extent record.
1250 *
1251 * At IO completion time the cums attached on the ordered extent record
1252 * are inserted into the btree
1253 */
Chris Masond3977122009-01-05 21:25:51 -05001254static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1255 struct bio *bio, int mirror_num,
1256 unsigned long bio_flags)
Chris Mason065631f2008-02-20 12:07:25 -05001257{
Chris Mason065631f2008-02-20 12:07:25 -05001258 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001259 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001260
Chris Masond20f7042008-12-08 16:58:54 -05001261 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Chris Mason44b8bd72008-04-16 11:14:51 -04001262 BUG_ON(ret);
Chris Mason4a69a412008-11-06 22:03:00 -05001263 return 0;
1264}
Chris Masone0156402008-04-16 11:15:20 -04001265
Chris Mason4a69a412008-11-06 22:03:00 -05001266/*
1267 * in order to insert checksums into the metadata in large chunks,
1268 * we wait until bio submission time. All the pages in the bio are
1269 * checksummed and sums are attached onto the ordered extent record.
1270 *
1271 * At IO completion time the cums attached on the ordered extent record
1272 * are inserted into the btree
1273 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001274static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Mason4a69a412008-11-06 22:03:00 -05001275 int mirror_num, unsigned long bio_flags)
1276{
1277 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason8b712842008-06-11 16:50:36 -04001278 return btrfs_map_bio(root, rw, bio, mirror_num, 1);
Chris Mason44b8bd72008-04-16 11:14:51 -04001279}
1280
Chris Masond352ac62008-09-29 15:18:18 -04001281/*
Chris Masoncad321a2008-12-17 14:51:42 -05001282 * extent_io.c submission hook. This does the right thing for csum calculation
1283 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001284 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001285static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masonc8b97812008-10-29 14:49:59 -04001286 int mirror_num, unsigned long bio_flags)
Chris Mason44b8bd72008-04-16 11:14:51 -04001287{
1288 struct btrfs_root *root = BTRFS_I(inode)->root;
1289 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001290 int skip_sum;
Chris Mason44b8bd72008-04-16 11:14:51 -04001291
Chris Masoncad321a2008-12-17 14:51:42 -05001292 skip_sum = btrfs_test_flag(inode, NODATASUM);
1293
Chris Masone6dcd2d2008-07-17 12:53:50 -04001294 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
1295 BUG_ON(ret);
Chris Mason065631f2008-02-20 12:07:25 -05001296
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001297 if (!(rw & (1 << BIO_RW))) {
Chris Masond20f7042008-12-08 16:58:54 -05001298 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Chris Masonc8b97812008-10-29 14:49:59 -04001299 return btrfs_submit_compressed_read(inode, bio,
1300 mirror_num, bio_flags);
Chris Masond20f7042008-12-08 16:58:54 -05001301 } else if (!skip_sum)
1302 btrfs_lookup_bio_sums(root, inode, bio, NULL);
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001303 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001304 } else if (!skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001305 /* csum items have already been cloned */
1306 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1307 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001308 /* we're doing a write, do the async checksumming */
1309 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001310 inode, rw, bio, mirror_num,
Chris Mason4a69a412008-11-06 22:03:00 -05001311 bio_flags, __btrfs_submit_bio_start,
1312 __btrfs_submit_bio_done);
Chris Mason19b9bdb2008-10-30 14:23:13 -04001313 }
1314
Chris Mason0b86a832008-03-24 15:01:56 -04001315mapit:
Chris Mason8b712842008-06-11 16:50:36 -04001316 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
Chris Mason065631f2008-02-20 12:07:25 -05001317}
Chris Mason6885f302008-02-20 16:11:05 -05001318
Chris Masond352ac62008-09-29 15:18:18 -04001319/*
1320 * given a list of ordered sums record them in the inode. This happens
1321 * at IO completion time based on sums calculated at bio submission time.
1322 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001323static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001324 struct inode *inode, u64 file_offset,
1325 struct list_head *list)
1326{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001327 struct btrfs_ordered_sum *sum;
1328
1329 btrfs_set_trans_block_group(trans, inode);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001330
1331 list_for_each_entry(sum, list, list) {
Chris Masond20f7042008-12-08 16:58:54 -05001332 btrfs_csum_file_blocks(trans,
1333 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001334 }
1335 return 0;
1336}
1337
Chris Masonea8c2812008-08-04 23:17:27 -04001338int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end)
1339{
Chris Masond3977122009-01-05 21:25:51 -05001340 if ((end & (PAGE_CACHE_SIZE - 1)) == 0)
Chris Mason771ed682008-11-06 22:02:51 -05001341 WARN_ON(1);
Chris Masonea8c2812008-08-04 23:17:27 -04001342 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
1343 GFP_NOFS);
1344}
1345
Chris Masond352ac62008-09-29 15:18:18 -04001346/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001347struct btrfs_writepage_fixup {
1348 struct page *page;
1349 struct btrfs_work work;
1350};
1351
Christoph Hellwigb2950862008-12-02 09:54:17 -05001352static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001353{
1354 struct btrfs_writepage_fixup *fixup;
1355 struct btrfs_ordered_extent *ordered;
1356 struct page *page;
1357 struct inode *inode;
1358 u64 page_start;
1359 u64 page_end;
1360
1361 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1362 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001363again:
Chris Mason247e7432008-07-17 12:53:51 -04001364 lock_page(page);
1365 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1366 ClearPageChecked(page);
1367 goto out_page;
1368 }
1369
1370 inode = page->mapping->host;
1371 page_start = page_offset(page);
1372 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1373
1374 lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001375
1376 /* already ordered? We're done */
1377 if (test_range_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
1378 EXTENT_ORDERED, 0)) {
Chris Mason247e7432008-07-17 12:53:51 -04001379 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001380 }
1381
1382 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1383 if (ordered) {
1384 unlock_extent(&BTRFS_I(inode)->io_tree, page_start,
1385 page_end, GFP_NOFS);
1386 unlock_page(page);
1387 btrfs_start_ordered_extent(inode, ordered, 1);
1388 goto again;
1389 }
Chris Mason247e7432008-07-17 12:53:51 -04001390
Chris Masonea8c2812008-08-04 23:17:27 -04001391 btrfs_set_extent_delalloc(inode, page_start, page_end);
Chris Mason247e7432008-07-17 12:53:51 -04001392 ClearPageChecked(page);
1393out:
1394 unlock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
1395out_page:
1396 unlock_page(page);
1397 page_cache_release(page);
1398}
1399
1400/*
1401 * There are a few paths in the higher layers of the kernel that directly
1402 * set the page dirty bit without asking the filesystem if it is a
1403 * good idea. This causes problems because we want to make sure COW
1404 * properly happens and the data=ordered rules are followed.
1405 *
Chris Masonc8b97812008-10-29 14:49:59 -04001406 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001407 * hasn't been properly setup for IO. We kick off an async process
1408 * to fix it up. The async helper will wait for ordered extents, set
1409 * the delalloc bit and make it safe to write the page.
1410 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001411static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001412{
1413 struct inode *inode = page->mapping->host;
1414 struct btrfs_writepage_fixup *fixup;
1415 struct btrfs_root *root = BTRFS_I(inode)->root;
1416 int ret;
1417
1418 ret = test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1419 EXTENT_ORDERED, 0);
1420 if (ret)
1421 return 0;
1422
1423 if (PageChecked(page))
1424 return -EAGAIN;
1425
1426 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1427 if (!fixup)
1428 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001429
Chris Mason247e7432008-07-17 12:53:51 -04001430 SetPageChecked(page);
1431 page_cache_get(page);
1432 fixup->work.func = btrfs_writepage_fixup_worker;
1433 fixup->page = page;
1434 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
1435 return -EAGAIN;
1436}
1437
Yan Zhengd899e052008-10-30 14:25:28 -04001438static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1439 struct inode *inode, u64 file_pos,
1440 u64 disk_bytenr, u64 disk_num_bytes,
1441 u64 num_bytes, u64 ram_bytes,
1442 u8 compression, u8 encryption,
1443 u16 other_encoding, int extent_type)
1444{
1445 struct btrfs_root *root = BTRFS_I(inode)->root;
1446 struct btrfs_file_extent_item *fi;
1447 struct btrfs_path *path;
1448 struct extent_buffer *leaf;
1449 struct btrfs_key ins;
1450 u64 hint;
1451 int ret;
1452
1453 path = btrfs_alloc_path();
1454 BUG_ON(!path);
1455
1456 ret = btrfs_drop_extents(trans, root, inode, file_pos,
1457 file_pos + num_bytes, file_pos, &hint);
1458 BUG_ON(ret);
1459
1460 ins.objectid = inode->i_ino;
1461 ins.offset = file_pos;
1462 ins.type = BTRFS_EXTENT_DATA_KEY;
1463 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
1464 BUG_ON(ret);
1465 leaf = path->nodes[0];
1466 fi = btrfs_item_ptr(leaf, path->slots[0],
1467 struct btrfs_file_extent_item);
1468 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1469 btrfs_set_file_extent_type(leaf, fi, extent_type);
1470 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1471 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1472 btrfs_set_file_extent_offset(leaf, fi, 0);
1473 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1474 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1475 btrfs_set_file_extent_compression(leaf, fi, compression);
1476 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1477 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
1478 btrfs_mark_buffer_dirty(leaf);
1479
1480 inode_add_bytes(inode, num_bytes);
1481 btrfs_drop_extent_cache(inode, file_pos, file_pos + num_bytes - 1, 0);
1482
1483 ins.objectid = disk_bytenr;
1484 ins.offset = disk_num_bytes;
1485 ins.type = BTRFS_EXTENT_ITEM_KEY;
1486 ret = btrfs_alloc_reserved_extent(trans, root, leaf->start,
1487 root->root_key.objectid,
1488 trans->transid, inode->i_ino, &ins);
1489 BUG_ON(ret);
1490
1491 btrfs_free_path(path);
1492 return 0;
1493}
1494
Chris Masond352ac62008-09-29 15:18:18 -04001495/* as ordered data IO finishes, this gets called so we can finish
1496 * an ordered extent if the range of bytes in the file it covers are
1497 * fully written.
1498 */
Chris Mason211f90e2008-07-18 11:56:15 -04001499static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001500{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001501 struct btrfs_root *root = BTRFS_I(inode)->root;
1502 struct btrfs_trans_handle *trans;
1503 struct btrfs_ordered_extent *ordered_extent;
1504 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan Zhengd899e052008-10-30 14:25:28 -04001505 int compressed = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001506 int ret;
1507
1508 ret = btrfs_dec_test_ordered_pending(inode, start, end - start + 1);
Chris Masonba1da2f2008-07-17 12:54:15 -04001509 if (!ret)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001510 return 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001511
Chris Masonf9295742008-07-17 12:54:14 -04001512 trans = btrfs_join_transaction(root, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001513
1514 ordered_extent = btrfs_lookup_ordered_extent(inode, start);
1515 BUG_ON(!ordered_extent);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001516 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags))
1517 goto nocow;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001518
1519 lock_extent(io_tree, ordered_extent->file_offset,
1520 ordered_extent->file_offset + ordered_extent->len - 1,
1521 GFP_NOFS);
1522
Chris Masonc8b97812008-10-29 14:49:59 -04001523 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Yan Zhengd899e052008-10-30 14:25:28 -04001524 compressed = 1;
1525 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
1526 BUG_ON(compressed);
1527 ret = btrfs_mark_extent_written(trans, root, inode,
1528 ordered_extent->file_offset,
1529 ordered_extent->file_offset +
1530 ordered_extent->len);
1531 BUG_ON(ret);
1532 } else {
1533 ret = insert_reserved_file_extent(trans, inode,
1534 ordered_extent->file_offset,
1535 ordered_extent->start,
1536 ordered_extent->disk_len,
1537 ordered_extent->len,
1538 ordered_extent->len,
1539 compressed, 0, 0,
1540 BTRFS_FILE_EXTENT_REG);
1541 BUG_ON(ret);
1542 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04001543 unlock_extent(io_tree, ordered_extent->file_offset,
1544 ordered_extent->file_offset + ordered_extent->len - 1,
1545 GFP_NOFS);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001546nocow:
Chris Masone6dcd2d2008-07-17 12:53:50 -04001547 add_pending_csums(trans, inode, ordered_extent->file_offset,
1548 &ordered_extent->list);
1549
Chris Mason34353022008-09-23 20:19:49 -04001550 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Chris Masondbe674a2008-07-17 12:54:05 -04001551 btrfs_ordered_update_i_size(inode, ordered_extent);
Chris Masone02119d2008-09-05 16:13:11 -04001552 btrfs_update_inode(trans, root, inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001553 btrfs_remove_ordered_extent(inode, ordered_extent);
Chris Mason34353022008-09-23 20:19:49 -04001554 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
Chris Mason7f3c74f2008-07-18 12:01:11 -04001555
Chris Masone6dcd2d2008-07-17 12:53:50 -04001556 /* once for us */
1557 btrfs_put_ordered_extent(ordered_extent);
1558 /* once for the tree */
1559 btrfs_put_ordered_extent(ordered_extent);
1560
Chris Masone6dcd2d2008-07-17 12:53:50 -04001561 btrfs_end_transaction(trans, root);
1562 return 0;
1563}
1564
Christoph Hellwigb2950862008-12-02 09:54:17 -05001565static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04001566 struct extent_state *state, int uptodate)
1567{
1568 return btrfs_finish_ordered_io(page->mapping->host, start, end);
1569}
1570
Chris Masond352ac62008-09-29 15:18:18 -04001571/*
1572 * When IO fails, either with EIO or csum verification fails, we
1573 * try other mirrors that might have a good copy of the data. This
1574 * io_failure_record is used to record state as we go through all the
1575 * mirrors. If another mirror has good data, the page is set up to date
1576 * and things continue. If a good mirror can't be found, the original
1577 * bio end_io callback is called to indicate things have failed.
1578 */
Chris Mason7e383262008-04-09 16:28:12 -04001579struct io_failure_record {
1580 struct page *page;
1581 u64 start;
1582 u64 len;
1583 u64 logical;
Chris Masond20f7042008-12-08 16:58:54 -05001584 unsigned long bio_flags;
Chris Mason7e383262008-04-09 16:28:12 -04001585 int last_mirror;
1586};
1587
Christoph Hellwigb2950862008-12-02 09:54:17 -05001588static int btrfs_io_failed_hook(struct bio *failed_bio,
Chris Mason1259ab72008-05-12 13:39:03 -04001589 struct page *page, u64 start, u64 end,
1590 struct extent_state *state)
Chris Mason7e383262008-04-09 16:28:12 -04001591{
1592 struct io_failure_record *failrec = NULL;
1593 u64 private;
1594 struct extent_map *em;
1595 struct inode *inode = page->mapping->host;
1596 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Chris Mason3b951512008-04-17 11:29:12 -04001597 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason7e383262008-04-09 16:28:12 -04001598 struct bio *bio;
1599 int num_copies;
1600 int ret;
Chris Mason1259ab72008-05-12 13:39:03 -04001601 int rw;
Chris Mason7e383262008-04-09 16:28:12 -04001602 u64 logical;
1603
1604 ret = get_state_private(failure_tree, start, &private);
1605 if (ret) {
Chris Mason7e383262008-04-09 16:28:12 -04001606 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
1607 if (!failrec)
1608 return -ENOMEM;
1609 failrec->start = start;
1610 failrec->len = end - start + 1;
1611 failrec->last_mirror = 0;
Chris Masond20f7042008-12-08 16:58:54 -05001612 failrec->bio_flags = 0;
Chris Mason7e383262008-04-09 16:28:12 -04001613
Chris Mason3b951512008-04-17 11:29:12 -04001614 spin_lock(&em_tree->lock);
1615 em = lookup_extent_mapping(em_tree, start, failrec->len);
1616 if (em->start > start || em->start + em->len < start) {
1617 free_extent_map(em);
1618 em = NULL;
1619 }
1620 spin_unlock(&em_tree->lock);
Chris Mason7e383262008-04-09 16:28:12 -04001621
1622 if (!em || IS_ERR(em)) {
1623 kfree(failrec);
1624 return -EIO;
1625 }
1626 logical = start - em->start;
1627 logical = em->block_start + logical;
Chris Masond20f7042008-12-08 16:58:54 -05001628 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
1629 logical = em->block_start;
1630 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
1631 }
Chris Mason7e383262008-04-09 16:28:12 -04001632 failrec->logical = logical;
1633 free_extent_map(em);
1634 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
1635 EXTENT_DIRTY, GFP_NOFS);
Chris Mason587f7702008-04-11 12:16:46 -04001636 set_state_private(failure_tree, start,
1637 (u64)(unsigned long)failrec);
Chris Mason7e383262008-04-09 16:28:12 -04001638 } else {
Chris Mason587f7702008-04-11 12:16:46 -04001639 failrec = (struct io_failure_record *)(unsigned long)private;
Chris Mason7e383262008-04-09 16:28:12 -04001640 }
1641 num_copies = btrfs_num_copies(
1642 &BTRFS_I(inode)->root->fs_info->mapping_tree,
1643 failrec->logical, failrec->len);
1644 failrec->last_mirror++;
1645 if (!state) {
Chris Masoncad321a2008-12-17 14:51:42 -05001646 spin_lock(&BTRFS_I(inode)->io_tree.lock);
Chris Mason7e383262008-04-09 16:28:12 -04001647 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
1648 failrec->start,
1649 EXTENT_LOCKED);
1650 if (state && state->start != failrec->start)
1651 state = NULL;
Chris Masoncad321a2008-12-17 14:51:42 -05001652 spin_unlock(&BTRFS_I(inode)->io_tree.lock);
Chris Mason7e383262008-04-09 16:28:12 -04001653 }
1654 if (!state || failrec->last_mirror > num_copies) {
1655 set_state_private(failure_tree, failrec->start, 0);
1656 clear_extent_bits(failure_tree, failrec->start,
1657 failrec->start + failrec->len - 1,
1658 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
1659 kfree(failrec);
1660 return -EIO;
1661 }
1662 bio = bio_alloc(GFP_NOFS, 1);
1663 bio->bi_private = state;
1664 bio->bi_end_io = failed_bio->bi_end_io;
1665 bio->bi_sector = failrec->logical >> 9;
1666 bio->bi_bdev = failed_bio->bi_bdev;
Chris Masone1c4b742008-04-22 13:26:46 -04001667 bio->bi_size = 0;
Chris Masond20f7042008-12-08 16:58:54 -05001668
Chris Mason7e383262008-04-09 16:28:12 -04001669 bio_add_page(bio, page, failrec->len, start - page_offset(page));
Chris Mason1259ab72008-05-12 13:39:03 -04001670 if (failed_bio->bi_rw & (1 << BIO_RW))
1671 rw = WRITE;
1672 else
1673 rw = READ;
1674
1675 BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
Chris Masonc8b97812008-10-29 14:49:59 -04001676 failrec->last_mirror,
Chris Masond20f7042008-12-08 16:58:54 -05001677 failrec->bio_flags);
Chris Mason1259ab72008-05-12 13:39:03 -04001678 return 0;
1679}
1680
Chris Masond352ac62008-09-29 15:18:18 -04001681/*
1682 * each time an IO finishes, we do a fast check in the IO failure tree
1683 * to see if we need to process or clean up an io_failure_record
1684 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001685static int btrfs_clean_io_failures(struct inode *inode, u64 start)
Chris Mason1259ab72008-05-12 13:39:03 -04001686{
1687 u64 private;
1688 u64 private_failure;
1689 struct io_failure_record *failure;
1690 int ret;
1691
1692 private = 0;
1693 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
1694 (u64)-1, 1, EXTENT_DIRTY)) {
1695 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
1696 start, &private_failure);
1697 if (ret == 0) {
1698 failure = (struct io_failure_record *)(unsigned long)
1699 private_failure;
1700 set_state_private(&BTRFS_I(inode)->io_failure_tree,
1701 failure->start, 0);
1702 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
1703 failure->start,
1704 failure->start + failure->len - 1,
1705 EXTENT_DIRTY | EXTENT_LOCKED,
1706 GFP_NOFS);
1707 kfree(failure);
1708 }
1709 }
Chris Mason7e383262008-04-09 16:28:12 -04001710 return 0;
1711}
1712
Chris Masond352ac62008-09-29 15:18:18 -04001713/*
1714 * when reads are done, we need to check csums to verify the data is correct
1715 * if there's a match, we allow the bio to finish. If not, we go through
1716 * the io_failure_record routines to find good copies
1717 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001718static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason70dec802008-01-29 09:59:12 -05001719 struct extent_state *state)
Chris Mason07157aa2007-08-30 08:50:51 -04001720{
Chris Mason35ebb932007-10-30 16:56:53 -04001721 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -04001722 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05001723 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04001724 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -05001725 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -04001726 int ret;
Chris Masonff79f812007-10-15 16:22:25 -04001727 struct btrfs_root *root = BTRFS_I(inode)->root;
1728 u32 csum = ~(u32)0;
Chris Masond1310b22008-01-24 16:13:08 -05001729
Chris Masond20f7042008-12-08 16:58:54 -05001730 if (PageChecked(page)) {
1731 ClearPageChecked(page);
1732 goto good;
1733 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001734 if (btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -05001735 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05001736
Yan Zheng17d217f2008-12-12 10:03:38 -05001737 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
1738 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1)) {
1739 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
1740 GFP_NOFS);
1741 return 0;
1742 }
1743
Yanc2e639f2008-02-04 08:57:25 -05001744 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -05001745 private = state->private;
1746 ret = 0;
1747 } else {
1748 ret = get_state_private(io_tree, start, &private);
1749 }
Chris Mason9ab86c82009-01-07 09:48:51 -05001750 kaddr = kmap_atomic(page, KM_USER0);
Chris Masond3977122009-01-05 21:25:51 -05001751 if (ret)
Chris Mason07157aa2007-08-30 08:50:51 -04001752 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05001753
Chris Masonff79f812007-10-15 16:22:25 -04001754 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
1755 btrfs_csum_final(csum, (char *)&csum);
Chris Masond3977122009-01-05 21:25:51 -05001756 if (csum != private)
Chris Mason07157aa2007-08-30 08:50:51 -04001757 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05001758
Chris Mason9ab86c82009-01-07 09:48:51 -05001759 kunmap_atomic(kaddr, KM_USER0);
Chris Masond20f7042008-12-08 16:58:54 -05001760good:
Chris Mason7e383262008-04-09 16:28:12 -04001761 /* if the io failure tree for this inode is non-empty,
1762 * check to see if we've recovered from a failed IO
1763 */
Chris Mason1259ab72008-05-12 13:39:03 -04001764 btrfs_clean_io_failures(inode, start);
Chris Mason07157aa2007-08-30 08:50:51 -04001765 return 0;
1766
1767zeroit:
Chris Masond3977122009-01-05 21:25:51 -05001768 printk(KERN_INFO "btrfs csum failed ino %lu off %llu csum %u "
1769 "private %llu\n", page->mapping->host->i_ino,
1770 (unsigned long long)start, csum,
1771 (unsigned long long)private);
Chris Masondb945352007-10-15 16:15:53 -04001772 memset(kaddr + offset, 1, end - start + 1);
1773 flush_dcache_page(page);
Chris Mason9ab86c82009-01-07 09:48:51 -05001774 kunmap_atomic(kaddr, KM_USER0);
Chris Mason3b951512008-04-17 11:29:12 -04001775 if (private == 0)
1776 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04001777 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04001778}
Chris Masonb888db22007-08-27 16:49:44 -04001779
Josef Bacik7b128762008-07-24 12:17:14 -04001780/*
1781 * This creates an orphan entry for the given inode in case something goes
1782 * wrong in the middle of an unlink/truncate.
1783 */
1784int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
1785{
1786 struct btrfs_root *root = BTRFS_I(inode)->root;
1787 int ret = 0;
1788
Yanbcc63ab2008-07-30 16:29:20 -04001789 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001790
1791 /* already on the orphan list, we're good */
1792 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
Yanbcc63ab2008-07-30 16:29:20 -04001793 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001794 return 0;
1795 }
1796
1797 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
1798
Yanbcc63ab2008-07-30 16:29:20 -04001799 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001800
1801 /*
1802 * insert an orphan item to track this unlinked/truncated file
1803 */
1804 ret = btrfs_insert_orphan_item(trans, root, inode->i_ino);
1805
1806 return ret;
1807}
1808
1809/*
1810 * We have done the truncate/delete so we can go ahead and remove the orphan
1811 * item for this particular inode.
1812 */
1813int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
1814{
1815 struct btrfs_root *root = BTRFS_I(inode)->root;
1816 int ret = 0;
1817
Yanbcc63ab2008-07-30 16:29:20 -04001818 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001819
1820 if (list_empty(&BTRFS_I(inode)->i_orphan)) {
Yanbcc63ab2008-07-30 16:29:20 -04001821 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001822 return 0;
1823 }
1824
1825 list_del_init(&BTRFS_I(inode)->i_orphan);
1826 if (!trans) {
Yanbcc63ab2008-07-30 16:29:20 -04001827 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001828 return 0;
1829 }
1830
Yanbcc63ab2008-07-30 16:29:20 -04001831 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001832
1833 ret = btrfs_del_orphan_item(trans, root, inode->i_ino);
1834
1835 return ret;
1836}
1837
1838/*
1839 * this cleans up any orphans that may be left on the list from the last use
1840 * of this root.
1841 */
1842void btrfs_orphan_cleanup(struct btrfs_root *root)
1843{
1844 struct btrfs_path *path;
1845 struct extent_buffer *leaf;
1846 struct btrfs_item *item;
1847 struct btrfs_key key, found_key;
1848 struct btrfs_trans_handle *trans;
1849 struct inode *inode;
1850 int ret = 0, nr_unlink = 0, nr_truncate = 0;
1851
Josef Bacik7b128762008-07-24 12:17:14 -04001852 path = btrfs_alloc_path();
1853 if (!path)
1854 return;
1855 path->reada = -1;
1856
1857 key.objectid = BTRFS_ORPHAN_OBJECTID;
1858 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
1859 key.offset = (u64)-1;
1860
Josef Bacik7b128762008-07-24 12:17:14 -04001861
1862 while (1) {
1863 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1864 if (ret < 0) {
1865 printk(KERN_ERR "Error searching slot for orphan: %d"
1866 "\n", ret);
1867 break;
1868 }
1869
1870 /*
1871 * if ret == 0 means we found what we were searching for, which
1872 * is weird, but possible, so only screw with path if we didnt
1873 * find the key and see if we have stuff that matches
1874 */
1875 if (ret > 0) {
1876 if (path->slots[0] == 0)
1877 break;
1878 path->slots[0]--;
1879 }
1880
1881 /* pull out the item */
1882 leaf = path->nodes[0];
1883 item = btrfs_item_nr(leaf, path->slots[0]);
1884 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1885
1886 /* make sure the item matches what we want */
1887 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
1888 break;
1889 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
1890 break;
1891
1892 /* release the path since we're done with it */
1893 btrfs_release_path(root, path);
1894
1895 /*
1896 * this is where we are basically btrfs_lookup, without the
1897 * crossing root thing. we store the inode number in the
1898 * offset of the orphan item.
1899 */
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001900 inode = btrfs_iget_locked(root->fs_info->sb,
Josef Bacik7b128762008-07-24 12:17:14 -04001901 found_key.offset, root);
1902 if (!inode)
1903 break;
1904
1905 if (inode->i_state & I_NEW) {
1906 BTRFS_I(inode)->root = root;
1907
1908 /* have to set the location manually */
1909 BTRFS_I(inode)->location.objectid = inode->i_ino;
1910 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
1911 BTRFS_I(inode)->location.offset = 0;
1912
1913 btrfs_read_locked_inode(inode);
1914 unlock_new_inode(inode);
1915 }
1916
1917 /*
1918 * add this inode to the orphan list so btrfs_orphan_del does
1919 * the proper thing when we hit it
1920 */
Yanbcc63ab2008-07-30 16:29:20 -04001921 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001922 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
Yanbcc63ab2008-07-30 16:29:20 -04001923 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001924
1925 /*
1926 * if this is a bad inode, means we actually succeeded in
1927 * removing the inode, but not the orphan record, which means
1928 * we need to manually delete the orphan since iput will just
1929 * do a destroy_inode
1930 */
1931 if (is_bad_inode(inode)) {
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001932 trans = btrfs_start_transaction(root, 1);
Josef Bacik7b128762008-07-24 12:17:14 -04001933 btrfs_orphan_del(trans, inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001934 btrfs_end_transaction(trans, root);
Josef Bacik7b128762008-07-24 12:17:14 -04001935 iput(inode);
1936 continue;
1937 }
1938
1939 /* if we have links, this was a truncate, lets do that */
1940 if (inode->i_nlink) {
1941 nr_truncate++;
1942 btrfs_truncate(inode);
1943 } else {
1944 nr_unlink++;
1945 }
1946
1947 /* this will do delete_inode and everything for us */
1948 iput(inode);
1949 }
1950
1951 if (nr_unlink)
1952 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
1953 if (nr_truncate)
1954 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
1955
1956 btrfs_free_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04001957}
1958
Chris Masond352ac62008-09-29 15:18:18 -04001959/*
1960 * read an inode from the btree into the in-memory inode
1961 */
Chris Mason39279cc2007-06-12 06:35:45 -04001962void btrfs_read_locked_inode(struct inode *inode)
1963{
1964 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04001965 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001966 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04001967 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04001968 struct btrfs_root *root = BTRFS_I(inode)->root;
1969 struct btrfs_key location;
1970 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -04001971 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04001972 int ret;
1973
1974 path = btrfs_alloc_path();
1975 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -04001976 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05001977
Chris Mason39279cc2007-06-12 06:35:45 -04001978 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001979 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04001980 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04001981
Chris Mason5f39d392007-10-15 16:14:19 -04001982 leaf = path->nodes[0];
1983 inode_item = btrfs_item_ptr(leaf, path->slots[0],
1984 struct btrfs_inode_item);
1985
1986 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
1987 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
1988 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
1989 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
Chris Masondbe674a2008-07-17 12:54:05 -04001990 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04001991
1992 tspec = btrfs_inode_atime(inode_item);
1993 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
1994 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
1995
1996 tspec = btrfs_inode_mtime(inode_item);
1997 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
1998 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
1999
2000 tspec = btrfs_inode_ctime(inode_item);
2001 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2002 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2003
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002004 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04002005 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Chris Masonc3027eb2008-12-08 16:40:21 -05002006 BTRFS_I(inode)->sequence = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04002007 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04002008 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002009 rdev = btrfs_inode_rdev(leaf, inode_item);
2010
Josef Bacikaec74772008-07-24 12:12:38 -04002011 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05002012 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Josef Bacikaec74772008-07-24 12:12:38 -04002013
Chris Mason5f39d392007-10-15 16:14:19 -04002014 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002015
Yan Zhengd2fb3432008-12-11 16:30:39 -05002016 BTRFS_I(inode)->block_group = btrfs_find_block_group(root, 0,
2017 alloc_group_block, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002018 btrfs_free_path(path);
2019 inode_item = NULL;
2020
Chris Mason39279cc2007-06-12 06:35:45 -04002021 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04002022 case S_IFREG:
2023 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04002024 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05002025 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002026 inode->i_fop = &btrfs_file_operations;
2027 inode->i_op = &btrfs_file_inode_operations;
2028 break;
2029 case S_IFDIR:
2030 inode->i_fop = &btrfs_dir_file_operations;
2031 if (root == root->fs_info->tree_root)
2032 inode->i_op = &btrfs_dir_ro_inode_operations;
2033 else
2034 inode->i_op = &btrfs_dir_inode_operations;
2035 break;
2036 case S_IFLNK:
2037 inode->i_op = &btrfs_symlink_inode_operations;
2038 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04002039 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04002040 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04002041 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05002042 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04002043 init_special_inode(inode, inode->i_mode, rdev);
2044 break;
Chris Mason39279cc2007-06-12 06:35:45 -04002045 }
2046 return;
2047
2048make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04002049 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002050 make_bad_inode(inode);
2051}
2052
Chris Masond352ac62008-09-29 15:18:18 -04002053/*
2054 * given a leaf and an inode, copy the inode fields into the leaf
2055 */
Chris Masone02119d2008-09-05 16:13:11 -04002056static void fill_inode_item(struct btrfs_trans_handle *trans,
2057 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04002058 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04002059 struct inode *inode)
2060{
Chris Mason5f39d392007-10-15 16:14:19 -04002061 btrfs_set_inode_uid(leaf, item, inode->i_uid);
2062 btrfs_set_inode_gid(leaf, item, inode->i_gid);
Chris Masondbe674a2008-07-17 12:54:05 -04002063 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
Chris Mason5f39d392007-10-15 16:14:19 -04002064 btrfs_set_inode_mode(leaf, item, inode->i_mode);
2065 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
2066
2067 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
2068 inode->i_atime.tv_sec);
2069 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
2070 inode->i_atime.tv_nsec);
2071
2072 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
2073 inode->i_mtime.tv_sec);
2074 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
2075 inode->i_mtime.tv_nsec);
2076
2077 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
2078 inode->i_ctime.tv_sec);
2079 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
2080 inode->i_ctime.tv_nsec);
2081
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002082 btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode));
Chris Masone02119d2008-09-05 16:13:11 -04002083 btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
Chris Masonc3027eb2008-12-08 16:40:21 -05002084 btrfs_set_inode_sequence(leaf, item, BTRFS_I(inode)->sequence);
Chris Masone02119d2008-09-05 16:13:11 -04002085 btrfs_set_inode_transid(leaf, item, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002086 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -05002087 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002088 btrfs_set_inode_block_group(leaf, item, BTRFS_I(inode)->block_group);
Chris Mason39279cc2007-06-12 06:35:45 -04002089}
2090
Chris Masond352ac62008-09-29 15:18:18 -04002091/*
2092 * copy everything in the in-memory inode into the btree.
2093 */
Chris Masond3977122009-01-05 21:25:51 -05002094noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
2095 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002096{
2097 struct btrfs_inode_item *inode_item;
2098 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002099 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002100 int ret;
2101
2102 path = btrfs_alloc_path();
2103 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -04002104 ret = btrfs_lookup_inode(trans, root, path,
2105 &BTRFS_I(inode)->location, 1);
2106 if (ret) {
2107 if (ret > 0)
2108 ret = -ENOENT;
2109 goto failed;
2110 }
2111
Chris Masonb4ce94d2009-02-04 09:25:08 -05002112 btrfs_unlock_up_safe(path, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002113 leaf = path->nodes[0];
2114 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04002115 struct btrfs_inode_item);
2116
Chris Masone02119d2008-09-05 16:13:11 -04002117 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002118 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002119 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002120 ret = 0;
2121failed:
Chris Mason39279cc2007-06-12 06:35:45 -04002122 btrfs_free_path(path);
2123 return ret;
2124}
2125
2126
Chris Masond352ac62008-09-29 15:18:18 -04002127/*
2128 * unlink helper that gets used here in inode.c and in the tree logging
2129 * recovery code. It remove a link in a directory with a given name, and
2130 * also drops the back refs in the inode to the directory
2131 */
Chris Masone02119d2008-09-05 16:13:11 -04002132int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2133 struct btrfs_root *root,
2134 struct inode *dir, struct inode *inode,
2135 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04002136{
2137 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04002138 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002139 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002140 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04002141 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04002142 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04002143
2144 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04002145 if (!path) {
2146 ret = -ENOMEM;
2147 goto err;
2148 }
2149
Chris Mason39279cc2007-06-12 06:35:45 -04002150 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
2151 name, name_len, -1);
2152 if (IS_ERR(di)) {
2153 ret = PTR_ERR(di);
2154 goto err;
2155 }
2156 if (!di) {
2157 ret = -ENOENT;
2158 goto err;
2159 }
Chris Mason5f39d392007-10-15 16:14:19 -04002160 leaf = path->nodes[0];
2161 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04002162 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04002163 if (ret)
2164 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -04002165 btrfs_release_path(root, path);
2166
Josef Bacikaec74772008-07-24 12:12:38 -04002167 ret = btrfs_del_inode_ref(trans, root, name, name_len,
Chris Masone02119d2008-09-05 16:13:11 -04002168 inode->i_ino,
2169 dir->i_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04002170 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05002171 printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
Josef Bacikaec74772008-07-24 12:12:38 -04002172 "inode %lu parent %lu\n", name_len, name,
Chris Masone02119d2008-09-05 16:13:11 -04002173 inode->i_ino, dir->i_ino);
Josef Bacikaec74772008-07-24 12:12:38 -04002174 goto err;
2175 }
2176
Chris Mason39279cc2007-06-12 06:35:45 -04002177 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04002178 index, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -04002179 if (IS_ERR(di)) {
2180 ret = PTR_ERR(di);
2181 goto err;
2182 }
2183 if (!di) {
2184 ret = -ENOENT;
2185 goto err;
2186 }
2187 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason925baed2008-06-25 16:01:30 -04002188 btrfs_release_path(root, path);
Chris Mason39279cc2007-06-12 06:35:45 -04002189
Chris Masone02119d2008-09-05 16:13:11 -04002190 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
2191 inode, dir->i_ino);
Chris Mason49eb7e42008-09-11 15:53:12 -04002192 BUG_ON(ret != 0 && ret != -ENOENT);
2193 if (ret != -ENOENT)
2194 BTRFS_I(dir)->log_dirty_trans = trans->transid;
Chris Masone02119d2008-09-05 16:13:11 -04002195
2196 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
2197 dir, index);
2198 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04002199err:
2200 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002201 if (ret)
2202 goto out;
2203
2204 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
2205 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2206 btrfs_update_inode(trans, root, dir);
2207 btrfs_drop_nlink(inode);
2208 ret = btrfs_update_inode(trans, root, inode);
2209 dir->i_sb->s_dirt = 1;
2210out:
Chris Mason39279cc2007-06-12 06:35:45 -04002211 return ret;
2212}
2213
2214static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
2215{
2216 struct btrfs_root *root;
2217 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04002218 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002219 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -05002220 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002221
2222 root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05002223
Chris Mason39279cc2007-06-12 06:35:45 -04002224 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002225
Chris Mason39279cc2007-06-12 06:35:45 -04002226 btrfs_set_trans_block_group(trans, dir);
Chris Masone02119d2008-09-05 16:13:11 -04002227 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
2228 dentry->d_name.name, dentry->d_name.len);
Josef Bacik7b128762008-07-24 12:17:14 -04002229
2230 if (inode->i_nlink == 0)
2231 ret = btrfs_orphan_add(trans, inode);
2232
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002233 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04002234
Chris Mason89ce8a62008-06-25 16:01:31 -04002235 btrfs_end_transaction_throttle(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002236 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002237 return ret;
2238}
2239
2240static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
2241{
2242 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002243 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002244 int ret;
2245 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04002246 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -05002247 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002248
Chris Mason3394e162008-11-17 20:42:26 -05002249 /*
2250 * the FIRST_FREE_OBJECTID check makes sure we don't try to rmdir
2251 * the root of a subvolume or snapshot
2252 */
2253 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE ||
2254 inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) {
Yan134d4512007-10-25 15:49:25 -04002255 return -ENOTEMPTY;
Chris Mason925baed2008-06-25 16:01:30 -04002256 }
Yan134d4512007-10-25 15:49:25 -04002257
Chris Mason39279cc2007-06-12 06:35:45 -04002258 trans = btrfs_start_transaction(root, 1);
2259 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -04002260
Josef Bacik7b128762008-07-24 12:17:14 -04002261 err = btrfs_orphan_add(trans, inode);
2262 if (err)
2263 goto fail_trans;
2264
Chris Mason39279cc2007-06-12 06:35:45 -04002265 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04002266 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
2267 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05002268 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04002269 btrfs_i_size_write(inode, 0);
Chris Mason39544012007-12-12 14:38:19 -05002270
Josef Bacik7b128762008-07-24 12:17:14 -04002271fail_trans:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002272 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04002273 ret = btrfs_end_transaction_throttle(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002274 btrfs_btree_balance_dirty(root, nr);
Chris Mason39544012007-12-12 14:38:19 -05002275
Chris Mason39279cc2007-06-12 06:35:45 -04002276 if (ret && !err)
2277 err = ret;
2278 return err;
2279}
2280
Chris Masond20f7042008-12-08 16:58:54 -05002281#if 0
Chris Mason39279cc2007-06-12 06:35:45 -04002282/*
Chris Mason323ac952008-10-01 19:05:46 -04002283 * when truncating bytes in a file, it is possible to avoid reading
2284 * the leaves that contain only checksum items. This can be the
2285 * majority of the IO required to delete a large file, but it must
2286 * be done carefully.
2287 *
2288 * The keys in the level just above the leaves are checked to make sure
2289 * the lowest key in a given leaf is a csum key, and starts at an offset
2290 * after the new size.
2291 *
2292 * Then the key for the next leaf is checked to make sure it also has
2293 * a checksum item for the same file. If it does, we know our target leaf
2294 * contains only checksum items, and it can be safely freed without reading
2295 * it.
2296 *
2297 * This is just an optimization targeted at large files. It may do
2298 * nothing. It will return 0 unless things went badly.
2299 */
2300static noinline int drop_csum_leaves(struct btrfs_trans_handle *trans,
2301 struct btrfs_root *root,
2302 struct btrfs_path *path,
2303 struct inode *inode, u64 new_size)
2304{
2305 struct btrfs_key key;
2306 int ret;
2307 int nritems;
2308 struct btrfs_key found_key;
2309 struct btrfs_key other_key;
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002310 struct btrfs_leaf_ref *ref;
2311 u64 leaf_gen;
2312 u64 leaf_start;
Chris Mason323ac952008-10-01 19:05:46 -04002313
2314 path->lowest_level = 1;
2315 key.objectid = inode->i_ino;
2316 key.type = BTRFS_CSUM_ITEM_KEY;
2317 key.offset = new_size;
2318again:
2319 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2320 if (ret < 0)
2321 goto out;
2322
2323 if (path->nodes[1] == NULL) {
2324 ret = 0;
2325 goto out;
2326 }
2327 ret = 0;
2328 btrfs_node_key_to_cpu(path->nodes[1], &found_key, path->slots[1]);
2329 nritems = btrfs_header_nritems(path->nodes[1]);
2330
2331 if (!nritems)
2332 goto out;
2333
2334 if (path->slots[1] >= nritems)
2335 goto next_node;
2336
2337 /* did we find a key greater than anything we want to delete? */
2338 if (found_key.objectid > inode->i_ino ||
2339 (found_key.objectid == inode->i_ino && found_key.type > key.type))
2340 goto out;
2341
2342 /* we check the next key in the node to make sure the leave contains
2343 * only checksum items. This comparison doesn't work if our
2344 * leaf is the last one in the node
2345 */
2346 if (path->slots[1] + 1 >= nritems) {
2347next_node:
2348 /* search forward from the last key in the node, this
2349 * will bring us into the next node in the tree
2350 */
2351 btrfs_node_key_to_cpu(path->nodes[1], &found_key, nritems - 1);
2352
2353 /* unlikely, but we inc below, so check to be safe */
2354 if (found_key.offset == (u64)-1)
2355 goto out;
2356
2357 /* search_forward needs a path with locks held, do the
2358 * search again for the original key. It is possible
2359 * this will race with a balance and return a path that
2360 * we could modify, but this drop is just an optimization
2361 * and is allowed to miss some leaves.
2362 */
2363 btrfs_release_path(root, path);
2364 found_key.offset++;
2365
2366 /* setup a max key for search_forward */
2367 other_key.offset = (u64)-1;
2368 other_key.type = key.type;
2369 other_key.objectid = key.objectid;
2370
2371 path->keep_locks = 1;
2372 ret = btrfs_search_forward(root, &found_key, &other_key,
2373 path, 0, 0);
2374 path->keep_locks = 0;
2375 if (ret || found_key.objectid != key.objectid ||
2376 found_key.type != key.type) {
2377 ret = 0;
2378 goto out;
2379 }
2380
2381 key.offset = found_key.offset;
2382 btrfs_release_path(root, path);
2383 cond_resched();
2384 goto again;
2385 }
2386
2387 /* we know there's one more slot after us in the tree,
2388 * read that key so we can verify it is also a checksum item
2389 */
2390 btrfs_node_key_to_cpu(path->nodes[1], &other_key, path->slots[1] + 1);
2391
2392 if (found_key.objectid < inode->i_ino)
2393 goto next_key;
2394
2395 if (found_key.type != key.type || found_key.offset < new_size)
2396 goto next_key;
2397
2398 /*
2399 * if the key for the next leaf isn't a csum key from this objectid,
2400 * we can't be sure there aren't good items inside this leaf.
2401 * Bail out
2402 */
2403 if (other_key.objectid != inode->i_ino || other_key.type != key.type)
2404 goto out;
2405
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002406 leaf_start = btrfs_node_blockptr(path->nodes[1], path->slots[1]);
2407 leaf_gen = btrfs_node_ptr_generation(path->nodes[1], path->slots[1]);
Chris Mason323ac952008-10-01 19:05:46 -04002408 /*
2409 * it is safe to delete this leaf, it contains only
2410 * csum items from this inode at an offset >= new_size
2411 */
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002412 ret = btrfs_del_leaf(trans, root, path, leaf_start);
Chris Mason323ac952008-10-01 19:05:46 -04002413 BUG_ON(ret);
2414
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002415 if (root->ref_cows && leaf_gen < trans->transid) {
2416 ref = btrfs_alloc_leaf_ref(root, 0);
2417 if (ref) {
2418 ref->root_gen = root->root_key.offset;
2419 ref->bytenr = leaf_start;
2420 ref->owner = 0;
2421 ref->generation = leaf_gen;
2422 ref->nritems = 0;
2423
Chris Masonbd56b302009-02-04 09:27:02 -05002424 btrfs_sort_leaf_ref(ref);
2425
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002426 ret = btrfs_add_leaf_ref(root, ref, 0);
2427 WARN_ON(ret);
2428 btrfs_free_leaf_ref(root, ref);
2429 } else {
2430 WARN_ON(1);
2431 }
2432 }
Chris Mason323ac952008-10-01 19:05:46 -04002433next_key:
2434 btrfs_release_path(root, path);
2435
2436 if (other_key.objectid == inode->i_ino &&
2437 other_key.type == key.type && other_key.offset > key.offset) {
2438 key.offset = other_key.offset;
2439 cond_resched();
2440 goto again;
2441 }
2442 ret = 0;
2443out:
2444 /* fixup any changes we've made to the path */
2445 path->lowest_level = 0;
2446 path->keep_locks = 0;
2447 btrfs_release_path(root, path);
2448 return ret;
2449}
2450
Chris Masond20f7042008-12-08 16:58:54 -05002451#endif
2452
Chris Mason323ac952008-10-01 19:05:46 -04002453/*
Chris Mason39279cc2007-06-12 06:35:45 -04002454 * this can truncate away extent items, csum items and directory items.
2455 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04002456 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04002457 *
2458 * csum items that cross the new i_size are truncated to the new size
2459 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04002460 *
2461 * min_type is the minimum key type to truncate down to. If set to 0, this
2462 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04002463 */
Chris Masone02119d2008-09-05 16:13:11 -04002464noinline int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
2465 struct btrfs_root *root,
2466 struct inode *inode,
2467 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04002468{
2469 int ret;
2470 struct btrfs_path *path;
2471 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04002472 struct btrfs_key found_key;
Chris Mason06d9a8d2009-02-04 09:30:58 -05002473 u32 found_type = (u8)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04002474 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002475 struct btrfs_file_extent_item *fi;
2476 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04002477 u64 extent_num_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002478 u64 item_end = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05002479 u64 root_gen = 0;
Chris Masond8d5f3e2007-12-11 12:42:00 -05002480 u64 root_owner = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002481 int found_extent;
2482 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05002483 int pending_del_nr = 0;
2484 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04002485 int extent_type = -1;
Chris Mason771ed682008-11-06 22:02:51 -05002486 int encoding;
Chris Mason3b951512008-04-17 11:29:12 -04002487 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002488
Chris Masone02119d2008-09-05 16:13:11 -04002489 if (root->ref_cows)
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002490 btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002491 path = btrfs_alloc_path();
Chris Mason3c69fae2007-08-07 15:52:22 -04002492 path->reada = -1;
Chris Mason39279cc2007-06-12 06:35:45 -04002493 BUG_ON(!path);
Chris Mason5f39d392007-10-15 16:14:19 -04002494
Chris Mason39279cc2007-06-12 06:35:45 -04002495 /* FIXME, add redo link to tree so we don't leak on crash */
2496 key.objectid = inode->i_ino;
2497 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04002498 key.type = (u8)-1;
2499
Chris Mason85e21ba2008-01-29 15:11:36 -05002500search_again:
2501 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masond3977122009-01-05 21:25:51 -05002502 if (ret < 0)
Chris Mason85e21ba2008-01-29 15:11:36 -05002503 goto error;
Chris Masond3977122009-01-05 21:25:51 -05002504
Chris Mason85e21ba2008-01-29 15:11:36 -05002505 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002506 /* there are no items in the tree for us to truncate, we're
2507 * done
2508 */
2509 if (path->slots[0] == 0) {
2510 ret = 0;
2511 goto error;
2512 }
Chris Mason85e21ba2008-01-29 15:11:36 -05002513 path->slots[0]--;
2514 }
2515
Chris Masond3977122009-01-05 21:25:51 -05002516 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04002517 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04002518 leaf = path->nodes[0];
2519 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2520 found_type = btrfs_key_type(&found_key);
Chris Mason771ed682008-11-06 22:02:51 -05002521 encoding = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002522
Chris Mason5f39d392007-10-15 16:14:19 -04002523 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -04002524 break;
Chris Mason5f39d392007-10-15 16:14:19 -04002525
Chris Mason85e21ba2008-01-29 15:11:36 -05002526 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04002527 break;
2528
Chris Mason5f39d392007-10-15 16:14:19 -04002529 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04002530 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04002531 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04002532 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04002533 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Mason771ed682008-11-06 22:02:51 -05002534 encoding = btrfs_file_extent_compression(leaf, fi);
2535 encoding |= btrfs_file_extent_encryption(leaf, fi);
2536 encoding |= btrfs_file_extent_other_encoding(leaf, fi);
2537
Chris Mason179e29e2007-11-01 11:28:41 -04002538 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04002539 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04002540 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04002541 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04002542 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04002543 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04002544 }
Yan008630c2007-11-07 13:31:09 -05002545 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04002546 }
Chris Masone02119d2008-09-05 16:13:11 -04002547 if (item_end < new_size) {
Chris Masond3977122009-01-05 21:25:51 -05002548 if (found_type == BTRFS_DIR_ITEM_KEY)
Chris Masonb888db22007-08-27 16:49:44 -04002549 found_type = BTRFS_INODE_ITEM_KEY;
Chris Masond3977122009-01-05 21:25:51 -05002550 else if (found_type == BTRFS_EXTENT_ITEM_KEY)
Chris Masond20f7042008-12-08 16:58:54 -05002551 found_type = BTRFS_EXTENT_DATA_KEY;
Chris Masond3977122009-01-05 21:25:51 -05002552 else if (found_type == BTRFS_EXTENT_DATA_KEY)
Chris Mason85e21ba2008-01-29 15:11:36 -05002553 found_type = BTRFS_XATTR_ITEM_KEY;
Chris Masond3977122009-01-05 21:25:51 -05002554 else if (found_type == BTRFS_XATTR_ITEM_KEY)
Chris Mason85e21ba2008-01-29 15:11:36 -05002555 found_type = BTRFS_INODE_REF_KEY;
Chris Masond3977122009-01-05 21:25:51 -05002556 else if (found_type)
Chris Masonb888db22007-08-27 16:49:44 -04002557 found_type--;
Chris Masond3977122009-01-05 21:25:51 -05002558 else
Chris Masonb888db22007-08-27 16:49:44 -04002559 break;
Yana61721d2007-09-17 11:08:38 -04002560 btrfs_set_key_type(&key, found_type);
Chris Mason85e21ba2008-01-29 15:11:36 -05002561 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04002562 }
Chris Masone02119d2008-09-05 16:13:11 -04002563 if (found_key.offset >= new_size)
Chris Mason39279cc2007-06-12 06:35:45 -04002564 del_item = 1;
2565 else
2566 del_item = 0;
2567 found_extent = 0;
2568
2569 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04002570 if (found_type != BTRFS_EXTENT_DATA_KEY)
2571 goto delete;
2572
2573 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04002574 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04002575 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason771ed682008-11-06 22:02:51 -05002576 if (!del_item && !encoding) {
Chris Masondb945352007-10-15 16:15:53 -04002577 u64 orig_num_bytes =
2578 btrfs_file_extent_num_bytes(leaf, fi);
Chris Masone02119d2008-09-05 16:13:11 -04002579 extent_num_bytes = new_size -
Chris Mason5f39d392007-10-15 16:14:19 -04002580 found_key.offset + root->sectorsize - 1;
Yanb1632b12008-01-30 11:54:04 -05002581 extent_num_bytes = extent_num_bytes &
2582 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04002583 btrfs_set_file_extent_num_bytes(leaf, fi,
2584 extent_num_bytes);
2585 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05002586 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04002587 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002588 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04002589 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002590 } else {
Chris Masondb945352007-10-15 16:15:53 -04002591 extent_num_bytes =
2592 btrfs_file_extent_disk_num_bytes(leaf,
2593 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04002594 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05002595 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04002596 if (extent_start != 0) {
2597 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04002598 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002599 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04002600 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002601 root_gen = btrfs_header_generation(leaf);
Chris Masond8d5f3e2007-12-11 12:42:00 -05002602 root_owner = btrfs_header_owner(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002603 }
Chris Mason90692182008-02-08 13:49:28 -05002604 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04002605 /*
2606 * we can't truncate inline items that have had
2607 * special encodings
2608 */
2609 if (!del_item &&
2610 btrfs_file_extent_compression(leaf, fi) == 0 &&
2611 btrfs_file_extent_encryption(leaf, fi) == 0 &&
2612 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002613 u32 size = new_size - found_key.offset;
2614
2615 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002616 inode_sub_bytes(inode, item_end + 1 -
2617 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04002618 }
2619 size =
2620 btrfs_file_extent_calc_inline_size(size);
Chris Mason90692182008-02-08 13:49:28 -05002621 ret = btrfs_truncate_item(trans, root, path,
Chris Masone02119d2008-09-05 16:13:11 -04002622 size, 1);
Chris Mason90692182008-02-08 13:49:28 -05002623 BUG_ON(ret);
Chris Masone02119d2008-09-05 16:13:11 -04002624 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002625 inode_sub_bytes(inode, item_end + 1 -
2626 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05002627 }
Chris Mason39279cc2007-06-12 06:35:45 -04002628 }
Chris Mason179e29e2007-11-01 11:28:41 -04002629delete:
Chris Mason39279cc2007-06-12 06:35:45 -04002630 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05002631 if (!pending_del_nr) {
2632 /* no pending yet, add ourselves */
2633 pending_del_slot = path->slots[0];
2634 pending_del_nr = 1;
2635 } else if (pending_del_nr &&
2636 path->slots[0] + 1 == pending_del_slot) {
2637 /* hop on the pending chunk */
2638 pending_del_nr++;
2639 pending_del_slot = path->slots[0];
2640 } else {
Chris Masond3977122009-01-05 21:25:51 -05002641 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05002642 }
Chris Mason39279cc2007-06-12 06:35:45 -04002643 } else {
2644 break;
2645 }
Chris Mason39279cc2007-06-12 06:35:45 -04002646 if (found_extent) {
2647 ret = btrfs_free_extent(trans, root, extent_start,
Chris Mason7bb86312007-12-11 09:25:06 -05002648 extent_num_bytes,
Zheng Yan31840ae2008-09-23 13:14:14 -04002649 leaf->start, root_owner,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002650 root_gen, inode->i_ino, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002651 BUG_ON(ret);
2652 }
Chris Mason85e21ba2008-01-29 15:11:36 -05002653next:
2654 if (path->slots[0] == 0) {
2655 if (pending_del_nr)
2656 goto del_pending;
2657 btrfs_release_path(root, path);
Chris Mason06d9a8d2009-02-04 09:30:58 -05002658 if (found_type == BTRFS_INODE_ITEM_KEY)
2659 break;
Chris Mason85e21ba2008-01-29 15:11:36 -05002660 goto search_again;
2661 }
2662
2663 path->slots[0]--;
2664 if (pending_del_nr &&
2665 path->slots[0] + 1 != pending_del_slot) {
2666 struct btrfs_key debug;
2667del_pending:
2668 btrfs_item_key_to_cpu(path->nodes[0], &debug,
2669 pending_del_slot);
2670 ret = btrfs_del_items(trans, root, path,
2671 pending_del_slot,
2672 pending_del_nr);
2673 BUG_ON(ret);
2674 pending_del_nr = 0;
2675 btrfs_release_path(root, path);
Chris Mason06d9a8d2009-02-04 09:30:58 -05002676 if (found_type == BTRFS_INODE_ITEM_KEY)
2677 break;
Chris Mason85e21ba2008-01-29 15:11:36 -05002678 goto search_again;
2679 }
Chris Mason39279cc2007-06-12 06:35:45 -04002680 }
2681 ret = 0;
2682error:
Chris Mason85e21ba2008-01-29 15:11:36 -05002683 if (pending_del_nr) {
2684 ret = btrfs_del_items(trans, root, path, pending_del_slot,
2685 pending_del_nr);
2686 }
Chris Mason39279cc2007-06-12 06:35:45 -04002687 btrfs_free_path(path);
2688 inode->i_sb->s_dirt = 1;
2689 return ret;
2690}
2691
Chris Masona52d9a82007-08-27 16:49:44 -04002692/*
2693 * taken from block_truncate_page, but does cow as it zeros out
2694 * any bytes left in the last page in the file.
2695 */
2696static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
2697{
2698 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -04002699 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002700 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2701 struct btrfs_ordered_extent *ordered;
2702 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04002703 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04002704 pgoff_t index = from >> PAGE_CACHE_SHIFT;
2705 unsigned offset = from & (PAGE_CACHE_SIZE-1);
2706 struct page *page;
2707 int ret = 0;
2708 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002709 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04002710
2711 if ((offset & (blocksize - 1)) == 0)
2712 goto out;
2713
2714 ret = -ENOMEM;
Chris Mason211c17f2008-05-15 09:13:45 -04002715again:
Chris Masona52d9a82007-08-27 16:49:44 -04002716 page = grab_cache_page(mapping, index);
2717 if (!page)
2718 goto out;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002719
2720 page_start = page_offset(page);
2721 page_end = page_start + PAGE_CACHE_SIZE - 1;
2722
Chris Masona52d9a82007-08-27 16:49:44 -04002723 if (!PageUptodate(page)) {
2724 ret = btrfs_readpage(NULL, page);
2725 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04002726 if (page->mapping != mapping) {
2727 unlock_page(page);
2728 page_cache_release(page);
2729 goto again;
2730 }
Chris Masona52d9a82007-08-27 16:49:44 -04002731 if (!PageUptodate(page)) {
2732 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04002733 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04002734 }
2735 }
Chris Mason211c17f2008-05-15 09:13:45 -04002736 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002737
2738 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
2739 set_page_extent_mapped(page);
2740
2741 ordered = btrfs_lookup_ordered_extent(inode, page_start);
2742 if (ordered) {
2743 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
2744 unlock_page(page);
2745 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04002746 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002747 btrfs_put_ordered_extent(ordered);
2748 goto again;
2749 }
2750
Chris Masonea8c2812008-08-04 23:17:27 -04002751 btrfs_set_extent_delalloc(inode, page_start, page_end);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002752 ret = 0;
2753 if (offset != PAGE_CACHE_SIZE) {
2754 kaddr = kmap(page);
2755 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
2756 flush_dcache_page(page);
2757 kunmap(page);
2758 }
Chris Mason247e7432008-07-17 12:53:51 -04002759 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002760 set_page_dirty(page);
2761 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04002762
Chris Mason89642222008-07-24 09:41:53 -04002763out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04002764 unlock_page(page);
2765 page_cache_release(page);
2766out:
2767 return ret;
2768}
2769
Yan Zheng9036c102008-10-30 14:19:41 -04002770int btrfs_cont_expand(struct inode *inode, loff_t size)
2771{
2772 struct btrfs_trans_handle *trans;
2773 struct btrfs_root *root = BTRFS_I(inode)->root;
2774 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2775 struct extent_map *em;
2776 u64 mask = root->sectorsize - 1;
2777 u64 hole_start = (inode->i_size + mask) & ~mask;
2778 u64 block_end = (size + mask) & ~mask;
2779 u64 last_byte;
2780 u64 cur_offset;
2781 u64 hole_size;
2782 int err;
2783
2784 if (size <= hole_start)
2785 return 0;
2786
Josef Bacik6a632092009-02-20 11:00:09 -05002787 err = btrfs_check_metadata_free_space(root);
Yan Zheng9036c102008-10-30 14:19:41 -04002788 if (err)
2789 return err;
2790
2791 btrfs_truncate_page(inode->i_mapping, inode->i_size);
2792
2793 while (1) {
2794 struct btrfs_ordered_extent *ordered;
2795 btrfs_wait_ordered_range(inode, hole_start,
2796 block_end - hole_start);
2797 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
2798 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
2799 if (!ordered)
2800 break;
2801 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
2802 btrfs_put_ordered_extent(ordered);
2803 }
2804
2805 trans = btrfs_start_transaction(root, 1);
2806 btrfs_set_trans_block_group(trans, inode);
2807
2808 cur_offset = hole_start;
2809 while (1) {
2810 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
2811 block_end - cur_offset, 0);
2812 BUG_ON(IS_ERR(em) || !em);
2813 last_byte = min(extent_map_end(em), block_end);
2814 last_byte = (last_byte + mask) & ~mask;
2815 if (test_bit(EXTENT_FLAG_VACANCY, &em->flags)) {
Chris Mason771ed682008-11-06 22:02:51 -05002816 u64 hint_byte = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04002817 hole_size = last_byte - cur_offset;
Chris Mason771ed682008-11-06 22:02:51 -05002818 err = btrfs_drop_extents(trans, root, inode,
2819 cur_offset,
2820 cur_offset + hole_size,
2821 cur_offset, &hint_byte);
2822 if (err)
2823 break;
Yan Zheng9036c102008-10-30 14:19:41 -04002824 err = btrfs_insert_file_extent(trans, root,
2825 inode->i_ino, cur_offset, 0,
2826 0, hole_size, 0, hole_size,
2827 0, 0, 0);
2828 btrfs_drop_extent_cache(inode, hole_start,
2829 last_byte - 1, 0);
2830 }
2831 free_extent_map(em);
2832 cur_offset = last_byte;
2833 if (err || cur_offset >= block_end)
2834 break;
2835 }
2836
2837 btrfs_end_transaction(trans, root);
2838 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
2839 return err;
2840}
2841
Chris Mason39279cc2007-06-12 06:35:45 -04002842static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
2843{
2844 struct inode *inode = dentry->d_inode;
2845 int err;
2846
2847 err = inode_change_ok(inode, attr);
2848 if (err)
2849 return err;
2850
2851 if (S_ISREG(inode->i_mode) &&
2852 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
Yan Zheng9036c102008-10-30 14:19:41 -04002853 err = btrfs_cont_expand(inode, attr->ia_size);
Chris Mason54aa1f42007-06-22 14:16:25 -04002854 if (err)
2855 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04002856 }
Yan Zheng9036c102008-10-30 14:19:41 -04002857
Chris Mason39279cc2007-06-12 06:35:45 -04002858 err = inode_setattr(inode, attr);
Josef Bacik33268ea2008-07-24 12:16:36 -04002859
2860 if (!err && ((attr->ia_valid & ATTR_MODE)))
2861 err = btrfs_acl_chmod(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002862 return err;
2863}
Chris Mason61295eb2008-01-14 16:24:38 -05002864
Chris Mason39279cc2007-06-12 06:35:45 -04002865void btrfs_delete_inode(struct inode *inode)
2866{
2867 struct btrfs_trans_handle *trans;
2868 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002869 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04002870 int ret;
2871
2872 truncate_inode_pages(&inode->i_data, 0);
2873 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04002874 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002875 goto no_delete;
2876 }
Chris Mason4a096752008-07-21 10:29:44 -04002877 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04002878
Chris Masondbe674a2008-07-17 12:54:05 -04002879 btrfs_i_size_write(inode, 0);
Yan Zheng180591b2009-01-06 09:58:06 -05002880 trans = btrfs_join_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002881
Chris Mason39279cc2007-06-12 06:35:45 -04002882 btrfs_set_trans_block_group(trans, inode);
Chris Masone02119d2008-09-05 16:13:11 -04002883 ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size, 0);
Josef Bacik7b128762008-07-24 12:17:14 -04002884 if (ret) {
2885 btrfs_orphan_del(NULL, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04002886 goto no_delete_lock;
Josef Bacik7b128762008-07-24 12:17:14 -04002887 }
2888
2889 btrfs_orphan_del(trans, inode);
Chris Mason85e21ba2008-01-29 15:11:36 -05002890
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002891 nr = trans->blocks_used;
Chris Mason85e21ba2008-01-29 15:11:36 -05002892 clear_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002893
Chris Mason39279cc2007-06-12 06:35:45 -04002894 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002895 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002896 return;
Chris Mason54aa1f42007-06-22 14:16:25 -04002897
2898no_delete_lock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002899 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04002900 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002901 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002902no_delete:
2903 clear_inode(inode);
2904}
2905
2906/*
2907 * this returns the key found in the dir entry in the location pointer.
2908 * If no dir entries were found, location->objectid is 0.
2909 */
2910static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
2911 struct btrfs_key *location)
2912{
2913 const char *name = dentry->d_name.name;
2914 int namelen = dentry->d_name.len;
2915 struct btrfs_dir_item *di;
2916 struct btrfs_path *path;
2917 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04002918 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002919
2920 path = btrfs_alloc_path();
2921 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -05002922
Chris Mason39279cc2007-06-12 06:35:45 -04002923 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
2924 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04002925 if (IS_ERR(di))
2926 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05002927
2928 if (!di || IS_ERR(di))
Chris Mason39544012007-12-12 14:38:19 -05002929 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05002930
Chris Mason5f39d392007-10-15 16:14:19 -04002931 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04002932out:
Chris Mason39279cc2007-06-12 06:35:45 -04002933 btrfs_free_path(path);
2934 return ret;
Chris Mason39544012007-12-12 14:38:19 -05002935out_err:
2936 location->objectid = 0;
2937 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04002938}
2939
2940/*
2941 * when we hit a tree root in a directory, the btrfs part of the inode
2942 * needs to be changed to reflect the root directory of the tree root. This
2943 * is kind of like crossing a mount point.
2944 */
2945static int fixup_tree_root_location(struct btrfs_root *root,
2946 struct btrfs_key *location,
Josef Bacik58176a92007-08-29 15:47:34 -04002947 struct btrfs_root **sub_root,
2948 struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04002949{
Chris Mason39279cc2007-06-12 06:35:45 -04002950 struct btrfs_root_item *ri;
2951
2952 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
2953 return 0;
2954 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
2955 return 0;
2956
Josef Bacik58176a92007-08-29 15:47:34 -04002957 *sub_root = btrfs_read_fs_root(root->fs_info, location,
2958 dentry->d_name.name,
2959 dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04002960 if (IS_ERR(*sub_root))
2961 return PTR_ERR(*sub_root);
2962
2963 ri = &(*sub_root)->root_item;
2964 location->objectid = btrfs_root_dirid(ri);
Chris Mason39279cc2007-06-12 06:35:45 -04002965 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
2966 location->offset = 0;
2967
Chris Mason39279cc2007-06-12 06:35:45 -04002968 return 0;
2969}
2970
Chris Masone02119d2008-09-05 16:13:11 -04002971static noinline void init_btrfs_i(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002972{
Chris Masone02119d2008-09-05 16:13:11 -04002973 struct btrfs_inode *bi = BTRFS_I(inode);
2974
2975 bi->i_acl = NULL;
2976 bi->i_default_acl = NULL;
2977
2978 bi->generation = 0;
Chris Masonc3027eb2008-12-08 16:40:21 -05002979 bi->sequence = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002980 bi->last_trans = 0;
2981 bi->logged_trans = 0;
2982 bi->delalloc_bytes = 0;
Josef Bacik6a632092009-02-20 11:00:09 -05002983 bi->reserved_bytes = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002984 bi->disk_i_size = 0;
2985 bi->flags = 0;
2986 bi->index_cnt = (u64)-1;
Chris Mason49eb7e42008-09-11 15:53:12 -04002987 bi->log_dirty_trans = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002988 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
2989 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04002990 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04002991 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
2992 inode->i_mapping, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04002993 INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes);
Chris Masonba1da2f2008-07-17 12:54:15 -04002994 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Masonee6e6502008-07-17 12:54:40 -04002995 mutex_init(&BTRFS_I(inode)->extent_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04002996 mutex_init(&BTRFS_I(inode)->log_mutex);
2997}
2998
2999static int btrfs_init_locked_inode(struct inode *inode, void *p)
3000{
3001 struct btrfs_iget_args *args = p;
3002 inode->i_ino = args->ino;
3003 init_btrfs_i(inode);
3004 BTRFS_I(inode)->root = args->root;
Josef Bacik6a632092009-02-20 11:00:09 -05003005 btrfs_set_inode_space_info(args->root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003006 return 0;
3007}
3008
3009static int btrfs_find_actor(struct inode *inode, void *opaque)
3010{
3011 struct btrfs_iget_args *args = opaque;
Chris Masond3977122009-01-05 21:25:51 -05003012 return args->ino == inode->i_ino &&
3013 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003014}
3015
Zheng Yan5b21f2e2008-09-26 10:05:38 -04003016struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
3017 struct btrfs_root *root, int wait)
3018{
3019 struct inode *inode;
3020 struct btrfs_iget_args args;
3021 args.ino = objectid;
3022 args.root = root;
3023
3024 if (wait) {
3025 inode = ilookup5(s, objectid, btrfs_find_actor,
3026 (void *)&args);
3027 } else {
3028 inode = ilookup5_nowait(s, objectid, btrfs_find_actor,
3029 (void *)&args);
3030 }
3031 return inode;
3032}
3033
Chris Mason39279cc2007-06-12 06:35:45 -04003034struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
3035 struct btrfs_root *root)
3036{
3037 struct inode *inode;
3038 struct btrfs_iget_args args;
3039 args.ino = objectid;
3040 args.root = root;
3041
3042 inode = iget5_locked(s, objectid, btrfs_find_actor,
3043 btrfs_init_locked_inode,
3044 (void *)&args);
3045 return inode;
3046}
3047
Balaji Rao1a54ef82008-07-21 02:01:04 +05303048/* Get an inode object given its location and corresponding root.
3049 * Returns in *is_new if the inode was read from disk
3050 */
3051struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
3052 struct btrfs_root *root, int *is_new)
3053{
3054 struct inode *inode;
3055
3056 inode = btrfs_iget_locked(s, location->objectid, root);
3057 if (!inode)
3058 return ERR_PTR(-EACCES);
3059
3060 if (inode->i_state & I_NEW) {
3061 BTRFS_I(inode)->root = root;
3062 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
3063 btrfs_read_locked_inode(inode);
3064 unlock_new_inode(inode);
3065 if (is_new)
3066 *is_new = 1;
3067 } else {
3068 if (is_new)
3069 *is_new = 0;
3070 }
3071
3072 return inode;
3073}
3074
Chris Mason3de45862008-11-17 21:02:50 -05003075struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04003076{
Chris Masond3977122009-01-05 21:25:51 -05003077 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003078 struct btrfs_inode *bi = BTRFS_I(dir);
3079 struct btrfs_root *root = bi->root;
3080 struct btrfs_root *sub_root = root;
3081 struct btrfs_key location;
Yan Zhengc146afa2008-11-12 14:34:12 -05003082 int ret, new;
Chris Mason39279cc2007-06-12 06:35:45 -04003083
3084 if (dentry->d_name.len > BTRFS_NAME_LEN)
3085 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04003086
Chris Mason39279cc2007-06-12 06:35:45 -04003087 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04003088
Chris Mason39279cc2007-06-12 06:35:45 -04003089 if (ret < 0)
3090 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04003091
Chris Mason39279cc2007-06-12 06:35:45 -04003092 inode = NULL;
3093 if (location.objectid) {
Josef Bacik58176a92007-08-29 15:47:34 -04003094 ret = fixup_tree_root_location(root, &location, &sub_root,
3095 dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04003096 if (ret < 0)
3097 return ERR_PTR(ret);
3098 if (ret > 0)
3099 return ERR_PTR(-ENOENT);
Balaji Rao1a54ef82008-07-21 02:01:04 +05303100 inode = btrfs_iget(dir->i_sb, &location, sub_root, &new);
3101 if (IS_ERR(inode))
3102 return ERR_CAST(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003103 }
Chris Mason3de45862008-11-17 21:02:50 -05003104 return inode;
3105}
3106
3107static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
3108 struct nameidata *nd)
3109{
3110 struct inode *inode;
3111
3112 if (dentry->d_name.len > BTRFS_NAME_LEN)
3113 return ERR_PTR(-ENAMETOOLONG);
3114
3115 inode = btrfs_lookup_dentry(dir, dentry);
3116 if (IS_ERR(inode))
3117 return ERR_CAST(inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003118
Chris Mason39279cc2007-06-12 06:35:45 -04003119 return d_splice_alias(inode, dentry);
3120}
3121
Chris Mason39279cc2007-06-12 06:35:45 -04003122static unsigned char btrfs_filetype_table[] = {
3123 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
3124};
3125
David Woodhousecbdf5a22008-08-06 19:42:33 +01003126static int btrfs_real_readdir(struct file *filp, void *dirent,
3127 filldir_t filldir)
Chris Mason39279cc2007-06-12 06:35:45 -04003128{
Chris Mason6da6aba2007-12-18 16:15:09 -05003129 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003130 struct btrfs_root *root = BTRFS_I(inode)->root;
3131 struct btrfs_item *item;
3132 struct btrfs_dir_item *di;
3133 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04003134 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04003135 struct btrfs_path *path;
3136 int ret;
3137 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04003138 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003139 int slot;
3140 int advance;
3141 unsigned char d_type;
3142 int over = 0;
3143 u32 di_cur;
3144 u32 di_total;
3145 u32 di_len;
3146 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04003147 char tmp_name[32];
3148 char *name_ptr;
3149 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04003150
3151 /* FIXME, use a real flag for deciding about the key type */
3152 if (root->fs_info->tree_root == root)
3153 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04003154
Chris Mason39544012007-12-12 14:38:19 -05003155 /* special case for "." */
3156 if (filp->f_pos == 0) {
3157 over = filldir(dirent, ".", 1,
3158 1, inode->i_ino,
3159 DT_DIR);
3160 if (over)
3161 return 0;
3162 filp->f_pos = 1;
3163 }
Chris Mason39544012007-12-12 14:38:19 -05003164 /* special case for .., just use the back ref */
3165 if (filp->f_pos == 1) {
David Woodhouse5ecc7e52008-08-17 15:14:48 +01003166 u64 pino = parent_ino(filp->f_path.dentry);
Chris Mason39544012007-12-12 14:38:19 -05003167 over = filldir(dirent, "..", 2,
David Woodhouse5ecc7e52008-08-17 15:14:48 +01003168 2, pino, DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05003169 if (over)
David Woodhouse49593bf2008-08-17 17:08:36 +01003170 return 0;
Chris Mason39544012007-12-12 14:38:19 -05003171 filp->f_pos = 2;
3172 }
David Woodhouse49593bf2008-08-17 17:08:36 +01003173 path = btrfs_alloc_path();
3174 path->reada = 2;
3175
Chris Mason39279cc2007-06-12 06:35:45 -04003176 btrfs_set_key_type(&key, key_type);
3177 key.offset = filp->f_pos;
David Woodhouse49593bf2008-08-17 17:08:36 +01003178 key.objectid = inode->i_ino;
Chris Mason5f39d392007-10-15 16:14:19 -04003179
Chris Mason39279cc2007-06-12 06:35:45 -04003180 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3181 if (ret < 0)
3182 goto err;
3183 advance = 0;
David Woodhouse49593bf2008-08-17 17:08:36 +01003184
3185 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04003186 leaf = path->nodes[0];
3187 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003188 slot = path->slots[0];
3189 if (advance || slot >= nritems) {
David Woodhouse49593bf2008-08-17 17:08:36 +01003190 if (slot >= nritems - 1) {
Chris Mason39279cc2007-06-12 06:35:45 -04003191 ret = btrfs_next_leaf(root, path);
3192 if (ret)
3193 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003194 leaf = path->nodes[0];
3195 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003196 slot = path->slots[0];
3197 } else {
3198 slot++;
3199 path->slots[0]++;
3200 }
3201 }
Chris Mason3de45862008-11-17 21:02:50 -05003202
Chris Mason39279cc2007-06-12 06:35:45 -04003203 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04003204 item = btrfs_item_nr(leaf, slot);
3205 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3206
3207 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04003208 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003209 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003210 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003211 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04003212 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04003213
3214 filp->f_pos = found_key.offset;
David Woodhouse49593bf2008-08-17 17:08:36 +01003215
Chris Mason39279cc2007-06-12 06:35:45 -04003216 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
3217 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003218 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01003219
3220 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04003221 struct btrfs_key location;
3222
3223 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01003224 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04003225 name_ptr = tmp_name;
3226 } else {
3227 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01003228 if (!name_ptr) {
3229 ret = -ENOMEM;
3230 goto err;
3231 }
Chris Mason5f39d392007-10-15 16:14:19 -04003232 }
3233 read_extent_buffer(leaf, name_ptr,
3234 (unsigned long)(di + 1), name_len);
3235
3236 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
3237 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05003238
3239 /* is this a reference to our own snapshot? If so
3240 * skip it
3241 */
3242 if (location.type == BTRFS_ROOT_ITEM_KEY &&
3243 location.objectid == root->root_key.objectid) {
3244 over = 0;
3245 goto skip;
3246 }
Chris Mason5f39d392007-10-15 16:14:19 -04003247 over = filldir(dirent, name_ptr, name_len,
David Woodhouse49593bf2008-08-17 17:08:36 +01003248 found_key.offset, location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04003249 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04003250
Chris Mason3de45862008-11-17 21:02:50 -05003251skip:
Chris Mason5f39d392007-10-15 16:14:19 -04003252 if (name_ptr != tmp_name)
3253 kfree(name_ptr);
3254
Chris Mason39279cc2007-06-12 06:35:45 -04003255 if (over)
3256 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05003257 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01003258 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04003259 di_cur += di_len;
3260 di = (struct btrfs_dir_item *)((char *)di + di_len);
3261 }
3262 }
David Woodhouse49593bf2008-08-17 17:08:36 +01003263
3264 /* Reached end of directory/root. Bump pos past the last item. */
Yan Zheng5e591a02008-02-19 11:41:02 -05003265 if (key_type == BTRFS_DIR_INDEX_KEY)
Chris Mason89f135d2009-01-28 15:34:27 -05003266 filp->f_pos = INT_LIMIT(off_t);
Yan Zheng5e591a02008-02-19 11:41:02 -05003267 else
3268 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04003269nopos:
3270 ret = 0;
3271err:
Chris Mason39279cc2007-06-12 06:35:45 -04003272 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003273 return ret;
3274}
3275
3276int btrfs_write_inode(struct inode *inode, int wait)
3277{
3278 struct btrfs_root *root = BTRFS_I(inode)->root;
3279 struct btrfs_trans_handle *trans;
3280 int ret = 0;
3281
Yan Zhengc146afa2008-11-12 14:34:12 -05003282 if (root->fs_info->btree_inode == inode)
Chris Mason4ca8b412008-08-05 13:30:48 -04003283 return 0;
3284
Chris Mason39279cc2007-06-12 06:35:45 -04003285 if (wait) {
Chris Masonf9295742008-07-17 12:54:14 -04003286 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003287 btrfs_set_trans_block_group(trans, inode);
3288 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04003289 }
3290 return ret;
3291}
3292
3293/*
Chris Mason54aa1f42007-06-22 14:16:25 -04003294 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04003295 * inode changes. But, it is most likely to find the inode in cache.
3296 * FIXME, needs more benchmarking...there are no reasons other than performance
3297 * to keep or drop this code.
3298 */
3299void btrfs_dirty_inode(struct inode *inode)
3300{
3301 struct btrfs_root *root = BTRFS_I(inode)->root;
3302 struct btrfs_trans_handle *trans;
3303
Chris Masonf9295742008-07-17 12:54:14 -04003304 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003305 btrfs_set_trans_block_group(trans, inode);
3306 btrfs_update_inode(trans, root, inode);
3307 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04003308}
3309
Chris Masond352ac62008-09-29 15:18:18 -04003310/*
3311 * find the highest existing sequence number in a directory
3312 * and then set the in-memory index_cnt variable to reflect
3313 * free sequence numbers
3314 */
Josef Bacikaec74772008-07-24 12:12:38 -04003315static int btrfs_set_inode_index_count(struct inode *inode)
3316{
3317 struct btrfs_root *root = BTRFS_I(inode)->root;
3318 struct btrfs_key key, found_key;
3319 struct btrfs_path *path;
3320 struct extent_buffer *leaf;
3321 int ret;
3322
3323 key.objectid = inode->i_ino;
3324 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
3325 key.offset = (u64)-1;
3326
3327 path = btrfs_alloc_path();
3328 if (!path)
3329 return -ENOMEM;
3330
3331 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3332 if (ret < 0)
3333 goto out;
3334 /* FIXME: we should be able to handle this */
3335 if (ret == 0)
3336 goto out;
3337 ret = 0;
3338
3339 /*
3340 * MAGIC NUMBER EXPLANATION:
3341 * since we search a directory based on f_pos we have to start at 2
3342 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
3343 * else has to start at 2
3344 */
3345 if (path->slots[0] == 0) {
3346 BTRFS_I(inode)->index_cnt = 2;
3347 goto out;
3348 }
3349
3350 path->slots[0]--;
3351
3352 leaf = path->nodes[0];
3353 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3354
3355 if (found_key.objectid != inode->i_ino ||
3356 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
3357 BTRFS_I(inode)->index_cnt = 2;
3358 goto out;
3359 }
3360
3361 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
3362out:
3363 btrfs_free_path(path);
3364 return ret;
3365}
3366
Chris Masond352ac62008-09-29 15:18:18 -04003367/*
3368 * helper to find a free sequence number in a given directory. This current
3369 * code is very simple, later versions will do smarter things in the btree
3370 */
Chris Mason3de45862008-11-17 21:02:50 -05003371int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04003372{
3373 int ret = 0;
3374
3375 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
3376 ret = btrfs_set_inode_index_count(dir);
Chris Masond3977122009-01-05 21:25:51 -05003377 if (ret)
Josef Bacikaec74772008-07-24 12:12:38 -04003378 return ret;
3379 }
3380
Chris Mason00e4e6b2008-08-05 11:18:09 -04003381 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04003382 BTRFS_I(dir)->index_cnt++;
3383
3384 return ret;
3385}
3386
Chris Mason39279cc2007-06-12 06:35:45 -04003387static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
3388 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04003389 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05003390 const char *name, int name_len,
Yan Zhengd2fb3432008-12-11 16:30:39 -05003391 u64 ref_objectid, u64 objectid,
3392 u64 alloc_hint, int mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04003393{
3394 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04003395 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04003396 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04003397 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05003398 struct btrfs_inode_ref *ref;
3399 struct btrfs_key key[2];
3400 u32 sizes[2];
3401 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04003402 int ret;
3403 int owner;
3404
Chris Mason5f39d392007-10-15 16:14:19 -04003405 path = btrfs_alloc_path();
3406 BUG_ON(!path);
3407
Chris Mason39279cc2007-06-12 06:35:45 -04003408 inode = new_inode(root->fs_info->sb);
3409 if (!inode)
3410 return ERR_PTR(-ENOMEM);
3411
Josef Bacikaec74772008-07-24 12:12:38 -04003412 if (dir) {
Chris Mason3de45862008-11-17 21:02:50 -05003413 ret = btrfs_set_inode_index(dir, index);
Josef Bacikaec74772008-07-24 12:12:38 -04003414 if (ret)
3415 return ERR_PTR(ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003416 }
3417 /*
3418 * index_cnt is ignored for everything but a dir,
3419 * btrfs_get_inode_index_count has an explanation for the magic
3420 * number
3421 */
Chris Masone02119d2008-09-05 16:13:11 -04003422 init_btrfs_i(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04003423 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04003424 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04003425 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik6a632092009-02-20 11:00:09 -05003426 btrfs_set_inode_space_info(root, inode);
Chris Masonb888db22007-08-27 16:49:44 -04003427
Chris Mason39279cc2007-06-12 06:35:45 -04003428 if (mode & S_IFDIR)
3429 owner = 0;
3430 else
3431 owner = 1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003432 BTRFS_I(inode)->block_group =
3433 btrfs_find_block_group(root, 0, alloc_hint, owner);
Yan Zheng17d217f2008-12-12 10:03:38 -05003434 if ((mode & S_IFREG)) {
3435 if (btrfs_test_opt(root, NODATASUM))
3436 btrfs_set_flag(inode, NODATASUM);
3437 if (btrfs_test_opt(root, NODATACOW))
3438 btrfs_set_flag(inode, NODATACOW);
3439 }
Chris Mason9c583092008-01-29 15:15:18 -05003440
3441 key[0].objectid = objectid;
3442 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
3443 key[0].offset = 0;
3444
3445 key[1].objectid = objectid;
3446 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
3447 key[1].offset = ref_objectid;
3448
3449 sizes[0] = sizeof(struct btrfs_inode_item);
3450 sizes[1] = name_len + sizeof(*ref);
3451
3452 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
3453 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04003454 goto fail;
3455
Chris Mason9c583092008-01-29 15:15:18 -05003456 if (objectid > root->highest_inode)
3457 root->highest_inode = objectid;
3458
Chris Mason79683f22008-11-19 22:00:53 -05003459 inode->i_uid = current_fsuid();
Chris Ball8c087b52009-02-04 09:29:54 -05003460
Chris Mason42f15d72009-02-06 11:35:57 -05003461 if (dir && (dir->i_mode & S_ISGID)) {
Chris Ball8c087b52009-02-04 09:29:54 -05003462 inode->i_gid = dir->i_gid;
3463 if (S_ISDIR(mode))
3464 mode |= S_ISGID;
3465 } else
3466 inode->i_gid = current_fsgid();
3467
Chris Mason39279cc2007-06-12 06:35:45 -04003468 inode->i_mode = mode;
3469 inode->i_ino = objectid;
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003470 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003471 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04003472 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3473 struct btrfs_inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003474 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05003475
3476 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
3477 struct btrfs_inode_ref);
3478 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04003479 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05003480 ptr = (unsigned long)(ref + 1);
3481 write_extent_buffer(path->nodes[0], name, ptr, name_len);
3482
Chris Mason5f39d392007-10-15 16:14:19 -04003483 btrfs_mark_buffer_dirty(path->nodes[0]);
3484 btrfs_free_path(path);
3485
Chris Mason39279cc2007-06-12 06:35:45 -04003486 location = &BTRFS_I(inode)->location;
3487 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04003488 location->offset = 0;
3489 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
3490
Chris Mason39279cc2007-06-12 06:35:45 -04003491 insert_inode_hash(inode);
3492 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04003493fail:
Josef Bacikaec74772008-07-24 12:12:38 -04003494 if (dir)
3495 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04003496 btrfs_free_path(path);
3497 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003498}
3499
3500static inline u8 btrfs_inode_type(struct inode *inode)
3501{
3502 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
3503}
3504
Chris Masond352ac62008-09-29 15:18:18 -04003505/*
3506 * utility function to add 'inode' into 'parent_inode' with
3507 * a give name and a given sequence number.
3508 * if 'add_backref' is true, also insert a backref from the
3509 * inode to the parent directory.
3510 */
Chris Masone02119d2008-09-05 16:13:11 -04003511int btrfs_add_link(struct btrfs_trans_handle *trans,
3512 struct inode *parent_inode, struct inode *inode,
3513 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04003514{
3515 int ret;
3516 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04003517 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Chris Mason5f39d392007-10-15 16:14:19 -04003518
Chris Mason39279cc2007-06-12 06:35:45 -04003519 key.objectid = inode->i_ino;
Chris Mason39279cc2007-06-12 06:35:45 -04003520 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
3521 key.offset = 0;
3522
Chris Masone02119d2008-09-05 16:13:11 -04003523 ret = btrfs_insert_dir_item(trans, root, name, name_len,
3524 parent_inode->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04003525 &key, btrfs_inode_type(inode),
Chris Mason00e4e6b2008-08-05 11:18:09 -04003526 index);
Chris Mason39279cc2007-06-12 06:35:45 -04003527 if (ret == 0) {
Chris Mason9c583092008-01-29 15:15:18 -05003528 if (add_backref) {
3529 ret = btrfs_insert_inode_ref(trans, root,
Chris Masone02119d2008-09-05 16:13:11 -04003530 name, name_len,
3531 inode->i_ino,
3532 parent_inode->i_ino,
3533 index);
Chris Mason9c583092008-01-29 15:15:18 -05003534 }
Chris Masondbe674a2008-07-17 12:54:05 -04003535 btrfs_i_size_write(parent_inode, parent_inode->i_size +
Chris Masone02119d2008-09-05 16:13:11 -04003536 name_len * 2);
Chris Mason79c44582007-06-25 10:09:33 -04003537 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Masone02119d2008-09-05 16:13:11 -04003538 ret = btrfs_update_inode(trans, root, parent_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003539 }
3540 return ret;
3541}
3542
3543static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05003544 struct dentry *dentry, struct inode *inode,
Chris Mason00e4e6b2008-08-05 11:18:09 -04003545 int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04003546{
Chris Masone02119d2008-09-05 16:13:11 -04003547 int err = btrfs_add_link(trans, dentry->d_parent->d_inode,
3548 inode, dentry->d_name.name,
3549 dentry->d_name.len, backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04003550 if (!err) {
3551 d_instantiate(dentry, inode);
3552 return 0;
3553 }
3554 if (err > 0)
3555 err = -EEXIST;
3556 return err;
3557}
3558
Josef Bacik618e21d2007-07-11 10:18:17 -04003559static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
3560 int mode, dev_t rdev)
3561{
3562 struct btrfs_trans_handle *trans;
3563 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05003564 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04003565 int err;
3566 int drop_inode = 0;
3567 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05003568 unsigned long nr = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04003569 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04003570
3571 if (!new_valid_dev(rdev))
3572 return -EINVAL;
3573
Josef Bacik6a632092009-02-20 11:00:09 -05003574 err = btrfs_check_metadata_free_space(root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003575 if (err)
3576 goto fail;
3577
Josef Bacik618e21d2007-07-11 10:18:17 -04003578 trans = btrfs_start_transaction(root, 1);
3579 btrfs_set_trans_block_group(trans, dir);
3580
3581 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3582 if (err) {
3583 err = -ENOSPC;
3584 goto out_unlock;
3585 }
3586
Josef Bacikaec74772008-07-24 12:12:38 -04003587 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05003588 dentry->d_name.len,
3589 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04003590 BTRFS_I(dir)->block_group, mode, &index);
Josef Bacik618e21d2007-07-11 10:18:17 -04003591 err = PTR_ERR(inode);
3592 if (IS_ERR(inode))
3593 goto out_unlock;
3594
Jim Owens0279b4c2009-02-04 09:29:13 -05003595 err = btrfs_init_inode_security(inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04003596 if (err) {
3597 drop_inode = 1;
3598 goto out_unlock;
3599 }
3600
Josef Bacik618e21d2007-07-11 10:18:17 -04003601 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04003602 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04003603 if (err)
3604 drop_inode = 1;
3605 else {
3606 inode->i_op = &btrfs_special_inode_operations;
3607 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04003608 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04003609 }
3610 dir->i_sb->s_dirt = 1;
3611 btrfs_update_inode_block_group(trans, inode);
3612 btrfs_update_inode_block_group(trans, dir);
3613out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003614 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04003615 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003616fail:
Josef Bacik618e21d2007-07-11 10:18:17 -04003617 if (drop_inode) {
3618 inode_dec_link_count(inode);
3619 iput(inode);
3620 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003621 btrfs_btree_balance_dirty(root, nr);
Josef Bacik618e21d2007-07-11 10:18:17 -04003622 return err;
3623}
3624
Chris Mason39279cc2007-06-12 06:35:45 -04003625static int btrfs_create(struct inode *dir, struct dentry *dentry,
3626 int mode, struct nameidata *nd)
3627{
3628 struct btrfs_trans_handle *trans;
3629 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05003630 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04003631 int err;
3632 int drop_inode = 0;
Chris Mason1832a6d2007-12-21 16:27:21 -05003633 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003634 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04003635 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003636
Josef Bacik6a632092009-02-20 11:00:09 -05003637 err = btrfs_check_metadata_free_space(root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003638 if (err)
3639 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04003640 trans = btrfs_start_transaction(root, 1);
3641 btrfs_set_trans_block_group(trans, dir);
3642
3643 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3644 if (err) {
3645 err = -ENOSPC;
3646 goto out_unlock;
3647 }
3648
Josef Bacikaec74772008-07-24 12:12:38 -04003649 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05003650 dentry->d_name.len,
3651 dentry->d_parent->d_inode->i_ino,
Chris Mason00e4e6b2008-08-05 11:18:09 -04003652 objectid, BTRFS_I(dir)->block_group, mode,
3653 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04003654 err = PTR_ERR(inode);
3655 if (IS_ERR(inode))
3656 goto out_unlock;
3657
Jim Owens0279b4c2009-02-04 09:29:13 -05003658 err = btrfs_init_inode_security(inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04003659 if (err) {
3660 drop_inode = 1;
3661 goto out_unlock;
3662 }
3663
Chris Mason39279cc2007-06-12 06:35:45 -04003664 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04003665 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04003666 if (err)
3667 drop_inode = 1;
3668 else {
3669 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003670 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003671 inode->i_fop = &btrfs_file_operations;
3672 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05003673 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003674 }
3675 dir->i_sb->s_dirt = 1;
3676 btrfs_update_inode_block_group(trans, inode);
3677 btrfs_update_inode_block_group(trans, dir);
3678out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003679 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04003680 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003681fail:
Chris Mason39279cc2007-06-12 06:35:45 -04003682 if (drop_inode) {
3683 inode_dec_link_count(inode);
3684 iput(inode);
3685 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003686 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003687 return err;
3688}
3689
3690static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
3691 struct dentry *dentry)
3692{
3693 struct btrfs_trans_handle *trans;
3694 struct btrfs_root *root = BTRFS_I(dir)->root;
3695 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04003696 u64 index;
Chris Mason1832a6d2007-12-21 16:27:21 -05003697 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003698 int err;
3699 int drop_inode = 0;
3700
3701 if (inode->i_nlink == 0)
3702 return -ENOENT;
3703
Chris Masone02119d2008-09-05 16:13:11 -04003704 btrfs_inc_nlink(inode);
Josef Bacik6a632092009-02-20 11:00:09 -05003705 err = btrfs_check_metadata_free_space(root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003706 if (err)
3707 goto fail;
Chris Mason3de45862008-11-17 21:02:50 -05003708 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003709 if (err)
3710 goto fail;
3711
Chris Mason39279cc2007-06-12 06:35:45 -04003712 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003713
Chris Mason39279cc2007-06-12 06:35:45 -04003714 btrfs_set_trans_block_group(trans, dir);
3715 atomic_inc(&inode->i_count);
Josef Bacikaec74772008-07-24 12:12:38 -04003716
Chris Mason00e4e6b2008-08-05 11:18:09 -04003717 err = btrfs_add_nondir(trans, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04003718
Chris Mason39279cc2007-06-12 06:35:45 -04003719 if (err)
3720 drop_inode = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04003721
Chris Mason39279cc2007-06-12 06:35:45 -04003722 dir->i_sb->s_dirt = 1;
3723 btrfs_update_inode_block_group(trans, dir);
Chris Mason54aa1f42007-06-22 14:16:25 -04003724 err = btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003725
Chris Mason54aa1f42007-06-22 14:16:25 -04003726 if (err)
3727 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003728
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003729 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04003730 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003731fail:
Chris Mason39279cc2007-06-12 06:35:45 -04003732 if (drop_inode) {
3733 inode_dec_link_count(inode);
3734 iput(inode);
3735 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003736 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003737 return err;
3738}
3739
Chris Mason39279cc2007-06-12 06:35:45 -04003740static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
3741{
Chris Masonb9d86662008-05-02 16:13:49 -04003742 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04003743 struct btrfs_trans_handle *trans;
3744 struct btrfs_root *root = BTRFS_I(dir)->root;
3745 int err = 0;
3746 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04003747 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04003748 u64 index = 0;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003749 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003750
Josef Bacik6a632092009-02-20 11:00:09 -05003751 err = btrfs_check_metadata_free_space(root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003752 if (err)
3753 goto out_unlock;
3754
Chris Mason39279cc2007-06-12 06:35:45 -04003755 trans = btrfs_start_transaction(root, 1);
3756 btrfs_set_trans_block_group(trans, dir);
Chris Mason5f39d392007-10-15 16:14:19 -04003757
Chris Mason39279cc2007-06-12 06:35:45 -04003758 if (IS_ERR(trans)) {
3759 err = PTR_ERR(trans);
3760 goto out_unlock;
3761 }
3762
3763 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3764 if (err) {
3765 err = -ENOSPC;
3766 goto out_unlock;
3767 }
3768
Josef Bacikaec74772008-07-24 12:12:38 -04003769 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05003770 dentry->d_name.len,
3771 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04003772 BTRFS_I(dir)->block_group, S_IFDIR | mode,
3773 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04003774 if (IS_ERR(inode)) {
3775 err = PTR_ERR(inode);
3776 goto out_fail;
3777 }
Chris Mason5f39d392007-10-15 16:14:19 -04003778
Chris Mason39279cc2007-06-12 06:35:45 -04003779 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04003780
Jim Owens0279b4c2009-02-04 09:29:13 -05003781 err = btrfs_init_inode_security(inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04003782 if (err)
3783 goto out_fail;
3784
Chris Mason39279cc2007-06-12 06:35:45 -04003785 inode->i_op = &btrfs_dir_inode_operations;
3786 inode->i_fop = &btrfs_dir_file_operations;
3787 btrfs_set_trans_block_group(trans, inode);
3788
Chris Masondbe674a2008-07-17 12:54:05 -04003789 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003790 err = btrfs_update_inode(trans, root, inode);
3791 if (err)
3792 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04003793
Chris Masone02119d2008-09-05 16:13:11 -04003794 err = btrfs_add_link(trans, dentry->d_parent->d_inode,
3795 inode, dentry->d_name.name,
3796 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04003797 if (err)
3798 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04003799
Chris Mason39279cc2007-06-12 06:35:45 -04003800 d_instantiate(dentry, inode);
3801 drop_on_err = 0;
3802 dir->i_sb->s_dirt = 1;
3803 btrfs_update_inode_block_group(trans, inode);
3804 btrfs_update_inode_block_group(trans, dir);
3805
3806out_fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003807 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04003808 btrfs_end_transaction_throttle(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04003809
Chris Mason39279cc2007-06-12 06:35:45 -04003810out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04003811 if (drop_on_err)
3812 iput(inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003813 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003814 return err;
3815}
3816
Chris Masond352ac62008-09-29 15:18:18 -04003817/* helper for btfs_get_extent. Given an existing extent in the tree,
3818 * and an extent that you want to insert, deal with overlap and insert
3819 * the new extent into the tree.
3820 */
Chris Mason3b951512008-04-17 11:29:12 -04003821static int merge_extent_mapping(struct extent_map_tree *em_tree,
3822 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003823 struct extent_map *em,
3824 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04003825{
3826 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04003827
Chris Masone6dcd2d2008-07-17 12:53:50 -04003828 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
3829 start_diff = map_start - em->start;
3830 em->start = map_start;
3831 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04003832 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
3833 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04003834 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04003835 em->block_len -= start_diff;
3836 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003837 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04003838}
3839
Chris Masonc8b97812008-10-29 14:49:59 -04003840static noinline int uncompress_inline(struct btrfs_path *path,
3841 struct inode *inode, struct page *page,
3842 size_t pg_offset, u64 extent_offset,
3843 struct btrfs_file_extent_item *item)
3844{
3845 int ret;
3846 struct extent_buffer *leaf = path->nodes[0];
3847 char *tmp;
3848 size_t max_size;
3849 unsigned long inline_size;
3850 unsigned long ptr;
3851
3852 WARN_ON(pg_offset != 0);
3853 max_size = btrfs_file_extent_ram_bytes(leaf, item);
3854 inline_size = btrfs_file_extent_inline_item_len(leaf,
3855 btrfs_item_nr(leaf, path->slots[0]));
3856 tmp = kmalloc(inline_size, GFP_NOFS);
3857 ptr = btrfs_file_extent_inline_start(item);
3858
3859 read_extent_buffer(leaf, tmp, ptr, inline_size);
3860
Chris Mason5b050f02008-11-11 09:34:41 -05003861 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04003862 ret = btrfs_zlib_decompress(tmp, page, extent_offset,
3863 inline_size, max_size);
3864 if (ret) {
3865 char *kaddr = kmap_atomic(page, KM_USER0);
3866 unsigned long copy_size = min_t(u64,
3867 PAGE_CACHE_SIZE - pg_offset,
3868 max_size - extent_offset);
3869 memset(kaddr + pg_offset, 0, copy_size);
3870 kunmap_atomic(kaddr, KM_USER0);
3871 }
3872 kfree(tmp);
3873 return 0;
3874}
3875
Chris Masond352ac62008-09-29 15:18:18 -04003876/*
3877 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05003878 * the ugly parts come from merging extents from the disk with the in-ram
3879 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04003880 * where the in-ram extents might be locked pending data=ordered completion.
3881 *
3882 * This also copies inline extents directly into the page.
3883 */
Chris Masond3977122009-01-05 21:25:51 -05003884
Chris Masona52d9a82007-08-27 16:49:44 -04003885struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05003886 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04003887 int create)
3888{
3889 int ret;
3890 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04003891 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04003892 u64 extent_start = 0;
3893 u64 extent_end = 0;
3894 u64 objectid = inode->i_ino;
3895 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04003896 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04003897 struct btrfs_root *root = BTRFS_I(inode)->root;
3898 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04003899 struct extent_buffer *leaf;
3900 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04003901 struct extent_map *em = NULL;
3902 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05003903 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04003904 struct btrfs_trans_handle *trans = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04003905 int compressed;
Chris Masona52d9a82007-08-27 16:49:44 -04003906
Chris Masona52d9a82007-08-27 16:49:44 -04003907again:
Chris Masond1310b22008-01-24 16:13:08 -05003908 spin_lock(&em_tree->lock);
3909 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04003910 if (em)
3911 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05003912 spin_unlock(&em_tree->lock);
3913
Chris Masona52d9a82007-08-27 16:49:44 -04003914 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04003915 if (em->start > start || em->start + em->len <= start)
3916 free_extent_map(em);
3917 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05003918 free_extent_map(em);
3919 else
3920 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04003921 }
Chris Masond1310b22008-01-24 16:13:08 -05003922 em = alloc_extent_map(GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04003923 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05003924 err = -ENOMEM;
3925 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04003926 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003927 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05003928 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05003929 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05003930 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04003931 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04003932
3933 if (!path) {
3934 path = btrfs_alloc_path();
3935 BUG_ON(!path);
3936 }
3937
Chris Mason179e29e2007-11-01 11:28:41 -04003938 ret = btrfs_lookup_file_extent(trans, root, path,
3939 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04003940 if (ret < 0) {
3941 err = ret;
3942 goto out;
3943 }
3944
3945 if (ret != 0) {
3946 if (path->slots[0] == 0)
3947 goto not_found;
3948 path->slots[0]--;
3949 }
3950
Chris Mason5f39d392007-10-15 16:14:19 -04003951 leaf = path->nodes[0];
3952 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04003953 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04003954 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04003955 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3956 found_type = btrfs_key_type(&found_key);
3957 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04003958 found_type != BTRFS_EXTENT_DATA_KEY) {
3959 goto not_found;
3960 }
3961
Chris Mason5f39d392007-10-15 16:14:19 -04003962 found_type = btrfs_file_extent_type(leaf, item);
3963 extent_start = found_key.offset;
Chris Masonc8b97812008-10-29 14:49:59 -04003964 compressed = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04003965 if (found_type == BTRFS_FILE_EXTENT_REG ||
3966 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04003967 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04003968 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04003969 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
3970 size_t size;
3971 size = btrfs_file_extent_inline_len(leaf, item);
3972 extent_end = (extent_start + size + root->sectorsize - 1) &
3973 ~((u64)root->sectorsize - 1);
3974 }
3975
3976 if (start >= extent_end) {
3977 path->slots[0]++;
3978 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
3979 ret = btrfs_next_leaf(root, path);
3980 if (ret < 0) {
3981 err = ret;
3982 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04003983 }
Yan Zheng9036c102008-10-30 14:19:41 -04003984 if (ret > 0)
3985 goto not_found;
3986 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04003987 }
Yan Zheng9036c102008-10-30 14:19:41 -04003988 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3989 if (found_key.objectid != objectid ||
3990 found_key.type != BTRFS_EXTENT_DATA_KEY)
3991 goto not_found;
3992 if (start + len <= found_key.offset)
3993 goto not_found;
3994 em->start = start;
3995 em->len = found_key.offset - start;
3996 goto not_found_em;
3997 }
3998
Yan Zhengd899e052008-10-30 14:25:28 -04003999 if (found_type == BTRFS_FILE_EXTENT_REG ||
4000 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04004001 em->start = extent_start;
4002 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05004003 em->orig_start = extent_start -
4004 btrfs_file_extent_offset(leaf, item);
Chris Masondb945352007-10-15 16:15:53 -04004005 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
4006 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004007 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04004008 goto insert;
4009 }
Chris Masonc8b97812008-10-29 14:49:59 -04004010 if (compressed) {
4011 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
4012 em->block_start = bytenr;
4013 em->block_len = btrfs_file_extent_disk_num_bytes(leaf,
4014 item);
4015 } else {
4016 bytenr += btrfs_file_extent_offset(leaf, item);
4017 em->block_start = bytenr;
4018 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04004019 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
4020 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04004021 }
Chris Masona52d9a82007-08-27 16:49:44 -04004022 goto insert;
4023 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004024 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04004025 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04004026 size_t size;
4027 size_t extent_offset;
4028 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04004029
Chris Masona52d9a82007-08-27 16:49:44 -04004030 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04004031 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04004032 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04004033 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04004034 goto out;
4035 }
4036
Yan Zheng9036c102008-10-30 14:19:41 -04004037 size = btrfs_file_extent_inline_len(leaf, item);
4038 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05004039 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04004040 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04004041 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05004042 em->len = (copy_size + root->sectorsize - 1) &
4043 ~((u64)root->sectorsize - 1);
Yan Zhengff5b7ee2008-11-10 07:34:43 -05004044 em->orig_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04004045 if (compressed)
4046 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Yan689f9342007-10-29 11:41:07 -04004047 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04004048 if (create == 0 && !PageUptodate(page)) {
Chris Masonc8b97812008-10-29 14:49:59 -04004049 if (btrfs_file_extent_compression(leaf, item) ==
4050 BTRFS_COMPRESS_ZLIB) {
4051 ret = uncompress_inline(path, inode, page,
4052 pg_offset,
4053 extent_offset, item);
4054 BUG_ON(ret);
4055 } else {
4056 map = kmap(page);
4057 read_extent_buffer(leaf, map + pg_offset, ptr,
4058 copy_size);
4059 kunmap(page);
4060 }
Chris Mason179e29e2007-11-01 11:28:41 -04004061 flush_dcache_page(page);
4062 } else if (create && PageUptodate(page)) {
4063 if (!trans) {
4064 kunmap(page);
4065 free_extent_map(em);
4066 em = NULL;
4067 btrfs_release_path(root, path);
Chris Masonf9295742008-07-17 12:54:14 -04004068 trans = btrfs_join_transaction(root, 1);
Chris Mason179e29e2007-11-01 11:28:41 -04004069 goto again;
4070 }
Chris Masonc8b97812008-10-29 14:49:59 -04004071 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05004072 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04004073 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04004074 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04004075 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04004076 }
Chris Masond1310b22008-01-24 16:13:08 -05004077 set_extent_uptodate(io_tree, em->start,
4078 extent_map_end(em) - 1, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04004079 goto insert;
4080 } else {
Chris Masond3977122009-01-05 21:25:51 -05004081 printk(KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04004082 WARN_ON(1);
4083 }
4084not_found:
4085 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05004086 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04004087not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04004088 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04004089 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04004090insert:
4091 btrfs_release_path(root, path);
Chris Masond1310b22008-01-24 16:13:08 -05004092 if (em->start > start || extent_map_end(em) <= start) {
Chris Masond3977122009-01-05 21:25:51 -05004093 printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
4094 "[%llu %llu]\n", (unsigned long long)em->start,
4095 (unsigned long long)em->len,
4096 (unsigned long long)start,
4097 (unsigned long long)len);
Chris Masona52d9a82007-08-27 16:49:44 -04004098 err = -EIO;
4099 goto out;
4100 }
Chris Masond1310b22008-01-24 16:13:08 -05004101
4102 err = 0;
4103 spin_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04004104 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04004105 /* it is possible that someone inserted the extent into the tree
4106 * while we had the lock dropped. It is also possible that
4107 * an overlapping map exists in the tree
4108 */
Chris Masona52d9a82007-08-27 16:49:44 -04004109 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04004110 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004111
4112 ret = 0;
4113
Chris Mason3b951512008-04-17 11:29:12 -04004114 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04004115 if (existing && (existing->start > start ||
4116 existing->start + existing->len <= start)) {
4117 free_extent_map(existing);
4118 existing = NULL;
4119 }
Chris Mason3b951512008-04-17 11:29:12 -04004120 if (!existing) {
4121 existing = lookup_extent_mapping(em_tree, em->start,
4122 em->len);
4123 if (existing) {
4124 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04004125 em, start,
4126 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04004127 free_extent_map(existing);
4128 if (err) {
4129 free_extent_map(em);
4130 em = NULL;
4131 }
4132 } else {
4133 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04004134 free_extent_map(em);
4135 em = NULL;
4136 }
4137 } else {
4138 free_extent_map(em);
4139 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004140 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04004141 }
Chris Masona52d9a82007-08-27 16:49:44 -04004142 }
Chris Masond1310b22008-01-24 16:13:08 -05004143 spin_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04004144out:
Chris Masonf4219502008-07-22 11:18:09 -04004145 if (path)
4146 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04004147 if (trans) {
4148 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05004149 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04004150 err = ret;
4151 }
Chris Masona52d9a82007-08-27 16:49:44 -04004152 if (err) {
4153 free_extent_map(em);
4154 WARN_ON(1);
4155 return ERR_PTR(err);
4156 }
4157 return em;
4158}
4159
Chris Mason16432982008-04-10 10:23:21 -04004160static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
4161 const struct iovec *iov, loff_t offset,
4162 unsigned long nr_segs)
4163{
Chris Masone1c4b742008-04-22 13:26:46 -04004164 return -EINVAL;
Chris Mason16432982008-04-10 10:23:21 -04004165}
4166
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004167static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
4168 __u64 start, __u64 len)
4169{
4170 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent);
4171}
4172
Chris Mason9ebefb182007-06-15 13:50:00 -04004173int btrfs_readpage(struct file *file, struct page *page)
4174{
Chris Masond1310b22008-01-24 16:13:08 -05004175 struct extent_io_tree *tree;
4176 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04004177 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04004178}
Chris Mason1832a6d2007-12-21 16:27:21 -05004179
Chris Mason39279cc2007-06-12 06:35:45 -04004180static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
4181{
Chris Masond1310b22008-01-24 16:13:08 -05004182 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04004183
4184
4185 if (current->flags & PF_MEMALLOC) {
4186 redirty_page_for_writepage(wbc, page);
4187 unlock_page(page);
4188 return 0;
4189 }
Chris Masond1310b22008-01-24 16:13:08 -05004190 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04004191 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
4192}
Chris Mason39279cc2007-06-12 06:35:45 -04004193
Chris Masonf4219502008-07-22 11:18:09 -04004194int btrfs_writepages(struct address_space *mapping,
4195 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04004196{
Chris Masond1310b22008-01-24 16:13:08 -05004197 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05004198
Chris Masond1310b22008-01-24 16:13:08 -05004199 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f02e2007-11-01 19:45:34 -04004200 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
4201}
4202
Chris Mason3ab2fb52007-11-08 10:59:22 -05004203static int
4204btrfs_readpages(struct file *file, struct address_space *mapping,
4205 struct list_head *pages, unsigned nr_pages)
4206{
Chris Masond1310b22008-01-24 16:13:08 -05004207 struct extent_io_tree *tree;
4208 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05004209 return extent_readpages(tree, mapping, pages, nr_pages,
4210 btrfs_get_extent);
4211}
Chris Masone6dcd2d2008-07-17 12:53:50 -04004212static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04004213{
Chris Masond1310b22008-01-24 16:13:08 -05004214 struct extent_io_tree *tree;
4215 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04004216 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004217
Chris Masond1310b22008-01-24 16:13:08 -05004218 tree = &BTRFS_I(page->mapping->host)->io_tree;
4219 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05004220 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04004221 if (ret == 1) {
4222 ClearPagePrivate(page);
4223 set_page_private(page, 0);
4224 page_cache_release(page);
4225 }
4226 return ret;
4227}
Chris Mason39279cc2007-06-12 06:35:45 -04004228
Chris Masone6dcd2d2008-07-17 12:53:50 -04004229static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
4230{
Chris Mason98509cf2008-09-11 15:51:43 -04004231 if (PageWriteback(page) || PageDirty(page))
4232 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05004233 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004234}
4235
Chris Masona52d9a82007-08-27 16:49:44 -04004236static void btrfs_invalidatepage(struct page *page, unsigned long offset)
4237{
Chris Masond1310b22008-01-24 16:13:08 -05004238 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004239 struct btrfs_ordered_extent *ordered;
4240 u64 page_start = page_offset(page);
4241 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04004242
Chris Masone6dcd2d2008-07-17 12:53:50 -04004243 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05004244 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004245 if (offset) {
4246 btrfs_releasepage(page, GFP_NOFS);
4247 return;
4248 }
4249
4250 lock_extent(tree, page_start, page_end, GFP_NOFS);
4251 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
4252 page_offset(page));
4253 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04004254 /*
4255 * IO on this page will never be started, so we need
4256 * to account for any ordered extents now
4257 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04004258 clear_extent_bit(tree, page_start, page_end,
4259 EXTENT_DIRTY | EXTENT_DELALLOC |
4260 EXTENT_LOCKED, 1, 0, GFP_NOFS);
Chris Mason211f90e2008-07-18 11:56:15 -04004261 btrfs_finish_ordered_io(page->mapping->host,
4262 page_start, page_end);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004263 btrfs_put_ordered_extent(ordered);
4264 lock_extent(tree, page_start, page_end, GFP_NOFS);
4265 }
4266 clear_extent_bit(tree, page_start, page_end,
4267 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
4268 EXTENT_ORDERED,
4269 1, 1, GFP_NOFS);
4270 __btrfs_releasepage(page, GFP_NOFS);
4271
Chris Mason4a096752008-07-21 10:29:44 -04004272 ClearPageChecked(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04004273 if (PagePrivate(page)) {
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04004274 ClearPagePrivate(page);
4275 set_page_private(page, 0);
4276 page_cache_release(page);
4277 }
Chris Mason39279cc2007-06-12 06:35:45 -04004278}
4279
Chris Mason9ebefb182007-06-15 13:50:00 -04004280/*
4281 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
4282 * called from a page fault handler when a page is first dirtied. Hence we must
4283 * be careful to check for EOF conditions here. We set the page up correctly
4284 * for a written page which means we get ENOSPC checking when writing into
4285 * holes and correct delalloc and unwritten extent mapping on filesystems that
4286 * support these features.
4287 *
4288 * We are not allowed to take the i_mutex here so we have to play games to
4289 * protect against truncate races as the page could now be beyond EOF. Because
4290 * vmtruncate() writes the inode size before removing pages, once we have the
4291 * page lock we can determine safely if the page is beyond EOF. If it is not
4292 * beyond EOF, then the page is guaranteed safe against truncation until we
4293 * unlock the page.
4294 */
4295int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
4296{
Chris Mason6da6aba2007-12-18 16:15:09 -05004297 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05004298 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004299 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4300 struct btrfs_ordered_extent *ordered;
4301 char *kaddr;
4302 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04004303 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05004304 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04004305 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004306 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04004307
Josef Bacik6a632092009-02-20 11:00:09 -05004308 ret = btrfs_check_data_free_space(root, inode, PAGE_CACHE_SIZE);
Chris Mason1832a6d2007-12-21 16:27:21 -05004309 if (ret)
4310 goto out;
4311
4312 ret = -EINVAL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004313again:
Chris Mason9ebefb182007-06-15 13:50:00 -04004314 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04004315 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004316 page_start = page_offset(page);
4317 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04004318
Chris Mason9ebefb182007-06-15 13:50:00 -04004319 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04004320 (page_start >= size)) {
Josef Bacik6a632092009-02-20 11:00:09 -05004321 btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
Chris Mason9ebefb182007-06-15 13:50:00 -04004322 /* page got truncated out from underneath us */
4323 goto out_unlock;
4324 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004325 wait_on_page_writeback(page);
4326
4327 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
4328 set_page_extent_mapped(page);
4329
Chris Masoneb84ae02008-07-17 13:53:27 -04004330 /*
4331 * we can't set the delalloc bits if there are pending ordered
4332 * extents. Drop our locks and wait for them to finish
4333 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04004334 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4335 if (ordered) {
4336 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
4337 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004338 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004339 btrfs_put_ordered_extent(ordered);
4340 goto again;
4341 }
4342
Chris Masonea8c2812008-08-04 23:17:27 -04004343 btrfs_set_extent_delalloc(inode, page_start, page_end);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004344 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04004345
4346 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04004347 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04004348 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04004349 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04004350 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04004351
Chris Masone6dcd2d2008-07-17 12:53:50 -04004352 if (zero_start != PAGE_CACHE_SIZE) {
4353 kaddr = kmap(page);
4354 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
4355 flush_dcache_page(page);
4356 kunmap(page);
4357 }
Chris Mason247e7432008-07-17 12:53:51 -04004358 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004359 set_page_dirty(page);
4360 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04004361
4362out_unlock:
4363 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05004364out:
Chris Mason9ebefb182007-06-15 13:50:00 -04004365 return ret;
4366}
4367
Chris Mason39279cc2007-06-12 06:35:45 -04004368static void btrfs_truncate(struct inode *inode)
4369{
4370 struct btrfs_root *root = BTRFS_I(inode)->root;
4371 int ret;
4372 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004373 unsigned long nr;
Chris Masondbe674a2008-07-17 12:54:05 -04004374 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04004375
4376 if (!S_ISREG(inode->i_mode))
4377 return;
4378 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
4379 return;
4380
4381 btrfs_truncate_page(inode->i_mapping, inode->i_size);
Chris Mason4a096752008-07-21 10:29:44 -04004382 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04004383
Chris Mason39279cc2007-06-12 06:35:45 -04004384 trans = btrfs_start_transaction(root, 1);
4385 btrfs_set_trans_block_group(trans, inode);
Chris Masondbe674a2008-07-17 12:54:05 -04004386 btrfs_i_size_write(inode, inode->i_size);
Chris Mason39279cc2007-06-12 06:35:45 -04004387
Josef Bacik7b128762008-07-24 12:17:14 -04004388 ret = btrfs_orphan_add(trans, inode);
4389 if (ret)
4390 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04004391 /* FIXME, add redo link to tree so we don't leak on crash */
Chris Masone02119d2008-09-05 16:13:11 -04004392 ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size,
Chris Mason85e21ba2008-01-29 15:11:36 -05004393 BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004394 btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004395
Josef Bacik7b128762008-07-24 12:17:14 -04004396 ret = btrfs_orphan_del(trans, inode);
4397 BUG_ON(ret);
4398
4399out:
4400 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04004401 ret = btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004402 BUG_ON(ret);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004403 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004404}
4405
Sven Wegener3b963622008-06-09 21:57:42 -04004406/*
Chris Masond352ac62008-09-29 15:18:18 -04004407 * create a new subvolume directory/inode (helper for the ioctl).
4408 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05004409int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
4410 struct btrfs_root *new_root, struct dentry *dentry,
4411 u64 new_dirid, u64 alloc_hint)
Chris Mason39279cc2007-06-12 06:35:45 -04004412{
Chris Mason39279cc2007-06-12 06:35:45 -04004413 struct inode *inode;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04004414 int error;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004415 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004416
Josef Bacikaec74772008-07-24 12:12:38 -04004417 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
Yan Zhengd2fb3432008-12-11 16:30:39 -05004418 new_dirid, alloc_hint, S_IFDIR | 0700, &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04004419 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004420 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004421 inode->i_op = &btrfs_dir_inode_operations;
4422 inode->i_fop = &btrfs_dir_file_operations;
4423
Chris Mason39279cc2007-06-12 06:35:45 -04004424 inode->i_nlink = 1;
Chris Masondbe674a2008-07-17 12:54:05 -04004425 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04004426
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04004427 error = btrfs_update_inode(trans, new_root, inode);
4428 if (error)
4429 return error;
4430
4431 d_instantiate(dentry, inode);
4432 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004433}
4434
Chris Masond352ac62008-09-29 15:18:18 -04004435/* helper function for file defrag and space balancing. This
4436 * forces readahead on a given range of bytes in an inode
4437 */
Chris Masonedbd8d42007-12-21 16:27:24 -05004438unsigned long btrfs_force_ra(struct address_space *mapping,
Chris Mason86479a02007-09-10 19:58:16 -04004439 struct file_ra_state *ra, struct file *file,
4440 pgoff_t offset, pgoff_t last_index)
4441{
Chris Mason8e7bf942008-04-28 09:02:36 -04004442 pgoff_t req_size = last_index - offset + 1;
Chris Mason86479a02007-09-10 19:58:16 -04004443
Chris Mason86479a02007-09-10 19:58:16 -04004444 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
4445 return offset + req_size;
Chris Mason86479a02007-09-10 19:58:16 -04004446}
4447
Chris Mason39279cc2007-06-12 06:35:45 -04004448struct inode *btrfs_alloc_inode(struct super_block *sb)
4449{
4450 struct btrfs_inode *ei;
4451
4452 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
4453 if (!ei)
4454 return NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04004455 ei->last_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004456 ei->logged_trans = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004457 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Josef Bacik33268ea2008-07-24 12:16:36 -04004458 ei->i_acl = BTRFS_ACL_NOT_CACHED;
4459 ei->i_default_acl = BTRFS_ACL_NOT_CACHED;
Josef Bacik7b128762008-07-24 12:17:14 -04004460 INIT_LIST_HEAD(&ei->i_orphan);
Chris Mason39279cc2007-06-12 06:35:45 -04004461 return &ei->vfs_inode;
4462}
4463
4464void btrfs_destroy_inode(struct inode *inode)
4465{
Chris Masone6dcd2d2008-07-17 12:53:50 -04004466 struct btrfs_ordered_extent *ordered;
Chris Mason39279cc2007-06-12 06:35:45 -04004467 WARN_ON(!list_empty(&inode->i_dentry));
4468 WARN_ON(inode->i_data.nrpages);
4469
Josef Bacik33268ea2008-07-24 12:16:36 -04004470 if (BTRFS_I(inode)->i_acl &&
4471 BTRFS_I(inode)->i_acl != BTRFS_ACL_NOT_CACHED)
4472 posix_acl_release(BTRFS_I(inode)->i_acl);
4473 if (BTRFS_I(inode)->i_default_acl &&
4474 BTRFS_I(inode)->i_default_acl != BTRFS_ACL_NOT_CACHED)
4475 posix_acl_release(BTRFS_I(inode)->i_default_acl);
4476
Yanbcc63ab2008-07-30 16:29:20 -04004477 spin_lock(&BTRFS_I(inode)->root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04004478 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
4479 printk(KERN_ERR "BTRFS: inode %lu: inode still on the orphan"
4480 " list\n", inode->i_ino);
4481 dump_stack();
4482 }
Yanbcc63ab2008-07-30 16:29:20 -04004483 spin_unlock(&BTRFS_I(inode)->root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04004484
Chris Masond3977122009-01-05 21:25:51 -05004485 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04004486 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
4487 if (!ordered)
4488 break;
4489 else {
Chris Masond3977122009-01-05 21:25:51 -05004490 printk(KERN_ERR "btrfs found ordered "
4491 "extent %llu %llu on inode cleanup\n",
4492 (unsigned long long)ordered->file_offset,
4493 (unsigned long long)ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004494 btrfs_remove_ordered_extent(inode, ordered);
4495 btrfs_put_ordered_extent(ordered);
4496 btrfs_put_ordered_extent(ordered);
4497 }
4498 }
Zheng Yan5b21f2e2008-09-26 10:05:38 -04004499 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004500 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
4501}
4502
Sven Wegener0ee0fda2008-07-30 16:54:26 -04004503static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04004504{
4505 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
4506
4507 inode_init_once(&ei->vfs_inode);
4508}
4509
4510void btrfs_destroy_cachep(void)
4511{
4512 if (btrfs_inode_cachep)
4513 kmem_cache_destroy(btrfs_inode_cachep);
4514 if (btrfs_trans_handle_cachep)
4515 kmem_cache_destroy(btrfs_trans_handle_cachep);
4516 if (btrfs_transaction_cachep)
4517 kmem_cache_destroy(btrfs_transaction_cachep);
4518 if (btrfs_bit_radix_cachep)
4519 kmem_cache_destroy(btrfs_bit_radix_cachep);
4520 if (btrfs_path_cachep)
4521 kmem_cache_destroy(btrfs_path_cachep);
4522}
4523
Chris Mason86479a02007-09-10 19:58:16 -04004524struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
Chris Mason92fee662007-07-25 12:31:35 -04004525 unsigned long extra_flags,
Chris Mason2b1f55b2008-09-24 11:48:04 -04004526 void (*ctor)(void *))
Chris Mason92fee662007-07-25 12:31:35 -04004527{
4528 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
Chris Mason2b1f55b2008-09-24 11:48:04 -04004529 SLAB_MEM_SPREAD | extra_flags), ctor);
Chris Mason92fee662007-07-25 12:31:35 -04004530}
4531
Chris Mason39279cc2007-06-12 06:35:45 -04004532int btrfs_init_cachep(void)
4533{
Chris Mason86479a02007-09-10 19:58:16 -04004534 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
Chris Mason92fee662007-07-25 12:31:35 -04004535 sizeof(struct btrfs_inode),
4536 0, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04004537 if (!btrfs_inode_cachep)
4538 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04004539 btrfs_trans_handle_cachep =
4540 btrfs_cache_create("btrfs_trans_handle_cache",
4541 sizeof(struct btrfs_trans_handle),
4542 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004543 if (!btrfs_trans_handle_cachep)
4544 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04004545 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
Chris Mason39279cc2007-06-12 06:35:45 -04004546 sizeof(struct btrfs_transaction),
Chris Mason92fee662007-07-25 12:31:35 -04004547 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004548 if (!btrfs_transaction_cachep)
4549 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04004550 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
Yan23223582007-09-17 11:08:52 -04004551 sizeof(struct btrfs_path),
Chris Mason92fee662007-07-25 12:31:35 -04004552 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004553 if (!btrfs_path_cachep)
4554 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04004555 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
Chris Mason92fee662007-07-25 12:31:35 -04004556 SLAB_DESTROY_BY_RCU, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004557 if (!btrfs_bit_radix_cachep)
4558 goto fail;
4559 return 0;
4560fail:
4561 btrfs_destroy_cachep();
4562 return -ENOMEM;
4563}
4564
4565static int btrfs_getattr(struct vfsmount *mnt,
4566 struct dentry *dentry, struct kstat *stat)
4567{
4568 struct inode *inode = dentry->d_inode;
4569 generic_fillattr(inode, stat);
Chris Mason3394e162008-11-17 20:42:26 -05004570 stat->dev = BTRFS_I(inode)->root->anon_super.s_dev;
Chris Masond6667462008-01-03 14:51:00 -05004571 stat->blksize = PAGE_CACHE_SIZE;
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004572 stat->blocks = (inode_get_bytes(inode) +
4573 BTRFS_I(inode)->delalloc_bytes) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04004574 return 0;
4575}
4576
Chris Masond3977122009-01-05 21:25:51 -05004577static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
4578 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04004579{
4580 struct btrfs_trans_handle *trans;
4581 struct btrfs_root *root = BTRFS_I(old_dir)->root;
4582 struct inode *new_inode = new_dentry->d_inode;
4583 struct inode *old_inode = old_dentry->d_inode;
4584 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004585 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004586 int ret;
4587
Chris Mason3394e162008-11-17 20:42:26 -05004588 /* we're not allowed to rename between subvolumes */
4589 if (BTRFS_I(old_inode)->root->root_key.objectid !=
4590 BTRFS_I(new_dir)->root->root_key.objectid)
4591 return -EXDEV;
4592
Chris Mason39279cc2007-06-12 06:35:45 -04004593 if (S_ISDIR(old_inode->i_mode) && new_inode &&
4594 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
4595 return -ENOTEMPTY;
4596 }
Chris Mason5f39d392007-10-15 16:14:19 -04004597
Chris Mason0660b5a2008-11-17 20:37:39 -05004598 /* to rename a snapshot or subvolume, we need to juggle the
4599 * backrefs. This isn't coded yet
4600 */
4601 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
4602 return -EXDEV;
4603
Josef Bacik6a632092009-02-20 11:00:09 -05004604 ret = btrfs_check_metadata_free_space(root);
Chris Mason1832a6d2007-12-21 16:27:21 -05004605 if (ret)
4606 goto out_unlock;
4607
Chris Mason39279cc2007-06-12 06:35:45 -04004608 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04004609
Chris Mason39279cc2007-06-12 06:35:45 -04004610 btrfs_set_trans_block_group(trans, new_dir);
Chris Mason39279cc2007-06-12 06:35:45 -04004611
Chris Masone02119d2008-09-05 16:13:11 -04004612 btrfs_inc_nlink(old_dentry->d_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004613 old_dir->i_ctime = old_dir->i_mtime = ctime;
4614 new_dir->i_ctime = new_dir->i_mtime = ctime;
4615 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04004616
Chris Masone02119d2008-09-05 16:13:11 -04004617 ret = btrfs_unlink_inode(trans, root, old_dir, old_dentry->d_inode,
4618 old_dentry->d_name.name,
4619 old_dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04004620 if (ret)
4621 goto out_fail;
4622
4623 if (new_inode) {
4624 new_inode->i_ctime = CURRENT_TIME;
Chris Masone02119d2008-09-05 16:13:11 -04004625 ret = btrfs_unlink_inode(trans, root, new_dir,
4626 new_dentry->d_inode,
4627 new_dentry->d_name.name,
4628 new_dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04004629 if (ret)
4630 goto out_fail;
Josef Bacik7b128762008-07-24 12:17:14 -04004631 if (new_inode->i_nlink == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004632 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Josef Bacik7b128762008-07-24 12:17:14 -04004633 if (ret)
4634 goto out_fail;
4635 }
Chris Masone02119d2008-09-05 16:13:11 -04004636
Chris Mason39279cc2007-06-12 06:35:45 -04004637 }
Chris Mason3de45862008-11-17 21:02:50 -05004638 ret = btrfs_set_inode_index(new_dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004639 if (ret)
4640 goto out_fail;
4641
Chris Masone02119d2008-09-05 16:13:11 -04004642 ret = btrfs_add_link(trans, new_dentry->d_parent->d_inode,
4643 old_inode, new_dentry->d_name.name,
4644 new_dentry->d_name.len, 1, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004645 if (ret)
4646 goto out_fail;
4647
4648out_fail:
Chris Masonab78c842008-07-29 16:15:18 -04004649 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05004650out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04004651 return ret;
4652}
4653
Chris Masond352ac62008-09-29 15:18:18 -04004654/*
4655 * some fairly slow code that needs optimization. This walks the list
4656 * of all the inodes with pending delalloc and forces them to disk.
4657 */
Chris Masonea8c2812008-08-04 23:17:27 -04004658int btrfs_start_delalloc_inodes(struct btrfs_root *root)
4659{
4660 struct list_head *head = &root->fs_info->delalloc_inodes;
4661 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04004662 struct inode *inode;
Chris Masonea8c2812008-08-04 23:17:27 -04004663
Yan Zhengc146afa2008-11-12 14:34:12 -05004664 if (root->fs_info->sb->s_flags & MS_RDONLY)
4665 return -EROFS;
4666
Chris Mason75eff682008-12-15 15:54:40 -05004667 spin_lock(&root->fs_info->delalloc_lock);
Chris Masond3977122009-01-05 21:25:51 -05004668 while (!list_empty(head)) {
Chris Masonea8c2812008-08-04 23:17:27 -04004669 binode = list_entry(head->next, struct btrfs_inode,
4670 delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04004671 inode = igrab(&binode->vfs_inode);
4672 if (!inode)
4673 list_del_init(&binode->delalloc_inodes);
Chris Mason75eff682008-12-15 15:54:40 -05004674 spin_unlock(&root->fs_info->delalloc_lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04004675 if (inode) {
Chris Mason8c8bee12008-09-29 11:19:10 -04004676 filemap_flush(inode->i_mapping);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04004677 iput(inode);
4678 }
4679 cond_resched();
Chris Mason75eff682008-12-15 15:54:40 -05004680 spin_lock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04004681 }
Chris Mason75eff682008-12-15 15:54:40 -05004682 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04004683
4684 /* the filemap_flush will queue IO into the worker threads, but
4685 * we have to make sure the IO is actually started and that
4686 * ordered extents get created before we return
4687 */
4688 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05004689 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05004690 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04004691 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05004692 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
4693 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04004694 }
4695 atomic_dec(&root->fs_info->async_submit_draining);
Chris Masonea8c2812008-08-04 23:17:27 -04004696 return 0;
4697}
4698
Chris Mason39279cc2007-06-12 06:35:45 -04004699static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
4700 const char *symname)
4701{
4702 struct btrfs_trans_handle *trans;
4703 struct btrfs_root *root = BTRFS_I(dir)->root;
4704 struct btrfs_path *path;
4705 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05004706 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04004707 int err;
4708 int drop_inode = 0;
4709 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004710 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04004711 int name_len;
4712 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04004713 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04004714 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04004715 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05004716 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004717
4718 name_len = strlen(symname) + 1;
4719 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
4720 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05004721
Josef Bacik6a632092009-02-20 11:00:09 -05004722 err = btrfs_check_metadata_free_space(root);
Chris Mason1832a6d2007-12-21 16:27:21 -05004723 if (err)
4724 goto out_fail;
4725
Chris Mason39279cc2007-06-12 06:35:45 -04004726 trans = btrfs_start_transaction(root, 1);
4727 btrfs_set_trans_block_group(trans, dir);
4728
4729 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
4730 if (err) {
4731 err = -ENOSPC;
4732 goto out_unlock;
4733 }
4734
Josef Bacikaec74772008-07-24 12:12:38 -04004735 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05004736 dentry->d_name.len,
4737 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04004738 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO,
4739 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04004740 err = PTR_ERR(inode);
4741 if (IS_ERR(inode))
4742 goto out_unlock;
4743
Jim Owens0279b4c2009-02-04 09:29:13 -05004744 err = btrfs_init_inode_security(inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04004745 if (err) {
4746 drop_inode = 1;
4747 goto out_unlock;
4748 }
4749
Chris Mason39279cc2007-06-12 06:35:45 -04004750 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04004751 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004752 if (err)
4753 drop_inode = 1;
4754 else {
4755 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04004756 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04004757 inode->i_fop = &btrfs_file_operations;
4758 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05004759 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04004760 }
4761 dir->i_sb->s_dirt = 1;
4762 btrfs_update_inode_block_group(trans, inode);
4763 btrfs_update_inode_block_group(trans, dir);
4764 if (drop_inode)
4765 goto out_unlock;
4766
4767 path = btrfs_alloc_path();
4768 BUG_ON(!path);
4769 key.objectid = inode->i_ino;
4770 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004771 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
4772 datasize = btrfs_file_extent_calc_inline_size(name_len);
4773 err = btrfs_insert_empty_item(trans, root, path, &key,
4774 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04004775 if (err) {
4776 drop_inode = 1;
4777 goto out_unlock;
4778 }
Chris Mason5f39d392007-10-15 16:14:19 -04004779 leaf = path->nodes[0];
4780 ei = btrfs_item_ptr(leaf, path->slots[0],
4781 struct btrfs_file_extent_item);
4782 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
4783 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04004784 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04004785 btrfs_set_file_extent_encryption(leaf, ei, 0);
4786 btrfs_set_file_extent_compression(leaf, ei, 0);
4787 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
4788 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
4789
Chris Mason39279cc2007-06-12 06:35:45 -04004790 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04004791 write_extent_buffer(leaf, symname, ptr, name_len);
4792 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004793 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04004794
Chris Mason39279cc2007-06-12 06:35:45 -04004795 inode->i_op = &btrfs_symlink_inode_operations;
4796 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04004797 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04004798 inode_set_bytes(inode, name_len);
Chris Masondbe674a2008-07-17 12:54:05 -04004799 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04004800 err = btrfs_update_inode(trans, root, inode);
4801 if (err)
4802 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04004803
4804out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004805 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04004806 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05004807out_fail:
Chris Mason39279cc2007-06-12 06:35:45 -04004808 if (drop_inode) {
4809 inode_dec_link_count(inode);
4810 iput(inode);
4811 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004812 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004813 return err;
4814}
Chris Mason16432982008-04-10 10:23:21 -04004815
Yan Zhengd899e052008-10-30 14:25:28 -04004816static int prealloc_file_range(struct inode *inode, u64 start, u64 end,
4817 u64 alloc_hint, int mode)
4818{
4819 struct btrfs_trans_handle *trans;
4820 struct btrfs_root *root = BTRFS_I(inode)->root;
4821 struct btrfs_key ins;
4822 u64 alloc_size;
4823 u64 cur_offset = start;
4824 u64 num_bytes = end - start;
4825 int ret = 0;
4826
4827 trans = btrfs_join_transaction(root, 1);
4828 BUG_ON(!trans);
4829 btrfs_set_trans_block_group(trans, inode);
4830
4831 while (num_bytes > 0) {
4832 alloc_size = min(num_bytes, root->fs_info->max_extent);
4833 ret = btrfs_reserve_extent(trans, root, alloc_size,
4834 root->sectorsize, 0, alloc_hint,
4835 (u64)-1, &ins, 1);
4836 if (ret) {
4837 WARN_ON(1);
4838 goto out;
4839 }
4840 ret = insert_reserved_file_extent(trans, inode,
4841 cur_offset, ins.objectid,
4842 ins.offset, ins.offset,
4843 ins.offset, 0, 0, 0,
4844 BTRFS_FILE_EXTENT_PREALLOC);
4845 BUG_ON(ret);
4846 num_bytes -= ins.offset;
4847 cur_offset += ins.offset;
4848 alloc_hint = ins.objectid + ins.offset;
4849 }
4850out:
4851 if (cur_offset > start) {
4852 inode->i_ctime = CURRENT_TIME;
4853 btrfs_set_flag(inode, PREALLOC);
4854 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
4855 cur_offset > i_size_read(inode))
4856 btrfs_i_size_write(inode, cur_offset);
4857 ret = btrfs_update_inode(trans, root, inode);
4858 BUG_ON(ret);
4859 }
4860
4861 btrfs_end_transaction(trans, root);
4862 return ret;
4863}
4864
4865static long btrfs_fallocate(struct inode *inode, int mode,
4866 loff_t offset, loff_t len)
4867{
4868 u64 cur_offset;
4869 u64 last_byte;
4870 u64 alloc_start;
4871 u64 alloc_end;
4872 u64 alloc_hint = 0;
4873 u64 mask = BTRFS_I(inode)->root->sectorsize - 1;
4874 struct extent_map *em;
4875 int ret;
4876
4877 alloc_start = offset & ~mask;
4878 alloc_end = (offset + len + mask) & ~mask;
4879
4880 mutex_lock(&inode->i_mutex);
4881 if (alloc_start > inode->i_size) {
4882 ret = btrfs_cont_expand(inode, alloc_start);
4883 if (ret)
4884 goto out;
4885 }
4886
4887 while (1) {
4888 struct btrfs_ordered_extent *ordered;
4889 lock_extent(&BTRFS_I(inode)->io_tree, alloc_start,
4890 alloc_end - 1, GFP_NOFS);
4891 ordered = btrfs_lookup_first_ordered_extent(inode,
4892 alloc_end - 1);
4893 if (ordered &&
4894 ordered->file_offset + ordered->len > alloc_start &&
4895 ordered->file_offset < alloc_end) {
4896 btrfs_put_ordered_extent(ordered);
4897 unlock_extent(&BTRFS_I(inode)->io_tree,
4898 alloc_start, alloc_end - 1, GFP_NOFS);
4899 btrfs_wait_ordered_range(inode, alloc_start,
4900 alloc_end - alloc_start);
4901 } else {
4902 if (ordered)
4903 btrfs_put_ordered_extent(ordered);
4904 break;
4905 }
4906 }
4907
4908 cur_offset = alloc_start;
4909 while (1) {
4910 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4911 alloc_end - cur_offset, 0);
4912 BUG_ON(IS_ERR(em) || !em);
4913 last_byte = min(extent_map_end(em), alloc_end);
4914 last_byte = (last_byte + mask) & ~mask;
4915 if (em->block_start == EXTENT_MAP_HOLE) {
4916 ret = prealloc_file_range(inode, cur_offset,
4917 last_byte, alloc_hint, mode);
4918 if (ret < 0) {
4919 free_extent_map(em);
4920 break;
4921 }
4922 }
4923 if (em->block_start <= EXTENT_MAP_LAST_BYTE)
4924 alloc_hint = em->block_start;
4925 free_extent_map(em);
4926
4927 cur_offset = last_byte;
4928 if (cur_offset >= alloc_end) {
4929 ret = 0;
4930 break;
4931 }
4932 }
4933 unlock_extent(&BTRFS_I(inode)->io_tree, alloc_start, alloc_end - 1,
4934 GFP_NOFS);
4935out:
4936 mutex_unlock(&inode->i_mutex);
4937 return ret;
4938}
4939
Chris Masone6dcd2d2008-07-17 12:53:50 -04004940static int btrfs_set_page_dirty(struct page *page)
4941{
Chris Masone6dcd2d2008-07-17 12:53:50 -04004942 return __set_page_dirty_nobuffers(page);
4943}
4944
Sven Wegener0ee0fda2008-07-30 16:54:26 -04004945static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05004946{
4947 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
4948 return -EACCES;
Josef Bacik33268ea2008-07-24 12:16:36 -04004949 return generic_permission(inode, mask, btrfs_check_acl);
Yanfdebe2b2008-01-14 13:26:08 -05004950}
Chris Mason39279cc2007-06-12 06:35:45 -04004951
4952static struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05004953 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04004954 .lookup = btrfs_lookup,
4955 .create = btrfs_create,
4956 .unlink = btrfs_unlink,
4957 .link = btrfs_link,
4958 .mkdir = btrfs_mkdir,
4959 .rmdir = btrfs_rmdir,
4960 .rename = btrfs_rename,
4961 .symlink = btrfs_symlink,
4962 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04004963 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04004964 .setxattr = btrfs_setxattr,
4965 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05004966 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04004967 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05004968 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04004969};
Chris Mason39279cc2007-06-12 06:35:45 -04004970static struct inode_operations btrfs_dir_ro_inode_operations = {
4971 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05004972 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04004973};
Chris Mason39279cc2007-06-12 06:35:45 -04004974static struct file_operations btrfs_dir_file_operations = {
4975 .llseek = generic_file_llseek,
4976 .read = generic_read_dir,
David Woodhousecbdf5a22008-08-06 19:42:33 +01004977 .readdir = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04004978 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04004979#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04004980 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04004981#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04004982 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04004983 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04004984};
4985
Chris Masond1310b22008-01-24 16:13:08 -05004986static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04004987 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05004988 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04004989 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04004990 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04004991 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04004992 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Mason1259ab72008-05-12 13:39:03 -04004993 .readpage_io_failed_hook = btrfs_io_failed_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05004994 .set_bit_hook = btrfs_set_bit_hook,
4995 .clear_bit_hook = btrfs_clear_bit_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04004996};
4997
Chris Mason35054392009-01-21 13:11:13 -05004998/*
4999 * btrfs doesn't support the bmap operation because swapfiles
5000 * use bmap to make a mapping of extents in the file. They assume
5001 * these extents won't change over the life of the file and they
5002 * use the bmap result to do IO directly to the drive.
5003 *
5004 * the btrfs bmap call would return logical addresses that aren't
5005 * suitable for IO and they also will change frequently as COW
5006 * operations happen. So, swapfile + btrfs == corruption.
5007 *
5008 * For now we're avoiding this by dropping bmap.
5009 */
Chris Mason39279cc2007-06-12 06:35:45 -04005010static struct address_space_operations btrfs_aops = {
5011 .readpage = btrfs_readpage,
5012 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -04005013 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05005014 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04005015 .sync_page = block_sync_page,
Chris Mason16432982008-04-10 10:23:21 -04005016 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04005017 .invalidatepage = btrfs_invalidatepage,
5018 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005019 .set_page_dirty = btrfs_set_page_dirty,
Chris Mason39279cc2007-06-12 06:35:45 -04005020};
5021
5022static struct address_space_operations btrfs_symlink_aops = {
5023 .readpage = btrfs_readpage,
5024 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04005025 .invalidatepage = btrfs_invalidatepage,
5026 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04005027};
5028
5029static struct inode_operations btrfs_file_inode_operations = {
5030 .truncate = btrfs_truncate,
5031 .getattr = btrfs_getattr,
5032 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04005033 .setxattr = btrfs_setxattr,
5034 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05005035 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04005036 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05005037 .permission = btrfs_permission,
Yan Zhengd899e052008-10-30 14:25:28 -04005038 .fallocate = btrfs_fallocate,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05005039 .fiemap = btrfs_fiemap,
Chris Mason39279cc2007-06-12 06:35:45 -04005040};
Josef Bacik618e21d2007-07-11 10:18:17 -04005041static struct inode_operations btrfs_special_inode_operations = {
5042 .getattr = btrfs_getattr,
5043 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05005044 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04005045 .setxattr = btrfs_setxattr,
5046 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04005047 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04005048 .removexattr = btrfs_removexattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04005049};
Chris Mason39279cc2007-06-12 06:35:45 -04005050static struct inode_operations btrfs_symlink_inode_operations = {
5051 .readlink = generic_readlink,
5052 .follow_link = page_follow_link_light,
5053 .put_link = page_put_link,
Yanfdebe2b2008-01-14 13:26:08 -05005054 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05005055 .setxattr = btrfs_setxattr,
5056 .getxattr = btrfs_getxattr,
5057 .listxattr = btrfs_listxattr,
5058 .removexattr = btrfs_removexattr,
Chris Mason39279cc2007-06-12 06:35:45 -04005059};