blob: 01438e9ba2e2c64a8de31dcd9c38403f89546a9e [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"
Josef Bacikdc89e982011-01-28 17:05:48 -050053#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080054#include "inode-map.h"
Chris Mason39279cc2007-06-12 06:35:45 -040055
56struct btrfs_iget_args {
57 u64 ino;
58 struct btrfs_root *root;
59};
60
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070061static const struct inode_operations btrfs_dir_inode_operations;
62static const struct inode_operations btrfs_symlink_inode_operations;
63static const struct inode_operations btrfs_dir_ro_inode_operations;
64static const struct inode_operations btrfs_special_inode_operations;
65static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070066static const struct address_space_operations btrfs_aops;
67static const struct address_space_operations btrfs_symlink_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070068static const struct file_operations btrfs_dir_file_operations;
Chris Masond1310b22008-01-24 16:13:08 -050069static struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040070
71static struct kmem_cache *btrfs_inode_cachep;
72struct kmem_cache *btrfs_trans_handle_cachep;
73struct kmem_cache *btrfs_transaction_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040074struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050075struct kmem_cache *btrfs_free_space_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040076
77#define S_SHIFT 12
78static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
79 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
80 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
81 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
82 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
83 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
84 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
85 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
86};
87
Josef Bacika41ad392011-01-31 15:30:16 -050088static int btrfs_setsize(struct inode *inode, loff_t newsize);
89static int btrfs_truncate(struct inode *inode);
Chris Masonc8b97812008-10-29 14:49:59 -040090static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end);
Chris Mason771ed682008-11-06 22:02:51 -050091static noinline int cow_file_range(struct inode *inode,
92 struct page *locked_page,
93 u64 start, u64 end, int *page_started,
94 unsigned long *nr_written, int unlock);
Josef Bacik7b128762008-07-24 12:17:14 -040095
Yan, Zhengf34f57a2009-11-12 09:35:27 +000096static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -050097 struct inode *inode, struct inode *dir,
98 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -050099{
100 int err;
101
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000102 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500103 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500104 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500105 return err;
106}
107
Chris Masond352ac62008-09-29 15:18:18 -0400108/*
Chris Masonc8b97812008-10-29 14:49:59 -0400109 * this does all the hard work for inserting an inline extent into
110 * the btree. The caller should have done a btrfs_drop_extents so that
111 * no overlapping inline items exist in the btree
112 */
Chris Masond3977122009-01-05 21:25:51 -0500113static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400114 struct btrfs_root *root, struct inode *inode,
115 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000116 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400117 struct page **compressed_pages)
118{
119 struct btrfs_key key;
120 struct btrfs_path *path;
121 struct extent_buffer *leaf;
122 struct page *page = NULL;
123 char *kaddr;
124 unsigned long ptr;
125 struct btrfs_file_extent_item *ei;
126 int err = 0;
127 int ret;
128 size_t cur_size = size;
129 size_t datasize;
130 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400131
Li Zefanfe3f5662011-03-28 08:30:38 +0000132 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400133 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400134
Chris Masond3977122009-01-05 21:25:51 -0500135 path = btrfs_alloc_path();
136 if (!path)
Chris Masonc8b97812008-10-29 14:49:59 -0400137 return -ENOMEM;
138
Chris Masonb9473432009-03-13 11:00:37 -0400139 path->leave_spinning = 1;
Chris Masonc8b97812008-10-29 14:49:59 -0400140 btrfs_set_trans_block_group(trans, inode);
141
Li Zefan33345d012011-04-20 10:31:50 +0800142 key.objectid = btrfs_ino(inode);
Chris Masonc8b97812008-10-29 14:49:59 -0400143 key.offset = start;
144 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
Chris Masonc8b97812008-10-29 14:49:59 -0400145 datasize = btrfs_file_extent_calc_inline_size(cur_size);
146
147 inode_add_bytes(inode, size);
148 ret = btrfs_insert_empty_item(trans, root, path, &key,
149 datasize);
150 BUG_ON(ret);
151 if (ret) {
152 err = ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400153 goto fail;
154 }
155 leaf = path->nodes[0];
156 ei = btrfs_item_ptr(leaf, path->slots[0],
157 struct btrfs_file_extent_item);
158 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
159 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
160 btrfs_set_file_extent_encryption(leaf, ei, 0);
161 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
162 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
163 ptr = btrfs_file_extent_inline_start(ei);
164
Li Zefan261507a02010-12-17 14:21:50 +0800165 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400166 struct page *cpage;
167 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500168 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400169 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500170 cur_size = min_t(unsigned long, compressed_size,
Chris Masonc8b97812008-10-29 14:49:59 -0400171 PAGE_CACHE_SIZE);
172
Chris Masonb9473432009-03-13 11:00:37 -0400173 kaddr = kmap_atomic(cpage, KM_USER0);
Chris Masonc8b97812008-10-29 14:49:59 -0400174 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Chris Masonb9473432009-03-13 11:00:37 -0400175 kunmap_atomic(kaddr, KM_USER0);
Chris Masonc8b97812008-10-29 14:49:59 -0400176
177 i++;
178 ptr += cur_size;
179 compressed_size -= cur_size;
180 }
181 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800182 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400183 } else {
184 page = find_get_page(inode->i_mapping,
185 start >> PAGE_CACHE_SHIFT);
186 btrfs_set_file_extent_compression(leaf, ei, 0);
187 kaddr = kmap_atomic(page, KM_USER0);
188 offset = start & (PAGE_CACHE_SIZE - 1);
189 write_extent_buffer(leaf, kaddr + offset, ptr, size);
190 kunmap_atomic(kaddr, KM_USER0);
191 page_cache_release(page);
192 }
193 btrfs_mark_buffer_dirty(leaf);
194 btrfs_free_path(path);
195
Yan, Zhengc2167752009-11-12 09:34:21 +0000196 /*
197 * we're an inline extent, so nobody can
198 * extend the file past i_size without locking
199 * a page we already have locked.
200 *
201 * We must do any isize and inode updates
202 * before we unlock the pages. Otherwise we
203 * could end up racing with unlink.
204 */
Chris Masonc8b97812008-10-29 14:49:59 -0400205 BTRFS_I(inode)->disk_i_size = inode->i_size;
206 btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000207
Chris Masonc8b97812008-10-29 14:49:59 -0400208 return 0;
209fail:
210 btrfs_free_path(path);
211 return err;
212}
213
214
215/*
216 * conditionally insert an inline extent into the file. This
217 * does the checks required to make sure the data is small enough
218 * to fit as an inline extent.
219 */
Chris Mason7f366cf2009-03-12 20:12:45 -0400220static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400221 struct btrfs_root *root,
222 struct inode *inode, u64 start, u64 end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000223 size_t compressed_size, int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400224 struct page **compressed_pages)
225{
226 u64 isize = i_size_read(inode);
227 u64 actual_end = min(end + 1, isize);
228 u64 inline_len = actual_end - start;
229 u64 aligned_end = (end + root->sectorsize - 1) &
230 ~((u64)root->sectorsize - 1);
231 u64 hint_byte;
232 u64 data_len = inline_len;
233 int ret;
234
235 if (compressed_size)
236 data_len = compressed_size;
237
238 if (start > 0 ||
Chris Mason70b99e62008-10-31 12:46:39 -0400239 actual_end >= PAGE_CACHE_SIZE ||
Chris Masonc8b97812008-10-29 14:49:59 -0400240 data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
241 (!compressed_size &&
242 (actual_end & (root->sectorsize - 1)) == 0) ||
243 end + 1 < isize ||
244 data_len > root->fs_info->max_inline) {
245 return 1;
246 }
247
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000248 ret = btrfs_drop_extents(trans, inode, start, aligned_end,
Chris Masona1ed8352009-09-11 12:27:37 -0400249 &hint_byte, 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400250 BUG_ON(ret);
251
252 if (isize > actual_end)
253 inline_len = min_t(u64, isize, actual_end);
254 ret = insert_inline_extent(trans, root, inode, start,
255 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000256 compress_type, compressed_pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400257 BUG_ON(ret);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400258 btrfs_delalloc_release_metadata(inode, end + 1 - start);
Chris Masona1ed8352009-09-11 12:27:37 -0400259 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
Chris Masonc8b97812008-10-29 14:49:59 -0400260 return 0;
261}
262
Chris Mason771ed682008-11-06 22:02:51 -0500263struct async_extent {
264 u64 start;
265 u64 ram_size;
266 u64 compressed_size;
267 struct page **pages;
268 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800269 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500270 struct list_head list;
271};
272
273struct async_cow {
274 struct inode *inode;
275 struct btrfs_root *root;
276 struct page *locked_page;
277 u64 start;
278 u64 end;
279 struct list_head extents;
280 struct btrfs_work work;
281};
282
283static noinline int add_async_extent(struct async_cow *cow,
284 u64 start, u64 ram_size,
285 u64 compressed_size,
286 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800287 unsigned long nr_pages,
288 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500289{
290 struct async_extent *async_extent;
291
292 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Yoshinori Sanodac97e512011-02-15 12:01:42 +0000293 BUG_ON(!async_extent);
Chris Mason771ed682008-11-06 22:02:51 -0500294 async_extent->start = start;
295 async_extent->ram_size = ram_size;
296 async_extent->compressed_size = compressed_size;
297 async_extent->pages = pages;
298 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800299 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500300 list_add_tail(&async_extent->list, &cow->extents);
301 return 0;
302}
303
Chris Masonc8b97812008-10-29 14:49:59 -0400304/*
Chris Mason771ed682008-11-06 22:02:51 -0500305 * we create compressed extents in two phases. The first
306 * phase compresses a range of pages that have already been
307 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400308 *
Chris Mason771ed682008-11-06 22:02:51 -0500309 * This is done inside an ordered work queue, and the compression
310 * is spread across many cpus. The actual IO submission is step
311 * two, and the ordered work queue takes care of making sure that
312 * happens in the same order things were put onto the queue by
313 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400314 *
Chris Mason771ed682008-11-06 22:02:51 -0500315 * If this code finds it can't get good compression, it puts an
316 * entry onto the work queue to write the uncompressed bytes. This
317 * makes sure that both compressed inodes and uncompressed inodes
318 * are written in the same order that pdflush sent them down.
Chris Masond352ac62008-09-29 15:18:18 -0400319 */
Chris Mason771ed682008-11-06 22:02:51 -0500320static noinline int compress_file_range(struct inode *inode,
321 struct page *locked_page,
322 u64 start, u64 end,
323 struct async_cow *async_cow,
324 int *num_added)
Chris Masonb888db22007-08-27 16:49:44 -0400325{
326 struct btrfs_root *root = BTRFS_I(inode)->root;
327 struct btrfs_trans_handle *trans;
Chris Masondb945352007-10-15 16:15:53 -0400328 u64 num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400329 u64 blocksize = root->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400330 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500331 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400332 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400333 struct page **pages = NULL;
334 unsigned long nr_pages;
335 unsigned long nr_pages_ret = 0;
336 unsigned long total_compressed = 0;
337 unsigned long total_in = 0;
338 unsigned long max_compressed = 128 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -0500339 unsigned long max_uncompressed = 128 * 1024;
Chris Masonc8b97812008-10-29 14:49:59 -0400340 int i;
341 int will_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800342 int compress_type = root->fs_info->compress_type;
Chris Masonb888db22007-08-27 16:49:44 -0400343
Chris Mason42dc7ba2008-12-15 11:44:56 -0500344 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400345again:
346 will_compress = 0;
347 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
348 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
349
Chris Masonf03d9301f2009-02-04 09:31:06 -0500350 /*
351 * we don't want to send crud past the end of i_size through
352 * compression, that's just a waste of CPU time. So, if the
353 * end of the file is before the start of our current
354 * requested range of bytes, we bail out to the uncompressed
355 * cleanup code that can deal with all of this.
356 *
357 * It isn't really the fastest way to fix things, but this is a
358 * very uncommon corner.
359 */
360 if (actual_end <= start)
361 goto cleanup_and_bail_uncompressed;
362
Chris Masonc8b97812008-10-29 14:49:59 -0400363 total_compressed = actual_end - start;
364
365 /* we want to make sure that amount of ram required to uncompress
366 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500367 * of a compressed extent to 128k. This is a crucial number
368 * because it also controls how easily we can spread reads across
369 * cpus for decompression.
370 *
371 * We also want to make sure the amount of IO required to do
372 * a random read is reasonably small, so we limit the size of
373 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400374 */
375 total_compressed = min(total_compressed, max_uncompressed);
Chris Masondb945352007-10-15 16:15:53 -0400376 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
Chris Masonbe20aa92007-12-17 20:14:01 -0500377 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400378 total_in = 0;
379 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400380
Chris Mason771ed682008-11-06 22:02:51 -0500381 /*
382 * we do compression for mount -o compress and when the
383 * inode has not been flagged as nocompress. This flag can
384 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400385 */
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200386 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) &&
Chris Mason1e701a32010-03-11 09:42:04 -0500387 (btrfs_test_opt(root, COMPRESS) ||
Liu Bo75e7cb72011-03-22 10:12:20 +0000388 (BTRFS_I(inode)->force_compress) ||
389 (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS))) {
Chris Masonc8b97812008-10-29 14:49:59 -0400390 WARN_ON(pages);
Chris Masoncfbc2462008-10-30 13:22:14 -0400391 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
Yoshinori Sanodac97e512011-02-15 12:01:42 +0000392 BUG_ON(!pages);
Chris Mason179e29e2007-11-01 11:28:41 -0400393
Li Zefan261507a02010-12-17 14:21:50 +0800394 if (BTRFS_I(inode)->force_compress)
395 compress_type = BTRFS_I(inode)->force_compress;
396
397 ret = btrfs_compress_pages(compress_type,
398 inode->i_mapping, start,
399 total_compressed, pages,
400 nr_pages, &nr_pages_ret,
401 &total_in,
402 &total_compressed,
403 max_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400404
405 if (!ret) {
406 unsigned long offset = total_compressed &
407 (PAGE_CACHE_SIZE - 1);
408 struct page *page = pages[nr_pages_ret - 1];
409 char *kaddr;
410
411 /* zero the tail end of the last page, we might be
412 * sending it down to disk
413 */
414 if (offset) {
415 kaddr = kmap_atomic(page, KM_USER0);
416 memset(kaddr + offset, 0,
417 PAGE_CACHE_SIZE - offset);
418 kunmap_atomic(kaddr, KM_USER0);
419 }
420 will_compress = 1;
421 }
422 }
423 if (start == 0) {
Chris Mason771ed682008-11-06 22:02:51 -0500424 trans = btrfs_join_transaction(root, 1);
Tsutomu Itoh3612b492011-01-25 02:51:38 +0000425 BUG_ON(IS_ERR(trans));
Chris Mason771ed682008-11-06 22:02:51 -0500426 btrfs_set_trans_block_group(trans, inode);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400427 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Mason771ed682008-11-06 22:02:51 -0500428
Chris Masonc8b97812008-10-29 14:49:59 -0400429 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500430 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400431 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500432 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400433 */
434 ret = cow_file_range_inline(trans, root, inode,
Li Zefanfe3f5662011-03-28 08:30:38 +0000435 start, end, 0, 0, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400436 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500437 /* try making a compressed inline extent */
Chris Masonc8b97812008-10-29 14:49:59 -0400438 ret = cow_file_range_inline(trans, root, inode,
439 start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000440 total_compressed,
441 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400442 }
443 if (ret == 0) {
Chris Mason771ed682008-11-06 22:02:51 -0500444 /*
445 * inline extent creation worked, we don't need
446 * to create any more async work items. Unlock
447 * and free up our temp pages.
448 */
Chris Masonc8b97812008-10-29 14:49:59 -0400449 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400450 &BTRFS_I(inode)->io_tree,
451 start, end, NULL,
452 EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
Chris Masona3429ab2009-10-08 12:30:20 -0400453 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400454 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000455
456 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400457 goto free_pages_out;
458 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000459 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400460 }
461
462 if (will_compress) {
463 /*
464 * we aren't doing an inline extent round the compressed size
465 * up to a block size boundary so the allocator does sane
466 * things
467 */
468 total_compressed = (total_compressed + blocksize - 1) &
469 ~(blocksize - 1);
470
471 /*
472 * one last check to make sure the compression is really a
473 * win, compare the page count read with the blocks on disk
474 */
475 total_in = (total_in + PAGE_CACHE_SIZE - 1) &
476 ~(PAGE_CACHE_SIZE - 1);
477 if (total_compressed >= total_in) {
478 will_compress = 0;
479 } else {
Chris Masonc8b97812008-10-29 14:49:59 -0400480 num_bytes = total_in;
481 }
482 }
483 if (!will_compress && pages) {
484 /*
485 * the compression code ran but failed to make things smaller,
486 * free any pages it allocated and our page pointer array
487 */
488 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400489 WARN_ON(pages[i]->mapping);
Chris Masonc8b97812008-10-29 14:49:59 -0400490 page_cache_release(pages[i]);
491 }
492 kfree(pages);
493 pages = NULL;
494 total_compressed = 0;
495 nr_pages_ret = 0;
496
497 /* flag the file so we don't compress in the future */
Chris Mason1e701a32010-03-11 09:42:04 -0500498 if (!btrfs_test_opt(root, FORCE_COMPRESS) &&
499 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500500 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500501 }
Chris Masonc8b97812008-10-29 14:49:59 -0400502 }
Chris Mason771ed682008-11-06 22:02:51 -0500503 if (will_compress) {
504 *num_added += 1;
505
506 /* the async work queues will take care of doing actual
507 * allocation on disk for these compressed pages,
508 * and will submit them to the elevator.
509 */
510 add_async_extent(async_cow, start, num_bytes,
Li Zefan261507a02010-12-17 14:21:50 +0800511 total_compressed, pages, nr_pages_ret,
512 compress_type);
Chris Mason771ed682008-11-06 22:02:51 -0500513
Yan, Zheng24ae6362010-12-06 07:02:36 +0000514 if (start + num_bytes < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500515 start += num_bytes;
516 pages = NULL;
517 cond_resched();
518 goto again;
519 }
520 } else {
Chris Masonf03d9301f2009-02-04 09:31:06 -0500521cleanup_and_bail_uncompressed:
Chris Mason771ed682008-11-06 22:02:51 -0500522 /*
523 * No compression, but we still need to write the pages in
524 * the file we've been given so far. redirty the locked
525 * page if it corresponds to our extent and set things up
526 * for the async work queue to run cow_file_range to do
527 * the normal delalloc dance
528 */
529 if (page_offset(locked_page) >= start &&
530 page_offset(locked_page) <= end) {
531 __set_page_dirty_nobuffers(locked_page);
532 /* unlocked later on in the async handlers */
533 }
Li Zefan261507a02010-12-17 14:21:50 +0800534 add_async_extent(async_cow, start, end - start + 1,
535 0, NULL, 0, BTRFS_COMPRESS_NONE);
Chris Mason771ed682008-11-06 22:02:51 -0500536 *num_added += 1;
537 }
538
539out:
540 return 0;
541
542free_pages_out:
543 for (i = 0; i < nr_pages_ret; i++) {
544 WARN_ON(pages[i]->mapping);
545 page_cache_release(pages[i]);
546 }
Chris Masond3977122009-01-05 21:25:51 -0500547 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500548
549 goto out;
550}
551
552/*
553 * phase two of compressed writeback. This is the ordered portion
554 * of the code, which only gets called in the order the work was
555 * queued. We walk all the async extents created by compress_file_range
556 * and send them down to the disk.
557 */
558static noinline int submit_compressed_extents(struct inode *inode,
559 struct async_cow *async_cow)
560{
561 struct async_extent *async_extent;
562 u64 alloc_hint = 0;
563 struct btrfs_trans_handle *trans;
564 struct btrfs_key ins;
565 struct extent_map *em;
566 struct btrfs_root *root = BTRFS_I(inode)->root;
567 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
568 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500569 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500570
571 if (list_empty(&async_cow->extents))
572 return 0;
573
Chris Mason771ed682008-11-06 22:02:51 -0500574
Chris Masond3977122009-01-05 21:25:51 -0500575 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500576 async_extent = list_entry(async_cow->extents.next,
577 struct async_extent, list);
578 list_del(&async_extent->list);
579
580 io_tree = &BTRFS_I(inode)->io_tree;
581
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500582retry:
Chris Mason771ed682008-11-06 22:02:51 -0500583 /* did the compression code fall back to uncompressed IO? */
584 if (!async_extent->pages) {
585 int page_started = 0;
586 unsigned long nr_written = 0;
587
588 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000589 async_extent->start +
590 async_extent->ram_size - 1, GFP_NOFS);
Chris Mason771ed682008-11-06 22:02:51 -0500591
592 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500593 ret = cow_file_range(inode, async_cow->locked_page,
594 async_extent->start,
595 async_extent->start +
596 async_extent->ram_size - 1,
597 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500598
599 /*
600 * if page_started, cow_file_range inserted an
601 * inline extent and took care of all the unlocking
602 * and IO for us. Otherwise, we need to submit
603 * all those pages down to the drive.
604 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500605 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500606 extent_write_locked_range(io_tree,
607 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500608 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500609 async_extent->ram_size - 1,
610 btrfs_get_extent,
611 WB_SYNC_ALL);
612 kfree(async_extent);
613 cond_resched();
614 continue;
615 }
616
617 lock_extent(io_tree, async_extent->start,
618 async_extent->start + async_extent->ram_size - 1,
619 GFP_NOFS);
Chris Mason771ed682008-11-06 22:02:51 -0500620
Yan, Zhengc2167752009-11-12 09:34:21 +0000621 trans = btrfs_join_transaction(root, 1);
Tsutomu Itoh3612b492011-01-25 02:51:38 +0000622 BUG_ON(IS_ERR(trans));
Chris Mason771ed682008-11-06 22:02:51 -0500623 ret = btrfs_reserve_extent(trans, root,
624 async_extent->compressed_size,
625 async_extent->compressed_size,
626 0, alloc_hint,
627 (u64)-1, &ins, 1);
Yan, Zhengc2167752009-11-12 09:34:21 +0000628 btrfs_end_transaction(trans, root);
629
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500630 if (ret) {
631 int i;
632 for (i = 0; i < async_extent->nr_pages; i++) {
633 WARN_ON(async_extent->pages[i]->mapping);
634 page_cache_release(async_extent->pages[i]);
635 }
636 kfree(async_extent->pages);
637 async_extent->nr_pages = 0;
638 async_extent->pages = NULL;
639 unlock_extent(io_tree, async_extent->start,
640 async_extent->start +
641 async_extent->ram_size - 1, GFP_NOFS);
642 goto retry;
643 }
644
Yan, Zhengc2167752009-11-12 09:34:21 +0000645 /*
646 * here we're doing allocation and writeback of the
647 * compressed pages
648 */
649 btrfs_drop_extent_cache(inode, async_extent->start,
650 async_extent->start +
651 async_extent->ram_size - 1, 0);
652
Chris Mason771ed682008-11-06 22:02:51 -0500653 em = alloc_extent_map(GFP_NOFS);
Tsutomu Itohc26a9202011-02-14 00:45:29 +0000654 BUG_ON(!em);
Chris Mason771ed682008-11-06 22:02:51 -0500655 em->start = async_extent->start;
656 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500657 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500658
659 em->block_start = ins.objectid;
660 em->block_len = ins.offset;
661 em->bdev = root->fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800662 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500663 set_bit(EXTENT_FLAG_PINNED, &em->flags);
664 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
665
Chris Masond3977122009-01-05 21:25:51 -0500666 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400667 write_lock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500668 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -0400669 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500670 if (ret != -EEXIST) {
671 free_extent_map(em);
672 break;
673 }
674 btrfs_drop_extent_cache(inode, async_extent->start,
675 async_extent->start +
676 async_extent->ram_size - 1, 0);
677 }
678
Li Zefan261507a02010-12-17 14:21:50 +0800679 ret = btrfs_add_ordered_extent_compress(inode,
680 async_extent->start,
681 ins.objectid,
682 async_extent->ram_size,
683 ins.offset,
684 BTRFS_ORDERED_COMPRESSED,
685 async_extent->compress_type);
Chris Mason771ed682008-11-06 22:02:51 -0500686 BUG_ON(ret);
687
Chris Mason771ed682008-11-06 22:02:51 -0500688 /*
689 * clear dirty, set writeback and unlock the pages.
690 */
691 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400692 &BTRFS_I(inode)->io_tree,
693 async_extent->start,
694 async_extent->start +
695 async_extent->ram_size - 1,
696 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
697 EXTENT_CLEAR_UNLOCK |
Chris Masona3429ab2009-10-08 12:30:20 -0400698 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400699 EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500700
701 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500702 async_extent->start,
703 async_extent->ram_size,
704 ins.objectid,
705 ins.offset, async_extent->pages,
706 async_extent->nr_pages);
Chris Mason771ed682008-11-06 22:02:51 -0500707
708 BUG_ON(ret);
Chris Mason771ed682008-11-06 22:02:51 -0500709 alloc_hint = ins.objectid + ins.offset;
710 kfree(async_extent);
711 cond_resched();
712 }
713
Chris Mason771ed682008-11-06 22:02:51 -0500714 return 0;
715}
716
Josef Bacik4b46fce2010-05-23 11:00:55 -0400717static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
718 u64 num_bytes)
719{
720 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
721 struct extent_map *em;
722 u64 alloc_hint = 0;
723
724 read_lock(&em_tree->lock);
725 em = search_extent_mapping(em_tree, start, num_bytes);
726 if (em) {
727 /*
728 * if block start isn't an actual block number then find the
729 * first block in this inode and use that as a hint. If that
730 * block is also bogus then just don't worry about it.
731 */
732 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
733 free_extent_map(em);
734 em = search_extent_mapping(em_tree, 0, 0);
735 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
736 alloc_hint = em->block_start;
737 if (em)
738 free_extent_map(em);
739 } else {
740 alloc_hint = em->block_start;
741 free_extent_map(em);
742 }
743 }
744 read_unlock(&em_tree->lock);
745
746 return alloc_hint;
747}
748
Li Zefan82d59022011-04-20 10:33:24 +0800749static inline bool is_free_space_inode(struct btrfs_root *root,
750 struct inode *inode)
751{
752 if (root == root->fs_info->tree_root ||
753 BTRFS_I(inode)->location.objectid == BTRFS_FREE_INO_OBJECTID)
754 return true;
755 return false;
756}
757
Chris Mason771ed682008-11-06 22:02:51 -0500758/*
759 * when extent_io.c finds a delayed allocation range in the file,
760 * the call backs end up in this code. The basic idea is to
761 * allocate extents on disk for the range, and create ordered data structs
762 * in ram to track those extents.
763 *
764 * locked_page is the page that writepage had locked already. We use
765 * it to make sure we don't do extra locks or unlocks.
766 *
767 * *page_started is set to one if we unlock locked_page and do everything
768 * required to start IO on it. It may be clean and already done with
769 * IO when we return.
770 */
771static noinline int cow_file_range(struct inode *inode,
772 struct page *locked_page,
773 u64 start, u64 end, int *page_started,
774 unsigned long *nr_written,
775 int unlock)
776{
777 struct btrfs_root *root = BTRFS_I(inode)->root;
778 struct btrfs_trans_handle *trans;
779 u64 alloc_hint = 0;
780 u64 num_bytes;
781 unsigned long ram_size;
782 u64 disk_num_bytes;
783 u64 cur_alloc_size;
784 u64 blocksize = root->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500785 struct btrfs_key ins;
786 struct extent_map *em;
787 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
788 int ret = 0;
789
Li Zefan82d59022011-04-20 10:33:24 +0800790 BUG_ON(is_free_space_inode(root, inode));
Chris Mason771ed682008-11-06 22:02:51 -0500791 trans = btrfs_join_transaction(root, 1);
Tsutomu Itoh3612b492011-01-25 02:51:38 +0000792 BUG_ON(IS_ERR(trans));
Chris Mason771ed682008-11-06 22:02:51 -0500793 btrfs_set_trans_block_group(trans, inode);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400794 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Mason771ed682008-11-06 22:02:51 -0500795
Chris Mason771ed682008-11-06 22:02:51 -0500796 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
797 num_bytes = max(blocksize, num_bytes);
798 disk_num_bytes = num_bytes;
799 ret = 0;
800
801 if (start == 0) {
802 /* lets try to make an inline extent */
803 ret = cow_file_range_inline(trans, root, inode,
Li Zefanfe3f5662011-03-28 08:30:38 +0000804 start, end, 0, 0, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500805 if (ret == 0) {
806 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400807 &BTRFS_I(inode)->io_tree,
808 start, end, NULL,
809 EXTENT_CLEAR_UNLOCK_PAGE |
810 EXTENT_CLEAR_UNLOCK |
811 EXTENT_CLEAR_DELALLOC |
812 EXTENT_CLEAR_DIRTY |
813 EXTENT_SET_WRITEBACK |
814 EXTENT_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000815
Chris Mason771ed682008-11-06 22:02:51 -0500816 *nr_written = *nr_written +
817 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
818 *page_started = 1;
819 ret = 0;
820 goto out;
821 }
822 }
Chris Masonc8b97812008-10-29 14:49:59 -0400823
824 BUG_ON(disk_num_bytes >
825 btrfs_super_total_bytes(&root->fs_info->super_copy));
826
Josef Bacik4b46fce2010-05-23 11:00:55 -0400827 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400828 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400829
Chris Masond3977122009-01-05 21:25:51 -0500830 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400831 unsigned long op;
832
Josef Bacik287a0ab2010-03-19 18:07:23 +0000833 cur_alloc_size = disk_num_bytes;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400834 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500835 root->sectorsize, 0, alloc_hint,
Chris Masone6dcd2d2008-07-17 12:53:50 -0400836 (u64)-1, &ins, 1);
Chris Masond3977122009-01-05 21:25:51 -0500837 BUG_ON(ret);
838
Chris Masone6dcd2d2008-07-17 12:53:50 -0400839 em = alloc_extent_map(GFP_NOFS);
Tsutomu Itohc26a9202011-02-14 00:45:29 +0000840 BUG_ON(!em);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400841 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500842 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500843 ram_size = ins.offset;
844 em->len = ins.offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400845
Chris Masone6dcd2d2008-07-17 12:53:50 -0400846 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400847 em->block_len = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400848 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400849 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -0400850
Chris Masond3977122009-01-05 21:25:51 -0500851 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400852 write_lock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400853 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -0400854 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400855 if (ret != -EEXIST) {
856 free_extent_map(em);
857 break;
858 }
859 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400860 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400861 }
862
Chris Mason98d20f62008-04-14 09:46:10 -0400863 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400864 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -0500865 ram_size, cur_alloc_size, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400866 BUG_ON(ret);
Chris Masonc8b97812008-10-29 14:49:59 -0400867
Yan Zheng17d217f2008-12-12 10:03:38 -0500868 if (root->root_key.objectid ==
869 BTRFS_DATA_RELOC_TREE_OBJECTID) {
870 ret = btrfs_reloc_clone_csums(inode, start,
871 cur_alloc_size);
872 BUG_ON(ret);
873 }
874
Chris Masond3977122009-01-05 21:25:51 -0500875 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -0400876 break;
Chris Masond3977122009-01-05 21:25:51 -0500877
Chris Masonc8b97812008-10-29 14:49:59 -0400878 /* we're not doing compressed IO, don't unlock the first
879 * page (which the caller expects to stay locked), don't
880 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -0400881 *
882 * Do set the Private2 bit so we know this page was properly
883 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -0400884 */
Chris Masona791e352009-10-08 11:27:10 -0400885 op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0;
886 op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
887 EXTENT_SET_PRIVATE2;
888
Chris Masonc8b97812008-10-29 14:49:59 -0400889 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
890 start, start + ram_size - 1,
Chris Masona791e352009-10-08 11:27:10 -0400891 locked_page, op);
Chris Masonc8b97812008-10-29 14:49:59 -0400892 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -0500893 num_bytes -= cur_alloc_size;
894 alloc_hint = ins.objectid + ins.offset;
895 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400896 }
Chris Masonb888db22007-08-27 16:49:44 -0400897out:
Chris Mason771ed682008-11-06 22:02:51 -0500898 ret = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400899 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400900
Chris Masonbe20aa92007-12-17 20:14:01 -0500901 return ret;
Chris Mason771ed682008-11-06 22:02:51 -0500902}
Chris Masonc8b97812008-10-29 14:49:59 -0400903
Chris Mason771ed682008-11-06 22:02:51 -0500904/*
905 * work queue call back to started compression on a file and pages
906 */
907static noinline void async_cow_start(struct btrfs_work *work)
908{
909 struct async_cow *async_cow;
910 int num_added = 0;
911 async_cow = container_of(work, struct async_cow, work);
912
913 compress_file_range(async_cow->inode, async_cow->locked_page,
914 async_cow->start, async_cow->end, async_cow,
915 &num_added);
916 if (num_added == 0)
917 async_cow->inode = NULL;
918}
919
920/*
921 * work queue call back to submit previously compressed pages
922 */
923static noinline void async_cow_submit(struct btrfs_work *work)
924{
925 struct async_cow *async_cow;
926 struct btrfs_root *root;
927 unsigned long nr_pages;
928
929 async_cow = container_of(work, struct async_cow, work);
930
931 root = async_cow->root;
932 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
933 PAGE_CACHE_SHIFT;
934
935 atomic_sub(nr_pages, &root->fs_info->async_delalloc_pages);
936
937 if (atomic_read(&root->fs_info->async_delalloc_pages) <
938 5 * 1042 * 1024 &&
939 waitqueue_active(&root->fs_info->async_submit_wait))
940 wake_up(&root->fs_info->async_submit_wait);
941
Chris Masond3977122009-01-05 21:25:51 -0500942 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -0500943 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -0500944}
Chris Masonc8b97812008-10-29 14:49:59 -0400945
Chris Mason771ed682008-11-06 22:02:51 -0500946static noinline void async_cow_free(struct btrfs_work *work)
947{
948 struct async_cow *async_cow;
949 async_cow = container_of(work, struct async_cow, work);
950 kfree(async_cow);
951}
952
953static int cow_file_range_async(struct inode *inode, struct page *locked_page,
954 u64 start, u64 end, int *page_started,
955 unsigned long *nr_written)
956{
957 struct async_cow *async_cow;
958 struct btrfs_root *root = BTRFS_I(inode)->root;
959 unsigned long nr_pages;
960 u64 cur_end;
961 int limit = 10 * 1024 * 1042;
962
Chris Masona3429ab2009-10-08 12:30:20 -0400963 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
964 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -0500965 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500966 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -0400967 BUG_ON(!async_cow);
Chris Mason771ed682008-11-06 22:02:51 -0500968 async_cow->inode = inode;
969 async_cow->root = root;
970 async_cow->locked_page = locked_page;
971 async_cow->start = start;
972
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200973 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
Chris Mason771ed682008-11-06 22:02:51 -0500974 cur_end = end;
975 else
976 cur_end = min(end, start + 512 * 1024 - 1);
977
978 async_cow->end = cur_end;
979 INIT_LIST_HEAD(&async_cow->extents);
980
981 async_cow->work.func = async_cow_start;
982 async_cow->work.ordered_func = async_cow_submit;
983 async_cow->work.ordered_free = async_cow_free;
984 async_cow->work.flags = 0;
985
Chris Mason771ed682008-11-06 22:02:51 -0500986 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
987 PAGE_CACHE_SHIFT;
988 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
989
990 btrfs_queue_worker(&root->fs_info->delalloc_workers,
991 &async_cow->work);
992
993 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
994 wait_event(root->fs_info->async_submit_wait,
995 (atomic_read(&root->fs_info->async_delalloc_pages) <
996 limit));
997 }
998
Chris Masond3977122009-01-05 21:25:51 -0500999 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -05001000 atomic_read(&root->fs_info->async_delalloc_pages)) {
1001 wait_event(root->fs_info->async_submit_wait,
1002 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1003 0));
1004 }
1005
1006 *nr_written += nr_pages;
1007 start = cur_end + 1;
1008 }
1009 *page_started = 1;
1010 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001011}
1012
Chris Masond3977122009-01-05 21:25:51 -05001013static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001014 u64 bytenr, u64 num_bytes)
1015{
1016 int ret;
1017 struct btrfs_ordered_sum *sums;
1018 LIST_HEAD(list);
1019
Yan Zheng07d400a2009-01-06 11:42:00 -05001020 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
1021 bytenr + num_bytes - 1, &list);
Yan Zheng17d217f2008-12-12 10:03:38 -05001022 if (ret == 0 && list_empty(&list))
1023 return 0;
1024
1025 while (!list_empty(&list)) {
1026 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1027 list_del(&sums->list);
1028 kfree(sums);
1029 }
1030 return 1;
1031}
1032
Chris Masond352ac62008-09-29 15:18:18 -04001033/*
1034 * when nowcow writeback call back. This checks for snapshots or COW copies
1035 * of the extents that exist in the file, and COWs the file as required.
1036 *
1037 * If no cow copies or snapshots exist, we write directly to the existing
1038 * blocks on disk
1039 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001040static noinline int run_delalloc_nocow(struct inode *inode,
1041 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001042 u64 start, u64 end, int *page_started, int force,
1043 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001044{
Chris Masonbe20aa92007-12-17 20:14:01 -05001045 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001046 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001047 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001048 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001049 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001050 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001051 u64 cow_start;
1052 u64 cur_offset;
1053 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001054 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001055 u64 disk_bytenr;
1056 u64 num_bytes;
1057 int extent_type;
1058 int ret;
Yan Zhengd899e052008-10-30 14:25:28 -04001059 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001060 int nocow;
1061 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001062 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001063 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001064
1065 path = btrfs_alloc_path();
1066 BUG_ON(!path);
Li Zefan82d59022011-04-20 10:33:24 +08001067
1068 nolock = is_free_space_inode(root, inode);
1069
1070 if (nolock)
Josef Bacik0cb59c92010-07-02 12:14:14 -04001071 trans = btrfs_join_transaction_nolock(root, 1);
Li Zefan82d59022011-04-20 10:33:24 +08001072 else
Josef Bacik0cb59c92010-07-02 12:14:14 -04001073 trans = btrfs_join_transaction(root, 1);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00001074 BUG_ON(IS_ERR(trans));
Chris Masonbe20aa92007-12-17 20:14:01 -05001075
Yan Zheng80ff3852008-10-30 14:20:02 -04001076 cow_start = (u64)-1;
1077 cur_offset = start;
1078 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001079 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001080 cur_offset, 0);
1081 BUG_ON(ret < 0);
1082 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1083 leaf = path->nodes[0];
1084 btrfs_item_key_to_cpu(leaf, &found_key,
1085 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001086 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001087 found_key.type == BTRFS_EXTENT_DATA_KEY)
1088 path->slots[0]--;
1089 }
1090 check_prev = 0;
1091next_slot:
1092 leaf = path->nodes[0];
1093 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1094 ret = btrfs_next_leaf(root, path);
1095 if (ret < 0)
1096 BUG_ON(1);
1097 if (ret > 0)
1098 break;
1099 leaf = path->nodes[0];
1100 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001101
Yan Zheng80ff3852008-10-30 14:20:02 -04001102 nocow = 0;
1103 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001104 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001105 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001106
Li Zefan33345d012011-04-20 10:31:50 +08001107 if (found_key.objectid > ino ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001108 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1109 found_key.offset > end)
1110 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001111
Yan Zheng80ff3852008-10-30 14:20:02 -04001112 if (found_key.offset > cur_offset) {
1113 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001114 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001115 goto out_check;
1116 }
Chris Masonc31f8832008-01-08 15:46:31 -05001117
Yan Zheng80ff3852008-10-30 14:20:02 -04001118 fi = btrfs_item_ptr(leaf, path->slots[0],
1119 struct btrfs_file_extent_item);
1120 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001121
Yan Zhengd899e052008-10-30 14:25:28 -04001122 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1123 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001124 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001125 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001126 extent_end = found_key.offset +
1127 btrfs_file_extent_num_bytes(leaf, fi);
1128 if (extent_end <= start) {
1129 path->slots[0]++;
1130 goto next_slot;
1131 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001132 if (disk_bytenr == 0)
1133 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001134 if (btrfs_file_extent_compression(leaf, fi) ||
1135 btrfs_file_extent_encryption(leaf, fi) ||
1136 btrfs_file_extent_other_encoding(leaf, fi))
1137 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001138 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1139 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001140 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001141 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001142 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001143 found_key.offset -
1144 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001145 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001146 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001147 disk_bytenr += cur_offset - found_key.offset;
1148 num_bytes = min(end + 1, extent_end) - cur_offset;
1149 /*
1150 * force cow if csum exists in the range.
1151 * this ensure that csum for a given extent are
1152 * either valid or do not exist.
1153 */
1154 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1155 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001156 nocow = 1;
1157 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1158 extent_end = found_key.offset +
1159 btrfs_file_extent_inline_len(leaf, fi);
1160 extent_end = ALIGN(extent_end, root->sectorsize);
1161 } else {
1162 BUG_ON(1);
1163 }
1164out_check:
1165 if (extent_end <= start) {
1166 path->slots[0]++;
1167 goto next_slot;
1168 }
1169 if (!nocow) {
1170 if (cow_start == (u64)-1)
1171 cow_start = cur_offset;
1172 cur_offset = extent_end;
1173 if (cur_offset > end)
1174 break;
1175 path->slots[0]++;
1176 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001177 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001178
Yan Zheng7ea394f2008-08-05 13:05:02 -04001179 btrfs_release_path(root, path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001180 if (cow_start != (u64)-1) {
1181 ret = cow_file_range(inode, locked_page, cow_start,
Chris Mason771ed682008-11-06 22:02:51 -05001182 found_key.offset - 1, page_started,
1183 nr_written, 1);
Yan Zheng80ff3852008-10-30 14:20:02 -04001184 BUG_ON(ret);
1185 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001186 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001187
Yan Zhengd899e052008-10-30 14:25:28 -04001188 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1189 struct extent_map *em;
1190 struct extent_map_tree *em_tree;
1191 em_tree = &BTRFS_I(inode)->extent_tree;
1192 em = alloc_extent_map(GFP_NOFS);
Tsutomu Itohc26a9202011-02-14 00:45:29 +00001193 BUG_ON(!em);
Yan Zhengd899e052008-10-30 14:25:28 -04001194 em->start = cur_offset;
Chris Mason445a6942008-11-10 11:53:33 -05001195 em->orig_start = em->start;
Yan Zhengd899e052008-10-30 14:25:28 -04001196 em->len = num_bytes;
1197 em->block_len = num_bytes;
1198 em->block_start = disk_bytenr;
1199 em->bdev = root->fs_info->fs_devices->latest_bdev;
1200 set_bit(EXTENT_FLAG_PINNED, &em->flags);
1201 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001202 write_lock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001203 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04001204 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001205 if (ret != -EEXIST) {
1206 free_extent_map(em);
1207 break;
1208 }
1209 btrfs_drop_extent_cache(inode, em->start,
1210 em->start + em->len - 1, 0);
1211 }
1212 type = BTRFS_ORDERED_PREALLOC;
1213 } else {
1214 type = BTRFS_ORDERED_NOCOW;
1215 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001216
1217 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001218 num_bytes, num_bytes, type);
1219 BUG_ON(ret);
Chris Mason771ed682008-11-06 22:02:51 -05001220
Yan, Zhengefa56462010-05-16 10:49:59 -04001221 if (root->root_key.objectid ==
1222 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1223 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1224 num_bytes);
1225 BUG_ON(ret);
1226 }
1227
Yan Zhengd899e052008-10-30 14:25:28 -04001228 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
Chris Masona791e352009-10-08 11:27:10 -04001229 cur_offset, cur_offset + num_bytes - 1,
1230 locked_page, EXTENT_CLEAR_UNLOCK_PAGE |
1231 EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
1232 EXTENT_SET_PRIVATE2);
Yan Zheng80ff3852008-10-30 14:20:02 -04001233 cur_offset = extent_end;
1234 if (cur_offset > end)
1235 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001236 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001237 btrfs_release_path(root, path);
1238
1239 if (cur_offset <= end && cow_start == (u64)-1)
1240 cow_start = cur_offset;
1241 if (cow_start != (u64)-1) {
1242 ret = cow_file_range(inode, locked_page, cow_start, end,
Chris Mason771ed682008-11-06 22:02:51 -05001243 page_started, nr_written, 1);
Yan Zheng80ff3852008-10-30 14:20:02 -04001244 BUG_ON(ret);
1245 }
1246
Josef Bacik0cb59c92010-07-02 12:14:14 -04001247 if (nolock) {
1248 ret = btrfs_end_transaction_nolock(trans, root);
1249 BUG_ON(ret);
1250 } else {
1251 ret = btrfs_end_transaction(trans, root);
1252 BUG_ON(ret);
1253 }
Yan Zheng7ea394f2008-08-05 13:05:02 -04001254 btrfs_free_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001255 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001256}
1257
Chris Masond352ac62008-09-29 15:18:18 -04001258/*
1259 * extent_io.c call back to do delayed allocation processing
1260 */
Chris Masonc8b97812008-10-29 14:49:59 -04001261static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001262 u64 start, u64 end, int *page_started,
1263 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001264{
Chris Masonbe20aa92007-12-17 20:14:01 -05001265 int ret;
Chris Mason7f366cf2009-03-12 20:12:45 -04001266 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona2135012008-06-25 16:01:30 -04001267
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001268 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)
Chris Masonc8b97812008-10-29 14:49:59 -04001269 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001270 page_started, 1, nr_written);
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001271 else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC)
Yan Zhengd899e052008-10-30 14:25:28 -04001272 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001273 page_started, 0, nr_written);
Chris Mason1e701a32010-03-11 09:42:04 -05001274 else if (!btrfs_test_opt(root, COMPRESS) &&
Liu Bo75e7cb72011-03-22 10:12:20 +00001275 !(BTRFS_I(inode)->force_compress) &&
1276 !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS))
Chris Mason7f366cf2009-03-12 20:12:45 -04001277 ret = cow_file_range(inode, locked_page, start, end,
1278 page_started, nr_written, 1);
Chris Masonbe20aa92007-12-17 20:14:01 -05001279 else
Chris Mason771ed682008-11-06 22:02:51 -05001280 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001281 page_started, nr_written);
Chris Masonb888db22007-08-27 16:49:44 -04001282 return ret;
1283}
1284
Josef Bacik9ed74f22009-09-11 16:12:44 -04001285static int btrfs_split_extent_hook(struct inode *inode,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001286 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001287{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001288 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001289 if (!(orig->state & EXTENT_DELALLOC))
1290 return 0;
1291
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001292 atomic_inc(&BTRFS_I(inode)->outstanding_extents);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001293 return 0;
1294}
1295
1296/*
1297 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1298 * extents so we can keep track of new extents that are just merged onto old
1299 * extents, such as when we are doing sequential writes, so we can properly
1300 * account for the metadata space we'll need.
1301 */
1302static int btrfs_merge_extent_hook(struct inode *inode,
1303 struct extent_state *new,
1304 struct extent_state *other)
1305{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001306 /* not delalloc, ignore it */
1307 if (!(other->state & EXTENT_DELALLOC))
1308 return 0;
1309
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001310 atomic_dec(&BTRFS_I(inode)->outstanding_extents);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001311 return 0;
1312}
1313
Chris Masond352ac62008-09-29 15:18:18 -04001314/*
1315 * extent_io.c set_bit_hook, used to track delayed allocation
1316 * bytes in this file, and to maintain the list of inodes that
1317 * have pending delalloc work to be done.
1318 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001319static int btrfs_set_bit_hook(struct inode *inode,
1320 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001321{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001322
Chris Mason75eff682008-12-15 15:54:40 -05001323 /*
1324 * set_bit and clear bit hooks normally require _irqsave/restore
1325 * but in this case, we are only testeing for the DELALLOC
1326 * bit, which is only set or cleared with irqs on
1327 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001328 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001329 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001330 u64 len = state->end + 1 - state->start;
Li Zefan82d59022011-04-20 10:33:24 +08001331 bool do_list = !is_free_space_inode(root, inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001332
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001333 if (*bits & EXTENT_FIRST_DELALLOC)
1334 *bits &= ~EXTENT_FIRST_DELALLOC;
1335 else
1336 atomic_inc(&BTRFS_I(inode)->outstanding_extents);
Josef Bacik287a0ab2010-03-19 18:07:23 +00001337
Chris Mason75eff682008-12-15 15:54:40 -05001338 spin_lock(&root->fs_info->delalloc_lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001339 BTRFS_I(inode)->delalloc_bytes += len;
1340 root->fs_info->delalloc_bytes += len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001341 if (do_list && list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
Chris Masonea8c2812008-08-04 23:17:27 -04001342 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1343 &root->fs_info->delalloc_inodes);
1344 }
Chris Mason75eff682008-12-15 15:54:40 -05001345 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001346 }
1347 return 0;
1348}
1349
Chris Masond352ac62008-09-29 15:18:18 -04001350/*
1351 * extent_io.c clear_bit_hook, see set_bit_hook for why
1352 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001353static int btrfs_clear_bit_hook(struct inode *inode,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001354 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001355{
Chris Mason75eff682008-12-15 15:54:40 -05001356 /*
1357 * set_bit and clear bit hooks normally require _irqsave/restore
1358 * but in this case, we are only testeing for the DELALLOC
1359 * bit, which is only set or cleared with irqs on
1360 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001361 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001362 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001363 u64 len = state->end + 1 - state->start;
Li Zefan82d59022011-04-20 10:33:24 +08001364 bool do_list = !is_free_space_inode(root, inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001365
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001366 if (*bits & EXTENT_FIRST_DELALLOC)
1367 *bits &= ~EXTENT_FIRST_DELALLOC;
1368 else if (!(*bits & EXTENT_DO_ACCOUNTING))
1369 atomic_dec(&BTRFS_I(inode)->outstanding_extents);
1370
1371 if (*bits & EXTENT_DO_ACCOUNTING)
1372 btrfs_delalloc_release_metadata(inode, len);
1373
Josef Bacik0cb59c92010-07-02 12:14:14 -04001374 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
1375 && do_list)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001376 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001377
Chris Mason75eff682008-12-15 15:54:40 -05001378 spin_lock(&root->fs_info->delalloc_lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001379 root->fs_info->delalloc_bytes -= len;
1380 BTRFS_I(inode)->delalloc_bytes -= len;
1381
Josef Bacik0cb59c92010-07-02 12:14:14 -04001382 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Chris Masonea8c2812008-08-04 23:17:27 -04001383 !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1384 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1385 }
Chris Mason75eff682008-12-15 15:54:40 -05001386 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001387 }
1388 return 0;
1389}
1390
Chris Masond352ac62008-09-29 15:18:18 -04001391/*
1392 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1393 * we don't create bios that span stripes or chunks
1394 */
Chris Mason239b14b2008-03-24 15:02:07 -04001395int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001396 size_t size, struct bio *bio,
1397 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001398{
1399 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
1400 struct btrfs_mapping_tree *map_tree;
Chris Masona62b9402008-10-03 16:31:08 -04001401 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001402 u64 length = 0;
1403 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001404 int ret;
1405
Chris Mason771ed682008-11-06 22:02:51 -05001406 if (bio_flags & EXTENT_BIO_COMPRESSED)
1407 return 0;
1408
Chris Masonf2d8d742008-04-21 10:03:05 -04001409 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001410 map_tree = &root->fs_info->mapping_tree;
1411 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04001412 ret = btrfs_map_block(map_tree, READ, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001413 &map_length, NULL, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04001414
Chris Masond3977122009-01-05 21:25:51 -05001415 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001416 return 1;
Andi Kleen411fc6b2010-10-29 15:14:31 -04001417 return ret;
Chris Mason239b14b2008-03-24 15:02:07 -04001418}
1419
Chris Masond352ac62008-09-29 15:18:18 -04001420/*
1421 * in order to insert checksums into the metadata in large chunks,
1422 * we wait until bio submission time. All the pages in the bio are
1423 * checksummed and sums are attached onto the ordered extent record.
1424 *
1425 * At IO completion time the cums attached on the ordered extent record
1426 * are inserted into the btree
1427 */
Chris Masond3977122009-01-05 21:25:51 -05001428static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1429 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001430 unsigned long bio_flags,
1431 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001432{
Chris Mason065631f2008-02-20 12:07:25 -05001433 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001434 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001435
Chris Masond20f7042008-12-08 16:58:54 -05001436 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Chris Mason44b8bd72008-04-16 11:14:51 -04001437 BUG_ON(ret);
Chris Mason4a69a412008-11-06 22:03:00 -05001438 return 0;
1439}
Chris Masone0156402008-04-16 11:15:20 -04001440
Chris Mason4a69a412008-11-06 22:03:00 -05001441/*
1442 * in order to insert checksums into the metadata in large chunks,
1443 * we wait until bio submission time. All the pages in the bio are
1444 * checksummed and sums are attached onto the ordered extent record.
1445 *
1446 * At IO completion time the cums attached on the ordered extent record
1447 * are inserted into the btree
1448 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001449static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001450 int mirror_num, unsigned long bio_flags,
1451 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001452{
1453 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason8b712842008-06-11 16:50:36 -04001454 return btrfs_map_bio(root, rw, bio, mirror_num, 1);
Chris Mason44b8bd72008-04-16 11:14:51 -04001455}
1456
Chris Masond352ac62008-09-29 15:18:18 -04001457/*
Chris Masoncad321a2008-12-17 14:51:42 -05001458 * extent_io.c submission hook. This does the right thing for csum calculation
1459 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001460 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001461static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001462 int mirror_num, unsigned long bio_flags,
1463 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001464{
1465 struct btrfs_root *root = BTRFS_I(inode)->root;
1466 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001467 int skip_sum;
Chris Mason44b8bd72008-04-16 11:14:51 -04001468
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001469 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001470
Li Zefan82d59022011-04-20 10:33:24 +08001471 if (is_free_space_inode(root, inode))
Josef Bacik0cb59c92010-07-02 12:14:14 -04001472 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 2);
1473 else
1474 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001475 BUG_ON(ret);
Chris Mason065631f2008-02-20 12:07:25 -05001476
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001477 if (!(rw & REQ_WRITE)) {
Chris Masond20f7042008-12-08 16:58:54 -05001478 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Chris Masonc8b97812008-10-29 14:49:59 -04001479 return btrfs_submit_compressed_read(inode, bio,
1480 mirror_num, bio_flags);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001481 } else if (!skip_sum) {
1482 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1483 if (ret)
1484 return ret;
1485 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001486 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001487 } else if (!skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001488 /* csum items have already been cloned */
1489 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1490 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001491 /* we're doing a write, do the async checksumming */
1492 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001493 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001494 bio_flags, bio_offset,
1495 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001496 __btrfs_submit_bio_done);
Chris Mason19b9bdb2008-10-30 14:23:13 -04001497 }
1498
Chris Mason0b86a832008-03-24 15:01:56 -04001499mapit:
Chris Mason8b712842008-06-11 16:50:36 -04001500 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
Chris Mason065631f2008-02-20 12:07:25 -05001501}
Chris Mason6885f302008-02-20 16:11:05 -05001502
Chris Masond352ac62008-09-29 15:18:18 -04001503/*
1504 * given a list of ordered sums record them in the inode. This happens
1505 * at IO completion time based on sums calculated at bio submission time.
1506 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001507static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001508 struct inode *inode, u64 file_offset,
1509 struct list_head *list)
1510{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001511 struct btrfs_ordered_sum *sum;
1512
1513 btrfs_set_trans_block_group(trans, inode);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001514
1515 list_for_each_entry(sum, list, list) {
Chris Masond20f7042008-12-08 16:58:54 -05001516 btrfs_csum_file_blocks(trans,
1517 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001518 }
1519 return 0;
1520}
1521
Josef Bacik2ac55d42010-02-03 19:33:23 +00001522int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1523 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001524{
Chris Masond3977122009-01-05 21:25:51 -05001525 if ((end & (PAGE_CACHE_SIZE - 1)) == 0)
Chris Mason771ed682008-11-06 22:02:51 -05001526 WARN_ON(1);
Chris Masonea8c2812008-08-04 23:17:27 -04001527 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001528 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001529}
1530
Chris Masond352ac62008-09-29 15:18:18 -04001531/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001532struct btrfs_writepage_fixup {
1533 struct page *page;
1534 struct btrfs_work work;
1535};
1536
Christoph Hellwigb2950862008-12-02 09:54:17 -05001537static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001538{
1539 struct btrfs_writepage_fixup *fixup;
1540 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001541 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001542 struct page *page;
1543 struct inode *inode;
1544 u64 page_start;
1545 u64 page_end;
1546
1547 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1548 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001549again:
Chris Mason247e7432008-07-17 12:53:51 -04001550 lock_page(page);
1551 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1552 ClearPageChecked(page);
1553 goto out_page;
1554 }
1555
1556 inode = page->mapping->host;
1557 page_start = page_offset(page);
1558 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1559
Josef Bacik2ac55d42010-02-03 19:33:23 +00001560 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
1561 &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001562
1563 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001564 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001565 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001566
1567 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1568 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001569 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1570 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001571 unlock_page(page);
1572 btrfs_start_ordered_extent(inode, ordered, 1);
1573 goto again;
1574 }
Chris Mason247e7432008-07-17 12:53:51 -04001575
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001576 BUG();
Josef Bacik2ac55d42010-02-03 19:33:23 +00001577 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001578 ClearPageChecked(page);
1579out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001580 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1581 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001582out_page:
1583 unlock_page(page);
1584 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08001585 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04001586}
1587
1588/*
1589 * There are a few paths in the higher layers of the kernel that directly
1590 * set the page dirty bit without asking the filesystem if it is a
1591 * good idea. This causes problems because we want to make sure COW
1592 * properly happens and the data=ordered rules are followed.
1593 *
Chris Masonc8b97812008-10-29 14:49:59 -04001594 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001595 * hasn't been properly setup for IO. We kick off an async process
1596 * to fix it up. The async helper will wait for ordered extents, set
1597 * the delalloc bit and make it safe to write the page.
1598 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001599static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001600{
1601 struct inode *inode = page->mapping->host;
1602 struct btrfs_writepage_fixup *fixup;
1603 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001604
Chris Mason8b62b722009-09-02 16:53:46 -04001605 /* this page is properly in the ordered list */
1606 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001607 return 0;
1608
1609 if (PageChecked(page))
1610 return -EAGAIN;
1611
1612 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1613 if (!fixup)
1614 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001615
Chris Mason247e7432008-07-17 12:53:51 -04001616 SetPageChecked(page);
1617 page_cache_get(page);
1618 fixup->work.func = btrfs_writepage_fixup_worker;
1619 fixup->page = page;
1620 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
1621 return -EAGAIN;
1622}
1623
Yan Zhengd899e052008-10-30 14:25:28 -04001624static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1625 struct inode *inode, u64 file_pos,
1626 u64 disk_bytenr, u64 disk_num_bytes,
1627 u64 num_bytes, u64 ram_bytes,
1628 u8 compression, u8 encryption,
1629 u16 other_encoding, int extent_type)
1630{
1631 struct btrfs_root *root = BTRFS_I(inode)->root;
1632 struct btrfs_file_extent_item *fi;
1633 struct btrfs_path *path;
1634 struct extent_buffer *leaf;
1635 struct btrfs_key ins;
1636 u64 hint;
1637 int ret;
1638
1639 path = btrfs_alloc_path();
1640 BUG_ON(!path);
1641
Chris Masonb9473432009-03-13 11:00:37 -04001642 path->leave_spinning = 1;
Chris Masona1ed8352009-09-11 12:27:37 -04001643
1644 /*
1645 * we may be replacing one extent in the tree with another.
1646 * The new extent is pinned in the extent map, and we don't want
1647 * to drop it from the cache until it is completely in the btree.
1648 *
1649 * So, tell btrfs_drop_extents to leave this extent in the cache.
1650 * the caller is expected to unpin it and allow it to be merged
1651 * with the others.
1652 */
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001653 ret = btrfs_drop_extents(trans, inode, file_pos, file_pos + num_bytes,
1654 &hint, 0);
Yan Zhengd899e052008-10-30 14:25:28 -04001655 BUG_ON(ret);
1656
Li Zefan33345d012011-04-20 10:31:50 +08001657 ins.objectid = btrfs_ino(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04001658 ins.offset = file_pos;
1659 ins.type = BTRFS_EXTENT_DATA_KEY;
1660 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
1661 BUG_ON(ret);
1662 leaf = path->nodes[0];
1663 fi = btrfs_item_ptr(leaf, path->slots[0],
1664 struct btrfs_file_extent_item);
1665 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1666 btrfs_set_file_extent_type(leaf, fi, extent_type);
1667 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1668 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1669 btrfs_set_file_extent_offset(leaf, fi, 0);
1670 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1671 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1672 btrfs_set_file_extent_compression(leaf, fi, compression);
1673 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1674 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04001675
1676 btrfs_unlock_up_safe(path, 1);
1677 btrfs_set_lock_blocking(leaf);
1678
Yan Zhengd899e052008-10-30 14:25:28 -04001679 btrfs_mark_buffer_dirty(leaf);
1680
1681 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04001682
1683 ins.objectid = disk_bytenr;
1684 ins.offset = disk_num_bytes;
1685 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001686 ret = btrfs_alloc_reserved_file_extent(trans, root,
1687 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08001688 btrfs_ino(inode), file_pos, &ins);
Yan Zhengd899e052008-10-30 14:25:28 -04001689 BUG_ON(ret);
Yan Zhengd899e052008-10-30 14:25:28 -04001690 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04001691
Yan Zhengd899e052008-10-30 14:25:28 -04001692 return 0;
1693}
1694
Chris Mason5d13a982009-03-13 11:41:46 -04001695/*
1696 * helper function for btrfs_finish_ordered_io, this
1697 * just reads in some of the csum leaves to prime them into ram
1698 * before we start the transaction. It limits the amount of btree
1699 * reads required while inside the transaction.
1700 */
Chris Masond352ac62008-09-29 15:18:18 -04001701/* as ordered data IO finishes, this gets called so we can finish
1702 * an ordered extent if the range of bytes in the file it covers are
1703 * fully written.
1704 */
Chris Mason211f90e2008-07-18 11:56:15 -04001705static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001706{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001707 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001708 struct btrfs_trans_handle *trans = NULL;
Chris Mason5d13a982009-03-13 11:41:46 -04001709 struct btrfs_ordered_extent *ordered_extent = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001710 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001711 struct extent_state *cached_state = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08001712 int compress_type = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001713 int ret;
Li Zefan82d59022011-04-20 10:33:24 +08001714 bool nolock;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001715
Josef Bacik5a1a3df2010-02-02 20:51:14 +00001716 ret = btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
1717 end - start + 1);
Chris Masonba1da2f2008-07-17 12:54:15 -04001718 if (!ret)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001719 return 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001720 BUG_ON(!ordered_extent);
Josef Bacikefd049f2010-02-02 20:50:10 +00001721
Li Zefan82d59022011-04-20 10:33:24 +08001722 nolock = is_free_space_inode(root, inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001723
Yan, Zhengc2167752009-11-12 09:34:21 +00001724 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
1725 BUG_ON(!list_empty(&ordered_extent->list));
1726 ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1727 if (!ret) {
Josef Bacik0cb59c92010-07-02 12:14:14 -04001728 if (nolock)
1729 trans = btrfs_join_transaction_nolock(root, 1);
1730 else
1731 trans = btrfs_join_transaction(root, 1);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00001732 BUG_ON(IS_ERR(trans));
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001733 btrfs_set_trans_block_group(trans, inode);
1734 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00001735 ret = btrfs_update_inode(trans, root, inode);
1736 BUG_ON(ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00001737 }
1738 goto out;
1739 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04001740
Josef Bacik2ac55d42010-02-03 19:33:23 +00001741 lock_extent_bits(io_tree, ordered_extent->file_offset,
1742 ordered_extent->file_offset + ordered_extent->len - 1,
1743 0, &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001744
Josef Bacik0cb59c92010-07-02 12:14:14 -04001745 if (nolock)
1746 trans = btrfs_join_transaction_nolock(root, 1);
1747 else
1748 trans = btrfs_join_transaction(root, 1);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00001749 BUG_ON(IS_ERR(trans));
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001750 btrfs_set_trans_block_group(trans, inode);
1751 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00001752
Chris Masonc8b97812008-10-29 14:49:59 -04001753 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08001754 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04001755 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08001756 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001757 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04001758 ordered_extent->file_offset,
1759 ordered_extent->file_offset +
1760 ordered_extent->len);
1761 BUG_ON(ret);
1762 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04001763 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04001764 ret = insert_reserved_file_extent(trans, inode,
1765 ordered_extent->file_offset,
1766 ordered_extent->start,
1767 ordered_extent->disk_len,
1768 ordered_extent->len,
1769 ordered_extent->len,
Li Zefan261507a02010-12-17 14:21:50 +08001770 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04001771 BTRFS_FILE_EXTENT_REG);
Chris Masona1ed8352009-09-11 12:27:37 -04001772 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
1773 ordered_extent->file_offset,
1774 ordered_extent->len);
Yan Zhengd899e052008-10-30 14:25:28 -04001775 BUG_ON(ret);
1776 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00001777 unlock_extent_cached(io_tree, ordered_extent->file_offset,
1778 ordered_extent->file_offset +
1779 ordered_extent->len - 1, &cached_state, GFP_NOFS);
1780
Chris Masone6dcd2d2008-07-17 12:53:50 -04001781 add_pending_csums(trans, inode, ordered_extent->file_offset,
1782 &ordered_extent->list);
1783
Josef Bacik1ef30be2011-04-05 19:25:36 -04001784 ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1785 if (!ret) {
1786 ret = btrfs_update_inode(trans, root, inode);
1787 BUG_ON(ret);
1788 }
1789 ret = 0;
Yan, Zhengc2167752009-11-12 09:34:21 +00001790out:
Josef Bacik0cb59c92010-07-02 12:14:14 -04001791 if (nolock) {
1792 if (trans)
1793 btrfs_end_transaction_nolock(trans, root);
1794 } else {
1795 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
1796 if (trans)
1797 btrfs_end_transaction(trans, root);
1798 }
1799
Chris Masone6dcd2d2008-07-17 12:53:50 -04001800 /* once for us */
1801 btrfs_put_ordered_extent(ordered_extent);
1802 /* once for the tree */
1803 btrfs_put_ordered_extent(ordered_extent);
1804
Chris Masone6dcd2d2008-07-17 12:53:50 -04001805 return 0;
1806}
1807
Christoph Hellwigb2950862008-12-02 09:54:17 -05001808static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04001809 struct extent_state *state, int uptodate)
1810{
liubo1abe9b82011-03-24 11:18:59 +00001811 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
1812
Chris Mason8b62b722009-09-02 16:53:46 -04001813 ClearPagePrivate2(page);
Chris Mason211f90e2008-07-18 11:56:15 -04001814 return btrfs_finish_ordered_io(page->mapping->host, start, end);
1815}
1816
Chris Masond352ac62008-09-29 15:18:18 -04001817/*
1818 * When IO fails, either with EIO or csum verification fails, we
1819 * try other mirrors that might have a good copy of the data. This
1820 * io_failure_record is used to record state as we go through all the
1821 * mirrors. If another mirror has good data, the page is set up to date
1822 * and things continue. If a good mirror can't be found, the original
1823 * bio end_io callback is called to indicate things have failed.
1824 */
Chris Mason7e383262008-04-09 16:28:12 -04001825struct io_failure_record {
1826 struct page *page;
1827 u64 start;
1828 u64 len;
1829 u64 logical;
Chris Masond20f7042008-12-08 16:58:54 -05001830 unsigned long bio_flags;
Chris Mason7e383262008-04-09 16:28:12 -04001831 int last_mirror;
1832};
1833
Christoph Hellwigb2950862008-12-02 09:54:17 -05001834static int btrfs_io_failed_hook(struct bio *failed_bio,
Chris Mason1259ab72008-05-12 13:39:03 -04001835 struct page *page, u64 start, u64 end,
1836 struct extent_state *state)
Chris Mason7e383262008-04-09 16:28:12 -04001837{
1838 struct io_failure_record *failrec = NULL;
1839 u64 private;
1840 struct extent_map *em;
1841 struct inode *inode = page->mapping->host;
1842 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Chris Mason3b951512008-04-17 11:29:12 -04001843 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason7e383262008-04-09 16:28:12 -04001844 struct bio *bio;
1845 int num_copies;
1846 int ret;
Chris Mason1259ab72008-05-12 13:39:03 -04001847 int rw;
Chris Mason7e383262008-04-09 16:28:12 -04001848 u64 logical;
1849
1850 ret = get_state_private(failure_tree, start, &private);
1851 if (ret) {
Chris Mason7e383262008-04-09 16:28:12 -04001852 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
1853 if (!failrec)
1854 return -ENOMEM;
1855 failrec->start = start;
1856 failrec->len = end - start + 1;
1857 failrec->last_mirror = 0;
Chris Masond20f7042008-12-08 16:58:54 -05001858 failrec->bio_flags = 0;
Chris Mason7e383262008-04-09 16:28:12 -04001859
Chris Mason890871b2009-09-02 16:24:52 -04001860 read_lock(&em_tree->lock);
Chris Mason3b951512008-04-17 11:29:12 -04001861 em = lookup_extent_mapping(em_tree, start, failrec->len);
1862 if (em->start > start || em->start + em->len < start) {
1863 free_extent_map(em);
1864 em = NULL;
1865 }
Chris Mason890871b2009-09-02 16:24:52 -04001866 read_unlock(&em_tree->lock);
Chris Mason7e383262008-04-09 16:28:12 -04001867
1868 if (!em || IS_ERR(em)) {
1869 kfree(failrec);
1870 return -EIO;
1871 }
1872 logical = start - em->start;
1873 logical = em->block_start + logical;
Chris Masond20f7042008-12-08 16:58:54 -05001874 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
1875 logical = em->block_start;
1876 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
Li Zefan261507a02010-12-17 14:21:50 +08001877 extent_set_compress_type(&failrec->bio_flags,
1878 em->compress_type);
Chris Masond20f7042008-12-08 16:58:54 -05001879 }
Chris Mason7e383262008-04-09 16:28:12 -04001880 failrec->logical = logical;
1881 free_extent_map(em);
1882 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
1883 EXTENT_DIRTY, GFP_NOFS);
Chris Mason587f7702008-04-11 12:16:46 -04001884 set_state_private(failure_tree, start,
1885 (u64)(unsigned long)failrec);
Chris Mason7e383262008-04-09 16:28:12 -04001886 } else {
Chris Mason587f7702008-04-11 12:16:46 -04001887 failrec = (struct io_failure_record *)(unsigned long)private;
Chris Mason7e383262008-04-09 16:28:12 -04001888 }
1889 num_copies = btrfs_num_copies(
1890 &BTRFS_I(inode)->root->fs_info->mapping_tree,
1891 failrec->logical, failrec->len);
1892 failrec->last_mirror++;
1893 if (!state) {
Chris Masoncad321a2008-12-17 14:51:42 -05001894 spin_lock(&BTRFS_I(inode)->io_tree.lock);
Chris Mason7e383262008-04-09 16:28:12 -04001895 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
1896 failrec->start,
1897 EXTENT_LOCKED);
1898 if (state && state->start != failrec->start)
1899 state = NULL;
Chris Masoncad321a2008-12-17 14:51:42 -05001900 spin_unlock(&BTRFS_I(inode)->io_tree.lock);
Chris Mason7e383262008-04-09 16:28:12 -04001901 }
1902 if (!state || failrec->last_mirror > num_copies) {
1903 set_state_private(failure_tree, failrec->start, 0);
1904 clear_extent_bits(failure_tree, failrec->start,
1905 failrec->start + failrec->len - 1,
1906 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
1907 kfree(failrec);
1908 return -EIO;
1909 }
1910 bio = bio_alloc(GFP_NOFS, 1);
1911 bio->bi_private = state;
1912 bio->bi_end_io = failed_bio->bi_end_io;
1913 bio->bi_sector = failrec->logical >> 9;
1914 bio->bi_bdev = failed_bio->bi_bdev;
Chris Masone1c4b742008-04-22 13:26:46 -04001915 bio->bi_size = 0;
Chris Masond20f7042008-12-08 16:58:54 -05001916
Chris Mason7e383262008-04-09 16:28:12 -04001917 bio_add_page(bio, page, failrec->len, start - page_offset(page));
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001918 if (failed_bio->bi_rw & REQ_WRITE)
Chris Mason1259ab72008-05-12 13:39:03 -04001919 rw = WRITE;
1920 else
1921 rw = READ;
1922
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001923 ret = BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
Chris Masonc8b97812008-10-29 14:49:59 -04001924 failrec->last_mirror,
Chris Masoneaf25d92010-05-25 09:48:28 -04001925 failrec->bio_flags, 0);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001926 return ret;
Chris Mason1259ab72008-05-12 13:39:03 -04001927}
1928
Chris Masond352ac62008-09-29 15:18:18 -04001929/*
1930 * each time an IO finishes, we do a fast check in the IO failure tree
1931 * to see if we need to process or clean up an io_failure_record
1932 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001933static int btrfs_clean_io_failures(struct inode *inode, u64 start)
Chris Mason1259ab72008-05-12 13:39:03 -04001934{
1935 u64 private;
1936 u64 private_failure;
1937 struct io_failure_record *failure;
1938 int ret;
1939
1940 private = 0;
1941 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
Chris Masonec29ed52011-02-23 16:23:20 -05001942 (u64)-1, 1, EXTENT_DIRTY, 0)) {
Chris Mason1259ab72008-05-12 13:39:03 -04001943 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
1944 start, &private_failure);
1945 if (ret == 0) {
1946 failure = (struct io_failure_record *)(unsigned long)
1947 private_failure;
1948 set_state_private(&BTRFS_I(inode)->io_failure_tree,
1949 failure->start, 0);
1950 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
1951 failure->start,
1952 failure->start + failure->len - 1,
1953 EXTENT_DIRTY | EXTENT_LOCKED,
1954 GFP_NOFS);
1955 kfree(failure);
1956 }
1957 }
Chris Mason7e383262008-04-09 16:28:12 -04001958 return 0;
1959}
1960
Chris Masond352ac62008-09-29 15:18:18 -04001961/*
1962 * when reads are done, we need to check csums to verify the data is correct
1963 * if there's a match, we allow the bio to finish. If not, we go through
1964 * the io_failure_record routines to find good copies
1965 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001966static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason70dec802008-01-29 09:59:12 -05001967 struct extent_state *state)
Chris Mason07157aa2007-08-30 08:50:51 -04001968{
Chris Mason35ebb932007-10-30 16:56:53 -04001969 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -04001970 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05001971 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04001972 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -05001973 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -04001974 int ret;
Chris Masonff79f812007-10-15 16:22:25 -04001975 struct btrfs_root *root = BTRFS_I(inode)->root;
1976 u32 csum = ~(u32)0;
Chris Masond1310b22008-01-24 16:13:08 -05001977
Chris Masond20f7042008-12-08 16:58:54 -05001978 if (PageChecked(page)) {
1979 ClearPageChecked(page);
1980 goto good;
1981 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001982
1983 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Chris Masonb6cda9b2007-12-14 15:30:32 -05001984 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05001985
Yan Zheng17d217f2008-12-12 10:03:38 -05001986 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04001987 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001988 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
1989 GFP_NOFS);
1990 return 0;
1991 }
1992
Yanc2e639f2008-02-04 08:57:25 -05001993 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -05001994 private = state->private;
1995 ret = 0;
1996 } else {
1997 ret = get_state_private(io_tree, start, &private);
1998 }
Chris Mason9ab86c82009-01-07 09:48:51 -05001999 kaddr = kmap_atomic(page, KM_USER0);
Chris Masond3977122009-01-05 21:25:51 -05002000 if (ret)
Chris Mason07157aa2007-08-30 08:50:51 -04002001 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002002
Chris Masonff79f812007-10-15 16:22:25 -04002003 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
2004 btrfs_csum_final(csum, (char *)&csum);
Chris Masond3977122009-01-05 21:25:51 -05002005 if (csum != private)
Chris Mason07157aa2007-08-30 08:50:51 -04002006 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002007
Chris Mason9ab86c82009-01-07 09:48:51 -05002008 kunmap_atomic(kaddr, KM_USER0);
Chris Masond20f7042008-12-08 16:58:54 -05002009good:
Chris Mason7e383262008-04-09 16:28:12 -04002010 /* if the io failure tree for this inode is non-empty,
2011 * check to see if we've recovered from a failed IO
2012 */
Chris Mason1259ab72008-05-12 13:39:03 -04002013 btrfs_clean_io_failures(inode, start);
Chris Mason07157aa2007-08-30 08:50:51 -04002014 return 0;
2015
2016zeroit:
Chris Mason193f2842009-04-27 07:29:05 -04002017 if (printk_ratelimit()) {
Li Zefan33345d012011-04-20 10:31:50 +08002018 printk(KERN_INFO "btrfs csum failed ino %llu off %llu csum %u "
2019 "private %llu\n",
2020 (unsigned long long)btrfs_ino(page->mapping->host),
Chris Mason193f2842009-04-27 07:29:05 -04002021 (unsigned long long)start, csum,
2022 (unsigned long long)private);
2023 }
Chris Masondb945352007-10-15 16:15:53 -04002024 memset(kaddr + offset, 1, end - start + 1);
2025 flush_dcache_page(page);
Chris Mason9ab86c82009-01-07 09:48:51 -05002026 kunmap_atomic(kaddr, KM_USER0);
Chris Mason3b951512008-04-17 11:29:12 -04002027 if (private == 0)
2028 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04002029 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04002030}
Chris Masonb888db22007-08-27 16:49:44 -04002031
Yan, Zheng24bbcf042009-11-12 09:36:34 +00002032struct delayed_iput {
2033 struct list_head list;
2034 struct inode *inode;
2035};
2036
2037void btrfs_add_delayed_iput(struct inode *inode)
2038{
2039 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2040 struct delayed_iput *delayed;
2041
2042 if (atomic_add_unless(&inode->i_count, -1, 1))
2043 return;
2044
2045 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2046 delayed->inode = inode;
2047
2048 spin_lock(&fs_info->delayed_iput_lock);
2049 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2050 spin_unlock(&fs_info->delayed_iput_lock);
2051}
2052
2053void btrfs_run_delayed_iputs(struct btrfs_root *root)
2054{
2055 LIST_HEAD(list);
2056 struct btrfs_fs_info *fs_info = root->fs_info;
2057 struct delayed_iput *delayed;
2058 int empty;
2059
2060 spin_lock(&fs_info->delayed_iput_lock);
2061 empty = list_empty(&fs_info->delayed_iputs);
2062 spin_unlock(&fs_info->delayed_iput_lock);
2063 if (empty)
2064 return;
2065
2066 down_read(&root->fs_info->cleanup_work_sem);
2067 spin_lock(&fs_info->delayed_iput_lock);
2068 list_splice_init(&fs_info->delayed_iputs, &list);
2069 spin_unlock(&fs_info->delayed_iput_lock);
2070
2071 while (!list_empty(&list)) {
2072 delayed = list_entry(list.next, struct delayed_iput, list);
2073 list_del(&delayed->list);
2074 iput(delayed->inode);
2075 kfree(delayed);
2076 }
2077 up_read(&root->fs_info->cleanup_work_sem);
2078}
2079
Josef Bacik7b128762008-07-24 12:17:14 -04002080/*
Yan, Zhengd68fc572010-05-16 10:49:58 -04002081 * calculate extra metadata reservation when snapshotting a subvolume
2082 * contains orphan files.
2083 */
2084void btrfs_orphan_pre_snapshot(struct btrfs_trans_handle *trans,
2085 struct btrfs_pending_snapshot *pending,
2086 u64 *bytes_to_reserve)
2087{
2088 struct btrfs_root *root;
2089 struct btrfs_block_rsv *block_rsv;
2090 u64 num_bytes;
2091 int index;
2092
2093 root = pending->root;
2094 if (!root->orphan_block_rsv || list_empty(&root->orphan_list))
2095 return;
2096
2097 block_rsv = root->orphan_block_rsv;
2098
2099 /* orphan block reservation for the snapshot */
2100 num_bytes = block_rsv->size;
2101
2102 /*
2103 * after the snapshot is created, COWing tree blocks may use more
2104 * space than it frees. So we should make sure there is enough
2105 * reserved space.
2106 */
2107 index = trans->transid & 0x1;
2108 if (block_rsv->reserved + block_rsv->freed[index] < block_rsv->size) {
2109 num_bytes += block_rsv->size -
2110 (block_rsv->reserved + block_rsv->freed[index]);
2111 }
2112
2113 *bytes_to_reserve += num_bytes;
2114}
2115
2116void btrfs_orphan_post_snapshot(struct btrfs_trans_handle *trans,
2117 struct btrfs_pending_snapshot *pending)
2118{
2119 struct btrfs_root *root = pending->root;
2120 struct btrfs_root *snap = pending->snap;
2121 struct btrfs_block_rsv *block_rsv;
2122 u64 num_bytes;
2123 int index;
2124 int ret;
2125
2126 if (!root->orphan_block_rsv || list_empty(&root->orphan_list))
2127 return;
2128
2129 /* refill source subvolume's orphan block reservation */
2130 block_rsv = root->orphan_block_rsv;
2131 index = trans->transid & 0x1;
2132 if (block_rsv->reserved + block_rsv->freed[index] < block_rsv->size) {
2133 num_bytes = block_rsv->size -
2134 (block_rsv->reserved + block_rsv->freed[index]);
2135 ret = btrfs_block_rsv_migrate(&pending->block_rsv,
2136 root->orphan_block_rsv,
2137 num_bytes);
2138 BUG_ON(ret);
2139 }
2140
2141 /* setup orphan block reservation for the snapshot */
2142 block_rsv = btrfs_alloc_block_rsv(snap);
2143 BUG_ON(!block_rsv);
2144
2145 btrfs_add_durable_block_rsv(root->fs_info, block_rsv);
2146 snap->orphan_block_rsv = block_rsv;
2147
2148 num_bytes = root->orphan_block_rsv->size;
2149 ret = btrfs_block_rsv_migrate(&pending->block_rsv,
2150 block_rsv, num_bytes);
2151 BUG_ON(ret);
2152
2153#if 0
2154 /* insert orphan item for the snapshot */
2155 WARN_ON(!root->orphan_item_inserted);
2156 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
2157 snap->root_key.objectid);
2158 BUG_ON(ret);
2159 snap->orphan_item_inserted = 1;
2160#endif
2161}
2162
2163enum btrfs_orphan_cleanup_state {
2164 ORPHAN_CLEANUP_STARTED = 1,
2165 ORPHAN_CLEANUP_DONE = 2,
2166};
2167
2168/*
2169 * This is called in transaction commmit time. If there are no orphan
2170 * files in the subvolume, it removes orphan item and frees block_rsv
2171 * structure.
2172 */
2173void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
2174 struct btrfs_root *root)
2175{
2176 int ret;
2177
2178 if (!list_empty(&root->orphan_list) ||
2179 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
2180 return;
2181
2182 if (root->orphan_item_inserted &&
2183 btrfs_root_refs(&root->root_item) > 0) {
2184 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
2185 root->root_key.objectid);
2186 BUG_ON(ret);
2187 root->orphan_item_inserted = 0;
2188 }
2189
2190 if (root->orphan_block_rsv) {
2191 WARN_ON(root->orphan_block_rsv->size > 0);
2192 btrfs_free_block_rsv(root, root->orphan_block_rsv);
2193 root->orphan_block_rsv = NULL;
2194 }
2195}
2196
2197/*
Josef Bacik7b128762008-07-24 12:17:14 -04002198 * This creates an orphan entry for the given inode in case something goes
2199 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04002200 *
2201 * NOTE: caller of this function should reserve 5 units of metadata for
2202 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04002203 */
2204int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2205{
2206 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002207 struct btrfs_block_rsv *block_rsv = NULL;
2208 int reserve = 0;
2209 int insert = 0;
2210 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002211
Yan, Zhengd68fc572010-05-16 10:49:58 -04002212 if (!root->orphan_block_rsv) {
2213 block_rsv = btrfs_alloc_block_rsv(root);
2214 BUG_ON(!block_rsv);
Josef Bacik7b128762008-07-24 12:17:14 -04002215 }
2216
Yan, Zhengd68fc572010-05-16 10:49:58 -04002217 spin_lock(&root->orphan_lock);
2218 if (!root->orphan_block_rsv) {
2219 root->orphan_block_rsv = block_rsv;
2220 } else if (block_rsv) {
2221 btrfs_free_block_rsv(root, block_rsv);
2222 block_rsv = NULL;
2223 }
Josef Bacik7b128762008-07-24 12:17:14 -04002224
Yan, Zhengd68fc572010-05-16 10:49:58 -04002225 if (list_empty(&BTRFS_I(inode)->i_orphan)) {
2226 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
2227#if 0
2228 /*
2229 * For proper ENOSPC handling, we should do orphan
2230 * cleanup when mounting. But this introduces backward
2231 * compatibility issue.
2232 */
2233 if (!xchg(&root->orphan_item_inserted, 1))
2234 insert = 2;
2235 else
2236 insert = 1;
2237#endif
2238 insert = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002239 }
Josef Bacik7b128762008-07-24 12:17:14 -04002240
Yan, Zhengd68fc572010-05-16 10:49:58 -04002241 if (!BTRFS_I(inode)->orphan_meta_reserved) {
2242 BTRFS_I(inode)->orphan_meta_reserved = 1;
2243 reserve = 1;
2244 }
2245 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002246
Yan, Zhengd68fc572010-05-16 10:49:58 -04002247 if (block_rsv)
2248 btrfs_add_durable_block_rsv(root->fs_info, block_rsv);
2249
2250 /* grab metadata reservation from transaction handle */
2251 if (reserve) {
2252 ret = btrfs_orphan_reserve_metadata(trans, inode);
2253 BUG_ON(ret);
2254 }
2255
2256 /* insert an orphan item to track this unlinked/truncated file */
2257 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08002258 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Yan, Zhengd68fc572010-05-16 10:49:58 -04002259 BUG_ON(ret);
2260 }
2261
2262 /* insert an orphan item to track subvolume contains orphan files */
2263 if (insert >= 2) {
2264 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
2265 root->root_key.objectid);
2266 BUG_ON(ret);
2267 }
2268 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002269}
2270
2271/*
2272 * We have done the truncate/delete so we can go ahead and remove the orphan
2273 * item for this particular inode.
2274 */
2275int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
2276{
2277 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002278 int delete_item = 0;
2279 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002280 int ret = 0;
2281
Yan, Zhengd68fc572010-05-16 10:49:58 -04002282 spin_lock(&root->orphan_lock);
2283 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
2284 list_del_init(&BTRFS_I(inode)->i_orphan);
2285 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04002286 }
2287
Yan, Zhengd68fc572010-05-16 10:49:58 -04002288 if (BTRFS_I(inode)->orphan_meta_reserved) {
2289 BTRFS_I(inode)->orphan_meta_reserved = 0;
2290 release_rsv = 1;
2291 }
2292 spin_unlock(&root->orphan_lock);
2293
2294 if (trans && delete_item) {
Li Zefan33345d012011-04-20 10:31:50 +08002295 ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode));
Yan, Zhengd68fc572010-05-16 10:49:58 -04002296 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04002297 }
2298
Yan, Zhengd68fc572010-05-16 10:49:58 -04002299 if (release_rsv)
2300 btrfs_orphan_release_metadata(inode);
Josef Bacik7b128762008-07-24 12:17:14 -04002301
Yan, Zhengd68fc572010-05-16 10:49:58 -04002302 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002303}
2304
2305/*
2306 * this cleans up any orphans that may be left on the list from the last use
2307 * of this root.
2308 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002309int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04002310{
2311 struct btrfs_path *path;
2312 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04002313 struct btrfs_key key, found_key;
2314 struct btrfs_trans_handle *trans;
2315 struct inode *inode;
2316 int ret = 0, nr_unlink = 0, nr_truncate = 0;
2317
Yan, Zhengd68fc572010-05-16 10:49:58 -04002318 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002319 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00002320
2321 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002322 if (!path) {
2323 ret = -ENOMEM;
2324 goto out;
2325 }
Josef Bacik7b128762008-07-24 12:17:14 -04002326 path->reada = -1;
2327
2328 key.objectid = BTRFS_ORPHAN_OBJECTID;
2329 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
2330 key.offset = (u64)-1;
2331
Josef Bacik7b128762008-07-24 12:17:14 -04002332 while (1) {
2333 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002334 if (ret < 0)
2335 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04002336
2337 /*
2338 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002339 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04002340 * find the key and see if we have stuff that matches
2341 */
2342 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002343 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002344 if (path->slots[0] == 0)
2345 break;
2346 path->slots[0]--;
2347 }
2348
2349 /* pull out the item */
2350 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04002351 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2352
2353 /* make sure the item matches what we want */
2354 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
2355 break;
2356 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
2357 break;
2358
2359 /* release the path since we're done with it */
2360 btrfs_release_path(root, path);
2361
2362 /*
2363 * this is where we are basically btrfs_lookup, without the
2364 * crossing root thing. we store the inode number in the
2365 * offset of the orphan item.
2366 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002367 found_key.objectid = found_key.offset;
2368 found_key.type = BTRFS_INODE_ITEM_KEY;
2369 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00002370 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002371 if (IS_ERR(inode)) {
2372 ret = PTR_ERR(inode);
2373 goto out;
2374 }
Josef Bacik7b128762008-07-24 12:17:14 -04002375
Josef Bacik7b128762008-07-24 12:17:14 -04002376 /*
2377 * add this inode to the orphan list so btrfs_orphan_del does
2378 * the proper thing when we hit it
2379 */
Yan, Zhengd68fc572010-05-16 10:49:58 -04002380 spin_lock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002381 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002382 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002383
2384 /*
2385 * if this is a bad inode, means we actually succeeded in
2386 * removing the inode, but not the orphan record, which means
2387 * we need to manually delete the orphan since iput will just
2388 * do a destroy_inode
2389 */
2390 if (is_bad_inode(inode)) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04002391 trans = btrfs_start_transaction(root, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002392 if (IS_ERR(trans)) {
2393 ret = PTR_ERR(trans);
2394 goto out;
2395 }
Josef Bacik7b128762008-07-24 12:17:14 -04002396 btrfs_orphan_del(trans, inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002397 btrfs_end_transaction(trans, root);
Josef Bacik7b128762008-07-24 12:17:14 -04002398 iput(inode);
2399 continue;
2400 }
2401
2402 /* if we have links, this was a truncate, lets do that */
2403 if (inode->i_nlink) {
Josef Bacika41ad392011-01-31 15:30:16 -05002404 if (!S_ISREG(inode->i_mode)) {
2405 WARN_ON(1);
2406 iput(inode);
2407 continue;
2408 }
Josef Bacik7b128762008-07-24 12:17:14 -04002409 nr_truncate++;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002410 ret = btrfs_truncate(inode);
Josef Bacik7b128762008-07-24 12:17:14 -04002411 } else {
2412 nr_unlink++;
2413 }
2414
2415 /* this will do delete_inode and everything for us */
2416 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002417 if (ret)
2418 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04002419 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04002420 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
2421
2422 if (root->orphan_block_rsv)
2423 btrfs_block_rsv_release(root, root->orphan_block_rsv,
2424 (u64)-1);
2425
2426 if (root->orphan_block_rsv || root->orphan_item_inserted) {
2427 trans = btrfs_join_transaction(root, 1);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002428 if (!IS_ERR(trans))
2429 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002430 }
Josef Bacik7b128762008-07-24 12:17:14 -04002431
2432 if (nr_unlink)
2433 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
2434 if (nr_truncate)
2435 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002436
2437out:
2438 if (ret)
2439 printk(KERN_CRIT "btrfs: could not do orphan cleanup %d\n", ret);
2440 btrfs_free_path(path);
2441 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002442}
2443
Chris Masond352ac62008-09-29 15:18:18 -04002444/*
Chris Mason46a53cc2009-04-27 11:47:50 -04002445 * very simple check to peek ahead in the leaf looking for xattrs. If we
2446 * don't find any xattrs, we know there can't be any acls.
2447 *
2448 * slot is the slot the inode is in, objectid is the objectid of the inode
2449 */
2450static noinline int acls_after_inode_item(struct extent_buffer *leaf,
2451 int slot, u64 objectid)
2452{
2453 u32 nritems = btrfs_header_nritems(leaf);
2454 struct btrfs_key found_key;
2455 int scanned = 0;
2456
2457 slot++;
2458 while (slot < nritems) {
2459 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2460
2461 /* we found a different objectid, there must not be acls */
2462 if (found_key.objectid != objectid)
2463 return 0;
2464
2465 /* we found an xattr, assume we've got an acl */
2466 if (found_key.type == BTRFS_XATTR_ITEM_KEY)
2467 return 1;
2468
2469 /*
2470 * we found a key greater than an xattr key, there can't
2471 * be any acls later on
2472 */
2473 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
2474 return 0;
2475
2476 slot++;
2477 scanned++;
2478
2479 /*
2480 * it goes inode, inode backrefs, xattrs, extents,
2481 * so if there are a ton of hard links to an inode there can
2482 * be a lot of backrefs. Don't waste time searching too hard,
2483 * this is just an optimization
2484 */
2485 if (scanned >= 8)
2486 break;
2487 }
2488 /* we hit the end of the leaf before we found an xattr or
2489 * something larger than an xattr. We have to assume the inode
2490 * has acls
2491 */
2492 return 1;
2493}
2494
2495/*
Chris Masond352ac62008-09-29 15:18:18 -04002496 * read an inode from the btree into the in-memory inode
2497 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002498static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002499{
2500 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002501 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002502 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04002503 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04002504 struct btrfs_root *root = BTRFS_I(inode)->root;
2505 struct btrfs_key location;
Chris Mason46a53cc2009-04-27 11:47:50 -04002506 int maybe_acls;
Chris Mason39279cc2007-06-12 06:35:45 -04002507 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -04002508 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04002509 int ret;
2510
2511 path = btrfs_alloc_path();
2512 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -04002513 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05002514
Chris Mason39279cc2007-06-12 06:35:45 -04002515 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002516 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04002517 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04002518
Chris Mason5f39d392007-10-15 16:14:19 -04002519 leaf = path->nodes[0];
2520 inode_item = btrfs_item_ptr(leaf, path->slots[0],
2521 struct btrfs_inode_item);
2522
2523 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
2524 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
2525 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
2526 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
Chris Masondbe674a2008-07-17 12:54:05 -04002527 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04002528
2529 tspec = btrfs_inode_atime(inode_item);
2530 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2531 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2532
2533 tspec = btrfs_inode_mtime(inode_item);
2534 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2535 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2536
2537 tspec = btrfs_inode_ctime(inode_item);
2538 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2539 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2540
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002541 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04002542 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Chris Masonc3027eb2008-12-08 16:40:21 -05002543 BTRFS_I(inode)->sequence = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04002544 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04002545 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002546 rdev = btrfs_inode_rdev(leaf, inode_item);
2547
Josef Bacikaec74772008-07-24 12:12:38 -04002548 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05002549 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Josef Bacikaec74772008-07-24 12:12:38 -04002550
Chris Mason5f39d392007-10-15 16:14:19 -04002551 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002552
Chris Mason46a53cc2009-04-27 11:47:50 -04002553 /*
2554 * try to precache a NULL acl entry for files that don't have
2555 * any xattrs or acls
2556 */
Li Zefan33345d012011-04-20 10:31:50 +08002557 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
2558 btrfs_ino(inode));
Al Viro72c04902009-06-24 16:58:48 -04002559 if (!maybe_acls)
2560 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04002561
Yan Zhengd2fb3432008-12-11 16:30:39 -05002562 BTRFS_I(inode)->block_group = btrfs_find_block_group(root, 0,
2563 alloc_group_block, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002564 btrfs_free_path(path);
2565 inode_item = NULL;
2566
Chris Mason39279cc2007-06-12 06:35:45 -04002567 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04002568 case S_IFREG:
2569 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04002570 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05002571 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002572 inode->i_fop = &btrfs_file_operations;
2573 inode->i_op = &btrfs_file_inode_operations;
2574 break;
2575 case S_IFDIR:
2576 inode->i_fop = &btrfs_dir_file_operations;
2577 if (root == root->fs_info->tree_root)
2578 inode->i_op = &btrfs_dir_ro_inode_operations;
2579 else
2580 inode->i_op = &btrfs_dir_inode_operations;
2581 break;
2582 case S_IFLNK:
2583 inode->i_op = &btrfs_symlink_inode_operations;
2584 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04002585 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04002586 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04002587 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05002588 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04002589 init_special_inode(inode, inode->i_mode, rdev);
2590 break;
Chris Mason39279cc2007-06-12 06:35:45 -04002591 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002592
2593 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002594 return;
2595
2596make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04002597 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002598 make_bad_inode(inode);
2599}
2600
Chris Masond352ac62008-09-29 15:18:18 -04002601/*
2602 * given a leaf and an inode, copy the inode fields into the leaf
2603 */
Chris Masone02119d2008-09-05 16:13:11 -04002604static void fill_inode_item(struct btrfs_trans_handle *trans,
2605 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04002606 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04002607 struct inode *inode)
2608{
Josef Bacik12ddb962011-04-05 13:02:27 -04002609 if (!leaf->map_token)
2610 map_private_extent_buffer(leaf, (unsigned long)item,
2611 sizeof(struct btrfs_inode_item),
2612 &leaf->map_token, &leaf->kaddr,
2613 &leaf->map_start, &leaf->map_len,
2614 KM_USER1);
2615
Chris Mason5f39d392007-10-15 16:14:19 -04002616 btrfs_set_inode_uid(leaf, item, inode->i_uid);
2617 btrfs_set_inode_gid(leaf, item, inode->i_gid);
Chris Masondbe674a2008-07-17 12:54:05 -04002618 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
Chris Mason5f39d392007-10-15 16:14:19 -04002619 btrfs_set_inode_mode(leaf, item, inode->i_mode);
2620 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
2621
2622 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
2623 inode->i_atime.tv_sec);
2624 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
2625 inode->i_atime.tv_nsec);
2626
2627 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
2628 inode->i_mtime.tv_sec);
2629 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
2630 inode->i_mtime.tv_nsec);
2631
2632 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
2633 inode->i_ctime.tv_sec);
2634 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
2635 inode->i_ctime.tv_nsec);
2636
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002637 btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode));
Chris Masone02119d2008-09-05 16:13:11 -04002638 btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
Chris Masonc3027eb2008-12-08 16:40:21 -05002639 btrfs_set_inode_sequence(leaf, item, BTRFS_I(inode)->sequence);
Chris Masone02119d2008-09-05 16:13:11 -04002640 btrfs_set_inode_transid(leaf, item, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002641 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -05002642 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002643 btrfs_set_inode_block_group(leaf, item, BTRFS_I(inode)->block_group);
Josef Bacik12ddb962011-04-05 13:02:27 -04002644
2645 if (leaf->map_token) {
2646 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2647 leaf->map_token = NULL;
2648 }
Chris Mason39279cc2007-06-12 06:35:45 -04002649}
2650
Chris Masond352ac62008-09-29 15:18:18 -04002651/*
2652 * copy everything in the in-memory inode into the btree.
2653 */
Chris Masond3977122009-01-05 21:25:51 -05002654noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
2655 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002656{
2657 struct btrfs_inode_item *inode_item;
2658 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002659 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002660 int ret;
2661
2662 path = btrfs_alloc_path();
2663 BUG_ON(!path);
Chris Masonb9473432009-03-13 11:00:37 -04002664 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002665 ret = btrfs_lookup_inode(trans, root, path,
2666 &BTRFS_I(inode)->location, 1);
2667 if (ret) {
2668 if (ret > 0)
2669 ret = -ENOENT;
2670 goto failed;
2671 }
2672
Chris Masonb4ce94d2009-02-04 09:25:08 -05002673 btrfs_unlock_up_safe(path, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002674 leaf = path->nodes[0];
2675 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04002676 struct btrfs_inode_item);
2677
Chris Masone02119d2008-09-05 16:13:11 -04002678 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002679 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002680 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002681 ret = 0;
2682failed:
Chris Mason39279cc2007-06-12 06:35:45 -04002683 btrfs_free_path(path);
2684 return ret;
2685}
2686
2687
Chris Masond352ac62008-09-29 15:18:18 -04002688/*
2689 * unlink helper that gets used here in inode.c and in the tree logging
2690 * recovery code. It remove a link in a directory with a given name, and
2691 * also drops the back refs in the inode to the directory
2692 */
Al Viro92986792011-03-04 17:14:37 +00002693static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2694 struct btrfs_root *root,
2695 struct inode *dir, struct inode *inode,
2696 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04002697{
2698 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04002699 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002700 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002701 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04002702 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04002703 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08002704 u64 ino = btrfs_ino(inode);
2705 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04002706
2707 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04002708 if (!path) {
2709 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00002710 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04002711 }
2712
Chris Masonb9473432009-03-13 11:00:37 -04002713 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08002714 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04002715 name, name_len, -1);
2716 if (IS_ERR(di)) {
2717 ret = PTR_ERR(di);
2718 goto err;
2719 }
2720 if (!di) {
2721 ret = -ENOENT;
2722 goto err;
2723 }
Chris Mason5f39d392007-10-15 16:14:19 -04002724 leaf = path->nodes[0];
2725 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04002726 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04002727 if (ret)
2728 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -04002729 btrfs_release_path(root, path);
2730
Li Zefan33345d012011-04-20 10:31:50 +08002731 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
2732 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04002733 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05002734 printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
Li Zefan33345d012011-04-20 10:31:50 +08002735 "inode %llu parent %llu\n", name_len, name,
2736 (unsigned long long)ino, (unsigned long long)dir_ino);
Josef Bacikaec74772008-07-24 12:12:38 -04002737 goto err;
2738 }
2739
Li Zefan33345d012011-04-20 10:31:50 +08002740 di = btrfs_lookup_dir_index_item(trans, root, path, dir_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04002741 index, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -04002742 if (IS_ERR(di)) {
2743 ret = PTR_ERR(di);
2744 goto err;
2745 }
2746 if (!di) {
2747 ret = -ENOENT;
2748 goto err;
2749 }
2750 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason925baed2008-06-25 16:01:30 -04002751 btrfs_release_path(root, path);
Chris Mason39279cc2007-06-12 06:35:45 -04002752
Chris Masone02119d2008-09-05 16:13:11 -04002753 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08002754 inode, dir_ino);
Chris Mason49eb7e42008-09-11 15:53:12 -04002755 BUG_ON(ret != 0 && ret != -ENOENT);
Chris Masone02119d2008-09-05 16:13:11 -04002756
2757 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
2758 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04002759 if (ret == -ENOENT)
2760 ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002761err:
2762 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002763 if (ret)
2764 goto out;
2765
2766 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
2767 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2768 btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04002769out:
Chris Mason39279cc2007-06-12 06:35:45 -04002770 return ret;
2771}
2772
Al Viro92986792011-03-04 17:14:37 +00002773int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2774 struct btrfs_root *root,
2775 struct inode *dir, struct inode *inode,
2776 const char *name, int name_len)
2777{
2778 int ret;
2779 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
2780 if (!ret) {
2781 btrfs_drop_nlink(inode);
2782 ret = btrfs_update_inode(trans, root, inode);
2783 }
2784 return ret;
2785}
2786
2787
Yan, Zhenga22285a2010-05-16 10:48:46 -04002788/* helper to check if there is any shared block in the path */
2789static int check_path_shared(struct btrfs_root *root,
2790 struct btrfs_path *path)
2791{
2792 struct extent_buffer *eb;
2793 int level;
Dan Carpenter0e4dcbef2010-06-01 08:23:11 +00002794 u64 refs = 1;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002795
2796 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
Josef Bacikdedefd72011-01-24 21:43:18 +00002797 int ret;
2798
Yan, Zhenga22285a2010-05-16 10:48:46 -04002799 if (!path->nodes[level])
2800 break;
2801 eb = path->nodes[level];
2802 if (!btrfs_block_can_be_shared(root, eb))
2803 continue;
2804 ret = btrfs_lookup_extent_info(NULL, root, eb->start, eb->len,
2805 &refs, NULL);
2806 if (refs > 1)
2807 return 1;
2808 }
Josef Bacikdedefd72011-01-24 21:43:18 +00002809 return 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002810}
2811
2812/*
2813 * helper to start transaction for unlink and rmdir.
2814 *
2815 * unlink and rmdir are special in btrfs, they do not always free space.
2816 * so in enospc case, we should make sure they will free space before
2817 * allowing them to use the global metadata reservation.
2818 */
2819static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
2820 struct dentry *dentry)
2821{
2822 struct btrfs_trans_handle *trans;
2823 struct btrfs_root *root = BTRFS_I(dir)->root;
2824 struct btrfs_path *path;
2825 struct btrfs_inode_ref *ref;
2826 struct btrfs_dir_item *di;
2827 struct inode *inode = dentry->d_inode;
2828 u64 index;
2829 int check_link = 1;
2830 int err = -ENOSPC;
2831 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08002832 u64 ino = btrfs_ino(inode);
2833 u64 dir_ino = btrfs_ino(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002834
2835 trans = btrfs_start_transaction(root, 10);
2836 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
2837 return trans;
2838
Li Zefan33345d012011-04-20 10:31:50 +08002839 if (ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhenga22285a2010-05-16 10:48:46 -04002840 return ERR_PTR(-ENOSPC);
2841
2842 /* check if there is someone else holds reference */
2843 if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1)
2844 return ERR_PTR(-ENOSPC);
2845
2846 if (atomic_read(&inode->i_count) > 2)
2847 return ERR_PTR(-ENOSPC);
2848
2849 if (xchg(&root->fs_info->enospc_unlink, 1))
2850 return ERR_PTR(-ENOSPC);
2851
2852 path = btrfs_alloc_path();
2853 if (!path) {
2854 root->fs_info->enospc_unlink = 0;
2855 return ERR_PTR(-ENOMEM);
2856 }
2857
2858 trans = btrfs_start_transaction(root, 0);
2859 if (IS_ERR(trans)) {
2860 btrfs_free_path(path);
2861 root->fs_info->enospc_unlink = 0;
2862 return trans;
2863 }
2864
2865 path->skip_locking = 1;
2866 path->search_commit_root = 1;
2867
2868 ret = btrfs_lookup_inode(trans, root, path,
2869 &BTRFS_I(dir)->location, 0);
2870 if (ret < 0) {
2871 err = ret;
2872 goto out;
2873 }
2874 if (ret == 0) {
2875 if (check_path_shared(root, path))
2876 goto out;
2877 } else {
2878 check_link = 0;
2879 }
2880 btrfs_release_path(root, path);
2881
2882 ret = btrfs_lookup_inode(trans, root, path,
2883 &BTRFS_I(inode)->location, 0);
2884 if (ret < 0) {
2885 err = ret;
2886 goto out;
2887 }
2888 if (ret == 0) {
2889 if (check_path_shared(root, path))
2890 goto out;
2891 } else {
2892 check_link = 0;
2893 }
2894 btrfs_release_path(root, path);
2895
2896 if (ret == 0 && S_ISREG(inode->i_mode)) {
2897 ret = btrfs_lookup_file_extent(trans, root, path,
Li Zefan33345d012011-04-20 10:31:50 +08002898 ino, (u64)-1, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002899 if (ret < 0) {
2900 err = ret;
2901 goto out;
2902 }
2903 BUG_ON(ret == 0);
2904 if (check_path_shared(root, path))
2905 goto out;
2906 btrfs_release_path(root, path);
2907 }
2908
2909 if (!check_link) {
2910 err = 0;
2911 goto out;
2912 }
2913
Li Zefan33345d012011-04-20 10:31:50 +08002914 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zhenga22285a2010-05-16 10:48:46 -04002915 dentry->d_name.name, dentry->d_name.len, 0);
2916 if (IS_ERR(di)) {
2917 err = PTR_ERR(di);
2918 goto out;
2919 }
2920 if (di) {
2921 if (check_path_shared(root, path))
2922 goto out;
2923 } else {
2924 err = 0;
2925 goto out;
2926 }
2927 btrfs_release_path(root, path);
2928
2929 ref = btrfs_lookup_inode_ref(trans, root, path,
2930 dentry->d_name.name, dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08002931 ino, dir_ino, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002932 if (IS_ERR(ref)) {
2933 err = PTR_ERR(ref);
2934 goto out;
2935 }
2936 BUG_ON(!ref);
2937 if (check_path_shared(root, path))
2938 goto out;
2939 index = btrfs_inode_ref_index(path->nodes[0], ref);
2940 btrfs_release_path(root, path);
2941
Li Zefan33345d012011-04-20 10:31:50 +08002942 di = btrfs_lookup_dir_index_item(trans, root, path, dir_ino, index,
Yan, Zhenga22285a2010-05-16 10:48:46 -04002943 dentry->d_name.name, dentry->d_name.len, 0);
2944 if (IS_ERR(di)) {
2945 err = PTR_ERR(di);
2946 goto out;
2947 }
2948 BUG_ON(ret == -ENOENT);
2949 if (check_path_shared(root, path))
2950 goto out;
2951
2952 err = 0;
2953out:
2954 btrfs_free_path(path);
2955 if (err) {
2956 btrfs_end_transaction(trans, root);
2957 root->fs_info->enospc_unlink = 0;
2958 return ERR_PTR(err);
2959 }
2960
2961 trans->block_rsv = &root->fs_info->global_block_rsv;
2962 return trans;
2963}
2964
2965static void __unlink_end_trans(struct btrfs_trans_handle *trans,
2966 struct btrfs_root *root)
2967{
2968 if (trans->block_rsv == &root->fs_info->global_block_rsv) {
2969 BUG_ON(!root->fs_info->enospc_unlink);
2970 root->fs_info->enospc_unlink = 0;
2971 }
2972 btrfs_end_transaction_throttle(trans, root);
2973}
2974
Chris Mason39279cc2007-06-12 06:35:45 -04002975static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
2976{
Yan, Zhenga22285a2010-05-16 10:48:46 -04002977 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04002978 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04002979 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002980 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -05002981 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002982
Yan, Zhenga22285a2010-05-16 10:48:46 -04002983 trans = __unlink_start_trans(dir, dentry);
2984 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05002985 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04002986
Chris Mason39279cc2007-06-12 06:35:45 -04002987 btrfs_set_trans_block_group(trans, dir);
Chris Mason12fcfd22009-03-24 10:24:20 -04002988
2989 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
2990
Chris Masone02119d2008-09-05 16:13:11 -04002991 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
2992 dentry->d_name.name, dentry->d_name.len);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002993 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04002994
Yan, Zhenga22285a2010-05-16 10:48:46 -04002995 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04002996 ret = btrfs_orphan_add(trans, inode);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002997 BUG_ON(ret);
2998 }
Josef Bacik7b128762008-07-24 12:17:14 -04002999
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003000 nr = trans->blocks_used;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003001 __unlink_end_trans(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003002 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003003 return ret;
3004}
3005
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003006int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3007 struct btrfs_root *root,
3008 struct inode *dir, u64 objectid,
3009 const char *name, int name_len)
3010{
3011 struct btrfs_path *path;
3012 struct extent_buffer *leaf;
3013 struct btrfs_dir_item *di;
3014 struct btrfs_key key;
3015 u64 index;
3016 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003017 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003018
3019 path = btrfs_alloc_path();
3020 if (!path)
3021 return -ENOMEM;
3022
Li Zefan33345d012011-04-20 10:31:50 +08003023 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003024 name, name_len, -1);
3025 BUG_ON(!di || IS_ERR(di));
3026
3027 leaf = path->nodes[0];
3028 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3029 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3030 ret = btrfs_delete_one_dir_name(trans, root, path, di);
3031 BUG_ON(ret);
3032 btrfs_release_path(root, path);
3033
3034 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3035 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003036 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003037 if (ret < 0) {
3038 BUG_ON(ret != -ENOENT);
Li Zefan33345d012011-04-20 10:31:50 +08003039 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003040 name, name_len);
3041 BUG_ON(!di || IS_ERR(di));
3042
3043 leaf = path->nodes[0];
3044 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3045 btrfs_release_path(root, path);
3046 index = key.offset;
3047 }
3048
Li Zefan33345d012011-04-20 10:31:50 +08003049 di = btrfs_lookup_dir_index_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003050 index, name, name_len, -1);
3051 BUG_ON(!di || IS_ERR(di));
3052
3053 leaf = path->nodes[0];
3054 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3055 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3056 ret = btrfs_delete_one_dir_name(trans, root, path, di);
3057 BUG_ON(ret);
3058 btrfs_release_path(root, path);
3059
3060 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
3061 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
3062 ret = btrfs_update_inode(trans, root, dir);
3063 BUG_ON(ret);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003064
3065 btrfs_free_path(path);
3066 return 0;
3067}
3068
Chris Mason39279cc2007-06-12 06:35:45 -04003069static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3070{
3071 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003072 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003073 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003074 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -05003075 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003076
Chris Mason3394e162008-11-17 20:42:26 -05003077 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE ||
Li Zefan33345d012011-04-20 10:31:50 +08003078 btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
Yan134d4512007-10-25 15:49:25 -04003079 return -ENOTEMPTY;
3080
Yan, Zhenga22285a2010-05-16 10:48:46 -04003081 trans = __unlink_start_trans(dir, dentry);
3082 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003083 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003084
Chris Mason39279cc2007-06-12 06:35:45 -04003085 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003086
Li Zefan33345d012011-04-20 10:31:50 +08003087 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003088 err = btrfs_unlink_subvol(trans, root, dir,
3089 BTRFS_I(inode)->location.objectid,
3090 dentry->d_name.name,
3091 dentry->d_name.len);
3092 goto out;
3093 }
3094
Josef Bacik7b128762008-07-24 12:17:14 -04003095 err = btrfs_orphan_add(trans, inode);
3096 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003097 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003098
Chris Mason39279cc2007-06-12 06:35:45 -04003099 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04003100 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3101 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05003102 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04003103 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003104out:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003105 nr = trans->blocks_used;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003106 __unlink_end_trans(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003107 btrfs_btree_balance_dirty(root, nr);
Chris Mason39544012007-12-12 14:38:19 -05003108
Chris Mason39279cc2007-06-12 06:35:45 -04003109 return err;
3110}
3111
Chris Masond20f7042008-12-08 16:58:54 -05003112#if 0
Chris Mason39279cc2007-06-12 06:35:45 -04003113/*
Chris Mason323ac952008-10-01 19:05:46 -04003114 * when truncating bytes in a file, it is possible to avoid reading
3115 * the leaves that contain only checksum items. This can be the
3116 * majority of the IO required to delete a large file, but it must
3117 * be done carefully.
3118 *
3119 * The keys in the level just above the leaves are checked to make sure
3120 * the lowest key in a given leaf is a csum key, and starts at an offset
3121 * after the new size.
3122 *
3123 * Then the key for the next leaf is checked to make sure it also has
3124 * a checksum item for the same file. If it does, we know our target leaf
3125 * contains only checksum items, and it can be safely freed without reading
3126 * it.
3127 *
3128 * This is just an optimization targeted at large files. It may do
3129 * nothing. It will return 0 unless things went badly.
3130 */
3131static noinline int drop_csum_leaves(struct btrfs_trans_handle *trans,
3132 struct btrfs_root *root,
3133 struct btrfs_path *path,
3134 struct inode *inode, u64 new_size)
3135{
3136 struct btrfs_key key;
3137 int ret;
3138 int nritems;
3139 struct btrfs_key found_key;
3140 struct btrfs_key other_key;
Yan Zheng5b84e8d2008-10-09 11:46:19 -04003141 struct btrfs_leaf_ref *ref;
3142 u64 leaf_gen;
3143 u64 leaf_start;
Chris Mason323ac952008-10-01 19:05:46 -04003144
3145 path->lowest_level = 1;
3146 key.objectid = inode->i_ino;
3147 key.type = BTRFS_CSUM_ITEM_KEY;
3148 key.offset = new_size;
3149again:
3150 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3151 if (ret < 0)
3152 goto out;
3153
3154 if (path->nodes[1] == NULL) {
3155 ret = 0;
3156 goto out;
3157 }
3158 ret = 0;
3159 btrfs_node_key_to_cpu(path->nodes[1], &found_key, path->slots[1]);
3160 nritems = btrfs_header_nritems(path->nodes[1]);
3161
3162 if (!nritems)
3163 goto out;
3164
3165 if (path->slots[1] >= nritems)
3166 goto next_node;
3167
3168 /* did we find a key greater than anything we want to delete? */
3169 if (found_key.objectid > inode->i_ino ||
3170 (found_key.objectid == inode->i_ino && found_key.type > key.type))
3171 goto out;
3172
3173 /* we check the next key in the node to make sure the leave contains
3174 * only checksum items. This comparison doesn't work if our
3175 * leaf is the last one in the node
3176 */
3177 if (path->slots[1] + 1 >= nritems) {
3178next_node:
3179 /* search forward from the last key in the node, this
3180 * will bring us into the next node in the tree
3181 */
3182 btrfs_node_key_to_cpu(path->nodes[1], &found_key, nritems - 1);
3183
3184 /* unlikely, but we inc below, so check to be safe */
3185 if (found_key.offset == (u64)-1)
3186 goto out;
3187
3188 /* search_forward needs a path with locks held, do the
3189 * search again for the original key. It is possible
3190 * this will race with a balance and return a path that
3191 * we could modify, but this drop is just an optimization
3192 * and is allowed to miss some leaves.
3193 */
3194 btrfs_release_path(root, path);
3195 found_key.offset++;
3196
3197 /* setup a max key for search_forward */
3198 other_key.offset = (u64)-1;
3199 other_key.type = key.type;
3200 other_key.objectid = key.objectid;
3201
3202 path->keep_locks = 1;
3203 ret = btrfs_search_forward(root, &found_key, &other_key,
3204 path, 0, 0);
3205 path->keep_locks = 0;
3206 if (ret || found_key.objectid != key.objectid ||
3207 found_key.type != key.type) {
3208 ret = 0;
3209 goto out;
3210 }
3211
3212 key.offset = found_key.offset;
3213 btrfs_release_path(root, path);
3214 cond_resched();
3215 goto again;
3216 }
3217
3218 /* we know there's one more slot after us in the tree,
3219 * read that key so we can verify it is also a checksum item
3220 */
3221 btrfs_node_key_to_cpu(path->nodes[1], &other_key, path->slots[1] + 1);
3222
3223 if (found_key.objectid < inode->i_ino)
3224 goto next_key;
3225
3226 if (found_key.type != key.type || found_key.offset < new_size)
3227 goto next_key;
3228
3229 /*
3230 * if the key for the next leaf isn't a csum key from this objectid,
3231 * we can't be sure there aren't good items inside this leaf.
3232 * Bail out
3233 */
3234 if (other_key.objectid != inode->i_ino || other_key.type != key.type)
3235 goto out;
3236
Yan Zheng5b84e8d2008-10-09 11:46:19 -04003237 leaf_start = btrfs_node_blockptr(path->nodes[1], path->slots[1]);
3238 leaf_gen = btrfs_node_ptr_generation(path->nodes[1], path->slots[1]);
Chris Mason323ac952008-10-01 19:05:46 -04003239 /*
3240 * it is safe to delete this leaf, it contains only
3241 * csum items from this inode at an offset >= new_size
3242 */
Yan Zheng5b84e8d2008-10-09 11:46:19 -04003243 ret = btrfs_del_leaf(trans, root, path, leaf_start);
Chris Mason323ac952008-10-01 19:05:46 -04003244 BUG_ON(ret);
3245
Yan Zheng5b84e8d2008-10-09 11:46:19 -04003246 if (root->ref_cows && leaf_gen < trans->transid) {
3247 ref = btrfs_alloc_leaf_ref(root, 0);
3248 if (ref) {
3249 ref->root_gen = root->root_key.offset;
3250 ref->bytenr = leaf_start;
3251 ref->owner = 0;
3252 ref->generation = leaf_gen;
3253 ref->nritems = 0;
3254
Chris Masonbd56b302009-02-04 09:27:02 -05003255 btrfs_sort_leaf_ref(ref);
3256
Yan Zheng5b84e8d2008-10-09 11:46:19 -04003257 ret = btrfs_add_leaf_ref(root, ref, 0);
3258 WARN_ON(ret);
3259 btrfs_free_leaf_ref(root, ref);
3260 } else {
3261 WARN_ON(1);
3262 }
3263 }
Chris Mason323ac952008-10-01 19:05:46 -04003264next_key:
3265 btrfs_release_path(root, path);
3266
3267 if (other_key.objectid == inode->i_ino &&
3268 other_key.type == key.type && other_key.offset > key.offset) {
3269 key.offset = other_key.offset;
3270 cond_resched();
3271 goto again;
3272 }
3273 ret = 0;
3274out:
3275 /* fixup any changes we've made to the path */
3276 path->lowest_level = 0;
3277 path->keep_locks = 0;
3278 btrfs_release_path(root, path);
3279 return ret;
3280}
3281
Chris Masond20f7042008-12-08 16:58:54 -05003282#endif
3283
Chris Mason323ac952008-10-01 19:05:46 -04003284/*
Chris Mason39279cc2007-06-12 06:35:45 -04003285 * this can truncate away extent items, csum items and directory items.
3286 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04003287 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04003288 *
3289 * csum items that cross the new i_size are truncated to the new size
3290 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04003291 *
3292 * min_type is the minimum key type to truncate down to. If set to 0, this
3293 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04003294 */
Yan, Zheng80825102009-11-12 09:35:36 +00003295int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
3296 struct btrfs_root *root,
3297 struct inode *inode,
3298 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003299{
Chris Mason39279cc2007-06-12 06:35:45 -04003300 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003301 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003302 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00003303 struct btrfs_key key;
3304 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04003305 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04003306 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003307 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003308 u64 item_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00003309 u64 mask = root->sectorsize - 1;
3310 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04003311 int found_extent;
3312 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003313 int pending_del_nr = 0;
3314 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04003315 int extent_type = -1;
Chris Mason771ed682008-11-06 22:02:51 -05003316 int encoding;
Yan, Zheng80825102009-11-12 09:35:36 +00003317 int ret;
3318 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08003319 u64 ino = btrfs_ino(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003320
3321 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04003322
Josef Bacik0af3d002010-06-21 14:48:16 -04003323 if (root->ref_cows || root == root->fs_info->tree_root)
Zheng Yan5b21f2e2008-09-26 10:05:38 -04003324 btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003325
Chris Mason39279cc2007-06-12 06:35:45 -04003326 path = btrfs_alloc_path();
3327 BUG_ON(!path);
Julia Lawall33c17ad2009-07-22 16:49:01 -04003328 path->reada = -1;
Chris Mason5f39d392007-10-15 16:14:19 -04003329
Li Zefan33345d012011-04-20 10:31:50 +08003330 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04003331 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04003332 key.type = (u8)-1;
3333
Chris Mason85e21ba2008-01-29 15:11:36 -05003334search_again:
Chris Masonb9473432009-03-13 11:00:37 -04003335 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05003336 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00003337 if (ret < 0) {
3338 err = ret;
3339 goto out;
3340 }
Chris Masond3977122009-01-05 21:25:51 -05003341
Chris Mason85e21ba2008-01-29 15:11:36 -05003342 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003343 /* there are no items in the tree for us to truncate, we're
3344 * done
3345 */
Yan, Zheng80825102009-11-12 09:35:36 +00003346 if (path->slots[0] == 0)
3347 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05003348 path->slots[0]--;
3349 }
3350
Chris Masond3977122009-01-05 21:25:51 -05003351 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04003352 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04003353 leaf = path->nodes[0];
3354 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3355 found_type = btrfs_key_type(&found_key);
Chris Mason771ed682008-11-06 22:02:51 -05003356 encoding = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003357
Li Zefan33345d012011-04-20 10:31:50 +08003358 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04003359 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003360
Chris Mason85e21ba2008-01-29 15:11:36 -05003361 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003362 break;
3363
Chris Mason5f39d392007-10-15 16:14:19 -04003364 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04003365 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04003366 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04003367 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04003368 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Mason771ed682008-11-06 22:02:51 -05003369 encoding = btrfs_file_extent_compression(leaf, fi);
3370 encoding |= btrfs_file_extent_encryption(leaf, fi);
3371 encoding |= btrfs_file_extent_other_encoding(leaf, fi);
3372
Chris Mason179e29e2007-11-01 11:28:41 -04003373 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04003374 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04003375 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04003376 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04003377 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04003378 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003379 }
Yan008630c2007-11-07 13:31:09 -05003380 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04003381 }
Yan, Zheng80825102009-11-12 09:35:36 +00003382 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04003383 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00003384 } else {
3385 if (item_end < new_size)
3386 break;
3387 if (found_key.offset >= new_size)
3388 del_item = 1;
3389 else
3390 del_item = 0;
3391 }
Chris Mason39279cc2007-06-12 06:35:45 -04003392 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003393 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04003394 if (found_type != BTRFS_EXTENT_DATA_KEY)
3395 goto delete;
3396
3397 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04003398 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04003399 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason771ed682008-11-06 22:02:51 -05003400 if (!del_item && !encoding) {
Chris Masondb945352007-10-15 16:15:53 -04003401 u64 orig_num_bytes =
3402 btrfs_file_extent_num_bytes(leaf, fi);
Chris Masone02119d2008-09-05 16:13:11 -04003403 extent_num_bytes = new_size -
Chris Mason5f39d392007-10-15 16:14:19 -04003404 found_key.offset + root->sectorsize - 1;
Yanb1632b12008-01-30 11:54:04 -05003405 extent_num_bytes = extent_num_bytes &
3406 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04003407 btrfs_set_file_extent_num_bytes(leaf, fi,
3408 extent_num_bytes);
3409 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05003410 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04003411 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003412 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04003413 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003414 } else {
Chris Masondb945352007-10-15 16:15:53 -04003415 extent_num_bytes =
3416 btrfs_file_extent_disk_num_bytes(leaf,
3417 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003418 extent_offset = found_key.offset -
3419 btrfs_file_extent_offset(leaf, fi);
3420
Chris Mason39279cc2007-06-12 06:35:45 -04003421 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05003422 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003423 if (extent_start != 0) {
3424 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04003425 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003426 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04003427 }
3428 }
Chris Mason90692182008-02-08 13:49:28 -05003429 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04003430 /*
3431 * we can't truncate inline items that have had
3432 * special encodings
3433 */
3434 if (!del_item &&
3435 btrfs_file_extent_compression(leaf, fi) == 0 &&
3436 btrfs_file_extent_encryption(leaf, fi) == 0 &&
3437 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003438 u32 size = new_size - found_key.offset;
3439
3440 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003441 inode_sub_bytes(inode, item_end + 1 -
3442 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04003443 }
3444 size =
3445 btrfs_file_extent_calc_inline_size(size);
Chris Mason90692182008-02-08 13:49:28 -05003446 ret = btrfs_truncate_item(trans, root, path,
Chris Masone02119d2008-09-05 16:13:11 -04003447 size, 1);
Chris Mason90692182008-02-08 13:49:28 -05003448 BUG_ON(ret);
Chris Masone02119d2008-09-05 16:13:11 -04003449 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003450 inode_sub_bytes(inode, item_end + 1 -
3451 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05003452 }
Chris Mason39279cc2007-06-12 06:35:45 -04003453 }
Chris Mason179e29e2007-11-01 11:28:41 -04003454delete:
Chris Mason39279cc2007-06-12 06:35:45 -04003455 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05003456 if (!pending_del_nr) {
3457 /* no pending yet, add ourselves */
3458 pending_del_slot = path->slots[0];
3459 pending_del_nr = 1;
3460 } else if (pending_del_nr &&
3461 path->slots[0] + 1 == pending_del_slot) {
3462 /* hop on the pending chunk */
3463 pending_del_nr++;
3464 pending_del_slot = path->slots[0];
3465 } else {
Chris Masond3977122009-01-05 21:25:51 -05003466 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05003467 }
Chris Mason39279cc2007-06-12 06:35:45 -04003468 } else {
3469 break;
3470 }
Josef Bacik0af3d002010-06-21 14:48:16 -04003471 if (found_extent && (root->ref_cows ||
3472 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04003473 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003474 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003475 extent_num_bytes, 0,
3476 btrfs_header_owner(leaf),
Li Zefan33345d012011-04-20 10:31:50 +08003477 ino, extent_offset);
Chris Mason39279cc2007-06-12 06:35:45 -04003478 BUG_ON(ret);
3479 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003480
Yan, Zheng80825102009-11-12 09:35:36 +00003481 if (found_type == BTRFS_INODE_ITEM_KEY)
3482 break;
3483
3484 if (path->slots[0] == 0 ||
3485 path->slots[0] != pending_del_slot) {
Li Zefan82d59022011-04-20 10:33:24 +08003486 if (root->ref_cows &&
3487 BTRFS_I(inode)->location.objectid !=
3488 BTRFS_FREE_INO_OBJECTID) {
Yan, Zheng80825102009-11-12 09:35:36 +00003489 err = -EAGAIN;
3490 goto out;
3491 }
3492 if (pending_del_nr) {
3493 ret = btrfs_del_items(trans, root, path,
3494 pending_del_slot,
3495 pending_del_nr);
3496 BUG_ON(ret);
3497 pending_del_nr = 0;
3498 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003499 btrfs_release_path(root, path);
3500 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00003501 } else {
3502 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05003503 }
Chris Mason39279cc2007-06-12 06:35:45 -04003504 }
Yan, Zheng80825102009-11-12 09:35:36 +00003505out:
Chris Mason85e21ba2008-01-29 15:11:36 -05003506 if (pending_del_nr) {
3507 ret = btrfs_del_items(trans, root, path, pending_del_slot,
3508 pending_del_nr);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003509 BUG_ON(ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05003510 }
Chris Mason39279cc2007-06-12 06:35:45 -04003511 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00003512 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003513}
3514
Chris Masona52d9a82007-08-27 16:49:44 -04003515/*
3516 * taken from block_truncate_page, but does cow as it zeros out
3517 * any bytes left in the last page in the file.
3518 */
3519static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
3520{
3521 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -04003522 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003523 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3524 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003525 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003526 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04003527 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04003528 pgoff_t index = from >> PAGE_CACHE_SHIFT;
3529 unsigned offset = from & (PAGE_CACHE_SIZE-1);
3530 struct page *page;
3531 int ret = 0;
3532 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003533 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04003534
3535 if ((offset & (blocksize - 1)) == 0)
3536 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003537 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003538 if (ret)
3539 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04003540
3541 ret = -ENOMEM;
Chris Mason211c17f2008-05-15 09:13:45 -04003542again:
Chris Masona52d9a82007-08-27 16:49:44 -04003543 page = grab_cache_page(mapping, index);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003544 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003545 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Masona52d9a82007-08-27 16:49:44 -04003546 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04003547 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003548
3549 page_start = page_offset(page);
3550 page_end = page_start + PAGE_CACHE_SIZE - 1;
3551
Chris Masona52d9a82007-08-27 16:49:44 -04003552 if (!PageUptodate(page)) {
3553 ret = btrfs_readpage(NULL, page);
3554 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04003555 if (page->mapping != mapping) {
3556 unlock_page(page);
3557 page_cache_release(page);
3558 goto again;
3559 }
Chris Masona52d9a82007-08-27 16:49:44 -04003560 if (!PageUptodate(page)) {
3561 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04003562 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04003563 }
3564 }
Chris Mason211c17f2008-05-15 09:13:45 -04003565 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003566
Josef Bacik2ac55d42010-02-03 19:33:23 +00003567 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state,
3568 GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003569 set_page_extent_mapped(page);
3570
3571 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3572 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00003573 unlock_extent_cached(io_tree, page_start, page_end,
3574 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003575 unlock_page(page);
3576 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04003577 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003578 btrfs_put_ordered_extent(ordered);
3579 goto again;
3580 }
3581
Josef Bacik2ac55d42010-02-03 19:33:23 +00003582 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Josef Bacik5d5e1032009-10-13 16:46:49 -04003583 EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003584 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003585
Josef Bacik2ac55d42010-02-03 19:33:23 +00003586 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
3587 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003588 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00003589 unlock_extent_cached(io_tree, page_start, page_end,
3590 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003591 goto out_unlock;
3592 }
3593
Chris Masone6dcd2d2008-07-17 12:53:50 -04003594 ret = 0;
3595 if (offset != PAGE_CACHE_SIZE) {
3596 kaddr = kmap(page);
3597 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
3598 flush_dcache_page(page);
3599 kunmap(page);
3600 }
Chris Mason247e7432008-07-17 12:53:51 -04003601 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003602 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003603 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
3604 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04003605
Chris Mason89642222008-07-24 09:41:53 -04003606out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04003607 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003608 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04003609 unlock_page(page);
3610 page_cache_release(page);
3611out:
3612 return ret;
3613}
3614
Josef Bacik695a0d02011-03-04 15:46:53 -05003615/*
3616 * This function puts in dummy file extents for the area we're creating a hole
3617 * for. So if we are truncating this file to a larger size we need to insert
3618 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
3619 * the range between oldsize and size
3620 */
Josef Bacika41ad392011-01-31 15:30:16 -05003621int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04003622{
3623 struct btrfs_trans_handle *trans;
3624 struct btrfs_root *root = BTRFS_I(inode)->root;
3625 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003626 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003627 struct extent_state *cached_state = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04003628 u64 mask = root->sectorsize - 1;
Josef Bacika41ad392011-01-31 15:30:16 -05003629 u64 hole_start = (oldsize + mask) & ~mask;
Yan Zheng9036c102008-10-30 14:19:41 -04003630 u64 block_end = (size + mask) & ~mask;
3631 u64 last_byte;
3632 u64 cur_offset;
3633 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003634 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04003635
3636 if (size <= hole_start)
3637 return 0;
3638
Yan Zheng9036c102008-10-30 14:19:41 -04003639 while (1) {
3640 struct btrfs_ordered_extent *ordered;
3641 btrfs_wait_ordered_range(inode, hole_start,
3642 block_end - hole_start);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003643 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
3644 &cached_state, GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003645 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
3646 if (!ordered)
3647 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003648 unlock_extent_cached(io_tree, hole_start, block_end - 1,
3649 &cached_state, GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003650 btrfs_put_ordered_extent(ordered);
3651 }
3652
Yan Zheng9036c102008-10-30 14:19:41 -04003653 cur_offset = hole_start;
3654 while (1) {
3655 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
3656 block_end - cur_offset, 0);
3657 BUG_ON(IS_ERR(em) || !em);
3658 last_byte = min(extent_map_end(em), block_end);
3659 last_byte = (last_byte + mask) & ~mask;
Yan, Zheng80825102009-11-12 09:35:36 +00003660 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Chris Mason771ed682008-11-06 22:02:51 -05003661 u64 hint_byte = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04003662 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00003663
Yan, Zhenga22285a2010-05-16 10:48:46 -04003664 trans = btrfs_start_transaction(root, 2);
3665 if (IS_ERR(trans)) {
3666 err = PTR_ERR(trans);
Chris Mason771ed682008-11-06 22:02:51 -05003667 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003668 }
Yan, Zheng80825102009-11-12 09:35:36 +00003669 btrfs_set_trans_block_group(trans, inode);
3670
3671 err = btrfs_drop_extents(trans, inode, cur_offset,
3672 cur_offset + hole_size,
3673 &hint_byte, 1);
Josef Bacik3893e332011-01-31 16:03:11 -05003674 if (err)
3675 break;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003676
Yan Zheng9036c102008-10-30 14:19:41 -04003677 err = btrfs_insert_file_extent(trans, root,
Li Zefan33345d012011-04-20 10:31:50 +08003678 btrfs_ino(inode), cur_offset, 0,
Yan Zheng9036c102008-10-30 14:19:41 -04003679 0, hole_size, 0, hole_size,
3680 0, 0, 0);
Josef Bacik3893e332011-01-31 16:03:11 -05003681 if (err)
3682 break;
Yan, Zheng80825102009-11-12 09:35:36 +00003683
Yan Zheng9036c102008-10-30 14:19:41 -04003684 btrfs_drop_extent_cache(inode, hole_start,
3685 last_byte - 1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003686
3687 btrfs_end_transaction(trans, root);
Yan Zheng9036c102008-10-30 14:19:41 -04003688 }
3689 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003690 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04003691 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00003692 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04003693 break;
3694 }
3695
Yan, Zhenga22285a2010-05-16 10:48:46 -04003696 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003697 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
3698 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003699 return err;
3700}
3701
Josef Bacika41ad392011-01-31 15:30:16 -05003702static int btrfs_setsize(struct inode *inode, loff_t newsize)
Yan, Zheng80825102009-11-12 09:35:36 +00003703{
Josef Bacika41ad392011-01-31 15:30:16 -05003704 loff_t oldsize = i_size_read(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003705 int ret;
3706
Josef Bacika41ad392011-01-31 15:30:16 -05003707 if (newsize == oldsize)
Yan, Zheng80825102009-11-12 09:35:36 +00003708 return 0;
3709
Josef Bacika41ad392011-01-31 15:30:16 -05003710 if (newsize > oldsize) {
3711 i_size_write(inode, newsize);
3712 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
3713 truncate_pagecache(inode, oldsize, newsize);
3714 ret = btrfs_cont_expand(inode, oldsize, newsize);
Yan, Zheng80825102009-11-12 09:35:36 +00003715 if (ret) {
Josef Bacika41ad392011-01-31 15:30:16 -05003716 btrfs_setsize(inode, oldsize);
Yan, Zheng80825102009-11-12 09:35:36 +00003717 return ret;
3718 }
3719
Josef Bacik930f0282011-03-04 14:41:41 -05003720 mark_inode_dirty(inode);
Josef Bacika41ad392011-01-31 15:30:16 -05003721 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00003722
Josef Bacika41ad392011-01-31 15:30:16 -05003723 /*
3724 * We're truncating a file that used to have good data down to
3725 * zero. Make sure it gets into the ordered flush list so that
3726 * any new writes get down to disk quickly.
3727 */
3728 if (newsize == 0)
3729 BTRFS_I(inode)->ordered_data_close = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00003730
Josef Bacika41ad392011-01-31 15:30:16 -05003731 /* we don't support swapfiles, so vmtruncate shouldn't fail */
3732 truncate_setsize(inode, newsize);
3733 ret = btrfs_truncate(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003734 }
3735
Josef Bacika41ad392011-01-31 15:30:16 -05003736 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00003737}
3738
Chris Mason39279cc2007-06-12 06:35:45 -04003739static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
3740{
3741 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08003742 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003743 int err;
3744
Li Zefanb83cc962010-12-20 16:04:08 +08003745 if (btrfs_root_readonly(root))
3746 return -EROFS;
3747
Chris Mason39279cc2007-06-12 06:35:45 -04003748 err = inode_change_ok(inode, attr);
3749 if (err)
3750 return err;
3751
Chris Mason5a3f23d2009-03-31 13:27:11 -04003752 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Josef Bacika41ad392011-01-31 15:30:16 -05003753 err = btrfs_setsize(inode, attr->ia_size);
Yan, Zheng80825102009-11-12 09:35:36 +00003754 if (err)
3755 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003756 }
Yan Zheng9036c102008-10-30 14:19:41 -04003757
Christoph Hellwig10257742010-06-04 11:30:02 +02003758 if (attr->ia_valid) {
3759 setattr_copy(inode, attr);
3760 mark_inode_dirty(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04003761
Christoph Hellwig10257742010-06-04 11:30:02 +02003762 if (attr->ia_valid & ATTR_MODE)
3763 err = btrfs_acl_chmod(inode);
3764 }
3765
Chris Mason39279cc2007-06-12 06:35:45 -04003766 return err;
3767}
Chris Mason61295eb2008-01-14 16:24:38 -05003768
Al Virobd555972010-06-07 11:35:40 -04003769void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003770{
3771 struct btrfs_trans_handle *trans;
3772 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003773 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04003774 int ret;
3775
liubo1abe9b82011-03-24 11:18:59 +00003776 trace_btrfs_inode_evict(inode);
3777
Chris Mason39279cc2007-06-12 06:35:45 -04003778 truncate_inode_pages(&inode->i_data, 0);
Josef Bacik0af3d002010-06-21 14:48:16 -04003779 if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 ||
Li Zefan82d59022011-04-20 10:33:24 +08003780 is_free_space_inode(root, inode)))
Al Virobd555972010-06-07 11:35:40 -04003781 goto no_delete;
3782
Chris Mason39279cc2007-06-12 06:35:45 -04003783 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04003784 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003785 goto no_delete;
3786 }
Al Virobd555972010-06-07 11:35:40 -04003787 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04003788 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04003789
Yan, Zhengc71bf092009-11-12 09:34:40 +00003790 if (root->fs_info->log_root_recovering) {
3791 BUG_ON(!list_empty(&BTRFS_I(inode)->i_orphan));
3792 goto no_delete;
3793 }
3794
Yan, Zheng76dda932009-09-21 16:00:26 -04003795 if (inode->i_nlink > 0) {
3796 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
3797 goto no_delete;
3798 }
3799
Chris Masondbe674a2008-07-17 12:54:05 -04003800 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003801
Yan, Zheng80825102009-11-12 09:35:36 +00003802 while (1) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003803 trans = btrfs_start_transaction(root, 0);
3804 BUG_ON(IS_ERR(trans));
Yan, Zheng80825102009-11-12 09:35:36 +00003805 btrfs_set_trans_block_group(trans, inode);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003806 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00003807
Yan, Zhengd68fc572010-05-16 10:49:58 -04003808 ret = btrfs_block_rsv_check(trans, root,
3809 root->orphan_block_rsv, 0, 5);
3810 if (ret) {
3811 BUG_ON(ret != -EAGAIN);
3812 ret = btrfs_commit_transaction(trans, root);
3813 BUG_ON(ret);
3814 continue;
3815 }
3816
3817 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003818 if (ret != -EAGAIN)
3819 break;
3820
3821 nr = trans->blocks_used;
3822 btrfs_end_transaction(trans, root);
3823 trans = NULL;
3824 btrfs_btree_balance_dirty(root, nr);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003825
Josef Bacik7b128762008-07-24 12:17:14 -04003826 }
3827
Yan, Zheng80825102009-11-12 09:35:36 +00003828 if (ret == 0) {
3829 ret = btrfs_orphan_del(trans, inode);
3830 BUG_ON(ret);
3831 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003832
Li Zefan581bb052011-04-20 10:06:11 +08003833 if (!(root == root->fs_info->tree_root ||
3834 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08003835 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08003836
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003837 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04003838 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003839 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003840no_delete:
Al Virobd555972010-06-07 11:35:40 -04003841 end_writeback(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003842 return;
Chris Mason39279cc2007-06-12 06:35:45 -04003843}
3844
3845/*
3846 * this returns the key found in the dir entry in the location pointer.
3847 * If no dir entries were found, location->objectid is 0.
3848 */
3849static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
3850 struct btrfs_key *location)
3851{
3852 const char *name = dentry->d_name.name;
3853 int namelen = dentry->d_name.len;
3854 struct btrfs_dir_item *di;
3855 struct btrfs_path *path;
3856 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04003857 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003858
3859 path = btrfs_alloc_path();
3860 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -05003861
Li Zefan33345d012011-04-20 10:31:50 +08003862 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04003863 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04003864 if (IS_ERR(di))
3865 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05003866
3867 if (!di || IS_ERR(di))
Chris Mason39544012007-12-12 14:38:19 -05003868 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05003869
Chris Mason5f39d392007-10-15 16:14:19 -04003870 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04003871out:
Chris Mason39279cc2007-06-12 06:35:45 -04003872 btrfs_free_path(path);
3873 return ret;
Chris Mason39544012007-12-12 14:38:19 -05003874out_err:
3875 location->objectid = 0;
3876 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04003877}
3878
3879/*
3880 * when we hit a tree root in a directory, the btrfs part of the inode
3881 * needs to be changed to reflect the root directory of the tree root. This
3882 * is kind of like crossing a mount point.
3883 */
3884static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003885 struct inode *dir,
3886 struct dentry *dentry,
3887 struct btrfs_key *location,
3888 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04003889{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003890 struct btrfs_path *path;
3891 struct btrfs_root *new_root;
3892 struct btrfs_root_ref *ref;
3893 struct extent_buffer *leaf;
3894 int ret;
3895 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003896
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003897 path = btrfs_alloc_path();
3898 if (!path) {
3899 err = -ENOMEM;
3900 goto out;
3901 }
Chris Mason39279cc2007-06-12 06:35:45 -04003902
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003903 err = -ENOENT;
3904 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
3905 BTRFS_I(dir)->root->root_key.objectid,
3906 location->objectid);
3907 if (ret) {
3908 if (ret < 0)
3909 err = ret;
3910 goto out;
3911 }
Chris Mason39279cc2007-06-12 06:35:45 -04003912
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003913 leaf = path->nodes[0];
3914 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08003915 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003916 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
3917 goto out;
3918
3919 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
3920 (unsigned long)(ref + 1),
3921 dentry->d_name.len);
3922 if (ret)
3923 goto out;
3924
3925 btrfs_release_path(root->fs_info->tree_root, path);
3926
3927 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
3928 if (IS_ERR(new_root)) {
3929 err = PTR_ERR(new_root);
3930 goto out;
3931 }
3932
3933 if (btrfs_root_refs(&new_root->root_item) == 0) {
3934 err = -ENOENT;
3935 goto out;
3936 }
3937
3938 *sub_root = new_root;
3939 location->objectid = btrfs_root_dirid(&new_root->root_item);
3940 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04003941 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003942 err = 0;
3943out:
3944 btrfs_free_path(path);
3945 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003946}
3947
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003948static void inode_tree_add(struct inode *inode)
3949{
3950 struct btrfs_root *root = BTRFS_I(inode)->root;
3951 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003952 struct rb_node **p;
3953 struct rb_node *parent;
Li Zefan33345d012011-04-20 10:31:50 +08003954 u64 ino = btrfs_ino(inode);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003955again:
3956 p = &root->inode_tree.rb_node;
3957 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003958
Al Viro1d3382cb2010-10-23 15:19:20 -04003959 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04003960 return;
3961
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003962 spin_lock(&root->inode_lock);
3963 while (*p) {
3964 parent = *p;
3965 entry = rb_entry(parent, struct btrfs_inode, rb_node);
3966
Li Zefan33345d012011-04-20 10:31:50 +08003967 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003968 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08003969 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003970 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003971 else {
3972 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04003973 (I_WILL_FREE | I_FREEING)));
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003974 rb_erase(parent, &root->inode_tree);
3975 RB_CLEAR_NODE(parent);
3976 spin_unlock(&root->inode_lock);
3977 goto again;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003978 }
3979 }
3980 rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
3981 rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
3982 spin_unlock(&root->inode_lock);
3983}
3984
3985static void inode_tree_del(struct inode *inode)
3986{
3987 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04003988 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003989
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003990 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003991 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003992 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003993 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04003994 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003995 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003996 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04003997
Josef Bacik0af3d002010-06-21 14:48:16 -04003998 /*
3999 * Free space cache has inodes in the tree root, but the tree root has a
4000 * root_refs of 0, so this could end up dropping the tree root as a
4001 * snapshot, so we need the extra !root->fs_info->tree_root check to
4002 * make sure we don't drop it.
4003 */
4004 if (empty && btrfs_root_refs(&root->root_item) == 0 &&
4005 root != root->fs_info->tree_root) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004006 synchronize_srcu(&root->fs_info->subvol_srcu);
4007 spin_lock(&root->inode_lock);
4008 empty = RB_EMPTY_ROOT(&root->inode_tree);
4009 spin_unlock(&root->inode_lock);
4010 if (empty)
4011 btrfs_add_dead_root(root);
4012 }
4013}
4014
4015int btrfs_invalidate_inodes(struct btrfs_root *root)
4016{
4017 struct rb_node *node;
4018 struct rb_node *prev;
4019 struct btrfs_inode *entry;
4020 struct inode *inode;
4021 u64 objectid = 0;
4022
4023 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4024
4025 spin_lock(&root->inode_lock);
4026again:
4027 node = root->inode_tree.rb_node;
4028 prev = NULL;
4029 while (node) {
4030 prev = node;
4031 entry = rb_entry(node, struct btrfs_inode, rb_node);
4032
Li Zefan33345d012011-04-20 10:31:50 +08004033 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004034 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004035 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004036 node = node->rb_right;
4037 else
4038 break;
4039 }
4040 if (!node) {
4041 while (prev) {
4042 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004043 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004044 node = prev;
4045 break;
4046 }
4047 prev = rb_next(prev);
4048 }
4049 }
4050 while (node) {
4051 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004052 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04004053 inode = igrab(&entry->vfs_inode);
4054 if (inode) {
4055 spin_unlock(&root->inode_lock);
4056 if (atomic_read(&inode->i_count) > 1)
4057 d_prune_aliases(inode);
4058 /*
Al Viro45321ac2010-06-07 13:43:19 -04004059 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04004060 * the inode cache when its usage count
4061 * hits zero.
4062 */
4063 iput(inode);
4064 cond_resched();
4065 spin_lock(&root->inode_lock);
4066 goto again;
4067 }
4068
4069 if (cond_resched_lock(&root->inode_lock))
4070 goto again;
4071
4072 node = rb_next(node);
4073 }
4074 spin_unlock(&root->inode_lock);
4075 return 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004076}
4077
Chris Masone02119d2008-09-05 16:13:11 -04004078static int btrfs_init_locked_inode(struct inode *inode, void *p)
4079{
4080 struct btrfs_iget_args *args = p;
4081 inode->i_ino = args->ino;
Chris Masone02119d2008-09-05 16:13:11 -04004082 BTRFS_I(inode)->root = args->root;
Josef Bacik6a632092009-02-20 11:00:09 -05004083 btrfs_set_inode_space_info(args->root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004084 return 0;
4085}
4086
4087static int btrfs_find_actor(struct inode *inode, void *opaque)
4088{
4089 struct btrfs_iget_args *args = opaque;
Li Zefan33345d012011-04-20 10:31:50 +08004090 return args->ino == btrfs_ino(inode) &&
Chris Masond3977122009-01-05 21:25:51 -05004091 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004092}
4093
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004094static struct inode *btrfs_iget_locked(struct super_block *s,
4095 u64 objectid,
4096 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04004097{
4098 struct inode *inode;
4099 struct btrfs_iget_args args;
4100 args.ino = objectid;
4101 args.root = root;
4102
4103 inode = iget5_locked(s, objectid, btrfs_find_actor,
4104 btrfs_init_locked_inode,
4105 (void *)&args);
4106 return inode;
4107}
4108
Balaji Rao1a54ef82008-07-21 02:01:04 +05304109/* Get an inode object given its location and corresponding root.
4110 * Returns in *is_new if the inode was read from disk
4111 */
4112struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00004113 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05304114{
4115 struct inode *inode;
4116
4117 inode = btrfs_iget_locked(s, location->objectid, root);
4118 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004119 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05304120
4121 if (inode->i_state & I_NEW) {
4122 BTRFS_I(inode)->root = root;
4123 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
4124 btrfs_read_locked_inode(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004125 inode_tree_add(inode);
Balaji Rao1a54ef82008-07-21 02:01:04 +05304126 unlock_new_inode(inode);
Josef Bacik73f73412009-12-04 17:38:27 +00004127 if (new)
4128 *new = 1;
Balaji Rao1a54ef82008-07-21 02:01:04 +05304129 }
4130
4131 return inode;
4132}
4133
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004134static struct inode *new_simple_dir(struct super_block *s,
4135 struct btrfs_key *key,
4136 struct btrfs_root *root)
4137{
4138 struct inode *inode = new_inode(s);
4139
4140 if (!inode)
4141 return ERR_PTR(-ENOMEM);
4142
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004143 BTRFS_I(inode)->root = root;
4144 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
4145 BTRFS_I(inode)->dummy_inode = 1;
4146
4147 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
4148 inode->i_op = &simple_dir_inode_operations;
4149 inode->i_fop = &simple_dir_operations;
4150 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
4151 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
4152
4153 return inode;
4154}
4155
Chris Mason3de45862008-11-17 21:02:50 -05004156struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04004157{
Chris Masond3977122009-01-05 21:25:51 -05004158 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004159 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004160 struct btrfs_root *sub_root = root;
4161 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04004162 int index;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004163 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004164
4165 if (dentry->d_name.len > BTRFS_NAME_LEN)
4166 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04004167
Chris Mason39279cc2007-06-12 06:35:45 -04004168 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04004169
Chris Mason39279cc2007-06-12 06:35:45 -04004170 if (ret < 0)
4171 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04004172
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004173 if (location.objectid == 0)
4174 return NULL;
4175
4176 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00004177 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004178 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004179 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004180
4181 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
4182
Yan, Zheng76dda932009-09-21 16:00:26 -04004183 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004184 ret = fixup_tree_root_location(root, dir, dentry,
4185 &location, &sub_root);
4186 if (ret < 0) {
4187 if (ret != -ENOENT)
4188 inode = ERR_PTR(ret);
4189 else
4190 inode = new_simple_dir(dir->i_sb, &location, sub_root);
4191 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00004192 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004193 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004194 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
4195
Julia Lawall34d19ba2011-01-24 19:55:19 +00004196 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00004197 down_read(&root->fs_info->cleanup_work_sem);
4198 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004199 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00004200 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004201 if (ret)
4202 inode = ERR_PTR(ret);
Yan, Zhengc71bf092009-11-12 09:34:40 +00004203 }
4204
Chris Mason3de45862008-11-17 21:02:50 -05004205 return inode;
4206}
4207
Nick Pigginfe15ce42011-01-07 17:49:23 +11004208static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04004209{
4210 struct btrfs_root *root;
4211
Yan, Zhengefefb142009-10-09 09:25:16 -04004212 if (!dentry->d_inode && !IS_ROOT(dentry))
4213 dentry = dentry->d_parent;
Yan, Zheng76dda932009-09-21 16:00:26 -04004214
Yan, Zhengefefb142009-10-09 09:25:16 -04004215 if (dentry->d_inode) {
4216 root = BTRFS_I(dentry->d_inode)->root;
4217 if (btrfs_root_refs(&root->root_item) == 0)
4218 return 1;
4219 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004220 return 0;
4221}
4222
Chris Mason3de45862008-11-17 21:02:50 -05004223static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
4224 struct nameidata *nd)
4225{
4226 struct inode *inode;
4227
Chris Mason3de45862008-11-17 21:02:50 -05004228 inode = btrfs_lookup_dentry(dir, dentry);
4229 if (IS_ERR(inode))
4230 return ERR_CAST(inode);
Josef Bacik7b128762008-07-24 12:17:14 -04004231
Chris Mason39279cc2007-06-12 06:35:45 -04004232 return d_splice_alias(inode, dentry);
4233}
4234
Chris Mason39279cc2007-06-12 06:35:45 -04004235static unsigned char btrfs_filetype_table[] = {
4236 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
4237};
4238
David Woodhousecbdf5a22008-08-06 19:42:33 +01004239static int btrfs_real_readdir(struct file *filp, void *dirent,
4240 filldir_t filldir)
Chris Mason39279cc2007-06-12 06:35:45 -04004241{
Chris Mason6da6aba2007-12-18 16:15:09 -05004242 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004243 struct btrfs_root *root = BTRFS_I(inode)->root;
4244 struct btrfs_item *item;
4245 struct btrfs_dir_item *di;
4246 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04004247 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004248 struct btrfs_path *path;
4249 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04004250 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004251 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04004252 unsigned char d_type;
4253 int over = 0;
4254 u32 di_cur;
4255 u32 di_total;
4256 u32 di_len;
4257 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04004258 char tmp_name[32];
4259 char *name_ptr;
4260 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04004261
4262 /* FIXME, use a real flag for deciding about the key type */
4263 if (root->fs_info->tree_root == root)
4264 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04004265
Chris Mason39544012007-12-12 14:38:19 -05004266 /* special case for "." */
4267 if (filp->f_pos == 0) {
Li Zefan33345d012011-04-20 10:31:50 +08004268 over = filldir(dirent, ".", 1, 1, btrfs_ino(inode), DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05004269 if (over)
4270 return 0;
4271 filp->f_pos = 1;
4272 }
Chris Mason39544012007-12-12 14:38:19 -05004273 /* special case for .., just use the back ref */
4274 if (filp->f_pos == 1) {
David Woodhouse5ecc7e52008-08-17 15:14:48 +01004275 u64 pino = parent_ino(filp->f_path.dentry);
Chris Mason39544012007-12-12 14:38:19 -05004276 over = filldir(dirent, "..", 2,
David Woodhouse5ecc7e52008-08-17 15:14:48 +01004277 2, pino, DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05004278 if (over)
David Woodhouse49593bf2008-08-17 17:08:36 +01004279 return 0;
Chris Mason39544012007-12-12 14:38:19 -05004280 filp->f_pos = 2;
4281 }
David Woodhouse49593bf2008-08-17 17:08:36 +01004282 path = btrfs_alloc_path();
4283 path->reada = 2;
4284
Chris Mason39279cc2007-06-12 06:35:45 -04004285 btrfs_set_key_type(&key, key_type);
4286 key.offset = filp->f_pos;
Li Zefan33345d012011-04-20 10:31:50 +08004287 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004288
Chris Mason39279cc2007-06-12 06:35:45 -04004289 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4290 if (ret < 0)
4291 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01004292
4293 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04004294 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04004295 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08004296 if (slot >= btrfs_header_nritems(leaf)) {
4297 ret = btrfs_next_leaf(root, path);
4298 if (ret < 0)
4299 goto err;
4300 else if (ret > 0)
4301 break;
4302 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04004303 }
Chris Mason3de45862008-11-17 21:02:50 -05004304
Chris Mason5f39d392007-10-15 16:14:19 -04004305 item = btrfs_item_nr(leaf, slot);
4306 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4307
4308 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04004309 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004310 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004311 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004312 if (found_key.offset < filp->f_pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08004313 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04004314
4315 filp->f_pos = found_key.offset;
David Woodhouse49593bf2008-08-17 17:08:36 +01004316
Chris Mason39279cc2007-06-12 06:35:45 -04004317 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
4318 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04004319 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01004320
4321 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04004322 struct btrfs_key location;
4323
Josef Bacik22a94d42011-03-16 16:47:17 -04004324 if (verify_dir_item(root, leaf, di))
4325 break;
4326
Chris Mason5f39d392007-10-15 16:14:19 -04004327 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01004328 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04004329 name_ptr = tmp_name;
4330 } else {
4331 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01004332 if (!name_ptr) {
4333 ret = -ENOMEM;
4334 goto err;
4335 }
Chris Mason5f39d392007-10-15 16:14:19 -04004336 }
4337 read_extent_buffer(leaf, name_ptr,
4338 (unsigned long)(di + 1), name_len);
4339
4340 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
4341 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05004342
4343 /* is this a reference to our own snapshot? If so
4344 * skip it
4345 */
4346 if (location.type == BTRFS_ROOT_ITEM_KEY &&
4347 location.objectid == root->root_key.objectid) {
4348 over = 0;
4349 goto skip;
4350 }
Chris Mason5f39d392007-10-15 16:14:19 -04004351 over = filldir(dirent, name_ptr, name_len,
David Woodhouse49593bf2008-08-17 17:08:36 +01004352 found_key.offset, location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04004353 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04004354
Chris Mason3de45862008-11-17 21:02:50 -05004355skip:
Chris Mason5f39d392007-10-15 16:14:19 -04004356 if (name_ptr != tmp_name)
4357 kfree(name_ptr);
4358
Chris Mason39279cc2007-06-12 06:35:45 -04004359 if (over)
4360 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05004361 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01004362 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04004363 di_cur += di_len;
4364 di = (struct btrfs_dir_item *)((char *)di + di_len);
4365 }
Li Zefanb9e03af2011-03-23 10:43:58 +08004366next:
4367 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04004368 }
David Woodhouse49593bf2008-08-17 17:08:36 +01004369
4370 /* Reached end of directory/root. Bump pos past the last item. */
Yan Zheng5e591a02008-02-19 11:41:02 -05004371 if (key_type == BTRFS_DIR_INDEX_KEY)
Jan Engelhardt406266a2009-12-09 22:00:38 +00004372 /*
4373 * 32-bit glibc will use getdents64, but then strtol -
4374 * so the last number we can serve is this.
4375 */
4376 filp->f_pos = 0x7fffffff;
Yan Zheng5e591a02008-02-19 11:41:02 -05004377 else
4378 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04004379nopos:
4380 ret = 0;
4381err:
Chris Mason39279cc2007-06-12 06:35:45 -04004382 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004383 return ret;
4384}
4385
Christoph Hellwiga9185b42010-03-05 09:21:37 +01004386int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04004387{
4388 struct btrfs_root *root = BTRFS_I(inode)->root;
4389 struct btrfs_trans_handle *trans;
4390 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04004391 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04004392
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004393 if (BTRFS_I(inode)->dummy_inode)
Chris Mason4ca8b412008-08-05 13:30:48 -04004394 return 0;
4395
Josef Bacik0af3d002010-06-21 14:48:16 -04004396 smp_mb();
Li Zefan82d59022011-04-20 10:33:24 +08004397 if (root->fs_info->closing && is_free_space_inode(root, inode))
4398 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04004399
Christoph Hellwiga9185b42010-03-05 09:21:37 +01004400 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04004401 if (nolock)
4402 trans = btrfs_join_transaction_nolock(root, 1);
4403 else
4404 trans = btrfs_join_transaction(root, 1);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00004405 if (IS_ERR(trans))
4406 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04004407 btrfs_set_trans_block_group(trans, inode);
Josef Bacik0af3d002010-06-21 14:48:16 -04004408 if (nolock)
4409 ret = btrfs_end_transaction_nolock(trans, root);
4410 else
4411 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004412 }
4413 return ret;
4414}
4415
4416/*
Chris Mason54aa1f42007-06-22 14:16:25 -04004417 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04004418 * inode changes. But, it is most likely to find the inode in cache.
4419 * FIXME, needs more benchmarking...there are no reasons other than performance
4420 * to keep or drop this code.
4421 */
4422void btrfs_dirty_inode(struct inode *inode)
4423{
4424 struct btrfs_root *root = BTRFS_I(inode)->root;
4425 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004426 int ret;
4427
4428 if (BTRFS_I(inode)->dummy_inode)
4429 return;
Chris Mason39279cc2007-06-12 06:35:45 -04004430
Chris Masonf9295742008-07-17 12:54:14 -04004431 trans = btrfs_join_transaction(root, 1);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00004432 BUG_ON(IS_ERR(trans));
Chris Mason39279cc2007-06-12 06:35:45 -04004433 btrfs_set_trans_block_group(trans, inode);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004434
4435 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04004436 if (ret && ret == -ENOSPC) {
4437 /* whoops, lets try again with the full transaction */
4438 btrfs_end_transaction(trans, root);
4439 trans = btrfs_start_transaction(root, 1);
Chris Mason9aeead72010-05-27 10:23:00 -04004440 if (IS_ERR(trans)) {
4441 if (printk_ratelimit()) {
4442 printk(KERN_ERR "btrfs: fail to "
Li Zefan33345d012011-04-20 10:31:50 +08004443 "dirty inode %llu error %ld\n",
4444 (unsigned long long)btrfs_ino(inode),
4445 PTR_ERR(trans));
Chris Mason9aeead72010-05-27 10:23:00 -04004446 }
4447 return;
4448 }
Chris Mason94b60442010-05-26 11:02:00 -04004449 btrfs_set_trans_block_group(trans, inode);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004450
Chris Mason94b60442010-05-26 11:02:00 -04004451 ret = btrfs_update_inode(trans, root, inode);
4452 if (ret) {
Chris Mason9aeead72010-05-27 10:23:00 -04004453 if (printk_ratelimit()) {
4454 printk(KERN_ERR "btrfs: fail to "
Li Zefan33345d012011-04-20 10:31:50 +08004455 "dirty inode %llu error %d\n",
4456 (unsigned long long)btrfs_ino(inode),
4457 ret);
Chris Mason9aeead72010-05-27 10:23:00 -04004458 }
Chris Mason94b60442010-05-26 11:02:00 -04004459 }
4460 }
Chris Mason39279cc2007-06-12 06:35:45 -04004461 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004462}
4463
Chris Masond352ac62008-09-29 15:18:18 -04004464/*
4465 * find the highest existing sequence number in a directory
4466 * and then set the in-memory index_cnt variable to reflect
4467 * free sequence numbers
4468 */
Josef Bacikaec74772008-07-24 12:12:38 -04004469static int btrfs_set_inode_index_count(struct inode *inode)
4470{
4471 struct btrfs_root *root = BTRFS_I(inode)->root;
4472 struct btrfs_key key, found_key;
4473 struct btrfs_path *path;
4474 struct extent_buffer *leaf;
4475 int ret;
4476
Li Zefan33345d012011-04-20 10:31:50 +08004477 key.objectid = btrfs_ino(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004478 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
4479 key.offset = (u64)-1;
4480
4481 path = btrfs_alloc_path();
4482 if (!path)
4483 return -ENOMEM;
4484
4485 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4486 if (ret < 0)
4487 goto out;
4488 /* FIXME: we should be able to handle this */
4489 if (ret == 0)
4490 goto out;
4491 ret = 0;
4492
4493 /*
4494 * MAGIC NUMBER EXPLANATION:
4495 * since we search a directory based on f_pos we have to start at 2
4496 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
4497 * else has to start at 2
4498 */
4499 if (path->slots[0] == 0) {
4500 BTRFS_I(inode)->index_cnt = 2;
4501 goto out;
4502 }
4503
4504 path->slots[0]--;
4505
4506 leaf = path->nodes[0];
4507 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4508
Li Zefan33345d012011-04-20 10:31:50 +08004509 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacikaec74772008-07-24 12:12:38 -04004510 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
4511 BTRFS_I(inode)->index_cnt = 2;
4512 goto out;
4513 }
4514
4515 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
4516out:
4517 btrfs_free_path(path);
4518 return ret;
4519}
4520
Chris Masond352ac62008-09-29 15:18:18 -04004521/*
4522 * helper to find a free sequence number in a given directory. This current
4523 * code is very simple, later versions will do smarter things in the btree
4524 */
Chris Mason3de45862008-11-17 21:02:50 -05004525int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04004526{
4527 int ret = 0;
4528
4529 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
4530 ret = btrfs_set_inode_index_count(dir);
Chris Masond3977122009-01-05 21:25:51 -05004531 if (ret)
Josef Bacikaec74772008-07-24 12:12:38 -04004532 return ret;
4533 }
4534
Chris Mason00e4e6b2008-08-05 11:18:09 -04004535 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04004536 BTRFS_I(dir)->index_cnt++;
4537
4538 return ret;
4539}
4540
Chris Mason39279cc2007-06-12 06:35:45 -04004541static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
4542 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04004543 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05004544 const char *name, int name_len,
Yan Zhengd2fb3432008-12-11 16:30:39 -05004545 u64 ref_objectid, u64 objectid,
4546 u64 alloc_hint, int mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04004547{
4548 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004549 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04004550 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04004551 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05004552 struct btrfs_inode_ref *ref;
4553 struct btrfs_key key[2];
4554 u32 sizes[2];
4555 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04004556 int ret;
4557 int owner;
4558
Chris Mason5f39d392007-10-15 16:14:19 -04004559 path = btrfs_alloc_path();
4560 BUG_ON(!path);
4561
Chris Mason39279cc2007-06-12 06:35:45 -04004562 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00004563 if (!inode) {
4564 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004565 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00004566 }
Chris Mason39279cc2007-06-12 06:35:45 -04004567
Li Zefan581bb052011-04-20 10:06:11 +08004568 /*
4569 * we have to initialize this early, so we can reclaim the inode
4570 * number if we fail afterwards in this function.
4571 */
4572 inode->i_ino = objectid;
4573
Josef Bacikaec74772008-07-24 12:12:38 -04004574 if (dir) {
liubo1abe9b82011-03-24 11:18:59 +00004575 trace_btrfs_inode_request(dir);
4576
Chris Mason3de45862008-11-17 21:02:50 -05004577 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04004578 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00004579 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04004580 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004581 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04004582 }
Josef Bacikaec74772008-07-24 12:12:38 -04004583 }
4584 /*
4585 * index_cnt is ignored for everything but a dir,
4586 * btrfs_get_inode_index_count has an explanation for the magic
4587 * number
4588 */
4589 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04004590 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04004591 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00004592 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik6a632092009-02-20 11:00:09 -05004593 btrfs_set_inode_space_info(root, inode);
Chris Masonb888db22007-08-27 16:49:44 -04004594
Chris Mason39279cc2007-06-12 06:35:45 -04004595 if (mode & S_IFDIR)
4596 owner = 0;
4597 else
4598 owner = 1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05004599 BTRFS_I(inode)->block_group =
4600 btrfs_find_block_group(root, 0, alloc_hint, owner);
Chris Mason9c583092008-01-29 15:15:18 -05004601
4602 key[0].objectid = objectid;
4603 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
4604 key[0].offset = 0;
4605
4606 key[1].objectid = objectid;
4607 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
4608 key[1].offset = ref_objectid;
4609
4610 sizes[0] = sizeof(struct btrfs_inode_item);
4611 sizes[1] = name_len + sizeof(*ref);
4612
Chris Masonb9473432009-03-13 11:00:37 -04004613 path->leave_spinning = 1;
Chris Mason9c583092008-01-29 15:15:18 -05004614 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
4615 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04004616 goto fail;
4617
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03004618 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004619 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004620 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04004621 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4622 struct btrfs_inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04004623 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05004624
4625 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
4626 struct btrfs_inode_ref);
4627 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04004628 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05004629 ptr = (unsigned long)(ref + 1);
4630 write_extent_buffer(path->nodes[0], name, ptr, name_len);
4631
Chris Mason5f39d392007-10-15 16:14:19 -04004632 btrfs_mark_buffer_dirty(path->nodes[0]);
4633 btrfs_free_path(path);
4634
Chris Mason39279cc2007-06-12 06:35:45 -04004635 location = &BTRFS_I(inode)->location;
4636 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04004637 location->offset = 0;
4638 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
4639
Christoph Hellwig6cbff002009-04-17 10:37:41 +02004640 btrfs_inherit_iflags(inode, dir);
4641
Chris Mason94272162009-07-02 12:26:06 -04004642 if ((mode & S_IFREG)) {
4643 if (btrfs_test_opt(root, NODATASUM))
4644 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo75e7cb72011-03-22 10:12:20 +00004645 if (btrfs_test_opt(root, NODATACOW) ||
4646 (BTRFS_I(dir)->flags & BTRFS_INODE_NODATACOW))
Chris Mason94272162009-07-02 12:26:06 -04004647 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
4648 }
4649
Chris Mason39279cc2007-06-12 06:35:45 -04004650 insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004651 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00004652
4653 trace_btrfs_inode_new(inode);
4654
Chris Mason39279cc2007-06-12 06:35:45 -04004655 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004656fail:
Josef Bacikaec74772008-07-24 12:12:38 -04004657 if (dir)
4658 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04004659 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04004660 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004661 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004662}
4663
4664static inline u8 btrfs_inode_type(struct inode *inode)
4665{
4666 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
4667}
4668
Chris Masond352ac62008-09-29 15:18:18 -04004669/*
4670 * utility function to add 'inode' into 'parent_inode' with
4671 * a give name and a given sequence number.
4672 * if 'add_backref' is true, also insert a backref from the
4673 * inode to the parent directory.
4674 */
Chris Masone02119d2008-09-05 16:13:11 -04004675int btrfs_add_link(struct btrfs_trans_handle *trans,
4676 struct inode *parent_inode, struct inode *inode,
4677 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04004678{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004679 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004680 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04004681 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08004682 u64 ino = btrfs_ino(inode);
4683 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004684
Li Zefan33345d012011-04-20 10:31:50 +08004685 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004686 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
4687 } else {
Li Zefan33345d012011-04-20 10:31:50 +08004688 key.objectid = ino;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004689 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
4690 key.offset = 0;
4691 }
Chris Mason39279cc2007-06-12 06:35:45 -04004692
Li Zefan33345d012011-04-20 10:31:50 +08004693 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004694 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
4695 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08004696 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004697 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08004698 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
4699 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004700 }
4701
Chris Mason39279cc2007-06-12 06:35:45 -04004702 if (ret == 0) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004703 ret = btrfs_insert_dir_item(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08004704 parent_ino, &key,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004705 btrfs_inode_type(inode), index);
4706 BUG_ON(ret);
4707
Chris Masondbe674a2008-07-17 12:54:05 -04004708 btrfs_i_size_write(parent_inode, parent_inode->i_size +
Chris Masone02119d2008-09-05 16:13:11 -04004709 name_len * 2);
Chris Mason79c44582007-06-25 10:09:33 -04004710 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Masone02119d2008-09-05 16:13:11 -04004711 ret = btrfs_update_inode(trans, root, parent_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004712 }
4713 return ret;
4714}
4715
4716static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00004717 struct inode *dir, struct dentry *dentry,
4718 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04004719{
Josef Bacika1b075d2010-11-19 20:36:11 +00004720 int err = btrfs_add_link(trans, dir, inode,
4721 dentry->d_name.name, dentry->d_name.len,
4722 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004723 if (!err) {
4724 d_instantiate(dentry, inode);
4725 return 0;
4726 }
4727 if (err > 0)
4728 err = -EEXIST;
4729 return err;
4730}
4731
Josef Bacik618e21d2007-07-11 10:18:17 -04004732static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
4733 int mode, dev_t rdev)
4734{
4735 struct btrfs_trans_handle *trans;
4736 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05004737 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04004738 int err;
4739 int drop_inode = 0;
4740 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05004741 unsigned long nr = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004742 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04004743
4744 if (!new_valid_dev(rdev))
4745 return -EINVAL;
4746
Josef Bacik9ed74f22009-09-11 16:12:44 -04004747 /*
4748 * 2 for inode item and ref
4749 * 2 for dir items
4750 * 1 for xattr if selinux is on
4751 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004752 trans = btrfs_start_transaction(root, 5);
4753 if (IS_ERR(trans))
4754 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05004755
Josef Bacik618e21d2007-07-11 10:18:17 -04004756 btrfs_set_trans_block_group(trans, dir);
4757
Li Zefan581bb052011-04-20 10:06:11 +08004758 err = btrfs_find_free_ino(root, &objectid);
4759 if (err)
4760 goto out_unlock;
4761
Josef Bacikaec74772008-07-24 12:12:38 -04004762 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08004763 dentry->d_name.len, btrfs_ino(dir), objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04004764 BTRFS_I(dir)->block_group, mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04004765 if (IS_ERR(inode)) {
4766 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04004767 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04004768 }
Josef Bacik618e21d2007-07-11 10:18:17 -04004769
Eric Paris2a7dba32011-02-01 11:05:39 -05004770 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04004771 if (err) {
4772 drop_inode = 1;
4773 goto out_unlock;
4774 }
4775
Josef Bacik618e21d2007-07-11 10:18:17 -04004776 btrfs_set_trans_block_group(trans, inode);
Josef Bacika1b075d2010-11-19 20:36:11 +00004777 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04004778 if (err)
4779 drop_inode = 1;
4780 else {
4781 inode->i_op = &btrfs_special_inode_operations;
4782 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04004783 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04004784 }
Josef Bacik618e21d2007-07-11 10:18:17 -04004785 btrfs_update_inode_block_group(trans, inode);
4786 btrfs_update_inode_block_group(trans, dir);
4787out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004788 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04004789 btrfs_end_transaction_throttle(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004790 btrfs_btree_balance_dirty(root, nr);
Josef Bacik618e21d2007-07-11 10:18:17 -04004791 if (drop_inode) {
4792 inode_dec_link_count(inode);
4793 iput(inode);
4794 }
Josef Bacik618e21d2007-07-11 10:18:17 -04004795 return err;
4796}
4797
Chris Mason39279cc2007-06-12 06:35:45 -04004798static int btrfs_create(struct inode *dir, struct dentry *dentry,
4799 int mode, struct nameidata *nd)
4800{
4801 struct btrfs_trans_handle *trans;
4802 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05004803 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04004804 int drop_inode = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004805 int err;
Chris Mason1832a6d2007-12-21 16:27:21 -05004806 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004807 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004808 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004809
Josef Bacik9ed74f22009-09-11 16:12:44 -04004810 /*
4811 * 2 for inode item and ref
4812 * 2 for dir items
4813 * 1 for xattr if selinux is on
4814 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004815 trans = btrfs_start_transaction(root, 5);
4816 if (IS_ERR(trans))
4817 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004818
Chris Mason39279cc2007-06-12 06:35:45 -04004819 btrfs_set_trans_block_group(trans, dir);
4820
Li Zefan581bb052011-04-20 10:06:11 +08004821 err = btrfs_find_free_ino(root, &objectid);
4822 if (err)
4823 goto out_unlock;
4824
Josef Bacikaec74772008-07-24 12:12:38 -04004825 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08004826 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacika1b075d2010-11-19 20:36:11 +00004827 BTRFS_I(dir)->block_group, mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04004828 if (IS_ERR(inode)) {
4829 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004830 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04004831 }
Chris Mason39279cc2007-06-12 06:35:45 -04004832
Eric Paris2a7dba32011-02-01 11:05:39 -05004833 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04004834 if (err) {
4835 drop_inode = 1;
4836 goto out_unlock;
4837 }
4838
Chris Mason39279cc2007-06-12 06:35:45 -04004839 btrfs_set_trans_block_group(trans, inode);
Josef Bacika1b075d2010-11-19 20:36:11 +00004840 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004841 if (err)
4842 drop_inode = 1;
4843 else {
4844 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04004845 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04004846 inode->i_fop = &btrfs_file_operations;
4847 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05004848 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04004849 }
Chris Mason39279cc2007-06-12 06:35:45 -04004850 btrfs_update_inode_block_group(trans, inode);
4851 btrfs_update_inode_block_group(trans, dir);
4852out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004853 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04004854 btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004855 if (drop_inode) {
4856 inode_dec_link_count(inode);
4857 iput(inode);
4858 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004859 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004860 return err;
4861}
4862
4863static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
4864 struct dentry *dentry)
4865{
4866 struct btrfs_trans_handle *trans;
4867 struct btrfs_root *root = BTRFS_I(dir)->root;
4868 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004869 u64 index;
Chris Mason1832a6d2007-12-21 16:27:21 -05004870 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004871 int err;
4872 int drop_inode = 0;
4873
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00004874 /* do not allow sys_link's with other subvols of the same device */
4875 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00004876 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00004877
Al Viroc055e992011-03-04 17:15:18 +00004878 if (inode->i_nlink == ~0U)
4879 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004880
Chris Mason3de45862008-11-17 21:02:50 -05004881 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004882 if (err)
4883 goto fail;
4884
Yan, Zhenga22285a2010-05-16 10:48:46 -04004885 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00004886 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04004887 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00004888 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04004889 */
Miao Xie7e6b6462011-02-18 09:21:17 +00004890 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004891 if (IS_ERR(trans)) {
4892 err = PTR_ERR(trans);
4893 goto fail;
4894 }
Chris Mason5f39d392007-10-15 16:14:19 -04004895
Miao Xie31534952011-04-13 13:19:21 +08004896 btrfs_inc_nlink(inode);
4897 inode->i_ctime = CURRENT_TIME;
4898
Chris Mason39279cc2007-06-12 06:35:45 -04004899 btrfs_set_trans_block_group(trans, dir);
Al Viro7de9c6ee2010-10-23 11:11:40 -04004900 ihold(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004901
Josef Bacika1b075d2010-11-19 20:36:11 +00004902 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04004903
Yan, Zhenga5719522009-09-24 09:17:31 -04004904 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04004905 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04004906 } else {
Josef Bacik6a912212010-11-20 09:48:00 +00004907 struct dentry *parent = dget_parent(dentry);
Yan, Zhenga5719522009-09-24 09:17:31 -04004908 btrfs_update_inode_block_group(trans, dir);
4909 err = btrfs_update_inode(trans, root, inode);
4910 BUG_ON(err);
Josef Bacik6a912212010-11-20 09:48:00 +00004911 btrfs_log_new_name(trans, inode, NULL, parent);
4912 dput(parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04004913 }
Chris Mason39279cc2007-06-12 06:35:45 -04004914
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004915 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04004916 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05004917fail:
Chris Mason39279cc2007-06-12 06:35:45 -04004918 if (drop_inode) {
4919 inode_dec_link_count(inode);
4920 iput(inode);
4921 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004922 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004923 return err;
4924}
4925
Chris Mason39279cc2007-06-12 06:35:45 -04004926static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
4927{
Chris Masonb9d86662008-05-02 16:13:49 -04004928 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04004929 struct btrfs_trans_handle *trans;
4930 struct btrfs_root *root = BTRFS_I(dir)->root;
4931 int err = 0;
4932 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04004933 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004934 u64 index = 0;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004935 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04004936
Josef Bacik9ed74f22009-09-11 16:12:44 -04004937 /*
4938 * 2 items for inode and ref
4939 * 2 items for dir items
4940 * 1 for xattr if selinux is on
4941 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004942 trans = btrfs_start_transaction(root, 5);
4943 if (IS_ERR(trans))
4944 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004945 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -04004946
Li Zefan581bb052011-04-20 10:06:11 +08004947 err = btrfs_find_free_ino(root, &objectid);
4948 if (err)
4949 goto out_fail;
4950
Josef Bacikaec74772008-07-24 12:12:38 -04004951 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08004952 dentry->d_name.len, btrfs_ino(dir), objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04004953 BTRFS_I(dir)->block_group, S_IFDIR | mode,
4954 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04004955 if (IS_ERR(inode)) {
4956 err = PTR_ERR(inode);
4957 goto out_fail;
4958 }
Chris Mason5f39d392007-10-15 16:14:19 -04004959
Chris Mason39279cc2007-06-12 06:35:45 -04004960 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04004961
Eric Paris2a7dba32011-02-01 11:05:39 -05004962 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04004963 if (err)
4964 goto out_fail;
4965
Chris Mason39279cc2007-06-12 06:35:45 -04004966 inode->i_op = &btrfs_dir_inode_operations;
4967 inode->i_fop = &btrfs_dir_file_operations;
4968 btrfs_set_trans_block_group(trans, inode);
4969
Chris Masondbe674a2008-07-17 12:54:05 -04004970 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004971 err = btrfs_update_inode(trans, root, inode);
4972 if (err)
4973 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04004974
Josef Bacika1b075d2010-11-19 20:36:11 +00004975 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
4976 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004977 if (err)
4978 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04004979
Chris Mason39279cc2007-06-12 06:35:45 -04004980 d_instantiate(dentry, inode);
4981 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004982 btrfs_update_inode_block_group(trans, inode);
4983 btrfs_update_inode_block_group(trans, dir);
4984
4985out_fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004986 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04004987 btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004988 if (drop_on_err)
4989 iput(inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004990 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004991 return err;
4992}
4993
Chris Masond352ac62008-09-29 15:18:18 -04004994/* helper for btfs_get_extent. Given an existing extent in the tree,
4995 * and an extent that you want to insert, deal with overlap and insert
4996 * the new extent into the tree.
4997 */
Chris Mason3b951512008-04-17 11:29:12 -04004998static int merge_extent_mapping(struct extent_map_tree *em_tree,
4999 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005000 struct extent_map *em,
5001 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04005002{
5003 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04005004
Chris Masone6dcd2d2008-07-17 12:53:50 -04005005 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
5006 start_diff = map_start - em->start;
5007 em->start = map_start;
5008 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04005009 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
5010 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04005011 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04005012 em->block_len -= start_diff;
5013 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005014 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04005015}
5016
Chris Masonc8b97812008-10-29 14:49:59 -04005017static noinline int uncompress_inline(struct btrfs_path *path,
5018 struct inode *inode, struct page *page,
5019 size_t pg_offset, u64 extent_offset,
5020 struct btrfs_file_extent_item *item)
5021{
5022 int ret;
5023 struct extent_buffer *leaf = path->nodes[0];
5024 char *tmp;
5025 size_t max_size;
5026 unsigned long inline_size;
5027 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08005028 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04005029
5030 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08005031 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04005032 max_size = btrfs_file_extent_ram_bytes(leaf, item);
5033 inline_size = btrfs_file_extent_inline_item_len(leaf,
5034 btrfs_item_nr(leaf, path->slots[0]));
5035 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04005036 if (!tmp)
5037 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04005038 ptr = btrfs_file_extent_inline_start(item);
5039
5040 read_extent_buffer(leaf, tmp, ptr, inline_size);
5041
Chris Mason5b050f02008-11-11 09:34:41 -05005042 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08005043 ret = btrfs_decompress(compress_type, tmp, page,
5044 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04005045 if (ret) {
5046 char *kaddr = kmap_atomic(page, KM_USER0);
5047 unsigned long copy_size = min_t(u64,
5048 PAGE_CACHE_SIZE - pg_offset,
5049 max_size - extent_offset);
5050 memset(kaddr + pg_offset, 0, copy_size);
5051 kunmap_atomic(kaddr, KM_USER0);
5052 }
5053 kfree(tmp);
5054 return 0;
5055}
5056
Chris Masond352ac62008-09-29 15:18:18 -04005057/*
5058 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05005059 * the ugly parts come from merging extents from the disk with the in-ram
5060 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04005061 * where the in-ram extents might be locked pending data=ordered completion.
5062 *
5063 * This also copies inline extents directly into the page.
5064 */
Chris Masond3977122009-01-05 21:25:51 -05005065
Chris Masona52d9a82007-08-27 16:49:44 -04005066struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05005067 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04005068 int create)
5069{
5070 int ret;
5071 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04005072 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04005073 u64 extent_start = 0;
5074 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08005075 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04005076 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04005077 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04005078 struct btrfs_root *root = BTRFS_I(inode)->root;
5079 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04005080 struct extent_buffer *leaf;
5081 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04005082 struct extent_map *em = NULL;
5083 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05005084 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04005085 struct btrfs_trans_handle *trans = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08005086 int compress_type;
Chris Masona52d9a82007-08-27 16:49:44 -04005087
Chris Masona52d9a82007-08-27 16:49:44 -04005088again:
Chris Mason890871b2009-09-02 16:24:52 -04005089 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005090 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04005091 if (em)
5092 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04005093 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005094
Chris Masona52d9a82007-08-27 16:49:44 -04005095 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04005096 if (em->start > start || em->start + em->len <= start)
5097 free_extent_map(em);
5098 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05005099 free_extent_map(em);
5100 else
5101 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005102 }
Chris Masond1310b22008-01-24 16:13:08 -05005103 em = alloc_extent_map(GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04005104 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05005105 err = -ENOMEM;
5106 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005107 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005108 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05005109 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05005110 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05005111 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04005112 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04005113
5114 if (!path) {
5115 path = btrfs_alloc_path();
5116 BUG_ON(!path);
5117 }
5118
Chris Mason179e29e2007-11-01 11:28:41 -04005119 ret = btrfs_lookup_file_extent(trans, root, path,
5120 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04005121 if (ret < 0) {
5122 err = ret;
5123 goto out;
5124 }
5125
5126 if (ret != 0) {
5127 if (path->slots[0] == 0)
5128 goto not_found;
5129 path->slots[0]--;
5130 }
5131
Chris Mason5f39d392007-10-15 16:14:19 -04005132 leaf = path->nodes[0];
5133 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04005134 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04005135 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04005136 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5137 found_type = btrfs_key_type(&found_key);
5138 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04005139 found_type != BTRFS_EXTENT_DATA_KEY) {
5140 goto not_found;
5141 }
5142
Chris Mason5f39d392007-10-15 16:14:19 -04005143 found_type = btrfs_file_extent_type(leaf, item);
5144 extent_start = found_key.offset;
Li Zefan261507a02010-12-17 14:21:50 +08005145 compress_type = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04005146 if (found_type == BTRFS_FILE_EXTENT_REG ||
5147 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04005148 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04005149 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04005150 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
5151 size_t size;
5152 size = btrfs_file_extent_inline_len(leaf, item);
5153 extent_end = (extent_start + size + root->sectorsize - 1) &
5154 ~((u64)root->sectorsize - 1);
5155 }
5156
5157 if (start >= extent_end) {
5158 path->slots[0]++;
5159 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
5160 ret = btrfs_next_leaf(root, path);
5161 if (ret < 0) {
5162 err = ret;
5163 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005164 }
Yan Zheng9036c102008-10-30 14:19:41 -04005165 if (ret > 0)
5166 goto not_found;
5167 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04005168 }
Yan Zheng9036c102008-10-30 14:19:41 -04005169 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5170 if (found_key.objectid != objectid ||
5171 found_key.type != BTRFS_EXTENT_DATA_KEY)
5172 goto not_found;
5173 if (start + len <= found_key.offset)
5174 goto not_found;
5175 em->start = start;
5176 em->len = found_key.offset - start;
5177 goto not_found_em;
5178 }
5179
Yan Zhengd899e052008-10-30 14:25:28 -04005180 if (found_type == BTRFS_FILE_EXTENT_REG ||
5181 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04005182 em->start = extent_start;
5183 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05005184 em->orig_start = extent_start -
5185 btrfs_file_extent_offset(leaf, item);
Chris Masondb945352007-10-15 16:15:53 -04005186 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
5187 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04005188 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04005189 goto insert;
5190 }
Li Zefan261507a02010-12-17 14:21:50 +08005191 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04005192 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08005193 em->compress_type = compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04005194 em->block_start = bytenr;
5195 em->block_len = btrfs_file_extent_disk_num_bytes(leaf,
5196 item);
5197 } else {
5198 bytenr += btrfs_file_extent_offset(leaf, item);
5199 em->block_start = bytenr;
5200 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04005201 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
5202 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04005203 }
Chris Masona52d9a82007-08-27 16:49:44 -04005204 goto insert;
5205 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04005206 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04005207 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04005208 size_t size;
5209 size_t extent_offset;
5210 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04005211
Chris Masona52d9a82007-08-27 16:49:44 -04005212 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04005213 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04005214 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04005215 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04005216 goto out;
5217 }
5218
Yan Zheng9036c102008-10-30 14:19:41 -04005219 size = btrfs_file_extent_inline_len(leaf, item);
5220 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05005221 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04005222 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04005223 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05005224 em->len = (copy_size + root->sectorsize - 1) &
5225 ~((u64)root->sectorsize - 1);
Yan Zhengff5b7ee2008-11-10 07:34:43 -05005226 em->orig_start = EXTENT_MAP_INLINE;
Li Zefan261507a02010-12-17 14:21:50 +08005227 if (compress_type) {
Chris Masonc8b97812008-10-29 14:49:59 -04005228 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08005229 em->compress_type = compress_type;
5230 }
Yan689f9342007-10-29 11:41:07 -04005231 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04005232 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08005233 if (btrfs_file_extent_compression(leaf, item) !=
5234 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04005235 ret = uncompress_inline(path, inode, page,
5236 pg_offset,
5237 extent_offset, item);
5238 BUG_ON(ret);
5239 } else {
5240 map = kmap(page);
5241 read_extent_buffer(leaf, map + pg_offset, ptr,
5242 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04005243 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
5244 memset(map + pg_offset + copy_size, 0,
5245 PAGE_CACHE_SIZE - pg_offset -
5246 copy_size);
5247 }
Chris Masonc8b97812008-10-29 14:49:59 -04005248 kunmap(page);
5249 }
Chris Mason179e29e2007-11-01 11:28:41 -04005250 flush_dcache_page(page);
5251 } else if (create && PageUptodate(page)) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005252 WARN_ON(1);
Chris Mason179e29e2007-11-01 11:28:41 -04005253 if (!trans) {
5254 kunmap(page);
5255 free_extent_map(em);
5256 em = NULL;
5257 btrfs_release_path(root, path);
Chris Masonf9295742008-07-17 12:54:14 -04005258 trans = btrfs_join_transaction(root, 1);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005259 if (IS_ERR(trans))
5260 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04005261 goto again;
5262 }
Chris Masonc8b97812008-10-29 14:49:59 -04005263 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05005264 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04005265 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04005266 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04005267 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04005268 }
Chris Masond1310b22008-01-24 16:13:08 -05005269 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00005270 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04005271 goto insert;
5272 } else {
Chris Masond3977122009-01-05 21:25:51 -05005273 printk(KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04005274 WARN_ON(1);
5275 }
5276not_found:
5277 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05005278 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04005279not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04005280 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04005281 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04005282insert:
5283 btrfs_release_path(root, path);
Chris Masond1310b22008-01-24 16:13:08 -05005284 if (em->start > start || extent_map_end(em) <= start) {
Chris Masond3977122009-01-05 21:25:51 -05005285 printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
5286 "[%llu %llu]\n", (unsigned long long)em->start,
5287 (unsigned long long)em->len,
5288 (unsigned long long)start,
5289 (unsigned long long)len);
Chris Masona52d9a82007-08-27 16:49:44 -04005290 err = -EIO;
5291 goto out;
5292 }
Chris Masond1310b22008-01-24 16:13:08 -05005293
5294 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04005295 write_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04005296 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04005297 /* it is possible that someone inserted the extent into the tree
5298 * while we had the lock dropped. It is also possible that
5299 * an overlapping map exists in the tree
5300 */
Chris Masona52d9a82007-08-27 16:49:44 -04005301 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04005302 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005303
5304 ret = 0;
5305
Chris Mason3b951512008-04-17 11:29:12 -04005306 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04005307 if (existing && (existing->start > start ||
5308 existing->start + existing->len <= start)) {
5309 free_extent_map(existing);
5310 existing = NULL;
5311 }
Chris Mason3b951512008-04-17 11:29:12 -04005312 if (!existing) {
5313 existing = lookup_extent_mapping(em_tree, em->start,
5314 em->len);
5315 if (existing) {
5316 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005317 em, start,
5318 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04005319 free_extent_map(existing);
5320 if (err) {
5321 free_extent_map(em);
5322 em = NULL;
5323 }
5324 } else {
5325 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04005326 free_extent_map(em);
5327 em = NULL;
5328 }
5329 } else {
5330 free_extent_map(em);
5331 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005332 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04005333 }
Chris Masona52d9a82007-08-27 16:49:44 -04005334 }
Chris Mason890871b2009-09-02 16:24:52 -04005335 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04005336out:
liubo1abe9b82011-03-24 11:18:59 +00005337
5338 trace_btrfs_get_extent(root, em);
5339
Chris Masonf4219502008-07-22 11:18:09 -04005340 if (path)
5341 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04005342 if (trans) {
5343 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05005344 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04005345 err = ret;
5346 }
Chris Masona52d9a82007-08-27 16:49:44 -04005347 if (err) {
5348 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04005349 return ERR_PTR(err);
5350 }
5351 return em;
5352}
5353
Chris Masonec29ed52011-02-23 16:23:20 -05005354struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
5355 size_t pg_offset, u64 start, u64 len,
5356 int create)
5357{
5358 struct extent_map *em;
5359 struct extent_map *hole_em = NULL;
5360 u64 range_start = start;
5361 u64 end;
5362 u64 found;
5363 u64 found_end;
5364 int err = 0;
5365
5366 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
5367 if (IS_ERR(em))
5368 return em;
5369 if (em) {
5370 /*
5371 * if our em maps to a hole, there might
5372 * actually be delalloc bytes behind it
5373 */
5374 if (em->block_start != EXTENT_MAP_HOLE)
5375 return em;
5376 else
5377 hole_em = em;
5378 }
5379
5380 /* check to see if we've wrapped (len == -1 or similar) */
5381 end = start + len;
5382 if (end < start)
5383 end = (u64)-1;
5384 else
5385 end -= 1;
5386
5387 em = NULL;
5388
5389 /* ok, we didn't find anything, lets look for delalloc */
5390 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
5391 end, len, EXTENT_DELALLOC, 1);
5392 found_end = range_start + found;
5393 if (found_end < range_start)
5394 found_end = (u64)-1;
5395
5396 /*
5397 * we didn't find anything useful, return
5398 * the original results from get_extent()
5399 */
5400 if (range_start > end || found_end <= start) {
5401 em = hole_em;
5402 hole_em = NULL;
5403 goto out;
5404 }
5405
5406 /* adjust the range_start to make sure it doesn't
5407 * go backwards from the start they passed in
5408 */
5409 range_start = max(start,range_start);
5410 found = found_end - range_start;
5411
5412 if (found > 0) {
5413 u64 hole_start = start;
5414 u64 hole_len = len;
5415
5416 em = alloc_extent_map(GFP_NOFS);
5417 if (!em) {
5418 err = -ENOMEM;
5419 goto out;
5420 }
5421 /*
5422 * when btrfs_get_extent can't find anything it
5423 * returns one huge hole
5424 *
5425 * make sure what it found really fits our range, and
5426 * adjust to make sure it is based on the start from
5427 * the caller
5428 */
5429 if (hole_em) {
5430 u64 calc_end = extent_map_end(hole_em);
5431
5432 if (calc_end <= start || (hole_em->start > end)) {
5433 free_extent_map(hole_em);
5434 hole_em = NULL;
5435 } else {
5436 hole_start = max(hole_em->start, start);
5437 hole_len = calc_end - hole_start;
5438 }
5439 }
5440 em->bdev = NULL;
5441 if (hole_em && range_start > hole_start) {
5442 /* our hole starts before our delalloc, so we
5443 * have to return just the parts of the hole
5444 * that go until the delalloc starts
5445 */
5446 em->len = min(hole_len,
5447 range_start - hole_start);
5448 em->start = hole_start;
5449 em->orig_start = hole_start;
5450 /*
5451 * don't adjust block start at all,
5452 * it is fixed at EXTENT_MAP_HOLE
5453 */
5454 em->block_start = hole_em->block_start;
5455 em->block_len = hole_len;
5456 } else {
5457 em->start = range_start;
5458 em->len = found;
5459 em->orig_start = range_start;
5460 em->block_start = EXTENT_MAP_DELALLOC;
5461 em->block_len = found;
5462 }
5463 } else if (hole_em) {
5464 return hole_em;
5465 }
5466out:
5467
5468 free_extent_map(hole_em);
5469 if (err) {
5470 free_extent_map(em);
5471 return ERR_PTR(err);
5472 }
5473 return em;
5474}
5475
Josef Bacik4b46fce2010-05-23 11:00:55 -04005476static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
Josef Bacik16d299a2011-04-06 14:53:07 -04005477 struct extent_map *em,
Josef Bacik4b46fce2010-05-23 11:00:55 -04005478 u64 start, u64 len)
5479{
5480 struct btrfs_root *root = BTRFS_I(inode)->root;
5481 struct btrfs_trans_handle *trans;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005482 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
5483 struct btrfs_key ins;
5484 u64 alloc_hint;
5485 int ret;
Josef Bacik16d299a2011-04-06 14:53:07 -04005486 bool insert = false;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005487
Josef Bacik16d299a2011-04-06 14:53:07 -04005488 /*
5489 * Ok if the extent map we looked up is a hole and is for the exact
5490 * range we want, there is no reason to allocate a new one, however if
5491 * it is not right then we need to free this one and drop the cache for
5492 * our range.
5493 */
5494 if (em->block_start != EXTENT_MAP_HOLE || em->start != start ||
5495 em->len != len) {
5496 free_extent_map(em);
5497 em = NULL;
5498 insert = true;
5499 btrfs_drop_extent_cache(inode, start, start + len - 1, 0);
5500 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04005501
5502 trans = btrfs_join_transaction(root, 0);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005503 if (IS_ERR(trans))
5504 return ERR_CAST(trans);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005505
5506 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
5507
5508 alloc_hint = get_extent_allocation_hint(inode, start, len);
5509 ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0,
5510 alloc_hint, (u64)-1, &ins, 1);
5511 if (ret) {
5512 em = ERR_PTR(ret);
5513 goto out;
5514 }
5515
Josef Bacik4b46fce2010-05-23 11:00:55 -04005516 if (!em) {
Josef Bacik16d299a2011-04-06 14:53:07 -04005517 em = alloc_extent_map(GFP_NOFS);
5518 if (!em) {
5519 em = ERR_PTR(-ENOMEM);
5520 goto out;
5521 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04005522 }
5523
5524 em->start = start;
5525 em->orig_start = em->start;
5526 em->len = ins.offset;
5527
5528 em->block_start = ins.objectid;
5529 em->block_len = ins.offset;
5530 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik16d299a2011-04-06 14:53:07 -04005531
5532 /*
5533 * We need to do this because if we're using the original em we searched
5534 * for, we could have EXTENT_FLAG_VACANCY set, and we don't want that.
5535 */
5536 em->flags = 0;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005537 set_bit(EXTENT_FLAG_PINNED, &em->flags);
5538
Josef Bacik16d299a2011-04-06 14:53:07 -04005539 while (insert) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04005540 write_lock(&em_tree->lock);
5541 ret = add_extent_mapping(em_tree, em);
5542 write_unlock(&em_tree->lock);
5543 if (ret != -EEXIST)
5544 break;
5545 btrfs_drop_extent_cache(inode, start, start + em->len - 1, 0);
5546 }
5547
5548 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
5549 ins.offset, ins.offset, 0);
5550 if (ret) {
5551 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
5552 em = ERR_PTR(ret);
5553 }
5554out:
5555 btrfs_end_transaction(trans, root);
5556 return em;
5557}
5558
Chris Mason46bfbb52010-05-26 11:04:10 -04005559/*
5560 * returns 1 when the nocow is safe, < 1 on error, 0 if the
5561 * block must be cow'd
5562 */
5563static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans,
5564 struct inode *inode, u64 offset, u64 len)
5565{
5566 struct btrfs_path *path;
5567 int ret;
5568 struct extent_buffer *leaf;
5569 struct btrfs_root *root = BTRFS_I(inode)->root;
5570 struct btrfs_file_extent_item *fi;
5571 struct btrfs_key key;
5572 u64 disk_bytenr;
5573 u64 backref_offset;
5574 u64 extent_end;
5575 u64 num_bytes;
5576 int slot;
5577 int found_type;
5578
5579 path = btrfs_alloc_path();
5580 if (!path)
5581 return -ENOMEM;
5582
Li Zefan33345d012011-04-20 10:31:50 +08005583 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04005584 offset, 0);
5585 if (ret < 0)
5586 goto out;
5587
5588 slot = path->slots[0];
5589 if (ret == 1) {
5590 if (slot == 0) {
5591 /* can't find the item, must cow */
5592 ret = 0;
5593 goto out;
5594 }
5595 slot--;
5596 }
5597 ret = 0;
5598 leaf = path->nodes[0];
5599 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08005600 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04005601 key.type != BTRFS_EXTENT_DATA_KEY) {
5602 /* not our file or wrong item type, must cow */
5603 goto out;
5604 }
5605
5606 if (key.offset > offset) {
5607 /* Wrong offset, must cow */
5608 goto out;
5609 }
5610
5611 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
5612 found_type = btrfs_file_extent_type(leaf, fi);
5613 if (found_type != BTRFS_FILE_EXTENT_REG &&
5614 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
5615 /* not a regular extent, must cow */
5616 goto out;
5617 }
5618 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
5619 backref_offset = btrfs_file_extent_offset(leaf, fi);
5620
5621 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
5622 if (extent_end < offset + len) {
5623 /* extent doesn't include our full range, must cow */
5624 goto out;
5625 }
5626
5627 if (btrfs_extent_readonly(root, disk_bytenr))
5628 goto out;
5629
5630 /*
5631 * look for other files referencing this extent, if we
5632 * find any we must cow
5633 */
Li Zefan33345d012011-04-20 10:31:50 +08005634 if (btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04005635 key.offset - backref_offset, disk_bytenr))
5636 goto out;
5637
5638 /*
5639 * adjust disk_bytenr and num_bytes to cover just the bytes
5640 * in this extent we are about to write. If there
5641 * are any csums in that range we have to cow in order
5642 * to keep the csums correct
5643 */
5644 disk_bytenr += backref_offset;
5645 disk_bytenr += offset - key.offset;
5646 num_bytes = min(offset + len, extent_end) - offset;
5647 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
5648 goto out;
5649 /*
5650 * all of the above have passed, it is safe to overwrite this extent
5651 * without cow
5652 */
5653 ret = 1;
5654out:
5655 btrfs_free_path(path);
5656 return ret;
5657}
5658
Josef Bacik4b46fce2010-05-23 11:00:55 -04005659static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
5660 struct buffer_head *bh_result, int create)
5661{
5662 struct extent_map *em;
5663 struct btrfs_root *root = BTRFS_I(inode)->root;
5664 u64 start = iblock << inode->i_blkbits;
5665 u64 len = bh_result->b_size;
Chris Mason46bfbb52010-05-26 11:04:10 -04005666 struct btrfs_trans_handle *trans;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005667
5668 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
5669 if (IS_ERR(em))
5670 return PTR_ERR(em);
5671
5672 /*
5673 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
5674 * io. INLINE is special, and we could probably kludge it in here, but
5675 * it's still buffered so for safety lets just fall back to the generic
5676 * buffered path.
5677 *
5678 * For COMPRESSED we _have_ to read the entire extent in so we can
5679 * decompress it, so there will be buffering required no matter what we
5680 * do, so go ahead and fallback to buffered.
5681 *
5682 * We return -ENOTBLK because thats what makes DIO go ahead and go back
5683 * to buffered IO. Don't blame me, this is the price we pay for using
5684 * the generic code.
5685 */
5686 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
5687 em->block_start == EXTENT_MAP_INLINE) {
5688 free_extent_map(em);
5689 return -ENOTBLK;
5690 }
5691
5692 /* Just a good old fashioned hole, return */
5693 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
5694 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
5695 free_extent_map(em);
5696 /* DIO will do one hole at a time, so just unlock a sector */
5697 unlock_extent(&BTRFS_I(inode)->io_tree, start,
5698 start + root->sectorsize - 1, GFP_NOFS);
5699 return 0;
5700 }
5701
5702 /*
5703 * We don't allocate a new extent in the following cases
5704 *
5705 * 1) The inode is marked as NODATACOW. In this case we'll just use the
5706 * existing extent.
5707 * 2) The extent is marked as PREALLOC. We're good to go here and can
5708 * just use the extent.
5709 *
5710 */
Chris Mason46bfbb52010-05-26 11:04:10 -04005711 if (!create) {
5712 len = em->len - (start - em->start);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005713 goto map;
Chris Mason46bfbb52010-05-26 11:04:10 -04005714 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04005715
5716 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
5717 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
5718 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04005719 int type;
5720 int ret;
Chris Mason46bfbb52010-05-26 11:04:10 -04005721 u64 block_start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005722
5723 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
5724 type = BTRFS_ORDERED_PREALLOC;
5725 else
5726 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04005727 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04005728 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04005729
5730 /*
5731 * we're not going to log anything, but we do need
5732 * to make sure the current transaction stays open
5733 * while we look for nocow cross refs
5734 */
5735 trans = btrfs_join_transaction(root, 0);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005736 if (IS_ERR(trans))
Chris Mason46bfbb52010-05-26 11:04:10 -04005737 goto must_cow;
5738
5739 if (can_nocow_odirect(trans, inode, start, len) == 1) {
5740 ret = btrfs_add_ordered_extent_dio(inode, start,
5741 block_start, len, len, type);
5742 btrfs_end_transaction(trans, root);
5743 if (ret) {
5744 free_extent_map(em);
5745 return ret;
5746 }
5747 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005748 }
Chris Mason46bfbb52010-05-26 11:04:10 -04005749 btrfs_end_transaction(trans, root);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005750 }
Chris Mason46bfbb52010-05-26 11:04:10 -04005751must_cow:
5752 /*
5753 * this will cow the extent, reset the len in case we changed
5754 * it above
5755 */
5756 len = bh_result->b_size;
Josef Bacik16d299a2011-04-06 14:53:07 -04005757 em = btrfs_new_extent_direct(inode, em, start, len);
Chris Mason46bfbb52010-05-26 11:04:10 -04005758 if (IS_ERR(em))
5759 return PTR_ERR(em);
5760 len = min(len, em->len - (start - em->start));
5761unlock:
Chris Mason4845e442010-05-25 20:56:50 -04005762 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, start + len - 1,
5763 EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DIRTY, 1,
5764 0, NULL, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005765map:
5766 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
5767 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04005768 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005769 bh_result->b_bdev = em->bdev;
5770 set_buffer_mapped(bh_result);
5771 if (create && !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
5772 set_buffer_new(bh_result);
5773
5774 free_extent_map(em);
5775
5776 return 0;
5777}
5778
5779struct btrfs_dio_private {
5780 struct inode *inode;
5781 u64 logical_offset;
5782 u64 disk_bytenr;
5783 u64 bytes;
5784 u32 *csums;
5785 void *private;
Miao Xiee65e1532010-11-22 03:04:43 +00005786
5787 /* number of bios pending for this dio */
5788 atomic_t pending_bios;
5789
5790 /* IO errors */
5791 int errors;
5792
5793 struct bio *orig_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005794};
5795
5796static void btrfs_endio_direct_read(struct bio *bio, int err)
5797{
Miao Xiee65e1532010-11-22 03:04:43 +00005798 struct btrfs_dio_private *dip = bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005799 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
5800 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005801 struct inode *inode = dip->inode;
5802 struct btrfs_root *root = BTRFS_I(inode)->root;
5803 u64 start;
5804 u32 *private = dip->csums;
5805
5806 start = dip->logical_offset;
5807 do {
5808 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
5809 struct page *page = bvec->bv_page;
5810 char *kaddr;
5811 u32 csum = ~(u32)0;
5812 unsigned long flags;
5813
5814 local_irq_save(flags);
5815 kaddr = kmap_atomic(page, KM_IRQ0);
5816 csum = btrfs_csum_data(root, kaddr + bvec->bv_offset,
5817 csum, bvec->bv_len);
5818 btrfs_csum_final(csum, (char *)&csum);
5819 kunmap_atomic(kaddr, KM_IRQ0);
5820 local_irq_restore(flags);
5821
5822 flush_dcache_page(bvec->bv_page);
5823 if (csum != *private) {
Li Zefan33345d012011-04-20 10:31:50 +08005824 printk(KERN_ERR "btrfs csum failed ino %llu off"
Josef Bacik4b46fce2010-05-23 11:00:55 -04005825 " %llu csum %u private %u\n",
Li Zefan33345d012011-04-20 10:31:50 +08005826 (unsigned long long)btrfs_ino(inode),
5827 (unsigned long long)start,
Josef Bacik4b46fce2010-05-23 11:00:55 -04005828 csum, *private);
5829 err = -EIO;
5830 }
5831 }
5832
5833 start += bvec->bv_len;
5834 private++;
5835 bvec++;
5836 } while (bvec <= bvec_end);
5837
5838 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
5839 dip->logical_offset + dip->bytes - 1, GFP_NOFS);
5840 bio->bi_private = dip->private;
5841
5842 kfree(dip->csums);
5843 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04005844
5845 /* If we had a csum failure make sure to clear the uptodate flag */
5846 if (err)
5847 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005848 dio_end_io(bio, err);
5849}
5850
5851static void btrfs_endio_direct_write(struct bio *bio, int err)
5852{
5853 struct btrfs_dio_private *dip = bio->bi_private;
5854 struct inode *inode = dip->inode;
5855 struct btrfs_root *root = BTRFS_I(inode)->root;
5856 struct btrfs_trans_handle *trans;
5857 struct btrfs_ordered_extent *ordered = NULL;
5858 struct extent_state *cached_state = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05005859 u64 ordered_offset = dip->logical_offset;
5860 u64 ordered_bytes = dip->bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005861 int ret;
5862
5863 if (err)
5864 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05005865again:
5866 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
5867 &ordered_offset,
5868 ordered_bytes);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005869 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05005870 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005871
5872 BUG_ON(!ordered);
5873
5874 trans = btrfs_join_transaction(root, 1);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005875 if (IS_ERR(trans)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04005876 err = -ENOMEM;
5877 goto out;
5878 }
5879 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
5880
5881 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags)) {
5882 ret = btrfs_ordered_update_i_size(inode, 0, ordered);
5883 if (!ret)
5884 ret = btrfs_update_inode(trans, root, inode);
5885 err = ret;
5886 goto out;
5887 }
5888
5889 lock_extent_bits(&BTRFS_I(inode)->io_tree, ordered->file_offset,
5890 ordered->file_offset + ordered->len - 1, 0,
5891 &cached_state, GFP_NOFS);
5892
5893 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags)) {
5894 ret = btrfs_mark_extent_written(trans, inode,
5895 ordered->file_offset,
5896 ordered->file_offset +
5897 ordered->len);
5898 if (ret) {
5899 err = ret;
5900 goto out_unlock;
5901 }
5902 } else {
5903 ret = insert_reserved_file_extent(trans, inode,
5904 ordered->file_offset,
5905 ordered->start,
5906 ordered->disk_len,
5907 ordered->len,
5908 ordered->len,
5909 0, 0, 0,
5910 BTRFS_FILE_EXTENT_REG);
5911 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
5912 ordered->file_offset, ordered->len);
5913 if (ret) {
5914 err = ret;
5915 WARN_ON(1);
5916 goto out_unlock;
5917 }
5918 }
5919
5920 add_pending_csums(trans, inode, ordered->file_offset, &ordered->list);
Josef Bacik1ef30be2011-04-05 19:25:36 -04005921 ret = btrfs_ordered_update_i_size(inode, 0, ordered);
5922 if (!ret)
5923 btrfs_update_inode(trans, root, inode);
5924 ret = 0;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005925out_unlock:
5926 unlock_extent_cached(&BTRFS_I(inode)->io_tree, ordered->file_offset,
5927 ordered->file_offset + ordered->len - 1,
5928 &cached_state, GFP_NOFS);
5929out:
5930 btrfs_delalloc_release_metadata(inode, ordered->len);
5931 btrfs_end_transaction(trans, root);
Chris Mason163cf092010-11-28 19:56:33 -05005932 ordered_offset = ordered->file_offset + ordered->len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005933 btrfs_put_ordered_extent(ordered);
5934 btrfs_put_ordered_extent(ordered);
Chris Mason163cf092010-11-28 19:56:33 -05005935
5936out_test:
5937 /*
5938 * our bio might span multiple ordered extents. If we haven't
5939 * completed the accounting for the whole dio, go back and try again
5940 */
5941 if (ordered_offset < dip->logical_offset + dip->bytes) {
5942 ordered_bytes = dip->logical_offset + dip->bytes -
5943 ordered_offset;
5944 goto again;
5945 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04005946out_done:
5947 bio->bi_private = dip->private;
5948
5949 kfree(dip->csums);
5950 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04005951
5952 /* If we had an error make sure to clear the uptodate flag */
5953 if (err)
5954 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005955 dio_end_io(bio, err);
5956}
5957
Chris Masoneaf25d92010-05-25 09:48:28 -04005958static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
5959 struct bio *bio, int mirror_num,
5960 unsigned long bio_flags, u64 offset)
5961{
5962 int ret;
5963 struct btrfs_root *root = BTRFS_I(inode)->root;
5964 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
5965 BUG_ON(ret);
5966 return 0;
5967}
5968
Miao Xiee65e1532010-11-22 03:04:43 +00005969static void btrfs_end_dio_bio(struct bio *bio, int err)
5970{
5971 struct btrfs_dio_private *dip = bio->bi_private;
5972
5973 if (err) {
Li Zefan33345d012011-04-20 10:31:50 +08005974 printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu "
Jan Beulich3dd14622010-12-07 14:54:09 +00005975 "sector %#Lx len %u err no %d\n",
Li Zefan33345d012011-04-20 10:31:50 +08005976 (unsigned long long)btrfs_ino(dip->inode), bio->bi_rw,
Jan Beulich3dd14622010-12-07 14:54:09 +00005977 (unsigned long long)bio->bi_sector, bio->bi_size, err);
Miao Xiee65e1532010-11-22 03:04:43 +00005978 dip->errors = 1;
5979
5980 /*
5981 * before atomic variable goto zero, we must make sure
5982 * dip->errors is perceived to be set.
5983 */
5984 smp_mb__before_atomic_dec();
5985 }
5986
5987 /* if there are more bios still pending for this dio, just exit */
5988 if (!atomic_dec_and_test(&dip->pending_bios))
5989 goto out;
5990
5991 if (dip->errors)
5992 bio_io_error(dip->orig_bio);
5993 else {
5994 set_bit(BIO_UPTODATE, &dip->orig_bio->bi_flags);
5995 bio_endio(dip->orig_bio, 0);
5996 }
5997out:
5998 bio_put(bio);
5999}
6000
6001static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
6002 u64 first_sector, gfp_t gfp_flags)
6003{
6004 int nr_vecs = bio_get_nr_vecs(bdev);
6005 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
6006}
6007
6008static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
6009 int rw, u64 file_offset, int skip_sum,
Josef Bacik1ae39932011-04-06 14:41:34 -04006010 u32 *csums, int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00006011{
6012 int write = rw & REQ_WRITE;
6013 struct btrfs_root *root = BTRFS_I(inode)->root;
6014 int ret;
6015
6016 bio_get(bio);
6017 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
6018 if (ret)
6019 goto err;
6020
Josef Bacik1ae39932011-04-06 14:41:34 -04006021 if (skip_sum)
6022 goto map;
6023
6024 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00006025 ret = btrfs_wq_submit_bio(root->fs_info,
6026 inode, rw, bio, 0, 0,
6027 file_offset,
6028 __btrfs_submit_bio_start_direct_io,
6029 __btrfs_submit_bio_done);
6030 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04006031 } else if (write) {
6032 /*
6033 * If we aren't doing async submit, calculate the csum of the
6034 * bio now.
6035 */
6036 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
6037 if (ret)
6038 goto err;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00006039 } else if (!skip_sum) {
6040 ret = btrfs_lookup_bio_sums_dio(root, inode, bio,
Miao Xiee65e1532010-11-22 03:04:43 +00006041 file_offset, csums);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00006042 if (ret)
6043 goto err;
6044 }
Miao Xiee65e1532010-11-22 03:04:43 +00006045
Josef Bacik1ae39932011-04-06 14:41:34 -04006046map:
6047 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006048err:
6049 bio_put(bio);
6050 return ret;
6051}
6052
6053static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
6054 int skip_sum)
6055{
6056 struct inode *inode = dip->inode;
6057 struct btrfs_root *root = BTRFS_I(inode)->root;
6058 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
6059 struct bio *bio;
6060 struct bio *orig_bio = dip->orig_bio;
6061 struct bio_vec *bvec = orig_bio->bi_io_vec;
6062 u64 start_sector = orig_bio->bi_sector;
6063 u64 file_offset = dip->logical_offset;
6064 u64 submit_len = 0;
6065 u64 map_length;
6066 int nr_pages = 0;
6067 u32 *csums = dip->csums;
6068 int ret = 0;
Josef Bacik1ae39932011-04-06 14:41:34 -04006069 int async_submit = 0;
Josef Bacik98bc3142011-03-22 11:00:46 -04006070 int write = rw & REQ_WRITE;
Miao Xiee65e1532010-11-22 03:04:43 +00006071
Miao Xiee65e1532010-11-22 03:04:43 +00006072 map_length = orig_bio->bi_size;
6073 ret = btrfs_map_block(map_tree, READ, start_sector << 9,
6074 &map_length, NULL, 0);
6075 if (ret) {
Josef Bacik64728bb2011-04-25 19:43:52 -04006076 bio_put(orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00006077 return -EIO;
6078 }
6079
Josef Bacik02f57c72011-04-06 14:25:44 -04006080 if (map_length >= orig_bio->bi_size) {
6081 bio = orig_bio;
6082 goto submit;
6083 }
6084
Josef Bacik1ae39932011-04-06 14:41:34 -04006085 async_submit = 1;
Josef Bacik02f57c72011-04-06 14:25:44 -04006086 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
6087 if (!bio)
6088 return -ENOMEM;
6089 bio->bi_private = dip;
6090 bio->bi_end_io = btrfs_end_dio_bio;
6091 atomic_inc(&dip->pending_bios);
6092
Miao Xiee65e1532010-11-22 03:04:43 +00006093 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
6094 if (unlikely(map_length < submit_len + bvec->bv_len ||
6095 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
6096 bvec->bv_offset) < bvec->bv_len)) {
6097 /*
6098 * inc the count before we submit the bio so
6099 * we know the end IO handler won't happen before
6100 * we inc the count. Otherwise, the dip might get freed
6101 * before we're done setting it up
6102 */
6103 atomic_inc(&dip->pending_bios);
6104 ret = __btrfs_submit_dio_bio(bio, inode, rw,
6105 file_offset, skip_sum,
Josef Bacik1ae39932011-04-06 14:41:34 -04006106 csums, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006107 if (ret) {
6108 bio_put(bio);
6109 atomic_dec(&dip->pending_bios);
6110 goto out_err;
6111 }
6112
Josef Bacik98bc3142011-03-22 11:00:46 -04006113 /* Write's use the ordered csums */
6114 if (!write && !skip_sum)
Miao Xiee65e1532010-11-22 03:04:43 +00006115 csums = csums + nr_pages;
6116 start_sector += submit_len >> 9;
6117 file_offset += submit_len;
6118
6119 submit_len = 0;
6120 nr_pages = 0;
6121
6122 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
6123 start_sector, GFP_NOFS);
6124 if (!bio)
6125 goto out_err;
6126 bio->bi_private = dip;
6127 bio->bi_end_io = btrfs_end_dio_bio;
6128
6129 map_length = orig_bio->bi_size;
6130 ret = btrfs_map_block(map_tree, READ, start_sector << 9,
6131 &map_length, NULL, 0);
6132 if (ret) {
6133 bio_put(bio);
6134 goto out_err;
6135 }
6136 } else {
6137 submit_len += bvec->bv_len;
6138 nr_pages ++;
6139 bvec++;
6140 }
6141 }
6142
Josef Bacik02f57c72011-04-06 14:25:44 -04006143submit:
Miao Xiee65e1532010-11-22 03:04:43 +00006144 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacik1ae39932011-04-06 14:41:34 -04006145 csums, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006146 if (!ret)
6147 return 0;
6148
6149 bio_put(bio);
6150out_err:
6151 dip->errors = 1;
6152 /*
6153 * before atomic variable goto zero, we must
6154 * make sure dip->errors is perceived to be set.
6155 */
6156 smp_mb__before_atomic_dec();
6157 if (atomic_dec_and_test(&dip->pending_bios))
6158 bio_io_error(dip->orig_bio);
6159
6160 /* bio_end_io() will handle error, so we needn't return it */
6161 return 0;
6162}
6163
Josef Bacik4b46fce2010-05-23 11:00:55 -04006164static void btrfs_submit_direct(int rw, struct bio *bio, struct inode *inode,
6165 loff_t file_offset)
6166{
6167 struct btrfs_root *root = BTRFS_I(inode)->root;
6168 struct btrfs_dio_private *dip;
6169 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006170 int skip_sum;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02006171 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006172 int ret = 0;
6173
6174 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
6175
6176 dip = kmalloc(sizeof(*dip), GFP_NOFS);
6177 if (!dip) {
6178 ret = -ENOMEM;
6179 goto free_ordered;
6180 }
6181 dip->csums = NULL;
6182
Josef Bacik98bc3142011-03-22 11:00:46 -04006183 /* Write's use the ordered csum stuff, so we don't need dip->csums */
6184 if (!write && !skip_sum) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04006185 dip->csums = kmalloc(sizeof(u32) * bio->bi_vcnt, GFP_NOFS);
6186 if (!dip->csums) {
Daniel J Bluemanb4966b72011-03-09 16:46:42 +00006187 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006188 ret = -ENOMEM;
6189 goto free_ordered;
6190 }
6191 }
6192
6193 dip->private = bio->bi_private;
6194 dip->inode = inode;
6195 dip->logical_offset = file_offset;
6196
Josef Bacik4b46fce2010-05-23 11:00:55 -04006197 dip->bytes = 0;
6198 do {
6199 dip->bytes += bvec->bv_len;
6200 bvec++;
6201 } while (bvec <= (bio->bi_io_vec + bio->bi_vcnt - 1));
6202
Chris Mason46bfbb52010-05-26 11:04:10 -04006203 dip->disk_bytenr = (u64)bio->bi_sector << 9;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006204 bio->bi_private = dip;
Miao Xiee65e1532010-11-22 03:04:43 +00006205 dip->errors = 0;
6206 dip->orig_bio = bio;
6207 atomic_set(&dip->pending_bios, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006208
6209 if (write)
6210 bio->bi_end_io = btrfs_endio_direct_write;
6211 else
6212 bio->bi_end_io = btrfs_endio_direct_read;
6213
Miao Xiee65e1532010-11-22 03:04:43 +00006214 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
6215 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04006216 return;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006217free_ordered:
6218 /*
6219 * If this is a write, we need to clean up the reserved space and kill
6220 * the ordered extent.
6221 */
6222 if (write) {
6223 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05006224 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006225 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
6226 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
6227 btrfs_free_reserved_extent(root, ordered->start,
6228 ordered->disk_len);
6229 btrfs_put_ordered_extent(ordered);
6230 btrfs_put_ordered_extent(ordered);
6231 }
6232 bio_endio(bio, ret);
6233}
6234
Chris Mason5a5f79b2010-05-26 21:33:37 -04006235static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
6236 const struct iovec *iov, loff_t offset,
6237 unsigned long nr_segs)
6238{
6239 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00006240 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04006241 size_t size;
6242 unsigned long addr;
6243 unsigned blocksize_mask = root->sectorsize - 1;
6244 ssize_t retval = -EINVAL;
6245 loff_t end = offset;
6246
6247 if (offset & blocksize_mask)
6248 goto out;
6249
6250 /* Check the memory alignment. Blocks cannot straddle pages */
6251 for (seg = 0; seg < nr_segs; seg++) {
6252 addr = (unsigned long)iov[seg].iov_base;
6253 size = iov[seg].iov_len;
6254 end += size;
Josef Bacika1b75f72011-04-08 15:51:18 +00006255 if ((addr & blocksize_mask) || (size & blocksize_mask))
Chris Mason5a5f79b2010-05-26 21:33:37 -04006256 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00006257
6258 /* If this is a write we don't need to check anymore */
6259 if (rw & WRITE)
6260 continue;
6261
6262 /*
6263 * Check to make sure we don't have duplicate iov_base's in this
6264 * iovec, if so return EINVAL, otherwise we'll get csum errors
6265 * when reading back.
6266 */
6267 for (i = seg + 1; i < nr_segs; i++) {
6268 if (iov[seg].iov_base == iov[i].iov_base)
6269 goto out;
6270 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04006271 }
6272 retval = 0;
6273out:
6274 return retval;
6275}
Chris Mason16432982008-04-10 10:23:21 -04006276static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
6277 const struct iovec *iov, loff_t offset,
6278 unsigned long nr_segs)
6279{
Josef Bacik4b46fce2010-05-23 11:00:55 -04006280 struct file *file = iocb->ki_filp;
6281 struct inode *inode = file->f_mapping->host;
6282 struct btrfs_ordered_extent *ordered;
Chris Mason4845e442010-05-25 20:56:50 -04006283 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006284 u64 lockstart, lockend;
6285 ssize_t ret;
Chris Mason4845e442010-05-25 20:56:50 -04006286 int writing = rw & WRITE;
6287 int write_bits = 0;
Chris Mason3f7c5792010-05-26 10:59:53 -04006288 size_t count = iov_length(iov, nr_segs);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006289
Chris Mason5a5f79b2010-05-26 21:33:37 -04006290 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
6291 offset, nr_segs)) {
6292 return 0;
6293 }
6294
Josef Bacik4b46fce2010-05-23 11:00:55 -04006295 lockstart = offset;
Chris Mason3f7c5792010-05-26 10:59:53 -04006296 lockend = offset + count - 1;
6297
6298 if (writing) {
6299 ret = btrfs_delalloc_reserve_space(inode, count);
6300 if (ret)
6301 goto out;
6302 }
Chris Mason4845e442010-05-25 20:56:50 -04006303
Josef Bacik4b46fce2010-05-23 11:00:55 -04006304 while (1) {
Chris Mason4845e442010-05-25 20:56:50 -04006305 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6306 0, &cached_state, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006307 /*
6308 * We're concerned with the entire range that we're going to be
6309 * doing DIO to, so we need to make sure theres no ordered
6310 * extents in this range.
6311 */
6312 ordered = btrfs_lookup_ordered_range(inode, lockstart,
6313 lockend - lockstart + 1);
6314 if (!ordered)
6315 break;
Chris Mason4845e442010-05-25 20:56:50 -04006316 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6317 &cached_state, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006318 btrfs_start_ordered_extent(inode, ordered, 1);
6319 btrfs_put_ordered_extent(ordered);
6320 cond_resched();
6321 }
6322
Chris Mason4845e442010-05-25 20:56:50 -04006323 /*
6324 * we don't use btrfs_set_extent_delalloc because we don't want
6325 * the dirty or uptodate bits
6326 */
6327 if (writing) {
6328 write_bits = EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING;
6329 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6330 EXTENT_DELALLOC, 0, NULL, &cached_state,
6331 GFP_NOFS);
6332 if (ret) {
6333 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6334 lockend, EXTENT_LOCKED | write_bits,
6335 1, 0, &cached_state, GFP_NOFS);
6336 goto out;
6337 }
6338 }
6339
6340 free_extent_state(cached_state);
6341 cached_state = NULL;
6342
Chris Mason5a5f79b2010-05-26 21:33:37 -04006343 ret = __blockdev_direct_IO(rw, iocb, inode,
6344 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
6345 iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
6346 btrfs_submit_direct, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006347
6348 if (ret < 0 && ret != -EIOCBQUEUED) {
Chris Mason4845e442010-05-25 20:56:50 -04006349 clear_extent_bit(&BTRFS_I(inode)->io_tree, offset,
6350 offset + iov_length(iov, nr_segs) - 1,
6351 EXTENT_LOCKED | write_bits, 1, 0,
6352 &cached_state, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006353 } else if (ret >= 0 && ret < iov_length(iov, nr_segs)) {
6354 /*
6355 * We're falling back to buffered, unlock the section we didn't
6356 * do IO on.
6357 */
Chris Mason4845e442010-05-25 20:56:50 -04006358 clear_extent_bit(&BTRFS_I(inode)->io_tree, offset + ret,
6359 offset + iov_length(iov, nr_segs) - 1,
6360 EXTENT_LOCKED | write_bits, 1, 0,
6361 &cached_state, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006362 }
Chris Mason4845e442010-05-25 20:56:50 -04006363out:
6364 free_extent_state(cached_state);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006365 return ret;
Chris Mason16432982008-04-10 10:23:21 -04006366}
6367
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05006368static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
6369 __u64 start, __u64 len)
6370{
Chris Masonec29ed52011-02-23 16:23:20 -05006371 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05006372}
6373
Chris Mason9ebefb182007-06-15 13:50:00 -04006374int btrfs_readpage(struct file *file, struct page *page)
6375{
Chris Masond1310b22008-01-24 16:13:08 -05006376 struct extent_io_tree *tree;
6377 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006378 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04006379}
Chris Mason1832a6d2007-12-21 16:27:21 -05006380
Chris Mason39279cc2007-06-12 06:35:45 -04006381static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
6382{
Chris Masond1310b22008-01-24 16:13:08 -05006383 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04006384
6385
6386 if (current->flags & PF_MEMALLOC) {
6387 redirty_page_for_writepage(wbc, page);
6388 unlock_page(page);
6389 return 0;
6390 }
Chris Masond1310b22008-01-24 16:13:08 -05006391 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006392 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
6393}
Chris Mason39279cc2007-06-12 06:35:45 -04006394
Chris Masonf4219502008-07-22 11:18:09 -04006395int btrfs_writepages(struct address_space *mapping,
6396 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04006397{
Chris Masond1310b22008-01-24 16:13:08 -05006398 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05006399
Chris Masond1310b22008-01-24 16:13:08 -05006400 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04006401 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
6402}
6403
Chris Mason3ab2fb52007-11-08 10:59:22 -05006404static int
6405btrfs_readpages(struct file *file, struct address_space *mapping,
6406 struct list_head *pages, unsigned nr_pages)
6407{
Chris Masond1310b22008-01-24 16:13:08 -05006408 struct extent_io_tree *tree;
6409 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05006410 return extent_readpages(tree, mapping, pages, nr_pages,
6411 btrfs_get_extent);
6412}
Chris Masone6dcd2d2008-07-17 12:53:50 -04006413static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04006414{
Chris Masond1310b22008-01-24 16:13:08 -05006415 struct extent_io_tree *tree;
6416 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04006417 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006418
Chris Masond1310b22008-01-24 16:13:08 -05006419 tree = &BTRFS_I(page->mapping->host)->io_tree;
6420 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05006421 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006422 if (ret == 1) {
6423 ClearPagePrivate(page);
6424 set_page_private(page, 0);
6425 page_cache_release(page);
6426 }
6427 return ret;
6428}
Chris Mason39279cc2007-06-12 06:35:45 -04006429
Chris Masone6dcd2d2008-07-17 12:53:50 -04006430static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
6431{
Chris Mason98509cfc2008-09-11 15:51:43 -04006432 if (PageWriteback(page) || PageDirty(page))
6433 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05006434 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006435}
6436
Chris Masona52d9a82007-08-27 16:49:44 -04006437static void btrfs_invalidatepage(struct page *page, unsigned long offset)
6438{
Chris Masond1310b22008-01-24 16:13:08 -05006439 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006440 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00006441 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006442 u64 page_start = page_offset(page);
6443 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04006444
Chris Mason8b62b722009-09-02 16:53:46 -04006445
6446 /*
6447 * we have the page locked, so new writeback can't start,
6448 * and the dirty bit won't be cleared while we are here.
6449 *
6450 * Wait for IO on this page so that we can safely clear
6451 * the PagePrivate2 bit and do ordered accounting
6452 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006453 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04006454
Chris Masond1310b22008-01-24 16:13:08 -05006455 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006456 if (offset) {
6457 btrfs_releasepage(page, GFP_NOFS);
6458 return;
6459 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00006460 lock_extent_bits(tree, page_start, page_end, 0, &cached_state,
6461 GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006462 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
6463 page_offset(page));
6464 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04006465 /*
6466 * IO on this page will never be started, so we need
6467 * to account for any ordered extents now
6468 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006469 clear_extent_bit(tree, page_start, page_end,
6470 EXTENT_DIRTY | EXTENT_DELALLOC |
Josef Bacik32c00af2009-10-08 13:34:05 -04006471 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING, 1, 0,
Josef Bacik2ac55d42010-02-03 19:33:23 +00006472 &cached_state, GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04006473 /*
6474 * whoever cleared the private bit is responsible
6475 * for the finish_ordered_io
6476 */
6477 if (TestClearPagePrivate2(page)) {
6478 btrfs_finish_ordered_io(page->mapping->host,
6479 page_start, page_end);
6480 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006481 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +00006482 cached_state = NULL;
6483 lock_extent_bits(tree, page_start, page_end, 0, &cached_state,
6484 GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006485 }
6486 clear_extent_bit(tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04006487 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
Josef Bacik2ac55d42010-02-03 19:33:23 +00006488 EXTENT_DO_ACCOUNTING, 1, 1, &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006489 __btrfs_releasepage(page, GFP_NOFS);
6490
Chris Mason4a096752008-07-21 10:29:44 -04006491 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04006492 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04006493 ClearPagePrivate(page);
6494 set_page_private(page, 0);
6495 page_cache_release(page);
6496 }
Chris Mason39279cc2007-06-12 06:35:45 -04006497}
6498
Chris Mason9ebefb182007-06-15 13:50:00 -04006499/*
6500 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
6501 * called from a page fault handler when a page is first dirtied. Hence we must
6502 * be careful to check for EOF conditions here. We set the page up correctly
6503 * for a written page which means we get ENOSPC checking when writing into
6504 * holes and correct delalloc and unwritten extent mapping on filesystems that
6505 * support these features.
6506 *
6507 * We are not allowed to take the i_mutex here so we have to play games to
6508 * protect against truncate races as the page could now be beyond EOF. Because
6509 * vmtruncate() writes the inode size before removing pages, once we have the
6510 * page lock we can determine safely if the page is beyond EOF. If it is not
6511 * beyond EOF, then the page is guaranteed safe against truncation until we
6512 * unlock the page.
6513 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07006514int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04006515{
Nick Pigginc2ec1752009-03-31 15:23:21 -07006516 struct page *page = vmf->page;
Chris Mason6da6aba2007-12-18 16:15:09 -05006517 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05006518 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006519 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
6520 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00006521 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006522 char *kaddr;
6523 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04006524 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05006525 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04006526 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006527 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04006528
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006529 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Nick Piggin56a76f82009-03-31 15:23:23 -07006530 if (ret) {
6531 if (ret == -ENOMEM)
6532 ret = VM_FAULT_OOM;
6533 else /* -ENOSPC, -EIO, etc */
6534 ret = VM_FAULT_SIGBUS;
Chris Mason1832a6d2007-12-21 16:27:21 -05006535 goto out;
Nick Piggin56a76f82009-03-31 15:23:23 -07006536 }
Chris Mason1832a6d2007-12-21 16:27:21 -05006537
Nick Piggin56a76f82009-03-31 15:23:23 -07006538 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006539again:
Chris Mason9ebefb182007-06-15 13:50:00 -04006540 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04006541 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006542 page_start = page_offset(page);
6543 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04006544
Chris Mason9ebefb182007-06-15 13:50:00 -04006545 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04006546 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04006547 /* page got truncated out from underneath us */
6548 goto out_unlock;
6549 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006550 wait_on_page_writeback(page);
6551
Josef Bacik2ac55d42010-02-03 19:33:23 +00006552 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state,
6553 GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006554 set_page_extent_mapped(page);
6555
Chris Masoneb84ae02008-07-17 13:53:27 -04006556 /*
6557 * we can't set the delalloc bits if there are pending ordered
6558 * extents. Drop our locks and wait for them to finish
6559 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006560 ordered = btrfs_lookup_ordered_extent(inode, page_start);
6561 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00006562 unlock_extent_cached(io_tree, page_start, page_end,
6563 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006564 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04006565 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006566 btrfs_put_ordered_extent(ordered);
6567 goto again;
6568 }
6569
Josef Bacikfbf19082009-10-01 17:10:23 -04006570 /*
6571 * XXX - page_mkwrite gets called every time the page is dirtied, even
6572 * if it was already dirty, so for space accounting reasons we need to
6573 * clear any delalloc bits for the range we are fixing to save. There
6574 * is probably a better way to do this, but for now keep consistent with
6575 * prepare_pages in the normal write path.
6576 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00006577 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04006578 EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00006579 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04006580
Josef Bacik2ac55d42010-02-03 19:33:23 +00006581 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
6582 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006583 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00006584 unlock_extent_cached(io_tree, page_start, page_end,
6585 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006586 ret = VM_FAULT_SIGBUS;
6587 goto out_unlock;
6588 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006589 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04006590
6591 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04006592 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04006593 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04006594 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04006595 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04006596
Chris Masone6dcd2d2008-07-17 12:53:50 -04006597 if (zero_start != PAGE_CACHE_SIZE) {
6598 kaddr = kmap(page);
6599 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
6600 flush_dcache_page(page);
6601 kunmap(page);
6602 }
Chris Mason247e7432008-07-17 12:53:51 -04006603 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006604 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04006605 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04006606
Chris Mason257c62e2009-10-13 13:21:08 -04006607 BTRFS_I(inode)->last_trans = root->fs_info->generation;
6608 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
6609
Josef Bacik2ac55d42010-02-03 19:33:23 +00006610 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04006611
6612out_unlock:
Chris Mason50a9b212009-09-11 12:33:12 -04006613 if (!ret)
6614 return VM_FAULT_LOCKED;
Chris Mason9ebefb182007-06-15 13:50:00 -04006615 unlock_page(page);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006616 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason1832a6d2007-12-21 16:27:21 -05006617out:
Chris Mason9ebefb182007-06-15 13:50:00 -04006618 return ret;
6619}
6620
Josef Bacika41ad392011-01-31 15:30:16 -05006621static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006622{
6623 struct btrfs_root *root = BTRFS_I(inode)->root;
6624 int ret;
Josef Bacik3893e332011-01-31 16:03:11 -05006625 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006626 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04006627 unsigned long nr;
Chris Masondbe674a2008-07-17 12:54:05 -04006628 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04006629
Josef Bacik5d5e1032009-10-13 16:46:49 -04006630 ret = btrfs_truncate_page(inode->i_mapping, inode->i_size);
6631 if (ret)
Josef Bacika41ad392011-01-31 15:30:16 -05006632 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00006633
Chris Mason4a096752008-07-21 10:29:44 -04006634 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Yan, Zheng80825102009-11-12 09:35:36 +00006635 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04006636
Josef Bacikf0cd8462011-03-04 14:37:08 -05006637 trans = btrfs_start_transaction(root, 5);
6638 if (IS_ERR(trans))
6639 return PTR_ERR(trans);
6640
6641 btrfs_set_trans_block_group(trans, inode);
6642
6643 ret = btrfs_orphan_add(trans, inode);
6644 if (ret) {
6645 btrfs_end_transaction(trans, root);
6646 return ret;
6647 }
6648
6649 nr = trans->blocks_used;
6650 btrfs_end_transaction(trans, root);
6651 btrfs_btree_balance_dirty(root, nr);
6652
6653 /* Now start a transaction for the truncate */
Yan, Zhengd68fc572010-05-16 10:49:58 -04006654 trans = btrfs_start_transaction(root, 0);
Josef Bacik3893e332011-01-31 16:03:11 -05006655 if (IS_ERR(trans))
6656 return PTR_ERR(trans);
Yan, Zheng80825102009-11-12 09:35:36 +00006657 btrfs_set_trans_block_group(trans, inode);
Yan, Zhengd68fc572010-05-16 10:49:58 -04006658 trans->block_rsv = root->orphan_block_rsv;
Chris Mason5a3f23d2009-03-31 13:27:11 -04006659
6660 /*
6661 * setattr is responsible for setting the ordered_data_close flag,
6662 * but that is only tested during the last file release. That
6663 * could happen well after the next commit, leaving a great big
6664 * window where new writes may get lost if someone chooses to write
6665 * to this file after truncating to zero
6666 *
6667 * The inode doesn't have any dirty data here, and so if we commit
6668 * this is a noop. If someone immediately starts writing to the inode
6669 * it is very likely we'll catch some of their writes in this
6670 * transaction, and the commit will find this file on the ordered
6671 * data list with good things to send down.
6672 *
6673 * This is a best effort solution, there is still a window where
6674 * using truncate to replace the contents of the file will
6675 * end up with a zero length file after a crash.
6676 */
6677 if (inode->i_size == 0 && BTRFS_I(inode)->ordered_data_close)
6678 btrfs_add_ordered_operation(trans, root, inode);
6679
Yan, Zheng80825102009-11-12 09:35:36 +00006680 while (1) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04006681 if (!trans) {
6682 trans = btrfs_start_transaction(root, 0);
Josef Bacik3893e332011-01-31 16:03:11 -05006683 if (IS_ERR(trans))
6684 return PTR_ERR(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04006685 btrfs_set_trans_block_group(trans, inode);
6686 trans->block_rsv = root->orphan_block_rsv;
6687 }
6688
6689 ret = btrfs_block_rsv_check(trans, root,
6690 root->orphan_block_rsv, 0, 5);
Josef Bacik3893e332011-01-31 16:03:11 -05006691 if (ret == -EAGAIN) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04006692 ret = btrfs_commit_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05006693 if (ret)
6694 return ret;
Yan, Zhengd68fc572010-05-16 10:49:58 -04006695 trans = NULL;
6696 continue;
Josef Bacik3893e332011-01-31 16:03:11 -05006697 } else if (ret) {
6698 err = ret;
6699 break;
Yan, Zhengd68fc572010-05-16 10:49:58 -04006700 }
6701
Yan, Zheng80825102009-11-12 09:35:36 +00006702 ret = btrfs_truncate_inode_items(trans, root, inode,
6703 inode->i_size,
6704 BTRFS_EXTENT_DATA_KEY);
Josef Bacik3893e332011-01-31 16:03:11 -05006705 if (ret != -EAGAIN) {
6706 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00006707 break;
Josef Bacik3893e332011-01-31 16:03:11 -05006708 }
Chris Mason39279cc2007-06-12 06:35:45 -04006709
Yan, Zheng80825102009-11-12 09:35:36 +00006710 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05006711 if (ret) {
6712 err = ret;
6713 break;
6714 }
Chris Mason5f39d392007-10-15 16:14:19 -04006715
Yan, Zheng80825102009-11-12 09:35:36 +00006716 nr = trans->blocks_used;
6717 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04006718 trans = NULL;
Yan, Zheng80825102009-11-12 09:35:36 +00006719 btrfs_btree_balance_dirty(root, nr);
Yan, Zheng80825102009-11-12 09:35:36 +00006720 }
6721
6722 if (ret == 0 && inode->i_nlink > 0) {
6723 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05006724 if (ret)
6725 err = ret;
Josef Bacikded5db92011-03-04 14:09:46 -05006726 } else if (ret && inode->i_nlink > 0) {
6727 /*
6728 * Failed to do the truncate, remove us from the in memory
6729 * orphan list.
6730 */
6731 ret = btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00006732 }
6733
6734 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05006735 if (ret && !err)
6736 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04006737
Josef Bacik7b128762008-07-24 12:17:14 -04006738 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04006739 ret = btrfs_end_transaction_throttle(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05006740 if (ret && !err)
6741 err = ret;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04006742 btrfs_btree_balance_dirty(root, nr);
Josef Bacika41ad392011-01-31 15:30:16 -05006743
Josef Bacik3893e332011-01-31 16:03:11 -05006744 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04006745}
6746
Sven Wegener3b963622008-06-09 21:57:42 -04006747/*
Chris Masond352ac62008-09-29 15:18:18 -04006748 * create a new subvolume directory/inode (helper for the ioctl).
6749 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05006750int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Yan, Zheng76dda932009-09-21 16:00:26 -04006751 struct btrfs_root *new_root,
Yan Zhengd2fb3432008-12-11 16:30:39 -05006752 u64 new_dirid, u64 alloc_hint)
Chris Mason39279cc2007-06-12 06:35:45 -04006753{
Chris Mason39279cc2007-06-12 06:35:45 -04006754 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04006755 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006756 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006757
Josef Bacikaec74772008-07-24 12:12:38 -04006758 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
Yan Zhengd2fb3432008-12-11 16:30:39 -05006759 new_dirid, alloc_hint, S_IFDIR | 0700, &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04006760 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04006761 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006762 inode->i_op = &btrfs_dir_inode_operations;
6763 inode->i_fop = &btrfs_dir_file_operations;
6764
Chris Mason39279cc2007-06-12 06:35:45 -04006765 inode->i_nlink = 1;
Chris Masondbe674a2008-07-17 12:54:05 -04006766 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04006767
Yan, Zheng76dda932009-09-21 16:00:26 -04006768 err = btrfs_update_inode(trans, new_root, inode);
6769 BUG_ON(err);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04006770
Yan, Zheng76dda932009-09-21 16:00:26 -04006771 iput(inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04006772 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006773}
6774
Chris Masond352ac62008-09-29 15:18:18 -04006775/* helper function for file defrag and space balancing. This
6776 * forces readahead on a given range of bytes in an inode
6777 */
Chris Masonedbd8d42007-12-21 16:27:24 -05006778unsigned long btrfs_force_ra(struct address_space *mapping,
Chris Mason86479a02007-09-10 19:58:16 -04006779 struct file_ra_state *ra, struct file *file,
6780 pgoff_t offset, pgoff_t last_index)
6781{
Chris Mason8e7bf942008-04-28 09:02:36 -04006782 pgoff_t req_size = last_index - offset + 1;
Chris Mason86479a02007-09-10 19:58:16 -04006783
Chris Mason86479a02007-09-10 19:58:16 -04006784 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
6785 return offset + req_size;
Chris Mason86479a02007-09-10 19:58:16 -04006786}
6787
Chris Mason39279cc2007-06-12 06:35:45 -04006788struct inode *btrfs_alloc_inode(struct super_block *sb)
6789{
6790 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006791 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006792
6793 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
6794 if (!ei)
6795 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006796
6797 ei->root = NULL;
6798 ei->space_info = NULL;
6799 ei->generation = 0;
6800 ei->sequence = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04006801 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04006802 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04006803 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006804 ei->delalloc_bytes = 0;
6805 ei->reserved_bytes = 0;
6806 ei->disk_i_size = 0;
6807 ei->flags = 0;
6808 ei->index_cnt = (u64)-1;
6809 ei->last_unlink_trans = 0;
6810
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006811 atomic_set(&ei->outstanding_extents, 0);
Josef Bacik57a45ced2011-01-25 16:30:38 -05006812 atomic_set(&ei->reserved_extents, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006813
6814 ei->ordered_data_close = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04006815 ei->orphan_meta_reserved = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006816 ei->dummy_inode = 0;
Li Zefan261507a02010-12-17 14:21:50 +08006817 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006818
6819 inode = &ei->vfs_inode;
6820 extent_map_tree_init(&ei->extent_tree, GFP_NOFS);
6821 extent_io_tree_init(&ei->io_tree, &inode->i_data, GFP_NOFS);
6822 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data, GFP_NOFS);
6823 mutex_init(&ei->log_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006824 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Josef Bacik7b128762008-07-24 12:17:14 -04006825 INIT_LIST_HEAD(&ei->i_orphan);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006826 INIT_LIST_HEAD(&ei->delalloc_inodes);
Chris Mason5a3f23d2009-03-31 13:27:11 -04006827 INIT_LIST_HEAD(&ei->ordered_operations);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006828 RB_CLEAR_NODE(&ei->rb_node);
6829
6830 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006831}
6832
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11006833static void btrfs_i_callback(struct rcu_head *head)
6834{
6835 struct inode *inode = container_of(head, struct inode, i_rcu);
6836 INIT_LIST_HEAD(&inode->i_dentry);
6837 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
6838}
6839
Chris Mason39279cc2007-06-12 06:35:45 -04006840void btrfs_destroy_inode(struct inode *inode)
6841{
Chris Masone6dcd2d2008-07-17 12:53:50 -04006842 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04006843 struct btrfs_root *root = BTRFS_I(inode)->root;
6844
Chris Mason39279cc2007-06-12 06:35:45 -04006845 WARN_ON(!list_empty(&inode->i_dentry));
6846 WARN_ON(inode->i_data.nrpages);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006847 WARN_ON(atomic_read(&BTRFS_I(inode)->outstanding_extents));
Josef Bacik57a45ced2011-01-25 16:30:38 -05006848 WARN_ON(atomic_read(&BTRFS_I(inode)->reserved_extents));
Chris Mason39279cc2007-06-12 06:35:45 -04006849
Chris Mason5a3f23d2009-03-31 13:27:11 -04006850 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05006851 * This can happen where we create an inode, but somebody else also
6852 * created the same inode and we need to destroy the one we already
6853 * created.
6854 */
6855 if (!root)
6856 goto free;
6857
6858 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04006859 * Make sure we're properly removed from the ordered operation
6860 * lists.
6861 */
6862 smp_mb();
6863 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
6864 spin_lock(&root->fs_info->ordered_extent_lock);
6865 list_del_init(&BTRFS_I(inode)->ordered_operations);
6866 spin_unlock(&root->fs_info->ordered_extent_lock);
6867 }
6868
Josef Bacik0af3d002010-06-21 14:48:16 -04006869 if (root == root->fs_info->tree_root) {
6870 struct btrfs_block_group_cache *block_group;
6871
6872 block_group = btrfs_lookup_block_group(root->fs_info,
6873 BTRFS_I(inode)->block_group);
6874 if (block_group && block_group->inode == inode) {
6875 spin_lock(&block_group->lock);
6876 block_group->inode = NULL;
6877 spin_unlock(&block_group->lock);
6878 btrfs_put_block_group(block_group);
6879 } else if (block_group) {
6880 btrfs_put_block_group(block_group);
6881 }
6882 }
6883
Yan, Zhengd68fc572010-05-16 10:49:58 -04006884 spin_lock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04006885 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
Li Zefan33345d012011-04-20 10:31:50 +08006886 printk(KERN_INFO "BTRFS: inode %llu still on the orphan list\n",
6887 (unsigned long long)btrfs_ino(inode));
Yan, Zheng80825102009-11-12 09:35:36 +00006888 list_del_init(&BTRFS_I(inode)->i_orphan);
Josef Bacik7b128762008-07-24 12:17:14 -04006889 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04006890 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04006891
Chris Masond3977122009-01-05 21:25:51 -05006892 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006893 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
6894 if (!ordered)
6895 break;
6896 else {
Chris Masond3977122009-01-05 21:25:51 -05006897 printk(KERN_ERR "btrfs found ordered "
6898 "extent %llu %llu on inode cleanup\n",
6899 (unsigned long long)ordered->file_offset,
6900 (unsigned long long)ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006901 btrfs_remove_ordered_extent(inode, ordered);
6902 btrfs_put_ordered_extent(ordered);
6903 btrfs_put_ordered_extent(ordered);
6904 }
6905 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006906 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04006907 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05006908free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11006909 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04006910}
6911
Al Viro45321ac2010-06-07 13:43:19 -04006912int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04006913{
6914 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04006915
Josef Bacik0af3d002010-06-21 14:48:16 -04006916 if (btrfs_root_refs(&root->root_item) == 0 &&
Li Zefan82d59022011-04-20 10:33:24 +08006917 !is_free_space_inode(root, inode))
Al Viro45321ac2010-06-07 13:43:19 -04006918 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04006919 else
Al Viro45321ac2010-06-07 13:43:19 -04006920 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04006921}
6922
Sven Wegener0ee0fda2008-07-30 16:54:26 -04006923static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04006924{
6925 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
6926
6927 inode_init_once(&ei->vfs_inode);
6928}
6929
6930void btrfs_destroy_cachep(void)
6931{
6932 if (btrfs_inode_cachep)
6933 kmem_cache_destroy(btrfs_inode_cachep);
6934 if (btrfs_trans_handle_cachep)
6935 kmem_cache_destroy(btrfs_trans_handle_cachep);
6936 if (btrfs_transaction_cachep)
6937 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04006938 if (btrfs_path_cachep)
6939 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05006940 if (btrfs_free_space_cachep)
6941 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04006942}
6943
6944int btrfs_init_cachep(void)
6945{
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02006946 btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
6947 sizeof(struct btrfs_inode), 0,
6948 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04006949 if (!btrfs_inode_cachep)
6950 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02006951
6952 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
6953 sizeof(struct btrfs_trans_handle), 0,
6954 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04006955 if (!btrfs_trans_handle_cachep)
6956 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02006957
6958 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
6959 sizeof(struct btrfs_transaction), 0,
6960 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04006961 if (!btrfs_transaction_cachep)
6962 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02006963
6964 btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
6965 sizeof(struct btrfs_path), 0,
6966 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04006967 if (!btrfs_path_cachep)
6968 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02006969
Josef Bacikdc89e982011-01-28 17:05:48 -05006970 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space_cache",
6971 sizeof(struct btrfs_free_space), 0,
6972 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
6973 if (!btrfs_free_space_cachep)
6974 goto fail;
6975
Chris Mason39279cc2007-06-12 06:35:45 -04006976 return 0;
6977fail:
6978 btrfs_destroy_cachep();
6979 return -ENOMEM;
6980}
6981
6982static int btrfs_getattr(struct vfsmount *mnt,
6983 struct dentry *dentry, struct kstat *stat)
6984{
6985 struct inode *inode = dentry->d_inode;
6986 generic_fillattr(inode, stat);
Chris Mason3394e162008-11-17 20:42:26 -05006987 stat->dev = BTRFS_I(inode)->root->anon_super.s_dev;
Chris Masond6667462008-01-03 14:51:00 -05006988 stat->blksize = PAGE_CACHE_SIZE;
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006989 stat->blocks = (inode_get_bytes(inode) +
6990 BTRFS_I(inode)->delalloc_bytes) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04006991 return 0;
6992}
6993
Liu Bo75e7cb72011-03-22 10:12:20 +00006994/*
6995 * If a file is moved, it will inherit the cow and compression flags of the new
6996 * directory.
6997 */
6998static void fixup_inode_flags(struct inode *dir, struct inode *inode)
6999{
7000 struct btrfs_inode *b_dir = BTRFS_I(dir);
7001 struct btrfs_inode *b_inode = BTRFS_I(inode);
7002
7003 if (b_dir->flags & BTRFS_INODE_NODATACOW)
7004 b_inode->flags |= BTRFS_INODE_NODATACOW;
7005 else
7006 b_inode->flags &= ~BTRFS_INODE_NODATACOW;
7007
7008 if (b_dir->flags & BTRFS_INODE_COMPRESS)
7009 b_inode->flags |= BTRFS_INODE_COMPRESS;
7010 else
7011 b_inode->flags &= ~BTRFS_INODE_COMPRESS;
7012}
7013
Chris Masond3977122009-01-05 21:25:51 -05007014static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7015 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04007016{
7017 struct btrfs_trans_handle *trans;
7018 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007019 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04007020 struct inode *new_inode = new_dentry->d_inode;
7021 struct inode *old_inode = old_dentry->d_inode;
7022 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007023 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007024 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04007025 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08007026 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007027
Li Zefan33345d012011-04-20 10:31:50 +08007028 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04007029 return -EPERM;
7030
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007031 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08007032 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05007033 return -EXDEV;
7034
Li Zefan33345d012011-04-20 10:31:50 +08007035 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
7036 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007037 return -ENOTEMPTY;
7038
Chris Mason39279cc2007-06-12 06:35:45 -04007039 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007040 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04007041 return -ENOTEMPTY;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007042 /*
7043 * we're using rename to replace one file with another.
7044 * and the replacement file is large. Start IO on it now so
7045 * we don't add too much work to the end of the transaction
7046 */
Bartlomiej Zolnierkiewicz4baf8c92009-08-07 13:47:08 -04007047 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
Chris Mason5a3f23d2009-03-31 13:27:11 -04007048 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
7049 filemap_flush(old_inode->i_mapping);
7050
Yan, Zheng76dda932009-09-21 16:00:26 -04007051 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08007052 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04007053 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04007054 /*
7055 * We want to reserve the absolute worst case amount of items. So if
7056 * both inodes are subvols and we need to unlink them then that would
7057 * require 4 item modifications, but if they are both normal inodes it
7058 * would require 5 item modifications, so we'll assume their normal
7059 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
7060 * should cover the worst case number of items we'll modify.
7061 */
7062 trans = btrfs_start_transaction(root, 20);
Johann Lombardib44c59a2011-03-31 13:23:47 +00007063 if (IS_ERR(trans)) {
7064 ret = PTR_ERR(trans);
7065 goto out_notrans;
7066 }
Chris Mason5f39d392007-10-15 16:14:19 -04007067
Yan, Zhenga5719522009-09-24 09:17:31 -04007068 btrfs_set_trans_block_group(trans, new_dir);
Chris Mason39279cc2007-06-12 06:35:45 -04007069
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007070 if (dest != root)
7071 btrfs_record_root_in_trans(trans, dest);
7072
Yan, Zhenga5719522009-09-24 09:17:31 -04007073 ret = btrfs_set_inode_index(new_dir, &index);
7074 if (ret)
7075 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007076
Li Zefan33345d012011-04-20 10:31:50 +08007077 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007078 /* force full log commit if subvolume involved. */
7079 root->fs_info->last_trans_log_full_commit = trans->transid;
7080 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04007081 ret = btrfs_insert_inode_ref(trans, dest,
7082 new_dentry->d_name.name,
7083 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08007084 old_ino,
7085 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04007086 if (ret)
7087 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007088 /*
7089 * this is an ugly little race, but the rename is required
7090 * to make sure that if we crash, the inode is either at the
7091 * old name or the new one. pinning the log transaction lets
7092 * us make sure we don't allow a log commit to come in after
7093 * we unlink the name but before we add the new name back in.
7094 */
7095 btrfs_pin_log_trans(root);
7096 }
Chris Mason12fcfd22009-03-24 10:24:20 -04007097 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04007098 * make sure the inode gets flushed if it is replacing
7099 * something.
7100 */
Li Zefan33345d012011-04-20 10:31:50 +08007101 if (new_inode && new_inode->i_size && S_ISREG(old_inode->i_mode))
Chris Mason5a3f23d2009-03-31 13:27:11 -04007102 btrfs_add_ordered_operation(trans, root, old_inode);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007103
Chris Mason39279cc2007-06-12 06:35:45 -04007104 old_dir->i_ctime = old_dir->i_mtime = ctime;
7105 new_dir->i_ctime = new_dir->i_mtime = ctime;
7106 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04007107
Chris Mason12fcfd22009-03-24 10:24:20 -04007108 if (old_dentry->d_parent != new_dentry->d_parent)
7109 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
7110
Li Zefan33345d012011-04-20 10:31:50 +08007111 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007112 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
7113 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
7114 old_dentry->d_name.name,
7115 old_dentry->d_name.len);
7116 } else {
Al Viro92986792011-03-04 17:14:37 +00007117 ret = __btrfs_unlink_inode(trans, root, old_dir,
7118 old_dentry->d_inode,
7119 old_dentry->d_name.name,
7120 old_dentry->d_name.len);
7121 if (!ret)
7122 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007123 }
7124 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04007125
7126 if (new_inode) {
7127 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08007128 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007129 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
7130 root_objectid = BTRFS_I(new_inode)->location.objectid;
7131 ret = btrfs_unlink_subvol(trans, dest, new_dir,
7132 root_objectid,
7133 new_dentry->d_name.name,
7134 new_dentry->d_name.len);
7135 BUG_ON(new_inode->i_nlink == 0);
7136 } else {
7137 ret = btrfs_unlink_inode(trans, dest, new_dir,
7138 new_dentry->d_inode,
7139 new_dentry->d_name.name,
7140 new_dentry->d_name.len);
7141 }
7142 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04007143 if (new_inode->i_nlink == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04007144 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007145 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04007146 }
Chris Mason39279cc2007-06-12 06:35:45 -04007147 }
Josef Bacikaec74772008-07-24 12:12:38 -04007148
Liu Bo75e7cb72011-03-22 10:12:20 +00007149 fixup_inode_flags(new_dir, old_inode);
7150
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007151 ret = btrfs_add_link(trans, new_dir, old_inode,
7152 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04007153 new_dentry->d_name.len, 0, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007154 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04007155
Li Zefan33345d012011-04-20 10:31:50 +08007156 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacik6a912212010-11-20 09:48:00 +00007157 struct dentry *parent = dget_parent(new_dentry);
7158 btrfs_log_new_name(trans, old_inode, old_dir, parent);
7159 dput(parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007160 btrfs_end_log_trans(root);
7161 }
Chris Mason39279cc2007-06-12 06:35:45 -04007162out_fail:
Chris Masonab78c842008-07-29 16:15:18 -04007163 btrfs_end_transaction_throttle(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00007164out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08007165 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04007166 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007167
Chris Mason39279cc2007-06-12 06:35:45 -04007168 return ret;
7169}
7170
Chris Masond352ac62008-09-29 15:18:18 -04007171/*
7172 * some fairly slow code that needs optimization. This walks the list
7173 * of all the inodes with pending delalloc and forces them to disk.
7174 */
Yan, Zheng24bbcf042009-11-12 09:36:34 +00007175int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
Chris Masonea8c2812008-08-04 23:17:27 -04007176{
7177 struct list_head *head = &root->fs_info->delalloc_inodes;
7178 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007179 struct inode *inode;
Chris Masonea8c2812008-08-04 23:17:27 -04007180
Yan Zhengc146afa2008-11-12 14:34:12 -05007181 if (root->fs_info->sb->s_flags & MS_RDONLY)
7182 return -EROFS;
7183
Chris Mason75eff682008-12-15 15:54:40 -05007184 spin_lock(&root->fs_info->delalloc_lock);
Chris Masond3977122009-01-05 21:25:51 -05007185 while (!list_empty(head)) {
Chris Masonea8c2812008-08-04 23:17:27 -04007186 binode = list_entry(head->next, struct btrfs_inode,
7187 delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007188 inode = igrab(&binode->vfs_inode);
7189 if (!inode)
7190 list_del_init(&binode->delalloc_inodes);
Chris Mason75eff682008-12-15 15:54:40 -05007191 spin_unlock(&root->fs_info->delalloc_lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007192 if (inode) {
Chris Mason8c8bee12008-09-29 11:19:10 -04007193 filemap_flush(inode->i_mapping);
Yan, Zheng24bbcf042009-11-12 09:36:34 +00007194 if (delay_iput)
7195 btrfs_add_delayed_iput(inode);
7196 else
7197 iput(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007198 }
7199 cond_resched();
Chris Mason75eff682008-12-15 15:54:40 -05007200 spin_lock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04007201 }
Chris Mason75eff682008-12-15 15:54:40 -05007202 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04007203
7204 /* the filemap_flush will queue IO into the worker threads, but
7205 * we have to make sure the IO is actually started and that
7206 * ordered extents get created before we return
7207 */
7208 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05007209 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05007210 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04007211 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05007212 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
7213 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04007214 }
7215 atomic_dec(&root->fs_info->async_submit_draining);
Chris Masonea8c2812008-08-04 23:17:27 -04007216 return 0;
7217}
7218
Josef Bacik0019f102010-10-15 15:18:40 -04007219int btrfs_start_one_delalloc_inode(struct btrfs_root *root, int delay_iput,
7220 int sync)
Yan, Zheng5da9d012010-05-16 10:46:25 -04007221{
7222 struct btrfs_inode *binode;
7223 struct inode *inode = NULL;
7224
7225 spin_lock(&root->fs_info->delalloc_lock);
7226 while (!list_empty(&root->fs_info->delalloc_inodes)) {
7227 binode = list_entry(root->fs_info->delalloc_inodes.next,
7228 struct btrfs_inode, delalloc_inodes);
7229 inode = igrab(&binode->vfs_inode);
7230 if (inode) {
7231 list_move_tail(&binode->delalloc_inodes,
7232 &root->fs_info->delalloc_inodes);
7233 break;
7234 }
7235
7236 list_del_init(&binode->delalloc_inodes);
7237 cond_resched_lock(&root->fs_info->delalloc_lock);
7238 }
7239 spin_unlock(&root->fs_info->delalloc_lock);
7240
7241 if (inode) {
Josef Bacik0019f102010-10-15 15:18:40 -04007242 if (sync) {
7243 filemap_write_and_wait(inode->i_mapping);
7244 /*
7245 * We have to do this because compression doesn't
7246 * actually set PG_writeback until it submits the pages
7247 * for IO, which happens in an async thread, so we could
7248 * race and not actually wait for any writeback pages
7249 * because they've not been submitted yet. Technically
7250 * this could still be the case for the ordered stuff
7251 * since the async thread may not have started to do its
7252 * work yet. If this becomes the case then we need to
7253 * figure out a way to make sure that in writepage we
7254 * wait for any async pages to be submitted before
7255 * returning so that fdatawait does what its supposed to
7256 * do.
7257 */
7258 btrfs_wait_ordered_range(inode, 0, (u64)-1);
7259 } else {
7260 filemap_flush(inode->i_mapping);
7261 }
Yan, Zheng5da9d012010-05-16 10:46:25 -04007262 if (delay_iput)
7263 btrfs_add_delayed_iput(inode);
7264 else
7265 iput(inode);
7266 return 1;
7267 }
7268 return 0;
7269}
7270
Chris Mason39279cc2007-06-12 06:35:45 -04007271static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
7272 const char *symname)
7273{
7274 struct btrfs_trans_handle *trans;
7275 struct btrfs_root *root = BTRFS_I(dir)->root;
7276 struct btrfs_path *path;
7277 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05007278 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04007279 int err;
7280 int drop_inode = 0;
7281 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007282 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04007283 int name_len;
7284 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04007285 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04007286 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04007287 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05007288 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007289
7290 name_len = strlen(symname) + 1;
7291 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
7292 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05007293
Josef Bacik9ed74f22009-09-11 16:12:44 -04007294 /*
7295 * 2 items for inode item and ref
7296 * 2 items for dir items
7297 * 1 item for xattr if selinux is on
7298 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04007299 trans = btrfs_start_transaction(root, 5);
7300 if (IS_ERR(trans))
7301 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05007302
Chris Mason39279cc2007-06-12 06:35:45 -04007303 btrfs_set_trans_block_group(trans, dir);
7304
Li Zefan581bb052011-04-20 10:06:11 +08007305 err = btrfs_find_free_ino(root, &objectid);
7306 if (err)
7307 goto out_unlock;
7308
Josef Bacikaec74772008-07-24 12:12:38 -04007309 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08007310 dentry->d_name.len, btrfs_ino(dir), objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04007311 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO,
7312 &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04007313 if (IS_ERR(inode)) {
7314 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007315 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04007316 }
Chris Mason39279cc2007-06-12 06:35:45 -04007317
Eric Paris2a7dba32011-02-01 11:05:39 -05007318 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04007319 if (err) {
7320 drop_inode = 1;
7321 goto out_unlock;
7322 }
7323
Chris Mason39279cc2007-06-12 06:35:45 -04007324 btrfs_set_trans_block_group(trans, inode);
Josef Bacika1b075d2010-11-19 20:36:11 +00007325 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04007326 if (err)
7327 drop_inode = 1;
7328 else {
7329 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04007330 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04007331 inode->i_fop = &btrfs_file_operations;
7332 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05007333 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04007334 }
Chris Mason39279cc2007-06-12 06:35:45 -04007335 btrfs_update_inode_block_group(trans, inode);
7336 btrfs_update_inode_block_group(trans, dir);
7337 if (drop_inode)
7338 goto out_unlock;
7339
7340 path = btrfs_alloc_path();
7341 BUG_ON(!path);
Li Zefan33345d012011-04-20 10:31:50 +08007342 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007343 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007344 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
7345 datasize = btrfs_file_extent_calc_inline_size(name_len);
7346 err = btrfs_insert_empty_item(trans, root, path, &key,
7347 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04007348 if (err) {
7349 drop_inode = 1;
7350 goto out_unlock;
7351 }
Chris Mason5f39d392007-10-15 16:14:19 -04007352 leaf = path->nodes[0];
7353 ei = btrfs_item_ptr(leaf, path->slots[0],
7354 struct btrfs_file_extent_item);
7355 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
7356 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04007357 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04007358 btrfs_set_file_extent_encryption(leaf, ei, 0);
7359 btrfs_set_file_extent_compression(leaf, ei, 0);
7360 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
7361 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
7362
Chris Mason39279cc2007-06-12 06:35:45 -04007363 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04007364 write_extent_buffer(leaf, symname, ptr, name_len);
7365 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04007366 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04007367
Chris Mason39279cc2007-06-12 06:35:45 -04007368 inode->i_op = &btrfs_symlink_inode_operations;
7369 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04007370 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04007371 inode_set_bytes(inode, name_len);
Chris Masondbe674a2008-07-17 12:54:05 -04007372 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04007373 err = btrfs_update_inode(trans, root, inode);
7374 if (err)
7375 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04007376
7377out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04007378 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04007379 btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04007380 if (drop_inode) {
7381 inode_dec_link_count(inode);
7382 iput(inode);
7383 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04007384 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04007385 return err;
7386}
Chris Mason16432982008-04-10 10:23:21 -04007387
Josef Bacik0af3d002010-06-21 14:48:16 -04007388static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
7389 u64 start, u64 num_bytes, u64 min_size,
7390 loff_t actual_len, u64 *alloc_hint,
7391 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04007392{
Yan Zhengd899e052008-10-30 14:25:28 -04007393 struct btrfs_root *root = BTRFS_I(inode)->root;
7394 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04007395 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00007396 u64 i_size;
Yan Zhengd899e052008-10-30 14:25:28 -04007397 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04007398 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04007399
Josef Bacik0af3d002010-06-21 14:48:16 -04007400 if (trans)
7401 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04007402 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04007403 if (own_trans) {
7404 trans = btrfs_start_transaction(root, 3);
7405 if (IS_ERR(trans)) {
7406 ret = PTR_ERR(trans);
7407 break;
7408 }
Yan Zhengd899e052008-10-30 14:25:28 -04007409 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00007410
Yan, Zhengefa56462010-05-16 10:49:59 -04007411 ret = btrfs_reserve_extent(trans, root, num_bytes, min_size,
7412 0, *alloc_hint, (u64)-1, &ins, 1);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007413 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04007414 if (own_trans)
7415 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04007416 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00007417 }
7418
Yan Zhengd899e052008-10-30 14:25:28 -04007419 ret = insert_reserved_file_extent(trans, inode,
7420 cur_offset, ins.objectid,
7421 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00007422 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04007423 BTRFS_FILE_EXTENT_PREALLOC);
7424 BUG_ON(ret);
Chris Masona1ed8352009-09-11 12:27:37 -04007425 btrfs_drop_extent_cache(inode, cur_offset,
7426 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007427
Yan Zhengd899e052008-10-30 14:25:28 -04007428 num_bytes -= ins.offset;
7429 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04007430 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00007431
Yan Zhengd899e052008-10-30 14:25:28 -04007432 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02007433 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04007434 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04007435 (actual_len > inode->i_size) &&
7436 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00007437 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00007438 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00007439 else
Josef Bacik55a61d12010-11-22 18:50:32 +00007440 i_size = cur_offset;
7441 i_size_write(inode, i_size);
7442 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007443 }
7444
Yan Zhengd899e052008-10-30 14:25:28 -04007445 ret = btrfs_update_inode(trans, root, inode);
7446 BUG_ON(ret);
Yan Zhengd899e052008-10-30 14:25:28 -04007447
Josef Bacik0af3d002010-06-21 14:48:16 -04007448 if (own_trans)
7449 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007450 }
Yan Zhengd899e052008-10-30 14:25:28 -04007451 return ret;
7452}
7453
Josef Bacik0af3d002010-06-21 14:48:16 -04007454int btrfs_prealloc_file_range(struct inode *inode, int mode,
7455 u64 start, u64 num_bytes, u64 min_size,
7456 loff_t actual_len, u64 *alloc_hint)
7457{
7458 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
7459 min_size, actual_len, alloc_hint,
7460 NULL);
7461}
7462
7463int btrfs_prealloc_file_range_trans(struct inode *inode,
7464 struct btrfs_trans_handle *trans, int mode,
7465 u64 start, u64 num_bytes, u64 min_size,
7466 loff_t actual_len, u64 *alloc_hint)
7467{
7468 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
7469 min_size, actual_len, alloc_hint, trans);
7470}
7471
Chris Masone6dcd2d2008-07-17 12:53:50 -04007472static int btrfs_set_page_dirty(struct page *page)
7473{
Chris Masone6dcd2d2008-07-17 12:53:50 -04007474 return __set_page_dirty_nobuffers(page);
7475}
7476
Nick Pigginb74c79e2011-01-07 17:49:58 +11007477static int btrfs_permission(struct inode *inode, int mask, unsigned int flags)
Yanfdebe2b2008-01-14 13:26:08 -05007478{
Li Zefanb83cc962010-12-20 16:04:08 +08007479 struct btrfs_root *root = BTRFS_I(inode)->root;
7480
7481 if (btrfs_root_readonly(root) && (mask & MAY_WRITE))
7482 return -EROFS;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02007483 if ((BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) && (mask & MAY_WRITE))
Yanfdebe2b2008-01-14 13:26:08 -05007484 return -EACCES;
Nick Pigginb74c79e2011-01-07 17:49:58 +11007485 return generic_permission(inode, mask, flags, btrfs_check_acl);
Yanfdebe2b2008-01-14 13:26:08 -05007486}
Chris Mason39279cc2007-06-12 06:35:45 -04007487
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007488static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05007489 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04007490 .lookup = btrfs_lookup,
7491 .create = btrfs_create,
7492 .unlink = btrfs_unlink,
7493 .link = btrfs_link,
7494 .mkdir = btrfs_mkdir,
7495 .rmdir = btrfs_rmdir,
7496 .rename = btrfs_rename,
7497 .symlink = btrfs_symlink,
7498 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04007499 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007500 .setxattr = btrfs_setxattr,
7501 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05007502 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007503 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05007504 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04007505};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007506static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007507 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05007508 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04007509};
Yan, Zheng76dda932009-09-21 16:00:26 -04007510
Alexey Dobriyan828c0952009-10-01 15:43:56 -07007511static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007512 .llseek = generic_file_llseek,
7513 .read = generic_read_dir,
David Woodhousecbdf5a22008-08-06 19:42:33 +01007514 .readdir = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04007515 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04007516#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04007517 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04007518#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04007519 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04007520 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04007521};
7522
Chris Masond1310b22008-01-24 16:13:08 -05007523static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04007524 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05007525 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04007526 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04007527 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04007528 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04007529 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Mason1259ab72008-05-12 13:39:03 -04007530 .readpage_io_failed_hook = btrfs_io_failed_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05007531 .set_bit_hook = btrfs_set_bit_hook,
7532 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04007533 .merge_extent_hook = btrfs_merge_extent_hook,
7534 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04007535};
7536
Chris Mason35054392009-01-21 13:11:13 -05007537/*
7538 * btrfs doesn't support the bmap operation because swapfiles
7539 * use bmap to make a mapping of extents in the file. They assume
7540 * these extents won't change over the life of the file and they
7541 * use the bmap result to do IO directly to the drive.
7542 *
7543 * the btrfs bmap call would return logical addresses that aren't
7544 * suitable for IO and they also will change frequently as COW
7545 * operations happen. So, swapfile + btrfs == corruption.
7546 *
7547 * For now we're avoiding this by dropping bmap.
7548 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07007549static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04007550 .readpage = btrfs_readpage,
7551 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04007552 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05007553 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04007554 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04007555 .invalidatepage = btrfs_invalidatepage,
7556 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04007557 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02007558 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04007559};
7560
Alexey Dobriyan7f094102009-09-21 17:01:10 -07007561static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04007562 .readpage = btrfs_readpage,
7563 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04007564 .invalidatepage = btrfs_invalidatepage,
7565 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04007566};
7567
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007568static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007569 .getattr = btrfs_getattr,
7570 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007571 .setxattr = btrfs_setxattr,
7572 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05007573 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007574 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05007575 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007576 .fiemap = btrfs_fiemap,
Chris Mason39279cc2007-06-12 06:35:45 -04007577};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007578static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04007579 .getattr = btrfs_getattr,
7580 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05007581 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007582 .setxattr = btrfs_setxattr,
7583 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04007584 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007585 .removexattr = btrfs_removexattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04007586};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007587static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007588 .readlink = generic_readlink,
7589 .follow_link = page_follow_link_light,
7590 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00007591 .getattr = btrfs_getattr,
Yanfdebe2b2008-01-14 13:26:08 -05007592 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05007593 .setxattr = btrfs_setxattr,
7594 .getxattr = btrfs_getxattr,
7595 .listxattr = btrfs_listxattr,
7596 .removexattr = btrfs_removexattr,
Chris Mason39279cc2007-06-12 06:35:45 -04007597};
Yan, Zheng76dda932009-09-21 16:00:26 -04007598
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04007599const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04007600 .d_delete = btrfs_dentry_delete,
7601};