blob: 00aefbdcc2dfabca958a56a34a3b80ae432e7499 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090039#include <linux/slab.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050040#include "compat.h"
Chris Mason39279cc2007-06-12 06:35:45 -040041#include "ctree.h"
42#include "disk-io.h"
43#include "transaction.h"
44#include "btrfs_inode.h"
45#include "ioctl.h"
46#include "print-tree.h"
Chris Mason0b86a832008-03-24 15:01:56 -040047#include "volumes.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040048#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040049#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040050#include "tree-log.h"
Chris Masonc8b97812008-10-29 14:49:59 -040051#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050052#include "locking.h"
Chris Mason39279cc2007-06-12 06:35:45 -040053
54struct btrfs_iget_args {
55 u64 ino;
56 struct btrfs_root *root;
57};
58
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070059static const struct inode_operations btrfs_dir_inode_operations;
60static const struct inode_operations btrfs_symlink_inode_operations;
61static const struct inode_operations btrfs_dir_ro_inode_operations;
62static const struct inode_operations btrfs_special_inode_operations;
63static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070064static const struct address_space_operations btrfs_aops;
65static const struct address_space_operations btrfs_symlink_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070066static const struct file_operations btrfs_dir_file_operations;
Chris Masond1310b22008-01-24 16:13:08 -050067static struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040068
69static struct kmem_cache *btrfs_inode_cachep;
70struct kmem_cache *btrfs_trans_handle_cachep;
71struct kmem_cache *btrfs_transaction_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040072struct kmem_cache *btrfs_path_cachep;
73
74#define S_SHIFT 12
75static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
76 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
77 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
78 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
79 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
80 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
81 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
82 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
83};
84
Josef Bacik7b128762008-07-24 12:17:14 -040085static void btrfs_truncate(struct inode *inode);
Chris Masonc8b97812008-10-29 14:49:59 -040086static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end);
Chris Mason771ed682008-11-06 22:02:51 -050087static noinline int cow_file_range(struct inode *inode,
88 struct page *locked_page,
89 u64 start, u64 end, int *page_started,
90 unsigned long *nr_written, int unlock);
Josef Bacik7b128762008-07-24 12:17:14 -040091
Yan, Zhengf34f57a2009-11-12 09:35:27 +000092static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
93 struct inode *inode, struct inode *dir)
Jim Owens0279b4c2009-02-04 09:29:13 -050094{
95 int err;
96
Yan, Zhengf34f57a2009-11-12 09:35:27 +000097 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -050098 if (!err)
Yan, Zhengf34f57a2009-11-12 09:35:27 +000099 err = btrfs_xattr_security_init(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500100 return err;
101}
102
Chris Masond352ac62008-09-29 15:18:18 -0400103/*
Chris Masonc8b97812008-10-29 14:49:59 -0400104 * this does all the hard work for inserting an inline extent into
105 * the btree. The caller should have done a btrfs_drop_extents so that
106 * no overlapping inline items exist in the btree
107 */
Chris Masond3977122009-01-05 21:25:51 -0500108static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400109 struct btrfs_root *root, struct inode *inode,
110 u64 start, size_t size, size_t compressed_size,
111 struct page **compressed_pages)
112{
113 struct btrfs_key key;
114 struct btrfs_path *path;
115 struct extent_buffer *leaf;
116 struct page *page = NULL;
117 char *kaddr;
118 unsigned long ptr;
119 struct btrfs_file_extent_item *ei;
120 int err = 0;
121 int ret;
122 size_t cur_size = size;
123 size_t datasize;
124 unsigned long offset;
125 int use_compress = 0;
126
127 if (compressed_size && compressed_pages) {
128 use_compress = 1;
129 cur_size = compressed_size;
130 }
131
Chris Masond3977122009-01-05 21:25:51 -0500132 path = btrfs_alloc_path();
133 if (!path)
Chris Masonc8b97812008-10-29 14:49:59 -0400134 return -ENOMEM;
135
Chris Masonb9473432009-03-13 11:00:37 -0400136 path->leave_spinning = 1;
Chris Masonc8b97812008-10-29 14:49:59 -0400137 btrfs_set_trans_block_group(trans, inode);
138
139 key.objectid = inode->i_ino;
140 key.offset = start;
141 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
Chris Masonc8b97812008-10-29 14:49:59 -0400142 datasize = btrfs_file_extent_calc_inline_size(cur_size);
143
144 inode_add_bytes(inode, size);
145 ret = btrfs_insert_empty_item(trans, root, path, &key,
146 datasize);
147 BUG_ON(ret);
148 if (ret) {
149 err = ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400150 goto fail;
151 }
152 leaf = path->nodes[0];
153 ei = btrfs_item_ptr(leaf, path->slots[0],
154 struct btrfs_file_extent_item);
155 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
156 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
157 btrfs_set_file_extent_encryption(leaf, ei, 0);
158 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
159 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
160 ptr = btrfs_file_extent_inline_start(ei);
161
162 if (use_compress) {
163 struct page *cpage;
164 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500165 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400166 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500167 cur_size = min_t(unsigned long, compressed_size,
Chris Masonc8b97812008-10-29 14:49:59 -0400168 PAGE_CACHE_SIZE);
169
Chris Masonb9473432009-03-13 11:00:37 -0400170 kaddr = kmap_atomic(cpage, KM_USER0);
Chris Masonc8b97812008-10-29 14:49:59 -0400171 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Chris Masonb9473432009-03-13 11:00:37 -0400172 kunmap_atomic(kaddr, KM_USER0);
Chris Masonc8b97812008-10-29 14:49:59 -0400173
174 i++;
175 ptr += cur_size;
176 compressed_size -= cur_size;
177 }
178 btrfs_set_file_extent_compression(leaf, ei,
179 BTRFS_COMPRESS_ZLIB);
180 } else {
181 page = find_get_page(inode->i_mapping,
182 start >> PAGE_CACHE_SHIFT);
183 btrfs_set_file_extent_compression(leaf, ei, 0);
184 kaddr = kmap_atomic(page, KM_USER0);
185 offset = start & (PAGE_CACHE_SIZE - 1);
186 write_extent_buffer(leaf, kaddr + offset, ptr, size);
187 kunmap_atomic(kaddr, KM_USER0);
188 page_cache_release(page);
189 }
190 btrfs_mark_buffer_dirty(leaf);
191 btrfs_free_path(path);
192
Yan, Zhengc2167752009-11-12 09:34:21 +0000193 /*
194 * we're an inline extent, so nobody can
195 * extend the file past i_size without locking
196 * a page we already have locked.
197 *
198 * We must do any isize and inode updates
199 * before we unlock the pages. Otherwise we
200 * could end up racing with unlink.
201 */
Chris Masonc8b97812008-10-29 14:49:59 -0400202 BTRFS_I(inode)->disk_i_size = inode->i_size;
203 btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000204
Chris Masonc8b97812008-10-29 14:49:59 -0400205 return 0;
206fail:
207 btrfs_free_path(path);
208 return err;
209}
210
211
212/*
213 * conditionally insert an inline extent into the file. This
214 * does the checks required to make sure the data is small enough
215 * to fit as an inline extent.
216 */
Chris Mason7f366cf2009-03-12 20:12:45 -0400217static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400218 struct btrfs_root *root,
219 struct inode *inode, u64 start, u64 end,
220 size_t compressed_size,
221 struct page **compressed_pages)
222{
223 u64 isize = i_size_read(inode);
224 u64 actual_end = min(end + 1, isize);
225 u64 inline_len = actual_end - start;
226 u64 aligned_end = (end + root->sectorsize - 1) &
227 ~((u64)root->sectorsize - 1);
228 u64 hint_byte;
229 u64 data_len = inline_len;
230 int ret;
231
232 if (compressed_size)
233 data_len = compressed_size;
234
235 if (start > 0 ||
Chris Mason70b99e62008-10-31 12:46:39 -0400236 actual_end >= PAGE_CACHE_SIZE ||
Chris Masonc8b97812008-10-29 14:49:59 -0400237 data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
238 (!compressed_size &&
239 (actual_end & (root->sectorsize - 1)) == 0) ||
240 end + 1 < isize ||
241 data_len > root->fs_info->max_inline) {
242 return 1;
243 }
244
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000245 ret = btrfs_drop_extents(trans, inode, start, aligned_end,
Chris Masona1ed8352009-09-11 12:27:37 -0400246 &hint_byte, 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400247 BUG_ON(ret);
248
249 if (isize > actual_end)
250 inline_len = min_t(u64, isize, actual_end);
251 ret = insert_inline_extent(trans, root, inode, start,
252 inline_len, compressed_size,
253 compressed_pages);
254 BUG_ON(ret);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400255 btrfs_delalloc_release_metadata(inode, end + 1 - start);
Chris Masona1ed8352009-09-11 12:27:37 -0400256 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
Chris Masonc8b97812008-10-29 14:49:59 -0400257 return 0;
258}
259
Chris Mason771ed682008-11-06 22:02:51 -0500260struct async_extent {
261 u64 start;
262 u64 ram_size;
263 u64 compressed_size;
264 struct page **pages;
265 unsigned long nr_pages;
266 struct list_head list;
267};
268
269struct async_cow {
270 struct inode *inode;
271 struct btrfs_root *root;
272 struct page *locked_page;
273 u64 start;
274 u64 end;
275 struct list_head extents;
276 struct btrfs_work work;
277};
278
279static noinline int add_async_extent(struct async_cow *cow,
280 u64 start, u64 ram_size,
281 u64 compressed_size,
282 struct page **pages,
283 unsigned long nr_pages)
284{
285 struct async_extent *async_extent;
286
287 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
288 async_extent->start = start;
289 async_extent->ram_size = ram_size;
290 async_extent->compressed_size = compressed_size;
291 async_extent->pages = pages;
292 async_extent->nr_pages = nr_pages;
293 list_add_tail(&async_extent->list, &cow->extents);
294 return 0;
295}
296
Chris Masonc8b97812008-10-29 14:49:59 -0400297/*
Chris Mason771ed682008-11-06 22:02:51 -0500298 * we create compressed extents in two phases. The first
299 * phase compresses a range of pages that have already been
300 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400301 *
Chris Mason771ed682008-11-06 22:02:51 -0500302 * This is done inside an ordered work queue, and the compression
303 * is spread across many cpus. The actual IO submission is step
304 * two, and the ordered work queue takes care of making sure that
305 * happens in the same order things were put onto the queue by
306 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400307 *
Chris Mason771ed682008-11-06 22:02:51 -0500308 * If this code finds it can't get good compression, it puts an
309 * entry onto the work queue to write the uncompressed bytes. This
310 * makes sure that both compressed inodes and uncompressed inodes
311 * are written in the same order that pdflush sent them down.
Chris Masond352ac62008-09-29 15:18:18 -0400312 */
Chris Mason771ed682008-11-06 22:02:51 -0500313static noinline int compress_file_range(struct inode *inode,
314 struct page *locked_page,
315 u64 start, u64 end,
316 struct async_cow *async_cow,
317 int *num_added)
Chris Masonb888db22007-08-27 16:49:44 -0400318{
319 struct btrfs_root *root = BTRFS_I(inode)->root;
320 struct btrfs_trans_handle *trans;
Chris Masondb945352007-10-15 16:15:53 -0400321 u64 num_bytes;
Chris Masonc8b97812008-10-29 14:49:59 -0400322 u64 orig_start;
323 u64 disk_num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400324 u64 blocksize = root->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400325 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500326 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400327 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400328 struct page **pages = NULL;
329 unsigned long nr_pages;
330 unsigned long nr_pages_ret = 0;
331 unsigned long total_compressed = 0;
332 unsigned long total_in = 0;
333 unsigned long max_compressed = 128 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -0500334 unsigned long max_uncompressed = 128 * 1024;
Chris Masonc8b97812008-10-29 14:49:59 -0400335 int i;
336 int will_compress;
Chris Masonb888db22007-08-27 16:49:44 -0400337
Chris Masonc8b97812008-10-29 14:49:59 -0400338 orig_start = start;
Chris Masonbe20aa92007-12-17 20:14:01 -0500339
Chris Mason42dc7ba2008-12-15 11:44:56 -0500340 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400341again:
342 will_compress = 0;
343 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
344 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
345
Chris Masonf03d9302009-02-04 09:31:06 -0500346 /*
347 * we don't want to send crud past the end of i_size through
348 * compression, that's just a waste of CPU time. So, if the
349 * end of the file is before the start of our current
350 * requested range of bytes, we bail out to the uncompressed
351 * cleanup code that can deal with all of this.
352 *
353 * It isn't really the fastest way to fix things, but this is a
354 * very uncommon corner.
355 */
356 if (actual_end <= start)
357 goto cleanup_and_bail_uncompressed;
358
Chris Masonc8b97812008-10-29 14:49:59 -0400359 total_compressed = actual_end - start;
360
361 /* we want to make sure that amount of ram required to uncompress
362 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500363 * of a compressed extent to 128k. This is a crucial number
364 * because it also controls how easily we can spread reads across
365 * cpus for decompression.
366 *
367 * We also want to make sure the amount of IO required to do
368 * a random read is reasonably small, so we limit the size of
369 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400370 */
371 total_compressed = min(total_compressed, max_uncompressed);
Chris Masondb945352007-10-15 16:15:53 -0400372 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
Chris Masonbe20aa92007-12-17 20:14:01 -0500373 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400374 disk_num_bytes = num_bytes;
375 total_in = 0;
376 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400377
Chris Mason771ed682008-11-06 22:02:51 -0500378 /*
379 * we do compression for mount -o compress and when the
380 * inode has not been flagged as nocompress. This flag can
381 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400382 */
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200383 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) &&
Chris Mason1e701a32010-03-11 09:42:04 -0500384 (btrfs_test_opt(root, COMPRESS) ||
385 (BTRFS_I(inode)->force_compress))) {
Chris Masonc8b97812008-10-29 14:49:59 -0400386 WARN_ON(pages);
Chris Masoncfbc2462008-10-30 13:22:14 -0400387 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
Chris Mason179e29e2007-11-01 11:28:41 -0400388
Chris Masonc8b97812008-10-29 14:49:59 -0400389 ret = btrfs_zlib_compress_pages(inode->i_mapping, start,
390 total_compressed, pages,
391 nr_pages, &nr_pages_ret,
392 &total_in,
393 &total_compressed,
394 max_compressed);
395
396 if (!ret) {
397 unsigned long offset = total_compressed &
398 (PAGE_CACHE_SIZE - 1);
399 struct page *page = pages[nr_pages_ret - 1];
400 char *kaddr;
401
402 /* zero the tail end of the last page, we might be
403 * sending it down to disk
404 */
405 if (offset) {
406 kaddr = kmap_atomic(page, KM_USER0);
407 memset(kaddr + offset, 0,
408 PAGE_CACHE_SIZE - offset);
409 kunmap_atomic(kaddr, KM_USER0);
410 }
411 will_compress = 1;
412 }
413 }
414 if (start == 0) {
Chris Mason771ed682008-11-06 22:02:51 -0500415 trans = btrfs_join_transaction(root, 1);
416 BUG_ON(!trans);
417 btrfs_set_trans_block_group(trans, inode);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400418 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Mason771ed682008-11-06 22:02:51 -0500419
Chris Masonc8b97812008-10-29 14:49:59 -0400420 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500421 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400422 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500423 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400424 */
425 ret = cow_file_range_inline(trans, root, inode,
426 start, end, 0, NULL);
427 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500428 /* try making a compressed inline extent */
Chris Masonc8b97812008-10-29 14:49:59 -0400429 ret = cow_file_range_inline(trans, root, inode,
430 start, end,
431 total_compressed, pages);
432 }
433 if (ret == 0) {
Chris Mason771ed682008-11-06 22:02:51 -0500434 /*
435 * inline extent creation worked, we don't need
436 * to create any more async work items. Unlock
437 * and free up our temp pages.
438 */
Chris Masonc8b97812008-10-29 14:49:59 -0400439 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400440 &BTRFS_I(inode)->io_tree,
441 start, end, NULL,
442 EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
Chris Masona3429ab2009-10-08 12:30:20 -0400443 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400444 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000445
446 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400447 goto free_pages_out;
448 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000449 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400450 }
451
452 if (will_compress) {
453 /*
454 * we aren't doing an inline extent round the compressed size
455 * up to a block size boundary so the allocator does sane
456 * things
457 */
458 total_compressed = (total_compressed + blocksize - 1) &
459 ~(blocksize - 1);
460
461 /*
462 * one last check to make sure the compression is really a
463 * win, compare the page count read with the blocks on disk
464 */
465 total_in = (total_in + PAGE_CACHE_SIZE - 1) &
466 ~(PAGE_CACHE_SIZE - 1);
467 if (total_compressed >= total_in) {
468 will_compress = 0;
469 } else {
470 disk_num_bytes = total_compressed;
471 num_bytes = total_in;
472 }
473 }
474 if (!will_compress && pages) {
475 /*
476 * the compression code ran but failed to make things smaller,
477 * free any pages it allocated and our page pointer array
478 */
479 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400480 WARN_ON(pages[i]->mapping);
Chris Masonc8b97812008-10-29 14:49:59 -0400481 page_cache_release(pages[i]);
482 }
483 kfree(pages);
484 pages = NULL;
485 total_compressed = 0;
486 nr_pages_ret = 0;
487
488 /* flag the file so we don't compress in the future */
Chris Mason1e701a32010-03-11 09:42:04 -0500489 if (!btrfs_test_opt(root, FORCE_COMPRESS) &&
490 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500491 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500492 }
Chris Masonc8b97812008-10-29 14:49:59 -0400493 }
Chris Mason771ed682008-11-06 22:02:51 -0500494 if (will_compress) {
495 *num_added += 1;
496
497 /* the async work queues will take care of doing actual
498 * allocation on disk for these compressed pages,
499 * and will submit them to the elevator.
500 */
501 add_async_extent(async_cow, start, num_bytes,
502 total_compressed, pages, nr_pages_ret);
503
Chris Mason42dc7ba2008-12-15 11:44:56 -0500504 if (start + num_bytes < end && start + num_bytes < actual_end) {
Chris Mason771ed682008-11-06 22:02:51 -0500505 start += num_bytes;
506 pages = NULL;
507 cond_resched();
508 goto again;
509 }
510 } else {
Chris Masonf03d9302009-02-04 09:31:06 -0500511cleanup_and_bail_uncompressed:
Chris Mason771ed682008-11-06 22:02:51 -0500512 /*
513 * No compression, but we still need to write the pages in
514 * the file we've been given so far. redirty the locked
515 * page if it corresponds to our extent and set things up
516 * for the async work queue to run cow_file_range to do
517 * the normal delalloc dance
518 */
519 if (page_offset(locked_page) >= start &&
520 page_offset(locked_page) <= end) {
521 __set_page_dirty_nobuffers(locked_page);
522 /* unlocked later on in the async handlers */
523 }
524 add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0);
525 *num_added += 1;
526 }
527
528out:
529 return 0;
530
531free_pages_out:
532 for (i = 0; i < nr_pages_ret; i++) {
533 WARN_ON(pages[i]->mapping);
534 page_cache_release(pages[i]);
535 }
Chris Masond3977122009-01-05 21:25:51 -0500536 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500537
538 goto out;
539}
540
541/*
542 * phase two of compressed writeback. This is the ordered portion
543 * of the code, which only gets called in the order the work was
544 * queued. We walk all the async extents created by compress_file_range
545 * and send them down to the disk.
546 */
547static noinline int submit_compressed_extents(struct inode *inode,
548 struct async_cow *async_cow)
549{
550 struct async_extent *async_extent;
551 u64 alloc_hint = 0;
552 struct btrfs_trans_handle *trans;
553 struct btrfs_key ins;
554 struct extent_map *em;
555 struct btrfs_root *root = BTRFS_I(inode)->root;
556 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
557 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500558 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500559
560 if (list_empty(&async_cow->extents))
561 return 0;
562
Chris Mason771ed682008-11-06 22:02:51 -0500563
Chris Masond3977122009-01-05 21:25:51 -0500564 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500565 async_extent = list_entry(async_cow->extents.next,
566 struct async_extent, list);
567 list_del(&async_extent->list);
568
569 io_tree = &BTRFS_I(inode)->io_tree;
570
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500571retry:
Chris Mason771ed682008-11-06 22:02:51 -0500572 /* did the compression code fall back to uncompressed IO? */
573 if (!async_extent->pages) {
574 int page_started = 0;
575 unsigned long nr_written = 0;
576
577 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000578 async_extent->start +
579 async_extent->ram_size - 1, GFP_NOFS);
Chris Mason771ed682008-11-06 22:02:51 -0500580
581 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500582 ret = cow_file_range(inode, async_cow->locked_page,
583 async_extent->start,
584 async_extent->start +
585 async_extent->ram_size - 1,
586 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500587
588 /*
589 * if page_started, cow_file_range inserted an
590 * inline extent and took care of all the unlocking
591 * and IO for us. Otherwise, we need to submit
592 * all those pages down to the drive.
593 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500594 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500595 extent_write_locked_range(io_tree,
596 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500597 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500598 async_extent->ram_size - 1,
599 btrfs_get_extent,
600 WB_SYNC_ALL);
601 kfree(async_extent);
602 cond_resched();
603 continue;
604 }
605
606 lock_extent(io_tree, async_extent->start,
607 async_extent->start + async_extent->ram_size - 1,
608 GFP_NOFS);
Chris Mason771ed682008-11-06 22:02:51 -0500609
Yan, Zhengc2167752009-11-12 09:34:21 +0000610 trans = btrfs_join_transaction(root, 1);
Chris Mason771ed682008-11-06 22:02:51 -0500611 ret = btrfs_reserve_extent(trans, root,
612 async_extent->compressed_size,
613 async_extent->compressed_size,
614 0, alloc_hint,
615 (u64)-1, &ins, 1);
Yan, Zhengc2167752009-11-12 09:34:21 +0000616 btrfs_end_transaction(trans, root);
617
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500618 if (ret) {
619 int i;
620 for (i = 0; i < async_extent->nr_pages; i++) {
621 WARN_ON(async_extent->pages[i]->mapping);
622 page_cache_release(async_extent->pages[i]);
623 }
624 kfree(async_extent->pages);
625 async_extent->nr_pages = 0;
626 async_extent->pages = NULL;
627 unlock_extent(io_tree, async_extent->start,
628 async_extent->start +
629 async_extent->ram_size - 1, GFP_NOFS);
630 goto retry;
631 }
632
Yan, Zhengc2167752009-11-12 09:34:21 +0000633 /*
634 * here we're doing allocation and writeback of the
635 * compressed pages
636 */
637 btrfs_drop_extent_cache(inode, async_extent->start,
638 async_extent->start +
639 async_extent->ram_size - 1, 0);
640
Chris Mason771ed682008-11-06 22:02:51 -0500641 em = alloc_extent_map(GFP_NOFS);
642 em->start = async_extent->start;
643 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500644 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500645
646 em->block_start = ins.objectid;
647 em->block_len = ins.offset;
648 em->bdev = root->fs_info->fs_devices->latest_bdev;
649 set_bit(EXTENT_FLAG_PINNED, &em->flags);
650 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
651
Chris Masond3977122009-01-05 21:25:51 -0500652 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400653 write_lock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500654 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -0400655 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500656 if (ret != -EEXIST) {
657 free_extent_map(em);
658 break;
659 }
660 btrfs_drop_extent_cache(inode, async_extent->start,
661 async_extent->start +
662 async_extent->ram_size - 1, 0);
663 }
664
665 ret = btrfs_add_ordered_extent(inode, async_extent->start,
666 ins.objectid,
667 async_extent->ram_size,
668 ins.offset,
669 BTRFS_ORDERED_COMPRESSED);
670 BUG_ON(ret);
671
Chris Mason771ed682008-11-06 22:02:51 -0500672 /*
673 * clear dirty, set writeback and unlock the pages.
674 */
675 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400676 &BTRFS_I(inode)->io_tree,
677 async_extent->start,
678 async_extent->start +
679 async_extent->ram_size - 1,
680 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
681 EXTENT_CLEAR_UNLOCK |
Chris Masona3429ab2009-10-08 12:30:20 -0400682 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400683 EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500684
685 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500686 async_extent->start,
687 async_extent->ram_size,
688 ins.objectid,
689 ins.offset, async_extent->pages,
690 async_extent->nr_pages);
Chris Mason771ed682008-11-06 22:02:51 -0500691
692 BUG_ON(ret);
Chris Mason771ed682008-11-06 22:02:51 -0500693 alloc_hint = ins.objectid + ins.offset;
694 kfree(async_extent);
695 cond_resched();
696 }
697
Chris Mason771ed682008-11-06 22:02:51 -0500698 return 0;
699}
700
Josef Bacik4b46fce2010-05-23 11:00:55 -0400701static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
702 u64 num_bytes)
703{
704 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
705 struct extent_map *em;
706 u64 alloc_hint = 0;
707
708 read_lock(&em_tree->lock);
709 em = search_extent_mapping(em_tree, start, num_bytes);
710 if (em) {
711 /*
712 * if block start isn't an actual block number then find the
713 * first block in this inode and use that as a hint. If that
714 * block is also bogus then just don't worry about it.
715 */
716 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
717 free_extent_map(em);
718 em = search_extent_mapping(em_tree, 0, 0);
719 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
720 alloc_hint = em->block_start;
721 if (em)
722 free_extent_map(em);
723 } else {
724 alloc_hint = em->block_start;
725 free_extent_map(em);
726 }
727 }
728 read_unlock(&em_tree->lock);
729
730 return alloc_hint;
731}
732
Chris Mason771ed682008-11-06 22:02:51 -0500733/*
734 * when extent_io.c finds a delayed allocation range in the file,
735 * the call backs end up in this code. The basic idea is to
736 * allocate extents on disk for the range, and create ordered data structs
737 * in ram to track those extents.
738 *
739 * locked_page is the page that writepage had locked already. We use
740 * it to make sure we don't do extra locks or unlocks.
741 *
742 * *page_started is set to one if we unlock locked_page and do everything
743 * required to start IO on it. It may be clean and already done with
744 * IO when we return.
745 */
746static noinline int cow_file_range(struct inode *inode,
747 struct page *locked_page,
748 u64 start, u64 end, int *page_started,
749 unsigned long *nr_written,
750 int unlock)
751{
752 struct btrfs_root *root = BTRFS_I(inode)->root;
753 struct btrfs_trans_handle *trans;
754 u64 alloc_hint = 0;
755 u64 num_bytes;
756 unsigned long ram_size;
757 u64 disk_num_bytes;
758 u64 cur_alloc_size;
759 u64 blocksize = root->sectorsize;
760 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500761 u64 isize = i_size_read(inode);
Chris Mason771ed682008-11-06 22:02:51 -0500762 struct btrfs_key ins;
763 struct extent_map *em;
764 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
765 int ret = 0;
766
767 trans = btrfs_join_transaction(root, 1);
768 BUG_ON(!trans);
769 btrfs_set_trans_block_group(trans, inode);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400770 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Mason771ed682008-11-06 22:02:51 -0500771
Chris Mason42dc7ba2008-12-15 11:44:56 -0500772 actual_end = min_t(u64, isize, end + 1);
Chris Mason771ed682008-11-06 22:02:51 -0500773
774 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
775 num_bytes = max(blocksize, num_bytes);
776 disk_num_bytes = num_bytes;
777 ret = 0;
778
779 if (start == 0) {
780 /* lets try to make an inline extent */
781 ret = cow_file_range_inline(trans, root, inode,
782 start, end, 0, NULL);
783 if (ret == 0) {
784 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400785 &BTRFS_I(inode)->io_tree,
786 start, end, NULL,
787 EXTENT_CLEAR_UNLOCK_PAGE |
788 EXTENT_CLEAR_UNLOCK |
789 EXTENT_CLEAR_DELALLOC |
790 EXTENT_CLEAR_DIRTY |
791 EXTENT_SET_WRITEBACK |
792 EXTENT_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000793
Chris Mason771ed682008-11-06 22:02:51 -0500794 *nr_written = *nr_written +
795 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
796 *page_started = 1;
797 ret = 0;
798 goto out;
799 }
800 }
Chris Masonc8b97812008-10-29 14:49:59 -0400801
802 BUG_ON(disk_num_bytes >
803 btrfs_super_total_bytes(&root->fs_info->super_copy));
804
Josef Bacik4b46fce2010-05-23 11:00:55 -0400805 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400806 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400807
Chris Masond3977122009-01-05 21:25:51 -0500808 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400809 unsigned long op;
810
Josef Bacik287a0ab2010-03-19 18:07:23 +0000811 cur_alloc_size = disk_num_bytes;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400812 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500813 root->sectorsize, 0, alloc_hint,
Chris Masone6dcd2d2008-07-17 12:53:50 -0400814 (u64)-1, &ins, 1);
Chris Masond3977122009-01-05 21:25:51 -0500815 BUG_ON(ret);
816
Chris Masone6dcd2d2008-07-17 12:53:50 -0400817 em = alloc_extent_map(GFP_NOFS);
818 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500819 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500820 ram_size = ins.offset;
821 em->len = ins.offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400822
Chris Masone6dcd2d2008-07-17 12:53:50 -0400823 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400824 em->block_len = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400825 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400826 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -0400827
Chris Masond3977122009-01-05 21:25:51 -0500828 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400829 write_lock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400830 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -0400831 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400832 if (ret != -EEXIST) {
833 free_extent_map(em);
834 break;
835 }
836 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400837 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400838 }
839
Chris Mason98d20f62008-04-14 09:46:10 -0400840 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400841 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -0500842 ram_size, cur_alloc_size, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400843 BUG_ON(ret);
Chris Masonc8b97812008-10-29 14:49:59 -0400844
Yan Zheng17d217f2008-12-12 10:03:38 -0500845 if (root->root_key.objectid ==
846 BTRFS_DATA_RELOC_TREE_OBJECTID) {
847 ret = btrfs_reloc_clone_csums(inode, start,
848 cur_alloc_size);
849 BUG_ON(ret);
850 }
851
Chris Masond3977122009-01-05 21:25:51 -0500852 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -0400853 break;
Chris Masond3977122009-01-05 21:25:51 -0500854
Chris Masonc8b97812008-10-29 14:49:59 -0400855 /* we're not doing compressed IO, don't unlock the first
856 * page (which the caller expects to stay locked), don't
857 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -0400858 *
859 * Do set the Private2 bit so we know this page was properly
860 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -0400861 */
Chris Masona791e352009-10-08 11:27:10 -0400862 op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0;
863 op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
864 EXTENT_SET_PRIVATE2;
865
Chris Masonc8b97812008-10-29 14:49:59 -0400866 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
867 start, start + ram_size - 1,
Chris Masona791e352009-10-08 11:27:10 -0400868 locked_page, op);
Chris Masonc8b97812008-10-29 14:49:59 -0400869 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -0500870 num_bytes -= cur_alloc_size;
871 alloc_hint = ins.objectid + ins.offset;
872 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400873 }
Chris Masonb888db22007-08-27 16:49:44 -0400874out:
Chris Mason771ed682008-11-06 22:02:51 -0500875 ret = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400876 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400877
Chris Masonbe20aa92007-12-17 20:14:01 -0500878 return ret;
Chris Mason771ed682008-11-06 22:02:51 -0500879}
Chris Masonc8b97812008-10-29 14:49:59 -0400880
Chris Mason771ed682008-11-06 22:02:51 -0500881/*
882 * work queue call back to started compression on a file and pages
883 */
884static noinline void async_cow_start(struct btrfs_work *work)
885{
886 struct async_cow *async_cow;
887 int num_added = 0;
888 async_cow = container_of(work, struct async_cow, work);
889
890 compress_file_range(async_cow->inode, async_cow->locked_page,
891 async_cow->start, async_cow->end, async_cow,
892 &num_added);
893 if (num_added == 0)
894 async_cow->inode = NULL;
895}
896
897/*
898 * work queue call back to submit previously compressed pages
899 */
900static noinline void async_cow_submit(struct btrfs_work *work)
901{
902 struct async_cow *async_cow;
903 struct btrfs_root *root;
904 unsigned long nr_pages;
905
906 async_cow = container_of(work, struct async_cow, work);
907
908 root = async_cow->root;
909 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
910 PAGE_CACHE_SHIFT;
911
912 atomic_sub(nr_pages, &root->fs_info->async_delalloc_pages);
913
914 if (atomic_read(&root->fs_info->async_delalloc_pages) <
915 5 * 1042 * 1024 &&
916 waitqueue_active(&root->fs_info->async_submit_wait))
917 wake_up(&root->fs_info->async_submit_wait);
918
Chris Masond3977122009-01-05 21:25:51 -0500919 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -0500920 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -0500921}
Chris Masonc8b97812008-10-29 14:49:59 -0400922
Chris Mason771ed682008-11-06 22:02:51 -0500923static noinline void async_cow_free(struct btrfs_work *work)
924{
925 struct async_cow *async_cow;
926 async_cow = container_of(work, struct async_cow, work);
927 kfree(async_cow);
928}
929
930static int cow_file_range_async(struct inode *inode, struct page *locked_page,
931 u64 start, u64 end, int *page_started,
932 unsigned long *nr_written)
933{
934 struct async_cow *async_cow;
935 struct btrfs_root *root = BTRFS_I(inode)->root;
936 unsigned long nr_pages;
937 u64 cur_end;
938 int limit = 10 * 1024 * 1042;
939
Chris Masona3429ab2009-10-08 12:30:20 -0400940 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
941 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -0500942 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500943 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
944 async_cow->inode = inode;
945 async_cow->root = root;
946 async_cow->locked_page = locked_page;
947 async_cow->start = start;
948
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200949 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
Chris Mason771ed682008-11-06 22:02:51 -0500950 cur_end = end;
951 else
952 cur_end = min(end, start + 512 * 1024 - 1);
953
954 async_cow->end = cur_end;
955 INIT_LIST_HEAD(&async_cow->extents);
956
957 async_cow->work.func = async_cow_start;
958 async_cow->work.ordered_func = async_cow_submit;
959 async_cow->work.ordered_free = async_cow_free;
960 async_cow->work.flags = 0;
961
Chris Mason771ed682008-11-06 22:02:51 -0500962 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
963 PAGE_CACHE_SHIFT;
964 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
965
966 btrfs_queue_worker(&root->fs_info->delalloc_workers,
967 &async_cow->work);
968
969 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
970 wait_event(root->fs_info->async_submit_wait,
971 (atomic_read(&root->fs_info->async_delalloc_pages) <
972 limit));
973 }
974
Chris Masond3977122009-01-05 21:25:51 -0500975 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -0500976 atomic_read(&root->fs_info->async_delalloc_pages)) {
977 wait_event(root->fs_info->async_submit_wait,
978 (atomic_read(&root->fs_info->async_delalloc_pages) ==
979 0));
980 }
981
982 *nr_written += nr_pages;
983 start = cur_end + 1;
984 }
985 *page_started = 1;
986 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -0500987}
988
Chris Masond3977122009-01-05 21:25:51 -0500989static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -0500990 u64 bytenr, u64 num_bytes)
991{
992 int ret;
993 struct btrfs_ordered_sum *sums;
994 LIST_HEAD(list);
995
Yan Zheng07d400a2009-01-06 11:42:00 -0500996 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
997 bytenr + num_bytes - 1, &list);
Yan Zheng17d217f2008-12-12 10:03:38 -0500998 if (ret == 0 && list_empty(&list))
999 return 0;
1000
1001 while (!list_empty(&list)) {
1002 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1003 list_del(&sums->list);
1004 kfree(sums);
1005 }
1006 return 1;
1007}
1008
Chris Masond352ac62008-09-29 15:18:18 -04001009/*
1010 * when nowcow writeback call back. This checks for snapshots or COW copies
1011 * of the extents that exist in the file, and COWs the file as required.
1012 *
1013 * If no cow copies or snapshots exist, we write directly to the existing
1014 * blocks on disk
1015 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001016static noinline int run_delalloc_nocow(struct inode *inode,
1017 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001018 u64 start, u64 end, int *page_started, int force,
1019 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001020{
Chris Masonbe20aa92007-12-17 20:14:01 -05001021 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001022 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001023 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001024 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001025 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001026 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001027 u64 cow_start;
1028 u64 cur_offset;
1029 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001030 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001031 u64 disk_bytenr;
1032 u64 num_bytes;
1033 int extent_type;
1034 int ret;
Yan Zhengd899e052008-10-30 14:25:28 -04001035 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001036 int nocow;
1037 int check_prev = 1;
Chris Masonbe20aa92007-12-17 20:14:01 -05001038
1039 path = btrfs_alloc_path();
1040 BUG_ON(!path);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001041 trans = btrfs_join_transaction(root, 1);
1042 BUG_ON(!trans);
Chris Masonbe20aa92007-12-17 20:14:01 -05001043
Yan Zheng80ff3852008-10-30 14:20:02 -04001044 cow_start = (u64)-1;
1045 cur_offset = start;
1046 while (1) {
1047 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
1048 cur_offset, 0);
1049 BUG_ON(ret < 0);
1050 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1051 leaf = path->nodes[0];
1052 btrfs_item_key_to_cpu(leaf, &found_key,
1053 path->slots[0] - 1);
1054 if (found_key.objectid == inode->i_ino &&
1055 found_key.type == BTRFS_EXTENT_DATA_KEY)
1056 path->slots[0]--;
1057 }
1058 check_prev = 0;
1059next_slot:
1060 leaf = path->nodes[0];
1061 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1062 ret = btrfs_next_leaf(root, path);
1063 if (ret < 0)
1064 BUG_ON(1);
1065 if (ret > 0)
1066 break;
1067 leaf = path->nodes[0];
1068 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001069
Yan Zheng80ff3852008-10-30 14:20:02 -04001070 nocow = 0;
1071 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001072 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001073 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001074
Yan Zheng80ff3852008-10-30 14:20:02 -04001075 if (found_key.objectid > inode->i_ino ||
1076 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1077 found_key.offset > end)
1078 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001079
Yan Zheng80ff3852008-10-30 14:20:02 -04001080 if (found_key.offset > cur_offset) {
1081 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001082 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001083 goto out_check;
1084 }
Chris Masonc31f8832008-01-08 15:46:31 -05001085
Yan Zheng80ff3852008-10-30 14:20:02 -04001086 fi = btrfs_item_ptr(leaf, path->slots[0],
1087 struct btrfs_file_extent_item);
1088 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001089
Yan Zhengd899e052008-10-30 14:25:28 -04001090 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1091 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001092 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001093 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001094 extent_end = found_key.offset +
1095 btrfs_file_extent_num_bytes(leaf, fi);
1096 if (extent_end <= start) {
1097 path->slots[0]++;
1098 goto next_slot;
1099 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001100 if (disk_bytenr == 0)
1101 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001102 if (btrfs_file_extent_compression(leaf, fi) ||
1103 btrfs_file_extent_encryption(leaf, fi) ||
1104 btrfs_file_extent_other_encoding(leaf, fi))
1105 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001106 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1107 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001108 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001109 goto out_check;
Yan Zheng17d217f2008-12-12 10:03:38 -05001110 if (btrfs_cross_ref_exist(trans, root, inode->i_ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001111 found_key.offset -
1112 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001113 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001114 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001115 disk_bytenr += cur_offset - found_key.offset;
1116 num_bytes = min(end + 1, extent_end) - cur_offset;
1117 /*
1118 * force cow if csum exists in the range.
1119 * this ensure that csum for a given extent are
1120 * either valid or do not exist.
1121 */
1122 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1123 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001124 nocow = 1;
1125 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1126 extent_end = found_key.offset +
1127 btrfs_file_extent_inline_len(leaf, fi);
1128 extent_end = ALIGN(extent_end, root->sectorsize);
1129 } else {
1130 BUG_ON(1);
1131 }
1132out_check:
1133 if (extent_end <= start) {
1134 path->slots[0]++;
1135 goto next_slot;
1136 }
1137 if (!nocow) {
1138 if (cow_start == (u64)-1)
1139 cow_start = cur_offset;
1140 cur_offset = extent_end;
1141 if (cur_offset > end)
1142 break;
1143 path->slots[0]++;
1144 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001145 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001146
Yan Zheng7ea394f2008-08-05 13:05:02 -04001147 btrfs_release_path(root, path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001148 if (cow_start != (u64)-1) {
1149 ret = cow_file_range(inode, locked_page, cow_start,
Chris Mason771ed682008-11-06 22:02:51 -05001150 found_key.offset - 1, page_started,
1151 nr_written, 1);
Yan Zheng80ff3852008-10-30 14:20:02 -04001152 BUG_ON(ret);
1153 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001154 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001155
Yan Zhengd899e052008-10-30 14:25:28 -04001156 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1157 struct extent_map *em;
1158 struct extent_map_tree *em_tree;
1159 em_tree = &BTRFS_I(inode)->extent_tree;
1160 em = alloc_extent_map(GFP_NOFS);
1161 em->start = cur_offset;
Chris Mason445a6942008-11-10 11:53:33 -05001162 em->orig_start = em->start;
Yan Zhengd899e052008-10-30 14:25:28 -04001163 em->len = num_bytes;
1164 em->block_len = num_bytes;
1165 em->block_start = disk_bytenr;
1166 em->bdev = root->fs_info->fs_devices->latest_bdev;
1167 set_bit(EXTENT_FLAG_PINNED, &em->flags);
1168 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001169 write_lock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001170 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04001171 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001172 if (ret != -EEXIST) {
1173 free_extent_map(em);
1174 break;
1175 }
1176 btrfs_drop_extent_cache(inode, em->start,
1177 em->start + em->len - 1, 0);
1178 }
1179 type = BTRFS_ORDERED_PREALLOC;
1180 } else {
1181 type = BTRFS_ORDERED_NOCOW;
1182 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001183
1184 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001185 num_bytes, num_bytes, type);
1186 BUG_ON(ret);
Chris Mason771ed682008-11-06 22:02:51 -05001187
Yan, Zhengefa56462010-05-16 10:49:59 -04001188 if (root->root_key.objectid ==
1189 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1190 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1191 num_bytes);
1192 BUG_ON(ret);
1193 }
1194
Yan Zhengd899e052008-10-30 14:25:28 -04001195 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
Chris Masona791e352009-10-08 11:27:10 -04001196 cur_offset, cur_offset + num_bytes - 1,
1197 locked_page, EXTENT_CLEAR_UNLOCK_PAGE |
1198 EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
1199 EXTENT_SET_PRIVATE2);
Yan Zheng80ff3852008-10-30 14:20:02 -04001200 cur_offset = extent_end;
1201 if (cur_offset > end)
1202 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001203 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001204 btrfs_release_path(root, path);
1205
1206 if (cur_offset <= end && cow_start == (u64)-1)
1207 cow_start = cur_offset;
1208 if (cow_start != (u64)-1) {
1209 ret = cow_file_range(inode, locked_page, cow_start, end,
Chris Mason771ed682008-11-06 22:02:51 -05001210 page_started, nr_written, 1);
Yan Zheng80ff3852008-10-30 14:20:02 -04001211 BUG_ON(ret);
1212 }
1213
1214 ret = btrfs_end_transaction(trans, root);
1215 BUG_ON(ret);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001216 btrfs_free_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001217 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001218}
1219
Chris Masond352ac62008-09-29 15:18:18 -04001220/*
1221 * extent_io.c call back to do delayed allocation processing
1222 */
Chris Masonc8b97812008-10-29 14:49:59 -04001223static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001224 u64 start, u64 end, int *page_started,
1225 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001226{
Chris Masonbe20aa92007-12-17 20:14:01 -05001227 int ret;
Chris Mason7f366cf2009-03-12 20:12:45 -04001228 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona2135012008-06-25 16:01:30 -04001229
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001230 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)
Chris Masonc8b97812008-10-29 14:49:59 -04001231 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001232 page_started, 1, nr_written);
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001233 else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC)
Yan Zhengd899e052008-10-30 14:25:28 -04001234 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001235 page_started, 0, nr_written);
Chris Mason1e701a32010-03-11 09:42:04 -05001236 else if (!btrfs_test_opt(root, COMPRESS) &&
1237 !(BTRFS_I(inode)->force_compress))
Chris Mason7f366cf2009-03-12 20:12:45 -04001238 ret = cow_file_range(inode, locked_page, start, end,
1239 page_started, nr_written, 1);
Chris Masonbe20aa92007-12-17 20:14:01 -05001240 else
Chris Mason771ed682008-11-06 22:02:51 -05001241 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001242 page_started, nr_written);
Chris Masonb888db22007-08-27 16:49:44 -04001243 return ret;
1244}
1245
Josef Bacik9ed74f22009-09-11 16:12:44 -04001246static int btrfs_split_extent_hook(struct inode *inode,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001247 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001248{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001249 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001250 if (!(orig->state & EXTENT_DELALLOC))
1251 return 0;
1252
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001253 atomic_inc(&BTRFS_I(inode)->outstanding_extents);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001254 return 0;
1255}
1256
1257/*
1258 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1259 * extents so we can keep track of new extents that are just merged onto old
1260 * extents, such as when we are doing sequential writes, so we can properly
1261 * account for the metadata space we'll need.
1262 */
1263static int btrfs_merge_extent_hook(struct inode *inode,
1264 struct extent_state *new,
1265 struct extent_state *other)
1266{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001267 /* not delalloc, ignore it */
1268 if (!(other->state & EXTENT_DELALLOC))
1269 return 0;
1270
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001271 atomic_dec(&BTRFS_I(inode)->outstanding_extents);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001272 return 0;
1273}
1274
Chris Masond352ac62008-09-29 15:18:18 -04001275/*
1276 * extent_io.c set_bit_hook, used to track delayed allocation
1277 * bytes in this file, and to maintain the list of inodes that
1278 * have pending delalloc work to be done.
1279 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001280static int btrfs_set_bit_hook(struct inode *inode,
1281 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001282{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001283
Chris Mason75eff682008-12-15 15:54:40 -05001284 /*
1285 * set_bit and clear bit hooks normally require _irqsave/restore
1286 * but in this case, we are only testeing for the DELALLOC
1287 * bit, which is only set or cleared with irqs on
1288 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001289 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001290 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001291 u64 len = state->end + 1 - state->start;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001292
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001293 if (*bits & EXTENT_FIRST_DELALLOC)
1294 *bits &= ~EXTENT_FIRST_DELALLOC;
1295 else
1296 atomic_inc(&BTRFS_I(inode)->outstanding_extents);
Josef Bacik287a0ab2010-03-19 18:07:23 +00001297
Chris Mason75eff682008-12-15 15:54:40 -05001298 spin_lock(&root->fs_info->delalloc_lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001299 BTRFS_I(inode)->delalloc_bytes += len;
1300 root->fs_info->delalloc_bytes += len;
Chris Masonea8c2812008-08-04 23:17:27 -04001301 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1302 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1303 &root->fs_info->delalloc_inodes);
1304 }
Chris Mason75eff682008-12-15 15:54:40 -05001305 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001306 }
1307 return 0;
1308}
1309
Chris Masond352ac62008-09-29 15:18:18 -04001310/*
1311 * extent_io.c clear_bit_hook, see set_bit_hook for why
1312 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001313static int btrfs_clear_bit_hook(struct inode *inode,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001314 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001315{
Chris Mason75eff682008-12-15 15:54:40 -05001316 /*
1317 * set_bit and clear bit hooks normally require _irqsave/restore
1318 * but in this case, we are only testeing for the DELALLOC
1319 * bit, which is only set or cleared with irqs on
1320 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001321 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001322 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001323 u64 len = state->end + 1 - state->start;
Chris Masonbcbfce82008-04-22 13:26:47 -04001324
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001325 if (*bits & EXTENT_FIRST_DELALLOC)
1326 *bits &= ~EXTENT_FIRST_DELALLOC;
1327 else if (!(*bits & EXTENT_DO_ACCOUNTING))
1328 atomic_dec(&BTRFS_I(inode)->outstanding_extents);
1329
1330 if (*bits & EXTENT_DO_ACCOUNTING)
1331 btrfs_delalloc_release_metadata(inode, len);
1332
1333 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID)
1334 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001335
Chris Mason75eff682008-12-15 15:54:40 -05001336 spin_lock(&root->fs_info->delalloc_lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001337 root->fs_info->delalloc_bytes -= len;
1338 BTRFS_I(inode)->delalloc_bytes -= len;
1339
Chris Masonea8c2812008-08-04 23:17:27 -04001340 if (BTRFS_I(inode)->delalloc_bytes == 0 &&
1341 !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1342 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1343 }
Chris Mason75eff682008-12-15 15:54:40 -05001344 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001345 }
1346 return 0;
1347}
1348
Chris Masond352ac62008-09-29 15:18:18 -04001349/*
1350 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1351 * we don't create bios that span stripes or chunks
1352 */
Chris Mason239b14b2008-03-24 15:02:07 -04001353int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001354 size_t size, struct bio *bio,
1355 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001356{
1357 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
1358 struct btrfs_mapping_tree *map_tree;
Chris Masona62b9402008-10-03 16:31:08 -04001359 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001360 u64 length = 0;
1361 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001362 int ret;
1363
Chris Mason771ed682008-11-06 22:02:51 -05001364 if (bio_flags & EXTENT_BIO_COMPRESSED)
1365 return 0;
1366
Chris Masonf2d8d742008-04-21 10:03:05 -04001367 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001368 map_tree = &root->fs_info->mapping_tree;
1369 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04001370 ret = btrfs_map_block(map_tree, READ, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001371 &map_length, NULL, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04001372
Chris Masond3977122009-01-05 21:25:51 -05001373 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001374 return 1;
Chris Mason239b14b2008-03-24 15:02:07 -04001375 return 0;
1376}
1377
Chris Masond352ac62008-09-29 15:18:18 -04001378/*
1379 * in order to insert checksums into the metadata in large chunks,
1380 * we wait until bio submission time. All the pages in the bio are
1381 * checksummed and sums are attached onto the ordered extent record.
1382 *
1383 * At IO completion time the cums attached on the ordered extent record
1384 * are inserted into the btree
1385 */
Chris Masond3977122009-01-05 21:25:51 -05001386static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1387 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001388 unsigned long bio_flags,
1389 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001390{
Chris Mason065631f2008-02-20 12:07:25 -05001391 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001392 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001393
Chris Masond20f7042008-12-08 16:58:54 -05001394 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Chris Mason44b8bd72008-04-16 11:14:51 -04001395 BUG_ON(ret);
Chris Mason4a69a412008-11-06 22:03:00 -05001396 return 0;
1397}
Chris Masone0156402008-04-16 11:15:20 -04001398
Chris Mason4a69a412008-11-06 22:03:00 -05001399/*
1400 * in order to insert checksums into the metadata in large chunks,
1401 * we wait until bio submission time. All the pages in the bio are
1402 * checksummed and sums are attached onto the ordered extent record.
1403 *
1404 * At IO completion time the cums attached on the ordered extent record
1405 * are inserted into the btree
1406 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001407static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001408 int mirror_num, unsigned long bio_flags,
1409 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001410{
1411 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason8b712842008-06-11 16:50:36 -04001412 return btrfs_map_bio(root, rw, bio, mirror_num, 1);
Chris Mason44b8bd72008-04-16 11:14:51 -04001413}
1414
Chris Masond352ac62008-09-29 15:18:18 -04001415/*
Chris Masoncad321a2008-12-17 14:51:42 -05001416 * extent_io.c submission hook. This does the right thing for csum calculation
1417 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001418 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001419static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001420 int mirror_num, unsigned long bio_flags,
1421 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001422{
1423 struct btrfs_root *root = BTRFS_I(inode)->root;
1424 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001425 int skip_sum;
Chris Mason44b8bd72008-04-16 11:14:51 -04001426
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001427 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001428
Chris Masone6dcd2d2008-07-17 12:53:50 -04001429 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
1430 BUG_ON(ret);
Chris Mason065631f2008-02-20 12:07:25 -05001431
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001432 if (!(rw & (1 << BIO_RW))) {
Chris Masond20f7042008-12-08 16:58:54 -05001433 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Chris Masonc8b97812008-10-29 14:49:59 -04001434 return btrfs_submit_compressed_read(inode, bio,
1435 mirror_num, bio_flags);
Chris Masond20f7042008-12-08 16:58:54 -05001436 } else if (!skip_sum)
1437 btrfs_lookup_bio_sums(root, inode, bio, NULL);
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001438 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001439 } else if (!skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001440 /* csum items have already been cloned */
1441 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1442 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001443 /* we're doing a write, do the async checksumming */
1444 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001445 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001446 bio_flags, bio_offset,
1447 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001448 __btrfs_submit_bio_done);
Chris Mason19b9bdb2008-10-30 14:23:13 -04001449 }
1450
Chris Mason0b86a832008-03-24 15:01:56 -04001451mapit:
Chris Mason8b712842008-06-11 16:50:36 -04001452 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
Chris Mason065631f2008-02-20 12:07:25 -05001453}
Chris Mason6885f302008-02-20 16:11:05 -05001454
Chris Masond352ac62008-09-29 15:18:18 -04001455/*
1456 * given a list of ordered sums record them in the inode. This happens
1457 * at IO completion time based on sums calculated at bio submission time.
1458 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001459static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001460 struct inode *inode, u64 file_offset,
1461 struct list_head *list)
1462{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001463 struct btrfs_ordered_sum *sum;
1464
1465 btrfs_set_trans_block_group(trans, inode);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001466
1467 list_for_each_entry(sum, list, list) {
Chris Masond20f7042008-12-08 16:58:54 -05001468 btrfs_csum_file_blocks(trans,
1469 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001470 }
1471 return 0;
1472}
1473
Josef Bacik2ac55d42010-02-03 19:33:23 +00001474int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1475 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001476{
Chris Masond3977122009-01-05 21:25:51 -05001477 if ((end & (PAGE_CACHE_SIZE - 1)) == 0)
Chris Mason771ed682008-11-06 22:02:51 -05001478 WARN_ON(1);
Chris Masonea8c2812008-08-04 23:17:27 -04001479 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001480 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001481}
1482
Chris Masond352ac62008-09-29 15:18:18 -04001483/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001484struct btrfs_writepage_fixup {
1485 struct page *page;
1486 struct btrfs_work work;
1487};
1488
Christoph Hellwigb2950862008-12-02 09:54:17 -05001489static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001490{
1491 struct btrfs_writepage_fixup *fixup;
1492 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001493 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001494 struct page *page;
1495 struct inode *inode;
1496 u64 page_start;
1497 u64 page_end;
1498
1499 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1500 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001501again:
Chris Mason247e7432008-07-17 12:53:51 -04001502 lock_page(page);
1503 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1504 ClearPageChecked(page);
1505 goto out_page;
1506 }
1507
1508 inode = page->mapping->host;
1509 page_start = page_offset(page);
1510 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1511
Josef Bacik2ac55d42010-02-03 19:33:23 +00001512 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
1513 &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001514
1515 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001516 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001517 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001518
1519 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1520 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001521 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1522 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001523 unlock_page(page);
1524 btrfs_start_ordered_extent(inode, ordered, 1);
1525 goto again;
1526 }
Chris Mason247e7432008-07-17 12:53:51 -04001527
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001528 BUG();
Josef Bacik2ac55d42010-02-03 19:33:23 +00001529 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001530 ClearPageChecked(page);
1531out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001532 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1533 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001534out_page:
1535 unlock_page(page);
1536 page_cache_release(page);
1537}
1538
1539/*
1540 * There are a few paths in the higher layers of the kernel that directly
1541 * set the page dirty bit without asking the filesystem if it is a
1542 * good idea. This causes problems because we want to make sure COW
1543 * properly happens and the data=ordered rules are followed.
1544 *
Chris Masonc8b97812008-10-29 14:49:59 -04001545 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001546 * hasn't been properly setup for IO. We kick off an async process
1547 * to fix it up. The async helper will wait for ordered extents, set
1548 * the delalloc bit and make it safe to write the page.
1549 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001550static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001551{
1552 struct inode *inode = page->mapping->host;
1553 struct btrfs_writepage_fixup *fixup;
1554 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001555
Chris Mason8b62b722009-09-02 16:53:46 -04001556 /* this page is properly in the ordered list */
1557 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001558 return 0;
1559
1560 if (PageChecked(page))
1561 return -EAGAIN;
1562
1563 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1564 if (!fixup)
1565 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001566
Chris Mason247e7432008-07-17 12:53:51 -04001567 SetPageChecked(page);
1568 page_cache_get(page);
1569 fixup->work.func = btrfs_writepage_fixup_worker;
1570 fixup->page = page;
1571 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
1572 return -EAGAIN;
1573}
1574
Yan Zhengd899e052008-10-30 14:25:28 -04001575static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1576 struct inode *inode, u64 file_pos,
1577 u64 disk_bytenr, u64 disk_num_bytes,
1578 u64 num_bytes, u64 ram_bytes,
1579 u8 compression, u8 encryption,
1580 u16 other_encoding, int extent_type)
1581{
1582 struct btrfs_root *root = BTRFS_I(inode)->root;
1583 struct btrfs_file_extent_item *fi;
1584 struct btrfs_path *path;
1585 struct extent_buffer *leaf;
1586 struct btrfs_key ins;
1587 u64 hint;
1588 int ret;
1589
1590 path = btrfs_alloc_path();
1591 BUG_ON(!path);
1592
Chris Masonb9473432009-03-13 11:00:37 -04001593 path->leave_spinning = 1;
Chris Masona1ed8352009-09-11 12:27:37 -04001594
1595 /*
1596 * we may be replacing one extent in the tree with another.
1597 * The new extent is pinned in the extent map, and we don't want
1598 * to drop it from the cache until it is completely in the btree.
1599 *
1600 * So, tell btrfs_drop_extents to leave this extent in the cache.
1601 * the caller is expected to unpin it and allow it to be merged
1602 * with the others.
1603 */
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001604 ret = btrfs_drop_extents(trans, inode, file_pos, file_pos + num_bytes,
1605 &hint, 0);
Yan Zhengd899e052008-10-30 14:25:28 -04001606 BUG_ON(ret);
1607
1608 ins.objectid = inode->i_ino;
1609 ins.offset = file_pos;
1610 ins.type = BTRFS_EXTENT_DATA_KEY;
1611 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
1612 BUG_ON(ret);
1613 leaf = path->nodes[0];
1614 fi = btrfs_item_ptr(leaf, path->slots[0],
1615 struct btrfs_file_extent_item);
1616 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1617 btrfs_set_file_extent_type(leaf, fi, extent_type);
1618 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1619 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1620 btrfs_set_file_extent_offset(leaf, fi, 0);
1621 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1622 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1623 btrfs_set_file_extent_compression(leaf, fi, compression);
1624 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1625 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04001626
1627 btrfs_unlock_up_safe(path, 1);
1628 btrfs_set_lock_blocking(leaf);
1629
Yan Zhengd899e052008-10-30 14:25:28 -04001630 btrfs_mark_buffer_dirty(leaf);
1631
1632 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04001633
1634 ins.objectid = disk_bytenr;
1635 ins.offset = disk_num_bytes;
1636 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001637 ret = btrfs_alloc_reserved_file_extent(trans, root,
1638 root->root_key.objectid,
1639 inode->i_ino, file_pos, &ins);
Yan Zhengd899e052008-10-30 14:25:28 -04001640 BUG_ON(ret);
Yan Zhengd899e052008-10-30 14:25:28 -04001641 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04001642
Yan Zhengd899e052008-10-30 14:25:28 -04001643 return 0;
1644}
1645
Chris Mason5d13a982009-03-13 11:41:46 -04001646/*
1647 * helper function for btrfs_finish_ordered_io, this
1648 * just reads in some of the csum leaves to prime them into ram
1649 * before we start the transaction. It limits the amount of btree
1650 * reads required while inside the transaction.
1651 */
Chris Masond352ac62008-09-29 15:18:18 -04001652/* as ordered data IO finishes, this gets called so we can finish
1653 * an ordered extent if the range of bytes in the file it covers are
1654 * fully written.
1655 */
Chris Mason211f90e2008-07-18 11:56:15 -04001656static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001657{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001658 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001659 struct btrfs_trans_handle *trans = NULL;
Chris Mason5d13a982009-03-13 11:41:46 -04001660 struct btrfs_ordered_extent *ordered_extent = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001661 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001662 struct extent_state *cached_state = NULL;
Yan Zhengd899e052008-10-30 14:25:28 -04001663 int compressed = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001664 int ret;
1665
Josef Bacik5a1a3df2010-02-02 20:51:14 +00001666 ret = btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
1667 end - start + 1);
Chris Masonba1da2f2008-07-17 12:54:15 -04001668 if (!ret)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001669 return 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001670 BUG_ON(!ordered_extent);
Josef Bacikefd049f2010-02-02 20:50:10 +00001671
Yan, Zhengc2167752009-11-12 09:34:21 +00001672 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
1673 BUG_ON(!list_empty(&ordered_extent->list));
1674 ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1675 if (!ret) {
1676 trans = btrfs_join_transaction(root, 1);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001677 btrfs_set_trans_block_group(trans, inode);
1678 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00001679 ret = btrfs_update_inode(trans, root, inode);
1680 BUG_ON(ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00001681 }
1682 goto out;
1683 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04001684
Josef Bacik2ac55d42010-02-03 19:33:23 +00001685 lock_extent_bits(io_tree, ordered_extent->file_offset,
1686 ordered_extent->file_offset + ordered_extent->len - 1,
1687 0, &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001688
Yan, Zhengc2167752009-11-12 09:34:21 +00001689 trans = btrfs_join_transaction(root, 1);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001690 btrfs_set_trans_block_group(trans, inode);
1691 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00001692
Chris Masonc8b97812008-10-29 14:49:59 -04001693 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Yan Zhengd899e052008-10-30 14:25:28 -04001694 compressed = 1;
1695 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
1696 BUG_ON(compressed);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001697 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04001698 ordered_extent->file_offset,
1699 ordered_extent->file_offset +
1700 ordered_extent->len);
1701 BUG_ON(ret);
1702 } else {
1703 ret = insert_reserved_file_extent(trans, inode,
1704 ordered_extent->file_offset,
1705 ordered_extent->start,
1706 ordered_extent->disk_len,
1707 ordered_extent->len,
1708 ordered_extent->len,
1709 compressed, 0, 0,
1710 BTRFS_FILE_EXTENT_REG);
Chris Masona1ed8352009-09-11 12:27:37 -04001711 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
1712 ordered_extent->file_offset,
1713 ordered_extent->len);
Yan Zhengd899e052008-10-30 14:25:28 -04001714 BUG_ON(ret);
1715 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00001716 unlock_extent_cached(io_tree, ordered_extent->file_offset,
1717 ordered_extent->file_offset +
1718 ordered_extent->len - 1, &cached_state, GFP_NOFS);
1719
Chris Masone6dcd2d2008-07-17 12:53:50 -04001720 add_pending_csums(trans, inode, ordered_extent->file_offset,
1721 &ordered_extent->list);
1722
Yan, Zhengc2167752009-11-12 09:34:21 +00001723 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1724 ret = btrfs_update_inode(trans, root, inode);
1725 BUG_ON(ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00001726out:
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001727 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
1728 if (trans)
1729 btrfs_end_transaction(trans, root);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001730 /* once for us */
1731 btrfs_put_ordered_extent(ordered_extent);
1732 /* once for the tree */
1733 btrfs_put_ordered_extent(ordered_extent);
1734
Chris Masone6dcd2d2008-07-17 12:53:50 -04001735 return 0;
1736}
1737
Christoph Hellwigb2950862008-12-02 09:54:17 -05001738static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04001739 struct extent_state *state, int uptodate)
1740{
Chris Mason8b62b722009-09-02 16:53:46 -04001741 ClearPagePrivate2(page);
Chris Mason211f90e2008-07-18 11:56:15 -04001742 return btrfs_finish_ordered_io(page->mapping->host, start, end);
1743}
1744
Chris Masond352ac62008-09-29 15:18:18 -04001745/*
1746 * When IO fails, either with EIO or csum verification fails, we
1747 * try other mirrors that might have a good copy of the data. This
1748 * io_failure_record is used to record state as we go through all the
1749 * mirrors. If another mirror has good data, the page is set up to date
1750 * and things continue. If a good mirror can't be found, the original
1751 * bio end_io callback is called to indicate things have failed.
1752 */
Chris Mason7e383262008-04-09 16:28:12 -04001753struct io_failure_record {
1754 struct page *page;
1755 u64 start;
1756 u64 len;
1757 u64 logical;
Chris Masond20f7042008-12-08 16:58:54 -05001758 unsigned long bio_flags;
Chris Mason7e383262008-04-09 16:28:12 -04001759 int last_mirror;
1760};
1761
Christoph Hellwigb2950862008-12-02 09:54:17 -05001762static int btrfs_io_failed_hook(struct bio *failed_bio,
Chris Mason1259ab72008-05-12 13:39:03 -04001763 struct page *page, u64 start, u64 end,
1764 struct extent_state *state)
Chris Mason7e383262008-04-09 16:28:12 -04001765{
1766 struct io_failure_record *failrec = NULL;
1767 u64 private;
1768 struct extent_map *em;
1769 struct inode *inode = page->mapping->host;
1770 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Chris Mason3b951512008-04-17 11:29:12 -04001771 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason7e383262008-04-09 16:28:12 -04001772 struct bio *bio;
1773 int num_copies;
1774 int ret;
Chris Mason1259ab72008-05-12 13:39:03 -04001775 int rw;
Chris Mason7e383262008-04-09 16:28:12 -04001776 u64 logical;
1777
1778 ret = get_state_private(failure_tree, start, &private);
1779 if (ret) {
Chris Mason7e383262008-04-09 16:28:12 -04001780 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
1781 if (!failrec)
1782 return -ENOMEM;
1783 failrec->start = start;
1784 failrec->len = end - start + 1;
1785 failrec->last_mirror = 0;
Chris Masond20f7042008-12-08 16:58:54 -05001786 failrec->bio_flags = 0;
Chris Mason7e383262008-04-09 16:28:12 -04001787
Chris Mason890871b2009-09-02 16:24:52 -04001788 read_lock(&em_tree->lock);
Chris Mason3b951512008-04-17 11:29:12 -04001789 em = lookup_extent_mapping(em_tree, start, failrec->len);
1790 if (em->start > start || em->start + em->len < start) {
1791 free_extent_map(em);
1792 em = NULL;
1793 }
Chris Mason890871b2009-09-02 16:24:52 -04001794 read_unlock(&em_tree->lock);
Chris Mason7e383262008-04-09 16:28:12 -04001795
1796 if (!em || IS_ERR(em)) {
1797 kfree(failrec);
1798 return -EIO;
1799 }
1800 logical = start - em->start;
1801 logical = em->block_start + logical;
Chris Masond20f7042008-12-08 16:58:54 -05001802 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
1803 logical = em->block_start;
1804 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
1805 }
Chris Mason7e383262008-04-09 16:28:12 -04001806 failrec->logical = logical;
1807 free_extent_map(em);
1808 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
1809 EXTENT_DIRTY, GFP_NOFS);
Chris Mason587f7702008-04-11 12:16:46 -04001810 set_state_private(failure_tree, start,
1811 (u64)(unsigned long)failrec);
Chris Mason7e383262008-04-09 16:28:12 -04001812 } else {
Chris Mason587f7702008-04-11 12:16:46 -04001813 failrec = (struct io_failure_record *)(unsigned long)private;
Chris Mason7e383262008-04-09 16:28:12 -04001814 }
1815 num_copies = btrfs_num_copies(
1816 &BTRFS_I(inode)->root->fs_info->mapping_tree,
1817 failrec->logical, failrec->len);
1818 failrec->last_mirror++;
1819 if (!state) {
Chris Masoncad321a2008-12-17 14:51:42 -05001820 spin_lock(&BTRFS_I(inode)->io_tree.lock);
Chris Mason7e383262008-04-09 16:28:12 -04001821 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
1822 failrec->start,
1823 EXTENT_LOCKED);
1824 if (state && state->start != failrec->start)
1825 state = NULL;
Chris Masoncad321a2008-12-17 14:51:42 -05001826 spin_unlock(&BTRFS_I(inode)->io_tree.lock);
Chris Mason7e383262008-04-09 16:28:12 -04001827 }
1828 if (!state || failrec->last_mirror > num_copies) {
1829 set_state_private(failure_tree, failrec->start, 0);
1830 clear_extent_bits(failure_tree, failrec->start,
1831 failrec->start + failrec->len - 1,
1832 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
1833 kfree(failrec);
1834 return -EIO;
1835 }
1836 bio = bio_alloc(GFP_NOFS, 1);
1837 bio->bi_private = state;
1838 bio->bi_end_io = failed_bio->bi_end_io;
1839 bio->bi_sector = failrec->logical >> 9;
1840 bio->bi_bdev = failed_bio->bi_bdev;
Chris Masone1c4b742008-04-22 13:26:46 -04001841 bio->bi_size = 0;
Chris Masond20f7042008-12-08 16:58:54 -05001842
Chris Mason7e383262008-04-09 16:28:12 -04001843 bio_add_page(bio, page, failrec->len, start - page_offset(page));
Chris Mason1259ab72008-05-12 13:39:03 -04001844 if (failed_bio->bi_rw & (1 << BIO_RW))
1845 rw = WRITE;
1846 else
1847 rw = READ;
1848
1849 BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
Chris Masonc8b97812008-10-29 14:49:59 -04001850 failrec->last_mirror,
Chris Masoneaf25d92010-05-25 09:48:28 -04001851 failrec->bio_flags, 0);
Chris Mason1259ab72008-05-12 13:39:03 -04001852 return 0;
1853}
1854
Chris Masond352ac62008-09-29 15:18:18 -04001855/*
1856 * each time an IO finishes, we do a fast check in the IO failure tree
1857 * to see if we need to process or clean up an io_failure_record
1858 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001859static int btrfs_clean_io_failures(struct inode *inode, u64 start)
Chris Mason1259ab72008-05-12 13:39:03 -04001860{
1861 u64 private;
1862 u64 private_failure;
1863 struct io_failure_record *failure;
1864 int ret;
1865
1866 private = 0;
1867 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
1868 (u64)-1, 1, EXTENT_DIRTY)) {
1869 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
1870 start, &private_failure);
1871 if (ret == 0) {
1872 failure = (struct io_failure_record *)(unsigned long)
1873 private_failure;
1874 set_state_private(&BTRFS_I(inode)->io_failure_tree,
1875 failure->start, 0);
1876 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
1877 failure->start,
1878 failure->start + failure->len - 1,
1879 EXTENT_DIRTY | EXTENT_LOCKED,
1880 GFP_NOFS);
1881 kfree(failure);
1882 }
1883 }
Chris Mason7e383262008-04-09 16:28:12 -04001884 return 0;
1885}
1886
Chris Masond352ac62008-09-29 15:18:18 -04001887/*
1888 * when reads are done, we need to check csums to verify the data is correct
1889 * if there's a match, we allow the bio to finish. If not, we go through
1890 * the io_failure_record routines to find good copies
1891 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001892static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason70dec802008-01-29 09:59:12 -05001893 struct extent_state *state)
Chris Mason07157aa2007-08-30 08:50:51 -04001894{
Chris Mason35ebb932007-10-30 16:56:53 -04001895 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -04001896 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05001897 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04001898 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -05001899 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -04001900 int ret;
Chris Masonff79f812007-10-15 16:22:25 -04001901 struct btrfs_root *root = BTRFS_I(inode)->root;
1902 u32 csum = ~(u32)0;
Chris Masond1310b22008-01-24 16:13:08 -05001903
Chris Masond20f7042008-12-08 16:58:54 -05001904 if (PageChecked(page)) {
1905 ClearPageChecked(page);
1906 goto good;
1907 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001908
1909 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Chris Masonb6cda9b2007-12-14 15:30:32 -05001910 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05001911
Yan Zheng17d217f2008-12-12 10:03:38 -05001912 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04001913 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001914 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
1915 GFP_NOFS);
1916 return 0;
1917 }
1918
Yanc2e639f2008-02-04 08:57:25 -05001919 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -05001920 private = state->private;
1921 ret = 0;
1922 } else {
1923 ret = get_state_private(io_tree, start, &private);
1924 }
Chris Mason9ab86c82009-01-07 09:48:51 -05001925 kaddr = kmap_atomic(page, KM_USER0);
Chris Masond3977122009-01-05 21:25:51 -05001926 if (ret)
Chris Mason07157aa2007-08-30 08:50:51 -04001927 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05001928
Chris Masonff79f812007-10-15 16:22:25 -04001929 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
1930 btrfs_csum_final(csum, (char *)&csum);
Chris Masond3977122009-01-05 21:25:51 -05001931 if (csum != private)
Chris Mason07157aa2007-08-30 08:50:51 -04001932 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05001933
Chris Mason9ab86c82009-01-07 09:48:51 -05001934 kunmap_atomic(kaddr, KM_USER0);
Chris Masond20f7042008-12-08 16:58:54 -05001935good:
Chris Mason7e383262008-04-09 16:28:12 -04001936 /* if the io failure tree for this inode is non-empty,
1937 * check to see if we've recovered from a failed IO
1938 */
Chris Mason1259ab72008-05-12 13:39:03 -04001939 btrfs_clean_io_failures(inode, start);
Chris Mason07157aa2007-08-30 08:50:51 -04001940 return 0;
1941
1942zeroit:
Chris Mason193f2842009-04-27 07:29:05 -04001943 if (printk_ratelimit()) {
1944 printk(KERN_INFO "btrfs csum failed ino %lu off %llu csum %u "
1945 "private %llu\n", page->mapping->host->i_ino,
1946 (unsigned long long)start, csum,
1947 (unsigned long long)private);
1948 }
Chris Masondb945352007-10-15 16:15:53 -04001949 memset(kaddr + offset, 1, end - start + 1);
1950 flush_dcache_page(page);
Chris Mason9ab86c82009-01-07 09:48:51 -05001951 kunmap_atomic(kaddr, KM_USER0);
Chris Mason3b951512008-04-17 11:29:12 -04001952 if (private == 0)
1953 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04001954 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04001955}
Chris Masonb888db22007-08-27 16:49:44 -04001956
Yan, Zheng24bbcf02009-11-12 09:36:34 +00001957struct delayed_iput {
1958 struct list_head list;
1959 struct inode *inode;
1960};
1961
1962void btrfs_add_delayed_iput(struct inode *inode)
1963{
1964 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
1965 struct delayed_iput *delayed;
1966
1967 if (atomic_add_unless(&inode->i_count, -1, 1))
1968 return;
1969
1970 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
1971 delayed->inode = inode;
1972
1973 spin_lock(&fs_info->delayed_iput_lock);
1974 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
1975 spin_unlock(&fs_info->delayed_iput_lock);
1976}
1977
1978void btrfs_run_delayed_iputs(struct btrfs_root *root)
1979{
1980 LIST_HEAD(list);
1981 struct btrfs_fs_info *fs_info = root->fs_info;
1982 struct delayed_iput *delayed;
1983 int empty;
1984
1985 spin_lock(&fs_info->delayed_iput_lock);
1986 empty = list_empty(&fs_info->delayed_iputs);
1987 spin_unlock(&fs_info->delayed_iput_lock);
1988 if (empty)
1989 return;
1990
1991 down_read(&root->fs_info->cleanup_work_sem);
1992 spin_lock(&fs_info->delayed_iput_lock);
1993 list_splice_init(&fs_info->delayed_iputs, &list);
1994 spin_unlock(&fs_info->delayed_iput_lock);
1995
1996 while (!list_empty(&list)) {
1997 delayed = list_entry(list.next, struct delayed_iput, list);
1998 list_del(&delayed->list);
1999 iput(delayed->inode);
2000 kfree(delayed);
2001 }
2002 up_read(&root->fs_info->cleanup_work_sem);
2003}
2004
Josef Bacik7b128762008-07-24 12:17:14 -04002005/*
Yan, Zhengd68fc572010-05-16 10:49:58 -04002006 * calculate extra metadata reservation when snapshotting a subvolume
2007 * contains orphan files.
2008 */
2009void btrfs_orphan_pre_snapshot(struct btrfs_trans_handle *trans,
2010 struct btrfs_pending_snapshot *pending,
2011 u64 *bytes_to_reserve)
2012{
2013 struct btrfs_root *root;
2014 struct btrfs_block_rsv *block_rsv;
2015 u64 num_bytes;
2016 int index;
2017
2018 root = pending->root;
2019 if (!root->orphan_block_rsv || list_empty(&root->orphan_list))
2020 return;
2021
2022 block_rsv = root->orphan_block_rsv;
2023
2024 /* orphan block reservation for the snapshot */
2025 num_bytes = block_rsv->size;
2026
2027 /*
2028 * after the snapshot is created, COWing tree blocks may use more
2029 * space than it frees. So we should make sure there is enough
2030 * reserved space.
2031 */
2032 index = trans->transid & 0x1;
2033 if (block_rsv->reserved + block_rsv->freed[index] < block_rsv->size) {
2034 num_bytes += block_rsv->size -
2035 (block_rsv->reserved + block_rsv->freed[index]);
2036 }
2037
2038 *bytes_to_reserve += num_bytes;
2039}
2040
2041void btrfs_orphan_post_snapshot(struct btrfs_trans_handle *trans,
2042 struct btrfs_pending_snapshot *pending)
2043{
2044 struct btrfs_root *root = pending->root;
2045 struct btrfs_root *snap = pending->snap;
2046 struct btrfs_block_rsv *block_rsv;
2047 u64 num_bytes;
2048 int index;
2049 int ret;
2050
2051 if (!root->orphan_block_rsv || list_empty(&root->orphan_list))
2052 return;
2053
2054 /* refill source subvolume's orphan block reservation */
2055 block_rsv = root->orphan_block_rsv;
2056 index = trans->transid & 0x1;
2057 if (block_rsv->reserved + block_rsv->freed[index] < block_rsv->size) {
2058 num_bytes = block_rsv->size -
2059 (block_rsv->reserved + block_rsv->freed[index]);
2060 ret = btrfs_block_rsv_migrate(&pending->block_rsv,
2061 root->orphan_block_rsv,
2062 num_bytes);
2063 BUG_ON(ret);
2064 }
2065
2066 /* setup orphan block reservation for the snapshot */
2067 block_rsv = btrfs_alloc_block_rsv(snap);
2068 BUG_ON(!block_rsv);
2069
2070 btrfs_add_durable_block_rsv(root->fs_info, block_rsv);
2071 snap->orphan_block_rsv = block_rsv;
2072
2073 num_bytes = root->orphan_block_rsv->size;
2074 ret = btrfs_block_rsv_migrate(&pending->block_rsv,
2075 block_rsv, num_bytes);
2076 BUG_ON(ret);
2077
2078#if 0
2079 /* insert orphan item for the snapshot */
2080 WARN_ON(!root->orphan_item_inserted);
2081 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
2082 snap->root_key.objectid);
2083 BUG_ON(ret);
2084 snap->orphan_item_inserted = 1;
2085#endif
2086}
2087
2088enum btrfs_orphan_cleanup_state {
2089 ORPHAN_CLEANUP_STARTED = 1,
2090 ORPHAN_CLEANUP_DONE = 2,
2091};
2092
2093/*
2094 * This is called in transaction commmit time. If there are no orphan
2095 * files in the subvolume, it removes orphan item and frees block_rsv
2096 * structure.
2097 */
2098void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
2099 struct btrfs_root *root)
2100{
2101 int ret;
2102
2103 if (!list_empty(&root->orphan_list) ||
2104 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
2105 return;
2106
2107 if (root->orphan_item_inserted &&
2108 btrfs_root_refs(&root->root_item) > 0) {
2109 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
2110 root->root_key.objectid);
2111 BUG_ON(ret);
2112 root->orphan_item_inserted = 0;
2113 }
2114
2115 if (root->orphan_block_rsv) {
2116 WARN_ON(root->orphan_block_rsv->size > 0);
2117 btrfs_free_block_rsv(root, root->orphan_block_rsv);
2118 root->orphan_block_rsv = NULL;
2119 }
2120}
2121
2122/*
Josef Bacik7b128762008-07-24 12:17:14 -04002123 * This creates an orphan entry for the given inode in case something goes
2124 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04002125 *
2126 * NOTE: caller of this function should reserve 5 units of metadata for
2127 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04002128 */
2129int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2130{
2131 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002132 struct btrfs_block_rsv *block_rsv = NULL;
2133 int reserve = 0;
2134 int insert = 0;
2135 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002136
Yan, Zhengd68fc572010-05-16 10:49:58 -04002137 if (!root->orphan_block_rsv) {
2138 block_rsv = btrfs_alloc_block_rsv(root);
2139 BUG_ON(!block_rsv);
Josef Bacik7b128762008-07-24 12:17:14 -04002140 }
2141
Yan, Zhengd68fc572010-05-16 10:49:58 -04002142 spin_lock(&root->orphan_lock);
2143 if (!root->orphan_block_rsv) {
2144 root->orphan_block_rsv = block_rsv;
2145 } else if (block_rsv) {
2146 btrfs_free_block_rsv(root, block_rsv);
2147 block_rsv = NULL;
2148 }
Josef Bacik7b128762008-07-24 12:17:14 -04002149
Yan, Zhengd68fc572010-05-16 10:49:58 -04002150 if (list_empty(&BTRFS_I(inode)->i_orphan)) {
2151 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
2152#if 0
2153 /*
2154 * For proper ENOSPC handling, we should do orphan
2155 * cleanup when mounting. But this introduces backward
2156 * compatibility issue.
2157 */
2158 if (!xchg(&root->orphan_item_inserted, 1))
2159 insert = 2;
2160 else
2161 insert = 1;
2162#endif
2163 insert = 1;
2164 } else {
2165 WARN_ON(!BTRFS_I(inode)->orphan_meta_reserved);
2166 }
Josef Bacik7b128762008-07-24 12:17:14 -04002167
Yan, Zhengd68fc572010-05-16 10:49:58 -04002168 if (!BTRFS_I(inode)->orphan_meta_reserved) {
2169 BTRFS_I(inode)->orphan_meta_reserved = 1;
2170 reserve = 1;
2171 }
2172 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002173
Yan, Zhengd68fc572010-05-16 10:49:58 -04002174 if (block_rsv)
2175 btrfs_add_durable_block_rsv(root->fs_info, block_rsv);
2176
2177 /* grab metadata reservation from transaction handle */
2178 if (reserve) {
2179 ret = btrfs_orphan_reserve_metadata(trans, inode);
2180 BUG_ON(ret);
2181 }
2182
2183 /* insert an orphan item to track this unlinked/truncated file */
2184 if (insert >= 1) {
2185 ret = btrfs_insert_orphan_item(trans, root, inode->i_ino);
2186 BUG_ON(ret);
2187 }
2188
2189 /* insert an orphan item to track subvolume contains orphan files */
2190 if (insert >= 2) {
2191 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
2192 root->root_key.objectid);
2193 BUG_ON(ret);
2194 }
2195 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002196}
2197
2198/*
2199 * We have done the truncate/delete so we can go ahead and remove the orphan
2200 * item for this particular inode.
2201 */
2202int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
2203{
2204 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002205 int delete_item = 0;
2206 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002207 int ret = 0;
2208
Yan, Zhengd68fc572010-05-16 10:49:58 -04002209 spin_lock(&root->orphan_lock);
2210 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
2211 list_del_init(&BTRFS_I(inode)->i_orphan);
2212 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04002213 }
2214
Yan, Zhengd68fc572010-05-16 10:49:58 -04002215 if (BTRFS_I(inode)->orphan_meta_reserved) {
2216 BTRFS_I(inode)->orphan_meta_reserved = 0;
2217 release_rsv = 1;
2218 }
2219 spin_unlock(&root->orphan_lock);
2220
2221 if (trans && delete_item) {
2222 ret = btrfs_del_orphan_item(trans, root, inode->i_ino);
2223 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04002224 }
2225
Yan, Zhengd68fc572010-05-16 10:49:58 -04002226 if (release_rsv)
2227 btrfs_orphan_release_metadata(inode);
Josef Bacik7b128762008-07-24 12:17:14 -04002228
Yan, Zhengd68fc572010-05-16 10:49:58 -04002229 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002230}
2231
2232/*
2233 * this cleans up any orphans that may be left on the list from the last use
2234 * of this root.
2235 */
2236void btrfs_orphan_cleanup(struct btrfs_root *root)
2237{
2238 struct btrfs_path *path;
2239 struct extent_buffer *leaf;
2240 struct btrfs_item *item;
2241 struct btrfs_key key, found_key;
2242 struct btrfs_trans_handle *trans;
2243 struct inode *inode;
2244 int ret = 0, nr_unlink = 0, nr_truncate = 0;
2245
Yan, Zhengd68fc572010-05-16 10:49:58 -04002246 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik7b128762008-07-24 12:17:14 -04002247 return;
Yan, Zhengc71bf092009-11-12 09:34:40 +00002248
2249 path = btrfs_alloc_path();
2250 BUG_ON(!path);
Josef Bacik7b128762008-07-24 12:17:14 -04002251 path->reada = -1;
2252
2253 key.objectid = BTRFS_ORPHAN_OBJECTID;
2254 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
2255 key.offset = (u64)-1;
2256
Josef Bacik7b128762008-07-24 12:17:14 -04002257 while (1) {
2258 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2259 if (ret < 0) {
2260 printk(KERN_ERR "Error searching slot for orphan: %d"
2261 "\n", ret);
2262 break;
2263 }
2264
2265 /*
2266 * if ret == 0 means we found what we were searching for, which
2267 * is weird, but possible, so only screw with path if we didnt
2268 * find the key and see if we have stuff that matches
2269 */
2270 if (ret > 0) {
2271 if (path->slots[0] == 0)
2272 break;
2273 path->slots[0]--;
2274 }
2275
2276 /* pull out the item */
2277 leaf = path->nodes[0];
2278 item = btrfs_item_nr(leaf, path->slots[0]);
2279 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2280
2281 /* make sure the item matches what we want */
2282 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
2283 break;
2284 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
2285 break;
2286
2287 /* release the path since we're done with it */
2288 btrfs_release_path(root, path);
2289
2290 /*
2291 * this is where we are basically btrfs_lookup, without the
2292 * crossing root thing. we store the inode number in the
2293 * offset of the orphan item.
2294 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002295 found_key.objectid = found_key.offset;
2296 found_key.type = BTRFS_INODE_ITEM_KEY;
2297 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00002298 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002299 BUG_ON(IS_ERR(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04002300
Josef Bacik7b128762008-07-24 12:17:14 -04002301 /*
2302 * add this inode to the orphan list so btrfs_orphan_del does
2303 * the proper thing when we hit it
2304 */
Yan, Zhengd68fc572010-05-16 10:49:58 -04002305 spin_lock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002306 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002307 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002308
2309 /*
2310 * if this is a bad inode, means we actually succeeded in
2311 * removing the inode, but not the orphan record, which means
2312 * we need to manually delete the orphan since iput will just
2313 * do a destroy_inode
2314 */
2315 if (is_bad_inode(inode)) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04002316 trans = btrfs_start_transaction(root, 0);
Josef Bacik7b128762008-07-24 12:17:14 -04002317 btrfs_orphan_del(trans, inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002318 btrfs_end_transaction(trans, root);
Josef Bacik7b128762008-07-24 12:17:14 -04002319 iput(inode);
2320 continue;
2321 }
2322
2323 /* if we have links, this was a truncate, lets do that */
2324 if (inode->i_nlink) {
2325 nr_truncate++;
2326 btrfs_truncate(inode);
2327 } else {
2328 nr_unlink++;
2329 }
2330
2331 /* this will do delete_inode and everything for us */
2332 iput(inode);
2333 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04002334 btrfs_free_path(path);
2335
2336 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
2337
2338 if (root->orphan_block_rsv)
2339 btrfs_block_rsv_release(root, root->orphan_block_rsv,
2340 (u64)-1);
2341
2342 if (root->orphan_block_rsv || root->orphan_item_inserted) {
2343 trans = btrfs_join_transaction(root, 1);
2344 btrfs_end_transaction(trans, root);
2345 }
Josef Bacik7b128762008-07-24 12:17:14 -04002346
2347 if (nr_unlink)
2348 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
2349 if (nr_truncate)
2350 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
Josef Bacik7b128762008-07-24 12:17:14 -04002351}
2352
Chris Masond352ac62008-09-29 15:18:18 -04002353/*
Chris Mason46a53cc2009-04-27 11:47:50 -04002354 * very simple check to peek ahead in the leaf looking for xattrs. If we
2355 * don't find any xattrs, we know there can't be any acls.
2356 *
2357 * slot is the slot the inode is in, objectid is the objectid of the inode
2358 */
2359static noinline int acls_after_inode_item(struct extent_buffer *leaf,
2360 int slot, u64 objectid)
2361{
2362 u32 nritems = btrfs_header_nritems(leaf);
2363 struct btrfs_key found_key;
2364 int scanned = 0;
2365
2366 slot++;
2367 while (slot < nritems) {
2368 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2369
2370 /* we found a different objectid, there must not be acls */
2371 if (found_key.objectid != objectid)
2372 return 0;
2373
2374 /* we found an xattr, assume we've got an acl */
2375 if (found_key.type == BTRFS_XATTR_ITEM_KEY)
2376 return 1;
2377
2378 /*
2379 * we found a key greater than an xattr key, there can't
2380 * be any acls later on
2381 */
2382 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
2383 return 0;
2384
2385 slot++;
2386 scanned++;
2387
2388 /*
2389 * it goes inode, inode backrefs, xattrs, extents,
2390 * so if there are a ton of hard links to an inode there can
2391 * be a lot of backrefs. Don't waste time searching too hard,
2392 * this is just an optimization
2393 */
2394 if (scanned >= 8)
2395 break;
2396 }
2397 /* we hit the end of the leaf before we found an xattr or
2398 * something larger than an xattr. We have to assume the inode
2399 * has acls
2400 */
2401 return 1;
2402}
2403
2404/*
Chris Masond352ac62008-09-29 15:18:18 -04002405 * read an inode from the btree into the in-memory inode
2406 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002407static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002408{
2409 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002410 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002411 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04002412 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04002413 struct btrfs_root *root = BTRFS_I(inode)->root;
2414 struct btrfs_key location;
Chris Mason46a53cc2009-04-27 11:47:50 -04002415 int maybe_acls;
Chris Mason39279cc2007-06-12 06:35:45 -04002416 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -04002417 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04002418 int ret;
2419
2420 path = btrfs_alloc_path();
2421 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -04002422 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05002423
Chris Mason39279cc2007-06-12 06:35:45 -04002424 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002425 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04002426 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04002427
Chris Mason5f39d392007-10-15 16:14:19 -04002428 leaf = path->nodes[0];
2429 inode_item = btrfs_item_ptr(leaf, path->slots[0],
2430 struct btrfs_inode_item);
2431
2432 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
2433 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
2434 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
2435 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
Chris Masondbe674a2008-07-17 12:54:05 -04002436 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04002437
2438 tspec = btrfs_inode_atime(inode_item);
2439 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2440 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2441
2442 tspec = btrfs_inode_mtime(inode_item);
2443 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2444 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2445
2446 tspec = btrfs_inode_ctime(inode_item);
2447 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2448 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2449
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002450 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04002451 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Chris Masonc3027eb2008-12-08 16:40:21 -05002452 BTRFS_I(inode)->sequence = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04002453 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04002454 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002455 rdev = btrfs_inode_rdev(leaf, inode_item);
2456
Josef Bacikaec74772008-07-24 12:12:38 -04002457 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05002458 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Josef Bacikaec74772008-07-24 12:12:38 -04002459
Chris Mason5f39d392007-10-15 16:14:19 -04002460 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002461
Chris Mason46a53cc2009-04-27 11:47:50 -04002462 /*
2463 * try to precache a NULL acl entry for files that don't have
2464 * any xattrs or acls
2465 */
2466 maybe_acls = acls_after_inode_item(leaf, path->slots[0], inode->i_ino);
Al Viro72c04902009-06-24 16:58:48 -04002467 if (!maybe_acls)
2468 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04002469
Yan Zhengd2fb3432008-12-11 16:30:39 -05002470 BTRFS_I(inode)->block_group = btrfs_find_block_group(root, 0,
2471 alloc_group_block, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002472 btrfs_free_path(path);
2473 inode_item = NULL;
2474
Chris Mason39279cc2007-06-12 06:35:45 -04002475 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04002476 case S_IFREG:
2477 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04002478 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05002479 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002480 inode->i_fop = &btrfs_file_operations;
2481 inode->i_op = &btrfs_file_inode_operations;
2482 break;
2483 case S_IFDIR:
2484 inode->i_fop = &btrfs_dir_file_operations;
2485 if (root == root->fs_info->tree_root)
2486 inode->i_op = &btrfs_dir_ro_inode_operations;
2487 else
2488 inode->i_op = &btrfs_dir_inode_operations;
2489 break;
2490 case S_IFLNK:
2491 inode->i_op = &btrfs_symlink_inode_operations;
2492 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04002493 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04002494 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04002495 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05002496 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04002497 init_special_inode(inode, inode->i_mode, rdev);
2498 break;
Chris Mason39279cc2007-06-12 06:35:45 -04002499 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002500
2501 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002502 return;
2503
2504make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04002505 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002506 make_bad_inode(inode);
2507}
2508
Chris Masond352ac62008-09-29 15:18:18 -04002509/*
2510 * given a leaf and an inode, copy the inode fields into the leaf
2511 */
Chris Masone02119d2008-09-05 16:13:11 -04002512static void fill_inode_item(struct btrfs_trans_handle *trans,
2513 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04002514 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04002515 struct inode *inode)
2516{
Chris Mason5f39d392007-10-15 16:14:19 -04002517 btrfs_set_inode_uid(leaf, item, inode->i_uid);
2518 btrfs_set_inode_gid(leaf, item, inode->i_gid);
Chris Masondbe674a2008-07-17 12:54:05 -04002519 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
Chris Mason5f39d392007-10-15 16:14:19 -04002520 btrfs_set_inode_mode(leaf, item, inode->i_mode);
2521 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
2522
2523 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
2524 inode->i_atime.tv_sec);
2525 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
2526 inode->i_atime.tv_nsec);
2527
2528 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
2529 inode->i_mtime.tv_sec);
2530 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
2531 inode->i_mtime.tv_nsec);
2532
2533 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
2534 inode->i_ctime.tv_sec);
2535 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
2536 inode->i_ctime.tv_nsec);
2537
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002538 btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode));
Chris Masone02119d2008-09-05 16:13:11 -04002539 btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
Chris Masonc3027eb2008-12-08 16:40:21 -05002540 btrfs_set_inode_sequence(leaf, item, BTRFS_I(inode)->sequence);
Chris Masone02119d2008-09-05 16:13:11 -04002541 btrfs_set_inode_transid(leaf, item, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002542 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -05002543 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002544 btrfs_set_inode_block_group(leaf, item, BTRFS_I(inode)->block_group);
Chris Mason39279cc2007-06-12 06:35:45 -04002545}
2546
Chris Masond352ac62008-09-29 15:18:18 -04002547/*
2548 * copy everything in the in-memory inode into the btree.
2549 */
Chris Masond3977122009-01-05 21:25:51 -05002550noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
2551 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002552{
2553 struct btrfs_inode_item *inode_item;
2554 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002555 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002556 int ret;
2557
2558 path = btrfs_alloc_path();
2559 BUG_ON(!path);
Chris Masonb9473432009-03-13 11:00:37 -04002560 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002561 ret = btrfs_lookup_inode(trans, root, path,
2562 &BTRFS_I(inode)->location, 1);
2563 if (ret) {
2564 if (ret > 0)
2565 ret = -ENOENT;
2566 goto failed;
2567 }
2568
Chris Masonb4ce94d2009-02-04 09:25:08 -05002569 btrfs_unlock_up_safe(path, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002570 leaf = path->nodes[0];
2571 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04002572 struct btrfs_inode_item);
2573
Chris Masone02119d2008-09-05 16:13:11 -04002574 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002575 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002576 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002577 ret = 0;
2578failed:
Chris Mason39279cc2007-06-12 06:35:45 -04002579 btrfs_free_path(path);
2580 return ret;
2581}
2582
2583
Chris Masond352ac62008-09-29 15:18:18 -04002584/*
2585 * unlink helper that gets used here in inode.c and in the tree logging
2586 * recovery code. It remove a link in a directory with a given name, and
2587 * also drops the back refs in the inode to the directory
2588 */
Chris Masone02119d2008-09-05 16:13:11 -04002589int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2590 struct btrfs_root *root,
2591 struct inode *dir, struct inode *inode,
2592 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04002593{
2594 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04002595 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002596 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002597 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04002598 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04002599 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04002600
2601 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04002602 if (!path) {
2603 ret = -ENOMEM;
2604 goto err;
2605 }
2606
Chris Masonb9473432009-03-13 11:00:37 -04002607 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002608 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
2609 name, name_len, -1);
2610 if (IS_ERR(di)) {
2611 ret = PTR_ERR(di);
2612 goto err;
2613 }
2614 if (!di) {
2615 ret = -ENOENT;
2616 goto err;
2617 }
Chris Mason5f39d392007-10-15 16:14:19 -04002618 leaf = path->nodes[0];
2619 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04002620 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04002621 if (ret)
2622 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -04002623 btrfs_release_path(root, path);
2624
Josef Bacikaec74772008-07-24 12:12:38 -04002625 ret = btrfs_del_inode_ref(trans, root, name, name_len,
Chris Masone02119d2008-09-05 16:13:11 -04002626 inode->i_ino,
2627 dir->i_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04002628 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05002629 printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
Josef Bacikaec74772008-07-24 12:12:38 -04002630 "inode %lu parent %lu\n", name_len, name,
Chris Masone02119d2008-09-05 16:13:11 -04002631 inode->i_ino, dir->i_ino);
Josef Bacikaec74772008-07-24 12:12:38 -04002632 goto err;
2633 }
2634
Chris Mason39279cc2007-06-12 06:35:45 -04002635 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04002636 index, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -04002637 if (IS_ERR(di)) {
2638 ret = PTR_ERR(di);
2639 goto err;
2640 }
2641 if (!di) {
2642 ret = -ENOENT;
2643 goto err;
2644 }
2645 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason925baed2008-06-25 16:01:30 -04002646 btrfs_release_path(root, path);
Chris Mason39279cc2007-06-12 06:35:45 -04002647
Chris Masone02119d2008-09-05 16:13:11 -04002648 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
2649 inode, dir->i_ino);
Chris Mason49eb7e42008-09-11 15:53:12 -04002650 BUG_ON(ret != 0 && ret != -ENOENT);
Chris Masone02119d2008-09-05 16:13:11 -04002651
2652 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
2653 dir, index);
2654 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04002655err:
2656 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002657 if (ret)
2658 goto out;
2659
2660 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
2661 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2662 btrfs_update_inode(trans, root, dir);
2663 btrfs_drop_nlink(inode);
2664 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04002665out:
Chris Mason39279cc2007-06-12 06:35:45 -04002666 return ret;
2667}
2668
Yan, Zhenga22285a2010-05-16 10:48:46 -04002669/* helper to check if there is any shared block in the path */
2670static int check_path_shared(struct btrfs_root *root,
2671 struct btrfs_path *path)
2672{
2673 struct extent_buffer *eb;
2674 int level;
2675 int ret;
2676 u64 refs;
2677
2678 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
2679 if (!path->nodes[level])
2680 break;
2681 eb = path->nodes[level];
2682 if (!btrfs_block_can_be_shared(root, eb))
2683 continue;
2684 ret = btrfs_lookup_extent_info(NULL, root, eb->start, eb->len,
2685 &refs, NULL);
2686 if (refs > 1)
2687 return 1;
2688 }
2689 return 0;
2690}
2691
2692/*
2693 * helper to start transaction for unlink and rmdir.
2694 *
2695 * unlink and rmdir are special in btrfs, they do not always free space.
2696 * so in enospc case, we should make sure they will free space before
2697 * allowing them to use the global metadata reservation.
2698 */
2699static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
2700 struct dentry *dentry)
2701{
2702 struct btrfs_trans_handle *trans;
2703 struct btrfs_root *root = BTRFS_I(dir)->root;
2704 struct btrfs_path *path;
2705 struct btrfs_inode_ref *ref;
2706 struct btrfs_dir_item *di;
2707 struct inode *inode = dentry->d_inode;
2708 u64 index;
2709 int check_link = 1;
2710 int err = -ENOSPC;
2711 int ret;
2712
2713 trans = btrfs_start_transaction(root, 10);
2714 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
2715 return trans;
2716
2717 if (inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
2718 return ERR_PTR(-ENOSPC);
2719
2720 /* check if there is someone else holds reference */
2721 if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1)
2722 return ERR_PTR(-ENOSPC);
2723
2724 if (atomic_read(&inode->i_count) > 2)
2725 return ERR_PTR(-ENOSPC);
2726
2727 if (xchg(&root->fs_info->enospc_unlink, 1))
2728 return ERR_PTR(-ENOSPC);
2729
2730 path = btrfs_alloc_path();
2731 if (!path) {
2732 root->fs_info->enospc_unlink = 0;
2733 return ERR_PTR(-ENOMEM);
2734 }
2735
2736 trans = btrfs_start_transaction(root, 0);
2737 if (IS_ERR(trans)) {
2738 btrfs_free_path(path);
2739 root->fs_info->enospc_unlink = 0;
2740 return trans;
2741 }
2742
2743 path->skip_locking = 1;
2744 path->search_commit_root = 1;
2745
2746 ret = btrfs_lookup_inode(trans, root, path,
2747 &BTRFS_I(dir)->location, 0);
2748 if (ret < 0) {
2749 err = ret;
2750 goto out;
2751 }
2752 if (ret == 0) {
2753 if (check_path_shared(root, path))
2754 goto out;
2755 } else {
2756 check_link = 0;
2757 }
2758 btrfs_release_path(root, path);
2759
2760 ret = btrfs_lookup_inode(trans, root, path,
2761 &BTRFS_I(inode)->location, 0);
2762 if (ret < 0) {
2763 err = ret;
2764 goto out;
2765 }
2766 if (ret == 0) {
2767 if (check_path_shared(root, path))
2768 goto out;
2769 } else {
2770 check_link = 0;
2771 }
2772 btrfs_release_path(root, path);
2773
2774 if (ret == 0 && S_ISREG(inode->i_mode)) {
2775 ret = btrfs_lookup_file_extent(trans, root, path,
2776 inode->i_ino, (u64)-1, 0);
2777 if (ret < 0) {
2778 err = ret;
2779 goto out;
2780 }
2781 BUG_ON(ret == 0);
2782 if (check_path_shared(root, path))
2783 goto out;
2784 btrfs_release_path(root, path);
2785 }
2786
2787 if (!check_link) {
2788 err = 0;
2789 goto out;
2790 }
2791
2792 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
2793 dentry->d_name.name, dentry->d_name.len, 0);
2794 if (IS_ERR(di)) {
2795 err = PTR_ERR(di);
2796 goto out;
2797 }
2798 if (di) {
2799 if (check_path_shared(root, path))
2800 goto out;
2801 } else {
2802 err = 0;
2803 goto out;
2804 }
2805 btrfs_release_path(root, path);
2806
2807 ref = btrfs_lookup_inode_ref(trans, root, path,
2808 dentry->d_name.name, dentry->d_name.len,
2809 inode->i_ino, dir->i_ino, 0);
2810 if (IS_ERR(ref)) {
2811 err = PTR_ERR(ref);
2812 goto out;
2813 }
2814 BUG_ON(!ref);
2815 if (check_path_shared(root, path))
2816 goto out;
2817 index = btrfs_inode_ref_index(path->nodes[0], ref);
2818 btrfs_release_path(root, path);
2819
2820 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino, index,
2821 dentry->d_name.name, dentry->d_name.len, 0);
2822 if (IS_ERR(di)) {
2823 err = PTR_ERR(di);
2824 goto out;
2825 }
2826 BUG_ON(ret == -ENOENT);
2827 if (check_path_shared(root, path))
2828 goto out;
2829
2830 err = 0;
2831out:
2832 btrfs_free_path(path);
2833 if (err) {
2834 btrfs_end_transaction(trans, root);
2835 root->fs_info->enospc_unlink = 0;
2836 return ERR_PTR(err);
2837 }
2838
2839 trans->block_rsv = &root->fs_info->global_block_rsv;
2840 return trans;
2841}
2842
2843static void __unlink_end_trans(struct btrfs_trans_handle *trans,
2844 struct btrfs_root *root)
2845{
2846 if (trans->block_rsv == &root->fs_info->global_block_rsv) {
2847 BUG_ON(!root->fs_info->enospc_unlink);
2848 root->fs_info->enospc_unlink = 0;
2849 }
2850 btrfs_end_transaction_throttle(trans, root);
2851}
2852
Chris Mason39279cc2007-06-12 06:35:45 -04002853static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
2854{
Yan, Zhenga22285a2010-05-16 10:48:46 -04002855 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04002856 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04002857 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002858 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -05002859 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002860
Yan, Zhenga22285a2010-05-16 10:48:46 -04002861 trans = __unlink_start_trans(dir, dentry);
2862 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05002863 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04002864
Chris Mason39279cc2007-06-12 06:35:45 -04002865 btrfs_set_trans_block_group(trans, dir);
Chris Mason12fcfd22009-03-24 10:24:20 -04002866
2867 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
2868
Chris Masone02119d2008-09-05 16:13:11 -04002869 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
2870 dentry->d_name.name, dentry->d_name.len);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002871 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04002872
Yan, Zhenga22285a2010-05-16 10:48:46 -04002873 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04002874 ret = btrfs_orphan_add(trans, inode);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002875 BUG_ON(ret);
2876 }
Josef Bacik7b128762008-07-24 12:17:14 -04002877
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002878 nr = trans->blocks_used;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002879 __unlink_end_trans(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002880 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002881 return ret;
2882}
2883
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002884int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
2885 struct btrfs_root *root,
2886 struct inode *dir, u64 objectid,
2887 const char *name, int name_len)
2888{
2889 struct btrfs_path *path;
2890 struct extent_buffer *leaf;
2891 struct btrfs_dir_item *di;
2892 struct btrfs_key key;
2893 u64 index;
2894 int ret;
2895
2896 path = btrfs_alloc_path();
2897 if (!path)
2898 return -ENOMEM;
2899
2900 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
2901 name, name_len, -1);
2902 BUG_ON(!di || IS_ERR(di));
2903
2904 leaf = path->nodes[0];
2905 btrfs_dir_item_key_to_cpu(leaf, di, &key);
2906 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
2907 ret = btrfs_delete_one_dir_name(trans, root, path, di);
2908 BUG_ON(ret);
2909 btrfs_release_path(root, path);
2910
2911 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
2912 objectid, root->root_key.objectid,
2913 dir->i_ino, &index, name, name_len);
2914 if (ret < 0) {
2915 BUG_ON(ret != -ENOENT);
2916 di = btrfs_search_dir_index_item(root, path, dir->i_ino,
2917 name, name_len);
2918 BUG_ON(!di || IS_ERR(di));
2919
2920 leaf = path->nodes[0];
2921 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2922 btrfs_release_path(root, path);
2923 index = key.offset;
2924 }
2925
2926 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
2927 index, name, name_len, -1);
2928 BUG_ON(!di || IS_ERR(di));
2929
2930 leaf = path->nodes[0];
2931 btrfs_dir_item_key_to_cpu(leaf, di, &key);
2932 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
2933 ret = btrfs_delete_one_dir_name(trans, root, path, di);
2934 BUG_ON(ret);
2935 btrfs_release_path(root, path);
2936
2937 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
2938 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2939 ret = btrfs_update_inode(trans, root, dir);
2940 BUG_ON(ret);
2941 dir->i_sb->s_dirt = 1;
2942
2943 btrfs_free_path(path);
2944 return 0;
2945}
2946
Chris Mason39279cc2007-06-12 06:35:45 -04002947static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
2948{
2949 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002950 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002951 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04002952 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -05002953 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002954
Chris Mason3394e162008-11-17 20:42:26 -05002955 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002956 inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan134d4512007-10-25 15:49:25 -04002957 return -ENOTEMPTY;
2958
Yan, Zhenga22285a2010-05-16 10:48:46 -04002959 trans = __unlink_start_trans(dir, dentry);
2960 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05002961 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05002962
Chris Mason39279cc2007-06-12 06:35:45 -04002963 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -04002964
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002965 if (unlikely(inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
2966 err = btrfs_unlink_subvol(trans, root, dir,
2967 BTRFS_I(inode)->location.objectid,
2968 dentry->d_name.name,
2969 dentry->d_name.len);
2970 goto out;
2971 }
2972
Josef Bacik7b128762008-07-24 12:17:14 -04002973 err = btrfs_orphan_add(trans, inode);
2974 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002975 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04002976
Chris Mason39279cc2007-06-12 06:35:45 -04002977 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04002978 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
2979 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05002980 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04002981 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002982out:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002983 nr = trans->blocks_used;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002984 __unlink_end_trans(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002985 btrfs_btree_balance_dirty(root, nr);
Chris Mason39544012007-12-12 14:38:19 -05002986
Chris Mason39279cc2007-06-12 06:35:45 -04002987 return err;
2988}
2989
Chris Masond20f7042008-12-08 16:58:54 -05002990#if 0
Chris Mason39279cc2007-06-12 06:35:45 -04002991/*
Chris Mason323ac952008-10-01 19:05:46 -04002992 * when truncating bytes in a file, it is possible to avoid reading
2993 * the leaves that contain only checksum items. This can be the
2994 * majority of the IO required to delete a large file, but it must
2995 * be done carefully.
2996 *
2997 * The keys in the level just above the leaves are checked to make sure
2998 * the lowest key in a given leaf is a csum key, and starts at an offset
2999 * after the new size.
3000 *
3001 * Then the key for the next leaf is checked to make sure it also has
3002 * a checksum item for the same file. If it does, we know our target leaf
3003 * contains only checksum items, and it can be safely freed without reading
3004 * it.
3005 *
3006 * This is just an optimization targeted at large files. It may do
3007 * nothing. It will return 0 unless things went badly.
3008 */
3009static noinline int drop_csum_leaves(struct btrfs_trans_handle *trans,
3010 struct btrfs_root *root,
3011 struct btrfs_path *path,
3012 struct inode *inode, u64 new_size)
3013{
3014 struct btrfs_key key;
3015 int ret;
3016 int nritems;
3017 struct btrfs_key found_key;
3018 struct btrfs_key other_key;
Yan Zheng5b84e8d2008-10-09 11:46:19 -04003019 struct btrfs_leaf_ref *ref;
3020 u64 leaf_gen;
3021 u64 leaf_start;
Chris Mason323ac952008-10-01 19:05:46 -04003022
3023 path->lowest_level = 1;
3024 key.objectid = inode->i_ino;
3025 key.type = BTRFS_CSUM_ITEM_KEY;
3026 key.offset = new_size;
3027again:
3028 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3029 if (ret < 0)
3030 goto out;
3031
3032 if (path->nodes[1] == NULL) {
3033 ret = 0;
3034 goto out;
3035 }
3036 ret = 0;
3037 btrfs_node_key_to_cpu(path->nodes[1], &found_key, path->slots[1]);
3038 nritems = btrfs_header_nritems(path->nodes[1]);
3039
3040 if (!nritems)
3041 goto out;
3042
3043 if (path->slots[1] >= nritems)
3044 goto next_node;
3045
3046 /* did we find a key greater than anything we want to delete? */
3047 if (found_key.objectid > inode->i_ino ||
3048 (found_key.objectid == inode->i_ino && found_key.type > key.type))
3049 goto out;
3050
3051 /* we check the next key in the node to make sure the leave contains
3052 * only checksum items. This comparison doesn't work if our
3053 * leaf is the last one in the node
3054 */
3055 if (path->slots[1] + 1 >= nritems) {
3056next_node:
3057 /* search forward from the last key in the node, this
3058 * will bring us into the next node in the tree
3059 */
3060 btrfs_node_key_to_cpu(path->nodes[1], &found_key, nritems - 1);
3061
3062 /* unlikely, but we inc below, so check to be safe */
3063 if (found_key.offset == (u64)-1)
3064 goto out;
3065
3066 /* search_forward needs a path with locks held, do the
3067 * search again for the original key. It is possible
3068 * this will race with a balance and return a path that
3069 * we could modify, but this drop is just an optimization
3070 * and is allowed to miss some leaves.
3071 */
3072 btrfs_release_path(root, path);
3073 found_key.offset++;
3074
3075 /* setup a max key for search_forward */
3076 other_key.offset = (u64)-1;
3077 other_key.type = key.type;
3078 other_key.objectid = key.objectid;
3079
3080 path->keep_locks = 1;
3081 ret = btrfs_search_forward(root, &found_key, &other_key,
3082 path, 0, 0);
3083 path->keep_locks = 0;
3084 if (ret || found_key.objectid != key.objectid ||
3085 found_key.type != key.type) {
3086 ret = 0;
3087 goto out;
3088 }
3089
3090 key.offset = found_key.offset;
3091 btrfs_release_path(root, path);
3092 cond_resched();
3093 goto again;
3094 }
3095
3096 /* we know there's one more slot after us in the tree,
3097 * read that key so we can verify it is also a checksum item
3098 */
3099 btrfs_node_key_to_cpu(path->nodes[1], &other_key, path->slots[1] + 1);
3100
3101 if (found_key.objectid < inode->i_ino)
3102 goto next_key;
3103
3104 if (found_key.type != key.type || found_key.offset < new_size)
3105 goto next_key;
3106
3107 /*
3108 * if the key for the next leaf isn't a csum key from this objectid,
3109 * we can't be sure there aren't good items inside this leaf.
3110 * Bail out
3111 */
3112 if (other_key.objectid != inode->i_ino || other_key.type != key.type)
3113 goto out;
3114
Yan Zheng5b84e8d2008-10-09 11:46:19 -04003115 leaf_start = btrfs_node_blockptr(path->nodes[1], path->slots[1]);
3116 leaf_gen = btrfs_node_ptr_generation(path->nodes[1], path->slots[1]);
Chris Mason323ac952008-10-01 19:05:46 -04003117 /*
3118 * it is safe to delete this leaf, it contains only
3119 * csum items from this inode at an offset >= new_size
3120 */
Yan Zheng5b84e8d2008-10-09 11:46:19 -04003121 ret = btrfs_del_leaf(trans, root, path, leaf_start);
Chris Mason323ac952008-10-01 19:05:46 -04003122 BUG_ON(ret);
3123
Yan Zheng5b84e8d2008-10-09 11:46:19 -04003124 if (root->ref_cows && leaf_gen < trans->transid) {
3125 ref = btrfs_alloc_leaf_ref(root, 0);
3126 if (ref) {
3127 ref->root_gen = root->root_key.offset;
3128 ref->bytenr = leaf_start;
3129 ref->owner = 0;
3130 ref->generation = leaf_gen;
3131 ref->nritems = 0;
3132
Chris Masonbd56b302009-02-04 09:27:02 -05003133 btrfs_sort_leaf_ref(ref);
3134
Yan Zheng5b84e8d2008-10-09 11:46:19 -04003135 ret = btrfs_add_leaf_ref(root, ref, 0);
3136 WARN_ON(ret);
3137 btrfs_free_leaf_ref(root, ref);
3138 } else {
3139 WARN_ON(1);
3140 }
3141 }
Chris Mason323ac952008-10-01 19:05:46 -04003142next_key:
3143 btrfs_release_path(root, path);
3144
3145 if (other_key.objectid == inode->i_ino &&
3146 other_key.type == key.type && other_key.offset > key.offset) {
3147 key.offset = other_key.offset;
3148 cond_resched();
3149 goto again;
3150 }
3151 ret = 0;
3152out:
3153 /* fixup any changes we've made to the path */
3154 path->lowest_level = 0;
3155 path->keep_locks = 0;
3156 btrfs_release_path(root, path);
3157 return ret;
3158}
3159
Chris Masond20f7042008-12-08 16:58:54 -05003160#endif
3161
Chris Mason323ac952008-10-01 19:05:46 -04003162/*
Chris Mason39279cc2007-06-12 06:35:45 -04003163 * this can truncate away extent items, csum items and directory items.
3164 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04003165 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04003166 *
3167 * csum items that cross the new i_size are truncated to the new size
3168 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04003169 *
3170 * min_type is the minimum key type to truncate down to. If set to 0, this
3171 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04003172 */
Yan, Zheng80825102009-11-12 09:35:36 +00003173int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
3174 struct btrfs_root *root,
3175 struct inode *inode,
3176 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003177{
Chris Mason39279cc2007-06-12 06:35:45 -04003178 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003179 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003180 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00003181 struct btrfs_key key;
3182 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04003183 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04003184 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003185 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003186 u64 item_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00003187 u64 mask = root->sectorsize - 1;
3188 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04003189 int found_extent;
3190 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003191 int pending_del_nr = 0;
3192 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04003193 int extent_type = -1;
Chris Mason771ed682008-11-06 22:02:51 -05003194 int encoding;
Yan, Zheng80825102009-11-12 09:35:36 +00003195 int ret;
3196 int err = 0;
3197
3198 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04003199
Chris Masone02119d2008-09-05 16:13:11 -04003200 if (root->ref_cows)
Zheng Yan5b21f2e2008-09-26 10:05:38 -04003201 btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003202
Chris Mason39279cc2007-06-12 06:35:45 -04003203 path = btrfs_alloc_path();
3204 BUG_ON(!path);
Julia Lawall33c17ad2009-07-22 16:49:01 -04003205 path->reada = -1;
Chris Mason5f39d392007-10-15 16:14:19 -04003206
Chris Mason39279cc2007-06-12 06:35:45 -04003207 key.objectid = inode->i_ino;
3208 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04003209 key.type = (u8)-1;
3210
Chris Mason85e21ba2008-01-29 15:11:36 -05003211search_again:
Chris Masonb9473432009-03-13 11:00:37 -04003212 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05003213 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00003214 if (ret < 0) {
3215 err = ret;
3216 goto out;
3217 }
Chris Masond3977122009-01-05 21:25:51 -05003218
Chris Mason85e21ba2008-01-29 15:11:36 -05003219 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003220 /* there are no items in the tree for us to truncate, we're
3221 * done
3222 */
Yan, Zheng80825102009-11-12 09:35:36 +00003223 if (path->slots[0] == 0)
3224 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05003225 path->slots[0]--;
3226 }
3227
Chris Masond3977122009-01-05 21:25:51 -05003228 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04003229 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04003230 leaf = path->nodes[0];
3231 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3232 found_type = btrfs_key_type(&found_key);
Chris Mason771ed682008-11-06 22:02:51 -05003233 encoding = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003234
Chris Mason5f39d392007-10-15 16:14:19 -04003235 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -04003236 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003237
Chris Mason85e21ba2008-01-29 15:11:36 -05003238 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003239 break;
3240
Chris Mason5f39d392007-10-15 16:14:19 -04003241 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04003242 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04003243 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04003244 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04003245 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Mason771ed682008-11-06 22:02:51 -05003246 encoding = btrfs_file_extent_compression(leaf, fi);
3247 encoding |= btrfs_file_extent_encryption(leaf, fi);
3248 encoding |= btrfs_file_extent_other_encoding(leaf, fi);
3249
Chris Mason179e29e2007-11-01 11:28:41 -04003250 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04003251 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04003252 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04003253 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04003254 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04003255 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003256 }
Yan008630c2007-11-07 13:31:09 -05003257 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04003258 }
Yan, Zheng80825102009-11-12 09:35:36 +00003259 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04003260 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00003261 } else {
3262 if (item_end < new_size)
3263 break;
3264 if (found_key.offset >= new_size)
3265 del_item = 1;
3266 else
3267 del_item = 0;
3268 }
Chris Mason39279cc2007-06-12 06:35:45 -04003269 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003270 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04003271 if (found_type != BTRFS_EXTENT_DATA_KEY)
3272 goto delete;
3273
3274 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04003275 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04003276 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason771ed682008-11-06 22:02:51 -05003277 if (!del_item && !encoding) {
Chris Masondb945352007-10-15 16:15:53 -04003278 u64 orig_num_bytes =
3279 btrfs_file_extent_num_bytes(leaf, fi);
Chris Masone02119d2008-09-05 16:13:11 -04003280 extent_num_bytes = new_size -
Chris Mason5f39d392007-10-15 16:14:19 -04003281 found_key.offset + root->sectorsize - 1;
Yanb1632b12008-01-30 11:54:04 -05003282 extent_num_bytes = extent_num_bytes &
3283 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04003284 btrfs_set_file_extent_num_bytes(leaf, fi,
3285 extent_num_bytes);
3286 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05003287 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04003288 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003289 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04003290 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003291 } else {
Chris Masondb945352007-10-15 16:15:53 -04003292 extent_num_bytes =
3293 btrfs_file_extent_disk_num_bytes(leaf,
3294 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003295 extent_offset = found_key.offset -
3296 btrfs_file_extent_offset(leaf, fi);
3297
Chris Mason39279cc2007-06-12 06:35:45 -04003298 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05003299 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003300 if (extent_start != 0) {
3301 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04003302 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003303 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04003304 }
3305 }
Chris Mason90692182008-02-08 13:49:28 -05003306 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04003307 /*
3308 * we can't truncate inline items that have had
3309 * special encodings
3310 */
3311 if (!del_item &&
3312 btrfs_file_extent_compression(leaf, fi) == 0 &&
3313 btrfs_file_extent_encryption(leaf, fi) == 0 &&
3314 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003315 u32 size = new_size - found_key.offset;
3316
3317 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003318 inode_sub_bytes(inode, item_end + 1 -
3319 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04003320 }
3321 size =
3322 btrfs_file_extent_calc_inline_size(size);
Chris Mason90692182008-02-08 13:49:28 -05003323 ret = btrfs_truncate_item(trans, root, path,
Chris Masone02119d2008-09-05 16:13:11 -04003324 size, 1);
Chris Mason90692182008-02-08 13:49:28 -05003325 BUG_ON(ret);
Chris Masone02119d2008-09-05 16:13:11 -04003326 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003327 inode_sub_bytes(inode, item_end + 1 -
3328 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05003329 }
Chris Mason39279cc2007-06-12 06:35:45 -04003330 }
Chris Mason179e29e2007-11-01 11:28:41 -04003331delete:
Chris Mason39279cc2007-06-12 06:35:45 -04003332 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05003333 if (!pending_del_nr) {
3334 /* no pending yet, add ourselves */
3335 pending_del_slot = path->slots[0];
3336 pending_del_nr = 1;
3337 } else if (pending_del_nr &&
3338 path->slots[0] + 1 == pending_del_slot) {
3339 /* hop on the pending chunk */
3340 pending_del_nr++;
3341 pending_del_slot = path->slots[0];
3342 } else {
Chris Masond3977122009-01-05 21:25:51 -05003343 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05003344 }
Chris Mason39279cc2007-06-12 06:35:45 -04003345 } else {
3346 break;
3347 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003348 if (found_extent && root->ref_cows) {
Chris Masonb9473432009-03-13 11:00:37 -04003349 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003350 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003351 extent_num_bytes, 0,
3352 btrfs_header_owner(leaf),
3353 inode->i_ino, extent_offset);
Chris Mason39279cc2007-06-12 06:35:45 -04003354 BUG_ON(ret);
3355 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003356
Yan, Zheng80825102009-11-12 09:35:36 +00003357 if (found_type == BTRFS_INODE_ITEM_KEY)
3358 break;
3359
3360 if (path->slots[0] == 0 ||
3361 path->slots[0] != pending_del_slot) {
3362 if (root->ref_cows) {
3363 err = -EAGAIN;
3364 goto out;
3365 }
3366 if (pending_del_nr) {
3367 ret = btrfs_del_items(trans, root, path,
3368 pending_del_slot,
3369 pending_del_nr);
3370 BUG_ON(ret);
3371 pending_del_nr = 0;
3372 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003373 btrfs_release_path(root, path);
3374 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00003375 } else {
3376 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05003377 }
Chris Mason39279cc2007-06-12 06:35:45 -04003378 }
Yan, Zheng80825102009-11-12 09:35:36 +00003379out:
Chris Mason85e21ba2008-01-29 15:11:36 -05003380 if (pending_del_nr) {
3381 ret = btrfs_del_items(trans, root, path, pending_del_slot,
3382 pending_del_nr);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003383 BUG_ON(ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05003384 }
Chris Mason39279cc2007-06-12 06:35:45 -04003385 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00003386 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003387}
3388
Chris Masona52d9a82007-08-27 16:49:44 -04003389/*
3390 * taken from block_truncate_page, but does cow as it zeros out
3391 * any bytes left in the last page in the file.
3392 */
3393static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
3394{
3395 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -04003396 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003397 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3398 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003399 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003400 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04003401 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04003402 pgoff_t index = from >> PAGE_CACHE_SHIFT;
3403 unsigned offset = from & (PAGE_CACHE_SIZE-1);
3404 struct page *page;
3405 int ret = 0;
3406 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003407 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04003408
3409 if ((offset & (blocksize - 1)) == 0)
3410 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003411 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003412 if (ret)
3413 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04003414
3415 ret = -ENOMEM;
Chris Mason211c17f2008-05-15 09:13:45 -04003416again:
Chris Masona52d9a82007-08-27 16:49:44 -04003417 page = grab_cache_page(mapping, index);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003418 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003419 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Masona52d9a82007-08-27 16:49:44 -04003420 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04003421 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003422
3423 page_start = page_offset(page);
3424 page_end = page_start + PAGE_CACHE_SIZE - 1;
3425
Chris Masona52d9a82007-08-27 16:49:44 -04003426 if (!PageUptodate(page)) {
3427 ret = btrfs_readpage(NULL, page);
3428 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04003429 if (page->mapping != mapping) {
3430 unlock_page(page);
3431 page_cache_release(page);
3432 goto again;
3433 }
Chris Masona52d9a82007-08-27 16:49:44 -04003434 if (!PageUptodate(page)) {
3435 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04003436 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04003437 }
3438 }
Chris Mason211c17f2008-05-15 09:13:45 -04003439 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003440
Josef Bacik2ac55d42010-02-03 19:33:23 +00003441 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state,
3442 GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003443 set_page_extent_mapped(page);
3444
3445 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3446 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00003447 unlock_extent_cached(io_tree, page_start, page_end,
3448 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003449 unlock_page(page);
3450 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04003451 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003452 btrfs_put_ordered_extent(ordered);
3453 goto again;
3454 }
3455
Josef Bacik2ac55d42010-02-03 19:33:23 +00003456 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Josef Bacik5d5e1032009-10-13 16:46:49 -04003457 EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003458 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003459
Josef Bacik2ac55d42010-02-03 19:33:23 +00003460 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
3461 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003462 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00003463 unlock_extent_cached(io_tree, page_start, page_end,
3464 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003465 goto out_unlock;
3466 }
3467
Chris Masone6dcd2d2008-07-17 12:53:50 -04003468 ret = 0;
3469 if (offset != PAGE_CACHE_SIZE) {
3470 kaddr = kmap(page);
3471 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
3472 flush_dcache_page(page);
3473 kunmap(page);
3474 }
Chris Mason247e7432008-07-17 12:53:51 -04003475 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003476 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003477 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
3478 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04003479
Chris Mason89642222008-07-24 09:41:53 -04003480out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04003481 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003482 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04003483 unlock_page(page);
3484 page_cache_release(page);
3485out:
3486 return ret;
3487}
3488
Yan Zheng9036c102008-10-30 14:19:41 -04003489int btrfs_cont_expand(struct inode *inode, loff_t size)
3490{
3491 struct btrfs_trans_handle *trans;
3492 struct btrfs_root *root = BTRFS_I(inode)->root;
3493 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003494 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003495 struct extent_state *cached_state = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04003496 u64 mask = root->sectorsize - 1;
3497 u64 hole_start = (inode->i_size + mask) & ~mask;
3498 u64 block_end = (size + mask) & ~mask;
3499 u64 last_byte;
3500 u64 cur_offset;
3501 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003502 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04003503
3504 if (size <= hole_start)
3505 return 0;
3506
Yan Zheng9036c102008-10-30 14:19:41 -04003507 while (1) {
3508 struct btrfs_ordered_extent *ordered;
3509 btrfs_wait_ordered_range(inode, hole_start,
3510 block_end - hole_start);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003511 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
3512 &cached_state, GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003513 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
3514 if (!ordered)
3515 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003516 unlock_extent_cached(io_tree, hole_start, block_end - 1,
3517 &cached_state, GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003518 btrfs_put_ordered_extent(ordered);
3519 }
3520
Yan Zheng9036c102008-10-30 14:19:41 -04003521 cur_offset = hole_start;
3522 while (1) {
3523 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
3524 block_end - cur_offset, 0);
3525 BUG_ON(IS_ERR(em) || !em);
3526 last_byte = min(extent_map_end(em), block_end);
3527 last_byte = (last_byte + mask) & ~mask;
Yan, Zheng80825102009-11-12 09:35:36 +00003528 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Chris Mason771ed682008-11-06 22:02:51 -05003529 u64 hint_byte = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04003530 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00003531
Yan, Zhenga22285a2010-05-16 10:48:46 -04003532 trans = btrfs_start_transaction(root, 2);
3533 if (IS_ERR(trans)) {
3534 err = PTR_ERR(trans);
Chris Mason771ed682008-11-06 22:02:51 -05003535 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003536 }
Yan, Zheng80825102009-11-12 09:35:36 +00003537 btrfs_set_trans_block_group(trans, inode);
3538
3539 err = btrfs_drop_extents(trans, inode, cur_offset,
3540 cur_offset + hole_size,
3541 &hint_byte, 1);
3542 BUG_ON(err);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003543
Yan Zheng9036c102008-10-30 14:19:41 -04003544 err = btrfs_insert_file_extent(trans, root,
3545 inode->i_ino, cur_offset, 0,
3546 0, hole_size, 0, hole_size,
3547 0, 0, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003548 BUG_ON(err);
3549
Yan Zheng9036c102008-10-30 14:19:41 -04003550 btrfs_drop_extent_cache(inode, hole_start,
3551 last_byte - 1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003552
3553 btrfs_end_transaction(trans, root);
Yan Zheng9036c102008-10-30 14:19:41 -04003554 }
3555 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003556 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04003557 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00003558 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04003559 break;
3560 }
3561
Yan, Zhenga22285a2010-05-16 10:48:46 -04003562 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003563 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
3564 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003565 return err;
3566}
3567
Yan, Zheng80825102009-11-12 09:35:36 +00003568static int btrfs_setattr_size(struct inode *inode, struct iattr *attr)
3569{
3570 struct btrfs_root *root = BTRFS_I(inode)->root;
3571 struct btrfs_trans_handle *trans;
3572 unsigned long nr;
3573 int ret;
3574
3575 if (attr->ia_size == inode->i_size)
3576 return 0;
3577
3578 if (attr->ia_size > inode->i_size) {
3579 unsigned long limit;
3580 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
3581 if (attr->ia_size > inode->i_sb->s_maxbytes)
3582 return -EFBIG;
3583 if (limit != RLIM_INFINITY && attr->ia_size > limit) {
3584 send_sig(SIGXFSZ, current, 0);
3585 return -EFBIG;
3586 }
3587 }
3588
Yan, Zhengd68fc572010-05-16 10:49:58 -04003589 trans = btrfs_start_transaction(root, 5);
3590 if (IS_ERR(trans))
3591 return PTR_ERR(trans);
3592
Yan, Zheng80825102009-11-12 09:35:36 +00003593 btrfs_set_trans_block_group(trans, inode);
3594
3595 ret = btrfs_orphan_add(trans, inode);
3596 BUG_ON(ret);
3597
3598 nr = trans->blocks_used;
3599 btrfs_end_transaction(trans, root);
Yan, Zheng80825102009-11-12 09:35:36 +00003600 btrfs_btree_balance_dirty(root, nr);
3601
3602 if (attr->ia_size > inode->i_size) {
3603 ret = btrfs_cont_expand(inode, attr->ia_size);
3604 if (ret) {
3605 btrfs_truncate(inode);
3606 return ret;
3607 }
3608
3609 i_size_write(inode, attr->ia_size);
3610 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
3611
Yan, Zhengd68fc572010-05-16 10:49:58 -04003612 trans = btrfs_start_transaction(root, 0);
3613 BUG_ON(IS_ERR(trans));
Yan, Zheng80825102009-11-12 09:35:36 +00003614 btrfs_set_trans_block_group(trans, inode);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003615 trans->block_rsv = root->orphan_block_rsv;
3616 BUG_ON(!trans->block_rsv);
Yan, Zheng80825102009-11-12 09:35:36 +00003617
3618 ret = btrfs_update_inode(trans, root, inode);
3619 BUG_ON(ret);
3620 if (inode->i_nlink > 0) {
3621 ret = btrfs_orphan_del(trans, inode);
3622 BUG_ON(ret);
3623 }
3624 nr = trans->blocks_used;
3625 btrfs_end_transaction(trans, root);
3626 btrfs_btree_balance_dirty(root, nr);
3627 return 0;
3628 }
3629
3630 /*
3631 * We're truncating a file that used to have good data down to
3632 * zero. Make sure it gets into the ordered flush list so that
3633 * any new writes get down to disk quickly.
3634 */
3635 if (attr->ia_size == 0)
3636 BTRFS_I(inode)->ordered_data_close = 1;
3637
3638 /* we don't support swapfiles, so vmtruncate shouldn't fail */
3639 ret = vmtruncate(inode, attr->ia_size);
3640 BUG_ON(ret);
3641
3642 return 0;
3643}
3644
Chris Mason39279cc2007-06-12 06:35:45 -04003645static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
3646{
3647 struct inode *inode = dentry->d_inode;
3648 int err;
3649
3650 err = inode_change_ok(inode, attr);
3651 if (err)
3652 return err;
3653
Chris Mason5a3f23d2009-03-31 13:27:11 -04003654 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Yan, Zheng80825102009-11-12 09:35:36 +00003655 err = btrfs_setattr_size(inode, attr);
3656 if (err)
3657 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003658 }
Yan, Zheng80825102009-11-12 09:35:36 +00003659 attr->ia_valid &= ~ATTR_SIZE;
Yan Zheng9036c102008-10-30 14:19:41 -04003660
Yan, Zheng80825102009-11-12 09:35:36 +00003661 if (attr->ia_valid)
3662 err = inode_setattr(inode, attr);
Josef Bacik33268ea2008-07-24 12:16:36 -04003663
3664 if (!err && ((attr->ia_valid & ATTR_MODE)))
3665 err = btrfs_acl_chmod(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003666 return err;
3667}
Chris Mason61295eb2008-01-14 16:24:38 -05003668
Chris Mason39279cc2007-06-12 06:35:45 -04003669void btrfs_delete_inode(struct inode *inode)
3670{
3671 struct btrfs_trans_handle *trans;
3672 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003673 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04003674 int ret;
3675
3676 truncate_inode_pages(&inode->i_data, 0);
3677 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04003678 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003679 goto no_delete;
3680 }
Chris Mason4a096752008-07-21 10:29:44 -04003681 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04003682
Yan, Zhengc71bf092009-11-12 09:34:40 +00003683 if (root->fs_info->log_root_recovering) {
3684 BUG_ON(!list_empty(&BTRFS_I(inode)->i_orphan));
3685 goto no_delete;
3686 }
3687
Yan, Zheng76dda932009-09-21 16:00:26 -04003688 if (inode->i_nlink > 0) {
3689 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
3690 goto no_delete;
3691 }
3692
Chris Masondbe674a2008-07-17 12:54:05 -04003693 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003694
Yan, Zheng80825102009-11-12 09:35:36 +00003695 while (1) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003696 trans = btrfs_start_transaction(root, 0);
3697 BUG_ON(IS_ERR(trans));
Yan, Zheng80825102009-11-12 09:35:36 +00003698 btrfs_set_trans_block_group(trans, inode);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003699 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00003700
Yan, Zhengd68fc572010-05-16 10:49:58 -04003701 ret = btrfs_block_rsv_check(trans, root,
3702 root->orphan_block_rsv, 0, 5);
3703 if (ret) {
3704 BUG_ON(ret != -EAGAIN);
3705 ret = btrfs_commit_transaction(trans, root);
3706 BUG_ON(ret);
3707 continue;
3708 }
3709
3710 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003711 if (ret != -EAGAIN)
3712 break;
3713
3714 nr = trans->blocks_used;
3715 btrfs_end_transaction(trans, root);
3716 trans = NULL;
3717 btrfs_btree_balance_dirty(root, nr);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003718
Josef Bacik7b128762008-07-24 12:17:14 -04003719 }
3720
Yan, Zheng80825102009-11-12 09:35:36 +00003721 if (ret == 0) {
3722 ret = btrfs_orphan_del(trans, inode);
3723 BUG_ON(ret);
3724 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003725
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003726 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04003727 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003728 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003729no_delete:
3730 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003731 return;
Chris Mason39279cc2007-06-12 06:35:45 -04003732}
3733
3734/*
3735 * this returns the key found in the dir entry in the location pointer.
3736 * If no dir entries were found, location->objectid is 0.
3737 */
3738static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
3739 struct btrfs_key *location)
3740{
3741 const char *name = dentry->d_name.name;
3742 int namelen = dentry->d_name.len;
3743 struct btrfs_dir_item *di;
3744 struct btrfs_path *path;
3745 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04003746 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003747
3748 path = btrfs_alloc_path();
3749 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -05003750
Chris Mason39279cc2007-06-12 06:35:45 -04003751 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
3752 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04003753 if (IS_ERR(di))
3754 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05003755
3756 if (!di || IS_ERR(di))
Chris Mason39544012007-12-12 14:38:19 -05003757 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05003758
Chris Mason5f39d392007-10-15 16:14:19 -04003759 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04003760out:
Chris Mason39279cc2007-06-12 06:35:45 -04003761 btrfs_free_path(path);
3762 return ret;
Chris Mason39544012007-12-12 14:38:19 -05003763out_err:
3764 location->objectid = 0;
3765 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04003766}
3767
3768/*
3769 * when we hit a tree root in a directory, the btrfs part of the inode
3770 * needs to be changed to reflect the root directory of the tree root. This
3771 * is kind of like crossing a mount point.
3772 */
3773static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003774 struct inode *dir,
3775 struct dentry *dentry,
3776 struct btrfs_key *location,
3777 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04003778{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003779 struct btrfs_path *path;
3780 struct btrfs_root *new_root;
3781 struct btrfs_root_ref *ref;
3782 struct extent_buffer *leaf;
3783 int ret;
3784 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003785
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003786 path = btrfs_alloc_path();
3787 if (!path) {
3788 err = -ENOMEM;
3789 goto out;
3790 }
Chris Mason39279cc2007-06-12 06:35:45 -04003791
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003792 err = -ENOENT;
3793 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
3794 BTRFS_I(dir)->root->root_key.objectid,
3795 location->objectid);
3796 if (ret) {
3797 if (ret < 0)
3798 err = ret;
3799 goto out;
3800 }
Chris Mason39279cc2007-06-12 06:35:45 -04003801
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003802 leaf = path->nodes[0];
3803 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
3804 if (btrfs_root_ref_dirid(leaf, ref) != dir->i_ino ||
3805 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
3806 goto out;
3807
3808 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
3809 (unsigned long)(ref + 1),
3810 dentry->d_name.len);
3811 if (ret)
3812 goto out;
3813
3814 btrfs_release_path(root->fs_info->tree_root, path);
3815
3816 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
3817 if (IS_ERR(new_root)) {
3818 err = PTR_ERR(new_root);
3819 goto out;
3820 }
3821
3822 if (btrfs_root_refs(&new_root->root_item) == 0) {
3823 err = -ENOENT;
3824 goto out;
3825 }
3826
3827 *sub_root = new_root;
3828 location->objectid = btrfs_root_dirid(&new_root->root_item);
3829 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04003830 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003831 err = 0;
3832out:
3833 btrfs_free_path(path);
3834 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003835}
3836
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003837static void inode_tree_add(struct inode *inode)
3838{
3839 struct btrfs_root *root = BTRFS_I(inode)->root;
3840 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003841 struct rb_node **p;
3842 struct rb_node *parent;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003843again:
3844 p = &root->inode_tree.rb_node;
3845 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003846
Yan, Zheng76dda932009-09-21 16:00:26 -04003847 if (hlist_unhashed(&inode->i_hash))
3848 return;
3849
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003850 spin_lock(&root->inode_lock);
3851 while (*p) {
3852 parent = *p;
3853 entry = rb_entry(parent, struct btrfs_inode, rb_node);
3854
3855 if (inode->i_ino < entry->vfs_inode.i_ino)
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003856 p = &parent->rb_left;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003857 else if (inode->i_ino > entry->vfs_inode.i_ino)
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003858 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003859 else {
3860 WARN_ON(!(entry->vfs_inode.i_state &
3861 (I_WILL_FREE | I_FREEING | I_CLEAR)));
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003862 rb_erase(parent, &root->inode_tree);
3863 RB_CLEAR_NODE(parent);
3864 spin_unlock(&root->inode_lock);
3865 goto again;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003866 }
3867 }
3868 rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
3869 rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
3870 spin_unlock(&root->inode_lock);
3871}
3872
3873static void inode_tree_del(struct inode *inode)
3874{
3875 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04003876 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003877
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003878 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003879 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003880 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003881 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04003882 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003883 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003884 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04003885
3886 if (empty && btrfs_root_refs(&root->root_item) == 0) {
3887 synchronize_srcu(&root->fs_info->subvol_srcu);
3888 spin_lock(&root->inode_lock);
3889 empty = RB_EMPTY_ROOT(&root->inode_tree);
3890 spin_unlock(&root->inode_lock);
3891 if (empty)
3892 btrfs_add_dead_root(root);
3893 }
3894}
3895
3896int btrfs_invalidate_inodes(struct btrfs_root *root)
3897{
3898 struct rb_node *node;
3899 struct rb_node *prev;
3900 struct btrfs_inode *entry;
3901 struct inode *inode;
3902 u64 objectid = 0;
3903
3904 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
3905
3906 spin_lock(&root->inode_lock);
3907again:
3908 node = root->inode_tree.rb_node;
3909 prev = NULL;
3910 while (node) {
3911 prev = node;
3912 entry = rb_entry(node, struct btrfs_inode, rb_node);
3913
3914 if (objectid < entry->vfs_inode.i_ino)
3915 node = node->rb_left;
3916 else if (objectid > entry->vfs_inode.i_ino)
3917 node = node->rb_right;
3918 else
3919 break;
3920 }
3921 if (!node) {
3922 while (prev) {
3923 entry = rb_entry(prev, struct btrfs_inode, rb_node);
3924 if (objectid <= entry->vfs_inode.i_ino) {
3925 node = prev;
3926 break;
3927 }
3928 prev = rb_next(prev);
3929 }
3930 }
3931 while (node) {
3932 entry = rb_entry(node, struct btrfs_inode, rb_node);
3933 objectid = entry->vfs_inode.i_ino + 1;
3934 inode = igrab(&entry->vfs_inode);
3935 if (inode) {
3936 spin_unlock(&root->inode_lock);
3937 if (atomic_read(&inode->i_count) > 1)
3938 d_prune_aliases(inode);
3939 /*
3940 * btrfs_drop_inode will remove it from
3941 * the inode cache when its usage count
3942 * hits zero.
3943 */
3944 iput(inode);
3945 cond_resched();
3946 spin_lock(&root->inode_lock);
3947 goto again;
3948 }
3949
3950 if (cond_resched_lock(&root->inode_lock))
3951 goto again;
3952
3953 node = rb_next(node);
3954 }
3955 spin_unlock(&root->inode_lock);
3956 return 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003957}
3958
Chris Masone02119d2008-09-05 16:13:11 -04003959static int btrfs_init_locked_inode(struct inode *inode, void *p)
3960{
3961 struct btrfs_iget_args *args = p;
3962 inode->i_ino = args->ino;
Chris Masone02119d2008-09-05 16:13:11 -04003963 BTRFS_I(inode)->root = args->root;
Josef Bacik6a632092009-02-20 11:00:09 -05003964 btrfs_set_inode_space_info(args->root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003965 return 0;
3966}
3967
3968static int btrfs_find_actor(struct inode *inode, void *opaque)
3969{
3970 struct btrfs_iget_args *args = opaque;
Chris Masond3977122009-01-05 21:25:51 -05003971 return args->ino == inode->i_ino &&
3972 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003973}
3974
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003975static struct inode *btrfs_iget_locked(struct super_block *s,
3976 u64 objectid,
3977 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04003978{
3979 struct inode *inode;
3980 struct btrfs_iget_args args;
3981 args.ino = objectid;
3982 args.root = root;
3983
3984 inode = iget5_locked(s, objectid, btrfs_find_actor,
3985 btrfs_init_locked_inode,
3986 (void *)&args);
3987 return inode;
3988}
3989
Balaji Rao1a54ef82008-07-21 02:01:04 +05303990/* Get an inode object given its location and corresponding root.
3991 * Returns in *is_new if the inode was read from disk
3992 */
3993struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00003994 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05303995{
3996 struct inode *inode;
3997
3998 inode = btrfs_iget_locked(s, location->objectid, root);
3999 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004000 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05304001
4002 if (inode->i_state & I_NEW) {
4003 BTRFS_I(inode)->root = root;
4004 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
4005 btrfs_read_locked_inode(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004006
4007 inode_tree_add(inode);
Balaji Rao1a54ef82008-07-21 02:01:04 +05304008 unlock_new_inode(inode);
Josef Bacik73f73412009-12-04 17:38:27 +00004009 if (new)
4010 *new = 1;
Balaji Rao1a54ef82008-07-21 02:01:04 +05304011 }
4012
4013 return inode;
4014}
4015
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004016static struct inode *new_simple_dir(struct super_block *s,
4017 struct btrfs_key *key,
4018 struct btrfs_root *root)
4019{
4020 struct inode *inode = new_inode(s);
4021
4022 if (!inode)
4023 return ERR_PTR(-ENOMEM);
4024
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004025 BTRFS_I(inode)->root = root;
4026 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
4027 BTRFS_I(inode)->dummy_inode = 1;
4028
4029 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
4030 inode->i_op = &simple_dir_inode_operations;
4031 inode->i_fop = &simple_dir_operations;
4032 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
4033 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
4034
4035 return inode;
4036}
4037
Chris Mason3de45862008-11-17 21:02:50 -05004038struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04004039{
Chris Masond3977122009-01-05 21:25:51 -05004040 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004041 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004042 struct btrfs_root *sub_root = root;
4043 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04004044 int index;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004045 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004046
Yan, Zheng76dda932009-09-21 16:00:26 -04004047 dentry->d_op = &btrfs_dentry_operations;
4048
Chris Mason39279cc2007-06-12 06:35:45 -04004049 if (dentry->d_name.len > BTRFS_NAME_LEN)
4050 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04004051
Chris Mason39279cc2007-06-12 06:35:45 -04004052 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04004053
Chris Mason39279cc2007-06-12 06:35:45 -04004054 if (ret < 0)
4055 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04004056
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004057 if (location.objectid == 0)
4058 return NULL;
4059
4060 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00004061 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004062 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004063 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004064
4065 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
4066
Yan, Zheng76dda932009-09-21 16:00:26 -04004067 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004068 ret = fixup_tree_root_location(root, dir, dentry,
4069 &location, &sub_root);
4070 if (ret < 0) {
4071 if (ret != -ENOENT)
4072 inode = ERR_PTR(ret);
4073 else
4074 inode = new_simple_dir(dir->i_sb, &location, sub_root);
4075 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00004076 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004077 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004078 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
4079
Yan, Zhengc71bf092009-11-12 09:34:40 +00004080 if (root != sub_root) {
4081 down_read(&root->fs_info->cleanup_work_sem);
4082 if (!(inode->i_sb->s_flags & MS_RDONLY))
4083 btrfs_orphan_cleanup(sub_root);
4084 up_read(&root->fs_info->cleanup_work_sem);
4085 }
4086
Chris Mason3de45862008-11-17 21:02:50 -05004087 return inode;
4088}
4089
Yan, Zheng76dda932009-09-21 16:00:26 -04004090static int btrfs_dentry_delete(struct dentry *dentry)
4091{
4092 struct btrfs_root *root;
4093
Yan, Zhengefefb142009-10-09 09:25:16 -04004094 if (!dentry->d_inode && !IS_ROOT(dentry))
4095 dentry = dentry->d_parent;
Yan, Zheng76dda932009-09-21 16:00:26 -04004096
Yan, Zhengefefb142009-10-09 09:25:16 -04004097 if (dentry->d_inode) {
4098 root = BTRFS_I(dentry->d_inode)->root;
4099 if (btrfs_root_refs(&root->root_item) == 0)
4100 return 1;
4101 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004102 return 0;
4103}
4104
Chris Mason3de45862008-11-17 21:02:50 -05004105static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
4106 struct nameidata *nd)
4107{
4108 struct inode *inode;
4109
Chris Mason3de45862008-11-17 21:02:50 -05004110 inode = btrfs_lookup_dentry(dir, dentry);
4111 if (IS_ERR(inode))
4112 return ERR_CAST(inode);
Josef Bacik7b128762008-07-24 12:17:14 -04004113
Chris Mason39279cc2007-06-12 06:35:45 -04004114 return d_splice_alias(inode, dentry);
4115}
4116
Chris Mason39279cc2007-06-12 06:35:45 -04004117static unsigned char btrfs_filetype_table[] = {
4118 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
4119};
4120
David Woodhousecbdf5a22008-08-06 19:42:33 +01004121static int btrfs_real_readdir(struct file *filp, void *dirent,
4122 filldir_t filldir)
Chris Mason39279cc2007-06-12 06:35:45 -04004123{
Chris Mason6da6aba2007-12-18 16:15:09 -05004124 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004125 struct btrfs_root *root = BTRFS_I(inode)->root;
4126 struct btrfs_item *item;
4127 struct btrfs_dir_item *di;
4128 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04004129 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004130 struct btrfs_path *path;
4131 int ret;
4132 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04004133 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004134 int slot;
4135 int advance;
4136 unsigned char d_type;
4137 int over = 0;
4138 u32 di_cur;
4139 u32 di_total;
4140 u32 di_len;
4141 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04004142 char tmp_name[32];
4143 char *name_ptr;
4144 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04004145
4146 /* FIXME, use a real flag for deciding about the key type */
4147 if (root->fs_info->tree_root == root)
4148 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04004149
Chris Mason39544012007-12-12 14:38:19 -05004150 /* special case for "." */
4151 if (filp->f_pos == 0) {
4152 over = filldir(dirent, ".", 1,
4153 1, inode->i_ino,
4154 DT_DIR);
4155 if (over)
4156 return 0;
4157 filp->f_pos = 1;
4158 }
Chris Mason39544012007-12-12 14:38:19 -05004159 /* special case for .., just use the back ref */
4160 if (filp->f_pos == 1) {
David Woodhouse5ecc7e52008-08-17 15:14:48 +01004161 u64 pino = parent_ino(filp->f_path.dentry);
Chris Mason39544012007-12-12 14:38:19 -05004162 over = filldir(dirent, "..", 2,
David Woodhouse5ecc7e52008-08-17 15:14:48 +01004163 2, pino, DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05004164 if (over)
David Woodhouse49593bf2008-08-17 17:08:36 +01004165 return 0;
Chris Mason39544012007-12-12 14:38:19 -05004166 filp->f_pos = 2;
4167 }
David Woodhouse49593bf2008-08-17 17:08:36 +01004168 path = btrfs_alloc_path();
4169 path->reada = 2;
4170
Chris Mason39279cc2007-06-12 06:35:45 -04004171 btrfs_set_key_type(&key, key_type);
4172 key.offset = filp->f_pos;
David Woodhouse49593bf2008-08-17 17:08:36 +01004173 key.objectid = inode->i_ino;
Chris Mason5f39d392007-10-15 16:14:19 -04004174
Chris Mason39279cc2007-06-12 06:35:45 -04004175 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4176 if (ret < 0)
4177 goto err;
4178 advance = 0;
David Woodhouse49593bf2008-08-17 17:08:36 +01004179
4180 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04004181 leaf = path->nodes[0];
4182 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004183 slot = path->slots[0];
4184 if (advance || slot >= nritems) {
David Woodhouse49593bf2008-08-17 17:08:36 +01004185 if (slot >= nritems - 1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004186 ret = btrfs_next_leaf(root, path);
4187 if (ret)
4188 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004189 leaf = path->nodes[0];
4190 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004191 slot = path->slots[0];
4192 } else {
4193 slot++;
4194 path->slots[0]++;
4195 }
4196 }
Chris Mason3de45862008-11-17 21:02:50 -05004197
Chris Mason39279cc2007-06-12 06:35:45 -04004198 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04004199 item = btrfs_item_nr(leaf, slot);
4200 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4201
4202 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04004203 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004204 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004205 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004206 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04004207 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04004208
4209 filp->f_pos = found_key.offset;
David Woodhouse49593bf2008-08-17 17:08:36 +01004210
Chris Mason39279cc2007-06-12 06:35:45 -04004211 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
4212 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04004213 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01004214
4215 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04004216 struct btrfs_key location;
4217
4218 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01004219 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04004220 name_ptr = tmp_name;
4221 } else {
4222 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01004223 if (!name_ptr) {
4224 ret = -ENOMEM;
4225 goto err;
4226 }
Chris Mason5f39d392007-10-15 16:14:19 -04004227 }
4228 read_extent_buffer(leaf, name_ptr,
4229 (unsigned long)(di + 1), name_len);
4230
4231 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
4232 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05004233
4234 /* is this a reference to our own snapshot? If so
4235 * skip it
4236 */
4237 if (location.type == BTRFS_ROOT_ITEM_KEY &&
4238 location.objectid == root->root_key.objectid) {
4239 over = 0;
4240 goto skip;
4241 }
Chris Mason5f39d392007-10-15 16:14:19 -04004242 over = filldir(dirent, name_ptr, name_len,
David Woodhouse49593bf2008-08-17 17:08:36 +01004243 found_key.offset, location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04004244 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04004245
Chris Mason3de45862008-11-17 21:02:50 -05004246skip:
Chris Mason5f39d392007-10-15 16:14:19 -04004247 if (name_ptr != tmp_name)
4248 kfree(name_ptr);
4249
Chris Mason39279cc2007-06-12 06:35:45 -04004250 if (over)
4251 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05004252 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01004253 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04004254 di_cur += di_len;
4255 di = (struct btrfs_dir_item *)((char *)di + di_len);
4256 }
4257 }
David Woodhouse49593bf2008-08-17 17:08:36 +01004258
4259 /* Reached end of directory/root. Bump pos past the last item. */
Yan Zheng5e591a02008-02-19 11:41:02 -05004260 if (key_type == BTRFS_DIR_INDEX_KEY)
Jan Engelhardt406266a2009-12-09 22:00:38 +00004261 /*
4262 * 32-bit glibc will use getdents64, but then strtol -
4263 * so the last number we can serve is this.
4264 */
4265 filp->f_pos = 0x7fffffff;
Yan Zheng5e591a02008-02-19 11:41:02 -05004266 else
4267 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04004268nopos:
4269 ret = 0;
4270err:
Chris Mason39279cc2007-06-12 06:35:45 -04004271 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004272 return ret;
4273}
4274
Christoph Hellwiga9185b42010-03-05 09:21:37 +01004275int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04004276{
4277 struct btrfs_root *root = BTRFS_I(inode)->root;
4278 struct btrfs_trans_handle *trans;
4279 int ret = 0;
4280
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004281 if (BTRFS_I(inode)->dummy_inode)
Chris Mason4ca8b412008-08-05 13:30:48 -04004282 return 0;
4283
Christoph Hellwiga9185b42010-03-05 09:21:37 +01004284 if (wbc->sync_mode == WB_SYNC_ALL) {
Chris Masonf9295742008-07-17 12:54:14 -04004285 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04004286 btrfs_set_trans_block_group(trans, inode);
4287 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004288 }
4289 return ret;
4290}
4291
4292/*
Chris Mason54aa1f42007-06-22 14:16:25 -04004293 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04004294 * inode changes. But, it is most likely to find the inode in cache.
4295 * FIXME, needs more benchmarking...there are no reasons other than performance
4296 * to keep or drop this code.
4297 */
4298void btrfs_dirty_inode(struct inode *inode)
4299{
4300 struct btrfs_root *root = BTRFS_I(inode)->root;
4301 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004302 int ret;
4303
4304 if (BTRFS_I(inode)->dummy_inode)
4305 return;
Chris Mason39279cc2007-06-12 06:35:45 -04004306
Chris Masonf9295742008-07-17 12:54:14 -04004307 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04004308 btrfs_set_trans_block_group(trans, inode);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004309
4310 ret = btrfs_update_inode(trans, root, inode);
4311 if (ret)
4312 printk(KERN_ERR"btrfs: fail to dirty inode %lu error %d\n",
4313 inode->i_ino, ret);
4314
Chris Mason39279cc2007-06-12 06:35:45 -04004315 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004316}
4317
Chris Masond352ac62008-09-29 15:18:18 -04004318/*
4319 * find the highest existing sequence number in a directory
4320 * and then set the in-memory index_cnt variable to reflect
4321 * free sequence numbers
4322 */
Josef Bacikaec74772008-07-24 12:12:38 -04004323static int btrfs_set_inode_index_count(struct inode *inode)
4324{
4325 struct btrfs_root *root = BTRFS_I(inode)->root;
4326 struct btrfs_key key, found_key;
4327 struct btrfs_path *path;
4328 struct extent_buffer *leaf;
4329 int ret;
4330
4331 key.objectid = inode->i_ino;
4332 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
4333 key.offset = (u64)-1;
4334
4335 path = btrfs_alloc_path();
4336 if (!path)
4337 return -ENOMEM;
4338
4339 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4340 if (ret < 0)
4341 goto out;
4342 /* FIXME: we should be able to handle this */
4343 if (ret == 0)
4344 goto out;
4345 ret = 0;
4346
4347 /*
4348 * MAGIC NUMBER EXPLANATION:
4349 * since we search a directory based on f_pos we have to start at 2
4350 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
4351 * else has to start at 2
4352 */
4353 if (path->slots[0] == 0) {
4354 BTRFS_I(inode)->index_cnt = 2;
4355 goto out;
4356 }
4357
4358 path->slots[0]--;
4359
4360 leaf = path->nodes[0];
4361 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4362
4363 if (found_key.objectid != inode->i_ino ||
4364 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
4365 BTRFS_I(inode)->index_cnt = 2;
4366 goto out;
4367 }
4368
4369 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
4370out:
4371 btrfs_free_path(path);
4372 return ret;
4373}
4374
Chris Masond352ac62008-09-29 15:18:18 -04004375/*
4376 * helper to find a free sequence number in a given directory. This current
4377 * code is very simple, later versions will do smarter things in the btree
4378 */
Chris Mason3de45862008-11-17 21:02:50 -05004379int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04004380{
4381 int ret = 0;
4382
4383 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
4384 ret = btrfs_set_inode_index_count(dir);
Chris Masond3977122009-01-05 21:25:51 -05004385 if (ret)
Josef Bacikaec74772008-07-24 12:12:38 -04004386 return ret;
4387 }
4388
Chris Mason00e4e6b2008-08-05 11:18:09 -04004389 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04004390 BTRFS_I(dir)->index_cnt++;
4391
4392 return ret;
4393}
4394
Chris Mason39279cc2007-06-12 06:35:45 -04004395static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
4396 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04004397 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05004398 const char *name, int name_len,
Yan Zhengd2fb3432008-12-11 16:30:39 -05004399 u64 ref_objectid, u64 objectid,
4400 u64 alloc_hint, int mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04004401{
4402 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004403 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04004404 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04004405 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05004406 struct btrfs_inode_ref *ref;
4407 struct btrfs_key key[2];
4408 u32 sizes[2];
4409 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04004410 int ret;
4411 int owner;
4412
Chris Mason5f39d392007-10-15 16:14:19 -04004413 path = btrfs_alloc_path();
4414 BUG_ON(!path);
4415
Chris Mason39279cc2007-06-12 06:35:45 -04004416 inode = new_inode(root->fs_info->sb);
4417 if (!inode)
4418 return ERR_PTR(-ENOMEM);
4419
Josef Bacikaec74772008-07-24 12:12:38 -04004420 if (dir) {
Chris Mason3de45862008-11-17 21:02:50 -05004421 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04004422 if (ret) {
4423 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004424 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04004425 }
Josef Bacikaec74772008-07-24 12:12:38 -04004426 }
4427 /*
4428 * index_cnt is ignored for everything but a dir,
4429 * btrfs_get_inode_index_count has an explanation for the magic
4430 * number
4431 */
4432 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04004433 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04004434 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik6a632092009-02-20 11:00:09 -05004435 btrfs_set_inode_space_info(root, inode);
Chris Masonb888db22007-08-27 16:49:44 -04004436
Chris Mason39279cc2007-06-12 06:35:45 -04004437 if (mode & S_IFDIR)
4438 owner = 0;
4439 else
4440 owner = 1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05004441 BTRFS_I(inode)->block_group =
4442 btrfs_find_block_group(root, 0, alloc_hint, owner);
Chris Mason9c583092008-01-29 15:15:18 -05004443
4444 key[0].objectid = objectid;
4445 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
4446 key[0].offset = 0;
4447
4448 key[1].objectid = objectid;
4449 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
4450 key[1].offset = ref_objectid;
4451
4452 sizes[0] = sizeof(struct btrfs_inode_item);
4453 sizes[1] = name_len + sizeof(*ref);
4454
Chris Masonb9473432009-03-13 11:00:37 -04004455 path->leave_spinning = 1;
Chris Mason9c583092008-01-29 15:15:18 -05004456 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
4457 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04004458 goto fail;
4459
Chris Mason79683f22008-11-19 22:00:53 -05004460 inode->i_uid = current_fsuid();
Chris Ball8c087b52009-02-04 09:29:54 -05004461
Chris Mason42f15d72009-02-06 11:35:57 -05004462 if (dir && (dir->i_mode & S_ISGID)) {
Chris Ball8c087b52009-02-04 09:29:54 -05004463 inode->i_gid = dir->i_gid;
4464 if (S_ISDIR(mode))
4465 mode |= S_ISGID;
4466 } else
4467 inode->i_gid = current_fsgid();
4468
Chris Mason39279cc2007-06-12 06:35:45 -04004469 inode->i_mode = mode;
4470 inode->i_ino = objectid;
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004471 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004472 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04004473 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4474 struct btrfs_inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04004475 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05004476
4477 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
4478 struct btrfs_inode_ref);
4479 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04004480 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05004481 ptr = (unsigned long)(ref + 1);
4482 write_extent_buffer(path->nodes[0], name, ptr, name_len);
4483
Chris Mason5f39d392007-10-15 16:14:19 -04004484 btrfs_mark_buffer_dirty(path->nodes[0]);
4485 btrfs_free_path(path);
4486
Chris Mason39279cc2007-06-12 06:35:45 -04004487 location = &BTRFS_I(inode)->location;
4488 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04004489 location->offset = 0;
4490 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
4491
Christoph Hellwig6cbff002009-04-17 10:37:41 +02004492 btrfs_inherit_iflags(inode, dir);
4493
Chris Mason94272162009-07-02 12:26:06 -04004494 if ((mode & S_IFREG)) {
4495 if (btrfs_test_opt(root, NODATASUM))
4496 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
4497 if (btrfs_test_opt(root, NODATACOW))
4498 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
4499 }
4500
Chris Mason39279cc2007-06-12 06:35:45 -04004501 insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004502 inode_tree_add(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004503 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004504fail:
Josef Bacikaec74772008-07-24 12:12:38 -04004505 if (dir)
4506 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04004507 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04004508 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004509 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004510}
4511
4512static inline u8 btrfs_inode_type(struct inode *inode)
4513{
4514 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
4515}
4516
Chris Masond352ac62008-09-29 15:18:18 -04004517/*
4518 * utility function to add 'inode' into 'parent_inode' with
4519 * a give name and a given sequence number.
4520 * if 'add_backref' is true, also insert a backref from the
4521 * inode to the parent directory.
4522 */
Chris Masone02119d2008-09-05 16:13:11 -04004523int btrfs_add_link(struct btrfs_trans_handle *trans,
4524 struct inode *parent_inode, struct inode *inode,
4525 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04004526{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004527 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004528 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04004529 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Chris Mason5f39d392007-10-15 16:14:19 -04004530
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004531 if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
4532 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
4533 } else {
4534 key.objectid = inode->i_ino;
4535 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
4536 key.offset = 0;
4537 }
Chris Mason39279cc2007-06-12 06:35:45 -04004538
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004539 if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
4540 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
4541 key.objectid, root->root_key.objectid,
4542 parent_inode->i_ino,
4543 index, name, name_len);
4544 } else if (add_backref) {
4545 ret = btrfs_insert_inode_ref(trans, root,
4546 name, name_len, inode->i_ino,
4547 parent_inode->i_ino, index);
4548 }
4549
Chris Mason39279cc2007-06-12 06:35:45 -04004550 if (ret == 0) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004551 ret = btrfs_insert_dir_item(trans, root, name, name_len,
4552 parent_inode->i_ino, &key,
4553 btrfs_inode_type(inode), index);
4554 BUG_ON(ret);
4555
Chris Masondbe674a2008-07-17 12:54:05 -04004556 btrfs_i_size_write(parent_inode, parent_inode->i_size +
Chris Masone02119d2008-09-05 16:13:11 -04004557 name_len * 2);
Chris Mason79c44582007-06-25 10:09:33 -04004558 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Masone02119d2008-09-05 16:13:11 -04004559 ret = btrfs_update_inode(trans, root, parent_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004560 }
4561 return ret;
4562}
4563
4564static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05004565 struct dentry *dentry, struct inode *inode,
Chris Mason00e4e6b2008-08-05 11:18:09 -04004566 int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04004567{
Chris Masone02119d2008-09-05 16:13:11 -04004568 int err = btrfs_add_link(trans, dentry->d_parent->d_inode,
4569 inode, dentry->d_name.name,
4570 dentry->d_name.len, backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004571 if (!err) {
4572 d_instantiate(dentry, inode);
4573 return 0;
4574 }
4575 if (err > 0)
4576 err = -EEXIST;
4577 return err;
4578}
4579
Josef Bacik618e21d2007-07-11 10:18:17 -04004580static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
4581 int mode, dev_t rdev)
4582{
4583 struct btrfs_trans_handle *trans;
4584 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05004585 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04004586 int err;
4587 int drop_inode = 0;
4588 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05004589 unsigned long nr = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004590 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04004591
4592 if (!new_valid_dev(rdev))
4593 return -EINVAL;
4594
Yan, Zhenga22285a2010-05-16 10:48:46 -04004595 err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
4596 if (err)
4597 return err;
4598
Josef Bacik9ed74f22009-09-11 16:12:44 -04004599 /*
4600 * 2 for inode item and ref
4601 * 2 for dir items
4602 * 1 for xattr if selinux is on
4603 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004604 trans = btrfs_start_transaction(root, 5);
4605 if (IS_ERR(trans))
4606 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05004607
Josef Bacik618e21d2007-07-11 10:18:17 -04004608 btrfs_set_trans_block_group(trans, dir);
4609
Josef Bacikaec74772008-07-24 12:12:38 -04004610 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05004611 dentry->d_name.len,
4612 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04004613 BTRFS_I(dir)->block_group, mode, &index);
Josef Bacik618e21d2007-07-11 10:18:17 -04004614 err = PTR_ERR(inode);
4615 if (IS_ERR(inode))
4616 goto out_unlock;
4617
Yan, Zhengf34f57a2009-11-12 09:35:27 +00004618 err = btrfs_init_inode_security(trans, inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04004619 if (err) {
4620 drop_inode = 1;
4621 goto out_unlock;
4622 }
4623
Josef Bacik618e21d2007-07-11 10:18:17 -04004624 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04004625 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04004626 if (err)
4627 drop_inode = 1;
4628 else {
4629 inode->i_op = &btrfs_special_inode_operations;
4630 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04004631 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04004632 }
Josef Bacik618e21d2007-07-11 10:18:17 -04004633 btrfs_update_inode_block_group(trans, inode);
4634 btrfs_update_inode_block_group(trans, dir);
4635out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004636 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04004637 btrfs_end_transaction_throttle(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004638 btrfs_btree_balance_dirty(root, nr);
Josef Bacik618e21d2007-07-11 10:18:17 -04004639 if (drop_inode) {
4640 inode_dec_link_count(inode);
4641 iput(inode);
4642 }
Josef Bacik618e21d2007-07-11 10:18:17 -04004643 return err;
4644}
4645
Chris Mason39279cc2007-06-12 06:35:45 -04004646static int btrfs_create(struct inode *dir, struct dentry *dentry,
4647 int mode, struct nameidata *nd)
4648{
4649 struct btrfs_trans_handle *trans;
4650 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05004651 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04004652 int drop_inode = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004653 int err;
Chris Mason1832a6d2007-12-21 16:27:21 -05004654 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004655 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004656 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004657
Yan, Zhenga22285a2010-05-16 10:48:46 -04004658 err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
4659 if (err)
4660 return err;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004661 /*
4662 * 2 for inode item and ref
4663 * 2 for dir items
4664 * 1 for xattr if selinux is on
4665 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004666 trans = btrfs_start_transaction(root, 5);
4667 if (IS_ERR(trans))
4668 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004669
Chris Mason39279cc2007-06-12 06:35:45 -04004670 btrfs_set_trans_block_group(trans, dir);
4671
Josef Bacikaec74772008-07-24 12:12:38 -04004672 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05004673 dentry->d_name.len,
4674 dentry->d_parent->d_inode->i_ino,
Chris Mason00e4e6b2008-08-05 11:18:09 -04004675 objectid, BTRFS_I(dir)->block_group, mode,
4676 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04004677 err = PTR_ERR(inode);
4678 if (IS_ERR(inode))
4679 goto out_unlock;
4680
Yan, Zhengf34f57a2009-11-12 09:35:27 +00004681 err = btrfs_init_inode_security(trans, inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04004682 if (err) {
4683 drop_inode = 1;
4684 goto out_unlock;
4685 }
4686
Chris Mason39279cc2007-06-12 06:35:45 -04004687 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04004688 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004689 if (err)
4690 drop_inode = 1;
4691 else {
4692 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04004693 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04004694 inode->i_fop = &btrfs_file_operations;
4695 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05004696 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04004697 }
Chris Mason39279cc2007-06-12 06:35:45 -04004698 btrfs_update_inode_block_group(trans, inode);
4699 btrfs_update_inode_block_group(trans, dir);
4700out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004701 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04004702 btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004703 if (drop_inode) {
4704 inode_dec_link_count(inode);
4705 iput(inode);
4706 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004707 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004708 return err;
4709}
4710
4711static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
4712 struct dentry *dentry)
4713{
4714 struct btrfs_trans_handle *trans;
4715 struct btrfs_root *root = BTRFS_I(dir)->root;
4716 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004717 u64 index;
Chris Mason1832a6d2007-12-21 16:27:21 -05004718 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004719 int err;
4720 int drop_inode = 0;
4721
4722 if (inode->i_nlink == 0)
4723 return -ENOENT;
4724
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00004725 /* do not allow sys_link's with other subvols of the same device */
4726 if (root->objectid != BTRFS_I(inode)->root->objectid)
4727 return -EPERM;
4728
Josef Bacik9ed74f22009-09-11 16:12:44 -04004729 btrfs_inc_nlink(inode);
4730
Chris Mason3de45862008-11-17 21:02:50 -05004731 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004732 if (err)
4733 goto fail;
4734
Yan, Zhenga22285a2010-05-16 10:48:46 -04004735 /*
4736 * 1 item for inode ref
4737 * 2 items for dir items
4738 */
4739 trans = btrfs_start_transaction(root, 3);
4740 if (IS_ERR(trans)) {
4741 err = PTR_ERR(trans);
4742 goto fail;
4743 }
Chris Mason5f39d392007-10-15 16:14:19 -04004744
Chris Mason39279cc2007-06-12 06:35:45 -04004745 btrfs_set_trans_block_group(trans, dir);
4746 atomic_inc(&inode->i_count);
Josef Bacikaec74772008-07-24 12:12:38 -04004747
Chris Mason00e4e6b2008-08-05 11:18:09 -04004748 err = btrfs_add_nondir(trans, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04004749
Yan, Zhenga5719522009-09-24 09:17:31 -04004750 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04004751 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04004752 } else {
4753 btrfs_update_inode_block_group(trans, dir);
4754 err = btrfs_update_inode(trans, root, inode);
4755 BUG_ON(err);
4756 btrfs_log_new_name(trans, inode, NULL, dentry->d_parent);
4757 }
Chris Mason39279cc2007-06-12 06:35:45 -04004758
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004759 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04004760 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05004761fail:
Chris Mason39279cc2007-06-12 06:35:45 -04004762 if (drop_inode) {
4763 inode_dec_link_count(inode);
4764 iput(inode);
4765 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004766 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004767 return err;
4768}
4769
Chris Mason39279cc2007-06-12 06:35:45 -04004770static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
4771{
Chris Masonb9d86662008-05-02 16:13:49 -04004772 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04004773 struct btrfs_trans_handle *trans;
4774 struct btrfs_root *root = BTRFS_I(dir)->root;
4775 int err = 0;
4776 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04004777 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004778 u64 index = 0;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004779 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04004780
Yan, Zhenga22285a2010-05-16 10:48:46 -04004781 err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
4782 if (err)
4783 return err;
4784
Josef Bacik9ed74f22009-09-11 16:12:44 -04004785 /*
4786 * 2 items for inode and ref
4787 * 2 items for dir items
4788 * 1 for xattr if selinux is on
4789 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004790 trans = btrfs_start_transaction(root, 5);
4791 if (IS_ERR(trans))
4792 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004793 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -04004794
Josef Bacikaec74772008-07-24 12:12:38 -04004795 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05004796 dentry->d_name.len,
4797 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04004798 BTRFS_I(dir)->block_group, S_IFDIR | mode,
4799 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04004800 if (IS_ERR(inode)) {
4801 err = PTR_ERR(inode);
4802 goto out_fail;
4803 }
Chris Mason5f39d392007-10-15 16:14:19 -04004804
Chris Mason39279cc2007-06-12 06:35:45 -04004805 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04004806
Yan, Zhengf34f57a2009-11-12 09:35:27 +00004807 err = btrfs_init_inode_security(trans, inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04004808 if (err)
4809 goto out_fail;
4810
Chris Mason39279cc2007-06-12 06:35:45 -04004811 inode->i_op = &btrfs_dir_inode_operations;
4812 inode->i_fop = &btrfs_dir_file_operations;
4813 btrfs_set_trans_block_group(trans, inode);
4814
Chris Masondbe674a2008-07-17 12:54:05 -04004815 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004816 err = btrfs_update_inode(trans, root, inode);
4817 if (err)
4818 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04004819
Chris Masone02119d2008-09-05 16:13:11 -04004820 err = btrfs_add_link(trans, dentry->d_parent->d_inode,
4821 inode, dentry->d_name.name,
4822 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004823 if (err)
4824 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04004825
Chris Mason39279cc2007-06-12 06:35:45 -04004826 d_instantiate(dentry, inode);
4827 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004828 btrfs_update_inode_block_group(trans, inode);
4829 btrfs_update_inode_block_group(trans, dir);
4830
4831out_fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004832 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04004833 btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004834 if (drop_on_err)
4835 iput(inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004836 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004837 return err;
4838}
4839
Chris Masond352ac62008-09-29 15:18:18 -04004840/* helper for btfs_get_extent. Given an existing extent in the tree,
4841 * and an extent that you want to insert, deal with overlap and insert
4842 * the new extent into the tree.
4843 */
Chris Mason3b951512008-04-17 11:29:12 -04004844static int merge_extent_mapping(struct extent_map_tree *em_tree,
4845 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04004846 struct extent_map *em,
4847 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04004848{
4849 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04004850
Chris Masone6dcd2d2008-07-17 12:53:50 -04004851 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
4852 start_diff = map_start - em->start;
4853 em->start = map_start;
4854 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04004855 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
4856 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04004857 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04004858 em->block_len -= start_diff;
4859 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004860 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04004861}
4862
Chris Masonc8b97812008-10-29 14:49:59 -04004863static noinline int uncompress_inline(struct btrfs_path *path,
4864 struct inode *inode, struct page *page,
4865 size_t pg_offset, u64 extent_offset,
4866 struct btrfs_file_extent_item *item)
4867{
4868 int ret;
4869 struct extent_buffer *leaf = path->nodes[0];
4870 char *tmp;
4871 size_t max_size;
4872 unsigned long inline_size;
4873 unsigned long ptr;
4874
4875 WARN_ON(pg_offset != 0);
4876 max_size = btrfs_file_extent_ram_bytes(leaf, item);
4877 inline_size = btrfs_file_extent_inline_item_len(leaf,
4878 btrfs_item_nr(leaf, path->slots[0]));
4879 tmp = kmalloc(inline_size, GFP_NOFS);
4880 ptr = btrfs_file_extent_inline_start(item);
4881
4882 read_extent_buffer(leaf, tmp, ptr, inline_size);
4883
Chris Mason5b050f02008-11-11 09:34:41 -05004884 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04004885 ret = btrfs_zlib_decompress(tmp, page, extent_offset,
4886 inline_size, max_size);
4887 if (ret) {
4888 char *kaddr = kmap_atomic(page, KM_USER0);
4889 unsigned long copy_size = min_t(u64,
4890 PAGE_CACHE_SIZE - pg_offset,
4891 max_size - extent_offset);
4892 memset(kaddr + pg_offset, 0, copy_size);
4893 kunmap_atomic(kaddr, KM_USER0);
4894 }
4895 kfree(tmp);
4896 return 0;
4897}
4898
Chris Masond352ac62008-09-29 15:18:18 -04004899/*
4900 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05004901 * the ugly parts come from merging extents from the disk with the in-ram
4902 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04004903 * where the in-ram extents might be locked pending data=ordered completion.
4904 *
4905 * This also copies inline extents directly into the page.
4906 */
Chris Masond3977122009-01-05 21:25:51 -05004907
Chris Masona52d9a82007-08-27 16:49:44 -04004908struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05004909 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04004910 int create)
4911{
4912 int ret;
4913 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04004914 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04004915 u64 extent_start = 0;
4916 u64 extent_end = 0;
4917 u64 objectid = inode->i_ino;
4918 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04004919 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04004920 struct btrfs_root *root = BTRFS_I(inode)->root;
4921 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04004922 struct extent_buffer *leaf;
4923 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04004924 struct extent_map *em = NULL;
4925 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05004926 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04004927 struct btrfs_trans_handle *trans = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04004928 int compressed;
Chris Masona52d9a82007-08-27 16:49:44 -04004929
Chris Masona52d9a82007-08-27 16:49:44 -04004930again:
Chris Mason890871b2009-09-02 16:24:52 -04004931 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05004932 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04004933 if (em)
4934 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04004935 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05004936
Chris Masona52d9a82007-08-27 16:49:44 -04004937 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04004938 if (em->start > start || em->start + em->len <= start)
4939 free_extent_map(em);
4940 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05004941 free_extent_map(em);
4942 else
4943 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004944 }
Chris Masond1310b22008-01-24 16:13:08 -05004945 em = alloc_extent_map(GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04004946 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05004947 err = -ENOMEM;
4948 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004949 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004950 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05004951 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05004952 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05004953 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04004954 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04004955
4956 if (!path) {
4957 path = btrfs_alloc_path();
4958 BUG_ON(!path);
4959 }
4960
Chris Mason179e29e2007-11-01 11:28:41 -04004961 ret = btrfs_lookup_file_extent(trans, root, path,
4962 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04004963 if (ret < 0) {
4964 err = ret;
4965 goto out;
4966 }
4967
4968 if (ret != 0) {
4969 if (path->slots[0] == 0)
4970 goto not_found;
4971 path->slots[0]--;
4972 }
4973
Chris Mason5f39d392007-10-15 16:14:19 -04004974 leaf = path->nodes[0];
4975 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04004976 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04004977 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04004978 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4979 found_type = btrfs_key_type(&found_key);
4980 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04004981 found_type != BTRFS_EXTENT_DATA_KEY) {
4982 goto not_found;
4983 }
4984
Chris Mason5f39d392007-10-15 16:14:19 -04004985 found_type = btrfs_file_extent_type(leaf, item);
4986 extent_start = found_key.offset;
Chris Masonc8b97812008-10-29 14:49:59 -04004987 compressed = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04004988 if (found_type == BTRFS_FILE_EXTENT_REG ||
4989 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04004990 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04004991 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04004992 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
4993 size_t size;
4994 size = btrfs_file_extent_inline_len(leaf, item);
4995 extent_end = (extent_start + size + root->sectorsize - 1) &
4996 ~((u64)root->sectorsize - 1);
4997 }
4998
4999 if (start >= extent_end) {
5000 path->slots[0]++;
5001 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
5002 ret = btrfs_next_leaf(root, path);
5003 if (ret < 0) {
5004 err = ret;
5005 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005006 }
Yan Zheng9036c102008-10-30 14:19:41 -04005007 if (ret > 0)
5008 goto not_found;
5009 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04005010 }
Yan Zheng9036c102008-10-30 14:19:41 -04005011 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5012 if (found_key.objectid != objectid ||
5013 found_key.type != BTRFS_EXTENT_DATA_KEY)
5014 goto not_found;
5015 if (start + len <= found_key.offset)
5016 goto not_found;
5017 em->start = start;
5018 em->len = found_key.offset - start;
5019 goto not_found_em;
5020 }
5021
Yan Zhengd899e052008-10-30 14:25:28 -04005022 if (found_type == BTRFS_FILE_EXTENT_REG ||
5023 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04005024 em->start = extent_start;
5025 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05005026 em->orig_start = extent_start -
5027 btrfs_file_extent_offset(leaf, item);
Chris Masondb945352007-10-15 16:15:53 -04005028 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
5029 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04005030 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04005031 goto insert;
5032 }
Chris Masonc8b97812008-10-29 14:49:59 -04005033 if (compressed) {
5034 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
5035 em->block_start = bytenr;
5036 em->block_len = btrfs_file_extent_disk_num_bytes(leaf,
5037 item);
5038 } else {
5039 bytenr += btrfs_file_extent_offset(leaf, item);
5040 em->block_start = bytenr;
5041 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04005042 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
5043 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04005044 }
Chris Masona52d9a82007-08-27 16:49:44 -04005045 goto insert;
5046 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04005047 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04005048 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04005049 size_t size;
5050 size_t extent_offset;
5051 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04005052
Chris Masona52d9a82007-08-27 16:49:44 -04005053 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04005054 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04005055 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04005056 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04005057 goto out;
5058 }
5059
Yan Zheng9036c102008-10-30 14:19:41 -04005060 size = btrfs_file_extent_inline_len(leaf, item);
5061 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05005062 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04005063 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04005064 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05005065 em->len = (copy_size + root->sectorsize - 1) &
5066 ~((u64)root->sectorsize - 1);
Yan Zhengff5b7ee2008-11-10 07:34:43 -05005067 em->orig_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04005068 if (compressed)
5069 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Yan689f9342007-10-29 11:41:07 -04005070 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04005071 if (create == 0 && !PageUptodate(page)) {
Chris Masonc8b97812008-10-29 14:49:59 -04005072 if (btrfs_file_extent_compression(leaf, item) ==
5073 BTRFS_COMPRESS_ZLIB) {
5074 ret = uncompress_inline(path, inode, page,
5075 pg_offset,
5076 extent_offset, item);
5077 BUG_ON(ret);
5078 } else {
5079 map = kmap(page);
5080 read_extent_buffer(leaf, map + pg_offset, ptr,
5081 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04005082 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
5083 memset(map + pg_offset + copy_size, 0,
5084 PAGE_CACHE_SIZE - pg_offset -
5085 copy_size);
5086 }
Chris Masonc8b97812008-10-29 14:49:59 -04005087 kunmap(page);
5088 }
Chris Mason179e29e2007-11-01 11:28:41 -04005089 flush_dcache_page(page);
5090 } else if (create && PageUptodate(page)) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005091 WARN_ON(1);
Chris Mason179e29e2007-11-01 11:28:41 -04005092 if (!trans) {
5093 kunmap(page);
5094 free_extent_map(em);
5095 em = NULL;
5096 btrfs_release_path(root, path);
Chris Masonf9295742008-07-17 12:54:14 -04005097 trans = btrfs_join_transaction(root, 1);
Chris Mason179e29e2007-11-01 11:28:41 -04005098 goto again;
5099 }
Chris Masonc8b97812008-10-29 14:49:59 -04005100 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05005101 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04005102 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04005103 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04005104 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04005105 }
Chris Masond1310b22008-01-24 16:13:08 -05005106 set_extent_uptodate(io_tree, em->start,
5107 extent_map_end(em) - 1, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04005108 goto insert;
5109 } else {
Chris Masond3977122009-01-05 21:25:51 -05005110 printk(KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04005111 WARN_ON(1);
5112 }
5113not_found:
5114 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05005115 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04005116not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04005117 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04005118 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04005119insert:
5120 btrfs_release_path(root, path);
Chris Masond1310b22008-01-24 16:13:08 -05005121 if (em->start > start || extent_map_end(em) <= start) {
Chris Masond3977122009-01-05 21:25:51 -05005122 printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
5123 "[%llu %llu]\n", (unsigned long long)em->start,
5124 (unsigned long long)em->len,
5125 (unsigned long long)start,
5126 (unsigned long long)len);
Chris Masona52d9a82007-08-27 16:49:44 -04005127 err = -EIO;
5128 goto out;
5129 }
Chris Masond1310b22008-01-24 16:13:08 -05005130
5131 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04005132 write_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04005133 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04005134 /* it is possible that someone inserted the extent into the tree
5135 * while we had the lock dropped. It is also possible that
5136 * an overlapping map exists in the tree
5137 */
Chris Masona52d9a82007-08-27 16:49:44 -04005138 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04005139 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005140
5141 ret = 0;
5142
Chris Mason3b951512008-04-17 11:29:12 -04005143 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04005144 if (existing && (existing->start > start ||
5145 existing->start + existing->len <= start)) {
5146 free_extent_map(existing);
5147 existing = NULL;
5148 }
Chris Mason3b951512008-04-17 11:29:12 -04005149 if (!existing) {
5150 existing = lookup_extent_mapping(em_tree, em->start,
5151 em->len);
5152 if (existing) {
5153 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005154 em, start,
5155 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04005156 free_extent_map(existing);
5157 if (err) {
5158 free_extent_map(em);
5159 em = NULL;
5160 }
5161 } else {
5162 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04005163 free_extent_map(em);
5164 em = NULL;
5165 }
5166 } else {
5167 free_extent_map(em);
5168 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005169 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04005170 }
Chris Masona52d9a82007-08-27 16:49:44 -04005171 }
Chris Mason890871b2009-09-02 16:24:52 -04005172 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04005173out:
Chris Masonf4219502008-07-22 11:18:09 -04005174 if (path)
5175 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04005176 if (trans) {
5177 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05005178 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04005179 err = ret;
5180 }
Chris Masona52d9a82007-08-27 16:49:44 -04005181 if (err) {
5182 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04005183 return ERR_PTR(err);
5184 }
5185 return em;
5186}
5187
Josef Bacik4b46fce2010-05-23 11:00:55 -04005188static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
5189 u64 start, u64 len)
5190{
5191 struct btrfs_root *root = BTRFS_I(inode)->root;
5192 struct btrfs_trans_handle *trans;
5193 struct extent_map *em;
5194 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
5195 struct btrfs_key ins;
5196 u64 alloc_hint;
5197 int ret;
5198
5199 btrfs_drop_extent_cache(inode, start, start + len - 1, 0);
5200
5201 trans = btrfs_join_transaction(root, 0);
5202 if (!trans)
5203 return ERR_PTR(-ENOMEM);
5204
5205 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
5206
5207 alloc_hint = get_extent_allocation_hint(inode, start, len);
5208 ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0,
5209 alloc_hint, (u64)-1, &ins, 1);
5210 if (ret) {
5211 em = ERR_PTR(ret);
5212 goto out;
5213 }
5214
5215 em = alloc_extent_map(GFP_NOFS);
5216 if (!em) {
5217 em = ERR_PTR(-ENOMEM);
5218 goto out;
5219 }
5220
5221 em->start = start;
5222 em->orig_start = em->start;
5223 em->len = ins.offset;
5224
5225 em->block_start = ins.objectid;
5226 em->block_len = ins.offset;
5227 em->bdev = root->fs_info->fs_devices->latest_bdev;
5228 set_bit(EXTENT_FLAG_PINNED, &em->flags);
5229
5230 while (1) {
5231 write_lock(&em_tree->lock);
5232 ret = add_extent_mapping(em_tree, em);
5233 write_unlock(&em_tree->lock);
5234 if (ret != -EEXIST)
5235 break;
5236 btrfs_drop_extent_cache(inode, start, start + em->len - 1, 0);
5237 }
5238
5239 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
5240 ins.offset, ins.offset, 0);
5241 if (ret) {
5242 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
5243 em = ERR_PTR(ret);
5244 }
5245out:
5246 btrfs_end_transaction(trans, root);
5247 return em;
5248}
5249
5250static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
5251 struct buffer_head *bh_result, int create)
5252{
5253 struct extent_map *em;
5254 struct btrfs_root *root = BTRFS_I(inode)->root;
5255 u64 start = iblock << inode->i_blkbits;
5256 u64 len = bh_result->b_size;
5257
5258 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
5259 if (IS_ERR(em))
5260 return PTR_ERR(em);
5261
5262 /*
5263 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
5264 * io. INLINE is special, and we could probably kludge it in here, but
5265 * it's still buffered so for safety lets just fall back to the generic
5266 * buffered path.
5267 *
5268 * For COMPRESSED we _have_ to read the entire extent in so we can
5269 * decompress it, so there will be buffering required no matter what we
5270 * do, so go ahead and fallback to buffered.
5271 *
5272 * We return -ENOTBLK because thats what makes DIO go ahead and go back
5273 * to buffered IO. Don't blame me, this is the price we pay for using
5274 * the generic code.
5275 */
5276 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
5277 em->block_start == EXTENT_MAP_INLINE) {
5278 free_extent_map(em);
5279 return -ENOTBLK;
5280 }
5281
5282 /* Just a good old fashioned hole, return */
5283 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
5284 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
5285 free_extent_map(em);
5286 /* DIO will do one hole at a time, so just unlock a sector */
5287 unlock_extent(&BTRFS_I(inode)->io_tree, start,
5288 start + root->sectorsize - 1, GFP_NOFS);
5289 return 0;
5290 }
5291
5292 /*
5293 * We don't allocate a new extent in the following cases
5294 *
5295 * 1) The inode is marked as NODATACOW. In this case we'll just use the
5296 * existing extent.
5297 * 2) The extent is marked as PREALLOC. We're good to go here and can
5298 * just use the extent.
5299 *
5300 */
5301 if (!create)
5302 goto map;
5303
5304 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
5305 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
5306 em->block_start != EXTENT_MAP_HOLE)) {
5307 u64 block_start;
5308 int type;
5309 int ret;
5310
5311 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
5312 type = BTRFS_ORDERED_PREALLOC;
5313 else
5314 type = BTRFS_ORDERED_NOCOW;
5315 len = min(len, em->block_len - (start - em->start));
5316 block_start = em->block_start + (start - em->start);
5317 ret = btrfs_add_ordered_extent_dio(inode, start,
5318 start, len, len, type);
5319 if (ret) {
5320 free_extent_map(em);
5321 return ret;
5322 }
5323 } else {
5324 free_extent_map(em);
5325 em = btrfs_new_extent_direct(inode, start, len);
5326 if (IS_ERR(em))
5327 return PTR_ERR(em);
5328 len = min(len, em->block_len);
5329 }
Chris Mason4845e442010-05-25 20:56:50 -04005330 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, start + len - 1,
5331 EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DIRTY, 1,
5332 0, NULL, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005333map:
5334 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
5335 inode->i_blkbits;
5336 bh_result->b_size = em->len - (start - em->start);
5337 bh_result->b_bdev = em->bdev;
5338 set_buffer_mapped(bh_result);
5339 if (create && !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
5340 set_buffer_new(bh_result);
5341
5342 free_extent_map(em);
5343
5344 return 0;
5345}
5346
5347struct btrfs_dio_private {
5348 struct inode *inode;
5349 u64 logical_offset;
5350 u64 disk_bytenr;
5351 u64 bytes;
5352 u32 *csums;
5353 void *private;
5354};
5355
5356static void btrfs_endio_direct_read(struct bio *bio, int err)
5357{
5358 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
5359 struct bio_vec *bvec = bio->bi_io_vec;
5360 struct btrfs_dio_private *dip = bio->bi_private;
5361 struct inode *inode = dip->inode;
5362 struct btrfs_root *root = BTRFS_I(inode)->root;
5363 u64 start;
5364 u32 *private = dip->csums;
5365
5366 start = dip->logical_offset;
5367 do {
5368 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
5369 struct page *page = bvec->bv_page;
5370 char *kaddr;
5371 u32 csum = ~(u32)0;
5372 unsigned long flags;
5373
5374 local_irq_save(flags);
5375 kaddr = kmap_atomic(page, KM_IRQ0);
5376 csum = btrfs_csum_data(root, kaddr + bvec->bv_offset,
5377 csum, bvec->bv_len);
5378 btrfs_csum_final(csum, (char *)&csum);
5379 kunmap_atomic(kaddr, KM_IRQ0);
5380 local_irq_restore(flags);
5381
5382 flush_dcache_page(bvec->bv_page);
5383 if (csum != *private) {
5384 printk(KERN_ERR "btrfs csum failed ino %lu off"
5385 " %llu csum %u private %u\n",
5386 inode->i_ino, (unsigned long long)start,
5387 csum, *private);
5388 err = -EIO;
5389 }
5390 }
5391
5392 start += bvec->bv_len;
5393 private++;
5394 bvec++;
5395 } while (bvec <= bvec_end);
5396
5397 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
5398 dip->logical_offset + dip->bytes - 1, GFP_NOFS);
5399 bio->bi_private = dip->private;
5400
5401 kfree(dip->csums);
5402 kfree(dip);
5403 dio_end_io(bio, err);
5404}
5405
5406static void btrfs_endio_direct_write(struct bio *bio, int err)
5407{
5408 struct btrfs_dio_private *dip = bio->bi_private;
5409 struct inode *inode = dip->inode;
5410 struct btrfs_root *root = BTRFS_I(inode)->root;
5411 struct btrfs_trans_handle *trans;
5412 struct btrfs_ordered_extent *ordered = NULL;
5413 struct extent_state *cached_state = NULL;
5414 int ret;
5415
5416 if (err)
5417 goto out_done;
5418
5419 ret = btrfs_dec_test_ordered_pending(inode, &ordered,
5420 dip->logical_offset, dip->bytes);
5421 if (!ret)
5422 goto out_done;
5423
5424 BUG_ON(!ordered);
5425
5426 trans = btrfs_join_transaction(root, 1);
5427 if (!trans) {
5428 err = -ENOMEM;
5429 goto out;
5430 }
5431 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
5432
5433 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags)) {
5434 ret = btrfs_ordered_update_i_size(inode, 0, ordered);
5435 if (!ret)
5436 ret = btrfs_update_inode(trans, root, inode);
5437 err = ret;
5438 goto out;
5439 }
5440
5441 lock_extent_bits(&BTRFS_I(inode)->io_tree, ordered->file_offset,
5442 ordered->file_offset + ordered->len - 1, 0,
5443 &cached_state, GFP_NOFS);
5444
5445 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags)) {
5446 ret = btrfs_mark_extent_written(trans, inode,
5447 ordered->file_offset,
5448 ordered->file_offset +
5449 ordered->len);
5450 if (ret) {
5451 err = ret;
5452 goto out_unlock;
5453 }
5454 } else {
5455 ret = insert_reserved_file_extent(trans, inode,
5456 ordered->file_offset,
5457 ordered->start,
5458 ordered->disk_len,
5459 ordered->len,
5460 ordered->len,
5461 0, 0, 0,
5462 BTRFS_FILE_EXTENT_REG);
5463 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
5464 ordered->file_offset, ordered->len);
5465 if (ret) {
5466 err = ret;
5467 WARN_ON(1);
5468 goto out_unlock;
5469 }
5470 }
5471
5472 add_pending_csums(trans, inode, ordered->file_offset, &ordered->list);
5473 btrfs_ordered_update_i_size(inode, 0, ordered);
5474 btrfs_update_inode(trans, root, inode);
5475out_unlock:
5476 unlock_extent_cached(&BTRFS_I(inode)->io_tree, ordered->file_offset,
5477 ordered->file_offset + ordered->len - 1,
5478 &cached_state, GFP_NOFS);
5479out:
5480 btrfs_delalloc_release_metadata(inode, ordered->len);
5481 btrfs_end_transaction(trans, root);
5482 btrfs_put_ordered_extent(ordered);
5483 btrfs_put_ordered_extent(ordered);
5484out_done:
5485 bio->bi_private = dip->private;
5486
5487 kfree(dip->csums);
5488 kfree(dip);
5489 dio_end_io(bio, err);
5490}
5491
Chris Masoneaf25d92010-05-25 09:48:28 -04005492static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
5493 struct bio *bio, int mirror_num,
5494 unsigned long bio_flags, u64 offset)
5495{
5496 int ret;
5497 struct btrfs_root *root = BTRFS_I(inode)->root;
5498 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
5499 BUG_ON(ret);
5500 return 0;
5501}
5502
Josef Bacik4b46fce2010-05-23 11:00:55 -04005503static void btrfs_submit_direct(int rw, struct bio *bio, struct inode *inode,
5504 loff_t file_offset)
5505{
5506 struct btrfs_root *root = BTRFS_I(inode)->root;
5507 struct btrfs_dio_private *dip;
5508 struct bio_vec *bvec = bio->bi_io_vec;
5509 u64 start;
5510 int skip_sum;
5511 int write = rw & (1 << BIO_RW);
5512 int ret = 0;
5513
5514 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
5515
5516 dip = kmalloc(sizeof(*dip), GFP_NOFS);
5517 if (!dip) {
5518 ret = -ENOMEM;
5519 goto free_ordered;
5520 }
5521 dip->csums = NULL;
5522
5523 if (!skip_sum) {
5524 dip->csums = kmalloc(sizeof(u32) * bio->bi_vcnt, GFP_NOFS);
5525 if (!dip->csums) {
5526 ret = -ENOMEM;
5527 goto free_ordered;
5528 }
5529 }
5530
5531 dip->private = bio->bi_private;
5532 dip->inode = inode;
5533 dip->logical_offset = file_offset;
5534
5535 start = dip->logical_offset;
5536 dip->bytes = 0;
5537 do {
5538 dip->bytes += bvec->bv_len;
5539 bvec++;
5540 } while (bvec <= (bio->bi_io_vec + bio->bi_vcnt - 1));
5541
5542 dip->disk_bytenr = bio->bi_sector << 9;
5543 bio->bi_private = dip;
5544
5545 if (write)
5546 bio->bi_end_io = btrfs_endio_direct_write;
5547 else
5548 bio->bi_end_io = btrfs_endio_direct_read;
5549
5550 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
5551 if (ret)
5552 goto out_err;
5553
Chris Masoneaf25d92010-05-25 09:48:28 -04005554 if (write && !skip_sum) {
5555 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
5556 inode, rw, bio, 0, 0,
5557 dip->logical_offset,
5558 __btrfs_submit_bio_start_direct_io,
5559 __btrfs_submit_bio_done);
5560 if (ret)
5561 goto out_err;
5562 return;
5563 } else if (!skip_sum)
Josef Bacik4b46fce2010-05-23 11:00:55 -04005564 btrfs_lookup_bio_sums_dio(root, inode, bio,
5565 dip->logical_offset, dip->csums);
5566
Chris Masoneaf25d92010-05-25 09:48:28 -04005567 ret = btrfs_map_bio(root, rw, bio, 0, 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005568 if (ret)
5569 goto out_err;
5570 return;
5571out_err:
5572 kfree(dip->csums);
5573 kfree(dip);
5574free_ordered:
5575 /*
5576 * If this is a write, we need to clean up the reserved space and kill
5577 * the ordered extent.
5578 */
5579 if (write) {
5580 struct btrfs_ordered_extent *ordered;
5581 ordered = btrfs_lookup_ordered_extent(inode,
5582 dip->logical_offset);
5583 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
5584 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
5585 btrfs_free_reserved_extent(root, ordered->start,
5586 ordered->disk_len);
5587 btrfs_put_ordered_extent(ordered);
5588 btrfs_put_ordered_extent(ordered);
5589 }
5590 bio_endio(bio, ret);
5591}
5592
Chris Mason16432982008-04-10 10:23:21 -04005593static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
5594 const struct iovec *iov, loff_t offset,
5595 unsigned long nr_segs)
5596{
Josef Bacik4b46fce2010-05-23 11:00:55 -04005597 struct file *file = iocb->ki_filp;
5598 struct inode *inode = file->f_mapping->host;
5599 struct btrfs_ordered_extent *ordered;
Chris Mason4845e442010-05-25 20:56:50 -04005600 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005601 u64 lockstart, lockend;
5602 ssize_t ret;
Chris Mason4845e442010-05-25 20:56:50 -04005603 int writing = rw & WRITE;
5604 int write_bits = 0;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005605
5606 lockstart = offset;
5607 lockend = offset + iov_length(iov, nr_segs) - 1;
Chris Mason4845e442010-05-25 20:56:50 -04005608
Josef Bacik4b46fce2010-05-23 11:00:55 -04005609 while (1) {
Chris Mason4845e442010-05-25 20:56:50 -04005610 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
5611 0, &cached_state, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005612 /*
5613 * We're concerned with the entire range that we're going to be
5614 * doing DIO to, so we need to make sure theres no ordered
5615 * extents in this range.
5616 */
5617 ordered = btrfs_lookup_ordered_range(inode, lockstart,
5618 lockend - lockstart + 1);
5619 if (!ordered)
5620 break;
Chris Mason4845e442010-05-25 20:56:50 -04005621 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
5622 &cached_state, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005623 btrfs_start_ordered_extent(inode, ordered, 1);
5624 btrfs_put_ordered_extent(ordered);
5625 cond_resched();
5626 }
5627
Chris Mason4845e442010-05-25 20:56:50 -04005628 /*
5629 * we don't use btrfs_set_extent_delalloc because we don't want
5630 * the dirty or uptodate bits
5631 */
5632 if (writing) {
5633 write_bits = EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING;
5634 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
5635 EXTENT_DELALLOC, 0, NULL, &cached_state,
5636 GFP_NOFS);
5637 if (ret) {
5638 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
5639 lockend, EXTENT_LOCKED | write_bits,
5640 1, 0, &cached_state, GFP_NOFS);
5641 goto out;
5642 }
5643 }
5644
5645 free_extent_state(cached_state);
5646 cached_state = NULL;
5647
Josef Bacik4b46fce2010-05-23 11:00:55 -04005648 ret = __blockdev_direct_IO(rw, iocb, inode, NULL, iov, offset, nr_segs,
5649 btrfs_get_blocks_direct, NULL,
5650 btrfs_submit_direct, 0);
5651
5652 if (ret < 0 && ret != -EIOCBQUEUED) {
Chris Mason4845e442010-05-25 20:56:50 -04005653 clear_extent_bit(&BTRFS_I(inode)->io_tree, offset,
5654 offset + iov_length(iov, nr_segs) - 1,
5655 EXTENT_LOCKED | write_bits, 1, 0,
5656 &cached_state, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005657 } else if (ret >= 0 && ret < iov_length(iov, nr_segs)) {
5658 /*
5659 * We're falling back to buffered, unlock the section we didn't
5660 * do IO on.
5661 */
Chris Mason4845e442010-05-25 20:56:50 -04005662 clear_extent_bit(&BTRFS_I(inode)->io_tree, offset + ret,
5663 offset + iov_length(iov, nr_segs) - 1,
5664 EXTENT_LOCKED | write_bits, 1, 0,
5665 &cached_state, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005666 }
Chris Mason4845e442010-05-25 20:56:50 -04005667out:
5668 free_extent_state(cached_state);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005669 return ret;
Chris Mason16432982008-04-10 10:23:21 -04005670}
5671
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05005672static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
5673 __u64 start, __u64 len)
5674{
5675 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent);
5676}
5677
Chris Mason9ebefb182007-06-15 13:50:00 -04005678int btrfs_readpage(struct file *file, struct page *page)
5679{
Chris Masond1310b22008-01-24 16:13:08 -05005680 struct extent_io_tree *tree;
5681 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04005682 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04005683}
Chris Mason1832a6d2007-12-21 16:27:21 -05005684
Chris Mason39279cc2007-06-12 06:35:45 -04005685static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
5686{
Chris Masond1310b22008-01-24 16:13:08 -05005687 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04005688
5689
5690 if (current->flags & PF_MEMALLOC) {
5691 redirty_page_for_writepage(wbc, page);
5692 unlock_page(page);
5693 return 0;
5694 }
Chris Masond1310b22008-01-24 16:13:08 -05005695 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04005696 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
5697}
Chris Mason39279cc2007-06-12 06:35:45 -04005698
Chris Masonf4219502008-07-22 11:18:09 -04005699int btrfs_writepages(struct address_space *mapping,
5700 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04005701{
Chris Masond1310b22008-01-24 16:13:08 -05005702 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05005703
Chris Masond1310b22008-01-24 16:13:08 -05005704 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04005705 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
5706}
5707
Chris Mason3ab2fb52007-11-08 10:59:22 -05005708static int
5709btrfs_readpages(struct file *file, struct address_space *mapping,
5710 struct list_head *pages, unsigned nr_pages)
5711{
Chris Masond1310b22008-01-24 16:13:08 -05005712 struct extent_io_tree *tree;
5713 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05005714 return extent_readpages(tree, mapping, pages, nr_pages,
5715 btrfs_get_extent);
5716}
Chris Masone6dcd2d2008-07-17 12:53:50 -04005717static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04005718{
Chris Masond1310b22008-01-24 16:13:08 -05005719 struct extent_io_tree *tree;
5720 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04005721 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005722
Chris Masond1310b22008-01-24 16:13:08 -05005723 tree = &BTRFS_I(page->mapping->host)->io_tree;
5724 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05005725 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04005726 if (ret == 1) {
5727 ClearPagePrivate(page);
5728 set_page_private(page, 0);
5729 page_cache_release(page);
5730 }
5731 return ret;
5732}
Chris Mason39279cc2007-06-12 06:35:45 -04005733
Chris Masone6dcd2d2008-07-17 12:53:50 -04005734static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
5735{
Chris Mason98509cf2008-09-11 15:51:43 -04005736 if (PageWriteback(page) || PageDirty(page))
5737 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05005738 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005739}
5740
Chris Masona52d9a82007-08-27 16:49:44 -04005741static void btrfs_invalidatepage(struct page *page, unsigned long offset)
5742{
Chris Masond1310b22008-01-24 16:13:08 -05005743 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005744 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00005745 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005746 u64 page_start = page_offset(page);
5747 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04005748
Chris Mason8b62b722009-09-02 16:53:46 -04005749
5750 /*
5751 * we have the page locked, so new writeback can't start,
5752 * and the dirty bit won't be cleared while we are here.
5753 *
5754 * Wait for IO on this page so that we can safely clear
5755 * the PagePrivate2 bit and do ordered accounting
5756 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04005757 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04005758
Chris Masond1310b22008-01-24 16:13:08 -05005759 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005760 if (offset) {
5761 btrfs_releasepage(page, GFP_NOFS);
5762 return;
5763 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00005764 lock_extent_bits(tree, page_start, page_end, 0, &cached_state,
5765 GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005766 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
5767 page_offset(page));
5768 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04005769 /*
5770 * IO on this page will never be started, so we need
5771 * to account for any ordered extents now
5772 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04005773 clear_extent_bit(tree, page_start, page_end,
5774 EXTENT_DIRTY | EXTENT_DELALLOC |
Josef Bacik32c00af2009-10-08 13:34:05 -04005775 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING, 1, 0,
Josef Bacik2ac55d42010-02-03 19:33:23 +00005776 &cached_state, GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04005777 /*
5778 * whoever cleared the private bit is responsible
5779 * for the finish_ordered_io
5780 */
5781 if (TestClearPagePrivate2(page)) {
5782 btrfs_finish_ordered_io(page->mapping->host,
5783 page_start, page_end);
5784 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005785 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +00005786 cached_state = NULL;
5787 lock_extent_bits(tree, page_start, page_end, 0, &cached_state,
5788 GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005789 }
5790 clear_extent_bit(tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04005791 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
Josef Bacik2ac55d42010-02-03 19:33:23 +00005792 EXTENT_DO_ACCOUNTING, 1, 1, &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005793 __btrfs_releasepage(page, GFP_NOFS);
5794
Chris Mason4a096752008-07-21 10:29:44 -04005795 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04005796 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04005797 ClearPagePrivate(page);
5798 set_page_private(page, 0);
5799 page_cache_release(page);
5800 }
Chris Mason39279cc2007-06-12 06:35:45 -04005801}
5802
Chris Mason9ebefb182007-06-15 13:50:00 -04005803/*
5804 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
5805 * called from a page fault handler when a page is first dirtied. Hence we must
5806 * be careful to check for EOF conditions here. We set the page up correctly
5807 * for a written page which means we get ENOSPC checking when writing into
5808 * holes and correct delalloc and unwritten extent mapping on filesystems that
5809 * support these features.
5810 *
5811 * We are not allowed to take the i_mutex here so we have to play games to
5812 * protect against truncate races as the page could now be beyond EOF. Because
5813 * vmtruncate() writes the inode size before removing pages, once we have the
5814 * page lock we can determine safely if the page is beyond EOF. If it is not
5815 * beyond EOF, then the page is guaranteed safe against truncation until we
5816 * unlock the page.
5817 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07005818int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04005819{
Nick Pigginc2ec1752009-03-31 15:23:21 -07005820 struct page *page = vmf->page;
Chris Mason6da6aba2007-12-18 16:15:09 -05005821 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05005822 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005823 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5824 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00005825 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005826 char *kaddr;
5827 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04005828 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05005829 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04005830 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005831 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04005832
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005833 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Nick Piggin56a76f82009-03-31 15:23:23 -07005834 if (ret) {
5835 if (ret == -ENOMEM)
5836 ret = VM_FAULT_OOM;
5837 else /* -ENOSPC, -EIO, etc */
5838 ret = VM_FAULT_SIGBUS;
Chris Mason1832a6d2007-12-21 16:27:21 -05005839 goto out;
Nick Piggin56a76f82009-03-31 15:23:23 -07005840 }
Chris Mason1832a6d2007-12-21 16:27:21 -05005841
Nick Piggin56a76f82009-03-31 15:23:23 -07005842 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04005843again:
Chris Mason9ebefb182007-06-15 13:50:00 -04005844 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04005845 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005846 page_start = page_offset(page);
5847 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04005848
Chris Mason9ebefb182007-06-15 13:50:00 -04005849 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04005850 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04005851 /* page got truncated out from underneath us */
5852 goto out_unlock;
5853 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005854 wait_on_page_writeback(page);
5855
Josef Bacik2ac55d42010-02-03 19:33:23 +00005856 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state,
5857 GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005858 set_page_extent_mapped(page);
5859
Chris Masoneb84ae02008-07-17 13:53:27 -04005860 /*
5861 * we can't set the delalloc bits if there are pending ordered
5862 * extents. Drop our locks and wait for them to finish
5863 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04005864 ordered = btrfs_lookup_ordered_extent(inode, page_start);
5865 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00005866 unlock_extent_cached(io_tree, page_start, page_end,
5867 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005868 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04005869 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005870 btrfs_put_ordered_extent(ordered);
5871 goto again;
5872 }
5873
Josef Bacikfbf19082009-10-01 17:10:23 -04005874 /*
5875 * XXX - page_mkwrite gets called every time the page is dirtied, even
5876 * if it was already dirty, so for space accounting reasons we need to
5877 * clear any delalloc bits for the range we are fixing to save. There
5878 * is probably a better way to do this, but for now keep consistent with
5879 * prepare_pages in the normal write path.
5880 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00005881 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04005882 EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00005883 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04005884
Josef Bacik2ac55d42010-02-03 19:33:23 +00005885 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
5886 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005887 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00005888 unlock_extent_cached(io_tree, page_start, page_end,
5889 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005890 ret = VM_FAULT_SIGBUS;
5891 goto out_unlock;
5892 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005893 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04005894
5895 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04005896 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04005897 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04005898 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04005899 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04005900
Chris Masone6dcd2d2008-07-17 12:53:50 -04005901 if (zero_start != PAGE_CACHE_SIZE) {
5902 kaddr = kmap(page);
5903 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
5904 flush_dcache_page(page);
5905 kunmap(page);
5906 }
Chris Mason247e7432008-07-17 12:53:51 -04005907 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005908 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04005909 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04005910
Chris Mason257c62e2009-10-13 13:21:08 -04005911 BTRFS_I(inode)->last_trans = root->fs_info->generation;
5912 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
5913
Josef Bacik2ac55d42010-02-03 19:33:23 +00005914 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04005915
5916out_unlock:
Chris Mason50a9b212009-09-11 12:33:12 -04005917 if (!ret)
5918 return VM_FAULT_LOCKED;
Chris Mason9ebefb182007-06-15 13:50:00 -04005919 unlock_page(page);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005920 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason1832a6d2007-12-21 16:27:21 -05005921out:
Chris Mason9ebefb182007-06-15 13:50:00 -04005922 return ret;
5923}
5924
Chris Mason39279cc2007-06-12 06:35:45 -04005925static void btrfs_truncate(struct inode *inode)
5926{
5927 struct btrfs_root *root = BTRFS_I(inode)->root;
5928 int ret;
5929 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04005930 unsigned long nr;
Chris Masondbe674a2008-07-17 12:54:05 -04005931 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04005932
Yan, Zheng80825102009-11-12 09:35:36 +00005933 if (!S_ISREG(inode->i_mode)) {
5934 WARN_ON(1);
Chris Mason39279cc2007-06-12 06:35:45 -04005935 return;
Yan, Zheng80825102009-11-12 09:35:36 +00005936 }
Chris Mason39279cc2007-06-12 06:35:45 -04005937
Josef Bacik5d5e1032009-10-13 16:46:49 -04005938 ret = btrfs_truncate_page(inode->i_mapping, inode->i_size);
5939 if (ret)
5940 return;
Yan, Zheng80825102009-11-12 09:35:36 +00005941
Chris Mason4a096752008-07-21 10:29:44 -04005942 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Yan, Zheng80825102009-11-12 09:35:36 +00005943 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005944
Yan, Zhengd68fc572010-05-16 10:49:58 -04005945 trans = btrfs_start_transaction(root, 0);
5946 BUG_ON(IS_ERR(trans));
Yan, Zheng80825102009-11-12 09:35:36 +00005947 btrfs_set_trans_block_group(trans, inode);
Yan, Zhengd68fc572010-05-16 10:49:58 -04005948 trans->block_rsv = root->orphan_block_rsv;
Chris Mason5a3f23d2009-03-31 13:27:11 -04005949
5950 /*
5951 * setattr is responsible for setting the ordered_data_close flag,
5952 * but that is only tested during the last file release. That
5953 * could happen well after the next commit, leaving a great big
5954 * window where new writes may get lost if someone chooses to write
5955 * to this file after truncating to zero
5956 *
5957 * The inode doesn't have any dirty data here, and so if we commit
5958 * this is a noop. If someone immediately starts writing to the inode
5959 * it is very likely we'll catch some of their writes in this
5960 * transaction, and the commit will find this file on the ordered
5961 * data list with good things to send down.
5962 *
5963 * This is a best effort solution, there is still a window where
5964 * using truncate to replace the contents of the file will
5965 * end up with a zero length file after a crash.
5966 */
5967 if (inode->i_size == 0 && BTRFS_I(inode)->ordered_data_close)
5968 btrfs_add_ordered_operation(trans, root, inode);
5969
Yan, Zheng80825102009-11-12 09:35:36 +00005970 while (1) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04005971 if (!trans) {
5972 trans = btrfs_start_transaction(root, 0);
5973 BUG_ON(IS_ERR(trans));
5974 btrfs_set_trans_block_group(trans, inode);
5975 trans->block_rsv = root->orphan_block_rsv;
5976 }
5977
5978 ret = btrfs_block_rsv_check(trans, root,
5979 root->orphan_block_rsv, 0, 5);
5980 if (ret) {
5981 BUG_ON(ret != -EAGAIN);
5982 ret = btrfs_commit_transaction(trans, root);
5983 BUG_ON(ret);
5984 trans = NULL;
5985 continue;
5986 }
5987
Yan, Zheng80825102009-11-12 09:35:36 +00005988 ret = btrfs_truncate_inode_items(trans, root, inode,
5989 inode->i_size,
5990 BTRFS_EXTENT_DATA_KEY);
5991 if (ret != -EAGAIN)
5992 break;
Chris Mason39279cc2007-06-12 06:35:45 -04005993
Yan, Zheng80825102009-11-12 09:35:36 +00005994 ret = btrfs_update_inode(trans, root, inode);
5995 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005996
Yan, Zheng80825102009-11-12 09:35:36 +00005997 nr = trans->blocks_used;
5998 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04005999 trans = NULL;
Yan, Zheng80825102009-11-12 09:35:36 +00006000 btrfs_btree_balance_dirty(root, nr);
Yan, Zheng80825102009-11-12 09:35:36 +00006001 }
6002
6003 if (ret == 0 && inode->i_nlink > 0) {
6004 ret = btrfs_orphan_del(trans, inode);
6005 BUG_ON(ret);
6006 }
6007
6008 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04006009 BUG_ON(ret);
6010
Josef Bacik7b128762008-07-24 12:17:14 -04006011 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04006012 ret = btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04006013 BUG_ON(ret);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04006014 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04006015}
6016
Sven Wegener3b963622008-06-09 21:57:42 -04006017/*
Chris Masond352ac62008-09-29 15:18:18 -04006018 * create a new subvolume directory/inode (helper for the ioctl).
6019 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05006020int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Yan, Zheng76dda932009-09-21 16:00:26 -04006021 struct btrfs_root *new_root,
Yan Zhengd2fb3432008-12-11 16:30:39 -05006022 u64 new_dirid, u64 alloc_hint)
Chris Mason39279cc2007-06-12 06:35:45 -04006023{
Chris Mason39279cc2007-06-12 06:35:45 -04006024 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04006025 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006026 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006027
Josef Bacikaec74772008-07-24 12:12:38 -04006028 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
Yan Zhengd2fb3432008-12-11 16:30:39 -05006029 new_dirid, alloc_hint, S_IFDIR | 0700, &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04006030 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04006031 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006032 inode->i_op = &btrfs_dir_inode_operations;
6033 inode->i_fop = &btrfs_dir_file_operations;
6034
Chris Mason39279cc2007-06-12 06:35:45 -04006035 inode->i_nlink = 1;
Chris Masondbe674a2008-07-17 12:54:05 -04006036 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04006037
Yan, Zheng76dda932009-09-21 16:00:26 -04006038 err = btrfs_update_inode(trans, new_root, inode);
6039 BUG_ON(err);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04006040
Yan, Zheng76dda932009-09-21 16:00:26 -04006041 iput(inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04006042 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006043}
6044
Chris Masond352ac62008-09-29 15:18:18 -04006045/* helper function for file defrag and space balancing. This
6046 * forces readahead on a given range of bytes in an inode
6047 */
Chris Masonedbd8d42007-12-21 16:27:24 -05006048unsigned long btrfs_force_ra(struct address_space *mapping,
Chris Mason86479a02007-09-10 19:58:16 -04006049 struct file_ra_state *ra, struct file *file,
6050 pgoff_t offset, pgoff_t last_index)
6051{
Chris Mason8e7bf942008-04-28 09:02:36 -04006052 pgoff_t req_size = last_index - offset + 1;
Chris Mason86479a02007-09-10 19:58:16 -04006053
Chris Mason86479a02007-09-10 19:58:16 -04006054 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
6055 return offset + req_size;
Chris Mason86479a02007-09-10 19:58:16 -04006056}
6057
Chris Mason39279cc2007-06-12 06:35:45 -04006058struct inode *btrfs_alloc_inode(struct super_block *sb)
6059{
6060 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006061 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006062
6063 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
6064 if (!ei)
6065 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006066
6067 ei->root = NULL;
6068 ei->space_info = NULL;
6069 ei->generation = 0;
6070 ei->sequence = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04006071 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04006072 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04006073 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006074 ei->delalloc_bytes = 0;
6075 ei->reserved_bytes = 0;
6076 ei->disk_i_size = 0;
6077 ei->flags = 0;
6078 ei->index_cnt = (u64)-1;
6079 ei->last_unlink_trans = 0;
6080
6081 spin_lock_init(&ei->accounting_lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006082 atomic_set(&ei->outstanding_extents, 0);
Josef Bacik32c00af2009-10-08 13:34:05 -04006083 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006084
6085 ei->ordered_data_close = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04006086 ei->orphan_meta_reserved = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006087 ei->dummy_inode = 0;
6088 ei->force_compress = 0;
6089
6090 inode = &ei->vfs_inode;
6091 extent_map_tree_init(&ei->extent_tree, GFP_NOFS);
6092 extent_io_tree_init(&ei->io_tree, &inode->i_data, GFP_NOFS);
6093 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data, GFP_NOFS);
6094 mutex_init(&ei->log_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006095 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Josef Bacik7b128762008-07-24 12:17:14 -04006096 INIT_LIST_HEAD(&ei->i_orphan);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006097 INIT_LIST_HEAD(&ei->delalloc_inodes);
Chris Mason5a3f23d2009-03-31 13:27:11 -04006098 INIT_LIST_HEAD(&ei->ordered_operations);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006099 RB_CLEAR_NODE(&ei->rb_node);
6100
6101 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006102}
6103
6104void btrfs_destroy_inode(struct inode *inode)
6105{
Chris Masone6dcd2d2008-07-17 12:53:50 -04006106 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04006107 struct btrfs_root *root = BTRFS_I(inode)->root;
6108
Chris Mason39279cc2007-06-12 06:35:45 -04006109 WARN_ON(!list_empty(&inode->i_dentry));
6110 WARN_ON(inode->i_data.nrpages);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006111 WARN_ON(atomic_read(&BTRFS_I(inode)->outstanding_extents));
6112 WARN_ON(BTRFS_I(inode)->reserved_extents);
Chris Mason39279cc2007-06-12 06:35:45 -04006113
Chris Mason5a3f23d2009-03-31 13:27:11 -04006114 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05006115 * This can happen where we create an inode, but somebody else also
6116 * created the same inode and we need to destroy the one we already
6117 * created.
6118 */
6119 if (!root)
6120 goto free;
6121
6122 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04006123 * Make sure we're properly removed from the ordered operation
6124 * lists.
6125 */
6126 smp_mb();
6127 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
6128 spin_lock(&root->fs_info->ordered_extent_lock);
6129 list_del_init(&BTRFS_I(inode)->ordered_operations);
6130 spin_unlock(&root->fs_info->ordered_extent_lock);
6131 }
6132
Yan, Zhengd68fc572010-05-16 10:49:58 -04006133 spin_lock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04006134 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
Yan, Zheng80825102009-11-12 09:35:36 +00006135 printk(KERN_INFO "BTRFS: inode %lu still on the orphan list\n",
6136 inode->i_ino);
6137 list_del_init(&BTRFS_I(inode)->i_orphan);
Josef Bacik7b128762008-07-24 12:17:14 -04006138 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04006139 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04006140
Chris Masond3977122009-01-05 21:25:51 -05006141 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006142 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
6143 if (!ordered)
6144 break;
6145 else {
Chris Masond3977122009-01-05 21:25:51 -05006146 printk(KERN_ERR "btrfs found ordered "
6147 "extent %llu %llu on inode cleanup\n",
6148 (unsigned long long)ordered->file_offset,
6149 (unsigned long long)ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006150 btrfs_remove_ordered_extent(inode, ordered);
6151 btrfs_put_ordered_extent(ordered);
6152 btrfs_put_ordered_extent(ordered);
6153 }
6154 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006155 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04006156 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05006157free:
Chris Mason39279cc2007-06-12 06:35:45 -04006158 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
6159}
6160
Yan, Zheng76dda932009-09-21 16:00:26 -04006161void btrfs_drop_inode(struct inode *inode)
6162{
6163 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04006164 if (inode->i_nlink > 0 && btrfs_root_refs(&root->root_item) == 0)
6165 generic_delete_inode(inode);
6166 else
6167 generic_drop_inode(inode);
6168}
6169
Sven Wegener0ee0fda2008-07-30 16:54:26 -04006170static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04006171{
6172 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
6173
6174 inode_init_once(&ei->vfs_inode);
6175}
6176
6177void btrfs_destroy_cachep(void)
6178{
6179 if (btrfs_inode_cachep)
6180 kmem_cache_destroy(btrfs_inode_cachep);
6181 if (btrfs_trans_handle_cachep)
6182 kmem_cache_destroy(btrfs_trans_handle_cachep);
6183 if (btrfs_transaction_cachep)
6184 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04006185 if (btrfs_path_cachep)
6186 kmem_cache_destroy(btrfs_path_cachep);
6187}
6188
6189int btrfs_init_cachep(void)
6190{
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02006191 btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
6192 sizeof(struct btrfs_inode), 0,
6193 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04006194 if (!btrfs_inode_cachep)
6195 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02006196
6197 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
6198 sizeof(struct btrfs_trans_handle), 0,
6199 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04006200 if (!btrfs_trans_handle_cachep)
6201 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02006202
6203 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
6204 sizeof(struct btrfs_transaction), 0,
6205 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04006206 if (!btrfs_transaction_cachep)
6207 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02006208
6209 btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
6210 sizeof(struct btrfs_path), 0,
6211 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04006212 if (!btrfs_path_cachep)
6213 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02006214
Chris Mason39279cc2007-06-12 06:35:45 -04006215 return 0;
6216fail:
6217 btrfs_destroy_cachep();
6218 return -ENOMEM;
6219}
6220
6221static int btrfs_getattr(struct vfsmount *mnt,
6222 struct dentry *dentry, struct kstat *stat)
6223{
6224 struct inode *inode = dentry->d_inode;
6225 generic_fillattr(inode, stat);
Chris Mason3394e162008-11-17 20:42:26 -05006226 stat->dev = BTRFS_I(inode)->root->anon_super.s_dev;
Chris Masond6667462008-01-03 14:51:00 -05006227 stat->blksize = PAGE_CACHE_SIZE;
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006228 stat->blocks = (inode_get_bytes(inode) +
6229 BTRFS_I(inode)->delalloc_bytes) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04006230 return 0;
6231}
6232
Chris Masond3977122009-01-05 21:25:51 -05006233static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
6234 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04006235{
6236 struct btrfs_trans_handle *trans;
6237 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006238 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04006239 struct inode *new_inode = new_dentry->d_inode;
6240 struct inode *old_inode = old_dentry->d_inode;
6241 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006242 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006243 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04006244 int ret;
6245
Yan, Zhengf679a842009-09-24 09:17:31 -04006246 if (new_dir->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
6247 return -EPERM;
6248
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006249 /* we only allow rename subvolume link between subvolumes */
6250 if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05006251 return -EXDEV;
6252
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006253 if (old_inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
6254 (new_inode && new_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID))
6255 return -ENOTEMPTY;
6256
Chris Mason39279cc2007-06-12 06:35:45 -04006257 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006258 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04006259 return -ENOTEMPTY;
Chris Mason5a3f23d2009-03-31 13:27:11 -04006260 /*
6261 * we're using rename to replace one file with another.
6262 * and the replacement file is large. Start IO on it now so
6263 * we don't add too much work to the end of the transaction
6264 */
Bartlomiej Zolnierkiewicz4baf8c92009-08-07 13:47:08 -04006265 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
Chris Mason5a3f23d2009-03-31 13:27:11 -04006266 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
6267 filemap_flush(old_inode->i_mapping);
6268
Yan, Zheng76dda932009-09-21 16:00:26 -04006269 /* close the racy window with snapshot create/destroy ioctl */
6270 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
6271 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006272 /*
6273 * We want to reserve the absolute worst case amount of items. So if
6274 * both inodes are subvols and we need to unlink them then that would
6275 * require 4 item modifications, but if they are both normal inodes it
6276 * would require 5 item modifications, so we'll assume their normal
6277 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
6278 * should cover the worst case number of items we'll modify.
6279 */
6280 trans = btrfs_start_transaction(root, 20);
6281 if (IS_ERR(trans))
6282 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04006283
Yan, Zhenga5719522009-09-24 09:17:31 -04006284 btrfs_set_trans_block_group(trans, new_dir);
Chris Mason39279cc2007-06-12 06:35:45 -04006285
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006286 if (dest != root)
6287 btrfs_record_root_in_trans(trans, dest);
6288
Yan, Zhenga5719522009-09-24 09:17:31 -04006289 ret = btrfs_set_inode_index(new_dir, &index);
6290 if (ret)
6291 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04006292
Yan, Zhenga5719522009-09-24 09:17:31 -04006293 if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006294 /* force full log commit if subvolume involved. */
6295 root->fs_info->last_trans_log_full_commit = trans->transid;
6296 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04006297 ret = btrfs_insert_inode_ref(trans, dest,
6298 new_dentry->d_name.name,
6299 new_dentry->d_name.len,
6300 old_inode->i_ino,
6301 new_dir->i_ino, index);
6302 if (ret)
6303 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006304 /*
6305 * this is an ugly little race, but the rename is required
6306 * to make sure that if we crash, the inode is either at the
6307 * old name or the new one. pinning the log transaction lets
6308 * us make sure we don't allow a log commit to come in after
6309 * we unlink the name but before we add the new name back in.
6310 */
6311 btrfs_pin_log_trans(root);
6312 }
Chris Mason12fcfd22009-03-24 10:24:20 -04006313 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04006314 * make sure the inode gets flushed if it is replacing
6315 * something.
6316 */
6317 if (new_inode && new_inode->i_size &&
6318 old_inode && S_ISREG(old_inode->i_mode)) {
6319 btrfs_add_ordered_operation(trans, root, old_inode);
6320 }
6321
Chris Mason39279cc2007-06-12 06:35:45 -04006322 old_dir->i_ctime = old_dir->i_mtime = ctime;
6323 new_dir->i_ctime = new_dir->i_mtime = ctime;
6324 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04006325
Chris Mason12fcfd22009-03-24 10:24:20 -04006326 if (old_dentry->d_parent != new_dentry->d_parent)
6327 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
6328
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006329 if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
6330 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
6331 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
6332 old_dentry->d_name.name,
6333 old_dentry->d_name.len);
6334 } else {
6335 btrfs_inc_nlink(old_dentry->d_inode);
6336 ret = btrfs_unlink_inode(trans, root, old_dir,
6337 old_dentry->d_inode,
6338 old_dentry->d_name.name,
6339 old_dentry->d_name.len);
6340 }
6341 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006342
6343 if (new_inode) {
6344 new_inode->i_ctime = CURRENT_TIME;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006345 if (unlikely(new_inode->i_ino ==
6346 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
6347 root_objectid = BTRFS_I(new_inode)->location.objectid;
6348 ret = btrfs_unlink_subvol(trans, dest, new_dir,
6349 root_objectid,
6350 new_dentry->d_name.name,
6351 new_dentry->d_name.len);
6352 BUG_ON(new_inode->i_nlink == 0);
6353 } else {
6354 ret = btrfs_unlink_inode(trans, dest, new_dir,
6355 new_dentry->d_inode,
6356 new_dentry->d_name.name,
6357 new_dentry->d_name.len);
6358 }
6359 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04006360 if (new_inode->i_nlink == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04006361 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006362 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04006363 }
Chris Mason39279cc2007-06-12 06:35:45 -04006364 }
Josef Bacikaec74772008-07-24 12:12:38 -04006365
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006366 ret = btrfs_add_link(trans, new_dir, old_inode,
6367 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04006368 new_dentry->d_name.len, 0, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006369 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006370
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006371 if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) {
6372 btrfs_log_new_name(trans, old_inode, old_dir,
6373 new_dentry->d_parent);
6374 btrfs_end_log_trans(root);
6375 }
Chris Mason39279cc2007-06-12 06:35:45 -04006376out_fail:
Chris Masonab78c842008-07-29 16:15:18 -04006377 btrfs_end_transaction_throttle(trans, root);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006378
Yan, Zheng76dda932009-09-21 16:00:26 -04006379 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
6380 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006381
Chris Mason39279cc2007-06-12 06:35:45 -04006382 return ret;
6383}
6384
Chris Masond352ac62008-09-29 15:18:18 -04006385/*
6386 * some fairly slow code that needs optimization. This walks the list
6387 * of all the inodes with pending delalloc and forces them to disk.
6388 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00006389int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
Chris Masonea8c2812008-08-04 23:17:27 -04006390{
6391 struct list_head *head = &root->fs_info->delalloc_inodes;
6392 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04006393 struct inode *inode;
Chris Masonea8c2812008-08-04 23:17:27 -04006394
Yan Zhengc146afa2008-11-12 14:34:12 -05006395 if (root->fs_info->sb->s_flags & MS_RDONLY)
6396 return -EROFS;
6397
Chris Mason75eff682008-12-15 15:54:40 -05006398 spin_lock(&root->fs_info->delalloc_lock);
Chris Masond3977122009-01-05 21:25:51 -05006399 while (!list_empty(head)) {
Chris Masonea8c2812008-08-04 23:17:27 -04006400 binode = list_entry(head->next, struct btrfs_inode,
6401 delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04006402 inode = igrab(&binode->vfs_inode);
6403 if (!inode)
6404 list_del_init(&binode->delalloc_inodes);
Chris Mason75eff682008-12-15 15:54:40 -05006405 spin_unlock(&root->fs_info->delalloc_lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04006406 if (inode) {
Chris Mason8c8bee12008-09-29 11:19:10 -04006407 filemap_flush(inode->i_mapping);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00006408 if (delay_iput)
6409 btrfs_add_delayed_iput(inode);
6410 else
6411 iput(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04006412 }
6413 cond_resched();
Chris Mason75eff682008-12-15 15:54:40 -05006414 spin_lock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04006415 }
Chris Mason75eff682008-12-15 15:54:40 -05006416 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04006417
6418 /* the filemap_flush will queue IO into the worker threads, but
6419 * we have to make sure the IO is actually started and that
6420 * ordered extents get created before we return
6421 */
6422 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05006423 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05006424 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04006425 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05006426 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
6427 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04006428 }
6429 atomic_dec(&root->fs_info->async_submit_draining);
Chris Masonea8c2812008-08-04 23:17:27 -04006430 return 0;
6431}
6432
Yan, Zheng5da9d012010-05-16 10:46:25 -04006433int btrfs_start_one_delalloc_inode(struct btrfs_root *root, int delay_iput)
6434{
6435 struct btrfs_inode *binode;
6436 struct inode *inode = NULL;
6437
6438 spin_lock(&root->fs_info->delalloc_lock);
6439 while (!list_empty(&root->fs_info->delalloc_inodes)) {
6440 binode = list_entry(root->fs_info->delalloc_inodes.next,
6441 struct btrfs_inode, delalloc_inodes);
6442 inode = igrab(&binode->vfs_inode);
6443 if (inode) {
6444 list_move_tail(&binode->delalloc_inodes,
6445 &root->fs_info->delalloc_inodes);
6446 break;
6447 }
6448
6449 list_del_init(&binode->delalloc_inodes);
6450 cond_resched_lock(&root->fs_info->delalloc_lock);
6451 }
6452 spin_unlock(&root->fs_info->delalloc_lock);
6453
6454 if (inode) {
6455 write_inode_now(inode, 0);
6456 if (delay_iput)
6457 btrfs_add_delayed_iput(inode);
6458 else
6459 iput(inode);
6460 return 1;
6461 }
6462 return 0;
6463}
6464
Chris Mason39279cc2007-06-12 06:35:45 -04006465static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
6466 const char *symname)
6467{
6468 struct btrfs_trans_handle *trans;
6469 struct btrfs_root *root = BTRFS_I(dir)->root;
6470 struct btrfs_path *path;
6471 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05006472 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006473 int err;
6474 int drop_inode = 0;
6475 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006476 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04006477 int name_len;
6478 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04006479 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04006480 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04006481 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05006482 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006483
6484 name_len = strlen(symname) + 1;
6485 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
6486 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05006487
Yan, Zhenga22285a2010-05-16 10:48:46 -04006488 err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
6489 if (err)
6490 return err;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006491 /*
6492 * 2 items for inode item and ref
6493 * 2 items for dir items
6494 * 1 item for xattr if selinux is on
6495 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006496 trans = btrfs_start_transaction(root, 5);
6497 if (IS_ERR(trans))
6498 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006499
Chris Mason39279cc2007-06-12 06:35:45 -04006500 btrfs_set_trans_block_group(trans, dir);
6501
Josef Bacikaec74772008-07-24 12:12:38 -04006502 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05006503 dentry->d_name.len,
6504 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04006505 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO,
6506 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006507 err = PTR_ERR(inode);
6508 if (IS_ERR(inode))
6509 goto out_unlock;
6510
Yan, Zhengf34f57a2009-11-12 09:35:27 +00006511 err = btrfs_init_inode_security(trans, inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04006512 if (err) {
6513 drop_inode = 1;
6514 goto out_unlock;
6515 }
6516
Chris Mason39279cc2007-06-12 06:35:45 -04006517 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006518 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006519 if (err)
6520 drop_inode = 1;
6521 else {
6522 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04006523 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04006524 inode->i_fop = &btrfs_file_operations;
6525 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05006526 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04006527 }
Chris Mason39279cc2007-06-12 06:35:45 -04006528 btrfs_update_inode_block_group(trans, inode);
6529 btrfs_update_inode_block_group(trans, dir);
6530 if (drop_inode)
6531 goto out_unlock;
6532
6533 path = btrfs_alloc_path();
6534 BUG_ON(!path);
6535 key.objectid = inode->i_ino;
6536 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006537 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
6538 datasize = btrfs_file_extent_calc_inline_size(name_len);
6539 err = btrfs_insert_empty_item(trans, root, path, &key,
6540 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04006541 if (err) {
6542 drop_inode = 1;
6543 goto out_unlock;
6544 }
Chris Mason5f39d392007-10-15 16:14:19 -04006545 leaf = path->nodes[0];
6546 ei = btrfs_item_ptr(leaf, path->slots[0],
6547 struct btrfs_file_extent_item);
6548 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
6549 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04006550 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04006551 btrfs_set_file_extent_encryption(leaf, ei, 0);
6552 btrfs_set_file_extent_compression(leaf, ei, 0);
6553 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
6554 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
6555
Chris Mason39279cc2007-06-12 06:35:45 -04006556 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04006557 write_extent_buffer(leaf, symname, ptr, name_len);
6558 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04006559 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04006560
Chris Mason39279cc2007-06-12 06:35:45 -04006561 inode->i_op = &btrfs_symlink_inode_operations;
6562 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04006563 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04006564 inode_set_bytes(inode, name_len);
Chris Masondbe674a2008-07-17 12:54:05 -04006565 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04006566 err = btrfs_update_inode(trans, root, inode);
6567 if (err)
6568 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04006569
6570out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04006571 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04006572 btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04006573 if (drop_inode) {
6574 inode_dec_link_count(inode);
6575 iput(inode);
6576 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04006577 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04006578 return err;
6579}
Chris Mason16432982008-04-10 10:23:21 -04006580
Yan, Zhengefa56462010-05-16 10:49:59 -04006581int btrfs_prealloc_file_range(struct inode *inode, int mode,
6582 u64 start, u64 num_bytes, u64 min_size,
6583 loff_t actual_len, u64 *alloc_hint)
Yan Zhengd899e052008-10-30 14:25:28 -04006584{
Yan, Zheng5a303d52009-11-12 09:34:52 +00006585 struct btrfs_trans_handle *trans;
Yan Zhengd899e052008-10-30 14:25:28 -04006586 struct btrfs_root *root = BTRFS_I(inode)->root;
6587 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04006588 u64 cur_offset = start;
Yan Zhengd899e052008-10-30 14:25:28 -04006589 int ret = 0;
6590
Yan Zhengd899e052008-10-30 14:25:28 -04006591 while (num_bytes > 0) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04006592 trans = btrfs_start_transaction(root, 3);
6593 if (IS_ERR(trans)) {
6594 ret = PTR_ERR(trans);
6595 break;
6596 }
Chris Mason3a1abec2009-12-17 15:47:17 -05006597
Yan, Zhengefa56462010-05-16 10:49:59 -04006598 ret = btrfs_reserve_extent(trans, root, num_bytes, min_size,
6599 0, *alloc_hint, (u64)-1, &ins, 1);
Yan Zhengd899e052008-10-30 14:25:28 -04006600 if (ret) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04006601 btrfs_end_transaction(trans, root);
6602 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00006603 }
6604
Yan Zhengd899e052008-10-30 14:25:28 -04006605 ret = insert_reserved_file_extent(trans, inode,
6606 cur_offset, ins.objectid,
6607 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00006608 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04006609 BTRFS_FILE_EXTENT_PREALLOC);
6610 BUG_ON(ret);
Chris Masona1ed8352009-09-11 12:27:37 -04006611 btrfs_drop_extent_cache(inode, cur_offset,
6612 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00006613
Yan Zhengd899e052008-10-30 14:25:28 -04006614 num_bytes -= ins.offset;
6615 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04006616 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00006617
Yan Zhengd899e052008-10-30 14:25:28 -04006618 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006619 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04006620 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04006621 (actual_len > inode->i_size) &&
6622 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00006623 if (cur_offset > actual_len)
Yan, Zhengefa56462010-05-16 10:49:59 -04006624 i_size_write(inode, actual_len);
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00006625 else
Yan, Zhengefa56462010-05-16 10:49:59 -04006626 i_size_write(inode, cur_offset);
6627 i_size_write(inode, cur_offset);
6628 btrfs_ordered_update_i_size(inode, cur_offset, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00006629 }
6630
Yan Zhengd899e052008-10-30 14:25:28 -04006631 ret = btrfs_update_inode(trans, root, inode);
6632 BUG_ON(ret);
Yan Zhengd899e052008-10-30 14:25:28 -04006633
Yan, Zheng5a303d52009-11-12 09:34:52 +00006634 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00006635 }
Yan Zhengd899e052008-10-30 14:25:28 -04006636 return ret;
6637}
6638
6639static long btrfs_fallocate(struct inode *inode, int mode,
6640 loff_t offset, loff_t len)
6641{
Josef Bacik2ac55d42010-02-03 19:33:23 +00006642 struct extent_state *cached_state = NULL;
Yan Zhengd899e052008-10-30 14:25:28 -04006643 u64 cur_offset;
6644 u64 last_byte;
6645 u64 alloc_start;
6646 u64 alloc_end;
6647 u64 alloc_hint = 0;
Chris Masone980b502009-04-24 14:39:24 -04006648 u64 locked_end;
Yan Zhengd899e052008-10-30 14:25:28 -04006649 u64 mask = BTRFS_I(inode)->root->sectorsize - 1;
6650 struct extent_map *em;
6651 int ret;
6652
6653 alloc_start = offset & ~mask;
6654 alloc_end = (offset + len + mask) & ~mask;
6655
Chris Mason546888d2009-04-21 11:53:38 -04006656 /*
6657 * wait for ordered IO before we have any locks. We'll loop again
6658 * below with the locks held.
6659 */
6660 btrfs_wait_ordered_range(inode, alloc_start, alloc_end - alloc_start);
6661
Yan Zhengd899e052008-10-30 14:25:28 -04006662 mutex_lock(&inode->i_mutex);
6663 if (alloc_start > inode->i_size) {
6664 ret = btrfs_cont_expand(inode, alloc_start);
6665 if (ret)
6666 goto out;
6667 }
6668
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006669 ret = btrfs_check_data_free_space(inode, alloc_end - alloc_start);
Josef Bacika970b0a2009-06-27 21:07:34 -04006670 if (ret)
6671 goto out;
6672
Chris Masone980b502009-04-24 14:39:24 -04006673 locked_end = alloc_end - 1;
Yan Zhengd899e052008-10-30 14:25:28 -04006674 while (1) {
6675 struct btrfs_ordered_extent *ordered;
Chris Mason546888d2009-04-21 11:53:38 -04006676
Chris Mason546888d2009-04-21 11:53:38 -04006677 /* the extent lock is ordered inside the running
6678 * transaction
6679 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00006680 lock_extent_bits(&BTRFS_I(inode)->io_tree, alloc_start,
6681 locked_end, 0, &cached_state, GFP_NOFS);
Yan Zhengd899e052008-10-30 14:25:28 -04006682 ordered = btrfs_lookup_first_ordered_extent(inode,
6683 alloc_end - 1);
6684 if (ordered &&
6685 ordered->file_offset + ordered->len > alloc_start &&
6686 ordered->file_offset < alloc_end) {
6687 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +00006688 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
6689 alloc_start, locked_end,
6690 &cached_state, GFP_NOFS);
Chris Mason546888d2009-04-21 11:53:38 -04006691 /*
6692 * we can't wait on the range with the transaction
6693 * running or with the extent lock held
6694 */
Yan Zhengd899e052008-10-30 14:25:28 -04006695 btrfs_wait_ordered_range(inode, alloc_start,
6696 alloc_end - alloc_start);
6697 } else {
6698 if (ordered)
6699 btrfs_put_ordered_extent(ordered);
6700 break;
6701 }
6702 }
6703
6704 cur_offset = alloc_start;
6705 while (1) {
6706 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
6707 alloc_end - cur_offset, 0);
6708 BUG_ON(IS_ERR(em) || !em);
6709 last_byte = min(extent_map_end(em), alloc_end);
6710 last_byte = (last_byte + mask) & ~mask;
Yan, Zheng5a303d52009-11-12 09:34:52 +00006711 if (em->block_start == EXTENT_MAP_HOLE ||
6712 (cur_offset >= inode->i_size &&
6713 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
Yan, Zhengefa56462010-05-16 10:49:59 -04006714 ret = btrfs_prealloc_file_range(inode, 0, cur_offset,
6715 last_byte - cur_offset,
6716 1 << inode->i_blkbits,
6717 offset + len,
6718 &alloc_hint);
Yan Zhengd899e052008-10-30 14:25:28 -04006719 if (ret < 0) {
6720 free_extent_map(em);
6721 break;
6722 }
6723 }
Yan Zhengd899e052008-10-30 14:25:28 -04006724 free_extent_map(em);
6725
6726 cur_offset = last_byte;
6727 if (cur_offset >= alloc_end) {
6728 ret = 0;
6729 break;
6730 }
6731 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00006732 unlock_extent_cached(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
6733 &cached_state, GFP_NOFS);
Chris Mason546888d2009-04-21 11:53:38 -04006734
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006735 btrfs_free_reserved_data_space(inode, alloc_end - alloc_start);
Yan Zhengd899e052008-10-30 14:25:28 -04006736out:
6737 mutex_unlock(&inode->i_mutex);
6738 return ret;
6739}
6740
Chris Masone6dcd2d2008-07-17 12:53:50 -04006741static int btrfs_set_page_dirty(struct page *page)
6742{
Chris Masone6dcd2d2008-07-17 12:53:50 -04006743 return __set_page_dirty_nobuffers(page);
6744}
6745
Sven Wegener0ee0fda2008-07-30 16:54:26 -04006746static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05006747{
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006748 if ((BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) && (mask & MAY_WRITE))
Yanfdebe2b2008-01-14 13:26:08 -05006749 return -EACCES;
Josef Bacik33268ea2008-07-24 12:16:36 -04006750 return generic_permission(inode, mask, btrfs_check_acl);
Yanfdebe2b2008-01-14 13:26:08 -05006751}
Chris Mason39279cc2007-06-12 06:35:45 -04006752
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07006753static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05006754 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04006755 .lookup = btrfs_lookup,
6756 .create = btrfs_create,
6757 .unlink = btrfs_unlink,
6758 .link = btrfs_link,
6759 .mkdir = btrfs_mkdir,
6760 .rmdir = btrfs_rmdir,
6761 .rename = btrfs_rename,
6762 .symlink = btrfs_symlink,
6763 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04006764 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04006765 .setxattr = btrfs_setxattr,
6766 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05006767 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04006768 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05006769 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04006770};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07006771static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04006772 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05006773 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04006774};
Yan, Zheng76dda932009-09-21 16:00:26 -04006775
Alexey Dobriyan828c0952009-10-01 15:43:56 -07006776static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04006777 .llseek = generic_file_llseek,
6778 .read = generic_read_dir,
David Woodhousecbdf5a22008-08-06 19:42:33 +01006779 .readdir = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04006780 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04006781#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04006782 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04006783#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04006784 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04006785 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04006786};
6787
Chris Masond1310b22008-01-24 16:13:08 -05006788static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04006789 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05006790 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04006791 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04006792 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006793 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04006794 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Mason1259ab72008-05-12 13:39:03 -04006795 .readpage_io_failed_hook = btrfs_io_failed_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05006796 .set_bit_hook = btrfs_set_bit_hook,
6797 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04006798 .merge_extent_hook = btrfs_merge_extent_hook,
6799 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04006800};
6801
Chris Mason35054392009-01-21 13:11:13 -05006802/*
6803 * btrfs doesn't support the bmap operation because swapfiles
6804 * use bmap to make a mapping of extents in the file. They assume
6805 * these extents won't change over the life of the file and they
6806 * use the bmap result to do IO directly to the drive.
6807 *
6808 * the btrfs bmap call would return logical addresses that aren't
6809 * suitable for IO and they also will change frequently as COW
6810 * operations happen. So, swapfile + btrfs == corruption.
6811 *
6812 * For now we're avoiding this by dropping bmap.
6813 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07006814static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04006815 .readpage = btrfs_readpage,
6816 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04006817 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05006818 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04006819 .sync_page = block_sync_page,
Chris Mason16432982008-04-10 10:23:21 -04006820 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04006821 .invalidatepage = btrfs_invalidatepage,
6822 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006823 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02006824 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04006825};
6826
Alexey Dobriyan7f094102009-09-21 17:01:10 -07006827static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04006828 .readpage = btrfs_readpage,
6829 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04006830 .invalidatepage = btrfs_invalidatepage,
6831 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04006832};
6833
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07006834static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04006835 .truncate = btrfs_truncate,
6836 .getattr = btrfs_getattr,
6837 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04006838 .setxattr = btrfs_setxattr,
6839 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05006840 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04006841 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05006842 .permission = btrfs_permission,
Yan Zhengd899e052008-10-30 14:25:28 -04006843 .fallocate = btrfs_fallocate,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05006844 .fiemap = btrfs_fiemap,
Chris Mason39279cc2007-06-12 06:35:45 -04006845};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07006846static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04006847 .getattr = btrfs_getattr,
6848 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05006849 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04006850 .setxattr = btrfs_setxattr,
6851 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04006852 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04006853 .removexattr = btrfs_removexattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04006854};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07006855static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04006856 .readlink = generic_readlink,
6857 .follow_link = page_follow_link_light,
6858 .put_link = page_put_link,
Yanfdebe2b2008-01-14 13:26:08 -05006859 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05006860 .setxattr = btrfs_setxattr,
6861 .getxattr = btrfs_getxattr,
6862 .listxattr = btrfs_listxattr,
6863 .removexattr = btrfs_removexattr,
Chris Mason39279cc2007-06-12 06:35:45 -04006864};
Yan, Zheng76dda932009-09-21 16:00:26 -04006865
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04006866const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04006867 .d_delete = btrfs_dentry_delete,
6868};