blob: 0577e77e661d9a15177f531241d703f2fb33c604 [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>
Chris Mason92fee662007-07-25 12:31:35 -040037#include <linux/version.h>
Josef Bacik5103e942007-11-16 11:45:54 -050038#include <linux/xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040039#include <linux/posix_acl.h>
Yan Zhengd899e052008-10-30 14:25:28 -040040#include <linux/falloc.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050041#include "compat.h"
Chris Mason39279cc2007-06-12 06:35:45 -040042#include "ctree.h"
43#include "disk-io.h"
44#include "transaction.h"
45#include "btrfs_inode.h"
46#include "ioctl.h"
47#include "print-tree.h"
Chris Mason0b86a832008-03-24 15:01:56 -040048#include "volumes.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040049#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040050#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040051#include "tree-log.h"
Yan Zheng5b84e8d2008-10-09 11:46:19 -040052#include "ref-cache.h"
Chris Masonc8b97812008-10-29 14:49:59 -040053#include "compression.h"
Chris Mason39279cc2007-06-12 06:35:45 -040054
55struct btrfs_iget_args {
56 u64 ino;
57 struct btrfs_root *root;
58};
59
60static struct inode_operations btrfs_dir_inode_operations;
61static struct inode_operations btrfs_symlink_inode_operations;
62static struct inode_operations btrfs_dir_ro_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -040063static struct inode_operations btrfs_special_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -040064static struct inode_operations btrfs_file_inode_operations;
65static struct address_space_operations btrfs_aops;
66static struct address_space_operations btrfs_symlink_aops;
67static struct file_operations btrfs_dir_file_operations;
Chris Masond1310b22008-01-24 16:13:08 -050068static struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040069
70static struct kmem_cache *btrfs_inode_cachep;
71struct kmem_cache *btrfs_trans_handle_cachep;
72struct kmem_cache *btrfs_transaction_cachep;
73struct kmem_cache *btrfs_bit_radix_cachep;
74struct kmem_cache *btrfs_path_cachep;
75
76#define S_SHIFT 12
77static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
78 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
79 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
80 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
81 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
82 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
83 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
84 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
85};
86
Josef Bacik7b128762008-07-24 12:17:14 -040087static void btrfs_truncate(struct inode *inode);
Chris Masonc8b97812008-10-29 14:49:59 -040088static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end);
Chris Mason771ed682008-11-06 22:02:51 -050089static noinline int cow_file_range(struct inode *inode,
90 struct page *locked_page,
91 u64 start, u64 end, int *page_started,
92 unsigned long *nr_written, int unlock);
Josef Bacik7b128762008-07-24 12:17:14 -040093
Chris Masond352ac62008-09-29 15:18:18 -040094/*
95 * a very lame attempt at stopping writes when the FS is 85% full. There
96 * are countless ways this is incorrect, but it is better than nothing.
97 */
Chris Mason1832a6d2007-12-21 16:27:21 -050098int btrfs_check_free_space(struct btrfs_root *root, u64 num_required,
99 int for_del)
100{
Chris Masona2135012008-06-25 16:01:30 -0400101 u64 total;
102 u64 used;
Chris Mason1832a6d2007-12-21 16:27:21 -0500103 u64 thresh;
104 int ret = 0;
105
Chris Mason75eff682008-12-15 15:54:40 -0500106 spin_lock(&root->fs_info->delalloc_lock);
Chris Masona2135012008-06-25 16:01:30 -0400107 total = btrfs_super_total_bytes(&root->fs_info->super_copy);
108 used = btrfs_super_bytes_used(&root->fs_info->super_copy);
Chris Mason1832a6d2007-12-21 16:27:21 -0500109 if (for_del)
Chris Masonf9ef6602008-01-03 09:22:38 -0500110 thresh = total * 90;
Chris Mason1832a6d2007-12-21 16:27:21 -0500111 else
Chris Masonf9ef6602008-01-03 09:22:38 -0500112 thresh = total * 85;
113
114 do_div(thresh, 100);
Chris Mason1832a6d2007-12-21 16:27:21 -0500115
Chris Mason1832a6d2007-12-21 16:27:21 -0500116 if (used + root->fs_info->delalloc_bytes + num_required > thresh)
117 ret = -ENOSPC;
Chris Mason75eff682008-12-15 15:54:40 -0500118 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason1832a6d2007-12-21 16:27:21 -0500119 return ret;
120}
121
Chris Masond352ac62008-09-29 15:18:18 -0400122/*
Chris Masonc8b97812008-10-29 14:49:59 -0400123 * this does all the hard work for inserting an inline extent into
124 * the btree. The caller should have done a btrfs_drop_extents so that
125 * no overlapping inline items exist in the btree
126 */
127static int noinline insert_inline_extent(struct btrfs_trans_handle *trans,
128 struct btrfs_root *root, struct inode *inode,
129 u64 start, size_t size, size_t compressed_size,
130 struct page **compressed_pages)
131{
132 struct btrfs_key key;
133 struct btrfs_path *path;
134 struct extent_buffer *leaf;
135 struct page *page = NULL;
136 char *kaddr;
137 unsigned long ptr;
138 struct btrfs_file_extent_item *ei;
139 int err = 0;
140 int ret;
141 size_t cur_size = size;
142 size_t datasize;
143 unsigned long offset;
144 int use_compress = 0;
145
146 if (compressed_size && compressed_pages) {
147 use_compress = 1;
148 cur_size = compressed_size;
149 }
150
151 path = btrfs_alloc_path(); if (!path)
152 return -ENOMEM;
153
154 btrfs_set_trans_block_group(trans, inode);
155
156 key.objectid = inode->i_ino;
157 key.offset = start;
158 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
159 inode_add_bytes(inode, size);
160 datasize = btrfs_file_extent_calc_inline_size(cur_size);
161
162 inode_add_bytes(inode, size);
163 ret = btrfs_insert_empty_item(trans, root, path, &key,
164 datasize);
165 BUG_ON(ret);
166 if (ret) {
167 err = ret;
168 printk("got bad ret %d\n", ret);
169 goto fail;
170 }
171 leaf = path->nodes[0];
172 ei = btrfs_item_ptr(leaf, path->slots[0],
173 struct btrfs_file_extent_item);
174 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
175 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
176 btrfs_set_file_extent_encryption(leaf, ei, 0);
177 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
178 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
179 ptr = btrfs_file_extent_inline_start(ei);
180
181 if (use_compress) {
182 struct page *cpage;
183 int i = 0;
184 while(compressed_size > 0) {
185 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500186 cur_size = min_t(unsigned long, compressed_size,
Chris Masonc8b97812008-10-29 14:49:59 -0400187 PAGE_CACHE_SIZE);
188
189 kaddr = kmap(cpage);
190 write_extent_buffer(leaf, kaddr, ptr, cur_size);
191 kunmap(cpage);
192
193 i++;
194 ptr += cur_size;
195 compressed_size -= cur_size;
196 }
197 btrfs_set_file_extent_compression(leaf, ei,
198 BTRFS_COMPRESS_ZLIB);
199 } else {
200 page = find_get_page(inode->i_mapping,
201 start >> PAGE_CACHE_SHIFT);
202 btrfs_set_file_extent_compression(leaf, ei, 0);
203 kaddr = kmap_atomic(page, KM_USER0);
204 offset = start & (PAGE_CACHE_SIZE - 1);
205 write_extent_buffer(leaf, kaddr + offset, ptr, size);
206 kunmap_atomic(kaddr, KM_USER0);
207 page_cache_release(page);
208 }
209 btrfs_mark_buffer_dirty(leaf);
210 btrfs_free_path(path);
211
212 BTRFS_I(inode)->disk_i_size = inode->i_size;
213 btrfs_update_inode(trans, root, inode);
214 return 0;
215fail:
216 btrfs_free_path(path);
217 return err;
218}
219
220
221/*
222 * conditionally insert an inline extent into the file. This
223 * does the checks required to make sure the data is small enough
224 * to fit as an inline extent.
225 */
226static int cow_file_range_inline(struct btrfs_trans_handle *trans,
227 struct btrfs_root *root,
228 struct inode *inode, u64 start, u64 end,
229 size_t compressed_size,
230 struct page **compressed_pages)
231{
232 u64 isize = i_size_read(inode);
233 u64 actual_end = min(end + 1, isize);
234 u64 inline_len = actual_end - start;
235 u64 aligned_end = (end + root->sectorsize - 1) &
236 ~((u64)root->sectorsize - 1);
237 u64 hint_byte;
238 u64 data_len = inline_len;
239 int ret;
240
241 if (compressed_size)
242 data_len = compressed_size;
243
244 if (start > 0 ||
Chris Mason70b99e62008-10-31 12:46:39 -0400245 actual_end >= PAGE_CACHE_SIZE ||
Chris Masonc8b97812008-10-29 14:49:59 -0400246 data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
247 (!compressed_size &&
248 (actual_end & (root->sectorsize - 1)) == 0) ||
249 end + 1 < isize ||
250 data_len > root->fs_info->max_inline) {
251 return 1;
252 }
253
Chris Masonc8b97812008-10-29 14:49:59 -0400254 ret = btrfs_drop_extents(trans, root, inode, start,
Chris Mason70b99e62008-10-31 12:46:39 -0400255 aligned_end, start, &hint_byte);
Chris Masonc8b97812008-10-29 14:49:59 -0400256 BUG_ON(ret);
257
258 if (isize > actual_end)
259 inline_len = min_t(u64, isize, actual_end);
260 ret = insert_inline_extent(trans, root, inode, start,
261 inline_len, compressed_size,
262 compressed_pages);
263 BUG_ON(ret);
264 btrfs_drop_extent_cache(inode, start, aligned_end, 0);
Chris Masonc8b97812008-10-29 14:49:59 -0400265 return 0;
266}
267
Chris Mason771ed682008-11-06 22:02:51 -0500268struct async_extent {
269 u64 start;
270 u64 ram_size;
271 u64 compressed_size;
272 struct page **pages;
273 unsigned long nr_pages;
274 struct list_head list;
275};
276
277struct async_cow {
278 struct inode *inode;
279 struct btrfs_root *root;
280 struct page *locked_page;
281 u64 start;
282 u64 end;
283 struct list_head extents;
284 struct btrfs_work work;
285};
286
287static noinline int add_async_extent(struct async_cow *cow,
288 u64 start, u64 ram_size,
289 u64 compressed_size,
290 struct page **pages,
291 unsigned long nr_pages)
292{
293 struct async_extent *async_extent;
294
295 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
296 async_extent->start = start;
297 async_extent->ram_size = ram_size;
298 async_extent->compressed_size = compressed_size;
299 async_extent->pages = pages;
300 async_extent->nr_pages = nr_pages;
301 list_add_tail(&async_extent->list, &cow->extents);
302 return 0;
303}
304
Chris Masonc8b97812008-10-29 14:49:59 -0400305/*
Chris Mason771ed682008-11-06 22:02:51 -0500306 * we create compressed extents in two phases. The first
307 * phase compresses a range of pages that have already been
308 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400309 *
Chris Mason771ed682008-11-06 22:02:51 -0500310 * This is done inside an ordered work queue, and the compression
311 * is spread across many cpus. The actual IO submission is step
312 * two, and the ordered work queue takes care of making sure that
313 * happens in the same order things were put onto the queue by
314 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400315 *
Chris Mason771ed682008-11-06 22:02:51 -0500316 * If this code finds it can't get good compression, it puts an
317 * entry onto the work queue to write the uncompressed bytes. This
318 * makes sure that both compressed inodes and uncompressed inodes
319 * are written in the same order that pdflush sent them down.
Chris Masond352ac62008-09-29 15:18:18 -0400320 */
Chris Mason771ed682008-11-06 22:02:51 -0500321static noinline int compress_file_range(struct inode *inode,
322 struct page *locked_page,
323 u64 start, u64 end,
324 struct async_cow *async_cow,
325 int *num_added)
Chris Masonb888db22007-08-27 16:49:44 -0400326{
327 struct btrfs_root *root = BTRFS_I(inode)->root;
328 struct btrfs_trans_handle *trans;
Chris Masondb945352007-10-15 16:15:53 -0400329 u64 num_bytes;
Chris Masonc8b97812008-10-29 14:49:59 -0400330 u64 orig_start;
331 u64 disk_num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400332 u64 blocksize = root->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400333 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500334 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400335 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400336 struct page **pages = NULL;
337 unsigned long nr_pages;
338 unsigned long nr_pages_ret = 0;
339 unsigned long total_compressed = 0;
340 unsigned long total_in = 0;
341 unsigned long max_compressed = 128 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -0500342 unsigned long max_uncompressed = 128 * 1024;
Chris Masonc8b97812008-10-29 14:49:59 -0400343 int i;
344 int will_compress;
Chris Masonb888db22007-08-27 16:49:44 -0400345
Chris Masonc8b97812008-10-29 14:49:59 -0400346 orig_start = start;
Chris Masonbe20aa92007-12-17 20:14:01 -0500347
Chris Mason42dc7ba2008-12-15 11:44:56 -0500348 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400349again:
350 will_compress = 0;
351 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
352 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
353
Chris Masonc8b97812008-10-29 14:49:59 -0400354 total_compressed = actual_end - start;
355
356 /* we want to make sure that amount of ram required to uncompress
357 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500358 * of a compressed extent to 128k. This is a crucial number
359 * because it also controls how easily we can spread reads across
360 * cpus for decompression.
361 *
362 * We also want to make sure the amount of IO required to do
363 * a random read is reasonably small, so we limit the size of
364 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400365 */
366 total_compressed = min(total_compressed, max_uncompressed);
Chris Masondb945352007-10-15 16:15:53 -0400367 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
Chris Masonbe20aa92007-12-17 20:14:01 -0500368 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400369 disk_num_bytes = num_bytes;
370 total_in = 0;
371 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400372
Chris Mason771ed682008-11-06 22:02:51 -0500373 /*
374 * we do compression for mount -o compress and when the
375 * inode has not been flagged as nocompress. This flag can
376 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400377 */
378 if (!btrfs_test_flag(inode, NOCOMPRESS) &&
379 btrfs_test_opt(root, COMPRESS)) {
380 WARN_ON(pages);
Chris Masoncfbc2462008-10-30 13:22:14 -0400381 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
Chris Mason179e29e2007-11-01 11:28:41 -0400382
Chris Masonc8b97812008-10-29 14:49:59 -0400383 ret = btrfs_zlib_compress_pages(inode->i_mapping, start,
384 total_compressed, pages,
385 nr_pages, &nr_pages_ret,
386 &total_in,
387 &total_compressed,
388 max_compressed);
389
390 if (!ret) {
391 unsigned long offset = total_compressed &
392 (PAGE_CACHE_SIZE - 1);
393 struct page *page = pages[nr_pages_ret - 1];
394 char *kaddr;
395
396 /* zero the tail end of the last page, we might be
397 * sending it down to disk
398 */
399 if (offset) {
400 kaddr = kmap_atomic(page, KM_USER0);
401 memset(kaddr + offset, 0,
402 PAGE_CACHE_SIZE - offset);
403 kunmap_atomic(kaddr, KM_USER0);
404 }
405 will_compress = 1;
406 }
407 }
408 if (start == 0) {
Chris Mason771ed682008-11-06 22:02:51 -0500409 trans = btrfs_join_transaction(root, 1);
410 BUG_ON(!trans);
411 btrfs_set_trans_block_group(trans, inode);
412
Chris Masonc8b97812008-10-29 14:49:59 -0400413 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500414 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400415 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500416 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400417 */
418 ret = cow_file_range_inline(trans, root, inode,
419 start, end, 0, NULL);
420 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500421 /* try making a compressed inline extent */
Chris Masonc8b97812008-10-29 14:49:59 -0400422 ret = cow_file_range_inline(trans, root, inode,
423 start, end,
424 total_compressed, pages);
425 }
Chris Mason771ed682008-11-06 22:02:51 -0500426 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400427 if (ret == 0) {
Chris Mason771ed682008-11-06 22:02:51 -0500428 /*
429 * inline extent creation worked, we don't need
430 * to create any more async work items. Unlock
431 * and free up our temp pages.
432 */
Chris Masonc8b97812008-10-29 14:49:59 -0400433 extent_clear_unlock_delalloc(inode,
434 &BTRFS_I(inode)->io_tree,
Chris Mason771ed682008-11-06 22:02:51 -0500435 start, end, NULL, 1, 0,
436 0, 1, 1, 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400437 ret = 0;
438 goto free_pages_out;
439 }
440 }
441
442 if (will_compress) {
443 /*
444 * we aren't doing an inline extent round the compressed size
445 * up to a block size boundary so the allocator does sane
446 * things
447 */
448 total_compressed = (total_compressed + blocksize - 1) &
449 ~(blocksize - 1);
450
451 /*
452 * one last check to make sure the compression is really a
453 * win, compare the page count read with the blocks on disk
454 */
455 total_in = (total_in + PAGE_CACHE_SIZE - 1) &
456 ~(PAGE_CACHE_SIZE - 1);
457 if (total_compressed >= total_in) {
458 will_compress = 0;
459 } else {
460 disk_num_bytes = total_compressed;
461 num_bytes = total_in;
462 }
463 }
464 if (!will_compress && pages) {
465 /*
466 * the compression code ran but failed to make things smaller,
467 * free any pages it allocated and our page pointer array
468 */
469 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400470 WARN_ON(pages[i]->mapping);
Chris Masonc8b97812008-10-29 14:49:59 -0400471 page_cache_release(pages[i]);
472 }
473 kfree(pages);
474 pages = NULL;
475 total_compressed = 0;
476 nr_pages_ret = 0;
477
478 /* flag the file so we don't compress in the future */
479 btrfs_set_flag(inode, NOCOMPRESS);
480 }
Chris Mason771ed682008-11-06 22:02:51 -0500481 if (will_compress) {
482 *num_added += 1;
483
484 /* the async work queues will take care of doing actual
485 * allocation on disk for these compressed pages,
486 * and will submit them to the elevator.
487 */
488 add_async_extent(async_cow, start, num_bytes,
489 total_compressed, pages, nr_pages_ret);
490
Chris Mason42dc7ba2008-12-15 11:44:56 -0500491 if (start + num_bytes < end && start + num_bytes < actual_end) {
Chris Mason771ed682008-11-06 22:02:51 -0500492 start += num_bytes;
493 pages = NULL;
494 cond_resched();
495 goto again;
496 }
497 } else {
498 /*
499 * No compression, but we still need to write the pages in
500 * the file we've been given so far. redirty the locked
501 * page if it corresponds to our extent and set things up
502 * for the async work queue to run cow_file_range to do
503 * the normal delalloc dance
504 */
505 if (page_offset(locked_page) >= start &&
506 page_offset(locked_page) <= end) {
507 __set_page_dirty_nobuffers(locked_page);
508 /* unlocked later on in the async handlers */
509 }
510 add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0);
511 *num_added += 1;
512 }
513
514out:
515 return 0;
516
517free_pages_out:
518 for (i = 0; i < nr_pages_ret; i++) {
519 WARN_ON(pages[i]->mapping);
520 page_cache_release(pages[i]);
521 }
522 if (pages)
523 kfree(pages);
524
525 goto out;
526}
527
528/*
529 * phase two of compressed writeback. This is the ordered portion
530 * of the code, which only gets called in the order the work was
531 * queued. We walk all the async extents created by compress_file_range
532 * and send them down to the disk.
533 */
534static noinline int submit_compressed_extents(struct inode *inode,
535 struct async_cow *async_cow)
536{
537 struct async_extent *async_extent;
538 u64 alloc_hint = 0;
539 struct btrfs_trans_handle *trans;
540 struct btrfs_key ins;
541 struct extent_map *em;
542 struct btrfs_root *root = BTRFS_I(inode)->root;
543 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
544 struct extent_io_tree *io_tree;
545 int ret;
546
547 if (list_empty(&async_cow->extents))
548 return 0;
549
550 trans = btrfs_join_transaction(root, 1);
551
552 while(!list_empty(&async_cow->extents)) {
553 async_extent = list_entry(async_cow->extents.next,
554 struct async_extent, list);
555 list_del(&async_extent->list);
556
557 io_tree = &BTRFS_I(inode)->io_tree;
558
559 /* did the compression code fall back to uncompressed IO? */
560 if (!async_extent->pages) {
561 int page_started = 0;
562 unsigned long nr_written = 0;
563
564 lock_extent(io_tree, async_extent->start,
565 async_extent->start + async_extent->ram_size - 1,
566 GFP_NOFS);
567
568 /* allocate blocks */
569 cow_file_range(inode, async_cow->locked_page,
570 async_extent->start,
571 async_extent->start +
572 async_extent->ram_size - 1,
573 &page_started, &nr_written, 0);
574
575 /*
576 * if page_started, cow_file_range inserted an
577 * inline extent and took care of all the unlocking
578 * and IO for us. Otherwise, we need to submit
579 * all those pages down to the drive.
580 */
581 if (!page_started)
582 extent_write_locked_range(io_tree,
583 inode, async_extent->start,
584 async_extent->start +
585 async_extent->ram_size - 1,
586 btrfs_get_extent,
587 WB_SYNC_ALL);
588 kfree(async_extent);
589 cond_resched();
590 continue;
591 }
592
593 lock_extent(io_tree, async_extent->start,
594 async_extent->start + async_extent->ram_size - 1,
595 GFP_NOFS);
596 /*
597 * here we're doing allocation and writeback of the
598 * compressed pages
599 */
600 btrfs_drop_extent_cache(inode, async_extent->start,
601 async_extent->start +
602 async_extent->ram_size - 1, 0);
603
604 ret = btrfs_reserve_extent(trans, root,
605 async_extent->compressed_size,
606 async_extent->compressed_size,
607 0, alloc_hint,
608 (u64)-1, &ins, 1);
609 BUG_ON(ret);
610 em = alloc_extent_map(GFP_NOFS);
611 em->start = async_extent->start;
612 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500613 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500614
615 em->block_start = ins.objectid;
616 em->block_len = ins.offset;
617 em->bdev = root->fs_info->fs_devices->latest_bdev;
618 set_bit(EXTENT_FLAG_PINNED, &em->flags);
619 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
620
621 while(1) {
622 spin_lock(&em_tree->lock);
623 ret = add_extent_mapping(em_tree, em);
624 spin_unlock(&em_tree->lock);
625 if (ret != -EEXIST) {
626 free_extent_map(em);
627 break;
628 }
629 btrfs_drop_extent_cache(inode, async_extent->start,
630 async_extent->start +
631 async_extent->ram_size - 1, 0);
632 }
633
634 ret = btrfs_add_ordered_extent(inode, async_extent->start,
635 ins.objectid,
636 async_extent->ram_size,
637 ins.offset,
638 BTRFS_ORDERED_COMPRESSED);
639 BUG_ON(ret);
640
641 btrfs_end_transaction(trans, root);
642
643 /*
644 * clear dirty, set writeback and unlock the pages.
645 */
646 extent_clear_unlock_delalloc(inode,
647 &BTRFS_I(inode)->io_tree,
648 async_extent->start,
649 async_extent->start +
650 async_extent->ram_size - 1,
651 NULL, 1, 1, 0, 1, 1, 0);
652
653 ret = btrfs_submit_compressed_write(inode,
654 async_extent->start,
655 async_extent->ram_size,
656 ins.objectid,
657 ins.offset, async_extent->pages,
658 async_extent->nr_pages);
659
660 BUG_ON(ret);
661 trans = btrfs_join_transaction(root, 1);
662 alloc_hint = ins.objectid + ins.offset;
663 kfree(async_extent);
664 cond_resched();
665 }
666
667 btrfs_end_transaction(trans, root);
668 return 0;
669}
670
671/*
672 * when extent_io.c finds a delayed allocation range in the file,
673 * the call backs end up in this code. The basic idea is to
674 * allocate extents on disk for the range, and create ordered data structs
675 * in ram to track those extents.
676 *
677 * locked_page is the page that writepage had locked already. We use
678 * it to make sure we don't do extra locks or unlocks.
679 *
680 * *page_started is set to one if we unlock locked_page and do everything
681 * required to start IO on it. It may be clean and already done with
682 * IO when we return.
683 */
684static noinline int cow_file_range(struct inode *inode,
685 struct page *locked_page,
686 u64 start, u64 end, int *page_started,
687 unsigned long *nr_written,
688 int unlock)
689{
690 struct btrfs_root *root = BTRFS_I(inode)->root;
691 struct btrfs_trans_handle *trans;
692 u64 alloc_hint = 0;
693 u64 num_bytes;
694 unsigned long ram_size;
695 u64 disk_num_bytes;
696 u64 cur_alloc_size;
697 u64 blocksize = root->sectorsize;
698 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500699 u64 isize = i_size_read(inode);
Chris Mason771ed682008-11-06 22:02:51 -0500700 struct btrfs_key ins;
701 struct extent_map *em;
702 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
703 int ret = 0;
704
705 trans = btrfs_join_transaction(root, 1);
706 BUG_ON(!trans);
707 btrfs_set_trans_block_group(trans, inode);
708
Chris Mason42dc7ba2008-12-15 11:44:56 -0500709 actual_end = min_t(u64, isize, end + 1);
Chris Mason771ed682008-11-06 22:02:51 -0500710
711 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
712 num_bytes = max(blocksize, num_bytes);
713 disk_num_bytes = num_bytes;
714 ret = 0;
715
716 if (start == 0) {
717 /* lets try to make an inline extent */
718 ret = cow_file_range_inline(trans, root, inode,
719 start, end, 0, NULL);
720 if (ret == 0) {
721 extent_clear_unlock_delalloc(inode,
722 &BTRFS_I(inode)->io_tree,
723 start, end, NULL, 1, 1,
724 1, 1, 1, 1);
725 *nr_written = *nr_written +
726 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
727 *page_started = 1;
728 ret = 0;
729 goto out;
730 }
731 }
Chris Masonc8b97812008-10-29 14:49:59 -0400732
733 BUG_ON(disk_num_bytes >
734 btrfs_super_total_bytes(&root->fs_info->super_copy));
735
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400736 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400737
Chris Masonc8b97812008-10-29 14:49:59 -0400738 while(disk_num_bytes > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400739 cur_alloc_size = min(disk_num_bytes, root->fs_info->max_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400740 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500741 root->sectorsize, 0, alloc_hint,
Chris Masone6dcd2d2008-07-17 12:53:50 -0400742 (u64)-1, &ins, 1);
Chris Masonc59f8952007-12-17 20:14:04 -0500743 if (ret) {
Chris Mason771ed682008-11-06 22:02:51 -0500744 BUG();
Chris Masonc59f8952007-12-17 20:14:04 -0500745 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400746 em = alloc_extent_map(GFP_NOFS);
747 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500748 em->orig_start = em->start;
Chris Masonc8b97812008-10-29 14:49:59 -0400749
Chris Mason771ed682008-11-06 22:02:51 -0500750 ram_size = ins.offset;
751 em->len = ins.offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400752
Chris Masone6dcd2d2008-07-17 12:53:50 -0400753 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400754 em->block_len = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400755 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400756 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -0400757
Chris Masone6dcd2d2008-07-17 12:53:50 -0400758 while(1) {
759 spin_lock(&em_tree->lock);
760 ret = add_extent_mapping(em_tree, em);
761 spin_unlock(&em_tree->lock);
762 if (ret != -EEXIST) {
763 free_extent_map(em);
764 break;
765 }
766 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400767 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400768 }
769
Chris Mason98d20f62008-04-14 09:46:10 -0400770 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400771 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -0500772 ram_size, cur_alloc_size, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400773 BUG_ON(ret);
Chris Masonc8b97812008-10-29 14:49:59 -0400774
Yan Zheng17d217f2008-12-12 10:03:38 -0500775 if (root->root_key.objectid ==
776 BTRFS_DATA_RELOC_TREE_OBJECTID) {
777 ret = btrfs_reloc_clone_csums(inode, start,
778 cur_alloc_size);
779 BUG_ON(ret);
780 }
781
Chris Masonc8b97812008-10-29 14:49:59 -0400782 if (disk_num_bytes < cur_alloc_size) {
783 printk("num_bytes %Lu cur_alloc %Lu\n", disk_num_bytes,
Chris Mason3b951512008-04-17 11:29:12 -0400784 cur_alloc_size);
785 break;
786 }
Chris Masonc8b97812008-10-29 14:49:59 -0400787 /* we're not doing compressed IO, don't unlock the first
788 * page (which the caller expects to stay locked), don't
789 * clear any dirty bits and don't set any writeback bits
790 */
791 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
792 start, start + ram_size - 1,
Chris Mason771ed682008-11-06 22:02:51 -0500793 locked_page, unlock, 1,
794 1, 0, 0, 0);
Chris Masonc8b97812008-10-29 14:49:59 -0400795 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -0500796 num_bytes -= cur_alloc_size;
797 alloc_hint = ins.objectid + ins.offset;
798 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400799 }
Chris Masonb888db22007-08-27 16:49:44 -0400800out:
Chris Mason771ed682008-11-06 22:02:51 -0500801 ret = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400802 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400803
Chris Masonbe20aa92007-12-17 20:14:01 -0500804 return ret;
Chris Mason771ed682008-11-06 22:02:51 -0500805}
Chris Masonc8b97812008-10-29 14:49:59 -0400806
Chris Mason771ed682008-11-06 22:02:51 -0500807/*
808 * work queue call back to started compression on a file and pages
809 */
810static noinline void async_cow_start(struct btrfs_work *work)
811{
812 struct async_cow *async_cow;
813 int num_added = 0;
814 async_cow = container_of(work, struct async_cow, work);
815
816 compress_file_range(async_cow->inode, async_cow->locked_page,
817 async_cow->start, async_cow->end, async_cow,
818 &num_added);
819 if (num_added == 0)
820 async_cow->inode = NULL;
821}
822
823/*
824 * work queue call back to submit previously compressed pages
825 */
826static noinline void async_cow_submit(struct btrfs_work *work)
827{
828 struct async_cow *async_cow;
829 struct btrfs_root *root;
830 unsigned long nr_pages;
831
832 async_cow = container_of(work, struct async_cow, work);
833
834 root = async_cow->root;
835 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
836 PAGE_CACHE_SHIFT;
837
838 atomic_sub(nr_pages, &root->fs_info->async_delalloc_pages);
839
840 if (atomic_read(&root->fs_info->async_delalloc_pages) <
841 5 * 1042 * 1024 &&
842 waitqueue_active(&root->fs_info->async_submit_wait))
843 wake_up(&root->fs_info->async_submit_wait);
844
845 if (async_cow->inode) {
846 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason70b99e62008-10-31 12:46:39 -0400847 }
Chris Mason771ed682008-11-06 22:02:51 -0500848}
Chris Masonc8b97812008-10-29 14:49:59 -0400849
Chris Mason771ed682008-11-06 22:02:51 -0500850static noinline void async_cow_free(struct btrfs_work *work)
851{
852 struct async_cow *async_cow;
853 async_cow = container_of(work, struct async_cow, work);
854 kfree(async_cow);
855}
856
857static int cow_file_range_async(struct inode *inode, struct page *locked_page,
858 u64 start, u64 end, int *page_started,
859 unsigned long *nr_written)
860{
861 struct async_cow *async_cow;
862 struct btrfs_root *root = BTRFS_I(inode)->root;
863 unsigned long nr_pages;
864 u64 cur_end;
865 int limit = 10 * 1024 * 1042;
866
867 if (!btrfs_test_opt(root, COMPRESS)) {
868 return cow_file_range(inode, locked_page, start, end,
869 page_started, nr_written, 1);
870 }
871
872 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED |
873 EXTENT_DELALLOC, 1, 0, GFP_NOFS);
874 while(start < end) {
875 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
876 async_cow->inode = inode;
877 async_cow->root = root;
878 async_cow->locked_page = locked_page;
879 async_cow->start = start;
880
881 if (btrfs_test_flag(inode, NOCOMPRESS))
882 cur_end = end;
883 else
884 cur_end = min(end, start + 512 * 1024 - 1);
885
886 async_cow->end = cur_end;
887 INIT_LIST_HEAD(&async_cow->extents);
888
889 async_cow->work.func = async_cow_start;
890 async_cow->work.ordered_func = async_cow_submit;
891 async_cow->work.ordered_free = async_cow_free;
892 async_cow->work.flags = 0;
893
Chris Mason771ed682008-11-06 22:02:51 -0500894 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
895 PAGE_CACHE_SHIFT;
896 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
897
898 btrfs_queue_worker(&root->fs_info->delalloc_workers,
899 &async_cow->work);
900
901 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
902 wait_event(root->fs_info->async_submit_wait,
903 (atomic_read(&root->fs_info->async_delalloc_pages) <
904 limit));
905 }
906
907 while(atomic_read(&root->fs_info->async_submit_draining) &&
908 atomic_read(&root->fs_info->async_delalloc_pages)) {
909 wait_event(root->fs_info->async_submit_wait,
910 (atomic_read(&root->fs_info->async_delalloc_pages) ==
911 0));
912 }
913
914 *nr_written += nr_pages;
915 start = cur_end + 1;
916 }
917 *page_started = 1;
918 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -0500919}
920
Yan Zheng17d217f2008-12-12 10:03:38 -0500921static int noinline csum_exist_in_range(struct btrfs_root *root,
922 u64 bytenr, u64 num_bytes)
923{
924 int ret;
925 struct btrfs_ordered_sum *sums;
926 LIST_HEAD(list);
927
928 ret = btrfs_lookup_csums_range(root, bytenr, bytenr + num_bytes - 1,
929 &list);
930 if (ret == 0 && list_empty(&list))
931 return 0;
932
933 while (!list_empty(&list)) {
934 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
935 list_del(&sums->list);
936 kfree(sums);
937 }
938 return 1;
939}
940
Chris Masond352ac62008-09-29 15:18:18 -0400941/*
942 * when nowcow writeback call back. This checks for snapshots or COW copies
943 * of the extents that exist in the file, and COWs the file as required.
944 *
945 * If no cow copies or snapshots exist, we write directly to the existing
946 * blocks on disk
947 */
Chris Masonc8b97812008-10-29 14:49:59 -0400948static int run_delalloc_nocow(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -0500949 u64 start, u64 end, int *page_started, int force,
950 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -0500951{
Chris Masonbe20aa92007-12-17 20:14:01 -0500952 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -0400953 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -0500954 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -0500955 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -0400956 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -0500957 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -0400958 u64 cow_start;
959 u64 cur_offset;
960 u64 extent_end;
961 u64 disk_bytenr;
962 u64 num_bytes;
963 int extent_type;
964 int ret;
Yan Zhengd899e052008-10-30 14:25:28 -0400965 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -0400966 int nocow;
967 int check_prev = 1;
Chris Masonbe20aa92007-12-17 20:14:01 -0500968
969 path = btrfs_alloc_path();
970 BUG_ON(!path);
Yan Zheng7ea394f2008-08-05 13:05:02 -0400971 trans = btrfs_join_transaction(root, 1);
972 BUG_ON(!trans);
Chris Masonbe20aa92007-12-17 20:14:01 -0500973
Yan Zheng80ff3852008-10-30 14:20:02 -0400974 cow_start = (u64)-1;
975 cur_offset = start;
976 while (1) {
977 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
978 cur_offset, 0);
979 BUG_ON(ret < 0);
980 if (ret > 0 && path->slots[0] > 0 && check_prev) {
981 leaf = path->nodes[0];
982 btrfs_item_key_to_cpu(leaf, &found_key,
983 path->slots[0] - 1);
984 if (found_key.objectid == inode->i_ino &&
985 found_key.type == BTRFS_EXTENT_DATA_KEY)
986 path->slots[0]--;
987 }
988 check_prev = 0;
989next_slot:
990 leaf = path->nodes[0];
991 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
992 ret = btrfs_next_leaf(root, path);
993 if (ret < 0)
994 BUG_ON(1);
995 if (ret > 0)
996 break;
997 leaf = path->nodes[0];
998 }
Chris Masonbe20aa92007-12-17 20:14:01 -0500999
Yan Zheng80ff3852008-10-30 14:20:02 -04001000 nocow = 0;
1001 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001002 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001003 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001004
Yan Zheng80ff3852008-10-30 14:20:02 -04001005 if (found_key.objectid > inode->i_ino ||
1006 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1007 found_key.offset > end)
1008 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001009
Yan Zheng80ff3852008-10-30 14:20:02 -04001010 if (found_key.offset > cur_offset) {
1011 extent_end = found_key.offset;
1012 goto out_check;
1013 }
Chris Masonc31f8832008-01-08 15:46:31 -05001014
Yan Zheng80ff3852008-10-30 14:20:02 -04001015 fi = btrfs_item_ptr(leaf, path->slots[0],
1016 struct btrfs_file_extent_item);
1017 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001018
Yan Zhengd899e052008-10-30 14:25:28 -04001019 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1020 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001021 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1022 extent_end = found_key.offset +
1023 btrfs_file_extent_num_bytes(leaf, fi);
1024 if (extent_end <= start) {
1025 path->slots[0]++;
1026 goto next_slot;
1027 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001028 if (disk_bytenr == 0)
1029 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001030 if (btrfs_file_extent_compression(leaf, fi) ||
1031 btrfs_file_extent_encryption(leaf, fi) ||
1032 btrfs_file_extent_other_encoding(leaf, fi))
1033 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001034 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1035 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001036 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001037 goto out_check;
Yan Zheng17d217f2008-12-12 10:03:38 -05001038 if (btrfs_cross_ref_exist(trans, root, inode->i_ino,
1039 disk_bytenr))
1040 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001041 disk_bytenr += btrfs_file_extent_offset(leaf, fi);
Yan Zheng17d217f2008-12-12 10:03:38 -05001042 disk_bytenr += cur_offset - found_key.offset;
1043 num_bytes = min(end + 1, extent_end) - cur_offset;
1044 /*
1045 * force cow if csum exists in the range.
1046 * this ensure that csum for a given extent are
1047 * either valid or do not exist.
1048 */
1049 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1050 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001051 nocow = 1;
1052 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1053 extent_end = found_key.offset +
1054 btrfs_file_extent_inline_len(leaf, fi);
1055 extent_end = ALIGN(extent_end, root->sectorsize);
1056 } else {
1057 BUG_ON(1);
1058 }
1059out_check:
1060 if (extent_end <= start) {
1061 path->slots[0]++;
1062 goto next_slot;
1063 }
1064 if (!nocow) {
1065 if (cow_start == (u64)-1)
1066 cow_start = cur_offset;
1067 cur_offset = extent_end;
1068 if (cur_offset > end)
1069 break;
1070 path->slots[0]++;
1071 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001072 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001073
Yan Zheng7ea394f2008-08-05 13:05:02 -04001074 btrfs_release_path(root, path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001075 if (cow_start != (u64)-1) {
1076 ret = cow_file_range(inode, locked_page, cow_start,
Chris Mason771ed682008-11-06 22:02:51 -05001077 found_key.offset - 1, page_started,
1078 nr_written, 1);
Yan Zheng80ff3852008-10-30 14:20:02 -04001079 BUG_ON(ret);
1080 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001081 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001082
Yan Zhengd899e052008-10-30 14:25:28 -04001083 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1084 struct extent_map *em;
1085 struct extent_map_tree *em_tree;
1086 em_tree = &BTRFS_I(inode)->extent_tree;
1087 em = alloc_extent_map(GFP_NOFS);
1088 em->start = cur_offset;
Chris Mason445a6942008-11-10 11:53:33 -05001089 em->orig_start = em->start;
Yan Zhengd899e052008-10-30 14:25:28 -04001090 em->len = num_bytes;
1091 em->block_len = num_bytes;
1092 em->block_start = disk_bytenr;
1093 em->bdev = root->fs_info->fs_devices->latest_bdev;
1094 set_bit(EXTENT_FLAG_PINNED, &em->flags);
1095 while (1) {
1096 spin_lock(&em_tree->lock);
1097 ret = add_extent_mapping(em_tree, em);
1098 spin_unlock(&em_tree->lock);
1099 if (ret != -EEXIST) {
1100 free_extent_map(em);
1101 break;
1102 }
1103 btrfs_drop_extent_cache(inode, em->start,
1104 em->start + em->len - 1, 0);
1105 }
1106 type = BTRFS_ORDERED_PREALLOC;
1107 } else {
1108 type = BTRFS_ORDERED_NOCOW;
1109 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001110
1111 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001112 num_bytes, num_bytes, type);
1113 BUG_ON(ret);
Chris Mason771ed682008-11-06 22:02:51 -05001114
Yan Zhengd899e052008-10-30 14:25:28 -04001115 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
1116 cur_offset, cur_offset + num_bytes - 1,
Chris Mason771ed682008-11-06 22:02:51 -05001117 locked_page, 1, 1, 1, 0, 0, 0);
Yan Zheng80ff3852008-10-30 14:20:02 -04001118 cur_offset = extent_end;
1119 if (cur_offset > end)
1120 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001121 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001122 btrfs_release_path(root, path);
1123
1124 if (cur_offset <= end && cow_start == (u64)-1)
1125 cow_start = cur_offset;
1126 if (cow_start != (u64)-1) {
1127 ret = cow_file_range(inode, locked_page, cow_start, end,
Chris Mason771ed682008-11-06 22:02:51 -05001128 page_started, nr_written, 1);
Yan Zheng80ff3852008-10-30 14:20:02 -04001129 BUG_ON(ret);
1130 }
1131
1132 ret = btrfs_end_transaction(trans, root);
1133 BUG_ON(ret);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001134 btrfs_free_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001135 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001136}
1137
Chris Masond352ac62008-09-29 15:18:18 -04001138/*
1139 * extent_io.c call back to do delayed allocation processing
1140 */
Chris Masonc8b97812008-10-29 14:49:59 -04001141static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001142 u64 start, u64 end, int *page_started,
1143 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001144{
Chris Masonbe20aa92007-12-17 20:14:01 -05001145 int ret;
Chris Masona2135012008-06-25 16:01:30 -04001146
Yan Zheng17d217f2008-12-12 10:03:38 -05001147 if (btrfs_test_flag(inode, NODATACOW))
Chris Masonc8b97812008-10-29 14:49:59 -04001148 ret = run_delalloc_nocow(inode, locked_page, start, end,
Liu Huice397c02008-12-01 20:31:40 -05001149 page_started, 1, nr_written);
Yan Zhengd899e052008-10-30 14:25:28 -04001150 else if (btrfs_test_flag(inode, PREALLOC))
1151 ret = run_delalloc_nocow(inode, locked_page, start, end,
Liu Huice397c02008-12-01 20:31:40 -05001152 page_started, 0, nr_written);
Chris Masonbe20aa92007-12-17 20:14:01 -05001153 else
Chris Mason771ed682008-11-06 22:02:51 -05001154 ret = cow_file_range_async(inode, locked_page, start, end,
1155 page_started, nr_written);
Chris Mason1832a6d2007-12-21 16:27:21 -05001156
Chris Masonb888db22007-08-27 16:49:44 -04001157 return ret;
1158}
1159
Chris Masond352ac62008-09-29 15:18:18 -04001160/*
1161 * extent_io.c set_bit_hook, used to track delayed allocation
1162 * bytes in this file, and to maintain the list of inodes that
1163 * have pending delalloc work to be done.
1164 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001165static int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -05001166 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -05001167{
Chris Mason75eff682008-12-15 15:54:40 -05001168 /*
1169 * set_bit and clear bit hooks normally require _irqsave/restore
1170 * but in this case, we are only testeing for the DELALLOC
1171 * bit, which is only set or cleared with irqs on
1172 */
Chris Masonb0c68f82008-01-31 11:05:37 -05001173 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001174 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason75eff682008-12-15 15:54:40 -05001175 spin_lock(&root->fs_info->delalloc_lock);
Chris Mason90692182008-02-08 13:49:28 -05001176 BTRFS_I(inode)->delalloc_bytes += end - start + 1;
Chris Mason291d6732008-01-29 15:55:23 -05001177 root->fs_info->delalloc_bytes += end - start + 1;
Chris Masonea8c2812008-08-04 23:17:27 -04001178 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1179 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1180 &root->fs_info->delalloc_inodes);
1181 }
Chris Mason75eff682008-12-15 15:54:40 -05001182 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001183 }
1184 return 0;
1185}
1186
Chris Masond352ac62008-09-29 15:18:18 -04001187/*
1188 * extent_io.c clear_bit_hook, see set_bit_hook for why
1189 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001190static int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -05001191 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -05001192{
Chris Mason75eff682008-12-15 15:54:40 -05001193 /*
1194 * set_bit and clear bit hooks normally require _irqsave/restore
1195 * but in this case, we are only testeing for the DELALLOC
1196 * bit, which is only set or cleared with irqs on
1197 */
Chris Masonb0c68f82008-01-31 11:05:37 -05001198 if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001199 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonbcbfce82008-04-22 13:26:47 -04001200
Chris Mason75eff682008-12-15 15:54:40 -05001201 spin_lock(&root->fs_info->delalloc_lock);
Chris Masonb0c68f82008-01-31 11:05:37 -05001202 if (end - start + 1 > root->fs_info->delalloc_bytes) {
1203 printk("warning: delalloc account %Lu %Lu\n",
1204 end - start + 1, root->fs_info->delalloc_bytes);
1205 root->fs_info->delalloc_bytes = 0;
Chris Mason90692182008-02-08 13:49:28 -05001206 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masonb0c68f82008-01-31 11:05:37 -05001207 } else {
1208 root->fs_info->delalloc_bytes -= end - start + 1;
Chris Mason90692182008-02-08 13:49:28 -05001209 BTRFS_I(inode)->delalloc_bytes -= end - start + 1;
Chris Masonb0c68f82008-01-31 11:05:37 -05001210 }
Chris Masonea8c2812008-08-04 23:17:27 -04001211 if (BTRFS_I(inode)->delalloc_bytes == 0 &&
1212 !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1213 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1214 }
Chris Mason75eff682008-12-15 15:54:40 -05001215 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001216 }
1217 return 0;
1218}
1219
Chris Masond352ac62008-09-29 15:18:18 -04001220/*
1221 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1222 * we don't create bios that span stripes or chunks
1223 */
Chris Mason239b14b2008-03-24 15:02:07 -04001224int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001225 size_t size, struct bio *bio,
1226 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001227{
1228 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
1229 struct btrfs_mapping_tree *map_tree;
Chris Masona62b9402008-10-03 16:31:08 -04001230 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001231 u64 length = 0;
1232 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001233 int ret;
1234
Chris Mason771ed682008-11-06 22:02:51 -05001235 if (bio_flags & EXTENT_BIO_COMPRESSED)
1236 return 0;
1237
Chris Masonf2d8d742008-04-21 10:03:05 -04001238 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001239 map_tree = &root->fs_info->mapping_tree;
1240 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04001241 ret = btrfs_map_block(map_tree, READ, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001242 &map_length, NULL, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04001243
Chris Mason239b14b2008-03-24 15:02:07 -04001244 if (map_length < length + size) {
Chris Mason239b14b2008-03-24 15:02:07 -04001245 return 1;
1246 }
1247 return 0;
1248}
1249
Chris Masond352ac62008-09-29 15:18:18 -04001250/*
1251 * in order to insert checksums into the metadata in large chunks,
1252 * we wait until bio submission time. All the pages in the bio are
1253 * checksummed and sums are attached onto the ordered extent record.
1254 *
1255 * At IO completion time the cums attached on the ordered extent record
1256 * are inserted into the btree
1257 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001258static int __btrfs_submit_bio_start(struct inode *inode, int rw, struct bio *bio,
Chris Masonc8b97812008-10-29 14:49:59 -04001259 int mirror_num, unsigned long bio_flags)
Chris Mason065631f2008-02-20 12:07:25 -05001260{
Chris Mason065631f2008-02-20 12:07:25 -05001261 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001262 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001263
Chris Masond20f7042008-12-08 16:58:54 -05001264 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Chris Mason44b8bd72008-04-16 11:14:51 -04001265 BUG_ON(ret);
Chris Mason4a69a412008-11-06 22:03:00 -05001266 return 0;
1267}
Chris Masone0156402008-04-16 11:15:20 -04001268
Chris Mason4a69a412008-11-06 22:03:00 -05001269/*
1270 * in order to insert checksums into the metadata in large chunks,
1271 * we wait until bio submission time. All the pages in the bio are
1272 * checksummed and sums are attached onto the ordered extent record.
1273 *
1274 * At IO completion time the cums attached on the ordered extent record
1275 * are inserted into the btree
1276 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001277static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Mason4a69a412008-11-06 22:03:00 -05001278 int mirror_num, unsigned long bio_flags)
1279{
1280 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason8b712842008-06-11 16:50:36 -04001281 return btrfs_map_bio(root, rw, bio, mirror_num, 1);
Chris Mason44b8bd72008-04-16 11:14:51 -04001282}
1283
Chris Masond352ac62008-09-29 15:18:18 -04001284/*
1285 * extent_io.c submission hook. This does the right thing for csum calculation on write,
1286 * or reading the csums from the tree before a read
1287 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001288static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masonc8b97812008-10-29 14:49:59 -04001289 int mirror_num, unsigned long bio_flags)
Chris Mason44b8bd72008-04-16 11:14:51 -04001290{
1291 struct btrfs_root *root = BTRFS_I(inode)->root;
1292 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001293 int skip_sum;
Chris Mason44b8bd72008-04-16 11:14:51 -04001294
Chris Masone6dcd2d2008-07-17 12:53:50 -04001295 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
1296 BUG_ON(ret);
Chris Mason065631f2008-02-20 12:07:25 -05001297
Yan Zheng17d217f2008-12-12 10:03:38 -05001298 skip_sum = btrfs_test_flag(inode, NODATASUM);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001299
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001300 if (!(rw & (1 << BIO_RW))) {
Chris Masond20f7042008-12-08 16:58:54 -05001301 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Chris Masonc8b97812008-10-29 14:49:59 -04001302 return btrfs_submit_compressed_read(inode, bio,
1303 mirror_num, bio_flags);
Chris Masond20f7042008-12-08 16:58:54 -05001304 } else if (!skip_sum)
1305 btrfs_lookup_bio_sums(root, inode, bio, NULL);
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001306 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001307 } else if (!skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001308 /* csum items have already been cloned */
1309 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1310 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001311 /* we're doing a write, do the async checksumming */
1312 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001313 inode, rw, bio, mirror_num,
Chris Mason4a69a412008-11-06 22:03:00 -05001314 bio_flags, __btrfs_submit_bio_start,
1315 __btrfs_submit_bio_done);
Chris Mason19b9bdb2008-10-30 14:23:13 -04001316 }
1317
Chris Mason0b86a832008-03-24 15:01:56 -04001318mapit:
Chris Mason8b712842008-06-11 16:50:36 -04001319 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
Chris Mason065631f2008-02-20 12:07:25 -05001320}
Chris Mason6885f302008-02-20 16:11:05 -05001321
Chris Masond352ac62008-09-29 15:18:18 -04001322/*
1323 * given a list of ordered sums record them in the inode. This happens
1324 * at IO completion time based on sums calculated at bio submission time.
1325 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001326static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001327 struct inode *inode, u64 file_offset,
1328 struct list_head *list)
1329{
1330 struct list_head *cur;
1331 struct btrfs_ordered_sum *sum;
1332
1333 btrfs_set_trans_block_group(trans, inode);
Chris Masonba1da2f2008-07-17 12:54:15 -04001334 list_for_each(cur, list) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04001335 sum = list_entry(cur, struct btrfs_ordered_sum, list);
Chris Masond20f7042008-12-08 16:58:54 -05001336 btrfs_csum_file_blocks(trans,
1337 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001338 }
1339 return 0;
1340}
1341
Chris Masonea8c2812008-08-04 23:17:27 -04001342int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end)
1343{
Chris Mason771ed682008-11-06 22:02:51 -05001344 if ((end & (PAGE_CACHE_SIZE - 1)) == 0) {
1345 WARN_ON(1);
1346 }
Chris Masonea8c2812008-08-04 23:17:27 -04001347 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
1348 GFP_NOFS);
1349}
1350
Chris Masond352ac62008-09-29 15:18:18 -04001351/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001352struct btrfs_writepage_fixup {
1353 struct page *page;
1354 struct btrfs_work work;
1355};
1356
Christoph Hellwigb2950862008-12-02 09:54:17 -05001357static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001358{
1359 struct btrfs_writepage_fixup *fixup;
1360 struct btrfs_ordered_extent *ordered;
1361 struct page *page;
1362 struct inode *inode;
1363 u64 page_start;
1364 u64 page_end;
1365
1366 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1367 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001368again:
Chris Mason247e7432008-07-17 12:53:51 -04001369 lock_page(page);
1370 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1371 ClearPageChecked(page);
1372 goto out_page;
1373 }
1374
1375 inode = page->mapping->host;
1376 page_start = page_offset(page);
1377 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1378
1379 lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001380
1381 /* already ordered? We're done */
1382 if (test_range_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
1383 EXTENT_ORDERED, 0)) {
Chris Mason247e7432008-07-17 12:53:51 -04001384 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001385 }
1386
1387 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1388 if (ordered) {
1389 unlock_extent(&BTRFS_I(inode)->io_tree, page_start,
1390 page_end, GFP_NOFS);
1391 unlock_page(page);
1392 btrfs_start_ordered_extent(inode, ordered, 1);
1393 goto again;
1394 }
Chris Mason247e7432008-07-17 12:53:51 -04001395
Chris Masonea8c2812008-08-04 23:17:27 -04001396 btrfs_set_extent_delalloc(inode, page_start, page_end);
Chris Mason247e7432008-07-17 12:53:51 -04001397 ClearPageChecked(page);
1398out:
1399 unlock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
1400out_page:
1401 unlock_page(page);
1402 page_cache_release(page);
1403}
1404
1405/*
1406 * There are a few paths in the higher layers of the kernel that directly
1407 * set the page dirty bit without asking the filesystem if it is a
1408 * good idea. This causes problems because we want to make sure COW
1409 * properly happens and the data=ordered rules are followed.
1410 *
Chris Masonc8b97812008-10-29 14:49:59 -04001411 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001412 * hasn't been properly setup for IO. We kick off an async process
1413 * to fix it up. The async helper will wait for ordered extents, set
1414 * the delalloc bit and make it safe to write the page.
1415 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001416static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001417{
1418 struct inode *inode = page->mapping->host;
1419 struct btrfs_writepage_fixup *fixup;
1420 struct btrfs_root *root = BTRFS_I(inode)->root;
1421 int ret;
1422
1423 ret = test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1424 EXTENT_ORDERED, 0);
1425 if (ret)
1426 return 0;
1427
1428 if (PageChecked(page))
1429 return -EAGAIN;
1430
1431 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1432 if (!fixup)
1433 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001434
Chris Mason247e7432008-07-17 12:53:51 -04001435 SetPageChecked(page);
1436 page_cache_get(page);
1437 fixup->work.func = btrfs_writepage_fixup_worker;
1438 fixup->page = page;
1439 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
1440 return -EAGAIN;
1441}
1442
Yan Zhengd899e052008-10-30 14:25:28 -04001443static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1444 struct inode *inode, u64 file_pos,
1445 u64 disk_bytenr, u64 disk_num_bytes,
1446 u64 num_bytes, u64 ram_bytes,
1447 u8 compression, u8 encryption,
1448 u16 other_encoding, int extent_type)
1449{
1450 struct btrfs_root *root = BTRFS_I(inode)->root;
1451 struct btrfs_file_extent_item *fi;
1452 struct btrfs_path *path;
1453 struct extent_buffer *leaf;
1454 struct btrfs_key ins;
1455 u64 hint;
1456 int ret;
1457
1458 path = btrfs_alloc_path();
1459 BUG_ON(!path);
1460
1461 ret = btrfs_drop_extents(trans, root, inode, file_pos,
1462 file_pos + num_bytes, file_pos, &hint);
1463 BUG_ON(ret);
1464
1465 ins.objectid = inode->i_ino;
1466 ins.offset = file_pos;
1467 ins.type = BTRFS_EXTENT_DATA_KEY;
1468 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
1469 BUG_ON(ret);
1470 leaf = path->nodes[0];
1471 fi = btrfs_item_ptr(leaf, path->slots[0],
1472 struct btrfs_file_extent_item);
1473 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1474 btrfs_set_file_extent_type(leaf, fi, extent_type);
1475 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1476 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1477 btrfs_set_file_extent_offset(leaf, fi, 0);
1478 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1479 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1480 btrfs_set_file_extent_compression(leaf, fi, compression);
1481 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1482 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
1483 btrfs_mark_buffer_dirty(leaf);
1484
1485 inode_add_bytes(inode, num_bytes);
1486 btrfs_drop_extent_cache(inode, file_pos, file_pos + num_bytes - 1, 0);
1487
1488 ins.objectid = disk_bytenr;
1489 ins.offset = disk_num_bytes;
1490 ins.type = BTRFS_EXTENT_ITEM_KEY;
1491 ret = btrfs_alloc_reserved_extent(trans, root, leaf->start,
1492 root->root_key.objectid,
1493 trans->transid, inode->i_ino, &ins);
1494 BUG_ON(ret);
1495
1496 btrfs_free_path(path);
1497 return 0;
1498}
1499
Chris Masond352ac62008-09-29 15:18:18 -04001500/* as ordered data IO finishes, this gets called so we can finish
1501 * an ordered extent if the range of bytes in the file it covers are
1502 * fully written.
1503 */
Chris Mason211f90e2008-07-18 11:56:15 -04001504static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001505{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001506 struct btrfs_root *root = BTRFS_I(inode)->root;
1507 struct btrfs_trans_handle *trans;
1508 struct btrfs_ordered_extent *ordered_extent;
1509 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan Zhengd899e052008-10-30 14:25:28 -04001510 int compressed = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001511 int ret;
1512
1513 ret = btrfs_dec_test_ordered_pending(inode, start, end - start + 1);
Chris Masonba1da2f2008-07-17 12:54:15 -04001514 if (!ret)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001515 return 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001516
Chris Masonf9295742008-07-17 12:54:14 -04001517 trans = btrfs_join_transaction(root, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001518
1519 ordered_extent = btrfs_lookup_ordered_extent(inode, start);
1520 BUG_ON(!ordered_extent);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001521 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags))
1522 goto nocow;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001523
1524 lock_extent(io_tree, ordered_extent->file_offset,
1525 ordered_extent->file_offset + ordered_extent->len - 1,
1526 GFP_NOFS);
1527
Chris Masonc8b97812008-10-29 14:49:59 -04001528 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Yan Zhengd899e052008-10-30 14:25:28 -04001529 compressed = 1;
1530 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
1531 BUG_ON(compressed);
1532 ret = btrfs_mark_extent_written(trans, root, inode,
1533 ordered_extent->file_offset,
1534 ordered_extent->file_offset +
1535 ordered_extent->len);
1536 BUG_ON(ret);
1537 } else {
1538 ret = insert_reserved_file_extent(trans, inode,
1539 ordered_extent->file_offset,
1540 ordered_extent->start,
1541 ordered_extent->disk_len,
1542 ordered_extent->len,
1543 ordered_extent->len,
1544 compressed, 0, 0,
1545 BTRFS_FILE_EXTENT_REG);
1546 BUG_ON(ret);
1547 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04001548 unlock_extent(io_tree, ordered_extent->file_offset,
1549 ordered_extent->file_offset + ordered_extent->len - 1,
1550 GFP_NOFS);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001551nocow:
Chris Masone6dcd2d2008-07-17 12:53:50 -04001552 add_pending_csums(trans, inode, ordered_extent->file_offset,
1553 &ordered_extent->list);
1554
Chris Mason34353022008-09-23 20:19:49 -04001555 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Chris Masondbe674a2008-07-17 12:54:05 -04001556 btrfs_ordered_update_i_size(inode, ordered_extent);
Chris Masone02119d2008-09-05 16:13:11 -04001557 btrfs_update_inode(trans, root, inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001558 btrfs_remove_ordered_extent(inode, ordered_extent);
Chris Mason34353022008-09-23 20:19:49 -04001559 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
Chris Mason7f3c74f2008-07-18 12:01:11 -04001560
Chris Masone6dcd2d2008-07-17 12:53:50 -04001561 /* once for us */
1562 btrfs_put_ordered_extent(ordered_extent);
1563 /* once for the tree */
1564 btrfs_put_ordered_extent(ordered_extent);
1565
Chris Masone6dcd2d2008-07-17 12:53:50 -04001566 btrfs_end_transaction(trans, root);
1567 return 0;
1568}
1569
Christoph Hellwigb2950862008-12-02 09:54:17 -05001570static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04001571 struct extent_state *state, int uptodate)
1572{
1573 return btrfs_finish_ordered_io(page->mapping->host, start, end);
1574}
1575
Chris Masond352ac62008-09-29 15:18:18 -04001576/*
1577 * When IO fails, either with EIO or csum verification fails, we
1578 * try other mirrors that might have a good copy of the data. This
1579 * io_failure_record is used to record state as we go through all the
1580 * mirrors. If another mirror has good data, the page is set up to date
1581 * and things continue. If a good mirror can't be found, the original
1582 * bio end_io callback is called to indicate things have failed.
1583 */
Chris Mason7e383262008-04-09 16:28:12 -04001584struct io_failure_record {
1585 struct page *page;
1586 u64 start;
1587 u64 len;
1588 u64 logical;
Chris Masond20f7042008-12-08 16:58:54 -05001589 unsigned long bio_flags;
Chris Mason7e383262008-04-09 16:28:12 -04001590 int last_mirror;
1591};
1592
Christoph Hellwigb2950862008-12-02 09:54:17 -05001593static int btrfs_io_failed_hook(struct bio *failed_bio,
Chris Mason1259ab72008-05-12 13:39:03 -04001594 struct page *page, u64 start, u64 end,
1595 struct extent_state *state)
Chris Mason7e383262008-04-09 16:28:12 -04001596{
1597 struct io_failure_record *failrec = NULL;
1598 u64 private;
1599 struct extent_map *em;
1600 struct inode *inode = page->mapping->host;
1601 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Chris Mason3b951512008-04-17 11:29:12 -04001602 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason7e383262008-04-09 16:28:12 -04001603 struct bio *bio;
1604 int num_copies;
1605 int ret;
Chris Mason1259ab72008-05-12 13:39:03 -04001606 int rw;
Chris Mason7e383262008-04-09 16:28:12 -04001607 u64 logical;
1608
1609 ret = get_state_private(failure_tree, start, &private);
1610 if (ret) {
Chris Mason7e383262008-04-09 16:28:12 -04001611 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
1612 if (!failrec)
1613 return -ENOMEM;
1614 failrec->start = start;
1615 failrec->len = end - start + 1;
1616 failrec->last_mirror = 0;
Chris Masond20f7042008-12-08 16:58:54 -05001617 failrec->bio_flags = 0;
Chris Mason7e383262008-04-09 16:28:12 -04001618
Chris Mason3b951512008-04-17 11:29:12 -04001619 spin_lock(&em_tree->lock);
1620 em = lookup_extent_mapping(em_tree, start, failrec->len);
1621 if (em->start > start || em->start + em->len < start) {
1622 free_extent_map(em);
1623 em = NULL;
1624 }
1625 spin_unlock(&em_tree->lock);
Chris Mason7e383262008-04-09 16:28:12 -04001626
1627 if (!em || IS_ERR(em)) {
1628 kfree(failrec);
1629 return -EIO;
1630 }
1631 logical = start - em->start;
1632 logical = em->block_start + logical;
Chris Masond20f7042008-12-08 16:58:54 -05001633 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
1634 logical = em->block_start;
1635 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
1636 }
Chris Mason7e383262008-04-09 16:28:12 -04001637 failrec->logical = logical;
1638 free_extent_map(em);
1639 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
1640 EXTENT_DIRTY, GFP_NOFS);
Chris Mason587f7702008-04-11 12:16:46 -04001641 set_state_private(failure_tree, start,
1642 (u64)(unsigned long)failrec);
Chris Mason7e383262008-04-09 16:28:12 -04001643 } else {
Chris Mason587f7702008-04-11 12:16:46 -04001644 failrec = (struct io_failure_record *)(unsigned long)private;
Chris Mason7e383262008-04-09 16:28:12 -04001645 }
1646 num_copies = btrfs_num_copies(
1647 &BTRFS_I(inode)->root->fs_info->mapping_tree,
1648 failrec->logical, failrec->len);
1649 failrec->last_mirror++;
1650 if (!state) {
1651 spin_lock_irq(&BTRFS_I(inode)->io_tree.lock);
1652 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
1653 failrec->start,
1654 EXTENT_LOCKED);
1655 if (state && state->start != failrec->start)
1656 state = NULL;
1657 spin_unlock_irq(&BTRFS_I(inode)->io_tree.lock);
1658 }
1659 if (!state || failrec->last_mirror > num_copies) {
1660 set_state_private(failure_tree, failrec->start, 0);
1661 clear_extent_bits(failure_tree, failrec->start,
1662 failrec->start + failrec->len - 1,
1663 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
1664 kfree(failrec);
1665 return -EIO;
1666 }
1667 bio = bio_alloc(GFP_NOFS, 1);
1668 bio->bi_private = state;
1669 bio->bi_end_io = failed_bio->bi_end_io;
1670 bio->bi_sector = failrec->logical >> 9;
1671 bio->bi_bdev = failed_bio->bi_bdev;
Chris Masone1c4b742008-04-22 13:26:46 -04001672 bio->bi_size = 0;
Chris Masond20f7042008-12-08 16:58:54 -05001673
Chris Mason7e383262008-04-09 16:28:12 -04001674 bio_add_page(bio, page, failrec->len, start - page_offset(page));
Chris Mason1259ab72008-05-12 13:39:03 -04001675 if (failed_bio->bi_rw & (1 << BIO_RW))
1676 rw = WRITE;
1677 else
1678 rw = READ;
1679
1680 BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
Chris Masonc8b97812008-10-29 14:49:59 -04001681 failrec->last_mirror,
Chris Masond20f7042008-12-08 16:58:54 -05001682 failrec->bio_flags);
Chris Mason1259ab72008-05-12 13:39:03 -04001683 return 0;
1684}
1685
Chris Masond352ac62008-09-29 15:18:18 -04001686/*
1687 * each time an IO finishes, we do a fast check in the IO failure tree
1688 * to see if we need to process or clean up an io_failure_record
1689 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001690static int btrfs_clean_io_failures(struct inode *inode, u64 start)
Chris Mason1259ab72008-05-12 13:39:03 -04001691{
1692 u64 private;
1693 u64 private_failure;
1694 struct io_failure_record *failure;
1695 int ret;
1696
1697 private = 0;
1698 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
1699 (u64)-1, 1, EXTENT_DIRTY)) {
1700 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
1701 start, &private_failure);
1702 if (ret == 0) {
1703 failure = (struct io_failure_record *)(unsigned long)
1704 private_failure;
1705 set_state_private(&BTRFS_I(inode)->io_failure_tree,
1706 failure->start, 0);
1707 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
1708 failure->start,
1709 failure->start + failure->len - 1,
1710 EXTENT_DIRTY | EXTENT_LOCKED,
1711 GFP_NOFS);
1712 kfree(failure);
1713 }
1714 }
Chris Mason7e383262008-04-09 16:28:12 -04001715 return 0;
1716}
1717
Chris Masond352ac62008-09-29 15:18:18 -04001718/*
1719 * when reads are done, we need to check csums to verify the data is correct
1720 * if there's a match, we allow the bio to finish. If not, we go through
1721 * the io_failure_record routines to find good copies
1722 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001723static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason70dec802008-01-29 09:59:12 -05001724 struct extent_state *state)
Chris Mason07157aa2007-08-30 08:50:51 -04001725{
Chris Mason35ebb932007-10-30 16:56:53 -04001726 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -04001727 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05001728 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04001729 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -05001730 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -04001731 int ret;
Chris Masonff79f812007-10-15 16:22:25 -04001732 struct btrfs_root *root = BTRFS_I(inode)->root;
1733 u32 csum = ~(u32)0;
Jens Axboebbf0d002007-10-19 09:23:07 -04001734 unsigned long flags;
Chris Masond1310b22008-01-24 16:13:08 -05001735
Chris Masond20f7042008-12-08 16:58:54 -05001736 if (PageChecked(page)) {
1737 ClearPageChecked(page);
1738 goto good;
1739 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001740 if (btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -05001741 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05001742
Yan Zheng17d217f2008-12-12 10:03:38 -05001743 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
1744 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1)) {
1745 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
1746 GFP_NOFS);
1747 return 0;
1748 }
1749
Yanc2e639f2008-02-04 08:57:25 -05001750 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -05001751 private = state->private;
1752 ret = 0;
1753 } else {
1754 ret = get_state_private(io_tree, start, &private);
1755 }
Jens Axboebbf0d002007-10-19 09:23:07 -04001756 local_irq_save(flags);
Chris Mason07157aa2007-08-30 08:50:51 -04001757 kaddr = kmap_atomic(page, KM_IRQ0);
1758 if (ret) {
1759 goto zeroit;
1760 }
Chris Masonff79f812007-10-15 16:22:25 -04001761 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
1762 btrfs_csum_final(csum, (char *)&csum);
1763 if (csum != private) {
Chris Mason07157aa2007-08-30 08:50:51 -04001764 goto zeroit;
1765 }
1766 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -04001767 local_irq_restore(flags);
Chris Masond20f7042008-12-08 16:58:54 -05001768good:
Chris Mason7e383262008-04-09 16:28:12 -04001769 /* if the io failure tree for this inode is non-empty,
1770 * check to see if we've recovered from a failed IO
1771 */
Chris Mason1259ab72008-05-12 13:39:03 -04001772 btrfs_clean_io_failures(inode, start);
Chris Mason07157aa2007-08-30 08:50:51 -04001773 return 0;
1774
1775zeroit:
Chris Masonaadfeb62008-01-29 09:10:27 -05001776 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
1777 page->mapping->host->i_ino, (unsigned long long)start, csum,
1778 private);
Chris Masondb945352007-10-15 16:15:53 -04001779 memset(kaddr + offset, 1, end - start + 1);
1780 flush_dcache_page(page);
Chris Mason07157aa2007-08-30 08:50:51 -04001781 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -04001782 local_irq_restore(flags);
Chris Mason3b951512008-04-17 11:29:12 -04001783 if (private == 0)
1784 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04001785 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04001786}
Chris Masonb888db22007-08-27 16:49:44 -04001787
Josef Bacik7b128762008-07-24 12:17:14 -04001788/*
1789 * This creates an orphan entry for the given inode in case something goes
1790 * wrong in the middle of an unlink/truncate.
1791 */
1792int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
1793{
1794 struct btrfs_root *root = BTRFS_I(inode)->root;
1795 int ret = 0;
1796
Yanbcc63ab2008-07-30 16:29:20 -04001797 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001798
1799 /* already on the orphan list, we're good */
1800 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
Yanbcc63ab2008-07-30 16:29:20 -04001801 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001802 return 0;
1803 }
1804
1805 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
1806
Yanbcc63ab2008-07-30 16:29:20 -04001807 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001808
1809 /*
1810 * insert an orphan item to track this unlinked/truncated file
1811 */
1812 ret = btrfs_insert_orphan_item(trans, root, inode->i_ino);
1813
1814 return ret;
1815}
1816
1817/*
1818 * We have done the truncate/delete so we can go ahead and remove the orphan
1819 * item for this particular inode.
1820 */
1821int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
1822{
1823 struct btrfs_root *root = BTRFS_I(inode)->root;
1824 int ret = 0;
1825
Yanbcc63ab2008-07-30 16:29:20 -04001826 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001827
1828 if (list_empty(&BTRFS_I(inode)->i_orphan)) {
Yanbcc63ab2008-07-30 16:29:20 -04001829 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001830 return 0;
1831 }
1832
1833 list_del_init(&BTRFS_I(inode)->i_orphan);
1834 if (!trans) {
Yanbcc63ab2008-07-30 16:29:20 -04001835 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001836 return 0;
1837 }
1838
Yanbcc63ab2008-07-30 16:29:20 -04001839 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001840
1841 ret = btrfs_del_orphan_item(trans, root, inode->i_ino);
1842
1843 return ret;
1844}
1845
1846/*
1847 * this cleans up any orphans that may be left on the list from the last use
1848 * of this root.
1849 */
1850void btrfs_orphan_cleanup(struct btrfs_root *root)
1851{
1852 struct btrfs_path *path;
1853 struct extent_buffer *leaf;
1854 struct btrfs_item *item;
1855 struct btrfs_key key, found_key;
1856 struct btrfs_trans_handle *trans;
1857 struct inode *inode;
1858 int ret = 0, nr_unlink = 0, nr_truncate = 0;
1859
Josef Bacik7b128762008-07-24 12:17:14 -04001860 path = btrfs_alloc_path();
1861 if (!path)
1862 return;
1863 path->reada = -1;
1864
1865 key.objectid = BTRFS_ORPHAN_OBJECTID;
1866 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
1867 key.offset = (u64)-1;
1868
Josef Bacik7b128762008-07-24 12:17:14 -04001869
1870 while (1) {
1871 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1872 if (ret < 0) {
1873 printk(KERN_ERR "Error searching slot for orphan: %d"
1874 "\n", ret);
1875 break;
1876 }
1877
1878 /*
1879 * if ret == 0 means we found what we were searching for, which
1880 * is weird, but possible, so only screw with path if we didnt
1881 * find the key and see if we have stuff that matches
1882 */
1883 if (ret > 0) {
1884 if (path->slots[0] == 0)
1885 break;
1886 path->slots[0]--;
1887 }
1888
1889 /* pull out the item */
1890 leaf = path->nodes[0];
1891 item = btrfs_item_nr(leaf, path->slots[0]);
1892 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1893
1894 /* make sure the item matches what we want */
1895 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
1896 break;
1897 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
1898 break;
1899
1900 /* release the path since we're done with it */
1901 btrfs_release_path(root, path);
1902
1903 /*
1904 * this is where we are basically btrfs_lookup, without the
1905 * crossing root thing. we store the inode number in the
1906 * offset of the orphan item.
1907 */
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001908 inode = btrfs_iget_locked(root->fs_info->sb,
Josef Bacik7b128762008-07-24 12:17:14 -04001909 found_key.offset, root);
1910 if (!inode)
1911 break;
1912
1913 if (inode->i_state & I_NEW) {
1914 BTRFS_I(inode)->root = root;
1915
1916 /* have to set the location manually */
1917 BTRFS_I(inode)->location.objectid = inode->i_ino;
1918 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
1919 BTRFS_I(inode)->location.offset = 0;
1920
1921 btrfs_read_locked_inode(inode);
1922 unlock_new_inode(inode);
1923 }
1924
1925 /*
1926 * add this inode to the orphan list so btrfs_orphan_del does
1927 * the proper thing when we hit it
1928 */
Yanbcc63ab2008-07-30 16:29:20 -04001929 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001930 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
Yanbcc63ab2008-07-30 16:29:20 -04001931 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001932
1933 /*
1934 * if this is a bad inode, means we actually succeeded in
1935 * removing the inode, but not the orphan record, which means
1936 * we need to manually delete the orphan since iput will just
1937 * do a destroy_inode
1938 */
1939 if (is_bad_inode(inode)) {
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001940 trans = btrfs_start_transaction(root, 1);
Josef Bacik7b128762008-07-24 12:17:14 -04001941 btrfs_orphan_del(trans, inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001942 btrfs_end_transaction(trans, root);
Josef Bacik7b128762008-07-24 12:17:14 -04001943 iput(inode);
1944 continue;
1945 }
1946
1947 /* if we have links, this was a truncate, lets do that */
1948 if (inode->i_nlink) {
1949 nr_truncate++;
1950 btrfs_truncate(inode);
1951 } else {
1952 nr_unlink++;
1953 }
1954
1955 /* this will do delete_inode and everything for us */
1956 iput(inode);
1957 }
1958
1959 if (nr_unlink)
1960 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
1961 if (nr_truncate)
1962 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
1963
1964 btrfs_free_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04001965}
1966
Chris Masond352ac62008-09-29 15:18:18 -04001967/*
1968 * read an inode from the btree into the in-memory inode
1969 */
Chris Mason39279cc2007-06-12 06:35:45 -04001970void btrfs_read_locked_inode(struct inode *inode)
1971{
1972 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04001973 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001974 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04001975 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04001976 struct btrfs_root *root = BTRFS_I(inode)->root;
1977 struct btrfs_key location;
1978 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -04001979 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04001980 int ret;
1981
1982 path = btrfs_alloc_path();
1983 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -04001984 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05001985
Chris Mason39279cc2007-06-12 06:35:45 -04001986 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001987 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04001988 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04001989
Chris Mason5f39d392007-10-15 16:14:19 -04001990 leaf = path->nodes[0];
1991 inode_item = btrfs_item_ptr(leaf, path->slots[0],
1992 struct btrfs_inode_item);
1993
1994 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
1995 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
1996 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
1997 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
Chris Masondbe674a2008-07-17 12:54:05 -04001998 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04001999
2000 tspec = btrfs_inode_atime(inode_item);
2001 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2002 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2003
2004 tspec = btrfs_inode_mtime(inode_item);
2005 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2006 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2007
2008 tspec = btrfs_inode_ctime(inode_item);
2009 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2010 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2011
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002012 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04002013 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Chris Masonc3027eb2008-12-08 16:40:21 -05002014 BTRFS_I(inode)->sequence = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04002015 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04002016 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002017 rdev = btrfs_inode_rdev(leaf, inode_item);
2018
Josef Bacikaec74772008-07-24 12:12:38 -04002019 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05002020 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Josef Bacikaec74772008-07-24 12:12:38 -04002021
Chris Mason5f39d392007-10-15 16:14:19 -04002022 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002023 BTRFS_I(inode)->block_group = btrfs_find_block_group(root, 0,
2024 alloc_group_block, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002025 btrfs_free_path(path);
2026 inode_item = NULL;
2027
Chris Mason39279cc2007-06-12 06:35:45 -04002028 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04002029 case S_IFREG:
2030 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04002031 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05002032 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002033 inode->i_fop = &btrfs_file_operations;
2034 inode->i_op = &btrfs_file_inode_operations;
2035 break;
2036 case S_IFDIR:
2037 inode->i_fop = &btrfs_dir_file_operations;
2038 if (root == root->fs_info->tree_root)
2039 inode->i_op = &btrfs_dir_ro_inode_operations;
2040 else
2041 inode->i_op = &btrfs_dir_inode_operations;
2042 break;
2043 case S_IFLNK:
2044 inode->i_op = &btrfs_symlink_inode_operations;
2045 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04002046 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04002047 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04002048 default:
2049 init_special_inode(inode, inode->i_mode, rdev);
2050 break;
Chris Mason39279cc2007-06-12 06:35:45 -04002051 }
2052 return;
2053
2054make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04002055 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002056 make_bad_inode(inode);
2057}
2058
Chris Masond352ac62008-09-29 15:18:18 -04002059/*
2060 * given a leaf and an inode, copy the inode fields into the leaf
2061 */
Chris Masone02119d2008-09-05 16:13:11 -04002062static void fill_inode_item(struct btrfs_trans_handle *trans,
2063 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04002064 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04002065 struct inode *inode)
2066{
Chris Mason5f39d392007-10-15 16:14:19 -04002067 btrfs_set_inode_uid(leaf, item, inode->i_uid);
2068 btrfs_set_inode_gid(leaf, item, inode->i_gid);
Chris Masondbe674a2008-07-17 12:54:05 -04002069 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
Chris Mason5f39d392007-10-15 16:14:19 -04002070 btrfs_set_inode_mode(leaf, item, inode->i_mode);
2071 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
2072
2073 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
2074 inode->i_atime.tv_sec);
2075 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
2076 inode->i_atime.tv_nsec);
2077
2078 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
2079 inode->i_mtime.tv_sec);
2080 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
2081 inode->i_mtime.tv_nsec);
2082
2083 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
2084 inode->i_ctime.tv_sec);
2085 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
2086 inode->i_ctime.tv_nsec);
2087
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002088 btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode));
Chris Masone02119d2008-09-05 16:13:11 -04002089 btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
Chris Masonc3027eb2008-12-08 16:40:21 -05002090 btrfs_set_inode_sequence(leaf, item, BTRFS_I(inode)->sequence);
Chris Masone02119d2008-09-05 16:13:11 -04002091 btrfs_set_inode_transid(leaf, item, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002092 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -05002093 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002094 btrfs_set_inode_block_group(leaf, item, BTRFS_I(inode)->block_group);
Chris Mason39279cc2007-06-12 06:35:45 -04002095}
2096
Chris Masond352ac62008-09-29 15:18:18 -04002097/*
2098 * copy everything in the in-memory inode into the btree.
2099 */
Chris Masonba1da2f2008-07-17 12:54:15 -04002100int noinline btrfs_update_inode(struct btrfs_trans_handle *trans,
Chris Mason39279cc2007-06-12 06:35:45 -04002101 struct btrfs_root *root,
2102 struct inode *inode)
2103{
2104 struct btrfs_inode_item *inode_item;
2105 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002106 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002107 int ret;
2108
2109 path = btrfs_alloc_path();
2110 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -04002111 ret = btrfs_lookup_inode(trans, root, path,
2112 &BTRFS_I(inode)->location, 1);
2113 if (ret) {
2114 if (ret > 0)
2115 ret = -ENOENT;
2116 goto failed;
2117 }
2118
Chris Mason5f39d392007-10-15 16:14:19 -04002119 leaf = path->nodes[0];
2120 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04002121 struct btrfs_inode_item);
2122
Chris Masone02119d2008-09-05 16:13:11 -04002123 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002124 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002125 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002126 ret = 0;
2127failed:
Chris Mason39279cc2007-06-12 06:35:45 -04002128 btrfs_free_path(path);
2129 return ret;
2130}
2131
2132
Chris Masond352ac62008-09-29 15:18:18 -04002133/*
2134 * unlink helper that gets used here in inode.c and in the tree logging
2135 * recovery code. It remove a link in a directory with a given name, and
2136 * also drops the back refs in the inode to the directory
2137 */
Chris Masone02119d2008-09-05 16:13:11 -04002138int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2139 struct btrfs_root *root,
2140 struct inode *dir, struct inode *inode,
2141 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04002142{
2143 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04002144 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002145 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002146 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04002147 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04002148 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04002149
2150 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04002151 if (!path) {
2152 ret = -ENOMEM;
2153 goto err;
2154 }
2155
Chris Mason39279cc2007-06-12 06:35:45 -04002156 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
2157 name, name_len, -1);
2158 if (IS_ERR(di)) {
2159 ret = PTR_ERR(di);
2160 goto err;
2161 }
2162 if (!di) {
2163 ret = -ENOENT;
2164 goto err;
2165 }
Chris Mason5f39d392007-10-15 16:14:19 -04002166 leaf = path->nodes[0];
2167 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04002168 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04002169 if (ret)
2170 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -04002171 btrfs_release_path(root, path);
2172
Josef Bacikaec74772008-07-24 12:12:38 -04002173 ret = btrfs_del_inode_ref(trans, root, name, name_len,
Chris Masone02119d2008-09-05 16:13:11 -04002174 inode->i_ino,
2175 dir->i_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04002176 if (ret) {
2177 printk("failed to delete reference to %.*s, "
2178 "inode %lu parent %lu\n", name_len, name,
Chris Masone02119d2008-09-05 16:13:11 -04002179 inode->i_ino, dir->i_ino);
Josef Bacikaec74772008-07-24 12:12:38 -04002180 goto err;
2181 }
2182
Chris Mason39279cc2007-06-12 06:35:45 -04002183 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04002184 index, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -04002185 if (IS_ERR(di)) {
2186 ret = PTR_ERR(di);
2187 goto err;
2188 }
2189 if (!di) {
2190 ret = -ENOENT;
2191 goto err;
2192 }
2193 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason925baed2008-06-25 16:01:30 -04002194 btrfs_release_path(root, path);
Chris Mason39279cc2007-06-12 06:35:45 -04002195
Chris Masone02119d2008-09-05 16:13:11 -04002196 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
2197 inode, dir->i_ino);
Chris Mason49eb7e42008-09-11 15:53:12 -04002198 BUG_ON(ret != 0 && ret != -ENOENT);
2199 if (ret != -ENOENT)
2200 BTRFS_I(dir)->log_dirty_trans = trans->transid;
Chris Masone02119d2008-09-05 16:13:11 -04002201
2202 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
2203 dir, index);
2204 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04002205err:
2206 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002207 if (ret)
2208 goto out;
2209
2210 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
2211 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2212 btrfs_update_inode(trans, root, dir);
2213 btrfs_drop_nlink(inode);
2214 ret = btrfs_update_inode(trans, root, inode);
2215 dir->i_sb->s_dirt = 1;
2216out:
Chris Mason39279cc2007-06-12 06:35:45 -04002217 return ret;
2218}
2219
2220static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
2221{
2222 struct btrfs_root *root;
2223 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04002224 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002225 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -05002226 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002227
2228 root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05002229
2230 ret = btrfs_check_free_space(root, 1, 1);
2231 if (ret)
2232 goto fail;
2233
Chris Mason39279cc2007-06-12 06:35:45 -04002234 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002235
Chris Mason39279cc2007-06-12 06:35:45 -04002236 btrfs_set_trans_block_group(trans, dir);
Chris Masone02119d2008-09-05 16:13:11 -04002237 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
2238 dentry->d_name.name, dentry->d_name.len);
Josef Bacik7b128762008-07-24 12:17:14 -04002239
2240 if (inode->i_nlink == 0)
2241 ret = btrfs_orphan_add(trans, inode);
2242
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002243 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04002244
Chris Mason89ce8a62008-06-25 16:01:31 -04002245 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002246fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002247 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002248 return ret;
2249}
2250
2251static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
2252{
2253 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002254 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002255 int ret;
2256 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04002257 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -05002258 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002259
Chris Mason3394e162008-11-17 20:42:26 -05002260 /*
2261 * the FIRST_FREE_OBJECTID check makes sure we don't try to rmdir
2262 * the root of a subvolume or snapshot
2263 */
2264 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE ||
2265 inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) {
Yan134d4512007-10-25 15:49:25 -04002266 return -ENOTEMPTY;
Chris Mason925baed2008-06-25 16:01:30 -04002267 }
Yan134d4512007-10-25 15:49:25 -04002268
Chris Mason1832a6d2007-12-21 16:27:21 -05002269 ret = btrfs_check_free_space(root, 1, 1);
2270 if (ret)
2271 goto fail;
2272
Chris Mason39279cc2007-06-12 06:35:45 -04002273 trans = btrfs_start_transaction(root, 1);
2274 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -04002275
Josef Bacik7b128762008-07-24 12:17:14 -04002276 err = btrfs_orphan_add(trans, inode);
2277 if (err)
2278 goto fail_trans;
2279
Chris Mason39279cc2007-06-12 06:35:45 -04002280 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04002281 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
2282 dentry->d_name.name, dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04002283 if (!err) {
Chris Masondbe674a2008-07-17 12:54:05 -04002284 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002285 }
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);
2519 if (ret < 0) {
2520 goto error;
2521 }
2522 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 Mason39279cc2007-06-12 06:35:45 -04002533 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 Masonb888db22007-08-27 16:49:44 -04002565 if (found_type == BTRFS_DIR_ITEM_KEY) {
2566 found_type = BTRFS_INODE_ITEM_KEY;
2567 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
Chris Masond20f7042008-12-08 16:58:54 -05002568 found_type = BTRFS_EXTENT_DATA_KEY;
Chris Mason85e21ba2008-01-29 15:11:36 -05002569 } else if (found_type == BTRFS_EXTENT_DATA_KEY) {
2570 found_type = BTRFS_XATTR_ITEM_KEY;
2571 } else if (found_type == BTRFS_XATTR_ITEM_KEY) {
2572 found_type = BTRFS_INODE_REF_KEY;
Chris Masonb888db22007-08-27 16:49:44 -04002573 } else if (found_type) {
2574 found_type--;
2575 } else {
2576 break;
Chris Mason39279cc2007-06-12 06:35:45 -04002577 }
Yana61721d2007-09-17 11:08:38 -04002578 btrfs_set_key_type(&key, found_type);
Chris Mason85e21ba2008-01-29 15:11:36 -05002579 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04002580 }
Chris Masone02119d2008-09-05 16:13:11 -04002581 if (found_key.offset >= new_size)
Chris Mason39279cc2007-06-12 06:35:45 -04002582 del_item = 1;
2583 else
2584 del_item = 0;
2585 found_extent = 0;
2586
2587 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04002588 if (found_type != BTRFS_EXTENT_DATA_KEY)
2589 goto delete;
2590
2591 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04002592 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04002593 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason771ed682008-11-06 22:02:51 -05002594 if (!del_item && !encoding) {
Chris Masondb945352007-10-15 16:15:53 -04002595 u64 orig_num_bytes =
2596 btrfs_file_extent_num_bytes(leaf, fi);
Chris Masone02119d2008-09-05 16:13:11 -04002597 extent_num_bytes = new_size -
Chris Mason5f39d392007-10-15 16:14:19 -04002598 found_key.offset + root->sectorsize - 1;
Yanb1632b12008-01-30 11:54:04 -05002599 extent_num_bytes = extent_num_bytes &
2600 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04002601 btrfs_set_file_extent_num_bytes(leaf, fi,
2602 extent_num_bytes);
2603 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05002604 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04002605 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002606 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04002607 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002608 } else {
Chris Masondb945352007-10-15 16:15:53 -04002609 extent_num_bytes =
2610 btrfs_file_extent_disk_num_bytes(leaf,
2611 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04002612 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05002613 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04002614 if (extent_start != 0) {
2615 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04002616 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002617 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04002618 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002619 root_gen = btrfs_header_generation(leaf);
Chris Masond8d5f3e2007-12-11 12:42:00 -05002620 root_owner = btrfs_header_owner(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002621 }
Chris Mason90692182008-02-08 13:49:28 -05002622 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04002623 /*
2624 * we can't truncate inline items that have had
2625 * special encodings
2626 */
2627 if (!del_item &&
2628 btrfs_file_extent_compression(leaf, fi) == 0 &&
2629 btrfs_file_extent_encryption(leaf, fi) == 0 &&
2630 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002631 u32 size = new_size - found_key.offset;
2632
2633 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002634 inode_sub_bytes(inode, item_end + 1 -
2635 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04002636 }
2637 size =
2638 btrfs_file_extent_calc_inline_size(size);
Chris Mason90692182008-02-08 13:49:28 -05002639 ret = btrfs_truncate_item(trans, root, path,
Chris Masone02119d2008-09-05 16:13:11 -04002640 size, 1);
Chris Mason90692182008-02-08 13:49:28 -05002641 BUG_ON(ret);
Chris Masone02119d2008-09-05 16:13:11 -04002642 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002643 inode_sub_bytes(inode, item_end + 1 -
2644 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05002645 }
Chris Mason39279cc2007-06-12 06:35:45 -04002646 }
Chris Mason179e29e2007-11-01 11:28:41 -04002647delete:
Chris Mason39279cc2007-06-12 06:35:45 -04002648 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05002649 if (!pending_del_nr) {
2650 /* no pending yet, add ourselves */
2651 pending_del_slot = path->slots[0];
2652 pending_del_nr = 1;
2653 } else if (pending_del_nr &&
2654 path->slots[0] + 1 == pending_del_slot) {
2655 /* hop on the pending chunk */
2656 pending_del_nr++;
2657 pending_del_slot = path->slots[0];
2658 } else {
2659 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot);
2660 }
Chris Mason39279cc2007-06-12 06:35:45 -04002661 } else {
2662 break;
2663 }
Chris Mason39279cc2007-06-12 06:35:45 -04002664 if (found_extent) {
2665 ret = btrfs_free_extent(trans, root, extent_start,
Chris Mason7bb86312007-12-11 09:25:06 -05002666 extent_num_bytes,
Zheng Yan31840ae2008-09-23 13:14:14 -04002667 leaf->start, root_owner,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002668 root_gen, inode->i_ino, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002669 BUG_ON(ret);
2670 }
Chris Mason85e21ba2008-01-29 15:11:36 -05002671next:
2672 if (path->slots[0] == 0) {
2673 if (pending_del_nr)
2674 goto del_pending;
2675 btrfs_release_path(root, path);
2676 goto search_again;
2677 }
2678
2679 path->slots[0]--;
2680 if (pending_del_nr &&
2681 path->slots[0] + 1 != pending_del_slot) {
2682 struct btrfs_key debug;
2683del_pending:
2684 btrfs_item_key_to_cpu(path->nodes[0], &debug,
2685 pending_del_slot);
2686 ret = btrfs_del_items(trans, root, path,
2687 pending_del_slot,
2688 pending_del_nr);
2689 BUG_ON(ret);
2690 pending_del_nr = 0;
2691 btrfs_release_path(root, path);
2692 goto search_again;
2693 }
Chris Mason39279cc2007-06-12 06:35:45 -04002694 }
2695 ret = 0;
2696error:
Chris Mason85e21ba2008-01-29 15:11:36 -05002697 if (pending_del_nr) {
2698 ret = btrfs_del_items(trans, root, path, pending_del_slot,
2699 pending_del_nr);
2700 }
Chris Mason39279cc2007-06-12 06:35:45 -04002701 btrfs_free_path(path);
2702 inode->i_sb->s_dirt = 1;
2703 return ret;
2704}
2705
Chris Masona52d9a82007-08-27 16:49:44 -04002706/*
2707 * taken from block_truncate_page, but does cow as it zeros out
2708 * any bytes left in the last page in the file.
2709 */
2710static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
2711{
2712 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -04002713 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002714 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2715 struct btrfs_ordered_extent *ordered;
2716 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04002717 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04002718 pgoff_t index = from >> PAGE_CACHE_SHIFT;
2719 unsigned offset = from & (PAGE_CACHE_SIZE-1);
2720 struct page *page;
2721 int ret = 0;
2722 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002723 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04002724
2725 if ((offset & (blocksize - 1)) == 0)
2726 goto out;
2727
2728 ret = -ENOMEM;
Chris Mason211c17f2008-05-15 09:13:45 -04002729again:
Chris Masona52d9a82007-08-27 16:49:44 -04002730 page = grab_cache_page(mapping, index);
2731 if (!page)
2732 goto out;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002733
2734 page_start = page_offset(page);
2735 page_end = page_start + PAGE_CACHE_SIZE - 1;
2736
Chris Masona52d9a82007-08-27 16:49:44 -04002737 if (!PageUptodate(page)) {
2738 ret = btrfs_readpage(NULL, page);
2739 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04002740 if (page->mapping != mapping) {
2741 unlock_page(page);
2742 page_cache_release(page);
2743 goto again;
2744 }
Chris Masona52d9a82007-08-27 16:49:44 -04002745 if (!PageUptodate(page)) {
2746 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04002747 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04002748 }
2749 }
Chris Mason211c17f2008-05-15 09:13:45 -04002750 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002751
2752 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
2753 set_page_extent_mapped(page);
2754
2755 ordered = btrfs_lookup_ordered_extent(inode, page_start);
2756 if (ordered) {
2757 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
2758 unlock_page(page);
2759 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04002760 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002761 btrfs_put_ordered_extent(ordered);
2762 goto again;
2763 }
2764
Chris Masonea8c2812008-08-04 23:17:27 -04002765 btrfs_set_extent_delalloc(inode, page_start, page_end);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002766 ret = 0;
2767 if (offset != PAGE_CACHE_SIZE) {
2768 kaddr = kmap(page);
2769 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
2770 flush_dcache_page(page);
2771 kunmap(page);
2772 }
Chris Mason247e7432008-07-17 12:53:51 -04002773 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002774 set_page_dirty(page);
2775 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04002776
Chris Mason89642222008-07-24 09:41:53 -04002777out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04002778 unlock_page(page);
2779 page_cache_release(page);
2780out:
2781 return ret;
2782}
2783
Yan Zheng9036c102008-10-30 14:19:41 -04002784int btrfs_cont_expand(struct inode *inode, loff_t size)
2785{
2786 struct btrfs_trans_handle *trans;
2787 struct btrfs_root *root = BTRFS_I(inode)->root;
2788 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2789 struct extent_map *em;
2790 u64 mask = root->sectorsize - 1;
2791 u64 hole_start = (inode->i_size + mask) & ~mask;
2792 u64 block_end = (size + mask) & ~mask;
2793 u64 last_byte;
2794 u64 cur_offset;
2795 u64 hole_size;
2796 int err;
2797
2798 if (size <= hole_start)
2799 return 0;
2800
2801 err = btrfs_check_free_space(root, 1, 0);
2802 if (err)
2803 return err;
2804
2805 btrfs_truncate_page(inode->i_mapping, inode->i_size);
2806
2807 while (1) {
2808 struct btrfs_ordered_extent *ordered;
2809 btrfs_wait_ordered_range(inode, hole_start,
2810 block_end - hole_start);
2811 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
2812 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
2813 if (!ordered)
2814 break;
2815 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
2816 btrfs_put_ordered_extent(ordered);
2817 }
2818
2819 trans = btrfs_start_transaction(root, 1);
2820 btrfs_set_trans_block_group(trans, inode);
2821
2822 cur_offset = hole_start;
2823 while (1) {
2824 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
2825 block_end - cur_offset, 0);
2826 BUG_ON(IS_ERR(em) || !em);
2827 last_byte = min(extent_map_end(em), block_end);
2828 last_byte = (last_byte + mask) & ~mask;
2829 if (test_bit(EXTENT_FLAG_VACANCY, &em->flags)) {
Chris Mason771ed682008-11-06 22:02:51 -05002830 u64 hint_byte = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04002831 hole_size = last_byte - cur_offset;
Chris Mason771ed682008-11-06 22:02:51 -05002832 err = btrfs_drop_extents(trans, root, inode,
2833 cur_offset,
2834 cur_offset + hole_size,
2835 cur_offset, &hint_byte);
2836 if (err)
2837 break;
Yan Zheng9036c102008-10-30 14:19:41 -04002838 err = btrfs_insert_file_extent(trans, root,
2839 inode->i_ino, cur_offset, 0,
2840 0, hole_size, 0, hole_size,
2841 0, 0, 0);
2842 btrfs_drop_extent_cache(inode, hole_start,
2843 last_byte - 1, 0);
2844 }
2845 free_extent_map(em);
2846 cur_offset = last_byte;
2847 if (err || cur_offset >= block_end)
2848 break;
2849 }
2850
2851 btrfs_end_transaction(trans, root);
2852 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
2853 return err;
2854}
2855
Chris Mason39279cc2007-06-12 06:35:45 -04002856static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
2857{
2858 struct inode *inode = dentry->d_inode;
2859 int err;
2860
2861 err = inode_change_ok(inode, attr);
2862 if (err)
2863 return err;
2864
2865 if (S_ISREG(inode->i_mode) &&
2866 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
Yan Zheng9036c102008-10-30 14:19:41 -04002867 err = btrfs_cont_expand(inode, attr->ia_size);
Chris Mason54aa1f42007-06-22 14:16:25 -04002868 if (err)
2869 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04002870 }
Yan Zheng9036c102008-10-30 14:19:41 -04002871
Chris Mason39279cc2007-06-12 06:35:45 -04002872 err = inode_setattr(inode, attr);
Josef Bacik33268ea2008-07-24 12:16:36 -04002873
2874 if (!err && ((attr->ia_valid & ATTR_MODE)))
2875 err = btrfs_acl_chmod(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002876 return err;
2877}
Chris Mason61295eb2008-01-14 16:24:38 -05002878
Chris Mason39279cc2007-06-12 06:35:45 -04002879void btrfs_delete_inode(struct inode *inode)
2880{
2881 struct btrfs_trans_handle *trans;
2882 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002883 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04002884 int ret;
2885
2886 truncate_inode_pages(&inode->i_data, 0);
2887 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04002888 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002889 goto no_delete;
2890 }
Chris Mason4a096752008-07-21 10:29:44 -04002891 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04002892
Chris Masondbe674a2008-07-17 12:54:05 -04002893 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002894 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002895
Chris Mason39279cc2007-06-12 06:35:45 -04002896 btrfs_set_trans_block_group(trans, inode);
Chris Masone02119d2008-09-05 16:13:11 -04002897 ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size, 0);
Josef Bacik7b128762008-07-24 12:17:14 -04002898 if (ret) {
2899 btrfs_orphan_del(NULL, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04002900 goto no_delete_lock;
Josef Bacik7b128762008-07-24 12:17:14 -04002901 }
2902
2903 btrfs_orphan_del(trans, inode);
Chris Mason85e21ba2008-01-29 15:11:36 -05002904
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002905 nr = trans->blocks_used;
Chris Mason85e21ba2008-01-29 15:11:36 -05002906 clear_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002907
Chris Mason39279cc2007-06-12 06:35:45 -04002908 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002909 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002910 return;
Chris Mason54aa1f42007-06-22 14:16:25 -04002911
2912no_delete_lock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002913 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04002914 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002915 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002916no_delete:
2917 clear_inode(inode);
2918}
2919
2920/*
2921 * this returns the key found in the dir entry in the location pointer.
2922 * If no dir entries were found, location->objectid is 0.
2923 */
2924static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
2925 struct btrfs_key *location)
2926{
2927 const char *name = dentry->d_name.name;
2928 int namelen = dentry->d_name.len;
2929 struct btrfs_dir_item *di;
2930 struct btrfs_path *path;
2931 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04002932 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002933
2934 path = btrfs_alloc_path();
2935 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -05002936
Chris Mason39279cc2007-06-12 06:35:45 -04002937 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
2938 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04002939 if (IS_ERR(di))
2940 ret = PTR_ERR(di);
Chris Mason39279cc2007-06-12 06:35:45 -04002941 if (!di || IS_ERR(di)) {
Chris Mason39544012007-12-12 14:38:19 -05002942 goto out_err;
Chris Mason39279cc2007-06-12 06:35:45 -04002943 }
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;
3023 return (args->ino == inode->i_ino &&
3024 args->root == BTRFS_I(inode)->root);
3025}
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{
3088 struct inode * inode;
3089 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)
3277 filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
3278 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 Mason8d5bf1c2008-09-11 15:51:21 -04003388 if (ret) {
Josef Bacikaec74772008-07-24 12:12:38 -04003389 return ret;
Chris Mason8d5bf1c2008-09-11 15:51:21 -04003390 }
Josef Bacikaec74772008-07-24 12:12:38 -04003391 }
3392
Chris Mason00e4e6b2008-08-05 11:18:09 -04003393 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04003394 BTRFS_I(dir)->index_cnt++;
3395
3396 return ret;
3397}
3398
Chris Mason39279cc2007-06-12 06:35:45 -04003399static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
3400 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04003401 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05003402 const char *name, int name_len,
Yan Zhengd2fb3432008-12-11 16:30:39 -05003403 u64 ref_objectid, u64 objectid,
3404 u64 alloc_hint, int mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04003405{
3406 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04003407 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04003408 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04003409 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05003410 struct btrfs_inode_ref *ref;
3411 struct btrfs_key key[2];
3412 u32 sizes[2];
3413 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04003414 int ret;
3415 int owner;
3416
Chris Mason5f39d392007-10-15 16:14:19 -04003417 path = btrfs_alloc_path();
3418 BUG_ON(!path);
3419
Chris Mason39279cc2007-06-12 06:35:45 -04003420 inode = new_inode(root->fs_info->sb);
3421 if (!inode)
3422 return ERR_PTR(-ENOMEM);
3423
Josef Bacikaec74772008-07-24 12:12:38 -04003424 if (dir) {
Chris Mason3de45862008-11-17 21:02:50 -05003425 ret = btrfs_set_inode_index(dir, index);
Josef Bacikaec74772008-07-24 12:12:38 -04003426 if (ret)
3427 return ERR_PTR(ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003428 }
3429 /*
3430 * index_cnt is ignored for everything but a dir,
3431 * btrfs_get_inode_index_count has an explanation for the magic
3432 * number
3433 */
Chris Masone02119d2008-09-05 16:13:11 -04003434 init_btrfs_i(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04003435 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04003436 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04003437 BTRFS_I(inode)->generation = trans->transid;
Chris Masonb888db22007-08-27 16:49:44 -04003438
Chris Mason39279cc2007-06-12 06:35:45 -04003439 if (mode & S_IFDIR)
3440 owner = 0;
3441 else
3442 owner = 1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003443 BTRFS_I(inode)->block_group =
3444 btrfs_find_block_group(root, 0, alloc_hint, owner);
Yan Zheng17d217f2008-12-12 10:03:38 -05003445 if ((mode & S_IFREG)) {
3446 if (btrfs_test_opt(root, NODATASUM))
3447 btrfs_set_flag(inode, NODATASUM);
3448 if (btrfs_test_opt(root, NODATACOW))
3449 btrfs_set_flag(inode, NODATACOW);
3450 }
Chris Mason9c583092008-01-29 15:15:18 -05003451
3452 key[0].objectid = objectid;
3453 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
3454 key[0].offset = 0;
3455
3456 key[1].objectid = objectid;
3457 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
3458 key[1].offset = ref_objectid;
3459
3460 sizes[0] = sizeof(struct btrfs_inode_item);
3461 sizes[1] = name_len + sizeof(*ref);
3462
3463 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
3464 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04003465 goto fail;
3466
Chris Mason9c583092008-01-29 15:15:18 -05003467 if (objectid > root->highest_inode)
3468 root->highest_inode = objectid;
3469
Chris Mason79683f22008-11-19 22:00:53 -05003470 inode->i_uid = current_fsuid();
3471 inode->i_gid = current_fsgid();
Chris Mason39279cc2007-06-12 06:35:45 -04003472 inode->i_mode = mode;
3473 inode->i_ino = objectid;
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003474 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003475 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04003476 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3477 struct btrfs_inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003478 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05003479
3480 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
3481 struct btrfs_inode_ref);
3482 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04003483 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05003484 ptr = (unsigned long)(ref + 1);
3485 write_extent_buffer(path->nodes[0], name, ptr, name_len);
3486
Chris Mason5f39d392007-10-15 16:14:19 -04003487 btrfs_mark_buffer_dirty(path->nodes[0]);
3488 btrfs_free_path(path);
3489
Chris Mason39279cc2007-06-12 06:35:45 -04003490 location = &BTRFS_I(inode)->location;
3491 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04003492 location->offset = 0;
3493 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
3494
Chris Mason39279cc2007-06-12 06:35:45 -04003495 insert_inode_hash(inode);
3496 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04003497fail:
Josef Bacikaec74772008-07-24 12:12:38 -04003498 if (dir)
3499 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04003500 btrfs_free_path(path);
3501 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003502}
3503
3504static inline u8 btrfs_inode_type(struct inode *inode)
3505{
3506 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
3507}
3508
Chris Masond352ac62008-09-29 15:18:18 -04003509/*
3510 * utility function to add 'inode' into 'parent_inode' with
3511 * a give name and a given sequence number.
3512 * if 'add_backref' is true, also insert a backref from the
3513 * inode to the parent directory.
3514 */
Chris Masone02119d2008-09-05 16:13:11 -04003515int btrfs_add_link(struct btrfs_trans_handle *trans,
3516 struct inode *parent_inode, struct inode *inode,
3517 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04003518{
3519 int ret;
3520 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04003521 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Chris Mason5f39d392007-10-15 16:14:19 -04003522
Chris Mason39279cc2007-06-12 06:35:45 -04003523 key.objectid = inode->i_ino;
Chris Mason39279cc2007-06-12 06:35:45 -04003524 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
3525 key.offset = 0;
3526
Chris Masone02119d2008-09-05 16:13:11 -04003527 ret = btrfs_insert_dir_item(trans, root, name, name_len,
3528 parent_inode->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04003529 &key, btrfs_inode_type(inode),
Chris Mason00e4e6b2008-08-05 11:18:09 -04003530 index);
Chris Mason39279cc2007-06-12 06:35:45 -04003531 if (ret == 0) {
Chris Mason9c583092008-01-29 15:15:18 -05003532 if (add_backref) {
3533 ret = btrfs_insert_inode_ref(trans, root,
Chris Masone02119d2008-09-05 16:13:11 -04003534 name, name_len,
3535 inode->i_ino,
3536 parent_inode->i_ino,
3537 index);
Chris Mason9c583092008-01-29 15:15:18 -05003538 }
Chris Masondbe674a2008-07-17 12:54:05 -04003539 btrfs_i_size_write(parent_inode, parent_inode->i_size +
Chris Masone02119d2008-09-05 16:13:11 -04003540 name_len * 2);
Chris Mason79c44582007-06-25 10:09:33 -04003541 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Masone02119d2008-09-05 16:13:11 -04003542 ret = btrfs_update_inode(trans, root, parent_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003543 }
3544 return ret;
3545}
3546
3547static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05003548 struct dentry *dentry, struct inode *inode,
Chris Mason00e4e6b2008-08-05 11:18:09 -04003549 int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04003550{
Chris Masone02119d2008-09-05 16:13:11 -04003551 int err = btrfs_add_link(trans, dentry->d_parent->d_inode,
3552 inode, dentry->d_name.name,
3553 dentry->d_name.len, backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04003554 if (!err) {
3555 d_instantiate(dentry, inode);
3556 return 0;
3557 }
3558 if (err > 0)
3559 err = -EEXIST;
3560 return err;
3561}
3562
Josef Bacik618e21d2007-07-11 10:18:17 -04003563static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
3564 int mode, dev_t rdev)
3565{
3566 struct btrfs_trans_handle *trans;
3567 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05003568 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04003569 int err;
3570 int drop_inode = 0;
3571 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05003572 unsigned long nr = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04003573 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04003574
3575 if (!new_valid_dev(rdev))
3576 return -EINVAL;
3577
Chris Mason1832a6d2007-12-21 16:27:21 -05003578 err = btrfs_check_free_space(root, 1, 0);
3579 if (err)
3580 goto fail;
3581
Josef Bacik618e21d2007-07-11 10:18:17 -04003582 trans = btrfs_start_transaction(root, 1);
3583 btrfs_set_trans_block_group(trans, dir);
3584
3585 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3586 if (err) {
3587 err = -ENOSPC;
3588 goto out_unlock;
3589 }
3590
Josef Bacikaec74772008-07-24 12:12:38 -04003591 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05003592 dentry->d_name.len,
3593 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04003594 BTRFS_I(dir)->block_group, mode, &index);
Josef Bacik618e21d2007-07-11 10:18:17 -04003595 err = PTR_ERR(inode);
3596 if (IS_ERR(inode))
3597 goto out_unlock;
3598
Josef Bacik33268ea2008-07-24 12:16:36 -04003599 err = btrfs_init_acl(inode, dir);
3600 if (err) {
3601 drop_inode = 1;
3602 goto out_unlock;
3603 }
3604
Josef Bacik618e21d2007-07-11 10:18:17 -04003605 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04003606 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04003607 if (err)
3608 drop_inode = 1;
3609 else {
3610 inode->i_op = &btrfs_special_inode_operations;
3611 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04003612 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04003613 }
3614 dir->i_sb->s_dirt = 1;
3615 btrfs_update_inode_block_group(trans, inode);
3616 btrfs_update_inode_block_group(trans, dir);
3617out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003618 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04003619 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003620fail:
Josef Bacik618e21d2007-07-11 10:18:17 -04003621 if (drop_inode) {
3622 inode_dec_link_count(inode);
3623 iput(inode);
3624 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003625 btrfs_btree_balance_dirty(root, nr);
Josef Bacik618e21d2007-07-11 10:18:17 -04003626 return err;
3627}
3628
Chris Mason39279cc2007-06-12 06:35:45 -04003629static int btrfs_create(struct inode *dir, struct dentry *dentry,
3630 int mode, struct nameidata *nd)
3631{
3632 struct btrfs_trans_handle *trans;
3633 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05003634 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04003635 int err;
3636 int drop_inode = 0;
Chris Mason1832a6d2007-12-21 16:27:21 -05003637 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003638 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04003639 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003640
Chris Mason1832a6d2007-12-21 16:27:21 -05003641 err = btrfs_check_free_space(root, 1, 0);
3642 if (err)
3643 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04003644 trans = btrfs_start_transaction(root, 1);
3645 btrfs_set_trans_block_group(trans, dir);
3646
3647 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3648 if (err) {
3649 err = -ENOSPC;
3650 goto out_unlock;
3651 }
3652
Josef Bacikaec74772008-07-24 12:12:38 -04003653 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05003654 dentry->d_name.len,
3655 dentry->d_parent->d_inode->i_ino,
Chris Mason00e4e6b2008-08-05 11:18:09 -04003656 objectid, BTRFS_I(dir)->block_group, mode,
3657 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04003658 err = PTR_ERR(inode);
3659 if (IS_ERR(inode))
3660 goto out_unlock;
3661
Josef Bacik33268ea2008-07-24 12:16:36 -04003662 err = btrfs_init_acl(inode, dir);
3663 if (err) {
3664 drop_inode = 1;
3665 goto out_unlock;
3666 }
3667
Chris Mason39279cc2007-06-12 06:35:45 -04003668 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04003669 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04003670 if (err)
3671 drop_inode = 1;
3672 else {
3673 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003674 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003675 inode->i_fop = &btrfs_file_operations;
3676 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05003677 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003678 }
3679 dir->i_sb->s_dirt = 1;
3680 btrfs_update_inode_block_group(trans, inode);
3681 btrfs_update_inode_block_group(trans, dir);
3682out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003683 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04003684 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003685fail:
Chris Mason39279cc2007-06-12 06:35:45 -04003686 if (drop_inode) {
3687 inode_dec_link_count(inode);
3688 iput(inode);
3689 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003690 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003691 return err;
3692}
3693
3694static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
3695 struct dentry *dentry)
3696{
3697 struct btrfs_trans_handle *trans;
3698 struct btrfs_root *root = BTRFS_I(dir)->root;
3699 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04003700 u64 index;
Chris Mason1832a6d2007-12-21 16:27:21 -05003701 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003702 int err;
3703 int drop_inode = 0;
3704
3705 if (inode->i_nlink == 0)
3706 return -ENOENT;
3707
Chris Masone02119d2008-09-05 16:13:11 -04003708 btrfs_inc_nlink(inode);
Chris Mason1832a6d2007-12-21 16:27:21 -05003709 err = btrfs_check_free_space(root, 1, 0);
3710 if (err)
3711 goto fail;
Chris Mason3de45862008-11-17 21:02:50 -05003712 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003713 if (err)
3714 goto fail;
3715
Chris Mason39279cc2007-06-12 06:35:45 -04003716 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003717
Chris Mason39279cc2007-06-12 06:35:45 -04003718 btrfs_set_trans_block_group(trans, dir);
3719 atomic_inc(&inode->i_count);
Josef Bacikaec74772008-07-24 12:12:38 -04003720
Chris Mason00e4e6b2008-08-05 11:18:09 -04003721 err = btrfs_add_nondir(trans, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04003722
Chris Mason39279cc2007-06-12 06:35:45 -04003723 if (err)
3724 drop_inode = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04003725
Chris Mason39279cc2007-06-12 06:35:45 -04003726 dir->i_sb->s_dirt = 1;
3727 btrfs_update_inode_block_group(trans, dir);
Chris Mason54aa1f42007-06-22 14:16:25 -04003728 err = btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003729
Chris Mason54aa1f42007-06-22 14:16:25 -04003730 if (err)
3731 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003732
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003733 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04003734 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003735fail:
Chris Mason39279cc2007-06-12 06:35:45 -04003736 if (drop_inode) {
3737 inode_dec_link_count(inode);
3738 iput(inode);
3739 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003740 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003741 return err;
3742}
3743
Chris Mason39279cc2007-06-12 06:35:45 -04003744static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
3745{
Chris Masonb9d86662008-05-02 16:13:49 -04003746 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04003747 struct btrfs_trans_handle *trans;
3748 struct btrfs_root *root = BTRFS_I(dir)->root;
3749 int err = 0;
3750 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04003751 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04003752 u64 index = 0;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003753 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003754
Chris Mason1832a6d2007-12-21 16:27:21 -05003755 err = btrfs_check_free_space(root, 1, 0);
3756 if (err)
3757 goto out_unlock;
3758
Chris Mason39279cc2007-06-12 06:35:45 -04003759 trans = btrfs_start_transaction(root, 1);
3760 btrfs_set_trans_block_group(trans, dir);
Chris Mason5f39d392007-10-15 16:14:19 -04003761
Chris Mason39279cc2007-06-12 06:35:45 -04003762 if (IS_ERR(trans)) {
3763 err = PTR_ERR(trans);
3764 goto out_unlock;
3765 }
3766
3767 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3768 if (err) {
3769 err = -ENOSPC;
3770 goto out_unlock;
3771 }
3772
Josef Bacikaec74772008-07-24 12:12:38 -04003773 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05003774 dentry->d_name.len,
3775 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04003776 BTRFS_I(dir)->block_group, S_IFDIR | mode,
3777 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04003778 if (IS_ERR(inode)) {
3779 err = PTR_ERR(inode);
3780 goto out_fail;
3781 }
Chris Mason5f39d392007-10-15 16:14:19 -04003782
Chris Mason39279cc2007-06-12 06:35:45 -04003783 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04003784
3785 err = btrfs_init_acl(inode, dir);
3786 if (err)
3787 goto out_fail;
3788
Chris Mason39279cc2007-06-12 06:35:45 -04003789 inode->i_op = &btrfs_dir_inode_operations;
3790 inode->i_fop = &btrfs_dir_file_operations;
3791 btrfs_set_trans_block_group(trans, inode);
3792
Chris Masondbe674a2008-07-17 12:54:05 -04003793 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003794 err = btrfs_update_inode(trans, root, inode);
3795 if (err)
3796 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04003797
Chris Masone02119d2008-09-05 16:13:11 -04003798 err = btrfs_add_link(trans, dentry->d_parent->d_inode,
3799 inode, dentry->d_name.name,
3800 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04003801 if (err)
3802 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04003803
Chris Mason39279cc2007-06-12 06:35:45 -04003804 d_instantiate(dentry, inode);
3805 drop_on_err = 0;
3806 dir->i_sb->s_dirt = 1;
3807 btrfs_update_inode_block_group(trans, inode);
3808 btrfs_update_inode_block_group(trans, dir);
3809
3810out_fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003811 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04003812 btrfs_end_transaction_throttle(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04003813
Chris Mason39279cc2007-06-12 06:35:45 -04003814out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04003815 if (drop_on_err)
3816 iput(inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003817 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003818 return err;
3819}
3820
Chris Masond352ac62008-09-29 15:18:18 -04003821/* helper for btfs_get_extent. Given an existing extent in the tree,
3822 * and an extent that you want to insert, deal with overlap and insert
3823 * the new extent into the tree.
3824 */
Chris Mason3b951512008-04-17 11:29:12 -04003825static int merge_extent_mapping(struct extent_map_tree *em_tree,
3826 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003827 struct extent_map *em,
3828 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04003829{
3830 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04003831
Chris Masone6dcd2d2008-07-17 12:53:50 -04003832 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
3833 start_diff = map_start - em->start;
3834 em->start = map_start;
3835 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04003836 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
3837 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04003838 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04003839 em->block_len -= start_diff;
3840 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003841 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04003842}
3843
Chris Masonc8b97812008-10-29 14:49:59 -04003844static noinline int uncompress_inline(struct btrfs_path *path,
3845 struct inode *inode, struct page *page,
3846 size_t pg_offset, u64 extent_offset,
3847 struct btrfs_file_extent_item *item)
3848{
3849 int ret;
3850 struct extent_buffer *leaf = path->nodes[0];
3851 char *tmp;
3852 size_t max_size;
3853 unsigned long inline_size;
3854 unsigned long ptr;
3855
3856 WARN_ON(pg_offset != 0);
3857 max_size = btrfs_file_extent_ram_bytes(leaf, item);
3858 inline_size = btrfs_file_extent_inline_item_len(leaf,
3859 btrfs_item_nr(leaf, path->slots[0]));
3860 tmp = kmalloc(inline_size, GFP_NOFS);
3861 ptr = btrfs_file_extent_inline_start(item);
3862
3863 read_extent_buffer(leaf, tmp, ptr, inline_size);
3864
Chris Mason5b050f02008-11-11 09:34:41 -05003865 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04003866 ret = btrfs_zlib_decompress(tmp, page, extent_offset,
3867 inline_size, max_size);
3868 if (ret) {
3869 char *kaddr = kmap_atomic(page, KM_USER0);
3870 unsigned long copy_size = min_t(u64,
3871 PAGE_CACHE_SIZE - pg_offset,
3872 max_size - extent_offset);
3873 memset(kaddr + pg_offset, 0, copy_size);
3874 kunmap_atomic(kaddr, KM_USER0);
3875 }
3876 kfree(tmp);
3877 return 0;
3878}
3879
Chris Masond352ac62008-09-29 15:18:18 -04003880/*
3881 * a bit scary, this does extent mapping from logical file offset to the disk.
3882 * the ugly parts come from merging extents from the disk with the
3883 * in-ram representation. This gets more complex because of the data=ordered code,
3884 * where the in-ram extents might be locked pending data=ordered completion.
3885 *
3886 * This also copies inline extents directly into the page.
3887 */
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 {
4084 printk("unkknown found_type %d\n", found_type);
4085 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) {
4096 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04004097 err = -EIO;
4098 goto out;
4099 }
Chris Masond1310b22008-01-24 16:13:08 -05004100
4101 err = 0;
4102 spin_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04004103 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04004104 /* it is possible that someone inserted the extent into the tree
4105 * while we had the lock dropped. It is also possible that
4106 * an overlapping map exists in the tree
4107 */
Chris Masona52d9a82007-08-27 16:49:44 -04004108 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04004109 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004110
4111 ret = 0;
4112
Chris Mason3b951512008-04-17 11:29:12 -04004113 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04004114 if (existing && (existing->start > start ||
4115 existing->start + existing->len <= start)) {
4116 free_extent_map(existing);
4117 existing = NULL;
4118 }
Chris Mason3b951512008-04-17 11:29:12 -04004119 if (!existing) {
4120 existing = lookup_extent_mapping(em_tree, em->start,
4121 em->len);
4122 if (existing) {
4123 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04004124 em, start,
4125 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04004126 free_extent_map(existing);
4127 if (err) {
4128 free_extent_map(em);
4129 em = NULL;
4130 }
4131 } else {
4132 err = -EIO;
4133 printk("failing to insert %Lu %Lu\n",
4134 start, len);
4135 free_extent_map(em);
4136 em = NULL;
4137 }
4138 } else {
4139 free_extent_map(em);
4140 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004141 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04004142 }
Chris Masona52d9a82007-08-27 16:49:44 -04004143 }
Chris Masond1310b22008-01-24 16:13:08 -05004144 spin_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04004145out:
Chris Masonf4219502008-07-22 11:18:09 -04004146 if (path)
4147 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04004148 if (trans) {
4149 ret = btrfs_end_transaction(trans, root);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004150 if (!err) {
Chris Masona52d9a82007-08-27 16:49:44 -04004151 err = ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004152 }
Chris Masona52d9a82007-08-27 16:49:44 -04004153 }
Chris Masona52d9a82007-08-27 16:49:44 -04004154 if (err) {
4155 free_extent_map(em);
4156 WARN_ON(1);
4157 return ERR_PTR(err);
4158 }
4159 return em;
4160}
4161
Chris Mason16432982008-04-10 10:23:21 -04004162static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
4163 const struct iovec *iov, loff_t offset,
4164 unsigned long nr_segs)
4165{
Chris Masone1c4b742008-04-22 13:26:46 -04004166 return -EINVAL;
Chris Mason16432982008-04-10 10:23:21 -04004167}
4168
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04004169static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
Chris Mason39279cc2007-06-12 06:35:45 -04004170{
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04004171 return extent_bmap(mapping, iblock, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04004172}
4173
Chris Mason9ebefb182007-06-15 13:50:00 -04004174int btrfs_readpage(struct file *file, struct page *page)
4175{
Chris Masond1310b22008-01-24 16:13:08 -05004176 struct extent_io_tree *tree;
4177 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04004178 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04004179}
Chris Mason1832a6d2007-12-21 16:27:21 -05004180
Chris Mason39279cc2007-06-12 06:35:45 -04004181static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
4182{
Chris Masond1310b22008-01-24 16:13:08 -05004183 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04004184
4185
4186 if (current->flags & PF_MEMALLOC) {
4187 redirty_page_for_writepage(wbc, page);
4188 unlock_page(page);
4189 return 0;
4190 }
Chris Masond1310b22008-01-24 16:13:08 -05004191 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04004192 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
4193}
Chris Mason39279cc2007-06-12 06:35:45 -04004194
Chris Masonf4219502008-07-22 11:18:09 -04004195int btrfs_writepages(struct address_space *mapping,
4196 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04004197{
Chris Masond1310b22008-01-24 16:13:08 -05004198 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05004199
Chris Masond1310b22008-01-24 16:13:08 -05004200 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04004201 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
4202}
4203
Chris Mason3ab2fb52007-11-08 10:59:22 -05004204static int
4205btrfs_readpages(struct file *file, struct address_space *mapping,
4206 struct list_head *pages, unsigned nr_pages)
4207{
Chris Masond1310b22008-01-24 16:13:08 -05004208 struct extent_io_tree *tree;
4209 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05004210 return extent_readpages(tree, mapping, pages, nr_pages,
4211 btrfs_get_extent);
4212}
Chris Masone6dcd2d2008-07-17 12:53:50 -04004213static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04004214{
Chris Masond1310b22008-01-24 16:13:08 -05004215 struct extent_io_tree *tree;
4216 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04004217 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004218
Chris Masond1310b22008-01-24 16:13:08 -05004219 tree = &BTRFS_I(page->mapping->host)->io_tree;
4220 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05004221 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04004222 if (ret == 1) {
4223 ClearPagePrivate(page);
4224 set_page_private(page, 0);
4225 page_cache_release(page);
4226 }
4227 return ret;
4228}
Chris Mason39279cc2007-06-12 06:35:45 -04004229
Chris Masone6dcd2d2008-07-17 12:53:50 -04004230static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
4231{
Chris Mason98509cf2008-09-11 15:51:43 -04004232 if (PageWriteback(page) || PageDirty(page))
4233 return 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004234 return __btrfs_releasepage(page, gfp_flags);
4235}
4236
Chris Masona52d9a82007-08-27 16:49:44 -04004237static void btrfs_invalidatepage(struct page *page, unsigned long offset)
4238{
Chris Masond1310b22008-01-24 16:13:08 -05004239 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004240 struct btrfs_ordered_extent *ordered;
4241 u64 page_start = page_offset(page);
4242 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04004243
Chris Masone6dcd2d2008-07-17 12:53:50 -04004244 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05004245 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004246 if (offset) {
4247 btrfs_releasepage(page, GFP_NOFS);
4248 return;
4249 }
4250
4251 lock_extent(tree, page_start, page_end, GFP_NOFS);
4252 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
4253 page_offset(page));
4254 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04004255 /*
4256 * IO on this page will never be started, so we need
4257 * to account for any ordered extents now
4258 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04004259 clear_extent_bit(tree, page_start, page_end,
4260 EXTENT_DIRTY | EXTENT_DELALLOC |
4261 EXTENT_LOCKED, 1, 0, GFP_NOFS);
Chris Mason211f90e2008-07-18 11:56:15 -04004262 btrfs_finish_ordered_io(page->mapping->host,
4263 page_start, page_end);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004264 btrfs_put_ordered_extent(ordered);
4265 lock_extent(tree, page_start, page_end, GFP_NOFS);
4266 }
4267 clear_extent_bit(tree, page_start, page_end,
4268 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
4269 EXTENT_ORDERED,
4270 1, 1, GFP_NOFS);
4271 __btrfs_releasepage(page, GFP_NOFS);
4272
Chris Mason4a096752008-07-21 10:29:44 -04004273 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04004274 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04004275 ClearPagePrivate(page);
4276 set_page_private(page, 0);
4277 page_cache_release(page);
4278 }
Chris Mason39279cc2007-06-12 06:35:45 -04004279}
4280
Chris Mason9ebefb182007-06-15 13:50:00 -04004281/*
4282 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
4283 * called from a page fault handler when a page is first dirtied. Hence we must
4284 * be careful to check for EOF conditions here. We set the page up correctly
4285 * for a written page which means we get ENOSPC checking when writing into
4286 * holes and correct delalloc and unwritten extent mapping on filesystems that
4287 * support these features.
4288 *
4289 * We are not allowed to take the i_mutex here so we have to play games to
4290 * protect against truncate races as the page could now be beyond EOF. Because
4291 * vmtruncate() writes the inode size before removing pages, once we have the
4292 * page lock we can determine safely if the page is beyond EOF. If it is not
4293 * beyond EOF, then the page is guaranteed safe against truncation until we
4294 * unlock the page.
4295 */
4296int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
4297{
Chris Mason6da6aba2007-12-18 16:15:09 -05004298 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05004299 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004300 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4301 struct btrfs_ordered_extent *ordered;
4302 char *kaddr;
4303 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04004304 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05004305 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04004306 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004307 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04004308
Chris Mason1832a6d2007-12-21 16:27:21 -05004309 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
Chris Mason1832a6d2007-12-21 16:27:21 -05004310 if (ret)
4311 goto out;
4312
4313 ret = -EINVAL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004314again:
Chris Mason9ebefb182007-06-15 13:50:00 -04004315 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04004316 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004317 page_start = page_offset(page);
4318 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04004319
Chris Mason9ebefb182007-06-15 13:50:00 -04004320 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04004321 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04004322 /* page got truncated out from underneath us */
4323 goto out_unlock;
4324 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004325 wait_on_page_writeback(page);
4326
4327 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
4328 set_page_extent_mapped(page);
4329
Chris Masoneb84ae02008-07-17 13:53:27 -04004330 /*
4331 * we can't set the delalloc bits if there are pending ordered
4332 * extents. Drop our locks and wait for them to finish
4333 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04004334 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4335 if (ordered) {
4336 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
4337 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004338 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004339 btrfs_put_ordered_extent(ordered);
4340 goto again;
4341 }
4342
Chris Masonea8c2812008-08-04 23:17:27 -04004343 btrfs_set_extent_delalloc(inode, page_start, page_end);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004344 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04004345
4346 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04004347 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04004348 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04004349 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04004350 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04004351
Chris Masone6dcd2d2008-07-17 12:53:50 -04004352 if (zero_start != PAGE_CACHE_SIZE) {
4353 kaddr = kmap(page);
4354 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
4355 flush_dcache_page(page);
4356 kunmap(page);
4357 }
Chris Mason247e7432008-07-17 12:53:51 -04004358 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004359 set_page_dirty(page);
4360 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04004361
4362out_unlock:
4363 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05004364out:
Chris Mason9ebefb182007-06-15 13:50:00 -04004365 return ret;
4366}
4367
Chris Mason39279cc2007-06-12 06:35:45 -04004368static void btrfs_truncate(struct inode *inode)
4369{
4370 struct btrfs_root *root = BTRFS_I(inode)->root;
4371 int ret;
4372 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004373 unsigned long nr;
Chris Masondbe674a2008-07-17 12:54:05 -04004374 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04004375
4376 if (!S_ISREG(inode->i_mode))
4377 return;
4378 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
4379 return;
4380
4381 btrfs_truncate_page(inode->i_mapping, inode->i_size);
Chris Mason4a096752008-07-21 10:29:44 -04004382 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04004383
Chris Mason39279cc2007-06-12 06:35:45 -04004384 trans = btrfs_start_transaction(root, 1);
4385 btrfs_set_trans_block_group(trans, inode);
Chris Masondbe674a2008-07-17 12:54:05 -04004386 btrfs_i_size_write(inode, inode->i_size);
Chris Mason39279cc2007-06-12 06:35:45 -04004387
Josef Bacik7b128762008-07-24 12:17:14 -04004388 ret = btrfs_orphan_add(trans, inode);
4389 if (ret)
4390 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04004391 /* FIXME, add redo link to tree so we don't leak on crash */
Chris Masone02119d2008-09-05 16:13:11 -04004392 ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size,
Chris Mason85e21ba2008-01-29 15:11:36 -05004393 BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004394 btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004395
Josef Bacik7b128762008-07-24 12:17:14 -04004396 ret = btrfs_orphan_del(trans, inode);
4397 BUG_ON(ret);
4398
4399out:
4400 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04004401 ret = btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004402 BUG_ON(ret);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004403 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004404}
4405
Sven Wegener3b963622008-06-09 21:57:42 -04004406/*
Chris Masond352ac62008-09-29 15:18:18 -04004407 * create a new subvolume directory/inode (helper for the ioctl).
4408 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05004409int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
4410 struct btrfs_root *new_root, struct dentry *dentry,
4411 u64 new_dirid, u64 alloc_hint)
Chris Mason39279cc2007-06-12 06:35:45 -04004412{
Chris Mason39279cc2007-06-12 06:35:45 -04004413 struct inode *inode;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04004414 int error;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004415 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004416
Josef Bacikaec74772008-07-24 12:12:38 -04004417 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
Yan Zhengd2fb3432008-12-11 16:30:39 -05004418 new_dirid, alloc_hint, S_IFDIR | 0700, &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04004419 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004420 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004421 inode->i_op = &btrfs_dir_inode_operations;
4422 inode->i_fop = &btrfs_dir_file_operations;
4423
Chris Mason39279cc2007-06-12 06:35:45 -04004424 inode->i_nlink = 1;
Chris Masondbe674a2008-07-17 12:54:05 -04004425 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04004426
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04004427 error = btrfs_update_inode(trans, new_root, inode);
4428 if (error)
4429 return error;
4430
4431 d_instantiate(dentry, inode);
4432 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004433}
4434
Chris Masond352ac62008-09-29 15:18:18 -04004435/* helper function for file defrag and space balancing. This
4436 * forces readahead on a given range of bytes in an inode
4437 */
Chris Masonedbd8d42007-12-21 16:27:24 -05004438unsigned long btrfs_force_ra(struct address_space *mapping,
Chris Mason86479a02007-09-10 19:58:16 -04004439 struct file_ra_state *ra, struct file *file,
4440 pgoff_t offset, pgoff_t last_index)
4441{
Chris Mason8e7bf942008-04-28 09:02:36 -04004442 pgoff_t req_size = last_index - offset + 1;
Chris Mason86479a02007-09-10 19:58:16 -04004443
Chris Mason86479a02007-09-10 19:58:16 -04004444 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
4445 return offset + req_size;
Chris Mason86479a02007-09-10 19:58:16 -04004446}
4447
Chris Mason39279cc2007-06-12 06:35:45 -04004448struct inode *btrfs_alloc_inode(struct super_block *sb)
4449{
4450 struct btrfs_inode *ei;
4451
4452 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
4453 if (!ei)
4454 return NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04004455 ei->last_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004456 ei->logged_trans = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004457 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Josef Bacik33268ea2008-07-24 12:16:36 -04004458 ei->i_acl = BTRFS_ACL_NOT_CACHED;
4459 ei->i_default_acl = BTRFS_ACL_NOT_CACHED;
Josef Bacik7b128762008-07-24 12:17:14 -04004460 INIT_LIST_HEAD(&ei->i_orphan);
Chris Mason39279cc2007-06-12 06:35:45 -04004461 return &ei->vfs_inode;
4462}
4463
4464void btrfs_destroy_inode(struct inode *inode)
4465{
Chris Masone6dcd2d2008-07-17 12:53:50 -04004466 struct btrfs_ordered_extent *ordered;
Chris Mason39279cc2007-06-12 06:35:45 -04004467 WARN_ON(!list_empty(&inode->i_dentry));
4468 WARN_ON(inode->i_data.nrpages);
4469
Josef Bacik33268ea2008-07-24 12:16:36 -04004470 if (BTRFS_I(inode)->i_acl &&
4471 BTRFS_I(inode)->i_acl != BTRFS_ACL_NOT_CACHED)
4472 posix_acl_release(BTRFS_I(inode)->i_acl);
4473 if (BTRFS_I(inode)->i_default_acl &&
4474 BTRFS_I(inode)->i_default_acl != BTRFS_ACL_NOT_CACHED)
4475 posix_acl_release(BTRFS_I(inode)->i_default_acl);
4476
Yanbcc63ab2008-07-30 16:29:20 -04004477 spin_lock(&BTRFS_I(inode)->root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04004478 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
4479 printk(KERN_ERR "BTRFS: inode %lu: inode still on the orphan"
4480 " list\n", inode->i_ino);
4481 dump_stack();
4482 }
Yanbcc63ab2008-07-30 16:29:20 -04004483 spin_unlock(&BTRFS_I(inode)->root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04004484
Chris Masone6dcd2d2008-07-17 12:53:50 -04004485 while(1) {
4486 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
4487 if (!ordered)
4488 break;
4489 else {
4490 printk("found ordered extent %Lu %Lu\n",
4491 ordered->file_offset, ordered->len);
4492 btrfs_remove_ordered_extent(inode, ordered);
4493 btrfs_put_ordered_extent(ordered);
4494 btrfs_put_ordered_extent(ordered);
4495 }
4496 }
Zheng Yan5b21f2e2008-09-26 10:05:38 -04004497 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004498 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
4499}
4500
Sven Wegener0ee0fda2008-07-30 16:54:26 -04004501static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04004502{
4503 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
4504
4505 inode_init_once(&ei->vfs_inode);
4506}
4507
4508void btrfs_destroy_cachep(void)
4509{
4510 if (btrfs_inode_cachep)
4511 kmem_cache_destroy(btrfs_inode_cachep);
4512 if (btrfs_trans_handle_cachep)
4513 kmem_cache_destroy(btrfs_trans_handle_cachep);
4514 if (btrfs_transaction_cachep)
4515 kmem_cache_destroy(btrfs_transaction_cachep);
4516 if (btrfs_bit_radix_cachep)
4517 kmem_cache_destroy(btrfs_bit_radix_cachep);
4518 if (btrfs_path_cachep)
4519 kmem_cache_destroy(btrfs_path_cachep);
4520}
4521
Chris Mason86479a02007-09-10 19:58:16 -04004522struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
Chris Mason92fee662007-07-25 12:31:35 -04004523 unsigned long extra_flags,
Chris Mason2b1f55b2008-09-24 11:48:04 -04004524 void (*ctor)(void *))
Chris Mason92fee662007-07-25 12:31:35 -04004525{
4526 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
Chris Mason2b1f55b2008-09-24 11:48:04 -04004527 SLAB_MEM_SPREAD | extra_flags), ctor);
Chris Mason92fee662007-07-25 12:31:35 -04004528}
4529
Chris Mason39279cc2007-06-12 06:35:45 -04004530int btrfs_init_cachep(void)
4531{
Chris Mason86479a02007-09-10 19:58:16 -04004532 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
Chris Mason92fee662007-07-25 12:31:35 -04004533 sizeof(struct btrfs_inode),
4534 0, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04004535 if (!btrfs_inode_cachep)
4536 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04004537 btrfs_trans_handle_cachep =
4538 btrfs_cache_create("btrfs_trans_handle_cache",
4539 sizeof(struct btrfs_trans_handle),
4540 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004541 if (!btrfs_trans_handle_cachep)
4542 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04004543 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
Chris Mason39279cc2007-06-12 06:35:45 -04004544 sizeof(struct btrfs_transaction),
Chris Mason92fee662007-07-25 12:31:35 -04004545 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004546 if (!btrfs_transaction_cachep)
4547 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04004548 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
Yan23223582007-09-17 11:08:52 -04004549 sizeof(struct btrfs_path),
Chris Mason92fee662007-07-25 12:31:35 -04004550 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004551 if (!btrfs_path_cachep)
4552 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04004553 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
Chris Mason92fee662007-07-25 12:31:35 -04004554 SLAB_DESTROY_BY_RCU, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004555 if (!btrfs_bit_radix_cachep)
4556 goto fail;
4557 return 0;
4558fail:
4559 btrfs_destroy_cachep();
4560 return -ENOMEM;
4561}
4562
4563static int btrfs_getattr(struct vfsmount *mnt,
4564 struct dentry *dentry, struct kstat *stat)
4565{
4566 struct inode *inode = dentry->d_inode;
4567 generic_fillattr(inode, stat);
Chris Mason3394e162008-11-17 20:42:26 -05004568 stat->dev = BTRFS_I(inode)->root->anon_super.s_dev;
Chris Masond6667462008-01-03 14:51:00 -05004569 stat->blksize = PAGE_CACHE_SIZE;
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004570 stat->blocks = (inode_get_bytes(inode) +
4571 BTRFS_I(inode)->delalloc_bytes) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04004572 return 0;
4573}
4574
4575static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
4576 struct inode * new_dir,struct dentry *new_dentry)
4577{
4578 struct btrfs_trans_handle *trans;
4579 struct btrfs_root *root = BTRFS_I(old_dir)->root;
4580 struct inode *new_inode = new_dentry->d_inode;
4581 struct inode *old_inode = old_dentry->d_inode;
4582 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004583 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004584 int ret;
4585
Chris Mason3394e162008-11-17 20:42:26 -05004586 /* we're not allowed to rename between subvolumes */
4587 if (BTRFS_I(old_inode)->root->root_key.objectid !=
4588 BTRFS_I(new_dir)->root->root_key.objectid)
4589 return -EXDEV;
4590
Chris Mason39279cc2007-06-12 06:35:45 -04004591 if (S_ISDIR(old_inode->i_mode) && new_inode &&
4592 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
4593 return -ENOTEMPTY;
4594 }
Chris Mason5f39d392007-10-15 16:14:19 -04004595
Chris Mason0660b5a2008-11-17 20:37:39 -05004596 /* to rename a snapshot or subvolume, we need to juggle the
4597 * backrefs. This isn't coded yet
4598 */
4599 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
4600 return -EXDEV;
4601
Chris Mason1832a6d2007-12-21 16:27:21 -05004602 ret = btrfs_check_free_space(root, 1, 0);
4603 if (ret)
4604 goto out_unlock;
4605
Chris Mason39279cc2007-06-12 06:35:45 -04004606 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04004607
Chris Mason39279cc2007-06-12 06:35:45 -04004608 btrfs_set_trans_block_group(trans, new_dir);
Chris Mason39279cc2007-06-12 06:35:45 -04004609
Chris Masone02119d2008-09-05 16:13:11 -04004610 btrfs_inc_nlink(old_dentry->d_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004611 old_dir->i_ctime = old_dir->i_mtime = ctime;
4612 new_dir->i_ctime = new_dir->i_mtime = ctime;
4613 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04004614
Chris Masone02119d2008-09-05 16:13:11 -04004615 ret = btrfs_unlink_inode(trans, root, old_dir, old_dentry->d_inode,
4616 old_dentry->d_name.name,
4617 old_dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04004618 if (ret)
4619 goto out_fail;
4620
4621 if (new_inode) {
4622 new_inode->i_ctime = CURRENT_TIME;
Chris Masone02119d2008-09-05 16:13:11 -04004623 ret = btrfs_unlink_inode(trans, root, new_dir,
4624 new_dentry->d_inode,
4625 new_dentry->d_name.name,
4626 new_dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04004627 if (ret)
4628 goto out_fail;
Josef Bacik7b128762008-07-24 12:17:14 -04004629 if (new_inode->i_nlink == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004630 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Josef Bacik7b128762008-07-24 12:17:14 -04004631 if (ret)
4632 goto out_fail;
4633 }
Chris Masone02119d2008-09-05 16:13:11 -04004634
Chris Mason39279cc2007-06-12 06:35:45 -04004635 }
Chris Mason3de45862008-11-17 21:02:50 -05004636 ret = btrfs_set_inode_index(new_dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004637 if (ret)
4638 goto out_fail;
4639
Chris Masone02119d2008-09-05 16:13:11 -04004640 ret = btrfs_add_link(trans, new_dentry->d_parent->d_inode,
4641 old_inode, new_dentry->d_name.name,
4642 new_dentry->d_name.len, 1, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004643 if (ret)
4644 goto out_fail;
4645
4646out_fail:
Chris Masonab78c842008-07-29 16:15:18 -04004647 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05004648out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04004649 return ret;
4650}
4651
Chris Masond352ac62008-09-29 15:18:18 -04004652/*
4653 * some fairly slow code that needs optimization. This walks the list
4654 * of all the inodes with pending delalloc and forces them to disk.
4655 */
Chris Masonea8c2812008-08-04 23:17:27 -04004656int btrfs_start_delalloc_inodes(struct btrfs_root *root)
4657{
4658 struct list_head *head = &root->fs_info->delalloc_inodes;
4659 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04004660 struct inode *inode;
Chris Masonea8c2812008-08-04 23:17:27 -04004661
Yan Zhengc146afa2008-11-12 14:34:12 -05004662 if (root->fs_info->sb->s_flags & MS_RDONLY)
4663 return -EROFS;
4664
Chris Mason75eff682008-12-15 15:54:40 -05004665 spin_lock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04004666 while(!list_empty(head)) {
4667 binode = list_entry(head->next, struct btrfs_inode,
4668 delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04004669 inode = igrab(&binode->vfs_inode);
4670 if (!inode)
4671 list_del_init(&binode->delalloc_inodes);
Chris Mason75eff682008-12-15 15:54:40 -05004672 spin_unlock(&root->fs_info->delalloc_lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04004673 if (inode) {
Chris Mason8c8bee12008-09-29 11:19:10 -04004674 filemap_flush(inode->i_mapping);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04004675 iput(inode);
4676 }
4677 cond_resched();
Chris Mason75eff682008-12-15 15:54:40 -05004678 spin_lock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04004679 }
Chris Mason75eff682008-12-15 15:54:40 -05004680 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04004681
4682 /* the filemap_flush will queue IO into the worker threads, but
4683 * we have to make sure the IO is actually started and that
4684 * ordered extents get created before we return
4685 */
4686 atomic_inc(&root->fs_info->async_submit_draining);
Chris Mason771ed682008-11-06 22:02:51 -05004687 while(atomic_read(&root->fs_info->nr_async_submits) ||
4688 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04004689 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05004690 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
4691 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04004692 }
4693 atomic_dec(&root->fs_info->async_submit_draining);
Chris Masonea8c2812008-08-04 23:17:27 -04004694 return 0;
4695}
4696
Chris Mason39279cc2007-06-12 06:35:45 -04004697static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
4698 const char *symname)
4699{
4700 struct btrfs_trans_handle *trans;
4701 struct btrfs_root *root = BTRFS_I(dir)->root;
4702 struct btrfs_path *path;
4703 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05004704 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04004705 int err;
4706 int drop_inode = 0;
4707 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004708 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04004709 int name_len;
4710 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04004711 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04004712 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04004713 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05004714 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004715
4716 name_len = strlen(symname) + 1;
4717 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
4718 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05004719
Chris Mason1832a6d2007-12-21 16:27:21 -05004720 err = btrfs_check_free_space(root, 1, 0);
4721 if (err)
4722 goto out_fail;
4723
Chris Mason39279cc2007-06-12 06:35:45 -04004724 trans = btrfs_start_transaction(root, 1);
4725 btrfs_set_trans_block_group(trans, dir);
4726
4727 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
4728 if (err) {
4729 err = -ENOSPC;
4730 goto out_unlock;
4731 }
4732
Josef Bacikaec74772008-07-24 12:12:38 -04004733 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05004734 dentry->d_name.len,
4735 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04004736 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO,
4737 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04004738 err = PTR_ERR(inode);
4739 if (IS_ERR(inode))
4740 goto out_unlock;
4741
Josef Bacik33268ea2008-07-24 12:16:36 -04004742 err = btrfs_init_acl(inode, dir);
4743 if (err) {
4744 drop_inode = 1;
4745 goto out_unlock;
4746 }
4747
Chris Mason39279cc2007-06-12 06:35:45 -04004748 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04004749 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004750 if (err)
4751 drop_inode = 1;
4752 else {
4753 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04004754 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04004755 inode->i_fop = &btrfs_file_operations;
4756 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05004757 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04004758 }
4759 dir->i_sb->s_dirt = 1;
4760 btrfs_update_inode_block_group(trans, inode);
4761 btrfs_update_inode_block_group(trans, dir);
4762 if (drop_inode)
4763 goto out_unlock;
4764
4765 path = btrfs_alloc_path();
4766 BUG_ON(!path);
4767 key.objectid = inode->i_ino;
4768 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004769 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
4770 datasize = btrfs_file_extent_calc_inline_size(name_len);
4771 err = btrfs_insert_empty_item(trans, root, path, &key,
4772 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04004773 if (err) {
4774 drop_inode = 1;
4775 goto out_unlock;
4776 }
Chris Mason5f39d392007-10-15 16:14:19 -04004777 leaf = path->nodes[0];
4778 ei = btrfs_item_ptr(leaf, path->slots[0],
4779 struct btrfs_file_extent_item);
4780 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
4781 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04004782 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04004783 btrfs_set_file_extent_encryption(leaf, ei, 0);
4784 btrfs_set_file_extent_compression(leaf, ei, 0);
4785 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
4786 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
4787
Chris Mason39279cc2007-06-12 06:35:45 -04004788 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04004789 write_extent_buffer(leaf, symname, ptr, name_len);
4790 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004791 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04004792
Chris Mason39279cc2007-06-12 06:35:45 -04004793 inode->i_op = &btrfs_symlink_inode_operations;
4794 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04004795 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04004796 inode_set_bytes(inode, name_len);
Chris Masondbe674a2008-07-17 12:54:05 -04004797 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04004798 err = btrfs_update_inode(trans, root, inode);
4799 if (err)
4800 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04004801
4802out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004803 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04004804 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05004805out_fail:
Chris Mason39279cc2007-06-12 06:35:45 -04004806 if (drop_inode) {
4807 inode_dec_link_count(inode);
4808 iput(inode);
4809 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004810 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004811 return err;
4812}
Chris Mason16432982008-04-10 10:23:21 -04004813
Yan Zhengd899e052008-10-30 14:25:28 -04004814static int prealloc_file_range(struct inode *inode, u64 start, u64 end,
4815 u64 alloc_hint, int mode)
4816{
4817 struct btrfs_trans_handle *trans;
4818 struct btrfs_root *root = BTRFS_I(inode)->root;
4819 struct btrfs_key ins;
4820 u64 alloc_size;
4821 u64 cur_offset = start;
4822 u64 num_bytes = end - start;
4823 int ret = 0;
4824
4825 trans = btrfs_join_transaction(root, 1);
4826 BUG_ON(!trans);
4827 btrfs_set_trans_block_group(trans, inode);
4828
4829 while (num_bytes > 0) {
4830 alloc_size = min(num_bytes, root->fs_info->max_extent);
4831 ret = btrfs_reserve_extent(trans, root, alloc_size,
4832 root->sectorsize, 0, alloc_hint,
4833 (u64)-1, &ins, 1);
4834 if (ret) {
4835 WARN_ON(1);
4836 goto out;
4837 }
4838 ret = insert_reserved_file_extent(trans, inode,
4839 cur_offset, ins.objectid,
4840 ins.offset, ins.offset,
4841 ins.offset, 0, 0, 0,
4842 BTRFS_FILE_EXTENT_PREALLOC);
4843 BUG_ON(ret);
4844 num_bytes -= ins.offset;
4845 cur_offset += ins.offset;
4846 alloc_hint = ins.objectid + ins.offset;
4847 }
4848out:
4849 if (cur_offset > start) {
4850 inode->i_ctime = CURRENT_TIME;
4851 btrfs_set_flag(inode, PREALLOC);
4852 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
4853 cur_offset > i_size_read(inode))
4854 btrfs_i_size_write(inode, cur_offset);
4855 ret = btrfs_update_inode(trans, root, inode);
4856 BUG_ON(ret);
4857 }
4858
4859 btrfs_end_transaction(trans, root);
4860 return ret;
4861}
4862
4863static long btrfs_fallocate(struct inode *inode, int mode,
4864 loff_t offset, loff_t len)
4865{
4866 u64 cur_offset;
4867 u64 last_byte;
4868 u64 alloc_start;
4869 u64 alloc_end;
4870 u64 alloc_hint = 0;
4871 u64 mask = BTRFS_I(inode)->root->sectorsize - 1;
4872 struct extent_map *em;
4873 int ret;
4874
4875 alloc_start = offset & ~mask;
4876 alloc_end = (offset + len + mask) & ~mask;
4877
4878 mutex_lock(&inode->i_mutex);
4879 if (alloc_start > inode->i_size) {
4880 ret = btrfs_cont_expand(inode, alloc_start);
4881 if (ret)
4882 goto out;
4883 }
4884
4885 while (1) {
4886 struct btrfs_ordered_extent *ordered;
4887 lock_extent(&BTRFS_I(inode)->io_tree, alloc_start,
4888 alloc_end - 1, GFP_NOFS);
4889 ordered = btrfs_lookup_first_ordered_extent(inode,
4890 alloc_end - 1);
4891 if (ordered &&
4892 ordered->file_offset + ordered->len > alloc_start &&
4893 ordered->file_offset < alloc_end) {
4894 btrfs_put_ordered_extent(ordered);
4895 unlock_extent(&BTRFS_I(inode)->io_tree,
4896 alloc_start, alloc_end - 1, GFP_NOFS);
4897 btrfs_wait_ordered_range(inode, alloc_start,
4898 alloc_end - alloc_start);
4899 } else {
4900 if (ordered)
4901 btrfs_put_ordered_extent(ordered);
4902 break;
4903 }
4904 }
4905
4906 cur_offset = alloc_start;
4907 while (1) {
4908 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4909 alloc_end - cur_offset, 0);
4910 BUG_ON(IS_ERR(em) || !em);
4911 last_byte = min(extent_map_end(em), alloc_end);
4912 last_byte = (last_byte + mask) & ~mask;
4913 if (em->block_start == EXTENT_MAP_HOLE) {
4914 ret = prealloc_file_range(inode, cur_offset,
4915 last_byte, alloc_hint, mode);
4916 if (ret < 0) {
4917 free_extent_map(em);
4918 break;
4919 }
4920 }
4921 if (em->block_start <= EXTENT_MAP_LAST_BYTE)
4922 alloc_hint = em->block_start;
4923 free_extent_map(em);
4924
4925 cur_offset = last_byte;
4926 if (cur_offset >= alloc_end) {
4927 ret = 0;
4928 break;
4929 }
4930 }
4931 unlock_extent(&BTRFS_I(inode)->io_tree, alloc_start, alloc_end - 1,
4932 GFP_NOFS);
4933out:
4934 mutex_unlock(&inode->i_mutex);
4935 return ret;
4936}
4937
Chris Masone6dcd2d2008-07-17 12:53:50 -04004938static int btrfs_set_page_dirty(struct page *page)
4939{
Chris Masone6dcd2d2008-07-17 12:53:50 -04004940 return __set_page_dirty_nobuffers(page);
4941}
4942
Sven Wegener0ee0fda2008-07-30 16:54:26 -04004943static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05004944{
4945 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
4946 return -EACCES;
Josef Bacik33268ea2008-07-24 12:16:36 -04004947 return generic_permission(inode, mask, btrfs_check_acl);
Yanfdebe2b2008-01-14 13:26:08 -05004948}
Chris Mason39279cc2007-06-12 06:35:45 -04004949
4950static struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05004951 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04004952 .lookup = btrfs_lookup,
4953 .create = btrfs_create,
4954 .unlink = btrfs_unlink,
4955 .link = btrfs_link,
4956 .mkdir = btrfs_mkdir,
4957 .rmdir = btrfs_rmdir,
4958 .rename = btrfs_rename,
4959 .symlink = btrfs_symlink,
4960 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04004961 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04004962 .setxattr = btrfs_setxattr,
4963 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05004964 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04004965 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05004966 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04004967};
Chris Mason39279cc2007-06-12 06:35:45 -04004968static struct inode_operations btrfs_dir_ro_inode_operations = {
4969 .lookup = btrfs_lookup,
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 file_operations btrfs_dir_file_operations = {
4973 .llseek = generic_file_llseek,
4974 .read = generic_read_dir,
David Woodhousecbdf5a22008-08-06 19:42:33 +01004975 .readdir = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04004976 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04004977#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04004978 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04004979#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04004980 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04004981 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04004982};
4983
Chris Masond1310b22008-01-24 16:13:08 -05004984static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04004985 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05004986 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04004987 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04004988 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04004989 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04004990 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Mason1259ab72008-05-12 13:39:03 -04004991 .readpage_io_failed_hook = btrfs_io_failed_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05004992 .set_bit_hook = btrfs_set_bit_hook,
4993 .clear_bit_hook = btrfs_clear_bit_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04004994};
4995
Chris Mason39279cc2007-06-12 06:35:45 -04004996static struct address_space_operations btrfs_aops = {
4997 .readpage = btrfs_readpage,
4998 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04004999 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05005000 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04005001 .sync_page = block_sync_page,
Chris Mason39279cc2007-06-12 06:35:45 -04005002 .bmap = btrfs_bmap,
Chris Mason16432982008-04-10 10:23:21 -04005003 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04005004 .invalidatepage = btrfs_invalidatepage,
5005 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005006 .set_page_dirty = btrfs_set_page_dirty,
Chris Mason39279cc2007-06-12 06:35:45 -04005007};
5008
5009static struct address_space_operations btrfs_symlink_aops = {
5010 .readpage = btrfs_readpage,
5011 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04005012 .invalidatepage = btrfs_invalidatepage,
5013 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04005014};
5015
5016static struct inode_operations btrfs_file_inode_operations = {
5017 .truncate = btrfs_truncate,
5018 .getattr = btrfs_getattr,
5019 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04005020 .setxattr = btrfs_setxattr,
5021 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05005022 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04005023 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05005024 .permission = btrfs_permission,
Yan Zhengd899e052008-10-30 14:25:28 -04005025 .fallocate = btrfs_fallocate,
Chris Mason39279cc2007-06-12 06:35:45 -04005026};
Josef Bacik618e21d2007-07-11 10:18:17 -04005027static struct inode_operations btrfs_special_inode_operations = {
5028 .getattr = btrfs_getattr,
5029 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05005030 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04005031 .setxattr = btrfs_setxattr,
5032 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04005033 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04005034 .removexattr = btrfs_removexattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04005035};
Chris Mason39279cc2007-06-12 06:35:45 -04005036static struct inode_operations btrfs_symlink_inode_operations = {
5037 .readlink = generic_readlink,
5038 .follow_link = page_follow_link_light,
5039 .put_link = page_put_link,
Yanfdebe2b2008-01-14 13:26:08 -05005040 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04005041};