blob: c0c0dc8f07fab6eb94bf4b0c9d7c8444a724fbf8 [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>
Kent Overstreeta27bb332013-05-07 16:19:08 -070035#include <linux/aio.h>
Chris Mason9ebefb182007-06-15 13:50:00 -040036#include <linux/bit_spinlock.h>
Josef Bacik5103e942007-11-16 11:45:54 -050037#include <linux/xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040038#include <linux/posix_acl.h>
Yan Zhengd899e052008-10-30 14:25:28 -040039#include <linux/falloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090040#include <linux/slab.h>
David Sterba7a36dde2011-05-06 15:33:15 +020041#include <linux/ratelimit.h>
Josef Bacik22c44fe2011-11-30 10:45:38 -050042#include <linux/mount.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000043#include <linux/btrfs.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050044#include <linux/blkdev.h>
Josef Bacikf23b5a52013-06-19 10:16:26 -040045#include <linux/posix_acl_xattr.h>
Chris Mason39279cc2007-06-12 06:35:45 -040046#include "ctree.h"
47#include "disk-io.h"
48#include "transaction.h"
49#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040050#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040051#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040052#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040053#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020054#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040055#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050056#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050057#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080058#include "inode-map.h"
Liu Bo38c227d2013-01-29 03:18:40 +000059#include "backref.h"
Josef Bacikf23b5a52013-06-19 10:16:26 -040060#include "hash.h"
Chris Mason39279cc2007-06-12 06:35:45 -040061
62struct btrfs_iget_args {
63 u64 ino;
64 struct btrfs_root *root;
65};
66
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070067static const struct inode_operations btrfs_dir_inode_operations;
68static const struct inode_operations btrfs_symlink_inode_operations;
69static const struct inode_operations btrfs_dir_ro_inode_operations;
70static const struct inode_operations btrfs_special_inode_operations;
71static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070072static const struct address_space_operations btrfs_aops;
73static const struct address_space_operations btrfs_symlink_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070074static const struct file_operations btrfs_dir_file_operations;
Chris Masond1310b22008-01-24 16:13:08 -050075static struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040076
77static struct kmem_cache *btrfs_inode_cachep;
Miao Xie8ccf6f192012-10-25 09:28:04 +000078static struct kmem_cache *btrfs_delalloc_work_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040079struct kmem_cache *btrfs_trans_handle_cachep;
80struct kmem_cache *btrfs_transaction_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040081struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050082struct kmem_cache *btrfs_free_space_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040083
84#define S_SHIFT 12
85static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
86 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
87 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
88 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
89 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
90 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
91 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
92 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
93};
94
Eric Sandeen3972f262013-01-12 02:57:22 +000095static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Josef Bacika41ad392011-01-31 15:30:16 -050096static int btrfs_truncate(struct inode *inode);
Josef Bacik5fd02042012-05-02 14:00:54 -040097static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Chris Mason771ed682008-11-06 22:02:51 -050098static noinline int cow_file_range(struct inode *inode,
99 struct page *locked_page,
100 u64 start, u64 end, int *page_started,
101 unsigned long *nr_written, int unlock);
Josef Bacik70c8a912012-10-11 16:54:30 -0400102static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
103 u64 len, u64 orig_start,
104 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -0400105 u64 orig_block_len, u64 ram_bytes,
106 int type);
Josef Bacik7b128762008-07-24 12:17:14 -0400107
Eric Sandeen48a3b632013-04-25 20:41:01 +0000108static int btrfs_dirty_inode(struct inode *inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400109
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000110static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500111 struct inode *inode, struct inode *dir,
112 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500113{
114 int err;
115
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000116 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500117 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500118 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500119 return err;
120}
121
Chris Masond352ac62008-09-29 15:18:18 -0400122/*
Chris Masonc8b97812008-10-29 14:49:59 -0400123 * this does all the hard work for inserting an inline extent into
124 * the btree. The caller should have done a btrfs_drop_extents so that
125 * no overlapping inline items exist in the btree
126 */
Chris Masond3977122009-01-05 21:25:51 -0500127static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400128 struct btrfs_root *root, struct inode *inode,
129 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000130 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400131 struct page **compressed_pages)
132{
133 struct btrfs_key key;
134 struct btrfs_path *path;
135 struct extent_buffer *leaf;
136 struct page *page = NULL;
137 char *kaddr;
138 unsigned long ptr;
139 struct btrfs_file_extent_item *ei;
140 int err = 0;
141 int ret;
142 size_t cur_size = size;
143 size_t datasize;
144 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400145
Li Zefanfe3f5662011-03-28 08:30:38 +0000146 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400147 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400148
Chris Masond3977122009-01-05 21:25:51 -0500149 path = btrfs_alloc_path();
150 if (!path)
Chris Masonc8b97812008-10-29 14:49:59 -0400151 return -ENOMEM;
152
Chris Masonb9473432009-03-13 11:00:37 -0400153 path->leave_spinning = 1;
Chris Masonc8b97812008-10-29 14:49:59 -0400154
Li Zefan33345d012011-04-20 10:31:50 +0800155 key.objectid = btrfs_ino(inode);
Chris Masonc8b97812008-10-29 14:49:59 -0400156 key.offset = start;
157 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
Chris Masonc8b97812008-10-29 14:49:59 -0400158 datasize = btrfs_file_extent_calc_inline_size(cur_size);
159
160 inode_add_bytes(inode, size);
161 ret = btrfs_insert_empty_item(trans, root, path, &key,
162 datasize);
Chris Masonc8b97812008-10-29 14:49:59 -0400163 if (ret) {
164 err = ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400165 goto fail;
166 }
167 leaf = path->nodes[0];
168 ei = btrfs_item_ptr(leaf, path->slots[0],
169 struct btrfs_file_extent_item);
170 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
171 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
172 btrfs_set_file_extent_encryption(leaf, ei, 0);
173 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
174 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
175 ptr = btrfs_file_extent_inline_start(ei);
176
Li Zefan261507a02010-12-17 14:21:50 +0800177 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400178 struct page *cpage;
179 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500180 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400181 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500182 cur_size = min_t(unsigned long, compressed_size,
Chris Masonc8b97812008-10-29 14:49:59 -0400183 PAGE_CACHE_SIZE);
184
Cong Wang7ac687d2011-11-25 23:14:28 +0800185 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400186 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800187 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400188
189 i++;
190 ptr += cur_size;
191 compressed_size -= cur_size;
192 }
193 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800194 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400195 } else {
196 page = find_get_page(inode->i_mapping,
197 start >> PAGE_CACHE_SHIFT);
198 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800199 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400200 offset = start & (PAGE_CACHE_SIZE - 1);
201 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800202 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400203 page_cache_release(page);
204 }
205 btrfs_mark_buffer_dirty(leaf);
206 btrfs_free_path(path);
207
Yan, Zhengc2167752009-11-12 09:34:21 +0000208 /*
209 * we're an inline extent, so nobody can
210 * extend the file past i_size without locking
211 * a page we already have locked.
212 *
213 * We must do any isize and inode updates
214 * before we unlock the pages. Otherwise we
215 * could end up racing with unlink.
216 */
Chris Masonc8b97812008-10-29 14:49:59 -0400217 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100218 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000219
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100220 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400221fail:
222 btrfs_free_path(path);
223 return err;
224}
225
226
227/*
228 * conditionally insert an inline extent into the file. This
229 * does the checks required to make sure the data is small enough
230 * to fit as an inline extent.
231 */
Josef Bacik00361582013-08-14 14:02:47 -0400232static noinline int cow_file_range_inline(struct btrfs_root *root,
233 struct inode *inode, u64 start,
234 u64 end, size_t compressed_size,
235 int compress_type,
236 struct page **compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400237{
Josef Bacik00361582013-08-14 14:02:47 -0400238 struct btrfs_trans_handle *trans;
Chris Masonc8b97812008-10-29 14:49:59 -0400239 u64 isize = i_size_read(inode);
240 u64 actual_end = min(end + 1, isize);
241 u64 inline_len = actual_end - start;
Qu Wenruofda28322013-02-26 08:10:22 +0000242 u64 aligned_end = ALIGN(end, root->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400243 u64 data_len = inline_len;
244 int ret;
245
246 if (compressed_size)
247 data_len = compressed_size;
248
249 if (start > 0 ||
Chris Mason70b99e62008-10-31 12:46:39 -0400250 actual_end >= PAGE_CACHE_SIZE ||
Chris Masonc8b97812008-10-29 14:49:59 -0400251 data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
252 (!compressed_size &&
253 (actual_end & (root->sectorsize - 1)) == 0) ||
254 end + 1 < isize ||
255 data_len > root->fs_info->max_inline) {
256 return 1;
257 }
258
Josef Bacik00361582013-08-14 14:02:47 -0400259 trans = btrfs_join_transaction(root);
260 if (IS_ERR(trans))
261 return PTR_ERR(trans);
262 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
263
Josef Bacik26714852012-08-29 12:24:27 -0400264 ret = btrfs_drop_extents(trans, root, inode, start, aligned_end, 1);
Josef Bacik00361582013-08-14 14:02:47 -0400265 if (ret) {
266 btrfs_abort_transaction(trans, root, ret);
267 goto out;
268 }
Chris Masonc8b97812008-10-29 14:49:59 -0400269
270 if (isize > actual_end)
271 inline_len = min_t(u64, isize, actual_end);
272 ret = insert_inline_extent(trans, root, inode, start,
273 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000274 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400275 if (ret && ret != -ENOSPC) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100276 btrfs_abort_transaction(trans, root, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400277 goto out;
Josef Bacik2adcac12012-05-23 16:10:14 -0400278 } else if (ret == -ENOSPC) {
Josef Bacik00361582013-08-14 14:02:47 -0400279 ret = 1;
280 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100281 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400282
Josef Bacikbdc20e62013-02-28 13:23:38 -0500283 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400284 btrfs_delalloc_release_metadata(inode, end + 1 - start);
Chris Masona1ed8352009-09-11 12:27:37 -0400285 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
Josef Bacik00361582013-08-14 14:02:47 -0400286out:
287 btrfs_end_transaction(trans, root);
288 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400289}
290
Chris Mason771ed682008-11-06 22:02:51 -0500291struct async_extent {
292 u64 start;
293 u64 ram_size;
294 u64 compressed_size;
295 struct page **pages;
296 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800297 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500298 struct list_head list;
299};
300
301struct async_cow {
302 struct inode *inode;
303 struct btrfs_root *root;
304 struct page *locked_page;
305 u64 start;
306 u64 end;
307 struct list_head extents;
308 struct btrfs_work work;
309};
310
311static noinline int add_async_extent(struct async_cow *cow,
312 u64 start, u64 ram_size,
313 u64 compressed_size,
314 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800315 unsigned long nr_pages,
316 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500317{
318 struct async_extent *async_extent;
319
320 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100321 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500322 async_extent->start = start;
323 async_extent->ram_size = ram_size;
324 async_extent->compressed_size = compressed_size;
325 async_extent->pages = pages;
326 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800327 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500328 list_add_tail(&async_extent->list, &cow->extents);
329 return 0;
330}
331
Chris Masonc8b97812008-10-29 14:49:59 -0400332/*
Chris Mason771ed682008-11-06 22:02:51 -0500333 * we create compressed extents in two phases. The first
334 * phase compresses a range of pages that have already been
335 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400336 *
Chris Mason771ed682008-11-06 22:02:51 -0500337 * This is done inside an ordered work queue, and the compression
338 * is spread across many cpus. The actual IO submission is step
339 * two, and the ordered work queue takes care of making sure that
340 * happens in the same order things were put onto the queue by
341 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400342 *
Chris Mason771ed682008-11-06 22:02:51 -0500343 * If this code finds it can't get good compression, it puts an
344 * entry onto the work queue to write the uncompressed bytes. This
345 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300346 * are written in the same order that the flusher thread sent them
347 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400348 */
Chris Mason771ed682008-11-06 22:02:51 -0500349static noinline int compress_file_range(struct inode *inode,
350 struct page *locked_page,
351 u64 start, u64 end,
352 struct async_cow *async_cow,
353 int *num_added)
Chris Masonb888db2b2007-08-27 16:49:44 -0400354{
355 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masondb945352007-10-15 16:15:53 -0400356 u64 num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400357 u64 blocksize = root->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400358 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500359 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400360 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400361 struct page **pages = NULL;
362 unsigned long nr_pages;
363 unsigned long nr_pages_ret = 0;
364 unsigned long total_compressed = 0;
365 unsigned long total_in = 0;
366 unsigned long max_compressed = 128 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -0500367 unsigned long max_uncompressed = 128 * 1024;
Chris Masonc8b97812008-10-29 14:49:59 -0400368 int i;
369 int will_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800370 int compress_type = root->fs_info->compress_type;
Chris Mason4adaa612013-03-26 13:07:00 -0400371 int redirty = 0;
Chris Masonb888db2b2007-08-27 16:49:44 -0400372
Liu Bo4cb13e52012-03-29 09:57:45 -0400373 /* if this is a small write inside eof, kick off a defrag */
374 if ((end - start + 1) < 16 * 1024 &&
375 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400376 btrfs_add_inode_defrag(NULL, inode);
377
Chris Mason42dc7ba2008-12-15 11:44:56 -0500378 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400379again:
380 will_compress = 0;
381 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
382 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
383
Chris Masonf03d9301f2009-02-04 09:31:06 -0500384 /*
385 * we don't want to send crud past the end of i_size through
386 * compression, that's just a waste of CPU time. So, if the
387 * end of the file is before the start of our current
388 * requested range of bytes, we bail out to the uncompressed
389 * cleanup code that can deal with all of this.
390 *
391 * It isn't really the fastest way to fix things, but this is a
392 * very uncommon corner.
393 */
394 if (actual_end <= start)
395 goto cleanup_and_bail_uncompressed;
396
Chris Masonc8b97812008-10-29 14:49:59 -0400397 total_compressed = actual_end - start;
398
399 /* we want to make sure that amount of ram required to uncompress
400 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500401 * of a compressed extent to 128k. This is a crucial number
402 * because it also controls how easily we can spread reads across
403 * cpus for decompression.
404 *
405 * We also want to make sure the amount of IO required to do
406 * a random read is reasonably small, so we limit the size of
407 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400408 */
409 total_compressed = min(total_compressed, max_uncompressed);
Qu Wenruofda28322013-02-26 08:10:22 +0000410 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Masonbe20aa92007-12-17 20:14:01 -0500411 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400412 total_in = 0;
413 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400414
Chris Mason771ed682008-11-06 22:02:51 -0500415 /*
416 * we do compression for mount -o compress and when the
417 * inode has not been flagged as nocompress. This flag can
418 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400419 */
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200420 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) &&
Chris Mason1e701a32010-03-11 09:42:04 -0500421 (btrfs_test_opt(root, COMPRESS) ||
Liu Bo75e7cb72011-03-22 10:12:20 +0000422 (BTRFS_I(inode)->force_compress) ||
423 (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS))) {
Chris Masonc8b97812008-10-29 14:49:59 -0400424 WARN_ON(pages);
Chris Masoncfbc2462008-10-30 13:22:14 -0400425 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800426 if (!pages) {
427 /* just bail out to the uncompressed code */
428 goto cont;
429 }
Chris Mason179e29e2007-11-01 11:28:41 -0400430
Li Zefan261507a02010-12-17 14:21:50 +0800431 if (BTRFS_I(inode)->force_compress)
432 compress_type = BTRFS_I(inode)->force_compress;
433
Chris Mason4adaa612013-03-26 13:07:00 -0400434 /*
435 * we need to call clear_page_dirty_for_io on each
436 * page in the range. Otherwise applications with the file
437 * mmap'd can wander in and change the page contents while
438 * we are compressing them.
439 *
440 * If the compression fails for any reason, we set the pages
441 * dirty again later on.
442 */
443 extent_range_clear_dirty_for_io(inode, start, end);
444 redirty = 1;
Li Zefan261507a02010-12-17 14:21:50 +0800445 ret = btrfs_compress_pages(compress_type,
446 inode->i_mapping, start,
447 total_compressed, pages,
448 nr_pages, &nr_pages_ret,
449 &total_in,
450 &total_compressed,
451 max_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400452
453 if (!ret) {
454 unsigned long offset = total_compressed &
455 (PAGE_CACHE_SIZE - 1);
456 struct page *page = pages[nr_pages_ret - 1];
457 char *kaddr;
458
459 /* zero the tail end of the last page, we might be
460 * sending it down to disk
461 */
462 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800463 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400464 memset(kaddr + offset, 0,
465 PAGE_CACHE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800466 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400467 }
468 will_compress = 1;
469 }
470 }
Li Zefan560f7d72011-09-08 10:22:01 +0800471cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400472 if (start == 0) {
473 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500474 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400475 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500476 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400477 */
Josef Bacik00361582013-08-14 14:02:47 -0400478 ret = cow_file_range_inline(root, inode, start, end,
479 0, 0, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400480 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500481 /* try making a compressed inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400482 ret = cow_file_range_inline(root, inode, start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000483 total_compressed,
484 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400485 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100486 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400487 unsigned long clear_flags = EXTENT_DELALLOC |
488 EXTENT_DEFRAG;
489 clear_flags |= (ret < 0) ? EXTENT_DO_ACCOUNTING : 0;
490
Chris Mason771ed682008-11-06 22:02:51 -0500491 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100492 * inline extent creation worked or returned error,
493 * we don't need to create any more async work items.
494 * Unlock and free up our temp pages.
Chris Mason771ed682008-11-06 22:02:51 -0500495 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400496 extent_clear_unlock_delalloc(inode, start, end, NULL,
Josef Bacik151a41b2013-07-29 13:22:24 -0400497 clear_flags, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400498 PAGE_CLEAR_DIRTY |
499 PAGE_SET_WRITEBACK |
500 PAGE_END_WRITEBACK);
Chris Masonc8b97812008-10-29 14:49:59 -0400501 goto free_pages_out;
502 }
503 }
504
505 if (will_compress) {
506 /*
507 * we aren't doing an inline extent round the compressed size
508 * up to a block size boundary so the allocator does sane
509 * things
510 */
Qu Wenruofda28322013-02-26 08:10:22 +0000511 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400512
513 /*
514 * one last check to make sure the compression is really a
515 * win, compare the page count read with the blocks on disk
516 */
Qu Wenruofda28322013-02-26 08:10:22 +0000517 total_in = ALIGN(total_in, PAGE_CACHE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400518 if (total_compressed >= total_in) {
519 will_compress = 0;
520 } else {
Chris Masonc8b97812008-10-29 14:49:59 -0400521 num_bytes = total_in;
522 }
523 }
524 if (!will_compress && pages) {
525 /*
526 * the compression code ran but failed to make things smaller,
527 * free any pages it allocated and our page pointer array
528 */
529 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400530 WARN_ON(pages[i]->mapping);
Chris Masonc8b97812008-10-29 14:49:59 -0400531 page_cache_release(pages[i]);
532 }
533 kfree(pages);
534 pages = NULL;
535 total_compressed = 0;
536 nr_pages_ret = 0;
537
538 /* flag the file so we don't compress in the future */
Chris Mason1e701a32010-03-11 09:42:04 -0500539 if (!btrfs_test_opt(root, FORCE_COMPRESS) &&
540 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500541 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500542 }
Chris Masonc8b97812008-10-29 14:49:59 -0400543 }
Chris Mason771ed682008-11-06 22:02:51 -0500544 if (will_compress) {
545 *num_added += 1;
546
547 /* the async work queues will take care of doing actual
548 * allocation on disk for these compressed pages,
549 * and will submit them to the elevator.
550 */
551 add_async_extent(async_cow, start, num_bytes,
Li Zefan261507a02010-12-17 14:21:50 +0800552 total_compressed, pages, nr_pages_ret,
553 compress_type);
Chris Mason771ed682008-11-06 22:02:51 -0500554
Yan, Zheng24ae6362010-12-06 07:02:36 +0000555 if (start + num_bytes < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500556 start += num_bytes;
557 pages = NULL;
558 cond_resched();
559 goto again;
560 }
561 } else {
Chris Masonf03d9301f2009-02-04 09:31:06 -0500562cleanup_and_bail_uncompressed:
Chris Mason771ed682008-11-06 22:02:51 -0500563 /*
564 * No compression, but we still need to write the pages in
565 * the file we've been given so far. redirty the locked
566 * page if it corresponds to our extent and set things up
567 * for the async work queue to run cow_file_range to do
568 * the normal delalloc dance
569 */
570 if (page_offset(locked_page) >= start &&
571 page_offset(locked_page) <= end) {
572 __set_page_dirty_nobuffers(locked_page);
573 /* unlocked later on in the async handlers */
574 }
Chris Mason4adaa612013-03-26 13:07:00 -0400575 if (redirty)
576 extent_range_redirty_for_io(inode, start, end);
Li Zefan261507a02010-12-17 14:21:50 +0800577 add_async_extent(async_cow, start, end - start + 1,
578 0, NULL, 0, BTRFS_COMPRESS_NONE);
Chris Mason771ed682008-11-06 22:02:51 -0500579 *num_added += 1;
580 }
581
582out:
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100583 return ret;
Chris Mason771ed682008-11-06 22:02:51 -0500584
585free_pages_out:
586 for (i = 0; i < nr_pages_ret; i++) {
587 WARN_ON(pages[i]->mapping);
588 page_cache_release(pages[i]);
589 }
Chris Masond3977122009-01-05 21:25:51 -0500590 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500591
592 goto out;
593}
594
595/*
596 * phase two of compressed writeback. This is the ordered portion
597 * of the code, which only gets called in the order the work was
598 * queued. We walk all the async extents created by compress_file_range
599 * and send them down to the disk.
600 */
601static noinline int submit_compressed_extents(struct inode *inode,
602 struct async_cow *async_cow)
603{
604 struct async_extent *async_extent;
605 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500606 struct btrfs_key ins;
607 struct extent_map *em;
608 struct btrfs_root *root = BTRFS_I(inode)->root;
609 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
610 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500611 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500612
613 if (list_empty(&async_cow->extents))
614 return 0;
615
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500616again:
Chris Masond3977122009-01-05 21:25:51 -0500617 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500618 async_extent = list_entry(async_cow->extents.next,
619 struct async_extent, list);
620 list_del(&async_extent->list);
621
622 io_tree = &BTRFS_I(inode)->io_tree;
623
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500624retry:
Chris Mason771ed682008-11-06 22:02:51 -0500625 /* did the compression code fall back to uncompressed IO? */
626 if (!async_extent->pages) {
627 int page_started = 0;
628 unsigned long nr_written = 0;
629
630 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000631 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100632 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500633
634 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500635 ret = cow_file_range(inode, async_cow->locked_page,
636 async_extent->start,
637 async_extent->start +
638 async_extent->ram_size - 1,
639 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500640
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100641 /* JDM XXX */
642
Chris Mason771ed682008-11-06 22:02:51 -0500643 /*
644 * if page_started, cow_file_range inserted an
645 * inline extent and took care of all the unlocking
646 * and IO for us. Otherwise, we need to submit
647 * all those pages down to the drive.
648 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500649 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500650 extent_write_locked_range(io_tree,
651 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500652 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500653 async_extent->ram_size - 1,
654 btrfs_get_extent,
655 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500656 else if (ret)
657 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500658 kfree(async_extent);
659 cond_resched();
660 continue;
661 }
662
663 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100664 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500665
Josef Bacik00361582013-08-14 14:02:47 -0400666 ret = btrfs_reserve_extent(root,
Chris Mason771ed682008-11-06 22:02:51 -0500667 async_extent->compressed_size,
668 async_extent->compressed_size,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500669 0, alloc_hint, &ins, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500670 if (ret) {
671 int i;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500672
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500673 for (i = 0; i < async_extent->nr_pages; i++) {
674 WARN_ON(async_extent->pages[i]->mapping);
675 page_cache_release(async_extent->pages[i]);
676 }
677 kfree(async_extent->pages);
678 async_extent->nr_pages = 0;
679 async_extent->pages = NULL;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500680
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400681 if (ret == -ENOSPC) {
682 unlock_extent(io_tree, async_extent->start,
683 async_extent->start +
684 async_extent->ram_size - 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100685 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400686 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500687 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500688 }
689
Yan, Zhengc2167752009-11-12 09:34:21 +0000690 /*
691 * here we're doing allocation and writeback of the
692 * compressed pages
693 */
694 btrfs_drop_extent_cache(inode, async_extent->start,
695 async_extent->start +
696 async_extent->ram_size - 1, 0);
697
David Sterba172ddd62011-04-21 00:48:27 +0200698 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000699 if (!em) {
700 ret = -ENOMEM;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500701 goto out_free_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000702 }
Chris Mason771ed682008-11-06 22:02:51 -0500703 em->start = async_extent->start;
704 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500705 em->orig_start = em->start;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400706 em->mod_start = em->start;
707 em->mod_len = em->len;
Chris Mason771ed682008-11-06 22:02:51 -0500708
709 em->block_start = ins.objectid;
710 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500711 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400712 em->ram_bytes = async_extent->ram_size;
Chris Mason771ed682008-11-06 22:02:51 -0500713 em->bdev = root->fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800714 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500715 set_bit(EXTENT_FLAG_PINNED, &em->flags);
716 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400717 em->generation = -1;
Chris Mason771ed682008-11-06 22:02:51 -0500718
Chris Masond3977122009-01-05 21:25:51 -0500719 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400720 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400721 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400722 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500723 if (ret != -EEXIST) {
724 free_extent_map(em);
725 break;
726 }
727 btrfs_drop_extent_cache(inode, async_extent->start,
728 async_extent->start +
729 async_extent->ram_size - 1, 0);
730 }
731
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500732 if (ret)
733 goto out_free_reserve;
734
Li Zefan261507a02010-12-17 14:21:50 +0800735 ret = btrfs_add_ordered_extent_compress(inode,
736 async_extent->start,
737 ins.objectid,
738 async_extent->ram_size,
739 ins.offset,
740 BTRFS_ORDERED_COMPRESSED,
741 async_extent->compress_type);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500742 if (ret)
743 goto out_free_reserve;
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 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400748 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400749 async_extent->start +
750 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400751 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
752 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400753 PAGE_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500754 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500755 async_extent->start,
756 async_extent->ram_size,
757 ins.objectid,
758 ins.offset, async_extent->pages,
759 async_extent->nr_pages);
Chris Mason771ed682008-11-06 22:02:51 -0500760 alloc_hint = ins.objectid + ins.offset;
761 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500762 if (ret)
763 goto out;
Chris Mason771ed682008-11-06 22:02:51 -0500764 cond_resched();
765 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100766 ret = 0;
767out:
768 return ret;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500769out_free_reserve:
770 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100771out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400772 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500773 async_extent->start +
774 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400775 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400776 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
777 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
778 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100779 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500780 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500781}
782
Josef Bacik4b46fce2010-05-23 11:00:55 -0400783static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
784 u64 num_bytes)
785{
786 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
787 struct extent_map *em;
788 u64 alloc_hint = 0;
789
790 read_lock(&em_tree->lock);
791 em = search_extent_mapping(em_tree, start, num_bytes);
792 if (em) {
793 /*
794 * if block start isn't an actual block number then find the
795 * first block in this inode and use that as a hint. If that
796 * block is also bogus then just don't worry about it.
797 */
798 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
799 free_extent_map(em);
800 em = search_extent_mapping(em_tree, 0, 0);
801 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
802 alloc_hint = em->block_start;
803 if (em)
804 free_extent_map(em);
805 } else {
806 alloc_hint = em->block_start;
807 free_extent_map(em);
808 }
809 }
810 read_unlock(&em_tree->lock);
811
812 return alloc_hint;
813}
814
Chris Mason771ed682008-11-06 22:02:51 -0500815/*
816 * when extent_io.c finds a delayed allocation range in the file,
817 * the call backs end up in this code. The basic idea is to
818 * allocate extents on disk for the range, and create ordered data structs
819 * in ram to track those extents.
820 *
821 * locked_page is the page that writepage had locked already. We use
822 * it to make sure we don't do extra locks or unlocks.
823 *
824 * *page_started is set to one if we unlock locked_page and do everything
825 * required to start IO on it. It may be clean and already done with
826 * IO when we return.
827 */
Josef Bacik00361582013-08-14 14:02:47 -0400828static noinline int cow_file_range(struct inode *inode,
829 struct page *locked_page,
830 u64 start, u64 end, int *page_started,
831 unsigned long *nr_written,
832 int unlock)
Chris Mason771ed682008-11-06 22:02:51 -0500833{
Josef Bacik00361582013-08-14 14:02:47 -0400834 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500835 u64 alloc_hint = 0;
836 u64 num_bytes;
837 unsigned long ram_size;
838 u64 disk_num_bytes;
839 u64 cur_alloc_size;
840 u64 blocksize = root->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500841 struct btrfs_key ins;
842 struct extent_map *em;
843 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
844 int ret = 0;
845
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400846 if (btrfs_is_free_space_inode(inode)) {
847 WARN_ON_ONCE(1);
848 return -EINVAL;
849 }
Chris Mason771ed682008-11-06 22:02:51 -0500850
Qu Wenruofda28322013-02-26 08:10:22 +0000851 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500852 num_bytes = max(blocksize, num_bytes);
853 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500854
Chris Mason4cb53002011-05-24 15:35:30 -0400855 /* if this is a small write inside eof, kick off defrag */
Liu Bo4cb13e52012-03-29 09:57:45 -0400856 if (num_bytes < 64 * 1024 &&
857 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Josef Bacik00361582013-08-14 14:02:47 -0400858 btrfs_add_inode_defrag(NULL, inode);
Chris Mason4cb53002011-05-24 15:35:30 -0400859
Chris Mason771ed682008-11-06 22:02:51 -0500860 if (start == 0) {
861 /* lets try to make an inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400862 ret = cow_file_range_inline(root, inode, start, end, 0, 0,
863 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500864 if (ret == 0) {
Josef Bacikc2790a22013-07-29 11:20:47 -0400865 extent_clear_unlock_delalloc(inode, start, end, NULL,
866 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400867 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400868 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
869 PAGE_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000870
Chris Mason771ed682008-11-06 22:02:51 -0500871 *nr_written = *nr_written +
872 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
873 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500874 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100875 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100876 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500877 }
878 }
Chris Masonc8b97812008-10-29 14:49:59 -0400879
880 BUG_ON(disk_num_bytes >
David Sterba6c417612011-04-13 15:41:04 +0200881 btrfs_super_total_bytes(root->fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400882
Josef Bacik4b46fce2010-05-23 11:00:55 -0400883 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400884 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400885
Chris Masond3977122009-01-05 21:25:51 -0500886 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400887 unsigned long op;
888
Josef Bacik287a0ab2010-03-19 18:07:23 +0000889 cur_alloc_size = disk_num_bytes;
Josef Bacik00361582013-08-14 14:02:47 -0400890 ret = btrfs_reserve_extent(root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500891 root->sectorsize, 0, alloc_hint,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500892 &ins, 1);
Josef Bacik00361582013-08-14 14:02:47 -0400893 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100894 goto out_unlock;
Chris Masond3977122009-01-05 21:25:51 -0500895
David Sterba172ddd62011-04-21 00:48:27 +0200896 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000897 if (!em) {
898 ret = -ENOMEM;
Liu Boace68ba2013-04-22 10:53:47 +0000899 goto out_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000900 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400901 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500902 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500903 ram_size = ins.offset;
904 em->len = ins.offset;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400905 em->mod_start = em->start;
906 em->mod_len = em->len;
Chris Masonc8b97812008-10-29 14:49:59 -0400907
Chris Masone6dcd2d2008-07-17 12:53:50 -0400908 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400909 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500910 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400911 em->ram_bytes = ram_size;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400912 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400913 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400914 em->generation = -1;
Chris Masonc8b97812008-10-29 14:49:59 -0400915
Chris Masond3977122009-01-05 21:25:51 -0500916 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400917 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400918 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400919 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400920 if (ret != -EEXIST) {
921 free_extent_map(em);
922 break;
923 }
924 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400925 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400926 }
Liu Boace68ba2013-04-22 10:53:47 +0000927 if (ret)
928 goto out_reserve;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400929
Chris Mason98d20f62008-04-14 09:46:10 -0400930 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400931 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -0500932 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +0000933 if (ret)
934 goto out_reserve;
Chris Masonc8b97812008-10-29 14:49:59 -0400935
Yan Zheng17d217f2008-12-12 10:03:38 -0500936 if (root->root_key.objectid ==
937 BTRFS_DATA_RELOC_TREE_OBJECTID) {
938 ret = btrfs_reloc_clone_csums(inode, start,
939 cur_alloc_size);
Josef Bacik00361582013-08-14 14:02:47 -0400940 if (ret)
Liu Boace68ba2013-04-22 10:53:47 +0000941 goto out_reserve;
Yan Zheng17d217f2008-12-12 10:03:38 -0500942 }
943
Chris Masond3977122009-01-05 21:25:51 -0500944 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -0400945 break;
Chris Masond3977122009-01-05 21:25:51 -0500946
Chris Masonc8b97812008-10-29 14:49:59 -0400947 /* we're not doing compressed IO, don't unlock the first
948 * page (which the caller expects to stay locked), don't
949 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -0400950 *
951 * Do set the Private2 bit so we know this page was properly
952 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -0400953 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400954 op = unlock ? PAGE_UNLOCK : 0;
955 op |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -0400956
Josef Bacikc2790a22013-07-29 11:20:47 -0400957 extent_clear_unlock_delalloc(inode, start,
958 start + ram_size - 1, locked_page,
959 EXTENT_LOCKED | EXTENT_DELALLOC,
960 op);
Chris Masonc8b97812008-10-29 14:49:59 -0400961 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -0500962 num_bytes -= cur_alloc_size;
963 alloc_hint = ins.objectid + ins.offset;
964 start += cur_alloc_size;
Chris Masonb888db2b2007-08-27 16:49:44 -0400965 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100966out:
Chris Masonbe20aa92007-12-17 20:14:01 -0500967 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +0000968
Liu Boace68ba2013-04-22 10:53:47 +0000969out_reserve:
970 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100971out_unlock:
Josef Bacikc2790a22013-07-29 11:20:47 -0400972 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Josef Bacik151a41b2013-07-29 13:22:24 -0400973 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
974 EXTENT_DELALLOC | EXTENT_DEFRAG,
975 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
976 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100977 goto out;
Chris Mason771ed682008-11-06 22:02:51 -0500978}
Chris Masonc8b97812008-10-29 14:49:59 -0400979
Chris Mason771ed682008-11-06 22:02:51 -0500980/*
981 * work queue call back to started compression on a file and pages
982 */
983static noinline void async_cow_start(struct btrfs_work *work)
984{
985 struct async_cow *async_cow;
986 int num_added = 0;
987 async_cow = container_of(work, struct async_cow, work);
988
989 compress_file_range(async_cow->inode, async_cow->locked_page,
990 async_cow->start, async_cow->end, async_cow,
991 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -0400992 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -0600993 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -0500994 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -0400995 }
Chris Mason771ed682008-11-06 22:02:51 -0500996}
997
998/*
999 * work queue call back to submit previously compressed pages
1000 */
1001static noinline void async_cow_submit(struct btrfs_work *work)
1002{
1003 struct async_cow *async_cow;
1004 struct btrfs_root *root;
1005 unsigned long nr_pages;
1006
1007 async_cow = container_of(work, struct async_cow, work);
1008
1009 root = async_cow->root;
1010 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
1011 PAGE_CACHE_SHIFT;
1012
Josef Bacik66657b32012-08-01 15:36:24 -04001013 if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) <
Liu Bo287082b2012-06-28 04:02:24 -06001014 5 * 1024 * 1024 &&
Chris Mason771ed682008-11-06 22:02:51 -05001015 waitqueue_active(&root->fs_info->async_submit_wait))
1016 wake_up(&root->fs_info->async_submit_wait);
1017
Chris Masond3977122009-01-05 21:25:51 -05001018 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001019 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001020}
Chris Masonc8b97812008-10-29 14:49:59 -04001021
Chris Mason771ed682008-11-06 22:02:51 -05001022static noinline void async_cow_free(struct btrfs_work *work)
1023{
1024 struct async_cow *async_cow;
1025 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001026 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001027 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001028 kfree(async_cow);
1029}
1030
1031static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1032 u64 start, u64 end, int *page_started,
1033 unsigned long *nr_written)
1034{
1035 struct async_cow *async_cow;
1036 struct btrfs_root *root = BTRFS_I(inode)->root;
1037 unsigned long nr_pages;
1038 u64 cur_end;
Liu Bo287082b2012-06-28 04:02:24 -06001039 int limit = 10 * 1024 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -05001040
Chris Masona3429ab2009-10-08 12:30:20 -04001041 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1042 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001043 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001044 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001045 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001046 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001047 async_cow->root = root;
1048 async_cow->locked_page = locked_page;
1049 async_cow->start = start;
1050
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001051 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
Chris Mason771ed682008-11-06 22:02:51 -05001052 cur_end = end;
1053 else
1054 cur_end = min(end, start + 512 * 1024 - 1);
1055
1056 async_cow->end = cur_end;
1057 INIT_LIST_HEAD(&async_cow->extents);
1058
1059 async_cow->work.func = async_cow_start;
1060 async_cow->work.ordered_func = async_cow_submit;
1061 async_cow->work.ordered_free = async_cow_free;
1062 async_cow->work.flags = 0;
1063
Chris Mason771ed682008-11-06 22:02:51 -05001064 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
1065 PAGE_CACHE_SHIFT;
1066 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
1067
1068 btrfs_queue_worker(&root->fs_info->delalloc_workers,
1069 &async_cow->work);
1070
1071 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
1072 wait_event(root->fs_info->async_submit_wait,
1073 (atomic_read(&root->fs_info->async_delalloc_pages) <
1074 limit));
1075 }
1076
Chris Masond3977122009-01-05 21:25:51 -05001077 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -05001078 atomic_read(&root->fs_info->async_delalloc_pages)) {
1079 wait_event(root->fs_info->async_submit_wait,
1080 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1081 0));
1082 }
1083
1084 *nr_written += nr_pages;
1085 start = cur_end + 1;
1086 }
1087 *page_started = 1;
1088 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001089}
1090
Chris Masond3977122009-01-05 21:25:51 -05001091static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001092 u64 bytenr, u64 num_bytes)
1093{
1094 int ret;
1095 struct btrfs_ordered_sum *sums;
1096 LIST_HEAD(list);
1097
Yan Zheng07d400a2009-01-06 11:42:00 -05001098 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001099 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001100 if (ret == 0 && list_empty(&list))
1101 return 0;
1102
1103 while (!list_empty(&list)) {
1104 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1105 list_del(&sums->list);
1106 kfree(sums);
1107 }
1108 return 1;
1109}
1110
Chris Masond352ac62008-09-29 15:18:18 -04001111/*
1112 * when nowcow writeback call back. This checks for snapshots or COW copies
1113 * of the extents that exist in the file, and COWs the file as required.
1114 *
1115 * If no cow copies or snapshots exist, we write directly to the existing
1116 * blocks on disk
1117 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001118static noinline int run_delalloc_nocow(struct inode *inode,
1119 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001120 u64 start, u64 end, int *page_started, int force,
1121 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001122{
Chris Masonbe20aa92007-12-17 20:14:01 -05001123 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001124 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001125 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001126 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001127 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001128 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001129 u64 cow_start;
1130 u64 cur_offset;
1131 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001132 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001133 u64 disk_bytenr;
1134 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001135 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001136 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001137 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001138 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001139 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001140 int nocow;
1141 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001142 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001143 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001144
1145 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001146 if (!path) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001147 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1148 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001149 EXTENT_DO_ACCOUNTING |
1150 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001151 PAGE_CLEAR_DIRTY |
1152 PAGE_SET_WRITEBACK |
1153 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001154 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001155 }
Li Zefan82d59022011-04-20 10:33:24 +08001156
Liu Bo83eea1f2012-07-10 05:28:39 -06001157 nolock = btrfs_is_free_space_inode(inode);
Li Zefan82d59022011-04-20 10:33:24 +08001158
1159 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001160 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001161 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001162 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001163
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001164 if (IS_ERR(trans)) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001165 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1166 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001167 EXTENT_DO_ACCOUNTING |
1168 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001169 PAGE_CLEAR_DIRTY |
1170 PAGE_SET_WRITEBACK |
1171 PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001172 btrfs_free_path(path);
1173 return PTR_ERR(trans);
1174 }
1175
Josef Bacik74b21072011-04-13 12:02:53 -04001176 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001177
Yan Zheng80ff3852008-10-30 14:20:02 -04001178 cow_start = (u64)-1;
1179 cur_offset = start;
1180 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001181 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001182 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001183 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001184 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001185 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1186 leaf = path->nodes[0];
1187 btrfs_item_key_to_cpu(leaf, &found_key,
1188 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001189 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001190 found_key.type == BTRFS_EXTENT_DATA_KEY)
1191 path->slots[0]--;
1192 }
1193 check_prev = 0;
1194next_slot:
1195 leaf = path->nodes[0];
1196 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1197 ret = btrfs_next_leaf(root, path);
Josef Bacikd788a342013-10-25 16:55:08 -04001198 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001199 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001200 if (ret > 0)
1201 break;
1202 leaf = path->nodes[0];
1203 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001204
Yan Zheng80ff3852008-10-30 14:20:02 -04001205 nocow = 0;
1206 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001207 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001208 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001209
Li Zefan33345d012011-04-20 10:31:50 +08001210 if (found_key.objectid > ino ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001211 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1212 found_key.offset > end)
1213 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001214
Yan Zheng80ff3852008-10-30 14:20:02 -04001215 if (found_key.offset > cur_offset) {
1216 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001217 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001218 goto out_check;
1219 }
Chris Masonc31f8832008-01-08 15:46:31 -05001220
Yan Zheng80ff3852008-10-30 14:20:02 -04001221 fi = btrfs_item_ptr(leaf, path->slots[0],
1222 struct btrfs_file_extent_item);
1223 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001224
Josef Bacikcc95bef2013-04-04 14:31:27 -04001225 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001226 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1227 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001228 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001229 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001230 extent_end = found_key.offset +
1231 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001232 disk_num_bytes =
1233 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001234 if (extent_end <= start) {
1235 path->slots[0]++;
1236 goto next_slot;
1237 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001238 if (disk_bytenr == 0)
1239 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001240 if (btrfs_file_extent_compression(leaf, fi) ||
1241 btrfs_file_extent_encryption(leaf, fi) ||
1242 btrfs_file_extent_other_encoding(leaf, fi))
1243 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001244 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1245 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001246 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001247 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001248 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001249 found_key.offset -
1250 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001251 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001252 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001253 disk_bytenr += cur_offset - found_key.offset;
1254 num_bytes = min(end + 1, extent_end) - cur_offset;
1255 /*
1256 * force cow if csum exists in the range.
1257 * this ensure that csum for a given extent are
1258 * either valid or do not exist.
1259 */
1260 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1261 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001262 nocow = 1;
1263 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1264 extent_end = found_key.offset +
1265 btrfs_file_extent_inline_len(leaf, fi);
1266 extent_end = ALIGN(extent_end, root->sectorsize);
1267 } else {
1268 BUG_ON(1);
1269 }
1270out_check:
1271 if (extent_end <= start) {
1272 path->slots[0]++;
1273 goto next_slot;
1274 }
1275 if (!nocow) {
1276 if (cow_start == (u64)-1)
1277 cow_start = cur_offset;
1278 cur_offset = extent_end;
1279 if (cur_offset > end)
1280 break;
1281 path->slots[0]++;
1282 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001283 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001284
David Sterbab3b4aa72011-04-21 01:20:15 +02001285 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001286 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001287 ret = cow_file_range(inode, locked_page,
1288 cow_start, found_key.offset - 1,
1289 page_started, nr_written, 1);
Josef Bacikd788a342013-10-25 16:55:08 -04001290 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001291 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001292 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001293 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001294
Yan Zhengd899e052008-10-30 14:25:28 -04001295 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1296 struct extent_map *em;
1297 struct extent_map_tree *em_tree;
1298 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001299 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001300 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001301 em->start = cur_offset;
Josef Bacik70c8a912012-10-11 16:54:30 -04001302 em->orig_start = found_key.offset - extent_offset;
Yan Zhengd899e052008-10-30 14:25:28 -04001303 em->len = num_bytes;
1304 em->block_len = num_bytes;
1305 em->block_start = disk_bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05001306 em->orig_block_len = disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001307 em->ram_bytes = ram_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04001308 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001309 em->mod_start = em->start;
1310 em->mod_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04001311 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacikb11e2342012-12-03 10:58:15 -05001312 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001313 em->generation = -1;
Yan Zhengd899e052008-10-30 14:25:28 -04001314 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001315 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001316 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001317 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001318 if (ret != -EEXIST) {
1319 free_extent_map(em);
1320 break;
1321 }
1322 btrfs_drop_extent_cache(inode, em->start,
1323 em->start + em->len - 1, 0);
1324 }
1325 type = BTRFS_ORDERED_PREALLOC;
1326 } else {
1327 type = BTRFS_ORDERED_NOCOW;
1328 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001329
1330 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001331 num_bytes, num_bytes, type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001332 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001333
Yan, Zhengefa56462010-05-16 10:49:59 -04001334 if (root->root_key.objectid ==
1335 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1336 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1337 num_bytes);
Josef Bacikd788a342013-10-25 16:55:08 -04001338 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001339 goto error;
Yan, Zhengefa56462010-05-16 10:49:59 -04001340 }
1341
Josef Bacikc2790a22013-07-29 11:20:47 -04001342 extent_clear_unlock_delalloc(inode, cur_offset,
1343 cur_offset + num_bytes - 1,
1344 locked_page, EXTENT_LOCKED |
1345 EXTENT_DELALLOC, PAGE_UNLOCK |
1346 PAGE_SET_PRIVATE2);
Yan Zheng80ff3852008-10-30 14:20:02 -04001347 cur_offset = extent_end;
1348 if (cur_offset > end)
1349 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001350 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001351 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001352
Josef Bacik17ca04a2012-05-31 15:58:55 -04001353 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001354 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001355 cur_offset = end;
1356 }
1357
Yan Zheng80ff3852008-10-30 14:20:02 -04001358 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001359 ret = cow_file_range(inode, locked_page, cow_start, end,
1360 page_started, nr_written, 1);
Josef Bacikd788a342013-10-25 16:55:08 -04001361 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001362 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001363 }
1364
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001365error:
Miao Xiea698d0752012-09-20 01:51:59 -06001366 err = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001367 if (!ret)
1368 ret = err;
1369
Josef Bacik17ca04a2012-05-31 15:58:55 -04001370 if (ret && cur_offset < end)
Josef Bacikc2790a22013-07-29 11:20:47 -04001371 extent_clear_unlock_delalloc(inode, cur_offset, end,
1372 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001373 EXTENT_DELALLOC | EXTENT_DEFRAG |
1374 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1375 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001376 PAGE_SET_WRITEBACK |
1377 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001378 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001379 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001380}
1381
Chris Masond352ac62008-09-29 15:18:18 -04001382/*
1383 * extent_io.c call back to do delayed allocation processing
1384 */
Chris Masonc8b97812008-10-29 14:49:59 -04001385static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001386 u64 start, u64 end, int *page_started,
1387 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001388{
Chris Masonbe20aa92007-12-17 20:14:01 -05001389 int ret;
Chris Mason7f366cf2009-03-12 20:12:45 -04001390 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona2135012008-06-25 16:01:30 -04001391
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001392 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) {
Chris Masonc8b97812008-10-29 14:49:59 -04001393 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001394 page_started, 1, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001395 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001396 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001397 page_started, 0, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001398 } else if (!btrfs_test_opt(root, COMPRESS) &&
1399 !(BTRFS_I(inode)->force_compress) &&
1400 !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS)) {
Chris Mason7f366cf2009-03-12 20:12:45 -04001401 ret = cow_file_range(inode, locked_page, start, end,
1402 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001403 } else {
1404 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1405 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001406 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001407 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001408 }
Chris Masonb888db2b2007-08-27 16:49:44 -04001409 return ret;
1410}
1411
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001412static void btrfs_split_extent_hook(struct inode *inode,
1413 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001414{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001415 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001416 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001417 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001418
Josef Bacik9e0baf62011-07-15 15:16:44 +00001419 spin_lock(&BTRFS_I(inode)->lock);
1420 BTRFS_I(inode)->outstanding_extents++;
1421 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001422}
1423
1424/*
1425 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1426 * extents so we can keep track of new extents that are just merged onto old
1427 * extents, such as when we are doing sequential writes, so we can properly
1428 * account for the metadata space we'll need.
1429 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001430static void btrfs_merge_extent_hook(struct inode *inode,
1431 struct extent_state *new,
1432 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001433{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001434 /* not delalloc, ignore it */
1435 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001436 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001437
Josef Bacik9e0baf62011-07-15 15:16:44 +00001438 spin_lock(&BTRFS_I(inode)->lock);
1439 BTRFS_I(inode)->outstanding_extents--;
1440 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001441}
1442
Miao Xieeb73c1b2013-05-15 07:48:22 +00001443static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1444 struct inode *inode)
1445{
1446 spin_lock(&root->delalloc_lock);
1447 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1448 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1449 &root->delalloc_inodes);
1450 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1451 &BTRFS_I(inode)->runtime_flags);
1452 root->nr_delalloc_inodes++;
1453 if (root->nr_delalloc_inodes == 1) {
1454 spin_lock(&root->fs_info->delalloc_root_lock);
1455 BUG_ON(!list_empty(&root->delalloc_root));
1456 list_add_tail(&root->delalloc_root,
1457 &root->fs_info->delalloc_roots);
1458 spin_unlock(&root->fs_info->delalloc_root_lock);
1459 }
1460 }
1461 spin_unlock(&root->delalloc_lock);
1462}
1463
1464static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1465 struct inode *inode)
1466{
1467 spin_lock(&root->delalloc_lock);
1468 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1469 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1470 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1471 &BTRFS_I(inode)->runtime_flags);
1472 root->nr_delalloc_inodes--;
1473 if (!root->nr_delalloc_inodes) {
1474 spin_lock(&root->fs_info->delalloc_root_lock);
1475 BUG_ON(list_empty(&root->delalloc_root));
1476 list_del_init(&root->delalloc_root);
1477 spin_unlock(&root->fs_info->delalloc_root_lock);
1478 }
1479 }
1480 spin_unlock(&root->delalloc_lock);
1481}
1482
Chris Masond352ac62008-09-29 15:18:18 -04001483/*
1484 * extent_io.c set_bit_hook, used to track delayed allocation
1485 * bytes in this file, and to maintain the list of inodes that
1486 * have pending delalloc work to be done.
1487 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001488static void btrfs_set_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001489 struct extent_state *state, unsigned long *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001490{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001491
Chris Mason75eff682008-12-15 15:54:40 -05001492 /*
1493 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001494 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001495 * bit, which is only set or cleared with irqs on
1496 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001497 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001498 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001499 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001500 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001501
Josef Bacik9e0baf62011-07-15 15:16:44 +00001502 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001503 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001504 } else {
1505 spin_lock(&BTRFS_I(inode)->lock);
1506 BTRFS_I(inode)->outstanding_extents++;
1507 spin_unlock(&BTRFS_I(inode)->lock);
1508 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001509
Miao Xie963d6782013-01-29 10:10:51 +00001510 __percpu_counter_add(&root->fs_info->delalloc_bytes, len,
1511 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001512 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001513 BTRFS_I(inode)->delalloc_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001514 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001515 &BTRFS_I(inode)->runtime_flags))
1516 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001517 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001518 }
Chris Mason291d6732008-01-29 15:55:23 -05001519}
1520
Chris Masond352ac62008-09-29 15:18:18 -04001521/*
1522 * extent_io.c clear_bit_hook, see set_bit_hook for why
1523 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001524static void btrfs_clear_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001525 struct extent_state *state,
1526 unsigned long *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001527{
Chris Mason75eff682008-12-15 15:54:40 -05001528 /*
1529 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001530 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001531 * bit, which is only set or cleared with irqs on
1532 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001533 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001534 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001535 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001536 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001537
Josef Bacik9e0baf62011-07-15 15:16:44 +00001538 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001539 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001540 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1541 spin_lock(&BTRFS_I(inode)->lock);
1542 BTRFS_I(inode)->outstanding_extents--;
1543 spin_unlock(&BTRFS_I(inode)->lock);
1544 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001545
Josef Bacikb6d08f02013-09-27 14:57:43 -04001546 /*
1547 * We don't reserve metadata space for space cache inodes so we
1548 * don't need to call dellalloc_release_metadata if there is an
1549 * error.
1550 */
1551 if (*bits & EXTENT_DO_ACCOUNTING &&
1552 root != root->fs_info->tree_root)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001553 btrfs_delalloc_release_metadata(inode, len);
1554
Josef Bacik0cb59c92010-07-02 12:14:14 -04001555 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Josef Bacik7ee9e442013-06-21 16:37:03 -04001556 && do_list && !(state->state & EXTENT_NORESERVE))
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001557 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001558
Miao Xie963d6782013-01-29 10:10:51 +00001559 __percpu_counter_add(&root->fs_info->delalloc_bytes, -len,
1560 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001561 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001562 BTRFS_I(inode)->delalloc_bytes -= len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001563 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001564 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001565 &BTRFS_I(inode)->runtime_flags))
1566 btrfs_del_delalloc_inode(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001567 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001568 }
Chris Mason291d6732008-01-29 15:55:23 -05001569}
1570
Chris Masond352ac62008-09-29 15:18:18 -04001571/*
1572 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1573 * we don't create bios that span stripes or chunks
1574 */
David Woodhouse64a16702009-07-15 23:29:37 +01001575int btrfs_merge_bio_hook(int rw, struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001576 size_t size, struct bio *bio,
1577 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001578{
1579 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Chris Masona62b9402008-10-03 16:31:08 -04001580 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001581 u64 length = 0;
1582 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001583 int ret;
1584
Chris Mason771ed682008-11-06 22:02:51 -05001585 if (bio_flags & EXTENT_BIO_COMPRESSED)
1586 return 0;
1587
Chris Masonf2d8d742008-04-21 10:03:05 -04001588 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001589 map_length = length;
David Woodhouse64a16702009-07-15 23:29:37 +01001590 ret = btrfs_map_block(root->fs_info, rw, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001591 &map_length, NULL, 0);
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001592 /* Will always return 0 with map_multi == NULL */
Jeff Mahoney3444a972011-10-03 23:23:13 -04001593 BUG_ON(ret < 0);
Chris Masond3977122009-01-05 21:25:51 -05001594 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001595 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001596 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001597}
1598
Chris Masond352ac62008-09-29 15:18:18 -04001599/*
1600 * in order to insert checksums into the metadata in large chunks,
1601 * we wait until bio submission time. All the pages in the bio are
1602 * checksummed and sums are attached onto the ordered extent record.
1603 *
1604 * At IO completion time the cums attached on the ordered extent record
1605 * are inserted into the btree
1606 */
Chris Masond3977122009-01-05 21:25:51 -05001607static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1608 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001609 unsigned long bio_flags,
1610 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001611{
Chris Mason065631f2008-02-20 12:07:25 -05001612 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001613 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001614
Chris Masond20f7042008-12-08 16:58:54 -05001615 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001616 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001617 return 0;
1618}
Chris Masone0156402008-04-16 11:15:20 -04001619
Chris Mason4a69a412008-11-06 22:03:00 -05001620/*
1621 * in order to insert checksums into the metadata in large chunks,
1622 * we wait until bio submission time. All the pages in the bio are
1623 * checksummed and sums are attached onto the ordered extent record.
1624 *
1625 * At IO completion time the cums attached on the ordered extent record
1626 * are inserted into the btree
1627 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001628static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001629 int mirror_num, unsigned long bio_flags,
1630 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001631{
1632 struct btrfs_root *root = BTRFS_I(inode)->root;
Stefan Behrens61891922012-11-05 18:51:52 +01001633 int ret;
1634
1635 ret = btrfs_map_bio(root, rw, bio, mirror_num, 1);
1636 if (ret)
1637 bio_endio(bio, ret);
1638 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001639}
1640
Chris Masond352ac62008-09-29 15:18:18 -04001641/*
Chris Masoncad321a2008-12-17 14:51:42 -05001642 * extent_io.c submission hook. This does the right thing for csum calculation
1643 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001644 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001645static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001646 int mirror_num, unsigned long bio_flags,
1647 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001648{
1649 struct btrfs_root *root = BTRFS_I(inode)->root;
1650 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001651 int skip_sum;
Jeff Mahoney04173412011-10-03 23:23:12 -04001652 int metadata = 0;
Josef Bacikb812ce22012-11-16 13:56:32 -05001653 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001654
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001655 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001656
Liu Bo83eea1f2012-07-10 05:28:39 -06001657 if (btrfs_is_free_space_inode(inode))
Jeff Mahoney04173412011-10-03 23:23:12 -04001658 metadata = 2;
1659
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001660 if (!(rw & REQ_WRITE)) {
Josef Bacik5fd02042012-05-02 14:00:54 -04001661 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1662 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001663 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001664
Chris Masond20f7042008-12-08 16:58:54 -05001665 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001666 ret = btrfs_submit_compressed_read(inode, bio,
1667 mirror_num,
1668 bio_flags);
1669 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001670 } else if (!skip_sum) {
1671 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1672 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001673 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001674 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001675 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001676 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001677 /* csum items have already been cloned */
1678 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1679 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001680 /* we're doing a write, do the async checksumming */
Stefan Behrens61891922012-11-05 18:51:52 +01001681 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001682 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001683 bio_flags, bio_offset,
1684 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001685 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001686 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001687 } else if (!skip_sum) {
1688 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1689 if (ret)
1690 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001691 }
1692
Chris Mason0b86a832008-03-24 15:01:56 -04001693mapit:
Stefan Behrens61891922012-11-05 18:51:52 +01001694 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
1695
1696out:
1697 if (ret < 0)
1698 bio_endio(bio, ret);
1699 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001700}
Chris Mason6885f302008-02-20 16:11:05 -05001701
Chris Masond352ac62008-09-29 15:18:18 -04001702/*
1703 * given a list of ordered sums record them in the inode. This happens
1704 * at IO completion time based on sums calculated at bio submission time.
1705 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001706static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001707 struct inode *inode, u64 file_offset,
1708 struct list_head *list)
1709{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001710 struct btrfs_ordered_sum *sum;
1711
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001712 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00001713 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05001714 btrfs_csum_file_blocks(trans,
1715 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00001716 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001717 }
1718 return 0;
1719}
1720
Josef Bacik2ac55d42010-02-03 19:33:23 +00001721int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1722 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001723{
Julia Lawall6c1500f2012-11-03 20:30:18 +00001724 WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04001725 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001726 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001727}
1728
Chris Masond352ac62008-09-29 15:18:18 -04001729/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001730struct btrfs_writepage_fixup {
1731 struct page *page;
1732 struct btrfs_work work;
1733};
1734
Christoph Hellwigb2950862008-12-02 09:54:17 -05001735static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001736{
1737 struct btrfs_writepage_fixup *fixup;
1738 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001739 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001740 struct page *page;
1741 struct inode *inode;
1742 u64 page_start;
1743 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001744 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001745
1746 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1747 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001748again:
Chris Mason247e7432008-07-17 12:53:51 -04001749 lock_page(page);
1750 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1751 ClearPageChecked(page);
1752 goto out_page;
1753 }
1754
1755 inode = page->mapping->host;
1756 page_start = page_offset(page);
1757 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1758
Josef Bacik2ac55d42010-02-03 19:33:23 +00001759 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001760 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001761
1762 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001763 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001764 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001765
1766 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1767 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001768 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1769 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001770 unlock_page(page);
1771 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001772 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04001773 goto again;
1774 }
Chris Mason247e7432008-07-17 12:53:51 -04001775
Jeff Mahoney87826df2012-02-15 16:23:57 +01001776 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
1777 if (ret) {
1778 mapping_set_error(page->mapping, ret);
1779 end_extent_writepage(page, ret, page_start, page_end);
1780 ClearPageChecked(page);
1781 goto out;
1782 }
1783
Josef Bacik2ac55d42010-02-03 19:33:23 +00001784 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001785 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001786 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04001787out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001788 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1789 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001790out_page:
1791 unlock_page(page);
1792 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08001793 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04001794}
1795
1796/*
1797 * There are a few paths in the higher layers of the kernel that directly
1798 * set the page dirty bit without asking the filesystem if it is a
1799 * good idea. This causes problems because we want to make sure COW
1800 * properly happens and the data=ordered rules are followed.
1801 *
Chris Masonc8b97812008-10-29 14:49:59 -04001802 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001803 * hasn't been properly setup for IO. We kick off an async process
1804 * to fix it up. The async helper will wait for ordered extents, set
1805 * the delalloc bit and make it safe to write the page.
1806 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001807static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001808{
1809 struct inode *inode = page->mapping->host;
1810 struct btrfs_writepage_fixup *fixup;
1811 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001812
Chris Mason8b62b722009-09-02 16:53:46 -04001813 /* this page is properly in the ordered list */
1814 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001815 return 0;
1816
1817 if (PageChecked(page))
1818 return -EAGAIN;
1819
1820 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1821 if (!fixup)
1822 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001823
Chris Mason247e7432008-07-17 12:53:51 -04001824 SetPageChecked(page);
1825 page_cache_get(page);
1826 fixup->work.func = btrfs_writepage_fixup_worker;
1827 fixup->page = page;
1828 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001829 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04001830}
1831
Yan Zhengd899e052008-10-30 14:25:28 -04001832static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1833 struct inode *inode, u64 file_pos,
1834 u64 disk_bytenr, u64 disk_num_bytes,
1835 u64 num_bytes, u64 ram_bytes,
1836 u8 compression, u8 encryption,
1837 u16 other_encoding, int extent_type)
1838{
1839 struct btrfs_root *root = BTRFS_I(inode)->root;
1840 struct btrfs_file_extent_item *fi;
1841 struct btrfs_path *path;
1842 struct extent_buffer *leaf;
1843 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04001844 int ret;
1845
1846 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07001847 if (!path)
1848 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04001849
Chris Masonb9473432009-03-13 11:00:37 -04001850 path->leave_spinning = 1;
Chris Masona1ed8352009-09-11 12:27:37 -04001851
1852 /*
1853 * we may be replacing one extent in the tree with another.
1854 * The new extent is pinned in the extent map, and we don't want
1855 * to drop it from the cache until it is completely in the btree.
1856 *
1857 * So, tell btrfs_drop_extents to leave this extent in the cache.
1858 * the caller is expected to unpin it and allow it to be merged
1859 * with the others.
1860 */
Josef Bacik5dc562c2012-08-17 13:14:17 -04001861 ret = btrfs_drop_extents(trans, root, inode, file_pos,
Josef Bacik26714852012-08-29 12:24:27 -04001862 file_pos + num_bytes, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001863 if (ret)
1864 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001865
Li Zefan33345d012011-04-20 10:31:50 +08001866 ins.objectid = btrfs_ino(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04001867 ins.offset = file_pos;
1868 ins.type = BTRFS_EXTENT_DATA_KEY;
1869 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001870 if (ret)
1871 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001872 leaf = path->nodes[0];
1873 fi = btrfs_item_ptr(leaf, path->slots[0],
1874 struct btrfs_file_extent_item);
1875 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1876 btrfs_set_file_extent_type(leaf, fi, extent_type);
1877 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1878 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1879 btrfs_set_file_extent_offset(leaf, fi, 0);
1880 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1881 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1882 btrfs_set_file_extent_compression(leaf, fi, compression);
1883 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1884 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04001885
Yan Zhengd899e052008-10-30 14:25:28 -04001886 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04001887 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04001888
1889 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04001890
1891 ins.objectid = disk_bytenr;
1892 ins.offset = disk_num_bytes;
1893 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001894 ret = btrfs_alloc_reserved_file_extent(trans, root,
1895 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08001896 btrfs_ino(inode), file_pos, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001897out:
Yan Zhengd899e052008-10-30 14:25:28 -04001898 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04001899
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001900 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04001901}
1902
Liu Bo38c227d2013-01-29 03:18:40 +00001903/* snapshot-aware defrag */
1904struct sa_defrag_extent_backref {
1905 struct rb_node node;
1906 struct old_sa_defrag_extent *old;
1907 u64 root_id;
1908 u64 inum;
1909 u64 file_pos;
1910 u64 extent_offset;
1911 u64 num_bytes;
1912 u64 generation;
1913};
1914
1915struct old_sa_defrag_extent {
1916 struct list_head list;
1917 struct new_sa_defrag_extent *new;
1918
1919 u64 extent_offset;
1920 u64 bytenr;
1921 u64 offset;
1922 u64 len;
1923 int count;
1924};
1925
1926struct new_sa_defrag_extent {
1927 struct rb_root root;
1928 struct list_head head;
1929 struct btrfs_path *path;
1930 struct inode *inode;
1931 u64 file_pos;
1932 u64 len;
1933 u64 bytenr;
1934 u64 disk_len;
1935 u8 compress_type;
1936};
1937
1938static int backref_comp(struct sa_defrag_extent_backref *b1,
1939 struct sa_defrag_extent_backref *b2)
1940{
1941 if (b1->root_id < b2->root_id)
1942 return -1;
1943 else if (b1->root_id > b2->root_id)
1944 return 1;
1945
1946 if (b1->inum < b2->inum)
1947 return -1;
1948 else if (b1->inum > b2->inum)
1949 return 1;
1950
1951 if (b1->file_pos < b2->file_pos)
1952 return -1;
1953 else if (b1->file_pos > b2->file_pos)
1954 return 1;
1955
1956 /*
1957 * [------------------------------] ===> (a range of space)
1958 * |<--->| |<---->| =============> (fs/file tree A)
1959 * |<---------------------------->| ===> (fs/file tree B)
1960 *
1961 * A range of space can refer to two file extents in one tree while
1962 * refer to only one file extent in another tree.
1963 *
1964 * So we may process a disk offset more than one time(two extents in A)
1965 * and locate at the same extent(one extent in B), then insert two same
1966 * backrefs(both refer to the extent in B).
1967 */
1968 return 0;
1969}
1970
1971static void backref_insert(struct rb_root *root,
1972 struct sa_defrag_extent_backref *backref)
1973{
1974 struct rb_node **p = &root->rb_node;
1975 struct rb_node *parent = NULL;
1976 struct sa_defrag_extent_backref *entry;
1977 int ret;
1978
1979 while (*p) {
1980 parent = *p;
1981 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
1982
1983 ret = backref_comp(backref, entry);
1984 if (ret < 0)
1985 p = &(*p)->rb_left;
1986 else
1987 p = &(*p)->rb_right;
1988 }
1989
1990 rb_link_node(&backref->node, parent, p);
1991 rb_insert_color(&backref->node, root);
1992}
1993
1994/*
1995 * Note the backref might has changed, and in this case we just return 0.
1996 */
1997static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
1998 void *ctx)
1999{
2000 struct btrfs_file_extent_item *extent;
2001 struct btrfs_fs_info *fs_info;
2002 struct old_sa_defrag_extent *old = ctx;
2003 struct new_sa_defrag_extent *new = old->new;
2004 struct btrfs_path *path = new->path;
2005 struct btrfs_key key;
2006 struct btrfs_root *root;
2007 struct sa_defrag_extent_backref *backref;
2008 struct extent_buffer *leaf;
2009 struct inode *inode = new->inode;
2010 int slot;
2011 int ret;
2012 u64 extent_offset;
2013 u64 num_bytes;
2014
2015 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
2016 inum == btrfs_ino(inode))
2017 return 0;
2018
2019 key.objectid = root_id;
2020 key.type = BTRFS_ROOT_ITEM_KEY;
2021 key.offset = (u64)-1;
2022
2023 fs_info = BTRFS_I(inode)->root->fs_info;
2024 root = btrfs_read_fs_root_no_name(fs_info, &key);
2025 if (IS_ERR(root)) {
2026 if (PTR_ERR(root) == -ENOENT)
2027 return 0;
2028 WARN_ON(1);
2029 pr_debug("inum=%llu, offset=%llu, root_id=%llu\n",
2030 inum, offset, root_id);
2031 return PTR_ERR(root);
2032 }
2033
2034 key.objectid = inum;
2035 key.type = BTRFS_EXTENT_DATA_KEY;
2036 if (offset > (u64)-1 << 32)
2037 key.offset = 0;
2038 else
2039 key.offset = offset;
2040
2041 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302042 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002043 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002044 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002045
2046 while (1) {
2047 cond_resched();
2048
2049 leaf = path->nodes[0];
2050 slot = path->slots[0];
2051
2052 if (slot >= btrfs_header_nritems(leaf)) {
2053 ret = btrfs_next_leaf(root, path);
2054 if (ret < 0) {
2055 goto out;
2056 } else if (ret > 0) {
2057 ret = 0;
2058 goto out;
2059 }
2060 continue;
2061 }
2062
2063 path->slots[0]++;
2064
2065 btrfs_item_key_to_cpu(leaf, &key, slot);
2066
2067 if (key.objectid > inum)
2068 goto out;
2069
2070 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2071 continue;
2072
2073 extent = btrfs_item_ptr(leaf, slot,
2074 struct btrfs_file_extent_item);
2075
2076 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2077 continue;
2078
Liu Boe68afa42013-07-01 22:13:26 +08002079 /*
2080 * 'offset' refers to the exact key.offset,
2081 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2082 * (key.offset - extent_offset).
2083 */
2084 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002085 continue;
2086
Liu Boe68afa42013-07-01 22:13:26 +08002087 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002088 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002089
Liu Bo38c227d2013-01-29 03:18:40 +00002090 if (extent_offset >= old->extent_offset + old->offset +
2091 old->len || extent_offset + num_bytes <=
2092 old->extent_offset + old->offset)
2093 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002094 break;
2095 }
2096
2097 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2098 if (!backref) {
2099 ret = -ENOENT;
2100 goto out;
2101 }
2102
2103 backref->root_id = root_id;
2104 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002105 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002106 backref->num_bytes = num_bytes;
2107 backref->extent_offset = extent_offset;
2108 backref->generation = btrfs_file_extent_generation(leaf, extent);
2109 backref->old = old;
2110 backref_insert(&new->root, backref);
2111 old->count++;
2112out:
2113 btrfs_release_path(path);
2114 WARN_ON(ret);
2115 return ret;
2116}
2117
2118static noinline bool record_extent_backrefs(struct btrfs_path *path,
2119 struct new_sa_defrag_extent *new)
2120{
2121 struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info;
2122 struct old_sa_defrag_extent *old, *tmp;
2123 int ret;
2124
2125 new->path = path;
2126
2127 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002128 ret = iterate_inodes_from_logical(old->bytenr +
2129 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002130 path, record_one_backref,
2131 old);
Josef Bacik4724b102013-11-05 11:11:40 -05002132 if (ret < 0 && ret != -ENOENT)
2133 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002134
2135 /* no backref to be processed for this extent */
2136 if (!old->count) {
2137 list_del(&old->list);
2138 kfree(old);
2139 }
2140 }
2141
2142 if (list_empty(&new->head))
2143 return false;
2144
2145 return true;
2146}
2147
2148static int relink_is_mergable(struct extent_buffer *leaf,
2149 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002150 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002151{
Liu Bo116e0022013-08-02 16:30:40 +08002152 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002153 return 0;
2154
2155 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2156 return 0;
2157
Liu Bo116e0022013-08-02 16:30:40 +08002158 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2159 return 0;
2160
2161 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002162 btrfs_file_extent_other_encoding(leaf, fi))
2163 return 0;
2164
2165 return 1;
2166}
2167
2168/*
2169 * Note the backref might has changed, and in this case we just return 0.
2170 */
2171static noinline int relink_extent_backref(struct btrfs_path *path,
2172 struct sa_defrag_extent_backref *prev,
2173 struct sa_defrag_extent_backref *backref)
2174{
2175 struct btrfs_file_extent_item *extent;
2176 struct btrfs_file_extent_item *item;
2177 struct btrfs_ordered_extent *ordered;
2178 struct btrfs_trans_handle *trans;
2179 struct btrfs_fs_info *fs_info;
2180 struct btrfs_root *root;
2181 struct btrfs_key key;
2182 struct extent_buffer *leaf;
2183 struct old_sa_defrag_extent *old = backref->old;
2184 struct new_sa_defrag_extent *new = old->new;
2185 struct inode *src_inode = new->inode;
2186 struct inode *inode;
2187 struct extent_state *cached = NULL;
2188 int ret = 0;
2189 u64 start;
2190 u64 len;
2191 u64 lock_start;
2192 u64 lock_end;
2193 bool merge = false;
2194 int index;
2195
2196 if (prev && prev->root_id == backref->root_id &&
2197 prev->inum == backref->inum &&
2198 prev->file_pos + prev->num_bytes == backref->file_pos)
2199 merge = true;
2200
2201 /* step 1: get root */
2202 key.objectid = backref->root_id;
2203 key.type = BTRFS_ROOT_ITEM_KEY;
2204 key.offset = (u64)-1;
2205
2206 fs_info = BTRFS_I(src_inode)->root->fs_info;
2207 index = srcu_read_lock(&fs_info->subvol_srcu);
2208
2209 root = btrfs_read_fs_root_no_name(fs_info, &key);
2210 if (IS_ERR(root)) {
2211 srcu_read_unlock(&fs_info->subvol_srcu, index);
2212 if (PTR_ERR(root) == -ENOENT)
2213 return 0;
2214 return PTR_ERR(root);
2215 }
Liu Bo38c227d2013-01-29 03:18:40 +00002216
2217 /* step 2: get inode */
2218 key.objectid = backref->inum;
2219 key.type = BTRFS_INODE_ITEM_KEY;
2220 key.offset = 0;
2221
2222 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2223 if (IS_ERR(inode)) {
2224 srcu_read_unlock(&fs_info->subvol_srcu, index);
2225 return 0;
2226 }
2227
2228 srcu_read_unlock(&fs_info->subvol_srcu, index);
2229
2230 /* step 3: relink backref */
2231 lock_start = backref->file_pos;
2232 lock_end = backref->file_pos + backref->num_bytes - 1;
2233 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2234 0, &cached);
2235
2236 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2237 if (ordered) {
2238 btrfs_put_ordered_extent(ordered);
2239 goto out_unlock;
2240 }
2241
2242 trans = btrfs_join_transaction(root);
2243 if (IS_ERR(trans)) {
2244 ret = PTR_ERR(trans);
2245 goto out_unlock;
2246 }
2247
2248 key.objectid = backref->inum;
2249 key.type = BTRFS_EXTENT_DATA_KEY;
2250 key.offset = backref->file_pos;
2251
2252 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2253 if (ret < 0) {
2254 goto out_free_path;
2255 } else if (ret > 0) {
2256 ret = 0;
2257 goto out_free_path;
2258 }
2259
2260 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2261 struct btrfs_file_extent_item);
2262
2263 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2264 backref->generation)
2265 goto out_free_path;
2266
2267 btrfs_release_path(path);
2268
2269 start = backref->file_pos;
2270 if (backref->extent_offset < old->extent_offset + old->offset)
2271 start += old->extent_offset + old->offset -
2272 backref->extent_offset;
2273
2274 len = min(backref->extent_offset + backref->num_bytes,
2275 old->extent_offset + old->offset + old->len);
2276 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2277
2278 ret = btrfs_drop_extents(trans, root, inode, start,
2279 start + len, 1);
2280 if (ret)
2281 goto out_free_path;
2282again:
2283 key.objectid = btrfs_ino(inode);
2284 key.type = BTRFS_EXTENT_DATA_KEY;
2285 key.offset = start;
2286
Liu Boa09a0a72013-03-11 09:20:58 +00002287 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002288 if (merge) {
2289 struct btrfs_file_extent_item *fi;
2290 u64 extent_len;
2291 struct btrfs_key found_key;
2292
2293 ret = btrfs_search_slot(trans, root, &key, path, 1, 1);
2294 if (ret < 0)
2295 goto out_free_path;
2296
2297 path->slots[0]--;
2298 leaf = path->nodes[0];
2299 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2300
2301 fi = btrfs_item_ptr(leaf, path->slots[0],
2302 struct btrfs_file_extent_item);
2303 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2304
Liu Bo116e0022013-08-02 16:30:40 +08002305 if (extent_len + found_key.offset == start &&
2306 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002307 btrfs_set_file_extent_num_bytes(leaf, fi,
2308 extent_len + len);
2309 btrfs_mark_buffer_dirty(leaf);
2310 inode_add_bytes(inode, len);
2311
2312 ret = 1;
2313 goto out_free_path;
2314 } else {
2315 merge = false;
2316 btrfs_release_path(path);
2317 goto again;
2318 }
2319 }
2320
2321 ret = btrfs_insert_empty_item(trans, root, path, &key,
2322 sizeof(*extent));
2323 if (ret) {
2324 btrfs_abort_transaction(trans, root, ret);
2325 goto out_free_path;
2326 }
2327
2328 leaf = path->nodes[0];
2329 item = btrfs_item_ptr(leaf, path->slots[0],
2330 struct btrfs_file_extent_item);
2331 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2332 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2333 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2334 btrfs_set_file_extent_num_bytes(leaf, item, len);
2335 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2336 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2337 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2338 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2339 btrfs_set_file_extent_encryption(leaf, item, 0);
2340 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2341
2342 btrfs_mark_buffer_dirty(leaf);
2343 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002344 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002345
2346 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
2347 new->disk_len, 0,
2348 backref->root_id, backref->inum,
2349 new->file_pos, 0); /* start - extent_offset */
2350 if (ret) {
2351 btrfs_abort_transaction(trans, root, ret);
2352 goto out_free_path;
2353 }
2354
2355 ret = 1;
2356out_free_path:
2357 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002358 path->leave_spinning = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002359 btrfs_end_transaction(trans, root);
2360out_unlock:
2361 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2362 &cached, GFP_NOFS);
2363 iput(inode);
2364 return ret;
2365}
2366
Liu Bo6f519562013-10-29 10:45:05 +08002367static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2368{
2369 struct old_sa_defrag_extent *old, *tmp;
2370
2371 if (!new)
2372 return;
2373
2374 list_for_each_entry_safe(old, tmp, &new->head, list) {
2375 list_del(&old->list);
2376 kfree(old);
2377 }
2378 kfree(new);
2379}
2380
Liu Bo38c227d2013-01-29 03:18:40 +00002381static void relink_file_extents(struct new_sa_defrag_extent *new)
2382{
2383 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002384 struct sa_defrag_extent_backref *backref;
2385 struct sa_defrag_extent_backref *prev = NULL;
2386 struct inode *inode;
2387 struct btrfs_root *root;
2388 struct rb_node *node;
2389 int ret;
2390
2391 inode = new->inode;
2392 root = BTRFS_I(inode)->root;
2393
2394 path = btrfs_alloc_path();
2395 if (!path)
2396 return;
2397
2398 if (!record_extent_backrefs(path, new)) {
2399 btrfs_free_path(path);
2400 goto out;
2401 }
2402 btrfs_release_path(path);
2403
2404 while (1) {
2405 node = rb_first(&new->root);
2406 if (!node)
2407 break;
2408 rb_erase(node, &new->root);
2409
2410 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2411
2412 ret = relink_extent_backref(path, prev, backref);
2413 WARN_ON(ret < 0);
2414
2415 kfree(prev);
2416
2417 if (ret == 1)
2418 prev = backref;
2419 else
2420 prev = NULL;
2421 cond_resched();
2422 }
2423 kfree(prev);
2424
2425 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002426out:
Liu Bo6f519562013-10-29 10:45:05 +08002427 free_sa_defrag_extent(new);
2428
Liu Bo38c227d2013-01-29 03:18:40 +00002429 atomic_dec(&root->fs_info->defrag_running);
2430 wake_up(&root->fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002431}
2432
2433static struct new_sa_defrag_extent *
2434record_old_file_extents(struct inode *inode,
2435 struct btrfs_ordered_extent *ordered)
2436{
2437 struct btrfs_root *root = BTRFS_I(inode)->root;
2438 struct btrfs_path *path;
2439 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002440 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002441 struct new_sa_defrag_extent *new;
2442 int ret;
2443
2444 new = kmalloc(sizeof(*new), GFP_NOFS);
2445 if (!new)
2446 return NULL;
2447
2448 new->inode = inode;
2449 new->file_pos = ordered->file_offset;
2450 new->len = ordered->len;
2451 new->bytenr = ordered->start;
2452 new->disk_len = ordered->disk_len;
2453 new->compress_type = ordered->compress_type;
2454 new->root = RB_ROOT;
2455 INIT_LIST_HEAD(&new->head);
2456
2457 path = btrfs_alloc_path();
2458 if (!path)
2459 goto out_kfree;
2460
2461 key.objectid = btrfs_ino(inode);
2462 key.type = BTRFS_EXTENT_DATA_KEY;
2463 key.offset = new->file_pos;
2464
2465 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2466 if (ret < 0)
2467 goto out_free_path;
2468 if (ret > 0 && path->slots[0] > 0)
2469 path->slots[0]--;
2470
2471 /* find out all the old extents for the file range */
2472 while (1) {
2473 struct btrfs_file_extent_item *extent;
2474 struct extent_buffer *l;
2475 int slot;
2476 u64 num_bytes;
2477 u64 offset;
2478 u64 end;
2479 u64 disk_bytenr;
2480 u64 extent_offset;
2481
2482 l = path->nodes[0];
2483 slot = path->slots[0];
2484
2485 if (slot >= btrfs_header_nritems(l)) {
2486 ret = btrfs_next_leaf(root, path);
2487 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002488 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002489 else if (ret > 0)
2490 break;
2491 continue;
2492 }
2493
2494 btrfs_item_key_to_cpu(l, &key, slot);
2495
2496 if (key.objectid != btrfs_ino(inode))
2497 break;
2498 if (key.type != BTRFS_EXTENT_DATA_KEY)
2499 break;
2500 if (key.offset >= new->file_pos + new->len)
2501 break;
2502
2503 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2504
2505 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2506 if (key.offset + num_bytes < new->file_pos)
2507 goto next;
2508
2509 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2510 if (!disk_bytenr)
2511 goto next;
2512
2513 extent_offset = btrfs_file_extent_offset(l, extent);
2514
2515 old = kmalloc(sizeof(*old), GFP_NOFS);
2516 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002517 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002518
2519 offset = max(new->file_pos, key.offset);
2520 end = min(new->file_pos + new->len, key.offset + num_bytes);
2521
2522 old->bytenr = disk_bytenr;
2523 old->extent_offset = extent_offset;
2524 old->offset = offset - key.offset;
2525 old->len = end - offset;
2526 old->new = new;
2527 old->count = 0;
2528 list_add_tail(&old->list, &new->head);
2529next:
2530 path->slots[0]++;
2531 cond_resched();
2532 }
2533
2534 btrfs_free_path(path);
2535 atomic_inc(&root->fs_info->defrag_running);
2536
2537 return new;
2538
Liu Bo38c227d2013-01-29 03:18:40 +00002539out_free_path:
2540 btrfs_free_path(path);
2541out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002542 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002543 return NULL;
2544}
2545
Chris Mason5d13a982009-03-13 11:41:46 -04002546/*
2547 * helper function for btrfs_finish_ordered_io, this
2548 * just reads in some of the csum leaves to prime them into ram
2549 * before we start the transaction. It limits the amount of btree
2550 * reads required while inside the transaction.
2551 */
Chris Masond352ac62008-09-29 15:18:18 -04002552/* as ordered data IO finishes, this gets called so we can finish
2553 * an ordered extent if the range of bytes in the file it covers are
2554 * fully written.
2555 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002556static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002557{
Josef Bacik5fd02042012-05-02 14:00:54 -04002558 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002559 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002560 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002561 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002562 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002563 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002564 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002565 int ret = 0;
2566 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002567 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002568 bool truncated = false;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002569
Liu Bo83eea1f2012-07-10 05:28:39 -06002570 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002571
Josef Bacik5fd02042012-05-02 14:00:54 -04002572 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2573 ret = -EIO;
2574 goto out;
2575 }
2576
Josef Bacik77cef2e2013-08-29 13:57:21 -04002577 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2578 truncated = true;
2579 logical_len = ordered_extent->truncated_len;
2580 /* Truncated the entire extent, don't bother adding */
2581 if (!logical_len)
2582 goto out;
2583 }
2584
Yan, Zhengc2167752009-11-12 09:34:21 +00002585 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002586 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Josef Bacik6c760c02012-11-09 10:53:21 -05002587 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2588 if (nolock)
2589 trans = btrfs_join_transaction_nolock(root);
2590 else
2591 trans = btrfs_join_transaction(root);
2592 if (IS_ERR(trans)) {
2593 ret = PTR_ERR(trans);
2594 trans = NULL;
2595 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002596 }
Josef Bacik6c760c02012-11-09 10:53:21 -05002597 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
2598 ret = btrfs_update_inode_fallback(trans, root, inode);
2599 if (ret) /* -ENOMEM or corruption */
2600 btrfs_abort_transaction(trans, root, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002601 goto out;
2602 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002603
Josef Bacik2ac55d42010-02-03 19:33:23 +00002604 lock_extent_bits(io_tree, ordered_extent->file_offset,
2605 ordered_extent->file_offset + ordered_extent->len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002606 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002607
Liu Bo38c227d2013-01-29 03:18:40 +00002608 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2609 ordered_extent->file_offset + ordered_extent->len - 1,
2610 EXTENT_DEFRAG, 1, cached_state);
2611 if (ret) {
2612 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
2613 if (last_snapshot >= BTRFS_I(inode)->generation)
2614 /* the inode is shared */
2615 new = record_old_file_extents(inode, ordered_extent);
2616
2617 clear_extent_bit(io_tree, ordered_extent->file_offset,
2618 ordered_extent->file_offset + ordered_extent->len - 1,
2619 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2620 }
2621
Josef Bacik0cb59c92010-07-02 12:14:14 -04002622 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002623 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002624 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002625 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002626 if (IS_ERR(trans)) {
2627 ret = PTR_ERR(trans);
2628 trans = NULL;
2629 goto out_unlock;
2630 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002631 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002632
Chris Masonc8b97812008-10-29 14:49:59 -04002633 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002634 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002635 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002636 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00002637 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002638 ordered_extent->file_offset,
2639 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002640 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002641 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04002642 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002643 ret = insert_reserved_file_extent(trans, inode,
2644 ordered_extent->file_offset,
2645 ordered_extent->start,
2646 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04002647 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08002648 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002649 BTRFS_FILE_EXTENT_REG);
Yan Zhengd899e052008-10-30 14:25:28 -04002650 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002651 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2652 ordered_extent->file_offset, ordered_extent->len,
2653 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002654 if (ret < 0) {
2655 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002656 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002657 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002658
Chris Masone6dcd2d2008-07-17 12:53:50 -04002659 add_pending_csums(trans, inode, ordered_extent->file_offset,
2660 &ordered_extent->list);
2661
Josef Bacik6c760c02012-11-09 10:53:21 -05002662 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2663 ret = btrfs_update_inode_fallback(trans, root, inode);
2664 if (ret) { /* -ENOMEM or corruption */
2665 btrfs_abort_transaction(trans, root, ret);
2666 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002667 }
2668 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002669out_unlock:
2670 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2671 ordered_extent->file_offset +
2672 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00002673out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002674 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04002675 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002676 if (trans)
2677 btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002678
Josef Bacik77cef2e2013-08-29 13:57:21 -04002679 if (ret || truncated) {
2680 u64 start, end;
2681
2682 if (truncated)
2683 start = ordered_extent->file_offset + logical_len;
2684 else
2685 start = ordered_extent->file_offset;
2686 end = ordered_extent->file_offset + ordered_extent->len - 1;
2687 clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS);
2688
2689 /* Drop the cache for the part of the extent we didn't write. */
2690 btrfs_drop_extent_cache(inode, start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04002691
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002692 /*
2693 * If the ordered extent had an IOERR or something else went
2694 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04002695 * back to the allocator. We only free the extent in the
2696 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002697 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04002698 if ((ret || !logical_len) &&
2699 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002700 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
2701 btrfs_free_reserved_extent(root, ordered_extent->start,
2702 ordered_extent->disk_len);
2703 }
2704
2705
Josef Bacik5fd02042012-05-02 14:00:54 -04002706 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002707 * This needs to be done to make sure anybody waiting knows we are done
2708 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002709 */
2710 btrfs_remove_ordered_extent(inode, ordered_extent);
2711
Liu Bo38c227d2013-01-29 03:18:40 +00002712 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08002713 if (new) {
2714 if (ret) {
2715 free_sa_defrag_extent(new);
2716 atomic_dec(&root->fs_info->defrag_running);
2717 } else {
2718 relink_file_extents(new);
2719 }
2720 }
Liu Bo38c227d2013-01-29 03:18:40 +00002721
Chris Masone6dcd2d2008-07-17 12:53:50 -04002722 /* once for us */
2723 btrfs_put_ordered_extent(ordered_extent);
2724 /* once for the tree */
2725 btrfs_put_ordered_extent(ordered_extent);
2726
Josef Bacik5fd02042012-05-02 14:00:54 -04002727 return ret;
2728}
2729
2730static void finish_ordered_fn(struct btrfs_work *work)
2731{
2732 struct btrfs_ordered_extent *ordered_extent;
2733 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2734 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002735}
2736
Christoph Hellwigb2950862008-12-02 09:54:17 -05002737static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04002738 struct extent_state *state, int uptodate)
2739{
Josef Bacik5fd02042012-05-02 14:00:54 -04002740 struct inode *inode = page->mapping->host;
2741 struct btrfs_root *root = BTRFS_I(inode)->root;
2742 struct btrfs_ordered_extent *ordered_extent = NULL;
2743 struct btrfs_workers *workers;
2744
liubo1abe9b82011-03-24 11:18:59 +00002745 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2746
Chris Mason8b62b722009-09-02 16:53:46 -04002747 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002748 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2749 end - start + 1, uptodate))
2750 return 0;
2751
2752 ordered_extent->work.func = finish_ordered_fn;
2753 ordered_extent->work.flags = 0;
2754
Liu Bo83eea1f2012-07-10 05:28:39 -06002755 if (btrfs_is_free_space_inode(inode))
Josef Bacik5fd02042012-05-02 14:00:54 -04002756 workers = &root->fs_info->endio_freespace_worker;
2757 else
2758 workers = &root->fs_info->endio_write_workers;
2759 btrfs_queue_worker(workers, &ordered_extent->work);
2760
2761 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04002762}
2763
Chris Masond352ac62008-09-29 15:18:18 -04002764/*
Chris Masond352ac62008-09-29 15:18:18 -04002765 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002766 * if there's a match, we allow the bio to finish. If not, the code in
2767 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002768 */
Miao Xiefacc8a222013-07-25 19:22:34 +08002769static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
2770 u64 phy_offset, struct page *page,
2771 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002772{
Miao Xie4eee4fa2012-12-21 09:17:45 +00002773 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04002774 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002775 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04002776 char *kaddr;
Chris Masonff79f812007-10-15 16:22:25 -04002777 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiefacc8a222013-07-25 19:22:34 +08002778 u32 csum_expected;
Chris Masonff79f812007-10-15 16:22:25 -04002779 u32 csum = ~(u32)0;
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002780 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
2781 DEFAULT_RATELIMIT_BURST);
Chris Masond1310b22008-01-24 16:13:08 -05002782
Chris Masond20f7042008-12-08 16:58:54 -05002783 if (PageChecked(page)) {
2784 ClearPageChecked(page);
2785 goto good;
2786 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002787
2788 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Jan Schmidt08d2f342011-05-04 16:18:50 +02002789 goto good;
Chris Masond20f7042008-12-08 16:58:54 -05002790
Yan Zheng17d217f2008-12-12 10:03:38 -05002791 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002792 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002793 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
2794 GFP_NOFS);
2795 return 0;
2796 }
2797
Miao Xiefacc8a222013-07-25 19:22:34 +08002798 phy_offset >>= inode->i_sb->s_blocksize_bits;
2799 csum_expected = *(((u32 *)io_bio->csum) + phy_offset);
Chris Masond3977122009-01-05 21:25:51 -05002800
Miao Xiefacc8a222013-07-25 19:22:34 +08002801 kaddr = kmap_atomic(page);
Liu Bob0496682013-03-14 14:57:45 +00002802 csum = btrfs_csum_data(kaddr + offset, csum, end - start + 1);
Chris Masonff79f812007-10-15 16:22:25 -04002803 btrfs_csum_final(csum, (char *)&csum);
Miao Xiefacc8a222013-07-25 19:22:34 +08002804 if (csum != csum_expected)
Chris Mason07157aa2007-08-30 08:50:51 -04002805 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002806
Cong Wang7ac687d2011-11-25 23:14:28 +08002807 kunmap_atomic(kaddr);
Chris Masond20f7042008-12-08 16:58:54 -05002808good:
Chris Mason07157aa2007-08-30 08:50:51 -04002809 return 0;
2810
2811zeroit:
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002812 if (__ratelimit(&_rs))
Miao Xiefacc8a222013-07-25 19:22:34 +08002813 btrfs_info(root->fs_info, "csum failed ino %llu off %llu csum %u expected csum %u",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002814 btrfs_ino(page->mapping->host), start, csum, csum_expected);
Chris Masondb945352007-10-15 16:15:53 -04002815 memset(kaddr + offset, 1, end - start + 1);
2816 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002817 kunmap_atomic(kaddr);
Miao Xiefacc8a222013-07-25 19:22:34 +08002818 if (csum_expected == 0)
Chris Mason3b951512008-04-17 11:29:12 -04002819 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04002820 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04002821}
Chris Masonb888db2b2007-08-27 16:49:44 -04002822
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002823struct delayed_iput {
2824 struct list_head list;
2825 struct inode *inode;
2826};
2827
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002828/* JDM: If this is fs-wide, why can't we add a pointer to
2829 * btrfs_inode instead and avoid the allocation? */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002830void btrfs_add_delayed_iput(struct inode *inode)
2831{
2832 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2833 struct delayed_iput *delayed;
2834
2835 if (atomic_add_unless(&inode->i_count, -1, 1))
2836 return;
2837
2838 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2839 delayed->inode = inode;
2840
2841 spin_lock(&fs_info->delayed_iput_lock);
2842 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2843 spin_unlock(&fs_info->delayed_iput_lock);
2844}
2845
2846void btrfs_run_delayed_iputs(struct btrfs_root *root)
2847{
2848 LIST_HEAD(list);
2849 struct btrfs_fs_info *fs_info = root->fs_info;
2850 struct delayed_iput *delayed;
2851 int empty;
2852
2853 spin_lock(&fs_info->delayed_iput_lock);
2854 empty = list_empty(&fs_info->delayed_iputs);
2855 spin_unlock(&fs_info->delayed_iput_lock);
2856 if (empty)
2857 return;
2858
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002859 spin_lock(&fs_info->delayed_iput_lock);
2860 list_splice_init(&fs_info->delayed_iputs, &list);
2861 spin_unlock(&fs_info->delayed_iput_lock);
2862
2863 while (!list_empty(&list)) {
2864 delayed = list_entry(list.next, struct delayed_iput, list);
2865 list_del(&delayed->list);
2866 iput(delayed->inode);
2867 kfree(delayed);
2868 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002869}
2870
Yan, Zhengd68fc572010-05-16 10:49:58 -04002871/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08002872 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04002873 * files in the subvolume, it removes orphan item and frees block_rsv
2874 * structure.
2875 */
2876void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
2877 struct btrfs_root *root)
2878{
Josef Bacik90290e12011-12-02 15:44:12 -05002879 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002880 int ret;
2881
Josef Bacik8a35d952012-05-23 14:26:42 -04002882 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04002883 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
2884 return;
2885
Josef Bacik90290e12011-12-02 15:44:12 -05002886 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04002887 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05002888 spin_unlock(&root->orphan_lock);
2889 return;
2890 }
2891
2892 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
2893 spin_unlock(&root->orphan_lock);
2894 return;
2895 }
2896
2897 block_rsv = root->orphan_block_rsv;
2898 root->orphan_block_rsv = NULL;
2899 spin_unlock(&root->orphan_lock);
2900
Yan, Zhengd68fc572010-05-16 10:49:58 -04002901 if (root->orphan_item_inserted &&
2902 btrfs_root_refs(&root->root_item) > 0) {
2903 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
2904 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04002905 if (ret)
2906 btrfs_abort_transaction(trans, root, ret);
2907 else
2908 root->orphan_item_inserted = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002909 }
2910
Josef Bacik90290e12011-12-02 15:44:12 -05002911 if (block_rsv) {
2912 WARN_ON(block_rsv->size > 0);
2913 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002914 }
2915}
2916
2917/*
Josef Bacik7b128762008-07-24 12:17:14 -04002918 * This creates an orphan entry for the given inode in case something goes
2919 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04002920 *
2921 * NOTE: caller of this function should reserve 5 units of metadata for
2922 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04002923 */
2924int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2925{
2926 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002927 struct btrfs_block_rsv *block_rsv = NULL;
2928 int reserve = 0;
2929 int insert = 0;
2930 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002931
Yan, Zhengd68fc572010-05-16 10:49:58 -04002932 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06002933 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00002934 if (!block_rsv)
2935 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04002936 }
2937
Yan, Zhengd68fc572010-05-16 10:49:58 -04002938 spin_lock(&root->orphan_lock);
2939 if (!root->orphan_block_rsv) {
2940 root->orphan_block_rsv = block_rsv;
2941 } else if (block_rsv) {
2942 btrfs_free_block_rsv(root, block_rsv);
2943 block_rsv = NULL;
2944 }
Josef Bacik7b128762008-07-24 12:17:14 -04002945
Josef Bacik8a35d952012-05-23 14:26:42 -04002946 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2947 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04002948#if 0
2949 /*
2950 * For proper ENOSPC handling, we should do orphan
2951 * cleanup when mounting. But this introduces backward
2952 * compatibility issue.
2953 */
2954 if (!xchg(&root->orphan_item_inserted, 1))
2955 insert = 2;
2956 else
2957 insert = 1;
2958#endif
2959 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06002960 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002961 }
Josef Bacik7b128762008-07-24 12:17:14 -04002962
Josef Bacik72ac3c02012-05-23 14:13:11 -04002963 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
2964 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04002965 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002966 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002967
Yan, Zhengd68fc572010-05-16 10:49:58 -04002968 /* grab metadata reservation from transaction handle */
2969 if (reserve) {
2970 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002971 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04002972 }
2973
2974 /* insert an orphan item to track this unlinked/truncated file */
2975 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08002976 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04002977 if (ret) {
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01002978 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04002979 if (reserve) {
2980 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
2981 &BTRFS_I(inode)->runtime_flags);
2982 btrfs_orphan_release_metadata(inode);
2983 }
2984 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04002985 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2986 &BTRFS_I(inode)->runtime_flags);
Josef Bacik4ef31a42013-08-13 14:10:08 -04002987 btrfs_abort_transaction(trans, root, ret);
2988 return ret;
2989 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002990 }
2991 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002992 }
2993
2994 /* insert an orphan item to track subvolume contains orphan files */
2995 if (insert >= 2) {
2996 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
2997 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002998 if (ret && ret != -EEXIST) {
2999 btrfs_abort_transaction(trans, root, ret);
3000 return ret;
3001 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003002 }
3003 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003004}
3005
3006/*
3007 * We have done the truncate/delete so we can go ahead and remove the orphan
3008 * item for this particular inode.
3009 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003010static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3011 struct inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003012{
3013 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003014 int delete_item = 0;
3015 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003016 int ret = 0;
3017
Yan, Zhengd68fc572010-05-16 10:49:58 -04003018 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003019 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3020 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003021 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003022
Josef Bacik72ac3c02012-05-23 14:13:11 -04003023 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3024 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003025 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003026 spin_unlock(&root->orphan_lock);
3027
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003028 if (delete_item) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003029 atomic_dec(&root->orphan_inodes);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003030 if (trans)
3031 ret = btrfs_del_orphan_item(trans, root,
3032 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04003033 }
Josef Bacik7b128762008-07-24 12:17:14 -04003034
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003035 if (release_rsv)
3036 btrfs_orphan_release_metadata(inode);
3037
Josef Bacik4ef31a42013-08-13 14:10:08 -04003038 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003039}
3040
3041/*
3042 * this cleans up any orphans that may be left on the list from the last use
3043 * of this root.
3044 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003045int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003046{
3047 struct btrfs_path *path;
3048 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003049 struct btrfs_key key, found_key;
3050 struct btrfs_trans_handle *trans;
3051 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003052 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003053 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3054
Yan, Zhengd68fc572010-05-16 10:49:58 -04003055 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003056 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003057
3058 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003059 if (!path) {
3060 ret = -ENOMEM;
3061 goto out;
3062 }
Josef Bacik7b128762008-07-24 12:17:14 -04003063 path->reada = -1;
3064
3065 key.objectid = BTRFS_ORPHAN_OBJECTID;
3066 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
3067 key.offset = (u64)-1;
3068
Josef Bacik7b128762008-07-24 12:17:14 -04003069 while (1) {
3070 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003071 if (ret < 0)
3072 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003073
3074 /*
3075 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003076 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003077 * find the key and see if we have stuff that matches
3078 */
3079 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003080 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003081 if (path->slots[0] == 0)
3082 break;
3083 path->slots[0]--;
3084 }
3085
3086 /* pull out the item */
3087 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003088 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3089
3090 /* make sure the item matches what we want */
3091 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3092 break;
3093 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
3094 break;
3095
3096 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003097 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003098
3099 /*
3100 * this is where we are basically btrfs_lookup, without the
3101 * crossing root thing. we store the inode number in the
3102 * offset of the orphan item.
3103 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003104
3105 if (found_key.offset == last_objectid) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003106 btrfs_err(root->fs_info,
3107 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003108 ret = -EINVAL;
3109 goto out;
3110 }
3111
3112 last_objectid = found_key.offset;
3113
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003114 found_key.objectid = found_key.offset;
3115 found_key.type = BTRFS_INODE_ITEM_KEY;
3116 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00003117 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303118 ret = PTR_ERR_OR_ZERO(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003119 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003120 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003121
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003122 if (ret == -ESTALE && root == root->fs_info->tree_root) {
3123 struct btrfs_root *dead_root;
3124 struct btrfs_fs_info *fs_info = root->fs_info;
3125 int is_dead_root = 0;
3126
3127 /*
3128 * this is an orphan in the tree root. Currently these
3129 * could come from 2 sources:
3130 * a) a snapshot deletion in progress
3131 * b) a free space cache inode
3132 * We need to distinguish those two, as the snapshot
3133 * orphan must not get deleted.
3134 * find_dead_roots already ran before us, so if this
3135 * is a snapshot deletion, we should find the root
3136 * in the dead_roots list
3137 */
3138 spin_lock(&fs_info->trans_lock);
3139 list_for_each_entry(dead_root, &fs_info->dead_roots,
3140 root_list) {
3141 if (dead_root->root_key.objectid ==
3142 found_key.objectid) {
3143 is_dead_root = 1;
3144 break;
3145 }
3146 }
3147 spin_unlock(&fs_info->trans_lock);
3148 if (is_dead_root) {
3149 /* prevent this orphan from being found again */
3150 key.offset = found_key.objectid - 1;
3151 continue;
3152 }
3153 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003154 /*
3155 * Inode is already gone but the orphan item is still there,
3156 * kill the orphan item.
3157 */
3158 if (ret == -ESTALE) {
3159 trans = btrfs_start_transaction(root, 1);
3160 if (IS_ERR(trans)) {
3161 ret = PTR_ERR(trans);
3162 goto out;
3163 }
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003164 btrfs_debug(root->fs_info, "auto deleting %Lu",
3165 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003166 ret = btrfs_del_orphan_item(trans, root,
3167 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003168 btrfs_end_transaction(trans, root);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003169 if (ret)
3170 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003171 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003172 }
Josef Bacik7b128762008-07-24 12:17:14 -04003173
Josef Bacik7b128762008-07-24 12:17:14 -04003174 /*
3175 * add this inode to the orphan list so btrfs_orphan_del does
3176 * the proper thing when we hit it
3177 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003178 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3179 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003180 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003181
Josef Bacik7b128762008-07-24 12:17:14 -04003182 /* if we have links, this was a truncate, lets do that */
3183 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303184 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003185 iput(inode);
3186 continue;
3187 }
Josef Bacik7b128762008-07-24 12:17:14 -04003188 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003189
3190 /* 1 for the orphan item deletion. */
3191 trans = btrfs_start_transaction(root, 1);
3192 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003193 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003194 ret = PTR_ERR(trans);
3195 goto out;
3196 }
3197 ret = btrfs_orphan_add(trans, inode);
3198 btrfs_end_transaction(trans, root);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003199 if (ret) {
3200 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003201 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003202 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003203
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003204 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003205 if (ret)
3206 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003207 } else {
3208 nr_unlink++;
3209 }
3210
3211 /* this will do delete_inode and everything for us */
3212 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003213 if (ret)
3214 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003215 }
Miao Xie3254c872011-11-10 20:45:05 -05003216 /* release the path since we're done with it */
3217 btrfs_release_path(path);
3218
Yan, Zhengd68fc572010-05-16 10:49:58 -04003219 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3220
3221 if (root->orphan_block_rsv)
3222 btrfs_block_rsv_release(root, root->orphan_block_rsv,
3223 (u64)-1);
3224
3225 if (root->orphan_block_rsv || root->orphan_item_inserted) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003226 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003227 if (!IS_ERR(trans))
3228 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003229 }
Josef Bacik7b128762008-07-24 12:17:14 -04003230
3231 if (nr_unlink)
David Sterba4884b472013-03-20 13:31:27 +00003232 btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003233 if (nr_truncate)
David Sterba4884b472013-03-20 13:31:27 +00003234 btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003235
3236out:
3237 if (ret)
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003238 btrfs_crit(root->fs_info,
3239 "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003240 btrfs_free_path(path);
3241 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003242}
3243
Chris Masond352ac62008-09-29 15:18:18 -04003244/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003245 * very simple check to peek ahead in the leaf looking for xattrs. If we
3246 * don't find any xattrs, we know there can't be any acls.
3247 *
3248 * slot is the slot the inode is in, objectid is the objectid of the inode
3249 */
3250static noinline int acls_after_inode_item(struct extent_buffer *leaf,
3251 int slot, u64 objectid)
3252{
3253 u32 nritems = btrfs_header_nritems(leaf);
3254 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003255 static u64 xattr_access = 0;
3256 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003257 int scanned = 0;
3258
Josef Bacikf23b5a52013-06-19 10:16:26 -04003259 if (!xattr_access) {
3260 xattr_access = btrfs_name_hash(POSIX_ACL_XATTR_ACCESS,
3261 strlen(POSIX_ACL_XATTR_ACCESS));
3262 xattr_default = btrfs_name_hash(POSIX_ACL_XATTR_DEFAULT,
3263 strlen(POSIX_ACL_XATTR_DEFAULT));
3264 }
3265
Chris Mason46a53cc2009-04-27 11:47:50 -04003266 slot++;
3267 while (slot < nritems) {
3268 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3269
3270 /* we found a different objectid, there must not be acls */
3271 if (found_key.objectid != objectid)
3272 return 0;
3273
3274 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003275 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
3276 if (found_key.offset == xattr_access ||
3277 found_key.offset == xattr_default)
3278 return 1;
3279 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003280
3281 /*
3282 * we found a key greater than an xattr key, there can't
3283 * be any acls later on
3284 */
3285 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3286 return 0;
3287
3288 slot++;
3289 scanned++;
3290
3291 /*
3292 * it goes inode, inode backrefs, xattrs, extents,
3293 * so if there are a ton of hard links to an inode there can
3294 * be a lot of backrefs. Don't waste time searching too hard,
3295 * this is just an optimization
3296 */
3297 if (scanned >= 8)
3298 break;
3299 }
3300 /* we hit the end of the leaf before we found an xattr or
3301 * something larger than an xattr. We have to assume the inode
3302 * has acls
3303 */
3304 return 1;
3305}
3306
3307/*
Chris Masond352ac62008-09-29 15:18:18 -04003308 * read an inode from the btree into the in-memory inode
3309 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003310static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003311{
3312 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003313 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003314 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04003315 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04003316 struct btrfs_root *root = BTRFS_I(inode)->root;
3317 struct btrfs_key location;
Chris Mason46a53cc2009-04-27 11:47:50 -04003318 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003319 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003320 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003321 bool filled = false;
3322
3323 ret = btrfs_fill_inode(inode, &rdev);
3324 if (!ret)
3325 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003326
3327 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07003328 if (!path)
3329 goto make_bad;
3330
Josef Bacikd90c7322011-05-17 09:50:54 -04003331 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003332 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003333
Chris Mason39279cc2007-06-12 06:35:45 -04003334 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003335 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04003336 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04003337
Chris Mason5f39d392007-10-15 16:14:19 -04003338 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003339
3340 if (filled)
3341 goto cache_acl;
3342
Chris Mason5f39d392007-10-15 16:14:19 -04003343 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3344 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003345 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003346 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003347 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3348 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04003349 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003350
3351 tspec = btrfs_inode_atime(inode_item);
3352 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3353 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3354
3355 tspec = btrfs_inode_mtime(inode_item);
3356 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3357 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3358
3359 tspec = btrfs_inode_ctime(inode_item);
3360 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3361 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3362
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003363 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003364 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003365 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3366
3367 /*
3368 * If we were modified in the current generation and evicted from memory
3369 * and then re-read we need to do a full sync since we don't have any
3370 * idea about which extents were modified before we were evicted from
3371 * cache.
3372 */
3373 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
3374 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3375 &BTRFS_I(inode)->runtime_flags);
3376
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003377 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003378 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003379 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003380 rdev = btrfs_inode_rdev(leaf, inode_item);
3381
Josef Bacikaec74772008-07-24 12:12:38 -04003382 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003383 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie2f7e33d2011-06-23 07:27:13 +00003384cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003385 /*
3386 * try to precache a NULL acl entry for files that don't have
3387 * any xattrs or acls
3388 */
Li Zefan33345d012011-04-20 10:31:50 +08003389 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
3390 btrfs_ino(inode));
Al Viro72c04902009-06-24 16:58:48 -04003391 if (!maybe_acls)
3392 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003393
Chris Mason39279cc2007-06-12 06:35:45 -04003394 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003395
Chris Mason39279cc2007-06-12 06:35:45 -04003396 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003397 case S_IFREG:
3398 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003399 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05003400 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003401 inode->i_fop = &btrfs_file_operations;
3402 inode->i_op = &btrfs_file_inode_operations;
3403 break;
3404 case S_IFDIR:
3405 inode->i_fop = &btrfs_dir_file_operations;
3406 if (root == root->fs_info->tree_root)
3407 inode->i_op = &btrfs_dir_ro_inode_operations;
3408 else
3409 inode->i_op = &btrfs_dir_inode_operations;
3410 break;
3411 case S_IFLNK:
3412 inode->i_op = &btrfs_symlink_inode_operations;
3413 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04003414 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003415 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003416 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003417 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003418 init_special_inode(inode, inode->i_mode, rdev);
3419 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003420 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003421
3422 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003423 return;
3424
3425make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003426 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003427 make_bad_inode(inode);
3428}
3429
Chris Masond352ac62008-09-29 15:18:18 -04003430/*
3431 * given a leaf and an inode, copy the inode fields into the leaf
3432 */
Chris Masone02119d2008-09-05 16:13:11 -04003433static void fill_inode_item(struct btrfs_trans_handle *trans,
3434 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003435 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003436 struct inode *inode)
3437{
Liu Bo51fab692012-12-27 09:01:21 +00003438 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003439
Liu Bo51fab692012-12-27 09:01:21 +00003440 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003441
Liu Bo51fab692012-12-27 09:01:21 +00003442 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3443 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3444 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3445 &token);
3446 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3447 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003448
Liu Bo51fab692012-12-27 09:01:21 +00003449 btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item),
3450 inode->i_atime.tv_sec, &token);
3451 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item),
3452 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003453
Liu Bo51fab692012-12-27 09:01:21 +00003454 btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item),
3455 inode->i_mtime.tv_sec, &token);
3456 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item),
3457 inode->i_mtime.tv_nsec, &token);
3458
3459 btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item),
3460 inode->i_ctime.tv_sec, &token);
3461 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item),
3462 inode->i_ctime.tv_nsec, &token);
3463
3464 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3465 &token);
3466 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3467 &token);
3468 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3469 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3470 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3471 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3472 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003473}
3474
Chris Masond352ac62008-09-29 15:18:18 -04003475/*
3476 * copy everything in the in-memory inode into the btree.
3477 */
Chris Mason21151332011-11-10 20:39:08 -05003478static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003479 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003480{
3481 struct btrfs_inode_item *inode_item;
3482 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003483 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003484 int ret;
3485
3486 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003487 if (!path)
3488 return -ENOMEM;
3489
Chris Masonb9473432009-03-13 11:00:37 -04003490 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003491 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3492 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003493 if (ret) {
3494 if (ret > 0)
3495 ret = -ENOENT;
3496 goto failed;
3497 }
3498
Chris Masonb4ce94d2009-02-04 09:25:08 -05003499 btrfs_unlock_up_safe(path, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003500 leaf = path->nodes[0];
3501 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003502 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003503
Chris Masone02119d2008-09-05 16:13:11 -04003504 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003505 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003506 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003507 ret = 0;
3508failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003509 btrfs_free_path(path);
3510 return ret;
3511}
3512
Chris Masond352ac62008-09-29 15:18:18 -04003513/*
Chris Mason21151332011-11-10 20:39:08 -05003514 * copy everything in the in-memory inode into the btree.
3515 */
3516noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3517 struct btrfs_root *root, struct inode *inode)
3518{
3519 int ret;
3520
3521 /*
3522 * If the inode is a free space inode, we can deadlock during commit
3523 * if we put it into the delayed code.
3524 *
3525 * The data relocation inode should also be directly updated
3526 * without delay
3527 */
Liu Bo83eea1f2012-07-10 05:28:39 -06003528 if (!btrfs_is_free_space_inode(inode)
Chris Mason21151332011-11-10 20:39:08 -05003529 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003530 btrfs_update_root_times(trans, root);
3531
Chris Mason21151332011-11-10 20:39:08 -05003532 ret = btrfs_delayed_update_inode(trans, root, inode);
3533 if (!ret)
3534 btrfs_set_inode_last_trans(trans, inode);
3535 return ret;
3536 }
3537
3538 return btrfs_update_inode_item(trans, root, inode);
3539}
3540
Josef Bacikbe6aef62012-10-22 15:43:12 -04003541noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3542 struct btrfs_root *root,
3543 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003544{
3545 int ret;
3546
3547 ret = btrfs_update_inode(trans, root, inode);
3548 if (ret == -ENOSPC)
3549 return btrfs_update_inode_item(trans, root, inode);
3550 return ret;
3551}
3552
3553/*
Chris Masond352ac62008-09-29 15:18:18 -04003554 * unlink helper that gets used here in inode.c and in the tree logging
3555 * recovery code. It remove a link in a directory with a given name, and
3556 * also drops the back refs in the inode to the directory
3557 */
Al Viro92986792011-03-04 17:14:37 +00003558static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3559 struct btrfs_root *root,
3560 struct inode *dir, struct inode *inode,
3561 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003562{
3563 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003564 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003565 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003566 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003567 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003568 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003569 u64 ino = btrfs_ino(inode);
3570 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003571
3572 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003573 if (!path) {
3574 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003575 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003576 }
3577
Chris Masonb9473432009-03-13 11:00:37 -04003578 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003579 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003580 name, name_len, -1);
3581 if (IS_ERR(di)) {
3582 ret = PTR_ERR(di);
3583 goto err;
3584 }
3585 if (!di) {
3586 ret = -ENOENT;
3587 goto err;
3588 }
Chris Mason5f39d392007-10-15 16:14:19 -04003589 leaf = path->nodes[0];
3590 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003591 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003592 if (ret)
3593 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003594 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003595
Li Zefan33345d012011-04-20 10:31:50 +08003596 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3597 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003598 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003599 btrfs_info(root->fs_info,
3600 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003601 name_len, name, ino, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003602 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003603 goto err;
3604 }
3605
Miao Xie16cdcec2011-04-22 18:12:22 +08003606 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003607 if (ret) {
3608 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003609 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003610 }
Chris Mason39279cc2007-06-12 06:35:45 -04003611
Chris Masone02119d2008-09-05 16:13:11 -04003612 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08003613 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003614 if (ret != 0 && ret != -ENOENT) {
3615 btrfs_abort_transaction(trans, root, ret);
3616 goto err;
3617 }
Chris Masone02119d2008-09-05 16:13:11 -04003618
3619 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
3620 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04003621 if (ret == -ENOENT)
3622 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003623 else if (ret)
3624 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003625err:
3626 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003627 if (ret)
3628 goto out;
3629
3630 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003631 inode_inc_iversion(inode);
3632 inode_inc_iversion(dir);
Chris Masone02119d2008-09-05 16:13:11 -04003633 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Tsutomu Itohb9959292012-06-25 21:25:22 -06003634 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04003635out:
Chris Mason39279cc2007-06-12 06:35:45 -04003636 return ret;
3637}
3638
Al Viro92986792011-03-04 17:14:37 +00003639int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3640 struct btrfs_root *root,
3641 struct inode *dir, struct inode *inode,
3642 const char *name, int name_len)
3643{
3644 int ret;
3645 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3646 if (!ret) {
Zach Brown8b558c52013-10-16 12:10:34 -07003647 drop_nlink(inode);
Al Viro92986792011-03-04 17:14:37 +00003648 ret = btrfs_update_inode(trans, root, inode);
3649 }
3650 return ret;
3651}
Yan, Zhenga22285a2010-05-16 10:48:46 -04003652
3653/*
3654 * helper to start transaction for unlink and rmdir.
3655 *
Josef Bacikd52be812013-05-29 14:54:47 -04003656 * unlink and rmdir are special in btrfs, they do not always free space, so
3657 * if we cannot make our reservations the normal way try and see if there is
3658 * plenty of slack room in the global reserve to migrate, otherwise we cannot
3659 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04003660 */
Josef Bacikd52be812013-05-29 14:54:47 -04003661static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003662{
3663 struct btrfs_trans_handle *trans;
3664 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003665 int ret;
3666
Josef Bacike70bea52011-10-11 14:18:24 -04003667 /*
3668 * 1 for the possible orphan item
3669 * 1 for the dir item
3670 * 1 for the dir index
3671 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04003672 * 1 for the inode
3673 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04003674 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003675 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
3676 return trans;
3677
Josef Bacikd52be812013-05-29 14:54:47 -04003678 if (PTR_ERR(trans) == -ENOSPC) {
3679 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003680
Josef Bacikd52be812013-05-29 14:54:47 -04003681 trans = btrfs_start_transaction(root, 0);
3682 if (IS_ERR(trans))
3683 return trans;
3684 ret = btrfs_cond_migrate_bytes(root->fs_info,
3685 &root->fs_info->trans_block_rsv,
3686 num_bytes, 5);
3687 if (ret) {
3688 btrfs_end_transaction(trans, root);
3689 return ERR_PTR(ret);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003690 }
Josef Bacik5a77d762011-11-01 14:32:23 -04003691 trans->block_rsv = &root->fs_info->trans_block_rsv;
Josef Bacikd52be812013-05-29 14:54:47 -04003692 trans->bytes_reserved = num_bytes;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003693 }
Josef Bacikd52be812013-05-29 14:54:47 -04003694 return trans;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003695}
3696
Chris Mason39279cc2007-06-12 06:35:45 -04003697static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3698{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003699 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003700 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04003701 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003702 int ret;
3703
Josef Bacikd52be812013-05-29 14:54:47 -04003704 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003705 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003706 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003707
Chris Mason12fcfd22009-03-24 10:24:20 -04003708 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
3709
Chris Masone02119d2008-09-05 16:13:11 -04003710 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3711 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003712 if (ret)
3713 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003714
Yan, Zhenga22285a2010-05-16 10:48:46 -04003715 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04003716 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003717 if (ret)
3718 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003719 }
Josef Bacik7b128762008-07-24 12:17:14 -04003720
Tsutomu Itohb5324022011-07-19 07:27:20 +00003721out:
Josef Bacikd52be812013-05-29 14:54:47 -04003722 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003723 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04003724 return ret;
3725}
3726
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003727int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3728 struct btrfs_root *root,
3729 struct inode *dir, u64 objectid,
3730 const char *name, int name_len)
3731{
3732 struct btrfs_path *path;
3733 struct extent_buffer *leaf;
3734 struct btrfs_dir_item *di;
3735 struct btrfs_key key;
3736 u64 index;
3737 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003738 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003739
3740 path = btrfs_alloc_path();
3741 if (!path)
3742 return -ENOMEM;
3743
Li Zefan33345d012011-04-20 10:31:50 +08003744 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003745 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003746 if (IS_ERR_OR_NULL(di)) {
3747 if (!di)
3748 ret = -ENOENT;
3749 else
3750 ret = PTR_ERR(di);
3751 goto out;
3752 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003753
3754 leaf = path->nodes[0];
3755 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3756 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3757 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003758 if (ret) {
3759 btrfs_abort_transaction(trans, root, ret);
3760 goto out;
3761 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003762 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003763
3764 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3765 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003766 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003767 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003768 if (ret != -ENOENT) {
3769 btrfs_abort_transaction(trans, root, ret);
3770 goto out;
3771 }
Li Zefan33345d012011-04-20 10:31:50 +08003772 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003773 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003774 if (IS_ERR_OR_NULL(di)) {
3775 if (!di)
3776 ret = -ENOENT;
3777 else
3778 ret = PTR_ERR(di);
3779 btrfs_abort_transaction(trans, root, ret);
3780 goto out;
3781 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003782
3783 leaf = path->nodes[0];
3784 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003785 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003786 index = key.offset;
3787 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003788 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003789
Miao Xie16cdcec2011-04-22 18:12:22 +08003790 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003791 if (ret) {
3792 btrfs_abort_transaction(trans, root, ret);
3793 goto out;
3794 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003795
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003796 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003797 inode_inc_iversion(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003798 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Josef Bacik5a24e842012-08-08 10:12:59 -06003799 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003800 if (ret)
3801 btrfs_abort_transaction(trans, root, ret);
3802out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04003803 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003804 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003805}
3806
Chris Mason39279cc2007-06-12 06:35:45 -04003807static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3808{
3809 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003810 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003811 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003812 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04003813
David Sterbab3ae2442012-09-13 16:04:34 -06003814 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04003815 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06003816 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
3817 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04003818
Josef Bacikd52be812013-05-29 14:54:47 -04003819 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003820 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003821 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003822
Li Zefan33345d012011-04-20 10:31:50 +08003823 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003824 err = btrfs_unlink_subvol(trans, root, dir,
3825 BTRFS_I(inode)->location.objectid,
3826 dentry->d_name.name,
3827 dentry->d_name.len);
3828 goto out;
3829 }
3830
Josef Bacik7b128762008-07-24 12:17:14 -04003831 err = btrfs_orphan_add(trans, inode);
3832 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003833 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003834
Chris Mason39279cc2007-06-12 06:35:45 -04003835 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04003836 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3837 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05003838 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04003839 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003840out:
Josef Bacikd52be812013-05-29 14:54:47 -04003841 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003842 btrfs_btree_balance_dirty(root);
Chris Mason39544012007-12-12 14:38:19 -05003843
Chris Mason39279cc2007-06-12 06:35:45 -04003844 return err;
3845}
3846
Chris Mason323ac952008-10-01 19:05:46 -04003847/*
Chris Mason39279cc2007-06-12 06:35:45 -04003848 * this can truncate away extent items, csum items and directory items.
3849 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04003850 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04003851 *
3852 * csum items that cross the new i_size are truncated to the new size
3853 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04003854 *
3855 * min_type is the minimum key type to truncate down to. If set to 0, this
3856 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04003857 */
Yan, Zheng80825102009-11-12 09:35:36 +00003858int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
3859 struct btrfs_root *root,
3860 struct inode *inode,
3861 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003862{
Chris Mason39279cc2007-06-12 06:35:45 -04003863 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003864 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003865 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00003866 struct btrfs_key key;
3867 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04003868 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04003869 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003870 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003871 u64 item_end = 0;
Josef Bacik7f4f6e02013-08-29 16:43:28 -04003872 u64 last_size = (u64)-1;
Yan, Zheng80825102009-11-12 09:35:36 +00003873 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04003874 int found_extent;
3875 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003876 int pending_del_nr = 0;
3877 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04003878 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00003879 int ret;
3880 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08003881 u64 ino = btrfs_ino(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003882
3883 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04003884
Mark Fasheh0eb0e192011-07-12 16:44:10 -07003885 path = btrfs_alloc_path();
3886 if (!path)
3887 return -ENOMEM;
3888 path->reada = -1;
3889
Josef Bacik5dc562c2012-08-17 13:14:17 -04003890 /*
3891 * We want to drop from the next block forward in case this new size is
3892 * not block aligned since we will be keeping the last block of the
3893 * extent just the way it is.
3894 */
Josef Bacik0af3d002010-06-21 14:48:16 -04003895 if (root->ref_cows || root == root->fs_info->tree_root)
Qu Wenruofda28322013-02-26 08:10:22 +00003896 btrfs_drop_extent_cache(inode, ALIGN(new_size,
3897 root->sectorsize), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003898
Miao Xie16cdcec2011-04-22 18:12:22 +08003899 /*
3900 * This function is also used to drop the items in the log tree before
3901 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
3902 * it is used to drop the loged items. So we shouldn't kill the delayed
3903 * items.
3904 */
3905 if (min_type == 0 && root == BTRFS_I(inode)->root)
3906 btrfs_kill_delayed_inode_items(inode);
3907
Li Zefan33345d012011-04-20 10:31:50 +08003908 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04003909 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04003910 key.type = (u8)-1;
3911
Chris Mason85e21ba2008-01-29 15:11:36 -05003912search_again:
Chris Masonb9473432009-03-13 11:00:37 -04003913 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05003914 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00003915 if (ret < 0) {
3916 err = ret;
3917 goto out;
3918 }
Chris Masond3977122009-01-05 21:25:51 -05003919
Chris Mason85e21ba2008-01-29 15:11:36 -05003920 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003921 /* there are no items in the tree for us to truncate, we're
3922 * done
3923 */
Yan, Zheng80825102009-11-12 09:35:36 +00003924 if (path->slots[0] == 0)
3925 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05003926 path->slots[0]--;
3927 }
3928
Chris Masond3977122009-01-05 21:25:51 -05003929 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04003930 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04003931 leaf = path->nodes[0];
3932 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3933 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04003934
Li Zefan33345d012011-04-20 10:31:50 +08003935 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04003936 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003937
Chris Mason85e21ba2008-01-29 15:11:36 -05003938 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003939 break;
3940
Chris Mason5f39d392007-10-15 16:14:19 -04003941 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04003942 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04003943 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04003944 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04003945 extent_type = btrfs_file_extent_type(leaf, fi);
3946 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04003947 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04003948 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04003949 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04003950 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04003951 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003952 }
Yan008630c2007-11-07 13:31:09 -05003953 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04003954 }
Yan, Zheng80825102009-11-12 09:35:36 +00003955 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04003956 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00003957 } else {
3958 if (item_end < new_size)
3959 break;
3960 if (found_key.offset >= new_size)
3961 del_item = 1;
3962 else
3963 del_item = 0;
3964 }
Chris Mason39279cc2007-06-12 06:35:45 -04003965 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003966 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04003967 if (found_type != BTRFS_EXTENT_DATA_KEY)
3968 goto delete;
3969
Josef Bacik7f4f6e02013-08-29 16:43:28 -04003970 if (del_item)
3971 last_size = found_key.offset;
3972 else
3973 last_size = new_size;
3974
Chris Mason179e29e2007-11-01 11:28:41 -04003975 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04003976 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04003977 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05003978 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04003979 u64 orig_num_bytes =
3980 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00003981 extent_num_bytes = ALIGN(new_size -
3982 found_key.offset,
3983 root->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04003984 btrfs_set_file_extent_num_bytes(leaf, fi,
3985 extent_num_bytes);
3986 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05003987 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04003988 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003989 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04003990 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003991 } else {
Chris Masondb945352007-10-15 16:15:53 -04003992 extent_num_bytes =
3993 btrfs_file_extent_disk_num_bytes(leaf,
3994 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003995 extent_offset = found_key.offset -
3996 btrfs_file_extent_offset(leaf, fi);
3997
Chris Mason39279cc2007-06-12 06:35:45 -04003998 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05003999 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004000 if (extent_start != 0) {
4001 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04004002 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004003 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004004 }
4005 }
Chris Mason90692182008-02-08 13:49:28 -05004006 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004007 /*
4008 * we can't truncate inline items that have had
4009 * special encodings
4010 */
4011 if (!del_item &&
4012 btrfs_file_extent_compression(leaf, fi) == 0 &&
4013 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4014 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004015 u32 size = new_size - found_key.offset;
4016
4017 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004018 inode_sub_bytes(inode, item_end + 1 -
4019 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04004020 }
4021 size =
4022 btrfs_file_extent_calc_inline_size(size);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004023 btrfs_truncate_item(root, path, size, 1);
Chris Masone02119d2008-09-05 16:13:11 -04004024 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004025 inode_sub_bytes(inode, item_end + 1 -
4026 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05004027 }
Chris Mason39279cc2007-06-12 06:35:45 -04004028 }
Chris Mason179e29e2007-11-01 11:28:41 -04004029delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004030 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004031 if (!pending_del_nr) {
4032 /* no pending yet, add ourselves */
4033 pending_del_slot = path->slots[0];
4034 pending_del_nr = 1;
4035 } else if (pending_del_nr &&
4036 path->slots[0] + 1 == pending_del_slot) {
4037 /* hop on the pending chunk */
4038 pending_del_nr++;
4039 pending_del_slot = path->slots[0];
4040 } else {
Chris Masond3977122009-01-05 21:25:51 -05004041 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004042 }
Chris Mason39279cc2007-06-12 06:35:45 -04004043 } else {
4044 break;
4045 }
Josef Bacik0af3d002010-06-21 14:48:16 -04004046 if (found_extent && (root->ref_cows ||
4047 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004048 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004049 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004050 extent_num_bytes, 0,
4051 btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02004052 ino, extent_offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004053 BUG_ON(ret);
4054 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004055
Yan, Zheng80825102009-11-12 09:35:36 +00004056 if (found_type == BTRFS_INODE_ITEM_KEY)
4057 break;
4058
4059 if (path->slots[0] == 0 ||
4060 path->slots[0] != pending_del_slot) {
Yan, Zheng80825102009-11-12 09:35:36 +00004061 if (pending_del_nr) {
4062 ret = btrfs_del_items(trans, root, path,
4063 pending_del_slot,
4064 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004065 if (ret) {
4066 btrfs_abort_transaction(trans,
4067 root, ret);
4068 goto error;
4069 }
Yan, Zheng80825102009-11-12 09:35:36 +00004070 pending_del_nr = 0;
4071 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004072 btrfs_release_path(path);
Chris Mason85e21ba2008-01-29 15:11:36 -05004073 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004074 } else {
4075 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004076 }
Chris Mason39279cc2007-06-12 06:35:45 -04004077 }
Yan, Zheng80825102009-11-12 09:35:36 +00004078out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004079 if (pending_del_nr) {
4080 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4081 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004082 if (ret)
4083 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004084 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004085error:
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004086 if (last_size != (u64)-1)
4087 btrfs_ordered_update_i_size(inode, last_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004088 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00004089 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004090}
4091
Chris Masona52d9a82007-08-27 16:49:44 -04004092/*
Josef Bacik2aaa6652012-08-29 14:27:18 -04004093 * btrfs_truncate_page - read, zero a chunk and write a page
4094 * @inode - inode that we're zeroing
4095 * @from - the offset to start zeroing
4096 * @len - the length to zero, 0 to zero the entire range respective to the
4097 * offset
4098 * @front - zero up to the offset instead of from the offset on
4099 *
4100 * This will find the page for the "from" offset and cow the page and zero the
4101 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004102 */
Josef Bacik2aaa6652012-08-29 14:27:18 -04004103int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
4104 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004105{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004106 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04004107 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004108 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4109 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004110 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004111 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04004112 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04004113 pgoff_t index = from >> PAGE_CACHE_SHIFT;
4114 unsigned offset = from & (PAGE_CACHE_SIZE-1);
4115 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004116 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004117 int ret = 0;
4118 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004119 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004120
Josef Bacik2aaa6652012-08-29 14:27:18 -04004121 if ((offset & (blocksize - 1)) == 0 &&
4122 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004123 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004124 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004125 if (ret)
4126 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004127
Chris Mason211c17f2008-05-15 09:13:45 -04004128again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004129 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004130 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004131 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Miao Xieac6a2b32012-12-05 10:56:13 +00004132 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004133 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004134 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004135
4136 page_start = page_offset(page);
4137 page_end = page_start + PAGE_CACHE_SIZE - 1;
4138
Chris Masona52d9a82007-08-27 16:49:44 -04004139 if (!PageUptodate(page)) {
4140 ret = btrfs_readpage(NULL, page);
4141 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004142 if (page->mapping != mapping) {
4143 unlock_page(page);
4144 page_cache_release(page);
4145 goto again;
4146 }
Chris Masona52d9a82007-08-27 16:49:44 -04004147 if (!PageUptodate(page)) {
4148 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004149 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004150 }
4151 }
Chris Mason211c17f2008-05-15 09:13:45 -04004152 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004153
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004154 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004155 set_page_extent_mapped(page);
4156
4157 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4158 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004159 unlock_extent_cached(io_tree, page_start, page_end,
4160 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004161 unlock_page(page);
4162 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004163 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004164 btrfs_put_ordered_extent(ordered);
4165 goto again;
4166 }
4167
Josef Bacik2ac55d42010-02-03 19:33:23 +00004168 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004169 EXTENT_DIRTY | EXTENT_DELALLOC |
4170 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004171 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004172
Josef Bacik2ac55d42010-02-03 19:33:23 +00004173 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
4174 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004175 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004176 unlock_extent_cached(io_tree, page_start, page_end,
4177 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004178 goto out_unlock;
4179 }
4180
Chris Masone6dcd2d2008-07-17 12:53:50 -04004181 if (offset != PAGE_CACHE_SIZE) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004182 if (!len)
4183 len = PAGE_CACHE_SIZE - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004184 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004185 if (front)
4186 memset(kaddr, 0, offset);
4187 else
4188 memset(kaddr + offset, 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004189 flush_dcache_page(page);
4190 kunmap(page);
4191 }
Chris Mason247e7432008-07-17 12:53:51 -04004192 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004193 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004194 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
4195 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004196
Chris Mason89642222008-07-24 09:41:53 -04004197out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004198 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004199 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04004200 unlock_page(page);
4201 page_cache_release(page);
4202out:
4203 return ret;
4204}
4205
Josef Bacik16e75492013-10-22 12:18:51 -04004206static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4207 u64 offset, u64 len)
4208{
4209 struct btrfs_trans_handle *trans;
4210 int ret;
4211
4212 /*
4213 * Still need to make sure the inode looks like it's been updated so
4214 * that any holes get logged if we fsync.
4215 */
4216 if (btrfs_fs_incompat(root->fs_info, NO_HOLES)) {
4217 BTRFS_I(inode)->last_trans = root->fs_info->generation;
4218 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4219 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4220 return 0;
4221 }
4222
4223 /*
4224 * 1 - for the one we're dropping
4225 * 1 - for the one we're adding
4226 * 1 - for updating the inode.
4227 */
4228 trans = btrfs_start_transaction(root, 3);
4229 if (IS_ERR(trans))
4230 return PTR_ERR(trans);
4231
4232 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4233 if (ret) {
4234 btrfs_abort_transaction(trans, root, ret);
4235 btrfs_end_transaction(trans, root);
4236 return ret;
4237 }
4238
4239 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset,
4240 0, 0, len, 0, len, 0, 0, 0);
4241 if (ret)
4242 btrfs_abort_transaction(trans, root, ret);
4243 else
4244 btrfs_update_inode(trans, root, inode);
4245 btrfs_end_transaction(trans, root);
4246 return ret;
4247}
4248
Josef Bacik695a0d02011-03-04 15:46:53 -05004249/*
4250 * This function puts in dummy file extents for the area we're creating a hole
4251 * for. So if we are truncating this file to a larger size we need to insert
4252 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4253 * the range between oldsize and size
4254 */
Josef Bacika41ad392011-01-31 15:30:16 -05004255int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004256{
Yan Zheng9036c102008-10-30 14:19:41 -04004257 struct btrfs_root *root = BTRFS_I(inode)->root;
4258 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004259 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004260 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004261 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Qu Wenruofda28322013-02-26 08:10:22 +00004262 u64 hole_start = ALIGN(oldsize, root->sectorsize);
4263 u64 block_end = ALIGN(size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004264 u64 last_byte;
4265 u64 cur_offset;
4266 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004267 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004268
Josef Bacika71754f2013-06-17 17:14:39 -04004269 /*
4270 * If our size started in the middle of a page we need to zero out the
4271 * rest of the page before we expand the i_size, otherwise we could
4272 * expose stale data.
4273 */
4274 err = btrfs_truncate_page(inode, oldsize, 0, 0);
4275 if (err)
4276 return err;
4277
Yan Zheng9036c102008-10-30 14:19:41 -04004278 if (size <= hole_start)
4279 return 0;
4280
Yan Zheng9036c102008-10-30 14:19:41 -04004281 while (1) {
4282 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004283
Josef Bacik2ac55d42010-02-03 19:33:23 +00004284 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004285 &cached_state);
Miao Xiefa7c1492013-09-26 13:15:27 +08004286 ordered = btrfs_lookup_ordered_range(inode, hole_start,
4287 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004288 if (!ordered)
4289 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004290 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4291 &cached_state, GFP_NOFS);
Miao Xiefa7c1492013-09-26 13:15:27 +08004292 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004293 btrfs_put_ordered_extent(ordered);
4294 }
4295
Yan Zheng9036c102008-10-30 14:19:41 -04004296 cur_offset = hole_start;
4297 while (1) {
4298 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4299 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004300 if (IS_ERR(em)) {
4301 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004302 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004303 break;
4304 }
Yan Zheng9036c102008-10-30 14:19:41 -04004305 last_byte = min(extent_map_end(em), block_end);
Qu Wenruofda28322013-02-26 08:10:22 +00004306 last_byte = ALIGN(last_byte , root->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004307 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004308 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004309 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004310
Josef Bacik16e75492013-10-22 12:18:51 -04004311 err = maybe_insert_hole(root, inode, cur_offset,
4312 hole_size);
4313 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004314 break;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004315 btrfs_drop_extent_cache(inode, cur_offset,
4316 cur_offset + hole_size - 1, 0);
4317 hole_em = alloc_extent_map();
4318 if (!hole_em) {
4319 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4320 &BTRFS_I(inode)->runtime_flags);
4321 goto next;
4322 }
4323 hole_em->start = cur_offset;
4324 hole_em->len = hole_size;
4325 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004326
Josef Bacik5dc562c2012-08-17 13:14:17 -04004327 hole_em->block_start = EXTENT_MAP_HOLE;
4328 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004329 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004330 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004331 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
4332 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Josef Bacik16e75492013-10-22 12:18:51 -04004333 hole_em->generation = root->fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004334
4335 while (1) {
4336 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004337 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004338 write_unlock(&em_tree->lock);
4339 if (err != -EEXIST)
4340 break;
4341 btrfs_drop_extent_cache(inode, cur_offset,
4342 cur_offset +
4343 hole_size - 1, 0);
4344 }
4345 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04004346 }
Josef Bacik16e75492013-10-22 12:18:51 -04004347next:
Yan Zheng9036c102008-10-30 14:19:41 -04004348 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004349 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004350 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004351 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004352 break;
4353 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004354 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004355 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4356 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004357 return err;
4358}
4359
Eric Sandeen3972f262013-01-12 02:57:22 +00004360static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004361{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004362 struct btrfs_root *root = BTRFS_I(inode)->root;
4363 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004364 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004365 loff_t newsize = attr->ia_size;
4366 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004367 int ret;
4368
Eric Sandeen3972f262013-01-12 02:57:22 +00004369 /*
4370 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4371 * special case where we need to update the times despite not having
4372 * these flags set. For all other operations the VFS set these flags
4373 * explicitly if it wants a timestamp update.
4374 */
4375 if (newsize != oldsize && (!(mask & (ATTR_CTIME | ATTR_MTIME))))
4376 inode->i_ctime = inode->i_mtime = current_fs_time(inode->i_sb);
4377
Josef Bacika41ad392011-01-31 15:30:16 -05004378 if (newsize > oldsize) {
Kirill A. Shutemov7caef262013-09-12 15:13:56 -07004379 truncate_pagecache(inode, newsize);
Josef Bacika41ad392011-01-31 15:30:16 -05004380 ret = btrfs_cont_expand(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004381 if (ret)
Yan, Zheng80825102009-11-12 09:35:36 +00004382 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004383
Miao Xief4a2f4c2011-12-14 20:12:01 -05004384 trans = btrfs_start_transaction(root, 1);
4385 if (IS_ERR(trans))
4386 return PTR_ERR(trans);
4387
4388 i_size_write(inode, newsize);
4389 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
4390 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004391 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05004392 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004393
Josef Bacika41ad392011-01-31 15:30:16 -05004394 /*
4395 * We're truncating a file that used to have good data down to
4396 * zero. Make sure it gets into the ordered flush list so that
4397 * any new writes get down to disk quickly.
4398 */
4399 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004400 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4401 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004402
Josef Bacikf3fe8202013-01-07 17:03:21 -05004403 /*
4404 * 1 for the orphan item we're going to add
4405 * 1 for the orphan item deletion.
4406 */
4407 trans = btrfs_start_transaction(root, 2);
4408 if (IS_ERR(trans))
4409 return PTR_ERR(trans);
4410
4411 /*
4412 * We need to do this in case we fail at _any_ point during the
4413 * actual truncate. Once we do the truncate_setsize we could
4414 * invalidate pages which forces any outstanding ordered io to
4415 * be instantly completed which will give us extents that need
4416 * to be truncated. If we fail to get an orphan inode down we
4417 * could have left over extents that were never meant to live,
4418 * so we need to garuntee from this point on that everything
4419 * will be consistent.
4420 */
4421 ret = btrfs_orphan_add(trans, inode);
4422 btrfs_end_transaction(trans, root);
4423 if (ret)
4424 return ret;
4425
Josef Bacika41ad392011-01-31 15:30:16 -05004426 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4427 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004428
4429 /* Disable nonlocked read DIO to avoid the end less truncate */
4430 btrfs_inode_block_unlocked_dio(inode);
4431 inode_dio_wait(inode);
4432 btrfs_inode_resume_unlocked_dio(inode);
4433
Josef Bacika41ad392011-01-31 15:30:16 -05004434 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004435 if (ret && inode->i_nlink) {
4436 int err;
4437
4438 /*
4439 * failed to truncate, disk_i_size is only adjusted down
4440 * as we remove extents, so it should represent the true
4441 * size of the inode, so reset the in memory size and
4442 * delete our orphan entry.
4443 */
4444 trans = btrfs_join_transaction(root);
4445 if (IS_ERR(trans)) {
4446 btrfs_orphan_del(NULL, inode);
4447 return ret;
4448 }
4449 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
4450 err = btrfs_orphan_del(trans, inode);
4451 if (err)
4452 btrfs_abort_transaction(trans, root, err);
4453 btrfs_end_transaction(trans, root);
4454 }
Yan, Zheng80825102009-11-12 09:35:36 +00004455 }
4456
Josef Bacika41ad392011-01-31 15:30:16 -05004457 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004458}
4459
Chris Mason39279cc2007-06-12 06:35:45 -04004460static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
4461{
4462 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08004463 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004464 int err;
4465
Li Zefanb83cc962010-12-20 16:04:08 +08004466 if (btrfs_root_readonly(root))
4467 return -EROFS;
4468
Chris Mason39279cc2007-06-12 06:35:45 -04004469 err = inode_change_ok(inode, attr);
4470 if (err)
4471 return err;
4472
Chris Mason5a3f23d2009-03-31 13:27:11 -04004473 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00004474 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00004475 if (err)
4476 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004477 }
Yan Zheng9036c102008-10-30 14:19:41 -04004478
Christoph Hellwig10257742010-06-04 11:30:02 +02004479 if (attr->ia_valid) {
4480 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004481 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004482 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04004483
Josef Bacik22c44fe2011-11-30 10:45:38 -05004484 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig10257742010-06-04 11:30:02 +02004485 err = btrfs_acl_chmod(inode);
4486 }
4487
Chris Mason39279cc2007-06-12 06:35:45 -04004488 return err;
4489}
Chris Mason61295eb2008-01-14 16:24:38 -05004490
Al Virobd555972010-06-07 11:35:40 -04004491void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004492{
4493 struct btrfs_trans_handle *trans;
4494 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04004495 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik07127182011-08-19 10:29:59 -04004496 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04004497 int ret;
4498
liubo1abe9b82011-03-24 11:18:59 +00004499 trace_btrfs_inode_evict(inode);
4500
Chris Mason39279cc2007-06-12 06:35:45 -04004501 truncate_inode_pages(&inode->i_data, 0);
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02004502 if (inode->i_nlink &&
4503 ((btrfs_root_refs(&root->root_item) != 0 &&
4504 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
4505 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04004506 goto no_delete;
4507
Chris Mason39279cc2007-06-12 06:35:45 -04004508 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04004509 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004510 goto no_delete;
4511 }
Al Virobd555972010-06-07 11:35:40 -04004512 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04004513 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04004514
Yan, Zhengc71bf092009-11-12 09:34:40 +00004515 if (root->fs_info->log_root_recovering) {
Liu Bo6bf02312012-06-25 21:59:09 -06004516 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04004517 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00004518 goto no_delete;
4519 }
4520
Yan, Zheng76dda932009-09-21 16:00:26 -04004521 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02004522 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
4523 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04004524 goto no_delete;
4525 }
4526
Miao Xie0e8c36a2012-12-19 06:59:51 +00004527 ret = btrfs_commit_inode_delayed_inode(inode);
4528 if (ret) {
4529 btrfs_orphan_del(NULL, inode);
4530 goto no_delete;
4531 }
4532
Miao Xie66d8f3d2012-09-06 04:02:28 -06004533 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04004534 if (!rsv) {
4535 btrfs_orphan_del(NULL, inode);
4536 goto no_delete;
4537 }
Josef Bacik4a338542011-08-29 11:01:31 -04004538 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04004539 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04004540 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04004541
Chris Masondbe674a2008-07-17 12:54:05 -04004542 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04004543
Josef Bacik4289a662011-08-05 13:22:24 -04004544 /*
Miao Xie8407aa42012-09-07 01:43:32 -06004545 * This is a bit simpler than btrfs_truncate since we've already
4546 * reserved our space for our orphan item in the unlink, so we just
4547 * need to reserve some slack space in case we add bytes and update
4548 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04004549 */
Yan, Zheng80825102009-11-12 09:35:36 +00004550 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00004551 ret = btrfs_block_rsv_refill(root, rsv, min_size,
4552 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00004553
Josef Bacik726c35f2011-09-26 15:46:06 -04004554 /*
4555 * Try and steal from the global reserve since we will
4556 * likely not use this space anyway, we want to try as
4557 * hard as possible to get this to work.
4558 */
4559 if (ret)
4560 ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
4561
Yan, Zhengd68fc572010-05-16 10:49:58 -04004562 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004563 btrfs_warn(root->fs_info,
4564 "Could not get space for a delete, will truncate on mount %d",
4565 ret);
Josef Bacik4289a662011-08-05 13:22:24 -04004566 btrfs_orphan_del(NULL, inode);
4567 btrfs_free_block_rsv(root, rsv);
4568 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04004569 }
4570
Miao Xie0e8c36a2012-12-19 06:59:51 +00004571 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04004572 if (IS_ERR(trans)) {
4573 btrfs_orphan_del(NULL, inode);
4574 btrfs_free_block_rsv(root, rsv);
4575 goto no_delete;
4576 }
4577
4578 trans->block_rsv = rsv;
4579
Yan, Zhengd68fc572010-05-16 10:49:58 -04004580 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04004581 if (ret != -ENOSPC)
Yan, Zheng80825102009-11-12 09:35:36 +00004582 break;
4583
Miao Xie8407aa42012-09-07 01:43:32 -06004584 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004585 btrfs_end_transaction(trans, root);
4586 trans = NULL;
Liu Bob53d3f52012-11-14 14:34:34 +00004587 btrfs_btree_balance_dirty(root);
Josef Bacik7b128762008-07-24 12:17:14 -04004588 }
4589
Josef Bacik4289a662011-08-05 13:22:24 -04004590 btrfs_free_block_rsv(root, rsv);
4591
Josef Bacik4ef31a42013-08-13 14:10:08 -04004592 /*
4593 * Errors here aren't a big deal, it just means we leave orphan items
4594 * in the tree. They will be cleaned up on the next mount.
4595 */
Yan, Zheng80825102009-11-12 09:35:36 +00004596 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04004597 trans->block_rsv = root->orphan_block_rsv;
Josef Bacik4ef31a42013-08-13 14:10:08 -04004598 btrfs_orphan_del(trans, inode);
4599 } else {
4600 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004601 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004602
Josef Bacik4289a662011-08-05 13:22:24 -04004603 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08004604 if (!(root == root->fs_info->tree_root ||
4605 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08004606 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08004607
Chris Mason54aa1f42007-06-22 14:16:25 -04004608 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004609 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04004610no_delete:
Miao Xie89042e52013-05-15 07:48:15 +00004611 btrfs_remove_delayed_node(inode);
Jan Karadbd57682012-05-03 14:48:02 +02004612 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004613 return;
Chris Mason39279cc2007-06-12 06:35:45 -04004614}
4615
4616/*
4617 * this returns the key found in the dir entry in the location pointer.
4618 * If no dir entries were found, location->objectid is 0.
4619 */
4620static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
4621 struct btrfs_key *location)
4622{
4623 const char *name = dentry->d_name.name;
4624 int namelen = dentry->d_name.len;
4625 struct btrfs_dir_item *di;
4626 struct btrfs_path *path;
4627 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04004628 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004629
4630 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07004631 if (!path)
4632 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05004633
Li Zefan33345d012011-04-20 10:31:50 +08004634 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04004635 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04004636 if (IS_ERR(di))
4637 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05004638
David Sterbac7040052011-04-19 18:00:01 +02004639 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05004640 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05004641
Chris Mason5f39d392007-10-15 16:14:19 -04004642 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04004643out:
Chris Mason39279cc2007-06-12 06:35:45 -04004644 btrfs_free_path(path);
4645 return ret;
Chris Mason39544012007-12-12 14:38:19 -05004646out_err:
4647 location->objectid = 0;
4648 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04004649}
4650
4651/*
4652 * when we hit a tree root in a directory, the btrfs part of the inode
4653 * needs to be changed to reflect the root directory of the tree root. This
4654 * is kind of like crossing a mount point.
4655 */
4656static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004657 struct inode *dir,
4658 struct dentry *dentry,
4659 struct btrfs_key *location,
4660 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04004661{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004662 struct btrfs_path *path;
4663 struct btrfs_root *new_root;
4664 struct btrfs_root_ref *ref;
4665 struct extent_buffer *leaf;
4666 int ret;
4667 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004668
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004669 path = btrfs_alloc_path();
4670 if (!path) {
4671 err = -ENOMEM;
4672 goto out;
4673 }
Chris Mason39279cc2007-06-12 06:35:45 -04004674
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004675 err = -ENOENT;
4676 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
4677 BTRFS_I(dir)->root->root_key.objectid,
4678 location->objectid);
4679 if (ret) {
4680 if (ret < 0)
4681 err = ret;
4682 goto out;
4683 }
Chris Mason39279cc2007-06-12 06:35:45 -04004684
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004685 leaf = path->nodes[0];
4686 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08004687 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004688 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
4689 goto out;
4690
4691 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
4692 (unsigned long)(ref + 1),
4693 dentry->d_name.len);
4694 if (ret)
4695 goto out;
4696
David Sterbab3b4aa72011-04-21 01:20:15 +02004697 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004698
4699 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
4700 if (IS_ERR(new_root)) {
4701 err = PTR_ERR(new_root);
4702 goto out;
4703 }
4704
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004705 *sub_root = new_root;
4706 location->objectid = btrfs_root_dirid(&new_root->root_item);
4707 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04004708 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004709 err = 0;
4710out:
4711 btrfs_free_path(path);
4712 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004713}
4714
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004715static void inode_tree_add(struct inode *inode)
4716{
4717 struct btrfs_root *root = BTRFS_I(inode)->root;
4718 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004719 struct rb_node **p;
4720 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01004721 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Li Zefan33345d012011-04-20 10:31:50 +08004722 u64 ino = btrfs_ino(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004723
Al Viro1d3382cb2010-10-23 15:19:20 -04004724 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004725 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00004726 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004727 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00004728 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004729 while (*p) {
4730 parent = *p;
4731 entry = rb_entry(parent, struct btrfs_inode, rb_node);
4732
Li Zefan33345d012011-04-20 10:31:50 +08004733 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004734 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004735 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004736 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004737 else {
4738 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04004739 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01004740 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004741 RB_CLEAR_NODE(parent);
4742 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01004743 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004744 }
4745 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01004746 rb_link_node(new, parent, p);
4747 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004748 spin_unlock(&root->inode_lock);
4749}
4750
4751static void inode_tree_del(struct inode *inode)
4752{
4753 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04004754 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004755
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004756 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004757 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004758 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004759 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04004760 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004761 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004762 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04004763
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02004764 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004765 synchronize_srcu(&root->fs_info->subvol_srcu);
4766 spin_lock(&root->inode_lock);
4767 empty = RB_EMPTY_ROOT(&root->inode_tree);
4768 spin_unlock(&root->inode_lock);
4769 if (empty)
4770 btrfs_add_dead_root(root);
4771 }
4772}
4773
Jeff Mahoney143bede2012-03-01 14:56:26 +01004774void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04004775{
4776 struct rb_node *node;
4777 struct rb_node *prev;
4778 struct btrfs_inode *entry;
4779 struct inode *inode;
4780 u64 objectid = 0;
4781
4782 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4783
4784 spin_lock(&root->inode_lock);
4785again:
4786 node = root->inode_tree.rb_node;
4787 prev = NULL;
4788 while (node) {
4789 prev = node;
4790 entry = rb_entry(node, struct btrfs_inode, rb_node);
4791
Li Zefan33345d012011-04-20 10:31:50 +08004792 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004793 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004794 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004795 node = node->rb_right;
4796 else
4797 break;
4798 }
4799 if (!node) {
4800 while (prev) {
4801 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004802 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004803 node = prev;
4804 break;
4805 }
4806 prev = rb_next(prev);
4807 }
4808 }
4809 while (node) {
4810 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004811 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04004812 inode = igrab(&entry->vfs_inode);
4813 if (inode) {
4814 spin_unlock(&root->inode_lock);
4815 if (atomic_read(&inode->i_count) > 1)
4816 d_prune_aliases(inode);
4817 /*
Al Viro45321ac2010-06-07 13:43:19 -04004818 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04004819 * the inode cache when its usage count
4820 * hits zero.
4821 */
4822 iput(inode);
4823 cond_resched();
4824 spin_lock(&root->inode_lock);
4825 goto again;
4826 }
4827
4828 if (cond_resched_lock(&root->inode_lock))
4829 goto again;
4830
4831 node = rb_next(node);
4832 }
4833 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004834}
4835
Chris Masone02119d2008-09-05 16:13:11 -04004836static int btrfs_init_locked_inode(struct inode *inode, void *p)
4837{
4838 struct btrfs_iget_args *args = p;
4839 inode->i_ino = args->ino;
Chris Masone02119d2008-09-05 16:13:11 -04004840 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004841 return 0;
4842}
4843
4844static int btrfs_find_actor(struct inode *inode, void *opaque)
4845{
4846 struct btrfs_iget_args *args = opaque;
Li Zefan33345d012011-04-20 10:31:50 +08004847 return args->ino == btrfs_ino(inode) &&
Chris Masond3977122009-01-05 21:25:51 -05004848 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004849}
4850
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004851static struct inode *btrfs_iget_locked(struct super_block *s,
4852 u64 objectid,
4853 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04004854{
4855 struct inode *inode;
4856 struct btrfs_iget_args args;
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01004857 unsigned long hashval = btrfs_inode_hash(objectid, root);
4858
Chris Mason39279cc2007-06-12 06:35:45 -04004859 args.ino = objectid;
4860 args.root = root;
4861
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01004862 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04004863 btrfs_init_locked_inode,
4864 (void *)&args);
4865 return inode;
4866}
4867
Balaji Rao1a54ef82008-07-21 02:01:04 +05304868/* Get an inode object given its location and corresponding root.
4869 * Returns in *is_new if the inode was read from disk
4870 */
4871struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00004872 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05304873{
4874 struct inode *inode;
4875
4876 inode = btrfs_iget_locked(s, location->objectid, root);
4877 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004878 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05304879
4880 if (inode->i_state & I_NEW) {
4881 BTRFS_I(inode)->root = root;
4882 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
4883 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07004884 if (!is_bad_inode(inode)) {
4885 inode_tree_add(inode);
4886 unlock_new_inode(inode);
4887 if (new)
4888 *new = 1;
4889 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04004890 unlock_new_inode(inode);
4891 iput(inode);
4892 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07004893 }
4894 }
4895
Balaji Rao1a54ef82008-07-21 02:01:04 +05304896 return inode;
4897}
4898
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004899static struct inode *new_simple_dir(struct super_block *s,
4900 struct btrfs_key *key,
4901 struct btrfs_root *root)
4902{
4903 struct inode *inode = new_inode(s);
4904
4905 if (!inode)
4906 return ERR_PTR(-ENOMEM);
4907
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004908 BTRFS_I(inode)->root = root;
4909 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04004910 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004911
4912 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08004913 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004914 inode->i_fop = &simple_dir_operations;
4915 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
4916 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
4917
4918 return inode;
4919}
4920
Chris Mason3de45862008-11-17 21:02:50 -05004921struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04004922{
Chris Masond3977122009-01-05 21:25:51 -05004923 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004924 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004925 struct btrfs_root *sub_root = root;
4926 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04004927 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004928 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004929
4930 if (dentry->d_name.len > BTRFS_NAME_LEN)
4931 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04004932
Jeff Layton39e3c952012-11-28 11:30:53 -05004933 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04004934 if (ret < 0)
4935 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04004936
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004937 if (location.objectid == 0)
4938 return NULL;
4939
4940 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00004941 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004942 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004943 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004944
4945 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
4946
Yan, Zheng76dda932009-09-21 16:00:26 -04004947 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004948 ret = fixup_tree_root_location(root, dir, dentry,
4949 &location, &sub_root);
4950 if (ret < 0) {
4951 if (ret != -ENOENT)
4952 inode = ERR_PTR(ret);
4953 else
4954 inode = new_simple_dir(dir->i_sb, &location, sub_root);
4955 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00004956 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004957 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004958 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
4959
Julia Lawall34d19ba2011-01-24 19:55:19 +00004960 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00004961 down_read(&root->fs_info->cleanup_work_sem);
4962 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004963 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00004964 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04004965 if (ret) {
4966 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004967 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04004968 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00004969 }
4970
Chris Mason3de45862008-11-17 21:02:50 -05004971 return inode;
4972}
4973
Nick Pigginfe15ce42011-01-07 17:49:23 +11004974static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04004975{
4976 struct btrfs_root *root;
Li Zefan848cce02012-02-21 17:04:28 +08004977 struct inode *inode = dentry->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04004978
Li Zefan848cce02012-02-21 17:04:28 +08004979 if (!inode && !IS_ROOT(dentry))
4980 inode = dentry->d_parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04004981
Li Zefan848cce02012-02-21 17:04:28 +08004982 if (inode) {
4983 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04004984 if (btrfs_root_refs(&root->root_item) == 0)
4985 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08004986
4987 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
4988 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04004989 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004990 return 0;
4991}
4992
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004993static void btrfs_dentry_release(struct dentry *dentry)
4994{
4995 if (dentry->d_fsdata)
4996 kfree(dentry->d_fsdata);
4997}
4998
Chris Mason3de45862008-11-17 21:02:50 -05004999static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005000 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005001{
Josef Bacika66e7cc2011-09-18 10:34:03 -04005002 struct dentry *ret;
5003
5004 ret = d_splice_alias(btrfs_lookup_dentry(dir, dentry), dentry);
Josef Bacika66e7cc2011-09-18 10:34:03 -04005005 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005006}
5007
Miao Xie16cdcec2011-04-22 18:12:22 +08005008unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005009 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5010};
5011
Al Viro9cdda8d2013-05-22 16:48:09 -04005012static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005013{
Al Viro9cdda8d2013-05-22 16:48:09 -04005014 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005015 struct btrfs_root *root = BTRFS_I(inode)->root;
5016 struct btrfs_item *item;
5017 struct btrfs_dir_item *di;
5018 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005019 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005020 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005021 struct list_head ins_list;
5022 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005023 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005024 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005025 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005026 unsigned char d_type;
5027 int over = 0;
5028 u32 di_cur;
5029 u32 di_total;
5030 u32 di_len;
5031 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005032 char tmp_name[32];
5033 char *name_ptr;
5034 int name_len;
Al Viro9cdda8d2013-05-22 16:48:09 -04005035 int is_curr = 0; /* ctx->pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04005036
5037 /* FIXME, use a real flag for deciding about the key type */
5038 if (root->fs_info->tree_root == root)
5039 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005040
Al Viro9cdda8d2013-05-22 16:48:09 -04005041 if (!dir_emit_dots(file, ctx))
5042 return 0;
5043
David Woodhouse49593bf2008-08-17 17:08:36 +01005044 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005045 if (!path)
5046 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005047
Josef Bacik026fd312011-05-13 10:32:11 -04005048 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005049
Miao Xie16cdcec2011-04-22 18:12:22 +08005050 if (key_type == BTRFS_DIR_INDEX_KEY) {
5051 INIT_LIST_HEAD(&ins_list);
5052 INIT_LIST_HEAD(&del_list);
5053 btrfs_get_delayed_items(inode, &ins_list, &del_list);
5054 }
5055
Chris Mason39279cc2007-06-12 06:35:45 -04005056 btrfs_set_key_type(&key, key_type);
Al Viro9cdda8d2013-05-22 16:48:09 -04005057 key.offset = ctx->pos;
Li Zefan33345d012011-04-20 10:31:50 +08005058 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005059
Chris Mason39279cc2007-06-12 06:35:45 -04005060 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5061 if (ret < 0)
5062 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005063
5064 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005065 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005066 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005067 if (slot >= btrfs_header_nritems(leaf)) {
5068 ret = btrfs_next_leaf(root, path);
5069 if (ret < 0)
5070 goto err;
5071 else if (ret > 0)
5072 break;
5073 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005074 }
Chris Mason3de45862008-11-17 21:02:50 -05005075
Ross Kirkdd3cc162013-09-16 15:58:09 +01005076 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04005077 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5078
5079 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005080 break;
Chris Mason5f39d392007-10-15 16:14:19 -04005081 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04005082 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005083 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005084 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08005085 if (key_type == BTRFS_DIR_INDEX_KEY &&
5086 btrfs_should_delete_dir_index(&del_list,
5087 found_key.offset))
5088 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005089
Al Viro9cdda8d2013-05-22 16:48:09 -04005090 ctx->pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08005091 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005092
Chris Mason39279cc2007-06-12 06:35:45 -04005093 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
5094 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005095 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01005096
5097 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04005098 struct btrfs_key location;
5099
Josef Bacik22a94d42011-03-16 16:47:17 -04005100 if (verify_dir_item(root, leaf, di))
5101 break;
5102
Chris Mason5f39d392007-10-15 16:14:19 -04005103 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01005104 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04005105 name_ptr = tmp_name;
5106 } else {
5107 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01005108 if (!name_ptr) {
5109 ret = -ENOMEM;
5110 goto err;
5111 }
Chris Mason5f39d392007-10-15 16:14:19 -04005112 }
5113 read_extent_buffer(leaf, name_ptr,
5114 (unsigned long)(di + 1), name_len);
5115
5116 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5117 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05005118
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005119
Chris Mason3de45862008-11-17 21:02:50 -05005120 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01005121 * skip it.
5122 *
5123 * In contrast to old kernels, we insert the snapshot's
5124 * dir item and dir index after it has been created, so
5125 * we won't find a reference to our own snapshot. We
5126 * still keep the following code for backward
5127 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05005128 */
5129 if (location.type == BTRFS_ROOT_ITEM_KEY &&
5130 location.objectid == root->root_key.objectid) {
5131 over = 0;
5132 goto skip;
5133 }
Al Viro9cdda8d2013-05-22 16:48:09 -04005134 over = !dir_emit(ctx, name_ptr, name_len,
5135 location.objectid, d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04005136
Chris Mason3de45862008-11-17 21:02:50 -05005137skip:
Chris Mason5f39d392007-10-15 16:14:19 -04005138 if (name_ptr != tmp_name)
5139 kfree(name_ptr);
5140
Chris Mason39279cc2007-06-12 06:35:45 -04005141 if (over)
5142 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05005143 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01005144 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04005145 di_cur += di_len;
5146 di = (struct btrfs_dir_item *)((char *)di + di_len);
5147 }
Li Zefanb9e03af2011-03-23 10:43:58 +08005148next:
5149 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005150 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005151
Miao Xie16cdcec2011-04-22 18:12:22 +08005152 if (key_type == BTRFS_DIR_INDEX_KEY) {
5153 if (is_curr)
Al Viro9cdda8d2013-05-22 16:48:09 -04005154 ctx->pos++;
5155 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005156 if (ret)
5157 goto nopos;
5158 }
5159
David Woodhouse49593bf2008-08-17 17:08:36 +01005160 /* Reached end of directory/root. Bump pos past the last item. */
Zach Browndb62efb2013-07-11 16:19:42 -07005161 ctx->pos++;
5162
5163 /*
5164 * Stop new entries from being returned after we return the last
5165 * entry.
5166 *
5167 * New directory entries are assigned a strictly increasing
5168 * offset. This means that new entries created during readdir
5169 * are *guaranteed* to be seen in the future by that readdir.
5170 * This has broken buggy programs which operate on names as
5171 * they're returned by readdir. Until we re-use freed offsets
5172 * we have this hack to stop new entries from being returned
5173 * under the assumption that they'll never reach this huge
5174 * offset.
5175 *
5176 * This is being careful not to overflow 32bit loff_t unless the
5177 * last entry requires it because doing so has broken 32bit apps
5178 * in the past.
5179 */
5180 if (key_type == BTRFS_DIR_INDEX_KEY) {
5181 if (ctx->pos >= INT_MAX)
5182 ctx->pos = LLONG_MAX;
5183 else
5184 ctx->pos = INT_MAX;
5185 }
Chris Mason39279cc2007-06-12 06:35:45 -04005186nopos:
5187 ret = 0;
5188err:
Miao Xie16cdcec2011-04-22 18:12:22 +08005189 if (key_type == BTRFS_DIR_INDEX_KEY)
5190 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005191 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005192 return ret;
5193}
5194
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005195int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005196{
5197 struct btrfs_root *root = BTRFS_I(inode)->root;
5198 struct btrfs_trans_handle *trans;
5199 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005200 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005201
Josef Bacik72ac3c02012-05-23 14:13:11 -04005202 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005203 return 0;
5204
Liu Bo83eea1f2012-07-10 05:28:39 -06005205 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08005206 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005207
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005208 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005209 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005210 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005211 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005212 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005213 if (IS_ERR(trans))
5214 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06005215 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005216 }
5217 return ret;
5218}
5219
5220/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005221 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005222 * inode changes. But, it is most likely to find the inode in cache.
5223 * FIXME, needs more benchmarking...there are no reasons other than performance
5224 * to keep or drop this code.
5225 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005226static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005227{
5228 struct btrfs_root *root = BTRFS_I(inode)->root;
5229 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005230 int ret;
5231
Josef Bacik72ac3c02012-05-23 14:13:11 -04005232 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005233 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005234
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005235 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005236 if (IS_ERR(trans))
5237 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005238
5239 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005240 if (ret && ret == -ENOSPC) {
5241 /* whoops, lets try again with the full transaction */
5242 btrfs_end_transaction(trans, root);
5243 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005244 if (IS_ERR(trans))
5245 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005246
Chris Mason94b60442010-05-26 11:02:00 -04005247 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005248 }
Chris Mason39279cc2007-06-12 06:35:45 -04005249 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08005250 if (BTRFS_I(inode)->delayed_node)
5251 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005252
5253 return ret;
5254}
5255
5256/*
5257 * This is a copy of file_update_time. We need this so we can return error on
5258 * ENOSPC for updating the inode in the case of file write and mmap writes.
5259 */
Josef Bacike41f9412012-03-26 09:46:47 -04005260static int btrfs_update_time(struct inode *inode, struct timespec *now,
5261 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005262{
Alexander Block2bc5565282012-06-15 09:49:33 +02005263 struct btrfs_root *root = BTRFS_I(inode)->root;
5264
5265 if (btrfs_root_readonly(root))
5266 return -EROFS;
5267
Josef Bacike41f9412012-03-26 09:46:47 -04005268 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005269 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04005270 if (flags & S_CTIME)
5271 inode->i_ctime = *now;
5272 if (flags & S_MTIME)
5273 inode->i_mtime = *now;
5274 if (flags & S_ATIME)
5275 inode->i_atime = *now;
5276 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005277}
5278
Chris Masond352ac62008-09-29 15:18:18 -04005279/*
5280 * find the highest existing sequence number in a directory
5281 * and then set the in-memory index_cnt variable to reflect
5282 * free sequence numbers
5283 */
Josef Bacikaec74772008-07-24 12:12:38 -04005284static int btrfs_set_inode_index_count(struct inode *inode)
5285{
5286 struct btrfs_root *root = BTRFS_I(inode)->root;
5287 struct btrfs_key key, found_key;
5288 struct btrfs_path *path;
5289 struct extent_buffer *leaf;
5290 int ret;
5291
Li Zefan33345d012011-04-20 10:31:50 +08005292 key.objectid = btrfs_ino(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005293 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
5294 key.offset = (u64)-1;
5295
5296 path = btrfs_alloc_path();
5297 if (!path)
5298 return -ENOMEM;
5299
5300 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5301 if (ret < 0)
5302 goto out;
5303 /* FIXME: we should be able to handle this */
5304 if (ret == 0)
5305 goto out;
5306 ret = 0;
5307
5308 /*
5309 * MAGIC NUMBER EXPLANATION:
5310 * since we search a directory based on f_pos we have to start at 2
5311 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5312 * else has to start at 2
5313 */
5314 if (path->slots[0] == 0) {
5315 BTRFS_I(inode)->index_cnt = 2;
5316 goto out;
5317 }
5318
5319 path->slots[0]--;
5320
5321 leaf = path->nodes[0];
5322 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5323
Li Zefan33345d012011-04-20 10:31:50 +08005324 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacikaec74772008-07-24 12:12:38 -04005325 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
5326 BTRFS_I(inode)->index_cnt = 2;
5327 goto out;
5328 }
5329
5330 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
5331out:
5332 btrfs_free_path(path);
5333 return ret;
5334}
5335
Chris Masond352ac62008-09-29 15:18:18 -04005336/*
5337 * helper to find a free sequence number in a given directory. This current
5338 * code is very simple, later versions will do smarter things in the btree
5339 */
Chris Mason3de45862008-11-17 21:02:50 -05005340int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04005341{
5342 int ret = 0;
5343
5344 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08005345 ret = btrfs_inode_delayed_dir_index_count(dir);
5346 if (ret) {
5347 ret = btrfs_set_inode_index_count(dir);
5348 if (ret)
5349 return ret;
5350 }
Josef Bacikaec74772008-07-24 12:12:38 -04005351 }
5352
Chris Mason00e4e6b2008-08-05 11:18:09 -04005353 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04005354 BTRFS_I(dir)->index_cnt++;
5355
5356 return ret;
5357}
5358
Chris Mason39279cc2007-06-12 06:35:45 -04005359static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5360 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04005361 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05005362 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04005363 u64 ref_objectid, u64 objectid,
5364 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04005365{
5366 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005367 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04005368 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04005369 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05005370 struct btrfs_inode_ref *ref;
5371 struct btrfs_key key[2];
5372 u32 sizes[2];
5373 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04005374 int ret;
5375 int owner;
5376
Chris Mason5f39d392007-10-15 16:14:19 -04005377 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005378 if (!path)
5379 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04005380
Chris Mason39279cc2007-06-12 06:35:45 -04005381 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005382 if (!inode) {
5383 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005384 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005385 }
Chris Mason39279cc2007-06-12 06:35:45 -04005386
Li Zefan581bb052011-04-20 10:06:11 +08005387 /*
5388 * we have to initialize this early, so we can reclaim the inode
5389 * number if we fail afterwards in this function.
5390 */
5391 inode->i_ino = objectid;
5392
Josef Bacikaec74772008-07-24 12:12:38 -04005393 if (dir) {
liubo1abe9b82011-03-24 11:18:59 +00005394 trace_btrfs_inode_request(dir);
5395
Chris Mason3de45862008-11-17 21:02:50 -05005396 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04005397 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005398 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005399 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005400 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04005401 }
Josef Bacikaec74772008-07-24 12:12:38 -04005402 }
5403 /*
5404 * index_cnt is ignored for everything but a dir,
5405 * btrfs_get_inode_index_count has an explanation for the magic
5406 * number
5407 */
5408 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04005409 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04005410 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00005411 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db2b2007-08-27 16:49:44 -04005412
Josef Bacik5dc562c2012-08-17 13:14:17 -04005413 /*
5414 * We could have gotten an inode number from somebody who was fsynced
5415 * and then removed in this same transaction, so let's just set full
5416 * sync since it will be a full sync anyway and this will blow away the
5417 * old info in the log.
5418 */
5419 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
5420
Al Viro569254b2011-07-24 17:08:40 -04005421 if (S_ISDIR(mode))
Chris Mason39279cc2007-06-12 06:35:45 -04005422 owner = 0;
5423 else
5424 owner = 1;
Chris Mason9c583092008-01-29 15:15:18 -05005425
5426 key[0].objectid = objectid;
5427 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
5428 key[0].offset = 0;
5429
Mark Fashehf1863732012-08-08 11:32:27 -07005430 /*
5431 * Start new inodes with an inode_ref. This is slightly more
5432 * efficient for small numbers of hard links since they will
5433 * be packed into one item. Extended refs will kick in if we
5434 * add more hard links than can fit in the ref item.
5435 */
Chris Mason9c583092008-01-29 15:15:18 -05005436 key[1].objectid = objectid;
5437 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
5438 key[1].offset = ref_objectid;
5439
5440 sizes[0] = sizeof(struct btrfs_inode_item);
5441 sizes[1] = name_len + sizeof(*ref);
5442
Chris Masonb9473432009-03-13 11:00:37 -04005443 path->leave_spinning = 1;
Chris Mason9c583092008-01-29 15:15:18 -05005444 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
5445 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04005446 goto fail;
5447
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03005448 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04005449 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005450 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04005451 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
5452 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06005453 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
5454 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04005455 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05005456
5457 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
5458 struct btrfs_inode_ref);
5459 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04005460 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05005461 ptr = (unsigned long)(ref + 1);
5462 write_extent_buffer(path->nodes[0], name, ptr, name_len);
5463
Chris Mason5f39d392007-10-15 16:14:19 -04005464 btrfs_mark_buffer_dirty(path->nodes[0]);
5465 btrfs_free_path(path);
5466
Chris Mason39279cc2007-06-12 06:35:45 -04005467 location = &BTRFS_I(inode)->location;
5468 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04005469 location->offset = 0;
5470 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
5471
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005472 btrfs_inherit_iflags(inode, dir);
5473
Al Viro569254b2011-07-24 17:08:40 -04005474 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04005475 if (btrfs_test_opt(root, NODATASUM))
5476 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo213490b2012-09-11 08:33:50 -06005477 if (btrfs_test_opt(root, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05005478 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
5479 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04005480 }
5481
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005482 btrfs_insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005483 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00005484
5485 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04005486 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00005487
Alexander Block8ea05e32012-07-25 17:35:53 +02005488 btrfs_update_root_times(trans, root);
5489
Chris Mason39279cc2007-06-12 06:35:45 -04005490 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005491fail:
Josef Bacikaec74772008-07-24 12:12:38 -04005492 if (dir)
5493 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04005494 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005495 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005496 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005497}
5498
5499static inline u8 btrfs_inode_type(struct inode *inode)
5500{
5501 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
5502}
5503
Chris Masond352ac62008-09-29 15:18:18 -04005504/*
5505 * utility function to add 'inode' into 'parent_inode' with
5506 * a give name and a given sequence number.
5507 * if 'add_backref' is true, also insert a backref from the
5508 * inode to the parent directory.
5509 */
Chris Masone02119d2008-09-05 16:13:11 -04005510int btrfs_add_link(struct btrfs_trans_handle *trans,
5511 struct inode *parent_inode, struct inode *inode,
5512 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005513{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005514 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005515 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04005516 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08005517 u64 ino = btrfs_ino(inode);
5518 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005519
Li Zefan33345d012011-04-20 10:31:50 +08005520 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005521 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
5522 } else {
Li Zefan33345d012011-04-20 10:31:50 +08005523 key.objectid = ino;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005524 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
5525 key.offset = 0;
5526 }
Chris Mason39279cc2007-06-12 06:35:45 -04005527
Li Zefan33345d012011-04-20 10:31:50 +08005528 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005529 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
5530 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08005531 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005532 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08005533 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
5534 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005535 }
5536
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005537 /* Nothing to clean up yet */
5538 if (ret)
5539 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005540
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005541 ret = btrfs_insert_dir_item(trans, root, name, name_len,
5542 parent_inode, &key,
5543 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05005544 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005545 goto fail_dir_item;
5546 else if (ret) {
5547 btrfs_abort_transaction(trans, root, ret);
5548 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005549 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005550
5551 btrfs_i_size_write(parent_inode, parent_inode->i_size +
5552 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005553 inode_inc_iversion(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005554 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
5555 ret = btrfs_update_inode(trans, root, parent_inode);
5556 if (ret)
5557 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005558 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05005559
5560fail_dir_item:
5561 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
5562 u64 local_index;
5563 int err;
5564 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
5565 key.objectid, root->root_key.objectid,
5566 parent_ino, &local_index, name, name_len);
5567
5568 } else if (add_backref) {
5569 u64 local_index;
5570 int err;
5571
5572 err = btrfs_del_inode_ref(trans, root, name, name_len,
5573 ino, parent_ino, &local_index);
5574 }
5575 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005576}
5577
5578static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00005579 struct inode *dir, struct dentry *dentry,
5580 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005581{
Josef Bacika1b075d2010-11-19 20:36:11 +00005582 int err = btrfs_add_link(trans, dir, inode,
5583 dentry->d_name.name, dentry->d_name.len,
5584 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005585 if (err > 0)
5586 err = -EEXIST;
5587 return err;
5588}
5589
Josef Bacik618e21d2007-07-11 10:18:17 -04005590static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04005591 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04005592{
5593 struct btrfs_trans_handle *trans;
5594 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005595 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04005596 int err;
5597 int drop_inode = 0;
5598 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005599 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04005600
5601 if (!new_valid_dev(rdev))
5602 return -EINVAL;
5603
Josef Bacik9ed74f22009-09-11 16:12:44 -04005604 /*
5605 * 2 for inode item and ref
5606 * 2 for dir items
5607 * 1 for xattr if selinux is on
5608 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005609 trans = btrfs_start_transaction(root, 5);
5610 if (IS_ERR(trans))
5611 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05005612
Li Zefan581bb052011-04-20 10:06:11 +08005613 err = btrfs_find_free_ino(root, &objectid);
5614 if (err)
5615 goto out_unlock;
5616
Josef Bacikaec74772008-07-24 12:12:38 -04005617 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005618 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04005619 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005620 if (IS_ERR(inode)) {
5621 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005622 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005623 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005624
Eric Paris2a7dba32011-02-01 11:05:39 -05005625 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005626 if (err) {
5627 drop_inode = 1;
5628 goto out_unlock;
5629 }
5630
Casey Schauflerad19db72011-12-15 10:09:07 -05005631 /*
5632 * If the active LSM wants to access the inode during
5633 * d_instantiate it needs these. Smack checks to see
5634 * if the filesystem supports xattrs by looking at the
5635 * ops vector.
5636 */
5637
5638 inode->i_op = &btrfs_special_inode_operations;
Josef Bacika1b075d2010-11-19 20:36:11 +00005639 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04005640 if (err)
5641 drop_inode = 1;
5642 else {
Josef Bacik618e21d2007-07-11 10:18:17 -04005643 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04005644 btrfs_update_inode(trans, root, inode);
Al Viro08c422c2011-12-23 07:58:13 -05005645 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005646 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005647out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005648 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00005649 btrfs_btree_balance_dirty(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04005650 if (drop_inode) {
5651 inode_dec_link_count(inode);
5652 iput(inode);
5653 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005654 return err;
5655}
5656
Chris Mason39279cc2007-06-12 06:35:45 -04005657static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04005658 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04005659{
5660 struct btrfs_trans_handle *trans;
5661 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005662 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005663 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04005664 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04005665 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005666 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005667
Josef Bacik9ed74f22009-09-11 16:12:44 -04005668 /*
5669 * 2 for inode item and ref
5670 * 2 for dir items
5671 * 1 for xattr if selinux is on
5672 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005673 trans = btrfs_start_transaction(root, 5);
5674 if (IS_ERR(trans))
5675 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005676
Li Zefan581bb052011-04-20 10:06:11 +08005677 err = btrfs_find_free_ino(root, &objectid);
5678 if (err)
5679 goto out_unlock;
5680
Josef Bacikaec74772008-07-24 12:12:38 -04005681 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005682 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04005683 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005684 if (IS_ERR(inode)) {
5685 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005686 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005687 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005688 drop_inode_on_err = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04005689
Eric Paris2a7dba32011-02-01 11:05:39 -05005690 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005691 if (err)
Josef Bacik33268ea2008-07-24 12:16:36 -04005692 goto out_unlock;
Josef Bacik33268ea2008-07-24 12:16:36 -04005693
Filipe Brandenburger9185aa52012-11-30 03:40:08 +00005694 err = btrfs_update_inode(trans, root, inode);
5695 if (err)
5696 goto out_unlock;
5697
Casey Schauflerad19db72011-12-15 10:09:07 -05005698 /*
5699 * If the active LSM wants to access the inode during
5700 * d_instantiate it needs these. Smack checks to see
5701 * if the filesystem supports xattrs by looking at the
5702 * ops vector.
5703 */
5704 inode->i_fop = &btrfs_file_operations;
5705 inode->i_op = &btrfs_file_inode_operations;
5706
Josef Bacika1b075d2010-11-19 20:36:11 +00005707 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005708 if (err)
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005709 goto out_unlock;
5710
5711 inode->i_mapping->a_ops = &btrfs_aops;
5712 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
5713 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
5714 d_instantiate(dentry, inode);
5715
Chris Mason39279cc2007-06-12 06:35:45 -04005716out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005717 btrfs_end_transaction(trans, root);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005718 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005719 inode_dec_link_count(inode);
5720 iput(inode);
5721 }
Liu Bob53d3f52012-11-14 14:34:34 +00005722 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005723 return err;
5724}
5725
5726static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
5727 struct dentry *dentry)
5728{
5729 struct btrfs_trans_handle *trans;
5730 struct btrfs_root *root = BTRFS_I(dir)->root;
5731 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005732 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04005733 int err;
5734 int drop_inode = 0;
5735
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005736 /* do not allow sys_link's with other subvols of the same device */
5737 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00005738 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005739
Mark Fashehf1863732012-08-08 11:32:27 -07005740 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00005741 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04005742
Chris Mason3de45862008-11-17 21:02:50 -05005743 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04005744 if (err)
5745 goto fail;
5746
Yan, Zhenga22285a2010-05-16 10:48:46 -04005747 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00005748 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04005749 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00005750 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04005751 */
Miao Xie7e6b6462011-02-18 09:21:17 +00005752 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005753 if (IS_ERR(trans)) {
5754 err = PTR_ERR(trans);
5755 goto fail;
5756 }
Chris Mason5f39d392007-10-15 16:14:19 -04005757
Zach Brown8b558c52013-10-16 12:10:34 -07005758 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005759 inode_inc_iversion(inode);
Miao Xie31534952011-04-13 13:19:21 +08005760 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6ee2010-10-23 11:11:40 -04005761 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04005762 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04005763
Josef Bacika1b075d2010-11-19 20:36:11 +00005764 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04005765
Yan, Zhenga5719522009-09-24 09:17:31 -04005766 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005767 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04005768 } else {
Al Viro10d9f302011-07-16 23:09:10 -04005769 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04005770 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005771 if (err)
5772 goto fail;
Al Viro08c422c2011-12-23 07:58:13 -05005773 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00005774 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04005775 }
Chris Mason39279cc2007-06-12 06:35:45 -04005776
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005777 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05005778fail:
Chris Mason39279cc2007-06-12 06:35:45 -04005779 if (drop_inode) {
5780 inode_dec_link_count(inode);
5781 iput(inode);
5782 }
Liu Bob53d3f52012-11-14 14:34:34 +00005783 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005784 return err;
5785}
5786
Al Viro18bb1db2011-07-26 01:41:39 -04005787static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04005788{
Chris Masonb9d86662008-05-02 16:13:49 -04005789 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04005790 struct btrfs_trans_handle *trans;
5791 struct btrfs_root *root = BTRFS_I(dir)->root;
5792 int err = 0;
5793 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04005794 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005795 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005796
Josef Bacik9ed74f22009-09-11 16:12:44 -04005797 /*
5798 * 2 items for inode and ref
5799 * 2 items for dir items
5800 * 1 for xattr if selinux is on
5801 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005802 trans = btrfs_start_transaction(root, 5);
5803 if (IS_ERR(trans))
5804 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04005805
Li Zefan581bb052011-04-20 10:06:11 +08005806 err = btrfs_find_free_ino(root, &objectid);
5807 if (err)
5808 goto out_fail;
5809
Josef Bacikaec74772008-07-24 12:12:38 -04005810 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005811 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04005812 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04005813 if (IS_ERR(inode)) {
5814 err = PTR_ERR(inode);
5815 goto out_fail;
5816 }
Chris Mason5f39d392007-10-15 16:14:19 -04005817
Chris Mason39279cc2007-06-12 06:35:45 -04005818 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04005819
Eric Paris2a7dba32011-02-01 11:05:39 -05005820 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005821 if (err)
5822 goto out_fail;
5823
Chris Mason39279cc2007-06-12 06:35:45 -04005824 inode->i_op = &btrfs_dir_inode_operations;
5825 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04005826
Chris Masondbe674a2008-07-17 12:54:05 -04005827 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005828 err = btrfs_update_inode(trans, root, inode);
5829 if (err)
5830 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005831
Josef Bacika1b075d2010-11-19 20:36:11 +00005832 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
5833 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005834 if (err)
5835 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005836
Chris Mason39279cc2007-06-12 06:35:45 -04005837 d_instantiate(dentry, inode);
5838 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005839
5840out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005841 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005842 if (drop_on_err)
5843 iput(inode);
Liu Bob53d3f52012-11-14 14:34:34 +00005844 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005845 return err;
5846}
5847
Chris Masond352ac62008-09-29 15:18:18 -04005848/* helper for btfs_get_extent. Given an existing extent in the tree,
5849 * and an extent that you want to insert, deal with overlap and insert
5850 * the new extent into the tree.
5851 */
Chris Mason3b951512008-04-17 11:29:12 -04005852static int merge_extent_mapping(struct extent_map_tree *em_tree,
5853 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005854 struct extent_map *em,
5855 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04005856{
5857 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04005858
Chris Masone6dcd2d2008-07-17 12:53:50 -04005859 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
5860 start_diff = map_start - em->start;
5861 em->start = map_start;
5862 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04005863 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
5864 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04005865 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04005866 em->block_len -= start_diff;
5867 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005868 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04005869}
5870
Chris Masonc8b97812008-10-29 14:49:59 -04005871static noinline int uncompress_inline(struct btrfs_path *path,
5872 struct inode *inode, struct page *page,
5873 size_t pg_offset, u64 extent_offset,
5874 struct btrfs_file_extent_item *item)
5875{
5876 int ret;
5877 struct extent_buffer *leaf = path->nodes[0];
5878 char *tmp;
5879 size_t max_size;
5880 unsigned long inline_size;
5881 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08005882 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04005883
5884 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08005885 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04005886 max_size = btrfs_file_extent_ram_bytes(leaf, item);
5887 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01005888 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04005889 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04005890 if (!tmp)
5891 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04005892 ptr = btrfs_file_extent_inline_start(item);
5893
5894 read_extent_buffer(leaf, tmp, ptr, inline_size);
5895
Chris Mason5b050f02008-11-11 09:34:41 -05005896 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08005897 ret = btrfs_decompress(compress_type, tmp, page,
5898 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04005899 if (ret) {
Cong Wang7ac687d2011-11-25 23:14:28 +08005900 char *kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04005901 unsigned long copy_size = min_t(u64,
5902 PAGE_CACHE_SIZE - pg_offset,
5903 max_size - extent_offset);
5904 memset(kaddr + pg_offset, 0, copy_size);
Cong Wang7ac687d2011-11-25 23:14:28 +08005905 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -04005906 }
5907 kfree(tmp);
5908 return 0;
5909}
5910
Chris Masond352ac62008-09-29 15:18:18 -04005911/*
5912 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05005913 * the ugly parts come from merging extents from the disk with the in-ram
5914 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04005915 * where the in-ram extents might be locked pending data=ordered completion.
5916 *
5917 * This also copies inline extents directly into the page.
5918 */
Chris Masond3977122009-01-05 21:25:51 -05005919
Chris Masona52d9a82007-08-27 16:49:44 -04005920struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05005921 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04005922 int create)
5923{
5924 int ret;
5925 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04005926 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04005927 u64 extent_start = 0;
5928 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08005929 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04005930 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04005931 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04005932 struct btrfs_root *root = BTRFS_I(inode)->root;
5933 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04005934 struct extent_buffer *leaf;
5935 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04005936 struct extent_map *em = NULL;
5937 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05005938 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04005939 struct btrfs_trans_handle *trans = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08005940 int compress_type;
Chris Masona52d9a82007-08-27 16:49:44 -04005941
Chris Masona52d9a82007-08-27 16:49:44 -04005942again:
Chris Mason890871b2009-09-02 16:24:52 -04005943 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005944 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04005945 if (em)
5946 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04005947 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005948
Chris Masona52d9a82007-08-27 16:49:44 -04005949 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04005950 if (em->start > start || em->start + em->len <= start)
5951 free_extent_map(em);
5952 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05005953 free_extent_map(em);
5954 else
5955 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005956 }
David Sterba172ddd62011-04-21 00:48:27 +02005957 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04005958 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05005959 err = -ENOMEM;
5960 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005961 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005962 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05005963 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05005964 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05005965 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04005966 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04005967
5968 if (!path) {
5969 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04005970 if (!path) {
5971 err = -ENOMEM;
5972 goto out;
5973 }
5974 /*
5975 * Chances are we'll be called again, so go ahead and do
5976 * readahead
5977 */
5978 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04005979 }
5980
Chris Mason179e29e2007-11-01 11:28:41 -04005981 ret = btrfs_lookup_file_extent(trans, root, path,
5982 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04005983 if (ret < 0) {
5984 err = ret;
5985 goto out;
5986 }
5987
5988 if (ret != 0) {
5989 if (path->slots[0] == 0)
5990 goto not_found;
5991 path->slots[0]--;
5992 }
5993
Chris Mason5f39d392007-10-15 16:14:19 -04005994 leaf = path->nodes[0];
5995 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04005996 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04005997 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04005998 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5999 found_type = btrfs_key_type(&found_key);
6000 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006001 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006002 /*
6003 * If we backup past the first extent we want to move forward
6004 * and see if there is an extent in front of us, otherwise we'll
6005 * say there is a hole for our whole search range which can
6006 * cause problems.
6007 */
6008 extent_end = start;
6009 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006010 }
6011
Chris Mason5f39d392007-10-15 16:14:19 -04006012 found_type = btrfs_file_extent_type(leaf, item);
6013 extent_start = found_key.offset;
Li Zefan261507a02010-12-17 14:21:50 +08006014 compress_type = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04006015 if (found_type == BTRFS_FILE_EXTENT_REG ||
6016 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006017 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006018 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006019 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6020 size_t size;
6021 size = btrfs_file_extent_inline_len(leaf, item);
Qu Wenruofda28322013-02-26 08:10:22 +00006022 extent_end = ALIGN(extent_start + size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04006023 }
Josef Bacik25a50342013-10-14 12:08:38 -04006024next:
Yan Zheng9036c102008-10-30 14:19:41 -04006025 if (start >= extent_end) {
6026 path->slots[0]++;
6027 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6028 ret = btrfs_next_leaf(root, path);
6029 if (ret < 0) {
6030 err = ret;
6031 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006032 }
Yan Zheng9036c102008-10-30 14:19:41 -04006033 if (ret > 0)
6034 goto not_found;
6035 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006036 }
Yan Zheng9036c102008-10-30 14:19:41 -04006037 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6038 if (found_key.objectid != objectid ||
6039 found_key.type != BTRFS_EXTENT_DATA_KEY)
6040 goto not_found;
6041 if (start + len <= found_key.offset)
6042 goto not_found;
6043 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006044 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006045 em->len = found_key.offset - start;
6046 goto not_found_em;
6047 }
6048
Josef Bacikcc95bef2013-04-04 14:31:27 -04006049 em->ram_bytes = btrfs_file_extent_ram_bytes(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04006050 if (found_type == BTRFS_FILE_EXTENT_REG ||
6051 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04006052 em->start = extent_start;
6053 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05006054 em->orig_start = extent_start -
6055 btrfs_file_extent_offset(leaf, item);
Josef Bacikb4939682012-12-03 10:31:19 -05006056 em->orig_block_len = btrfs_file_extent_disk_num_bytes(leaf,
6057 item);
Chris Masondb945352007-10-15 16:15:53 -04006058 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
6059 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04006060 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04006061 goto insert;
6062 }
Li Zefan261507a02010-12-17 14:21:50 +08006063 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006064 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08006065 em->compress_type = compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006066 em->block_start = bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05006067 em->block_len = em->orig_block_len;
Chris Masonc8b97812008-10-29 14:49:59 -04006068 } else {
6069 bytenr += btrfs_file_extent_offset(leaf, item);
6070 em->block_start = bytenr;
6071 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04006072 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
6073 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04006074 }
Chris Masona52d9a82007-08-27 16:49:44 -04006075 goto insert;
6076 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006077 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006078 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006079 size_t size;
6080 size_t extent_offset;
6081 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006082
Chris Masona52d9a82007-08-27 16:49:44 -04006083 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04006084 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04006085 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04006086 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04006087 goto out;
6088 }
6089
Yan Zheng9036c102008-10-30 14:19:41 -04006090 size = btrfs_file_extent_inline_len(leaf, item);
6091 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05006092 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04006093 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006094 em->start = extent_start + extent_offset;
Qu Wenruofda28322013-02-26 08:10:22 +00006095 em->len = ALIGN(copy_size, root->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006096 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006097 em->orig_start = em->start;
Li Zefan261507a02010-12-17 14:21:50 +08006098 if (compress_type) {
Chris Masonc8b97812008-10-29 14:49:59 -04006099 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08006100 em->compress_type = compress_type;
6101 }
Yan689f9342007-10-29 11:41:07 -04006102 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006103 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006104 if (btrfs_file_extent_compression(leaf, item) !=
6105 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006106 ret = uncompress_inline(path, inode, page,
6107 pg_offset,
6108 extent_offset, item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006109 BUG_ON(ret); /* -ENOMEM */
Chris Masonc8b97812008-10-29 14:49:59 -04006110 } else {
6111 map = kmap(page);
6112 read_extent_buffer(leaf, map + pg_offset, ptr,
6113 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04006114 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
6115 memset(map + pg_offset + copy_size, 0,
6116 PAGE_CACHE_SIZE - pg_offset -
6117 copy_size);
6118 }
Chris Masonc8b97812008-10-29 14:49:59 -04006119 kunmap(page);
6120 }
Chris Mason179e29e2007-11-01 11:28:41 -04006121 flush_dcache_page(page);
6122 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006123 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006124 if (!trans) {
6125 kunmap(page);
6126 free_extent_map(em);
6127 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006128
David Sterbab3b4aa72011-04-21 01:20:15 +02006129 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006130 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006131
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006132 if (IS_ERR(trans))
6133 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006134 goto again;
6135 }
Chris Masonc8b97812008-10-29 14:49:59 -04006136 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006137 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006138 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006139 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006140 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006141 }
Chris Masond1310b22008-01-24 16:13:08 -05006142 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006143 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006144 goto insert;
6145 } else {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00006146 WARN(1, KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04006147 }
6148not_found:
6149 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006150 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006151 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006152not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04006153 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04006154 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006155insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006156 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006157 if (em->start > start || extent_map_end(em) <= start) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006158 btrfs_err(root->fs_info, "bad extent! em: [%llu %llu] passed [%llu %llu]",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006159 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04006160 err = -EIO;
6161 goto out;
6162 }
Chris Masond1310b22008-01-24 16:13:08 -05006163
6164 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006165 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006166 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006167 /* it is possible that someone inserted the extent into the tree
6168 * while we had the lock dropped. It is also possible that
6169 * an overlapping map exists in the tree
6170 */
Chris Masona52d9a82007-08-27 16:49:44 -04006171 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04006172 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006173
6174 ret = 0;
6175
Chris Mason3b951512008-04-17 11:29:12 -04006176 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04006177 if (existing && (existing->start > start ||
6178 existing->start + existing->len <= start)) {
6179 free_extent_map(existing);
6180 existing = NULL;
6181 }
Chris Mason3b951512008-04-17 11:29:12 -04006182 if (!existing) {
6183 existing = lookup_extent_mapping(em_tree, em->start,
6184 em->len);
6185 if (existing) {
6186 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006187 em, start,
6188 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04006189 free_extent_map(existing);
6190 if (err) {
6191 free_extent_map(em);
6192 em = NULL;
6193 }
6194 } else {
6195 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04006196 free_extent_map(em);
6197 em = NULL;
6198 }
6199 } else {
6200 free_extent_map(em);
6201 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006202 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006203 }
Chris Masona52d9a82007-08-27 16:49:44 -04006204 }
Chris Mason890871b2009-09-02 16:24:52 -04006205 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006206out:
liubo1abe9b82011-03-24 11:18:59 +00006207
Steven Rostedt4cd85872013-11-14 22:57:29 -05006208 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00006209
Chris Masonf4219502008-07-22 11:18:09 -04006210 if (path)
6211 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04006212 if (trans) {
6213 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05006214 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04006215 err = ret;
6216 }
Chris Masona52d9a82007-08-27 16:49:44 -04006217 if (err) {
6218 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04006219 return ERR_PTR(err);
6220 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006221 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04006222 return em;
6223}
6224
Chris Masonec29ed52011-02-23 16:23:20 -05006225struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
6226 size_t pg_offset, u64 start, u64 len,
6227 int create)
6228{
6229 struct extent_map *em;
6230 struct extent_map *hole_em = NULL;
6231 u64 range_start = start;
6232 u64 end;
6233 u64 found;
6234 u64 found_end;
6235 int err = 0;
6236
6237 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
6238 if (IS_ERR(em))
6239 return em;
6240 if (em) {
6241 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00006242 * if our em maps to
6243 * - a hole or
6244 * - a pre-alloc extent,
6245 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05006246 */
Liu Bof9e4fb52013-01-07 10:10:12 +00006247 if (em->block_start != EXTENT_MAP_HOLE &&
6248 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05006249 return em;
6250 else
6251 hole_em = em;
6252 }
6253
6254 /* check to see if we've wrapped (len == -1 or similar) */
6255 end = start + len;
6256 if (end < start)
6257 end = (u64)-1;
6258 else
6259 end -= 1;
6260
6261 em = NULL;
6262
6263 /* ok, we didn't find anything, lets look for delalloc */
6264 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
6265 end, len, EXTENT_DELALLOC, 1);
6266 found_end = range_start + found;
6267 if (found_end < range_start)
6268 found_end = (u64)-1;
6269
6270 /*
6271 * we didn't find anything useful, return
6272 * the original results from get_extent()
6273 */
6274 if (range_start > end || found_end <= start) {
6275 em = hole_em;
6276 hole_em = NULL;
6277 goto out;
6278 }
6279
6280 /* adjust the range_start to make sure it doesn't
6281 * go backwards from the start they passed in
6282 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05306283 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05006284 found = found_end - range_start;
6285
6286 if (found > 0) {
6287 u64 hole_start = start;
6288 u64 hole_len = len;
6289
David Sterba172ddd62011-04-21 00:48:27 +02006290 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05006291 if (!em) {
6292 err = -ENOMEM;
6293 goto out;
6294 }
6295 /*
6296 * when btrfs_get_extent can't find anything it
6297 * returns one huge hole
6298 *
6299 * make sure what it found really fits our range, and
6300 * adjust to make sure it is based on the start from
6301 * the caller
6302 */
6303 if (hole_em) {
6304 u64 calc_end = extent_map_end(hole_em);
6305
6306 if (calc_end <= start || (hole_em->start > end)) {
6307 free_extent_map(hole_em);
6308 hole_em = NULL;
6309 } else {
6310 hole_start = max(hole_em->start, start);
6311 hole_len = calc_end - hole_start;
6312 }
6313 }
6314 em->bdev = NULL;
6315 if (hole_em && range_start > hole_start) {
6316 /* our hole starts before our delalloc, so we
6317 * have to return just the parts of the hole
6318 * that go until the delalloc starts
6319 */
6320 em->len = min(hole_len,
6321 range_start - hole_start);
6322 em->start = hole_start;
6323 em->orig_start = hole_start;
6324 /*
6325 * don't adjust block start at all,
6326 * it is fixed at EXTENT_MAP_HOLE
6327 */
6328 em->block_start = hole_em->block_start;
6329 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00006330 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
6331 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05006332 } else {
6333 em->start = range_start;
6334 em->len = found;
6335 em->orig_start = range_start;
6336 em->block_start = EXTENT_MAP_DELALLOC;
6337 em->block_len = found;
6338 }
6339 } else if (hole_em) {
6340 return hole_em;
6341 }
6342out:
6343
6344 free_extent_map(hole_em);
6345 if (err) {
6346 free_extent_map(em);
6347 return ERR_PTR(err);
6348 }
6349 return em;
6350}
6351
Josef Bacik4b46fce2010-05-23 11:00:55 -04006352static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
6353 u64 start, u64 len)
6354{
6355 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04006356 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006357 struct btrfs_key ins;
6358 u64 alloc_hint;
6359 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006360
Josef Bacik4b46fce2010-05-23 11:00:55 -04006361 alloc_hint = get_extent_allocation_hint(inode, start, len);
Josef Bacik00361582013-08-14 14:02:47 -04006362 ret = btrfs_reserve_extent(root, len, root->sectorsize, 0,
Josef Bacik81c9ad22012-01-18 10:56:06 -05006363 alloc_hint, &ins, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006364 if (ret)
6365 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006366
Josef Bacik70c8a912012-10-11 16:54:30 -04006367 em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006368 ins.offset, ins.offset, ins.offset, 0);
Josef Bacik00361582013-08-14 14:02:47 -04006369 if (IS_ERR(em)) {
6370 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
6371 return em;
6372 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006373
6374 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
6375 ins.offset, ins.offset, 0);
6376 if (ret) {
6377 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
Josef Bacik00361582013-08-14 14:02:47 -04006378 free_extent_map(em);
6379 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006380 }
Josef Bacik00361582013-08-14 14:02:47 -04006381
Josef Bacik4b46fce2010-05-23 11:00:55 -04006382 return em;
6383}
6384
Chris Mason46bfbb52010-05-26 11:04:10 -04006385/*
6386 * returns 1 when the nocow is safe, < 1 on error, 0 if the
6387 * block must be cow'd
6388 */
Josef Bacik00361582013-08-14 14:02:47 -04006389noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04006390 u64 *orig_start, u64 *orig_block_len,
6391 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04006392{
Josef Bacik00361582013-08-14 14:02:47 -04006393 struct btrfs_trans_handle *trans;
Chris Mason46bfbb52010-05-26 11:04:10 -04006394 struct btrfs_path *path;
6395 int ret;
6396 struct extent_buffer *leaf;
6397 struct btrfs_root *root = BTRFS_I(inode)->root;
6398 struct btrfs_file_extent_item *fi;
6399 struct btrfs_key key;
6400 u64 disk_bytenr;
6401 u64 backref_offset;
6402 u64 extent_end;
6403 u64 num_bytes;
6404 int slot;
6405 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04006406 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Chris Mason46bfbb52010-05-26 11:04:10 -04006407 path = btrfs_alloc_path();
6408 if (!path)
6409 return -ENOMEM;
6410
Josef Bacik00361582013-08-14 14:02:47 -04006411 ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04006412 offset, 0);
6413 if (ret < 0)
6414 goto out;
6415
6416 slot = path->slots[0];
6417 if (ret == 1) {
6418 if (slot == 0) {
6419 /* can't find the item, must cow */
6420 ret = 0;
6421 goto out;
6422 }
6423 slot--;
6424 }
6425 ret = 0;
6426 leaf = path->nodes[0];
6427 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08006428 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04006429 key.type != BTRFS_EXTENT_DATA_KEY) {
6430 /* not our file or wrong item type, must cow */
6431 goto out;
6432 }
6433
6434 if (key.offset > offset) {
6435 /* Wrong offset, must cow */
6436 goto out;
6437 }
6438
6439 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
6440 found_type = btrfs_file_extent_type(leaf, fi);
6441 if (found_type != BTRFS_FILE_EXTENT_REG &&
6442 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
6443 /* not a regular extent, must cow */
6444 goto out;
6445 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04006446
6447 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
6448 goto out;
6449
Chris Mason46bfbb52010-05-26 11:04:10 -04006450 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04006451 if (disk_bytenr == 0)
6452 goto out;
6453
6454 if (btrfs_file_extent_compression(leaf, fi) ||
6455 btrfs_file_extent_encryption(leaf, fi) ||
6456 btrfs_file_extent_other_encoding(leaf, fi))
6457 goto out;
6458
Chris Mason46bfbb52010-05-26 11:04:10 -04006459 backref_offset = btrfs_file_extent_offset(leaf, fi);
6460
Josef Bacik7ee9e442013-06-21 16:37:03 -04006461 if (orig_start) {
6462 *orig_start = key.offset - backref_offset;
6463 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
6464 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
6465 }
Josef Bacikeb384b52013-04-24 16:32:55 -04006466
Chris Mason46bfbb52010-05-26 11:04:10 -04006467 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason46bfbb52010-05-26 11:04:10 -04006468
6469 if (btrfs_extent_readonly(root, disk_bytenr))
6470 goto out;
Josef Bacik1bda19e2013-10-18 12:10:36 -04006471 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04006472
6473 /*
6474 * look for other files referencing this extent, if we
6475 * find any we must cow
6476 */
Josef Bacik00361582013-08-14 14:02:47 -04006477 trans = btrfs_join_transaction(root);
6478 if (IS_ERR(trans)) {
6479 ret = 0;
Chris Mason46bfbb52010-05-26 11:04:10 -04006480 goto out;
Josef Bacik00361582013-08-14 14:02:47 -04006481 }
6482
6483 ret = btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
6484 key.offset - backref_offset, disk_bytenr);
6485 btrfs_end_transaction(trans, root);
6486 if (ret) {
6487 ret = 0;
6488 goto out;
6489 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006490
6491 /*
6492 * adjust disk_bytenr and num_bytes to cover just the bytes
6493 * in this extent we are about to write. If there
6494 * are any csums in that range we have to cow in order
6495 * to keep the csums correct
6496 */
6497 disk_bytenr += backref_offset;
6498 disk_bytenr += offset - key.offset;
Josef Bacikeb384b52013-04-24 16:32:55 -04006499 num_bytes = min(offset + *len, extent_end) - offset;
Chris Mason46bfbb52010-05-26 11:04:10 -04006500 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
6501 goto out;
6502 /*
6503 * all of the above have passed, it is safe to overwrite this extent
6504 * without cow
6505 */
Josef Bacikeb384b52013-04-24 16:32:55 -04006506 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04006507 ret = 1;
6508out:
6509 btrfs_free_path(path);
6510 return ret;
6511}
6512
Josef Bacikeb838e72012-07-31 16:28:48 -04006513static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
6514 struct extent_state **cached_state, int writing)
6515{
6516 struct btrfs_ordered_extent *ordered;
6517 int ret = 0;
6518
6519 while (1) {
6520 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6521 0, cached_state);
6522 /*
6523 * We're concerned with the entire range that we're going to be
6524 * doing DIO to, so we need to make sure theres no ordered
6525 * extents in this range.
6526 */
6527 ordered = btrfs_lookup_ordered_range(inode, lockstart,
6528 lockend - lockstart + 1);
6529
6530 /*
6531 * We need to make sure there are no buffered pages in this
6532 * range either, we could have raced between the invalidate in
6533 * generic_file_direct_write and locking the extent. The
6534 * invalidate needs to happen so that reads after a write do not
6535 * get stale data.
6536 */
6537 if (!ordered && (!writing ||
6538 !test_range_bit(&BTRFS_I(inode)->io_tree,
6539 lockstart, lockend, EXTENT_UPTODATE, 0,
6540 *cached_state)))
6541 break;
6542
6543 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6544 cached_state, GFP_NOFS);
6545
6546 if (ordered) {
6547 btrfs_start_ordered_extent(inode, ordered, 1);
6548 btrfs_put_ordered_extent(ordered);
6549 } else {
6550 /* Screw you mmap */
6551 ret = filemap_write_and_wait_range(inode->i_mapping,
6552 lockstart,
6553 lockend);
6554 if (ret)
6555 break;
6556
6557 /*
6558 * If we found a page that couldn't be invalidated just
6559 * fall back to buffered.
6560 */
6561 ret = invalidate_inode_pages2_range(inode->i_mapping,
6562 lockstart >> PAGE_CACHE_SHIFT,
6563 lockend >> PAGE_CACHE_SHIFT);
6564 if (ret)
6565 break;
6566 }
6567
6568 cond_resched();
6569 }
6570
6571 return ret;
6572}
6573
Josef Bacik69ffb542012-09-11 15:40:07 -04006574static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
6575 u64 len, u64 orig_start,
6576 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006577 u64 orig_block_len, u64 ram_bytes,
6578 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04006579{
6580 struct extent_map_tree *em_tree;
6581 struct extent_map *em;
6582 struct btrfs_root *root = BTRFS_I(inode)->root;
6583 int ret;
6584
6585 em_tree = &BTRFS_I(inode)->extent_tree;
6586 em = alloc_extent_map();
6587 if (!em)
6588 return ERR_PTR(-ENOMEM);
6589
6590 em->start = start;
6591 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04006592 em->mod_start = start;
6593 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04006594 em->len = len;
6595 em->block_len = block_len;
6596 em->block_start = block_start;
6597 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05006598 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04006599 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04006600 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04006601 set_bit(EXTENT_FLAG_PINNED, &em->flags);
6602 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05006603 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04006604
6605 do {
6606 btrfs_drop_extent_cache(inode, em->start,
6607 em->start + em->len - 1, 0);
6608 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006609 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04006610 write_unlock(&em_tree->lock);
6611 } while (ret == -EEXIST);
6612
6613 if (ret) {
6614 free_extent_map(em);
6615 return ERR_PTR(ret);
6616 }
6617
6618 return em;
6619}
6620
6621
Josef Bacik4b46fce2010-05-23 11:00:55 -04006622static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
6623 struct buffer_head *bh_result, int create)
6624{
6625 struct extent_map *em;
6626 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04006627 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006628 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04006629 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006630 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04006631 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00006632 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006633
Miao Xie172a5042013-02-21 02:48:22 -07006634 if (create)
Josef Bacikeb838e72012-07-31 16:28:48 -04006635 unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07006636 else
Josef Bacikc3298612012-08-03 16:49:19 -04006637 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04006638
Josef Bacikc3298612012-08-03 16:49:19 -04006639 lockstart = start;
6640 lockend = start + len - 1;
6641
Josef Bacikeb838e72012-07-31 16:28:48 -04006642 /*
6643 * If this errors out it's because we couldn't invalidate pagecache for
6644 * this range and we need to fallback to buffered.
6645 */
6646 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
6647 return -ENOTBLK;
6648
Josef Bacik4b46fce2010-05-23 11:00:55 -04006649 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04006650 if (IS_ERR(em)) {
6651 ret = PTR_ERR(em);
6652 goto unlock_err;
6653 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006654
6655 /*
6656 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
6657 * io. INLINE is special, and we could probably kludge it in here, but
6658 * it's still buffered so for safety lets just fall back to the generic
6659 * buffered path.
6660 *
6661 * For COMPRESSED we _have_ to read the entire extent in so we can
6662 * decompress it, so there will be buffering required no matter what we
6663 * do, so go ahead and fallback to buffered.
6664 *
6665 * We return -ENOTBLK because thats what makes DIO go ahead and go back
6666 * to buffered IO. Don't blame me, this is the price we pay for using
6667 * the generic code.
6668 */
6669 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
6670 em->block_start == EXTENT_MAP_INLINE) {
6671 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006672 ret = -ENOTBLK;
6673 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006674 }
6675
6676 /* Just a good old fashioned hole, return */
6677 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
6678 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
6679 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006680 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006681 }
6682
6683 /*
6684 * We don't allocate a new extent in the following cases
6685 *
6686 * 1) The inode is marked as NODATACOW. In this case we'll just use the
6687 * existing extent.
6688 * 2) The extent is marked as PREALLOC. We're good to go here and can
6689 * just use the extent.
6690 *
6691 */
Chris Mason46bfbb52010-05-26 11:04:10 -04006692 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04006693 len = min(len, em->len - (start - em->start));
6694 lockstart = start + len;
6695 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04006696 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006697
6698 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
6699 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
6700 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04006701 int type;
6702 int ret;
Josef Bacikeb384b52013-04-24 16:32:55 -04006703 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006704
6705 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6706 type = BTRFS_ORDERED_PREALLOC;
6707 else
6708 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04006709 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04006710 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04006711
Josef Bacik00361582013-08-14 14:02:47 -04006712 if (can_nocow_extent(inode, start, &len, &orig_start,
Josef Bacik7ee9e442013-06-21 16:37:03 -04006713 &orig_block_len, &ram_bytes) == 1) {
Josef Bacik69ffb542012-09-11 15:40:07 -04006714 if (type == BTRFS_ORDERED_PREALLOC) {
6715 free_extent_map(em);
6716 em = create_pinned_em(inode, start, len,
6717 orig_start,
Josef Bacikb4939682012-12-03 10:31:19 -05006718 block_start, len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006719 orig_block_len,
6720 ram_bytes, type);
Josef Bacik00361582013-08-14 14:02:47 -04006721 if (IS_ERR(em))
Josef Bacik69ffb542012-09-11 15:40:07 -04006722 goto unlock_err;
Josef Bacik69ffb542012-09-11 15:40:07 -04006723 }
6724
Chris Mason46bfbb52010-05-26 11:04:10 -04006725 ret = btrfs_add_ordered_extent_dio(inode, start,
6726 block_start, len, len, type);
Chris Mason46bfbb52010-05-26 11:04:10 -04006727 if (ret) {
6728 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006729 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04006730 }
6731 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006732 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006733 }
Josef Bacik00361582013-08-14 14:02:47 -04006734
Chris Mason46bfbb52010-05-26 11:04:10 -04006735 /*
6736 * this will cow the extent, reset the len in case we changed
6737 * it above
6738 */
6739 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04006740 free_extent_map(em);
6741 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04006742 if (IS_ERR(em)) {
6743 ret = PTR_ERR(em);
6744 goto unlock_err;
6745 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006746 len = min(len, em->len - (start - em->start));
6747unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04006748 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
6749 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04006750 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006751 bh_result->b_bdev = em->bdev;
6752 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04006753 if (create) {
6754 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6755 set_buffer_new(bh_result);
6756
6757 /*
6758 * Need to update the i_size under the extent lock so buffered
6759 * readers will get the updated i_size when we unlock.
6760 */
6761 if (start + len > i_size_read(inode))
6762 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00006763
Miao Xie172a5042013-02-21 02:48:22 -07006764 spin_lock(&BTRFS_I(inode)->lock);
6765 BTRFS_I(inode)->outstanding_extents++;
6766 spin_unlock(&BTRFS_I(inode)->lock);
6767
Miao Xie09348562013-02-07 10:12:07 +00006768 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6769 lockstart + len - 1, EXTENT_DELALLOC, NULL,
6770 &cached_state, GFP_NOFS);
6771 BUG_ON(ret);
Josef Bacikc3473e82012-06-19 10:59:00 -04006772 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006773
Josef Bacikeb838e72012-07-31 16:28:48 -04006774 /*
6775 * In the case of write we need to clear and unlock the entire range,
6776 * in the case of read we need to unlock only the end area that we
6777 * aren't using if there is any left over space.
6778 */
Liu Bo24c03fa2012-08-22 20:10:38 -06006779 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00006780 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6781 lockend, unlock_bits, 1, 0,
6782 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06006783 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04006784 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06006785 }
Josef Bacikeb838e72012-07-31 16:28:48 -04006786
Josef Bacik4b46fce2010-05-23 11:00:55 -04006787 free_extent_map(em);
6788
6789 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006790
6791unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04006792 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6793 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
6794 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006795}
6796
Josef Bacik4b46fce2010-05-23 11:00:55 -04006797static void btrfs_endio_direct_read(struct bio *bio, int err)
6798{
Miao Xiee65e1532010-11-22 03:04:43 +00006799 struct btrfs_dio_private *dip = bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006800 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
6801 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006802 struct inode *inode = dip->inode;
6803 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason9be33952013-05-17 18:30:14 -04006804 struct bio *dio_bio;
Miao Xiefacc8a222013-07-25 19:22:34 +08006805 u32 *csums = (u32 *)dip->csum;
6806 int index = 0;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006807 u64 start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006808
6809 start = dip->logical_offset;
6810 do {
6811 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
6812 struct page *page = bvec->bv_page;
6813 char *kaddr;
6814 u32 csum = ~(u32)0;
6815 unsigned long flags;
6816
6817 local_irq_save(flags);
Cong Wang7ac687d2011-11-25 23:14:28 +08006818 kaddr = kmap_atomic(page);
Liu Bob0496682013-03-14 14:57:45 +00006819 csum = btrfs_csum_data(kaddr + bvec->bv_offset,
Josef Bacik4b46fce2010-05-23 11:00:55 -04006820 csum, bvec->bv_len);
6821 btrfs_csum_final(csum, (char *)&csum);
Cong Wang7ac687d2011-11-25 23:14:28 +08006822 kunmap_atomic(kaddr);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006823 local_irq_restore(flags);
6824
6825 flush_dcache_page(bvec->bv_page);
Miao Xiefacc8a222013-07-25 19:22:34 +08006826 if (csum != csums[index]) {
6827 btrfs_err(root->fs_info, "csum failed ino %llu off %llu csum %u expected csum %u",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006828 btrfs_ino(inode), start, csum,
6829 csums[index]);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006830 err = -EIO;
6831 }
6832 }
6833
6834 start += bvec->bv_len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006835 bvec++;
Miao Xiefacc8a222013-07-25 19:22:34 +08006836 index++;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006837 } while (bvec <= bvec_end);
6838
6839 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006840 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04006841 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006842
Josef Bacik4b46fce2010-05-23 11:00:55 -04006843 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006844
6845 /* If we had a csum failure make sure to clear the uptodate flag */
6846 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04006847 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
6848 dio_end_io(dio_bio, err);
6849 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006850}
6851
6852static void btrfs_endio_direct_write(struct bio *bio, int err)
6853{
6854 struct btrfs_dio_private *dip = bio->bi_private;
6855 struct inode *inode = dip->inode;
6856 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006857 struct btrfs_ordered_extent *ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05006858 u64 ordered_offset = dip->logical_offset;
6859 u64 ordered_bytes = dip->bytes;
Chris Mason9be33952013-05-17 18:30:14 -04006860 struct bio *dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006861 int ret;
6862
6863 if (err)
6864 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05006865again:
6866 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
6867 &ordered_offset,
Josef Bacik5fd02042012-05-02 14:00:54 -04006868 ordered_bytes, !err);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006869 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05006870 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006871
Josef Bacik5fd02042012-05-02 14:00:54 -04006872 ordered->work.func = finish_ordered_fn;
6873 ordered->work.flags = 0;
6874 btrfs_queue_worker(&root->fs_info->endio_write_workers,
6875 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05006876out_test:
6877 /*
6878 * our bio might span multiple ordered extents. If we haven't
6879 * completed the accounting for the whole dio, go back and try again
6880 */
6881 if (ordered_offset < dip->logical_offset + dip->bytes) {
6882 ordered_bytes = dip->logical_offset + dip->bytes -
6883 ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04006884 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05006885 goto again;
6886 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006887out_done:
Chris Mason9be33952013-05-17 18:30:14 -04006888 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006889
Josef Bacik4b46fce2010-05-23 11:00:55 -04006890 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006891
6892 /* If we had an error make sure to clear the uptodate flag */
6893 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04006894 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
6895 dio_end_io(dio_bio, err);
6896 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006897}
6898
Chris Masoneaf25d92010-05-25 09:48:28 -04006899static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
6900 struct bio *bio, int mirror_num,
6901 unsigned long bio_flags, u64 offset)
6902{
6903 int ret;
6904 struct btrfs_root *root = BTRFS_I(inode)->root;
6905 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006906 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04006907 return 0;
6908}
6909
Miao Xiee65e1532010-11-22 03:04:43 +00006910static void btrfs_end_dio_bio(struct bio *bio, int err)
6911{
6912 struct btrfs_dio_private *dip = bio->bi_private;
6913
6914 if (err) {
Li Zefan33345d012011-04-20 10:31:50 +08006915 printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu "
Jan Beulich3dd14622010-12-07 14:54:09 +00006916 "sector %#Lx len %u err no %d\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006917 btrfs_ino(dip->inode), bio->bi_rw,
Jan Beulich3dd14622010-12-07 14:54:09 +00006918 (unsigned long long)bio->bi_sector, bio->bi_size, err);
Miao Xiee65e1532010-11-22 03:04:43 +00006919 dip->errors = 1;
6920
6921 /*
6922 * before atomic variable goto zero, we must make sure
6923 * dip->errors is perceived to be set.
6924 */
6925 smp_mb__before_atomic_dec();
6926 }
6927
6928 /* if there are more bios still pending for this dio, just exit */
6929 if (!atomic_dec_and_test(&dip->pending_bios))
6930 goto out;
6931
Chris Mason9be33952013-05-17 18:30:14 -04006932 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00006933 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04006934 } else {
6935 set_bit(BIO_UPTODATE, &dip->dio_bio->bi_flags);
Miao Xiee65e1532010-11-22 03:04:43 +00006936 bio_endio(dip->orig_bio, 0);
6937 }
6938out:
6939 bio_put(bio);
6940}
6941
6942static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
6943 u64 first_sector, gfp_t gfp_flags)
6944{
6945 int nr_vecs = bio_get_nr_vecs(bdev);
6946 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
6947}
6948
6949static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
6950 int rw, u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04006951 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00006952{
Miao Xiefacc8a222013-07-25 19:22:34 +08006953 struct btrfs_dio_private *dip = bio->bi_private;
Miao Xiee65e1532010-11-22 03:04:43 +00006954 int write = rw & REQ_WRITE;
6955 struct btrfs_root *root = BTRFS_I(inode)->root;
6956 int ret;
6957
Josef Bacikb812ce22012-11-16 13:56:32 -05006958 if (async_submit)
6959 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
6960
Miao Xiee65e1532010-11-22 03:04:43 +00006961 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04006962
6963 if (!write) {
6964 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
6965 if (ret)
6966 goto err;
6967 }
Miao Xiee65e1532010-11-22 03:04:43 +00006968
Josef Bacik1ae39932011-04-06 14:41:34 -04006969 if (skip_sum)
6970 goto map;
6971
6972 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00006973 ret = btrfs_wq_submit_bio(root->fs_info,
6974 inode, rw, bio, 0, 0,
6975 file_offset,
6976 __btrfs_submit_bio_start_direct_io,
6977 __btrfs_submit_bio_done);
6978 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04006979 } else if (write) {
6980 /*
6981 * If we aren't doing async submit, calculate the csum of the
6982 * bio now.
6983 */
6984 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
6985 if (ret)
6986 goto err;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00006987 } else if (!skip_sum) {
Miao Xiefacc8a222013-07-25 19:22:34 +08006988 ret = btrfs_lookup_bio_sums_dio(root, inode, dip, bio,
6989 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00006990 if (ret)
6991 goto err;
6992 }
Miao Xiee65e1532010-11-22 03:04:43 +00006993
Josef Bacik1ae39932011-04-06 14:41:34 -04006994map:
6995 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006996err:
6997 bio_put(bio);
6998 return ret;
6999}
7000
7001static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
7002 int skip_sum)
7003{
7004 struct inode *inode = dip->inode;
7005 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00007006 struct bio *bio;
7007 struct bio *orig_bio = dip->orig_bio;
7008 struct bio_vec *bvec = orig_bio->bi_io_vec;
7009 u64 start_sector = orig_bio->bi_sector;
7010 u64 file_offset = dip->logical_offset;
7011 u64 submit_len = 0;
7012 u64 map_length;
7013 int nr_pages = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007014 int ret = 0;
Josef Bacik1ae39932011-04-06 14:41:34 -04007015 int async_submit = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007016
Miao Xiee65e1532010-11-22 03:04:43 +00007017 map_length = orig_bio->bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007018 ret = btrfs_map_block(root->fs_info, rw, start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007019 &map_length, NULL, 0);
7020 if (ret) {
Josef Bacik64728bb2011-04-25 19:43:52 -04007021 bio_put(orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00007022 return -EIO;
7023 }
Miao Xiefacc8a222013-07-25 19:22:34 +08007024
Josef Bacik02f57c72011-04-06 14:25:44 -04007025 if (map_length >= orig_bio->bi_size) {
7026 bio = orig_bio;
7027 goto submit;
7028 }
7029
David Woodhouse53b381b2013-01-29 18:40:14 -05007030 /* async crcs make it difficult to collect full stripe writes. */
7031 if (btrfs_get_alloc_profile(root, 1) &
7032 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6))
7033 async_submit = 0;
7034 else
7035 async_submit = 1;
7036
Josef Bacik02f57c72011-04-06 14:25:44 -04007037 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
7038 if (!bio)
7039 return -ENOMEM;
7040 bio->bi_private = dip;
7041 bio->bi_end_io = btrfs_end_dio_bio;
7042 atomic_inc(&dip->pending_bios);
7043
Miao Xiee65e1532010-11-22 03:04:43 +00007044 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
7045 if (unlikely(map_length < submit_len + bvec->bv_len ||
7046 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
7047 bvec->bv_offset) < bvec->bv_len)) {
7048 /*
7049 * inc the count before we submit the bio so
7050 * we know the end IO handler won't happen before
7051 * we inc the count. Otherwise, the dip might get freed
7052 * before we're done setting it up
7053 */
7054 atomic_inc(&dip->pending_bios);
7055 ret = __btrfs_submit_dio_bio(bio, inode, rw,
7056 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007057 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007058 if (ret) {
7059 bio_put(bio);
7060 atomic_dec(&dip->pending_bios);
7061 goto out_err;
7062 }
7063
Miao Xiee65e1532010-11-22 03:04:43 +00007064 start_sector += submit_len >> 9;
7065 file_offset += submit_len;
7066
7067 submit_len = 0;
7068 nr_pages = 0;
7069
7070 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
7071 start_sector, GFP_NOFS);
7072 if (!bio)
7073 goto out_err;
7074 bio->bi_private = dip;
7075 bio->bi_end_io = btrfs_end_dio_bio;
7076
7077 map_length = orig_bio->bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007078 ret = btrfs_map_block(root->fs_info, rw,
Stefan Behrens3ec706c2012-11-05 15:46:42 +01007079 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007080 &map_length, NULL, 0);
7081 if (ret) {
7082 bio_put(bio);
7083 goto out_err;
7084 }
7085 } else {
7086 submit_len += bvec->bv_len;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307087 nr_pages++;
Miao Xiee65e1532010-11-22 03:04:43 +00007088 bvec++;
7089 }
7090 }
7091
Josef Bacik02f57c72011-04-06 14:25:44 -04007092submit:
Miao Xiee65e1532010-11-22 03:04:43 +00007093 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007094 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007095 if (!ret)
7096 return 0;
7097
7098 bio_put(bio);
7099out_err:
7100 dip->errors = 1;
7101 /*
7102 * before atomic variable goto zero, we must
7103 * make sure dip->errors is perceived to be set.
7104 */
7105 smp_mb__before_atomic_dec();
7106 if (atomic_dec_and_test(&dip->pending_bios))
7107 bio_io_error(dip->orig_bio);
7108
7109 /* bio_end_io() will handle error, so we needn't return it */
7110 return 0;
7111}
7112
Chris Mason9be33952013-05-17 18:30:14 -04007113static void btrfs_submit_direct(int rw, struct bio *dio_bio,
7114 struct inode *inode, loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007115{
7116 struct btrfs_root *root = BTRFS_I(inode)->root;
7117 struct btrfs_dio_private *dip;
Chris Mason9be33952013-05-17 18:30:14 -04007118 struct bio *io_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007119 int skip_sum;
Miao Xiefacc8a222013-07-25 19:22:34 +08007120 int sum_len;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02007121 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007122 int ret = 0;
Miao Xiefacc8a222013-07-25 19:22:34 +08007123 u16 csum_size;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007124
7125 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
7126
Chris Mason9be33952013-05-17 18:30:14 -04007127 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04007128 if (!io_bio) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007129 ret = -ENOMEM;
7130 goto free_ordered;
7131 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007132
Miao Xiefacc8a222013-07-25 19:22:34 +08007133 if (!skip_sum && !write) {
7134 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
7135 sum_len = dio_bio->bi_size >> inode->i_sb->s_blocksize_bits;
7136 sum_len *= csum_size;
7137 } else {
7138 sum_len = 0;
7139 }
7140
7141 dip = kmalloc(sizeof(*dip) + sum_len, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04007142 if (!dip) {
7143 ret = -ENOMEM;
7144 goto free_io_bio;
7145 }
7146
7147 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007148 dip->inode = inode;
7149 dip->logical_offset = file_offset;
Miao Xiee6da5d2e2013-06-19 18:19:17 +08007150 dip->bytes = dio_bio->bi_size;
Chris Mason9be33952013-05-17 18:30:14 -04007151 dip->disk_bytenr = (u64)dio_bio->bi_sector << 9;
7152 io_bio->bi_private = dip;
Miao Xiee65e1532010-11-22 03:04:43 +00007153 dip->errors = 0;
Chris Mason9be33952013-05-17 18:30:14 -04007154 dip->orig_bio = io_bio;
7155 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00007156 atomic_set(&dip->pending_bios, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007157
7158 if (write)
Chris Mason9be33952013-05-17 18:30:14 -04007159 io_bio->bi_end_io = btrfs_endio_direct_write;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007160 else
Chris Mason9be33952013-05-17 18:30:14 -04007161 io_bio->bi_end_io = btrfs_endio_direct_read;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007162
Miao Xiee65e1532010-11-22 03:04:43 +00007163 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
7164 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04007165 return;
Chris Mason9be33952013-05-17 18:30:14 -04007166
7167free_io_bio:
7168 bio_put(io_bio);
7169
Josef Bacik4b46fce2010-05-23 11:00:55 -04007170free_ordered:
7171 /*
7172 * If this is a write, we need to clean up the reserved space and kill
7173 * the ordered extent.
7174 */
7175 if (write) {
7176 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05007177 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007178 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
7179 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
7180 btrfs_free_reserved_extent(root, ordered->start,
7181 ordered->disk_len);
7182 btrfs_put_ordered_extent(ordered);
7183 btrfs_put_ordered_extent(ordered);
7184 }
Chris Mason9be33952013-05-17 18:30:14 -04007185 bio_endio(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007186}
7187
Chris Mason5a5f79b2010-05-26 21:33:37 -04007188static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
7189 const struct iovec *iov, loff_t offset,
7190 unsigned long nr_segs)
7191{
7192 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00007193 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007194 size_t size;
7195 unsigned long addr;
7196 unsigned blocksize_mask = root->sectorsize - 1;
7197 ssize_t retval = -EINVAL;
7198 loff_t end = offset;
7199
7200 if (offset & blocksize_mask)
7201 goto out;
7202
7203 /* Check the memory alignment. Blocks cannot straddle pages */
7204 for (seg = 0; seg < nr_segs; seg++) {
7205 addr = (unsigned long)iov[seg].iov_base;
7206 size = iov[seg].iov_len;
7207 end += size;
Josef Bacika1b75f72011-04-08 15:51:18 +00007208 if ((addr & blocksize_mask) || (size & blocksize_mask))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007209 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00007210
7211 /* If this is a write we don't need to check anymore */
7212 if (rw & WRITE)
7213 continue;
7214
7215 /*
7216 * Check to make sure we don't have duplicate iov_base's in this
7217 * iovec, if so return EINVAL, otherwise we'll get csum errors
7218 * when reading back.
7219 */
7220 for (i = seg + 1; i < nr_segs; i++) {
7221 if (iov[seg].iov_base == iov[i].iov_base)
7222 goto out;
7223 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04007224 }
7225 retval = 0;
7226out:
7227 return retval;
7228}
Josef Bacikeb838e72012-07-31 16:28:48 -04007229
Chris Mason16432982008-04-10 10:23:21 -04007230static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
7231 const struct iovec *iov, loff_t offset,
7232 unsigned long nr_segs)
7233{
Josef Bacik4b46fce2010-05-23 11:00:55 -04007234 struct file *file = iocb->ki_filp;
7235 struct inode *inode = file->f_mapping->host;
Miao Xie09348562013-02-07 10:12:07 +00007236 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00007237 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00007238 bool wakeup = true;
7239 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00007240 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007241
Chris Mason5a5f79b2010-05-26 21:33:37 -04007242 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
Josef Bacikeb838e72012-07-31 16:28:48 -04007243 offset, nr_segs))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007244 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007245
Miao Xie38851cc2013-02-08 07:04:11 +00007246 atomic_inc(&inode->i_dio_count);
7247 smp_mb__after_atomic_inc();
7248
Josef Bacik0e267c42013-07-02 10:38:02 -04007249 /*
7250 * The generic stuff only does filemap_write_and_wait_range, which isn't
7251 * enough if we've written compressed pages to this area, so we need to
7252 * call btrfs_wait_ordered_range to make absolutely sure that any
7253 * outstanding dirty pages are on disk.
7254 */
7255 count = iov_length(iov, nr_segs);
Josef Bacik0ef8b722013-10-25 16:13:35 -04007256 ret = btrfs_wait_ordered_range(inode, offset, count);
7257 if (ret)
7258 return ret;
Josef Bacik0e267c42013-07-02 10:38:02 -04007259
Miao Xie09348562013-02-07 10:12:07 +00007260 if (rw & WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00007261 /*
7262 * If the write DIO is beyond the EOF, we need update
7263 * the isize, but it is protected by i_mutex. So we can
7264 * not unlock the i_mutex at this case.
7265 */
7266 if (offset + count <= inode->i_size) {
7267 mutex_unlock(&inode->i_mutex);
7268 relock = true;
7269 }
Miao Xie09348562013-02-07 10:12:07 +00007270 ret = btrfs_delalloc_reserve_space(inode, count);
7271 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00007272 goto out;
7273 } else if (unlikely(test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
7274 &BTRFS_I(inode)->runtime_flags))) {
7275 inode_dio_done(inode);
7276 flags = DIO_LOCKING | DIO_SKIP_HOLES;
7277 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00007278 }
7279
7280 ret = __blockdev_direct_IO(rw, iocb, inode,
7281 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
7282 iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
Miao Xie2e60a512013-02-08 07:01:08 +00007283 btrfs_submit_direct, flags);
Miao Xie09348562013-02-07 10:12:07 +00007284 if (rw & WRITE) {
7285 if (ret < 0 && ret != -EIOCBQUEUED)
7286 btrfs_delalloc_release_space(inode, count);
Miao Xie172a5042013-02-21 02:48:22 -07007287 else if (ret >= 0 && (size_t)ret < count)
Miao Xie09348562013-02-07 10:12:07 +00007288 btrfs_delalloc_release_space(inode,
7289 count - (size_t)ret);
Miao Xie172a5042013-02-21 02:48:22 -07007290 else
7291 btrfs_delalloc_release_metadata(inode, 0);
Miao Xie09348562013-02-07 10:12:07 +00007292 }
Miao Xie38851cc2013-02-08 07:04:11 +00007293out:
Miao Xie2e60a512013-02-08 07:01:08 +00007294 if (wakeup)
7295 inode_dio_done(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00007296 if (relock)
7297 mutex_lock(&inode->i_mutex);
Miao Xie09348562013-02-07 10:12:07 +00007298
7299 return ret;
Chris Mason16432982008-04-10 10:23:21 -04007300}
7301
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007302#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
7303
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007304static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
7305 __u64 start, __u64 len)
7306{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007307 int ret;
7308
7309 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
7310 if (ret)
7311 return ret;
7312
Chris Masonec29ed52011-02-23 16:23:20 -05007313 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007314}
7315
Chris Mason9ebefb182007-06-15 13:50:00 -04007316int btrfs_readpage(struct file *file, struct page *page)
7317{
Chris Masond1310b22008-01-24 16:13:08 -05007318 struct extent_io_tree *tree;
7319 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02007320 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04007321}
Chris Mason1832a6d2007-12-21 16:27:21 -05007322
Chris Mason39279cc2007-06-12 06:35:45 -04007323static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
7324{
Chris Masond1310b22008-01-24 16:13:08 -05007325 struct extent_io_tree *tree;
Chris Masonb888db2b2007-08-27 16:49:44 -04007326
7327
7328 if (current->flags & PF_MEMALLOC) {
7329 redirty_page_for_writepage(wbc, page);
7330 unlock_page(page);
7331 return 0;
7332 }
Chris Masond1310b22008-01-24 16:13:08 -05007333 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04007334 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
7335}
Chris Mason39279cc2007-06-12 06:35:45 -04007336
Eric Sandeen48a3b632013-04-25 20:41:01 +00007337static int btrfs_writepages(struct address_space *mapping,
7338 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04007339{
Chris Masond1310b22008-01-24 16:13:08 -05007340 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05007341
Chris Masond1310b22008-01-24 16:13:08 -05007342 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f02e2007-11-01 19:45:34 -04007343 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
7344}
7345
Chris Mason3ab2fb52007-11-08 10:59:22 -05007346static int
7347btrfs_readpages(struct file *file, struct address_space *mapping,
7348 struct list_head *pages, unsigned nr_pages)
7349{
Chris Masond1310b22008-01-24 16:13:08 -05007350 struct extent_io_tree *tree;
7351 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05007352 return extent_readpages(tree, mapping, pages, nr_pages,
7353 btrfs_get_extent);
7354}
Chris Masone6dcd2d2008-07-17 12:53:50 -04007355static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04007356{
Chris Masond1310b22008-01-24 16:13:08 -05007357 struct extent_io_tree *tree;
7358 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04007359 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04007360
Chris Masond1310b22008-01-24 16:13:08 -05007361 tree = &BTRFS_I(page->mapping->host)->io_tree;
7362 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05007363 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04007364 if (ret == 1) {
7365 ClearPagePrivate(page);
7366 set_page_private(page, 0);
7367 page_cache_release(page);
7368 }
7369 return ret;
7370}
Chris Mason39279cc2007-06-12 06:35:45 -04007371
Chris Masone6dcd2d2008-07-17 12:53:50 -04007372static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
7373{
Chris Mason98509cf2008-09-11 15:51:43 -04007374 if (PageWriteback(page) || PageDirty(page))
7375 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05007376 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007377}
7378
Lukas Czernerd47992f2013-05-21 23:17:23 -04007379static void btrfs_invalidatepage(struct page *page, unsigned int offset,
7380 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04007381{
Josef Bacik5fd02042012-05-02 14:00:54 -04007382 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05007383 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007384 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007385 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007386 u64 page_start = page_offset(page);
7387 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04007388
Chris Mason8b62b722009-09-02 16:53:46 -04007389 /*
7390 * we have the page locked, so new writeback can't start,
7391 * and the dirty bit won't be cleared while we are here.
7392 *
7393 * Wait for IO on this page so that we can safely clear
7394 * the PagePrivate2 bit and do ordered accounting
7395 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007396 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04007397
Josef Bacik5fd02042012-05-02 14:00:54 -04007398 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007399 if (offset) {
7400 btrfs_releasepage(page, GFP_NOFS);
7401 return;
7402 }
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007403 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Miao Xie4eee4fa2012-12-21 09:17:45 +00007404 ordered = btrfs_lookup_ordered_extent(inode, page_offset(page));
Chris Masone6dcd2d2008-07-17 12:53:50 -04007405 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04007406 /*
7407 * IO on this page will never be started, so we need
7408 * to account for any ordered extents now
7409 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007410 clear_extent_bit(tree, page_start, page_end,
7411 EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06007412 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
7413 EXTENT_DEFRAG, 1, 0, &cached_state, GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04007414 /*
7415 * whoever cleared the private bit is responsible
7416 * for the finish_ordered_io
7417 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04007418 if (TestClearPagePrivate2(page)) {
7419 struct btrfs_ordered_inode_tree *tree;
7420 u64 new_len;
7421
7422 tree = &BTRFS_I(inode)->ordered_tree;
7423
7424 spin_lock_irq(&tree->lock);
7425 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
7426 new_len = page_start - ordered->file_offset;
7427 if (new_len < ordered->truncated_len)
7428 ordered->truncated_len = new_len;
7429 spin_unlock_irq(&tree->lock);
7430
7431 if (btrfs_dec_test_ordered_pending(inode, &ordered,
7432 page_start,
7433 PAGE_CACHE_SIZE, 1))
7434 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04007435 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007436 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +00007437 cached_state = NULL;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007438 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007439 }
7440 clear_extent_bit(tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04007441 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06007442 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
7443 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007444 __btrfs_releasepage(page, GFP_NOFS);
7445
Chris Mason4a096752008-07-21 10:29:44 -04007446 ClearPageChecked(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04007447 if (PagePrivate(page)) {
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04007448 ClearPagePrivate(page);
7449 set_page_private(page, 0);
7450 page_cache_release(page);
7451 }
Chris Mason39279cc2007-06-12 06:35:45 -04007452}
7453
Chris Mason9ebefb182007-06-15 13:50:00 -04007454/*
7455 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
7456 * called from a page fault handler when a page is first dirtied. Hence we must
7457 * be careful to check for EOF conditions here. We set the page up correctly
7458 * for a written page which means we get ENOSPC checking when writing into
7459 * holes and correct delalloc and unwritten extent mapping on filesystems that
7460 * support these features.
7461 *
7462 * We are not allowed to take the i_mutex here so we have to play games to
7463 * protect against truncate races as the page could now be beyond EOF. Because
7464 * vmtruncate() writes the inode size before removing pages, once we have the
7465 * page lock we can determine safely if the page is beyond EOF. If it is not
7466 * beyond EOF, then the page is guaranteed safe against truncation until we
7467 * unlock the page.
7468 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07007469int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04007470{
Nick Pigginc2ec1752009-03-31 15:23:21 -07007471 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05007472 struct inode *inode = file_inode(vma->vm_file);
Chris Mason1832a6d2007-12-21 16:27:21 -05007473 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007474 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7475 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007476 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007477 char *kaddr;
7478 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04007479 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05007480 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05007481 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007482 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007483 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04007484
Jan Karab2b5ef52012-06-12 16:20:45 +02007485 sb_start_pagefault(inode->i_sb);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04007486 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007487 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04007488 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05007489 reserved = 1;
7490 }
Nick Piggin56a76f82009-03-31 15:23:23 -07007491 if (ret) {
7492 if (ret == -ENOMEM)
7493 ret = VM_FAULT_OOM;
7494 else /* -ENOSPC, -EIO, etc */
7495 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05007496 if (reserved)
7497 goto out;
7498 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07007499 }
Chris Mason1832a6d2007-12-21 16:27:21 -05007500
Nick Piggin56a76f82009-03-31 15:23:23 -07007501 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007502again:
Chris Mason9ebefb182007-06-15 13:50:00 -04007503 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04007504 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007505 page_start = page_offset(page);
7506 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04007507
Chris Mason9ebefb182007-06-15 13:50:00 -04007508 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04007509 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04007510 /* page got truncated out from underneath us */
7511 goto out_unlock;
7512 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007513 wait_on_page_writeback(page);
7514
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007515 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007516 set_page_extent_mapped(page);
7517
Chris Masoneb84ae02008-07-17 13:53:27 -04007518 /*
7519 * we can't set the delalloc bits if there are pending ordered
7520 * extents. Drop our locks and wait for them to finish
7521 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007522 ordered = btrfs_lookup_ordered_extent(inode, page_start);
7523 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007524 unlock_extent_cached(io_tree, page_start, page_end,
7525 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007526 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04007527 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007528 btrfs_put_ordered_extent(ordered);
7529 goto again;
7530 }
7531
Josef Bacikfbf19082009-10-01 17:10:23 -04007532 /*
7533 * XXX - page_mkwrite gets called every time the page is dirtied, even
7534 * if it was already dirty, so for space accounting reasons we need to
7535 * clear any delalloc bits for the range we are fixing to save. There
7536 * is probably a better way to do this, but for now keep consistent with
7537 * prepare_pages in the normal write path.
7538 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00007539 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06007540 EXTENT_DIRTY | EXTENT_DELALLOC |
7541 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00007542 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04007543
Josef Bacik2ac55d42010-02-03 19:33:23 +00007544 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
7545 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007546 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007547 unlock_extent_cached(io_tree, page_start, page_end,
7548 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007549 ret = VM_FAULT_SIGBUS;
7550 goto out_unlock;
7551 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007552 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04007553
7554 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04007555 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04007556 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04007557 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04007558 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04007559
Chris Masone6dcd2d2008-07-17 12:53:50 -04007560 if (zero_start != PAGE_CACHE_SIZE) {
7561 kaddr = kmap(page);
7562 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
7563 flush_dcache_page(page);
7564 kunmap(page);
7565 }
Chris Mason247e7432008-07-17 12:53:51 -04007566 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007567 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04007568 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007569
Chris Mason257c62e2009-10-13 13:21:08 -04007570 BTRFS_I(inode)->last_trans = root->fs_info->generation;
7571 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06007572 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04007573
Josef Bacik2ac55d42010-02-03 19:33:23 +00007574 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04007575
7576out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02007577 if (!ret) {
7578 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04007579 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02007580 }
Chris Mason9ebefb182007-06-15 13:50:00 -04007581 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05007582out:
Josef Bacikec39e182012-01-12 19:10:12 -05007583 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007584out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02007585 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04007586 return ret;
7587}
7588
Josef Bacika41ad392011-01-31 15:30:16 -05007589static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04007590{
7591 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04007592 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04007593 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05007594 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007595 struct btrfs_trans_handle *trans;
Chris Masondbe674a2008-07-17 12:54:05 -04007596 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04007597 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04007598
Josef Bacik0ef8b722013-10-25 16:13:35 -04007599 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
7600 (u64)-1);
7601 if (ret)
7602 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04007603
Josef Bacikfcb80c22011-05-03 10:40:22 -04007604 /*
7605 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
7606 * 3 things going on here
7607 *
7608 * 1) We need to reserve space for our orphan item and the space to
7609 * delete our orphan item. Lord knows we don't want to have a dangling
7610 * orphan item because we didn't reserve space to remove it.
7611 *
7612 * 2) We need to reserve space to update our inode.
7613 *
7614 * 3) We need to have something to cache all the space that is going to
7615 * be free'd up by the truncate operation, but also have some slack
7616 * space reserved in case it uses space during the truncate (thank you
7617 * very much snapshotting).
7618 *
7619 * And we need these to all be seperate. The fact is we can use alot of
7620 * space doing the truncate, and we have no earthly idea how much space
7621 * we will use, so we need the truncate reservation to be seperate so it
7622 * doesn't end up using space reserved for updating the inode or
7623 * removing the orphan item. We also need to be able to stop the
7624 * transaction and start a new one, which means we need to be able to
7625 * update the inode several times, and we have no idea of knowing how
7626 * many times that will be, so we can't just reserve 1 item for the
7627 * entirety of the opration, so that has to be done seperately as well.
7628 * Then there is the orphan item, which does indeed need to be held on
7629 * to for the whole operation, and we need nobody to touch this reserved
7630 * space except the orphan code.
7631 *
7632 * So that leaves us with
7633 *
7634 * 1) root->orphan_block_rsv - for the orphan deletion.
7635 * 2) rsv - for the truncate reservation, which we will steal from the
7636 * transaction reservation.
7637 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
7638 * updating the inode.
7639 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06007640 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007641 if (!rsv)
7642 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04007643 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04007644 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05007645
Josef Bacik907cbce2011-08-08 13:46:15 -04007646 /*
Josef Bacik07127182011-08-19 10:29:59 -04007647 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04007648 * 1 for updating the inode.
7649 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05007650 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007651 if (IS_ERR(trans)) {
7652 err = PTR_ERR(trans);
7653 goto out;
7654 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05007655
Josef Bacik907cbce2011-08-08 13:46:15 -04007656 /* Migrate the slack space for the truncate to our reserve */
7657 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
7658 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007659 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05007660
Chris Mason5a3f23d2009-03-31 13:27:11 -04007661 /*
7662 * setattr is responsible for setting the ordered_data_close flag,
7663 * but that is only tested during the last file release. That
7664 * could happen well after the next commit, leaving a great big
7665 * window where new writes may get lost if someone chooses to write
7666 * to this file after truncating to zero
7667 *
7668 * The inode doesn't have any dirty data here, and so if we commit
7669 * this is a noop. If someone immediately starts writing to the inode
7670 * it is very likely we'll catch some of their writes in this
7671 * transaction, and the commit will find this file on the ordered
7672 * data list with good things to send down.
7673 *
7674 * This is a best effort solution, there is still a window where
7675 * using truncate to replace the contents of the file will
7676 * end up with a zero length file after a crash.
7677 */
Josef Bacik72ac3c02012-05-23 14:13:11 -04007678 if (inode->i_size == 0 && test_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
7679 &BTRFS_I(inode)->runtime_flags))
Chris Mason5a3f23d2009-03-31 13:27:11 -04007680 btrfs_add_ordered_operation(trans, root, inode);
7681
Josef Bacik5dc562c2012-08-17 13:14:17 -04007682 /*
7683 * So if we truncate and then write and fsync we normally would just
7684 * write the extents that changed, which is a problem if we need to
7685 * first truncate that entire inode. So set this flag so we write out
7686 * all of the extents in the inode to the sync log so we're completely
7687 * safe.
7688 */
7689 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007690 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04007691
Yan, Zheng80825102009-11-12 09:35:36 +00007692 while (1) {
7693 ret = btrfs_truncate_inode_items(trans, root, inode,
7694 inode->i_size,
7695 BTRFS_EXTENT_DATA_KEY);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007696 if (ret != -ENOSPC) {
Josef Bacik3893e332011-01-31 16:03:11 -05007697 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007698 break;
Josef Bacik3893e332011-01-31 16:03:11 -05007699 }
Chris Mason39279cc2007-06-12 06:35:45 -04007700
Josef Bacikfcb80c22011-05-03 10:40:22 -04007701 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007702 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007703 if (ret) {
7704 err = ret;
7705 break;
7706 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04007707
Yan, Zheng80825102009-11-12 09:35:36 +00007708 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007709 btrfs_btree_balance_dirty(root);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007710
7711 trans = btrfs_start_transaction(root, 2);
7712 if (IS_ERR(trans)) {
7713 ret = err = PTR_ERR(trans);
7714 trans = NULL;
7715 break;
7716 }
7717
7718 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
7719 rsv, min_size);
7720 BUG_ON(ret); /* shouldn't happen */
7721 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007722 }
7723
7724 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04007725 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007726 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007727 if (ret)
7728 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007729 }
7730
Chris Mason917c16b2011-11-08 14:49:59 -05007731 if (trans) {
7732 trans->block_rsv = &root->fs_info->trans_block_rsv;
7733 ret = btrfs_update_inode(trans, root, inode);
7734 if (ret && !err)
7735 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04007736
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007737 ret = btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007738 btrfs_btree_balance_dirty(root);
Chris Mason917c16b2011-11-08 14:49:59 -05007739 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04007740
7741out:
7742 btrfs_free_block_rsv(root, rsv);
7743
Josef Bacik3893e332011-01-31 16:03:11 -05007744 if (ret && !err)
7745 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05007746
Josef Bacik3893e332011-01-31 16:03:11 -05007747 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007748}
7749
Sven Wegener3b963622008-06-09 21:57:42 -04007750/*
Chris Masond352ac62008-09-29 15:18:18 -04007751 * create a new subvolume directory/inode (helper for the ioctl).
7752 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05007753int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04007754 struct btrfs_root *new_root, u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04007755{
Chris Mason39279cc2007-06-12 06:35:45 -04007756 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04007757 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007758 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007759
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01007760 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
7761 new_dirid, new_dirid,
7762 S_IFDIR | (~current_umask() & S_IRWXUGO),
7763 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04007764 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04007765 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007766 inode->i_op = &btrfs_dir_inode_operations;
7767 inode->i_fop = &btrfs_dir_file_operations;
7768
Miklos Szeredibfe86842011-10-28 14:13:29 +02007769 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04007770 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04007771
Yan, Zheng76dda932009-09-21 16:00:26 -04007772 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04007773
Yan, Zheng76dda932009-09-21 16:00:26 -04007774 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07007775 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007776}
7777
Chris Mason39279cc2007-06-12 06:35:45 -04007778struct inode *btrfs_alloc_inode(struct super_block *sb)
7779{
7780 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007781 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007782
7783 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
7784 if (!ei)
7785 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007786
7787 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007788 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04007789 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04007790 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04007791 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007792 ei->delalloc_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007793 ei->disk_i_size = 0;
7794 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04007795 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007796 ei->index_cnt = (u64)-1;
7797 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06007798 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007799
Josef Bacik9e0baf62011-07-15 15:16:44 +00007800 spin_lock_init(&ei->lock);
7801 ei->outstanding_extents = 0;
7802 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007803
Josef Bacik72ac3c02012-05-23 14:13:11 -04007804 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08007805 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007806
Miao Xie16cdcec2011-04-22 18:12:22 +08007807 ei->delayed_node = NULL;
7808
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007809 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02007810 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02007811 extent_io_tree_init(&ei->io_tree, &inode->i_data);
7812 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04007813 ei->io_tree.track_uptodate = 1;
7814 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05007815 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007816 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05007817 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007818 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007819 INIT_LIST_HEAD(&ei->delalloc_inodes);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007820 INIT_LIST_HEAD(&ei->ordered_operations);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007821 RB_CLEAR_NODE(&ei->rb_node);
7822
7823 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007824}
7825
Josef Bacikaaedb552013-10-11 14:44:09 -04007826#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
7827void btrfs_test_destroy_inode(struct inode *inode)
7828{
7829 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
7830 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
7831}
7832#endif
7833
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007834static void btrfs_i_callback(struct rcu_head *head)
7835{
7836 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007837 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
7838}
7839
Chris Mason39279cc2007-06-12 06:35:45 -04007840void btrfs_destroy_inode(struct inode *inode)
7841{
Chris Masone6dcd2d2008-07-17 12:53:50 -04007842 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007843 struct btrfs_root *root = BTRFS_I(inode)->root;
7844
Al Virob3d9b7a2012-06-09 13:51:19 -04007845 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04007846 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00007847 WARN_ON(BTRFS_I(inode)->outstanding_extents);
7848 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04007849 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
7850 WARN_ON(BTRFS_I(inode)->csum_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04007851
Chris Mason5a3f23d2009-03-31 13:27:11 -04007852 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05007853 * This can happen where we create an inode, but somebody else also
7854 * created the same inode and we need to destroy the one we already
7855 * created.
7856 */
7857 if (!root)
7858 goto free;
7859
7860 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04007861 * Make sure we're properly removed from the ordered operation
7862 * lists.
7863 */
7864 smp_mb();
7865 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
Miao Xie199c2a92013-05-15 07:48:23 +00007866 spin_lock(&root->fs_info->ordered_root_lock);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007867 list_del_init(&BTRFS_I(inode)->ordered_operations);
Miao Xie199c2a92013-05-15 07:48:23 +00007868 spin_unlock(&root->fs_info->ordered_root_lock);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007869 }
7870
Josef Bacik8a35d952012-05-23 14:26:42 -04007871 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
7872 &BTRFS_I(inode)->runtime_flags)) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00007873 btrfs_info(root->fs_info, "inode %llu still on the orphan list",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02007874 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04007875 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04007876 }
Josef Bacik7b128762008-07-24 12:17:14 -04007877
Chris Masond3977122009-01-05 21:25:51 -05007878 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04007879 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
7880 if (!ordered)
7881 break;
7882 else {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00007883 btrfs_err(root->fs_info, "found ordered extent %llu %llu on inode cleanup",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02007884 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007885 btrfs_remove_ordered_extent(inode, ordered);
7886 btrfs_put_ordered_extent(ordered);
7887 btrfs_put_ordered_extent(ordered);
7888 }
7889 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007890 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007891 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05007892free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007893 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04007894}
7895
Al Viro45321ac2010-06-07 13:43:19 -04007896int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04007897{
7898 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04007899
Naohiro Aota6379ef92013-06-06 09:56:34 +00007900 if (root == NULL)
7901 return 1;
7902
Liu Bofa6ac872013-02-20 14:10:23 +00007903 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02007904 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04007905 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04007906 else
Al Viro45321ac2010-06-07 13:43:19 -04007907 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04007908}
7909
Sven Wegener0ee0fda2008-07-30 16:54:26 -04007910static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04007911{
7912 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
7913
7914 inode_init_once(&ei->vfs_inode);
7915}
7916
7917void btrfs_destroy_cachep(void)
7918{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10007919 /*
7920 * Make sure all delayed rcu free inodes are flushed before we
7921 * destroy cache.
7922 */
7923 rcu_barrier();
Chris Mason39279cc2007-06-12 06:35:45 -04007924 if (btrfs_inode_cachep)
7925 kmem_cache_destroy(btrfs_inode_cachep);
7926 if (btrfs_trans_handle_cachep)
7927 kmem_cache_destroy(btrfs_trans_handle_cachep);
7928 if (btrfs_transaction_cachep)
7929 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04007930 if (btrfs_path_cachep)
7931 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05007932 if (btrfs_free_space_cachep)
7933 kmem_cache_destroy(btrfs_free_space_cachep);
Miao Xie8ccf6f192012-10-25 09:28:04 +00007934 if (btrfs_delalloc_work_cachep)
7935 kmem_cache_destroy(btrfs_delalloc_work_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04007936}
7937
7938int btrfs_init_cachep(void)
7939{
David Sterba837e1972012-09-07 03:00:48 -06007940 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007941 sizeof(struct btrfs_inode), 0,
7942 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04007943 if (!btrfs_inode_cachep)
7944 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007945
David Sterba837e1972012-09-07 03:00:48 -06007946 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007947 sizeof(struct btrfs_trans_handle), 0,
7948 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007949 if (!btrfs_trans_handle_cachep)
7950 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007951
David Sterba837e1972012-09-07 03:00:48 -06007952 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007953 sizeof(struct btrfs_transaction), 0,
7954 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007955 if (!btrfs_transaction_cachep)
7956 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007957
David Sterba837e1972012-09-07 03:00:48 -06007958 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007959 sizeof(struct btrfs_path), 0,
7960 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007961 if (!btrfs_path_cachep)
7962 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007963
David Sterba837e1972012-09-07 03:00:48 -06007964 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05007965 sizeof(struct btrfs_free_space), 0,
7966 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
7967 if (!btrfs_free_space_cachep)
7968 goto fail;
7969
Miao Xie8ccf6f192012-10-25 09:28:04 +00007970 btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
7971 sizeof(struct btrfs_delalloc_work), 0,
7972 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
7973 NULL);
7974 if (!btrfs_delalloc_work_cachep)
7975 goto fail;
7976
Chris Mason39279cc2007-06-12 06:35:45 -04007977 return 0;
7978fail:
7979 btrfs_destroy_cachep();
7980 return -ENOMEM;
7981}
7982
7983static int btrfs_getattr(struct vfsmount *mnt,
7984 struct dentry *dentry, struct kstat *stat)
7985{
Miao Xiedf0af1a2013-01-29 10:11:59 +00007986 u64 delalloc_bytes;
Chris Mason39279cc2007-06-12 06:35:45 -04007987 struct inode *inode = dentry->d_inode;
David Sterbafadc0d82011-11-20 07:33:38 -05007988 u32 blocksize = inode->i_sb->s_blocksize;
7989
Chris Mason39279cc2007-06-12 06:35:45 -04007990 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04007991 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05007992 stat->blksize = PAGE_CACHE_SIZE;
Miao Xiedf0af1a2013-01-29 10:11:59 +00007993
7994 spin_lock(&BTRFS_I(inode)->lock);
7995 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
7996 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05007997 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00007998 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04007999 return 0;
8000}
8001
Chris Masond3977122009-01-05 21:25:51 -05008002static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
8003 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04008004{
8005 struct btrfs_trans_handle *trans;
8006 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008007 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04008008 struct inode *new_inode = new_dentry->d_inode;
8009 struct inode *old_inode = old_dentry->d_inode;
8010 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008011 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008012 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04008013 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08008014 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008015
Li Zefan33345d012011-04-20 10:31:50 +08008016 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04008017 return -EPERM;
8018
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008019 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08008020 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05008021 return -EXDEV;
8022
Li Zefan33345d012011-04-20 10:31:50 +08008023 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
8024 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008025 return -ENOTEMPTY;
8026
Chris Mason39279cc2007-06-12 06:35:45 -04008027 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008028 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04008029 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05008030
8031
8032 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04008033 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05008034 new_dentry->d_name.name,
8035 new_dentry->d_name.len);
8036
8037 if (ret) {
8038 if (ret == -EEXIST) {
8039 /* we shouldn't get
8040 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05308041 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05008042 return ret;
8043 }
8044 } else {
8045 /* maybe -EOVERFLOW */
8046 return ret;
8047 }
8048 }
8049 ret = 0;
8050
Chris Mason5a3f23d2009-03-31 13:27:11 -04008051 /*
8052 * we're using rename to replace one file with another.
8053 * and the replacement file is large. Start IO on it now so
8054 * we don't add too much work to the end of the transaction
8055 */
Bartlomiej Zolnierkiewicz4baf8c92009-08-07 13:47:08 -04008056 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
Chris Mason5a3f23d2009-03-31 13:27:11 -04008057 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
8058 filemap_flush(old_inode->i_mapping);
8059
Yan, Zheng76dda932009-09-21 16:00:26 -04008060 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08008061 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008062 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008063 /*
8064 * We want to reserve the absolute worst case amount of items. So if
8065 * both inodes are subvols and we need to unlink them then that would
8066 * require 4 item modifications, but if they are both normal inodes it
8067 * would require 5 item modifications, so we'll assume their normal
8068 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
8069 * should cover the worst case number of items we'll modify.
8070 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04008071 trans = btrfs_start_transaction(root, 11);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008072 if (IS_ERR(trans)) {
8073 ret = PTR_ERR(trans);
8074 goto out_notrans;
8075 }
Chris Mason5f39d392007-10-15 16:14:19 -04008076
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008077 if (dest != root)
8078 btrfs_record_root_in_trans(trans, dest);
8079
Yan, Zhenga5719522009-09-24 09:17:31 -04008080 ret = btrfs_set_inode_index(new_dir, &index);
8081 if (ret)
8082 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008083
Li Zefan33345d012011-04-20 10:31:50 +08008084 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008085 /* force full log commit if subvolume involved. */
8086 root->fs_info->last_trans_log_full_commit = trans->transid;
8087 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04008088 ret = btrfs_insert_inode_ref(trans, dest,
8089 new_dentry->d_name.name,
8090 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08008091 old_ino,
8092 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04008093 if (ret)
8094 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008095 /*
8096 * this is an ugly little race, but the rename is required
8097 * to make sure that if we crash, the inode is either at the
8098 * old name or the new one. pinning the log transaction lets
8099 * us make sure we don't allow a log commit to come in after
8100 * we unlink the name but before we add the new name back in.
8101 */
8102 btrfs_pin_log_trans(root);
8103 }
Chris Mason12fcfd22009-03-24 10:24:20 -04008104 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04008105 * make sure the inode gets flushed if it is replacing
8106 * something.
8107 */
Li Zefan33345d012011-04-20 10:31:50 +08008108 if (new_inode && new_inode->i_size && S_ISREG(old_inode->i_mode))
Chris Mason5a3f23d2009-03-31 13:27:11 -04008109 btrfs_add_ordered_operation(trans, root, old_inode);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008110
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008111 inode_inc_iversion(old_dir);
8112 inode_inc_iversion(new_dir);
8113 inode_inc_iversion(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008114 old_dir->i_ctime = old_dir->i_mtime = ctime;
8115 new_dir->i_ctime = new_dir->i_mtime = ctime;
8116 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04008117
Chris Mason12fcfd22009-03-24 10:24:20 -04008118 if (old_dentry->d_parent != new_dentry->d_parent)
8119 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
8120
Li Zefan33345d012011-04-20 10:31:50 +08008121 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008122 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
8123 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
8124 old_dentry->d_name.name,
8125 old_dentry->d_name.len);
8126 } else {
Al Viro92986792011-03-04 17:14:37 +00008127 ret = __btrfs_unlink_inode(trans, root, old_dir,
8128 old_dentry->d_inode,
8129 old_dentry->d_name.name,
8130 old_dentry->d_name.len);
8131 if (!ret)
8132 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008133 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008134 if (ret) {
8135 btrfs_abort_transaction(trans, root, ret);
8136 goto out_fail;
8137 }
Chris Mason39279cc2007-06-12 06:35:45 -04008138
8139 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008140 inode_inc_iversion(new_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008141 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08008142 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008143 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
8144 root_objectid = BTRFS_I(new_inode)->location.objectid;
8145 ret = btrfs_unlink_subvol(trans, dest, new_dir,
8146 root_objectid,
8147 new_dentry->d_name.name,
8148 new_dentry->d_name.len);
8149 BUG_ON(new_inode->i_nlink == 0);
8150 } else {
8151 ret = btrfs_unlink_inode(trans, dest, new_dir,
8152 new_dentry->d_inode,
8153 new_dentry->d_name.name,
8154 new_dentry->d_name.len);
8155 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04008156 if (!ret && new_inode->i_nlink == 0)
Chris Masone02119d2008-09-05 16:13:11 -04008157 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008158 if (ret) {
8159 btrfs_abort_transaction(trans, root, ret);
8160 goto out_fail;
8161 }
Chris Mason39279cc2007-06-12 06:35:45 -04008162 }
Josef Bacikaec74772008-07-24 12:12:38 -04008163
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008164 ret = btrfs_add_link(trans, new_dir, old_inode,
8165 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04008166 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008167 if (ret) {
8168 btrfs_abort_transaction(trans, root, ret);
8169 goto out_fail;
8170 }
Chris Mason39279cc2007-06-12 06:35:45 -04008171
Li Zefan33345d012011-04-20 10:31:50 +08008172 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04008173 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00008174 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008175 btrfs_end_log_trans(root);
8176 }
Chris Mason39279cc2007-06-12 06:35:45 -04008177out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008178 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008179out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08008180 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008181 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008182
Chris Mason39279cc2007-06-12 06:35:45 -04008183 return ret;
8184}
8185
Miao Xie8ccf6f192012-10-25 09:28:04 +00008186static void btrfs_run_delalloc_work(struct btrfs_work *work)
8187{
8188 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04008189 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008190
8191 delalloc_work = container_of(work, struct btrfs_delalloc_work,
8192 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04008193 inode = delalloc_work->inode;
8194 if (delalloc_work->wait) {
8195 btrfs_wait_ordered_range(inode, 0, (u64)-1);
8196 } else {
8197 filemap_flush(inode->i_mapping);
8198 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8199 &BTRFS_I(inode)->runtime_flags))
8200 filemap_flush(inode->i_mapping);
8201 }
Miao Xie8ccf6f192012-10-25 09:28:04 +00008202
8203 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -04008204 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008205 else
Josef Bacik9f23e282013-10-28 15:03:41 -04008206 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008207 complete(&delalloc_work->completion);
8208}
8209
8210struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
8211 int wait, int delay_iput)
8212{
8213 struct btrfs_delalloc_work *work;
8214
8215 work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
8216 if (!work)
8217 return NULL;
8218
8219 init_completion(&work->completion);
8220 INIT_LIST_HEAD(&work->list);
8221 work->inode = inode;
8222 work->wait = wait;
8223 work->delay_iput = delay_iput;
8224 work->work.func = btrfs_run_delalloc_work;
8225
8226 return work;
8227}
8228
8229void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
8230{
8231 wait_for_completion(&work->completion);
8232 kmem_cache_free(btrfs_delalloc_work_cachep, work);
8233}
8234
Chris Masond352ac62008-09-29 15:18:18 -04008235/*
8236 * some fairly slow code that needs optimization. This walks the list
8237 * of all the inodes with pending delalloc and forces them to disk.
8238 */
Miao Xieeb73c1b2013-05-15 07:48:22 +00008239static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
Chris Masonea8c2812008-08-04 23:17:27 -04008240{
Chris Masonea8c2812008-08-04 23:17:27 -04008241 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008242 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008243 struct btrfs_delalloc_work *work, *next;
8244 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00008245 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008246 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04008247
Miao Xie8ccf6f192012-10-25 09:28:04 +00008248 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008249 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00008250
Miao Xieeb73c1b2013-05-15 07:48:22 +00008251 spin_lock(&root->delalloc_lock);
8252 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008253 while (!list_empty(&splice)) {
8254 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04008255 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008256
Miao Xieeb73c1b2013-05-15 07:48:22 +00008257 list_move_tail(&binode->delalloc_inodes,
8258 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008259 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00008260 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00008261 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008262 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008263 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008264 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008265
8266 work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
8267 if (unlikely(!work)) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -04008268 if (delay_iput)
8269 btrfs_add_delayed_iput(inode);
8270 else
8271 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008272 ret = -ENOMEM;
8273 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008274 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008275 list_add_tail(&work->list, &works);
8276 btrfs_queue_worker(&root->fs_info->flush_workers,
8277 &work->work);
8278
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008279 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00008280 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04008281 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008282 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04008283
Miao Xie1eafa6c2013-01-22 10:49:00 +00008284 list_for_each_entry_safe(work, next, &works, list) {
8285 list_del_init(&work->list);
8286 btrfs_wait_and_free_delalloc_work(work);
8287 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008288 return 0;
8289out:
8290 list_for_each_entry_safe(work, next, &works, list) {
8291 list_del_init(&work->list);
8292 btrfs_wait_and_free_delalloc_work(work);
8293 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008294
Miao Xieeb73c1b2013-05-15 07:48:22 +00008295 if (!list_empty_careful(&splice)) {
8296 spin_lock(&root->delalloc_lock);
8297 list_splice_tail(&splice, &root->delalloc_inodes);
8298 spin_unlock(&root->delalloc_lock);
8299 }
8300 return ret;
8301}
8302
8303int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
8304{
8305 int ret;
8306
8307 if (root->fs_info->sb->s_flags & MS_RDONLY)
8308 return -EROFS;
8309
8310 ret = __start_delalloc_inodes(root, delay_iput);
8311 /*
8312 * the filemap_flush will queue IO into the worker threads, but
Chris Mason8c8bee12008-09-29 11:19:10 -04008313 * we have to make sure the IO is actually started and that
8314 * ordered extents get created before we return
8315 */
8316 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05008317 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05008318 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04008319 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05008320 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
8321 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04008322 }
8323 atomic_dec(&root->fs_info->async_submit_draining);
Miao Xieeb73c1b2013-05-15 07:48:22 +00008324 return ret;
8325}
8326
Miao Xie91aef862013-11-04 23:13:26 +08008327int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput)
Miao Xieeb73c1b2013-05-15 07:48:22 +00008328{
8329 struct btrfs_root *root;
8330 struct list_head splice;
8331 int ret;
8332
8333 if (fs_info->sb->s_flags & MS_RDONLY)
8334 return -EROFS;
8335
8336 INIT_LIST_HEAD(&splice);
8337
8338 spin_lock(&fs_info->delalloc_root_lock);
8339 list_splice_init(&fs_info->delalloc_roots, &splice);
8340 while (!list_empty(&splice)) {
8341 root = list_first_entry(&splice, struct btrfs_root,
8342 delalloc_root);
8343 root = btrfs_grab_fs_root(root);
8344 BUG_ON(!root);
8345 list_move_tail(&root->delalloc_root,
8346 &fs_info->delalloc_roots);
8347 spin_unlock(&fs_info->delalloc_root_lock);
8348
8349 ret = __start_delalloc_inodes(root, delay_iput);
8350 btrfs_put_fs_root(root);
8351 if (ret)
8352 goto out;
8353
8354 spin_lock(&fs_info->delalloc_root_lock);
8355 }
8356 spin_unlock(&fs_info->delalloc_root_lock);
8357
8358 atomic_inc(&fs_info->async_submit_draining);
8359 while (atomic_read(&fs_info->nr_async_submits) ||
8360 atomic_read(&fs_info->async_delalloc_pages)) {
8361 wait_event(fs_info->async_submit_wait,
8362 (atomic_read(&fs_info->nr_async_submits) == 0 &&
8363 atomic_read(&fs_info->async_delalloc_pages) == 0));
8364 }
8365 atomic_dec(&fs_info->async_submit_draining);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008366 return 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008367out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00008368 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00008369 spin_lock(&fs_info->delalloc_root_lock);
8370 list_splice_tail(&splice, &fs_info->delalloc_roots);
8371 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008372 }
Miao Xie8ccf6f192012-10-25 09:28:04 +00008373 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04008374}
8375
Chris Mason39279cc2007-06-12 06:35:45 -04008376static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
8377 const char *symname)
8378{
8379 struct btrfs_trans_handle *trans;
8380 struct btrfs_root *root = BTRFS_I(dir)->root;
8381 struct btrfs_path *path;
8382 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05008383 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04008384 int err;
8385 int drop_inode = 0;
8386 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05308387 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008388 int name_len;
8389 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04008390 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04008391 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04008392 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04008393
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01008394 name_len = strlen(symname);
Chris Mason39279cc2007-06-12 06:35:45 -04008395 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
8396 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05008397
Josef Bacik9ed74f22009-09-11 16:12:44 -04008398 /*
8399 * 2 items for inode item and ref
8400 * 2 items for dir items
8401 * 1 item for xattr if selinux is on
8402 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04008403 trans = btrfs_start_transaction(root, 5);
8404 if (IS_ERR(trans))
8405 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05008406
Li Zefan581bb052011-04-20 10:06:11 +08008407 err = btrfs_find_free_ino(root, &objectid);
8408 if (err)
8409 goto out_unlock;
8410
Josef Bacikaec74772008-07-24 12:12:38 -04008411 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08008412 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04008413 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008414 if (IS_ERR(inode)) {
8415 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008416 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008417 }
Chris Mason39279cc2007-06-12 06:35:45 -04008418
Eric Paris2a7dba32011-02-01 11:05:39 -05008419 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04008420 if (err) {
8421 drop_inode = 1;
8422 goto out_unlock;
8423 }
8424
Casey Schauflerad19db72011-12-15 10:09:07 -05008425 /*
8426 * If the active LSM wants to access the inode during
8427 * d_instantiate it needs these. Smack checks to see
8428 * if the filesystem supports xattrs by looking at the
8429 * ops vector.
8430 */
8431 inode->i_fop = &btrfs_file_operations;
8432 inode->i_op = &btrfs_file_inode_operations;
8433
Josef Bacika1b075d2010-11-19 20:36:11 +00008434 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04008435 if (err)
8436 drop_inode = 1;
8437 else {
8438 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04008439 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05008440 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04008441 }
Chris Mason39279cc2007-06-12 06:35:45 -04008442 if (drop_inode)
8443 goto out_unlock;
8444
8445 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07008446 if (!path) {
8447 err = -ENOMEM;
8448 drop_inode = 1;
8449 goto out_unlock;
8450 }
Li Zefan33345d012011-04-20 10:31:50 +08008451 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008452 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008453 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
8454 datasize = btrfs_file_extent_calc_inline_size(name_len);
8455 err = btrfs_insert_empty_item(trans, root, path, &key,
8456 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04008457 if (err) {
8458 drop_inode = 1;
Julia Lawallb0839162011-05-14 07:10:51 +00008459 btrfs_free_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04008460 goto out_unlock;
8461 }
Chris Mason5f39d392007-10-15 16:14:19 -04008462 leaf = path->nodes[0];
8463 ei = btrfs_item_ptr(leaf, path->slots[0],
8464 struct btrfs_file_extent_item);
8465 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
8466 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04008467 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04008468 btrfs_set_file_extent_encryption(leaf, ei, 0);
8469 btrfs_set_file_extent_compression(leaf, ei, 0);
8470 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
8471 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
8472
Chris Mason39279cc2007-06-12 06:35:45 -04008473 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04008474 write_extent_buffer(leaf, symname, ptr, name_len);
8475 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04008476 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04008477
Chris Mason39279cc2007-06-12 06:35:45 -04008478 inode->i_op = &btrfs_symlink_inode_operations;
8479 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04008480 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04008481 inode_set_bytes(inode, name_len);
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01008482 btrfs_i_size_write(inode, name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -04008483 err = btrfs_update_inode(trans, root, inode);
8484 if (err)
8485 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04008486
8487out_unlock:
Al Viro08c422c2011-12-23 07:58:13 -05008488 if (!err)
8489 d_instantiate(dentry, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008490 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04008491 if (drop_inode) {
8492 inode_dec_link_count(inode);
8493 iput(inode);
8494 }
Liu Bob53d3f52012-11-14 14:34:34 +00008495 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04008496 return err;
8497}
Chris Mason16432982008-04-10 10:23:21 -04008498
Josef Bacik0af3d002010-06-21 14:48:16 -04008499static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
8500 u64 start, u64 num_bytes, u64 min_size,
8501 loff_t actual_len, u64 *alloc_hint,
8502 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04008503{
Josef Bacik5dc562c2012-08-17 13:14:17 -04008504 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
8505 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04008506 struct btrfs_root *root = BTRFS_I(inode)->root;
8507 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04008508 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00008509 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05008510 u64 cur_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04008511 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04008512 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04008513
Josef Bacik0af3d002010-06-21 14:48:16 -04008514 if (trans)
8515 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04008516 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008517 if (own_trans) {
8518 trans = btrfs_start_transaction(root, 3);
8519 if (IS_ERR(trans)) {
8520 ret = PTR_ERR(trans);
8521 break;
8522 }
Yan Zhengd899e052008-10-30 14:25:28 -04008523 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00008524
Chris Mason154ea282013-03-05 11:11:26 -05008525 cur_bytes = min(num_bytes, 256ULL * 1024 * 1024);
8526 cur_bytes = max(cur_bytes, min_size);
Josef Bacik00361582013-08-14 14:02:47 -04008527 ret = btrfs_reserve_extent(root, cur_bytes, min_size, 0,
8528 *alloc_hint, &ins, 1);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008529 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008530 if (own_trans)
8531 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008532 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00008533 }
8534
Yan Zhengd899e052008-10-30 14:25:28 -04008535 ret = insert_reserved_file_extent(trans, inode,
8536 cur_offset, ins.objectid,
8537 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00008538 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04008539 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008540 if (ret) {
Josef Bacik857cc2f2013-10-07 15:21:08 -04008541 btrfs_free_reserved_extent(root, ins.objectid,
8542 ins.offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008543 btrfs_abort_transaction(trans, root, ret);
8544 if (own_trans)
8545 btrfs_end_transaction(trans, root);
8546 break;
8547 }
Chris Masona1ed8352009-09-11 12:27:37 -04008548 btrfs_drop_extent_cache(inode, cur_offset,
8549 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008550
Josef Bacik5dc562c2012-08-17 13:14:17 -04008551 em = alloc_extent_map();
8552 if (!em) {
8553 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
8554 &BTRFS_I(inode)->runtime_flags);
8555 goto next;
8556 }
8557
8558 em->start = cur_offset;
8559 em->orig_start = cur_offset;
8560 em->len = ins.offset;
8561 em->block_start = ins.objectid;
8562 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05008563 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04008564 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008565 em->bdev = root->fs_info->fs_devices->latest_bdev;
8566 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
8567 em->generation = trans->transid;
8568
8569 while (1) {
8570 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04008571 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04008572 write_unlock(&em_tree->lock);
8573 if (ret != -EEXIST)
8574 break;
8575 btrfs_drop_extent_cache(inode, cur_offset,
8576 cur_offset + ins.offset - 1,
8577 0);
8578 }
8579 free_extent_map(em);
8580next:
Yan Zhengd899e052008-10-30 14:25:28 -04008581 num_bytes -= ins.offset;
8582 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04008583 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00008584
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008585 inode_inc_iversion(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04008586 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02008587 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04008588 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04008589 (actual_len > inode->i_size) &&
8590 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00008591 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00008592 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00008593 else
Josef Bacik55a61d12010-11-22 18:50:32 +00008594 i_size = cur_offset;
8595 i_size_write(inode, i_size);
8596 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008597 }
8598
Yan Zhengd899e052008-10-30 14:25:28 -04008599 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008600
8601 if (ret) {
8602 btrfs_abort_transaction(trans, root, ret);
8603 if (own_trans)
8604 btrfs_end_transaction(trans, root);
8605 break;
8606 }
Yan Zhengd899e052008-10-30 14:25:28 -04008607
Josef Bacik0af3d002010-06-21 14:48:16 -04008608 if (own_trans)
8609 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008610 }
Yan Zhengd899e052008-10-30 14:25:28 -04008611 return ret;
8612}
8613
Josef Bacik0af3d002010-06-21 14:48:16 -04008614int btrfs_prealloc_file_range(struct inode *inode, int mode,
8615 u64 start, u64 num_bytes, u64 min_size,
8616 loff_t actual_len, u64 *alloc_hint)
8617{
8618 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
8619 min_size, actual_len, alloc_hint,
8620 NULL);
8621}
8622
8623int btrfs_prealloc_file_range_trans(struct inode *inode,
8624 struct btrfs_trans_handle *trans, int mode,
8625 u64 start, u64 num_bytes, u64 min_size,
8626 loff_t actual_len, u64 *alloc_hint)
8627{
8628 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
8629 min_size, actual_len, alloc_hint, trans);
8630}
8631
Chris Masone6dcd2d2008-07-17 12:53:50 -04008632static int btrfs_set_page_dirty(struct page *page)
8633{
Chris Masone6dcd2d2008-07-17 12:53:50 -04008634 return __set_page_dirty_nobuffers(page);
8635}
8636
Al Viro10556cb22011-06-20 19:28:19 -04008637static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05008638{
Li Zefanb83cc962010-12-20 16:04:08 +08008639 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00008640 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08008641
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00008642 if (mask & MAY_WRITE &&
8643 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
8644 if (btrfs_root_readonly(root))
8645 return -EROFS;
8646 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
8647 return -EACCES;
8648 }
Al Viro2830ba72011-06-20 19:16:29 -04008649 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05008650}
Chris Mason39279cc2007-06-12 06:35:45 -04008651
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008652static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05008653 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04008654 .lookup = btrfs_lookup,
8655 .create = btrfs_create,
8656 .unlink = btrfs_unlink,
8657 .link = btrfs_link,
8658 .mkdir = btrfs_mkdir,
8659 .rmdir = btrfs_rmdir,
8660 .rename = btrfs_rename,
8661 .symlink = btrfs_symlink,
8662 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04008663 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008664 .setxattr = btrfs_setxattr,
8665 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05008666 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008667 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05008668 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008669 .get_acl = btrfs_get_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -07008670 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008671};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008672static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008673 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05008674 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008675 .get_acl = btrfs_get_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -07008676 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008677};
Yan, Zheng76dda932009-09-21 16:00:26 -04008678
Alexey Dobriyan828c0952009-10-01 15:43:56 -07008679static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008680 .llseek = generic_file_llseek,
8681 .read = generic_read_dir,
Al Viro9cdda8d2013-05-22 16:48:09 -04008682 .iterate = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04008683 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04008684#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04008685 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04008686#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04008687 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04008688 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04008689};
8690
Chris Masond1310b22008-01-24 16:13:08 -05008691static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04008692 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05008693 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04008694 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04008695 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008696 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04008697 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05008698 .set_bit_hook = btrfs_set_bit_hook,
8699 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04008700 .merge_extent_hook = btrfs_merge_extent_hook,
8701 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04008702};
8703
Chris Mason35054392009-01-21 13:11:13 -05008704/*
8705 * btrfs doesn't support the bmap operation because swapfiles
8706 * use bmap to make a mapping of extents in the file. They assume
8707 * these extents won't change over the life of the file and they
8708 * use the bmap result to do IO directly to the drive.
8709 *
8710 * the btrfs bmap call would return logical addresses that aren't
8711 * suitable for IO and they also will change frequently as COW
8712 * operations happen. So, swapfile + btrfs == corruption.
8713 *
8714 * For now we're avoiding this by dropping bmap.
8715 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008716static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008717 .readpage = btrfs_readpage,
8718 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -04008719 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05008720 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04008721 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04008722 .invalidatepage = btrfs_invalidatepage,
8723 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008724 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02008725 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04008726};
8727
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008728static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008729 .readpage = btrfs_readpage,
8730 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04008731 .invalidatepage = btrfs_invalidatepage,
8732 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04008733};
8734
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008735static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008736 .getattr = btrfs_getattr,
8737 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008738 .setxattr = btrfs_setxattr,
8739 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05008740 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008741 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05008742 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008743 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008744 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008745 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008746};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008747static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04008748 .getattr = btrfs_getattr,
8749 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008750 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008751 .setxattr = btrfs_setxattr,
8752 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04008753 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008754 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008755 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008756 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -04008757};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008758static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008759 .readlink = generic_readlink,
8760 .follow_link = page_follow_link_light,
8761 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00008762 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -05008763 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008764 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05008765 .setxattr = btrfs_setxattr,
8766 .getxattr = btrfs_getxattr,
8767 .listxattr = btrfs_listxattr,
8768 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008769 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008770 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008771};
Yan, Zheng76dda932009-09-21 16:00:26 -04008772
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04008773const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04008774 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -04008775 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -04008776};