blob: e9b76bcd1c129e0d1f4664f54fefd33caba72e08 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Mason8f18cf12008-04-25 16:53:30 -040019#include <linux/kernel.h>
Chris Mason065631f2008-02-20 12:07:25 -050020#include <linux/bio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040021#include <linux/buffer_head.h>
Sage Weilf2eb0a22008-05-02 14:43:14 -040022#include <linux/file.h>
Chris Mason39279cc2007-06-12 06:35:45 -040023#include <linux/fs.h>
24#include <linux/pagemap.h>
25#include <linux/highmem.h>
26#include <linux/time.h>
27#include <linux/init.h>
28#include <linux/string.h>
Chris Mason39279cc2007-06-12 06:35:45 -040029#include <linux/backing-dev.h>
30#include <linux/mpage.h>
31#include <linux/swap.h>
32#include <linux/writeback.h>
33#include <linux/statfs.h>
34#include <linux/compat.h>
Chris Mason9ebefb182007-06-15 13:50:00 -040035#include <linux/bit_spinlock.h>
Josef Bacik5103e942007-11-16 11:45:54 -050036#include <linux/xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040037#include <linux/posix_acl.h>
Yan Zhengd899e052008-10-30 14:25:28 -040038#include <linux/falloc.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050039#include "compat.h"
Chris Mason39279cc2007-06-12 06:35:45 -040040#include "ctree.h"
41#include "disk-io.h"
42#include "transaction.h"
43#include "btrfs_inode.h"
44#include "ioctl.h"
45#include "print-tree.h"
Chris Mason0b86a832008-03-24 15:01:56 -040046#include "volumes.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040047#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040048#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040049#include "tree-log.h"
Chris Masonc8b97812008-10-29 14:49:59 -040050#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050051#include "locking.h"
Chris Mason39279cc2007-06-12 06:35:45 -040052
53struct btrfs_iget_args {
54 u64 ino;
55 struct btrfs_root *root;
56};
57
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070058static const struct inode_operations btrfs_dir_inode_operations;
59static const struct inode_operations btrfs_symlink_inode_operations;
60static const struct inode_operations btrfs_dir_ro_inode_operations;
61static const struct inode_operations btrfs_special_inode_operations;
62static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070063static const struct address_space_operations btrfs_aops;
64static const struct address_space_operations btrfs_symlink_aops;
Chris Mason39279cc2007-06-12 06:35:45 -040065static struct file_operations btrfs_dir_file_operations;
Chris Masond1310b22008-01-24 16:13:08 -050066static struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040067
68static struct kmem_cache *btrfs_inode_cachep;
69struct kmem_cache *btrfs_trans_handle_cachep;
70struct kmem_cache *btrfs_transaction_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040071struct kmem_cache *btrfs_path_cachep;
72
73#define S_SHIFT 12
74static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
75 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
76 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
77 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
78 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
79 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
80 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
81 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
82};
83
Josef Bacik7b128762008-07-24 12:17:14 -040084static void btrfs_truncate(struct inode *inode);
Chris Masonc8b97812008-10-29 14:49:59 -040085static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end);
Chris Mason771ed682008-11-06 22:02:51 -050086static noinline int cow_file_range(struct inode *inode,
87 struct page *locked_page,
88 u64 start, u64 end, int *page_started,
89 unsigned long *nr_written, int unlock);
Josef Bacik7b128762008-07-24 12:17:14 -040090
Jim Owens0279b4c2009-02-04 09:29:13 -050091static int btrfs_init_inode_security(struct inode *inode, struct inode *dir)
92{
93 int err;
94
95 err = btrfs_init_acl(inode, dir);
96 if (!err)
97 err = btrfs_xattr_security_init(inode, dir);
98 return err;
99}
100
Chris Masond352ac62008-09-29 15:18:18 -0400101/*
Chris Masonc8b97812008-10-29 14:49:59 -0400102 * this does all the hard work for inserting an inline extent into
103 * the btree. The caller should have done a btrfs_drop_extents so that
104 * no overlapping inline items exist in the btree
105 */
Chris Masond3977122009-01-05 21:25:51 -0500106static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400107 struct btrfs_root *root, struct inode *inode,
108 u64 start, size_t size, size_t compressed_size,
109 struct page **compressed_pages)
110{
111 struct btrfs_key key;
112 struct btrfs_path *path;
113 struct extent_buffer *leaf;
114 struct page *page = NULL;
115 char *kaddr;
116 unsigned long ptr;
117 struct btrfs_file_extent_item *ei;
118 int err = 0;
119 int ret;
120 size_t cur_size = size;
121 size_t datasize;
122 unsigned long offset;
123 int use_compress = 0;
124
125 if (compressed_size && compressed_pages) {
126 use_compress = 1;
127 cur_size = compressed_size;
128 }
129
Chris Masond3977122009-01-05 21:25:51 -0500130 path = btrfs_alloc_path();
131 if (!path)
Chris Masonc8b97812008-10-29 14:49:59 -0400132 return -ENOMEM;
133
Chris Masonb9473432009-03-13 11:00:37 -0400134 path->leave_spinning = 1;
Chris Masonc8b97812008-10-29 14:49:59 -0400135 btrfs_set_trans_block_group(trans, inode);
136
137 key.objectid = inode->i_ino;
138 key.offset = start;
139 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
Chris Masonc8b97812008-10-29 14:49:59 -0400140 datasize = btrfs_file_extent_calc_inline_size(cur_size);
141
142 inode_add_bytes(inode, size);
143 ret = btrfs_insert_empty_item(trans, root, path, &key,
144 datasize);
145 BUG_ON(ret);
146 if (ret) {
147 err = ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400148 goto fail;
149 }
150 leaf = path->nodes[0];
151 ei = btrfs_item_ptr(leaf, path->slots[0],
152 struct btrfs_file_extent_item);
153 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
154 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
155 btrfs_set_file_extent_encryption(leaf, ei, 0);
156 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
157 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
158 ptr = btrfs_file_extent_inline_start(ei);
159
160 if (use_compress) {
161 struct page *cpage;
162 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500163 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400164 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500165 cur_size = min_t(unsigned long, compressed_size,
Chris Masonc8b97812008-10-29 14:49:59 -0400166 PAGE_CACHE_SIZE);
167
Chris Masonb9473432009-03-13 11:00:37 -0400168 kaddr = kmap_atomic(cpage, KM_USER0);
Chris Masonc8b97812008-10-29 14:49:59 -0400169 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Chris Masonb9473432009-03-13 11:00:37 -0400170 kunmap_atomic(kaddr, KM_USER0);
Chris Masonc8b97812008-10-29 14:49:59 -0400171
172 i++;
173 ptr += cur_size;
174 compressed_size -= cur_size;
175 }
176 btrfs_set_file_extent_compression(leaf, ei,
177 BTRFS_COMPRESS_ZLIB);
178 } else {
179 page = find_get_page(inode->i_mapping,
180 start >> PAGE_CACHE_SHIFT);
181 btrfs_set_file_extent_compression(leaf, ei, 0);
182 kaddr = kmap_atomic(page, KM_USER0);
183 offset = start & (PAGE_CACHE_SIZE - 1);
184 write_extent_buffer(leaf, kaddr + offset, ptr, size);
185 kunmap_atomic(kaddr, KM_USER0);
186 page_cache_release(page);
187 }
188 btrfs_mark_buffer_dirty(leaf);
189 btrfs_free_path(path);
190
191 BTRFS_I(inode)->disk_i_size = inode->i_size;
192 btrfs_update_inode(trans, root, inode);
193 return 0;
194fail:
195 btrfs_free_path(path);
196 return err;
197}
198
199
200/*
201 * conditionally insert an inline extent into the file. This
202 * does the checks required to make sure the data is small enough
203 * to fit as an inline extent.
204 */
Chris Mason7f366cf2009-03-12 20:12:45 -0400205static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400206 struct btrfs_root *root,
207 struct inode *inode, u64 start, u64 end,
208 size_t compressed_size,
209 struct page **compressed_pages)
210{
211 u64 isize = i_size_read(inode);
212 u64 actual_end = min(end + 1, isize);
213 u64 inline_len = actual_end - start;
214 u64 aligned_end = (end + root->sectorsize - 1) &
215 ~((u64)root->sectorsize - 1);
216 u64 hint_byte;
217 u64 data_len = inline_len;
218 int ret;
219
220 if (compressed_size)
221 data_len = compressed_size;
222
223 if (start > 0 ||
Chris Mason70b99e62008-10-31 12:46:39 -0400224 actual_end >= PAGE_CACHE_SIZE ||
Chris Masonc8b97812008-10-29 14:49:59 -0400225 data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
226 (!compressed_size &&
227 (actual_end & (root->sectorsize - 1)) == 0) ||
228 end + 1 < isize ||
229 data_len > root->fs_info->max_inline) {
230 return 1;
231 }
232
Chris Masonc8b97812008-10-29 14:49:59 -0400233 ret = btrfs_drop_extents(trans, root, inode, start,
Chris Masona1ed8352009-09-11 12:27:37 -0400234 aligned_end, aligned_end, start,
235 &hint_byte, 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400236 BUG_ON(ret);
237
238 if (isize > actual_end)
239 inline_len = min_t(u64, isize, actual_end);
240 ret = insert_inline_extent(trans, root, inode, start,
241 inline_len, compressed_size,
242 compressed_pages);
243 BUG_ON(ret);
Chris Masona1ed8352009-09-11 12:27:37 -0400244 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
Chris Masonc8b97812008-10-29 14:49:59 -0400245 return 0;
246}
247
Chris Mason771ed682008-11-06 22:02:51 -0500248struct async_extent {
249 u64 start;
250 u64 ram_size;
251 u64 compressed_size;
252 struct page **pages;
253 unsigned long nr_pages;
254 struct list_head list;
255};
256
257struct async_cow {
258 struct inode *inode;
259 struct btrfs_root *root;
260 struct page *locked_page;
261 u64 start;
262 u64 end;
263 struct list_head extents;
264 struct btrfs_work work;
265};
266
267static noinline int add_async_extent(struct async_cow *cow,
268 u64 start, u64 ram_size,
269 u64 compressed_size,
270 struct page **pages,
271 unsigned long nr_pages)
272{
273 struct async_extent *async_extent;
274
275 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
276 async_extent->start = start;
277 async_extent->ram_size = ram_size;
278 async_extent->compressed_size = compressed_size;
279 async_extent->pages = pages;
280 async_extent->nr_pages = nr_pages;
281 list_add_tail(&async_extent->list, &cow->extents);
282 return 0;
283}
284
Chris Masonc8b97812008-10-29 14:49:59 -0400285/*
Chris Mason771ed682008-11-06 22:02:51 -0500286 * we create compressed extents in two phases. The first
287 * phase compresses a range of pages that have already been
288 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400289 *
Chris Mason771ed682008-11-06 22:02:51 -0500290 * This is done inside an ordered work queue, and the compression
291 * is spread across many cpus. The actual IO submission is step
292 * two, and the ordered work queue takes care of making sure that
293 * happens in the same order things were put onto the queue by
294 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400295 *
Chris Mason771ed682008-11-06 22:02:51 -0500296 * If this code finds it can't get good compression, it puts an
297 * entry onto the work queue to write the uncompressed bytes. This
298 * makes sure that both compressed inodes and uncompressed inodes
299 * are written in the same order that pdflush sent them down.
Chris Masond352ac62008-09-29 15:18:18 -0400300 */
Chris Mason771ed682008-11-06 22:02:51 -0500301static noinline int compress_file_range(struct inode *inode,
302 struct page *locked_page,
303 u64 start, u64 end,
304 struct async_cow *async_cow,
305 int *num_added)
Chris Masonb888db22007-08-27 16:49:44 -0400306{
307 struct btrfs_root *root = BTRFS_I(inode)->root;
308 struct btrfs_trans_handle *trans;
Chris Masondb945352007-10-15 16:15:53 -0400309 u64 num_bytes;
Chris Masonc8b97812008-10-29 14:49:59 -0400310 u64 orig_start;
311 u64 disk_num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400312 u64 blocksize = root->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400313 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500314 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400315 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400316 struct page **pages = NULL;
317 unsigned long nr_pages;
318 unsigned long nr_pages_ret = 0;
319 unsigned long total_compressed = 0;
320 unsigned long total_in = 0;
321 unsigned long max_compressed = 128 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -0500322 unsigned long max_uncompressed = 128 * 1024;
Chris Masonc8b97812008-10-29 14:49:59 -0400323 int i;
324 int will_compress;
Chris Masonb888db22007-08-27 16:49:44 -0400325
Chris Masonc8b97812008-10-29 14:49:59 -0400326 orig_start = start;
Chris Masonbe20aa92007-12-17 20:14:01 -0500327
Chris Mason42dc7ba2008-12-15 11:44:56 -0500328 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400329again:
330 will_compress = 0;
331 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
332 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
333
Chris Masonf03d9302009-02-04 09:31:06 -0500334 /*
335 * we don't want to send crud past the end of i_size through
336 * compression, that's just a waste of CPU time. So, if the
337 * end of the file is before the start of our current
338 * requested range of bytes, we bail out to the uncompressed
339 * cleanup code that can deal with all of this.
340 *
341 * It isn't really the fastest way to fix things, but this is a
342 * very uncommon corner.
343 */
344 if (actual_end <= start)
345 goto cleanup_and_bail_uncompressed;
346
Chris Masonc8b97812008-10-29 14:49:59 -0400347 total_compressed = actual_end - start;
348
349 /* we want to make sure that amount of ram required to uncompress
350 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500351 * of a compressed extent to 128k. This is a crucial number
352 * because it also controls how easily we can spread reads across
353 * cpus for decompression.
354 *
355 * We also want to make sure the amount of IO required to do
356 * a random read is reasonably small, so we limit the size of
357 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400358 */
359 total_compressed = min(total_compressed, max_uncompressed);
Chris Masondb945352007-10-15 16:15:53 -0400360 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
Chris Masonbe20aa92007-12-17 20:14:01 -0500361 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400362 disk_num_bytes = num_bytes;
363 total_in = 0;
364 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400365
Chris Mason771ed682008-11-06 22:02:51 -0500366 /*
367 * we do compression for mount -o compress and when the
368 * inode has not been flagged as nocompress. This flag can
369 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400370 */
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200371 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) &&
Chris Masonc8b97812008-10-29 14:49:59 -0400372 btrfs_test_opt(root, COMPRESS)) {
373 WARN_ON(pages);
Chris Masoncfbc2462008-10-30 13:22:14 -0400374 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
Chris Mason179e29e2007-11-01 11:28:41 -0400375
Chris Masonc8b97812008-10-29 14:49:59 -0400376 ret = btrfs_zlib_compress_pages(inode->i_mapping, start,
377 total_compressed, pages,
378 nr_pages, &nr_pages_ret,
379 &total_in,
380 &total_compressed,
381 max_compressed);
382
383 if (!ret) {
384 unsigned long offset = total_compressed &
385 (PAGE_CACHE_SIZE - 1);
386 struct page *page = pages[nr_pages_ret - 1];
387 char *kaddr;
388
389 /* zero the tail end of the last page, we might be
390 * sending it down to disk
391 */
392 if (offset) {
393 kaddr = kmap_atomic(page, KM_USER0);
394 memset(kaddr + offset, 0,
395 PAGE_CACHE_SIZE - offset);
396 kunmap_atomic(kaddr, KM_USER0);
397 }
398 will_compress = 1;
399 }
400 }
401 if (start == 0) {
Chris Mason771ed682008-11-06 22:02:51 -0500402 trans = btrfs_join_transaction(root, 1);
403 BUG_ON(!trans);
404 btrfs_set_trans_block_group(trans, inode);
405
Chris Masonc8b97812008-10-29 14:49:59 -0400406 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500407 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400408 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500409 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400410 */
411 ret = cow_file_range_inline(trans, root, inode,
412 start, end, 0, NULL);
413 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500414 /* try making a compressed inline extent */
Chris Masonc8b97812008-10-29 14:49:59 -0400415 ret = cow_file_range_inline(trans, root, inode,
416 start, end,
417 total_compressed, pages);
418 }
Chris Mason771ed682008-11-06 22:02:51 -0500419 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400420 if (ret == 0) {
Chris Mason771ed682008-11-06 22:02:51 -0500421 /*
422 * inline extent creation worked, we don't need
423 * to create any more async work items. Unlock
424 * and free up our temp pages.
425 */
Chris Masonc8b97812008-10-29 14:49:59 -0400426 extent_clear_unlock_delalloc(inode,
427 &BTRFS_I(inode)->io_tree,
Chris Mason771ed682008-11-06 22:02:51 -0500428 start, end, NULL, 1, 0,
Chris Mason8b62b722009-09-02 16:53:46 -0400429 0, 1, 1, 1, 0);
Chris Masonc8b97812008-10-29 14:49:59 -0400430 ret = 0;
431 goto free_pages_out;
432 }
433 }
434
435 if (will_compress) {
436 /*
437 * we aren't doing an inline extent round the compressed size
438 * up to a block size boundary so the allocator does sane
439 * things
440 */
441 total_compressed = (total_compressed + blocksize - 1) &
442 ~(blocksize - 1);
443
444 /*
445 * one last check to make sure the compression is really a
446 * win, compare the page count read with the blocks on disk
447 */
448 total_in = (total_in + PAGE_CACHE_SIZE - 1) &
449 ~(PAGE_CACHE_SIZE - 1);
450 if (total_compressed >= total_in) {
451 will_compress = 0;
452 } else {
453 disk_num_bytes = total_compressed;
454 num_bytes = total_in;
455 }
456 }
457 if (!will_compress && pages) {
458 /*
459 * the compression code ran but failed to make things smaller,
460 * free any pages it allocated and our page pointer array
461 */
462 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400463 WARN_ON(pages[i]->mapping);
Chris Masonc8b97812008-10-29 14:49:59 -0400464 page_cache_release(pages[i]);
465 }
466 kfree(pages);
467 pages = NULL;
468 total_compressed = 0;
469 nr_pages_ret = 0;
470
471 /* flag the file so we don't compress in the future */
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200472 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Masonc8b97812008-10-29 14:49:59 -0400473 }
Chris Mason771ed682008-11-06 22:02:51 -0500474 if (will_compress) {
475 *num_added += 1;
476
477 /* the async work queues will take care of doing actual
478 * allocation on disk for these compressed pages,
479 * and will submit them to the elevator.
480 */
481 add_async_extent(async_cow, start, num_bytes,
482 total_compressed, pages, nr_pages_ret);
483
Chris Mason42dc7ba2008-12-15 11:44:56 -0500484 if (start + num_bytes < end && start + num_bytes < actual_end) {
Chris Mason771ed682008-11-06 22:02:51 -0500485 start += num_bytes;
486 pages = NULL;
487 cond_resched();
488 goto again;
489 }
490 } else {
Chris Masonf03d9302009-02-04 09:31:06 -0500491cleanup_and_bail_uncompressed:
Chris Mason771ed682008-11-06 22:02:51 -0500492 /*
493 * No compression, but we still need to write the pages in
494 * the file we've been given so far. redirty the locked
495 * page if it corresponds to our extent and set things up
496 * for the async work queue to run cow_file_range to do
497 * the normal delalloc dance
498 */
499 if (page_offset(locked_page) >= start &&
500 page_offset(locked_page) <= end) {
501 __set_page_dirty_nobuffers(locked_page);
502 /* unlocked later on in the async handlers */
503 }
504 add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0);
505 *num_added += 1;
506 }
507
508out:
509 return 0;
510
511free_pages_out:
512 for (i = 0; i < nr_pages_ret; i++) {
513 WARN_ON(pages[i]->mapping);
514 page_cache_release(pages[i]);
515 }
Chris Masond3977122009-01-05 21:25:51 -0500516 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500517
518 goto out;
519}
520
521/*
522 * phase two of compressed writeback. This is the ordered portion
523 * of the code, which only gets called in the order the work was
524 * queued. We walk all the async extents created by compress_file_range
525 * and send them down to the disk.
526 */
527static noinline int submit_compressed_extents(struct inode *inode,
528 struct async_cow *async_cow)
529{
530 struct async_extent *async_extent;
531 u64 alloc_hint = 0;
532 struct btrfs_trans_handle *trans;
533 struct btrfs_key ins;
534 struct extent_map *em;
535 struct btrfs_root *root = BTRFS_I(inode)->root;
536 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
537 struct extent_io_tree *io_tree;
538 int ret;
539
540 if (list_empty(&async_cow->extents))
541 return 0;
542
543 trans = btrfs_join_transaction(root, 1);
544
Chris Masond3977122009-01-05 21:25:51 -0500545 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500546 async_extent = list_entry(async_cow->extents.next,
547 struct async_extent, list);
548 list_del(&async_extent->list);
549
550 io_tree = &BTRFS_I(inode)->io_tree;
551
552 /* did the compression code fall back to uncompressed IO? */
553 if (!async_extent->pages) {
554 int page_started = 0;
555 unsigned long nr_written = 0;
556
557 lock_extent(io_tree, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500558 async_extent->start +
559 async_extent->ram_size - 1, GFP_NOFS);
Chris Mason771ed682008-11-06 22:02:51 -0500560
561 /* allocate blocks */
562 cow_file_range(inode, async_cow->locked_page,
563 async_extent->start,
564 async_extent->start +
565 async_extent->ram_size - 1,
566 &page_started, &nr_written, 0);
567
568 /*
569 * if page_started, cow_file_range inserted an
570 * inline extent and took care of all the unlocking
571 * and IO for us. Otherwise, we need to submit
572 * all those pages down to the drive.
573 */
574 if (!page_started)
575 extent_write_locked_range(io_tree,
576 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500577 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500578 async_extent->ram_size - 1,
579 btrfs_get_extent,
580 WB_SYNC_ALL);
581 kfree(async_extent);
582 cond_resched();
583 continue;
584 }
585
586 lock_extent(io_tree, async_extent->start,
587 async_extent->start + async_extent->ram_size - 1,
588 GFP_NOFS);
589 /*
590 * here we're doing allocation and writeback of the
591 * compressed pages
592 */
593 btrfs_drop_extent_cache(inode, async_extent->start,
594 async_extent->start +
595 async_extent->ram_size - 1, 0);
596
597 ret = btrfs_reserve_extent(trans, root,
598 async_extent->compressed_size,
599 async_extent->compressed_size,
600 0, alloc_hint,
601 (u64)-1, &ins, 1);
602 BUG_ON(ret);
603 em = alloc_extent_map(GFP_NOFS);
604 em->start = async_extent->start;
605 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500606 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500607
608 em->block_start = ins.objectid;
609 em->block_len = ins.offset;
610 em->bdev = root->fs_info->fs_devices->latest_bdev;
611 set_bit(EXTENT_FLAG_PINNED, &em->flags);
612 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
613
Chris Masond3977122009-01-05 21:25:51 -0500614 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400615 write_lock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500616 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -0400617 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500618 if (ret != -EEXIST) {
619 free_extent_map(em);
620 break;
621 }
622 btrfs_drop_extent_cache(inode, async_extent->start,
623 async_extent->start +
624 async_extent->ram_size - 1, 0);
625 }
626
627 ret = btrfs_add_ordered_extent(inode, async_extent->start,
628 ins.objectid,
629 async_extent->ram_size,
630 ins.offset,
631 BTRFS_ORDERED_COMPRESSED);
632 BUG_ON(ret);
633
634 btrfs_end_transaction(trans, root);
635
636 /*
637 * clear dirty, set writeback and unlock the pages.
638 */
639 extent_clear_unlock_delalloc(inode,
640 &BTRFS_I(inode)->io_tree,
641 async_extent->start,
642 async_extent->start +
643 async_extent->ram_size - 1,
Chris Mason8b62b722009-09-02 16:53:46 -0400644 NULL, 1, 1, 0, 1, 1, 0, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500645
646 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500647 async_extent->start,
648 async_extent->ram_size,
649 ins.objectid,
650 ins.offset, async_extent->pages,
651 async_extent->nr_pages);
Chris Mason771ed682008-11-06 22:02:51 -0500652
653 BUG_ON(ret);
654 trans = btrfs_join_transaction(root, 1);
655 alloc_hint = ins.objectid + ins.offset;
656 kfree(async_extent);
657 cond_resched();
658 }
659
660 btrfs_end_transaction(trans, root);
661 return 0;
662}
663
664/*
665 * when extent_io.c finds a delayed allocation range in the file,
666 * the call backs end up in this code. The basic idea is to
667 * allocate extents on disk for the range, and create ordered data structs
668 * in ram to track those extents.
669 *
670 * locked_page is the page that writepage had locked already. We use
671 * it to make sure we don't do extra locks or unlocks.
672 *
673 * *page_started is set to one if we unlock locked_page and do everything
674 * required to start IO on it. It may be clean and already done with
675 * IO when we return.
676 */
677static noinline int cow_file_range(struct inode *inode,
678 struct page *locked_page,
679 u64 start, u64 end, int *page_started,
680 unsigned long *nr_written,
681 int unlock)
682{
683 struct btrfs_root *root = BTRFS_I(inode)->root;
684 struct btrfs_trans_handle *trans;
685 u64 alloc_hint = 0;
686 u64 num_bytes;
687 unsigned long ram_size;
688 u64 disk_num_bytes;
689 u64 cur_alloc_size;
690 u64 blocksize = root->sectorsize;
691 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500692 u64 isize = i_size_read(inode);
Chris Mason771ed682008-11-06 22:02:51 -0500693 struct btrfs_key ins;
694 struct extent_map *em;
695 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
696 int ret = 0;
697
698 trans = btrfs_join_transaction(root, 1);
699 BUG_ON(!trans);
700 btrfs_set_trans_block_group(trans, inode);
701
Chris Mason42dc7ba2008-12-15 11:44:56 -0500702 actual_end = min_t(u64, isize, end + 1);
Chris Mason771ed682008-11-06 22:02:51 -0500703
704 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
705 num_bytes = max(blocksize, num_bytes);
706 disk_num_bytes = num_bytes;
707 ret = 0;
708
709 if (start == 0) {
710 /* lets try to make an inline extent */
711 ret = cow_file_range_inline(trans, root, inode,
712 start, end, 0, NULL);
713 if (ret == 0) {
714 extent_clear_unlock_delalloc(inode,
715 &BTRFS_I(inode)->io_tree,
716 start, end, NULL, 1, 1,
Chris Mason8b62b722009-09-02 16:53:46 -0400717 1, 1, 1, 1, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500718 *nr_written = *nr_written +
719 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
720 *page_started = 1;
721 ret = 0;
722 goto out;
723 }
724 }
Chris Masonc8b97812008-10-29 14:49:59 -0400725
726 BUG_ON(disk_num_bytes >
727 btrfs_super_total_bytes(&root->fs_info->super_copy));
728
Chris Masonb917b7c2009-09-18 16:07:03 -0400729
730 read_lock(&BTRFS_I(inode)->extent_tree.lock);
731 em = search_extent_mapping(&BTRFS_I(inode)->extent_tree,
732 start, num_bytes);
733 if (em) {
734 alloc_hint = em->block_start;
735 free_extent_map(em);
736 }
737 read_unlock(&BTRFS_I(inode)->extent_tree.lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400738 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400739
Chris Masond3977122009-01-05 21:25:51 -0500740 while (disk_num_bytes > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400741 cur_alloc_size = min(disk_num_bytes, root->fs_info->max_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400742 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500743 root->sectorsize, 0, alloc_hint,
Chris Masone6dcd2d2008-07-17 12:53:50 -0400744 (u64)-1, &ins, 1);
Chris Masond3977122009-01-05 21:25:51 -0500745 BUG_ON(ret);
746
Chris Masone6dcd2d2008-07-17 12:53:50 -0400747 em = alloc_extent_map(GFP_NOFS);
748 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500749 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500750 ram_size = ins.offset;
751 em->len = ins.offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400752
Chris Masone6dcd2d2008-07-17 12:53:50 -0400753 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400754 em->block_len = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400755 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400756 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -0400757
Chris Masond3977122009-01-05 21:25:51 -0500758 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400759 write_lock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400760 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -0400761 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400762 if (ret != -EEXIST) {
763 free_extent_map(em);
764 break;
765 }
766 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400767 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400768 }
769
Chris Mason98d20f62008-04-14 09:46:10 -0400770 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400771 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -0500772 ram_size, cur_alloc_size, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400773 BUG_ON(ret);
Chris Masonc8b97812008-10-29 14:49:59 -0400774
Yan Zheng17d217f2008-12-12 10:03:38 -0500775 if (root->root_key.objectid ==
776 BTRFS_DATA_RELOC_TREE_OBJECTID) {
777 ret = btrfs_reloc_clone_csums(inode, start,
778 cur_alloc_size);
779 BUG_ON(ret);
780 }
781
Chris Masond3977122009-01-05 21:25:51 -0500782 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -0400783 break;
Chris Masond3977122009-01-05 21:25:51 -0500784
Chris Masonc8b97812008-10-29 14:49:59 -0400785 /* we're not doing compressed IO, don't unlock the first
786 * page (which the caller expects to stay locked), don't
787 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -0400788 *
789 * Do set the Private2 bit so we know this page was properly
790 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -0400791 */
792 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
793 start, start + ram_size - 1,
Chris Mason771ed682008-11-06 22:02:51 -0500794 locked_page, unlock, 1,
Chris Mason8b62b722009-09-02 16:53:46 -0400795 1, 0, 0, 0, 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400796 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -0500797 num_bytes -= cur_alloc_size;
798 alloc_hint = ins.objectid + ins.offset;
799 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400800 }
Chris Masonb888db22007-08-27 16:49:44 -0400801out:
Chris Mason771ed682008-11-06 22:02:51 -0500802 ret = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400803 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400804
Chris Masonbe20aa92007-12-17 20:14:01 -0500805 return ret;
Chris Mason771ed682008-11-06 22:02:51 -0500806}
Chris Masonc8b97812008-10-29 14:49:59 -0400807
Chris Mason771ed682008-11-06 22:02:51 -0500808/*
809 * work queue call back to started compression on a file and pages
810 */
811static noinline void async_cow_start(struct btrfs_work *work)
812{
813 struct async_cow *async_cow;
814 int num_added = 0;
815 async_cow = container_of(work, struct async_cow, work);
816
817 compress_file_range(async_cow->inode, async_cow->locked_page,
818 async_cow->start, async_cow->end, async_cow,
819 &num_added);
820 if (num_added == 0)
821 async_cow->inode = NULL;
822}
823
824/*
825 * work queue call back to submit previously compressed pages
826 */
827static noinline void async_cow_submit(struct btrfs_work *work)
828{
829 struct async_cow *async_cow;
830 struct btrfs_root *root;
831 unsigned long nr_pages;
832
833 async_cow = container_of(work, struct async_cow, work);
834
835 root = async_cow->root;
836 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
837 PAGE_CACHE_SHIFT;
838
839 atomic_sub(nr_pages, &root->fs_info->async_delalloc_pages);
840
841 if (atomic_read(&root->fs_info->async_delalloc_pages) <
842 5 * 1042 * 1024 &&
843 waitqueue_active(&root->fs_info->async_submit_wait))
844 wake_up(&root->fs_info->async_submit_wait);
845
Chris Masond3977122009-01-05 21:25:51 -0500846 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -0500847 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -0500848}
Chris Masonc8b97812008-10-29 14:49:59 -0400849
Chris Mason771ed682008-11-06 22:02:51 -0500850static noinline void async_cow_free(struct btrfs_work *work)
851{
852 struct async_cow *async_cow;
853 async_cow = container_of(work, struct async_cow, work);
854 kfree(async_cow);
855}
856
857static int cow_file_range_async(struct inode *inode, struct page *locked_page,
858 u64 start, u64 end, int *page_started,
859 unsigned long *nr_written)
860{
861 struct async_cow *async_cow;
862 struct btrfs_root *root = BTRFS_I(inode)->root;
863 unsigned long nr_pages;
864 u64 cur_end;
865 int limit = 10 * 1024 * 1042;
866
Chris Mason771ed682008-11-06 22:02:51 -0500867 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED |
Chris Mason2c64c532009-09-02 15:04:12 -0400868 EXTENT_DELALLOC, 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -0500869 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500870 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
871 async_cow->inode = inode;
872 async_cow->root = root;
873 async_cow->locked_page = locked_page;
874 async_cow->start = start;
875
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200876 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
Chris Mason771ed682008-11-06 22:02:51 -0500877 cur_end = end;
878 else
879 cur_end = min(end, start + 512 * 1024 - 1);
880
881 async_cow->end = cur_end;
882 INIT_LIST_HEAD(&async_cow->extents);
883
884 async_cow->work.func = async_cow_start;
885 async_cow->work.ordered_func = async_cow_submit;
886 async_cow->work.ordered_free = async_cow_free;
887 async_cow->work.flags = 0;
888
Chris Mason771ed682008-11-06 22:02:51 -0500889 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
890 PAGE_CACHE_SHIFT;
891 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
892
893 btrfs_queue_worker(&root->fs_info->delalloc_workers,
894 &async_cow->work);
895
896 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
897 wait_event(root->fs_info->async_submit_wait,
898 (atomic_read(&root->fs_info->async_delalloc_pages) <
899 limit));
900 }
901
Chris Masond3977122009-01-05 21:25:51 -0500902 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -0500903 atomic_read(&root->fs_info->async_delalloc_pages)) {
904 wait_event(root->fs_info->async_submit_wait,
905 (atomic_read(&root->fs_info->async_delalloc_pages) ==
906 0));
907 }
908
909 *nr_written += nr_pages;
910 start = cur_end + 1;
911 }
912 *page_started = 1;
913 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -0500914}
915
Chris Masond3977122009-01-05 21:25:51 -0500916static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -0500917 u64 bytenr, u64 num_bytes)
918{
919 int ret;
920 struct btrfs_ordered_sum *sums;
921 LIST_HEAD(list);
922
Yan Zheng07d400a2009-01-06 11:42:00 -0500923 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
924 bytenr + num_bytes - 1, &list);
Yan Zheng17d217f2008-12-12 10:03:38 -0500925 if (ret == 0 && list_empty(&list))
926 return 0;
927
928 while (!list_empty(&list)) {
929 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
930 list_del(&sums->list);
931 kfree(sums);
932 }
933 return 1;
934}
935
Chris Masond352ac62008-09-29 15:18:18 -0400936/*
937 * when nowcow writeback call back. This checks for snapshots or COW copies
938 * of the extents that exist in the file, and COWs the file as required.
939 *
940 * If no cow copies or snapshots exist, we write directly to the existing
941 * blocks on disk
942 */
Chris Mason7f366cf2009-03-12 20:12:45 -0400943static noinline int run_delalloc_nocow(struct inode *inode,
944 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -0500945 u64 start, u64 end, int *page_started, int force,
946 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -0500947{
Chris Masonbe20aa92007-12-17 20:14:01 -0500948 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -0400949 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -0500950 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -0500951 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -0400952 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -0500953 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -0400954 u64 cow_start;
955 u64 cur_offset;
956 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400957 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -0400958 u64 disk_bytenr;
959 u64 num_bytes;
960 int extent_type;
961 int ret;
Yan Zhengd899e052008-10-30 14:25:28 -0400962 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -0400963 int nocow;
964 int check_prev = 1;
Chris Masonbe20aa92007-12-17 20:14:01 -0500965
966 path = btrfs_alloc_path();
967 BUG_ON(!path);
Yan Zheng7ea394f2008-08-05 13:05:02 -0400968 trans = btrfs_join_transaction(root, 1);
969 BUG_ON(!trans);
Chris Masonbe20aa92007-12-17 20:14:01 -0500970
Yan Zheng80ff3852008-10-30 14:20:02 -0400971 cow_start = (u64)-1;
972 cur_offset = start;
973 while (1) {
974 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
975 cur_offset, 0);
976 BUG_ON(ret < 0);
977 if (ret > 0 && path->slots[0] > 0 && check_prev) {
978 leaf = path->nodes[0];
979 btrfs_item_key_to_cpu(leaf, &found_key,
980 path->slots[0] - 1);
981 if (found_key.objectid == inode->i_ino &&
982 found_key.type == BTRFS_EXTENT_DATA_KEY)
983 path->slots[0]--;
984 }
985 check_prev = 0;
986next_slot:
987 leaf = path->nodes[0];
988 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
989 ret = btrfs_next_leaf(root, path);
990 if (ret < 0)
991 BUG_ON(1);
992 if (ret > 0)
993 break;
994 leaf = path->nodes[0];
995 }
Chris Masonbe20aa92007-12-17 20:14:01 -0500996
Yan Zheng80ff3852008-10-30 14:20:02 -0400997 nocow = 0;
998 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -0500999 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001000 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001001
Yan Zheng80ff3852008-10-30 14:20:02 -04001002 if (found_key.objectid > inode->i_ino ||
1003 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1004 found_key.offset > end)
1005 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001006
Yan Zheng80ff3852008-10-30 14:20:02 -04001007 if (found_key.offset > cur_offset) {
1008 extent_end = found_key.offset;
1009 goto out_check;
1010 }
Chris Masonc31f8832008-01-08 15:46:31 -05001011
Yan Zheng80ff3852008-10-30 14:20:02 -04001012 fi = btrfs_item_ptr(leaf, path->slots[0],
1013 struct btrfs_file_extent_item);
1014 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001015
Yan Zhengd899e052008-10-30 14:25:28 -04001016 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1017 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001018 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001019 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001020 extent_end = found_key.offset +
1021 btrfs_file_extent_num_bytes(leaf, fi);
1022 if (extent_end <= start) {
1023 path->slots[0]++;
1024 goto next_slot;
1025 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001026 if (disk_bytenr == 0)
1027 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001028 if (btrfs_file_extent_compression(leaf, fi) ||
1029 btrfs_file_extent_encryption(leaf, fi) ||
1030 btrfs_file_extent_other_encoding(leaf, fi))
1031 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001032 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1033 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001034 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001035 goto out_check;
Yan Zheng17d217f2008-12-12 10:03:38 -05001036 if (btrfs_cross_ref_exist(trans, root, inode->i_ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001037 found_key.offset -
1038 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001039 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001040 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001041 disk_bytenr += cur_offset - found_key.offset;
1042 num_bytes = min(end + 1, extent_end) - cur_offset;
1043 /*
1044 * force cow if csum exists in the range.
1045 * this ensure that csum for a given extent are
1046 * either valid or do not exist.
1047 */
1048 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1049 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001050 nocow = 1;
1051 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1052 extent_end = found_key.offset +
1053 btrfs_file_extent_inline_len(leaf, fi);
1054 extent_end = ALIGN(extent_end, root->sectorsize);
1055 } else {
1056 BUG_ON(1);
1057 }
1058out_check:
1059 if (extent_end <= start) {
1060 path->slots[0]++;
1061 goto next_slot;
1062 }
1063 if (!nocow) {
1064 if (cow_start == (u64)-1)
1065 cow_start = cur_offset;
1066 cur_offset = extent_end;
1067 if (cur_offset > end)
1068 break;
1069 path->slots[0]++;
1070 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001071 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001072
Yan Zheng7ea394f2008-08-05 13:05:02 -04001073 btrfs_release_path(root, path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001074 if (cow_start != (u64)-1) {
1075 ret = cow_file_range(inode, locked_page, cow_start,
Chris Mason771ed682008-11-06 22:02:51 -05001076 found_key.offset - 1, page_started,
1077 nr_written, 1);
Yan Zheng80ff3852008-10-30 14:20:02 -04001078 BUG_ON(ret);
1079 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001080 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001081
Yan Zhengd899e052008-10-30 14:25:28 -04001082 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1083 struct extent_map *em;
1084 struct extent_map_tree *em_tree;
1085 em_tree = &BTRFS_I(inode)->extent_tree;
1086 em = alloc_extent_map(GFP_NOFS);
1087 em->start = cur_offset;
Chris Mason445a6942008-11-10 11:53:33 -05001088 em->orig_start = em->start;
Yan Zhengd899e052008-10-30 14:25:28 -04001089 em->len = num_bytes;
1090 em->block_len = num_bytes;
1091 em->block_start = disk_bytenr;
1092 em->bdev = root->fs_info->fs_devices->latest_bdev;
1093 set_bit(EXTENT_FLAG_PINNED, &em->flags);
1094 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001095 write_lock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001096 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04001097 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001098 if (ret != -EEXIST) {
1099 free_extent_map(em);
1100 break;
1101 }
1102 btrfs_drop_extent_cache(inode, em->start,
1103 em->start + em->len - 1, 0);
1104 }
1105 type = BTRFS_ORDERED_PREALLOC;
1106 } else {
1107 type = BTRFS_ORDERED_NOCOW;
1108 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001109
1110 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001111 num_bytes, num_bytes, type);
1112 BUG_ON(ret);
Chris Mason771ed682008-11-06 22:02:51 -05001113
Yan Zhengd899e052008-10-30 14:25:28 -04001114 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
1115 cur_offset, cur_offset + num_bytes - 1,
Chris Mason8b62b722009-09-02 16:53:46 -04001116 locked_page, 1, 1, 1, 0, 0, 0, 1);
Yan Zheng80ff3852008-10-30 14:20:02 -04001117 cur_offset = extent_end;
1118 if (cur_offset > end)
1119 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001120 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001121 btrfs_release_path(root, path);
1122
1123 if (cur_offset <= end && cow_start == (u64)-1)
1124 cow_start = cur_offset;
1125 if (cow_start != (u64)-1) {
1126 ret = cow_file_range(inode, locked_page, cow_start, end,
Chris Mason771ed682008-11-06 22:02:51 -05001127 page_started, nr_written, 1);
Yan Zheng80ff3852008-10-30 14:20:02 -04001128 BUG_ON(ret);
1129 }
1130
1131 ret = btrfs_end_transaction(trans, root);
1132 BUG_ON(ret);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001133 btrfs_free_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001134 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001135}
1136
Chris Masond352ac62008-09-29 15:18:18 -04001137/*
1138 * extent_io.c call back to do delayed allocation processing
1139 */
Chris Masonc8b97812008-10-29 14:49:59 -04001140static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001141 u64 start, u64 end, int *page_started,
1142 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001143{
Chris Masonbe20aa92007-12-17 20:14:01 -05001144 int ret;
Chris Mason7f366cf2009-03-12 20:12:45 -04001145 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona2135012008-06-25 16:01:30 -04001146
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001147 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)
Chris Masonc8b97812008-10-29 14:49:59 -04001148 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001149 page_started, 1, nr_written);
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001150 else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC)
Yan Zhengd899e052008-10-30 14:25:28 -04001151 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001152 page_started, 0, nr_written);
Chris Mason7f366cf2009-03-12 20:12:45 -04001153 else if (!btrfs_test_opt(root, COMPRESS))
1154 ret = cow_file_range(inode, locked_page, start, end,
1155 page_started, nr_written, 1);
Chris Masonbe20aa92007-12-17 20:14:01 -05001156 else
Chris Mason771ed682008-11-06 22:02:51 -05001157 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001158 page_started, nr_written);
Chris Masonb888db22007-08-27 16:49:44 -04001159 return ret;
1160}
1161
Chris Masond352ac62008-09-29 15:18:18 -04001162/*
1163 * extent_io.c set_bit_hook, used to track delayed allocation
1164 * bytes in this file, and to maintain the list of inodes that
1165 * have pending delalloc work to be done.
1166 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001167static int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -05001168 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -05001169{
Chris Mason75eff682008-12-15 15:54:40 -05001170 /*
1171 * set_bit and clear bit hooks normally require _irqsave/restore
1172 * but in this case, we are only testeing for the DELALLOC
1173 * bit, which is only set or cleared with irqs on
1174 */
Chris Masonb0c68f82008-01-31 11:05:37 -05001175 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001176 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik6a632092009-02-20 11:00:09 -05001177 btrfs_delalloc_reserve_space(root, inode, end - start + 1);
Chris Mason75eff682008-12-15 15:54:40 -05001178 spin_lock(&root->fs_info->delalloc_lock);
Chris Mason90692182008-02-08 13:49:28 -05001179 BTRFS_I(inode)->delalloc_bytes += end - start + 1;
Chris Mason291d6732008-01-29 15:55:23 -05001180 root->fs_info->delalloc_bytes += end - start + 1;
Chris Masonea8c2812008-08-04 23:17:27 -04001181 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1182 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1183 &root->fs_info->delalloc_inodes);
1184 }
Chris Mason75eff682008-12-15 15:54:40 -05001185 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001186 }
1187 return 0;
1188}
1189
Chris Masond352ac62008-09-29 15:18:18 -04001190/*
1191 * extent_io.c clear_bit_hook, see set_bit_hook for why
1192 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001193static int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -05001194 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -05001195{
Chris Mason75eff682008-12-15 15:54:40 -05001196 /*
1197 * set_bit and clear bit hooks normally require _irqsave/restore
1198 * but in this case, we are only testeing for the DELALLOC
1199 * bit, which is only set or cleared with irqs on
1200 */
Chris Masonb0c68f82008-01-31 11:05:37 -05001201 if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001202 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonbcbfce82008-04-22 13:26:47 -04001203
Chris Mason75eff682008-12-15 15:54:40 -05001204 spin_lock(&root->fs_info->delalloc_lock);
Chris Masonb0c68f82008-01-31 11:05:37 -05001205 if (end - start + 1 > root->fs_info->delalloc_bytes) {
Chris Masond3977122009-01-05 21:25:51 -05001206 printk(KERN_INFO "btrfs warning: delalloc account "
1207 "%llu %llu\n",
1208 (unsigned long long)end - start + 1,
1209 (unsigned long long)
1210 root->fs_info->delalloc_bytes);
Josef Bacik6a632092009-02-20 11:00:09 -05001211 btrfs_delalloc_free_space(root, inode, (u64)-1);
Chris Masonb0c68f82008-01-31 11:05:37 -05001212 root->fs_info->delalloc_bytes = 0;
Chris Mason90692182008-02-08 13:49:28 -05001213 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masonb0c68f82008-01-31 11:05:37 -05001214 } else {
Josef Bacik6a632092009-02-20 11:00:09 -05001215 btrfs_delalloc_free_space(root, inode,
1216 end - start + 1);
Chris Masonb0c68f82008-01-31 11:05:37 -05001217 root->fs_info->delalloc_bytes -= end - start + 1;
Chris Mason90692182008-02-08 13:49:28 -05001218 BTRFS_I(inode)->delalloc_bytes -= end - start + 1;
Chris Masonb0c68f82008-01-31 11:05:37 -05001219 }
Chris Masonea8c2812008-08-04 23:17:27 -04001220 if (BTRFS_I(inode)->delalloc_bytes == 0 &&
1221 !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1222 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1223 }
Chris Mason75eff682008-12-15 15:54:40 -05001224 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001225 }
1226 return 0;
1227}
1228
Chris Masond352ac62008-09-29 15:18:18 -04001229/*
1230 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1231 * we don't create bios that span stripes or chunks
1232 */
Chris Mason239b14b2008-03-24 15:02:07 -04001233int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001234 size_t size, struct bio *bio,
1235 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001236{
1237 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
1238 struct btrfs_mapping_tree *map_tree;
Chris Masona62b9402008-10-03 16:31:08 -04001239 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001240 u64 length = 0;
1241 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001242 int ret;
1243
Chris Mason771ed682008-11-06 22:02:51 -05001244 if (bio_flags & EXTENT_BIO_COMPRESSED)
1245 return 0;
1246
Chris Masonf2d8d742008-04-21 10:03:05 -04001247 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001248 map_tree = &root->fs_info->mapping_tree;
1249 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04001250 ret = btrfs_map_block(map_tree, READ, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001251 &map_length, NULL, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04001252
Chris Masond3977122009-01-05 21:25:51 -05001253 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001254 return 1;
Chris Mason239b14b2008-03-24 15:02:07 -04001255 return 0;
1256}
1257
Chris Masond352ac62008-09-29 15:18:18 -04001258/*
1259 * in order to insert checksums into the metadata in large chunks,
1260 * we wait until bio submission time. All the pages in the bio are
1261 * checksummed and sums are attached onto the ordered extent record.
1262 *
1263 * At IO completion time the cums attached on the ordered extent record
1264 * are inserted into the btree
1265 */
Chris Masond3977122009-01-05 21:25:51 -05001266static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1267 struct bio *bio, int mirror_num,
1268 unsigned long bio_flags)
Chris Mason065631f2008-02-20 12:07:25 -05001269{
Chris Mason065631f2008-02-20 12:07:25 -05001270 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001271 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001272
Chris Masond20f7042008-12-08 16:58:54 -05001273 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Chris Mason44b8bd72008-04-16 11:14:51 -04001274 BUG_ON(ret);
Chris Mason4a69a412008-11-06 22:03:00 -05001275 return 0;
1276}
Chris Masone0156402008-04-16 11:15:20 -04001277
Chris Mason4a69a412008-11-06 22:03:00 -05001278/*
1279 * in order to insert checksums into the metadata in large chunks,
1280 * we wait until bio submission time. All the pages in the bio are
1281 * checksummed and sums are attached onto the ordered extent record.
1282 *
1283 * At IO completion time the cums attached on the ordered extent record
1284 * are inserted into the btree
1285 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001286static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Mason4a69a412008-11-06 22:03:00 -05001287 int mirror_num, unsigned long bio_flags)
1288{
1289 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason8b712842008-06-11 16:50:36 -04001290 return btrfs_map_bio(root, rw, bio, mirror_num, 1);
Chris Mason44b8bd72008-04-16 11:14:51 -04001291}
1292
Chris Masond352ac62008-09-29 15:18:18 -04001293/*
Chris Masoncad321a2008-12-17 14:51:42 -05001294 * extent_io.c submission hook. This does the right thing for csum calculation
1295 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001296 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001297static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masonc8b97812008-10-29 14:49:59 -04001298 int mirror_num, unsigned long bio_flags)
Chris Mason44b8bd72008-04-16 11:14:51 -04001299{
1300 struct btrfs_root *root = BTRFS_I(inode)->root;
1301 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001302 int skip_sum;
Chris Mason44b8bd72008-04-16 11:14:51 -04001303
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001304 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001305
Chris Masone6dcd2d2008-07-17 12:53:50 -04001306 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
1307 BUG_ON(ret);
Chris Mason065631f2008-02-20 12:07:25 -05001308
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001309 if (!(rw & (1 << BIO_RW))) {
Chris Masond20f7042008-12-08 16:58:54 -05001310 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Chris Masonc8b97812008-10-29 14:49:59 -04001311 return btrfs_submit_compressed_read(inode, bio,
1312 mirror_num, bio_flags);
Chris Masond20f7042008-12-08 16:58:54 -05001313 } else if (!skip_sum)
1314 btrfs_lookup_bio_sums(root, inode, bio, NULL);
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001315 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001316 } else if (!skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001317 /* csum items have already been cloned */
1318 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1319 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001320 /* we're doing a write, do the async checksumming */
1321 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001322 inode, rw, bio, mirror_num,
Chris Mason4a69a412008-11-06 22:03:00 -05001323 bio_flags, __btrfs_submit_bio_start,
1324 __btrfs_submit_bio_done);
Chris Mason19b9bdb2008-10-30 14:23:13 -04001325 }
1326
Chris Mason0b86a832008-03-24 15:01:56 -04001327mapit:
Chris Mason8b712842008-06-11 16:50:36 -04001328 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
Chris Mason065631f2008-02-20 12:07:25 -05001329}
Chris Mason6885f302008-02-20 16:11:05 -05001330
Chris Masond352ac62008-09-29 15:18:18 -04001331/*
1332 * given a list of ordered sums record them in the inode. This happens
1333 * at IO completion time based on sums calculated at bio submission time.
1334 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001335static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001336 struct inode *inode, u64 file_offset,
1337 struct list_head *list)
1338{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001339 struct btrfs_ordered_sum *sum;
1340
1341 btrfs_set_trans_block_group(trans, inode);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001342
1343 list_for_each_entry(sum, list, list) {
Chris Masond20f7042008-12-08 16:58:54 -05001344 btrfs_csum_file_blocks(trans,
1345 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001346 }
1347 return 0;
1348}
1349
Chris Masonea8c2812008-08-04 23:17:27 -04001350int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end)
1351{
Chris Masond3977122009-01-05 21:25:51 -05001352 if ((end & (PAGE_CACHE_SIZE - 1)) == 0)
Chris Mason771ed682008-11-06 22:02:51 -05001353 WARN_ON(1);
Chris Masonea8c2812008-08-04 23:17:27 -04001354 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
1355 GFP_NOFS);
1356}
1357
Chris Masond352ac62008-09-29 15:18:18 -04001358/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001359struct btrfs_writepage_fixup {
1360 struct page *page;
1361 struct btrfs_work work;
1362};
1363
Christoph Hellwigb2950862008-12-02 09:54:17 -05001364static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001365{
1366 struct btrfs_writepage_fixup *fixup;
1367 struct btrfs_ordered_extent *ordered;
1368 struct page *page;
1369 struct inode *inode;
1370 u64 page_start;
1371 u64 page_end;
1372
1373 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1374 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001375again:
Chris Mason247e7432008-07-17 12:53:51 -04001376 lock_page(page);
1377 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1378 ClearPageChecked(page);
1379 goto out_page;
1380 }
1381
1382 inode = page->mapping->host;
1383 page_start = page_offset(page);
1384 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1385
1386 lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001387
1388 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001389 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001390 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001391
1392 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1393 if (ordered) {
1394 unlock_extent(&BTRFS_I(inode)->io_tree, page_start,
1395 page_end, GFP_NOFS);
1396 unlock_page(page);
1397 btrfs_start_ordered_extent(inode, ordered, 1);
1398 goto again;
1399 }
Chris Mason247e7432008-07-17 12:53:51 -04001400
Chris Masonea8c2812008-08-04 23:17:27 -04001401 btrfs_set_extent_delalloc(inode, page_start, page_end);
Chris Mason247e7432008-07-17 12:53:51 -04001402 ClearPageChecked(page);
1403out:
1404 unlock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
1405out_page:
1406 unlock_page(page);
1407 page_cache_release(page);
1408}
1409
1410/*
1411 * There are a few paths in the higher layers of the kernel that directly
1412 * set the page dirty bit without asking the filesystem if it is a
1413 * good idea. This causes problems because we want to make sure COW
1414 * properly happens and the data=ordered rules are followed.
1415 *
Chris Masonc8b97812008-10-29 14:49:59 -04001416 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001417 * hasn't been properly setup for IO. We kick off an async process
1418 * to fix it up. The async helper will wait for ordered extents, set
1419 * the delalloc bit and make it safe to write the page.
1420 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001421static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001422{
1423 struct inode *inode = page->mapping->host;
1424 struct btrfs_writepage_fixup *fixup;
1425 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001426
Chris Mason8b62b722009-09-02 16:53:46 -04001427 /* this page is properly in the ordered list */
1428 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001429 return 0;
1430
1431 if (PageChecked(page))
1432 return -EAGAIN;
1433
1434 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1435 if (!fixup)
1436 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001437
Chris Mason247e7432008-07-17 12:53:51 -04001438 SetPageChecked(page);
1439 page_cache_get(page);
1440 fixup->work.func = btrfs_writepage_fixup_worker;
1441 fixup->page = page;
1442 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
1443 return -EAGAIN;
1444}
1445
Yan Zhengd899e052008-10-30 14:25:28 -04001446static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1447 struct inode *inode, u64 file_pos,
1448 u64 disk_bytenr, u64 disk_num_bytes,
1449 u64 num_bytes, u64 ram_bytes,
Chris Masone980b502009-04-24 14:39:24 -04001450 u64 locked_end,
Yan Zhengd899e052008-10-30 14:25:28 -04001451 u8 compression, u8 encryption,
1452 u16 other_encoding, int extent_type)
1453{
1454 struct btrfs_root *root = BTRFS_I(inode)->root;
1455 struct btrfs_file_extent_item *fi;
1456 struct btrfs_path *path;
1457 struct extent_buffer *leaf;
1458 struct btrfs_key ins;
1459 u64 hint;
1460 int ret;
1461
1462 path = btrfs_alloc_path();
1463 BUG_ON(!path);
1464
Chris Masonb9473432009-03-13 11:00:37 -04001465 path->leave_spinning = 1;
Chris Masona1ed8352009-09-11 12:27:37 -04001466
1467 /*
1468 * we may be replacing one extent in the tree with another.
1469 * The new extent is pinned in the extent map, and we don't want
1470 * to drop it from the cache until it is completely in the btree.
1471 *
1472 * So, tell btrfs_drop_extents to leave this extent in the cache.
1473 * the caller is expected to unpin it and allow it to be merged
1474 * with the others.
1475 */
Yan Zhengd899e052008-10-30 14:25:28 -04001476 ret = btrfs_drop_extents(trans, root, inode, file_pos,
Chris Masone980b502009-04-24 14:39:24 -04001477 file_pos + num_bytes, locked_end,
Chris Masona1ed8352009-09-11 12:27:37 -04001478 file_pos, &hint, 0);
Yan Zhengd899e052008-10-30 14:25:28 -04001479 BUG_ON(ret);
1480
1481 ins.objectid = inode->i_ino;
1482 ins.offset = file_pos;
1483 ins.type = BTRFS_EXTENT_DATA_KEY;
1484 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
1485 BUG_ON(ret);
1486 leaf = path->nodes[0];
1487 fi = btrfs_item_ptr(leaf, path->slots[0],
1488 struct btrfs_file_extent_item);
1489 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1490 btrfs_set_file_extent_type(leaf, fi, extent_type);
1491 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1492 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1493 btrfs_set_file_extent_offset(leaf, fi, 0);
1494 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1495 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1496 btrfs_set_file_extent_compression(leaf, fi, compression);
1497 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1498 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04001499
1500 btrfs_unlock_up_safe(path, 1);
1501 btrfs_set_lock_blocking(leaf);
1502
Yan Zhengd899e052008-10-30 14:25:28 -04001503 btrfs_mark_buffer_dirty(leaf);
1504
1505 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04001506
1507 ins.objectid = disk_bytenr;
1508 ins.offset = disk_num_bytes;
1509 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001510 ret = btrfs_alloc_reserved_file_extent(trans, root,
1511 root->root_key.objectid,
1512 inode->i_ino, file_pos, &ins);
Yan Zhengd899e052008-10-30 14:25:28 -04001513 BUG_ON(ret);
Yan Zhengd899e052008-10-30 14:25:28 -04001514 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04001515
Yan Zhengd899e052008-10-30 14:25:28 -04001516 return 0;
1517}
1518
Chris Mason5d13a982009-03-13 11:41:46 -04001519/*
1520 * helper function for btrfs_finish_ordered_io, this
1521 * just reads in some of the csum leaves to prime them into ram
1522 * before we start the transaction. It limits the amount of btree
1523 * reads required while inside the transaction.
1524 */
1525static noinline void reada_csum(struct btrfs_root *root,
1526 struct btrfs_path *path,
1527 struct btrfs_ordered_extent *ordered_extent)
1528{
1529 struct btrfs_ordered_sum *sum;
1530 u64 bytenr;
1531
1532 sum = list_entry(ordered_extent->list.next, struct btrfs_ordered_sum,
1533 list);
1534 bytenr = sum->sums[0].bytenr;
1535
1536 /*
1537 * we don't care about the results, the point of this search is
1538 * just to get the btree leaves into ram
1539 */
1540 btrfs_lookup_csum(NULL, root->fs_info->csum_root, path, bytenr, 0);
1541}
1542
Chris Masond352ac62008-09-29 15:18:18 -04001543/* as ordered data IO finishes, this gets called so we can finish
1544 * an ordered extent if the range of bytes in the file it covers are
1545 * fully written.
1546 */
Chris Mason211f90e2008-07-18 11:56:15 -04001547static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001548{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001549 struct btrfs_root *root = BTRFS_I(inode)->root;
1550 struct btrfs_trans_handle *trans;
Chris Mason5d13a982009-03-13 11:41:46 -04001551 struct btrfs_ordered_extent *ordered_extent = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001552 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonb7ec40d2009-03-12 20:12:45 -04001553 struct btrfs_path *path;
Yan Zhengd899e052008-10-30 14:25:28 -04001554 int compressed = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001555 int ret;
1556
1557 ret = btrfs_dec_test_ordered_pending(inode, start, end - start + 1);
Chris Masonba1da2f2008-07-17 12:54:15 -04001558 if (!ret)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001559 return 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001560
Chris Masonb7ec40d2009-03-12 20:12:45 -04001561 /*
1562 * before we join the transaction, try to do some of our IO.
1563 * This will limit the amount of IO that we have to do with
1564 * the transaction running. We're unlikely to need to do any
1565 * IO if the file extents are new, the disk_i_size checks
1566 * covers the most common case.
1567 */
1568 if (start < BTRFS_I(inode)->disk_i_size) {
1569 path = btrfs_alloc_path();
1570 if (path) {
1571 ret = btrfs_lookup_file_extent(NULL, root, path,
1572 inode->i_ino,
1573 start, 0);
Chris Mason5d13a982009-03-13 11:41:46 -04001574 ordered_extent = btrfs_lookup_ordered_extent(inode,
1575 start);
1576 if (!list_empty(&ordered_extent->list)) {
1577 btrfs_release_path(root, path);
1578 reada_csum(root, path, ordered_extent);
1579 }
Chris Masonb7ec40d2009-03-12 20:12:45 -04001580 btrfs_free_path(path);
1581 }
1582 }
1583
Chris Masonf9295742008-07-17 12:54:14 -04001584 trans = btrfs_join_transaction(root, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001585
Chris Mason5d13a982009-03-13 11:41:46 -04001586 if (!ordered_extent)
1587 ordered_extent = btrfs_lookup_ordered_extent(inode, start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001588 BUG_ON(!ordered_extent);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001589 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags))
1590 goto nocow;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001591
1592 lock_extent(io_tree, ordered_extent->file_offset,
1593 ordered_extent->file_offset + ordered_extent->len - 1,
1594 GFP_NOFS);
1595
Chris Masonc8b97812008-10-29 14:49:59 -04001596 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Yan Zhengd899e052008-10-30 14:25:28 -04001597 compressed = 1;
1598 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
1599 BUG_ON(compressed);
1600 ret = btrfs_mark_extent_written(trans, root, inode,
1601 ordered_extent->file_offset,
1602 ordered_extent->file_offset +
1603 ordered_extent->len);
1604 BUG_ON(ret);
1605 } else {
1606 ret = insert_reserved_file_extent(trans, inode,
1607 ordered_extent->file_offset,
1608 ordered_extent->start,
1609 ordered_extent->disk_len,
1610 ordered_extent->len,
1611 ordered_extent->len,
Chris Masone980b502009-04-24 14:39:24 -04001612 ordered_extent->file_offset +
1613 ordered_extent->len,
Yan Zhengd899e052008-10-30 14:25:28 -04001614 compressed, 0, 0,
1615 BTRFS_FILE_EXTENT_REG);
Chris Masona1ed8352009-09-11 12:27:37 -04001616 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
1617 ordered_extent->file_offset,
1618 ordered_extent->len);
Yan Zhengd899e052008-10-30 14:25:28 -04001619 BUG_ON(ret);
1620 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04001621 unlock_extent(io_tree, ordered_extent->file_offset,
1622 ordered_extent->file_offset + ordered_extent->len - 1,
1623 GFP_NOFS);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001624nocow:
Chris Masone6dcd2d2008-07-17 12:53:50 -04001625 add_pending_csums(trans, inode, ordered_extent->file_offset,
1626 &ordered_extent->list);
1627
Chris Mason34353022008-09-23 20:19:49 -04001628 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Chris Masondbe674a2008-07-17 12:54:05 -04001629 btrfs_ordered_update_i_size(inode, ordered_extent);
Chris Masone02119d2008-09-05 16:13:11 -04001630 btrfs_update_inode(trans, root, inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001631 btrfs_remove_ordered_extent(inode, ordered_extent);
Chris Mason34353022008-09-23 20:19:49 -04001632 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
Chris Mason7f3c74f2008-07-18 12:01:11 -04001633
Chris Masone6dcd2d2008-07-17 12:53:50 -04001634 /* once for us */
1635 btrfs_put_ordered_extent(ordered_extent);
1636 /* once for the tree */
1637 btrfs_put_ordered_extent(ordered_extent);
1638
Chris Masone6dcd2d2008-07-17 12:53:50 -04001639 btrfs_end_transaction(trans, root);
1640 return 0;
1641}
1642
Christoph Hellwigb2950862008-12-02 09:54:17 -05001643static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04001644 struct extent_state *state, int uptodate)
1645{
Chris Mason8b62b722009-09-02 16:53:46 -04001646 ClearPagePrivate2(page);
Chris Mason211f90e2008-07-18 11:56:15 -04001647 return btrfs_finish_ordered_io(page->mapping->host, start, end);
1648}
1649
Chris Masond352ac62008-09-29 15:18:18 -04001650/*
1651 * When IO fails, either with EIO or csum verification fails, we
1652 * try other mirrors that might have a good copy of the data. This
1653 * io_failure_record is used to record state as we go through all the
1654 * mirrors. If another mirror has good data, the page is set up to date
1655 * and things continue. If a good mirror can't be found, the original
1656 * bio end_io callback is called to indicate things have failed.
1657 */
Chris Mason7e383262008-04-09 16:28:12 -04001658struct io_failure_record {
1659 struct page *page;
1660 u64 start;
1661 u64 len;
1662 u64 logical;
Chris Masond20f7042008-12-08 16:58:54 -05001663 unsigned long bio_flags;
Chris Mason7e383262008-04-09 16:28:12 -04001664 int last_mirror;
1665};
1666
Christoph Hellwigb2950862008-12-02 09:54:17 -05001667static int btrfs_io_failed_hook(struct bio *failed_bio,
Chris Mason1259ab72008-05-12 13:39:03 -04001668 struct page *page, u64 start, u64 end,
1669 struct extent_state *state)
Chris Mason7e383262008-04-09 16:28:12 -04001670{
1671 struct io_failure_record *failrec = NULL;
1672 u64 private;
1673 struct extent_map *em;
1674 struct inode *inode = page->mapping->host;
1675 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Chris Mason3b951512008-04-17 11:29:12 -04001676 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason7e383262008-04-09 16:28:12 -04001677 struct bio *bio;
1678 int num_copies;
1679 int ret;
Chris Mason1259ab72008-05-12 13:39:03 -04001680 int rw;
Chris Mason7e383262008-04-09 16:28:12 -04001681 u64 logical;
1682
1683 ret = get_state_private(failure_tree, start, &private);
1684 if (ret) {
Chris Mason7e383262008-04-09 16:28:12 -04001685 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
1686 if (!failrec)
1687 return -ENOMEM;
1688 failrec->start = start;
1689 failrec->len = end - start + 1;
1690 failrec->last_mirror = 0;
Chris Masond20f7042008-12-08 16:58:54 -05001691 failrec->bio_flags = 0;
Chris Mason7e383262008-04-09 16:28:12 -04001692
Chris Mason890871b2009-09-02 16:24:52 -04001693 read_lock(&em_tree->lock);
Chris Mason3b951512008-04-17 11:29:12 -04001694 em = lookup_extent_mapping(em_tree, start, failrec->len);
1695 if (em->start > start || em->start + em->len < start) {
1696 free_extent_map(em);
1697 em = NULL;
1698 }
Chris Mason890871b2009-09-02 16:24:52 -04001699 read_unlock(&em_tree->lock);
Chris Mason7e383262008-04-09 16:28:12 -04001700
1701 if (!em || IS_ERR(em)) {
1702 kfree(failrec);
1703 return -EIO;
1704 }
1705 logical = start - em->start;
1706 logical = em->block_start + logical;
Chris Masond20f7042008-12-08 16:58:54 -05001707 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
1708 logical = em->block_start;
1709 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
1710 }
Chris Mason7e383262008-04-09 16:28:12 -04001711 failrec->logical = logical;
1712 free_extent_map(em);
1713 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
1714 EXTENT_DIRTY, GFP_NOFS);
Chris Mason587f7702008-04-11 12:16:46 -04001715 set_state_private(failure_tree, start,
1716 (u64)(unsigned long)failrec);
Chris Mason7e383262008-04-09 16:28:12 -04001717 } else {
Chris Mason587f7702008-04-11 12:16:46 -04001718 failrec = (struct io_failure_record *)(unsigned long)private;
Chris Mason7e383262008-04-09 16:28:12 -04001719 }
1720 num_copies = btrfs_num_copies(
1721 &BTRFS_I(inode)->root->fs_info->mapping_tree,
1722 failrec->logical, failrec->len);
1723 failrec->last_mirror++;
1724 if (!state) {
Chris Masoncad321a2008-12-17 14:51:42 -05001725 spin_lock(&BTRFS_I(inode)->io_tree.lock);
Chris Mason7e383262008-04-09 16:28:12 -04001726 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
1727 failrec->start,
1728 EXTENT_LOCKED);
1729 if (state && state->start != failrec->start)
1730 state = NULL;
Chris Masoncad321a2008-12-17 14:51:42 -05001731 spin_unlock(&BTRFS_I(inode)->io_tree.lock);
Chris Mason7e383262008-04-09 16:28:12 -04001732 }
1733 if (!state || failrec->last_mirror > num_copies) {
1734 set_state_private(failure_tree, failrec->start, 0);
1735 clear_extent_bits(failure_tree, failrec->start,
1736 failrec->start + failrec->len - 1,
1737 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
1738 kfree(failrec);
1739 return -EIO;
1740 }
1741 bio = bio_alloc(GFP_NOFS, 1);
1742 bio->bi_private = state;
1743 bio->bi_end_io = failed_bio->bi_end_io;
1744 bio->bi_sector = failrec->logical >> 9;
1745 bio->bi_bdev = failed_bio->bi_bdev;
Chris Masone1c4b742008-04-22 13:26:46 -04001746 bio->bi_size = 0;
Chris Masond20f7042008-12-08 16:58:54 -05001747
Chris Mason7e383262008-04-09 16:28:12 -04001748 bio_add_page(bio, page, failrec->len, start - page_offset(page));
Chris Mason1259ab72008-05-12 13:39:03 -04001749 if (failed_bio->bi_rw & (1 << BIO_RW))
1750 rw = WRITE;
1751 else
1752 rw = READ;
1753
1754 BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
Chris Masonc8b97812008-10-29 14:49:59 -04001755 failrec->last_mirror,
Chris Masond20f7042008-12-08 16:58:54 -05001756 failrec->bio_flags);
Chris Mason1259ab72008-05-12 13:39:03 -04001757 return 0;
1758}
1759
Chris Masond352ac62008-09-29 15:18:18 -04001760/*
1761 * each time an IO finishes, we do a fast check in the IO failure tree
1762 * to see if we need to process or clean up an io_failure_record
1763 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001764static int btrfs_clean_io_failures(struct inode *inode, u64 start)
Chris Mason1259ab72008-05-12 13:39:03 -04001765{
1766 u64 private;
1767 u64 private_failure;
1768 struct io_failure_record *failure;
1769 int ret;
1770
1771 private = 0;
1772 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
1773 (u64)-1, 1, EXTENT_DIRTY)) {
1774 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
1775 start, &private_failure);
1776 if (ret == 0) {
1777 failure = (struct io_failure_record *)(unsigned long)
1778 private_failure;
1779 set_state_private(&BTRFS_I(inode)->io_failure_tree,
1780 failure->start, 0);
1781 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
1782 failure->start,
1783 failure->start + failure->len - 1,
1784 EXTENT_DIRTY | EXTENT_LOCKED,
1785 GFP_NOFS);
1786 kfree(failure);
1787 }
1788 }
Chris Mason7e383262008-04-09 16:28:12 -04001789 return 0;
1790}
1791
Chris Masond352ac62008-09-29 15:18:18 -04001792/*
1793 * when reads are done, we need to check csums to verify the data is correct
1794 * if there's a match, we allow the bio to finish. If not, we go through
1795 * the io_failure_record routines to find good copies
1796 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001797static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason70dec802008-01-29 09:59:12 -05001798 struct extent_state *state)
Chris Mason07157aa2007-08-30 08:50:51 -04001799{
Chris Mason35ebb932007-10-30 16:56:53 -04001800 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -04001801 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05001802 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04001803 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -05001804 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -04001805 int ret;
Chris Masonff79f812007-10-15 16:22:25 -04001806 struct btrfs_root *root = BTRFS_I(inode)->root;
1807 u32 csum = ~(u32)0;
Chris Masond1310b22008-01-24 16:13:08 -05001808
Chris Masond20f7042008-12-08 16:58:54 -05001809 if (PageChecked(page)) {
1810 ClearPageChecked(page);
1811 goto good;
1812 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001813
1814 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Chris Masonb6cda9b2007-12-14 15:30:32 -05001815 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05001816
Yan Zheng17d217f2008-12-12 10:03:38 -05001817 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04001818 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001819 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
1820 GFP_NOFS);
1821 return 0;
1822 }
1823
Yanc2e639f2008-02-04 08:57:25 -05001824 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -05001825 private = state->private;
1826 ret = 0;
1827 } else {
1828 ret = get_state_private(io_tree, start, &private);
1829 }
Chris Mason9ab86c82009-01-07 09:48:51 -05001830 kaddr = kmap_atomic(page, KM_USER0);
Chris Masond3977122009-01-05 21:25:51 -05001831 if (ret)
Chris Mason07157aa2007-08-30 08:50:51 -04001832 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05001833
Chris Masonff79f812007-10-15 16:22:25 -04001834 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
1835 btrfs_csum_final(csum, (char *)&csum);
Chris Masond3977122009-01-05 21:25:51 -05001836 if (csum != private)
Chris Mason07157aa2007-08-30 08:50:51 -04001837 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05001838
Chris Mason9ab86c82009-01-07 09:48:51 -05001839 kunmap_atomic(kaddr, KM_USER0);
Chris Masond20f7042008-12-08 16:58:54 -05001840good:
Chris Mason7e383262008-04-09 16:28:12 -04001841 /* if the io failure tree for this inode is non-empty,
1842 * check to see if we've recovered from a failed IO
1843 */
Chris Mason1259ab72008-05-12 13:39:03 -04001844 btrfs_clean_io_failures(inode, start);
Chris Mason07157aa2007-08-30 08:50:51 -04001845 return 0;
1846
1847zeroit:
Chris Mason193f2842009-04-27 07:29:05 -04001848 if (printk_ratelimit()) {
1849 printk(KERN_INFO "btrfs csum failed ino %lu off %llu csum %u "
1850 "private %llu\n", page->mapping->host->i_ino,
1851 (unsigned long long)start, csum,
1852 (unsigned long long)private);
1853 }
Chris Masondb945352007-10-15 16:15:53 -04001854 memset(kaddr + offset, 1, end - start + 1);
1855 flush_dcache_page(page);
Chris Mason9ab86c82009-01-07 09:48:51 -05001856 kunmap_atomic(kaddr, KM_USER0);
Chris Mason3b951512008-04-17 11:29:12 -04001857 if (private == 0)
1858 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04001859 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04001860}
Chris Masonb888db22007-08-27 16:49:44 -04001861
Josef Bacik7b128762008-07-24 12:17:14 -04001862/*
1863 * This creates an orphan entry for the given inode in case something goes
1864 * wrong in the middle of an unlink/truncate.
1865 */
1866int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
1867{
1868 struct btrfs_root *root = BTRFS_I(inode)->root;
1869 int ret = 0;
1870
Yanbcc63ab2008-07-30 16:29:20 -04001871 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001872
1873 /* already on the orphan list, we're good */
1874 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
Yanbcc63ab2008-07-30 16:29:20 -04001875 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001876 return 0;
1877 }
1878
1879 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
1880
Yanbcc63ab2008-07-30 16:29:20 -04001881 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001882
1883 /*
1884 * insert an orphan item to track this unlinked/truncated file
1885 */
1886 ret = btrfs_insert_orphan_item(trans, root, inode->i_ino);
1887
1888 return ret;
1889}
1890
1891/*
1892 * We have done the truncate/delete so we can go ahead and remove the orphan
1893 * item for this particular inode.
1894 */
1895int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
1896{
1897 struct btrfs_root *root = BTRFS_I(inode)->root;
1898 int ret = 0;
1899
Yanbcc63ab2008-07-30 16:29:20 -04001900 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001901
1902 if (list_empty(&BTRFS_I(inode)->i_orphan)) {
Yanbcc63ab2008-07-30 16:29:20 -04001903 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001904 return 0;
1905 }
1906
1907 list_del_init(&BTRFS_I(inode)->i_orphan);
1908 if (!trans) {
Yanbcc63ab2008-07-30 16:29:20 -04001909 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001910 return 0;
1911 }
1912
Yanbcc63ab2008-07-30 16:29:20 -04001913 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001914
1915 ret = btrfs_del_orphan_item(trans, root, inode->i_ino);
1916
1917 return ret;
1918}
1919
1920/*
1921 * this cleans up any orphans that may be left on the list from the last use
1922 * of this root.
1923 */
1924void btrfs_orphan_cleanup(struct btrfs_root *root)
1925{
1926 struct btrfs_path *path;
1927 struct extent_buffer *leaf;
1928 struct btrfs_item *item;
1929 struct btrfs_key key, found_key;
1930 struct btrfs_trans_handle *trans;
1931 struct inode *inode;
1932 int ret = 0, nr_unlink = 0, nr_truncate = 0;
1933
Josef Bacik7b128762008-07-24 12:17:14 -04001934 path = btrfs_alloc_path();
1935 if (!path)
1936 return;
1937 path->reada = -1;
1938
1939 key.objectid = BTRFS_ORPHAN_OBJECTID;
1940 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
1941 key.offset = (u64)-1;
1942
Josef Bacik7b128762008-07-24 12:17:14 -04001943
1944 while (1) {
1945 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1946 if (ret < 0) {
1947 printk(KERN_ERR "Error searching slot for orphan: %d"
1948 "\n", ret);
1949 break;
1950 }
1951
1952 /*
1953 * if ret == 0 means we found what we were searching for, which
1954 * is weird, but possible, so only screw with path if we didnt
1955 * find the key and see if we have stuff that matches
1956 */
1957 if (ret > 0) {
1958 if (path->slots[0] == 0)
1959 break;
1960 path->slots[0]--;
1961 }
1962
1963 /* pull out the item */
1964 leaf = path->nodes[0];
1965 item = btrfs_item_nr(leaf, path->slots[0]);
1966 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1967
1968 /* make sure the item matches what we want */
1969 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
1970 break;
1971 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
1972 break;
1973
1974 /* release the path since we're done with it */
1975 btrfs_release_path(root, path);
1976
1977 /*
1978 * this is where we are basically btrfs_lookup, without the
1979 * crossing root thing. we store the inode number in the
1980 * offset of the orphan item.
1981 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001982 found_key.objectid = found_key.offset;
1983 found_key.type = BTRFS_INODE_ITEM_KEY;
1984 found_key.offset = 0;
1985 inode = btrfs_iget(root->fs_info->sb, &found_key, root);
1986 if (IS_ERR(inode))
Josef Bacik7b128762008-07-24 12:17:14 -04001987 break;
1988
Josef Bacik7b128762008-07-24 12:17:14 -04001989 /*
1990 * add this inode to the orphan list so btrfs_orphan_del does
1991 * the proper thing when we hit it
1992 */
Yanbcc63ab2008-07-30 16:29:20 -04001993 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001994 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
Yanbcc63ab2008-07-30 16:29:20 -04001995 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001996
1997 /*
1998 * if this is a bad inode, means we actually succeeded in
1999 * removing the inode, but not the orphan record, which means
2000 * we need to manually delete the orphan since iput will just
2001 * do a destroy_inode
2002 */
2003 if (is_bad_inode(inode)) {
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002004 trans = btrfs_start_transaction(root, 1);
Josef Bacik7b128762008-07-24 12:17:14 -04002005 btrfs_orphan_del(trans, inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002006 btrfs_end_transaction(trans, root);
Josef Bacik7b128762008-07-24 12:17:14 -04002007 iput(inode);
2008 continue;
2009 }
2010
2011 /* if we have links, this was a truncate, lets do that */
2012 if (inode->i_nlink) {
2013 nr_truncate++;
2014 btrfs_truncate(inode);
2015 } else {
2016 nr_unlink++;
2017 }
2018
2019 /* this will do delete_inode and everything for us */
2020 iput(inode);
2021 }
2022
2023 if (nr_unlink)
2024 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
2025 if (nr_truncate)
2026 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
2027
2028 btrfs_free_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04002029}
2030
Chris Masond352ac62008-09-29 15:18:18 -04002031/*
Chris Mason46a53cc2009-04-27 11:47:50 -04002032 * very simple check to peek ahead in the leaf looking for xattrs. If we
2033 * don't find any xattrs, we know there can't be any acls.
2034 *
2035 * slot is the slot the inode is in, objectid is the objectid of the inode
2036 */
2037static noinline int acls_after_inode_item(struct extent_buffer *leaf,
2038 int slot, u64 objectid)
2039{
2040 u32 nritems = btrfs_header_nritems(leaf);
2041 struct btrfs_key found_key;
2042 int scanned = 0;
2043
2044 slot++;
2045 while (slot < nritems) {
2046 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2047
2048 /* we found a different objectid, there must not be acls */
2049 if (found_key.objectid != objectid)
2050 return 0;
2051
2052 /* we found an xattr, assume we've got an acl */
2053 if (found_key.type == BTRFS_XATTR_ITEM_KEY)
2054 return 1;
2055
2056 /*
2057 * we found a key greater than an xattr key, there can't
2058 * be any acls later on
2059 */
2060 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
2061 return 0;
2062
2063 slot++;
2064 scanned++;
2065
2066 /*
2067 * it goes inode, inode backrefs, xattrs, extents,
2068 * so if there are a ton of hard links to an inode there can
2069 * be a lot of backrefs. Don't waste time searching too hard,
2070 * this is just an optimization
2071 */
2072 if (scanned >= 8)
2073 break;
2074 }
2075 /* we hit the end of the leaf before we found an xattr or
2076 * something larger than an xattr. We have to assume the inode
2077 * has acls
2078 */
2079 return 1;
2080}
2081
2082/*
Chris Masond352ac62008-09-29 15:18:18 -04002083 * read an inode from the btree into the in-memory inode
2084 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002085static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002086{
2087 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002088 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002089 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04002090 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04002091 struct btrfs_root *root = BTRFS_I(inode)->root;
2092 struct btrfs_key location;
Chris Mason46a53cc2009-04-27 11:47:50 -04002093 int maybe_acls;
Chris Mason39279cc2007-06-12 06:35:45 -04002094 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -04002095 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04002096 int ret;
2097
2098 path = btrfs_alloc_path();
2099 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -04002100 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05002101
Chris Mason39279cc2007-06-12 06:35:45 -04002102 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002103 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04002104 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04002105
Chris Mason5f39d392007-10-15 16:14:19 -04002106 leaf = path->nodes[0];
2107 inode_item = btrfs_item_ptr(leaf, path->slots[0],
2108 struct btrfs_inode_item);
2109
2110 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
2111 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
2112 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
2113 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
Chris Masondbe674a2008-07-17 12:54:05 -04002114 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04002115
2116 tspec = btrfs_inode_atime(inode_item);
2117 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2118 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2119
2120 tspec = btrfs_inode_mtime(inode_item);
2121 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2122 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2123
2124 tspec = btrfs_inode_ctime(inode_item);
2125 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2126 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2127
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002128 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04002129 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Chris Masonc3027eb2008-12-08 16:40:21 -05002130 BTRFS_I(inode)->sequence = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04002131 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04002132 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002133 rdev = btrfs_inode_rdev(leaf, inode_item);
2134
Josef Bacikaec74772008-07-24 12:12:38 -04002135 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05002136 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Josef Bacikaec74772008-07-24 12:12:38 -04002137
Chris Mason5f39d392007-10-15 16:14:19 -04002138 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002139
Chris Mason46a53cc2009-04-27 11:47:50 -04002140 /*
2141 * try to precache a NULL acl entry for files that don't have
2142 * any xattrs or acls
2143 */
2144 maybe_acls = acls_after_inode_item(leaf, path->slots[0], inode->i_ino);
Al Viro72c04902009-06-24 16:58:48 -04002145 if (!maybe_acls)
2146 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04002147
Yan Zhengd2fb3432008-12-11 16:30:39 -05002148 BTRFS_I(inode)->block_group = btrfs_find_block_group(root, 0,
2149 alloc_group_block, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002150 btrfs_free_path(path);
2151 inode_item = NULL;
2152
Chris Mason39279cc2007-06-12 06:35:45 -04002153 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04002154 case S_IFREG:
2155 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04002156 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05002157 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002158 inode->i_fop = &btrfs_file_operations;
2159 inode->i_op = &btrfs_file_inode_operations;
2160 break;
2161 case S_IFDIR:
2162 inode->i_fop = &btrfs_dir_file_operations;
2163 if (root == root->fs_info->tree_root)
2164 inode->i_op = &btrfs_dir_ro_inode_operations;
2165 else
2166 inode->i_op = &btrfs_dir_inode_operations;
2167 break;
2168 case S_IFLNK:
2169 inode->i_op = &btrfs_symlink_inode_operations;
2170 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04002171 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04002172 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04002173 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05002174 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04002175 init_special_inode(inode, inode->i_mode, rdev);
2176 break;
Chris Mason39279cc2007-06-12 06:35:45 -04002177 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002178
2179 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002180 return;
2181
2182make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04002183 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002184 make_bad_inode(inode);
2185}
2186
Chris Masond352ac62008-09-29 15:18:18 -04002187/*
2188 * given a leaf and an inode, copy the inode fields into the leaf
2189 */
Chris Masone02119d2008-09-05 16:13:11 -04002190static void fill_inode_item(struct btrfs_trans_handle *trans,
2191 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04002192 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04002193 struct inode *inode)
2194{
Chris Mason5f39d392007-10-15 16:14:19 -04002195 btrfs_set_inode_uid(leaf, item, inode->i_uid);
2196 btrfs_set_inode_gid(leaf, item, inode->i_gid);
Chris Masondbe674a2008-07-17 12:54:05 -04002197 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
Chris Mason5f39d392007-10-15 16:14:19 -04002198 btrfs_set_inode_mode(leaf, item, inode->i_mode);
2199 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
2200
2201 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
2202 inode->i_atime.tv_sec);
2203 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
2204 inode->i_atime.tv_nsec);
2205
2206 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
2207 inode->i_mtime.tv_sec);
2208 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
2209 inode->i_mtime.tv_nsec);
2210
2211 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
2212 inode->i_ctime.tv_sec);
2213 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
2214 inode->i_ctime.tv_nsec);
2215
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002216 btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode));
Chris Masone02119d2008-09-05 16:13:11 -04002217 btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
Chris Masonc3027eb2008-12-08 16:40:21 -05002218 btrfs_set_inode_sequence(leaf, item, BTRFS_I(inode)->sequence);
Chris Masone02119d2008-09-05 16:13:11 -04002219 btrfs_set_inode_transid(leaf, item, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002220 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -05002221 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002222 btrfs_set_inode_block_group(leaf, item, BTRFS_I(inode)->block_group);
Chris Mason39279cc2007-06-12 06:35:45 -04002223}
2224
Chris Masond352ac62008-09-29 15:18:18 -04002225/*
2226 * copy everything in the in-memory inode into the btree.
2227 */
Chris Masond3977122009-01-05 21:25:51 -05002228noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
2229 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002230{
2231 struct btrfs_inode_item *inode_item;
2232 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002233 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002234 int ret;
2235
2236 path = btrfs_alloc_path();
2237 BUG_ON(!path);
Chris Masonb9473432009-03-13 11:00:37 -04002238 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002239 ret = btrfs_lookup_inode(trans, root, path,
2240 &BTRFS_I(inode)->location, 1);
2241 if (ret) {
2242 if (ret > 0)
2243 ret = -ENOENT;
2244 goto failed;
2245 }
2246
Chris Masonb4ce94d2009-02-04 09:25:08 -05002247 btrfs_unlock_up_safe(path, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002248 leaf = path->nodes[0];
2249 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04002250 struct btrfs_inode_item);
2251
Chris Masone02119d2008-09-05 16:13:11 -04002252 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002253 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002254 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002255 ret = 0;
2256failed:
Chris Mason39279cc2007-06-12 06:35:45 -04002257 btrfs_free_path(path);
2258 return ret;
2259}
2260
2261
Chris Masond352ac62008-09-29 15:18:18 -04002262/*
2263 * unlink helper that gets used here in inode.c and in the tree logging
2264 * recovery code. It remove a link in a directory with a given name, and
2265 * also drops the back refs in the inode to the directory
2266 */
Chris Masone02119d2008-09-05 16:13:11 -04002267int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2268 struct btrfs_root *root,
2269 struct inode *dir, struct inode *inode,
2270 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04002271{
2272 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04002273 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002274 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002275 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04002276 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04002277 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04002278
2279 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04002280 if (!path) {
2281 ret = -ENOMEM;
2282 goto err;
2283 }
2284
Chris Masonb9473432009-03-13 11:00:37 -04002285 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002286 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
2287 name, name_len, -1);
2288 if (IS_ERR(di)) {
2289 ret = PTR_ERR(di);
2290 goto err;
2291 }
2292 if (!di) {
2293 ret = -ENOENT;
2294 goto err;
2295 }
Chris Mason5f39d392007-10-15 16:14:19 -04002296 leaf = path->nodes[0];
2297 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04002298 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04002299 if (ret)
2300 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -04002301 btrfs_release_path(root, path);
2302
Josef Bacikaec74772008-07-24 12:12:38 -04002303 ret = btrfs_del_inode_ref(trans, root, name, name_len,
Chris Masone02119d2008-09-05 16:13:11 -04002304 inode->i_ino,
2305 dir->i_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04002306 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05002307 printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
Josef Bacikaec74772008-07-24 12:12:38 -04002308 "inode %lu parent %lu\n", name_len, name,
Chris Masone02119d2008-09-05 16:13:11 -04002309 inode->i_ino, dir->i_ino);
Josef Bacikaec74772008-07-24 12:12:38 -04002310 goto err;
2311 }
2312
Chris Mason39279cc2007-06-12 06:35:45 -04002313 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04002314 index, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -04002315 if (IS_ERR(di)) {
2316 ret = PTR_ERR(di);
2317 goto err;
2318 }
2319 if (!di) {
2320 ret = -ENOENT;
2321 goto err;
2322 }
2323 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason925baed2008-06-25 16:01:30 -04002324 btrfs_release_path(root, path);
Chris Mason39279cc2007-06-12 06:35:45 -04002325
Chris Masone02119d2008-09-05 16:13:11 -04002326 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
2327 inode, dir->i_ino);
Chris Mason49eb7e42008-09-11 15:53:12 -04002328 BUG_ON(ret != 0 && ret != -ENOENT);
Chris Masone02119d2008-09-05 16:13:11 -04002329
2330 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
2331 dir, index);
2332 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04002333err:
2334 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002335 if (ret)
2336 goto out;
2337
2338 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
2339 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2340 btrfs_update_inode(trans, root, dir);
2341 btrfs_drop_nlink(inode);
2342 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04002343out:
Chris Mason39279cc2007-06-12 06:35:45 -04002344 return ret;
2345}
2346
2347static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
2348{
2349 struct btrfs_root *root;
2350 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04002351 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002352 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -05002353 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002354
2355 root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05002356
Chris Mason39279cc2007-06-12 06:35:45 -04002357 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002358
Chris Mason39279cc2007-06-12 06:35:45 -04002359 btrfs_set_trans_block_group(trans, dir);
Chris Mason12fcfd22009-03-24 10:24:20 -04002360
2361 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
2362
Chris Masone02119d2008-09-05 16:13:11 -04002363 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
2364 dentry->d_name.name, dentry->d_name.len);
Josef Bacik7b128762008-07-24 12:17:14 -04002365
2366 if (inode->i_nlink == 0)
2367 ret = btrfs_orphan_add(trans, inode);
2368
Chris Masond3c2fdc2007-09-17 10:58:06 -04002369 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04002370
Chris Mason89ce8a62008-06-25 16:01:31 -04002371 btrfs_end_transaction_throttle(trans, root);
Chris Masond3c2fdc2007-09-17 10:58:06 -04002372 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002373 return ret;
2374}
2375
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002376int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
2377 struct btrfs_root *root,
2378 struct inode *dir, u64 objectid,
2379 const char *name, int name_len)
2380{
2381 struct btrfs_path *path;
2382 struct extent_buffer *leaf;
2383 struct btrfs_dir_item *di;
2384 struct btrfs_key key;
2385 u64 index;
2386 int ret;
2387
2388 path = btrfs_alloc_path();
2389 if (!path)
2390 return -ENOMEM;
2391
2392 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
2393 name, name_len, -1);
2394 BUG_ON(!di || IS_ERR(di));
2395
2396 leaf = path->nodes[0];
2397 btrfs_dir_item_key_to_cpu(leaf, di, &key);
2398 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
2399 ret = btrfs_delete_one_dir_name(trans, root, path, di);
2400 BUG_ON(ret);
2401 btrfs_release_path(root, path);
2402
2403 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
2404 objectid, root->root_key.objectid,
2405 dir->i_ino, &index, name, name_len);
2406 if (ret < 0) {
2407 BUG_ON(ret != -ENOENT);
2408 di = btrfs_search_dir_index_item(root, path, dir->i_ino,
2409 name, name_len);
2410 BUG_ON(!di || IS_ERR(di));
2411
2412 leaf = path->nodes[0];
2413 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2414 btrfs_release_path(root, path);
2415 index = key.offset;
2416 }
2417
2418 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
2419 index, name, name_len, -1);
2420 BUG_ON(!di || IS_ERR(di));
2421
2422 leaf = path->nodes[0];
2423 btrfs_dir_item_key_to_cpu(leaf, di, &key);
2424 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
2425 ret = btrfs_delete_one_dir_name(trans, root, path, di);
2426 BUG_ON(ret);
2427 btrfs_release_path(root, path);
2428
2429 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
2430 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2431 ret = btrfs_update_inode(trans, root, dir);
2432 BUG_ON(ret);
2433 dir->i_sb->s_dirt = 1;
2434
2435 btrfs_free_path(path);
2436 return 0;
2437}
2438
Chris Mason39279cc2007-06-12 06:35:45 -04002439static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
2440{
2441 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002442 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002443 int ret;
2444 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04002445 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -05002446 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002447
Chris Mason3394e162008-11-17 20:42:26 -05002448 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002449 inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan134d4512007-10-25 15:49:25 -04002450 return -ENOTEMPTY;
2451
Chris Mason39279cc2007-06-12 06:35:45 -04002452 trans = btrfs_start_transaction(root, 1);
2453 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -04002454
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002455 if (unlikely(inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
2456 err = btrfs_unlink_subvol(trans, root, dir,
2457 BTRFS_I(inode)->location.objectid,
2458 dentry->d_name.name,
2459 dentry->d_name.len);
2460 goto out;
2461 }
2462
Josef Bacik7b128762008-07-24 12:17:14 -04002463 err = btrfs_orphan_add(trans, inode);
2464 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002465 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04002466
Chris Mason39279cc2007-06-12 06:35:45 -04002467 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04002468 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
2469 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05002470 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04002471 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002472out:
Chris Masond3c2fdc2007-09-17 10:58:06 -04002473 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04002474 ret = btrfs_end_transaction_throttle(trans, root);
Chris Masond3c2fdc2007-09-17 10:58:06 -04002475 btrfs_btree_balance_dirty(root, nr);
Chris Mason39544012007-12-12 14:38:19 -05002476
Chris Mason39279cc2007-06-12 06:35:45 -04002477 if (ret && !err)
2478 err = ret;
2479 return err;
2480}
2481
Chris Masond20f7042008-12-08 16:58:54 -05002482#if 0
Chris Mason39279cc2007-06-12 06:35:45 -04002483/*
Chris Mason323ac952008-10-01 19:05:46 -04002484 * when truncating bytes in a file, it is possible to avoid reading
2485 * the leaves that contain only checksum items. This can be the
2486 * majority of the IO required to delete a large file, but it must
2487 * be done carefully.
2488 *
2489 * The keys in the level just above the leaves are checked to make sure
2490 * the lowest key in a given leaf is a csum key, and starts at an offset
2491 * after the new size.
2492 *
2493 * Then the key for the next leaf is checked to make sure it also has
2494 * a checksum item for the same file. If it does, we know our target leaf
2495 * contains only checksum items, and it can be safely freed without reading
2496 * it.
2497 *
2498 * This is just an optimization targeted at large files. It may do
2499 * nothing. It will return 0 unless things went badly.
2500 */
2501static noinline int drop_csum_leaves(struct btrfs_trans_handle *trans,
2502 struct btrfs_root *root,
2503 struct btrfs_path *path,
2504 struct inode *inode, u64 new_size)
2505{
2506 struct btrfs_key key;
2507 int ret;
2508 int nritems;
2509 struct btrfs_key found_key;
2510 struct btrfs_key other_key;
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002511 struct btrfs_leaf_ref *ref;
2512 u64 leaf_gen;
2513 u64 leaf_start;
Chris Mason323ac952008-10-01 19:05:46 -04002514
2515 path->lowest_level = 1;
2516 key.objectid = inode->i_ino;
2517 key.type = BTRFS_CSUM_ITEM_KEY;
2518 key.offset = new_size;
2519again:
2520 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2521 if (ret < 0)
2522 goto out;
2523
2524 if (path->nodes[1] == NULL) {
2525 ret = 0;
2526 goto out;
2527 }
2528 ret = 0;
2529 btrfs_node_key_to_cpu(path->nodes[1], &found_key, path->slots[1]);
2530 nritems = btrfs_header_nritems(path->nodes[1]);
2531
2532 if (!nritems)
2533 goto out;
2534
2535 if (path->slots[1] >= nritems)
2536 goto next_node;
2537
2538 /* did we find a key greater than anything we want to delete? */
2539 if (found_key.objectid > inode->i_ino ||
2540 (found_key.objectid == inode->i_ino && found_key.type > key.type))
2541 goto out;
2542
2543 /* we check the next key in the node to make sure the leave contains
2544 * only checksum items. This comparison doesn't work if our
2545 * leaf is the last one in the node
2546 */
2547 if (path->slots[1] + 1 >= nritems) {
2548next_node:
2549 /* search forward from the last key in the node, this
2550 * will bring us into the next node in the tree
2551 */
2552 btrfs_node_key_to_cpu(path->nodes[1], &found_key, nritems - 1);
2553
2554 /* unlikely, but we inc below, so check to be safe */
2555 if (found_key.offset == (u64)-1)
2556 goto out;
2557
2558 /* search_forward needs a path with locks held, do the
2559 * search again for the original key. It is possible
2560 * this will race with a balance and return a path that
2561 * we could modify, but this drop is just an optimization
2562 * and is allowed to miss some leaves.
2563 */
2564 btrfs_release_path(root, path);
2565 found_key.offset++;
2566
2567 /* setup a max key for search_forward */
2568 other_key.offset = (u64)-1;
2569 other_key.type = key.type;
2570 other_key.objectid = key.objectid;
2571
2572 path->keep_locks = 1;
2573 ret = btrfs_search_forward(root, &found_key, &other_key,
2574 path, 0, 0);
2575 path->keep_locks = 0;
2576 if (ret || found_key.objectid != key.objectid ||
2577 found_key.type != key.type) {
2578 ret = 0;
2579 goto out;
2580 }
2581
2582 key.offset = found_key.offset;
2583 btrfs_release_path(root, path);
2584 cond_resched();
2585 goto again;
2586 }
2587
2588 /* we know there's one more slot after us in the tree,
2589 * read that key so we can verify it is also a checksum item
2590 */
2591 btrfs_node_key_to_cpu(path->nodes[1], &other_key, path->slots[1] + 1);
2592
2593 if (found_key.objectid < inode->i_ino)
2594 goto next_key;
2595
2596 if (found_key.type != key.type || found_key.offset < new_size)
2597 goto next_key;
2598
2599 /*
2600 * if the key for the next leaf isn't a csum key from this objectid,
2601 * we can't be sure there aren't good items inside this leaf.
2602 * Bail out
2603 */
2604 if (other_key.objectid != inode->i_ino || other_key.type != key.type)
2605 goto out;
2606
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002607 leaf_start = btrfs_node_blockptr(path->nodes[1], path->slots[1]);
2608 leaf_gen = btrfs_node_ptr_generation(path->nodes[1], path->slots[1]);
Chris Mason323ac952008-10-01 19:05:46 -04002609 /*
2610 * it is safe to delete this leaf, it contains only
2611 * csum items from this inode at an offset >= new_size
2612 */
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002613 ret = btrfs_del_leaf(trans, root, path, leaf_start);
Chris Mason323ac952008-10-01 19:05:46 -04002614 BUG_ON(ret);
2615
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002616 if (root->ref_cows && leaf_gen < trans->transid) {
2617 ref = btrfs_alloc_leaf_ref(root, 0);
2618 if (ref) {
2619 ref->root_gen = root->root_key.offset;
2620 ref->bytenr = leaf_start;
2621 ref->owner = 0;
2622 ref->generation = leaf_gen;
2623 ref->nritems = 0;
2624
Chris Masonbd56b302009-02-04 09:27:02 -05002625 btrfs_sort_leaf_ref(ref);
2626
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002627 ret = btrfs_add_leaf_ref(root, ref, 0);
2628 WARN_ON(ret);
2629 btrfs_free_leaf_ref(root, ref);
2630 } else {
2631 WARN_ON(1);
2632 }
2633 }
Chris Mason323ac952008-10-01 19:05:46 -04002634next_key:
2635 btrfs_release_path(root, path);
2636
2637 if (other_key.objectid == inode->i_ino &&
2638 other_key.type == key.type && other_key.offset > key.offset) {
2639 key.offset = other_key.offset;
2640 cond_resched();
2641 goto again;
2642 }
2643 ret = 0;
2644out:
2645 /* fixup any changes we've made to the path */
2646 path->lowest_level = 0;
2647 path->keep_locks = 0;
2648 btrfs_release_path(root, path);
2649 return ret;
2650}
2651
Chris Masond20f7042008-12-08 16:58:54 -05002652#endif
2653
Chris Mason323ac952008-10-01 19:05:46 -04002654/*
Chris Mason39279cc2007-06-12 06:35:45 -04002655 * this can truncate away extent items, csum items and directory items.
2656 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04002657 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04002658 *
2659 * csum items that cross the new i_size are truncated to the new size
2660 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04002661 *
2662 * min_type is the minimum key type to truncate down to. If set to 0, this
2663 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04002664 */
Chris Masone02119d2008-09-05 16:13:11 -04002665noinline int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
2666 struct btrfs_root *root,
2667 struct inode *inode,
2668 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04002669{
2670 int ret;
2671 struct btrfs_path *path;
2672 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04002673 struct btrfs_key found_key;
Chris Mason06d9a8d2009-02-04 09:30:58 -05002674 u32 found_type = (u8)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04002675 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002676 struct btrfs_file_extent_item *fi;
2677 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04002678 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002679 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002680 u64 item_end = 0;
2681 int found_extent;
2682 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05002683 int pending_del_nr = 0;
2684 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04002685 int extent_type = -1;
Chris Mason771ed682008-11-06 22:02:51 -05002686 int encoding;
Chris Mason3b951512008-04-17 11:29:12 -04002687 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002688
Chris Masone02119d2008-09-05 16:13:11 -04002689 if (root->ref_cows)
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002690 btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002691 path = btrfs_alloc_path();
2692 BUG_ON(!path);
Julia Lawall33c17ad2009-07-22 16:49:01 -04002693 path->reada = -1;
Chris Mason5f39d392007-10-15 16:14:19 -04002694
Chris Mason39279cc2007-06-12 06:35:45 -04002695 /* FIXME, add redo link to tree so we don't leak on crash */
2696 key.objectid = inode->i_ino;
2697 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04002698 key.type = (u8)-1;
2699
Chris Mason85e21ba2008-01-29 15:11:36 -05002700search_again:
Chris Masonb9473432009-03-13 11:00:37 -04002701 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05002702 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masond3977122009-01-05 21:25:51 -05002703 if (ret < 0)
Chris Mason85e21ba2008-01-29 15:11:36 -05002704 goto error;
Chris Masond3977122009-01-05 21:25:51 -05002705
Chris Mason85e21ba2008-01-29 15:11:36 -05002706 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002707 /* there are no items in the tree for us to truncate, we're
2708 * done
2709 */
2710 if (path->slots[0] == 0) {
2711 ret = 0;
2712 goto error;
2713 }
Chris Mason85e21ba2008-01-29 15:11:36 -05002714 path->slots[0]--;
2715 }
2716
Chris Masond3977122009-01-05 21:25:51 -05002717 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04002718 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04002719 leaf = path->nodes[0];
2720 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2721 found_type = btrfs_key_type(&found_key);
Chris Mason771ed682008-11-06 22:02:51 -05002722 encoding = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002723
Chris Mason5f39d392007-10-15 16:14:19 -04002724 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -04002725 break;
Chris Mason5f39d392007-10-15 16:14:19 -04002726
Chris Mason85e21ba2008-01-29 15:11:36 -05002727 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04002728 break;
2729
Chris Mason5f39d392007-10-15 16:14:19 -04002730 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04002731 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04002732 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04002733 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04002734 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Mason771ed682008-11-06 22:02:51 -05002735 encoding = btrfs_file_extent_compression(leaf, fi);
2736 encoding |= btrfs_file_extent_encryption(leaf, fi);
2737 encoding |= btrfs_file_extent_other_encoding(leaf, fi);
2738
Chris Mason179e29e2007-11-01 11:28:41 -04002739 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04002740 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04002741 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04002742 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04002743 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04002744 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04002745 }
Yan008630c2007-11-07 13:31:09 -05002746 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04002747 }
Chris Masone02119d2008-09-05 16:13:11 -04002748 if (item_end < new_size) {
Chris Masond3977122009-01-05 21:25:51 -05002749 if (found_type == BTRFS_DIR_ITEM_KEY)
Chris Masonb888db22007-08-27 16:49:44 -04002750 found_type = BTRFS_INODE_ITEM_KEY;
Chris Masond3977122009-01-05 21:25:51 -05002751 else if (found_type == BTRFS_EXTENT_ITEM_KEY)
Chris Masond20f7042008-12-08 16:58:54 -05002752 found_type = BTRFS_EXTENT_DATA_KEY;
Chris Masond3977122009-01-05 21:25:51 -05002753 else if (found_type == BTRFS_EXTENT_DATA_KEY)
Chris Mason85e21ba2008-01-29 15:11:36 -05002754 found_type = BTRFS_XATTR_ITEM_KEY;
Chris Masond3977122009-01-05 21:25:51 -05002755 else if (found_type == BTRFS_XATTR_ITEM_KEY)
Chris Mason85e21ba2008-01-29 15:11:36 -05002756 found_type = BTRFS_INODE_REF_KEY;
Chris Masond3977122009-01-05 21:25:51 -05002757 else if (found_type)
Chris Masonb888db22007-08-27 16:49:44 -04002758 found_type--;
Chris Masond3977122009-01-05 21:25:51 -05002759 else
Chris Masonb888db22007-08-27 16:49:44 -04002760 break;
Yana61721d2007-09-17 11:08:38 -04002761 btrfs_set_key_type(&key, found_type);
Chris Mason85e21ba2008-01-29 15:11:36 -05002762 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04002763 }
Chris Masone02119d2008-09-05 16:13:11 -04002764 if (found_key.offset >= new_size)
Chris Mason39279cc2007-06-12 06:35:45 -04002765 del_item = 1;
2766 else
2767 del_item = 0;
2768 found_extent = 0;
2769
2770 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04002771 if (found_type != BTRFS_EXTENT_DATA_KEY)
2772 goto delete;
2773
2774 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04002775 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04002776 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason771ed682008-11-06 22:02:51 -05002777 if (!del_item && !encoding) {
Chris Masondb945352007-10-15 16:15:53 -04002778 u64 orig_num_bytes =
2779 btrfs_file_extent_num_bytes(leaf, fi);
Chris Masone02119d2008-09-05 16:13:11 -04002780 extent_num_bytes = new_size -
Chris Mason5f39d392007-10-15 16:14:19 -04002781 found_key.offset + root->sectorsize - 1;
Yanb1632b12008-01-30 11:54:04 -05002782 extent_num_bytes = extent_num_bytes &
2783 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04002784 btrfs_set_file_extent_num_bytes(leaf, fi,
2785 extent_num_bytes);
2786 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05002787 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04002788 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002789 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04002790 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002791 } else {
Chris Masondb945352007-10-15 16:15:53 -04002792 extent_num_bytes =
2793 btrfs_file_extent_disk_num_bytes(leaf,
2794 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002795 extent_offset = found_key.offset -
2796 btrfs_file_extent_offset(leaf, fi);
2797
Chris Mason39279cc2007-06-12 06:35:45 -04002798 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05002799 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04002800 if (extent_start != 0) {
2801 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04002802 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002803 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04002804 }
2805 }
Chris Mason90692182008-02-08 13:49:28 -05002806 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04002807 /*
2808 * we can't truncate inline items that have had
2809 * special encodings
2810 */
2811 if (!del_item &&
2812 btrfs_file_extent_compression(leaf, fi) == 0 &&
2813 btrfs_file_extent_encryption(leaf, fi) == 0 &&
2814 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002815 u32 size = new_size - found_key.offset;
2816
2817 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002818 inode_sub_bytes(inode, item_end + 1 -
2819 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04002820 }
2821 size =
2822 btrfs_file_extent_calc_inline_size(size);
Chris Mason90692182008-02-08 13:49:28 -05002823 ret = btrfs_truncate_item(trans, root, path,
Chris Masone02119d2008-09-05 16:13:11 -04002824 size, 1);
Chris Mason90692182008-02-08 13:49:28 -05002825 BUG_ON(ret);
Chris Masone02119d2008-09-05 16:13:11 -04002826 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002827 inode_sub_bytes(inode, item_end + 1 -
2828 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05002829 }
Chris Mason39279cc2007-06-12 06:35:45 -04002830 }
Chris Mason179e29e2007-11-01 11:28:41 -04002831delete:
Chris Mason39279cc2007-06-12 06:35:45 -04002832 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05002833 if (!pending_del_nr) {
2834 /* no pending yet, add ourselves */
2835 pending_del_slot = path->slots[0];
2836 pending_del_nr = 1;
2837 } else if (pending_del_nr &&
2838 path->slots[0] + 1 == pending_del_slot) {
2839 /* hop on the pending chunk */
2840 pending_del_nr++;
2841 pending_del_slot = path->slots[0];
2842 } else {
Chris Masond3977122009-01-05 21:25:51 -05002843 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05002844 }
Chris Mason39279cc2007-06-12 06:35:45 -04002845 } else {
2846 break;
2847 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002848 if (found_extent && root->ref_cows) {
Chris Masonb9473432009-03-13 11:00:37 -04002849 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002850 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002851 extent_num_bytes, 0,
2852 btrfs_header_owner(leaf),
2853 inode->i_ino, extent_offset);
Chris Mason39279cc2007-06-12 06:35:45 -04002854 BUG_ON(ret);
2855 }
Chris Mason85e21ba2008-01-29 15:11:36 -05002856next:
2857 if (path->slots[0] == 0) {
2858 if (pending_del_nr)
2859 goto del_pending;
2860 btrfs_release_path(root, path);
Chris Mason06d9a8d2009-02-04 09:30:58 -05002861 if (found_type == BTRFS_INODE_ITEM_KEY)
2862 break;
Chris Mason85e21ba2008-01-29 15:11:36 -05002863 goto search_again;
2864 }
2865
2866 path->slots[0]--;
2867 if (pending_del_nr &&
2868 path->slots[0] + 1 != pending_del_slot) {
2869 struct btrfs_key debug;
2870del_pending:
2871 btrfs_item_key_to_cpu(path->nodes[0], &debug,
2872 pending_del_slot);
2873 ret = btrfs_del_items(trans, root, path,
2874 pending_del_slot,
2875 pending_del_nr);
2876 BUG_ON(ret);
2877 pending_del_nr = 0;
2878 btrfs_release_path(root, path);
Chris Mason06d9a8d2009-02-04 09:30:58 -05002879 if (found_type == BTRFS_INODE_ITEM_KEY)
2880 break;
Chris Mason85e21ba2008-01-29 15:11:36 -05002881 goto search_again;
2882 }
Chris Mason39279cc2007-06-12 06:35:45 -04002883 }
2884 ret = 0;
2885error:
Chris Mason85e21ba2008-01-29 15:11:36 -05002886 if (pending_del_nr) {
2887 ret = btrfs_del_items(trans, root, path, pending_del_slot,
2888 pending_del_nr);
2889 }
Chris Mason39279cc2007-06-12 06:35:45 -04002890 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002891 return ret;
2892}
2893
Chris Masona52d9a82007-08-27 16:49:44 -04002894/*
2895 * taken from block_truncate_page, but does cow as it zeros out
2896 * any bytes left in the last page in the file.
2897 */
2898static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
2899{
2900 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -04002901 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002902 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2903 struct btrfs_ordered_extent *ordered;
2904 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04002905 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04002906 pgoff_t index = from >> PAGE_CACHE_SHIFT;
2907 unsigned offset = from & (PAGE_CACHE_SIZE-1);
2908 struct page *page;
2909 int ret = 0;
2910 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002911 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04002912
2913 if ((offset & (blocksize - 1)) == 0)
2914 goto out;
2915
2916 ret = -ENOMEM;
Chris Mason211c17f2008-05-15 09:13:45 -04002917again:
Chris Masona52d9a82007-08-27 16:49:44 -04002918 page = grab_cache_page(mapping, index);
2919 if (!page)
2920 goto out;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002921
2922 page_start = page_offset(page);
2923 page_end = page_start + PAGE_CACHE_SIZE - 1;
2924
Chris Masona52d9a82007-08-27 16:49:44 -04002925 if (!PageUptodate(page)) {
2926 ret = btrfs_readpage(NULL, page);
2927 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04002928 if (page->mapping != mapping) {
2929 unlock_page(page);
2930 page_cache_release(page);
2931 goto again;
2932 }
Chris Masona52d9a82007-08-27 16:49:44 -04002933 if (!PageUptodate(page)) {
2934 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04002935 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04002936 }
2937 }
Chris Mason211c17f2008-05-15 09:13:45 -04002938 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002939
2940 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
2941 set_page_extent_mapped(page);
2942
2943 ordered = btrfs_lookup_ordered_extent(inode, page_start);
2944 if (ordered) {
2945 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
2946 unlock_page(page);
2947 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04002948 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002949 btrfs_put_ordered_extent(ordered);
2950 goto again;
2951 }
2952
Chris Masonea8c2812008-08-04 23:17:27 -04002953 btrfs_set_extent_delalloc(inode, page_start, page_end);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002954 ret = 0;
2955 if (offset != PAGE_CACHE_SIZE) {
2956 kaddr = kmap(page);
2957 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
2958 flush_dcache_page(page);
2959 kunmap(page);
2960 }
Chris Mason247e7432008-07-17 12:53:51 -04002961 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002962 set_page_dirty(page);
2963 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04002964
Chris Mason89642222008-07-24 09:41:53 -04002965out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04002966 unlock_page(page);
2967 page_cache_release(page);
2968out:
2969 return ret;
2970}
2971
Yan Zheng9036c102008-10-30 14:19:41 -04002972int btrfs_cont_expand(struct inode *inode, loff_t size)
2973{
2974 struct btrfs_trans_handle *trans;
2975 struct btrfs_root *root = BTRFS_I(inode)->root;
2976 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2977 struct extent_map *em;
2978 u64 mask = root->sectorsize - 1;
2979 u64 hole_start = (inode->i_size + mask) & ~mask;
2980 u64 block_end = (size + mask) & ~mask;
2981 u64 last_byte;
2982 u64 cur_offset;
2983 u64 hole_size;
2984 int err;
2985
2986 if (size <= hole_start)
2987 return 0;
2988
Josef Bacik6a632092009-02-20 11:00:09 -05002989 err = btrfs_check_metadata_free_space(root);
Yan Zheng9036c102008-10-30 14:19:41 -04002990 if (err)
2991 return err;
2992
2993 btrfs_truncate_page(inode->i_mapping, inode->i_size);
2994
2995 while (1) {
2996 struct btrfs_ordered_extent *ordered;
2997 btrfs_wait_ordered_range(inode, hole_start,
2998 block_end - hole_start);
2999 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
3000 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
3001 if (!ordered)
3002 break;
3003 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
3004 btrfs_put_ordered_extent(ordered);
3005 }
3006
3007 trans = btrfs_start_transaction(root, 1);
3008 btrfs_set_trans_block_group(trans, inode);
3009
3010 cur_offset = hole_start;
3011 while (1) {
3012 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
3013 block_end - cur_offset, 0);
3014 BUG_ON(IS_ERR(em) || !em);
3015 last_byte = min(extent_map_end(em), block_end);
3016 last_byte = (last_byte + mask) & ~mask;
3017 if (test_bit(EXTENT_FLAG_VACANCY, &em->flags)) {
Chris Mason771ed682008-11-06 22:02:51 -05003018 u64 hint_byte = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04003019 hole_size = last_byte - cur_offset;
Chris Mason771ed682008-11-06 22:02:51 -05003020 err = btrfs_drop_extents(trans, root, inode,
3021 cur_offset,
3022 cur_offset + hole_size,
Chris Masone980b502009-04-24 14:39:24 -04003023 block_end,
Chris Masona1ed8352009-09-11 12:27:37 -04003024 cur_offset, &hint_byte, 1);
Chris Mason771ed682008-11-06 22:02:51 -05003025 if (err)
3026 break;
Yan Zheng9036c102008-10-30 14:19:41 -04003027 err = btrfs_insert_file_extent(trans, root,
3028 inode->i_ino, cur_offset, 0,
3029 0, hole_size, 0, hole_size,
3030 0, 0, 0);
3031 btrfs_drop_extent_cache(inode, hole_start,
3032 last_byte - 1, 0);
3033 }
3034 free_extent_map(em);
3035 cur_offset = last_byte;
3036 if (err || cur_offset >= block_end)
3037 break;
3038 }
3039
3040 btrfs_end_transaction(trans, root);
3041 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
3042 return err;
3043}
3044
Chris Mason39279cc2007-06-12 06:35:45 -04003045static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
3046{
3047 struct inode *inode = dentry->d_inode;
3048 int err;
3049
3050 err = inode_change_ok(inode, attr);
3051 if (err)
3052 return err;
3053
Chris Mason5a3f23d2009-03-31 13:27:11 -04003054 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
3055 if (attr->ia_size > inode->i_size) {
3056 err = btrfs_cont_expand(inode, attr->ia_size);
3057 if (err)
3058 return err;
3059 } else if (inode->i_size > 0 &&
3060 attr->ia_size == 0) {
3061
3062 /* we're truncating a file that used to have good
3063 * data down to zero. Make sure it gets into
3064 * the ordered flush list so that any new writes
3065 * get down to disk quickly.
3066 */
3067 BTRFS_I(inode)->ordered_data_close = 1;
3068 }
Chris Mason39279cc2007-06-12 06:35:45 -04003069 }
Yan Zheng9036c102008-10-30 14:19:41 -04003070
Chris Mason39279cc2007-06-12 06:35:45 -04003071 err = inode_setattr(inode, attr);
Josef Bacik33268ea2008-07-24 12:16:36 -04003072
3073 if (!err && ((attr->ia_valid & ATTR_MODE)))
3074 err = btrfs_acl_chmod(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003075 return err;
3076}
Chris Mason61295eb2008-01-14 16:24:38 -05003077
Chris Mason39279cc2007-06-12 06:35:45 -04003078void btrfs_delete_inode(struct inode *inode)
3079{
3080 struct btrfs_trans_handle *trans;
3081 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdc2007-09-17 10:58:06 -04003082 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04003083 int ret;
3084
3085 truncate_inode_pages(&inode->i_data, 0);
3086 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04003087 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003088 goto no_delete;
3089 }
Chris Mason4a096752008-07-21 10:29:44 -04003090 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04003091
Yan, Zheng76dda932009-09-21 16:00:26 -04003092 if (inode->i_nlink > 0) {
3093 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
3094 goto no_delete;
3095 }
3096
Chris Masondbe674a2008-07-17 12:54:05 -04003097 btrfs_i_size_write(inode, 0);
Yan Zheng180591b2009-01-06 09:58:06 -05003098 trans = btrfs_join_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003099
Chris Mason39279cc2007-06-12 06:35:45 -04003100 btrfs_set_trans_block_group(trans, inode);
Chris Masone02119d2008-09-05 16:13:11 -04003101 ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size, 0);
Josef Bacik7b128762008-07-24 12:17:14 -04003102 if (ret) {
3103 btrfs_orphan_del(NULL, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04003104 goto no_delete_lock;
Josef Bacik7b128762008-07-24 12:17:14 -04003105 }
3106
3107 btrfs_orphan_del(trans, inode);
Chris Mason85e21ba2008-01-29 15:11:36 -05003108
Chris Masond3c2fdc2007-09-17 10:58:06 -04003109 nr = trans->blocks_used;
Chris Mason85e21ba2008-01-29 15:11:36 -05003110 clear_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003111
Chris Mason39279cc2007-06-12 06:35:45 -04003112 btrfs_end_transaction(trans, root);
Chris Masond3c2fdc2007-09-17 10:58:06 -04003113 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003114 return;
Chris Mason54aa1f42007-06-22 14:16:25 -04003115
3116no_delete_lock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04003117 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04003118 btrfs_end_transaction(trans, root);
Chris Masond3c2fdc2007-09-17 10:58:06 -04003119 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003120no_delete:
3121 clear_inode(inode);
3122}
3123
3124/*
3125 * this returns the key found in the dir entry in the location pointer.
3126 * If no dir entries were found, location->objectid is 0.
3127 */
3128static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
3129 struct btrfs_key *location)
3130{
3131 const char *name = dentry->d_name.name;
3132 int namelen = dentry->d_name.len;
3133 struct btrfs_dir_item *di;
3134 struct btrfs_path *path;
3135 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04003136 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003137
3138 path = btrfs_alloc_path();
3139 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -05003140
Chris Mason39279cc2007-06-12 06:35:45 -04003141 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
3142 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04003143 if (IS_ERR(di))
3144 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05003145
3146 if (!di || IS_ERR(di))
Chris Mason39544012007-12-12 14:38:19 -05003147 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05003148
Chris Mason5f39d392007-10-15 16:14:19 -04003149 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04003150out:
Chris Mason39279cc2007-06-12 06:35:45 -04003151 btrfs_free_path(path);
3152 return ret;
Chris Mason39544012007-12-12 14:38:19 -05003153out_err:
3154 location->objectid = 0;
3155 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04003156}
3157
3158/*
3159 * when we hit a tree root in a directory, the btrfs part of the inode
3160 * needs to be changed to reflect the root directory of the tree root. This
3161 * is kind of like crossing a mount point.
3162 */
3163static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003164 struct inode *dir,
3165 struct dentry *dentry,
3166 struct btrfs_key *location,
3167 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04003168{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003169 struct btrfs_path *path;
3170 struct btrfs_root *new_root;
3171 struct btrfs_root_ref *ref;
3172 struct extent_buffer *leaf;
3173 int ret;
3174 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003175
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003176 path = btrfs_alloc_path();
3177 if (!path) {
3178 err = -ENOMEM;
3179 goto out;
3180 }
Chris Mason39279cc2007-06-12 06:35:45 -04003181
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003182 err = -ENOENT;
3183 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
3184 BTRFS_I(dir)->root->root_key.objectid,
3185 location->objectid);
3186 if (ret) {
3187 if (ret < 0)
3188 err = ret;
3189 goto out;
3190 }
Chris Mason39279cc2007-06-12 06:35:45 -04003191
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003192 leaf = path->nodes[0];
3193 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
3194 if (btrfs_root_ref_dirid(leaf, ref) != dir->i_ino ||
3195 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
3196 goto out;
3197
3198 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
3199 (unsigned long)(ref + 1),
3200 dentry->d_name.len);
3201 if (ret)
3202 goto out;
3203
3204 btrfs_release_path(root->fs_info->tree_root, path);
3205
3206 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
3207 if (IS_ERR(new_root)) {
3208 err = PTR_ERR(new_root);
3209 goto out;
3210 }
3211
3212 if (btrfs_root_refs(&new_root->root_item) == 0) {
3213 err = -ENOENT;
3214 goto out;
3215 }
3216
3217 *sub_root = new_root;
3218 location->objectid = btrfs_root_dirid(&new_root->root_item);
3219 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04003220 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003221 err = 0;
3222out:
3223 btrfs_free_path(path);
3224 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003225}
3226
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003227static void inode_tree_add(struct inode *inode)
3228{
3229 struct btrfs_root *root = BTRFS_I(inode)->root;
3230 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003231 struct rb_node **p;
3232 struct rb_node *parent;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003233again:
3234 p = &root->inode_tree.rb_node;
3235 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003236
Yan, Zheng76dda932009-09-21 16:00:26 -04003237 if (hlist_unhashed(&inode->i_hash))
3238 return;
3239
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003240 spin_lock(&root->inode_lock);
3241 while (*p) {
3242 parent = *p;
3243 entry = rb_entry(parent, struct btrfs_inode, rb_node);
3244
3245 if (inode->i_ino < entry->vfs_inode.i_ino)
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003246 p = &parent->rb_left;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003247 else if (inode->i_ino > entry->vfs_inode.i_ino)
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003248 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003249 else {
3250 WARN_ON(!(entry->vfs_inode.i_state &
3251 (I_WILL_FREE | I_FREEING | I_CLEAR)));
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003252 rb_erase(parent, &root->inode_tree);
3253 RB_CLEAR_NODE(parent);
3254 spin_unlock(&root->inode_lock);
3255 goto again;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003256 }
3257 }
3258 rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
3259 rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
3260 spin_unlock(&root->inode_lock);
3261}
3262
3263static void inode_tree_del(struct inode *inode)
3264{
3265 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04003266 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003267
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003268 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003269 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003270 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003271 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04003272 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003273 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003274 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04003275
3276 if (empty && btrfs_root_refs(&root->root_item) == 0) {
3277 synchronize_srcu(&root->fs_info->subvol_srcu);
3278 spin_lock(&root->inode_lock);
3279 empty = RB_EMPTY_ROOT(&root->inode_tree);
3280 spin_unlock(&root->inode_lock);
3281 if (empty)
3282 btrfs_add_dead_root(root);
3283 }
3284}
3285
3286int btrfs_invalidate_inodes(struct btrfs_root *root)
3287{
3288 struct rb_node *node;
3289 struct rb_node *prev;
3290 struct btrfs_inode *entry;
3291 struct inode *inode;
3292 u64 objectid = 0;
3293
3294 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
3295
3296 spin_lock(&root->inode_lock);
3297again:
3298 node = root->inode_tree.rb_node;
3299 prev = NULL;
3300 while (node) {
3301 prev = node;
3302 entry = rb_entry(node, struct btrfs_inode, rb_node);
3303
3304 if (objectid < entry->vfs_inode.i_ino)
3305 node = node->rb_left;
3306 else if (objectid > entry->vfs_inode.i_ino)
3307 node = node->rb_right;
3308 else
3309 break;
3310 }
3311 if (!node) {
3312 while (prev) {
3313 entry = rb_entry(prev, struct btrfs_inode, rb_node);
3314 if (objectid <= entry->vfs_inode.i_ino) {
3315 node = prev;
3316 break;
3317 }
3318 prev = rb_next(prev);
3319 }
3320 }
3321 while (node) {
3322 entry = rb_entry(node, struct btrfs_inode, rb_node);
3323 objectid = entry->vfs_inode.i_ino + 1;
3324 inode = igrab(&entry->vfs_inode);
3325 if (inode) {
3326 spin_unlock(&root->inode_lock);
3327 if (atomic_read(&inode->i_count) > 1)
3328 d_prune_aliases(inode);
3329 /*
3330 * btrfs_drop_inode will remove it from
3331 * the inode cache when its usage count
3332 * hits zero.
3333 */
3334 iput(inode);
3335 cond_resched();
3336 spin_lock(&root->inode_lock);
3337 goto again;
3338 }
3339
3340 if (cond_resched_lock(&root->inode_lock))
3341 goto again;
3342
3343 node = rb_next(node);
3344 }
3345 spin_unlock(&root->inode_lock);
3346 return 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003347}
3348
Chris Masone02119d2008-09-05 16:13:11 -04003349static noinline void init_btrfs_i(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003350{
Chris Masone02119d2008-09-05 16:13:11 -04003351 struct btrfs_inode *bi = BTRFS_I(inode);
3352
Chris Masone02119d2008-09-05 16:13:11 -04003353 bi->generation = 0;
Chris Masonc3027eb2008-12-08 16:40:21 -05003354 bi->sequence = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003355 bi->last_trans = 0;
3356 bi->logged_trans = 0;
3357 bi->delalloc_bytes = 0;
Josef Bacik6a632092009-02-20 11:00:09 -05003358 bi->reserved_bytes = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003359 bi->disk_i_size = 0;
3360 bi->flags = 0;
3361 bi->index_cnt = (u64)-1;
Chris Mason12fcfd22009-03-24 10:24:20 -04003362 bi->last_unlink_trans = 0;
Chris Mason27574952009-05-14 13:10:02 -04003363 bi->ordered_data_close = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003364 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
3365 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04003366 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04003367 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
3368 inode->i_mapping, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04003369 INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes);
Chris Mason5a3f23d2009-03-31 13:27:11 -04003370 INIT_LIST_HEAD(&BTRFS_I(inode)->ordered_operations);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003371 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Chris Masonba1da2f2008-07-17 12:54:15 -04003372 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Masonee6e6502008-07-17 12:54:40 -04003373 mutex_init(&BTRFS_I(inode)->extent_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04003374 mutex_init(&BTRFS_I(inode)->log_mutex);
3375}
3376
3377static int btrfs_init_locked_inode(struct inode *inode, void *p)
3378{
3379 struct btrfs_iget_args *args = p;
3380 inode->i_ino = args->ino;
3381 init_btrfs_i(inode);
3382 BTRFS_I(inode)->root = args->root;
Josef Bacik6a632092009-02-20 11:00:09 -05003383 btrfs_set_inode_space_info(args->root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003384 return 0;
3385}
3386
3387static int btrfs_find_actor(struct inode *inode, void *opaque)
3388{
3389 struct btrfs_iget_args *args = opaque;
Chris Masond3977122009-01-05 21:25:51 -05003390 return args->ino == inode->i_ino &&
3391 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003392}
3393
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003394static struct inode *btrfs_iget_locked(struct super_block *s,
3395 u64 objectid,
3396 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04003397{
3398 struct inode *inode;
3399 struct btrfs_iget_args args;
3400 args.ino = objectid;
3401 args.root = root;
3402
3403 inode = iget5_locked(s, objectid, btrfs_find_actor,
3404 btrfs_init_locked_inode,
3405 (void *)&args);
3406 return inode;
3407}
3408
Balaji Rao1a54ef82008-07-21 02:01:04 +05303409/* Get an inode object given its location and corresponding root.
3410 * Returns in *is_new if the inode was read from disk
3411 */
3412struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003413 struct btrfs_root *root)
Balaji Rao1a54ef82008-07-21 02:01:04 +05303414{
3415 struct inode *inode;
3416
3417 inode = btrfs_iget_locked(s, location->objectid, root);
3418 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003419 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05303420
3421 if (inode->i_state & I_NEW) {
3422 BTRFS_I(inode)->root = root;
3423 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
3424 btrfs_read_locked_inode(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003425
3426 inode_tree_add(inode);
Balaji Rao1a54ef82008-07-21 02:01:04 +05303427 unlock_new_inode(inode);
Balaji Rao1a54ef82008-07-21 02:01:04 +05303428 }
3429
3430 return inode;
3431}
3432
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003433static struct inode *new_simple_dir(struct super_block *s,
3434 struct btrfs_key *key,
3435 struct btrfs_root *root)
3436{
3437 struct inode *inode = new_inode(s);
3438
3439 if (!inode)
3440 return ERR_PTR(-ENOMEM);
3441
3442 init_btrfs_i(inode);
3443
3444 BTRFS_I(inode)->root = root;
3445 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
3446 BTRFS_I(inode)->dummy_inode = 1;
3447
3448 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
3449 inode->i_op = &simple_dir_inode_operations;
3450 inode->i_fop = &simple_dir_operations;
3451 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
3452 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
3453
3454 return inode;
3455}
3456
Chris Mason3de45862008-11-17 21:02:50 -05003457struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04003458{
Chris Masond3977122009-01-05 21:25:51 -05003459 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003460 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003461 struct btrfs_root *sub_root = root;
3462 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04003463 int index;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003464 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04003465
Yan, Zheng76dda932009-09-21 16:00:26 -04003466 dentry->d_op = &btrfs_dentry_operations;
3467
Chris Mason39279cc2007-06-12 06:35:45 -04003468 if (dentry->d_name.len > BTRFS_NAME_LEN)
3469 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04003470
Chris Mason39279cc2007-06-12 06:35:45 -04003471 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04003472
Chris Mason39279cc2007-06-12 06:35:45 -04003473 if (ret < 0)
3474 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04003475
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003476 if (location.objectid == 0)
3477 return NULL;
3478
3479 if (location.type == BTRFS_INODE_ITEM_KEY) {
3480 inode = btrfs_iget(dir->i_sb, &location, root);
3481 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003482 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003483
3484 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
3485
Yan, Zheng76dda932009-09-21 16:00:26 -04003486 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003487 ret = fixup_tree_root_location(root, dir, dentry,
3488 &location, &sub_root);
3489 if (ret < 0) {
3490 if (ret != -ENOENT)
3491 inode = ERR_PTR(ret);
3492 else
3493 inode = new_simple_dir(dir->i_sb, &location, sub_root);
3494 } else {
Chris Mason39279cc2007-06-12 06:35:45 -04003495 inode = btrfs_iget(dir->i_sb, &location, sub_root);
Chris Mason39279cc2007-06-12 06:35:45 -04003496 }
Yan, Zheng76dda932009-09-21 16:00:26 -04003497 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
3498
Chris Mason3de45862008-11-17 21:02:50 -05003499 return inode;
3500}
3501
Yan, Zheng76dda932009-09-21 16:00:26 -04003502static int btrfs_dentry_delete(struct dentry *dentry)
3503{
3504 struct btrfs_root *root;
3505
3506 if (!dentry->d_inode)
3507 return 0;
3508
3509 root = BTRFS_I(dentry->d_inode)->root;
3510 if (btrfs_root_refs(&root->root_item) == 0)
3511 return 1;
3512 return 0;
3513}
3514
Chris Mason3de45862008-11-17 21:02:50 -05003515static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
3516 struct nameidata *nd)
3517{
3518 struct inode *inode;
3519
Chris Mason3de45862008-11-17 21:02:50 -05003520 inode = btrfs_lookup_dentry(dir, dentry);
3521 if (IS_ERR(inode))
3522 return ERR_CAST(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003523
Chris Mason39279cc2007-06-12 06:35:45 -04003524 return d_splice_alias(inode, dentry);
3525}
3526
3527static unsigned char btrfs_filetype_table[] = {
3528 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
3529};
3530
David Woodhousecbdf5a22008-08-06 19:42:33 +01003531static int btrfs_real_readdir(struct file *filp, void *dirent,
3532 filldir_t filldir)
Chris Mason39279cc2007-06-12 06:35:45 -04003533{
3534 struct inode *inode = filp->f_dentry->d_inode;
3535 struct btrfs_root *root = BTRFS_I(inode)->root;
3536 struct btrfs_item *item;
3537 struct btrfs_dir_item *di;
3538 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04003539 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04003540 struct btrfs_path *path;
3541 int ret;
3542 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04003543 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003544 int slot;
3545 int advance;
3546 unsigned char d_type;
3547 int over = 0;
3548 u32 di_cur;
3549 u32 di_total;
3550 u32 di_len;
3551 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04003552 char tmp_name[32];
3553 char *name_ptr;
3554 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04003555
3556 /* FIXME, use a real flag for deciding about the key type */
3557 if (root->fs_info->tree_root == root)
3558 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04003559
Chris Mason39544012007-12-12 14:38:19 -05003560 /* special case for "." */
3561 if (filp->f_pos == 0) {
3562 over = filldir(dirent, ".", 1,
3563 1, inode->i_ino,
3564 DT_DIR);
3565 if (over)
3566 return 0;
3567 filp->f_pos = 1;
3568 }
Chris Mason39544012007-12-12 14:38:19 -05003569 /* special case for .., just use the back ref */
3570 if (filp->f_pos == 1) {
David Woodhouse5ecc7e52008-08-17 15:14:48 +01003571 u64 pino = parent_ino(filp->f_path.dentry);
Chris Mason39544012007-12-12 14:38:19 -05003572 over = filldir(dirent, "..", 2,
David Woodhouse5ecc7e52008-08-17 15:14:48 +01003573 2, pino, DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05003574 if (over)
David Woodhouse49593bf2008-08-17 17:08:36 +01003575 return 0;
Chris Mason39544012007-12-12 14:38:19 -05003576 filp->f_pos = 2;
3577 }
David Woodhouse49593bf2008-08-17 17:08:36 +01003578 path = btrfs_alloc_path();
3579 path->reada = 2;
3580
Chris Mason39279cc2007-06-12 06:35:45 -04003581 btrfs_set_key_type(&key, key_type);
3582 key.offset = filp->f_pos;
David Woodhouse49593bf2008-08-17 17:08:36 +01003583 key.objectid = inode->i_ino;
Chris Mason5f39d392007-10-15 16:14:19 -04003584
Chris Mason39279cc2007-06-12 06:35:45 -04003585 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3586 if (ret < 0)
3587 goto err;
3588 advance = 0;
David Woodhouse49593bf2008-08-17 17:08:36 +01003589
3590 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04003591 leaf = path->nodes[0];
3592 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003593 slot = path->slots[0];
3594 if (advance || slot >= nritems) {
David Woodhouse49593bf2008-08-17 17:08:36 +01003595 if (slot >= nritems - 1) {
Chris Mason39279cc2007-06-12 06:35:45 -04003596 ret = btrfs_next_leaf(root, path);
3597 if (ret)
3598 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003599 leaf = path->nodes[0];
3600 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003601 slot = path->slots[0];
3602 } else {
3603 slot++;
3604 path->slots[0]++;
3605 }
3606 }
Chris Mason3de45862008-11-17 21:02:50 -05003607
Chris Mason39279cc2007-06-12 06:35:45 -04003608 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04003609 item = btrfs_item_nr(leaf, slot);
3610 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3611
3612 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04003613 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003614 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003615 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003616 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04003617 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04003618
3619 filp->f_pos = found_key.offset;
David Woodhouse49593bf2008-08-17 17:08:36 +01003620
Chris Mason39279cc2007-06-12 06:35:45 -04003621 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
3622 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003623 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01003624
3625 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04003626 struct btrfs_key location;
3627
3628 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01003629 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04003630 name_ptr = tmp_name;
3631 } else {
3632 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01003633 if (!name_ptr) {
3634 ret = -ENOMEM;
3635 goto err;
3636 }
Chris Mason5f39d392007-10-15 16:14:19 -04003637 }
3638 read_extent_buffer(leaf, name_ptr,
3639 (unsigned long)(di + 1), name_len);
3640
3641 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
3642 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05003643
3644 /* is this a reference to our own snapshot? If so
3645 * skip it
3646 */
3647 if (location.type == BTRFS_ROOT_ITEM_KEY &&
3648 location.objectid == root->root_key.objectid) {
3649 over = 0;
3650 goto skip;
3651 }
Chris Mason5f39d392007-10-15 16:14:19 -04003652 over = filldir(dirent, name_ptr, name_len,
David Woodhouse49593bf2008-08-17 17:08:36 +01003653 found_key.offset, location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04003654 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04003655
Chris Mason3de45862008-11-17 21:02:50 -05003656skip:
Chris Mason5f39d392007-10-15 16:14:19 -04003657 if (name_ptr != tmp_name)
3658 kfree(name_ptr);
3659
Chris Mason39279cc2007-06-12 06:35:45 -04003660 if (over)
3661 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05003662 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01003663 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04003664 di_cur += di_len;
3665 di = (struct btrfs_dir_item *)((char *)di + di_len);
3666 }
3667 }
David Woodhouse49593bf2008-08-17 17:08:36 +01003668
3669 /* Reached end of directory/root. Bump pos past the last item. */
Yan Zheng5e591a02008-02-19 11:41:02 -05003670 if (key_type == BTRFS_DIR_INDEX_KEY)
Chris Mason89f135d2009-01-28 15:34:27 -05003671 filp->f_pos = INT_LIMIT(off_t);
Yan Zheng5e591a02008-02-19 11:41:02 -05003672 else
3673 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04003674nopos:
3675 ret = 0;
3676err:
Chris Mason39279cc2007-06-12 06:35:45 -04003677 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003678 return ret;
3679}
3680
3681int btrfs_write_inode(struct inode *inode, int wait)
3682{
3683 struct btrfs_root *root = BTRFS_I(inode)->root;
3684 struct btrfs_trans_handle *trans;
3685 int ret = 0;
3686
Yan Zhengc146afa2008-11-12 14:34:12 -05003687 if (root->fs_info->btree_inode == inode)
Chris Mason4ca8b412008-08-05 13:30:48 -04003688 return 0;
3689
Chris Mason39279cc2007-06-12 06:35:45 -04003690 if (wait) {
Chris Masonf9295742008-07-17 12:54:14 -04003691 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003692 btrfs_set_trans_block_group(trans, inode);
3693 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04003694 }
3695 return ret;
3696}
3697
3698/*
Chris Mason54aa1f42007-06-22 14:16:25 -04003699 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04003700 * inode changes. But, it is most likely to find the inode in cache.
3701 * FIXME, needs more benchmarking...there are no reasons other than performance
3702 * to keep or drop this code.
3703 */
3704void btrfs_dirty_inode(struct inode *inode)
3705{
3706 struct btrfs_root *root = BTRFS_I(inode)->root;
3707 struct btrfs_trans_handle *trans;
3708
Chris Masonf9295742008-07-17 12:54:14 -04003709 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003710 btrfs_set_trans_block_group(trans, inode);
3711 btrfs_update_inode(trans, root, inode);
3712 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04003713}
3714
Chris Masond352ac62008-09-29 15:18:18 -04003715/*
3716 * find the highest existing sequence number in a directory
3717 * and then set the in-memory index_cnt variable to reflect
3718 * free sequence numbers
3719 */
Josef Bacikaec74772008-07-24 12:12:38 -04003720static int btrfs_set_inode_index_count(struct inode *inode)
3721{
3722 struct btrfs_root *root = BTRFS_I(inode)->root;
3723 struct btrfs_key key, found_key;
3724 struct btrfs_path *path;
3725 struct extent_buffer *leaf;
3726 int ret;
3727
3728 key.objectid = inode->i_ino;
3729 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
3730 key.offset = (u64)-1;
3731
3732 path = btrfs_alloc_path();
3733 if (!path)
3734 return -ENOMEM;
3735
3736 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3737 if (ret < 0)
3738 goto out;
3739 /* FIXME: we should be able to handle this */
3740 if (ret == 0)
3741 goto out;
3742 ret = 0;
3743
3744 /*
3745 * MAGIC NUMBER EXPLANATION:
3746 * since we search a directory based on f_pos we have to start at 2
3747 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
3748 * else has to start at 2
3749 */
3750 if (path->slots[0] == 0) {
3751 BTRFS_I(inode)->index_cnt = 2;
3752 goto out;
3753 }
3754
3755 path->slots[0]--;
3756
3757 leaf = path->nodes[0];
3758 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3759
3760 if (found_key.objectid != inode->i_ino ||
3761 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
3762 BTRFS_I(inode)->index_cnt = 2;
3763 goto out;
3764 }
3765
3766 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
3767out:
3768 btrfs_free_path(path);
3769 return ret;
3770}
3771
Chris Masond352ac62008-09-29 15:18:18 -04003772/*
3773 * helper to find a free sequence number in a given directory. This current
3774 * code is very simple, later versions will do smarter things in the btree
3775 */
Chris Mason3de45862008-11-17 21:02:50 -05003776int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04003777{
3778 int ret = 0;
3779
3780 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
3781 ret = btrfs_set_inode_index_count(dir);
Chris Masond3977122009-01-05 21:25:51 -05003782 if (ret)
Josef Bacikaec74772008-07-24 12:12:38 -04003783 return ret;
3784 }
3785
Chris Mason00e4e6b2008-08-05 11:18:09 -04003786 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04003787 BTRFS_I(dir)->index_cnt++;
3788
3789 return ret;
3790}
3791
Chris Mason39279cc2007-06-12 06:35:45 -04003792static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
3793 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04003794 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05003795 const char *name, int name_len,
Yan Zhengd2fb3432008-12-11 16:30:39 -05003796 u64 ref_objectid, u64 objectid,
3797 u64 alloc_hint, int mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04003798{
3799 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04003800 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04003801 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04003802 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05003803 struct btrfs_inode_ref *ref;
3804 struct btrfs_key key[2];
3805 u32 sizes[2];
3806 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04003807 int ret;
3808 int owner;
3809
Chris Mason5f39d392007-10-15 16:14:19 -04003810 path = btrfs_alloc_path();
3811 BUG_ON(!path);
3812
Chris Mason39279cc2007-06-12 06:35:45 -04003813 inode = new_inode(root->fs_info->sb);
3814 if (!inode)
3815 return ERR_PTR(-ENOMEM);
3816
Josef Bacikaec74772008-07-24 12:12:38 -04003817 if (dir) {
Chris Mason3de45862008-11-17 21:02:50 -05003818 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04003819 if (ret) {
3820 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04003821 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04003822 }
Josef Bacikaec74772008-07-24 12:12:38 -04003823 }
3824 /*
3825 * index_cnt is ignored for everything but a dir,
3826 * btrfs_get_inode_index_count has an explanation for the magic
3827 * number
3828 */
Chris Masone02119d2008-09-05 16:13:11 -04003829 init_btrfs_i(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04003830 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04003831 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04003832 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik6a632092009-02-20 11:00:09 -05003833 btrfs_set_inode_space_info(root, inode);
Chris Masonb888db22007-08-27 16:49:44 -04003834
Chris Mason39279cc2007-06-12 06:35:45 -04003835 if (mode & S_IFDIR)
3836 owner = 0;
3837 else
3838 owner = 1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003839 BTRFS_I(inode)->block_group =
3840 btrfs_find_block_group(root, 0, alloc_hint, owner);
Chris Mason9c583092008-01-29 15:15:18 -05003841
3842 key[0].objectid = objectid;
3843 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
3844 key[0].offset = 0;
3845
3846 key[1].objectid = objectid;
3847 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
3848 key[1].offset = ref_objectid;
3849
3850 sizes[0] = sizeof(struct btrfs_inode_item);
3851 sizes[1] = name_len + sizeof(*ref);
3852
Chris Masonb9473432009-03-13 11:00:37 -04003853 path->leave_spinning = 1;
Chris Mason9c583092008-01-29 15:15:18 -05003854 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
3855 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04003856 goto fail;
3857
Chris Mason79683f22008-11-19 22:00:53 -05003858 inode->i_uid = current_fsuid();
Chris Ball8c087b52009-02-04 09:29:54 -05003859
Chris Mason42f15d72009-02-06 11:35:57 -05003860 if (dir && (dir->i_mode & S_ISGID)) {
Chris Ball8c087b52009-02-04 09:29:54 -05003861 inode->i_gid = dir->i_gid;
3862 if (S_ISDIR(mode))
3863 mode |= S_ISGID;
3864 } else
3865 inode->i_gid = current_fsgid();
3866
Chris Mason39279cc2007-06-12 06:35:45 -04003867 inode->i_mode = mode;
3868 inode->i_ino = objectid;
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003869 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003870 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04003871 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3872 struct btrfs_inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003873 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05003874
3875 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
3876 struct btrfs_inode_ref);
3877 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04003878 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05003879 ptr = (unsigned long)(ref + 1);
3880 write_extent_buffer(path->nodes[0], name, ptr, name_len);
3881
Chris Mason5f39d392007-10-15 16:14:19 -04003882 btrfs_mark_buffer_dirty(path->nodes[0]);
3883 btrfs_free_path(path);
3884
Chris Mason39279cc2007-06-12 06:35:45 -04003885 location = &BTRFS_I(inode)->location;
3886 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04003887 location->offset = 0;
3888 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
3889
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003890 btrfs_inherit_iflags(inode, dir);
3891
Chris Mason94272162009-07-02 12:26:06 -04003892 if ((mode & S_IFREG)) {
3893 if (btrfs_test_opt(root, NODATASUM))
3894 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
3895 if (btrfs_test_opt(root, NODATACOW))
3896 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
3897 }
3898
Chris Mason39279cc2007-06-12 06:35:45 -04003899 insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003900 inode_tree_add(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003901 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04003902fail:
Josef Bacikaec74772008-07-24 12:12:38 -04003903 if (dir)
3904 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04003905 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04003906 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003907 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003908}
3909
3910static inline u8 btrfs_inode_type(struct inode *inode)
3911{
3912 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
3913}
3914
Chris Masond352ac62008-09-29 15:18:18 -04003915/*
3916 * utility function to add 'inode' into 'parent_inode' with
3917 * a give name and a given sequence number.
3918 * if 'add_backref' is true, also insert a backref from the
3919 * inode to the parent directory.
3920 */
Chris Masone02119d2008-09-05 16:13:11 -04003921int btrfs_add_link(struct btrfs_trans_handle *trans,
3922 struct inode *parent_inode, struct inode *inode,
3923 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04003924{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003925 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003926 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04003927 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Chris Mason5f39d392007-10-15 16:14:19 -04003928
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003929 if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
3930 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
3931 } else {
3932 key.objectid = inode->i_ino;
3933 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
3934 key.offset = 0;
3935 }
Chris Mason39279cc2007-06-12 06:35:45 -04003936
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003937 if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
3938 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
3939 key.objectid, root->root_key.objectid,
3940 parent_inode->i_ino,
3941 index, name, name_len);
3942 } else if (add_backref) {
3943 ret = btrfs_insert_inode_ref(trans, root,
3944 name, name_len, inode->i_ino,
3945 parent_inode->i_ino, index);
3946 }
3947
Chris Mason39279cc2007-06-12 06:35:45 -04003948 if (ret == 0) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003949 ret = btrfs_insert_dir_item(trans, root, name, name_len,
3950 parent_inode->i_ino, &key,
3951 btrfs_inode_type(inode), index);
3952 BUG_ON(ret);
3953
Chris Masondbe674a2008-07-17 12:54:05 -04003954 btrfs_i_size_write(parent_inode, parent_inode->i_size +
Chris Masone02119d2008-09-05 16:13:11 -04003955 name_len * 2);
Chris Mason79c44582007-06-25 10:09:33 -04003956 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Masone02119d2008-09-05 16:13:11 -04003957 ret = btrfs_update_inode(trans, root, parent_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003958 }
3959 return ret;
3960}
3961
3962static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05003963 struct dentry *dentry, struct inode *inode,
Chris Mason00e4e6b2008-08-05 11:18:09 -04003964 int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04003965{
Chris Masone02119d2008-09-05 16:13:11 -04003966 int err = btrfs_add_link(trans, dentry->d_parent->d_inode,
3967 inode, dentry->d_name.name,
3968 dentry->d_name.len, backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04003969 if (!err) {
3970 d_instantiate(dentry, inode);
3971 return 0;
3972 }
3973 if (err > 0)
3974 err = -EEXIST;
3975 return err;
3976}
3977
Josef Bacik618e21d2007-07-11 10:18:17 -04003978static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
3979 int mode, dev_t rdev)
3980{
3981 struct btrfs_trans_handle *trans;
3982 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05003983 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04003984 int err;
3985 int drop_inode = 0;
3986 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05003987 unsigned long nr = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04003988 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04003989
3990 if (!new_valid_dev(rdev))
3991 return -EINVAL;
3992
Josef Bacik6a632092009-02-20 11:00:09 -05003993 err = btrfs_check_metadata_free_space(root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003994 if (err)
3995 goto fail;
3996
Josef Bacik618e21d2007-07-11 10:18:17 -04003997 trans = btrfs_start_transaction(root, 1);
3998 btrfs_set_trans_block_group(trans, dir);
3999
4000 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
4001 if (err) {
4002 err = -ENOSPC;
4003 goto out_unlock;
4004 }
4005
Josef Bacikaec74772008-07-24 12:12:38 -04004006 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05004007 dentry->d_name.len,
4008 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04004009 BTRFS_I(dir)->block_group, mode, &index);
Josef Bacik618e21d2007-07-11 10:18:17 -04004010 err = PTR_ERR(inode);
4011 if (IS_ERR(inode))
4012 goto out_unlock;
4013
Jim Owens0279b4c2009-02-04 09:29:13 -05004014 err = btrfs_init_inode_security(inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04004015 if (err) {
4016 drop_inode = 1;
4017 goto out_unlock;
4018 }
4019
Josef Bacik618e21d2007-07-11 10:18:17 -04004020 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04004021 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04004022 if (err)
4023 drop_inode = 1;
4024 else {
4025 inode->i_op = &btrfs_special_inode_operations;
4026 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04004027 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04004028 }
Josef Bacik618e21d2007-07-11 10:18:17 -04004029 btrfs_update_inode_block_group(trans, inode);
4030 btrfs_update_inode_block_group(trans, dir);
4031out_unlock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04004032 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04004033 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05004034fail:
Josef Bacik618e21d2007-07-11 10:18:17 -04004035 if (drop_inode) {
4036 inode_dec_link_count(inode);
4037 iput(inode);
4038 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04004039 btrfs_btree_balance_dirty(root, nr);
Josef Bacik618e21d2007-07-11 10:18:17 -04004040 return err;
4041}
4042
Chris Mason39279cc2007-06-12 06:35:45 -04004043static int btrfs_create(struct inode *dir, struct dentry *dentry,
4044 int mode, struct nameidata *nd)
4045{
4046 struct btrfs_trans_handle *trans;
4047 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05004048 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04004049 int err;
4050 int drop_inode = 0;
Chris Mason1832a6d2007-12-21 16:27:21 -05004051 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004052 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004053 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004054
Josef Bacik6a632092009-02-20 11:00:09 -05004055 err = btrfs_check_metadata_free_space(root);
Chris Mason1832a6d2007-12-21 16:27:21 -05004056 if (err)
4057 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04004058 trans = btrfs_start_transaction(root, 1);
4059 btrfs_set_trans_block_group(trans, dir);
4060
4061 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
4062 if (err) {
4063 err = -ENOSPC;
4064 goto out_unlock;
4065 }
4066
Josef Bacikaec74772008-07-24 12:12:38 -04004067 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05004068 dentry->d_name.len,
4069 dentry->d_parent->d_inode->i_ino,
Chris Mason00e4e6b2008-08-05 11:18:09 -04004070 objectid, BTRFS_I(dir)->block_group, mode,
4071 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04004072 err = PTR_ERR(inode);
4073 if (IS_ERR(inode))
4074 goto out_unlock;
4075
Jim Owens0279b4c2009-02-04 09:29:13 -05004076 err = btrfs_init_inode_security(inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04004077 if (err) {
4078 drop_inode = 1;
4079 goto out_unlock;
4080 }
4081
Chris Mason39279cc2007-06-12 06:35:45 -04004082 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04004083 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004084 if (err)
4085 drop_inode = 1;
4086 else {
4087 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04004088 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04004089 inode->i_fop = &btrfs_file_operations;
4090 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05004091 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04004092 }
Chris Mason39279cc2007-06-12 06:35:45 -04004093 btrfs_update_inode_block_group(trans, inode);
4094 btrfs_update_inode_block_group(trans, dir);
4095out_unlock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04004096 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04004097 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05004098fail:
Chris Mason39279cc2007-06-12 06:35:45 -04004099 if (drop_inode) {
4100 inode_dec_link_count(inode);
4101 iput(inode);
4102 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04004103 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004104 return err;
4105}
4106
4107static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
4108 struct dentry *dentry)
4109{
4110 struct btrfs_trans_handle *trans;
4111 struct btrfs_root *root = BTRFS_I(dir)->root;
4112 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004113 u64 index;
Chris Mason1832a6d2007-12-21 16:27:21 -05004114 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004115 int err;
4116 int drop_inode = 0;
4117
4118 if (inode->i_nlink == 0)
4119 return -ENOENT;
4120
Chris Masone02119d2008-09-05 16:13:11 -04004121 btrfs_inc_nlink(inode);
Josef Bacik6a632092009-02-20 11:00:09 -05004122 err = btrfs_check_metadata_free_space(root);
Chris Mason1832a6d2007-12-21 16:27:21 -05004123 if (err)
4124 goto fail;
Chris Mason3de45862008-11-17 21:02:50 -05004125 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004126 if (err)
4127 goto fail;
4128
Chris Mason39279cc2007-06-12 06:35:45 -04004129 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04004130
Chris Mason39279cc2007-06-12 06:35:45 -04004131 btrfs_set_trans_block_group(trans, dir);
4132 atomic_inc(&inode->i_count);
Josef Bacikaec74772008-07-24 12:12:38 -04004133
Chris Mason00e4e6b2008-08-05 11:18:09 -04004134 err = btrfs_add_nondir(trans, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04004135
Yan, Zhenga5719522009-09-24 09:17:31 -04004136 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04004137 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04004138 } else {
4139 btrfs_update_inode_block_group(trans, dir);
4140 err = btrfs_update_inode(trans, root, inode);
4141 BUG_ON(err);
4142 btrfs_log_new_name(trans, inode, NULL, dentry->d_parent);
4143 }
Chris Mason39279cc2007-06-12 06:35:45 -04004144
Chris Masond3c2fdc2007-09-17 10:58:06 -04004145 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04004146 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05004147fail:
Chris Mason39279cc2007-06-12 06:35:45 -04004148 if (drop_inode) {
4149 inode_dec_link_count(inode);
4150 iput(inode);
4151 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04004152 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004153 return err;
4154}
4155
Chris Mason39279cc2007-06-12 06:35:45 -04004156static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
4157{
Chris Masonb9d86662008-05-02 16:13:49 -04004158 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04004159 struct btrfs_trans_handle *trans;
4160 struct btrfs_root *root = BTRFS_I(dir)->root;
4161 int err = 0;
4162 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04004163 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004164 u64 index = 0;
Chris Masond3c2fdc2007-09-17 10:58:06 -04004165 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04004166
Josef Bacik6a632092009-02-20 11:00:09 -05004167 err = btrfs_check_metadata_free_space(root);
Chris Mason1832a6d2007-12-21 16:27:21 -05004168 if (err)
4169 goto out_unlock;
4170
Chris Mason39279cc2007-06-12 06:35:45 -04004171 trans = btrfs_start_transaction(root, 1);
4172 btrfs_set_trans_block_group(trans, dir);
Chris Mason5f39d392007-10-15 16:14:19 -04004173
Chris Mason39279cc2007-06-12 06:35:45 -04004174 if (IS_ERR(trans)) {
4175 err = PTR_ERR(trans);
4176 goto out_unlock;
4177 }
4178
4179 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
4180 if (err) {
4181 err = -ENOSPC;
4182 goto out_unlock;
4183 }
4184
Josef Bacikaec74772008-07-24 12:12:38 -04004185 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05004186 dentry->d_name.len,
4187 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04004188 BTRFS_I(dir)->block_group, S_IFDIR | mode,
4189 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04004190 if (IS_ERR(inode)) {
4191 err = PTR_ERR(inode);
4192 goto out_fail;
4193 }
Chris Mason5f39d392007-10-15 16:14:19 -04004194
Chris Mason39279cc2007-06-12 06:35:45 -04004195 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04004196
Jim Owens0279b4c2009-02-04 09:29:13 -05004197 err = btrfs_init_inode_security(inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04004198 if (err)
4199 goto out_fail;
4200
Chris Mason39279cc2007-06-12 06:35:45 -04004201 inode->i_op = &btrfs_dir_inode_operations;
4202 inode->i_fop = &btrfs_dir_file_operations;
4203 btrfs_set_trans_block_group(trans, inode);
4204
Chris Masondbe674a2008-07-17 12:54:05 -04004205 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004206 err = btrfs_update_inode(trans, root, inode);
4207 if (err)
4208 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04004209
Chris Masone02119d2008-09-05 16:13:11 -04004210 err = btrfs_add_link(trans, dentry->d_parent->d_inode,
4211 inode, dentry->d_name.name,
4212 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004213 if (err)
4214 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04004215
Chris Mason39279cc2007-06-12 06:35:45 -04004216 d_instantiate(dentry, inode);
4217 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004218 btrfs_update_inode_block_group(trans, inode);
4219 btrfs_update_inode_block_group(trans, dir);
4220
4221out_fail:
Chris Masond3c2fdc2007-09-17 10:58:06 -04004222 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04004223 btrfs_end_transaction_throttle(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04004224
Chris Mason39279cc2007-06-12 06:35:45 -04004225out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04004226 if (drop_on_err)
4227 iput(inode);
Chris Masond3c2fdc2007-09-17 10:58:06 -04004228 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004229 return err;
4230}
4231
Chris Masond352ac62008-09-29 15:18:18 -04004232/* helper for btfs_get_extent. Given an existing extent in the tree,
4233 * and an extent that you want to insert, deal with overlap and insert
4234 * the new extent into the tree.
4235 */
Chris Mason3b951512008-04-17 11:29:12 -04004236static int merge_extent_mapping(struct extent_map_tree *em_tree,
4237 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04004238 struct extent_map *em,
4239 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04004240{
4241 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04004242
Chris Masone6dcd2d2008-07-17 12:53:50 -04004243 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
4244 start_diff = map_start - em->start;
4245 em->start = map_start;
4246 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04004247 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
4248 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04004249 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04004250 em->block_len -= start_diff;
4251 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004252 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04004253}
4254
Chris Masonc8b97812008-10-29 14:49:59 -04004255static noinline int uncompress_inline(struct btrfs_path *path,
4256 struct inode *inode, struct page *page,
4257 size_t pg_offset, u64 extent_offset,
4258 struct btrfs_file_extent_item *item)
4259{
4260 int ret;
4261 struct extent_buffer *leaf = path->nodes[0];
4262 char *tmp;
4263 size_t max_size;
4264 unsigned long inline_size;
4265 unsigned long ptr;
4266
4267 WARN_ON(pg_offset != 0);
4268 max_size = btrfs_file_extent_ram_bytes(leaf, item);
4269 inline_size = btrfs_file_extent_inline_item_len(leaf,
4270 btrfs_item_nr(leaf, path->slots[0]));
4271 tmp = kmalloc(inline_size, GFP_NOFS);
4272 ptr = btrfs_file_extent_inline_start(item);
4273
4274 read_extent_buffer(leaf, tmp, ptr, inline_size);
4275
Chris Mason5b050f02008-11-11 09:34:41 -05004276 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04004277 ret = btrfs_zlib_decompress(tmp, page, extent_offset,
4278 inline_size, max_size);
4279 if (ret) {
4280 char *kaddr = kmap_atomic(page, KM_USER0);
4281 unsigned long copy_size = min_t(u64,
4282 PAGE_CACHE_SIZE - pg_offset,
4283 max_size - extent_offset);
4284 memset(kaddr + pg_offset, 0, copy_size);
4285 kunmap_atomic(kaddr, KM_USER0);
4286 }
4287 kfree(tmp);
4288 return 0;
4289}
4290
Chris Masond352ac62008-09-29 15:18:18 -04004291/*
4292 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05004293 * the ugly parts come from merging extents from the disk with the in-ram
4294 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04004295 * where the in-ram extents might be locked pending data=ordered completion.
4296 *
4297 * This also copies inline extents directly into the page.
4298 */
Chris Masond3977122009-01-05 21:25:51 -05004299
Chris Masona52d9a82007-08-27 16:49:44 -04004300struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05004301 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04004302 int create)
4303{
4304 int ret;
4305 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04004306 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04004307 u64 extent_start = 0;
4308 u64 extent_end = 0;
4309 u64 objectid = inode->i_ino;
4310 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04004311 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04004312 struct btrfs_root *root = BTRFS_I(inode)->root;
4313 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04004314 struct extent_buffer *leaf;
4315 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04004316 struct extent_map *em = NULL;
4317 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05004318 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04004319 struct btrfs_trans_handle *trans = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04004320 int compressed;
Chris Masona52d9a82007-08-27 16:49:44 -04004321
Chris Masona52d9a82007-08-27 16:49:44 -04004322again:
Chris Mason890871b2009-09-02 16:24:52 -04004323 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05004324 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04004325 if (em)
4326 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04004327 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05004328
Chris Masona52d9a82007-08-27 16:49:44 -04004329 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04004330 if (em->start > start || em->start + em->len <= start)
4331 free_extent_map(em);
4332 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05004333 free_extent_map(em);
4334 else
4335 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004336 }
Chris Masond1310b22008-01-24 16:13:08 -05004337 em = alloc_extent_map(GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04004338 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05004339 err = -ENOMEM;
4340 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004341 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004342 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05004343 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05004344 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05004345 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04004346 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04004347
4348 if (!path) {
4349 path = btrfs_alloc_path();
4350 BUG_ON(!path);
4351 }
4352
Chris Mason179e29e2007-11-01 11:28:41 -04004353 ret = btrfs_lookup_file_extent(trans, root, path,
4354 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04004355 if (ret < 0) {
4356 err = ret;
4357 goto out;
4358 }
4359
4360 if (ret != 0) {
4361 if (path->slots[0] == 0)
4362 goto not_found;
4363 path->slots[0]--;
4364 }
4365
Chris Mason5f39d392007-10-15 16:14:19 -04004366 leaf = path->nodes[0];
4367 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04004368 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04004369 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04004370 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4371 found_type = btrfs_key_type(&found_key);
4372 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04004373 found_type != BTRFS_EXTENT_DATA_KEY) {
4374 goto not_found;
4375 }
4376
Chris Mason5f39d392007-10-15 16:14:19 -04004377 found_type = btrfs_file_extent_type(leaf, item);
4378 extent_start = found_key.offset;
Chris Masonc8b97812008-10-29 14:49:59 -04004379 compressed = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04004380 if (found_type == BTRFS_FILE_EXTENT_REG ||
4381 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04004382 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04004383 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04004384 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
4385 size_t size;
4386 size = btrfs_file_extent_inline_len(leaf, item);
4387 extent_end = (extent_start + size + root->sectorsize - 1) &
4388 ~((u64)root->sectorsize - 1);
4389 }
4390
4391 if (start >= extent_end) {
4392 path->slots[0]++;
4393 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
4394 ret = btrfs_next_leaf(root, path);
4395 if (ret < 0) {
4396 err = ret;
4397 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004398 }
Yan Zheng9036c102008-10-30 14:19:41 -04004399 if (ret > 0)
4400 goto not_found;
4401 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04004402 }
Yan Zheng9036c102008-10-30 14:19:41 -04004403 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4404 if (found_key.objectid != objectid ||
4405 found_key.type != BTRFS_EXTENT_DATA_KEY)
4406 goto not_found;
4407 if (start + len <= found_key.offset)
4408 goto not_found;
4409 em->start = start;
4410 em->len = found_key.offset - start;
4411 goto not_found_em;
4412 }
4413
Yan Zhengd899e052008-10-30 14:25:28 -04004414 if (found_type == BTRFS_FILE_EXTENT_REG ||
4415 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04004416 em->start = extent_start;
4417 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05004418 em->orig_start = extent_start -
4419 btrfs_file_extent_offset(leaf, item);
Chris Masondb945352007-10-15 16:15:53 -04004420 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
4421 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004422 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04004423 goto insert;
4424 }
Chris Masonc8b97812008-10-29 14:49:59 -04004425 if (compressed) {
4426 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
4427 em->block_start = bytenr;
4428 em->block_len = btrfs_file_extent_disk_num_bytes(leaf,
4429 item);
4430 } else {
4431 bytenr += btrfs_file_extent_offset(leaf, item);
4432 em->block_start = bytenr;
4433 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04004434 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
4435 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04004436 }
Chris Masona52d9a82007-08-27 16:49:44 -04004437 goto insert;
4438 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004439 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04004440 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04004441 size_t size;
4442 size_t extent_offset;
4443 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04004444
Chris Masona52d9a82007-08-27 16:49:44 -04004445 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04004446 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04004447 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04004448 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04004449 goto out;
4450 }
4451
Yan Zheng9036c102008-10-30 14:19:41 -04004452 size = btrfs_file_extent_inline_len(leaf, item);
4453 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05004454 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04004455 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04004456 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05004457 em->len = (copy_size + root->sectorsize - 1) &
4458 ~((u64)root->sectorsize - 1);
Yan Zhengff5b7ee2008-11-10 07:34:43 -05004459 em->orig_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04004460 if (compressed)
4461 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Yan689f9342007-10-29 11:41:07 -04004462 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04004463 if (create == 0 && !PageUptodate(page)) {
Chris Masonc8b97812008-10-29 14:49:59 -04004464 if (btrfs_file_extent_compression(leaf, item) ==
4465 BTRFS_COMPRESS_ZLIB) {
4466 ret = uncompress_inline(path, inode, page,
4467 pg_offset,
4468 extent_offset, item);
4469 BUG_ON(ret);
4470 } else {
4471 map = kmap(page);
4472 read_extent_buffer(leaf, map + pg_offset, ptr,
4473 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04004474 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
4475 memset(map + pg_offset + copy_size, 0,
4476 PAGE_CACHE_SIZE - pg_offset -
4477 copy_size);
4478 }
Chris Masonc8b97812008-10-29 14:49:59 -04004479 kunmap(page);
4480 }
Chris Mason179e29e2007-11-01 11:28:41 -04004481 flush_dcache_page(page);
4482 } else if (create && PageUptodate(page)) {
4483 if (!trans) {
4484 kunmap(page);
4485 free_extent_map(em);
4486 em = NULL;
4487 btrfs_release_path(root, path);
Chris Masonf9295742008-07-17 12:54:14 -04004488 trans = btrfs_join_transaction(root, 1);
Chris Mason179e29e2007-11-01 11:28:41 -04004489 goto again;
4490 }
Chris Masonc8b97812008-10-29 14:49:59 -04004491 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05004492 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04004493 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04004494 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04004495 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04004496 }
Chris Masond1310b22008-01-24 16:13:08 -05004497 set_extent_uptodate(io_tree, em->start,
4498 extent_map_end(em) - 1, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04004499 goto insert;
4500 } else {
Chris Masond3977122009-01-05 21:25:51 -05004501 printk(KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04004502 WARN_ON(1);
4503 }
4504not_found:
4505 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05004506 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04004507not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04004508 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04004509 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04004510insert:
4511 btrfs_release_path(root, path);
Chris Masond1310b22008-01-24 16:13:08 -05004512 if (em->start > start || extent_map_end(em) <= start) {
Chris Masond3977122009-01-05 21:25:51 -05004513 printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
4514 "[%llu %llu]\n", (unsigned long long)em->start,
4515 (unsigned long long)em->len,
4516 (unsigned long long)start,
4517 (unsigned long long)len);
Chris Masona52d9a82007-08-27 16:49:44 -04004518 err = -EIO;
4519 goto out;
4520 }
Chris Masond1310b22008-01-24 16:13:08 -05004521
4522 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04004523 write_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04004524 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04004525 /* it is possible that someone inserted the extent into the tree
4526 * while we had the lock dropped. It is also possible that
4527 * an overlapping map exists in the tree
4528 */
Chris Masona52d9a82007-08-27 16:49:44 -04004529 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04004530 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004531
4532 ret = 0;
4533
Chris Mason3b951512008-04-17 11:29:12 -04004534 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04004535 if (existing && (existing->start > start ||
4536 existing->start + existing->len <= start)) {
4537 free_extent_map(existing);
4538 existing = NULL;
4539 }
Chris Mason3b951512008-04-17 11:29:12 -04004540 if (!existing) {
4541 existing = lookup_extent_mapping(em_tree, em->start,
4542 em->len);
4543 if (existing) {
4544 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04004545 em, start,
4546 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04004547 free_extent_map(existing);
4548 if (err) {
4549 free_extent_map(em);
4550 em = NULL;
4551 }
4552 } else {
4553 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04004554 free_extent_map(em);
4555 em = NULL;
4556 }
4557 } else {
4558 free_extent_map(em);
4559 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004560 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04004561 }
Chris Masona52d9a82007-08-27 16:49:44 -04004562 }
Chris Mason890871b2009-09-02 16:24:52 -04004563 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04004564out:
Chris Masonf4219502008-07-22 11:18:09 -04004565 if (path)
4566 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04004567 if (trans) {
4568 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05004569 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04004570 err = ret;
4571 }
Chris Masona52d9a82007-08-27 16:49:44 -04004572 if (err) {
4573 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04004574 return ERR_PTR(err);
4575 }
4576 return em;
4577}
4578
Chris Mason16432982008-04-10 10:23:21 -04004579static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
4580 const struct iovec *iov, loff_t offset,
4581 unsigned long nr_segs)
4582{
Chris Masone1c4b742008-04-22 13:26:46 -04004583 return -EINVAL;
Chris Mason16432982008-04-10 10:23:21 -04004584}
4585
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004586static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
4587 __u64 start, __u64 len)
4588{
4589 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent);
4590}
4591
Chris Mason9ebefb182007-06-15 13:50:00 -04004592int btrfs_readpage(struct file *file, struct page *page)
4593{
Chris Masond1310b22008-01-24 16:13:08 -05004594 struct extent_io_tree *tree;
4595 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04004596 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04004597}
Chris Mason1832a6d2007-12-21 16:27:21 -05004598
Chris Mason39279cc2007-06-12 06:35:45 -04004599static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
4600{
Chris Masond1310b22008-01-24 16:13:08 -05004601 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04004602
4603
4604 if (current->flags & PF_MEMALLOC) {
4605 redirty_page_for_writepage(wbc, page);
4606 unlock_page(page);
4607 return 0;
4608 }
Chris Masond1310b22008-01-24 16:13:08 -05004609 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04004610 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
4611}
Chris Mason39279cc2007-06-12 06:35:45 -04004612
Chris Masonf4219502008-07-22 11:18:09 -04004613int btrfs_writepages(struct address_space *mapping,
4614 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04004615{
Chris Masond1310b22008-01-24 16:13:08 -05004616 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05004617
Chris Masond1310b22008-01-24 16:13:08 -05004618 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04004619 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
4620}
4621
Chris Mason3ab2fb52007-11-08 10:59:22 -05004622static int
4623btrfs_readpages(struct file *file, struct address_space *mapping,
4624 struct list_head *pages, unsigned nr_pages)
4625{
Chris Masond1310b22008-01-24 16:13:08 -05004626 struct extent_io_tree *tree;
4627 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05004628 return extent_readpages(tree, mapping, pages, nr_pages,
4629 btrfs_get_extent);
4630}
Chris Masone6dcd2d2008-07-17 12:53:50 -04004631static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04004632{
Chris Masond1310b22008-01-24 16:13:08 -05004633 struct extent_io_tree *tree;
4634 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04004635 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004636
Chris Masond1310b22008-01-24 16:13:08 -05004637 tree = &BTRFS_I(page->mapping->host)->io_tree;
4638 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05004639 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04004640 if (ret == 1) {
4641 ClearPagePrivate(page);
4642 set_page_private(page, 0);
4643 page_cache_release(page);
4644 }
4645 return ret;
4646}
Chris Mason39279cc2007-06-12 06:35:45 -04004647
Chris Masone6dcd2d2008-07-17 12:53:50 -04004648static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
4649{
Chris Mason98509cf2008-09-11 15:51:43 -04004650 if (PageWriteback(page) || PageDirty(page))
4651 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05004652 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004653}
4654
Chris Masona52d9a82007-08-27 16:49:44 -04004655static void btrfs_invalidatepage(struct page *page, unsigned long offset)
4656{
Chris Masond1310b22008-01-24 16:13:08 -05004657 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004658 struct btrfs_ordered_extent *ordered;
4659 u64 page_start = page_offset(page);
4660 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04004661
Chris Mason8b62b722009-09-02 16:53:46 -04004662
4663 /*
4664 * we have the page locked, so new writeback can't start,
4665 * and the dirty bit won't be cleared while we are here.
4666 *
4667 * Wait for IO on this page so that we can safely clear
4668 * the PagePrivate2 bit and do ordered accounting
4669 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04004670 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04004671
Chris Masond1310b22008-01-24 16:13:08 -05004672 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004673 if (offset) {
4674 btrfs_releasepage(page, GFP_NOFS);
4675 return;
4676 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004677 lock_extent(tree, page_start, page_end, GFP_NOFS);
4678 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
4679 page_offset(page));
4680 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04004681 /*
4682 * IO on this page will never be started, so we need
4683 * to account for any ordered extents now
4684 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04004685 clear_extent_bit(tree, page_start, page_end,
4686 EXTENT_DIRTY | EXTENT_DELALLOC |
Chris Mason2c64c532009-09-02 15:04:12 -04004687 EXTENT_LOCKED, 1, 0, NULL, GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04004688 /*
4689 * whoever cleared the private bit is responsible
4690 * for the finish_ordered_io
4691 */
4692 if (TestClearPagePrivate2(page)) {
4693 btrfs_finish_ordered_io(page->mapping->host,
4694 page_start, page_end);
4695 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004696 btrfs_put_ordered_extent(ordered);
4697 lock_extent(tree, page_start, page_end, GFP_NOFS);
4698 }
4699 clear_extent_bit(tree, page_start, page_end,
Chris Mason8b62b722009-09-02 16:53:46 -04004700 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC,
Chris Mason2c64c532009-09-02 15:04:12 -04004701 1, 1, NULL, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004702 __btrfs_releasepage(page, GFP_NOFS);
4703
Chris Mason4a096752008-07-21 10:29:44 -04004704 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04004705 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04004706 ClearPagePrivate(page);
4707 set_page_private(page, 0);
4708 page_cache_release(page);
4709 }
Chris Mason39279cc2007-06-12 06:35:45 -04004710}
4711
Chris Mason9ebefb182007-06-15 13:50:00 -04004712/*
4713 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
4714 * called from a page fault handler when a page is first dirtied. Hence we must
4715 * be careful to check for EOF conditions here. We set the page up correctly
4716 * for a written page which means we get ENOSPC checking when writing into
4717 * holes and correct delalloc and unwritten extent mapping on filesystems that
4718 * support these features.
4719 *
4720 * We are not allowed to take the i_mutex here so we have to play games to
4721 * protect against truncate races as the page could now be beyond EOF. Because
4722 * vmtruncate() writes the inode size before removing pages, once we have the
4723 * page lock we can determine safely if the page is beyond EOF. If it is not
4724 * beyond EOF, then the page is guaranteed safe against truncation until we
4725 * unlock the page.
4726 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07004727int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04004728{
Nick Pigginc2ec1752009-03-31 15:23:21 -07004729 struct page *page = vmf->page;
Chris Mason6da6aba2007-12-18 16:15:09 -05004730 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05004731 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004732 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4733 struct btrfs_ordered_extent *ordered;
4734 char *kaddr;
4735 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04004736 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05004737 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04004738 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004739 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04004740
Josef Bacik6a632092009-02-20 11:00:09 -05004741 ret = btrfs_check_data_free_space(root, inode, PAGE_CACHE_SIZE);
Nick Piggin56a76f82009-03-31 15:23:23 -07004742 if (ret) {
4743 if (ret == -ENOMEM)
4744 ret = VM_FAULT_OOM;
4745 else /* -ENOSPC, -EIO, etc */
4746 ret = VM_FAULT_SIGBUS;
Chris Mason1832a6d2007-12-21 16:27:21 -05004747 goto out;
Nick Piggin56a76f82009-03-31 15:23:23 -07004748 }
Chris Mason1832a6d2007-12-21 16:27:21 -05004749
Nick Piggin56a76f82009-03-31 15:23:23 -07004750 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04004751again:
Chris Mason9ebefb182007-06-15 13:50:00 -04004752 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04004753 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004754 page_start = page_offset(page);
4755 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04004756
Chris Mason9ebefb182007-06-15 13:50:00 -04004757 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04004758 (page_start >= size)) {
Josef Bacik6a632092009-02-20 11:00:09 -05004759 btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
Chris Mason9ebefb182007-06-15 13:50:00 -04004760 /* page got truncated out from underneath us */
4761 goto out_unlock;
4762 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004763 wait_on_page_writeback(page);
4764
4765 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
4766 set_page_extent_mapped(page);
4767
Chris Masoneb84ae02008-07-17 13:53:27 -04004768 /*
4769 * we can't set the delalloc bits if there are pending ordered
4770 * extents. Drop our locks and wait for them to finish
4771 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04004772 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4773 if (ordered) {
4774 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
4775 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004776 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004777 btrfs_put_ordered_extent(ordered);
4778 goto again;
4779 }
4780
Chris Masonea8c2812008-08-04 23:17:27 -04004781 btrfs_set_extent_delalloc(inode, page_start, page_end);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004782 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04004783
4784 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04004785 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04004786 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04004787 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04004788 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04004789
Chris Masone6dcd2d2008-07-17 12:53:50 -04004790 if (zero_start != PAGE_CACHE_SIZE) {
4791 kaddr = kmap(page);
4792 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
4793 flush_dcache_page(page);
4794 kunmap(page);
4795 }
Chris Mason247e7432008-07-17 12:53:51 -04004796 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004797 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04004798 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04004799
4800 BTRFS_I(inode)->last_trans = root->fs_info->generation + 1;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004801 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04004802
4803out_unlock:
Chris Mason50a9b212009-09-11 12:33:12 -04004804 if (!ret)
4805 return VM_FAULT_LOCKED;
Chris Mason9ebefb182007-06-15 13:50:00 -04004806 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05004807out:
Chris Mason9ebefb182007-06-15 13:50:00 -04004808 return ret;
4809}
4810
Chris Mason39279cc2007-06-12 06:35:45 -04004811static void btrfs_truncate(struct inode *inode)
4812{
4813 struct btrfs_root *root = BTRFS_I(inode)->root;
4814 int ret;
4815 struct btrfs_trans_handle *trans;
Chris Masond3c2fdc2007-09-17 10:58:06 -04004816 unsigned long nr;
Chris Masondbe674a2008-07-17 12:54:05 -04004817 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04004818
4819 if (!S_ISREG(inode->i_mode))
4820 return;
4821 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
4822 return;
4823
4824 btrfs_truncate_page(inode->i_mapping, inode->i_size);
Chris Mason4a096752008-07-21 10:29:44 -04004825 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04004826
Chris Mason39279cc2007-06-12 06:35:45 -04004827 trans = btrfs_start_transaction(root, 1);
Chris Mason5a3f23d2009-03-31 13:27:11 -04004828
4829 /*
4830 * setattr is responsible for setting the ordered_data_close flag,
4831 * but that is only tested during the last file release. That
4832 * could happen well after the next commit, leaving a great big
4833 * window where new writes may get lost if someone chooses to write
4834 * to this file after truncating to zero
4835 *
4836 * The inode doesn't have any dirty data here, and so if we commit
4837 * this is a noop. If someone immediately starts writing to the inode
4838 * it is very likely we'll catch some of their writes in this
4839 * transaction, and the commit will find this file on the ordered
4840 * data list with good things to send down.
4841 *
4842 * This is a best effort solution, there is still a window where
4843 * using truncate to replace the contents of the file will
4844 * end up with a zero length file after a crash.
4845 */
4846 if (inode->i_size == 0 && BTRFS_I(inode)->ordered_data_close)
4847 btrfs_add_ordered_operation(trans, root, inode);
4848
Chris Mason39279cc2007-06-12 06:35:45 -04004849 btrfs_set_trans_block_group(trans, inode);
Chris Masondbe674a2008-07-17 12:54:05 -04004850 btrfs_i_size_write(inode, inode->i_size);
Chris Mason39279cc2007-06-12 06:35:45 -04004851
Josef Bacik7b128762008-07-24 12:17:14 -04004852 ret = btrfs_orphan_add(trans, inode);
4853 if (ret)
4854 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04004855 /* FIXME, add redo link to tree so we don't leak on crash */
Chris Masone02119d2008-09-05 16:13:11 -04004856 ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size,
Chris Mason85e21ba2008-01-29 15:11:36 -05004857 BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004858 btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004859
Josef Bacik7b128762008-07-24 12:17:14 -04004860 ret = btrfs_orphan_del(trans, inode);
4861 BUG_ON(ret);
4862
4863out:
4864 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04004865 ret = btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004866 BUG_ON(ret);
Chris Masond3c2fdc2007-09-17 10:58:06 -04004867 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004868}
4869
Sven Wegener3b963622008-06-09 21:57:42 -04004870/*
Chris Masond352ac62008-09-29 15:18:18 -04004871 * create a new subvolume directory/inode (helper for the ioctl).
4872 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05004873int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Yan, Zheng76dda932009-09-21 16:00:26 -04004874 struct btrfs_root *new_root,
Yan Zhengd2fb3432008-12-11 16:30:39 -05004875 u64 new_dirid, u64 alloc_hint)
Chris Mason39279cc2007-06-12 06:35:45 -04004876{
Chris Mason39279cc2007-06-12 06:35:45 -04004877 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04004878 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004879 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004880
Josef Bacikaec74772008-07-24 12:12:38 -04004881 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
Yan Zhengd2fb3432008-12-11 16:30:39 -05004882 new_dirid, alloc_hint, S_IFDIR | 0700, &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04004883 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004884 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004885 inode->i_op = &btrfs_dir_inode_operations;
4886 inode->i_fop = &btrfs_dir_file_operations;
4887
Chris Mason39279cc2007-06-12 06:35:45 -04004888 inode->i_nlink = 1;
Chris Masondbe674a2008-07-17 12:54:05 -04004889 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04004890
Yan, Zheng76dda932009-09-21 16:00:26 -04004891 err = btrfs_update_inode(trans, new_root, inode);
4892 BUG_ON(err);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04004893
Yan, Zheng76dda932009-09-21 16:00:26 -04004894 iput(inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04004895 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004896}
4897
Chris Masond352ac62008-09-29 15:18:18 -04004898/* helper function for file defrag and space balancing. This
4899 * forces readahead on a given range of bytes in an inode
4900 */
Chris Masonedbd8d42007-12-21 16:27:24 -05004901unsigned long btrfs_force_ra(struct address_space *mapping,
Chris Mason86479a02007-09-10 19:58:16 -04004902 struct file_ra_state *ra, struct file *file,
4903 pgoff_t offset, pgoff_t last_index)
4904{
Chris Mason8e7bf942008-04-28 09:02:36 -04004905 pgoff_t req_size = last_index - offset + 1;
Chris Mason86479a02007-09-10 19:58:16 -04004906
Chris Mason86479a02007-09-10 19:58:16 -04004907 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
4908 return offset + req_size;
Chris Mason86479a02007-09-10 19:58:16 -04004909}
4910
Chris Mason39279cc2007-06-12 06:35:45 -04004911struct inode *btrfs_alloc_inode(struct super_block *sb)
4912{
4913 struct btrfs_inode *ei;
4914
4915 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
4916 if (!ei)
4917 return NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04004918 ei->last_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004919 ei->logged_trans = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004920 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Josef Bacik7b128762008-07-24 12:17:14 -04004921 INIT_LIST_HEAD(&ei->i_orphan);
Chris Mason5a3f23d2009-03-31 13:27:11 -04004922 INIT_LIST_HEAD(&ei->ordered_operations);
Chris Mason39279cc2007-06-12 06:35:45 -04004923 return &ei->vfs_inode;
4924}
4925
4926void btrfs_destroy_inode(struct inode *inode)
4927{
Chris Masone6dcd2d2008-07-17 12:53:50 -04004928 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04004929 struct btrfs_root *root = BTRFS_I(inode)->root;
4930
Chris Mason39279cc2007-06-12 06:35:45 -04004931 WARN_ON(!list_empty(&inode->i_dentry));
4932 WARN_ON(inode->i_data.nrpages);
4933
Chris Mason5a3f23d2009-03-31 13:27:11 -04004934 /*
4935 * Make sure we're properly removed from the ordered operation
4936 * lists.
4937 */
4938 smp_mb();
4939 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
4940 spin_lock(&root->fs_info->ordered_extent_lock);
4941 list_del_init(&BTRFS_I(inode)->ordered_operations);
4942 spin_unlock(&root->fs_info->ordered_extent_lock);
4943 }
4944
4945 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04004946 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
4947 printk(KERN_ERR "BTRFS: inode %lu: inode still on the orphan"
4948 " list\n", inode->i_ino);
4949 dump_stack();
4950 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04004951 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04004952
Chris Masond3977122009-01-05 21:25:51 -05004953 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04004954 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
4955 if (!ordered)
4956 break;
4957 else {
Chris Masond3977122009-01-05 21:25:51 -05004958 printk(KERN_ERR "btrfs found ordered "
4959 "extent %llu %llu on inode cleanup\n",
4960 (unsigned long long)ordered->file_offset,
4961 (unsigned long long)ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004962 btrfs_remove_ordered_extent(inode, ordered);
4963 btrfs_put_ordered_extent(ordered);
4964 btrfs_put_ordered_extent(ordered);
4965 }
4966 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004967 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04004968 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004969 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
4970}
4971
Yan, Zheng76dda932009-09-21 16:00:26 -04004972void btrfs_drop_inode(struct inode *inode)
4973{
4974 struct btrfs_root *root = BTRFS_I(inode)->root;
4975
4976 if (inode->i_nlink > 0 && btrfs_root_refs(&root->root_item) == 0)
4977 generic_delete_inode(inode);
4978 else
4979 generic_drop_inode(inode);
4980}
4981
Sven Wegener0ee0fda2008-07-30 16:54:26 -04004982static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04004983{
4984 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
4985
4986 inode_init_once(&ei->vfs_inode);
4987}
4988
4989void btrfs_destroy_cachep(void)
4990{
4991 if (btrfs_inode_cachep)
4992 kmem_cache_destroy(btrfs_inode_cachep);
4993 if (btrfs_trans_handle_cachep)
4994 kmem_cache_destroy(btrfs_trans_handle_cachep);
4995 if (btrfs_transaction_cachep)
4996 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04004997 if (btrfs_path_cachep)
4998 kmem_cache_destroy(btrfs_path_cachep);
4999}
5000
5001int btrfs_init_cachep(void)
5002{
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02005003 btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
5004 sizeof(struct btrfs_inode), 0,
5005 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04005006 if (!btrfs_inode_cachep)
5007 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02005008
5009 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
5010 sizeof(struct btrfs_trans_handle), 0,
5011 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005012 if (!btrfs_trans_handle_cachep)
5013 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02005014
5015 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
5016 sizeof(struct btrfs_transaction), 0,
5017 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005018 if (!btrfs_transaction_cachep)
5019 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02005020
5021 btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
5022 sizeof(struct btrfs_path), 0,
5023 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005024 if (!btrfs_path_cachep)
5025 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02005026
Chris Mason39279cc2007-06-12 06:35:45 -04005027 return 0;
5028fail:
5029 btrfs_destroy_cachep();
5030 return -ENOMEM;
5031}
5032
5033static int btrfs_getattr(struct vfsmount *mnt,
5034 struct dentry *dentry, struct kstat *stat)
5035{
5036 struct inode *inode = dentry->d_inode;
5037 generic_fillattr(inode, stat);
Chris Mason3394e162008-11-17 20:42:26 -05005038 stat->dev = BTRFS_I(inode)->root->anon_super.s_dev;
Chris Masond6667462008-01-03 14:51:00 -05005039 stat->blksize = PAGE_CACHE_SIZE;
Yan Zhenga76a3cd2008-10-09 11:46:29 -04005040 stat->blocks = (inode_get_bytes(inode) +
5041 BTRFS_I(inode)->delalloc_bytes) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04005042 return 0;
5043}
5044
Chris Masond3977122009-01-05 21:25:51 -05005045static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
5046 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005047{
5048 struct btrfs_trans_handle *trans;
5049 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005050 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005051 struct inode *new_inode = new_dentry->d_inode;
5052 struct inode *old_inode = old_dentry->d_inode;
5053 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005054 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005055 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04005056 int ret;
5057
Yan, Zhengf679a842009-09-24 09:17:31 -04005058 if (new_dir->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5059 return -EPERM;
5060
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005061 /* we only allow rename subvolume link between subvolumes */
5062 if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05005063 return -EXDEV;
5064
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005065 if (old_inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
5066 (new_inode && new_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID))
5067 return -ENOTEMPTY;
5068
Chris Mason39279cc2007-06-12 06:35:45 -04005069 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005070 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04005071 return -ENOTEMPTY;
Chris Mason0660b5a2008-11-17 20:37:39 -05005072
Josef Bacik6a632092009-02-20 11:00:09 -05005073 ret = btrfs_check_metadata_free_space(root);
Chris Mason1832a6d2007-12-21 16:27:21 -05005074 if (ret)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005075 return ret;
Chris Mason1832a6d2007-12-21 16:27:21 -05005076
Chris Mason5a3f23d2009-03-31 13:27:11 -04005077 /*
5078 * we're using rename to replace one file with another.
5079 * and the replacement file is large. Start IO on it now so
5080 * we don't add too much work to the end of the transaction
5081 */
Bartlomiej Zolnierkiewicz4baf8c92009-08-07 13:47:08 -04005082 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
Chris Mason5a3f23d2009-03-31 13:27:11 -04005083 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
5084 filemap_flush(old_inode->i_mapping);
5085
Yan, Zheng76dda932009-09-21 16:00:26 -04005086 /* close the racy window with snapshot create/destroy ioctl */
5087 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
5088 down_read(&root->fs_info->subvol_sem);
Chris Mason5f39d392007-10-15 16:14:19 -04005089
Chris Mason39279cc2007-06-12 06:35:45 -04005090 trans = btrfs_start_transaction(root, 1);
Yan, Zhenga5719522009-09-24 09:17:31 -04005091 btrfs_set_trans_block_group(trans, new_dir);
Chris Mason39279cc2007-06-12 06:35:45 -04005092
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005093 if (dest != root)
5094 btrfs_record_root_in_trans(trans, dest);
5095
Yan, Zhenga5719522009-09-24 09:17:31 -04005096 ret = btrfs_set_inode_index(new_dir, &index);
5097 if (ret)
5098 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04005099
Yan, Zhenga5719522009-09-24 09:17:31 -04005100 if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005101 /* force full log commit if subvolume involved. */
5102 root->fs_info->last_trans_log_full_commit = trans->transid;
5103 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04005104 ret = btrfs_insert_inode_ref(trans, dest,
5105 new_dentry->d_name.name,
5106 new_dentry->d_name.len,
5107 old_inode->i_ino,
5108 new_dir->i_ino, index);
5109 if (ret)
5110 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005111 /*
5112 * this is an ugly little race, but the rename is required
5113 * to make sure that if we crash, the inode is either at the
5114 * old name or the new one. pinning the log transaction lets
5115 * us make sure we don't allow a log commit to come in after
5116 * we unlink the name but before we add the new name back in.
5117 */
5118 btrfs_pin_log_trans(root);
5119 }
Chris Mason12fcfd22009-03-24 10:24:20 -04005120 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04005121 * make sure the inode gets flushed if it is replacing
5122 * something.
5123 */
5124 if (new_inode && new_inode->i_size &&
5125 old_inode && S_ISREG(old_inode->i_mode)) {
5126 btrfs_add_ordered_operation(trans, root, old_inode);
5127 }
5128
Chris Mason39279cc2007-06-12 06:35:45 -04005129 old_dir->i_ctime = old_dir->i_mtime = ctime;
5130 new_dir->i_ctime = new_dir->i_mtime = ctime;
5131 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04005132
Chris Mason12fcfd22009-03-24 10:24:20 -04005133 if (old_dentry->d_parent != new_dentry->d_parent)
5134 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
5135
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005136 if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
5137 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
5138 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
5139 old_dentry->d_name.name,
5140 old_dentry->d_name.len);
5141 } else {
5142 btrfs_inc_nlink(old_dentry->d_inode);
5143 ret = btrfs_unlink_inode(trans, root, old_dir,
5144 old_dentry->d_inode,
5145 old_dentry->d_name.name,
5146 old_dentry->d_name.len);
5147 }
5148 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005149
5150 if (new_inode) {
5151 new_inode->i_ctime = CURRENT_TIME;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005152 if (unlikely(new_inode->i_ino ==
5153 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
5154 root_objectid = BTRFS_I(new_inode)->location.objectid;
5155 ret = btrfs_unlink_subvol(trans, dest, new_dir,
5156 root_objectid,
5157 new_dentry->d_name.name,
5158 new_dentry->d_name.len);
5159 BUG_ON(new_inode->i_nlink == 0);
5160 } else {
5161 ret = btrfs_unlink_inode(trans, dest, new_dir,
5162 new_dentry->d_inode,
5163 new_dentry->d_name.name,
5164 new_dentry->d_name.len);
5165 }
5166 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04005167 if (new_inode->i_nlink == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04005168 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005169 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04005170 }
Chris Mason39279cc2007-06-12 06:35:45 -04005171 }
Josef Bacikaec74772008-07-24 12:12:38 -04005172
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005173 ret = btrfs_add_link(trans, new_dir, old_inode,
5174 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04005175 new_dentry->d_name.len, 0, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005176 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005177
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005178 if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) {
5179 btrfs_log_new_name(trans, old_inode, old_dir,
5180 new_dentry->d_parent);
5181 btrfs_end_log_trans(root);
5182 }
Chris Mason39279cc2007-06-12 06:35:45 -04005183out_fail:
Chris Masonab78c842008-07-29 16:15:18 -04005184 btrfs_end_transaction_throttle(trans, root);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005185
Yan, Zheng76dda932009-09-21 16:00:26 -04005186 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
5187 up_read(&root->fs_info->subvol_sem);
Chris Mason39279cc2007-06-12 06:35:45 -04005188 return ret;
5189}
5190
Chris Masond352ac62008-09-29 15:18:18 -04005191/*
5192 * some fairly slow code that needs optimization. This walks the list
5193 * of all the inodes with pending delalloc and forces them to disk.
5194 */
Chris Masonea8c2812008-08-04 23:17:27 -04005195int btrfs_start_delalloc_inodes(struct btrfs_root *root)
5196{
5197 struct list_head *head = &root->fs_info->delalloc_inodes;
5198 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04005199 struct inode *inode;
Chris Masonea8c2812008-08-04 23:17:27 -04005200
Yan Zhengc146afa2008-11-12 14:34:12 -05005201 if (root->fs_info->sb->s_flags & MS_RDONLY)
5202 return -EROFS;
5203
Chris Mason75eff682008-12-15 15:54:40 -05005204 spin_lock(&root->fs_info->delalloc_lock);
Chris Masond3977122009-01-05 21:25:51 -05005205 while (!list_empty(head)) {
Chris Masonea8c2812008-08-04 23:17:27 -04005206 binode = list_entry(head->next, struct btrfs_inode,
5207 delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04005208 inode = igrab(&binode->vfs_inode);
5209 if (!inode)
5210 list_del_init(&binode->delalloc_inodes);
Chris Mason75eff682008-12-15 15:54:40 -05005211 spin_unlock(&root->fs_info->delalloc_lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04005212 if (inode) {
Chris Mason8c8bee12008-09-29 11:19:10 -04005213 filemap_flush(inode->i_mapping);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04005214 iput(inode);
5215 }
5216 cond_resched();
Chris Mason75eff682008-12-15 15:54:40 -05005217 spin_lock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04005218 }
Chris Mason75eff682008-12-15 15:54:40 -05005219 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04005220
5221 /* the filemap_flush will queue IO into the worker threads, but
5222 * we have to make sure the IO is actually started and that
5223 * ordered extents get created before we return
5224 */
5225 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05005226 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05005227 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04005228 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05005229 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
5230 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04005231 }
5232 atomic_dec(&root->fs_info->async_submit_draining);
Chris Masonea8c2812008-08-04 23:17:27 -04005233 return 0;
5234}
5235
Chris Mason39279cc2007-06-12 06:35:45 -04005236static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
5237 const char *symname)
5238{
5239 struct btrfs_trans_handle *trans;
5240 struct btrfs_root *root = BTRFS_I(dir)->root;
5241 struct btrfs_path *path;
5242 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05005243 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04005244 int err;
5245 int drop_inode = 0;
5246 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005247 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04005248 int name_len;
5249 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04005250 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04005251 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04005252 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05005253 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005254
5255 name_len = strlen(symname) + 1;
5256 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
5257 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05005258
Josef Bacik6a632092009-02-20 11:00:09 -05005259 err = btrfs_check_metadata_free_space(root);
Chris Mason1832a6d2007-12-21 16:27:21 -05005260 if (err)
5261 goto out_fail;
5262
Chris Mason39279cc2007-06-12 06:35:45 -04005263 trans = btrfs_start_transaction(root, 1);
5264 btrfs_set_trans_block_group(trans, dir);
5265
5266 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
5267 if (err) {
5268 err = -ENOSPC;
5269 goto out_unlock;
5270 }
5271
Josef Bacikaec74772008-07-24 12:12:38 -04005272 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05005273 dentry->d_name.len,
5274 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04005275 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO,
5276 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04005277 err = PTR_ERR(inode);
5278 if (IS_ERR(inode))
5279 goto out_unlock;
5280
Jim Owens0279b4c2009-02-04 09:29:13 -05005281 err = btrfs_init_inode_security(inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04005282 if (err) {
5283 drop_inode = 1;
5284 goto out_unlock;
5285 }
5286
Chris Mason39279cc2007-06-12 06:35:45 -04005287 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04005288 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005289 if (err)
5290 drop_inode = 1;
5291 else {
5292 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04005293 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04005294 inode->i_fop = &btrfs_file_operations;
5295 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05005296 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04005297 }
Chris Mason39279cc2007-06-12 06:35:45 -04005298 btrfs_update_inode_block_group(trans, inode);
5299 btrfs_update_inode_block_group(trans, dir);
5300 if (drop_inode)
5301 goto out_unlock;
5302
5303 path = btrfs_alloc_path();
5304 BUG_ON(!path);
5305 key.objectid = inode->i_ino;
5306 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005307 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
5308 datasize = btrfs_file_extent_calc_inline_size(name_len);
5309 err = btrfs_insert_empty_item(trans, root, path, &key,
5310 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04005311 if (err) {
5312 drop_inode = 1;
5313 goto out_unlock;
5314 }
Chris Mason5f39d392007-10-15 16:14:19 -04005315 leaf = path->nodes[0];
5316 ei = btrfs_item_ptr(leaf, path->slots[0],
5317 struct btrfs_file_extent_item);
5318 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
5319 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04005320 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04005321 btrfs_set_file_extent_encryption(leaf, ei, 0);
5322 btrfs_set_file_extent_compression(leaf, ei, 0);
5323 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
5324 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
5325
Chris Mason39279cc2007-06-12 06:35:45 -04005326 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04005327 write_extent_buffer(leaf, symname, ptr, name_len);
5328 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04005329 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04005330
Chris Mason39279cc2007-06-12 06:35:45 -04005331 inode->i_op = &btrfs_symlink_inode_operations;
5332 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04005333 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04005334 inode_set_bytes(inode, name_len);
Chris Masondbe674a2008-07-17 12:54:05 -04005335 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04005336 err = btrfs_update_inode(trans, root, inode);
5337 if (err)
5338 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04005339
5340out_unlock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04005341 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04005342 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05005343out_fail:
Chris Mason39279cc2007-06-12 06:35:45 -04005344 if (drop_inode) {
5345 inode_dec_link_count(inode);
5346 iput(inode);
5347 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04005348 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04005349 return err;
5350}
Chris Mason16432982008-04-10 10:23:21 -04005351
Chris Mason546888d2009-04-21 11:53:38 -04005352static int prealloc_file_range(struct btrfs_trans_handle *trans,
5353 struct inode *inode, u64 start, u64 end,
Chris Masone980b502009-04-24 14:39:24 -04005354 u64 locked_end, u64 alloc_hint, int mode)
Yan Zhengd899e052008-10-30 14:25:28 -04005355{
Yan Zhengd899e052008-10-30 14:25:28 -04005356 struct btrfs_root *root = BTRFS_I(inode)->root;
5357 struct btrfs_key ins;
5358 u64 alloc_size;
5359 u64 cur_offset = start;
5360 u64 num_bytes = end - start;
5361 int ret = 0;
5362
Yan Zhengd899e052008-10-30 14:25:28 -04005363 while (num_bytes > 0) {
5364 alloc_size = min(num_bytes, root->fs_info->max_extent);
5365 ret = btrfs_reserve_extent(trans, root, alloc_size,
5366 root->sectorsize, 0, alloc_hint,
5367 (u64)-1, &ins, 1);
5368 if (ret) {
5369 WARN_ON(1);
5370 goto out;
5371 }
5372 ret = insert_reserved_file_extent(trans, inode,
5373 cur_offset, ins.objectid,
5374 ins.offset, ins.offset,
Chris Masone980b502009-04-24 14:39:24 -04005375 ins.offset, locked_end,
5376 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04005377 BTRFS_FILE_EXTENT_PREALLOC);
5378 BUG_ON(ret);
Chris Masona1ed8352009-09-11 12:27:37 -04005379 btrfs_drop_extent_cache(inode, cur_offset,
5380 cur_offset + ins.offset -1, 0);
Yan Zhengd899e052008-10-30 14:25:28 -04005381 num_bytes -= ins.offset;
5382 cur_offset += ins.offset;
5383 alloc_hint = ins.objectid + ins.offset;
5384 }
5385out:
5386 if (cur_offset > start) {
5387 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005388 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04005389 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
5390 cur_offset > i_size_read(inode))
5391 btrfs_i_size_write(inode, cur_offset);
5392 ret = btrfs_update_inode(trans, root, inode);
5393 BUG_ON(ret);
5394 }
5395
Yan Zhengd899e052008-10-30 14:25:28 -04005396 return ret;
5397}
5398
5399static long btrfs_fallocate(struct inode *inode, int mode,
5400 loff_t offset, loff_t len)
5401{
5402 u64 cur_offset;
5403 u64 last_byte;
5404 u64 alloc_start;
5405 u64 alloc_end;
5406 u64 alloc_hint = 0;
Chris Masone980b502009-04-24 14:39:24 -04005407 u64 locked_end;
Yan Zhengd899e052008-10-30 14:25:28 -04005408 u64 mask = BTRFS_I(inode)->root->sectorsize - 1;
5409 struct extent_map *em;
Chris Mason546888d2009-04-21 11:53:38 -04005410 struct btrfs_trans_handle *trans;
Josef Bacika970b0a2009-06-27 21:07:34 -04005411 struct btrfs_root *root;
Yan Zhengd899e052008-10-30 14:25:28 -04005412 int ret;
5413
5414 alloc_start = offset & ~mask;
5415 alloc_end = (offset + len + mask) & ~mask;
5416
Chris Mason546888d2009-04-21 11:53:38 -04005417 /*
5418 * wait for ordered IO before we have any locks. We'll loop again
5419 * below with the locks held.
5420 */
5421 btrfs_wait_ordered_range(inode, alloc_start, alloc_end - alloc_start);
5422
Yan Zhengd899e052008-10-30 14:25:28 -04005423 mutex_lock(&inode->i_mutex);
5424 if (alloc_start > inode->i_size) {
5425 ret = btrfs_cont_expand(inode, alloc_start);
5426 if (ret)
5427 goto out;
5428 }
5429
Josef Bacika970b0a2009-06-27 21:07:34 -04005430 root = BTRFS_I(inode)->root;
5431
5432 ret = btrfs_check_data_free_space(root, inode,
5433 alloc_end - alloc_start);
5434 if (ret)
5435 goto out;
5436
Chris Masone980b502009-04-24 14:39:24 -04005437 locked_end = alloc_end - 1;
Yan Zhengd899e052008-10-30 14:25:28 -04005438 while (1) {
5439 struct btrfs_ordered_extent *ordered;
Chris Mason546888d2009-04-21 11:53:38 -04005440
5441 trans = btrfs_start_transaction(BTRFS_I(inode)->root, 1);
5442 if (!trans) {
5443 ret = -EIO;
Josef Bacika970b0a2009-06-27 21:07:34 -04005444 goto out_free;
Chris Mason546888d2009-04-21 11:53:38 -04005445 }
5446
5447 /* the extent lock is ordered inside the running
5448 * transaction
5449 */
Chris Masone980b502009-04-24 14:39:24 -04005450 lock_extent(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
5451 GFP_NOFS);
Yan Zhengd899e052008-10-30 14:25:28 -04005452 ordered = btrfs_lookup_first_ordered_extent(inode,
5453 alloc_end - 1);
5454 if (ordered &&
5455 ordered->file_offset + ordered->len > alloc_start &&
5456 ordered->file_offset < alloc_end) {
5457 btrfs_put_ordered_extent(ordered);
5458 unlock_extent(&BTRFS_I(inode)->io_tree,
Chris Masone980b502009-04-24 14:39:24 -04005459 alloc_start, locked_end, GFP_NOFS);
Chris Mason546888d2009-04-21 11:53:38 -04005460 btrfs_end_transaction(trans, BTRFS_I(inode)->root);
5461
5462 /*
5463 * we can't wait on the range with the transaction
5464 * running or with the extent lock held
5465 */
Yan Zhengd899e052008-10-30 14:25:28 -04005466 btrfs_wait_ordered_range(inode, alloc_start,
5467 alloc_end - alloc_start);
5468 } else {
5469 if (ordered)
5470 btrfs_put_ordered_extent(ordered);
5471 break;
5472 }
5473 }
5474
5475 cur_offset = alloc_start;
5476 while (1) {
5477 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
5478 alloc_end - cur_offset, 0);
5479 BUG_ON(IS_ERR(em) || !em);
5480 last_byte = min(extent_map_end(em), alloc_end);
5481 last_byte = (last_byte + mask) & ~mask;
5482 if (em->block_start == EXTENT_MAP_HOLE) {
Chris Mason546888d2009-04-21 11:53:38 -04005483 ret = prealloc_file_range(trans, inode, cur_offset,
Chris Masone980b502009-04-24 14:39:24 -04005484 last_byte, locked_end + 1,
5485 alloc_hint, mode);
Yan Zhengd899e052008-10-30 14:25:28 -04005486 if (ret < 0) {
5487 free_extent_map(em);
5488 break;
5489 }
5490 }
5491 if (em->block_start <= EXTENT_MAP_LAST_BYTE)
5492 alloc_hint = em->block_start;
5493 free_extent_map(em);
5494
5495 cur_offset = last_byte;
5496 if (cur_offset >= alloc_end) {
5497 ret = 0;
5498 break;
5499 }
5500 }
Chris Masone980b502009-04-24 14:39:24 -04005501 unlock_extent(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
Yan Zhengd899e052008-10-30 14:25:28 -04005502 GFP_NOFS);
Chris Mason546888d2009-04-21 11:53:38 -04005503
5504 btrfs_end_transaction(trans, BTRFS_I(inode)->root);
Josef Bacika970b0a2009-06-27 21:07:34 -04005505out_free:
5506 btrfs_free_reserved_data_space(root, inode, alloc_end - alloc_start);
Yan Zhengd899e052008-10-30 14:25:28 -04005507out:
5508 mutex_unlock(&inode->i_mutex);
5509 return ret;
5510}
5511
Chris Masone6dcd2d2008-07-17 12:53:50 -04005512static int btrfs_set_page_dirty(struct page *page)
5513{
Chris Masone6dcd2d2008-07-17 12:53:50 -04005514 return __set_page_dirty_nobuffers(page);
5515}
5516
Sven Wegener0ee0fda2008-07-30 16:54:26 -04005517static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05005518{
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005519 if ((BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) && (mask & MAY_WRITE))
Yanfdebe2b2008-01-14 13:26:08 -05005520 return -EACCES;
Josef Bacik33268ea2008-07-24 12:16:36 -04005521 return generic_permission(inode, mask, btrfs_check_acl);
Yanfdebe2b2008-01-14 13:26:08 -05005522}
Chris Mason39279cc2007-06-12 06:35:45 -04005523
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07005524static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05005525 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04005526 .lookup = btrfs_lookup,
5527 .create = btrfs_create,
5528 .unlink = btrfs_unlink,
5529 .link = btrfs_link,
5530 .mkdir = btrfs_mkdir,
5531 .rmdir = btrfs_rmdir,
5532 .rename = btrfs_rename,
5533 .symlink = btrfs_symlink,
5534 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04005535 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04005536 .setxattr = btrfs_setxattr,
5537 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05005538 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04005539 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05005540 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04005541};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07005542static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04005543 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05005544 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04005545};
Yan, Zheng76dda932009-09-21 16:00:26 -04005546
Chris Mason39279cc2007-06-12 06:35:45 -04005547static struct file_operations btrfs_dir_file_operations = {
5548 .llseek = generic_file_llseek,
5549 .read = generic_read_dir,
David Woodhousecbdf5a22008-08-06 19:42:33 +01005550 .readdir = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04005551 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04005552#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04005553 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04005554#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04005555 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04005556 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04005557};
5558
Chris Masond1310b22008-01-24 16:13:08 -05005559static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04005560 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05005561 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04005562 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04005563 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005564 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04005565 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Mason1259ab72008-05-12 13:39:03 -04005566 .readpage_io_failed_hook = btrfs_io_failed_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05005567 .set_bit_hook = btrfs_set_bit_hook,
5568 .clear_bit_hook = btrfs_clear_bit_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04005569};
5570
Chris Mason35054392009-01-21 13:11:13 -05005571/*
5572 * btrfs doesn't support the bmap operation because swapfiles
5573 * use bmap to make a mapping of extents in the file. They assume
5574 * these extents won't change over the life of the file and they
5575 * use the bmap result to do IO directly to the drive.
5576 *
5577 * the btrfs bmap call would return logical addresses that aren't
5578 * suitable for IO and they also will change frequently as COW
5579 * operations happen. So, swapfile + btrfs == corruption.
5580 *
5581 * For now we're avoiding this by dropping bmap.
5582 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07005583static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04005584 .readpage = btrfs_readpage,
5585 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04005586 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05005587 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04005588 .sync_page = block_sync_page,
Chris Mason16432982008-04-10 10:23:21 -04005589 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04005590 .invalidatepage = btrfs_invalidatepage,
5591 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005592 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02005593 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04005594};
5595
Alexey Dobriyan7f094102009-09-21 17:01:10 -07005596static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04005597 .readpage = btrfs_readpage,
5598 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04005599 .invalidatepage = btrfs_invalidatepage,
5600 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04005601};
5602
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07005603static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04005604 .truncate = btrfs_truncate,
5605 .getattr = btrfs_getattr,
5606 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04005607 .setxattr = btrfs_setxattr,
5608 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05005609 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04005610 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05005611 .permission = btrfs_permission,
Yan Zhengd899e052008-10-30 14:25:28 -04005612 .fallocate = btrfs_fallocate,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05005613 .fiemap = btrfs_fiemap,
Chris Mason39279cc2007-06-12 06:35:45 -04005614};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07005615static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04005616 .getattr = btrfs_getattr,
5617 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05005618 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04005619 .setxattr = btrfs_setxattr,
5620 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04005621 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04005622 .removexattr = btrfs_removexattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04005623};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07005624static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04005625 .readlink = generic_readlink,
5626 .follow_link = page_follow_link_light,
5627 .put_link = page_put_link,
Yanfdebe2b2008-01-14 13:26:08 -05005628 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05005629 .setxattr = btrfs_setxattr,
5630 .getxattr = btrfs_getxattr,
5631 .listxattr = btrfs_listxattr,
5632 .removexattr = btrfs_removexattr,
Chris Mason39279cc2007-06-12 06:35:45 -04005633};
Yan, Zheng76dda932009-09-21 16:00:26 -04005634
5635struct dentry_operations btrfs_dentry_operations = {
5636 .d_delete = btrfs_dentry_delete,
5637};