blob: 6cef48572e9e92c1ceedd3bcb9cf4d071e386a87 [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 Masonf03d9302009-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 Masonf03d9302009-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, Zheng24bbcf02009-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
Miao Xie16cdcec2011-04-22 18:12:22 +08002662 /*
2663 * If root is tree root, it means this inode is used to
2664 * store free space information. And these inodes are updated
2665 * when committing the transaction, so they needn't delaye to
2666 * be updated, or deadlock will occured.
2667 */
Chris Masondcc6d072011-05-22 07:07:01 -04002668 if (!is_free_space_inode(root, inode)) {
Miao Xie16cdcec2011-04-22 18:12:22 +08002669 ret = btrfs_delayed_update_inode(trans, root, inode);
2670 if (!ret)
2671 btrfs_set_inode_last_trans(trans, inode);
2672 return ret;
2673 }
2674
Chris Mason39279cc2007-06-12 06:35:45 -04002675 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08002676 if (!path)
2677 return -ENOMEM;
2678
Chris Masonb9473432009-03-13 11:00:37 -04002679 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08002680 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
2681 1);
Chris Mason39279cc2007-06-12 06:35:45 -04002682 if (ret) {
2683 if (ret > 0)
2684 ret = -ENOENT;
2685 goto failed;
2686 }
2687
Chris Masonb4ce94d2009-02-04 09:25:08 -05002688 btrfs_unlock_up_safe(path, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002689 leaf = path->nodes[0];
2690 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08002691 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04002692
Chris Masone02119d2008-09-05 16:13:11 -04002693 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002694 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002695 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002696 ret = 0;
2697failed:
Chris Mason39279cc2007-06-12 06:35:45 -04002698 btrfs_free_path(path);
2699 return ret;
2700}
2701
Chris Masond352ac62008-09-29 15:18:18 -04002702/*
2703 * unlink helper that gets used here in inode.c and in the tree logging
2704 * recovery code. It remove a link in a directory with a given name, and
2705 * also drops the back refs in the inode to the directory
2706 */
Al Viro92986792011-03-04 17:14:37 +00002707static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2708 struct btrfs_root *root,
2709 struct inode *dir, struct inode *inode,
2710 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04002711{
2712 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04002713 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002714 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002715 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04002716 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04002717 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08002718 u64 ino = btrfs_ino(inode);
2719 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04002720
2721 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04002722 if (!path) {
2723 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00002724 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04002725 }
2726
Chris Masonb9473432009-03-13 11:00:37 -04002727 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08002728 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04002729 name, name_len, -1);
2730 if (IS_ERR(di)) {
2731 ret = PTR_ERR(di);
2732 goto err;
2733 }
2734 if (!di) {
2735 ret = -ENOENT;
2736 goto err;
2737 }
Chris Mason5f39d392007-10-15 16:14:19 -04002738 leaf = path->nodes[0];
2739 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04002740 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04002741 if (ret)
2742 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -04002743 btrfs_release_path(root, path);
2744
Li Zefan33345d012011-04-20 10:31:50 +08002745 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
2746 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04002747 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05002748 printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
Li Zefan33345d012011-04-20 10:31:50 +08002749 "inode %llu parent %llu\n", name_len, name,
2750 (unsigned long long)ino, (unsigned long long)dir_ino);
Josef Bacikaec74772008-07-24 12:12:38 -04002751 goto err;
2752 }
2753
Miao Xie16cdcec2011-04-22 18:12:22 +08002754 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
2755 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04002756 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -04002757
Chris Masone02119d2008-09-05 16:13:11 -04002758 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08002759 inode, dir_ino);
Chris Mason49eb7e42008-09-11 15:53:12 -04002760 BUG_ON(ret != 0 && ret != -ENOENT);
Chris Masone02119d2008-09-05 16:13:11 -04002761
2762 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
2763 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04002764 if (ret == -ENOENT)
2765 ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002766err:
2767 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002768 if (ret)
2769 goto out;
2770
2771 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
2772 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2773 btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04002774out:
Chris Mason39279cc2007-06-12 06:35:45 -04002775 return ret;
2776}
2777
Al Viro92986792011-03-04 17:14:37 +00002778int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2779 struct btrfs_root *root,
2780 struct inode *dir, struct inode *inode,
2781 const char *name, int name_len)
2782{
2783 int ret;
2784 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
2785 if (!ret) {
2786 btrfs_drop_nlink(inode);
2787 ret = btrfs_update_inode(trans, root, inode);
2788 }
2789 return ret;
2790}
2791
2792
Yan, Zhenga22285a2010-05-16 10:48:46 -04002793/* helper to check if there is any shared block in the path */
2794static int check_path_shared(struct btrfs_root *root,
2795 struct btrfs_path *path)
2796{
2797 struct extent_buffer *eb;
2798 int level;
Dan Carpenter0e4dcbef2010-06-01 08:23:11 +00002799 u64 refs = 1;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002800
2801 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
Josef Bacikdedefd72011-01-24 21:43:18 +00002802 int ret;
2803
Yan, Zhenga22285a2010-05-16 10:48:46 -04002804 if (!path->nodes[level])
2805 break;
2806 eb = path->nodes[level];
2807 if (!btrfs_block_can_be_shared(root, eb))
2808 continue;
2809 ret = btrfs_lookup_extent_info(NULL, root, eb->start, eb->len,
2810 &refs, NULL);
2811 if (refs > 1)
2812 return 1;
2813 }
Josef Bacikdedefd72011-01-24 21:43:18 +00002814 return 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002815}
2816
2817/*
2818 * helper to start transaction for unlink and rmdir.
2819 *
2820 * unlink and rmdir are special in btrfs, they do not always free space.
2821 * so in enospc case, we should make sure they will free space before
2822 * allowing them to use the global metadata reservation.
2823 */
2824static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
2825 struct dentry *dentry)
2826{
2827 struct btrfs_trans_handle *trans;
2828 struct btrfs_root *root = BTRFS_I(dir)->root;
2829 struct btrfs_path *path;
2830 struct btrfs_inode_ref *ref;
2831 struct btrfs_dir_item *di;
2832 struct inode *inode = dentry->d_inode;
2833 u64 index;
2834 int check_link = 1;
2835 int err = -ENOSPC;
2836 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08002837 u64 ino = btrfs_ino(inode);
2838 u64 dir_ino = btrfs_ino(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002839
2840 trans = btrfs_start_transaction(root, 10);
2841 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
2842 return trans;
2843
Li Zefan33345d012011-04-20 10:31:50 +08002844 if (ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhenga22285a2010-05-16 10:48:46 -04002845 return ERR_PTR(-ENOSPC);
2846
2847 /* check if there is someone else holds reference */
2848 if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1)
2849 return ERR_PTR(-ENOSPC);
2850
2851 if (atomic_read(&inode->i_count) > 2)
2852 return ERR_PTR(-ENOSPC);
2853
2854 if (xchg(&root->fs_info->enospc_unlink, 1))
2855 return ERR_PTR(-ENOSPC);
2856
2857 path = btrfs_alloc_path();
2858 if (!path) {
2859 root->fs_info->enospc_unlink = 0;
2860 return ERR_PTR(-ENOMEM);
2861 }
2862
2863 trans = btrfs_start_transaction(root, 0);
2864 if (IS_ERR(trans)) {
2865 btrfs_free_path(path);
2866 root->fs_info->enospc_unlink = 0;
2867 return trans;
2868 }
2869
2870 path->skip_locking = 1;
2871 path->search_commit_root = 1;
2872
2873 ret = btrfs_lookup_inode(trans, root, path,
2874 &BTRFS_I(dir)->location, 0);
2875 if (ret < 0) {
2876 err = ret;
2877 goto out;
2878 }
2879 if (ret == 0) {
2880 if (check_path_shared(root, path))
2881 goto out;
2882 } else {
2883 check_link = 0;
2884 }
2885 btrfs_release_path(root, path);
2886
2887 ret = btrfs_lookup_inode(trans, root, path,
2888 &BTRFS_I(inode)->location, 0);
2889 if (ret < 0) {
2890 err = ret;
2891 goto out;
2892 }
2893 if (ret == 0) {
2894 if (check_path_shared(root, path))
2895 goto out;
2896 } else {
2897 check_link = 0;
2898 }
2899 btrfs_release_path(root, path);
2900
2901 if (ret == 0 && S_ISREG(inode->i_mode)) {
2902 ret = btrfs_lookup_file_extent(trans, root, path,
Li Zefan33345d012011-04-20 10:31:50 +08002903 ino, (u64)-1, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002904 if (ret < 0) {
2905 err = ret;
2906 goto out;
2907 }
2908 BUG_ON(ret == 0);
2909 if (check_path_shared(root, path))
2910 goto out;
2911 btrfs_release_path(root, path);
2912 }
2913
2914 if (!check_link) {
2915 err = 0;
2916 goto out;
2917 }
2918
Li Zefan33345d012011-04-20 10:31:50 +08002919 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zhenga22285a2010-05-16 10:48:46 -04002920 dentry->d_name.name, dentry->d_name.len, 0);
2921 if (IS_ERR(di)) {
2922 err = PTR_ERR(di);
2923 goto out;
2924 }
2925 if (di) {
2926 if (check_path_shared(root, path))
2927 goto out;
2928 } else {
2929 err = 0;
2930 goto out;
2931 }
2932 btrfs_release_path(root, path);
2933
2934 ref = btrfs_lookup_inode_ref(trans, root, path,
2935 dentry->d_name.name, dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08002936 ino, dir_ino, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002937 if (IS_ERR(ref)) {
2938 err = PTR_ERR(ref);
2939 goto out;
2940 }
2941 BUG_ON(!ref);
2942 if (check_path_shared(root, path))
2943 goto out;
2944 index = btrfs_inode_ref_index(path->nodes[0], ref);
2945 btrfs_release_path(root, path);
2946
Miao Xie16cdcec2011-04-22 18:12:22 +08002947 /*
2948 * This is a commit root search, if we can lookup inode item and other
2949 * relative items in the commit root, it means the transaction of
2950 * dir/file creation has been committed, and the dir index item that we
2951 * delay to insert has also been inserted into the commit root. So
2952 * we needn't worry about the delayed insertion of the dir index item
2953 * here.
2954 */
Li Zefan33345d012011-04-20 10:31:50 +08002955 di = btrfs_lookup_dir_index_item(trans, root, path, dir_ino, index,
Yan, Zhenga22285a2010-05-16 10:48:46 -04002956 dentry->d_name.name, dentry->d_name.len, 0);
2957 if (IS_ERR(di)) {
2958 err = PTR_ERR(di);
2959 goto out;
2960 }
2961 BUG_ON(ret == -ENOENT);
2962 if (check_path_shared(root, path))
2963 goto out;
2964
2965 err = 0;
2966out:
2967 btrfs_free_path(path);
2968 if (err) {
2969 btrfs_end_transaction(trans, root);
2970 root->fs_info->enospc_unlink = 0;
2971 return ERR_PTR(err);
2972 }
2973
2974 trans->block_rsv = &root->fs_info->global_block_rsv;
2975 return trans;
2976}
2977
2978static void __unlink_end_trans(struct btrfs_trans_handle *trans,
2979 struct btrfs_root *root)
2980{
2981 if (trans->block_rsv == &root->fs_info->global_block_rsv) {
2982 BUG_ON(!root->fs_info->enospc_unlink);
2983 root->fs_info->enospc_unlink = 0;
2984 }
2985 btrfs_end_transaction_throttle(trans, root);
2986}
2987
Chris Mason39279cc2007-06-12 06:35:45 -04002988static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
2989{
Yan, Zhenga22285a2010-05-16 10:48:46 -04002990 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04002991 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04002992 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002993 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -05002994 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002995
Yan, Zhenga22285a2010-05-16 10:48:46 -04002996 trans = __unlink_start_trans(dir, dentry);
2997 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05002998 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04002999
Chris Mason39279cc2007-06-12 06:35:45 -04003000 btrfs_set_trans_block_group(trans, dir);
Chris Mason12fcfd22009-03-24 10:24:20 -04003001
3002 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
3003
Chris Masone02119d2008-09-05 16:13:11 -04003004 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3005 dentry->d_name.name, dentry->d_name.len);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003006 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04003007
Yan, Zhenga22285a2010-05-16 10:48:46 -04003008 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04003009 ret = btrfs_orphan_add(trans, inode);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003010 BUG_ON(ret);
3011 }
Josef Bacik7b128762008-07-24 12:17:14 -04003012
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003013 nr = trans->blocks_used;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003014 __unlink_end_trans(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003015 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003016 return ret;
3017}
3018
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003019int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3020 struct btrfs_root *root,
3021 struct inode *dir, u64 objectid,
3022 const char *name, int name_len)
3023{
3024 struct btrfs_path *path;
3025 struct extent_buffer *leaf;
3026 struct btrfs_dir_item *di;
3027 struct btrfs_key key;
3028 u64 index;
3029 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003030 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003031
3032 path = btrfs_alloc_path();
3033 if (!path)
3034 return -ENOMEM;
3035
Li Zefan33345d012011-04-20 10:31:50 +08003036 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003037 name, name_len, -1);
3038 BUG_ON(!di || IS_ERR(di));
3039
3040 leaf = path->nodes[0];
3041 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3042 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3043 ret = btrfs_delete_one_dir_name(trans, root, path, di);
3044 BUG_ON(ret);
3045 btrfs_release_path(root, path);
3046
3047 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3048 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003049 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003050 if (ret < 0) {
3051 BUG_ON(ret != -ENOENT);
Li Zefan33345d012011-04-20 10:31:50 +08003052 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003053 name, name_len);
3054 BUG_ON(!di || IS_ERR(di));
3055
3056 leaf = path->nodes[0];
3057 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3058 btrfs_release_path(root, path);
3059 index = key.offset;
3060 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003061 btrfs_release_path(root, path);
3062
Miao Xie16cdcec2011-04-22 18:12:22 +08003063 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
3064 BUG_ON(ret);
3065
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003066 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
3067 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
3068 ret = btrfs_update_inode(trans, root, dir);
3069 BUG_ON(ret);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003070
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003071 return 0;
3072}
3073
Chris Mason39279cc2007-06-12 06:35:45 -04003074static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3075{
3076 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003077 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003078 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003079 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -05003080 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003081
Chris Mason3394e162008-11-17 20:42:26 -05003082 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE ||
Li Zefan33345d012011-04-20 10:31:50 +08003083 btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
Yan134d4512007-10-25 15:49:25 -04003084 return -ENOTEMPTY;
3085
Yan, Zhenga22285a2010-05-16 10:48:46 -04003086 trans = __unlink_start_trans(dir, dentry);
3087 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003088 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003089
Chris Mason39279cc2007-06-12 06:35:45 -04003090 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003091
Li Zefan33345d012011-04-20 10:31:50 +08003092 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003093 err = btrfs_unlink_subvol(trans, root, dir,
3094 BTRFS_I(inode)->location.objectid,
3095 dentry->d_name.name,
3096 dentry->d_name.len);
3097 goto out;
3098 }
3099
Josef Bacik7b128762008-07-24 12:17:14 -04003100 err = btrfs_orphan_add(trans, inode);
3101 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003102 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003103
Chris Mason39279cc2007-06-12 06:35:45 -04003104 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04003105 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3106 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05003107 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04003108 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003109out:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003110 nr = trans->blocks_used;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003111 __unlink_end_trans(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003112 btrfs_btree_balance_dirty(root, nr);
Chris Mason39544012007-12-12 14:38:19 -05003113
Chris Mason39279cc2007-06-12 06:35:45 -04003114 return err;
3115}
3116
Chris Masond20f7042008-12-08 16:58:54 -05003117#if 0
Chris Mason39279cc2007-06-12 06:35:45 -04003118/*
Chris Mason323ac952008-10-01 19:05:46 -04003119 * when truncating bytes in a file, it is possible to avoid reading
3120 * the leaves that contain only checksum items. This can be the
3121 * majority of the IO required to delete a large file, but it must
3122 * be done carefully.
3123 *
3124 * The keys in the level just above the leaves are checked to make sure
3125 * the lowest key in a given leaf is a csum key, and starts at an offset
3126 * after the new size.
3127 *
3128 * Then the key for the next leaf is checked to make sure it also has
3129 * a checksum item for the same file. If it does, we know our target leaf
3130 * contains only checksum items, and it can be safely freed without reading
3131 * it.
3132 *
3133 * This is just an optimization targeted at large files. It may do
3134 * nothing. It will return 0 unless things went badly.
3135 */
3136static noinline int drop_csum_leaves(struct btrfs_trans_handle *trans,
3137 struct btrfs_root *root,
3138 struct btrfs_path *path,
3139 struct inode *inode, u64 new_size)
3140{
3141 struct btrfs_key key;
3142 int ret;
3143 int nritems;
3144 struct btrfs_key found_key;
3145 struct btrfs_key other_key;
Yan Zheng5b84e8d2008-10-09 11:46:19 -04003146 struct btrfs_leaf_ref *ref;
3147 u64 leaf_gen;
3148 u64 leaf_start;
Chris Mason323ac952008-10-01 19:05:46 -04003149
3150 path->lowest_level = 1;
3151 key.objectid = inode->i_ino;
3152 key.type = BTRFS_CSUM_ITEM_KEY;
3153 key.offset = new_size;
3154again:
3155 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3156 if (ret < 0)
3157 goto out;
3158
3159 if (path->nodes[1] == NULL) {
3160 ret = 0;
3161 goto out;
3162 }
3163 ret = 0;
3164 btrfs_node_key_to_cpu(path->nodes[1], &found_key, path->slots[1]);
3165 nritems = btrfs_header_nritems(path->nodes[1]);
3166
3167 if (!nritems)
3168 goto out;
3169
3170 if (path->slots[1] >= nritems)
3171 goto next_node;
3172
3173 /* did we find a key greater than anything we want to delete? */
3174 if (found_key.objectid > inode->i_ino ||
3175 (found_key.objectid == inode->i_ino && found_key.type > key.type))
3176 goto out;
3177
3178 /* we check the next key in the node to make sure the leave contains
3179 * only checksum items. This comparison doesn't work if our
3180 * leaf is the last one in the node
3181 */
3182 if (path->slots[1] + 1 >= nritems) {
3183next_node:
3184 /* search forward from the last key in the node, this
3185 * will bring us into the next node in the tree
3186 */
3187 btrfs_node_key_to_cpu(path->nodes[1], &found_key, nritems - 1);
3188
3189 /* unlikely, but we inc below, so check to be safe */
3190 if (found_key.offset == (u64)-1)
3191 goto out;
3192
3193 /* search_forward needs a path with locks held, do the
3194 * search again for the original key. It is possible
3195 * this will race with a balance and return a path that
3196 * we could modify, but this drop is just an optimization
3197 * and is allowed to miss some leaves.
3198 */
3199 btrfs_release_path(root, path);
3200 found_key.offset++;
3201
3202 /* setup a max key for search_forward */
3203 other_key.offset = (u64)-1;
3204 other_key.type = key.type;
3205 other_key.objectid = key.objectid;
3206
3207 path->keep_locks = 1;
3208 ret = btrfs_search_forward(root, &found_key, &other_key,
3209 path, 0, 0);
3210 path->keep_locks = 0;
3211 if (ret || found_key.objectid != key.objectid ||
3212 found_key.type != key.type) {
3213 ret = 0;
3214 goto out;
3215 }
3216
3217 key.offset = found_key.offset;
3218 btrfs_release_path(root, path);
3219 cond_resched();
3220 goto again;
3221 }
3222
3223 /* we know there's one more slot after us in the tree,
3224 * read that key so we can verify it is also a checksum item
3225 */
3226 btrfs_node_key_to_cpu(path->nodes[1], &other_key, path->slots[1] + 1);
3227
3228 if (found_key.objectid < inode->i_ino)
3229 goto next_key;
3230
3231 if (found_key.type != key.type || found_key.offset < new_size)
3232 goto next_key;
3233
3234 /*
3235 * if the key for the next leaf isn't a csum key from this objectid,
3236 * we can't be sure there aren't good items inside this leaf.
3237 * Bail out
3238 */
3239 if (other_key.objectid != inode->i_ino || other_key.type != key.type)
3240 goto out;
3241
Yan Zheng5b84e8d2008-10-09 11:46:19 -04003242 leaf_start = btrfs_node_blockptr(path->nodes[1], path->slots[1]);
3243 leaf_gen = btrfs_node_ptr_generation(path->nodes[1], path->slots[1]);
Chris Mason323ac952008-10-01 19:05:46 -04003244 /*
3245 * it is safe to delete this leaf, it contains only
3246 * csum items from this inode at an offset >= new_size
3247 */
Yan Zheng5b84e8d2008-10-09 11:46:19 -04003248 ret = btrfs_del_leaf(trans, root, path, leaf_start);
Chris Mason323ac952008-10-01 19:05:46 -04003249 BUG_ON(ret);
3250
Yan Zheng5b84e8d2008-10-09 11:46:19 -04003251 if (root->ref_cows && leaf_gen < trans->transid) {
3252 ref = btrfs_alloc_leaf_ref(root, 0);
3253 if (ref) {
3254 ref->root_gen = root->root_key.offset;
3255 ref->bytenr = leaf_start;
3256 ref->owner = 0;
3257 ref->generation = leaf_gen;
3258 ref->nritems = 0;
3259
Chris Masonbd56b302009-02-04 09:27:02 -05003260 btrfs_sort_leaf_ref(ref);
3261
Yan Zheng5b84e8d2008-10-09 11:46:19 -04003262 ret = btrfs_add_leaf_ref(root, ref, 0);
3263 WARN_ON(ret);
3264 btrfs_free_leaf_ref(root, ref);
3265 } else {
3266 WARN_ON(1);
3267 }
3268 }
Chris Mason323ac952008-10-01 19:05:46 -04003269next_key:
3270 btrfs_release_path(root, path);
3271
3272 if (other_key.objectid == inode->i_ino &&
3273 other_key.type == key.type && other_key.offset > key.offset) {
3274 key.offset = other_key.offset;
3275 cond_resched();
3276 goto again;
3277 }
3278 ret = 0;
3279out:
3280 /* fixup any changes we've made to the path */
3281 path->lowest_level = 0;
3282 path->keep_locks = 0;
3283 btrfs_release_path(root, path);
3284 return ret;
3285}
3286
Chris Masond20f7042008-12-08 16:58:54 -05003287#endif
3288
Chris Mason323ac952008-10-01 19:05:46 -04003289/*
Chris Mason39279cc2007-06-12 06:35:45 -04003290 * this can truncate away extent items, csum items and directory items.
3291 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04003292 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04003293 *
3294 * csum items that cross the new i_size are truncated to the new size
3295 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04003296 *
3297 * min_type is the minimum key type to truncate down to. If set to 0, this
3298 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04003299 */
Yan, Zheng80825102009-11-12 09:35:36 +00003300int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
3301 struct btrfs_root *root,
3302 struct inode *inode,
3303 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003304{
Chris Mason39279cc2007-06-12 06:35:45 -04003305 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003306 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003307 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00003308 struct btrfs_key key;
3309 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04003310 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04003311 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003312 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003313 u64 item_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00003314 u64 mask = root->sectorsize - 1;
3315 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04003316 int found_extent;
3317 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003318 int pending_del_nr = 0;
3319 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04003320 int extent_type = -1;
Chris Mason771ed682008-11-06 22:02:51 -05003321 int encoding;
Yan, Zheng80825102009-11-12 09:35:36 +00003322 int ret;
3323 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08003324 u64 ino = btrfs_ino(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003325
3326 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04003327
Josef Bacik0af3d002010-06-21 14:48:16 -04003328 if (root->ref_cows || root == root->fs_info->tree_root)
Zheng Yan5b21f2e2008-09-26 10:05:38 -04003329 btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003330
Miao Xie16cdcec2011-04-22 18:12:22 +08003331 /*
3332 * This function is also used to drop the items in the log tree before
3333 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
3334 * it is used to drop the loged items. So we shouldn't kill the delayed
3335 * items.
3336 */
3337 if (min_type == 0 && root == BTRFS_I(inode)->root)
3338 btrfs_kill_delayed_inode_items(inode);
3339
Chris Mason39279cc2007-06-12 06:35:45 -04003340 path = btrfs_alloc_path();
3341 BUG_ON(!path);
Julia Lawall33c17ad2009-07-22 16:49:01 -04003342 path->reada = -1;
Chris Mason5f39d392007-10-15 16:14:19 -04003343
Li Zefan33345d012011-04-20 10:31:50 +08003344 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04003345 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04003346 key.type = (u8)-1;
3347
Chris Mason85e21ba2008-01-29 15:11:36 -05003348search_again:
Chris Masonb9473432009-03-13 11:00:37 -04003349 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05003350 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00003351 if (ret < 0) {
3352 err = ret;
3353 goto out;
3354 }
Chris Masond3977122009-01-05 21:25:51 -05003355
Chris Mason85e21ba2008-01-29 15:11:36 -05003356 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003357 /* there are no items in the tree for us to truncate, we're
3358 * done
3359 */
Yan, Zheng80825102009-11-12 09:35:36 +00003360 if (path->slots[0] == 0)
3361 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05003362 path->slots[0]--;
3363 }
3364
Chris Masond3977122009-01-05 21:25:51 -05003365 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04003366 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04003367 leaf = path->nodes[0];
3368 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3369 found_type = btrfs_key_type(&found_key);
Chris Mason771ed682008-11-06 22:02:51 -05003370 encoding = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003371
Li Zefan33345d012011-04-20 10:31:50 +08003372 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04003373 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003374
Chris Mason85e21ba2008-01-29 15:11:36 -05003375 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003376 break;
3377
Chris Mason5f39d392007-10-15 16:14:19 -04003378 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04003379 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04003380 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04003381 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04003382 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Mason771ed682008-11-06 22:02:51 -05003383 encoding = btrfs_file_extent_compression(leaf, fi);
3384 encoding |= btrfs_file_extent_encryption(leaf, fi);
3385 encoding |= btrfs_file_extent_other_encoding(leaf, fi);
3386
Chris Mason179e29e2007-11-01 11:28:41 -04003387 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04003388 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04003389 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04003390 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04003391 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04003392 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003393 }
Yan008630c2007-11-07 13:31:09 -05003394 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04003395 }
Yan, Zheng80825102009-11-12 09:35:36 +00003396 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04003397 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00003398 } else {
3399 if (item_end < new_size)
3400 break;
3401 if (found_key.offset >= new_size)
3402 del_item = 1;
3403 else
3404 del_item = 0;
3405 }
Chris Mason39279cc2007-06-12 06:35:45 -04003406 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003407 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04003408 if (found_type != BTRFS_EXTENT_DATA_KEY)
3409 goto delete;
3410
3411 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04003412 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04003413 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason771ed682008-11-06 22:02:51 -05003414 if (!del_item && !encoding) {
Chris Masondb945352007-10-15 16:15:53 -04003415 u64 orig_num_bytes =
3416 btrfs_file_extent_num_bytes(leaf, fi);
Chris Masone02119d2008-09-05 16:13:11 -04003417 extent_num_bytes = new_size -
Chris Mason5f39d392007-10-15 16:14:19 -04003418 found_key.offset + root->sectorsize - 1;
Yanb1632b12008-01-30 11:54:04 -05003419 extent_num_bytes = extent_num_bytes &
3420 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04003421 btrfs_set_file_extent_num_bytes(leaf, fi,
3422 extent_num_bytes);
3423 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05003424 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04003425 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003426 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04003427 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003428 } else {
Chris Masondb945352007-10-15 16:15:53 -04003429 extent_num_bytes =
3430 btrfs_file_extent_disk_num_bytes(leaf,
3431 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003432 extent_offset = found_key.offset -
3433 btrfs_file_extent_offset(leaf, fi);
3434
Chris Mason39279cc2007-06-12 06:35:45 -04003435 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05003436 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003437 if (extent_start != 0) {
3438 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04003439 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003440 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04003441 }
3442 }
Chris Mason90692182008-02-08 13:49:28 -05003443 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04003444 /*
3445 * we can't truncate inline items that have had
3446 * special encodings
3447 */
3448 if (!del_item &&
3449 btrfs_file_extent_compression(leaf, fi) == 0 &&
3450 btrfs_file_extent_encryption(leaf, fi) == 0 &&
3451 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003452 u32 size = new_size - found_key.offset;
3453
3454 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003455 inode_sub_bytes(inode, item_end + 1 -
3456 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04003457 }
3458 size =
3459 btrfs_file_extent_calc_inline_size(size);
Chris Mason90692182008-02-08 13:49:28 -05003460 ret = btrfs_truncate_item(trans, root, path,
Chris Masone02119d2008-09-05 16:13:11 -04003461 size, 1);
Chris Mason90692182008-02-08 13:49:28 -05003462 BUG_ON(ret);
Chris Masone02119d2008-09-05 16:13:11 -04003463 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003464 inode_sub_bytes(inode, item_end + 1 -
3465 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05003466 }
Chris Mason39279cc2007-06-12 06:35:45 -04003467 }
Chris Mason179e29e2007-11-01 11:28:41 -04003468delete:
Chris Mason39279cc2007-06-12 06:35:45 -04003469 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05003470 if (!pending_del_nr) {
3471 /* no pending yet, add ourselves */
3472 pending_del_slot = path->slots[0];
3473 pending_del_nr = 1;
3474 } else if (pending_del_nr &&
3475 path->slots[0] + 1 == pending_del_slot) {
3476 /* hop on the pending chunk */
3477 pending_del_nr++;
3478 pending_del_slot = path->slots[0];
3479 } else {
Chris Masond3977122009-01-05 21:25:51 -05003480 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05003481 }
Chris Mason39279cc2007-06-12 06:35:45 -04003482 } else {
3483 break;
3484 }
Josef Bacik0af3d002010-06-21 14:48:16 -04003485 if (found_extent && (root->ref_cows ||
3486 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04003487 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003488 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003489 extent_num_bytes, 0,
3490 btrfs_header_owner(leaf),
Li Zefan33345d012011-04-20 10:31:50 +08003491 ino, extent_offset);
Chris Mason39279cc2007-06-12 06:35:45 -04003492 BUG_ON(ret);
3493 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003494
Yan, Zheng80825102009-11-12 09:35:36 +00003495 if (found_type == BTRFS_INODE_ITEM_KEY)
3496 break;
3497
3498 if (path->slots[0] == 0 ||
3499 path->slots[0] != pending_del_slot) {
Li Zefan82d59022011-04-20 10:33:24 +08003500 if (root->ref_cows &&
3501 BTRFS_I(inode)->location.objectid !=
3502 BTRFS_FREE_INO_OBJECTID) {
Yan, Zheng80825102009-11-12 09:35:36 +00003503 err = -EAGAIN;
3504 goto out;
3505 }
3506 if (pending_del_nr) {
3507 ret = btrfs_del_items(trans, root, path,
3508 pending_del_slot,
3509 pending_del_nr);
3510 BUG_ON(ret);
3511 pending_del_nr = 0;
3512 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003513 btrfs_release_path(root, path);
3514 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00003515 } else {
3516 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05003517 }
Chris Mason39279cc2007-06-12 06:35:45 -04003518 }
Yan, Zheng80825102009-11-12 09:35:36 +00003519out:
Chris Mason85e21ba2008-01-29 15:11:36 -05003520 if (pending_del_nr) {
3521 ret = btrfs_del_items(trans, root, path, pending_del_slot,
3522 pending_del_nr);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003523 BUG_ON(ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05003524 }
Chris Mason39279cc2007-06-12 06:35:45 -04003525 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00003526 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003527}
3528
Chris Masona52d9a82007-08-27 16:49:44 -04003529/*
3530 * taken from block_truncate_page, but does cow as it zeros out
3531 * any bytes left in the last page in the file.
3532 */
3533static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
3534{
3535 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -04003536 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003537 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3538 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003539 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003540 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04003541 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04003542 pgoff_t index = from >> PAGE_CACHE_SHIFT;
3543 unsigned offset = from & (PAGE_CACHE_SIZE-1);
3544 struct page *page;
3545 int ret = 0;
3546 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003547 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04003548
3549 if ((offset & (blocksize - 1)) == 0)
3550 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003551 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003552 if (ret)
3553 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04003554
3555 ret = -ENOMEM;
Chris Mason211c17f2008-05-15 09:13:45 -04003556again:
Chris Masona52d9a82007-08-27 16:49:44 -04003557 page = grab_cache_page(mapping, index);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003558 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003559 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Masona52d9a82007-08-27 16:49:44 -04003560 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04003561 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003562
3563 page_start = page_offset(page);
3564 page_end = page_start + PAGE_CACHE_SIZE - 1;
3565
Chris Masona52d9a82007-08-27 16:49:44 -04003566 if (!PageUptodate(page)) {
3567 ret = btrfs_readpage(NULL, page);
3568 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04003569 if (page->mapping != mapping) {
3570 unlock_page(page);
3571 page_cache_release(page);
3572 goto again;
3573 }
Chris Masona52d9a82007-08-27 16:49:44 -04003574 if (!PageUptodate(page)) {
3575 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04003576 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04003577 }
3578 }
Chris Mason211c17f2008-05-15 09:13:45 -04003579 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003580
Josef Bacik2ac55d42010-02-03 19:33:23 +00003581 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state,
3582 GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003583 set_page_extent_mapped(page);
3584
3585 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3586 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00003587 unlock_extent_cached(io_tree, page_start, page_end,
3588 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003589 unlock_page(page);
3590 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04003591 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003592 btrfs_put_ordered_extent(ordered);
3593 goto again;
3594 }
3595
Josef Bacik2ac55d42010-02-03 19:33:23 +00003596 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Josef Bacik5d5e1032009-10-13 16:46:49 -04003597 EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003598 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003599
Josef Bacik2ac55d42010-02-03 19:33:23 +00003600 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
3601 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003602 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00003603 unlock_extent_cached(io_tree, page_start, page_end,
3604 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003605 goto out_unlock;
3606 }
3607
Chris Masone6dcd2d2008-07-17 12:53:50 -04003608 ret = 0;
3609 if (offset != PAGE_CACHE_SIZE) {
3610 kaddr = kmap(page);
3611 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
3612 flush_dcache_page(page);
3613 kunmap(page);
3614 }
Chris Mason247e7432008-07-17 12:53:51 -04003615 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003616 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003617 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
3618 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04003619
Chris Mason89642222008-07-24 09:41:53 -04003620out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04003621 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003622 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04003623 unlock_page(page);
3624 page_cache_release(page);
3625out:
3626 return ret;
3627}
3628
Josef Bacik695a0d02011-03-04 15:46:53 -05003629/*
3630 * This function puts in dummy file extents for the area we're creating a hole
3631 * for. So if we are truncating this file to a larger size we need to insert
3632 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
3633 * the range between oldsize and size
3634 */
Josef Bacika41ad392011-01-31 15:30:16 -05003635int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04003636{
3637 struct btrfs_trans_handle *trans;
3638 struct btrfs_root *root = BTRFS_I(inode)->root;
3639 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003640 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003641 struct extent_state *cached_state = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04003642 u64 mask = root->sectorsize - 1;
Josef Bacika41ad392011-01-31 15:30:16 -05003643 u64 hole_start = (oldsize + mask) & ~mask;
Yan Zheng9036c102008-10-30 14:19:41 -04003644 u64 block_end = (size + mask) & ~mask;
3645 u64 last_byte;
3646 u64 cur_offset;
3647 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003648 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04003649
3650 if (size <= hole_start)
3651 return 0;
3652
Yan Zheng9036c102008-10-30 14:19:41 -04003653 while (1) {
3654 struct btrfs_ordered_extent *ordered;
3655 btrfs_wait_ordered_range(inode, hole_start,
3656 block_end - hole_start);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003657 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
3658 &cached_state, GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003659 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
3660 if (!ordered)
3661 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003662 unlock_extent_cached(io_tree, hole_start, block_end - 1,
3663 &cached_state, GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003664 btrfs_put_ordered_extent(ordered);
3665 }
3666
Yan Zheng9036c102008-10-30 14:19:41 -04003667 cur_offset = hole_start;
3668 while (1) {
3669 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
3670 block_end - cur_offset, 0);
3671 BUG_ON(IS_ERR(em) || !em);
3672 last_byte = min(extent_map_end(em), block_end);
3673 last_byte = (last_byte + mask) & ~mask;
Yan, Zheng80825102009-11-12 09:35:36 +00003674 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Chris Mason771ed682008-11-06 22:02:51 -05003675 u64 hint_byte = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04003676 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00003677
Yan, Zhenga22285a2010-05-16 10:48:46 -04003678 trans = btrfs_start_transaction(root, 2);
3679 if (IS_ERR(trans)) {
3680 err = PTR_ERR(trans);
Chris Mason771ed682008-11-06 22:02:51 -05003681 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003682 }
Yan, Zheng80825102009-11-12 09:35:36 +00003683 btrfs_set_trans_block_group(trans, inode);
3684
3685 err = btrfs_drop_extents(trans, inode, cur_offset,
3686 cur_offset + hole_size,
3687 &hint_byte, 1);
Josef Bacik3893e332011-01-31 16:03:11 -05003688 if (err)
3689 break;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003690
Yan Zheng9036c102008-10-30 14:19:41 -04003691 err = btrfs_insert_file_extent(trans, root,
Li Zefan33345d012011-04-20 10:31:50 +08003692 btrfs_ino(inode), cur_offset, 0,
Yan Zheng9036c102008-10-30 14:19:41 -04003693 0, hole_size, 0, hole_size,
3694 0, 0, 0);
Josef Bacik3893e332011-01-31 16:03:11 -05003695 if (err)
3696 break;
Yan, Zheng80825102009-11-12 09:35:36 +00003697
Yan Zheng9036c102008-10-30 14:19:41 -04003698 btrfs_drop_extent_cache(inode, hole_start,
3699 last_byte - 1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003700
3701 btrfs_end_transaction(trans, root);
Yan Zheng9036c102008-10-30 14:19:41 -04003702 }
3703 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003704 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04003705 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00003706 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04003707 break;
3708 }
3709
Yan, Zhenga22285a2010-05-16 10:48:46 -04003710 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003711 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
3712 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003713 return err;
3714}
3715
Josef Bacika41ad392011-01-31 15:30:16 -05003716static int btrfs_setsize(struct inode *inode, loff_t newsize)
Yan, Zheng80825102009-11-12 09:35:36 +00003717{
Josef Bacika41ad392011-01-31 15:30:16 -05003718 loff_t oldsize = i_size_read(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003719 int ret;
3720
Josef Bacika41ad392011-01-31 15:30:16 -05003721 if (newsize == oldsize)
Yan, Zheng80825102009-11-12 09:35:36 +00003722 return 0;
3723
Josef Bacika41ad392011-01-31 15:30:16 -05003724 if (newsize > oldsize) {
3725 i_size_write(inode, newsize);
3726 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
3727 truncate_pagecache(inode, oldsize, newsize);
3728 ret = btrfs_cont_expand(inode, oldsize, newsize);
Yan, Zheng80825102009-11-12 09:35:36 +00003729 if (ret) {
Josef Bacika41ad392011-01-31 15:30:16 -05003730 btrfs_setsize(inode, oldsize);
Yan, Zheng80825102009-11-12 09:35:36 +00003731 return ret;
3732 }
3733
Josef Bacik930f0282011-03-04 14:41:41 -05003734 mark_inode_dirty(inode);
Josef Bacika41ad392011-01-31 15:30:16 -05003735 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00003736
Josef Bacika41ad392011-01-31 15:30:16 -05003737 /*
3738 * We're truncating a file that used to have good data down to
3739 * zero. Make sure it gets into the ordered flush list so that
3740 * any new writes get down to disk quickly.
3741 */
3742 if (newsize == 0)
3743 BTRFS_I(inode)->ordered_data_close = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00003744
Josef Bacika41ad392011-01-31 15:30:16 -05003745 /* we don't support swapfiles, so vmtruncate shouldn't fail */
3746 truncate_setsize(inode, newsize);
3747 ret = btrfs_truncate(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003748 }
3749
Josef Bacika41ad392011-01-31 15:30:16 -05003750 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00003751}
3752
Chris Mason39279cc2007-06-12 06:35:45 -04003753static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
3754{
3755 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08003756 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003757 int err;
3758
Li Zefanb83cc962010-12-20 16:04:08 +08003759 if (btrfs_root_readonly(root))
3760 return -EROFS;
3761
Chris Mason39279cc2007-06-12 06:35:45 -04003762 err = inode_change_ok(inode, attr);
3763 if (err)
3764 return err;
3765
Chris Mason5a3f23d2009-03-31 13:27:11 -04003766 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Josef Bacika41ad392011-01-31 15:30:16 -05003767 err = btrfs_setsize(inode, attr->ia_size);
Yan, Zheng80825102009-11-12 09:35:36 +00003768 if (err)
3769 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003770 }
Yan Zheng9036c102008-10-30 14:19:41 -04003771
Christoph Hellwig10257742010-06-04 11:30:02 +02003772 if (attr->ia_valid) {
3773 setattr_copy(inode, attr);
3774 mark_inode_dirty(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04003775
Christoph Hellwig10257742010-06-04 11:30:02 +02003776 if (attr->ia_valid & ATTR_MODE)
3777 err = btrfs_acl_chmod(inode);
3778 }
3779
Chris Mason39279cc2007-06-12 06:35:45 -04003780 return err;
3781}
Chris Mason61295eb2008-01-14 16:24:38 -05003782
Al Virobd555972010-06-07 11:35:40 -04003783void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003784{
3785 struct btrfs_trans_handle *trans;
3786 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003787 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04003788 int ret;
3789
liubo1abe9b82011-03-24 11:18:59 +00003790 trace_btrfs_inode_evict(inode);
3791
Chris Mason39279cc2007-06-12 06:35:45 -04003792 truncate_inode_pages(&inode->i_data, 0);
Josef Bacik0af3d002010-06-21 14:48:16 -04003793 if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 ||
Li Zefan82d59022011-04-20 10:33:24 +08003794 is_free_space_inode(root, inode)))
Al Virobd555972010-06-07 11:35:40 -04003795 goto no_delete;
3796
Chris Mason39279cc2007-06-12 06:35:45 -04003797 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04003798 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003799 goto no_delete;
3800 }
Al Virobd555972010-06-07 11:35:40 -04003801 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04003802 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04003803
Yan, Zhengc71bf092009-11-12 09:34:40 +00003804 if (root->fs_info->log_root_recovering) {
3805 BUG_ON(!list_empty(&BTRFS_I(inode)->i_orphan));
3806 goto no_delete;
3807 }
3808
Yan, Zheng76dda932009-09-21 16:00:26 -04003809 if (inode->i_nlink > 0) {
3810 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
3811 goto no_delete;
3812 }
3813
Chris Masondbe674a2008-07-17 12:54:05 -04003814 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003815
Yan, Zheng80825102009-11-12 09:35:36 +00003816 while (1) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003817 trans = btrfs_start_transaction(root, 0);
3818 BUG_ON(IS_ERR(trans));
Yan, Zheng80825102009-11-12 09:35:36 +00003819 btrfs_set_trans_block_group(trans, inode);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003820 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00003821
Yan, Zhengd68fc572010-05-16 10:49:58 -04003822 ret = btrfs_block_rsv_check(trans, root,
3823 root->orphan_block_rsv, 0, 5);
3824 if (ret) {
3825 BUG_ON(ret != -EAGAIN);
3826 ret = btrfs_commit_transaction(trans, root);
3827 BUG_ON(ret);
3828 continue;
3829 }
3830
3831 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003832 if (ret != -EAGAIN)
3833 break;
3834
3835 nr = trans->blocks_used;
3836 btrfs_end_transaction(trans, root);
3837 trans = NULL;
3838 btrfs_btree_balance_dirty(root, nr);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003839
Josef Bacik7b128762008-07-24 12:17:14 -04003840 }
3841
Yan, Zheng80825102009-11-12 09:35:36 +00003842 if (ret == 0) {
3843 ret = btrfs_orphan_del(trans, inode);
3844 BUG_ON(ret);
3845 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003846
Li Zefan581bb052011-04-20 10:06:11 +08003847 if (!(root == root->fs_info->tree_root ||
3848 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08003849 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08003850
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003851 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04003852 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003853 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003854no_delete:
Al Virobd555972010-06-07 11:35:40 -04003855 end_writeback(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003856 return;
Chris Mason39279cc2007-06-12 06:35:45 -04003857}
3858
3859/*
3860 * this returns the key found in the dir entry in the location pointer.
3861 * If no dir entries were found, location->objectid is 0.
3862 */
3863static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
3864 struct btrfs_key *location)
3865{
3866 const char *name = dentry->d_name.name;
3867 int namelen = dentry->d_name.len;
3868 struct btrfs_dir_item *di;
3869 struct btrfs_path *path;
3870 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04003871 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003872
3873 path = btrfs_alloc_path();
3874 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -05003875
Li Zefan33345d012011-04-20 10:31:50 +08003876 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04003877 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04003878 if (IS_ERR(di))
3879 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05003880
3881 if (!di || IS_ERR(di))
Chris Mason39544012007-12-12 14:38:19 -05003882 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05003883
Chris Mason5f39d392007-10-15 16:14:19 -04003884 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04003885out:
Chris Mason39279cc2007-06-12 06:35:45 -04003886 btrfs_free_path(path);
3887 return ret;
Chris Mason39544012007-12-12 14:38:19 -05003888out_err:
3889 location->objectid = 0;
3890 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04003891}
3892
3893/*
3894 * when we hit a tree root in a directory, the btrfs part of the inode
3895 * needs to be changed to reflect the root directory of the tree root. This
3896 * is kind of like crossing a mount point.
3897 */
3898static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003899 struct inode *dir,
3900 struct dentry *dentry,
3901 struct btrfs_key *location,
3902 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04003903{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003904 struct btrfs_path *path;
3905 struct btrfs_root *new_root;
3906 struct btrfs_root_ref *ref;
3907 struct extent_buffer *leaf;
3908 int ret;
3909 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003910
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003911 path = btrfs_alloc_path();
3912 if (!path) {
3913 err = -ENOMEM;
3914 goto out;
3915 }
Chris Mason39279cc2007-06-12 06:35:45 -04003916
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003917 err = -ENOENT;
3918 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
3919 BTRFS_I(dir)->root->root_key.objectid,
3920 location->objectid);
3921 if (ret) {
3922 if (ret < 0)
3923 err = ret;
3924 goto out;
3925 }
Chris Mason39279cc2007-06-12 06:35:45 -04003926
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003927 leaf = path->nodes[0];
3928 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08003929 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003930 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
3931 goto out;
3932
3933 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
3934 (unsigned long)(ref + 1),
3935 dentry->d_name.len);
3936 if (ret)
3937 goto out;
3938
3939 btrfs_release_path(root->fs_info->tree_root, path);
3940
3941 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
3942 if (IS_ERR(new_root)) {
3943 err = PTR_ERR(new_root);
3944 goto out;
3945 }
3946
3947 if (btrfs_root_refs(&new_root->root_item) == 0) {
3948 err = -ENOENT;
3949 goto out;
3950 }
3951
3952 *sub_root = new_root;
3953 location->objectid = btrfs_root_dirid(&new_root->root_item);
3954 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04003955 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003956 err = 0;
3957out:
3958 btrfs_free_path(path);
3959 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003960}
3961
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003962static void inode_tree_add(struct inode *inode)
3963{
3964 struct btrfs_root *root = BTRFS_I(inode)->root;
3965 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003966 struct rb_node **p;
3967 struct rb_node *parent;
Li Zefan33345d012011-04-20 10:31:50 +08003968 u64 ino = btrfs_ino(inode);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003969again:
3970 p = &root->inode_tree.rb_node;
3971 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003972
Al Viro1d3382cb2010-10-23 15:19:20 -04003973 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04003974 return;
3975
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003976 spin_lock(&root->inode_lock);
3977 while (*p) {
3978 parent = *p;
3979 entry = rb_entry(parent, struct btrfs_inode, rb_node);
3980
Li Zefan33345d012011-04-20 10:31:50 +08003981 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003982 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08003983 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003984 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003985 else {
3986 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04003987 (I_WILL_FREE | I_FREEING)));
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003988 rb_erase(parent, &root->inode_tree);
3989 RB_CLEAR_NODE(parent);
3990 spin_unlock(&root->inode_lock);
3991 goto again;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003992 }
3993 }
3994 rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
3995 rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
3996 spin_unlock(&root->inode_lock);
3997}
3998
3999static void inode_tree_del(struct inode *inode)
4000{
4001 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04004002 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004003
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004004 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004005 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004006 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004007 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04004008 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004009 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004010 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04004011
Josef Bacik0af3d002010-06-21 14:48:16 -04004012 /*
4013 * Free space cache has inodes in the tree root, but the tree root has a
4014 * root_refs of 0, so this could end up dropping the tree root as a
4015 * snapshot, so we need the extra !root->fs_info->tree_root check to
4016 * make sure we don't drop it.
4017 */
4018 if (empty && btrfs_root_refs(&root->root_item) == 0 &&
4019 root != root->fs_info->tree_root) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004020 synchronize_srcu(&root->fs_info->subvol_srcu);
4021 spin_lock(&root->inode_lock);
4022 empty = RB_EMPTY_ROOT(&root->inode_tree);
4023 spin_unlock(&root->inode_lock);
4024 if (empty)
4025 btrfs_add_dead_root(root);
4026 }
4027}
4028
4029int btrfs_invalidate_inodes(struct btrfs_root *root)
4030{
4031 struct rb_node *node;
4032 struct rb_node *prev;
4033 struct btrfs_inode *entry;
4034 struct inode *inode;
4035 u64 objectid = 0;
4036
4037 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4038
4039 spin_lock(&root->inode_lock);
4040again:
4041 node = root->inode_tree.rb_node;
4042 prev = NULL;
4043 while (node) {
4044 prev = node;
4045 entry = rb_entry(node, struct btrfs_inode, rb_node);
4046
Li Zefan33345d012011-04-20 10:31:50 +08004047 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004048 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004049 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004050 node = node->rb_right;
4051 else
4052 break;
4053 }
4054 if (!node) {
4055 while (prev) {
4056 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004057 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004058 node = prev;
4059 break;
4060 }
4061 prev = rb_next(prev);
4062 }
4063 }
4064 while (node) {
4065 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004066 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04004067 inode = igrab(&entry->vfs_inode);
4068 if (inode) {
4069 spin_unlock(&root->inode_lock);
4070 if (atomic_read(&inode->i_count) > 1)
4071 d_prune_aliases(inode);
4072 /*
Al Viro45321ac2010-06-07 13:43:19 -04004073 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04004074 * the inode cache when its usage count
4075 * hits zero.
4076 */
4077 iput(inode);
4078 cond_resched();
4079 spin_lock(&root->inode_lock);
4080 goto again;
4081 }
4082
4083 if (cond_resched_lock(&root->inode_lock))
4084 goto again;
4085
4086 node = rb_next(node);
4087 }
4088 spin_unlock(&root->inode_lock);
4089 return 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004090}
4091
Chris Masone02119d2008-09-05 16:13:11 -04004092static int btrfs_init_locked_inode(struct inode *inode, void *p)
4093{
4094 struct btrfs_iget_args *args = p;
4095 inode->i_ino = args->ino;
Chris Masone02119d2008-09-05 16:13:11 -04004096 BTRFS_I(inode)->root = args->root;
Josef Bacik6a632092009-02-20 11:00:09 -05004097 btrfs_set_inode_space_info(args->root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004098 return 0;
4099}
4100
4101static int btrfs_find_actor(struct inode *inode, void *opaque)
4102{
4103 struct btrfs_iget_args *args = opaque;
Li Zefan33345d012011-04-20 10:31:50 +08004104 return args->ino == btrfs_ino(inode) &&
Chris Masond3977122009-01-05 21:25:51 -05004105 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004106}
4107
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004108static struct inode *btrfs_iget_locked(struct super_block *s,
4109 u64 objectid,
4110 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04004111{
4112 struct inode *inode;
4113 struct btrfs_iget_args args;
4114 args.ino = objectid;
4115 args.root = root;
4116
4117 inode = iget5_locked(s, objectid, btrfs_find_actor,
4118 btrfs_init_locked_inode,
4119 (void *)&args);
4120 return inode;
4121}
4122
Balaji Rao1a54ef82008-07-21 02:01:04 +05304123/* Get an inode object given its location and corresponding root.
4124 * Returns in *is_new if the inode was read from disk
4125 */
4126struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00004127 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05304128{
4129 struct inode *inode;
4130
4131 inode = btrfs_iget_locked(s, location->objectid, root);
4132 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004133 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05304134
4135 if (inode->i_state & I_NEW) {
4136 BTRFS_I(inode)->root = root;
4137 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
4138 btrfs_read_locked_inode(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004139 inode_tree_add(inode);
Balaji Rao1a54ef82008-07-21 02:01:04 +05304140 unlock_new_inode(inode);
Josef Bacik73f73412009-12-04 17:38:27 +00004141 if (new)
4142 *new = 1;
Balaji Rao1a54ef82008-07-21 02:01:04 +05304143 }
4144
4145 return inode;
4146}
4147
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004148static struct inode *new_simple_dir(struct super_block *s,
4149 struct btrfs_key *key,
4150 struct btrfs_root *root)
4151{
4152 struct inode *inode = new_inode(s);
4153
4154 if (!inode)
4155 return ERR_PTR(-ENOMEM);
4156
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004157 BTRFS_I(inode)->root = root;
4158 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
4159 BTRFS_I(inode)->dummy_inode = 1;
4160
4161 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
4162 inode->i_op = &simple_dir_inode_operations;
4163 inode->i_fop = &simple_dir_operations;
4164 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
4165 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
4166
4167 return inode;
4168}
4169
Chris Mason3de45862008-11-17 21:02:50 -05004170struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04004171{
Chris Masond3977122009-01-05 21:25:51 -05004172 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004173 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004174 struct btrfs_root *sub_root = root;
4175 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04004176 int index;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004177 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004178
4179 if (dentry->d_name.len > BTRFS_NAME_LEN)
4180 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04004181
Chris Mason39279cc2007-06-12 06:35:45 -04004182 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04004183
Chris Mason39279cc2007-06-12 06:35:45 -04004184 if (ret < 0)
4185 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04004186
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004187 if (location.objectid == 0)
4188 return NULL;
4189
4190 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00004191 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004192 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004193 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004194
4195 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
4196
Yan, Zheng76dda932009-09-21 16:00:26 -04004197 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004198 ret = fixup_tree_root_location(root, dir, dentry,
4199 &location, &sub_root);
4200 if (ret < 0) {
4201 if (ret != -ENOENT)
4202 inode = ERR_PTR(ret);
4203 else
4204 inode = new_simple_dir(dir->i_sb, &location, sub_root);
4205 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00004206 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004207 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004208 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
4209
Julia Lawall34d19ba2011-01-24 19:55:19 +00004210 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00004211 down_read(&root->fs_info->cleanup_work_sem);
4212 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004213 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00004214 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004215 if (ret)
4216 inode = ERR_PTR(ret);
Yan, Zhengc71bf092009-11-12 09:34:40 +00004217 }
4218
Chris Mason3de45862008-11-17 21:02:50 -05004219 return inode;
4220}
4221
Nick Pigginfe15ce42011-01-07 17:49:23 +11004222static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04004223{
4224 struct btrfs_root *root;
4225
Yan, Zhengefefb142009-10-09 09:25:16 -04004226 if (!dentry->d_inode && !IS_ROOT(dentry))
4227 dentry = dentry->d_parent;
Yan, Zheng76dda932009-09-21 16:00:26 -04004228
Yan, Zhengefefb142009-10-09 09:25:16 -04004229 if (dentry->d_inode) {
4230 root = BTRFS_I(dentry->d_inode)->root;
4231 if (btrfs_root_refs(&root->root_item) == 0)
4232 return 1;
4233 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004234 return 0;
4235}
4236
Chris Mason3de45862008-11-17 21:02:50 -05004237static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
4238 struct nameidata *nd)
4239{
4240 struct inode *inode;
4241
Chris Mason3de45862008-11-17 21:02:50 -05004242 inode = btrfs_lookup_dentry(dir, dentry);
4243 if (IS_ERR(inode))
4244 return ERR_CAST(inode);
Josef Bacik7b128762008-07-24 12:17:14 -04004245
Chris Mason39279cc2007-06-12 06:35:45 -04004246 return d_splice_alias(inode, dentry);
4247}
4248
Miao Xie16cdcec2011-04-22 18:12:22 +08004249unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04004250 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
4251};
4252
David Woodhousecbdf5a22008-08-06 19:42:33 +01004253static int btrfs_real_readdir(struct file *filp, void *dirent,
4254 filldir_t filldir)
Chris Mason39279cc2007-06-12 06:35:45 -04004255{
Chris Mason6da6aba2007-12-18 16:15:09 -05004256 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004257 struct btrfs_root *root = BTRFS_I(inode)->root;
4258 struct btrfs_item *item;
4259 struct btrfs_dir_item *di;
4260 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04004261 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004262 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08004263 struct list_head ins_list;
4264 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04004265 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04004266 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004267 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04004268 unsigned char d_type;
4269 int over = 0;
4270 u32 di_cur;
4271 u32 di_total;
4272 u32 di_len;
4273 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04004274 char tmp_name[32];
4275 char *name_ptr;
4276 int name_len;
Miao Xie16cdcec2011-04-22 18:12:22 +08004277 int is_curr = 0; /* filp->f_pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04004278
4279 /* FIXME, use a real flag for deciding about the key type */
4280 if (root->fs_info->tree_root == root)
4281 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04004282
Chris Mason39544012007-12-12 14:38:19 -05004283 /* special case for "." */
4284 if (filp->f_pos == 0) {
Li Zefan33345d012011-04-20 10:31:50 +08004285 over = filldir(dirent, ".", 1, 1, btrfs_ino(inode), DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05004286 if (over)
4287 return 0;
4288 filp->f_pos = 1;
4289 }
Chris Mason39544012007-12-12 14:38:19 -05004290 /* special case for .., just use the back ref */
4291 if (filp->f_pos == 1) {
David Woodhouse5ecc7e52008-08-17 15:14:48 +01004292 u64 pino = parent_ino(filp->f_path.dentry);
Chris Mason39544012007-12-12 14:38:19 -05004293 over = filldir(dirent, "..", 2,
David Woodhouse5ecc7e52008-08-17 15:14:48 +01004294 2, pino, DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05004295 if (over)
David Woodhouse49593bf2008-08-17 17:08:36 +01004296 return 0;
Chris Mason39544012007-12-12 14:38:19 -05004297 filp->f_pos = 2;
4298 }
David Woodhouse49593bf2008-08-17 17:08:36 +01004299 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08004300 if (!path)
4301 return -ENOMEM;
David Woodhouse49593bf2008-08-17 17:08:36 +01004302 path->reada = 2;
4303
Miao Xie16cdcec2011-04-22 18:12:22 +08004304 if (key_type == BTRFS_DIR_INDEX_KEY) {
4305 INIT_LIST_HEAD(&ins_list);
4306 INIT_LIST_HEAD(&del_list);
4307 btrfs_get_delayed_items(inode, &ins_list, &del_list);
4308 }
4309
Chris Mason39279cc2007-06-12 06:35:45 -04004310 btrfs_set_key_type(&key, key_type);
4311 key.offset = filp->f_pos;
Li Zefan33345d012011-04-20 10:31:50 +08004312 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004313
Chris Mason39279cc2007-06-12 06:35:45 -04004314 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4315 if (ret < 0)
4316 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01004317
4318 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04004319 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04004320 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08004321 if (slot >= btrfs_header_nritems(leaf)) {
4322 ret = btrfs_next_leaf(root, path);
4323 if (ret < 0)
4324 goto err;
4325 else if (ret > 0)
4326 break;
4327 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04004328 }
Chris Mason3de45862008-11-17 21:02:50 -05004329
Chris Mason5f39d392007-10-15 16:14:19 -04004330 item = btrfs_item_nr(leaf, slot);
4331 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4332
4333 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04004334 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004335 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004336 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004337 if (found_key.offset < filp->f_pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08004338 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08004339 if (key_type == BTRFS_DIR_INDEX_KEY &&
4340 btrfs_should_delete_dir_index(&del_list,
4341 found_key.offset))
4342 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04004343
4344 filp->f_pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08004345 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01004346
Chris Mason39279cc2007-06-12 06:35:45 -04004347 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
4348 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04004349 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01004350
4351 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04004352 struct btrfs_key location;
4353
Josef Bacik22a94d42011-03-16 16:47:17 -04004354 if (verify_dir_item(root, leaf, di))
4355 break;
4356
Chris Mason5f39d392007-10-15 16:14:19 -04004357 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01004358 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04004359 name_ptr = tmp_name;
4360 } else {
4361 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01004362 if (!name_ptr) {
4363 ret = -ENOMEM;
4364 goto err;
4365 }
Chris Mason5f39d392007-10-15 16:14:19 -04004366 }
4367 read_extent_buffer(leaf, name_ptr,
4368 (unsigned long)(di + 1), name_len);
4369
4370 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
4371 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05004372
4373 /* is this a reference to our own snapshot? If so
4374 * skip it
4375 */
4376 if (location.type == BTRFS_ROOT_ITEM_KEY &&
4377 location.objectid == root->root_key.objectid) {
4378 over = 0;
4379 goto skip;
4380 }
Chris Mason5f39d392007-10-15 16:14:19 -04004381 over = filldir(dirent, name_ptr, name_len,
David Woodhouse49593bf2008-08-17 17:08:36 +01004382 found_key.offset, location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04004383 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04004384
Chris Mason3de45862008-11-17 21:02:50 -05004385skip:
Chris Mason5f39d392007-10-15 16:14:19 -04004386 if (name_ptr != tmp_name)
4387 kfree(name_ptr);
4388
Chris Mason39279cc2007-06-12 06:35:45 -04004389 if (over)
4390 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05004391 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01004392 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04004393 di_cur += di_len;
4394 di = (struct btrfs_dir_item *)((char *)di + di_len);
4395 }
Li Zefanb9e03af2011-03-23 10:43:58 +08004396next:
4397 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04004398 }
David Woodhouse49593bf2008-08-17 17:08:36 +01004399
Miao Xie16cdcec2011-04-22 18:12:22 +08004400 if (key_type == BTRFS_DIR_INDEX_KEY) {
4401 if (is_curr)
4402 filp->f_pos++;
4403 ret = btrfs_readdir_delayed_dir_index(filp, dirent, filldir,
4404 &ins_list);
4405 if (ret)
4406 goto nopos;
4407 }
4408
David Woodhouse49593bf2008-08-17 17:08:36 +01004409 /* Reached end of directory/root. Bump pos past the last item. */
Yan Zheng5e591a02008-02-19 11:41:02 -05004410 if (key_type == BTRFS_DIR_INDEX_KEY)
Jan Engelhardt406266a2009-12-09 22:00:38 +00004411 /*
4412 * 32-bit glibc will use getdents64, but then strtol -
4413 * so the last number we can serve is this.
4414 */
4415 filp->f_pos = 0x7fffffff;
Yan Zheng5e591a02008-02-19 11:41:02 -05004416 else
4417 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04004418nopos:
4419 ret = 0;
4420err:
Miao Xie16cdcec2011-04-22 18:12:22 +08004421 if (key_type == BTRFS_DIR_INDEX_KEY)
4422 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04004423 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004424 return ret;
4425}
4426
Christoph Hellwiga9185b42010-03-05 09:21:37 +01004427int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04004428{
4429 struct btrfs_root *root = BTRFS_I(inode)->root;
4430 struct btrfs_trans_handle *trans;
4431 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04004432 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04004433
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004434 if (BTRFS_I(inode)->dummy_inode)
Chris Mason4ca8b412008-08-05 13:30:48 -04004435 return 0;
4436
Josef Bacik0af3d002010-06-21 14:48:16 -04004437 smp_mb();
Li Zefan82d59022011-04-20 10:33:24 +08004438 if (root->fs_info->closing && is_free_space_inode(root, inode))
4439 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04004440
Christoph Hellwiga9185b42010-03-05 09:21:37 +01004441 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04004442 if (nolock)
4443 trans = btrfs_join_transaction_nolock(root, 1);
4444 else
4445 trans = btrfs_join_transaction(root, 1);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00004446 if (IS_ERR(trans))
4447 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04004448 btrfs_set_trans_block_group(trans, inode);
Josef Bacik0af3d002010-06-21 14:48:16 -04004449 if (nolock)
4450 ret = btrfs_end_transaction_nolock(trans, root);
4451 else
4452 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004453 }
4454 return ret;
4455}
4456
4457/*
Chris Mason54aa1f42007-06-22 14:16:25 -04004458 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04004459 * inode changes. But, it is most likely to find the inode in cache.
4460 * FIXME, needs more benchmarking...there are no reasons other than performance
4461 * to keep or drop this code.
4462 */
4463void btrfs_dirty_inode(struct inode *inode)
4464{
4465 struct btrfs_root *root = BTRFS_I(inode)->root;
4466 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004467 int ret;
4468
4469 if (BTRFS_I(inode)->dummy_inode)
4470 return;
Chris Mason39279cc2007-06-12 06:35:45 -04004471
Chris Masonf9295742008-07-17 12:54:14 -04004472 trans = btrfs_join_transaction(root, 1);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00004473 BUG_ON(IS_ERR(trans));
Chris Mason39279cc2007-06-12 06:35:45 -04004474 btrfs_set_trans_block_group(trans, inode);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004475
4476 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04004477 if (ret && ret == -ENOSPC) {
4478 /* whoops, lets try again with the full transaction */
4479 btrfs_end_transaction(trans, root);
4480 trans = btrfs_start_transaction(root, 1);
Chris Mason9aeead72010-05-27 10:23:00 -04004481 if (IS_ERR(trans)) {
4482 if (printk_ratelimit()) {
4483 printk(KERN_ERR "btrfs: fail to "
Li Zefan33345d012011-04-20 10:31:50 +08004484 "dirty inode %llu error %ld\n",
4485 (unsigned long long)btrfs_ino(inode),
4486 PTR_ERR(trans));
Chris Mason9aeead72010-05-27 10:23:00 -04004487 }
4488 return;
4489 }
Chris Mason94b60442010-05-26 11:02:00 -04004490 btrfs_set_trans_block_group(trans, inode);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004491
Chris Mason94b60442010-05-26 11:02:00 -04004492 ret = btrfs_update_inode(trans, root, inode);
4493 if (ret) {
Chris Mason9aeead72010-05-27 10:23:00 -04004494 if (printk_ratelimit()) {
4495 printk(KERN_ERR "btrfs: fail to "
Li Zefan33345d012011-04-20 10:31:50 +08004496 "dirty inode %llu error %d\n",
4497 (unsigned long long)btrfs_ino(inode),
4498 ret);
Chris Mason9aeead72010-05-27 10:23:00 -04004499 }
Chris Mason94b60442010-05-26 11:02:00 -04004500 }
4501 }
Chris Mason39279cc2007-06-12 06:35:45 -04004502 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08004503 if (BTRFS_I(inode)->delayed_node)
4504 btrfs_balance_delayed_items(root);
Chris Mason39279cc2007-06-12 06:35:45 -04004505}
4506
Chris Masond352ac62008-09-29 15:18:18 -04004507/*
4508 * find the highest existing sequence number in a directory
4509 * and then set the in-memory index_cnt variable to reflect
4510 * free sequence numbers
4511 */
Josef Bacikaec74772008-07-24 12:12:38 -04004512static int btrfs_set_inode_index_count(struct inode *inode)
4513{
4514 struct btrfs_root *root = BTRFS_I(inode)->root;
4515 struct btrfs_key key, found_key;
4516 struct btrfs_path *path;
4517 struct extent_buffer *leaf;
4518 int ret;
4519
Li Zefan33345d012011-04-20 10:31:50 +08004520 key.objectid = btrfs_ino(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004521 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
4522 key.offset = (u64)-1;
4523
4524 path = btrfs_alloc_path();
4525 if (!path)
4526 return -ENOMEM;
4527
4528 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4529 if (ret < 0)
4530 goto out;
4531 /* FIXME: we should be able to handle this */
4532 if (ret == 0)
4533 goto out;
4534 ret = 0;
4535
4536 /*
4537 * MAGIC NUMBER EXPLANATION:
4538 * since we search a directory based on f_pos we have to start at 2
4539 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
4540 * else has to start at 2
4541 */
4542 if (path->slots[0] == 0) {
4543 BTRFS_I(inode)->index_cnt = 2;
4544 goto out;
4545 }
4546
4547 path->slots[0]--;
4548
4549 leaf = path->nodes[0];
4550 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4551
Li Zefan33345d012011-04-20 10:31:50 +08004552 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacikaec74772008-07-24 12:12:38 -04004553 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
4554 BTRFS_I(inode)->index_cnt = 2;
4555 goto out;
4556 }
4557
4558 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
4559out:
4560 btrfs_free_path(path);
4561 return ret;
4562}
4563
Chris Masond352ac62008-09-29 15:18:18 -04004564/*
4565 * helper to find a free sequence number in a given directory. This current
4566 * code is very simple, later versions will do smarter things in the btree
4567 */
Chris Mason3de45862008-11-17 21:02:50 -05004568int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04004569{
4570 int ret = 0;
4571
4572 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08004573 ret = btrfs_inode_delayed_dir_index_count(dir);
4574 if (ret) {
4575 ret = btrfs_set_inode_index_count(dir);
4576 if (ret)
4577 return ret;
4578 }
Josef Bacikaec74772008-07-24 12:12:38 -04004579 }
4580
Chris Mason00e4e6b2008-08-05 11:18:09 -04004581 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04004582 BTRFS_I(dir)->index_cnt++;
4583
4584 return ret;
4585}
4586
Chris Mason39279cc2007-06-12 06:35:45 -04004587static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
4588 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04004589 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05004590 const char *name, int name_len,
Yan Zhengd2fb3432008-12-11 16:30:39 -05004591 u64 ref_objectid, u64 objectid,
4592 u64 alloc_hint, int mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04004593{
4594 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004595 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04004596 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04004597 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05004598 struct btrfs_inode_ref *ref;
4599 struct btrfs_key key[2];
4600 u32 sizes[2];
4601 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04004602 int ret;
4603 int owner;
4604
Chris Mason5f39d392007-10-15 16:14:19 -04004605 path = btrfs_alloc_path();
4606 BUG_ON(!path);
4607
Chris Mason39279cc2007-06-12 06:35:45 -04004608 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00004609 if (!inode) {
4610 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004611 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00004612 }
Chris Mason39279cc2007-06-12 06:35:45 -04004613
Li Zefan581bb052011-04-20 10:06:11 +08004614 /*
4615 * we have to initialize this early, so we can reclaim the inode
4616 * number if we fail afterwards in this function.
4617 */
4618 inode->i_ino = objectid;
4619
Josef Bacikaec74772008-07-24 12:12:38 -04004620 if (dir) {
liubo1abe9b82011-03-24 11:18:59 +00004621 trace_btrfs_inode_request(dir);
4622
Chris Mason3de45862008-11-17 21:02:50 -05004623 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04004624 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00004625 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04004626 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004627 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04004628 }
Josef Bacikaec74772008-07-24 12:12:38 -04004629 }
4630 /*
4631 * index_cnt is ignored for everything but a dir,
4632 * btrfs_get_inode_index_count has an explanation for the magic
4633 * number
4634 */
4635 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04004636 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04004637 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00004638 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik6a632092009-02-20 11:00:09 -05004639 btrfs_set_inode_space_info(root, inode);
Chris Masonb888db22007-08-27 16:49:44 -04004640
Chris Mason39279cc2007-06-12 06:35:45 -04004641 if (mode & S_IFDIR)
4642 owner = 0;
4643 else
4644 owner = 1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05004645 BTRFS_I(inode)->block_group =
4646 btrfs_find_block_group(root, 0, alloc_hint, owner);
Chris Mason9c583092008-01-29 15:15:18 -05004647
4648 key[0].objectid = objectid;
4649 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
4650 key[0].offset = 0;
4651
4652 key[1].objectid = objectid;
4653 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
4654 key[1].offset = ref_objectid;
4655
4656 sizes[0] = sizeof(struct btrfs_inode_item);
4657 sizes[1] = name_len + sizeof(*ref);
4658
Chris Masonb9473432009-03-13 11:00:37 -04004659 path->leave_spinning = 1;
Chris Mason9c583092008-01-29 15:15:18 -05004660 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
4661 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04004662 goto fail;
4663
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03004664 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004665 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004666 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04004667 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4668 struct btrfs_inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04004669 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05004670
4671 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
4672 struct btrfs_inode_ref);
4673 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04004674 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05004675 ptr = (unsigned long)(ref + 1);
4676 write_extent_buffer(path->nodes[0], name, ptr, name_len);
4677
Chris Mason5f39d392007-10-15 16:14:19 -04004678 btrfs_mark_buffer_dirty(path->nodes[0]);
4679 btrfs_free_path(path);
4680
Chris Mason39279cc2007-06-12 06:35:45 -04004681 location = &BTRFS_I(inode)->location;
4682 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04004683 location->offset = 0;
4684 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
4685
Christoph Hellwig6cbff002009-04-17 10:37:41 +02004686 btrfs_inherit_iflags(inode, dir);
4687
Chris Mason94272162009-07-02 12:26:06 -04004688 if ((mode & S_IFREG)) {
4689 if (btrfs_test_opt(root, NODATASUM))
4690 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo75e7cb72011-03-22 10:12:20 +00004691 if (btrfs_test_opt(root, NODATACOW) ||
4692 (BTRFS_I(dir)->flags & BTRFS_INODE_NODATACOW))
Chris Mason94272162009-07-02 12:26:06 -04004693 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
4694 }
4695
Chris Mason39279cc2007-06-12 06:35:45 -04004696 insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004697 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00004698
4699 trace_btrfs_inode_new(inode);
4700
Chris Mason39279cc2007-06-12 06:35:45 -04004701 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004702fail:
Josef Bacikaec74772008-07-24 12:12:38 -04004703 if (dir)
4704 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04004705 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04004706 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004707 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004708}
4709
4710static inline u8 btrfs_inode_type(struct inode *inode)
4711{
4712 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
4713}
4714
Chris Masond352ac62008-09-29 15:18:18 -04004715/*
4716 * utility function to add 'inode' into 'parent_inode' with
4717 * a give name and a given sequence number.
4718 * if 'add_backref' is true, also insert a backref from the
4719 * inode to the parent directory.
4720 */
Chris Masone02119d2008-09-05 16:13:11 -04004721int btrfs_add_link(struct btrfs_trans_handle *trans,
4722 struct inode *parent_inode, struct inode *inode,
4723 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04004724{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004725 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004726 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04004727 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08004728 u64 ino = btrfs_ino(inode);
4729 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004730
Li Zefan33345d012011-04-20 10:31:50 +08004731 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004732 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
4733 } else {
Li Zefan33345d012011-04-20 10:31:50 +08004734 key.objectid = ino;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004735 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
4736 key.offset = 0;
4737 }
Chris Mason39279cc2007-06-12 06:35:45 -04004738
Li Zefan33345d012011-04-20 10:31:50 +08004739 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004740 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
4741 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08004742 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004743 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08004744 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
4745 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004746 }
4747
Chris Mason39279cc2007-06-12 06:35:45 -04004748 if (ret == 0) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004749 ret = btrfs_insert_dir_item(trans, root, name, name_len,
Miao Xie16cdcec2011-04-22 18:12:22 +08004750 parent_inode, &key,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004751 btrfs_inode_type(inode), index);
4752 BUG_ON(ret);
4753
Chris Masondbe674a2008-07-17 12:54:05 -04004754 btrfs_i_size_write(parent_inode, parent_inode->i_size +
Chris Masone02119d2008-09-05 16:13:11 -04004755 name_len * 2);
Chris Mason79c44582007-06-25 10:09:33 -04004756 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Masone02119d2008-09-05 16:13:11 -04004757 ret = btrfs_update_inode(trans, root, parent_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004758 }
4759 return ret;
4760}
4761
4762static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00004763 struct inode *dir, struct dentry *dentry,
4764 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04004765{
Josef Bacika1b075d2010-11-19 20:36:11 +00004766 int err = btrfs_add_link(trans, dir, inode,
4767 dentry->d_name.name, dentry->d_name.len,
4768 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004769 if (!err) {
4770 d_instantiate(dentry, inode);
4771 return 0;
4772 }
4773 if (err > 0)
4774 err = -EEXIST;
4775 return err;
4776}
4777
Josef Bacik618e21d2007-07-11 10:18:17 -04004778static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
4779 int mode, dev_t rdev)
4780{
4781 struct btrfs_trans_handle *trans;
4782 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05004783 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04004784 int err;
4785 int drop_inode = 0;
4786 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05004787 unsigned long nr = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004788 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04004789
4790 if (!new_valid_dev(rdev))
4791 return -EINVAL;
4792
Josef Bacik9ed74f22009-09-11 16:12:44 -04004793 /*
4794 * 2 for inode item and ref
4795 * 2 for dir items
4796 * 1 for xattr if selinux is on
4797 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004798 trans = btrfs_start_transaction(root, 5);
4799 if (IS_ERR(trans))
4800 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05004801
Josef Bacik618e21d2007-07-11 10:18:17 -04004802 btrfs_set_trans_block_group(trans, dir);
4803
Li Zefan581bb052011-04-20 10:06:11 +08004804 err = btrfs_find_free_ino(root, &objectid);
4805 if (err)
4806 goto out_unlock;
4807
Josef Bacikaec74772008-07-24 12:12:38 -04004808 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08004809 dentry->d_name.len, btrfs_ino(dir), objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04004810 BTRFS_I(dir)->block_group, mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04004811 if (IS_ERR(inode)) {
4812 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04004813 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04004814 }
Josef Bacik618e21d2007-07-11 10:18:17 -04004815
Eric Paris2a7dba32011-02-01 11:05:39 -05004816 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04004817 if (err) {
4818 drop_inode = 1;
4819 goto out_unlock;
4820 }
4821
Josef Bacik618e21d2007-07-11 10:18:17 -04004822 btrfs_set_trans_block_group(trans, inode);
Josef Bacika1b075d2010-11-19 20:36:11 +00004823 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04004824 if (err)
4825 drop_inode = 1;
4826 else {
4827 inode->i_op = &btrfs_special_inode_operations;
4828 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04004829 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04004830 }
Josef Bacik618e21d2007-07-11 10:18:17 -04004831 btrfs_update_inode_block_group(trans, inode);
4832 btrfs_update_inode_block_group(trans, dir);
4833out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004834 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04004835 btrfs_end_transaction_throttle(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004836 btrfs_btree_balance_dirty(root, nr);
Josef Bacik618e21d2007-07-11 10:18:17 -04004837 if (drop_inode) {
4838 inode_dec_link_count(inode);
4839 iput(inode);
4840 }
Josef Bacik618e21d2007-07-11 10:18:17 -04004841 return err;
4842}
4843
Chris Mason39279cc2007-06-12 06:35:45 -04004844static int btrfs_create(struct inode *dir, struct dentry *dentry,
4845 int mode, struct nameidata *nd)
4846{
4847 struct btrfs_trans_handle *trans;
4848 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05004849 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04004850 int drop_inode = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004851 int err;
Chris Mason1832a6d2007-12-21 16:27:21 -05004852 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004853 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004854 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004855
Josef Bacik9ed74f22009-09-11 16:12:44 -04004856 /*
4857 * 2 for inode item and ref
4858 * 2 for dir items
4859 * 1 for xattr if selinux is on
4860 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004861 trans = btrfs_start_transaction(root, 5);
4862 if (IS_ERR(trans))
4863 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004864
Chris Mason39279cc2007-06-12 06:35:45 -04004865 btrfs_set_trans_block_group(trans, dir);
4866
Li Zefan581bb052011-04-20 10:06:11 +08004867 err = btrfs_find_free_ino(root, &objectid);
4868 if (err)
4869 goto out_unlock;
4870
Josef Bacikaec74772008-07-24 12:12:38 -04004871 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08004872 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacika1b075d2010-11-19 20:36:11 +00004873 BTRFS_I(dir)->block_group, mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04004874 if (IS_ERR(inode)) {
4875 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004876 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04004877 }
Chris Mason39279cc2007-06-12 06:35:45 -04004878
Eric Paris2a7dba32011-02-01 11:05:39 -05004879 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04004880 if (err) {
4881 drop_inode = 1;
4882 goto out_unlock;
4883 }
4884
Chris Mason39279cc2007-06-12 06:35:45 -04004885 btrfs_set_trans_block_group(trans, inode);
Josef Bacika1b075d2010-11-19 20:36:11 +00004886 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004887 if (err)
4888 drop_inode = 1;
4889 else {
4890 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04004891 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04004892 inode->i_fop = &btrfs_file_operations;
4893 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05004894 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04004895 }
Chris Mason39279cc2007-06-12 06:35:45 -04004896 btrfs_update_inode_block_group(trans, inode);
4897 btrfs_update_inode_block_group(trans, dir);
4898out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004899 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04004900 btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004901 if (drop_inode) {
4902 inode_dec_link_count(inode);
4903 iput(inode);
4904 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004905 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004906 return err;
4907}
4908
4909static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
4910 struct dentry *dentry)
4911{
4912 struct btrfs_trans_handle *trans;
4913 struct btrfs_root *root = BTRFS_I(dir)->root;
4914 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004915 u64 index;
Chris Mason1832a6d2007-12-21 16:27:21 -05004916 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004917 int err;
4918 int drop_inode = 0;
4919
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00004920 /* do not allow sys_link's with other subvols of the same device */
4921 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00004922 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00004923
Al Viroc055e992011-03-04 17:15:18 +00004924 if (inode->i_nlink == ~0U)
4925 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004926
Chris Mason3de45862008-11-17 21:02:50 -05004927 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004928 if (err)
4929 goto fail;
4930
Yan, Zhenga22285a2010-05-16 10:48:46 -04004931 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00004932 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04004933 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00004934 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04004935 */
Miao Xie7e6b6462011-02-18 09:21:17 +00004936 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004937 if (IS_ERR(trans)) {
4938 err = PTR_ERR(trans);
4939 goto fail;
4940 }
Chris Mason5f39d392007-10-15 16:14:19 -04004941
Miao Xie31534952011-04-13 13:19:21 +08004942 btrfs_inc_nlink(inode);
4943 inode->i_ctime = CURRENT_TIME;
4944
Chris Mason39279cc2007-06-12 06:35:45 -04004945 btrfs_set_trans_block_group(trans, dir);
Al Viro7de9c6ee2010-10-23 11:11:40 -04004946 ihold(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004947
Josef Bacika1b075d2010-11-19 20:36:11 +00004948 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04004949
Yan, Zhenga5719522009-09-24 09:17:31 -04004950 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04004951 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04004952 } else {
Josef Bacik6a912212010-11-20 09:48:00 +00004953 struct dentry *parent = dget_parent(dentry);
Yan, Zhenga5719522009-09-24 09:17:31 -04004954 btrfs_update_inode_block_group(trans, dir);
4955 err = btrfs_update_inode(trans, root, inode);
4956 BUG_ON(err);
Josef Bacik6a912212010-11-20 09:48:00 +00004957 btrfs_log_new_name(trans, inode, NULL, parent);
4958 dput(parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04004959 }
Chris Mason39279cc2007-06-12 06:35:45 -04004960
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004961 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04004962 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05004963fail:
Chris Mason39279cc2007-06-12 06:35:45 -04004964 if (drop_inode) {
4965 inode_dec_link_count(inode);
4966 iput(inode);
4967 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004968 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004969 return err;
4970}
4971
Chris Mason39279cc2007-06-12 06:35:45 -04004972static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
4973{
Chris Masonb9d86662008-05-02 16:13:49 -04004974 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04004975 struct btrfs_trans_handle *trans;
4976 struct btrfs_root *root = BTRFS_I(dir)->root;
4977 int err = 0;
4978 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04004979 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004980 u64 index = 0;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004981 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04004982
Josef Bacik9ed74f22009-09-11 16:12:44 -04004983 /*
4984 * 2 items for inode and ref
4985 * 2 items for dir items
4986 * 1 for xattr if selinux is on
4987 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004988 trans = btrfs_start_transaction(root, 5);
4989 if (IS_ERR(trans))
4990 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004991 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -04004992
Li Zefan581bb052011-04-20 10:06:11 +08004993 err = btrfs_find_free_ino(root, &objectid);
4994 if (err)
4995 goto out_fail;
4996
Josef Bacikaec74772008-07-24 12:12:38 -04004997 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08004998 dentry->d_name.len, btrfs_ino(dir), objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04004999 BTRFS_I(dir)->block_group, S_IFDIR | mode,
5000 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04005001 if (IS_ERR(inode)) {
5002 err = PTR_ERR(inode);
5003 goto out_fail;
5004 }
Chris Mason5f39d392007-10-15 16:14:19 -04005005
Chris Mason39279cc2007-06-12 06:35:45 -04005006 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04005007
Eric Paris2a7dba32011-02-01 11:05:39 -05005008 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005009 if (err)
5010 goto out_fail;
5011
Chris Mason39279cc2007-06-12 06:35:45 -04005012 inode->i_op = &btrfs_dir_inode_operations;
5013 inode->i_fop = &btrfs_dir_file_operations;
5014 btrfs_set_trans_block_group(trans, inode);
5015
Chris Masondbe674a2008-07-17 12:54:05 -04005016 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005017 err = btrfs_update_inode(trans, root, inode);
5018 if (err)
5019 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005020
Josef Bacika1b075d2010-11-19 20:36:11 +00005021 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
5022 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005023 if (err)
5024 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005025
Chris Mason39279cc2007-06-12 06:35:45 -04005026 d_instantiate(dentry, inode);
5027 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005028 btrfs_update_inode_block_group(trans, inode);
5029 btrfs_update_inode_block_group(trans, dir);
5030
5031out_fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04005032 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04005033 btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005034 if (drop_on_err)
5035 iput(inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04005036 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04005037 return err;
5038}
5039
Chris Masond352ac62008-09-29 15:18:18 -04005040/* helper for btfs_get_extent. Given an existing extent in the tree,
5041 * and an extent that you want to insert, deal with overlap and insert
5042 * the new extent into the tree.
5043 */
Chris Mason3b951512008-04-17 11:29:12 -04005044static int merge_extent_mapping(struct extent_map_tree *em_tree,
5045 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005046 struct extent_map *em,
5047 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04005048{
5049 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04005050
Chris Masone6dcd2d2008-07-17 12:53:50 -04005051 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
5052 start_diff = map_start - em->start;
5053 em->start = map_start;
5054 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04005055 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
5056 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04005057 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04005058 em->block_len -= start_diff;
5059 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005060 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04005061}
5062
Chris Masonc8b97812008-10-29 14:49:59 -04005063static noinline int uncompress_inline(struct btrfs_path *path,
5064 struct inode *inode, struct page *page,
5065 size_t pg_offset, u64 extent_offset,
5066 struct btrfs_file_extent_item *item)
5067{
5068 int ret;
5069 struct extent_buffer *leaf = path->nodes[0];
5070 char *tmp;
5071 size_t max_size;
5072 unsigned long inline_size;
5073 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08005074 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04005075
5076 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08005077 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04005078 max_size = btrfs_file_extent_ram_bytes(leaf, item);
5079 inline_size = btrfs_file_extent_inline_item_len(leaf,
5080 btrfs_item_nr(leaf, path->slots[0]));
5081 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04005082 if (!tmp)
5083 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04005084 ptr = btrfs_file_extent_inline_start(item);
5085
5086 read_extent_buffer(leaf, tmp, ptr, inline_size);
5087
Chris Mason5b050f02008-11-11 09:34:41 -05005088 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08005089 ret = btrfs_decompress(compress_type, tmp, page,
5090 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04005091 if (ret) {
5092 char *kaddr = kmap_atomic(page, KM_USER0);
5093 unsigned long copy_size = min_t(u64,
5094 PAGE_CACHE_SIZE - pg_offset,
5095 max_size - extent_offset);
5096 memset(kaddr + pg_offset, 0, copy_size);
5097 kunmap_atomic(kaddr, KM_USER0);
5098 }
5099 kfree(tmp);
5100 return 0;
5101}
5102
Chris Masond352ac62008-09-29 15:18:18 -04005103/*
5104 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05005105 * the ugly parts come from merging extents from the disk with the in-ram
5106 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04005107 * where the in-ram extents might be locked pending data=ordered completion.
5108 *
5109 * This also copies inline extents directly into the page.
5110 */
Chris Masond3977122009-01-05 21:25:51 -05005111
Chris Masona52d9a82007-08-27 16:49:44 -04005112struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05005113 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04005114 int create)
5115{
5116 int ret;
5117 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04005118 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04005119 u64 extent_start = 0;
5120 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08005121 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04005122 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04005123 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04005124 struct btrfs_root *root = BTRFS_I(inode)->root;
5125 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04005126 struct extent_buffer *leaf;
5127 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04005128 struct extent_map *em = NULL;
5129 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05005130 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04005131 struct btrfs_trans_handle *trans = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08005132 int compress_type;
Chris Masona52d9a82007-08-27 16:49:44 -04005133
Chris Masona52d9a82007-08-27 16:49:44 -04005134again:
Chris Mason890871b2009-09-02 16:24:52 -04005135 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005136 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04005137 if (em)
5138 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04005139 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005140
Chris Masona52d9a82007-08-27 16:49:44 -04005141 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04005142 if (em->start > start || em->start + em->len <= start)
5143 free_extent_map(em);
5144 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05005145 free_extent_map(em);
5146 else
5147 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005148 }
Chris Masond1310b22008-01-24 16:13:08 -05005149 em = alloc_extent_map(GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04005150 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05005151 err = -ENOMEM;
5152 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005153 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005154 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05005155 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05005156 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05005157 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04005158 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04005159
5160 if (!path) {
5161 path = btrfs_alloc_path();
5162 BUG_ON(!path);
5163 }
5164
Chris Mason179e29e2007-11-01 11:28:41 -04005165 ret = btrfs_lookup_file_extent(trans, root, path,
5166 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04005167 if (ret < 0) {
5168 err = ret;
5169 goto out;
5170 }
5171
5172 if (ret != 0) {
5173 if (path->slots[0] == 0)
5174 goto not_found;
5175 path->slots[0]--;
5176 }
5177
Chris Mason5f39d392007-10-15 16:14:19 -04005178 leaf = path->nodes[0];
5179 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04005180 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04005181 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04005182 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5183 found_type = btrfs_key_type(&found_key);
5184 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04005185 found_type != BTRFS_EXTENT_DATA_KEY) {
5186 goto not_found;
5187 }
5188
Chris Mason5f39d392007-10-15 16:14:19 -04005189 found_type = btrfs_file_extent_type(leaf, item);
5190 extent_start = found_key.offset;
Li Zefan261507a02010-12-17 14:21:50 +08005191 compress_type = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04005192 if (found_type == BTRFS_FILE_EXTENT_REG ||
5193 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04005194 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04005195 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04005196 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
5197 size_t size;
5198 size = btrfs_file_extent_inline_len(leaf, item);
5199 extent_end = (extent_start + size + root->sectorsize - 1) &
5200 ~((u64)root->sectorsize - 1);
5201 }
5202
5203 if (start >= extent_end) {
5204 path->slots[0]++;
5205 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
5206 ret = btrfs_next_leaf(root, path);
5207 if (ret < 0) {
5208 err = ret;
5209 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005210 }
Yan Zheng9036c102008-10-30 14:19:41 -04005211 if (ret > 0)
5212 goto not_found;
5213 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04005214 }
Yan Zheng9036c102008-10-30 14:19:41 -04005215 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5216 if (found_key.objectid != objectid ||
5217 found_key.type != BTRFS_EXTENT_DATA_KEY)
5218 goto not_found;
5219 if (start + len <= found_key.offset)
5220 goto not_found;
5221 em->start = start;
5222 em->len = found_key.offset - start;
5223 goto not_found_em;
5224 }
5225
Yan Zhengd899e052008-10-30 14:25:28 -04005226 if (found_type == BTRFS_FILE_EXTENT_REG ||
5227 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04005228 em->start = extent_start;
5229 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05005230 em->orig_start = extent_start -
5231 btrfs_file_extent_offset(leaf, item);
Chris Masondb945352007-10-15 16:15:53 -04005232 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
5233 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04005234 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04005235 goto insert;
5236 }
Li Zefan261507a02010-12-17 14:21:50 +08005237 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04005238 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08005239 em->compress_type = compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04005240 em->block_start = bytenr;
5241 em->block_len = btrfs_file_extent_disk_num_bytes(leaf,
5242 item);
5243 } else {
5244 bytenr += btrfs_file_extent_offset(leaf, item);
5245 em->block_start = bytenr;
5246 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04005247 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
5248 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04005249 }
Chris Masona52d9a82007-08-27 16:49:44 -04005250 goto insert;
5251 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04005252 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04005253 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04005254 size_t size;
5255 size_t extent_offset;
5256 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04005257
Chris Masona52d9a82007-08-27 16:49:44 -04005258 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04005259 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04005260 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04005261 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04005262 goto out;
5263 }
5264
Yan Zheng9036c102008-10-30 14:19:41 -04005265 size = btrfs_file_extent_inline_len(leaf, item);
5266 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05005267 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04005268 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04005269 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05005270 em->len = (copy_size + root->sectorsize - 1) &
5271 ~((u64)root->sectorsize - 1);
Yan Zhengff5b7ee2008-11-10 07:34:43 -05005272 em->orig_start = EXTENT_MAP_INLINE;
Li Zefan261507a02010-12-17 14:21:50 +08005273 if (compress_type) {
Chris Masonc8b97812008-10-29 14:49:59 -04005274 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08005275 em->compress_type = compress_type;
5276 }
Yan689f9342007-10-29 11:41:07 -04005277 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04005278 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08005279 if (btrfs_file_extent_compression(leaf, item) !=
5280 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04005281 ret = uncompress_inline(path, inode, page,
5282 pg_offset,
5283 extent_offset, item);
5284 BUG_ON(ret);
5285 } else {
5286 map = kmap(page);
5287 read_extent_buffer(leaf, map + pg_offset, ptr,
5288 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04005289 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
5290 memset(map + pg_offset + copy_size, 0,
5291 PAGE_CACHE_SIZE - pg_offset -
5292 copy_size);
5293 }
Chris Masonc8b97812008-10-29 14:49:59 -04005294 kunmap(page);
5295 }
Chris Mason179e29e2007-11-01 11:28:41 -04005296 flush_dcache_page(page);
5297 } else if (create && PageUptodate(page)) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005298 WARN_ON(1);
Chris Mason179e29e2007-11-01 11:28:41 -04005299 if (!trans) {
5300 kunmap(page);
5301 free_extent_map(em);
5302 em = NULL;
5303 btrfs_release_path(root, path);
Chris Masonf9295742008-07-17 12:54:14 -04005304 trans = btrfs_join_transaction(root, 1);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005305 if (IS_ERR(trans))
5306 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04005307 goto again;
5308 }
Chris Masonc8b97812008-10-29 14:49:59 -04005309 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05005310 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04005311 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04005312 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04005313 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04005314 }
Chris Masond1310b22008-01-24 16:13:08 -05005315 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00005316 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04005317 goto insert;
5318 } else {
Chris Masond3977122009-01-05 21:25:51 -05005319 printk(KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04005320 WARN_ON(1);
5321 }
5322not_found:
5323 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05005324 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04005325not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04005326 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04005327 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04005328insert:
5329 btrfs_release_path(root, path);
Chris Masond1310b22008-01-24 16:13:08 -05005330 if (em->start > start || extent_map_end(em) <= start) {
Chris Masond3977122009-01-05 21:25:51 -05005331 printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
5332 "[%llu %llu]\n", (unsigned long long)em->start,
5333 (unsigned long long)em->len,
5334 (unsigned long long)start,
5335 (unsigned long long)len);
Chris Masona52d9a82007-08-27 16:49:44 -04005336 err = -EIO;
5337 goto out;
5338 }
Chris Masond1310b22008-01-24 16:13:08 -05005339
5340 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04005341 write_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04005342 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04005343 /* it is possible that someone inserted the extent into the tree
5344 * while we had the lock dropped. It is also possible that
5345 * an overlapping map exists in the tree
5346 */
Chris Masona52d9a82007-08-27 16:49:44 -04005347 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04005348 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005349
5350 ret = 0;
5351
Chris Mason3b951512008-04-17 11:29:12 -04005352 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04005353 if (existing && (existing->start > start ||
5354 existing->start + existing->len <= start)) {
5355 free_extent_map(existing);
5356 existing = NULL;
5357 }
Chris Mason3b951512008-04-17 11:29:12 -04005358 if (!existing) {
5359 existing = lookup_extent_mapping(em_tree, em->start,
5360 em->len);
5361 if (existing) {
5362 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005363 em, start,
5364 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04005365 free_extent_map(existing);
5366 if (err) {
5367 free_extent_map(em);
5368 em = NULL;
5369 }
5370 } else {
5371 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04005372 free_extent_map(em);
5373 em = NULL;
5374 }
5375 } else {
5376 free_extent_map(em);
5377 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005378 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04005379 }
Chris Masona52d9a82007-08-27 16:49:44 -04005380 }
Chris Mason890871b2009-09-02 16:24:52 -04005381 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04005382out:
liubo1abe9b82011-03-24 11:18:59 +00005383
5384 trace_btrfs_get_extent(root, em);
5385
Chris Masonf4219502008-07-22 11:18:09 -04005386 if (path)
5387 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04005388 if (trans) {
5389 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05005390 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04005391 err = ret;
5392 }
Chris Masona52d9a82007-08-27 16:49:44 -04005393 if (err) {
5394 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04005395 return ERR_PTR(err);
5396 }
5397 return em;
5398}
5399
Chris Masonec29ed52011-02-23 16:23:20 -05005400struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
5401 size_t pg_offset, u64 start, u64 len,
5402 int create)
5403{
5404 struct extent_map *em;
5405 struct extent_map *hole_em = NULL;
5406 u64 range_start = start;
5407 u64 end;
5408 u64 found;
5409 u64 found_end;
5410 int err = 0;
5411
5412 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
5413 if (IS_ERR(em))
5414 return em;
5415 if (em) {
5416 /*
5417 * if our em maps to a hole, there might
5418 * actually be delalloc bytes behind it
5419 */
5420 if (em->block_start != EXTENT_MAP_HOLE)
5421 return em;
5422 else
5423 hole_em = em;
5424 }
5425
5426 /* check to see if we've wrapped (len == -1 or similar) */
5427 end = start + len;
5428 if (end < start)
5429 end = (u64)-1;
5430 else
5431 end -= 1;
5432
5433 em = NULL;
5434
5435 /* ok, we didn't find anything, lets look for delalloc */
5436 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
5437 end, len, EXTENT_DELALLOC, 1);
5438 found_end = range_start + found;
5439 if (found_end < range_start)
5440 found_end = (u64)-1;
5441
5442 /*
5443 * we didn't find anything useful, return
5444 * the original results from get_extent()
5445 */
5446 if (range_start > end || found_end <= start) {
5447 em = hole_em;
5448 hole_em = NULL;
5449 goto out;
5450 }
5451
5452 /* adjust the range_start to make sure it doesn't
5453 * go backwards from the start they passed in
5454 */
5455 range_start = max(start,range_start);
5456 found = found_end - range_start;
5457
5458 if (found > 0) {
5459 u64 hole_start = start;
5460 u64 hole_len = len;
5461
5462 em = alloc_extent_map(GFP_NOFS);
5463 if (!em) {
5464 err = -ENOMEM;
5465 goto out;
5466 }
5467 /*
5468 * when btrfs_get_extent can't find anything it
5469 * returns one huge hole
5470 *
5471 * make sure what it found really fits our range, and
5472 * adjust to make sure it is based on the start from
5473 * the caller
5474 */
5475 if (hole_em) {
5476 u64 calc_end = extent_map_end(hole_em);
5477
5478 if (calc_end <= start || (hole_em->start > end)) {
5479 free_extent_map(hole_em);
5480 hole_em = NULL;
5481 } else {
5482 hole_start = max(hole_em->start, start);
5483 hole_len = calc_end - hole_start;
5484 }
5485 }
5486 em->bdev = NULL;
5487 if (hole_em && range_start > hole_start) {
5488 /* our hole starts before our delalloc, so we
5489 * have to return just the parts of the hole
5490 * that go until the delalloc starts
5491 */
5492 em->len = min(hole_len,
5493 range_start - hole_start);
5494 em->start = hole_start;
5495 em->orig_start = hole_start;
5496 /*
5497 * don't adjust block start at all,
5498 * it is fixed at EXTENT_MAP_HOLE
5499 */
5500 em->block_start = hole_em->block_start;
5501 em->block_len = hole_len;
5502 } else {
5503 em->start = range_start;
5504 em->len = found;
5505 em->orig_start = range_start;
5506 em->block_start = EXTENT_MAP_DELALLOC;
5507 em->block_len = found;
5508 }
5509 } else if (hole_em) {
5510 return hole_em;
5511 }
5512out:
5513
5514 free_extent_map(hole_em);
5515 if (err) {
5516 free_extent_map(em);
5517 return ERR_PTR(err);
5518 }
5519 return em;
5520}
5521
Josef Bacik4b46fce2010-05-23 11:00:55 -04005522static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
Josef Bacik16d299a2011-04-06 14:53:07 -04005523 struct extent_map *em,
Josef Bacik4b46fce2010-05-23 11:00:55 -04005524 u64 start, u64 len)
5525{
5526 struct btrfs_root *root = BTRFS_I(inode)->root;
5527 struct btrfs_trans_handle *trans;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005528 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
5529 struct btrfs_key ins;
5530 u64 alloc_hint;
5531 int ret;
Josef Bacik16d299a2011-04-06 14:53:07 -04005532 bool insert = false;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005533
Josef Bacik16d299a2011-04-06 14:53:07 -04005534 /*
5535 * Ok if the extent map we looked up is a hole and is for the exact
5536 * range we want, there is no reason to allocate a new one, however if
5537 * it is not right then we need to free this one and drop the cache for
5538 * our range.
5539 */
5540 if (em->block_start != EXTENT_MAP_HOLE || em->start != start ||
5541 em->len != len) {
5542 free_extent_map(em);
5543 em = NULL;
5544 insert = true;
5545 btrfs_drop_extent_cache(inode, start, start + len - 1, 0);
5546 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04005547
5548 trans = btrfs_join_transaction(root, 0);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005549 if (IS_ERR(trans))
5550 return ERR_CAST(trans);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005551
5552 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
5553
5554 alloc_hint = get_extent_allocation_hint(inode, start, len);
5555 ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0,
5556 alloc_hint, (u64)-1, &ins, 1);
5557 if (ret) {
5558 em = ERR_PTR(ret);
5559 goto out;
5560 }
5561
Josef Bacik4b46fce2010-05-23 11:00:55 -04005562 if (!em) {
Josef Bacik16d299a2011-04-06 14:53:07 -04005563 em = alloc_extent_map(GFP_NOFS);
5564 if (!em) {
5565 em = ERR_PTR(-ENOMEM);
5566 goto out;
5567 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04005568 }
5569
5570 em->start = start;
5571 em->orig_start = em->start;
5572 em->len = ins.offset;
5573
5574 em->block_start = ins.objectid;
5575 em->block_len = ins.offset;
5576 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik16d299a2011-04-06 14:53:07 -04005577
5578 /*
5579 * We need to do this because if we're using the original em we searched
5580 * for, we could have EXTENT_FLAG_VACANCY set, and we don't want that.
5581 */
5582 em->flags = 0;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005583 set_bit(EXTENT_FLAG_PINNED, &em->flags);
5584
Josef Bacik16d299a2011-04-06 14:53:07 -04005585 while (insert) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04005586 write_lock(&em_tree->lock);
5587 ret = add_extent_mapping(em_tree, em);
5588 write_unlock(&em_tree->lock);
5589 if (ret != -EEXIST)
5590 break;
5591 btrfs_drop_extent_cache(inode, start, start + em->len - 1, 0);
5592 }
5593
5594 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
5595 ins.offset, ins.offset, 0);
5596 if (ret) {
5597 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
5598 em = ERR_PTR(ret);
5599 }
5600out:
5601 btrfs_end_transaction(trans, root);
5602 return em;
5603}
5604
Chris Mason46bfbb52010-05-26 11:04:10 -04005605/*
5606 * returns 1 when the nocow is safe, < 1 on error, 0 if the
5607 * block must be cow'd
5608 */
5609static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans,
5610 struct inode *inode, u64 offset, u64 len)
5611{
5612 struct btrfs_path *path;
5613 int ret;
5614 struct extent_buffer *leaf;
5615 struct btrfs_root *root = BTRFS_I(inode)->root;
5616 struct btrfs_file_extent_item *fi;
5617 struct btrfs_key key;
5618 u64 disk_bytenr;
5619 u64 backref_offset;
5620 u64 extent_end;
5621 u64 num_bytes;
5622 int slot;
5623 int found_type;
5624
5625 path = btrfs_alloc_path();
5626 if (!path)
5627 return -ENOMEM;
5628
Li Zefan33345d012011-04-20 10:31:50 +08005629 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04005630 offset, 0);
5631 if (ret < 0)
5632 goto out;
5633
5634 slot = path->slots[0];
5635 if (ret == 1) {
5636 if (slot == 0) {
5637 /* can't find the item, must cow */
5638 ret = 0;
5639 goto out;
5640 }
5641 slot--;
5642 }
5643 ret = 0;
5644 leaf = path->nodes[0];
5645 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08005646 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04005647 key.type != BTRFS_EXTENT_DATA_KEY) {
5648 /* not our file or wrong item type, must cow */
5649 goto out;
5650 }
5651
5652 if (key.offset > offset) {
5653 /* Wrong offset, must cow */
5654 goto out;
5655 }
5656
5657 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
5658 found_type = btrfs_file_extent_type(leaf, fi);
5659 if (found_type != BTRFS_FILE_EXTENT_REG &&
5660 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
5661 /* not a regular extent, must cow */
5662 goto out;
5663 }
5664 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
5665 backref_offset = btrfs_file_extent_offset(leaf, fi);
5666
5667 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
5668 if (extent_end < offset + len) {
5669 /* extent doesn't include our full range, must cow */
5670 goto out;
5671 }
5672
5673 if (btrfs_extent_readonly(root, disk_bytenr))
5674 goto out;
5675
5676 /*
5677 * look for other files referencing this extent, if we
5678 * find any we must cow
5679 */
Li Zefan33345d012011-04-20 10:31:50 +08005680 if (btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04005681 key.offset - backref_offset, disk_bytenr))
5682 goto out;
5683
5684 /*
5685 * adjust disk_bytenr and num_bytes to cover just the bytes
5686 * in this extent we are about to write. If there
5687 * are any csums in that range we have to cow in order
5688 * to keep the csums correct
5689 */
5690 disk_bytenr += backref_offset;
5691 disk_bytenr += offset - key.offset;
5692 num_bytes = min(offset + len, extent_end) - offset;
5693 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
5694 goto out;
5695 /*
5696 * all of the above have passed, it is safe to overwrite this extent
5697 * without cow
5698 */
5699 ret = 1;
5700out:
5701 btrfs_free_path(path);
5702 return ret;
5703}
5704
Josef Bacik4b46fce2010-05-23 11:00:55 -04005705static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
5706 struct buffer_head *bh_result, int create)
5707{
5708 struct extent_map *em;
5709 struct btrfs_root *root = BTRFS_I(inode)->root;
5710 u64 start = iblock << inode->i_blkbits;
5711 u64 len = bh_result->b_size;
Chris Mason46bfbb52010-05-26 11:04:10 -04005712 struct btrfs_trans_handle *trans;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005713
5714 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
5715 if (IS_ERR(em))
5716 return PTR_ERR(em);
5717
5718 /*
5719 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
5720 * io. INLINE is special, and we could probably kludge it in here, but
5721 * it's still buffered so for safety lets just fall back to the generic
5722 * buffered path.
5723 *
5724 * For COMPRESSED we _have_ to read the entire extent in so we can
5725 * decompress it, so there will be buffering required no matter what we
5726 * do, so go ahead and fallback to buffered.
5727 *
5728 * We return -ENOTBLK because thats what makes DIO go ahead and go back
5729 * to buffered IO. Don't blame me, this is the price we pay for using
5730 * the generic code.
5731 */
5732 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
5733 em->block_start == EXTENT_MAP_INLINE) {
5734 free_extent_map(em);
5735 return -ENOTBLK;
5736 }
5737
5738 /* Just a good old fashioned hole, return */
5739 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
5740 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
5741 free_extent_map(em);
5742 /* DIO will do one hole at a time, so just unlock a sector */
5743 unlock_extent(&BTRFS_I(inode)->io_tree, start,
5744 start + root->sectorsize - 1, GFP_NOFS);
5745 return 0;
5746 }
5747
5748 /*
5749 * We don't allocate a new extent in the following cases
5750 *
5751 * 1) The inode is marked as NODATACOW. In this case we'll just use the
5752 * existing extent.
5753 * 2) The extent is marked as PREALLOC. We're good to go here and can
5754 * just use the extent.
5755 *
5756 */
Chris Mason46bfbb52010-05-26 11:04:10 -04005757 if (!create) {
5758 len = em->len - (start - em->start);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005759 goto map;
Chris Mason46bfbb52010-05-26 11:04:10 -04005760 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04005761
5762 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
5763 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
5764 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04005765 int type;
5766 int ret;
Chris Mason46bfbb52010-05-26 11:04:10 -04005767 u64 block_start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005768
5769 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
5770 type = BTRFS_ORDERED_PREALLOC;
5771 else
5772 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04005773 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04005774 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04005775
5776 /*
5777 * we're not going to log anything, but we do need
5778 * to make sure the current transaction stays open
5779 * while we look for nocow cross refs
5780 */
5781 trans = btrfs_join_transaction(root, 0);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005782 if (IS_ERR(trans))
Chris Mason46bfbb52010-05-26 11:04:10 -04005783 goto must_cow;
5784
5785 if (can_nocow_odirect(trans, inode, start, len) == 1) {
5786 ret = btrfs_add_ordered_extent_dio(inode, start,
5787 block_start, len, len, type);
5788 btrfs_end_transaction(trans, root);
5789 if (ret) {
5790 free_extent_map(em);
5791 return ret;
5792 }
5793 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005794 }
Chris Mason46bfbb52010-05-26 11:04:10 -04005795 btrfs_end_transaction(trans, root);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005796 }
Chris Mason46bfbb52010-05-26 11:04:10 -04005797must_cow:
5798 /*
5799 * this will cow the extent, reset the len in case we changed
5800 * it above
5801 */
5802 len = bh_result->b_size;
Josef Bacik16d299a2011-04-06 14:53:07 -04005803 em = btrfs_new_extent_direct(inode, em, start, len);
Chris Mason46bfbb52010-05-26 11:04:10 -04005804 if (IS_ERR(em))
5805 return PTR_ERR(em);
5806 len = min(len, em->len - (start - em->start));
5807unlock:
Chris Mason4845e442010-05-25 20:56:50 -04005808 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, start + len - 1,
5809 EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DIRTY, 1,
5810 0, NULL, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005811map:
5812 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
5813 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04005814 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005815 bh_result->b_bdev = em->bdev;
5816 set_buffer_mapped(bh_result);
5817 if (create && !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
5818 set_buffer_new(bh_result);
5819
5820 free_extent_map(em);
5821
5822 return 0;
5823}
5824
5825struct btrfs_dio_private {
5826 struct inode *inode;
5827 u64 logical_offset;
5828 u64 disk_bytenr;
5829 u64 bytes;
5830 u32 *csums;
5831 void *private;
Miao Xiee65e1532010-11-22 03:04:43 +00005832
5833 /* number of bios pending for this dio */
5834 atomic_t pending_bios;
5835
5836 /* IO errors */
5837 int errors;
5838
5839 struct bio *orig_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005840};
5841
5842static void btrfs_endio_direct_read(struct bio *bio, int err)
5843{
Miao Xiee65e1532010-11-22 03:04:43 +00005844 struct btrfs_dio_private *dip = bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005845 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
5846 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005847 struct inode *inode = dip->inode;
5848 struct btrfs_root *root = BTRFS_I(inode)->root;
5849 u64 start;
5850 u32 *private = dip->csums;
5851
5852 start = dip->logical_offset;
5853 do {
5854 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
5855 struct page *page = bvec->bv_page;
5856 char *kaddr;
5857 u32 csum = ~(u32)0;
5858 unsigned long flags;
5859
5860 local_irq_save(flags);
5861 kaddr = kmap_atomic(page, KM_IRQ0);
5862 csum = btrfs_csum_data(root, kaddr + bvec->bv_offset,
5863 csum, bvec->bv_len);
5864 btrfs_csum_final(csum, (char *)&csum);
5865 kunmap_atomic(kaddr, KM_IRQ0);
5866 local_irq_restore(flags);
5867
5868 flush_dcache_page(bvec->bv_page);
5869 if (csum != *private) {
Li Zefan33345d012011-04-20 10:31:50 +08005870 printk(KERN_ERR "btrfs csum failed ino %llu off"
Josef Bacik4b46fce2010-05-23 11:00:55 -04005871 " %llu csum %u private %u\n",
Li Zefan33345d012011-04-20 10:31:50 +08005872 (unsigned long long)btrfs_ino(inode),
5873 (unsigned long long)start,
Josef Bacik4b46fce2010-05-23 11:00:55 -04005874 csum, *private);
5875 err = -EIO;
5876 }
5877 }
5878
5879 start += bvec->bv_len;
5880 private++;
5881 bvec++;
5882 } while (bvec <= bvec_end);
5883
5884 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
5885 dip->logical_offset + dip->bytes - 1, GFP_NOFS);
5886 bio->bi_private = dip->private;
5887
5888 kfree(dip->csums);
5889 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04005890
5891 /* If we had a csum failure make sure to clear the uptodate flag */
5892 if (err)
5893 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005894 dio_end_io(bio, err);
5895}
5896
5897static void btrfs_endio_direct_write(struct bio *bio, int err)
5898{
5899 struct btrfs_dio_private *dip = bio->bi_private;
5900 struct inode *inode = dip->inode;
5901 struct btrfs_root *root = BTRFS_I(inode)->root;
5902 struct btrfs_trans_handle *trans;
5903 struct btrfs_ordered_extent *ordered = NULL;
5904 struct extent_state *cached_state = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05005905 u64 ordered_offset = dip->logical_offset;
5906 u64 ordered_bytes = dip->bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005907 int ret;
5908
5909 if (err)
5910 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05005911again:
5912 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
5913 &ordered_offset,
5914 ordered_bytes);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005915 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05005916 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005917
5918 BUG_ON(!ordered);
5919
5920 trans = btrfs_join_transaction(root, 1);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005921 if (IS_ERR(trans)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04005922 err = -ENOMEM;
5923 goto out;
5924 }
5925 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
5926
5927 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags)) {
5928 ret = btrfs_ordered_update_i_size(inode, 0, ordered);
5929 if (!ret)
5930 ret = btrfs_update_inode(trans, root, inode);
5931 err = ret;
5932 goto out;
5933 }
5934
5935 lock_extent_bits(&BTRFS_I(inode)->io_tree, ordered->file_offset,
5936 ordered->file_offset + ordered->len - 1, 0,
5937 &cached_state, GFP_NOFS);
5938
5939 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags)) {
5940 ret = btrfs_mark_extent_written(trans, inode,
5941 ordered->file_offset,
5942 ordered->file_offset +
5943 ordered->len);
5944 if (ret) {
5945 err = ret;
5946 goto out_unlock;
5947 }
5948 } else {
5949 ret = insert_reserved_file_extent(trans, inode,
5950 ordered->file_offset,
5951 ordered->start,
5952 ordered->disk_len,
5953 ordered->len,
5954 ordered->len,
5955 0, 0, 0,
5956 BTRFS_FILE_EXTENT_REG);
5957 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
5958 ordered->file_offset, ordered->len);
5959 if (ret) {
5960 err = ret;
5961 WARN_ON(1);
5962 goto out_unlock;
5963 }
5964 }
5965
5966 add_pending_csums(trans, inode, ordered->file_offset, &ordered->list);
Josef Bacik1ef30be2011-04-05 19:25:36 -04005967 ret = btrfs_ordered_update_i_size(inode, 0, ordered);
5968 if (!ret)
5969 btrfs_update_inode(trans, root, inode);
5970 ret = 0;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005971out_unlock:
5972 unlock_extent_cached(&BTRFS_I(inode)->io_tree, ordered->file_offset,
5973 ordered->file_offset + ordered->len - 1,
5974 &cached_state, GFP_NOFS);
5975out:
5976 btrfs_delalloc_release_metadata(inode, ordered->len);
5977 btrfs_end_transaction(trans, root);
Chris Mason163cf092010-11-28 19:56:33 -05005978 ordered_offset = ordered->file_offset + ordered->len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005979 btrfs_put_ordered_extent(ordered);
5980 btrfs_put_ordered_extent(ordered);
Chris Mason163cf092010-11-28 19:56:33 -05005981
5982out_test:
5983 /*
5984 * our bio might span multiple ordered extents. If we haven't
5985 * completed the accounting for the whole dio, go back and try again
5986 */
5987 if (ordered_offset < dip->logical_offset + dip->bytes) {
5988 ordered_bytes = dip->logical_offset + dip->bytes -
5989 ordered_offset;
5990 goto again;
5991 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04005992out_done:
5993 bio->bi_private = dip->private;
5994
5995 kfree(dip->csums);
5996 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04005997
5998 /* If we had an error make sure to clear the uptodate flag */
5999 if (err)
6000 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006001 dio_end_io(bio, err);
6002}
6003
Chris Masoneaf25d92010-05-25 09:48:28 -04006004static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
6005 struct bio *bio, int mirror_num,
6006 unsigned long bio_flags, u64 offset)
6007{
6008 int ret;
6009 struct btrfs_root *root = BTRFS_I(inode)->root;
6010 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
6011 BUG_ON(ret);
6012 return 0;
6013}
6014
Miao Xiee65e1532010-11-22 03:04:43 +00006015static void btrfs_end_dio_bio(struct bio *bio, int err)
6016{
6017 struct btrfs_dio_private *dip = bio->bi_private;
6018
6019 if (err) {
Li Zefan33345d012011-04-20 10:31:50 +08006020 printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu "
Jan Beulich3dd14622010-12-07 14:54:09 +00006021 "sector %#Lx len %u err no %d\n",
Li Zefan33345d012011-04-20 10:31:50 +08006022 (unsigned long long)btrfs_ino(dip->inode), bio->bi_rw,
Jan Beulich3dd14622010-12-07 14:54:09 +00006023 (unsigned long long)bio->bi_sector, bio->bi_size, err);
Miao Xiee65e1532010-11-22 03:04:43 +00006024 dip->errors = 1;
6025
6026 /*
6027 * before atomic variable goto zero, we must make sure
6028 * dip->errors is perceived to be set.
6029 */
6030 smp_mb__before_atomic_dec();
6031 }
6032
6033 /* if there are more bios still pending for this dio, just exit */
6034 if (!atomic_dec_and_test(&dip->pending_bios))
6035 goto out;
6036
6037 if (dip->errors)
6038 bio_io_error(dip->orig_bio);
6039 else {
6040 set_bit(BIO_UPTODATE, &dip->orig_bio->bi_flags);
6041 bio_endio(dip->orig_bio, 0);
6042 }
6043out:
6044 bio_put(bio);
6045}
6046
6047static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
6048 u64 first_sector, gfp_t gfp_flags)
6049{
6050 int nr_vecs = bio_get_nr_vecs(bdev);
6051 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
6052}
6053
6054static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
6055 int rw, u64 file_offset, int skip_sum,
Josef Bacik1ae39932011-04-06 14:41:34 -04006056 u32 *csums, int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00006057{
6058 int write = rw & REQ_WRITE;
6059 struct btrfs_root *root = BTRFS_I(inode)->root;
6060 int ret;
6061
6062 bio_get(bio);
6063 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
6064 if (ret)
6065 goto err;
6066
Josef Bacik1ae39932011-04-06 14:41:34 -04006067 if (skip_sum)
6068 goto map;
6069
6070 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00006071 ret = btrfs_wq_submit_bio(root->fs_info,
6072 inode, rw, bio, 0, 0,
6073 file_offset,
6074 __btrfs_submit_bio_start_direct_io,
6075 __btrfs_submit_bio_done);
6076 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04006077 } else if (write) {
6078 /*
6079 * If we aren't doing async submit, calculate the csum of the
6080 * bio now.
6081 */
6082 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
6083 if (ret)
6084 goto err;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00006085 } else if (!skip_sum) {
6086 ret = btrfs_lookup_bio_sums_dio(root, inode, bio,
Miao Xiee65e1532010-11-22 03:04:43 +00006087 file_offset, csums);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00006088 if (ret)
6089 goto err;
6090 }
Miao Xiee65e1532010-11-22 03:04:43 +00006091
Josef Bacik1ae39932011-04-06 14:41:34 -04006092map:
6093 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006094err:
6095 bio_put(bio);
6096 return ret;
6097}
6098
6099static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
6100 int skip_sum)
6101{
6102 struct inode *inode = dip->inode;
6103 struct btrfs_root *root = BTRFS_I(inode)->root;
6104 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
6105 struct bio *bio;
6106 struct bio *orig_bio = dip->orig_bio;
6107 struct bio_vec *bvec = orig_bio->bi_io_vec;
6108 u64 start_sector = orig_bio->bi_sector;
6109 u64 file_offset = dip->logical_offset;
6110 u64 submit_len = 0;
6111 u64 map_length;
6112 int nr_pages = 0;
6113 u32 *csums = dip->csums;
6114 int ret = 0;
Josef Bacik1ae39932011-04-06 14:41:34 -04006115 int async_submit = 0;
Josef Bacik98bc3142011-03-22 11:00:46 -04006116 int write = rw & REQ_WRITE;
Miao Xiee65e1532010-11-22 03:04:43 +00006117
Miao Xiee65e1532010-11-22 03:04:43 +00006118 map_length = orig_bio->bi_size;
6119 ret = btrfs_map_block(map_tree, READ, start_sector << 9,
6120 &map_length, NULL, 0);
6121 if (ret) {
Josef Bacik64728bb2011-04-25 19:43:52 -04006122 bio_put(orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00006123 return -EIO;
6124 }
6125
Josef Bacik02f57c72011-04-06 14:25:44 -04006126 if (map_length >= orig_bio->bi_size) {
6127 bio = orig_bio;
6128 goto submit;
6129 }
6130
Josef Bacik1ae39932011-04-06 14:41:34 -04006131 async_submit = 1;
Josef Bacik02f57c72011-04-06 14:25:44 -04006132 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
6133 if (!bio)
6134 return -ENOMEM;
6135 bio->bi_private = dip;
6136 bio->bi_end_io = btrfs_end_dio_bio;
6137 atomic_inc(&dip->pending_bios);
6138
Miao Xiee65e1532010-11-22 03:04:43 +00006139 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
6140 if (unlikely(map_length < submit_len + bvec->bv_len ||
6141 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
6142 bvec->bv_offset) < bvec->bv_len)) {
6143 /*
6144 * inc the count before we submit the bio so
6145 * we know the end IO handler won't happen before
6146 * we inc the count. Otherwise, the dip might get freed
6147 * before we're done setting it up
6148 */
6149 atomic_inc(&dip->pending_bios);
6150 ret = __btrfs_submit_dio_bio(bio, inode, rw,
6151 file_offset, skip_sum,
Josef Bacik1ae39932011-04-06 14:41:34 -04006152 csums, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006153 if (ret) {
6154 bio_put(bio);
6155 atomic_dec(&dip->pending_bios);
6156 goto out_err;
6157 }
6158
Josef Bacik98bc3142011-03-22 11:00:46 -04006159 /* Write's use the ordered csums */
6160 if (!write && !skip_sum)
Miao Xiee65e1532010-11-22 03:04:43 +00006161 csums = csums + nr_pages;
6162 start_sector += submit_len >> 9;
6163 file_offset += submit_len;
6164
6165 submit_len = 0;
6166 nr_pages = 0;
6167
6168 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
6169 start_sector, GFP_NOFS);
6170 if (!bio)
6171 goto out_err;
6172 bio->bi_private = dip;
6173 bio->bi_end_io = btrfs_end_dio_bio;
6174
6175 map_length = orig_bio->bi_size;
6176 ret = btrfs_map_block(map_tree, READ, start_sector << 9,
6177 &map_length, NULL, 0);
6178 if (ret) {
6179 bio_put(bio);
6180 goto out_err;
6181 }
6182 } else {
6183 submit_len += bvec->bv_len;
6184 nr_pages ++;
6185 bvec++;
6186 }
6187 }
6188
Josef Bacik02f57c72011-04-06 14:25:44 -04006189submit:
Miao Xiee65e1532010-11-22 03:04:43 +00006190 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacik1ae39932011-04-06 14:41:34 -04006191 csums, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006192 if (!ret)
6193 return 0;
6194
6195 bio_put(bio);
6196out_err:
6197 dip->errors = 1;
6198 /*
6199 * before atomic variable goto zero, we must
6200 * make sure dip->errors is perceived to be set.
6201 */
6202 smp_mb__before_atomic_dec();
6203 if (atomic_dec_and_test(&dip->pending_bios))
6204 bio_io_error(dip->orig_bio);
6205
6206 /* bio_end_io() will handle error, so we needn't return it */
6207 return 0;
6208}
6209
Josef Bacik4b46fce2010-05-23 11:00:55 -04006210static void btrfs_submit_direct(int rw, struct bio *bio, struct inode *inode,
6211 loff_t file_offset)
6212{
6213 struct btrfs_root *root = BTRFS_I(inode)->root;
6214 struct btrfs_dio_private *dip;
6215 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006216 int skip_sum;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02006217 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006218 int ret = 0;
6219
6220 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
6221
6222 dip = kmalloc(sizeof(*dip), GFP_NOFS);
6223 if (!dip) {
6224 ret = -ENOMEM;
6225 goto free_ordered;
6226 }
6227 dip->csums = NULL;
6228
Josef Bacik98bc3142011-03-22 11:00:46 -04006229 /* Write's use the ordered csum stuff, so we don't need dip->csums */
6230 if (!write && !skip_sum) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04006231 dip->csums = kmalloc(sizeof(u32) * bio->bi_vcnt, GFP_NOFS);
6232 if (!dip->csums) {
Daniel J Bluemanb4966b72011-03-09 16:46:42 +00006233 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006234 ret = -ENOMEM;
6235 goto free_ordered;
6236 }
6237 }
6238
6239 dip->private = bio->bi_private;
6240 dip->inode = inode;
6241 dip->logical_offset = file_offset;
6242
Josef Bacik4b46fce2010-05-23 11:00:55 -04006243 dip->bytes = 0;
6244 do {
6245 dip->bytes += bvec->bv_len;
6246 bvec++;
6247 } while (bvec <= (bio->bi_io_vec + bio->bi_vcnt - 1));
6248
Chris Mason46bfbb52010-05-26 11:04:10 -04006249 dip->disk_bytenr = (u64)bio->bi_sector << 9;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006250 bio->bi_private = dip;
Miao Xiee65e1532010-11-22 03:04:43 +00006251 dip->errors = 0;
6252 dip->orig_bio = bio;
6253 atomic_set(&dip->pending_bios, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006254
6255 if (write)
6256 bio->bi_end_io = btrfs_endio_direct_write;
6257 else
6258 bio->bi_end_io = btrfs_endio_direct_read;
6259
Miao Xiee65e1532010-11-22 03:04:43 +00006260 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
6261 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04006262 return;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006263free_ordered:
6264 /*
6265 * If this is a write, we need to clean up the reserved space and kill
6266 * the ordered extent.
6267 */
6268 if (write) {
6269 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05006270 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006271 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
6272 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
6273 btrfs_free_reserved_extent(root, ordered->start,
6274 ordered->disk_len);
6275 btrfs_put_ordered_extent(ordered);
6276 btrfs_put_ordered_extent(ordered);
6277 }
6278 bio_endio(bio, ret);
6279}
6280
Chris Mason5a5f79b2010-05-26 21:33:37 -04006281static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
6282 const struct iovec *iov, loff_t offset,
6283 unsigned long nr_segs)
6284{
6285 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00006286 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04006287 size_t size;
6288 unsigned long addr;
6289 unsigned blocksize_mask = root->sectorsize - 1;
6290 ssize_t retval = -EINVAL;
6291 loff_t end = offset;
6292
6293 if (offset & blocksize_mask)
6294 goto out;
6295
6296 /* Check the memory alignment. Blocks cannot straddle pages */
6297 for (seg = 0; seg < nr_segs; seg++) {
6298 addr = (unsigned long)iov[seg].iov_base;
6299 size = iov[seg].iov_len;
6300 end += size;
Josef Bacika1b75f72011-04-08 15:51:18 +00006301 if ((addr & blocksize_mask) || (size & blocksize_mask))
Chris Mason5a5f79b2010-05-26 21:33:37 -04006302 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00006303
6304 /* If this is a write we don't need to check anymore */
6305 if (rw & WRITE)
6306 continue;
6307
6308 /*
6309 * Check to make sure we don't have duplicate iov_base's in this
6310 * iovec, if so return EINVAL, otherwise we'll get csum errors
6311 * when reading back.
6312 */
6313 for (i = seg + 1; i < nr_segs; i++) {
6314 if (iov[seg].iov_base == iov[i].iov_base)
6315 goto out;
6316 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04006317 }
6318 retval = 0;
6319out:
6320 return retval;
6321}
Chris Mason16432982008-04-10 10:23:21 -04006322static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
6323 const struct iovec *iov, loff_t offset,
6324 unsigned long nr_segs)
6325{
Josef Bacik4b46fce2010-05-23 11:00:55 -04006326 struct file *file = iocb->ki_filp;
6327 struct inode *inode = file->f_mapping->host;
6328 struct btrfs_ordered_extent *ordered;
Chris Mason4845e442010-05-25 20:56:50 -04006329 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006330 u64 lockstart, lockend;
6331 ssize_t ret;
Chris Mason4845e442010-05-25 20:56:50 -04006332 int writing = rw & WRITE;
6333 int write_bits = 0;
Chris Mason3f7c5792010-05-26 10:59:53 -04006334 size_t count = iov_length(iov, nr_segs);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006335
Chris Mason5a5f79b2010-05-26 21:33:37 -04006336 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
6337 offset, nr_segs)) {
6338 return 0;
6339 }
6340
Josef Bacik4b46fce2010-05-23 11:00:55 -04006341 lockstart = offset;
Chris Mason3f7c5792010-05-26 10:59:53 -04006342 lockend = offset + count - 1;
6343
6344 if (writing) {
6345 ret = btrfs_delalloc_reserve_space(inode, count);
6346 if (ret)
6347 goto out;
6348 }
Chris Mason4845e442010-05-25 20:56:50 -04006349
Josef Bacik4b46fce2010-05-23 11:00:55 -04006350 while (1) {
Chris Mason4845e442010-05-25 20:56:50 -04006351 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6352 0, &cached_state, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006353 /*
6354 * We're concerned with the entire range that we're going to be
6355 * doing DIO to, so we need to make sure theres no ordered
6356 * extents in this range.
6357 */
6358 ordered = btrfs_lookup_ordered_range(inode, lockstart,
6359 lockend - lockstart + 1);
6360 if (!ordered)
6361 break;
Chris Mason4845e442010-05-25 20:56:50 -04006362 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6363 &cached_state, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006364 btrfs_start_ordered_extent(inode, ordered, 1);
6365 btrfs_put_ordered_extent(ordered);
6366 cond_resched();
6367 }
6368
Chris Mason4845e442010-05-25 20:56:50 -04006369 /*
6370 * we don't use btrfs_set_extent_delalloc because we don't want
6371 * the dirty or uptodate bits
6372 */
6373 if (writing) {
6374 write_bits = EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING;
6375 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6376 EXTENT_DELALLOC, 0, NULL, &cached_state,
6377 GFP_NOFS);
6378 if (ret) {
6379 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6380 lockend, EXTENT_LOCKED | write_bits,
6381 1, 0, &cached_state, GFP_NOFS);
6382 goto out;
6383 }
6384 }
6385
6386 free_extent_state(cached_state);
6387 cached_state = NULL;
6388
Chris Mason5a5f79b2010-05-26 21:33:37 -04006389 ret = __blockdev_direct_IO(rw, iocb, inode,
6390 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
6391 iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
6392 btrfs_submit_direct, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006393
6394 if (ret < 0 && ret != -EIOCBQUEUED) {
Chris Mason4845e442010-05-25 20:56:50 -04006395 clear_extent_bit(&BTRFS_I(inode)->io_tree, offset,
6396 offset + iov_length(iov, nr_segs) - 1,
6397 EXTENT_LOCKED | write_bits, 1, 0,
6398 &cached_state, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006399 } else if (ret >= 0 && ret < iov_length(iov, nr_segs)) {
6400 /*
6401 * We're falling back to buffered, unlock the section we didn't
6402 * do IO on.
6403 */
Chris Mason4845e442010-05-25 20:56:50 -04006404 clear_extent_bit(&BTRFS_I(inode)->io_tree, offset + ret,
6405 offset + iov_length(iov, nr_segs) - 1,
6406 EXTENT_LOCKED | write_bits, 1, 0,
6407 &cached_state, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006408 }
Chris Mason4845e442010-05-25 20:56:50 -04006409out:
6410 free_extent_state(cached_state);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006411 return ret;
Chris Mason16432982008-04-10 10:23:21 -04006412}
6413
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05006414static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
6415 __u64 start, __u64 len)
6416{
Chris Masonec29ed52011-02-23 16:23:20 -05006417 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05006418}
6419
Chris Mason9ebefb182007-06-15 13:50:00 -04006420int btrfs_readpage(struct file *file, struct page *page)
6421{
Chris Masond1310b22008-01-24 16:13:08 -05006422 struct extent_io_tree *tree;
6423 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006424 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04006425}
Chris Mason1832a6d2007-12-21 16:27:21 -05006426
Chris Mason39279cc2007-06-12 06:35:45 -04006427static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
6428{
Chris Masond1310b22008-01-24 16:13:08 -05006429 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04006430
6431
6432 if (current->flags & PF_MEMALLOC) {
6433 redirty_page_for_writepage(wbc, page);
6434 unlock_page(page);
6435 return 0;
6436 }
Chris Masond1310b22008-01-24 16:13:08 -05006437 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006438 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
6439}
Chris Mason39279cc2007-06-12 06:35:45 -04006440
Chris Masonf4219502008-07-22 11:18:09 -04006441int btrfs_writepages(struct address_space *mapping,
6442 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04006443{
Chris Masond1310b22008-01-24 16:13:08 -05006444 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05006445
Chris Masond1310b22008-01-24 16:13:08 -05006446 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04006447 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
6448}
6449
Chris Mason3ab2fb52007-11-08 10:59:22 -05006450static int
6451btrfs_readpages(struct file *file, struct address_space *mapping,
6452 struct list_head *pages, unsigned nr_pages)
6453{
Chris Masond1310b22008-01-24 16:13:08 -05006454 struct extent_io_tree *tree;
6455 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05006456 return extent_readpages(tree, mapping, pages, nr_pages,
6457 btrfs_get_extent);
6458}
Chris Masone6dcd2d2008-07-17 12:53:50 -04006459static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04006460{
Chris Masond1310b22008-01-24 16:13:08 -05006461 struct extent_io_tree *tree;
6462 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04006463 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006464
Chris Masond1310b22008-01-24 16:13:08 -05006465 tree = &BTRFS_I(page->mapping->host)->io_tree;
6466 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05006467 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006468 if (ret == 1) {
6469 ClearPagePrivate(page);
6470 set_page_private(page, 0);
6471 page_cache_release(page);
6472 }
6473 return ret;
6474}
Chris Mason39279cc2007-06-12 06:35:45 -04006475
Chris Masone6dcd2d2008-07-17 12:53:50 -04006476static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
6477{
Chris Mason98509cf2008-09-11 15:51:43 -04006478 if (PageWriteback(page) || PageDirty(page))
6479 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05006480 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006481}
6482
Chris Masona52d9a82007-08-27 16:49:44 -04006483static void btrfs_invalidatepage(struct page *page, unsigned long offset)
6484{
Chris Masond1310b22008-01-24 16:13:08 -05006485 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006486 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00006487 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006488 u64 page_start = page_offset(page);
6489 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04006490
Chris Mason8b62b722009-09-02 16:53:46 -04006491
6492 /*
6493 * we have the page locked, so new writeback can't start,
6494 * and the dirty bit won't be cleared while we are here.
6495 *
6496 * Wait for IO on this page so that we can safely clear
6497 * the PagePrivate2 bit and do ordered accounting
6498 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006499 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04006500
Chris Masond1310b22008-01-24 16:13:08 -05006501 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006502 if (offset) {
6503 btrfs_releasepage(page, GFP_NOFS);
6504 return;
6505 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00006506 lock_extent_bits(tree, page_start, page_end, 0, &cached_state,
6507 GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006508 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
6509 page_offset(page));
6510 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04006511 /*
6512 * IO on this page will never be started, so we need
6513 * to account for any ordered extents now
6514 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006515 clear_extent_bit(tree, page_start, page_end,
6516 EXTENT_DIRTY | EXTENT_DELALLOC |
Josef Bacik32c00af2009-10-08 13:34:05 -04006517 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING, 1, 0,
Josef Bacik2ac55d42010-02-03 19:33:23 +00006518 &cached_state, GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04006519 /*
6520 * whoever cleared the private bit is responsible
6521 * for the finish_ordered_io
6522 */
6523 if (TestClearPagePrivate2(page)) {
6524 btrfs_finish_ordered_io(page->mapping->host,
6525 page_start, page_end);
6526 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006527 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +00006528 cached_state = NULL;
6529 lock_extent_bits(tree, page_start, page_end, 0, &cached_state,
6530 GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006531 }
6532 clear_extent_bit(tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04006533 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
Josef Bacik2ac55d42010-02-03 19:33:23 +00006534 EXTENT_DO_ACCOUNTING, 1, 1, &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006535 __btrfs_releasepage(page, GFP_NOFS);
6536
Chris Mason4a096752008-07-21 10:29:44 -04006537 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04006538 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04006539 ClearPagePrivate(page);
6540 set_page_private(page, 0);
6541 page_cache_release(page);
6542 }
Chris Mason39279cc2007-06-12 06:35:45 -04006543}
6544
Chris Mason9ebefb182007-06-15 13:50:00 -04006545/*
6546 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
6547 * called from a page fault handler when a page is first dirtied. Hence we must
6548 * be careful to check for EOF conditions here. We set the page up correctly
6549 * for a written page which means we get ENOSPC checking when writing into
6550 * holes and correct delalloc and unwritten extent mapping on filesystems that
6551 * support these features.
6552 *
6553 * We are not allowed to take the i_mutex here so we have to play games to
6554 * protect against truncate races as the page could now be beyond EOF. Because
6555 * vmtruncate() writes the inode size before removing pages, once we have the
6556 * page lock we can determine safely if the page is beyond EOF. If it is not
6557 * beyond EOF, then the page is guaranteed safe against truncation until we
6558 * unlock the page.
6559 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07006560int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04006561{
Nick Pigginc2ec1752009-03-31 15:23:21 -07006562 struct page *page = vmf->page;
Chris Mason6da6aba2007-12-18 16:15:09 -05006563 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05006564 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006565 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
6566 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00006567 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006568 char *kaddr;
6569 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04006570 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05006571 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04006572 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006573 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04006574
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006575 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Nick Piggin56a76f82009-03-31 15:23:23 -07006576 if (ret) {
6577 if (ret == -ENOMEM)
6578 ret = VM_FAULT_OOM;
6579 else /* -ENOSPC, -EIO, etc */
6580 ret = VM_FAULT_SIGBUS;
Chris Mason1832a6d2007-12-21 16:27:21 -05006581 goto out;
Nick Piggin56a76f82009-03-31 15:23:23 -07006582 }
Chris Mason1832a6d2007-12-21 16:27:21 -05006583
Nick Piggin56a76f82009-03-31 15:23:23 -07006584 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006585again:
Chris Mason9ebefb182007-06-15 13:50:00 -04006586 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04006587 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006588 page_start = page_offset(page);
6589 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04006590
Chris Mason9ebefb182007-06-15 13:50:00 -04006591 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04006592 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04006593 /* page got truncated out from underneath us */
6594 goto out_unlock;
6595 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006596 wait_on_page_writeback(page);
6597
Josef Bacik2ac55d42010-02-03 19:33:23 +00006598 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state,
6599 GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006600 set_page_extent_mapped(page);
6601
Chris Masoneb84ae02008-07-17 13:53:27 -04006602 /*
6603 * we can't set the delalloc bits if there are pending ordered
6604 * extents. Drop our locks and wait for them to finish
6605 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006606 ordered = btrfs_lookup_ordered_extent(inode, page_start);
6607 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00006608 unlock_extent_cached(io_tree, page_start, page_end,
6609 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006610 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04006611 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006612 btrfs_put_ordered_extent(ordered);
6613 goto again;
6614 }
6615
Josef Bacikfbf19082009-10-01 17:10:23 -04006616 /*
6617 * XXX - page_mkwrite gets called every time the page is dirtied, even
6618 * if it was already dirty, so for space accounting reasons we need to
6619 * clear any delalloc bits for the range we are fixing to save. There
6620 * is probably a better way to do this, but for now keep consistent with
6621 * prepare_pages in the normal write path.
6622 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00006623 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04006624 EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00006625 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04006626
Josef Bacik2ac55d42010-02-03 19:33:23 +00006627 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
6628 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006629 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00006630 unlock_extent_cached(io_tree, page_start, page_end,
6631 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006632 ret = VM_FAULT_SIGBUS;
6633 goto out_unlock;
6634 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006635 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04006636
6637 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04006638 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04006639 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04006640 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04006641 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04006642
Chris Masone6dcd2d2008-07-17 12:53:50 -04006643 if (zero_start != PAGE_CACHE_SIZE) {
6644 kaddr = kmap(page);
6645 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
6646 flush_dcache_page(page);
6647 kunmap(page);
6648 }
Chris Mason247e7432008-07-17 12:53:51 -04006649 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006650 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04006651 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04006652
Chris Mason257c62e2009-10-13 13:21:08 -04006653 BTRFS_I(inode)->last_trans = root->fs_info->generation;
6654 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
6655
Josef Bacik2ac55d42010-02-03 19:33:23 +00006656 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04006657
6658out_unlock:
Chris Mason50a9b212009-09-11 12:33:12 -04006659 if (!ret)
6660 return VM_FAULT_LOCKED;
Chris Mason9ebefb182007-06-15 13:50:00 -04006661 unlock_page(page);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006662 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason1832a6d2007-12-21 16:27:21 -05006663out:
Chris Mason9ebefb182007-06-15 13:50:00 -04006664 return ret;
6665}
6666
Josef Bacika41ad392011-01-31 15:30:16 -05006667static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006668{
6669 struct btrfs_root *root = BTRFS_I(inode)->root;
6670 int ret;
Josef Bacik3893e332011-01-31 16:03:11 -05006671 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006672 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04006673 unsigned long nr;
Chris Masondbe674a2008-07-17 12:54:05 -04006674 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04006675
Josef Bacik5d5e1032009-10-13 16:46:49 -04006676 ret = btrfs_truncate_page(inode->i_mapping, inode->i_size);
6677 if (ret)
Josef Bacika41ad392011-01-31 15:30:16 -05006678 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00006679
Chris Mason4a096752008-07-21 10:29:44 -04006680 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Yan, Zheng80825102009-11-12 09:35:36 +00006681 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04006682
Josef Bacikf0cd8462011-03-04 14:37:08 -05006683 trans = btrfs_start_transaction(root, 5);
6684 if (IS_ERR(trans))
6685 return PTR_ERR(trans);
6686
6687 btrfs_set_trans_block_group(trans, inode);
6688
6689 ret = btrfs_orphan_add(trans, inode);
6690 if (ret) {
6691 btrfs_end_transaction(trans, root);
6692 return ret;
6693 }
6694
6695 nr = trans->blocks_used;
6696 btrfs_end_transaction(trans, root);
6697 btrfs_btree_balance_dirty(root, nr);
6698
6699 /* Now start a transaction for the truncate */
Yan, Zhengd68fc572010-05-16 10:49:58 -04006700 trans = btrfs_start_transaction(root, 0);
Josef Bacik3893e332011-01-31 16:03:11 -05006701 if (IS_ERR(trans))
6702 return PTR_ERR(trans);
Yan, Zheng80825102009-11-12 09:35:36 +00006703 btrfs_set_trans_block_group(trans, inode);
Yan, Zhengd68fc572010-05-16 10:49:58 -04006704 trans->block_rsv = root->orphan_block_rsv;
Chris Mason5a3f23d2009-03-31 13:27:11 -04006705
6706 /*
6707 * setattr is responsible for setting the ordered_data_close flag,
6708 * but that is only tested during the last file release. That
6709 * could happen well after the next commit, leaving a great big
6710 * window where new writes may get lost if someone chooses to write
6711 * to this file after truncating to zero
6712 *
6713 * The inode doesn't have any dirty data here, and so if we commit
6714 * this is a noop. If someone immediately starts writing to the inode
6715 * it is very likely we'll catch some of their writes in this
6716 * transaction, and the commit will find this file on the ordered
6717 * data list with good things to send down.
6718 *
6719 * This is a best effort solution, there is still a window where
6720 * using truncate to replace the contents of the file will
6721 * end up with a zero length file after a crash.
6722 */
6723 if (inode->i_size == 0 && BTRFS_I(inode)->ordered_data_close)
6724 btrfs_add_ordered_operation(trans, root, inode);
6725
Yan, Zheng80825102009-11-12 09:35:36 +00006726 while (1) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04006727 if (!trans) {
6728 trans = btrfs_start_transaction(root, 0);
Josef Bacik3893e332011-01-31 16:03:11 -05006729 if (IS_ERR(trans))
6730 return PTR_ERR(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04006731 btrfs_set_trans_block_group(trans, inode);
6732 trans->block_rsv = root->orphan_block_rsv;
6733 }
6734
6735 ret = btrfs_block_rsv_check(trans, root,
6736 root->orphan_block_rsv, 0, 5);
Josef Bacik3893e332011-01-31 16:03:11 -05006737 if (ret == -EAGAIN) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04006738 ret = btrfs_commit_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05006739 if (ret)
6740 return ret;
Yan, Zhengd68fc572010-05-16 10:49:58 -04006741 trans = NULL;
6742 continue;
Josef Bacik3893e332011-01-31 16:03:11 -05006743 } else if (ret) {
6744 err = ret;
6745 break;
Yan, Zhengd68fc572010-05-16 10:49:58 -04006746 }
6747
Yan, Zheng80825102009-11-12 09:35:36 +00006748 ret = btrfs_truncate_inode_items(trans, root, inode,
6749 inode->i_size,
6750 BTRFS_EXTENT_DATA_KEY);
Josef Bacik3893e332011-01-31 16:03:11 -05006751 if (ret != -EAGAIN) {
6752 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00006753 break;
Josef Bacik3893e332011-01-31 16:03:11 -05006754 }
Chris Mason39279cc2007-06-12 06:35:45 -04006755
Yan, Zheng80825102009-11-12 09:35:36 +00006756 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05006757 if (ret) {
6758 err = ret;
6759 break;
6760 }
Chris Mason5f39d392007-10-15 16:14:19 -04006761
Yan, Zheng80825102009-11-12 09:35:36 +00006762 nr = trans->blocks_used;
6763 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04006764 trans = NULL;
Yan, Zheng80825102009-11-12 09:35:36 +00006765 btrfs_btree_balance_dirty(root, nr);
Yan, Zheng80825102009-11-12 09:35:36 +00006766 }
6767
6768 if (ret == 0 && inode->i_nlink > 0) {
6769 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05006770 if (ret)
6771 err = ret;
Josef Bacikded5db92011-03-04 14:09:46 -05006772 } else if (ret && inode->i_nlink > 0) {
6773 /*
6774 * Failed to do the truncate, remove us from the in memory
6775 * orphan list.
6776 */
6777 ret = btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00006778 }
6779
6780 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05006781 if (ret && !err)
6782 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04006783
Josef Bacik7b128762008-07-24 12:17:14 -04006784 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04006785 ret = btrfs_end_transaction_throttle(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05006786 if (ret && !err)
6787 err = ret;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04006788 btrfs_btree_balance_dirty(root, nr);
Josef Bacika41ad392011-01-31 15:30:16 -05006789
Josef Bacik3893e332011-01-31 16:03:11 -05006790 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04006791}
6792
Sven Wegener3b963622008-06-09 21:57:42 -04006793/*
Chris Masond352ac62008-09-29 15:18:18 -04006794 * create a new subvolume directory/inode (helper for the ioctl).
6795 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05006796int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Yan, Zheng76dda932009-09-21 16:00:26 -04006797 struct btrfs_root *new_root,
Yan Zhengd2fb3432008-12-11 16:30:39 -05006798 u64 new_dirid, u64 alloc_hint)
Chris Mason39279cc2007-06-12 06:35:45 -04006799{
Chris Mason39279cc2007-06-12 06:35:45 -04006800 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04006801 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006802 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006803
Josef Bacikaec74772008-07-24 12:12:38 -04006804 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
Yan Zhengd2fb3432008-12-11 16:30:39 -05006805 new_dirid, alloc_hint, S_IFDIR | 0700, &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04006806 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04006807 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006808 inode->i_op = &btrfs_dir_inode_operations;
6809 inode->i_fop = &btrfs_dir_file_operations;
6810
Chris Mason39279cc2007-06-12 06:35:45 -04006811 inode->i_nlink = 1;
Chris Masondbe674a2008-07-17 12:54:05 -04006812 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04006813
Yan, Zheng76dda932009-09-21 16:00:26 -04006814 err = btrfs_update_inode(trans, new_root, inode);
6815 BUG_ON(err);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04006816
Yan, Zheng76dda932009-09-21 16:00:26 -04006817 iput(inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04006818 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006819}
6820
Chris Masond352ac62008-09-29 15:18:18 -04006821/* helper function for file defrag and space balancing. This
6822 * forces readahead on a given range of bytes in an inode
6823 */
Chris Masonedbd8d42007-12-21 16:27:24 -05006824unsigned long btrfs_force_ra(struct address_space *mapping,
Chris Mason86479a02007-09-10 19:58:16 -04006825 struct file_ra_state *ra, struct file *file,
6826 pgoff_t offset, pgoff_t last_index)
6827{
Chris Mason8e7bf942008-04-28 09:02:36 -04006828 pgoff_t req_size = last_index - offset + 1;
Chris Mason86479a02007-09-10 19:58:16 -04006829
Chris Mason86479a02007-09-10 19:58:16 -04006830 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
6831 return offset + req_size;
Chris Mason86479a02007-09-10 19:58:16 -04006832}
6833
Chris Mason39279cc2007-06-12 06:35:45 -04006834struct inode *btrfs_alloc_inode(struct super_block *sb)
6835{
6836 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006837 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006838
6839 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
6840 if (!ei)
6841 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006842
6843 ei->root = NULL;
6844 ei->space_info = NULL;
6845 ei->generation = 0;
6846 ei->sequence = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04006847 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04006848 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04006849 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006850 ei->delalloc_bytes = 0;
6851 ei->reserved_bytes = 0;
6852 ei->disk_i_size = 0;
6853 ei->flags = 0;
6854 ei->index_cnt = (u64)-1;
6855 ei->last_unlink_trans = 0;
6856
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006857 atomic_set(&ei->outstanding_extents, 0);
Josef Bacik57a45ced2011-01-25 16:30:38 -05006858 atomic_set(&ei->reserved_extents, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006859
6860 ei->ordered_data_close = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04006861 ei->orphan_meta_reserved = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006862 ei->dummy_inode = 0;
Li Zefan261507a02010-12-17 14:21:50 +08006863 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006864
Miao Xie16cdcec2011-04-22 18:12:22 +08006865 ei->delayed_node = NULL;
6866
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006867 inode = &ei->vfs_inode;
6868 extent_map_tree_init(&ei->extent_tree, GFP_NOFS);
6869 extent_io_tree_init(&ei->io_tree, &inode->i_data, GFP_NOFS);
6870 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data, GFP_NOFS);
6871 mutex_init(&ei->log_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006872 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Josef Bacik7b128762008-07-24 12:17:14 -04006873 INIT_LIST_HEAD(&ei->i_orphan);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006874 INIT_LIST_HEAD(&ei->delalloc_inodes);
Chris Mason5a3f23d2009-03-31 13:27:11 -04006875 INIT_LIST_HEAD(&ei->ordered_operations);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006876 RB_CLEAR_NODE(&ei->rb_node);
6877
6878 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006879}
6880
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11006881static void btrfs_i_callback(struct rcu_head *head)
6882{
6883 struct inode *inode = container_of(head, struct inode, i_rcu);
6884 INIT_LIST_HEAD(&inode->i_dentry);
6885 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
6886}
6887
Chris Mason39279cc2007-06-12 06:35:45 -04006888void btrfs_destroy_inode(struct inode *inode)
6889{
Chris Masone6dcd2d2008-07-17 12:53:50 -04006890 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04006891 struct btrfs_root *root = BTRFS_I(inode)->root;
6892
Chris Mason39279cc2007-06-12 06:35:45 -04006893 WARN_ON(!list_empty(&inode->i_dentry));
6894 WARN_ON(inode->i_data.nrpages);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006895 WARN_ON(atomic_read(&BTRFS_I(inode)->outstanding_extents));
Josef Bacik57a45ced2011-01-25 16:30:38 -05006896 WARN_ON(atomic_read(&BTRFS_I(inode)->reserved_extents));
Chris Mason39279cc2007-06-12 06:35:45 -04006897
Chris Mason5a3f23d2009-03-31 13:27:11 -04006898 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05006899 * This can happen where we create an inode, but somebody else also
6900 * created the same inode and we need to destroy the one we already
6901 * created.
6902 */
6903 if (!root)
6904 goto free;
6905
6906 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04006907 * Make sure we're properly removed from the ordered operation
6908 * lists.
6909 */
6910 smp_mb();
6911 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
6912 spin_lock(&root->fs_info->ordered_extent_lock);
6913 list_del_init(&BTRFS_I(inode)->ordered_operations);
6914 spin_unlock(&root->fs_info->ordered_extent_lock);
6915 }
6916
Josef Bacik0af3d002010-06-21 14:48:16 -04006917 if (root == root->fs_info->tree_root) {
6918 struct btrfs_block_group_cache *block_group;
6919
6920 block_group = btrfs_lookup_block_group(root->fs_info,
6921 BTRFS_I(inode)->block_group);
6922 if (block_group && block_group->inode == inode) {
6923 spin_lock(&block_group->lock);
6924 block_group->inode = NULL;
6925 spin_unlock(&block_group->lock);
6926 btrfs_put_block_group(block_group);
6927 } else if (block_group) {
6928 btrfs_put_block_group(block_group);
6929 }
6930 }
6931
Yan, Zhengd68fc572010-05-16 10:49:58 -04006932 spin_lock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04006933 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
Li Zefan33345d012011-04-20 10:31:50 +08006934 printk(KERN_INFO "BTRFS: inode %llu still on the orphan list\n",
6935 (unsigned long long)btrfs_ino(inode));
Yan, Zheng80825102009-11-12 09:35:36 +00006936 list_del_init(&BTRFS_I(inode)->i_orphan);
Josef Bacik7b128762008-07-24 12:17:14 -04006937 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04006938 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04006939
Chris Masond3977122009-01-05 21:25:51 -05006940 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006941 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
6942 if (!ordered)
6943 break;
6944 else {
Chris Masond3977122009-01-05 21:25:51 -05006945 printk(KERN_ERR "btrfs found ordered "
6946 "extent %llu %llu on inode cleanup\n",
6947 (unsigned long long)ordered->file_offset,
6948 (unsigned long long)ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006949 btrfs_remove_ordered_extent(inode, ordered);
6950 btrfs_put_ordered_extent(ordered);
6951 btrfs_put_ordered_extent(ordered);
6952 }
6953 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006954 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04006955 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05006956free:
Miao Xie16cdcec2011-04-22 18:12:22 +08006957 btrfs_remove_delayed_node(inode);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11006958 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04006959}
6960
Al Viro45321ac2010-06-07 13:43:19 -04006961int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04006962{
6963 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04006964
Josef Bacik0af3d002010-06-21 14:48:16 -04006965 if (btrfs_root_refs(&root->root_item) == 0 &&
Li Zefan82d59022011-04-20 10:33:24 +08006966 !is_free_space_inode(root, inode))
Al Viro45321ac2010-06-07 13:43:19 -04006967 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04006968 else
Al Viro45321ac2010-06-07 13:43:19 -04006969 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04006970}
6971
Sven Wegener0ee0fda2008-07-30 16:54:26 -04006972static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04006973{
6974 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
6975
6976 inode_init_once(&ei->vfs_inode);
6977}
6978
6979void btrfs_destroy_cachep(void)
6980{
6981 if (btrfs_inode_cachep)
6982 kmem_cache_destroy(btrfs_inode_cachep);
6983 if (btrfs_trans_handle_cachep)
6984 kmem_cache_destroy(btrfs_trans_handle_cachep);
6985 if (btrfs_transaction_cachep)
6986 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04006987 if (btrfs_path_cachep)
6988 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05006989 if (btrfs_free_space_cachep)
6990 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04006991}
6992
6993int btrfs_init_cachep(void)
6994{
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02006995 btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
6996 sizeof(struct btrfs_inode), 0,
6997 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04006998 if (!btrfs_inode_cachep)
6999 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007000
7001 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
7002 sizeof(struct btrfs_trans_handle), 0,
7003 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007004 if (!btrfs_trans_handle_cachep)
7005 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007006
7007 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
7008 sizeof(struct btrfs_transaction), 0,
7009 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007010 if (!btrfs_transaction_cachep)
7011 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007012
7013 btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
7014 sizeof(struct btrfs_path), 0,
7015 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007016 if (!btrfs_path_cachep)
7017 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007018
Josef Bacikdc89e982011-01-28 17:05:48 -05007019 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space_cache",
7020 sizeof(struct btrfs_free_space), 0,
7021 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
7022 if (!btrfs_free_space_cachep)
7023 goto fail;
7024
Chris Mason39279cc2007-06-12 06:35:45 -04007025 return 0;
7026fail:
7027 btrfs_destroy_cachep();
7028 return -ENOMEM;
7029}
7030
7031static int btrfs_getattr(struct vfsmount *mnt,
7032 struct dentry *dentry, struct kstat *stat)
7033{
7034 struct inode *inode = dentry->d_inode;
7035 generic_fillattr(inode, stat);
Chris Mason3394e162008-11-17 20:42:26 -05007036 stat->dev = BTRFS_I(inode)->root->anon_super.s_dev;
Chris Masond6667462008-01-03 14:51:00 -05007037 stat->blksize = PAGE_CACHE_SIZE;
Yan Zhenga76a3cd2008-10-09 11:46:29 -04007038 stat->blocks = (inode_get_bytes(inode) +
7039 BTRFS_I(inode)->delalloc_bytes) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04007040 return 0;
7041}
7042
Liu Bo75e7cb72011-03-22 10:12:20 +00007043/*
7044 * If a file is moved, it will inherit the cow and compression flags of the new
7045 * directory.
7046 */
7047static void fixup_inode_flags(struct inode *dir, struct inode *inode)
7048{
7049 struct btrfs_inode *b_dir = BTRFS_I(dir);
7050 struct btrfs_inode *b_inode = BTRFS_I(inode);
7051
7052 if (b_dir->flags & BTRFS_INODE_NODATACOW)
7053 b_inode->flags |= BTRFS_INODE_NODATACOW;
7054 else
7055 b_inode->flags &= ~BTRFS_INODE_NODATACOW;
7056
7057 if (b_dir->flags & BTRFS_INODE_COMPRESS)
7058 b_inode->flags |= BTRFS_INODE_COMPRESS;
7059 else
7060 b_inode->flags &= ~BTRFS_INODE_COMPRESS;
7061}
7062
Chris Masond3977122009-01-05 21:25:51 -05007063static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7064 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04007065{
7066 struct btrfs_trans_handle *trans;
7067 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007068 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04007069 struct inode *new_inode = new_dentry->d_inode;
7070 struct inode *old_inode = old_dentry->d_inode;
7071 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007072 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007073 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04007074 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08007075 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007076
Li Zefan33345d012011-04-20 10:31:50 +08007077 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04007078 return -EPERM;
7079
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007080 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08007081 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05007082 return -EXDEV;
7083
Li Zefan33345d012011-04-20 10:31:50 +08007084 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
7085 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007086 return -ENOTEMPTY;
7087
Chris Mason39279cc2007-06-12 06:35:45 -04007088 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007089 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04007090 return -ENOTEMPTY;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007091 /*
7092 * we're using rename to replace one file with another.
7093 * and the replacement file is large. Start IO on it now so
7094 * we don't add too much work to the end of the transaction
7095 */
Bartlomiej Zolnierkiewicz4baf8c92009-08-07 13:47:08 -04007096 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
Chris Mason5a3f23d2009-03-31 13:27:11 -04007097 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
7098 filemap_flush(old_inode->i_mapping);
7099
Yan, Zheng76dda932009-09-21 16:00:26 -04007100 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08007101 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04007102 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04007103 /*
7104 * We want to reserve the absolute worst case amount of items. So if
7105 * both inodes are subvols and we need to unlink them then that would
7106 * require 4 item modifications, but if they are both normal inodes it
7107 * would require 5 item modifications, so we'll assume their normal
7108 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
7109 * should cover the worst case number of items we'll modify.
7110 */
7111 trans = btrfs_start_transaction(root, 20);
Johann Lombardib44c59a2011-03-31 13:23:47 +00007112 if (IS_ERR(trans)) {
7113 ret = PTR_ERR(trans);
7114 goto out_notrans;
7115 }
Chris Mason5f39d392007-10-15 16:14:19 -04007116
Yan, Zhenga5719522009-09-24 09:17:31 -04007117 btrfs_set_trans_block_group(trans, new_dir);
Chris Mason39279cc2007-06-12 06:35:45 -04007118
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007119 if (dest != root)
7120 btrfs_record_root_in_trans(trans, dest);
7121
Yan, Zhenga5719522009-09-24 09:17:31 -04007122 ret = btrfs_set_inode_index(new_dir, &index);
7123 if (ret)
7124 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007125
Li Zefan33345d012011-04-20 10:31:50 +08007126 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007127 /* force full log commit if subvolume involved. */
7128 root->fs_info->last_trans_log_full_commit = trans->transid;
7129 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04007130 ret = btrfs_insert_inode_ref(trans, dest,
7131 new_dentry->d_name.name,
7132 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08007133 old_ino,
7134 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04007135 if (ret)
7136 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007137 /*
7138 * this is an ugly little race, but the rename is required
7139 * to make sure that if we crash, the inode is either at the
7140 * old name or the new one. pinning the log transaction lets
7141 * us make sure we don't allow a log commit to come in after
7142 * we unlink the name but before we add the new name back in.
7143 */
7144 btrfs_pin_log_trans(root);
7145 }
Chris Mason12fcfd22009-03-24 10:24:20 -04007146 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04007147 * make sure the inode gets flushed if it is replacing
7148 * something.
7149 */
Li Zefan33345d012011-04-20 10:31:50 +08007150 if (new_inode && new_inode->i_size && S_ISREG(old_inode->i_mode))
Chris Mason5a3f23d2009-03-31 13:27:11 -04007151 btrfs_add_ordered_operation(trans, root, old_inode);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007152
Chris Mason39279cc2007-06-12 06:35:45 -04007153 old_dir->i_ctime = old_dir->i_mtime = ctime;
7154 new_dir->i_ctime = new_dir->i_mtime = ctime;
7155 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04007156
Chris Mason12fcfd22009-03-24 10:24:20 -04007157 if (old_dentry->d_parent != new_dentry->d_parent)
7158 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
7159
Li Zefan33345d012011-04-20 10:31:50 +08007160 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007161 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
7162 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
7163 old_dentry->d_name.name,
7164 old_dentry->d_name.len);
7165 } else {
Al Viro92986792011-03-04 17:14:37 +00007166 ret = __btrfs_unlink_inode(trans, root, old_dir,
7167 old_dentry->d_inode,
7168 old_dentry->d_name.name,
7169 old_dentry->d_name.len);
7170 if (!ret)
7171 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007172 }
7173 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04007174
7175 if (new_inode) {
7176 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08007177 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007178 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
7179 root_objectid = BTRFS_I(new_inode)->location.objectid;
7180 ret = btrfs_unlink_subvol(trans, dest, new_dir,
7181 root_objectid,
7182 new_dentry->d_name.name,
7183 new_dentry->d_name.len);
7184 BUG_ON(new_inode->i_nlink == 0);
7185 } else {
7186 ret = btrfs_unlink_inode(trans, dest, new_dir,
7187 new_dentry->d_inode,
7188 new_dentry->d_name.name,
7189 new_dentry->d_name.len);
7190 }
7191 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04007192 if (new_inode->i_nlink == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04007193 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007194 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04007195 }
Chris Mason39279cc2007-06-12 06:35:45 -04007196 }
Josef Bacikaec74772008-07-24 12:12:38 -04007197
Liu Bo75e7cb72011-03-22 10:12:20 +00007198 fixup_inode_flags(new_dir, old_inode);
7199
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007200 ret = btrfs_add_link(trans, new_dir, old_inode,
7201 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04007202 new_dentry->d_name.len, 0, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007203 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04007204
Li Zefan33345d012011-04-20 10:31:50 +08007205 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacik6a912212010-11-20 09:48:00 +00007206 struct dentry *parent = dget_parent(new_dentry);
7207 btrfs_log_new_name(trans, old_inode, old_dir, parent);
7208 dput(parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007209 btrfs_end_log_trans(root);
7210 }
Chris Mason39279cc2007-06-12 06:35:45 -04007211out_fail:
Chris Masonab78c842008-07-29 16:15:18 -04007212 btrfs_end_transaction_throttle(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00007213out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08007214 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04007215 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007216
Chris Mason39279cc2007-06-12 06:35:45 -04007217 return ret;
7218}
7219
Chris Masond352ac62008-09-29 15:18:18 -04007220/*
7221 * some fairly slow code that needs optimization. This walks the list
7222 * of all the inodes with pending delalloc and forces them to disk.
7223 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00007224int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
Chris Masonea8c2812008-08-04 23:17:27 -04007225{
7226 struct list_head *head = &root->fs_info->delalloc_inodes;
7227 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007228 struct inode *inode;
Chris Masonea8c2812008-08-04 23:17:27 -04007229
Yan Zhengc146afa2008-11-12 14:34:12 -05007230 if (root->fs_info->sb->s_flags & MS_RDONLY)
7231 return -EROFS;
7232
Chris Mason75eff682008-12-15 15:54:40 -05007233 spin_lock(&root->fs_info->delalloc_lock);
Chris Masond3977122009-01-05 21:25:51 -05007234 while (!list_empty(head)) {
Chris Masonea8c2812008-08-04 23:17:27 -04007235 binode = list_entry(head->next, struct btrfs_inode,
7236 delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007237 inode = igrab(&binode->vfs_inode);
7238 if (!inode)
7239 list_del_init(&binode->delalloc_inodes);
Chris Mason75eff682008-12-15 15:54:40 -05007240 spin_unlock(&root->fs_info->delalloc_lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007241 if (inode) {
Chris Mason8c8bee12008-09-29 11:19:10 -04007242 filemap_flush(inode->i_mapping);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00007243 if (delay_iput)
7244 btrfs_add_delayed_iput(inode);
7245 else
7246 iput(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007247 }
7248 cond_resched();
Chris Mason75eff682008-12-15 15:54:40 -05007249 spin_lock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04007250 }
Chris Mason75eff682008-12-15 15:54:40 -05007251 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04007252
7253 /* the filemap_flush will queue IO into the worker threads, but
7254 * we have to make sure the IO is actually started and that
7255 * ordered extents get created before we return
7256 */
7257 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05007258 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05007259 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04007260 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05007261 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
7262 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04007263 }
7264 atomic_dec(&root->fs_info->async_submit_draining);
Chris Masonea8c2812008-08-04 23:17:27 -04007265 return 0;
7266}
7267
Josef Bacik0019f102010-10-15 15:18:40 -04007268int btrfs_start_one_delalloc_inode(struct btrfs_root *root, int delay_iput,
7269 int sync)
Yan, Zheng5da9d012010-05-16 10:46:25 -04007270{
7271 struct btrfs_inode *binode;
7272 struct inode *inode = NULL;
7273
7274 spin_lock(&root->fs_info->delalloc_lock);
7275 while (!list_empty(&root->fs_info->delalloc_inodes)) {
7276 binode = list_entry(root->fs_info->delalloc_inodes.next,
7277 struct btrfs_inode, delalloc_inodes);
7278 inode = igrab(&binode->vfs_inode);
7279 if (inode) {
7280 list_move_tail(&binode->delalloc_inodes,
7281 &root->fs_info->delalloc_inodes);
7282 break;
7283 }
7284
7285 list_del_init(&binode->delalloc_inodes);
7286 cond_resched_lock(&root->fs_info->delalloc_lock);
7287 }
7288 spin_unlock(&root->fs_info->delalloc_lock);
7289
7290 if (inode) {
Josef Bacik0019f102010-10-15 15:18:40 -04007291 if (sync) {
7292 filemap_write_and_wait(inode->i_mapping);
7293 /*
7294 * We have to do this because compression doesn't
7295 * actually set PG_writeback until it submits the pages
7296 * for IO, which happens in an async thread, so we could
7297 * race and not actually wait for any writeback pages
7298 * because they've not been submitted yet. Technically
7299 * this could still be the case for the ordered stuff
7300 * since the async thread may not have started to do its
7301 * work yet. If this becomes the case then we need to
7302 * figure out a way to make sure that in writepage we
7303 * wait for any async pages to be submitted before
7304 * returning so that fdatawait does what its supposed to
7305 * do.
7306 */
7307 btrfs_wait_ordered_range(inode, 0, (u64)-1);
7308 } else {
7309 filemap_flush(inode->i_mapping);
7310 }
Yan, Zheng5da9d012010-05-16 10:46:25 -04007311 if (delay_iput)
7312 btrfs_add_delayed_iput(inode);
7313 else
7314 iput(inode);
7315 return 1;
7316 }
7317 return 0;
7318}
7319
Chris Mason39279cc2007-06-12 06:35:45 -04007320static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
7321 const char *symname)
7322{
7323 struct btrfs_trans_handle *trans;
7324 struct btrfs_root *root = BTRFS_I(dir)->root;
7325 struct btrfs_path *path;
7326 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05007327 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04007328 int err;
7329 int drop_inode = 0;
7330 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007331 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04007332 int name_len;
7333 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04007334 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04007335 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04007336 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05007337 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007338
7339 name_len = strlen(symname) + 1;
7340 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
7341 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05007342
Josef Bacik9ed74f22009-09-11 16:12:44 -04007343 /*
7344 * 2 items for inode item and ref
7345 * 2 items for dir items
7346 * 1 item for xattr if selinux is on
7347 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04007348 trans = btrfs_start_transaction(root, 5);
7349 if (IS_ERR(trans))
7350 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05007351
Chris Mason39279cc2007-06-12 06:35:45 -04007352 btrfs_set_trans_block_group(trans, dir);
7353
Li Zefan581bb052011-04-20 10:06:11 +08007354 err = btrfs_find_free_ino(root, &objectid);
7355 if (err)
7356 goto out_unlock;
7357
Josef Bacikaec74772008-07-24 12:12:38 -04007358 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08007359 dentry->d_name.len, btrfs_ino(dir), objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04007360 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO,
7361 &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04007362 if (IS_ERR(inode)) {
7363 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007364 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04007365 }
Chris Mason39279cc2007-06-12 06:35:45 -04007366
Eric Paris2a7dba32011-02-01 11:05:39 -05007367 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04007368 if (err) {
7369 drop_inode = 1;
7370 goto out_unlock;
7371 }
7372
Chris Mason39279cc2007-06-12 06:35:45 -04007373 btrfs_set_trans_block_group(trans, inode);
Josef Bacika1b075d2010-11-19 20:36:11 +00007374 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04007375 if (err)
7376 drop_inode = 1;
7377 else {
7378 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04007379 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04007380 inode->i_fop = &btrfs_file_operations;
7381 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05007382 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04007383 }
Chris Mason39279cc2007-06-12 06:35:45 -04007384 btrfs_update_inode_block_group(trans, inode);
7385 btrfs_update_inode_block_group(trans, dir);
7386 if (drop_inode)
7387 goto out_unlock;
7388
7389 path = btrfs_alloc_path();
7390 BUG_ON(!path);
Li Zefan33345d012011-04-20 10:31:50 +08007391 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007392 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007393 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
7394 datasize = btrfs_file_extent_calc_inline_size(name_len);
7395 err = btrfs_insert_empty_item(trans, root, path, &key,
7396 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04007397 if (err) {
7398 drop_inode = 1;
7399 goto out_unlock;
7400 }
Chris Mason5f39d392007-10-15 16:14:19 -04007401 leaf = path->nodes[0];
7402 ei = btrfs_item_ptr(leaf, path->slots[0],
7403 struct btrfs_file_extent_item);
7404 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
7405 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04007406 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04007407 btrfs_set_file_extent_encryption(leaf, ei, 0);
7408 btrfs_set_file_extent_compression(leaf, ei, 0);
7409 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
7410 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
7411
Chris Mason39279cc2007-06-12 06:35:45 -04007412 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04007413 write_extent_buffer(leaf, symname, ptr, name_len);
7414 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04007415 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04007416
Chris Mason39279cc2007-06-12 06:35:45 -04007417 inode->i_op = &btrfs_symlink_inode_operations;
7418 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04007419 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04007420 inode_set_bytes(inode, name_len);
Chris Masondbe674a2008-07-17 12:54:05 -04007421 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04007422 err = btrfs_update_inode(trans, root, inode);
7423 if (err)
7424 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04007425
7426out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04007427 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04007428 btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04007429 if (drop_inode) {
7430 inode_dec_link_count(inode);
7431 iput(inode);
7432 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04007433 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04007434 return err;
7435}
Chris Mason16432982008-04-10 10:23:21 -04007436
Josef Bacik0af3d002010-06-21 14:48:16 -04007437static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
7438 u64 start, u64 num_bytes, u64 min_size,
7439 loff_t actual_len, u64 *alloc_hint,
7440 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04007441{
Yan Zhengd899e052008-10-30 14:25:28 -04007442 struct btrfs_root *root = BTRFS_I(inode)->root;
7443 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04007444 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00007445 u64 i_size;
Yan Zhengd899e052008-10-30 14:25:28 -04007446 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04007447 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04007448
Josef Bacik0af3d002010-06-21 14:48:16 -04007449 if (trans)
7450 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04007451 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04007452 if (own_trans) {
7453 trans = btrfs_start_transaction(root, 3);
7454 if (IS_ERR(trans)) {
7455 ret = PTR_ERR(trans);
7456 break;
7457 }
Yan Zhengd899e052008-10-30 14:25:28 -04007458 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00007459
Yan, Zhengefa56462010-05-16 10:49:59 -04007460 ret = btrfs_reserve_extent(trans, root, num_bytes, min_size,
7461 0, *alloc_hint, (u64)-1, &ins, 1);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007462 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04007463 if (own_trans)
7464 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04007465 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00007466 }
7467
Yan Zhengd899e052008-10-30 14:25:28 -04007468 ret = insert_reserved_file_extent(trans, inode,
7469 cur_offset, ins.objectid,
7470 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00007471 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04007472 BTRFS_FILE_EXTENT_PREALLOC);
7473 BUG_ON(ret);
Chris Masona1ed8352009-09-11 12:27:37 -04007474 btrfs_drop_extent_cache(inode, cur_offset,
7475 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007476
Yan Zhengd899e052008-10-30 14:25:28 -04007477 num_bytes -= ins.offset;
7478 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04007479 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00007480
Yan Zhengd899e052008-10-30 14:25:28 -04007481 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02007482 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04007483 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04007484 (actual_len > inode->i_size) &&
7485 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00007486 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00007487 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00007488 else
Josef Bacik55a61d12010-11-22 18:50:32 +00007489 i_size = cur_offset;
7490 i_size_write(inode, i_size);
7491 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007492 }
7493
Yan Zhengd899e052008-10-30 14:25:28 -04007494 ret = btrfs_update_inode(trans, root, inode);
7495 BUG_ON(ret);
Yan Zhengd899e052008-10-30 14:25:28 -04007496
Josef Bacik0af3d002010-06-21 14:48:16 -04007497 if (own_trans)
7498 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007499 }
Yan Zhengd899e052008-10-30 14:25:28 -04007500 return ret;
7501}
7502
Josef Bacik0af3d002010-06-21 14:48:16 -04007503int btrfs_prealloc_file_range(struct inode *inode, int mode,
7504 u64 start, u64 num_bytes, u64 min_size,
7505 loff_t actual_len, u64 *alloc_hint)
7506{
7507 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
7508 min_size, actual_len, alloc_hint,
7509 NULL);
7510}
7511
7512int btrfs_prealloc_file_range_trans(struct inode *inode,
7513 struct btrfs_trans_handle *trans, int mode,
7514 u64 start, u64 num_bytes, u64 min_size,
7515 loff_t actual_len, u64 *alloc_hint)
7516{
7517 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
7518 min_size, actual_len, alloc_hint, trans);
7519}
7520
Chris Masone6dcd2d2008-07-17 12:53:50 -04007521static int btrfs_set_page_dirty(struct page *page)
7522{
Chris Masone6dcd2d2008-07-17 12:53:50 -04007523 return __set_page_dirty_nobuffers(page);
7524}
7525
Nick Pigginb74c79e2011-01-07 17:49:58 +11007526static int btrfs_permission(struct inode *inode, int mask, unsigned int flags)
Yanfdebe2b2008-01-14 13:26:08 -05007527{
Li Zefanb83cc962010-12-20 16:04:08 +08007528 struct btrfs_root *root = BTRFS_I(inode)->root;
7529
7530 if (btrfs_root_readonly(root) && (mask & MAY_WRITE))
7531 return -EROFS;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02007532 if ((BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) && (mask & MAY_WRITE))
Yanfdebe2b2008-01-14 13:26:08 -05007533 return -EACCES;
Nick Pigginb74c79e2011-01-07 17:49:58 +11007534 return generic_permission(inode, mask, flags, btrfs_check_acl);
Yanfdebe2b2008-01-14 13:26:08 -05007535}
Chris Mason39279cc2007-06-12 06:35:45 -04007536
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007537static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05007538 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04007539 .lookup = btrfs_lookup,
7540 .create = btrfs_create,
7541 .unlink = btrfs_unlink,
7542 .link = btrfs_link,
7543 .mkdir = btrfs_mkdir,
7544 .rmdir = btrfs_rmdir,
7545 .rename = btrfs_rename,
7546 .symlink = btrfs_symlink,
7547 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04007548 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007549 .setxattr = btrfs_setxattr,
7550 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05007551 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007552 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05007553 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04007554};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007555static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007556 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05007557 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04007558};
Yan, Zheng76dda932009-09-21 16:00:26 -04007559
Alexey Dobriyan828c0952009-10-01 15:43:56 -07007560static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007561 .llseek = generic_file_llseek,
7562 .read = generic_read_dir,
David Woodhousecbdf5a22008-08-06 19:42:33 +01007563 .readdir = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04007564 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04007565#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04007566 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04007567#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04007568 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04007569 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04007570};
7571
Chris Masond1310b22008-01-24 16:13:08 -05007572static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04007573 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05007574 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04007575 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04007576 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04007577 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04007578 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Mason1259ab72008-05-12 13:39:03 -04007579 .readpage_io_failed_hook = btrfs_io_failed_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05007580 .set_bit_hook = btrfs_set_bit_hook,
7581 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04007582 .merge_extent_hook = btrfs_merge_extent_hook,
7583 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04007584};
7585
Chris Mason35054392009-01-21 13:11:13 -05007586/*
7587 * btrfs doesn't support the bmap operation because swapfiles
7588 * use bmap to make a mapping of extents in the file. They assume
7589 * these extents won't change over the life of the file and they
7590 * use the bmap result to do IO directly to the drive.
7591 *
7592 * the btrfs bmap call would return logical addresses that aren't
7593 * suitable for IO and they also will change frequently as COW
7594 * operations happen. So, swapfile + btrfs == corruption.
7595 *
7596 * For now we're avoiding this by dropping bmap.
7597 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07007598static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04007599 .readpage = btrfs_readpage,
7600 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04007601 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05007602 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04007603 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04007604 .invalidatepage = btrfs_invalidatepage,
7605 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04007606 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02007607 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04007608};
7609
Alexey Dobriyan7f094102009-09-21 17:01:10 -07007610static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04007611 .readpage = btrfs_readpage,
7612 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04007613 .invalidatepage = btrfs_invalidatepage,
7614 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04007615};
7616
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007617static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007618 .getattr = btrfs_getattr,
7619 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007620 .setxattr = btrfs_setxattr,
7621 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05007622 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007623 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05007624 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007625 .fiemap = btrfs_fiemap,
Chris Mason39279cc2007-06-12 06:35:45 -04007626};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007627static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04007628 .getattr = btrfs_getattr,
7629 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05007630 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007631 .setxattr = btrfs_setxattr,
7632 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04007633 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007634 .removexattr = btrfs_removexattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04007635};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007636static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007637 .readlink = generic_readlink,
7638 .follow_link = page_follow_link_light,
7639 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00007640 .getattr = btrfs_getattr,
Yanfdebe2b2008-01-14 13:26:08 -05007641 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05007642 .setxattr = btrfs_setxattr,
7643 .getxattr = btrfs_getxattr,
7644 .listxattr = btrfs_listxattr,
7645 .removexattr = btrfs_removexattr,
Chris Mason39279cc2007-06-12 06:35:45 -04007646};
Yan, Zheng76dda932009-09-21 16:00:26 -04007647
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04007648const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04007649 .d_delete = btrfs_dentry_delete,
7650};