blob: 9e51325828bc2a23b2767d716fba346b9a3b0f53 [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>
David Sterba7a36dde2011-05-06 15:33:15 +020040#include <linux/ratelimit.h>
Josef Bacik22c44fe2011-11-30 10:45:38 -050041#include <linux/mount.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050042#include "compat.h"
Chris Mason39279cc2007-06-12 06:35:45 -040043#include "ctree.h"
44#include "disk-io.h"
45#include "transaction.h"
46#include "btrfs_inode.h"
47#include "ioctl.h"
48#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040049#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040050#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040051#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020052#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040053#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050054#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050055#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080056#include "inode-map.h"
Chris Mason39279cc2007-06-12 06:35:45 -040057
58struct btrfs_iget_args {
59 u64 ino;
60 struct btrfs_root *root;
61};
62
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070063static const struct inode_operations btrfs_dir_inode_operations;
64static const struct inode_operations btrfs_symlink_inode_operations;
65static const struct inode_operations btrfs_dir_ro_inode_operations;
66static const struct inode_operations btrfs_special_inode_operations;
67static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070068static const struct address_space_operations btrfs_aops;
69static const struct address_space_operations btrfs_symlink_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070070static const struct file_operations btrfs_dir_file_operations;
Chris Masond1310b22008-01-24 16:13:08 -050071static struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040072
73static struct kmem_cache *btrfs_inode_cachep;
74struct kmem_cache *btrfs_trans_handle_cachep;
75struct kmem_cache *btrfs_transaction_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040076struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050077struct kmem_cache *btrfs_free_space_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040078
79#define S_SHIFT 12
80static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
81 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
82 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
83 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
84 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
85 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
86 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
87 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
88};
89
Josef Bacika41ad392011-01-31 15:30:16 -050090static int btrfs_setsize(struct inode *inode, loff_t newsize);
91static int btrfs_truncate(struct inode *inode);
Chris Masonc8b97812008-10-29 14:49:59 -040092static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end);
Chris Mason771ed682008-11-06 22:02:51 -050093static noinline int cow_file_range(struct inode *inode,
94 struct page *locked_page,
95 u64 start, u64 end, int *page_started,
96 unsigned long *nr_written, int unlock);
Chris Mason21151332011-11-10 20:39:08 -050097static noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
98 struct btrfs_root *root, struct inode *inode);
Josef Bacik7b128762008-07-24 12:17:14 -040099
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000100static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500101 struct inode *inode, struct inode *dir,
102 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500103{
104 int err;
105
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000106 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500107 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500108 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500109 return err;
110}
111
Chris Masond352ac62008-09-29 15:18:18 -0400112/*
Chris Masonc8b97812008-10-29 14:49:59 -0400113 * this does all the hard work for inserting an inline extent into
114 * the btree. The caller should have done a btrfs_drop_extents so that
115 * no overlapping inline items exist in the btree
116 */
Chris Masond3977122009-01-05 21:25:51 -0500117static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400118 struct btrfs_root *root, struct inode *inode,
119 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000120 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400121 struct page **compressed_pages)
122{
123 struct btrfs_key key;
124 struct btrfs_path *path;
125 struct extent_buffer *leaf;
126 struct page *page = NULL;
127 char *kaddr;
128 unsigned long ptr;
129 struct btrfs_file_extent_item *ei;
130 int err = 0;
131 int ret;
132 size_t cur_size = size;
133 size_t datasize;
134 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400135
Li Zefanfe3f5662011-03-28 08:30:38 +0000136 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400137 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400138
Chris Masond3977122009-01-05 21:25:51 -0500139 path = btrfs_alloc_path();
140 if (!path)
Chris Masonc8b97812008-10-29 14:49:59 -0400141 return -ENOMEM;
142
Chris Masonb9473432009-03-13 11:00:37 -0400143 path->leave_spinning = 1;
Chris Masonc8b97812008-10-29 14:49:59 -0400144
Li Zefan33345d012011-04-20 10:31:50 +0800145 key.objectid = btrfs_ino(inode);
Chris Masonc8b97812008-10-29 14:49:59 -0400146 key.offset = start;
147 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
Chris Masonc8b97812008-10-29 14:49:59 -0400148 datasize = btrfs_file_extent_calc_inline_size(cur_size);
149
150 inode_add_bytes(inode, size);
151 ret = btrfs_insert_empty_item(trans, root, path, &key,
152 datasize);
Chris Masonc8b97812008-10-29 14:49:59 -0400153 if (ret) {
154 err = ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400155 goto fail;
156 }
157 leaf = path->nodes[0];
158 ei = btrfs_item_ptr(leaf, path->slots[0],
159 struct btrfs_file_extent_item);
160 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
161 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
162 btrfs_set_file_extent_encryption(leaf, ei, 0);
163 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
164 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
165 ptr = btrfs_file_extent_inline_start(ei);
166
Li Zefan261507a02010-12-17 14:21:50 +0800167 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400168 struct page *cpage;
169 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500170 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400171 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500172 cur_size = min_t(unsigned long, compressed_size,
Chris Masonc8b97812008-10-29 14:49:59 -0400173 PAGE_CACHE_SIZE);
174
Cong Wang7ac687d2011-11-25 23:14:28 +0800175 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400176 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800177 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400178
179 i++;
180 ptr += cur_size;
181 compressed_size -= cur_size;
182 }
183 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800184 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400185 } else {
186 page = find_get_page(inode->i_mapping,
187 start >> PAGE_CACHE_SHIFT);
188 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800189 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400190 offset = start & (PAGE_CACHE_SIZE - 1);
191 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800192 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400193 page_cache_release(page);
194 }
195 btrfs_mark_buffer_dirty(leaf);
196 btrfs_free_path(path);
197
Yan, Zhengc2167752009-11-12 09:34:21 +0000198 /*
199 * we're an inline extent, so nobody can
200 * extend the file past i_size without locking
201 * a page we already have locked.
202 *
203 * We must do any isize and inode updates
204 * before we unlock the pages. Otherwise we
205 * could end up racing with unlink.
206 */
Chris Masonc8b97812008-10-29 14:49:59 -0400207 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100208 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000209
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100210 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400211fail:
212 btrfs_free_path(path);
213 return err;
214}
215
216
217/*
218 * conditionally insert an inline extent into the file. This
219 * does the checks required to make sure the data is small enough
220 * to fit as an inline extent.
221 */
Chris Mason7f366cf2009-03-12 20:12:45 -0400222static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400223 struct btrfs_root *root,
224 struct inode *inode, u64 start, u64 end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000225 size_t compressed_size, int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400226 struct page **compressed_pages)
227{
228 u64 isize = i_size_read(inode);
229 u64 actual_end = min(end + 1, isize);
230 u64 inline_len = actual_end - start;
231 u64 aligned_end = (end + root->sectorsize - 1) &
232 ~((u64)root->sectorsize - 1);
233 u64 hint_byte;
234 u64 data_len = inline_len;
235 int ret;
236
237 if (compressed_size)
238 data_len = compressed_size;
239
240 if (start > 0 ||
Chris Mason70b99e62008-10-31 12:46:39 -0400241 actual_end >= PAGE_CACHE_SIZE ||
Chris Masonc8b97812008-10-29 14:49:59 -0400242 data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
243 (!compressed_size &&
244 (actual_end & (root->sectorsize - 1)) == 0) ||
245 end + 1 < isize ||
246 data_len > root->fs_info->max_inline) {
247 return 1;
248 }
249
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000250 ret = btrfs_drop_extents(trans, inode, start, aligned_end,
Chris Masona1ed8352009-09-11 12:27:37 -0400251 &hint_byte, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100252 if (ret)
253 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400254
255 if (isize > actual_end)
256 inline_len = min_t(u64, isize, actual_end);
257 ret = insert_inline_extent(trans, root, inode, start,
258 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000259 compress_type, compressed_pages);
Josef Bacik4e050df2012-05-23 16:10:14 -0400260 if (ret && ret != -ENOSPC) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100261 btrfs_abort_transaction(trans, root, ret);
262 return ret;
Josef Bacik4e050df2012-05-23 16:10:14 -0400263 } else if (ret == -ENOSPC) {
264 return 1;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100265 }
Josef Bacik4e050df2012-05-23 16:10:14 -0400266
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400267 btrfs_delalloc_release_metadata(inode, end + 1 - start);
Chris Masona1ed8352009-09-11 12:27:37 -0400268 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
Chris Masonc8b97812008-10-29 14:49:59 -0400269 return 0;
270}
271
Chris Mason771ed682008-11-06 22:02:51 -0500272struct async_extent {
273 u64 start;
274 u64 ram_size;
275 u64 compressed_size;
276 struct page **pages;
277 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800278 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500279 struct list_head list;
280};
281
282struct async_cow {
283 struct inode *inode;
284 struct btrfs_root *root;
285 struct page *locked_page;
286 u64 start;
287 u64 end;
288 struct list_head extents;
289 struct btrfs_work work;
290};
291
292static noinline int add_async_extent(struct async_cow *cow,
293 u64 start, u64 ram_size,
294 u64 compressed_size,
295 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800296 unsigned long nr_pages,
297 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500298{
299 struct async_extent *async_extent;
300
301 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100302 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500303 async_extent->start = start;
304 async_extent->ram_size = ram_size;
305 async_extent->compressed_size = compressed_size;
306 async_extent->pages = pages;
307 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800308 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500309 list_add_tail(&async_extent->list, &cow->extents);
310 return 0;
311}
312
Chris Masonc8b97812008-10-29 14:49:59 -0400313/*
Chris Mason771ed682008-11-06 22:02:51 -0500314 * we create compressed extents in two phases. The first
315 * phase compresses a range of pages that have already been
316 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400317 *
Chris Mason771ed682008-11-06 22:02:51 -0500318 * This is done inside an ordered work queue, and the compression
319 * is spread across many cpus. The actual IO submission is step
320 * two, and the ordered work queue takes care of making sure that
321 * happens in the same order things were put onto the queue by
322 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400323 *
Chris Mason771ed682008-11-06 22:02:51 -0500324 * If this code finds it can't get good compression, it puts an
325 * entry onto the work queue to write the uncompressed bytes. This
326 * makes sure that both compressed inodes and uncompressed inodes
327 * are written in the same order that pdflush sent them down.
Chris Masond352ac62008-09-29 15:18:18 -0400328 */
Chris Mason771ed682008-11-06 22:02:51 -0500329static noinline int compress_file_range(struct inode *inode,
330 struct page *locked_page,
331 u64 start, u64 end,
332 struct async_cow *async_cow,
333 int *num_added)
Chris Masonb888db22007-08-27 16:49:44 -0400334{
335 struct btrfs_root *root = BTRFS_I(inode)->root;
336 struct btrfs_trans_handle *trans;
Chris Masondb945352007-10-15 16:15:53 -0400337 u64 num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400338 u64 blocksize = root->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400339 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500340 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400341 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400342 struct page **pages = NULL;
343 unsigned long nr_pages;
344 unsigned long nr_pages_ret = 0;
345 unsigned long total_compressed = 0;
346 unsigned long total_in = 0;
347 unsigned long max_compressed = 128 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -0500348 unsigned long max_uncompressed = 128 * 1024;
Chris Masonc8b97812008-10-29 14:49:59 -0400349 int i;
350 int will_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800351 int compress_type = root->fs_info->compress_type;
Chris Mason84519c02013-03-26 13:07:00 -0400352 int redirty = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400353
Liu Bo4cb13e52012-03-29 09:57:45 -0400354 /* if this is a small write inside eof, kick off a defrag */
355 if ((end - start + 1) < 16 * 1024 &&
356 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400357 btrfs_add_inode_defrag(NULL, inode);
358
Chris Mason42dc7ba2008-12-15 11:44:56 -0500359 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400360again:
361 will_compress = 0;
362 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
363 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
364
Chris Masonf03d9302009-02-04 09:31:06 -0500365 /*
366 * we don't want to send crud past the end of i_size through
367 * compression, that's just a waste of CPU time. So, if the
368 * end of the file is before the start of our current
369 * requested range of bytes, we bail out to the uncompressed
370 * cleanup code that can deal with all of this.
371 *
372 * It isn't really the fastest way to fix things, but this is a
373 * very uncommon corner.
374 */
375 if (actual_end <= start)
376 goto cleanup_and_bail_uncompressed;
377
Chris Masonc8b97812008-10-29 14:49:59 -0400378 total_compressed = actual_end - start;
379
380 /* we want to make sure that amount of ram required to uncompress
381 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500382 * of a compressed extent to 128k. This is a crucial number
383 * because it also controls how easily we can spread reads across
384 * cpus for decompression.
385 *
386 * We also want to make sure the amount of IO required to do
387 * a random read is reasonably small, so we limit the size of
388 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400389 */
390 total_compressed = min(total_compressed, max_uncompressed);
Chris Masondb945352007-10-15 16:15:53 -0400391 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
Chris Masonbe20aa92007-12-17 20:14:01 -0500392 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400393 total_in = 0;
394 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400395
Chris Mason771ed682008-11-06 22:02:51 -0500396 /*
397 * we do compression for mount -o compress and when the
398 * inode has not been flagged as nocompress. This flag can
399 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400400 */
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200401 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) &&
Chris Mason1e701a32010-03-11 09:42:04 -0500402 (btrfs_test_opt(root, COMPRESS) ||
Liu Bo75e7cb72011-03-22 10:12:20 +0000403 (BTRFS_I(inode)->force_compress) ||
404 (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS))) {
Chris Masonc8b97812008-10-29 14:49:59 -0400405 WARN_ON(pages);
Chris Masoncfbc2462008-10-30 13:22:14 -0400406 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800407 if (!pages) {
408 /* just bail out to the uncompressed code */
409 goto cont;
410 }
Chris Mason179e29e2007-11-01 11:28:41 -0400411
Li Zefan261507a02010-12-17 14:21:50 +0800412 if (BTRFS_I(inode)->force_compress)
413 compress_type = BTRFS_I(inode)->force_compress;
414
Chris Mason84519c02013-03-26 13:07:00 -0400415 /*
416 * we need to call clear_page_dirty_for_io on each
417 * page in the range. Otherwise applications with the file
418 * mmap'd can wander in and change the page contents while
419 * we are compressing them.
420 *
421 * If the compression fails for any reason, we set the pages
422 * dirty again later on.
423 */
424 extent_range_clear_dirty_for_io(inode, start, end);
425 redirty = 1;
Li Zefan261507a02010-12-17 14:21:50 +0800426 ret = btrfs_compress_pages(compress_type,
427 inode->i_mapping, start,
428 total_compressed, pages,
429 nr_pages, &nr_pages_ret,
430 &total_in,
431 &total_compressed,
432 max_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400433
434 if (!ret) {
435 unsigned long offset = total_compressed &
436 (PAGE_CACHE_SIZE - 1);
437 struct page *page = pages[nr_pages_ret - 1];
438 char *kaddr;
439
440 /* zero the tail end of the last page, we might be
441 * sending it down to disk
442 */
443 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800444 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400445 memset(kaddr + offset, 0,
446 PAGE_CACHE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800447 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400448 }
449 will_compress = 1;
450 }
451 }
Li Zefan560f7d72011-09-08 10:22:01 +0800452cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400453 if (start == 0) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -0400454 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100455 if (IS_ERR(trans)) {
456 ret = PTR_ERR(trans);
457 trans = NULL;
458 goto cleanup_and_out;
459 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400460 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Mason771ed682008-11-06 22:02:51 -0500461
Chris Masonc8b97812008-10-29 14:49:59 -0400462 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500463 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400464 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500465 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400466 */
467 ret = cow_file_range_inline(trans, root, inode,
Li Zefanfe3f5662011-03-28 08:30:38 +0000468 start, end, 0, 0, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400469 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500470 /* try making a compressed inline extent */
Chris Masonc8b97812008-10-29 14:49:59 -0400471 ret = cow_file_range_inline(trans, root, inode,
472 start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000473 total_compressed,
474 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400475 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100476 if (ret <= 0) {
Chris Mason771ed682008-11-06 22:02:51 -0500477 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100478 * inline extent creation worked or returned error,
479 * we don't need to create any more async work items.
480 * Unlock and free up our temp pages.
Chris Mason771ed682008-11-06 22:02:51 -0500481 */
Chris Masonc8b97812008-10-29 14:49:59 -0400482 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400483 &BTRFS_I(inode)->io_tree,
484 start, end, NULL,
485 EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
Chris Masona3429ab2009-10-08 12:30:20 -0400486 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400487 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000488
489 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400490 goto free_pages_out;
491 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000492 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400493 }
494
495 if (will_compress) {
496 /*
497 * we aren't doing an inline extent round the compressed size
498 * up to a block size boundary so the allocator does sane
499 * things
500 */
501 total_compressed = (total_compressed + blocksize - 1) &
502 ~(blocksize - 1);
503
504 /*
505 * one last check to make sure the compression is really a
506 * win, compare the page count read with the blocks on disk
507 */
508 total_in = (total_in + PAGE_CACHE_SIZE - 1) &
509 ~(PAGE_CACHE_SIZE - 1);
510 if (total_compressed >= total_in) {
511 will_compress = 0;
512 } else {
Chris Masonc8b97812008-10-29 14:49:59 -0400513 num_bytes = total_in;
514 }
515 }
516 if (!will_compress && pages) {
517 /*
518 * the compression code ran but failed to make things smaller,
519 * free any pages it allocated and our page pointer array
520 */
521 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400522 WARN_ON(pages[i]->mapping);
Chris Masonc8b97812008-10-29 14:49:59 -0400523 page_cache_release(pages[i]);
524 }
525 kfree(pages);
526 pages = NULL;
527 total_compressed = 0;
528 nr_pages_ret = 0;
529
530 /* flag the file so we don't compress in the future */
Chris Mason1e701a32010-03-11 09:42:04 -0500531 if (!btrfs_test_opt(root, FORCE_COMPRESS) &&
532 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500533 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500534 }
Chris Masonc8b97812008-10-29 14:49:59 -0400535 }
Chris Mason771ed682008-11-06 22:02:51 -0500536 if (will_compress) {
537 *num_added += 1;
538
539 /* the async work queues will take care of doing actual
540 * allocation on disk for these compressed pages,
541 * and will submit them to the elevator.
542 */
543 add_async_extent(async_cow, start, num_bytes,
Li Zefan261507a02010-12-17 14:21:50 +0800544 total_compressed, pages, nr_pages_ret,
545 compress_type);
Chris Mason771ed682008-11-06 22:02:51 -0500546
Yan, Zheng24ae6362010-12-06 07:02:36 +0000547 if (start + num_bytes < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500548 start += num_bytes;
549 pages = NULL;
550 cond_resched();
551 goto again;
552 }
553 } else {
Chris Masonf03d9302009-02-04 09:31:06 -0500554cleanup_and_bail_uncompressed:
Chris Mason771ed682008-11-06 22:02:51 -0500555 /*
556 * No compression, but we still need to write the pages in
557 * the file we've been given so far. redirty the locked
558 * page if it corresponds to our extent and set things up
559 * for the async work queue to run cow_file_range to do
560 * the normal delalloc dance
561 */
562 if (page_offset(locked_page) >= start &&
563 page_offset(locked_page) <= end) {
564 __set_page_dirty_nobuffers(locked_page);
565 /* unlocked later on in the async handlers */
566 }
Chris Mason84519c02013-03-26 13:07:00 -0400567 if (redirty)
568 extent_range_redirty_for_io(inode, start, end);
Li Zefan261507a02010-12-17 14:21:50 +0800569 add_async_extent(async_cow, start, end - start + 1,
570 0, NULL, 0, BTRFS_COMPRESS_NONE);
Chris Mason771ed682008-11-06 22:02:51 -0500571 *num_added += 1;
572 }
573
574out:
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100575 return ret;
Chris Mason771ed682008-11-06 22:02:51 -0500576
577free_pages_out:
578 for (i = 0; i < nr_pages_ret; i++) {
579 WARN_ON(pages[i]->mapping);
580 page_cache_release(pages[i]);
581 }
Chris Masond3977122009-01-05 21:25:51 -0500582 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500583
584 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100585
586cleanup_and_out:
587 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
588 start, end, NULL,
589 EXTENT_CLEAR_UNLOCK_PAGE |
590 EXTENT_CLEAR_DIRTY |
591 EXTENT_CLEAR_DELALLOC |
592 EXTENT_SET_WRITEBACK |
593 EXTENT_END_WRITEBACK);
594 if (!trans || IS_ERR(trans))
595 btrfs_error(root->fs_info, ret, "Failed to join transaction");
596 else
597 btrfs_abort_transaction(trans, root, ret);
598 goto free_pages_out;
Chris Mason771ed682008-11-06 22:02:51 -0500599}
600
601/*
602 * phase two of compressed writeback. This is the ordered portion
603 * of the code, which only gets called in the order the work was
604 * queued. We walk all the async extents created by compress_file_range
605 * and send them down to the disk.
606 */
607static noinline int submit_compressed_extents(struct inode *inode,
608 struct async_cow *async_cow)
609{
610 struct async_extent *async_extent;
611 u64 alloc_hint = 0;
612 struct btrfs_trans_handle *trans;
613 struct btrfs_key ins;
614 struct extent_map *em;
615 struct btrfs_root *root = BTRFS_I(inode)->root;
616 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
617 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500618 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500619
620 if (list_empty(&async_cow->extents))
621 return 0;
622
Chris Mason771ed682008-11-06 22:02:51 -0500623
Chris Masond3977122009-01-05 21:25:51 -0500624 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500625 async_extent = list_entry(async_cow->extents.next,
626 struct async_extent, list);
627 list_del(&async_extent->list);
628
629 io_tree = &BTRFS_I(inode)->io_tree;
630
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500631retry:
Chris Mason771ed682008-11-06 22:02:51 -0500632 /* did the compression code fall back to uncompressed IO? */
633 if (!async_extent->pages) {
634 int page_started = 0;
635 unsigned long nr_written = 0;
636
637 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000638 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100639 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500640
641 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500642 ret = cow_file_range(inode, async_cow->locked_page,
643 async_extent->start,
644 async_extent->start +
645 async_extent->ram_size - 1,
646 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500647
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100648 /* JDM XXX */
649
Chris Mason771ed682008-11-06 22:02:51 -0500650 /*
651 * if page_started, cow_file_range inserted an
652 * inline extent and took care of all the unlocking
653 * and IO for us. Otherwise, we need to submit
654 * all those pages down to the drive.
655 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500656 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500657 extent_write_locked_range(io_tree,
658 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500659 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500660 async_extent->ram_size - 1,
661 btrfs_get_extent,
662 WB_SYNC_ALL);
663 kfree(async_extent);
664 cond_resched();
665 continue;
666 }
667
668 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100669 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500670
Josef Bacik7a7eaa42011-04-13 12:54:33 -0400671 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100672 if (IS_ERR(trans)) {
673 ret = PTR_ERR(trans);
674 } else {
675 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
676 ret = btrfs_reserve_extent(trans, root,
Chris Mason771ed682008-11-06 22:02:51 -0500677 async_extent->compressed_size,
678 async_extent->compressed_size,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500679 0, alloc_hint, &ins, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100680 if (ret)
681 btrfs_abort_transaction(trans, root, ret);
682 btrfs_end_transaction(trans, root);
683 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000684
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500685 if (ret) {
686 int i;
687 for (i = 0; i < async_extent->nr_pages; i++) {
688 WARN_ON(async_extent->pages[i]->mapping);
689 page_cache_release(async_extent->pages[i]);
690 }
691 kfree(async_extent->pages);
692 async_extent->nr_pages = 0;
693 async_extent->pages = NULL;
694 unlock_extent(io_tree, async_extent->start,
695 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100696 async_extent->ram_size - 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100697 if (ret == -ENOSPC)
698 goto retry;
699 goto out_free; /* JDM: Requeue? */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500700 }
701
Yan, Zhengc2167752009-11-12 09:34:21 +0000702 /*
703 * here we're doing allocation and writeback of the
704 * compressed pages
705 */
706 btrfs_drop_extent_cache(inode, async_extent->start,
707 async_extent->start +
708 async_extent->ram_size - 1, 0);
709
David Sterba172ddd62011-04-21 00:48:27 +0200710 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100711 BUG_ON(!em); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500712 em->start = async_extent->start;
713 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500714 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500715
716 em->block_start = ins.objectid;
717 em->block_len = ins.offset;
718 em->bdev = root->fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800719 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500720 set_bit(EXTENT_FLAG_PINNED, &em->flags);
721 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
722
Chris Masond3977122009-01-05 21:25:51 -0500723 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400724 write_lock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500725 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -0400726 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500727 if (ret != -EEXIST) {
728 free_extent_map(em);
729 break;
730 }
731 btrfs_drop_extent_cache(inode, async_extent->start,
732 async_extent->start +
733 async_extent->ram_size - 1, 0);
734 }
735
Li Zefan261507a02010-12-17 14:21:50 +0800736 ret = btrfs_add_ordered_extent_compress(inode,
737 async_extent->start,
738 ins.objectid,
739 async_extent->ram_size,
740 ins.offset,
741 BTRFS_ORDERED_COMPRESSED,
742 async_extent->compress_type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100743 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500744
Chris Mason771ed682008-11-06 22:02:51 -0500745 /*
746 * clear dirty, set writeback and unlock the pages.
747 */
748 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400749 &BTRFS_I(inode)->io_tree,
750 async_extent->start,
751 async_extent->start +
752 async_extent->ram_size - 1,
753 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
754 EXTENT_CLEAR_UNLOCK |
Chris Masona3429ab2009-10-08 12:30:20 -0400755 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400756 EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500757
758 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500759 async_extent->start,
760 async_extent->ram_size,
761 ins.objectid,
762 ins.offset, async_extent->pages,
763 async_extent->nr_pages);
Chris Mason771ed682008-11-06 22:02:51 -0500764
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100765 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500766 alloc_hint = ins.objectid + ins.offset;
767 kfree(async_extent);
768 cond_resched();
769 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100770 ret = 0;
771out:
772 return ret;
773out_free:
774 kfree(async_extent);
775 goto out;
Chris Mason771ed682008-11-06 22:02:51 -0500776}
777
Josef Bacik4b46fce2010-05-23 11:00:55 -0400778static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
779 u64 num_bytes)
780{
781 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
782 struct extent_map *em;
783 u64 alloc_hint = 0;
784
785 read_lock(&em_tree->lock);
786 em = search_extent_mapping(em_tree, start, num_bytes);
787 if (em) {
788 /*
789 * if block start isn't an actual block number then find the
790 * first block in this inode and use that as a hint. If that
791 * block is also bogus then just don't worry about it.
792 */
793 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
794 free_extent_map(em);
795 em = search_extent_mapping(em_tree, 0, 0);
796 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
797 alloc_hint = em->block_start;
798 if (em)
799 free_extent_map(em);
800 } else {
801 alloc_hint = em->block_start;
802 free_extent_map(em);
803 }
804 }
805 read_unlock(&em_tree->lock);
806
807 return alloc_hint;
808}
809
Chris Mason771ed682008-11-06 22:02:51 -0500810/*
811 * when extent_io.c finds a delayed allocation range in the file,
812 * the call backs end up in this code. The basic idea is to
813 * allocate extents on disk for the range, and create ordered data structs
814 * in ram to track those extents.
815 *
816 * locked_page is the page that writepage had locked already. We use
817 * it to make sure we don't do extra locks or unlocks.
818 *
819 * *page_started is set to one if we unlock locked_page and do everything
820 * required to start IO on it. It may be clean and already done with
821 * IO when we return.
822 */
823static noinline int cow_file_range(struct inode *inode,
824 struct page *locked_page,
825 u64 start, u64 end, int *page_started,
826 unsigned long *nr_written,
827 int unlock)
828{
829 struct btrfs_root *root = BTRFS_I(inode)->root;
830 struct btrfs_trans_handle *trans;
831 u64 alloc_hint = 0;
832 u64 num_bytes;
833 unsigned long ram_size;
834 u64 disk_num_bytes;
835 u64 cur_alloc_size;
836 u64 blocksize = root->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500837 struct btrfs_key ins;
838 struct extent_map *em;
839 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
840 int ret = 0;
841
Chris Mason2cf85722011-07-26 15:35:09 -0400842 BUG_ON(btrfs_is_free_space_inode(root, inode));
Josef Bacik7a7eaa42011-04-13 12:54:33 -0400843 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100844 if (IS_ERR(trans)) {
845 extent_clear_unlock_delalloc(inode,
846 &BTRFS_I(inode)->io_tree,
847 start, end, NULL,
848 EXTENT_CLEAR_UNLOCK_PAGE |
849 EXTENT_CLEAR_UNLOCK |
850 EXTENT_CLEAR_DELALLOC |
851 EXTENT_CLEAR_DIRTY |
852 EXTENT_SET_WRITEBACK |
853 EXTENT_END_WRITEBACK);
854 return PTR_ERR(trans);
855 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400856 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Mason771ed682008-11-06 22:02:51 -0500857
Chris Mason771ed682008-11-06 22:02:51 -0500858 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
859 num_bytes = max(blocksize, num_bytes);
860 disk_num_bytes = num_bytes;
861 ret = 0;
862
Chris Mason4cb53002011-05-24 15:35:30 -0400863 /* if this is a small write inside eof, kick off defrag */
Liu Bo4cb13e52012-03-29 09:57:45 -0400864 if (num_bytes < 64 * 1024 &&
865 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400866 btrfs_add_inode_defrag(trans, inode);
867
Chris Mason771ed682008-11-06 22:02:51 -0500868 if (start == 0) {
869 /* lets try to make an inline extent */
870 ret = cow_file_range_inline(trans, root, inode,
Li Zefanfe3f5662011-03-28 08:30:38 +0000871 start, end, 0, 0, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500872 if (ret == 0) {
873 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400874 &BTRFS_I(inode)->io_tree,
875 start, end, NULL,
876 EXTENT_CLEAR_UNLOCK_PAGE |
877 EXTENT_CLEAR_UNLOCK |
878 EXTENT_CLEAR_DELALLOC |
879 EXTENT_CLEAR_DIRTY |
880 EXTENT_SET_WRITEBACK |
881 EXTENT_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000882
Chris Mason771ed682008-11-06 22:02:51 -0500883 *nr_written = *nr_written +
884 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
885 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500886 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100887 } else if (ret < 0) {
888 btrfs_abort_transaction(trans, root, ret);
889 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500890 }
891 }
Chris Masonc8b97812008-10-29 14:49:59 -0400892
893 BUG_ON(disk_num_bytes >
David Sterba6c417612011-04-13 15:41:04 +0200894 btrfs_super_total_bytes(root->fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400895
Josef Bacik4b46fce2010-05-23 11:00:55 -0400896 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400897 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400898
Chris Masond3977122009-01-05 21:25:51 -0500899 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400900 unsigned long op;
901
Josef Bacik287a0ab2010-03-19 18:07:23 +0000902 cur_alloc_size = disk_num_bytes;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400903 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500904 root->sectorsize, 0, alloc_hint,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500905 &ins, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100906 if (ret < 0) {
907 btrfs_abort_transaction(trans, root, ret);
908 goto out_unlock;
909 }
Chris Masond3977122009-01-05 21:25:51 -0500910
David Sterba172ddd62011-04-21 00:48:27 +0200911 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100912 BUG_ON(!em); /* -ENOMEM */
Chris Masone6dcd2d2008-07-17 12:53:50 -0400913 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500914 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500915 ram_size = ins.offset;
916 em->len = ins.offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400917
Chris Masone6dcd2d2008-07-17 12:53:50 -0400918 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400919 em->block_len = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400920 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400921 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -0400922
Chris Masond3977122009-01-05 21:25:51 -0500923 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400924 write_lock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400925 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -0400926 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400927 if (ret != -EEXIST) {
928 free_extent_map(em);
929 break;
930 }
931 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400932 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400933 }
934
Chris Mason98d20f62008-04-14 09:46:10 -0400935 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400936 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -0500937 ram_size, cur_alloc_size, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100938 BUG_ON(ret); /* -ENOMEM */
Chris Masonc8b97812008-10-29 14:49:59 -0400939
Yan Zheng17d217f2008-12-12 10:03:38 -0500940 if (root->root_key.objectid ==
941 BTRFS_DATA_RELOC_TREE_OBJECTID) {
942 ret = btrfs_reloc_clone_csums(inode, start,
943 cur_alloc_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100944 if (ret) {
945 btrfs_abort_transaction(trans, root, ret);
946 goto out_unlock;
947 }
Yan Zheng17d217f2008-12-12 10:03:38 -0500948 }
949
Chris Masond3977122009-01-05 21:25:51 -0500950 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -0400951 break;
Chris Masond3977122009-01-05 21:25:51 -0500952
Chris Masonc8b97812008-10-29 14:49:59 -0400953 /* we're not doing compressed IO, don't unlock the first
954 * page (which the caller expects to stay locked), don't
955 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -0400956 *
957 * Do set the Private2 bit so we know this page was properly
958 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -0400959 */
Chris Masona791e352009-10-08 11:27:10 -0400960 op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0;
961 op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
962 EXTENT_SET_PRIVATE2;
963
Chris Masonc8b97812008-10-29 14:49:59 -0400964 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
965 start, start + ram_size - 1,
Chris Masona791e352009-10-08 11:27:10 -0400966 locked_page, op);
Chris Masonc8b97812008-10-29 14:49:59 -0400967 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -0500968 num_bytes -= cur_alloc_size;
969 alloc_hint = ins.objectid + ins.offset;
970 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400971 }
Chris Mason771ed682008-11-06 22:02:51 -0500972 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100973out:
Chris Masonb888db22007-08-27 16:49:44 -0400974 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400975
Chris Masonbe20aa92007-12-17 20:14:01 -0500976 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100977out_unlock:
978 extent_clear_unlock_delalloc(inode,
979 &BTRFS_I(inode)->io_tree,
980 start, end, NULL,
981 EXTENT_CLEAR_UNLOCK_PAGE |
982 EXTENT_CLEAR_UNLOCK |
983 EXTENT_CLEAR_DELALLOC |
984 EXTENT_CLEAR_DIRTY |
985 EXTENT_SET_WRITEBACK |
986 EXTENT_END_WRITEBACK);
987
988 goto out;
Chris Mason771ed682008-11-06 22:02:51 -0500989}
Chris Masonc8b97812008-10-29 14:49:59 -0400990
Chris Mason771ed682008-11-06 22:02:51 -0500991/*
992 * work queue call back to started compression on a file and pages
993 */
994static noinline void async_cow_start(struct btrfs_work *work)
995{
996 struct async_cow *async_cow;
997 int num_added = 0;
998 async_cow = container_of(work, struct async_cow, work);
999
1000 compress_file_range(async_cow->inode, async_cow->locked_page,
1001 async_cow->start, async_cow->end, async_cow,
1002 &num_added);
1003 if (num_added == 0)
1004 async_cow->inode = NULL;
1005}
1006
1007/*
1008 * work queue call back to submit previously compressed pages
1009 */
1010static noinline void async_cow_submit(struct btrfs_work *work)
1011{
1012 struct async_cow *async_cow;
1013 struct btrfs_root *root;
1014 unsigned long nr_pages;
1015
1016 async_cow = container_of(work, struct async_cow, work);
1017
1018 root = async_cow->root;
1019 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
1020 PAGE_CACHE_SHIFT;
1021
1022 atomic_sub(nr_pages, &root->fs_info->async_delalloc_pages);
1023
1024 if (atomic_read(&root->fs_info->async_delalloc_pages) <
1025 5 * 1042 * 1024 &&
1026 waitqueue_active(&root->fs_info->async_submit_wait))
1027 wake_up(&root->fs_info->async_submit_wait);
1028
Chris Masond3977122009-01-05 21:25:51 -05001029 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001030 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001031}
Chris Masonc8b97812008-10-29 14:49:59 -04001032
Chris Mason771ed682008-11-06 22:02:51 -05001033static noinline void async_cow_free(struct btrfs_work *work)
1034{
1035 struct async_cow *async_cow;
1036 async_cow = container_of(work, struct async_cow, work);
1037 kfree(async_cow);
1038}
1039
1040static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1041 u64 start, u64 end, int *page_started,
1042 unsigned long *nr_written)
1043{
1044 struct async_cow *async_cow;
1045 struct btrfs_root *root = BTRFS_I(inode)->root;
1046 unsigned long nr_pages;
1047 u64 cur_end;
1048 int limit = 10 * 1024 * 1042;
1049
Chris Masona3429ab2009-10-08 12:30:20 -04001050 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1051 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001052 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001053 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001054 BUG_ON(!async_cow); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001055 async_cow->inode = inode;
1056 async_cow->root = root;
1057 async_cow->locked_page = locked_page;
1058 async_cow->start = start;
1059
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001060 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
Chris Mason771ed682008-11-06 22:02:51 -05001061 cur_end = end;
1062 else
1063 cur_end = min(end, start + 512 * 1024 - 1);
1064
1065 async_cow->end = cur_end;
1066 INIT_LIST_HEAD(&async_cow->extents);
1067
1068 async_cow->work.func = async_cow_start;
1069 async_cow->work.ordered_func = async_cow_submit;
1070 async_cow->work.ordered_free = async_cow_free;
1071 async_cow->work.flags = 0;
1072
Chris Mason771ed682008-11-06 22:02:51 -05001073 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
1074 PAGE_CACHE_SHIFT;
1075 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
1076
1077 btrfs_queue_worker(&root->fs_info->delalloc_workers,
1078 &async_cow->work);
1079
1080 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
1081 wait_event(root->fs_info->async_submit_wait,
1082 (atomic_read(&root->fs_info->async_delalloc_pages) <
1083 limit));
1084 }
1085
Chris Masond3977122009-01-05 21:25:51 -05001086 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -05001087 atomic_read(&root->fs_info->async_delalloc_pages)) {
1088 wait_event(root->fs_info->async_submit_wait,
1089 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1090 0));
1091 }
1092
1093 *nr_written += nr_pages;
1094 start = cur_end + 1;
1095 }
1096 *page_started = 1;
1097 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001098}
1099
Chris Masond3977122009-01-05 21:25:51 -05001100static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001101 u64 bytenr, u64 num_bytes)
1102{
1103 int ret;
1104 struct btrfs_ordered_sum *sums;
1105 LIST_HEAD(list);
1106
Yan Zheng07d400a2009-01-06 11:42:00 -05001107 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001108 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001109 if (ret == 0 && list_empty(&list))
1110 return 0;
1111
1112 while (!list_empty(&list)) {
1113 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1114 list_del(&sums->list);
1115 kfree(sums);
1116 }
1117 return 1;
1118}
1119
Chris Masond352ac62008-09-29 15:18:18 -04001120/*
1121 * when nowcow writeback call back. This checks for snapshots or COW copies
1122 * of the extents that exist in the file, and COWs the file as required.
1123 *
1124 * If no cow copies or snapshots exist, we write directly to the existing
1125 * blocks on disk
1126 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001127static noinline int run_delalloc_nocow(struct inode *inode,
1128 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001129 u64 start, u64 end, int *page_started, int force,
1130 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001131{
Chris Masonbe20aa92007-12-17 20:14:01 -05001132 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001133 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001134 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001135 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001136 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001137 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001138 u64 cow_start;
1139 u64 cur_offset;
1140 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001141 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001142 u64 disk_bytenr;
1143 u64 num_bytes;
1144 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001145 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001146 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001147 int nocow;
1148 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001149 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001150 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001151
1152 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07001153 if (!path)
1154 return -ENOMEM;
Li Zefan82d59022011-04-20 10:33:24 +08001155
Chris Mason2cf85722011-07-26 15:35:09 -04001156 nolock = btrfs_is_free_space_inode(root, inode);
Li Zefan82d59022011-04-20 10:33:24 +08001157
1158 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001159 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001160 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001161 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001162
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001163 if (IS_ERR(trans)) {
1164 btrfs_free_path(path);
1165 return PTR_ERR(trans);
1166 }
1167
Josef Bacik74b21072011-04-13 12:02:53 -04001168 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001169
Yan Zheng80ff3852008-10-30 14:20:02 -04001170 cow_start = (u64)-1;
1171 cur_offset = start;
1172 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001173 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001174 cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001175 if (ret < 0) {
1176 btrfs_abort_transaction(trans, root, ret);
1177 goto error;
1178 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001179 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1180 leaf = path->nodes[0];
1181 btrfs_item_key_to_cpu(leaf, &found_key,
1182 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001183 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001184 found_key.type == BTRFS_EXTENT_DATA_KEY)
1185 path->slots[0]--;
1186 }
1187 check_prev = 0;
1188next_slot:
1189 leaf = path->nodes[0];
1190 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1191 ret = btrfs_next_leaf(root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001192 if (ret < 0) {
1193 btrfs_abort_transaction(trans, root, ret);
1194 goto error;
1195 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001196 if (ret > 0)
1197 break;
1198 leaf = path->nodes[0];
1199 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001200
Yan Zheng80ff3852008-10-30 14:20:02 -04001201 nocow = 0;
1202 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001203 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001204 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001205
Li Zefan33345d012011-04-20 10:31:50 +08001206 if (found_key.objectid > ino ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001207 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1208 found_key.offset > end)
1209 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001210
Yan Zheng80ff3852008-10-30 14:20:02 -04001211 if (found_key.offset > cur_offset) {
1212 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001213 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001214 goto out_check;
1215 }
Chris Masonc31f8832008-01-08 15:46:31 -05001216
Yan Zheng80ff3852008-10-30 14:20:02 -04001217 fi = btrfs_item_ptr(leaf, path->slots[0],
1218 struct btrfs_file_extent_item);
1219 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001220
Yan Zhengd899e052008-10-30 14:25:28 -04001221 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1222 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001223 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001224 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001225 extent_end = found_key.offset +
1226 btrfs_file_extent_num_bytes(leaf, fi);
1227 if (extent_end <= start) {
1228 path->slots[0]++;
1229 goto next_slot;
1230 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001231 if (disk_bytenr == 0)
1232 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001233 if (btrfs_file_extent_compression(leaf, fi) ||
1234 btrfs_file_extent_encryption(leaf, fi) ||
1235 btrfs_file_extent_other_encoding(leaf, fi))
1236 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001237 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1238 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001239 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001240 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001241 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001242 found_key.offset -
1243 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001244 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001245 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001246 disk_bytenr += cur_offset - found_key.offset;
1247 num_bytes = min(end + 1, extent_end) - cur_offset;
1248 /*
1249 * force cow if csum exists in the range.
1250 * this ensure that csum for a given extent are
1251 * either valid or do not exist.
1252 */
1253 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1254 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001255 nocow = 1;
1256 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1257 extent_end = found_key.offset +
1258 btrfs_file_extent_inline_len(leaf, fi);
1259 extent_end = ALIGN(extent_end, root->sectorsize);
1260 } else {
1261 BUG_ON(1);
1262 }
1263out_check:
1264 if (extent_end <= start) {
1265 path->slots[0]++;
1266 goto next_slot;
1267 }
1268 if (!nocow) {
1269 if (cow_start == (u64)-1)
1270 cow_start = cur_offset;
1271 cur_offset = extent_end;
1272 if (cur_offset > end)
1273 break;
1274 path->slots[0]++;
1275 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001276 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001277
David Sterbab3b4aa72011-04-21 01:20:15 +02001278 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001279 if (cow_start != (u64)-1) {
1280 ret = cow_file_range(inode, locked_page, cow_start,
Chris Mason771ed682008-11-06 22:02:51 -05001281 found_key.offset - 1, page_started,
1282 nr_written, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001283 if (ret) {
1284 btrfs_abort_transaction(trans, root, ret);
1285 goto error;
1286 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001287 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001288 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001289
Yan Zhengd899e052008-10-30 14:25:28 -04001290 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1291 struct extent_map *em;
1292 struct extent_map_tree *em_tree;
1293 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001294 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001295 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001296 em->start = cur_offset;
Chris Mason445a6942008-11-10 11:53:33 -05001297 em->orig_start = em->start;
Yan Zhengd899e052008-10-30 14:25:28 -04001298 em->len = num_bytes;
1299 em->block_len = num_bytes;
1300 em->block_start = disk_bytenr;
1301 em->bdev = root->fs_info->fs_devices->latest_bdev;
1302 set_bit(EXTENT_FLAG_PINNED, &em->flags);
1303 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001304 write_lock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001305 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04001306 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001307 if (ret != -EEXIST) {
1308 free_extent_map(em);
1309 break;
1310 }
1311 btrfs_drop_extent_cache(inode, em->start,
1312 em->start + em->len - 1, 0);
1313 }
1314 type = BTRFS_ORDERED_PREALLOC;
1315 } else {
1316 type = BTRFS_ORDERED_NOCOW;
1317 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001318
1319 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001320 num_bytes, num_bytes, type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001321 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001322
Yan, Zhengefa56462010-05-16 10:49:59 -04001323 if (root->root_key.objectid ==
1324 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1325 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1326 num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001327 if (ret) {
1328 btrfs_abort_transaction(trans, root, ret);
1329 goto error;
1330 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001331 }
1332
Yan Zhengd899e052008-10-30 14:25:28 -04001333 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
Chris Masona791e352009-10-08 11:27:10 -04001334 cur_offset, cur_offset + num_bytes - 1,
1335 locked_page, EXTENT_CLEAR_UNLOCK_PAGE |
1336 EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
1337 EXTENT_SET_PRIVATE2);
Yan Zheng80ff3852008-10-30 14:20:02 -04001338 cur_offset = extent_end;
1339 if (cur_offset > end)
1340 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001341 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001342 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001343
1344 if (cur_offset <= end && cow_start == (u64)-1)
1345 cow_start = cur_offset;
1346 if (cow_start != (u64)-1) {
1347 ret = cow_file_range(inode, locked_page, cow_start, end,
Chris Mason771ed682008-11-06 22:02:51 -05001348 page_started, nr_written, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001349 if (ret) {
1350 btrfs_abort_transaction(trans, root, ret);
1351 goto error;
1352 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001353 }
1354
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001355error:
Josef Bacik0cb59c92010-07-02 12:14:14 -04001356 if (nolock) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001357 err = btrfs_end_transaction_nolock(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001358 } else {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001359 err = btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001360 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001361 if (!ret)
1362 ret = err;
1363
Yan Zheng7ea394f2008-08-05 13:05:02 -04001364 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001365 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001366}
1367
Chris Masond352ac62008-09-29 15:18:18 -04001368/*
1369 * extent_io.c call back to do delayed allocation processing
1370 */
Chris Masonc8b97812008-10-29 14:49:59 -04001371static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001372 u64 start, u64 end, int *page_started,
1373 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001374{
Chris Masonbe20aa92007-12-17 20:14:01 -05001375 int ret;
Chris Mason7f366cf2009-03-12 20:12:45 -04001376 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona2135012008-06-25 16:01:30 -04001377
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001378 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)
Chris Masonc8b97812008-10-29 14:49:59 -04001379 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001380 page_started, 1, nr_written);
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001381 else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC)
Yan Zhengd899e052008-10-30 14:25:28 -04001382 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001383 page_started, 0, nr_written);
Chris Mason1e701a32010-03-11 09:42:04 -05001384 else if (!btrfs_test_opt(root, COMPRESS) &&
Liu Bo75e7cb72011-03-22 10:12:20 +00001385 !(BTRFS_I(inode)->force_compress) &&
1386 !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS))
Chris Mason7f366cf2009-03-12 20:12:45 -04001387 ret = cow_file_range(inode, locked_page, start, end,
1388 page_started, nr_written, 1);
Chris Masonbe20aa92007-12-17 20:14:01 -05001389 else
Chris Mason771ed682008-11-06 22:02:51 -05001390 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001391 page_started, nr_written);
Chris Masonb888db22007-08-27 16:49:44 -04001392 return ret;
1393}
1394
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001395static void btrfs_split_extent_hook(struct inode *inode,
1396 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001397{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001398 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001399 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001400 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001401
Josef Bacik9e0baf62011-07-15 15:16:44 +00001402 spin_lock(&BTRFS_I(inode)->lock);
1403 BTRFS_I(inode)->outstanding_extents++;
1404 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001405}
1406
1407/*
1408 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1409 * extents so we can keep track of new extents that are just merged onto old
1410 * extents, such as when we are doing sequential writes, so we can properly
1411 * account for the metadata space we'll need.
1412 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001413static void btrfs_merge_extent_hook(struct inode *inode,
1414 struct extent_state *new,
1415 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001416{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001417 /* not delalloc, ignore it */
1418 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001419 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001420
Josef Bacik9e0baf62011-07-15 15:16:44 +00001421 spin_lock(&BTRFS_I(inode)->lock);
1422 BTRFS_I(inode)->outstanding_extents--;
1423 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001424}
1425
Chris Masond352ac62008-09-29 15:18:18 -04001426/*
1427 * extent_io.c set_bit_hook, used to track delayed allocation
1428 * bytes in this file, and to maintain the list of inodes that
1429 * have pending delalloc work to be done.
1430 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001431static void btrfs_set_bit_hook(struct inode *inode,
1432 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001433{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001434
Chris Mason75eff682008-12-15 15:54:40 -05001435 /*
1436 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001437 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001438 * bit, which is only set or cleared with irqs on
1439 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001440 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001441 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001442 u64 len = state->end + 1 - state->start;
Chris Mason2cf85722011-07-26 15:35:09 -04001443 bool do_list = !btrfs_is_free_space_inode(root, inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001444
Josef Bacik9e0baf62011-07-15 15:16:44 +00001445 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001446 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001447 } else {
1448 spin_lock(&BTRFS_I(inode)->lock);
1449 BTRFS_I(inode)->outstanding_extents++;
1450 spin_unlock(&BTRFS_I(inode)->lock);
1451 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001452
Chris Mason75eff682008-12-15 15:54:40 -05001453 spin_lock(&root->fs_info->delalloc_lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001454 BTRFS_I(inode)->delalloc_bytes += len;
1455 root->fs_info->delalloc_bytes += len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001456 if (do_list && list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
Chris Masonea8c2812008-08-04 23:17:27 -04001457 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1458 &root->fs_info->delalloc_inodes);
1459 }
Chris Mason75eff682008-12-15 15:54:40 -05001460 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001461 }
Chris Mason291d6732008-01-29 15:55:23 -05001462}
1463
Chris Masond352ac62008-09-29 15:18:18 -04001464/*
1465 * extent_io.c clear_bit_hook, see set_bit_hook for why
1466 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001467static void btrfs_clear_bit_hook(struct inode *inode,
1468 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001469{
Chris Mason75eff682008-12-15 15:54:40 -05001470 /*
1471 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001472 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001473 * bit, which is only set or cleared with irqs on
1474 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001475 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001476 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001477 u64 len = state->end + 1 - state->start;
Chris Mason2cf85722011-07-26 15:35:09 -04001478 bool do_list = !btrfs_is_free_space_inode(root, inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001479
Josef Bacik9e0baf62011-07-15 15:16:44 +00001480 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001481 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001482 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1483 spin_lock(&BTRFS_I(inode)->lock);
1484 BTRFS_I(inode)->outstanding_extents--;
1485 spin_unlock(&BTRFS_I(inode)->lock);
1486 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001487
1488 if (*bits & EXTENT_DO_ACCOUNTING)
1489 btrfs_delalloc_release_metadata(inode, len);
1490
Josef Bacik0cb59c92010-07-02 12:14:14 -04001491 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
1492 && do_list)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001493 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001494
Chris Mason75eff682008-12-15 15:54:40 -05001495 spin_lock(&root->fs_info->delalloc_lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001496 root->fs_info->delalloc_bytes -= len;
1497 BTRFS_I(inode)->delalloc_bytes -= len;
1498
Josef Bacik0cb59c92010-07-02 12:14:14 -04001499 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Chris Masonea8c2812008-08-04 23:17:27 -04001500 !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1501 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1502 }
Chris Mason75eff682008-12-15 15:54:40 -05001503 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001504 }
Chris Mason291d6732008-01-29 15:55:23 -05001505}
1506
Chris Masond352ac62008-09-29 15:18:18 -04001507/*
1508 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1509 * we don't create bios that span stripes or chunks
1510 */
Chris Mason239b14b2008-03-24 15:02:07 -04001511int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001512 size_t size, struct bio *bio,
1513 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001514{
1515 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
1516 struct btrfs_mapping_tree *map_tree;
Chris Masona62b9402008-10-03 16:31:08 -04001517 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001518 u64 length = 0;
1519 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001520 int ret;
1521
Chris Mason771ed682008-11-06 22:02:51 -05001522 if (bio_flags & EXTENT_BIO_COMPRESSED)
1523 return 0;
1524
Chris Masonf2d8d742008-04-21 10:03:05 -04001525 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001526 map_tree = &root->fs_info->mapping_tree;
1527 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04001528 ret = btrfs_map_block(map_tree, READ, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001529 &map_length, NULL, 0);
Jeff Mahoney3444a972011-10-03 23:23:13 -04001530 /* Will always return 0 or 1 with map_multi == NULL */
1531 BUG_ON(ret < 0);
Chris Masond3977122009-01-05 21:25:51 -05001532 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001533 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001534 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001535}
1536
Chris Masond352ac62008-09-29 15:18:18 -04001537/*
1538 * in order to insert checksums into the metadata in large chunks,
1539 * we wait until bio submission time. All the pages in the bio are
1540 * checksummed and sums are attached onto the ordered extent record.
1541 *
1542 * At IO completion time the cums attached on the ordered extent record
1543 * are inserted into the btree
1544 */
Chris Masond3977122009-01-05 21:25:51 -05001545static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1546 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001547 unsigned long bio_flags,
1548 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001549{
Chris Mason065631f2008-02-20 12:07:25 -05001550 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001551 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001552
Chris Masond20f7042008-12-08 16:58:54 -05001553 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001554 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001555 return 0;
1556}
Chris Masone0156402008-04-16 11:15:20 -04001557
Chris Mason4a69a412008-11-06 22:03:00 -05001558/*
1559 * in order to insert checksums into the metadata in large chunks,
1560 * we wait until bio submission time. All the pages in the bio are
1561 * checksummed and sums are attached onto the ordered extent record.
1562 *
1563 * At IO completion time the cums attached on the ordered extent record
1564 * are inserted into the btree
1565 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001566static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001567 int mirror_num, unsigned long bio_flags,
1568 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001569{
1570 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason8b712842008-06-11 16:50:36 -04001571 return btrfs_map_bio(root, rw, bio, mirror_num, 1);
Chris Mason44b8bd72008-04-16 11:14:51 -04001572}
1573
Chris Masond352ac62008-09-29 15:18:18 -04001574/*
Chris Masoncad321a2008-12-17 14:51:42 -05001575 * extent_io.c submission hook. This does the right thing for csum calculation
1576 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001577 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001578static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001579 int mirror_num, unsigned long bio_flags,
1580 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001581{
1582 struct btrfs_root *root = BTRFS_I(inode)->root;
1583 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001584 int skip_sum;
Jeff Mahoney04173412011-10-03 23:23:12 -04001585 int metadata = 0;
Chris Mason44b8bd72008-04-16 11:14:51 -04001586
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001587 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001588
Chris Mason2cf85722011-07-26 15:35:09 -04001589 if (btrfs_is_free_space_inode(root, inode))
Jeff Mahoney04173412011-10-03 23:23:12 -04001590 metadata = 2;
1591
1592 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
Jeff Mahoney355808c2011-10-03 23:23:14 -04001593 if (ret)
1594 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001595
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001596 if (!(rw & REQ_WRITE)) {
Chris Masond20f7042008-12-08 16:58:54 -05001597 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Chris Masonc8b97812008-10-29 14:49:59 -04001598 return btrfs_submit_compressed_read(inode, bio,
1599 mirror_num, bio_flags);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001600 } else if (!skip_sum) {
1601 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1602 if (ret)
1603 return ret;
1604 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001605 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001606 } else if (!skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001607 /* csum items have already been cloned */
1608 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1609 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001610 /* we're doing a write, do the async checksumming */
1611 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001612 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001613 bio_flags, bio_offset,
1614 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001615 __btrfs_submit_bio_done);
Chris Mason19b9bdb2008-10-30 14:23:13 -04001616 }
1617
Chris Mason0b86a832008-03-24 15:01:56 -04001618mapit:
Chris Mason8b712842008-06-11 16:50:36 -04001619 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
Chris Mason065631f2008-02-20 12:07:25 -05001620}
Chris Mason6885f302008-02-20 16:11:05 -05001621
Chris Masond352ac62008-09-29 15:18:18 -04001622/*
1623 * given a list of ordered sums record them in the inode. This happens
1624 * at IO completion time based on sums calculated at bio submission time.
1625 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001626static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001627 struct inode *inode, u64 file_offset,
1628 struct list_head *list)
1629{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001630 struct btrfs_ordered_sum *sum;
1631
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001632 list_for_each_entry(sum, list, list) {
Chris Masond20f7042008-12-08 16:58:54 -05001633 btrfs_csum_file_blocks(trans,
1634 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001635 }
1636 return 0;
1637}
1638
Josef Bacik2ac55d42010-02-03 19:33:23 +00001639int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1640 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001641{
Chris Masond3977122009-01-05 21:25:51 -05001642 if ((end & (PAGE_CACHE_SIZE - 1)) == 0)
Chris Mason771ed682008-11-06 22:02:51 -05001643 WARN_ON(1);
Chris Masonea8c2812008-08-04 23:17:27 -04001644 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001645 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001646}
1647
Chris Masond352ac62008-09-29 15:18:18 -04001648/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001649struct btrfs_writepage_fixup {
1650 struct page *page;
1651 struct btrfs_work work;
1652};
1653
Christoph Hellwigb2950862008-12-02 09:54:17 -05001654static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001655{
1656 struct btrfs_writepage_fixup *fixup;
1657 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001658 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001659 struct page *page;
1660 struct inode *inode;
1661 u64 page_start;
1662 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001663 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001664
1665 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1666 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001667again:
Chris Mason247e7432008-07-17 12:53:51 -04001668 lock_page(page);
1669 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1670 ClearPageChecked(page);
1671 goto out_page;
1672 }
1673
1674 inode = page->mapping->host;
1675 page_start = page_offset(page);
1676 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1677
Josef Bacik2ac55d42010-02-03 19:33:23 +00001678 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001679 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001680
1681 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001682 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001683 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001684
1685 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1686 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001687 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1688 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001689 unlock_page(page);
1690 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001691 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04001692 goto again;
1693 }
Chris Mason247e7432008-07-17 12:53:51 -04001694
Jeff Mahoney87826df2012-02-15 16:23:57 +01001695 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
1696 if (ret) {
1697 mapping_set_error(page->mapping, ret);
1698 end_extent_writepage(page, ret, page_start, page_end);
1699 ClearPageChecked(page);
1700 goto out;
1701 }
1702
Josef Bacik2ac55d42010-02-03 19:33:23 +00001703 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001704 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001705 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04001706out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001707 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1708 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001709out_page:
1710 unlock_page(page);
1711 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08001712 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04001713}
1714
1715/*
1716 * There are a few paths in the higher layers of the kernel that directly
1717 * set the page dirty bit without asking the filesystem if it is a
1718 * good idea. This causes problems because we want to make sure COW
1719 * properly happens and the data=ordered rules are followed.
1720 *
Chris Masonc8b97812008-10-29 14:49:59 -04001721 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001722 * hasn't been properly setup for IO. We kick off an async process
1723 * to fix it up. The async helper will wait for ordered extents, set
1724 * the delalloc bit and make it safe to write the page.
1725 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001726static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001727{
1728 struct inode *inode = page->mapping->host;
1729 struct btrfs_writepage_fixup *fixup;
1730 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001731
Chris Mason8b62b722009-09-02 16:53:46 -04001732 /* this page is properly in the ordered list */
1733 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001734 return 0;
1735
1736 if (PageChecked(page))
1737 return -EAGAIN;
1738
1739 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1740 if (!fixup)
1741 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001742
Chris Mason247e7432008-07-17 12:53:51 -04001743 SetPageChecked(page);
1744 page_cache_get(page);
1745 fixup->work.func = btrfs_writepage_fixup_worker;
1746 fixup->page = page;
1747 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001748 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04001749}
1750
Yan Zhengd899e052008-10-30 14:25:28 -04001751static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1752 struct inode *inode, u64 file_pos,
1753 u64 disk_bytenr, u64 disk_num_bytes,
1754 u64 num_bytes, u64 ram_bytes,
1755 u8 compression, u8 encryption,
1756 u16 other_encoding, int extent_type)
1757{
1758 struct btrfs_root *root = BTRFS_I(inode)->root;
1759 struct btrfs_file_extent_item *fi;
1760 struct btrfs_path *path;
1761 struct extent_buffer *leaf;
1762 struct btrfs_key ins;
1763 u64 hint;
1764 int ret;
1765
1766 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07001767 if (!path)
1768 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04001769
Chris Masonb9473432009-03-13 11:00:37 -04001770 path->leave_spinning = 1;
Chris Masona1ed8352009-09-11 12:27:37 -04001771
1772 /*
1773 * we may be replacing one extent in the tree with another.
1774 * The new extent is pinned in the extent map, and we don't want
1775 * to drop it from the cache until it is completely in the btree.
1776 *
1777 * So, tell btrfs_drop_extents to leave this extent in the cache.
1778 * the caller is expected to unpin it and allow it to be merged
1779 * with the others.
1780 */
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001781 ret = btrfs_drop_extents(trans, inode, file_pos, file_pos + num_bytes,
1782 &hint, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001783 if (ret)
1784 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001785
Li Zefan33345d012011-04-20 10:31:50 +08001786 ins.objectid = btrfs_ino(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04001787 ins.offset = file_pos;
1788 ins.type = BTRFS_EXTENT_DATA_KEY;
1789 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001790 if (ret)
1791 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001792 leaf = path->nodes[0];
1793 fi = btrfs_item_ptr(leaf, path->slots[0],
1794 struct btrfs_file_extent_item);
1795 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1796 btrfs_set_file_extent_type(leaf, fi, extent_type);
1797 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1798 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1799 btrfs_set_file_extent_offset(leaf, fi, 0);
1800 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1801 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1802 btrfs_set_file_extent_compression(leaf, fi, compression);
1803 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1804 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04001805
1806 btrfs_unlock_up_safe(path, 1);
1807 btrfs_set_lock_blocking(leaf);
1808
Yan Zhengd899e052008-10-30 14:25:28 -04001809 btrfs_mark_buffer_dirty(leaf);
1810
1811 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04001812
1813 ins.objectid = disk_bytenr;
1814 ins.offset = disk_num_bytes;
1815 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001816 ret = btrfs_alloc_reserved_file_extent(trans, root,
1817 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08001818 btrfs_ino(inode), file_pos, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001819out:
Yan Zhengd899e052008-10-30 14:25:28 -04001820 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04001821
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001822 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04001823}
1824
Chris Mason5d13a982009-03-13 11:41:46 -04001825/*
1826 * helper function for btrfs_finish_ordered_io, this
1827 * just reads in some of the csum leaves to prime them into ram
1828 * before we start the transaction. It limits the amount of btree
1829 * reads required while inside the transaction.
1830 */
Chris Masond352ac62008-09-29 15:18:18 -04001831/* as ordered data IO finishes, this gets called so we can finish
1832 * an ordered extent if the range of bytes in the file it covers are
1833 * fully written.
1834 */
Chris Mason211f90e2008-07-18 11:56:15 -04001835static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001836{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001837 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001838 struct btrfs_trans_handle *trans = NULL;
Chris Mason5d13a982009-03-13 11:41:46 -04001839 struct btrfs_ordered_extent *ordered_extent = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001840 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001841 struct extent_state *cached_state = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08001842 int compress_type = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001843 int ret;
Li Zefan82d59022011-04-20 10:33:24 +08001844 bool nolock;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001845
Josef Bacik5a1a3df2010-02-02 20:51:14 +00001846 ret = btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
1847 end - start + 1);
Chris Masonba1da2f2008-07-17 12:54:15 -04001848 if (!ret)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001849 return 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001850 BUG_ON(!ordered_extent); /* Logic error */
Josef Bacikefd049f2010-02-02 20:50:10 +00001851
Chris Mason2cf85722011-07-26 15:35:09 -04001852 nolock = btrfs_is_free_space_inode(root, inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001853
Yan, Zhengc2167752009-11-12 09:34:21 +00001854 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001855 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Yan, Zhengc2167752009-11-12 09:34:21 +00001856 ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1857 if (!ret) {
Josef Bacik0cb59c92010-07-02 12:14:14 -04001858 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001859 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001860 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001861 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001862 if (IS_ERR(trans))
1863 return PTR_ERR(trans);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001864 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Mason21151332011-11-10 20:39:08 -05001865 ret = btrfs_update_inode_fallback(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001866 if (ret) /* -ENOMEM or corruption */
1867 btrfs_abort_transaction(trans, root, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00001868 }
1869 goto out;
1870 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04001871
Josef Bacik2ac55d42010-02-03 19:33:23 +00001872 lock_extent_bits(io_tree, ordered_extent->file_offset,
1873 ordered_extent->file_offset + ordered_extent->len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001874 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001875
Josef Bacik0cb59c92010-07-02 12:14:14 -04001876 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001877 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001878 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001879 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001880 if (IS_ERR(trans)) {
1881 ret = PTR_ERR(trans);
1882 trans = NULL;
1883 goto out_unlock;
1884 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001885 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00001886
Chris Masonc8b97812008-10-29 14:49:59 -04001887 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08001888 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04001889 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08001890 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001891 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04001892 ordered_extent->file_offset,
1893 ordered_extent->file_offset +
1894 ordered_extent->len);
Yan Zhengd899e052008-10-30 14:25:28 -04001895 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04001896 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04001897 ret = insert_reserved_file_extent(trans, inode,
1898 ordered_extent->file_offset,
1899 ordered_extent->start,
1900 ordered_extent->disk_len,
1901 ordered_extent->len,
1902 ordered_extent->len,
Li Zefan261507a02010-12-17 14:21:50 +08001903 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04001904 BTRFS_FILE_EXTENT_REG);
Chris Masona1ed8352009-09-11 12:27:37 -04001905 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
1906 ordered_extent->file_offset,
1907 ordered_extent->len);
Yan Zhengd899e052008-10-30 14:25:28 -04001908 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00001909 unlock_extent_cached(io_tree, ordered_extent->file_offset,
1910 ordered_extent->file_offset +
1911 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001912 if (ret < 0) {
1913 btrfs_abort_transaction(trans, root, ret);
1914 goto out;
1915 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00001916
Chris Masone6dcd2d2008-07-17 12:53:50 -04001917 add_pending_csums(trans, inode, ordered_extent->file_offset,
1918 &ordered_extent->list);
1919
Josef Bacik1ef30be2011-04-05 19:25:36 -04001920 ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent);
Miao Xiea39f7522011-09-11 10:52:25 -04001921 if (!ret || !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Chris Mason21151332011-11-10 20:39:08 -05001922 ret = btrfs_update_inode_fallback(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001923 if (ret) { /* -ENOMEM or corruption */
1924 btrfs_abort_transaction(trans, root, ret);
1925 goto out;
1926 }
Josef Bacik1ef30be2011-04-05 19:25:36 -04001927 }
1928 ret = 0;
Yan, Zhengc2167752009-11-12 09:34:21 +00001929out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04001930 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04001931 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Josef Bacik5b0e95b2011-10-06 08:58:24 -04001932 if (trans) {
1933 if (nolock)
Josef Bacik0cb59c92010-07-02 12:14:14 -04001934 btrfs_end_transaction_nolock(trans, root);
Josef Bacik5b0e95b2011-10-06 08:58:24 -04001935 else
Josef Bacik0cb59c92010-07-02 12:14:14 -04001936 btrfs_end_transaction(trans, root);
1937 }
1938
Chris Masone6dcd2d2008-07-17 12:53:50 -04001939 /* once for us */
1940 btrfs_put_ordered_extent(ordered_extent);
1941 /* once for the tree */
1942 btrfs_put_ordered_extent(ordered_extent);
1943
Chris Masone6dcd2d2008-07-17 12:53:50 -04001944 return 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001945out_unlock:
1946 unlock_extent_cached(io_tree, ordered_extent->file_offset,
1947 ordered_extent->file_offset +
1948 ordered_extent->len - 1, &cached_state, GFP_NOFS);
1949 goto out;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001950}
1951
Christoph Hellwigb2950862008-12-02 09:54:17 -05001952static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04001953 struct extent_state *state, int uptodate)
1954{
liubo1abe9b82011-03-24 11:18:59 +00001955 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
1956
Chris Mason8b62b722009-09-02 16:53:46 -04001957 ClearPagePrivate2(page);
Chris Mason211f90e2008-07-18 11:56:15 -04001958 return btrfs_finish_ordered_io(page->mapping->host, start, end);
1959}
1960
Chris Masond352ac62008-09-29 15:18:18 -04001961/*
Chris Masond352ac62008-09-29 15:18:18 -04001962 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001963 * if there's a match, we allow the bio to finish. If not, the code in
1964 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04001965 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001966static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
Josef Bacik5cf1ab52012-04-16 09:42:26 -04001967 struct extent_state *state, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04001968{
Chris Mason35ebb932007-10-30 16:56:53 -04001969 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -04001970 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05001971 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04001972 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -05001973 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -04001974 int ret;
Chris Masonff79f812007-10-15 16:22:25 -04001975 struct btrfs_root *root = BTRFS_I(inode)->root;
1976 u32 csum = ~(u32)0;
Chris Masond1310b22008-01-24 16:13:08 -05001977
Chris Masond20f7042008-12-08 16:58:54 -05001978 if (PageChecked(page)) {
1979 ClearPageChecked(page);
1980 goto good;
1981 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001982
1983 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Jan Schmidt08d2f342011-05-04 16:18:50 +02001984 goto good;
Chris Masond20f7042008-12-08 16:58:54 -05001985
Yan Zheng17d217f2008-12-12 10:03:38 -05001986 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04001987 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001988 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
1989 GFP_NOFS);
1990 return 0;
1991 }
1992
Yanc2e639f2008-02-04 08:57:25 -05001993 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -05001994 private = state->private;
1995 ret = 0;
1996 } else {
1997 ret = get_state_private(io_tree, start, &private);
1998 }
Cong Wang7ac687d2011-11-25 23:14:28 +08001999 kaddr = kmap_atomic(page);
Chris Masond3977122009-01-05 21:25:51 -05002000 if (ret)
Chris Mason07157aa2007-08-30 08:50:51 -04002001 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002002
Chris Masonff79f812007-10-15 16:22:25 -04002003 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
2004 btrfs_csum_final(csum, (char *)&csum);
Chris Masond3977122009-01-05 21:25:51 -05002005 if (csum != private)
Chris Mason07157aa2007-08-30 08:50:51 -04002006 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002007
Cong Wang7ac687d2011-11-25 23:14:28 +08002008 kunmap_atomic(kaddr);
Chris Masond20f7042008-12-08 16:58:54 -05002009good:
Chris Mason07157aa2007-08-30 08:50:51 -04002010 return 0;
2011
2012zeroit:
Chris Mason945d8962011-05-22 12:33:42 -04002013 printk_ratelimited(KERN_INFO "btrfs csum failed ino %llu off %llu csum %u "
Li Zefan33345d012011-04-20 10:31:50 +08002014 "private %llu\n",
2015 (unsigned long long)btrfs_ino(page->mapping->host),
Chris Mason193f2842009-04-27 07:29:05 -04002016 (unsigned long long)start, csum,
2017 (unsigned long long)private);
Chris Masondb945352007-10-15 16:15:53 -04002018 memset(kaddr + offset, 1, end - start + 1);
2019 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002020 kunmap_atomic(kaddr);
Chris Mason3b951512008-04-17 11:29:12 -04002021 if (private == 0)
2022 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04002023 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04002024}
Chris Masonb888db22007-08-27 16:49:44 -04002025
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002026struct delayed_iput {
2027 struct list_head list;
2028 struct inode *inode;
2029};
2030
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002031/* JDM: If this is fs-wide, why can't we add a pointer to
2032 * btrfs_inode instead and avoid the allocation? */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002033void btrfs_add_delayed_iput(struct inode *inode)
2034{
2035 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2036 struct delayed_iput *delayed;
2037
2038 if (atomic_add_unless(&inode->i_count, -1, 1))
2039 return;
2040
2041 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2042 delayed->inode = inode;
2043
2044 spin_lock(&fs_info->delayed_iput_lock);
2045 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2046 spin_unlock(&fs_info->delayed_iput_lock);
2047}
2048
2049void btrfs_run_delayed_iputs(struct btrfs_root *root)
2050{
2051 LIST_HEAD(list);
2052 struct btrfs_fs_info *fs_info = root->fs_info;
2053 struct delayed_iput *delayed;
2054 int empty;
2055
2056 spin_lock(&fs_info->delayed_iput_lock);
2057 empty = list_empty(&fs_info->delayed_iputs);
2058 spin_unlock(&fs_info->delayed_iput_lock);
2059 if (empty)
2060 return;
2061
2062 down_read(&root->fs_info->cleanup_work_sem);
2063 spin_lock(&fs_info->delayed_iput_lock);
2064 list_splice_init(&fs_info->delayed_iputs, &list);
2065 spin_unlock(&fs_info->delayed_iput_lock);
2066
2067 while (!list_empty(&list)) {
2068 delayed = list_entry(list.next, struct delayed_iput, list);
2069 list_del(&delayed->list);
2070 iput(delayed->inode);
2071 kfree(delayed);
2072 }
2073 up_read(&root->fs_info->cleanup_work_sem);
2074}
2075
Yan, Zhengd68fc572010-05-16 10:49:58 -04002076enum btrfs_orphan_cleanup_state {
2077 ORPHAN_CLEANUP_STARTED = 1,
2078 ORPHAN_CLEANUP_DONE = 2,
2079};
2080
2081/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08002082 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04002083 * files in the subvolume, it removes orphan item and frees block_rsv
2084 * structure.
2085 */
2086void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
2087 struct btrfs_root *root)
2088{
Josef Bacik90290e12011-12-02 15:44:12 -05002089 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002090 int ret;
2091
2092 if (!list_empty(&root->orphan_list) ||
2093 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
2094 return;
2095
Josef Bacik90290e12011-12-02 15:44:12 -05002096 spin_lock(&root->orphan_lock);
2097 if (!list_empty(&root->orphan_list)) {
2098 spin_unlock(&root->orphan_lock);
2099 return;
2100 }
2101
2102 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
2103 spin_unlock(&root->orphan_lock);
2104 return;
2105 }
2106
2107 block_rsv = root->orphan_block_rsv;
2108 root->orphan_block_rsv = NULL;
2109 spin_unlock(&root->orphan_lock);
2110
Yan, Zhengd68fc572010-05-16 10:49:58 -04002111 if (root->orphan_item_inserted &&
2112 btrfs_root_refs(&root->root_item) > 0) {
2113 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
2114 root->root_key.objectid);
2115 BUG_ON(ret);
2116 root->orphan_item_inserted = 0;
2117 }
2118
Josef Bacik90290e12011-12-02 15:44:12 -05002119 if (block_rsv) {
2120 WARN_ON(block_rsv->size > 0);
2121 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002122 }
2123}
2124
2125/*
Josef Bacik7b128762008-07-24 12:17:14 -04002126 * This creates an orphan entry for the given inode in case something goes
2127 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04002128 *
2129 * NOTE: caller of this function should reserve 5 units of metadata for
2130 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04002131 */
2132int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2133{
2134 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002135 struct btrfs_block_rsv *block_rsv = NULL;
2136 int reserve = 0;
2137 int insert = 0;
2138 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002139
Yan, Zhengd68fc572010-05-16 10:49:58 -04002140 if (!root->orphan_block_rsv) {
2141 block_rsv = btrfs_alloc_block_rsv(root);
Tsutomu Itohb5324022011-07-19 07:27:20 +00002142 if (!block_rsv)
2143 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04002144 }
2145
Yan, Zhengd68fc572010-05-16 10:49:58 -04002146 spin_lock(&root->orphan_lock);
2147 if (!root->orphan_block_rsv) {
2148 root->orphan_block_rsv = block_rsv;
2149 } else if (block_rsv) {
2150 btrfs_free_block_rsv(root, block_rsv);
2151 block_rsv = NULL;
2152 }
Josef Bacik7b128762008-07-24 12:17:14 -04002153
Yan, Zhengd68fc572010-05-16 10:49:58 -04002154 if (list_empty(&BTRFS_I(inode)->i_orphan)) {
2155 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
2156#if 0
2157 /*
2158 * For proper ENOSPC handling, we should do orphan
2159 * cleanup when mounting. But this introduces backward
2160 * compatibility issue.
2161 */
2162 if (!xchg(&root->orphan_item_inserted, 1))
2163 insert = 2;
2164 else
2165 insert = 1;
2166#endif
2167 insert = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002168 }
Josef Bacik7b128762008-07-24 12:17:14 -04002169
Yan, Zhengd68fc572010-05-16 10:49:58 -04002170 if (!BTRFS_I(inode)->orphan_meta_reserved) {
2171 BTRFS_I(inode)->orphan_meta_reserved = 1;
2172 reserve = 1;
2173 }
2174 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002175
Yan, Zhengd68fc572010-05-16 10:49:58 -04002176 /* grab metadata reservation from transaction handle */
2177 if (reserve) {
2178 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002179 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04002180 }
2181
2182 /* insert an orphan item to track this unlinked/truncated file */
2183 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08002184 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002185 if (ret && ret != -EEXIST) {
2186 btrfs_abort_transaction(trans, root, ret);
2187 return ret;
2188 }
2189 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002190 }
2191
2192 /* insert an orphan item to track subvolume contains orphan files */
2193 if (insert >= 2) {
2194 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
2195 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002196 if (ret && ret != -EEXIST) {
2197 btrfs_abort_transaction(trans, root, ret);
2198 return ret;
2199 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04002200 }
2201 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002202}
2203
2204/*
2205 * We have done the truncate/delete so we can go ahead and remove the orphan
2206 * item for this particular inode.
2207 */
2208int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
2209{
2210 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002211 int delete_item = 0;
2212 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002213 int ret = 0;
2214
Yan, Zhengd68fc572010-05-16 10:49:58 -04002215 spin_lock(&root->orphan_lock);
2216 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
2217 list_del_init(&BTRFS_I(inode)->i_orphan);
2218 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04002219 }
2220
Yan, Zhengd68fc572010-05-16 10:49:58 -04002221 if (BTRFS_I(inode)->orphan_meta_reserved) {
2222 BTRFS_I(inode)->orphan_meta_reserved = 0;
2223 release_rsv = 1;
2224 }
2225 spin_unlock(&root->orphan_lock);
2226
2227 if (trans && delete_item) {
Li Zefan33345d012011-04-20 10:31:50 +08002228 ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002229 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacik7b128762008-07-24 12:17:14 -04002230 }
2231
Yan, Zhengd68fc572010-05-16 10:49:58 -04002232 if (release_rsv)
2233 btrfs_orphan_release_metadata(inode);
Josef Bacik7b128762008-07-24 12:17:14 -04002234
Yan, Zhengd68fc572010-05-16 10:49:58 -04002235 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002236}
2237
2238/*
2239 * this cleans up any orphans that may be left on the list from the last use
2240 * of this root.
2241 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002242int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04002243{
2244 struct btrfs_path *path;
2245 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04002246 struct btrfs_key key, found_key;
2247 struct btrfs_trans_handle *trans;
2248 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04002249 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002250 int ret = 0, nr_unlink = 0, nr_truncate = 0;
2251
Yan, Zhengd68fc572010-05-16 10:49:58 -04002252 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002253 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00002254
2255 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002256 if (!path) {
2257 ret = -ENOMEM;
2258 goto out;
2259 }
Josef Bacik7b128762008-07-24 12:17:14 -04002260 path->reada = -1;
2261
2262 key.objectid = BTRFS_ORPHAN_OBJECTID;
2263 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
2264 key.offset = (u64)-1;
2265
Josef Bacik7b128762008-07-24 12:17:14 -04002266 while (1) {
2267 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002268 if (ret < 0)
2269 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04002270
2271 /*
2272 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002273 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04002274 * find the key and see if we have stuff that matches
2275 */
2276 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002277 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002278 if (path->slots[0] == 0)
2279 break;
2280 path->slots[0]--;
2281 }
2282
2283 /* pull out the item */
2284 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04002285 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2286
2287 /* make sure the item matches what we want */
2288 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
2289 break;
2290 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
2291 break;
2292
2293 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02002294 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04002295
2296 /*
2297 * this is where we are basically btrfs_lookup, without the
2298 * crossing root thing. we store the inode number in the
2299 * offset of the orphan item.
2300 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04002301
2302 if (found_key.offset == last_objectid) {
2303 printk(KERN_ERR "btrfs: Error removing orphan entry, "
2304 "stopping orphan cleanup\n");
2305 ret = -EINVAL;
2306 goto out;
2307 }
2308
2309 last_objectid = found_key.offset;
2310
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002311 found_key.objectid = found_key.offset;
2312 found_key.type = BTRFS_INODE_ITEM_KEY;
2313 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00002314 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Josef Bacika8c9e572011-09-21 16:55:59 -04002315 ret = PTR_RET(inode);
2316 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002317 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04002318
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05002319 if (ret == -ESTALE && root == root->fs_info->tree_root) {
2320 struct btrfs_root *dead_root;
2321 struct btrfs_fs_info *fs_info = root->fs_info;
2322 int is_dead_root = 0;
2323
2324 /*
2325 * this is an orphan in the tree root. Currently these
2326 * could come from 2 sources:
2327 * a) a snapshot deletion in progress
2328 * b) a free space cache inode
2329 * We need to distinguish those two, as the snapshot
2330 * orphan must not get deleted.
2331 * find_dead_roots already ran before us, so if this
2332 * is a snapshot deletion, we should find the root
2333 * in the dead_roots list
2334 */
2335 spin_lock(&fs_info->trans_lock);
2336 list_for_each_entry(dead_root, &fs_info->dead_roots,
2337 root_list) {
2338 if (dead_root->root_key.objectid ==
2339 found_key.objectid) {
2340 is_dead_root = 1;
2341 break;
2342 }
2343 }
2344 spin_unlock(&fs_info->trans_lock);
2345 if (is_dead_root) {
2346 /* prevent this orphan from being found again */
2347 key.offset = found_key.objectid - 1;
2348 continue;
2349 }
2350 }
Josef Bacika8c9e572011-09-21 16:55:59 -04002351 /*
2352 * Inode is already gone but the orphan item is still there,
2353 * kill the orphan item.
2354 */
2355 if (ret == -ESTALE) {
2356 trans = btrfs_start_transaction(root, 1);
2357 if (IS_ERR(trans)) {
2358 ret = PTR_ERR(trans);
2359 goto out;
2360 }
2361 ret = btrfs_del_orphan_item(trans, root,
2362 found_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002363 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacika8c9e572011-09-21 16:55:59 -04002364 btrfs_end_transaction(trans, root);
2365 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002366 }
Josef Bacik7b128762008-07-24 12:17:14 -04002367
Josef Bacik7b128762008-07-24 12:17:14 -04002368 /*
2369 * add this inode to the orphan list so btrfs_orphan_del does
2370 * the proper thing when we hit it
2371 */
Yan, Zhengd68fc572010-05-16 10:49:58 -04002372 spin_lock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002373 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002374 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002375
Josef Bacik7b128762008-07-24 12:17:14 -04002376 /* if we have links, this was a truncate, lets do that */
2377 if (inode->i_nlink) {
Josef Bacika41ad392011-01-31 15:30:16 -05002378 if (!S_ISREG(inode->i_mode)) {
2379 WARN_ON(1);
2380 iput(inode);
2381 continue;
2382 }
Josef Bacik7b128762008-07-24 12:17:14 -04002383 nr_truncate++;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002384 ret = btrfs_truncate(inode);
Josef Bacik7b128762008-07-24 12:17:14 -04002385 } else {
2386 nr_unlink++;
2387 }
2388
2389 /* this will do delete_inode and everything for us */
2390 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002391 if (ret)
2392 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04002393 }
Miao Xie3254c872011-11-10 20:45:05 -05002394 /* release the path since we're done with it */
2395 btrfs_release_path(path);
2396
Yan, Zhengd68fc572010-05-16 10:49:58 -04002397 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
2398
2399 if (root->orphan_block_rsv)
2400 btrfs_block_rsv_release(root, root->orphan_block_rsv,
2401 (u64)-1);
2402
2403 if (root->orphan_block_rsv || root->orphan_item_inserted) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002404 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002405 if (!IS_ERR(trans))
2406 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002407 }
Josef Bacik7b128762008-07-24 12:17:14 -04002408
2409 if (nr_unlink)
2410 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
2411 if (nr_truncate)
2412 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002413
2414out:
2415 if (ret)
2416 printk(KERN_CRIT "btrfs: could not do orphan cleanup %d\n", ret);
2417 btrfs_free_path(path);
2418 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002419}
2420
Chris Masond352ac62008-09-29 15:18:18 -04002421/*
Chris Mason46a53cc2009-04-27 11:47:50 -04002422 * very simple check to peek ahead in the leaf looking for xattrs. If we
2423 * don't find any xattrs, we know there can't be any acls.
2424 *
2425 * slot is the slot the inode is in, objectid is the objectid of the inode
2426 */
2427static noinline int acls_after_inode_item(struct extent_buffer *leaf,
2428 int slot, u64 objectid)
2429{
2430 u32 nritems = btrfs_header_nritems(leaf);
2431 struct btrfs_key found_key;
2432 int scanned = 0;
2433
2434 slot++;
2435 while (slot < nritems) {
2436 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2437
2438 /* we found a different objectid, there must not be acls */
2439 if (found_key.objectid != objectid)
2440 return 0;
2441
2442 /* we found an xattr, assume we've got an acl */
2443 if (found_key.type == BTRFS_XATTR_ITEM_KEY)
2444 return 1;
2445
2446 /*
2447 * we found a key greater than an xattr key, there can't
2448 * be any acls later on
2449 */
2450 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
2451 return 0;
2452
2453 slot++;
2454 scanned++;
2455
2456 /*
2457 * it goes inode, inode backrefs, xattrs, extents,
2458 * so if there are a ton of hard links to an inode there can
2459 * be a lot of backrefs. Don't waste time searching too hard,
2460 * this is just an optimization
2461 */
2462 if (scanned >= 8)
2463 break;
2464 }
2465 /* we hit the end of the leaf before we found an xattr or
2466 * something larger than an xattr. We have to assume the inode
2467 * has acls
2468 */
2469 return 1;
2470}
2471
2472/*
Chris Masond352ac62008-09-29 15:18:18 -04002473 * read an inode from the btree into the in-memory inode
2474 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002475static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002476{
2477 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002478 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002479 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04002480 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04002481 struct btrfs_root *root = BTRFS_I(inode)->root;
2482 struct btrfs_key location;
Chris Mason46a53cc2009-04-27 11:47:50 -04002483 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04002484 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04002485 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00002486 bool filled = false;
2487
2488 ret = btrfs_fill_inode(inode, &rdev);
2489 if (!ret)
2490 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04002491
2492 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07002493 if (!path)
2494 goto make_bad;
2495
Josef Bacikd90c7322011-05-17 09:50:54 -04002496 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002497 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05002498
Chris Mason39279cc2007-06-12 06:35:45 -04002499 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002500 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04002501 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04002502
Chris Mason5f39d392007-10-15 16:14:19 -04002503 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00002504
2505 if (filled)
2506 goto cache_acl;
2507
Chris Mason5f39d392007-10-15 16:14:19 -04002508 inode_item = btrfs_item_ptr(leaf, path->slots[0],
2509 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002510 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02002511 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04002512 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
2513 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
Chris Masondbe674a2008-07-17 12:54:05 -04002514 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04002515
2516 tspec = btrfs_inode_atime(inode_item);
2517 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2518 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2519
2520 tspec = btrfs_inode_mtime(inode_item);
2521 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2522 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2523
2524 tspec = btrfs_inode_ctime(inode_item);
2525 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2526 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2527
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002528 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04002529 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Chris Masonc3027eb2008-12-08 16:40:21 -05002530 BTRFS_I(inode)->sequence = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04002531 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04002532 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002533 rdev = btrfs_inode_rdev(leaf, inode_item);
2534
Josef Bacikaec74772008-07-24 12:12:38 -04002535 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05002536 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie2f7e33d2011-06-23 07:27:13 +00002537cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04002538 /*
2539 * try to precache a NULL acl entry for files that don't have
2540 * any xattrs or acls
2541 */
Li Zefan33345d012011-04-20 10:31:50 +08002542 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
2543 btrfs_ino(inode));
Al Viro72c04902009-06-24 16:58:48 -04002544 if (!maybe_acls)
2545 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04002546
Chris Mason39279cc2007-06-12 06:35:45 -04002547 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002548
Chris Mason39279cc2007-06-12 06:35:45 -04002549 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04002550 case S_IFREG:
2551 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04002552 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05002553 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002554 inode->i_fop = &btrfs_file_operations;
2555 inode->i_op = &btrfs_file_inode_operations;
2556 break;
2557 case S_IFDIR:
2558 inode->i_fop = &btrfs_dir_file_operations;
2559 if (root == root->fs_info->tree_root)
2560 inode->i_op = &btrfs_dir_ro_inode_operations;
2561 else
2562 inode->i_op = &btrfs_dir_inode_operations;
2563 break;
2564 case S_IFLNK:
2565 inode->i_op = &btrfs_symlink_inode_operations;
2566 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04002567 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04002568 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04002569 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05002570 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04002571 init_special_inode(inode, inode->i_mode, rdev);
2572 break;
Chris Mason39279cc2007-06-12 06:35:45 -04002573 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002574
2575 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002576 return;
2577
2578make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04002579 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002580 make_bad_inode(inode);
2581}
2582
Chris Masond352ac62008-09-29 15:18:18 -04002583/*
2584 * given a leaf and an inode, copy the inode fields into the leaf
2585 */
Chris Masone02119d2008-09-05 16:13:11 -04002586static void fill_inode_item(struct btrfs_trans_handle *trans,
2587 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04002588 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04002589 struct inode *inode)
2590{
Chris Mason5f39d392007-10-15 16:14:19 -04002591 btrfs_set_inode_uid(leaf, item, inode->i_uid);
2592 btrfs_set_inode_gid(leaf, item, inode->i_gid);
Chris Masondbe674a2008-07-17 12:54:05 -04002593 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
Chris Mason5f39d392007-10-15 16:14:19 -04002594 btrfs_set_inode_mode(leaf, item, inode->i_mode);
2595 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
2596
2597 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
2598 inode->i_atime.tv_sec);
2599 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
2600 inode->i_atime.tv_nsec);
2601
2602 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
2603 inode->i_mtime.tv_sec);
2604 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
2605 inode->i_mtime.tv_nsec);
2606
2607 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
2608 inode->i_ctime.tv_sec);
2609 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
2610 inode->i_ctime.tv_nsec);
2611
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002612 btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode));
Chris Masone02119d2008-09-05 16:13:11 -04002613 btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
Chris Masonc3027eb2008-12-08 16:40:21 -05002614 btrfs_set_inode_sequence(leaf, item, BTRFS_I(inode)->sequence);
Chris Masone02119d2008-09-05 16:13:11 -04002615 btrfs_set_inode_transid(leaf, item, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002616 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -05002617 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Josef Bacikd82a6f12011-05-11 15:26:06 -04002618 btrfs_set_inode_block_group(leaf, item, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002619}
2620
Chris Masond352ac62008-09-29 15:18:18 -04002621/*
2622 * copy everything in the in-memory inode into the btree.
2623 */
Chris Mason21151332011-11-10 20:39:08 -05002624static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05002625 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002626{
2627 struct btrfs_inode_item *inode_item;
2628 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002629 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002630 int ret;
2631
2632 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08002633 if (!path)
2634 return -ENOMEM;
2635
Chris Masonb9473432009-03-13 11:00:37 -04002636 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08002637 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
2638 1);
Chris Mason39279cc2007-06-12 06:35:45 -04002639 if (ret) {
2640 if (ret > 0)
2641 ret = -ENOENT;
2642 goto failed;
2643 }
2644
Chris Masonb4ce94d2009-02-04 09:25:08 -05002645 btrfs_unlock_up_safe(path, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002646 leaf = path->nodes[0];
2647 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08002648 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04002649
Chris Masone02119d2008-09-05 16:13:11 -04002650 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002651 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002652 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002653 ret = 0;
2654failed:
Chris Mason39279cc2007-06-12 06:35:45 -04002655 btrfs_free_path(path);
2656 return ret;
2657}
2658
Chris Masond352ac62008-09-29 15:18:18 -04002659/*
Chris Mason21151332011-11-10 20:39:08 -05002660 * copy everything in the in-memory inode into the btree.
2661 */
2662noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
2663 struct btrfs_root *root, struct inode *inode)
2664{
2665 int ret;
2666
2667 /*
2668 * If the inode is a free space inode, we can deadlock during commit
2669 * if we put it into the delayed code.
2670 *
2671 * The data relocation inode should also be directly updated
2672 * without delay
2673 */
2674 if (!btrfs_is_free_space_inode(root, inode)
2675 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) {
2676 ret = btrfs_delayed_update_inode(trans, root, inode);
2677 if (!ret)
2678 btrfs_set_inode_last_trans(trans, inode);
2679 return ret;
2680 }
2681
2682 return btrfs_update_inode_item(trans, root, inode);
2683}
2684
2685static noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
2686 struct btrfs_root *root, struct inode *inode)
2687{
2688 int ret;
2689
2690 ret = btrfs_update_inode(trans, root, inode);
2691 if (ret == -ENOSPC)
2692 return btrfs_update_inode_item(trans, root, inode);
2693 return ret;
2694}
2695
2696/*
Chris Masond352ac62008-09-29 15:18:18 -04002697 * unlink helper that gets used here in inode.c and in the tree logging
2698 * recovery code. It remove a link in a directory with a given name, and
2699 * also drops the back refs in the inode to the directory
2700 */
Al Viro92986792011-03-04 17:14:37 +00002701static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2702 struct btrfs_root *root,
2703 struct inode *dir, struct inode *inode,
2704 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04002705{
2706 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04002707 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002708 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002709 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04002710 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04002711 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08002712 u64 ino = btrfs_ino(inode);
2713 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04002714
2715 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04002716 if (!path) {
2717 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00002718 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04002719 }
2720
Chris Masonb9473432009-03-13 11:00:37 -04002721 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08002722 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04002723 name, name_len, -1);
2724 if (IS_ERR(di)) {
2725 ret = PTR_ERR(di);
2726 goto err;
2727 }
2728 if (!di) {
2729 ret = -ENOENT;
2730 goto err;
2731 }
Chris Mason5f39d392007-10-15 16:14:19 -04002732 leaf = path->nodes[0];
2733 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04002734 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04002735 if (ret)
2736 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02002737 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002738
Li Zefan33345d012011-04-20 10:31:50 +08002739 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
2740 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04002741 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05002742 printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
Li Zefan33345d012011-04-20 10:31:50 +08002743 "inode %llu parent %llu\n", name_len, name,
2744 (unsigned long long)ino, (unsigned long long)dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002745 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04002746 goto err;
2747 }
2748
Miao Xie16cdcec2011-04-22 18:12:22 +08002749 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002750 if (ret) {
2751 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04002752 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002753 }
Chris Mason39279cc2007-06-12 06:35:45 -04002754
Chris Masone02119d2008-09-05 16:13:11 -04002755 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08002756 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002757 if (ret != 0 && ret != -ENOENT) {
2758 btrfs_abort_transaction(trans, root, ret);
2759 goto err;
2760 }
Chris Masone02119d2008-09-05 16:13:11 -04002761
2762 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
2763 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04002764 if (ret == -ENOENT)
2765 ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002766err:
2767 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002768 if (ret)
2769 goto out;
2770
2771 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
2772 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2773 btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04002774out:
Chris Mason39279cc2007-06-12 06:35:45 -04002775 return ret;
2776}
2777
Al Viro92986792011-03-04 17:14:37 +00002778int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2779 struct btrfs_root *root,
2780 struct inode *dir, struct inode *inode,
2781 const char *name, int name_len)
2782{
2783 int ret;
2784 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
2785 if (!ret) {
2786 btrfs_drop_nlink(inode);
2787 ret = btrfs_update_inode(trans, root, inode);
2788 }
2789 return ret;
2790}
2791
2792
Yan, Zhenga22285a2010-05-16 10:48:46 -04002793/* helper to check if there is any shared block in the path */
2794static int check_path_shared(struct btrfs_root *root,
2795 struct btrfs_path *path)
2796{
2797 struct extent_buffer *eb;
2798 int level;
Dan Carpenter0e4dcbe2010-06-01 08:23:11 +00002799 u64 refs = 1;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002800
2801 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
Josef Bacikdedefd72011-01-24 21:43:18 +00002802 int ret;
2803
Yan, Zhenga22285a2010-05-16 10:48:46 -04002804 if (!path->nodes[level])
2805 break;
2806 eb = path->nodes[level];
2807 if (!btrfs_block_can_be_shared(root, eb))
2808 continue;
2809 ret = btrfs_lookup_extent_info(NULL, root, eb->start, eb->len,
2810 &refs, NULL);
2811 if (refs > 1)
2812 return 1;
2813 }
Josef Bacikdedefd72011-01-24 21:43:18 +00002814 return 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002815}
2816
2817/*
2818 * helper to start transaction for unlink and rmdir.
2819 *
2820 * unlink and rmdir are special in btrfs, they do not always free space.
2821 * so in enospc case, we should make sure they will free space before
2822 * allowing them to use the global metadata reservation.
2823 */
2824static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
2825 struct dentry *dentry)
2826{
2827 struct btrfs_trans_handle *trans;
2828 struct btrfs_root *root = BTRFS_I(dir)->root;
2829 struct btrfs_path *path;
2830 struct btrfs_inode_ref *ref;
2831 struct btrfs_dir_item *di;
2832 struct inode *inode = dentry->d_inode;
2833 u64 index;
2834 int check_link = 1;
2835 int err = -ENOSPC;
2836 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08002837 u64 ino = btrfs_ino(inode);
2838 u64 dir_ino = btrfs_ino(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002839
Josef Bacike70bea52011-10-11 14:18:24 -04002840 /*
2841 * 1 for the possible orphan item
2842 * 1 for the dir item
2843 * 1 for the dir index
2844 * 1 for the inode ref
2845 * 1 for the inode ref in the tree log
2846 * 2 for the dir entries in the log
2847 * 1 for the inode
2848 */
2849 trans = btrfs_start_transaction(root, 8);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002850 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
2851 return trans;
2852
Li Zefan33345d012011-04-20 10:31:50 +08002853 if (ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhenga22285a2010-05-16 10:48:46 -04002854 return ERR_PTR(-ENOSPC);
2855
2856 /* check if there is someone else holds reference */
2857 if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1)
2858 return ERR_PTR(-ENOSPC);
2859
2860 if (atomic_read(&inode->i_count) > 2)
2861 return ERR_PTR(-ENOSPC);
2862
2863 if (xchg(&root->fs_info->enospc_unlink, 1))
2864 return ERR_PTR(-ENOSPC);
2865
2866 path = btrfs_alloc_path();
2867 if (!path) {
2868 root->fs_info->enospc_unlink = 0;
2869 return ERR_PTR(-ENOMEM);
2870 }
2871
Josef Bacik3880a1b2011-10-14 14:46:51 -04002872 /* 1 for the orphan item */
2873 trans = btrfs_start_transaction(root, 1);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002874 if (IS_ERR(trans)) {
2875 btrfs_free_path(path);
2876 root->fs_info->enospc_unlink = 0;
2877 return trans;
2878 }
2879
2880 path->skip_locking = 1;
2881 path->search_commit_root = 1;
2882
2883 ret = btrfs_lookup_inode(trans, root, path,
2884 &BTRFS_I(dir)->location, 0);
2885 if (ret < 0) {
2886 err = ret;
2887 goto out;
2888 }
2889 if (ret == 0) {
2890 if (check_path_shared(root, path))
2891 goto out;
2892 } else {
2893 check_link = 0;
2894 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002895 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002896
2897 ret = btrfs_lookup_inode(trans, root, path,
2898 &BTRFS_I(inode)->location, 0);
2899 if (ret < 0) {
2900 err = ret;
2901 goto out;
2902 }
2903 if (ret == 0) {
2904 if (check_path_shared(root, path))
2905 goto out;
2906 } else {
2907 check_link = 0;
2908 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002909 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002910
2911 if (ret == 0 && S_ISREG(inode->i_mode)) {
2912 ret = btrfs_lookup_file_extent(trans, root, path,
Li Zefan33345d012011-04-20 10:31:50 +08002913 ino, (u64)-1, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002914 if (ret < 0) {
2915 err = ret;
2916 goto out;
2917 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002918 BUG_ON(ret == 0); /* Corruption */
Yan, Zhenga22285a2010-05-16 10:48:46 -04002919 if (check_path_shared(root, path))
2920 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +02002921 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002922 }
2923
2924 if (!check_link) {
2925 err = 0;
2926 goto out;
2927 }
2928
Li Zefan33345d012011-04-20 10:31:50 +08002929 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zhenga22285a2010-05-16 10:48:46 -04002930 dentry->d_name.name, dentry->d_name.len, 0);
2931 if (IS_ERR(di)) {
2932 err = PTR_ERR(di);
2933 goto out;
2934 }
2935 if (di) {
2936 if (check_path_shared(root, path))
2937 goto out;
2938 } else {
2939 err = 0;
2940 goto out;
2941 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002942 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002943
2944 ref = btrfs_lookup_inode_ref(trans, root, path,
2945 dentry->d_name.name, dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08002946 ino, dir_ino, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002947 if (IS_ERR(ref)) {
2948 err = PTR_ERR(ref);
2949 goto out;
2950 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002951 BUG_ON(!ref); /* Logic error */
Yan, Zhenga22285a2010-05-16 10:48:46 -04002952 if (check_path_shared(root, path))
2953 goto out;
2954 index = btrfs_inode_ref_index(path->nodes[0], ref);
David Sterbab3b4aa72011-04-21 01:20:15 +02002955 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002956
Miao Xie16cdcec2011-04-22 18:12:22 +08002957 /*
2958 * This is a commit root search, if we can lookup inode item and other
2959 * relative items in the commit root, it means the transaction of
2960 * dir/file creation has been committed, and the dir index item that we
2961 * delay to insert has also been inserted into the commit root. So
2962 * we needn't worry about the delayed insertion of the dir index item
2963 * here.
2964 */
Li Zefan33345d012011-04-20 10:31:50 +08002965 di = btrfs_lookup_dir_index_item(trans, root, path, dir_ino, index,
Yan, Zhenga22285a2010-05-16 10:48:46 -04002966 dentry->d_name.name, dentry->d_name.len, 0);
2967 if (IS_ERR(di)) {
2968 err = PTR_ERR(di);
2969 goto out;
2970 }
2971 BUG_ON(ret == -ENOENT);
2972 if (check_path_shared(root, path))
2973 goto out;
2974
2975 err = 0;
2976out:
2977 btrfs_free_path(path);
Josef Bacik3880a1b2011-10-14 14:46:51 -04002978 /* Migrate the orphan reservation over */
2979 if (!err)
2980 err = btrfs_block_rsv_migrate(trans->block_rsv,
2981 &root->fs_info->global_block_rsv,
Josef Bacik5a77d762011-11-01 14:32:23 -04002982 trans->bytes_reserved);
Josef Bacik3880a1b2011-10-14 14:46:51 -04002983
Yan, Zhenga22285a2010-05-16 10:48:46 -04002984 if (err) {
2985 btrfs_end_transaction(trans, root);
2986 root->fs_info->enospc_unlink = 0;
2987 return ERR_PTR(err);
2988 }
2989
2990 trans->block_rsv = &root->fs_info->global_block_rsv;
2991 return trans;
2992}
2993
2994static void __unlink_end_trans(struct btrfs_trans_handle *trans,
2995 struct btrfs_root *root)
2996{
2997 if (trans->block_rsv == &root->fs_info->global_block_rsv) {
Josef Bacik5a77d762011-11-01 14:32:23 -04002998 btrfs_block_rsv_release(root, trans->block_rsv,
2999 trans->bytes_reserved);
3000 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003001 BUG_ON(!root->fs_info->enospc_unlink);
3002 root->fs_info->enospc_unlink = 0;
3003 }
Josef Bacik7ad85bb2012-01-12 19:10:12 -05003004 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003005}
3006
Chris Mason39279cc2007-06-12 06:35:45 -04003007static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3008{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003009 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003010 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04003011 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003012 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -05003013 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003014
Yan, Zhenga22285a2010-05-16 10:48:46 -04003015 trans = __unlink_start_trans(dir, dentry);
3016 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003017 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003018
Chris Mason12fcfd22009-03-24 10:24:20 -04003019 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
3020
Chris Masone02119d2008-09-05 16:13:11 -04003021 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3022 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003023 if (ret)
3024 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003025
Yan, Zhenga22285a2010-05-16 10:48:46 -04003026 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04003027 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003028 if (ret)
3029 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003030 }
Josef Bacik7b128762008-07-24 12:17:14 -04003031
Tsutomu Itohb5324022011-07-19 07:27:20 +00003032out:
Chris Masond3c2fdc2007-09-17 10:58:06 -04003033 nr = trans->blocks_used;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003034 __unlink_end_trans(trans, root);
Chris Masond3c2fdc2007-09-17 10:58:06 -04003035 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003036 return ret;
3037}
3038
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003039int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3040 struct btrfs_root *root,
3041 struct inode *dir, u64 objectid,
3042 const char *name, int name_len)
3043{
3044 struct btrfs_path *path;
3045 struct extent_buffer *leaf;
3046 struct btrfs_dir_item *di;
3047 struct btrfs_key key;
3048 u64 index;
3049 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003050 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003051
3052 path = btrfs_alloc_path();
3053 if (!path)
3054 return -ENOMEM;
3055
Li Zefan33345d012011-04-20 10:31:50 +08003056 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003057 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003058 if (IS_ERR_OR_NULL(di)) {
3059 if (!di)
3060 ret = -ENOENT;
3061 else
3062 ret = PTR_ERR(di);
3063 goto out;
3064 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003065
3066 leaf = path->nodes[0];
3067 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3068 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3069 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003070 if (ret) {
3071 btrfs_abort_transaction(trans, root, ret);
3072 goto out;
3073 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003074 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003075
3076 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3077 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003078 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003079 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003080 if (ret != -ENOENT) {
3081 btrfs_abort_transaction(trans, root, ret);
3082 goto out;
3083 }
Li Zefan33345d012011-04-20 10:31:50 +08003084 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003085 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003086 if (IS_ERR_OR_NULL(di)) {
3087 if (!di)
3088 ret = -ENOENT;
3089 else
3090 ret = PTR_ERR(di);
3091 btrfs_abort_transaction(trans, root, ret);
3092 goto out;
3093 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003094
3095 leaf = path->nodes[0];
3096 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003097 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003098 index = key.offset;
3099 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003100 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003101
Miao Xie16cdcec2011-04-22 18:12:22 +08003102 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003103 if (ret) {
3104 btrfs_abort_transaction(trans, root, ret);
3105 goto out;
3106 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003107
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003108 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
3109 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
3110 ret = btrfs_update_inode(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003111 if (ret)
3112 btrfs_abort_transaction(trans, root, ret);
3113out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04003114 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003115 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003116}
3117
Chris Mason39279cc2007-06-12 06:35:45 -04003118static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3119{
3120 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003121 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003122 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003123 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -05003124 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003125
Chris Mason3394e162008-11-17 20:42:26 -05003126 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE ||
Li Zefan33345d012011-04-20 10:31:50 +08003127 btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
Yan134d4512007-10-25 15:49:25 -04003128 return -ENOTEMPTY;
3129
Yan, Zhenga22285a2010-05-16 10:48:46 -04003130 trans = __unlink_start_trans(dir, dentry);
3131 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003132 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003133
Li Zefan33345d012011-04-20 10:31:50 +08003134 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003135 err = btrfs_unlink_subvol(trans, root, dir,
3136 BTRFS_I(inode)->location.objectid,
3137 dentry->d_name.name,
3138 dentry->d_name.len);
3139 goto out;
3140 }
3141
Josef Bacik7b128762008-07-24 12:17:14 -04003142 err = btrfs_orphan_add(trans, inode);
3143 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003144 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003145
Chris Mason39279cc2007-06-12 06:35:45 -04003146 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04003147 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3148 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05003149 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04003150 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003151out:
Chris Masond3c2fdc2007-09-17 10:58:06 -04003152 nr = trans->blocks_used;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003153 __unlink_end_trans(trans, root);
Chris Masond3c2fdc2007-09-17 10:58:06 -04003154 btrfs_btree_balance_dirty(root, nr);
Chris Mason39544012007-12-12 14:38:19 -05003155
Chris Mason39279cc2007-06-12 06:35:45 -04003156 return err;
3157}
3158
Chris Mason323ac952008-10-01 19:05:46 -04003159/*
Chris Mason39279cc2007-06-12 06:35:45 -04003160 * this can truncate away extent items, csum items and directory items.
3161 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04003162 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04003163 *
3164 * csum items that cross the new i_size are truncated to the new size
3165 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04003166 *
3167 * min_type is the minimum key type to truncate down to. If set to 0, this
3168 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04003169 */
Yan, Zheng80825102009-11-12 09:35:36 +00003170int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
3171 struct btrfs_root *root,
3172 struct inode *inode,
3173 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003174{
Chris Mason39279cc2007-06-12 06:35:45 -04003175 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003176 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003177 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00003178 struct btrfs_key key;
3179 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04003180 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04003181 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003182 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003183 u64 item_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00003184 u64 mask = root->sectorsize - 1;
3185 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04003186 int found_extent;
3187 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003188 int pending_del_nr = 0;
3189 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04003190 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00003191 int ret;
3192 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08003193 u64 ino = btrfs_ino(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003194
3195 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04003196
Mark Fasheh0eb0e192011-07-12 16:44:10 -07003197 path = btrfs_alloc_path();
3198 if (!path)
3199 return -ENOMEM;
3200 path->reada = -1;
3201
Josef Bacik0af3d002010-06-21 14:48:16 -04003202 if (root->ref_cows || root == root->fs_info->tree_root)
Zheng Yan5b21f2e2008-09-26 10:05:38 -04003203 btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003204
Miao Xie16cdcec2011-04-22 18:12:22 +08003205 /*
3206 * This function is also used to drop the items in the log tree before
3207 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
3208 * it is used to drop the loged items. So we shouldn't kill the delayed
3209 * items.
3210 */
3211 if (min_type == 0 && root == BTRFS_I(inode)->root)
3212 btrfs_kill_delayed_inode_items(inode);
3213
Li Zefan33345d012011-04-20 10:31:50 +08003214 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04003215 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04003216 key.type = (u8)-1;
3217
Chris Mason85e21ba2008-01-29 15:11:36 -05003218search_again:
Chris Masonb9473432009-03-13 11:00:37 -04003219 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05003220 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00003221 if (ret < 0) {
3222 err = ret;
3223 goto out;
3224 }
Chris Masond3977122009-01-05 21:25:51 -05003225
Chris Mason85e21ba2008-01-29 15:11:36 -05003226 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003227 /* there are no items in the tree for us to truncate, we're
3228 * done
3229 */
Yan, Zheng80825102009-11-12 09:35:36 +00003230 if (path->slots[0] == 0)
3231 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05003232 path->slots[0]--;
3233 }
3234
Chris Masond3977122009-01-05 21:25:51 -05003235 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04003236 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04003237 leaf = path->nodes[0];
3238 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3239 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04003240
Li Zefan33345d012011-04-20 10:31:50 +08003241 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04003242 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003243
Chris Mason85e21ba2008-01-29 15:11:36 -05003244 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003245 break;
3246
Chris Mason5f39d392007-10-15 16:14:19 -04003247 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04003248 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04003249 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04003250 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04003251 extent_type = btrfs_file_extent_type(leaf, fi);
3252 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04003253 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04003254 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04003255 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04003256 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04003257 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003258 }
Yan008630c2007-11-07 13:31:09 -05003259 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04003260 }
Yan, Zheng80825102009-11-12 09:35:36 +00003261 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04003262 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00003263 } else {
3264 if (item_end < new_size)
3265 break;
3266 if (found_key.offset >= new_size)
3267 del_item = 1;
3268 else
3269 del_item = 0;
3270 }
Chris Mason39279cc2007-06-12 06:35:45 -04003271 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003272 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04003273 if (found_type != BTRFS_EXTENT_DATA_KEY)
3274 goto delete;
3275
3276 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04003277 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04003278 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a62012-01-12 19:10:12 -05003279 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04003280 u64 orig_num_bytes =
3281 btrfs_file_extent_num_bytes(leaf, fi);
Chris Masone02119d2008-09-05 16:13:11 -04003282 extent_num_bytes = new_size -
Chris Mason5f39d392007-10-15 16:14:19 -04003283 found_key.offset + root->sectorsize - 1;
Yanb1632b12008-01-30 11:54:04 -05003284 extent_num_bytes = extent_num_bytes &
3285 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04003286 btrfs_set_file_extent_num_bytes(leaf, fi,
3287 extent_num_bytes);
3288 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05003289 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04003290 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003291 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04003292 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003293 } else {
Chris Masondb945352007-10-15 16:15:53 -04003294 extent_num_bytes =
3295 btrfs_file_extent_disk_num_bytes(leaf,
3296 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003297 extent_offset = found_key.offset -
3298 btrfs_file_extent_offset(leaf, fi);
3299
Chris Mason39279cc2007-06-12 06:35:45 -04003300 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05003301 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003302 if (extent_start != 0) {
3303 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04003304 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003305 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04003306 }
3307 }
Chris Mason90692182008-02-08 13:49:28 -05003308 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04003309 /*
3310 * we can't truncate inline items that have had
3311 * special encodings
3312 */
3313 if (!del_item &&
3314 btrfs_file_extent_compression(leaf, fi) == 0 &&
3315 btrfs_file_extent_encryption(leaf, fi) == 0 &&
3316 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003317 u32 size = new_size - found_key.offset;
3318
3319 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003320 inode_sub_bytes(inode, item_end + 1 -
3321 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04003322 }
3323 size =
3324 btrfs_file_extent_calc_inline_size(size);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003325 btrfs_truncate_item(trans, root, path,
3326 size, 1);
Chris Masone02119d2008-09-05 16:13:11 -04003327 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003328 inode_sub_bytes(inode, item_end + 1 -
3329 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05003330 }
Chris Mason39279cc2007-06-12 06:35:45 -04003331 }
Chris Mason179e29e2007-11-01 11:28:41 -04003332delete:
Chris Mason39279cc2007-06-12 06:35:45 -04003333 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05003334 if (!pending_del_nr) {
3335 /* no pending yet, add ourselves */
3336 pending_del_slot = path->slots[0];
3337 pending_del_nr = 1;
3338 } else if (pending_del_nr &&
3339 path->slots[0] + 1 == pending_del_slot) {
3340 /* hop on the pending chunk */
3341 pending_del_nr++;
3342 pending_del_slot = path->slots[0];
3343 } else {
Chris Masond3977122009-01-05 21:25:51 -05003344 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05003345 }
Chris Mason39279cc2007-06-12 06:35:45 -04003346 } else {
3347 break;
3348 }
Josef Bacik0af3d002010-06-21 14:48:16 -04003349 if (found_extent && (root->ref_cows ||
3350 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04003351 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003352 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003353 extent_num_bytes, 0,
3354 btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003355 ino, extent_offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003356 BUG_ON(ret);
3357 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003358
Yan, Zheng80825102009-11-12 09:35:36 +00003359 if (found_type == BTRFS_INODE_ITEM_KEY)
3360 break;
3361
3362 if (path->slots[0] == 0 ||
3363 path->slots[0] != pending_del_slot) {
Li Zefan82d59022011-04-20 10:33:24 +08003364 if (root->ref_cows &&
3365 BTRFS_I(inode)->location.objectid !=
3366 BTRFS_FREE_INO_OBJECTID) {
Yan, Zheng80825102009-11-12 09:35:36 +00003367 err = -EAGAIN;
3368 goto out;
3369 }
3370 if (pending_del_nr) {
3371 ret = btrfs_del_items(trans, root, path,
3372 pending_del_slot,
3373 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003374 if (ret) {
3375 btrfs_abort_transaction(trans,
3376 root, ret);
3377 goto error;
3378 }
Yan, Zheng80825102009-11-12 09:35:36 +00003379 pending_del_nr = 0;
3380 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003381 btrfs_release_path(path);
Chris Mason85e21ba2008-01-29 15:11:36 -05003382 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00003383 } else {
3384 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05003385 }
Chris Mason39279cc2007-06-12 06:35:45 -04003386 }
Yan, Zheng80825102009-11-12 09:35:36 +00003387out:
Chris Mason85e21ba2008-01-29 15:11:36 -05003388 if (pending_del_nr) {
3389 ret = btrfs_del_items(trans, root, path, pending_del_slot,
3390 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003391 if (ret)
3392 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05003393 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003394error:
Chris Mason39279cc2007-06-12 06:35:45 -04003395 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00003396 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003397}
3398
Chris Masona52d9a82007-08-27 16:49:44 -04003399/*
3400 * taken from block_truncate_page, but does cow as it zeros out
3401 * any bytes left in the last page in the file.
3402 */
3403static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
3404{
3405 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -04003406 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003407 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3408 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003409 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003410 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04003411 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04003412 pgoff_t index = from >> PAGE_CACHE_SHIFT;
3413 unsigned offset = from & (PAGE_CACHE_SIZE-1);
3414 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04003415 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04003416 int ret = 0;
3417 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003418 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04003419
3420 if ((offset & (blocksize - 1)) == 0)
3421 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003422 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003423 if (ret)
3424 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04003425
3426 ret = -ENOMEM;
Chris Mason211c17f2008-05-15 09:13:45 -04003427again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04003428 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003429 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003430 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Masona52d9a82007-08-27 16:49:44 -04003431 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04003432 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003433
3434 page_start = page_offset(page);
3435 page_end = page_start + PAGE_CACHE_SIZE - 1;
3436
Chris Masona52d9a82007-08-27 16:49:44 -04003437 if (!PageUptodate(page)) {
3438 ret = btrfs_readpage(NULL, page);
3439 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04003440 if (page->mapping != mapping) {
3441 unlock_page(page);
3442 page_cache_release(page);
3443 goto again;
3444 }
Chris Masona52d9a82007-08-27 16:49:44 -04003445 if (!PageUptodate(page)) {
3446 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04003447 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04003448 }
3449 }
Chris Mason211c17f2008-05-15 09:13:45 -04003450 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003451
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003452 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003453 set_page_extent_mapped(page);
3454
3455 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3456 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00003457 unlock_extent_cached(io_tree, page_start, page_end,
3458 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003459 unlock_page(page);
3460 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04003461 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003462 btrfs_put_ordered_extent(ordered);
3463 goto again;
3464 }
3465
Josef Bacik2ac55d42010-02-03 19:33:23 +00003466 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Josef Bacik5d5e1032009-10-13 16:46:49 -04003467 EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003468 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003469
Josef Bacik2ac55d42010-02-03 19:33:23 +00003470 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
3471 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003472 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00003473 unlock_extent_cached(io_tree, page_start, page_end,
3474 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003475 goto out_unlock;
3476 }
3477
Chris Masone6dcd2d2008-07-17 12:53:50 -04003478 ret = 0;
3479 if (offset != PAGE_CACHE_SIZE) {
3480 kaddr = kmap(page);
3481 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
3482 flush_dcache_page(page);
3483 kunmap(page);
3484 }
Chris Mason247e7432008-07-17 12:53:51 -04003485 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003486 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003487 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
3488 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04003489
Chris Mason89642222008-07-24 09:41:53 -04003490out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04003491 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003492 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04003493 unlock_page(page);
3494 page_cache_release(page);
3495out:
3496 return ret;
3497}
3498
Josef Bacik695a0d02011-03-04 15:46:53 -05003499/*
3500 * This function puts in dummy file extents for the area we're creating a hole
3501 * for. So if we are truncating this file to a larger size we need to insert
3502 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
3503 * the range between oldsize and size
3504 */
Josef Bacika41ad392011-01-31 15:30:16 -05003505int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04003506{
3507 struct btrfs_trans_handle *trans;
3508 struct btrfs_root *root = BTRFS_I(inode)->root;
3509 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003510 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003511 struct extent_state *cached_state = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04003512 u64 mask = root->sectorsize - 1;
Josef Bacika41ad392011-01-31 15:30:16 -05003513 u64 hole_start = (oldsize + mask) & ~mask;
Yan Zheng9036c102008-10-30 14:19:41 -04003514 u64 block_end = (size + mask) & ~mask;
3515 u64 last_byte;
3516 u64 cur_offset;
3517 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003518 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04003519
3520 if (size <= hole_start)
3521 return 0;
3522
Yan Zheng9036c102008-10-30 14:19:41 -04003523 while (1) {
3524 struct btrfs_ordered_extent *ordered;
3525 btrfs_wait_ordered_range(inode, hole_start,
3526 block_end - hole_start);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003527 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003528 &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04003529 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
3530 if (!ordered)
3531 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003532 unlock_extent_cached(io_tree, hole_start, block_end - 1,
3533 &cached_state, GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003534 btrfs_put_ordered_extent(ordered);
3535 }
3536
Yan Zheng9036c102008-10-30 14:19:41 -04003537 cur_offset = hole_start;
3538 while (1) {
3539 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
3540 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003541 if (IS_ERR(em)) {
3542 err = PTR_ERR(em);
3543 break;
3544 }
Yan Zheng9036c102008-10-30 14:19:41 -04003545 last_byte = min(extent_map_end(em), block_end);
3546 last_byte = (last_byte + mask) & ~mask;
Yan, Zheng80825102009-11-12 09:35:36 +00003547 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Chris Mason771ed682008-11-06 22:02:51 -05003548 u64 hint_byte = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04003549 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00003550
Miao Xie36423202011-12-14 20:12:02 -05003551 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003552 if (IS_ERR(trans)) {
3553 err = PTR_ERR(trans);
Chris Mason771ed682008-11-06 22:02:51 -05003554 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003555 }
Yan, Zheng80825102009-11-12 09:35:36 +00003556
3557 err = btrfs_drop_extents(trans, inode, cur_offset,
3558 cur_offset + hole_size,
3559 &hint_byte, 1);
Miao Xie5b397372011-09-11 10:52:24 -04003560 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003561 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04003562 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05003563 break;
Miao Xie5b397372011-09-11 10:52:24 -04003564 }
Josef Bacik9ed74f22009-09-11 16:12:44 -04003565
Yan Zheng9036c102008-10-30 14:19:41 -04003566 err = btrfs_insert_file_extent(trans, root,
Li Zefan33345d012011-04-20 10:31:50 +08003567 btrfs_ino(inode), cur_offset, 0,
Yan Zheng9036c102008-10-30 14:19:41 -04003568 0, hole_size, 0, hole_size,
3569 0, 0, 0);
Miao Xie5b397372011-09-11 10:52:24 -04003570 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003571 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04003572 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05003573 break;
Miao Xie5b397372011-09-11 10:52:24 -04003574 }
Yan, Zheng80825102009-11-12 09:35:36 +00003575
Yan Zheng9036c102008-10-30 14:19:41 -04003576 btrfs_drop_extent_cache(inode, hole_start,
3577 last_byte - 1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003578
Miao Xie36423202011-12-14 20:12:02 -05003579 btrfs_update_inode(trans, root, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003580 btrfs_end_transaction(trans, root);
Yan Zheng9036c102008-10-30 14:19:41 -04003581 }
3582 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003583 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04003584 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00003585 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04003586 break;
3587 }
3588
Yan, Zhenga22285a2010-05-16 10:48:46 -04003589 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003590 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
3591 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003592 return err;
3593}
3594
Josef Bacika41ad392011-01-31 15:30:16 -05003595static int btrfs_setsize(struct inode *inode, loff_t newsize)
Yan, Zheng80825102009-11-12 09:35:36 +00003596{
Miao Xief4a2f4c2011-12-14 20:12:01 -05003597 struct btrfs_root *root = BTRFS_I(inode)->root;
3598 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05003599 loff_t oldsize = i_size_read(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003600 int ret;
3601
Josef Bacika41ad392011-01-31 15:30:16 -05003602 if (newsize == oldsize)
Yan, Zheng80825102009-11-12 09:35:36 +00003603 return 0;
3604
Josef Bacika41ad392011-01-31 15:30:16 -05003605 if (newsize > oldsize) {
Josef Bacika41ad392011-01-31 15:30:16 -05003606 truncate_pagecache(inode, oldsize, newsize);
3607 ret = btrfs_cont_expand(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05003608 if (ret)
Yan, Zheng80825102009-11-12 09:35:36 +00003609 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00003610
Miao Xief4a2f4c2011-12-14 20:12:01 -05003611 trans = btrfs_start_transaction(root, 1);
3612 if (IS_ERR(trans))
3613 return PTR_ERR(trans);
3614
3615 i_size_write(inode, newsize);
3616 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
3617 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05003618 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05003619 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00003620
Josef Bacika41ad392011-01-31 15:30:16 -05003621 /*
3622 * We're truncating a file that used to have good data down to
3623 * zero. Make sure it gets into the ordered flush list so that
3624 * any new writes get down to disk quickly.
3625 */
3626 if (newsize == 0)
3627 BTRFS_I(inode)->ordered_data_close = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00003628
Josef Bacika41ad392011-01-31 15:30:16 -05003629 /* we don't support swapfiles, so vmtruncate shouldn't fail */
3630 truncate_setsize(inode, newsize);
3631 ret = btrfs_truncate(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003632 }
3633
Josef Bacika41ad392011-01-31 15:30:16 -05003634 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00003635}
3636
Chris Mason39279cc2007-06-12 06:35:45 -04003637static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
3638{
3639 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08003640 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003641 int err;
3642
Li Zefanb83cc962010-12-20 16:04:08 +08003643 if (btrfs_root_readonly(root))
3644 return -EROFS;
3645
Chris Mason39279cc2007-06-12 06:35:45 -04003646 err = inode_change_ok(inode, attr);
3647 if (err)
3648 return err;
3649
Chris Mason5a3f23d2009-03-31 13:27:11 -04003650 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Josef Bacika41ad392011-01-31 15:30:16 -05003651 err = btrfs_setsize(inode, attr->ia_size);
Yan, Zheng80825102009-11-12 09:35:36 +00003652 if (err)
3653 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003654 }
Yan Zheng9036c102008-10-30 14:19:41 -04003655
Christoph Hellwig10257742010-06-04 11:30:02 +02003656 if (attr->ia_valid) {
3657 setattr_copy(inode, attr);
Josef Bacik22c44fe2011-11-30 10:45:38 -05003658 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04003659
Josef Bacik22c44fe2011-11-30 10:45:38 -05003660 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig10257742010-06-04 11:30:02 +02003661 err = btrfs_acl_chmod(inode);
3662 }
3663
Chris Mason39279cc2007-06-12 06:35:45 -04003664 return err;
3665}
Chris Mason61295eb2008-01-14 16:24:38 -05003666
Al Virobd555972010-06-07 11:35:40 -04003667void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003668{
3669 struct btrfs_trans_handle *trans;
3670 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04003671 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik07127182011-08-19 10:29:59 -04003672 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Masond3c2fdc2007-09-17 10:58:06 -04003673 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04003674 int ret;
3675
liubo1abe9b82011-03-24 11:18:59 +00003676 trace_btrfs_inode_evict(inode);
3677
Chris Mason39279cc2007-06-12 06:35:45 -04003678 truncate_inode_pages(&inode->i_data, 0);
Josef Bacik0af3d002010-06-21 14:48:16 -04003679 if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 ||
Chris Mason2cf85722011-07-26 15:35:09 -04003680 btrfs_is_free_space_inode(root, inode)))
Al Virobd555972010-06-07 11:35:40 -04003681 goto no_delete;
3682
Chris Mason39279cc2007-06-12 06:35:45 -04003683 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04003684 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003685 goto no_delete;
3686 }
Al Virobd555972010-06-07 11:35:40 -04003687 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04003688 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04003689
Yan, Zhengc71bf092009-11-12 09:34:40 +00003690 if (root->fs_info->log_root_recovering) {
3691 BUG_ON(!list_empty(&BTRFS_I(inode)->i_orphan));
3692 goto no_delete;
3693 }
3694
Yan, Zheng76dda932009-09-21 16:00:26 -04003695 if (inode->i_nlink > 0) {
3696 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
3697 goto no_delete;
3698 }
3699
Josef Bacik4289a662011-08-05 13:22:24 -04003700 rsv = btrfs_alloc_block_rsv(root);
3701 if (!rsv) {
3702 btrfs_orphan_del(NULL, inode);
3703 goto no_delete;
3704 }
Josef Bacik4a338542011-08-29 11:01:31 -04003705 rsv->size = min_size;
Josef Bacik726c35f2011-09-26 15:46:06 -04003706 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04003707
Chris Masondbe674a2008-07-17 12:54:05 -04003708 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003709
Josef Bacik4289a662011-08-05 13:22:24 -04003710 /*
3711 * This is a bit simpler than btrfs_truncate since
3712 *
3713 * 1) We've already reserved our space for our orphan item in the
3714 * unlink.
3715 * 2) We're going to delete the inode item, so we don't need to update
3716 * it at all.
3717 *
3718 * So we just need to reserve some slack space in case we add bytes when
3719 * doing the truncate.
3720 */
Yan, Zheng80825102009-11-12 09:35:36 +00003721 while (1) {
Miao Xieaa38a712011-11-18 17:43:00 +08003722 ret = btrfs_block_rsv_refill_noflush(root, rsv, min_size);
Yan, Zheng80825102009-11-12 09:35:36 +00003723
Josef Bacik726c35f2011-09-26 15:46:06 -04003724 /*
3725 * Try and steal from the global reserve since we will
3726 * likely not use this space anyway, we want to try as
3727 * hard as possible to get this to work.
3728 */
3729 if (ret)
3730 ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
3731
Yan, Zhengd68fc572010-05-16 10:49:58 -04003732 if (ret) {
Josef Bacik4289a662011-08-05 13:22:24 -04003733 printk(KERN_WARNING "Could not get space for a "
Josef Bacik482e6dc2011-08-19 10:31:56 -04003734 "delete, will truncate on mount %d\n", ret);
Josef Bacik4289a662011-08-05 13:22:24 -04003735 btrfs_orphan_del(NULL, inode);
3736 btrfs_free_block_rsv(root, rsv);
3737 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003738 }
3739
Josef Bacik4289a662011-08-05 13:22:24 -04003740 trans = btrfs_start_transaction(root, 0);
3741 if (IS_ERR(trans)) {
3742 btrfs_orphan_del(NULL, inode);
3743 btrfs_free_block_rsv(root, rsv);
3744 goto no_delete;
3745 }
3746
3747 trans->block_rsv = rsv;
3748
Yan, Zhengd68fc572010-05-16 10:49:58 -04003749 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003750 if (ret != -EAGAIN)
3751 break;
3752
3753 nr = trans->blocks_used;
3754 btrfs_end_transaction(trans, root);
3755 trans = NULL;
3756 btrfs_btree_balance_dirty(root, nr);
Josef Bacik7b128762008-07-24 12:17:14 -04003757 }
3758
Josef Bacik4289a662011-08-05 13:22:24 -04003759 btrfs_free_block_rsv(root, rsv);
3760
Yan, Zheng80825102009-11-12 09:35:36 +00003761 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04003762 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00003763 ret = btrfs_orphan_del(trans, inode);
3764 BUG_ON(ret);
3765 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003766
Josef Bacik4289a662011-08-05 13:22:24 -04003767 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08003768 if (!(root == root->fs_info->tree_root ||
3769 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08003770 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08003771
Chris Masond3c2fdc2007-09-17 10:58:06 -04003772 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04003773 btrfs_end_transaction(trans, root);
Chris Masond3c2fdc2007-09-17 10:58:06 -04003774 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003775no_delete:
Al Virobd555972010-06-07 11:35:40 -04003776 end_writeback(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003777 return;
Chris Mason39279cc2007-06-12 06:35:45 -04003778}
3779
3780/*
3781 * this returns the key found in the dir entry in the location pointer.
3782 * If no dir entries were found, location->objectid is 0.
3783 */
3784static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
3785 struct btrfs_key *location)
3786{
3787 const char *name = dentry->d_name.name;
3788 int namelen = dentry->d_name.len;
3789 struct btrfs_dir_item *di;
3790 struct btrfs_path *path;
3791 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04003792 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003793
3794 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07003795 if (!path)
3796 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05003797
Li Zefan33345d012011-04-20 10:31:50 +08003798 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04003799 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04003800 if (IS_ERR(di))
3801 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05003802
David Sterbac7040052011-04-19 18:00:01 +02003803 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05003804 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05003805
Chris Mason5f39d392007-10-15 16:14:19 -04003806 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04003807out:
Chris Mason39279cc2007-06-12 06:35:45 -04003808 btrfs_free_path(path);
3809 return ret;
Chris Mason39544012007-12-12 14:38:19 -05003810out_err:
3811 location->objectid = 0;
3812 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04003813}
3814
3815/*
3816 * when we hit a tree root in a directory, the btrfs part of the inode
3817 * needs to be changed to reflect the root directory of the tree root. This
3818 * is kind of like crossing a mount point.
3819 */
3820static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003821 struct inode *dir,
3822 struct dentry *dentry,
3823 struct btrfs_key *location,
3824 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04003825{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003826 struct btrfs_path *path;
3827 struct btrfs_root *new_root;
3828 struct btrfs_root_ref *ref;
3829 struct extent_buffer *leaf;
3830 int ret;
3831 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003832
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003833 path = btrfs_alloc_path();
3834 if (!path) {
3835 err = -ENOMEM;
3836 goto out;
3837 }
Chris Mason39279cc2007-06-12 06:35:45 -04003838
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003839 err = -ENOENT;
3840 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
3841 BTRFS_I(dir)->root->root_key.objectid,
3842 location->objectid);
3843 if (ret) {
3844 if (ret < 0)
3845 err = ret;
3846 goto out;
3847 }
Chris Mason39279cc2007-06-12 06:35:45 -04003848
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003849 leaf = path->nodes[0];
3850 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08003851 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003852 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
3853 goto out;
3854
3855 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
3856 (unsigned long)(ref + 1),
3857 dentry->d_name.len);
3858 if (ret)
3859 goto out;
3860
David Sterbab3b4aa72011-04-21 01:20:15 +02003861 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003862
3863 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
3864 if (IS_ERR(new_root)) {
3865 err = PTR_ERR(new_root);
3866 goto out;
3867 }
3868
3869 if (btrfs_root_refs(&new_root->root_item) == 0) {
3870 err = -ENOENT;
3871 goto out;
3872 }
3873
3874 *sub_root = new_root;
3875 location->objectid = btrfs_root_dirid(&new_root->root_item);
3876 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04003877 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003878 err = 0;
3879out:
3880 btrfs_free_path(path);
3881 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003882}
3883
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003884static void inode_tree_add(struct inode *inode)
3885{
3886 struct btrfs_root *root = BTRFS_I(inode)->root;
3887 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003888 struct rb_node **p;
3889 struct rb_node *parent;
Li Zefan33345d012011-04-20 10:31:50 +08003890 u64 ino = btrfs_ino(inode);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003891again:
3892 p = &root->inode_tree.rb_node;
3893 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003894
Al Viro1d3382c2010-10-23 15:19:20 -04003895 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04003896 return;
3897
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003898 spin_lock(&root->inode_lock);
3899 while (*p) {
3900 parent = *p;
3901 entry = rb_entry(parent, struct btrfs_inode, rb_node);
3902
Li Zefan33345d012011-04-20 10:31:50 +08003903 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003904 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08003905 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003906 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003907 else {
3908 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04003909 (I_WILL_FREE | I_FREEING)));
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003910 rb_erase(parent, &root->inode_tree);
3911 RB_CLEAR_NODE(parent);
3912 spin_unlock(&root->inode_lock);
3913 goto again;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003914 }
3915 }
3916 rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
3917 rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
3918 spin_unlock(&root->inode_lock);
3919}
3920
3921static void inode_tree_del(struct inode *inode)
3922{
3923 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04003924 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003925
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003926 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003927 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003928 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003929 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04003930 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003931 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003932 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04003933
Josef Bacik0af3d002010-06-21 14:48:16 -04003934 /*
3935 * Free space cache has inodes in the tree root, but the tree root has a
3936 * root_refs of 0, so this could end up dropping the tree root as a
3937 * snapshot, so we need the extra !root->fs_info->tree_root check to
3938 * make sure we don't drop it.
3939 */
3940 if (empty && btrfs_root_refs(&root->root_item) == 0 &&
3941 root != root->fs_info->tree_root) {
Yan, Zheng76dda932009-09-21 16:00:26 -04003942 synchronize_srcu(&root->fs_info->subvol_srcu);
3943 spin_lock(&root->inode_lock);
3944 empty = RB_EMPTY_ROOT(&root->inode_tree);
3945 spin_unlock(&root->inode_lock);
3946 if (empty)
3947 btrfs_add_dead_root(root);
3948 }
3949}
3950
Jeff Mahoney143bede2012-03-01 14:56:26 +01003951void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04003952{
3953 struct rb_node *node;
3954 struct rb_node *prev;
3955 struct btrfs_inode *entry;
3956 struct inode *inode;
3957 u64 objectid = 0;
3958
3959 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
3960
3961 spin_lock(&root->inode_lock);
3962again:
3963 node = root->inode_tree.rb_node;
3964 prev = NULL;
3965 while (node) {
3966 prev = node;
3967 entry = rb_entry(node, struct btrfs_inode, rb_node);
3968
Li Zefan33345d012011-04-20 10:31:50 +08003969 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04003970 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08003971 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04003972 node = node->rb_right;
3973 else
3974 break;
3975 }
3976 if (!node) {
3977 while (prev) {
3978 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08003979 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04003980 node = prev;
3981 break;
3982 }
3983 prev = rb_next(prev);
3984 }
3985 }
3986 while (node) {
3987 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08003988 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04003989 inode = igrab(&entry->vfs_inode);
3990 if (inode) {
3991 spin_unlock(&root->inode_lock);
3992 if (atomic_read(&inode->i_count) > 1)
3993 d_prune_aliases(inode);
3994 /*
Al Viro45321ac2010-06-07 13:43:19 -04003995 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04003996 * the inode cache when its usage count
3997 * hits zero.
3998 */
3999 iput(inode);
4000 cond_resched();
4001 spin_lock(&root->inode_lock);
4002 goto again;
4003 }
4004
4005 if (cond_resched_lock(&root->inode_lock))
4006 goto again;
4007
4008 node = rb_next(node);
4009 }
4010 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004011}
4012
Chris Masone02119d2008-09-05 16:13:11 -04004013static int btrfs_init_locked_inode(struct inode *inode, void *p)
4014{
4015 struct btrfs_iget_args *args = p;
4016 inode->i_ino = args->ino;
Chris Masone02119d2008-09-05 16:13:11 -04004017 BTRFS_I(inode)->root = args->root;
Josef Bacik6a632092009-02-20 11:00:09 -05004018 btrfs_set_inode_space_info(args->root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004019 return 0;
4020}
4021
4022static int btrfs_find_actor(struct inode *inode, void *opaque)
4023{
4024 struct btrfs_iget_args *args = opaque;
Li Zefan33345d012011-04-20 10:31:50 +08004025 return args->ino == btrfs_ino(inode) &&
Chris Masond3977122009-01-05 21:25:51 -05004026 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004027}
4028
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004029static struct inode *btrfs_iget_locked(struct super_block *s,
4030 u64 objectid,
4031 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04004032{
4033 struct inode *inode;
4034 struct btrfs_iget_args args;
4035 args.ino = objectid;
4036 args.root = root;
4037
4038 inode = iget5_locked(s, objectid, btrfs_find_actor,
4039 btrfs_init_locked_inode,
4040 (void *)&args);
4041 return inode;
4042}
4043
Balaji Rao1a54ef82008-07-21 02:01:04 +05304044/* Get an inode object given its location and corresponding root.
4045 * Returns in *is_new if the inode was read from disk
4046 */
4047struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00004048 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05304049{
4050 struct inode *inode;
4051
4052 inode = btrfs_iget_locked(s, location->objectid, root);
4053 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004054 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05304055
4056 if (inode->i_state & I_NEW) {
4057 BTRFS_I(inode)->root = root;
4058 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
4059 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07004060 if (!is_bad_inode(inode)) {
4061 inode_tree_add(inode);
4062 unlock_new_inode(inode);
4063 if (new)
4064 *new = 1;
4065 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04004066 unlock_new_inode(inode);
4067 iput(inode);
4068 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07004069 }
4070 }
4071
Balaji Rao1a54ef82008-07-21 02:01:04 +05304072 return inode;
4073}
4074
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004075static struct inode *new_simple_dir(struct super_block *s,
4076 struct btrfs_key *key,
4077 struct btrfs_root *root)
4078{
4079 struct inode *inode = new_inode(s);
4080
4081 if (!inode)
4082 return ERR_PTR(-ENOMEM);
4083
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004084 BTRFS_I(inode)->root = root;
4085 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
4086 BTRFS_I(inode)->dummy_inode = 1;
4087
4088 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08004089 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004090 inode->i_fop = &simple_dir_operations;
4091 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
4092 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
4093
4094 return inode;
4095}
4096
Chris Mason3de45862008-11-17 21:02:50 -05004097struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04004098{
Chris Masond3977122009-01-05 21:25:51 -05004099 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004100 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004101 struct btrfs_root *sub_root = root;
4102 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04004103 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004104 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004105
4106 if (dentry->d_name.len > BTRFS_NAME_LEN)
4107 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04004108
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004109 if (unlikely(d_need_lookup(dentry))) {
4110 memcpy(&location, dentry->d_fsdata, sizeof(struct btrfs_key));
4111 kfree(dentry->d_fsdata);
4112 dentry->d_fsdata = NULL;
Josef Bacika66e7cc2011-09-18 10:34:03 -04004113 /* This thing is hashed, drop it for now */
4114 d_drop(dentry);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004115 } else {
4116 ret = btrfs_inode_by_name(dir, dentry, &location);
4117 }
Chris Mason5f39d392007-10-15 16:14:19 -04004118
Chris Mason39279cc2007-06-12 06:35:45 -04004119 if (ret < 0)
4120 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04004121
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004122 if (location.objectid == 0)
4123 return NULL;
4124
4125 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00004126 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004127 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004128 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004129
4130 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
4131
Yan, Zheng76dda932009-09-21 16:00:26 -04004132 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004133 ret = fixup_tree_root_location(root, dir, dentry,
4134 &location, &sub_root);
4135 if (ret < 0) {
4136 if (ret != -ENOENT)
4137 inode = ERR_PTR(ret);
4138 else
4139 inode = new_simple_dir(dir->i_sb, &location, sub_root);
4140 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00004141 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004142 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004143 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
4144
Julia Lawall34d19ba2011-01-24 19:55:19 +00004145 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00004146 down_read(&root->fs_info->cleanup_work_sem);
4147 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004148 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00004149 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004150 if (ret)
4151 inode = ERR_PTR(ret);
Yan, Zhengc71bf092009-11-12 09:34:40 +00004152 }
4153
Chris Mason3de45862008-11-17 21:02:50 -05004154 return inode;
4155}
4156
Nick Pigginfe15ce42011-01-07 17:49:23 +11004157static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04004158{
4159 struct btrfs_root *root;
Li Zefan848cce02012-02-21 17:04:28 +08004160 struct inode *inode = dentry->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04004161
Li Zefan848cce02012-02-21 17:04:28 +08004162 if (!inode && !IS_ROOT(dentry))
4163 inode = dentry->d_parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04004164
Li Zefan848cce02012-02-21 17:04:28 +08004165 if (inode) {
4166 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04004167 if (btrfs_root_refs(&root->root_item) == 0)
4168 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08004169
4170 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
4171 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04004172 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004173 return 0;
4174}
4175
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004176static void btrfs_dentry_release(struct dentry *dentry)
4177{
4178 if (dentry->d_fsdata)
4179 kfree(dentry->d_fsdata);
4180}
4181
Chris Mason3de45862008-11-17 21:02:50 -05004182static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
4183 struct nameidata *nd)
4184{
Josef Bacika66e7cc2011-09-18 10:34:03 -04004185 struct dentry *ret;
4186
4187 ret = d_splice_alias(btrfs_lookup_dentry(dir, dentry), dentry);
4188 if (unlikely(d_need_lookup(dentry))) {
4189 spin_lock(&dentry->d_lock);
4190 dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
4191 spin_unlock(&dentry->d_lock);
4192 }
4193 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004194}
4195
Miao Xie16cdcec2011-04-22 18:12:22 +08004196unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04004197 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
4198};
4199
David Woodhousecbdf5a22008-08-06 19:42:33 +01004200static int btrfs_real_readdir(struct file *filp, void *dirent,
4201 filldir_t filldir)
Chris Mason39279cc2007-06-12 06:35:45 -04004202{
Chris Mason6da6aba2007-12-18 16:15:09 -05004203 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004204 struct btrfs_root *root = BTRFS_I(inode)->root;
4205 struct btrfs_item *item;
4206 struct btrfs_dir_item *di;
4207 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04004208 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004209 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08004210 struct list_head ins_list;
4211 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04004212 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04004213 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004214 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04004215 unsigned char d_type;
4216 int over = 0;
4217 u32 di_cur;
4218 u32 di_total;
4219 u32 di_len;
4220 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04004221 char tmp_name[32];
4222 char *name_ptr;
4223 int name_len;
Miao Xie16cdcec2011-04-22 18:12:22 +08004224 int is_curr = 0; /* filp->f_pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04004225
4226 /* FIXME, use a real flag for deciding about the key type */
4227 if (root->fs_info->tree_root == root)
4228 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04004229
Chris Mason39544012007-12-12 14:38:19 -05004230 /* special case for "." */
4231 if (filp->f_pos == 0) {
Hidetoshi Seto3765fef2011-09-18 10:20:46 -04004232 over = filldir(dirent, ".", 1,
4233 filp->f_pos, btrfs_ino(inode), DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05004234 if (over)
4235 return 0;
4236 filp->f_pos = 1;
4237 }
Chris Mason39544012007-12-12 14:38:19 -05004238 /* special case for .., just use the back ref */
4239 if (filp->f_pos == 1) {
David Woodhouse5ecc7e52008-08-17 15:14:48 +01004240 u64 pino = parent_ino(filp->f_path.dentry);
Chris Mason39544012007-12-12 14:38:19 -05004241 over = filldir(dirent, "..", 2,
Hidetoshi Seto3765fef2011-09-18 10:20:46 -04004242 filp->f_pos, pino, DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05004243 if (over)
David Woodhouse49593bf2008-08-17 17:08:36 +01004244 return 0;
Chris Mason39544012007-12-12 14:38:19 -05004245 filp->f_pos = 2;
4246 }
David Woodhouse49593bf2008-08-17 17:08:36 +01004247 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08004248 if (!path)
4249 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04004250
Josef Bacik026fd312011-05-13 10:32:11 -04004251 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01004252
Miao Xie16cdcec2011-04-22 18:12:22 +08004253 if (key_type == BTRFS_DIR_INDEX_KEY) {
4254 INIT_LIST_HEAD(&ins_list);
4255 INIT_LIST_HEAD(&del_list);
4256 btrfs_get_delayed_items(inode, &ins_list, &del_list);
4257 }
4258
Chris Mason39279cc2007-06-12 06:35:45 -04004259 btrfs_set_key_type(&key, key_type);
4260 key.offset = filp->f_pos;
Li Zefan33345d012011-04-20 10:31:50 +08004261 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004262
Chris Mason39279cc2007-06-12 06:35:45 -04004263 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4264 if (ret < 0)
4265 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01004266
4267 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04004268 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04004269 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08004270 if (slot >= btrfs_header_nritems(leaf)) {
4271 ret = btrfs_next_leaf(root, path);
4272 if (ret < 0)
4273 goto err;
4274 else if (ret > 0)
4275 break;
4276 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04004277 }
Chris Mason3de45862008-11-17 21:02:50 -05004278
Chris Mason5f39d392007-10-15 16:14:19 -04004279 item = btrfs_item_nr(leaf, slot);
4280 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4281
4282 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04004283 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004284 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004285 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004286 if (found_key.offset < filp->f_pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08004287 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08004288 if (key_type == BTRFS_DIR_INDEX_KEY &&
4289 btrfs_should_delete_dir_index(&del_list,
4290 found_key.offset))
4291 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04004292
4293 filp->f_pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08004294 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01004295
Chris Mason39279cc2007-06-12 06:35:45 -04004296 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
4297 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04004298 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01004299
4300 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04004301 struct btrfs_key location;
4302
Josef Bacik22a94d42011-03-16 16:47:17 -04004303 if (verify_dir_item(root, leaf, di))
4304 break;
4305
Chris Mason5f39d392007-10-15 16:14:19 -04004306 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01004307 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04004308 name_ptr = tmp_name;
4309 } else {
4310 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01004311 if (!name_ptr) {
4312 ret = -ENOMEM;
4313 goto err;
4314 }
Chris Mason5f39d392007-10-15 16:14:19 -04004315 }
4316 read_extent_buffer(leaf, name_ptr,
4317 (unsigned long)(di + 1), name_len);
4318
4319 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
4320 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05004321
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004322
Chris Mason3de45862008-11-17 21:02:50 -05004323 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01004324 * skip it.
4325 *
4326 * In contrast to old kernels, we insert the snapshot's
4327 * dir item and dir index after it has been created, so
4328 * we won't find a reference to our own snapshot. We
4329 * still keep the following code for backward
4330 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05004331 */
4332 if (location.type == BTRFS_ROOT_ITEM_KEY &&
4333 location.objectid == root->root_key.objectid) {
4334 over = 0;
4335 goto skip;
4336 }
Chris Mason5f39d392007-10-15 16:14:19 -04004337 over = filldir(dirent, name_ptr, name_len,
David Woodhouse49593bf2008-08-17 17:08:36 +01004338 found_key.offset, location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04004339 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04004340
Chris Mason3de45862008-11-17 21:02:50 -05004341skip:
Chris Mason5f39d392007-10-15 16:14:19 -04004342 if (name_ptr != tmp_name)
4343 kfree(name_ptr);
4344
Chris Mason39279cc2007-06-12 06:35:45 -04004345 if (over)
4346 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05004347 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01004348 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04004349 di_cur += di_len;
4350 di = (struct btrfs_dir_item *)((char *)di + di_len);
4351 }
Li Zefanb9e03af2011-03-23 10:43:58 +08004352next:
4353 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04004354 }
David Woodhouse49593bf2008-08-17 17:08:36 +01004355
Miao Xie16cdcec2011-04-22 18:12:22 +08004356 if (key_type == BTRFS_DIR_INDEX_KEY) {
4357 if (is_curr)
4358 filp->f_pos++;
4359 ret = btrfs_readdir_delayed_dir_index(filp, dirent, filldir,
4360 &ins_list);
4361 if (ret)
4362 goto nopos;
4363 }
4364
David Woodhouse49593bf2008-08-17 17:08:36 +01004365 /* Reached end of directory/root. Bump pos past the last item. */
Yan Zheng5e591a02008-02-19 11:41:02 -05004366 if (key_type == BTRFS_DIR_INDEX_KEY)
Jan Engelhardt406266a2009-12-09 22:00:38 +00004367 /*
4368 * 32-bit glibc will use getdents64, but then strtol -
4369 * so the last number we can serve is this.
4370 */
4371 filp->f_pos = 0x7fffffff;
Yan Zheng5e591a02008-02-19 11:41:02 -05004372 else
4373 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04004374nopos:
4375 ret = 0;
4376err:
Miao Xie16cdcec2011-04-22 18:12:22 +08004377 if (key_type == BTRFS_DIR_INDEX_KEY)
4378 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04004379 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004380 return ret;
4381}
4382
Christoph Hellwiga9185b42010-03-05 09:21:37 +01004383int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04004384{
4385 struct btrfs_root *root = BTRFS_I(inode)->root;
4386 struct btrfs_trans_handle *trans;
4387 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04004388 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04004389
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004390 if (BTRFS_I(inode)->dummy_inode)
Chris Mason4ca8b412008-08-05 13:30:48 -04004391 return 0;
4392
Chris Mason2cf85722011-07-26 15:35:09 -04004393 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(root, inode))
Li Zefan82d59022011-04-20 10:33:24 +08004394 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04004395
Christoph Hellwiga9185b42010-03-05 09:21:37 +01004396 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04004397 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004398 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04004399 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004400 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00004401 if (IS_ERR(trans))
4402 return PTR_ERR(trans);
Josef Bacik0af3d002010-06-21 14:48:16 -04004403 if (nolock)
4404 ret = btrfs_end_transaction_nolock(trans, root);
4405 else
4406 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004407 }
4408 return ret;
4409}
4410
4411/*
Chris Mason54aa1f42007-06-22 14:16:25 -04004412 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04004413 * inode changes. But, it is most likely to find the inode in cache.
4414 * FIXME, needs more benchmarking...there are no reasons other than performance
4415 * to keep or drop this code.
4416 */
Josef Bacik22c44fe2011-11-30 10:45:38 -05004417int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004418{
4419 struct btrfs_root *root = BTRFS_I(inode)->root;
4420 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004421 int ret;
4422
4423 if (BTRFS_I(inode)->dummy_inode)
Josef Bacik22c44fe2011-11-30 10:45:38 -05004424 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004425
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004426 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004427 if (IS_ERR(trans))
4428 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004429
4430 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04004431 if (ret && ret == -ENOSPC) {
4432 /* whoops, lets try again with the full transaction */
4433 btrfs_end_transaction(trans, root);
4434 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004435 if (IS_ERR(trans))
4436 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004437
Chris Mason94b60442010-05-26 11:02:00 -04004438 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04004439 }
Chris Mason39279cc2007-06-12 06:35:45 -04004440 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08004441 if (BTRFS_I(inode)->delayed_node)
4442 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004443
4444 return ret;
4445}
4446
4447/*
4448 * This is a copy of file_update_time. We need this so we can return error on
4449 * ENOSPC for updating the inode in the case of file write and mmap writes.
4450 */
4451int btrfs_update_time(struct file *file)
4452{
4453 struct inode *inode = file->f_path.dentry->d_inode;
4454 struct timespec now;
4455 int ret;
4456 enum { S_MTIME = 1, S_CTIME = 2, S_VERSION = 4 } sync_it = 0;
4457
4458 /* First try to exhaust all avenues to not sync */
4459 if (IS_NOCMTIME(inode))
4460 return 0;
4461
4462 now = current_fs_time(inode->i_sb);
4463 if (!timespec_equal(&inode->i_mtime, &now))
4464 sync_it = S_MTIME;
4465
4466 if (!timespec_equal(&inode->i_ctime, &now))
4467 sync_it |= S_CTIME;
4468
4469 if (IS_I_VERSION(inode))
4470 sync_it |= S_VERSION;
4471
4472 if (!sync_it)
4473 return 0;
4474
4475 /* Finally allowed to write? Takes lock. */
4476 if (mnt_want_write_file(file))
4477 return 0;
4478
4479 /* Only change inode inside the lock region */
4480 if (sync_it & S_VERSION)
4481 inode_inc_iversion(inode);
4482 if (sync_it & S_CTIME)
4483 inode->i_ctime = now;
4484 if (sync_it & S_MTIME)
4485 inode->i_mtime = now;
4486 ret = btrfs_dirty_inode(inode);
4487 if (!ret)
4488 mark_inode_dirty_sync(inode);
4489 mnt_drop_write(file->f_path.mnt);
4490 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004491}
4492
Chris Masond352ac62008-09-29 15:18:18 -04004493/*
4494 * find the highest existing sequence number in a directory
4495 * and then set the in-memory index_cnt variable to reflect
4496 * free sequence numbers
4497 */
Josef Bacikaec74772008-07-24 12:12:38 -04004498static int btrfs_set_inode_index_count(struct inode *inode)
4499{
4500 struct btrfs_root *root = BTRFS_I(inode)->root;
4501 struct btrfs_key key, found_key;
4502 struct btrfs_path *path;
4503 struct extent_buffer *leaf;
4504 int ret;
4505
Li Zefan33345d012011-04-20 10:31:50 +08004506 key.objectid = btrfs_ino(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004507 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
4508 key.offset = (u64)-1;
4509
4510 path = btrfs_alloc_path();
4511 if (!path)
4512 return -ENOMEM;
4513
4514 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4515 if (ret < 0)
4516 goto out;
4517 /* FIXME: we should be able to handle this */
4518 if (ret == 0)
4519 goto out;
4520 ret = 0;
4521
4522 /*
4523 * MAGIC NUMBER EXPLANATION:
4524 * since we search a directory based on f_pos we have to start at 2
4525 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
4526 * else has to start at 2
4527 */
4528 if (path->slots[0] == 0) {
4529 BTRFS_I(inode)->index_cnt = 2;
4530 goto out;
4531 }
4532
4533 path->slots[0]--;
4534
4535 leaf = path->nodes[0];
4536 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4537
Li Zefan33345d012011-04-20 10:31:50 +08004538 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacikaec74772008-07-24 12:12:38 -04004539 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
4540 BTRFS_I(inode)->index_cnt = 2;
4541 goto out;
4542 }
4543
4544 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
4545out:
4546 btrfs_free_path(path);
4547 return ret;
4548}
4549
Chris Masond352ac62008-09-29 15:18:18 -04004550/*
4551 * helper to find a free sequence number in a given directory. This current
4552 * code is very simple, later versions will do smarter things in the btree
4553 */
Chris Mason3de45862008-11-17 21:02:50 -05004554int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04004555{
4556 int ret = 0;
4557
4558 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08004559 ret = btrfs_inode_delayed_dir_index_count(dir);
4560 if (ret) {
4561 ret = btrfs_set_inode_index_count(dir);
4562 if (ret)
4563 return ret;
4564 }
Josef Bacikaec74772008-07-24 12:12:38 -04004565 }
4566
Chris Mason00e4e6b2008-08-05 11:18:09 -04004567 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04004568 BTRFS_I(dir)->index_cnt++;
4569
4570 return ret;
4571}
4572
Chris Mason39279cc2007-06-12 06:35:45 -04004573static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
4574 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04004575 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05004576 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04004577 u64 ref_objectid, u64 objectid,
4578 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04004579{
4580 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004581 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04004582 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04004583 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05004584 struct btrfs_inode_ref *ref;
4585 struct btrfs_key key[2];
4586 u32 sizes[2];
4587 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04004588 int ret;
4589 int owner;
4590
Chris Mason5f39d392007-10-15 16:14:19 -04004591 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07004592 if (!path)
4593 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04004594
Chris Mason39279cc2007-06-12 06:35:45 -04004595 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00004596 if (!inode) {
4597 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004598 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00004599 }
Chris Mason39279cc2007-06-12 06:35:45 -04004600
Li Zefan581bb052011-04-20 10:06:11 +08004601 /*
4602 * we have to initialize this early, so we can reclaim the inode
4603 * number if we fail afterwards in this function.
4604 */
4605 inode->i_ino = objectid;
4606
Josef Bacikaec74772008-07-24 12:12:38 -04004607 if (dir) {
liubo1abe9b82011-03-24 11:18:59 +00004608 trace_btrfs_inode_request(dir);
4609
Chris Mason3de45862008-11-17 21:02:50 -05004610 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04004611 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00004612 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04004613 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004614 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04004615 }
Josef Bacikaec74772008-07-24 12:12:38 -04004616 }
4617 /*
4618 * index_cnt is ignored for everything but a dir,
4619 * btrfs_get_inode_index_count has an explanation for the magic
4620 * number
4621 */
4622 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04004623 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04004624 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00004625 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik6a632092009-02-20 11:00:09 -05004626 btrfs_set_inode_space_info(root, inode);
Chris Masonb888db22007-08-27 16:49:44 -04004627
Al Viro569254b2011-07-24 17:08:40 -04004628 if (S_ISDIR(mode))
Chris Mason39279cc2007-06-12 06:35:45 -04004629 owner = 0;
4630 else
4631 owner = 1;
Chris Mason9c583092008-01-29 15:15:18 -05004632
4633 key[0].objectid = objectid;
4634 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
4635 key[0].offset = 0;
4636
4637 key[1].objectid = objectid;
4638 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
4639 key[1].offset = ref_objectid;
4640
4641 sizes[0] = sizeof(struct btrfs_inode_item);
4642 sizes[1] = name_len + sizeof(*ref);
4643
Chris Masonb9473432009-03-13 11:00:37 -04004644 path->leave_spinning = 1;
Chris Mason9c583092008-01-29 15:15:18 -05004645 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
4646 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04004647 goto fail;
4648
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03004649 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004650 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004651 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04004652 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4653 struct btrfs_inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04004654 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05004655
4656 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
4657 struct btrfs_inode_ref);
4658 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04004659 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05004660 ptr = (unsigned long)(ref + 1);
4661 write_extent_buffer(path->nodes[0], name, ptr, name_len);
4662
Chris Mason5f39d392007-10-15 16:14:19 -04004663 btrfs_mark_buffer_dirty(path->nodes[0]);
4664 btrfs_free_path(path);
4665
Chris Mason39279cc2007-06-12 06:35:45 -04004666 location = &BTRFS_I(inode)->location;
4667 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04004668 location->offset = 0;
4669 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
4670
Christoph Hellwig6cbff002009-04-17 10:37:41 +02004671 btrfs_inherit_iflags(inode, dir);
4672
Al Viro569254b2011-07-24 17:08:40 -04004673 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04004674 if (btrfs_test_opt(root, NODATASUM))
4675 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo75e7cb72011-03-22 10:12:20 +00004676 if (btrfs_test_opt(root, NODATACOW) ||
4677 (BTRFS_I(dir)->flags & BTRFS_INODE_NODATACOW))
Chris Mason94272162009-07-02 12:26:06 -04004678 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
4679 }
4680
Chris Mason39279cc2007-06-12 06:35:45 -04004681 insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004682 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00004683
4684 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04004685 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00004686
Chris Mason39279cc2007-06-12 06:35:45 -04004687 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004688fail:
Josef Bacikaec74772008-07-24 12:12:38 -04004689 if (dir)
4690 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04004691 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04004692 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004693 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004694}
4695
4696static inline u8 btrfs_inode_type(struct inode *inode)
4697{
4698 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
4699}
4700
Chris Masond352ac62008-09-29 15:18:18 -04004701/*
4702 * utility function to add 'inode' into 'parent_inode' with
4703 * a give name and a given sequence number.
4704 * if 'add_backref' is true, also insert a backref from the
4705 * inode to the parent directory.
4706 */
Chris Masone02119d2008-09-05 16:13:11 -04004707int btrfs_add_link(struct btrfs_trans_handle *trans,
4708 struct inode *parent_inode, struct inode *inode,
4709 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04004710{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004711 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004712 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04004713 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08004714 u64 ino = btrfs_ino(inode);
4715 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004716
Li Zefan33345d012011-04-20 10:31:50 +08004717 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004718 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
4719 } else {
Li Zefan33345d012011-04-20 10:31:50 +08004720 key.objectid = ino;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004721 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
4722 key.offset = 0;
4723 }
Chris Mason39279cc2007-06-12 06:35:45 -04004724
Li Zefan33345d012011-04-20 10:31:50 +08004725 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004726 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
4727 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08004728 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004729 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08004730 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
4731 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004732 }
4733
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004734 /* Nothing to clean up yet */
4735 if (ret)
4736 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004737
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004738 ret = btrfs_insert_dir_item(trans, root, name, name_len,
4739 parent_inode, &key,
4740 btrfs_inode_type(inode), index);
4741 if (ret == -EEXIST)
4742 goto fail_dir_item;
4743 else if (ret) {
4744 btrfs_abort_transaction(trans, root, ret);
4745 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004746 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004747
4748 btrfs_i_size_write(parent_inode, parent_inode->i_size +
4749 name_len * 2);
4750 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
4751 ret = btrfs_update_inode(trans, root, parent_inode);
4752 if (ret)
4753 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004754 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05004755
4756fail_dir_item:
4757 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
4758 u64 local_index;
4759 int err;
4760 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
4761 key.objectid, root->root_key.objectid,
4762 parent_ino, &local_index, name, name_len);
4763
4764 } else if (add_backref) {
4765 u64 local_index;
4766 int err;
4767
4768 err = btrfs_del_inode_ref(trans, root, name, name_len,
4769 ino, parent_ino, &local_index);
4770 }
4771 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004772}
4773
4774static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00004775 struct inode *dir, struct dentry *dentry,
4776 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04004777{
Josef Bacika1b075d2010-11-19 20:36:11 +00004778 int err = btrfs_add_link(trans, dir, inode,
4779 dentry->d_name.name, dentry->d_name.len,
4780 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004781 if (err > 0)
4782 err = -EEXIST;
4783 return err;
4784}
4785
Josef Bacik618e21d2007-07-11 10:18:17 -04004786static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04004787 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04004788{
4789 struct btrfs_trans_handle *trans;
4790 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05004791 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04004792 int err;
4793 int drop_inode = 0;
4794 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05004795 unsigned long nr = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004796 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04004797
4798 if (!new_valid_dev(rdev))
4799 return -EINVAL;
4800
Josef Bacik9ed74f22009-09-11 16:12:44 -04004801 /*
4802 * 2 for inode item and ref
4803 * 2 for dir items
4804 * 1 for xattr if selinux is on
4805 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004806 trans = btrfs_start_transaction(root, 5);
4807 if (IS_ERR(trans))
4808 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05004809
Li Zefan581bb052011-04-20 10:06:11 +08004810 err = btrfs_find_free_ino(root, &objectid);
4811 if (err)
4812 goto out_unlock;
4813
Josef Bacikaec74772008-07-24 12:12:38 -04004814 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08004815 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04004816 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04004817 if (IS_ERR(inode)) {
4818 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04004819 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04004820 }
Josef Bacik618e21d2007-07-11 10:18:17 -04004821
Eric Paris2a7dba32011-02-01 11:05:39 -05004822 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04004823 if (err) {
4824 drop_inode = 1;
4825 goto out_unlock;
4826 }
4827
Casey Schauflerad19db72011-12-15 10:09:07 -05004828 /*
4829 * If the active LSM wants to access the inode during
4830 * d_instantiate it needs these. Smack checks to see
4831 * if the filesystem supports xattrs by looking at the
4832 * ops vector.
4833 */
4834
4835 inode->i_op = &btrfs_special_inode_operations;
Josef Bacika1b075d2010-11-19 20:36:11 +00004836 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04004837 if (err)
4838 drop_inode = 1;
4839 else {
Josef Bacik618e21d2007-07-11 10:18:17 -04004840 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04004841 btrfs_update_inode(trans, root, inode);
Al Viro08c422c2011-12-23 07:58:13 -05004842 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04004843 }
Josef Bacik618e21d2007-07-11 10:18:17 -04004844out_unlock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04004845 nr = trans->blocks_used;
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004846 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004847 btrfs_btree_balance_dirty(root, nr);
Josef Bacik618e21d2007-07-11 10:18:17 -04004848 if (drop_inode) {
4849 inode_dec_link_count(inode);
4850 iput(inode);
4851 }
Josef Bacik618e21d2007-07-11 10:18:17 -04004852 return err;
4853}
4854
Chris Mason39279cc2007-06-12 06:35:45 -04004855static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viro4acdaf22011-07-26 01:42:34 -04004856 umode_t mode, struct nameidata *nd)
Chris Mason39279cc2007-06-12 06:35:45 -04004857{
4858 struct btrfs_trans_handle *trans;
4859 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05004860 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04004861 int drop_inode = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004862 int err;
Chris Mason1832a6d2007-12-21 16:27:21 -05004863 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004864 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004865 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004866
Josef Bacik9ed74f22009-09-11 16:12:44 -04004867 /*
4868 * 2 for inode item and ref
4869 * 2 for dir items
4870 * 1 for xattr if selinux is on
4871 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004872 trans = btrfs_start_transaction(root, 5);
4873 if (IS_ERR(trans))
4874 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004875
Li Zefan581bb052011-04-20 10:06:11 +08004876 err = btrfs_find_free_ino(root, &objectid);
4877 if (err)
4878 goto out_unlock;
4879
Josef Bacikaec74772008-07-24 12:12:38 -04004880 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08004881 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04004882 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04004883 if (IS_ERR(inode)) {
4884 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004885 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04004886 }
Chris Mason39279cc2007-06-12 06:35:45 -04004887
Eric Paris2a7dba32011-02-01 11:05:39 -05004888 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04004889 if (err) {
4890 drop_inode = 1;
4891 goto out_unlock;
4892 }
4893
Casey Schauflerad19db72011-12-15 10:09:07 -05004894 /*
4895 * If the active LSM wants to access the inode during
4896 * d_instantiate it needs these. Smack checks to see
4897 * if the filesystem supports xattrs by looking at the
4898 * ops vector.
4899 */
4900 inode->i_fop = &btrfs_file_operations;
4901 inode->i_op = &btrfs_file_inode_operations;
4902
Josef Bacika1b075d2010-11-19 20:36:11 +00004903 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004904 if (err)
4905 drop_inode = 1;
4906 else {
4907 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04004908 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05004909 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Al Viro08c422c2011-12-23 07:58:13 -05004910 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004911 }
Chris Mason39279cc2007-06-12 06:35:45 -04004912out_unlock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04004913 nr = trans->blocks_used;
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004914 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004915 if (drop_inode) {
4916 inode_dec_link_count(inode);
4917 iput(inode);
4918 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04004919 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004920 return err;
4921}
4922
4923static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
4924 struct dentry *dentry)
4925{
4926 struct btrfs_trans_handle *trans;
4927 struct btrfs_root *root = BTRFS_I(dir)->root;
4928 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004929 u64 index;
Chris Mason1832a6d2007-12-21 16:27:21 -05004930 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004931 int err;
4932 int drop_inode = 0;
4933
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00004934 /* do not allow sys_link's with other subvols of the same device */
4935 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00004936 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00004937
Al Viroc055e992011-03-04 17:15:18 +00004938 if (inode->i_nlink == ~0U)
4939 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004940
Chris Mason3de45862008-11-17 21:02:50 -05004941 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004942 if (err)
4943 goto fail;
4944
Yan, Zhenga22285a2010-05-16 10:48:46 -04004945 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00004946 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04004947 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00004948 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04004949 */
Miao Xie7e6b6462011-02-18 09:21:17 +00004950 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004951 if (IS_ERR(trans)) {
4952 err = PTR_ERR(trans);
4953 goto fail;
4954 }
Chris Mason5f39d392007-10-15 16:14:19 -04004955
Miao Xie31534952011-04-13 13:19:21 +08004956 btrfs_inc_nlink(inode);
4957 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6e2010-10-23 11:11:40 -04004958 ihold(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004959
Josef Bacika1b075d2010-11-19 20:36:11 +00004960 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04004961
Yan, Zhenga5719522009-09-24 09:17:31 -04004962 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04004963 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04004964 } else {
Al Viro10d9f302011-07-16 23:09:10 -04004965 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04004966 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004967 if (err)
4968 goto fail;
Al Viro08c422c2011-12-23 07:58:13 -05004969 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00004970 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04004971 }
Chris Mason39279cc2007-06-12 06:35:45 -04004972
Chris Masond3c2fdc2007-09-17 10:58:06 -04004973 nr = trans->blocks_used;
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004974 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05004975fail:
Chris Mason39279cc2007-06-12 06:35:45 -04004976 if (drop_inode) {
4977 inode_dec_link_count(inode);
4978 iput(inode);
4979 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04004980 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004981 return err;
4982}
4983
Al Viro18bb1db2011-07-26 01:41:39 -04004984static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04004985{
Chris Masonb9d86662008-05-02 16:13:49 -04004986 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04004987 struct btrfs_trans_handle *trans;
4988 struct btrfs_root *root = BTRFS_I(dir)->root;
4989 int err = 0;
4990 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04004991 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004992 u64 index = 0;
Chris Masond3c2fdc2007-09-17 10:58:06 -04004993 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04004994
Josef Bacik9ed74f22009-09-11 16:12:44 -04004995 /*
4996 * 2 items for inode and ref
4997 * 2 items for dir items
4998 * 1 for xattr if selinux is on
4999 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005000 trans = btrfs_start_transaction(root, 5);
5001 if (IS_ERR(trans))
5002 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04005003
Li Zefan581bb052011-04-20 10:06:11 +08005004 err = btrfs_find_free_ino(root, &objectid);
5005 if (err)
5006 goto out_fail;
5007
Josef Bacikaec74772008-07-24 12:12:38 -04005008 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005009 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005010 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04005011 if (IS_ERR(inode)) {
5012 err = PTR_ERR(inode);
5013 goto out_fail;
5014 }
Chris Mason5f39d392007-10-15 16:14:19 -04005015
Chris Mason39279cc2007-06-12 06:35:45 -04005016 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04005017
Eric Paris2a7dba32011-02-01 11:05:39 -05005018 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005019 if (err)
5020 goto out_fail;
5021
Chris Mason39279cc2007-06-12 06:35:45 -04005022 inode->i_op = &btrfs_dir_inode_operations;
5023 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04005024
Chris Masondbe674a2008-07-17 12:54:05 -04005025 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005026 err = btrfs_update_inode(trans, root, inode);
5027 if (err)
5028 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005029
Josef Bacika1b075d2010-11-19 20:36:11 +00005030 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
5031 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005032 if (err)
5033 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005034
Chris Mason39279cc2007-06-12 06:35:45 -04005035 d_instantiate(dentry, inode);
5036 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005037
5038out_fail:
Chris Masond3c2fdc2007-09-17 10:58:06 -04005039 nr = trans->blocks_used;
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005040 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005041 if (drop_on_err)
5042 iput(inode);
Chris Masond3c2fdc2007-09-17 10:58:06 -04005043 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04005044 return err;
5045}
5046
Chris Masond352ac62008-09-29 15:18:18 -04005047/* helper for btfs_get_extent. Given an existing extent in the tree,
5048 * and an extent that you want to insert, deal with overlap and insert
5049 * the new extent into the tree.
5050 */
Chris Mason3b951512008-04-17 11:29:12 -04005051static int merge_extent_mapping(struct extent_map_tree *em_tree,
5052 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005053 struct extent_map *em,
5054 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04005055{
5056 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04005057
Chris Masone6dcd2d2008-07-17 12:53:50 -04005058 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
5059 start_diff = map_start - em->start;
5060 em->start = map_start;
5061 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04005062 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
5063 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04005064 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04005065 em->block_len -= start_diff;
5066 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005067 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04005068}
5069
Chris Masonc8b97812008-10-29 14:49:59 -04005070static noinline int uncompress_inline(struct btrfs_path *path,
5071 struct inode *inode, struct page *page,
5072 size_t pg_offset, u64 extent_offset,
5073 struct btrfs_file_extent_item *item)
5074{
5075 int ret;
5076 struct extent_buffer *leaf = path->nodes[0];
5077 char *tmp;
5078 size_t max_size;
5079 unsigned long inline_size;
5080 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08005081 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04005082
5083 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08005084 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04005085 max_size = btrfs_file_extent_ram_bytes(leaf, item);
5086 inline_size = btrfs_file_extent_inline_item_len(leaf,
5087 btrfs_item_nr(leaf, path->slots[0]));
5088 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04005089 if (!tmp)
5090 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04005091 ptr = btrfs_file_extent_inline_start(item);
5092
5093 read_extent_buffer(leaf, tmp, ptr, inline_size);
5094
Chris Mason5b050f02008-11-11 09:34:41 -05005095 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08005096 ret = btrfs_decompress(compress_type, tmp, page,
5097 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04005098 if (ret) {
Cong Wang7ac687d2011-11-25 23:14:28 +08005099 char *kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04005100 unsigned long copy_size = min_t(u64,
5101 PAGE_CACHE_SIZE - pg_offset,
5102 max_size - extent_offset);
5103 memset(kaddr + pg_offset, 0, copy_size);
Cong Wang7ac687d2011-11-25 23:14:28 +08005104 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -04005105 }
5106 kfree(tmp);
5107 return 0;
5108}
5109
Chris Masond352ac62008-09-29 15:18:18 -04005110/*
5111 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05005112 * the ugly parts come from merging extents from the disk with the in-ram
5113 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04005114 * where the in-ram extents might be locked pending data=ordered completion.
5115 *
5116 * This also copies inline extents directly into the page.
5117 */
Chris Masond3977122009-01-05 21:25:51 -05005118
Chris Masona52d9a82007-08-27 16:49:44 -04005119struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05005120 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04005121 int create)
5122{
5123 int ret;
5124 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04005125 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04005126 u64 extent_start = 0;
5127 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08005128 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04005129 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04005130 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04005131 struct btrfs_root *root = BTRFS_I(inode)->root;
5132 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04005133 struct extent_buffer *leaf;
5134 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04005135 struct extent_map *em = NULL;
5136 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05005137 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04005138 struct btrfs_trans_handle *trans = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08005139 int compress_type;
Chris Masona52d9a82007-08-27 16:49:44 -04005140
Chris Masona52d9a82007-08-27 16:49:44 -04005141again:
Chris Mason890871b2009-09-02 16:24:52 -04005142 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005143 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04005144 if (em)
5145 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04005146 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005147
Chris Masona52d9a82007-08-27 16:49:44 -04005148 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04005149 if (em->start > start || em->start + em->len <= start)
5150 free_extent_map(em);
5151 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05005152 free_extent_map(em);
5153 else
5154 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005155 }
David Sterba172ddd62011-04-21 00:48:27 +02005156 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04005157 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05005158 err = -ENOMEM;
5159 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005160 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005161 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05005162 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05005163 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05005164 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04005165 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04005166
5167 if (!path) {
5168 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04005169 if (!path) {
5170 err = -ENOMEM;
5171 goto out;
5172 }
5173 /*
5174 * Chances are we'll be called again, so go ahead and do
5175 * readahead
5176 */
5177 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04005178 }
5179
Chris Mason179e29e2007-11-01 11:28:41 -04005180 ret = btrfs_lookup_file_extent(trans, root, path,
5181 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04005182 if (ret < 0) {
5183 err = ret;
5184 goto out;
5185 }
5186
5187 if (ret != 0) {
5188 if (path->slots[0] == 0)
5189 goto not_found;
5190 path->slots[0]--;
5191 }
5192
Chris Mason5f39d392007-10-15 16:14:19 -04005193 leaf = path->nodes[0];
5194 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04005195 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04005196 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04005197 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5198 found_type = btrfs_key_type(&found_key);
5199 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04005200 found_type != BTRFS_EXTENT_DATA_KEY) {
5201 goto not_found;
5202 }
5203
Chris Mason5f39d392007-10-15 16:14:19 -04005204 found_type = btrfs_file_extent_type(leaf, item);
5205 extent_start = found_key.offset;
Li Zefan261507a02010-12-17 14:21:50 +08005206 compress_type = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04005207 if (found_type == BTRFS_FILE_EXTENT_REG ||
5208 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04005209 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04005210 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04005211 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
5212 size_t size;
5213 size = btrfs_file_extent_inline_len(leaf, item);
5214 extent_end = (extent_start + size + root->sectorsize - 1) &
5215 ~((u64)root->sectorsize - 1);
5216 }
5217
5218 if (start >= extent_end) {
5219 path->slots[0]++;
5220 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
5221 ret = btrfs_next_leaf(root, path);
5222 if (ret < 0) {
5223 err = ret;
5224 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005225 }
Yan Zheng9036c102008-10-30 14:19:41 -04005226 if (ret > 0)
5227 goto not_found;
5228 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04005229 }
Yan Zheng9036c102008-10-30 14:19:41 -04005230 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5231 if (found_key.objectid != objectid ||
5232 found_key.type != BTRFS_EXTENT_DATA_KEY)
5233 goto not_found;
5234 if (start + len <= found_key.offset)
5235 goto not_found;
5236 em->start = start;
5237 em->len = found_key.offset - start;
5238 goto not_found_em;
5239 }
5240
Yan Zhengd899e052008-10-30 14:25:28 -04005241 if (found_type == BTRFS_FILE_EXTENT_REG ||
5242 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04005243 em->start = extent_start;
5244 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05005245 em->orig_start = extent_start -
5246 btrfs_file_extent_offset(leaf, item);
Chris Masondb945352007-10-15 16:15:53 -04005247 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
5248 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04005249 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04005250 goto insert;
5251 }
Li Zefan261507a02010-12-17 14:21:50 +08005252 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04005253 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08005254 em->compress_type = compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04005255 em->block_start = bytenr;
5256 em->block_len = btrfs_file_extent_disk_num_bytes(leaf,
5257 item);
5258 } else {
5259 bytenr += btrfs_file_extent_offset(leaf, item);
5260 em->block_start = bytenr;
5261 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04005262 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
5263 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04005264 }
Chris Masona52d9a82007-08-27 16:49:44 -04005265 goto insert;
5266 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04005267 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04005268 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04005269 size_t size;
5270 size_t extent_offset;
5271 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04005272
Chris Masona52d9a82007-08-27 16:49:44 -04005273 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04005274 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04005275 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04005276 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04005277 goto out;
5278 }
5279
Yan Zheng9036c102008-10-30 14:19:41 -04005280 size = btrfs_file_extent_inline_len(leaf, item);
5281 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05005282 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04005283 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04005284 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05005285 em->len = (copy_size + root->sectorsize - 1) &
5286 ~((u64)root->sectorsize - 1);
Yan Zhengff5b7ee2008-11-10 07:34:43 -05005287 em->orig_start = EXTENT_MAP_INLINE;
Li Zefan261507a02010-12-17 14:21:50 +08005288 if (compress_type) {
Chris Masonc8b97812008-10-29 14:49:59 -04005289 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08005290 em->compress_type = compress_type;
5291 }
Yan689f9342007-10-29 11:41:07 -04005292 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04005293 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08005294 if (btrfs_file_extent_compression(leaf, item) !=
5295 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04005296 ret = uncompress_inline(path, inode, page,
5297 pg_offset,
5298 extent_offset, item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005299 BUG_ON(ret); /* -ENOMEM */
Chris Masonc8b97812008-10-29 14:49:59 -04005300 } else {
5301 map = kmap(page);
5302 read_extent_buffer(leaf, map + pg_offset, ptr,
5303 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04005304 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
5305 memset(map + pg_offset + copy_size, 0,
5306 PAGE_CACHE_SIZE - pg_offset -
5307 copy_size);
5308 }
Chris Masonc8b97812008-10-29 14:49:59 -04005309 kunmap(page);
5310 }
Chris Mason179e29e2007-11-01 11:28:41 -04005311 flush_dcache_page(page);
5312 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01005313 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04005314 if (!trans) {
5315 kunmap(page);
5316 free_extent_map(em);
5317 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04005318
David Sterbab3b4aa72011-04-21 01:20:15 +02005319 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005320 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04005321
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005322 if (IS_ERR(trans))
5323 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04005324 goto again;
5325 }
Chris Masonc8b97812008-10-29 14:49:59 -04005326 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05005327 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04005328 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04005329 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04005330 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04005331 }
Chris Masond1310b22008-01-24 16:13:08 -05005332 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00005333 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04005334 goto insert;
5335 } else {
Chris Masond3977122009-01-05 21:25:51 -05005336 printk(KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04005337 WARN_ON(1);
5338 }
5339not_found:
5340 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05005341 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04005342not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04005343 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04005344 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04005345insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02005346 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05005347 if (em->start > start || extent_map_end(em) <= start) {
Chris Masond3977122009-01-05 21:25:51 -05005348 printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
5349 "[%llu %llu]\n", (unsigned long long)em->start,
5350 (unsigned long long)em->len,
5351 (unsigned long long)start,
5352 (unsigned long long)len);
Chris Masona52d9a82007-08-27 16:49:44 -04005353 err = -EIO;
5354 goto out;
5355 }
Chris Masond1310b22008-01-24 16:13:08 -05005356
5357 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04005358 write_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04005359 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04005360 /* it is possible that someone inserted the extent into the tree
5361 * while we had the lock dropped. It is also possible that
5362 * an overlapping map exists in the tree
5363 */
Chris Masona52d9a82007-08-27 16:49:44 -04005364 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04005365 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005366
5367 ret = 0;
5368
Chris Mason3b951512008-04-17 11:29:12 -04005369 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04005370 if (existing && (existing->start > start ||
5371 existing->start + existing->len <= start)) {
5372 free_extent_map(existing);
5373 existing = NULL;
5374 }
Chris Mason3b951512008-04-17 11:29:12 -04005375 if (!existing) {
5376 existing = lookup_extent_mapping(em_tree, em->start,
5377 em->len);
5378 if (existing) {
5379 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005380 em, start,
5381 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04005382 free_extent_map(existing);
5383 if (err) {
5384 free_extent_map(em);
5385 em = NULL;
5386 }
5387 } else {
5388 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04005389 free_extent_map(em);
5390 em = NULL;
5391 }
5392 } else {
5393 free_extent_map(em);
5394 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005395 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04005396 }
Chris Masona52d9a82007-08-27 16:49:44 -04005397 }
Chris Mason890871b2009-09-02 16:24:52 -04005398 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04005399out:
liubo1abe9b82011-03-24 11:18:59 +00005400
5401 trace_btrfs_get_extent(root, em);
5402
Chris Masonf4219502008-07-22 11:18:09 -04005403 if (path)
5404 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04005405 if (trans) {
5406 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05005407 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04005408 err = ret;
5409 }
Chris Masona52d9a82007-08-27 16:49:44 -04005410 if (err) {
5411 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04005412 return ERR_PTR(err);
5413 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005414 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04005415 return em;
5416}
5417
Chris Masonec29ed52011-02-23 16:23:20 -05005418struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
5419 size_t pg_offset, u64 start, u64 len,
5420 int create)
5421{
5422 struct extent_map *em;
5423 struct extent_map *hole_em = NULL;
5424 u64 range_start = start;
5425 u64 end;
5426 u64 found;
5427 u64 found_end;
5428 int err = 0;
5429
5430 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
5431 if (IS_ERR(em))
5432 return em;
5433 if (em) {
5434 /*
5435 * if our em maps to a hole, there might
5436 * actually be delalloc bytes behind it
5437 */
5438 if (em->block_start != EXTENT_MAP_HOLE)
5439 return em;
5440 else
5441 hole_em = em;
5442 }
5443
5444 /* check to see if we've wrapped (len == -1 or similar) */
5445 end = start + len;
5446 if (end < start)
5447 end = (u64)-1;
5448 else
5449 end -= 1;
5450
5451 em = NULL;
5452
5453 /* ok, we didn't find anything, lets look for delalloc */
5454 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
5455 end, len, EXTENT_DELALLOC, 1);
5456 found_end = range_start + found;
5457 if (found_end < range_start)
5458 found_end = (u64)-1;
5459
5460 /*
5461 * we didn't find anything useful, return
5462 * the original results from get_extent()
5463 */
5464 if (range_start > end || found_end <= start) {
5465 em = hole_em;
5466 hole_em = NULL;
5467 goto out;
5468 }
5469
5470 /* adjust the range_start to make sure it doesn't
5471 * go backwards from the start they passed in
5472 */
5473 range_start = max(start,range_start);
5474 found = found_end - range_start;
5475
5476 if (found > 0) {
5477 u64 hole_start = start;
5478 u64 hole_len = len;
5479
David Sterba172ddd62011-04-21 00:48:27 +02005480 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05005481 if (!em) {
5482 err = -ENOMEM;
5483 goto out;
5484 }
5485 /*
5486 * when btrfs_get_extent can't find anything it
5487 * returns one huge hole
5488 *
5489 * make sure what it found really fits our range, and
5490 * adjust to make sure it is based on the start from
5491 * the caller
5492 */
5493 if (hole_em) {
5494 u64 calc_end = extent_map_end(hole_em);
5495
5496 if (calc_end <= start || (hole_em->start > end)) {
5497 free_extent_map(hole_em);
5498 hole_em = NULL;
5499 } else {
5500 hole_start = max(hole_em->start, start);
5501 hole_len = calc_end - hole_start;
5502 }
5503 }
5504 em->bdev = NULL;
5505 if (hole_em && range_start > hole_start) {
5506 /* our hole starts before our delalloc, so we
5507 * have to return just the parts of the hole
5508 * that go until the delalloc starts
5509 */
5510 em->len = min(hole_len,
5511 range_start - hole_start);
5512 em->start = hole_start;
5513 em->orig_start = hole_start;
5514 /*
5515 * don't adjust block start at all,
5516 * it is fixed at EXTENT_MAP_HOLE
5517 */
5518 em->block_start = hole_em->block_start;
5519 em->block_len = hole_len;
5520 } else {
5521 em->start = range_start;
5522 em->len = found;
5523 em->orig_start = range_start;
5524 em->block_start = EXTENT_MAP_DELALLOC;
5525 em->block_len = found;
5526 }
5527 } else if (hole_em) {
5528 return hole_em;
5529 }
5530out:
5531
5532 free_extent_map(hole_em);
5533 if (err) {
5534 free_extent_map(em);
5535 return ERR_PTR(err);
5536 }
5537 return em;
5538}
5539
Josef Bacik4b46fce2010-05-23 11:00:55 -04005540static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
Josef Bacik16d299a2011-04-06 14:53:07 -04005541 struct extent_map *em,
Josef Bacik4b46fce2010-05-23 11:00:55 -04005542 u64 start, u64 len)
5543{
5544 struct btrfs_root *root = BTRFS_I(inode)->root;
5545 struct btrfs_trans_handle *trans;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005546 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
5547 struct btrfs_key ins;
5548 u64 alloc_hint;
5549 int ret;
Josef Bacik16d299a2011-04-06 14:53:07 -04005550 bool insert = false;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005551
Josef Bacik16d299a2011-04-06 14:53:07 -04005552 /*
5553 * Ok if the extent map we looked up is a hole and is for the exact
5554 * range we want, there is no reason to allocate a new one, however if
5555 * it is not right then we need to free this one and drop the cache for
5556 * our range.
5557 */
5558 if (em->block_start != EXTENT_MAP_HOLE || em->start != start ||
5559 em->len != len) {
5560 free_extent_map(em);
5561 em = NULL;
5562 insert = true;
5563 btrfs_drop_extent_cache(inode, start, start + len - 1, 0);
5564 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04005565
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005566 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005567 if (IS_ERR(trans))
5568 return ERR_CAST(trans);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005569
Chris Mason4cb53002011-05-24 15:35:30 -04005570 if (start <= BTRFS_I(inode)->disk_i_size && len < 64 * 1024)
5571 btrfs_add_inode_defrag(trans, inode);
5572
Josef Bacik4b46fce2010-05-23 11:00:55 -04005573 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
5574
5575 alloc_hint = get_extent_allocation_hint(inode, start, len);
5576 ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0,
Josef Bacik81c9ad22012-01-18 10:56:06 -05005577 alloc_hint, &ins, 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005578 if (ret) {
5579 em = ERR_PTR(ret);
5580 goto out;
5581 }
5582
Josef Bacik4b46fce2010-05-23 11:00:55 -04005583 if (!em) {
David Sterba172ddd62011-04-21 00:48:27 +02005584 em = alloc_extent_map();
Josef Bacik16d299a2011-04-06 14:53:07 -04005585 if (!em) {
5586 em = ERR_PTR(-ENOMEM);
5587 goto out;
5588 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04005589 }
5590
5591 em->start = start;
5592 em->orig_start = em->start;
5593 em->len = ins.offset;
5594
5595 em->block_start = ins.objectid;
5596 em->block_len = ins.offset;
5597 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik16d299a2011-04-06 14:53:07 -04005598
5599 /*
5600 * We need to do this because if we're using the original em we searched
5601 * for, we could have EXTENT_FLAG_VACANCY set, and we don't want that.
5602 */
5603 em->flags = 0;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005604 set_bit(EXTENT_FLAG_PINNED, &em->flags);
5605
Josef Bacik16d299a2011-04-06 14:53:07 -04005606 while (insert) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04005607 write_lock(&em_tree->lock);
5608 ret = add_extent_mapping(em_tree, em);
5609 write_unlock(&em_tree->lock);
5610 if (ret != -EEXIST)
5611 break;
5612 btrfs_drop_extent_cache(inode, start, start + em->len - 1, 0);
5613 }
5614
5615 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
5616 ins.offset, ins.offset, 0);
5617 if (ret) {
5618 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
5619 em = ERR_PTR(ret);
5620 }
5621out:
5622 btrfs_end_transaction(trans, root);
5623 return em;
5624}
5625
Chris Mason46bfbb52010-05-26 11:04:10 -04005626/*
5627 * returns 1 when the nocow is safe, < 1 on error, 0 if the
5628 * block must be cow'd
5629 */
5630static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans,
5631 struct inode *inode, u64 offset, u64 len)
5632{
5633 struct btrfs_path *path;
5634 int ret;
5635 struct extent_buffer *leaf;
5636 struct btrfs_root *root = BTRFS_I(inode)->root;
5637 struct btrfs_file_extent_item *fi;
5638 struct btrfs_key key;
5639 u64 disk_bytenr;
5640 u64 backref_offset;
5641 u64 extent_end;
5642 u64 num_bytes;
5643 int slot;
5644 int found_type;
5645
5646 path = btrfs_alloc_path();
5647 if (!path)
5648 return -ENOMEM;
5649
Li Zefan33345d012011-04-20 10:31:50 +08005650 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04005651 offset, 0);
5652 if (ret < 0)
5653 goto out;
5654
5655 slot = path->slots[0];
5656 if (ret == 1) {
5657 if (slot == 0) {
5658 /* can't find the item, must cow */
5659 ret = 0;
5660 goto out;
5661 }
5662 slot--;
5663 }
5664 ret = 0;
5665 leaf = path->nodes[0];
5666 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08005667 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04005668 key.type != BTRFS_EXTENT_DATA_KEY) {
5669 /* not our file or wrong item type, must cow */
5670 goto out;
5671 }
5672
5673 if (key.offset > offset) {
5674 /* Wrong offset, must cow */
5675 goto out;
5676 }
5677
5678 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
5679 found_type = btrfs_file_extent_type(leaf, fi);
5680 if (found_type != BTRFS_FILE_EXTENT_REG &&
5681 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
5682 /* not a regular extent, must cow */
5683 goto out;
5684 }
5685 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
5686 backref_offset = btrfs_file_extent_offset(leaf, fi);
5687
5688 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
5689 if (extent_end < offset + len) {
5690 /* extent doesn't include our full range, must cow */
5691 goto out;
5692 }
5693
5694 if (btrfs_extent_readonly(root, disk_bytenr))
5695 goto out;
5696
5697 /*
5698 * look for other files referencing this extent, if we
5699 * find any we must cow
5700 */
Li Zefan33345d012011-04-20 10:31:50 +08005701 if (btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04005702 key.offset - backref_offset, disk_bytenr))
5703 goto out;
5704
5705 /*
5706 * adjust disk_bytenr and num_bytes to cover just the bytes
5707 * in this extent we are about to write. If there
5708 * are any csums in that range we have to cow in order
5709 * to keep the csums correct
5710 */
5711 disk_bytenr += backref_offset;
5712 disk_bytenr += offset - key.offset;
5713 num_bytes = min(offset + len, extent_end) - offset;
5714 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
5715 goto out;
5716 /*
5717 * all of the above have passed, it is safe to overwrite this extent
5718 * without cow
5719 */
5720 ret = 1;
5721out:
5722 btrfs_free_path(path);
5723 return ret;
5724}
5725
Josef Bacik4b46fce2010-05-23 11:00:55 -04005726static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
5727 struct buffer_head *bh_result, int create)
5728{
5729 struct extent_map *em;
5730 struct btrfs_root *root = BTRFS_I(inode)->root;
5731 u64 start = iblock << inode->i_blkbits;
5732 u64 len = bh_result->b_size;
Chris Mason46bfbb52010-05-26 11:04:10 -04005733 struct btrfs_trans_handle *trans;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005734
5735 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
5736 if (IS_ERR(em))
5737 return PTR_ERR(em);
5738
5739 /*
5740 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
5741 * io. INLINE is special, and we could probably kludge it in here, but
5742 * it's still buffered so for safety lets just fall back to the generic
5743 * buffered path.
5744 *
5745 * For COMPRESSED we _have_ to read the entire extent in so we can
5746 * decompress it, so there will be buffering required no matter what we
5747 * do, so go ahead and fallback to buffered.
5748 *
5749 * We return -ENOTBLK because thats what makes DIO go ahead and go back
5750 * to buffered IO. Don't blame me, this is the price we pay for using
5751 * the generic code.
5752 */
5753 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
5754 em->block_start == EXTENT_MAP_INLINE) {
5755 free_extent_map(em);
5756 return -ENOTBLK;
5757 }
5758
5759 /* Just a good old fashioned hole, return */
5760 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
5761 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
5762 free_extent_map(em);
5763 /* DIO will do one hole at a time, so just unlock a sector */
5764 unlock_extent(&BTRFS_I(inode)->io_tree, start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01005765 start + root->sectorsize - 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005766 return 0;
5767 }
5768
5769 /*
5770 * We don't allocate a new extent in the following cases
5771 *
5772 * 1) The inode is marked as NODATACOW. In this case we'll just use the
5773 * existing extent.
5774 * 2) The extent is marked as PREALLOC. We're good to go here and can
5775 * just use the extent.
5776 *
5777 */
Chris Mason46bfbb52010-05-26 11:04:10 -04005778 if (!create) {
5779 len = em->len - (start - em->start);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005780 goto map;
Chris Mason46bfbb52010-05-26 11:04:10 -04005781 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04005782
5783 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
5784 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
5785 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04005786 int type;
5787 int ret;
Chris Mason46bfbb52010-05-26 11:04:10 -04005788 u64 block_start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005789
5790 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
5791 type = BTRFS_ORDERED_PREALLOC;
5792 else
5793 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04005794 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04005795 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04005796
5797 /*
5798 * we're not going to log anything, but we do need
5799 * to make sure the current transaction stays open
5800 * while we look for nocow cross refs
5801 */
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005802 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005803 if (IS_ERR(trans))
Chris Mason46bfbb52010-05-26 11:04:10 -04005804 goto must_cow;
5805
5806 if (can_nocow_odirect(trans, inode, start, len) == 1) {
5807 ret = btrfs_add_ordered_extent_dio(inode, start,
5808 block_start, len, len, type);
5809 btrfs_end_transaction(trans, root);
5810 if (ret) {
5811 free_extent_map(em);
5812 return ret;
5813 }
5814 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005815 }
Chris Mason46bfbb52010-05-26 11:04:10 -04005816 btrfs_end_transaction(trans, root);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005817 }
Chris Mason46bfbb52010-05-26 11:04:10 -04005818must_cow:
5819 /*
5820 * this will cow the extent, reset the len in case we changed
5821 * it above
5822 */
5823 len = bh_result->b_size;
Josef Bacik16d299a2011-04-06 14:53:07 -04005824 em = btrfs_new_extent_direct(inode, em, start, len);
Chris Mason46bfbb52010-05-26 11:04:10 -04005825 if (IS_ERR(em))
5826 return PTR_ERR(em);
5827 len = min(len, em->len - (start - em->start));
5828unlock:
Chris Mason4845e442010-05-25 20:56:50 -04005829 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, start + len - 1,
5830 EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DIRTY, 1,
5831 0, NULL, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005832map:
5833 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
5834 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04005835 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005836 bh_result->b_bdev = em->bdev;
5837 set_buffer_mapped(bh_result);
5838 if (create && !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
5839 set_buffer_new(bh_result);
5840
5841 free_extent_map(em);
5842
5843 return 0;
5844}
5845
5846struct btrfs_dio_private {
5847 struct inode *inode;
5848 u64 logical_offset;
5849 u64 disk_bytenr;
5850 u64 bytes;
5851 u32 *csums;
5852 void *private;
Miao Xiee65e1532010-11-22 03:04:43 +00005853
5854 /* number of bios pending for this dio */
5855 atomic_t pending_bios;
5856
5857 /* IO errors */
5858 int errors;
5859
5860 struct bio *orig_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005861};
5862
5863static void btrfs_endio_direct_read(struct bio *bio, int err)
5864{
Miao Xiee65e1532010-11-22 03:04:43 +00005865 struct btrfs_dio_private *dip = bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005866 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
5867 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005868 struct inode *inode = dip->inode;
5869 struct btrfs_root *root = BTRFS_I(inode)->root;
5870 u64 start;
5871 u32 *private = dip->csums;
5872
5873 start = dip->logical_offset;
5874 do {
5875 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
5876 struct page *page = bvec->bv_page;
5877 char *kaddr;
5878 u32 csum = ~(u32)0;
5879 unsigned long flags;
5880
5881 local_irq_save(flags);
Cong Wang7ac687d2011-11-25 23:14:28 +08005882 kaddr = kmap_atomic(page);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005883 csum = btrfs_csum_data(root, kaddr + bvec->bv_offset,
5884 csum, bvec->bv_len);
5885 btrfs_csum_final(csum, (char *)&csum);
Cong Wang7ac687d2011-11-25 23:14:28 +08005886 kunmap_atomic(kaddr);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005887 local_irq_restore(flags);
5888
5889 flush_dcache_page(bvec->bv_page);
5890 if (csum != *private) {
Li Zefan33345d012011-04-20 10:31:50 +08005891 printk(KERN_ERR "btrfs csum failed ino %llu off"
Josef Bacik4b46fce2010-05-23 11:00:55 -04005892 " %llu csum %u private %u\n",
Li Zefan33345d012011-04-20 10:31:50 +08005893 (unsigned long long)btrfs_ino(inode),
5894 (unsigned long long)start,
Josef Bacik4b46fce2010-05-23 11:00:55 -04005895 csum, *private);
5896 err = -EIO;
5897 }
5898 }
5899
5900 start += bvec->bv_len;
5901 private++;
5902 bvec++;
5903 } while (bvec <= bvec_end);
5904
5905 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01005906 dip->logical_offset + dip->bytes - 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005907 bio->bi_private = dip->private;
5908
5909 kfree(dip->csums);
5910 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04005911
5912 /* If we had a csum failure make sure to clear the uptodate flag */
5913 if (err)
5914 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005915 dio_end_io(bio, err);
5916}
5917
5918static void btrfs_endio_direct_write(struct bio *bio, int err)
5919{
5920 struct btrfs_dio_private *dip = bio->bi_private;
5921 struct inode *inode = dip->inode;
5922 struct btrfs_root *root = BTRFS_I(inode)->root;
5923 struct btrfs_trans_handle *trans;
5924 struct btrfs_ordered_extent *ordered = NULL;
5925 struct extent_state *cached_state = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05005926 u64 ordered_offset = dip->logical_offset;
5927 u64 ordered_bytes = dip->bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005928 int ret;
5929
5930 if (err)
5931 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05005932again:
5933 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
5934 &ordered_offset,
5935 ordered_bytes);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005936 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05005937 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005938
5939 BUG_ON(!ordered);
5940
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005941 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005942 if (IS_ERR(trans)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04005943 err = -ENOMEM;
5944 goto out;
5945 }
5946 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
5947
5948 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags)) {
5949 ret = btrfs_ordered_update_i_size(inode, 0, ordered);
5950 if (!ret)
Chris Mason21151332011-11-10 20:39:08 -05005951 err = btrfs_update_inode_fallback(trans, root, inode);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005952 goto out;
5953 }
5954
5955 lock_extent_bits(&BTRFS_I(inode)->io_tree, ordered->file_offset,
5956 ordered->file_offset + ordered->len - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01005957 &cached_state);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005958
5959 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags)) {
5960 ret = btrfs_mark_extent_written(trans, inode,
5961 ordered->file_offset,
5962 ordered->file_offset +
5963 ordered->len);
5964 if (ret) {
5965 err = ret;
5966 goto out_unlock;
5967 }
5968 } else {
5969 ret = insert_reserved_file_extent(trans, inode,
5970 ordered->file_offset,
5971 ordered->start,
5972 ordered->disk_len,
5973 ordered->len,
5974 ordered->len,
5975 0, 0, 0,
5976 BTRFS_FILE_EXTENT_REG);
5977 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
5978 ordered->file_offset, ordered->len);
5979 if (ret) {
5980 err = ret;
5981 WARN_ON(1);
5982 goto out_unlock;
5983 }
5984 }
5985
5986 add_pending_csums(trans, inode, ordered->file_offset, &ordered->list);
Josef Bacik1ef30be2011-04-05 19:25:36 -04005987 ret = btrfs_ordered_update_i_size(inode, 0, ordered);
Miao Xiea39f7522011-09-11 10:52:25 -04005988 if (!ret || !test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags))
Chris Mason21151332011-11-10 20:39:08 -05005989 btrfs_update_inode_fallback(trans, root, inode);
Josef Bacik1ef30be2011-04-05 19:25:36 -04005990 ret = 0;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005991out_unlock:
5992 unlock_extent_cached(&BTRFS_I(inode)->io_tree, ordered->file_offset,
5993 ordered->file_offset + ordered->len - 1,
5994 &cached_state, GFP_NOFS);
5995out:
5996 btrfs_delalloc_release_metadata(inode, ordered->len);
5997 btrfs_end_transaction(trans, root);
Chris Mason163cf092010-11-28 19:56:33 -05005998 ordered_offset = ordered->file_offset + ordered->len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005999 btrfs_put_ordered_extent(ordered);
6000 btrfs_put_ordered_extent(ordered);
Chris Mason163cf092010-11-28 19:56:33 -05006001
6002out_test:
6003 /*
6004 * our bio might span multiple ordered extents. If we haven't
6005 * completed the accounting for the whole dio, go back and try again
6006 */
6007 if (ordered_offset < dip->logical_offset + dip->bytes) {
6008 ordered_bytes = dip->logical_offset + dip->bytes -
6009 ordered_offset;
6010 goto again;
6011 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006012out_done:
6013 bio->bi_private = dip->private;
6014
6015 kfree(dip->csums);
6016 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006017
6018 /* If we had an error make sure to clear the uptodate flag */
6019 if (err)
6020 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006021 dio_end_io(bio, err);
6022}
6023
Chris Masoneaf25d92010-05-25 09:48:28 -04006024static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
6025 struct bio *bio, int mirror_num,
6026 unsigned long bio_flags, u64 offset)
6027{
6028 int ret;
6029 struct btrfs_root *root = BTRFS_I(inode)->root;
6030 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006031 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04006032 return 0;
6033}
6034
Miao Xiee65e1532010-11-22 03:04:43 +00006035static void btrfs_end_dio_bio(struct bio *bio, int err)
6036{
6037 struct btrfs_dio_private *dip = bio->bi_private;
6038
6039 if (err) {
Li Zefan33345d012011-04-20 10:31:50 +08006040 printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu "
Jan Beulich3dd14622010-12-07 14:54:09 +00006041 "sector %#Lx len %u err no %d\n",
Li Zefan33345d012011-04-20 10:31:50 +08006042 (unsigned long long)btrfs_ino(dip->inode), bio->bi_rw,
Jan Beulich3dd14622010-12-07 14:54:09 +00006043 (unsigned long long)bio->bi_sector, bio->bi_size, err);
Miao Xiee65e1532010-11-22 03:04:43 +00006044 dip->errors = 1;
6045
6046 /*
6047 * before atomic variable goto zero, we must make sure
6048 * dip->errors is perceived to be set.
6049 */
6050 smp_mb__before_atomic_dec();
6051 }
6052
6053 /* if there are more bios still pending for this dio, just exit */
6054 if (!atomic_dec_and_test(&dip->pending_bios))
6055 goto out;
6056
6057 if (dip->errors)
6058 bio_io_error(dip->orig_bio);
6059 else {
6060 set_bit(BIO_UPTODATE, &dip->orig_bio->bi_flags);
6061 bio_endio(dip->orig_bio, 0);
6062 }
6063out:
6064 bio_put(bio);
6065}
6066
6067static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
6068 u64 first_sector, gfp_t gfp_flags)
6069{
6070 int nr_vecs = bio_get_nr_vecs(bdev);
6071 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
6072}
6073
6074static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
6075 int rw, u64 file_offset, int skip_sum,
Josef Bacik1ae39932011-04-06 14:41:34 -04006076 u32 *csums, int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00006077{
6078 int write = rw & REQ_WRITE;
6079 struct btrfs_root *root = BTRFS_I(inode)->root;
6080 int ret;
6081
6082 bio_get(bio);
6083 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
6084 if (ret)
6085 goto err;
6086
Josef Bacik1ae39932011-04-06 14:41:34 -04006087 if (skip_sum)
6088 goto map;
6089
6090 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00006091 ret = btrfs_wq_submit_bio(root->fs_info,
6092 inode, rw, bio, 0, 0,
6093 file_offset,
6094 __btrfs_submit_bio_start_direct_io,
6095 __btrfs_submit_bio_done);
6096 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04006097 } else if (write) {
6098 /*
6099 * If we aren't doing async submit, calculate the csum of the
6100 * bio now.
6101 */
6102 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
6103 if (ret)
6104 goto err;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00006105 } else if (!skip_sum) {
6106 ret = btrfs_lookup_bio_sums_dio(root, inode, bio,
Miao Xiee65e1532010-11-22 03:04:43 +00006107 file_offset, csums);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00006108 if (ret)
6109 goto err;
6110 }
Miao Xiee65e1532010-11-22 03:04:43 +00006111
Josef Bacik1ae39932011-04-06 14:41:34 -04006112map:
6113 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006114err:
6115 bio_put(bio);
6116 return ret;
6117}
6118
6119static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
6120 int skip_sum)
6121{
6122 struct inode *inode = dip->inode;
6123 struct btrfs_root *root = BTRFS_I(inode)->root;
6124 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
6125 struct bio *bio;
6126 struct bio *orig_bio = dip->orig_bio;
6127 struct bio_vec *bvec = orig_bio->bi_io_vec;
6128 u64 start_sector = orig_bio->bi_sector;
6129 u64 file_offset = dip->logical_offset;
6130 u64 submit_len = 0;
6131 u64 map_length;
6132 int nr_pages = 0;
6133 u32 *csums = dip->csums;
6134 int ret = 0;
Josef Bacik1ae39932011-04-06 14:41:34 -04006135 int async_submit = 0;
Josef Bacik98bc3142011-03-22 11:00:46 -04006136 int write = rw & REQ_WRITE;
Miao Xiee65e1532010-11-22 03:04:43 +00006137
Miao Xiee65e1532010-11-22 03:04:43 +00006138 map_length = orig_bio->bi_size;
6139 ret = btrfs_map_block(map_tree, READ, start_sector << 9,
6140 &map_length, NULL, 0);
6141 if (ret) {
Josef Bacik64728bb2011-04-25 19:43:52 -04006142 bio_put(orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00006143 return -EIO;
6144 }
6145
Josef Bacik02f57c72011-04-06 14:25:44 -04006146 if (map_length >= orig_bio->bi_size) {
6147 bio = orig_bio;
6148 goto submit;
6149 }
6150
Josef Bacik1ae39932011-04-06 14:41:34 -04006151 async_submit = 1;
Josef Bacik02f57c72011-04-06 14:25:44 -04006152 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
6153 if (!bio)
6154 return -ENOMEM;
6155 bio->bi_private = dip;
6156 bio->bi_end_io = btrfs_end_dio_bio;
6157 atomic_inc(&dip->pending_bios);
6158
Miao Xiee65e1532010-11-22 03:04:43 +00006159 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
6160 if (unlikely(map_length < submit_len + bvec->bv_len ||
6161 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
6162 bvec->bv_offset) < bvec->bv_len)) {
6163 /*
6164 * inc the count before we submit the bio so
6165 * we know the end IO handler won't happen before
6166 * we inc the count. Otherwise, the dip might get freed
6167 * before we're done setting it up
6168 */
6169 atomic_inc(&dip->pending_bios);
6170 ret = __btrfs_submit_dio_bio(bio, inode, rw,
6171 file_offset, skip_sum,
Josef Bacik1ae39932011-04-06 14:41:34 -04006172 csums, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006173 if (ret) {
6174 bio_put(bio);
6175 atomic_dec(&dip->pending_bios);
6176 goto out_err;
6177 }
6178
Josef Bacik98bc3142011-03-22 11:00:46 -04006179 /* Write's use the ordered csums */
6180 if (!write && !skip_sum)
Miao Xiee65e1532010-11-22 03:04:43 +00006181 csums = csums + nr_pages;
6182 start_sector += submit_len >> 9;
6183 file_offset += submit_len;
6184
6185 submit_len = 0;
6186 nr_pages = 0;
6187
6188 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
6189 start_sector, GFP_NOFS);
6190 if (!bio)
6191 goto out_err;
6192 bio->bi_private = dip;
6193 bio->bi_end_io = btrfs_end_dio_bio;
6194
6195 map_length = orig_bio->bi_size;
6196 ret = btrfs_map_block(map_tree, READ, start_sector << 9,
6197 &map_length, NULL, 0);
6198 if (ret) {
6199 bio_put(bio);
6200 goto out_err;
6201 }
6202 } else {
6203 submit_len += bvec->bv_len;
6204 nr_pages ++;
6205 bvec++;
6206 }
6207 }
6208
Josef Bacik02f57c72011-04-06 14:25:44 -04006209submit:
Miao Xiee65e1532010-11-22 03:04:43 +00006210 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacik1ae39932011-04-06 14:41:34 -04006211 csums, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006212 if (!ret)
6213 return 0;
6214
6215 bio_put(bio);
6216out_err:
6217 dip->errors = 1;
6218 /*
6219 * before atomic variable goto zero, we must
6220 * make sure dip->errors is perceived to be set.
6221 */
6222 smp_mb__before_atomic_dec();
6223 if (atomic_dec_and_test(&dip->pending_bios))
6224 bio_io_error(dip->orig_bio);
6225
6226 /* bio_end_io() will handle error, so we needn't return it */
6227 return 0;
6228}
6229
Josef Bacik4b46fce2010-05-23 11:00:55 -04006230static void btrfs_submit_direct(int rw, struct bio *bio, struct inode *inode,
6231 loff_t file_offset)
6232{
6233 struct btrfs_root *root = BTRFS_I(inode)->root;
6234 struct btrfs_dio_private *dip;
6235 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006236 int skip_sum;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02006237 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006238 int ret = 0;
6239
6240 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
6241
6242 dip = kmalloc(sizeof(*dip), GFP_NOFS);
6243 if (!dip) {
6244 ret = -ENOMEM;
6245 goto free_ordered;
6246 }
6247 dip->csums = NULL;
6248
Josef Bacik98bc3142011-03-22 11:00:46 -04006249 /* Write's use the ordered csum stuff, so we don't need dip->csums */
6250 if (!write && !skip_sum) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04006251 dip->csums = kmalloc(sizeof(u32) * bio->bi_vcnt, GFP_NOFS);
6252 if (!dip->csums) {
Daniel J Bluemanb4966b72011-03-09 16:46:42 +00006253 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006254 ret = -ENOMEM;
6255 goto free_ordered;
6256 }
6257 }
6258
6259 dip->private = bio->bi_private;
6260 dip->inode = inode;
6261 dip->logical_offset = file_offset;
6262
Josef Bacik4b46fce2010-05-23 11:00:55 -04006263 dip->bytes = 0;
6264 do {
6265 dip->bytes += bvec->bv_len;
6266 bvec++;
6267 } while (bvec <= (bio->bi_io_vec + bio->bi_vcnt - 1));
6268
Chris Mason46bfbb52010-05-26 11:04:10 -04006269 dip->disk_bytenr = (u64)bio->bi_sector << 9;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006270 bio->bi_private = dip;
Miao Xiee65e1532010-11-22 03:04:43 +00006271 dip->errors = 0;
6272 dip->orig_bio = bio;
6273 atomic_set(&dip->pending_bios, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006274
6275 if (write)
6276 bio->bi_end_io = btrfs_endio_direct_write;
6277 else
6278 bio->bi_end_io = btrfs_endio_direct_read;
6279
Miao Xiee65e1532010-11-22 03:04:43 +00006280 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
6281 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04006282 return;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006283free_ordered:
6284 /*
6285 * If this is a write, we need to clean up the reserved space and kill
6286 * the ordered extent.
6287 */
6288 if (write) {
6289 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05006290 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006291 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
6292 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
6293 btrfs_free_reserved_extent(root, ordered->start,
6294 ordered->disk_len);
6295 btrfs_put_ordered_extent(ordered);
6296 btrfs_put_ordered_extent(ordered);
6297 }
6298 bio_endio(bio, ret);
6299}
6300
Chris Mason5a5f79b2010-05-26 21:33:37 -04006301static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
6302 const struct iovec *iov, loff_t offset,
6303 unsigned long nr_segs)
6304{
6305 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00006306 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04006307 size_t size;
6308 unsigned long addr;
6309 unsigned blocksize_mask = root->sectorsize - 1;
6310 ssize_t retval = -EINVAL;
6311 loff_t end = offset;
6312
6313 if (offset & blocksize_mask)
6314 goto out;
6315
6316 /* Check the memory alignment. Blocks cannot straddle pages */
6317 for (seg = 0; seg < nr_segs; seg++) {
6318 addr = (unsigned long)iov[seg].iov_base;
6319 size = iov[seg].iov_len;
6320 end += size;
Josef Bacika1b75f72011-04-08 15:51:18 +00006321 if ((addr & blocksize_mask) || (size & blocksize_mask))
Chris Mason5a5f79b2010-05-26 21:33:37 -04006322 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00006323
6324 /* If this is a write we don't need to check anymore */
6325 if (rw & WRITE)
6326 continue;
6327
6328 /*
6329 * Check to make sure we don't have duplicate iov_base's in this
6330 * iovec, if so return EINVAL, otherwise we'll get csum errors
6331 * when reading back.
6332 */
6333 for (i = seg + 1; i < nr_segs; i++) {
6334 if (iov[seg].iov_base == iov[i].iov_base)
6335 goto out;
6336 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04006337 }
6338 retval = 0;
6339out:
6340 return retval;
6341}
Chris Mason16432982008-04-10 10:23:21 -04006342static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
6343 const struct iovec *iov, loff_t offset,
6344 unsigned long nr_segs)
6345{
Josef Bacik4b46fce2010-05-23 11:00:55 -04006346 struct file *file = iocb->ki_filp;
6347 struct inode *inode = file->f_mapping->host;
6348 struct btrfs_ordered_extent *ordered;
Chris Mason4845e442010-05-25 20:56:50 -04006349 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006350 u64 lockstart, lockend;
6351 ssize_t ret;
Chris Mason4845e442010-05-25 20:56:50 -04006352 int writing = rw & WRITE;
6353 int write_bits = 0;
Chris Mason3f7c5792010-05-26 10:59:53 -04006354 size_t count = iov_length(iov, nr_segs);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006355
Chris Mason5a5f79b2010-05-26 21:33:37 -04006356 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
6357 offset, nr_segs)) {
6358 return 0;
6359 }
6360
Josef Bacik4b46fce2010-05-23 11:00:55 -04006361 lockstart = offset;
Chris Mason3f7c5792010-05-26 10:59:53 -04006362 lockend = offset + count - 1;
6363
6364 if (writing) {
6365 ret = btrfs_delalloc_reserve_space(inode, count);
6366 if (ret)
6367 goto out;
6368 }
Chris Mason4845e442010-05-25 20:56:50 -04006369
Josef Bacik4b46fce2010-05-23 11:00:55 -04006370 while (1) {
Chris Mason4845e442010-05-25 20:56:50 -04006371 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006372 0, &cached_state);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006373 /*
6374 * We're concerned with the entire range that we're going to be
6375 * doing DIO to, so we need to make sure theres no ordered
6376 * extents in this range.
6377 */
6378 ordered = btrfs_lookup_ordered_range(inode, lockstart,
6379 lockend - lockstart + 1);
6380 if (!ordered)
6381 break;
Chris Mason4845e442010-05-25 20:56:50 -04006382 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6383 &cached_state, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006384 btrfs_start_ordered_extent(inode, ordered, 1);
6385 btrfs_put_ordered_extent(ordered);
6386 cond_resched();
6387 }
6388
Chris Mason4845e442010-05-25 20:56:50 -04006389 /*
6390 * we don't use btrfs_set_extent_delalloc because we don't want
6391 * the dirty or uptodate bits
6392 */
6393 if (writing) {
6394 write_bits = EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING;
6395 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01006396 EXTENT_DELALLOC, NULL, &cached_state,
Chris Mason4845e442010-05-25 20:56:50 -04006397 GFP_NOFS);
6398 if (ret) {
6399 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6400 lockend, EXTENT_LOCKED | write_bits,
6401 1, 0, &cached_state, GFP_NOFS);
6402 goto out;
6403 }
6404 }
6405
6406 free_extent_state(cached_state);
6407 cached_state = NULL;
6408
Chris Mason5a5f79b2010-05-26 21:33:37 -04006409 ret = __blockdev_direct_IO(rw, iocb, inode,
6410 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
6411 iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
6412 btrfs_submit_direct, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006413
6414 if (ret < 0 && ret != -EIOCBQUEUED) {
Chris Mason4845e442010-05-25 20:56:50 -04006415 clear_extent_bit(&BTRFS_I(inode)->io_tree, offset,
6416 offset + iov_length(iov, nr_segs) - 1,
6417 EXTENT_LOCKED | write_bits, 1, 0,
6418 &cached_state, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006419 } else if (ret >= 0 && ret < iov_length(iov, nr_segs)) {
6420 /*
6421 * We're falling back to buffered, unlock the section we didn't
6422 * do IO on.
6423 */
Chris Mason4845e442010-05-25 20:56:50 -04006424 clear_extent_bit(&BTRFS_I(inode)->io_tree, offset + ret,
6425 offset + iov_length(iov, nr_segs) - 1,
6426 EXTENT_LOCKED | write_bits, 1, 0,
6427 &cached_state, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006428 }
Chris Mason4845e442010-05-25 20:56:50 -04006429out:
6430 free_extent_state(cached_state);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006431 return ret;
Chris Mason16432982008-04-10 10:23:21 -04006432}
6433
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05006434static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
6435 __u64 start, __u64 len)
6436{
Chris Masonec29ed52011-02-23 16:23:20 -05006437 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05006438}
6439
Chris Mason9ebefb182007-06-15 13:50:00 -04006440int btrfs_readpage(struct file *file, struct page *page)
6441{
Chris Masond1310b22008-01-24 16:13:08 -05006442 struct extent_io_tree *tree;
6443 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02006444 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006445}
Chris Mason1832a6d2007-12-21 16:27:21 -05006446
Chris Mason39279cc2007-06-12 06:35:45 -04006447static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
6448{
Chris Masond1310b22008-01-24 16:13:08 -05006449 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04006450
6451
6452 if (current->flags & PF_MEMALLOC) {
6453 redirty_page_for_writepage(wbc, page);
6454 unlock_page(page);
6455 return 0;
6456 }
Chris Masond1310b22008-01-24 16:13:08 -05006457 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006458 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
6459}
Chris Mason39279cc2007-06-12 06:35:45 -04006460
Chris Masonf4219502008-07-22 11:18:09 -04006461int btrfs_writepages(struct address_space *mapping,
6462 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04006463{
Chris Masond1310b22008-01-24 16:13:08 -05006464 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05006465
Chris Masond1310b22008-01-24 16:13:08 -05006466 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04006467 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
6468}
6469
Chris Mason3ab2fb52007-11-08 10:59:22 -05006470static int
6471btrfs_readpages(struct file *file, struct address_space *mapping,
6472 struct list_head *pages, unsigned nr_pages)
6473{
Chris Masond1310b22008-01-24 16:13:08 -05006474 struct extent_io_tree *tree;
6475 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05006476 return extent_readpages(tree, mapping, pages, nr_pages,
6477 btrfs_get_extent);
6478}
Chris Masone6dcd2d2008-07-17 12:53:50 -04006479static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04006480{
Chris Masond1310b22008-01-24 16:13:08 -05006481 struct extent_io_tree *tree;
6482 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04006483 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006484
Chris Masond1310b22008-01-24 16:13:08 -05006485 tree = &BTRFS_I(page->mapping->host)->io_tree;
6486 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05006487 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006488 if (ret == 1) {
6489 ClearPagePrivate(page);
6490 set_page_private(page, 0);
6491 page_cache_release(page);
6492 }
6493 return ret;
6494}
Chris Mason39279cc2007-06-12 06:35:45 -04006495
Chris Masone6dcd2d2008-07-17 12:53:50 -04006496static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
6497{
Chris Mason98509cf2008-09-11 15:51:43 -04006498 if (PageWriteback(page) || PageDirty(page))
6499 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05006500 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006501}
6502
Chris Masona52d9a82007-08-27 16:49:44 -04006503static void btrfs_invalidatepage(struct page *page, unsigned long offset)
6504{
Chris Masond1310b22008-01-24 16:13:08 -05006505 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006506 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00006507 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006508 u64 page_start = page_offset(page);
6509 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04006510
Chris Mason8b62b722009-09-02 16:53:46 -04006511
6512 /*
6513 * we have the page locked, so new writeback can't start,
6514 * and the dirty bit won't be cleared while we are here.
6515 *
6516 * Wait for IO on this page so that we can safely clear
6517 * the PagePrivate2 bit and do ordered accounting
6518 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006519 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04006520
Chris Masond1310b22008-01-24 16:13:08 -05006521 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006522 if (offset) {
6523 btrfs_releasepage(page, GFP_NOFS);
6524 return;
6525 }
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006526 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006527 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
6528 page_offset(page));
6529 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04006530 /*
6531 * IO on this page will never be started, so we need
6532 * to account for any ordered extents now
6533 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006534 clear_extent_bit(tree, page_start, page_end,
6535 EXTENT_DIRTY | EXTENT_DELALLOC |
Josef Bacik32c00af2009-10-08 13:34:05 -04006536 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING, 1, 0,
Josef Bacik2ac55d42010-02-03 19:33:23 +00006537 &cached_state, GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04006538 /*
6539 * whoever cleared the private bit is responsible
6540 * for the finish_ordered_io
6541 */
6542 if (TestClearPagePrivate2(page)) {
6543 btrfs_finish_ordered_io(page->mapping->host,
6544 page_start, page_end);
6545 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006546 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +00006547 cached_state = NULL;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006548 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006549 }
6550 clear_extent_bit(tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04006551 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
Josef Bacik2ac55d42010-02-03 19:33:23 +00006552 EXTENT_DO_ACCOUNTING, 1, 1, &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006553 __btrfs_releasepage(page, GFP_NOFS);
6554
Chris Mason4a096752008-07-21 10:29:44 -04006555 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04006556 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04006557 ClearPagePrivate(page);
6558 set_page_private(page, 0);
6559 page_cache_release(page);
6560 }
Chris Mason39279cc2007-06-12 06:35:45 -04006561}
6562
Chris Mason9ebefb182007-06-15 13:50:00 -04006563/*
6564 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
6565 * called from a page fault handler when a page is first dirtied. Hence we must
6566 * be careful to check for EOF conditions here. We set the page up correctly
6567 * for a written page which means we get ENOSPC checking when writing into
6568 * holes and correct delalloc and unwritten extent mapping on filesystems that
6569 * support these features.
6570 *
6571 * We are not allowed to take the i_mutex here so we have to play games to
6572 * protect against truncate races as the page could now be beyond EOF. Because
6573 * vmtruncate() writes the inode size before removing pages, once we have the
6574 * page lock we can determine safely if the page is beyond EOF. If it is not
6575 * beyond EOF, then the page is guaranteed safe against truncation until we
6576 * unlock the page.
6577 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07006578int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04006579{
Nick Pigginc2ec1752009-03-31 15:23:21 -07006580 struct page *page = vmf->page;
Chris Mason6da6aba2007-12-18 16:15:09 -05006581 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05006582 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006583 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
6584 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00006585 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006586 char *kaddr;
6587 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04006588 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05006589 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05006590 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006591 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006592 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04006593
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006594 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05006595 if (!ret) {
Josef Bacik22c44fe2011-11-30 10:45:38 -05006596 ret = btrfs_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05006597 reserved = 1;
6598 }
Nick Piggin56a76f82009-03-31 15:23:23 -07006599 if (ret) {
6600 if (ret == -ENOMEM)
6601 ret = VM_FAULT_OOM;
6602 else /* -ENOSPC, -EIO, etc */
6603 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05006604 if (reserved)
6605 goto out;
6606 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07006607 }
Chris Mason1832a6d2007-12-21 16:27:21 -05006608
Nick Piggin56a76f82009-03-31 15:23:23 -07006609 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006610again:
Chris Mason9ebefb182007-06-15 13:50:00 -04006611 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04006612 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006613 page_start = page_offset(page);
6614 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04006615
Chris Mason9ebefb182007-06-15 13:50:00 -04006616 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04006617 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04006618 /* page got truncated out from underneath us */
6619 goto out_unlock;
6620 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006621 wait_on_page_writeback(page);
6622
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006623 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006624 set_page_extent_mapped(page);
6625
Chris Masoneb84ae02008-07-17 13:53:27 -04006626 /*
6627 * we can't set the delalloc bits if there are pending ordered
6628 * extents. Drop our locks and wait for them to finish
6629 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006630 ordered = btrfs_lookup_ordered_extent(inode, page_start);
6631 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00006632 unlock_extent_cached(io_tree, page_start, page_end,
6633 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006634 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04006635 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006636 btrfs_put_ordered_extent(ordered);
6637 goto again;
6638 }
6639
Josef Bacikfbf19082009-10-01 17:10:23 -04006640 /*
6641 * XXX - page_mkwrite gets called every time the page is dirtied, even
6642 * if it was already dirty, so for space accounting reasons we need to
6643 * clear any delalloc bits for the range we are fixing to save. There
6644 * is probably a better way to do this, but for now keep consistent with
6645 * prepare_pages in the normal write path.
6646 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00006647 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04006648 EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00006649 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04006650
Josef Bacik2ac55d42010-02-03 19:33:23 +00006651 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
6652 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006653 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00006654 unlock_extent_cached(io_tree, page_start, page_end,
6655 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006656 ret = VM_FAULT_SIGBUS;
6657 goto out_unlock;
6658 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006659 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04006660
6661 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04006662 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04006663 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04006664 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04006665 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04006666
Chris Masone6dcd2d2008-07-17 12:53:50 -04006667 if (zero_start != PAGE_CACHE_SIZE) {
6668 kaddr = kmap(page);
6669 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
6670 flush_dcache_page(page);
6671 kunmap(page);
6672 }
Chris Mason247e7432008-07-17 12:53:51 -04006673 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006674 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04006675 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04006676
Chris Mason257c62e2009-10-13 13:21:08 -04006677 BTRFS_I(inode)->last_trans = root->fs_info->generation;
6678 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
6679
Josef Bacik2ac55d42010-02-03 19:33:23 +00006680 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04006681
6682out_unlock:
Chris Mason50a9b212009-09-11 12:33:12 -04006683 if (!ret)
6684 return VM_FAULT_LOCKED;
Chris Mason9ebefb182007-06-15 13:50:00 -04006685 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05006686out:
Josef Bacikec39e182012-01-12 19:10:12 -05006687 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05006688out_noreserve:
Chris Mason9ebefb182007-06-15 13:50:00 -04006689 return ret;
6690}
6691
Josef Bacika41ad392011-01-31 15:30:16 -05006692static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006693{
6694 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04006695 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04006696 int ret;
Josef Bacik3893e332011-01-31 16:03:11 -05006697 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006698 struct btrfs_trans_handle *trans;
Chris Masond3c2fdc2007-09-17 10:58:06 -04006699 unsigned long nr;
Chris Masondbe674a2008-07-17 12:54:05 -04006700 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04006701 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04006702
Josef Bacik5d5e1032009-10-13 16:46:49 -04006703 ret = btrfs_truncate_page(inode->i_mapping, inode->i_size);
6704 if (ret)
Josef Bacika41ad392011-01-31 15:30:16 -05006705 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00006706
Chris Mason4a096752008-07-21 10:29:44 -04006707 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Yan, Zheng80825102009-11-12 09:35:36 +00006708 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04006709
Josef Bacikfcb80c22011-05-03 10:40:22 -04006710 /*
6711 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
6712 * 3 things going on here
6713 *
6714 * 1) We need to reserve space for our orphan item and the space to
6715 * delete our orphan item. Lord knows we don't want to have a dangling
6716 * orphan item because we didn't reserve space to remove it.
6717 *
6718 * 2) We need to reserve space to update our inode.
6719 *
6720 * 3) We need to have something to cache all the space that is going to
6721 * be free'd up by the truncate operation, but also have some slack
6722 * space reserved in case it uses space during the truncate (thank you
6723 * very much snapshotting).
6724 *
6725 * And we need these to all be seperate. The fact is we can use alot of
6726 * space doing the truncate, and we have no earthly idea how much space
6727 * we will use, so we need the truncate reservation to be seperate so it
6728 * doesn't end up using space reserved for updating the inode or
6729 * removing the orphan item. We also need to be able to stop the
6730 * transaction and start a new one, which means we need to be able to
6731 * update the inode several times, and we have no idea of knowing how
6732 * many times that will be, so we can't just reserve 1 item for the
6733 * entirety of the opration, so that has to be done seperately as well.
6734 * Then there is the orphan item, which does indeed need to be held on
6735 * to for the whole operation, and we need nobody to touch this reserved
6736 * space except the orphan code.
6737 *
6738 * So that leaves us with
6739 *
6740 * 1) root->orphan_block_rsv - for the orphan deletion.
6741 * 2) rsv - for the truncate reservation, which we will steal from the
6742 * transaction reservation.
6743 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
6744 * updating the inode.
6745 */
6746 rsv = btrfs_alloc_block_rsv(root);
6747 if (!rsv)
6748 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04006749 rsv->size = min_size;
Josef Bacikf0cd8462011-03-04 14:37:08 -05006750
Josef Bacik907cbce2011-08-08 13:46:15 -04006751 /*
Josef Bacik07127182011-08-19 10:29:59 -04006752 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04006753 * 1 for the orphan item we're going to add
6754 * 1 for the orphan item deletion
6755 * 1 for updating the inode.
6756 */
Josef Bacikfcb80c22011-05-03 10:40:22 -04006757 trans = btrfs_start_transaction(root, 4);
6758 if (IS_ERR(trans)) {
6759 err = PTR_ERR(trans);
6760 goto out;
6761 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05006762
Josef Bacik907cbce2011-08-08 13:46:15 -04006763 /* Migrate the slack space for the truncate to our reserve */
6764 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
6765 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04006766 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05006767
6768 ret = btrfs_orphan_add(trans, inode);
6769 if (ret) {
6770 btrfs_end_transaction(trans, root);
Josef Bacikfcb80c22011-05-03 10:40:22 -04006771 goto out;
Josef Bacikf0cd8462011-03-04 14:37:08 -05006772 }
6773
Chris Mason5a3f23d2009-03-31 13:27:11 -04006774 /*
6775 * setattr is responsible for setting the ordered_data_close flag,
6776 * but that is only tested during the last file release. That
6777 * could happen well after the next commit, leaving a great big
6778 * window where new writes may get lost if someone chooses to write
6779 * to this file after truncating to zero
6780 *
6781 * The inode doesn't have any dirty data here, and so if we commit
6782 * this is a noop. If someone immediately starts writing to the inode
6783 * it is very likely we'll catch some of their writes in this
6784 * transaction, and the commit will find this file on the ordered
6785 * data list with good things to send down.
6786 *
6787 * This is a best effort solution, there is still a window where
6788 * using truncate to replace the contents of the file will
6789 * end up with a zero length file after a crash.
6790 */
6791 if (inode->i_size == 0 && BTRFS_I(inode)->ordered_data_close)
6792 btrfs_add_ordered_operation(trans, root, inode);
6793
Yan, Zheng80825102009-11-12 09:35:36 +00006794 while (1) {
Josef Bacik36ba0222011-10-18 12:15:48 -04006795 ret = btrfs_block_rsv_refill(root, rsv, min_size);
Josef Bacik907cbce2011-08-08 13:46:15 -04006796 if (ret) {
6797 /*
6798 * This can only happen with the original transaction we
6799 * started above, every other time we shouldn't have a
6800 * transaction started yet.
6801 */
6802 if (ret == -EAGAIN)
6803 goto end_trans;
6804 err = ret;
6805 break;
6806 }
6807
Yan, Zhengd68fc572010-05-16 10:49:58 -04006808 if (!trans) {
Josef Bacik907cbce2011-08-08 13:46:15 -04006809 /* Just need the 1 for updating the inode */
6810 trans = btrfs_start_transaction(root, 1);
Josef Bacikfcb80c22011-05-03 10:40:22 -04006811 if (IS_ERR(trans)) {
Josef Bacik7041ee92011-12-09 13:26:22 -05006812 ret = err = PTR_ERR(trans);
6813 trans = NULL;
6814 break;
Josef Bacikfcb80c22011-05-03 10:40:22 -04006815 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04006816 }
6817
Josef Bacik907cbce2011-08-08 13:46:15 -04006818 trans->block_rsv = rsv;
6819
Yan, Zheng80825102009-11-12 09:35:36 +00006820 ret = btrfs_truncate_inode_items(trans, root, inode,
6821 inode->i_size,
6822 BTRFS_EXTENT_DATA_KEY);
Josef Bacik3893e332011-01-31 16:03:11 -05006823 if (ret != -EAGAIN) {
6824 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00006825 break;
Josef Bacik3893e332011-01-31 16:03:11 -05006826 }
Chris Mason39279cc2007-06-12 06:35:45 -04006827
Josef Bacikfcb80c22011-05-03 10:40:22 -04006828 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00006829 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05006830 if (ret) {
6831 err = ret;
6832 break;
6833 }
Josef Bacik907cbce2011-08-08 13:46:15 -04006834end_trans:
Yan, Zheng80825102009-11-12 09:35:36 +00006835 nr = trans->blocks_used;
6836 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04006837 trans = NULL;
Yan, Zheng80825102009-11-12 09:35:36 +00006838 btrfs_btree_balance_dirty(root, nr);
Yan, Zheng80825102009-11-12 09:35:36 +00006839 }
6840
6841 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04006842 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00006843 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05006844 if (ret)
6845 err = ret;
Josef Bacikded5db92011-03-04 14:09:46 -05006846 } else if (ret && inode->i_nlink > 0) {
6847 /*
6848 * Failed to do the truncate, remove us from the in memory
6849 * orphan list.
6850 */
6851 ret = btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00006852 }
6853
Chris Mason917c16b2011-11-08 14:49:59 -05006854 if (trans) {
6855 trans->block_rsv = &root->fs_info->trans_block_rsv;
6856 ret = btrfs_update_inode(trans, root, inode);
6857 if (ret && !err)
6858 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04006859
Chris Mason917c16b2011-11-08 14:49:59 -05006860 nr = trans->blocks_used;
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006861 ret = btrfs_end_transaction(trans, root);
Chris Mason917c16b2011-11-08 14:49:59 -05006862 btrfs_btree_balance_dirty(root, nr);
6863 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04006864
6865out:
6866 btrfs_free_block_rsv(root, rsv);
6867
Josef Bacik3893e332011-01-31 16:03:11 -05006868 if (ret && !err)
6869 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05006870
Josef Bacik3893e332011-01-31 16:03:11 -05006871 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04006872}
6873
Sven Wegener3b963622008-06-09 21:57:42 -04006874/*
Chris Masond352ac62008-09-29 15:18:18 -04006875 * create a new subvolume directory/inode (helper for the ioctl).
6876 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05006877int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Josef Bacikd82a6f12011-05-11 15:26:06 -04006878 struct btrfs_root *new_root, u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04006879{
Chris Mason39279cc2007-06-12 06:35:45 -04006880 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04006881 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006882 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006883
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01006884 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
6885 new_dirid, new_dirid,
6886 S_IFDIR | (~current_umask() & S_IRWXUGO),
6887 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04006888 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04006889 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006890 inode->i_op = &btrfs_dir_inode_operations;
6891 inode->i_fop = &btrfs_dir_file_operations;
6892
Miklos Szeredibfe86842011-10-28 14:13:29 +02006893 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04006894 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04006895
Yan, Zheng76dda932009-09-21 16:00:26 -04006896 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04006897
Yan, Zheng76dda932009-09-21 16:00:26 -04006898 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07006899 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04006900}
6901
Chris Mason39279cc2007-06-12 06:35:45 -04006902struct inode *btrfs_alloc_inode(struct super_block *sb)
6903{
6904 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006905 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006906
6907 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
6908 if (!ei)
6909 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006910
6911 ei->root = NULL;
6912 ei->space_info = NULL;
6913 ei->generation = 0;
6914 ei->sequence = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04006915 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04006916 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04006917 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006918 ei->delalloc_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006919 ei->disk_i_size = 0;
6920 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04006921 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006922 ei->index_cnt = (u64)-1;
6923 ei->last_unlink_trans = 0;
6924
Josef Bacik9e0baf62011-07-15 15:16:44 +00006925 spin_lock_init(&ei->lock);
6926 ei->outstanding_extents = 0;
6927 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006928
6929 ei->ordered_data_close = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04006930 ei->orphan_meta_reserved = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006931 ei->dummy_inode = 0;
Chris Mason4cb53002011-05-24 15:35:30 -04006932 ei->in_defrag = 0;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05006933 ei->delalloc_meta_reserved = 0;
Li Zefan261507a02010-12-17 14:21:50 +08006934 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006935
Miao Xie16cdcec2011-04-22 18:12:22 +08006936 ei->delayed_node = NULL;
6937
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006938 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02006939 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02006940 extent_io_tree_init(&ei->io_tree, &inode->i_data);
6941 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04006942 ei->io_tree.track_uptodate = 1;
6943 ei->io_failure_tree.track_uptodate = 1;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006944 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05006945 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006946 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Josef Bacik7b128762008-07-24 12:17:14 -04006947 INIT_LIST_HEAD(&ei->i_orphan);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006948 INIT_LIST_HEAD(&ei->delalloc_inodes);
Chris Mason5a3f23d2009-03-31 13:27:11 -04006949 INIT_LIST_HEAD(&ei->ordered_operations);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006950 RB_CLEAR_NODE(&ei->rb_node);
6951
6952 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006953}
6954
Nick Pigginfa0d7e32011-01-07 17:49:49 +11006955static void btrfs_i_callback(struct rcu_head *head)
6956{
6957 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e32011-01-07 17:49:49 +11006958 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
6959}
6960
Chris Mason39279cc2007-06-12 06:35:45 -04006961void btrfs_destroy_inode(struct inode *inode)
6962{
Chris Masone6dcd2d2008-07-17 12:53:50 -04006963 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04006964 struct btrfs_root *root = BTRFS_I(inode)->root;
6965
Chris Mason39279cc2007-06-12 06:35:45 -04006966 WARN_ON(!list_empty(&inode->i_dentry));
6967 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00006968 WARN_ON(BTRFS_I(inode)->outstanding_extents);
6969 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04006970 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
6971 WARN_ON(BTRFS_I(inode)->csum_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04006972
Chris Mason5a3f23d2009-03-31 13:27:11 -04006973 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05006974 * This can happen where we create an inode, but somebody else also
6975 * created the same inode and we need to destroy the one we already
6976 * created.
6977 */
6978 if (!root)
6979 goto free;
6980
6981 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04006982 * Make sure we're properly removed from the ordered operation
6983 * lists.
6984 */
6985 smp_mb();
6986 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
6987 spin_lock(&root->fs_info->ordered_extent_lock);
6988 list_del_init(&BTRFS_I(inode)->ordered_operations);
6989 spin_unlock(&root->fs_info->ordered_extent_lock);
6990 }
6991
Yan, Zhengd68fc572010-05-16 10:49:58 -04006992 spin_lock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04006993 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
Li Zefan33345d012011-04-20 10:31:50 +08006994 printk(KERN_INFO "BTRFS: inode %llu still on the orphan list\n",
6995 (unsigned long long)btrfs_ino(inode));
Yan, Zheng80825102009-11-12 09:35:36 +00006996 list_del_init(&BTRFS_I(inode)->i_orphan);
Josef Bacik7b128762008-07-24 12:17:14 -04006997 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04006998 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04006999
Chris Masond3977122009-01-05 21:25:51 -05007000 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04007001 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
7002 if (!ordered)
7003 break;
7004 else {
Chris Masond3977122009-01-05 21:25:51 -05007005 printk(KERN_ERR "btrfs found ordered "
7006 "extent %llu %llu on inode cleanup\n",
7007 (unsigned long long)ordered->file_offset,
7008 (unsigned long long)ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007009 btrfs_remove_ordered_extent(inode, ordered);
7010 btrfs_put_ordered_extent(ordered);
7011 btrfs_put_ordered_extent(ordered);
7012 }
7013 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007014 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007015 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05007016free:
Miao Xie16cdcec2011-04-22 18:12:22 +08007017 btrfs_remove_delayed_node(inode);
Nick Pigginfa0d7e32011-01-07 17:49:49 +11007018 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04007019}
7020
Al Viro45321ac2010-06-07 13:43:19 -04007021int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04007022{
7023 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04007024
Josef Bacik0af3d002010-06-21 14:48:16 -04007025 if (btrfs_root_refs(&root->root_item) == 0 &&
Chris Mason2cf85722011-07-26 15:35:09 -04007026 !btrfs_is_free_space_inode(root, inode))
Al Viro45321ac2010-06-07 13:43:19 -04007027 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04007028 else
Al Viro45321ac2010-06-07 13:43:19 -04007029 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04007030}
7031
Sven Wegener0ee0fda2008-07-30 16:54:26 -04007032static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04007033{
7034 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
7035
7036 inode_init_once(&ei->vfs_inode);
7037}
7038
7039void btrfs_destroy_cachep(void)
7040{
7041 if (btrfs_inode_cachep)
7042 kmem_cache_destroy(btrfs_inode_cachep);
7043 if (btrfs_trans_handle_cachep)
7044 kmem_cache_destroy(btrfs_trans_handle_cachep);
7045 if (btrfs_transaction_cachep)
7046 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04007047 if (btrfs_path_cachep)
7048 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05007049 if (btrfs_free_space_cachep)
7050 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04007051}
7052
7053int btrfs_init_cachep(void)
7054{
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007055 btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
7056 sizeof(struct btrfs_inode), 0,
7057 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04007058 if (!btrfs_inode_cachep)
7059 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007060
7061 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
7062 sizeof(struct btrfs_trans_handle), 0,
7063 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007064 if (!btrfs_trans_handle_cachep)
7065 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007066
7067 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
7068 sizeof(struct btrfs_transaction), 0,
7069 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007070 if (!btrfs_transaction_cachep)
7071 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007072
7073 btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
7074 sizeof(struct btrfs_path), 0,
7075 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007076 if (!btrfs_path_cachep)
7077 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007078
Josef Bacikdc89e982011-01-28 17:05:48 -05007079 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space_cache",
7080 sizeof(struct btrfs_free_space), 0,
7081 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
7082 if (!btrfs_free_space_cachep)
7083 goto fail;
7084
Chris Mason39279cc2007-06-12 06:35:45 -04007085 return 0;
7086fail:
7087 btrfs_destroy_cachep();
7088 return -ENOMEM;
7089}
7090
7091static int btrfs_getattr(struct vfsmount *mnt,
7092 struct dentry *dentry, struct kstat *stat)
7093{
7094 struct inode *inode = dentry->d_inode;
David Sterbafadc0d82011-11-20 07:33:38 -05007095 u32 blocksize = inode->i_sb->s_blocksize;
7096
Chris Mason39279cc2007-06-12 06:35:45 -04007097 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04007098 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05007099 stat->blksize = PAGE_CACHE_SIZE;
David Sterbafadc0d82011-11-20 07:33:38 -05007100 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
7101 ALIGN(BTRFS_I(inode)->delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04007102 return 0;
7103}
7104
Liu Bo75e7cb72011-03-22 10:12:20 +00007105/*
7106 * If a file is moved, it will inherit the cow and compression flags of the new
7107 * directory.
7108 */
7109static void fixup_inode_flags(struct inode *dir, struct inode *inode)
7110{
7111 struct btrfs_inode *b_dir = BTRFS_I(dir);
7112 struct btrfs_inode *b_inode = BTRFS_I(inode);
7113
7114 if (b_dir->flags & BTRFS_INODE_NODATACOW)
7115 b_inode->flags |= BTRFS_INODE_NODATACOW;
7116 else
7117 b_inode->flags &= ~BTRFS_INODE_NODATACOW;
7118
7119 if (b_dir->flags & BTRFS_INODE_COMPRESS)
7120 b_inode->flags |= BTRFS_INODE_COMPRESS;
7121 else
7122 b_inode->flags &= ~BTRFS_INODE_COMPRESS;
7123}
7124
Chris Masond3977122009-01-05 21:25:51 -05007125static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7126 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04007127{
7128 struct btrfs_trans_handle *trans;
7129 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007130 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04007131 struct inode *new_inode = new_dentry->d_inode;
7132 struct inode *old_inode = old_dentry->d_inode;
7133 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007134 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007135 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04007136 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08007137 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007138
Li Zefan33345d012011-04-20 10:31:50 +08007139 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04007140 return -EPERM;
7141
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007142 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08007143 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05007144 return -EXDEV;
7145
Li Zefan33345d012011-04-20 10:31:50 +08007146 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
7147 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007148 return -ENOTEMPTY;
7149
Chris Mason39279cc2007-06-12 06:35:45 -04007150 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007151 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04007152 return -ENOTEMPTY;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007153 /*
7154 * we're using rename to replace one file with another.
7155 * and the replacement file is large. Start IO on it now so
7156 * we don't add too much work to the end of the transaction
7157 */
Bartlomiej Zolnierkiewicz4baf8c92009-08-07 13:47:08 -04007158 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
Chris Mason5a3f23d2009-03-31 13:27:11 -04007159 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
7160 filemap_flush(old_inode->i_mapping);
7161
Yan, Zheng76dda932009-09-21 16:00:26 -04007162 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08007163 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04007164 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04007165 /*
7166 * We want to reserve the absolute worst case amount of items. So if
7167 * both inodes are subvols and we need to unlink them then that would
7168 * require 4 item modifications, but if they are both normal inodes it
7169 * would require 5 item modifications, so we'll assume their normal
7170 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
7171 * should cover the worst case number of items we'll modify.
7172 */
7173 trans = btrfs_start_transaction(root, 20);
Johann Lombardib44c59a2011-03-31 13:23:47 +00007174 if (IS_ERR(trans)) {
7175 ret = PTR_ERR(trans);
7176 goto out_notrans;
7177 }
Chris Mason5f39d392007-10-15 16:14:19 -04007178
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007179 if (dest != root)
7180 btrfs_record_root_in_trans(trans, dest);
7181
Yan, Zhenga5719522009-09-24 09:17:31 -04007182 ret = btrfs_set_inode_index(new_dir, &index);
7183 if (ret)
7184 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007185
Li Zefan33345d012011-04-20 10:31:50 +08007186 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007187 /* force full log commit if subvolume involved. */
7188 root->fs_info->last_trans_log_full_commit = trans->transid;
7189 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04007190 ret = btrfs_insert_inode_ref(trans, dest,
7191 new_dentry->d_name.name,
7192 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08007193 old_ino,
7194 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04007195 if (ret)
7196 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007197 /*
7198 * this is an ugly little race, but the rename is required
7199 * to make sure that if we crash, the inode is either at the
7200 * old name or the new one. pinning the log transaction lets
7201 * us make sure we don't allow a log commit to come in after
7202 * we unlink the name but before we add the new name back in.
7203 */
7204 btrfs_pin_log_trans(root);
7205 }
Chris Mason12fcfd22009-03-24 10:24:20 -04007206 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04007207 * make sure the inode gets flushed if it is replacing
7208 * something.
7209 */
Li Zefan33345d012011-04-20 10:31:50 +08007210 if (new_inode && new_inode->i_size && S_ISREG(old_inode->i_mode))
Chris Mason5a3f23d2009-03-31 13:27:11 -04007211 btrfs_add_ordered_operation(trans, root, old_inode);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007212
Chris Mason39279cc2007-06-12 06:35:45 -04007213 old_dir->i_ctime = old_dir->i_mtime = ctime;
7214 new_dir->i_ctime = new_dir->i_mtime = ctime;
7215 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04007216
Chris Mason12fcfd22009-03-24 10:24:20 -04007217 if (old_dentry->d_parent != new_dentry->d_parent)
7218 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
7219
Li Zefan33345d012011-04-20 10:31:50 +08007220 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007221 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
7222 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
7223 old_dentry->d_name.name,
7224 old_dentry->d_name.len);
7225 } else {
Al Viro92986792011-03-04 17:14:37 +00007226 ret = __btrfs_unlink_inode(trans, root, old_dir,
7227 old_dentry->d_inode,
7228 old_dentry->d_name.name,
7229 old_dentry->d_name.len);
7230 if (!ret)
7231 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007232 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007233 if (ret) {
7234 btrfs_abort_transaction(trans, root, ret);
7235 goto out_fail;
7236 }
Chris Mason39279cc2007-06-12 06:35:45 -04007237
7238 if (new_inode) {
7239 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08007240 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007241 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
7242 root_objectid = BTRFS_I(new_inode)->location.objectid;
7243 ret = btrfs_unlink_subvol(trans, dest, new_dir,
7244 root_objectid,
7245 new_dentry->d_name.name,
7246 new_dentry->d_name.len);
7247 BUG_ON(new_inode->i_nlink == 0);
7248 } else {
7249 ret = btrfs_unlink_inode(trans, dest, new_dir,
7250 new_dentry->d_inode,
7251 new_dentry->d_name.name,
7252 new_dentry->d_name.len);
7253 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007254 if (!ret && new_inode->i_nlink == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04007255 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007256 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04007257 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007258 if (ret) {
7259 btrfs_abort_transaction(trans, root, ret);
7260 goto out_fail;
7261 }
Chris Mason39279cc2007-06-12 06:35:45 -04007262 }
Josef Bacikaec74772008-07-24 12:12:38 -04007263
Liu Bo75e7cb72011-03-22 10:12:20 +00007264 fixup_inode_flags(new_dir, old_inode);
7265
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007266 ret = btrfs_add_link(trans, new_dir, old_inode,
7267 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04007268 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007269 if (ret) {
7270 btrfs_abort_transaction(trans, root, ret);
7271 goto out_fail;
7272 }
Chris Mason39279cc2007-06-12 06:35:45 -04007273
Li Zefan33345d012011-04-20 10:31:50 +08007274 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04007275 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00007276 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007277 btrfs_end_log_trans(root);
7278 }
Chris Mason39279cc2007-06-12 06:35:45 -04007279out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007280 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00007281out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08007282 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04007283 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007284
Chris Mason39279cc2007-06-12 06:35:45 -04007285 return ret;
7286}
7287
Chris Masond352ac62008-09-29 15:18:18 -04007288/*
7289 * some fairly slow code that needs optimization. This walks the list
7290 * of all the inodes with pending delalloc and forces them to disk.
7291 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00007292int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
Chris Masonea8c2812008-08-04 23:17:27 -04007293{
7294 struct list_head *head = &root->fs_info->delalloc_inodes;
7295 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007296 struct inode *inode;
Chris Masonea8c2812008-08-04 23:17:27 -04007297
Yan Zhengc146afa2008-11-12 14:34:12 -05007298 if (root->fs_info->sb->s_flags & MS_RDONLY)
7299 return -EROFS;
7300
Chris Mason75eff682008-12-15 15:54:40 -05007301 spin_lock(&root->fs_info->delalloc_lock);
Chris Masond3977122009-01-05 21:25:51 -05007302 while (!list_empty(head)) {
Chris Masonea8c2812008-08-04 23:17:27 -04007303 binode = list_entry(head->next, struct btrfs_inode,
7304 delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007305 inode = igrab(&binode->vfs_inode);
7306 if (!inode)
7307 list_del_init(&binode->delalloc_inodes);
Chris Mason75eff682008-12-15 15:54:40 -05007308 spin_unlock(&root->fs_info->delalloc_lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007309 if (inode) {
Chris Mason8c8bee12008-09-29 11:19:10 -04007310 filemap_flush(inode->i_mapping);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00007311 if (delay_iput)
7312 btrfs_add_delayed_iput(inode);
7313 else
7314 iput(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007315 }
7316 cond_resched();
Chris Mason75eff682008-12-15 15:54:40 -05007317 spin_lock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04007318 }
Chris Mason75eff682008-12-15 15:54:40 -05007319 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04007320
7321 /* the filemap_flush will queue IO into the worker threads, but
7322 * we have to make sure the IO is actually started and that
7323 * ordered extents get created before we return
7324 */
7325 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05007326 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05007327 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04007328 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05007329 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
7330 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04007331 }
7332 atomic_dec(&root->fs_info->async_submit_draining);
Chris Masonea8c2812008-08-04 23:17:27 -04007333 return 0;
7334}
7335
Chris Mason39279cc2007-06-12 06:35:45 -04007336static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
7337 const char *symname)
7338{
7339 struct btrfs_trans_handle *trans;
7340 struct btrfs_root *root = BTRFS_I(dir)->root;
7341 struct btrfs_path *path;
7342 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05007343 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04007344 int err;
7345 int drop_inode = 0;
7346 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007347 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04007348 int name_len;
7349 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04007350 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04007351 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04007352 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05007353 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007354
7355 name_len = strlen(symname) + 1;
7356 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
7357 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05007358
Josef Bacik9ed74f22009-09-11 16:12:44 -04007359 /*
7360 * 2 items for inode item and ref
7361 * 2 items for dir items
7362 * 1 item for xattr if selinux is on
7363 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04007364 trans = btrfs_start_transaction(root, 5);
7365 if (IS_ERR(trans))
7366 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05007367
Li Zefan581bb052011-04-20 10:06:11 +08007368 err = btrfs_find_free_ino(root, &objectid);
7369 if (err)
7370 goto out_unlock;
7371
Josef Bacikaec74772008-07-24 12:12:38 -04007372 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08007373 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04007374 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04007375 if (IS_ERR(inode)) {
7376 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007377 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04007378 }
Chris Mason39279cc2007-06-12 06:35:45 -04007379
Eric Paris2a7dba32011-02-01 11:05:39 -05007380 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04007381 if (err) {
7382 drop_inode = 1;
7383 goto out_unlock;
7384 }
7385
Casey Schauflerad19db72011-12-15 10:09:07 -05007386 /*
7387 * If the active LSM wants to access the inode during
7388 * d_instantiate it needs these. Smack checks to see
7389 * if the filesystem supports xattrs by looking at the
7390 * ops vector.
7391 */
7392 inode->i_fop = &btrfs_file_operations;
7393 inode->i_op = &btrfs_file_inode_operations;
7394
Josef Bacika1b075d2010-11-19 20:36:11 +00007395 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04007396 if (err)
7397 drop_inode = 1;
7398 else {
7399 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04007400 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05007401 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04007402 }
Chris Mason39279cc2007-06-12 06:35:45 -04007403 if (drop_inode)
7404 goto out_unlock;
7405
7406 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07007407 if (!path) {
7408 err = -ENOMEM;
7409 drop_inode = 1;
7410 goto out_unlock;
7411 }
Li Zefan33345d012011-04-20 10:31:50 +08007412 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007413 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007414 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
7415 datasize = btrfs_file_extent_calc_inline_size(name_len);
7416 err = btrfs_insert_empty_item(trans, root, path, &key,
7417 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04007418 if (err) {
7419 drop_inode = 1;
Julia Lawallb0839162011-05-14 07:10:51 +00007420 btrfs_free_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04007421 goto out_unlock;
7422 }
Chris Mason5f39d392007-10-15 16:14:19 -04007423 leaf = path->nodes[0];
7424 ei = btrfs_item_ptr(leaf, path->slots[0],
7425 struct btrfs_file_extent_item);
7426 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
7427 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04007428 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04007429 btrfs_set_file_extent_encryption(leaf, ei, 0);
7430 btrfs_set_file_extent_compression(leaf, ei, 0);
7431 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
7432 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
7433
Chris Mason39279cc2007-06-12 06:35:45 -04007434 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04007435 write_extent_buffer(leaf, symname, ptr, name_len);
7436 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04007437 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04007438
Chris Mason39279cc2007-06-12 06:35:45 -04007439 inode->i_op = &btrfs_symlink_inode_operations;
7440 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04007441 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04007442 inode_set_bytes(inode, name_len);
Chris Masondbe674a2008-07-17 12:54:05 -04007443 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04007444 err = btrfs_update_inode(trans, root, inode);
7445 if (err)
7446 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04007447
7448out_unlock:
Al Viro08c422c2011-12-23 07:58:13 -05007449 if (!err)
7450 d_instantiate(dentry, inode);
Chris Masond3c2fdc2007-09-17 10:58:06 -04007451 nr = trans->blocks_used;
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007452 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04007453 if (drop_inode) {
7454 inode_dec_link_count(inode);
7455 iput(inode);
7456 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04007457 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04007458 return err;
7459}
Chris Mason16432982008-04-10 10:23:21 -04007460
Josef Bacik0af3d002010-06-21 14:48:16 -04007461static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
7462 u64 start, u64 num_bytes, u64 min_size,
7463 loff_t actual_len, u64 *alloc_hint,
7464 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04007465{
Yan Zhengd899e052008-10-30 14:25:28 -04007466 struct btrfs_root *root = BTRFS_I(inode)->root;
7467 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04007468 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00007469 u64 i_size;
Yan Zhengd899e052008-10-30 14:25:28 -04007470 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04007471 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04007472
Josef Bacik0af3d002010-06-21 14:48:16 -04007473 if (trans)
7474 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04007475 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04007476 if (own_trans) {
7477 trans = btrfs_start_transaction(root, 3);
7478 if (IS_ERR(trans)) {
7479 ret = PTR_ERR(trans);
7480 break;
7481 }
Yan Zhengd899e052008-10-30 14:25:28 -04007482 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00007483
Yan, Zhengefa56462010-05-16 10:49:59 -04007484 ret = btrfs_reserve_extent(trans, root, num_bytes, min_size,
Josef Bacik81c9ad22012-01-18 10:56:06 -05007485 0, *alloc_hint, &ins, 1);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007486 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04007487 if (own_trans)
7488 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04007489 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00007490 }
7491
Yan Zhengd899e052008-10-30 14:25:28 -04007492 ret = insert_reserved_file_extent(trans, inode,
7493 cur_offset, ins.objectid,
7494 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00007495 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04007496 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007497 if (ret) {
7498 btrfs_abort_transaction(trans, root, ret);
7499 if (own_trans)
7500 btrfs_end_transaction(trans, root);
7501 break;
7502 }
Chris Masona1ed8352009-09-11 12:27:37 -04007503 btrfs_drop_extent_cache(inode, cur_offset,
7504 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007505
Yan Zhengd899e052008-10-30 14:25:28 -04007506 num_bytes -= ins.offset;
7507 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04007508 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00007509
Yan Zhengd899e052008-10-30 14:25:28 -04007510 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02007511 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04007512 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04007513 (actual_len > inode->i_size) &&
7514 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00007515 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00007516 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00007517 else
Josef Bacik55a61d12010-11-22 18:50:32 +00007518 i_size = cur_offset;
7519 i_size_write(inode, i_size);
7520 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007521 }
7522
Yan Zhengd899e052008-10-30 14:25:28 -04007523 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007524
7525 if (ret) {
7526 btrfs_abort_transaction(trans, root, ret);
7527 if (own_trans)
7528 btrfs_end_transaction(trans, root);
7529 break;
7530 }
Yan Zhengd899e052008-10-30 14:25:28 -04007531
Josef Bacik0af3d002010-06-21 14:48:16 -04007532 if (own_trans)
7533 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007534 }
Yan Zhengd899e052008-10-30 14:25:28 -04007535 return ret;
7536}
7537
Josef Bacik0af3d002010-06-21 14:48:16 -04007538int btrfs_prealloc_file_range(struct inode *inode, int mode,
7539 u64 start, u64 num_bytes, u64 min_size,
7540 loff_t actual_len, u64 *alloc_hint)
7541{
7542 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
7543 min_size, actual_len, alloc_hint,
7544 NULL);
7545}
7546
7547int btrfs_prealloc_file_range_trans(struct inode *inode,
7548 struct btrfs_trans_handle *trans, int mode,
7549 u64 start, u64 num_bytes, u64 min_size,
7550 loff_t actual_len, u64 *alloc_hint)
7551{
7552 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
7553 min_size, actual_len, alloc_hint, trans);
7554}
7555
Chris Masone6dcd2d2008-07-17 12:53:50 -04007556static int btrfs_set_page_dirty(struct page *page)
7557{
Chris Masone6dcd2d2008-07-17 12:53:50 -04007558 return __set_page_dirty_nobuffers(page);
7559}
7560
Al Viro10556cb2011-06-20 19:28:19 -04007561static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05007562{
Li Zefanb83cc962010-12-20 16:04:08 +08007563 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00007564 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08007565
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00007566 if (mask & MAY_WRITE &&
7567 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
7568 if (btrfs_root_readonly(root))
7569 return -EROFS;
7570 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
7571 return -EACCES;
7572 }
Al Viro2830ba72011-06-20 19:16:29 -04007573 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05007574}
Chris Mason39279cc2007-06-12 06:35:45 -04007575
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007576static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05007577 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04007578 .lookup = btrfs_lookup,
7579 .create = btrfs_create,
7580 .unlink = btrfs_unlink,
7581 .link = btrfs_link,
7582 .mkdir = btrfs_mkdir,
7583 .rmdir = btrfs_rmdir,
7584 .rename = btrfs_rename,
7585 .symlink = btrfs_symlink,
7586 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04007587 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007588 .setxattr = btrfs_setxattr,
7589 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05007590 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007591 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05007592 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02007593 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04007594};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007595static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007596 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05007597 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02007598 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04007599};
Yan, Zheng76dda932009-09-21 16:00:26 -04007600
Alexey Dobriyan828c0952009-10-01 15:43:56 -07007601static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007602 .llseek = generic_file_llseek,
7603 .read = generic_read_dir,
David Woodhousecbdf5a22008-08-06 19:42:33 +01007604 .readdir = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04007605 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04007606#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04007607 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04007608#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04007609 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04007610 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04007611};
7612
Chris Masond1310b22008-01-24 16:13:08 -05007613static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04007614 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05007615 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04007616 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04007617 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04007618 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04007619 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05007620 .set_bit_hook = btrfs_set_bit_hook,
7621 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04007622 .merge_extent_hook = btrfs_merge_extent_hook,
7623 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04007624};
7625
Chris Mason35054392009-01-21 13:11:13 -05007626/*
7627 * btrfs doesn't support the bmap operation because swapfiles
7628 * use bmap to make a mapping of extents in the file. They assume
7629 * these extents won't change over the life of the file and they
7630 * use the bmap result to do IO directly to the drive.
7631 *
7632 * the btrfs bmap call would return logical addresses that aren't
7633 * suitable for IO and they also will change frequently as COW
7634 * operations happen. So, swapfile + btrfs == corruption.
7635 *
7636 * For now we're avoiding this by dropping bmap.
7637 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07007638static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04007639 .readpage = btrfs_readpage,
7640 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04007641 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05007642 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04007643 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04007644 .invalidatepage = btrfs_invalidatepage,
7645 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04007646 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02007647 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04007648};
7649
Alexey Dobriyan7f094102009-09-21 17:01:10 -07007650static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04007651 .readpage = btrfs_readpage,
7652 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04007653 .invalidatepage = btrfs_invalidatepage,
7654 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04007655};
7656
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007657static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007658 .getattr = btrfs_getattr,
7659 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007660 .setxattr = btrfs_setxattr,
7661 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05007662 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007663 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05007664 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007665 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02007666 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04007667};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007668static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04007669 .getattr = btrfs_getattr,
7670 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05007671 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007672 .setxattr = btrfs_setxattr,
7673 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04007674 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007675 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02007676 .get_acl = btrfs_get_acl,
Josef Bacik618e21d2007-07-11 10:18:17 -04007677};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007678static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007679 .readlink = generic_readlink,
7680 .follow_link = page_follow_link_light,
7681 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00007682 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -05007683 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05007684 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05007685 .setxattr = btrfs_setxattr,
7686 .getxattr = btrfs_getxattr,
7687 .listxattr = btrfs_listxattr,
7688 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02007689 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04007690};
Yan, Zheng76dda932009-09-21 16:00:26 -04007691
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04007692const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04007693 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -04007694 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -04007695};