blob: c0ca9c3723c0661823b4c0f6b7c018c66dcfd369 [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 */
Chris Masond3977122009-01-05 21:25:51 -0500127static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400128 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
Chris Masond3977122009-01-05 21:25:51 -0500151 path = btrfs_alloc_path();
152 if (!path)
Chris Masonc8b97812008-10-29 14:49:59 -0400153 return -ENOMEM;
154
155 btrfs_set_trans_block_group(trans, inode);
156
157 key.objectid = inode->i_ino;
158 key.offset = start;
159 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
160 inode_add_bytes(inode, size);
161 datasize = btrfs_file_extent_calc_inline_size(cur_size);
162
163 inode_add_bytes(inode, size);
164 ret = btrfs_insert_empty_item(trans, root, path, &key,
165 datasize);
166 BUG_ON(ret);
167 if (ret) {
168 err = ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400169 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;
Chris Masond3977122009-01-05 21:25:51 -0500184 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400185 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 Masonb888db2b2007-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 Masonb888db2b2007-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 }
Chris Masond3977122009-01-05 21:25:51 -0500522 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500523
524 goto out;
525}
526
527/*
528 * phase two of compressed writeback. This is the ordered portion
529 * of the code, which only gets called in the order the work was
530 * queued. We walk all the async extents created by compress_file_range
531 * and send them down to the disk.
532 */
533static noinline int submit_compressed_extents(struct inode *inode,
534 struct async_cow *async_cow)
535{
536 struct async_extent *async_extent;
537 u64 alloc_hint = 0;
538 struct btrfs_trans_handle *trans;
539 struct btrfs_key ins;
540 struct extent_map *em;
541 struct btrfs_root *root = BTRFS_I(inode)->root;
542 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
543 struct extent_io_tree *io_tree;
544 int ret;
545
546 if (list_empty(&async_cow->extents))
547 return 0;
548
549 trans = btrfs_join_transaction(root, 1);
550
Chris Masond3977122009-01-05 21:25:51 -0500551 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500552 async_extent = list_entry(async_cow->extents.next,
553 struct async_extent, list);
554 list_del(&async_extent->list);
555
556 io_tree = &BTRFS_I(inode)->io_tree;
557
558 /* did the compression code fall back to uncompressed IO? */
559 if (!async_extent->pages) {
560 int page_started = 0;
561 unsigned long nr_written = 0;
562
563 lock_extent(io_tree, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500564 async_extent->start +
565 async_extent->ram_size - 1, GFP_NOFS);
Chris Mason771ed682008-11-06 22:02:51 -0500566
567 /* allocate blocks */
568 cow_file_range(inode, async_cow->locked_page,
569 async_extent->start,
570 async_extent->start +
571 async_extent->ram_size - 1,
572 &page_started, &nr_written, 0);
573
574 /*
575 * if page_started, cow_file_range inserted an
576 * inline extent and took care of all the unlocking
577 * and IO for us. Otherwise, we need to submit
578 * all those pages down to the drive.
579 */
580 if (!page_started)
581 extent_write_locked_range(io_tree,
582 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500583 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500584 async_extent->ram_size - 1,
585 btrfs_get_extent,
586 WB_SYNC_ALL);
587 kfree(async_extent);
588 cond_resched();
589 continue;
590 }
591
592 lock_extent(io_tree, async_extent->start,
593 async_extent->start + async_extent->ram_size - 1,
594 GFP_NOFS);
595 /*
596 * here we're doing allocation and writeback of the
597 * compressed pages
598 */
599 btrfs_drop_extent_cache(inode, async_extent->start,
600 async_extent->start +
601 async_extent->ram_size - 1, 0);
602
603 ret = btrfs_reserve_extent(trans, root,
604 async_extent->compressed_size,
605 async_extent->compressed_size,
606 0, alloc_hint,
607 (u64)-1, &ins, 1);
608 BUG_ON(ret);
609 em = alloc_extent_map(GFP_NOFS);
610 em->start = async_extent->start;
611 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500612 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500613
614 em->block_start = ins.objectid;
615 em->block_len = ins.offset;
616 em->bdev = root->fs_info->fs_devices->latest_bdev;
617 set_bit(EXTENT_FLAG_PINNED, &em->flags);
618 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
619
Chris Masond3977122009-01-05 21:25:51 -0500620 while (1) {
Chris Mason771ed682008-11-06 22:02:51 -0500621 spin_lock(&em_tree->lock);
622 ret = add_extent_mapping(em_tree, em);
623 spin_unlock(&em_tree->lock);
624 if (ret != -EEXIST) {
625 free_extent_map(em);
626 break;
627 }
628 btrfs_drop_extent_cache(inode, async_extent->start,
629 async_extent->start +
630 async_extent->ram_size - 1, 0);
631 }
632
633 ret = btrfs_add_ordered_extent(inode, async_extent->start,
634 ins.objectid,
635 async_extent->ram_size,
636 ins.offset,
637 BTRFS_ORDERED_COMPRESSED);
638 BUG_ON(ret);
639
640 btrfs_end_transaction(trans, root);
641
642 /*
643 * clear dirty, set writeback and unlock the pages.
644 */
645 extent_clear_unlock_delalloc(inode,
646 &BTRFS_I(inode)->io_tree,
647 async_extent->start,
648 async_extent->start +
649 async_extent->ram_size - 1,
650 NULL, 1, 1, 0, 1, 1, 0);
651
652 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500653 async_extent->start,
654 async_extent->ram_size,
655 ins.objectid,
656 ins.offset, async_extent->pages,
657 async_extent->nr_pages);
Chris Mason771ed682008-11-06 22:02:51 -0500658
659 BUG_ON(ret);
660 trans = btrfs_join_transaction(root, 1);
661 alloc_hint = ins.objectid + ins.offset;
662 kfree(async_extent);
663 cond_resched();
664 }
665
666 btrfs_end_transaction(trans, root);
667 return 0;
668}
669
670/*
671 * when extent_io.c finds a delayed allocation range in the file,
672 * the call backs end up in this code. The basic idea is to
673 * allocate extents on disk for the range, and create ordered data structs
674 * in ram to track those extents.
675 *
676 * locked_page is the page that writepage had locked already. We use
677 * it to make sure we don't do extra locks or unlocks.
678 *
679 * *page_started is set to one if we unlock locked_page and do everything
680 * required to start IO on it. It may be clean and already done with
681 * IO when we return.
682 */
683static noinline int cow_file_range(struct inode *inode,
684 struct page *locked_page,
685 u64 start, u64 end, int *page_started,
686 unsigned long *nr_written,
687 int unlock)
688{
689 struct btrfs_root *root = BTRFS_I(inode)->root;
690 struct btrfs_trans_handle *trans;
691 u64 alloc_hint = 0;
692 u64 num_bytes;
693 unsigned long ram_size;
694 u64 disk_num_bytes;
695 u64 cur_alloc_size;
696 u64 blocksize = root->sectorsize;
697 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500698 u64 isize = i_size_read(inode);
Chris Mason771ed682008-11-06 22:02:51 -0500699 struct btrfs_key ins;
700 struct extent_map *em;
701 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
702 int ret = 0;
703
704 trans = btrfs_join_transaction(root, 1);
705 BUG_ON(!trans);
706 btrfs_set_trans_block_group(trans, inode);
707
Chris Mason42dc7ba2008-12-15 11:44:56 -0500708 actual_end = min_t(u64, isize, end + 1);
Chris Mason771ed682008-11-06 22:02:51 -0500709
710 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
711 num_bytes = max(blocksize, num_bytes);
712 disk_num_bytes = num_bytes;
713 ret = 0;
714
715 if (start == 0) {
716 /* lets try to make an inline extent */
717 ret = cow_file_range_inline(trans, root, inode,
718 start, end, 0, NULL);
719 if (ret == 0) {
720 extent_clear_unlock_delalloc(inode,
721 &BTRFS_I(inode)->io_tree,
722 start, end, NULL, 1, 1,
723 1, 1, 1, 1);
724 *nr_written = *nr_written +
725 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
726 *page_started = 1;
727 ret = 0;
728 goto out;
729 }
730 }
Chris Masonc8b97812008-10-29 14:49:59 -0400731
732 BUG_ON(disk_num_bytes >
733 btrfs_super_total_bytes(&root->fs_info->super_copy));
734
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400735 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400736
Chris Masond3977122009-01-05 21:25:51 -0500737 while (disk_num_bytes > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400738 cur_alloc_size = min(disk_num_bytes, root->fs_info->max_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400739 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500740 root->sectorsize, 0, alloc_hint,
Chris Masone6dcd2d2008-07-17 12:53:50 -0400741 (u64)-1, &ins, 1);
Chris Masond3977122009-01-05 21:25:51 -0500742 BUG_ON(ret);
743
Chris Masone6dcd2d2008-07-17 12:53:50 -0400744 em = alloc_extent_map(GFP_NOFS);
745 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500746 em->orig_start = em->start;
Chris Masonc8b97812008-10-29 14:49:59 -0400747
Chris Mason771ed682008-11-06 22:02:51 -0500748 ram_size = ins.offset;
749 em->len = ins.offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400750
Chris Masone6dcd2d2008-07-17 12:53:50 -0400751 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400752 em->block_len = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400753 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400754 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -0400755
Chris Masond3977122009-01-05 21:25:51 -0500756 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -0400757 spin_lock(&em_tree->lock);
758 ret = add_extent_mapping(em_tree, em);
759 spin_unlock(&em_tree->lock);
760 if (ret != -EEXIST) {
761 free_extent_map(em);
762 break;
763 }
764 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400765 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400766 }
767
Chris Mason98d20f62008-04-14 09:46:10 -0400768 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400769 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -0500770 ram_size, cur_alloc_size, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400771 BUG_ON(ret);
Chris Masonc8b97812008-10-29 14:49:59 -0400772
Yan Zheng17d217f2008-12-12 10:03:38 -0500773 if (root->root_key.objectid ==
774 BTRFS_DATA_RELOC_TREE_OBJECTID) {
775 ret = btrfs_reloc_clone_csums(inode, start,
776 cur_alloc_size);
777 BUG_ON(ret);
778 }
779
Chris Masond3977122009-01-05 21:25:51 -0500780 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -0400781 break;
Chris Masond3977122009-01-05 21:25:51 -0500782
Chris Masonc8b97812008-10-29 14:49:59 -0400783 /* we're not doing compressed IO, don't unlock the first
784 * page (which the caller expects to stay locked), don't
785 * clear any dirty bits and don't set any writeback bits
786 */
787 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
788 start, start + ram_size - 1,
Chris Mason771ed682008-11-06 22:02:51 -0500789 locked_page, unlock, 1,
790 1, 0, 0, 0);
Chris Masonc8b97812008-10-29 14:49:59 -0400791 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -0500792 num_bytes -= cur_alloc_size;
793 alloc_hint = ins.objectid + ins.offset;
794 start += cur_alloc_size;
Chris Masonb888db2b2007-08-27 16:49:44 -0400795 }
Chris Masonb888db2b2007-08-27 16:49:44 -0400796out:
Chris Mason771ed682008-11-06 22:02:51 -0500797 ret = 0;
Chris Masonb888db2b2007-08-27 16:49:44 -0400798 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400799
Chris Masonbe20aa92007-12-17 20:14:01 -0500800 return ret;
Chris Mason771ed682008-11-06 22:02:51 -0500801}
Chris Masonc8b97812008-10-29 14:49:59 -0400802
Chris Mason771ed682008-11-06 22:02:51 -0500803/*
804 * work queue call back to started compression on a file and pages
805 */
806static noinline void async_cow_start(struct btrfs_work *work)
807{
808 struct async_cow *async_cow;
809 int num_added = 0;
810 async_cow = container_of(work, struct async_cow, work);
811
812 compress_file_range(async_cow->inode, async_cow->locked_page,
813 async_cow->start, async_cow->end, async_cow,
814 &num_added);
815 if (num_added == 0)
816 async_cow->inode = NULL;
817}
818
819/*
820 * work queue call back to submit previously compressed pages
821 */
822static noinline void async_cow_submit(struct btrfs_work *work)
823{
824 struct async_cow *async_cow;
825 struct btrfs_root *root;
826 unsigned long nr_pages;
827
828 async_cow = container_of(work, struct async_cow, work);
829
830 root = async_cow->root;
831 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
832 PAGE_CACHE_SHIFT;
833
834 atomic_sub(nr_pages, &root->fs_info->async_delalloc_pages);
835
836 if (atomic_read(&root->fs_info->async_delalloc_pages) <
837 5 * 1042 * 1024 &&
838 waitqueue_active(&root->fs_info->async_submit_wait))
839 wake_up(&root->fs_info->async_submit_wait);
840
Chris Masond3977122009-01-05 21:25:51 -0500841 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -0500842 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -0500843}
Chris Masonc8b97812008-10-29 14:49:59 -0400844
Chris Mason771ed682008-11-06 22:02:51 -0500845static noinline void async_cow_free(struct btrfs_work *work)
846{
847 struct async_cow *async_cow;
848 async_cow = container_of(work, struct async_cow, work);
849 kfree(async_cow);
850}
851
852static int cow_file_range_async(struct inode *inode, struct page *locked_page,
853 u64 start, u64 end, int *page_started,
854 unsigned long *nr_written)
855{
856 struct async_cow *async_cow;
857 struct btrfs_root *root = BTRFS_I(inode)->root;
858 unsigned long nr_pages;
859 u64 cur_end;
860 int limit = 10 * 1024 * 1042;
861
862 if (!btrfs_test_opt(root, COMPRESS)) {
863 return cow_file_range(inode, locked_page, start, end,
864 page_started, nr_written, 1);
865 }
866
867 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED |
868 EXTENT_DELALLOC, 1, 0, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -0500869 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500870 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
871 async_cow->inode = inode;
872 async_cow->root = root;
873 async_cow->locked_page = locked_page;
874 async_cow->start = start;
875
876 if (btrfs_test_flag(inode, NOCOMPRESS))
877 cur_end = end;
878 else
879 cur_end = min(end, start + 512 * 1024 - 1);
880
881 async_cow->end = cur_end;
882 INIT_LIST_HEAD(&async_cow->extents);
883
884 async_cow->work.func = async_cow_start;
885 async_cow->work.ordered_func = async_cow_submit;
886 async_cow->work.ordered_free = async_cow_free;
887 async_cow->work.flags = 0;
888
Chris Mason771ed682008-11-06 22:02:51 -0500889 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
890 PAGE_CACHE_SHIFT;
891 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
892
893 btrfs_queue_worker(&root->fs_info->delalloc_workers,
894 &async_cow->work);
895
896 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
897 wait_event(root->fs_info->async_submit_wait,
898 (atomic_read(&root->fs_info->async_delalloc_pages) <
899 limit));
900 }
901
Chris Masond3977122009-01-05 21:25:51 -0500902 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -0500903 atomic_read(&root->fs_info->async_delalloc_pages)) {
904 wait_event(root->fs_info->async_submit_wait,
905 (atomic_read(&root->fs_info->async_delalloc_pages) ==
906 0));
907 }
908
909 *nr_written += nr_pages;
910 start = cur_end + 1;
911 }
912 *page_started = 1;
913 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -0500914}
915
Chris Masond3977122009-01-05 21:25:51 -0500916static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -0500917 u64 bytenr, u64 num_bytes)
918{
919 int ret;
920 struct btrfs_ordered_sum *sums;
921 LIST_HEAD(list);
922
923 ret = btrfs_lookup_csums_range(root, bytenr, bytenr + num_bytes - 1,
924 &list);
925 if (ret == 0 && list_empty(&list))
926 return 0;
927
928 while (!list_empty(&list)) {
929 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
930 list_del(&sums->list);
931 kfree(sums);
932 }
933 return 1;
934}
935
Chris Masond352ac62008-09-29 15:18:18 -0400936/*
937 * when nowcow writeback call back. This checks for snapshots or COW copies
938 * of the extents that exist in the file, and COWs the file as required.
939 *
940 * If no cow copies or snapshots exist, we write directly to the existing
941 * blocks on disk
942 */
Chris Masonc8b97812008-10-29 14:49:59 -0400943static int run_delalloc_nocow(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -0500944 u64 start, u64 end, int *page_started, int force,
945 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -0500946{
Chris Masonbe20aa92007-12-17 20:14:01 -0500947 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -0400948 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -0500949 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -0500950 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -0400951 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -0500952 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -0400953 u64 cow_start;
954 u64 cur_offset;
955 u64 extent_end;
956 u64 disk_bytenr;
957 u64 num_bytes;
958 int extent_type;
959 int ret;
Yan Zhengd899e052008-10-30 14:25:28 -0400960 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -0400961 int nocow;
962 int check_prev = 1;
Chris Masonbe20aa92007-12-17 20:14:01 -0500963
964 path = btrfs_alloc_path();
965 BUG_ON(!path);
Yan Zheng7ea394f2008-08-05 13:05:02 -0400966 trans = btrfs_join_transaction(root, 1);
967 BUG_ON(!trans);
Chris Masonbe20aa92007-12-17 20:14:01 -0500968
Yan Zheng80ff3852008-10-30 14:20:02 -0400969 cow_start = (u64)-1;
970 cur_offset = start;
971 while (1) {
972 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
973 cur_offset, 0);
974 BUG_ON(ret < 0);
975 if (ret > 0 && path->slots[0] > 0 && check_prev) {
976 leaf = path->nodes[0];
977 btrfs_item_key_to_cpu(leaf, &found_key,
978 path->slots[0] - 1);
979 if (found_key.objectid == inode->i_ino &&
980 found_key.type == BTRFS_EXTENT_DATA_KEY)
981 path->slots[0]--;
982 }
983 check_prev = 0;
984next_slot:
985 leaf = path->nodes[0];
986 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
987 ret = btrfs_next_leaf(root, path);
988 if (ret < 0)
989 BUG_ON(1);
990 if (ret > 0)
991 break;
992 leaf = path->nodes[0];
993 }
Chris Masonbe20aa92007-12-17 20:14:01 -0500994
Yan Zheng80ff3852008-10-30 14:20:02 -0400995 nocow = 0;
996 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -0500997 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -0400998 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -0500999
Yan Zheng80ff3852008-10-30 14:20:02 -04001000 if (found_key.objectid > inode->i_ino ||
1001 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1002 found_key.offset > end)
1003 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001004
Yan Zheng80ff3852008-10-30 14:20:02 -04001005 if (found_key.offset > cur_offset) {
1006 extent_end = found_key.offset;
1007 goto out_check;
1008 }
Chris Masonc31f8832008-01-08 15:46:31 -05001009
Yan Zheng80ff3852008-10-30 14:20:02 -04001010 fi = btrfs_item_ptr(leaf, path->slots[0],
1011 struct btrfs_file_extent_item);
1012 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001013
Yan Zhengd899e052008-10-30 14:25:28 -04001014 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1015 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001016 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1017 extent_end = found_key.offset +
1018 btrfs_file_extent_num_bytes(leaf, fi);
1019 if (extent_end <= start) {
1020 path->slots[0]++;
1021 goto next_slot;
1022 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001023 if (disk_bytenr == 0)
1024 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001025 if (btrfs_file_extent_compression(leaf, fi) ||
1026 btrfs_file_extent_encryption(leaf, fi) ||
1027 btrfs_file_extent_other_encoding(leaf, fi))
1028 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001029 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1030 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001031 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001032 goto out_check;
Yan Zheng17d217f2008-12-12 10:03:38 -05001033 if (btrfs_cross_ref_exist(trans, root, inode->i_ino,
1034 disk_bytenr))
1035 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001036 disk_bytenr += btrfs_file_extent_offset(leaf, fi);
Yan Zheng17d217f2008-12-12 10:03:38 -05001037 disk_bytenr += cur_offset - found_key.offset;
1038 num_bytes = min(end + 1, extent_end) - cur_offset;
1039 /*
1040 * force cow if csum exists in the range.
1041 * this ensure that csum for a given extent are
1042 * either valid or do not exist.
1043 */
1044 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1045 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001046 nocow = 1;
1047 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1048 extent_end = found_key.offset +
1049 btrfs_file_extent_inline_len(leaf, fi);
1050 extent_end = ALIGN(extent_end, root->sectorsize);
1051 } else {
1052 BUG_ON(1);
1053 }
1054out_check:
1055 if (extent_end <= start) {
1056 path->slots[0]++;
1057 goto next_slot;
1058 }
1059 if (!nocow) {
1060 if (cow_start == (u64)-1)
1061 cow_start = cur_offset;
1062 cur_offset = extent_end;
1063 if (cur_offset > end)
1064 break;
1065 path->slots[0]++;
1066 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001067 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001068
Yan Zheng7ea394f2008-08-05 13:05:02 -04001069 btrfs_release_path(root, path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001070 if (cow_start != (u64)-1) {
1071 ret = cow_file_range(inode, locked_page, cow_start,
Chris Mason771ed682008-11-06 22:02:51 -05001072 found_key.offset - 1, page_started,
1073 nr_written, 1);
Yan Zheng80ff3852008-10-30 14:20:02 -04001074 BUG_ON(ret);
1075 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001076 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001077
Yan Zhengd899e052008-10-30 14:25:28 -04001078 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1079 struct extent_map *em;
1080 struct extent_map_tree *em_tree;
1081 em_tree = &BTRFS_I(inode)->extent_tree;
1082 em = alloc_extent_map(GFP_NOFS);
1083 em->start = cur_offset;
Chris Mason445a6942008-11-10 11:53:33 -05001084 em->orig_start = em->start;
Yan Zhengd899e052008-10-30 14:25:28 -04001085 em->len = num_bytes;
1086 em->block_len = num_bytes;
1087 em->block_start = disk_bytenr;
1088 em->bdev = root->fs_info->fs_devices->latest_bdev;
1089 set_bit(EXTENT_FLAG_PINNED, &em->flags);
1090 while (1) {
1091 spin_lock(&em_tree->lock);
1092 ret = add_extent_mapping(em_tree, em);
1093 spin_unlock(&em_tree->lock);
1094 if (ret != -EEXIST) {
1095 free_extent_map(em);
1096 break;
1097 }
1098 btrfs_drop_extent_cache(inode, em->start,
1099 em->start + em->len - 1, 0);
1100 }
1101 type = BTRFS_ORDERED_PREALLOC;
1102 } else {
1103 type = BTRFS_ORDERED_NOCOW;
1104 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001105
1106 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001107 num_bytes, num_bytes, type);
1108 BUG_ON(ret);
Chris Mason771ed682008-11-06 22:02:51 -05001109
Yan Zhengd899e052008-10-30 14:25:28 -04001110 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
1111 cur_offset, cur_offset + num_bytes - 1,
Chris Mason771ed682008-11-06 22:02:51 -05001112 locked_page, 1, 1, 1, 0, 0, 0);
Yan Zheng80ff3852008-10-30 14:20:02 -04001113 cur_offset = extent_end;
1114 if (cur_offset > end)
1115 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001116 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001117 btrfs_release_path(root, path);
1118
1119 if (cur_offset <= end && cow_start == (u64)-1)
1120 cow_start = cur_offset;
1121 if (cow_start != (u64)-1) {
1122 ret = cow_file_range(inode, locked_page, cow_start, end,
Chris Mason771ed682008-11-06 22:02:51 -05001123 page_started, nr_written, 1);
Yan Zheng80ff3852008-10-30 14:20:02 -04001124 BUG_ON(ret);
1125 }
1126
1127 ret = btrfs_end_transaction(trans, root);
1128 BUG_ON(ret);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001129 btrfs_free_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001130 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001131}
1132
Chris Masond352ac62008-09-29 15:18:18 -04001133/*
1134 * extent_io.c call back to do delayed allocation processing
1135 */
Chris Masonc8b97812008-10-29 14:49:59 -04001136static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001137 u64 start, u64 end, int *page_started,
1138 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001139{
Chris Masonbe20aa92007-12-17 20:14:01 -05001140 int ret;
Chris Masona2135012008-06-25 16:01:30 -04001141
Yan Zheng17d217f2008-12-12 10:03:38 -05001142 if (btrfs_test_flag(inode, NODATACOW))
Chris Masonc8b97812008-10-29 14:49:59 -04001143 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001144 page_started, 1, nr_written);
Yan Zhengd899e052008-10-30 14:25:28 -04001145 else if (btrfs_test_flag(inode, PREALLOC))
1146 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001147 page_started, 0, nr_written);
Chris Masonbe20aa92007-12-17 20:14:01 -05001148 else
Chris Mason771ed682008-11-06 22:02:51 -05001149 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001150 page_started, nr_written);
Chris Mason1832a6d2007-12-21 16:27:21 -05001151
Chris Masonb888db2b2007-08-27 16:49:44 -04001152 return ret;
1153}
1154
Chris Masond352ac62008-09-29 15:18:18 -04001155/*
1156 * extent_io.c set_bit_hook, used to track delayed allocation
1157 * bytes in this file, and to maintain the list of inodes that
1158 * have pending delalloc work to be done.
1159 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001160static int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -05001161 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -05001162{
Chris Mason75eff682008-12-15 15:54:40 -05001163 /*
1164 * set_bit and clear bit hooks normally require _irqsave/restore
1165 * but in this case, we are only testeing for the DELALLOC
1166 * bit, which is only set or cleared with irqs on
1167 */
Chris Masonb0c68f82008-01-31 11:05:37 -05001168 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001169 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason75eff682008-12-15 15:54:40 -05001170 spin_lock(&root->fs_info->delalloc_lock);
Chris Mason90692182008-02-08 13:49:28 -05001171 BTRFS_I(inode)->delalloc_bytes += end - start + 1;
Chris Mason291d6732008-01-29 15:55:23 -05001172 root->fs_info->delalloc_bytes += end - start + 1;
Chris Masonea8c2812008-08-04 23:17:27 -04001173 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1174 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1175 &root->fs_info->delalloc_inodes);
1176 }
Chris Mason75eff682008-12-15 15:54:40 -05001177 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001178 }
1179 return 0;
1180}
1181
Chris Masond352ac62008-09-29 15:18:18 -04001182/*
1183 * extent_io.c clear_bit_hook, see set_bit_hook for why
1184 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001185static int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -05001186 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -05001187{
Chris Mason75eff682008-12-15 15:54:40 -05001188 /*
1189 * set_bit and clear bit hooks normally require _irqsave/restore
1190 * but in this case, we are only testeing for the DELALLOC
1191 * bit, which is only set or cleared with irqs on
1192 */
Chris Masonb0c68f82008-01-31 11:05:37 -05001193 if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001194 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonbcbfce82008-04-22 13:26:47 -04001195
Chris Mason75eff682008-12-15 15:54:40 -05001196 spin_lock(&root->fs_info->delalloc_lock);
Chris Masonb0c68f82008-01-31 11:05:37 -05001197 if (end - start + 1 > root->fs_info->delalloc_bytes) {
Chris Masond3977122009-01-05 21:25:51 -05001198 printk(KERN_INFO "btrfs warning: delalloc account "
1199 "%llu %llu\n",
1200 (unsigned long long)end - start + 1,
1201 (unsigned long long)
1202 root->fs_info->delalloc_bytes);
Chris Masonb0c68f82008-01-31 11:05:37 -05001203 root->fs_info->delalloc_bytes = 0;
Chris Mason90692182008-02-08 13:49:28 -05001204 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masonb0c68f82008-01-31 11:05:37 -05001205 } else {
1206 root->fs_info->delalloc_bytes -= end - start + 1;
Chris Mason90692182008-02-08 13:49:28 -05001207 BTRFS_I(inode)->delalloc_bytes -= end - start + 1;
Chris Masonb0c68f82008-01-31 11:05:37 -05001208 }
Chris Masonea8c2812008-08-04 23:17:27 -04001209 if (BTRFS_I(inode)->delalloc_bytes == 0 &&
1210 !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1211 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1212 }
Chris Mason75eff682008-12-15 15:54:40 -05001213 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001214 }
1215 return 0;
1216}
1217
Chris Masond352ac62008-09-29 15:18:18 -04001218/*
1219 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1220 * we don't create bios that span stripes or chunks
1221 */
Chris Mason239b14b2008-03-24 15:02:07 -04001222int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001223 size_t size, struct bio *bio,
1224 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001225{
1226 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
1227 struct btrfs_mapping_tree *map_tree;
Chris Masona62b9402008-10-03 16:31:08 -04001228 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001229 u64 length = 0;
1230 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001231 int ret;
1232
Chris Mason771ed682008-11-06 22:02:51 -05001233 if (bio_flags & EXTENT_BIO_COMPRESSED)
1234 return 0;
1235
Chris Masonf2d8d742008-04-21 10:03:05 -04001236 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001237 map_tree = &root->fs_info->mapping_tree;
1238 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04001239 ret = btrfs_map_block(map_tree, READ, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001240 &map_length, NULL, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04001241
Chris Masond3977122009-01-05 21:25:51 -05001242 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001243 return 1;
Chris Mason239b14b2008-03-24 15:02:07 -04001244 return 0;
1245}
1246
Chris Masond352ac62008-09-29 15:18:18 -04001247/*
1248 * in order to insert checksums into the metadata in large chunks,
1249 * we wait until bio submission time. All the pages in the bio are
1250 * checksummed and sums are attached onto the ordered extent record.
1251 *
1252 * At IO completion time the cums attached on the ordered extent record
1253 * are inserted into the btree
1254 */
Chris Masond3977122009-01-05 21:25:51 -05001255static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1256 struct bio *bio, int mirror_num,
1257 unsigned long bio_flags)
Chris Mason065631f2008-02-20 12:07:25 -05001258{
Chris Mason065631f2008-02-20 12:07:25 -05001259 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001260 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001261
Chris Masond20f7042008-12-08 16:58:54 -05001262 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Chris Mason44b8bd72008-04-16 11:14:51 -04001263 BUG_ON(ret);
Chris Mason4a69a412008-11-06 22:03:00 -05001264 return 0;
1265}
Chris Masone0156402008-04-16 11:15:20 -04001266
Chris Mason4a69a412008-11-06 22:03:00 -05001267/*
1268 * in order to insert checksums into the metadata in large chunks,
1269 * we wait until bio submission time. All the pages in the bio are
1270 * checksummed and sums are attached onto the ordered extent record.
1271 *
1272 * At IO completion time the cums attached on the ordered extent record
1273 * are inserted into the btree
1274 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001275static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Mason4a69a412008-11-06 22:03:00 -05001276 int mirror_num, unsigned long bio_flags)
1277{
1278 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason8b712842008-06-11 16:50:36 -04001279 return btrfs_map_bio(root, rw, bio, mirror_num, 1);
Chris Mason44b8bd72008-04-16 11:14:51 -04001280}
1281
Chris Masond352ac62008-09-29 15:18:18 -04001282/*
Chris Masoncad321a2008-12-17 14:51:42 -05001283 * extent_io.c submission hook. This does the right thing for csum calculation
1284 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001285 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001286static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masonc8b97812008-10-29 14:49:59 -04001287 int mirror_num, unsigned long bio_flags)
Chris Mason44b8bd72008-04-16 11:14:51 -04001288{
1289 struct btrfs_root *root = BTRFS_I(inode)->root;
1290 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001291 int skip_sum;
Chris Mason44b8bd72008-04-16 11:14:51 -04001292
Chris Masoncad321a2008-12-17 14:51:42 -05001293 skip_sum = btrfs_test_flag(inode, NODATASUM);
1294
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
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001298 if (!(rw & (1 << BIO_RW))) {
Chris Masond20f7042008-12-08 16:58:54 -05001299 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Chris Masonc8b97812008-10-29 14:49:59 -04001300 return btrfs_submit_compressed_read(inode, bio,
1301 mirror_num, bio_flags);
Chris Masond20f7042008-12-08 16:58:54 -05001302 } else if (!skip_sum)
1303 btrfs_lookup_bio_sums(root, inode, bio, NULL);
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001304 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001305 } else if (!skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001306 /* csum items have already been cloned */
1307 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1308 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001309 /* we're doing a write, do the async checksumming */
1310 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001311 inode, rw, bio, mirror_num,
Chris Mason4a69a412008-11-06 22:03:00 -05001312 bio_flags, __btrfs_submit_bio_start,
1313 __btrfs_submit_bio_done);
Chris Mason19b9bdb2008-10-30 14:23:13 -04001314 }
1315
Chris Mason0b86a832008-03-24 15:01:56 -04001316mapit:
Chris Mason8b712842008-06-11 16:50:36 -04001317 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
Chris Mason065631f2008-02-20 12:07:25 -05001318}
Chris Mason6885f302008-02-20 16:11:05 -05001319
Chris Masond352ac62008-09-29 15:18:18 -04001320/*
1321 * given a list of ordered sums record them in the inode. This happens
1322 * at IO completion time based on sums calculated at bio submission time.
1323 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001324static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001325 struct inode *inode, u64 file_offset,
1326 struct list_head *list)
1327{
1328 struct list_head *cur;
1329 struct btrfs_ordered_sum *sum;
1330
1331 btrfs_set_trans_block_group(trans, inode);
Chris Masonba1da2f2008-07-17 12:54:15 -04001332 list_for_each(cur, list) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04001333 sum = list_entry(cur, struct btrfs_ordered_sum, list);
Chris Masond20f7042008-12-08 16:58:54 -05001334 btrfs_csum_file_blocks(trans,
1335 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001336 }
1337 return 0;
1338}
1339
Chris Masonea8c2812008-08-04 23:17:27 -04001340int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end)
1341{
Chris Masond3977122009-01-05 21:25:51 -05001342 if ((end & (PAGE_CACHE_SIZE - 1)) == 0)
Chris Mason771ed682008-11-06 22:02:51 -05001343 WARN_ON(1);
Chris Masonea8c2812008-08-04 23:17:27 -04001344 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
1345 GFP_NOFS);
1346}
1347
Chris Masond352ac62008-09-29 15:18:18 -04001348/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001349struct btrfs_writepage_fixup {
1350 struct page *page;
1351 struct btrfs_work work;
1352};
1353
Christoph Hellwigb2950862008-12-02 09:54:17 -05001354static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001355{
1356 struct btrfs_writepage_fixup *fixup;
1357 struct btrfs_ordered_extent *ordered;
1358 struct page *page;
1359 struct inode *inode;
1360 u64 page_start;
1361 u64 page_end;
1362
1363 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1364 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001365again:
Chris Mason247e7432008-07-17 12:53:51 -04001366 lock_page(page);
1367 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1368 ClearPageChecked(page);
1369 goto out_page;
1370 }
1371
1372 inode = page->mapping->host;
1373 page_start = page_offset(page);
1374 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1375
1376 lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001377
1378 /* already ordered? We're done */
1379 if (test_range_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
1380 EXTENT_ORDERED, 0)) {
Chris Mason247e7432008-07-17 12:53:51 -04001381 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001382 }
1383
1384 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1385 if (ordered) {
1386 unlock_extent(&BTRFS_I(inode)->io_tree, page_start,
1387 page_end, GFP_NOFS);
1388 unlock_page(page);
1389 btrfs_start_ordered_extent(inode, ordered, 1);
1390 goto again;
1391 }
Chris Mason247e7432008-07-17 12:53:51 -04001392
Chris Masonea8c2812008-08-04 23:17:27 -04001393 btrfs_set_extent_delalloc(inode, page_start, page_end);
Chris Mason247e7432008-07-17 12:53:51 -04001394 ClearPageChecked(page);
1395out:
1396 unlock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
1397out_page:
1398 unlock_page(page);
1399 page_cache_release(page);
1400}
1401
1402/*
1403 * There are a few paths in the higher layers of the kernel that directly
1404 * set the page dirty bit without asking the filesystem if it is a
1405 * good idea. This causes problems because we want to make sure COW
1406 * properly happens and the data=ordered rules are followed.
1407 *
Chris Masonc8b97812008-10-29 14:49:59 -04001408 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001409 * hasn't been properly setup for IO. We kick off an async process
1410 * to fix it up. The async helper will wait for ordered extents, set
1411 * the delalloc bit and make it safe to write the page.
1412 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001413static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001414{
1415 struct inode *inode = page->mapping->host;
1416 struct btrfs_writepage_fixup *fixup;
1417 struct btrfs_root *root = BTRFS_I(inode)->root;
1418 int ret;
1419
1420 ret = test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1421 EXTENT_ORDERED, 0);
1422 if (ret)
1423 return 0;
1424
1425 if (PageChecked(page))
1426 return -EAGAIN;
1427
1428 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1429 if (!fixup)
1430 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001431
Chris Mason247e7432008-07-17 12:53:51 -04001432 SetPageChecked(page);
1433 page_cache_get(page);
1434 fixup->work.func = btrfs_writepage_fixup_worker;
1435 fixup->page = page;
1436 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
1437 return -EAGAIN;
1438}
1439
Yan Zhengd899e052008-10-30 14:25:28 -04001440static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1441 struct inode *inode, u64 file_pos,
1442 u64 disk_bytenr, u64 disk_num_bytes,
1443 u64 num_bytes, u64 ram_bytes,
1444 u8 compression, u8 encryption,
1445 u16 other_encoding, int extent_type)
1446{
1447 struct btrfs_root *root = BTRFS_I(inode)->root;
1448 struct btrfs_file_extent_item *fi;
1449 struct btrfs_path *path;
1450 struct extent_buffer *leaf;
1451 struct btrfs_key ins;
1452 u64 hint;
1453 int ret;
1454
1455 path = btrfs_alloc_path();
1456 BUG_ON(!path);
1457
1458 ret = btrfs_drop_extents(trans, root, inode, file_pos,
1459 file_pos + num_bytes, file_pos, &hint);
1460 BUG_ON(ret);
1461
1462 ins.objectid = inode->i_ino;
1463 ins.offset = file_pos;
1464 ins.type = BTRFS_EXTENT_DATA_KEY;
1465 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
1466 BUG_ON(ret);
1467 leaf = path->nodes[0];
1468 fi = btrfs_item_ptr(leaf, path->slots[0],
1469 struct btrfs_file_extent_item);
1470 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1471 btrfs_set_file_extent_type(leaf, fi, extent_type);
1472 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1473 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1474 btrfs_set_file_extent_offset(leaf, fi, 0);
1475 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1476 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1477 btrfs_set_file_extent_compression(leaf, fi, compression);
1478 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1479 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
1480 btrfs_mark_buffer_dirty(leaf);
1481
1482 inode_add_bytes(inode, num_bytes);
1483 btrfs_drop_extent_cache(inode, file_pos, file_pos + num_bytes - 1, 0);
1484
1485 ins.objectid = disk_bytenr;
1486 ins.offset = disk_num_bytes;
1487 ins.type = BTRFS_EXTENT_ITEM_KEY;
1488 ret = btrfs_alloc_reserved_extent(trans, root, leaf->start,
1489 root->root_key.objectid,
1490 trans->transid, inode->i_ino, &ins);
1491 BUG_ON(ret);
1492
1493 btrfs_free_path(path);
1494 return 0;
1495}
1496
Chris Masond352ac62008-09-29 15:18:18 -04001497/* as ordered data IO finishes, this gets called so we can finish
1498 * an ordered extent if the range of bytes in the file it covers are
1499 * fully written.
1500 */
Chris Mason211f90e2008-07-18 11:56:15 -04001501static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001502{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001503 struct btrfs_root *root = BTRFS_I(inode)->root;
1504 struct btrfs_trans_handle *trans;
1505 struct btrfs_ordered_extent *ordered_extent;
1506 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan Zhengd899e052008-10-30 14:25:28 -04001507 int compressed = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001508 int ret;
1509
1510 ret = btrfs_dec_test_ordered_pending(inode, start, end - start + 1);
Chris Masonba1da2f2008-07-17 12:54:15 -04001511 if (!ret)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001512 return 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001513
Chris Masonf9295742008-07-17 12:54:14 -04001514 trans = btrfs_join_transaction(root, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001515
1516 ordered_extent = btrfs_lookup_ordered_extent(inode, start);
1517 BUG_ON(!ordered_extent);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001518 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags))
1519 goto nocow;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001520
1521 lock_extent(io_tree, ordered_extent->file_offset,
1522 ordered_extent->file_offset + ordered_extent->len - 1,
1523 GFP_NOFS);
1524
Chris Masonc8b97812008-10-29 14:49:59 -04001525 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Yan Zhengd899e052008-10-30 14:25:28 -04001526 compressed = 1;
1527 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
1528 BUG_ON(compressed);
1529 ret = btrfs_mark_extent_written(trans, root, inode,
1530 ordered_extent->file_offset,
1531 ordered_extent->file_offset +
1532 ordered_extent->len);
1533 BUG_ON(ret);
1534 } else {
1535 ret = insert_reserved_file_extent(trans, inode,
1536 ordered_extent->file_offset,
1537 ordered_extent->start,
1538 ordered_extent->disk_len,
1539 ordered_extent->len,
1540 ordered_extent->len,
1541 compressed, 0, 0,
1542 BTRFS_FILE_EXTENT_REG);
1543 BUG_ON(ret);
1544 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04001545 unlock_extent(io_tree, ordered_extent->file_offset,
1546 ordered_extent->file_offset + ordered_extent->len - 1,
1547 GFP_NOFS);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001548nocow:
Chris Masone6dcd2d2008-07-17 12:53:50 -04001549 add_pending_csums(trans, inode, ordered_extent->file_offset,
1550 &ordered_extent->list);
1551
Chris Mason34353022008-09-23 20:19:49 -04001552 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Chris Masondbe674a2008-07-17 12:54:05 -04001553 btrfs_ordered_update_i_size(inode, ordered_extent);
Chris Masone02119d2008-09-05 16:13:11 -04001554 btrfs_update_inode(trans, root, inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001555 btrfs_remove_ordered_extent(inode, ordered_extent);
Chris Mason34353022008-09-23 20:19:49 -04001556 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
Chris Mason7f3c74f2008-07-18 12:01:11 -04001557
Chris Masone6dcd2d2008-07-17 12:53:50 -04001558 /* once for us */
1559 btrfs_put_ordered_extent(ordered_extent);
1560 /* once for the tree */
1561 btrfs_put_ordered_extent(ordered_extent);
1562
Chris Masone6dcd2d2008-07-17 12:53:50 -04001563 btrfs_end_transaction(trans, root);
1564 return 0;
1565}
1566
Christoph Hellwigb2950862008-12-02 09:54:17 -05001567static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04001568 struct extent_state *state, int uptodate)
1569{
1570 return btrfs_finish_ordered_io(page->mapping->host, start, end);
1571}
1572
Chris Masond352ac62008-09-29 15:18:18 -04001573/*
1574 * When IO fails, either with EIO or csum verification fails, we
1575 * try other mirrors that might have a good copy of the data. This
1576 * io_failure_record is used to record state as we go through all the
1577 * mirrors. If another mirror has good data, the page is set up to date
1578 * and things continue. If a good mirror can't be found, the original
1579 * bio end_io callback is called to indicate things have failed.
1580 */
Chris Mason7e383262008-04-09 16:28:12 -04001581struct io_failure_record {
1582 struct page *page;
1583 u64 start;
1584 u64 len;
1585 u64 logical;
Chris Masond20f7042008-12-08 16:58:54 -05001586 unsigned long bio_flags;
Chris Mason7e383262008-04-09 16:28:12 -04001587 int last_mirror;
1588};
1589
Christoph Hellwigb2950862008-12-02 09:54:17 -05001590static int btrfs_io_failed_hook(struct bio *failed_bio,
Chris Mason1259ab72008-05-12 13:39:03 -04001591 struct page *page, u64 start, u64 end,
1592 struct extent_state *state)
Chris Mason7e383262008-04-09 16:28:12 -04001593{
1594 struct io_failure_record *failrec = NULL;
1595 u64 private;
1596 struct extent_map *em;
1597 struct inode *inode = page->mapping->host;
1598 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Chris Mason3b951512008-04-17 11:29:12 -04001599 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason7e383262008-04-09 16:28:12 -04001600 struct bio *bio;
1601 int num_copies;
1602 int ret;
Chris Mason1259ab72008-05-12 13:39:03 -04001603 int rw;
Chris Mason7e383262008-04-09 16:28:12 -04001604 u64 logical;
1605
1606 ret = get_state_private(failure_tree, start, &private);
1607 if (ret) {
Chris Mason7e383262008-04-09 16:28:12 -04001608 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
1609 if (!failrec)
1610 return -ENOMEM;
1611 failrec->start = start;
1612 failrec->len = end - start + 1;
1613 failrec->last_mirror = 0;
Chris Masond20f7042008-12-08 16:58:54 -05001614 failrec->bio_flags = 0;
Chris Mason7e383262008-04-09 16:28:12 -04001615
Chris Mason3b951512008-04-17 11:29:12 -04001616 spin_lock(&em_tree->lock);
1617 em = lookup_extent_mapping(em_tree, start, failrec->len);
1618 if (em->start > start || em->start + em->len < start) {
1619 free_extent_map(em);
1620 em = NULL;
1621 }
1622 spin_unlock(&em_tree->lock);
Chris Mason7e383262008-04-09 16:28:12 -04001623
1624 if (!em || IS_ERR(em)) {
1625 kfree(failrec);
1626 return -EIO;
1627 }
1628 logical = start - em->start;
1629 logical = em->block_start + logical;
Chris Masond20f7042008-12-08 16:58:54 -05001630 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
1631 logical = em->block_start;
1632 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
1633 }
Chris Mason7e383262008-04-09 16:28:12 -04001634 failrec->logical = logical;
1635 free_extent_map(em);
1636 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
1637 EXTENT_DIRTY, GFP_NOFS);
Chris Mason587f7702008-04-11 12:16:46 -04001638 set_state_private(failure_tree, start,
1639 (u64)(unsigned long)failrec);
Chris Mason7e383262008-04-09 16:28:12 -04001640 } else {
Chris Mason587f7702008-04-11 12:16:46 -04001641 failrec = (struct io_failure_record *)(unsigned long)private;
Chris Mason7e383262008-04-09 16:28:12 -04001642 }
1643 num_copies = btrfs_num_copies(
1644 &BTRFS_I(inode)->root->fs_info->mapping_tree,
1645 failrec->logical, failrec->len);
1646 failrec->last_mirror++;
1647 if (!state) {
Chris Masoncad321a2008-12-17 14:51:42 -05001648 spin_lock(&BTRFS_I(inode)->io_tree.lock);
Chris Mason7e383262008-04-09 16:28:12 -04001649 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
1650 failrec->start,
1651 EXTENT_LOCKED);
1652 if (state && state->start != failrec->start)
1653 state = NULL;
Chris Masoncad321a2008-12-17 14:51:42 -05001654 spin_unlock(&BTRFS_I(inode)->io_tree.lock);
Chris Mason7e383262008-04-09 16:28:12 -04001655 }
1656 if (!state || failrec->last_mirror > num_copies) {
1657 set_state_private(failure_tree, failrec->start, 0);
1658 clear_extent_bits(failure_tree, failrec->start,
1659 failrec->start + failrec->len - 1,
1660 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
1661 kfree(failrec);
1662 return -EIO;
1663 }
1664 bio = bio_alloc(GFP_NOFS, 1);
1665 bio->bi_private = state;
1666 bio->bi_end_io = failed_bio->bi_end_io;
1667 bio->bi_sector = failrec->logical >> 9;
1668 bio->bi_bdev = failed_bio->bi_bdev;
Chris Masone1c4b742008-04-22 13:26:46 -04001669 bio->bi_size = 0;
Chris Masond20f7042008-12-08 16:58:54 -05001670
Chris Mason7e383262008-04-09 16:28:12 -04001671 bio_add_page(bio, page, failrec->len, start - page_offset(page));
Chris Mason1259ab72008-05-12 13:39:03 -04001672 if (failed_bio->bi_rw & (1 << BIO_RW))
1673 rw = WRITE;
1674 else
1675 rw = READ;
1676
1677 BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
Chris Masonc8b97812008-10-29 14:49:59 -04001678 failrec->last_mirror,
Chris Masond20f7042008-12-08 16:58:54 -05001679 failrec->bio_flags);
Chris Mason1259ab72008-05-12 13:39:03 -04001680 return 0;
1681}
1682
Chris Masond352ac62008-09-29 15:18:18 -04001683/*
1684 * each time an IO finishes, we do a fast check in the IO failure tree
1685 * to see if we need to process or clean up an io_failure_record
1686 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001687static int btrfs_clean_io_failures(struct inode *inode, u64 start)
Chris Mason1259ab72008-05-12 13:39:03 -04001688{
1689 u64 private;
1690 u64 private_failure;
1691 struct io_failure_record *failure;
1692 int ret;
1693
1694 private = 0;
1695 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
1696 (u64)-1, 1, EXTENT_DIRTY)) {
1697 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
1698 start, &private_failure);
1699 if (ret == 0) {
1700 failure = (struct io_failure_record *)(unsigned long)
1701 private_failure;
1702 set_state_private(&BTRFS_I(inode)->io_failure_tree,
1703 failure->start, 0);
1704 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
1705 failure->start,
1706 failure->start + failure->len - 1,
1707 EXTENT_DIRTY | EXTENT_LOCKED,
1708 GFP_NOFS);
1709 kfree(failure);
1710 }
1711 }
Chris Mason7e383262008-04-09 16:28:12 -04001712 return 0;
1713}
1714
Chris Masond352ac62008-09-29 15:18:18 -04001715/*
1716 * when reads are done, we need to check csums to verify the data is correct
1717 * if there's a match, we allow the bio to finish. If not, we go through
1718 * the io_failure_record routines to find good copies
1719 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001720static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason70dec802008-01-29 09:59:12 -05001721 struct extent_state *state)
Chris Mason07157aa2007-08-30 08:50:51 -04001722{
Chris Mason35ebb932007-10-30 16:56:53 -04001723 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -04001724 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05001725 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04001726 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -05001727 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -04001728 int ret;
Chris Masonff79f812007-10-15 16:22:25 -04001729 struct btrfs_root *root = BTRFS_I(inode)->root;
1730 u32 csum = ~(u32)0;
Jens Axboebbf0d002007-10-19 09:23:07 -04001731 unsigned long flags;
Chris Masond1310b22008-01-24 16:13:08 -05001732
Chris Masond20f7042008-12-08 16:58:54 -05001733 if (PageChecked(page)) {
1734 ClearPageChecked(page);
1735 goto good;
1736 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001737 if (btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -05001738 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05001739
Yan Zheng17d217f2008-12-12 10:03:38 -05001740 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
1741 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1)) {
1742 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
1743 GFP_NOFS);
1744 return 0;
1745 }
1746
Yanc2e639f2008-02-04 08:57:25 -05001747 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -05001748 private = state->private;
1749 ret = 0;
1750 } else {
1751 ret = get_state_private(io_tree, start, &private);
1752 }
Jens Axboebbf0d002007-10-19 09:23:07 -04001753 local_irq_save(flags);
Chris Mason07157aa2007-08-30 08:50:51 -04001754 kaddr = kmap_atomic(page, KM_IRQ0);
Chris Masond3977122009-01-05 21:25:51 -05001755 if (ret)
Chris Mason07157aa2007-08-30 08:50:51 -04001756 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05001757
Chris Masonff79f812007-10-15 16:22:25 -04001758 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
1759 btrfs_csum_final(csum, (char *)&csum);
Chris Masond3977122009-01-05 21:25:51 -05001760 if (csum != private)
Chris Mason07157aa2007-08-30 08:50:51 -04001761 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05001762
Chris Mason07157aa2007-08-30 08:50:51 -04001763 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -04001764 local_irq_restore(flags);
Chris Masond20f7042008-12-08 16:58:54 -05001765good:
Chris Mason7e383262008-04-09 16:28:12 -04001766 /* if the io failure tree for this inode is non-empty,
1767 * check to see if we've recovered from a failed IO
1768 */
Chris Mason1259ab72008-05-12 13:39:03 -04001769 btrfs_clean_io_failures(inode, start);
Chris Mason07157aa2007-08-30 08:50:51 -04001770 return 0;
1771
1772zeroit:
Chris Masond3977122009-01-05 21:25:51 -05001773 printk(KERN_INFO "btrfs csum failed ino %lu off %llu csum %u "
1774 "private %llu\n", page->mapping->host->i_ino,
1775 (unsigned long long)start, csum,
1776 (unsigned long long)private);
Chris Masondb945352007-10-15 16:15:53 -04001777 memset(kaddr + offset, 1, end - start + 1);
1778 flush_dcache_page(page);
Chris Mason07157aa2007-08-30 08:50:51 -04001779 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -04001780 local_irq_restore(flags);
Chris Mason3b951512008-04-17 11:29:12 -04001781 if (private == 0)
1782 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04001783 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04001784}
Chris Masonb888db2b2007-08-27 16:49:44 -04001785
Josef Bacik7b128762008-07-24 12:17:14 -04001786/*
1787 * This creates an orphan entry for the given inode in case something goes
1788 * wrong in the middle of an unlink/truncate.
1789 */
1790int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
1791{
1792 struct btrfs_root *root = BTRFS_I(inode)->root;
1793 int ret = 0;
1794
Yanbcc63ab2008-07-30 16:29:20 -04001795 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001796
1797 /* already on the orphan list, we're good */
1798 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
Yanbcc63ab2008-07-30 16:29:20 -04001799 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001800 return 0;
1801 }
1802
1803 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
1804
Yanbcc63ab2008-07-30 16:29:20 -04001805 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001806
1807 /*
1808 * insert an orphan item to track this unlinked/truncated file
1809 */
1810 ret = btrfs_insert_orphan_item(trans, root, inode->i_ino);
1811
1812 return ret;
1813}
1814
1815/*
1816 * We have done the truncate/delete so we can go ahead and remove the orphan
1817 * item for this particular inode.
1818 */
1819int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
1820{
1821 struct btrfs_root *root = BTRFS_I(inode)->root;
1822 int ret = 0;
1823
Yanbcc63ab2008-07-30 16:29:20 -04001824 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001825
1826 if (list_empty(&BTRFS_I(inode)->i_orphan)) {
Yanbcc63ab2008-07-30 16:29:20 -04001827 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001828 return 0;
1829 }
1830
1831 list_del_init(&BTRFS_I(inode)->i_orphan);
1832 if (!trans) {
Yanbcc63ab2008-07-30 16:29:20 -04001833 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001834 return 0;
1835 }
1836
Yanbcc63ab2008-07-30 16:29:20 -04001837 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001838
1839 ret = btrfs_del_orphan_item(trans, root, inode->i_ino);
1840
1841 return ret;
1842}
1843
1844/*
1845 * this cleans up any orphans that may be left on the list from the last use
1846 * of this root.
1847 */
1848void btrfs_orphan_cleanup(struct btrfs_root *root)
1849{
1850 struct btrfs_path *path;
1851 struct extent_buffer *leaf;
1852 struct btrfs_item *item;
1853 struct btrfs_key key, found_key;
1854 struct btrfs_trans_handle *trans;
1855 struct inode *inode;
1856 int ret = 0, nr_unlink = 0, nr_truncate = 0;
1857
Josef Bacik7b128762008-07-24 12:17:14 -04001858 path = btrfs_alloc_path();
1859 if (!path)
1860 return;
1861 path->reada = -1;
1862
1863 key.objectid = BTRFS_ORPHAN_OBJECTID;
1864 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
1865 key.offset = (u64)-1;
1866
Josef Bacik7b128762008-07-24 12:17:14 -04001867
1868 while (1) {
1869 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1870 if (ret < 0) {
1871 printk(KERN_ERR "Error searching slot for orphan: %d"
1872 "\n", ret);
1873 break;
1874 }
1875
1876 /*
1877 * if ret == 0 means we found what we were searching for, which
1878 * is weird, but possible, so only screw with path if we didnt
1879 * find the key and see if we have stuff that matches
1880 */
1881 if (ret > 0) {
1882 if (path->slots[0] == 0)
1883 break;
1884 path->slots[0]--;
1885 }
1886
1887 /* pull out the item */
1888 leaf = path->nodes[0];
1889 item = btrfs_item_nr(leaf, path->slots[0]);
1890 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1891
1892 /* make sure the item matches what we want */
1893 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
1894 break;
1895 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
1896 break;
1897
1898 /* release the path since we're done with it */
1899 btrfs_release_path(root, path);
1900
1901 /*
1902 * this is where we are basically btrfs_lookup, without the
1903 * crossing root thing. we store the inode number in the
1904 * offset of the orphan item.
1905 */
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001906 inode = btrfs_iget_locked(root->fs_info->sb,
Josef Bacik7b128762008-07-24 12:17:14 -04001907 found_key.offset, root);
1908 if (!inode)
1909 break;
1910
1911 if (inode->i_state & I_NEW) {
1912 BTRFS_I(inode)->root = root;
1913
1914 /* have to set the location manually */
1915 BTRFS_I(inode)->location.objectid = inode->i_ino;
1916 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
1917 BTRFS_I(inode)->location.offset = 0;
1918
1919 btrfs_read_locked_inode(inode);
1920 unlock_new_inode(inode);
1921 }
1922
1923 /*
1924 * add this inode to the orphan list so btrfs_orphan_del does
1925 * the proper thing when we hit it
1926 */
Yanbcc63ab2008-07-30 16:29:20 -04001927 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001928 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
Yanbcc63ab2008-07-30 16:29:20 -04001929 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001930
1931 /*
1932 * if this is a bad inode, means we actually succeeded in
1933 * removing the inode, but not the orphan record, which means
1934 * we need to manually delete the orphan since iput will just
1935 * do a destroy_inode
1936 */
1937 if (is_bad_inode(inode)) {
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001938 trans = btrfs_start_transaction(root, 1);
Josef Bacik7b128762008-07-24 12:17:14 -04001939 btrfs_orphan_del(trans, inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001940 btrfs_end_transaction(trans, root);
Josef Bacik7b128762008-07-24 12:17:14 -04001941 iput(inode);
1942 continue;
1943 }
1944
1945 /* if we have links, this was a truncate, lets do that */
1946 if (inode->i_nlink) {
1947 nr_truncate++;
1948 btrfs_truncate(inode);
1949 } else {
1950 nr_unlink++;
1951 }
1952
1953 /* this will do delete_inode and everything for us */
1954 iput(inode);
1955 }
1956
1957 if (nr_unlink)
1958 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
1959 if (nr_truncate)
1960 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
1961
1962 btrfs_free_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04001963}
1964
Chris Masond352ac62008-09-29 15:18:18 -04001965/*
1966 * read an inode from the btree into the in-memory inode
1967 */
Chris Mason39279cc2007-06-12 06:35:45 -04001968void btrfs_read_locked_inode(struct inode *inode)
1969{
1970 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04001971 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001972 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04001973 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04001974 struct btrfs_root *root = BTRFS_I(inode)->root;
1975 struct btrfs_key location;
1976 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -04001977 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04001978 int ret;
1979
1980 path = btrfs_alloc_path();
1981 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -04001982 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05001983
Chris Mason39279cc2007-06-12 06:35:45 -04001984 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001985 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04001986 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04001987
Chris Mason5f39d392007-10-15 16:14:19 -04001988 leaf = path->nodes[0];
1989 inode_item = btrfs_item_ptr(leaf, path->slots[0],
1990 struct btrfs_inode_item);
1991
1992 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
1993 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
1994 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
1995 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
Chris Masondbe674a2008-07-17 12:54:05 -04001996 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04001997
1998 tspec = btrfs_inode_atime(inode_item);
1999 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2000 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2001
2002 tspec = btrfs_inode_mtime(inode_item);
2003 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2004 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2005
2006 tspec = btrfs_inode_ctime(inode_item);
2007 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2008 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2009
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002010 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04002011 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Chris Masonc3027eb2008-12-08 16:40:21 -05002012 BTRFS_I(inode)->sequence = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04002013 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04002014 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002015 rdev = btrfs_inode_rdev(leaf, inode_item);
2016
Josef Bacikaec74772008-07-24 12:12:38 -04002017 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05002018 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Josef Bacikaec74772008-07-24 12:12:38 -04002019
Chris Mason5f39d392007-10-15 16:14:19 -04002020 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002021 BTRFS_I(inode)->block_group = btrfs_find_block_group(root, 0,
2022 alloc_group_block, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002023 btrfs_free_path(path);
2024 inode_item = NULL;
2025
Chris Mason39279cc2007-06-12 06:35:45 -04002026 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04002027 case S_IFREG:
2028 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04002029 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05002030 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002031 inode->i_fop = &btrfs_file_operations;
2032 inode->i_op = &btrfs_file_inode_operations;
2033 break;
2034 case S_IFDIR:
2035 inode->i_fop = &btrfs_dir_file_operations;
2036 if (root == root->fs_info->tree_root)
2037 inode->i_op = &btrfs_dir_ro_inode_operations;
2038 else
2039 inode->i_op = &btrfs_dir_inode_operations;
2040 break;
2041 case S_IFLNK:
2042 inode->i_op = &btrfs_symlink_inode_operations;
2043 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04002044 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04002045 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04002046 default:
2047 init_special_inode(inode, inode->i_mode, rdev);
2048 break;
Chris Mason39279cc2007-06-12 06:35:45 -04002049 }
2050 return;
2051
2052make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04002053 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002054 make_bad_inode(inode);
2055}
2056
Chris Masond352ac62008-09-29 15:18:18 -04002057/*
2058 * given a leaf and an inode, copy the inode fields into the leaf
2059 */
Chris Masone02119d2008-09-05 16:13:11 -04002060static void fill_inode_item(struct btrfs_trans_handle *trans,
2061 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04002062 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04002063 struct inode *inode)
2064{
Chris Mason5f39d392007-10-15 16:14:19 -04002065 btrfs_set_inode_uid(leaf, item, inode->i_uid);
2066 btrfs_set_inode_gid(leaf, item, inode->i_gid);
Chris Masondbe674a2008-07-17 12:54:05 -04002067 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
Chris Mason5f39d392007-10-15 16:14:19 -04002068 btrfs_set_inode_mode(leaf, item, inode->i_mode);
2069 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
2070
2071 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
2072 inode->i_atime.tv_sec);
2073 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
2074 inode->i_atime.tv_nsec);
2075
2076 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
2077 inode->i_mtime.tv_sec);
2078 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
2079 inode->i_mtime.tv_nsec);
2080
2081 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
2082 inode->i_ctime.tv_sec);
2083 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
2084 inode->i_ctime.tv_nsec);
2085
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002086 btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode));
Chris Masone02119d2008-09-05 16:13:11 -04002087 btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
Chris Masonc3027eb2008-12-08 16:40:21 -05002088 btrfs_set_inode_sequence(leaf, item, BTRFS_I(inode)->sequence);
Chris Masone02119d2008-09-05 16:13:11 -04002089 btrfs_set_inode_transid(leaf, item, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002090 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -05002091 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002092 btrfs_set_inode_block_group(leaf, item, BTRFS_I(inode)->block_group);
Chris Mason39279cc2007-06-12 06:35:45 -04002093}
2094
Chris Masond352ac62008-09-29 15:18:18 -04002095/*
2096 * copy everything in the in-memory inode into the btree.
2097 */
Chris Masond3977122009-01-05 21:25:51 -05002098noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
2099 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002100{
2101 struct btrfs_inode_item *inode_item;
2102 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002103 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002104 int ret;
2105
2106 path = btrfs_alloc_path();
2107 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -04002108 ret = btrfs_lookup_inode(trans, root, path,
2109 &BTRFS_I(inode)->location, 1);
2110 if (ret) {
2111 if (ret > 0)
2112 ret = -ENOENT;
2113 goto failed;
2114 }
2115
Chris Mason5f39d392007-10-15 16:14:19 -04002116 leaf = path->nodes[0];
2117 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04002118 struct btrfs_inode_item);
2119
Chris Masone02119d2008-09-05 16:13:11 -04002120 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002121 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002122 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002123 ret = 0;
2124failed:
Chris Mason39279cc2007-06-12 06:35:45 -04002125 btrfs_free_path(path);
2126 return ret;
2127}
2128
2129
Chris Masond352ac62008-09-29 15:18:18 -04002130/*
2131 * unlink helper that gets used here in inode.c and in the tree logging
2132 * recovery code. It remove a link in a directory with a given name, and
2133 * also drops the back refs in the inode to the directory
2134 */
Chris Masone02119d2008-09-05 16:13:11 -04002135int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2136 struct btrfs_root *root,
2137 struct inode *dir, struct inode *inode,
2138 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04002139{
2140 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04002141 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002142 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002143 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04002144 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04002145 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04002146
2147 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04002148 if (!path) {
2149 ret = -ENOMEM;
2150 goto err;
2151 }
2152
Chris Mason39279cc2007-06-12 06:35:45 -04002153 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
2154 name, name_len, -1);
2155 if (IS_ERR(di)) {
2156 ret = PTR_ERR(di);
2157 goto err;
2158 }
2159 if (!di) {
2160 ret = -ENOENT;
2161 goto err;
2162 }
Chris Mason5f39d392007-10-15 16:14:19 -04002163 leaf = path->nodes[0];
2164 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04002165 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04002166 if (ret)
2167 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -04002168 btrfs_release_path(root, path);
2169
Josef Bacikaec74772008-07-24 12:12:38 -04002170 ret = btrfs_del_inode_ref(trans, root, name, name_len,
Chris Masone02119d2008-09-05 16:13:11 -04002171 inode->i_ino,
2172 dir->i_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04002173 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05002174 printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
Josef Bacikaec74772008-07-24 12:12:38 -04002175 "inode %lu parent %lu\n", name_len, name,
Chris Masone02119d2008-09-05 16:13:11 -04002176 inode->i_ino, dir->i_ino);
Josef Bacikaec74772008-07-24 12:12:38 -04002177 goto err;
2178 }
2179
Chris Mason39279cc2007-06-12 06:35:45 -04002180 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04002181 index, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -04002182 if (IS_ERR(di)) {
2183 ret = PTR_ERR(di);
2184 goto err;
2185 }
2186 if (!di) {
2187 ret = -ENOENT;
2188 goto err;
2189 }
2190 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason925baed2008-06-25 16:01:30 -04002191 btrfs_release_path(root, path);
Chris Mason39279cc2007-06-12 06:35:45 -04002192
Chris Masone02119d2008-09-05 16:13:11 -04002193 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
2194 inode, dir->i_ino);
Chris Mason49eb7e42008-09-11 15:53:12 -04002195 BUG_ON(ret != 0 && ret != -ENOENT);
2196 if (ret != -ENOENT)
2197 BTRFS_I(dir)->log_dirty_trans = trans->transid;
Chris Masone02119d2008-09-05 16:13:11 -04002198
2199 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
2200 dir, index);
2201 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04002202err:
2203 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002204 if (ret)
2205 goto out;
2206
2207 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
2208 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2209 btrfs_update_inode(trans, root, dir);
2210 btrfs_drop_nlink(inode);
2211 ret = btrfs_update_inode(trans, root, inode);
2212 dir->i_sb->s_dirt = 1;
2213out:
Chris Mason39279cc2007-06-12 06:35:45 -04002214 return ret;
2215}
2216
2217static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
2218{
2219 struct btrfs_root *root;
2220 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04002221 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002222 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -05002223 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002224
2225 root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05002226
2227 ret = btrfs_check_free_space(root, 1, 1);
2228 if (ret)
2229 goto fail;
2230
Chris Mason39279cc2007-06-12 06:35:45 -04002231 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002232
Chris Mason39279cc2007-06-12 06:35:45 -04002233 btrfs_set_trans_block_group(trans, dir);
Chris Masone02119d2008-09-05 16:13:11 -04002234 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
2235 dentry->d_name.name, dentry->d_name.len);
Josef Bacik7b128762008-07-24 12:17:14 -04002236
2237 if (inode->i_nlink == 0)
2238 ret = btrfs_orphan_add(trans, inode);
2239
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002240 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04002241
Chris Mason89ce8a62008-06-25 16:01:31 -04002242 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002243fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002244 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002245 return ret;
2246}
2247
2248static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
2249{
2250 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002251 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002252 int ret;
2253 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04002254 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -05002255 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002256
Chris Mason3394e162008-11-17 20:42:26 -05002257 /*
2258 * the FIRST_FREE_OBJECTID check makes sure we don't try to rmdir
2259 * the root of a subvolume or snapshot
2260 */
2261 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE ||
2262 inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) {
Yan134d4512007-10-25 15:49:25 -04002263 return -ENOTEMPTY;
Chris Mason925baed2008-06-25 16:01:30 -04002264 }
Yan134d4512007-10-25 15:49:25 -04002265
Chris Mason1832a6d2007-12-21 16:27:21 -05002266 ret = btrfs_check_free_space(root, 1, 1);
2267 if (ret)
2268 goto fail;
2269
Chris Mason39279cc2007-06-12 06:35:45 -04002270 trans = btrfs_start_transaction(root, 1);
2271 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -04002272
Josef Bacik7b128762008-07-24 12:17:14 -04002273 err = btrfs_orphan_add(trans, inode);
2274 if (err)
2275 goto fail_trans;
2276
Chris Mason39279cc2007-06-12 06:35:45 -04002277 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04002278 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
2279 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05002280 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04002281 btrfs_i_size_write(inode, 0);
Chris Mason39544012007-12-12 14:38:19 -05002282
Josef Bacik7b128762008-07-24 12:17:14 -04002283fail_trans:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002284 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04002285 ret = btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002286fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002287 btrfs_btree_balance_dirty(root, nr);
Chris Mason39544012007-12-12 14:38:19 -05002288
Chris Mason39279cc2007-06-12 06:35:45 -04002289 if (ret && !err)
2290 err = ret;
2291 return err;
2292}
2293
Chris Masond20f7042008-12-08 16:58:54 -05002294#if 0
Chris Mason39279cc2007-06-12 06:35:45 -04002295/*
Chris Mason323ac952008-10-01 19:05:46 -04002296 * when truncating bytes in a file, it is possible to avoid reading
2297 * the leaves that contain only checksum items. This can be the
2298 * majority of the IO required to delete a large file, but it must
2299 * be done carefully.
2300 *
2301 * The keys in the level just above the leaves are checked to make sure
2302 * the lowest key in a given leaf is a csum key, and starts at an offset
2303 * after the new size.
2304 *
2305 * Then the key for the next leaf is checked to make sure it also has
2306 * a checksum item for the same file. If it does, we know our target leaf
2307 * contains only checksum items, and it can be safely freed without reading
2308 * it.
2309 *
2310 * This is just an optimization targeted at large files. It may do
2311 * nothing. It will return 0 unless things went badly.
2312 */
2313static noinline int drop_csum_leaves(struct btrfs_trans_handle *trans,
2314 struct btrfs_root *root,
2315 struct btrfs_path *path,
2316 struct inode *inode, u64 new_size)
2317{
2318 struct btrfs_key key;
2319 int ret;
2320 int nritems;
2321 struct btrfs_key found_key;
2322 struct btrfs_key other_key;
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002323 struct btrfs_leaf_ref *ref;
2324 u64 leaf_gen;
2325 u64 leaf_start;
Chris Mason323ac952008-10-01 19:05:46 -04002326
2327 path->lowest_level = 1;
2328 key.objectid = inode->i_ino;
2329 key.type = BTRFS_CSUM_ITEM_KEY;
2330 key.offset = new_size;
2331again:
2332 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2333 if (ret < 0)
2334 goto out;
2335
2336 if (path->nodes[1] == NULL) {
2337 ret = 0;
2338 goto out;
2339 }
2340 ret = 0;
2341 btrfs_node_key_to_cpu(path->nodes[1], &found_key, path->slots[1]);
2342 nritems = btrfs_header_nritems(path->nodes[1]);
2343
2344 if (!nritems)
2345 goto out;
2346
2347 if (path->slots[1] >= nritems)
2348 goto next_node;
2349
2350 /* did we find a key greater than anything we want to delete? */
2351 if (found_key.objectid > inode->i_ino ||
2352 (found_key.objectid == inode->i_ino && found_key.type > key.type))
2353 goto out;
2354
2355 /* we check the next key in the node to make sure the leave contains
2356 * only checksum items. This comparison doesn't work if our
2357 * leaf is the last one in the node
2358 */
2359 if (path->slots[1] + 1 >= nritems) {
2360next_node:
2361 /* search forward from the last key in the node, this
2362 * will bring us into the next node in the tree
2363 */
2364 btrfs_node_key_to_cpu(path->nodes[1], &found_key, nritems - 1);
2365
2366 /* unlikely, but we inc below, so check to be safe */
2367 if (found_key.offset == (u64)-1)
2368 goto out;
2369
2370 /* search_forward needs a path with locks held, do the
2371 * search again for the original key. It is possible
2372 * this will race with a balance and return a path that
2373 * we could modify, but this drop is just an optimization
2374 * and is allowed to miss some leaves.
2375 */
2376 btrfs_release_path(root, path);
2377 found_key.offset++;
2378
2379 /* setup a max key for search_forward */
2380 other_key.offset = (u64)-1;
2381 other_key.type = key.type;
2382 other_key.objectid = key.objectid;
2383
2384 path->keep_locks = 1;
2385 ret = btrfs_search_forward(root, &found_key, &other_key,
2386 path, 0, 0);
2387 path->keep_locks = 0;
2388 if (ret || found_key.objectid != key.objectid ||
2389 found_key.type != key.type) {
2390 ret = 0;
2391 goto out;
2392 }
2393
2394 key.offset = found_key.offset;
2395 btrfs_release_path(root, path);
2396 cond_resched();
2397 goto again;
2398 }
2399
2400 /* we know there's one more slot after us in the tree,
2401 * read that key so we can verify it is also a checksum item
2402 */
2403 btrfs_node_key_to_cpu(path->nodes[1], &other_key, path->slots[1] + 1);
2404
2405 if (found_key.objectid < inode->i_ino)
2406 goto next_key;
2407
2408 if (found_key.type != key.type || found_key.offset < new_size)
2409 goto next_key;
2410
2411 /*
2412 * if the key for the next leaf isn't a csum key from this objectid,
2413 * we can't be sure there aren't good items inside this leaf.
2414 * Bail out
2415 */
2416 if (other_key.objectid != inode->i_ino || other_key.type != key.type)
2417 goto out;
2418
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002419 leaf_start = btrfs_node_blockptr(path->nodes[1], path->slots[1]);
2420 leaf_gen = btrfs_node_ptr_generation(path->nodes[1], path->slots[1]);
Chris Mason323ac952008-10-01 19:05:46 -04002421 /*
2422 * it is safe to delete this leaf, it contains only
2423 * csum items from this inode at an offset >= new_size
2424 */
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002425 ret = btrfs_del_leaf(trans, root, path, leaf_start);
Chris Mason323ac952008-10-01 19:05:46 -04002426 BUG_ON(ret);
2427
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002428 if (root->ref_cows && leaf_gen < trans->transid) {
2429 ref = btrfs_alloc_leaf_ref(root, 0);
2430 if (ref) {
2431 ref->root_gen = root->root_key.offset;
2432 ref->bytenr = leaf_start;
2433 ref->owner = 0;
2434 ref->generation = leaf_gen;
2435 ref->nritems = 0;
2436
2437 ret = btrfs_add_leaf_ref(root, ref, 0);
2438 WARN_ON(ret);
2439 btrfs_free_leaf_ref(root, ref);
2440 } else {
2441 WARN_ON(1);
2442 }
2443 }
Chris Mason323ac952008-10-01 19:05:46 -04002444next_key:
2445 btrfs_release_path(root, path);
2446
2447 if (other_key.objectid == inode->i_ino &&
2448 other_key.type == key.type && other_key.offset > key.offset) {
2449 key.offset = other_key.offset;
2450 cond_resched();
2451 goto again;
2452 }
2453 ret = 0;
2454out:
2455 /* fixup any changes we've made to the path */
2456 path->lowest_level = 0;
2457 path->keep_locks = 0;
2458 btrfs_release_path(root, path);
2459 return ret;
2460}
2461
Chris Masond20f7042008-12-08 16:58:54 -05002462#endif
2463
Chris Mason323ac952008-10-01 19:05:46 -04002464/*
Chris Mason39279cc2007-06-12 06:35:45 -04002465 * this can truncate away extent items, csum items and directory items.
2466 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04002467 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04002468 *
2469 * csum items that cross the new i_size are truncated to the new size
2470 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04002471 *
2472 * min_type is the minimum key type to truncate down to. If set to 0, this
2473 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04002474 */
Chris Masone02119d2008-09-05 16:13:11 -04002475noinline int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
2476 struct btrfs_root *root,
2477 struct inode *inode,
2478 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04002479{
2480 int ret;
2481 struct btrfs_path *path;
2482 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04002483 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04002484 u32 found_type;
Chris Mason5f39d392007-10-15 16:14:19 -04002485 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002486 struct btrfs_file_extent_item *fi;
2487 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04002488 u64 extent_num_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002489 u64 item_end = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05002490 u64 root_gen = 0;
Chris Masond8d5f3e2007-12-11 12:42:00 -05002491 u64 root_owner = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002492 int found_extent;
2493 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05002494 int pending_del_nr = 0;
2495 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04002496 int extent_type = -1;
Chris Mason771ed682008-11-06 22:02:51 -05002497 int encoding;
Chris Mason3b951512008-04-17 11:29:12 -04002498 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002499
Chris Masone02119d2008-09-05 16:13:11 -04002500 if (root->ref_cows)
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002501 btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002502 path = btrfs_alloc_path();
Chris Mason3c69fae2007-08-07 15:52:22 -04002503 path->reada = -1;
Chris Mason39279cc2007-06-12 06:35:45 -04002504 BUG_ON(!path);
Chris Mason5f39d392007-10-15 16:14:19 -04002505
Chris Mason39279cc2007-06-12 06:35:45 -04002506 /* FIXME, add redo link to tree so we don't leak on crash */
2507 key.objectid = inode->i_ino;
2508 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04002509 key.type = (u8)-1;
2510
Chris Mason85e21ba2008-01-29 15:11:36 -05002511 btrfs_init_path(path);
Chris Mason323ac952008-10-01 19:05:46 -04002512
Chris Mason85e21ba2008-01-29 15:11:36 -05002513search_again:
2514 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masond3977122009-01-05 21:25:51 -05002515 if (ret < 0)
Chris Mason85e21ba2008-01-29 15:11:36 -05002516 goto error;
Chris Masond3977122009-01-05 21:25:51 -05002517
Chris Mason85e21ba2008-01-29 15:11:36 -05002518 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002519 /* there are no items in the tree for us to truncate, we're
2520 * done
2521 */
2522 if (path->slots[0] == 0) {
2523 ret = 0;
2524 goto error;
2525 }
Chris Mason85e21ba2008-01-29 15:11:36 -05002526 path->slots[0]--;
2527 }
2528
Chris Masond3977122009-01-05 21:25:51 -05002529 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04002530 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04002531 leaf = path->nodes[0];
2532 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2533 found_type = btrfs_key_type(&found_key);
Chris Mason771ed682008-11-06 22:02:51 -05002534 encoding = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002535
Chris Mason5f39d392007-10-15 16:14:19 -04002536 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -04002537 break;
Chris Mason5f39d392007-10-15 16:14:19 -04002538
Chris Mason85e21ba2008-01-29 15:11:36 -05002539 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04002540 break;
2541
Chris Mason5f39d392007-10-15 16:14:19 -04002542 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04002543 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04002544 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04002545 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04002546 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Mason771ed682008-11-06 22:02:51 -05002547 encoding = btrfs_file_extent_compression(leaf, fi);
2548 encoding |= btrfs_file_extent_encryption(leaf, fi);
2549 encoding |= btrfs_file_extent_other_encoding(leaf, fi);
2550
Chris Mason179e29e2007-11-01 11:28:41 -04002551 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04002552 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04002553 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04002554 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04002555 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04002556 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04002557 }
Yan008630c2007-11-07 13:31:09 -05002558 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04002559 }
Chris Masone02119d2008-09-05 16:13:11 -04002560 if (item_end < new_size) {
Chris Masond3977122009-01-05 21:25:51 -05002561 if (found_type == BTRFS_DIR_ITEM_KEY)
Chris Masonb888db2b2007-08-27 16:49:44 -04002562 found_type = BTRFS_INODE_ITEM_KEY;
Chris Masond3977122009-01-05 21:25:51 -05002563 else if (found_type == BTRFS_EXTENT_ITEM_KEY)
Chris Masond20f7042008-12-08 16:58:54 -05002564 found_type = BTRFS_EXTENT_DATA_KEY;
Chris Masond3977122009-01-05 21:25:51 -05002565 else if (found_type == BTRFS_EXTENT_DATA_KEY)
Chris Mason85e21ba2008-01-29 15:11:36 -05002566 found_type = BTRFS_XATTR_ITEM_KEY;
Chris Masond3977122009-01-05 21:25:51 -05002567 else if (found_type == BTRFS_XATTR_ITEM_KEY)
Chris Mason85e21ba2008-01-29 15:11:36 -05002568 found_type = BTRFS_INODE_REF_KEY;
Chris Masond3977122009-01-05 21:25:51 -05002569 else if (found_type)
Chris Masonb888db2b2007-08-27 16:49:44 -04002570 found_type--;
Chris Masond3977122009-01-05 21:25:51 -05002571 else
Chris Masonb888db2b2007-08-27 16:49:44 -04002572 break;
Yana61721d2007-09-17 11:08:38 -04002573 btrfs_set_key_type(&key, found_type);
Chris Mason85e21ba2008-01-29 15:11:36 -05002574 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04002575 }
Chris Masone02119d2008-09-05 16:13:11 -04002576 if (found_key.offset >= new_size)
Chris Mason39279cc2007-06-12 06:35:45 -04002577 del_item = 1;
2578 else
2579 del_item = 0;
2580 found_extent = 0;
2581
2582 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04002583 if (found_type != BTRFS_EXTENT_DATA_KEY)
2584 goto delete;
2585
2586 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04002587 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04002588 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason771ed682008-11-06 22:02:51 -05002589 if (!del_item && !encoding) {
Chris Masondb945352007-10-15 16:15:53 -04002590 u64 orig_num_bytes =
2591 btrfs_file_extent_num_bytes(leaf, fi);
Chris Masone02119d2008-09-05 16:13:11 -04002592 extent_num_bytes = new_size -
Chris Mason5f39d392007-10-15 16:14:19 -04002593 found_key.offset + root->sectorsize - 1;
Yanb1632b12008-01-30 11:54:04 -05002594 extent_num_bytes = extent_num_bytes &
2595 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04002596 btrfs_set_file_extent_num_bytes(leaf, fi,
2597 extent_num_bytes);
2598 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05002599 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04002600 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002601 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04002602 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002603 } else {
Chris Masondb945352007-10-15 16:15:53 -04002604 extent_num_bytes =
2605 btrfs_file_extent_disk_num_bytes(leaf,
2606 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04002607 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05002608 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04002609 if (extent_start != 0) {
2610 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04002611 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002612 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04002613 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002614 root_gen = btrfs_header_generation(leaf);
Chris Masond8d5f3e2007-12-11 12:42:00 -05002615 root_owner = btrfs_header_owner(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002616 }
Chris Mason90692182008-02-08 13:49:28 -05002617 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04002618 /*
2619 * we can't truncate inline items that have had
2620 * special encodings
2621 */
2622 if (!del_item &&
2623 btrfs_file_extent_compression(leaf, fi) == 0 &&
2624 btrfs_file_extent_encryption(leaf, fi) == 0 &&
2625 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002626 u32 size = new_size - found_key.offset;
2627
2628 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002629 inode_sub_bytes(inode, item_end + 1 -
2630 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04002631 }
2632 size =
2633 btrfs_file_extent_calc_inline_size(size);
Chris Mason90692182008-02-08 13:49:28 -05002634 ret = btrfs_truncate_item(trans, root, path,
Chris Masone02119d2008-09-05 16:13:11 -04002635 size, 1);
Chris Mason90692182008-02-08 13:49:28 -05002636 BUG_ON(ret);
Chris Masone02119d2008-09-05 16:13:11 -04002637 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002638 inode_sub_bytes(inode, item_end + 1 -
2639 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05002640 }
Chris Mason39279cc2007-06-12 06:35:45 -04002641 }
Chris Mason179e29e2007-11-01 11:28:41 -04002642delete:
Chris Mason39279cc2007-06-12 06:35:45 -04002643 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05002644 if (!pending_del_nr) {
2645 /* no pending yet, add ourselves */
2646 pending_del_slot = path->slots[0];
2647 pending_del_nr = 1;
2648 } else if (pending_del_nr &&
2649 path->slots[0] + 1 == pending_del_slot) {
2650 /* hop on the pending chunk */
2651 pending_del_nr++;
2652 pending_del_slot = path->slots[0];
2653 } else {
Chris Masond3977122009-01-05 21:25:51 -05002654 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05002655 }
Chris Mason39279cc2007-06-12 06:35:45 -04002656 } else {
2657 break;
2658 }
Chris Mason39279cc2007-06-12 06:35:45 -04002659 if (found_extent) {
2660 ret = btrfs_free_extent(trans, root, extent_start,
Chris Mason7bb86312007-12-11 09:25:06 -05002661 extent_num_bytes,
Zheng Yan31840ae2008-09-23 13:14:14 -04002662 leaf->start, root_owner,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002663 root_gen, inode->i_ino, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002664 BUG_ON(ret);
2665 }
Chris Mason85e21ba2008-01-29 15:11:36 -05002666next:
2667 if (path->slots[0] == 0) {
2668 if (pending_del_nr)
2669 goto del_pending;
2670 btrfs_release_path(root, path);
2671 goto search_again;
2672 }
2673
2674 path->slots[0]--;
2675 if (pending_del_nr &&
2676 path->slots[0] + 1 != pending_del_slot) {
2677 struct btrfs_key debug;
2678del_pending:
2679 btrfs_item_key_to_cpu(path->nodes[0], &debug,
2680 pending_del_slot);
2681 ret = btrfs_del_items(trans, root, path,
2682 pending_del_slot,
2683 pending_del_nr);
2684 BUG_ON(ret);
2685 pending_del_nr = 0;
2686 btrfs_release_path(root, path);
2687 goto search_again;
2688 }
Chris Mason39279cc2007-06-12 06:35:45 -04002689 }
2690 ret = 0;
2691error:
Chris Mason85e21ba2008-01-29 15:11:36 -05002692 if (pending_del_nr) {
2693 ret = btrfs_del_items(trans, root, path, pending_del_slot,
2694 pending_del_nr);
2695 }
Chris Mason39279cc2007-06-12 06:35:45 -04002696 btrfs_free_path(path);
2697 inode->i_sb->s_dirt = 1;
2698 return ret;
2699}
2700
Chris Masona52d9a82007-08-27 16:49:44 -04002701/*
2702 * taken from block_truncate_page, but does cow as it zeros out
2703 * any bytes left in the last page in the file.
2704 */
2705static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
2706{
2707 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -04002708 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002709 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2710 struct btrfs_ordered_extent *ordered;
2711 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04002712 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04002713 pgoff_t index = from >> PAGE_CACHE_SHIFT;
2714 unsigned offset = from & (PAGE_CACHE_SIZE-1);
2715 struct page *page;
2716 int ret = 0;
2717 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002718 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04002719
2720 if ((offset & (blocksize - 1)) == 0)
2721 goto out;
2722
2723 ret = -ENOMEM;
Chris Mason211c17f2008-05-15 09:13:45 -04002724again:
Chris Masona52d9a82007-08-27 16:49:44 -04002725 page = grab_cache_page(mapping, index);
2726 if (!page)
2727 goto out;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002728
2729 page_start = page_offset(page);
2730 page_end = page_start + PAGE_CACHE_SIZE - 1;
2731
Chris Masona52d9a82007-08-27 16:49:44 -04002732 if (!PageUptodate(page)) {
2733 ret = btrfs_readpage(NULL, page);
2734 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04002735 if (page->mapping != mapping) {
2736 unlock_page(page);
2737 page_cache_release(page);
2738 goto again;
2739 }
Chris Masona52d9a82007-08-27 16:49:44 -04002740 if (!PageUptodate(page)) {
2741 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04002742 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04002743 }
2744 }
Chris Mason211c17f2008-05-15 09:13:45 -04002745 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002746
2747 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
2748 set_page_extent_mapped(page);
2749
2750 ordered = btrfs_lookup_ordered_extent(inode, page_start);
2751 if (ordered) {
2752 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
2753 unlock_page(page);
2754 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04002755 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002756 btrfs_put_ordered_extent(ordered);
2757 goto again;
2758 }
2759
Chris Masonea8c2812008-08-04 23:17:27 -04002760 btrfs_set_extent_delalloc(inode, page_start, page_end);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002761 ret = 0;
2762 if (offset != PAGE_CACHE_SIZE) {
2763 kaddr = kmap(page);
2764 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
2765 flush_dcache_page(page);
2766 kunmap(page);
2767 }
Chris Mason247e7432008-07-17 12:53:51 -04002768 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002769 set_page_dirty(page);
2770 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04002771
Chris Mason89642222008-07-24 09:41:53 -04002772out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04002773 unlock_page(page);
2774 page_cache_release(page);
2775out:
2776 return ret;
2777}
2778
Yan Zheng9036c102008-10-30 14:19:41 -04002779int btrfs_cont_expand(struct inode *inode, loff_t size)
2780{
2781 struct btrfs_trans_handle *trans;
2782 struct btrfs_root *root = BTRFS_I(inode)->root;
2783 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2784 struct extent_map *em;
2785 u64 mask = root->sectorsize - 1;
2786 u64 hole_start = (inode->i_size + mask) & ~mask;
2787 u64 block_end = (size + mask) & ~mask;
2788 u64 last_byte;
2789 u64 cur_offset;
2790 u64 hole_size;
2791 int err;
2792
2793 if (size <= hole_start)
2794 return 0;
2795
2796 err = btrfs_check_free_space(root, 1, 0);
2797 if (err)
2798 return err;
2799
2800 btrfs_truncate_page(inode->i_mapping, inode->i_size);
2801
2802 while (1) {
2803 struct btrfs_ordered_extent *ordered;
2804 btrfs_wait_ordered_range(inode, hole_start,
2805 block_end - hole_start);
2806 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
2807 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
2808 if (!ordered)
2809 break;
2810 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
2811 btrfs_put_ordered_extent(ordered);
2812 }
2813
2814 trans = btrfs_start_transaction(root, 1);
2815 btrfs_set_trans_block_group(trans, inode);
2816
2817 cur_offset = hole_start;
2818 while (1) {
2819 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
2820 block_end - cur_offset, 0);
2821 BUG_ON(IS_ERR(em) || !em);
2822 last_byte = min(extent_map_end(em), block_end);
2823 last_byte = (last_byte + mask) & ~mask;
2824 if (test_bit(EXTENT_FLAG_VACANCY, &em->flags)) {
Chris Mason771ed682008-11-06 22:02:51 -05002825 u64 hint_byte = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04002826 hole_size = last_byte - cur_offset;
Chris Mason771ed682008-11-06 22:02:51 -05002827 err = btrfs_drop_extents(trans, root, inode,
2828 cur_offset,
2829 cur_offset + hole_size,
2830 cur_offset, &hint_byte);
2831 if (err)
2832 break;
Yan Zheng9036c102008-10-30 14:19:41 -04002833 err = btrfs_insert_file_extent(trans, root,
2834 inode->i_ino, cur_offset, 0,
2835 0, hole_size, 0, hole_size,
2836 0, 0, 0);
2837 btrfs_drop_extent_cache(inode, hole_start,
2838 last_byte - 1, 0);
2839 }
2840 free_extent_map(em);
2841 cur_offset = last_byte;
2842 if (err || cur_offset >= block_end)
2843 break;
2844 }
2845
2846 btrfs_end_transaction(trans, root);
2847 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
2848 return err;
2849}
2850
Chris Mason39279cc2007-06-12 06:35:45 -04002851static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
2852{
2853 struct inode *inode = dentry->d_inode;
2854 int err;
2855
2856 err = inode_change_ok(inode, attr);
2857 if (err)
2858 return err;
2859
2860 if (S_ISREG(inode->i_mode) &&
2861 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
Yan Zheng9036c102008-10-30 14:19:41 -04002862 err = btrfs_cont_expand(inode, attr->ia_size);
Chris Mason54aa1f42007-06-22 14:16:25 -04002863 if (err)
2864 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04002865 }
Yan Zheng9036c102008-10-30 14:19:41 -04002866
Chris Mason39279cc2007-06-12 06:35:45 -04002867 err = inode_setattr(inode, attr);
Josef Bacik33268ea2008-07-24 12:16:36 -04002868
2869 if (!err && ((attr->ia_valid & ATTR_MODE)))
2870 err = btrfs_acl_chmod(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002871 return err;
2872}
Chris Mason61295eb2008-01-14 16:24:38 -05002873
Chris Mason39279cc2007-06-12 06:35:45 -04002874void btrfs_delete_inode(struct inode *inode)
2875{
2876 struct btrfs_trans_handle *trans;
2877 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002878 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04002879 int ret;
2880
2881 truncate_inode_pages(&inode->i_data, 0);
2882 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04002883 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002884 goto no_delete;
2885 }
Chris Mason4a096752008-07-21 10:29:44 -04002886 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04002887
Chris Masondbe674a2008-07-17 12:54:05 -04002888 btrfs_i_size_write(inode, 0);
Yan Zheng180591b2009-01-06 09:58:06 -05002889 trans = btrfs_join_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002890
Chris Mason39279cc2007-06-12 06:35:45 -04002891 btrfs_set_trans_block_group(trans, inode);
Chris Masone02119d2008-09-05 16:13:11 -04002892 ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size, 0);
Josef Bacik7b128762008-07-24 12:17:14 -04002893 if (ret) {
2894 btrfs_orphan_del(NULL, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04002895 goto no_delete_lock;
Josef Bacik7b128762008-07-24 12:17:14 -04002896 }
2897
2898 btrfs_orphan_del(trans, inode);
Chris Mason85e21ba2008-01-29 15:11:36 -05002899
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002900 nr = trans->blocks_used;
Chris Mason85e21ba2008-01-29 15:11:36 -05002901 clear_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002902
Chris Mason39279cc2007-06-12 06:35:45 -04002903 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002904 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002905 return;
Chris Mason54aa1f42007-06-22 14:16:25 -04002906
2907no_delete_lock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002908 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04002909 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002910 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002911no_delete:
2912 clear_inode(inode);
2913}
2914
2915/*
2916 * this returns the key found in the dir entry in the location pointer.
2917 * If no dir entries were found, location->objectid is 0.
2918 */
2919static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
2920 struct btrfs_key *location)
2921{
2922 const char *name = dentry->d_name.name;
2923 int namelen = dentry->d_name.len;
2924 struct btrfs_dir_item *di;
2925 struct btrfs_path *path;
2926 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04002927 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002928
2929 path = btrfs_alloc_path();
2930 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -05002931
Chris Mason39279cc2007-06-12 06:35:45 -04002932 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
2933 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04002934 if (IS_ERR(di))
2935 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05002936
2937 if (!di || IS_ERR(di))
Chris Mason39544012007-12-12 14:38:19 -05002938 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05002939
Chris Mason5f39d392007-10-15 16:14:19 -04002940 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04002941out:
Chris Mason39279cc2007-06-12 06:35:45 -04002942 btrfs_free_path(path);
2943 return ret;
Chris Mason39544012007-12-12 14:38:19 -05002944out_err:
2945 location->objectid = 0;
2946 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04002947}
2948
2949/*
2950 * when we hit a tree root in a directory, the btrfs part of the inode
2951 * needs to be changed to reflect the root directory of the tree root. This
2952 * is kind of like crossing a mount point.
2953 */
2954static int fixup_tree_root_location(struct btrfs_root *root,
2955 struct btrfs_key *location,
Josef Bacik58176a92007-08-29 15:47:34 -04002956 struct btrfs_root **sub_root,
2957 struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04002958{
Chris Mason39279cc2007-06-12 06:35:45 -04002959 struct btrfs_root_item *ri;
2960
2961 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
2962 return 0;
2963 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
2964 return 0;
2965
Josef Bacik58176a92007-08-29 15:47:34 -04002966 *sub_root = btrfs_read_fs_root(root->fs_info, location,
2967 dentry->d_name.name,
2968 dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04002969 if (IS_ERR(*sub_root))
2970 return PTR_ERR(*sub_root);
2971
2972 ri = &(*sub_root)->root_item;
2973 location->objectid = btrfs_root_dirid(ri);
Chris Mason39279cc2007-06-12 06:35:45 -04002974 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
2975 location->offset = 0;
2976
Chris Mason39279cc2007-06-12 06:35:45 -04002977 return 0;
2978}
2979
Chris Masone02119d2008-09-05 16:13:11 -04002980static noinline void init_btrfs_i(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002981{
Chris Masone02119d2008-09-05 16:13:11 -04002982 struct btrfs_inode *bi = BTRFS_I(inode);
2983
2984 bi->i_acl = NULL;
2985 bi->i_default_acl = NULL;
2986
2987 bi->generation = 0;
Chris Masonc3027eb2008-12-08 16:40:21 -05002988 bi->sequence = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002989 bi->last_trans = 0;
2990 bi->logged_trans = 0;
2991 bi->delalloc_bytes = 0;
2992 bi->disk_i_size = 0;
2993 bi->flags = 0;
2994 bi->index_cnt = (u64)-1;
Chris Mason49eb7e42008-09-11 15:53:12 -04002995 bi->log_dirty_trans = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002996 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
2997 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db2b2007-08-27 16:49:44 -04002998 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04002999 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
3000 inode->i_mapping, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04003001 INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes);
Chris Masonba1da2f2008-07-17 12:54:15 -04003002 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Masonee6e6502008-07-17 12:54:40 -04003003 mutex_init(&BTRFS_I(inode)->extent_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04003004 mutex_init(&BTRFS_I(inode)->log_mutex);
3005}
3006
3007static int btrfs_init_locked_inode(struct inode *inode, void *p)
3008{
3009 struct btrfs_iget_args *args = p;
3010 inode->i_ino = args->ino;
3011 init_btrfs_i(inode);
3012 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003013 return 0;
3014}
3015
3016static int btrfs_find_actor(struct inode *inode, void *opaque)
3017{
3018 struct btrfs_iget_args *args = opaque;
Chris Masond3977122009-01-05 21:25:51 -05003019 return args->ino == inode->i_ino &&
3020 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003021}
3022
Zheng Yan5b21f2e2008-09-26 10:05:38 -04003023struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
3024 struct btrfs_root *root, int wait)
3025{
3026 struct inode *inode;
3027 struct btrfs_iget_args args;
3028 args.ino = objectid;
3029 args.root = root;
3030
3031 if (wait) {
3032 inode = ilookup5(s, objectid, btrfs_find_actor,
3033 (void *)&args);
3034 } else {
3035 inode = ilookup5_nowait(s, objectid, btrfs_find_actor,
3036 (void *)&args);
3037 }
3038 return inode;
3039}
3040
Chris Mason39279cc2007-06-12 06:35:45 -04003041struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
3042 struct btrfs_root *root)
3043{
3044 struct inode *inode;
3045 struct btrfs_iget_args args;
3046 args.ino = objectid;
3047 args.root = root;
3048
3049 inode = iget5_locked(s, objectid, btrfs_find_actor,
3050 btrfs_init_locked_inode,
3051 (void *)&args);
3052 return inode;
3053}
3054
Balaji Rao1a54ef82008-07-21 02:01:04 +05303055/* Get an inode object given its location and corresponding root.
3056 * Returns in *is_new if the inode was read from disk
3057 */
3058struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
3059 struct btrfs_root *root, int *is_new)
3060{
3061 struct inode *inode;
3062
3063 inode = btrfs_iget_locked(s, location->objectid, root);
3064 if (!inode)
3065 return ERR_PTR(-EACCES);
3066
3067 if (inode->i_state & I_NEW) {
3068 BTRFS_I(inode)->root = root;
3069 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
3070 btrfs_read_locked_inode(inode);
3071 unlock_new_inode(inode);
3072 if (is_new)
3073 *is_new = 1;
3074 } else {
3075 if (is_new)
3076 *is_new = 0;
3077 }
3078
3079 return inode;
3080}
3081
Chris Mason3de45862008-11-17 21:02:50 -05003082struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04003083{
Chris Masond3977122009-01-05 21:25:51 -05003084 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003085 struct btrfs_inode *bi = BTRFS_I(dir);
3086 struct btrfs_root *root = bi->root;
3087 struct btrfs_root *sub_root = root;
3088 struct btrfs_key location;
Yan Zhengc146afa2008-11-12 14:34:12 -05003089 int ret, new;
Chris Mason39279cc2007-06-12 06:35:45 -04003090
3091 if (dentry->d_name.len > BTRFS_NAME_LEN)
3092 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04003093
Chris Mason39279cc2007-06-12 06:35:45 -04003094 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04003095
Chris Mason39279cc2007-06-12 06:35:45 -04003096 if (ret < 0)
3097 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04003098
Chris Mason39279cc2007-06-12 06:35:45 -04003099 inode = NULL;
3100 if (location.objectid) {
Josef Bacik58176a92007-08-29 15:47:34 -04003101 ret = fixup_tree_root_location(root, &location, &sub_root,
3102 dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04003103 if (ret < 0)
3104 return ERR_PTR(ret);
3105 if (ret > 0)
3106 return ERR_PTR(-ENOENT);
Balaji Rao1a54ef82008-07-21 02:01:04 +05303107 inode = btrfs_iget(dir->i_sb, &location, sub_root, &new);
3108 if (IS_ERR(inode))
3109 return ERR_CAST(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003110 }
Chris Mason3de45862008-11-17 21:02:50 -05003111 return inode;
3112}
3113
3114static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
3115 struct nameidata *nd)
3116{
3117 struct inode *inode;
3118
3119 if (dentry->d_name.len > BTRFS_NAME_LEN)
3120 return ERR_PTR(-ENAMETOOLONG);
3121
3122 inode = btrfs_lookup_dentry(dir, dentry);
3123 if (IS_ERR(inode))
3124 return ERR_CAST(inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003125
Chris Mason39279cc2007-06-12 06:35:45 -04003126 return d_splice_alias(inode, dentry);
3127}
3128
Chris Mason39279cc2007-06-12 06:35:45 -04003129static unsigned char btrfs_filetype_table[] = {
3130 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
3131};
3132
David Woodhousecbdf5a22008-08-06 19:42:33 +01003133static int btrfs_real_readdir(struct file *filp, void *dirent,
3134 filldir_t filldir)
Chris Mason39279cc2007-06-12 06:35:45 -04003135{
Chris Mason6da6aba2007-12-18 16:15:09 -05003136 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003137 struct btrfs_root *root = BTRFS_I(inode)->root;
3138 struct btrfs_item *item;
3139 struct btrfs_dir_item *di;
3140 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04003141 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04003142 struct btrfs_path *path;
3143 int ret;
3144 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04003145 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003146 int slot;
3147 int advance;
3148 unsigned char d_type;
3149 int over = 0;
3150 u32 di_cur;
3151 u32 di_total;
3152 u32 di_len;
3153 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04003154 char tmp_name[32];
3155 char *name_ptr;
3156 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04003157
3158 /* FIXME, use a real flag for deciding about the key type */
3159 if (root->fs_info->tree_root == root)
3160 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04003161
Chris Mason39544012007-12-12 14:38:19 -05003162 /* special case for "." */
3163 if (filp->f_pos == 0) {
3164 over = filldir(dirent, ".", 1,
3165 1, inode->i_ino,
3166 DT_DIR);
3167 if (over)
3168 return 0;
3169 filp->f_pos = 1;
3170 }
Chris Mason39544012007-12-12 14:38:19 -05003171 /* special case for .., just use the back ref */
3172 if (filp->f_pos == 1) {
David Woodhouse5ecc7e52008-08-17 15:14:48 +01003173 u64 pino = parent_ino(filp->f_path.dentry);
Chris Mason39544012007-12-12 14:38:19 -05003174 over = filldir(dirent, "..", 2,
David Woodhouse5ecc7e52008-08-17 15:14:48 +01003175 2, pino, DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05003176 if (over)
David Woodhouse49593bf2008-08-17 17:08:36 +01003177 return 0;
Chris Mason39544012007-12-12 14:38:19 -05003178 filp->f_pos = 2;
3179 }
David Woodhouse49593bf2008-08-17 17:08:36 +01003180 path = btrfs_alloc_path();
3181 path->reada = 2;
3182
Chris Mason39279cc2007-06-12 06:35:45 -04003183 btrfs_set_key_type(&key, key_type);
3184 key.offset = filp->f_pos;
David Woodhouse49593bf2008-08-17 17:08:36 +01003185 key.objectid = inode->i_ino;
Chris Mason5f39d392007-10-15 16:14:19 -04003186
Chris Mason39279cc2007-06-12 06:35:45 -04003187 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3188 if (ret < 0)
3189 goto err;
3190 advance = 0;
David Woodhouse49593bf2008-08-17 17:08:36 +01003191
3192 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04003193 leaf = path->nodes[0];
3194 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003195 slot = path->slots[0];
3196 if (advance || slot >= nritems) {
David Woodhouse49593bf2008-08-17 17:08:36 +01003197 if (slot >= nritems - 1) {
Chris Mason39279cc2007-06-12 06:35:45 -04003198 ret = btrfs_next_leaf(root, path);
3199 if (ret)
3200 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003201 leaf = path->nodes[0];
3202 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003203 slot = path->slots[0];
3204 } else {
3205 slot++;
3206 path->slots[0]++;
3207 }
3208 }
Chris Mason3de45862008-11-17 21:02:50 -05003209
Chris Mason39279cc2007-06-12 06:35:45 -04003210 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04003211 item = btrfs_item_nr(leaf, slot);
3212 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3213
3214 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04003215 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003216 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003217 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003218 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04003219 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04003220
3221 filp->f_pos = found_key.offset;
David Woodhouse49593bf2008-08-17 17:08:36 +01003222
Chris Mason39279cc2007-06-12 06:35:45 -04003223 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
3224 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003225 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01003226
3227 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04003228 struct btrfs_key location;
3229
3230 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01003231 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04003232 name_ptr = tmp_name;
3233 } else {
3234 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01003235 if (!name_ptr) {
3236 ret = -ENOMEM;
3237 goto err;
3238 }
Chris Mason5f39d392007-10-15 16:14:19 -04003239 }
3240 read_extent_buffer(leaf, name_ptr,
3241 (unsigned long)(di + 1), name_len);
3242
3243 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
3244 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05003245
3246 /* is this a reference to our own snapshot? If so
3247 * skip it
3248 */
3249 if (location.type == BTRFS_ROOT_ITEM_KEY &&
3250 location.objectid == root->root_key.objectid) {
3251 over = 0;
3252 goto skip;
3253 }
Chris Mason5f39d392007-10-15 16:14:19 -04003254 over = filldir(dirent, name_ptr, name_len,
David Woodhouse49593bf2008-08-17 17:08:36 +01003255 found_key.offset, location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04003256 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04003257
Chris Mason3de45862008-11-17 21:02:50 -05003258skip:
Chris Mason5f39d392007-10-15 16:14:19 -04003259 if (name_ptr != tmp_name)
3260 kfree(name_ptr);
3261
Chris Mason39279cc2007-06-12 06:35:45 -04003262 if (over)
3263 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05003264 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01003265 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04003266 di_cur += di_len;
3267 di = (struct btrfs_dir_item *)((char *)di + di_len);
3268 }
3269 }
David Woodhouse49593bf2008-08-17 17:08:36 +01003270
3271 /* Reached end of directory/root. Bump pos past the last item. */
Yan Zheng5e591a02008-02-19 11:41:02 -05003272 if (key_type == BTRFS_DIR_INDEX_KEY)
3273 filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
3274 else
3275 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04003276nopos:
3277 ret = 0;
3278err:
Chris Mason39279cc2007-06-12 06:35:45 -04003279 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003280 return ret;
3281}
3282
3283int btrfs_write_inode(struct inode *inode, int wait)
3284{
3285 struct btrfs_root *root = BTRFS_I(inode)->root;
3286 struct btrfs_trans_handle *trans;
3287 int ret = 0;
3288
Yan Zhengc146afa2008-11-12 14:34:12 -05003289 if (root->fs_info->btree_inode == inode)
Chris Mason4ca8b412008-08-05 13:30:48 -04003290 return 0;
3291
Chris Mason39279cc2007-06-12 06:35:45 -04003292 if (wait) {
Chris Masonf9295742008-07-17 12:54:14 -04003293 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003294 btrfs_set_trans_block_group(trans, inode);
3295 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04003296 }
3297 return ret;
3298}
3299
3300/*
Chris Mason54aa1f42007-06-22 14:16:25 -04003301 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04003302 * inode changes. But, it is most likely to find the inode in cache.
3303 * FIXME, needs more benchmarking...there are no reasons other than performance
3304 * to keep or drop this code.
3305 */
3306void btrfs_dirty_inode(struct inode *inode)
3307{
3308 struct btrfs_root *root = BTRFS_I(inode)->root;
3309 struct btrfs_trans_handle *trans;
3310
Chris Masonf9295742008-07-17 12:54:14 -04003311 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003312 btrfs_set_trans_block_group(trans, inode);
3313 btrfs_update_inode(trans, root, inode);
3314 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04003315}
3316
Chris Masond352ac62008-09-29 15:18:18 -04003317/*
3318 * find the highest existing sequence number in a directory
3319 * and then set the in-memory index_cnt variable to reflect
3320 * free sequence numbers
3321 */
Josef Bacikaec74772008-07-24 12:12:38 -04003322static int btrfs_set_inode_index_count(struct inode *inode)
3323{
3324 struct btrfs_root *root = BTRFS_I(inode)->root;
3325 struct btrfs_key key, found_key;
3326 struct btrfs_path *path;
3327 struct extent_buffer *leaf;
3328 int ret;
3329
3330 key.objectid = inode->i_ino;
3331 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
3332 key.offset = (u64)-1;
3333
3334 path = btrfs_alloc_path();
3335 if (!path)
3336 return -ENOMEM;
3337
3338 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3339 if (ret < 0)
3340 goto out;
3341 /* FIXME: we should be able to handle this */
3342 if (ret == 0)
3343 goto out;
3344 ret = 0;
3345
3346 /*
3347 * MAGIC NUMBER EXPLANATION:
3348 * since we search a directory based on f_pos we have to start at 2
3349 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
3350 * else has to start at 2
3351 */
3352 if (path->slots[0] == 0) {
3353 BTRFS_I(inode)->index_cnt = 2;
3354 goto out;
3355 }
3356
3357 path->slots[0]--;
3358
3359 leaf = path->nodes[0];
3360 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3361
3362 if (found_key.objectid != inode->i_ino ||
3363 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
3364 BTRFS_I(inode)->index_cnt = 2;
3365 goto out;
3366 }
3367
3368 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
3369out:
3370 btrfs_free_path(path);
3371 return ret;
3372}
3373
Chris Masond352ac62008-09-29 15:18:18 -04003374/*
3375 * helper to find a free sequence number in a given directory. This current
3376 * code is very simple, later versions will do smarter things in the btree
3377 */
Chris Mason3de45862008-11-17 21:02:50 -05003378int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04003379{
3380 int ret = 0;
3381
3382 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
3383 ret = btrfs_set_inode_index_count(dir);
Chris Masond3977122009-01-05 21:25:51 -05003384 if (ret)
Josef Bacikaec74772008-07-24 12:12:38 -04003385 return ret;
3386 }
3387
Chris Mason00e4e6b2008-08-05 11:18:09 -04003388 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04003389 BTRFS_I(dir)->index_cnt++;
3390
3391 return ret;
3392}
3393
Chris Mason39279cc2007-06-12 06:35:45 -04003394static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
3395 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04003396 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05003397 const char *name, int name_len,
Yan Zhengd2fb3432008-12-11 16:30:39 -05003398 u64 ref_objectid, u64 objectid,
3399 u64 alloc_hint, int mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04003400{
3401 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04003402 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04003403 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04003404 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05003405 struct btrfs_inode_ref *ref;
3406 struct btrfs_key key[2];
3407 u32 sizes[2];
3408 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04003409 int ret;
3410 int owner;
3411
Chris Mason5f39d392007-10-15 16:14:19 -04003412 path = btrfs_alloc_path();
3413 BUG_ON(!path);
3414
Chris Mason39279cc2007-06-12 06:35:45 -04003415 inode = new_inode(root->fs_info->sb);
3416 if (!inode)
3417 return ERR_PTR(-ENOMEM);
3418
Josef Bacikaec74772008-07-24 12:12:38 -04003419 if (dir) {
Chris Mason3de45862008-11-17 21:02:50 -05003420 ret = btrfs_set_inode_index(dir, index);
Josef Bacikaec74772008-07-24 12:12:38 -04003421 if (ret)
3422 return ERR_PTR(ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003423 }
3424 /*
3425 * index_cnt is ignored for everything but a dir,
3426 * btrfs_get_inode_index_count has an explanation for the magic
3427 * number
3428 */
Chris Masone02119d2008-09-05 16:13:11 -04003429 init_btrfs_i(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04003430 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04003431 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04003432 BTRFS_I(inode)->generation = trans->transid;
Chris Masonb888db2b2007-08-27 16:49:44 -04003433
Chris Mason39279cc2007-06-12 06:35:45 -04003434 if (mode & S_IFDIR)
3435 owner = 0;
3436 else
3437 owner = 1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003438 BTRFS_I(inode)->block_group =
3439 btrfs_find_block_group(root, 0, alloc_hint, owner);
Yan Zheng17d217f2008-12-12 10:03:38 -05003440 if ((mode & S_IFREG)) {
3441 if (btrfs_test_opt(root, NODATASUM))
3442 btrfs_set_flag(inode, NODATASUM);
3443 if (btrfs_test_opt(root, NODATACOW))
3444 btrfs_set_flag(inode, NODATACOW);
3445 }
Chris Mason9c583092008-01-29 15:15:18 -05003446
3447 key[0].objectid = objectid;
3448 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
3449 key[0].offset = 0;
3450
3451 key[1].objectid = objectid;
3452 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
3453 key[1].offset = ref_objectid;
3454
3455 sizes[0] = sizeof(struct btrfs_inode_item);
3456 sizes[1] = name_len + sizeof(*ref);
3457
3458 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
3459 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04003460 goto fail;
3461
Chris Mason9c583092008-01-29 15:15:18 -05003462 if (objectid > root->highest_inode)
3463 root->highest_inode = objectid;
3464
Chris Mason79683f22008-11-19 22:00:53 -05003465 inode->i_uid = current_fsuid();
3466 inode->i_gid = current_fsgid();
Chris Mason39279cc2007-06-12 06:35:45 -04003467 inode->i_mode = mode;
3468 inode->i_ino = objectid;
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003469 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003470 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04003471 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3472 struct btrfs_inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003473 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05003474
3475 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
3476 struct btrfs_inode_ref);
3477 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04003478 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05003479 ptr = (unsigned long)(ref + 1);
3480 write_extent_buffer(path->nodes[0], name, ptr, name_len);
3481
Chris Mason5f39d392007-10-15 16:14:19 -04003482 btrfs_mark_buffer_dirty(path->nodes[0]);
3483 btrfs_free_path(path);
3484
Chris Mason39279cc2007-06-12 06:35:45 -04003485 location = &BTRFS_I(inode)->location;
3486 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04003487 location->offset = 0;
3488 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
3489
Chris Mason39279cc2007-06-12 06:35:45 -04003490 insert_inode_hash(inode);
3491 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04003492fail:
Josef Bacikaec74772008-07-24 12:12:38 -04003493 if (dir)
3494 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04003495 btrfs_free_path(path);
3496 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003497}
3498
3499static inline u8 btrfs_inode_type(struct inode *inode)
3500{
3501 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
3502}
3503
Chris Masond352ac62008-09-29 15:18:18 -04003504/*
3505 * utility function to add 'inode' into 'parent_inode' with
3506 * a give name and a given sequence number.
3507 * if 'add_backref' is true, also insert a backref from the
3508 * inode to the parent directory.
3509 */
Chris Masone02119d2008-09-05 16:13:11 -04003510int btrfs_add_link(struct btrfs_trans_handle *trans,
3511 struct inode *parent_inode, struct inode *inode,
3512 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04003513{
3514 int ret;
3515 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04003516 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Chris Mason5f39d392007-10-15 16:14:19 -04003517
Chris Mason39279cc2007-06-12 06:35:45 -04003518 key.objectid = inode->i_ino;
Chris Mason39279cc2007-06-12 06:35:45 -04003519 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
3520 key.offset = 0;
3521
Chris Masone02119d2008-09-05 16:13:11 -04003522 ret = btrfs_insert_dir_item(trans, root, name, name_len,
3523 parent_inode->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04003524 &key, btrfs_inode_type(inode),
Chris Mason00e4e6b2008-08-05 11:18:09 -04003525 index);
Chris Mason39279cc2007-06-12 06:35:45 -04003526 if (ret == 0) {
Chris Mason9c583092008-01-29 15:15:18 -05003527 if (add_backref) {
3528 ret = btrfs_insert_inode_ref(trans, root,
Chris Masone02119d2008-09-05 16:13:11 -04003529 name, name_len,
3530 inode->i_ino,
3531 parent_inode->i_ino,
3532 index);
Chris Mason9c583092008-01-29 15:15:18 -05003533 }
Chris Masondbe674a2008-07-17 12:54:05 -04003534 btrfs_i_size_write(parent_inode, parent_inode->i_size +
Chris Masone02119d2008-09-05 16:13:11 -04003535 name_len * 2);
Chris Mason79c44582007-06-25 10:09:33 -04003536 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Masone02119d2008-09-05 16:13:11 -04003537 ret = btrfs_update_inode(trans, root, parent_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003538 }
3539 return ret;
3540}
3541
3542static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05003543 struct dentry *dentry, struct inode *inode,
Chris Mason00e4e6b2008-08-05 11:18:09 -04003544 int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04003545{
Chris Masone02119d2008-09-05 16:13:11 -04003546 int err = btrfs_add_link(trans, dentry->d_parent->d_inode,
3547 inode, dentry->d_name.name,
3548 dentry->d_name.len, backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04003549 if (!err) {
3550 d_instantiate(dentry, inode);
3551 return 0;
3552 }
3553 if (err > 0)
3554 err = -EEXIST;
3555 return err;
3556}
3557
Josef Bacik618e21d2007-07-11 10:18:17 -04003558static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
3559 int mode, dev_t rdev)
3560{
3561 struct btrfs_trans_handle *trans;
3562 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05003563 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04003564 int err;
3565 int drop_inode = 0;
3566 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05003567 unsigned long nr = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04003568 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04003569
3570 if (!new_valid_dev(rdev))
3571 return -EINVAL;
3572
Chris Mason1832a6d2007-12-21 16:27:21 -05003573 err = btrfs_check_free_space(root, 1, 0);
3574 if (err)
3575 goto fail;
3576
Josef Bacik618e21d2007-07-11 10:18:17 -04003577 trans = btrfs_start_transaction(root, 1);
3578 btrfs_set_trans_block_group(trans, dir);
3579
3580 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3581 if (err) {
3582 err = -ENOSPC;
3583 goto out_unlock;
3584 }
3585
Josef Bacikaec74772008-07-24 12:12:38 -04003586 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05003587 dentry->d_name.len,
3588 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04003589 BTRFS_I(dir)->block_group, mode, &index);
Josef Bacik618e21d2007-07-11 10:18:17 -04003590 err = PTR_ERR(inode);
3591 if (IS_ERR(inode))
3592 goto out_unlock;
3593
Josef Bacik33268ea2008-07-24 12:16:36 -04003594 err = btrfs_init_acl(inode, dir);
3595 if (err) {
3596 drop_inode = 1;
3597 goto out_unlock;
3598 }
3599
Josef Bacik618e21d2007-07-11 10:18:17 -04003600 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04003601 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04003602 if (err)
3603 drop_inode = 1;
3604 else {
3605 inode->i_op = &btrfs_special_inode_operations;
3606 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04003607 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04003608 }
3609 dir->i_sb->s_dirt = 1;
3610 btrfs_update_inode_block_group(trans, inode);
3611 btrfs_update_inode_block_group(trans, dir);
3612out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003613 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04003614 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003615fail:
Josef Bacik618e21d2007-07-11 10:18:17 -04003616 if (drop_inode) {
3617 inode_dec_link_count(inode);
3618 iput(inode);
3619 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003620 btrfs_btree_balance_dirty(root, nr);
Josef Bacik618e21d2007-07-11 10:18:17 -04003621 return err;
3622}
3623
Chris Mason39279cc2007-06-12 06:35:45 -04003624static int btrfs_create(struct inode *dir, struct dentry *dentry,
3625 int mode, struct nameidata *nd)
3626{
3627 struct btrfs_trans_handle *trans;
3628 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05003629 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04003630 int err;
3631 int drop_inode = 0;
Chris Mason1832a6d2007-12-21 16:27:21 -05003632 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003633 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04003634 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003635
Chris Mason1832a6d2007-12-21 16:27:21 -05003636 err = btrfs_check_free_space(root, 1, 0);
3637 if (err)
3638 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04003639 trans = btrfs_start_transaction(root, 1);
3640 btrfs_set_trans_block_group(trans, dir);
3641
3642 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3643 if (err) {
3644 err = -ENOSPC;
3645 goto out_unlock;
3646 }
3647
Josef Bacikaec74772008-07-24 12:12:38 -04003648 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05003649 dentry->d_name.len,
3650 dentry->d_parent->d_inode->i_ino,
Chris Mason00e4e6b2008-08-05 11:18:09 -04003651 objectid, BTRFS_I(dir)->block_group, mode,
3652 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04003653 err = PTR_ERR(inode);
3654 if (IS_ERR(inode))
3655 goto out_unlock;
3656
Josef Bacik33268ea2008-07-24 12:16:36 -04003657 err = btrfs_init_acl(inode, dir);
3658 if (err) {
3659 drop_inode = 1;
3660 goto out_unlock;
3661 }
3662
Chris Mason39279cc2007-06-12 06:35:45 -04003663 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04003664 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04003665 if (err)
3666 drop_inode = 1;
3667 else {
3668 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003669 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003670 inode->i_fop = &btrfs_file_operations;
3671 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05003672 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003673 }
3674 dir->i_sb->s_dirt = 1;
3675 btrfs_update_inode_block_group(trans, inode);
3676 btrfs_update_inode_block_group(trans, dir);
3677out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003678 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04003679 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003680fail:
Chris Mason39279cc2007-06-12 06:35:45 -04003681 if (drop_inode) {
3682 inode_dec_link_count(inode);
3683 iput(inode);
3684 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003685 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003686 return err;
3687}
3688
3689static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
3690 struct dentry *dentry)
3691{
3692 struct btrfs_trans_handle *trans;
3693 struct btrfs_root *root = BTRFS_I(dir)->root;
3694 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04003695 u64 index;
Chris Mason1832a6d2007-12-21 16:27:21 -05003696 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003697 int err;
3698 int drop_inode = 0;
3699
3700 if (inode->i_nlink == 0)
3701 return -ENOENT;
3702
Chris Masone02119d2008-09-05 16:13:11 -04003703 btrfs_inc_nlink(inode);
Chris Mason1832a6d2007-12-21 16:27:21 -05003704 err = btrfs_check_free_space(root, 1, 0);
3705 if (err)
3706 goto fail;
Chris Mason3de45862008-11-17 21:02:50 -05003707 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003708 if (err)
3709 goto fail;
3710
Chris Mason39279cc2007-06-12 06:35:45 -04003711 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003712
Chris Mason39279cc2007-06-12 06:35:45 -04003713 btrfs_set_trans_block_group(trans, dir);
3714 atomic_inc(&inode->i_count);
Josef Bacikaec74772008-07-24 12:12:38 -04003715
Chris Mason00e4e6b2008-08-05 11:18:09 -04003716 err = btrfs_add_nondir(trans, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04003717
Chris Mason39279cc2007-06-12 06:35:45 -04003718 if (err)
3719 drop_inode = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04003720
Chris Mason39279cc2007-06-12 06:35:45 -04003721 dir->i_sb->s_dirt = 1;
3722 btrfs_update_inode_block_group(trans, dir);
Chris Mason54aa1f42007-06-22 14:16:25 -04003723 err = btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003724
Chris Mason54aa1f42007-06-22 14:16:25 -04003725 if (err)
3726 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003727
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003728 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04003729 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003730fail:
Chris Mason39279cc2007-06-12 06:35:45 -04003731 if (drop_inode) {
3732 inode_dec_link_count(inode);
3733 iput(inode);
3734 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003735 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003736 return err;
3737}
3738
Chris Mason39279cc2007-06-12 06:35:45 -04003739static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
3740{
Chris Masonb9d86662008-05-02 16:13:49 -04003741 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04003742 struct btrfs_trans_handle *trans;
3743 struct btrfs_root *root = BTRFS_I(dir)->root;
3744 int err = 0;
3745 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04003746 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04003747 u64 index = 0;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003748 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003749
Chris Mason1832a6d2007-12-21 16:27:21 -05003750 err = btrfs_check_free_space(root, 1, 0);
3751 if (err)
3752 goto out_unlock;
3753
Chris Mason39279cc2007-06-12 06:35:45 -04003754 trans = btrfs_start_transaction(root, 1);
3755 btrfs_set_trans_block_group(trans, dir);
Chris Mason5f39d392007-10-15 16:14:19 -04003756
Chris Mason39279cc2007-06-12 06:35:45 -04003757 if (IS_ERR(trans)) {
3758 err = PTR_ERR(trans);
3759 goto out_unlock;
3760 }
3761
3762 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3763 if (err) {
3764 err = -ENOSPC;
3765 goto out_unlock;
3766 }
3767
Josef Bacikaec74772008-07-24 12:12:38 -04003768 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05003769 dentry->d_name.len,
3770 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04003771 BTRFS_I(dir)->block_group, S_IFDIR | mode,
3772 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04003773 if (IS_ERR(inode)) {
3774 err = PTR_ERR(inode);
3775 goto out_fail;
3776 }
Chris Mason5f39d392007-10-15 16:14:19 -04003777
Chris Mason39279cc2007-06-12 06:35:45 -04003778 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04003779
3780 err = btrfs_init_acl(inode, dir);
3781 if (err)
3782 goto out_fail;
3783
Chris Mason39279cc2007-06-12 06:35:45 -04003784 inode->i_op = &btrfs_dir_inode_operations;
3785 inode->i_fop = &btrfs_dir_file_operations;
3786 btrfs_set_trans_block_group(trans, inode);
3787
Chris Masondbe674a2008-07-17 12:54:05 -04003788 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003789 err = btrfs_update_inode(trans, root, inode);
3790 if (err)
3791 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04003792
Chris Masone02119d2008-09-05 16:13:11 -04003793 err = btrfs_add_link(trans, dentry->d_parent->d_inode,
3794 inode, dentry->d_name.name,
3795 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04003796 if (err)
3797 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04003798
Chris Mason39279cc2007-06-12 06:35:45 -04003799 d_instantiate(dentry, inode);
3800 drop_on_err = 0;
3801 dir->i_sb->s_dirt = 1;
3802 btrfs_update_inode_block_group(trans, inode);
3803 btrfs_update_inode_block_group(trans, dir);
3804
3805out_fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003806 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04003807 btrfs_end_transaction_throttle(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04003808
Chris Mason39279cc2007-06-12 06:35:45 -04003809out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04003810 if (drop_on_err)
3811 iput(inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003812 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003813 return err;
3814}
3815
Chris Masond352ac62008-09-29 15:18:18 -04003816/* helper for btfs_get_extent. Given an existing extent in the tree,
3817 * and an extent that you want to insert, deal with overlap and insert
3818 * the new extent into the tree.
3819 */
Chris Mason3b951512008-04-17 11:29:12 -04003820static int merge_extent_mapping(struct extent_map_tree *em_tree,
3821 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003822 struct extent_map *em,
3823 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04003824{
3825 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04003826
Chris Masone6dcd2d2008-07-17 12:53:50 -04003827 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
3828 start_diff = map_start - em->start;
3829 em->start = map_start;
3830 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04003831 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
3832 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04003833 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04003834 em->block_len -= start_diff;
3835 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003836 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04003837}
3838
Chris Masonc8b97812008-10-29 14:49:59 -04003839static noinline int uncompress_inline(struct btrfs_path *path,
3840 struct inode *inode, struct page *page,
3841 size_t pg_offset, u64 extent_offset,
3842 struct btrfs_file_extent_item *item)
3843{
3844 int ret;
3845 struct extent_buffer *leaf = path->nodes[0];
3846 char *tmp;
3847 size_t max_size;
3848 unsigned long inline_size;
3849 unsigned long ptr;
3850
3851 WARN_ON(pg_offset != 0);
3852 max_size = btrfs_file_extent_ram_bytes(leaf, item);
3853 inline_size = btrfs_file_extent_inline_item_len(leaf,
3854 btrfs_item_nr(leaf, path->slots[0]));
3855 tmp = kmalloc(inline_size, GFP_NOFS);
3856 ptr = btrfs_file_extent_inline_start(item);
3857
3858 read_extent_buffer(leaf, tmp, ptr, inline_size);
3859
Chris Mason5b050f02008-11-11 09:34:41 -05003860 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04003861 ret = btrfs_zlib_decompress(tmp, page, extent_offset,
3862 inline_size, max_size);
3863 if (ret) {
3864 char *kaddr = kmap_atomic(page, KM_USER0);
3865 unsigned long copy_size = min_t(u64,
3866 PAGE_CACHE_SIZE - pg_offset,
3867 max_size - extent_offset);
3868 memset(kaddr + pg_offset, 0, copy_size);
3869 kunmap_atomic(kaddr, KM_USER0);
3870 }
3871 kfree(tmp);
3872 return 0;
3873}
3874
Chris Masond352ac62008-09-29 15:18:18 -04003875/*
3876 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05003877 * the ugly parts come from merging extents from the disk with the in-ram
3878 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04003879 * where the in-ram extents might be locked pending data=ordered completion.
3880 *
3881 * This also copies inline extents directly into the page.
3882 */
Chris Masond3977122009-01-05 21:25:51 -05003883
Chris Masona52d9a82007-08-27 16:49:44 -04003884struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05003885 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04003886 int create)
3887{
3888 int ret;
3889 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04003890 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04003891 u64 extent_start = 0;
3892 u64 extent_end = 0;
3893 u64 objectid = inode->i_ino;
3894 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04003895 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04003896 struct btrfs_root *root = BTRFS_I(inode)->root;
3897 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04003898 struct extent_buffer *leaf;
3899 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04003900 struct extent_map *em = NULL;
3901 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05003902 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04003903 struct btrfs_trans_handle *trans = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04003904 int compressed;
Chris Masona52d9a82007-08-27 16:49:44 -04003905
Chris Masona52d9a82007-08-27 16:49:44 -04003906again:
Chris Masond1310b22008-01-24 16:13:08 -05003907 spin_lock(&em_tree->lock);
3908 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04003909 if (em)
3910 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05003911 spin_unlock(&em_tree->lock);
3912
Chris Masona52d9a82007-08-27 16:49:44 -04003913 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04003914 if (em->start > start || em->start + em->len <= start)
3915 free_extent_map(em);
3916 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05003917 free_extent_map(em);
3918 else
3919 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04003920 }
Chris Masond1310b22008-01-24 16:13:08 -05003921 em = alloc_extent_map(GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04003922 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05003923 err = -ENOMEM;
3924 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04003925 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003926 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05003927 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05003928 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05003929 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04003930 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04003931
3932 if (!path) {
3933 path = btrfs_alloc_path();
3934 BUG_ON(!path);
3935 }
3936
Chris Mason179e29e2007-11-01 11:28:41 -04003937 ret = btrfs_lookup_file_extent(trans, root, path,
3938 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04003939 if (ret < 0) {
3940 err = ret;
3941 goto out;
3942 }
3943
3944 if (ret != 0) {
3945 if (path->slots[0] == 0)
3946 goto not_found;
3947 path->slots[0]--;
3948 }
3949
Chris Mason5f39d392007-10-15 16:14:19 -04003950 leaf = path->nodes[0];
3951 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04003952 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04003953 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04003954 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3955 found_type = btrfs_key_type(&found_key);
3956 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04003957 found_type != BTRFS_EXTENT_DATA_KEY) {
3958 goto not_found;
3959 }
3960
Chris Mason5f39d392007-10-15 16:14:19 -04003961 found_type = btrfs_file_extent_type(leaf, item);
3962 extent_start = found_key.offset;
Chris Masonc8b97812008-10-29 14:49:59 -04003963 compressed = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04003964 if (found_type == BTRFS_FILE_EXTENT_REG ||
3965 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04003966 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04003967 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04003968 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
3969 size_t size;
3970 size = btrfs_file_extent_inline_len(leaf, item);
3971 extent_end = (extent_start + size + root->sectorsize - 1) &
3972 ~((u64)root->sectorsize - 1);
3973 }
3974
3975 if (start >= extent_end) {
3976 path->slots[0]++;
3977 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
3978 ret = btrfs_next_leaf(root, path);
3979 if (ret < 0) {
3980 err = ret;
3981 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04003982 }
Yan Zheng9036c102008-10-30 14:19:41 -04003983 if (ret > 0)
3984 goto not_found;
3985 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04003986 }
Yan Zheng9036c102008-10-30 14:19:41 -04003987 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3988 if (found_key.objectid != objectid ||
3989 found_key.type != BTRFS_EXTENT_DATA_KEY)
3990 goto not_found;
3991 if (start + len <= found_key.offset)
3992 goto not_found;
3993 em->start = start;
3994 em->len = found_key.offset - start;
3995 goto not_found_em;
3996 }
3997
Yan Zhengd899e052008-10-30 14:25:28 -04003998 if (found_type == BTRFS_FILE_EXTENT_REG ||
3999 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04004000 em->start = extent_start;
4001 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05004002 em->orig_start = extent_start -
4003 btrfs_file_extent_offset(leaf, item);
Chris Masondb945352007-10-15 16:15:53 -04004004 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
4005 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004006 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04004007 goto insert;
4008 }
Chris Masonc8b97812008-10-29 14:49:59 -04004009 if (compressed) {
4010 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
4011 em->block_start = bytenr;
4012 em->block_len = btrfs_file_extent_disk_num_bytes(leaf,
4013 item);
4014 } else {
4015 bytenr += btrfs_file_extent_offset(leaf, item);
4016 em->block_start = bytenr;
4017 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04004018 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
4019 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04004020 }
Chris Masona52d9a82007-08-27 16:49:44 -04004021 goto insert;
4022 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004023 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04004024 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04004025 size_t size;
4026 size_t extent_offset;
4027 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04004028
Chris Masona52d9a82007-08-27 16:49:44 -04004029 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04004030 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04004031 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04004032 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04004033 goto out;
4034 }
4035
Yan Zheng9036c102008-10-30 14:19:41 -04004036 size = btrfs_file_extent_inline_len(leaf, item);
4037 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05004038 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04004039 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04004040 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05004041 em->len = (copy_size + root->sectorsize - 1) &
4042 ~((u64)root->sectorsize - 1);
Yan Zhengff5b7ee2008-11-10 07:34:43 -05004043 em->orig_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04004044 if (compressed)
4045 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Yan689f9342007-10-29 11:41:07 -04004046 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04004047 if (create == 0 && !PageUptodate(page)) {
Chris Masonc8b97812008-10-29 14:49:59 -04004048 if (btrfs_file_extent_compression(leaf, item) ==
4049 BTRFS_COMPRESS_ZLIB) {
4050 ret = uncompress_inline(path, inode, page,
4051 pg_offset,
4052 extent_offset, item);
4053 BUG_ON(ret);
4054 } else {
4055 map = kmap(page);
4056 read_extent_buffer(leaf, map + pg_offset, ptr,
4057 copy_size);
4058 kunmap(page);
4059 }
Chris Mason179e29e2007-11-01 11:28:41 -04004060 flush_dcache_page(page);
4061 } else if (create && PageUptodate(page)) {
4062 if (!trans) {
4063 kunmap(page);
4064 free_extent_map(em);
4065 em = NULL;
4066 btrfs_release_path(root, path);
Chris Masonf9295742008-07-17 12:54:14 -04004067 trans = btrfs_join_transaction(root, 1);
Chris Mason179e29e2007-11-01 11:28:41 -04004068 goto again;
4069 }
Chris Masonc8b97812008-10-29 14:49:59 -04004070 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05004071 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04004072 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04004073 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04004074 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04004075 }
Chris Masond1310b22008-01-24 16:13:08 -05004076 set_extent_uptodate(io_tree, em->start,
4077 extent_map_end(em) - 1, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04004078 goto insert;
4079 } else {
Chris Masond3977122009-01-05 21:25:51 -05004080 printk(KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04004081 WARN_ON(1);
4082 }
4083not_found:
4084 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05004085 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04004086not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04004087 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04004088 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04004089insert:
4090 btrfs_release_path(root, path);
Chris Masond1310b22008-01-24 16:13:08 -05004091 if (em->start > start || extent_map_end(em) <= start) {
Chris Masond3977122009-01-05 21:25:51 -05004092 printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
4093 "[%llu %llu]\n", (unsigned long long)em->start,
4094 (unsigned long long)em->len,
4095 (unsigned long long)start,
4096 (unsigned long long)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;
Chris Mason3b951512008-04-17 11:29:12 -04004133 free_extent_map(em);
4134 em = NULL;
4135 }
4136 } else {
4137 free_extent_map(em);
4138 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004139 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04004140 }
Chris Masona52d9a82007-08-27 16:49:44 -04004141 }
Chris Masond1310b22008-01-24 16:13:08 -05004142 spin_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04004143out:
Chris Masonf4219502008-07-22 11:18:09 -04004144 if (path)
4145 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04004146 if (trans) {
4147 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05004148 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04004149 err = ret;
4150 }
Chris Masona52d9a82007-08-27 16:49:44 -04004151 if (err) {
4152 free_extent_map(em);
4153 WARN_ON(1);
4154 return ERR_PTR(err);
4155 }
4156 return em;
4157}
4158
Chris Mason16432982008-04-10 10:23:21 -04004159static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
4160 const struct iovec *iov, loff_t offset,
4161 unsigned long nr_segs)
4162{
Chris Masone1c4b742008-04-22 13:26:46 -04004163 return -EINVAL;
Chris Mason16432982008-04-10 10:23:21 -04004164}
4165
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04004166static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
Chris Mason39279cc2007-06-12 06:35:45 -04004167{
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04004168 return extent_bmap(mapping, iblock, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04004169}
4170
Chris Mason9ebefb182007-06-15 13:50:00 -04004171int btrfs_readpage(struct file *file, struct page *page)
4172{
Chris Masond1310b22008-01-24 16:13:08 -05004173 struct extent_io_tree *tree;
4174 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04004175 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04004176}
Chris Mason1832a6d2007-12-21 16:27:21 -05004177
Chris Mason39279cc2007-06-12 06:35:45 -04004178static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
4179{
Chris Masond1310b22008-01-24 16:13:08 -05004180 struct extent_io_tree *tree;
Chris Masonb888db2b2007-08-27 16:49:44 -04004181
4182
4183 if (current->flags & PF_MEMALLOC) {
4184 redirty_page_for_writepage(wbc, page);
4185 unlock_page(page);
4186 return 0;
4187 }
Chris Masond1310b22008-01-24 16:13:08 -05004188 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04004189 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
4190}
Chris Mason39279cc2007-06-12 06:35:45 -04004191
Chris Masonf4219502008-07-22 11:18:09 -04004192int btrfs_writepages(struct address_space *mapping,
4193 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04004194{
Chris Masond1310b22008-01-24 16:13:08 -05004195 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05004196
Chris Masond1310b22008-01-24 16:13:08 -05004197 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f02e2007-11-01 19:45:34 -04004198 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
4199}
4200
Chris Mason3ab2fb52007-11-08 10:59:22 -05004201static int
4202btrfs_readpages(struct file *file, struct address_space *mapping,
4203 struct list_head *pages, unsigned nr_pages)
4204{
Chris Masond1310b22008-01-24 16:13:08 -05004205 struct extent_io_tree *tree;
4206 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05004207 return extent_readpages(tree, mapping, pages, nr_pages,
4208 btrfs_get_extent);
4209}
Chris Masone6dcd2d2008-07-17 12:53:50 -04004210static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04004211{
Chris Masond1310b22008-01-24 16:13:08 -05004212 struct extent_io_tree *tree;
4213 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04004214 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004215
Chris Masond1310b22008-01-24 16:13:08 -05004216 tree = &BTRFS_I(page->mapping->host)->io_tree;
4217 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05004218 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04004219 if (ret == 1) {
4220 ClearPagePrivate(page);
4221 set_page_private(page, 0);
4222 page_cache_release(page);
4223 }
4224 return ret;
4225}
Chris Mason39279cc2007-06-12 06:35:45 -04004226
Chris Masone6dcd2d2008-07-17 12:53:50 -04004227static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
4228{
Chris Mason98509cf2008-09-11 15:51:43 -04004229 if (PageWriteback(page) || PageDirty(page))
4230 return 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004231 return __btrfs_releasepage(page, gfp_flags);
4232}
4233
Chris Masona52d9a82007-08-27 16:49:44 -04004234static void btrfs_invalidatepage(struct page *page, unsigned long offset)
4235{
Chris Masond1310b22008-01-24 16:13:08 -05004236 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004237 struct btrfs_ordered_extent *ordered;
4238 u64 page_start = page_offset(page);
4239 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04004240
Chris Masone6dcd2d2008-07-17 12:53:50 -04004241 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05004242 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004243 if (offset) {
4244 btrfs_releasepage(page, GFP_NOFS);
4245 return;
4246 }
4247
4248 lock_extent(tree, page_start, page_end, GFP_NOFS);
4249 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
4250 page_offset(page));
4251 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04004252 /*
4253 * IO on this page will never be started, so we need
4254 * to account for any ordered extents now
4255 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04004256 clear_extent_bit(tree, page_start, page_end,
4257 EXTENT_DIRTY | EXTENT_DELALLOC |
4258 EXTENT_LOCKED, 1, 0, GFP_NOFS);
Chris Mason211f90e2008-07-18 11:56:15 -04004259 btrfs_finish_ordered_io(page->mapping->host,
4260 page_start, page_end);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004261 btrfs_put_ordered_extent(ordered);
4262 lock_extent(tree, page_start, page_end, GFP_NOFS);
4263 }
4264 clear_extent_bit(tree, page_start, page_end,
4265 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
4266 EXTENT_ORDERED,
4267 1, 1, GFP_NOFS);
4268 __btrfs_releasepage(page, GFP_NOFS);
4269
Chris Mason4a096752008-07-21 10:29:44 -04004270 ClearPageChecked(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04004271 if (PagePrivate(page)) {
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04004272 ClearPagePrivate(page);
4273 set_page_private(page, 0);
4274 page_cache_release(page);
4275 }
Chris Mason39279cc2007-06-12 06:35:45 -04004276}
4277
Chris Mason9ebefb182007-06-15 13:50:00 -04004278/*
4279 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
4280 * called from a page fault handler when a page is first dirtied. Hence we must
4281 * be careful to check for EOF conditions here. We set the page up correctly
4282 * for a written page which means we get ENOSPC checking when writing into
4283 * holes and correct delalloc and unwritten extent mapping on filesystems that
4284 * support these features.
4285 *
4286 * We are not allowed to take the i_mutex here so we have to play games to
4287 * protect against truncate races as the page could now be beyond EOF. Because
4288 * vmtruncate() writes the inode size before removing pages, once we have the
4289 * page lock we can determine safely if the page is beyond EOF. If it is not
4290 * beyond EOF, then the page is guaranteed safe against truncation until we
4291 * unlock the page.
4292 */
4293int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
4294{
Chris Mason6da6aba2007-12-18 16:15:09 -05004295 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05004296 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004297 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4298 struct btrfs_ordered_extent *ordered;
4299 char *kaddr;
4300 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04004301 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05004302 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04004303 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004304 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04004305
Chris Mason1832a6d2007-12-21 16:27:21 -05004306 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
Chris Mason1832a6d2007-12-21 16:27:21 -05004307 if (ret)
4308 goto out;
4309
4310 ret = -EINVAL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004311again:
Chris Mason9ebefb182007-06-15 13:50:00 -04004312 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04004313 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004314 page_start = page_offset(page);
4315 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04004316
Chris Mason9ebefb182007-06-15 13:50:00 -04004317 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04004318 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04004319 /* page got truncated out from underneath us */
4320 goto out_unlock;
4321 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004322 wait_on_page_writeback(page);
4323
4324 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
4325 set_page_extent_mapped(page);
4326
Chris Masoneb84ae02008-07-17 13:53:27 -04004327 /*
4328 * we can't set the delalloc bits if there are pending ordered
4329 * extents. Drop our locks and wait for them to finish
4330 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04004331 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4332 if (ordered) {
4333 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
4334 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004335 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004336 btrfs_put_ordered_extent(ordered);
4337 goto again;
4338 }
4339
Chris Masonea8c2812008-08-04 23:17:27 -04004340 btrfs_set_extent_delalloc(inode, page_start, page_end);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004341 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04004342
4343 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04004344 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04004345 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04004346 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04004347 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04004348
Chris Masone6dcd2d2008-07-17 12:53:50 -04004349 if (zero_start != PAGE_CACHE_SIZE) {
4350 kaddr = kmap(page);
4351 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
4352 flush_dcache_page(page);
4353 kunmap(page);
4354 }
Chris Mason247e7432008-07-17 12:53:51 -04004355 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004356 set_page_dirty(page);
4357 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04004358
4359out_unlock:
4360 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05004361out:
Chris Mason9ebefb182007-06-15 13:50:00 -04004362 return ret;
4363}
4364
Chris Mason39279cc2007-06-12 06:35:45 -04004365static void btrfs_truncate(struct inode *inode)
4366{
4367 struct btrfs_root *root = BTRFS_I(inode)->root;
4368 int ret;
4369 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004370 unsigned long nr;
Chris Masondbe674a2008-07-17 12:54:05 -04004371 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04004372
4373 if (!S_ISREG(inode->i_mode))
4374 return;
4375 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
4376 return;
4377
4378 btrfs_truncate_page(inode->i_mapping, inode->i_size);
Chris Mason4a096752008-07-21 10:29:44 -04004379 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04004380
Chris Mason39279cc2007-06-12 06:35:45 -04004381 trans = btrfs_start_transaction(root, 1);
4382 btrfs_set_trans_block_group(trans, inode);
Chris Masondbe674a2008-07-17 12:54:05 -04004383 btrfs_i_size_write(inode, inode->i_size);
Chris Mason39279cc2007-06-12 06:35:45 -04004384
Josef Bacik7b128762008-07-24 12:17:14 -04004385 ret = btrfs_orphan_add(trans, inode);
4386 if (ret)
4387 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04004388 /* FIXME, add redo link to tree so we don't leak on crash */
Chris Masone02119d2008-09-05 16:13:11 -04004389 ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size,
Chris Mason85e21ba2008-01-29 15:11:36 -05004390 BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004391 btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004392
Josef Bacik7b128762008-07-24 12:17:14 -04004393 ret = btrfs_orphan_del(trans, inode);
4394 BUG_ON(ret);
4395
4396out:
4397 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04004398 ret = btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004399 BUG_ON(ret);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004400 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004401}
4402
Sven Wegener3b963622008-06-09 21:57:42 -04004403/*
Chris Masond352ac62008-09-29 15:18:18 -04004404 * create a new subvolume directory/inode (helper for the ioctl).
4405 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05004406int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
4407 struct btrfs_root *new_root, struct dentry *dentry,
4408 u64 new_dirid, u64 alloc_hint)
Chris Mason39279cc2007-06-12 06:35:45 -04004409{
Chris Mason39279cc2007-06-12 06:35:45 -04004410 struct inode *inode;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04004411 int error;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004412 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004413
Josef Bacikaec74772008-07-24 12:12:38 -04004414 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
Yan Zhengd2fb3432008-12-11 16:30:39 -05004415 new_dirid, alloc_hint, S_IFDIR | 0700, &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04004416 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004417 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004418 inode->i_op = &btrfs_dir_inode_operations;
4419 inode->i_fop = &btrfs_dir_file_operations;
4420
Chris Mason39279cc2007-06-12 06:35:45 -04004421 inode->i_nlink = 1;
Chris Masondbe674a2008-07-17 12:54:05 -04004422 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04004423
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04004424 error = btrfs_update_inode(trans, new_root, inode);
4425 if (error)
4426 return error;
4427
4428 d_instantiate(dentry, inode);
4429 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004430}
4431
Chris Masond352ac62008-09-29 15:18:18 -04004432/* helper function for file defrag and space balancing. This
4433 * forces readahead on a given range of bytes in an inode
4434 */
Chris Masonedbd8d42007-12-21 16:27:24 -05004435unsigned long btrfs_force_ra(struct address_space *mapping,
Chris Mason86479a02007-09-10 19:58:16 -04004436 struct file_ra_state *ra, struct file *file,
4437 pgoff_t offset, pgoff_t last_index)
4438{
Chris Mason8e7bf942008-04-28 09:02:36 -04004439 pgoff_t req_size = last_index - offset + 1;
Chris Mason86479a02007-09-10 19:58:16 -04004440
Chris Mason86479a02007-09-10 19:58:16 -04004441 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
4442 return offset + req_size;
Chris Mason86479a02007-09-10 19:58:16 -04004443}
4444
Chris Mason39279cc2007-06-12 06:35:45 -04004445struct inode *btrfs_alloc_inode(struct super_block *sb)
4446{
4447 struct btrfs_inode *ei;
4448
4449 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
4450 if (!ei)
4451 return NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04004452 ei->last_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004453 ei->logged_trans = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004454 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Josef Bacik33268ea2008-07-24 12:16:36 -04004455 ei->i_acl = BTRFS_ACL_NOT_CACHED;
4456 ei->i_default_acl = BTRFS_ACL_NOT_CACHED;
Josef Bacik7b128762008-07-24 12:17:14 -04004457 INIT_LIST_HEAD(&ei->i_orphan);
Chris Mason39279cc2007-06-12 06:35:45 -04004458 return &ei->vfs_inode;
4459}
4460
4461void btrfs_destroy_inode(struct inode *inode)
4462{
Chris Masone6dcd2d2008-07-17 12:53:50 -04004463 struct btrfs_ordered_extent *ordered;
Chris Mason39279cc2007-06-12 06:35:45 -04004464 WARN_ON(!list_empty(&inode->i_dentry));
4465 WARN_ON(inode->i_data.nrpages);
4466
Josef Bacik33268ea2008-07-24 12:16:36 -04004467 if (BTRFS_I(inode)->i_acl &&
4468 BTRFS_I(inode)->i_acl != BTRFS_ACL_NOT_CACHED)
4469 posix_acl_release(BTRFS_I(inode)->i_acl);
4470 if (BTRFS_I(inode)->i_default_acl &&
4471 BTRFS_I(inode)->i_default_acl != BTRFS_ACL_NOT_CACHED)
4472 posix_acl_release(BTRFS_I(inode)->i_default_acl);
4473
Yanbcc63ab2008-07-30 16:29:20 -04004474 spin_lock(&BTRFS_I(inode)->root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04004475 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
4476 printk(KERN_ERR "BTRFS: inode %lu: inode still on the orphan"
4477 " list\n", inode->i_ino);
4478 dump_stack();
4479 }
Yanbcc63ab2008-07-30 16:29:20 -04004480 spin_unlock(&BTRFS_I(inode)->root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04004481
Chris Masond3977122009-01-05 21:25:51 -05004482 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04004483 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
4484 if (!ordered)
4485 break;
4486 else {
Chris Masond3977122009-01-05 21:25:51 -05004487 printk(KERN_ERR "btrfs found ordered "
4488 "extent %llu %llu on inode cleanup\n",
4489 (unsigned long long)ordered->file_offset,
4490 (unsigned long long)ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004491 btrfs_remove_ordered_extent(inode, ordered);
4492 btrfs_put_ordered_extent(ordered);
4493 btrfs_put_ordered_extent(ordered);
4494 }
4495 }
Zheng Yan5b21f2e2008-09-26 10:05:38 -04004496 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004497 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
4498}
4499
Sven Wegener0ee0fda2008-07-30 16:54:26 -04004500static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04004501{
4502 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
4503
4504 inode_init_once(&ei->vfs_inode);
4505}
4506
4507void btrfs_destroy_cachep(void)
4508{
4509 if (btrfs_inode_cachep)
4510 kmem_cache_destroy(btrfs_inode_cachep);
4511 if (btrfs_trans_handle_cachep)
4512 kmem_cache_destroy(btrfs_trans_handle_cachep);
4513 if (btrfs_transaction_cachep)
4514 kmem_cache_destroy(btrfs_transaction_cachep);
4515 if (btrfs_bit_radix_cachep)
4516 kmem_cache_destroy(btrfs_bit_radix_cachep);
4517 if (btrfs_path_cachep)
4518 kmem_cache_destroy(btrfs_path_cachep);
4519}
4520
Chris Mason86479a02007-09-10 19:58:16 -04004521struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
Chris Mason92fee662007-07-25 12:31:35 -04004522 unsigned long extra_flags,
Chris Mason2b1f55b2008-09-24 11:48:04 -04004523 void (*ctor)(void *))
Chris Mason92fee662007-07-25 12:31:35 -04004524{
4525 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
Chris Mason2b1f55b2008-09-24 11:48:04 -04004526 SLAB_MEM_SPREAD | extra_flags), ctor);
Chris Mason92fee662007-07-25 12:31:35 -04004527}
4528
Chris Mason39279cc2007-06-12 06:35:45 -04004529int btrfs_init_cachep(void)
4530{
Chris Mason86479a02007-09-10 19:58:16 -04004531 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
Chris Mason92fee662007-07-25 12:31:35 -04004532 sizeof(struct btrfs_inode),
4533 0, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04004534 if (!btrfs_inode_cachep)
4535 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04004536 btrfs_trans_handle_cachep =
4537 btrfs_cache_create("btrfs_trans_handle_cache",
4538 sizeof(struct btrfs_trans_handle),
4539 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004540 if (!btrfs_trans_handle_cachep)
4541 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04004542 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
Chris Mason39279cc2007-06-12 06:35:45 -04004543 sizeof(struct btrfs_transaction),
Chris Mason92fee662007-07-25 12:31:35 -04004544 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004545 if (!btrfs_transaction_cachep)
4546 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04004547 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
Yan23223582007-09-17 11:08:52 -04004548 sizeof(struct btrfs_path),
Chris Mason92fee662007-07-25 12:31:35 -04004549 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004550 if (!btrfs_path_cachep)
4551 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04004552 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
Chris Mason92fee662007-07-25 12:31:35 -04004553 SLAB_DESTROY_BY_RCU, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004554 if (!btrfs_bit_radix_cachep)
4555 goto fail;
4556 return 0;
4557fail:
4558 btrfs_destroy_cachep();
4559 return -ENOMEM;
4560}
4561
4562static int btrfs_getattr(struct vfsmount *mnt,
4563 struct dentry *dentry, struct kstat *stat)
4564{
4565 struct inode *inode = dentry->d_inode;
4566 generic_fillattr(inode, stat);
Chris Mason3394e162008-11-17 20:42:26 -05004567 stat->dev = BTRFS_I(inode)->root->anon_super.s_dev;
Chris Masond6667462008-01-03 14:51:00 -05004568 stat->blksize = PAGE_CACHE_SIZE;
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004569 stat->blocks = (inode_get_bytes(inode) +
4570 BTRFS_I(inode)->delalloc_bytes) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04004571 return 0;
4572}
4573
Chris Masond3977122009-01-05 21:25:51 -05004574static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
4575 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04004576{
4577 struct btrfs_trans_handle *trans;
4578 struct btrfs_root *root = BTRFS_I(old_dir)->root;
4579 struct inode *new_inode = new_dentry->d_inode;
4580 struct inode *old_inode = old_dentry->d_inode;
4581 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004582 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004583 int ret;
4584
Chris Mason3394e162008-11-17 20:42:26 -05004585 /* we're not allowed to rename between subvolumes */
4586 if (BTRFS_I(old_inode)->root->root_key.objectid !=
4587 BTRFS_I(new_dir)->root->root_key.objectid)
4588 return -EXDEV;
4589
Chris Mason39279cc2007-06-12 06:35:45 -04004590 if (S_ISDIR(old_inode->i_mode) && new_inode &&
4591 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
4592 return -ENOTEMPTY;
4593 }
Chris Mason5f39d392007-10-15 16:14:19 -04004594
Chris Mason0660b5a2008-11-17 20:37:39 -05004595 /* to rename a snapshot or subvolume, we need to juggle the
4596 * backrefs. This isn't coded yet
4597 */
4598 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
4599 return -EXDEV;
4600
Chris Mason1832a6d2007-12-21 16:27:21 -05004601 ret = btrfs_check_free_space(root, 1, 0);
4602 if (ret)
4603 goto out_unlock;
4604
Chris Mason39279cc2007-06-12 06:35:45 -04004605 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04004606
Chris Mason39279cc2007-06-12 06:35:45 -04004607 btrfs_set_trans_block_group(trans, new_dir);
Chris Mason39279cc2007-06-12 06:35:45 -04004608
Chris Masone02119d2008-09-05 16:13:11 -04004609 btrfs_inc_nlink(old_dentry->d_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004610 old_dir->i_ctime = old_dir->i_mtime = ctime;
4611 new_dir->i_ctime = new_dir->i_mtime = ctime;
4612 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04004613
Chris Masone02119d2008-09-05 16:13:11 -04004614 ret = btrfs_unlink_inode(trans, root, old_dir, old_dentry->d_inode,
4615 old_dentry->d_name.name,
4616 old_dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04004617 if (ret)
4618 goto out_fail;
4619
4620 if (new_inode) {
4621 new_inode->i_ctime = CURRENT_TIME;
Chris Masone02119d2008-09-05 16:13:11 -04004622 ret = btrfs_unlink_inode(trans, root, new_dir,
4623 new_dentry->d_inode,
4624 new_dentry->d_name.name,
4625 new_dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04004626 if (ret)
4627 goto out_fail;
Josef Bacik7b128762008-07-24 12:17:14 -04004628 if (new_inode->i_nlink == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004629 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Josef Bacik7b128762008-07-24 12:17:14 -04004630 if (ret)
4631 goto out_fail;
4632 }
Chris Masone02119d2008-09-05 16:13:11 -04004633
Chris Mason39279cc2007-06-12 06:35:45 -04004634 }
Chris Mason3de45862008-11-17 21:02:50 -05004635 ret = btrfs_set_inode_index(new_dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004636 if (ret)
4637 goto out_fail;
4638
Chris Masone02119d2008-09-05 16:13:11 -04004639 ret = btrfs_add_link(trans, new_dentry->d_parent->d_inode,
4640 old_inode, new_dentry->d_name.name,
4641 new_dentry->d_name.len, 1, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004642 if (ret)
4643 goto out_fail;
4644
4645out_fail:
Chris Masonab78c842008-07-29 16:15:18 -04004646 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05004647out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04004648 return ret;
4649}
4650
Chris Masond352ac62008-09-29 15:18:18 -04004651/*
4652 * some fairly slow code that needs optimization. This walks the list
4653 * of all the inodes with pending delalloc and forces them to disk.
4654 */
Chris Masonea8c2812008-08-04 23:17:27 -04004655int btrfs_start_delalloc_inodes(struct btrfs_root *root)
4656{
4657 struct list_head *head = &root->fs_info->delalloc_inodes;
4658 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04004659 struct inode *inode;
Chris Masonea8c2812008-08-04 23:17:27 -04004660
Yan Zhengc146afa2008-11-12 14:34:12 -05004661 if (root->fs_info->sb->s_flags & MS_RDONLY)
4662 return -EROFS;
4663
Chris Mason75eff682008-12-15 15:54:40 -05004664 spin_lock(&root->fs_info->delalloc_lock);
Chris Masond3977122009-01-05 21:25:51 -05004665 while (!list_empty(head)) {
Chris Masonea8c2812008-08-04 23:17:27 -04004666 binode = list_entry(head->next, struct btrfs_inode,
4667 delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04004668 inode = igrab(&binode->vfs_inode);
4669 if (!inode)
4670 list_del_init(&binode->delalloc_inodes);
Chris Mason75eff682008-12-15 15:54:40 -05004671 spin_unlock(&root->fs_info->delalloc_lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04004672 if (inode) {
Chris Mason8c8bee12008-09-29 11:19:10 -04004673 filemap_flush(inode->i_mapping);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04004674 iput(inode);
4675 }
4676 cond_resched();
Chris Mason75eff682008-12-15 15:54:40 -05004677 spin_lock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04004678 }
Chris Mason75eff682008-12-15 15:54:40 -05004679 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04004680
4681 /* the filemap_flush will queue IO into the worker threads, but
4682 * we have to make sure the IO is actually started and that
4683 * ordered extents get created before we return
4684 */
4685 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05004686 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05004687 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04004688 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05004689 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
4690 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04004691 }
4692 atomic_dec(&root->fs_info->async_submit_draining);
Chris Masonea8c2812008-08-04 23:17:27 -04004693 return 0;
4694}
4695
Chris Mason39279cc2007-06-12 06:35:45 -04004696static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
4697 const char *symname)
4698{
4699 struct btrfs_trans_handle *trans;
4700 struct btrfs_root *root = BTRFS_I(dir)->root;
4701 struct btrfs_path *path;
4702 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05004703 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04004704 int err;
4705 int drop_inode = 0;
4706 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004707 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04004708 int name_len;
4709 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04004710 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04004711 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04004712 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05004713 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004714
4715 name_len = strlen(symname) + 1;
4716 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
4717 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05004718
Chris Mason1832a6d2007-12-21 16:27:21 -05004719 err = btrfs_check_free_space(root, 1, 0);
4720 if (err)
4721 goto out_fail;
4722
Chris Mason39279cc2007-06-12 06:35:45 -04004723 trans = btrfs_start_transaction(root, 1);
4724 btrfs_set_trans_block_group(trans, dir);
4725
4726 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
4727 if (err) {
4728 err = -ENOSPC;
4729 goto out_unlock;
4730 }
4731
Josef Bacikaec74772008-07-24 12:12:38 -04004732 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05004733 dentry->d_name.len,
4734 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04004735 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO,
4736 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04004737 err = PTR_ERR(inode);
4738 if (IS_ERR(inode))
4739 goto out_unlock;
4740
Josef Bacik33268ea2008-07-24 12:16:36 -04004741 err = btrfs_init_acl(inode, dir);
4742 if (err) {
4743 drop_inode = 1;
4744 goto out_unlock;
4745 }
4746
Chris Mason39279cc2007-06-12 06:35:45 -04004747 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04004748 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004749 if (err)
4750 drop_inode = 1;
4751 else {
4752 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04004753 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04004754 inode->i_fop = &btrfs_file_operations;
4755 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05004756 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04004757 }
4758 dir->i_sb->s_dirt = 1;
4759 btrfs_update_inode_block_group(trans, inode);
4760 btrfs_update_inode_block_group(trans, dir);
4761 if (drop_inode)
4762 goto out_unlock;
4763
4764 path = btrfs_alloc_path();
4765 BUG_ON(!path);
4766 key.objectid = inode->i_ino;
4767 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004768 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
4769 datasize = btrfs_file_extent_calc_inline_size(name_len);
4770 err = btrfs_insert_empty_item(trans, root, path, &key,
4771 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04004772 if (err) {
4773 drop_inode = 1;
4774 goto out_unlock;
4775 }
Chris Mason5f39d392007-10-15 16:14:19 -04004776 leaf = path->nodes[0];
4777 ei = btrfs_item_ptr(leaf, path->slots[0],
4778 struct btrfs_file_extent_item);
4779 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
4780 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04004781 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04004782 btrfs_set_file_extent_encryption(leaf, ei, 0);
4783 btrfs_set_file_extent_compression(leaf, ei, 0);
4784 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
4785 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
4786
Chris Mason39279cc2007-06-12 06:35:45 -04004787 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04004788 write_extent_buffer(leaf, symname, ptr, name_len);
4789 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004790 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04004791
Chris Mason39279cc2007-06-12 06:35:45 -04004792 inode->i_op = &btrfs_symlink_inode_operations;
4793 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04004794 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04004795 inode_set_bytes(inode, name_len);
Chris Masondbe674a2008-07-17 12:54:05 -04004796 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04004797 err = btrfs_update_inode(trans, root, inode);
4798 if (err)
4799 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04004800
4801out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004802 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04004803 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05004804out_fail:
Chris Mason39279cc2007-06-12 06:35:45 -04004805 if (drop_inode) {
4806 inode_dec_link_count(inode);
4807 iput(inode);
4808 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004809 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004810 return err;
4811}
Chris Mason16432982008-04-10 10:23:21 -04004812
Yan Zhengd899e052008-10-30 14:25:28 -04004813static int prealloc_file_range(struct inode *inode, u64 start, u64 end,
4814 u64 alloc_hint, int mode)
4815{
4816 struct btrfs_trans_handle *trans;
4817 struct btrfs_root *root = BTRFS_I(inode)->root;
4818 struct btrfs_key ins;
4819 u64 alloc_size;
4820 u64 cur_offset = start;
4821 u64 num_bytes = end - start;
4822 int ret = 0;
4823
4824 trans = btrfs_join_transaction(root, 1);
4825 BUG_ON(!trans);
4826 btrfs_set_trans_block_group(trans, inode);
4827
4828 while (num_bytes > 0) {
4829 alloc_size = min(num_bytes, root->fs_info->max_extent);
4830 ret = btrfs_reserve_extent(trans, root, alloc_size,
4831 root->sectorsize, 0, alloc_hint,
4832 (u64)-1, &ins, 1);
4833 if (ret) {
4834 WARN_ON(1);
4835 goto out;
4836 }
4837 ret = insert_reserved_file_extent(trans, inode,
4838 cur_offset, ins.objectid,
4839 ins.offset, ins.offset,
4840 ins.offset, 0, 0, 0,
4841 BTRFS_FILE_EXTENT_PREALLOC);
4842 BUG_ON(ret);
4843 num_bytes -= ins.offset;
4844 cur_offset += ins.offset;
4845 alloc_hint = ins.objectid + ins.offset;
4846 }
4847out:
4848 if (cur_offset > start) {
4849 inode->i_ctime = CURRENT_TIME;
4850 btrfs_set_flag(inode, PREALLOC);
4851 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
4852 cur_offset > i_size_read(inode))
4853 btrfs_i_size_write(inode, cur_offset);
4854 ret = btrfs_update_inode(trans, root, inode);
4855 BUG_ON(ret);
4856 }
4857
4858 btrfs_end_transaction(trans, root);
4859 return ret;
4860}
4861
4862static long btrfs_fallocate(struct inode *inode, int mode,
4863 loff_t offset, loff_t len)
4864{
4865 u64 cur_offset;
4866 u64 last_byte;
4867 u64 alloc_start;
4868 u64 alloc_end;
4869 u64 alloc_hint = 0;
4870 u64 mask = BTRFS_I(inode)->root->sectorsize - 1;
4871 struct extent_map *em;
4872 int ret;
4873
4874 alloc_start = offset & ~mask;
4875 alloc_end = (offset + len + mask) & ~mask;
4876
4877 mutex_lock(&inode->i_mutex);
4878 if (alloc_start > inode->i_size) {
4879 ret = btrfs_cont_expand(inode, alloc_start);
4880 if (ret)
4881 goto out;
4882 }
4883
4884 while (1) {
4885 struct btrfs_ordered_extent *ordered;
4886 lock_extent(&BTRFS_I(inode)->io_tree, alloc_start,
4887 alloc_end - 1, GFP_NOFS);
4888 ordered = btrfs_lookup_first_ordered_extent(inode,
4889 alloc_end - 1);
4890 if (ordered &&
4891 ordered->file_offset + ordered->len > alloc_start &&
4892 ordered->file_offset < alloc_end) {
4893 btrfs_put_ordered_extent(ordered);
4894 unlock_extent(&BTRFS_I(inode)->io_tree,
4895 alloc_start, alloc_end - 1, GFP_NOFS);
4896 btrfs_wait_ordered_range(inode, alloc_start,
4897 alloc_end - alloc_start);
4898 } else {
4899 if (ordered)
4900 btrfs_put_ordered_extent(ordered);
4901 break;
4902 }
4903 }
4904
4905 cur_offset = alloc_start;
4906 while (1) {
4907 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4908 alloc_end - cur_offset, 0);
4909 BUG_ON(IS_ERR(em) || !em);
4910 last_byte = min(extent_map_end(em), alloc_end);
4911 last_byte = (last_byte + mask) & ~mask;
4912 if (em->block_start == EXTENT_MAP_HOLE) {
4913 ret = prealloc_file_range(inode, cur_offset,
4914 last_byte, alloc_hint, mode);
4915 if (ret < 0) {
4916 free_extent_map(em);
4917 break;
4918 }
4919 }
4920 if (em->block_start <= EXTENT_MAP_LAST_BYTE)
4921 alloc_hint = em->block_start;
4922 free_extent_map(em);
4923
4924 cur_offset = last_byte;
4925 if (cur_offset >= alloc_end) {
4926 ret = 0;
4927 break;
4928 }
4929 }
4930 unlock_extent(&BTRFS_I(inode)->io_tree, alloc_start, alloc_end - 1,
4931 GFP_NOFS);
4932out:
4933 mutex_unlock(&inode->i_mutex);
4934 return ret;
4935}
4936
Chris Masone6dcd2d2008-07-17 12:53:50 -04004937static int btrfs_set_page_dirty(struct page *page)
4938{
Chris Masone6dcd2d2008-07-17 12:53:50 -04004939 return __set_page_dirty_nobuffers(page);
4940}
4941
Sven Wegener0ee0fda2008-07-30 16:54:26 -04004942static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05004943{
4944 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
4945 return -EACCES;
Josef Bacik33268ea2008-07-24 12:16:36 -04004946 return generic_permission(inode, mask, btrfs_check_acl);
Yanfdebe2b2008-01-14 13:26:08 -05004947}
Chris Mason39279cc2007-06-12 06:35:45 -04004948
4949static struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05004950 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04004951 .lookup = btrfs_lookup,
4952 .create = btrfs_create,
4953 .unlink = btrfs_unlink,
4954 .link = btrfs_link,
4955 .mkdir = btrfs_mkdir,
4956 .rmdir = btrfs_rmdir,
4957 .rename = btrfs_rename,
4958 .symlink = btrfs_symlink,
4959 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04004960 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04004961 .setxattr = btrfs_setxattr,
4962 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05004963 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04004964 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05004965 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04004966};
Chris Mason39279cc2007-06-12 06:35:45 -04004967static struct inode_operations btrfs_dir_ro_inode_operations = {
4968 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05004969 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04004970};
Chris Mason39279cc2007-06-12 06:35:45 -04004971static struct file_operations btrfs_dir_file_operations = {
4972 .llseek = generic_file_llseek,
4973 .read = generic_read_dir,
David Woodhousecbdf5a22008-08-06 19:42:33 +01004974 .readdir = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04004975 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04004976#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04004977 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04004978#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04004979 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04004980 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04004981};
4982
Chris Masond1310b22008-01-24 16:13:08 -05004983static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04004984 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05004985 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04004986 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04004987 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04004988 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04004989 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Mason1259ab72008-05-12 13:39:03 -04004990 .readpage_io_failed_hook = btrfs_io_failed_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05004991 .set_bit_hook = btrfs_set_bit_hook,
4992 .clear_bit_hook = btrfs_clear_bit_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04004993};
4994
Chris Mason39279cc2007-06-12 06:35:45 -04004995static struct address_space_operations btrfs_aops = {
4996 .readpage = btrfs_readpage,
4997 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -04004998 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05004999 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04005000 .sync_page = block_sync_page,
Chris Mason39279cc2007-06-12 06:35:45 -04005001 .bmap = btrfs_bmap,
Chris Mason16432982008-04-10 10:23:21 -04005002 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04005003 .invalidatepage = btrfs_invalidatepage,
5004 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005005 .set_page_dirty = btrfs_set_page_dirty,
Chris Mason39279cc2007-06-12 06:35:45 -04005006};
5007
5008static struct address_space_operations btrfs_symlink_aops = {
5009 .readpage = btrfs_readpage,
5010 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04005011 .invalidatepage = btrfs_invalidatepage,
5012 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04005013};
5014
5015static struct inode_operations btrfs_file_inode_operations = {
5016 .truncate = btrfs_truncate,
5017 .getattr = btrfs_getattr,
5018 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04005019 .setxattr = btrfs_setxattr,
5020 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05005021 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04005022 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05005023 .permission = btrfs_permission,
Yan Zhengd899e052008-10-30 14:25:28 -04005024 .fallocate = btrfs_fallocate,
Chris Mason39279cc2007-06-12 06:35:45 -04005025};
Josef Bacik618e21d2007-07-11 10:18:17 -04005026static struct inode_operations btrfs_special_inode_operations = {
5027 .getattr = btrfs_getattr,
5028 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05005029 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04005030 .setxattr = btrfs_setxattr,
5031 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04005032 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04005033 .removexattr = btrfs_removexattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04005034};
Chris Mason39279cc2007-06-12 06:35:45 -04005035static struct inode_operations btrfs_symlink_inode_operations = {
5036 .readlink = generic_readlink,
5037 .follow_link = page_follow_link_light,
5038 .put_link = page_put_link,
Yanfdebe2b2008-01-14 13:26:08 -05005039 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04005040};