blob: 4a79e1c5ebd07c5ffbb4c580ccf31fde3a9b9f32 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Mason8f18cf12008-04-25 16:53:30 -040019#include <linux/kernel.h>
Chris Mason065631f2008-02-20 12:07:25 -050020#include <linux/bio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040021#include <linux/buffer_head.h>
Sage Weilf2eb0a22008-05-02 14:43:14 -040022#include <linux/file.h>
Chris Mason39279cc2007-06-12 06:35:45 -040023#include <linux/fs.h>
24#include <linux/pagemap.h>
25#include <linux/highmem.h>
26#include <linux/time.h>
27#include <linux/init.h>
28#include <linux/string.h>
29#include <linux/smp_lock.h>
30#include <linux/backing-dev.h>
31#include <linux/mpage.h>
32#include <linux/swap.h>
33#include <linux/writeback.h>
34#include <linux/statfs.h>
35#include <linux/compat.h>
Chris Mason9ebefb182007-06-15 13:50:00 -040036#include <linux/bit_spinlock.h>
Josef Bacik5103e942007-11-16 11:45:54 -050037#include <linux/xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040038#include <linux/posix_acl.h>
Yan Zhengd899e052008-10-30 14:25:28 -040039#include <linux/falloc.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050040#include "compat.h"
Chris Mason39279cc2007-06-12 06:35:45 -040041#include "ctree.h"
42#include "disk-io.h"
43#include "transaction.h"
44#include "btrfs_inode.h"
45#include "ioctl.h"
46#include "print-tree.h"
Chris Mason0b86a832008-03-24 15:01:56 -040047#include "volumes.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040048#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040049#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040050#include "tree-log.h"
Yan Zheng5b84e8d2008-10-09 11:46:19 -040051#include "ref-cache.h"
Chris Masonc8b97812008-10-29 14:49:59 -040052#include "compression.h"
Chris Mason39279cc2007-06-12 06:35:45 -040053
54struct btrfs_iget_args {
55 u64 ino;
56 struct btrfs_root *root;
57};
58
59static struct inode_operations btrfs_dir_inode_operations;
60static struct inode_operations btrfs_symlink_inode_operations;
61static struct inode_operations btrfs_dir_ro_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -040062static struct inode_operations btrfs_special_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -040063static struct inode_operations btrfs_file_inode_operations;
64static struct address_space_operations btrfs_aops;
65static struct address_space_operations btrfs_symlink_aops;
66static struct file_operations btrfs_dir_file_operations;
Chris Masond1310b22008-01-24 16:13:08 -050067static struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040068
69static struct kmem_cache *btrfs_inode_cachep;
70struct kmem_cache *btrfs_trans_handle_cachep;
71struct kmem_cache *btrfs_transaction_cachep;
72struct kmem_cache *btrfs_bit_radix_cachep;
73struct kmem_cache *btrfs_path_cachep;
74
75#define S_SHIFT 12
76static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
77 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
78 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
79 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
80 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
81 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
82 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
83 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
84};
85
Josef Bacik7b128762008-07-24 12:17:14 -040086static void btrfs_truncate(struct inode *inode);
Chris Masonc8b97812008-10-29 14:49:59 -040087static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end);
Chris Mason771ed682008-11-06 22:02:51 -050088static noinline int cow_file_range(struct inode *inode,
89 struct page *locked_page,
90 u64 start, u64 end, int *page_started,
91 unsigned long *nr_written, int unlock);
Josef Bacik7b128762008-07-24 12:17:14 -040092
Jim Owens0279b4c2009-02-04 09:29:13 -050093static int btrfs_init_inode_security(struct inode *inode, struct inode *dir)
94{
95 int err;
96
97 err = btrfs_init_acl(inode, dir);
98 if (!err)
99 err = btrfs_xattr_security_init(inode, dir);
100 return err;
101}
102
Chris Masond352ac62008-09-29 15:18:18 -0400103/*
104 * a very lame attempt at stopping writes when the FS is 85% full. There
105 * are countless ways this is incorrect, but it is better than nothing.
106 */
Chris Mason1832a6d2007-12-21 16:27:21 -0500107int btrfs_check_free_space(struct btrfs_root *root, u64 num_required,
108 int for_del)
109{
Chris Masona2135012008-06-25 16:01:30 -0400110 u64 total;
111 u64 used;
Chris Mason1832a6d2007-12-21 16:27:21 -0500112 u64 thresh;
113 int ret = 0;
114
Chris Mason75eff682008-12-15 15:54:40 -0500115 spin_lock(&root->fs_info->delalloc_lock);
Chris Masona2135012008-06-25 16:01:30 -0400116 total = btrfs_super_total_bytes(&root->fs_info->super_copy);
117 used = btrfs_super_bytes_used(&root->fs_info->super_copy);
Chris Mason1832a6d2007-12-21 16:27:21 -0500118 if (for_del)
Chris Masonf9ef6602008-01-03 09:22:38 -0500119 thresh = total * 90;
Chris Mason1832a6d2007-12-21 16:27:21 -0500120 else
Chris Masonf9ef6602008-01-03 09:22:38 -0500121 thresh = total * 85;
122
123 do_div(thresh, 100);
Chris Mason1832a6d2007-12-21 16:27:21 -0500124
Chris Mason1832a6d2007-12-21 16:27:21 -0500125 if (used + root->fs_info->delalloc_bytes + num_required > thresh)
126 ret = -ENOSPC;
Chris Mason75eff682008-12-15 15:54:40 -0500127 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason1832a6d2007-12-21 16:27:21 -0500128 return ret;
129}
130
Chris Masond352ac62008-09-29 15:18:18 -0400131/*
Chris Masonc8b97812008-10-29 14:49:59 -0400132 * this does all the hard work for inserting an inline extent into
133 * the btree. The caller should have done a btrfs_drop_extents so that
134 * no overlapping inline items exist in the btree
135 */
Chris Masond3977122009-01-05 21:25:51 -0500136static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400137 struct btrfs_root *root, struct inode *inode,
138 u64 start, size_t size, size_t compressed_size,
139 struct page **compressed_pages)
140{
141 struct btrfs_key key;
142 struct btrfs_path *path;
143 struct extent_buffer *leaf;
144 struct page *page = NULL;
145 char *kaddr;
146 unsigned long ptr;
147 struct btrfs_file_extent_item *ei;
148 int err = 0;
149 int ret;
150 size_t cur_size = size;
151 size_t datasize;
152 unsigned long offset;
153 int use_compress = 0;
154
155 if (compressed_size && compressed_pages) {
156 use_compress = 1;
157 cur_size = compressed_size;
158 }
159
Chris Masond3977122009-01-05 21:25:51 -0500160 path = btrfs_alloc_path();
161 if (!path)
Chris Masonc8b97812008-10-29 14:49:59 -0400162 return -ENOMEM;
163
164 btrfs_set_trans_block_group(trans, inode);
165
166 key.objectid = inode->i_ino;
167 key.offset = start;
168 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
Chris Masonc8b97812008-10-29 14:49:59 -0400169 datasize = btrfs_file_extent_calc_inline_size(cur_size);
170
171 inode_add_bytes(inode, size);
172 ret = btrfs_insert_empty_item(trans, root, path, &key,
173 datasize);
174 BUG_ON(ret);
175 if (ret) {
176 err = ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400177 goto fail;
178 }
179 leaf = path->nodes[0];
180 ei = btrfs_item_ptr(leaf, path->slots[0],
181 struct btrfs_file_extent_item);
182 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
183 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
184 btrfs_set_file_extent_encryption(leaf, ei, 0);
185 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
186 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
187 ptr = btrfs_file_extent_inline_start(ei);
188
189 if (use_compress) {
190 struct page *cpage;
191 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500192 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400193 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500194 cur_size = min_t(unsigned long, compressed_size,
Chris Masonc8b97812008-10-29 14:49:59 -0400195 PAGE_CACHE_SIZE);
196
197 kaddr = kmap(cpage);
198 write_extent_buffer(leaf, kaddr, ptr, cur_size);
199 kunmap(cpage);
200
201 i++;
202 ptr += cur_size;
203 compressed_size -= cur_size;
204 }
205 btrfs_set_file_extent_compression(leaf, ei,
206 BTRFS_COMPRESS_ZLIB);
207 } else {
208 page = find_get_page(inode->i_mapping,
209 start >> PAGE_CACHE_SHIFT);
210 btrfs_set_file_extent_compression(leaf, ei, 0);
211 kaddr = kmap_atomic(page, KM_USER0);
212 offset = start & (PAGE_CACHE_SIZE - 1);
213 write_extent_buffer(leaf, kaddr + offset, ptr, size);
214 kunmap_atomic(kaddr, KM_USER0);
215 page_cache_release(page);
216 }
217 btrfs_mark_buffer_dirty(leaf);
218 btrfs_free_path(path);
219
220 BTRFS_I(inode)->disk_i_size = inode->i_size;
221 btrfs_update_inode(trans, root, inode);
222 return 0;
223fail:
224 btrfs_free_path(path);
225 return err;
226}
227
228
229/*
230 * conditionally insert an inline extent into the file. This
231 * does the checks required to make sure the data is small enough
232 * to fit as an inline extent.
233 */
234static int cow_file_range_inline(struct btrfs_trans_handle *trans,
235 struct btrfs_root *root,
236 struct inode *inode, u64 start, u64 end,
237 size_t compressed_size,
238 struct page **compressed_pages)
239{
240 u64 isize = i_size_read(inode);
241 u64 actual_end = min(end + 1, isize);
242 u64 inline_len = actual_end - start;
243 u64 aligned_end = (end + root->sectorsize - 1) &
244 ~((u64)root->sectorsize - 1);
245 u64 hint_byte;
246 u64 data_len = inline_len;
247 int ret;
248
249 if (compressed_size)
250 data_len = compressed_size;
251
252 if (start > 0 ||
Chris Mason70b99e62008-10-31 12:46:39 -0400253 actual_end >= PAGE_CACHE_SIZE ||
Chris Masonc8b97812008-10-29 14:49:59 -0400254 data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
255 (!compressed_size &&
256 (actual_end & (root->sectorsize - 1)) == 0) ||
257 end + 1 < isize ||
258 data_len > root->fs_info->max_inline) {
259 return 1;
260 }
261
Chris Masonc8b97812008-10-29 14:49:59 -0400262 ret = btrfs_drop_extents(trans, root, inode, start,
Chris Mason70b99e62008-10-31 12:46:39 -0400263 aligned_end, start, &hint_byte);
Chris Masonc8b97812008-10-29 14:49:59 -0400264 BUG_ON(ret);
265
266 if (isize > actual_end)
267 inline_len = min_t(u64, isize, actual_end);
268 ret = insert_inline_extent(trans, root, inode, start,
269 inline_len, compressed_size,
270 compressed_pages);
271 BUG_ON(ret);
272 btrfs_drop_extent_cache(inode, start, aligned_end, 0);
Chris Masonc8b97812008-10-29 14:49:59 -0400273 return 0;
274}
275
Chris Mason771ed682008-11-06 22:02:51 -0500276struct async_extent {
277 u64 start;
278 u64 ram_size;
279 u64 compressed_size;
280 struct page **pages;
281 unsigned long nr_pages;
282 struct list_head list;
283};
284
285struct async_cow {
286 struct inode *inode;
287 struct btrfs_root *root;
288 struct page *locked_page;
289 u64 start;
290 u64 end;
291 struct list_head extents;
292 struct btrfs_work work;
293};
294
295static noinline int add_async_extent(struct async_cow *cow,
296 u64 start, u64 ram_size,
297 u64 compressed_size,
298 struct page **pages,
299 unsigned long nr_pages)
300{
301 struct async_extent *async_extent;
302
303 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
304 async_extent->start = start;
305 async_extent->ram_size = ram_size;
306 async_extent->compressed_size = compressed_size;
307 async_extent->pages = pages;
308 async_extent->nr_pages = nr_pages;
309 list_add_tail(&async_extent->list, &cow->extents);
310 return 0;
311}
312
Chris Masonc8b97812008-10-29 14:49:59 -0400313/*
Chris Mason771ed682008-11-06 22:02:51 -0500314 * we create compressed extents in two phases. The first
315 * phase compresses a range of pages that have already been
316 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400317 *
Chris Mason771ed682008-11-06 22:02:51 -0500318 * This is done inside an ordered work queue, and the compression
319 * is spread across many cpus. The actual IO submission is step
320 * two, and the ordered work queue takes care of making sure that
321 * happens in the same order things were put onto the queue by
322 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400323 *
Chris Mason771ed682008-11-06 22:02:51 -0500324 * If this code finds it can't get good compression, it puts an
325 * entry onto the work queue to write the uncompressed bytes. This
326 * makes sure that both compressed inodes and uncompressed inodes
327 * are written in the same order that pdflush sent them down.
Chris Masond352ac62008-09-29 15:18:18 -0400328 */
Chris Mason771ed682008-11-06 22:02:51 -0500329static noinline int compress_file_range(struct inode *inode,
330 struct page *locked_page,
331 u64 start, u64 end,
332 struct async_cow *async_cow,
333 int *num_added)
Chris Masonb888db22007-08-27 16:49:44 -0400334{
335 struct btrfs_root *root = BTRFS_I(inode)->root;
336 struct btrfs_trans_handle *trans;
Chris Masondb945352007-10-15 16:15:53 -0400337 u64 num_bytes;
Chris Masonc8b97812008-10-29 14:49:59 -0400338 u64 orig_start;
339 u64 disk_num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400340 u64 blocksize = root->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400341 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500342 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400343 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400344 struct page **pages = NULL;
345 unsigned long nr_pages;
346 unsigned long nr_pages_ret = 0;
347 unsigned long total_compressed = 0;
348 unsigned long total_in = 0;
349 unsigned long max_compressed = 128 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -0500350 unsigned long max_uncompressed = 128 * 1024;
Chris Masonc8b97812008-10-29 14:49:59 -0400351 int i;
352 int will_compress;
Chris Masonb888db22007-08-27 16:49:44 -0400353
Chris Masonc8b97812008-10-29 14:49:59 -0400354 orig_start = start;
Chris Masonbe20aa92007-12-17 20:14:01 -0500355
Chris Mason42dc7ba2008-12-15 11:44:56 -0500356 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400357again:
358 will_compress = 0;
359 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
360 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
361
Chris Masonc8b97812008-10-29 14:49:59 -0400362 total_compressed = actual_end - start;
363
364 /* we want to make sure that amount of ram required to uncompress
365 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500366 * of a compressed extent to 128k. This is a crucial number
367 * because it also controls how easily we can spread reads across
368 * cpus for decompression.
369 *
370 * We also want to make sure the amount of IO required to do
371 * a random read is reasonably small, so we limit the size of
372 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400373 */
374 total_compressed = min(total_compressed, max_uncompressed);
Chris Masondb945352007-10-15 16:15:53 -0400375 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
Chris Masonbe20aa92007-12-17 20:14:01 -0500376 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400377 disk_num_bytes = num_bytes;
378 total_in = 0;
379 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400380
Chris Mason771ed682008-11-06 22:02:51 -0500381 /*
382 * we do compression for mount -o compress and when the
383 * inode has not been flagged as nocompress. This flag can
384 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400385 */
386 if (!btrfs_test_flag(inode, NOCOMPRESS) &&
387 btrfs_test_opt(root, COMPRESS)) {
388 WARN_ON(pages);
Chris Masoncfbc2462008-10-30 13:22:14 -0400389 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
Chris Mason179e29e2007-11-01 11:28:41 -0400390
Chris Masonc8b97812008-10-29 14:49:59 -0400391 ret = btrfs_zlib_compress_pages(inode->i_mapping, start,
392 total_compressed, pages,
393 nr_pages, &nr_pages_ret,
394 &total_in,
395 &total_compressed,
396 max_compressed);
397
398 if (!ret) {
399 unsigned long offset = total_compressed &
400 (PAGE_CACHE_SIZE - 1);
401 struct page *page = pages[nr_pages_ret - 1];
402 char *kaddr;
403
404 /* zero the tail end of the last page, we might be
405 * sending it down to disk
406 */
407 if (offset) {
408 kaddr = kmap_atomic(page, KM_USER0);
409 memset(kaddr + offset, 0,
410 PAGE_CACHE_SIZE - offset);
411 kunmap_atomic(kaddr, KM_USER0);
412 }
413 will_compress = 1;
414 }
415 }
416 if (start == 0) {
Chris Mason771ed682008-11-06 22:02:51 -0500417 trans = btrfs_join_transaction(root, 1);
418 BUG_ON(!trans);
419 btrfs_set_trans_block_group(trans, inode);
420
Chris Masonc8b97812008-10-29 14:49:59 -0400421 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500422 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400423 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500424 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400425 */
426 ret = cow_file_range_inline(trans, root, inode,
427 start, end, 0, NULL);
428 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500429 /* try making a compressed inline extent */
Chris Masonc8b97812008-10-29 14:49:59 -0400430 ret = cow_file_range_inline(trans, root, inode,
431 start, end,
432 total_compressed, pages);
433 }
Chris Mason771ed682008-11-06 22:02:51 -0500434 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400435 if (ret == 0) {
Chris Mason771ed682008-11-06 22:02:51 -0500436 /*
437 * inline extent creation worked, we don't need
438 * to create any more async work items. Unlock
439 * and free up our temp pages.
440 */
Chris Masonc8b97812008-10-29 14:49:59 -0400441 extent_clear_unlock_delalloc(inode,
442 &BTRFS_I(inode)->io_tree,
Chris Mason771ed682008-11-06 22:02:51 -0500443 start, end, NULL, 1, 0,
444 0, 1, 1, 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400445 ret = 0;
446 goto free_pages_out;
447 }
448 }
449
450 if (will_compress) {
451 /*
452 * we aren't doing an inline extent round the compressed size
453 * up to a block size boundary so the allocator does sane
454 * things
455 */
456 total_compressed = (total_compressed + blocksize - 1) &
457 ~(blocksize - 1);
458
459 /*
460 * one last check to make sure the compression is really a
461 * win, compare the page count read with the blocks on disk
462 */
463 total_in = (total_in + PAGE_CACHE_SIZE - 1) &
464 ~(PAGE_CACHE_SIZE - 1);
465 if (total_compressed >= total_in) {
466 will_compress = 0;
467 } else {
468 disk_num_bytes = total_compressed;
469 num_bytes = total_in;
470 }
471 }
472 if (!will_compress && pages) {
473 /*
474 * the compression code ran but failed to make things smaller,
475 * free any pages it allocated and our page pointer array
476 */
477 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400478 WARN_ON(pages[i]->mapping);
Chris Masonc8b97812008-10-29 14:49:59 -0400479 page_cache_release(pages[i]);
480 }
481 kfree(pages);
482 pages = NULL;
483 total_compressed = 0;
484 nr_pages_ret = 0;
485
486 /* flag the file so we don't compress in the future */
487 btrfs_set_flag(inode, NOCOMPRESS);
488 }
Chris Mason771ed682008-11-06 22:02:51 -0500489 if (will_compress) {
490 *num_added += 1;
491
492 /* the async work queues will take care of doing actual
493 * allocation on disk for these compressed pages,
494 * and will submit them to the elevator.
495 */
496 add_async_extent(async_cow, start, num_bytes,
497 total_compressed, pages, nr_pages_ret);
498
Chris Mason42dc7ba2008-12-15 11:44:56 -0500499 if (start + num_bytes < end && start + num_bytes < actual_end) {
Chris Mason771ed682008-11-06 22:02:51 -0500500 start += num_bytes;
501 pages = NULL;
502 cond_resched();
503 goto again;
504 }
505 } else {
506 /*
507 * No compression, but we still need to write the pages in
508 * the file we've been given so far. redirty the locked
509 * page if it corresponds to our extent and set things up
510 * for the async work queue to run cow_file_range to do
511 * the normal delalloc dance
512 */
513 if (page_offset(locked_page) >= start &&
514 page_offset(locked_page) <= end) {
515 __set_page_dirty_nobuffers(locked_page);
516 /* unlocked later on in the async handlers */
517 }
518 add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0);
519 *num_added += 1;
520 }
521
522out:
523 return 0;
524
525free_pages_out:
526 for (i = 0; i < nr_pages_ret; i++) {
527 WARN_ON(pages[i]->mapping);
528 page_cache_release(pages[i]);
529 }
Chris Masond3977122009-01-05 21:25:51 -0500530 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500531
532 goto out;
533}
534
535/*
536 * phase two of compressed writeback. This is the ordered portion
537 * of the code, which only gets called in the order the work was
538 * queued. We walk all the async extents created by compress_file_range
539 * and send them down to the disk.
540 */
541static noinline int submit_compressed_extents(struct inode *inode,
542 struct async_cow *async_cow)
543{
544 struct async_extent *async_extent;
545 u64 alloc_hint = 0;
546 struct btrfs_trans_handle *trans;
547 struct btrfs_key ins;
548 struct extent_map *em;
549 struct btrfs_root *root = BTRFS_I(inode)->root;
550 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
551 struct extent_io_tree *io_tree;
552 int ret;
553
554 if (list_empty(&async_cow->extents))
555 return 0;
556
557 trans = btrfs_join_transaction(root, 1);
558
Chris Masond3977122009-01-05 21:25:51 -0500559 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500560 async_extent = list_entry(async_cow->extents.next,
561 struct async_extent, list);
562 list_del(&async_extent->list);
563
564 io_tree = &BTRFS_I(inode)->io_tree;
565
566 /* did the compression code fall back to uncompressed IO? */
567 if (!async_extent->pages) {
568 int page_started = 0;
569 unsigned long nr_written = 0;
570
571 lock_extent(io_tree, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500572 async_extent->start +
573 async_extent->ram_size - 1, GFP_NOFS);
Chris Mason771ed682008-11-06 22:02:51 -0500574
575 /* allocate blocks */
576 cow_file_range(inode, async_cow->locked_page,
577 async_extent->start,
578 async_extent->start +
579 async_extent->ram_size - 1,
580 &page_started, &nr_written, 0);
581
582 /*
583 * if page_started, cow_file_range inserted an
584 * inline extent and took care of all the unlocking
585 * and IO for us. Otherwise, we need to submit
586 * all those pages down to the drive.
587 */
588 if (!page_started)
589 extent_write_locked_range(io_tree,
590 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500591 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500592 async_extent->ram_size - 1,
593 btrfs_get_extent,
594 WB_SYNC_ALL);
595 kfree(async_extent);
596 cond_resched();
597 continue;
598 }
599
600 lock_extent(io_tree, async_extent->start,
601 async_extent->start + async_extent->ram_size - 1,
602 GFP_NOFS);
603 /*
604 * here we're doing allocation and writeback of the
605 * compressed pages
606 */
607 btrfs_drop_extent_cache(inode, async_extent->start,
608 async_extent->start +
609 async_extent->ram_size - 1, 0);
610
611 ret = btrfs_reserve_extent(trans, root,
612 async_extent->compressed_size,
613 async_extent->compressed_size,
614 0, alloc_hint,
615 (u64)-1, &ins, 1);
616 BUG_ON(ret);
617 em = alloc_extent_map(GFP_NOFS);
618 em->start = async_extent->start;
619 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500620 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500621
622 em->block_start = ins.objectid;
623 em->block_len = ins.offset;
624 em->bdev = root->fs_info->fs_devices->latest_bdev;
625 set_bit(EXTENT_FLAG_PINNED, &em->flags);
626 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
627
Chris Masond3977122009-01-05 21:25:51 -0500628 while (1) {
Chris Mason771ed682008-11-06 22:02:51 -0500629 spin_lock(&em_tree->lock);
630 ret = add_extent_mapping(em_tree, em);
631 spin_unlock(&em_tree->lock);
632 if (ret != -EEXIST) {
633 free_extent_map(em);
634 break;
635 }
636 btrfs_drop_extent_cache(inode, async_extent->start,
637 async_extent->start +
638 async_extent->ram_size - 1, 0);
639 }
640
641 ret = btrfs_add_ordered_extent(inode, async_extent->start,
642 ins.objectid,
643 async_extent->ram_size,
644 ins.offset,
645 BTRFS_ORDERED_COMPRESSED);
646 BUG_ON(ret);
647
648 btrfs_end_transaction(trans, root);
649
650 /*
651 * clear dirty, set writeback and unlock the pages.
652 */
653 extent_clear_unlock_delalloc(inode,
654 &BTRFS_I(inode)->io_tree,
655 async_extent->start,
656 async_extent->start +
657 async_extent->ram_size - 1,
658 NULL, 1, 1, 0, 1, 1, 0);
659
660 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500661 async_extent->start,
662 async_extent->ram_size,
663 ins.objectid,
664 ins.offset, async_extent->pages,
665 async_extent->nr_pages);
Chris Mason771ed682008-11-06 22:02:51 -0500666
667 BUG_ON(ret);
668 trans = btrfs_join_transaction(root, 1);
669 alloc_hint = ins.objectid + ins.offset;
670 kfree(async_extent);
671 cond_resched();
672 }
673
674 btrfs_end_transaction(trans, root);
675 return 0;
676}
677
678/*
679 * when extent_io.c finds a delayed allocation range in the file,
680 * the call backs end up in this code. The basic idea is to
681 * allocate extents on disk for the range, and create ordered data structs
682 * in ram to track those extents.
683 *
684 * locked_page is the page that writepage had locked already. We use
685 * it to make sure we don't do extra locks or unlocks.
686 *
687 * *page_started is set to one if we unlock locked_page and do everything
688 * required to start IO on it. It may be clean and already done with
689 * IO when we return.
690 */
691static noinline int cow_file_range(struct inode *inode,
692 struct page *locked_page,
693 u64 start, u64 end, int *page_started,
694 unsigned long *nr_written,
695 int unlock)
696{
697 struct btrfs_root *root = BTRFS_I(inode)->root;
698 struct btrfs_trans_handle *trans;
699 u64 alloc_hint = 0;
700 u64 num_bytes;
701 unsigned long ram_size;
702 u64 disk_num_bytes;
703 u64 cur_alloc_size;
704 u64 blocksize = root->sectorsize;
705 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500706 u64 isize = i_size_read(inode);
Chris Mason771ed682008-11-06 22:02:51 -0500707 struct btrfs_key ins;
708 struct extent_map *em;
709 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
710 int ret = 0;
711
712 trans = btrfs_join_transaction(root, 1);
713 BUG_ON(!trans);
714 btrfs_set_trans_block_group(trans, inode);
715
Chris Mason42dc7ba2008-12-15 11:44:56 -0500716 actual_end = min_t(u64, isize, end + 1);
Chris Mason771ed682008-11-06 22:02:51 -0500717
718 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
719 num_bytes = max(blocksize, num_bytes);
720 disk_num_bytes = num_bytes;
721 ret = 0;
722
723 if (start == 0) {
724 /* lets try to make an inline extent */
725 ret = cow_file_range_inline(trans, root, inode,
726 start, end, 0, NULL);
727 if (ret == 0) {
728 extent_clear_unlock_delalloc(inode,
729 &BTRFS_I(inode)->io_tree,
730 start, end, NULL, 1, 1,
731 1, 1, 1, 1);
732 *nr_written = *nr_written +
733 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
734 *page_started = 1;
735 ret = 0;
736 goto out;
737 }
738 }
Chris Masonc8b97812008-10-29 14:49:59 -0400739
740 BUG_ON(disk_num_bytes >
741 btrfs_super_total_bytes(&root->fs_info->super_copy));
742
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400743 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400744
Chris Masond3977122009-01-05 21:25:51 -0500745 while (disk_num_bytes > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400746 cur_alloc_size = min(disk_num_bytes, root->fs_info->max_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400747 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500748 root->sectorsize, 0, alloc_hint,
Chris Masone6dcd2d2008-07-17 12:53:50 -0400749 (u64)-1, &ins, 1);
Chris Masond3977122009-01-05 21:25:51 -0500750 BUG_ON(ret);
751
Chris Masone6dcd2d2008-07-17 12:53:50 -0400752 em = alloc_extent_map(GFP_NOFS);
753 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500754 em->orig_start = em->start;
Chris Masonc8b97812008-10-29 14:49:59 -0400755
Chris Mason771ed682008-11-06 22:02:51 -0500756 ram_size = ins.offset;
757 em->len = ins.offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400758
Chris Masone6dcd2d2008-07-17 12:53:50 -0400759 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400760 em->block_len = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400761 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400762 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -0400763
Chris Masond3977122009-01-05 21:25:51 -0500764 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -0400765 spin_lock(&em_tree->lock);
766 ret = add_extent_mapping(em_tree, em);
767 spin_unlock(&em_tree->lock);
768 if (ret != -EEXIST) {
769 free_extent_map(em);
770 break;
771 }
772 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400773 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400774 }
775
Chris Mason98d20f62008-04-14 09:46:10 -0400776 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400777 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -0500778 ram_size, cur_alloc_size, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400779 BUG_ON(ret);
Chris Masonc8b97812008-10-29 14:49:59 -0400780
Yan Zheng17d217f2008-12-12 10:03:38 -0500781 if (root->root_key.objectid ==
782 BTRFS_DATA_RELOC_TREE_OBJECTID) {
783 ret = btrfs_reloc_clone_csums(inode, start,
784 cur_alloc_size);
785 BUG_ON(ret);
786 }
787
Chris Masond3977122009-01-05 21:25:51 -0500788 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -0400789 break;
Chris Masond3977122009-01-05 21:25:51 -0500790
Chris Masonc8b97812008-10-29 14:49:59 -0400791 /* we're not doing compressed IO, don't unlock the first
792 * page (which the caller expects to stay locked), don't
793 * clear any dirty bits and don't set any writeback bits
794 */
795 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
796 start, start + ram_size - 1,
Chris Mason771ed682008-11-06 22:02:51 -0500797 locked_page, unlock, 1,
798 1, 0, 0, 0);
Chris Masonc8b97812008-10-29 14:49:59 -0400799 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -0500800 num_bytes -= cur_alloc_size;
801 alloc_hint = ins.objectid + ins.offset;
802 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400803 }
Chris Masonb888db22007-08-27 16:49:44 -0400804out:
Chris Mason771ed682008-11-06 22:02:51 -0500805 ret = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400806 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400807
Chris Masonbe20aa92007-12-17 20:14:01 -0500808 return ret;
Chris Mason771ed682008-11-06 22:02:51 -0500809}
Chris Masonc8b97812008-10-29 14:49:59 -0400810
Chris Mason771ed682008-11-06 22:02:51 -0500811/*
812 * work queue call back to started compression on a file and pages
813 */
814static noinline void async_cow_start(struct btrfs_work *work)
815{
816 struct async_cow *async_cow;
817 int num_added = 0;
818 async_cow = container_of(work, struct async_cow, work);
819
820 compress_file_range(async_cow->inode, async_cow->locked_page,
821 async_cow->start, async_cow->end, async_cow,
822 &num_added);
823 if (num_added == 0)
824 async_cow->inode = NULL;
825}
826
827/*
828 * work queue call back to submit previously compressed pages
829 */
830static noinline void async_cow_submit(struct btrfs_work *work)
831{
832 struct async_cow *async_cow;
833 struct btrfs_root *root;
834 unsigned long nr_pages;
835
836 async_cow = container_of(work, struct async_cow, work);
837
838 root = async_cow->root;
839 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
840 PAGE_CACHE_SHIFT;
841
842 atomic_sub(nr_pages, &root->fs_info->async_delalloc_pages);
843
844 if (atomic_read(&root->fs_info->async_delalloc_pages) <
845 5 * 1042 * 1024 &&
846 waitqueue_active(&root->fs_info->async_submit_wait))
847 wake_up(&root->fs_info->async_submit_wait);
848
Chris Masond3977122009-01-05 21:25:51 -0500849 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -0500850 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -0500851}
Chris Masonc8b97812008-10-29 14:49:59 -0400852
Chris Mason771ed682008-11-06 22:02:51 -0500853static noinline void async_cow_free(struct btrfs_work *work)
854{
855 struct async_cow *async_cow;
856 async_cow = container_of(work, struct async_cow, work);
857 kfree(async_cow);
858}
859
860static int cow_file_range_async(struct inode *inode, struct page *locked_page,
861 u64 start, u64 end, int *page_started,
862 unsigned long *nr_written)
863{
864 struct async_cow *async_cow;
865 struct btrfs_root *root = BTRFS_I(inode)->root;
866 unsigned long nr_pages;
867 u64 cur_end;
868 int limit = 10 * 1024 * 1042;
869
870 if (!btrfs_test_opt(root, COMPRESS)) {
871 return cow_file_range(inode, locked_page, start, end,
872 page_started, nr_written, 1);
873 }
874
875 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED |
876 EXTENT_DELALLOC, 1, 0, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -0500877 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500878 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
879 async_cow->inode = inode;
880 async_cow->root = root;
881 async_cow->locked_page = locked_page;
882 async_cow->start = start;
883
884 if (btrfs_test_flag(inode, NOCOMPRESS))
885 cur_end = end;
886 else
887 cur_end = min(end, start + 512 * 1024 - 1);
888
889 async_cow->end = cur_end;
890 INIT_LIST_HEAD(&async_cow->extents);
891
892 async_cow->work.func = async_cow_start;
893 async_cow->work.ordered_func = async_cow_submit;
894 async_cow->work.ordered_free = async_cow_free;
895 async_cow->work.flags = 0;
896
Chris Mason771ed682008-11-06 22:02:51 -0500897 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
898 PAGE_CACHE_SHIFT;
899 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
900
901 btrfs_queue_worker(&root->fs_info->delalloc_workers,
902 &async_cow->work);
903
904 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
905 wait_event(root->fs_info->async_submit_wait,
906 (atomic_read(&root->fs_info->async_delalloc_pages) <
907 limit));
908 }
909
Chris Masond3977122009-01-05 21:25:51 -0500910 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -0500911 atomic_read(&root->fs_info->async_delalloc_pages)) {
912 wait_event(root->fs_info->async_submit_wait,
913 (atomic_read(&root->fs_info->async_delalloc_pages) ==
914 0));
915 }
916
917 *nr_written += nr_pages;
918 start = cur_end + 1;
919 }
920 *page_started = 1;
921 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -0500922}
923
Chris Masond3977122009-01-05 21:25:51 -0500924static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -0500925 u64 bytenr, u64 num_bytes)
926{
927 int ret;
928 struct btrfs_ordered_sum *sums;
929 LIST_HEAD(list);
930
Yan Zheng07d400a2009-01-06 11:42:00 -0500931 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
932 bytenr + num_bytes - 1, &list);
Yan Zheng17d217f2008-12-12 10:03:38 -0500933 if (ret == 0 && list_empty(&list))
934 return 0;
935
936 while (!list_empty(&list)) {
937 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
938 list_del(&sums->list);
939 kfree(sums);
940 }
941 return 1;
942}
943
Chris Masond352ac62008-09-29 15:18:18 -0400944/*
945 * when nowcow writeback call back. This checks for snapshots or COW copies
946 * of the extents that exist in the file, and COWs the file as required.
947 *
948 * If no cow copies or snapshots exist, we write directly to the existing
949 * blocks on disk
950 */
Chris Masonc8b97812008-10-29 14:49:59 -0400951static int run_delalloc_nocow(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -0500952 u64 start, u64 end, int *page_started, int force,
953 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -0500954{
Chris Masonbe20aa92007-12-17 20:14:01 -0500955 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -0400956 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -0500957 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -0500958 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -0400959 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -0500960 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -0400961 u64 cow_start;
962 u64 cur_offset;
963 u64 extent_end;
964 u64 disk_bytenr;
965 u64 num_bytes;
966 int extent_type;
967 int ret;
Yan Zhengd899e052008-10-30 14:25:28 -0400968 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -0400969 int nocow;
970 int check_prev = 1;
Chris Masonbe20aa92007-12-17 20:14:01 -0500971
972 path = btrfs_alloc_path();
973 BUG_ON(!path);
Yan Zheng7ea394f2008-08-05 13:05:02 -0400974 trans = btrfs_join_transaction(root, 1);
975 BUG_ON(!trans);
Chris Masonbe20aa92007-12-17 20:14:01 -0500976
Yan Zheng80ff3852008-10-30 14:20:02 -0400977 cow_start = (u64)-1;
978 cur_offset = start;
979 while (1) {
980 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
981 cur_offset, 0);
982 BUG_ON(ret < 0);
983 if (ret > 0 && path->slots[0] > 0 && check_prev) {
984 leaf = path->nodes[0];
985 btrfs_item_key_to_cpu(leaf, &found_key,
986 path->slots[0] - 1);
987 if (found_key.objectid == inode->i_ino &&
988 found_key.type == BTRFS_EXTENT_DATA_KEY)
989 path->slots[0]--;
990 }
991 check_prev = 0;
992next_slot:
993 leaf = path->nodes[0];
994 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
995 ret = btrfs_next_leaf(root, path);
996 if (ret < 0)
997 BUG_ON(1);
998 if (ret > 0)
999 break;
1000 leaf = path->nodes[0];
1001 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001002
Yan Zheng80ff3852008-10-30 14:20:02 -04001003 nocow = 0;
1004 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001005 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001006 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001007
Yan Zheng80ff3852008-10-30 14:20:02 -04001008 if (found_key.objectid > inode->i_ino ||
1009 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1010 found_key.offset > end)
1011 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001012
Yan Zheng80ff3852008-10-30 14:20:02 -04001013 if (found_key.offset > cur_offset) {
1014 extent_end = found_key.offset;
1015 goto out_check;
1016 }
Chris Masonc31f8832008-01-08 15:46:31 -05001017
Yan Zheng80ff3852008-10-30 14:20:02 -04001018 fi = btrfs_item_ptr(leaf, path->slots[0],
1019 struct btrfs_file_extent_item);
1020 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001021
Yan Zhengd899e052008-10-30 14:25:28 -04001022 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1023 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001024 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1025 extent_end = found_key.offset +
1026 btrfs_file_extent_num_bytes(leaf, fi);
1027 if (extent_end <= start) {
1028 path->slots[0]++;
1029 goto next_slot;
1030 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001031 if (disk_bytenr == 0)
1032 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001033 if (btrfs_file_extent_compression(leaf, fi) ||
1034 btrfs_file_extent_encryption(leaf, fi) ||
1035 btrfs_file_extent_other_encoding(leaf, fi))
1036 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001037 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1038 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001039 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001040 goto out_check;
Yan Zheng17d217f2008-12-12 10:03:38 -05001041 if (btrfs_cross_ref_exist(trans, root, inode->i_ino,
1042 disk_bytenr))
1043 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001044 disk_bytenr += btrfs_file_extent_offset(leaf, fi);
Yan Zheng17d217f2008-12-12 10:03:38 -05001045 disk_bytenr += cur_offset - found_key.offset;
1046 num_bytes = min(end + 1, extent_end) - cur_offset;
1047 /*
1048 * force cow if csum exists in the range.
1049 * this ensure that csum for a given extent are
1050 * either valid or do not exist.
1051 */
1052 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1053 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001054 nocow = 1;
1055 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1056 extent_end = found_key.offset +
1057 btrfs_file_extent_inline_len(leaf, fi);
1058 extent_end = ALIGN(extent_end, root->sectorsize);
1059 } else {
1060 BUG_ON(1);
1061 }
1062out_check:
1063 if (extent_end <= start) {
1064 path->slots[0]++;
1065 goto next_slot;
1066 }
1067 if (!nocow) {
1068 if (cow_start == (u64)-1)
1069 cow_start = cur_offset;
1070 cur_offset = extent_end;
1071 if (cur_offset > end)
1072 break;
1073 path->slots[0]++;
1074 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001075 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001076
Yan Zheng7ea394f2008-08-05 13:05:02 -04001077 btrfs_release_path(root, path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001078 if (cow_start != (u64)-1) {
1079 ret = cow_file_range(inode, locked_page, cow_start,
Chris Mason771ed682008-11-06 22:02:51 -05001080 found_key.offset - 1, page_started,
1081 nr_written, 1);
Yan Zheng80ff3852008-10-30 14:20:02 -04001082 BUG_ON(ret);
1083 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001084 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001085
Yan Zhengd899e052008-10-30 14:25:28 -04001086 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1087 struct extent_map *em;
1088 struct extent_map_tree *em_tree;
1089 em_tree = &BTRFS_I(inode)->extent_tree;
1090 em = alloc_extent_map(GFP_NOFS);
1091 em->start = cur_offset;
Chris Mason445a6942008-11-10 11:53:33 -05001092 em->orig_start = em->start;
Yan Zhengd899e052008-10-30 14:25:28 -04001093 em->len = num_bytes;
1094 em->block_len = num_bytes;
1095 em->block_start = disk_bytenr;
1096 em->bdev = root->fs_info->fs_devices->latest_bdev;
1097 set_bit(EXTENT_FLAG_PINNED, &em->flags);
1098 while (1) {
1099 spin_lock(&em_tree->lock);
1100 ret = add_extent_mapping(em_tree, em);
1101 spin_unlock(&em_tree->lock);
1102 if (ret != -EEXIST) {
1103 free_extent_map(em);
1104 break;
1105 }
1106 btrfs_drop_extent_cache(inode, em->start,
1107 em->start + em->len - 1, 0);
1108 }
1109 type = BTRFS_ORDERED_PREALLOC;
1110 } else {
1111 type = BTRFS_ORDERED_NOCOW;
1112 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001113
1114 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001115 num_bytes, num_bytes, type);
1116 BUG_ON(ret);
Chris Mason771ed682008-11-06 22:02:51 -05001117
Yan Zhengd899e052008-10-30 14:25:28 -04001118 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
1119 cur_offset, cur_offset + num_bytes - 1,
Chris Mason771ed682008-11-06 22:02:51 -05001120 locked_page, 1, 1, 1, 0, 0, 0);
Yan Zheng80ff3852008-10-30 14:20:02 -04001121 cur_offset = extent_end;
1122 if (cur_offset > end)
1123 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001124 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001125 btrfs_release_path(root, path);
1126
1127 if (cur_offset <= end && cow_start == (u64)-1)
1128 cow_start = cur_offset;
1129 if (cow_start != (u64)-1) {
1130 ret = cow_file_range(inode, locked_page, cow_start, end,
Chris Mason771ed682008-11-06 22:02:51 -05001131 page_started, nr_written, 1);
Yan Zheng80ff3852008-10-30 14:20:02 -04001132 BUG_ON(ret);
1133 }
1134
1135 ret = btrfs_end_transaction(trans, root);
1136 BUG_ON(ret);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001137 btrfs_free_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001138 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001139}
1140
Chris Masond352ac62008-09-29 15:18:18 -04001141/*
1142 * extent_io.c call back to do delayed allocation processing
1143 */
Chris Masonc8b97812008-10-29 14:49:59 -04001144static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001145 u64 start, u64 end, int *page_started,
1146 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001147{
Chris Masonbe20aa92007-12-17 20:14:01 -05001148 int ret;
Chris Masona2135012008-06-25 16:01:30 -04001149
Yan Zheng17d217f2008-12-12 10:03:38 -05001150 if (btrfs_test_flag(inode, NODATACOW))
Chris Masonc8b97812008-10-29 14:49:59 -04001151 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001152 page_started, 1, nr_written);
Yan Zhengd899e052008-10-30 14:25:28 -04001153 else if (btrfs_test_flag(inode, PREALLOC))
1154 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001155 page_started, 0, nr_written);
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 Mason1832a6d2007-12-21 16:27:21 -05001159
Chris Masonb888db22007-08-27 16:49:44 -04001160 return ret;
1161}
1162
Chris Masond352ac62008-09-29 15:18:18 -04001163/*
1164 * extent_io.c set_bit_hook, used to track delayed allocation
1165 * bytes in this file, and to maintain the list of inodes that
1166 * have pending delalloc work to be done.
1167 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001168static int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -05001169 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -05001170{
Chris Mason75eff682008-12-15 15:54:40 -05001171 /*
1172 * set_bit and clear bit hooks normally require _irqsave/restore
1173 * but in this case, we are only testeing for the DELALLOC
1174 * bit, which is only set or cleared with irqs on
1175 */
Chris Masonb0c68f82008-01-31 11:05:37 -05001176 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001177 struct btrfs_root *root = BTRFS_I(inode)->root;
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);
Chris Masonb0c68f82008-01-31 11:05:37 -05001211 root->fs_info->delalloc_bytes = 0;
Chris Mason90692182008-02-08 13:49:28 -05001212 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masonb0c68f82008-01-31 11:05:37 -05001213 } else {
1214 root->fs_info->delalloc_bytes -= end - start + 1;
Chris Mason90692182008-02-08 13:49:28 -05001215 BTRFS_I(inode)->delalloc_bytes -= end - start + 1;
Chris Masonb0c68f82008-01-31 11:05:37 -05001216 }
Chris Masonea8c2812008-08-04 23:17:27 -04001217 if (BTRFS_I(inode)->delalloc_bytes == 0 &&
1218 !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1219 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1220 }
Chris Mason75eff682008-12-15 15:54:40 -05001221 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001222 }
1223 return 0;
1224}
1225
Chris Masond352ac62008-09-29 15:18:18 -04001226/*
1227 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1228 * we don't create bios that span stripes or chunks
1229 */
Chris Mason239b14b2008-03-24 15:02:07 -04001230int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001231 size_t size, struct bio *bio,
1232 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001233{
1234 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
1235 struct btrfs_mapping_tree *map_tree;
Chris Masona62b9402008-10-03 16:31:08 -04001236 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001237 u64 length = 0;
1238 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001239 int ret;
1240
Chris Mason771ed682008-11-06 22:02:51 -05001241 if (bio_flags & EXTENT_BIO_COMPRESSED)
1242 return 0;
1243
Chris Masonf2d8d742008-04-21 10:03:05 -04001244 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001245 map_tree = &root->fs_info->mapping_tree;
1246 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04001247 ret = btrfs_map_block(map_tree, READ, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001248 &map_length, NULL, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04001249
Chris Masond3977122009-01-05 21:25:51 -05001250 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001251 return 1;
Chris Mason239b14b2008-03-24 15:02:07 -04001252 return 0;
1253}
1254
Chris Masond352ac62008-09-29 15:18:18 -04001255/*
1256 * in order to insert checksums into the metadata in large chunks,
1257 * we wait until bio submission time. All the pages in the bio are
1258 * checksummed and sums are attached onto the ordered extent record.
1259 *
1260 * At IO completion time the cums attached on the ordered extent record
1261 * are inserted into the btree
1262 */
Chris Masond3977122009-01-05 21:25:51 -05001263static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1264 struct bio *bio, int mirror_num,
1265 unsigned long bio_flags)
Chris Mason065631f2008-02-20 12:07:25 -05001266{
Chris Mason065631f2008-02-20 12:07:25 -05001267 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001268 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001269
Chris Masond20f7042008-12-08 16:58:54 -05001270 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Chris Mason44b8bd72008-04-16 11:14:51 -04001271 BUG_ON(ret);
Chris Mason4a69a412008-11-06 22:03:00 -05001272 return 0;
1273}
Chris Masone0156402008-04-16 11:15:20 -04001274
Chris Mason4a69a412008-11-06 22:03:00 -05001275/*
1276 * in order to insert checksums into the metadata in large chunks,
1277 * we wait until bio submission time. All the pages in the bio are
1278 * checksummed and sums are attached onto the ordered extent record.
1279 *
1280 * At IO completion time the cums attached on the ordered extent record
1281 * are inserted into the btree
1282 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001283static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Mason4a69a412008-11-06 22:03:00 -05001284 int mirror_num, unsigned long bio_flags)
1285{
1286 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason8b712842008-06-11 16:50:36 -04001287 return btrfs_map_bio(root, rw, bio, mirror_num, 1);
Chris Mason44b8bd72008-04-16 11:14:51 -04001288}
1289
Chris Masond352ac62008-09-29 15:18:18 -04001290/*
Chris Masoncad321a2008-12-17 14:51:42 -05001291 * extent_io.c submission hook. This does the right thing for csum calculation
1292 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001293 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001294static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masonc8b97812008-10-29 14:49:59 -04001295 int mirror_num, unsigned long bio_flags)
Chris Mason44b8bd72008-04-16 11:14:51 -04001296{
1297 struct btrfs_root *root = BTRFS_I(inode)->root;
1298 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001299 int skip_sum;
Chris Mason44b8bd72008-04-16 11:14:51 -04001300
Chris Masoncad321a2008-12-17 14:51:42 -05001301 skip_sum = btrfs_test_flag(inode, NODATASUM);
1302
Chris Masone6dcd2d2008-07-17 12:53:50 -04001303 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
1304 BUG_ON(ret);
Chris Mason065631f2008-02-20 12:07:25 -05001305
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001306 if (!(rw & (1 << BIO_RW))) {
Chris Masond20f7042008-12-08 16:58:54 -05001307 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Chris Masonc8b97812008-10-29 14:49:59 -04001308 return btrfs_submit_compressed_read(inode, bio,
1309 mirror_num, bio_flags);
Chris Masond20f7042008-12-08 16:58:54 -05001310 } else if (!skip_sum)
1311 btrfs_lookup_bio_sums(root, inode, bio, NULL);
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001312 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001313 } else if (!skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001314 /* csum items have already been cloned */
1315 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1316 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001317 /* we're doing a write, do the async checksumming */
1318 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001319 inode, rw, bio, mirror_num,
Chris Mason4a69a412008-11-06 22:03:00 -05001320 bio_flags, __btrfs_submit_bio_start,
1321 __btrfs_submit_bio_done);
Chris Mason19b9bdb2008-10-30 14:23:13 -04001322 }
1323
Chris Mason0b86a832008-03-24 15:01:56 -04001324mapit:
Chris Mason8b712842008-06-11 16:50:36 -04001325 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
Chris Mason065631f2008-02-20 12:07:25 -05001326}
Chris Mason6885f302008-02-20 16:11:05 -05001327
Chris Masond352ac62008-09-29 15:18:18 -04001328/*
1329 * given a list of ordered sums record them in the inode. This happens
1330 * at IO completion time based on sums calculated at bio submission time.
1331 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001332static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001333 struct inode *inode, u64 file_offset,
1334 struct list_head *list)
1335{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001336 struct btrfs_ordered_sum *sum;
1337
1338 btrfs_set_trans_block_group(trans, inode);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001339
1340 list_for_each_entry(sum, list, list) {
Chris Masond20f7042008-12-08 16:58:54 -05001341 btrfs_csum_file_blocks(trans,
1342 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001343 }
1344 return 0;
1345}
1346
Chris Masonea8c2812008-08-04 23:17:27 -04001347int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end)
1348{
Chris Masond3977122009-01-05 21:25:51 -05001349 if ((end & (PAGE_CACHE_SIZE - 1)) == 0)
Chris Mason771ed682008-11-06 22:02:51 -05001350 WARN_ON(1);
Chris Masonea8c2812008-08-04 23:17:27 -04001351 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
1352 GFP_NOFS);
1353}
1354
Chris Masond352ac62008-09-29 15:18:18 -04001355/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001356struct btrfs_writepage_fixup {
1357 struct page *page;
1358 struct btrfs_work work;
1359};
1360
Christoph Hellwigb2950862008-12-02 09:54:17 -05001361static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001362{
1363 struct btrfs_writepage_fixup *fixup;
1364 struct btrfs_ordered_extent *ordered;
1365 struct page *page;
1366 struct inode *inode;
1367 u64 page_start;
1368 u64 page_end;
1369
1370 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1371 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001372again:
Chris Mason247e7432008-07-17 12:53:51 -04001373 lock_page(page);
1374 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1375 ClearPageChecked(page);
1376 goto out_page;
1377 }
1378
1379 inode = page->mapping->host;
1380 page_start = page_offset(page);
1381 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1382
1383 lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001384
1385 /* already ordered? We're done */
1386 if (test_range_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
1387 EXTENT_ORDERED, 0)) {
Chris Mason247e7432008-07-17 12:53:51 -04001388 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001389 }
1390
1391 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1392 if (ordered) {
1393 unlock_extent(&BTRFS_I(inode)->io_tree, page_start,
1394 page_end, GFP_NOFS);
1395 unlock_page(page);
1396 btrfs_start_ordered_extent(inode, ordered, 1);
1397 goto again;
1398 }
Chris Mason247e7432008-07-17 12:53:51 -04001399
Chris Masonea8c2812008-08-04 23:17:27 -04001400 btrfs_set_extent_delalloc(inode, page_start, page_end);
Chris Mason247e7432008-07-17 12:53:51 -04001401 ClearPageChecked(page);
1402out:
1403 unlock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
1404out_page:
1405 unlock_page(page);
1406 page_cache_release(page);
1407}
1408
1409/*
1410 * There are a few paths in the higher layers of the kernel that directly
1411 * set the page dirty bit without asking the filesystem if it is a
1412 * good idea. This causes problems because we want to make sure COW
1413 * properly happens and the data=ordered rules are followed.
1414 *
Chris Masonc8b97812008-10-29 14:49:59 -04001415 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001416 * hasn't been properly setup for IO. We kick off an async process
1417 * to fix it up. The async helper will wait for ordered extents, set
1418 * the delalloc bit and make it safe to write the page.
1419 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001420static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001421{
1422 struct inode *inode = page->mapping->host;
1423 struct btrfs_writepage_fixup *fixup;
1424 struct btrfs_root *root = BTRFS_I(inode)->root;
1425 int ret;
1426
1427 ret = test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1428 EXTENT_ORDERED, 0);
1429 if (ret)
1430 return 0;
1431
1432 if (PageChecked(page))
1433 return -EAGAIN;
1434
1435 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1436 if (!fixup)
1437 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001438
Chris Mason247e7432008-07-17 12:53:51 -04001439 SetPageChecked(page);
1440 page_cache_get(page);
1441 fixup->work.func = btrfs_writepage_fixup_worker;
1442 fixup->page = page;
1443 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
1444 return -EAGAIN;
1445}
1446
Yan Zhengd899e052008-10-30 14:25:28 -04001447static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1448 struct inode *inode, u64 file_pos,
1449 u64 disk_bytenr, u64 disk_num_bytes,
1450 u64 num_bytes, u64 ram_bytes,
1451 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
1465 ret = btrfs_drop_extents(trans, root, inode, file_pos,
1466 file_pos + num_bytes, file_pos, &hint);
1467 BUG_ON(ret);
1468
1469 ins.objectid = inode->i_ino;
1470 ins.offset = file_pos;
1471 ins.type = BTRFS_EXTENT_DATA_KEY;
1472 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
1473 BUG_ON(ret);
1474 leaf = path->nodes[0];
1475 fi = btrfs_item_ptr(leaf, path->slots[0],
1476 struct btrfs_file_extent_item);
1477 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1478 btrfs_set_file_extent_type(leaf, fi, extent_type);
1479 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1480 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1481 btrfs_set_file_extent_offset(leaf, fi, 0);
1482 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1483 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1484 btrfs_set_file_extent_compression(leaf, fi, compression);
1485 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1486 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
1487 btrfs_mark_buffer_dirty(leaf);
1488
1489 inode_add_bytes(inode, num_bytes);
1490 btrfs_drop_extent_cache(inode, file_pos, file_pos + num_bytes - 1, 0);
1491
1492 ins.objectid = disk_bytenr;
1493 ins.offset = disk_num_bytes;
1494 ins.type = BTRFS_EXTENT_ITEM_KEY;
1495 ret = btrfs_alloc_reserved_extent(trans, root, leaf->start,
1496 root->root_key.objectid,
1497 trans->transid, inode->i_ino, &ins);
1498 BUG_ON(ret);
1499
1500 btrfs_free_path(path);
1501 return 0;
1502}
1503
Chris Masond352ac62008-09-29 15:18:18 -04001504/* as ordered data IO finishes, this gets called so we can finish
1505 * an ordered extent if the range of bytes in the file it covers are
1506 * fully written.
1507 */
Chris Mason211f90e2008-07-18 11:56:15 -04001508static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001509{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001510 struct btrfs_root *root = BTRFS_I(inode)->root;
1511 struct btrfs_trans_handle *trans;
1512 struct btrfs_ordered_extent *ordered_extent;
1513 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan Zhengd899e052008-10-30 14:25:28 -04001514 int compressed = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001515 int ret;
1516
1517 ret = btrfs_dec_test_ordered_pending(inode, start, end - start + 1);
Chris Masonba1da2f2008-07-17 12:54:15 -04001518 if (!ret)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001519 return 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001520
Chris Masonf9295742008-07-17 12:54:14 -04001521 trans = btrfs_join_transaction(root, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001522
1523 ordered_extent = btrfs_lookup_ordered_extent(inode, start);
1524 BUG_ON(!ordered_extent);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001525 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags))
1526 goto nocow;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001527
1528 lock_extent(io_tree, ordered_extent->file_offset,
1529 ordered_extent->file_offset + ordered_extent->len - 1,
1530 GFP_NOFS);
1531
Chris Masonc8b97812008-10-29 14:49:59 -04001532 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Yan Zhengd899e052008-10-30 14:25:28 -04001533 compressed = 1;
1534 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
1535 BUG_ON(compressed);
1536 ret = btrfs_mark_extent_written(trans, root, inode,
1537 ordered_extent->file_offset,
1538 ordered_extent->file_offset +
1539 ordered_extent->len);
1540 BUG_ON(ret);
1541 } else {
1542 ret = insert_reserved_file_extent(trans, inode,
1543 ordered_extent->file_offset,
1544 ordered_extent->start,
1545 ordered_extent->disk_len,
1546 ordered_extent->len,
1547 ordered_extent->len,
1548 compressed, 0, 0,
1549 BTRFS_FILE_EXTENT_REG);
1550 BUG_ON(ret);
1551 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04001552 unlock_extent(io_tree, ordered_extent->file_offset,
1553 ordered_extent->file_offset + ordered_extent->len - 1,
1554 GFP_NOFS);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001555nocow:
Chris Masone6dcd2d2008-07-17 12:53:50 -04001556 add_pending_csums(trans, inode, ordered_extent->file_offset,
1557 &ordered_extent->list);
1558
Chris Mason34353022008-09-23 20:19:49 -04001559 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Chris Masondbe674a2008-07-17 12:54:05 -04001560 btrfs_ordered_update_i_size(inode, ordered_extent);
Chris Masone02119d2008-09-05 16:13:11 -04001561 btrfs_update_inode(trans, root, inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001562 btrfs_remove_ordered_extent(inode, ordered_extent);
Chris Mason34353022008-09-23 20:19:49 -04001563 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
Chris Mason7f3c74f2008-07-18 12:01:11 -04001564
Chris Masone6dcd2d2008-07-17 12:53:50 -04001565 /* once for us */
1566 btrfs_put_ordered_extent(ordered_extent);
1567 /* once for the tree */
1568 btrfs_put_ordered_extent(ordered_extent);
1569
Chris Masone6dcd2d2008-07-17 12:53:50 -04001570 btrfs_end_transaction(trans, root);
1571 return 0;
1572}
1573
Christoph Hellwigb2950862008-12-02 09:54:17 -05001574static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04001575 struct extent_state *state, int uptodate)
1576{
1577 return btrfs_finish_ordered_io(page->mapping->host, start, end);
1578}
1579
Chris Masond352ac62008-09-29 15:18:18 -04001580/*
1581 * When IO fails, either with EIO or csum verification fails, we
1582 * try other mirrors that might have a good copy of the data. This
1583 * io_failure_record is used to record state as we go through all the
1584 * mirrors. If another mirror has good data, the page is set up to date
1585 * and things continue. If a good mirror can't be found, the original
1586 * bio end_io callback is called to indicate things have failed.
1587 */
Chris Mason7e383262008-04-09 16:28:12 -04001588struct io_failure_record {
1589 struct page *page;
1590 u64 start;
1591 u64 len;
1592 u64 logical;
Chris Masond20f7042008-12-08 16:58:54 -05001593 unsigned long bio_flags;
Chris Mason7e383262008-04-09 16:28:12 -04001594 int last_mirror;
1595};
1596
Christoph Hellwigb2950862008-12-02 09:54:17 -05001597static int btrfs_io_failed_hook(struct bio *failed_bio,
Chris Mason1259ab72008-05-12 13:39:03 -04001598 struct page *page, u64 start, u64 end,
1599 struct extent_state *state)
Chris Mason7e383262008-04-09 16:28:12 -04001600{
1601 struct io_failure_record *failrec = NULL;
1602 u64 private;
1603 struct extent_map *em;
1604 struct inode *inode = page->mapping->host;
1605 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Chris Mason3b951512008-04-17 11:29:12 -04001606 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason7e383262008-04-09 16:28:12 -04001607 struct bio *bio;
1608 int num_copies;
1609 int ret;
Chris Mason1259ab72008-05-12 13:39:03 -04001610 int rw;
Chris Mason7e383262008-04-09 16:28:12 -04001611 u64 logical;
1612
1613 ret = get_state_private(failure_tree, start, &private);
1614 if (ret) {
Chris Mason7e383262008-04-09 16:28:12 -04001615 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
1616 if (!failrec)
1617 return -ENOMEM;
1618 failrec->start = start;
1619 failrec->len = end - start + 1;
1620 failrec->last_mirror = 0;
Chris Masond20f7042008-12-08 16:58:54 -05001621 failrec->bio_flags = 0;
Chris Mason7e383262008-04-09 16:28:12 -04001622
Chris Mason3b951512008-04-17 11:29:12 -04001623 spin_lock(&em_tree->lock);
1624 em = lookup_extent_mapping(em_tree, start, failrec->len);
1625 if (em->start > start || em->start + em->len < start) {
1626 free_extent_map(em);
1627 em = NULL;
1628 }
1629 spin_unlock(&em_tree->lock);
Chris Mason7e383262008-04-09 16:28:12 -04001630
1631 if (!em || IS_ERR(em)) {
1632 kfree(failrec);
1633 return -EIO;
1634 }
1635 logical = start - em->start;
1636 logical = em->block_start + logical;
Chris Masond20f7042008-12-08 16:58:54 -05001637 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
1638 logical = em->block_start;
1639 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
1640 }
Chris Mason7e383262008-04-09 16:28:12 -04001641 failrec->logical = logical;
1642 free_extent_map(em);
1643 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
1644 EXTENT_DIRTY, GFP_NOFS);
Chris Mason587f7702008-04-11 12:16:46 -04001645 set_state_private(failure_tree, start,
1646 (u64)(unsigned long)failrec);
Chris Mason7e383262008-04-09 16:28:12 -04001647 } else {
Chris Mason587f7702008-04-11 12:16:46 -04001648 failrec = (struct io_failure_record *)(unsigned long)private;
Chris Mason7e383262008-04-09 16:28:12 -04001649 }
1650 num_copies = btrfs_num_copies(
1651 &BTRFS_I(inode)->root->fs_info->mapping_tree,
1652 failrec->logical, failrec->len);
1653 failrec->last_mirror++;
1654 if (!state) {
Chris Masoncad321a2008-12-17 14:51:42 -05001655 spin_lock(&BTRFS_I(inode)->io_tree.lock);
Chris Mason7e383262008-04-09 16:28:12 -04001656 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
1657 failrec->start,
1658 EXTENT_LOCKED);
1659 if (state && state->start != failrec->start)
1660 state = NULL;
Chris Masoncad321a2008-12-17 14:51:42 -05001661 spin_unlock(&BTRFS_I(inode)->io_tree.lock);
Chris Mason7e383262008-04-09 16:28:12 -04001662 }
1663 if (!state || failrec->last_mirror > num_copies) {
1664 set_state_private(failure_tree, failrec->start, 0);
1665 clear_extent_bits(failure_tree, failrec->start,
1666 failrec->start + failrec->len - 1,
1667 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
1668 kfree(failrec);
1669 return -EIO;
1670 }
1671 bio = bio_alloc(GFP_NOFS, 1);
1672 bio->bi_private = state;
1673 bio->bi_end_io = failed_bio->bi_end_io;
1674 bio->bi_sector = failrec->logical >> 9;
1675 bio->bi_bdev = failed_bio->bi_bdev;
Chris Masone1c4b742008-04-22 13:26:46 -04001676 bio->bi_size = 0;
Chris Masond20f7042008-12-08 16:58:54 -05001677
Chris Mason7e383262008-04-09 16:28:12 -04001678 bio_add_page(bio, page, failrec->len, start - page_offset(page));
Chris Mason1259ab72008-05-12 13:39:03 -04001679 if (failed_bio->bi_rw & (1 << BIO_RW))
1680 rw = WRITE;
1681 else
1682 rw = READ;
1683
1684 BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
Chris Masonc8b97812008-10-29 14:49:59 -04001685 failrec->last_mirror,
Chris Masond20f7042008-12-08 16:58:54 -05001686 failrec->bio_flags);
Chris Mason1259ab72008-05-12 13:39:03 -04001687 return 0;
1688}
1689
Chris Masond352ac62008-09-29 15:18:18 -04001690/*
1691 * each time an IO finishes, we do a fast check in the IO failure tree
1692 * to see if we need to process or clean up an io_failure_record
1693 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001694static int btrfs_clean_io_failures(struct inode *inode, u64 start)
Chris Mason1259ab72008-05-12 13:39:03 -04001695{
1696 u64 private;
1697 u64 private_failure;
1698 struct io_failure_record *failure;
1699 int ret;
1700
1701 private = 0;
1702 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
1703 (u64)-1, 1, EXTENT_DIRTY)) {
1704 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
1705 start, &private_failure);
1706 if (ret == 0) {
1707 failure = (struct io_failure_record *)(unsigned long)
1708 private_failure;
1709 set_state_private(&BTRFS_I(inode)->io_failure_tree,
1710 failure->start, 0);
1711 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
1712 failure->start,
1713 failure->start + failure->len - 1,
1714 EXTENT_DIRTY | EXTENT_LOCKED,
1715 GFP_NOFS);
1716 kfree(failure);
1717 }
1718 }
Chris Mason7e383262008-04-09 16:28:12 -04001719 return 0;
1720}
1721
Chris Masond352ac62008-09-29 15:18:18 -04001722/*
1723 * when reads are done, we need to check csums to verify the data is correct
1724 * if there's a match, we allow the bio to finish. If not, we go through
1725 * the io_failure_record routines to find good copies
1726 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001727static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason70dec802008-01-29 09:59:12 -05001728 struct extent_state *state)
Chris Mason07157aa2007-08-30 08:50:51 -04001729{
Chris Mason35ebb932007-10-30 16:56:53 -04001730 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -04001731 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05001732 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04001733 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -05001734 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -04001735 int ret;
Chris Masonff79f812007-10-15 16:22:25 -04001736 struct btrfs_root *root = BTRFS_I(inode)->root;
1737 u32 csum = ~(u32)0;
Chris Masond1310b22008-01-24 16:13:08 -05001738
Chris Masond20f7042008-12-08 16:58:54 -05001739 if (PageChecked(page)) {
1740 ClearPageChecked(page);
1741 goto good;
1742 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001743 if (btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -05001744 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05001745
Yan Zheng17d217f2008-12-12 10:03:38 -05001746 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
1747 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1)) {
1748 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
1749 GFP_NOFS);
1750 return 0;
1751 }
1752
Yanc2e639f2008-02-04 08:57:25 -05001753 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -05001754 private = state->private;
1755 ret = 0;
1756 } else {
1757 ret = get_state_private(io_tree, start, &private);
1758 }
Chris Mason9ab86c82009-01-07 09:48:51 -05001759 kaddr = kmap_atomic(page, KM_USER0);
Chris Masond3977122009-01-05 21:25:51 -05001760 if (ret)
Chris Mason07157aa2007-08-30 08:50:51 -04001761 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05001762
Chris Masonff79f812007-10-15 16:22:25 -04001763 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
1764 btrfs_csum_final(csum, (char *)&csum);
Chris Masond3977122009-01-05 21:25:51 -05001765 if (csum != private)
Chris Mason07157aa2007-08-30 08:50:51 -04001766 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05001767
Chris Mason9ab86c82009-01-07 09:48:51 -05001768 kunmap_atomic(kaddr, KM_USER0);
Chris Masond20f7042008-12-08 16:58:54 -05001769good:
Chris Mason7e383262008-04-09 16:28:12 -04001770 /* if the io failure tree for this inode is non-empty,
1771 * check to see if we've recovered from a failed IO
1772 */
Chris Mason1259ab72008-05-12 13:39:03 -04001773 btrfs_clean_io_failures(inode, start);
Chris Mason07157aa2007-08-30 08:50:51 -04001774 return 0;
1775
1776zeroit:
Chris Masond3977122009-01-05 21:25:51 -05001777 printk(KERN_INFO "btrfs csum failed ino %lu off %llu csum %u "
1778 "private %llu\n", page->mapping->host->i_ino,
1779 (unsigned long long)start, csum,
1780 (unsigned long long)private);
Chris Masondb945352007-10-15 16:15:53 -04001781 memset(kaddr + offset, 1, end - start + 1);
1782 flush_dcache_page(page);
Chris Mason9ab86c82009-01-07 09:48:51 -05001783 kunmap_atomic(kaddr, KM_USER0);
Chris Mason3b951512008-04-17 11:29:12 -04001784 if (private == 0)
1785 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04001786 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04001787}
Chris Masonb888db22007-08-27 16:49:44 -04001788
Josef Bacik7b128762008-07-24 12:17:14 -04001789/*
1790 * This creates an orphan entry for the given inode in case something goes
1791 * wrong in the middle of an unlink/truncate.
1792 */
1793int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
1794{
1795 struct btrfs_root *root = BTRFS_I(inode)->root;
1796 int ret = 0;
1797
Yanbcc63ab2008-07-30 16:29:20 -04001798 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001799
1800 /* already on the orphan list, we're good */
1801 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
Yanbcc63ab2008-07-30 16:29:20 -04001802 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001803 return 0;
1804 }
1805
1806 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
1807
Yanbcc63ab2008-07-30 16:29:20 -04001808 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001809
1810 /*
1811 * insert an orphan item to track this unlinked/truncated file
1812 */
1813 ret = btrfs_insert_orphan_item(trans, root, inode->i_ino);
1814
1815 return ret;
1816}
1817
1818/*
1819 * We have done the truncate/delete so we can go ahead and remove the orphan
1820 * item for this particular inode.
1821 */
1822int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
1823{
1824 struct btrfs_root *root = BTRFS_I(inode)->root;
1825 int ret = 0;
1826
Yanbcc63ab2008-07-30 16:29:20 -04001827 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001828
1829 if (list_empty(&BTRFS_I(inode)->i_orphan)) {
Yanbcc63ab2008-07-30 16:29:20 -04001830 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001831 return 0;
1832 }
1833
1834 list_del_init(&BTRFS_I(inode)->i_orphan);
1835 if (!trans) {
Yanbcc63ab2008-07-30 16:29:20 -04001836 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001837 return 0;
1838 }
1839
Yanbcc63ab2008-07-30 16:29:20 -04001840 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001841
1842 ret = btrfs_del_orphan_item(trans, root, inode->i_ino);
1843
1844 return ret;
1845}
1846
1847/*
1848 * this cleans up any orphans that may be left on the list from the last use
1849 * of this root.
1850 */
1851void btrfs_orphan_cleanup(struct btrfs_root *root)
1852{
1853 struct btrfs_path *path;
1854 struct extent_buffer *leaf;
1855 struct btrfs_item *item;
1856 struct btrfs_key key, found_key;
1857 struct btrfs_trans_handle *trans;
1858 struct inode *inode;
1859 int ret = 0, nr_unlink = 0, nr_truncate = 0;
1860
Josef Bacik7b128762008-07-24 12:17:14 -04001861 path = btrfs_alloc_path();
1862 if (!path)
1863 return;
1864 path->reada = -1;
1865
1866 key.objectid = BTRFS_ORPHAN_OBJECTID;
1867 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
1868 key.offset = (u64)-1;
1869
Josef Bacik7b128762008-07-24 12:17:14 -04001870
1871 while (1) {
1872 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1873 if (ret < 0) {
1874 printk(KERN_ERR "Error searching slot for orphan: %d"
1875 "\n", ret);
1876 break;
1877 }
1878
1879 /*
1880 * if ret == 0 means we found what we were searching for, which
1881 * is weird, but possible, so only screw with path if we didnt
1882 * find the key and see if we have stuff that matches
1883 */
1884 if (ret > 0) {
1885 if (path->slots[0] == 0)
1886 break;
1887 path->slots[0]--;
1888 }
1889
1890 /* pull out the item */
1891 leaf = path->nodes[0];
1892 item = btrfs_item_nr(leaf, path->slots[0]);
1893 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1894
1895 /* make sure the item matches what we want */
1896 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
1897 break;
1898 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
1899 break;
1900
1901 /* release the path since we're done with it */
1902 btrfs_release_path(root, path);
1903
1904 /*
1905 * this is where we are basically btrfs_lookup, without the
1906 * crossing root thing. we store the inode number in the
1907 * offset of the orphan item.
1908 */
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001909 inode = btrfs_iget_locked(root->fs_info->sb,
Josef Bacik7b128762008-07-24 12:17:14 -04001910 found_key.offset, root);
1911 if (!inode)
1912 break;
1913
1914 if (inode->i_state & I_NEW) {
1915 BTRFS_I(inode)->root = root;
1916
1917 /* have to set the location manually */
1918 BTRFS_I(inode)->location.objectid = inode->i_ino;
1919 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
1920 BTRFS_I(inode)->location.offset = 0;
1921
1922 btrfs_read_locked_inode(inode);
1923 unlock_new_inode(inode);
1924 }
1925
1926 /*
1927 * add this inode to the orphan list so btrfs_orphan_del does
1928 * the proper thing when we hit it
1929 */
Yanbcc63ab2008-07-30 16:29:20 -04001930 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001931 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
Yanbcc63ab2008-07-30 16:29:20 -04001932 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001933
1934 /*
1935 * if this is a bad inode, means we actually succeeded in
1936 * removing the inode, but not the orphan record, which means
1937 * we need to manually delete the orphan since iput will just
1938 * do a destroy_inode
1939 */
1940 if (is_bad_inode(inode)) {
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001941 trans = btrfs_start_transaction(root, 1);
Josef Bacik7b128762008-07-24 12:17:14 -04001942 btrfs_orphan_del(trans, inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001943 btrfs_end_transaction(trans, root);
Josef Bacik7b128762008-07-24 12:17:14 -04001944 iput(inode);
1945 continue;
1946 }
1947
1948 /* if we have links, this was a truncate, lets do that */
1949 if (inode->i_nlink) {
1950 nr_truncate++;
1951 btrfs_truncate(inode);
1952 } else {
1953 nr_unlink++;
1954 }
1955
1956 /* this will do delete_inode and everything for us */
1957 iput(inode);
1958 }
1959
1960 if (nr_unlink)
1961 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
1962 if (nr_truncate)
1963 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
1964
1965 btrfs_free_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04001966}
1967
Chris Masond352ac62008-09-29 15:18:18 -04001968/*
1969 * read an inode from the btree into the in-memory inode
1970 */
Chris Mason39279cc2007-06-12 06:35:45 -04001971void btrfs_read_locked_inode(struct inode *inode)
1972{
1973 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04001974 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001975 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04001976 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04001977 struct btrfs_root *root = BTRFS_I(inode)->root;
1978 struct btrfs_key location;
1979 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -04001980 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04001981 int ret;
1982
1983 path = btrfs_alloc_path();
1984 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -04001985 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05001986
Chris Mason39279cc2007-06-12 06:35:45 -04001987 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001988 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04001989 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04001990
Chris Mason5f39d392007-10-15 16:14:19 -04001991 leaf = path->nodes[0];
1992 inode_item = btrfs_item_ptr(leaf, path->slots[0],
1993 struct btrfs_inode_item);
1994
1995 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
1996 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
1997 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
1998 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
Chris Masondbe674a2008-07-17 12:54:05 -04001999 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04002000
2001 tspec = btrfs_inode_atime(inode_item);
2002 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2003 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2004
2005 tspec = btrfs_inode_mtime(inode_item);
2006 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2007 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2008
2009 tspec = btrfs_inode_ctime(inode_item);
2010 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2011 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2012
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002013 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04002014 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Chris Masonc3027eb2008-12-08 16:40:21 -05002015 BTRFS_I(inode)->sequence = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04002016 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04002017 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002018 rdev = btrfs_inode_rdev(leaf, inode_item);
2019
Josef Bacikaec74772008-07-24 12:12:38 -04002020 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05002021 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Josef Bacikaec74772008-07-24 12:12:38 -04002022
Chris Mason5f39d392007-10-15 16:14:19 -04002023 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002024 BTRFS_I(inode)->block_group = btrfs_find_block_group(root, 0,
2025 alloc_group_block, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002026 btrfs_free_path(path);
2027 inode_item = NULL;
2028
Chris Mason39279cc2007-06-12 06:35:45 -04002029 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04002030 case S_IFREG:
2031 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04002032 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05002033 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002034 inode->i_fop = &btrfs_file_operations;
2035 inode->i_op = &btrfs_file_inode_operations;
2036 break;
2037 case S_IFDIR:
2038 inode->i_fop = &btrfs_dir_file_operations;
2039 if (root == root->fs_info->tree_root)
2040 inode->i_op = &btrfs_dir_ro_inode_operations;
2041 else
2042 inode->i_op = &btrfs_dir_inode_operations;
2043 break;
2044 case S_IFLNK:
2045 inode->i_op = &btrfs_symlink_inode_operations;
2046 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04002047 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04002048 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04002049 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05002050 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04002051 init_special_inode(inode, inode->i_mode, rdev);
2052 break;
Chris Mason39279cc2007-06-12 06:35:45 -04002053 }
2054 return;
2055
2056make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04002057 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002058 make_bad_inode(inode);
2059}
2060
Chris Masond352ac62008-09-29 15:18:18 -04002061/*
2062 * given a leaf and an inode, copy the inode fields into the leaf
2063 */
Chris Masone02119d2008-09-05 16:13:11 -04002064static void fill_inode_item(struct btrfs_trans_handle *trans,
2065 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04002066 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04002067 struct inode *inode)
2068{
Chris Mason5f39d392007-10-15 16:14:19 -04002069 btrfs_set_inode_uid(leaf, item, inode->i_uid);
2070 btrfs_set_inode_gid(leaf, item, inode->i_gid);
Chris Masondbe674a2008-07-17 12:54:05 -04002071 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
Chris Mason5f39d392007-10-15 16:14:19 -04002072 btrfs_set_inode_mode(leaf, item, inode->i_mode);
2073 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
2074
2075 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
2076 inode->i_atime.tv_sec);
2077 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
2078 inode->i_atime.tv_nsec);
2079
2080 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
2081 inode->i_mtime.tv_sec);
2082 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
2083 inode->i_mtime.tv_nsec);
2084
2085 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
2086 inode->i_ctime.tv_sec);
2087 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
2088 inode->i_ctime.tv_nsec);
2089
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002090 btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode));
Chris Masone02119d2008-09-05 16:13:11 -04002091 btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
Chris Masonc3027eb2008-12-08 16:40:21 -05002092 btrfs_set_inode_sequence(leaf, item, BTRFS_I(inode)->sequence);
Chris Masone02119d2008-09-05 16:13:11 -04002093 btrfs_set_inode_transid(leaf, item, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002094 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -05002095 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002096 btrfs_set_inode_block_group(leaf, item, BTRFS_I(inode)->block_group);
Chris Mason39279cc2007-06-12 06:35:45 -04002097}
2098
Chris Masond352ac62008-09-29 15:18:18 -04002099/*
2100 * copy everything in the in-memory inode into the btree.
2101 */
Chris Masond3977122009-01-05 21:25:51 -05002102noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
2103 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002104{
2105 struct btrfs_inode_item *inode_item;
2106 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002107 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002108 int ret;
2109
2110 path = btrfs_alloc_path();
2111 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -04002112 ret = btrfs_lookup_inode(trans, root, path,
2113 &BTRFS_I(inode)->location, 1);
2114 if (ret) {
2115 if (ret > 0)
2116 ret = -ENOENT;
2117 goto failed;
2118 }
2119
Chris Mason5f39d392007-10-15 16:14:19 -04002120 leaf = path->nodes[0];
2121 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04002122 struct btrfs_inode_item);
2123
Chris Masone02119d2008-09-05 16:13:11 -04002124 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002125 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002126 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002127 ret = 0;
2128failed:
Chris Mason39279cc2007-06-12 06:35:45 -04002129 btrfs_free_path(path);
2130 return ret;
2131}
2132
2133
Chris Masond352ac62008-09-29 15:18:18 -04002134/*
2135 * unlink helper that gets used here in inode.c and in the tree logging
2136 * recovery code. It remove a link in a directory with a given name, and
2137 * also drops the back refs in the inode to the directory
2138 */
Chris Masone02119d2008-09-05 16:13:11 -04002139int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2140 struct btrfs_root *root,
2141 struct inode *dir, struct inode *inode,
2142 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04002143{
2144 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04002145 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002146 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002147 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04002148 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04002149 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04002150
2151 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04002152 if (!path) {
2153 ret = -ENOMEM;
2154 goto err;
2155 }
2156
Chris Mason39279cc2007-06-12 06:35:45 -04002157 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
2158 name, name_len, -1);
2159 if (IS_ERR(di)) {
2160 ret = PTR_ERR(di);
2161 goto err;
2162 }
2163 if (!di) {
2164 ret = -ENOENT;
2165 goto err;
2166 }
Chris Mason5f39d392007-10-15 16:14:19 -04002167 leaf = path->nodes[0];
2168 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04002169 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04002170 if (ret)
2171 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -04002172 btrfs_release_path(root, path);
2173
Josef Bacikaec74772008-07-24 12:12:38 -04002174 ret = btrfs_del_inode_ref(trans, root, name, name_len,
Chris Masone02119d2008-09-05 16:13:11 -04002175 inode->i_ino,
2176 dir->i_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04002177 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05002178 printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
Josef Bacikaec74772008-07-24 12:12:38 -04002179 "inode %lu parent %lu\n", name_len, name,
Chris Masone02119d2008-09-05 16:13:11 -04002180 inode->i_ino, dir->i_ino);
Josef Bacikaec74772008-07-24 12:12:38 -04002181 goto err;
2182 }
2183
Chris Mason39279cc2007-06-12 06:35:45 -04002184 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04002185 index, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -04002186 if (IS_ERR(di)) {
2187 ret = PTR_ERR(di);
2188 goto err;
2189 }
2190 if (!di) {
2191 ret = -ENOENT;
2192 goto err;
2193 }
2194 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason925baed2008-06-25 16:01:30 -04002195 btrfs_release_path(root, path);
Chris Mason39279cc2007-06-12 06:35:45 -04002196
Chris Masone02119d2008-09-05 16:13:11 -04002197 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
2198 inode, dir->i_ino);
Chris Mason49eb7e42008-09-11 15:53:12 -04002199 BUG_ON(ret != 0 && ret != -ENOENT);
2200 if (ret != -ENOENT)
2201 BTRFS_I(dir)->log_dirty_trans = trans->transid;
Chris Masone02119d2008-09-05 16:13:11 -04002202
2203 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
2204 dir, index);
2205 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04002206err:
2207 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002208 if (ret)
2209 goto out;
2210
2211 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
2212 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2213 btrfs_update_inode(trans, root, dir);
2214 btrfs_drop_nlink(inode);
2215 ret = btrfs_update_inode(trans, root, inode);
2216 dir->i_sb->s_dirt = 1;
2217out:
Chris Mason39279cc2007-06-12 06:35:45 -04002218 return ret;
2219}
2220
2221static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
2222{
2223 struct btrfs_root *root;
2224 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04002225 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002226 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -05002227 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002228
2229 root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05002230
2231 ret = btrfs_check_free_space(root, 1, 1);
2232 if (ret)
2233 goto fail;
2234
Chris Mason39279cc2007-06-12 06:35:45 -04002235 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002236
Chris Mason39279cc2007-06-12 06:35:45 -04002237 btrfs_set_trans_block_group(trans, dir);
Chris Masone02119d2008-09-05 16:13:11 -04002238 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
2239 dentry->d_name.name, dentry->d_name.len);
Josef Bacik7b128762008-07-24 12:17:14 -04002240
2241 if (inode->i_nlink == 0)
2242 ret = btrfs_orphan_add(trans, inode);
2243
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002244 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04002245
Chris Mason89ce8a62008-06-25 16:01:31 -04002246 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002247fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002248 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002249 return ret;
2250}
2251
2252static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
2253{
2254 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002255 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002256 int ret;
2257 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04002258 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -05002259 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002260
Chris Mason3394e162008-11-17 20:42:26 -05002261 /*
2262 * the FIRST_FREE_OBJECTID check makes sure we don't try to rmdir
2263 * the root of a subvolume or snapshot
2264 */
2265 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE ||
2266 inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) {
Yan134d4512007-10-25 15:49:25 -04002267 return -ENOTEMPTY;
Chris Mason925baed2008-06-25 16:01:30 -04002268 }
Yan134d4512007-10-25 15:49:25 -04002269
Chris Mason1832a6d2007-12-21 16:27:21 -05002270 ret = btrfs_check_free_space(root, 1, 1);
2271 if (ret)
2272 goto fail;
2273
Chris Mason39279cc2007-06-12 06:35:45 -04002274 trans = btrfs_start_transaction(root, 1);
2275 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -04002276
Josef Bacik7b128762008-07-24 12:17:14 -04002277 err = btrfs_orphan_add(trans, inode);
2278 if (err)
2279 goto fail_trans;
2280
Chris Mason39279cc2007-06-12 06:35:45 -04002281 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04002282 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
2283 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05002284 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04002285 btrfs_i_size_write(inode, 0);
Chris Mason39544012007-12-12 14:38:19 -05002286
Josef Bacik7b128762008-07-24 12:17:14 -04002287fail_trans:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002288 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04002289 ret = btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002290fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002291 btrfs_btree_balance_dirty(root, nr);
Chris Mason39544012007-12-12 14:38:19 -05002292
Chris Mason39279cc2007-06-12 06:35:45 -04002293 if (ret && !err)
2294 err = ret;
2295 return err;
2296}
2297
Chris Masond20f7042008-12-08 16:58:54 -05002298#if 0
Chris Mason39279cc2007-06-12 06:35:45 -04002299/*
Chris Mason323ac952008-10-01 19:05:46 -04002300 * when truncating bytes in a file, it is possible to avoid reading
2301 * the leaves that contain only checksum items. This can be the
2302 * majority of the IO required to delete a large file, but it must
2303 * be done carefully.
2304 *
2305 * The keys in the level just above the leaves are checked to make sure
2306 * the lowest key in a given leaf is a csum key, and starts at an offset
2307 * after the new size.
2308 *
2309 * Then the key for the next leaf is checked to make sure it also has
2310 * a checksum item for the same file. If it does, we know our target leaf
2311 * contains only checksum items, and it can be safely freed without reading
2312 * it.
2313 *
2314 * This is just an optimization targeted at large files. It may do
2315 * nothing. It will return 0 unless things went badly.
2316 */
2317static noinline int drop_csum_leaves(struct btrfs_trans_handle *trans,
2318 struct btrfs_root *root,
2319 struct btrfs_path *path,
2320 struct inode *inode, u64 new_size)
2321{
2322 struct btrfs_key key;
2323 int ret;
2324 int nritems;
2325 struct btrfs_key found_key;
2326 struct btrfs_key other_key;
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002327 struct btrfs_leaf_ref *ref;
2328 u64 leaf_gen;
2329 u64 leaf_start;
Chris Mason323ac952008-10-01 19:05:46 -04002330
2331 path->lowest_level = 1;
2332 key.objectid = inode->i_ino;
2333 key.type = BTRFS_CSUM_ITEM_KEY;
2334 key.offset = new_size;
2335again:
2336 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2337 if (ret < 0)
2338 goto out;
2339
2340 if (path->nodes[1] == NULL) {
2341 ret = 0;
2342 goto out;
2343 }
2344 ret = 0;
2345 btrfs_node_key_to_cpu(path->nodes[1], &found_key, path->slots[1]);
2346 nritems = btrfs_header_nritems(path->nodes[1]);
2347
2348 if (!nritems)
2349 goto out;
2350
2351 if (path->slots[1] >= nritems)
2352 goto next_node;
2353
2354 /* did we find a key greater than anything we want to delete? */
2355 if (found_key.objectid > inode->i_ino ||
2356 (found_key.objectid == inode->i_ino && found_key.type > key.type))
2357 goto out;
2358
2359 /* we check the next key in the node to make sure the leave contains
2360 * only checksum items. This comparison doesn't work if our
2361 * leaf is the last one in the node
2362 */
2363 if (path->slots[1] + 1 >= nritems) {
2364next_node:
2365 /* search forward from the last key in the node, this
2366 * will bring us into the next node in the tree
2367 */
2368 btrfs_node_key_to_cpu(path->nodes[1], &found_key, nritems - 1);
2369
2370 /* unlikely, but we inc below, so check to be safe */
2371 if (found_key.offset == (u64)-1)
2372 goto out;
2373
2374 /* search_forward needs a path with locks held, do the
2375 * search again for the original key. It is possible
2376 * this will race with a balance and return a path that
2377 * we could modify, but this drop is just an optimization
2378 * and is allowed to miss some leaves.
2379 */
2380 btrfs_release_path(root, path);
2381 found_key.offset++;
2382
2383 /* setup a max key for search_forward */
2384 other_key.offset = (u64)-1;
2385 other_key.type = key.type;
2386 other_key.objectid = key.objectid;
2387
2388 path->keep_locks = 1;
2389 ret = btrfs_search_forward(root, &found_key, &other_key,
2390 path, 0, 0);
2391 path->keep_locks = 0;
2392 if (ret || found_key.objectid != key.objectid ||
2393 found_key.type != key.type) {
2394 ret = 0;
2395 goto out;
2396 }
2397
2398 key.offset = found_key.offset;
2399 btrfs_release_path(root, path);
2400 cond_resched();
2401 goto again;
2402 }
2403
2404 /* we know there's one more slot after us in the tree,
2405 * read that key so we can verify it is also a checksum item
2406 */
2407 btrfs_node_key_to_cpu(path->nodes[1], &other_key, path->slots[1] + 1);
2408
2409 if (found_key.objectid < inode->i_ino)
2410 goto next_key;
2411
2412 if (found_key.type != key.type || found_key.offset < new_size)
2413 goto next_key;
2414
2415 /*
2416 * if the key for the next leaf isn't a csum key from this objectid,
2417 * we can't be sure there aren't good items inside this leaf.
2418 * Bail out
2419 */
2420 if (other_key.objectid != inode->i_ino || other_key.type != key.type)
2421 goto out;
2422
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002423 leaf_start = btrfs_node_blockptr(path->nodes[1], path->slots[1]);
2424 leaf_gen = btrfs_node_ptr_generation(path->nodes[1], path->slots[1]);
Chris Mason323ac952008-10-01 19:05:46 -04002425 /*
2426 * it is safe to delete this leaf, it contains only
2427 * csum items from this inode at an offset >= new_size
2428 */
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002429 ret = btrfs_del_leaf(trans, root, path, leaf_start);
Chris Mason323ac952008-10-01 19:05:46 -04002430 BUG_ON(ret);
2431
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002432 if (root->ref_cows && leaf_gen < trans->transid) {
2433 ref = btrfs_alloc_leaf_ref(root, 0);
2434 if (ref) {
2435 ref->root_gen = root->root_key.offset;
2436 ref->bytenr = leaf_start;
2437 ref->owner = 0;
2438 ref->generation = leaf_gen;
2439 ref->nritems = 0;
2440
2441 ret = btrfs_add_leaf_ref(root, ref, 0);
2442 WARN_ON(ret);
2443 btrfs_free_leaf_ref(root, ref);
2444 } else {
2445 WARN_ON(1);
2446 }
2447 }
Chris Mason323ac952008-10-01 19:05:46 -04002448next_key:
2449 btrfs_release_path(root, path);
2450
2451 if (other_key.objectid == inode->i_ino &&
2452 other_key.type == key.type && other_key.offset > key.offset) {
2453 key.offset = other_key.offset;
2454 cond_resched();
2455 goto again;
2456 }
2457 ret = 0;
2458out:
2459 /* fixup any changes we've made to the path */
2460 path->lowest_level = 0;
2461 path->keep_locks = 0;
2462 btrfs_release_path(root, path);
2463 return ret;
2464}
2465
Chris Masond20f7042008-12-08 16:58:54 -05002466#endif
2467
Chris Mason323ac952008-10-01 19:05:46 -04002468/*
Chris Mason39279cc2007-06-12 06:35:45 -04002469 * this can truncate away extent items, csum items and directory items.
2470 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04002471 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04002472 *
2473 * csum items that cross the new i_size are truncated to the new size
2474 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04002475 *
2476 * min_type is the minimum key type to truncate down to. If set to 0, this
2477 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04002478 */
Chris Masone02119d2008-09-05 16:13:11 -04002479noinline int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
2480 struct btrfs_root *root,
2481 struct inode *inode,
2482 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04002483{
2484 int ret;
2485 struct btrfs_path *path;
2486 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04002487 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04002488 u32 found_type;
Chris Mason5f39d392007-10-15 16:14:19 -04002489 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002490 struct btrfs_file_extent_item *fi;
2491 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04002492 u64 extent_num_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002493 u64 item_end = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05002494 u64 root_gen = 0;
Chris Masond8d5f3e2007-12-11 12:42:00 -05002495 u64 root_owner = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002496 int found_extent;
2497 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05002498 int pending_del_nr = 0;
2499 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04002500 int extent_type = -1;
Chris Mason771ed682008-11-06 22:02:51 -05002501 int encoding;
Chris Mason3b951512008-04-17 11:29:12 -04002502 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002503
Chris Masone02119d2008-09-05 16:13:11 -04002504 if (root->ref_cows)
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002505 btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002506 path = btrfs_alloc_path();
Chris Mason3c69fae2007-08-07 15:52:22 -04002507 path->reada = -1;
Chris Mason39279cc2007-06-12 06:35:45 -04002508 BUG_ON(!path);
Chris Mason5f39d392007-10-15 16:14:19 -04002509
Chris Mason39279cc2007-06-12 06:35:45 -04002510 /* FIXME, add redo link to tree so we don't leak on crash */
2511 key.objectid = inode->i_ino;
2512 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04002513 key.type = (u8)-1;
2514
Chris Mason85e21ba2008-01-29 15:11:36 -05002515 btrfs_init_path(path);
Chris Mason323ac952008-10-01 19:05:46 -04002516
Chris Mason85e21ba2008-01-29 15:11:36 -05002517search_again:
2518 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masond3977122009-01-05 21:25:51 -05002519 if (ret < 0)
Chris Mason85e21ba2008-01-29 15:11:36 -05002520 goto error;
Chris Masond3977122009-01-05 21:25:51 -05002521
Chris Mason85e21ba2008-01-29 15:11:36 -05002522 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002523 /* there are no items in the tree for us to truncate, we're
2524 * done
2525 */
2526 if (path->slots[0] == 0) {
2527 ret = 0;
2528 goto error;
2529 }
Chris Mason85e21ba2008-01-29 15:11:36 -05002530 path->slots[0]--;
2531 }
2532
Chris Masond3977122009-01-05 21:25:51 -05002533 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04002534 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04002535 leaf = path->nodes[0];
2536 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2537 found_type = btrfs_key_type(&found_key);
Chris Mason771ed682008-11-06 22:02:51 -05002538 encoding = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002539
Chris Mason5f39d392007-10-15 16:14:19 -04002540 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -04002541 break;
Chris Mason5f39d392007-10-15 16:14:19 -04002542
Chris Mason85e21ba2008-01-29 15:11:36 -05002543 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04002544 break;
2545
Chris Mason5f39d392007-10-15 16:14:19 -04002546 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04002547 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04002548 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04002549 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04002550 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Mason771ed682008-11-06 22:02:51 -05002551 encoding = btrfs_file_extent_compression(leaf, fi);
2552 encoding |= btrfs_file_extent_encryption(leaf, fi);
2553 encoding |= btrfs_file_extent_other_encoding(leaf, fi);
2554
Chris Mason179e29e2007-11-01 11:28:41 -04002555 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04002556 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04002557 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04002558 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04002559 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04002560 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04002561 }
Yan008630c2007-11-07 13:31:09 -05002562 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04002563 }
Chris Masone02119d2008-09-05 16:13:11 -04002564 if (item_end < new_size) {
Chris Masond3977122009-01-05 21:25:51 -05002565 if (found_type == BTRFS_DIR_ITEM_KEY)
Chris Masonb888db22007-08-27 16:49:44 -04002566 found_type = BTRFS_INODE_ITEM_KEY;
Chris Masond3977122009-01-05 21:25:51 -05002567 else if (found_type == BTRFS_EXTENT_ITEM_KEY)
Chris Masond20f7042008-12-08 16:58:54 -05002568 found_type = BTRFS_EXTENT_DATA_KEY;
Chris Masond3977122009-01-05 21:25:51 -05002569 else if (found_type == BTRFS_EXTENT_DATA_KEY)
Chris Mason85e21ba2008-01-29 15:11:36 -05002570 found_type = BTRFS_XATTR_ITEM_KEY;
Chris Masond3977122009-01-05 21:25:51 -05002571 else if (found_type == BTRFS_XATTR_ITEM_KEY)
Chris Mason85e21ba2008-01-29 15:11:36 -05002572 found_type = BTRFS_INODE_REF_KEY;
Chris Masond3977122009-01-05 21:25:51 -05002573 else if (found_type)
Chris Masonb888db22007-08-27 16:49:44 -04002574 found_type--;
Chris Masond3977122009-01-05 21:25:51 -05002575 else
Chris Masonb888db22007-08-27 16:49:44 -04002576 break;
Yana61721d2007-09-17 11:08:38 -04002577 btrfs_set_key_type(&key, found_type);
Chris Mason85e21ba2008-01-29 15:11:36 -05002578 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04002579 }
Chris Masone02119d2008-09-05 16:13:11 -04002580 if (found_key.offset >= new_size)
Chris Mason39279cc2007-06-12 06:35:45 -04002581 del_item = 1;
2582 else
2583 del_item = 0;
2584 found_extent = 0;
2585
2586 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04002587 if (found_type != BTRFS_EXTENT_DATA_KEY)
2588 goto delete;
2589
2590 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04002591 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04002592 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason771ed682008-11-06 22:02:51 -05002593 if (!del_item && !encoding) {
Chris Masondb945352007-10-15 16:15:53 -04002594 u64 orig_num_bytes =
2595 btrfs_file_extent_num_bytes(leaf, fi);
Chris Masone02119d2008-09-05 16:13:11 -04002596 extent_num_bytes = new_size -
Chris Mason5f39d392007-10-15 16:14:19 -04002597 found_key.offset + root->sectorsize - 1;
Yanb1632b12008-01-30 11:54:04 -05002598 extent_num_bytes = extent_num_bytes &
2599 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04002600 btrfs_set_file_extent_num_bytes(leaf, fi,
2601 extent_num_bytes);
2602 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05002603 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04002604 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002605 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04002606 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002607 } else {
Chris Masondb945352007-10-15 16:15:53 -04002608 extent_num_bytes =
2609 btrfs_file_extent_disk_num_bytes(leaf,
2610 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04002611 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05002612 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04002613 if (extent_start != 0) {
2614 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04002615 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002616 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04002617 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002618 root_gen = btrfs_header_generation(leaf);
Chris Masond8d5f3e2007-12-11 12:42:00 -05002619 root_owner = btrfs_header_owner(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002620 }
Chris Mason90692182008-02-08 13:49:28 -05002621 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04002622 /*
2623 * we can't truncate inline items that have had
2624 * special encodings
2625 */
2626 if (!del_item &&
2627 btrfs_file_extent_compression(leaf, fi) == 0 &&
2628 btrfs_file_extent_encryption(leaf, fi) == 0 &&
2629 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002630 u32 size = new_size - found_key.offset;
2631
2632 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002633 inode_sub_bytes(inode, item_end + 1 -
2634 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04002635 }
2636 size =
2637 btrfs_file_extent_calc_inline_size(size);
Chris Mason90692182008-02-08 13:49:28 -05002638 ret = btrfs_truncate_item(trans, root, path,
Chris Masone02119d2008-09-05 16:13:11 -04002639 size, 1);
Chris Mason90692182008-02-08 13:49:28 -05002640 BUG_ON(ret);
Chris Masone02119d2008-09-05 16:13:11 -04002641 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002642 inode_sub_bytes(inode, item_end + 1 -
2643 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05002644 }
Chris Mason39279cc2007-06-12 06:35:45 -04002645 }
Chris Mason179e29e2007-11-01 11:28:41 -04002646delete:
Chris Mason39279cc2007-06-12 06:35:45 -04002647 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05002648 if (!pending_del_nr) {
2649 /* no pending yet, add ourselves */
2650 pending_del_slot = path->slots[0];
2651 pending_del_nr = 1;
2652 } else if (pending_del_nr &&
2653 path->slots[0] + 1 == pending_del_slot) {
2654 /* hop on the pending chunk */
2655 pending_del_nr++;
2656 pending_del_slot = path->slots[0];
2657 } else {
Chris Masond3977122009-01-05 21:25:51 -05002658 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05002659 }
Chris Mason39279cc2007-06-12 06:35:45 -04002660 } else {
2661 break;
2662 }
Chris Mason39279cc2007-06-12 06:35:45 -04002663 if (found_extent) {
2664 ret = btrfs_free_extent(trans, root, extent_start,
Chris Mason7bb86312007-12-11 09:25:06 -05002665 extent_num_bytes,
Zheng Yan31840ae2008-09-23 13:14:14 -04002666 leaf->start, root_owner,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002667 root_gen, inode->i_ino, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002668 BUG_ON(ret);
2669 }
Chris Mason85e21ba2008-01-29 15:11:36 -05002670next:
2671 if (path->slots[0] == 0) {
2672 if (pending_del_nr)
2673 goto del_pending;
2674 btrfs_release_path(root, path);
2675 goto search_again;
2676 }
2677
2678 path->slots[0]--;
2679 if (pending_del_nr &&
2680 path->slots[0] + 1 != pending_del_slot) {
2681 struct btrfs_key debug;
2682del_pending:
2683 btrfs_item_key_to_cpu(path->nodes[0], &debug,
2684 pending_del_slot);
2685 ret = btrfs_del_items(trans, root, path,
2686 pending_del_slot,
2687 pending_del_nr);
2688 BUG_ON(ret);
2689 pending_del_nr = 0;
2690 btrfs_release_path(root, path);
2691 goto search_again;
2692 }
Chris Mason39279cc2007-06-12 06:35:45 -04002693 }
2694 ret = 0;
2695error:
Chris Mason85e21ba2008-01-29 15:11:36 -05002696 if (pending_del_nr) {
2697 ret = btrfs_del_items(trans, root, path, pending_del_slot,
2698 pending_del_nr);
2699 }
Chris Mason39279cc2007-06-12 06:35:45 -04002700 btrfs_free_path(path);
2701 inode->i_sb->s_dirt = 1;
2702 return ret;
2703}
2704
Chris Masona52d9a82007-08-27 16:49:44 -04002705/*
2706 * taken from block_truncate_page, but does cow as it zeros out
2707 * any bytes left in the last page in the file.
2708 */
2709static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
2710{
2711 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -04002712 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002713 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2714 struct btrfs_ordered_extent *ordered;
2715 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04002716 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04002717 pgoff_t index = from >> PAGE_CACHE_SHIFT;
2718 unsigned offset = from & (PAGE_CACHE_SIZE-1);
2719 struct page *page;
2720 int ret = 0;
2721 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002722 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04002723
2724 if ((offset & (blocksize - 1)) == 0)
2725 goto out;
2726
2727 ret = -ENOMEM;
Chris Mason211c17f2008-05-15 09:13:45 -04002728again:
Chris Masona52d9a82007-08-27 16:49:44 -04002729 page = grab_cache_page(mapping, index);
2730 if (!page)
2731 goto out;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002732
2733 page_start = page_offset(page);
2734 page_end = page_start + PAGE_CACHE_SIZE - 1;
2735
Chris Masona52d9a82007-08-27 16:49:44 -04002736 if (!PageUptodate(page)) {
2737 ret = btrfs_readpage(NULL, page);
2738 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04002739 if (page->mapping != mapping) {
2740 unlock_page(page);
2741 page_cache_release(page);
2742 goto again;
2743 }
Chris Masona52d9a82007-08-27 16:49:44 -04002744 if (!PageUptodate(page)) {
2745 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04002746 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04002747 }
2748 }
Chris Mason211c17f2008-05-15 09:13:45 -04002749 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002750
2751 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
2752 set_page_extent_mapped(page);
2753
2754 ordered = btrfs_lookup_ordered_extent(inode, page_start);
2755 if (ordered) {
2756 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
2757 unlock_page(page);
2758 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04002759 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002760 btrfs_put_ordered_extent(ordered);
2761 goto again;
2762 }
2763
Chris Masonea8c2812008-08-04 23:17:27 -04002764 btrfs_set_extent_delalloc(inode, page_start, page_end);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002765 ret = 0;
2766 if (offset != PAGE_CACHE_SIZE) {
2767 kaddr = kmap(page);
2768 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
2769 flush_dcache_page(page);
2770 kunmap(page);
2771 }
Chris Mason247e7432008-07-17 12:53:51 -04002772 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002773 set_page_dirty(page);
2774 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04002775
Chris Mason89642222008-07-24 09:41:53 -04002776out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04002777 unlock_page(page);
2778 page_cache_release(page);
2779out:
2780 return ret;
2781}
2782
Yan Zheng9036c102008-10-30 14:19:41 -04002783int btrfs_cont_expand(struct inode *inode, loff_t size)
2784{
2785 struct btrfs_trans_handle *trans;
2786 struct btrfs_root *root = BTRFS_I(inode)->root;
2787 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2788 struct extent_map *em;
2789 u64 mask = root->sectorsize - 1;
2790 u64 hole_start = (inode->i_size + mask) & ~mask;
2791 u64 block_end = (size + mask) & ~mask;
2792 u64 last_byte;
2793 u64 cur_offset;
2794 u64 hole_size;
2795 int err;
2796
2797 if (size <= hole_start)
2798 return 0;
2799
2800 err = btrfs_check_free_space(root, 1, 0);
2801 if (err)
2802 return err;
2803
2804 btrfs_truncate_page(inode->i_mapping, inode->i_size);
2805
2806 while (1) {
2807 struct btrfs_ordered_extent *ordered;
2808 btrfs_wait_ordered_range(inode, hole_start,
2809 block_end - hole_start);
2810 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
2811 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
2812 if (!ordered)
2813 break;
2814 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
2815 btrfs_put_ordered_extent(ordered);
2816 }
2817
2818 trans = btrfs_start_transaction(root, 1);
2819 btrfs_set_trans_block_group(trans, inode);
2820
2821 cur_offset = hole_start;
2822 while (1) {
2823 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
2824 block_end - cur_offset, 0);
2825 BUG_ON(IS_ERR(em) || !em);
2826 last_byte = min(extent_map_end(em), block_end);
2827 last_byte = (last_byte + mask) & ~mask;
2828 if (test_bit(EXTENT_FLAG_VACANCY, &em->flags)) {
Chris Mason771ed682008-11-06 22:02:51 -05002829 u64 hint_byte = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04002830 hole_size = last_byte - cur_offset;
Chris Mason771ed682008-11-06 22:02:51 -05002831 err = btrfs_drop_extents(trans, root, inode,
2832 cur_offset,
2833 cur_offset + hole_size,
2834 cur_offset, &hint_byte);
2835 if (err)
2836 break;
Yan Zheng9036c102008-10-30 14:19:41 -04002837 err = btrfs_insert_file_extent(trans, root,
2838 inode->i_ino, cur_offset, 0,
2839 0, hole_size, 0, hole_size,
2840 0, 0, 0);
2841 btrfs_drop_extent_cache(inode, hole_start,
2842 last_byte - 1, 0);
2843 }
2844 free_extent_map(em);
2845 cur_offset = last_byte;
2846 if (err || cur_offset >= block_end)
2847 break;
2848 }
2849
2850 btrfs_end_transaction(trans, root);
2851 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
2852 return err;
2853}
2854
Chris Mason39279cc2007-06-12 06:35:45 -04002855static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
2856{
2857 struct inode *inode = dentry->d_inode;
2858 int err;
2859
2860 err = inode_change_ok(inode, attr);
2861 if (err)
2862 return err;
2863
2864 if (S_ISREG(inode->i_mode) &&
2865 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
Yan Zheng9036c102008-10-30 14:19:41 -04002866 err = btrfs_cont_expand(inode, attr->ia_size);
Chris Mason54aa1f42007-06-22 14:16:25 -04002867 if (err)
2868 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04002869 }
Yan Zheng9036c102008-10-30 14:19:41 -04002870
Chris Mason39279cc2007-06-12 06:35:45 -04002871 err = inode_setattr(inode, attr);
Josef Bacik33268ea2008-07-24 12:16:36 -04002872
2873 if (!err && ((attr->ia_valid & ATTR_MODE)))
2874 err = btrfs_acl_chmod(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002875 return err;
2876}
Chris Mason61295eb2008-01-14 16:24:38 -05002877
Chris Mason39279cc2007-06-12 06:35:45 -04002878void btrfs_delete_inode(struct inode *inode)
2879{
2880 struct btrfs_trans_handle *trans;
2881 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002882 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04002883 int ret;
2884
2885 truncate_inode_pages(&inode->i_data, 0);
2886 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04002887 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002888 goto no_delete;
2889 }
Chris Mason4a096752008-07-21 10:29:44 -04002890 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04002891
Chris Masondbe674a2008-07-17 12:54:05 -04002892 btrfs_i_size_write(inode, 0);
Yan Zheng180591b2009-01-06 09:58:06 -05002893 trans = btrfs_join_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002894
Chris Mason39279cc2007-06-12 06:35:45 -04002895 btrfs_set_trans_block_group(trans, inode);
Chris Masone02119d2008-09-05 16:13:11 -04002896 ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size, 0);
Josef Bacik7b128762008-07-24 12:17:14 -04002897 if (ret) {
2898 btrfs_orphan_del(NULL, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04002899 goto no_delete_lock;
Josef Bacik7b128762008-07-24 12:17:14 -04002900 }
2901
2902 btrfs_orphan_del(trans, inode);
Chris Mason85e21ba2008-01-29 15:11:36 -05002903
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002904 nr = trans->blocks_used;
Chris Mason85e21ba2008-01-29 15:11:36 -05002905 clear_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002906
Chris Mason39279cc2007-06-12 06:35:45 -04002907 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002908 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002909 return;
Chris Mason54aa1f42007-06-22 14:16:25 -04002910
2911no_delete_lock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002912 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04002913 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002914 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002915no_delete:
2916 clear_inode(inode);
2917}
2918
2919/*
2920 * this returns the key found in the dir entry in the location pointer.
2921 * If no dir entries were found, location->objectid is 0.
2922 */
2923static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
2924 struct btrfs_key *location)
2925{
2926 const char *name = dentry->d_name.name;
2927 int namelen = dentry->d_name.len;
2928 struct btrfs_dir_item *di;
2929 struct btrfs_path *path;
2930 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04002931 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002932
2933 path = btrfs_alloc_path();
2934 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -05002935
Chris Mason39279cc2007-06-12 06:35:45 -04002936 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
2937 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04002938 if (IS_ERR(di))
2939 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05002940
2941 if (!di || IS_ERR(di))
Chris Mason39544012007-12-12 14:38:19 -05002942 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05002943
Chris Mason5f39d392007-10-15 16:14:19 -04002944 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04002945out:
Chris Mason39279cc2007-06-12 06:35:45 -04002946 btrfs_free_path(path);
2947 return ret;
Chris Mason39544012007-12-12 14:38:19 -05002948out_err:
2949 location->objectid = 0;
2950 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04002951}
2952
2953/*
2954 * when we hit a tree root in a directory, the btrfs part of the inode
2955 * needs to be changed to reflect the root directory of the tree root. This
2956 * is kind of like crossing a mount point.
2957 */
2958static int fixup_tree_root_location(struct btrfs_root *root,
2959 struct btrfs_key *location,
Josef Bacik58176a92007-08-29 15:47:34 -04002960 struct btrfs_root **sub_root,
2961 struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04002962{
Chris Mason39279cc2007-06-12 06:35:45 -04002963 struct btrfs_root_item *ri;
2964
2965 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
2966 return 0;
2967 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
2968 return 0;
2969
Josef Bacik58176a92007-08-29 15:47:34 -04002970 *sub_root = btrfs_read_fs_root(root->fs_info, location,
2971 dentry->d_name.name,
2972 dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04002973 if (IS_ERR(*sub_root))
2974 return PTR_ERR(*sub_root);
2975
2976 ri = &(*sub_root)->root_item;
2977 location->objectid = btrfs_root_dirid(ri);
Chris Mason39279cc2007-06-12 06:35:45 -04002978 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
2979 location->offset = 0;
2980
Chris Mason39279cc2007-06-12 06:35:45 -04002981 return 0;
2982}
2983
Chris Masone02119d2008-09-05 16:13:11 -04002984static noinline void init_btrfs_i(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002985{
Chris Masone02119d2008-09-05 16:13:11 -04002986 struct btrfs_inode *bi = BTRFS_I(inode);
2987
2988 bi->i_acl = NULL;
2989 bi->i_default_acl = NULL;
2990
2991 bi->generation = 0;
Chris Masonc3027eb2008-12-08 16:40:21 -05002992 bi->sequence = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002993 bi->last_trans = 0;
2994 bi->logged_trans = 0;
2995 bi->delalloc_bytes = 0;
2996 bi->disk_i_size = 0;
2997 bi->flags = 0;
2998 bi->index_cnt = (u64)-1;
Chris Mason49eb7e42008-09-11 15:53:12 -04002999 bi->log_dirty_trans = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003000 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
3001 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04003002 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04003003 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
3004 inode->i_mapping, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04003005 INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes);
Chris Masonba1da2f2008-07-17 12:54:15 -04003006 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Masonee6e6502008-07-17 12:54:40 -04003007 mutex_init(&BTRFS_I(inode)->extent_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04003008 mutex_init(&BTRFS_I(inode)->log_mutex);
3009}
3010
3011static int btrfs_init_locked_inode(struct inode *inode, void *p)
3012{
3013 struct btrfs_iget_args *args = p;
3014 inode->i_ino = args->ino;
3015 init_btrfs_i(inode);
3016 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003017 return 0;
3018}
3019
3020static int btrfs_find_actor(struct inode *inode, void *opaque)
3021{
3022 struct btrfs_iget_args *args = opaque;
Chris Masond3977122009-01-05 21:25:51 -05003023 return args->ino == inode->i_ino &&
3024 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003025}
3026
Zheng Yan5b21f2e2008-09-26 10:05:38 -04003027struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
3028 struct btrfs_root *root, int wait)
3029{
3030 struct inode *inode;
3031 struct btrfs_iget_args args;
3032 args.ino = objectid;
3033 args.root = root;
3034
3035 if (wait) {
3036 inode = ilookup5(s, objectid, btrfs_find_actor,
3037 (void *)&args);
3038 } else {
3039 inode = ilookup5_nowait(s, objectid, btrfs_find_actor,
3040 (void *)&args);
3041 }
3042 return inode;
3043}
3044
Chris Mason39279cc2007-06-12 06:35:45 -04003045struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
3046 struct btrfs_root *root)
3047{
3048 struct inode *inode;
3049 struct btrfs_iget_args args;
3050 args.ino = objectid;
3051 args.root = root;
3052
3053 inode = iget5_locked(s, objectid, btrfs_find_actor,
3054 btrfs_init_locked_inode,
3055 (void *)&args);
3056 return inode;
3057}
3058
Balaji Rao1a54ef82008-07-21 02:01:04 +05303059/* Get an inode object given its location and corresponding root.
3060 * Returns in *is_new if the inode was read from disk
3061 */
3062struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
3063 struct btrfs_root *root, int *is_new)
3064{
3065 struct inode *inode;
3066
3067 inode = btrfs_iget_locked(s, location->objectid, root);
3068 if (!inode)
3069 return ERR_PTR(-EACCES);
3070
3071 if (inode->i_state & I_NEW) {
3072 BTRFS_I(inode)->root = root;
3073 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
3074 btrfs_read_locked_inode(inode);
3075 unlock_new_inode(inode);
3076 if (is_new)
3077 *is_new = 1;
3078 } else {
3079 if (is_new)
3080 *is_new = 0;
3081 }
3082
3083 return inode;
3084}
3085
Chris Mason3de45862008-11-17 21:02:50 -05003086struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04003087{
Chris Masond3977122009-01-05 21:25:51 -05003088 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003089 struct btrfs_inode *bi = BTRFS_I(dir);
3090 struct btrfs_root *root = bi->root;
3091 struct btrfs_root *sub_root = root;
3092 struct btrfs_key location;
Yan Zhengc146afa2008-11-12 14:34:12 -05003093 int ret, new;
Chris Mason39279cc2007-06-12 06:35:45 -04003094
3095 if (dentry->d_name.len > BTRFS_NAME_LEN)
3096 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04003097
Chris Mason39279cc2007-06-12 06:35:45 -04003098 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04003099
Chris Mason39279cc2007-06-12 06:35:45 -04003100 if (ret < 0)
3101 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04003102
Chris Mason39279cc2007-06-12 06:35:45 -04003103 inode = NULL;
3104 if (location.objectid) {
Josef Bacik58176a92007-08-29 15:47:34 -04003105 ret = fixup_tree_root_location(root, &location, &sub_root,
3106 dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04003107 if (ret < 0)
3108 return ERR_PTR(ret);
3109 if (ret > 0)
3110 return ERR_PTR(-ENOENT);
Balaji Rao1a54ef82008-07-21 02:01:04 +05303111 inode = btrfs_iget(dir->i_sb, &location, sub_root, &new);
3112 if (IS_ERR(inode))
3113 return ERR_CAST(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003114 }
Chris Mason3de45862008-11-17 21:02:50 -05003115 return inode;
3116}
3117
3118static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
3119 struct nameidata *nd)
3120{
3121 struct inode *inode;
3122
3123 if (dentry->d_name.len > BTRFS_NAME_LEN)
3124 return ERR_PTR(-ENAMETOOLONG);
3125
3126 inode = btrfs_lookup_dentry(dir, dentry);
3127 if (IS_ERR(inode))
3128 return ERR_CAST(inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003129
Chris Mason39279cc2007-06-12 06:35:45 -04003130 return d_splice_alias(inode, dentry);
3131}
3132
Chris Mason39279cc2007-06-12 06:35:45 -04003133static unsigned char btrfs_filetype_table[] = {
3134 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
3135};
3136
David Woodhousecbdf5a22008-08-06 19:42:33 +01003137static int btrfs_real_readdir(struct file *filp, void *dirent,
3138 filldir_t filldir)
Chris Mason39279cc2007-06-12 06:35:45 -04003139{
Chris Mason6da6aba2007-12-18 16:15:09 -05003140 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003141 struct btrfs_root *root = BTRFS_I(inode)->root;
3142 struct btrfs_item *item;
3143 struct btrfs_dir_item *di;
3144 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04003145 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04003146 struct btrfs_path *path;
3147 int ret;
3148 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04003149 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003150 int slot;
3151 int advance;
3152 unsigned char d_type;
3153 int over = 0;
3154 u32 di_cur;
3155 u32 di_total;
3156 u32 di_len;
3157 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04003158 char tmp_name[32];
3159 char *name_ptr;
3160 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04003161
3162 /* FIXME, use a real flag for deciding about the key type */
3163 if (root->fs_info->tree_root == root)
3164 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04003165
Chris Mason39544012007-12-12 14:38:19 -05003166 /* special case for "." */
3167 if (filp->f_pos == 0) {
3168 over = filldir(dirent, ".", 1,
3169 1, inode->i_ino,
3170 DT_DIR);
3171 if (over)
3172 return 0;
3173 filp->f_pos = 1;
3174 }
Chris Mason39544012007-12-12 14:38:19 -05003175 /* special case for .., just use the back ref */
3176 if (filp->f_pos == 1) {
David Woodhouse5ecc7e52008-08-17 15:14:48 +01003177 u64 pino = parent_ino(filp->f_path.dentry);
Chris Mason39544012007-12-12 14:38:19 -05003178 over = filldir(dirent, "..", 2,
David Woodhouse5ecc7e52008-08-17 15:14:48 +01003179 2, pino, DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05003180 if (over)
David Woodhouse49593bf2008-08-17 17:08:36 +01003181 return 0;
Chris Mason39544012007-12-12 14:38:19 -05003182 filp->f_pos = 2;
3183 }
David Woodhouse49593bf2008-08-17 17:08:36 +01003184 path = btrfs_alloc_path();
3185 path->reada = 2;
3186
Chris Mason39279cc2007-06-12 06:35:45 -04003187 btrfs_set_key_type(&key, key_type);
3188 key.offset = filp->f_pos;
David Woodhouse49593bf2008-08-17 17:08:36 +01003189 key.objectid = inode->i_ino;
Chris Mason5f39d392007-10-15 16:14:19 -04003190
Chris Mason39279cc2007-06-12 06:35:45 -04003191 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3192 if (ret < 0)
3193 goto err;
3194 advance = 0;
David Woodhouse49593bf2008-08-17 17:08:36 +01003195
3196 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04003197 leaf = path->nodes[0];
3198 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003199 slot = path->slots[0];
3200 if (advance || slot >= nritems) {
David Woodhouse49593bf2008-08-17 17:08:36 +01003201 if (slot >= nritems - 1) {
Chris Mason39279cc2007-06-12 06:35:45 -04003202 ret = btrfs_next_leaf(root, path);
3203 if (ret)
3204 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003205 leaf = path->nodes[0];
3206 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003207 slot = path->slots[0];
3208 } else {
3209 slot++;
3210 path->slots[0]++;
3211 }
3212 }
Chris Mason3de45862008-11-17 21:02:50 -05003213
Chris Mason39279cc2007-06-12 06:35:45 -04003214 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04003215 item = btrfs_item_nr(leaf, slot);
3216 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3217
3218 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04003219 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003220 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003221 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003222 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04003223 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04003224
3225 filp->f_pos = found_key.offset;
David Woodhouse49593bf2008-08-17 17:08:36 +01003226
Chris Mason39279cc2007-06-12 06:35:45 -04003227 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
3228 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003229 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01003230
3231 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04003232 struct btrfs_key location;
3233
3234 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01003235 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04003236 name_ptr = tmp_name;
3237 } else {
3238 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01003239 if (!name_ptr) {
3240 ret = -ENOMEM;
3241 goto err;
3242 }
Chris Mason5f39d392007-10-15 16:14:19 -04003243 }
3244 read_extent_buffer(leaf, name_ptr,
3245 (unsigned long)(di + 1), name_len);
3246
3247 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
3248 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05003249
3250 /* is this a reference to our own snapshot? If so
3251 * skip it
3252 */
3253 if (location.type == BTRFS_ROOT_ITEM_KEY &&
3254 location.objectid == root->root_key.objectid) {
3255 over = 0;
3256 goto skip;
3257 }
Chris Mason5f39d392007-10-15 16:14:19 -04003258 over = filldir(dirent, name_ptr, name_len,
David Woodhouse49593bf2008-08-17 17:08:36 +01003259 found_key.offset, location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04003260 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04003261
Chris Mason3de45862008-11-17 21:02:50 -05003262skip:
Chris Mason5f39d392007-10-15 16:14:19 -04003263 if (name_ptr != tmp_name)
3264 kfree(name_ptr);
3265
Chris Mason39279cc2007-06-12 06:35:45 -04003266 if (over)
3267 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05003268 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01003269 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04003270 di_cur += di_len;
3271 di = (struct btrfs_dir_item *)((char *)di + di_len);
3272 }
3273 }
David Woodhouse49593bf2008-08-17 17:08:36 +01003274
3275 /* Reached end of directory/root. Bump pos past the last item. */
Yan Zheng5e591a02008-02-19 11:41:02 -05003276 if (key_type == BTRFS_DIR_INDEX_KEY)
Chris Mason89f135d2009-01-28 15:34:27 -05003277 filp->f_pos = INT_LIMIT(off_t);
Yan Zheng5e591a02008-02-19 11:41:02 -05003278 else
3279 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04003280nopos:
3281 ret = 0;
3282err:
Chris Mason39279cc2007-06-12 06:35:45 -04003283 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003284 return ret;
3285}
3286
3287int btrfs_write_inode(struct inode *inode, int wait)
3288{
3289 struct btrfs_root *root = BTRFS_I(inode)->root;
3290 struct btrfs_trans_handle *trans;
3291 int ret = 0;
3292
Yan Zhengc146afa2008-11-12 14:34:12 -05003293 if (root->fs_info->btree_inode == inode)
Chris Mason4ca8b412008-08-05 13:30:48 -04003294 return 0;
3295
Chris Mason39279cc2007-06-12 06:35:45 -04003296 if (wait) {
Chris Masonf9295742008-07-17 12:54:14 -04003297 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003298 btrfs_set_trans_block_group(trans, inode);
3299 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04003300 }
3301 return ret;
3302}
3303
3304/*
Chris Mason54aa1f42007-06-22 14:16:25 -04003305 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04003306 * inode changes. But, it is most likely to find the inode in cache.
3307 * FIXME, needs more benchmarking...there are no reasons other than performance
3308 * to keep or drop this code.
3309 */
3310void btrfs_dirty_inode(struct inode *inode)
3311{
3312 struct btrfs_root *root = BTRFS_I(inode)->root;
3313 struct btrfs_trans_handle *trans;
3314
Chris Masonf9295742008-07-17 12:54:14 -04003315 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003316 btrfs_set_trans_block_group(trans, inode);
3317 btrfs_update_inode(trans, root, inode);
3318 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04003319}
3320
Chris Masond352ac62008-09-29 15:18:18 -04003321/*
3322 * find the highest existing sequence number in a directory
3323 * and then set the in-memory index_cnt variable to reflect
3324 * free sequence numbers
3325 */
Josef Bacikaec74772008-07-24 12:12:38 -04003326static int btrfs_set_inode_index_count(struct inode *inode)
3327{
3328 struct btrfs_root *root = BTRFS_I(inode)->root;
3329 struct btrfs_key key, found_key;
3330 struct btrfs_path *path;
3331 struct extent_buffer *leaf;
3332 int ret;
3333
3334 key.objectid = inode->i_ino;
3335 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
3336 key.offset = (u64)-1;
3337
3338 path = btrfs_alloc_path();
3339 if (!path)
3340 return -ENOMEM;
3341
3342 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3343 if (ret < 0)
3344 goto out;
3345 /* FIXME: we should be able to handle this */
3346 if (ret == 0)
3347 goto out;
3348 ret = 0;
3349
3350 /*
3351 * MAGIC NUMBER EXPLANATION:
3352 * since we search a directory based on f_pos we have to start at 2
3353 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
3354 * else has to start at 2
3355 */
3356 if (path->slots[0] == 0) {
3357 BTRFS_I(inode)->index_cnt = 2;
3358 goto out;
3359 }
3360
3361 path->slots[0]--;
3362
3363 leaf = path->nodes[0];
3364 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3365
3366 if (found_key.objectid != inode->i_ino ||
3367 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
3368 BTRFS_I(inode)->index_cnt = 2;
3369 goto out;
3370 }
3371
3372 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
3373out:
3374 btrfs_free_path(path);
3375 return ret;
3376}
3377
Chris Masond352ac62008-09-29 15:18:18 -04003378/*
3379 * helper to find a free sequence number in a given directory. This current
3380 * code is very simple, later versions will do smarter things in the btree
3381 */
Chris Mason3de45862008-11-17 21:02:50 -05003382int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04003383{
3384 int ret = 0;
3385
3386 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
3387 ret = btrfs_set_inode_index_count(dir);
Chris Masond3977122009-01-05 21:25:51 -05003388 if (ret)
Josef Bacikaec74772008-07-24 12:12:38 -04003389 return ret;
3390 }
3391
Chris Mason00e4e6b2008-08-05 11:18:09 -04003392 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04003393 BTRFS_I(dir)->index_cnt++;
3394
3395 return ret;
3396}
3397
Chris Mason39279cc2007-06-12 06:35:45 -04003398static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
3399 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04003400 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05003401 const char *name, int name_len,
Yan Zhengd2fb3432008-12-11 16:30:39 -05003402 u64 ref_objectid, u64 objectid,
3403 u64 alloc_hint, int mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04003404{
3405 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04003406 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04003407 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04003408 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05003409 struct btrfs_inode_ref *ref;
3410 struct btrfs_key key[2];
3411 u32 sizes[2];
3412 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04003413 int ret;
3414 int owner;
3415
Chris Mason5f39d392007-10-15 16:14:19 -04003416 path = btrfs_alloc_path();
3417 BUG_ON(!path);
3418
Chris Mason39279cc2007-06-12 06:35:45 -04003419 inode = new_inode(root->fs_info->sb);
3420 if (!inode)
3421 return ERR_PTR(-ENOMEM);
3422
Josef Bacikaec74772008-07-24 12:12:38 -04003423 if (dir) {
Chris Mason3de45862008-11-17 21:02:50 -05003424 ret = btrfs_set_inode_index(dir, index);
Josef Bacikaec74772008-07-24 12:12:38 -04003425 if (ret)
3426 return ERR_PTR(ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003427 }
3428 /*
3429 * index_cnt is ignored for everything but a dir,
3430 * btrfs_get_inode_index_count has an explanation for the magic
3431 * number
3432 */
Chris Masone02119d2008-09-05 16:13:11 -04003433 init_btrfs_i(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04003434 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04003435 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04003436 BTRFS_I(inode)->generation = trans->transid;
Chris Masonb888db22007-08-27 16:49:44 -04003437
Chris Mason39279cc2007-06-12 06:35:45 -04003438 if (mode & S_IFDIR)
3439 owner = 0;
3440 else
3441 owner = 1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003442 BTRFS_I(inode)->block_group =
3443 btrfs_find_block_group(root, 0, alloc_hint, owner);
Yan Zheng17d217f2008-12-12 10:03:38 -05003444 if ((mode & S_IFREG)) {
3445 if (btrfs_test_opt(root, NODATASUM))
3446 btrfs_set_flag(inode, NODATASUM);
3447 if (btrfs_test_opt(root, NODATACOW))
3448 btrfs_set_flag(inode, NODATACOW);
3449 }
Chris Mason9c583092008-01-29 15:15:18 -05003450
3451 key[0].objectid = objectid;
3452 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
3453 key[0].offset = 0;
3454
3455 key[1].objectid = objectid;
3456 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
3457 key[1].offset = ref_objectid;
3458
3459 sizes[0] = sizeof(struct btrfs_inode_item);
3460 sizes[1] = name_len + sizeof(*ref);
3461
3462 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
3463 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04003464 goto fail;
3465
Chris Mason9c583092008-01-29 15:15:18 -05003466 if (objectid > root->highest_inode)
3467 root->highest_inode = objectid;
3468
Chris Mason79683f22008-11-19 22:00:53 -05003469 inode->i_uid = current_fsuid();
3470 inode->i_gid = current_fsgid();
Chris Mason39279cc2007-06-12 06:35:45 -04003471 inode->i_mode = mode;
3472 inode->i_ino = objectid;
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003473 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003474 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04003475 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3476 struct btrfs_inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003477 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05003478
3479 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
3480 struct btrfs_inode_ref);
3481 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04003482 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05003483 ptr = (unsigned long)(ref + 1);
3484 write_extent_buffer(path->nodes[0], name, ptr, name_len);
3485
Chris Mason5f39d392007-10-15 16:14:19 -04003486 btrfs_mark_buffer_dirty(path->nodes[0]);
3487 btrfs_free_path(path);
3488
Chris Mason39279cc2007-06-12 06:35:45 -04003489 location = &BTRFS_I(inode)->location;
3490 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04003491 location->offset = 0;
3492 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
3493
Chris Mason39279cc2007-06-12 06:35:45 -04003494 insert_inode_hash(inode);
3495 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04003496fail:
Josef Bacikaec74772008-07-24 12:12:38 -04003497 if (dir)
3498 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04003499 btrfs_free_path(path);
3500 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003501}
3502
3503static inline u8 btrfs_inode_type(struct inode *inode)
3504{
3505 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
3506}
3507
Chris Masond352ac62008-09-29 15:18:18 -04003508/*
3509 * utility function to add 'inode' into 'parent_inode' with
3510 * a give name and a given sequence number.
3511 * if 'add_backref' is true, also insert a backref from the
3512 * inode to the parent directory.
3513 */
Chris Masone02119d2008-09-05 16:13:11 -04003514int btrfs_add_link(struct btrfs_trans_handle *trans,
3515 struct inode *parent_inode, struct inode *inode,
3516 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04003517{
3518 int ret;
3519 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04003520 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Chris Mason5f39d392007-10-15 16:14:19 -04003521
Chris Mason39279cc2007-06-12 06:35:45 -04003522 key.objectid = inode->i_ino;
Chris Mason39279cc2007-06-12 06:35:45 -04003523 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
3524 key.offset = 0;
3525
Chris Masone02119d2008-09-05 16:13:11 -04003526 ret = btrfs_insert_dir_item(trans, root, name, name_len,
3527 parent_inode->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04003528 &key, btrfs_inode_type(inode),
Chris Mason00e4e6b2008-08-05 11:18:09 -04003529 index);
Chris Mason39279cc2007-06-12 06:35:45 -04003530 if (ret == 0) {
Chris Mason9c583092008-01-29 15:15:18 -05003531 if (add_backref) {
3532 ret = btrfs_insert_inode_ref(trans, root,
Chris Masone02119d2008-09-05 16:13:11 -04003533 name, name_len,
3534 inode->i_ino,
3535 parent_inode->i_ino,
3536 index);
Chris Mason9c583092008-01-29 15:15:18 -05003537 }
Chris Masondbe674a2008-07-17 12:54:05 -04003538 btrfs_i_size_write(parent_inode, parent_inode->i_size +
Chris Masone02119d2008-09-05 16:13:11 -04003539 name_len * 2);
Chris Mason79c44582007-06-25 10:09:33 -04003540 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Masone02119d2008-09-05 16:13:11 -04003541 ret = btrfs_update_inode(trans, root, parent_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003542 }
3543 return ret;
3544}
3545
3546static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05003547 struct dentry *dentry, struct inode *inode,
Chris Mason00e4e6b2008-08-05 11:18:09 -04003548 int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04003549{
Chris Masone02119d2008-09-05 16:13:11 -04003550 int err = btrfs_add_link(trans, dentry->d_parent->d_inode,
3551 inode, dentry->d_name.name,
3552 dentry->d_name.len, backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04003553 if (!err) {
3554 d_instantiate(dentry, inode);
3555 return 0;
3556 }
3557 if (err > 0)
3558 err = -EEXIST;
3559 return err;
3560}
3561
Josef Bacik618e21d2007-07-11 10:18:17 -04003562static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
3563 int mode, dev_t rdev)
3564{
3565 struct btrfs_trans_handle *trans;
3566 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05003567 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04003568 int err;
3569 int drop_inode = 0;
3570 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05003571 unsigned long nr = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04003572 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04003573
3574 if (!new_valid_dev(rdev))
3575 return -EINVAL;
3576
Chris Mason1832a6d2007-12-21 16:27:21 -05003577 err = btrfs_check_free_space(root, 1, 0);
3578 if (err)
3579 goto fail;
3580
Josef Bacik618e21d2007-07-11 10:18:17 -04003581 trans = btrfs_start_transaction(root, 1);
3582 btrfs_set_trans_block_group(trans, dir);
3583
3584 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3585 if (err) {
3586 err = -ENOSPC;
3587 goto out_unlock;
3588 }
3589
Josef Bacikaec74772008-07-24 12:12:38 -04003590 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05003591 dentry->d_name.len,
3592 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04003593 BTRFS_I(dir)->block_group, mode, &index);
Josef Bacik618e21d2007-07-11 10:18:17 -04003594 err = PTR_ERR(inode);
3595 if (IS_ERR(inode))
3596 goto out_unlock;
3597
Jim Owens0279b4c2009-02-04 09:29:13 -05003598 err = btrfs_init_inode_security(inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04003599 if (err) {
3600 drop_inode = 1;
3601 goto out_unlock;
3602 }
3603
Josef Bacik618e21d2007-07-11 10:18:17 -04003604 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04003605 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04003606 if (err)
3607 drop_inode = 1;
3608 else {
3609 inode->i_op = &btrfs_special_inode_operations;
3610 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04003611 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04003612 }
3613 dir->i_sb->s_dirt = 1;
3614 btrfs_update_inode_block_group(trans, inode);
3615 btrfs_update_inode_block_group(trans, dir);
3616out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003617 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04003618 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003619fail:
Josef Bacik618e21d2007-07-11 10:18:17 -04003620 if (drop_inode) {
3621 inode_dec_link_count(inode);
3622 iput(inode);
3623 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003624 btrfs_btree_balance_dirty(root, nr);
Josef Bacik618e21d2007-07-11 10:18:17 -04003625 return err;
3626}
3627
Chris Mason39279cc2007-06-12 06:35:45 -04003628static int btrfs_create(struct inode *dir, struct dentry *dentry,
3629 int mode, struct nameidata *nd)
3630{
3631 struct btrfs_trans_handle *trans;
3632 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05003633 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04003634 int err;
3635 int drop_inode = 0;
Chris Mason1832a6d2007-12-21 16:27:21 -05003636 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003637 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04003638 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003639
Chris Mason1832a6d2007-12-21 16:27:21 -05003640 err = btrfs_check_free_space(root, 1, 0);
3641 if (err)
3642 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04003643 trans = btrfs_start_transaction(root, 1);
3644 btrfs_set_trans_block_group(trans, dir);
3645
3646 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3647 if (err) {
3648 err = -ENOSPC;
3649 goto out_unlock;
3650 }
3651
Josef Bacikaec74772008-07-24 12:12:38 -04003652 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05003653 dentry->d_name.len,
3654 dentry->d_parent->d_inode->i_ino,
Chris Mason00e4e6b2008-08-05 11:18:09 -04003655 objectid, BTRFS_I(dir)->block_group, mode,
3656 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04003657 err = PTR_ERR(inode);
3658 if (IS_ERR(inode))
3659 goto out_unlock;
3660
Jim Owens0279b4c2009-02-04 09:29:13 -05003661 err = btrfs_init_inode_security(inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04003662 if (err) {
3663 drop_inode = 1;
3664 goto out_unlock;
3665 }
3666
Chris Mason39279cc2007-06-12 06:35:45 -04003667 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04003668 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04003669 if (err)
3670 drop_inode = 1;
3671 else {
3672 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003673 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003674 inode->i_fop = &btrfs_file_operations;
3675 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05003676 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003677 }
3678 dir->i_sb->s_dirt = 1;
3679 btrfs_update_inode_block_group(trans, inode);
3680 btrfs_update_inode_block_group(trans, dir);
3681out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003682 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04003683 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003684fail:
Chris Mason39279cc2007-06-12 06:35:45 -04003685 if (drop_inode) {
3686 inode_dec_link_count(inode);
3687 iput(inode);
3688 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003689 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003690 return err;
3691}
3692
3693static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
3694 struct dentry *dentry)
3695{
3696 struct btrfs_trans_handle *trans;
3697 struct btrfs_root *root = BTRFS_I(dir)->root;
3698 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04003699 u64 index;
Chris Mason1832a6d2007-12-21 16:27:21 -05003700 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003701 int err;
3702 int drop_inode = 0;
3703
3704 if (inode->i_nlink == 0)
3705 return -ENOENT;
3706
Chris Masone02119d2008-09-05 16:13:11 -04003707 btrfs_inc_nlink(inode);
Chris Mason1832a6d2007-12-21 16:27:21 -05003708 err = btrfs_check_free_space(root, 1, 0);
3709 if (err)
3710 goto fail;
Chris Mason3de45862008-11-17 21:02:50 -05003711 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003712 if (err)
3713 goto fail;
3714
Chris Mason39279cc2007-06-12 06:35:45 -04003715 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003716
Chris Mason39279cc2007-06-12 06:35:45 -04003717 btrfs_set_trans_block_group(trans, dir);
3718 atomic_inc(&inode->i_count);
Josef Bacikaec74772008-07-24 12:12:38 -04003719
Chris Mason00e4e6b2008-08-05 11:18:09 -04003720 err = btrfs_add_nondir(trans, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04003721
Chris Mason39279cc2007-06-12 06:35:45 -04003722 if (err)
3723 drop_inode = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04003724
Chris Mason39279cc2007-06-12 06:35:45 -04003725 dir->i_sb->s_dirt = 1;
3726 btrfs_update_inode_block_group(trans, dir);
Chris Mason54aa1f42007-06-22 14:16:25 -04003727 err = btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003728
Chris Mason54aa1f42007-06-22 14:16:25 -04003729 if (err)
3730 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003731
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003732 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04003733 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003734fail:
Chris Mason39279cc2007-06-12 06:35:45 -04003735 if (drop_inode) {
3736 inode_dec_link_count(inode);
3737 iput(inode);
3738 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003739 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003740 return err;
3741}
3742
Chris Mason39279cc2007-06-12 06:35:45 -04003743static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
3744{
Chris Masonb9d86662008-05-02 16:13:49 -04003745 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04003746 struct btrfs_trans_handle *trans;
3747 struct btrfs_root *root = BTRFS_I(dir)->root;
3748 int err = 0;
3749 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04003750 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04003751 u64 index = 0;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003752 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003753
Chris Mason1832a6d2007-12-21 16:27:21 -05003754 err = btrfs_check_free_space(root, 1, 0);
3755 if (err)
3756 goto out_unlock;
3757
Chris Mason39279cc2007-06-12 06:35:45 -04003758 trans = btrfs_start_transaction(root, 1);
3759 btrfs_set_trans_block_group(trans, dir);
Chris Mason5f39d392007-10-15 16:14:19 -04003760
Chris Mason39279cc2007-06-12 06:35:45 -04003761 if (IS_ERR(trans)) {
3762 err = PTR_ERR(trans);
3763 goto out_unlock;
3764 }
3765
3766 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3767 if (err) {
3768 err = -ENOSPC;
3769 goto out_unlock;
3770 }
3771
Josef Bacikaec74772008-07-24 12:12:38 -04003772 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05003773 dentry->d_name.len,
3774 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04003775 BTRFS_I(dir)->block_group, S_IFDIR | mode,
3776 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04003777 if (IS_ERR(inode)) {
3778 err = PTR_ERR(inode);
3779 goto out_fail;
3780 }
Chris Mason5f39d392007-10-15 16:14:19 -04003781
Chris Mason39279cc2007-06-12 06:35:45 -04003782 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04003783
Jim Owens0279b4c2009-02-04 09:29:13 -05003784 err = btrfs_init_inode_security(inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04003785 if (err)
3786 goto out_fail;
3787
Chris Mason39279cc2007-06-12 06:35:45 -04003788 inode->i_op = &btrfs_dir_inode_operations;
3789 inode->i_fop = &btrfs_dir_file_operations;
3790 btrfs_set_trans_block_group(trans, inode);
3791
Chris Masondbe674a2008-07-17 12:54:05 -04003792 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003793 err = btrfs_update_inode(trans, root, inode);
3794 if (err)
3795 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04003796
Chris Masone02119d2008-09-05 16:13:11 -04003797 err = btrfs_add_link(trans, dentry->d_parent->d_inode,
3798 inode, dentry->d_name.name,
3799 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04003800 if (err)
3801 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04003802
Chris Mason39279cc2007-06-12 06:35:45 -04003803 d_instantiate(dentry, inode);
3804 drop_on_err = 0;
3805 dir->i_sb->s_dirt = 1;
3806 btrfs_update_inode_block_group(trans, inode);
3807 btrfs_update_inode_block_group(trans, dir);
3808
3809out_fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003810 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04003811 btrfs_end_transaction_throttle(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04003812
Chris Mason39279cc2007-06-12 06:35:45 -04003813out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04003814 if (drop_on_err)
3815 iput(inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003816 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003817 return err;
3818}
3819
Chris Masond352ac62008-09-29 15:18:18 -04003820/* helper for btfs_get_extent. Given an existing extent in the tree,
3821 * and an extent that you want to insert, deal with overlap and insert
3822 * the new extent into the tree.
3823 */
Chris Mason3b951512008-04-17 11:29:12 -04003824static int merge_extent_mapping(struct extent_map_tree *em_tree,
3825 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003826 struct extent_map *em,
3827 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04003828{
3829 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04003830
Chris Masone6dcd2d2008-07-17 12:53:50 -04003831 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
3832 start_diff = map_start - em->start;
3833 em->start = map_start;
3834 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04003835 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
3836 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04003837 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04003838 em->block_len -= start_diff;
3839 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003840 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04003841}
3842
Chris Masonc8b97812008-10-29 14:49:59 -04003843static noinline int uncompress_inline(struct btrfs_path *path,
3844 struct inode *inode, struct page *page,
3845 size_t pg_offset, u64 extent_offset,
3846 struct btrfs_file_extent_item *item)
3847{
3848 int ret;
3849 struct extent_buffer *leaf = path->nodes[0];
3850 char *tmp;
3851 size_t max_size;
3852 unsigned long inline_size;
3853 unsigned long ptr;
3854
3855 WARN_ON(pg_offset != 0);
3856 max_size = btrfs_file_extent_ram_bytes(leaf, item);
3857 inline_size = btrfs_file_extent_inline_item_len(leaf,
3858 btrfs_item_nr(leaf, path->slots[0]));
3859 tmp = kmalloc(inline_size, GFP_NOFS);
3860 ptr = btrfs_file_extent_inline_start(item);
3861
3862 read_extent_buffer(leaf, tmp, ptr, inline_size);
3863
Chris Mason5b050f02008-11-11 09:34:41 -05003864 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04003865 ret = btrfs_zlib_decompress(tmp, page, extent_offset,
3866 inline_size, max_size);
3867 if (ret) {
3868 char *kaddr = kmap_atomic(page, KM_USER0);
3869 unsigned long copy_size = min_t(u64,
3870 PAGE_CACHE_SIZE - pg_offset,
3871 max_size - extent_offset);
3872 memset(kaddr + pg_offset, 0, copy_size);
3873 kunmap_atomic(kaddr, KM_USER0);
3874 }
3875 kfree(tmp);
3876 return 0;
3877}
3878
Chris Masond352ac62008-09-29 15:18:18 -04003879/*
3880 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05003881 * the ugly parts come from merging extents from the disk with the in-ram
3882 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04003883 * where the in-ram extents might be locked pending data=ordered completion.
3884 *
3885 * This also copies inline extents directly into the page.
3886 */
Chris Masond3977122009-01-05 21:25:51 -05003887
Chris Masona52d9a82007-08-27 16:49:44 -04003888struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05003889 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04003890 int create)
3891{
3892 int ret;
3893 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04003894 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04003895 u64 extent_start = 0;
3896 u64 extent_end = 0;
3897 u64 objectid = inode->i_ino;
3898 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04003899 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04003900 struct btrfs_root *root = BTRFS_I(inode)->root;
3901 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04003902 struct extent_buffer *leaf;
3903 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04003904 struct extent_map *em = NULL;
3905 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05003906 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04003907 struct btrfs_trans_handle *trans = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04003908 int compressed;
Chris Masona52d9a82007-08-27 16:49:44 -04003909
Chris Masona52d9a82007-08-27 16:49:44 -04003910again:
Chris Masond1310b22008-01-24 16:13:08 -05003911 spin_lock(&em_tree->lock);
3912 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04003913 if (em)
3914 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05003915 spin_unlock(&em_tree->lock);
3916
Chris Masona52d9a82007-08-27 16:49:44 -04003917 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04003918 if (em->start > start || em->start + em->len <= start)
3919 free_extent_map(em);
3920 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05003921 free_extent_map(em);
3922 else
3923 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04003924 }
Chris Masond1310b22008-01-24 16:13:08 -05003925 em = alloc_extent_map(GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04003926 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05003927 err = -ENOMEM;
3928 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04003929 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003930 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05003931 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05003932 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05003933 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04003934 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04003935
3936 if (!path) {
3937 path = btrfs_alloc_path();
3938 BUG_ON(!path);
3939 }
3940
Chris Mason179e29e2007-11-01 11:28:41 -04003941 ret = btrfs_lookup_file_extent(trans, root, path,
3942 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04003943 if (ret < 0) {
3944 err = ret;
3945 goto out;
3946 }
3947
3948 if (ret != 0) {
3949 if (path->slots[0] == 0)
3950 goto not_found;
3951 path->slots[0]--;
3952 }
3953
Chris Mason5f39d392007-10-15 16:14:19 -04003954 leaf = path->nodes[0];
3955 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04003956 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04003957 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04003958 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3959 found_type = btrfs_key_type(&found_key);
3960 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04003961 found_type != BTRFS_EXTENT_DATA_KEY) {
3962 goto not_found;
3963 }
3964
Chris Mason5f39d392007-10-15 16:14:19 -04003965 found_type = btrfs_file_extent_type(leaf, item);
3966 extent_start = found_key.offset;
Chris Masonc8b97812008-10-29 14:49:59 -04003967 compressed = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04003968 if (found_type == BTRFS_FILE_EXTENT_REG ||
3969 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04003970 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04003971 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04003972 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
3973 size_t size;
3974 size = btrfs_file_extent_inline_len(leaf, item);
3975 extent_end = (extent_start + size + root->sectorsize - 1) &
3976 ~((u64)root->sectorsize - 1);
3977 }
3978
3979 if (start >= extent_end) {
3980 path->slots[0]++;
3981 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
3982 ret = btrfs_next_leaf(root, path);
3983 if (ret < 0) {
3984 err = ret;
3985 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04003986 }
Yan Zheng9036c102008-10-30 14:19:41 -04003987 if (ret > 0)
3988 goto not_found;
3989 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04003990 }
Yan Zheng9036c102008-10-30 14:19:41 -04003991 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3992 if (found_key.objectid != objectid ||
3993 found_key.type != BTRFS_EXTENT_DATA_KEY)
3994 goto not_found;
3995 if (start + len <= found_key.offset)
3996 goto not_found;
3997 em->start = start;
3998 em->len = found_key.offset - start;
3999 goto not_found_em;
4000 }
4001
Yan Zhengd899e052008-10-30 14:25:28 -04004002 if (found_type == BTRFS_FILE_EXTENT_REG ||
4003 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04004004 em->start = extent_start;
4005 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05004006 em->orig_start = extent_start -
4007 btrfs_file_extent_offset(leaf, item);
Chris Masondb945352007-10-15 16:15:53 -04004008 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
4009 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004010 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04004011 goto insert;
4012 }
Chris Masonc8b97812008-10-29 14:49:59 -04004013 if (compressed) {
4014 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
4015 em->block_start = bytenr;
4016 em->block_len = btrfs_file_extent_disk_num_bytes(leaf,
4017 item);
4018 } else {
4019 bytenr += btrfs_file_extent_offset(leaf, item);
4020 em->block_start = bytenr;
4021 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04004022 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
4023 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04004024 }
Chris Masona52d9a82007-08-27 16:49:44 -04004025 goto insert;
4026 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004027 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04004028 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04004029 size_t size;
4030 size_t extent_offset;
4031 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04004032
Chris Masona52d9a82007-08-27 16:49:44 -04004033 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04004034 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04004035 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04004036 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04004037 goto out;
4038 }
4039
Yan Zheng9036c102008-10-30 14:19:41 -04004040 size = btrfs_file_extent_inline_len(leaf, item);
4041 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05004042 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04004043 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04004044 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05004045 em->len = (copy_size + root->sectorsize - 1) &
4046 ~((u64)root->sectorsize - 1);
Yan Zhengff5b7ee2008-11-10 07:34:43 -05004047 em->orig_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04004048 if (compressed)
4049 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Yan689f9342007-10-29 11:41:07 -04004050 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04004051 if (create == 0 && !PageUptodate(page)) {
Chris Masonc8b97812008-10-29 14:49:59 -04004052 if (btrfs_file_extent_compression(leaf, item) ==
4053 BTRFS_COMPRESS_ZLIB) {
4054 ret = uncompress_inline(path, inode, page,
4055 pg_offset,
4056 extent_offset, item);
4057 BUG_ON(ret);
4058 } else {
4059 map = kmap(page);
4060 read_extent_buffer(leaf, map + pg_offset, ptr,
4061 copy_size);
4062 kunmap(page);
4063 }
Chris Mason179e29e2007-11-01 11:28:41 -04004064 flush_dcache_page(page);
4065 } else if (create && PageUptodate(page)) {
4066 if (!trans) {
4067 kunmap(page);
4068 free_extent_map(em);
4069 em = NULL;
4070 btrfs_release_path(root, path);
Chris Masonf9295742008-07-17 12:54:14 -04004071 trans = btrfs_join_transaction(root, 1);
Chris Mason179e29e2007-11-01 11:28:41 -04004072 goto again;
4073 }
Chris Masonc8b97812008-10-29 14:49:59 -04004074 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05004075 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04004076 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04004077 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04004078 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04004079 }
Chris Masond1310b22008-01-24 16:13:08 -05004080 set_extent_uptodate(io_tree, em->start,
4081 extent_map_end(em) - 1, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04004082 goto insert;
4083 } else {
Chris Masond3977122009-01-05 21:25:51 -05004084 printk(KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04004085 WARN_ON(1);
4086 }
4087not_found:
4088 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05004089 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04004090not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04004091 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04004092 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04004093insert:
4094 btrfs_release_path(root, path);
Chris Masond1310b22008-01-24 16:13:08 -05004095 if (em->start > start || extent_map_end(em) <= start) {
Chris Masond3977122009-01-05 21:25:51 -05004096 printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
4097 "[%llu %llu]\n", (unsigned long long)em->start,
4098 (unsigned long long)em->len,
4099 (unsigned long long)start,
4100 (unsigned long long)len);
Chris Masona52d9a82007-08-27 16:49:44 -04004101 err = -EIO;
4102 goto out;
4103 }
Chris Masond1310b22008-01-24 16:13:08 -05004104
4105 err = 0;
4106 spin_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04004107 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04004108 /* it is possible that someone inserted the extent into the tree
4109 * while we had the lock dropped. It is also possible that
4110 * an overlapping map exists in the tree
4111 */
Chris Masona52d9a82007-08-27 16:49:44 -04004112 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04004113 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004114
4115 ret = 0;
4116
Chris Mason3b951512008-04-17 11:29:12 -04004117 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04004118 if (existing && (existing->start > start ||
4119 existing->start + existing->len <= start)) {
4120 free_extent_map(existing);
4121 existing = NULL;
4122 }
Chris Mason3b951512008-04-17 11:29:12 -04004123 if (!existing) {
4124 existing = lookup_extent_mapping(em_tree, em->start,
4125 em->len);
4126 if (existing) {
4127 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04004128 em, start,
4129 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04004130 free_extent_map(existing);
4131 if (err) {
4132 free_extent_map(em);
4133 em = NULL;
4134 }
4135 } else {
4136 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04004137 free_extent_map(em);
4138 em = NULL;
4139 }
4140 } else {
4141 free_extent_map(em);
4142 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004143 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04004144 }
Chris Masona52d9a82007-08-27 16:49:44 -04004145 }
Chris Masond1310b22008-01-24 16:13:08 -05004146 spin_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04004147out:
Chris Masonf4219502008-07-22 11:18:09 -04004148 if (path)
4149 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04004150 if (trans) {
4151 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05004152 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04004153 err = ret;
4154 }
Chris Masona52d9a82007-08-27 16:49:44 -04004155 if (err) {
4156 free_extent_map(em);
4157 WARN_ON(1);
4158 return ERR_PTR(err);
4159 }
4160 return em;
4161}
4162
Chris Mason16432982008-04-10 10:23:21 -04004163static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
4164 const struct iovec *iov, loff_t offset,
4165 unsigned long nr_segs)
4166{
Chris Masone1c4b742008-04-22 13:26:46 -04004167 return -EINVAL;
Chris Mason16432982008-04-10 10:23:21 -04004168}
4169
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004170static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
4171 __u64 start, __u64 len)
4172{
4173 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent);
4174}
4175
Chris Mason9ebefb182007-06-15 13:50:00 -04004176int btrfs_readpage(struct file *file, struct page *page)
4177{
Chris Masond1310b22008-01-24 16:13:08 -05004178 struct extent_io_tree *tree;
4179 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04004180 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04004181}
Chris Mason1832a6d2007-12-21 16:27:21 -05004182
Chris Mason39279cc2007-06-12 06:35:45 -04004183static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
4184{
Chris Masond1310b22008-01-24 16:13:08 -05004185 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04004186
4187
4188 if (current->flags & PF_MEMALLOC) {
4189 redirty_page_for_writepage(wbc, page);
4190 unlock_page(page);
4191 return 0;
4192 }
Chris Masond1310b22008-01-24 16:13:08 -05004193 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04004194 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
4195}
Chris Mason39279cc2007-06-12 06:35:45 -04004196
Chris Masonf4219502008-07-22 11:18:09 -04004197int btrfs_writepages(struct address_space *mapping,
4198 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04004199{
Chris Masond1310b22008-01-24 16:13:08 -05004200 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05004201
Chris Masond1310b22008-01-24 16:13:08 -05004202 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04004203 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
4204}
4205
Chris Mason3ab2fb52007-11-08 10:59:22 -05004206static int
4207btrfs_readpages(struct file *file, struct address_space *mapping,
4208 struct list_head *pages, unsigned nr_pages)
4209{
Chris Masond1310b22008-01-24 16:13:08 -05004210 struct extent_io_tree *tree;
4211 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05004212 return extent_readpages(tree, mapping, pages, nr_pages,
4213 btrfs_get_extent);
4214}
Chris Masone6dcd2d2008-07-17 12:53:50 -04004215static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04004216{
Chris Masond1310b22008-01-24 16:13:08 -05004217 struct extent_io_tree *tree;
4218 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04004219 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004220
Chris Masond1310b22008-01-24 16:13:08 -05004221 tree = &BTRFS_I(page->mapping->host)->io_tree;
4222 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05004223 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04004224 if (ret == 1) {
4225 ClearPagePrivate(page);
4226 set_page_private(page, 0);
4227 page_cache_release(page);
4228 }
4229 return ret;
4230}
Chris Mason39279cc2007-06-12 06:35:45 -04004231
Chris Masone6dcd2d2008-07-17 12:53:50 -04004232static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
4233{
Chris Mason98509cf2008-09-11 15:51:43 -04004234 if (PageWriteback(page) || PageDirty(page))
4235 return 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004236 return __btrfs_releasepage(page, gfp_flags);
4237}
4238
Chris Masona52d9a82007-08-27 16:49:44 -04004239static void btrfs_invalidatepage(struct page *page, unsigned long offset)
4240{
Chris Masond1310b22008-01-24 16:13:08 -05004241 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004242 struct btrfs_ordered_extent *ordered;
4243 u64 page_start = page_offset(page);
4244 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04004245
Chris Masone6dcd2d2008-07-17 12:53:50 -04004246 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05004247 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004248 if (offset) {
4249 btrfs_releasepage(page, GFP_NOFS);
4250 return;
4251 }
4252
4253 lock_extent(tree, page_start, page_end, GFP_NOFS);
4254 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
4255 page_offset(page));
4256 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04004257 /*
4258 * IO on this page will never be started, so we need
4259 * to account for any ordered extents now
4260 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04004261 clear_extent_bit(tree, page_start, page_end,
4262 EXTENT_DIRTY | EXTENT_DELALLOC |
4263 EXTENT_LOCKED, 1, 0, GFP_NOFS);
Chris Mason211f90e2008-07-18 11:56:15 -04004264 btrfs_finish_ordered_io(page->mapping->host,
4265 page_start, page_end);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004266 btrfs_put_ordered_extent(ordered);
4267 lock_extent(tree, page_start, page_end, GFP_NOFS);
4268 }
4269 clear_extent_bit(tree, page_start, page_end,
4270 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
4271 EXTENT_ORDERED,
4272 1, 1, GFP_NOFS);
4273 __btrfs_releasepage(page, GFP_NOFS);
4274
Chris Mason4a096752008-07-21 10:29:44 -04004275 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04004276 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04004277 ClearPagePrivate(page);
4278 set_page_private(page, 0);
4279 page_cache_release(page);
4280 }
Chris Mason39279cc2007-06-12 06:35:45 -04004281}
4282
Chris Mason9ebefb182007-06-15 13:50:00 -04004283/*
4284 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
4285 * called from a page fault handler when a page is first dirtied. Hence we must
4286 * be careful to check for EOF conditions here. We set the page up correctly
4287 * for a written page which means we get ENOSPC checking when writing into
4288 * holes and correct delalloc and unwritten extent mapping on filesystems that
4289 * support these features.
4290 *
4291 * We are not allowed to take the i_mutex here so we have to play games to
4292 * protect against truncate races as the page could now be beyond EOF. Because
4293 * vmtruncate() writes the inode size before removing pages, once we have the
4294 * page lock we can determine safely if the page is beyond EOF. If it is not
4295 * beyond EOF, then the page is guaranteed safe against truncation until we
4296 * unlock the page.
4297 */
4298int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
4299{
Chris Mason6da6aba2007-12-18 16:15:09 -05004300 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05004301 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004302 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4303 struct btrfs_ordered_extent *ordered;
4304 char *kaddr;
4305 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04004306 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05004307 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04004308 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004309 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04004310
Chris Mason1832a6d2007-12-21 16:27:21 -05004311 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
Chris Mason1832a6d2007-12-21 16:27:21 -05004312 if (ret)
4313 goto out;
4314
4315 ret = -EINVAL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004316again:
Chris Mason9ebefb182007-06-15 13:50:00 -04004317 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04004318 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004319 page_start = page_offset(page);
4320 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04004321
Chris Mason9ebefb182007-06-15 13:50:00 -04004322 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04004323 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04004324 /* page got truncated out from underneath us */
4325 goto out_unlock;
4326 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004327 wait_on_page_writeback(page);
4328
4329 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
4330 set_page_extent_mapped(page);
4331
Chris Masoneb84ae02008-07-17 13:53:27 -04004332 /*
4333 * we can't set the delalloc bits if there are pending ordered
4334 * extents. Drop our locks and wait for them to finish
4335 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04004336 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4337 if (ordered) {
4338 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
4339 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004340 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004341 btrfs_put_ordered_extent(ordered);
4342 goto again;
4343 }
4344
Chris Masonea8c2812008-08-04 23:17:27 -04004345 btrfs_set_extent_delalloc(inode, page_start, page_end);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004346 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04004347
4348 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04004349 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04004350 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04004351 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04004352 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04004353
Chris Masone6dcd2d2008-07-17 12:53:50 -04004354 if (zero_start != PAGE_CACHE_SIZE) {
4355 kaddr = kmap(page);
4356 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
4357 flush_dcache_page(page);
4358 kunmap(page);
4359 }
Chris Mason247e7432008-07-17 12:53:51 -04004360 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004361 set_page_dirty(page);
4362 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04004363
4364out_unlock:
4365 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05004366out:
Chris Mason9ebefb182007-06-15 13:50:00 -04004367 return ret;
4368}
4369
Chris Mason39279cc2007-06-12 06:35:45 -04004370static void btrfs_truncate(struct inode *inode)
4371{
4372 struct btrfs_root *root = BTRFS_I(inode)->root;
4373 int ret;
4374 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004375 unsigned long nr;
Chris Masondbe674a2008-07-17 12:54:05 -04004376 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04004377
4378 if (!S_ISREG(inode->i_mode))
4379 return;
4380 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
4381 return;
4382
4383 btrfs_truncate_page(inode->i_mapping, inode->i_size);
Chris Mason4a096752008-07-21 10:29:44 -04004384 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04004385
Chris Mason39279cc2007-06-12 06:35:45 -04004386 trans = btrfs_start_transaction(root, 1);
4387 btrfs_set_trans_block_group(trans, inode);
Chris Masondbe674a2008-07-17 12:54:05 -04004388 btrfs_i_size_write(inode, inode->i_size);
Chris Mason39279cc2007-06-12 06:35:45 -04004389
Josef Bacik7b128762008-07-24 12:17:14 -04004390 ret = btrfs_orphan_add(trans, inode);
4391 if (ret)
4392 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04004393 /* FIXME, add redo link to tree so we don't leak on crash */
Chris Masone02119d2008-09-05 16:13:11 -04004394 ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size,
Chris Mason85e21ba2008-01-29 15:11:36 -05004395 BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004396 btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004397
Josef Bacik7b128762008-07-24 12:17:14 -04004398 ret = btrfs_orphan_del(trans, inode);
4399 BUG_ON(ret);
4400
4401out:
4402 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04004403 ret = btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004404 BUG_ON(ret);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004405 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004406}
4407
Sven Wegener3b963622008-06-09 21:57:42 -04004408/*
Chris Masond352ac62008-09-29 15:18:18 -04004409 * create a new subvolume directory/inode (helper for the ioctl).
4410 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05004411int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
4412 struct btrfs_root *new_root, struct dentry *dentry,
4413 u64 new_dirid, u64 alloc_hint)
Chris Mason39279cc2007-06-12 06:35:45 -04004414{
Chris Mason39279cc2007-06-12 06:35:45 -04004415 struct inode *inode;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04004416 int error;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004417 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004418
Josef Bacikaec74772008-07-24 12:12:38 -04004419 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
Yan Zhengd2fb3432008-12-11 16:30:39 -05004420 new_dirid, alloc_hint, S_IFDIR | 0700, &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04004421 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004422 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004423 inode->i_op = &btrfs_dir_inode_operations;
4424 inode->i_fop = &btrfs_dir_file_operations;
4425
Chris Mason39279cc2007-06-12 06:35:45 -04004426 inode->i_nlink = 1;
Chris Masondbe674a2008-07-17 12:54:05 -04004427 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04004428
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04004429 error = btrfs_update_inode(trans, new_root, inode);
4430 if (error)
4431 return error;
4432
4433 d_instantiate(dentry, inode);
4434 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004435}
4436
Chris Masond352ac62008-09-29 15:18:18 -04004437/* helper function for file defrag and space balancing. This
4438 * forces readahead on a given range of bytes in an inode
4439 */
Chris Masonedbd8d42007-12-21 16:27:24 -05004440unsigned long btrfs_force_ra(struct address_space *mapping,
Chris Mason86479a02007-09-10 19:58:16 -04004441 struct file_ra_state *ra, struct file *file,
4442 pgoff_t offset, pgoff_t last_index)
4443{
Chris Mason8e7bf942008-04-28 09:02:36 -04004444 pgoff_t req_size = last_index - offset + 1;
Chris Mason86479a02007-09-10 19:58:16 -04004445
Chris Mason86479a02007-09-10 19:58:16 -04004446 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
4447 return offset + req_size;
Chris Mason86479a02007-09-10 19:58:16 -04004448}
4449
Chris Mason39279cc2007-06-12 06:35:45 -04004450struct inode *btrfs_alloc_inode(struct super_block *sb)
4451{
4452 struct btrfs_inode *ei;
4453
4454 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
4455 if (!ei)
4456 return NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04004457 ei->last_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004458 ei->logged_trans = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004459 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Josef Bacik33268ea2008-07-24 12:16:36 -04004460 ei->i_acl = BTRFS_ACL_NOT_CACHED;
4461 ei->i_default_acl = BTRFS_ACL_NOT_CACHED;
Josef Bacik7b128762008-07-24 12:17:14 -04004462 INIT_LIST_HEAD(&ei->i_orphan);
Chris Mason39279cc2007-06-12 06:35:45 -04004463 return &ei->vfs_inode;
4464}
4465
4466void btrfs_destroy_inode(struct inode *inode)
4467{
Chris Masone6dcd2d2008-07-17 12:53:50 -04004468 struct btrfs_ordered_extent *ordered;
Chris Mason39279cc2007-06-12 06:35:45 -04004469 WARN_ON(!list_empty(&inode->i_dentry));
4470 WARN_ON(inode->i_data.nrpages);
4471
Josef Bacik33268ea2008-07-24 12:16:36 -04004472 if (BTRFS_I(inode)->i_acl &&
4473 BTRFS_I(inode)->i_acl != BTRFS_ACL_NOT_CACHED)
4474 posix_acl_release(BTRFS_I(inode)->i_acl);
4475 if (BTRFS_I(inode)->i_default_acl &&
4476 BTRFS_I(inode)->i_default_acl != BTRFS_ACL_NOT_CACHED)
4477 posix_acl_release(BTRFS_I(inode)->i_default_acl);
4478
Yanbcc63ab2008-07-30 16:29:20 -04004479 spin_lock(&BTRFS_I(inode)->root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04004480 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
4481 printk(KERN_ERR "BTRFS: inode %lu: inode still on the orphan"
4482 " list\n", inode->i_ino);
4483 dump_stack();
4484 }
Yanbcc63ab2008-07-30 16:29:20 -04004485 spin_unlock(&BTRFS_I(inode)->root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04004486
Chris Masond3977122009-01-05 21:25:51 -05004487 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04004488 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
4489 if (!ordered)
4490 break;
4491 else {
Chris Masond3977122009-01-05 21:25:51 -05004492 printk(KERN_ERR "btrfs found ordered "
4493 "extent %llu %llu on inode cleanup\n",
4494 (unsigned long long)ordered->file_offset,
4495 (unsigned long long)ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004496 btrfs_remove_ordered_extent(inode, ordered);
4497 btrfs_put_ordered_extent(ordered);
4498 btrfs_put_ordered_extent(ordered);
4499 }
4500 }
Zheng Yan5b21f2e2008-09-26 10:05:38 -04004501 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004502 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
4503}
4504
Sven Wegener0ee0fda2008-07-30 16:54:26 -04004505static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04004506{
4507 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
4508
4509 inode_init_once(&ei->vfs_inode);
4510}
4511
4512void btrfs_destroy_cachep(void)
4513{
4514 if (btrfs_inode_cachep)
4515 kmem_cache_destroy(btrfs_inode_cachep);
4516 if (btrfs_trans_handle_cachep)
4517 kmem_cache_destroy(btrfs_trans_handle_cachep);
4518 if (btrfs_transaction_cachep)
4519 kmem_cache_destroy(btrfs_transaction_cachep);
4520 if (btrfs_bit_radix_cachep)
4521 kmem_cache_destroy(btrfs_bit_radix_cachep);
4522 if (btrfs_path_cachep)
4523 kmem_cache_destroy(btrfs_path_cachep);
4524}
4525
Chris Mason86479a02007-09-10 19:58:16 -04004526struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
Chris Mason92fee662007-07-25 12:31:35 -04004527 unsigned long extra_flags,
Chris Mason2b1f55b2008-09-24 11:48:04 -04004528 void (*ctor)(void *))
Chris Mason92fee662007-07-25 12:31:35 -04004529{
4530 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
Chris Mason2b1f55b2008-09-24 11:48:04 -04004531 SLAB_MEM_SPREAD | extra_flags), ctor);
Chris Mason92fee662007-07-25 12:31:35 -04004532}
4533
Chris Mason39279cc2007-06-12 06:35:45 -04004534int btrfs_init_cachep(void)
4535{
Chris Mason86479a02007-09-10 19:58:16 -04004536 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
Chris Mason92fee662007-07-25 12:31:35 -04004537 sizeof(struct btrfs_inode),
4538 0, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04004539 if (!btrfs_inode_cachep)
4540 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04004541 btrfs_trans_handle_cachep =
4542 btrfs_cache_create("btrfs_trans_handle_cache",
4543 sizeof(struct btrfs_trans_handle),
4544 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004545 if (!btrfs_trans_handle_cachep)
4546 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04004547 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
Chris Mason39279cc2007-06-12 06:35:45 -04004548 sizeof(struct btrfs_transaction),
Chris Mason92fee662007-07-25 12:31:35 -04004549 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004550 if (!btrfs_transaction_cachep)
4551 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04004552 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
Yan23223582007-09-17 11:08:52 -04004553 sizeof(struct btrfs_path),
Chris Mason92fee662007-07-25 12:31:35 -04004554 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004555 if (!btrfs_path_cachep)
4556 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04004557 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
Chris Mason92fee662007-07-25 12:31:35 -04004558 SLAB_DESTROY_BY_RCU, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004559 if (!btrfs_bit_radix_cachep)
4560 goto fail;
4561 return 0;
4562fail:
4563 btrfs_destroy_cachep();
4564 return -ENOMEM;
4565}
4566
4567static int btrfs_getattr(struct vfsmount *mnt,
4568 struct dentry *dentry, struct kstat *stat)
4569{
4570 struct inode *inode = dentry->d_inode;
4571 generic_fillattr(inode, stat);
Chris Mason3394e162008-11-17 20:42:26 -05004572 stat->dev = BTRFS_I(inode)->root->anon_super.s_dev;
Chris Masond6667462008-01-03 14:51:00 -05004573 stat->blksize = PAGE_CACHE_SIZE;
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004574 stat->blocks = (inode_get_bytes(inode) +
4575 BTRFS_I(inode)->delalloc_bytes) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04004576 return 0;
4577}
4578
Chris Masond3977122009-01-05 21:25:51 -05004579static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
4580 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04004581{
4582 struct btrfs_trans_handle *trans;
4583 struct btrfs_root *root = BTRFS_I(old_dir)->root;
4584 struct inode *new_inode = new_dentry->d_inode;
4585 struct inode *old_inode = old_dentry->d_inode;
4586 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004587 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004588 int ret;
4589
Chris Mason3394e162008-11-17 20:42:26 -05004590 /* we're not allowed to rename between subvolumes */
4591 if (BTRFS_I(old_inode)->root->root_key.objectid !=
4592 BTRFS_I(new_dir)->root->root_key.objectid)
4593 return -EXDEV;
4594
Chris Mason39279cc2007-06-12 06:35:45 -04004595 if (S_ISDIR(old_inode->i_mode) && new_inode &&
4596 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
4597 return -ENOTEMPTY;
4598 }
Chris Mason5f39d392007-10-15 16:14:19 -04004599
Chris Mason0660b5a2008-11-17 20:37:39 -05004600 /* to rename a snapshot or subvolume, we need to juggle the
4601 * backrefs. This isn't coded yet
4602 */
4603 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
4604 return -EXDEV;
4605
Chris Mason1832a6d2007-12-21 16:27:21 -05004606 ret = btrfs_check_free_space(root, 1, 0);
4607 if (ret)
4608 goto out_unlock;
4609
Chris Mason39279cc2007-06-12 06:35:45 -04004610 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04004611
Chris Mason39279cc2007-06-12 06:35:45 -04004612 btrfs_set_trans_block_group(trans, new_dir);
Chris Mason39279cc2007-06-12 06:35:45 -04004613
Chris Masone02119d2008-09-05 16:13:11 -04004614 btrfs_inc_nlink(old_dentry->d_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004615 old_dir->i_ctime = old_dir->i_mtime = ctime;
4616 new_dir->i_ctime = new_dir->i_mtime = ctime;
4617 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04004618
Chris Masone02119d2008-09-05 16:13:11 -04004619 ret = btrfs_unlink_inode(trans, root, old_dir, old_dentry->d_inode,
4620 old_dentry->d_name.name,
4621 old_dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04004622 if (ret)
4623 goto out_fail;
4624
4625 if (new_inode) {
4626 new_inode->i_ctime = CURRENT_TIME;
Chris Masone02119d2008-09-05 16:13:11 -04004627 ret = btrfs_unlink_inode(trans, root, new_dir,
4628 new_dentry->d_inode,
4629 new_dentry->d_name.name,
4630 new_dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04004631 if (ret)
4632 goto out_fail;
Josef Bacik7b128762008-07-24 12:17:14 -04004633 if (new_inode->i_nlink == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004634 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Josef Bacik7b128762008-07-24 12:17:14 -04004635 if (ret)
4636 goto out_fail;
4637 }
Chris Masone02119d2008-09-05 16:13:11 -04004638
Chris Mason39279cc2007-06-12 06:35:45 -04004639 }
Chris Mason3de45862008-11-17 21:02:50 -05004640 ret = btrfs_set_inode_index(new_dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004641 if (ret)
4642 goto out_fail;
4643
Chris Masone02119d2008-09-05 16:13:11 -04004644 ret = btrfs_add_link(trans, new_dentry->d_parent->d_inode,
4645 old_inode, new_dentry->d_name.name,
4646 new_dentry->d_name.len, 1, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004647 if (ret)
4648 goto out_fail;
4649
4650out_fail:
Chris Masonab78c842008-07-29 16:15:18 -04004651 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05004652out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04004653 return ret;
4654}
4655
Chris Masond352ac62008-09-29 15:18:18 -04004656/*
4657 * some fairly slow code that needs optimization. This walks the list
4658 * of all the inodes with pending delalloc and forces them to disk.
4659 */
Chris Masonea8c2812008-08-04 23:17:27 -04004660int btrfs_start_delalloc_inodes(struct btrfs_root *root)
4661{
4662 struct list_head *head = &root->fs_info->delalloc_inodes;
4663 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04004664 struct inode *inode;
Chris Masonea8c2812008-08-04 23:17:27 -04004665
Yan Zhengc146afa2008-11-12 14:34:12 -05004666 if (root->fs_info->sb->s_flags & MS_RDONLY)
4667 return -EROFS;
4668
Chris Mason75eff682008-12-15 15:54:40 -05004669 spin_lock(&root->fs_info->delalloc_lock);
Chris Masond3977122009-01-05 21:25:51 -05004670 while (!list_empty(head)) {
Chris Masonea8c2812008-08-04 23:17:27 -04004671 binode = list_entry(head->next, struct btrfs_inode,
4672 delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04004673 inode = igrab(&binode->vfs_inode);
4674 if (!inode)
4675 list_del_init(&binode->delalloc_inodes);
Chris Mason75eff682008-12-15 15:54:40 -05004676 spin_unlock(&root->fs_info->delalloc_lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04004677 if (inode) {
Chris Mason8c8bee1d2008-09-29 11:19:10 -04004678 filemap_flush(inode->i_mapping);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04004679 iput(inode);
4680 }
4681 cond_resched();
Chris Mason75eff682008-12-15 15:54:40 -05004682 spin_lock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04004683 }
Chris Mason75eff682008-12-15 15:54:40 -05004684 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason8c8bee1d2008-09-29 11:19:10 -04004685
4686 /* the filemap_flush will queue IO into the worker threads, but
4687 * we have to make sure the IO is actually started and that
4688 * ordered extents get created before we return
4689 */
4690 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05004691 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05004692 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee1d2008-09-29 11:19:10 -04004693 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05004694 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
4695 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee1d2008-09-29 11:19:10 -04004696 }
4697 atomic_dec(&root->fs_info->async_submit_draining);
Chris Masonea8c2812008-08-04 23:17:27 -04004698 return 0;
4699}
4700
Chris Mason39279cc2007-06-12 06:35:45 -04004701static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
4702 const char *symname)
4703{
4704 struct btrfs_trans_handle *trans;
4705 struct btrfs_root *root = BTRFS_I(dir)->root;
4706 struct btrfs_path *path;
4707 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05004708 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04004709 int err;
4710 int drop_inode = 0;
4711 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004712 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04004713 int name_len;
4714 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04004715 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04004716 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04004717 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05004718 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004719
4720 name_len = strlen(symname) + 1;
4721 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
4722 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05004723
Chris Mason1832a6d2007-12-21 16:27:21 -05004724 err = btrfs_check_free_space(root, 1, 0);
4725 if (err)
4726 goto out_fail;
4727
Chris Mason39279cc2007-06-12 06:35:45 -04004728 trans = btrfs_start_transaction(root, 1);
4729 btrfs_set_trans_block_group(trans, dir);
4730
4731 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
4732 if (err) {
4733 err = -ENOSPC;
4734 goto out_unlock;
4735 }
4736
Josef Bacikaec74772008-07-24 12:12:38 -04004737 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05004738 dentry->d_name.len,
4739 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04004740 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO,
4741 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04004742 err = PTR_ERR(inode);
4743 if (IS_ERR(inode))
4744 goto out_unlock;
4745
Jim Owens0279b4c2009-02-04 09:29:13 -05004746 err = btrfs_init_inode_security(inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04004747 if (err) {
4748 drop_inode = 1;
4749 goto out_unlock;
4750 }
4751
Chris Mason39279cc2007-06-12 06:35:45 -04004752 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04004753 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004754 if (err)
4755 drop_inode = 1;
4756 else {
4757 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04004758 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04004759 inode->i_fop = &btrfs_file_operations;
4760 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05004761 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04004762 }
4763 dir->i_sb->s_dirt = 1;
4764 btrfs_update_inode_block_group(trans, inode);
4765 btrfs_update_inode_block_group(trans, dir);
4766 if (drop_inode)
4767 goto out_unlock;
4768
4769 path = btrfs_alloc_path();
4770 BUG_ON(!path);
4771 key.objectid = inode->i_ino;
4772 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004773 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
4774 datasize = btrfs_file_extent_calc_inline_size(name_len);
4775 err = btrfs_insert_empty_item(trans, root, path, &key,
4776 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04004777 if (err) {
4778 drop_inode = 1;
4779 goto out_unlock;
4780 }
Chris Mason5f39d392007-10-15 16:14:19 -04004781 leaf = path->nodes[0];
4782 ei = btrfs_item_ptr(leaf, path->slots[0],
4783 struct btrfs_file_extent_item);
4784 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
4785 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04004786 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04004787 btrfs_set_file_extent_encryption(leaf, ei, 0);
4788 btrfs_set_file_extent_compression(leaf, ei, 0);
4789 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
4790 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
4791
Chris Mason39279cc2007-06-12 06:35:45 -04004792 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04004793 write_extent_buffer(leaf, symname, ptr, name_len);
4794 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004795 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04004796
Chris Mason39279cc2007-06-12 06:35:45 -04004797 inode->i_op = &btrfs_symlink_inode_operations;
4798 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04004799 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04004800 inode_set_bytes(inode, name_len);
Chris Masondbe674a2008-07-17 12:54:05 -04004801 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04004802 err = btrfs_update_inode(trans, root, inode);
4803 if (err)
4804 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04004805
4806out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004807 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04004808 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05004809out_fail:
Chris Mason39279cc2007-06-12 06:35:45 -04004810 if (drop_inode) {
4811 inode_dec_link_count(inode);
4812 iput(inode);
4813 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004814 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004815 return err;
4816}
Chris Mason16432982008-04-10 10:23:21 -04004817
Yan Zhengd899e052008-10-30 14:25:28 -04004818static int prealloc_file_range(struct inode *inode, u64 start, u64 end,
4819 u64 alloc_hint, int mode)
4820{
4821 struct btrfs_trans_handle *trans;
4822 struct btrfs_root *root = BTRFS_I(inode)->root;
4823 struct btrfs_key ins;
4824 u64 alloc_size;
4825 u64 cur_offset = start;
4826 u64 num_bytes = end - start;
4827 int ret = 0;
4828
4829 trans = btrfs_join_transaction(root, 1);
4830 BUG_ON(!trans);
4831 btrfs_set_trans_block_group(trans, inode);
4832
4833 while (num_bytes > 0) {
4834 alloc_size = min(num_bytes, root->fs_info->max_extent);
4835 ret = btrfs_reserve_extent(trans, root, alloc_size,
4836 root->sectorsize, 0, alloc_hint,
4837 (u64)-1, &ins, 1);
4838 if (ret) {
4839 WARN_ON(1);
4840 goto out;
4841 }
4842 ret = insert_reserved_file_extent(trans, inode,
4843 cur_offset, ins.objectid,
4844 ins.offset, ins.offset,
4845 ins.offset, 0, 0, 0,
4846 BTRFS_FILE_EXTENT_PREALLOC);
4847 BUG_ON(ret);
4848 num_bytes -= ins.offset;
4849 cur_offset += ins.offset;
4850 alloc_hint = ins.objectid + ins.offset;
4851 }
4852out:
4853 if (cur_offset > start) {
4854 inode->i_ctime = CURRENT_TIME;
4855 btrfs_set_flag(inode, PREALLOC);
4856 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
4857 cur_offset > i_size_read(inode))
4858 btrfs_i_size_write(inode, cur_offset);
4859 ret = btrfs_update_inode(trans, root, inode);
4860 BUG_ON(ret);
4861 }
4862
4863 btrfs_end_transaction(trans, root);
4864 return ret;
4865}
4866
4867static long btrfs_fallocate(struct inode *inode, int mode,
4868 loff_t offset, loff_t len)
4869{
4870 u64 cur_offset;
4871 u64 last_byte;
4872 u64 alloc_start;
4873 u64 alloc_end;
4874 u64 alloc_hint = 0;
4875 u64 mask = BTRFS_I(inode)->root->sectorsize - 1;
4876 struct extent_map *em;
4877 int ret;
4878
4879 alloc_start = offset & ~mask;
4880 alloc_end = (offset + len + mask) & ~mask;
4881
4882 mutex_lock(&inode->i_mutex);
4883 if (alloc_start > inode->i_size) {
4884 ret = btrfs_cont_expand(inode, alloc_start);
4885 if (ret)
4886 goto out;
4887 }
4888
4889 while (1) {
4890 struct btrfs_ordered_extent *ordered;
4891 lock_extent(&BTRFS_I(inode)->io_tree, alloc_start,
4892 alloc_end - 1, GFP_NOFS);
4893 ordered = btrfs_lookup_first_ordered_extent(inode,
4894 alloc_end - 1);
4895 if (ordered &&
4896 ordered->file_offset + ordered->len > alloc_start &&
4897 ordered->file_offset < alloc_end) {
4898 btrfs_put_ordered_extent(ordered);
4899 unlock_extent(&BTRFS_I(inode)->io_tree,
4900 alloc_start, alloc_end - 1, GFP_NOFS);
4901 btrfs_wait_ordered_range(inode, alloc_start,
4902 alloc_end - alloc_start);
4903 } else {
4904 if (ordered)
4905 btrfs_put_ordered_extent(ordered);
4906 break;
4907 }
4908 }
4909
4910 cur_offset = alloc_start;
4911 while (1) {
4912 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4913 alloc_end - cur_offset, 0);
4914 BUG_ON(IS_ERR(em) || !em);
4915 last_byte = min(extent_map_end(em), alloc_end);
4916 last_byte = (last_byte + mask) & ~mask;
4917 if (em->block_start == EXTENT_MAP_HOLE) {
4918 ret = prealloc_file_range(inode, cur_offset,
4919 last_byte, alloc_hint, mode);
4920 if (ret < 0) {
4921 free_extent_map(em);
4922 break;
4923 }
4924 }
4925 if (em->block_start <= EXTENT_MAP_LAST_BYTE)
4926 alloc_hint = em->block_start;
4927 free_extent_map(em);
4928
4929 cur_offset = last_byte;
4930 if (cur_offset >= alloc_end) {
4931 ret = 0;
4932 break;
4933 }
4934 }
4935 unlock_extent(&BTRFS_I(inode)->io_tree, alloc_start, alloc_end - 1,
4936 GFP_NOFS);
4937out:
4938 mutex_unlock(&inode->i_mutex);
4939 return ret;
4940}
4941
Chris Masone6dcd2d2008-07-17 12:53:50 -04004942static int btrfs_set_page_dirty(struct page *page)
4943{
Chris Masone6dcd2d2008-07-17 12:53:50 -04004944 return __set_page_dirty_nobuffers(page);
4945}
4946
Sven Wegener0ee0fda2008-07-30 16:54:26 -04004947static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05004948{
4949 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
4950 return -EACCES;
Josef Bacik33268ea2008-07-24 12:16:36 -04004951 return generic_permission(inode, mask, btrfs_check_acl);
Yanfdebe2b2008-01-14 13:26:08 -05004952}
Chris Mason39279cc2007-06-12 06:35:45 -04004953
4954static struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05004955 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04004956 .lookup = btrfs_lookup,
4957 .create = btrfs_create,
4958 .unlink = btrfs_unlink,
4959 .link = btrfs_link,
4960 .mkdir = btrfs_mkdir,
4961 .rmdir = btrfs_rmdir,
4962 .rename = btrfs_rename,
4963 .symlink = btrfs_symlink,
4964 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04004965 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04004966 .setxattr = btrfs_setxattr,
4967 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05004968 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04004969 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05004970 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04004971};
Chris Mason39279cc2007-06-12 06:35:45 -04004972static struct inode_operations btrfs_dir_ro_inode_operations = {
4973 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05004974 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04004975};
Chris Mason39279cc2007-06-12 06:35:45 -04004976static struct file_operations btrfs_dir_file_operations = {
4977 .llseek = generic_file_llseek,
4978 .read = generic_read_dir,
David Woodhousecbdf5a22008-08-06 19:42:33 +01004979 .readdir = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04004980 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04004981#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04004982 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04004983#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04004984 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04004985 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04004986};
4987
Chris Masond1310b22008-01-24 16:13:08 -05004988static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04004989 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05004990 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04004991 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04004992 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04004993 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04004994 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Mason1259ab72008-05-12 13:39:03 -04004995 .readpage_io_failed_hook = btrfs_io_failed_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05004996 .set_bit_hook = btrfs_set_bit_hook,
4997 .clear_bit_hook = btrfs_clear_bit_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04004998};
4999
Chris Mason35054392009-01-21 13:11:13 -05005000/*
5001 * btrfs doesn't support the bmap operation because swapfiles
5002 * use bmap to make a mapping of extents in the file. They assume
5003 * these extents won't change over the life of the file and they
5004 * use the bmap result to do IO directly to the drive.
5005 *
5006 * the btrfs bmap call would return logical addresses that aren't
5007 * suitable for IO and they also will change frequently as COW
5008 * operations happen. So, swapfile + btrfs == corruption.
5009 *
5010 * For now we're avoiding this by dropping bmap.
5011 */
Chris Mason39279cc2007-06-12 06:35:45 -04005012static struct address_space_operations btrfs_aops = {
5013 .readpage = btrfs_readpage,
5014 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04005015 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05005016 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04005017 .sync_page = block_sync_page,
Chris Mason16432982008-04-10 10:23:21 -04005018 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04005019 .invalidatepage = btrfs_invalidatepage,
5020 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005021 .set_page_dirty = btrfs_set_page_dirty,
Chris Mason39279cc2007-06-12 06:35:45 -04005022};
5023
5024static struct address_space_operations btrfs_symlink_aops = {
5025 .readpage = btrfs_readpage,
5026 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04005027 .invalidatepage = btrfs_invalidatepage,
5028 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04005029};
5030
5031static struct inode_operations btrfs_file_inode_operations = {
5032 .truncate = btrfs_truncate,
5033 .getattr = btrfs_getattr,
5034 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04005035 .setxattr = btrfs_setxattr,
5036 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05005037 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04005038 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05005039 .permission = btrfs_permission,
Yan Zhengd899e052008-10-30 14:25:28 -04005040 .fallocate = btrfs_fallocate,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05005041 .fiemap = btrfs_fiemap,
Chris Mason39279cc2007-06-12 06:35:45 -04005042};
Josef Bacik618e21d2007-07-11 10:18:17 -04005043static struct inode_operations btrfs_special_inode_operations = {
5044 .getattr = btrfs_getattr,
5045 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05005046 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04005047 .setxattr = btrfs_setxattr,
5048 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04005049 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04005050 .removexattr = btrfs_removexattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04005051};
Chris Mason39279cc2007-06-12 06:35:45 -04005052static struct inode_operations btrfs_symlink_inode_operations = {
5053 .readlink = generic_readlink,
5054 .follow_link = page_follow_link_light,
5055 .put_link = page_put_link,
Yanfdebe2b2008-01-14 13:26:08 -05005056 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05005057 .setxattr = btrfs_setxattr,
5058 .getxattr = btrfs_getxattr,
5059 .listxattr = btrfs_listxattr,
5060 .removexattr = btrfs_removexattr,
Chris Mason39279cc2007-06-12 06:35:45 -04005061};