blob: b78d3ab789ca2fc174b6cea6e8be6ad8ee6ab6a0 [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,
97 struct inode *inode, struct inode *dir)
Jim Owens0279b4c2009-02-04 09:29:13 -050098{
99 int err;
100
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000101 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500102 if (!err)
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000103 err = btrfs_xattr_security_init(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500104 return err;
105}
106
Chris Masond352ac62008-09-29 15:18:18 -0400107/*
Chris Masonc8b97812008-10-29 14:49:59 -0400108 * this does all the hard work for inserting an inline extent into
109 * the btree. The caller should have done a btrfs_drop_extents so that
110 * no overlapping inline items exist in the btree
111 */
Chris Masond3977122009-01-05 21:25:51 -0500112static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400113 struct btrfs_root *root, struct inode *inode,
114 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000115 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400116 struct page **compressed_pages)
117{
118 struct btrfs_key key;
119 struct btrfs_path *path;
120 struct extent_buffer *leaf;
121 struct page *page = NULL;
122 char *kaddr;
123 unsigned long ptr;
124 struct btrfs_file_extent_item *ei;
125 int err = 0;
126 int ret;
127 size_t cur_size = size;
128 size_t datasize;
129 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400130
Li Zefanfe3f5662011-03-28 08:30:38 +0000131 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400132 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400133
Chris Masond3977122009-01-05 21:25:51 -0500134 path = btrfs_alloc_path();
135 if (!path)
Chris Masonc8b97812008-10-29 14:49:59 -0400136 return -ENOMEM;
137
Chris Masonb9473432009-03-13 11:00:37 -0400138 path->leave_spinning = 1;
Chris Masonc8b97812008-10-29 14:49:59 -0400139 btrfs_set_trans_block_group(trans, inode);
140
Li Zefan33345d012011-04-20 10:31:50 +0800141 key.objectid = btrfs_ino(inode);
Chris Masonc8b97812008-10-29 14:49:59 -0400142 key.offset = start;
143 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
Chris Masonc8b97812008-10-29 14:49:59 -0400144 datasize = btrfs_file_extent_calc_inline_size(cur_size);
145
146 inode_add_bytes(inode, size);
147 ret = btrfs_insert_empty_item(trans, root, path, &key,
148 datasize);
149 BUG_ON(ret);
150 if (ret) {
151 err = ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400152 goto fail;
153 }
154 leaf = path->nodes[0];
155 ei = btrfs_item_ptr(leaf, path->slots[0],
156 struct btrfs_file_extent_item);
157 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
158 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
159 btrfs_set_file_extent_encryption(leaf, ei, 0);
160 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
161 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
162 ptr = btrfs_file_extent_inline_start(ei);
163
Li Zefan261507a02010-12-17 14:21:50 +0800164 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400165 struct page *cpage;
166 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500167 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400168 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500169 cur_size = min_t(unsigned long, compressed_size,
Chris Masonc8b97812008-10-29 14:49:59 -0400170 PAGE_CACHE_SIZE);
171
Chris Masonb9473432009-03-13 11:00:37 -0400172 kaddr = kmap_atomic(cpage, KM_USER0);
Chris Masonc8b97812008-10-29 14:49:59 -0400173 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Chris Masonb9473432009-03-13 11:00:37 -0400174 kunmap_atomic(kaddr, KM_USER0);
Chris Masonc8b97812008-10-29 14:49:59 -0400175
176 i++;
177 ptr += cur_size;
178 compressed_size -= cur_size;
179 }
180 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800181 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400182 } else {
183 page = find_get_page(inode->i_mapping,
184 start >> PAGE_CACHE_SHIFT);
185 btrfs_set_file_extent_compression(leaf, ei, 0);
186 kaddr = kmap_atomic(page, KM_USER0);
187 offset = start & (PAGE_CACHE_SIZE - 1);
188 write_extent_buffer(leaf, kaddr + offset, ptr, size);
189 kunmap_atomic(kaddr, KM_USER0);
190 page_cache_release(page);
191 }
192 btrfs_mark_buffer_dirty(leaf);
193 btrfs_free_path(path);
194
Yan, Zhengc2167752009-11-12 09:34:21 +0000195 /*
196 * we're an inline extent, so nobody can
197 * extend the file past i_size without locking
198 * a page we already have locked.
199 *
200 * We must do any isize and inode updates
201 * before we unlock the pages. Otherwise we
202 * could end up racing with unlink.
203 */
Chris Masonc8b97812008-10-29 14:49:59 -0400204 BTRFS_I(inode)->disk_i_size = inode->i_size;
205 btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000206
Chris Masonc8b97812008-10-29 14:49:59 -0400207 return 0;
208fail:
209 btrfs_free_path(path);
210 return err;
211}
212
213
214/*
215 * conditionally insert an inline extent into the file. This
216 * does the checks required to make sure the data is small enough
217 * to fit as an inline extent.
218 */
Chris Mason7f366cf2009-03-12 20:12:45 -0400219static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400220 struct btrfs_root *root,
221 struct inode *inode, u64 start, u64 end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000222 size_t compressed_size, int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400223 struct page **compressed_pages)
224{
225 u64 isize = i_size_read(inode);
226 u64 actual_end = min(end + 1, isize);
227 u64 inline_len = actual_end - start;
228 u64 aligned_end = (end + root->sectorsize - 1) &
229 ~((u64)root->sectorsize - 1);
230 u64 hint_byte;
231 u64 data_len = inline_len;
232 int ret;
233
234 if (compressed_size)
235 data_len = compressed_size;
236
237 if (start > 0 ||
Chris Mason70b99e62008-10-31 12:46:39 -0400238 actual_end >= PAGE_CACHE_SIZE ||
Chris Masonc8b97812008-10-29 14:49:59 -0400239 data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
240 (!compressed_size &&
241 (actual_end & (root->sectorsize - 1)) == 0) ||
242 end + 1 < isize ||
243 data_len > root->fs_info->max_inline) {
244 return 1;
245 }
246
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000247 ret = btrfs_drop_extents(trans, inode, start, aligned_end,
Chris Masona1ed8352009-09-11 12:27:37 -0400248 &hint_byte, 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400249 BUG_ON(ret);
250
251 if (isize > actual_end)
252 inline_len = min_t(u64, isize, actual_end);
253 ret = insert_inline_extent(trans, root, inode, start,
254 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000255 compress_type, compressed_pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400256 BUG_ON(ret);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400257 btrfs_delalloc_release_metadata(inode, end + 1 - start);
Chris Masona1ed8352009-09-11 12:27:37 -0400258 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
Chris Masonc8b97812008-10-29 14:49:59 -0400259 return 0;
260}
261
Chris Mason771ed682008-11-06 22:02:51 -0500262struct async_extent {
263 u64 start;
264 u64 ram_size;
265 u64 compressed_size;
266 struct page **pages;
267 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800268 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500269 struct list_head list;
270};
271
272struct async_cow {
273 struct inode *inode;
274 struct btrfs_root *root;
275 struct page *locked_page;
276 u64 start;
277 u64 end;
278 struct list_head extents;
279 struct btrfs_work work;
280};
281
282static noinline int add_async_extent(struct async_cow *cow,
283 u64 start, u64 ram_size,
284 u64 compressed_size,
285 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800286 unsigned long nr_pages,
287 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500288{
289 struct async_extent *async_extent;
290
291 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Yoshinori Sanodac97e52011-02-15 12:01:42 +0000292 BUG_ON(!async_extent);
Chris Mason771ed682008-11-06 22:02:51 -0500293 async_extent->start = start;
294 async_extent->ram_size = ram_size;
295 async_extent->compressed_size = compressed_size;
296 async_extent->pages = pages;
297 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800298 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500299 list_add_tail(&async_extent->list, &cow->extents);
300 return 0;
301}
302
Chris Masonc8b97812008-10-29 14:49:59 -0400303/*
Chris Mason771ed682008-11-06 22:02:51 -0500304 * we create compressed extents in two phases. The first
305 * phase compresses a range of pages that have already been
306 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400307 *
Chris Mason771ed682008-11-06 22:02:51 -0500308 * This is done inside an ordered work queue, and the compression
309 * is spread across many cpus. The actual IO submission is step
310 * two, and the ordered work queue takes care of making sure that
311 * happens in the same order things were put onto the queue by
312 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400313 *
Chris Mason771ed682008-11-06 22:02:51 -0500314 * If this code finds it can't get good compression, it puts an
315 * entry onto the work queue to write the uncompressed bytes. This
316 * makes sure that both compressed inodes and uncompressed inodes
317 * are written in the same order that pdflush sent them down.
Chris Masond352ac62008-09-29 15:18:18 -0400318 */
Chris Mason771ed682008-11-06 22:02:51 -0500319static noinline int compress_file_range(struct inode *inode,
320 struct page *locked_page,
321 u64 start, u64 end,
322 struct async_cow *async_cow,
323 int *num_added)
Chris Masonb888db22007-08-27 16:49:44 -0400324{
325 struct btrfs_root *root = BTRFS_I(inode)->root;
326 struct btrfs_trans_handle *trans;
Chris Masondb945352007-10-15 16:15:53 -0400327 u64 num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400328 u64 blocksize = root->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400329 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500330 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400331 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400332 struct page **pages = NULL;
333 unsigned long nr_pages;
334 unsigned long nr_pages_ret = 0;
335 unsigned long total_compressed = 0;
336 unsigned long total_in = 0;
337 unsigned long max_compressed = 128 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -0500338 unsigned long max_uncompressed = 128 * 1024;
Chris Masonc8b97812008-10-29 14:49:59 -0400339 int i;
340 int will_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800341 int compress_type = root->fs_info->compress_type;
Chris Masonb888db22007-08-27 16:49:44 -0400342
Chris Mason42dc7ba2008-12-15 11:44:56 -0500343 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400344again:
345 will_compress = 0;
346 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
347 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
348
Chris Masonf03d9302009-02-04 09:31:06 -0500349 /*
350 * we don't want to send crud past the end of i_size through
351 * compression, that's just a waste of CPU time. So, if the
352 * end of the file is before the start of our current
353 * requested range of bytes, we bail out to the uncompressed
354 * cleanup code that can deal with all of this.
355 *
356 * It isn't really the fastest way to fix things, but this is a
357 * very uncommon corner.
358 */
359 if (actual_end <= start)
360 goto cleanup_and_bail_uncompressed;
361
Chris Masonc8b97812008-10-29 14:49:59 -0400362 total_compressed = actual_end - start;
363
364 /* we want to make sure that amount of ram required to uncompress
365 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500366 * of a compressed extent to 128k. This is a crucial number
367 * because it also controls how easily we can spread reads across
368 * cpus for decompression.
369 *
370 * We also want to make sure the amount of IO required to do
371 * a random read is reasonably small, so we limit the size of
372 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400373 */
374 total_compressed = min(total_compressed, max_uncompressed);
Chris Masondb945352007-10-15 16:15:53 -0400375 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
Chris Masonbe20aa92007-12-17 20:14:01 -0500376 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400377 total_in = 0;
378 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400379
Chris Mason771ed682008-11-06 22:02:51 -0500380 /*
381 * we do compression for mount -o compress and when the
382 * inode has not been flagged as nocompress. This flag can
383 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400384 */
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200385 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) &&
Chris Mason1e701a32010-03-11 09:42:04 -0500386 (btrfs_test_opt(root, COMPRESS) ||
Liu Bo75e7cb72011-03-22 10:12:20 +0000387 (BTRFS_I(inode)->force_compress) ||
388 (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS))) {
Chris Masonc8b97812008-10-29 14:49:59 -0400389 WARN_ON(pages);
Chris Masoncfbc2462008-10-30 13:22:14 -0400390 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
Yoshinori Sanodac97e52011-02-15 12:01:42 +0000391 BUG_ON(!pages);
Chris Mason179e29e2007-11-01 11:28:41 -0400392
Li Zefan261507a02010-12-17 14:21:50 +0800393 if (BTRFS_I(inode)->force_compress)
394 compress_type = BTRFS_I(inode)->force_compress;
395
396 ret = btrfs_compress_pages(compress_type,
397 inode->i_mapping, start,
398 total_compressed, pages,
399 nr_pages, &nr_pages_ret,
400 &total_in,
401 &total_compressed,
402 max_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400403
404 if (!ret) {
405 unsigned long offset = total_compressed &
406 (PAGE_CACHE_SIZE - 1);
407 struct page *page = pages[nr_pages_ret - 1];
408 char *kaddr;
409
410 /* zero the tail end of the last page, we might be
411 * sending it down to disk
412 */
413 if (offset) {
414 kaddr = kmap_atomic(page, KM_USER0);
415 memset(kaddr + offset, 0,
416 PAGE_CACHE_SIZE - offset);
417 kunmap_atomic(kaddr, KM_USER0);
418 }
419 will_compress = 1;
420 }
421 }
422 if (start == 0) {
Chris Mason771ed682008-11-06 22:02:51 -0500423 trans = btrfs_join_transaction(root, 1);
Tsutomu Itoh3612b492011-01-25 02:51:38 +0000424 BUG_ON(IS_ERR(trans));
Chris Mason771ed682008-11-06 22:02:51 -0500425 btrfs_set_trans_block_group(trans, inode);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400426 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Mason771ed682008-11-06 22:02:51 -0500427
Chris Masonc8b97812008-10-29 14:49:59 -0400428 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500429 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400430 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500431 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400432 */
433 ret = cow_file_range_inline(trans, root, inode,
Li Zefanfe3f5662011-03-28 08:30:38 +0000434 start, end, 0, 0, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400435 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500436 /* try making a compressed inline extent */
Chris Masonc8b97812008-10-29 14:49:59 -0400437 ret = cow_file_range_inline(trans, root, inode,
438 start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000439 total_compressed,
440 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400441 }
442 if (ret == 0) {
Chris Mason771ed682008-11-06 22:02:51 -0500443 /*
444 * inline extent creation worked, we don't need
445 * to create any more async work items. Unlock
446 * and free up our temp pages.
447 */
Chris Masonc8b97812008-10-29 14:49:59 -0400448 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400449 &BTRFS_I(inode)->io_tree,
450 start, end, NULL,
451 EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
Chris Masona3429ab2009-10-08 12:30:20 -0400452 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400453 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000454
455 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400456 goto free_pages_out;
457 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000458 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400459 }
460
461 if (will_compress) {
462 /*
463 * we aren't doing an inline extent round the compressed size
464 * up to a block size boundary so the allocator does sane
465 * things
466 */
467 total_compressed = (total_compressed + blocksize - 1) &
468 ~(blocksize - 1);
469
470 /*
471 * one last check to make sure the compression is really a
472 * win, compare the page count read with the blocks on disk
473 */
474 total_in = (total_in + PAGE_CACHE_SIZE - 1) &
475 ~(PAGE_CACHE_SIZE - 1);
476 if (total_compressed >= total_in) {
477 will_compress = 0;
478 } else {
Chris Masonc8b97812008-10-29 14:49:59 -0400479 num_bytes = total_in;
480 }
481 }
482 if (!will_compress && pages) {
483 /*
484 * the compression code ran but failed to make things smaller,
485 * free any pages it allocated and our page pointer array
486 */
487 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400488 WARN_ON(pages[i]->mapping);
Chris Masonc8b97812008-10-29 14:49:59 -0400489 page_cache_release(pages[i]);
490 }
491 kfree(pages);
492 pages = NULL;
493 total_compressed = 0;
494 nr_pages_ret = 0;
495
496 /* flag the file so we don't compress in the future */
Chris Mason1e701a32010-03-11 09:42:04 -0500497 if (!btrfs_test_opt(root, FORCE_COMPRESS) &&
498 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500499 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500500 }
Chris Masonc8b97812008-10-29 14:49:59 -0400501 }
Chris Mason771ed682008-11-06 22:02:51 -0500502 if (will_compress) {
503 *num_added += 1;
504
505 /* the async work queues will take care of doing actual
506 * allocation on disk for these compressed pages,
507 * and will submit them to the elevator.
508 */
509 add_async_extent(async_cow, start, num_bytes,
Li Zefan261507a02010-12-17 14:21:50 +0800510 total_compressed, pages, nr_pages_ret,
511 compress_type);
Chris Mason771ed682008-11-06 22:02:51 -0500512
Yan, Zheng24ae6362010-12-06 07:02:36 +0000513 if (start + num_bytes < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500514 start += num_bytes;
515 pages = NULL;
516 cond_resched();
517 goto again;
518 }
519 } else {
Chris Masonf03d9302009-02-04 09:31:06 -0500520cleanup_and_bail_uncompressed:
Chris Mason771ed682008-11-06 22:02:51 -0500521 /*
522 * No compression, but we still need to write the pages in
523 * the file we've been given so far. redirty the locked
524 * page if it corresponds to our extent and set things up
525 * for the async work queue to run cow_file_range to do
526 * the normal delalloc dance
527 */
528 if (page_offset(locked_page) >= start &&
529 page_offset(locked_page) <= end) {
530 __set_page_dirty_nobuffers(locked_page);
531 /* unlocked later on in the async handlers */
532 }
Li Zefan261507a02010-12-17 14:21:50 +0800533 add_async_extent(async_cow, start, end - start + 1,
534 0, NULL, 0, BTRFS_COMPRESS_NONE);
Chris Mason771ed682008-11-06 22:02:51 -0500535 *num_added += 1;
536 }
537
538out:
539 return 0;
540
541free_pages_out:
542 for (i = 0; i < nr_pages_ret; i++) {
543 WARN_ON(pages[i]->mapping);
544 page_cache_release(pages[i]);
545 }
Chris Masond3977122009-01-05 21:25:51 -0500546 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500547
548 goto out;
549}
550
551/*
552 * phase two of compressed writeback. This is the ordered portion
553 * of the code, which only gets called in the order the work was
554 * queued. We walk all the async extents created by compress_file_range
555 * and send them down to the disk.
556 */
557static noinline int submit_compressed_extents(struct inode *inode,
558 struct async_cow *async_cow)
559{
560 struct async_extent *async_extent;
561 u64 alloc_hint = 0;
562 struct btrfs_trans_handle *trans;
563 struct btrfs_key ins;
564 struct extent_map *em;
565 struct btrfs_root *root = BTRFS_I(inode)->root;
566 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
567 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500568 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500569
570 if (list_empty(&async_cow->extents))
571 return 0;
572
Chris Mason771ed682008-11-06 22:02:51 -0500573
Chris Masond3977122009-01-05 21:25:51 -0500574 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500575 async_extent = list_entry(async_cow->extents.next,
576 struct async_extent, list);
577 list_del(&async_extent->list);
578
579 io_tree = &BTRFS_I(inode)->io_tree;
580
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500581retry:
Chris Mason771ed682008-11-06 22:02:51 -0500582 /* did the compression code fall back to uncompressed IO? */
583 if (!async_extent->pages) {
584 int page_started = 0;
585 unsigned long nr_written = 0;
586
587 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000588 async_extent->start +
589 async_extent->ram_size - 1, GFP_NOFS);
Chris Mason771ed682008-11-06 22:02:51 -0500590
591 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500592 ret = cow_file_range(inode, async_cow->locked_page,
593 async_extent->start,
594 async_extent->start +
595 async_extent->ram_size - 1,
596 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500597
598 /*
599 * if page_started, cow_file_range inserted an
600 * inline extent and took care of all the unlocking
601 * and IO for us. Otherwise, we need to submit
602 * all those pages down to the drive.
603 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500604 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500605 extent_write_locked_range(io_tree,
606 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500607 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500608 async_extent->ram_size - 1,
609 btrfs_get_extent,
610 WB_SYNC_ALL);
611 kfree(async_extent);
612 cond_resched();
613 continue;
614 }
615
616 lock_extent(io_tree, async_extent->start,
617 async_extent->start + async_extent->ram_size - 1,
618 GFP_NOFS);
Chris Mason771ed682008-11-06 22:02:51 -0500619
Yan, Zhengc2167752009-11-12 09:34:21 +0000620 trans = btrfs_join_transaction(root, 1);
Tsutomu Itoh3612b492011-01-25 02:51:38 +0000621 BUG_ON(IS_ERR(trans));
Chris Mason771ed682008-11-06 22:02:51 -0500622 ret = btrfs_reserve_extent(trans, root,
623 async_extent->compressed_size,
624 async_extent->compressed_size,
625 0, alloc_hint,
626 (u64)-1, &ins, 1);
Yan, Zhengc2167752009-11-12 09:34:21 +0000627 btrfs_end_transaction(trans, root);
628
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500629 if (ret) {
630 int i;
631 for (i = 0; i < async_extent->nr_pages; i++) {
632 WARN_ON(async_extent->pages[i]->mapping);
633 page_cache_release(async_extent->pages[i]);
634 }
635 kfree(async_extent->pages);
636 async_extent->nr_pages = 0;
637 async_extent->pages = NULL;
638 unlock_extent(io_tree, async_extent->start,
639 async_extent->start +
640 async_extent->ram_size - 1, GFP_NOFS);
641 goto retry;
642 }
643
Yan, Zhengc2167752009-11-12 09:34:21 +0000644 /*
645 * here we're doing allocation and writeback of the
646 * compressed pages
647 */
648 btrfs_drop_extent_cache(inode, async_extent->start,
649 async_extent->start +
650 async_extent->ram_size - 1, 0);
651
Chris Mason771ed682008-11-06 22:02:51 -0500652 em = alloc_extent_map(GFP_NOFS);
Tsutomu Itohc26a9202011-02-14 00:45:29 +0000653 BUG_ON(!em);
Chris Mason771ed682008-11-06 22:02:51 -0500654 em->start = async_extent->start;
655 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500656 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500657
658 em->block_start = ins.objectid;
659 em->block_len = ins.offset;
660 em->bdev = root->fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800661 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500662 set_bit(EXTENT_FLAG_PINNED, &em->flags);
663 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
664
Chris Masond3977122009-01-05 21:25:51 -0500665 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400666 write_lock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500667 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -0400668 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500669 if (ret != -EEXIST) {
670 free_extent_map(em);
671 break;
672 }
673 btrfs_drop_extent_cache(inode, async_extent->start,
674 async_extent->start +
675 async_extent->ram_size - 1, 0);
676 }
677
Li Zefan261507a02010-12-17 14:21:50 +0800678 ret = btrfs_add_ordered_extent_compress(inode,
679 async_extent->start,
680 ins.objectid,
681 async_extent->ram_size,
682 ins.offset,
683 BTRFS_ORDERED_COMPRESSED,
684 async_extent->compress_type);
Chris Mason771ed682008-11-06 22:02:51 -0500685 BUG_ON(ret);
686
Chris Mason771ed682008-11-06 22:02:51 -0500687 /*
688 * clear dirty, set writeback and unlock the pages.
689 */
690 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400691 &BTRFS_I(inode)->io_tree,
692 async_extent->start,
693 async_extent->start +
694 async_extent->ram_size - 1,
695 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
696 EXTENT_CLEAR_UNLOCK |
Chris Masona3429ab2009-10-08 12:30:20 -0400697 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400698 EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500699
700 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500701 async_extent->start,
702 async_extent->ram_size,
703 ins.objectid,
704 ins.offset, async_extent->pages,
705 async_extent->nr_pages);
Chris Mason771ed682008-11-06 22:02:51 -0500706
707 BUG_ON(ret);
Chris Mason771ed682008-11-06 22:02:51 -0500708 alloc_hint = ins.objectid + ins.offset;
709 kfree(async_extent);
710 cond_resched();
711 }
712
Chris Mason771ed682008-11-06 22:02:51 -0500713 return 0;
714}
715
Josef Bacik4b46fce2010-05-23 11:00:55 -0400716static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
717 u64 num_bytes)
718{
719 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
720 struct extent_map *em;
721 u64 alloc_hint = 0;
722
723 read_lock(&em_tree->lock);
724 em = search_extent_mapping(em_tree, start, num_bytes);
725 if (em) {
726 /*
727 * if block start isn't an actual block number then find the
728 * first block in this inode and use that as a hint. If that
729 * block is also bogus then just don't worry about it.
730 */
731 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
732 free_extent_map(em);
733 em = search_extent_mapping(em_tree, 0, 0);
734 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
735 alloc_hint = em->block_start;
736 if (em)
737 free_extent_map(em);
738 } else {
739 alloc_hint = em->block_start;
740 free_extent_map(em);
741 }
742 }
743 read_unlock(&em_tree->lock);
744
745 return alloc_hint;
746}
747
Li Zefan82d59022011-04-20 10:33:24 +0800748static inline bool is_free_space_inode(struct btrfs_root *root,
749 struct inode *inode)
750{
751 if (root == root->fs_info->tree_root ||
752 BTRFS_I(inode)->location.objectid == BTRFS_FREE_INO_OBJECTID)
753 return true;
754 return false;
755}
756
Chris Mason771ed682008-11-06 22:02:51 -0500757/*
758 * when extent_io.c finds a delayed allocation range in the file,
759 * the call backs end up in this code. The basic idea is to
760 * allocate extents on disk for the range, and create ordered data structs
761 * in ram to track those extents.
762 *
763 * locked_page is the page that writepage had locked already. We use
764 * it to make sure we don't do extra locks or unlocks.
765 *
766 * *page_started is set to one if we unlock locked_page and do everything
767 * required to start IO on it. It may be clean and already done with
768 * IO when we return.
769 */
770static noinline int cow_file_range(struct inode *inode,
771 struct page *locked_page,
772 u64 start, u64 end, int *page_started,
773 unsigned long *nr_written,
774 int unlock)
775{
776 struct btrfs_root *root = BTRFS_I(inode)->root;
777 struct btrfs_trans_handle *trans;
778 u64 alloc_hint = 0;
779 u64 num_bytes;
780 unsigned long ram_size;
781 u64 disk_num_bytes;
782 u64 cur_alloc_size;
783 u64 blocksize = root->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500784 struct btrfs_key ins;
785 struct extent_map *em;
786 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
787 int ret = 0;
788
Li Zefan82d59022011-04-20 10:33:24 +0800789 BUG_ON(is_free_space_inode(root, inode));
Chris Mason771ed682008-11-06 22:02:51 -0500790 trans = btrfs_join_transaction(root, 1);
Tsutomu Itoh3612b492011-01-25 02:51:38 +0000791 BUG_ON(IS_ERR(trans));
Chris Mason771ed682008-11-06 22:02:51 -0500792 btrfs_set_trans_block_group(trans, inode);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400793 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Mason771ed682008-11-06 22:02:51 -0500794
Chris Mason771ed682008-11-06 22:02:51 -0500795 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
796 num_bytes = max(blocksize, num_bytes);
797 disk_num_bytes = num_bytes;
798 ret = 0;
799
800 if (start == 0) {
801 /* lets try to make an inline extent */
802 ret = cow_file_range_inline(trans, root, inode,
Li Zefanfe3f5662011-03-28 08:30:38 +0000803 start, end, 0, 0, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500804 if (ret == 0) {
805 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400806 &BTRFS_I(inode)->io_tree,
807 start, end, NULL,
808 EXTENT_CLEAR_UNLOCK_PAGE |
809 EXTENT_CLEAR_UNLOCK |
810 EXTENT_CLEAR_DELALLOC |
811 EXTENT_CLEAR_DIRTY |
812 EXTENT_SET_WRITEBACK |
813 EXTENT_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000814
Chris Mason771ed682008-11-06 22:02:51 -0500815 *nr_written = *nr_written +
816 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
817 *page_started = 1;
818 ret = 0;
819 goto out;
820 }
821 }
Chris Masonc8b97812008-10-29 14:49:59 -0400822
823 BUG_ON(disk_num_bytes >
824 btrfs_super_total_bytes(&root->fs_info->super_copy));
825
Josef Bacik4b46fce2010-05-23 11:00:55 -0400826 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400827 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400828
Chris Masond3977122009-01-05 21:25:51 -0500829 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400830 unsigned long op;
831
Josef Bacik287a0ab2010-03-19 18:07:23 +0000832 cur_alloc_size = disk_num_bytes;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400833 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500834 root->sectorsize, 0, alloc_hint,
Chris Masone6dcd2d2008-07-17 12:53:50 -0400835 (u64)-1, &ins, 1);
Chris Masond3977122009-01-05 21:25:51 -0500836 BUG_ON(ret);
837
Chris Masone6dcd2d2008-07-17 12:53:50 -0400838 em = alloc_extent_map(GFP_NOFS);
Tsutomu Itohc26a9202011-02-14 00:45:29 +0000839 BUG_ON(!em);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400840 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500841 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500842 ram_size = ins.offset;
843 em->len = ins.offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400844
Chris Masone6dcd2d2008-07-17 12:53:50 -0400845 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400846 em->block_len = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400847 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400848 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -0400849
Chris Masond3977122009-01-05 21:25:51 -0500850 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400851 write_lock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400852 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -0400853 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400854 if (ret != -EEXIST) {
855 free_extent_map(em);
856 break;
857 }
858 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400859 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400860 }
861
Chris Mason98d20f62008-04-14 09:46:10 -0400862 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400863 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -0500864 ram_size, cur_alloc_size, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400865 BUG_ON(ret);
Chris Masonc8b97812008-10-29 14:49:59 -0400866
Yan Zheng17d217f2008-12-12 10:03:38 -0500867 if (root->root_key.objectid ==
868 BTRFS_DATA_RELOC_TREE_OBJECTID) {
869 ret = btrfs_reloc_clone_csums(inode, start,
870 cur_alloc_size);
871 BUG_ON(ret);
872 }
873
Chris Masond3977122009-01-05 21:25:51 -0500874 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -0400875 break;
Chris Masond3977122009-01-05 21:25:51 -0500876
Chris Masonc8b97812008-10-29 14:49:59 -0400877 /* we're not doing compressed IO, don't unlock the first
878 * page (which the caller expects to stay locked), don't
879 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -0400880 *
881 * Do set the Private2 bit so we know this page was properly
882 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -0400883 */
Chris Masona791e352009-10-08 11:27:10 -0400884 op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0;
885 op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
886 EXTENT_SET_PRIVATE2;
887
Chris Masonc8b97812008-10-29 14:49:59 -0400888 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
889 start, start + ram_size - 1,
Chris Masona791e352009-10-08 11:27:10 -0400890 locked_page, op);
Chris Masonc8b97812008-10-29 14:49:59 -0400891 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -0500892 num_bytes -= cur_alloc_size;
893 alloc_hint = ins.objectid + ins.offset;
894 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400895 }
Chris Masonb888db22007-08-27 16:49:44 -0400896out:
Chris Mason771ed682008-11-06 22:02:51 -0500897 ret = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400898 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400899
Chris Masonbe20aa92007-12-17 20:14:01 -0500900 return ret;
Chris Mason771ed682008-11-06 22:02:51 -0500901}
Chris Masonc8b97812008-10-29 14:49:59 -0400902
Chris Mason771ed682008-11-06 22:02:51 -0500903/*
904 * work queue call back to started compression on a file and pages
905 */
906static noinline void async_cow_start(struct btrfs_work *work)
907{
908 struct async_cow *async_cow;
909 int num_added = 0;
910 async_cow = container_of(work, struct async_cow, work);
911
912 compress_file_range(async_cow->inode, async_cow->locked_page,
913 async_cow->start, async_cow->end, async_cow,
914 &num_added);
915 if (num_added == 0)
916 async_cow->inode = NULL;
917}
918
919/*
920 * work queue call back to submit previously compressed pages
921 */
922static noinline void async_cow_submit(struct btrfs_work *work)
923{
924 struct async_cow *async_cow;
925 struct btrfs_root *root;
926 unsigned long nr_pages;
927
928 async_cow = container_of(work, struct async_cow, work);
929
930 root = async_cow->root;
931 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
932 PAGE_CACHE_SHIFT;
933
934 atomic_sub(nr_pages, &root->fs_info->async_delalloc_pages);
935
936 if (atomic_read(&root->fs_info->async_delalloc_pages) <
937 5 * 1042 * 1024 &&
938 waitqueue_active(&root->fs_info->async_submit_wait))
939 wake_up(&root->fs_info->async_submit_wait);
940
Chris Masond3977122009-01-05 21:25:51 -0500941 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -0500942 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -0500943}
Chris Masonc8b97812008-10-29 14:49:59 -0400944
Chris Mason771ed682008-11-06 22:02:51 -0500945static noinline void async_cow_free(struct btrfs_work *work)
946{
947 struct async_cow *async_cow;
948 async_cow = container_of(work, struct async_cow, work);
949 kfree(async_cow);
950}
951
952static int cow_file_range_async(struct inode *inode, struct page *locked_page,
953 u64 start, u64 end, int *page_started,
954 unsigned long *nr_written)
955{
956 struct async_cow *async_cow;
957 struct btrfs_root *root = BTRFS_I(inode)->root;
958 unsigned long nr_pages;
959 u64 cur_end;
960 int limit = 10 * 1024 * 1042;
961
Chris Masona3429ab2009-10-08 12:30:20 -0400962 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
963 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -0500964 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500965 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
966 async_cow->inode = inode;
967 async_cow->root = root;
968 async_cow->locked_page = locked_page;
969 async_cow->start = start;
970
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200971 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
Chris Mason771ed682008-11-06 22:02:51 -0500972 cur_end = end;
973 else
974 cur_end = min(end, start + 512 * 1024 - 1);
975
976 async_cow->end = cur_end;
977 INIT_LIST_HEAD(&async_cow->extents);
978
979 async_cow->work.func = async_cow_start;
980 async_cow->work.ordered_func = async_cow_submit;
981 async_cow->work.ordered_free = async_cow_free;
982 async_cow->work.flags = 0;
983
Chris Mason771ed682008-11-06 22:02:51 -0500984 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
985 PAGE_CACHE_SHIFT;
986 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
987
988 btrfs_queue_worker(&root->fs_info->delalloc_workers,
989 &async_cow->work);
990
991 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
992 wait_event(root->fs_info->async_submit_wait,
993 (atomic_read(&root->fs_info->async_delalloc_pages) <
994 limit));
995 }
996
Chris Masond3977122009-01-05 21:25:51 -0500997 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -0500998 atomic_read(&root->fs_info->async_delalloc_pages)) {
999 wait_event(root->fs_info->async_submit_wait,
1000 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1001 0));
1002 }
1003
1004 *nr_written += nr_pages;
1005 start = cur_end + 1;
1006 }
1007 *page_started = 1;
1008 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001009}
1010
Chris Masond3977122009-01-05 21:25:51 -05001011static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001012 u64 bytenr, u64 num_bytes)
1013{
1014 int ret;
1015 struct btrfs_ordered_sum *sums;
1016 LIST_HEAD(list);
1017
Yan Zheng07d400a2009-01-06 11:42:00 -05001018 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
1019 bytenr + num_bytes - 1, &list);
Yan Zheng17d217f2008-12-12 10:03:38 -05001020 if (ret == 0 && list_empty(&list))
1021 return 0;
1022
1023 while (!list_empty(&list)) {
1024 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1025 list_del(&sums->list);
1026 kfree(sums);
1027 }
1028 return 1;
1029}
1030
Chris Masond352ac62008-09-29 15:18:18 -04001031/*
1032 * when nowcow writeback call back. This checks for snapshots or COW copies
1033 * of the extents that exist in the file, and COWs the file as required.
1034 *
1035 * If no cow copies or snapshots exist, we write directly to the existing
1036 * blocks on disk
1037 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001038static noinline int run_delalloc_nocow(struct inode *inode,
1039 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001040 u64 start, u64 end, int *page_started, int force,
1041 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001042{
Chris Masonbe20aa92007-12-17 20:14:01 -05001043 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001044 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001045 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001046 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001047 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001048 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001049 u64 cow_start;
1050 u64 cur_offset;
1051 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001052 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001053 u64 disk_bytenr;
1054 u64 num_bytes;
1055 int extent_type;
1056 int ret;
Yan Zhengd899e052008-10-30 14:25:28 -04001057 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001058 int nocow;
1059 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001060 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001061 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001062
1063 path = btrfs_alloc_path();
1064 BUG_ON(!path);
Li Zefan82d59022011-04-20 10:33:24 +08001065
1066 nolock = is_free_space_inode(root, inode);
1067
1068 if (nolock)
Josef Bacik0cb59c92010-07-02 12:14:14 -04001069 trans = btrfs_join_transaction_nolock(root, 1);
Li Zefan82d59022011-04-20 10:33:24 +08001070 else
Josef Bacik0cb59c92010-07-02 12:14:14 -04001071 trans = btrfs_join_transaction(root, 1);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00001072 BUG_ON(IS_ERR(trans));
Chris Masonbe20aa92007-12-17 20:14:01 -05001073
Yan Zheng80ff3852008-10-30 14:20:02 -04001074 cow_start = (u64)-1;
1075 cur_offset = start;
1076 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001077 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001078 cur_offset, 0);
1079 BUG_ON(ret < 0);
1080 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1081 leaf = path->nodes[0];
1082 btrfs_item_key_to_cpu(leaf, &found_key,
1083 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001084 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001085 found_key.type == BTRFS_EXTENT_DATA_KEY)
1086 path->slots[0]--;
1087 }
1088 check_prev = 0;
1089next_slot:
1090 leaf = path->nodes[0];
1091 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1092 ret = btrfs_next_leaf(root, path);
1093 if (ret < 0)
1094 BUG_ON(1);
1095 if (ret > 0)
1096 break;
1097 leaf = path->nodes[0];
1098 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001099
Yan Zheng80ff3852008-10-30 14:20:02 -04001100 nocow = 0;
1101 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001102 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001103 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001104
Li Zefan33345d012011-04-20 10:31:50 +08001105 if (found_key.objectid > ino ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001106 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1107 found_key.offset > end)
1108 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001109
Yan Zheng80ff3852008-10-30 14:20:02 -04001110 if (found_key.offset > cur_offset) {
1111 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001112 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001113 goto out_check;
1114 }
Chris Masonc31f8832008-01-08 15:46:31 -05001115
Yan Zheng80ff3852008-10-30 14:20:02 -04001116 fi = btrfs_item_ptr(leaf, path->slots[0],
1117 struct btrfs_file_extent_item);
1118 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001119
Yan Zhengd899e052008-10-30 14:25:28 -04001120 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1121 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001122 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001123 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001124 extent_end = found_key.offset +
1125 btrfs_file_extent_num_bytes(leaf, fi);
1126 if (extent_end <= start) {
1127 path->slots[0]++;
1128 goto next_slot;
1129 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001130 if (disk_bytenr == 0)
1131 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001132 if (btrfs_file_extent_compression(leaf, fi) ||
1133 btrfs_file_extent_encryption(leaf, fi) ||
1134 btrfs_file_extent_other_encoding(leaf, fi))
1135 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001136 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1137 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001138 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001139 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001140 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001141 found_key.offset -
1142 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001143 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001144 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001145 disk_bytenr += cur_offset - found_key.offset;
1146 num_bytes = min(end + 1, extent_end) - cur_offset;
1147 /*
1148 * force cow if csum exists in the range.
1149 * this ensure that csum for a given extent are
1150 * either valid or do not exist.
1151 */
1152 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1153 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001154 nocow = 1;
1155 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1156 extent_end = found_key.offset +
1157 btrfs_file_extent_inline_len(leaf, fi);
1158 extent_end = ALIGN(extent_end, root->sectorsize);
1159 } else {
1160 BUG_ON(1);
1161 }
1162out_check:
1163 if (extent_end <= start) {
1164 path->slots[0]++;
1165 goto next_slot;
1166 }
1167 if (!nocow) {
1168 if (cow_start == (u64)-1)
1169 cow_start = cur_offset;
1170 cur_offset = extent_end;
1171 if (cur_offset > end)
1172 break;
1173 path->slots[0]++;
1174 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001175 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001176
Yan Zheng7ea394f2008-08-05 13:05:02 -04001177 btrfs_release_path(root, path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001178 if (cow_start != (u64)-1) {
1179 ret = cow_file_range(inode, locked_page, cow_start,
Chris Mason771ed682008-11-06 22:02:51 -05001180 found_key.offset - 1, page_started,
1181 nr_written, 1);
Yan Zheng80ff3852008-10-30 14:20:02 -04001182 BUG_ON(ret);
1183 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001184 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001185
Yan Zhengd899e052008-10-30 14:25:28 -04001186 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1187 struct extent_map *em;
1188 struct extent_map_tree *em_tree;
1189 em_tree = &BTRFS_I(inode)->extent_tree;
1190 em = alloc_extent_map(GFP_NOFS);
Tsutomu Itohc26a9202011-02-14 00:45:29 +00001191 BUG_ON(!em);
Yan Zhengd899e052008-10-30 14:25:28 -04001192 em->start = cur_offset;
Chris Mason445a6942008-11-10 11:53:33 -05001193 em->orig_start = em->start;
Yan Zhengd899e052008-10-30 14:25:28 -04001194 em->len = num_bytes;
1195 em->block_len = num_bytes;
1196 em->block_start = disk_bytenr;
1197 em->bdev = root->fs_info->fs_devices->latest_bdev;
1198 set_bit(EXTENT_FLAG_PINNED, &em->flags);
1199 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001200 write_lock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001201 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04001202 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001203 if (ret != -EEXIST) {
1204 free_extent_map(em);
1205 break;
1206 }
1207 btrfs_drop_extent_cache(inode, em->start,
1208 em->start + em->len - 1, 0);
1209 }
1210 type = BTRFS_ORDERED_PREALLOC;
1211 } else {
1212 type = BTRFS_ORDERED_NOCOW;
1213 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001214
1215 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001216 num_bytes, num_bytes, type);
1217 BUG_ON(ret);
Chris Mason771ed682008-11-06 22:02:51 -05001218
Yan, Zhengefa56462010-05-16 10:49:59 -04001219 if (root->root_key.objectid ==
1220 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1221 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1222 num_bytes);
1223 BUG_ON(ret);
1224 }
1225
Yan Zhengd899e052008-10-30 14:25:28 -04001226 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
Chris Masona791e352009-10-08 11:27:10 -04001227 cur_offset, cur_offset + num_bytes - 1,
1228 locked_page, EXTENT_CLEAR_UNLOCK_PAGE |
1229 EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
1230 EXTENT_SET_PRIVATE2);
Yan Zheng80ff3852008-10-30 14:20:02 -04001231 cur_offset = extent_end;
1232 if (cur_offset > end)
1233 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001234 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001235 btrfs_release_path(root, path);
1236
1237 if (cur_offset <= end && cow_start == (u64)-1)
1238 cow_start = cur_offset;
1239 if (cow_start != (u64)-1) {
1240 ret = cow_file_range(inode, locked_page, cow_start, end,
Chris Mason771ed682008-11-06 22:02:51 -05001241 page_started, nr_written, 1);
Yan Zheng80ff3852008-10-30 14:20:02 -04001242 BUG_ON(ret);
1243 }
1244
Josef Bacik0cb59c92010-07-02 12:14:14 -04001245 if (nolock) {
1246 ret = btrfs_end_transaction_nolock(trans, root);
1247 BUG_ON(ret);
1248 } else {
1249 ret = btrfs_end_transaction(trans, root);
1250 BUG_ON(ret);
1251 }
Yan Zheng7ea394f2008-08-05 13:05:02 -04001252 btrfs_free_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001253 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001254}
1255
Chris Masond352ac62008-09-29 15:18:18 -04001256/*
1257 * extent_io.c call back to do delayed allocation processing
1258 */
Chris Masonc8b97812008-10-29 14:49:59 -04001259static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001260 u64 start, u64 end, int *page_started,
1261 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001262{
Chris Masonbe20aa92007-12-17 20:14:01 -05001263 int ret;
Chris Mason7f366cf2009-03-12 20:12:45 -04001264 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona2135012008-06-25 16:01:30 -04001265
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001266 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)
Chris Masonc8b97812008-10-29 14:49:59 -04001267 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001268 page_started, 1, nr_written);
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001269 else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC)
Yan Zhengd899e052008-10-30 14:25:28 -04001270 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001271 page_started, 0, nr_written);
Chris Mason1e701a32010-03-11 09:42:04 -05001272 else if (!btrfs_test_opt(root, COMPRESS) &&
Liu Bo75e7cb72011-03-22 10:12:20 +00001273 !(BTRFS_I(inode)->force_compress) &&
1274 !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS))
Chris Mason7f366cf2009-03-12 20:12:45 -04001275 ret = cow_file_range(inode, locked_page, start, end,
1276 page_started, nr_written, 1);
Chris Masonbe20aa92007-12-17 20:14:01 -05001277 else
Chris Mason771ed682008-11-06 22:02:51 -05001278 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001279 page_started, nr_written);
Chris Masonb888db22007-08-27 16:49:44 -04001280 return ret;
1281}
1282
Josef Bacik9ed74f22009-09-11 16:12:44 -04001283static int btrfs_split_extent_hook(struct inode *inode,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001284 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001285{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001286 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001287 if (!(orig->state & EXTENT_DELALLOC))
1288 return 0;
1289
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001290 atomic_inc(&BTRFS_I(inode)->outstanding_extents);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001291 return 0;
1292}
1293
1294/*
1295 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1296 * extents so we can keep track of new extents that are just merged onto old
1297 * extents, such as when we are doing sequential writes, so we can properly
1298 * account for the metadata space we'll need.
1299 */
1300static int btrfs_merge_extent_hook(struct inode *inode,
1301 struct extent_state *new,
1302 struct extent_state *other)
1303{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001304 /* not delalloc, ignore it */
1305 if (!(other->state & EXTENT_DELALLOC))
1306 return 0;
1307
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001308 atomic_dec(&BTRFS_I(inode)->outstanding_extents);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001309 return 0;
1310}
1311
Chris Masond352ac62008-09-29 15:18:18 -04001312/*
1313 * extent_io.c set_bit_hook, used to track delayed allocation
1314 * bytes in this file, and to maintain the list of inodes that
1315 * have pending delalloc work to be done.
1316 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001317static int btrfs_set_bit_hook(struct inode *inode,
1318 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001319{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001320
Chris Mason75eff682008-12-15 15:54:40 -05001321 /*
1322 * set_bit and clear bit hooks normally require _irqsave/restore
1323 * but in this case, we are only testeing for the DELALLOC
1324 * bit, which is only set or cleared with irqs on
1325 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001326 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001327 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001328 u64 len = state->end + 1 - state->start;
Li Zefan82d59022011-04-20 10:33:24 +08001329 bool do_list = !is_free_space_inode(root, inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001330
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001331 if (*bits & EXTENT_FIRST_DELALLOC)
1332 *bits &= ~EXTENT_FIRST_DELALLOC;
1333 else
1334 atomic_inc(&BTRFS_I(inode)->outstanding_extents);
Josef Bacik287a0ab2010-03-19 18:07:23 +00001335
Chris Mason75eff682008-12-15 15:54:40 -05001336 spin_lock(&root->fs_info->delalloc_lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001337 BTRFS_I(inode)->delalloc_bytes += len;
1338 root->fs_info->delalloc_bytes += len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001339 if (do_list && list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
Chris Masonea8c2812008-08-04 23:17:27 -04001340 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1341 &root->fs_info->delalloc_inodes);
1342 }
Chris Mason75eff682008-12-15 15:54:40 -05001343 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001344 }
1345 return 0;
1346}
1347
Chris Masond352ac62008-09-29 15:18:18 -04001348/*
1349 * extent_io.c clear_bit_hook, see set_bit_hook for why
1350 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001351static int btrfs_clear_bit_hook(struct inode *inode,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001352 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001353{
Chris Mason75eff682008-12-15 15:54:40 -05001354 /*
1355 * set_bit and clear bit hooks normally require _irqsave/restore
1356 * but in this case, we are only testeing for the DELALLOC
1357 * bit, which is only set or cleared with irqs on
1358 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001359 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001360 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001361 u64 len = state->end + 1 - state->start;
Li Zefan82d59022011-04-20 10:33:24 +08001362 bool do_list = !is_free_space_inode(root, inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001363
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001364 if (*bits & EXTENT_FIRST_DELALLOC)
1365 *bits &= ~EXTENT_FIRST_DELALLOC;
1366 else if (!(*bits & EXTENT_DO_ACCOUNTING))
1367 atomic_dec(&BTRFS_I(inode)->outstanding_extents);
1368
1369 if (*bits & EXTENT_DO_ACCOUNTING)
1370 btrfs_delalloc_release_metadata(inode, len);
1371
Josef Bacik0cb59c92010-07-02 12:14:14 -04001372 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
1373 && do_list)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001374 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001375
Chris Mason75eff682008-12-15 15:54:40 -05001376 spin_lock(&root->fs_info->delalloc_lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001377 root->fs_info->delalloc_bytes -= len;
1378 BTRFS_I(inode)->delalloc_bytes -= len;
1379
Josef Bacik0cb59c92010-07-02 12:14:14 -04001380 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Chris Masonea8c2812008-08-04 23:17:27 -04001381 !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1382 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1383 }
Chris Mason75eff682008-12-15 15:54:40 -05001384 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001385 }
1386 return 0;
1387}
1388
Chris Masond352ac62008-09-29 15:18:18 -04001389/*
1390 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1391 * we don't create bios that span stripes or chunks
1392 */
Chris Mason239b14b2008-03-24 15:02:07 -04001393int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001394 size_t size, struct bio *bio,
1395 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001396{
1397 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
1398 struct btrfs_mapping_tree *map_tree;
Chris Masona62b9402008-10-03 16:31:08 -04001399 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001400 u64 length = 0;
1401 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001402 int ret;
1403
Chris Mason771ed682008-11-06 22:02:51 -05001404 if (bio_flags & EXTENT_BIO_COMPRESSED)
1405 return 0;
1406
Chris Masonf2d8d742008-04-21 10:03:05 -04001407 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001408 map_tree = &root->fs_info->mapping_tree;
1409 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04001410 ret = btrfs_map_block(map_tree, READ, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001411 &map_length, NULL, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04001412
Chris Masond3977122009-01-05 21:25:51 -05001413 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001414 return 1;
Andi Kleen411fc6b2010-10-29 15:14:31 -04001415 return ret;
Chris Mason239b14b2008-03-24 15:02:07 -04001416}
1417
Chris Masond352ac62008-09-29 15:18:18 -04001418/*
1419 * in order to insert checksums into the metadata in large chunks,
1420 * we wait until bio submission time. All the pages in the bio are
1421 * checksummed and sums are attached onto the ordered extent record.
1422 *
1423 * At IO completion time the cums attached on the ordered extent record
1424 * are inserted into the btree
1425 */
Chris Masond3977122009-01-05 21:25:51 -05001426static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1427 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001428 unsigned long bio_flags,
1429 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001430{
Chris Mason065631f2008-02-20 12:07:25 -05001431 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001432 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001433
Chris Masond20f7042008-12-08 16:58:54 -05001434 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Chris Mason44b8bd72008-04-16 11:14:51 -04001435 BUG_ON(ret);
Chris Mason4a69a412008-11-06 22:03:00 -05001436 return 0;
1437}
Chris Masone0156402008-04-16 11:15:20 -04001438
Chris Mason4a69a412008-11-06 22:03:00 -05001439/*
1440 * in order to insert checksums into the metadata in large chunks,
1441 * we wait until bio submission time. All the pages in the bio are
1442 * checksummed and sums are attached onto the ordered extent record.
1443 *
1444 * At IO completion time the cums attached on the ordered extent record
1445 * are inserted into the btree
1446 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001447static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001448 int mirror_num, unsigned long bio_flags,
1449 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001450{
1451 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason8b712842008-06-11 16:50:36 -04001452 return btrfs_map_bio(root, rw, bio, mirror_num, 1);
Chris Mason44b8bd72008-04-16 11:14:51 -04001453}
1454
Chris Masond352ac62008-09-29 15:18:18 -04001455/*
Chris Masoncad321a2008-12-17 14:51:42 -05001456 * extent_io.c submission hook. This does the right thing for csum calculation
1457 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001458 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001459static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001460 int mirror_num, unsigned long bio_flags,
1461 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001462{
1463 struct btrfs_root *root = BTRFS_I(inode)->root;
1464 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001465 int skip_sum;
Chris Mason44b8bd72008-04-16 11:14:51 -04001466
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001467 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001468
Li Zefan82d59022011-04-20 10:33:24 +08001469 if (is_free_space_inode(root, inode))
Josef Bacik0cb59c92010-07-02 12:14:14 -04001470 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 2);
1471 else
1472 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001473 BUG_ON(ret);
Chris Mason065631f2008-02-20 12:07:25 -05001474
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001475 if (!(rw & REQ_WRITE)) {
Chris Masond20f7042008-12-08 16:58:54 -05001476 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Chris Masonc8b97812008-10-29 14:49:59 -04001477 return btrfs_submit_compressed_read(inode, bio,
1478 mirror_num, bio_flags);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001479 } else if (!skip_sum) {
1480 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1481 if (ret)
1482 return ret;
1483 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001484 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001485 } else if (!skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001486 /* csum items have already been cloned */
1487 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1488 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001489 /* we're doing a write, do the async checksumming */
1490 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001491 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001492 bio_flags, bio_offset,
1493 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001494 __btrfs_submit_bio_done);
Chris Mason19b9bdb2008-10-30 14:23:13 -04001495 }
1496
Chris Mason0b86a832008-03-24 15:01:56 -04001497mapit:
Chris Mason8b712842008-06-11 16:50:36 -04001498 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
Chris Mason065631f2008-02-20 12:07:25 -05001499}
Chris Mason6885f302008-02-20 16:11:05 -05001500
Chris Masond352ac62008-09-29 15:18:18 -04001501/*
1502 * given a list of ordered sums record them in the inode. This happens
1503 * at IO completion time based on sums calculated at bio submission time.
1504 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001505static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001506 struct inode *inode, u64 file_offset,
1507 struct list_head *list)
1508{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001509 struct btrfs_ordered_sum *sum;
1510
1511 btrfs_set_trans_block_group(trans, inode);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001512
1513 list_for_each_entry(sum, list, list) {
Chris Masond20f7042008-12-08 16:58:54 -05001514 btrfs_csum_file_blocks(trans,
1515 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001516 }
1517 return 0;
1518}
1519
Josef Bacik2ac55d42010-02-03 19:33:23 +00001520int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1521 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001522{
Chris Masond3977122009-01-05 21:25:51 -05001523 if ((end & (PAGE_CACHE_SIZE - 1)) == 0)
Chris Mason771ed682008-11-06 22:02:51 -05001524 WARN_ON(1);
Chris Masonea8c2812008-08-04 23:17:27 -04001525 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001526 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001527}
1528
Chris Masond352ac62008-09-29 15:18:18 -04001529/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001530struct btrfs_writepage_fixup {
1531 struct page *page;
1532 struct btrfs_work work;
1533};
1534
Christoph Hellwigb2950862008-12-02 09:54:17 -05001535static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001536{
1537 struct btrfs_writepage_fixup *fixup;
1538 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001539 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001540 struct page *page;
1541 struct inode *inode;
1542 u64 page_start;
1543 u64 page_end;
1544
1545 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1546 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001547again:
Chris Mason247e7432008-07-17 12:53:51 -04001548 lock_page(page);
1549 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1550 ClearPageChecked(page);
1551 goto out_page;
1552 }
1553
1554 inode = page->mapping->host;
1555 page_start = page_offset(page);
1556 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1557
Josef Bacik2ac55d42010-02-03 19:33:23 +00001558 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
1559 &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001560
1561 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001562 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001563 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001564
1565 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1566 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001567 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1568 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001569 unlock_page(page);
1570 btrfs_start_ordered_extent(inode, ordered, 1);
1571 goto again;
1572 }
Chris Mason247e7432008-07-17 12:53:51 -04001573
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001574 BUG();
Josef Bacik2ac55d42010-02-03 19:33:23 +00001575 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001576 ClearPageChecked(page);
1577out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001578 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1579 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001580out_page:
1581 unlock_page(page);
1582 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08001583 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04001584}
1585
1586/*
1587 * There are a few paths in the higher layers of the kernel that directly
1588 * set the page dirty bit without asking the filesystem if it is a
1589 * good idea. This causes problems because we want to make sure COW
1590 * properly happens and the data=ordered rules are followed.
1591 *
Chris Masonc8b97812008-10-29 14:49:59 -04001592 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001593 * hasn't been properly setup for IO. We kick off an async process
1594 * to fix it up. The async helper will wait for ordered extents, set
1595 * the delalloc bit and make it safe to write the page.
1596 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001597static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001598{
1599 struct inode *inode = page->mapping->host;
1600 struct btrfs_writepage_fixup *fixup;
1601 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001602
Chris Mason8b62b722009-09-02 16:53:46 -04001603 /* this page is properly in the ordered list */
1604 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001605 return 0;
1606
1607 if (PageChecked(page))
1608 return -EAGAIN;
1609
1610 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1611 if (!fixup)
1612 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001613
Chris Mason247e7432008-07-17 12:53:51 -04001614 SetPageChecked(page);
1615 page_cache_get(page);
1616 fixup->work.func = btrfs_writepage_fixup_worker;
1617 fixup->page = page;
1618 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
1619 return -EAGAIN;
1620}
1621
Yan Zhengd899e052008-10-30 14:25:28 -04001622static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1623 struct inode *inode, u64 file_pos,
1624 u64 disk_bytenr, u64 disk_num_bytes,
1625 u64 num_bytes, u64 ram_bytes,
1626 u8 compression, u8 encryption,
1627 u16 other_encoding, int extent_type)
1628{
1629 struct btrfs_root *root = BTRFS_I(inode)->root;
1630 struct btrfs_file_extent_item *fi;
1631 struct btrfs_path *path;
1632 struct extent_buffer *leaf;
1633 struct btrfs_key ins;
1634 u64 hint;
1635 int ret;
1636
1637 path = btrfs_alloc_path();
1638 BUG_ON(!path);
1639
Chris Masonb9473432009-03-13 11:00:37 -04001640 path->leave_spinning = 1;
Chris Masona1ed8352009-09-11 12:27:37 -04001641
1642 /*
1643 * we may be replacing one extent in the tree with another.
1644 * The new extent is pinned in the extent map, and we don't want
1645 * to drop it from the cache until it is completely in the btree.
1646 *
1647 * So, tell btrfs_drop_extents to leave this extent in the cache.
1648 * the caller is expected to unpin it and allow it to be merged
1649 * with the others.
1650 */
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001651 ret = btrfs_drop_extents(trans, inode, file_pos, file_pos + num_bytes,
1652 &hint, 0);
Yan Zhengd899e052008-10-30 14:25:28 -04001653 BUG_ON(ret);
1654
Li Zefan33345d012011-04-20 10:31:50 +08001655 ins.objectid = btrfs_ino(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04001656 ins.offset = file_pos;
1657 ins.type = BTRFS_EXTENT_DATA_KEY;
1658 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
1659 BUG_ON(ret);
1660 leaf = path->nodes[0];
1661 fi = btrfs_item_ptr(leaf, path->slots[0],
1662 struct btrfs_file_extent_item);
1663 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1664 btrfs_set_file_extent_type(leaf, fi, extent_type);
1665 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1666 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1667 btrfs_set_file_extent_offset(leaf, fi, 0);
1668 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1669 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1670 btrfs_set_file_extent_compression(leaf, fi, compression);
1671 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1672 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04001673
1674 btrfs_unlock_up_safe(path, 1);
1675 btrfs_set_lock_blocking(leaf);
1676
Yan Zhengd899e052008-10-30 14:25:28 -04001677 btrfs_mark_buffer_dirty(leaf);
1678
1679 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04001680
1681 ins.objectid = disk_bytenr;
1682 ins.offset = disk_num_bytes;
1683 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001684 ret = btrfs_alloc_reserved_file_extent(trans, root,
1685 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08001686 btrfs_ino(inode), file_pos, &ins);
Yan Zhengd899e052008-10-30 14:25:28 -04001687 BUG_ON(ret);
Yan Zhengd899e052008-10-30 14:25:28 -04001688 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04001689
Yan Zhengd899e052008-10-30 14:25:28 -04001690 return 0;
1691}
1692
Chris Mason5d13a982009-03-13 11:41:46 -04001693/*
1694 * helper function for btrfs_finish_ordered_io, this
1695 * just reads in some of the csum leaves to prime them into ram
1696 * before we start the transaction. It limits the amount of btree
1697 * reads required while inside the transaction.
1698 */
Chris Masond352ac62008-09-29 15:18:18 -04001699/* as ordered data IO finishes, this gets called so we can finish
1700 * an ordered extent if the range of bytes in the file it covers are
1701 * fully written.
1702 */
Chris Mason211f90e2008-07-18 11:56:15 -04001703static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001704{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001705 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001706 struct btrfs_trans_handle *trans = NULL;
Chris Mason5d13a982009-03-13 11:41:46 -04001707 struct btrfs_ordered_extent *ordered_extent = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001708 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001709 struct extent_state *cached_state = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08001710 int compress_type = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001711 int ret;
Li Zefan82d59022011-04-20 10:33:24 +08001712 bool nolock;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001713
Josef Bacik5a1a3df2010-02-02 20:51:14 +00001714 ret = btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
1715 end - start + 1);
Chris Masonba1da2f2008-07-17 12:54:15 -04001716 if (!ret)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001717 return 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001718 BUG_ON(!ordered_extent);
Josef Bacikefd049f2010-02-02 20:50:10 +00001719
Li Zefan82d59022011-04-20 10:33:24 +08001720 nolock = is_free_space_inode(root, inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001721
Yan, Zhengc2167752009-11-12 09:34:21 +00001722 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
1723 BUG_ON(!list_empty(&ordered_extent->list));
1724 ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1725 if (!ret) {
Josef Bacik0cb59c92010-07-02 12:14:14 -04001726 if (nolock)
1727 trans = btrfs_join_transaction_nolock(root, 1);
1728 else
1729 trans = btrfs_join_transaction(root, 1);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00001730 BUG_ON(IS_ERR(trans));
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001731 btrfs_set_trans_block_group(trans, inode);
1732 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00001733 ret = btrfs_update_inode(trans, root, inode);
1734 BUG_ON(ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00001735 }
1736 goto out;
1737 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04001738
Josef Bacik2ac55d42010-02-03 19:33:23 +00001739 lock_extent_bits(io_tree, ordered_extent->file_offset,
1740 ordered_extent->file_offset + ordered_extent->len - 1,
1741 0, &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001742
Josef Bacik0cb59c92010-07-02 12:14:14 -04001743 if (nolock)
1744 trans = btrfs_join_transaction_nolock(root, 1);
1745 else
1746 trans = btrfs_join_transaction(root, 1);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00001747 BUG_ON(IS_ERR(trans));
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001748 btrfs_set_trans_block_group(trans, inode);
1749 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00001750
Chris Masonc8b97812008-10-29 14:49:59 -04001751 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08001752 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04001753 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08001754 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001755 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04001756 ordered_extent->file_offset,
1757 ordered_extent->file_offset +
1758 ordered_extent->len);
1759 BUG_ON(ret);
1760 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04001761 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04001762 ret = insert_reserved_file_extent(trans, inode,
1763 ordered_extent->file_offset,
1764 ordered_extent->start,
1765 ordered_extent->disk_len,
1766 ordered_extent->len,
1767 ordered_extent->len,
Li Zefan261507a02010-12-17 14:21:50 +08001768 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04001769 BTRFS_FILE_EXTENT_REG);
Chris Masona1ed8352009-09-11 12:27:37 -04001770 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
1771 ordered_extent->file_offset,
1772 ordered_extent->len);
Yan Zhengd899e052008-10-30 14:25:28 -04001773 BUG_ON(ret);
1774 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00001775 unlock_extent_cached(io_tree, ordered_extent->file_offset,
1776 ordered_extent->file_offset +
1777 ordered_extent->len - 1, &cached_state, GFP_NOFS);
1778
Chris Masone6dcd2d2008-07-17 12:53:50 -04001779 add_pending_csums(trans, inode, ordered_extent->file_offset,
1780 &ordered_extent->list);
1781
Josef Bacik1ef30be2011-04-05 19:25:36 -04001782 ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1783 if (!ret) {
1784 ret = btrfs_update_inode(trans, root, inode);
1785 BUG_ON(ret);
1786 }
1787 ret = 0;
Yan, Zhengc2167752009-11-12 09:34:21 +00001788out:
Josef Bacik0cb59c92010-07-02 12:14:14 -04001789 if (nolock) {
1790 if (trans)
1791 btrfs_end_transaction_nolock(trans, root);
1792 } else {
1793 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
1794 if (trans)
1795 btrfs_end_transaction(trans, root);
1796 }
1797
Chris Masone6dcd2d2008-07-17 12:53:50 -04001798 /* once for us */
1799 btrfs_put_ordered_extent(ordered_extent);
1800 /* once for the tree */
1801 btrfs_put_ordered_extent(ordered_extent);
1802
Chris Masone6dcd2d2008-07-17 12:53:50 -04001803 return 0;
1804}
1805
Christoph Hellwigb2950862008-12-02 09:54:17 -05001806static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04001807 struct extent_state *state, int uptodate)
1808{
liubo1abe9b82011-03-24 11:18:59 +00001809 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
1810
Chris Mason8b62b722009-09-02 16:53:46 -04001811 ClearPagePrivate2(page);
Chris Mason211f90e2008-07-18 11:56:15 -04001812 return btrfs_finish_ordered_io(page->mapping->host, start, end);
1813}
1814
Chris Masond352ac62008-09-29 15:18:18 -04001815/*
1816 * When IO fails, either with EIO or csum verification fails, we
1817 * try other mirrors that might have a good copy of the data. This
1818 * io_failure_record is used to record state as we go through all the
1819 * mirrors. If another mirror has good data, the page is set up to date
1820 * and things continue. If a good mirror can't be found, the original
1821 * bio end_io callback is called to indicate things have failed.
1822 */
Chris Mason7e383262008-04-09 16:28:12 -04001823struct io_failure_record {
1824 struct page *page;
1825 u64 start;
1826 u64 len;
1827 u64 logical;
Chris Masond20f7042008-12-08 16:58:54 -05001828 unsigned long bio_flags;
Chris Mason7e383262008-04-09 16:28:12 -04001829 int last_mirror;
1830};
1831
Christoph Hellwigb2950862008-12-02 09:54:17 -05001832static int btrfs_io_failed_hook(struct bio *failed_bio,
Chris Mason1259ab72008-05-12 13:39:03 -04001833 struct page *page, u64 start, u64 end,
1834 struct extent_state *state)
Chris Mason7e383262008-04-09 16:28:12 -04001835{
1836 struct io_failure_record *failrec = NULL;
1837 u64 private;
1838 struct extent_map *em;
1839 struct inode *inode = page->mapping->host;
1840 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Chris Mason3b951512008-04-17 11:29:12 -04001841 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason7e383262008-04-09 16:28:12 -04001842 struct bio *bio;
1843 int num_copies;
1844 int ret;
Chris Mason1259ab72008-05-12 13:39:03 -04001845 int rw;
Chris Mason7e383262008-04-09 16:28:12 -04001846 u64 logical;
1847
1848 ret = get_state_private(failure_tree, start, &private);
1849 if (ret) {
Chris Mason7e383262008-04-09 16:28:12 -04001850 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
1851 if (!failrec)
1852 return -ENOMEM;
1853 failrec->start = start;
1854 failrec->len = end - start + 1;
1855 failrec->last_mirror = 0;
Chris Masond20f7042008-12-08 16:58:54 -05001856 failrec->bio_flags = 0;
Chris Mason7e383262008-04-09 16:28:12 -04001857
Chris Mason890871b2009-09-02 16:24:52 -04001858 read_lock(&em_tree->lock);
Chris Mason3b951512008-04-17 11:29:12 -04001859 em = lookup_extent_mapping(em_tree, start, failrec->len);
1860 if (em->start > start || em->start + em->len < start) {
1861 free_extent_map(em);
1862 em = NULL;
1863 }
Chris Mason890871b2009-09-02 16:24:52 -04001864 read_unlock(&em_tree->lock);
Chris Mason7e383262008-04-09 16:28:12 -04001865
1866 if (!em || IS_ERR(em)) {
1867 kfree(failrec);
1868 return -EIO;
1869 }
1870 logical = start - em->start;
1871 logical = em->block_start + logical;
Chris Masond20f7042008-12-08 16:58:54 -05001872 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
1873 logical = em->block_start;
1874 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
Li Zefan261507a02010-12-17 14:21:50 +08001875 extent_set_compress_type(&failrec->bio_flags,
1876 em->compress_type);
Chris Masond20f7042008-12-08 16:58:54 -05001877 }
Chris Mason7e383262008-04-09 16:28:12 -04001878 failrec->logical = logical;
1879 free_extent_map(em);
1880 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
1881 EXTENT_DIRTY, GFP_NOFS);
Chris Mason587f7702008-04-11 12:16:46 -04001882 set_state_private(failure_tree, start,
1883 (u64)(unsigned long)failrec);
Chris Mason7e383262008-04-09 16:28:12 -04001884 } else {
Chris Mason587f7702008-04-11 12:16:46 -04001885 failrec = (struct io_failure_record *)(unsigned long)private;
Chris Mason7e383262008-04-09 16:28:12 -04001886 }
1887 num_copies = btrfs_num_copies(
1888 &BTRFS_I(inode)->root->fs_info->mapping_tree,
1889 failrec->logical, failrec->len);
1890 failrec->last_mirror++;
1891 if (!state) {
Chris Masoncad321a2008-12-17 14:51:42 -05001892 spin_lock(&BTRFS_I(inode)->io_tree.lock);
Chris Mason7e383262008-04-09 16:28:12 -04001893 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
1894 failrec->start,
1895 EXTENT_LOCKED);
1896 if (state && state->start != failrec->start)
1897 state = NULL;
Chris Masoncad321a2008-12-17 14:51:42 -05001898 spin_unlock(&BTRFS_I(inode)->io_tree.lock);
Chris Mason7e383262008-04-09 16:28:12 -04001899 }
1900 if (!state || failrec->last_mirror > num_copies) {
1901 set_state_private(failure_tree, failrec->start, 0);
1902 clear_extent_bits(failure_tree, failrec->start,
1903 failrec->start + failrec->len - 1,
1904 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
1905 kfree(failrec);
1906 return -EIO;
1907 }
1908 bio = bio_alloc(GFP_NOFS, 1);
1909 bio->bi_private = state;
1910 bio->bi_end_io = failed_bio->bi_end_io;
1911 bio->bi_sector = failrec->logical >> 9;
1912 bio->bi_bdev = failed_bio->bi_bdev;
Chris Masone1c4b742008-04-22 13:26:46 -04001913 bio->bi_size = 0;
Chris Masond20f7042008-12-08 16:58:54 -05001914
Chris Mason7e383262008-04-09 16:28:12 -04001915 bio_add_page(bio, page, failrec->len, start - page_offset(page));
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001916 if (failed_bio->bi_rw & REQ_WRITE)
Chris Mason1259ab72008-05-12 13:39:03 -04001917 rw = WRITE;
1918 else
1919 rw = READ;
1920
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001921 ret = BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
Chris Masonc8b97812008-10-29 14:49:59 -04001922 failrec->last_mirror,
Chris Masoneaf25d92010-05-25 09:48:28 -04001923 failrec->bio_flags, 0);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001924 return ret;
Chris Mason1259ab72008-05-12 13:39:03 -04001925}
1926
Chris Masond352ac62008-09-29 15:18:18 -04001927/*
1928 * each time an IO finishes, we do a fast check in the IO failure tree
1929 * to see if we need to process or clean up an io_failure_record
1930 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001931static int btrfs_clean_io_failures(struct inode *inode, u64 start)
Chris Mason1259ab72008-05-12 13:39:03 -04001932{
1933 u64 private;
1934 u64 private_failure;
1935 struct io_failure_record *failure;
1936 int ret;
1937
1938 private = 0;
1939 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
Chris Masonec29ed52011-02-23 16:23:20 -05001940 (u64)-1, 1, EXTENT_DIRTY, 0)) {
Chris Mason1259ab72008-05-12 13:39:03 -04001941 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
1942 start, &private_failure);
1943 if (ret == 0) {
1944 failure = (struct io_failure_record *)(unsigned long)
1945 private_failure;
1946 set_state_private(&BTRFS_I(inode)->io_failure_tree,
1947 failure->start, 0);
1948 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
1949 failure->start,
1950 failure->start + failure->len - 1,
1951 EXTENT_DIRTY | EXTENT_LOCKED,
1952 GFP_NOFS);
1953 kfree(failure);
1954 }
1955 }
Chris Mason7e383262008-04-09 16:28:12 -04001956 return 0;
1957}
1958
Chris Masond352ac62008-09-29 15:18:18 -04001959/*
1960 * when reads are done, we need to check csums to verify the data is correct
1961 * if there's a match, we allow the bio to finish. If not, we go through
1962 * the io_failure_record routines to find good copies
1963 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001964static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason70dec802008-01-29 09:59:12 -05001965 struct extent_state *state)
Chris Mason07157aa2007-08-30 08:50:51 -04001966{
Chris Mason35ebb932007-10-30 16:56:53 -04001967 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -04001968 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05001969 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04001970 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -05001971 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -04001972 int ret;
Chris Masonff79f812007-10-15 16:22:25 -04001973 struct btrfs_root *root = BTRFS_I(inode)->root;
1974 u32 csum = ~(u32)0;
Chris Masond1310b22008-01-24 16:13:08 -05001975
Chris Masond20f7042008-12-08 16:58:54 -05001976 if (PageChecked(page)) {
1977 ClearPageChecked(page);
1978 goto good;
1979 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001980
1981 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Chris Masonb6cda9b2007-12-14 15:30:32 -05001982 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05001983
Yan Zheng17d217f2008-12-12 10:03:38 -05001984 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04001985 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001986 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
1987 GFP_NOFS);
1988 return 0;
1989 }
1990
Yanc2e639f2008-02-04 08:57:25 -05001991 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -05001992 private = state->private;
1993 ret = 0;
1994 } else {
1995 ret = get_state_private(io_tree, start, &private);
1996 }
Chris Mason9ab86c82009-01-07 09:48:51 -05001997 kaddr = kmap_atomic(page, KM_USER0);
Chris Masond3977122009-01-05 21:25:51 -05001998 if (ret)
Chris Mason07157aa2007-08-30 08:50:51 -04001999 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002000
Chris Masonff79f812007-10-15 16:22:25 -04002001 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
2002 btrfs_csum_final(csum, (char *)&csum);
Chris Masond3977122009-01-05 21:25:51 -05002003 if (csum != private)
Chris Mason07157aa2007-08-30 08:50:51 -04002004 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002005
Chris Mason9ab86c82009-01-07 09:48:51 -05002006 kunmap_atomic(kaddr, KM_USER0);
Chris Masond20f7042008-12-08 16:58:54 -05002007good:
Chris Mason7e383262008-04-09 16:28:12 -04002008 /* if the io failure tree for this inode is non-empty,
2009 * check to see if we've recovered from a failed IO
2010 */
Chris Mason1259ab72008-05-12 13:39:03 -04002011 btrfs_clean_io_failures(inode, start);
Chris Mason07157aa2007-08-30 08:50:51 -04002012 return 0;
2013
2014zeroit:
Chris Mason193f2842009-04-27 07:29:05 -04002015 if (printk_ratelimit()) {
Li Zefan33345d012011-04-20 10:31:50 +08002016 printk(KERN_INFO "btrfs csum failed ino %llu off %llu csum %u "
2017 "private %llu\n",
2018 (unsigned long long)btrfs_ino(page->mapping->host),
Chris Mason193f2842009-04-27 07:29:05 -04002019 (unsigned long long)start, csum,
2020 (unsigned long long)private);
2021 }
Chris Masondb945352007-10-15 16:15:53 -04002022 memset(kaddr + offset, 1, end - start + 1);
2023 flush_dcache_page(page);
Chris Mason9ab86c82009-01-07 09:48:51 -05002024 kunmap_atomic(kaddr, KM_USER0);
Chris Mason3b951512008-04-17 11:29:12 -04002025 if (private == 0)
2026 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04002027 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04002028}
Chris Masonb888db22007-08-27 16:49:44 -04002029
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002030struct delayed_iput {
2031 struct list_head list;
2032 struct inode *inode;
2033};
2034
2035void btrfs_add_delayed_iput(struct inode *inode)
2036{
2037 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2038 struct delayed_iput *delayed;
2039
2040 if (atomic_add_unless(&inode->i_count, -1, 1))
2041 return;
2042
2043 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2044 delayed->inode = inode;
2045
2046 spin_lock(&fs_info->delayed_iput_lock);
2047 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2048 spin_unlock(&fs_info->delayed_iput_lock);
2049}
2050
2051void btrfs_run_delayed_iputs(struct btrfs_root *root)
2052{
2053 LIST_HEAD(list);
2054 struct btrfs_fs_info *fs_info = root->fs_info;
2055 struct delayed_iput *delayed;
2056 int empty;
2057
2058 spin_lock(&fs_info->delayed_iput_lock);
2059 empty = list_empty(&fs_info->delayed_iputs);
2060 spin_unlock(&fs_info->delayed_iput_lock);
2061 if (empty)
2062 return;
2063
2064 down_read(&root->fs_info->cleanup_work_sem);
2065 spin_lock(&fs_info->delayed_iput_lock);
2066 list_splice_init(&fs_info->delayed_iputs, &list);
2067 spin_unlock(&fs_info->delayed_iput_lock);
2068
2069 while (!list_empty(&list)) {
2070 delayed = list_entry(list.next, struct delayed_iput, list);
2071 list_del(&delayed->list);
2072 iput(delayed->inode);
2073 kfree(delayed);
2074 }
2075 up_read(&root->fs_info->cleanup_work_sem);
2076}
2077
Josef Bacik7b128762008-07-24 12:17:14 -04002078/*
Yan, Zhengd68fc572010-05-16 10:49:58 -04002079 * calculate extra metadata reservation when snapshotting a subvolume
2080 * contains orphan files.
2081 */
2082void btrfs_orphan_pre_snapshot(struct btrfs_trans_handle *trans,
2083 struct btrfs_pending_snapshot *pending,
2084 u64 *bytes_to_reserve)
2085{
2086 struct btrfs_root *root;
2087 struct btrfs_block_rsv *block_rsv;
2088 u64 num_bytes;
2089 int index;
2090
2091 root = pending->root;
2092 if (!root->orphan_block_rsv || list_empty(&root->orphan_list))
2093 return;
2094
2095 block_rsv = root->orphan_block_rsv;
2096
2097 /* orphan block reservation for the snapshot */
2098 num_bytes = block_rsv->size;
2099
2100 /*
2101 * after the snapshot is created, COWing tree blocks may use more
2102 * space than it frees. So we should make sure there is enough
2103 * reserved space.
2104 */
2105 index = trans->transid & 0x1;
2106 if (block_rsv->reserved + block_rsv->freed[index] < block_rsv->size) {
2107 num_bytes += block_rsv->size -
2108 (block_rsv->reserved + block_rsv->freed[index]);
2109 }
2110
2111 *bytes_to_reserve += num_bytes;
2112}
2113
2114void btrfs_orphan_post_snapshot(struct btrfs_trans_handle *trans,
2115 struct btrfs_pending_snapshot *pending)
2116{
2117 struct btrfs_root *root = pending->root;
2118 struct btrfs_root *snap = pending->snap;
2119 struct btrfs_block_rsv *block_rsv;
2120 u64 num_bytes;
2121 int index;
2122 int ret;
2123
2124 if (!root->orphan_block_rsv || list_empty(&root->orphan_list))
2125 return;
2126
2127 /* refill source subvolume's orphan block reservation */
2128 block_rsv = root->orphan_block_rsv;
2129 index = trans->transid & 0x1;
2130 if (block_rsv->reserved + block_rsv->freed[index] < block_rsv->size) {
2131 num_bytes = block_rsv->size -
2132 (block_rsv->reserved + block_rsv->freed[index]);
2133 ret = btrfs_block_rsv_migrate(&pending->block_rsv,
2134 root->orphan_block_rsv,
2135 num_bytes);
2136 BUG_ON(ret);
2137 }
2138
2139 /* setup orphan block reservation for the snapshot */
2140 block_rsv = btrfs_alloc_block_rsv(snap);
2141 BUG_ON(!block_rsv);
2142
2143 btrfs_add_durable_block_rsv(root->fs_info, block_rsv);
2144 snap->orphan_block_rsv = block_rsv;
2145
2146 num_bytes = root->orphan_block_rsv->size;
2147 ret = btrfs_block_rsv_migrate(&pending->block_rsv,
2148 block_rsv, num_bytes);
2149 BUG_ON(ret);
2150
2151#if 0
2152 /* insert orphan item for the snapshot */
2153 WARN_ON(!root->orphan_item_inserted);
2154 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
2155 snap->root_key.objectid);
2156 BUG_ON(ret);
2157 snap->orphan_item_inserted = 1;
2158#endif
2159}
2160
2161enum btrfs_orphan_cleanup_state {
2162 ORPHAN_CLEANUP_STARTED = 1,
2163 ORPHAN_CLEANUP_DONE = 2,
2164};
2165
2166/*
2167 * This is called in transaction commmit time. If there are no orphan
2168 * files in the subvolume, it removes orphan item and frees block_rsv
2169 * structure.
2170 */
2171void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
2172 struct btrfs_root *root)
2173{
2174 int ret;
2175
2176 if (!list_empty(&root->orphan_list) ||
2177 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
2178 return;
2179
2180 if (root->orphan_item_inserted &&
2181 btrfs_root_refs(&root->root_item) > 0) {
2182 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
2183 root->root_key.objectid);
2184 BUG_ON(ret);
2185 root->orphan_item_inserted = 0;
2186 }
2187
2188 if (root->orphan_block_rsv) {
2189 WARN_ON(root->orphan_block_rsv->size > 0);
2190 btrfs_free_block_rsv(root, root->orphan_block_rsv);
2191 root->orphan_block_rsv = NULL;
2192 }
2193}
2194
2195/*
Josef Bacik7b128762008-07-24 12:17:14 -04002196 * This creates an orphan entry for the given inode in case something goes
2197 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04002198 *
2199 * NOTE: caller of this function should reserve 5 units of metadata for
2200 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04002201 */
2202int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2203{
2204 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002205 struct btrfs_block_rsv *block_rsv = NULL;
2206 int reserve = 0;
2207 int insert = 0;
2208 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002209
Yan, Zhengd68fc572010-05-16 10:49:58 -04002210 if (!root->orphan_block_rsv) {
2211 block_rsv = btrfs_alloc_block_rsv(root);
2212 BUG_ON(!block_rsv);
Josef Bacik7b128762008-07-24 12:17:14 -04002213 }
2214
Yan, Zhengd68fc572010-05-16 10:49:58 -04002215 spin_lock(&root->orphan_lock);
2216 if (!root->orphan_block_rsv) {
2217 root->orphan_block_rsv = block_rsv;
2218 } else if (block_rsv) {
2219 btrfs_free_block_rsv(root, block_rsv);
2220 block_rsv = NULL;
2221 }
Josef Bacik7b128762008-07-24 12:17:14 -04002222
Yan, Zhengd68fc572010-05-16 10:49:58 -04002223 if (list_empty(&BTRFS_I(inode)->i_orphan)) {
2224 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
2225#if 0
2226 /*
2227 * For proper ENOSPC handling, we should do orphan
2228 * cleanup when mounting. But this introduces backward
2229 * compatibility issue.
2230 */
2231 if (!xchg(&root->orphan_item_inserted, 1))
2232 insert = 2;
2233 else
2234 insert = 1;
2235#endif
2236 insert = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002237 }
Josef Bacik7b128762008-07-24 12:17:14 -04002238
Yan, Zhengd68fc572010-05-16 10:49:58 -04002239 if (!BTRFS_I(inode)->orphan_meta_reserved) {
2240 BTRFS_I(inode)->orphan_meta_reserved = 1;
2241 reserve = 1;
2242 }
2243 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002244
Yan, Zhengd68fc572010-05-16 10:49:58 -04002245 if (block_rsv)
2246 btrfs_add_durable_block_rsv(root->fs_info, block_rsv);
2247
2248 /* grab metadata reservation from transaction handle */
2249 if (reserve) {
2250 ret = btrfs_orphan_reserve_metadata(trans, inode);
2251 BUG_ON(ret);
2252 }
2253
2254 /* insert an orphan item to track this unlinked/truncated file */
2255 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08002256 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Yan, Zhengd68fc572010-05-16 10:49:58 -04002257 BUG_ON(ret);
2258 }
2259
2260 /* insert an orphan item to track subvolume contains orphan files */
2261 if (insert >= 2) {
2262 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
2263 root->root_key.objectid);
2264 BUG_ON(ret);
2265 }
2266 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002267}
2268
2269/*
2270 * We have done the truncate/delete so we can go ahead and remove the orphan
2271 * item for this particular inode.
2272 */
2273int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
2274{
2275 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002276 int delete_item = 0;
2277 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002278 int ret = 0;
2279
Yan, Zhengd68fc572010-05-16 10:49:58 -04002280 spin_lock(&root->orphan_lock);
2281 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
2282 list_del_init(&BTRFS_I(inode)->i_orphan);
2283 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04002284 }
2285
Yan, Zhengd68fc572010-05-16 10:49:58 -04002286 if (BTRFS_I(inode)->orphan_meta_reserved) {
2287 BTRFS_I(inode)->orphan_meta_reserved = 0;
2288 release_rsv = 1;
2289 }
2290 spin_unlock(&root->orphan_lock);
2291
2292 if (trans && delete_item) {
Li Zefan33345d012011-04-20 10:31:50 +08002293 ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode));
Yan, Zhengd68fc572010-05-16 10:49:58 -04002294 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04002295 }
2296
Yan, Zhengd68fc572010-05-16 10:49:58 -04002297 if (release_rsv)
2298 btrfs_orphan_release_metadata(inode);
Josef Bacik7b128762008-07-24 12:17:14 -04002299
Yan, Zhengd68fc572010-05-16 10:49:58 -04002300 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002301}
2302
2303/*
2304 * this cleans up any orphans that may be left on the list from the last use
2305 * of this root.
2306 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002307int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04002308{
2309 struct btrfs_path *path;
2310 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04002311 struct btrfs_key key, found_key;
2312 struct btrfs_trans_handle *trans;
2313 struct inode *inode;
2314 int ret = 0, nr_unlink = 0, nr_truncate = 0;
2315
Yan, Zhengd68fc572010-05-16 10:49:58 -04002316 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002317 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00002318
2319 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002320 if (!path) {
2321 ret = -ENOMEM;
2322 goto out;
2323 }
Josef Bacik7b128762008-07-24 12:17:14 -04002324 path->reada = -1;
2325
2326 key.objectid = BTRFS_ORPHAN_OBJECTID;
2327 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
2328 key.offset = (u64)-1;
2329
Josef Bacik7b128762008-07-24 12:17:14 -04002330 while (1) {
2331 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002332 if (ret < 0)
2333 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04002334
2335 /*
2336 * if ret == 0 means we found what we were searching for, which
2337 * is weird, but possible, so only screw with path if we didnt
2338 * find the key and see if we have stuff that matches
2339 */
2340 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002341 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002342 if (path->slots[0] == 0)
2343 break;
2344 path->slots[0]--;
2345 }
2346
2347 /* pull out the item */
2348 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04002349 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2350
2351 /* make sure the item matches what we want */
2352 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
2353 break;
2354 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
2355 break;
2356
2357 /* release the path since we're done with it */
2358 btrfs_release_path(root, path);
2359
2360 /*
2361 * this is where we are basically btrfs_lookup, without the
2362 * crossing root thing. we store the inode number in the
2363 * offset of the orphan item.
2364 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002365 found_key.objectid = found_key.offset;
2366 found_key.type = BTRFS_INODE_ITEM_KEY;
2367 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00002368 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002369 if (IS_ERR(inode)) {
2370 ret = PTR_ERR(inode);
2371 goto out;
2372 }
Josef Bacik7b128762008-07-24 12:17:14 -04002373
Josef Bacik7b128762008-07-24 12:17:14 -04002374 /*
2375 * add this inode to the orphan list so btrfs_orphan_del does
2376 * the proper thing when we hit it
2377 */
Yan, Zhengd68fc572010-05-16 10:49:58 -04002378 spin_lock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002379 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002380 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002381
2382 /*
2383 * if this is a bad inode, means we actually succeeded in
2384 * removing the inode, but not the orphan record, which means
2385 * we need to manually delete the orphan since iput will just
2386 * do a destroy_inode
2387 */
2388 if (is_bad_inode(inode)) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04002389 trans = btrfs_start_transaction(root, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002390 if (IS_ERR(trans)) {
2391 ret = PTR_ERR(trans);
2392 goto out;
2393 }
Josef Bacik7b128762008-07-24 12:17:14 -04002394 btrfs_orphan_del(trans, inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002395 btrfs_end_transaction(trans, root);
Josef Bacik7b128762008-07-24 12:17:14 -04002396 iput(inode);
2397 continue;
2398 }
2399
2400 /* if we have links, this was a truncate, lets do that */
2401 if (inode->i_nlink) {
Josef Bacika41ad392011-01-31 15:30:16 -05002402 if (!S_ISREG(inode->i_mode)) {
2403 WARN_ON(1);
2404 iput(inode);
2405 continue;
2406 }
Josef Bacik7b128762008-07-24 12:17:14 -04002407 nr_truncate++;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002408 ret = btrfs_truncate(inode);
Josef Bacik7b128762008-07-24 12:17:14 -04002409 } else {
2410 nr_unlink++;
2411 }
2412
2413 /* this will do delete_inode and everything for us */
2414 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002415 if (ret)
2416 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04002417 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04002418 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
2419
2420 if (root->orphan_block_rsv)
2421 btrfs_block_rsv_release(root, root->orphan_block_rsv,
2422 (u64)-1);
2423
2424 if (root->orphan_block_rsv || root->orphan_item_inserted) {
2425 trans = btrfs_join_transaction(root, 1);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002426 if (!IS_ERR(trans))
2427 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002428 }
Josef Bacik7b128762008-07-24 12:17:14 -04002429
2430 if (nr_unlink)
2431 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
2432 if (nr_truncate)
2433 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002434
2435out:
2436 if (ret)
2437 printk(KERN_CRIT "btrfs: could not do orphan cleanup %d\n", ret);
2438 btrfs_free_path(path);
2439 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002440}
2441
Chris Masond352ac62008-09-29 15:18:18 -04002442/*
Chris Mason46a53cc2009-04-27 11:47:50 -04002443 * very simple check to peek ahead in the leaf looking for xattrs. If we
2444 * don't find any xattrs, we know there can't be any acls.
2445 *
2446 * slot is the slot the inode is in, objectid is the objectid of the inode
2447 */
2448static noinline int acls_after_inode_item(struct extent_buffer *leaf,
2449 int slot, u64 objectid)
2450{
2451 u32 nritems = btrfs_header_nritems(leaf);
2452 struct btrfs_key found_key;
2453 int scanned = 0;
2454
2455 slot++;
2456 while (slot < nritems) {
2457 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2458
2459 /* we found a different objectid, there must not be acls */
2460 if (found_key.objectid != objectid)
2461 return 0;
2462
2463 /* we found an xattr, assume we've got an acl */
2464 if (found_key.type == BTRFS_XATTR_ITEM_KEY)
2465 return 1;
2466
2467 /*
2468 * we found a key greater than an xattr key, there can't
2469 * be any acls later on
2470 */
2471 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
2472 return 0;
2473
2474 slot++;
2475 scanned++;
2476
2477 /*
2478 * it goes inode, inode backrefs, xattrs, extents,
2479 * so if there are a ton of hard links to an inode there can
2480 * be a lot of backrefs. Don't waste time searching too hard,
2481 * this is just an optimization
2482 */
2483 if (scanned >= 8)
2484 break;
2485 }
2486 /* we hit the end of the leaf before we found an xattr or
2487 * something larger than an xattr. We have to assume the inode
2488 * has acls
2489 */
2490 return 1;
2491}
2492
2493/*
Chris Masond352ac62008-09-29 15:18:18 -04002494 * read an inode from the btree into the in-memory inode
2495 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002496static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002497{
2498 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002499 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002500 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04002501 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04002502 struct btrfs_root *root = BTRFS_I(inode)->root;
2503 struct btrfs_key location;
Chris Mason46a53cc2009-04-27 11:47:50 -04002504 int maybe_acls;
Chris Mason39279cc2007-06-12 06:35:45 -04002505 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -04002506 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04002507 int ret;
2508
2509 path = btrfs_alloc_path();
2510 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -04002511 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05002512
Chris Mason39279cc2007-06-12 06:35:45 -04002513 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002514 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04002515 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04002516
Chris Mason5f39d392007-10-15 16:14:19 -04002517 leaf = path->nodes[0];
2518 inode_item = btrfs_item_ptr(leaf, path->slots[0],
2519 struct btrfs_inode_item);
2520
2521 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
2522 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
2523 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
2524 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
Chris Masondbe674a2008-07-17 12:54:05 -04002525 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04002526
2527 tspec = btrfs_inode_atime(inode_item);
2528 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2529 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2530
2531 tspec = btrfs_inode_mtime(inode_item);
2532 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2533 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2534
2535 tspec = btrfs_inode_ctime(inode_item);
2536 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2537 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2538
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002539 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04002540 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Chris Masonc3027eb2008-12-08 16:40:21 -05002541 BTRFS_I(inode)->sequence = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04002542 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04002543 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002544 rdev = btrfs_inode_rdev(leaf, inode_item);
2545
Josef Bacikaec74772008-07-24 12:12:38 -04002546 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05002547 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Josef Bacikaec74772008-07-24 12:12:38 -04002548
Chris Mason5f39d392007-10-15 16:14:19 -04002549 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002550
Chris Mason46a53cc2009-04-27 11:47:50 -04002551 /*
2552 * try to precache a NULL acl entry for files that don't have
2553 * any xattrs or acls
2554 */
Li Zefan33345d012011-04-20 10:31:50 +08002555 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
2556 btrfs_ino(inode));
Al Viro72c04902009-06-24 16:58:48 -04002557 if (!maybe_acls)
2558 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04002559
Yan Zhengd2fb3432008-12-11 16:30:39 -05002560 BTRFS_I(inode)->block_group = btrfs_find_block_group(root, 0,
2561 alloc_group_block, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002562 btrfs_free_path(path);
2563 inode_item = NULL;
2564
Chris Mason39279cc2007-06-12 06:35:45 -04002565 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04002566 case S_IFREG:
2567 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04002568 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05002569 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002570 inode->i_fop = &btrfs_file_operations;
2571 inode->i_op = &btrfs_file_inode_operations;
2572 break;
2573 case S_IFDIR:
2574 inode->i_fop = &btrfs_dir_file_operations;
2575 if (root == root->fs_info->tree_root)
2576 inode->i_op = &btrfs_dir_ro_inode_operations;
2577 else
2578 inode->i_op = &btrfs_dir_inode_operations;
2579 break;
2580 case S_IFLNK:
2581 inode->i_op = &btrfs_symlink_inode_operations;
2582 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04002583 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04002584 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04002585 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05002586 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04002587 init_special_inode(inode, inode->i_mode, rdev);
2588 break;
Chris Mason39279cc2007-06-12 06:35:45 -04002589 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002590
2591 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002592 return;
2593
2594make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04002595 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002596 make_bad_inode(inode);
2597}
2598
Chris Masond352ac62008-09-29 15:18:18 -04002599/*
2600 * given a leaf and an inode, copy the inode fields into the leaf
2601 */
Chris Masone02119d2008-09-05 16:13:11 -04002602static void fill_inode_item(struct btrfs_trans_handle *trans,
2603 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04002604 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04002605 struct inode *inode)
2606{
Josef Bacik12ddb962011-04-05 13:02:27 -04002607 if (!leaf->map_token)
2608 map_private_extent_buffer(leaf, (unsigned long)item,
2609 sizeof(struct btrfs_inode_item),
2610 &leaf->map_token, &leaf->kaddr,
2611 &leaf->map_start, &leaf->map_len,
2612 KM_USER1);
2613
Chris Mason5f39d392007-10-15 16:14:19 -04002614 btrfs_set_inode_uid(leaf, item, inode->i_uid);
2615 btrfs_set_inode_gid(leaf, item, inode->i_gid);
Chris Masondbe674a2008-07-17 12:54:05 -04002616 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
Chris Mason5f39d392007-10-15 16:14:19 -04002617 btrfs_set_inode_mode(leaf, item, inode->i_mode);
2618 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
2619
2620 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
2621 inode->i_atime.tv_sec);
2622 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
2623 inode->i_atime.tv_nsec);
2624
2625 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
2626 inode->i_mtime.tv_sec);
2627 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
2628 inode->i_mtime.tv_nsec);
2629
2630 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
2631 inode->i_ctime.tv_sec);
2632 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
2633 inode->i_ctime.tv_nsec);
2634
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002635 btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode));
Chris Masone02119d2008-09-05 16:13:11 -04002636 btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
Chris Masonc3027eb2008-12-08 16:40:21 -05002637 btrfs_set_inode_sequence(leaf, item, BTRFS_I(inode)->sequence);
Chris Masone02119d2008-09-05 16:13:11 -04002638 btrfs_set_inode_transid(leaf, item, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002639 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -05002640 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002641 btrfs_set_inode_block_group(leaf, item, BTRFS_I(inode)->block_group);
Josef Bacik12ddb962011-04-05 13:02:27 -04002642
2643 if (leaf->map_token) {
2644 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2645 leaf->map_token = NULL;
2646 }
Chris Mason39279cc2007-06-12 06:35:45 -04002647}
2648
Chris Masond352ac62008-09-29 15:18:18 -04002649/*
2650 * copy everything in the in-memory inode into the btree.
2651 */
Chris Masond3977122009-01-05 21:25:51 -05002652noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
2653 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002654{
2655 struct btrfs_inode_item *inode_item;
2656 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002657 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002658 int ret;
2659
2660 path = btrfs_alloc_path();
2661 BUG_ON(!path);
Chris Masonb9473432009-03-13 11:00:37 -04002662 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002663 ret = btrfs_lookup_inode(trans, root, path,
2664 &BTRFS_I(inode)->location, 1);
2665 if (ret) {
2666 if (ret > 0)
2667 ret = -ENOENT;
2668 goto failed;
2669 }
2670
Chris Masonb4ce94d2009-02-04 09:25:08 -05002671 btrfs_unlock_up_safe(path, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002672 leaf = path->nodes[0];
2673 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04002674 struct btrfs_inode_item);
2675
Chris Masone02119d2008-09-05 16:13:11 -04002676 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002677 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002678 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002679 ret = 0;
2680failed:
Chris Mason39279cc2007-06-12 06:35:45 -04002681 btrfs_free_path(path);
2682 return ret;
2683}
2684
2685
Chris Masond352ac62008-09-29 15:18:18 -04002686/*
2687 * unlink helper that gets used here in inode.c and in the tree logging
2688 * recovery code. It remove a link in a directory with a given name, and
2689 * also drops the back refs in the inode to the directory
2690 */
Al Viro92986792011-03-04 17:14:37 +00002691static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2692 struct btrfs_root *root,
2693 struct inode *dir, struct inode *inode,
2694 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04002695{
2696 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04002697 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002698 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002699 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04002700 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04002701 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08002702 u64 ino = btrfs_ino(inode);
2703 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04002704
2705 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04002706 if (!path) {
2707 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00002708 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04002709 }
2710
Chris Masonb9473432009-03-13 11:00:37 -04002711 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08002712 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04002713 name, name_len, -1);
2714 if (IS_ERR(di)) {
2715 ret = PTR_ERR(di);
2716 goto err;
2717 }
2718 if (!di) {
2719 ret = -ENOENT;
2720 goto err;
2721 }
Chris Mason5f39d392007-10-15 16:14:19 -04002722 leaf = path->nodes[0];
2723 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04002724 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04002725 if (ret)
2726 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -04002727 btrfs_release_path(root, path);
2728
Li Zefan33345d012011-04-20 10:31:50 +08002729 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
2730 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04002731 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05002732 printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
Li Zefan33345d012011-04-20 10:31:50 +08002733 "inode %llu parent %llu\n", name_len, name,
2734 (unsigned long long)ino, (unsigned long long)dir_ino);
Josef Bacikaec74772008-07-24 12:12:38 -04002735 goto err;
2736 }
2737
Li Zefan33345d012011-04-20 10:31:50 +08002738 di = btrfs_lookup_dir_index_item(trans, root, path, dir_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04002739 index, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -04002740 if (IS_ERR(di)) {
2741 ret = PTR_ERR(di);
2742 goto err;
2743 }
2744 if (!di) {
2745 ret = -ENOENT;
2746 goto err;
2747 }
2748 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason925baed2008-06-25 16:01:30 -04002749 btrfs_release_path(root, path);
Chris Mason39279cc2007-06-12 06:35:45 -04002750
Chris Masone02119d2008-09-05 16:13:11 -04002751 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08002752 inode, dir_ino);
Chris Mason49eb7e42008-09-11 15:53:12 -04002753 BUG_ON(ret != 0 && ret != -ENOENT);
Chris Masone02119d2008-09-05 16:13:11 -04002754
2755 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
2756 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04002757 if (ret == -ENOENT)
2758 ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002759err:
2760 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002761 if (ret)
2762 goto out;
2763
2764 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
2765 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2766 btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04002767out:
Chris Mason39279cc2007-06-12 06:35:45 -04002768 return ret;
2769}
2770
Al Viro92986792011-03-04 17:14:37 +00002771int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2772 struct btrfs_root *root,
2773 struct inode *dir, struct inode *inode,
2774 const char *name, int name_len)
2775{
2776 int ret;
2777 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
2778 if (!ret) {
2779 btrfs_drop_nlink(inode);
2780 ret = btrfs_update_inode(trans, root, inode);
2781 }
2782 return ret;
2783}
2784
2785
Yan, Zhenga22285a2010-05-16 10:48:46 -04002786/* helper to check if there is any shared block in the path */
2787static int check_path_shared(struct btrfs_root *root,
2788 struct btrfs_path *path)
2789{
2790 struct extent_buffer *eb;
2791 int level;
Dan Carpenter0e4dcbe2010-06-01 08:23:11 +00002792 u64 refs = 1;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002793
2794 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
Josef Bacikdedefd72011-01-24 21:43:18 +00002795 int ret;
2796
Yan, Zhenga22285a2010-05-16 10:48:46 -04002797 if (!path->nodes[level])
2798 break;
2799 eb = path->nodes[level];
2800 if (!btrfs_block_can_be_shared(root, eb))
2801 continue;
2802 ret = btrfs_lookup_extent_info(NULL, root, eb->start, eb->len,
2803 &refs, NULL);
2804 if (refs > 1)
2805 return 1;
2806 }
Josef Bacikdedefd72011-01-24 21:43:18 +00002807 return 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002808}
2809
2810/*
2811 * helper to start transaction for unlink and rmdir.
2812 *
2813 * unlink and rmdir are special in btrfs, they do not always free space.
2814 * so in enospc case, we should make sure they will free space before
2815 * allowing them to use the global metadata reservation.
2816 */
2817static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
2818 struct dentry *dentry)
2819{
2820 struct btrfs_trans_handle *trans;
2821 struct btrfs_root *root = BTRFS_I(dir)->root;
2822 struct btrfs_path *path;
2823 struct btrfs_inode_ref *ref;
2824 struct btrfs_dir_item *di;
2825 struct inode *inode = dentry->d_inode;
2826 u64 index;
2827 int check_link = 1;
2828 int err = -ENOSPC;
2829 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08002830 u64 ino = btrfs_ino(inode);
2831 u64 dir_ino = btrfs_ino(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002832
2833 trans = btrfs_start_transaction(root, 10);
2834 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
2835 return trans;
2836
Li Zefan33345d012011-04-20 10:31:50 +08002837 if (ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhenga22285a2010-05-16 10:48:46 -04002838 return ERR_PTR(-ENOSPC);
2839
2840 /* check if there is someone else holds reference */
2841 if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1)
2842 return ERR_PTR(-ENOSPC);
2843
2844 if (atomic_read(&inode->i_count) > 2)
2845 return ERR_PTR(-ENOSPC);
2846
2847 if (xchg(&root->fs_info->enospc_unlink, 1))
2848 return ERR_PTR(-ENOSPC);
2849
2850 path = btrfs_alloc_path();
2851 if (!path) {
2852 root->fs_info->enospc_unlink = 0;
2853 return ERR_PTR(-ENOMEM);
2854 }
2855
2856 trans = btrfs_start_transaction(root, 0);
2857 if (IS_ERR(trans)) {
2858 btrfs_free_path(path);
2859 root->fs_info->enospc_unlink = 0;
2860 return trans;
2861 }
2862
2863 path->skip_locking = 1;
2864 path->search_commit_root = 1;
2865
2866 ret = btrfs_lookup_inode(trans, root, path,
2867 &BTRFS_I(dir)->location, 0);
2868 if (ret < 0) {
2869 err = ret;
2870 goto out;
2871 }
2872 if (ret == 0) {
2873 if (check_path_shared(root, path))
2874 goto out;
2875 } else {
2876 check_link = 0;
2877 }
2878 btrfs_release_path(root, path);
2879
2880 ret = btrfs_lookup_inode(trans, root, path,
2881 &BTRFS_I(inode)->location, 0);
2882 if (ret < 0) {
2883 err = ret;
2884 goto out;
2885 }
2886 if (ret == 0) {
2887 if (check_path_shared(root, path))
2888 goto out;
2889 } else {
2890 check_link = 0;
2891 }
2892 btrfs_release_path(root, path);
2893
2894 if (ret == 0 && S_ISREG(inode->i_mode)) {
2895 ret = btrfs_lookup_file_extent(trans, root, path,
Li Zefan33345d012011-04-20 10:31:50 +08002896 ino, (u64)-1, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002897 if (ret < 0) {
2898 err = ret;
2899 goto out;
2900 }
2901 BUG_ON(ret == 0);
2902 if (check_path_shared(root, path))
2903 goto out;
2904 btrfs_release_path(root, path);
2905 }
2906
2907 if (!check_link) {
2908 err = 0;
2909 goto out;
2910 }
2911
Li Zefan33345d012011-04-20 10:31:50 +08002912 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zhenga22285a2010-05-16 10:48:46 -04002913 dentry->d_name.name, dentry->d_name.len, 0);
2914 if (IS_ERR(di)) {
2915 err = PTR_ERR(di);
2916 goto out;
2917 }
2918 if (di) {
2919 if (check_path_shared(root, path))
2920 goto out;
2921 } else {
2922 err = 0;
2923 goto out;
2924 }
2925 btrfs_release_path(root, path);
2926
2927 ref = btrfs_lookup_inode_ref(trans, root, path,
2928 dentry->d_name.name, dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08002929 ino, dir_ino, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002930 if (IS_ERR(ref)) {
2931 err = PTR_ERR(ref);
2932 goto out;
2933 }
2934 BUG_ON(!ref);
2935 if (check_path_shared(root, path))
2936 goto out;
2937 index = btrfs_inode_ref_index(path->nodes[0], ref);
2938 btrfs_release_path(root, path);
2939
Li Zefan33345d012011-04-20 10:31:50 +08002940 di = btrfs_lookup_dir_index_item(trans, root, path, dir_ino, index,
Yan, Zhenga22285a2010-05-16 10:48:46 -04002941 dentry->d_name.name, dentry->d_name.len, 0);
2942 if (IS_ERR(di)) {
2943 err = PTR_ERR(di);
2944 goto out;
2945 }
2946 BUG_ON(ret == -ENOENT);
2947 if (check_path_shared(root, path))
2948 goto out;
2949
2950 err = 0;
2951out:
2952 btrfs_free_path(path);
2953 if (err) {
2954 btrfs_end_transaction(trans, root);
2955 root->fs_info->enospc_unlink = 0;
2956 return ERR_PTR(err);
2957 }
2958
2959 trans->block_rsv = &root->fs_info->global_block_rsv;
2960 return trans;
2961}
2962
2963static void __unlink_end_trans(struct btrfs_trans_handle *trans,
2964 struct btrfs_root *root)
2965{
2966 if (trans->block_rsv == &root->fs_info->global_block_rsv) {
2967 BUG_ON(!root->fs_info->enospc_unlink);
2968 root->fs_info->enospc_unlink = 0;
2969 }
2970 btrfs_end_transaction_throttle(trans, root);
2971}
2972
Chris Mason39279cc2007-06-12 06:35:45 -04002973static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
2974{
Yan, Zhenga22285a2010-05-16 10:48:46 -04002975 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04002976 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04002977 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002978 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -05002979 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002980
Yan, Zhenga22285a2010-05-16 10:48:46 -04002981 trans = __unlink_start_trans(dir, dentry);
2982 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05002983 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04002984
Chris Mason39279cc2007-06-12 06:35:45 -04002985 btrfs_set_trans_block_group(trans, dir);
Chris Mason12fcfd22009-03-24 10:24:20 -04002986
2987 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
2988
Chris Masone02119d2008-09-05 16:13:11 -04002989 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
2990 dentry->d_name.name, dentry->d_name.len);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002991 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04002992
Yan, Zhenga22285a2010-05-16 10:48:46 -04002993 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04002994 ret = btrfs_orphan_add(trans, inode);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002995 BUG_ON(ret);
2996 }
Josef Bacik7b128762008-07-24 12:17:14 -04002997
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002998 nr = trans->blocks_used;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002999 __unlink_end_trans(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003000 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003001 return ret;
3002}
3003
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003004int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3005 struct btrfs_root *root,
3006 struct inode *dir, u64 objectid,
3007 const char *name, int name_len)
3008{
3009 struct btrfs_path *path;
3010 struct extent_buffer *leaf;
3011 struct btrfs_dir_item *di;
3012 struct btrfs_key key;
3013 u64 index;
3014 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003015 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003016
3017 path = btrfs_alloc_path();
3018 if (!path)
3019 return -ENOMEM;
3020
Li Zefan33345d012011-04-20 10:31:50 +08003021 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003022 name, name_len, -1);
3023 BUG_ON(!di || IS_ERR(di));
3024
3025 leaf = path->nodes[0];
3026 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3027 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3028 ret = btrfs_delete_one_dir_name(trans, root, path, di);
3029 BUG_ON(ret);
3030 btrfs_release_path(root, path);
3031
3032 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3033 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003034 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003035 if (ret < 0) {
3036 BUG_ON(ret != -ENOENT);
Li Zefan33345d012011-04-20 10:31:50 +08003037 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003038 name, name_len);
3039 BUG_ON(!di || IS_ERR(di));
3040
3041 leaf = path->nodes[0];
3042 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3043 btrfs_release_path(root, path);
3044 index = key.offset;
3045 }
3046
Li Zefan33345d012011-04-20 10:31:50 +08003047 di = btrfs_lookup_dir_index_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003048 index, name, name_len, -1);
3049 BUG_ON(!di || IS_ERR(di));
3050
3051 leaf = path->nodes[0];
3052 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3053 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3054 ret = btrfs_delete_one_dir_name(trans, root, path, di);
3055 BUG_ON(ret);
3056 btrfs_release_path(root, path);
3057
3058 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
3059 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
3060 ret = btrfs_update_inode(trans, root, dir);
3061 BUG_ON(ret);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003062
3063 btrfs_free_path(path);
3064 return 0;
3065}
3066
Chris Mason39279cc2007-06-12 06:35:45 -04003067static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3068{
3069 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003070 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003071 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003072 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -05003073 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003074
Chris Mason3394e162008-11-17 20:42:26 -05003075 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE ||
Li Zefan33345d012011-04-20 10:31:50 +08003076 btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
Yan134d4512007-10-25 15:49:25 -04003077 return -ENOTEMPTY;
3078
Yan, Zhenga22285a2010-05-16 10:48:46 -04003079 trans = __unlink_start_trans(dir, dentry);
3080 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003081 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003082
Chris Mason39279cc2007-06-12 06:35:45 -04003083 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003084
Li Zefan33345d012011-04-20 10:31:50 +08003085 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003086 err = btrfs_unlink_subvol(trans, root, dir,
3087 BTRFS_I(inode)->location.objectid,
3088 dentry->d_name.name,
3089 dentry->d_name.len);
3090 goto out;
3091 }
3092
Josef Bacik7b128762008-07-24 12:17:14 -04003093 err = btrfs_orphan_add(trans, inode);
3094 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003095 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003096
Chris Mason39279cc2007-06-12 06:35:45 -04003097 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04003098 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3099 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05003100 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04003101 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003102out:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003103 nr = trans->blocks_used;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003104 __unlink_end_trans(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003105 btrfs_btree_balance_dirty(root, nr);
Chris Mason39544012007-12-12 14:38:19 -05003106
Chris Mason39279cc2007-06-12 06:35:45 -04003107 return err;
3108}
3109
Chris Masond20f7042008-12-08 16:58:54 -05003110#if 0
Chris Mason39279cc2007-06-12 06:35:45 -04003111/*
Chris Mason323ac952008-10-01 19:05:46 -04003112 * when truncating bytes in a file, it is possible to avoid reading
3113 * the leaves that contain only checksum items. This can be the
3114 * majority of the IO required to delete a large file, but it must
3115 * be done carefully.
3116 *
3117 * The keys in the level just above the leaves are checked to make sure
3118 * the lowest key in a given leaf is a csum key, and starts at an offset
3119 * after the new size.
3120 *
3121 * Then the key for the next leaf is checked to make sure it also has
3122 * a checksum item for the same file. If it does, we know our target leaf
3123 * contains only checksum items, and it can be safely freed without reading
3124 * it.
3125 *
3126 * This is just an optimization targeted at large files. It may do
3127 * nothing. It will return 0 unless things went badly.
3128 */
3129static noinline int drop_csum_leaves(struct btrfs_trans_handle *trans,
3130 struct btrfs_root *root,
3131 struct btrfs_path *path,
3132 struct inode *inode, u64 new_size)
3133{
3134 struct btrfs_key key;
3135 int ret;
3136 int nritems;
3137 struct btrfs_key found_key;
3138 struct btrfs_key other_key;
Yan Zheng5b84e8d2008-10-09 11:46:19 -04003139 struct btrfs_leaf_ref *ref;
3140 u64 leaf_gen;
3141 u64 leaf_start;
Chris Mason323ac952008-10-01 19:05:46 -04003142
3143 path->lowest_level = 1;
3144 key.objectid = inode->i_ino;
3145 key.type = BTRFS_CSUM_ITEM_KEY;
3146 key.offset = new_size;
3147again:
3148 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3149 if (ret < 0)
3150 goto out;
3151
3152 if (path->nodes[1] == NULL) {
3153 ret = 0;
3154 goto out;
3155 }
3156 ret = 0;
3157 btrfs_node_key_to_cpu(path->nodes[1], &found_key, path->slots[1]);
3158 nritems = btrfs_header_nritems(path->nodes[1]);
3159
3160 if (!nritems)
3161 goto out;
3162
3163 if (path->slots[1] >= nritems)
3164 goto next_node;
3165
3166 /* did we find a key greater than anything we want to delete? */
3167 if (found_key.objectid > inode->i_ino ||
3168 (found_key.objectid == inode->i_ino && found_key.type > key.type))
3169 goto out;
3170
3171 /* we check the next key in the node to make sure the leave contains
3172 * only checksum items. This comparison doesn't work if our
3173 * leaf is the last one in the node
3174 */
3175 if (path->slots[1] + 1 >= nritems) {
3176next_node:
3177 /* search forward from the last key in the node, this
3178 * will bring us into the next node in the tree
3179 */
3180 btrfs_node_key_to_cpu(path->nodes[1], &found_key, nritems - 1);
3181
3182 /* unlikely, but we inc below, so check to be safe */
3183 if (found_key.offset == (u64)-1)
3184 goto out;
3185
3186 /* search_forward needs a path with locks held, do the
3187 * search again for the original key. It is possible
3188 * this will race with a balance and return a path that
3189 * we could modify, but this drop is just an optimization
3190 * and is allowed to miss some leaves.
3191 */
3192 btrfs_release_path(root, path);
3193 found_key.offset++;
3194
3195 /* setup a max key for search_forward */
3196 other_key.offset = (u64)-1;
3197 other_key.type = key.type;
3198 other_key.objectid = key.objectid;
3199
3200 path->keep_locks = 1;
3201 ret = btrfs_search_forward(root, &found_key, &other_key,
3202 path, 0, 0);
3203 path->keep_locks = 0;
3204 if (ret || found_key.objectid != key.objectid ||
3205 found_key.type != key.type) {
3206 ret = 0;
3207 goto out;
3208 }
3209
3210 key.offset = found_key.offset;
3211 btrfs_release_path(root, path);
3212 cond_resched();
3213 goto again;
3214 }
3215
3216 /* we know there's one more slot after us in the tree,
3217 * read that key so we can verify it is also a checksum item
3218 */
3219 btrfs_node_key_to_cpu(path->nodes[1], &other_key, path->slots[1] + 1);
3220
3221 if (found_key.objectid < inode->i_ino)
3222 goto next_key;
3223
3224 if (found_key.type != key.type || found_key.offset < new_size)
3225 goto next_key;
3226
3227 /*
3228 * if the key for the next leaf isn't a csum key from this objectid,
3229 * we can't be sure there aren't good items inside this leaf.
3230 * Bail out
3231 */
3232 if (other_key.objectid != inode->i_ino || other_key.type != key.type)
3233 goto out;
3234
Yan Zheng5b84e8d2008-10-09 11:46:19 -04003235 leaf_start = btrfs_node_blockptr(path->nodes[1], path->slots[1]);
3236 leaf_gen = btrfs_node_ptr_generation(path->nodes[1], path->slots[1]);
Chris Mason323ac952008-10-01 19:05:46 -04003237 /*
3238 * it is safe to delete this leaf, it contains only
3239 * csum items from this inode at an offset >= new_size
3240 */
Yan Zheng5b84e8d2008-10-09 11:46:19 -04003241 ret = btrfs_del_leaf(trans, root, path, leaf_start);
Chris Mason323ac952008-10-01 19:05:46 -04003242 BUG_ON(ret);
3243
Yan Zheng5b84e8d2008-10-09 11:46:19 -04003244 if (root->ref_cows && leaf_gen < trans->transid) {
3245 ref = btrfs_alloc_leaf_ref(root, 0);
3246 if (ref) {
3247 ref->root_gen = root->root_key.offset;
3248 ref->bytenr = leaf_start;
3249 ref->owner = 0;
3250 ref->generation = leaf_gen;
3251 ref->nritems = 0;
3252
Chris Masonbd56b302009-02-04 09:27:02 -05003253 btrfs_sort_leaf_ref(ref);
3254
Yan Zheng5b84e8d2008-10-09 11:46:19 -04003255 ret = btrfs_add_leaf_ref(root, ref, 0);
3256 WARN_ON(ret);
3257 btrfs_free_leaf_ref(root, ref);
3258 } else {
3259 WARN_ON(1);
3260 }
3261 }
Chris Mason323ac952008-10-01 19:05:46 -04003262next_key:
3263 btrfs_release_path(root, path);
3264
3265 if (other_key.objectid == inode->i_ino &&
3266 other_key.type == key.type && other_key.offset > key.offset) {
3267 key.offset = other_key.offset;
3268 cond_resched();
3269 goto again;
3270 }
3271 ret = 0;
3272out:
3273 /* fixup any changes we've made to the path */
3274 path->lowest_level = 0;
3275 path->keep_locks = 0;
3276 btrfs_release_path(root, path);
3277 return ret;
3278}
3279
Chris Masond20f7042008-12-08 16:58:54 -05003280#endif
3281
Chris Mason323ac952008-10-01 19:05:46 -04003282/*
Chris Mason39279cc2007-06-12 06:35:45 -04003283 * this can truncate away extent items, csum items and directory items.
3284 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04003285 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04003286 *
3287 * csum items that cross the new i_size are truncated to the new size
3288 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04003289 *
3290 * min_type is the minimum key type to truncate down to. If set to 0, this
3291 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04003292 */
Yan, Zheng80825102009-11-12 09:35:36 +00003293int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
3294 struct btrfs_root *root,
3295 struct inode *inode,
3296 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003297{
Chris Mason39279cc2007-06-12 06:35:45 -04003298 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003299 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003300 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00003301 struct btrfs_key key;
3302 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04003303 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04003304 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003305 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003306 u64 item_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00003307 u64 mask = root->sectorsize - 1;
3308 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04003309 int found_extent;
3310 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003311 int pending_del_nr = 0;
3312 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04003313 int extent_type = -1;
Chris Mason771ed682008-11-06 22:02:51 -05003314 int encoding;
Yan, Zheng80825102009-11-12 09:35:36 +00003315 int ret;
3316 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08003317 u64 ino = btrfs_ino(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003318
3319 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04003320
Josef Bacik0af3d002010-06-21 14:48:16 -04003321 if (root->ref_cows || root == root->fs_info->tree_root)
Zheng Yan5b21f2e2008-09-26 10:05:38 -04003322 btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003323
Chris Mason39279cc2007-06-12 06:35:45 -04003324 path = btrfs_alloc_path();
3325 BUG_ON(!path);
Julia Lawall33c17ad2009-07-22 16:49:01 -04003326 path->reada = -1;
Chris Mason5f39d392007-10-15 16:14:19 -04003327
Li Zefan33345d012011-04-20 10:31:50 +08003328 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04003329 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04003330 key.type = (u8)-1;
3331
Chris Mason85e21ba2008-01-29 15:11:36 -05003332search_again:
Chris Masonb9473432009-03-13 11:00:37 -04003333 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05003334 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00003335 if (ret < 0) {
3336 err = ret;
3337 goto out;
3338 }
Chris Masond3977122009-01-05 21:25:51 -05003339
Chris Mason85e21ba2008-01-29 15:11:36 -05003340 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003341 /* there are no items in the tree for us to truncate, we're
3342 * done
3343 */
Yan, Zheng80825102009-11-12 09:35:36 +00003344 if (path->slots[0] == 0)
3345 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05003346 path->slots[0]--;
3347 }
3348
Chris Masond3977122009-01-05 21:25:51 -05003349 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04003350 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04003351 leaf = path->nodes[0];
3352 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3353 found_type = btrfs_key_type(&found_key);
Chris Mason771ed682008-11-06 22:02:51 -05003354 encoding = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003355
Li Zefan33345d012011-04-20 10:31:50 +08003356 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04003357 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003358
Chris Mason85e21ba2008-01-29 15:11:36 -05003359 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003360 break;
3361
Chris Mason5f39d392007-10-15 16:14:19 -04003362 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04003363 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04003364 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04003365 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04003366 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Mason771ed682008-11-06 22:02:51 -05003367 encoding = btrfs_file_extent_compression(leaf, fi);
3368 encoding |= btrfs_file_extent_encryption(leaf, fi);
3369 encoding |= btrfs_file_extent_other_encoding(leaf, fi);
3370
Chris Mason179e29e2007-11-01 11:28:41 -04003371 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04003372 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04003373 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04003374 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04003375 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04003376 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003377 }
Yan008630c2007-11-07 13:31:09 -05003378 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04003379 }
Yan, Zheng80825102009-11-12 09:35:36 +00003380 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04003381 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00003382 } else {
3383 if (item_end < new_size)
3384 break;
3385 if (found_key.offset >= new_size)
3386 del_item = 1;
3387 else
3388 del_item = 0;
3389 }
Chris Mason39279cc2007-06-12 06:35:45 -04003390 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003391 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04003392 if (found_type != BTRFS_EXTENT_DATA_KEY)
3393 goto delete;
3394
3395 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04003396 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04003397 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason771ed682008-11-06 22:02:51 -05003398 if (!del_item && !encoding) {
Chris Masondb945352007-10-15 16:15:53 -04003399 u64 orig_num_bytes =
3400 btrfs_file_extent_num_bytes(leaf, fi);
Chris Masone02119d2008-09-05 16:13:11 -04003401 extent_num_bytes = new_size -
Chris Mason5f39d392007-10-15 16:14:19 -04003402 found_key.offset + root->sectorsize - 1;
Yanb1632b12008-01-30 11:54:04 -05003403 extent_num_bytes = extent_num_bytes &
3404 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04003405 btrfs_set_file_extent_num_bytes(leaf, fi,
3406 extent_num_bytes);
3407 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05003408 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04003409 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003410 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04003411 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003412 } else {
Chris Masondb945352007-10-15 16:15:53 -04003413 extent_num_bytes =
3414 btrfs_file_extent_disk_num_bytes(leaf,
3415 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003416 extent_offset = found_key.offset -
3417 btrfs_file_extent_offset(leaf, fi);
3418
Chris Mason39279cc2007-06-12 06:35:45 -04003419 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05003420 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003421 if (extent_start != 0) {
3422 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04003423 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003424 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04003425 }
3426 }
Chris Mason90692182008-02-08 13:49:28 -05003427 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04003428 /*
3429 * we can't truncate inline items that have had
3430 * special encodings
3431 */
3432 if (!del_item &&
3433 btrfs_file_extent_compression(leaf, fi) == 0 &&
3434 btrfs_file_extent_encryption(leaf, fi) == 0 &&
3435 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003436 u32 size = new_size - found_key.offset;
3437
3438 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003439 inode_sub_bytes(inode, item_end + 1 -
3440 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04003441 }
3442 size =
3443 btrfs_file_extent_calc_inline_size(size);
Chris Mason90692182008-02-08 13:49:28 -05003444 ret = btrfs_truncate_item(trans, root, path,
Chris Masone02119d2008-09-05 16:13:11 -04003445 size, 1);
Chris Mason90692182008-02-08 13:49:28 -05003446 BUG_ON(ret);
Chris Masone02119d2008-09-05 16:13:11 -04003447 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003448 inode_sub_bytes(inode, item_end + 1 -
3449 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05003450 }
Chris Mason39279cc2007-06-12 06:35:45 -04003451 }
Chris Mason179e29e2007-11-01 11:28:41 -04003452delete:
Chris Mason39279cc2007-06-12 06:35:45 -04003453 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05003454 if (!pending_del_nr) {
3455 /* no pending yet, add ourselves */
3456 pending_del_slot = path->slots[0];
3457 pending_del_nr = 1;
3458 } else if (pending_del_nr &&
3459 path->slots[0] + 1 == pending_del_slot) {
3460 /* hop on the pending chunk */
3461 pending_del_nr++;
3462 pending_del_slot = path->slots[0];
3463 } else {
Chris Masond3977122009-01-05 21:25:51 -05003464 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05003465 }
Chris Mason39279cc2007-06-12 06:35:45 -04003466 } else {
3467 break;
3468 }
Josef Bacik0af3d002010-06-21 14:48:16 -04003469 if (found_extent && (root->ref_cows ||
3470 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04003471 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003472 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003473 extent_num_bytes, 0,
3474 btrfs_header_owner(leaf),
Li Zefan33345d012011-04-20 10:31:50 +08003475 ino, extent_offset);
Chris Mason39279cc2007-06-12 06:35:45 -04003476 BUG_ON(ret);
3477 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003478
Yan, Zheng80825102009-11-12 09:35:36 +00003479 if (found_type == BTRFS_INODE_ITEM_KEY)
3480 break;
3481
3482 if (path->slots[0] == 0 ||
3483 path->slots[0] != pending_del_slot) {
Li Zefan82d59022011-04-20 10:33:24 +08003484 if (root->ref_cows &&
3485 BTRFS_I(inode)->location.objectid !=
3486 BTRFS_FREE_INO_OBJECTID) {
Yan, Zheng80825102009-11-12 09:35:36 +00003487 err = -EAGAIN;
3488 goto out;
3489 }
3490 if (pending_del_nr) {
3491 ret = btrfs_del_items(trans, root, path,
3492 pending_del_slot,
3493 pending_del_nr);
3494 BUG_ON(ret);
3495 pending_del_nr = 0;
3496 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003497 btrfs_release_path(root, path);
3498 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00003499 } else {
3500 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05003501 }
Chris Mason39279cc2007-06-12 06:35:45 -04003502 }
Yan, Zheng80825102009-11-12 09:35:36 +00003503out:
Chris Mason85e21ba2008-01-29 15:11:36 -05003504 if (pending_del_nr) {
3505 ret = btrfs_del_items(trans, root, path, pending_del_slot,
3506 pending_del_nr);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003507 BUG_ON(ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05003508 }
Chris Mason39279cc2007-06-12 06:35:45 -04003509 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00003510 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003511}
3512
Chris Masona52d9a82007-08-27 16:49:44 -04003513/*
3514 * taken from block_truncate_page, but does cow as it zeros out
3515 * any bytes left in the last page in the file.
3516 */
3517static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
3518{
3519 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -04003520 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003521 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3522 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003523 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003524 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04003525 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04003526 pgoff_t index = from >> PAGE_CACHE_SHIFT;
3527 unsigned offset = from & (PAGE_CACHE_SIZE-1);
3528 struct page *page;
3529 int ret = 0;
3530 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003531 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04003532
3533 if ((offset & (blocksize - 1)) == 0)
3534 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003535 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003536 if (ret)
3537 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04003538
3539 ret = -ENOMEM;
Chris Mason211c17f2008-05-15 09:13:45 -04003540again:
Chris Masona52d9a82007-08-27 16:49:44 -04003541 page = grab_cache_page(mapping, index);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003542 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003543 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Masona52d9a82007-08-27 16:49:44 -04003544 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04003545 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003546
3547 page_start = page_offset(page);
3548 page_end = page_start + PAGE_CACHE_SIZE - 1;
3549
Chris Masona52d9a82007-08-27 16:49:44 -04003550 if (!PageUptodate(page)) {
3551 ret = btrfs_readpage(NULL, page);
3552 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04003553 if (page->mapping != mapping) {
3554 unlock_page(page);
3555 page_cache_release(page);
3556 goto again;
3557 }
Chris Masona52d9a82007-08-27 16:49:44 -04003558 if (!PageUptodate(page)) {
3559 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04003560 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04003561 }
3562 }
Chris Mason211c17f2008-05-15 09:13:45 -04003563 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003564
Josef Bacik2ac55d42010-02-03 19:33:23 +00003565 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state,
3566 GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003567 set_page_extent_mapped(page);
3568
3569 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3570 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00003571 unlock_extent_cached(io_tree, page_start, page_end,
3572 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003573 unlock_page(page);
3574 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04003575 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003576 btrfs_put_ordered_extent(ordered);
3577 goto again;
3578 }
3579
Josef Bacik2ac55d42010-02-03 19:33:23 +00003580 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Josef Bacik5d5e1032009-10-13 16:46:49 -04003581 EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003582 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003583
Josef Bacik2ac55d42010-02-03 19:33:23 +00003584 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
3585 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003586 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00003587 unlock_extent_cached(io_tree, page_start, page_end,
3588 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003589 goto out_unlock;
3590 }
3591
Chris Masone6dcd2d2008-07-17 12:53:50 -04003592 ret = 0;
3593 if (offset != PAGE_CACHE_SIZE) {
3594 kaddr = kmap(page);
3595 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
3596 flush_dcache_page(page);
3597 kunmap(page);
3598 }
Chris Mason247e7432008-07-17 12:53:51 -04003599 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003600 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003601 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
3602 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04003603
Chris Mason89642222008-07-24 09:41:53 -04003604out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04003605 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003606 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04003607 unlock_page(page);
3608 page_cache_release(page);
3609out:
3610 return ret;
3611}
3612
Josef Bacik695a0d02011-03-04 15:46:53 -05003613/*
3614 * This function puts in dummy file extents for the area we're creating a hole
3615 * for. So if we are truncating this file to a larger size we need to insert
3616 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
3617 * the range between oldsize and size
3618 */
Josef Bacika41ad392011-01-31 15:30:16 -05003619int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04003620{
3621 struct btrfs_trans_handle *trans;
3622 struct btrfs_root *root = BTRFS_I(inode)->root;
3623 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003624 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003625 struct extent_state *cached_state = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04003626 u64 mask = root->sectorsize - 1;
Josef Bacika41ad392011-01-31 15:30:16 -05003627 u64 hole_start = (oldsize + mask) & ~mask;
Yan Zheng9036c102008-10-30 14:19:41 -04003628 u64 block_end = (size + mask) & ~mask;
3629 u64 last_byte;
3630 u64 cur_offset;
3631 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003632 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04003633
3634 if (size <= hole_start)
3635 return 0;
3636
Yan Zheng9036c102008-10-30 14:19:41 -04003637 while (1) {
3638 struct btrfs_ordered_extent *ordered;
3639 btrfs_wait_ordered_range(inode, hole_start,
3640 block_end - hole_start);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003641 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
3642 &cached_state, GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003643 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
3644 if (!ordered)
3645 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003646 unlock_extent_cached(io_tree, hole_start, block_end - 1,
3647 &cached_state, GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003648 btrfs_put_ordered_extent(ordered);
3649 }
3650
Yan Zheng9036c102008-10-30 14:19:41 -04003651 cur_offset = hole_start;
3652 while (1) {
3653 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
3654 block_end - cur_offset, 0);
3655 BUG_ON(IS_ERR(em) || !em);
3656 last_byte = min(extent_map_end(em), block_end);
3657 last_byte = (last_byte + mask) & ~mask;
Yan, Zheng80825102009-11-12 09:35:36 +00003658 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Chris Mason771ed682008-11-06 22:02:51 -05003659 u64 hint_byte = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04003660 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00003661
Yan, Zhenga22285a2010-05-16 10:48:46 -04003662 trans = btrfs_start_transaction(root, 2);
3663 if (IS_ERR(trans)) {
3664 err = PTR_ERR(trans);
Chris Mason771ed682008-11-06 22:02:51 -05003665 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003666 }
Yan, Zheng80825102009-11-12 09:35:36 +00003667 btrfs_set_trans_block_group(trans, inode);
3668
3669 err = btrfs_drop_extents(trans, inode, cur_offset,
3670 cur_offset + hole_size,
3671 &hint_byte, 1);
Josef Bacik3893e332011-01-31 16:03:11 -05003672 if (err)
3673 break;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003674
Yan Zheng9036c102008-10-30 14:19:41 -04003675 err = btrfs_insert_file_extent(trans, root,
Li Zefan33345d012011-04-20 10:31:50 +08003676 btrfs_ino(inode), cur_offset, 0,
Yan Zheng9036c102008-10-30 14:19:41 -04003677 0, hole_size, 0, hole_size,
3678 0, 0, 0);
Josef Bacik3893e332011-01-31 16:03:11 -05003679 if (err)
3680 break;
Yan, Zheng80825102009-11-12 09:35:36 +00003681
Yan Zheng9036c102008-10-30 14:19:41 -04003682 btrfs_drop_extent_cache(inode, hole_start,
3683 last_byte - 1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003684
3685 btrfs_end_transaction(trans, root);
Yan Zheng9036c102008-10-30 14:19:41 -04003686 }
3687 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003688 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04003689 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00003690 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04003691 break;
3692 }
3693
Yan, Zhenga22285a2010-05-16 10:48:46 -04003694 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003695 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
3696 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003697 return err;
3698}
3699
Josef Bacika41ad392011-01-31 15:30:16 -05003700static int btrfs_setsize(struct inode *inode, loff_t newsize)
Yan, Zheng80825102009-11-12 09:35:36 +00003701{
Josef Bacika41ad392011-01-31 15:30:16 -05003702 loff_t oldsize = i_size_read(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003703 int ret;
3704
Josef Bacika41ad392011-01-31 15:30:16 -05003705 if (newsize == oldsize)
Yan, Zheng80825102009-11-12 09:35:36 +00003706 return 0;
3707
Josef Bacika41ad392011-01-31 15:30:16 -05003708 if (newsize > oldsize) {
3709 i_size_write(inode, newsize);
3710 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
3711 truncate_pagecache(inode, oldsize, newsize);
3712 ret = btrfs_cont_expand(inode, oldsize, newsize);
Yan, Zheng80825102009-11-12 09:35:36 +00003713 if (ret) {
Josef Bacika41ad392011-01-31 15:30:16 -05003714 btrfs_setsize(inode, oldsize);
Yan, Zheng80825102009-11-12 09:35:36 +00003715 return ret;
3716 }
3717
Josef Bacik930f0282011-03-04 14:41:41 -05003718 mark_inode_dirty(inode);
Josef Bacika41ad392011-01-31 15:30:16 -05003719 } else {
3720
3721 /*
3722 * We're truncating a file that used to have good data down to
3723 * zero. Make sure it gets into the ordered flush list so that
3724 * any new writes get down to disk quickly.
3725 */
3726 if (newsize == 0)
3727 BTRFS_I(inode)->ordered_data_close = 1;
3728
3729 /* we don't support swapfiles, so vmtruncate shouldn't fail */
3730 truncate_setsize(inode, newsize);
3731 ret = btrfs_truncate(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003732 }
3733
Josef Bacika41ad392011-01-31 15:30:16 -05003734 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00003735}
3736
Chris Mason39279cc2007-06-12 06:35:45 -04003737static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
3738{
3739 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08003740 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003741 int err;
3742
Li Zefanb83cc962010-12-20 16:04:08 +08003743 if (btrfs_root_readonly(root))
3744 return -EROFS;
3745
Chris Mason39279cc2007-06-12 06:35:45 -04003746 err = inode_change_ok(inode, attr);
3747 if (err)
3748 return err;
3749
Chris Mason5a3f23d2009-03-31 13:27:11 -04003750 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Josef Bacika41ad392011-01-31 15:30:16 -05003751 err = btrfs_setsize(inode, attr->ia_size);
Yan, Zheng80825102009-11-12 09:35:36 +00003752 if (err)
3753 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003754 }
Yan Zheng9036c102008-10-30 14:19:41 -04003755
Christoph Hellwig10257742010-06-04 11:30:02 +02003756 if (attr->ia_valid) {
3757 setattr_copy(inode, attr);
3758 mark_inode_dirty(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04003759
Christoph Hellwig10257742010-06-04 11:30:02 +02003760 if (attr->ia_valid & ATTR_MODE)
3761 err = btrfs_acl_chmod(inode);
3762 }
3763
Chris Mason39279cc2007-06-12 06:35:45 -04003764 return err;
3765}
Chris Mason61295eb2008-01-14 16:24:38 -05003766
Al Virobd555972010-06-07 11:35:40 -04003767void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003768{
3769 struct btrfs_trans_handle *trans;
3770 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003771 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04003772 int ret;
3773
liubo1abe9b82011-03-24 11:18:59 +00003774 trace_btrfs_inode_evict(inode);
3775
Chris Mason39279cc2007-06-12 06:35:45 -04003776 truncate_inode_pages(&inode->i_data, 0);
Josef Bacik0af3d002010-06-21 14:48:16 -04003777 if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 ||
Li Zefan82d59022011-04-20 10:33:24 +08003778 is_free_space_inode(root, inode)))
Al Virobd555972010-06-07 11:35:40 -04003779 goto no_delete;
3780
Chris Mason39279cc2007-06-12 06:35:45 -04003781 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04003782 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003783 goto no_delete;
3784 }
Al Virobd555972010-06-07 11:35:40 -04003785 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04003786 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04003787
Yan, Zhengc71bf092009-11-12 09:34:40 +00003788 if (root->fs_info->log_root_recovering) {
3789 BUG_ON(!list_empty(&BTRFS_I(inode)->i_orphan));
3790 goto no_delete;
3791 }
3792
Yan, Zheng76dda932009-09-21 16:00:26 -04003793 if (inode->i_nlink > 0) {
3794 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
3795 goto no_delete;
3796 }
3797
Chris Masondbe674a2008-07-17 12:54:05 -04003798 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003799
Yan, Zheng80825102009-11-12 09:35:36 +00003800 while (1) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003801 trans = btrfs_start_transaction(root, 0);
3802 BUG_ON(IS_ERR(trans));
Yan, Zheng80825102009-11-12 09:35:36 +00003803 btrfs_set_trans_block_group(trans, inode);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003804 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00003805
Yan, Zhengd68fc572010-05-16 10:49:58 -04003806 ret = btrfs_block_rsv_check(trans, root,
3807 root->orphan_block_rsv, 0, 5);
3808 if (ret) {
3809 BUG_ON(ret != -EAGAIN);
3810 ret = btrfs_commit_transaction(trans, root);
3811 BUG_ON(ret);
3812 continue;
3813 }
3814
3815 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003816 if (ret != -EAGAIN)
3817 break;
3818
3819 nr = trans->blocks_used;
3820 btrfs_end_transaction(trans, root);
3821 trans = NULL;
3822 btrfs_btree_balance_dirty(root, nr);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003823
Josef Bacik7b128762008-07-24 12:17:14 -04003824 }
3825
Yan, Zheng80825102009-11-12 09:35:36 +00003826 if (ret == 0) {
3827 ret = btrfs_orphan_del(trans, inode);
3828 BUG_ON(ret);
3829 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003830
Li Zefan581bb052011-04-20 10:06:11 +08003831 if (!(root == root->fs_info->tree_root ||
3832 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08003833 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08003834
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003835 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04003836 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003837 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003838no_delete:
Al Virobd555972010-06-07 11:35:40 -04003839 end_writeback(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003840 return;
Chris Mason39279cc2007-06-12 06:35:45 -04003841}
3842
3843/*
3844 * this returns the key found in the dir entry in the location pointer.
3845 * If no dir entries were found, location->objectid is 0.
3846 */
3847static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
3848 struct btrfs_key *location)
3849{
3850 const char *name = dentry->d_name.name;
3851 int namelen = dentry->d_name.len;
3852 struct btrfs_dir_item *di;
3853 struct btrfs_path *path;
3854 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04003855 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003856
3857 path = btrfs_alloc_path();
3858 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -05003859
Li Zefan33345d012011-04-20 10:31:50 +08003860 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04003861 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04003862 if (IS_ERR(di))
3863 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05003864
3865 if (!di || IS_ERR(di))
Chris Mason39544012007-12-12 14:38:19 -05003866 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05003867
Chris Mason5f39d392007-10-15 16:14:19 -04003868 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04003869out:
Chris Mason39279cc2007-06-12 06:35:45 -04003870 btrfs_free_path(path);
3871 return ret;
Chris Mason39544012007-12-12 14:38:19 -05003872out_err:
3873 location->objectid = 0;
3874 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04003875}
3876
3877/*
3878 * when we hit a tree root in a directory, the btrfs part of the inode
3879 * needs to be changed to reflect the root directory of the tree root. This
3880 * is kind of like crossing a mount point.
3881 */
3882static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003883 struct inode *dir,
3884 struct dentry *dentry,
3885 struct btrfs_key *location,
3886 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04003887{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003888 struct btrfs_path *path;
3889 struct btrfs_root *new_root;
3890 struct btrfs_root_ref *ref;
3891 struct extent_buffer *leaf;
3892 int ret;
3893 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003894
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003895 path = btrfs_alloc_path();
3896 if (!path) {
3897 err = -ENOMEM;
3898 goto out;
3899 }
Chris Mason39279cc2007-06-12 06:35:45 -04003900
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003901 err = -ENOENT;
3902 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
3903 BTRFS_I(dir)->root->root_key.objectid,
3904 location->objectid);
3905 if (ret) {
3906 if (ret < 0)
3907 err = ret;
3908 goto out;
3909 }
Chris Mason39279cc2007-06-12 06:35:45 -04003910
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003911 leaf = path->nodes[0];
3912 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08003913 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003914 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
3915 goto out;
3916
3917 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
3918 (unsigned long)(ref + 1),
3919 dentry->d_name.len);
3920 if (ret)
3921 goto out;
3922
3923 btrfs_release_path(root->fs_info->tree_root, path);
3924
3925 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
3926 if (IS_ERR(new_root)) {
3927 err = PTR_ERR(new_root);
3928 goto out;
3929 }
3930
3931 if (btrfs_root_refs(&new_root->root_item) == 0) {
3932 err = -ENOENT;
3933 goto out;
3934 }
3935
3936 *sub_root = new_root;
3937 location->objectid = btrfs_root_dirid(&new_root->root_item);
3938 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04003939 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003940 err = 0;
3941out:
3942 btrfs_free_path(path);
3943 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003944}
3945
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003946static void inode_tree_add(struct inode *inode)
3947{
3948 struct btrfs_root *root = BTRFS_I(inode)->root;
3949 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003950 struct rb_node **p;
3951 struct rb_node *parent;
Li Zefan33345d012011-04-20 10:31:50 +08003952 u64 ino = btrfs_ino(inode);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003953again:
3954 p = &root->inode_tree.rb_node;
3955 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003956
Al Viro1d3382cb2010-10-23 15:19:20 -04003957 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04003958 return;
3959
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003960 spin_lock(&root->inode_lock);
3961 while (*p) {
3962 parent = *p;
3963 entry = rb_entry(parent, struct btrfs_inode, rb_node);
3964
Li Zefan33345d012011-04-20 10:31:50 +08003965 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003966 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08003967 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003968 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003969 else {
3970 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04003971 (I_WILL_FREE | I_FREEING)));
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003972 rb_erase(parent, &root->inode_tree);
3973 RB_CLEAR_NODE(parent);
3974 spin_unlock(&root->inode_lock);
3975 goto again;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003976 }
3977 }
3978 rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
3979 rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
3980 spin_unlock(&root->inode_lock);
3981}
3982
3983static void inode_tree_del(struct inode *inode)
3984{
3985 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04003986 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003987
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003988 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003989 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003990 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003991 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04003992 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003993 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003994 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04003995
Josef Bacik0af3d002010-06-21 14:48:16 -04003996 /*
3997 * Free space cache has inodes in the tree root, but the tree root has a
3998 * root_refs of 0, so this could end up dropping the tree root as a
3999 * snapshot, so we need the extra !root->fs_info->tree_root check to
4000 * make sure we don't drop it.
4001 */
4002 if (empty && btrfs_root_refs(&root->root_item) == 0 &&
4003 root != root->fs_info->tree_root) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004004 synchronize_srcu(&root->fs_info->subvol_srcu);
4005 spin_lock(&root->inode_lock);
4006 empty = RB_EMPTY_ROOT(&root->inode_tree);
4007 spin_unlock(&root->inode_lock);
4008 if (empty)
4009 btrfs_add_dead_root(root);
4010 }
4011}
4012
4013int btrfs_invalidate_inodes(struct btrfs_root *root)
4014{
4015 struct rb_node *node;
4016 struct rb_node *prev;
4017 struct btrfs_inode *entry;
4018 struct inode *inode;
4019 u64 objectid = 0;
4020
4021 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4022
4023 spin_lock(&root->inode_lock);
4024again:
4025 node = root->inode_tree.rb_node;
4026 prev = NULL;
4027 while (node) {
4028 prev = node;
4029 entry = rb_entry(node, struct btrfs_inode, rb_node);
4030
Li Zefan33345d012011-04-20 10:31:50 +08004031 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004032 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004033 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004034 node = node->rb_right;
4035 else
4036 break;
4037 }
4038 if (!node) {
4039 while (prev) {
4040 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004041 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004042 node = prev;
4043 break;
4044 }
4045 prev = rb_next(prev);
4046 }
4047 }
4048 while (node) {
4049 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004050 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04004051 inode = igrab(&entry->vfs_inode);
4052 if (inode) {
4053 spin_unlock(&root->inode_lock);
4054 if (atomic_read(&inode->i_count) > 1)
4055 d_prune_aliases(inode);
4056 /*
Al Viro45321ac2010-06-07 13:43:19 -04004057 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04004058 * the inode cache when its usage count
4059 * hits zero.
4060 */
4061 iput(inode);
4062 cond_resched();
4063 spin_lock(&root->inode_lock);
4064 goto again;
4065 }
4066
4067 if (cond_resched_lock(&root->inode_lock))
4068 goto again;
4069
4070 node = rb_next(node);
4071 }
4072 spin_unlock(&root->inode_lock);
4073 return 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004074}
4075
Chris Masone02119d2008-09-05 16:13:11 -04004076static int btrfs_init_locked_inode(struct inode *inode, void *p)
4077{
4078 struct btrfs_iget_args *args = p;
4079 inode->i_ino = args->ino;
Chris Masone02119d2008-09-05 16:13:11 -04004080 BTRFS_I(inode)->root = args->root;
Josef Bacik6a632092009-02-20 11:00:09 -05004081 btrfs_set_inode_space_info(args->root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004082 return 0;
4083}
4084
4085static int btrfs_find_actor(struct inode *inode, void *opaque)
4086{
4087 struct btrfs_iget_args *args = opaque;
Li Zefan33345d012011-04-20 10:31:50 +08004088 return args->ino == btrfs_ino(inode) &&
Chris Masond3977122009-01-05 21:25:51 -05004089 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004090}
4091
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004092static struct inode *btrfs_iget_locked(struct super_block *s,
4093 u64 objectid,
4094 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04004095{
4096 struct inode *inode;
4097 struct btrfs_iget_args args;
4098 args.ino = objectid;
4099 args.root = root;
4100
4101 inode = iget5_locked(s, objectid, btrfs_find_actor,
4102 btrfs_init_locked_inode,
4103 (void *)&args);
4104 return inode;
4105}
4106
Balaji Rao1a54ef82008-07-21 02:01:04 +05304107/* Get an inode object given its location and corresponding root.
4108 * Returns in *is_new if the inode was read from disk
4109 */
4110struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00004111 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05304112{
4113 struct inode *inode;
4114
4115 inode = btrfs_iget_locked(s, location->objectid, root);
4116 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004117 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05304118
4119 if (inode->i_state & I_NEW) {
4120 BTRFS_I(inode)->root = root;
4121 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
4122 btrfs_read_locked_inode(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004123 inode_tree_add(inode);
Balaji Rao1a54ef82008-07-21 02:01:04 +05304124 unlock_new_inode(inode);
Josef Bacik73f73412009-12-04 17:38:27 +00004125 if (new)
4126 *new = 1;
Balaji Rao1a54ef82008-07-21 02:01:04 +05304127 }
4128
4129 return inode;
4130}
4131
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004132static struct inode *new_simple_dir(struct super_block *s,
4133 struct btrfs_key *key,
4134 struct btrfs_root *root)
4135{
4136 struct inode *inode = new_inode(s);
4137
4138 if (!inode)
4139 return ERR_PTR(-ENOMEM);
4140
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004141 BTRFS_I(inode)->root = root;
4142 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
4143 BTRFS_I(inode)->dummy_inode = 1;
4144
4145 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
4146 inode->i_op = &simple_dir_inode_operations;
4147 inode->i_fop = &simple_dir_operations;
4148 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
4149 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
4150
4151 return inode;
4152}
4153
Chris Mason3de45862008-11-17 21:02:50 -05004154struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04004155{
Chris Masond3977122009-01-05 21:25:51 -05004156 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004157 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004158 struct btrfs_root *sub_root = root;
4159 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04004160 int index;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004161 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004162
4163 if (dentry->d_name.len > BTRFS_NAME_LEN)
4164 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04004165
Chris Mason39279cc2007-06-12 06:35:45 -04004166 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04004167
Chris Mason39279cc2007-06-12 06:35:45 -04004168 if (ret < 0)
4169 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04004170
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004171 if (location.objectid == 0)
4172 return NULL;
4173
4174 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00004175 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004176 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004177 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004178
4179 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
4180
Yan, Zheng76dda932009-09-21 16:00:26 -04004181 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004182 ret = fixup_tree_root_location(root, dir, dentry,
4183 &location, &sub_root);
4184 if (ret < 0) {
4185 if (ret != -ENOENT)
4186 inode = ERR_PTR(ret);
4187 else
4188 inode = new_simple_dir(dir->i_sb, &location, sub_root);
4189 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00004190 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004191 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004192 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
4193
Julia Lawall34d19ba2011-01-24 19:55:19 +00004194 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00004195 down_read(&root->fs_info->cleanup_work_sem);
4196 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004197 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00004198 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004199 if (ret)
4200 inode = ERR_PTR(ret);
Yan, Zhengc71bf092009-11-12 09:34:40 +00004201 }
4202
Chris Mason3de45862008-11-17 21:02:50 -05004203 return inode;
4204}
4205
Nick Pigginfe15ce42011-01-07 17:49:23 +11004206static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04004207{
4208 struct btrfs_root *root;
4209
Yan, Zhengefefb142009-10-09 09:25:16 -04004210 if (!dentry->d_inode && !IS_ROOT(dentry))
4211 dentry = dentry->d_parent;
Yan, Zheng76dda932009-09-21 16:00:26 -04004212
Yan, Zhengefefb142009-10-09 09:25:16 -04004213 if (dentry->d_inode) {
4214 root = BTRFS_I(dentry->d_inode)->root;
4215 if (btrfs_root_refs(&root->root_item) == 0)
4216 return 1;
4217 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004218 return 0;
4219}
4220
Chris Mason3de45862008-11-17 21:02:50 -05004221static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
4222 struct nameidata *nd)
4223{
4224 struct inode *inode;
4225
Chris Mason3de45862008-11-17 21:02:50 -05004226 inode = btrfs_lookup_dentry(dir, dentry);
4227 if (IS_ERR(inode))
4228 return ERR_CAST(inode);
Josef Bacik7b128762008-07-24 12:17:14 -04004229
Chris Mason39279cc2007-06-12 06:35:45 -04004230 return d_splice_alias(inode, dentry);
4231}
4232
Chris Mason39279cc2007-06-12 06:35:45 -04004233static unsigned char btrfs_filetype_table[] = {
4234 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
4235};
4236
David Woodhousecbdf5a22008-08-06 19:42:33 +01004237static int btrfs_real_readdir(struct file *filp, void *dirent,
4238 filldir_t filldir)
Chris Mason39279cc2007-06-12 06:35:45 -04004239{
Chris Mason6da6aba2007-12-18 16:15:09 -05004240 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004241 struct btrfs_root *root = BTRFS_I(inode)->root;
4242 struct btrfs_item *item;
4243 struct btrfs_dir_item *di;
4244 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04004245 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004246 struct btrfs_path *path;
4247 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04004248 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004249 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04004250 unsigned char d_type;
4251 int over = 0;
4252 u32 di_cur;
4253 u32 di_total;
4254 u32 di_len;
4255 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04004256 char tmp_name[32];
4257 char *name_ptr;
4258 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04004259
4260 /* FIXME, use a real flag for deciding about the key type */
4261 if (root->fs_info->tree_root == root)
4262 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04004263
Chris Mason39544012007-12-12 14:38:19 -05004264 /* special case for "." */
4265 if (filp->f_pos == 0) {
Li Zefan33345d012011-04-20 10:31:50 +08004266 over = filldir(dirent, ".", 1, 1, btrfs_ino(inode), DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05004267 if (over)
4268 return 0;
4269 filp->f_pos = 1;
4270 }
Chris Mason39544012007-12-12 14:38:19 -05004271 /* special case for .., just use the back ref */
4272 if (filp->f_pos == 1) {
David Woodhouse5ecc7e52008-08-17 15:14:48 +01004273 u64 pino = parent_ino(filp->f_path.dentry);
Chris Mason39544012007-12-12 14:38:19 -05004274 over = filldir(dirent, "..", 2,
David Woodhouse5ecc7e52008-08-17 15:14:48 +01004275 2, pino, DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05004276 if (over)
David Woodhouse49593bf2008-08-17 17:08:36 +01004277 return 0;
Chris Mason39544012007-12-12 14:38:19 -05004278 filp->f_pos = 2;
4279 }
David Woodhouse49593bf2008-08-17 17:08:36 +01004280 path = btrfs_alloc_path();
4281 path->reada = 2;
4282
Chris Mason39279cc2007-06-12 06:35:45 -04004283 btrfs_set_key_type(&key, key_type);
4284 key.offset = filp->f_pos;
Li Zefan33345d012011-04-20 10:31:50 +08004285 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004286
Chris Mason39279cc2007-06-12 06:35:45 -04004287 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4288 if (ret < 0)
4289 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01004290
4291 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04004292 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04004293 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08004294 if (slot >= btrfs_header_nritems(leaf)) {
4295 ret = btrfs_next_leaf(root, path);
4296 if (ret < 0)
4297 goto err;
4298 else if (ret > 0)
4299 break;
4300 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04004301 }
Chris Mason3de45862008-11-17 21:02:50 -05004302
Chris Mason5f39d392007-10-15 16:14:19 -04004303 item = btrfs_item_nr(leaf, slot);
4304 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4305
4306 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04004307 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004308 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004309 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004310 if (found_key.offset < filp->f_pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08004311 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04004312
4313 filp->f_pos = found_key.offset;
David Woodhouse49593bf2008-08-17 17:08:36 +01004314
Chris Mason39279cc2007-06-12 06:35:45 -04004315 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
4316 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04004317 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01004318
4319 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04004320 struct btrfs_key location;
4321
Josef Bacik22a94d42011-03-16 16:47:17 -04004322 if (verify_dir_item(root, leaf, di))
4323 break;
4324
Chris Mason5f39d392007-10-15 16:14:19 -04004325 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01004326 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04004327 name_ptr = tmp_name;
4328 } else {
4329 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01004330 if (!name_ptr) {
4331 ret = -ENOMEM;
4332 goto err;
4333 }
Chris Mason5f39d392007-10-15 16:14:19 -04004334 }
4335 read_extent_buffer(leaf, name_ptr,
4336 (unsigned long)(di + 1), name_len);
4337
4338 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
4339 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05004340
4341 /* is this a reference to our own snapshot? If so
4342 * skip it
4343 */
4344 if (location.type == BTRFS_ROOT_ITEM_KEY &&
4345 location.objectid == root->root_key.objectid) {
4346 over = 0;
4347 goto skip;
4348 }
Chris Mason5f39d392007-10-15 16:14:19 -04004349 over = filldir(dirent, name_ptr, name_len,
David Woodhouse49593bf2008-08-17 17:08:36 +01004350 found_key.offset, location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04004351 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04004352
Chris Mason3de45862008-11-17 21:02:50 -05004353skip:
Chris Mason5f39d392007-10-15 16:14:19 -04004354 if (name_ptr != tmp_name)
4355 kfree(name_ptr);
4356
Chris Mason39279cc2007-06-12 06:35:45 -04004357 if (over)
4358 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05004359 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01004360 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04004361 di_cur += di_len;
4362 di = (struct btrfs_dir_item *)((char *)di + di_len);
4363 }
Li Zefanb9e03af2011-03-23 10:43:58 +08004364next:
4365 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04004366 }
David Woodhouse49593bf2008-08-17 17:08:36 +01004367
4368 /* Reached end of directory/root. Bump pos past the last item. */
Yan Zheng5e591a02008-02-19 11:41:02 -05004369 if (key_type == BTRFS_DIR_INDEX_KEY)
Jan Engelhardt406266a2009-12-09 22:00:38 +00004370 /*
4371 * 32-bit glibc will use getdents64, but then strtol -
4372 * so the last number we can serve is this.
4373 */
4374 filp->f_pos = 0x7fffffff;
Yan Zheng5e591a02008-02-19 11:41:02 -05004375 else
4376 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04004377nopos:
4378 ret = 0;
4379err:
Chris Mason39279cc2007-06-12 06:35:45 -04004380 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004381 return ret;
4382}
4383
Christoph Hellwiga9185b42010-03-05 09:21:37 +01004384int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04004385{
4386 struct btrfs_root *root = BTRFS_I(inode)->root;
4387 struct btrfs_trans_handle *trans;
4388 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04004389 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04004390
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004391 if (BTRFS_I(inode)->dummy_inode)
Chris Mason4ca8b412008-08-05 13:30:48 -04004392 return 0;
4393
Josef Bacik0af3d002010-06-21 14:48:16 -04004394 smp_mb();
Li Zefan82d59022011-04-20 10:33:24 +08004395 if (root->fs_info->closing && is_free_space_inode(root, inode))
4396 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04004397
Christoph Hellwiga9185b42010-03-05 09:21:37 +01004398 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04004399 if (nolock)
4400 trans = btrfs_join_transaction_nolock(root, 1);
4401 else
4402 trans = btrfs_join_transaction(root, 1);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00004403 if (IS_ERR(trans))
4404 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04004405 btrfs_set_trans_block_group(trans, inode);
Josef Bacik0af3d002010-06-21 14:48:16 -04004406 if (nolock)
4407 ret = btrfs_end_transaction_nolock(trans, root);
4408 else
4409 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004410 }
4411 return ret;
4412}
4413
4414/*
Chris Mason54aa1f42007-06-22 14:16:25 -04004415 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04004416 * inode changes. But, it is most likely to find the inode in cache.
4417 * FIXME, needs more benchmarking...there are no reasons other than performance
4418 * to keep or drop this code.
4419 */
4420void btrfs_dirty_inode(struct inode *inode)
4421{
4422 struct btrfs_root *root = BTRFS_I(inode)->root;
4423 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004424 int ret;
4425
4426 if (BTRFS_I(inode)->dummy_inode)
4427 return;
Chris Mason39279cc2007-06-12 06:35:45 -04004428
Chris Masonf9295742008-07-17 12:54:14 -04004429 trans = btrfs_join_transaction(root, 1);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00004430 BUG_ON(IS_ERR(trans));
Chris Mason39279cc2007-06-12 06:35:45 -04004431 btrfs_set_trans_block_group(trans, inode);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004432
4433 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04004434 if (ret && ret == -ENOSPC) {
4435 /* whoops, lets try again with the full transaction */
4436 btrfs_end_transaction(trans, root);
4437 trans = btrfs_start_transaction(root, 1);
Chris Mason9aeead72010-05-27 10:23:00 -04004438 if (IS_ERR(trans)) {
4439 if (printk_ratelimit()) {
4440 printk(KERN_ERR "btrfs: fail to "
Li Zefan33345d012011-04-20 10:31:50 +08004441 "dirty inode %llu error %ld\n",
4442 (unsigned long long)btrfs_ino(inode),
4443 PTR_ERR(trans));
Chris Mason9aeead72010-05-27 10:23:00 -04004444 }
4445 return;
4446 }
Chris Mason94b60442010-05-26 11:02:00 -04004447 btrfs_set_trans_block_group(trans, inode);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004448
Chris Mason94b60442010-05-26 11:02:00 -04004449 ret = btrfs_update_inode(trans, root, inode);
4450 if (ret) {
Chris Mason9aeead72010-05-27 10:23:00 -04004451 if (printk_ratelimit()) {
4452 printk(KERN_ERR "btrfs: fail to "
Li Zefan33345d012011-04-20 10:31:50 +08004453 "dirty inode %llu error %d\n",
4454 (unsigned long long)btrfs_ino(inode),
4455 ret);
Chris Mason9aeead72010-05-27 10:23:00 -04004456 }
Chris Mason94b60442010-05-26 11:02:00 -04004457 }
4458 }
Chris Mason39279cc2007-06-12 06:35:45 -04004459 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004460}
4461
Chris Masond352ac62008-09-29 15:18:18 -04004462/*
4463 * find the highest existing sequence number in a directory
4464 * and then set the in-memory index_cnt variable to reflect
4465 * free sequence numbers
4466 */
Josef Bacikaec74772008-07-24 12:12:38 -04004467static int btrfs_set_inode_index_count(struct inode *inode)
4468{
4469 struct btrfs_root *root = BTRFS_I(inode)->root;
4470 struct btrfs_key key, found_key;
4471 struct btrfs_path *path;
4472 struct extent_buffer *leaf;
4473 int ret;
4474
Li Zefan33345d012011-04-20 10:31:50 +08004475 key.objectid = btrfs_ino(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004476 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
4477 key.offset = (u64)-1;
4478
4479 path = btrfs_alloc_path();
4480 if (!path)
4481 return -ENOMEM;
4482
4483 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4484 if (ret < 0)
4485 goto out;
4486 /* FIXME: we should be able to handle this */
4487 if (ret == 0)
4488 goto out;
4489 ret = 0;
4490
4491 /*
4492 * MAGIC NUMBER EXPLANATION:
4493 * since we search a directory based on f_pos we have to start at 2
4494 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
4495 * else has to start at 2
4496 */
4497 if (path->slots[0] == 0) {
4498 BTRFS_I(inode)->index_cnt = 2;
4499 goto out;
4500 }
4501
4502 path->slots[0]--;
4503
4504 leaf = path->nodes[0];
4505 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4506
Li Zefan33345d012011-04-20 10:31:50 +08004507 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacikaec74772008-07-24 12:12:38 -04004508 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
4509 BTRFS_I(inode)->index_cnt = 2;
4510 goto out;
4511 }
4512
4513 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
4514out:
4515 btrfs_free_path(path);
4516 return ret;
4517}
4518
Chris Masond352ac62008-09-29 15:18:18 -04004519/*
4520 * helper to find a free sequence number in a given directory. This current
4521 * code is very simple, later versions will do smarter things in the btree
4522 */
Chris Mason3de45862008-11-17 21:02:50 -05004523int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04004524{
4525 int ret = 0;
4526
4527 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
4528 ret = btrfs_set_inode_index_count(dir);
Chris Masond3977122009-01-05 21:25:51 -05004529 if (ret)
Josef Bacikaec74772008-07-24 12:12:38 -04004530 return ret;
4531 }
4532
Chris Mason00e4e6b2008-08-05 11:18:09 -04004533 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04004534 BTRFS_I(dir)->index_cnt++;
4535
4536 return ret;
4537}
4538
Chris Mason39279cc2007-06-12 06:35:45 -04004539static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
4540 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04004541 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05004542 const char *name, int name_len,
Yan Zhengd2fb3432008-12-11 16:30:39 -05004543 u64 ref_objectid, u64 objectid,
4544 u64 alloc_hint, int mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04004545{
4546 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004547 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04004548 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04004549 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05004550 struct btrfs_inode_ref *ref;
4551 struct btrfs_key key[2];
4552 u32 sizes[2];
4553 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04004554 int ret;
4555 int owner;
4556
Chris Mason5f39d392007-10-15 16:14:19 -04004557 path = btrfs_alloc_path();
4558 BUG_ON(!path);
4559
Chris Mason39279cc2007-06-12 06:35:45 -04004560 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00004561 if (!inode) {
4562 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004563 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00004564 }
Chris Mason39279cc2007-06-12 06:35:45 -04004565
Li Zefan581bb052011-04-20 10:06:11 +08004566 /*
4567 * we have to initialize this early, so we can reclaim the inode
4568 * number if we fail afterwards in this function.
4569 */
4570 inode->i_ino = objectid;
4571
Josef Bacikaec74772008-07-24 12:12:38 -04004572 if (dir) {
liubo1abe9b82011-03-24 11:18:59 +00004573 trace_btrfs_inode_request(dir);
4574
Chris Mason3de45862008-11-17 21:02:50 -05004575 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04004576 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00004577 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04004578 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004579 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04004580 }
Josef Bacikaec74772008-07-24 12:12:38 -04004581 }
4582 /*
4583 * index_cnt is ignored for everything but a dir,
4584 * btrfs_get_inode_index_count has an explanation for the magic
4585 * number
4586 */
4587 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04004588 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04004589 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00004590 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik6a632092009-02-20 11:00:09 -05004591 btrfs_set_inode_space_info(root, inode);
Chris Masonb888db22007-08-27 16:49:44 -04004592
Chris Mason39279cc2007-06-12 06:35:45 -04004593 if (mode & S_IFDIR)
4594 owner = 0;
4595 else
4596 owner = 1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05004597 BTRFS_I(inode)->block_group =
4598 btrfs_find_block_group(root, 0, alloc_hint, owner);
Chris Mason9c583092008-01-29 15:15:18 -05004599
4600 key[0].objectid = objectid;
4601 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
4602 key[0].offset = 0;
4603
4604 key[1].objectid = objectid;
4605 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
4606 key[1].offset = ref_objectid;
4607
4608 sizes[0] = sizeof(struct btrfs_inode_item);
4609 sizes[1] = name_len + sizeof(*ref);
4610
Chris Masonb9473432009-03-13 11:00:37 -04004611 path->leave_spinning = 1;
Chris Mason9c583092008-01-29 15:15:18 -05004612 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
4613 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04004614 goto fail;
4615
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03004616 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004617 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004618 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04004619 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4620 struct btrfs_inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04004621 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05004622
4623 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
4624 struct btrfs_inode_ref);
4625 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04004626 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05004627 ptr = (unsigned long)(ref + 1);
4628 write_extent_buffer(path->nodes[0], name, ptr, name_len);
4629
Chris Mason5f39d392007-10-15 16:14:19 -04004630 btrfs_mark_buffer_dirty(path->nodes[0]);
4631 btrfs_free_path(path);
4632
Chris Mason39279cc2007-06-12 06:35:45 -04004633 location = &BTRFS_I(inode)->location;
4634 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04004635 location->offset = 0;
4636 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
4637
Christoph Hellwig6cbff002009-04-17 10:37:41 +02004638 btrfs_inherit_iflags(inode, dir);
4639
Chris Mason94272162009-07-02 12:26:06 -04004640 if ((mode & S_IFREG)) {
4641 if (btrfs_test_opt(root, NODATASUM))
4642 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo75e7cb72011-03-22 10:12:20 +00004643 if (btrfs_test_opt(root, NODATACOW) ||
4644 (BTRFS_I(dir)->flags & BTRFS_INODE_NODATACOW))
Chris Mason94272162009-07-02 12:26:06 -04004645 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
4646 }
4647
Chris Mason39279cc2007-06-12 06:35:45 -04004648 insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004649 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00004650
4651 trace_btrfs_inode_new(inode);
4652
Chris Mason39279cc2007-06-12 06:35:45 -04004653 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004654fail:
Josef Bacikaec74772008-07-24 12:12:38 -04004655 if (dir)
4656 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04004657 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04004658 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004659 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004660}
4661
4662static inline u8 btrfs_inode_type(struct inode *inode)
4663{
4664 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
4665}
4666
Chris Masond352ac62008-09-29 15:18:18 -04004667/*
4668 * utility function to add 'inode' into 'parent_inode' with
4669 * a give name and a given sequence number.
4670 * if 'add_backref' is true, also insert a backref from the
4671 * inode to the parent directory.
4672 */
Chris Masone02119d2008-09-05 16:13:11 -04004673int btrfs_add_link(struct btrfs_trans_handle *trans,
4674 struct inode *parent_inode, struct inode *inode,
4675 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04004676{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004677 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004678 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04004679 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08004680 u64 ino = btrfs_ino(inode);
4681 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004682
Li Zefan33345d012011-04-20 10:31:50 +08004683 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004684 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
4685 } else {
Li Zefan33345d012011-04-20 10:31:50 +08004686 key.objectid = ino;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004687 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
4688 key.offset = 0;
4689 }
Chris Mason39279cc2007-06-12 06:35:45 -04004690
Li Zefan33345d012011-04-20 10:31:50 +08004691 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004692 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
4693 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08004694 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004695 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08004696 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
4697 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004698 }
4699
Chris Mason39279cc2007-06-12 06:35:45 -04004700 if (ret == 0) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004701 ret = btrfs_insert_dir_item(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08004702 parent_ino, &key,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004703 btrfs_inode_type(inode), index);
4704 BUG_ON(ret);
4705
Chris Masondbe674a2008-07-17 12:54:05 -04004706 btrfs_i_size_write(parent_inode, parent_inode->i_size +
Chris Masone02119d2008-09-05 16:13:11 -04004707 name_len * 2);
Chris Mason79c44582007-06-25 10:09:33 -04004708 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Masone02119d2008-09-05 16:13:11 -04004709 ret = btrfs_update_inode(trans, root, parent_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004710 }
4711 return ret;
4712}
4713
4714static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00004715 struct inode *dir, struct dentry *dentry,
4716 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04004717{
Josef Bacika1b075d2010-11-19 20:36:11 +00004718 int err = btrfs_add_link(trans, dir, inode,
4719 dentry->d_name.name, dentry->d_name.len,
4720 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004721 if (!err) {
4722 d_instantiate(dentry, inode);
4723 return 0;
4724 }
4725 if (err > 0)
4726 err = -EEXIST;
4727 return err;
4728}
4729
Josef Bacik618e21d2007-07-11 10:18:17 -04004730static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
4731 int mode, dev_t rdev)
4732{
4733 struct btrfs_trans_handle *trans;
4734 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05004735 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04004736 int err;
4737 int drop_inode = 0;
4738 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05004739 unsigned long nr = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004740 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04004741
4742 if (!new_valid_dev(rdev))
4743 return -EINVAL;
4744
Josef Bacik9ed74f22009-09-11 16:12:44 -04004745 /*
4746 * 2 for inode item and ref
4747 * 2 for dir items
4748 * 1 for xattr if selinux is on
4749 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004750 trans = btrfs_start_transaction(root, 5);
4751 if (IS_ERR(trans))
4752 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05004753
Josef Bacik618e21d2007-07-11 10:18:17 -04004754 btrfs_set_trans_block_group(trans, dir);
4755
Li Zefan581bb052011-04-20 10:06:11 +08004756 err = btrfs_find_free_ino(root, &objectid);
4757 if (err)
4758 goto out_unlock;
4759
Josef Bacikaec74772008-07-24 12:12:38 -04004760 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08004761 dentry->d_name.len, btrfs_ino(dir), objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04004762 BTRFS_I(dir)->block_group, mode, &index);
Josef Bacik618e21d2007-07-11 10:18:17 -04004763 err = PTR_ERR(inode);
4764 if (IS_ERR(inode))
4765 goto out_unlock;
4766
Yan, Zhengf34f57a2009-11-12 09:35:27 +00004767 err = btrfs_init_inode_security(trans, inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04004768 if (err) {
4769 drop_inode = 1;
4770 goto out_unlock;
4771 }
4772
Josef Bacik618e21d2007-07-11 10:18:17 -04004773 btrfs_set_trans_block_group(trans, inode);
Josef Bacika1b075d2010-11-19 20:36:11 +00004774 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04004775 if (err)
4776 drop_inode = 1;
4777 else {
4778 inode->i_op = &btrfs_special_inode_operations;
4779 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04004780 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04004781 }
Josef Bacik618e21d2007-07-11 10:18:17 -04004782 btrfs_update_inode_block_group(trans, inode);
4783 btrfs_update_inode_block_group(trans, dir);
4784out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004785 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04004786 btrfs_end_transaction_throttle(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004787 btrfs_btree_balance_dirty(root, nr);
Josef Bacik618e21d2007-07-11 10:18:17 -04004788 if (drop_inode) {
4789 inode_dec_link_count(inode);
4790 iput(inode);
4791 }
Josef Bacik618e21d2007-07-11 10:18:17 -04004792 return err;
4793}
4794
Chris Mason39279cc2007-06-12 06:35:45 -04004795static int btrfs_create(struct inode *dir, struct dentry *dentry,
4796 int mode, struct nameidata *nd)
4797{
4798 struct btrfs_trans_handle *trans;
4799 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05004800 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04004801 int drop_inode = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004802 int err;
Chris Mason1832a6d2007-12-21 16:27:21 -05004803 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004804 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004805 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004806
Josef Bacik9ed74f22009-09-11 16:12:44 -04004807 /*
4808 * 2 for inode item and ref
4809 * 2 for dir items
4810 * 1 for xattr if selinux is on
4811 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004812 trans = btrfs_start_transaction(root, 5);
4813 if (IS_ERR(trans))
4814 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004815
Chris Mason39279cc2007-06-12 06:35:45 -04004816 btrfs_set_trans_block_group(trans, dir);
4817
Li Zefan581bb052011-04-20 10:06:11 +08004818 err = btrfs_find_free_ino(root, &objectid);
4819 if (err)
4820 goto out_unlock;
4821
Josef Bacikaec74772008-07-24 12:12:38 -04004822 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08004823 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacika1b075d2010-11-19 20:36:11 +00004824 BTRFS_I(dir)->block_group, mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04004825 err = PTR_ERR(inode);
4826 if (IS_ERR(inode))
4827 goto out_unlock;
4828
Yan, Zhengf34f57a2009-11-12 09:35:27 +00004829 err = btrfs_init_inode_security(trans, inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04004830 if (err) {
4831 drop_inode = 1;
4832 goto out_unlock;
4833 }
4834
Chris Mason39279cc2007-06-12 06:35:45 -04004835 btrfs_set_trans_block_group(trans, inode);
Josef Bacika1b075d2010-11-19 20:36:11 +00004836 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004837 if (err)
4838 drop_inode = 1;
4839 else {
4840 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04004841 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04004842 inode->i_fop = &btrfs_file_operations;
4843 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05004844 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04004845 }
Chris Mason39279cc2007-06-12 06:35:45 -04004846 btrfs_update_inode_block_group(trans, inode);
4847 btrfs_update_inode_block_group(trans, dir);
4848out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004849 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04004850 btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004851 if (drop_inode) {
4852 inode_dec_link_count(inode);
4853 iput(inode);
4854 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004855 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004856 return err;
4857}
4858
4859static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
4860 struct dentry *dentry)
4861{
4862 struct btrfs_trans_handle *trans;
4863 struct btrfs_root *root = BTRFS_I(dir)->root;
4864 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004865 u64 index;
Chris Mason1832a6d2007-12-21 16:27:21 -05004866 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004867 int err;
4868 int drop_inode = 0;
4869
4870 if (inode->i_nlink == 0)
4871 return -ENOENT;
4872
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00004873 /* do not allow sys_link's with other subvols of the same device */
4874 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00004875 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00004876
Al Viroc055e992011-03-04 17:15:18 +00004877 if (inode->i_nlink == ~0U)
4878 return -EMLINK;
4879
Chris Mason3de45862008-11-17 21:02:50 -05004880 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004881 if (err)
4882 goto fail;
4883
Yan, Zhenga22285a2010-05-16 10:48:46 -04004884 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00004885 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04004886 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00004887 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04004888 */
Miao Xie7e6b6462011-02-18 09:21:17 +00004889 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004890 if (IS_ERR(trans)) {
4891 err = PTR_ERR(trans);
4892 goto fail;
4893 }
Chris Mason5f39d392007-10-15 16:14:19 -04004894
Miao Xie31534952011-04-13 13:19:21 +08004895 btrfs_inc_nlink(inode);
4896 inode->i_ctime = CURRENT_TIME;
4897
Chris Mason39279cc2007-06-12 06:35:45 -04004898 btrfs_set_trans_block_group(trans, dir);
Al Viro7de9c6ee2010-10-23 11:11:40 -04004899 ihold(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004900
Josef Bacika1b075d2010-11-19 20:36:11 +00004901 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04004902
Yan, Zhenga5719522009-09-24 09:17:31 -04004903 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04004904 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04004905 } else {
Josef Bacik6a912212010-11-20 09:48:00 +00004906 struct dentry *parent = dget_parent(dentry);
Yan, Zhenga5719522009-09-24 09:17:31 -04004907 btrfs_update_inode_block_group(trans, dir);
4908 err = btrfs_update_inode(trans, root, inode);
4909 BUG_ON(err);
Josef Bacik6a912212010-11-20 09:48:00 +00004910 btrfs_log_new_name(trans, inode, NULL, parent);
4911 dput(parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04004912 }
Chris Mason39279cc2007-06-12 06:35:45 -04004913
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004914 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04004915 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05004916fail:
Chris Mason39279cc2007-06-12 06:35:45 -04004917 if (drop_inode) {
4918 inode_dec_link_count(inode);
4919 iput(inode);
4920 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004921 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004922 return err;
4923}
4924
Chris Mason39279cc2007-06-12 06:35:45 -04004925static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
4926{
Chris Masonb9d86662008-05-02 16:13:49 -04004927 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04004928 struct btrfs_trans_handle *trans;
4929 struct btrfs_root *root = BTRFS_I(dir)->root;
4930 int err = 0;
4931 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04004932 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004933 u64 index = 0;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004934 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04004935
Josef Bacik9ed74f22009-09-11 16:12:44 -04004936 /*
4937 * 2 items for inode and ref
4938 * 2 items for dir items
4939 * 1 for xattr if selinux is on
4940 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004941 trans = btrfs_start_transaction(root, 5);
4942 if (IS_ERR(trans))
4943 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004944 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -04004945
Li Zefan581bb052011-04-20 10:06:11 +08004946 err = btrfs_find_free_ino(root, &objectid);
4947 if (err)
4948 goto out_fail;
4949
Josef Bacikaec74772008-07-24 12:12:38 -04004950 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08004951 dentry->d_name.len, btrfs_ino(dir), objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04004952 BTRFS_I(dir)->block_group, S_IFDIR | mode,
4953 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04004954 if (IS_ERR(inode)) {
4955 err = PTR_ERR(inode);
4956 goto out_fail;
4957 }
Chris Mason5f39d392007-10-15 16:14:19 -04004958
Chris Mason39279cc2007-06-12 06:35:45 -04004959 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04004960
Yan, Zhengf34f57a2009-11-12 09:35:27 +00004961 err = btrfs_init_inode_security(trans, inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04004962 if (err)
4963 goto out_fail;
4964
Chris Mason39279cc2007-06-12 06:35:45 -04004965 inode->i_op = &btrfs_dir_inode_operations;
4966 inode->i_fop = &btrfs_dir_file_operations;
4967 btrfs_set_trans_block_group(trans, inode);
4968
Chris Masondbe674a2008-07-17 12:54:05 -04004969 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004970 err = btrfs_update_inode(trans, root, inode);
4971 if (err)
4972 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04004973
Josef Bacika1b075d2010-11-19 20:36:11 +00004974 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
4975 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004976 if (err)
4977 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04004978
Chris Mason39279cc2007-06-12 06:35:45 -04004979 d_instantiate(dentry, inode);
4980 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004981 btrfs_update_inode_block_group(trans, inode);
4982 btrfs_update_inode_block_group(trans, dir);
4983
4984out_fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004985 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04004986 btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004987 if (drop_on_err)
4988 iput(inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004989 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004990 return err;
4991}
4992
Chris Masond352ac62008-09-29 15:18:18 -04004993/* helper for btfs_get_extent. Given an existing extent in the tree,
4994 * and an extent that you want to insert, deal with overlap and insert
4995 * the new extent into the tree.
4996 */
Chris Mason3b951512008-04-17 11:29:12 -04004997static int merge_extent_mapping(struct extent_map_tree *em_tree,
4998 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04004999 struct extent_map *em,
5000 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04005001{
5002 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04005003
Chris Masone6dcd2d2008-07-17 12:53:50 -04005004 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
5005 start_diff = map_start - em->start;
5006 em->start = map_start;
5007 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04005008 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
5009 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04005010 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04005011 em->block_len -= start_diff;
5012 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005013 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04005014}
5015
Chris Masonc8b97812008-10-29 14:49:59 -04005016static noinline int uncompress_inline(struct btrfs_path *path,
5017 struct inode *inode, struct page *page,
5018 size_t pg_offset, u64 extent_offset,
5019 struct btrfs_file_extent_item *item)
5020{
5021 int ret;
5022 struct extent_buffer *leaf = path->nodes[0];
5023 char *tmp;
5024 size_t max_size;
5025 unsigned long inline_size;
5026 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08005027 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04005028
5029 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08005030 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04005031 max_size = btrfs_file_extent_ram_bytes(leaf, item);
5032 inline_size = btrfs_file_extent_inline_item_len(leaf,
5033 btrfs_item_nr(leaf, path->slots[0]));
5034 tmp = kmalloc(inline_size, GFP_NOFS);
5035 ptr = btrfs_file_extent_inline_start(item);
5036
5037 read_extent_buffer(leaf, tmp, ptr, inline_size);
5038
Chris Mason5b050f02008-11-11 09:34:41 -05005039 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08005040 ret = btrfs_decompress(compress_type, tmp, page,
5041 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04005042 if (ret) {
5043 char *kaddr = kmap_atomic(page, KM_USER0);
5044 unsigned long copy_size = min_t(u64,
5045 PAGE_CACHE_SIZE - pg_offset,
5046 max_size - extent_offset);
5047 memset(kaddr + pg_offset, 0, copy_size);
5048 kunmap_atomic(kaddr, KM_USER0);
5049 }
5050 kfree(tmp);
5051 return 0;
5052}
5053
Chris Masond352ac62008-09-29 15:18:18 -04005054/*
5055 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05005056 * the ugly parts come from merging extents from the disk with the in-ram
5057 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04005058 * where the in-ram extents might be locked pending data=ordered completion.
5059 *
5060 * This also copies inline extents directly into the page.
5061 */
Chris Masond3977122009-01-05 21:25:51 -05005062
Chris Masona52d9a82007-08-27 16:49:44 -04005063struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05005064 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04005065 int create)
5066{
5067 int ret;
5068 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04005069 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04005070 u64 extent_start = 0;
5071 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08005072 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04005073 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04005074 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04005075 struct btrfs_root *root = BTRFS_I(inode)->root;
5076 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04005077 struct extent_buffer *leaf;
5078 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04005079 struct extent_map *em = NULL;
5080 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05005081 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04005082 struct btrfs_trans_handle *trans = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08005083 int compress_type;
Chris Masona52d9a82007-08-27 16:49:44 -04005084
Chris Masona52d9a82007-08-27 16:49:44 -04005085again:
Chris Mason890871b2009-09-02 16:24:52 -04005086 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005087 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04005088 if (em)
5089 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04005090 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005091
Chris Masona52d9a82007-08-27 16:49:44 -04005092 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04005093 if (em->start > start || em->start + em->len <= start)
5094 free_extent_map(em);
5095 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05005096 free_extent_map(em);
5097 else
5098 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005099 }
Chris Masond1310b22008-01-24 16:13:08 -05005100 em = alloc_extent_map(GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04005101 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05005102 err = -ENOMEM;
5103 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005104 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005105 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05005106 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05005107 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05005108 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04005109 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04005110
5111 if (!path) {
5112 path = btrfs_alloc_path();
5113 BUG_ON(!path);
5114 }
5115
Chris Mason179e29e2007-11-01 11:28:41 -04005116 ret = btrfs_lookup_file_extent(trans, root, path,
5117 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04005118 if (ret < 0) {
5119 err = ret;
5120 goto out;
5121 }
5122
5123 if (ret != 0) {
5124 if (path->slots[0] == 0)
5125 goto not_found;
5126 path->slots[0]--;
5127 }
5128
Chris Mason5f39d392007-10-15 16:14:19 -04005129 leaf = path->nodes[0];
5130 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04005131 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04005132 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04005133 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5134 found_type = btrfs_key_type(&found_key);
5135 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04005136 found_type != BTRFS_EXTENT_DATA_KEY) {
5137 goto not_found;
5138 }
5139
Chris Mason5f39d392007-10-15 16:14:19 -04005140 found_type = btrfs_file_extent_type(leaf, item);
5141 extent_start = found_key.offset;
Li Zefan261507a02010-12-17 14:21:50 +08005142 compress_type = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04005143 if (found_type == BTRFS_FILE_EXTENT_REG ||
5144 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04005145 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04005146 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04005147 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
5148 size_t size;
5149 size = btrfs_file_extent_inline_len(leaf, item);
5150 extent_end = (extent_start + size + root->sectorsize - 1) &
5151 ~((u64)root->sectorsize - 1);
5152 }
5153
5154 if (start >= extent_end) {
5155 path->slots[0]++;
5156 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
5157 ret = btrfs_next_leaf(root, path);
5158 if (ret < 0) {
5159 err = ret;
5160 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005161 }
Yan Zheng9036c102008-10-30 14:19:41 -04005162 if (ret > 0)
5163 goto not_found;
5164 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04005165 }
Yan Zheng9036c102008-10-30 14:19:41 -04005166 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5167 if (found_key.objectid != objectid ||
5168 found_key.type != BTRFS_EXTENT_DATA_KEY)
5169 goto not_found;
5170 if (start + len <= found_key.offset)
5171 goto not_found;
5172 em->start = start;
5173 em->len = found_key.offset - start;
5174 goto not_found_em;
5175 }
5176
Yan Zhengd899e052008-10-30 14:25:28 -04005177 if (found_type == BTRFS_FILE_EXTENT_REG ||
5178 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04005179 em->start = extent_start;
5180 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05005181 em->orig_start = extent_start -
5182 btrfs_file_extent_offset(leaf, item);
Chris Masondb945352007-10-15 16:15:53 -04005183 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
5184 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04005185 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04005186 goto insert;
5187 }
Li Zefan261507a02010-12-17 14:21:50 +08005188 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04005189 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08005190 em->compress_type = compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04005191 em->block_start = bytenr;
5192 em->block_len = btrfs_file_extent_disk_num_bytes(leaf,
5193 item);
5194 } else {
5195 bytenr += btrfs_file_extent_offset(leaf, item);
5196 em->block_start = bytenr;
5197 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04005198 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
5199 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04005200 }
Chris Masona52d9a82007-08-27 16:49:44 -04005201 goto insert;
5202 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04005203 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04005204 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04005205 size_t size;
5206 size_t extent_offset;
5207 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04005208
Chris Masona52d9a82007-08-27 16:49:44 -04005209 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04005210 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04005211 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04005212 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04005213 goto out;
5214 }
5215
Yan Zheng9036c102008-10-30 14:19:41 -04005216 size = btrfs_file_extent_inline_len(leaf, item);
5217 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05005218 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04005219 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04005220 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05005221 em->len = (copy_size + root->sectorsize - 1) &
5222 ~((u64)root->sectorsize - 1);
Yan Zhengff5b7ee2008-11-10 07:34:43 -05005223 em->orig_start = EXTENT_MAP_INLINE;
Li Zefan261507a02010-12-17 14:21:50 +08005224 if (compress_type) {
Chris Masonc8b97812008-10-29 14:49:59 -04005225 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08005226 em->compress_type = compress_type;
5227 }
Yan689f9342007-10-29 11:41:07 -04005228 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04005229 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08005230 if (btrfs_file_extent_compression(leaf, item) !=
5231 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04005232 ret = uncompress_inline(path, inode, page,
5233 pg_offset,
5234 extent_offset, item);
5235 BUG_ON(ret);
5236 } else {
5237 map = kmap(page);
5238 read_extent_buffer(leaf, map + pg_offset, ptr,
5239 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04005240 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
5241 memset(map + pg_offset + copy_size, 0,
5242 PAGE_CACHE_SIZE - pg_offset -
5243 copy_size);
5244 }
Chris Masonc8b97812008-10-29 14:49:59 -04005245 kunmap(page);
5246 }
Chris Mason179e29e2007-11-01 11:28:41 -04005247 flush_dcache_page(page);
5248 } else if (create && PageUptodate(page)) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005249 WARN_ON(1);
Chris Mason179e29e2007-11-01 11:28:41 -04005250 if (!trans) {
5251 kunmap(page);
5252 free_extent_map(em);
5253 em = NULL;
5254 btrfs_release_path(root, path);
Chris Masonf9295742008-07-17 12:54:14 -04005255 trans = btrfs_join_transaction(root, 1);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005256 if (IS_ERR(trans))
5257 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04005258 goto again;
5259 }
Chris Masonc8b97812008-10-29 14:49:59 -04005260 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05005261 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04005262 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04005263 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04005264 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04005265 }
Chris Masond1310b22008-01-24 16:13:08 -05005266 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00005267 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04005268 goto insert;
5269 } else {
Chris Masond3977122009-01-05 21:25:51 -05005270 printk(KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04005271 WARN_ON(1);
5272 }
5273not_found:
5274 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05005275 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04005276not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04005277 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04005278 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04005279insert:
5280 btrfs_release_path(root, path);
Chris Masond1310b22008-01-24 16:13:08 -05005281 if (em->start > start || extent_map_end(em) <= start) {
Chris Masond3977122009-01-05 21:25:51 -05005282 printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
5283 "[%llu %llu]\n", (unsigned long long)em->start,
5284 (unsigned long long)em->len,
5285 (unsigned long long)start,
5286 (unsigned long long)len);
Chris Masona52d9a82007-08-27 16:49:44 -04005287 err = -EIO;
5288 goto out;
5289 }
Chris Masond1310b22008-01-24 16:13:08 -05005290
5291 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04005292 write_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04005293 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04005294 /* it is possible that someone inserted the extent into the tree
5295 * while we had the lock dropped. It is also possible that
5296 * an overlapping map exists in the tree
5297 */
Chris Masona52d9a82007-08-27 16:49:44 -04005298 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04005299 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005300
5301 ret = 0;
5302
Chris Mason3b951512008-04-17 11:29:12 -04005303 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04005304 if (existing && (existing->start > start ||
5305 existing->start + existing->len <= start)) {
5306 free_extent_map(existing);
5307 existing = NULL;
5308 }
Chris Mason3b951512008-04-17 11:29:12 -04005309 if (!existing) {
5310 existing = lookup_extent_mapping(em_tree, em->start,
5311 em->len);
5312 if (existing) {
5313 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005314 em, start,
5315 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04005316 free_extent_map(existing);
5317 if (err) {
5318 free_extent_map(em);
5319 em = NULL;
5320 }
5321 } else {
5322 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04005323 free_extent_map(em);
5324 em = NULL;
5325 }
5326 } else {
5327 free_extent_map(em);
5328 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005329 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04005330 }
Chris Masona52d9a82007-08-27 16:49:44 -04005331 }
Chris Mason890871b2009-09-02 16:24:52 -04005332 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04005333out:
liubo1abe9b82011-03-24 11:18:59 +00005334
5335 trace_btrfs_get_extent(root, em);
5336
Chris Masonf4219502008-07-22 11:18:09 -04005337 if (path)
5338 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04005339 if (trans) {
5340 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05005341 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04005342 err = ret;
5343 }
Chris Masona52d9a82007-08-27 16:49:44 -04005344 if (err) {
5345 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04005346 return ERR_PTR(err);
5347 }
5348 return em;
5349}
5350
Chris Masonec29ed52011-02-23 16:23:20 -05005351struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
5352 size_t pg_offset, u64 start, u64 len,
5353 int create)
5354{
5355 struct extent_map *em;
5356 struct extent_map *hole_em = NULL;
5357 u64 range_start = start;
5358 u64 end;
5359 u64 found;
5360 u64 found_end;
5361 int err = 0;
5362
5363 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
5364 if (IS_ERR(em))
5365 return em;
5366 if (em) {
5367 /*
5368 * if our em maps to a hole, there might
5369 * actually be delalloc bytes behind it
5370 */
5371 if (em->block_start != EXTENT_MAP_HOLE)
5372 return em;
5373 else
5374 hole_em = em;
5375 }
5376
5377 /* check to see if we've wrapped (len == -1 or similar) */
5378 end = start + len;
5379 if (end < start)
5380 end = (u64)-1;
5381 else
5382 end -= 1;
5383
5384 em = NULL;
5385
5386 /* ok, we didn't find anything, lets look for delalloc */
5387 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
5388 end, len, EXTENT_DELALLOC, 1);
5389 found_end = range_start + found;
5390 if (found_end < range_start)
5391 found_end = (u64)-1;
5392
5393 /*
5394 * we didn't find anything useful, return
5395 * the original results from get_extent()
5396 */
5397 if (range_start > end || found_end <= start) {
5398 em = hole_em;
5399 hole_em = NULL;
5400 goto out;
5401 }
5402
5403 /* adjust the range_start to make sure it doesn't
5404 * go backwards from the start they passed in
5405 */
5406 range_start = max(start,range_start);
5407 found = found_end - range_start;
5408
5409 if (found > 0) {
5410 u64 hole_start = start;
5411 u64 hole_len = len;
5412
5413 em = alloc_extent_map(GFP_NOFS);
5414 if (!em) {
5415 err = -ENOMEM;
5416 goto out;
5417 }
5418 /*
5419 * when btrfs_get_extent can't find anything it
5420 * returns one huge hole
5421 *
5422 * make sure what it found really fits our range, and
5423 * adjust to make sure it is based on the start from
5424 * the caller
5425 */
5426 if (hole_em) {
5427 u64 calc_end = extent_map_end(hole_em);
5428
5429 if (calc_end <= start || (hole_em->start > end)) {
5430 free_extent_map(hole_em);
5431 hole_em = NULL;
5432 } else {
5433 hole_start = max(hole_em->start, start);
5434 hole_len = calc_end - hole_start;
5435 }
5436 }
5437 em->bdev = NULL;
5438 if (hole_em && range_start > hole_start) {
5439 /* our hole starts before our delalloc, so we
5440 * have to return just the parts of the hole
5441 * that go until the delalloc starts
5442 */
5443 em->len = min(hole_len,
5444 range_start - hole_start);
5445 em->start = hole_start;
5446 em->orig_start = hole_start;
5447 /*
5448 * don't adjust block start at all,
5449 * it is fixed at EXTENT_MAP_HOLE
5450 */
5451 em->block_start = hole_em->block_start;
5452 em->block_len = hole_len;
5453 } else {
5454 em->start = range_start;
5455 em->len = found;
5456 em->orig_start = range_start;
5457 em->block_start = EXTENT_MAP_DELALLOC;
5458 em->block_len = found;
5459 }
5460 } else if (hole_em) {
5461 return hole_em;
5462 }
5463out:
5464
5465 free_extent_map(hole_em);
5466 if (err) {
5467 free_extent_map(em);
5468 return ERR_PTR(err);
5469 }
5470 return em;
5471}
5472
Josef Bacik4b46fce2010-05-23 11:00:55 -04005473static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
Josef Bacik16d299a2011-04-06 14:53:07 -04005474 struct extent_map *em,
Josef Bacik4b46fce2010-05-23 11:00:55 -04005475 u64 start, u64 len)
5476{
5477 struct btrfs_root *root = BTRFS_I(inode)->root;
5478 struct btrfs_trans_handle *trans;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005479 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
5480 struct btrfs_key ins;
5481 u64 alloc_hint;
5482 int ret;
Josef Bacik16d299a2011-04-06 14:53:07 -04005483 bool insert = false;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005484
Josef Bacik16d299a2011-04-06 14:53:07 -04005485 /*
5486 * Ok if the extent map we looked up is a hole and is for the exact
5487 * range we want, there is no reason to allocate a new one, however if
5488 * it is not right then we need to free this one and drop the cache for
5489 * our range.
5490 */
5491 if (em->block_start != EXTENT_MAP_HOLE || em->start != start ||
5492 em->len != len) {
5493 free_extent_map(em);
5494 em = NULL;
5495 insert = true;
5496 btrfs_drop_extent_cache(inode, start, start + len - 1, 0);
5497 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04005498
5499 trans = btrfs_join_transaction(root, 0);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005500 if (IS_ERR(trans))
5501 return ERR_CAST(trans);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005502
5503 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
5504
5505 alloc_hint = get_extent_allocation_hint(inode, start, len);
5506 ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0,
5507 alloc_hint, (u64)-1, &ins, 1);
5508 if (ret) {
5509 em = ERR_PTR(ret);
5510 goto out;
5511 }
5512
Josef Bacik4b46fce2010-05-23 11:00:55 -04005513 if (!em) {
Josef Bacik16d299a2011-04-06 14:53:07 -04005514 em = alloc_extent_map(GFP_NOFS);
5515 if (!em) {
5516 em = ERR_PTR(-ENOMEM);
5517 goto out;
5518 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04005519 }
5520
5521 em->start = start;
5522 em->orig_start = em->start;
5523 em->len = ins.offset;
5524
5525 em->block_start = ins.objectid;
5526 em->block_len = ins.offset;
5527 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik16d299a2011-04-06 14:53:07 -04005528
5529 /*
5530 * We need to do this because if we're using the original em we searched
5531 * for, we could have EXTENT_FLAG_VACANCY set, and we don't want that.
5532 */
5533 em->flags = 0;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005534 set_bit(EXTENT_FLAG_PINNED, &em->flags);
5535
Josef Bacik16d299a2011-04-06 14:53:07 -04005536 while (insert) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04005537 write_lock(&em_tree->lock);
5538 ret = add_extent_mapping(em_tree, em);
5539 write_unlock(&em_tree->lock);
5540 if (ret != -EEXIST)
5541 break;
5542 btrfs_drop_extent_cache(inode, start, start + em->len - 1, 0);
5543 }
5544
5545 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
5546 ins.offset, ins.offset, 0);
5547 if (ret) {
5548 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
5549 em = ERR_PTR(ret);
5550 }
5551out:
5552 btrfs_end_transaction(trans, root);
5553 return em;
5554}
5555
Chris Mason46bfbb52010-05-26 11:04:10 -04005556/*
5557 * returns 1 when the nocow is safe, < 1 on error, 0 if the
5558 * block must be cow'd
5559 */
5560static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans,
5561 struct inode *inode, u64 offset, u64 len)
5562{
5563 struct btrfs_path *path;
5564 int ret;
5565 struct extent_buffer *leaf;
5566 struct btrfs_root *root = BTRFS_I(inode)->root;
5567 struct btrfs_file_extent_item *fi;
5568 struct btrfs_key key;
5569 u64 disk_bytenr;
5570 u64 backref_offset;
5571 u64 extent_end;
5572 u64 num_bytes;
5573 int slot;
5574 int found_type;
5575
5576 path = btrfs_alloc_path();
5577 if (!path)
5578 return -ENOMEM;
5579
Li Zefan33345d012011-04-20 10:31:50 +08005580 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04005581 offset, 0);
5582 if (ret < 0)
5583 goto out;
5584
5585 slot = path->slots[0];
5586 if (ret == 1) {
5587 if (slot == 0) {
5588 /* can't find the item, must cow */
5589 ret = 0;
5590 goto out;
5591 }
5592 slot--;
5593 }
5594 ret = 0;
5595 leaf = path->nodes[0];
5596 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08005597 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04005598 key.type != BTRFS_EXTENT_DATA_KEY) {
5599 /* not our file or wrong item type, must cow */
5600 goto out;
5601 }
5602
5603 if (key.offset > offset) {
5604 /* Wrong offset, must cow */
5605 goto out;
5606 }
5607
5608 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
5609 found_type = btrfs_file_extent_type(leaf, fi);
5610 if (found_type != BTRFS_FILE_EXTENT_REG &&
5611 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
5612 /* not a regular extent, must cow */
5613 goto out;
5614 }
5615 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
5616 backref_offset = btrfs_file_extent_offset(leaf, fi);
5617
5618 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
5619 if (extent_end < offset + len) {
5620 /* extent doesn't include our full range, must cow */
5621 goto out;
5622 }
5623
5624 if (btrfs_extent_readonly(root, disk_bytenr))
5625 goto out;
5626
5627 /*
5628 * look for other files referencing this extent, if we
5629 * find any we must cow
5630 */
Li Zefan33345d012011-04-20 10:31:50 +08005631 if (btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04005632 key.offset - backref_offset, disk_bytenr))
5633 goto out;
5634
5635 /*
5636 * adjust disk_bytenr and num_bytes to cover just the bytes
5637 * in this extent we are about to write. If there
5638 * are any csums in that range we have to cow in order
5639 * to keep the csums correct
5640 */
5641 disk_bytenr += backref_offset;
5642 disk_bytenr += offset - key.offset;
5643 num_bytes = min(offset + len, extent_end) - offset;
5644 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
5645 goto out;
5646 /*
5647 * all of the above have passed, it is safe to overwrite this extent
5648 * without cow
5649 */
5650 ret = 1;
5651out:
5652 btrfs_free_path(path);
5653 return ret;
5654}
5655
Josef Bacik4b46fce2010-05-23 11:00:55 -04005656static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
5657 struct buffer_head *bh_result, int create)
5658{
5659 struct extent_map *em;
5660 struct btrfs_root *root = BTRFS_I(inode)->root;
5661 u64 start = iblock << inode->i_blkbits;
5662 u64 len = bh_result->b_size;
Chris Mason46bfbb52010-05-26 11:04:10 -04005663 struct btrfs_trans_handle *trans;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005664
5665 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
5666 if (IS_ERR(em))
5667 return PTR_ERR(em);
5668
5669 /*
5670 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
5671 * io. INLINE is special, and we could probably kludge it in here, but
5672 * it's still buffered so for safety lets just fall back to the generic
5673 * buffered path.
5674 *
5675 * For COMPRESSED we _have_ to read the entire extent in so we can
5676 * decompress it, so there will be buffering required no matter what we
5677 * do, so go ahead and fallback to buffered.
5678 *
5679 * We return -ENOTBLK because thats what makes DIO go ahead and go back
5680 * to buffered IO. Don't blame me, this is the price we pay for using
5681 * the generic code.
5682 */
5683 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
5684 em->block_start == EXTENT_MAP_INLINE) {
5685 free_extent_map(em);
5686 return -ENOTBLK;
5687 }
5688
5689 /* Just a good old fashioned hole, return */
5690 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
5691 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
5692 free_extent_map(em);
5693 /* DIO will do one hole at a time, so just unlock a sector */
5694 unlock_extent(&BTRFS_I(inode)->io_tree, start,
5695 start + root->sectorsize - 1, GFP_NOFS);
5696 return 0;
5697 }
5698
5699 /*
5700 * We don't allocate a new extent in the following cases
5701 *
5702 * 1) The inode is marked as NODATACOW. In this case we'll just use the
5703 * existing extent.
5704 * 2) The extent is marked as PREALLOC. We're good to go here and can
5705 * just use the extent.
5706 *
5707 */
Chris Mason46bfbb52010-05-26 11:04:10 -04005708 if (!create) {
5709 len = em->len - (start - em->start);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005710 goto map;
Chris Mason46bfbb52010-05-26 11:04:10 -04005711 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04005712
5713 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
5714 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
5715 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04005716 int type;
5717 int ret;
Chris Mason46bfbb52010-05-26 11:04:10 -04005718 u64 block_start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005719
5720 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
5721 type = BTRFS_ORDERED_PREALLOC;
5722 else
5723 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04005724 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04005725 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04005726
5727 /*
5728 * we're not going to log anything, but we do need
5729 * to make sure the current transaction stays open
5730 * while we look for nocow cross refs
5731 */
5732 trans = btrfs_join_transaction(root, 0);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005733 if (IS_ERR(trans))
Chris Mason46bfbb52010-05-26 11:04:10 -04005734 goto must_cow;
5735
5736 if (can_nocow_odirect(trans, inode, start, len) == 1) {
5737 ret = btrfs_add_ordered_extent_dio(inode, start,
5738 block_start, len, len, type);
5739 btrfs_end_transaction(trans, root);
5740 if (ret) {
5741 free_extent_map(em);
5742 return ret;
5743 }
5744 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005745 }
Chris Mason46bfbb52010-05-26 11:04:10 -04005746 btrfs_end_transaction(trans, root);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005747 }
Chris Mason46bfbb52010-05-26 11:04:10 -04005748must_cow:
5749 /*
5750 * this will cow the extent, reset the len in case we changed
5751 * it above
5752 */
5753 len = bh_result->b_size;
Josef Bacik16d299a2011-04-06 14:53:07 -04005754 em = btrfs_new_extent_direct(inode, em, start, len);
Chris Mason46bfbb52010-05-26 11:04:10 -04005755 if (IS_ERR(em))
5756 return PTR_ERR(em);
5757 len = min(len, em->len - (start - em->start));
5758unlock:
Chris Mason4845e442010-05-25 20:56:50 -04005759 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, start + len - 1,
5760 EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DIRTY, 1,
5761 0, NULL, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005762map:
5763 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
5764 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04005765 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005766 bh_result->b_bdev = em->bdev;
5767 set_buffer_mapped(bh_result);
5768 if (create && !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
5769 set_buffer_new(bh_result);
5770
5771 free_extent_map(em);
5772
5773 return 0;
5774}
5775
5776struct btrfs_dio_private {
5777 struct inode *inode;
5778 u64 logical_offset;
5779 u64 disk_bytenr;
5780 u64 bytes;
5781 u32 *csums;
5782 void *private;
Miao Xiee65e1532010-11-22 03:04:43 +00005783
5784 /* number of bios pending for this dio */
5785 atomic_t pending_bios;
5786
5787 /* IO errors */
5788 int errors;
5789
5790 struct bio *orig_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005791};
5792
5793static void btrfs_endio_direct_read(struct bio *bio, int err)
5794{
Miao Xiee65e1532010-11-22 03:04:43 +00005795 struct btrfs_dio_private *dip = bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005796 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
5797 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005798 struct inode *inode = dip->inode;
5799 struct btrfs_root *root = BTRFS_I(inode)->root;
5800 u64 start;
5801 u32 *private = dip->csums;
5802
5803 start = dip->logical_offset;
5804 do {
5805 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
5806 struct page *page = bvec->bv_page;
5807 char *kaddr;
5808 u32 csum = ~(u32)0;
5809 unsigned long flags;
5810
5811 local_irq_save(flags);
5812 kaddr = kmap_atomic(page, KM_IRQ0);
5813 csum = btrfs_csum_data(root, kaddr + bvec->bv_offset,
5814 csum, bvec->bv_len);
5815 btrfs_csum_final(csum, (char *)&csum);
5816 kunmap_atomic(kaddr, KM_IRQ0);
5817 local_irq_restore(flags);
5818
5819 flush_dcache_page(bvec->bv_page);
5820 if (csum != *private) {
Li Zefan33345d012011-04-20 10:31:50 +08005821 printk(KERN_ERR "btrfs csum failed ino %llu off"
Josef Bacik4b46fce2010-05-23 11:00:55 -04005822 " %llu csum %u private %u\n",
Li Zefan33345d012011-04-20 10:31:50 +08005823 (unsigned long long)btrfs_ino(inode),
5824 (unsigned long long)start,
Josef Bacik4b46fce2010-05-23 11:00:55 -04005825 csum, *private);
5826 err = -EIO;
5827 }
5828 }
5829
5830 start += bvec->bv_len;
5831 private++;
5832 bvec++;
5833 } while (bvec <= bvec_end);
5834
5835 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
5836 dip->logical_offset + dip->bytes - 1, GFP_NOFS);
5837 bio->bi_private = dip->private;
5838
5839 kfree(dip->csums);
5840 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04005841
5842 /* If we had a csum failure make sure to clear the uptodate flag */
5843 if (err)
5844 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005845 dio_end_io(bio, err);
5846}
5847
5848static void btrfs_endio_direct_write(struct bio *bio, int err)
5849{
5850 struct btrfs_dio_private *dip = bio->bi_private;
5851 struct inode *inode = dip->inode;
5852 struct btrfs_root *root = BTRFS_I(inode)->root;
5853 struct btrfs_trans_handle *trans;
5854 struct btrfs_ordered_extent *ordered = NULL;
5855 struct extent_state *cached_state = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05005856 u64 ordered_offset = dip->logical_offset;
5857 u64 ordered_bytes = dip->bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005858 int ret;
5859
5860 if (err)
5861 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05005862again:
5863 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
5864 &ordered_offset,
5865 ordered_bytes);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005866 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05005867 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005868
5869 BUG_ON(!ordered);
5870
5871 trans = btrfs_join_transaction(root, 1);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005872 if (IS_ERR(trans)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04005873 err = -ENOMEM;
5874 goto out;
5875 }
5876 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
5877
5878 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags)) {
5879 ret = btrfs_ordered_update_i_size(inode, 0, ordered);
5880 if (!ret)
5881 ret = btrfs_update_inode(trans, root, inode);
5882 err = ret;
5883 goto out;
5884 }
5885
5886 lock_extent_bits(&BTRFS_I(inode)->io_tree, ordered->file_offset,
5887 ordered->file_offset + ordered->len - 1, 0,
5888 &cached_state, GFP_NOFS);
5889
5890 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags)) {
5891 ret = btrfs_mark_extent_written(trans, inode,
5892 ordered->file_offset,
5893 ordered->file_offset +
5894 ordered->len);
5895 if (ret) {
5896 err = ret;
5897 goto out_unlock;
5898 }
5899 } else {
5900 ret = insert_reserved_file_extent(trans, inode,
5901 ordered->file_offset,
5902 ordered->start,
5903 ordered->disk_len,
5904 ordered->len,
5905 ordered->len,
5906 0, 0, 0,
5907 BTRFS_FILE_EXTENT_REG);
5908 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
5909 ordered->file_offset, ordered->len);
5910 if (ret) {
5911 err = ret;
5912 WARN_ON(1);
5913 goto out_unlock;
5914 }
5915 }
5916
5917 add_pending_csums(trans, inode, ordered->file_offset, &ordered->list);
Josef Bacik1ef30be2011-04-05 19:25:36 -04005918 ret = btrfs_ordered_update_i_size(inode, 0, ordered);
5919 if (!ret)
5920 btrfs_update_inode(trans, root, inode);
5921 ret = 0;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005922out_unlock:
5923 unlock_extent_cached(&BTRFS_I(inode)->io_tree, ordered->file_offset,
5924 ordered->file_offset + ordered->len - 1,
5925 &cached_state, GFP_NOFS);
5926out:
5927 btrfs_delalloc_release_metadata(inode, ordered->len);
5928 btrfs_end_transaction(trans, root);
Chris Mason163cf092010-11-28 19:56:33 -05005929 ordered_offset = ordered->file_offset + ordered->len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005930 btrfs_put_ordered_extent(ordered);
5931 btrfs_put_ordered_extent(ordered);
Chris Mason163cf092010-11-28 19:56:33 -05005932
5933out_test:
5934 /*
5935 * our bio might span multiple ordered extents. If we haven't
5936 * completed the accounting for the whole dio, go back and try again
5937 */
5938 if (ordered_offset < dip->logical_offset + dip->bytes) {
5939 ordered_bytes = dip->logical_offset + dip->bytes -
5940 ordered_offset;
5941 goto again;
5942 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04005943out_done:
5944 bio->bi_private = dip->private;
5945
5946 kfree(dip->csums);
5947 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04005948
5949 /* If we had an error make sure to clear the uptodate flag */
5950 if (err)
5951 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005952 dio_end_io(bio, err);
5953}
5954
Chris Masoneaf25d92010-05-25 09:48:28 -04005955static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
5956 struct bio *bio, int mirror_num,
5957 unsigned long bio_flags, u64 offset)
5958{
5959 int ret;
5960 struct btrfs_root *root = BTRFS_I(inode)->root;
5961 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
5962 BUG_ON(ret);
5963 return 0;
5964}
5965
Miao Xiee65e1532010-11-22 03:04:43 +00005966static void btrfs_end_dio_bio(struct bio *bio, int err)
5967{
5968 struct btrfs_dio_private *dip = bio->bi_private;
5969
5970 if (err) {
Li Zefan33345d012011-04-20 10:31:50 +08005971 printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu "
Jan Beulich3dd14622010-12-07 14:54:09 +00005972 "sector %#Lx len %u err no %d\n",
Li Zefan33345d012011-04-20 10:31:50 +08005973 (unsigned long long)btrfs_ino(dip->inode), bio->bi_rw,
Jan Beulich3dd14622010-12-07 14:54:09 +00005974 (unsigned long long)bio->bi_sector, bio->bi_size, err);
Miao Xiee65e1532010-11-22 03:04:43 +00005975 dip->errors = 1;
5976
5977 /*
5978 * before atomic variable goto zero, we must make sure
5979 * dip->errors is perceived to be set.
5980 */
5981 smp_mb__before_atomic_dec();
5982 }
5983
5984 /* if there are more bios still pending for this dio, just exit */
5985 if (!atomic_dec_and_test(&dip->pending_bios))
5986 goto out;
5987
5988 if (dip->errors)
5989 bio_io_error(dip->orig_bio);
5990 else {
5991 set_bit(BIO_UPTODATE, &dip->orig_bio->bi_flags);
5992 bio_endio(dip->orig_bio, 0);
5993 }
5994out:
5995 bio_put(bio);
5996}
5997
5998static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
5999 u64 first_sector, gfp_t gfp_flags)
6000{
6001 int nr_vecs = bio_get_nr_vecs(bdev);
6002 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
6003}
6004
6005static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
6006 int rw, u64 file_offset, int skip_sum,
Josef Bacik1ae39932011-04-06 14:41:34 -04006007 u32 *csums, int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00006008{
6009 int write = rw & REQ_WRITE;
6010 struct btrfs_root *root = BTRFS_I(inode)->root;
6011 int ret;
6012
6013 bio_get(bio);
6014 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
6015 if (ret)
6016 goto err;
6017
Josef Bacik1ae39932011-04-06 14:41:34 -04006018 if (skip_sum)
6019 goto map;
6020
6021 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00006022 ret = btrfs_wq_submit_bio(root->fs_info,
6023 inode, rw, bio, 0, 0,
6024 file_offset,
6025 __btrfs_submit_bio_start_direct_io,
6026 __btrfs_submit_bio_done);
6027 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04006028 } else if (write) {
6029 /*
6030 * If we aren't doing async submit, calculate the csum of the
6031 * bio now.
6032 */
6033 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
6034 if (ret)
6035 goto err;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00006036 } else if (!skip_sum) {
6037 ret = btrfs_lookup_bio_sums_dio(root, inode, bio,
Miao Xiee65e1532010-11-22 03:04:43 +00006038 file_offset, csums);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00006039 if (ret)
6040 goto err;
6041 }
Miao Xiee65e1532010-11-22 03:04:43 +00006042
Josef Bacik1ae39932011-04-06 14:41:34 -04006043map:
6044 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006045err:
6046 bio_put(bio);
6047 return ret;
6048}
6049
6050static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
6051 int skip_sum)
6052{
6053 struct inode *inode = dip->inode;
6054 struct btrfs_root *root = BTRFS_I(inode)->root;
6055 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
6056 struct bio *bio;
6057 struct bio *orig_bio = dip->orig_bio;
6058 struct bio_vec *bvec = orig_bio->bi_io_vec;
6059 u64 start_sector = orig_bio->bi_sector;
6060 u64 file_offset = dip->logical_offset;
6061 u64 submit_len = 0;
6062 u64 map_length;
6063 int nr_pages = 0;
6064 u32 *csums = dip->csums;
6065 int ret = 0;
Josef Bacik1ae39932011-04-06 14:41:34 -04006066 int async_submit = 0;
Josef Bacik98bc3142011-03-22 11:00:46 -04006067 int write = rw & REQ_WRITE;
Miao Xiee65e1532010-11-22 03:04:43 +00006068
Miao Xiee65e1532010-11-22 03:04:43 +00006069 map_length = orig_bio->bi_size;
6070 ret = btrfs_map_block(map_tree, READ, start_sector << 9,
6071 &map_length, NULL, 0);
6072 if (ret) {
6073 bio_put(bio);
6074 return -EIO;
6075 }
6076
Josef Bacik02f57c72011-04-06 14:25:44 -04006077 if (map_length >= orig_bio->bi_size) {
6078 bio = orig_bio;
6079 goto submit;
6080 }
6081
Josef Bacik1ae39932011-04-06 14:41:34 -04006082 async_submit = 1;
Josef Bacik02f57c72011-04-06 14:25:44 -04006083 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
6084 if (!bio)
6085 return -ENOMEM;
6086 bio->bi_private = dip;
6087 bio->bi_end_io = btrfs_end_dio_bio;
6088 atomic_inc(&dip->pending_bios);
6089
Miao Xiee65e1532010-11-22 03:04:43 +00006090 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
6091 if (unlikely(map_length < submit_len + bvec->bv_len ||
6092 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
6093 bvec->bv_offset) < bvec->bv_len)) {
6094 /*
6095 * inc the count before we submit the bio so
6096 * we know the end IO handler won't happen before
6097 * we inc the count. Otherwise, the dip might get freed
6098 * before we're done setting it up
6099 */
6100 atomic_inc(&dip->pending_bios);
6101 ret = __btrfs_submit_dio_bio(bio, inode, rw,
6102 file_offset, skip_sum,
Josef Bacik1ae39932011-04-06 14:41:34 -04006103 csums, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006104 if (ret) {
6105 bio_put(bio);
6106 atomic_dec(&dip->pending_bios);
6107 goto out_err;
6108 }
6109
Josef Bacik98bc3142011-03-22 11:00:46 -04006110 /* Write's use the ordered csums */
6111 if (!write && !skip_sum)
Miao Xiee65e1532010-11-22 03:04:43 +00006112 csums = csums + nr_pages;
6113 start_sector += submit_len >> 9;
6114 file_offset += submit_len;
6115
6116 submit_len = 0;
6117 nr_pages = 0;
6118
6119 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
6120 start_sector, GFP_NOFS);
6121 if (!bio)
6122 goto out_err;
6123 bio->bi_private = dip;
6124 bio->bi_end_io = btrfs_end_dio_bio;
6125
6126 map_length = orig_bio->bi_size;
6127 ret = btrfs_map_block(map_tree, READ, start_sector << 9,
6128 &map_length, NULL, 0);
6129 if (ret) {
6130 bio_put(bio);
6131 goto out_err;
6132 }
6133 } else {
6134 submit_len += bvec->bv_len;
6135 nr_pages ++;
6136 bvec++;
6137 }
6138 }
6139
Josef Bacik02f57c72011-04-06 14:25:44 -04006140submit:
Miao Xiee65e1532010-11-22 03:04:43 +00006141 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacik1ae39932011-04-06 14:41:34 -04006142 csums, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006143 if (!ret)
6144 return 0;
6145
6146 bio_put(bio);
6147out_err:
6148 dip->errors = 1;
6149 /*
6150 * before atomic variable goto zero, we must
6151 * make sure dip->errors is perceived to be set.
6152 */
6153 smp_mb__before_atomic_dec();
6154 if (atomic_dec_and_test(&dip->pending_bios))
6155 bio_io_error(dip->orig_bio);
6156
6157 /* bio_end_io() will handle error, so we needn't return it */
6158 return 0;
6159}
6160
Josef Bacik4b46fce2010-05-23 11:00:55 -04006161static void btrfs_submit_direct(int rw, struct bio *bio, struct inode *inode,
6162 loff_t file_offset)
6163{
6164 struct btrfs_root *root = BTRFS_I(inode)->root;
6165 struct btrfs_dio_private *dip;
6166 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006167 int skip_sum;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02006168 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006169 int ret = 0;
6170
6171 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
6172
6173 dip = kmalloc(sizeof(*dip), GFP_NOFS);
6174 if (!dip) {
6175 ret = -ENOMEM;
6176 goto free_ordered;
6177 }
6178 dip->csums = NULL;
6179
Josef Bacik98bc3142011-03-22 11:00:46 -04006180 /* Write's use the ordered csum stuff, so we don't need dip->csums */
6181 if (!write && !skip_sum) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04006182 dip->csums = kmalloc(sizeof(u32) * bio->bi_vcnt, GFP_NOFS);
6183 if (!dip->csums) {
Daniel J Bluemanb4966b72011-03-09 16:46:42 +00006184 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006185 ret = -ENOMEM;
6186 goto free_ordered;
6187 }
6188 }
6189
6190 dip->private = bio->bi_private;
6191 dip->inode = inode;
6192 dip->logical_offset = file_offset;
6193
Josef Bacik4b46fce2010-05-23 11:00:55 -04006194 dip->bytes = 0;
6195 do {
6196 dip->bytes += bvec->bv_len;
6197 bvec++;
6198 } while (bvec <= (bio->bi_io_vec + bio->bi_vcnt - 1));
6199
Chris Mason46bfbb52010-05-26 11:04:10 -04006200 dip->disk_bytenr = (u64)bio->bi_sector << 9;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006201 bio->bi_private = dip;
Miao Xiee65e1532010-11-22 03:04:43 +00006202 dip->errors = 0;
6203 dip->orig_bio = bio;
6204 atomic_set(&dip->pending_bios, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006205
6206 if (write)
6207 bio->bi_end_io = btrfs_endio_direct_write;
6208 else
6209 bio->bi_end_io = btrfs_endio_direct_read;
6210
Miao Xiee65e1532010-11-22 03:04:43 +00006211 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
6212 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04006213 return;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006214free_ordered:
6215 /*
6216 * If this is a write, we need to clean up the reserved space and kill
6217 * the ordered extent.
6218 */
6219 if (write) {
6220 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05006221 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006222 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
6223 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
6224 btrfs_free_reserved_extent(root, ordered->start,
6225 ordered->disk_len);
6226 btrfs_put_ordered_extent(ordered);
6227 btrfs_put_ordered_extent(ordered);
6228 }
6229 bio_endio(bio, ret);
6230}
6231
Chris Mason5a5f79b2010-05-26 21:33:37 -04006232static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
6233 const struct iovec *iov, loff_t offset,
6234 unsigned long nr_segs)
6235{
6236 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00006237 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04006238 size_t size;
6239 unsigned long addr;
6240 unsigned blocksize_mask = root->sectorsize - 1;
6241 ssize_t retval = -EINVAL;
6242 loff_t end = offset;
6243
6244 if (offset & blocksize_mask)
6245 goto out;
6246
6247 /* Check the memory alignment. Blocks cannot straddle pages */
6248 for (seg = 0; seg < nr_segs; seg++) {
6249 addr = (unsigned long)iov[seg].iov_base;
6250 size = iov[seg].iov_len;
6251 end += size;
Josef Bacika1b75f72011-04-08 15:51:18 +00006252 if ((addr & blocksize_mask) || (size & blocksize_mask))
Chris Mason5a5f79b2010-05-26 21:33:37 -04006253 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00006254
6255 /* If this is a write we don't need to check anymore */
6256 if (rw & WRITE)
6257 continue;
6258
6259 /*
6260 * Check to make sure we don't have duplicate iov_base's in this
6261 * iovec, if so return EINVAL, otherwise we'll get csum errors
6262 * when reading back.
6263 */
6264 for (i = seg + 1; i < nr_segs; i++) {
6265 if (iov[seg].iov_base == iov[i].iov_base)
6266 goto out;
6267 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04006268 }
6269 retval = 0;
6270out:
6271 return retval;
6272}
Chris Mason16432982008-04-10 10:23:21 -04006273static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
6274 const struct iovec *iov, loff_t offset,
6275 unsigned long nr_segs)
6276{
Josef Bacik4b46fce2010-05-23 11:00:55 -04006277 struct file *file = iocb->ki_filp;
6278 struct inode *inode = file->f_mapping->host;
6279 struct btrfs_ordered_extent *ordered;
Chris Mason4845e442010-05-25 20:56:50 -04006280 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006281 u64 lockstart, lockend;
6282 ssize_t ret;
Chris Mason4845e442010-05-25 20:56:50 -04006283 int writing = rw & WRITE;
6284 int write_bits = 0;
Chris Mason3f7c5792010-05-26 10:59:53 -04006285 size_t count = iov_length(iov, nr_segs);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006286
Chris Mason5a5f79b2010-05-26 21:33:37 -04006287 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
6288 offset, nr_segs)) {
6289 return 0;
6290 }
6291
Josef Bacik4b46fce2010-05-23 11:00:55 -04006292 lockstart = offset;
Chris Mason3f7c5792010-05-26 10:59:53 -04006293 lockend = offset + count - 1;
6294
6295 if (writing) {
6296 ret = btrfs_delalloc_reserve_space(inode, count);
6297 if (ret)
6298 goto out;
6299 }
Chris Mason4845e442010-05-25 20:56:50 -04006300
Josef Bacik4b46fce2010-05-23 11:00:55 -04006301 while (1) {
Chris Mason4845e442010-05-25 20:56:50 -04006302 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6303 0, &cached_state, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006304 /*
6305 * We're concerned with the entire range that we're going to be
6306 * doing DIO to, so we need to make sure theres no ordered
6307 * extents in this range.
6308 */
6309 ordered = btrfs_lookup_ordered_range(inode, lockstart,
6310 lockend - lockstart + 1);
6311 if (!ordered)
6312 break;
Chris Mason4845e442010-05-25 20:56:50 -04006313 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6314 &cached_state, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006315 btrfs_start_ordered_extent(inode, ordered, 1);
6316 btrfs_put_ordered_extent(ordered);
6317 cond_resched();
6318 }
6319
Chris Mason4845e442010-05-25 20:56:50 -04006320 /*
6321 * we don't use btrfs_set_extent_delalloc because we don't want
6322 * the dirty or uptodate bits
6323 */
6324 if (writing) {
6325 write_bits = EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING;
6326 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6327 EXTENT_DELALLOC, 0, NULL, &cached_state,
6328 GFP_NOFS);
6329 if (ret) {
6330 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6331 lockend, EXTENT_LOCKED | write_bits,
6332 1, 0, &cached_state, GFP_NOFS);
6333 goto out;
6334 }
6335 }
6336
6337 free_extent_state(cached_state);
6338 cached_state = NULL;
6339
Chris Mason5a5f79b2010-05-26 21:33:37 -04006340 ret = __blockdev_direct_IO(rw, iocb, inode,
6341 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
6342 iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
6343 btrfs_submit_direct, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006344
6345 if (ret < 0 && ret != -EIOCBQUEUED) {
Chris Mason4845e442010-05-25 20:56:50 -04006346 clear_extent_bit(&BTRFS_I(inode)->io_tree, offset,
6347 offset + iov_length(iov, nr_segs) - 1,
6348 EXTENT_LOCKED | write_bits, 1, 0,
6349 &cached_state, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006350 } else if (ret >= 0 && ret < iov_length(iov, nr_segs)) {
6351 /*
6352 * We're falling back to buffered, unlock the section we didn't
6353 * do IO on.
6354 */
Chris Mason4845e442010-05-25 20:56:50 -04006355 clear_extent_bit(&BTRFS_I(inode)->io_tree, offset + ret,
6356 offset + iov_length(iov, nr_segs) - 1,
6357 EXTENT_LOCKED | write_bits, 1, 0,
6358 &cached_state, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006359 }
Chris Mason4845e442010-05-25 20:56:50 -04006360out:
6361 free_extent_state(cached_state);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006362 return ret;
Chris Mason16432982008-04-10 10:23:21 -04006363}
6364
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05006365static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
6366 __u64 start, __u64 len)
6367{
Chris Masonec29ed52011-02-23 16:23:20 -05006368 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05006369}
6370
Chris Mason9ebefb182007-06-15 13:50:00 -04006371int btrfs_readpage(struct file *file, struct page *page)
6372{
Chris Masond1310b22008-01-24 16:13:08 -05006373 struct extent_io_tree *tree;
6374 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006375 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04006376}
Chris Mason1832a6d2007-12-21 16:27:21 -05006377
Chris Mason39279cc2007-06-12 06:35:45 -04006378static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
6379{
Chris Masond1310b22008-01-24 16:13:08 -05006380 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04006381
6382
6383 if (current->flags & PF_MEMALLOC) {
6384 redirty_page_for_writepage(wbc, page);
6385 unlock_page(page);
6386 return 0;
6387 }
Chris Masond1310b22008-01-24 16:13:08 -05006388 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006389 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
6390}
Chris Mason39279cc2007-06-12 06:35:45 -04006391
Chris Masonf4219502008-07-22 11:18:09 -04006392int btrfs_writepages(struct address_space *mapping,
6393 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04006394{
Chris Masond1310b22008-01-24 16:13:08 -05006395 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05006396
Chris Masond1310b22008-01-24 16:13:08 -05006397 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04006398 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
6399}
6400
Chris Mason3ab2fb52007-11-08 10:59:22 -05006401static int
6402btrfs_readpages(struct file *file, struct address_space *mapping,
6403 struct list_head *pages, unsigned nr_pages)
6404{
Chris Masond1310b22008-01-24 16:13:08 -05006405 struct extent_io_tree *tree;
6406 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05006407 return extent_readpages(tree, mapping, pages, nr_pages,
6408 btrfs_get_extent);
6409}
Chris Masone6dcd2d2008-07-17 12:53:50 -04006410static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04006411{
Chris Masond1310b22008-01-24 16:13:08 -05006412 struct extent_io_tree *tree;
6413 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04006414 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006415
Chris Masond1310b22008-01-24 16:13:08 -05006416 tree = &BTRFS_I(page->mapping->host)->io_tree;
6417 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05006418 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006419 if (ret == 1) {
6420 ClearPagePrivate(page);
6421 set_page_private(page, 0);
6422 page_cache_release(page);
6423 }
6424 return ret;
6425}
Chris Mason39279cc2007-06-12 06:35:45 -04006426
Chris Masone6dcd2d2008-07-17 12:53:50 -04006427static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
6428{
Chris Mason98509cf2008-09-11 15:51:43 -04006429 if (PageWriteback(page) || PageDirty(page))
6430 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05006431 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006432}
6433
Chris Masona52d9a82007-08-27 16:49:44 -04006434static void btrfs_invalidatepage(struct page *page, unsigned long offset)
6435{
Chris Masond1310b22008-01-24 16:13:08 -05006436 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006437 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00006438 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006439 u64 page_start = page_offset(page);
6440 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04006441
Chris Mason8b62b722009-09-02 16:53:46 -04006442
6443 /*
6444 * we have the page locked, so new writeback can't start,
6445 * and the dirty bit won't be cleared while we are here.
6446 *
6447 * Wait for IO on this page so that we can safely clear
6448 * the PagePrivate2 bit and do ordered accounting
6449 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006450 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04006451
Chris Masond1310b22008-01-24 16:13:08 -05006452 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006453 if (offset) {
6454 btrfs_releasepage(page, GFP_NOFS);
6455 return;
6456 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00006457 lock_extent_bits(tree, page_start, page_end, 0, &cached_state,
6458 GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006459 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
6460 page_offset(page));
6461 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04006462 /*
6463 * IO on this page will never be started, so we need
6464 * to account for any ordered extents now
6465 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006466 clear_extent_bit(tree, page_start, page_end,
6467 EXTENT_DIRTY | EXTENT_DELALLOC |
Josef Bacik32c00af2009-10-08 13:34:05 -04006468 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING, 1, 0,
Josef Bacik2ac55d42010-02-03 19:33:23 +00006469 &cached_state, GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04006470 /*
6471 * whoever cleared the private bit is responsible
6472 * for the finish_ordered_io
6473 */
6474 if (TestClearPagePrivate2(page)) {
6475 btrfs_finish_ordered_io(page->mapping->host,
6476 page_start, page_end);
6477 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006478 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +00006479 cached_state = NULL;
6480 lock_extent_bits(tree, page_start, page_end, 0, &cached_state,
6481 GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006482 }
6483 clear_extent_bit(tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04006484 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
Josef Bacik2ac55d42010-02-03 19:33:23 +00006485 EXTENT_DO_ACCOUNTING, 1, 1, &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006486 __btrfs_releasepage(page, GFP_NOFS);
6487
Chris Mason4a096752008-07-21 10:29:44 -04006488 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04006489 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04006490 ClearPagePrivate(page);
6491 set_page_private(page, 0);
6492 page_cache_release(page);
6493 }
Chris Mason39279cc2007-06-12 06:35:45 -04006494}
6495
Chris Mason9ebefb182007-06-15 13:50:00 -04006496/*
6497 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
6498 * called from a page fault handler when a page is first dirtied. Hence we must
6499 * be careful to check for EOF conditions here. We set the page up correctly
6500 * for a written page which means we get ENOSPC checking when writing into
6501 * holes and correct delalloc and unwritten extent mapping on filesystems that
6502 * support these features.
6503 *
6504 * We are not allowed to take the i_mutex here so we have to play games to
6505 * protect against truncate races as the page could now be beyond EOF. Because
6506 * vmtruncate() writes the inode size before removing pages, once we have the
6507 * page lock we can determine safely if the page is beyond EOF. If it is not
6508 * beyond EOF, then the page is guaranteed safe against truncation until we
6509 * unlock the page.
6510 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07006511int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04006512{
Nick Pigginc2ec1752009-03-31 15:23:21 -07006513 struct page *page = vmf->page;
Chris Mason6da6aba2007-12-18 16:15:09 -05006514 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05006515 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006516 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
6517 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00006518 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006519 char *kaddr;
6520 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04006521 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05006522 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04006523 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006524 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04006525
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006526 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Nick Piggin56a76f82009-03-31 15:23:23 -07006527 if (ret) {
6528 if (ret == -ENOMEM)
6529 ret = VM_FAULT_OOM;
6530 else /* -ENOSPC, -EIO, etc */
6531 ret = VM_FAULT_SIGBUS;
Chris Mason1832a6d2007-12-21 16:27:21 -05006532 goto out;
Nick Piggin56a76f82009-03-31 15:23:23 -07006533 }
Chris Mason1832a6d2007-12-21 16:27:21 -05006534
Nick Piggin56a76f82009-03-31 15:23:23 -07006535 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006536again:
Chris Mason9ebefb182007-06-15 13:50:00 -04006537 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04006538 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006539 page_start = page_offset(page);
6540 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04006541
Chris Mason9ebefb182007-06-15 13:50:00 -04006542 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04006543 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04006544 /* page got truncated out from underneath us */
6545 goto out_unlock;
6546 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006547 wait_on_page_writeback(page);
6548
Josef Bacik2ac55d42010-02-03 19:33:23 +00006549 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state,
6550 GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006551 set_page_extent_mapped(page);
6552
Chris Masoneb84ae02008-07-17 13:53:27 -04006553 /*
6554 * we can't set the delalloc bits if there are pending ordered
6555 * extents. Drop our locks and wait for them to finish
6556 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006557 ordered = btrfs_lookup_ordered_extent(inode, page_start);
6558 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00006559 unlock_extent_cached(io_tree, page_start, page_end,
6560 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006561 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04006562 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006563 btrfs_put_ordered_extent(ordered);
6564 goto again;
6565 }
6566
Josef Bacikfbf19082009-10-01 17:10:23 -04006567 /*
6568 * XXX - page_mkwrite gets called every time the page is dirtied, even
6569 * if it was already dirty, so for space accounting reasons we need to
6570 * clear any delalloc bits for the range we are fixing to save. There
6571 * is probably a better way to do this, but for now keep consistent with
6572 * prepare_pages in the normal write path.
6573 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00006574 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04006575 EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00006576 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04006577
Josef Bacik2ac55d42010-02-03 19:33:23 +00006578 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
6579 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006580 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00006581 unlock_extent_cached(io_tree, page_start, page_end,
6582 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006583 ret = VM_FAULT_SIGBUS;
6584 goto out_unlock;
6585 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006586 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04006587
6588 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04006589 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04006590 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04006591 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04006592 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04006593
Chris Masone6dcd2d2008-07-17 12:53:50 -04006594 if (zero_start != PAGE_CACHE_SIZE) {
6595 kaddr = kmap(page);
6596 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
6597 flush_dcache_page(page);
6598 kunmap(page);
6599 }
Chris Mason247e7432008-07-17 12:53:51 -04006600 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006601 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04006602 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04006603
Chris Mason257c62e2009-10-13 13:21:08 -04006604 BTRFS_I(inode)->last_trans = root->fs_info->generation;
6605 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
6606
Josef Bacik2ac55d42010-02-03 19:33:23 +00006607 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04006608
6609out_unlock:
Chris Mason50a9b212009-09-11 12:33:12 -04006610 if (!ret)
6611 return VM_FAULT_LOCKED;
Chris Mason9ebefb182007-06-15 13:50:00 -04006612 unlock_page(page);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006613 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason1832a6d2007-12-21 16:27:21 -05006614out:
Chris Mason9ebefb182007-06-15 13:50:00 -04006615 return ret;
6616}
6617
Josef Bacika41ad392011-01-31 15:30:16 -05006618static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006619{
6620 struct btrfs_root *root = BTRFS_I(inode)->root;
6621 int ret;
Josef Bacik3893e332011-01-31 16:03:11 -05006622 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006623 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04006624 unsigned long nr;
Chris Masondbe674a2008-07-17 12:54:05 -04006625 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04006626
Josef Bacik5d5e1032009-10-13 16:46:49 -04006627 ret = btrfs_truncate_page(inode->i_mapping, inode->i_size);
6628 if (ret)
Josef Bacika41ad392011-01-31 15:30:16 -05006629 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00006630
Chris Mason4a096752008-07-21 10:29:44 -04006631 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Yan, Zheng80825102009-11-12 09:35:36 +00006632 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04006633
Josef Bacikf0cd8462011-03-04 14:37:08 -05006634 trans = btrfs_start_transaction(root, 5);
6635 if (IS_ERR(trans))
6636 return PTR_ERR(trans);
6637
6638 btrfs_set_trans_block_group(trans, inode);
6639
6640 ret = btrfs_orphan_add(trans, inode);
6641 if (ret) {
6642 btrfs_end_transaction(trans, root);
6643 return ret;
6644 }
6645
6646 nr = trans->blocks_used;
6647 btrfs_end_transaction(trans, root);
6648 btrfs_btree_balance_dirty(root, nr);
6649
6650 /* Now start a transaction for the truncate */
Yan, Zhengd68fc572010-05-16 10:49:58 -04006651 trans = btrfs_start_transaction(root, 0);
Josef Bacik3893e332011-01-31 16:03:11 -05006652 if (IS_ERR(trans))
6653 return PTR_ERR(trans);
Yan, Zheng80825102009-11-12 09:35:36 +00006654 btrfs_set_trans_block_group(trans, inode);
Yan, Zhengd68fc572010-05-16 10:49:58 -04006655 trans->block_rsv = root->orphan_block_rsv;
Chris Mason5a3f23d2009-03-31 13:27:11 -04006656
6657 /*
6658 * setattr is responsible for setting the ordered_data_close flag,
6659 * but that is only tested during the last file release. That
6660 * could happen well after the next commit, leaving a great big
6661 * window where new writes may get lost if someone chooses to write
6662 * to this file after truncating to zero
6663 *
6664 * The inode doesn't have any dirty data here, and so if we commit
6665 * this is a noop. If someone immediately starts writing to the inode
6666 * it is very likely we'll catch some of their writes in this
6667 * transaction, and the commit will find this file on the ordered
6668 * data list with good things to send down.
6669 *
6670 * This is a best effort solution, there is still a window where
6671 * using truncate to replace the contents of the file will
6672 * end up with a zero length file after a crash.
6673 */
6674 if (inode->i_size == 0 && BTRFS_I(inode)->ordered_data_close)
6675 btrfs_add_ordered_operation(trans, root, inode);
6676
Yan, Zheng80825102009-11-12 09:35:36 +00006677 while (1) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04006678 if (!trans) {
6679 trans = btrfs_start_transaction(root, 0);
Josef Bacik3893e332011-01-31 16:03:11 -05006680 if (IS_ERR(trans))
6681 return PTR_ERR(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04006682 btrfs_set_trans_block_group(trans, inode);
6683 trans->block_rsv = root->orphan_block_rsv;
6684 }
6685
6686 ret = btrfs_block_rsv_check(trans, root,
6687 root->orphan_block_rsv, 0, 5);
Josef Bacik3893e332011-01-31 16:03:11 -05006688 if (ret == -EAGAIN) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04006689 ret = btrfs_commit_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05006690 if (ret)
6691 return ret;
Yan, Zhengd68fc572010-05-16 10:49:58 -04006692 trans = NULL;
6693 continue;
Josef Bacik3893e332011-01-31 16:03:11 -05006694 } else if (ret) {
6695 err = ret;
6696 break;
Yan, Zhengd68fc572010-05-16 10:49:58 -04006697 }
6698
Yan, Zheng80825102009-11-12 09:35:36 +00006699 ret = btrfs_truncate_inode_items(trans, root, inode,
6700 inode->i_size,
6701 BTRFS_EXTENT_DATA_KEY);
Josef Bacik3893e332011-01-31 16:03:11 -05006702 if (ret != -EAGAIN) {
6703 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00006704 break;
Josef Bacik3893e332011-01-31 16:03:11 -05006705 }
Chris Mason39279cc2007-06-12 06:35:45 -04006706
Yan, Zheng80825102009-11-12 09:35:36 +00006707 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05006708 if (ret) {
6709 err = ret;
6710 break;
6711 }
Chris Mason5f39d392007-10-15 16:14:19 -04006712
Yan, Zheng80825102009-11-12 09:35:36 +00006713 nr = trans->blocks_used;
6714 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04006715 trans = NULL;
Yan, Zheng80825102009-11-12 09:35:36 +00006716 btrfs_btree_balance_dirty(root, nr);
Yan, Zheng80825102009-11-12 09:35:36 +00006717 }
6718
6719 if (ret == 0 && inode->i_nlink > 0) {
6720 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05006721 if (ret)
6722 err = ret;
Josef Bacikded5db92011-03-04 14:09:46 -05006723 } else if (ret && inode->i_nlink > 0) {
6724 /*
6725 * Failed to do the truncate, remove us from the in memory
6726 * orphan list.
6727 */
6728 ret = btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00006729 }
6730
6731 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05006732 if (ret && !err)
6733 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04006734
Josef Bacik7b128762008-07-24 12:17:14 -04006735 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04006736 ret = btrfs_end_transaction_throttle(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05006737 if (ret && !err)
6738 err = ret;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04006739 btrfs_btree_balance_dirty(root, nr);
Josef Bacika41ad392011-01-31 15:30:16 -05006740
Josef Bacik3893e332011-01-31 16:03:11 -05006741 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04006742}
6743
Sven Wegener3b963622008-06-09 21:57:42 -04006744/*
Chris Masond352ac62008-09-29 15:18:18 -04006745 * create a new subvolume directory/inode (helper for the ioctl).
6746 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05006747int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Yan, Zheng76dda932009-09-21 16:00:26 -04006748 struct btrfs_root *new_root,
Yan Zhengd2fb3432008-12-11 16:30:39 -05006749 u64 new_dirid, u64 alloc_hint)
Chris Mason39279cc2007-06-12 06:35:45 -04006750{
Chris Mason39279cc2007-06-12 06:35:45 -04006751 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04006752 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006753 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006754
Josef Bacikaec74772008-07-24 12:12:38 -04006755 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
Yan Zhengd2fb3432008-12-11 16:30:39 -05006756 new_dirid, alloc_hint, S_IFDIR | 0700, &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04006757 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04006758 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006759 inode->i_op = &btrfs_dir_inode_operations;
6760 inode->i_fop = &btrfs_dir_file_operations;
6761
Chris Mason39279cc2007-06-12 06:35:45 -04006762 inode->i_nlink = 1;
Chris Masondbe674a2008-07-17 12:54:05 -04006763 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04006764
Yan, Zheng76dda932009-09-21 16:00:26 -04006765 err = btrfs_update_inode(trans, new_root, inode);
6766 BUG_ON(err);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04006767
Yan, Zheng76dda932009-09-21 16:00:26 -04006768 iput(inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04006769 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006770}
6771
Chris Masond352ac62008-09-29 15:18:18 -04006772/* helper function for file defrag and space balancing. This
6773 * forces readahead on a given range of bytes in an inode
6774 */
Chris Masonedbd8d42007-12-21 16:27:24 -05006775unsigned long btrfs_force_ra(struct address_space *mapping,
Chris Mason86479a02007-09-10 19:58:16 -04006776 struct file_ra_state *ra, struct file *file,
6777 pgoff_t offset, pgoff_t last_index)
6778{
Chris Mason8e7bf942008-04-28 09:02:36 -04006779 pgoff_t req_size = last_index - offset + 1;
Chris Mason86479a02007-09-10 19:58:16 -04006780
Chris Mason86479a02007-09-10 19:58:16 -04006781 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
6782 return offset + req_size;
Chris Mason86479a02007-09-10 19:58:16 -04006783}
6784
Chris Mason39279cc2007-06-12 06:35:45 -04006785struct inode *btrfs_alloc_inode(struct super_block *sb)
6786{
6787 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006788 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006789
6790 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
6791 if (!ei)
6792 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006793
6794 ei->root = NULL;
6795 ei->space_info = NULL;
6796 ei->generation = 0;
6797 ei->sequence = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04006798 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04006799 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04006800 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006801 ei->delalloc_bytes = 0;
6802 ei->reserved_bytes = 0;
6803 ei->disk_i_size = 0;
6804 ei->flags = 0;
6805 ei->index_cnt = (u64)-1;
6806 ei->last_unlink_trans = 0;
6807
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006808 atomic_set(&ei->outstanding_extents, 0);
Josef Bacik57a45ced2011-01-25 16:30:38 -05006809 atomic_set(&ei->reserved_extents, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006810
6811 ei->ordered_data_close = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04006812 ei->orphan_meta_reserved = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006813 ei->dummy_inode = 0;
Li Zefan261507a02010-12-17 14:21:50 +08006814 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006815
6816 inode = &ei->vfs_inode;
6817 extent_map_tree_init(&ei->extent_tree, GFP_NOFS);
6818 extent_io_tree_init(&ei->io_tree, &inode->i_data, GFP_NOFS);
6819 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data, GFP_NOFS);
6820 mutex_init(&ei->log_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006821 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Josef Bacik7b128762008-07-24 12:17:14 -04006822 INIT_LIST_HEAD(&ei->i_orphan);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006823 INIT_LIST_HEAD(&ei->delalloc_inodes);
Chris Mason5a3f23d2009-03-31 13:27:11 -04006824 INIT_LIST_HEAD(&ei->ordered_operations);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006825 RB_CLEAR_NODE(&ei->rb_node);
6826
6827 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006828}
6829
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11006830static void btrfs_i_callback(struct rcu_head *head)
6831{
6832 struct inode *inode = container_of(head, struct inode, i_rcu);
6833 INIT_LIST_HEAD(&inode->i_dentry);
6834 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
6835}
6836
Chris Mason39279cc2007-06-12 06:35:45 -04006837void btrfs_destroy_inode(struct inode *inode)
6838{
Chris Masone6dcd2d2008-07-17 12:53:50 -04006839 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04006840 struct btrfs_root *root = BTRFS_I(inode)->root;
6841
Chris Mason39279cc2007-06-12 06:35:45 -04006842 WARN_ON(!list_empty(&inode->i_dentry));
6843 WARN_ON(inode->i_data.nrpages);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006844 WARN_ON(atomic_read(&BTRFS_I(inode)->outstanding_extents));
Josef Bacik57a45ced2011-01-25 16:30:38 -05006845 WARN_ON(atomic_read(&BTRFS_I(inode)->reserved_extents));
Chris Mason39279cc2007-06-12 06:35:45 -04006846
Chris Mason5a3f23d2009-03-31 13:27:11 -04006847 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05006848 * This can happen where we create an inode, but somebody else also
6849 * created the same inode and we need to destroy the one we already
6850 * created.
6851 */
6852 if (!root)
6853 goto free;
6854
6855 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04006856 * Make sure we're properly removed from the ordered operation
6857 * lists.
6858 */
6859 smp_mb();
6860 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
6861 spin_lock(&root->fs_info->ordered_extent_lock);
6862 list_del_init(&BTRFS_I(inode)->ordered_operations);
6863 spin_unlock(&root->fs_info->ordered_extent_lock);
6864 }
6865
Josef Bacik0af3d002010-06-21 14:48:16 -04006866 if (root == root->fs_info->tree_root) {
6867 struct btrfs_block_group_cache *block_group;
6868
6869 block_group = btrfs_lookup_block_group(root->fs_info,
6870 BTRFS_I(inode)->block_group);
6871 if (block_group && block_group->inode == inode) {
6872 spin_lock(&block_group->lock);
6873 block_group->inode = NULL;
6874 spin_unlock(&block_group->lock);
6875 btrfs_put_block_group(block_group);
6876 } else if (block_group) {
6877 btrfs_put_block_group(block_group);
6878 }
6879 }
6880
Yan, Zhengd68fc572010-05-16 10:49:58 -04006881 spin_lock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04006882 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
Li Zefan33345d012011-04-20 10:31:50 +08006883 printk(KERN_INFO "BTRFS: inode %llu still on the orphan list\n",
6884 (unsigned long long)btrfs_ino(inode));
Yan, Zheng80825102009-11-12 09:35:36 +00006885 list_del_init(&BTRFS_I(inode)->i_orphan);
Josef Bacik7b128762008-07-24 12:17:14 -04006886 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04006887 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04006888
Chris Masond3977122009-01-05 21:25:51 -05006889 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006890 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
6891 if (!ordered)
6892 break;
6893 else {
Chris Masond3977122009-01-05 21:25:51 -05006894 printk(KERN_ERR "btrfs found ordered "
6895 "extent %llu %llu on inode cleanup\n",
6896 (unsigned long long)ordered->file_offset,
6897 (unsigned long long)ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006898 btrfs_remove_ordered_extent(inode, ordered);
6899 btrfs_put_ordered_extent(ordered);
6900 btrfs_put_ordered_extent(ordered);
6901 }
6902 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006903 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04006904 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05006905free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11006906 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04006907}
6908
Al Viro45321ac2010-06-07 13:43:19 -04006909int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04006910{
6911 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04006912
Josef Bacik0af3d002010-06-21 14:48:16 -04006913 if (btrfs_root_refs(&root->root_item) == 0 &&
Li Zefan82d59022011-04-20 10:33:24 +08006914 !is_free_space_inode(root, inode))
Al Viro45321ac2010-06-07 13:43:19 -04006915 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04006916 else
Al Viro45321ac2010-06-07 13:43:19 -04006917 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04006918}
6919
Sven Wegener0ee0fda2008-07-30 16:54:26 -04006920static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04006921{
6922 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
6923
6924 inode_init_once(&ei->vfs_inode);
6925}
6926
6927void btrfs_destroy_cachep(void)
6928{
6929 if (btrfs_inode_cachep)
6930 kmem_cache_destroy(btrfs_inode_cachep);
6931 if (btrfs_trans_handle_cachep)
6932 kmem_cache_destroy(btrfs_trans_handle_cachep);
6933 if (btrfs_transaction_cachep)
6934 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04006935 if (btrfs_path_cachep)
6936 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05006937 if (btrfs_free_space_cachep)
6938 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04006939}
6940
6941int btrfs_init_cachep(void)
6942{
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02006943 btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
6944 sizeof(struct btrfs_inode), 0,
6945 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04006946 if (!btrfs_inode_cachep)
6947 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02006948
6949 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
6950 sizeof(struct btrfs_trans_handle), 0,
6951 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04006952 if (!btrfs_trans_handle_cachep)
6953 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02006954
6955 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
6956 sizeof(struct btrfs_transaction), 0,
6957 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04006958 if (!btrfs_transaction_cachep)
6959 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02006960
6961 btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
6962 sizeof(struct btrfs_path), 0,
6963 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04006964 if (!btrfs_path_cachep)
6965 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02006966
Josef Bacikdc89e982011-01-28 17:05:48 -05006967 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space_cache",
6968 sizeof(struct btrfs_free_space), 0,
6969 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
6970 if (!btrfs_free_space_cachep)
6971 goto fail;
6972
Chris Mason39279cc2007-06-12 06:35:45 -04006973 return 0;
6974fail:
6975 btrfs_destroy_cachep();
6976 return -ENOMEM;
6977}
6978
6979static int btrfs_getattr(struct vfsmount *mnt,
6980 struct dentry *dentry, struct kstat *stat)
6981{
6982 struct inode *inode = dentry->d_inode;
6983 generic_fillattr(inode, stat);
Chris Mason3394e162008-11-17 20:42:26 -05006984 stat->dev = BTRFS_I(inode)->root->anon_super.s_dev;
Chris Masond6667462008-01-03 14:51:00 -05006985 stat->blksize = PAGE_CACHE_SIZE;
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006986 stat->blocks = (inode_get_bytes(inode) +
6987 BTRFS_I(inode)->delalloc_bytes) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04006988 return 0;
6989}
6990
Liu Bo75e7cb72011-03-22 10:12:20 +00006991/*
6992 * If a file is moved, it will inherit the cow and compression flags of the new
6993 * directory.
6994 */
6995static void fixup_inode_flags(struct inode *dir, struct inode *inode)
6996{
6997 struct btrfs_inode *b_dir = BTRFS_I(dir);
6998 struct btrfs_inode *b_inode = BTRFS_I(inode);
6999
7000 if (b_dir->flags & BTRFS_INODE_NODATACOW)
7001 b_inode->flags |= BTRFS_INODE_NODATACOW;
7002 else
7003 b_inode->flags &= ~BTRFS_INODE_NODATACOW;
7004
7005 if (b_dir->flags & BTRFS_INODE_COMPRESS)
7006 b_inode->flags |= BTRFS_INODE_COMPRESS;
7007 else
7008 b_inode->flags &= ~BTRFS_INODE_COMPRESS;
7009}
7010
Chris Masond3977122009-01-05 21:25:51 -05007011static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7012 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04007013{
7014 struct btrfs_trans_handle *trans;
7015 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007016 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04007017 struct inode *new_inode = new_dentry->d_inode;
7018 struct inode *old_inode = old_dentry->d_inode;
7019 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007020 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007021 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04007022 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08007023 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007024
Li Zefan33345d012011-04-20 10:31:50 +08007025 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04007026 return -EPERM;
7027
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007028 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08007029 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05007030 return -EXDEV;
7031
Li Zefan33345d012011-04-20 10:31:50 +08007032 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
7033 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007034 return -ENOTEMPTY;
7035
Chris Mason39279cc2007-06-12 06:35:45 -04007036 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007037 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04007038 return -ENOTEMPTY;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007039 /*
7040 * we're using rename to replace one file with another.
7041 * and the replacement file is large. Start IO on it now so
7042 * we don't add too much work to the end of the transaction
7043 */
Bartlomiej Zolnierkiewicz4baf8c92009-08-07 13:47:08 -04007044 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
Chris Mason5a3f23d2009-03-31 13:27:11 -04007045 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
7046 filemap_flush(old_inode->i_mapping);
7047
Yan, Zheng76dda932009-09-21 16:00:26 -04007048 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08007049 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04007050 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04007051 /*
7052 * We want to reserve the absolute worst case amount of items. So if
7053 * both inodes are subvols and we need to unlink them then that would
7054 * require 4 item modifications, but if they are both normal inodes it
7055 * would require 5 item modifications, so we'll assume their normal
7056 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
7057 * should cover the worst case number of items we'll modify.
7058 */
7059 trans = btrfs_start_transaction(root, 20);
Johann Lombardib44c59a2011-03-31 13:23:47 +00007060 if (IS_ERR(trans)) {
7061 ret = PTR_ERR(trans);
7062 goto out_notrans;
7063 }
Chris Mason5f39d392007-10-15 16:14:19 -04007064
Yan, Zhenga5719522009-09-24 09:17:31 -04007065 btrfs_set_trans_block_group(trans, new_dir);
Chris Mason39279cc2007-06-12 06:35:45 -04007066
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007067 if (dest != root)
7068 btrfs_record_root_in_trans(trans, dest);
7069
Yan, Zhenga5719522009-09-24 09:17:31 -04007070 ret = btrfs_set_inode_index(new_dir, &index);
7071 if (ret)
7072 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007073
Li Zefan33345d012011-04-20 10:31:50 +08007074 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007075 /* force full log commit if subvolume involved. */
7076 root->fs_info->last_trans_log_full_commit = trans->transid;
7077 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04007078 ret = btrfs_insert_inode_ref(trans, dest,
7079 new_dentry->d_name.name,
7080 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08007081 old_ino,
7082 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04007083 if (ret)
7084 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007085 /*
7086 * this is an ugly little race, but the rename is required
7087 * to make sure that if we crash, the inode is either at the
7088 * old name or the new one. pinning the log transaction lets
7089 * us make sure we don't allow a log commit to come in after
7090 * we unlink the name but before we add the new name back in.
7091 */
7092 btrfs_pin_log_trans(root);
7093 }
Chris Mason12fcfd22009-03-24 10:24:20 -04007094 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04007095 * make sure the inode gets flushed if it is replacing
7096 * something.
7097 */
Li Zefan33345d012011-04-20 10:31:50 +08007098 if (new_inode && new_inode->i_size && S_ISREG(old_inode->i_mode))
Chris Mason5a3f23d2009-03-31 13:27:11 -04007099 btrfs_add_ordered_operation(trans, root, old_inode);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007100
Chris Mason39279cc2007-06-12 06:35:45 -04007101 old_dir->i_ctime = old_dir->i_mtime = ctime;
7102 new_dir->i_ctime = new_dir->i_mtime = ctime;
7103 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04007104
Chris Mason12fcfd22009-03-24 10:24:20 -04007105 if (old_dentry->d_parent != new_dentry->d_parent)
7106 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
7107
Li Zefan33345d012011-04-20 10:31:50 +08007108 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007109 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
7110 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
7111 old_dentry->d_name.name,
7112 old_dentry->d_name.len);
7113 } else {
Al Viro92986792011-03-04 17:14:37 +00007114 ret = __btrfs_unlink_inode(trans, root, old_dir,
7115 old_dentry->d_inode,
7116 old_dentry->d_name.name,
7117 old_dentry->d_name.len);
7118 if (!ret)
7119 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007120 }
7121 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04007122
7123 if (new_inode) {
7124 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08007125 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007126 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
7127 root_objectid = BTRFS_I(new_inode)->location.objectid;
7128 ret = btrfs_unlink_subvol(trans, dest, new_dir,
7129 root_objectid,
7130 new_dentry->d_name.name,
7131 new_dentry->d_name.len);
7132 BUG_ON(new_inode->i_nlink == 0);
7133 } else {
7134 ret = btrfs_unlink_inode(trans, dest, new_dir,
7135 new_dentry->d_inode,
7136 new_dentry->d_name.name,
7137 new_dentry->d_name.len);
7138 }
7139 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04007140 if (new_inode->i_nlink == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04007141 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007142 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04007143 }
Chris Mason39279cc2007-06-12 06:35:45 -04007144 }
Josef Bacikaec74772008-07-24 12:12:38 -04007145
Liu Bo75e7cb72011-03-22 10:12:20 +00007146 fixup_inode_flags(new_dir, old_inode);
7147
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007148 ret = btrfs_add_link(trans, new_dir, old_inode,
7149 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04007150 new_dentry->d_name.len, 0, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007151 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04007152
Li Zefan33345d012011-04-20 10:31:50 +08007153 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacik6a912212010-11-20 09:48:00 +00007154 struct dentry *parent = dget_parent(new_dentry);
7155 btrfs_log_new_name(trans, old_inode, old_dir, parent);
7156 dput(parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007157 btrfs_end_log_trans(root);
7158 }
Chris Mason39279cc2007-06-12 06:35:45 -04007159out_fail:
Chris Masonab78c842008-07-29 16:15:18 -04007160 btrfs_end_transaction_throttle(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00007161out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08007162 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04007163 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007164
Chris Mason39279cc2007-06-12 06:35:45 -04007165 return ret;
7166}
7167
Chris Masond352ac62008-09-29 15:18:18 -04007168/*
7169 * some fairly slow code that needs optimization. This walks the list
7170 * of all the inodes with pending delalloc and forces them to disk.
7171 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00007172int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
Chris Masonea8c2812008-08-04 23:17:27 -04007173{
7174 struct list_head *head = &root->fs_info->delalloc_inodes;
7175 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007176 struct inode *inode;
Chris Masonea8c2812008-08-04 23:17:27 -04007177
Yan Zhengc146afa2008-11-12 14:34:12 -05007178 if (root->fs_info->sb->s_flags & MS_RDONLY)
7179 return -EROFS;
7180
Chris Mason75eff682008-12-15 15:54:40 -05007181 spin_lock(&root->fs_info->delalloc_lock);
Chris Masond3977122009-01-05 21:25:51 -05007182 while (!list_empty(head)) {
Chris Masonea8c2812008-08-04 23:17:27 -04007183 binode = list_entry(head->next, struct btrfs_inode,
7184 delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007185 inode = igrab(&binode->vfs_inode);
7186 if (!inode)
7187 list_del_init(&binode->delalloc_inodes);
Chris Mason75eff682008-12-15 15:54:40 -05007188 spin_unlock(&root->fs_info->delalloc_lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007189 if (inode) {
Chris Mason8c8bee1d2008-09-29 11:19:10 -04007190 filemap_flush(inode->i_mapping);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00007191 if (delay_iput)
7192 btrfs_add_delayed_iput(inode);
7193 else
7194 iput(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007195 }
7196 cond_resched();
Chris Mason75eff682008-12-15 15:54:40 -05007197 spin_lock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04007198 }
Chris Mason75eff682008-12-15 15:54:40 -05007199 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason8c8bee1d2008-09-29 11:19:10 -04007200
7201 /* the filemap_flush will queue IO into the worker threads, but
7202 * we have to make sure the IO is actually started and that
7203 * ordered extents get created before we return
7204 */
7205 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05007206 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05007207 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee1d2008-09-29 11:19:10 -04007208 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05007209 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
7210 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee1d2008-09-29 11:19:10 -04007211 }
7212 atomic_dec(&root->fs_info->async_submit_draining);
Chris Masonea8c2812008-08-04 23:17:27 -04007213 return 0;
7214}
7215
Josef Bacik0019f102010-10-15 15:18:40 -04007216int btrfs_start_one_delalloc_inode(struct btrfs_root *root, int delay_iput,
7217 int sync)
Yan, Zheng5da9d012010-05-16 10:46:25 -04007218{
7219 struct btrfs_inode *binode;
7220 struct inode *inode = NULL;
7221
7222 spin_lock(&root->fs_info->delalloc_lock);
7223 while (!list_empty(&root->fs_info->delalloc_inodes)) {
7224 binode = list_entry(root->fs_info->delalloc_inodes.next,
7225 struct btrfs_inode, delalloc_inodes);
7226 inode = igrab(&binode->vfs_inode);
7227 if (inode) {
7228 list_move_tail(&binode->delalloc_inodes,
7229 &root->fs_info->delalloc_inodes);
7230 break;
7231 }
7232
7233 list_del_init(&binode->delalloc_inodes);
7234 cond_resched_lock(&root->fs_info->delalloc_lock);
7235 }
7236 spin_unlock(&root->fs_info->delalloc_lock);
7237
7238 if (inode) {
Josef Bacik0019f102010-10-15 15:18:40 -04007239 if (sync) {
7240 filemap_write_and_wait(inode->i_mapping);
7241 /*
7242 * We have to do this because compression doesn't
7243 * actually set PG_writeback until it submits the pages
7244 * for IO, which happens in an async thread, so we could
7245 * race and not actually wait for any writeback pages
7246 * because they've not been submitted yet. Technically
7247 * this could still be the case for the ordered stuff
7248 * since the async thread may not have started to do its
7249 * work yet. If this becomes the case then we need to
7250 * figure out a way to make sure that in writepage we
7251 * wait for any async pages to be submitted before
7252 * returning so that fdatawait does what its supposed to
7253 * do.
7254 */
7255 btrfs_wait_ordered_range(inode, 0, (u64)-1);
7256 } else {
7257 filemap_flush(inode->i_mapping);
7258 }
Yan, Zheng5da9d012010-05-16 10:46:25 -04007259 if (delay_iput)
7260 btrfs_add_delayed_iput(inode);
7261 else
7262 iput(inode);
7263 return 1;
7264 }
7265 return 0;
7266}
7267
Chris Mason39279cc2007-06-12 06:35:45 -04007268static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
7269 const char *symname)
7270{
7271 struct btrfs_trans_handle *trans;
7272 struct btrfs_root *root = BTRFS_I(dir)->root;
7273 struct btrfs_path *path;
7274 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05007275 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04007276 int err;
7277 int drop_inode = 0;
7278 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007279 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04007280 int name_len;
7281 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04007282 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04007283 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04007284 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05007285 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007286
7287 name_len = strlen(symname) + 1;
7288 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
7289 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05007290
Josef Bacik9ed74f22009-09-11 16:12:44 -04007291 /*
7292 * 2 items for inode item and ref
7293 * 2 items for dir items
7294 * 1 item for xattr if selinux is on
7295 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04007296 trans = btrfs_start_transaction(root, 5);
7297 if (IS_ERR(trans))
7298 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05007299
Chris Mason39279cc2007-06-12 06:35:45 -04007300 btrfs_set_trans_block_group(trans, dir);
7301
Li Zefan581bb052011-04-20 10:06:11 +08007302 err = btrfs_find_free_ino(root, &objectid);
7303 if (err)
7304 goto out_unlock;
7305
Josef Bacikaec74772008-07-24 12:12:38 -04007306 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08007307 dentry->d_name.len, btrfs_ino(dir), objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04007308 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO,
7309 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04007310 err = PTR_ERR(inode);
7311 if (IS_ERR(inode))
7312 goto out_unlock;
7313
Yan, Zhengf34f57a2009-11-12 09:35:27 +00007314 err = btrfs_init_inode_security(trans, inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04007315 if (err) {
7316 drop_inode = 1;
7317 goto out_unlock;
7318 }
7319
Chris Mason39279cc2007-06-12 06:35:45 -04007320 btrfs_set_trans_block_group(trans, inode);
Josef Bacika1b075d2010-11-19 20:36:11 +00007321 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04007322 if (err)
7323 drop_inode = 1;
7324 else {
7325 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04007326 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04007327 inode->i_fop = &btrfs_file_operations;
7328 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05007329 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04007330 }
Chris Mason39279cc2007-06-12 06:35:45 -04007331 btrfs_update_inode_block_group(trans, inode);
7332 btrfs_update_inode_block_group(trans, dir);
7333 if (drop_inode)
7334 goto out_unlock;
7335
7336 path = btrfs_alloc_path();
7337 BUG_ON(!path);
Li Zefan33345d012011-04-20 10:31:50 +08007338 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007339 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007340 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
7341 datasize = btrfs_file_extent_calc_inline_size(name_len);
7342 err = btrfs_insert_empty_item(trans, root, path, &key,
7343 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04007344 if (err) {
7345 drop_inode = 1;
7346 goto out_unlock;
7347 }
Chris Mason5f39d392007-10-15 16:14:19 -04007348 leaf = path->nodes[0];
7349 ei = btrfs_item_ptr(leaf, path->slots[0],
7350 struct btrfs_file_extent_item);
7351 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
7352 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04007353 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04007354 btrfs_set_file_extent_encryption(leaf, ei, 0);
7355 btrfs_set_file_extent_compression(leaf, ei, 0);
7356 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
7357 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
7358
Chris Mason39279cc2007-06-12 06:35:45 -04007359 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04007360 write_extent_buffer(leaf, symname, ptr, name_len);
7361 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04007362 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04007363
Chris Mason39279cc2007-06-12 06:35:45 -04007364 inode->i_op = &btrfs_symlink_inode_operations;
7365 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04007366 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04007367 inode_set_bytes(inode, name_len);
Chris Masondbe674a2008-07-17 12:54:05 -04007368 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04007369 err = btrfs_update_inode(trans, root, inode);
7370 if (err)
7371 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04007372
7373out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04007374 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04007375 btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04007376 if (drop_inode) {
7377 inode_dec_link_count(inode);
7378 iput(inode);
7379 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04007380 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04007381 return err;
7382}
Chris Mason16432982008-04-10 10:23:21 -04007383
Josef Bacik0af3d002010-06-21 14:48:16 -04007384static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
7385 u64 start, u64 num_bytes, u64 min_size,
7386 loff_t actual_len, u64 *alloc_hint,
7387 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04007388{
Yan Zhengd899e052008-10-30 14:25:28 -04007389 struct btrfs_root *root = BTRFS_I(inode)->root;
7390 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04007391 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00007392 u64 i_size;
Yan Zhengd899e052008-10-30 14:25:28 -04007393 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04007394 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04007395
Josef Bacik0af3d002010-06-21 14:48:16 -04007396 if (trans)
7397 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04007398 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04007399 if (own_trans) {
7400 trans = btrfs_start_transaction(root, 3);
7401 if (IS_ERR(trans)) {
7402 ret = PTR_ERR(trans);
7403 break;
7404 }
Yan Zhengd899e052008-10-30 14:25:28 -04007405 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00007406
Yan, Zhengefa56462010-05-16 10:49:59 -04007407 ret = btrfs_reserve_extent(trans, root, num_bytes, min_size,
7408 0, *alloc_hint, (u64)-1, &ins, 1);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007409 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04007410 if (own_trans)
7411 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04007412 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00007413 }
7414
Yan Zhengd899e052008-10-30 14:25:28 -04007415 ret = insert_reserved_file_extent(trans, inode,
7416 cur_offset, ins.objectid,
7417 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00007418 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04007419 BTRFS_FILE_EXTENT_PREALLOC);
7420 BUG_ON(ret);
Chris Masona1ed8352009-09-11 12:27:37 -04007421 btrfs_drop_extent_cache(inode, cur_offset,
7422 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007423
Yan Zhengd899e052008-10-30 14:25:28 -04007424 num_bytes -= ins.offset;
7425 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04007426 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00007427
Yan Zhengd899e052008-10-30 14:25:28 -04007428 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02007429 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04007430 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04007431 (actual_len > inode->i_size) &&
7432 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00007433 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00007434 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00007435 else
Josef Bacik55a61d12010-11-22 18:50:32 +00007436 i_size = cur_offset;
7437 i_size_write(inode, i_size);
7438 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007439 }
7440
Yan Zhengd899e052008-10-30 14:25:28 -04007441 ret = btrfs_update_inode(trans, root, inode);
7442 BUG_ON(ret);
Yan Zhengd899e052008-10-30 14:25:28 -04007443
Josef Bacik0af3d002010-06-21 14:48:16 -04007444 if (own_trans)
7445 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007446 }
Yan Zhengd899e052008-10-30 14:25:28 -04007447 return ret;
7448}
7449
Josef Bacik0af3d002010-06-21 14:48:16 -04007450int btrfs_prealloc_file_range(struct inode *inode, int mode,
7451 u64 start, u64 num_bytes, u64 min_size,
7452 loff_t actual_len, u64 *alloc_hint)
7453{
7454 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
7455 min_size, actual_len, alloc_hint,
7456 NULL);
7457}
7458
7459int btrfs_prealloc_file_range_trans(struct inode *inode,
7460 struct btrfs_trans_handle *trans, int mode,
7461 u64 start, u64 num_bytes, u64 min_size,
7462 loff_t actual_len, u64 *alloc_hint)
7463{
7464 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
7465 min_size, actual_len, alloc_hint, trans);
7466}
7467
Chris Masone6dcd2d2008-07-17 12:53:50 -04007468static int btrfs_set_page_dirty(struct page *page)
7469{
Chris Masone6dcd2d2008-07-17 12:53:50 -04007470 return __set_page_dirty_nobuffers(page);
7471}
7472
Nick Pigginb74c79e2011-01-07 17:49:58 +11007473static int btrfs_permission(struct inode *inode, int mask, unsigned int flags)
Yanfdebe2b2008-01-14 13:26:08 -05007474{
Li Zefanb83cc962010-12-20 16:04:08 +08007475 struct btrfs_root *root = BTRFS_I(inode)->root;
7476
7477 if (btrfs_root_readonly(root) && (mask & MAY_WRITE))
7478 return -EROFS;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02007479 if ((BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) && (mask & MAY_WRITE))
Yanfdebe2b2008-01-14 13:26:08 -05007480 return -EACCES;
Nick Pigginb74c79e2011-01-07 17:49:58 +11007481 return generic_permission(inode, mask, flags, btrfs_check_acl);
Yanfdebe2b2008-01-14 13:26:08 -05007482}
Chris Mason39279cc2007-06-12 06:35:45 -04007483
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007484static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05007485 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04007486 .lookup = btrfs_lookup,
7487 .create = btrfs_create,
7488 .unlink = btrfs_unlink,
7489 .link = btrfs_link,
7490 .mkdir = btrfs_mkdir,
7491 .rmdir = btrfs_rmdir,
7492 .rename = btrfs_rename,
7493 .symlink = btrfs_symlink,
7494 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04007495 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007496 .setxattr = btrfs_setxattr,
7497 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05007498 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007499 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05007500 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04007501};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007502static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007503 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05007504 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04007505};
Yan, Zheng76dda932009-09-21 16:00:26 -04007506
Alexey Dobriyan828c0952009-10-01 15:43:56 -07007507static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007508 .llseek = generic_file_llseek,
7509 .read = generic_read_dir,
David Woodhousecbdf5a22008-08-06 19:42:33 +01007510 .readdir = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04007511 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04007512#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04007513 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04007514#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04007515 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04007516 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04007517};
7518
Chris Masond1310b22008-01-24 16:13:08 -05007519static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04007520 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05007521 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04007522 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04007523 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04007524 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04007525 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Mason1259ab72008-05-12 13:39:03 -04007526 .readpage_io_failed_hook = btrfs_io_failed_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05007527 .set_bit_hook = btrfs_set_bit_hook,
7528 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04007529 .merge_extent_hook = btrfs_merge_extent_hook,
7530 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04007531};
7532
Chris Mason35054392009-01-21 13:11:13 -05007533/*
7534 * btrfs doesn't support the bmap operation because swapfiles
7535 * use bmap to make a mapping of extents in the file. They assume
7536 * these extents won't change over the life of the file and they
7537 * use the bmap result to do IO directly to the drive.
7538 *
7539 * the btrfs bmap call would return logical addresses that aren't
7540 * suitable for IO and they also will change frequently as COW
7541 * operations happen. So, swapfile + btrfs == corruption.
7542 *
7543 * For now we're avoiding this by dropping bmap.
7544 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07007545static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04007546 .readpage = btrfs_readpage,
7547 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04007548 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05007549 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04007550 .sync_page = block_sync_page,
Chris Mason16432982008-04-10 10:23:21 -04007551 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04007552 .invalidatepage = btrfs_invalidatepage,
7553 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04007554 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02007555 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04007556};
7557
Alexey Dobriyan7f094102009-09-21 17:01:10 -07007558static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04007559 .readpage = btrfs_readpage,
7560 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04007561 .invalidatepage = btrfs_invalidatepage,
7562 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04007563};
7564
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007565static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007566 .getattr = btrfs_getattr,
7567 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007568 .setxattr = btrfs_setxattr,
7569 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05007570 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007571 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05007572 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007573 .fiemap = btrfs_fiemap,
Chris Mason39279cc2007-06-12 06:35:45 -04007574};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007575static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04007576 .getattr = btrfs_getattr,
7577 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05007578 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007579 .setxattr = btrfs_setxattr,
7580 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04007581 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007582 .removexattr = btrfs_removexattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04007583};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007584static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007585 .readlink = generic_readlink,
7586 .follow_link = page_follow_link_light,
7587 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00007588 .getattr = btrfs_getattr,
Yanfdebe2b2008-01-14 13:26:08 -05007589 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05007590 .setxattr = btrfs_setxattr,
7591 .getxattr = btrfs_getxattr,
7592 .listxattr = btrfs_listxattr,
7593 .removexattr = btrfs_removexattr,
Chris Mason39279cc2007-06-12 06:35:45 -04007594};
Yan, Zheng76dda932009-09-21 16:00:26 -04007595
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04007596const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04007597 .d_delete = btrfs_dentry_delete,
7598};