blob: 552e08afc7fba1e3d274092c6c2aa220a37f581e [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
Chris Masonb9473432009-03-13 11:00:37 -0400137 path->leave_spinning = 1;
Chris Masonc8b97812008-10-29 14:49:59 -0400138 btrfs_set_trans_block_group(trans, inode);
139
140 key.objectid = inode->i_ino;
141 key.offset = start;
142 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
Chris Masonc8b97812008-10-29 14:49:59 -0400143 datasize = btrfs_file_extent_calc_inline_size(cur_size);
144
145 inode_add_bytes(inode, size);
146 ret = btrfs_insert_empty_item(trans, root, path, &key,
147 datasize);
148 BUG_ON(ret);
149 if (ret) {
150 err = ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400151 goto fail;
152 }
153 leaf = path->nodes[0];
154 ei = btrfs_item_ptr(leaf, path->slots[0],
155 struct btrfs_file_extent_item);
156 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
157 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
158 btrfs_set_file_extent_encryption(leaf, ei, 0);
159 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
160 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
161 ptr = btrfs_file_extent_inline_start(ei);
162
163 if (use_compress) {
164 struct page *cpage;
165 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500166 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400167 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500168 cur_size = min_t(unsigned long, compressed_size,
Chris Masonc8b97812008-10-29 14:49:59 -0400169 PAGE_CACHE_SIZE);
170
Chris Masonb9473432009-03-13 11:00:37 -0400171 kaddr = kmap_atomic(cpage, KM_USER0);
Chris Masonc8b97812008-10-29 14:49:59 -0400172 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Chris Masonb9473432009-03-13 11:00:37 -0400173 kunmap_atomic(kaddr, KM_USER0);
Chris Masonc8b97812008-10-29 14:49:59 -0400174
175 i++;
176 ptr += cur_size;
177 compressed_size -= cur_size;
178 }
179 btrfs_set_file_extent_compression(leaf, ei,
180 BTRFS_COMPRESS_ZLIB);
181 } else {
182 page = find_get_page(inode->i_mapping,
183 start >> PAGE_CACHE_SHIFT);
184 btrfs_set_file_extent_compression(leaf, ei, 0);
185 kaddr = kmap_atomic(page, KM_USER0);
186 offset = start & (PAGE_CACHE_SIZE - 1);
187 write_extent_buffer(leaf, kaddr + offset, ptr, size);
188 kunmap_atomic(kaddr, KM_USER0);
189 page_cache_release(page);
190 }
191 btrfs_mark_buffer_dirty(leaf);
192 btrfs_free_path(path);
193
194 BTRFS_I(inode)->disk_i_size = inode->i_size;
195 btrfs_update_inode(trans, root, inode);
196 return 0;
197fail:
198 btrfs_free_path(path);
199 return err;
200}
201
202
203/*
204 * conditionally insert an inline extent into the file. This
205 * does the checks required to make sure the data is small enough
206 * to fit as an inline extent.
207 */
Chris Mason7f366cf2009-03-12 20:12:45 -0400208static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400209 struct btrfs_root *root,
210 struct inode *inode, u64 start, u64 end,
211 size_t compressed_size,
212 struct page **compressed_pages)
213{
214 u64 isize = i_size_read(inode);
215 u64 actual_end = min(end + 1, isize);
216 u64 inline_len = actual_end - start;
217 u64 aligned_end = (end + root->sectorsize - 1) &
218 ~((u64)root->sectorsize - 1);
219 u64 hint_byte;
220 u64 data_len = inline_len;
221 int ret;
222
223 if (compressed_size)
224 data_len = compressed_size;
225
226 if (start > 0 ||
Chris Mason70b99e62008-10-31 12:46:39 -0400227 actual_end >= PAGE_CACHE_SIZE ||
Chris Masonc8b97812008-10-29 14:49:59 -0400228 data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
229 (!compressed_size &&
230 (actual_end & (root->sectorsize - 1)) == 0) ||
231 end + 1 < isize ||
232 data_len > root->fs_info->max_inline) {
233 return 1;
234 }
235
Chris Masonc8b97812008-10-29 14:49:59 -0400236 ret = btrfs_drop_extents(trans, root, inode, start,
Chris Masone980b502009-04-24 14:39:24 -0400237 aligned_end, aligned_end, start, &hint_byte);
Chris Masonc8b97812008-10-29 14:49:59 -0400238 BUG_ON(ret);
239
240 if (isize > actual_end)
241 inline_len = min_t(u64, isize, actual_end);
242 ret = insert_inline_extent(trans, root, inode, start,
243 inline_len, compressed_size,
244 compressed_pages);
245 BUG_ON(ret);
246 btrfs_drop_extent_cache(inode, start, aligned_end, 0);
Chris Masonc8b97812008-10-29 14:49:59 -0400247 return 0;
248}
249
Chris Mason771ed682008-11-06 22:02:51 -0500250struct async_extent {
251 u64 start;
252 u64 ram_size;
253 u64 compressed_size;
254 struct page **pages;
255 unsigned long nr_pages;
256 struct list_head list;
257};
258
259struct async_cow {
260 struct inode *inode;
261 struct btrfs_root *root;
262 struct page *locked_page;
263 u64 start;
264 u64 end;
265 struct list_head extents;
266 struct btrfs_work work;
267};
268
269static noinline int add_async_extent(struct async_cow *cow,
270 u64 start, u64 ram_size,
271 u64 compressed_size,
272 struct page **pages,
273 unsigned long nr_pages)
274{
275 struct async_extent *async_extent;
276
277 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
278 async_extent->start = start;
279 async_extent->ram_size = ram_size;
280 async_extent->compressed_size = compressed_size;
281 async_extent->pages = pages;
282 async_extent->nr_pages = nr_pages;
283 list_add_tail(&async_extent->list, &cow->extents);
284 return 0;
285}
286
Chris Masonc8b97812008-10-29 14:49:59 -0400287/*
Chris Mason771ed682008-11-06 22:02:51 -0500288 * we create compressed extents in two phases. The first
289 * phase compresses a range of pages that have already been
290 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400291 *
Chris Mason771ed682008-11-06 22:02:51 -0500292 * This is done inside an ordered work queue, and the compression
293 * is spread across many cpus. The actual IO submission is step
294 * two, and the ordered work queue takes care of making sure that
295 * happens in the same order things were put onto the queue by
296 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400297 *
Chris Mason771ed682008-11-06 22:02:51 -0500298 * If this code finds it can't get good compression, it puts an
299 * entry onto the work queue to write the uncompressed bytes. This
300 * makes sure that both compressed inodes and uncompressed inodes
301 * are written in the same order that pdflush sent them down.
Chris Masond352ac62008-09-29 15:18:18 -0400302 */
Chris Mason771ed682008-11-06 22:02:51 -0500303static noinline int compress_file_range(struct inode *inode,
304 struct page *locked_page,
305 u64 start, u64 end,
306 struct async_cow *async_cow,
307 int *num_added)
Chris Masonb888db22007-08-27 16:49:44 -0400308{
309 struct btrfs_root *root = BTRFS_I(inode)->root;
310 struct btrfs_trans_handle *trans;
Chris Masondb945352007-10-15 16:15:53 -0400311 u64 num_bytes;
Chris Masonc8b97812008-10-29 14:49:59 -0400312 u64 orig_start;
313 u64 disk_num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400314 u64 blocksize = root->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400315 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500316 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400317 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400318 struct page **pages = NULL;
319 unsigned long nr_pages;
320 unsigned long nr_pages_ret = 0;
321 unsigned long total_compressed = 0;
322 unsigned long total_in = 0;
323 unsigned long max_compressed = 128 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -0500324 unsigned long max_uncompressed = 128 * 1024;
Chris Masonc8b97812008-10-29 14:49:59 -0400325 int i;
326 int will_compress;
Chris Masonb888db22007-08-27 16:49:44 -0400327
Chris Masonc8b97812008-10-29 14:49:59 -0400328 orig_start = start;
Chris Masonbe20aa92007-12-17 20:14:01 -0500329
Chris Mason42dc7ba2008-12-15 11:44:56 -0500330 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400331again:
332 will_compress = 0;
333 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
334 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
335
Chris Masonf03d9302009-02-04 09:31:06 -0500336 /*
337 * we don't want to send crud past the end of i_size through
338 * compression, that's just a waste of CPU time. So, if the
339 * end of the file is before the start of our current
340 * requested range of bytes, we bail out to the uncompressed
341 * cleanup code that can deal with all of this.
342 *
343 * It isn't really the fastest way to fix things, but this is a
344 * very uncommon corner.
345 */
346 if (actual_end <= start)
347 goto cleanup_and_bail_uncompressed;
348
Chris Masonc8b97812008-10-29 14:49:59 -0400349 total_compressed = actual_end - start;
350
351 /* we want to make sure that amount of ram required to uncompress
352 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500353 * of a compressed extent to 128k. This is a crucial number
354 * because it also controls how easily we can spread reads across
355 * cpus for decompression.
356 *
357 * We also want to make sure the amount of IO required to do
358 * a random read is reasonably small, so we limit the size of
359 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400360 */
361 total_compressed = min(total_compressed, max_uncompressed);
Chris Masondb945352007-10-15 16:15:53 -0400362 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
Chris Masonbe20aa92007-12-17 20:14:01 -0500363 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400364 disk_num_bytes = num_bytes;
365 total_in = 0;
366 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400367
Chris Mason771ed682008-11-06 22:02:51 -0500368 /*
369 * we do compression for mount -o compress and when the
370 * inode has not been flagged as nocompress. This flag can
371 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400372 */
373 if (!btrfs_test_flag(inode, NOCOMPRESS) &&
374 btrfs_test_opt(root, COMPRESS)) {
375 WARN_ON(pages);
Chris Masoncfbc2462008-10-30 13:22:14 -0400376 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
Chris Mason179e29e2007-11-01 11:28:41 -0400377
Chris Masonc8b97812008-10-29 14:49:59 -0400378 ret = btrfs_zlib_compress_pages(inode->i_mapping, start,
379 total_compressed, pages,
380 nr_pages, &nr_pages_ret,
381 &total_in,
382 &total_compressed,
383 max_compressed);
384
385 if (!ret) {
386 unsigned long offset = total_compressed &
387 (PAGE_CACHE_SIZE - 1);
388 struct page *page = pages[nr_pages_ret - 1];
389 char *kaddr;
390
391 /* zero the tail end of the last page, we might be
392 * sending it down to disk
393 */
394 if (offset) {
395 kaddr = kmap_atomic(page, KM_USER0);
396 memset(kaddr + offset, 0,
397 PAGE_CACHE_SIZE - offset);
398 kunmap_atomic(kaddr, KM_USER0);
399 }
400 will_compress = 1;
401 }
402 }
403 if (start == 0) {
Chris Mason771ed682008-11-06 22:02:51 -0500404 trans = btrfs_join_transaction(root, 1);
405 BUG_ON(!trans);
406 btrfs_set_trans_block_group(trans, inode);
407
Chris Masonc8b97812008-10-29 14:49:59 -0400408 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500409 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400410 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500411 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400412 */
413 ret = cow_file_range_inline(trans, root, inode,
414 start, end, 0, NULL);
415 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500416 /* try making a compressed inline extent */
Chris Masonc8b97812008-10-29 14:49:59 -0400417 ret = cow_file_range_inline(trans, root, inode,
418 start, end,
419 total_compressed, pages);
420 }
Chris Mason771ed682008-11-06 22:02:51 -0500421 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400422 if (ret == 0) {
Chris Mason771ed682008-11-06 22:02:51 -0500423 /*
424 * inline extent creation worked, we don't need
425 * to create any more async work items. Unlock
426 * and free up our temp pages.
427 */
Chris Masonc8b97812008-10-29 14:49:59 -0400428 extent_clear_unlock_delalloc(inode,
429 &BTRFS_I(inode)->io_tree,
Chris Mason771ed682008-11-06 22:02:51 -0500430 start, end, NULL, 1, 0,
431 0, 1, 1, 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400432 ret = 0;
433 goto free_pages_out;
434 }
435 }
436
437 if (will_compress) {
438 /*
439 * we aren't doing an inline extent round the compressed size
440 * up to a block size boundary so the allocator does sane
441 * things
442 */
443 total_compressed = (total_compressed + blocksize - 1) &
444 ~(blocksize - 1);
445
446 /*
447 * one last check to make sure the compression is really a
448 * win, compare the page count read with the blocks on disk
449 */
450 total_in = (total_in + PAGE_CACHE_SIZE - 1) &
451 ~(PAGE_CACHE_SIZE - 1);
452 if (total_compressed >= total_in) {
453 will_compress = 0;
454 } else {
455 disk_num_bytes = total_compressed;
456 num_bytes = total_in;
457 }
458 }
459 if (!will_compress && pages) {
460 /*
461 * the compression code ran but failed to make things smaller,
462 * free any pages it allocated and our page pointer array
463 */
464 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400465 WARN_ON(pages[i]->mapping);
Chris Masonc8b97812008-10-29 14:49:59 -0400466 page_cache_release(pages[i]);
467 }
468 kfree(pages);
469 pages = NULL;
470 total_compressed = 0;
471 nr_pages_ret = 0;
472
473 /* flag the file so we don't compress in the future */
474 btrfs_set_flag(inode, NOCOMPRESS);
475 }
Chris Mason771ed682008-11-06 22:02:51 -0500476 if (will_compress) {
477 *num_added += 1;
478
479 /* the async work queues will take care of doing actual
480 * allocation on disk for these compressed pages,
481 * and will submit them to the elevator.
482 */
483 add_async_extent(async_cow, start, num_bytes,
484 total_compressed, pages, nr_pages_ret);
485
Chris Mason42dc7ba2008-12-15 11:44:56 -0500486 if (start + num_bytes < end && start + num_bytes < actual_end) {
Chris Mason771ed682008-11-06 22:02:51 -0500487 start += num_bytes;
488 pages = NULL;
489 cond_resched();
490 goto again;
491 }
492 } else {
Chris Masonf03d9302009-02-04 09:31:06 -0500493cleanup_and_bail_uncompressed:
Chris Mason771ed682008-11-06 22:02:51 -0500494 /*
495 * No compression, but we still need to write the pages in
496 * the file we've been given so far. redirty the locked
497 * page if it corresponds to our extent and set things up
498 * for the async work queue to run cow_file_range to do
499 * the normal delalloc dance
500 */
501 if (page_offset(locked_page) >= start &&
502 page_offset(locked_page) <= end) {
503 __set_page_dirty_nobuffers(locked_page);
504 /* unlocked later on in the async handlers */
505 }
506 add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0);
507 *num_added += 1;
508 }
509
510out:
511 return 0;
512
513free_pages_out:
514 for (i = 0; i < nr_pages_ret; i++) {
515 WARN_ON(pages[i]->mapping);
516 page_cache_release(pages[i]);
517 }
Chris Masond3977122009-01-05 21:25:51 -0500518 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500519
520 goto out;
521}
522
523/*
524 * phase two of compressed writeback. This is the ordered portion
525 * of the code, which only gets called in the order the work was
526 * queued. We walk all the async extents created by compress_file_range
527 * and send them down to the disk.
528 */
529static noinline int submit_compressed_extents(struct inode *inode,
530 struct async_cow *async_cow)
531{
532 struct async_extent *async_extent;
533 u64 alloc_hint = 0;
534 struct btrfs_trans_handle *trans;
535 struct btrfs_key ins;
536 struct extent_map *em;
537 struct btrfs_root *root = BTRFS_I(inode)->root;
538 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
539 struct extent_io_tree *io_tree;
540 int ret;
541
542 if (list_empty(&async_cow->extents))
543 return 0;
544
545 trans = btrfs_join_transaction(root, 1);
546
Chris Masond3977122009-01-05 21:25:51 -0500547 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500548 async_extent = list_entry(async_cow->extents.next,
549 struct async_extent, list);
550 list_del(&async_extent->list);
551
552 io_tree = &BTRFS_I(inode)->io_tree;
553
554 /* did the compression code fall back to uncompressed IO? */
555 if (!async_extent->pages) {
556 int page_started = 0;
557 unsigned long nr_written = 0;
558
559 lock_extent(io_tree, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500560 async_extent->start +
561 async_extent->ram_size - 1, GFP_NOFS);
Chris Mason771ed682008-11-06 22:02:51 -0500562
563 /* allocate blocks */
564 cow_file_range(inode, async_cow->locked_page,
565 async_extent->start,
566 async_extent->start +
567 async_extent->ram_size - 1,
568 &page_started, &nr_written, 0);
569
570 /*
571 * if page_started, cow_file_range inserted an
572 * inline extent and took care of all the unlocking
573 * and IO for us. Otherwise, we need to submit
574 * all those pages down to the drive.
575 */
576 if (!page_started)
577 extent_write_locked_range(io_tree,
578 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500579 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500580 async_extent->ram_size - 1,
581 btrfs_get_extent,
582 WB_SYNC_ALL);
583 kfree(async_extent);
584 cond_resched();
585 continue;
586 }
587
588 lock_extent(io_tree, async_extent->start,
589 async_extent->start + async_extent->ram_size - 1,
590 GFP_NOFS);
591 /*
592 * here we're doing allocation and writeback of the
593 * compressed pages
594 */
595 btrfs_drop_extent_cache(inode, async_extent->start,
596 async_extent->start +
597 async_extent->ram_size - 1, 0);
598
599 ret = btrfs_reserve_extent(trans, root,
600 async_extent->compressed_size,
601 async_extent->compressed_size,
602 0, alloc_hint,
603 (u64)-1, &ins, 1);
604 BUG_ON(ret);
605 em = alloc_extent_map(GFP_NOFS);
606 em->start = async_extent->start;
607 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500608 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500609
610 em->block_start = ins.objectid;
611 em->block_len = ins.offset;
612 em->bdev = root->fs_info->fs_devices->latest_bdev;
613 set_bit(EXTENT_FLAG_PINNED, &em->flags);
614 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
615
Chris Masond3977122009-01-05 21:25:51 -0500616 while (1) {
Chris Mason771ed682008-11-06 22:02:51 -0500617 spin_lock(&em_tree->lock);
618 ret = add_extent_mapping(em_tree, em);
619 spin_unlock(&em_tree->lock);
620 if (ret != -EEXIST) {
621 free_extent_map(em);
622 break;
623 }
624 btrfs_drop_extent_cache(inode, async_extent->start,
625 async_extent->start +
626 async_extent->ram_size - 1, 0);
627 }
628
629 ret = btrfs_add_ordered_extent(inode, async_extent->start,
630 ins.objectid,
631 async_extent->ram_size,
632 ins.offset,
633 BTRFS_ORDERED_COMPRESSED);
634 BUG_ON(ret);
635
636 btrfs_end_transaction(trans, root);
637
638 /*
639 * clear dirty, set writeback and unlock the pages.
640 */
641 extent_clear_unlock_delalloc(inode,
642 &BTRFS_I(inode)->io_tree,
643 async_extent->start,
644 async_extent->start +
645 async_extent->ram_size - 1,
646 NULL, 1, 1, 0, 1, 1, 0);
647
648 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500649 async_extent->start,
650 async_extent->ram_size,
651 ins.objectid,
652 ins.offset, async_extent->pages,
653 async_extent->nr_pages);
Chris Mason771ed682008-11-06 22:02:51 -0500654
655 BUG_ON(ret);
656 trans = btrfs_join_transaction(root, 1);
657 alloc_hint = ins.objectid + ins.offset;
658 kfree(async_extent);
659 cond_resched();
660 }
661
662 btrfs_end_transaction(trans, root);
663 return 0;
664}
665
666/*
667 * when extent_io.c finds a delayed allocation range in the file,
668 * the call backs end up in this code. The basic idea is to
669 * allocate extents on disk for the range, and create ordered data structs
670 * in ram to track those extents.
671 *
672 * locked_page is the page that writepage had locked already. We use
673 * it to make sure we don't do extra locks or unlocks.
674 *
675 * *page_started is set to one if we unlock locked_page and do everything
676 * required to start IO on it. It may be clean and already done with
677 * IO when we return.
678 */
679static noinline int cow_file_range(struct inode *inode,
680 struct page *locked_page,
681 u64 start, u64 end, int *page_started,
682 unsigned long *nr_written,
683 int unlock)
684{
685 struct btrfs_root *root = BTRFS_I(inode)->root;
686 struct btrfs_trans_handle *trans;
687 u64 alloc_hint = 0;
688 u64 num_bytes;
689 unsigned long ram_size;
690 u64 disk_num_bytes;
691 u64 cur_alloc_size;
692 u64 blocksize = root->sectorsize;
693 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500694 u64 isize = i_size_read(inode);
Chris Mason771ed682008-11-06 22:02:51 -0500695 struct btrfs_key ins;
696 struct extent_map *em;
697 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
698 int ret = 0;
699
700 trans = btrfs_join_transaction(root, 1);
701 BUG_ON(!trans);
702 btrfs_set_trans_block_group(trans, inode);
703
Chris Mason42dc7ba2008-12-15 11:44:56 -0500704 actual_end = min_t(u64, isize, end + 1);
Chris Mason771ed682008-11-06 22:02:51 -0500705
706 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
707 num_bytes = max(blocksize, num_bytes);
708 disk_num_bytes = num_bytes;
709 ret = 0;
710
711 if (start == 0) {
712 /* lets try to make an inline extent */
713 ret = cow_file_range_inline(trans, root, inode,
714 start, end, 0, NULL);
715 if (ret == 0) {
716 extent_clear_unlock_delalloc(inode,
717 &BTRFS_I(inode)->io_tree,
718 start, end, NULL, 1, 1,
719 1, 1, 1, 1);
720 *nr_written = *nr_written +
721 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
722 *page_started = 1;
723 ret = 0;
724 goto out;
725 }
726 }
Chris Masonc8b97812008-10-29 14:49:59 -0400727
728 BUG_ON(disk_num_bytes >
729 btrfs_super_total_bytes(&root->fs_info->super_copy));
730
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400731 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400732
Chris Masond3977122009-01-05 21:25:51 -0500733 while (disk_num_bytes > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400734 cur_alloc_size = min(disk_num_bytes, root->fs_info->max_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400735 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500736 root->sectorsize, 0, alloc_hint,
Chris Masone6dcd2d2008-07-17 12:53:50 -0400737 (u64)-1, &ins, 1);
Chris Masond3977122009-01-05 21:25:51 -0500738 BUG_ON(ret);
739
Chris Masone6dcd2d2008-07-17 12:53:50 -0400740 em = alloc_extent_map(GFP_NOFS);
741 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500742 em->orig_start = em->start;
Chris Masonc8b97812008-10-29 14:49:59 -0400743
Chris Mason771ed682008-11-06 22:02:51 -0500744 ram_size = ins.offset;
745 em->len = ins.offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400746
Chris Masone6dcd2d2008-07-17 12:53:50 -0400747 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400748 em->block_len = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400749 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400750 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -0400751
Chris Masond3977122009-01-05 21:25:51 -0500752 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -0400753 spin_lock(&em_tree->lock);
754 ret = add_extent_mapping(em_tree, em);
755 spin_unlock(&em_tree->lock);
756 if (ret != -EEXIST) {
757 free_extent_map(em);
758 break;
759 }
760 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400761 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400762 }
763
Chris Mason98d20f62008-04-14 09:46:10 -0400764 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400765 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -0500766 ram_size, cur_alloc_size, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400767 BUG_ON(ret);
Chris Masonc8b97812008-10-29 14:49:59 -0400768
Yan Zheng17d217f2008-12-12 10:03:38 -0500769 if (root->root_key.objectid ==
770 BTRFS_DATA_RELOC_TREE_OBJECTID) {
771 ret = btrfs_reloc_clone_csums(inode, start,
772 cur_alloc_size);
773 BUG_ON(ret);
774 }
775
Chris Masond3977122009-01-05 21:25:51 -0500776 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -0400777 break;
Chris Masond3977122009-01-05 21:25:51 -0500778
Chris Masonc8b97812008-10-29 14:49:59 -0400779 /* we're not doing compressed IO, don't unlock the first
780 * page (which the caller expects to stay locked), don't
781 * clear any dirty bits and don't set any writeback bits
782 */
783 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
784 start, start + ram_size - 1,
Chris Mason771ed682008-11-06 22:02:51 -0500785 locked_page, unlock, 1,
786 1, 0, 0, 0);
Chris Masonc8b97812008-10-29 14:49:59 -0400787 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -0500788 num_bytes -= cur_alloc_size;
789 alloc_hint = ins.objectid + ins.offset;
790 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400791 }
Chris Masonb888db22007-08-27 16:49:44 -0400792out:
Chris Mason771ed682008-11-06 22:02:51 -0500793 ret = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400794 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400795
Chris Masonbe20aa92007-12-17 20:14:01 -0500796 return ret;
Chris Mason771ed682008-11-06 22:02:51 -0500797}
Chris Masonc8b97812008-10-29 14:49:59 -0400798
Chris Mason771ed682008-11-06 22:02:51 -0500799/*
800 * work queue call back to started compression on a file and pages
801 */
802static noinline void async_cow_start(struct btrfs_work *work)
803{
804 struct async_cow *async_cow;
805 int num_added = 0;
806 async_cow = container_of(work, struct async_cow, work);
807
808 compress_file_range(async_cow->inode, async_cow->locked_page,
809 async_cow->start, async_cow->end, async_cow,
810 &num_added);
811 if (num_added == 0)
812 async_cow->inode = NULL;
813}
814
815/*
816 * work queue call back to submit previously compressed pages
817 */
818static noinline void async_cow_submit(struct btrfs_work *work)
819{
820 struct async_cow *async_cow;
821 struct btrfs_root *root;
822 unsigned long nr_pages;
823
824 async_cow = container_of(work, struct async_cow, work);
825
826 root = async_cow->root;
827 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
828 PAGE_CACHE_SHIFT;
829
830 atomic_sub(nr_pages, &root->fs_info->async_delalloc_pages);
831
832 if (atomic_read(&root->fs_info->async_delalloc_pages) <
833 5 * 1042 * 1024 &&
834 waitqueue_active(&root->fs_info->async_submit_wait))
835 wake_up(&root->fs_info->async_submit_wait);
836
Chris Masond3977122009-01-05 21:25:51 -0500837 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -0500838 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -0500839}
Chris Masonc8b97812008-10-29 14:49:59 -0400840
Chris Mason771ed682008-11-06 22:02:51 -0500841static noinline void async_cow_free(struct btrfs_work *work)
842{
843 struct async_cow *async_cow;
844 async_cow = container_of(work, struct async_cow, work);
845 kfree(async_cow);
846}
847
848static int cow_file_range_async(struct inode *inode, struct page *locked_page,
849 u64 start, u64 end, int *page_started,
850 unsigned long *nr_written)
851{
852 struct async_cow *async_cow;
853 struct btrfs_root *root = BTRFS_I(inode)->root;
854 unsigned long nr_pages;
855 u64 cur_end;
856 int limit = 10 * 1024 * 1042;
857
Chris Mason771ed682008-11-06 22:02:51 -0500858 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED |
859 EXTENT_DELALLOC, 1, 0, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -0500860 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500861 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
862 async_cow->inode = inode;
863 async_cow->root = root;
864 async_cow->locked_page = locked_page;
865 async_cow->start = start;
866
867 if (btrfs_test_flag(inode, NOCOMPRESS))
868 cur_end = end;
869 else
870 cur_end = min(end, start + 512 * 1024 - 1);
871
872 async_cow->end = cur_end;
873 INIT_LIST_HEAD(&async_cow->extents);
874
875 async_cow->work.func = async_cow_start;
876 async_cow->work.ordered_func = async_cow_submit;
877 async_cow->work.ordered_free = async_cow_free;
878 async_cow->work.flags = 0;
879
Chris Mason771ed682008-11-06 22:02:51 -0500880 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
881 PAGE_CACHE_SHIFT;
882 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
883
884 btrfs_queue_worker(&root->fs_info->delalloc_workers,
885 &async_cow->work);
886
887 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
888 wait_event(root->fs_info->async_submit_wait,
889 (atomic_read(&root->fs_info->async_delalloc_pages) <
890 limit));
891 }
892
Chris Masond3977122009-01-05 21:25:51 -0500893 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -0500894 atomic_read(&root->fs_info->async_delalloc_pages)) {
895 wait_event(root->fs_info->async_submit_wait,
896 (atomic_read(&root->fs_info->async_delalloc_pages) ==
897 0));
898 }
899
900 *nr_written += nr_pages;
901 start = cur_end + 1;
902 }
903 *page_started = 1;
904 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -0500905}
906
Chris Masond3977122009-01-05 21:25:51 -0500907static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -0500908 u64 bytenr, u64 num_bytes)
909{
910 int ret;
911 struct btrfs_ordered_sum *sums;
912 LIST_HEAD(list);
913
Yan Zheng07d400a2009-01-06 11:42:00 -0500914 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
915 bytenr + num_bytes - 1, &list);
Yan Zheng17d217f2008-12-12 10:03:38 -0500916 if (ret == 0 && list_empty(&list))
917 return 0;
918
919 while (!list_empty(&list)) {
920 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
921 list_del(&sums->list);
922 kfree(sums);
923 }
924 return 1;
925}
926
Chris Masond352ac62008-09-29 15:18:18 -0400927/*
928 * when nowcow writeback call back. This checks for snapshots or COW copies
929 * of the extents that exist in the file, and COWs the file as required.
930 *
931 * If no cow copies or snapshots exist, we write directly to the existing
932 * blocks on disk
933 */
Chris Mason7f366cf2009-03-12 20:12:45 -0400934static noinline int run_delalloc_nocow(struct inode *inode,
935 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -0500936 u64 start, u64 end, int *page_started, int force,
937 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -0500938{
Chris Masonbe20aa92007-12-17 20:14:01 -0500939 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -0400940 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -0500941 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -0500942 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -0400943 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -0500944 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -0400945 u64 cow_start;
946 u64 cur_offset;
947 u64 extent_end;
948 u64 disk_bytenr;
949 u64 num_bytes;
950 int extent_type;
951 int ret;
Yan Zhengd899e052008-10-30 14:25:28 -0400952 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -0400953 int nocow;
954 int check_prev = 1;
Chris Masonbe20aa92007-12-17 20:14:01 -0500955
956 path = btrfs_alloc_path();
957 BUG_ON(!path);
Yan Zheng7ea394f2008-08-05 13:05:02 -0400958 trans = btrfs_join_transaction(root, 1);
959 BUG_ON(!trans);
Chris Masonbe20aa92007-12-17 20:14:01 -0500960
Yan Zheng80ff3852008-10-30 14:20:02 -0400961 cow_start = (u64)-1;
962 cur_offset = start;
963 while (1) {
964 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
965 cur_offset, 0);
966 BUG_ON(ret < 0);
967 if (ret > 0 && path->slots[0] > 0 && check_prev) {
968 leaf = path->nodes[0];
969 btrfs_item_key_to_cpu(leaf, &found_key,
970 path->slots[0] - 1);
971 if (found_key.objectid == inode->i_ino &&
972 found_key.type == BTRFS_EXTENT_DATA_KEY)
973 path->slots[0]--;
974 }
975 check_prev = 0;
976next_slot:
977 leaf = path->nodes[0];
978 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
979 ret = btrfs_next_leaf(root, path);
980 if (ret < 0)
981 BUG_ON(1);
982 if (ret > 0)
983 break;
984 leaf = path->nodes[0];
985 }
Chris Masonbe20aa92007-12-17 20:14:01 -0500986
Yan Zheng80ff3852008-10-30 14:20:02 -0400987 nocow = 0;
988 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -0500989 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -0400990 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -0500991
Yan Zheng80ff3852008-10-30 14:20:02 -0400992 if (found_key.objectid > inode->i_ino ||
993 found_key.type > BTRFS_EXTENT_DATA_KEY ||
994 found_key.offset > end)
995 break;
Chris Masonbe20aa92007-12-17 20:14:01 -0500996
Yan Zheng80ff3852008-10-30 14:20:02 -0400997 if (found_key.offset > cur_offset) {
998 extent_end = found_key.offset;
999 goto out_check;
1000 }
Chris Masonc31f8832008-01-08 15:46:31 -05001001
Yan Zheng80ff3852008-10-30 14:20:02 -04001002 fi = btrfs_item_ptr(leaf, path->slots[0],
1003 struct btrfs_file_extent_item);
1004 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001005
Yan Zhengd899e052008-10-30 14:25:28 -04001006 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1007 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001008 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1009 extent_end = found_key.offset +
1010 btrfs_file_extent_num_bytes(leaf, fi);
1011 if (extent_end <= start) {
1012 path->slots[0]++;
1013 goto next_slot;
1014 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001015 if (disk_bytenr == 0)
1016 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001017 if (btrfs_file_extent_compression(leaf, fi) ||
1018 btrfs_file_extent_encryption(leaf, fi) ||
1019 btrfs_file_extent_other_encoding(leaf, fi))
1020 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001021 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1022 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001023 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001024 goto out_check;
Yan Zheng17d217f2008-12-12 10:03:38 -05001025 if (btrfs_cross_ref_exist(trans, root, inode->i_ino,
1026 disk_bytenr))
1027 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001028 disk_bytenr += btrfs_file_extent_offset(leaf, fi);
Yan Zheng17d217f2008-12-12 10:03:38 -05001029 disk_bytenr += cur_offset - found_key.offset;
1030 num_bytes = min(end + 1, extent_end) - cur_offset;
1031 /*
1032 * force cow if csum exists in the range.
1033 * this ensure that csum for a given extent are
1034 * either valid or do not exist.
1035 */
1036 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1037 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001038 nocow = 1;
1039 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1040 extent_end = found_key.offset +
1041 btrfs_file_extent_inline_len(leaf, fi);
1042 extent_end = ALIGN(extent_end, root->sectorsize);
1043 } else {
1044 BUG_ON(1);
1045 }
1046out_check:
1047 if (extent_end <= start) {
1048 path->slots[0]++;
1049 goto next_slot;
1050 }
1051 if (!nocow) {
1052 if (cow_start == (u64)-1)
1053 cow_start = cur_offset;
1054 cur_offset = extent_end;
1055 if (cur_offset > end)
1056 break;
1057 path->slots[0]++;
1058 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001059 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001060
Yan Zheng7ea394f2008-08-05 13:05:02 -04001061 btrfs_release_path(root, path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001062 if (cow_start != (u64)-1) {
1063 ret = cow_file_range(inode, locked_page, cow_start,
Chris Mason771ed682008-11-06 22:02:51 -05001064 found_key.offset - 1, page_started,
1065 nr_written, 1);
Yan Zheng80ff3852008-10-30 14:20:02 -04001066 BUG_ON(ret);
1067 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001068 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001069
Yan Zhengd899e052008-10-30 14:25:28 -04001070 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1071 struct extent_map *em;
1072 struct extent_map_tree *em_tree;
1073 em_tree = &BTRFS_I(inode)->extent_tree;
1074 em = alloc_extent_map(GFP_NOFS);
1075 em->start = cur_offset;
Chris Mason445a6942008-11-10 11:53:33 -05001076 em->orig_start = em->start;
Yan Zhengd899e052008-10-30 14:25:28 -04001077 em->len = num_bytes;
1078 em->block_len = num_bytes;
1079 em->block_start = disk_bytenr;
1080 em->bdev = root->fs_info->fs_devices->latest_bdev;
1081 set_bit(EXTENT_FLAG_PINNED, &em->flags);
1082 while (1) {
1083 spin_lock(&em_tree->lock);
1084 ret = add_extent_mapping(em_tree, em);
1085 spin_unlock(&em_tree->lock);
1086 if (ret != -EEXIST) {
1087 free_extent_map(em);
1088 break;
1089 }
1090 btrfs_drop_extent_cache(inode, em->start,
1091 em->start + em->len - 1, 0);
1092 }
1093 type = BTRFS_ORDERED_PREALLOC;
1094 } else {
1095 type = BTRFS_ORDERED_NOCOW;
1096 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001097
1098 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001099 num_bytes, num_bytes, type);
1100 BUG_ON(ret);
Chris Mason771ed682008-11-06 22:02:51 -05001101
Yan Zhengd899e052008-10-30 14:25:28 -04001102 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
1103 cur_offset, cur_offset + num_bytes - 1,
Chris Mason771ed682008-11-06 22:02:51 -05001104 locked_page, 1, 1, 1, 0, 0, 0);
Yan Zheng80ff3852008-10-30 14:20:02 -04001105 cur_offset = extent_end;
1106 if (cur_offset > end)
1107 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001108 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001109 btrfs_release_path(root, path);
1110
1111 if (cur_offset <= end && cow_start == (u64)-1)
1112 cow_start = cur_offset;
1113 if (cow_start != (u64)-1) {
1114 ret = cow_file_range(inode, locked_page, cow_start, end,
Chris Mason771ed682008-11-06 22:02:51 -05001115 page_started, nr_written, 1);
Yan Zheng80ff3852008-10-30 14:20:02 -04001116 BUG_ON(ret);
1117 }
1118
1119 ret = btrfs_end_transaction(trans, root);
1120 BUG_ON(ret);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001121 btrfs_free_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001122 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001123}
1124
Chris Masond352ac62008-09-29 15:18:18 -04001125/*
1126 * extent_io.c call back to do delayed allocation processing
1127 */
Chris Masonc8b97812008-10-29 14:49:59 -04001128static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001129 u64 start, u64 end, int *page_started,
1130 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001131{
Chris Masonbe20aa92007-12-17 20:14:01 -05001132 int ret;
Chris Mason7f366cf2009-03-12 20:12:45 -04001133 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona2135012008-06-25 16:01:30 -04001134
Yan Zheng17d217f2008-12-12 10:03:38 -05001135 if (btrfs_test_flag(inode, NODATACOW))
Chris Masonc8b97812008-10-29 14:49:59 -04001136 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001137 page_started, 1, nr_written);
Yan Zhengd899e052008-10-30 14:25:28 -04001138 else if (btrfs_test_flag(inode, PREALLOC))
1139 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001140 page_started, 0, nr_written);
Chris Mason7f366cf2009-03-12 20:12:45 -04001141 else if (!btrfs_test_opt(root, COMPRESS))
1142 ret = cow_file_range(inode, locked_page, start, end,
1143 page_started, nr_written, 1);
Chris Masonbe20aa92007-12-17 20:14:01 -05001144 else
Chris Mason771ed682008-11-06 22:02:51 -05001145 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001146 page_started, nr_written);
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,
Chris Masone980b502009-04-24 14:39:24 -04001442 u64 locked_end,
Yan Zhengd899e052008-10-30 14:25:28 -04001443 u8 compression, u8 encryption,
1444 u16 other_encoding, int extent_type)
1445{
1446 struct btrfs_root *root = BTRFS_I(inode)->root;
1447 struct btrfs_file_extent_item *fi;
1448 struct btrfs_path *path;
1449 struct extent_buffer *leaf;
1450 struct btrfs_key ins;
1451 u64 hint;
1452 int ret;
1453
1454 path = btrfs_alloc_path();
1455 BUG_ON(!path);
1456
Chris Masonb9473432009-03-13 11:00:37 -04001457 path->leave_spinning = 1;
Yan Zhengd899e052008-10-30 14:25:28 -04001458 ret = btrfs_drop_extents(trans, root, inode, file_pos,
Chris Masone980b502009-04-24 14:39:24 -04001459 file_pos + num_bytes, locked_end,
1460 file_pos, &hint);
Yan Zhengd899e052008-10-30 14:25:28 -04001461 BUG_ON(ret);
1462
1463 ins.objectid = inode->i_ino;
1464 ins.offset = file_pos;
1465 ins.type = BTRFS_EXTENT_DATA_KEY;
1466 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
1467 BUG_ON(ret);
1468 leaf = path->nodes[0];
1469 fi = btrfs_item_ptr(leaf, path->slots[0],
1470 struct btrfs_file_extent_item);
1471 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1472 btrfs_set_file_extent_type(leaf, fi, extent_type);
1473 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1474 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1475 btrfs_set_file_extent_offset(leaf, fi, 0);
1476 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1477 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1478 btrfs_set_file_extent_compression(leaf, fi, compression);
1479 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1480 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04001481
1482 btrfs_unlock_up_safe(path, 1);
1483 btrfs_set_lock_blocking(leaf);
1484
Yan Zhengd899e052008-10-30 14:25:28 -04001485 btrfs_mark_buffer_dirty(leaf);
1486
1487 inode_add_bytes(inode, num_bytes);
1488 btrfs_drop_extent_cache(inode, file_pos, file_pos + num_bytes - 1, 0);
1489
1490 ins.objectid = disk_bytenr;
1491 ins.offset = disk_num_bytes;
1492 ins.type = BTRFS_EXTENT_ITEM_KEY;
1493 ret = btrfs_alloc_reserved_extent(trans, root, leaf->start,
1494 root->root_key.objectid,
1495 trans->transid, inode->i_ino, &ins);
1496 BUG_ON(ret);
Yan Zhengd899e052008-10-30 14:25:28 -04001497 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04001498
Yan Zhengd899e052008-10-30 14:25:28 -04001499 return 0;
1500}
1501
Chris Mason5d13a982009-03-13 11:41:46 -04001502/*
1503 * helper function for btrfs_finish_ordered_io, this
1504 * just reads in some of the csum leaves to prime them into ram
1505 * before we start the transaction. It limits the amount of btree
1506 * reads required while inside the transaction.
1507 */
1508static noinline void reada_csum(struct btrfs_root *root,
1509 struct btrfs_path *path,
1510 struct btrfs_ordered_extent *ordered_extent)
1511{
1512 struct btrfs_ordered_sum *sum;
1513 u64 bytenr;
1514
1515 sum = list_entry(ordered_extent->list.next, struct btrfs_ordered_sum,
1516 list);
1517 bytenr = sum->sums[0].bytenr;
1518
1519 /*
1520 * we don't care about the results, the point of this search is
1521 * just to get the btree leaves into ram
1522 */
1523 btrfs_lookup_csum(NULL, root->fs_info->csum_root, path, bytenr, 0);
1524}
1525
Chris Masond352ac62008-09-29 15:18:18 -04001526/* as ordered data IO finishes, this gets called so we can finish
1527 * an ordered extent if the range of bytes in the file it covers are
1528 * fully written.
1529 */
Chris Mason211f90e2008-07-18 11:56:15 -04001530static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001531{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001532 struct btrfs_root *root = BTRFS_I(inode)->root;
1533 struct btrfs_trans_handle *trans;
Chris Mason5d13a982009-03-13 11:41:46 -04001534 struct btrfs_ordered_extent *ordered_extent = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001535 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonb7ec40d2009-03-12 20:12:45 -04001536 struct btrfs_path *path;
Yan Zhengd899e052008-10-30 14:25:28 -04001537 int compressed = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001538 int ret;
1539
1540 ret = btrfs_dec_test_ordered_pending(inode, start, end - start + 1);
Chris Masonba1da2f2008-07-17 12:54:15 -04001541 if (!ret)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001542 return 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001543
Chris Masonb7ec40d2009-03-12 20:12:45 -04001544 /*
1545 * before we join the transaction, try to do some of our IO.
1546 * This will limit the amount of IO that we have to do with
1547 * the transaction running. We're unlikely to need to do any
1548 * IO if the file extents are new, the disk_i_size checks
1549 * covers the most common case.
1550 */
1551 if (start < BTRFS_I(inode)->disk_i_size) {
1552 path = btrfs_alloc_path();
1553 if (path) {
1554 ret = btrfs_lookup_file_extent(NULL, root, path,
1555 inode->i_ino,
1556 start, 0);
Chris Mason5d13a982009-03-13 11:41:46 -04001557 ordered_extent = btrfs_lookup_ordered_extent(inode,
1558 start);
1559 if (!list_empty(&ordered_extent->list)) {
1560 btrfs_release_path(root, path);
1561 reada_csum(root, path, ordered_extent);
1562 }
Chris Masonb7ec40d2009-03-12 20:12:45 -04001563 btrfs_free_path(path);
1564 }
1565 }
1566
Chris Masonf9295742008-07-17 12:54:14 -04001567 trans = btrfs_join_transaction(root, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001568
Chris Mason5d13a982009-03-13 11:41:46 -04001569 if (!ordered_extent)
1570 ordered_extent = btrfs_lookup_ordered_extent(inode, start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001571 BUG_ON(!ordered_extent);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001572 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags))
1573 goto nocow;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001574
1575 lock_extent(io_tree, ordered_extent->file_offset,
1576 ordered_extent->file_offset + ordered_extent->len - 1,
1577 GFP_NOFS);
1578
Chris Masonc8b97812008-10-29 14:49:59 -04001579 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Yan Zhengd899e052008-10-30 14:25:28 -04001580 compressed = 1;
1581 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
1582 BUG_ON(compressed);
1583 ret = btrfs_mark_extent_written(trans, root, inode,
1584 ordered_extent->file_offset,
1585 ordered_extent->file_offset +
1586 ordered_extent->len);
1587 BUG_ON(ret);
1588 } else {
1589 ret = insert_reserved_file_extent(trans, inode,
1590 ordered_extent->file_offset,
1591 ordered_extent->start,
1592 ordered_extent->disk_len,
1593 ordered_extent->len,
1594 ordered_extent->len,
Chris Masone980b502009-04-24 14:39:24 -04001595 ordered_extent->file_offset +
1596 ordered_extent->len,
Yan Zhengd899e052008-10-30 14:25:28 -04001597 compressed, 0, 0,
1598 BTRFS_FILE_EXTENT_REG);
1599 BUG_ON(ret);
1600 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04001601 unlock_extent(io_tree, ordered_extent->file_offset,
1602 ordered_extent->file_offset + ordered_extent->len - 1,
1603 GFP_NOFS);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001604nocow:
Chris Masone6dcd2d2008-07-17 12:53:50 -04001605 add_pending_csums(trans, inode, ordered_extent->file_offset,
1606 &ordered_extent->list);
1607
Chris Mason34353022008-09-23 20:19:49 -04001608 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Chris Masondbe674a2008-07-17 12:54:05 -04001609 btrfs_ordered_update_i_size(inode, ordered_extent);
Chris Masone02119d2008-09-05 16:13:11 -04001610 btrfs_update_inode(trans, root, inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001611 btrfs_remove_ordered_extent(inode, ordered_extent);
Chris Mason34353022008-09-23 20:19:49 -04001612 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
Chris Mason7f3c74f2008-07-18 12:01:11 -04001613
Chris Masone6dcd2d2008-07-17 12:53:50 -04001614 /* once for us */
1615 btrfs_put_ordered_extent(ordered_extent);
1616 /* once for the tree */
1617 btrfs_put_ordered_extent(ordered_extent);
1618
Chris Masone6dcd2d2008-07-17 12:53:50 -04001619 btrfs_end_transaction(trans, root);
1620 return 0;
1621}
1622
Christoph Hellwigb2950862008-12-02 09:54:17 -05001623static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04001624 struct extent_state *state, int uptodate)
1625{
1626 return btrfs_finish_ordered_io(page->mapping->host, start, end);
1627}
1628
Chris Masond352ac62008-09-29 15:18:18 -04001629/*
1630 * When IO fails, either with EIO or csum verification fails, we
1631 * try other mirrors that might have a good copy of the data. This
1632 * io_failure_record is used to record state as we go through all the
1633 * mirrors. If another mirror has good data, the page is set up to date
1634 * and things continue. If a good mirror can't be found, the original
1635 * bio end_io callback is called to indicate things have failed.
1636 */
Chris Mason7e383262008-04-09 16:28:12 -04001637struct io_failure_record {
1638 struct page *page;
1639 u64 start;
1640 u64 len;
1641 u64 logical;
Chris Masond20f7042008-12-08 16:58:54 -05001642 unsigned long bio_flags;
Chris Mason7e383262008-04-09 16:28:12 -04001643 int last_mirror;
1644};
1645
Christoph Hellwigb2950862008-12-02 09:54:17 -05001646static int btrfs_io_failed_hook(struct bio *failed_bio,
Chris Mason1259ab72008-05-12 13:39:03 -04001647 struct page *page, u64 start, u64 end,
1648 struct extent_state *state)
Chris Mason7e383262008-04-09 16:28:12 -04001649{
1650 struct io_failure_record *failrec = NULL;
1651 u64 private;
1652 struct extent_map *em;
1653 struct inode *inode = page->mapping->host;
1654 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Chris Mason3b951512008-04-17 11:29:12 -04001655 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason7e383262008-04-09 16:28:12 -04001656 struct bio *bio;
1657 int num_copies;
1658 int ret;
Chris Mason1259ab72008-05-12 13:39:03 -04001659 int rw;
Chris Mason7e383262008-04-09 16:28:12 -04001660 u64 logical;
1661
1662 ret = get_state_private(failure_tree, start, &private);
1663 if (ret) {
Chris Mason7e383262008-04-09 16:28:12 -04001664 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
1665 if (!failrec)
1666 return -ENOMEM;
1667 failrec->start = start;
1668 failrec->len = end - start + 1;
1669 failrec->last_mirror = 0;
Chris Masond20f7042008-12-08 16:58:54 -05001670 failrec->bio_flags = 0;
Chris Mason7e383262008-04-09 16:28:12 -04001671
Chris Mason3b951512008-04-17 11:29:12 -04001672 spin_lock(&em_tree->lock);
1673 em = lookup_extent_mapping(em_tree, start, failrec->len);
1674 if (em->start > start || em->start + em->len < start) {
1675 free_extent_map(em);
1676 em = NULL;
1677 }
1678 spin_unlock(&em_tree->lock);
Chris Mason7e383262008-04-09 16:28:12 -04001679
1680 if (!em || IS_ERR(em)) {
1681 kfree(failrec);
1682 return -EIO;
1683 }
1684 logical = start - em->start;
1685 logical = em->block_start + logical;
Chris Masond20f7042008-12-08 16:58:54 -05001686 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
1687 logical = em->block_start;
1688 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
1689 }
Chris Mason7e383262008-04-09 16:28:12 -04001690 failrec->logical = logical;
1691 free_extent_map(em);
1692 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
1693 EXTENT_DIRTY, GFP_NOFS);
Chris Mason587f7702008-04-11 12:16:46 -04001694 set_state_private(failure_tree, start,
1695 (u64)(unsigned long)failrec);
Chris Mason7e383262008-04-09 16:28:12 -04001696 } else {
Chris Mason587f7702008-04-11 12:16:46 -04001697 failrec = (struct io_failure_record *)(unsigned long)private;
Chris Mason7e383262008-04-09 16:28:12 -04001698 }
1699 num_copies = btrfs_num_copies(
1700 &BTRFS_I(inode)->root->fs_info->mapping_tree,
1701 failrec->logical, failrec->len);
1702 failrec->last_mirror++;
1703 if (!state) {
Chris Masoncad321a2008-12-17 14:51:42 -05001704 spin_lock(&BTRFS_I(inode)->io_tree.lock);
Chris Mason7e383262008-04-09 16:28:12 -04001705 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
1706 failrec->start,
1707 EXTENT_LOCKED);
1708 if (state && state->start != failrec->start)
1709 state = NULL;
Chris Masoncad321a2008-12-17 14:51:42 -05001710 spin_unlock(&BTRFS_I(inode)->io_tree.lock);
Chris Mason7e383262008-04-09 16:28:12 -04001711 }
1712 if (!state || failrec->last_mirror > num_copies) {
1713 set_state_private(failure_tree, failrec->start, 0);
1714 clear_extent_bits(failure_tree, failrec->start,
1715 failrec->start + failrec->len - 1,
1716 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
1717 kfree(failrec);
1718 return -EIO;
1719 }
1720 bio = bio_alloc(GFP_NOFS, 1);
1721 bio->bi_private = state;
1722 bio->bi_end_io = failed_bio->bi_end_io;
1723 bio->bi_sector = failrec->logical >> 9;
1724 bio->bi_bdev = failed_bio->bi_bdev;
Chris Masone1c4b742008-04-22 13:26:46 -04001725 bio->bi_size = 0;
Chris Masond20f7042008-12-08 16:58:54 -05001726
Chris Mason7e383262008-04-09 16:28:12 -04001727 bio_add_page(bio, page, failrec->len, start - page_offset(page));
Chris Mason1259ab72008-05-12 13:39:03 -04001728 if (failed_bio->bi_rw & (1 << BIO_RW))
1729 rw = WRITE;
1730 else
1731 rw = READ;
1732
1733 BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
Chris Masonc8b97812008-10-29 14:49:59 -04001734 failrec->last_mirror,
Chris Masond20f7042008-12-08 16:58:54 -05001735 failrec->bio_flags);
Chris Mason1259ab72008-05-12 13:39:03 -04001736 return 0;
1737}
1738
Chris Masond352ac62008-09-29 15:18:18 -04001739/*
1740 * each time an IO finishes, we do a fast check in the IO failure tree
1741 * to see if we need to process or clean up an io_failure_record
1742 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001743static int btrfs_clean_io_failures(struct inode *inode, u64 start)
Chris Mason1259ab72008-05-12 13:39:03 -04001744{
1745 u64 private;
1746 u64 private_failure;
1747 struct io_failure_record *failure;
1748 int ret;
1749
1750 private = 0;
1751 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
1752 (u64)-1, 1, EXTENT_DIRTY)) {
1753 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
1754 start, &private_failure);
1755 if (ret == 0) {
1756 failure = (struct io_failure_record *)(unsigned long)
1757 private_failure;
1758 set_state_private(&BTRFS_I(inode)->io_failure_tree,
1759 failure->start, 0);
1760 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
1761 failure->start,
1762 failure->start + failure->len - 1,
1763 EXTENT_DIRTY | EXTENT_LOCKED,
1764 GFP_NOFS);
1765 kfree(failure);
1766 }
1767 }
Chris Mason7e383262008-04-09 16:28:12 -04001768 return 0;
1769}
1770
Chris Masond352ac62008-09-29 15:18:18 -04001771/*
1772 * when reads are done, we need to check csums to verify the data is correct
1773 * if there's a match, we allow the bio to finish. If not, we go through
1774 * the io_failure_record routines to find good copies
1775 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001776static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason70dec802008-01-29 09:59:12 -05001777 struct extent_state *state)
Chris Mason07157aa2007-08-30 08:50:51 -04001778{
Chris Mason35ebb932007-10-30 16:56:53 -04001779 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -04001780 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05001781 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04001782 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -05001783 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -04001784 int ret;
Chris Masonff79f812007-10-15 16:22:25 -04001785 struct btrfs_root *root = BTRFS_I(inode)->root;
1786 u32 csum = ~(u32)0;
Chris Masond1310b22008-01-24 16:13:08 -05001787
Chris Masond20f7042008-12-08 16:58:54 -05001788 if (PageChecked(page)) {
1789 ClearPageChecked(page);
1790 goto good;
1791 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001792 if (btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -05001793 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05001794
Yan Zheng17d217f2008-12-12 10:03:38 -05001795 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
1796 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1)) {
1797 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
1798 GFP_NOFS);
1799 return 0;
1800 }
1801
Yanc2e639f2008-02-04 08:57:25 -05001802 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -05001803 private = state->private;
1804 ret = 0;
1805 } else {
1806 ret = get_state_private(io_tree, start, &private);
1807 }
Chris Mason9ab86c82009-01-07 09:48:51 -05001808 kaddr = kmap_atomic(page, KM_USER0);
Chris Masond3977122009-01-05 21:25:51 -05001809 if (ret)
Chris Mason07157aa2007-08-30 08:50:51 -04001810 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05001811
Chris Masonff79f812007-10-15 16:22:25 -04001812 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
1813 btrfs_csum_final(csum, (char *)&csum);
Chris Masond3977122009-01-05 21:25:51 -05001814 if (csum != private)
Chris Mason07157aa2007-08-30 08:50:51 -04001815 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05001816
Chris Mason9ab86c82009-01-07 09:48:51 -05001817 kunmap_atomic(kaddr, KM_USER0);
Chris Masond20f7042008-12-08 16:58:54 -05001818good:
Chris Mason7e383262008-04-09 16:28:12 -04001819 /* if the io failure tree for this inode is non-empty,
1820 * check to see if we've recovered from a failed IO
1821 */
Chris Mason1259ab72008-05-12 13:39:03 -04001822 btrfs_clean_io_failures(inode, start);
Chris Mason07157aa2007-08-30 08:50:51 -04001823 return 0;
1824
1825zeroit:
Chris Mason193f2842009-04-27 07:29:05 -04001826 if (printk_ratelimit()) {
1827 printk(KERN_INFO "btrfs csum failed ino %lu off %llu csum %u "
1828 "private %llu\n", page->mapping->host->i_ino,
1829 (unsigned long long)start, csum,
1830 (unsigned long long)private);
1831 }
Chris Masondb945352007-10-15 16:15:53 -04001832 memset(kaddr + offset, 1, end - start + 1);
1833 flush_dcache_page(page);
Chris Mason9ab86c82009-01-07 09:48:51 -05001834 kunmap_atomic(kaddr, KM_USER0);
Chris Mason3b951512008-04-17 11:29:12 -04001835 if (private == 0)
1836 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04001837 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04001838}
Chris Masonb888db22007-08-27 16:49:44 -04001839
Josef Bacik7b128762008-07-24 12:17:14 -04001840/*
1841 * This creates an orphan entry for the given inode in case something goes
1842 * wrong in the middle of an unlink/truncate.
1843 */
1844int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
1845{
1846 struct btrfs_root *root = BTRFS_I(inode)->root;
1847 int ret = 0;
1848
Yanbcc63ab2008-07-30 16:29:20 -04001849 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001850
1851 /* already on the orphan list, we're good */
1852 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
Yanbcc63ab2008-07-30 16:29:20 -04001853 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001854 return 0;
1855 }
1856
1857 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
1858
Yanbcc63ab2008-07-30 16:29:20 -04001859 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001860
1861 /*
1862 * insert an orphan item to track this unlinked/truncated file
1863 */
1864 ret = btrfs_insert_orphan_item(trans, root, inode->i_ino);
1865
1866 return ret;
1867}
1868
1869/*
1870 * We have done the truncate/delete so we can go ahead and remove the orphan
1871 * item for this particular inode.
1872 */
1873int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
1874{
1875 struct btrfs_root *root = BTRFS_I(inode)->root;
1876 int ret = 0;
1877
Yanbcc63ab2008-07-30 16:29:20 -04001878 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001879
1880 if (list_empty(&BTRFS_I(inode)->i_orphan)) {
Yanbcc63ab2008-07-30 16:29:20 -04001881 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001882 return 0;
1883 }
1884
1885 list_del_init(&BTRFS_I(inode)->i_orphan);
1886 if (!trans) {
Yanbcc63ab2008-07-30 16:29:20 -04001887 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001888 return 0;
1889 }
1890
Yanbcc63ab2008-07-30 16:29:20 -04001891 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001892
1893 ret = btrfs_del_orphan_item(trans, root, inode->i_ino);
1894
1895 return ret;
1896}
1897
1898/*
1899 * this cleans up any orphans that may be left on the list from the last use
1900 * of this root.
1901 */
1902void btrfs_orphan_cleanup(struct btrfs_root *root)
1903{
1904 struct btrfs_path *path;
1905 struct extent_buffer *leaf;
1906 struct btrfs_item *item;
1907 struct btrfs_key key, found_key;
1908 struct btrfs_trans_handle *trans;
1909 struct inode *inode;
1910 int ret = 0, nr_unlink = 0, nr_truncate = 0;
1911
Josef Bacik7b128762008-07-24 12:17:14 -04001912 path = btrfs_alloc_path();
1913 if (!path)
1914 return;
1915 path->reada = -1;
1916
1917 key.objectid = BTRFS_ORPHAN_OBJECTID;
1918 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
1919 key.offset = (u64)-1;
1920
Josef Bacik7b128762008-07-24 12:17:14 -04001921
1922 while (1) {
1923 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1924 if (ret < 0) {
1925 printk(KERN_ERR "Error searching slot for orphan: %d"
1926 "\n", ret);
1927 break;
1928 }
1929
1930 /*
1931 * if ret == 0 means we found what we were searching for, which
1932 * is weird, but possible, so only screw with path if we didnt
1933 * find the key and see if we have stuff that matches
1934 */
1935 if (ret > 0) {
1936 if (path->slots[0] == 0)
1937 break;
1938 path->slots[0]--;
1939 }
1940
1941 /* pull out the item */
1942 leaf = path->nodes[0];
1943 item = btrfs_item_nr(leaf, path->slots[0]);
1944 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1945
1946 /* make sure the item matches what we want */
1947 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
1948 break;
1949 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
1950 break;
1951
1952 /* release the path since we're done with it */
1953 btrfs_release_path(root, path);
1954
1955 /*
1956 * this is where we are basically btrfs_lookup, without the
1957 * crossing root thing. we store the inode number in the
1958 * offset of the orphan item.
1959 */
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001960 inode = btrfs_iget_locked(root->fs_info->sb,
Josef Bacik7b128762008-07-24 12:17:14 -04001961 found_key.offset, root);
1962 if (!inode)
1963 break;
1964
1965 if (inode->i_state & I_NEW) {
1966 BTRFS_I(inode)->root = root;
1967
1968 /* have to set the location manually */
1969 BTRFS_I(inode)->location.objectid = inode->i_ino;
1970 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
1971 BTRFS_I(inode)->location.offset = 0;
1972
1973 btrfs_read_locked_inode(inode);
1974 unlock_new_inode(inode);
1975 }
1976
1977 /*
1978 * add this inode to the orphan list so btrfs_orphan_del does
1979 * the proper thing when we hit it
1980 */
Yanbcc63ab2008-07-30 16:29:20 -04001981 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001982 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
Yanbcc63ab2008-07-30 16:29:20 -04001983 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001984
1985 /*
1986 * if this is a bad inode, means we actually succeeded in
1987 * removing the inode, but not the orphan record, which means
1988 * we need to manually delete the orphan since iput will just
1989 * do a destroy_inode
1990 */
1991 if (is_bad_inode(inode)) {
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001992 trans = btrfs_start_transaction(root, 1);
Josef Bacik7b128762008-07-24 12:17:14 -04001993 btrfs_orphan_del(trans, inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001994 btrfs_end_transaction(trans, root);
Josef Bacik7b128762008-07-24 12:17:14 -04001995 iput(inode);
1996 continue;
1997 }
1998
1999 /* if we have links, this was a truncate, lets do that */
2000 if (inode->i_nlink) {
2001 nr_truncate++;
2002 btrfs_truncate(inode);
2003 } else {
2004 nr_unlink++;
2005 }
2006
2007 /* this will do delete_inode and everything for us */
2008 iput(inode);
2009 }
2010
2011 if (nr_unlink)
2012 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
2013 if (nr_truncate)
2014 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
2015
2016 btrfs_free_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04002017}
2018
Chris Masond352ac62008-09-29 15:18:18 -04002019/*
2020 * read an inode from the btree into the in-memory inode
2021 */
Chris Mason39279cc2007-06-12 06:35:45 -04002022void btrfs_read_locked_inode(struct inode *inode)
2023{
2024 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002025 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002026 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04002027 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04002028 struct btrfs_root *root = BTRFS_I(inode)->root;
2029 struct btrfs_key location;
2030 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -04002031 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04002032 int ret;
2033
2034 path = btrfs_alloc_path();
2035 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -04002036 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05002037
Chris Mason39279cc2007-06-12 06:35:45 -04002038 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002039 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04002040 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04002041
Chris Mason5f39d392007-10-15 16:14:19 -04002042 leaf = path->nodes[0];
2043 inode_item = btrfs_item_ptr(leaf, path->slots[0],
2044 struct btrfs_inode_item);
2045
2046 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
2047 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
2048 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
2049 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
Chris Masondbe674a2008-07-17 12:54:05 -04002050 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04002051
2052 tspec = btrfs_inode_atime(inode_item);
2053 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2054 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2055
2056 tspec = btrfs_inode_mtime(inode_item);
2057 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2058 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2059
2060 tspec = btrfs_inode_ctime(inode_item);
2061 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2062 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2063
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002064 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04002065 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Chris Masonc3027eb2008-12-08 16:40:21 -05002066 BTRFS_I(inode)->sequence = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04002067 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04002068 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002069 rdev = btrfs_inode_rdev(leaf, inode_item);
2070
Josef Bacikaec74772008-07-24 12:12:38 -04002071 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05002072 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Josef Bacikaec74772008-07-24 12:12:38 -04002073
Chris Mason5f39d392007-10-15 16:14:19 -04002074 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002075
Yan Zhengd2fb3432008-12-11 16:30:39 -05002076 BTRFS_I(inode)->block_group = btrfs_find_block_group(root, 0,
2077 alloc_group_block, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002078 btrfs_free_path(path);
2079 inode_item = NULL;
2080
Chris Mason39279cc2007-06-12 06:35:45 -04002081 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04002082 case S_IFREG:
2083 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04002084 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05002085 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002086 inode->i_fop = &btrfs_file_operations;
2087 inode->i_op = &btrfs_file_inode_operations;
2088 break;
2089 case S_IFDIR:
2090 inode->i_fop = &btrfs_dir_file_operations;
2091 if (root == root->fs_info->tree_root)
2092 inode->i_op = &btrfs_dir_ro_inode_operations;
2093 else
2094 inode->i_op = &btrfs_dir_inode_operations;
2095 break;
2096 case S_IFLNK:
2097 inode->i_op = &btrfs_symlink_inode_operations;
2098 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04002099 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04002100 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04002101 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05002102 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04002103 init_special_inode(inode, inode->i_mode, rdev);
2104 break;
Chris Mason39279cc2007-06-12 06:35:45 -04002105 }
2106 return;
2107
2108make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04002109 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002110 make_bad_inode(inode);
2111}
2112
Chris Masond352ac62008-09-29 15:18:18 -04002113/*
2114 * given a leaf and an inode, copy the inode fields into the leaf
2115 */
Chris Masone02119d2008-09-05 16:13:11 -04002116static void fill_inode_item(struct btrfs_trans_handle *trans,
2117 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04002118 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04002119 struct inode *inode)
2120{
Chris Mason5f39d392007-10-15 16:14:19 -04002121 btrfs_set_inode_uid(leaf, item, inode->i_uid);
2122 btrfs_set_inode_gid(leaf, item, inode->i_gid);
Chris Masondbe674a2008-07-17 12:54:05 -04002123 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
Chris Mason5f39d392007-10-15 16:14:19 -04002124 btrfs_set_inode_mode(leaf, item, inode->i_mode);
2125 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
2126
2127 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
2128 inode->i_atime.tv_sec);
2129 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
2130 inode->i_atime.tv_nsec);
2131
2132 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
2133 inode->i_mtime.tv_sec);
2134 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
2135 inode->i_mtime.tv_nsec);
2136
2137 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
2138 inode->i_ctime.tv_sec);
2139 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
2140 inode->i_ctime.tv_nsec);
2141
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002142 btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode));
Chris Masone02119d2008-09-05 16:13:11 -04002143 btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
Chris Masonc3027eb2008-12-08 16:40:21 -05002144 btrfs_set_inode_sequence(leaf, item, BTRFS_I(inode)->sequence);
Chris Masone02119d2008-09-05 16:13:11 -04002145 btrfs_set_inode_transid(leaf, item, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002146 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -05002147 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002148 btrfs_set_inode_block_group(leaf, item, BTRFS_I(inode)->block_group);
Chris Mason39279cc2007-06-12 06:35:45 -04002149}
2150
Chris Masond352ac62008-09-29 15:18:18 -04002151/*
2152 * copy everything in the in-memory inode into the btree.
2153 */
Chris Masond3977122009-01-05 21:25:51 -05002154noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
2155 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002156{
2157 struct btrfs_inode_item *inode_item;
2158 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002159 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002160 int ret;
2161
2162 path = btrfs_alloc_path();
2163 BUG_ON(!path);
Chris Masonb9473432009-03-13 11:00:37 -04002164 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002165 ret = btrfs_lookup_inode(trans, root, path,
2166 &BTRFS_I(inode)->location, 1);
2167 if (ret) {
2168 if (ret > 0)
2169 ret = -ENOENT;
2170 goto failed;
2171 }
2172
Chris Masonb4ce94d2009-02-04 09:25:08 -05002173 btrfs_unlock_up_safe(path, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002174 leaf = path->nodes[0];
2175 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04002176 struct btrfs_inode_item);
2177
Chris Masone02119d2008-09-05 16:13:11 -04002178 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002179 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002180 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002181 ret = 0;
2182failed:
Chris Mason39279cc2007-06-12 06:35:45 -04002183 btrfs_free_path(path);
2184 return ret;
2185}
2186
2187
Chris Masond352ac62008-09-29 15:18:18 -04002188/*
2189 * unlink helper that gets used here in inode.c and in the tree logging
2190 * recovery code. It remove a link in a directory with a given name, and
2191 * also drops the back refs in the inode to the directory
2192 */
Chris Masone02119d2008-09-05 16:13:11 -04002193int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2194 struct btrfs_root *root,
2195 struct inode *dir, struct inode *inode,
2196 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04002197{
2198 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04002199 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002200 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002201 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04002202 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04002203 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04002204
2205 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04002206 if (!path) {
2207 ret = -ENOMEM;
2208 goto err;
2209 }
2210
Chris Masonb9473432009-03-13 11:00:37 -04002211 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002212 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
2213 name, name_len, -1);
2214 if (IS_ERR(di)) {
2215 ret = PTR_ERR(di);
2216 goto err;
2217 }
2218 if (!di) {
2219 ret = -ENOENT;
2220 goto err;
2221 }
Chris Mason5f39d392007-10-15 16:14:19 -04002222 leaf = path->nodes[0];
2223 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04002224 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04002225 if (ret)
2226 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -04002227 btrfs_release_path(root, path);
2228
Josef Bacikaec74772008-07-24 12:12:38 -04002229 ret = btrfs_del_inode_ref(trans, root, name, name_len,
Chris Masone02119d2008-09-05 16:13:11 -04002230 inode->i_ino,
2231 dir->i_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04002232 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05002233 printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
Josef Bacikaec74772008-07-24 12:12:38 -04002234 "inode %lu parent %lu\n", name_len, name,
Chris Masone02119d2008-09-05 16:13:11 -04002235 inode->i_ino, dir->i_ino);
Josef Bacikaec74772008-07-24 12:12:38 -04002236 goto err;
2237 }
2238
Chris Mason39279cc2007-06-12 06:35:45 -04002239 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04002240 index, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -04002241 if (IS_ERR(di)) {
2242 ret = PTR_ERR(di);
2243 goto err;
2244 }
2245 if (!di) {
2246 ret = -ENOENT;
2247 goto err;
2248 }
2249 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason925baed2008-06-25 16:01:30 -04002250 btrfs_release_path(root, path);
Chris Mason39279cc2007-06-12 06:35:45 -04002251
Chris Masone02119d2008-09-05 16:13:11 -04002252 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
2253 inode, dir->i_ino);
Chris Mason49eb7e42008-09-11 15:53:12 -04002254 BUG_ON(ret != 0 && ret != -ENOENT);
Chris Masone02119d2008-09-05 16:13:11 -04002255
2256 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
2257 dir, index);
2258 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04002259err:
2260 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002261 if (ret)
2262 goto out;
2263
2264 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
2265 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2266 btrfs_update_inode(trans, root, dir);
2267 btrfs_drop_nlink(inode);
2268 ret = btrfs_update_inode(trans, root, inode);
2269 dir->i_sb->s_dirt = 1;
2270out:
Chris Mason39279cc2007-06-12 06:35:45 -04002271 return ret;
2272}
2273
2274static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
2275{
2276 struct btrfs_root *root;
2277 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04002278 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002279 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -05002280 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002281
2282 root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05002283
Chris Mason39279cc2007-06-12 06:35:45 -04002284 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002285
Chris Mason39279cc2007-06-12 06:35:45 -04002286 btrfs_set_trans_block_group(trans, dir);
Chris Mason12fcfd22009-03-24 10:24:20 -04002287
2288 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
2289
Chris Masone02119d2008-09-05 16:13:11 -04002290 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
2291 dentry->d_name.name, dentry->d_name.len);
Josef Bacik7b128762008-07-24 12:17:14 -04002292
2293 if (inode->i_nlink == 0)
2294 ret = btrfs_orphan_add(trans, inode);
2295
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002296 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04002297
Chris Mason89ce8a62008-06-25 16:01:31 -04002298 btrfs_end_transaction_throttle(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002299 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002300 return ret;
2301}
2302
2303static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
2304{
2305 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002306 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002307 int ret;
2308 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04002309 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -05002310 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002311
Chris Mason3394e162008-11-17 20:42:26 -05002312 /*
2313 * the FIRST_FREE_OBJECTID check makes sure we don't try to rmdir
2314 * the root of a subvolume or snapshot
2315 */
2316 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE ||
2317 inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) {
Yan134d4512007-10-25 15:49:25 -04002318 return -ENOTEMPTY;
Chris Mason925baed2008-06-25 16:01:30 -04002319 }
Yan134d4512007-10-25 15:49:25 -04002320
Chris Mason39279cc2007-06-12 06:35:45 -04002321 trans = btrfs_start_transaction(root, 1);
2322 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -04002323
Josef Bacik7b128762008-07-24 12:17:14 -04002324 err = btrfs_orphan_add(trans, inode);
2325 if (err)
2326 goto fail_trans;
2327
Chris Mason39279cc2007-06-12 06:35:45 -04002328 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04002329 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
2330 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05002331 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04002332 btrfs_i_size_write(inode, 0);
Chris Mason39544012007-12-12 14:38:19 -05002333
Josef Bacik7b128762008-07-24 12:17:14 -04002334fail_trans:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002335 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04002336 ret = btrfs_end_transaction_throttle(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002337 btrfs_btree_balance_dirty(root, nr);
Chris Mason39544012007-12-12 14:38:19 -05002338
Chris Mason39279cc2007-06-12 06:35:45 -04002339 if (ret && !err)
2340 err = ret;
2341 return err;
2342}
2343
Chris Masond20f7042008-12-08 16:58:54 -05002344#if 0
Chris Mason39279cc2007-06-12 06:35:45 -04002345/*
Chris Mason323ac952008-10-01 19:05:46 -04002346 * when truncating bytes in a file, it is possible to avoid reading
2347 * the leaves that contain only checksum items. This can be the
2348 * majority of the IO required to delete a large file, but it must
2349 * be done carefully.
2350 *
2351 * The keys in the level just above the leaves are checked to make sure
2352 * the lowest key in a given leaf is a csum key, and starts at an offset
2353 * after the new size.
2354 *
2355 * Then the key for the next leaf is checked to make sure it also has
2356 * a checksum item for the same file. If it does, we know our target leaf
2357 * contains only checksum items, and it can be safely freed without reading
2358 * it.
2359 *
2360 * This is just an optimization targeted at large files. It may do
2361 * nothing. It will return 0 unless things went badly.
2362 */
2363static noinline int drop_csum_leaves(struct btrfs_trans_handle *trans,
2364 struct btrfs_root *root,
2365 struct btrfs_path *path,
2366 struct inode *inode, u64 new_size)
2367{
2368 struct btrfs_key key;
2369 int ret;
2370 int nritems;
2371 struct btrfs_key found_key;
2372 struct btrfs_key other_key;
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002373 struct btrfs_leaf_ref *ref;
2374 u64 leaf_gen;
2375 u64 leaf_start;
Chris Mason323ac952008-10-01 19:05:46 -04002376
2377 path->lowest_level = 1;
2378 key.objectid = inode->i_ino;
2379 key.type = BTRFS_CSUM_ITEM_KEY;
2380 key.offset = new_size;
2381again:
2382 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2383 if (ret < 0)
2384 goto out;
2385
2386 if (path->nodes[1] == NULL) {
2387 ret = 0;
2388 goto out;
2389 }
2390 ret = 0;
2391 btrfs_node_key_to_cpu(path->nodes[1], &found_key, path->slots[1]);
2392 nritems = btrfs_header_nritems(path->nodes[1]);
2393
2394 if (!nritems)
2395 goto out;
2396
2397 if (path->slots[1] >= nritems)
2398 goto next_node;
2399
2400 /* did we find a key greater than anything we want to delete? */
2401 if (found_key.objectid > inode->i_ino ||
2402 (found_key.objectid == inode->i_ino && found_key.type > key.type))
2403 goto out;
2404
2405 /* we check the next key in the node to make sure the leave contains
2406 * only checksum items. This comparison doesn't work if our
2407 * leaf is the last one in the node
2408 */
2409 if (path->slots[1] + 1 >= nritems) {
2410next_node:
2411 /* search forward from the last key in the node, this
2412 * will bring us into the next node in the tree
2413 */
2414 btrfs_node_key_to_cpu(path->nodes[1], &found_key, nritems - 1);
2415
2416 /* unlikely, but we inc below, so check to be safe */
2417 if (found_key.offset == (u64)-1)
2418 goto out;
2419
2420 /* search_forward needs a path with locks held, do the
2421 * search again for the original key. It is possible
2422 * this will race with a balance and return a path that
2423 * we could modify, but this drop is just an optimization
2424 * and is allowed to miss some leaves.
2425 */
2426 btrfs_release_path(root, path);
2427 found_key.offset++;
2428
2429 /* setup a max key for search_forward */
2430 other_key.offset = (u64)-1;
2431 other_key.type = key.type;
2432 other_key.objectid = key.objectid;
2433
2434 path->keep_locks = 1;
2435 ret = btrfs_search_forward(root, &found_key, &other_key,
2436 path, 0, 0);
2437 path->keep_locks = 0;
2438 if (ret || found_key.objectid != key.objectid ||
2439 found_key.type != key.type) {
2440 ret = 0;
2441 goto out;
2442 }
2443
2444 key.offset = found_key.offset;
2445 btrfs_release_path(root, path);
2446 cond_resched();
2447 goto again;
2448 }
2449
2450 /* we know there's one more slot after us in the tree,
2451 * read that key so we can verify it is also a checksum item
2452 */
2453 btrfs_node_key_to_cpu(path->nodes[1], &other_key, path->slots[1] + 1);
2454
2455 if (found_key.objectid < inode->i_ino)
2456 goto next_key;
2457
2458 if (found_key.type != key.type || found_key.offset < new_size)
2459 goto next_key;
2460
2461 /*
2462 * if the key for the next leaf isn't a csum key from this objectid,
2463 * we can't be sure there aren't good items inside this leaf.
2464 * Bail out
2465 */
2466 if (other_key.objectid != inode->i_ino || other_key.type != key.type)
2467 goto out;
2468
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002469 leaf_start = btrfs_node_blockptr(path->nodes[1], path->slots[1]);
2470 leaf_gen = btrfs_node_ptr_generation(path->nodes[1], path->slots[1]);
Chris Mason323ac952008-10-01 19:05:46 -04002471 /*
2472 * it is safe to delete this leaf, it contains only
2473 * csum items from this inode at an offset >= new_size
2474 */
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002475 ret = btrfs_del_leaf(trans, root, path, leaf_start);
Chris Mason323ac952008-10-01 19:05:46 -04002476 BUG_ON(ret);
2477
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002478 if (root->ref_cows && leaf_gen < trans->transid) {
2479 ref = btrfs_alloc_leaf_ref(root, 0);
2480 if (ref) {
2481 ref->root_gen = root->root_key.offset;
2482 ref->bytenr = leaf_start;
2483 ref->owner = 0;
2484 ref->generation = leaf_gen;
2485 ref->nritems = 0;
2486
Chris Masonbd56b302009-02-04 09:27:02 -05002487 btrfs_sort_leaf_ref(ref);
2488
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002489 ret = btrfs_add_leaf_ref(root, ref, 0);
2490 WARN_ON(ret);
2491 btrfs_free_leaf_ref(root, ref);
2492 } else {
2493 WARN_ON(1);
2494 }
2495 }
Chris Mason323ac952008-10-01 19:05:46 -04002496next_key:
2497 btrfs_release_path(root, path);
2498
2499 if (other_key.objectid == inode->i_ino &&
2500 other_key.type == key.type && other_key.offset > key.offset) {
2501 key.offset = other_key.offset;
2502 cond_resched();
2503 goto again;
2504 }
2505 ret = 0;
2506out:
2507 /* fixup any changes we've made to the path */
2508 path->lowest_level = 0;
2509 path->keep_locks = 0;
2510 btrfs_release_path(root, path);
2511 return ret;
2512}
2513
Chris Masond20f7042008-12-08 16:58:54 -05002514#endif
2515
Chris Mason323ac952008-10-01 19:05:46 -04002516/*
Chris Mason39279cc2007-06-12 06:35:45 -04002517 * this can truncate away extent items, csum items and directory items.
2518 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04002519 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04002520 *
2521 * csum items that cross the new i_size are truncated to the new size
2522 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04002523 *
2524 * min_type is the minimum key type to truncate down to. If set to 0, this
2525 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04002526 */
Chris Masone02119d2008-09-05 16:13:11 -04002527noinline int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
2528 struct btrfs_root *root,
2529 struct inode *inode,
2530 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04002531{
2532 int ret;
2533 struct btrfs_path *path;
2534 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04002535 struct btrfs_key found_key;
Chris Mason06d9a8d2009-02-04 09:30:58 -05002536 u32 found_type = (u8)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04002537 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002538 struct btrfs_file_extent_item *fi;
2539 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04002540 u64 extent_num_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002541 u64 item_end = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05002542 u64 root_gen = 0;
Chris Masond8d5f3e2007-12-11 12:42:00 -05002543 u64 root_owner = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002544 int found_extent;
2545 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05002546 int pending_del_nr = 0;
2547 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04002548 int extent_type = -1;
Chris Mason771ed682008-11-06 22:02:51 -05002549 int encoding;
Chris Mason3b951512008-04-17 11:29:12 -04002550 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002551
Chris Masone02119d2008-09-05 16:13:11 -04002552 if (root->ref_cows)
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002553 btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002554 path = btrfs_alloc_path();
Chris Mason3c69fae2007-08-07 15:52:22 -04002555 path->reada = -1;
Chris Mason39279cc2007-06-12 06:35:45 -04002556 BUG_ON(!path);
Chris Mason5f39d392007-10-15 16:14:19 -04002557
Chris Mason39279cc2007-06-12 06:35:45 -04002558 /* FIXME, add redo link to tree so we don't leak on crash */
2559 key.objectid = inode->i_ino;
2560 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04002561 key.type = (u8)-1;
2562
Chris Mason85e21ba2008-01-29 15:11:36 -05002563search_again:
Chris Masonb9473432009-03-13 11:00:37 -04002564 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05002565 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masond3977122009-01-05 21:25:51 -05002566 if (ret < 0)
Chris Mason85e21ba2008-01-29 15:11:36 -05002567 goto error;
Chris Masond3977122009-01-05 21:25:51 -05002568
Chris Mason85e21ba2008-01-29 15:11:36 -05002569 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002570 /* there are no items in the tree for us to truncate, we're
2571 * done
2572 */
2573 if (path->slots[0] == 0) {
2574 ret = 0;
2575 goto error;
2576 }
Chris Mason85e21ba2008-01-29 15:11:36 -05002577 path->slots[0]--;
2578 }
2579
Chris Masond3977122009-01-05 21:25:51 -05002580 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04002581 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04002582 leaf = path->nodes[0];
2583 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2584 found_type = btrfs_key_type(&found_key);
Chris Mason771ed682008-11-06 22:02:51 -05002585 encoding = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002586
Chris Mason5f39d392007-10-15 16:14:19 -04002587 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -04002588 break;
Chris Mason5f39d392007-10-15 16:14:19 -04002589
Chris Mason85e21ba2008-01-29 15:11:36 -05002590 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04002591 break;
2592
Chris Mason5f39d392007-10-15 16:14:19 -04002593 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04002594 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04002595 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04002596 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04002597 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Mason771ed682008-11-06 22:02:51 -05002598 encoding = btrfs_file_extent_compression(leaf, fi);
2599 encoding |= btrfs_file_extent_encryption(leaf, fi);
2600 encoding |= btrfs_file_extent_other_encoding(leaf, fi);
2601
Chris Mason179e29e2007-11-01 11:28:41 -04002602 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04002603 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04002604 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04002605 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04002606 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04002607 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04002608 }
Yan008630c2007-11-07 13:31:09 -05002609 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04002610 }
Chris Masone02119d2008-09-05 16:13:11 -04002611 if (item_end < new_size) {
Chris Masond3977122009-01-05 21:25:51 -05002612 if (found_type == BTRFS_DIR_ITEM_KEY)
Chris Masonb888db22007-08-27 16:49:44 -04002613 found_type = BTRFS_INODE_ITEM_KEY;
Chris Masond3977122009-01-05 21:25:51 -05002614 else if (found_type == BTRFS_EXTENT_ITEM_KEY)
Chris Masond20f7042008-12-08 16:58:54 -05002615 found_type = BTRFS_EXTENT_DATA_KEY;
Chris Masond3977122009-01-05 21:25:51 -05002616 else if (found_type == BTRFS_EXTENT_DATA_KEY)
Chris Mason85e21ba2008-01-29 15:11:36 -05002617 found_type = BTRFS_XATTR_ITEM_KEY;
Chris Masond3977122009-01-05 21:25:51 -05002618 else if (found_type == BTRFS_XATTR_ITEM_KEY)
Chris Mason85e21ba2008-01-29 15:11:36 -05002619 found_type = BTRFS_INODE_REF_KEY;
Chris Masond3977122009-01-05 21:25:51 -05002620 else if (found_type)
Chris Masonb888db22007-08-27 16:49:44 -04002621 found_type--;
Chris Masond3977122009-01-05 21:25:51 -05002622 else
Chris Masonb888db22007-08-27 16:49:44 -04002623 break;
Yana61721d2007-09-17 11:08:38 -04002624 btrfs_set_key_type(&key, found_type);
Chris Mason85e21ba2008-01-29 15:11:36 -05002625 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04002626 }
Chris Masone02119d2008-09-05 16:13:11 -04002627 if (found_key.offset >= new_size)
Chris Mason39279cc2007-06-12 06:35:45 -04002628 del_item = 1;
2629 else
2630 del_item = 0;
2631 found_extent = 0;
2632
2633 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04002634 if (found_type != BTRFS_EXTENT_DATA_KEY)
2635 goto delete;
2636
2637 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04002638 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04002639 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason771ed682008-11-06 22:02:51 -05002640 if (!del_item && !encoding) {
Chris Masondb945352007-10-15 16:15:53 -04002641 u64 orig_num_bytes =
2642 btrfs_file_extent_num_bytes(leaf, fi);
Chris Masone02119d2008-09-05 16:13:11 -04002643 extent_num_bytes = new_size -
Chris Mason5f39d392007-10-15 16:14:19 -04002644 found_key.offset + root->sectorsize - 1;
Yanb1632b12008-01-30 11:54:04 -05002645 extent_num_bytes = extent_num_bytes &
2646 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04002647 btrfs_set_file_extent_num_bytes(leaf, fi,
2648 extent_num_bytes);
2649 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05002650 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04002651 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002652 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04002653 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002654 } else {
Chris Masondb945352007-10-15 16:15:53 -04002655 extent_num_bytes =
2656 btrfs_file_extent_disk_num_bytes(leaf,
2657 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04002658 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05002659 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04002660 if (extent_start != 0) {
2661 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04002662 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002663 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04002664 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002665 root_gen = btrfs_header_generation(leaf);
Chris Masond8d5f3e2007-12-11 12:42:00 -05002666 root_owner = btrfs_header_owner(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002667 }
Chris Mason90692182008-02-08 13:49:28 -05002668 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04002669 /*
2670 * we can't truncate inline items that have had
2671 * special encodings
2672 */
2673 if (!del_item &&
2674 btrfs_file_extent_compression(leaf, fi) == 0 &&
2675 btrfs_file_extent_encryption(leaf, fi) == 0 &&
2676 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002677 u32 size = new_size - found_key.offset;
2678
2679 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002680 inode_sub_bytes(inode, item_end + 1 -
2681 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04002682 }
2683 size =
2684 btrfs_file_extent_calc_inline_size(size);
Chris Mason90692182008-02-08 13:49:28 -05002685 ret = btrfs_truncate_item(trans, root, path,
Chris Masone02119d2008-09-05 16:13:11 -04002686 size, 1);
Chris Mason90692182008-02-08 13:49:28 -05002687 BUG_ON(ret);
Chris Masone02119d2008-09-05 16:13:11 -04002688 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002689 inode_sub_bytes(inode, item_end + 1 -
2690 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05002691 }
Chris Mason39279cc2007-06-12 06:35:45 -04002692 }
Chris Mason179e29e2007-11-01 11:28:41 -04002693delete:
Chris Mason39279cc2007-06-12 06:35:45 -04002694 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05002695 if (!pending_del_nr) {
2696 /* no pending yet, add ourselves */
2697 pending_del_slot = path->slots[0];
2698 pending_del_nr = 1;
2699 } else if (pending_del_nr &&
2700 path->slots[0] + 1 == pending_del_slot) {
2701 /* hop on the pending chunk */
2702 pending_del_nr++;
2703 pending_del_slot = path->slots[0];
2704 } else {
Chris Masond3977122009-01-05 21:25:51 -05002705 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05002706 }
Chris Mason39279cc2007-06-12 06:35:45 -04002707 } else {
2708 break;
2709 }
Chris Mason39279cc2007-06-12 06:35:45 -04002710 if (found_extent) {
Chris Masonb9473432009-03-13 11:00:37 -04002711 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002712 ret = btrfs_free_extent(trans, root, extent_start,
Chris Mason7bb86312007-12-11 09:25:06 -05002713 extent_num_bytes,
Zheng Yan31840ae2008-09-23 13:14:14 -04002714 leaf->start, root_owner,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002715 root_gen, inode->i_ino, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002716 BUG_ON(ret);
2717 }
Chris Mason85e21ba2008-01-29 15:11:36 -05002718next:
2719 if (path->slots[0] == 0) {
2720 if (pending_del_nr)
2721 goto del_pending;
2722 btrfs_release_path(root, path);
Chris Mason06d9a8d2009-02-04 09:30:58 -05002723 if (found_type == BTRFS_INODE_ITEM_KEY)
2724 break;
Chris Mason85e21ba2008-01-29 15:11:36 -05002725 goto search_again;
2726 }
2727
2728 path->slots[0]--;
2729 if (pending_del_nr &&
2730 path->slots[0] + 1 != pending_del_slot) {
2731 struct btrfs_key debug;
2732del_pending:
2733 btrfs_item_key_to_cpu(path->nodes[0], &debug,
2734 pending_del_slot);
2735 ret = btrfs_del_items(trans, root, path,
2736 pending_del_slot,
2737 pending_del_nr);
2738 BUG_ON(ret);
2739 pending_del_nr = 0;
2740 btrfs_release_path(root, path);
Chris Mason06d9a8d2009-02-04 09:30:58 -05002741 if (found_type == BTRFS_INODE_ITEM_KEY)
2742 break;
Chris Mason85e21ba2008-01-29 15:11:36 -05002743 goto search_again;
2744 }
Chris Mason39279cc2007-06-12 06:35:45 -04002745 }
2746 ret = 0;
2747error:
Chris Mason85e21ba2008-01-29 15:11:36 -05002748 if (pending_del_nr) {
2749 ret = btrfs_del_items(trans, root, path, pending_del_slot,
2750 pending_del_nr);
2751 }
Chris Mason39279cc2007-06-12 06:35:45 -04002752 btrfs_free_path(path);
2753 inode->i_sb->s_dirt = 1;
2754 return ret;
2755}
2756
Chris Masona52d9a82007-08-27 16:49:44 -04002757/*
2758 * taken from block_truncate_page, but does cow as it zeros out
2759 * any bytes left in the last page in the file.
2760 */
2761static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
2762{
2763 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -04002764 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002765 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2766 struct btrfs_ordered_extent *ordered;
2767 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04002768 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04002769 pgoff_t index = from >> PAGE_CACHE_SHIFT;
2770 unsigned offset = from & (PAGE_CACHE_SIZE-1);
2771 struct page *page;
2772 int ret = 0;
2773 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002774 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04002775
2776 if ((offset & (blocksize - 1)) == 0)
2777 goto out;
2778
2779 ret = -ENOMEM;
Chris Mason211c17f2008-05-15 09:13:45 -04002780again:
Chris Masona52d9a82007-08-27 16:49:44 -04002781 page = grab_cache_page(mapping, index);
2782 if (!page)
2783 goto out;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002784
2785 page_start = page_offset(page);
2786 page_end = page_start + PAGE_CACHE_SIZE - 1;
2787
Chris Masona52d9a82007-08-27 16:49:44 -04002788 if (!PageUptodate(page)) {
2789 ret = btrfs_readpage(NULL, page);
2790 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04002791 if (page->mapping != mapping) {
2792 unlock_page(page);
2793 page_cache_release(page);
2794 goto again;
2795 }
Chris Masona52d9a82007-08-27 16:49:44 -04002796 if (!PageUptodate(page)) {
2797 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04002798 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04002799 }
2800 }
Chris Mason211c17f2008-05-15 09:13:45 -04002801 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002802
2803 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
2804 set_page_extent_mapped(page);
2805
2806 ordered = btrfs_lookup_ordered_extent(inode, page_start);
2807 if (ordered) {
2808 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
2809 unlock_page(page);
2810 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04002811 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002812 btrfs_put_ordered_extent(ordered);
2813 goto again;
2814 }
2815
Chris Masonea8c2812008-08-04 23:17:27 -04002816 btrfs_set_extent_delalloc(inode, page_start, page_end);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002817 ret = 0;
2818 if (offset != PAGE_CACHE_SIZE) {
2819 kaddr = kmap(page);
2820 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
2821 flush_dcache_page(page);
2822 kunmap(page);
2823 }
Chris Mason247e7432008-07-17 12:53:51 -04002824 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002825 set_page_dirty(page);
2826 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04002827
Chris Mason89642222008-07-24 09:41:53 -04002828out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04002829 unlock_page(page);
2830 page_cache_release(page);
2831out:
2832 return ret;
2833}
2834
Yan Zheng9036c102008-10-30 14:19:41 -04002835int btrfs_cont_expand(struct inode *inode, loff_t size)
2836{
2837 struct btrfs_trans_handle *trans;
2838 struct btrfs_root *root = BTRFS_I(inode)->root;
2839 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2840 struct extent_map *em;
2841 u64 mask = root->sectorsize - 1;
2842 u64 hole_start = (inode->i_size + mask) & ~mask;
2843 u64 block_end = (size + mask) & ~mask;
2844 u64 last_byte;
2845 u64 cur_offset;
2846 u64 hole_size;
2847 int err;
2848
2849 if (size <= hole_start)
2850 return 0;
2851
Josef Bacik6a632092009-02-20 11:00:09 -05002852 err = btrfs_check_metadata_free_space(root);
Yan Zheng9036c102008-10-30 14:19:41 -04002853 if (err)
2854 return err;
2855
2856 btrfs_truncate_page(inode->i_mapping, inode->i_size);
2857
2858 while (1) {
2859 struct btrfs_ordered_extent *ordered;
2860 btrfs_wait_ordered_range(inode, hole_start,
2861 block_end - hole_start);
2862 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
2863 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
2864 if (!ordered)
2865 break;
2866 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
2867 btrfs_put_ordered_extent(ordered);
2868 }
2869
2870 trans = btrfs_start_transaction(root, 1);
2871 btrfs_set_trans_block_group(trans, inode);
2872
2873 cur_offset = hole_start;
2874 while (1) {
2875 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
2876 block_end - cur_offset, 0);
2877 BUG_ON(IS_ERR(em) || !em);
2878 last_byte = min(extent_map_end(em), block_end);
2879 last_byte = (last_byte + mask) & ~mask;
2880 if (test_bit(EXTENT_FLAG_VACANCY, &em->flags)) {
Chris Mason771ed682008-11-06 22:02:51 -05002881 u64 hint_byte = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04002882 hole_size = last_byte - cur_offset;
Chris Mason771ed682008-11-06 22:02:51 -05002883 err = btrfs_drop_extents(trans, root, inode,
2884 cur_offset,
2885 cur_offset + hole_size,
Chris Masone980b502009-04-24 14:39:24 -04002886 block_end,
Chris Mason771ed682008-11-06 22:02:51 -05002887 cur_offset, &hint_byte);
2888 if (err)
2889 break;
Yan Zheng9036c102008-10-30 14:19:41 -04002890 err = btrfs_insert_file_extent(trans, root,
2891 inode->i_ino, cur_offset, 0,
2892 0, hole_size, 0, hole_size,
2893 0, 0, 0);
2894 btrfs_drop_extent_cache(inode, hole_start,
2895 last_byte - 1, 0);
2896 }
2897 free_extent_map(em);
2898 cur_offset = last_byte;
2899 if (err || cur_offset >= block_end)
2900 break;
2901 }
2902
2903 btrfs_end_transaction(trans, root);
2904 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
2905 return err;
2906}
2907
Chris Mason39279cc2007-06-12 06:35:45 -04002908static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
2909{
2910 struct inode *inode = dentry->d_inode;
2911 int err;
2912
2913 err = inode_change_ok(inode, attr);
2914 if (err)
2915 return err;
2916
Chris Mason5a3f23d2009-03-31 13:27:11 -04002917 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
2918 if (attr->ia_size > inode->i_size) {
2919 err = btrfs_cont_expand(inode, attr->ia_size);
2920 if (err)
2921 return err;
2922 } else if (inode->i_size > 0 &&
2923 attr->ia_size == 0) {
2924
2925 /* we're truncating a file that used to have good
2926 * data down to zero. Make sure it gets into
2927 * the ordered flush list so that any new writes
2928 * get down to disk quickly.
2929 */
2930 BTRFS_I(inode)->ordered_data_close = 1;
2931 }
Chris Mason39279cc2007-06-12 06:35:45 -04002932 }
Yan Zheng9036c102008-10-30 14:19:41 -04002933
Chris Mason39279cc2007-06-12 06:35:45 -04002934 err = inode_setattr(inode, attr);
Josef Bacik33268ea2008-07-24 12:16:36 -04002935
2936 if (!err && ((attr->ia_valid & ATTR_MODE)))
2937 err = btrfs_acl_chmod(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002938 return err;
2939}
Chris Mason61295eb2008-01-14 16:24:38 -05002940
Chris Mason39279cc2007-06-12 06:35:45 -04002941void btrfs_delete_inode(struct inode *inode)
2942{
2943 struct btrfs_trans_handle *trans;
2944 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002945 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04002946 int ret;
2947
2948 truncate_inode_pages(&inode->i_data, 0);
2949 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04002950 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002951 goto no_delete;
2952 }
Chris Mason4a096752008-07-21 10:29:44 -04002953 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04002954
Chris Masondbe674a2008-07-17 12:54:05 -04002955 btrfs_i_size_write(inode, 0);
Yan Zheng180591b2009-01-06 09:58:06 -05002956 trans = btrfs_join_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002957
Chris Mason39279cc2007-06-12 06:35:45 -04002958 btrfs_set_trans_block_group(trans, inode);
Chris Masone02119d2008-09-05 16:13:11 -04002959 ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size, 0);
Josef Bacik7b128762008-07-24 12:17:14 -04002960 if (ret) {
2961 btrfs_orphan_del(NULL, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04002962 goto no_delete_lock;
Josef Bacik7b128762008-07-24 12:17:14 -04002963 }
2964
2965 btrfs_orphan_del(trans, inode);
Chris Mason85e21ba2008-01-29 15:11:36 -05002966
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002967 nr = trans->blocks_used;
Chris Mason85e21ba2008-01-29 15:11:36 -05002968 clear_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002969
Chris Mason39279cc2007-06-12 06:35:45 -04002970 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002971 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002972 return;
Chris Mason54aa1f42007-06-22 14:16:25 -04002973
2974no_delete_lock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002975 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04002976 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002977 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002978no_delete:
2979 clear_inode(inode);
2980}
2981
2982/*
2983 * this returns the key found in the dir entry in the location pointer.
2984 * If no dir entries were found, location->objectid is 0.
2985 */
2986static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
2987 struct btrfs_key *location)
2988{
2989 const char *name = dentry->d_name.name;
2990 int namelen = dentry->d_name.len;
2991 struct btrfs_dir_item *di;
2992 struct btrfs_path *path;
2993 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04002994 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002995
2996 path = btrfs_alloc_path();
2997 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -05002998
Chris Mason39279cc2007-06-12 06:35:45 -04002999 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
3000 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04003001 if (IS_ERR(di))
3002 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05003003
3004 if (!di || IS_ERR(di))
Chris Mason39544012007-12-12 14:38:19 -05003005 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05003006
Chris Mason5f39d392007-10-15 16:14:19 -04003007 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04003008out:
Chris Mason39279cc2007-06-12 06:35:45 -04003009 btrfs_free_path(path);
3010 return ret;
Chris Mason39544012007-12-12 14:38:19 -05003011out_err:
3012 location->objectid = 0;
3013 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04003014}
3015
3016/*
3017 * when we hit a tree root in a directory, the btrfs part of the inode
3018 * needs to be changed to reflect the root directory of the tree root. This
3019 * is kind of like crossing a mount point.
3020 */
3021static int fixup_tree_root_location(struct btrfs_root *root,
3022 struct btrfs_key *location,
Josef Bacik58176a92007-08-29 15:47:34 -04003023 struct btrfs_root **sub_root,
3024 struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04003025{
Chris Mason39279cc2007-06-12 06:35:45 -04003026 struct btrfs_root_item *ri;
3027
3028 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
3029 return 0;
3030 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
3031 return 0;
3032
Josef Bacik58176a92007-08-29 15:47:34 -04003033 *sub_root = btrfs_read_fs_root(root->fs_info, location,
3034 dentry->d_name.name,
3035 dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04003036 if (IS_ERR(*sub_root))
3037 return PTR_ERR(*sub_root);
3038
3039 ri = &(*sub_root)->root_item;
3040 location->objectid = btrfs_root_dirid(ri);
Chris Mason39279cc2007-06-12 06:35:45 -04003041 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
3042 location->offset = 0;
3043
Chris Mason39279cc2007-06-12 06:35:45 -04003044 return 0;
3045}
3046
Chris Masone02119d2008-09-05 16:13:11 -04003047static noinline void init_btrfs_i(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003048{
Chris Masone02119d2008-09-05 16:13:11 -04003049 struct btrfs_inode *bi = BTRFS_I(inode);
3050
3051 bi->i_acl = NULL;
3052 bi->i_default_acl = NULL;
3053
3054 bi->generation = 0;
Chris Masonc3027eb2008-12-08 16:40:21 -05003055 bi->sequence = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003056 bi->last_trans = 0;
3057 bi->logged_trans = 0;
3058 bi->delalloc_bytes = 0;
Josef Bacik6a632092009-02-20 11:00:09 -05003059 bi->reserved_bytes = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003060 bi->disk_i_size = 0;
3061 bi->flags = 0;
3062 bi->index_cnt = (u64)-1;
Chris Mason12fcfd22009-03-24 10:24:20 -04003063 bi->last_unlink_trans = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003064 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
3065 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04003066 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04003067 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
3068 inode->i_mapping, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04003069 INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes);
Chris Mason5a3f23d2009-03-31 13:27:11 -04003070 INIT_LIST_HEAD(&BTRFS_I(inode)->ordered_operations);
Chris Masonba1da2f2008-07-17 12:54:15 -04003071 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Masonee6e6502008-07-17 12:54:40 -04003072 mutex_init(&BTRFS_I(inode)->extent_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04003073 mutex_init(&BTRFS_I(inode)->log_mutex);
3074}
3075
3076static int btrfs_init_locked_inode(struct inode *inode, void *p)
3077{
3078 struct btrfs_iget_args *args = p;
3079 inode->i_ino = args->ino;
3080 init_btrfs_i(inode);
3081 BTRFS_I(inode)->root = args->root;
Josef Bacik6a632092009-02-20 11:00:09 -05003082 btrfs_set_inode_space_info(args->root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003083 return 0;
3084}
3085
3086static int btrfs_find_actor(struct inode *inode, void *opaque)
3087{
3088 struct btrfs_iget_args *args = opaque;
Chris Masond3977122009-01-05 21:25:51 -05003089 return args->ino == inode->i_ino &&
3090 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003091}
3092
Zheng Yan5b21f2e2008-09-26 10:05:38 -04003093struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
3094 struct btrfs_root *root, int wait)
3095{
3096 struct inode *inode;
3097 struct btrfs_iget_args args;
3098 args.ino = objectid;
3099 args.root = root;
3100
3101 if (wait) {
3102 inode = ilookup5(s, objectid, btrfs_find_actor,
3103 (void *)&args);
3104 } else {
3105 inode = ilookup5_nowait(s, objectid, btrfs_find_actor,
3106 (void *)&args);
3107 }
3108 return inode;
3109}
3110
Chris Mason39279cc2007-06-12 06:35:45 -04003111struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
3112 struct btrfs_root *root)
3113{
3114 struct inode *inode;
3115 struct btrfs_iget_args args;
3116 args.ino = objectid;
3117 args.root = root;
3118
3119 inode = iget5_locked(s, objectid, btrfs_find_actor,
3120 btrfs_init_locked_inode,
3121 (void *)&args);
3122 return inode;
3123}
3124
Balaji Rao1a54ef82008-07-21 02:01:04 +05303125/* Get an inode object given its location and corresponding root.
3126 * Returns in *is_new if the inode was read from disk
3127 */
3128struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
3129 struct btrfs_root *root, int *is_new)
3130{
3131 struct inode *inode;
3132
3133 inode = btrfs_iget_locked(s, location->objectid, root);
3134 if (!inode)
3135 return ERR_PTR(-EACCES);
3136
3137 if (inode->i_state & I_NEW) {
3138 BTRFS_I(inode)->root = root;
3139 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
3140 btrfs_read_locked_inode(inode);
3141 unlock_new_inode(inode);
3142 if (is_new)
3143 *is_new = 1;
3144 } else {
3145 if (is_new)
3146 *is_new = 0;
3147 }
3148
3149 return inode;
3150}
3151
Chris Mason3de45862008-11-17 21:02:50 -05003152struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04003153{
Chris Masond3977122009-01-05 21:25:51 -05003154 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003155 struct btrfs_inode *bi = BTRFS_I(dir);
3156 struct btrfs_root *root = bi->root;
3157 struct btrfs_root *sub_root = root;
3158 struct btrfs_key location;
Yan Zhengc146afa2008-11-12 14:34:12 -05003159 int ret, new;
Chris Mason39279cc2007-06-12 06:35:45 -04003160
3161 if (dentry->d_name.len > BTRFS_NAME_LEN)
3162 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04003163
Chris Mason39279cc2007-06-12 06:35:45 -04003164 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04003165
Chris Mason39279cc2007-06-12 06:35:45 -04003166 if (ret < 0)
3167 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04003168
Chris Mason39279cc2007-06-12 06:35:45 -04003169 inode = NULL;
3170 if (location.objectid) {
Josef Bacik58176a92007-08-29 15:47:34 -04003171 ret = fixup_tree_root_location(root, &location, &sub_root,
3172 dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04003173 if (ret < 0)
3174 return ERR_PTR(ret);
3175 if (ret > 0)
3176 return ERR_PTR(-ENOENT);
Balaji Rao1a54ef82008-07-21 02:01:04 +05303177 inode = btrfs_iget(dir->i_sb, &location, sub_root, &new);
3178 if (IS_ERR(inode))
3179 return ERR_CAST(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003180 }
Chris Mason3de45862008-11-17 21:02:50 -05003181 return inode;
3182}
3183
3184static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
3185 struct nameidata *nd)
3186{
3187 struct inode *inode;
3188
3189 if (dentry->d_name.len > BTRFS_NAME_LEN)
3190 return ERR_PTR(-ENAMETOOLONG);
3191
3192 inode = btrfs_lookup_dentry(dir, dentry);
3193 if (IS_ERR(inode))
3194 return ERR_CAST(inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003195
Chris Mason39279cc2007-06-12 06:35:45 -04003196 return d_splice_alias(inode, dentry);
3197}
3198
Chris Mason39279cc2007-06-12 06:35:45 -04003199static unsigned char btrfs_filetype_table[] = {
3200 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
3201};
3202
David Woodhousecbdf5a22008-08-06 19:42:33 +01003203static int btrfs_real_readdir(struct file *filp, void *dirent,
3204 filldir_t filldir)
Chris Mason39279cc2007-06-12 06:35:45 -04003205{
Chris Mason6da6aba2007-12-18 16:15:09 -05003206 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003207 struct btrfs_root *root = BTRFS_I(inode)->root;
3208 struct btrfs_item *item;
3209 struct btrfs_dir_item *di;
3210 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04003211 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04003212 struct btrfs_path *path;
3213 int ret;
3214 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04003215 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003216 int slot;
3217 int advance;
3218 unsigned char d_type;
3219 int over = 0;
3220 u32 di_cur;
3221 u32 di_total;
3222 u32 di_len;
3223 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04003224 char tmp_name[32];
3225 char *name_ptr;
3226 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04003227
3228 /* FIXME, use a real flag for deciding about the key type */
3229 if (root->fs_info->tree_root == root)
3230 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04003231
Chris Mason39544012007-12-12 14:38:19 -05003232 /* special case for "." */
3233 if (filp->f_pos == 0) {
3234 over = filldir(dirent, ".", 1,
3235 1, inode->i_ino,
3236 DT_DIR);
3237 if (over)
3238 return 0;
3239 filp->f_pos = 1;
3240 }
Chris Mason39544012007-12-12 14:38:19 -05003241 /* special case for .., just use the back ref */
3242 if (filp->f_pos == 1) {
David Woodhouse5ecc7e52008-08-17 15:14:48 +01003243 u64 pino = parent_ino(filp->f_path.dentry);
Chris Mason39544012007-12-12 14:38:19 -05003244 over = filldir(dirent, "..", 2,
David Woodhouse5ecc7e52008-08-17 15:14:48 +01003245 2, pino, DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05003246 if (over)
David Woodhouse49593bf2008-08-17 17:08:36 +01003247 return 0;
Chris Mason39544012007-12-12 14:38:19 -05003248 filp->f_pos = 2;
3249 }
David Woodhouse49593bf2008-08-17 17:08:36 +01003250 path = btrfs_alloc_path();
3251 path->reada = 2;
3252
Chris Mason39279cc2007-06-12 06:35:45 -04003253 btrfs_set_key_type(&key, key_type);
3254 key.offset = filp->f_pos;
David Woodhouse49593bf2008-08-17 17:08:36 +01003255 key.objectid = inode->i_ino;
Chris Mason5f39d392007-10-15 16:14:19 -04003256
Chris Mason39279cc2007-06-12 06:35:45 -04003257 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3258 if (ret < 0)
3259 goto err;
3260 advance = 0;
David Woodhouse49593bf2008-08-17 17:08:36 +01003261
3262 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04003263 leaf = path->nodes[0];
3264 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003265 slot = path->slots[0];
3266 if (advance || slot >= nritems) {
David Woodhouse49593bf2008-08-17 17:08:36 +01003267 if (slot >= nritems - 1) {
Chris Mason39279cc2007-06-12 06:35:45 -04003268 ret = btrfs_next_leaf(root, path);
3269 if (ret)
3270 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003271 leaf = path->nodes[0];
3272 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003273 slot = path->slots[0];
3274 } else {
3275 slot++;
3276 path->slots[0]++;
3277 }
3278 }
Chris Mason3de45862008-11-17 21:02:50 -05003279
Chris Mason39279cc2007-06-12 06:35:45 -04003280 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04003281 item = btrfs_item_nr(leaf, slot);
3282 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3283
3284 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04003285 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003286 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003287 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003288 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04003289 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04003290
3291 filp->f_pos = found_key.offset;
David Woodhouse49593bf2008-08-17 17:08:36 +01003292
Chris Mason39279cc2007-06-12 06:35:45 -04003293 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
3294 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003295 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01003296
3297 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04003298 struct btrfs_key location;
3299
3300 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01003301 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04003302 name_ptr = tmp_name;
3303 } else {
3304 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01003305 if (!name_ptr) {
3306 ret = -ENOMEM;
3307 goto err;
3308 }
Chris Mason5f39d392007-10-15 16:14:19 -04003309 }
3310 read_extent_buffer(leaf, name_ptr,
3311 (unsigned long)(di + 1), name_len);
3312
3313 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
3314 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05003315
3316 /* is this a reference to our own snapshot? If so
3317 * skip it
3318 */
3319 if (location.type == BTRFS_ROOT_ITEM_KEY &&
3320 location.objectid == root->root_key.objectid) {
3321 over = 0;
3322 goto skip;
3323 }
Chris Mason5f39d392007-10-15 16:14:19 -04003324 over = filldir(dirent, name_ptr, name_len,
David Woodhouse49593bf2008-08-17 17:08:36 +01003325 found_key.offset, location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04003326 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04003327
Chris Mason3de45862008-11-17 21:02:50 -05003328skip:
Chris Mason5f39d392007-10-15 16:14:19 -04003329 if (name_ptr != tmp_name)
3330 kfree(name_ptr);
3331
Chris Mason39279cc2007-06-12 06:35:45 -04003332 if (over)
3333 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05003334 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01003335 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04003336 di_cur += di_len;
3337 di = (struct btrfs_dir_item *)((char *)di + di_len);
3338 }
3339 }
David Woodhouse49593bf2008-08-17 17:08:36 +01003340
3341 /* Reached end of directory/root. Bump pos past the last item. */
Yan Zheng5e591a02008-02-19 11:41:02 -05003342 if (key_type == BTRFS_DIR_INDEX_KEY)
Chris Mason89f135d2009-01-28 15:34:27 -05003343 filp->f_pos = INT_LIMIT(off_t);
Yan Zheng5e591a02008-02-19 11:41:02 -05003344 else
3345 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04003346nopos:
3347 ret = 0;
3348err:
Chris Mason39279cc2007-06-12 06:35:45 -04003349 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003350 return ret;
3351}
3352
3353int btrfs_write_inode(struct inode *inode, int wait)
3354{
3355 struct btrfs_root *root = BTRFS_I(inode)->root;
3356 struct btrfs_trans_handle *trans;
3357 int ret = 0;
3358
Yan Zhengc146afa2008-11-12 14:34:12 -05003359 if (root->fs_info->btree_inode == inode)
Chris Mason4ca8b412008-08-05 13:30:48 -04003360 return 0;
3361
Chris Mason39279cc2007-06-12 06:35:45 -04003362 if (wait) {
Chris Masonf9295742008-07-17 12:54:14 -04003363 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003364 btrfs_set_trans_block_group(trans, inode);
3365 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04003366 }
3367 return ret;
3368}
3369
3370/*
Chris Mason54aa1f42007-06-22 14:16:25 -04003371 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04003372 * inode changes. But, it is most likely to find the inode in cache.
3373 * FIXME, needs more benchmarking...there are no reasons other than performance
3374 * to keep or drop this code.
3375 */
3376void btrfs_dirty_inode(struct inode *inode)
3377{
3378 struct btrfs_root *root = BTRFS_I(inode)->root;
3379 struct btrfs_trans_handle *trans;
3380
Chris Masonf9295742008-07-17 12:54:14 -04003381 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003382 btrfs_set_trans_block_group(trans, inode);
3383 btrfs_update_inode(trans, root, inode);
3384 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04003385}
3386
Chris Masond352ac62008-09-29 15:18:18 -04003387/*
3388 * find the highest existing sequence number in a directory
3389 * and then set the in-memory index_cnt variable to reflect
3390 * free sequence numbers
3391 */
Josef Bacikaec74772008-07-24 12:12:38 -04003392static int btrfs_set_inode_index_count(struct inode *inode)
3393{
3394 struct btrfs_root *root = BTRFS_I(inode)->root;
3395 struct btrfs_key key, found_key;
3396 struct btrfs_path *path;
3397 struct extent_buffer *leaf;
3398 int ret;
3399
3400 key.objectid = inode->i_ino;
3401 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
3402 key.offset = (u64)-1;
3403
3404 path = btrfs_alloc_path();
3405 if (!path)
3406 return -ENOMEM;
3407
3408 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3409 if (ret < 0)
3410 goto out;
3411 /* FIXME: we should be able to handle this */
3412 if (ret == 0)
3413 goto out;
3414 ret = 0;
3415
3416 /*
3417 * MAGIC NUMBER EXPLANATION:
3418 * since we search a directory based on f_pos we have to start at 2
3419 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
3420 * else has to start at 2
3421 */
3422 if (path->slots[0] == 0) {
3423 BTRFS_I(inode)->index_cnt = 2;
3424 goto out;
3425 }
3426
3427 path->slots[0]--;
3428
3429 leaf = path->nodes[0];
3430 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3431
3432 if (found_key.objectid != inode->i_ino ||
3433 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
3434 BTRFS_I(inode)->index_cnt = 2;
3435 goto out;
3436 }
3437
3438 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
3439out:
3440 btrfs_free_path(path);
3441 return ret;
3442}
3443
Chris Masond352ac62008-09-29 15:18:18 -04003444/*
3445 * helper to find a free sequence number in a given directory. This current
3446 * code is very simple, later versions will do smarter things in the btree
3447 */
Chris Mason3de45862008-11-17 21:02:50 -05003448int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04003449{
3450 int ret = 0;
3451
3452 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
3453 ret = btrfs_set_inode_index_count(dir);
Chris Masond3977122009-01-05 21:25:51 -05003454 if (ret)
Josef Bacikaec74772008-07-24 12:12:38 -04003455 return ret;
3456 }
3457
Chris Mason00e4e6b2008-08-05 11:18:09 -04003458 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04003459 BTRFS_I(dir)->index_cnt++;
3460
3461 return ret;
3462}
3463
Chris Mason39279cc2007-06-12 06:35:45 -04003464static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
3465 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04003466 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05003467 const char *name, int name_len,
Yan Zhengd2fb3432008-12-11 16:30:39 -05003468 u64 ref_objectid, u64 objectid,
3469 u64 alloc_hint, int mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04003470{
3471 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04003472 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04003473 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04003474 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05003475 struct btrfs_inode_ref *ref;
3476 struct btrfs_key key[2];
3477 u32 sizes[2];
3478 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04003479 int ret;
3480 int owner;
3481
Chris Mason5f39d392007-10-15 16:14:19 -04003482 path = btrfs_alloc_path();
3483 BUG_ON(!path);
3484
Chris Mason39279cc2007-06-12 06:35:45 -04003485 inode = new_inode(root->fs_info->sb);
3486 if (!inode)
3487 return ERR_PTR(-ENOMEM);
3488
Josef Bacikaec74772008-07-24 12:12:38 -04003489 if (dir) {
Chris Mason3de45862008-11-17 21:02:50 -05003490 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04003491 if (ret) {
3492 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04003493 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04003494 }
Josef Bacikaec74772008-07-24 12:12:38 -04003495 }
3496 /*
3497 * index_cnt is ignored for everything but a dir,
3498 * btrfs_get_inode_index_count has an explanation for the magic
3499 * number
3500 */
Chris Masone02119d2008-09-05 16:13:11 -04003501 init_btrfs_i(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04003502 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04003503 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04003504 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik6a632092009-02-20 11:00:09 -05003505 btrfs_set_inode_space_info(root, inode);
Chris Masonb888db22007-08-27 16:49:44 -04003506
Chris Mason39279cc2007-06-12 06:35:45 -04003507 if (mode & S_IFDIR)
3508 owner = 0;
3509 else
3510 owner = 1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003511 BTRFS_I(inode)->block_group =
3512 btrfs_find_block_group(root, 0, alloc_hint, owner);
Yan Zheng17d217f2008-12-12 10:03:38 -05003513 if ((mode & S_IFREG)) {
3514 if (btrfs_test_opt(root, NODATASUM))
3515 btrfs_set_flag(inode, NODATASUM);
3516 if (btrfs_test_opt(root, NODATACOW))
3517 btrfs_set_flag(inode, NODATACOW);
3518 }
Chris Mason9c583092008-01-29 15:15:18 -05003519
3520 key[0].objectid = objectid;
3521 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
3522 key[0].offset = 0;
3523
3524 key[1].objectid = objectid;
3525 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
3526 key[1].offset = ref_objectid;
3527
3528 sizes[0] = sizeof(struct btrfs_inode_item);
3529 sizes[1] = name_len + sizeof(*ref);
3530
Chris Masonb9473432009-03-13 11:00:37 -04003531 path->leave_spinning = 1;
Chris Mason9c583092008-01-29 15:15:18 -05003532 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
3533 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04003534 goto fail;
3535
Chris Mason9c583092008-01-29 15:15:18 -05003536 if (objectid > root->highest_inode)
3537 root->highest_inode = objectid;
3538
Chris Mason79683f22008-11-19 22:00:53 -05003539 inode->i_uid = current_fsuid();
Chris Ball8c087b52009-02-04 09:29:54 -05003540
Chris Mason42f15d72009-02-06 11:35:57 -05003541 if (dir && (dir->i_mode & S_ISGID)) {
Chris Ball8c087b52009-02-04 09:29:54 -05003542 inode->i_gid = dir->i_gid;
3543 if (S_ISDIR(mode))
3544 mode |= S_ISGID;
3545 } else
3546 inode->i_gid = current_fsgid();
3547
Chris Mason39279cc2007-06-12 06:35:45 -04003548 inode->i_mode = mode;
3549 inode->i_ino = objectid;
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003550 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003551 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04003552 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3553 struct btrfs_inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003554 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05003555
3556 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
3557 struct btrfs_inode_ref);
3558 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04003559 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05003560 ptr = (unsigned long)(ref + 1);
3561 write_extent_buffer(path->nodes[0], name, ptr, name_len);
3562
Chris Mason5f39d392007-10-15 16:14:19 -04003563 btrfs_mark_buffer_dirty(path->nodes[0]);
3564 btrfs_free_path(path);
3565
Chris Mason39279cc2007-06-12 06:35:45 -04003566 location = &BTRFS_I(inode)->location;
3567 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04003568 location->offset = 0;
3569 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
3570
Chris Mason39279cc2007-06-12 06:35:45 -04003571 insert_inode_hash(inode);
3572 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04003573fail:
Josef Bacikaec74772008-07-24 12:12:38 -04003574 if (dir)
3575 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04003576 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04003577 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003578 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003579}
3580
3581static inline u8 btrfs_inode_type(struct inode *inode)
3582{
3583 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
3584}
3585
Chris Masond352ac62008-09-29 15:18:18 -04003586/*
3587 * utility function to add 'inode' into 'parent_inode' with
3588 * a give name and a given sequence number.
3589 * if 'add_backref' is true, also insert a backref from the
3590 * inode to the parent directory.
3591 */
Chris Masone02119d2008-09-05 16:13:11 -04003592int btrfs_add_link(struct btrfs_trans_handle *trans,
3593 struct inode *parent_inode, struct inode *inode,
3594 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04003595{
3596 int ret;
3597 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04003598 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Chris Mason5f39d392007-10-15 16:14:19 -04003599
Chris Mason39279cc2007-06-12 06:35:45 -04003600 key.objectid = inode->i_ino;
Chris Mason39279cc2007-06-12 06:35:45 -04003601 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
3602 key.offset = 0;
3603
Chris Masone02119d2008-09-05 16:13:11 -04003604 ret = btrfs_insert_dir_item(trans, root, name, name_len,
3605 parent_inode->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04003606 &key, btrfs_inode_type(inode),
Chris Mason00e4e6b2008-08-05 11:18:09 -04003607 index);
Chris Mason39279cc2007-06-12 06:35:45 -04003608 if (ret == 0) {
Chris Mason9c583092008-01-29 15:15:18 -05003609 if (add_backref) {
3610 ret = btrfs_insert_inode_ref(trans, root,
Chris Masone02119d2008-09-05 16:13:11 -04003611 name, name_len,
3612 inode->i_ino,
3613 parent_inode->i_ino,
3614 index);
Chris Mason9c583092008-01-29 15:15:18 -05003615 }
Chris Masondbe674a2008-07-17 12:54:05 -04003616 btrfs_i_size_write(parent_inode, parent_inode->i_size +
Chris Masone02119d2008-09-05 16:13:11 -04003617 name_len * 2);
Chris Mason79c44582007-06-25 10:09:33 -04003618 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Masone02119d2008-09-05 16:13:11 -04003619 ret = btrfs_update_inode(trans, root, parent_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003620 }
3621 return ret;
3622}
3623
3624static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05003625 struct dentry *dentry, struct inode *inode,
Chris Mason00e4e6b2008-08-05 11:18:09 -04003626 int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04003627{
Chris Masone02119d2008-09-05 16:13:11 -04003628 int err = btrfs_add_link(trans, dentry->d_parent->d_inode,
3629 inode, dentry->d_name.name,
3630 dentry->d_name.len, backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04003631 if (!err) {
3632 d_instantiate(dentry, inode);
3633 return 0;
3634 }
3635 if (err > 0)
3636 err = -EEXIST;
3637 return err;
3638}
3639
Josef Bacik618e21d2007-07-11 10:18:17 -04003640static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
3641 int mode, dev_t rdev)
3642{
3643 struct btrfs_trans_handle *trans;
3644 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05003645 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04003646 int err;
3647 int drop_inode = 0;
3648 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05003649 unsigned long nr = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04003650 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04003651
3652 if (!new_valid_dev(rdev))
3653 return -EINVAL;
3654
Josef Bacik6a632092009-02-20 11:00:09 -05003655 err = btrfs_check_metadata_free_space(root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003656 if (err)
3657 goto fail;
3658
Josef Bacik618e21d2007-07-11 10:18:17 -04003659 trans = btrfs_start_transaction(root, 1);
3660 btrfs_set_trans_block_group(trans, dir);
3661
3662 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3663 if (err) {
3664 err = -ENOSPC;
3665 goto out_unlock;
3666 }
3667
Josef Bacikaec74772008-07-24 12:12:38 -04003668 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05003669 dentry->d_name.len,
3670 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04003671 BTRFS_I(dir)->block_group, mode, &index);
Josef Bacik618e21d2007-07-11 10:18:17 -04003672 err = PTR_ERR(inode);
3673 if (IS_ERR(inode))
3674 goto out_unlock;
3675
Jim Owens0279b4c2009-02-04 09:29:13 -05003676 err = btrfs_init_inode_security(inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04003677 if (err) {
3678 drop_inode = 1;
3679 goto out_unlock;
3680 }
3681
Josef Bacik618e21d2007-07-11 10:18:17 -04003682 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04003683 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04003684 if (err)
3685 drop_inode = 1;
3686 else {
3687 inode->i_op = &btrfs_special_inode_operations;
3688 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04003689 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04003690 }
3691 dir->i_sb->s_dirt = 1;
3692 btrfs_update_inode_block_group(trans, inode);
3693 btrfs_update_inode_block_group(trans, dir);
3694out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003695 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04003696 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003697fail:
Josef Bacik618e21d2007-07-11 10:18:17 -04003698 if (drop_inode) {
3699 inode_dec_link_count(inode);
3700 iput(inode);
3701 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003702 btrfs_btree_balance_dirty(root, nr);
Josef Bacik618e21d2007-07-11 10:18:17 -04003703 return err;
3704}
3705
Chris Mason39279cc2007-06-12 06:35:45 -04003706static int btrfs_create(struct inode *dir, struct dentry *dentry,
3707 int mode, struct nameidata *nd)
3708{
3709 struct btrfs_trans_handle *trans;
3710 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05003711 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04003712 int err;
3713 int drop_inode = 0;
Chris Mason1832a6d2007-12-21 16:27:21 -05003714 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003715 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04003716 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003717
Josef Bacik6a632092009-02-20 11:00:09 -05003718 err = btrfs_check_metadata_free_space(root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003719 if (err)
3720 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04003721 trans = btrfs_start_transaction(root, 1);
3722 btrfs_set_trans_block_group(trans, dir);
3723
3724 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3725 if (err) {
3726 err = -ENOSPC;
3727 goto out_unlock;
3728 }
3729
Josef Bacikaec74772008-07-24 12:12:38 -04003730 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05003731 dentry->d_name.len,
3732 dentry->d_parent->d_inode->i_ino,
Chris Mason00e4e6b2008-08-05 11:18:09 -04003733 objectid, BTRFS_I(dir)->block_group, mode,
3734 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04003735 err = PTR_ERR(inode);
3736 if (IS_ERR(inode))
3737 goto out_unlock;
3738
Jim Owens0279b4c2009-02-04 09:29:13 -05003739 err = btrfs_init_inode_security(inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04003740 if (err) {
3741 drop_inode = 1;
3742 goto out_unlock;
3743 }
3744
Chris Mason39279cc2007-06-12 06:35:45 -04003745 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04003746 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04003747 if (err)
3748 drop_inode = 1;
3749 else {
3750 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003751 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003752 inode->i_fop = &btrfs_file_operations;
3753 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05003754 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003755 }
3756 dir->i_sb->s_dirt = 1;
3757 btrfs_update_inode_block_group(trans, inode);
3758 btrfs_update_inode_block_group(trans, dir);
3759out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003760 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04003761 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003762fail:
Chris Mason39279cc2007-06-12 06:35:45 -04003763 if (drop_inode) {
3764 inode_dec_link_count(inode);
3765 iput(inode);
3766 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003767 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003768 return err;
3769}
3770
3771static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
3772 struct dentry *dentry)
3773{
3774 struct btrfs_trans_handle *trans;
3775 struct btrfs_root *root = BTRFS_I(dir)->root;
3776 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04003777 u64 index;
Chris Mason1832a6d2007-12-21 16:27:21 -05003778 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003779 int err;
3780 int drop_inode = 0;
3781
3782 if (inode->i_nlink == 0)
3783 return -ENOENT;
3784
Chris Masone02119d2008-09-05 16:13:11 -04003785 btrfs_inc_nlink(inode);
Josef Bacik6a632092009-02-20 11:00:09 -05003786 err = btrfs_check_metadata_free_space(root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003787 if (err)
3788 goto fail;
Chris Mason3de45862008-11-17 21:02:50 -05003789 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003790 if (err)
3791 goto fail;
3792
Chris Mason39279cc2007-06-12 06:35:45 -04003793 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003794
Chris Mason39279cc2007-06-12 06:35:45 -04003795 btrfs_set_trans_block_group(trans, dir);
3796 atomic_inc(&inode->i_count);
Josef Bacikaec74772008-07-24 12:12:38 -04003797
Chris Mason00e4e6b2008-08-05 11:18:09 -04003798 err = btrfs_add_nondir(trans, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04003799
Chris Mason39279cc2007-06-12 06:35:45 -04003800 if (err)
3801 drop_inode = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04003802
Chris Mason39279cc2007-06-12 06:35:45 -04003803 dir->i_sb->s_dirt = 1;
3804 btrfs_update_inode_block_group(trans, dir);
Chris Mason54aa1f42007-06-22 14:16:25 -04003805 err = btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003806
Chris Mason54aa1f42007-06-22 14:16:25 -04003807 if (err)
3808 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003809
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003810 nr = trans->blocks_used;
Chris Mason12fcfd22009-03-24 10:24:20 -04003811
3812 btrfs_log_new_name(trans, inode, NULL, dentry->d_parent);
Chris Masonab78c842008-07-29 16:15:18 -04003813 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003814fail:
Chris Mason39279cc2007-06-12 06:35:45 -04003815 if (drop_inode) {
3816 inode_dec_link_count(inode);
3817 iput(inode);
3818 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003819 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003820 return err;
3821}
3822
Chris Mason39279cc2007-06-12 06:35:45 -04003823static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
3824{
Chris Masonb9d86662008-05-02 16:13:49 -04003825 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04003826 struct btrfs_trans_handle *trans;
3827 struct btrfs_root *root = BTRFS_I(dir)->root;
3828 int err = 0;
3829 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04003830 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04003831 u64 index = 0;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003832 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003833
Josef Bacik6a632092009-02-20 11:00:09 -05003834 err = btrfs_check_metadata_free_space(root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003835 if (err)
3836 goto out_unlock;
3837
Chris Mason39279cc2007-06-12 06:35:45 -04003838 trans = btrfs_start_transaction(root, 1);
3839 btrfs_set_trans_block_group(trans, dir);
Chris Mason5f39d392007-10-15 16:14:19 -04003840
Chris Mason39279cc2007-06-12 06:35:45 -04003841 if (IS_ERR(trans)) {
3842 err = PTR_ERR(trans);
3843 goto out_unlock;
3844 }
3845
3846 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3847 if (err) {
3848 err = -ENOSPC;
3849 goto out_unlock;
3850 }
3851
Josef Bacikaec74772008-07-24 12:12:38 -04003852 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05003853 dentry->d_name.len,
3854 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04003855 BTRFS_I(dir)->block_group, S_IFDIR | mode,
3856 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04003857 if (IS_ERR(inode)) {
3858 err = PTR_ERR(inode);
3859 goto out_fail;
3860 }
Chris Mason5f39d392007-10-15 16:14:19 -04003861
Chris Mason39279cc2007-06-12 06:35:45 -04003862 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04003863
Jim Owens0279b4c2009-02-04 09:29:13 -05003864 err = btrfs_init_inode_security(inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04003865 if (err)
3866 goto out_fail;
3867
Chris Mason39279cc2007-06-12 06:35:45 -04003868 inode->i_op = &btrfs_dir_inode_operations;
3869 inode->i_fop = &btrfs_dir_file_operations;
3870 btrfs_set_trans_block_group(trans, inode);
3871
Chris Masondbe674a2008-07-17 12:54:05 -04003872 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003873 err = btrfs_update_inode(trans, root, inode);
3874 if (err)
3875 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04003876
Chris Masone02119d2008-09-05 16:13:11 -04003877 err = btrfs_add_link(trans, dentry->d_parent->d_inode,
3878 inode, dentry->d_name.name,
3879 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04003880 if (err)
3881 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04003882
Chris Mason39279cc2007-06-12 06:35:45 -04003883 d_instantiate(dentry, inode);
3884 drop_on_err = 0;
3885 dir->i_sb->s_dirt = 1;
3886 btrfs_update_inode_block_group(trans, inode);
3887 btrfs_update_inode_block_group(trans, dir);
3888
3889out_fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003890 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04003891 btrfs_end_transaction_throttle(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04003892
Chris Mason39279cc2007-06-12 06:35:45 -04003893out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04003894 if (drop_on_err)
3895 iput(inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003896 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003897 return err;
3898}
3899
Chris Masond352ac62008-09-29 15:18:18 -04003900/* helper for btfs_get_extent. Given an existing extent in the tree,
3901 * and an extent that you want to insert, deal with overlap and insert
3902 * the new extent into the tree.
3903 */
Chris Mason3b951512008-04-17 11:29:12 -04003904static int merge_extent_mapping(struct extent_map_tree *em_tree,
3905 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003906 struct extent_map *em,
3907 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04003908{
3909 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04003910
Chris Masone6dcd2d2008-07-17 12:53:50 -04003911 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
3912 start_diff = map_start - em->start;
3913 em->start = map_start;
3914 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04003915 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
3916 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04003917 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04003918 em->block_len -= start_diff;
3919 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003920 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04003921}
3922
Chris Masonc8b97812008-10-29 14:49:59 -04003923static noinline int uncompress_inline(struct btrfs_path *path,
3924 struct inode *inode, struct page *page,
3925 size_t pg_offset, u64 extent_offset,
3926 struct btrfs_file_extent_item *item)
3927{
3928 int ret;
3929 struct extent_buffer *leaf = path->nodes[0];
3930 char *tmp;
3931 size_t max_size;
3932 unsigned long inline_size;
3933 unsigned long ptr;
3934
3935 WARN_ON(pg_offset != 0);
3936 max_size = btrfs_file_extent_ram_bytes(leaf, item);
3937 inline_size = btrfs_file_extent_inline_item_len(leaf,
3938 btrfs_item_nr(leaf, path->slots[0]));
3939 tmp = kmalloc(inline_size, GFP_NOFS);
3940 ptr = btrfs_file_extent_inline_start(item);
3941
3942 read_extent_buffer(leaf, tmp, ptr, inline_size);
3943
Chris Mason5b050f02008-11-11 09:34:41 -05003944 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04003945 ret = btrfs_zlib_decompress(tmp, page, extent_offset,
3946 inline_size, max_size);
3947 if (ret) {
3948 char *kaddr = kmap_atomic(page, KM_USER0);
3949 unsigned long copy_size = min_t(u64,
3950 PAGE_CACHE_SIZE - pg_offset,
3951 max_size - extent_offset);
3952 memset(kaddr + pg_offset, 0, copy_size);
3953 kunmap_atomic(kaddr, KM_USER0);
3954 }
3955 kfree(tmp);
3956 return 0;
3957}
3958
Chris Masond352ac62008-09-29 15:18:18 -04003959/*
3960 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05003961 * the ugly parts come from merging extents from the disk with the in-ram
3962 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04003963 * where the in-ram extents might be locked pending data=ordered completion.
3964 *
3965 * This also copies inline extents directly into the page.
3966 */
Chris Masond3977122009-01-05 21:25:51 -05003967
Chris Masona52d9a82007-08-27 16:49:44 -04003968struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05003969 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04003970 int create)
3971{
3972 int ret;
3973 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04003974 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04003975 u64 extent_start = 0;
3976 u64 extent_end = 0;
3977 u64 objectid = inode->i_ino;
3978 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04003979 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04003980 struct btrfs_root *root = BTRFS_I(inode)->root;
3981 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04003982 struct extent_buffer *leaf;
3983 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04003984 struct extent_map *em = NULL;
3985 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05003986 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04003987 struct btrfs_trans_handle *trans = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04003988 int compressed;
Chris Masona52d9a82007-08-27 16:49:44 -04003989
Chris Masona52d9a82007-08-27 16:49:44 -04003990again:
Chris Masond1310b22008-01-24 16:13:08 -05003991 spin_lock(&em_tree->lock);
3992 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04003993 if (em)
3994 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05003995 spin_unlock(&em_tree->lock);
3996
Chris Masona52d9a82007-08-27 16:49:44 -04003997 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04003998 if (em->start > start || em->start + em->len <= start)
3999 free_extent_map(em);
4000 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05004001 free_extent_map(em);
4002 else
4003 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004004 }
Chris Masond1310b22008-01-24 16:13:08 -05004005 em = alloc_extent_map(GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04004006 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05004007 err = -ENOMEM;
4008 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004009 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004010 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05004011 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05004012 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05004013 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04004014 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04004015
4016 if (!path) {
4017 path = btrfs_alloc_path();
4018 BUG_ON(!path);
4019 }
4020
Chris Mason179e29e2007-11-01 11:28:41 -04004021 ret = btrfs_lookup_file_extent(trans, root, path,
4022 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04004023 if (ret < 0) {
4024 err = ret;
4025 goto out;
4026 }
4027
4028 if (ret != 0) {
4029 if (path->slots[0] == 0)
4030 goto not_found;
4031 path->slots[0]--;
4032 }
4033
Chris Mason5f39d392007-10-15 16:14:19 -04004034 leaf = path->nodes[0];
4035 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04004036 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04004037 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04004038 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4039 found_type = btrfs_key_type(&found_key);
4040 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04004041 found_type != BTRFS_EXTENT_DATA_KEY) {
4042 goto not_found;
4043 }
4044
Chris Mason5f39d392007-10-15 16:14:19 -04004045 found_type = btrfs_file_extent_type(leaf, item);
4046 extent_start = found_key.offset;
Chris Masonc8b97812008-10-29 14:49:59 -04004047 compressed = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04004048 if (found_type == BTRFS_FILE_EXTENT_REG ||
4049 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04004050 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04004051 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04004052 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
4053 size_t size;
4054 size = btrfs_file_extent_inline_len(leaf, item);
4055 extent_end = (extent_start + size + root->sectorsize - 1) &
4056 ~((u64)root->sectorsize - 1);
4057 }
4058
4059 if (start >= extent_end) {
4060 path->slots[0]++;
4061 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
4062 ret = btrfs_next_leaf(root, path);
4063 if (ret < 0) {
4064 err = ret;
4065 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004066 }
Yan Zheng9036c102008-10-30 14:19:41 -04004067 if (ret > 0)
4068 goto not_found;
4069 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04004070 }
Yan Zheng9036c102008-10-30 14:19:41 -04004071 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4072 if (found_key.objectid != objectid ||
4073 found_key.type != BTRFS_EXTENT_DATA_KEY)
4074 goto not_found;
4075 if (start + len <= found_key.offset)
4076 goto not_found;
4077 em->start = start;
4078 em->len = found_key.offset - start;
4079 goto not_found_em;
4080 }
4081
Yan Zhengd899e052008-10-30 14:25:28 -04004082 if (found_type == BTRFS_FILE_EXTENT_REG ||
4083 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04004084 em->start = extent_start;
4085 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05004086 em->orig_start = extent_start -
4087 btrfs_file_extent_offset(leaf, item);
Chris Masondb945352007-10-15 16:15:53 -04004088 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
4089 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004090 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04004091 goto insert;
4092 }
Chris Masonc8b97812008-10-29 14:49:59 -04004093 if (compressed) {
4094 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
4095 em->block_start = bytenr;
4096 em->block_len = btrfs_file_extent_disk_num_bytes(leaf,
4097 item);
4098 } else {
4099 bytenr += btrfs_file_extent_offset(leaf, item);
4100 em->block_start = bytenr;
4101 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04004102 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
4103 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04004104 }
Chris Masona52d9a82007-08-27 16:49:44 -04004105 goto insert;
4106 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004107 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04004108 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04004109 size_t size;
4110 size_t extent_offset;
4111 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04004112
Chris Masona52d9a82007-08-27 16:49:44 -04004113 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04004114 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04004115 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04004116 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04004117 goto out;
4118 }
4119
Yan Zheng9036c102008-10-30 14:19:41 -04004120 size = btrfs_file_extent_inline_len(leaf, item);
4121 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05004122 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04004123 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04004124 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05004125 em->len = (copy_size + root->sectorsize - 1) &
4126 ~((u64)root->sectorsize - 1);
Yan Zhengff5b7ee2008-11-10 07:34:43 -05004127 em->orig_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04004128 if (compressed)
4129 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Yan689f9342007-10-29 11:41:07 -04004130 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04004131 if (create == 0 && !PageUptodate(page)) {
Chris Masonc8b97812008-10-29 14:49:59 -04004132 if (btrfs_file_extent_compression(leaf, item) ==
4133 BTRFS_COMPRESS_ZLIB) {
4134 ret = uncompress_inline(path, inode, page,
4135 pg_offset,
4136 extent_offset, item);
4137 BUG_ON(ret);
4138 } else {
4139 map = kmap(page);
4140 read_extent_buffer(leaf, map + pg_offset, ptr,
4141 copy_size);
4142 kunmap(page);
4143 }
Chris Mason179e29e2007-11-01 11:28:41 -04004144 flush_dcache_page(page);
4145 } else if (create && PageUptodate(page)) {
4146 if (!trans) {
4147 kunmap(page);
4148 free_extent_map(em);
4149 em = NULL;
4150 btrfs_release_path(root, path);
Chris Masonf9295742008-07-17 12:54:14 -04004151 trans = btrfs_join_transaction(root, 1);
Chris Mason179e29e2007-11-01 11:28:41 -04004152 goto again;
4153 }
Chris Masonc8b97812008-10-29 14:49:59 -04004154 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05004155 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04004156 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04004157 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04004158 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04004159 }
Chris Masond1310b22008-01-24 16:13:08 -05004160 set_extent_uptodate(io_tree, em->start,
4161 extent_map_end(em) - 1, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04004162 goto insert;
4163 } else {
Chris Masond3977122009-01-05 21:25:51 -05004164 printk(KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04004165 WARN_ON(1);
4166 }
4167not_found:
4168 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05004169 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04004170not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04004171 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04004172 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04004173insert:
4174 btrfs_release_path(root, path);
Chris Masond1310b22008-01-24 16:13:08 -05004175 if (em->start > start || extent_map_end(em) <= start) {
Chris Masond3977122009-01-05 21:25:51 -05004176 printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
4177 "[%llu %llu]\n", (unsigned long long)em->start,
4178 (unsigned long long)em->len,
4179 (unsigned long long)start,
4180 (unsigned long long)len);
Chris Masona52d9a82007-08-27 16:49:44 -04004181 err = -EIO;
4182 goto out;
4183 }
Chris Masond1310b22008-01-24 16:13:08 -05004184
4185 err = 0;
4186 spin_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04004187 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04004188 /* it is possible that someone inserted the extent into the tree
4189 * while we had the lock dropped. It is also possible that
4190 * an overlapping map exists in the tree
4191 */
Chris Masona52d9a82007-08-27 16:49:44 -04004192 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04004193 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004194
4195 ret = 0;
4196
Chris Mason3b951512008-04-17 11:29:12 -04004197 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04004198 if (existing && (existing->start > start ||
4199 existing->start + existing->len <= start)) {
4200 free_extent_map(existing);
4201 existing = NULL;
4202 }
Chris Mason3b951512008-04-17 11:29:12 -04004203 if (!existing) {
4204 existing = lookup_extent_mapping(em_tree, em->start,
4205 em->len);
4206 if (existing) {
4207 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04004208 em, start,
4209 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04004210 free_extent_map(existing);
4211 if (err) {
4212 free_extent_map(em);
4213 em = NULL;
4214 }
4215 } else {
4216 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04004217 free_extent_map(em);
4218 em = NULL;
4219 }
4220 } else {
4221 free_extent_map(em);
4222 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004223 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04004224 }
Chris Masona52d9a82007-08-27 16:49:44 -04004225 }
Chris Masond1310b22008-01-24 16:13:08 -05004226 spin_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04004227out:
Chris Masonf4219502008-07-22 11:18:09 -04004228 if (path)
4229 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04004230 if (trans) {
4231 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05004232 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04004233 err = ret;
4234 }
Chris Masona52d9a82007-08-27 16:49:44 -04004235 if (err) {
4236 free_extent_map(em);
4237 WARN_ON(1);
4238 return ERR_PTR(err);
4239 }
4240 return em;
4241}
4242
Chris Mason16432982008-04-10 10:23:21 -04004243static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
4244 const struct iovec *iov, loff_t offset,
4245 unsigned long nr_segs)
4246{
Chris Masone1c4b742008-04-22 13:26:46 -04004247 return -EINVAL;
Chris Mason16432982008-04-10 10:23:21 -04004248}
4249
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004250static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
4251 __u64 start, __u64 len)
4252{
4253 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent);
4254}
4255
Chris Mason9ebefb182007-06-15 13:50:00 -04004256int btrfs_readpage(struct file *file, struct page *page)
4257{
Chris Masond1310b22008-01-24 16:13:08 -05004258 struct extent_io_tree *tree;
4259 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04004260 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04004261}
Chris Mason1832a6d2007-12-21 16:27:21 -05004262
Chris Mason39279cc2007-06-12 06:35:45 -04004263static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
4264{
Chris Masond1310b22008-01-24 16:13:08 -05004265 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04004266
4267
4268 if (current->flags & PF_MEMALLOC) {
4269 redirty_page_for_writepage(wbc, page);
4270 unlock_page(page);
4271 return 0;
4272 }
Chris Masond1310b22008-01-24 16:13:08 -05004273 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04004274 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
4275}
Chris Mason39279cc2007-06-12 06:35:45 -04004276
Chris Masonf4219502008-07-22 11:18:09 -04004277int btrfs_writepages(struct address_space *mapping,
4278 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04004279{
Chris Masond1310b22008-01-24 16:13:08 -05004280 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05004281
Chris Masond1310b22008-01-24 16:13:08 -05004282 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04004283 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
4284}
4285
Chris Mason3ab2fb52007-11-08 10:59:22 -05004286static int
4287btrfs_readpages(struct file *file, struct address_space *mapping,
4288 struct list_head *pages, unsigned nr_pages)
4289{
Chris Masond1310b22008-01-24 16:13:08 -05004290 struct extent_io_tree *tree;
4291 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05004292 return extent_readpages(tree, mapping, pages, nr_pages,
4293 btrfs_get_extent);
4294}
Chris Masone6dcd2d2008-07-17 12:53:50 -04004295static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04004296{
Chris Masond1310b22008-01-24 16:13:08 -05004297 struct extent_io_tree *tree;
4298 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04004299 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004300
Chris Masond1310b22008-01-24 16:13:08 -05004301 tree = &BTRFS_I(page->mapping->host)->io_tree;
4302 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05004303 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04004304 if (ret == 1) {
4305 ClearPagePrivate(page);
4306 set_page_private(page, 0);
4307 page_cache_release(page);
4308 }
4309 return ret;
4310}
Chris Mason39279cc2007-06-12 06:35:45 -04004311
Chris Masone6dcd2d2008-07-17 12:53:50 -04004312static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
4313{
Chris Mason98509cf2008-09-11 15:51:43 -04004314 if (PageWriteback(page) || PageDirty(page))
4315 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05004316 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004317}
4318
Chris Masona52d9a82007-08-27 16:49:44 -04004319static void btrfs_invalidatepage(struct page *page, unsigned long offset)
4320{
Chris Masond1310b22008-01-24 16:13:08 -05004321 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004322 struct btrfs_ordered_extent *ordered;
4323 u64 page_start = page_offset(page);
4324 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04004325
Chris Masone6dcd2d2008-07-17 12:53:50 -04004326 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05004327 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004328 if (offset) {
4329 btrfs_releasepage(page, GFP_NOFS);
4330 return;
4331 }
4332
4333 lock_extent(tree, page_start, page_end, GFP_NOFS);
4334 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
4335 page_offset(page));
4336 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04004337 /*
4338 * IO on this page will never be started, so we need
4339 * to account for any ordered extents now
4340 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04004341 clear_extent_bit(tree, page_start, page_end,
4342 EXTENT_DIRTY | EXTENT_DELALLOC |
4343 EXTENT_LOCKED, 1, 0, GFP_NOFS);
Chris Mason211f90e2008-07-18 11:56:15 -04004344 btrfs_finish_ordered_io(page->mapping->host,
4345 page_start, page_end);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004346 btrfs_put_ordered_extent(ordered);
4347 lock_extent(tree, page_start, page_end, GFP_NOFS);
4348 }
4349 clear_extent_bit(tree, page_start, page_end,
4350 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
4351 EXTENT_ORDERED,
4352 1, 1, GFP_NOFS);
4353 __btrfs_releasepage(page, GFP_NOFS);
4354
Chris Mason4a096752008-07-21 10:29:44 -04004355 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04004356 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04004357 ClearPagePrivate(page);
4358 set_page_private(page, 0);
4359 page_cache_release(page);
4360 }
Chris Mason39279cc2007-06-12 06:35:45 -04004361}
4362
Chris Mason9ebefb182007-06-15 13:50:00 -04004363/*
4364 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
4365 * called from a page fault handler when a page is first dirtied. Hence we must
4366 * be careful to check for EOF conditions here. We set the page up correctly
4367 * for a written page which means we get ENOSPC checking when writing into
4368 * holes and correct delalloc and unwritten extent mapping on filesystems that
4369 * support these features.
4370 *
4371 * We are not allowed to take the i_mutex here so we have to play games to
4372 * protect against truncate races as the page could now be beyond EOF. Because
4373 * vmtruncate() writes the inode size before removing pages, once we have the
4374 * page lock we can determine safely if the page is beyond EOF. If it is not
4375 * beyond EOF, then the page is guaranteed safe against truncation until we
4376 * unlock the page.
4377 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07004378int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04004379{
Nick Pigginc2ec1752009-03-31 15:23:21 -07004380 struct page *page = vmf->page;
Chris Mason6da6aba2007-12-18 16:15:09 -05004381 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05004382 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004383 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4384 struct btrfs_ordered_extent *ordered;
4385 char *kaddr;
4386 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04004387 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05004388 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04004389 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004390 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04004391
Josef Bacik6a632092009-02-20 11:00:09 -05004392 ret = btrfs_check_data_free_space(root, inode, PAGE_CACHE_SIZE);
Nick Piggin56a76f82009-03-31 15:23:23 -07004393 if (ret) {
4394 if (ret == -ENOMEM)
4395 ret = VM_FAULT_OOM;
4396 else /* -ENOSPC, -EIO, etc */
4397 ret = VM_FAULT_SIGBUS;
Chris Mason1832a6d2007-12-21 16:27:21 -05004398 goto out;
Nick Piggin56a76f82009-03-31 15:23:23 -07004399 }
Chris Mason1832a6d2007-12-21 16:27:21 -05004400
Nick Piggin56a76f82009-03-31 15:23:23 -07004401 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04004402again:
Chris Mason9ebefb182007-06-15 13:50:00 -04004403 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04004404 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004405 page_start = page_offset(page);
4406 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04004407
Chris Mason9ebefb182007-06-15 13:50:00 -04004408 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04004409 (page_start >= size)) {
Josef Bacik6a632092009-02-20 11:00:09 -05004410 btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
Chris Mason9ebefb182007-06-15 13:50:00 -04004411 /* page got truncated out from underneath us */
4412 goto out_unlock;
4413 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004414 wait_on_page_writeback(page);
4415
4416 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
4417 set_page_extent_mapped(page);
4418
Chris Masoneb84ae02008-07-17 13:53:27 -04004419 /*
4420 * we can't set the delalloc bits if there are pending ordered
4421 * extents. Drop our locks and wait for them to finish
4422 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04004423 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4424 if (ordered) {
4425 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
4426 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004427 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004428 btrfs_put_ordered_extent(ordered);
4429 goto again;
4430 }
4431
Chris Masonea8c2812008-08-04 23:17:27 -04004432 btrfs_set_extent_delalloc(inode, page_start, page_end);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004433 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04004434
4435 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04004436 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04004437 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04004438 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04004439 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04004440
Chris Masone6dcd2d2008-07-17 12:53:50 -04004441 if (zero_start != PAGE_CACHE_SIZE) {
4442 kaddr = kmap(page);
4443 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
4444 flush_dcache_page(page);
4445 kunmap(page);
4446 }
Chris Mason247e7432008-07-17 12:53:51 -04004447 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004448 set_page_dirty(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04004449
4450 BTRFS_I(inode)->last_trans = root->fs_info->generation + 1;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004451 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04004452
4453out_unlock:
4454 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05004455out:
Chris Mason9ebefb182007-06-15 13:50:00 -04004456 return ret;
4457}
4458
Chris Mason39279cc2007-06-12 06:35:45 -04004459static void btrfs_truncate(struct inode *inode)
4460{
4461 struct btrfs_root *root = BTRFS_I(inode)->root;
4462 int ret;
4463 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004464 unsigned long nr;
Chris Masondbe674a2008-07-17 12:54:05 -04004465 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04004466
4467 if (!S_ISREG(inode->i_mode))
4468 return;
4469 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
4470 return;
4471
4472 btrfs_truncate_page(inode->i_mapping, inode->i_size);
Chris Mason4a096752008-07-21 10:29:44 -04004473 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04004474
Chris Mason39279cc2007-06-12 06:35:45 -04004475 trans = btrfs_start_transaction(root, 1);
Chris Mason5a3f23d2009-03-31 13:27:11 -04004476
4477 /*
4478 * setattr is responsible for setting the ordered_data_close flag,
4479 * but that is only tested during the last file release. That
4480 * could happen well after the next commit, leaving a great big
4481 * window where new writes may get lost if someone chooses to write
4482 * to this file after truncating to zero
4483 *
4484 * The inode doesn't have any dirty data here, and so if we commit
4485 * this is a noop. If someone immediately starts writing to the inode
4486 * it is very likely we'll catch some of their writes in this
4487 * transaction, and the commit will find this file on the ordered
4488 * data list with good things to send down.
4489 *
4490 * This is a best effort solution, there is still a window where
4491 * using truncate to replace the contents of the file will
4492 * end up with a zero length file after a crash.
4493 */
4494 if (inode->i_size == 0 && BTRFS_I(inode)->ordered_data_close)
4495 btrfs_add_ordered_operation(trans, root, inode);
4496
Chris Mason39279cc2007-06-12 06:35:45 -04004497 btrfs_set_trans_block_group(trans, inode);
Chris Masondbe674a2008-07-17 12:54:05 -04004498 btrfs_i_size_write(inode, inode->i_size);
Chris Mason39279cc2007-06-12 06:35:45 -04004499
Josef Bacik7b128762008-07-24 12:17:14 -04004500 ret = btrfs_orphan_add(trans, inode);
4501 if (ret)
4502 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04004503 /* FIXME, add redo link to tree so we don't leak on crash */
Chris Masone02119d2008-09-05 16:13:11 -04004504 ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size,
Chris Mason85e21ba2008-01-29 15:11:36 -05004505 BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004506 btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004507
Josef Bacik7b128762008-07-24 12:17:14 -04004508 ret = btrfs_orphan_del(trans, inode);
4509 BUG_ON(ret);
4510
4511out:
4512 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04004513 ret = btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004514 BUG_ON(ret);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004515 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004516}
4517
Sven Wegener3b963622008-06-09 21:57:42 -04004518/*
Chris Masond352ac62008-09-29 15:18:18 -04004519 * create a new subvolume directory/inode (helper for the ioctl).
4520 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05004521int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
4522 struct btrfs_root *new_root, struct dentry *dentry,
4523 u64 new_dirid, u64 alloc_hint)
Chris Mason39279cc2007-06-12 06:35:45 -04004524{
Chris Mason39279cc2007-06-12 06:35:45 -04004525 struct inode *inode;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04004526 int error;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004527 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004528
Josef Bacikaec74772008-07-24 12:12:38 -04004529 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
Yan Zhengd2fb3432008-12-11 16:30:39 -05004530 new_dirid, alloc_hint, S_IFDIR | 0700, &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04004531 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004532 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004533 inode->i_op = &btrfs_dir_inode_operations;
4534 inode->i_fop = &btrfs_dir_file_operations;
4535
Chris Mason39279cc2007-06-12 06:35:45 -04004536 inode->i_nlink = 1;
Chris Masondbe674a2008-07-17 12:54:05 -04004537 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04004538
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04004539 error = btrfs_update_inode(trans, new_root, inode);
4540 if (error)
4541 return error;
4542
4543 d_instantiate(dentry, inode);
4544 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004545}
4546
Chris Masond352ac62008-09-29 15:18:18 -04004547/* helper function for file defrag and space balancing. This
4548 * forces readahead on a given range of bytes in an inode
4549 */
Chris Masonedbd8d42007-12-21 16:27:24 -05004550unsigned long btrfs_force_ra(struct address_space *mapping,
Chris Mason86479a02007-09-10 19:58:16 -04004551 struct file_ra_state *ra, struct file *file,
4552 pgoff_t offset, pgoff_t last_index)
4553{
Chris Mason8e7bf942008-04-28 09:02:36 -04004554 pgoff_t req_size = last_index - offset + 1;
Chris Mason86479a02007-09-10 19:58:16 -04004555
Chris Mason86479a02007-09-10 19:58:16 -04004556 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
4557 return offset + req_size;
Chris Mason86479a02007-09-10 19:58:16 -04004558}
4559
Chris Mason39279cc2007-06-12 06:35:45 -04004560struct inode *btrfs_alloc_inode(struct super_block *sb)
4561{
4562 struct btrfs_inode *ei;
4563
4564 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
4565 if (!ei)
4566 return NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04004567 ei->last_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004568 ei->logged_trans = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004569 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Josef Bacik33268ea2008-07-24 12:16:36 -04004570 ei->i_acl = BTRFS_ACL_NOT_CACHED;
4571 ei->i_default_acl = BTRFS_ACL_NOT_CACHED;
Josef Bacik7b128762008-07-24 12:17:14 -04004572 INIT_LIST_HEAD(&ei->i_orphan);
Chris Mason5a3f23d2009-03-31 13:27:11 -04004573 INIT_LIST_HEAD(&ei->ordered_operations);
Chris Mason39279cc2007-06-12 06:35:45 -04004574 return &ei->vfs_inode;
4575}
4576
4577void btrfs_destroy_inode(struct inode *inode)
4578{
Chris Masone6dcd2d2008-07-17 12:53:50 -04004579 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04004580 struct btrfs_root *root = BTRFS_I(inode)->root;
4581
Chris Mason39279cc2007-06-12 06:35:45 -04004582 WARN_ON(!list_empty(&inode->i_dentry));
4583 WARN_ON(inode->i_data.nrpages);
4584
Josef Bacik33268ea2008-07-24 12:16:36 -04004585 if (BTRFS_I(inode)->i_acl &&
4586 BTRFS_I(inode)->i_acl != BTRFS_ACL_NOT_CACHED)
4587 posix_acl_release(BTRFS_I(inode)->i_acl);
4588 if (BTRFS_I(inode)->i_default_acl &&
4589 BTRFS_I(inode)->i_default_acl != BTRFS_ACL_NOT_CACHED)
4590 posix_acl_release(BTRFS_I(inode)->i_default_acl);
4591
Chris Mason5a3f23d2009-03-31 13:27:11 -04004592 /*
4593 * Make sure we're properly removed from the ordered operation
4594 * lists.
4595 */
4596 smp_mb();
4597 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
4598 spin_lock(&root->fs_info->ordered_extent_lock);
4599 list_del_init(&BTRFS_I(inode)->ordered_operations);
4600 spin_unlock(&root->fs_info->ordered_extent_lock);
4601 }
4602
4603 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04004604 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
4605 printk(KERN_ERR "BTRFS: inode %lu: inode still on the orphan"
4606 " list\n", inode->i_ino);
4607 dump_stack();
4608 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04004609 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04004610
Chris Masond3977122009-01-05 21:25:51 -05004611 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04004612 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
4613 if (!ordered)
4614 break;
4615 else {
Chris Masond3977122009-01-05 21:25:51 -05004616 printk(KERN_ERR "btrfs found ordered "
4617 "extent %llu %llu on inode cleanup\n",
4618 (unsigned long long)ordered->file_offset,
4619 (unsigned long long)ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004620 btrfs_remove_ordered_extent(inode, ordered);
4621 btrfs_put_ordered_extent(ordered);
4622 btrfs_put_ordered_extent(ordered);
4623 }
4624 }
Zheng Yan5b21f2e2008-09-26 10:05:38 -04004625 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004626 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
4627}
4628
Sven Wegener0ee0fda2008-07-30 16:54:26 -04004629static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04004630{
4631 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
4632
4633 inode_init_once(&ei->vfs_inode);
4634}
4635
4636void btrfs_destroy_cachep(void)
4637{
4638 if (btrfs_inode_cachep)
4639 kmem_cache_destroy(btrfs_inode_cachep);
4640 if (btrfs_trans_handle_cachep)
4641 kmem_cache_destroy(btrfs_trans_handle_cachep);
4642 if (btrfs_transaction_cachep)
4643 kmem_cache_destroy(btrfs_transaction_cachep);
4644 if (btrfs_bit_radix_cachep)
4645 kmem_cache_destroy(btrfs_bit_radix_cachep);
4646 if (btrfs_path_cachep)
4647 kmem_cache_destroy(btrfs_path_cachep);
4648}
4649
4650int btrfs_init_cachep(void)
4651{
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02004652 btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
4653 sizeof(struct btrfs_inode), 0,
4654 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04004655 if (!btrfs_inode_cachep)
4656 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02004657
4658 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
4659 sizeof(struct btrfs_trans_handle), 0,
4660 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004661 if (!btrfs_trans_handle_cachep)
4662 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02004663
4664 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
4665 sizeof(struct btrfs_transaction), 0,
4666 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004667 if (!btrfs_transaction_cachep)
4668 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02004669
4670 btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
4671 sizeof(struct btrfs_path), 0,
4672 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004673 if (!btrfs_path_cachep)
4674 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02004675
4676 btrfs_bit_radix_cachep = kmem_cache_create("btrfs_radix", 256, 0,
4677 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD |
4678 SLAB_DESTROY_BY_RCU, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004679 if (!btrfs_bit_radix_cachep)
4680 goto fail;
4681 return 0;
4682fail:
4683 btrfs_destroy_cachep();
4684 return -ENOMEM;
4685}
4686
4687static int btrfs_getattr(struct vfsmount *mnt,
4688 struct dentry *dentry, struct kstat *stat)
4689{
4690 struct inode *inode = dentry->d_inode;
4691 generic_fillattr(inode, stat);
Chris Mason3394e162008-11-17 20:42:26 -05004692 stat->dev = BTRFS_I(inode)->root->anon_super.s_dev;
Chris Masond6667462008-01-03 14:51:00 -05004693 stat->blksize = PAGE_CACHE_SIZE;
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004694 stat->blocks = (inode_get_bytes(inode) +
4695 BTRFS_I(inode)->delalloc_bytes) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04004696 return 0;
4697}
4698
Chris Masond3977122009-01-05 21:25:51 -05004699static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
4700 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04004701{
4702 struct btrfs_trans_handle *trans;
4703 struct btrfs_root *root = BTRFS_I(old_dir)->root;
4704 struct inode *new_inode = new_dentry->d_inode;
4705 struct inode *old_inode = old_dentry->d_inode;
4706 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004707 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004708 int ret;
4709
Chris Mason3394e162008-11-17 20:42:26 -05004710 /* we're not allowed to rename between subvolumes */
4711 if (BTRFS_I(old_inode)->root->root_key.objectid !=
4712 BTRFS_I(new_dir)->root->root_key.objectid)
4713 return -EXDEV;
4714
Chris Mason39279cc2007-06-12 06:35:45 -04004715 if (S_ISDIR(old_inode->i_mode) && new_inode &&
4716 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
4717 return -ENOTEMPTY;
4718 }
Chris Mason5f39d392007-10-15 16:14:19 -04004719
Chris Mason0660b5a2008-11-17 20:37:39 -05004720 /* to rename a snapshot or subvolume, we need to juggle the
4721 * backrefs. This isn't coded yet
4722 */
4723 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
4724 return -EXDEV;
4725
Josef Bacik6a632092009-02-20 11:00:09 -05004726 ret = btrfs_check_metadata_free_space(root);
Chris Mason1832a6d2007-12-21 16:27:21 -05004727 if (ret)
4728 goto out_unlock;
4729
Chris Mason5a3f23d2009-03-31 13:27:11 -04004730 /*
4731 * we're using rename to replace one file with another.
4732 * and the replacement file is large. Start IO on it now so
4733 * we don't add too much work to the end of the transaction
4734 */
4735 if (new_inode && old_inode && S_ISREG(old_inode->i_mode) &&
4736 new_inode->i_size &&
4737 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
4738 filemap_flush(old_inode->i_mapping);
4739
Chris Mason39279cc2007-06-12 06:35:45 -04004740 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04004741
Chris Mason12fcfd22009-03-24 10:24:20 -04004742 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04004743 * make sure the inode gets flushed if it is replacing
4744 * something.
4745 */
4746 if (new_inode && new_inode->i_size &&
4747 old_inode && S_ISREG(old_inode->i_mode)) {
4748 btrfs_add_ordered_operation(trans, root, old_inode);
4749 }
4750
4751 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04004752 * this is an ugly little race, but the rename is required to make
4753 * sure that if we crash, the inode is either at the old name
4754 * or the new one. pinning the log transaction lets us make sure
4755 * we don't allow a log commit to come in after we unlink the
4756 * name but before we add the new name back in.
4757 */
4758 btrfs_pin_log_trans(root);
4759
Chris Mason39279cc2007-06-12 06:35:45 -04004760 btrfs_set_trans_block_group(trans, new_dir);
Chris Mason39279cc2007-06-12 06:35:45 -04004761
Chris Masone02119d2008-09-05 16:13:11 -04004762 btrfs_inc_nlink(old_dentry->d_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004763 old_dir->i_ctime = old_dir->i_mtime = ctime;
4764 new_dir->i_ctime = new_dir->i_mtime = ctime;
4765 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04004766
Chris Mason12fcfd22009-03-24 10:24:20 -04004767 if (old_dentry->d_parent != new_dentry->d_parent)
4768 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
4769
Chris Masone02119d2008-09-05 16:13:11 -04004770 ret = btrfs_unlink_inode(trans, root, old_dir, old_dentry->d_inode,
4771 old_dentry->d_name.name,
4772 old_dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04004773 if (ret)
4774 goto out_fail;
4775
4776 if (new_inode) {
4777 new_inode->i_ctime = CURRENT_TIME;
Chris Masone02119d2008-09-05 16:13:11 -04004778 ret = btrfs_unlink_inode(trans, root, new_dir,
4779 new_dentry->d_inode,
4780 new_dentry->d_name.name,
4781 new_dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04004782 if (ret)
4783 goto out_fail;
Josef Bacik7b128762008-07-24 12:17:14 -04004784 if (new_inode->i_nlink == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004785 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Josef Bacik7b128762008-07-24 12:17:14 -04004786 if (ret)
4787 goto out_fail;
4788 }
Chris Masone02119d2008-09-05 16:13:11 -04004789
Chris Mason39279cc2007-06-12 06:35:45 -04004790 }
Chris Mason3de45862008-11-17 21:02:50 -05004791 ret = btrfs_set_inode_index(new_dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004792 if (ret)
4793 goto out_fail;
4794
Chris Masone02119d2008-09-05 16:13:11 -04004795 ret = btrfs_add_link(trans, new_dentry->d_parent->d_inode,
4796 old_inode, new_dentry->d_name.name,
4797 new_dentry->d_name.len, 1, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004798 if (ret)
4799 goto out_fail;
4800
Chris Mason12fcfd22009-03-24 10:24:20 -04004801 btrfs_log_new_name(trans, old_inode, old_dir,
4802 new_dentry->d_parent);
Chris Mason39279cc2007-06-12 06:35:45 -04004803out_fail:
Chris Mason12fcfd22009-03-24 10:24:20 -04004804
4805 /* this btrfs_end_log_trans just allows the current
4806 * log-sub transaction to complete
4807 */
4808 btrfs_end_log_trans(root);
Chris Masonab78c842008-07-29 16:15:18 -04004809 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05004810out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04004811 return ret;
4812}
4813
Chris Masond352ac62008-09-29 15:18:18 -04004814/*
4815 * some fairly slow code that needs optimization. This walks the list
4816 * of all the inodes with pending delalloc and forces them to disk.
4817 */
Chris Masonea8c2812008-08-04 23:17:27 -04004818int btrfs_start_delalloc_inodes(struct btrfs_root *root)
4819{
4820 struct list_head *head = &root->fs_info->delalloc_inodes;
4821 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04004822 struct inode *inode;
Chris Masonea8c2812008-08-04 23:17:27 -04004823
Yan Zhengc146afa2008-11-12 14:34:12 -05004824 if (root->fs_info->sb->s_flags & MS_RDONLY)
4825 return -EROFS;
4826
Chris Mason75eff682008-12-15 15:54:40 -05004827 spin_lock(&root->fs_info->delalloc_lock);
Chris Masond3977122009-01-05 21:25:51 -05004828 while (!list_empty(head)) {
Chris Masonea8c2812008-08-04 23:17:27 -04004829 binode = list_entry(head->next, struct btrfs_inode,
4830 delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04004831 inode = igrab(&binode->vfs_inode);
4832 if (!inode)
4833 list_del_init(&binode->delalloc_inodes);
Chris Mason75eff682008-12-15 15:54:40 -05004834 spin_unlock(&root->fs_info->delalloc_lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04004835 if (inode) {
Chris Mason8c8bee12008-09-29 11:19:10 -04004836 filemap_flush(inode->i_mapping);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04004837 iput(inode);
4838 }
4839 cond_resched();
Chris Mason75eff682008-12-15 15:54:40 -05004840 spin_lock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04004841 }
Chris Mason75eff682008-12-15 15:54:40 -05004842 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04004843
4844 /* the filemap_flush will queue IO into the worker threads, but
4845 * we have to make sure the IO is actually started and that
4846 * ordered extents get created before we return
4847 */
4848 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05004849 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05004850 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04004851 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05004852 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
4853 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04004854 }
4855 atomic_dec(&root->fs_info->async_submit_draining);
Chris Masonea8c2812008-08-04 23:17:27 -04004856 return 0;
4857}
4858
Chris Mason39279cc2007-06-12 06:35:45 -04004859static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
4860 const char *symname)
4861{
4862 struct btrfs_trans_handle *trans;
4863 struct btrfs_root *root = BTRFS_I(dir)->root;
4864 struct btrfs_path *path;
4865 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05004866 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04004867 int err;
4868 int drop_inode = 0;
4869 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004870 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04004871 int name_len;
4872 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04004873 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04004874 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04004875 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05004876 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004877
4878 name_len = strlen(symname) + 1;
4879 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
4880 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05004881
Josef Bacik6a632092009-02-20 11:00:09 -05004882 err = btrfs_check_metadata_free_space(root);
Chris Mason1832a6d2007-12-21 16:27:21 -05004883 if (err)
4884 goto out_fail;
4885
Chris Mason39279cc2007-06-12 06:35:45 -04004886 trans = btrfs_start_transaction(root, 1);
4887 btrfs_set_trans_block_group(trans, dir);
4888
4889 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
4890 if (err) {
4891 err = -ENOSPC;
4892 goto out_unlock;
4893 }
4894
Josef Bacikaec74772008-07-24 12:12:38 -04004895 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05004896 dentry->d_name.len,
4897 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04004898 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO,
4899 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04004900 err = PTR_ERR(inode);
4901 if (IS_ERR(inode))
4902 goto out_unlock;
4903
Jim Owens0279b4c2009-02-04 09:29:13 -05004904 err = btrfs_init_inode_security(inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04004905 if (err) {
4906 drop_inode = 1;
4907 goto out_unlock;
4908 }
4909
Chris Mason39279cc2007-06-12 06:35:45 -04004910 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04004911 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004912 if (err)
4913 drop_inode = 1;
4914 else {
4915 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04004916 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04004917 inode->i_fop = &btrfs_file_operations;
4918 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05004919 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04004920 }
4921 dir->i_sb->s_dirt = 1;
4922 btrfs_update_inode_block_group(trans, inode);
4923 btrfs_update_inode_block_group(trans, dir);
4924 if (drop_inode)
4925 goto out_unlock;
4926
4927 path = btrfs_alloc_path();
4928 BUG_ON(!path);
4929 key.objectid = inode->i_ino;
4930 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004931 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
4932 datasize = btrfs_file_extent_calc_inline_size(name_len);
4933 err = btrfs_insert_empty_item(trans, root, path, &key,
4934 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04004935 if (err) {
4936 drop_inode = 1;
4937 goto out_unlock;
4938 }
Chris Mason5f39d392007-10-15 16:14:19 -04004939 leaf = path->nodes[0];
4940 ei = btrfs_item_ptr(leaf, path->slots[0],
4941 struct btrfs_file_extent_item);
4942 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
4943 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04004944 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04004945 btrfs_set_file_extent_encryption(leaf, ei, 0);
4946 btrfs_set_file_extent_compression(leaf, ei, 0);
4947 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
4948 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
4949
Chris Mason39279cc2007-06-12 06:35:45 -04004950 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04004951 write_extent_buffer(leaf, symname, ptr, name_len);
4952 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004953 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04004954
Chris Mason39279cc2007-06-12 06:35:45 -04004955 inode->i_op = &btrfs_symlink_inode_operations;
4956 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04004957 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04004958 inode_set_bytes(inode, name_len);
Chris Masondbe674a2008-07-17 12:54:05 -04004959 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04004960 err = btrfs_update_inode(trans, root, inode);
4961 if (err)
4962 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04004963
4964out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004965 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04004966 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05004967out_fail:
Chris Mason39279cc2007-06-12 06:35:45 -04004968 if (drop_inode) {
4969 inode_dec_link_count(inode);
4970 iput(inode);
4971 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004972 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004973 return err;
4974}
Chris Mason16432982008-04-10 10:23:21 -04004975
Chris Mason546888d2009-04-21 11:53:38 -04004976static int prealloc_file_range(struct btrfs_trans_handle *trans,
4977 struct inode *inode, u64 start, u64 end,
Chris Masone980b502009-04-24 14:39:24 -04004978 u64 locked_end, u64 alloc_hint, int mode)
Yan Zhengd899e052008-10-30 14:25:28 -04004979{
Yan Zhengd899e052008-10-30 14:25:28 -04004980 struct btrfs_root *root = BTRFS_I(inode)->root;
4981 struct btrfs_key ins;
4982 u64 alloc_size;
4983 u64 cur_offset = start;
4984 u64 num_bytes = end - start;
4985 int ret = 0;
4986
Yan Zhengd899e052008-10-30 14:25:28 -04004987 while (num_bytes > 0) {
4988 alloc_size = min(num_bytes, root->fs_info->max_extent);
4989 ret = btrfs_reserve_extent(trans, root, alloc_size,
4990 root->sectorsize, 0, alloc_hint,
4991 (u64)-1, &ins, 1);
4992 if (ret) {
4993 WARN_ON(1);
4994 goto out;
4995 }
4996 ret = insert_reserved_file_extent(trans, inode,
4997 cur_offset, ins.objectid,
4998 ins.offset, ins.offset,
Chris Masone980b502009-04-24 14:39:24 -04004999 ins.offset, locked_end,
5000 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04005001 BTRFS_FILE_EXTENT_PREALLOC);
5002 BUG_ON(ret);
5003 num_bytes -= ins.offset;
5004 cur_offset += ins.offset;
5005 alloc_hint = ins.objectid + ins.offset;
5006 }
5007out:
5008 if (cur_offset > start) {
5009 inode->i_ctime = CURRENT_TIME;
5010 btrfs_set_flag(inode, PREALLOC);
5011 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
5012 cur_offset > i_size_read(inode))
5013 btrfs_i_size_write(inode, cur_offset);
5014 ret = btrfs_update_inode(trans, root, inode);
5015 BUG_ON(ret);
5016 }
5017
Yan Zhengd899e052008-10-30 14:25:28 -04005018 return ret;
5019}
5020
5021static long btrfs_fallocate(struct inode *inode, int mode,
5022 loff_t offset, loff_t len)
5023{
5024 u64 cur_offset;
5025 u64 last_byte;
5026 u64 alloc_start;
5027 u64 alloc_end;
5028 u64 alloc_hint = 0;
Chris Masone980b502009-04-24 14:39:24 -04005029 u64 locked_end;
Yan Zhengd899e052008-10-30 14:25:28 -04005030 u64 mask = BTRFS_I(inode)->root->sectorsize - 1;
5031 struct extent_map *em;
Chris Mason546888d2009-04-21 11:53:38 -04005032 struct btrfs_trans_handle *trans;
Yan Zhengd899e052008-10-30 14:25:28 -04005033 int ret;
5034
5035 alloc_start = offset & ~mask;
5036 alloc_end = (offset + len + mask) & ~mask;
5037
Chris Mason546888d2009-04-21 11:53:38 -04005038 /*
5039 * wait for ordered IO before we have any locks. We'll loop again
5040 * below with the locks held.
5041 */
5042 btrfs_wait_ordered_range(inode, alloc_start, alloc_end - alloc_start);
5043
Yan Zhengd899e052008-10-30 14:25:28 -04005044 mutex_lock(&inode->i_mutex);
5045 if (alloc_start > inode->i_size) {
5046 ret = btrfs_cont_expand(inode, alloc_start);
5047 if (ret)
5048 goto out;
5049 }
5050
Chris Masone980b502009-04-24 14:39:24 -04005051 locked_end = alloc_end - 1;
Yan Zhengd899e052008-10-30 14:25:28 -04005052 while (1) {
5053 struct btrfs_ordered_extent *ordered;
Chris Mason546888d2009-04-21 11:53:38 -04005054
5055 trans = btrfs_start_transaction(BTRFS_I(inode)->root, 1);
5056 if (!trans) {
5057 ret = -EIO;
5058 goto out;
5059 }
5060
5061 /* the extent lock is ordered inside the running
5062 * transaction
5063 */
Chris Masone980b502009-04-24 14:39:24 -04005064 lock_extent(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
5065 GFP_NOFS);
Yan Zhengd899e052008-10-30 14:25:28 -04005066 ordered = btrfs_lookup_first_ordered_extent(inode,
5067 alloc_end - 1);
5068 if (ordered &&
5069 ordered->file_offset + ordered->len > alloc_start &&
5070 ordered->file_offset < alloc_end) {
5071 btrfs_put_ordered_extent(ordered);
5072 unlock_extent(&BTRFS_I(inode)->io_tree,
Chris Masone980b502009-04-24 14:39:24 -04005073 alloc_start, locked_end, GFP_NOFS);
Chris Mason546888d2009-04-21 11:53:38 -04005074 btrfs_end_transaction(trans, BTRFS_I(inode)->root);
5075
5076 /*
5077 * we can't wait on the range with the transaction
5078 * running or with the extent lock held
5079 */
Yan Zhengd899e052008-10-30 14:25:28 -04005080 btrfs_wait_ordered_range(inode, alloc_start,
5081 alloc_end - alloc_start);
5082 } else {
5083 if (ordered)
5084 btrfs_put_ordered_extent(ordered);
5085 break;
5086 }
5087 }
5088
5089 cur_offset = alloc_start;
5090 while (1) {
5091 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
5092 alloc_end - cur_offset, 0);
5093 BUG_ON(IS_ERR(em) || !em);
5094 last_byte = min(extent_map_end(em), alloc_end);
5095 last_byte = (last_byte + mask) & ~mask;
5096 if (em->block_start == EXTENT_MAP_HOLE) {
Chris Mason546888d2009-04-21 11:53:38 -04005097 ret = prealloc_file_range(trans, inode, cur_offset,
Chris Masone980b502009-04-24 14:39:24 -04005098 last_byte, locked_end + 1,
5099 alloc_hint, mode);
Yan Zhengd899e052008-10-30 14:25:28 -04005100 if (ret < 0) {
5101 free_extent_map(em);
5102 break;
5103 }
5104 }
5105 if (em->block_start <= EXTENT_MAP_LAST_BYTE)
5106 alloc_hint = em->block_start;
5107 free_extent_map(em);
5108
5109 cur_offset = last_byte;
5110 if (cur_offset >= alloc_end) {
5111 ret = 0;
5112 break;
5113 }
5114 }
Chris Masone980b502009-04-24 14:39:24 -04005115 unlock_extent(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
Yan Zhengd899e052008-10-30 14:25:28 -04005116 GFP_NOFS);
Chris Mason546888d2009-04-21 11:53:38 -04005117
5118 btrfs_end_transaction(trans, BTRFS_I(inode)->root);
Yan Zhengd899e052008-10-30 14:25:28 -04005119out:
5120 mutex_unlock(&inode->i_mutex);
5121 return ret;
5122}
5123
Chris Masone6dcd2d2008-07-17 12:53:50 -04005124static int btrfs_set_page_dirty(struct page *page)
5125{
Chris Masone6dcd2d2008-07-17 12:53:50 -04005126 return __set_page_dirty_nobuffers(page);
5127}
5128
Sven Wegener0ee0fda2008-07-30 16:54:26 -04005129static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05005130{
5131 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
5132 return -EACCES;
Josef Bacik33268ea2008-07-24 12:16:36 -04005133 return generic_permission(inode, mask, btrfs_check_acl);
Yanfdebe2b2008-01-14 13:26:08 -05005134}
Chris Mason39279cc2007-06-12 06:35:45 -04005135
5136static struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05005137 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04005138 .lookup = btrfs_lookup,
5139 .create = btrfs_create,
5140 .unlink = btrfs_unlink,
5141 .link = btrfs_link,
5142 .mkdir = btrfs_mkdir,
5143 .rmdir = btrfs_rmdir,
5144 .rename = btrfs_rename,
5145 .symlink = btrfs_symlink,
5146 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04005147 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04005148 .setxattr = btrfs_setxattr,
5149 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05005150 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04005151 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05005152 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04005153};
Chris Mason39279cc2007-06-12 06:35:45 -04005154static struct inode_operations btrfs_dir_ro_inode_operations = {
5155 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05005156 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04005157};
Chris Mason39279cc2007-06-12 06:35:45 -04005158static struct file_operations btrfs_dir_file_operations = {
5159 .llseek = generic_file_llseek,
5160 .read = generic_read_dir,
David Woodhousecbdf5a22008-08-06 19:42:33 +01005161 .readdir = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04005162 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04005163#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04005164 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04005165#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04005166 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04005167 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04005168};
5169
Chris Masond1310b22008-01-24 16:13:08 -05005170static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04005171 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05005172 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04005173 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04005174 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005175 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04005176 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Mason1259ab72008-05-12 13:39:03 -04005177 .readpage_io_failed_hook = btrfs_io_failed_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05005178 .set_bit_hook = btrfs_set_bit_hook,
5179 .clear_bit_hook = btrfs_clear_bit_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04005180};
5181
Chris Mason35054392009-01-21 13:11:13 -05005182/*
5183 * btrfs doesn't support the bmap operation because swapfiles
5184 * use bmap to make a mapping of extents in the file. They assume
5185 * these extents won't change over the life of the file and they
5186 * use the bmap result to do IO directly to the drive.
5187 *
5188 * the btrfs bmap call would return logical addresses that aren't
5189 * suitable for IO and they also will change frequently as COW
5190 * operations happen. So, swapfile + btrfs == corruption.
5191 *
5192 * For now we're avoiding this by dropping bmap.
5193 */
Chris Mason39279cc2007-06-12 06:35:45 -04005194static struct address_space_operations btrfs_aops = {
5195 .readpage = btrfs_readpage,
5196 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04005197 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05005198 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04005199 .sync_page = block_sync_page,
Chris Mason16432982008-04-10 10:23:21 -04005200 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04005201 .invalidatepage = btrfs_invalidatepage,
5202 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005203 .set_page_dirty = btrfs_set_page_dirty,
Chris Mason39279cc2007-06-12 06:35:45 -04005204};
5205
5206static struct address_space_operations btrfs_symlink_aops = {
5207 .readpage = btrfs_readpage,
5208 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04005209 .invalidatepage = btrfs_invalidatepage,
5210 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04005211};
5212
5213static struct inode_operations btrfs_file_inode_operations = {
5214 .truncate = btrfs_truncate,
5215 .getattr = btrfs_getattr,
5216 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04005217 .setxattr = btrfs_setxattr,
5218 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05005219 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04005220 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05005221 .permission = btrfs_permission,
Yan Zhengd899e052008-10-30 14:25:28 -04005222 .fallocate = btrfs_fallocate,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05005223 .fiemap = btrfs_fiemap,
Chris Mason39279cc2007-06-12 06:35:45 -04005224};
Josef Bacik618e21d2007-07-11 10:18:17 -04005225static struct inode_operations btrfs_special_inode_operations = {
5226 .getattr = btrfs_getattr,
5227 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05005228 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04005229 .setxattr = btrfs_setxattr,
5230 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04005231 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04005232 .removexattr = btrfs_removexattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04005233};
Chris Mason39279cc2007-06-12 06:35:45 -04005234static struct inode_operations btrfs_symlink_inode_operations = {
5235 .readlink = generic_readlink,
5236 .follow_link = page_follow_link_light,
5237 .put_link = page_put_link,
Yanfdebe2b2008-01-14 13:26:08 -05005238 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05005239 .setxattr = btrfs_setxattr,
5240 .getxattr = btrfs_getxattr,
5241 .listxattr = btrfs_listxattr,
5242 .removexattr = btrfs_removexattr,
Chris Mason39279cc2007-06-12 06:35:45 -04005243};