blob: d8393ddc72a38ae7fc28fc66652454627116f35b [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Mason8f18cf12008-04-25 16:53:30 -040019#include <linux/kernel.h>
Chris Mason065631f2008-02-20 12:07:25 -050020#include <linux/bio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040021#include <linux/buffer_head.h>
Sage Weilf2eb0a22008-05-02 14:43:14 -040022#include <linux/file.h>
Chris Mason39279cc2007-06-12 06:35:45 -040023#include <linux/fs.h>
24#include <linux/pagemap.h>
25#include <linux/highmem.h>
26#include <linux/time.h>
27#include <linux/init.h>
28#include <linux/string.h>
Chris Mason39279cc2007-06-12 06:35:45 -040029#include <linux/backing-dev.h>
30#include <linux/mpage.h>
31#include <linux/swap.h>
32#include <linux/writeback.h>
33#include <linux/statfs.h>
34#include <linux/compat.h>
Chris Mason9ebefb182007-06-15 13:50:00 -040035#include <linux/bit_spinlock.h>
Josef Bacik5103e942007-11-16 11:45:54 -050036#include <linux/xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040037#include <linux/posix_acl.h>
Yan Zhengd899e052008-10-30 14:25:28 -040038#include <linux/falloc.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050039#include "compat.h"
Chris Mason39279cc2007-06-12 06:35:45 -040040#include "ctree.h"
41#include "disk-io.h"
42#include "transaction.h"
43#include "btrfs_inode.h"
44#include "ioctl.h"
45#include "print-tree.h"
Chris Mason0b86a832008-03-24 15:01:56 -040046#include "volumes.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040047#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040048#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040049#include "tree-log.h"
Chris Masonc8b97812008-10-29 14:49:59 -040050#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050051#include "locking.h"
Chris Mason39279cc2007-06-12 06:35:45 -040052
53struct btrfs_iget_args {
54 u64 ino;
55 struct btrfs_root *root;
56};
57
58static struct inode_operations btrfs_dir_inode_operations;
59static struct inode_operations btrfs_symlink_inode_operations;
60static struct inode_operations btrfs_dir_ro_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -040061static struct inode_operations btrfs_special_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -040062static struct inode_operations btrfs_file_inode_operations;
63static struct address_space_operations btrfs_aops;
64static struct address_space_operations btrfs_symlink_aops;
65static struct file_operations btrfs_dir_file_operations;
Chris Masond1310b22008-01-24 16:13:08 -050066static struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040067
68static struct kmem_cache *btrfs_inode_cachep;
69struct kmem_cache *btrfs_trans_handle_cachep;
70struct kmem_cache *btrfs_transaction_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040071struct kmem_cache *btrfs_path_cachep;
72
73#define S_SHIFT 12
74static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
75 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
76 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
77 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
78 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
79 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
80 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
81 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
82};
83
Josef Bacik7b128762008-07-24 12:17:14 -040084static void btrfs_truncate(struct inode *inode);
Chris Masonc8b97812008-10-29 14:49:59 -040085static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end);
Chris Mason771ed682008-11-06 22:02:51 -050086static noinline int cow_file_range(struct inode *inode,
87 struct page *locked_page,
88 u64 start, u64 end, int *page_started,
89 unsigned long *nr_written, int unlock);
Josef Bacik7b128762008-07-24 12:17:14 -040090
Jim Owens0279b4c2009-02-04 09:29:13 -050091static int btrfs_init_inode_security(struct inode *inode, struct inode *dir)
92{
93 int err;
94
95 err = btrfs_init_acl(inode, dir);
96 if (!err)
97 err = btrfs_xattr_security_init(inode, dir);
98 return err;
99}
100
Chris Masond352ac62008-09-29 15:18:18 -0400101/*
Chris Masonc8b97812008-10-29 14:49:59 -0400102 * this does all the hard work for inserting an inline extent into
103 * the btree. The caller should have done a btrfs_drop_extents so that
104 * no overlapping inline items exist in the btree
105 */
Chris Masond3977122009-01-05 21:25:51 -0500106static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400107 struct btrfs_root *root, struct inode *inode,
108 u64 start, size_t size, size_t compressed_size,
109 struct page **compressed_pages)
110{
111 struct btrfs_key key;
112 struct btrfs_path *path;
113 struct extent_buffer *leaf;
114 struct page *page = NULL;
115 char *kaddr;
116 unsigned long ptr;
117 struct btrfs_file_extent_item *ei;
118 int err = 0;
119 int ret;
120 size_t cur_size = size;
121 size_t datasize;
122 unsigned long offset;
123 int use_compress = 0;
124
125 if (compressed_size && compressed_pages) {
126 use_compress = 1;
127 cur_size = compressed_size;
128 }
129
Chris Masond3977122009-01-05 21:25:51 -0500130 path = btrfs_alloc_path();
131 if (!path)
Chris Masonc8b97812008-10-29 14:49:59 -0400132 return -ENOMEM;
133
Chris Masonb9473432009-03-13 11:00:37 -0400134 path->leave_spinning = 1;
Chris Masonc8b97812008-10-29 14:49:59 -0400135 btrfs_set_trans_block_group(trans, inode);
136
137 key.objectid = inode->i_ino;
138 key.offset = start;
139 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
Chris Masonc8b97812008-10-29 14:49:59 -0400140 datasize = btrfs_file_extent_calc_inline_size(cur_size);
141
142 inode_add_bytes(inode, size);
143 ret = btrfs_insert_empty_item(trans, root, path, &key,
144 datasize);
145 BUG_ON(ret);
146 if (ret) {
147 err = ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400148 goto fail;
149 }
150 leaf = path->nodes[0];
151 ei = btrfs_item_ptr(leaf, path->slots[0],
152 struct btrfs_file_extent_item);
153 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
154 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
155 btrfs_set_file_extent_encryption(leaf, ei, 0);
156 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
157 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
158 ptr = btrfs_file_extent_inline_start(ei);
159
160 if (use_compress) {
161 struct page *cpage;
162 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500163 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400164 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500165 cur_size = min_t(unsigned long, compressed_size,
Chris Masonc8b97812008-10-29 14:49:59 -0400166 PAGE_CACHE_SIZE);
167
Chris Masonb9473432009-03-13 11:00:37 -0400168 kaddr = kmap_atomic(cpage, KM_USER0);
Chris Masonc8b97812008-10-29 14:49:59 -0400169 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Chris Masonb9473432009-03-13 11:00:37 -0400170 kunmap_atomic(kaddr, KM_USER0);
Chris Masonc8b97812008-10-29 14:49:59 -0400171
172 i++;
173 ptr += cur_size;
174 compressed_size -= cur_size;
175 }
176 btrfs_set_file_extent_compression(leaf, ei,
177 BTRFS_COMPRESS_ZLIB);
178 } else {
179 page = find_get_page(inode->i_mapping,
180 start >> PAGE_CACHE_SHIFT);
181 btrfs_set_file_extent_compression(leaf, ei, 0);
182 kaddr = kmap_atomic(page, KM_USER0);
183 offset = start & (PAGE_CACHE_SIZE - 1);
184 write_extent_buffer(leaf, kaddr + offset, ptr, size);
185 kunmap_atomic(kaddr, KM_USER0);
186 page_cache_release(page);
187 }
188 btrfs_mark_buffer_dirty(leaf);
189 btrfs_free_path(path);
190
191 BTRFS_I(inode)->disk_i_size = inode->i_size;
192 btrfs_update_inode(trans, root, inode);
193 return 0;
194fail:
195 btrfs_free_path(path);
196 return err;
197}
198
199
200/*
201 * conditionally insert an inline extent into the file. This
202 * does the checks required to make sure the data is small enough
203 * to fit as an inline extent.
204 */
Chris Mason7f366cf2009-03-12 20:12:45 -0400205static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400206 struct btrfs_root *root,
207 struct inode *inode, u64 start, u64 end,
208 size_t compressed_size,
209 struct page **compressed_pages)
210{
211 u64 isize = i_size_read(inode);
212 u64 actual_end = min(end + 1, isize);
213 u64 inline_len = actual_end - start;
214 u64 aligned_end = (end + root->sectorsize - 1) &
215 ~((u64)root->sectorsize - 1);
216 u64 hint_byte;
217 u64 data_len = inline_len;
218 int ret;
219
220 if (compressed_size)
221 data_len = compressed_size;
222
223 if (start > 0 ||
Chris Mason70b99e62008-10-31 12:46:39 -0400224 actual_end >= PAGE_CACHE_SIZE ||
Chris Masonc8b97812008-10-29 14:49:59 -0400225 data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
226 (!compressed_size &&
227 (actual_end & (root->sectorsize - 1)) == 0) ||
228 end + 1 < isize ||
229 data_len > root->fs_info->max_inline) {
230 return 1;
231 }
232
Chris Masonc8b97812008-10-29 14:49:59 -0400233 ret = btrfs_drop_extents(trans, root, inode, start,
Chris Masona1ed8352009-09-11 12:27:37 -0400234 aligned_end, aligned_end, start,
235 &hint_byte, 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400236 BUG_ON(ret);
237
238 if (isize > actual_end)
239 inline_len = min_t(u64, isize, actual_end);
240 ret = insert_inline_extent(trans, root, inode, start,
241 inline_len, compressed_size,
242 compressed_pages);
243 BUG_ON(ret);
Chris Masona1ed8352009-09-11 12:27:37 -0400244 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
Chris Masonc8b97812008-10-29 14:49:59 -0400245 return 0;
246}
247
Chris Mason771ed682008-11-06 22:02:51 -0500248struct async_extent {
249 u64 start;
250 u64 ram_size;
251 u64 compressed_size;
252 struct page **pages;
253 unsigned long nr_pages;
254 struct list_head list;
255};
256
257struct async_cow {
258 struct inode *inode;
259 struct btrfs_root *root;
260 struct page *locked_page;
261 u64 start;
262 u64 end;
263 struct list_head extents;
264 struct btrfs_work work;
265};
266
267static noinline int add_async_extent(struct async_cow *cow,
268 u64 start, u64 ram_size,
269 u64 compressed_size,
270 struct page **pages,
271 unsigned long nr_pages)
272{
273 struct async_extent *async_extent;
274
275 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
276 async_extent->start = start;
277 async_extent->ram_size = ram_size;
278 async_extent->compressed_size = compressed_size;
279 async_extent->pages = pages;
280 async_extent->nr_pages = nr_pages;
281 list_add_tail(&async_extent->list, &cow->extents);
282 return 0;
283}
284
Chris Masonc8b97812008-10-29 14:49:59 -0400285/*
Chris Mason771ed682008-11-06 22:02:51 -0500286 * we create compressed extents in two phases. The first
287 * phase compresses a range of pages that have already been
288 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400289 *
Chris Mason771ed682008-11-06 22:02:51 -0500290 * This is done inside an ordered work queue, and the compression
291 * is spread across many cpus. The actual IO submission is step
292 * two, and the ordered work queue takes care of making sure that
293 * happens in the same order things were put onto the queue by
294 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400295 *
Chris Mason771ed682008-11-06 22:02:51 -0500296 * If this code finds it can't get good compression, it puts an
297 * entry onto the work queue to write the uncompressed bytes. This
298 * makes sure that both compressed inodes and uncompressed inodes
299 * are written in the same order that pdflush sent them down.
Chris Masond352ac62008-09-29 15:18:18 -0400300 */
Chris Mason771ed682008-11-06 22:02:51 -0500301static noinline int compress_file_range(struct inode *inode,
302 struct page *locked_page,
303 u64 start, u64 end,
304 struct async_cow *async_cow,
305 int *num_added)
Chris Masonb888db22007-08-27 16:49:44 -0400306{
307 struct btrfs_root *root = BTRFS_I(inode)->root;
308 struct btrfs_trans_handle *trans;
Chris Masondb945352007-10-15 16:15:53 -0400309 u64 num_bytes;
Chris Masonc8b97812008-10-29 14:49:59 -0400310 u64 orig_start;
311 u64 disk_num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400312 u64 blocksize = root->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400313 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500314 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400315 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400316 struct page **pages = NULL;
317 unsigned long nr_pages;
318 unsigned long nr_pages_ret = 0;
319 unsigned long total_compressed = 0;
320 unsigned long total_in = 0;
321 unsigned long max_compressed = 128 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -0500322 unsigned long max_uncompressed = 128 * 1024;
Chris Masonc8b97812008-10-29 14:49:59 -0400323 int i;
324 int will_compress;
Chris Masonb888db22007-08-27 16:49:44 -0400325
Chris Masonc8b97812008-10-29 14:49:59 -0400326 orig_start = start;
Chris Masonbe20aa92007-12-17 20:14:01 -0500327
Chris Mason42dc7ba2008-12-15 11:44:56 -0500328 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400329again:
330 will_compress = 0;
331 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
332 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
333
Chris Masonf03d9302009-02-04 09:31:06 -0500334 /*
335 * we don't want to send crud past the end of i_size through
336 * compression, that's just a waste of CPU time. So, if the
337 * end of the file is before the start of our current
338 * requested range of bytes, we bail out to the uncompressed
339 * cleanup code that can deal with all of this.
340 *
341 * It isn't really the fastest way to fix things, but this is a
342 * very uncommon corner.
343 */
344 if (actual_end <= start)
345 goto cleanup_and_bail_uncompressed;
346
Chris Masonc8b97812008-10-29 14:49:59 -0400347 total_compressed = actual_end - start;
348
349 /* we want to make sure that amount of ram required to uncompress
350 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500351 * of a compressed extent to 128k. This is a crucial number
352 * because it also controls how easily we can spread reads across
353 * cpus for decompression.
354 *
355 * We also want to make sure the amount of IO required to do
356 * a random read is reasonably small, so we limit the size of
357 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400358 */
359 total_compressed = min(total_compressed, max_uncompressed);
Chris Masondb945352007-10-15 16:15:53 -0400360 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
Chris Masonbe20aa92007-12-17 20:14:01 -0500361 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400362 disk_num_bytes = num_bytes;
363 total_in = 0;
364 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400365
Chris Mason771ed682008-11-06 22:02:51 -0500366 /*
367 * we do compression for mount -o compress and when the
368 * inode has not been flagged as nocompress. This flag can
369 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400370 */
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200371 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) &&
Chris Masonc8b97812008-10-29 14:49:59 -0400372 btrfs_test_opt(root, COMPRESS)) {
373 WARN_ON(pages);
Chris Masoncfbc2462008-10-30 13:22:14 -0400374 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
Chris Mason179e29e2007-11-01 11:28:41 -0400375
Chris Masonc8b97812008-10-29 14:49:59 -0400376 ret = btrfs_zlib_compress_pages(inode->i_mapping, start,
377 total_compressed, pages,
378 nr_pages, &nr_pages_ret,
379 &total_in,
380 &total_compressed,
381 max_compressed);
382
383 if (!ret) {
384 unsigned long offset = total_compressed &
385 (PAGE_CACHE_SIZE - 1);
386 struct page *page = pages[nr_pages_ret - 1];
387 char *kaddr;
388
389 /* zero the tail end of the last page, we might be
390 * sending it down to disk
391 */
392 if (offset) {
393 kaddr = kmap_atomic(page, KM_USER0);
394 memset(kaddr + offset, 0,
395 PAGE_CACHE_SIZE - offset);
396 kunmap_atomic(kaddr, KM_USER0);
397 }
398 will_compress = 1;
399 }
400 }
401 if (start == 0) {
Chris Mason771ed682008-11-06 22:02:51 -0500402 trans = btrfs_join_transaction(root, 1);
403 BUG_ON(!trans);
404 btrfs_set_trans_block_group(trans, inode);
405
Chris Masonc8b97812008-10-29 14:49:59 -0400406 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500407 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400408 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500409 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400410 */
411 ret = cow_file_range_inline(trans, root, inode,
412 start, end, 0, NULL);
413 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500414 /* try making a compressed inline extent */
Chris Masonc8b97812008-10-29 14:49:59 -0400415 ret = cow_file_range_inline(trans, root, inode,
416 start, end,
417 total_compressed, pages);
418 }
Chris Mason771ed682008-11-06 22:02:51 -0500419 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400420 if (ret == 0) {
Chris Mason771ed682008-11-06 22:02:51 -0500421 /*
422 * inline extent creation worked, we don't need
423 * to create any more async work items. Unlock
424 * and free up our temp pages.
425 */
Chris Masonc8b97812008-10-29 14:49:59 -0400426 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400427 &BTRFS_I(inode)->io_tree,
428 start, end, NULL,
429 EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
Chris Masona3429ab2009-10-08 12:30:20 -0400430 EXTENT_CLEAR_DELALLOC |
Josef Bacik32c00af2009-10-08 13:34:05 -0400431 EXTENT_CLEAR_ACCOUNTING |
Chris Masona791e352009-10-08 11:27:10 -0400432 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK);
Chris Masonc8b97812008-10-29 14:49:59 -0400433 ret = 0;
434 goto free_pages_out;
435 }
436 }
437
438 if (will_compress) {
439 /*
440 * we aren't doing an inline extent round the compressed size
441 * up to a block size boundary so the allocator does sane
442 * things
443 */
444 total_compressed = (total_compressed + blocksize - 1) &
445 ~(blocksize - 1);
446
447 /*
448 * one last check to make sure the compression is really a
449 * win, compare the page count read with the blocks on disk
450 */
451 total_in = (total_in + PAGE_CACHE_SIZE - 1) &
452 ~(PAGE_CACHE_SIZE - 1);
453 if (total_compressed >= total_in) {
454 will_compress = 0;
455 } else {
456 disk_num_bytes = total_compressed;
457 num_bytes = total_in;
458 }
459 }
460 if (!will_compress && pages) {
461 /*
462 * the compression code ran but failed to make things smaller,
463 * free any pages it allocated and our page pointer array
464 */
465 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400466 WARN_ON(pages[i]->mapping);
Chris Masonc8b97812008-10-29 14:49:59 -0400467 page_cache_release(pages[i]);
468 }
469 kfree(pages);
470 pages = NULL;
471 total_compressed = 0;
472 nr_pages_ret = 0;
473
474 /* flag the file so we don't compress in the future */
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200475 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Masonc8b97812008-10-29 14:49:59 -0400476 }
Chris Mason771ed682008-11-06 22:02:51 -0500477 if (will_compress) {
478 *num_added += 1;
479
480 /* the async work queues will take care of doing actual
481 * allocation on disk for these compressed pages,
482 * and will submit them to the elevator.
483 */
484 add_async_extent(async_cow, start, num_bytes,
485 total_compressed, pages, nr_pages_ret);
486
Chris Mason42dc7ba2008-12-15 11:44:56 -0500487 if (start + num_bytes < end && start + num_bytes < actual_end) {
Chris Mason771ed682008-11-06 22:02:51 -0500488 start += num_bytes;
489 pages = NULL;
490 cond_resched();
491 goto again;
492 }
493 } else {
Chris Masonf03d9302009-02-04 09:31:06 -0500494cleanup_and_bail_uncompressed:
Chris Mason771ed682008-11-06 22:02:51 -0500495 /*
496 * No compression, but we still need to write the pages in
497 * the file we've been given so far. redirty the locked
498 * page if it corresponds to our extent and set things up
499 * for the async work queue to run cow_file_range to do
500 * the normal delalloc dance
501 */
502 if (page_offset(locked_page) >= start &&
503 page_offset(locked_page) <= end) {
504 __set_page_dirty_nobuffers(locked_page);
505 /* unlocked later on in the async handlers */
506 }
507 add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0);
508 *num_added += 1;
509 }
510
511out:
512 return 0;
513
514free_pages_out:
515 for (i = 0; i < nr_pages_ret; i++) {
516 WARN_ON(pages[i]->mapping);
517 page_cache_release(pages[i]);
518 }
Chris Masond3977122009-01-05 21:25:51 -0500519 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500520
521 goto out;
522}
523
524/*
525 * phase two of compressed writeback. This is the ordered portion
526 * of the code, which only gets called in the order the work was
527 * queued. We walk all the async extents created by compress_file_range
528 * and send them down to the disk.
529 */
530static noinline int submit_compressed_extents(struct inode *inode,
531 struct async_cow *async_cow)
532{
533 struct async_extent *async_extent;
534 u64 alloc_hint = 0;
535 struct btrfs_trans_handle *trans;
536 struct btrfs_key ins;
537 struct extent_map *em;
538 struct btrfs_root *root = BTRFS_I(inode)->root;
539 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
540 struct extent_io_tree *io_tree;
541 int ret;
542
543 if (list_empty(&async_cow->extents))
544 return 0;
545
546 trans = btrfs_join_transaction(root, 1);
547
Chris Masond3977122009-01-05 21:25:51 -0500548 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500549 async_extent = list_entry(async_cow->extents.next,
550 struct async_extent, list);
551 list_del(&async_extent->list);
552
553 io_tree = &BTRFS_I(inode)->io_tree;
554
555 /* did the compression code fall back to uncompressed IO? */
556 if (!async_extent->pages) {
557 int page_started = 0;
558 unsigned long nr_written = 0;
559
560 lock_extent(io_tree, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500561 async_extent->start +
562 async_extent->ram_size - 1, GFP_NOFS);
Chris Mason771ed682008-11-06 22:02:51 -0500563
564 /* allocate blocks */
565 cow_file_range(inode, async_cow->locked_page,
566 async_extent->start,
567 async_extent->start +
568 async_extent->ram_size - 1,
569 &page_started, &nr_written, 0);
570
571 /*
572 * if page_started, cow_file_range inserted an
573 * inline extent and took care of all the unlocking
574 * and IO for us. Otherwise, we need to submit
575 * all those pages down to the drive.
576 */
577 if (!page_started)
578 extent_write_locked_range(io_tree,
579 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500580 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500581 async_extent->ram_size - 1,
582 btrfs_get_extent,
583 WB_SYNC_ALL);
584 kfree(async_extent);
585 cond_resched();
586 continue;
587 }
588
589 lock_extent(io_tree, async_extent->start,
590 async_extent->start + async_extent->ram_size - 1,
591 GFP_NOFS);
592 /*
593 * here we're doing allocation and writeback of the
594 * compressed pages
595 */
596 btrfs_drop_extent_cache(inode, async_extent->start,
597 async_extent->start +
598 async_extent->ram_size - 1, 0);
599
600 ret = btrfs_reserve_extent(trans, root,
601 async_extent->compressed_size,
602 async_extent->compressed_size,
603 0, alloc_hint,
604 (u64)-1, &ins, 1);
605 BUG_ON(ret);
606 em = alloc_extent_map(GFP_NOFS);
607 em->start = async_extent->start;
608 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500609 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500610
611 em->block_start = ins.objectid;
612 em->block_len = ins.offset;
613 em->bdev = root->fs_info->fs_devices->latest_bdev;
614 set_bit(EXTENT_FLAG_PINNED, &em->flags);
615 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
616
Chris Masond3977122009-01-05 21:25:51 -0500617 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400618 write_lock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500619 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -0400620 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500621 if (ret != -EEXIST) {
622 free_extent_map(em);
623 break;
624 }
625 btrfs_drop_extent_cache(inode, async_extent->start,
626 async_extent->start +
627 async_extent->ram_size - 1, 0);
628 }
629
630 ret = btrfs_add_ordered_extent(inode, async_extent->start,
631 ins.objectid,
632 async_extent->ram_size,
633 ins.offset,
634 BTRFS_ORDERED_COMPRESSED);
635 BUG_ON(ret);
636
637 btrfs_end_transaction(trans, root);
638
639 /*
640 * clear dirty, set writeback and unlock the pages.
641 */
642 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400643 &BTRFS_I(inode)->io_tree,
644 async_extent->start,
645 async_extent->start +
646 async_extent->ram_size - 1,
647 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
648 EXTENT_CLEAR_UNLOCK |
Chris Masona3429ab2009-10-08 12:30:20 -0400649 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400650 EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500651
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,
Chris Masona791e352009-10-08 11:27:10 -0400721 &BTRFS_I(inode)->io_tree,
722 start, end, NULL,
723 EXTENT_CLEAR_UNLOCK_PAGE |
724 EXTENT_CLEAR_UNLOCK |
725 EXTENT_CLEAR_DELALLOC |
Josef Bacik32c00af2009-10-08 13:34:05 -0400726 EXTENT_CLEAR_ACCOUNTING |
Chris Masona791e352009-10-08 11:27:10 -0400727 EXTENT_CLEAR_DIRTY |
728 EXTENT_SET_WRITEBACK |
729 EXTENT_END_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500730 *nr_written = *nr_written +
731 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
732 *page_started = 1;
733 ret = 0;
734 goto out;
735 }
736 }
Chris Masonc8b97812008-10-29 14:49:59 -0400737
738 BUG_ON(disk_num_bytes >
739 btrfs_super_total_bytes(&root->fs_info->super_copy));
740
Chris Masonb917b7c2009-09-18 16:07:03 -0400741
742 read_lock(&BTRFS_I(inode)->extent_tree.lock);
743 em = search_extent_mapping(&BTRFS_I(inode)->extent_tree,
744 start, num_bytes);
745 if (em) {
Josef Bacik6346c932009-11-10 21:23:47 -0500746 /*
747 * if block start isn't an actual block number then find the
748 * first block in this inode and use that as a hint. If that
749 * block is also bogus then just don't worry about it.
750 */
751 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
752 free_extent_map(em);
753 em = search_extent_mapping(em_tree, 0, 0);
754 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
755 alloc_hint = em->block_start;
756 if (em)
757 free_extent_map(em);
758 } else {
759 alloc_hint = em->block_start;
760 free_extent_map(em);
761 }
Chris Masonb917b7c2009-09-18 16:07:03 -0400762 }
763 read_unlock(&BTRFS_I(inode)->extent_tree.lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400764 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400765
Chris Masond3977122009-01-05 21:25:51 -0500766 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400767 unsigned long op;
768
Chris Masonc8b97812008-10-29 14:49:59 -0400769 cur_alloc_size = min(disk_num_bytes, root->fs_info->max_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400770 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500771 root->sectorsize, 0, alloc_hint,
Chris Masone6dcd2d2008-07-17 12:53:50 -0400772 (u64)-1, &ins, 1);
Chris Masond3977122009-01-05 21:25:51 -0500773 BUG_ON(ret);
774
Chris Masone6dcd2d2008-07-17 12:53:50 -0400775 em = alloc_extent_map(GFP_NOFS);
776 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500777 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500778 ram_size = ins.offset;
779 em->len = ins.offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400780
Chris Masone6dcd2d2008-07-17 12:53:50 -0400781 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400782 em->block_len = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400783 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400784 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -0400785
Chris Masond3977122009-01-05 21:25:51 -0500786 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400787 write_lock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400788 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -0400789 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400790 if (ret != -EEXIST) {
791 free_extent_map(em);
792 break;
793 }
794 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400795 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400796 }
797
Chris Mason98d20f62008-04-14 09:46:10 -0400798 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400799 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -0500800 ram_size, cur_alloc_size, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400801 BUG_ON(ret);
Chris Masonc8b97812008-10-29 14:49:59 -0400802
Yan Zheng17d217f2008-12-12 10:03:38 -0500803 if (root->root_key.objectid ==
804 BTRFS_DATA_RELOC_TREE_OBJECTID) {
805 ret = btrfs_reloc_clone_csums(inode, start,
806 cur_alloc_size);
807 BUG_ON(ret);
808 }
809
Chris Masond3977122009-01-05 21:25:51 -0500810 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -0400811 break;
Chris Masond3977122009-01-05 21:25:51 -0500812
Chris Masonc8b97812008-10-29 14:49:59 -0400813 /* we're not doing compressed IO, don't unlock the first
814 * page (which the caller expects to stay locked), don't
815 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -0400816 *
817 * Do set the Private2 bit so we know this page was properly
818 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -0400819 */
Chris Masona791e352009-10-08 11:27:10 -0400820 op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0;
821 op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
822 EXTENT_SET_PRIVATE2;
823
Chris Masonc8b97812008-10-29 14:49:59 -0400824 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
825 start, start + ram_size - 1,
Chris Masona791e352009-10-08 11:27:10 -0400826 locked_page, op);
Chris Masonc8b97812008-10-29 14:49:59 -0400827 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -0500828 num_bytes -= cur_alloc_size;
829 alloc_hint = ins.objectid + ins.offset;
830 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400831 }
Chris Masonb888db22007-08-27 16:49:44 -0400832out:
Chris Mason771ed682008-11-06 22:02:51 -0500833 ret = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400834 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400835
Chris Masonbe20aa92007-12-17 20:14:01 -0500836 return ret;
Chris Mason771ed682008-11-06 22:02:51 -0500837}
Chris Masonc8b97812008-10-29 14:49:59 -0400838
Chris Mason771ed682008-11-06 22:02:51 -0500839/*
840 * work queue call back to started compression on a file and pages
841 */
842static noinline void async_cow_start(struct btrfs_work *work)
843{
844 struct async_cow *async_cow;
845 int num_added = 0;
846 async_cow = container_of(work, struct async_cow, work);
847
848 compress_file_range(async_cow->inode, async_cow->locked_page,
849 async_cow->start, async_cow->end, async_cow,
850 &num_added);
851 if (num_added == 0)
852 async_cow->inode = NULL;
853}
854
855/*
856 * work queue call back to submit previously compressed pages
857 */
858static noinline void async_cow_submit(struct btrfs_work *work)
859{
860 struct async_cow *async_cow;
861 struct btrfs_root *root;
862 unsigned long nr_pages;
863
864 async_cow = container_of(work, struct async_cow, work);
865
866 root = async_cow->root;
867 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
868 PAGE_CACHE_SHIFT;
869
870 atomic_sub(nr_pages, &root->fs_info->async_delalloc_pages);
871
872 if (atomic_read(&root->fs_info->async_delalloc_pages) <
873 5 * 1042 * 1024 &&
874 waitqueue_active(&root->fs_info->async_submit_wait))
875 wake_up(&root->fs_info->async_submit_wait);
876
Chris Masond3977122009-01-05 21:25:51 -0500877 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -0500878 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -0500879}
Chris Masonc8b97812008-10-29 14:49:59 -0400880
Chris Mason771ed682008-11-06 22:02:51 -0500881static noinline void async_cow_free(struct btrfs_work *work)
882{
883 struct async_cow *async_cow;
884 async_cow = container_of(work, struct async_cow, work);
885 kfree(async_cow);
886}
887
888static int cow_file_range_async(struct inode *inode, struct page *locked_page,
889 u64 start, u64 end, int *page_started,
890 unsigned long *nr_written)
891{
892 struct async_cow *async_cow;
893 struct btrfs_root *root = BTRFS_I(inode)->root;
894 unsigned long nr_pages;
895 u64 cur_end;
896 int limit = 10 * 1024 * 1042;
897
Chris Masona3429ab2009-10-08 12:30:20 -0400898 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
899 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -0500900 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500901 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
902 async_cow->inode = inode;
903 async_cow->root = root;
904 async_cow->locked_page = locked_page;
905 async_cow->start = start;
906
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200907 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
Chris Mason771ed682008-11-06 22:02:51 -0500908 cur_end = end;
909 else
910 cur_end = min(end, start + 512 * 1024 - 1);
911
912 async_cow->end = cur_end;
913 INIT_LIST_HEAD(&async_cow->extents);
914
915 async_cow->work.func = async_cow_start;
916 async_cow->work.ordered_func = async_cow_submit;
917 async_cow->work.ordered_free = async_cow_free;
918 async_cow->work.flags = 0;
919
Chris Mason771ed682008-11-06 22:02:51 -0500920 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
921 PAGE_CACHE_SHIFT;
922 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
923
924 btrfs_queue_worker(&root->fs_info->delalloc_workers,
925 &async_cow->work);
926
927 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
928 wait_event(root->fs_info->async_submit_wait,
929 (atomic_read(&root->fs_info->async_delalloc_pages) <
930 limit));
931 }
932
Chris Masond3977122009-01-05 21:25:51 -0500933 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -0500934 atomic_read(&root->fs_info->async_delalloc_pages)) {
935 wait_event(root->fs_info->async_submit_wait,
936 (atomic_read(&root->fs_info->async_delalloc_pages) ==
937 0));
938 }
939
940 *nr_written += nr_pages;
941 start = cur_end + 1;
942 }
943 *page_started = 1;
944 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -0500945}
946
Chris Masond3977122009-01-05 21:25:51 -0500947static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -0500948 u64 bytenr, u64 num_bytes)
949{
950 int ret;
951 struct btrfs_ordered_sum *sums;
952 LIST_HEAD(list);
953
Yan Zheng07d400a2009-01-06 11:42:00 -0500954 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
955 bytenr + num_bytes - 1, &list);
Yan Zheng17d217f2008-12-12 10:03:38 -0500956 if (ret == 0 && list_empty(&list))
957 return 0;
958
959 while (!list_empty(&list)) {
960 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
961 list_del(&sums->list);
962 kfree(sums);
963 }
964 return 1;
965}
966
Chris Masond352ac62008-09-29 15:18:18 -0400967/*
968 * when nowcow writeback call back. This checks for snapshots or COW copies
969 * of the extents that exist in the file, and COWs the file as required.
970 *
971 * If no cow copies or snapshots exist, we write directly to the existing
972 * blocks on disk
973 */
Chris Mason7f366cf2009-03-12 20:12:45 -0400974static noinline int run_delalloc_nocow(struct inode *inode,
975 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -0500976 u64 start, u64 end, int *page_started, int force,
977 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -0500978{
Chris Masonbe20aa92007-12-17 20:14:01 -0500979 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -0400980 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -0500981 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -0500982 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -0400983 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -0500984 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -0400985 u64 cow_start;
986 u64 cur_offset;
987 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400988 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -0400989 u64 disk_bytenr;
990 u64 num_bytes;
991 int extent_type;
992 int ret;
Yan Zhengd899e052008-10-30 14:25:28 -0400993 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -0400994 int nocow;
995 int check_prev = 1;
Chris Masonbe20aa92007-12-17 20:14:01 -0500996
997 path = btrfs_alloc_path();
998 BUG_ON(!path);
Yan Zheng7ea394f2008-08-05 13:05:02 -0400999 trans = btrfs_join_transaction(root, 1);
1000 BUG_ON(!trans);
Chris Masonbe20aa92007-12-17 20:14:01 -05001001
Yan Zheng80ff3852008-10-30 14:20:02 -04001002 cow_start = (u64)-1;
1003 cur_offset = start;
1004 while (1) {
1005 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
1006 cur_offset, 0);
1007 BUG_ON(ret < 0);
1008 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1009 leaf = path->nodes[0];
1010 btrfs_item_key_to_cpu(leaf, &found_key,
1011 path->slots[0] - 1);
1012 if (found_key.objectid == inode->i_ino &&
1013 found_key.type == BTRFS_EXTENT_DATA_KEY)
1014 path->slots[0]--;
1015 }
1016 check_prev = 0;
1017next_slot:
1018 leaf = path->nodes[0];
1019 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1020 ret = btrfs_next_leaf(root, path);
1021 if (ret < 0)
1022 BUG_ON(1);
1023 if (ret > 0)
1024 break;
1025 leaf = path->nodes[0];
1026 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001027
Yan Zheng80ff3852008-10-30 14:20:02 -04001028 nocow = 0;
1029 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001030 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001031 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001032
Yan Zheng80ff3852008-10-30 14:20:02 -04001033 if (found_key.objectid > inode->i_ino ||
1034 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1035 found_key.offset > end)
1036 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001037
Yan Zheng80ff3852008-10-30 14:20:02 -04001038 if (found_key.offset > cur_offset) {
1039 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001040 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001041 goto out_check;
1042 }
Chris Masonc31f8832008-01-08 15:46:31 -05001043
Yan Zheng80ff3852008-10-30 14:20:02 -04001044 fi = btrfs_item_ptr(leaf, path->slots[0],
1045 struct btrfs_file_extent_item);
1046 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001047
Yan Zhengd899e052008-10-30 14:25:28 -04001048 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1049 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001050 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001051 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001052 extent_end = found_key.offset +
1053 btrfs_file_extent_num_bytes(leaf, fi);
1054 if (extent_end <= start) {
1055 path->slots[0]++;
1056 goto next_slot;
1057 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001058 if (disk_bytenr == 0)
1059 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001060 if (btrfs_file_extent_compression(leaf, fi) ||
1061 btrfs_file_extent_encryption(leaf, fi) ||
1062 btrfs_file_extent_other_encoding(leaf, fi))
1063 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001064 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1065 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001066 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001067 goto out_check;
Yan Zheng17d217f2008-12-12 10:03:38 -05001068 if (btrfs_cross_ref_exist(trans, root, inode->i_ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001069 found_key.offset -
1070 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001071 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001072 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001073 disk_bytenr += cur_offset - found_key.offset;
1074 num_bytes = min(end + 1, extent_end) - cur_offset;
1075 /*
1076 * force cow if csum exists in the range.
1077 * this ensure that csum for a given extent are
1078 * either valid or do not exist.
1079 */
1080 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1081 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001082 nocow = 1;
1083 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1084 extent_end = found_key.offset +
1085 btrfs_file_extent_inline_len(leaf, fi);
1086 extent_end = ALIGN(extent_end, root->sectorsize);
1087 } else {
1088 BUG_ON(1);
1089 }
1090out_check:
1091 if (extent_end <= start) {
1092 path->slots[0]++;
1093 goto next_slot;
1094 }
1095 if (!nocow) {
1096 if (cow_start == (u64)-1)
1097 cow_start = cur_offset;
1098 cur_offset = extent_end;
1099 if (cur_offset > end)
1100 break;
1101 path->slots[0]++;
1102 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001103 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001104
Yan Zheng7ea394f2008-08-05 13:05:02 -04001105 btrfs_release_path(root, path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001106 if (cow_start != (u64)-1) {
1107 ret = cow_file_range(inode, locked_page, cow_start,
Chris Mason771ed682008-11-06 22:02:51 -05001108 found_key.offset - 1, page_started,
1109 nr_written, 1);
Yan Zheng80ff3852008-10-30 14:20:02 -04001110 BUG_ON(ret);
1111 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001112 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001113
Yan Zhengd899e052008-10-30 14:25:28 -04001114 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1115 struct extent_map *em;
1116 struct extent_map_tree *em_tree;
1117 em_tree = &BTRFS_I(inode)->extent_tree;
1118 em = alloc_extent_map(GFP_NOFS);
1119 em->start = cur_offset;
Chris Mason445a6942008-11-10 11:53:33 -05001120 em->orig_start = em->start;
Yan Zhengd899e052008-10-30 14:25:28 -04001121 em->len = num_bytes;
1122 em->block_len = num_bytes;
1123 em->block_start = disk_bytenr;
1124 em->bdev = root->fs_info->fs_devices->latest_bdev;
1125 set_bit(EXTENT_FLAG_PINNED, &em->flags);
1126 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001127 write_lock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001128 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04001129 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001130 if (ret != -EEXIST) {
1131 free_extent_map(em);
1132 break;
1133 }
1134 btrfs_drop_extent_cache(inode, em->start,
1135 em->start + em->len - 1, 0);
1136 }
1137 type = BTRFS_ORDERED_PREALLOC;
1138 } else {
1139 type = BTRFS_ORDERED_NOCOW;
1140 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001141
1142 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001143 num_bytes, num_bytes, type);
1144 BUG_ON(ret);
Chris Mason771ed682008-11-06 22:02:51 -05001145
Yan Zhengd899e052008-10-30 14:25:28 -04001146 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
Chris Masona791e352009-10-08 11:27:10 -04001147 cur_offset, cur_offset + num_bytes - 1,
1148 locked_page, EXTENT_CLEAR_UNLOCK_PAGE |
1149 EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
1150 EXTENT_SET_PRIVATE2);
Yan Zheng80ff3852008-10-30 14:20:02 -04001151 cur_offset = extent_end;
1152 if (cur_offset > end)
1153 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001154 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001155 btrfs_release_path(root, path);
1156
1157 if (cur_offset <= end && cow_start == (u64)-1)
1158 cow_start = cur_offset;
1159 if (cow_start != (u64)-1) {
1160 ret = cow_file_range(inode, locked_page, cow_start, end,
Chris Mason771ed682008-11-06 22:02:51 -05001161 page_started, nr_written, 1);
Yan Zheng80ff3852008-10-30 14:20:02 -04001162 BUG_ON(ret);
1163 }
1164
1165 ret = btrfs_end_transaction(trans, root);
1166 BUG_ON(ret);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001167 btrfs_free_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001168 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001169}
1170
Chris Masond352ac62008-09-29 15:18:18 -04001171/*
1172 * extent_io.c call back to do delayed allocation processing
1173 */
Chris Masonc8b97812008-10-29 14:49:59 -04001174static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001175 u64 start, u64 end, int *page_started,
1176 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001177{
Chris Masonbe20aa92007-12-17 20:14:01 -05001178 int ret;
Chris Mason7f366cf2009-03-12 20:12:45 -04001179 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona2135012008-06-25 16:01:30 -04001180
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001181 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)
Chris Masonc8b97812008-10-29 14:49:59 -04001182 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001183 page_started, 1, nr_written);
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001184 else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC)
Yan Zhengd899e052008-10-30 14:25:28 -04001185 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001186 page_started, 0, nr_written);
Chris Mason7f366cf2009-03-12 20:12:45 -04001187 else if (!btrfs_test_opt(root, COMPRESS))
1188 ret = cow_file_range(inode, locked_page, start, end,
1189 page_started, nr_written, 1);
Chris Masonbe20aa92007-12-17 20:14:01 -05001190 else
Chris Mason771ed682008-11-06 22:02:51 -05001191 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001192 page_started, nr_written);
Chris Masonb888db22007-08-27 16:49:44 -04001193 return ret;
1194}
1195
Josef Bacik9ed74f22009-09-11 16:12:44 -04001196static int btrfs_split_extent_hook(struct inode *inode,
1197 struct extent_state *orig, u64 split)
1198{
1199 struct btrfs_root *root = BTRFS_I(inode)->root;
1200 u64 size;
1201
1202 if (!(orig->state & EXTENT_DELALLOC))
1203 return 0;
1204
1205 size = orig->end - orig->start + 1;
1206 if (size > root->fs_info->max_extent) {
1207 u64 num_extents;
1208 u64 new_size;
1209
1210 new_size = orig->end - split + 1;
1211 num_extents = div64_u64(size + root->fs_info->max_extent - 1,
1212 root->fs_info->max_extent);
1213
1214 /*
Josef Bacik32c00af2009-10-08 13:34:05 -04001215 * if we break a large extent up then leave oustanding_extents
1216 * be, since we've already accounted for the large extent.
Josef Bacik9ed74f22009-09-11 16:12:44 -04001217 */
1218 if (div64_u64(new_size + root->fs_info->max_extent - 1,
1219 root->fs_info->max_extent) < num_extents)
1220 return 0;
1221 }
1222
Josef Bacik32c00af2009-10-08 13:34:05 -04001223 spin_lock(&BTRFS_I(inode)->accounting_lock);
1224 BTRFS_I(inode)->outstanding_extents++;
1225 spin_unlock(&BTRFS_I(inode)->accounting_lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001226
1227 return 0;
1228}
1229
1230/*
1231 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1232 * extents so we can keep track of new extents that are just merged onto old
1233 * extents, such as when we are doing sequential writes, so we can properly
1234 * account for the metadata space we'll need.
1235 */
1236static int btrfs_merge_extent_hook(struct inode *inode,
1237 struct extent_state *new,
1238 struct extent_state *other)
1239{
1240 struct btrfs_root *root = BTRFS_I(inode)->root;
1241 u64 new_size, old_size;
1242 u64 num_extents;
1243
1244 /* not delalloc, ignore it */
1245 if (!(other->state & EXTENT_DELALLOC))
1246 return 0;
1247
1248 old_size = other->end - other->start + 1;
1249 if (new->start < other->start)
1250 new_size = other->end - new->start + 1;
1251 else
1252 new_size = new->end - other->start + 1;
1253
1254 /* we're not bigger than the max, unreserve the space and go */
1255 if (new_size <= root->fs_info->max_extent) {
Josef Bacik32c00af2009-10-08 13:34:05 -04001256 spin_lock(&BTRFS_I(inode)->accounting_lock);
1257 BTRFS_I(inode)->outstanding_extents--;
1258 spin_unlock(&BTRFS_I(inode)->accounting_lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001259 return 0;
1260 }
1261
1262 /*
1263 * If we grew by another max_extent, just return, we want to keep that
1264 * reserved amount.
1265 */
1266 num_extents = div64_u64(old_size + root->fs_info->max_extent - 1,
1267 root->fs_info->max_extent);
1268 if (div64_u64(new_size + root->fs_info->max_extent - 1,
1269 root->fs_info->max_extent) > num_extents)
1270 return 0;
1271
Josef Bacik32c00af2009-10-08 13:34:05 -04001272 spin_lock(&BTRFS_I(inode)->accounting_lock);
1273 BTRFS_I(inode)->outstanding_extents--;
1274 spin_unlock(&BTRFS_I(inode)->accounting_lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001275
1276 return 0;
1277}
1278
Chris Masond352ac62008-09-29 15:18:18 -04001279/*
1280 * extent_io.c set_bit_hook, used to track delayed allocation
1281 * bytes in this file, and to maintain the list of inodes that
1282 * have pending delalloc work to be done.
1283 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001284static int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -05001285 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -05001286{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001287
Chris Mason75eff682008-12-15 15:54:40 -05001288 /*
1289 * set_bit and clear bit hooks normally require _irqsave/restore
1290 * but in this case, we are only testeing for the DELALLOC
1291 * bit, which is only set or cleared with irqs on
1292 */
Chris Masonb0c68f82008-01-31 11:05:37 -05001293 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001294 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001295
Josef Bacik32c00af2009-10-08 13:34:05 -04001296 spin_lock(&BTRFS_I(inode)->accounting_lock);
1297 BTRFS_I(inode)->outstanding_extents++;
1298 spin_unlock(&BTRFS_I(inode)->accounting_lock);
Josef Bacik6a632092009-02-20 11:00:09 -05001299 btrfs_delalloc_reserve_space(root, inode, end - start + 1);
Chris Mason75eff682008-12-15 15:54:40 -05001300 spin_lock(&root->fs_info->delalloc_lock);
Chris Mason90692182008-02-08 13:49:28 -05001301 BTRFS_I(inode)->delalloc_bytes += end - start + 1;
Chris Mason291d6732008-01-29 15:55:23 -05001302 root->fs_info->delalloc_bytes += end - start + 1;
Chris Masonea8c2812008-08-04 23:17:27 -04001303 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1304 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1305 &root->fs_info->delalloc_inodes);
1306 }
Chris Mason75eff682008-12-15 15:54:40 -05001307 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001308 }
1309 return 0;
1310}
1311
Chris Masond352ac62008-09-29 15:18:18 -04001312/*
1313 * extent_io.c clear_bit_hook, see set_bit_hook for why
1314 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001315static int btrfs_clear_bit_hook(struct inode *inode,
1316 struct extent_state *state, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -05001317{
Chris Mason75eff682008-12-15 15:54:40 -05001318 /*
1319 * set_bit and clear bit hooks normally require _irqsave/restore
1320 * but in this case, we are only testeing for the DELALLOC
1321 * bit, which is only set or cleared with irqs on
1322 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001323 if ((state->state & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001324 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonbcbfce82008-04-22 13:26:47 -04001325
Josef Bacik32c00af2009-10-08 13:34:05 -04001326 if (bits & EXTENT_DO_ACCOUNTING) {
1327 spin_lock(&BTRFS_I(inode)->accounting_lock);
1328 BTRFS_I(inode)->outstanding_extents--;
1329 spin_unlock(&BTRFS_I(inode)->accounting_lock);
1330 btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
1331 }
Josef Bacik9ed74f22009-09-11 16:12:44 -04001332
Chris Mason75eff682008-12-15 15:54:40 -05001333 spin_lock(&root->fs_info->delalloc_lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001334 if (state->end - state->start + 1 >
1335 root->fs_info->delalloc_bytes) {
Chris Masond3977122009-01-05 21:25:51 -05001336 printk(KERN_INFO "btrfs warning: delalloc account "
1337 "%llu %llu\n",
Josef Bacik9ed74f22009-09-11 16:12:44 -04001338 (unsigned long long)
1339 state->end - state->start + 1,
Chris Masond3977122009-01-05 21:25:51 -05001340 (unsigned long long)
1341 root->fs_info->delalloc_bytes);
Josef Bacik6a632092009-02-20 11:00:09 -05001342 btrfs_delalloc_free_space(root, inode, (u64)-1);
Chris Masonb0c68f82008-01-31 11:05:37 -05001343 root->fs_info->delalloc_bytes = 0;
Chris Mason90692182008-02-08 13:49:28 -05001344 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masonb0c68f82008-01-31 11:05:37 -05001345 } else {
Josef Bacik6a632092009-02-20 11:00:09 -05001346 btrfs_delalloc_free_space(root, inode,
Josef Bacik9ed74f22009-09-11 16:12:44 -04001347 state->end -
1348 state->start + 1);
1349 root->fs_info->delalloc_bytes -= state->end -
1350 state->start + 1;
1351 BTRFS_I(inode)->delalloc_bytes -= state->end -
1352 state->start + 1;
Chris Masonb0c68f82008-01-31 11:05:37 -05001353 }
Chris Masonea8c2812008-08-04 23:17:27 -04001354 if (BTRFS_I(inode)->delalloc_bytes == 0 &&
1355 !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1356 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1357 }
Chris Mason75eff682008-12-15 15:54:40 -05001358 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001359 }
1360 return 0;
1361}
1362
Chris Masond352ac62008-09-29 15:18:18 -04001363/*
1364 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1365 * we don't create bios that span stripes or chunks
1366 */
Chris Mason239b14b2008-03-24 15:02:07 -04001367int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001368 size_t size, struct bio *bio,
1369 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001370{
1371 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
1372 struct btrfs_mapping_tree *map_tree;
Chris Masona62b9402008-10-03 16:31:08 -04001373 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001374 u64 length = 0;
1375 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001376 int ret;
1377
Chris Mason771ed682008-11-06 22:02:51 -05001378 if (bio_flags & EXTENT_BIO_COMPRESSED)
1379 return 0;
1380
Chris Masonf2d8d742008-04-21 10:03:05 -04001381 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001382 map_tree = &root->fs_info->mapping_tree;
1383 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04001384 ret = btrfs_map_block(map_tree, READ, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001385 &map_length, NULL, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04001386
Chris Masond3977122009-01-05 21:25:51 -05001387 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001388 return 1;
Chris Mason239b14b2008-03-24 15:02:07 -04001389 return 0;
1390}
1391
Chris Masond352ac62008-09-29 15:18:18 -04001392/*
1393 * in order to insert checksums into the metadata in large chunks,
1394 * we wait until bio submission time. All the pages in the bio are
1395 * checksummed and sums are attached onto the ordered extent record.
1396 *
1397 * At IO completion time the cums attached on the ordered extent record
1398 * are inserted into the btree
1399 */
Chris Masond3977122009-01-05 21:25:51 -05001400static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1401 struct bio *bio, int mirror_num,
1402 unsigned long bio_flags)
Chris Mason065631f2008-02-20 12:07:25 -05001403{
Chris Mason065631f2008-02-20 12:07:25 -05001404 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001405 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001406
Chris Masond20f7042008-12-08 16:58:54 -05001407 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Chris Mason44b8bd72008-04-16 11:14:51 -04001408 BUG_ON(ret);
Chris Mason4a69a412008-11-06 22:03:00 -05001409 return 0;
1410}
Chris Masone0156402008-04-16 11:15:20 -04001411
Chris Mason4a69a412008-11-06 22:03:00 -05001412/*
1413 * in order to insert checksums into the metadata in large chunks,
1414 * we wait until bio submission time. All the pages in the bio are
1415 * checksummed and sums are attached onto the ordered extent record.
1416 *
1417 * At IO completion time the cums attached on the ordered extent record
1418 * are inserted into the btree
1419 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001420static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Mason4a69a412008-11-06 22:03:00 -05001421 int mirror_num, unsigned long bio_flags)
1422{
1423 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason8b712842008-06-11 16:50:36 -04001424 return btrfs_map_bio(root, rw, bio, mirror_num, 1);
Chris Mason44b8bd72008-04-16 11:14:51 -04001425}
1426
Chris Masond352ac62008-09-29 15:18:18 -04001427/*
Chris Masoncad321a2008-12-17 14:51:42 -05001428 * extent_io.c submission hook. This does the right thing for csum calculation
1429 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001430 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001431static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masonc8b97812008-10-29 14:49:59 -04001432 int mirror_num, unsigned long bio_flags)
Chris Mason44b8bd72008-04-16 11:14:51 -04001433{
1434 struct btrfs_root *root = BTRFS_I(inode)->root;
1435 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001436 int skip_sum;
Chris Mason44b8bd72008-04-16 11:14:51 -04001437
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001438 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001439
Chris Masone6dcd2d2008-07-17 12:53:50 -04001440 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
1441 BUG_ON(ret);
Chris Mason065631f2008-02-20 12:07:25 -05001442
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001443 if (!(rw & (1 << BIO_RW))) {
Chris Masond20f7042008-12-08 16:58:54 -05001444 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Chris Masonc8b97812008-10-29 14:49:59 -04001445 return btrfs_submit_compressed_read(inode, bio,
1446 mirror_num, bio_flags);
Chris Masond20f7042008-12-08 16:58:54 -05001447 } else if (!skip_sum)
1448 btrfs_lookup_bio_sums(root, inode, bio, NULL);
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001449 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001450 } else if (!skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001451 /* csum items have already been cloned */
1452 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1453 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001454 /* we're doing a write, do the async checksumming */
1455 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001456 inode, rw, bio, mirror_num,
Chris Mason4a69a412008-11-06 22:03:00 -05001457 bio_flags, __btrfs_submit_bio_start,
1458 __btrfs_submit_bio_done);
Chris Mason19b9bdb2008-10-30 14:23:13 -04001459 }
1460
Chris Mason0b86a832008-03-24 15:01:56 -04001461mapit:
Chris Mason8b712842008-06-11 16:50:36 -04001462 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
Chris Mason065631f2008-02-20 12:07:25 -05001463}
Chris Mason6885f302008-02-20 16:11:05 -05001464
Chris Masond352ac62008-09-29 15:18:18 -04001465/*
1466 * given a list of ordered sums record them in the inode. This happens
1467 * at IO completion time based on sums calculated at bio submission time.
1468 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001469static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001470 struct inode *inode, u64 file_offset,
1471 struct list_head *list)
1472{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001473 struct btrfs_ordered_sum *sum;
1474
1475 btrfs_set_trans_block_group(trans, inode);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001476
1477 list_for_each_entry(sum, list, list) {
Chris Masond20f7042008-12-08 16:58:54 -05001478 btrfs_csum_file_blocks(trans,
1479 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001480 }
1481 return 0;
1482}
1483
Chris Masonea8c2812008-08-04 23:17:27 -04001484int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end)
1485{
Chris Masond3977122009-01-05 21:25:51 -05001486 if ((end & (PAGE_CACHE_SIZE - 1)) == 0)
Chris Mason771ed682008-11-06 22:02:51 -05001487 WARN_ON(1);
Chris Masonea8c2812008-08-04 23:17:27 -04001488 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
1489 GFP_NOFS);
1490}
1491
Chris Masond352ac62008-09-29 15:18:18 -04001492/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001493struct btrfs_writepage_fixup {
1494 struct page *page;
1495 struct btrfs_work work;
1496};
1497
Christoph Hellwigb2950862008-12-02 09:54:17 -05001498static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001499{
1500 struct btrfs_writepage_fixup *fixup;
1501 struct btrfs_ordered_extent *ordered;
1502 struct page *page;
1503 struct inode *inode;
1504 u64 page_start;
1505 u64 page_end;
1506
1507 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1508 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001509again:
Chris Mason247e7432008-07-17 12:53:51 -04001510 lock_page(page);
1511 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1512 ClearPageChecked(page);
1513 goto out_page;
1514 }
1515
1516 inode = page->mapping->host;
1517 page_start = page_offset(page);
1518 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1519
1520 lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001521
1522 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001523 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001524 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001525
1526 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1527 if (ordered) {
1528 unlock_extent(&BTRFS_I(inode)->io_tree, page_start,
1529 page_end, GFP_NOFS);
1530 unlock_page(page);
1531 btrfs_start_ordered_extent(inode, ordered, 1);
1532 goto again;
1533 }
Chris Mason247e7432008-07-17 12:53:51 -04001534
Chris Masonea8c2812008-08-04 23:17:27 -04001535 btrfs_set_extent_delalloc(inode, page_start, page_end);
Chris Mason247e7432008-07-17 12:53:51 -04001536 ClearPageChecked(page);
1537out:
1538 unlock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
1539out_page:
1540 unlock_page(page);
1541 page_cache_release(page);
1542}
1543
1544/*
1545 * There are a few paths in the higher layers of the kernel that directly
1546 * set the page dirty bit without asking the filesystem if it is a
1547 * good idea. This causes problems because we want to make sure COW
1548 * properly happens and the data=ordered rules are followed.
1549 *
Chris Masonc8b97812008-10-29 14:49:59 -04001550 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001551 * hasn't been properly setup for IO. We kick off an async process
1552 * to fix it up. The async helper will wait for ordered extents, set
1553 * the delalloc bit and make it safe to write the page.
1554 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001555static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001556{
1557 struct inode *inode = page->mapping->host;
1558 struct btrfs_writepage_fixup *fixup;
1559 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001560
Chris Mason8b62b722009-09-02 16:53:46 -04001561 /* this page is properly in the ordered list */
1562 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001563 return 0;
1564
1565 if (PageChecked(page))
1566 return -EAGAIN;
1567
1568 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1569 if (!fixup)
1570 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001571
Chris Mason247e7432008-07-17 12:53:51 -04001572 SetPageChecked(page);
1573 page_cache_get(page);
1574 fixup->work.func = btrfs_writepage_fixup_worker;
1575 fixup->page = page;
1576 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
1577 return -EAGAIN;
1578}
1579
Yan Zhengd899e052008-10-30 14:25:28 -04001580static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1581 struct inode *inode, u64 file_pos,
1582 u64 disk_bytenr, u64 disk_num_bytes,
1583 u64 num_bytes, u64 ram_bytes,
Chris Masone980b502009-04-24 14:39:24 -04001584 u64 locked_end,
Yan Zhengd899e052008-10-30 14:25:28 -04001585 u8 compression, u8 encryption,
1586 u16 other_encoding, int extent_type)
1587{
1588 struct btrfs_root *root = BTRFS_I(inode)->root;
1589 struct btrfs_file_extent_item *fi;
1590 struct btrfs_path *path;
1591 struct extent_buffer *leaf;
1592 struct btrfs_key ins;
1593 u64 hint;
1594 int ret;
1595
1596 path = btrfs_alloc_path();
1597 BUG_ON(!path);
1598
Chris Masonb9473432009-03-13 11:00:37 -04001599 path->leave_spinning = 1;
Chris Masona1ed8352009-09-11 12:27:37 -04001600
1601 /*
1602 * we may be replacing one extent in the tree with another.
1603 * The new extent is pinned in the extent map, and we don't want
1604 * to drop it from the cache until it is completely in the btree.
1605 *
1606 * So, tell btrfs_drop_extents to leave this extent in the cache.
1607 * the caller is expected to unpin it and allow it to be merged
1608 * with the others.
1609 */
Yan Zhengd899e052008-10-30 14:25:28 -04001610 ret = btrfs_drop_extents(trans, root, inode, file_pos,
Chris Masone980b502009-04-24 14:39:24 -04001611 file_pos + num_bytes, locked_end,
Chris Masona1ed8352009-09-11 12:27:37 -04001612 file_pos, &hint, 0);
Yan Zhengd899e052008-10-30 14:25:28 -04001613 BUG_ON(ret);
1614
1615 ins.objectid = inode->i_ino;
1616 ins.offset = file_pos;
1617 ins.type = BTRFS_EXTENT_DATA_KEY;
1618 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
1619 BUG_ON(ret);
1620 leaf = path->nodes[0];
1621 fi = btrfs_item_ptr(leaf, path->slots[0],
1622 struct btrfs_file_extent_item);
1623 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1624 btrfs_set_file_extent_type(leaf, fi, extent_type);
1625 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1626 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1627 btrfs_set_file_extent_offset(leaf, fi, 0);
1628 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1629 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1630 btrfs_set_file_extent_compression(leaf, fi, compression);
1631 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1632 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04001633
1634 btrfs_unlock_up_safe(path, 1);
1635 btrfs_set_lock_blocking(leaf);
1636
Yan Zhengd899e052008-10-30 14:25:28 -04001637 btrfs_mark_buffer_dirty(leaf);
1638
1639 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04001640
1641 ins.objectid = disk_bytenr;
1642 ins.offset = disk_num_bytes;
1643 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001644 ret = btrfs_alloc_reserved_file_extent(trans, root,
1645 root->root_key.objectid,
1646 inode->i_ino, file_pos, &ins);
Yan Zhengd899e052008-10-30 14:25:28 -04001647 BUG_ON(ret);
Yan Zhengd899e052008-10-30 14:25:28 -04001648 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04001649
Yan Zhengd899e052008-10-30 14:25:28 -04001650 return 0;
1651}
1652
Chris Mason5d13a982009-03-13 11:41:46 -04001653/*
1654 * helper function for btrfs_finish_ordered_io, this
1655 * just reads in some of the csum leaves to prime them into ram
1656 * before we start the transaction. It limits the amount of btree
1657 * reads required while inside the transaction.
1658 */
1659static noinline void reada_csum(struct btrfs_root *root,
1660 struct btrfs_path *path,
1661 struct btrfs_ordered_extent *ordered_extent)
1662{
1663 struct btrfs_ordered_sum *sum;
1664 u64 bytenr;
1665
1666 sum = list_entry(ordered_extent->list.next, struct btrfs_ordered_sum,
1667 list);
1668 bytenr = sum->sums[0].bytenr;
1669
1670 /*
1671 * we don't care about the results, the point of this search is
1672 * just to get the btree leaves into ram
1673 */
1674 btrfs_lookup_csum(NULL, root->fs_info->csum_root, path, bytenr, 0);
1675}
1676
Chris Masond352ac62008-09-29 15:18:18 -04001677/* as ordered data IO finishes, this gets called so we can finish
1678 * an ordered extent if the range of bytes in the file it covers are
1679 * fully written.
1680 */
Chris Mason211f90e2008-07-18 11:56:15 -04001681static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001682{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001683 struct btrfs_root *root = BTRFS_I(inode)->root;
1684 struct btrfs_trans_handle *trans;
Chris Mason5d13a982009-03-13 11:41:46 -04001685 struct btrfs_ordered_extent *ordered_extent = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001686 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonb7ec40d2009-03-12 20:12:45 -04001687 struct btrfs_path *path;
Yan Zhengd899e052008-10-30 14:25:28 -04001688 int compressed = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001689 int ret;
1690
1691 ret = btrfs_dec_test_ordered_pending(inode, start, end - start + 1);
Chris Masonba1da2f2008-07-17 12:54:15 -04001692 if (!ret)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001693 return 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001694
Chris Masonb7ec40d2009-03-12 20:12:45 -04001695 /*
1696 * before we join the transaction, try to do some of our IO.
1697 * This will limit the amount of IO that we have to do with
1698 * the transaction running. We're unlikely to need to do any
1699 * IO if the file extents are new, the disk_i_size checks
1700 * covers the most common case.
1701 */
1702 if (start < BTRFS_I(inode)->disk_i_size) {
1703 path = btrfs_alloc_path();
1704 if (path) {
1705 ret = btrfs_lookup_file_extent(NULL, root, path,
1706 inode->i_ino,
1707 start, 0);
Chris Mason5d13a982009-03-13 11:41:46 -04001708 ordered_extent = btrfs_lookup_ordered_extent(inode,
1709 start);
1710 if (!list_empty(&ordered_extent->list)) {
1711 btrfs_release_path(root, path);
1712 reada_csum(root, path, ordered_extent);
1713 }
Chris Masonb7ec40d2009-03-12 20:12:45 -04001714 btrfs_free_path(path);
1715 }
1716 }
1717
Chris Masonf9295742008-07-17 12:54:14 -04001718 trans = btrfs_join_transaction(root, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001719
Chris Mason5d13a982009-03-13 11:41:46 -04001720 if (!ordered_extent)
1721 ordered_extent = btrfs_lookup_ordered_extent(inode, start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001722 BUG_ON(!ordered_extent);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001723 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags))
1724 goto nocow;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001725
1726 lock_extent(io_tree, ordered_extent->file_offset,
1727 ordered_extent->file_offset + ordered_extent->len - 1,
1728 GFP_NOFS);
1729
Chris Masonc8b97812008-10-29 14:49:59 -04001730 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Yan Zhengd899e052008-10-30 14:25:28 -04001731 compressed = 1;
1732 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
1733 BUG_ON(compressed);
1734 ret = btrfs_mark_extent_written(trans, root, inode,
1735 ordered_extent->file_offset,
1736 ordered_extent->file_offset +
1737 ordered_extent->len);
1738 BUG_ON(ret);
1739 } else {
1740 ret = insert_reserved_file_extent(trans, inode,
1741 ordered_extent->file_offset,
1742 ordered_extent->start,
1743 ordered_extent->disk_len,
1744 ordered_extent->len,
1745 ordered_extent->len,
Chris Masone980b502009-04-24 14:39:24 -04001746 ordered_extent->file_offset +
1747 ordered_extent->len,
Yan Zhengd899e052008-10-30 14:25:28 -04001748 compressed, 0, 0,
1749 BTRFS_FILE_EXTENT_REG);
Chris Masona1ed8352009-09-11 12:27:37 -04001750 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
1751 ordered_extent->file_offset,
1752 ordered_extent->len);
Yan Zhengd899e052008-10-30 14:25:28 -04001753 BUG_ON(ret);
1754 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04001755 unlock_extent(io_tree, ordered_extent->file_offset,
1756 ordered_extent->file_offset + ordered_extent->len - 1,
1757 GFP_NOFS);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001758nocow:
Chris Masone6dcd2d2008-07-17 12:53:50 -04001759 add_pending_csums(trans, inode, ordered_extent->file_offset,
1760 &ordered_extent->list);
1761
Chris Mason34353022008-09-23 20:19:49 -04001762 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Chris Masondbe674a2008-07-17 12:54:05 -04001763 btrfs_ordered_update_i_size(inode, ordered_extent);
Chris Masone02119d2008-09-05 16:13:11 -04001764 btrfs_update_inode(trans, root, inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001765 btrfs_remove_ordered_extent(inode, ordered_extent);
Chris Mason34353022008-09-23 20:19:49 -04001766 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
Chris Mason7f3c74f2008-07-18 12:01:11 -04001767
Chris Masone6dcd2d2008-07-17 12:53:50 -04001768 /* once for us */
1769 btrfs_put_ordered_extent(ordered_extent);
1770 /* once for the tree */
1771 btrfs_put_ordered_extent(ordered_extent);
1772
Chris Masone6dcd2d2008-07-17 12:53:50 -04001773 btrfs_end_transaction(trans, root);
1774 return 0;
1775}
1776
Christoph Hellwigb2950862008-12-02 09:54:17 -05001777static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04001778 struct extent_state *state, int uptodate)
1779{
Chris Mason8b62b722009-09-02 16:53:46 -04001780 ClearPagePrivate2(page);
Chris Mason211f90e2008-07-18 11:56:15 -04001781 return btrfs_finish_ordered_io(page->mapping->host, start, end);
1782}
1783
Chris Masond352ac62008-09-29 15:18:18 -04001784/*
1785 * When IO fails, either with EIO or csum verification fails, we
1786 * try other mirrors that might have a good copy of the data. This
1787 * io_failure_record is used to record state as we go through all the
1788 * mirrors. If another mirror has good data, the page is set up to date
1789 * and things continue. If a good mirror can't be found, the original
1790 * bio end_io callback is called to indicate things have failed.
1791 */
Chris Mason7e383262008-04-09 16:28:12 -04001792struct io_failure_record {
1793 struct page *page;
1794 u64 start;
1795 u64 len;
1796 u64 logical;
Chris Masond20f7042008-12-08 16:58:54 -05001797 unsigned long bio_flags;
Chris Mason7e383262008-04-09 16:28:12 -04001798 int last_mirror;
1799};
1800
Christoph Hellwigb2950862008-12-02 09:54:17 -05001801static int btrfs_io_failed_hook(struct bio *failed_bio,
Chris Mason1259ab72008-05-12 13:39:03 -04001802 struct page *page, u64 start, u64 end,
1803 struct extent_state *state)
Chris Mason7e383262008-04-09 16:28:12 -04001804{
1805 struct io_failure_record *failrec = NULL;
1806 u64 private;
1807 struct extent_map *em;
1808 struct inode *inode = page->mapping->host;
1809 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Chris Mason3b951512008-04-17 11:29:12 -04001810 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason7e383262008-04-09 16:28:12 -04001811 struct bio *bio;
1812 int num_copies;
1813 int ret;
Chris Mason1259ab72008-05-12 13:39:03 -04001814 int rw;
Chris Mason7e383262008-04-09 16:28:12 -04001815 u64 logical;
1816
1817 ret = get_state_private(failure_tree, start, &private);
1818 if (ret) {
Chris Mason7e383262008-04-09 16:28:12 -04001819 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
1820 if (!failrec)
1821 return -ENOMEM;
1822 failrec->start = start;
1823 failrec->len = end - start + 1;
1824 failrec->last_mirror = 0;
Chris Masond20f7042008-12-08 16:58:54 -05001825 failrec->bio_flags = 0;
Chris Mason7e383262008-04-09 16:28:12 -04001826
Chris Mason890871b2009-09-02 16:24:52 -04001827 read_lock(&em_tree->lock);
Chris Mason3b951512008-04-17 11:29:12 -04001828 em = lookup_extent_mapping(em_tree, start, failrec->len);
1829 if (em->start > start || em->start + em->len < start) {
1830 free_extent_map(em);
1831 em = NULL;
1832 }
Chris Mason890871b2009-09-02 16:24:52 -04001833 read_unlock(&em_tree->lock);
Chris Mason7e383262008-04-09 16:28:12 -04001834
1835 if (!em || IS_ERR(em)) {
1836 kfree(failrec);
1837 return -EIO;
1838 }
1839 logical = start - em->start;
1840 logical = em->block_start + logical;
Chris Masond20f7042008-12-08 16:58:54 -05001841 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
1842 logical = em->block_start;
1843 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
1844 }
Chris Mason7e383262008-04-09 16:28:12 -04001845 failrec->logical = logical;
1846 free_extent_map(em);
1847 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
1848 EXTENT_DIRTY, GFP_NOFS);
Chris Mason587f7702008-04-11 12:16:46 -04001849 set_state_private(failure_tree, start,
1850 (u64)(unsigned long)failrec);
Chris Mason7e383262008-04-09 16:28:12 -04001851 } else {
Chris Mason587f7702008-04-11 12:16:46 -04001852 failrec = (struct io_failure_record *)(unsigned long)private;
Chris Mason7e383262008-04-09 16:28:12 -04001853 }
1854 num_copies = btrfs_num_copies(
1855 &BTRFS_I(inode)->root->fs_info->mapping_tree,
1856 failrec->logical, failrec->len);
1857 failrec->last_mirror++;
1858 if (!state) {
Chris Masoncad321a2008-12-17 14:51:42 -05001859 spin_lock(&BTRFS_I(inode)->io_tree.lock);
Chris Mason7e383262008-04-09 16:28:12 -04001860 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
1861 failrec->start,
1862 EXTENT_LOCKED);
1863 if (state && state->start != failrec->start)
1864 state = NULL;
Chris Masoncad321a2008-12-17 14:51:42 -05001865 spin_unlock(&BTRFS_I(inode)->io_tree.lock);
Chris Mason7e383262008-04-09 16:28:12 -04001866 }
1867 if (!state || failrec->last_mirror > num_copies) {
1868 set_state_private(failure_tree, failrec->start, 0);
1869 clear_extent_bits(failure_tree, failrec->start,
1870 failrec->start + failrec->len - 1,
1871 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
1872 kfree(failrec);
1873 return -EIO;
1874 }
1875 bio = bio_alloc(GFP_NOFS, 1);
1876 bio->bi_private = state;
1877 bio->bi_end_io = failed_bio->bi_end_io;
1878 bio->bi_sector = failrec->logical >> 9;
1879 bio->bi_bdev = failed_bio->bi_bdev;
Chris Masone1c4b742008-04-22 13:26:46 -04001880 bio->bi_size = 0;
Chris Masond20f7042008-12-08 16:58:54 -05001881
Chris Mason7e383262008-04-09 16:28:12 -04001882 bio_add_page(bio, page, failrec->len, start - page_offset(page));
Chris Mason1259ab72008-05-12 13:39:03 -04001883 if (failed_bio->bi_rw & (1 << BIO_RW))
1884 rw = WRITE;
1885 else
1886 rw = READ;
1887
1888 BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
Chris Masonc8b97812008-10-29 14:49:59 -04001889 failrec->last_mirror,
Chris Masond20f7042008-12-08 16:58:54 -05001890 failrec->bio_flags);
Chris Mason1259ab72008-05-12 13:39:03 -04001891 return 0;
1892}
1893
Chris Masond352ac62008-09-29 15:18:18 -04001894/*
1895 * each time an IO finishes, we do a fast check in the IO failure tree
1896 * to see if we need to process or clean up an io_failure_record
1897 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001898static int btrfs_clean_io_failures(struct inode *inode, u64 start)
Chris Mason1259ab72008-05-12 13:39:03 -04001899{
1900 u64 private;
1901 u64 private_failure;
1902 struct io_failure_record *failure;
1903 int ret;
1904
1905 private = 0;
1906 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
1907 (u64)-1, 1, EXTENT_DIRTY)) {
1908 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
1909 start, &private_failure);
1910 if (ret == 0) {
1911 failure = (struct io_failure_record *)(unsigned long)
1912 private_failure;
1913 set_state_private(&BTRFS_I(inode)->io_failure_tree,
1914 failure->start, 0);
1915 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
1916 failure->start,
1917 failure->start + failure->len - 1,
1918 EXTENT_DIRTY | EXTENT_LOCKED,
1919 GFP_NOFS);
1920 kfree(failure);
1921 }
1922 }
Chris Mason7e383262008-04-09 16:28:12 -04001923 return 0;
1924}
1925
Chris Masond352ac62008-09-29 15:18:18 -04001926/*
1927 * when reads are done, we need to check csums to verify the data is correct
1928 * if there's a match, we allow the bio to finish. If not, we go through
1929 * the io_failure_record routines to find good copies
1930 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001931static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason70dec802008-01-29 09:59:12 -05001932 struct extent_state *state)
Chris Mason07157aa2007-08-30 08:50:51 -04001933{
Chris Mason35ebb932007-10-30 16:56:53 -04001934 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -04001935 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05001936 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04001937 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -05001938 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -04001939 int ret;
Chris Masonff79f812007-10-15 16:22:25 -04001940 struct btrfs_root *root = BTRFS_I(inode)->root;
1941 u32 csum = ~(u32)0;
Chris Masond1310b22008-01-24 16:13:08 -05001942
Chris Masond20f7042008-12-08 16:58:54 -05001943 if (PageChecked(page)) {
1944 ClearPageChecked(page);
1945 goto good;
1946 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001947
1948 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Chris Masonb6cda9b2007-12-14 15:30:32 -05001949 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05001950
Yan Zheng17d217f2008-12-12 10:03:38 -05001951 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04001952 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001953 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
1954 GFP_NOFS);
1955 return 0;
1956 }
1957
Yanc2e639f2008-02-04 08:57:25 -05001958 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -05001959 private = state->private;
1960 ret = 0;
1961 } else {
1962 ret = get_state_private(io_tree, start, &private);
1963 }
Chris Mason9ab86c82009-01-07 09:48:51 -05001964 kaddr = kmap_atomic(page, KM_USER0);
Chris Masond3977122009-01-05 21:25:51 -05001965 if (ret)
Chris Mason07157aa2007-08-30 08:50:51 -04001966 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05001967
Chris Masonff79f812007-10-15 16:22:25 -04001968 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
1969 btrfs_csum_final(csum, (char *)&csum);
Chris Masond3977122009-01-05 21:25:51 -05001970 if (csum != private)
Chris Mason07157aa2007-08-30 08:50:51 -04001971 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05001972
Chris Mason9ab86c82009-01-07 09:48:51 -05001973 kunmap_atomic(kaddr, KM_USER0);
Chris Masond20f7042008-12-08 16:58:54 -05001974good:
Chris Mason7e383262008-04-09 16:28:12 -04001975 /* if the io failure tree for this inode is non-empty,
1976 * check to see if we've recovered from a failed IO
1977 */
Chris Mason1259ab72008-05-12 13:39:03 -04001978 btrfs_clean_io_failures(inode, start);
Chris Mason07157aa2007-08-30 08:50:51 -04001979 return 0;
1980
1981zeroit:
Chris Mason193f2842009-04-27 07:29:05 -04001982 if (printk_ratelimit()) {
1983 printk(KERN_INFO "btrfs csum failed ino %lu off %llu csum %u "
1984 "private %llu\n", page->mapping->host->i_ino,
1985 (unsigned long long)start, csum,
1986 (unsigned long long)private);
1987 }
Chris Masondb945352007-10-15 16:15:53 -04001988 memset(kaddr + offset, 1, end - start + 1);
1989 flush_dcache_page(page);
Chris Mason9ab86c82009-01-07 09:48:51 -05001990 kunmap_atomic(kaddr, KM_USER0);
Chris Mason3b951512008-04-17 11:29:12 -04001991 if (private == 0)
1992 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04001993 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04001994}
Chris Masonb888db22007-08-27 16:49:44 -04001995
Josef Bacik7b128762008-07-24 12:17:14 -04001996/*
1997 * This creates an orphan entry for the given inode in case something goes
1998 * wrong in the middle of an unlink/truncate.
1999 */
2000int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2001{
2002 struct btrfs_root *root = BTRFS_I(inode)->root;
2003 int ret = 0;
2004
Yanbcc63ab2008-07-30 16:29:20 -04002005 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002006
2007 /* already on the orphan list, we're good */
2008 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
Yanbcc63ab2008-07-30 16:29:20 -04002009 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002010 return 0;
2011 }
2012
2013 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
2014
Yanbcc63ab2008-07-30 16:29:20 -04002015 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002016
2017 /*
2018 * insert an orphan item to track this unlinked/truncated file
2019 */
2020 ret = btrfs_insert_orphan_item(trans, root, inode->i_ino);
2021
2022 return ret;
2023}
2024
2025/*
2026 * We have done the truncate/delete so we can go ahead and remove the orphan
2027 * item for this particular inode.
2028 */
2029int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
2030{
2031 struct btrfs_root *root = BTRFS_I(inode)->root;
2032 int ret = 0;
2033
Yanbcc63ab2008-07-30 16:29:20 -04002034 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002035
2036 if (list_empty(&BTRFS_I(inode)->i_orphan)) {
Yanbcc63ab2008-07-30 16:29:20 -04002037 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002038 return 0;
2039 }
2040
2041 list_del_init(&BTRFS_I(inode)->i_orphan);
2042 if (!trans) {
Yanbcc63ab2008-07-30 16:29:20 -04002043 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002044 return 0;
2045 }
2046
Yanbcc63ab2008-07-30 16:29:20 -04002047 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002048
2049 ret = btrfs_del_orphan_item(trans, root, inode->i_ino);
2050
2051 return ret;
2052}
2053
2054/*
2055 * this cleans up any orphans that may be left on the list from the last use
2056 * of this root.
2057 */
2058void btrfs_orphan_cleanup(struct btrfs_root *root)
2059{
2060 struct btrfs_path *path;
2061 struct extent_buffer *leaf;
2062 struct btrfs_item *item;
2063 struct btrfs_key key, found_key;
2064 struct btrfs_trans_handle *trans;
2065 struct inode *inode;
2066 int ret = 0, nr_unlink = 0, nr_truncate = 0;
2067
Josef Bacik7b128762008-07-24 12:17:14 -04002068 path = btrfs_alloc_path();
2069 if (!path)
2070 return;
2071 path->reada = -1;
2072
2073 key.objectid = BTRFS_ORPHAN_OBJECTID;
2074 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
2075 key.offset = (u64)-1;
2076
Josef Bacik7b128762008-07-24 12:17:14 -04002077
2078 while (1) {
2079 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2080 if (ret < 0) {
2081 printk(KERN_ERR "Error searching slot for orphan: %d"
2082 "\n", ret);
2083 break;
2084 }
2085
2086 /*
2087 * if ret == 0 means we found what we were searching for, which
2088 * is weird, but possible, so only screw with path if we didnt
2089 * find the key and see if we have stuff that matches
2090 */
2091 if (ret > 0) {
2092 if (path->slots[0] == 0)
2093 break;
2094 path->slots[0]--;
2095 }
2096
2097 /* pull out the item */
2098 leaf = path->nodes[0];
2099 item = btrfs_item_nr(leaf, path->slots[0]);
2100 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2101
2102 /* make sure the item matches what we want */
2103 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
2104 break;
2105 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
2106 break;
2107
2108 /* release the path since we're done with it */
2109 btrfs_release_path(root, path);
2110
2111 /*
2112 * this is where we are basically btrfs_lookup, without the
2113 * crossing root thing. we store the inode number in the
2114 * offset of the orphan item.
2115 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002116 found_key.objectid = found_key.offset;
2117 found_key.type = BTRFS_INODE_ITEM_KEY;
2118 found_key.offset = 0;
2119 inode = btrfs_iget(root->fs_info->sb, &found_key, root);
2120 if (IS_ERR(inode))
Josef Bacik7b128762008-07-24 12:17:14 -04002121 break;
2122
Josef Bacik7b128762008-07-24 12:17:14 -04002123 /*
2124 * add this inode to the orphan list so btrfs_orphan_del does
2125 * the proper thing when we hit it
2126 */
Yanbcc63ab2008-07-30 16:29:20 -04002127 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002128 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
Yanbcc63ab2008-07-30 16:29:20 -04002129 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002130
2131 /*
2132 * if this is a bad inode, means we actually succeeded in
2133 * removing the inode, but not the orphan record, which means
2134 * we need to manually delete the orphan since iput will just
2135 * do a destroy_inode
2136 */
2137 if (is_bad_inode(inode)) {
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002138 trans = btrfs_start_transaction(root, 1);
Josef Bacik7b128762008-07-24 12:17:14 -04002139 btrfs_orphan_del(trans, inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002140 btrfs_end_transaction(trans, root);
Josef Bacik7b128762008-07-24 12:17:14 -04002141 iput(inode);
2142 continue;
2143 }
2144
2145 /* if we have links, this was a truncate, lets do that */
2146 if (inode->i_nlink) {
2147 nr_truncate++;
2148 btrfs_truncate(inode);
2149 } else {
2150 nr_unlink++;
2151 }
2152
2153 /* this will do delete_inode and everything for us */
2154 iput(inode);
2155 }
2156
2157 if (nr_unlink)
2158 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
2159 if (nr_truncate)
2160 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
2161
2162 btrfs_free_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04002163}
2164
Chris Masond352ac62008-09-29 15:18:18 -04002165/*
Chris Mason46a53cc2009-04-27 11:47:50 -04002166 * very simple check to peek ahead in the leaf looking for xattrs. If we
2167 * don't find any xattrs, we know there can't be any acls.
2168 *
2169 * slot is the slot the inode is in, objectid is the objectid of the inode
2170 */
2171static noinline int acls_after_inode_item(struct extent_buffer *leaf,
2172 int slot, u64 objectid)
2173{
2174 u32 nritems = btrfs_header_nritems(leaf);
2175 struct btrfs_key found_key;
2176 int scanned = 0;
2177
2178 slot++;
2179 while (slot < nritems) {
2180 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2181
2182 /* we found a different objectid, there must not be acls */
2183 if (found_key.objectid != objectid)
2184 return 0;
2185
2186 /* we found an xattr, assume we've got an acl */
2187 if (found_key.type == BTRFS_XATTR_ITEM_KEY)
2188 return 1;
2189
2190 /*
2191 * we found a key greater than an xattr key, there can't
2192 * be any acls later on
2193 */
2194 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
2195 return 0;
2196
2197 slot++;
2198 scanned++;
2199
2200 /*
2201 * it goes inode, inode backrefs, xattrs, extents,
2202 * so if there are a ton of hard links to an inode there can
2203 * be a lot of backrefs. Don't waste time searching too hard,
2204 * this is just an optimization
2205 */
2206 if (scanned >= 8)
2207 break;
2208 }
2209 /* we hit the end of the leaf before we found an xattr or
2210 * something larger than an xattr. We have to assume the inode
2211 * has acls
2212 */
2213 return 1;
2214}
2215
2216/*
Chris Masond352ac62008-09-29 15:18:18 -04002217 * read an inode from the btree into the in-memory inode
2218 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002219static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002220{
2221 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002222 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002223 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04002224 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04002225 struct btrfs_root *root = BTRFS_I(inode)->root;
2226 struct btrfs_key location;
Chris Mason46a53cc2009-04-27 11:47:50 -04002227 int maybe_acls;
Chris Mason39279cc2007-06-12 06:35:45 -04002228 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -04002229 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04002230 int ret;
2231
2232 path = btrfs_alloc_path();
2233 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -04002234 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05002235
Chris Mason39279cc2007-06-12 06:35:45 -04002236 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002237 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04002238 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04002239
Chris Mason5f39d392007-10-15 16:14:19 -04002240 leaf = path->nodes[0];
2241 inode_item = btrfs_item_ptr(leaf, path->slots[0],
2242 struct btrfs_inode_item);
2243
2244 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
2245 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
2246 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
2247 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
Chris Masondbe674a2008-07-17 12:54:05 -04002248 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04002249
2250 tspec = btrfs_inode_atime(inode_item);
2251 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2252 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2253
2254 tspec = btrfs_inode_mtime(inode_item);
2255 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2256 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2257
2258 tspec = btrfs_inode_ctime(inode_item);
2259 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2260 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2261
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002262 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04002263 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Chris Masonc3027eb2008-12-08 16:40:21 -05002264 BTRFS_I(inode)->sequence = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04002265 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04002266 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002267 rdev = btrfs_inode_rdev(leaf, inode_item);
2268
Josef Bacikaec74772008-07-24 12:12:38 -04002269 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05002270 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Josef Bacikaec74772008-07-24 12:12:38 -04002271
Chris Mason5f39d392007-10-15 16:14:19 -04002272 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002273
Chris Mason46a53cc2009-04-27 11:47:50 -04002274 /*
2275 * try to precache a NULL acl entry for files that don't have
2276 * any xattrs or acls
2277 */
2278 maybe_acls = acls_after_inode_item(leaf, path->slots[0], inode->i_ino);
Al Viro72c04902009-06-24 16:58:48 -04002279 if (!maybe_acls)
2280 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04002281
Yan Zhengd2fb3432008-12-11 16:30:39 -05002282 BTRFS_I(inode)->block_group = btrfs_find_block_group(root, 0,
2283 alloc_group_block, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002284 btrfs_free_path(path);
2285 inode_item = NULL;
2286
Chris Mason39279cc2007-06-12 06:35:45 -04002287 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04002288 case S_IFREG:
2289 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04002290 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05002291 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002292 inode->i_fop = &btrfs_file_operations;
2293 inode->i_op = &btrfs_file_inode_operations;
2294 break;
2295 case S_IFDIR:
2296 inode->i_fop = &btrfs_dir_file_operations;
2297 if (root == root->fs_info->tree_root)
2298 inode->i_op = &btrfs_dir_ro_inode_operations;
2299 else
2300 inode->i_op = &btrfs_dir_inode_operations;
2301 break;
2302 case S_IFLNK:
2303 inode->i_op = &btrfs_symlink_inode_operations;
2304 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04002305 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04002306 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04002307 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05002308 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04002309 init_special_inode(inode, inode->i_mode, rdev);
2310 break;
Chris Mason39279cc2007-06-12 06:35:45 -04002311 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002312
2313 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002314 return;
2315
2316make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04002317 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002318 make_bad_inode(inode);
2319}
2320
Chris Masond352ac62008-09-29 15:18:18 -04002321/*
2322 * given a leaf and an inode, copy the inode fields into the leaf
2323 */
Chris Masone02119d2008-09-05 16:13:11 -04002324static void fill_inode_item(struct btrfs_trans_handle *trans,
2325 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04002326 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04002327 struct inode *inode)
2328{
Chris Mason5f39d392007-10-15 16:14:19 -04002329 btrfs_set_inode_uid(leaf, item, inode->i_uid);
2330 btrfs_set_inode_gid(leaf, item, inode->i_gid);
Chris Masondbe674a2008-07-17 12:54:05 -04002331 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
Chris Mason5f39d392007-10-15 16:14:19 -04002332 btrfs_set_inode_mode(leaf, item, inode->i_mode);
2333 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
2334
2335 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
2336 inode->i_atime.tv_sec);
2337 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
2338 inode->i_atime.tv_nsec);
2339
2340 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
2341 inode->i_mtime.tv_sec);
2342 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
2343 inode->i_mtime.tv_nsec);
2344
2345 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
2346 inode->i_ctime.tv_sec);
2347 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
2348 inode->i_ctime.tv_nsec);
2349
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002350 btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode));
Chris Masone02119d2008-09-05 16:13:11 -04002351 btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
Chris Masonc3027eb2008-12-08 16:40:21 -05002352 btrfs_set_inode_sequence(leaf, item, BTRFS_I(inode)->sequence);
Chris Masone02119d2008-09-05 16:13:11 -04002353 btrfs_set_inode_transid(leaf, item, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002354 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -05002355 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002356 btrfs_set_inode_block_group(leaf, item, BTRFS_I(inode)->block_group);
Chris Mason39279cc2007-06-12 06:35:45 -04002357}
2358
Chris Masond352ac62008-09-29 15:18:18 -04002359/*
2360 * copy everything in the in-memory inode into the btree.
2361 */
Chris Masond3977122009-01-05 21:25:51 -05002362noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
2363 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002364{
2365 struct btrfs_inode_item *inode_item;
2366 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002367 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002368 int ret;
2369
2370 path = btrfs_alloc_path();
2371 BUG_ON(!path);
Chris Masonb9473432009-03-13 11:00:37 -04002372 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002373 ret = btrfs_lookup_inode(trans, root, path,
2374 &BTRFS_I(inode)->location, 1);
2375 if (ret) {
2376 if (ret > 0)
2377 ret = -ENOENT;
2378 goto failed;
2379 }
2380
Chris Masonb4ce94d2009-02-04 09:25:08 -05002381 btrfs_unlock_up_safe(path, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002382 leaf = path->nodes[0];
2383 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04002384 struct btrfs_inode_item);
2385
Chris Masone02119d2008-09-05 16:13:11 -04002386 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002387 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002388 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002389 ret = 0;
2390failed:
Chris Mason39279cc2007-06-12 06:35:45 -04002391 btrfs_free_path(path);
2392 return ret;
2393}
2394
2395
Chris Masond352ac62008-09-29 15:18:18 -04002396/*
2397 * unlink helper that gets used here in inode.c and in the tree logging
2398 * recovery code. It remove a link in a directory with a given name, and
2399 * also drops the back refs in the inode to the directory
2400 */
Chris Masone02119d2008-09-05 16:13:11 -04002401int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2402 struct btrfs_root *root,
2403 struct inode *dir, struct inode *inode,
2404 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04002405{
2406 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04002407 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002408 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002409 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04002410 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04002411 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04002412
2413 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04002414 if (!path) {
2415 ret = -ENOMEM;
2416 goto err;
2417 }
2418
Chris Masonb9473432009-03-13 11:00:37 -04002419 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002420 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
2421 name, name_len, -1);
2422 if (IS_ERR(di)) {
2423 ret = PTR_ERR(di);
2424 goto err;
2425 }
2426 if (!di) {
2427 ret = -ENOENT;
2428 goto err;
2429 }
Chris Mason5f39d392007-10-15 16:14:19 -04002430 leaf = path->nodes[0];
2431 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04002432 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04002433 if (ret)
2434 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -04002435 btrfs_release_path(root, path);
2436
Josef Bacikaec74772008-07-24 12:12:38 -04002437 ret = btrfs_del_inode_ref(trans, root, name, name_len,
Chris Masone02119d2008-09-05 16:13:11 -04002438 inode->i_ino,
2439 dir->i_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04002440 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05002441 printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
Josef Bacikaec74772008-07-24 12:12:38 -04002442 "inode %lu parent %lu\n", name_len, name,
Chris Masone02119d2008-09-05 16:13:11 -04002443 inode->i_ino, dir->i_ino);
Josef Bacikaec74772008-07-24 12:12:38 -04002444 goto err;
2445 }
2446
Chris Mason39279cc2007-06-12 06:35:45 -04002447 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04002448 index, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -04002449 if (IS_ERR(di)) {
2450 ret = PTR_ERR(di);
2451 goto err;
2452 }
2453 if (!di) {
2454 ret = -ENOENT;
2455 goto err;
2456 }
2457 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason925baed2008-06-25 16:01:30 -04002458 btrfs_release_path(root, path);
Chris Mason39279cc2007-06-12 06:35:45 -04002459
Chris Masone02119d2008-09-05 16:13:11 -04002460 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
2461 inode, dir->i_ino);
Chris Mason49eb7e42008-09-11 15:53:12 -04002462 BUG_ON(ret != 0 && ret != -ENOENT);
Chris Masone02119d2008-09-05 16:13:11 -04002463
2464 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
2465 dir, index);
2466 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04002467err:
2468 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002469 if (ret)
2470 goto out;
2471
2472 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
2473 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2474 btrfs_update_inode(trans, root, dir);
2475 btrfs_drop_nlink(inode);
2476 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04002477out:
Chris Mason39279cc2007-06-12 06:35:45 -04002478 return ret;
2479}
2480
2481static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
2482{
2483 struct btrfs_root *root;
2484 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04002485 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002486 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -05002487 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002488
2489 root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05002490
Chris Mason39279cc2007-06-12 06:35:45 -04002491 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002492
Chris Mason39279cc2007-06-12 06:35:45 -04002493 btrfs_set_trans_block_group(trans, dir);
Chris Mason12fcfd22009-03-24 10:24:20 -04002494
2495 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
2496
Chris Masone02119d2008-09-05 16:13:11 -04002497 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
2498 dentry->d_name.name, dentry->d_name.len);
Josef Bacik7b128762008-07-24 12:17:14 -04002499
2500 if (inode->i_nlink == 0)
2501 ret = btrfs_orphan_add(trans, inode);
2502
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002503 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04002504
Chris Mason89ce8a62008-06-25 16:01:31 -04002505 btrfs_end_transaction_throttle(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002506 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002507 return ret;
2508}
2509
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002510int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
2511 struct btrfs_root *root,
2512 struct inode *dir, u64 objectid,
2513 const char *name, int name_len)
2514{
2515 struct btrfs_path *path;
2516 struct extent_buffer *leaf;
2517 struct btrfs_dir_item *di;
2518 struct btrfs_key key;
2519 u64 index;
2520 int ret;
2521
2522 path = btrfs_alloc_path();
2523 if (!path)
2524 return -ENOMEM;
2525
2526 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
2527 name, name_len, -1);
2528 BUG_ON(!di || IS_ERR(di));
2529
2530 leaf = path->nodes[0];
2531 btrfs_dir_item_key_to_cpu(leaf, di, &key);
2532 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
2533 ret = btrfs_delete_one_dir_name(trans, root, path, di);
2534 BUG_ON(ret);
2535 btrfs_release_path(root, path);
2536
2537 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
2538 objectid, root->root_key.objectid,
2539 dir->i_ino, &index, name, name_len);
2540 if (ret < 0) {
2541 BUG_ON(ret != -ENOENT);
2542 di = btrfs_search_dir_index_item(root, path, dir->i_ino,
2543 name, name_len);
2544 BUG_ON(!di || IS_ERR(di));
2545
2546 leaf = path->nodes[0];
2547 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2548 btrfs_release_path(root, path);
2549 index = key.offset;
2550 }
2551
2552 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
2553 index, name, name_len, -1);
2554 BUG_ON(!di || IS_ERR(di));
2555
2556 leaf = path->nodes[0];
2557 btrfs_dir_item_key_to_cpu(leaf, di, &key);
2558 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
2559 ret = btrfs_delete_one_dir_name(trans, root, path, di);
2560 BUG_ON(ret);
2561 btrfs_release_path(root, path);
2562
2563 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
2564 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2565 ret = btrfs_update_inode(trans, root, dir);
2566 BUG_ON(ret);
2567 dir->i_sb->s_dirt = 1;
2568
2569 btrfs_free_path(path);
2570 return 0;
2571}
2572
Chris Mason39279cc2007-06-12 06:35:45 -04002573static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
2574{
2575 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002576 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002577 int ret;
2578 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04002579 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -05002580 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002581
Chris Mason3394e162008-11-17 20:42:26 -05002582 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002583 inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan134d4512007-10-25 15:49:25 -04002584 return -ENOTEMPTY;
2585
Chris Mason39279cc2007-06-12 06:35:45 -04002586 trans = btrfs_start_transaction(root, 1);
2587 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -04002588
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002589 if (unlikely(inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
2590 err = btrfs_unlink_subvol(trans, root, dir,
2591 BTRFS_I(inode)->location.objectid,
2592 dentry->d_name.name,
2593 dentry->d_name.len);
2594 goto out;
2595 }
2596
Josef Bacik7b128762008-07-24 12:17:14 -04002597 err = btrfs_orphan_add(trans, inode);
2598 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002599 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04002600
Chris Mason39279cc2007-06-12 06:35:45 -04002601 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04002602 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
2603 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05002604 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04002605 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002606out:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002607 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04002608 ret = btrfs_end_transaction_throttle(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002609 btrfs_btree_balance_dirty(root, nr);
Chris Mason39544012007-12-12 14:38:19 -05002610
Chris Mason39279cc2007-06-12 06:35:45 -04002611 if (ret && !err)
2612 err = ret;
2613 return err;
2614}
2615
Chris Masond20f7042008-12-08 16:58:54 -05002616#if 0
Chris Mason39279cc2007-06-12 06:35:45 -04002617/*
Chris Mason323ac952008-10-01 19:05:46 -04002618 * when truncating bytes in a file, it is possible to avoid reading
2619 * the leaves that contain only checksum items. This can be the
2620 * majority of the IO required to delete a large file, but it must
2621 * be done carefully.
2622 *
2623 * The keys in the level just above the leaves are checked to make sure
2624 * the lowest key in a given leaf is a csum key, and starts at an offset
2625 * after the new size.
2626 *
2627 * Then the key for the next leaf is checked to make sure it also has
2628 * a checksum item for the same file. If it does, we know our target leaf
2629 * contains only checksum items, and it can be safely freed without reading
2630 * it.
2631 *
2632 * This is just an optimization targeted at large files. It may do
2633 * nothing. It will return 0 unless things went badly.
2634 */
2635static noinline int drop_csum_leaves(struct btrfs_trans_handle *trans,
2636 struct btrfs_root *root,
2637 struct btrfs_path *path,
2638 struct inode *inode, u64 new_size)
2639{
2640 struct btrfs_key key;
2641 int ret;
2642 int nritems;
2643 struct btrfs_key found_key;
2644 struct btrfs_key other_key;
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002645 struct btrfs_leaf_ref *ref;
2646 u64 leaf_gen;
2647 u64 leaf_start;
Chris Mason323ac952008-10-01 19:05:46 -04002648
2649 path->lowest_level = 1;
2650 key.objectid = inode->i_ino;
2651 key.type = BTRFS_CSUM_ITEM_KEY;
2652 key.offset = new_size;
2653again:
2654 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2655 if (ret < 0)
2656 goto out;
2657
2658 if (path->nodes[1] == NULL) {
2659 ret = 0;
2660 goto out;
2661 }
2662 ret = 0;
2663 btrfs_node_key_to_cpu(path->nodes[1], &found_key, path->slots[1]);
2664 nritems = btrfs_header_nritems(path->nodes[1]);
2665
2666 if (!nritems)
2667 goto out;
2668
2669 if (path->slots[1] >= nritems)
2670 goto next_node;
2671
2672 /* did we find a key greater than anything we want to delete? */
2673 if (found_key.objectid > inode->i_ino ||
2674 (found_key.objectid == inode->i_ino && found_key.type > key.type))
2675 goto out;
2676
2677 /* we check the next key in the node to make sure the leave contains
2678 * only checksum items. This comparison doesn't work if our
2679 * leaf is the last one in the node
2680 */
2681 if (path->slots[1] + 1 >= nritems) {
2682next_node:
2683 /* search forward from the last key in the node, this
2684 * will bring us into the next node in the tree
2685 */
2686 btrfs_node_key_to_cpu(path->nodes[1], &found_key, nritems - 1);
2687
2688 /* unlikely, but we inc below, so check to be safe */
2689 if (found_key.offset == (u64)-1)
2690 goto out;
2691
2692 /* search_forward needs a path with locks held, do the
2693 * search again for the original key. It is possible
2694 * this will race with a balance and return a path that
2695 * we could modify, but this drop is just an optimization
2696 * and is allowed to miss some leaves.
2697 */
2698 btrfs_release_path(root, path);
2699 found_key.offset++;
2700
2701 /* setup a max key for search_forward */
2702 other_key.offset = (u64)-1;
2703 other_key.type = key.type;
2704 other_key.objectid = key.objectid;
2705
2706 path->keep_locks = 1;
2707 ret = btrfs_search_forward(root, &found_key, &other_key,
2708 path, 0, 0);
2709 path->keep_locks = 0;
2710 if (ret || found_key.objectid != key.objectid ||
2711 found_key.type != key.type) {
2712 ret = 0;
2713 goto out;
2714 }
2715
2716 key.offset = found_key.offset;
2717 btrfs_release_path(root, path);
2718 cond_resched();
2719 goto again;
2720 }
2721
2722 /* we know there's one more slot after us in the tree,
2723 * read that key so we can verify it is also a checksum item
2724 */
2725 btrfs_node_key_to_cpu(path->nodes[1], &other_key, path->slots[1] + 1);
2726
2727 if (found_key.objectid < inode->i_ino)
2728 goto next_key;
2729
2730 if (found_key.type != key.type || found_key.offset < new_size)
2731 goto next_key;
2732
2733 /*
2734 * if the key for the next leaf isn't a csum key from this objectid,
2735 * we can't be sure there aren't good items inside this leaf.
2736 * Bail out
2737 */
2738 if (other_key.objectid != inode->i_ino || other_key.type != key.type)
2739 goto out;
2740
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002741 leaf_start = btrfs_node_blockptr(path->nodes[1], path->slots[1]);
2742 leaf_gen = btrfs_node_ptr_generation(path->nodes[1], path->slots[1]);
Chris Mason323ac952008-10-01 19:05:46 -04002743 /*
2744 * it is safe to delete this leaf, it contains only
2745 * csum items from this inode at an offset >= new_size
2746 */
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002747 ret = btrfs_del_leaf(trans, root, path, leaf_start);
Chris Mason323ac952008-10-01 19:05:46 -04002748 BUG_ON(ret);
2749
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002750 if (root->ref_cows && leaf_gen < trans->transid) {
2751 ref = btrfs_alloc_leaf_ref(root, 0);
2752 if (ref) {
2753 ref->root_gen = root->root_key.offset;
2754 ref->bytenr = leaf_start;
2755 ref->owner = 0;
2756 ref->generation = leaf_gen;
2757 ref->nritems = 0;
2758
Chris Masonbd56b302009-02-04 09:27:02 -05002759 btrfs_sort_leaf_ref(ref);
2760
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002761 ret = btrfs_add_leaf_ref(root, ref, 0);
2762 WARN_ON(ret);
2763 btrfs_free_leaf_ref(root, ref);
2764 } else {
2765 WARN_ON(1);
2766 }
2767 }
Chris Mason323ac952008-10-01 19:05:46 -04002768next_key:
2769 btrfs_release_path(root, path);
2770
2771 if (other_key.objectid == inode->i_ino &&
2772 other_key.type == key.type && other_key.offset > key.offset) {
2773 key.offset = other_key.offset;
2774 cond_resched();
2775 goto again;
2776 }
2777 ret = 0;
2778out:
2779 /* fixup any changes we've made to the path */
2780 path->lowest_level = 0;
2781 path->keep_locks = 0;
2782 btrfs_release_path(root, path);
2783 return ret;
2784}
2785
Chris Masond20f7042008-12-08 16:58:54 -05002786#endif
2787
Chris Mason323ac952008-10-01 19:05:46 -04002788/*
Chris Mason39279cc2007-06-12 06:35:45 -04002789 * this can truncate away extent items, csum items and directory items.
2790 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04002791 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04002792 *
2793 * csum items that cross the new i_size are truncated to the new size
2794 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04002795 *
2796 * min_type is the minimum key type to truncate down to. If set to 0, this
2797 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04002798 */
Chris Masone02119d2008-09-05 16:13:11 -04002799noinline int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
2800 struct btrfs_root *root,
2801 struct inode *inode,
2802 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04002803{
2804 int ret;
2805 struct btrfs_path *path;
2806 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04002807 struct btrfs_key found_key;
Chris Mason06d9a8d2009-02-04 09:30:58 -05002808 u32 found_type = (u8)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04002809 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002810 struct btrfs_file_extent_item *fi;
2811 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04002812 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002813 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002814 u64 item_end = 0;
2815 int found_extent;
2816 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05002817 int pending_del_nr = 0;
2818 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04002819 int extent_type = -1;
Chris Mason771ed682008-11-06 22:02:51 -05002820 int encoding;
Chris Mason3b951512008-04-17 11:29:12 -04002821 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002822
Chris Masone02119d2008-09-05 16:13:11 -04002823 if (root->ref_cows)
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002824 btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002825 path = btrfs_alloc_path();
2826 BUG_ON(!path);
Julia Lawall33c17ad2009-07-22 16:49:01 -04002827 path->reada = -1;
Chris Mason5f39d392007-10-15 16:14:19 -04002828
Chris Mason39279cc2007-06-12 06:35:45 -04002829 /* FIXME, add redo link to tree so we don't leak on crash */
2830 key.objectid = inode->i_ino;
2831 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04002832 key.type = (u8)-1;
2833
Chris Mason85e21ba2008-01-29 15:11:36 -05002834search_again:
Chris Masonb9473432009-03-13 11:00:37 -04002835 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05002836 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masond3977122009-01-05 21:25:51 -05002837 if (ret < 0)
Chris Mason85e21ba2008-01-29 15:11:36 -05002838 goto error;
Chris Masond3977122009-01-05 21:25:51 -05002839
Chris Mason85e21ba2008-01-29 15:11:36 -05002840 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002841 /* there are no items in the tree for us to truncate, we're
2842 * done
2843 */
2844 if (path->slots[0] == 0) {
2845 ret = 0;
2846 goto error;
2847 }
Chris Mason85e21ba2008-01-29 15:11:36 -05002848 path->slots[0]--;
2849 }
2850
Chris Masond3977122009-01-05 21:25:51 -05002851 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04002852 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04002853 leaf = path->nodes[0];
2854 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2855 found_type = btrfs_key_type(&found_key);
Chris Mason771ed682008-11-06 22:02:51 -05002856 encoding = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002857
Chris Mason5f39d392007-10-15 16:14:19 -04002858 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -04002859 break;
Chris Mason5f39d392007-10-15 16:14:19 -04002860
Chris Mason85e21ba2008-01-29 15:11:36 -05002861 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04002862 break;
2863
Chris Mason5f39d392007-10-15 16:14:19 -04002864 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04002865 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04002866 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04002867 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04002868 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Mason771ed682008-11-06 22:02:51 -05002869 encoding = btrfs_file_extent_compression(leaf, fi);
2870 encoding |= btrfs_file_extent_encryption(leaf, fi);
2871 encoding |= btrfs_file_extent_other_encoding(leaf, fi);
2872
Chris Mason179e29e2007-11-01 11:28:41 -04002873 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04002874 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04002875 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04002876 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04002877 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04002878 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04002879 }
Yan008630c2007-11-07 13:31:09 -05002880 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04002881 }
Chris Masone02119d2008-09-05 16:13:11 -04002882 if (item_end < new_size) {
Chris Masond3977122009-01-05 21:25:51 -05002883 if (found_type == BTRFS_DIR_ITEM_KEY)
Chris Masonb888db22007-08-27 16:49:44 -04002884 found_type = BTRFS_INODE_ITEM_KEY;
Chris Masond3977122009-01-05 21:25:51 -05002885 else if (found_type == BTRFS_EXTENT_ITEM_KEY)
Chris Masond20f7042008-12-08 16:58:54 -05002886 found_type = BTRFS_EXTENT_DATA_KEY;
Chris Masond3977122009-01-05 21:25:51 -05002887 else if (found_type == BTRFS_EXTENT_DATA_KEY)
Chris Mason85e21ba2008-01-29 15:11:36 -05002888 found_type = BTRFS_XATTR_ITEM_KEY;
Chris Masond3977122009-01-05 21:25:51 -05002889 else if (found_type == BTRFS_XATTR_ITEM_KEY)
Chris Mason85e21ba2008-01-29 15:11:36 -05002890 found_type = BTRFS_INODE_REF_KEY;
Chris Masond3977122009-01-05 21:25:51 -05002891 else if (found_type)
Chris Masonb888db22007-08-27 16:49:44 -04002892 found_type--;
Chris Masond3977122009-01-05 21:25:51 -05002893 else
Chris Masonb888db22007-08-27 16:49:44 -04002894 break;
Yana61721d2007-09-17 11:08:38 -04002895 btrfs_set_key_type(&key, found_type);
Chris Mason85e21ba2008-01-29 15:11:36 -05002896 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04002897 }
Chris Masone02119d2008-09-05 16:13:11 -04002898 if (found_key.offset >= new_size)
Chris Mason39279cc2007-06-12 06:35:45 -04002899 del_item = 1;
2900 else
2901 del_item = 0;
2902 found_extent = 0;
2903
2904 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04002905 if (found_type != BTRFS_EXTENT_DATA_KEY)
2906 goto delete;
2907
2908 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04002909 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04002910 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason771ed682008-11-06 22:02:51 -05002911 if (!del_item && !encoding) {
Chris Masondb945352007-10-15 16:15:53 -04002912 u64 orig_num_bytes =
2913 btrfs_file_extent_num_bytes(leaf, fi);
Chris Masone02119d2008-09-05 16:13:11 -04002914 extent_num_bytes = new_size -
Chris Mason5f39d392007-10-15 16:14:19 -04002915 found_key.offset + root->sectorsize - 1;
Yanb1632b12008-01-30 11:54:04 -05002916 extent_num_bytes = extent_num_bytes &
2917 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04002918 btrfs_set_file_extent_num_bytes(leaf, fi,
2919 extent_num_bytes);
2920 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05002921 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04002922 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002923 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04002924 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002925 } else {
Chris Masondb945352007-10-15 16:15:53 -04002926 extent_num_bytes =
2927 btrfs_file_extent_disk_num_bytes(leaf,
2928 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002929 extent_offset = found_key.offset -
2930 btrfs_file_extent_offset(leaf, fi);
2931
Chris Mason39279cc2007-06-12 06:35:45 -04002932 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05002933 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04002934 if (extent_start != 0) {
2935 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04002936 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002937 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04002938 }
2939 }
Chris Mason90692182008-02-08 13:49:28 -05002940 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04002941 /*
2942 * we can't truncate inline items that have had
2943 * special encodings
2944 */
2945 if (!del_item &&
2946 btrfs_file_extent_compression(leaf, fi) == 0 &&
2947 btrfs_file_extent_encryption(leaf, fi) == 0 &&
2948 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002949 u32 size = new_size - found_key.offset;
2950
2951 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002952 inode_sub_bytes(inode, item_end + 1 -
2953 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04002954 }
2955 size =
2956 btrfs_file_extent_calc_inline_size(size);
Chris Mason90692182008-02-08 13:49:28 -05002957 ret = btrfs_truncate_item(trans, root, path,
Chris Masone02119d2008-09-05 16:13:11 -04002958 size, 1);
Chris Mason90692182008-02-08 13:49:28 -05002959 BUG_ON(ret);
Chris Masone02119d2008-09-05 16:13:11 -04002960 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002961 inode_sub_bytes(inode, item_end + 1 -
2962 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05002963 }
Chris Mason39279cc2007-06-12 06:35:45 -04002964 }
Chris Mason179e29e2007-11-01 11:28:41 -04002965delete:
Chris Mason39279cc2007-06-12 06:35:45 -04002966 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05002967 if (!pending_del_nr) {
2968 /* no pending yet, add ourselves */
2969 pending_del_slot = path->slots[0];
2970 pending_del_nr = 1;
2971 } else if (pending_del_nr &&
2972 path->slots[0] + 1 == pending_del_slot) {
2973 /* hop on the pending chunk */
2974 pending_del_nr++;
2975 pending_del_slot = path->slots[0];
2976 } else {
Chris Masond3977122009-01-05 21:25:51 -05002977 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05002978 }
Chris Mason39279cc2007-06-12 06:35:45 -04002979 } else {
2980 break;
2981 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002982 if (found_extent && root->ref_cows) {
Chris Masonb9473432009-03-13 11:00:37 -04002983 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002984 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002985 extent_num_bytes, 0,
2986 btrfs_header_owner(leaf),
2987 inode->i_ino, extent_offset);
Chris Mason39279cc2007-06-12 06:35:45 -04002988 BUG_ON(ret);
2989 }
Chris Mason85e21ba2008-01-29 15:11:36 -05002990next:
2991 if (path->slots[0] == 0) {
2992 if (pending_del_nr)
2993 goto del_pending;
2994 btrfs_release_path(root, path);
Chris Mason06d9a8d2009-02-04 09:30:58 -05002995 if (found_type == BTRFS_INODE_ITEM_KEY)
2996 break;
Chris Mason85e21ba2008-01-29 15:11:36 -05002997 goto search_again;
2998 }
2999
3000 path->slots[0]--;
3001 if (pending_del_nr &&
3002 path->slots[0] + 1 != pending_del_slot) {
3003 struct btrfs_key debug;
3004del_pending:
3005 btrfs_item_key_to_cpu(path->nodes[0], &debug,
3006 pending_del_slot);
3007 ret = btrfs_del_items(trans, root, path,
3008 pending_del_slot,
3009 pending_del_nr);
3010 BUG_ON(ret);
3011 pending_del_nr = 0;
3012 btrfs_release_path(root, path);
Chris Mason06d9a8d2009-02-04 09:30:58 -05003013 if (found_type == BTRFS_INODE_ITEM_KEY)
3014 break;
Chris Mason85e21ba2008-01-29 15:11:36 -05003015 goto search_again;
3016 }
Chris Mason39279cc2007-06-12 06:35:45 -04003017 }
3018 ret = 0;
3019error:
Chris Mason85e21ba2008-01-29 15:11:36 -05003020 if (pending_del_nr) {
3021 ret = btrfs_del_items(trans, root, path, pending_del_slot,
3022 pending_del_nr);
3023 }
Chris Mason39279cc2007-06-12 06:35:45 -04003024 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003025 return ret;
3026}
3027
Chris Masona52d9a82007-08-27 16:49:44 -04003028/*
3029 * taken from block_truncate_page, but does cow as it zeros out
3030 * any bytes left in the last page in the file.
3031 */
3032static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
3033{
3034 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -04003035 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003036 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3037 struct btrfs_ordered_extent *ordered;
3038 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04003039 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04003040 pgoff_t index = from >> PAGE_CACHE_SHIFT;
3041 unsigned offset = from & (PAGE_CACHE_SIZE-1);
3042 struct page *page;
3043 int ret = 0;
3044 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003045 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04003046
3047 if ((offset & (blocksize - 1)) == 0)
3048 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04003049 ret = btrfs_check_data_free_space(root, inode, PAGE_CACHE_SIZE);
3050 if (ret)
3051 goto out;
3052
3053 ret = btrfs_reserve_metadata_for_delalloc(root, inode, 1);
3054 if (ret)
3055 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04003056
3057 ret = -ENOMEM;
Chris Mason211c17f2008-05-15 09:13:45 -04003058again:
Chris Masona52d9a82007-08-27 16:49:44 -04003059 page = grab_cache_page(mapping, index);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003060 if (!page) {
3061 btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
3062 btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
Chris Masona52d9a82007-08-27 16:49:44 -04003063 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04003064 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003065
3066 page_start = page_offset(page);
3067 page_end = page_start + PAGE_CACHE_SIZE - 1;
3068
Chris Masona52d9a82007-08-27 16:49:44 -04003069 if (!PageUptodate(page)) {
3070 ret = btrfs_readpage(NULL, page);
3071 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04003072 if (page->mapping != mapping) {
3073 unlock_page(page);
3074 page_cache_release(page);
3075 goto again;
3076 }
Chris Masona52d9a82007-08-27 16:49:44 -04003077 if (!PageUptodate(page)) {
3078 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04003079 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04003080 }
3081 }
Chris Mason211c17f2008-05-15 09:13:45 -04003082 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003083
3084 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
3085 set_page_extent_mapped(page);
3086
3087 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3088 if (ordered) {
3089 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3090 unlock_page(page);
3091 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04003092 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003093 btrfs_put_ordered_extent(ordered);
3094 goto again;
3095 }
3096
Josef Bacik5d5e1032009-10-13 16:46:49 -04003097 clear_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
3098 EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
3099 GFP_NOFS);
3100
Josef Bacik9ed74f22009-09-11 16:12:44 -04003101 ret = btrfs_set_extent_delalloc(inode, page_start, page_end);
3102 if (ret) {
3103 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3104 goto out_unlock;
3105 }
3106
Chris Masone6dcd2d2008-07-17 12:53:50 -04003107 ret = 0;
3108 if (offset != PAGE_CACHE_SIZE) {
3109 kaddr = kmap(page);
3110 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
3111 flush_dcache_page(page);
3112 kunmap(page);
3113 }
Chris Mason247e7432008-07-17 12:53:51 -04003114 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003115 set_page_dirty(page);
3116 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04003117
Chris Mason89642222008-07-24 09:41:53 -04003118out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04003119 if (ret)
3120 btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
3121 btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003122 unlock_page(page);
3123 page_cache_release(page);
3124out:
3125 return ret;
3126}
3127
Yan Zheng9036c102008-10-30 14:19:41 -04003128int btrfs_cont_expand(struct inode *inode, loff_t size)
3129{
3130 struct btrfs_trans_handle *trans;
3131 struct btrfs_root *root = BTRFS_I(inode)->root;
3132 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3133 struct extent_map *em;
3134 u64 mask = root->sectorsize - 1;
3135 u64 hole_start = (inode->i_size + mask) & ~mask;
3136 u64 block_end = (size + mask) & ~mask;
3137 u64 last_byte;
3138 u64 cur_offset;
3139 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003140 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04003141
3142 if (size <= hole_start)
3143 return 0;
3144
Josef Bacik5d5e1032009-10-13 16:46:49 -04003145 err = btrfs_truncate_page(inode->i_mapping, inode->i_size);
3146 if (err)
3147 return err;
Yan Zheng9036c102008-10-30 14:19:41 -04003148
3149 while (1) {
3150 struct btrfs_ordered_extent *ordered;
3151 btrfs_wait_ordered_range(inode, hole_start,
3152 block_end - hole_start);
3153 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
3154 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
3155 if (!ordered)
3156 break;
3157 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
3158 btrfs_put_ordered_extent(ordered);
3159 }
3160
3161 trans = btrfs_start_transaction(root, 1);
3162 btrfs_set_trans_block_group(trans, inode);
3163
3164 cur_offset = hole_start;
3165 while (1) {
3166 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
3167 block_end - cur_offset, 0);
3168 BUG_ON(IS_ERR(em) || !em);
3169 last_byte = min(extent_map_end(em), block_end);
3170 last_byte = (last_byte + mask) & ~mask;
3171 if (test_bit(EXTENT_FLAG_VACANCY, &em->flags)) {
Chris Mason771ed682008-11-06 22:02:51 -05003172 u64 hint_byte = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04003173 hole_size = last_byte - cur_offset;
Chris Mason771ed682008-11-06 22:02:51 -05003174 err = btrfs_drop_extents(trans, root, inode,
3175 cur_offset,
3176 cur_offset + hole_size,
Chris Masone980b502009-04-24 14:39:24 -04003177 block_end,
Chris Masona1ed8352009-09-11 12:27:37 -04003178 cur_offset, &hint_byte, 1);
Chris Mason771ed682008-11-06 22:02:51 -05003179 if (err)
3180 break;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003181
3182 err = btrfs_reserve_metadata_space(root, 1);
3183 if (err)
3184 break;
3185
Yan Zheng9036c102008-10-30 14:19:41 -04003186 err = btrfs_insert_file_extent(trans, root,
3187 inode->i_ino, cur_offset, 0,
3188 0, hole_size, 0, hole_size,
3189 0, 0, 0);
3190 btrfs_drop_extent_cache(inode, hole_start,
3191 last_byte - 1, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003192 btrfs_unreserve_metadata_space(root, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04003193 }
3194 free_extent_map(em);
3195 cur_offset = last_byte;
3196 if (err || cur_offset >= block_end)
3197 break;
3198 }
3199
3200 btrfs_end_transaction(trans, root);
3201 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
3202 return err;
3203}
3204
Chris Mason39279cc2007-06-12 06:35:45 -04003205static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
3206{
3207 struct inode *inode = dentry->d_inode;
3208 int err;
3209
3210 err = inode_change_ok(inode, attr);
3211 if (err)
3212 return err;
3213
Chris Mason5a3f23d2009-03-31 13:27:11 -04003214 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
3215 if (attr->ia_size > inode->i_size) {
3216 err = btrfs_cont_expand(inode, attr->ia_size);
3217 if (err)
3218 return err;
3219 } else if (inode->i_size > 0 &&
3220 attr->ia_size == 0) {
3221
3222 /* we're truncating a file that used to have good
3223 * data down to zero. Make sure it gets into
3224 * the ordered flush list so that any new writes
3225 * get down to disk quickly.
3226 */
3227 BTRFS_I(inode)->ordered_data_close = 1;
3228 }
Chris Mason39279cc2007-06-12 06:35:45 -04003229 }
Yan Zheng9036c102008-10-30 14:19:41 -04003230
Chris Mason39279cc2007-06-12 06:35:45 -04003231 err = inode_setattr(inode, attr);
Josef Bacik33268ea2008-07-24 12:16:36 -04003232
3233 if (!err && ((attr->ia_valid & ATTR_MODE)))
3234 err = btrfs_acl_chmod(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003235 return err;
3236}
Chris Mason61295eb2008-01-14 16:24:38 -05003237
Chris Mason39279cc2007-06-12 06:35:45 -04003238void btrfs_delete_inode(struct inode *inode)
3239{
3240 struct btrfs_trans_handle *trans;
3241 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003242 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04003243 int ret;
3244
3245 truncate_inode_pages(&inode->i_data, 0);
3246 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04003247 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003248 goto no_delete;
3249 }
Chris Mason4a096752008-07-21 10:29:44 -04003250 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04003251
Yan, Zheng76dda932009-09-21 16:00:26 -04003252 if (inode->i_nlink > 0) {
3253 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
3254 goto no_delete;
3255 }
3256
Chris Masondbe674a2008-07-17 12:54:05 -04003257 btrfs_i_size_write(inode, 0);
Yan Zheng180591b2009-01-06 09:58:06 -05003258 trans = btrfs_join_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003259
Chris Mason39279cc2007-06-12 06:35:45 -04003260 btrfs_set_trans_block_group(trans, inode);
Chris Masone02119d2008-09-05 16:13:11 -04003261 ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size, 0);
Josef Bacik7b128762008-07-24 12:17:14 -04003262 if (ret) {
3263 btrfs_orphan_del(NULL, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04003264 goto no_delete_lock;
Josef Bacik7b128762008-07-24 12:17:14 -04003265 }
3266
3267 btrfs_orphan_del(trans, inode);
Chris Mason85e21ba2008-01-29 15:11:36 -05003268
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003269 nr = trans->blocks_used;
Chris Mason85e21ba2008-01-29 15:11:36 -05003270 clear_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003271
Chris Mason39279cc2007-06-12 06:35:45 -04003272 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003273 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003274 return;
Chris Mason54aa1f42007-06-22 14:16:25 -04003275
3276no_delete_lock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003277 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04003278 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003279 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003280no_delete:
3281 clear_inode(inode);
3282}
3283
3284/*
3285 * this returns the key found in the dir entry in the location pointer.
3286 * If no dir entries were found, location->objectid is 0.
3287 */
3288static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
3289 struct btrfs_key *location)
3290{
3291 const char *name = dentry->d_name.name;
3292 int namelen = dentry->d_name.len;
3293 struct btrfs_dir_item *di;
3294 struct btrfs_path *path;
3295 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04003296 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003297
3298 path = btrfs_alloc_path();
3299 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -05003300
Chris Mason39279cc2007-06-12 06:35:45 -04003301 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
3302 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04003303 if (IS_ERR(di))
3304 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05003305
3306 if (!di || IS_ERR(di))
Chris Mason39544012007-12-12 14:38:19 -05003307 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05003308
Chris Mason5f39d392007-10-15 16:14:19 -04003309 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04003310out:
Chris Mason39279cc2007-06-12 06:35:45 -04003311 btrfs_free_path(path);
3312 return ret;
Chris Mason39544012007-12-12 14:38:19 -05003313out_err:
3314 location->objectid = 0;
3315 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04003316}
3317
3318/*
3319 * when we hit a tree root in a directory, the btrfs part of the inode
3320 * needs to be changed to reflect the root directory of the tree root. This
3321 * is kind of like crossing a mount point.
3322 */
3323static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003324 struct inode *dir,
3325 struct dentry *dentry,
3326 struct btrfs_key *location,
3327 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04003328{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003329 struct btrfs_path *path;
3330 struct btrfs_root *new_root;
3331 struct btrfs_root_ref *ref;
3332 struct extent_buffer *leaf;
3333 int ret;
3334 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003335
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003336 path = btrfs_alloc_path();
3337 if (!path) {
3338 err = -ENOMEM;
3339 goto out;
3340 }
Chris Mason39279cc2007-06-12 06:35:45 -04003341
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003342 err = -ENOENT;
3343 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
3344 BTRFS_I(dir)->root->root_key.objectid,
3345 location->objectid);
3346 if (ret) {
3347 if (ret < 0)
3348 err = ret;
3349 goto out;
3350 }
Chris Mason39279cc2007-06-12 06:35:45 -04003351
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003352 leaf = path->nodes[0];
3353 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
3354 if (btrfs_root_ref_dirid(leaf, ref) != dir->i_ino ||
3355 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
3356 goto out;
3357
3358 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
3359 (unsigned long)(ref + 1),
3360 dentry->d_name.len);
3361 if (ret)
3362 goto out;
3363
3364 btrfs_release_path(root->fs_info->tree_root, path);
3365
3366 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
3367 if (IS_ERR(new_root)) {
3368 err = PTR_ERR(new_root);
3369 goto out;
3370 }
3371
3372 if (btrfs_root_refs(&new_root->root_item) == 0) {
3373 err = -ENOENT;
3374 goto out;
3375 }
3376
3377 *sub_root = new_root;
3378 location->objectid = btrfs_root_dirid(&new_root->root_item);
3379 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04003380 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003381 err = 0;
3382out:
3383 btrfs_free_path(path);
3384 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003385}
3386
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003387static void inode_tree_add(struct inode *inode)
3388{
3389 struct btrfs_root *root = BTRFS_I(inode)->root;
3390 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003391 struct rb_node **p;
3392 struct rb_node *parent;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003393again:
3394 p = &root->inode_tree.rb_node;
3395 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003396
Yan, Zheng76dda932009-09-21 16:00:26 -04003397 if (hlist_unhashed(&inode->i_hash))
3398 return;
3399
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003400 spin_lock(&root->inode_lock);
3401 while (*p) {
3402 parent = *p;
3403 entry = rb_entry(parent, struct btrfs_inode, rb_node);
3404
3405 if (inode->i_ino < entry->vfs_inode.i_ino)
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003406 p = &parent->rb_left;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003407 else if (inode->i_ino > entry->vfs_inode.i_ino)
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003408 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003409 else {
3410 WARN_ON(!(entry->vfs_inode.i_state &
3411 (I_WILL_FREE | I_FREEING | I_CLEAR)));
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003412 rb_erase(parent, &root->inode_tree);
3413 RB_CLEAR_NODE(parent);
3414 spin_unlock(&root->inode_lock);
3415 goto again;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003416 }
3417 }
3418 rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
3419 rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
3420 spin_unlock(&root->inode_lock);
3421}
3422
3423static void inode_tree_del(struct inode *inode)
3424{
3425 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04003426 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003427
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003428 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003429 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003430 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003431 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04003432 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003433 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003434 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04003435
3436 if (empty && btrfs_root_refs(&root->root_item) == 0) {
3437 synchronize_srcu(&root->fs_info->subvol_srcu);
3438 spin_lock(&root->inode_lock);
3439 empty = RB_EMPTY_ROOT(&root->inode_tree);
3440 spin_unlock(&root->inode_lock);
3441 if (empty)
3442 btrfs_add_dead_root(root);
3443 }
3444}
3445
3446int btrfs_invalidate_inodes(struct btrfs_root *root)
3447{
3448 struct rb_node *node;
3449 struct rb_node *prev;
3450 struct btrfs_inode *entry;
3451 struct inode *inode;
3452 u64 objectid = 0;
3453
3454 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
3455
3456 spin_lock(&root->inode_lock);
3457again:
3458 node = root->inode_tree.rb_node;
3459 prev = NULL;
3460 while (node) {
3461 prev = node;
3462 entry = rb_entry(node, struct btrfs_inode, rb_node);
3463
3464 if (objectid < entry->vfs_inode.i_ino)
3465 node = node->rb_left;
3466 else if (objectid > entry->vfs_inode.i_ino)
3467 node = node->rb_right;
3468 else
3469 break;
3470 }
3471 if (!node) {
3472 while (prev) {
3473 entry = rb_entry(prev, struct btrfs_inode, rb_node);
3474 if (objectid <= entry->vfs_inode.i_ino) {
3475 node = prev;
3476 break;
3477 }
3478 prev = rb_next(prev);
3479 }
3480 }
3481 while (node) {
3482 entry = rb_entry(node, struct btrfs_inode, rb_node);
3483 objectid = entry->vfs_inode.i_ino + 1;
3484 inode = igrab(&entry->vfs_inode);
3485 if (inode) {
3486 spin_unlock(&root->inode_lock);
3487 if (atomic_read(&inode->i_count) > 1)
3488 d_prune_aliases(inode);
3489 /*
3490 * btrfs_drop_inode will remove it from
3491 * the inode cache when its usage count
3492 * hits zero.
3493 */
3494 iput(inode);
3495 cond_resched();
3496 spin_lock(&root->inode_lock);
3497 goto again;
3498 }
3499
3500 if (cond_resched_lock(&root->inode_lock))
3501 goto again;
3502
3503 node = rb_next(node);
3504 }
3505 spin_unlock(&root->inode_lock);
3506 return 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003507}
3508
Chris Masone02119d2008-09-05 16:13:11 -04003509static noinline void init_btrfs_i(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003510{
Chris Masone02119d2008-09-05 16:13:11 -04003511 struct btrfs_inode *bi = BTRFS_I(inode);
3512
Chris Masone02119d2008-09-05 16:13:11 -04003513 bi->generation = 0;
Chris Masonc3027eb2008-12-08 16:40:21 -05003514 bi->sequence = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003515 bi->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04003516 bi->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003517 bi->logged_trans = 0;
3518 bi->delalloc_bytes = 0;
Josef Bacik6a632092009-02-20 11:00:09 -05003519 bi->reserved_bytes = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003520 bi->disk_i_size = 0;
3521 bi->flags = 0;
3522 bi->index_cnt = (u64)-1;
Chris Mason12fcfd22009-03-24 10:24:20 -04003523 bi->last_unlink_trans = 0;
Chris Mason27574952009-05-14 13:10:02 -04003524 bi->ordered_data_close = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003525 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
3526 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04003527 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04003528 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
3529 inode->i_mapping, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04003530 INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes);
Chris Mason5a3f23d2009-03-31 13:27:11 -04003531 INIT_LIST_HEAD(&BTRFS_I(inode)->ordered_operations);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003532 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Chris Masonba1da2f2008-07-17 12:54:15 -04003533 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Masonee6e6502008-07-17 12:54:40 -04003534 mutex_init(&BTRFS_I(inode)->extent_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04003535 mutex_init(&BTRFS_I(inode)->log_mutex);
3536}
3537
3538static int btrfs_init_locked_inode(struct inode *inode, void *p)
3539{
3540 struct btrfs_iget_args *args = p;
3541 inode->i_ino = args->ino;
3542 init_btrfs_i(inode);
3543 BTRFS_I(inode)->root = args->root;
Josef Bacik6a632092009-02-20 11:00:09 -05003544 btrfs_set_inode_space_info(args->root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003545 return 0;
3546}
3547
3548static int btrfs_find_actor(struct inode *inode, void *opaque)
3549{
3550 struct btrfs_iget_args *args = opaque;
Chris Masond3977122009-01-05 21:25:51 -05003551 return args->ino == inode->i_ino &&
3552 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003553}
3554
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003555static struct inode *btrfs_iget_locked(struct super_block *s,
3556 u64 objectid,
3557 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04003558{
3559 struct inode *inode;
3560 struct btrfs_iget_args args;
3561 args.ino = objectid;
3562 args.root = root;
3563
3564 inode = iget5_locked(s, objectid, btrfs_find_actor,
3565 btrfs_init_locked_inode,
3566 (void *)&args);
3567 return inode;
3568}
3569
Balaji Rao1a54ef82008-07-21 02:01:04 +05303570/* Get an inode object given its location and corresponding root.
3571 * Returns in *is_new if the inode was read from disk
3572 */
3573struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003574 struct btrfs_root *root)
Balaji Rao1a54ef82008-07-21 02:01:04 +05303575{
3576 struct inode *inode;
3577
3578 inode = btrfs_iget_locked(s, location->objectid, root);
3579 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003580 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05303581
3582 if (inode->i_state & I_NEW) {
3583 BTRFS_I(inode)->root = root;
3584 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
3585 btrfs_read_locked_inode(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003586
3587 inode_tree_add(inode);
Balaji Rao1a54ef82008-07-21 02:01:04 +05303588 unlock_new_inode(inode);
Balaji Rao1a54ef82008-07-21 02:01:04 +05303589 }
3590
3591 return inode;
3592}
3593
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003594static struct inode *new_simple_dir(struct super_block *s,
3595 struct btrfs_key *key,
3596 struct btrfs_root *root)
3597{
3598 struct inode *inode = new_inode(s);
3599
3600 if (!inode)
3601 return ERR_PTR(-ENOMEM);
3602
3603 init_btrfs_i(inode);
3604
3605 BTRFS_I(inode)->root = root;
3606 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
3607 BTRFS_I(inode)->dummy_inode = 1;
3608
3609 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
3610 inode->i_op = &simple_dir_inode_operations;
3611 inode->i_fop = &simple_dir_operations;
3612 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
3613 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
3614
3615 return inode;
3616}
3617
Chris Mason3de45862008-11-17 21:02:50 -05003618struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04003619{
Chris Masond3977122009-01-05 21:25:51 -05003620 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003621 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003622 struct btrfs_root *sub_root = root;
3623 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04003624 int index;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003625 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04003626
Yan, Zheng76dda932009-09-21 16:00:26 -04003627 dentry->d_op = &btrfs_dentry_operations;
3628
Chris Mason39279cc2007-06-12 06:35:45 -04003629 if (dentry->d_name.len > BTRFS_NAME_LEN)
3630 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04003631
Chris Mason39279cc2007-06-12 06:35:45 -04003632 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04003633
Chris Mason39279cc2007-06-12 06:35:45 -04003634 if (ret < 0)
3635 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04003636
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003637 if (location.objectid == 0)
3638 return NULL;
3639
3640 if (location.type == BTRFS_INODE_ITEM_KEY) {
3641 inode = btrfs_iget(dir->i_sb, &location, root);
3642 return inode;
3643 }
3644
3645 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
3646
Yan, Zheng76dda932009-09-21 16:00:26 -04003647 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003648 ret = fixup_tree_root_location(root, dir, dentry,
3649 &location, &sub_root);
3650 if (ret < 0) {
3651 if (ret != -ENOENT)
3652 inode = ERR_PTR(ret);
3653 else
3654 inode = new_simple_dir(dir->i_sb, &location, sub_root);
3655 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003656 inode = btrfs_iget(dir->i_sb, &location, sub_root);
Chris Mason39279cc2007-06-12 06:35:45 -04003657 }
Yan, Zheng76dda932009-09-21 16:00:26 -04003658 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
3659
Chris Mason3de45862008-11-17 21:02:50 -05003660 return inode;
3661}
3662
Yan, Zheng76dda932009-09-21 16:00:26 -04003663static int btrfs_dentry_delete(struct dentry *dentry)
3664{
3665 struct btrfs_root *root;
3666
Yan, Zhengefefb142009-10-09 09:25:16 -04003667 if (!dentry->d_inode && !IS_ROOT(dentry))
3668 dentry = dentry->d_parent;
Yan, Zheng76dda932009-09-21 16:00:26 -04003669
Yan, Zhengefefb142009-10-09 09:25:16 -04003670 if (dentry->d_inode) {
3671 root = BTRFS_I(dentry->d_inode)->root;
3672 if (btrfs_root_refs(&root->root_item) == 0)
3673 return 1;
3674 }
Yan, Zheng76dda932009-09-21 16:00:26 -04003675 return 0;
3676}
3677
Chris Mason3de45862008-11-17 21:02:50 -05003678static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
3679 struct nameidata *nd)
3680{
3681 struct inode *inode;
3682
Chris Mason3de45862008-11-17 21:02:50 -05003683 inode = btrfs_lookup_dentry(dir, dentry);
3684 if (IS_ERR(inode))
3685 return ERR_CAST(inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003686
Chris Mason39279cc2007-06-12 06:35:45 -04003687 return d_splice_alias(inode, dentry);
3688}
3689
Chris Mason39279cc2007-06-12 06:35:45 -04003690static unsigned char btrfs_filetype_table[] = {
3691 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
3692};
3693
David Woodhousecbdf5a22008-08-06 19:42:33 +01003694static int btrfs_real_readdir(struct file *filp, void *dirent,
3695 filldir_t filldir)
Chris Mason39279cc2007-06-12 06:35:45 -04003696{
Chris Mason6da6aba2007-12-18 16:15:09 -05003697 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003698 struct btrfs_root *root = BTRFS_I(inode)->root;
3699 struct btrfs_item *item;
3700 struct btrfs_dir_item *di;
3701 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04003702 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04003703 struct btrfs_path *path;
3704 int ret;
3705 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04003706 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003707 int slot;
3708 int advance;
3709 unsigned char d_type;
3710 int over = 0;
3711 u32 di_cur;
3712 u32 di_total;
3713 u32 di_len;
3714 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04003715 char tmp_name[32];
3716 char *name_ptr;
3717 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04003718
3719 /* FIXME, use a real flag for deciding about the key type */
3720 if (root->fs_info->tree_root == root)
3721 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04003722
Chris Mason39544012007-12-12 14:38:19 -05003723 /* special case for "." */
3724 if (filp->f_pos == 0) {
3725 over = filldir(dirent, ".", 1,
3726 1, inode->i_ino,
3727 DT_DIR);
3728 if (over)
3729 return 0;
3730 filp->f_pos = 1;
3731 }
Chris Mason39544012007-12-12 14:38:19 -05003732 /* special case for .., just use the back ref */
3733 if (filp->f_pos == 1) {
David Woodhouse5ecc7e52008-08-17 15:14:48 +01003734 u64 pino = parent_ino(filp->f_path.dentry);
Chris Mason39544012007-12-12 14:38:19 -05003735 over = filldir(dirent, "..", 2,
David Woodhouse5ecc7e52008-08-17 15:14:48 +01003736 2, pino, DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05003737 if (over)
David Woodhouse49593bf2008-08-17 17:08:36 +01003738 return 0;
Chris Mason39544012007-12-12 14:38:19 -05003739 filp->f_pos = 2;
3740 }
David Woodhouse49593bf2008-08-17 17:08:36 +01003741 path = btrfs_alloc_path();
3742 path->reada = 2;
3743
Chris Mason39279cc2007-06-12 06:35:45 -04003744 btrfs_set_key_type(&key, key_type);
3745 key.offset = filp->f_pos;
David Woodhouse49593bf2008-08-17 17:08:36 +01003746 key.objectid = inode->i_ino;
Chris Mason5f39d392007-10-15 16:14:19 -04003747
Chris Mason39279cc2007-06-12 06:35:45 -04003748 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3749 if (ret < 0)
3750 goto err;
3751 advance = 0;
David Woodhouse49593bf2008-08-17 17:08:36 +01003752
3753 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04003754 leaf = path->nodes[0];
3755 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003756 slot = path->slots[0];
3757 if (advance || slot >= nritems) {
David Woodhouse49593bf2008-08-17 17:08:36 +01003758 if (slot >= nritems - 1) {
Chris Mason39279cc2007-06-12 06:35:45 -04003759 ret = btrfs_next_leaf(root, path);
3760 if (ret)
3761 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003762 leaf = path->nodes[0];
3763 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003764 slot = path->slots[0];
3765 } else {
3766 slot++;
3767 path->slots[0]++;
3768 }
3769 }
Chris Mason3de45862008-11-17 21:02:50 -05003770
Chris Mason39279cc2007-06-12 06:35:45 -04003771 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04003772 item = btrfs_item_nr(leaf, slot);
3773 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3774
3775 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04003776 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003777 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003778 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003779 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04003780 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04003781
3782 filp->f_pos = found_key.offset;
David Woodhouse49593bf2008-08-17 17:08:36 +01003783
Chris Mason39279cc2007-06-12 06:35:45 -04003784 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
3785 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003786 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01003787
3788 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04003789 struct btrfs_key location;
3790
3791 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01003792 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04003793 name_ptr = tmp_name;
3794 } else {
3795 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01003796 if (!name_ptr) {
3797 ret = -ENOMEM;
3798 goto err;
3799 }
Chris Mason5f39d392007-10-15 16:14:19 -04003800 }
3801 read_extent_buffer(leaf, name_ptr,
3802 (unsigned long)(di + 1), name_len);
3803
3804 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
3805 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05003806
3807 /* is this a reference to our own snapshot? If so
3808 * skip it
3809 */
3810 if (location.type == BTRFS_ROOT_ITEM_KEY &&
3811 location.objectid == root->root_key.objectid) {
3812 over = 0;
3813 goto skip;
3814 }
Chris Mason5f39d392007-10-15 16:14:19 -04003815 over = filldir(dirent, name_ptr, name_len,
David Woodhouse49593bf2008-08-17 17:08:36 +01003816 found_key.offset, location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04003817 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04003818
Chris Mason3de45862008-11-17 21:02:50 -05003819skip:
Chris Mason5f39d392007-10-15 16:14:19 -04003820 if (name_ptr != tmp_name)
3821 kfree(name_ptr);
3822
Chris Mason39279cc2007-06-12 06:35:45 -04003823 if (over)
3824 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05003825 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01003826 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04003827 di_cur += di_len;
3828 di = (struct btrfs_dir_item *)((char *)di + di_len);
3829 }
3830 }
David Woodhouse49593bf2008-08-17 17:08:36 +01003831
3832 /* Reached end of directory/root. Bump pos past the last item. */
Yan Zheng5e591a02008-02-19 11:41:02 -05003833 if (key_type == BTRFS_DIR_INDEX_KEY)
Chris Mason89f135d2009-01-28 15:34:27 -05003834 filp->f_pos = INT_LIMIT(off_t);
Yan Zheng5e591a02008-02-19 11:41:02 -05003835 else
3836 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04003837nopos:
3838 ret = 0;
3839err:
Chris Mason39279cc2007-06-12 06:35:45 -04003840 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003841 return ret;
3842}
3843
3844int btrfs_write_inode(struct inode *inode, int wait)
3845{
3846 struct btrfs_root *root = BTRFS_I(inode)->root;
3847 struct btrfs_trans_handle *trans;
3848 int ret = 0;
3849
Yan Zhengc146afa2008-11-12 14:34:12 -05003850 if (root->fs_info->btree_inode == inode)
Chris Mason4ca8b412008-08-05 13:30:48 -04003851 return 0;
3852
Chris Mason39279cc2007-06-12 06:35:45 -04003853 if (wait) {
Chris Masonf9295742008-07-17 12:54:14 -04003854 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003855 btrfs_set_trans_block_group(trans, inode);
3856 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04003857 }
3858 return ret;
3859}
3860
3861/*
Chris Mason54aa1f42007-06-22 14:16:25 -04003862 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04003863 * inode changes. But, it is most likely to find the inode in cache.
3864 * FIXME, needs more benchmarking...there are no reasons other than performance
3865 * to keep or drop this code.
3866 */
3867void btrfs_dirty_inode(struct inode *inode)
3868{
3869 struct btrfs_root *root = BTRFS_I(inode)->root;
3870 struct btrfs_trans_handle *trans;
3871
Chris Masonf9295742008-07-17 12:54:14 -04003872 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003873 btrfs_set_trans_block_group(trans, inode);
3874 btrfs_update_inode(trans, root, inode);
3875 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04003876}
3877
Chris Masond352ac62008-09-29 15:18:18 -04003878/*
3879 * find the highest existing sequence number in a directory
3880 * and then set the in-memory index_cnt variable to reflect
3881 * free sequence numbers
3882 */
Josef Bacikaec74772008-07-24 12:12:38 -04003883static int btrfs_set_inode_index_count(struct inode *inode)
3884{
3885 struct btrfs_root *root = BTRFS_I(inode)->root;
3886 struct btrfs_key key, found_key;
3887 struct btrfs_path *path;
3888 struct extent_buffer *leaf;
3889 int ret;
3890
3891 key.objectid = inode->i_ino;
3892 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
3893 key.offset = (u64)-1;
3894
3895 path = btrfs_alloc_path();
3896 if (!path)
3897 return -ENOMEM;
3898
3899 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3900 if (ret < 0)
3901 goto out;
3902 /* FIXME: we should be able to handle this */
3903 if (ret == 0)
3904 goto out;
3905 ret = 0;
3906
3907 /*
3908 * MAGIC NUMBER EXPLANATION:
3909 * since we search a directory based on f_pos we have to start at 2
3910 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
3911 * else has to start at 2
3912 */
3913 if (path->slots[0] == 0) {
3914 BTRFS_I(inode)->index_cnt = 2;
3915 goto out;
3916 }
3917
3918 path->slots[0]--;
3919
3920 leaf = path->nodes[0];
3921 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3922
3923 if (found_key.objectid != inode->i_ino ||
3924 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
3925 BTRFS_I(inode)->index_cnt = 2;
3926 goto out;
3927 }
3928
3929 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
3930out:
3931 btrfs_free_path(path);
3932 return ret;
3933}
3934
Chris Masond352ac62008-09-29 15:18:18 -04003935/*
3936 * helper to find a free sequence number in a given directory. This current
3937 * code is very simple, later versions will do smarter things in the btree
3938 */
Chris Mason3de45862008-11-17 21:02:50 -05003939int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04003940{
3941 int ret = 0;
3942
3943 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
3944 ret = btrfs_set_inode_index_count(dir);
Chris Masond3977122009-01-05 21:25:51 -05003945 if (ret)
Josef Bacikaec74772008-07-24 12:12:38 -04003946 return ret;
3947 }
3948
Chris Mason00e4e6b2008-08-05 11:18:09 -04003949 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04003950 BTRFS_I(dir)->index_cnt++;
3951
3952 return ret;
3953}
3954
Chris Mason39279cc2007-06-12 06:35:45 -04003955static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
3956 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04003957 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05003958 const char *name, int name_len,
Yan Zhengd2fb3432008-12-11 16:30:39 -05003959 u64 ref_objectid, u64 objectid,
3960 u64 alloc_hint, int mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04003961{
3962 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04003963 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04003964 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04003965 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05003966 struct btrfs_inode_ref *ref;
3967 struct btrfs_key key[2];
3968 u32 sizes[2];
3969 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04003970 int ret;
3971 int owner;
3972
Chris Mason5f39d392007-10-15 16:14:19 -04003973 path = btrfs_alloc_path();
3974 BUG_ON(!path);
3975
Chris Mason39279cc2007-06-12 06:35:45 -04003976 inode = new_inode(root->fs_info->sb);
3977 if (!inode)
3978 return ERR_PTR(-ENOMEM);
3979
Josef Bacikaec74772008-07-24 12:12:38 -04003980 if (dir) {
Chris Mason3de45862008-11-17 21:02:50 -05003981 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04003982 if (ret) {
3983 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04003984 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04003985 }
Josef Bacikaec74772008-07-24 12:12:38 -04003986 }
3987 /*
3988 * index_cnt is ignored for everything but a dir,
3989 * btrfs_get_inode_index_count has an explanation for the magic
3990 * number
3991 */
Chris Masone02119d2008-09-05 16:13:11 -04003992 init_btrfs_i(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04003993 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04003994 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04003995 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik6a632092009-02-20 11:00:09 -05003996 btrfs_set_inode_space_info(root, inode);
Chris Masonb888db22007-08-27 16:49:44 -04003997
Chris Mason39279cc2007-06-12 06:35:45 -04003998 if (mode & S_IFDIR)
3999 owner = 0;
4000 else
4001 owner = 1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05004002 BTRFS_I(inode)->block_group =
4003 btrfs_find_block_group(root, 0, alloc_hint, owner);
Chris Mason9c583092008-01-29 15:15:18 -05004004
4005 key[0].objectid = objectid;
4006 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
4007 key[0].offset = 0;
4008
4009 key[1].objectid = objectid;
4010 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
4011 key[1].offset = ref_objectid;
4012
4013 sizes[0] = sizeof(struct btrfs_inode_item);
4014 sizes[1] = name_len + sizeof(*ref);
4015
Chris Masonb9473432009-03-13 11:00:37 -04004016 path->leave_spinning = 1;
Chris Mason9c583092008-01-29 15:15:18 -05004017 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
4018 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04004019 goto fail;
4020
Chris Mason79683f22008-11-19 22:00:53 -05004021 inode->i_uid = current_fsuid();
Chris Ball8c087b52009-02-04 09:29:54 -05004022
Chris Mason42f15d72009-02-06 11:35:57 -05004023 if (dir && (dir->i_mode & S_ISGID)) {
Chris Ball8c087b52009-02-04 09:29:54 -05004024 inode->i_gid = dir->i_gid;
4025 if (S_ISDIR(mode))
4026 mode |= S_ISGID;
4027 } else
4028 inode->i_gid = current_fsgid();
4029
Chris Mason39279cc2007-06-12 06:35:45 -04004030 inode->i_mode = mode;
4031 inode->i_ino = objectid;
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004032 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004033 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04004034 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4035 struct btrfs_inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04004036 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05004037
4038 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
4039 struct btrfs_inode_ref);
4040 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04004041 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05004042 ptr = (unsigned long)(ref + 1);
4043 write_extent_buffer(path->nodes[0], name, ptr, name_len);
4044
Chris Mason5f39d392007-10-15 16:14:19 -04004045 btrfs_mark_buffer_dirty(path->nodes[0]);
4046 btrfs_free_path(path);
4047
Chris Mason39279cc2007-06-12 06:35:45 -04004048 location = &BTRFS_I(inode)->location;
4049 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04004050 location->offset = 0;
4051 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
4052
Christoph Hellwig6cbff002009-04-17 10:37:41 +02004053 btrfs_inherit_iflags(inode, dir);
4054
Chris Mason94272162009-07-02 12:26:06 -04004055 if ((mode & S_IFREG)) {
4056 if (btrfs_test_opt(root, NODATASUM))
4057 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
4058 if (btrfs_test_opt(root, NODATACOW))
4059 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
4060 }
4061
Chris Mason39279cc2007-06-12 06:35:45 -04004062 insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004063 inode_tree_add(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004064 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004065fail:
Josef Bacikaec74772008-07-24 12:12:38 -04004066 if (dir)
4067 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04004068 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04004069 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004070 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004071}
4072
4073static inline u8 btrfs_inode_type(struct inode *inode)
4074{
4075 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
4076}
4077
Chris Masond352ac62008-09-29 15:18:18 -04004078/*
4079 * utility function to add 'inode' into 'parent_inode' with
4080 * a give name and a given sequence number.
4081 * if 'add_backref' is true, also insert a backref from the
4082 * inode to the parent directory.
4083 */
Chris Masone02119d2008-09-05 16:13:11 -04004084int btrfs_add_link(struct btrfs_trans_handle *trans,
4085 struct inode *parent_inode, struct inode *inode,
4086 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04004087{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004088 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004089 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04004090 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Chris Mason5f39d392007-10-15 16:14:19 -04004091
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004092 if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
4093 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
4094 } else {
4095 key.objectid = inode->i_ino;
4096 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
4097 key.offset = 0;
4098 }
Chris Mason39279cc2007-06-12 06:35:45 -04004099
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004100 if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
4101 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
4102 key.objectid, root->root_key.objectid,
4103 parent_inode->i_ino,
4104 index, name, name_len);
4105 } else if (add_backref) {
4106 ret = btrfs_insert_inode_ref(trans, root,
4107 name, name_len, inode->i_ino,
4108 parent_inode->i_ino, index);
4109 }
4110
Chris Mason39279cc2007-06-12 06:35:45 -04004111 if (ret == 0) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004112 ret = btrfs_insert_dir_item(trans, root, name, name_len,
4113 parent_inode->i_ino, &key,
4114 btrfs_inode_type(inode), index);
4115 BUG_ON(ret);
4116
Chris Masondbe674a2008-07-17 12:54:05 -04004117 btrfs_i_size_write(parent_inode, parent_inode->i_size +
Chris Masone02119d2008-09-05 16:13:11 -04004118 name_len * 2);
Chris Mason79c44582007-06-25 10:09:33 -04004119 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Masone02119d2008-09-05 16:13:11 -04004120 ret = btrfs_update_inode(trans, root, parent_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004121 }
4122 return ret;
4123}
4124
4125static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05004126 struct dentry *dentry, struct inode *inode,
Chris Mason00e4e6b2008-08-05 11:18:09 -04004127 int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04004128{
Chris Masone02119d2008-09-05 16:13:11 -04004129 int err = btrfs_add_link(trans, dentry->d_parent->d_inode,
4130 inode, dentry->d_name.name,
4131 dentry->d_name.len, backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004132 if (!err) {
4133 d_instantiate(dentry, inode);
4134 return 0;
4135 }
4136 if (err > 0)
4137 err = -EEXIST;
4138 return err;
4139}
4140
Josef Bacik618e21d2007-07-11 10:18:17 -04004141static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
4142 int mode, dev_t rdev)
4143{
4144 struct btrfs_trans_handle *trans;
4145 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05004146 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04004147 int err;
4148 int drop_inode = 0;
4149 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05004150 unsigned long nr = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004151 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04004152
4153 if (!new_valid_dev(rdev))
4154 return -EINVAL;
4155
Josef Bacik9ed74f22009-09-11 16:12:44 -04004156 /*
4157 * 2 for inode item and ref
4158 * 2 for dir items
4159 * 1 for xattr if selinux is on
4160 */
4161 err = btrfs_reserve_metadata_space(root, 5);
Chris Mason1832a6d2007-12-21 16:27:21 -05004162 if (err)
Josef Bacik9ed74f22009-09-11 16:12:44 -04004163 return err;
Chris Mason1832a6d2007-12-21 16:27:21 -05004164
Josef Bacik618e21d2007-07-11 10:18:17 -04004165 trans = btrfs_start_transaction(root, 1);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004166 if (!trans)
4167 goto fail;
Josef Bacik618e21d2007-07-11 10:18:17 -04004168 btrfs_set_trans_block_group(trans, dir);
4169
4170 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
4171 if (err) {
4172 err = -ENOSPC;
4173 goto out_unlock;
4174 }
4175
Josef Bacikaec74772008-07-24 12:12:38 -04004176 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05004177 dentry->d_name.len,
4178 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04004179 BTRFS_I(dir)->block_group, mode, &index);
Josef Bacik618e21d2007-07-11 10:18:17 -04004180 err = PTR_ERR(inode);
4181 if (IS_ERR(inode))
4182 goto out_unlock;
4183
Jim Owens0279b4c2009-02-04 09:29:13 -05004184 err = btrfs_init_inode_security(inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04004185 if (err) {
4186 drop_inode = 1;
4187 goto out_unlock;
4188 }
4189
Josef Bacik618e21d2007-07-11 10:18:17 -04004190 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04004191 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04004192 if (err)
4193 drop_inode = 1;
4194 else {
4195 inode->i_op = &btrfs_special_inode_operations;
4196 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04004197 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04004198 }
Josef Bacik618e21d2007-07-11 10:18:17 -04004199 btrfs_update_inode_block_group(trans, inode);
4200 btrfs_update_inode_block_group(trans, dir);
4201out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004202 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04004203 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05004204fail:
Josef Bacik9ed74f22009-09-11 16:12:44 -04004205 btrfs_unreserve_metadata_space(root, 5);
Josef Bacik618e21d2007-07-11 10:18:17 -04004206 if (drop_inode) {
4207 inode_dec_link_count(inode);
4208 iput(inode);
4209 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004210 btrfs_btree_balance_dirty(root, nr);
Josef Bacik618e21d2007-07-11 10:18:17 -04004211 return err;
4212}
4213
Chris Mason39279cc2007-06-12 06:35:45 -04004214static int btrfs_create(struct inode *dir, struct dentry *dentry,
4215 int mode, struct nameidata *nd)
4216{
4217 struct btrfs_trans_handle *trans;
4218 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05004219 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04004220 int err;
4221 int drop_inode = 0;
Chris Mason1832a6d2007-12-21 16:27:21 -05004222 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004223 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004224 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004225
Josef Bacik9ed74f22009-09-11 16:12:44 -04004226 /*
4227 * 2 for inode item and ref
4228 * 2 for dir items
4229 * 1 for xattr if selinux is on
4230 */
4231 err = btrfs_reserve_metadata_space(root, 5);
Chris Mason1832a6d2007-12-21 16:27:21 -05004232 if (err)
Josef Bacik9ed74f22009-09-11 16:12:44 -04004233 return err;
4234
Chris Mason39279cc2007-06-12 06:35:45 -04004235 trans = btrfs_start_transaction(root, 1);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004236 if (!trans)
4237 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04004238 btrfs_set_trans_block_group(trans, dir);
4239
4240 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
4241 if (err) {
4242 err = -ENOSPC;
4243 goto out_unlock;
4244 }
4245
Josef Bacikaec74772008-07-24 12:12:38 -04004246 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05004247 dentry->d_name.len,
4248 dentry->d_parent->d_inode->i_ino,
Chris Mason00e4e6b2008-08-05 11:18:09 -04004249 objectid, BTRFS_I(dir)->block_group, mode,
4250 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04004251 err = PTR_ERR(inode);
4252 if (IS_ERR(inode))
4253 goto out_unlock;
4254
Jim Owens0279b4c2009-02-04 09:29:13 -05004255 err = btrfs_init_inode_security(inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04004256 if (err) {
4257 drop_inode = 1;
4258 goto out_unlock;
4259 }
4260
Chris Mason39279cc2007-06-12 06:35:45 -04004261 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04004262 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004263 if (err)
4264 drop_inode = 1;
4265 else {
4266 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04004267 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04004268 inode->i_fop = &btrfs_file_operations;
4269 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05004270 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04004271 }
Chris Mason39279cc2007-06-12 06:35:45 -04004272 btrfs_update_inode_block_group(trans, inode);
4273 btrfs_update_inode_block_group(trans, dir);
4274out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004275 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04004276 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05004277fail:
Josef Bacik9ed74f22009-09-11 16:12:44 -04004278 btrfs_unreserve_metadata_space(root, 5);
Chris Mason39279cc2007-06-12 06:35:45 -04004279 if (drop_inode) {
4280 inode_dec_link_count(inode);
4281 iput(inode);
4282 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004283 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004284 return err;
4285}
4286
4287static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
4288 struct dentry *dentry)
4289{
4290 struct btrfs_trans_handle *trans;
4291 struct btrfs_root *root = BTRFS_I(dir)->root;
4292 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004293 u64 index;
Chris Mason1832a6d2007-12-21 16:27:21 -05004294 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004295 int err;
4296 int drop_inode = 0;
4297
4298 if (inode->i_nlink == 0)
4299 return -ENOENT;
4300
Josef Bacik9ed74f22009-09-11 16:12:44 -04004301 /*
4302 * 1 item for inode ref
4303 * 2 items for dir items
4304 */
4305 err = btrfs_reserve_metadata_space(root, 3);
Chris Mason1832a6d2007-12-21 16:27:21 -05004306 if (err)
Josef Bacik9ed74f22009-09-11 16:12:44 -04004307 return err;
4308
4309 btrfs_inc_nlink(inode);
4310
Chris Mason3de45862008-11-17 21:02:50 -05004311 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004312 if (err)
4313 goto fail;
4314
Chris Mason39279cc2007-06-12 06:35:45 -04004315 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04004316
Chris Mason39279cc2007-06-12 06:35:45 -04004317 btrfs_set_trans_block_group(trans, dir);
4318 atomic_inc(&inode->i_count);
Josef Bacikaec74772008-07-24 12:12:38 -04004319
Chris Mason00e4e6b2008-08-05 11:18:09 -04004320 err = btrfs_add_nondir(trans, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04004321
Yan, Zhenga5719522009-09-24 09:17:31 -04004322 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04004323 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04004324 } else {
4325 btrfs_update_inode_block_group(trans, dir);
4326 err = btrfs_update_inode(trans, root, inode);
4327 BUG_ON(err);
4328 btrfs_log_new_name(trans, inode, NULL, dentry->d_parent);
4329 }
Chris Mason39279cc2007-06-12 06:35:45 -04004330
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004331 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04004332 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05004333fail:
Josef Bacik9ed74f22009-09-11 16:12:44 -04004334 btrfs_unreserve_metadata_space(root, 3);
Chris Mason39279cc2007-06-12 06:35:45 -04004335 if (drop_inode) {
4336 inode_dec_link_count(inode);
4337 iput(inode);
4338 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004339 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004340 return err;
4341}
4342
Chris Mason39279cc2007-06-12 06:35:45 -04004343static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
4344{
Chris Masonb9d86662008-05-02 16:13:49 -04004345 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04004346 struct btrfs_trans_handle *trans;
4347 struct btrfs_root *root = BTRFS_I(dir)->root;
4348 int err = 0;
4349 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04004350 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004351 u64 index = 0;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004352 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04004353
Josef Bacik9ed74f22009-09-11 16:12:44 -04004354 /*
4355 * 2 items for inode and ref
4356 * 2 items for dir items
4357 * 1 for xattr if selinux is on
4358 */
4359 err = btrfs_reserve_metadata_space(root, 5);
Chris Mason1832a6d2007-12-21 16:27:21 -05004360 if (err)
Josef Bacik9ed74f22009-09-11 16:12:44 -04004361 return err;
Chris Mason1832a6d2007-12-21 16:27:21 -05004362
Chris Mason39279cc2007-06-12 06:35:45 -04004363 trans = btrfs_start_transaction(root, 1);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004364 if (!trans) {
4365 err = -ENOMEM;
Chris Mason39279cc2007-06-12 06:35:45 -04004366 goto out_unlock;
4367 }
Josef Bacik9ed74f22009-09-11 16:12:44 -04004368 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -04004369
4370 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
4371 if (err) {
4372 err = -ENOSPC;
4373 goto out_unlock;
4374 }
4375
Josef Bacikaec74772008-07-24 12:12:38 -04004376 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05004377 dentry->d_name.len,
4378 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04004379 BTRFS_I(dir)->block_group, S_IFDIR | mode,
4380 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04004381 if (IS_ERR(inode)) {
4382 err = PTR_ERR(inode);
4383 goto out_fail;
4384 }
Chris Mason5f39d392007-10-15 16:14:19 -04004385
Chris Mason39279cc2007-06-12 06:35:45 -04004386 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04004387
Jim Owens0279b4c2009-02-04 09:29:13 -05004388 err = btrfs_init_inode_security(inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04004389 if (err)
4390 goto out_fail;
4391
Chris Mason39279cc2007-06-12 06:35:45 -04004392 inode->i_op = &btrfs_dir_inode_operations;
4393 inode->i_fop = &btrfs_dir_file_operations;
4394 btrfs_set_trans_block_group(trans, inode);
4395
Chris Masondbe674a2008-07-17 12:54:05 -04004396 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004397 err = btrfs_update_inode(trans, root, inode);
4398 if (err)
4399 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04004400
Chris Masone02119d2008-09-05 16:13:11 -04004401 err = btrfs_add_link(trans, dentry->d_parent->d_inode,
4402 inode, dentry->d_name.name,
4403 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004404 if (err)
4405 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04004406
Chris Mason39279cc2007-06-12 06:35:45 -04004407 d_instantiate(dentry, inode);
4408 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004409 btrfs_update_inode_block_group(trans, inode);
4410 btrfs_update_inode_block_group(trans, dir);
4411
4412out_fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004413 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04004414 btrfs_end_transaction_throttle(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04004415
Chris Mason39279cc2007-06-12 06:35:45 -04004416out_unlock:
Josef Bacik9ed74f22009-09-11 16:12:44 -04004417 btrfs_unreserve_metadata_space(root, 5);
Chris Mason39279cc2007-06-12 06:35:45 -04004418 if (drop_on_err)
4419 iput(inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004420 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004421 return err;
4422}
4423
Chris Masond352ac62008-09-29 15:18:18 -04004424/* helper for btfs_get_extent. Given an existing extent in the tree,
4425 * and an extent that you want to insert, deal with overlap and insert
4426 * the new extent into the tree.
4427 */
Chris Mason3b951512008-04-17 11:29:12 -04004428static int merge_extent_mapping(struct extent_map_tree *em_tree,
4429 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04004430 struct extent_map *em,
4431 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04004432{
4433 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04004434
Chris Masone6dcd2d2008-07-17 12:53:50 -04004435 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
4436 start_diff = map_start - em->start;
4437 em->start = map_start;
4438 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04004439 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
4440 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04004441 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04004442 em->block_len -= start_diff;
4443 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004444 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04004445}
4446
Chris Masonc8b97812008-10-29 14:49:59 -04004447static noinline int uncompress_inline(struct btrfs_path *path,
4448 struct inode *inode, struct page *page,
4449 size_t pg_offset, u64 extent_offset,
4450 struct btrfs_file_extent_item *item)
4451{
4452 int ret;
4453 struct extent_buffer *leaf = path->nodes[0];
4454 char *tmp;
4455 size_t max_size;
4456 unsigned long inline_size;
4457 unsigned long ptr;
4458
4459 WARN_ON(pg_offset != 0);
4460 max_size = btrfs_file_extent_ram_bytes(leaf, item);
4461 inline_size = btrfs_file_extent_inline_item_len(leaf,
4462 btrfs_item_nr(leaf, path->slots[0]));
4463 tmp = kmalloc(inline_size, GFP_NOFS);
4464 ptr = btrfs_file_extent_inline_start(item);
4465
4466 read_extent_buffer(leaf, tmp, ptr, inline_size);
4467
Chris Mason5b050f02008-11-11 09:34:41 -05004468 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04004469 ret = btrfs_zlib_decompress(tmp, page, extent_offset,
4470 inline_size, max_size);
4471 if (ret) {
4472 char *kaddr = kmap_atomic(page, KM_USER0);
4473 unsigned long copy_size = min_t(u64,
4474 PAGE_CACHE_SIZE - pg_offset,
4475 max_size - extent_offset);
4476 memset(kaddr + pg_offset, 0, copy_size);
4477 kunmap_atomic(kaddr, KM_USER0);
4478 }
4479 kfree(tmp);
4480 return 0;
4481}
4482
Chris Masond352ac62008-09-29 15:18:18 -04004483/*
4484 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05004485 * the ugly parts come from merging extents from the disk with the in-ram
4486 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04004487 * where the in-ram extents might be locked pending data=ordered completion.
4488 *
4489 * This also copies inline extents directly into the page.
4490 */
Chris Masond3977122009-01-05 21:25:51 -05004491
Chris Masona52d9a82007-08-27 16:49:44 -04004492struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05004493 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04004494 int create)
4495{
4496 int ret;
4497 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04004498 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04004499 u64 extent_start = 0;
4500 u64 extent_end = 0;
4501 u64 objectid = inode->i_ino;
4502 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04004503 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04004504 struct btrfs_root *root = BTRFS_I(inode)->root;
4505 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04004506 struct extent_buffer *leaf;
4507 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04004508 struct extent_map *em = NULL;
4509 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05004510 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04004511 struct btrfs_trans_handle *trans = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04004512 int compressed;
Chris Masona52d9a82007-08-27 16:49:44 -04004513
Chris Masona52d9a82007-08-27 16:49:44 -04004514again:
Chris Mason890871b2009-09-02 16:24:52 -04004515 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05004516 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04004517 if (em)
4518 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04004519 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05004520
Chris Masona52d9a82007-08-27 16:49:44 -04004521 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04004522 if (em->start > start || em->start + em->len <= start)
4523 free_extent_map(em);
4524 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05004525 free_extent_map(em);
4526 else
4527 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004528 }
Chris Masond1310b22008-01-24 16:13:08 -05004529 em = alloc_extent_map(GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04004530 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05004531 err = -ENOMEM;
4532 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004533 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004534 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05004535 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05004536 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05004537 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04004538 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04004539
4540 if (!path) {
4541 path = btrfs_alloc_path();
4542 BUG_ON(!path);
4543 }
4544
Chris Mason179e29e2007-11-01 11:28:41 -04004545 ret = btrfs_lookup_file_extent(trans, root, path,
4546 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04004547 if (ret < 0) {
4548 err = ret;
4549 goto out;
4550 }
4551
4552 if (ret != 0) {
4553 if (path->slots[0] == 0)
4554 goto not_found;
4555 path->slots[0]--;
4556 }
4557
Chris Mason5f39d392007-10-15 16:14:19 -04004558 leaf = path->nodes[0];
4559 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04004560 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04004561 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04004562 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4563 found_type = btrfs_key_type(&found_key);
4564 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04004565 found_type != BTRFS_EXTENT_DATA_KEY) {
4566 goto not_found;
4567 }
4568
Chris Mason5f39d392007-10-15 16:14:19 -04004569 found_type = btrfs_file_extent_type(leaf, item);
4570 extent_start = found_key.offset;
Chris Masonc8b97812008-10-29 14:49:59 -04004571 compressed = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04004572 if (found_type == BTRFS_FILE_EXTENT_REG ||
4573 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04004574 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04004575 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04004576 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
4577 size_t size;
4578 size = btrfs_file_extent_inline_len(leaf, item);
4579 extent_end = (extent_start + size + root->sectorsize - 1) &
4580 ~((u64)root->sectorsize - 1);
4581 }
4582
4583 if (start >= extent_end) {
4584 path->slots[0]++;
4585 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
4586 ret = btrfs_next_leaf(root, path);
4587 if (ret < 0) {
4588 err = ret;
4589 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004590 }
Yan Zheng9036c102008-10-30 14:19:41 -04004591 if (ret > 0)
4592 goto not_found;
4593 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04004594 }
Yan Zheng9036c102008-10-30 14:19:41 -04004595 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4596 if (found_key.objectid != objectid ||
4597 found_key.type != BTRFS_EXTENT_DATA_KEY)
4598 goto not_found;
4599 if (start + len <= found_key.offset)
4600 goto not_found;
4601 em->start = start;
4602 em->len = found_key.offset - start;
4603 goto not_found_em;
4604 }
4605
Yan Zhengd899e052008-10-30 14:25:28 -04004606 if (found_type == BTRFS_FILE_EXTENT_REG ||
4607 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04004608 em->start = extent_start;
4609 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05004610 em->orig_start = extent_start -
4611 btrfs_file_extent_offset(leaf, item);
Chris Masondb945352007-10-15 16:15:53 -04004612 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
4613 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004614 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04004615 goto insert;
4616 }
Chris Masonc8b97812008-10-29 14:49:59 -04004617 if (compressed) {
4618 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
4619 em->block_start = bytenr;
4620 em->block_len = btrfs_file_extent_disk_num_bytes(leaf,
4621 item);
4622 } else {
4623 bytenr += btrfs_file_extent_offset(leaf, item);
4624 em->block_start = bytenr;
4625 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04004626 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
4627 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04004628 }
Chris Masona52d9a82007-08-27 16:49:44 -04004629 goto insert;
4630 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004631 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04004632 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04004633 size_t size;
4634 size_t extent_offset;
4635 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04004636
Chris Masona52d9a82007-08-27 16:49:44 -04004637 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04004638 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04004639 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04004640 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04004641 goto out;
4642 }
4643
Yan Zheng9036c102008-10-30 14:19:41 -04004644 size = btrfs_file_extent_inline_len(leaf, item);
4645 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05004646 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04004647 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04004648 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05004649 em->len = (copy_size + root->sectorsize - 1) &
4650 ~((u64)root->sectorsize - 1);
Yan Zhengff5b7ee2008-11-10 07:34:43 -05004651 em->orig_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04004652 if (compressed)
4653 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Yan689f9342007-10-29 11:41:07 -04004654 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04004655 if (create == 0 && !PageUptodate(page)) {
Chris Masonc8b97812008-10-29 14:49:59 -04004656 if (btrfs_file_extent_compression(leaf, item) ==
4657 BTRFS_COMPRESS_ZLIB) {
4658 ret = uncompress_inline(path, inode, page,
4659 pg_offset,
4660 extent_offset, item);
4661 BUG_ON(ret);
4662 } else {
4663 map = kmap(page);
4664 read_extent_buffer(leaf, map + pg_offset, ptr,
4665 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04004666 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
4667 memset(map + pg_offset + copy_size, 0,
4668 PAGE_CACHE_SIZE - pg_offset -
4669 copy_size);
4670 }
Chris Masonc8b97812008-10-29 14:49:59 -04004671 kunmap(page);
4672 }
Chris Mason179e29e2007-11-01 11:28:41 -04004673 flush_dcache_page(page);
4674 } else if (create && PageUptodate(page)) {
4675 if (!trans) {
4676 kunmap(page);
4677 free_extent_map(em);
4678 em = NULL;
4679 btrfs_release_path(root, path);
Chris Masonf9295742008-07-17 12:54:14 -04004680 trans = btrfs_join_transaction(root, 1);
Chris Mason179e29e2007-11-01 11:28:41 -04004681 goto again;
4682 }
Chris Masonc8b97812008-10-29 14:49:59 -04004683 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05004684 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04004685 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04004686 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04004687 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04004688 }
Chris Masond1310b22008-01-24 16:13:08 -05004689 set_extent_uptodate(io_tree, em->start,
4690 extent_map_end(em) - 1, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04004691 goto insert;
4692 } else {
Chris Masond3977122009-01-05 21:25:51 -05004693 printk(KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04004694 WARN_ON(1);
4695 }
4696not_found:
4697 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05004698 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04004699not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04004700 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04004701 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04004702insert:
4703 btrfs_release_path(root, path);
Chris Masond1310b22008-01-24 16:13:08 -05004704 if (em->start > start || extent_map_end(em) <= start) {
Chris Masond3977122009-01-05 21:25:51 -05004705 printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
4706 "[%llu %llu]\n", (unsigned long long)em->start,
4707 (unsigned long long)em->len,
4708 (unsigned long long)start,
4709 (unsigned long long)len);
Chris Masona52d9a82007-08-27 16:49:44 -04004710 err = -EIO;
4711 goto out;
4712 }
Chris Masond1310b22008-01-24 16:13:08 -05004713
4714 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04004715 write_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04004716 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04004717 /* it is possible that someone inserted the extent into the tree
4718 * while we had the lock dropped. It is also possible that
4719 * an overlapping map exists in the tree
4720 */
Chris Masona52d9a82007-08-27 16:49:44 -04004721 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04004722 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004723
4724 ret = 0;
4725
Chris Mason3b951512008-04-17 11:29:12 -04004726 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04004727 if (existing && (existing->start > start ||
4728 existing->start + existing->len <= start)) {
4729 free_extent_map(existing);
4730 existing = NULL;
4731 }
Chris Mason3b951512008-04-17 11:29:12 -04004732 if (!existing) {
4733 existing = lookup_extent_mapping(em_tree, em->start,
4734 em->len);
4735 if (existing) {
4736 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04004737 em, start,
4738 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04004739 free_extent_map(existing);
4740 if (err) {
4741 free_extent_map(em);
4742 em = NULL;
4743 }
4744 } else {
4745 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04004746 free_extent_map(em);
4747 em = NULL;
4748 }
4749 } else {
4750 free_extent_map(em);
4751 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004752 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04004753 }
Chris Masona52d9a82007-08-27 16:49:44 -04004754 }
Chris Mason890871b2009-09-02 16:24:52 -04004755 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04004756out:
Chris Masonf4219502008-07-22 11:18:09 -04004757 if (path)
4758 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04004759 if (trans) {
4760 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05004761 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04004762 err = ret;
4763 }
Chris Masona52d9a82007-08-27 16:49:44 -04004764 if (err) {
4765 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04004766 return ERR_PTR(err);
4767 }
4768 return em;
4769}
4770
Chris Mason16432982008-04-10 10:23:21 -04004771static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
4772 const struct iovec *iov, loff_t offset,
4773 unsigned long nr_segs)
4774{
Chris Masone1c4b742008-04-22 13:26:46 -04004775 return -EINVAL;
Chris Mason16432982008-04-10 10:23:21 -04004776}
4777
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004778static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
4779 __u64 start, __u64 len)
4780{
4781 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent);
4782}
4783
Chris Mason9ebefb182007-06-15 13:50:00 -04004784int btrfs_readpage(struct file *file, struct page *page)
4785{
Chris Masond1310b22008-01-24 16:13:08 -05004786 struct extent_io_tree *tree;
4787 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04004788 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04004789}
Chris Mason1832a6d2007-12-21 16:27:21 -05004790
Chris Mason39279cc2007-06-12 06:35:45 -04004791static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
4792{
Chris Masond1310b22008-01-24 16:13:08 -05004793 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04004794
4795
4796 if (current->flags & PF_MEMALLOC) {
4797 redirty_page_for_writepage(wbc, page);
4798 unlock_page(page);
4799 return 0;
4800 }
Chris Masond1310b22008-01-24 16:13:08 -05004801 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04004802 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
4803}
Chris Mason39279cc2007-06-12 06:35:45 -04004804
Chris Masonf4219502008-07-22 11:18:09 -04004805int btrfs_writepages(struct address_space *mapping,
4806 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04004807{
Chris Masond1310b22008-01-24 16:13:08 -05004808 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05004809
Chris Masond1310b22008-01-24 16:13:08 -05004810 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04004811 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
4812}
4813
Chris Mason3ab2fb52007-11-08 10:59:22 -05004814static int
4815btrfs_readpages(struct file *file, struct address_space *mapping,
4816 struct list_head *pages, unsigned nr_pages)
4817{
Chris Masond1310b22008-01-24 16:13:08 -05004818 struct extent_io_tree *tree;
4819 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05004820 return extent_readpages(tree, mapping, pages, nr_pages,
4821 btrfs_get_extent);
4822}
Chris Masone6dcd2d2008-07-17 12:53:50 -04004823static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04004824{
Chris Masond1310b22008-01-24 16:13:08 -05004825 struct extent_io_tree *tree;
4826 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04004827 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004828
Chris Masond1310b22008-01-24 16:13:08 -05004829 tree = &BTRFS_I(page->mapping->host)->io_tree;
4830 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05004831 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04004832 if (ret == 1) {
4833 ClearPagePrivate(page);
4834 set_page_private(page, 0);
4835 page_cache_release(page);
4836 }
4837 return ret;
4838}
Chris Mason39279cc2007-06-12 06:35:45 -04004839
Chris Masone6dcd2d2008-07-17 12:53:50 -04004840static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
4841{
Chris Mason98509cf2008-09-11 15:51:43 -04004842 if (PageWriteback(page) || PageDirty(page))
4843 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05004844 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004845}
4846
Chris Masona52d9a82007-08-27 16:49:44 -04004847static void btrfs_invalidatepage(struct page *page, unsigned long offset)
4848{
Chris Masond1310b22008-01-24 16:13:08 -05004849 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004850 struct btrfs_ordered_extent *ordered;
4851 u64 page_start = page_offset(page);
4852 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04004853
Chris Mason8b62b722009-09-02 16:53:46 -04004854
4855 /*
4856 * we have the page locked, so new writeback can't start,
4857 * and the dirty bit won't be cleared while we are here.
4858 *
4859 * Wait for IO on this page so that we can safely clear
4860 * the PagePrivate2 bit and do ordered accounting
4861 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04004862 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04004863
Chris Masond1310b22008-01-24 16:13:08 -05004864 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004865 if (offset) {
4866 btrfs_releasepage(page, GFP_NOFS);
4867 return;
4868 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004869 lock_extent(tree, page_start, page_end, GFP_NOFS);
4870 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
4871 page_offset(page));
4872 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04004873 /*
4874 * IO on this page will never be started, so we need
4875 * to account for any ordered extents now
4876 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04004877 clear_extent_bit(tree, page_start, page_end,
4878 EXTENT_DIRTY | EXTENT_DELALLOC |
Josef Bacik32c00af2009-10-08 13:34:05 -04004879 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING, 1, 0,
4880 NULL, GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04004881 /*
4882 * whoever cleared the private bit is responsible
4883 * for the finish_ordered_io
4884 */
4885 if (TestClearPagePrivate2(page)) {
4886 btrfs_finish_ordered_io(page->mapping->host,
4887 page_start, page_end);
4888 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004889 btrfs_put_ordered_extent(ordered);
4890 lock_extent(tree, page_start, page_end, GFP_NOFS);
4891 }
4892 clear_extent_bit(tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04004893 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
4894 EXTENT_DO_ACCOUNTING, 1, 1, NULL, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004895 __btrfs_releasepage(page, GFP_NOFS);
4896
Chris Mason4a096752008-07-21 10:29:44 -04004897 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04004898 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04004899 ClearPagePrivate(page);
4900 set_page_private(page, 0);
4901 page_cache_release(page);
4902 }
Chris Mason39279cc2007-06-12 06:35:45 -04004903}
4904
Chris Mason9ebefb182007-06-15 13:50:00 -04004905/*
4906 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
4907 * called from a page fault handler when a page is first dirtied. Hence we must
4908 * be careful to check for EOF conditions here. We set the page up correctly
4909 * for a written page which means we get ENOSPC checking when writing into
4910 * holes and correct delalloc and unwritten extent mapping on filesystems that
4911 * support these features.
4912 *
4913 * We are not allowed to take the i_mutex here so we have to play games to
4914 * protect against truncate races as the page could now be beyond EOF. Because
4915 * vmtruncate() writes the inode size before removing pages, once we have the
4916 * page lock we can determine safely if the page is beyond EOF. If it is not
4917 * beyond EOF, then the page is guaranteed safe against truncation until we
4918 * unlock the page.
4919 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07004920int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04004921{
Nick Pigginc2ec1752009-03-31 15:23:21 -07004922 struct page *page = vmf->page;
Chris Mason6da6aba2007-12-18 16:15:09 -05004923 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05004924 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004925 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4926 struct btrfs_ordered_extent *ordered;
4927 char *kaddr;
4928 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04004929 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05004930 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04004931 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004932 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04004933
Josef Bacik6a632092009-02-20 11:00:09 -05004934 ret = btrfs_check_data_free_space(root, inode, PAGE_CACHE_SIZE);
Nick Piggin56a76f82009-03-31 15:23:23 -07004935 if (ret) {
4936 if (ret == -ENOMEM)
4937 ret = VM_FAULT_OOM;
4938 else /* -ENOSPC, -EIO, etc */
4939 ret = VM_FAULT_SIGBUS;
Chris Mason1832a6d2007-12-21 16:27:21 -05004940 goto out;
Nick Piggin56a76f82009-03-31 15:23:23 -07004941 }
Chris Mason1832a6d2007-12-21 16:27:21 -05004942
Josef Bacik9ed74f22009-09-11 16:12:44 -04004943 ret = btrfs_reserve_metadata_for_delalloc(root, inode, 1);
4944 if (ret) {
4945 btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
4946 ret = VM_FAULT_SIGBUS;
4947 goto out;
4948 }
4949
Nick Piggin56a76f82009-03-31 15:23:23 -07004950 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04004951again:
Chris Mason9ebefb182007-06-15 13:50:00 -04004952 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04004953 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004954 page_start = page_offset(page);
4955 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04004956
Chris Mason9ebefb182007-06-15 13:50:00 -04004957 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04004958 (page_start >= size)) {
Josef Bacik6a632092009-02-20 11:00:09 -05004959 btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
Chris Mason9ebefb182007-06-15 13:50:00 -04004960 /* page got truncated out from underneath us */
4961 goto out_unlock;
4962 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004963 wait_on_page_writeback(page);
4964
4965 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
4966 set_page_extent_mapped(page);
4967
Chris Masoneb84ae02008-07-17 13:53:27 -04004968 /*
4969 * we can't set the delalloc bits if there are pending ordered
4970 * extents. Drop our locks and wait for them to finish
4971 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04004972 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4973 if (ordered) {
4974 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
4975 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004976 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004977 btrfs_put_ordered_extent(ordered);
4978 goto again;
4979 }
4980
Josef Bacikfbf19082009-10-01 17:10:23 -04004981 /*
4982 * XXX - page_mkwrite gets called every time the page is dirtied, even
4983 * if it was already dirty, so for space accounting reasons we need to
4984 * clear any delalloc bits for the range we are fixing to save. There
4985 * is probably a better way to do this, but for now keep consistent with
4986 * prepare_pages in the normal write path.
4987 */
4988 clear_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04004989 EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
4990 GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04004991
Josef Bacik9ed74f22009-09-11 16:12:44 -04004992 ret = btrfs_set_extent_delalloc(inode, page_start, page_end);
4993 if (ret) {
4994 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
4995 ret = VM_FAULT_SIGBUS;
Josef Bacikfbf19082009-10-01 17:10:23 -04004996 btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004997 goto out_unlock;
4998 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004999 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04005000
5001 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04005002 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04005003 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04005004 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04005005 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04005006
Chris Masone6dcd2d2008-07-17 12:53:50 -04005007 if (zero_start != PAGE_CACHE_SIZE) {
5008 kaddr = kmap(page);
5009 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
5010 flush_dcache_page(page);
5011 kunmap(page);
5012 }
Chris Mason247e7432008-07-17 12:53:51 -04005013 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005014 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04005015 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04005016
Chris Mason257c62e2009-10-13 13:21:08 -04005017 BTRFS_I(inode)->last_trans = root->fs_info->generation;
5018 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
5019
Chris Masone6dcd2d2008-07-17 12:53:50 -04005020 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04005021
5022out_unlock:
Josef Bacik9ed74f22009-09-11 16:12:44 -04005023 btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
Chris Mason50a9b212009-09-11 12:33:12 -04005024 if (!ret)
5025 return VM_FAULT_LOCKED;
Chris Mason9ebefb182007-06-15 13:50:00 -04005026 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05005027out:
Chris Mason9ebefb182007-06-15 13:50:00 -04005028 return ret;
5029}
5030
Chris Mason39279cc2007-06-12 06:35:45 -04005031static void btrfs_truncate(struct inode *inode)
5032{
5033 struct btrfs_root *root = BTRFS_I(inode)->root;
5034 int ret;
5035 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04005036 unsigned long nr;
Chris Masondbe674a2008-07-17 12:54:05 -04005037 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04005038
5039 if (!S_ISREG(inode->i_mode))
5040 return;
5041 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
5042 return;
5043
Josef Bacik5d5e1032009-10-13 16:46:49 -04005044 ret = btrfs_truncate_page(inode->i_mapping, inode->i_size);
5045 if (ret)
5046 return;
Chris Mason4a096752008-07-21 10:29:44 -04005047 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04005048
Chris Mason39279cc2007-06-12 06:35:45 -04005049 trans = btrfs_start_transaction(root, 1);
Chris Mason5a3f23d2009-03-31 13:27:11 -04005050
5051 /*
5052 * setattr is responsible for setting the ordered_data_close flag,
5053 * but that is only tested during the last file release. That
5054 * could happen well after the next commit, leaving a great big
5055 * window where new writes may get lost if someone chooses to write
5056 * to this file after truncating to zero
5057 *
5058 * The inode doesn't have any dirty data here, and so if we commit
5059 * this is a noop. If someone immediately starts writing to the inode
5060 * it is very likely we'll catch some of their writes in this
5061 * transaction, and the commit will find this file on the ordered
5062 * data list with good things to send down.
5063 *
5064 * This is a best effort solution, there is still a window where
5065 * using truncate to replace the contents of the file will
5066 * end up with a zero length file after a crash.
5067 */
5068 if (inode->i_size == 0 && BTRFS_I(inode)->ordered_data_close)
5069 btrfs_add_ordered_operation(trans, root, inode);
5070
Chris Mason39279cc2007-06-12 06:35:45 -04005071 btrfs_set_trans_block_group(trans, inode);
Chris Masondbe674a2008-07-17 12:54:05 -04005072 btrfs_i_size_write(inode, inode->i_size);
Chris Mason39279cc2007-06-12 06:35:45 -04005073
Josef Bacik7b128762008-07-24 12:17:14 -04005074 ret = btrfs_orphan_add(trans, inode);
5075 if (ret)
5076 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04005077 /* FIXME, add redo link to tree so we don't leak on crash */
Chris Masone02119d2008-09-05 16:13:11 -04005078 ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size,
Chris Mason85e21ba2008-01-29 15:11:36 -05005079 BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04005080 btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005081
Josef Bacik7b128762008-07-24 12:17:14 -04005082 ret = btrfs_orphan_del(trans, inode);
5083 BUG_ON(ret);
5084
5085out:
5086 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04005087 ret = btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005088 BUG_ON(ret);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04005089 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04005090}
5091
Sven Wegener3b963622008-06-09 21:57:42 -04005092/*
Chris Masond352ac62008-09-29 15:18:18 -04005093 * create a new subvolume directory/inode (helper for the ioctl).
5094 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05005095int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Yan, Zheng76dda932009-09-21 16:00:26 -04005096 struct btrfs_root *new_root,
Yan Zhengd2fb3432008-12-11 16:30:39 -05005097 u64 new_dirid, u64 alloc_hint)
Chris Mason39279cc2007-06-12 06:35:45 -04005098{
Chris Mason39279cc2007-06-12 06:35:45 -04005099 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005100 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005101 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005102
Josef Bacikaec74772008-07-24 12:12:38 -04005103 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
Yan Zhengd2fb3432008-12-11 16:30:39 -05005104 new_dirid, alloc_hint, S_IFDIR | 0700, &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04005105 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005106 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005107 inode->i_op = &btrfs_dir_inode_operations;
5108 inode->i_fop = &btrfs_dir_file_operations;
5109
Chris Mason39279cc2007-06-12 06:35:45 -04005110 inode->i_nlink = 1;
Chris Masondbe674a2008-07-17 12:54:05 -04005111 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04005112
Yan, Zheng76dda932009-09-21 16:00:26 -04005113 err = btrfs_update_inode(trans, new_root, inode);
5114 BUG_ON(err);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04005115
Yan, Zheng76dda932009-09-21 16:00:26 -04005116 iput(inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04005117 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005118}
5119
Chris Masond352ac62008-09-29 15:18:18 -04005120/* helper function for file defrag and space balancing. This
5121 * forces readahead on a given range of bytes in an inode
5122 */
Chris Masonedbd8d42007-12-21 16:27:24 -05005123unsigned long btrfs_force_ra(struct address_space *mapping,
Chris Mason86479a02007-09-10 19:58:16 -04005124 struct file_ra_state *ra, struct file *file,
5125 pgoff_t offset, pgoff_t last_index)
5126{
Chris Mason8e7bf942008-04-28 09:02:36 -04005127 pgoff_t req_size = last_index - offset + 1;
Chris Mason86479a02007-09-10 19:58:16 -04005128
Chris Mason86479a02007-09-10 19:58:16 -04005129 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
5130 return offset + req_size;
Chris Mason86479a02007-09-10 19:58:16 -04005131}
5132
Chris Mason39279cc2007-06-12 06:35:45 -04005133struct inode *btrfs_alloc_inode(struct super_block *sb)
5134{
5135 struct btrfs_inode *ei;
5136
5137 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
5138 if (!ei)
5139 return NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04005140 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04005141 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04005142 ei->logged_trans = 0;
Josef Bacik32c00af2009-10-08 13:34:05 -04005143 ei->outstanding_extents = 0;
5144 ei->reserved_extents = 0;
5145 spin_lock_init(&ei->accounting_lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005146 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Josef Bacik7b128762008-07-24 12:17:14 -04005147 INIT_LIST_HEAD(&ei->i_orphan);
Chris Mason5a3f23d2009-03-31 13:27:11 -04005148 INIT_LIST_HEAD(&ei->ordered_operations);
Chris Mason39279cc2007-06-12 06:35:45 -04005149 return &ei->vfs_inode;
5150}
5151
5152void btrfs_destroy_inode(struct inode *inode)
5153{
Chris Masone6dcd2d2008-07-17 12:53:50 -04005154 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04005155 struct btrfs_root *root = BTRFS_I(inode)->root;
5156
Chris Mason39279cc2007-06-12 06:35:45 -04005157 WARN_ON(!list_empty(&inode->i_dentry));
5158 WARN_ON(inode->i_data.nrpages);
5159
Chris Mason5a3f23d2009-03-31 13:27:11 -04005160 /*
5161 * Make sure we're properly removed from the ordered operation
5162 * lists.
5163 */
5164 smp_mb();
5165 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
5166 spin_lock(&root->fs_info->ordered_extent_lock);
5167 list_del_init(&BTRFS_I(inode)->ordered_operations);
5168 spin_unlock(&root->fs_info->ordered_extent_lock);
5169 }
5170
5171 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04005172 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
5173 printk(KERN_ERR "BTRFS: inode %lu: inode still on the orphan"
5174 " list\n", inode->i_ino);
5175 dump_stack();
5176 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04005177 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04005178
Chris Masond3977122009-01-05 21:25:51 -05005179 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04005180 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
5181 if (!ordered)
5182 break;
5183 else {
Chris Masond3977122009-01-05 21:25:51 -05005184 printk(KERN_ERR "btrfs found ordered "
5185 "extent %llu %llu on inode cleanup\n",
5186 (unsigned long long)ordered->file_offset,
5187 (unsigned long long)ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005188 btrfs_remove_ordered_extent(inode, ordered);
5189 btrfs_put_ordered_extent(ordered);
5190 btrfs_put_ordered_extent(ordered);
5191 }
5192 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005193 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04005194 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005195 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
5196}
5197
Yan, Zheng76dda932009-09-21 16:00:26 -04005198void btrfs_drop_inode(struct inode *inode)
5199{
5200 struct btrfs_root *root = BTRFS_I(inode)->root;
5201
5202 if (inode->i_nlink > 0 && btrfs_root_refs(&root->root_item) == 0)
5203 generic_delete_inode(inode);
5204 else
5205 generic_drop_inode(inode);
5206}
5207
Sven Wegener0ee0fda2008-07-30 16:54:26 -04005208static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04005209{
5210 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
5211
5212 inode_init_once(&ei->vfs_inode);
5213}
5214
5215void btrfs_destroy_cachep(void)
5216{
5217 if (btrfs_inode_cachep)
5218 kmem_cache_destroy(btrfs_inode_cachep);
5219 if (btrfs_trans_handle_cachep)
5220 kmem_cache_destroy(btrfs_trans_handle_cachep);
5221 if (btrfs_transaction_cachep)
5222 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04005223 if (btrfs_path_cachep)
5224 kmem_cache_destroy(btrfs_path_cachep);
5225}
5226
5227int btrfs_init_cachep(void)
5228{
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02005229 btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
5230 sizeof(struct btrfs_inode), 0,
5231 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04005232 if (!btrfs_inode_cachep)
5233 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02005234
5235 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
5236 sizeof(struct btrfs_trans_handle), 0,
5237 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005238 if (!btrfs_trans_handle_cachep)
5239 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02005240
5241 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
5242 sizeof(struct btrfs_transaction), 0,
5243 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005244 if (!btrfs_transaction_cachep)
5245 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02005246
5247 btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
5248 sizeof(struct btrfs_path), 0,
5249 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005250 if (!btrfs_path_cachep)
5251 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02005252
Chris Mason39279cc2007-06-12 06:35:45 -04005253 return 0;
5254fail:
5255 btrfs_destroy_cachep();
5256 return -ENOMEM;
5257}
5258
5259static int btrfs_getattr(struct vfsmount *mnt,
5260 struct dentry *dentry, struct kstat *stat)
5261{
5262 struct inode *inode = dentry->d_inode;
5263 generic_fillattr(inode, stat);
Chris Mason3394e162008-11-17 20:42:26 -05005264 stat->dev = BTRFS_I(inode)->root->anon_super.s_dev;
Chris Masond6667462008-01-03 14:51:00 -05005265 stat->blksize = PAGE_CACHE_SIZE;
Yan Zhenga76a3cd2008-10-09 11:46:29 -04005266 stat->blocks = (inode_get_bytes(inode) +
5267 BTRFS_I(inode)->delalloc_bytes) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04005268 return 0;
5269}
5270
Chris Masond3977122009-01-05 21:25:51 -05005271static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
5272 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005273{
5274 struct btrfs_trans_handle *trans;
5275 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005276 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005277 struct inode *new_inode = new_dentry->d_inode;
5278 struct inode *old_inode = old_dentry->d_inode;
5279 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005280 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005281 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04005282 int ret;
5283
Yan, Zhengf679a842009-09-24 09:17:31 -04005284 if (new_dir->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5285 return -EPERM;
5286
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005287 /* we only allow rename subvolume link between subvolumes */
5288 if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05005289 return -EXDEV;
5290
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005291 if (old_inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
5292 (new_inode && new_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID))
5293 return -ENOTEMPTY;
5294
Chris Mason39279cc2007-06-12 06:35:45 -04005295 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005296 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04005297 return -ENOTEMPTY;
Chris Mason0660b5a2008-11-17 20:37:39 -05005298
Josef Bacik9ed74f22009-09-11 16:12:44 -04005299 /*
5300 * 2 items for dir items
5301 * 1 item for orphan entry
5302 * 1 item for ref
5303 */
5304 ret = btrfs_reserve_metadata_space(root, 4);
Chris Mason1832a6d2007-12-21 16:27:21 -05005305 if (ret)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005306 return ret;
Chris Mason1832a6d2007-12-21 16:27:21 -05005307
Chris Mason5a3f23d2009-03-31 13:27:11 -04005308 /*
5309 * we're using rename to replace one file with another.
5310 * and the replacement file is large. Start IO on it now so
5311 * we don't add too much work to the end of the transaction
5312 */
Bartlomiej Zolnierkiewicz4baf8c92009-08-07 13:47:08 -04005313 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
Chris Mason5a3f23d2009-03-31 13:27:11 -04005314 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
5315 filemap_flush(old_inode->i_mapping);
5316
Yan, Zheng76dda932009-09-21 16:00:26 -04005317 /* close the racy window with snapshot create/destroy ioctl */
5318 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
5319 down_read(&root->fs_info->subvol_sem);
5320
Chris Mason39279cc2007-06-12 06:35:45 -04005321 trans = btrfs_start_transaction(root, 1);
Yan, Zhenga5719522009-09-24 09:17:31 -04005322 btrfs_set_trans_block_group(trans, new_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04005323
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005324 if (dest != root)
5325 btrfs_record_root_in_trans(trans, dest);
5326
Yan, Zhenga5719522009-09-24 09:17:31 -04005327 ret = btrfs_set_inode_index(new_dir, &index);
5328 if (ret)
5329 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04005330
Yan, Zhenga5719522009-09-24 09:17:31 -04005331 if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005332 /* force full log commit if subvolume involved. */
5333 root->fs_info->last_trans_log_full_commit = trans->transid;
5334 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04005335 ret = btrfs_insert_inode_ref(trans, dest,
5336 new_dentry->d_name.name,
5337 new_dentry->d_name.len,
5338 old_inode->i_ino,
5339 new_dir->i_ino, index);
5340 if (ret)
5341 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005342 /*
5343 * this is an ugly little race, but the rename is required
5344 * to make sure that if we crash, the inode is either at the
5345 * old name or the new one. pinning the log transaction lets
5346 * us make sure we don't allow a log commit to come in after
5347 * we unlink the name but before we add the new name back in.
5348 */
5349 btrfs_pin_log_trans(root);
5350 }
Yan, Zhenga5719522009-09-24 09:17:31 -04005351 /*
5352 * make sure the inode gets flushed if it is replacing
5353 * something.
5354 */
5355 if (new_inode && new_inode->i_size &&
5356 old_inode && S_ISREG(old_inode->i_mode)) {
5357 btrfs_add_ordered_operation(trans, root, old_inode);
5358 }
Chris Mason39279cc2007-06-12 06:35:45 -04005359
Chris Mason39279cc2007-06-12 06:35:45 -04005360 old_dir->i_ctime = old_dir->i_mtime = ctime;
5361 new_dir->i_ctime = new_dir->i_mtime = ctime;
5362 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04005363
Chris Mason12fcfd22009-03-24 10:24:20 -04005364 if (old_dentry->d_parent != new_dentry->d_parent)
5365 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
5366
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005367 if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
5368 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
5369 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
5370 old_dentry->d_name.name,
5371 old_dentry->d_name.len);
5372 } else {
5373 btrfs_inc_nlink(old_dentry->d_inode);
5374 ret = btrfs_unlink_inode(trans, root, old_dir,
5375 old_dentry->d_inode,
5376 old_dentry->d_name.name,
5377 old_dentry->d_name.len);
5378 }
5379 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005380
5381 if (new_inode) {
5382 new_inode->i_ctime = CURRENT_TIME;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005383 if (unlikely(new_inode->i_ino ==
5384 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
5385 root_objectid = BTRFS_I(new_inode)->location.objectid;
5386 ret = btrfs_unlink_subvol(trans, dest, new_dir,
5387 root_objectid,
5388 new_dentry->d_name.name,
5389 new_dentry->d_name.len);
5390 BUG_ON(new_inode->i_nlink == 0);
5391 } else {
5392 ret = btrfs_unlink_inode(trans, dest, new_dir,
5393 new_dentry->d_inode,
5394 new_dentry->d_name.name,
5395 new_dentry->d_name.len);
5396 }
5397 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04005398 if (new_inode->i_nlink == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04005399 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005400 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04005401 }
Chris Mason39279cc2007-06-12 06:35:45 -04005402 }
Josef Bacikaec74772008-07-24 12:12:38 -04005403
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005404 ret = btrfs_add_link(trans, new_dir, old_inode,
5405 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04005406 new_dentry->d_name.len, 0, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005407 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005408
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005409 if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) {
5410 btrfs_log_new_name(trans, old_inode, old_dir,
5411 new_dentry->d_parent);
5412 btrfs_end_log_trans(root);
5413 }
Yan, Zhenga5719522009-09-24 09:17:31 -04005414out_fail:
Chris Masonab78c842008-07-29 16:15:18 -04005415 btrfs_end_transaction_throttle(trans, root);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005416
Yan, Zheng76dda932009-09-21 16:00:26 -04005417 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
5418 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005419
5420 btrfs_unreserve_metadata_space(root, 4);
Chris Mason39279cc2007-06-12 06:35:45 -04005421 return ret;
5422}
5423
Chris Masond352ac62008-09-29 15:18:18 -04005424/*
5425 * some fairly slow code that needs optimization. This walks the list
5426 * of all the inodes with pending delalloc and forces them to disk.
5427 */
Chris Masonea8c2812008-08-04 23:17:27 -04005428int btrfs_start_delalloc_inodes(struct btrfs_root *root)
5429{
5430 struct list_head *head = &root->fs_info->delalloc_inodes;
5431 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04005432 struct inode *inode;
Chris Masonea8c2812008-08-04 23:17:27 -04005433
Yan Zhengc146afa2008-11-12 14:34:12 -05005434 if (root->fs_info->sb->s_flags & MS_RDONLY)
5435 return -EROFS;
5436
Chris Mason75eff682008-12-15 15:54:40 -05005437 spin_lock(&root->fs_info->delalloc_lock);
Chris Masond3977122009-01-05 21:25:51 -05005438 while (!list_empty(head)) {
Chris Masonea8c2812008-08-04 23:17:27 -04005439 binode = list_entry(head->next, struct btrfs_inode,
5440 delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04005441 inode = igrab(&binode->vfs_inode);
5442 if (!inode)
5443 list_del_init(&binode->delalloc_inodes);
Chris Mason75eff682008-12-15 15:54:40 -05005444 spin_unlock(&root->fs_info->delalloc_lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04005445 if (inode) {
Chris Mason8c8bee12008-09-29 11:19:10 -04005446 filemap_flush(inode->i_mapping);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04005447 iput(inode);
5448 }
5449 cond_resched();
Chris Mason75eff682008-12-15 15:54:40 -05005450 spin_lock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04005451 }
Chris Mason75eff682008-12-15 15:54:40 -05005452 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04005453
5454 /* the filemap_flush will queue IO into the worker threads, but
5455 * we have to make sure the IO is actually started and that
5456 * ordered extents get created before we return
5457 */
5458 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05005459 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05005460 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04005461 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05005462 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
5463 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04005464 }
5465 atomic_dec(&root->fs_info->async_submit_draining);
Chris Masonea8c2812008-08-04 23:17:27 -04005466 return 0;
5467}
5468
Chris Mason39279cc2007-06-12 06:35:45 -04005469static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
5470 const char *symname)
5471{
5472 struct btrfs_trans_handle *trans;
5473 struct btrfs_root *root = BTRFS_I(dir)->root;
5474 struct btrfs_path *path;
5475 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05005476 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04005477 int err;
5478 int drop_inode = 0;
5479 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005480 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04005481 int name_len;
5482 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04005483 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04005484 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04005485 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05005486 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005487
5488 name_len = strlen(symname) + 1;
5489 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
5490 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05005491
Josef Bacik9ed74f22009-09-11 16:12:44 -04005492 /*
5493 * 2 items for inode item and ref
5494 * 2 items for dir items
5495 * 1 item for xattr if selinux is on
5496 */
5497 err = btrfs_reserve_metadata_space(root, 5);
Chris Mason1832a6d2007-12-21 16:27:21 -05005498 if (err)
Josef Bacik9ed74f22009-09-11 16:12:44 -04005499 return err;
Chris Mason1832a6d2007-12-21 16:27:21 -05005500
Chris Mason39279cc2007-06-12 06:35:45 -04005501 trans = btrfs_start_transaction(root, 1);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005502 if (!trans)
5503 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04005504 btrfs_set_trans_block_group(trans, dir);
5505
5506 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
5507 if (err) {
5508 err = -ENOSPC;
5509 goto out_unlock;
5510 }
5511
Josef Bacikaec74772008-07-24 12:12:38 -04005512 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05005513 dentry->d_name.len,
5514 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04005515 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO,
5516 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04005517 err = PTR_ERR(inode);
5518 if (IS_ERR(inode))
5519 goto out_unlock;
5520
Jim Owens0279b4c2009-02-04 09:29:13 -05005521 err = btrfs_init_inode_security(inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04005522 if (err) {
5523 drop_inode = 1;
5524 goto out_unlock;
5525 }
5526
Chris Mason39279cc2007-06-12 06:35:45 -04005527 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04005528 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005529 if (err)
5530 drop_inode = 1;
5531 else {
5532 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04005533 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04005534 inode->i_fop = &btrfs_file_operations;
5535 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05005536 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04005537 }
Chris Mason39279cc2007-06-12 06:35:45 -04005538 btrfs_update_inode_block_group(trans, inode);
5539 btrfs_update_inode_block_group(trans, dir);
5540 if (drop_inode)
5541 goto out_unlock;
5542
5543 path = btrfs_alloc_path();
5544 BUG_ON(!path);
5545 key.objectid = inode->i_ino;
5546 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005547 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
5548 datasize = btrfs_file_extent_calc_inline_size(name_len);
5549 err = btrfs_insert_empty_item(trans, root, path, &key,
5550 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04005551 if (err) {
5552 drop_inode = 1;
5553 goto out_unlock;
5554 }
Chris Mason5f39d392007-10-15 16:14:19 -04005555 leaf = path->nodes[0];
5556 ei = btrfs_item_ptr(leaf, path->slots[0],
5557 struct btrfs_file_extent_item);
5558 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
5559 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04005560 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04005561 btrfs_set_file_extent_encryption(leaf, ei, 0);
5562 btrfs_set_file_extent_compression(leaf, ei, 0);
5563 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
5564 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
5565
Chris Mason39279cc2007-06-12 06:35:45 -04005566 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04005567 write_extent_buffer(leaf, symname, ptr, name_len);
5568 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04005569 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04005570
Chris Mason39279cc2007-06-12 06:35:45 -04005571 inode->i_op = &btrfs_symlink_inode_operations;
5572 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04005573 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04005574 inode_set_bytes(inode, name_len);
Chris Masondbe674a2008-07-17 12:54:05 -04005575 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04005576 err = btrfs_update_inode(trans, root, inode);
5577 if (err)
5578 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04005579
5580out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04005581 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04005582 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05005583out_fail:
Josef Bacik9ed74f22009-09-11 16:12:44 -04005584 btrfs_unreserve_metadata_space(root, 5);
Chris Mason39279cc2007-06-12 06:35:45 -04005585 if (drop_inode) {
5586 inode_dec_link_count(inode);
5587 iput(inode);
5588 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04005589 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04005590 return err;
5591}
Chris Mason16432982008-04-10 10:23:21 -04005592
Chris Mason546888d2009-04-21 11:53:38 -04005593static int prealloc_file_range(struct btrfs_trans_handle *trans,
5594 struct inode *inode, u64 start, u64 end,
Chris Masone980b502009-04-24 14:39:24 -04005595 u64 locked_end, u64 alloc_hint, int mode)
Yan Zhengd899e052008-10-30 14:25:28 -04005596{
Yan Zhengd899e052008-10-30 14:25:28 -04005597 struct btrfs_root *root = BTRFS_I(inode)->root;
5598 struct btrfs_key ins;
5599 u64 alloc_size;
5600 u64 cur_offset = start;
5601 u64 num_bytes = end - start;
5602 int ret = 0;
5603
Yan Zhengd899e052008-10-30 14:25:28 -04005604 while (num_bytes > 0) {
5605 alloc_size = min(num_bytes, root->fs_info->max_extent);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005606
5607 ret = btrfs_reserve_metadata_space(root, 1);
5608 if (ret)
5609 goto out;
5610
Yan Zhengd899e052008-10-30 14:25:28 -04005611 ret = btrfs_reserve_extent(trans, root, alloc_size,
5612 root->sectorsize, 0, alloc_hint,
5613 (u64)-1, &ins, 1);
5614 if (ret) {
5615 WARN_ON(1);
5616 goto out;
5617 }
5618 ret = insert_reserved_file_extent(trans, inode,
5619 cur_offset, ins.objectid,
5620 ins.offset, ins.offset,
Chris Masone980b502009-04-24 14:39:24 -04005621 ins.offset, locked_end,
5622 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04005623 BTRFS_FILE_EXTENT_PREALLOC);
5624 BUG_ON(ret);
Chris Masona1ed8352009-09-11 12:27:37 -04005625 btrfs_drop_extent_cache(inode, cur_offset,
5626 cur_offset + ins.offset -1, 0);
Yan Zhengd899e052008-10-30 14:25:28 -04005627 num_bytes -= ins.offset;
5628 cur_offset += ins.offset;
5629 alloc_hint = ins.objectid + ins.offset;
Josef Bacik9ed74f22009-09-11 16:12:44 -04005630 btrfs_unreserve_metadata_space(root, 1);
Yan Zhengd899e052008-10-30 14:25:28 -04005631 }
5632out:
5633 if (cur_offset > start) {
5634 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005635 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04005636 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
5637 cur_offset > i_size_read(inode))
5638 btrfs_i_size_write(inode, cur_offset);
5639 ret = btrfs_update_inode(trans, root, inode);
5640 BUG_ON(ret);
5641 }
5642
Yan Zhengd899e052008-10-30 14:25:28 -04005643 return ret;
5644}
5645
5646static long btrfs_fallocate(struct inode *inode, int mode,
5647 loff_t offset, loff_t len)
5648{
5649 u64 cur_offset;
5650 u64 last_byte;
5651 u64 alloc_start;
5652 u64 alloc_end;
5653 u64 alloc_hint = 0;
Chris Masone980b502009-04-24 14:39:24 -04005654 u64 locked_end;
Yan Zhengd899e052008-10-30 14:25:28 -04005655 u64 mask = BTRFS_I(inode)->root->sectorsize - 1;
5656 struct extent_map *em;
Chris Mason546888d2009-04-21 11:53:38 -04005657 struct btrfs_trans_handle *trans;
Josef Bacika970b0a2009-06-27 21:07:34 -04005658 struct btrfs_root *root;
Yan Zhengd899e052008-10-30 14:25:28 -04005659 int ret;
5660
5661 alloc_start = offset & ~mask;
5662 alloc_end = (offset + len + mask) & ~mask;
5663
Chris Mason546888d2009-04-21 11:53:38 -04005664 /*
5665 * wait for ordered IO before we have any locks. We'll loop again
5666 * below with the locks held.
5667 */
5668 btrfs_wait_ordered_range(inode, alloc_start, alloc_end - alloc_start);
5669
Yan Zhengd899e052008-10-30 14:25:28 -04005670 mutex_lock(&inode->i_mutex);
5671 if (alloc_start > inode->i_size) {
5672 ret = btrfs_cont_expand(inode, alloc_start);
5673 if (ret)
5674 goto out;
5675 }
5676
Josef Bacika970b0a2009-06-27 21:07:34 -04005677 root = BTRFS_I(inode)->root;
5678
5679 ret = btrfs_check_data_free_space(root, inode,
5680 alloc_end - alloc_start);
5681 if (ret)
5682 goto out;
5683
Chris Masone980b502009-04-24 14:39:24 -04005684 locked_end = alloc_end - 1;
Yan Zhengd899e052008-10-30 14:25:28 -04005685 while (1) {
5686 struct btrfs_ordered_extent *ordered;
Chris Mason546888d2009-04-21 11:53:38 -04005687
5688 trans = btrfs_start_transaction(BTRFS_I(inode)->root, 1);
5689 if (!trans) {
5690 ret = -EIO;
Josef Bacika970b0a2009-06-27 21:07:34 -04005691 goto out_free;
Chris Mason546888d2009-04-21 11:53:38 -04005692 }
5693
5694 /* the extent lock is ordered inside the running
5695 * transaction
5696 */
Chris Masone980b502009-04-24 14:39:24 -04005697 lock_extent(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
5698 GFP_NOFS);
Yan Zhengd899e052008-10-30 14:25:28 -04005699 ordered = btrfs_lookup_first_ordered_extent(inode,
5700 alloc_end - 1);
5701 if (ordered &&
5702 ordered->file_offset + ordered->len > alloc_start &&
5703 ordered->file_offset < alloc_end) {
5704 btrfs_put_ordered_extent(ordered);
5705 unlock_extent(&BTRFS_I(inode)->io_tree,
Chris Masone980b502009-04-24 14:39:24 -04005706 alloc_start, locked_end, GFP_NOFS);
Chris Mason546888d2009-04-21 11:53:38 -04005707 btrfs_end_transaction(trans, BTRFS_I(inode)->root);
5708
5709 /*
5710 * we can't wait on the range with the transaction
5711 * running or with the extent lock held
5712 */
Yan Zhengd899e052008-10-30 14:25:28 -04005713 btrfs_wait_ordered_range(inode, alloc_start,
5714 alloc_end - alloc_start);
5715 } else {
5716 if (ordered)
5717 btrfs_put_ordered_extent(ordered);
5718 break;
5719 }
5720 }
5721
5722 cur_offset = alloc_start;
5723 while (1) {
5724 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
5725 alloc_end - cur_offset, 0);
5726 BUG_ON(IS_ERR(em) || !em);
5727 last_byte = min(extent_map_end(em), alloc_end);
5728 last_byte = (last_byte + mask) & ~mask;
5729 if (em->block_start == EXTENT_MAP_HOLE) {
Chris Mason546888d2009-04-21 11:53:38 -04005730 ret = prealloc_file_range(trans, inode, cur_offset,
Chris Masone980b502009-04-24 14:39:24 -04005731 last_byte, locked_end + 1,
5732 alloc_hint, mode);
Yan Zhengd899e052008-10-30 14:25:28 -04005733 if (ret < 0) {
5734 free_extent_map(em);
5735 break;
5736 }
5737 }
5738 if (em->block_start <= EXTENT_MAP_LAST_BYTE)
5739 alloc_hint = em->block_start;
5740 free_extent_map(em);
5741
5742 cur_offset = last_byte;
5743 if (cur_offset >= alloc_end) {
5744 ret = 0;
5745 break;
5746 }
5747 }
Chris Masone980b502009-04-24 14:39:24 -04005748 unlock_extent(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
Yan Zhengd899e052008-10-30 14:25:28 -04005749 GFP_NOFS);
Chris Mason546888d2009-04-21 11:53:38 -04005750
5751 btrfs_end_transaction(trans, BTRFS_I(inode)->root);
Josef Bacika970b0a2009-06-27 21:07:34 -04005752out_free:
5753 btrfs_free_reserved_data_space(root, inode, alloc_end - alloc_start);
Yan Zhengd899e052008-10-30 14:25:28 -04005754out:
5755 mutex_unlock(&inode->i_mutex);
5756 return ret;
5757}
5758
Chris Masone6dcd2d2008-07-17 12:53:50 -04005759static int btrfs_set_page_dirty(struct page *page)
5760{
Chris Masone6dcd2d2008-07-17 12:53:50 -04005761 return __set_page_dirty_nobuffers(page);
5762}
5763
Sven Wegener0ee0fda2008-07-30 16:54:26 -04005764static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05005765{
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005766 if ((BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) && (mask & MAY_WRITE))
Yanfdebe2b2008-01-14 13:26:08 -05005767 return -EACCES;
Josef Bacik33268ea2008-07-24 12:16:36 -04005768 return generic_permission(inode, mask, btrfs_check_acl);
Yanfdebe2b2008-01-14 13:26:08 -05005769}
Chris Mason39279cc2007-06-12 06:35:45 -04005770
5771static struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05005772 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04005773 .lookup = btrfs_lookup,
5774 .create = btrfs_create,
5775 .unlink = btrfs_unlink,
5776 .link = btrfs_link,
5777 .mkdir = btrfs_mkdir,
5778 .rmdir = btrfs_rmdir,
5779 .rename = btrfs_rename,
5780 .symlink = btrfs_symlink,
5781 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04005782 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04005783 .setxattr = btrfs_setxattr,
5784 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05005785 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04005786 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05005787 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04005788};
Chris Mason39279cc2007-06-12 06:35:45 -04005789static struct inode_operations btrfs_dir_ro_inode_operations = {
5790 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05005791 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04005792};
Yan, Zheng76dda932009-09-21 16:00:26 -04005793
Chris Mason39279cc2007-06-12 06:35:45 -04005794static struct file_operations btrfs_dir_file_operations = {
5795 .llseek = generic_file_llseek,
5796 .read = generic_read_dir,
David Woodhousecbdf5a22008-08-06 19:42:33 +01005797 .readdir = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04005798 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04005799#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04005800 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04005801#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04005802 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04005803 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04005804};
5805
Chris Masond1310b22008-01-24 16:13:08 -05005806static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04005807 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05005808 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04005809 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04005810 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005811 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04005812 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Mason1259ab72008-05-12 13:39:03 -04005813 .readpage_io_failed_hook = btrfs_io_failed_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05005814 .set_bit_hook = btrfs_set_bit_hook,
5815 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04005816 .merge_extent_hook = btrfs_merge_extent_hook,
5817 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04005818};
5819
Chris Mason35054392009-01-21 13:11:13 -05005820/*
5821 * btrfs doesn't support the bmap operation because swapfiles
5822 * use bmap to make a mapping of extents in the file. They assume
5823 * these extents won't change over the life of the file and they
5824 * use the bmap result to do IO directly to the drive.
5825 *
5826 * the btrfs bmap call would return logical addresses that aren't
5827 * suitable for IO and they also will change frequently as COW
5828 * operations happen. So, swapfile + btrfs == corruption.
5829 *
5830 * For now we're avoiding this by dropping bmap.
5831 */
Chris Mason39279cc2007-06-12 06:35:45 -04005832static struct address_space_operations btrfs_aops = {
5833 .readpage = btrfs_readpage,
5834 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04005835 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05005836 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04005837 .sync_page = block_sync_page,
Chris Mason16432982008-04-10 10:23:21 -04005838 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04005839 .invalidatepage = btrfs_invalidatepage,
5840 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005841 .set_page_dirty = btrfs_set_page_dirty,
Chris Mason39279cc2007-06-12 06:35:45 -04005842};
5843
5844static struct address_space_operations btrfs_symlink_aops = {
5845 .readpage = btrfs_readpage,
5846 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04005847 .invalidatepage = btrfs_invalidatepage,
5848 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04005849};
5850
5851static struct inode_operations btrfs_file_inode_operations = {
5852 .truncate = btrfs_truncate,
5853 .getattr = btrfs_getattr,
5854 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04005855 .setxattr = btrfs_setxattr,
5856 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05005857 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04005858 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05005859 .permission = btrfs_permission,
Yan Zhengd899e052008-10-30 14:25:28 -04005860 .fallocate = btrfs_fallocate,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05005861 .fiemap = btrfs_fiemap,
Chris Mason39279cc2007-06-12 06:35:45 -04005862};
Josef Bacik618e21d2007-07-11 10:18:17 -04005863static struct inode_operations btrfs_special_inode_operations = {
5864 .getattr = btrfs_getattr,
5865 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05005866 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04005867 .setxattr = btrfs_setxattr,
5868 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04005869 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04005870 .removexattr = btrfs_removexattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04005871};
Chris Mason39279cc2007-06-12 06:35:45 -04005872static struct inode_operations btrfs_symlink_inode_operations = {
5873 .readlink = generic_readlink,
5874 .follow_link = page_follow_link_light,
5875 .put_link = page_put_link,
Yanfdebe2b2008-01-14 13:26:08 -05005876 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05005877 .setxattr = btrfs_setxattr,
5878 .getxattr = btrfs_getxattr,
5879 .listxattr = btrfs_listxattr,
5880 .removexattr = btrfs_removexattr,
Chris Mason39279cc2007-06-12 06:35:45 -04005881};
Yan, Zheng76dda932009-09-21 16:00:26 -04005882
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04005883const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04005884 .d_delete = btrfs_dentry_delete,
5885};