blob: 23f18eb5fb5588c9df13cbb42bbc42ff5347fd1a [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,
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000128 struct btrfs_path *path, int extent_inserted,
Chris Masonc8b97812008-10-29 14:49:59 -0400129 struct btrfs_root *root, struct inode *inode,
130 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000131 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400132 struct page **compressed_pages)
133{
Chris Masonc8b97812008-10-29 14:49:59 -0400134 struct extent_buffer *leaf;
135 struct page *page = NULL;
136 char *kaddr;
137 unsigned long ptr;
138 struct btrfs_file_extent_item *ei;
139 int err = 0;
140 int ret;
141 size_t cur_size = size;
Chris Masonc8b97812008-10-29 14:49:59 -0400142 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400143
Li Zefanfe3f5662011-03-28 08:30:38 +0000144 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400145 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400146
Chris Masonc8b97812008-10-29 14:49:59 -0400147 inode_add_bytes(inode, size);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000148
149 if (!extent_inserted) {
150 struct btrfs_key key;
151 size_t datasize;
152
153 key.objectid = btrfs_ino(inode);
154 key.offset = start;
155 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
156
157 datasize = btrfs_file_extent_calc_inline_size(cur_size);
158 path->leave_spinning = 1;
159 ret = btrfs_insert_empty_item(trans, root, path, &key,
160 datasize);
161 if (ret) {
162 err = ret;
163 goto fail;
164 }
Chris Masonc8b97812008-10-29 14:49:59 -0400165 }
166 leaf = path->nodes[0];
167 ei = btrfs_item_ptr(leaf, path->slots[0],
168 struct btrfs_file_extent_item);
169 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
170 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
171 btrfs_set_file_extent_encryption(leaf, ei, 0);
172 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
173 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
174 ptr = btrfs_file_extent_inline_start(ei);
175
Li Zefan261507a02010-12-17 14:21:50 +0800176 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400177 struct page *cpage;
178 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500179 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400180 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500181 cur_size = min_t(unsigned long, compressed_size,
Chris Masonc8b97812008-10-29 14:49:59 -0400182 PAGE_CACHE_SIZE);
183
Cong Wang7ac687d2011-11-25 23:14:28 +0800184 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400185 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800186 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400187
188 i++;
189 ptr += cur_size;
190 compressed_size -= cur_size;
191 }
192 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800193 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400194 } else {
195 page = find_get_page(inode->i_mapping,
196 start >> PAGE_CACHE_SHIFT);
197 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800198 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400199 offset = start & (PAGE_CACHE_SIZE - 1);
200 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800201 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400202 page_cache_release(page);
203 }
204 btrfs_mark_buffer_dirty(leaf);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000205 btrfs_release_path(path);
Chris Masonc8b97812008-10-29 14:49:59 -0400206
Yan, Zhengc2167752009-11-12 09:34:21 +0000207 /*
208 * we're an inline extent, so nobody can
209 * extend the file past i_size without locking
210 * a page we already have locked.
211 *
212 * We must do any isize and inode updates
213 * before we unlock the pages. Otherwise we
214 * could end up racing with unlink.
215 */
Chris Masonc8b97812008-10-29 14:49:59 -0400216 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100217 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000218
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100219 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400220fail:
Chris Masonc8b97812008-10-29 14:49:59 -0400221 return err;
222}
223
224
225/*
226 * conditionally insert an inline extent into the file. This
227 * does the checks required to make sure the data is small enough
228 * to fit as an inline extent.
229 */
Josef Bacik00361582013-08-14 14:02:47 -0400230static noinline int cow_file_range_inline(struct btrfs_root *root,
231 struct inode *inode, u64 start,
232 u64 end, size_t compressed_size,
233 int compress_type,
234 struct page **compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400235{
Josef Bacik00361582013-08-14 14:02:47 -0400236 struct btrfs_trans_handle *trans;
Chris Masonc8b97812008-10-29 14:49:59 -0400237 u64 isize = i_size_read(inode);
238 u64 actual_end = min(end + 1, isize);
239 u64 inline_len = actual_end - start;
Qu Wenruofda28322013-02-26 08:10:22 +0000240 u64 aligned_end = ALIGN(end, root->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400241 u64 data_len = inline_len;
242 int ret;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000243 struct btrfs_path *path;
244 int extent_inserted = 0;
245 u32 extent_item_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400246
247 if (compressed_size)
248 data_len = compressed_size;
249
250 if (start > 0 ||
Chris Mason70b99e62008-10-31 12:46:39 -0400251 actual_end >= PAGE_CACHE_SIZE ||
Chris Masonc8b97812008-10-29 14:49:59 -0400252 data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
253 (!compressed_size &&
254 (actual_end & (root->sectorsize - 1)) == 0) ||
255 end + 1 < isize ||
256 data_len > root->fs_info->max_inline) {
257 return 1;
258 }
259
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000260 path = btrfs_alloc_path();
261 if (!path)
262 return -ENOMEM;
263
Josef Bacik00361582013-08-14 14:02:47 -0400264 trans = btrfs_join_transaction(root);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000265 if (IS_ERR(trans)) {
266 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400267 return PTR_ERR(trans);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000268 }
Josef Bacik00361582013-08-14 14:02:47 -0400269 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
270
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000271 if (compressed_size && compressed_pages)
272 extent_item_size = btrfs_file_extent_calc_inline_size(
273 compressed_size);
274 else
275 extent_item_size = btrfs_file_extent_calc_inline_size(
276 inline_len);
277
278 ret = __btrfs_drop_extents(trans, root, inode, path,
279 start, aligned_end, NULL,
280 1, 1, extent_item_size, &extent_inserted);
Josef Bacik00361582013-08-14 14:02:47 -0400281 if (ret) {
282 btrfs_abort_transaction(trans, root, ret);
283 goto out;
284 }
Chris Masonc8b97812008-10-29 14:49:59 -0400285
286 if (isize > actual_end)
287 inline_len = min_t(u64, isize, actual_end);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000288 ret = insert_inline_extent(trans, path, extent_inserted,
289 root, inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400290 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000291 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400292 if (ret && ret != -ENOSPC) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100293 btrfs_abort_transaction(trans, root, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400294 goto out;
Josef Bacik2adcac12012-05-23 16:10:14 -0400295 } else if (ret == -ENOSPC) {
Josef Bacik00361582013-08-14 14:02:47 -0400296 ret = 1;
297 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100298 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400299
Josef Bacikbdc20e62013-02-28 13:23:38 -0500300 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400301 btrfs_delalloc_release_metadata(inode, end + 1 - start);
Chris Masona1ed8352009-09-11 12:27:37 -0400302 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
Josef Bacik00361582013-08-14 14:02:47 -0400303out:
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000304 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400305 btrfs_end_transaction(trans, root);
306 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400307}
308
Chris Mason771ed682008-11-06 22:02:51 -0500309struct async_extent {
310 u64 start;
311 u64 ram_size;
312 u64 compressed_size;
313 struct page **pages;
314 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800315 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500316 struct list_head list;
317};
318
319struct async_cow {
320 struct inode *inode;
321 struct btrfs_root *root;
322 struct page *locked_page;
323 u64 start;
324 u64 end;
325 struct list_head extents;
326 struct btrfs_work work;
327};
328
329static noinline int add_async_extent(struct async_cow *cow,
330 u64 start, u64 ram_size,
331 u64 compressed_size,
332 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800333 unsigned long nr_pages,
334 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500335{
336 struct async_extent *async_extent;
337
338 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100339 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500340 async_extent->start = start;
341 async_extent->ram_size = ram_size;
342 async_extent->compressed_size = compressed_size;
343 async_extent->pages = pages;
344 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800345 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500346 list_add_tail(&async_extent->list, &cow->extents);
347 return 0;
348}
349
Chris Masonc8b97812008-10-29 14:49:59 -0400350/*
Chris Mason771ed682008-11-06 22:02:51 -0500351 * we create compressed extents in two phases. The first
352 * phase compresses a range of pages that have already been
353 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400354 *
Chris Mason771ed682008-11-06 22:02:51 -0500355 * This is done inside an ordered work queue, and the compression
356 * is spread across many cpus. The actual IO submission is step
357 * two, and the ordered work queue takes care of making sure that
358 * happens in the same order things were put onto the queue by
359 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400360 *
Chris Mason771ed682008-11-06 22:02:51 -0500361 * If this code finds it can't get good compression, it puts an
362 * entry onto the work queue to write the uncompressed bytes. This
363 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300364 * are written in the same order that the flusher thread sent them
365 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400366 */
Chris Mason771ed682008-11-06 22:02:51 -0500367static noinline int compress_file_range(struct inode *inode,
368 struct page *locked_page,
369 u64 start, u64 end,
370 struct async_cow *async_cow,
371 int *num_added)
Chris Masonb888db2b2007-08-27 16:49:44 -0400372{
373 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masondb945352007-10-15 16:15:53 -0400374 u64 num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400375 u64 blocksize = root->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400376 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500377 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400378 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400379 struct page **pages = NULL;
380 unsigned long nr_pages;
381 unsigned long nr_pages_ret = 0;
382 unsigned long total_compressed = 0;
383 unsigned long total_in = 0;
384 unsigned long max_compressed = 128 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -0500385 unsigned long max_uncompressed = 128 * 1024;
Chris Masonc8b97812008-10-29 14:49:59 -0400386 int i;
387 int will_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800388 int compress_type = root->fs_info->compress_type;
Chris Mason4adaa612013-03-26 13:07:00 -0400389 int redirty = 0;
Chris Masonb888db2b2007-08-27 16:49:44 -0400390
Liu Bo4cb13e52012-03-29 09:57:45 -0400391 /* if this is a small write inside eof, kick off a defrag */
392 if ((end - start + 1) < 16 * 1024 &&
393 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400394 btrfs_add_inode_defrag(NULL, inode);
395
Chris Mason42dc7ba2008-12-15 11:44:56 -0500396 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400397again:
398 will_compress = 0;
399 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
400 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
401
Chris Masonf03d9301f2009-02-04 09:31:06 -0500402 /*
403 * we don't want to send crud past the end of i_size through
404 * compression, that's just a waste of CPU time. So, if the
405 * end of the file is before the start of our current
406 * requested range of bytes, we bail out to the uncompressed
407 * cleanup code that can deal with all of this.
408 *
409 * It isn't really the fastest way to fix things, but this is a
410 * very uncommon corner.
411 */
412 if (actual_end <= start)
413 goto cleanup_and_bail_uncompressed;
414
Chris Masonc8b97812008-10-29 14:49:59 -0400415 total_compressed = actual_end - start;
416
417 /* we want to make sure that amount of ram required to uncompress
418 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500419 * of a compressed extent to 128k. This is a crucial number
420 * because it also controls how easily we can spread reads across
421 * cpus for decompression.
422 *
423 * We also want to make sure the amount of IO required to do
424 * a random read is reasonably small, so we limit the size of
425 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400426 */
427 total_compressed = min(total_compressed, max_uncompressed);
Qu Wenruofda28322013-02-26 08:10:22 +0000428 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Masonbe20aa92007-12-17 20:14:01 -0500429 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400430 total_in = 0;
431 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400432
Chris Mason771ed682008-11-06 22:02:51 -0500433 /*
434 * we do compression for mount -o compress and when the
435 * inode has not been flagged as nocompress. This flag can
436 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400437 */
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200438 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) &&
Chris Mason1e701a32010-03-11 09:42:04 -0500439 (btrfs_test_opt(root, COMPRESS) ||
Liu Bo75e7cb72011-03-22 10:12:20 +0000440 (BTRFS_I(inode)->force_compress) ||
441 (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS))) {
Chris Masonc8b97812008-10-29 14:49:59 -0400442 WARN_ON(pages);
Chris Masoncfbc2462008-10-30 13:22:14 -0400443 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800444 if (!pages) {
445 /* just bail out to the uncompressed code */
446 goto cont;
447 }
Chris Mason179e29e2007-11-01 11:28:41 -0400448
Li Zefan261507a02010-12-17 14:21:50 +0800449 if (BTRFS_I(inode)->force_compress)
450 compress_type = BTRFS_I(inode)->force_compress;
451
Chris Mason4adaa612013-03-26 13:07:00 -0400452 /*
453 * we need to call clear_page_dirty_for_io on each
454 * page in the range. Otherwise applications with the file
455 * mmap'd can wander in and change the page contents while
456 * we are compressing them.
457 *
458 * If the compression fails for any reason, we set the pages
459 * dirty again later on.
460 */
461 extent_range_clear_dirty_for_io(inode, start, end);
462 redirty = 1;
Li Zefan261507a02010-12-17 14:21:50 +0800463 ret = btrfs_compress_pages(compress_type,
464 inode->i_mapping, start,
465 total_compressed, pages,
466 nr_pages, &nr_pages_ret,
467 &total_in,
468 &total_compressed,
469 max_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400470
471 if (!ret) {
472 unsigned long offset = total_compressed &
473 (PAGE_CACHE_SIZE - 1);
474 struct page *page = pages[nr_pages_ret - 1];
475 char *kaddr;
476
477 /* zero the tail end of the last page, we might be
478 * sending it down to disk
479 */
480 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800481 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400482 memset(kaddr + offset, 0,
483 PAGE_CACHE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800484 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400485 }
486 will_compress = 1;
487 }
488 }
Li Zefan560f7d72011-09-08 10:22:01 +0800489cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400490 if (start == 0) {
491 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500492 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400493 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500494 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400495 */
Josef Bacik00361582013-08-14 14:02:47 -0400496 ret = cow_file_range_inline(root, inode, start, end,
497 0, 0, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400498 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500499 /* try making a compressed inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400500 ret = cow_file_range_inline(root, inode, start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000501 total_compressed,
502 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400503 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100504 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400505 unsigned long clear_flags = EXTENT_DELALLOC |
506 EXTENT_DEFRAG;
507 clear_flags |= (ret < 0) ? EXTENT_DO_ACCOUNTING : 0;
508
Chris Mason771ed682008-11-06 22:02:51 -0500509 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100510 * inline extent creation worked or returned error,
511 * we don't need to create any more async work items.
512 * Unlock and free up our temp pages.
Chris Mason771ed682008-11-06 22:02:51 -0500513 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400514 extent_clear_unlock_delalloc(inode, start, end, NULL,
Josef Bacik151a41b2013-07-29 13:22:24 -0400515 clear_flags, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400516 PAGE_CLEAR_DIRTY |
517 PAGE_SET_WRITEBACK |
518 PAGE_END_WRITEBACK);
Chris Masonc8b97812008-10-29 14:49:59 -0400519 goto free_pages_out;
520 }
521 }
522
523 if (will_compress) {
524 /*
525 * we aren't doing an inline extent round the compressed size
526 * up to a block size boundary so the allocator does sane
527 * things
528 */
Qu Wenruofda28322013-02-26 08:10:22 +0000529 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400530
531 /*
532 * one last check to make sure the compression is really a
533 * win, compare the page count read with the blocks on disk
534 */
Qu Wenruofda28322013-02-26 08:10:22 +0000535 total_in = ALIGN(total_in, PAGE_CACHE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400536 if (total_compressed >= total_in) {
537 will_compress = 0;
538 } else {
Chris Masonc8b97812008-10-29 14:49:59 -0400539 num_bytes = total_in;
540 }
541 }
542 if (!will_compress && pages) {
543 /*
544 * the compression code ran but failed to make things smaller,
545 * free any pages it allocated and our page pointer array
546 */
547 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400548 WARN_ON(pages[i]->mapping);
Chris Masonc8b97812008-10-29 14:49:59 -0400549 page_cache_release(pages[i]);
550 }
551 kfree(pages);
552 pages = NULL;
553 total_compressed = 0;
554 nr_pages_ret = 0;
555
556 /* flag the file so we don't compress in the future */
Chris Mason1e701a32010-03-11 09:42:04 -0500557 if (!btrfs_test_opt(root, FORCE_COMPRESS) &&
558 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500559 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500560 }
Chris Masonc8b97812008-10-29 14:49:59 -0400561 }
Chris Mason771ed682008-11-06 22:02:51 -0500562 if (will_compress) {
563 *num_added += 1;
564
565 /* the async work queues will take care of doing actual
566 * allocation on disk for these compressed pages,
567 * and will submit them to the elevator.
568 */
569 add_async_extent(async_cow, start, num_bytes,
Li Zefan261507a02010-12-17 14:21:50 +0800570 total_compressed, pages, nr_pages_ret,
571 compress_type);
Chris Mason771ed682008-11-06 22:02:51 -0500572
Yan, Zheng24ae6362010-12-06 07:02:36 +0000573 if (start + num_bytes < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500574 start += num_bytes;
575 pages = NULL;
576 cond_resched();
577 goto again;
578 }
579 } else {
Chris Masonf03d9301f2009-02-04 09:31:06 -0500580cleanup_and_bail_uncompressed:
Chris Mason771ed682008-11-06 22:02:51 -0500581 /*
582 * No compression, but we still need to write the pages in
583 * the file we've been given so far. redirty the locked
584 * page if it corresponds to our extent and set things up
585 * for the async work queue to run cow_file_range to do
586 * the normal delalloc dance
587 */
588 if (page_offset(locked_page) >= start &&
589 page_offset(locked_page) <= end) {
590 __set_page_dirty_nobuffers(locked_page);
591 /* unlocked later on in the async handlers */
592 }
Chris Mason4adaa612013-03-26 13:07:00 -0400593 if (redirty)
594 extent_range_redirty_for_io(inode, start, end);
Li Zefan261507a02010-12-17 14:21:50 +0800595 add_async_extent(async_cow, start, end - start + 1,
596 0, NULL, 0, BTRFS_COMPRESS_NONE);
Chris Mason771ed682008-11-06 22:02:51 -0500597 *num_added += 1;
598 }
599
600out:
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100601 return ret;
Chris Mason771ed682008-11-06 22:02:51 -0500602
603free_pages_out:
604 for (i = 0; i < nr_pages_ret; i++) {
605 WARN_ON(pages[i]->mapping);
606 page_cache_release(pages[i]);
607 }
Chris Masond3977122009-01-05 21:25:51 -0500608 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500609
610 goto out;
611}
612
613/*
614 * phase two of compressed writeback. This is the ordered portion
615 * of the code, which only gets called in the order the work was
616 * queued. We walk all the async extents created by compress_file_range
617 * and send them down to the disk.
618 */
619static noinline int submit_compressed_extents(struct inode *inode,
620 struct async_cow *async_cow)
621{
622 struct async_extent *async_extent;
623 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500624 struct btrfs_key ins;
625 struct extent_map *em;
626 struct btrfs_root *root = BTRFS_I(inode)->root;
627 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
628 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500629 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500630
631 if (list_empty(&async_cow->extents))
632 return 0;
633
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500634again:
Chris Masond3977122009-01-05 21:25:51 -0500635 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500636 async_extent = list_entry(async_cow->extents.next,
637 struct async_extent, list);
638 list_del(&async_extent->list);
639
640 io_tree = &BTRFS_I(inode)->io_tree;
641
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500642retry:
Chris Mason771ed682008-11-06 22:02:51 -0500643 /* did the compression code fall back to uncompressed IO? */
644 if (!async_extent->pages) {
645 int page_started = 0;
646 unsigned long nr_written = 0;
647
648 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000649 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100650 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500651
652 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500653 ret = cow_file_range(inode, async_cow->locked_page,
654 async_extent->start,
655 async_extent->start +
656 async_extent->ram_size - 1,
657 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500658
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100659 /* JDM XXX */
660
Chris Mason771ed682008-11-06 22:02:51 -0500661 /*
662 * if page_started, cow_file_range inserted an
663 * inline extent and took care of all the unlocking
664 * and IO for us. Otherwise, we need to submit
665 * all those pages down to the drive.
666 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500667 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500668 extent_write_locked_range(io_tree,
669 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500670 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500671 async_extent->ram_size - 1,
672 btrfs_get_extent,
673 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500674 else if (ret)
675 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500676 kfree(async_extent);
677 cond_resched();
678 continue;
679 }
680
681 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100682 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500683
Josef Bacik00361582013-08-14 14:02:47 -0400684 ret = btrfs_reserve_extent(root,
Chris Mason771ed682008-11-06 22:02:51 -0500685 async_extent->compressed_size,
686 async_extent->compressed_size,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500687 0, alloc_hint, &ins, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500688 if (ret) {
689 int i;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500690
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500691 for (i = 0; i < async_extent->nr_pages; i++) {
692 WARN_ON(async_extent->pages[i]->mapping);
693 page_cache_release(async_extent->pages[i]);
694 }
695 kfree(async_extent->pages);
696 async_extent->nr_pages = 0;
697 async_extent->pages = NULL;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500698
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400699 if (ret == -ENOSPC) {
700 unlock_extent(io_tree, async_extent->start,
701 async_extent->start +
702 async_extent->ram_size - 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100703 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400704 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500705 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500706 }
707
Yan, Zhengc2167752009-11-12 09:34:21 +0000708 /*
709 * here we're doing allocation and writeback of the
710 * compressed pages
711 */
712 btrfs_drop_extent_cache(inode, async_extent->start,
713 async_extent->start +
714 async_extent->ram_size - 1, 0);
715
David Sterba172ddd62011-04-21 00:48:27 +0200716 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000717 if (!em) {
718 ret = -ENOMEM;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500719 goto out_free_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000720 }
Chris Mason771ed682008-11-06 22:02:51 -0500721 em->start = async_extent->start;
722 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500723 em->orig_start = em->start;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400724 em->mod_start = em->start;
725 em->mod_len = em->len;
Chris Mason771ed682008-11-06 22:02:51 -0500726
727 em->block_start = ins.objectid;
728 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500729 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400730 em->ram_bytes = async_extent->ram_size;
Chris Mason771ed682008-11-06 22:02:51 -0500731 em->bdev = root->fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800732 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500733 set_bit(EXTENT_FLAG_PINNED, &em->flags);
734 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400735 em->generation = -1;
Chris Mason771ed682008-11-06 22:02:51 -0500736
Chris Masond3977122009-01-05 21:25:51 -0500737 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400738 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400739 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400740 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500741 if (ret != -EEXIST) {
742 free_extent_map(em);
743 break;
744 }
745 btrfs_drop_extent_cache(inode, async_extent->start,
746 async_extent->start +
747 async_extent->ram_size - 1, 0);
748 }
749
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500750 if (ret)
751 goto out_free_reserve;
752
Li Zefan261507a02010-12-17 14:21:50 +0800753 ret = btrfs_add_ordered_extent_compress(inode,
754 async_extent->start,
755 ins.objectid,
756 async_extent->ram_size,
757 ins.offset,
758 BTRFS_ORDERED_COMPRESSED,
759 async_extent->compress_type);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500760 if (ret)
761 goto out_free_reserve;
Chris Mason771ed682008-11-06 22:02:51 -0500762
Chris Mason771ed682008-11-06 22:02:51 -0500763 /*
764 * clear dirty, set writeback and unlock the pages.
765 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400766 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400767 async_extent->start +
768 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400769 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
770 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400771 PAGE_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500772 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500773 async_extent->start,
774 async_extent->ram_size,
775 ins.objectid,
776 ins.offset, async_extent->pages,
777 async_extent->nr_pages);
Chris Mason771ed682008-11-06 22:02:51 -0500778 alloc_hint = ins.objectid + ins.offset;
779 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500780 if (ret)
781 goto out;
Chris Mason771ed682008-11-06 22:02:51 -0500782 cond_resched();
783 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100784 ret = 0;
785out:
786 return ret;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500787out_free_reserve:
788 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100789out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400790 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500791 async_extent->start +
792 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400793 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400794 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
795 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
796 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100797 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500798 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500799}
800
Josef Bacik4b46fce2010-05-23 11:00:55 -0400801static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
802 u64 num_bytes)
803{
804 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
805 struct extent_map *em;
806 u64 alloc_hint = 0;
807
808 read_lock(&em_tree->lock);
809 em = search_extent_mapping(em_tree, start, num_bytes);
810 if (em) {
811 /*
812 * if block start isn't an actual block number then find the
813 * first block in this inode and use that as a hint. If that
814 * block is also bogus then just don't worry about it.
815 */
816 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
817 free_extent_map(em);
818 em = search_extent_mapping(em_tree, 0, 0);
819 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
820 alloc_hint = em->block_start;
821 if (em)
822 free_extent_map(em);
823 } else {
824 alloc_hint = em->block_start;
825 free_extent_map(em);
826 }
827 }
828 read_unlock(&em_tree->lock);
829
830 return alloc_hint;
831}
832
Chris Mason771ed682008-11-06 22:02:51 -0500833/*
834 * when extent_io.c finds a delayed allocation range in the file,
835 * the call backs end up in this code. The basic idea is to
836 * allocate extents on disk for the range, and create ordered data structs
837 * in ram to track those extents.
838 *
839 * locked_page is the page that writepage had locked already. We use
840 * it to make sure we don't do extra locks or unlocks.
841 *
842 * *page_started is set to one if we unlock locked_page and do everything
843 * required to start IO on it. It may be clean and already done with
844 * IO when we return.
845 */
Josef Bacik00361582013-08-14 14:02:47 -0400846static noinline int cow_file_range(struct inode *inode,
847 struct page *locked_page,
848 u64 start, u64 end, int *page_started,
849 unsigned long *nr_written,
850 int unlock)
Chris Mason771ed682008-11-06 22:02:51 -0500851{
Josef Bacik00361582013-08-14 14:02:47 -0400852 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500853 u64 alloc_hint = 0;
854 u64 num_bytes;
855 unsigned long ram_size;
856 u64 disk_num_bytes;
857 u64 cur_alloc_size;
858 u64 blocksize = root->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500859 struct btrfs_key ins;
860 struct extent_map *em;
861 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
862 int ret = 0;
863
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400864 if (btrfs_is_free_space_inode(inode)) {
865 WARN_ON_ONCE(1);
866 return -EINVAL;
867 }
Chris Mason771ed682008-11-06 22:02:51 -0500868
Qu Wenruofda28322013-02-26 08:10:22 +0000869 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500870 num_bytes = max(blocksize, num_bytes);
871 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500872
Chris Mason4cb53002011-05-24 15:35:30 -0400873 /* if this is a small write inside eof, kick off defrag */
Liu Bo4cb13e52012-03-29 09:57:45 -0400874 if (num_bytes < 64 * 1024 &&
875 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Josef Bacik00361582013-08-14 14:02:47 -0400876 btrfs_add_inode_defrag(NULL, inode);
Chris Mason4cb53002011-05-24 15:35:30 -0400877
Chris Mason771ed682008-11-06 22:02:51 -0500878 if (start == 0) {
879 /* lets try to make an inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400880 ret = cow_file_range_inline(root, inode, start, end, 0, 0,
881 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500882 if (ret == 0) {
Josef Bacikc2790a22013-07-29 11:20:47 -0400883 extent_clear_unlock_delalloc(inode, start, end, NULL,
884 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400885 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400886 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
887 PAGE_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000888
Chris Mason771ed682008-11-06 22:02:51 -0500889 *nr_written = *nr_written +
890 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
891 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500892 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100893 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100894 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500895 }
896 }
Chris Masonc8b97812008-10-29 14:49:59 -0400897
898 BUG_ON(disk_num_bytes >
David Sterba6c417612011-04-13 15:41:04 +0200899 btrfs_super_total_bytes(root->fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400900
Josef Bacik4b46fce2010-05-23 11:00:55 -0400901 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400902 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400903
Chris Masond3977122009-01-05 21:25:51 -0500904 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400905 unsigned long op;
906
Josef Bacik287a0ab2010-03-19 18:07:23 +0000907 cur_alloc_size = disk_num_bytes;
Josef Bacik00361582013-08-14 14:02:47 -0400908 ret = btrfs_reserve_extent(root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500909 root->sectorsize, 0, alloc_hint,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500910 &ins, 1);
Josef Bacik00361582013-08-14 14:02:47 -0400911 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100912 goto out_unlock;
Chris Masond3977122009-01-05 21:25:51 -0500913
David Sterba172ddd62011-04-21 00:48:27 +0200914 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000915 if (!em) {
916 ret = -ENOMEM;
Liu Boace68ba2013-04-22 10:53:47 +0000917 goto out_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000918 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400919 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500920 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500921 ram_size = ins.offset;
922 em->len = ins.offset;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400923 em->mod_start = em->start;
924 em->mod_len = em->len;
Chris Masonc8b97812008-10-29 14:49:59 -0400925
Chris Masone6dcd2d2008-07-17 12:53:50 -0400926 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400927 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500928 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400929 em->ram_bytes = ram_size;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400930 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400931 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400932 em->generation = -1;
Chris Masonc8b97812008-10-29 14:49:59 -0400933
Chris Masond3977122009-01-05 21:25:51 -0500934 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400935 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400936 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400937 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400938 if (ret != -EEXIST) {
939 free_extent_map(em);
940 break;
941 }
942 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400943 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400944 }
Liu Boace68ba2013-04-22 10:53:47 +0000945 if (ret)
946 goto out_reserve;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400947
Chris Mason98d20f62008-04-14 09:46:10 -0400948 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400949 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -0500950 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +0000951 if (ret)
952 goto out_reserve;
Chris Masonc8b97812008-10-29 14:49:59 -0400953
Yan Zheng17d217f2008-12-12 10:03:38 -0500954 if (root->root_key.objectid ==
955 BTRFS_DATA_RELOC_TREE_OBJECTID) {
956 ret = btrfs_reloc_clone_csums(inode, start,
957 cur_alloc_size);
Josef Bacik00361582013-08-14 14:02:47 -0400958 if (ret)
Liu Boace68ba2013-04-22 10:53:47 +0000959 goto out_reserve;
Yan Zheng17d217f2008-12-12 10:03:38 -0500960 }
961
Chris Masond3977122009-01-05 21:25:51 -0500962 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -0400963 break;
Chris Masond3977122009-01-05 21:25:51 -0500964
Chris Masonc8b97812008-10-29 14:49:59 -0400965 /* we're not doing compressed IO, don't unlock the first
966 * page (which the caller expects to stay locked), don't
967 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -0400968 *
969 * Do set the Private2 bit so we know this page was properly
970 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -0400971 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400972 op = unlock ? PAGE_UNLOCK : 0;
973 op |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -0400974
Josef Bacikc2790a22013-07-29 11:20:47 -0400975 extent_clear_unlock_delalloc(inode, start,
976 start + ram_size - 1, locked_page,
977 EXTENT_LOCKED | EXTENT_DELALLOC,
978 op);
Chris Masonc8b97812008-10-29 14:49:59 -0400979 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -0500980 num_bytes -= cur_alloc_size;
981 alloc_hint = ins.objectid + ins.offset;
982 start += cur_alloc_size;
Chris Masonb888db2b2007-08-27 16:49:44 -0400983 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100984out:
Chris Masonbe20aa92007-12-17 20:14:01 -0500985 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +0000986
Liu Boace68ba2013-04-22 10:53:47 +0000987out_reserve:
988 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100989out_unlock:
Josef Bacikc2790a22013-07-29 11:20:47 -0400990 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Josef Bacik151a41b2013-07-29 13:22:24 -0400991 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
992 EXTENT_DELALLOC | EXTENT_DEFRAG,
993 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
994 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100995 goto out;
Chris Mason771ed682008-11-06 22:02:51 -0500996}
Chris Masonc8b97812008-10-29 14:49:59 -0400997
Chris Mason771ed682008-11-06 22:02:51 -0500998/*
999 * work queue call back to started compression on a file and pages
1000 */
1001static noinline void async_cow_start(struct btrfs_work *work)
1002{
1003 struct async_cow *async_cow;
1004 int num_added = 0;
1005 async_cow = container_of(work, struct async_cow, work);
1006
1007 compress_file_range(async_cow->inode, async_cow->locked_page,
1008 async_cow->start, async_cow->end, async_cow,
1009 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001010 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001011 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001012 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001013 }
Chris Mason771ed682008-11-06 22:02:51 -05001014}
1015
1016/*
1017 * work queue call back to submit previously compressed pages
1018 */
1019static noinline void async_cow_submit(struct btrfs_work *work)
1020{
1021 struct async_cow *async_cow;
1022 struct btrfs_root *root;
1023 unsigned long nr_pages;
1024
1025 async_cow = container_of(work, struct async_cow, work);
1026
1027 root = async_cow->root;
1028 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
1029 PAGE_CACHE_SHIFT;
1030
Josef Bacik66657b32012-08-01 15:36:24 -04001031 if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) <
Liu Bo287082b2012-06-28 04:02:24 -06001032 5 * 1024 * 1024 &&
Chris Mason771ed682008-11-06 22:02:51 -05001033 waitqueue_active(&root->fs_info->async_submit_wait))
1034 wake_up(&root->fs_info->async_submit_wait);
1035
Chris Masond3977122009-01-05 21:25:51 -05001036 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001037 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001038}
Chris Masonc8b97812008-10-29 14:49:59 -04001039
Chris Mason771ed682008-11-06 22:02:51 -05001040static noinline void async_cow_free(struct btrfs_work *work)
1041{
1042 struct async_cow *async_cow;
1043 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001044 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001045 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001046 kfree(async_cow);
1047}
1048
1049static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1050 u64 start, u64 end, int *page_started,
1051 unsigned long *nr_written)
1052{
1053 struct async_cow *async_cow;
1054 struct btrfs_root *root = BTRFS_I(inode)->root;
1055 unsigned long nr_pages;
1056 u64 cur_end;
Liu Bo287082b2012-06-28 04:02:24 -06001057 int limit = 10 * 1024 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -05001058
Chris Masona3429ab2009-10-08 12:30:20 -04001059 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1060 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001061 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001062 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001063 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001064 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001065 async_cow->root = root;
1066 async_cow->locked_page = locked_page;
1067 async_cow->start = start;
1068
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001069 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
Chris Mason771ed682008-11-06 22:02:51 -05001070 cur_end = end;
1071 else
1072 cur_end = min(end, start + 512 * 1024 - 1);
1073
1074 async_cow->end = cur_end;
1075 INIT_LIST_HEAD(&async_cow->extents);
1076
1077 async_cow->work.func = async_cow_start;
1078 async_cow->work.ordered_func = async_cow_submit;
1079 async_cow->work.ordered_free = async_cow_free;
1080 async_cow->work.flags = 0;
1081
Chris Mason771ed682008-11-06 22:02:51 -05001082 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
1083 PAGE_CACHE_SHIFT;
1084 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
1085
1086 btrfs_queue_worker(&root->fs_info->delalloc_workers,
1087 &async_cow->work);
1088
1089 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
1090 wait_event(root->fs_info->async_submit_wait,
1091 (atomic_read(&root->fs_info->async_delalloc_pages) <
1092 limit));
1093 }
1094
Chris Masond3977122009-01-05 21:25:51 -05001095 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -05001096 atomic_read(&root->fs_info->async_delalloc_pages)) {
1097 wait_event(root->fs_info->async_submit_wait,
1098 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1099 0));
1100 }
1101
1102 *nr_written += nr_pages;
1103 start = cur_end + 1;
1104 }
1105 *page_started = 1;
1106 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001107}
1108
Chris Masond3977122009-01-05 21:25:51 -05001109static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001110 u64 bytenr, u64 num_bytes)
1111{
1112 int ret;
1113 struct btrfs_ordered_sum *sums;
1114 LIST_HEAD(list);
1115
Yan Zheng07d400a2009-01-06 11:42:00 -05001116 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001117 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001118 if (ret == 0 && list_empty(&list))
1119 return 0;
1120
1121 while (!list_empty(&list)) {
1122 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1123 list_del(&sums->list);
1124 kfree(sums);
1125 }
1126 return 1;
1127}
1128
Chris Masond352ac62008-09-29 15:18:18 -04001129/*
1130 * when nowcow writeback call back. This checks for snapshots or COW copies
1131 * of the extents that exist in the file, and COWs the file as required.
1132 *
1133 * If no cow copies or snapshots exist, we write directly to the existing
1134 * blocks on disk
1135 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001136static noinline int run_delalloc_nocow(struct inode *inode,
1137 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001138 u64 start, u64 end, int *page_started, int force,
1139 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001140{
Chris Masonbe20aa92007-12-17 20:14:01 -05001141 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001142 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001143 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001144 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001145 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001146 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001147 u64 cow_start;
1148 u64 cur_offset;
1149 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001150 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001151 u64 disk_bytenr;
1152 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001153 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001154 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001155 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001156 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001157 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001158 int nocow;
1159 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001160 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001161 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001162
1163 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001164 if (!path) {
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);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001172 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001173 }
Li Zefan82d59022011-04-20 10:33:24 +08001174
Liu Bo83eea1f2012-07-10 05:28:39 -06001175 nolock = btrfs_is_free_space_inode(inode);
Li Zefan82d59022011-04-20 10:33:24 +08001176
1177 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001178 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001179 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001180 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001181
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001182 if (IS_ERR(trans)) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001183 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1184 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001185 EXTENT_DO_ACCOUNTING |
1186 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001187 PAGE_CLEAR_DIRTY |
1188 PAGE_SET_WRITEBACK |
1189 PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001190 btrfs_free_path(path);
1191 return PTR_ERR(trans);
1192 }
1193
Josef Bacik74b21072011-04-13 12:02:53 -04001194 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001195
Yan Zheng80ff3852008-10-30 14:20:02 -04001196 cow_start = (u64)-1;
1197 cur_offset = start;
1198 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001199 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001200 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001201 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001202 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001203 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1204 leaf = path->nodes[0];
1205 btrfs_item_key_to_cpu(leaf, &found_key,
1206 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001207 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001208 found_key.type == BTRFS_EXTENT_DATA_KEY)
1209 path->slots[0]--;
1210 }
1211 check_prev = 0;
1212next_slot:
1213 leaf = path->nodes[0];
1214 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1215 ret = btrfs_next_leaf(root, path);
Josef Bacikd788a342013-10-25 16:55:08 -04001216 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001217 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001218 if (ret > 0)
1219 break;
1220 leaf = path->nodes[0];
1221 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001222
Yan Zheng80ff3852008-10-30 14:20:02 -04001223 nocow = 0;
1224 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001225 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001226 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001227
Li Zefan33345d012011-04-20 10:31:50 +08001228 if (found_key.objectid > ino ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001229 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1230 found_key.offset > end)
1231 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001232
Yan Zheng80ff3852008-10-30 14:20:02 -04001233 if (found_key.offset > cur_offset) {
1234 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001235 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001236 goto out_check;
1237 }
Chris Masonc31f8832008-01-08 15:46:31 -05001238
Yan Zheng80ff3852008-10-30 14:20:02 -04001239 fi = btrfs_item_ptr(leaf, path->slots[0],
1240 struct btrfs_file_extent_item);
1241 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001242
Josef Bacikcc95bef2013-04-04 14:31:27 -04001243 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001244 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1245 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001246 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001247 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001248 extent_end = found_key.offset +
1249 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001250 disk_num_bytes =
1251 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001252 if (extent_end <= start) {
1253 path->slots[0]++;
1254 goto next_slot;
1255 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001256 if (disk_bytenr == 0)
1257 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001258 if (btrfs_file_extent_compression(leaf, fi) ||
1259 btrfs_file_extent_encryption(leaf, fi) ||
1260 btrfs_file_extent_other_encoding(leaf, fi))
1261 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001262 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1263 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001264 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001265 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001266 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001267 found_key.offset -
1268 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001269 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001270 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001271 disk_bytenr += cur_offset - found_key.offset;
1272 num_bytes = min(end + 1, extent_end) - cur_offset;
1273 /*
1274 * force cow if csum exists in the range.
1275 * this ensure that csum for a given extent are
1276 * either valid or do not exist.
1277 */
1278 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1279 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001280 nocow = 1;
1281 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1282 extent_end = found_key.offset +
1283 btrfs_file_extent_inline_len(leaf, fi);
1284 extent_end = ALIGN(extent_end, root->sectorsize);
1285 } else {
1286 BUG_ON(1);
1287 }
1288out_check:
1289 if (extent_end <= start) {
1290 path->slots[0]++;
1291 goto next_slot;
1292 }
1293 if (!nocow) {
1294 if (cow_start == (u64)-1)
1295 cow_start = cur_offset;
1296 cur_offset = extent_end;
1297 if (cur_offset > end)
1298 break;
1299 path->slots[0]++;
1300 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001301 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001302
David Sterbab3b4aa72011-04-21 01:20:15 +02001303 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001304 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001305 ret = cow_file_range(inode, locked_page,
1306 cow_start, found_key.offset - 1,
1307 page_started, nr_written, 1);
Josef Bacikd788a342013-10-25 16:55:08 -04001308 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001309 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001310 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001311 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001312
Yan Zhengd899e052008-10-30 14:25:28 -04001313 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1314 struct extent_map *em;
1315 struct extent_map_tree *em_tree;
1316 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001317 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001318 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001319 em->start = cur_offset;
Josef Bacik70c8a912012-10-11 16:54:30 -04001320 em->orig_start = found_key.offset - extent_offset;
Yan Zhengd899e052008-10-30 14:25:28 -04001321 em->len = num_bytes;
1322 em->block_len = num_bytes;
1323 em->block_start = disk_bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05001324 em->orig_block_len = disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001325 em->ram_bytes = ram_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04001326 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001327 em->mod_start = em->start;
1328 em->mod_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04001329 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacikb11e2342012-12-03 10:58:15 -05001330 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001331 em->generation = -1;
Yan Zhengd899e052008-10-30 14:25:28 -04001332 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001333 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001334 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001335 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001336 if (ret != -EEXIST) {
1337 free_extent_map(em);
1338 break;
1339 }
1340 btrfs_drop_extent_cache(inode, em->start,
1341 em->start + em->len - 1, 0);
1342 }
1343 type = BTRFS_ORDERED_PREALLOC;
1344 } else {
1345 type = BTRFS_ORDERED_NOCOW;
1346 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001347
1348 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001349 num_bytes, num_bytes, type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001350 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001351
Yan, Zhengefa56462010-05-16 10:49:59 -04001352 if (root->root_key.objectid ==
1353 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1354 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1355 num_bytes);
Josef Bacikd788a342013-10-25 16:55:08 -04001356 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001357 goto error;
Yan, Zhengefa56462010-05-16 10:49:59 -04001358 }
1359
Josef Bacikc2790a22013-07-29 11:20:47 -04001360 extent_clear_unlock_delalloc(inode, cur_offset,
1361 cur_offset + num_bytes - 1,
1362 locked_page, EXTENT_LOCKED |
1363 EXTENT_DELALLOC, PAGE_UNLOCK |
1364 PAGE_SET_PRIVATE2);
Yan Zheng80ff3852008-10-30 14:20:02 -04001365 cur_offset = extent_end;
1366 if (cur_offset > end)
1367 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001368 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001369 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001370
Josef Bacik17ca04a2012-05-31 15:58:55 -04001371 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001372 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001373 cur_offset = end;
1374 }
1375
Yan Zheng80ff3852008-10-30 14:20:02 -04001376 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001377 ret = cow_file_range(inode, locked_page, cow_start, end,
1378 page_started, nr_written, 1);
Josef Bacikd788a342013-10-25 16:55:08 -04001379 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001380 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001381 }
1382
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001383error:
Miao Xiea698d0752012-09-20 01:51:59 -06001384 err = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001385 if (!ret)
1386 ret = err;
1387
Josef Bacik17ca04a2012-05-31 15:58:55 -04001388 if (ret && cur_offset < end)
Josef Bacikc2790a22013-07-29 11:20:47 -04001389 extent_clear_unlock_delalloc(inode, cur_offset, end,
1390 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001391 EXTENT_DELALLOC | EXTENT_DEFRAG |
1392 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1393 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001394 PAGE_SET_WRITEBACK |
1395 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001396 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001397 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001398}
1399
Chris Masond352ac62008-09-29 15:18:18 -04001400/*
1401 * extent_io.c call back to do delayed allocation processing
1402 */
Chris Masonc8b97812008-10-29 14:49:59 -04001403static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001404 u64 start, u64 end, int *page_started,
1405 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001406{
Chris Masonbe20aa92007-12-17 20:14:01 -05001407 int ret;
Chris Mason7f366cf2009-03-12 20:12:45 -04001408 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona2135012008-06-25 16:01:30 -04001409
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001410 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) {
Chris Masonc8b97812008-10-29 14:49:59 -04001411 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001412 page_started, 1, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001413 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001414 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001415 page_started, 0, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001416 } else if (!btrfs_test_opt(root, COMPRESS) &&
1417 !(BTRFS_I(inode)->force_compress) &&
1418 !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS)) {
Chris Mason7f366cf2009-03-12 20:12:45 -04001419 ret = cow_file_range(inode, locked_page, start, end,
1420 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001421 } else {
1422 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1423 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001424 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001425 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001426 }
Chris Masonb888db2b2007-08-27 16:49:44 -04001427 return ret;
1428}
1429
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001430static void btrfs_split_extent_hook(struct inode *inode,
1431 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001432{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001433 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001434 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001435 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001436
Josef Bacik9e0baf62011-07-15 15:16:44 +00001437 spin_lock(&BTRFS_I(inode)->lock);
1438 BTRFS_I(inode)->outstanding_extents++;
1439 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001440}
1441
1442/*
1443 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1444 * extents so we can keep track of new extents that are just merged onto old
1445 * extents, such as when we are doing sequential writes, so we can properly
1446 * account for the metadata space we'll need.
1447 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001448static void btrfs_merge_extent_hook(struct inode *inode,
1449 struct extent_state *new,
1450 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001451{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001452 /* not delalloc, ignore it */
1453 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001454 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001455
Josef Bacik9e0baf62011-07-15 15:16:44 +00001456 spin_lock(&BTRFS_I(inode)->lock);
1457 BTRFS_I(inode)->outstanding_extents--;
1458 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001459}
1460
Miao Xieeb73c1b2013-05-15 07:48:22 +00001461static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1462 struct inode *inode)
1463{
1464 spin_lock(&root->delalloc_lock);
1465 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1466 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1467 &root->delalloc_inodes);
1468 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1469 &BTRFS_I(inode)->runtime_flags);
1470 root->nr_delalloc_inodes++;
1471 if (root->nr_delalloc_inodes == 1) {
1472 spin_lock(&root->fs_info->delalloc_root_lock);
1473 BUG_ON(!list_empty(&root->delalloc_root));
1474 list_add_tail(&root->delalloc_root,
1475 &root->fs_info->delalloc_roots);
1476 spin_unlock(&root->fs_info->delalloc_root_lock);
1477 }
1478 }
1479 spin_unlock(&root->delalloc_lock);
1480}
1481
1482static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1483 struct inode *inode)
1484{
1485 spin_lock(&root->delalloc_lock);
1486 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1487 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1488 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1489 &BTRFS_I(inode)->runtime_flags);
1490 root->nr_delalloc_inodes--;
1491 if (!root->nr_delalloc_inodes) {
1492 spin_lock(&root->fs_info->delalloc_root_lock);
1493 BUG_ON(list_empty(&root->delalloc_root));
1494 list_del_init(&root->delalloc_root);
1495 spin_unlock(&root->fs_info->delalloc_root_lock);
1496 }
1497 }
1498 spin_unlock(&root->delalloc_lock);
1499}
1500
Chris Masond352ac62008-09-29 15:18:18 -04001501/*
1502 * extent_io.c set_bit_hook, used to track delayed allocation
1503 * bytes in this file, and to maintain the list of inodes that
1504 * have pending delalloc work to be done.
1505 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001506static void btrfs_set_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001507 struct extent_state *state, unsigned long *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001508{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001509
Chris Mason75eff682008-12-15 15:54:40 -05001510 /*
1511 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001512 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001513 * bit, which is only set or cleared with irqs on
1514 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001515 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001516 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001517 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001518 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001519
Josef Bacik9e0baf62011-07-15 15:16:44 +00001520 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001521 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001522 } else {
1523 spin_lock(&BTRFS_I(inode)->lock);
1524 BTRFS_I(inode)->outstanding_extents++;
1525 spin_unlock(&BTRFS_I(inode)->lock);
1526 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001527
Miao Xie963d6782013-01-29 10:10:51 +00001528 __percpu_counter_add(&root->fs_info->delalloc_bytes, len,
1529 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001530 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001531 BTRFS_I(inode)->delalloc_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001532 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001533 &BTRFS_I(inode)->runtime_flags))
1534 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001535 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001536 }
Chris Mason291d6732008-01-29 15:55:23 -05001537}
1538
Chris Masond352ac62008-09-29 15:18:18 -04001539/*
1540 * extent_io.c clear_bit_hook, see set_bit_hook for why
1541 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001542static void btrfs_clear_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001543 struct extent_state *state,
1544 unsigned long *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001545{
Chris Mason75eff682008-12-15 15:54:40 -05001546 /*
1547 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001548 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001549 * bit, which is only set or cleared with irqs on
1550 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001551 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001552 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001553 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001554 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001555
Josef Bacik9e0baf62011-07-15 15:16:44 +00001556 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001557 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001558 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1559 spin_lock(&BTRFS_I(inode)->lock);
1560 BTRFS_I(inode)->outstanding_extents--;
1561 spin_unlock(&BTRFS_I(inode)->lock);
1562 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001563
Josef Bacikb6d08f02013-09-27 14:57:43 -04001564 /*
1565 * We don't reserve metadata space for space cache inodes so we
1566 * don't need to call dellalloc_release_metadata if there is an
1567 * error.
1568 */
1569 if (*bits & EXTENT_DO_ACCOUNTING &&
1570 root != root->fs_info->tree_root)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001571 btrfs_delalloc_release_metadata(inode, len);
1572
Josef Bacik0cb59c92010-07-02 12:14:14 -04001573 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Josef Bacik7ee9e442013-06-21 16:37:03 -04001574 && do_list && !(state->state & EXTENT_NORESERVE))
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001575 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001576
Miao Xie963d6782013-01-29 10:10:51 +00001577 __percpu_counter_add(&root->fs_info->delalloc_bytes, -len,
1578 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001579 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001580 BTRFS_I(inode)->delalloc_bytes -= len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001581 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001582 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001583 &BTRFS_I(inode)->runtime_flags))
1584 btrfs_del_delalloc_inode(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001585 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001586 }
Chris Mason291d6732008-01-29 15:55:23 -05001587}
1588
Chris Masond352ac62008-09-29 15:18:18 -04001589/*
1590 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1591 * we don't create bios that span stripes or chunks
1592 */
David Woodhouse64a16702009-07-15 23:29:37 +01001593int btrfs_merge_bio_hook(int rw, struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001594 size_t size, struct bio *bio,
1595 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001596{
1597 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Chris Masona62b9402008-10-03 16:31:08 -04001598 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001599 u64 length = 0;
1600 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001601 int ret;
1602
Chris Mason771ed682008-11-06 22:02:51 -05001603 if (bio_flags & EXTENT_BIO_COMPRESSED)
1604 return 0;
1605
Chris Masonf2d8d742008-04-21 10:03:05 -04001606 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001607 map_length = length;
David Woodhouse64a16702009-07-15 23:29:37 +01001608 ret = btrfs_map_block(root->fs_info, rw, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001609 &map_length, NULL, 0);
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001610 /* Will always return 0 with map_multi == NULL */
Jeff Mahoney3444a972011-10-03 23:23:13 -04001611 BUG_ON(ret < 0);
Chris Masond3977122009-01-05 21:25:51 -05001612 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001613 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001614 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001615}
1616
Chris Masond352ac62008-09-29 15:18:18 -04001617/*
1618 * in order to insert checksums into the metadata in large chunks,
1619 * we wait until bio submission time. All the pages in the bio are
1620 * checksummed and sums are attached onto the ordered extent record.
1621 *
1622 * At IO completion time the cums attached on the ordered extent record
1623 * are inserted into the btree
1624 */
Chris Masond3977122009-01-05 21:25:51 -05001625static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1626 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001627 unsigned long bio_flags,
1628 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001629{
Chris Mason065631f2008-02-20 12:07:25 -05001630 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001631 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001632
Chris Masond20f7042008-12-08 16:58:54 -05001633 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001634 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001635 return 0;
1636}
Chris Masone0156402008-04-16 11:15:20 -04001637
Chris Mason4a69a412008-11-06 22:03:00 -05001638/*
1639 * in order to insert checksums into the metadata in large chunks,
1640 * we wait until bio submission time. All the pages in the bio are
1641 * checksummed and sums are attached onto the ordered extent record.
1642 *
1643 * At IO completion time the cums attached on the ordered extent record
1644 * are inserted into the btree
1645 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001646static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001647 int mirror_num, unsigned long bio_flags,
1648 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001649{
1650 struct btrfs_root *root = BTRFS_I(inode)->root;
Stefan Behrens61891922012-11-05 18:51:52 +01001651 int ret;
1652
1653 ret = btrfs_map_bio(root, rw, bio, mirror_num, 1);
1654 if (ret)
1655 bio_endio(bio, ret);
1656 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001657}
1658
Chris Masond352ac62008-09-29 15:18:18 -04001659/*
Chris Masoncad321a2008-12-17 14:51:42 -05001660 * extent_io.c submission hook. This does the right thing for csum calculation
1661 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001662 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001663static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001664 int mirror_num, unsigned long bio_flags,
1665 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001666{
1667 struct btrfs_root *root = BTRFS_I(inode)->root;
1668 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001669 int skip_sum;
Jeff Mahoney04173412011-10-03 23:23:12 -04001670 int metadata = 0;
Josef Bacikb812ce22012-11-16 13:56:32 -05001671 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001672
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001673 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001674
Liu Bo83eea1f2012-07-10 05:28:39 -06001675 if (btrfs_is_free_space_inode(inode))
Jeff Mahoney04173412011-10-03 23:23:12 -04001676 metadata = 2;
1677
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001678 if (!(rw & REQ_WRITE)) {
Josef Bacik5fd02042012-05-02 14:00:54 -04001679 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1680 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001681 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001682
Chris Masond20f7042008-12-08 16:58:54 -05001683 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001684 ret = btrfs_submit_compressed_read(inode, bio,
1685 mirror_num,
1686 bio_flags);
1687 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001688 } else if (!skip_sum) {
1689 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1690 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001691 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001692 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001693 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001694 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001695 /* csum items have already been cloned */
1696 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1697 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001698 /* we're doing a write, do the async checksumming */
Stefan Behrens61891922012-11-05 18:51:52 +01001699 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001700 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001701 bio_flags, bio_offset,
1702 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001703 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001704 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001705 } else if (!skip_sum) {
1706 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1707 if (ret)
1708 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001709 }
1710
Chris Mason0b86a832008-03-24 15:01:56 -04001711mapit:
Stefan Behrens61891922012-11-05 18:51:52 +01001712 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
1713
1714out:
1715 if (ret < 0)
1716 bio_endio(bio, ret);
1717 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001718}
Chris Mason6885f302008-02-20 16:11:05 -05001719
Chris Masond352ac62008-09-29 15:18:18 -04001720/*
1721 * given a list of ordered sums record them in the inode. This happens
1722 * at IO completion time based on sums calculated at bio submission time.
1723 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001724static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001725 struct inode *inode, u64 file_offset,
1726 struct list_head *list)
1727{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001728 struct btrfs_ordered_sum *sum;
1729
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001730 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00001731 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05001732 btrfs_csum_file_blocks(trans,
1733 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00001734 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001735 }
1736 return 0;
1737}
1738
Josef Bacik2ac55d42010-02-03 19:33:23 +00001739int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1740 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001741{
Julia Lawall6c1500f2012-11-03 20:30:18 +00001742 WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04001743 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001744 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001745}
1746
Chris Masond352ac62008-09-29 15:18:18 -04001747/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001748struct btrfs_writepage_fixup {
1749 struct page *page;
1750 struct btrfs_work work;
1751};
1752
Christoph Hellwigb2950862008-12-02 09:54:17 -05001753static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001754{
1755 struct btrfs_writepage_fixup *fixup;
1756 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001757 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001758 struct page *page;
1759 struct inode *inode;
1760 u64 page_start;
1761 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001762 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001763
1764 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1765 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001766again:
Chris Mason247e7432008-07-17 12:53:51 -04001767 lock_page(page);
1768 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1769 ClearPageChecked(page);
1770 goto out_page;
1771 }
1772
1773 inode = page->mapping->host;
1774 page_start = page_offset(page);
1775 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1776
Josef Bacik2ac55d42010-02-03 19:33:23 +00001777 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001778 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001779
1780 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001781 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001782 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001783
1784 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1785 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001786 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1787 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001788 unlock_page(page);
1789 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001790 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04001791 goto again;
1792 }
Chris Mason247e7432008-07-17 12:53:51 -04001793
Jeff Mahoney87826df2012-02-15 16:23:57 +01001794 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
1795 if (ret) {
1796 mapping_set_error(page->mapping, ret);
1797 end_extent_writepage(page, ret, page_start, page_end);
1798 ClearPageChecked(page);
1799 goto out;
1800 }
1801
Josef Bacik2ac55d42010-02-03 19:33:23 +00001802 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001803 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001804 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04001805out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001806 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1807 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001808out_page:
1809 unlock_page(page);
1810 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08001811 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04001812}
1813
1814/*
1815 * There are a few paths in the higher layers of the kernel that directly
1816 * set the page dirty bit without asking the filesystem if it is a
1817 * good idea. This causes problems because we want to make sure COW
1818 * properly happens and the data=ordered rules are followed.
1819 *
Chris Masonc8b97812008-10-29 14:49:59 -04001820 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001821 * hasn't been properly setup for IO. We kick off an async process
1822 * to fix it up. The async helper will wait for ordered extents, set
1823 * the delalloc bit and make it safe to write the page.
1824 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001825static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001826{
1827 struct inode *inode = page->mapping->host;
1828 struct btrfs_writepage_fixup *fixup;
1829 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001830
Chris Mason8b62b722009-09-02 16:53:46 -04001831 /* this page is properly in the ordered list */
1832 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001833 return 0;
1834
1835 if (PageChecked(page))
1836 return -EAGAIN;
1837
1838 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1839 if (!fixup)
1840 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001841
Chris Mason247e7432008-07-17 12:53:51 -04001842 SetPageChecked(page);
1843 page_cache_get(page);
1844 fixup->work.func = btrfs_writepage_fixup_worker;
1845 fixup->page = page;
1846 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001847 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04001848}
1849
Yan Zhengd899e052008-10-30 14:25:28 -04001850static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1851 struct inode *inode, u64 file_pos,
1852 u64 disk_bytenr, u64 disk_num_bytes,
1853 u64 num_bytes, u64 ram_bytes,
1854 u8 compression, u8 encryption,
1855 u16 other_encoding, int extent_type)
1856{
1857 struct btrfs_root *root = BTRFS_I(inode)->root;
1858 struct btrfs_file_extent_item *fi;
1859 struct btrfs_path *path;
1860 struct extent_buffer *leaf;
1861 struct btrfs_key ins;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00001862 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04001863 int ret;
1864
1865 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07001866 if (!path)
1867 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04001868
Chris Masona1ed8352009-09-11 12:27:37 -04001869 /*
1870 * we may be replacing one extent in the tree with another.
1871 * The new extent is pinned in the extent map, and we don't want
1872 * to drop it from the cache until it is completely in the btree.
1873 *
1874 * So, tell btrfs_drop_extents to leave this extent in the cache.
1875 * the caller is expected to unpin it and allow it to be merged
1876 * with the others.
1877 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00001878 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
1879 file_pos + num_bytes, NULL, 0,
1880 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001881 if (ret)
1882 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001883
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00001884 if (!extent_inserted) {
1885 ins.objectid = btrfs_ino(inode);
1886 ins.offset = file_pos;
1887 ins.type = BTRFS_EXTENT_DATA_KEY;
1888
1889 path->leave_spinning = 1;
1890 ret = btrfs_insert_empty_item(trans, root, path, &ins,
1891 sizeof(*fi));
1892 if (ret)
1893 goto out;
1894 }
Yan Zhengd899e052008-10-30 14:25:28 -04001895 leaf = path->nodes[0];
1896 fi = btrfs_item_ptr(leaf, path->slots[0],
1897 struct btrfs_file_extent_item);
1898 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1899 btrfs_set_file_extent_type(leaf, fi, extent_type);
1900 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1901 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1902 btrfs_set_file_extent_offset(leaf, fi, 0);
1903 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1904 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1905 btrfs_set_file_extent_compression(leaf, fi, compression);
1906 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1907 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04001908
Yan Zhengd899e052008-10-30 14:25:28 -04001909 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04001910 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04001911
1912 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04001913
1914 ins.objectid = disk_bytenr;
1915 ins.offset = disk_num_bytes;
1916 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001917 ret = btrfs_alloc_reserved_file_extent(trans, root,
1918 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08001919 btrfs_ino(inode), file_pos, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001920out:
Yan Zhengd899e052008-10-30 14:25:28 -04001921 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04001922
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001923 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04001924}
1925
Liu Bo38c227d2013-01-29 03:18:40 +00001926/* snapshot-aware defrag */
1927struct sa_defrag_extent_backref {
1928 struct rb_node node;
1929 struct old_sa_defrag_extent *old;
1930 u64 root_id;
1931 u64 inum;
1932 u64 file_pos;
1933 u64 extent_offset;
1934 u64 num_bytes;
1935 u64 generation;
1936};
1937
1938struct old_sa_defrag_extent {
1939 struct list_head list;
1940 struct new_sa_defrag_extent *new;
1941
1942 u64 extent_offset;
1943 u64 bytenr;
1944 u64 offset;
1945 u64 len;
1946 int count;
1947};
1948
1949struct new_sa_defrag_extent {
1950 struct rb_root root;
1951 struct list_head head;
1952 struct btrfs_path *path;
1953 struct inode *inode;
1954 u64 file_pos;
1955 u64 len;
1956 u64 bytenr;
1957 u64 disk_len;
1958 u8 compress_type;
1959};
1960
1961static int backref_comp(struct sa_defrag_extent_backref *b1,
1962 struct sa_defrag_extent_backref *b2)
1963{
1964 if (b1->root_id < b2->root_id)
1965 return -1;
1966 else if (b1->root_id > b2->root_id)
1967 return 1;
1968
1969 if (b1->inum < b2->inum)
1970 return -1;
1971 else if (b1->inum > b2->inum)
1972 return 1;
1973
1974 if (b1->file_pos < b2->file_pos)
1975 return -1;
1976 else if (b1->file_pos > b2->file_pos)
1977 return 1;
1978
1979 /*
1980 * [------------------------------] ===> (a range of space)
1981 * |<--->| |<---->| =============> (fs/file tree A)
1982 * |<---------------------------->| ===> (fs/file tree B)
1983 *
1984 * A range of space can refer to two file extents in one tree while
1985 * refer to only one file extent in another tree.
1986 *
1987 * So we may process a disk offset more than one time(two extents in A)
1988 * and locate at the same extent(one extent in B), then insert two same
1989 * backrefs(both refer to the extent in B).
1990 */
1991 return 0;
1992}
1993
1994static void backref_insert(struct rb_root *root,
1995 struct sa_defrag_extent_backref *backref)
1996{
1997 struct rb_node **p = &root->rb_node;
1998 struct rb_node *parent = NULL;
1999 struct sa_defrag_extent_backref *entry;
2000 int ret;
2001
2002 while (*p) {
2003 parent = *p;
2004 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2005
2006 ret = backref_comp(backref, entry);
2007 if (ret < 0)
2008 p = &(*p)->rb_left;
2009 else
2010 p = &(*p)->rb_right;
2011 }
2012
2013 rb_link_node(&backref->node, parent, p);
2014 rb_insert_color(&backref->node, root);
2015}
2016
2017/*
2018 * Note the backref might has changed, and in this case we just return 0.
2019 */
2020static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2021 void *ctx)
2022{
2023 struct btrfs_file_extent_item *extent;
2024 struct btrfs_fs_info *fs_info;
2025 struct old_sa_defrag_extent *old = ctx;
2026 struct new_sa_defrag_extent *new = old->new;
2027 struct btrfs_path *path = new->path;
2028 struct btrfs_key key;
2029 struct btrfs_root *root;
2030 struct sa_defrag_extent_backref *backref;
2031 struct extent_buffer *leaf;
2032 struct inode *inode = new->inode;
2033 int slot;
2034 int ret;
2035 u64 extent_offset;
2036 u64 num_bytes;
2037
2038 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
2039 inum == btrfs_ino(inode))
2040 return 0;
2041
2042 key.objectid = root_id;
2043 key.type = BTRFS_ROOT_ITEM_KEY;
2044 key.offset = (u64)-1;
2045
2046 fs_info = BTRFS_I(inode)->root->fs_info;
2047 root = btrfs_read_fs_root_no_name(fs_info, &key);
2048 if (IS_ERR(root)) {
2049 if (PTR_ERR(root) == -ENOENT)
2050 return 0;
2051 WARN_ON(1);
2052 pr_debug("inum=%llu, offset=%llu, root_id=%llu\n",
2053 inum, offset, root_id);
2054 return PTR_ERR(root);
2055 }
2056
2057 key.objectid = inum;
2058 key.type = BTRFS_EXTENT_DATA_KEY;
2059 if (offset > (u64)-1 << 32)
2060 key.offset = 0;
2061 else
2062 key.offset = offset;
2063
2064 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302065 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002066 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002067 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002068
2069 while (1) {
2070 cond_resched();
2071
2072 leaf = path->nodes[0];
2073 slot = path->slots[0];
2074
2075 if (slot >= btrfs_header_nritems(leaf)) {
2076 ret = btrfs_next_leaf(root, path);
2077 if (ret < 0) {
2078 goto out;
2079 } else if (ret > 0) {
2080 ret = 0;
2081 goto out;
2082 }
2083 continue;
2084 }
2085
2086 path->slots[0]++;
2087
2088 btrfs_item_key_to_cpu(leaf, &key, slot);
2089
2090 if (key.objectid > inum)
2091 goto out;
2092
2093 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2094 continue;
2095
2096 extent = btrfs_item_ptr(leaf, slot,
2097 struct btrfs_file_extent_item);
2098
2099 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2100 continue;
2101
Liu Boe68afa42013-07-01 22:13:26 +08002102 /*
2103 * 'offset' refers to the exact key.offset,
2104 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2105 * (key.offset - extent_offset).
2106 */
2107 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002108 continue;
2109
Liu Boe68afa42013-07-01 22:13:26 +08002110 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002111 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002112
Liu Bo38c227d2013-01-29 03:18:40 +00002113 if (extent_offset >= old->extent_offset + old->offset +
2114 old->len || extent_offset + num_bytes <=
2115 old->extent_offset + old->offset)
2116 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002117 break;
2118 }
2119
2120 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2121 if (!backref) {
2122 ret = -ENOENT;
2123 goto out;
2124 }
2125
2126 backref->root_id = root_id;
2127 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002128 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002129 backref->num_bytes = num_bytes;
2130 backref->extent_offset = extent_offset;
2131 backref->generation = btrfs_file_extent_generation(leaf, extent);
2132 backref->old = old;
2133 backref_insert(&new->root, backref);
2134 old->count++;
2135out:
2136 btrfs_release_path(path);
2137 WARN_ON(ret);
2138 return ret;
2139}
2140
2141static noinline bool record_extent_backrefs(struct btrfs_path *path,
2142 struct new_sa_defrag_extent *new)
2143{
2144 struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info;
2145 struct old_sa_defrag_extent *old, *tmp;
2146 int ret;
2147
2148 new->path = path;
2149
2150 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002151 ret = iterate_inodes_from_logical(old->bytenr +
2152 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002153 path, record_one_backref,
2154 old);
Josef Bacik4724b102013-11-05 11:11:40 -05002155 if (ret < 0 && ret != -ENOENT)
2156 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002157
2158 /* no backref to be processed for this extent */
2159 if (!old->count) {
2160 list_del(&old->list);
2161 kfree(old);
2162 }
2163 }
2164
2165 if (list_empty(&new->head))
2166 return false;
2167
2168 return true;
2169}
2170
2171static int relink_is_mergable(struct extent_buffer *leaf,
2172 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002173 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002174{
Liu Bo116e0022013-08-02 16:30:40 +08002175 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002176 return 0;
2177
2178 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2179 return 0;
2180
Liu Bo116e0022013-08-02 16:30:40 +08002181 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2182 return 0;
2183
2184 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002185 btrfs_file_extent_other_encoding(leaf, fi))
2186 return 0;
2187
2188 return 1;
2189}
2190
2191/*
2192 * Note the backref might has changed, and in this case we just return 0.
2193 */
2194static noinline int relink_extent_backref(struct btrfs_path *path,
2195 struct sa_defrag_extent_backref *prev,
2196 struct sa_defrag_extent_backref *backref)
2197{
2198 struct btrfs_file_extent_item *extent;
2199 struct btrfs_file_extent_item *item;
2200 struct btrfs_ordered_extent *ordered;
2201 struct btrfs_trans_handle *trans;
2202 struct btrfs_fs_info *fs_info;
2203 struct btrfs_root *root;
2204 struct btrfs_key key;
2205 struct extent_buffer *leaf;
2206 struct old_sa_defrag_extent *old = backref->old;
2207 struct new_sa_defrag_extent *new = old->new;
2208 struct inode *src_inode = new->inode;
2209 struct inode *inode;
2210 struct extent_state *cached = NULL;
2211 int ret = 0;
2212 u64 start;
2213 u64 len;
2214 u64 lock_start;
2215 u64 lock_end;
2216 bool merge = false;
2217 int index;
2218
2219 if (prev && prev->root_id == backref->root_id &&
2220 prev->inum == backref->inum &&
2221 prev->file_pos + prev->num_bytes == backref->file_pos)
2222 merge = true;
2223
2224 /* step 1: get root */
2225 key.objectid = backref->root_id;
2226 key.type = BTRFS_ROOT_ITEM_KEY;
2227 key.offset = (u64)-1;
2228
2229 fs_info = BTRFS_I(src_inode)->root->fs_info;
2230 index = srcu_read_lock(&fs_info->subvol_srcu);
2231
2232 root = btrfs_read_fs_root_no_name(fs_info, &key);
2233 if (IS_ERR(root)) {
2234 srcu_read_unlock(&fs_info->subvol_srcu, index);
2235 if (PTR_ERR(root) == -ENOENT)
2236 return 0;
2237 return PTR_ERR(root);
2238 }
Liu Bo38c227d2013-01-29 03:18:40 +00002239
2240 /* step 2: get inode */
2241 key.objectid = backref->inum;
2242 key.type = BTRFS_INODE_ITEM_KEY;
2243 key.offset = 0;
2244
2245 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2246 if (IS_ERR(inode)) {
2247 srcu_read_unlock(&fs_info->subvol_srcu, index);
2248 return 0;
2249 }
2250
2251 srcu_read_unlock(&fs_info->subvol_srcu, index);
2252
2253 /* step 3: relink backref */
2254 lock_start = backref->file_pos;
2255 lock_end = backref->file_pos + backref->num_bytes - 1;
2256 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2257 0, &cached);
2258
2259 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2260 if (ordered) {
2261 btrfs_put_ordered_extent(ordered);
2262 goto out_unlock;
2263 }
2264
2265 trans = btrfs_join_transaction(root);
2266 if (IS_ERR(trans)) {
2267 ret = PTR_ERR(trans);
2268 goto out_unlock;
2269 }
2270
2271 key.objectid = backref->inum;
2272 key.type = BTRFS_EXTENT_DATA_KEY;
2273 key.offset = backref->file_pos;
2274
2275 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2276 if (ret < 0) {
2277 goto out_free_path;
2278 } else if (ret > 0) {
2279 ret = 0;
2280 goto out_free_path;
2281 }
2282
2283 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2284 struct btrfs_file_extent_item);
2285
2286 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2287 backref->generation)
2288 goto out_free_path;
2289
2290 btrfs_release_path(path);
2291
2292 start = backref->file_pos;
2293 if (backref->extent_offset < old->extent_offset + old->offset)
2294 start += old->extent_offset + old->offset -
2295 backref->extent_offset;
2296
2297 len = min(backref->extent_offset + backref->num_bytes,
2298 old->extent_offset + old->offset + old->len);
2299 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2300
2301 ret = btrfs_drop_extents(trans, root, inode, start,
2302 start + len, 1);
2303 if (ret)
2304 goto out_free_path;
2305again:
2306 key.objectid = btrfs_ino(inode);
2307 key.type = BTRFS_EXTENT_DATA_KEY;
2308 key.offset = start;
2309
Liu Boa09a0a72013-03-11 09:20:58 +00002310 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002311 if (merge) {
2312 struct btrfs_file_extent_item *fi;
2313 u64 extent_len;
2314 struct btrfs_key found_key;
2315
2316 ret = btrfs_search_slot(trans, root, &key, path, 1, 1);
2317 if (ret < 0)
2318 goto out_free_path;
2319
2320 path->slots[0]--;
2321 leaf = path->nodes[0];
2322 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2323
2324 fi = btrfs_item_ptr(leaf, path->slots[0],
2325 struct btrfs_file_extent_item);
2326 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2327
Liu Bo116e0022013-08-02 16:30:40 +08002328 if (extent_len + found_key.offset == start &&
2329 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002330 btrfs_set_file_extent_num_bytes(leaf, fi,
2331 extent_len + len);
2332 btrfs_mark_buffer_dirty(leaf);
2333 inode_add_bytes(inode, len);
2334
2335 ret = 1;
2336 goto out_free_path;
2337 } else {
2338 merge = false;
2339 btrfs_release_path(path);
2340 goto again;
2341 }
2342 }
2343
2344 ret = btrfs_insert_empty_item(trans, root, path, &key,
2345 sizeof(*extent));
2346 if (ret) {
2347 btrfs_abort_transaction(trans, root, ret);
2348 goto out_free_path;
2349 }
2350
2351 leaf = path->nodes[0];
2352 item = btrfs_item_ptr(leaf, path->slots[0],
2353 struct btrfs_file_extent_item);
2354 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2355 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2356 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2357 btrfs_set_file_extent_num_bytes(leaf, item, len);
2358 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2359 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2360 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2361 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2362 btrfs_set_file_extent_encryption(leaf, item, 0);
2363 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2364
2365 btrfs_mark_buffer_dirty(leaf);
2366 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002367 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002368
2369 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
2370 new->disk_len, 0,
2371 backref->root_id, backref->inum,
2372 new->file_pos, 0); /* start - extent_offset */
2373 if (ret) {
2374 btrfs_abort_transaction(trans, root, ret);
2375 goto out_free_path;
2376 }
2377
2378 ret = 1;
2379out_free_path:
2380 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002381 path->leave_spinning = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002382 btrfs_end_transaction(trans, root);
2383out_unlock:
2384 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2385 &cached, GFP_NOFS);
2386 iput(inode);
2387 return ret;
2388}
2389
Liu Bo6f519562013-10-29 10:45:05 +08002390static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2391{
2392 struct old_sa_defrag_extent *old, *tmp;
2393
2394 if (!new)
2395 return;
2396
2397 list_for_each_entry_safe(old, tmp, &new->head, list) {
2398 list_del(&old->list);
2399 kfree(old);
2400 }
2401 kfree(new);
2402}
2403
Liu Bo38c227d2013-01-29 03:18:40 +00002404static void relink_file_extents(struct new_sa_defrag_extent *new)
2405{
2406 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002407 struct sa_defrag_extent_backref *backref;
2408 struct sa_defrag_extent_backref *prev = NULL;
2409 struct inode *inode;
2410 struct btrfs_root *root;
2411 struct rb_node *node;
2412 int ret;
2413
2414 inode = new->inode;
2415 root = BTRFS_I(inode)->root;
2416
2417 path = btrfs_alloc_path();
2418 if (!path)
2419 return;
2420
2421 if (!record_extent_backrefs(path, new)) {
2422 btrfs_free_path(path);
2423 goto out;
2424 }
2425 btrfs_release_path(path);
2426
2427 while (1) {
2428 node = rb_first(&new->root);
2429 if (!node)
2430 break;
2431 rb_erase(node, &new->root);
2432
2433 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2434
2435 ret = relink_extent_backref(path, prev, backref);
2436 WARN_ON(ret < 0);
2437
2438 kfree(prev);
2439
2440 if (ret == 1)
2441 prev = backref;
2442 else
2443 prev = NULL;
2444 cond_resched();
2445 }
2446 kfree(prev);
2447
2448 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002449out:
Liu Bo6f519562013-10-29 10:45:05 +08002450 free_sa_defrag_extent(new);
2451
Liu Bo38c227d2013-01-29 03:18:40 +00002452 atomic_dec(&root->fs_info->defrag_running);
2453 wake_up(&root->fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002454}
2455
2456static struct new_sa_defrag_extent *
2457record_old_file_extents(struct inode *inode,
2458 struct btrfs_ordered_extent *ordered)
2459{
2460 struct btrfs_root *root = BTRFS_I(inode)->root;
2461 struct btrfs_path *path;
2462 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002463 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002464 struct new_sa_defrag_extent *new;
2465 int ret;
2466
2467 new = kmalloc(sizeof(*new), GFP_NOFS);
2468 if (!new)
2469 return NULL;
2470
2471 new->inode = inode;
2472 new->file_pos = ordered->file_offset;
2473 new->len = ordered->len;
2474 new->bytenr = ordered->start;
2475 new->disk_len = ordered->disk_len;
2476 new->compress_type = ordered->compress_type;
2477 new->root = RB_ROOT;
2478 INIT_LIST_HEAD(&new->head);
2479
2480 path = btrfs_alloc_path();
2481 if (!path)
2482 goto out_kfree;
2483
2484 key.objectid = btrfs_ino(inode);
2485 key.type = BTRFS_EXTENT_DATA_KEY;
2486 key.offset = new->file_pos;
2487
2488 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2489 if (ret < 0)
2490 goto out_free_path;
2491 if (ret > 0 && path->slots[0] > 0)
2492 path->slots[0]--;
2493
2494 /* find out all the old extents for the file range */
2495 while (1) {
2496 struct btrfs_file_extent_item *extent;
2497 struct extent_buffer *l;
2498 int slot;
2499 u64 num_bytes;
2500 u64 offset;
2501 u64 end;
2502 u64 disk_bytenr;
2503 u64 extent_offset;
2504
2505 l = path->nodes[0];
2506 slot = path->slots[0];
2507
2508 if (slot >= btrfs_header_nritems(l)) {
2509 ret = btrfs_next_leaf(root, path);
2510 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002511 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002512 else if (ret > 0)
2513 break;
2514 continue;
2515 }
2516
2517 btrfs_item_key_to_cpu(l, &key, slot);
2518
2519 if (key.objectid != btrfs_ino(inode))
2520 break;
2521 if (key.type != BTRFS_EXTENT_DATA_KEY)
2522 break;
2523 if (key.offset >= new->file_pos + new->len)
2524 break;
2525
2526 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2527
2528 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2529 if (key.offset + num_bytes < new->file_pos)
2530 goto next;
2531
2532 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2533 if (!disk_bytenr)
2534 goto next;
2535
2536 extent_offset = btrfs_file_extent_offset(l, extent);
2537
2538 old = kmalloc(sizeof(*old), GFP_NOFS);
2539 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002540 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002541
2542 offset = max(new->file_pos, key.offset);
2543 end = min(new->file_pos + new->len, key.offset + num_bytes);
2544
2545 old->bytenr = disk_bytenr;
2546 old->extent_offset = extent_offset;
2547 old->offset = offset - key.offset;
2548 old->len = end - offset;
2549 old->new = new;
2550 old->count = 0;
2551 list_add_tail(&old->list, &new->head);
2552next:
2553 path->slots[0]++;
2554 cond_resched();
2555 }
2556
2557 btrfs_free_path(path);
2558 atomic_inc(&root->fs_info->defrag_running);
2559
2560 return new;
2561
Liu Bo38c227d2013-01-29 03:18:40 +00002562out_free_path:
2563 btrfs_free_path(path);
2564out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002565 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002566 return NULL;
2567}
2568
Chris Masond352ac62008-09-29 15:18:18 -04002569/* as ordered data IO finishes, this gets called so we can finish
2570 * an ordered extent if the range of bytes in the file it covers are
2571 * fully written.
2572 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002573static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002574{
Josef Bacik5fd02042012-05-02 14:00:54 -04002575 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002576 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002577 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002578 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002579 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002580 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002581 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002582 int ret = 0;
2583 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002584 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002585 bool truncated = false;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002586
Liu Bo83eea1f2012-07-10 05:28:39 -06002587 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002588
Josef Bacik5fd02042012-05-02 14:00:54 -04002589 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2590 ret = -EIO;
2591 goto out;
2592 }
2593
Josef Bacik77cef2e2013-08-29 13:57:21 -04002594 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2595 truncated = true;
2596 logical_len = ordered_extent->truncated_len;
2597 /* Truncated the entire extent, don't bother adding */
2598 if (!logical_len)
2599 goto out;
2600 }
2601
Yan, Zhengc2167752009-11-12 09:34:21 +00002602 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002603 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Josef Bacik6c760c02012-11-09 10:53:21 -05002604 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2605 if (nolock)
2606 trans = btrfs_join_transaction_nolock(root);
2607 else
2608 trans = btrfs_join_transaction(root);
2609 if (IS_ERR(trans)) {
2610 ret = PTR_ERR(trans);
2611 trans = NULL;
2612 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002613 }
Josef Bacik6c760c02012-11-09 10:53:21 -05002614 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
2615 ret = btrfs_update_inode_fallback(trans, root, inode);
2616 if (ret) /* -ENOMEM or corruption */
2617 btrfs_abort_transaction(trans, root, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002618 goto out;
2619 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002620
Josef Bacik2ac55d42010-02-03 19:33:23 +00002621 lock_extent_bits(io_tree, ordered_extent->file_offset,
2622 ordered_extent->file_offset + ordered_extent->len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002623 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002624
Liu Bo38c227d2013-01-29 03:18:40 +00002625 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2626 ordered_extent->file_offset + ordered_extent->len - 1,
2627 EXTENT_DEFRAG, 1, cached_state);
2628 if (ret) {
2629 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
2630 if (last_snapshot >= BTRFS_I(inode)->generation)
2631 /* the inode is shared */
2632 new = record_old_file_extents(inode, ordered_extent);
2633
2634 clear_extent_bit(io_tree, ordered_extent->file_offset,
2635 ordered_extent->file_offset + ordered_extent->len - 1,
2636 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2637 }
2638
Josef Bacik0cb59c92010-07-02 12:14:14 -04002639 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002640 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002641 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002642 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002643 if (IS_ERR(trans)) {
2644 ret = PTR_ERR(trans);
2645 trans = NULL;
2646 goto out_unlock;
2647 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002648 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002649
Chris Masonc8b97812008-10-29 14:49:59 -04002650 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002651 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002652 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002653 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00002654 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002655 ordered_extent->file_offset,
2656 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002657 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002658 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04002659 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002660 ret = insert_reserved_file_extent(trans, inode,
2661 ordered_extent->file_offset,
2662 ordered_extent->start,
2663 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04002664 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08002665 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002666 BTRFS_FILE_EXTENT_REG);
Yan Zhengd899e052008-10-30 14:25:28 -04002667 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002668 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2669 ordered_extent->file_offset, ordered_extent->len,
2670 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002671 if (ret < 0) {
2672 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002673 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002674 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002675
Chris Masone6dcd2d2008-07-17 12:53:50 -04002676 add_pending_csums(trans, inode, ordered_extent->file_offset,
2677 &ordered_extent->list);
2678
Josef Bacik6c760c02012-11-09 10:53:21 -05002679 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2680 ret = btrfs_update_inode_fallback(trans, root, inode);
2681 if (ret) { /* -ENOMEM or corruption */
2682 btrfs_abort_transaction(trans, root, ret);
2683 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002684 }
2685 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002686out_unlock:
2687 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2688 ordered_extent->file_offset +
2689 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00002690out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002691 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04002692 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002693 if (trans)
2694 btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002695
Josef Bacik77cef2e2013-08-29 13:57:21 -04002696 if (ret || truncated) {
2697 u64 start, end;
2698
2699 if (truncated)
2700 start = ordered_extent->file_offset + logical_len;
2701 else
2702 start = ordered_extent->file_offset;
2703 end = ordered_extent->file_offset + ordered_extent->len - 1;
2704 clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS);
2705
2706 /* Drop the cache for the part of the extent we didn't write. */
2707 btrfs_drop_extent_cache(inode, start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04002708
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002709 /*
2710 * If the ordered extent had an IOERR or something else went
2711 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04002712 * back to the allocator. We only free the extent in the
2713 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002714 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04002715 if ((ret || !logical_len) &&
2716 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002717 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
2718 btrfs_free_reserved_extent(root, ordered_extent->start,
2719 ordered_extent->disk_len);
2720 }
2721
2722
Josef Bacik5fd02042012-05-02 14:00:54 -04002723 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002724 * This needs to be done to make sure anybody waiting knows we are done
2725 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002726 */
2727 btrfs_remove_ordered_extent(inode, ordered_extent);
2728
Liu Bo38c227d2013-01-29 03:18:40 +00002729 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08002730 if (new) {
2731 if (ret) {
2732 free_sa_defrag_extent(new);
2733 atomic_dec(&root->fs_info->defrag_running);
2734 } else {
2735 relink_file_extents(new);
2736 }
2737 }
Liu Bo38c227d2013-01-29 03:18:40 +00002738
Chris Masone6dcd2d2008-07-17 12:53:50 -04002739 /* once for us */
2740 btrfs_put_ordered_extent(ordered_extent);
2741 /* once for the tree */
2742 btrfs_put_ordered_extent(ordered_extent);
2743
Josef Bacik5fd02042012-05-02 14:00:54 -04002744 return ret;
2745}
2746
2747static void finish_ordered_fn(struct btrfs_work *work)
2748{
2749 struct btrfs_ordered_extent *ordered_extent;
2750 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2751 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002752}
2753
Christoph Hellwigb2950862008-12-02 09:54:17 -05002754static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04002755 struct extent_state *state, int uptodate)
2756{
Josef Bacik5fd02042012-05-02 14:00:54 -04002757 struct inode *inode = page->mapping->host;
2758 struct btrfs_root *root = BTRFS_I(inode)->root;
2759 struct btrfs_ordered_extent *ordered_extent = NULL;
2760 struct btrfs_workers *workers;
2761
liubo1abe9b82011-03-24 11:18:59 +00002762 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2763
Chris Mason8b62b722009-09-02 16:53:46 -04002764 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002765 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2766 end - start + 1, uptodate))
2767 return 0;
2768
2769 ordered_extent->work.func = finish_ordered_fn;
2770 ordered_extent->work.flags = 0;
2771
Liu Bo83eea1f2012-07-10 05:28:39 -06002772 if (btrfs_is_free_space_inode(inode))
Josef Bacik5fd02042012-05-02 14:00:54 -04002773 workers = &root->fs_info->endio_freespace_worker;
2774 else
2775 workers = &root->fs_info->endio_write_workers;
2776 btrfs_queue_worker(workers, &ordered_extent->work);
2777
2778 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04002779}
2780
Chris Masond352ac62008-09-29 15:18:18 -04002781/*
Chris Masond352ac62008-09-29 15:18:18 -04002782 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002783 * if there's a match, we allow the bio to finish. If not, the code in
2784 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002785 */
Miao Xiefacc8a222013-07-25 19:22:34 +08002786static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
2787 u64 phy_offset, struct page *page,
2788 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002789{
Miao Xie4eee4fa2012-12-21 09:17:45 +00002790 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04002791 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002792 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04002793 char *kaddr;
Chris Masonff79f812007-10-15 16:22:25 -04002794 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiefacc8a222013-07-25 19:22:34 +08002795 u32 csum_expected;
Chris Masonff79f812007-10-15 16:22:25 -04002796 u32 csum = ~(u32)0;
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002797 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
2798 DEFAULT_RATELIMIT_BURST);
Chris Masond1310b22008-01-24 16:13:08 -05002799
Chris Masond20f7042008-12-08 16:58:54 -05002800 if (PageChecked(page)) {
2801 ClearPageChecked(page);
2802 goto good;
2803 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002804
2805 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Jan Schmidt08d2f342011-05-04 16:18:50 +02002806 goto good;
Chris Masond20f7042008-12-08 16:58:54 -05002807
Yan Zheng17d217f2008-12-12 10:03:38 -05002808 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002809 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002810 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
2811 GFP_NOFS);
2812 return 0;
2813 }
2814
Miao Xiefacc8a222013-07-25 19:22:34 +08002815 phy_offset >>= inode->i_sb->s_blocksize_bits;
2816 csum_expected = *(((u32 *)io_bio->csum) + phy_offset);
Chris Masond3977122009-01-05 21:25:51 -05002817
Miao Xiefacc8a222013-07-25 19:22:34 +08002818 kaddr = kmap_atomic(page);
Liu Bob0496682013-03-14 14:57:45 +00002819 csum = btrfs_csum_data(kaddr + offset, csum, end - start + 1);
Chris Masonff79f812007-10-15 16:22:25 -04002820 btrfs_csum_final(csum, (char *)&csum);
Miao Xiefacc8a222013-07-25 19:22:34 +08002821 if (csum != csum_expected)
Chris Mason07157aa2007-08-30 08:50:51 -04002822 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002823
Cong Wang7ac687d2011-11-25 23:14:28 +08002824 kunmap_atomic(kaddr);
Chris Masond20f7042008-12-08 16:58:54 -05002825good:
Chris Mason07157aa2007-08-30 08:50:51 -04002826 return 0;
2827
2828zeroit:
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002829 if (__ratelimit(&_rs))
Miao Xiefacc8a222013-07-25 19:22:34 +08002830 btrfs_info(root->fs_info, "csum failed ino %llu off %llu csum %u expected csum %u",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002831 btrfs_ino(page->mapping->host), start, csum, csum_expected);
Chris Masondb945352007-10-15 16:15:53 -04002832 memset(kaddr + offset, 1, end - start + 1);
2833 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002834 kunmap_atomic(kaddr);
Miao Xiefacc8a222013-07-25 19:22:34 +08002835 if (csum_expected == 0)
Chris Mason3b951512008-04-17 11:29:12 -04002836 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04002837 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04002838}
Chris Masonb888db2b2007-08-27 16:49:44 -04002839
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002840struct delayed_iput {
2841 struct list_head list;
2842 struct inode *inode;
2843};
2844
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002845/* JDM: If this is fs-wide, why can't we add a pointer to
2846 * btrfs_inode instead and avoid the allocation? */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002847void btrfs_add_delayed_iput(struct inode *inode)
2848{
2849 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2850 struct delayed_iput *delayed;
2851
2852 if (atomic_add_unless(&inode->i_count, -1, 1))
2853 return;
2854
2855 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2856 delayed->inode = inode;
2857
2858 spin_lock(&fs_info->delayed_iput_lock);
2859 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2860 spin_unlock(&fs_info->delayed_iput_lock);
2861}
2862
2863void btrfs_run_delayed_iputs(struct btrfs_root *root)
2864{
2865 LIST_HEAD(list);
2866 struct btrfs_fs_info *fs_info = root->fs_info;
2867 struct delayed_iput *delayed;
2868 int empty;
2869
2870 spin_lock(&fs_info->delayed_iput_lock);
2871 empty = list_empty(&fs_info->delayed_iputs);
2872 spin_unlock(&fs_info->delayed_iput_lock);
2873 if (empty)
2874 return;
2875
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002876 spin_lock(&fs_info->delayed_iput_lock);
2877 list_splice_init(&fs_info->delayed_iputs, &list);
2878 spin_unlock(&fs_info->delayed_iput_lock);
2879
2880 while (!list_empty(&list)) {
2881 delayed = list_entry(list.next, struct delayed_iput, list);
2882 list_del(&delayed->list);
2883 iput(delayed->inode);
2884 kfree(delayed);
2885 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002886}
2887
Yan, Zhengd68fc572010-05-16 10:49:58 -04002888/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08002889 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04002890 * files in the subvolume, it removes orphan item and frees block_rsv
2891 * structure.
2892 */
2893void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
2894 struct btrfs_root *root)
2895{
Josef Bacik90290e12011-12-02 15:44:12 -05002896 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002897 int ret;
2898
Josef Bacik8a35d952012-05-23 14:26:42 -04002899 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04002900 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
2901 return;
2902
Josef Bacik90290e12011-12-02 15:44:12 -05002903 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04002904 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05002905 spin_unlock(&root->orphan_lock);
2906 return;
2907 }
2908
2909 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
2910 spin_unlock(&root->orphan_lock);
2911 return;
2912 }
2913
2914 block_rsv = root->orphan_block_rsv;
2915 root->orphan_block_rsv = NULL;
2916 spin_unlock(&root->orphan_lock);
2917
Yan, Zhengd68fc572010-05-16 10:49:58 -04002918 if (root->orphan_item_inserted &&
2919 btrfs_root_refs(&root->root_item) > 0) {
2920 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
2921 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04002922 if (ret)
2923 btrfs_abort_transaction(trans, root, ret);
2924 else
2925 root->orphan_item_inserted = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002926 }
2927
Josef Bacik90290e12011-12-02 15:44:12 -05002928 if (block_rsv) {
2929 WARN_ON(block_rsv->size > 0);
2930 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002931 }
2932}
2933
2934/*
Josef Bacik7b128762008-07-24 12:17:14 -04002935 * This creates an orphan entry for the given inode in case something goes
2936 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04002937 *
2938 * NOTE: caller of this function should reserve 5 units of metadata for
2939 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04002940 */
2941int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2942{
2943 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002944 struct btrfs_block_rsv *block_rsv = NULL;
2945 int reserve = 0;
2946 int insert = 0;
2947 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002948
Yan, Zhengd68fc572010-05-16 10:49:58 -04002949 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06002950 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00002951 if (!block_rsv)
2952 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04002953 }
2954
Yan, Zhengd68fc572010-05-16 10:49:58 -04002955 spin_lock(&root->orphan_lock);
2956 if (!root->orphan_block_rsv) {
2957 root->orphan_block_rsv = block_rsv;
2958 } else if (block_rsv) {
2959 btrfs_free_block_rsv(root, block_rsv);
2960 block_rsv = NULL;
2961 }
Josef Bacik7b128762008-07-24 12:17:14 -04002962
Josef Bacik8a35d952012-05-23 14:26:42 -04002963 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2964 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04002965#if 0
2966 /*
2967 * For proper ENOSPC handling, we should do orphan
2968 * cleanup when mounting. But this introduces backward
2969 * compatibility issue.
2970 */
2971 if (!xchg(&root->orphan_item_inserted, 1))
2972 insert = 2;
2973 else
2974 insert = 1;
2975#endif
2976 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06002977 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002978 }
Josef Bacik7b128762008-07-24 12:17:14 -04002979
Josef Bacik72ac3c02012-05-23 14:13:11 -04002980 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
2981 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04002982 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002983 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002984
Yan, Zhengd68fc572010-05-16 10:49:58 -04002985 /* grab metadata reservation from transaction handle */
2986 if (reserve) {
2987 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002988 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04002989 }
2990
2991 /* insert an orphan item to track this unlinked/truncated file */
2992 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08002993 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04002994 if (ret) {
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01002995 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04002996 if (reserve) {
2997 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
2998 &BTRFS_I(inode)->runtime_flags);
2999 btrfs_orphan_release_metadata(inode);
3000 }
3001 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003002 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3003 &BTRFS_I(inode)->runtime_flags);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003004 btrfs_abort_transaction(trans, root, ret);
3005 return ret;
3006 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003007 }
3008 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003009 }
3010
3011 /* insert an orphan item to track subvolume contains orphan files */
3012 if (insert >= 2) {
3013 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
3014 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003015 if (ret && ret != -EEXIST) {
3016 btrfs_abort_transaction(trans, root, ret);
3017 return ret;
3018 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003019 }
3020 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003021}
3022
3023/*
3024 * We have done the truncate/delete so we can go ahead and remove the orphan
3025 * item for this particular inode.
3026 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003027static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3028 struct inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003029{
3030 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003031 int delete_item = 0;
3032 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003033 int ret = 0;
3034
Yan, Zhengd68fc572010-05-16 10:49:58 -04003035 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003036 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3037 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003038 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003039
Josef Bacik72ac3c02012-05-23 14:13:11 -04003040 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3041 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003042 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003043 spin_unlock(&root->orphan_lock);
3044
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003045 if (delete_item) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003046 atomic_dec(&root->orphan_inodes);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003047 if (trans)
3048 ret = btrfs_del_orphan_item(trans, root,
3049 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04003050 }
Josef Bacik7b128762008-07-24 12:17:14 -04003051
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003052 if (release_rsv)
3053 btrfs_orphan_release_metadata(inode);
3054
Josef Bacik4ef31a42013-08-13 14:10:08 -04003055 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003056}
3057
3058/*
3059 * this cleans up any orphans that may be left on the list from the last use
3060 * of this root.
3061 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003062int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003063{
3064 struct btrfs_path *path;
3065 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003066 struct btrfs_key key, found_key;
3067 struct btrfs_trans_handle *trans;
3068 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003069 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003070 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3071
Yan, Zhengd68fc572010-05-16 10:49:58 -04003072 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003073 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003074
3075 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003076 if (!path) {
3077 ret = -ENOMEM;
3078 goto out;
3079 }
Josef Bacik7b128762008-07-24 12:17:14 -04003080 path->reada = -1;
3081
3082 key.objectid = BTRFS_ORPHAN_OBJECTID;
3083 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
3084 key.offset = (u64)-1;
3085
Josef Bacik7b128762008-07-24 12:17:14 -04003086 while (1) {
3087 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003088 if (ret < 0)
3089 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003090
3091 /*
3092 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003093 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003094 * find the key and see if we have stuff that matches
3095 */
3096 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003097 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003098 if (path->slots[0] == 0)
3099 break;
3100 path->slots[0]--;
3101 }
3102
3103 /* pull out the item */
3104 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003105 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3106
3107 /* make sure the item matches what we want */
3108 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3109 break;
3110 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
3111 break;
3112
3113 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003114 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003115
3116 /*
3117 * this is where we are basically btrfs_lookup, without the
3118 * crossing root thing. we store the inode number in the
3119 * offset of the orphan item.
3120 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003121
3122 if (found_key.offset == last_objectid) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003123 btrfs_err(root->fs_info,
3124 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003125 ret = -EINVAL;
3126 goto out;
3127 }
3128
3129 last_objectid = found_key.offset;
3130
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003131 found_key.objectid = found_key.offset;
3132 found_key.type = BTRFS_INODE_ITEM_KEY;
3133 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00003134 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303135 ret = PTR_ERR_OR_ZERO(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003136 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003137 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003138
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003139 if (ret == -ESTALE && root == root->fs_info->tree_root) {
3140 struct btrfs_root *dead_root;
3141 struct btrfs_fs_info *fs_info = root->fs_info;
3142 int is_dead_root = 0;
3143
3144 /*
3145 * this is an orphan in the tree root. Currently these
3146 * could come from 2 sources:
3147 * a) a snapshot deletion in progress
3148 * b) a free space cache inode
3149 * We need to distinguish those two, as the snapshot
3150 * orphan must not get deleted.
3151 * find_dead_roots already ran before us, so if this
3152 * is a snapshot deletion, we should find the root
3153 * in the dead_roots list
3154 */
3155 spin_lock(&fs_info->trans_lock);
3156 list_for_each_entry(dead_root, &fs_info->dead_roots,
3157 root_list) {
3158 if (dead_root->root_key.objectid ==
3159 found_key.objectid) {
3160 is_dead_root = 1;
3161 break;
3162 }
3163 }
3164 spin_unlock(&fs_info->trans_lock);
3165 if (is_dead_root) {
3166 /* prevent this orphan from being found again */
3167 key.offset = found_key.objectid - 1;
3168 continue;
3169 }
3170 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003171 /*
3172 * Inode is already gone but the orphan item is still there,
3173 * kill the orphan item.
3174 */
3175 if (ret == -ESTALE) {
3176 trans = btrfs_start_transaction(root, 1);
3177 if (IS_ERR(trans)) {
3178 ret = PTR_ERR(trans);
3179 goto out;
3180 }
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003181 btrfs_debug(root->fs_info, "auto deleting %Lu",
3182 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003183 ret = btrfs_del_orphan_item(trans, root,
3184 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003185 btrfs_end_transaction(trans, root);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003186 if (ret)
3187 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003188 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003189 }
Josef Bacik7b128762008-07-24 12:17:14 -04003190
Josef Bacik7b128762008-07-24 12:17:14 -04003191 /*
3192 * add this inode to the orphan list so btrfs_orphan_del does
3193 * the proper thing when we hit it
3194 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003195 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3196 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003197 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003198
Josef Bacik7b128762008-07-24 12:17:14 -04003199 /* if we have links, this was a truncate, lets do that */
3200 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303201 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003202 iput(inode);
3203 continue;
3204 }
Josef Bacik7b128762008-07-24 12:17:14 -04003205 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003206
3207 /* 1 for the orphan item deletion. */
3208 trans = btrfs_start_transaction(root, 1);
3209 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003210 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003211 ret = PTR_ERR(trans);
3212 goto out;
3213 }
3214 ret = btrfs_orphan_add(trans, inode);
3215 btrfs_end_transaction(trans, root);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003216 if (ret) {
3217 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003218 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003219 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003220
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003221 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003222 if (ret)
3223 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003224 } else {
3225 nr_unlink++;
3226 }
3227
3228 /* this will do delete_inode and everything for us */
3229 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003230 if (ret)
3231 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003232 }
Miao Xie3254c872011-11-10 20:45:05 -05003233 /* release the path since we're done with it */
3234 btrfs_release_path(path);
3235
Yan, Zhengd68fc572010-05-16 10:49:58 -04003236 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3237
3238 if (root->orphan_block_rsv)
3239 btrfs_block_rsv_release(root, root->orphan_block_rsv,
3240 (u64)-1);
3241
3242 if (root->orphan_block_rsv || root->orphan_item_inserted) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003243 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003244 if (!IS_ERR(trans))
3245 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003246 }
Josef Bacik7b128762008-07-24 12:17:14 -04003247
3248 if (nr_unlink)
David Sterba4884b472013-03-20 13:31:27 +00003249 btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003250 if (nr_truncate)
David Sterba4884b472013-03-20 13:31:27 +00003251 btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003252
3253out:
3254 if (ret)
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003255 btrfs_crit(root->fs_info,
3256 "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003257 btrfs_free_path(path);
3258 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003259}
3260
Chris Masond352ac62008-09-29 15:18:18 -04003261/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003262 * very simple check to peek ahead in the leaf looking for xattrs. If we
3263 * don't find any xattrs, we know there can't be any acls.
3264 *
3265 * slot is the slot the inode is in, objectid is the objectid of the inode
3266 */
3267static noinline int acls_after_inode_item(struct extent_buffer *leaf,
3268 int slot, u64 objectid)
3269{
3270 u32 nritems = btrfs_header_nritems(leaf);
3271 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003272 static u64 xattr_access = 0;
3273 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003274 int scanned = 0;
3275
Josef Bacikf23b5a52013-06-19 10:16:26 -04003276 if (!xattr_access) {
3277 xattr_access = btrfs_name_hash(POSIX_ACL_XATTR_ACCESS,
3278 strlen(POSIX_ACL_XATTR_ACCESS));
3279 xattr_default = btrfs_name_hash(POSIX_ACL_XATTR_DEFAULT,
3280 strlen(POSIX_ACL_XATTR_DEFAULT));
3281 }
3282
Chris Mason46a53cc2009-04-27 11:47:50 -04003283 slot++;
3284 while (slot < nritems) {
3285 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3286
3287 /* we found a different objectid, there must not be acls */
3288 if (found_key.objectid != objectid)
3289 return 0;
3290
3291 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003292 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
3293 if (found_key.offset == xattr_access ||
3294 found_key.offset == xattr_default)
3295 return 1;
3296 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003297
3298 /*
3299 * we found a key greater than an xattr key, there can't
3300 * be any acls later on
3301 */
3302 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3303 return 0;
3304
3305 slot++;
3306 scanned++;
3307
3308 /*
3309 * it goes inode, inode backrefs, xattrs, extents,
3310 * so if there are a ton of hard links to an inode there can
3311 * be a lot of backrefs. Don't waste time searching too hard,
3312 * this is just an optimization
3313 */
3314 if (scanned >= 8)
3315 break;
3316 }
3317 /* we hit the end of the leaf before we found an xattr or
3318 * something larger than an xattr. We have to assume the inode
3319 * has acls
3320 */
3321 return 1;
3322}
3323
3324/*
Chris Masond352ac62008-09-29 15:18:18 -04003325 * read an inode from the btree into the in-memory inode
3326 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003327static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003328{
3329 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003330 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003331 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04003332 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04003333 struct btrfs_root *root = BTRFS_I(inode)->root;
3334 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003335 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003336 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003337 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003338 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003339 bool filled = false;
3340
3341 ret = btrfs_fill_inode(inode, &rdev);
3342 if (!ret)
3343 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003344
3345 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07003346 if (!path)
3347 goto make_bad;
3348
Josef Bacikd90c7322011-05-17 09:50:54 -04003349 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003350 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003351
Chris Mason39279cc2007-06-12 06:35:45 -04003352 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003353 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04003354 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04003355
Chris Mason5f39d392007-10-15 16:14:19 -04003356 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003357
3358 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003359 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003360
Chris Mason5f39d392007-10-15 16:14:19 -04003361 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3362 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003363 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003364 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003365 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3366 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04003367 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003368
3369 tspec = btrfs_inode_atime(inode_item);
3370 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3371 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3372
3373 tspec = btrfs_inode_mtime(inode_item);
3374 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3375 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3376
3377 tspec = btrfs_inode_ctime(inode_item);
3378 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3379 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3380
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003381 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003382 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003383 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3384
3385 /*
3386 * If we were modified in the current generation and evicted from memory
3387 * and then re-read we need to do a full sync since we don't have any
3388 * idea about which extents were modified before we were evicted from
3389 * cache.
3390 */
3391 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
3392 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3393 &BTRFS_I(inode)->runtime_flags);
3394
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003395 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003396 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003397 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003398 rdev = btrfs_inode_rdev(leaf, inode_item);
3399
Josef Bacikaec74772008-07-24 12:12:38 -04003400 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003401 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003402
3403cache_index:
3404 path->slots[0]++;
3405 if (inode->i_nlink != 1 ||
3406 path->slots[0] >= btrfs_header_nritems(leaf))
3407 goto cache_acl;
3408
3409 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
3410 if (location.objectid != btrfs_ino(inode))
3411 goto cache_acl;
3412
3413 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3414 if (location.type == BTRFS_INODE_REF_KEY) {
3415 struct btrfs_inode_ref *ref;
3416
3417 ref = (struct btrfs_inode_ref *)ptr;
3418 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3419 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3420 struct btrfs_inode_extref *extref;
3421
3422 extref = (struct btrfs_inode_extref *)ptr;
3423 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3424 extref);
3425 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003426cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003427 /*
3428 * try to precache a NULL acl entry for files that don't have
3429 * any xattrs or acls
3430 */
Li Zefan33345d012011-04-20 10:31:50 +08003431 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
3432 btrfs_ino(inode));
Al Viro72c04902009-06-24 16:58:48 -04003433 if (!maybe_acls)
3434 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003435
Chris Mason39279cc2007-06-12 06:35:45 -04003436 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003437
Chris Mason39279cc2007-06-12 06:35:45 -04003438 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003439 case S_IFREG:
3440 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003441 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05003442 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003443 inode->i_fop = &btrfs_file_operations;
3444 inode->i_op = &btrfs_file_inode_operations;
3445 break;
3446 case S_IFDIR:
3447 inode->i_fop = &btrfs_dir_file_operations;
3448 if (root == root->fs_info->tree_root)
3449 inode->i_op = &btrfs_dir_ro_inode_operations;
3450 else
3451 inode->i_op = &btrfs_dir_inode_operations;
3452 break;
3453 case S_IFLNK:
3454 inode->i_op = &btrfs_symlink_inode_operations;
3455 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04003456 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003457 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003458 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003459 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003460 init_special_inode(inode, inode->i_mode, rdev);
3461 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003462 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003463
3464 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003465 return;
3466
3467make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003468 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003469 make_bad_inode(inode);
3470}
3471
Chris Masond352ac62008-09-29 15:18:18 -04003472/*
3473 * given a leaf and an inode, copy the inode fields into the leaf
3474 */
Chris Masone02119d2008-09-05 16:13:11 -04003475static void fill_inode_item(struct btrfs_trans_handle *trans,
3476 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003477 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003478 struct inode *inode)
3479{
Liu Bo51fab692012-12-27 09:01:21 +00003480 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003481
Liu Bo51fab692012-12-27 09:01:21 +00003482 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003483
Liu Bo51fab692012-12-27 09:01:21 +00003484 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3485 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3486 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3487 &token);
3488 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3489 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003490
Liu Bo51fab692012-12-27 09:01:21 +00003491 btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item),
3492 inode->i_atime.tv_sec, &token);
3493 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item),
3494 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003495
Liu Bo51fab692012-12-27 09:01:21 +00003496 btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item),
3497 inode->i_mtime.tv_sec, &token);
3498 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item),
3499 inode->i_mtime.tv_nsec, &token);
3500
3501 btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item),
3502 inode->i_ctime.tv_sec, &token);
3503 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item),
3504 inode->i_ctime.tv_nsec, &token);
3505
3506 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3507 &token);
3508 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3509 &token);
3510 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3511 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3512 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3513 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3514 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003515}
3516
Chris Masond352ac62008-09-29 15:18:18 -04003517/*
3518 * copy everything in the in-memory inode into the btree.
3519 */
Chris Mason21151332011-11-10 20:39:08 -05003520static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003521 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003522{
3523 struct btrfs_inode_item *inode_item;
3524 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003525 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003526 int ret;
3527
3528 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003529 if (!path)
3530 return -ENOMEM;
3531
Chris Masonb9473432009-03-13 11:00:37 -04003532 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003533 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3534 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003535 if (ret) {
3536 if (ret > 0)
3537 ret = -ENOENT;
3538 goto failed;
3539 }
3540
Chris Mason5f39d392007-10-15 16:14:19 -04003541 leaf = path->nodes[0];
3542 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003543 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003544
Chris Masone02119d2008-09-05 16:13:11 -04003545 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003546 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003547 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003548 ret = 0;
3549failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003550 btrfs_free_path(path);
3551 return ret;
3552}
3553
Chris Masond352ac62008-09-29 15:18:18 -04003554/*
Chris Mason21151332011-11-10 20:39:08 -05003555 * copy everything in the in-memory inode into the btree.
3556 */
3557noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3558 struct btrfs_root *root, struct inode *inode)
3559{
3560 int ret;
3561
3562 /*
3563 * If the inode is a free space inode, we can deadlock during commit
3564 * if we put it into the delayed code.
3565 *
3566 * The data relocation inode should also be directly updated
3567 * without delay
3568 */
Liu Bo83eea1f2012-07-10 05:28:39 -06003569 if (!btrfs_is_free_space_inode(inode)
Chris Mason21151332011-11-10 20:39:08 -05003570 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003571 btrfs_update_root_times(trans, root);
3572
Chris Mason21151332011-11-10 20:39:08 -05003573 ret = btrfs_delayed_update_inode(trans, root, inode);
3574 if (!ret)
3575 btrfs_set_inode_last_trans(trans, inode);
3576 return ret;
3577 }
3578
3579 return btrfs_update_inode_item(trans, root, inode);
3580}
3581
Josef Bacikbe6aef62012-10-22 15:43:12 -04003582noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3583 struct btrfs_root *root,
3584 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003585{
3586 int ret;
3587
3588 ret = btrfs_update_inode(trans, root, inode);
3589 if (ret == -ENOSPC)
3590 return btrfs_update_inode_item(trans, root, inode);
3591 return ret;
3592}
3593
3594/*
Chris Masond352ac62008-09-29 15:18:18 -04003595 * unlink helper that gets used here in inode.c and in the tree logging
3596 * recovery code. It remove a link in a directory with a given name, and
3597 * also drops the back refs in the inode to the directory
3598 */
Al Viro92986792011-03-04 17:14:37 +00003599static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3600 struct btrfs_root *root,
3601 struct inode *dir, struct inode *inode,
3602 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003603{
3604 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003605 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003606 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003607 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003608 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003609 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003610 u64 ino = btrfs_ino(inode);
3611 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003612
3613 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003614 if (!path) {
3615 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003616 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003617 }
3618
Chris Masonb9473432009-03-13 11:00:37 -04003619 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003620 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003621 name, name_len, -1);
3622 if (IS_ERR(di)) {
3623 ret = PTR_ERR(di);
3624 goto err;
3625 }
3626 if (!di) {
3627 ret = -ENOENT;
3628 goto err;
3629 }
Chris Mason5f39d392007-10-15 16:14:19 -04003630 leaf = path->nodes[0];
3631 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003632 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003633 if (ret)
3634 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003635 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003636
Miao Xie67de1172013-12-26 13:07:06 +08003637 /*
3638 * If we don't have dir index, we have to get it by looking up
3639 * the inode ref, since we get the inode ref, remove it directly,
3640 * it is unnecessary to do delayed deletion.
3641 *
3642 * But if we have dir index, needn't search inode ref to get it.
3643 * Since the inode ref is close to the inode item, it is better
3644 * that we delay to delete it, and just do this deletion when
3645 * we update the inode item.
3646 */
3647 if (BTRFS_I(inode)->dir_index) {
3648 ret = btrfs_delayed_delete_inode_ref(inode);
3649 if (!ret) {
3650 index = BTRFS_I(inode)->dir_index;
3651 goto skip_backref;
3652 }
3653 }
3654
Li Zefan33345d012011-04-20 10:31:50 +08003655 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3656 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003657 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003658 btrfs_info(root->fs_info,
3659 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003660 name_len, name, ino, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003661 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003662 goto err;
3663 }
Miao Xie67de1172013-12-26 13:07:06 +08003664skip_backref:
Miao Xie16cdcec2011-04-22 18:12:22 +08003665 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003666 if (ret) {
3667 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003668 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003669 }
Chris Mason39279cc2007-06-12 06:35:45 -04003670
Chris Masone02119d2008-09-05 16:13:11 -04003671 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08003672 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003673 if (ret != 0 && ret != -ENOENT) {
3674 btrfs_abort_transaction(trans, root, ret);
3675 goto err;
3676 }
Chris Masone02119d2008-09-05 16:13:11 -04003677
3678 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
3679 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04003680 if (ret == -ENOENT)
3681 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003682 else if (ret)
3683 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003684err:
3685 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003686 if (ret)
3687 goto out;
3688
3689 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003690 inode_inc_iversion(inode);
3691 inode_inc_iversion(dir);
Chris Masone02119d2008-09-05 16:13:11 -04003692 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Tsutomu Itohb9959292012-06-25 21:25:22 -06003693 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04003694out:
Chris Mason39279cc2007-06-12 06:35:45 -04003695 return ret;
3696}
3697
Al Viro92986792011-03-04 17:14:37 +00003698int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3699 struct btrfs_root *root,
3700 struct inode *dir, struct inode *inode,
3701 const char *name, int name_len)
3702{
3703 int ret;
3704 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3705 if (!ret) {
Zach Brown8b558c52013-10-16 12:10:34 -07003706 drop_nlink(inode);
Al Viro92986792011-03-04 17:14:37 +00003707 ret = btrfs_update_inode(trans, root, inode);
3708 }
3709 return ret;
3710}
Yan, Zhenga22285a2010-05-16 10:48:46 -04003711
3712/*
3713 * helper to start transaction for unlink and rmdir.
3714 *
Josef Bacikd52be812013-05-29 14:54:47 -04003715 * unlink and rmdir are special in btrfs, they do not always free space, so
3716 * if we cannot make our reservations the normal way try and see if there is
3717 * plenty of slack room in the global reserve to migrate, otherwise we cannot
3718 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04003719 */
Josef Bacikd52be812013-05-29 14:54:47 -04003720static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003721{
3722 struct btrfs_trans_handle *trans;
3723 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003724 int ret;
3725
Josef Bacike70bea52011-10-11 14:18:24 -04003726 /*
3727 * 1 for the possible orphan item
3728 * 1 for the dir item
3729 * 1 for the dir index
3730 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04003731 * 1 for the inode
3732 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04003733 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003734 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
3735 return trans;
3736
Josef Bacikd52be812013-05-29 14:54:47 -04003737 if (PTR_ERR(trans) == -ENOSPC) {
3738 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003739
Josef Bacikd52be812013-05-29 14:54:47 -04003740 trans = btrfs_start_transaction(root, 0);
3741 if (IS_ERR(trans))
3742 return trans;
3743 ret = btrfs_cond_migrate_bytes(root->fs_info,
3744 &root->fs_info->trans_block_rsv,
3745 num_bytes, 5);
3746 if (ret) {
3747 btrfs_end_transaction(trans, root);
3748 return ERR_PTR(ret);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003749 }
Josef Bacik5a77d762011-11-01 14:32:23 -04003750 trans->block_rsv = &root->fs_info->trans_block_rsv;
Josef Bacikd52be812013-05-29 14:54:47 -04003751 trans->bytes_reserved = num_bytes;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003752 }
Josef Bacikd52be812013-05-29 14:54:47 -04003753 return trans;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003754}
3755
Chris Mason39279cc2007-06-12 06:35:45 -04003756static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3757{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003758 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003759 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04003760 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003761 int ret;
3762
Josef Bacikd52be812013-05-29 14:54:47 -04003763 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003764 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003765 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003766
Chris Mason12fcfd22009-03-24 10:24:20 -04003767 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
3768
Chris Masone02119d2008-09-05 16:13:11 -04003769 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3770 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003771 if (ret)
3772 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003773
Yan, Zhenga22285a2010-05-16 10:48:46 -04003774 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04003775 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003776 if (ret)
3777 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003778 }
Josef Bacik7b128762008-07-24 12:17:14 -04003779
Tsutomu Itohb5324022011-07-19 07:27:20 +00003780out:
Josef Bacikd52be812013-05-29 14:54:47 -04003781 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003782 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04003783 return ret;
3784}
3785
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003786int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3787 struct btrfs_root *root,
3788 struct inode *dir, u64 objectid,
3789 const char *name, int name_len)
3790{
3791 struct btrfs_path *path;
3792 struct extent_buffer *leaf;
3793 struct btrfs_dir_item *di;
3794 struct btrfs_key key;
3795 u64 index;
3796 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003797 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003798
3799 path = btrfs_alloc_path();
3800 if (!path)
3801 return -ENOMEM;
3802
Li Zefan33345d012011-04-20 10:31:50 +08003803 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003804 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003805 if (IS_ERR_OR_NULL(di)) {
3806 if (!di)
3807 ret = -ENOENT;
3808 else
3809 ret = PTR_ERR(di);
3810 goto out;
3811 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003812
3813 leaf = path->nodes[0];
3814 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3815 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3816 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003817 if (ret) {
3818 btrfs_abort_transaction(trans, root, ret);
3819 goto out;
3820 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003821 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003822
3823 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3824 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003825 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003826 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003827 if (ret != -ENOENT) {
3828 btrfs_abort_transaction(trans, root, ret);
3829 goto out;
3830 }
Li Zefan33345d012011-04-20 10:31:50 +08003831 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003832 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003833 if (IS_ERR_OR_NULL(di)) {
3834 if (!di)
3835 ret = -ENOENT;
3836 else
3837 ret = PTR_ERR(di);
3838 btrfs_abort_transaction(trans, root, ret);
3839 goto out;
3840 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003841
3842 leaf = path->nodes[0];
3843 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003844 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003845 index = key.offset;
3846 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003847 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003848
Miao Xie16cdcec2011-04-22 18:12:22 +08003849 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003850 if (ret) {
3851 btrfs_abort_transaction(trans, root, ret);
3852 goto out;
3853 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003854
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003855 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003856 inode_inc_iversion(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003857 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Josef Bacik5a24e842012-08-08 10:12:59 -06003858 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003859 if (ret)
3860 btrfs_abort_transaction(trans, root, ret);
3861out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04003862 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003863 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003864}
3865
Chris Mason39279cc2007-06-12 06:35:45 -04003866static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3867{
3868 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003869 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003870 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003871 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04003872
David Sterbab3ae2442012-09-13 16:04:34 -06003873 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04003874 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06003875 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
3876 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04003877
Josef Bacikd52be812013-05-29 14:54:47 -04003878 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003879 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003880 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003881
Li Zefan33345d012011-04-20 10:31:50 +08003882 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003883 err = btrfs_unlink_subvol(trans, root, dir,
3884 BTRFS_I(inode)->location.objectid,
3885 dentry->d_name.name,
3886 dentry->d_name.len);
3887 goto out;
3888 }
3889
Josef Bacik7b128762008-07-24 12:17:14 -04003890 err = btrfs_orphan_add(trans, inode);
3891 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003892 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003893
Chris Mason39279cc2007-06-12 06:35:45 -04003894 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04003895 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3896 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05003897 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04003898 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003899out:
Josef Bacikd52be812013-05-29 14:54:47 -04003900 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003901 btrfs_btree_balance_dirty(root);
Chris Mason39544012007-12-12 14:38:19 -05003902
Chris Mason39279cc2007-06-12 06:35:45 -04003903 return err;
3904}
3905
Chris Mason323ac952008-10-01 19:05:46 -04003906/*
Chris Mason39279cc2007-06-12 06:35:45 -04003907 * this can truncate away extent items, csum items and directory items.
3908 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04003909 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04003910 *
3911 * csum items that cross the new i_size are truncated to the new size
3912 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04003913 *
3914 * min_type is the minimum key type to truncate down to. If set to 0, this
3915 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04003916 */
Yan, Zheng80825102009-11-12 09:35:36 +00003917int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
3918 struct btrfs_root *root,
3919 struct inode *inode,
3920 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003921{
Chris Mason39279cc2007-06-12 06:35:45 -04003922 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003923 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003924 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00003925 struct btrfs_key key;
3926 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04003927 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04003928 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003929 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003930 u64 item_end = 0;
Josef Bacik7f4f6e02013-08-29 16:43:28 -04003931 u64 last_size = (u64)-1;
Yan, Zheng80825102009-11-12 09:35:36 +00003932 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04003933 int found_extent;
3934 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003935 int pending_del_nr = 0;
3936 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04003937 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00003938 int ret;
3939 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08003940 u64 ino = btrfs_ino(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003941
3942 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04003943
Mark Fasheh0eb0e192011-07-12 16:44:10 -07003944 path = btrfs_alloc_path();
3945 if (!path)
3946 return -ENOMEM;
3947 path->reada = -1;
3948
Josef Bacik5dc562c2012-08-17 13:14:17 -04003949 /*
3950 * We want to drop from the next block forward in case this new size is
3951 * not block aligned since we will be keeping the last block of the
3952 * extent just the way it is.
3953 */
Josef Bacik0af3d002010-06-21 14:48:16 -04003954 if (root->ref_cows || root == root->fs_info->tree_root)
Qu Wenruofda28322013-02-26 08:10:22 +00003955 btrfs_drop_extent_cache(inode, ALIGN(new_size,
3956 root->sectorsize), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003957
Miao Xie16cdcec2011-04-22 18:12:22 +08003958 /*
3959 * This function is also used to drop the items in the log tree before
3960 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
3961 * it is used to drop the loged items. So we shouldn't kill the delayed
3962 * items.
3963 */
3964 if (min_type == 0 && root == BTRFS_I(inode)->root)
3965 btrfs_kill_delayed_inode_items(inode);
3966
Li Zefan33345d012011-04-20 10:31:50 +08003967 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04003968 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04003969 key.type = (u8)-1;
3970
Chris Mason85e21ba2008-01-29 15:11:36 -05003971search_again:
Chris Masonb9473432009-03-13 11:00:37 -04003972 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05003973 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00003974 if (ret < 0) {
3975 err = ret;
3976 goto out;
3977 }
Chris Masond3977122009-01-05 21:25:51 -05003978
Chris Mason85e21ba2008-01-29 15:11:36 -05003979 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003980 /* there are no items in the tree for us to truncate, we're
3981 * done
3982 */
Yan, Zheng80825102009-11-12 09:35:36 +00003983 if (path->slots[0] == 0)
3984 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05003985 path->slots[0]--;
3986 }
3987
Chris Masond3977122009-01-05 21:25:51 -05003988 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04003989 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04003990 leaf = path->nodes[0];
3991 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3992 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04003993
Li Zefan33345d012011-04-20 10:31:50 +08003994 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04003995 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003996
Chris Mason85e21ba2008-01-29 15:11:36 -05003997 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003998 break;
3999
Chris Mason5f39d392007-10-15 16:14:19 -04004000 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004001 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004002 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004003 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004004 extent_type = btrfs_file_extent_type(leaf, fi);
4005 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004006 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004007 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04004008 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004009 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04004010 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004011 }
Yan008630c2007-11-07 13:31:09 -05004012 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004013 }
Yan, Zheng80825102009-11-12 09:35:36 +00004014 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004015 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004016 } else {
4017 if (item_end < new_size)
4018 break;
4019 if (found_key.offset >= new_size)
4020 del_item = 1;
4021 else
4022 del_item = 0;
4023 }
Chris Mason39279cc2007-06-12 06:35:45 -04004024 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004025 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004026 if (found_type != BTRFS_EXTENT_DATA_KEY)
4027 goto delete;
4028
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004029 if (del_item)
4030 last_size = found_key.offset;
4031 else
4032 last_size = new_size;
4033
Chris Mason179e29e2007-11-01 11:28:41 -04004034 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004035 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004036 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004037 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004038 u64 orig_num_bytes =
4039 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004040 extent_num_bytes = ALIGN(new_size -
4041 found_key.offset,
4042 root->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004043 btrfs_set_file_extent_num_bytes(leaf, fi,
4044 extent_num_bytes);
4045 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004046 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04004047 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004048 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004049 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004050 } else {
Chris Masondb945352007-10-15 16:15:53 -04004051 extent_num_bytes =
4052 btrfs_file_extent_disk_num_bytes(leaf,
4053 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004054 extent_offset = found_key.offset -
4055 btrfs_file_extent_offset(leaf, fi);
4056
Chris Mason39279cc2007-06-12 06:35:45 -04004057 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004058 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004059 if (extent_start != 0) {
4060 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04004061 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004062 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004063 }
4064 }
Chris Mason90692182008-02-08 13:49:28 -05004065 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004066 /*
4067 * we can't truncate inline items that have had
4068 * special encodings
4069 */
4070 if (!del_item &&
4071 btrfs_file_extent_compression(leaf, fi) == 0 &&
4072 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4073 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004074 u32 size = new_size - found_key.offset;
4075
4076 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004077 inode_sub_bytes(inode, item_end + 1 -
4078 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04004079 }
4080 size =
4081 btrfs_file_extent_calc_inline_size(size);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004082 btrfs_truncate_item(root, path, size, 1);
Chris Masone02119d2008-09-05 16:13:11 -04004083 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004084 inode_sub_bytes(inode, item_end + 1 -
4085 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05004086 }
Chris Mason39279cc2007-06-12 06:35:45 -04004087 }
Chris Mason179e29e2007-11-01 11:28:41 -04004088delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004089 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004090 if (!pending_del_nr) {
4091 /* no pending yet, add ourselves */
4092 pending_del_slot = path->slots[0];
4093 pending_del_nr = 1;
4094 } else if (pending_del_nr &&
4095 path->slots[0] + 1 == pending_del_slot) {
4096 /* hop on the pending chunk */
4097 pending_del_nr++;
4098 pending_del_slot = path->slots[0];
4099 } else {
Chris Masond3977122009-01-05 21:25:51 -05004100 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004101 }
Chris Mason39279cc2007-06-12 06:35:45 -04004102 } else {
4103 break;
4104 }
Josef Bacik0af3d002010-06-21 14:48:16 -04004105 if (found_extent && (root->ref_cows ||
4106 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004107 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004108 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004109 extent_num_bytes, 0,
4110 btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02004111 ino, extent_offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004112 BUG_ON(ret);
4113 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004114
Yan, Zheng80825102009-11-12 09:35:36 +00004115 if (found_type == BTRFS_INODE_ITEM_KEY)
4116 break;
4117
4118 if (path->slots[0] == 0 ||
4119 path->slots[0] != pending_del_slot) {
Yan, Zheng80825102009-11-12 09:35:36 +00004120 if (pending_del_nr) {
4121 ret = btrfs_del_items(trans, root, path,
4122 pending_del_slot,
4123 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004124 if (ret) {
4125 btrfs_abort_transaction(trans,
4126 root, ret);
4127 goto error;
4128 }
Yan, Zheng80825102009-11-12 09:35:36 +00004129 pending_del_nr = 0;
4130 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004131 btrfs_release_path(path);
Chris Mason85e21ba2008-01-29 15:11:36 -05004132 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004133 } else {
4134 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004135 }
Chris Mason39279cc2007-06-12 06:35:45 -04004136 }
Yan, Zheng80825102009-11-12 09:35:36 +00004137out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004138 if (pending_del_nr) {
4139 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4140 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004141 if (ret)
4142 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004143 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004144error:
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004145 if (last_size != (u64)-1)
4146 btrfs_ordered_update_i_size(inode, last_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004147 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00004148 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004149}
4150
Chris Masona52d9a82007-08-27 16:49:44 -04004151/*
Josef Bacik2aaa6652012-08-29 14:27:18 -04004152 * btrfs_truncate_page - read, zero a chunk and write a page
4153 * @inode - inode that we're zeroing
4154 * @from - the offset to start zeroing
4155 * @len - the length to zero, 0 to zero the entire range respective to the
4156 * offset
4157 * @front - zero up to the offset instead of from the offset on
4158 *
4159 * This will find the page for the "from" offset and cow the page and zero the
4160 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004161 */
Josef Bacik2aaa6652012-08-29 14:27:18 -04004162int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
4163 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004164{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004165 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04004166 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004167 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4168 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004169 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004170 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04004171 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04004172 pgoff_t index = from >> PAGE_CACHE_SHIFT;
4173 unsigned offset = from & (PAGE_CACHE_SIZE-1);
4174 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004175 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004176 int ret = 0;
4177 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004178 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004179
Josef Bacik2aaa6652012-08-29 14:27:18 -04004180 if ((offset & (blocksize - 1)) == 0 &&
4181 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004182 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004183 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004184 if (ret)
4185 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004186
Chris Mason211c17f2008-05-15 09:13:45 -04004187again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004188 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004189 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004190 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Miao Xieac6a2b32012-12-05 10:56:13 +00004191 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004192 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004193 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004194
4195 page_start = page_offset(page);
4196 page_end = page_start + PAGE_CACHE_SIZE - 1;
4197
Chris Masona52d9a82007-08-27 16:49:44 -04004198 if (!PageUptodate(page)) {
4199 ret = btrfs_readpage(NULL, page);
4200 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004201 if (page->mapping != mapping) {
4202 unlock_page(page);
4203 page_cache_release(page);
4204 goto again;
4205 }
Chris Masona52d9a82007-08-27 16:49:44 -04004206 if (!PageUptodate(page)) {
4207 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004208 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004209 }
4210 }
Chris Mason211c17f2008-05-15 09:13:45 -04004211 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004212
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004213 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004214 set_page_extent_mapped(page);
4215
4216 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4217 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004218 unlock_extent_cached(io_tree, page_start, page_end,
4219 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004220 unlock_page(page);
4221 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004222 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004223 btrfs_put_ordered_extent(ordered);
4224 goto again;
4225 }
4226
Josef Bacik2ac55d42010-02-03 19:33:23 +00004227 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004228 EXTENT_DIRTY | EXTENT_DELALLOC |
4229 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004230 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004231
Josef Bacik2ac55d42010-02-03 19:33:23 +00004232 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
4233 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004234 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004235 unlock_extent_cached(io_tree, page_start, page_end,
4236 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004237 goto out_unlock;
4238 }
4239
Chris Masone6dcd2d2008-07-17 12:53:50 -04004240 if (offset != PAGE_CACHE_SIZE) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004241 if (!len)
4242 len = PAGE_CACHE_SIZE - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004243 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004244 if (front)
4245 memset(kaddr, 0, offset);
4246 else
4247 memset(kaddr + offset, 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004248 flush_dcache_page(page);
4249 kunmap(page);
4250 }
Chris Mason247e7432008-07-17 12:53:51 -04004251 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004252 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004253 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
4254 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004255
Chris Mason89642222008-07-24 09:41:53 -04004256out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004257 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004258 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04004259 unlock_page(page);
4260 page_cache_release(page);
4261out:
4262 return ret;
4263}
4264
Josef Bacik16e75492013-10-22 12:18:51 -04004265static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4266 u64 offset, u64 len)
4267{
4268 struct btrfs_trans_handle *trans;
4269 int ret;
4270
4271 /*
4272 * Still need to make sure the inode looks like it's been updated so
4273 * that any holes get logged if we fsync.
4274 */
4275 if (btrfs_fs_incompat(root->fs_info, NO_HOLES)) {
4276 BTRFS_I(inode)->last_trans = root->fs_info->generation;
4277 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4278 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4279 return 0;
4280 }
4281
4282 /*
4283 * 1 - for the one we're dropping
4284 * 1 - for the one we're adding
4285 * 1 - for updating the inode.
4286 */
4287 trans = btrfs_start_transaction(root, 3);
4288 if (IS_ERR(trans))
4289 return PTR_ERR(trans);
4290
4291 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4292 if (ret) {
4293 btrfs_abort_transaction(trans, root, ret);
4294 btrfs_end_transaction(trans, root);
4295 return ret;
4296 }
4297
4298 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset,
4299 0, 0, len, 0, len, 0, 0, 0);
4300 if (ret)
4301 btrfs_abort_transaction(trans, root, ret);
4302 else
4303 btrfs_update_inode(trans, root, inode);
4304 btrfs_end_transaction(trans, root);
4305 return ret;
4306}
4307
Josef Bacik695a0d02011-03-04 15:46:53 -05004308/*
4309 * This function puts in dummy file extents for the area we're creating a hole
4310 * for. So if we are truncating this file to a larger size we need to insert
4311 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4312 * the range between oldsize and size
4313 */
Josef Bacika41ad392011-01-31 15:30:16 -05004314int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004315{
Yan Zheng9036c102008-10-30 14:19:41 -04004316 struct btrfs_root *root = BTRFS_I(inode)->root;
4317 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004318 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004319 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004320 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Qu Wenruofda28322013-02-26 08:10:22 +00004321 u64 hole_start = ALIGN(oldsize, root->sectorsize);
4322 u64 block_end = ALIGN(size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004323 u64 last_byte;
4324 u64 cur_offset;
4325 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004326 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004327
Josef Bacika71754f2013-06-17 17:14:39 -04004328 /*
4329 * If our size started in the middle of a page we need to zero out the
4330 * rest of the page before we expand the i_size, otherwise we could
4331 * expose stale data.
4332 */
4333 err = btrfs_truncate_page(inode, oldsize, 0, 0);
4334 if (err)
4335 return err;
4336
Yan Zheng9036c102008-10-30 14:19:41 -04004337 if (size <= hole_start)
4338 return 0;
4339
Yan Zheng9036c102008-10-30 14:19:41 -04004340 while (1) {
4341 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004342
Josef Bacik2ac55d42010-02-03 19:33:23 +00004343 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004344 &cached_state);
Miao Xiefa7c1492013-09-26 13:15:27 +08004345 ordered = btrfs_lookup_ordered_range(inode, hole_start,
4346 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004347 if (!ordered)
4348 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004349 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4350 &cached_state, GFP_NOFS);
Miao Xiefa7c1492013-09-26 13:15:27 +08004351 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004352 btrfs_put_ordered_extent(ordered);
4353 }
4354
Yan Zheng9036c102008-10-30 14:19:41 -04004355 cur_offset = hole_start;
4356 while (1) {
4357 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4358 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004359 if (IS_ERR(em)) {
4360 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004361 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004362 break;
4363 }
Yan Zheng9036c102008-10-30 14:19:41 -04004364 last_byte = min(extent_map_end(em), block_end);
Qu Wenruofda28322013-02-26 08:10:22 +00004365 last_byte = ALIGN(last_byte , root->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004366 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004367 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004368 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004369
Josef Bacik16e75492013-10-22 12:18:51 -04004370 err = maybe_insert_hole(root, inode, cur_offset,
4371 hole_size);
4372 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004373 break;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004374 btrfs_drop_extent_cache(inode, cur_offset,
4375 cur_offset + hole_size - 1, 0);
4376 hole_em = alloc_extent_map();
4377 if (!hole_em) {
4378 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4379 &BTRFS_I(inode)->runtime_flags);
4380 goto next;
4381 }
4382 hole_em->start = cur_offset;
4383 hole_em->len = hole_size;
4384 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004385
Josef Bacik5dc562c2012-08-17 13:14:17 -04004386 hole_em->block_start = EXTENT_MAP_HOLE;
4387 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004388 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004389 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004390 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
4391 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Josef Bacik16e75492013-10-22 12:18:51 -04004392 hole_em->generation = root->fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004393
4394 while (1) {
4395 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004396 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004397 write_unlock(&em_tree->lock);
4398 if (err != -EEXIST)
4399 break;
4400 btrfs_drop_extent_cache(inode, cur_offset,
4401 cur_offset +
4402 hole_size - 1, 0);
4403 }
4404 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04004405 }
Josef Bacik16e75492013-10-22 12:18:51 -04004406next:
Yan Zheng9036c102008-10-30 14:19:41 -04004407 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004408 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004409 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004410 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004411 break;
4412 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004413 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004414 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4415 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004416 return err;
4417}
4418
Eric Sandeen3972f262013-01-12 02:57:22 +00004419static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004420{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004421 struct btrfs_root *root = BTRFS_I(inode)->root;
4422 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004423 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004424 loff_t newsize = attr->ia_size;
4425 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004426 int ret;
4427
Eric Sandeen3972f262013-01-12 02:57:22 +00004428 /*
4429 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4430 * special case where we need to update the times despite not having
4431 * these flags set. For all other operations the VFS set these flags
4432 * explicitly if it wants a timestamp update.
4433 */
4434 if (newsize != oldsize && (!(mask & (ATTR_CTIME | ATTR_MTIME))))
4435 inode->i_ctime = inode->i_mtime = current_fs_time(inode->i_sb);
4436
Josef Bacika41ad392011-01-31 15:30:16 -05004437 if (newsize > oldsize) {
Kirill A. Shutemov7caef262013-09-12 15:13:56 -07004438 truncate_pagecache(inode, newsize);
Josef Bacika41ad392011-01-31 15:30:16 -05004439 ret = btrfs_cont_expand(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004440 if (ret)
Yan, Zheng80825102009-11-12 09:35:36 +00004441 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004442
Miao Xief4a2f4c2011-12-14 20:12:01 -05004443 trans = btrfs_start_transaction(root, 1);
4444 if (IS_ERR(trans))
4445 return PTR_ERR(trans);
4446
4447 i_size_write(inode, newsize);
4448 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
4449 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004450 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05004451 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004452
Josef Bacika41ad392011-01-31 15:30:16 -05004453 /*
4454 * We're truncating a file that used to have good data down to
4455 * zero. Make sure it gets into the ordered flush list so that
4456 * any new writes get down to disk quickly.
4457 */
4458 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004459 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4460 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004461
Josef Bacikf3fe8202013-01-07 17:03:21 -05004462 /*
4463 * 1 for the orphan item we're going to add
4464 * 1 for the orphan item deletion.
4465 */
4466 trans = btrfs_start_transaction(root, 2);
4467 if (IS_ERR(trans))
4468 return PTR_ERR(trans);
4469
4470 /*
4471 * We need to do this in case we fail at _any_ point during the
4472 * actual truncate. Once we do the truncate_setsize we could
4473 * invalidate pages which forces any outstanding ordered io to
4474 * be instantly completed which will give us extents that need
4475 * to be truncated. If we fail to get an orphan inode down we
4476 * could have left over extents that were never meant to live,
4477 * so we need to garuntee from this point on that everything
4478 * will be consistent.
4479 */
4480 ret = btrfs_orphan_add(trans, inode);
4481 btrfs_end_transaction(trans, root);
4482 if (ret)
4483 return ret;
4484
Josef Bacika41ad392011-01-31 15:30:16 -05004485 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4486 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004487
4488 /* Disable nonlocked read DIO to avoid the end less truncate */
4489 btrfs_inode_block_unlocked_dio(inode);
4490 inode_dio_wait(inode);
4491 btrfs_inode_resume_unlocked_dio(inode);
4492
Josef Bacika41ad392011-01-31 15:30:16 -05004493 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004494 if (ret && inode->i_nlink) {
4495 int err;
4496
4497 /*
4498 * failed to truncate, disk_i_size is only adjusted down
4499 * as we remove extents, so it should represent the true
4500 * size of the inode, so reset the in memory size and
4501 * delete our orphan entry.
4502 */
4503 trans = btrfs_join_transaction(root);
4504 if (IS_ERR(trans)) {
4505 btrfs_orphan_del(NULL, inode);
4506 return ret;
4507 }
4508 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
4509 err = btrfs_orphan_del(trans, inode);
4510 if (err)
4511 btrfs_abort_transaction(trans, root, err);
4512 btrfs_end_transaction(trans, root);
4513 }
Yan, Zheng80825102009-11-12 09:35:36 +00004514 }
4515
Josef Bacika41ad392011-01-31 15:30:16 -05004516 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004517}
4518
Chris Mason39279cc2007-06-12 06:35:45 -04004519static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
4520{
4521 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08004522 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004523 int err;
4524
Li Zefanb83cc962010-12-20 16:04:08 +08004525 if (btrfs_root_readonly(root))
4526 return -EROFS;
4527
Chris Mason39279cc2007-06-12 06:35:45 -04004528 err = inode_change_ok(inode, attr);
4529 if (err)
4530 return err;
4531
Chris Mason5a3f23d2009-03-31 13:27:11 -04004532 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00004533 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00004534 if (err)
4535 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004536 }
Yan Zheng9036c102008-10-30 14:19:41 -04004537
Christoph Hellwig10257742010-06-04 11:30:02 +02004538 if (attr->ia_valid) {
4539 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004540 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004541 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04004542
Josef Bacik22c44fe2011-11-30 10:45:38 -05004543 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig10257742010-06-04 11:30:02 +02004544 err = btrfs_acl_chmod(inode);
4545 }
4546
Chris Mason39279cc2007-06-12 06:35:45 -04004547 return err;
4548}
Chris Mason61295eb2008-01-14 16:24:38 -05004549
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004550/*
4551 * While truncating the inode pages during eviction, we get the VFS calling
4552 * btrfs_invalidatepage() against each page of the inode. This is slow because
4553 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
4554 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
4555 * extent_state structures over and over, wasting lots of time.
4556 *
4557 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
4558 * those expensive operations on a per page basis and do only the ordered io
4559 * finishing, while we release here the extent_map and extent_state structures,
4560 * without the excessive merging and splitting.
4561 */
4562static void evict_inode_truncate_pages(struct inode *inode)
4563{
4564 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4565 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
4566 struct rb_node *node;
4567
4568 ASSERT(inode->i_state & I_FREEING);
4569 truncate_inode_pages(&inode->i_data, 0);
4570
4571 write_lock(&map_tree->lock);
4572 while (!RB_EMPTY_ROOT(&map_tree->map)) {
4573 struct extent_map *em;
4574
4575 node = rb_first(&map_tree->map);
4576 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08004577 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
4578 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004579 remove_extent_mapping(map_tree, em);
4580 free_extent_map(em);
4581 }
4582 write_unlock(&map_tree->lock);
4583
4584 spin_lock(&io_tree->lock);
4585 while (!RB_EMPTY_ROOT(&io_tree->state)) {
4586 struct extent_state *state;
4587 struct extent_state *cached_state = NULL;
4588
4589 node = rb_first(&io_tree->state);
4590 state = rb_entry(node, struct extent_state, rb_node);
4591 atomic_inc(&state->refs);
4592 spin_unlock(&io_tree->lock);
4593
4594 lock_extent_bits(io_tree, state->start, state->end,
4595 0, &cached_state);
4596 clear_extent_bit(io_tree, state->start, state->end,
4597 EXTENT_LOCKED | EXTENT_DIRTY |
4598 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
4599 EXTENT_DEFRAG, 1, 1,
4600 &cached_state, GFP_NOFS);
4601 free_extent_state(state);
4602
4603 spin_lock(&io_tree->lock);
4604 }
4605 spin_unlock(&io_tree->lock);
4606}
4607
Al Virobd555972010-06-07 11:35:40 -04004608void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004609{
4610 struct btrfs_trans_handle *trans;
4611 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04004612 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik07127182011-08-19 10:29:59 -04004613 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04004614 int ret;
4615
liubo1abe9b82011-03-24 11:18:59 +00004616 trace_btrfs_inode_evict(inode);
4617
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004618 evict_inode_truncate_pages(inode);
4619
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02004620 if (inode->i_nlink &&
4621 ((btrfs_root_refs(&root->root_item) != 0 &&
4622 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
4623 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04004624 goto no_delete;
4625
Chris Mason39279cc2007-06-12 06:35:45 -04004626 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04004627 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004628 goto no_delete;
4629 }
Al Virobd555972010-06-07 11:35:40 -04004630 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04004631 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04004632
Yan, Zhengc71bf092009-11-12 09:34:40 +00004633 if (root->fs_info->log_root_recovering) {
Liu Bo6bf02312012-06-25 21:59:09 -06004634 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04004635 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00004636 goto no_delete;
4637 }
4638
Yan, Zheng76dda932009-09-21 16:00:26 -04004639 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02004640 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
4641 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04004642 goto no_delete;
4643 }
4644
Miao Xie0e8c36a2012-12-19 06:59:51 +00004645 ret = btrfs_commit_inode_delayed_inode(inode);
4646 if (ret) {
4647 btrfs_orphan_del(NULL, inode);
4648 goto no_delete;
4649 }
4650
Miao Xie66d8f3d2012-09-06 04:02:28 -06004651 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04004652 if (!rsv) {
4653 btrfs_orphan_del(NULL, inode);
4654 goto no_delete;
4655 }
Josef Bacik4a338542011-08-29 11:01:31 -04004656 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04004657 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04004658 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04004659
Chris Masondbe674a2008-07-17 12:54:05 -04004660 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04004661
Josef Bacik4289a662011-08-05 13:22:24 -04004662 /*
Miao Xie8407aa42012-09-07 01:43:32 -06004663 * This is a bit simpler than btrfs_truncate since we've already
4664 * reserved our space for our orphan item in the unlink, so we just
4665 * need to reserve some slack space in case we add bytes and update
4666 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04004667 */
Yan, Zheng80825102009-11-12 09:35:36 +00004668 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00004669 ret = btrfs_block_rsv_refill(root, rsv, min_size,
4670 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00004671
Josef Bacik726c35f2011-09-26 15:46:06 -04004672 /*
4673 * Try and steal from the global reserve since we will
4674 * likely not use this space anyway, we want to try as
4675 * hard as possible to get this to work.
4676 */
4677 if (ret)
4678 ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
4679
Yan, Zhengd68fc572010-05-16 10:49:58 -04004680 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004681 btrfs_warn(root->fs_info,
4682 "Could not get space for a delete, will truncate on mount %d",
4683 ret);
Josef Bacik4289a662011-08-05 13:22:24 -04004684 btrfs_orphan_del(NULL, inode);
4685 btrfs_free_block_rsv(root, rsv);
4686 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04004687 }
4688
Miao Xie0e8c36a2012-12-19 06:59:51 +00004689 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04004690 if (IS_ERR(trans)) {
4691 btrfs_orphan_del(NULL, inode);
4692 btrfs_free_block_rsv(root, rsv);
4693 goto no_delete;
4694 }
4695
4696 trans->block_rsv = rsv;
4697
Yan, Zhengd68fc572010-05-16 10:49:58 -04004698 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04004699 if (ret != -ENOSPC)
Yan, Zheng80825102009-11-12 09:35:36 +00004700 break;
4701
Miao Xie8407aa42012-09-07 01:43:32 -06004702 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004703 btrfs_end_transaction(trans, root);
4704 trans = NULL;
Liu Bob53d3f52012-11-14 14:34:34 +00004705 btrfs_btree_balance_dirty(root);
Josef Bacik7b128762008-07-24 12:17:14 -04004706 }
4707
Josef Bacik4289a662011-08-05 13:22:24 -04004708 btrfs_free_block_rsv(root, rsv);
4709
Josef Bacik4ef31a42013-08-13 14:10:08 -04004710 /*
4711 * Errors here aren't a big deal, it just means we leave orphan items
4712 * in the tree. They will be cleaned up on the next mount.
4713 */
Yan, Zheng80825102009-11-12 09:35:36 +00004714 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04004715 trans->block_rsv = root->orphan_block_rsv;
Josef Bacik4ef31a42013-08-13 14:10:08 -04004716 btrfs_orphan_del(trans, inode);
4717 } else {
4718 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004719 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004720
Josef Bacik4289a662011-08-05 13:22:24 -04004721 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08004722 if (!(root == root->fs_info->tree_root ||
4723 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08004724 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08004725
Chris Mason54aa1f42007-06-22 14:16:25 -04004726 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004727 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04004728no_delete:
Miao Xie89042e52013-05-15 07:48:15 +00004729 btrfs_remove_delayed_node(inode);
Jan Karadbd57682012-05-03 14:48:02 +02004730 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004731 return;
Chris Mason39279cc2007-06-12 06:35:45 -04004732}
4733
4734/*
4735 * this returns the key found in the dir entry in the location pointer.
4736 * If no dir entries were found, location->objectid is 0.
4737 */
4738static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
4739 struct btrfs_key *location)
4740{
4741 const char *name = dentry->d_name.name;
4742 int namelen = dentry->d_name.len;
4743 struct btrfs_dir_item *di;
4744 struct btrfs_path *path;
4745 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04004746 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004747
4748 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07004749 if (!path)
4750 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05004751
Li Zefan33345d012011-04-20 10:31:50 +08004752 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04004753 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04004754 if (IS_ERR(di))
4755 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05004756
David Sterbac7040052011-04-19 18:00:01 +02004757 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05004758 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05004759
Chris Mason5f39d392007-10-15 16:14:19 -04004760 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04004761out:
Chris Mason39279cc2007-06-12 06:35:45 -04004762 btrfs_free_path(path);
4763 return ret;
Chris Mason39544012007-12-12 14:38:19 -05004764out_err:
4765 location->objectid = 0;
4766 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04004767}
4768
4769/*
4770 * when we hit a tree root in a directory, the btrfs part of the inode
4771 * needs to be changed to reflect the root directory of the tree root. This
4772 * is kind of like crossing a mount point.
4773 */
4774static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004775 struct inode *dir,
4776 struct dentry *dentry,
4777 struct btrfs_key *location,
4778 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04004779{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004780 struct btrfs_path *path;
4781 struct btrfs_root *new_root;
4782 struct btrfs_root_ref *ref;
4783 struct extent_buffer *leaf;
4784 int ret;
4785 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004786
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004787 path = btrfs_alloc_path();
4788 if (!path) {
4789 err = -ENOMEM;
4790 goto out;
4791 }
Chris Mason39279cc2007-06-12 06:35:45 -04004792
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004793 err = -ENOENT;
Kelley Nielsen75ac2dd2013-11-04 19:35:58 -08004794 ret = btrfs_find_item(root->fs_info->tree_root, path,
4795 BTRFS_I(dir)->root->root_key.objectid,
4796 location->objectid, BTRFS_ROOT_REF_KEY, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004797 if (ret) {
4798 if (ret < 0)
4799 err = ret;
4800 goto out;
4801 }
Chris Mason39279cc2007-06-12 06:35:45 -04004802
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004803 leaf = path->nodes[0];
4804 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08004805 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004806 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
4807 goto out;
4808
4809 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
4810 (unsigned long)(ref + 1),
4811 dentry->d_name.len);
4812 if (ret)
4813 goto out;
4814
David Sterbab3b4aa72011-04-21 01:20:15 +02004815 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004816
4817 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
4818 if (IS_ERR(new_root)) {
4819 err = PTR_ERR(new_root);
4820 goto out;
4821 }
4822
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004823 *sub_root = new_root;
4824 location->objectid = btrfs_root_dirid(&new_root->root_item);
4825 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04004826 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004827 err = 0;
4828out:
4829 btrfs_free_path(path);
4830 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004831}
4832
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004833static void inode_tree_add(struct inode *inode)
4834{
4835 struct btrfs_root *root = BTRFS_I(inode)->root;
4836 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004837 struct rb_node **p;
4838 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01004839 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Li Zefan33345d012011-04-20 10:31:50 +08004840 u64 ino = btrfs_ino(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004841
Al Viro1d3382cb2010-10-23 15:19:20 -04004842 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004843 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00004844 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004845 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00004846 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004847 while (*p) {
4848 parent = *p;
4849 entry = rb_entry(parent, struct btrfs_inode, rb_node);
4850
Li Zefan33345d012011-04-20 10:31:50 +08004851 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004852 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004853 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004854 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004855 else {
4856 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04004857 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01004858 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004859 RB_CLEAR_NODE(parent);
4860 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01004861 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004862 }
4863 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01004864 rb_link_node(new, parent, p);
4865 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004866 spin_unlock(&root->inode_lock);
4867}
4868
4869static void inode_tree_del(struct inode *inode)
4870{
4871 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04004872 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004873
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004874 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004875 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004876 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004877 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04004878 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004879 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004880 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04004881
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02004882 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004883 synchronize_srcu(&root->fs_info->subvol_srcu);
4884 spin_lock(&root->inode_lock);
4885 empty = RB_EMPTY_ROOT(&root->inode_tree);
4886 spin_unlock(&root->inode_lock);
4887 if (empty)
4888 btrfs_add_dead_root(root);
4889 }
4890}
4891
Jeff Mahoney143bede2012-03-01 14:56:26 +01004892void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04004893{
4894 struct rb_node *node;
4895 struct rb_node *prev;
4896 struct btrfs_inode *entry;
4897 struct inode *inode;
4898 u64 objectid = 0;
4899
4900 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4901
4902 spin_lock(&root->inode_lock);
4903again:
4904 node = root->inode_tree.rb_node;
4905 prev = NULL;
4906 while (node) {
4907 prev = node;
4908 entry = rb_entry(node, struct btrfs_inode, rb_node);
4909
Li Zefan33345d012011-04-20 10:31:50 +08004910 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004911 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004912 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004913 node = node->rb_right;
4914 else
4915 break;
4916 }
4917 if (!node) {
4918 while (prev) {
4919 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004920 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004921 node = prev;
4922 break;
4923 }
4924 prev = rb_next(prev);
4925 }
4926 }
4927 while (node) {
4928 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004929 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04004930 inode = igrab(&entry->vfs_inode);
4931 if (inode) {
4932 spin_unlock(&root->inode_lock);
4933 if (atomic_read(&inode->i_count) > 1)
4934 d_prune_aliases(inode);
4935 /*
Al Viro45321ac2010-06-07 13:43:19 -04004936 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04004937 * the inode cache when its usage count
4938 * hits zero.
4939 */
4940 iput(inode);
4941 cond_resched();
4942 spin_lock(&root->inode_lock);
4943 goto again;
4944 }
4945
4946 if (cond_resched_lock(&root->inode_lock))
4947 goto again;
4948
4949 node = rb_next(node);
4950 }
4951 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004952}
4953
Chris Masone02119d2008-09-05 16:13:11 -04004954static int btrfs_init_locked_inode(struct inode *inode, void *p)
4955{
4956 struct btrfs_iget_args *args = p;
4957 inode->i_ino = args->ino;
Chris Masone02119d2008-09-05 16:13:11 -04004958 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004959 return 0;
4960}
4961
4962static int btrfs_find_actor(struct inode *inode, void *opaque)
4963{
4964 struct btrfs_iget_args *args = opaque;
Li Zefan33345d012011-04-20 10:31:50 +08004965 return args->ino == btrfs_ino(inode) &&
Chris Masond3977122009-01-05 21:25:51 -05004966 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004967}
4968
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004969static struct inode *btrfs_iget_locked(struct super_block *s,
4970 u64 objectid,
4971 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04004972{
4973 struct inode *inode;
4974 struct btrfs_iget_args args;
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01004975 unsigned long hashval = btrfs_inode_hash(objectid, root);
4976
Chris Mason39279cc2007-06-12 06:35:45 -04004977 args.ino = objectid;
4978 args.root = root;
4979
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01004980 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04004981 btrfs_init_locked_inode,
4982 (void *)&args);
4983 return inode;
4984}
4985
Balaji Rao1a54ef82008-07-21 02:01:04 +05304986/* Get an inode object given its location and corresponding root.
4987 * Returns in *is_new if the inode was read from disk
4988 */
4989struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00004990 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05304991{
4992 struct inode *inode;
4993
4994 inode = btrfs_iget_locked(s, location->objectid, root);
4995 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004996 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05304997
4998 if (inode->i_state & I_NEW) {
4999 BTRFS_I(inode)->root = root;
5000 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
5001 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005002 if (!is_bad_inode(inode)) {
5003 inode_tree_add(inode);
5004 unlock_new_inode(inode);
5005 if (new)
5006 *new = 1;
5007 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005008 unlock_new_inode(inode);
5009 iput(inode);
5010 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005011 }
5012 }
5013
Balaji Rao1a54ef82008-07-21 02:01:04 +05305014 return inode;
5015}
5016
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005017static struct inode *new_simple_dir(struct super_block *s,
5018 struct btrfs_key *key,
5019 struct btrfs_root *root)
5020{
5021 struct inode *inode = new_inode(s);
5022
5023 if (!inode)
5024 return ERR_PTR(-ENOMEM);
5025
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005026 BTRFS_I(inode)->root = root;
5027 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005028 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005029
5030 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005031 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005032 inode->i_fop = &simple_dir_operations;
5033 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
5034 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
5035
5036 return inode;
5037}
5038
Chris Mason3de45862008-11-17 21:02:50 -05005039struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005040{
Chris Masond3977122009-01-05 21:25:51 -05005041 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005042 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005043 struct btrfs_root *sub_root = root;
5044 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005045 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005046 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005047
5048 if (dentry->d_name.len > BTRFS_NAME_LEN)
5049 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005050
Jeff Layton39e3c952012-11-28 11:30:53 -05005051 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005052 if (ret < 0)
5053 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005054
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005055 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005056 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005057
5058 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005059 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005060 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005061 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005062
5063 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5064
Yan, Zheng76dda932009-09-21 16:00:26 -04005065 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005066 ret = fixup_tree_root_location(root, dir, dentry,
5067 &location, &sub_root);
5068 if (ret < 0) {
5069 if (ret != -ENOENT)
5070 inode = ERR_PTR(ret);
5071 else
5072 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5073 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005074 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005075 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005076 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
5077
Julia Lawall34d19ba2011-01-24 19:55:19 +00005078 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00005079 down_read(&root->fs_info->cleanup_work_sem);
5080 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005081 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005082 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005083 if (ret) {
5084 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005085 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005086 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005087 }
5088
Chris Mason3de45862008-11-17 21:02:50 -05005089 return inode;
5090}
5091
Nick Pigginfe15ce42011-01-07 17:49:23 +11005092static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005093{
5094 struct btrfs_root *root;
Li Zefan848cce02012-02-21 17:04:28 +08005095 struct inode *inode = dentry->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005096
Li Zefan848cce02012-02-21 17:04:28 +08005097 if (!inode && !IS_ROOT(dentry))
5098 inode = dentry->d_parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005099
Li Zefan848cce02012-02-21 17:04:28 +08005100 if (inode) {
5101 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005102 if (btrfs_root_refs(&root->root_item) == 0)
5103 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005104
5105 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5106 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005107 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005108 return 0;
5109}
5110
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005111static void btrfs_dentry_release(struct dentry *dentry)
5112{
5113 if (dentry->d_fsdata)
5114 kfree(dentry->d_fsdata);
5115}
5116
Chris Mason3de45862008-11-17 21:02:50 -05005117static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005118 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005119{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005120 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005121
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005122 inode = btrfs_lookup_dentry(dir, dentry);
5123 if (IS_ERR(inode)) {
5124 if (PTR_ERR(inode) == -ENOENT)
5125 inode = NULL;
5126 else
5127 return ERR_CAST(inode);
5128 }
5129
5130 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005131}
5132
Miao Xie16cdcec2011-04-22 18:12:22 +08005133unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005134 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5135};
5136
Al Viro9cdda8d2013-05-22 16:48:09 -04005137static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005138{
Al Viro9cdda8d2013-05-22 16:48:09 -04005139 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005140 struct btrfs_root *root = BTRFS_I(inode)->root;
5141 struct btrfs_item *item;
5142 struct btrfs_dir_item *di;
5143 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005144 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005145 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005146 struct list_head ins_list;
5147 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005148 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005149 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005150 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005151 unsigned char d_type;
5152 int over = 0;
5153 u32 di_cur;
5154 u32 di_total;
5155 u32 di_len;
5156 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005157 char tmp_name[32];
5158 char *name_ptr;
5159 int name_len;
Al Viro9cdda8d2013-05-22 16:48:09 -04005160 int is_curr = 0; /* ctx->pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04005161
5162 /* FIXME, use a real flag for deciding about the key type */
5163 if (root->fs_info->tree_root == root)
5164 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005165
Al Viro9cdda8d2013-05-22 16:48:09 -04005166 if (!dir_emit_dots(file, ctx))
5167 return 0;
5168
David Woodhouse49593bf2008-08-17 17:08:36 +01005169 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005170 if (!path)
5171 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005172
Josef Bacik026fd312011-05-13 10:32:11 -04005173 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005174
Miao Xie16cdcec2011-04-22 18:12:22 +08005175 if (key_type == BTRFS_DIR_INDEX_KEY) {
5176 INIT_LIST_HEAD(&ins_list);
5177 INIT_LIST_HEAD(&del_list);
5178 btrfs_get_delayed_items(inode, &ins_list, &del_list);
5179 }
5180
Chris Mason39279cc2007-06-12 06:35:45 -04005181 btrfs_set_key_type(&key, key_type);
Al Viro9cdda8d2013-05-22 16:48:09 -04005182 key.offset = ctx->pos;
Li Zefan33345d012011-04-20 10:31:50 +08005183 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005184
Chris Mason39279cc2007-06-12 06:35:45 -04005185 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5186 if (ret < 0)
5187 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005188
5189 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005190 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005191 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005192 if (slot >= btrfs_header_nritems(leaf)) {
5193 ret = btrfs_next_leaf(root, path);
5194 if (ret < 0)
5195 goto err;
5196 else if (ret > 0)
5197 break;
5198 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005199 }
Chris Mason3de45862008-11-17 21:02:50 -05005200
Ross Kirkdd3cc162013-09-16 15:58:09 +01005201 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04005202 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5203
5204 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005205 break;
Chris Mason5f39d392007-10-15 16:14:19 -04005206 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04005207 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005208 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005209 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08005210 if (key_type == BTRFS_DIR_INDEX_KEY &&
5211 btrfs_should_delete_dir_index(&del_list,
5212 found_key.offset))
5213 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005214
Al Viro9cdda8d2013-05-22 16:48:09 -04005215 ctx->pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08005216 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005217
Chris Mason39279cc2007-06-12 06:35:45 -04005218 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
5219 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005220 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01005221
5222 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04005223 struct btrfs_key location;
5224
Josef Bacik22a94d42011-03-16 16:47:17 -04005225 if (verify_dir_item(root, leaf, di))
5226 break;
5227
Chris Mason5f39d392007-10-15 16:14:19 -04005228 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01005229 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04005230 name_ptr = tmp_name;
5231 } else {
5232 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01005233 if (!name_ptr) {
5234 ret = -ENOMEM;
5235 goto err;
5236 }
Chris Mason5f39d392007-10-15 16:14:19 -04005237 }
5238 read_extent_buffer(leaf, name_ptr,
5239 (unsigned long)(di + 1), name_len);
5240
5241 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5242 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05005243
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005244
Chris Mason3de45862008-11-17 21:02:50 -05005245 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01005246 * skip it.
5247 *
5248 * In contrast to old kernels, we insert the snapshot's
5249 * dir item and dir index after it has been created, so
5250 * we won't find a reference to our own snapshot. We
5251 * still keep the following code for backward
5252 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05005253 */
5254 if (location.type == BTRFS_ROOT_ITEM_KEY &&
5255 location.objectid == root->root_key.objectid) {
5256 over = 0;
5257 goto skip;
5258 }
Al Viro9cdda8d2013-05-22 16:48:09 -04005259 over = !dir_emit(ctx, name_ptr, name_len,
5260 location.objectid, d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04005261
Chris Mason3de45862008-11-17 21:02:50 -05005262skip:
Chris Mason5f39d392007-10-15 16:14:19 -04005263 if (name_ptr != tmp_name)
5264 kfree(name_ptr);
5265
Chris Mason39279cc2007-06-12 06:35:45 -04005266 if (over)
5267 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05005268 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01005269 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04005270 di_cur += di_len;
5271 di = (struct btrfs_dir_item *)((char *)di + di_len);
5272 }
Li Zefanb9e03af2011-03-23 10:43:58 +08005273next:
5274 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005275 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005276
Miao Xie16cdcec2011-04-22 18:12:22 +08005277 if (key_type == BTRFS_DIR_INDEX_KEY) {
5278 if (is_curr)
Al Viro9cdda8d2013-05-22 16:48:09 -04005279 ctx->pos++;
5280 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005281 if (ret)
5282 goto nopos;
5283 }
5284
David Woodhouse49593bf2008-08-17 17:08:36 +01005285 /* Reached end of directory/root. Bump pos past the last item. */
Zach Browndb62efb2013-07-11 16:19:42 -07005286 ctx->pos++;
5287
5288 /*
5289 * Stop new entries from being returned after we return the last
5290 * entry.
5291 *
5292 * New directory entries are assigned a strictly increasing
5293 * offset. This means that new entries created during readdir
5294 * are *guaranteed* to be seen in the future by that readdir.
5295 * This has broken buggy programs which operate on names as
5296 * they're returned by readdir. Until we re-use freed offsets
5297 * we have this hack to stop new entries from being returned
5298 * under the assumption that they'll never reach this huge
5299 * offset.
5300 *
5301 * This is being careful not to overflow 32bit loff_t unless the
5302 * last entry requires it because doing so has broken 32bit apps
5303 * in the past.
5304 */
5305 if (key_type == BTRFS_DIR_INDEX_KEY) {
5306 if (ctx->pos >= INT_MAX)
5307 ctx->pos = LLONG_MAX;
5308 else
5309 ctx->pos = INT_MAX;
5310 }
Chris Mason39279cc2007-06-12 06:35:45 -04005311nopos:
5312 ret = 0;
5313err:
Miao Xie16cdcec2011-04-22 18:12:22 +08005314 if (key_type == BTRFS_DIR_INDEX_KEY)
5315 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005316 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005317 return ret;
5318}
5319
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005320int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005321{
5322 struct btrfs_root *root = BTRFS_I(inode)->root;
5323 struct btrfs_trans_handle *trans;
5324 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005325 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005326
Josef Bacik72ac3c02012-05-23 14:13:11 -04005327 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005328 return 0;
5329
Liu Bo83eea1f2012-07-10 05:28:39 -06005330 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08005331 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005332
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005333 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005334 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005335 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005336 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005337 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005338 if (IS_ERR(trans))
5339 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06005340 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005341 }
5342 return ret;
5343}
5344
5345/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005346 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005347 * inode changes. But, it is most likely to find the inode in cache.
5348 * FIXME, needs more benchmarking...there are no reasons other than performance
5349 * to keep or drop this code.
5350 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005351static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005352{
5353 struct btrfs_root *root = BTRFS_I(inode)->root;
5354 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005355 int ret;
5356
Josef Bacik72ac3c02012-05-23 14:13:11 -04005357 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005358 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005359
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005360 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005361 if (IS_ERR(trans))
5362 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005363
5364 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005365 if (ret && ret == -ENOSPC) {
5366 /* whoops, lets try again with the full transaction */
5367 btrfs_end_transaction(trans, root);
5368 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005369 if (IS_ERR(trans))
5370 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005371
Chris Mason94b60442010-05-26 11:02:00 -04005372 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005373 }
Chris Mason39279cc2007-06-12 06:35:45 -04005374 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08005375 if (BTRFS_I(inode)->delayed_node)
5376 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005377
5378 return ret;
5379}
5380
5381/*
5382 * This is a copy of file_update_time. We need this so we can return error on
5383 * ENOSPC for updating the inode in the case of file write and mmap writes.
5384 */
Josef Bacike41f9412012-03-26 09:46:47 -04005385static int btrfs_update_time(struct inode *inode, struct timespec *now,
5386 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005387{
Alexander Block2bc5565282012-06-15 09:49:33 +02005388 struct btrfs_root *root = BTRFS_I(inode)->root;
5389
5390 if (btrfs_root_readonly(root))
5391 return -EROFS;
5392
Josef Bacike41f9412012-03-26 09:46:47 -04005393 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005394 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04005395 if (flags & S_CTIME)
5396 inode->i_ctime = *now;
5397 if (flags & S_MTIME)
5398 inode->i_mtime = *now;
5399 if (flags & S_ATIME)
5400 inode->i_atime = *now;
5401 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005402}
5403
Chris Masond352ac62008-09-29 15:18:18 -04005404/*
5405 * find the highest existing sequence number in a directory
5406 * and then set the in-memory index_cnt variable to reflect
5407 * free sequence numbers
5408 */
Josef Bacikaec74772008-07-24 12:12:38 -04005409static int btrfs_set_inode_index_count(struct inode *inode)
5410{
5411 struct btrfs_root *root = BTRFS_I(inode)->root;
5412 struct btrfs_key key, found_key;
5413 struct btrfs_path *path;
5414 struct extent_buffer *leaf;
5415 int ret;
5416
Li Zefan33345d012011-04-20 10:31:50 +08005417 key.objectid = btrfs_ino(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005418 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
5419 key.offset = (u64)-1;
5420
5421 path = btrfs_alloc_path();
5422 if (!path)
5423 return -ENOMEM;
5424
5425 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5426 if (ret < 0)
5427 goto out;
5428 /* FIXME: we should be able to handle this */
5429 if (ret == 0)
5430 goto out;
5431 ret = 0;
5432
5433 /*
5434 * MAGIC NUMBER EXPLANATION:
5435 * since we search a directory based on f_pos we have to start at 2
5436 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5437 * else has to start at 2
5438 */
5439 if (path->slots[0] == 0) {
5440 BTRFS_I(inode)->index_cnt = 2;
5441 goto out;
5442 }
5443
5444 path->slots[0]--;
5445
5446 leaf = path->nodes[0];
5447 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5448
Li Zefan33345d012011-04-20 10:31:50 +08005449 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacikaec74772008-07-24 12:12:38 -04005450 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
5451 BTRFS_I(inode)->index_cnt = 2;
5452 goto out;
5453 }
5454
5455 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
5456out:
5457 btrfs_free_path(path);
5458 return ret;
5459}
5460
Chris Masond352ac62008-09-29 15:18:18 -04005461/*
5462 * helper to find a free sequence number in a given directory. This current
5463 * code is very simple, later versions will do smarter things in the btree
5464 */
Chris Mason3de45862008-11-17 21:02:50 -05005465int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04005466{
5467 int ret = 0;
5468
5469 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08005470 ret = btrfs_inode_delayed_dir_index_count(dir);
5471 if (ret) {
5472 ret = btrfs_set_inode_index_count(dir);
5473 if (ret)
5474 return ret;
5475 }
Josef Bacikaec74772008-07-24 12:12:38 -04005476 }
5477
Chris Mason00e4e6b2008-08-05 11:18:09 -04005478 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04005479 BTRFS_I(dir)->index_cnt++;
5480
5481 return ret;
5482}
5483
Chris Mason39279cc2007-06-12 06:35:45 -04005484static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5485 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04005486 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05005487 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04005488 u64 ref_objectid, u64 objectid,
5489 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04005490{
5491 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005492 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04005493 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04005494 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05005495 struct btrfs_inode_ref *ref;
5496 struct btrfs_key key[2];
5497 u32 sizes[2];
5498 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04005499 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005500
Chris Mason5f39d392007-10-15 16:14:19 -04005501 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005502 if (!path)
5503 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04005504
Chris Mason39279cc2007-06-12 06:35:45 -04005505 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005506 if (!inode) {
5507 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005508 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005509 }
Chris Mason39279cc2007-06-12 06:35:45 -04005510
Li Zefan581bb052011-04-20 10:06:11 +08005511 /*
5512 * we have to initialize this early, so we can reclaim the inode
5513 * number if we fail afterwards in this function.
5514 */
5515 inode->i_ino = objectid;
5516
Josef Bacikaec74772008-07-24 12:12:38 -04005517 if (dir) {
liubo1abe9b82011-03-24 11:18:59 +00005518 trace_btrfs_inode_request(dir);
5519
Chris Mason3de45862008-11-17 21:02:50 -05005520 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04005521 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005522 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005523 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005524 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04005525 }
Josef Bacikaec74772008-07-24 12:12:38 -04005526 }
5527 /*
5528 * index_cnt is ignored for everything but a dir,
5529 * btrfs_get_inode_index_count has an explanation for the magic
5530 * number
5531 */
5532 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08005533 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04005534 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04005535 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00005536 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db2b2007-08-27 16:49:44 -04005537
Josef Bacik5dc562c2012-08-17 13:14:17 -04005538 /*
5539 * We could have gotten an inode number from somebody who was fsynced
5540 * and then removed in this same transaction, so let's just set full
5541 * sync since it will be a full sync anyway and this will blow away the
5542 * old info in the log.
5543 */
5544 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
5545
Chris Mason9c583092008-01-29 15:15:18 -05005546 key[0].objectid = objectid;
5547 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
5548 key[0].offset = 0;
5549
Mark Fashehf1863732012-08-08 11:32:27 -07005550 /*
5551 * Start new inodes with an inode_ref. This is slightly more
5552 * efficient for small numbers of hard links since they will
5553 * be packed into one item. Extended refs will kick in if we
5554 * add more hard links than can fit in the ref item.
5555 */
Chris Mason9c583092008-01-29 15:15:18 -05005556 key[1].objectid = objectid;
5557 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
5558 key[1].offset = ref_objectid;
5559
5560 sizes[0] = sizeof(struct btrfs_inode_item);
5561 sizes[1] = name_len + sizeof(*ref);
5562
Chris Masonb9473432009-03-13 11:00:37 -04005563 path->leave_spinning = 1;
Chris Mason9c583092008-01-29 15:15:18 -05005564 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
5565 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04005566 goto fail;
5567
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03005568 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04005569 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005570 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04005571 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
5572 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06005573 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
5574 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04005575 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05005576
5577 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
5578 struct btrfs_inode_ref);
5579 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04005580 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05005581 ptr = (unsigned long)(ref + 1);
5582 write_extent_buffer(path->nodes[0], name, ptr, name_len);
5583
Chris Mason5f39d392007-10-15 16:14:19 -04005584 btrfs_mark_buffer_dirty(path->nodes[0]);
5585 btrfs_free_path(path);
5586
Chris Mason39279cc2007-06-12 06:35:45 -04005587 location = &BTRFS_I(inode)->location;
5588 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04005589 location->offset = 0;
5590 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
5591
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005592 btrfs_inherit_iflags(inode, dir);
5593
Al Viro569254b2011-07-24 17:08:40 -04005594 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04005595 if (btrfs_test_opt(root, NODATASUM))
5596 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo213490b2012-09-11 08:33:50 -06005597 if (btrfs_test_opt(root, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05005598 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
5599 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04005600 }
5601
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005602 btrfs_insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005603 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00005604
5605 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04005606 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00005607
Alexander Block8ea05e32012-07-25 17:35:53 +02005608 btrfs_update_root_times(trans, root);
5609
Chris Mason39279cc2007-06-12 06:35:45 -04005610 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005611fail:
Josef Bacikaec74772008-07-24 12:12:38 -04005612 if (dir)
5613 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04005614 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005615 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005616 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005617}
5618
5619static inline u8 btrfs_inode_type(struct inode *inode)
5620{
5621 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
5622}
5623
Chris Masond352ac62008-09-29 15:18:18 -04005624/*
5625 * utility function to add 'inode' into 'parent_inode' with
5626 * a give name and a given sequence number.
5627 * if 'add_backref' is true, also insert a backref from the
5628 * inode to the parent directory.
5629 */
Chris Masone02119d2008-09-05 16:13:11 -04005630int btrfs_add_link(struct btrfs_trans_handle *trans,
5631 struct inode *parent_inode, struct inode *inode,
5632 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005633{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005634 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005635 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04005636 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08005637 u64 ino = btrfs_ino(inode);
5638 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005639
Li Zefan33345d012011-04-20 10:31:50 +08005640 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005641 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
5642 } else {
Li Zefan33345d012011-04-20 10:31:50 +08005643 key.objectid = ino;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005644 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
5645 key.offset = 0;
5646 }
Chris Mason39279cc2007-06-12 06:35:45 -04005647
Li Zefan33345d012011-04-20 10:31:50 +08005648 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005649 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
5650 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08005651 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005652 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08005653 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
5654 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005655 }
5656
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005657 /* Nothing to clean up yet */
5658 if (ret)
5659 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005660
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005661 ret = btrfs_insert_dir_item(trans, root, name, name_len,
5662 parent_inode, &key,
5663 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05005664 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005665 goto fail_dir_item;
5666 else if (ret) {
5667 btrfs_abort_transaction(trans, root, ret);
5668 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005669 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005670
5671 btrfs_i_size_write(parent_inode, parent_inode->i_size +
5672 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005673 inode_inc_iversion(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005674 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
5675 ret = btrfs_update_inode(trans, root, parent_inode);
5676 if (ret)
5677 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005678 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05005679
5680fail_dir_item:
5681 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
5682 u64 local_index;
5683 int err;
5684 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
5685 key.objectid, root->root_key.objectid,
5686 parent_ino, &local_index, name, name_len);
5687
5688 } else if (add_backref) {
5689 u64 local_index;
5690 int err;
5691
5692 err = btrfs_del_inode_ref(trans, root, name, name_len,
5693 ino, parent_ino, &local_index);
5694 }
5695 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005696}
5697
5698static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00005699 struct inode *dir, struct dentry *dentry,
5700 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005701{
Josef Bacika1b075d2010-11-19 20:36:11 +00005702 int err = btrfs_add_link(trans, dir, inode,
5703 dentry->d_name.name, dentry->d_name.len,
5704 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005705 if (err > 0)
5706 err = -EEXIST;
5707 return err;
5708}
5709
Josef Bacik618e21d2007-07-11 10:18:17 -04005710static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04005711 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04005712{
5713 struct btrfs_trans_handle *trans;
5714 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005715 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04005716 int err;
5717 int drop_inode = 0;
5718 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005719 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04005720
5721 if (!new_valid_dev(rdev))
5722 return -EINVAL;
5723
Josef Bacik9ed74f22009-09-11 16:12:44 -04005724 /*
5725 * 2 for inode item and ref
5726 * 2 for dir items
5727 * 1 for xattr if selinux is on
5728 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005729 trans = btrfs_start_transaction(root, 5);
5730 if (IS_ERR(trans))
5731 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05005732
Li Zefan581bb052011-04-20 10:06:11 +08005733 err = btrfs_find_free_ino(root, &objectid);
5734 if (err)
5735 goto out_unlock;
5736
Josef Bacikaec74772008-07-24 12:12:38 -04005737 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005738 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04005739 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005740 if (IS_ERR(inode)) {
5741 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005742 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005743 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005744
Eric Paris2a7dba32011-02-01 11:05:39 -05005745 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005746 if (err) {
5747 drop_inode = 1;
5748 goto out_unlock;
5749 }
5750
Casey Schauflerad19db72011-12-15 10:09:07 -05005751 /*
5752 * If the active LSM wants to access the inode during
5753 * d_instantiate it needs these. Smack checks to see
5754 * if the filesystem supports xattrs by looking at the
5755 * ops vector.
5756 */
5757
5758 inode->i_op = &btrfs_special_inode_operations;
Josef Bacika1b075d2010-11-19 20:36:11 +00005759 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04005760 if (err)
5761 drop_inode = 1;
5762 else {
Josef Bacik618e21d2007-07-11 10:18:17 -04005763 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04005764 btrfs_update_inode(trans, root, inode);
Al Viro08c422c2011-12-23 07:58:13 -05005765 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005766 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005767out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005768 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00005769 btrfs_btree_balance_dirty(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04005770 if (drop_inode) {
5771 inode_dec_link_count(inode);
5772 iput(inode);
5773 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005774 return err;
5775}
5776
Chris Mason39279cc2007-06-12 06:35:45 -04005777static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04005778 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04005779{
5780 struct btrfs_trans_handle *trans;
5781 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005782 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005783 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04005784 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04005785 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005786 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005787
Josef Bacik9ed74f22009-09-11 16:12:44 -04005788 /*
5789 * 2 for inode item and ref
5790 * 2 for dir items
5791 * 1 for xattr if selinux is on
5792 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005793 trans = btrfs_start_transaction(root, 5);
5794 if (IS_ERR(trans))
5795 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005796
Li Zefan581bb052011-04-20 10:06:11 +08005797 err = btrfs_find_free_ino(root, &objectid);
5798 if (err)
5799 goto out_unlock;
5800
Josef Bacikaec74772008-07-24 12:12:38 -04005801 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005802 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04005803 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005804 if (IS_ERR(inode)) {
5805 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005806 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005807 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005808 drop_inode_on_err = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04005809
Eric Paris2a7dba32011-02-01 11:05:39 -05005810 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005811 if (err)
Josef Bacik33268ea2008-07-24 12:16:36 -04005812 goto out_unlock;
Josef Bacik33268ea2008-07-24 12:16:36 -04005813
Filipe Brandenburger9185aa52012-11-30 03:40:08 +00005814 err = btrfs_update_inode(trans, root, inode);
5815 if (err)
5816 goto out_unlock;
5817
Casey Schauflerad19db72011-12-15 10:09:07 -05005818 /*
5819 * If the active LSM wants to access the inode during
5820 * d_instantiate it needs these. Smack checks to see
5821 * if the filesystem supports xattrs by looking at the
5822 * ops vector.
5823 */
5824 inode->i_fop = &btrfs_file_operations;
5825 inode->i_op = &btrfs_file_inode_operations;
5826
Josef Bacika1b075d2010-11-19 20:36:11 +00005827 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005828 if (err)
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005829 goto out_unlock;
5830
5831 inode->i_mapping->a_ops = &btrfs_aops;
5832 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
5833 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
5834 d_instantiate(dentry, inode);
5835
Chris Mason39279cc2007-06-12 06:35:45 -04005836out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005837 btrfs_end_transaction(trans, root);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005838 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005839 inode_dec_link_count(inode);
5840 iput(inode);
5841 }
Liu Bob53d3f52012-11-14 14:34:34 +00005842 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005843 return err;
5844}
5845
5846static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
5847 struct dentry *dentry)
5848{
5849 struct btrfs_trans_handle *trans;
5850 struct btrfs_root *root = BTRFS_I(dir)->root;
5851 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005852 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04005853 int err;
5854 int drop_inode = 0;
5855
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005856 /* do not allow sys_link's with other subvols of the same device */
5857 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00005858 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005859
Mark Fashehf1863732012-08-08 11:32:27 -07005860 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00005861 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04005862
Chris Mason3de45862008-11-17 21:02:50 -05005863 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04005864 if (err)
5865 goto fail;
5866
Yan, Zhenga22285a2010-05-16 10:48:46 -04005867 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00005868 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04005869 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00005870 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04005871 */
Miao Xie7e6b6462011-02-18 09:21:17 +00005872 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005873 if (IS_ERR(trans)) {
5874 err = PTR_ERR(trans);
5875 goto fail;
5876 }
Chris Mason5f39d392007-10-15 16:14:19 -04005877
Miao Xie67de1172013-12-26 13:07:06 +08005878 /* There are several dir indexes for this inode, clear the cache. */
5879 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07005880 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005881 inode_inc_iversion(inode);
Miao Xie31534952011-04-13 13:19:21 +08005882 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6ee2010-10-23 11:11:40 -04005883 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04005884 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04005885
Josef Bacika1b075d2010-11-19 20:36:11 +00005886 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04005887
Yan, Zhenga5719522009-09-24 09:17:31 -04005888 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005889 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04005890 } else {
Al Viro10d9f302011-07-16 23:09:10 -04005891 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04005892 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005893 if (err)
5894 goto fail;
Al Viro08c422c2011-12-23 07:58:13 -05005895 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00005896 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04005897 }
Chris Mason39279cc2007-06-12 06:35:45 -04005898
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005899 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05005900fail:
Chris Mason39279cc2007-06-12 06:35:45 -04005901 if (drop_inode) {
5902 inode_dec_link_count(inode);
5903 iput(inode);
5904 }
Liu Bob53d3f52012-11-14 14:34:34 +00005905 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005906 return err;
5907}
5908
Al Viro18bb1db2011-07-26 01:41:39 -04005909static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04005910{
Chris Masonb9d86662008-05-02 16:13:49 -04005911 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04005912 struct btrfs_trans_handle *trans;
5913 struct btrfs_root *root = BTRFS_I(dir)->root;
5914 int err = 0;
5915 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04005916 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005917 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005918
Josef Bacik9ed74f22009-09-11 16:12:44 -04005919 /*
5920 * 2 items for inode and ref
5921 * 2 items for dir items
5922 * 1 for xattr if selinux is on
5923 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005924 trans = btrfs_start_transaction(root, 5);
5925 if (IS_ERR(trans))
5926 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04005927
Li Zefan581bb052011-04-20 10:06:11 +08005928 err = btrfs_find_free_ino(root, &objectid);
5929 if (err)
5930 goto out_fail;
5931
Josef Bacikaec74772008-07-24 12:12:38 -04005932 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005933 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04005934 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04005935 if (IS_ERR(inode)) {
5936 err = PTR_ERR(inode);
5937 goto out_fail;
5938 }
Chris Mason5f39d392007-10-15 16:14:19 -04005939
Chris Mason39279cc2007-06-12 06:35:45 -04005940 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04005941
Eric Paris2a7dba32011-02-01 11:05:39 -05005942 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005943 if (err)
5944 goto out_fail;
5945
Chris Mason39279cc2007-06-12 06:35:45 -04005946 inode->i_op = &btrfs_dir_inode_operations;
5947 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04005948
Chris Masondbe674a2008-07-17 12:54:05 -04005949 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005950 err = btrfs_update_inode(trans, root, inode);
5951 if (err)
5952 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005953
Josef Bacika1b075d2010-11-19 20:36:11 +00005954 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
5955 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005956 if (err)
5957 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005958
Chris Mason39279cc2007-06-12 06:35:45 -04005959 d_instantiate(dentry, inode);
5960 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005961
5962out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005963 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005964 if (drop_on_err)
5965 iput(inode);
Liu Bob53d3f52012-11-14 14:34:34 +00005966 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005967 return err;
5968}
5969
Chris Masond352ac62008-09-29 15:18:18 -04005970/* helper for btfs_get_extent. Given an existing extent in the tree,
5971 * and an extent that you want to insert, deal with overlap and insert
5972 * the new extent into the tree.
5973 */
Chris Mason3b951512008-04-17 11:29:12 -04005974static int merge_extent_mapping(struct extent_map_tree *em_tree,
5975 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005976 struct extent_map *em,
5977 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04005978{
5979 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04005980
Chris Masone6dcd2d2008-07-17 12:53:50 -04005981 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
5982 start_diff = map_start - em->start;
5983 em->start = map_start;
5984 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04005985 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
5986 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04005987 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04005988 em->block_len -= start_diff;
5989 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005990 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04005991}
5992
Chris Masonc8b97812008-10-29 14:49:59 -04005993static noinline int uncompress_inline(struct btrfs_path *path,
5994 struct inode *inode, struct page *page,
5995 size_t pg_offset, u64 extent_offset,
5996 struct btrfs_file_extent_item *item)
5997{
5998 int ret;
5999 struct extent_buffer *leaf = path->nodes[0];
6000 char *tmp;
6001 size_t max_size;
6002 unsigned long inline_size;
6003 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006004 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006005
6006 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006007 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006008 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6009 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006010 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006011 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006012 if (!tmp)
6013 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006014 ptr = btrfs_file_extent_inline_start(item);
6015
6016 read_extent_buffer(leaf, tmp, ptr, inline_size);
6017
Chris Mason5b050f02008-11-11 09:34:41 -05006018 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006019 ret = btrfs_decompress(compress_type, tmp, page,
6020 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006021 if (ret) {
Cong Wang7ac687d2011-11-25 23:14:28 +08006022 char *kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04006023 unsigned long copy_size = min_t(u64,
6024 PAGE_CACHE_SIZE - pg_offset,
6025 max_size - extent_offset);
6026 memset(kaddr + pg_offset, 0, copy_size);
Cong Wang7ac687d2011-11-25 23:14:28 +08006027 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -04006028 }
6029 kfree(tmp);
6030 return 0;
6031}
6032
Chris Masond352ac62008-09-29 15:18:18 -04006033/*
6034 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006035 * the ugly parts come from merging extents from the disk with the in-ram
6036 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006037 * where the in-ram extents might be locked pending data=ordered completion.
6038 *
6039 * This also copies inline extents directly into the page.
6040 */
Chris Masond3977122009-01-05 21:25:51 -05006041
Chris Masona52d9a82007-08-27 16:49:44 -04006042struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05006043 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04006044 int create)
6045{
6046 int ret;
6047 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04006048 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04006049 u64 extent_start = 0;
6050 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08006051 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006052 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006053 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04006054 struct btrfs_root *root = BTRFS_I(inode)->root;
6055 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006056 struct extent_buffer *leaf;
6057 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006058 struct extent_map *em = NULL;
6059 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05006060 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006061 struct btrfs_trans_handle *trans = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08006062 int compress_type;
Chris Masona52d9a82007-08-27 16:49:44 -04006063
Chris Masona52d9a82007-08-27 16:49:44 -04006064again:
Chris Mason890871b2009-09-02 16:24:52 -04006065 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006066 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006067 if (em)
6068 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006069 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006070
Chris Masona52d9a82007-08-27 16:49:44 -04006071 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006072 if (em->start > start || em->start + em->len <= start)
6073 free_extent_map(em);
6074 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006075 free_extent_map(em);
6076 else
6077 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006078 }
David Sterba172ddd62011-04-21 00:48:27 +02006079 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006080 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006081 err = -ENOMEM;
6082 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006083 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006084 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006085 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006086 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006087 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006088 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006089
6090 if (!path) {
6091 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006092 if (!path) {
6093 err = -ENOMEM;
6094 goto out;
6095 }
6096 /*
6097 * Chances are we'll be called again, so go ahead and do
6098 * readahead
6099 */
6100 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04006101 }
6102
Chris Mason179e29e2007-11-01 11:28:41 -04006103 ret = btrfs_lookup_file_extent(trans, root, path,
6104 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006105 if (ret < 0) {
6106 err = ret;
6107 goto out;
6108 }
6109
6110 if (ret != 0) {
6111 if (path->slots[0] == 0)
6112 goto not_found;
6113 path->slots[0]--;
6114 }
6115
Chris Mason5f39d392007-10-15 16:14:19 -04006116 leaf = path->nodes[0];
6117 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006118 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006119 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006120 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6121 found_type = btrfs_key_type(&found_key);
6122 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006123 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006124 /*
6125 * If we backup past the first extent we want to move forward
6126 * and see if there is an extent in front of us, otherwise we'll
6127 * say there is a hole for our whole search range which can
6128 * cause problems.
6129 */
6130 extent_end = start;
6131 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006132 }
6133
Chris Mason5f39d392007-10-15 16:14:19 -04006134 found_type = btrfs_file_extent_type(leaf, item);
6135 extent_start = found_key.offset;
Li Zefan261507a02010-12-17 14:21:50 +08006136 compress_type = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04006137 if (found_type == BTRFS_FILE_EXTENT_REG ||
6138 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006139 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006140 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006141 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6142 size_t size;
6143 size = btrfs_file_extent_inline_len(leaf, item);
Qu Wenruofda28322013-02-26 08:10:22 +00006144 extent_end = ALIGN(extent_start + size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04006145 }
Josef Bacik25a50342013-10-14 12:08:38 -04006146next:
Yan Zheng9036c102008-10-30 14:19:41 -04006147 if (start >= extent_end) {
6148 path->slots[0]++;
6149 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6150 ret = btrfs_next_leaf(root, path);
6151 if (ret < 0) {
6152 err = ret;
6153 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006154 }
Yan Zheng9036c102008-10-30 14:19:41 -04006155 if (ret > 0)
6156 goto not_found;
6157 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006158 }
Yan Zheng9036c102008-10-30 14:19:41 -04006159 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6160 if (found_key.objectid != objectid ||
6161 found_key.type != BTRFS_EXTENT_DATA_KEY)
6162 goto not_found;
6163 if (start + len <= found_key.offset)
6164 goto not_found;
6165 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006166 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006167 em->len = found_key.offset - start;
6168 goto not_found_em;
6169 }
6170
Josef Bacikcc95bef2013-04-04 14:31:27 -04006171 em->ram_bytes = btrfs_file_extent_ram_bytes(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04006172 if (found_type == BTRFS_FILE_EXTENT_REG ||
6173 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04006174 em->start = extent_start;
6175 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05006176 em->orig_start = extent_start -
6177 btrfs_file_extent_offset(leaf, item);
Josef Bacikb4939682012-12-03 10:31:19 -05006178 em->orig_block_len = btrfs_file_extent_disk_num_bytes(leaf,
6179 item);
Chris Masondb945352007-10-15 16:15:53 -04006180 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
6181 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04006182 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04006183 goto insert;
6184 }
Li Zefan261507a02010-12-17 14:21:50 +08006185 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006186 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08006187 em->compress_type = compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006188 em->block_start = bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05006189 em->block_len = em->orig_block_len;
Chris Masonc8b97812008-10-29 14:49:59 -04006190 } else {
6191 bytenr += btrfs_file_extent_offset(leaf, item);
6192 em->block_start = bytenr;
6193 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04006194 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
6195 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04006196 }
Chris Masona52d9a82007-08-27 16:49:44 -04006197 goto insert;
6198 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006199 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006200 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006201 size_t size;
6202 size_t extent_offset;
6203 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006204
Chris Masona52d9a82007-08-27 16:49:44 -04006205 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04006206 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04006207 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04006208 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04006209 goto out;
6210 }
6211
Yan Zheng9036c102008-10-30 14:19:41 -04006212 size = btrfs_file_extent_inline_len(leaf, item);
6213 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05006214 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04006215 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006216 em->start = extent_start + extent_offset;
Qu Wenruofda28322013-02-26 08:10:22 +00006217 em->len = ALIGN(copy_size, root->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006218 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006219 em->orig_start = em->start;
Li Zefan261507a02010-12-17 14:21:50 +08006220 if (compress_type) {
Chris Masonc8b97812008-10-29 14:49:59 -04006221 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08006222 em->compress_type = compress_type;
6223 }
Yan689f9342007-10-29 11:41:07 -04006224 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006225 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006226 if (btrfs_file_extent_compression(leaf, item) !=
6227 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006228 ret = uncompress_inline(path, inode, page,
6229 pg_offset,
6230 extent_offset, item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006231 BUG_ON(ret); /* -ENOMEM */
Chris Masonc8b97812008-10-29 14:49:59 -04006232 } else {
6233 map = kmap(page);
6234 read_extent_buffer(leaf, map + pg_offset, ptr,
6235 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04006236 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
6237 memset(map + pg_offset + copy_size, 0,
6238 PAGE_CACHE_SIZE - pg_offset -
6239 copy_size);
6240 }
Chris Masonc8b97812008-10-29 14:49:59 -04006241 kunmap(page);
6242 }
Chris Mason179e29e2007-11-01 11:28:41 -04006243 flush_dcache_page(page);
6244 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006245 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006246 if (!trans) {
6247 kunmap(page);
6248 free_extent_map(em);
6249 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006250
David Sterbab3b4aa72011-04-21 01:20:15 +02006251 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006252 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006253
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006254 if (IS_ERR(trans))
6255 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006256 goto again;
6257 }
Chris Masonc8b97812008-10-29 14:49:59 -04006258 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006259 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006260 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006261 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006262 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006263 }
Chris Masond1310b22008-01-24 16:13:08 -05006264 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006265 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006266 goto insert;
6267 } else {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00006268 WARN(1, KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04006269 }
6270not_found:
6271 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006272 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006273 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006274not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04006275 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04006276 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006277insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006278 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006279 if (em->start > start || extent_map_end(em) <= start) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006280 btrfs_err(root->fs_info, "bad extent! em: [%llu %llu] passed [%llu %llu]",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006281 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04006282 err = -EIO;
6283 goto out;
6284 }
Chris Masond1310b22008-01-24 16:13:08 -05006285
6286 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006287 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006288 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006289 /* it is possible that someone inserted the extent into the tree
6290 * while we had the lock dropped. It is also possible that
6291 * an overlapping map exists in the tree
6292 */
Chris Masona52d9a82007-08-27 16:49:44 -04006293 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04006294 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006295
6296 ret = 0;
6297
Chris Mason3b951512008-04-17 11:29:12 -04006298 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04006299 if (existing && (existing->start > start ||
6300 existing->start + existing->len <= start)) {
6301 free_extent_map(existing);
6302 existing = NULL;
6303 }
Chris Mason3b951512008-04-17 11:29:12 -04006304 if (!existing) {
6305 existing = lookup_extent_mapping(em_tree, em->start,
6306 em->len);
6307 if (existing) {
6308 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006309 em, start,
6310 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04006311 free_extent_map(existing);
6312 if (err) {
6313 free_extent_map(em);
6314 em = NULL;
6315 }
6316 } else {
6317 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04006318 free_extent_map(em);
6319 em = NULL;
6320 }
6321 } else {
6322 free_extent_map(em);
6323 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006324 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006325 }
Chris Masona52d9a82007-08-27 16:49:44 -04006326 }
Chris Mason890871b2009-09-02 16:24:52 -04006327 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006328out:
liubo1abe9b82011-03-24 11:18:59 +00006329
Steven Rostedt4cd85872013-11-14 22:57:29 -05006330 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00006331
Chris Masonf4219502008-07-22 11:18:09 -04006332 if (path)
6333 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04006334 if (trans) {
6335 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05006336 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04006337 err = ret;
6338 }
Chris Masona52d9a82007-08-27 16:49:44 -04006339 if (err) {
6340 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04006341 return ERR_PTR(err);
6342 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006343 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04006344 return em;
6345}
6346
Chris Masonec29ed52011-02-23 16:23:20 -05006347struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
6348 size_t pg_offset, u64 start, u64 len,
6349 int create)
6350{
6351 struct extent_map *em;
6352 struct extent_map *hole_em = NULL;
6353 u64 range_start = start;
6354 u64 end;
6355 u64 found;
6356 u64 found_end;
6357 int err = 0;
6358
6359 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
6360 if (IS_ERR(em))
6361 return em;
6362 if (em) {
6363 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00006364 * if our em maps to
6365 * - a hole or
6366 * - a pre-alloc extent,
6367 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05006368 */
Liu Bof9e4fb52013-01-07 10:10:12 +00006369 if (em->block_start != EXTENT_MAP_HOLE &&
6370 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05006371 return em;
6372 else
6373 hole_em = em;
6374 }
6375
6376 /* check to see if we've wrapped (len == -1 or similar) */
6377 end = start + len;
6378 if (end < start)
6379 end = (u64)-1;
6380 else
6381 end -= 1;
6382
6383 em = NULL;
6384
6385 /* ok, we didn't find anything, lets look for delalloc */
6386 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
6387 end, len, EXTENT_DELALLOC, 1);
6388 found_end = range_start + found;
6389 if (found_end < range_start)
6390 found_end = (u64)-1;
6391
6392 /*
6393 * we didn't find anything useful, return
6394 * the original results from get_extent()
6395 */
6396 if (range_start > end || found_end <= start) {
6397 em = hole_em;
6398 hole_em = NULL;
6399 goto out;
6400 }
6401
6402 /* adjust the range_start to make sure it doesn't
6403 * go backwards from the start they passed in
6404 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05306405 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05006406 found = found_end - range_start;
6407
6408 if (found > 0) {
6409 u64 hole_start = start;
6410 u64 hole_len = len;
6411
David Sterba172ddd62011-04-21 00:48:27 +02006412 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05006413 if (!em) {
6414 err = -ENOMEM;
6415 goto out;
6416 }
6417 /*
6418 * when btrfs_get_extent can't find anything it
6419 * returns one huge hole
6420 *
6421 * make sure what it found really fits our range, and
6422 * adjust to make sure it is based on the start from
6423 * the caller
6424 */
6425 if (hole_em) {
6426 u64 calc_end = extent_map_end(hole_em);
6427
6428 if (calc_end <= start || (hole_em->start > end)) {
6429 free_extent_map(hole_em);
6430 hole_em = NULL;
6431 } else {
6432 hole_start = max(hole_em->start, start);
6433 hole_len = calc_end - hole_start;
6434 }
6435 }
6436 em->bdev = NULL;
6437 if (hole_em && range_start > hole_start) {
6438 /* our hole starts before our delalloc, so we
6439 * have to return just the parts of the hole
6440 * that go until the delalloc starts
6441 */
6442 em->len = min(hole_len,
6443 range_start - hole_start);
6444 em->start = hole_start;
6445 em->orig_start = hole_start;
6446 /*
6447 * don't adjust block start at all,
6448 * it is fixed at EXTENT_MAP_HOLE
6449 */
6450 em->block_start = hole_em->block_start;
6451 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00006452 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
6453 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05006454 } else {
6455 em->start = range_start;
6456 em->len = found;
6457 em->orig_start = range_start;
6458 em->block_start = EXTENT_MAP_DELALLOC;
6459 em->block_len = found;
6460 }
6461 } else if (hole_em) {
6462 return hole_em;
6463 }
6464out:
6465
6466 free_extent_map(hole_em);
6467 if (err) {
6468 free_extent_map(em);
6469 return ERR_PTR(err);
6470 }
6471 return em;
6472}
6473
Josef Bacik4b46fce2010-05-23 11:00:55 -04006474static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
6475 u64 start, u64 len)
6476{
6477 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04006478 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006479 struct btrfs_key ins;
6480 u64 alloc_hint;
6481 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006482
Josef Bacik4b46fce2010-05-23 11:00:55 -04006483 alloc_hint = get_extent_allocation_hint(inode, start, len);
Josef Bacik00361582013-08-14 14:02:47 -04006484 ret = btrfs_reserve_extent(root, len, root->sectorsize, 0,
Josef Bacik81c9ad22012-01-18 10:56:06 -05006485 alloc_hint, &ins, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006486 if (ret)
6487 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006488
Josef Bacik70c8a912012-10-11 16:54:30 -04006489 em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006490 ins.offset, ins.offset, ins.offset, 0);
Josef Bacik00361582013-08-14 14:02:47 -04006491 if (IS_ERR(em)) {
6492 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
6493 return em;
6494 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006495
6496 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
6497 ins.offset, ins.offset, 0);
6498 if (ret) {
6499 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
Josef Bacik00361582013-08-14 14:02:47 -04006500 free_extent_map(em);
6501 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006502 }
Josef Bacik00361582013-08-14 14:02:47 -04006503
Josef Bacik4b46fce2010-05-23 11:00:55 -04006504 return em;
6505}
6506
Chris Mason46bfbb52010-05-26 11:04:10 -04006507/*
6508 * returns 1 when the nocow is safe, < 1 on error, 0 if the
6509 * block must be cow'd
6510 */
Josef Bacik00361582013-08-14 14:02:47 -04006511noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04006512 u64 *orig_start, u64 *orig_block_len,
6513 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04006514{
Josef Bacik00361582013-08-14 14:02:47 -04006515 struct btrfs_trans_handle *trans;
Chris Mason46bfbb52010-05-26 11:04:10 -04006516 struct btrfs_path *path;
6517 int ret;
6518 struct extent_buffer *leaf;
6519 struct btrfs_root *root = BTRFS_I(inode)->root;
6520 struct btrfs_file_extent_item *fi;
6521 struct btrfs_key key;
6522 u64 disk_bytenr;
6523 u64 backref_offset;
6524 u64 extent_end;
6525 u64 num_bytes;
6526 int slot;
6527 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04006528 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08006529
Chris Mason46bfbb52010-05-26 11:04:10 -04006530 path = btrfs_alloc_path();
6531 if (!path)
6532 return -ENOMEM;
6533
Josef Bacik00361582013-08-14 14:02:47 -04006534 ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04006535 offset, 0);
6536 if (ret < 0)
6537 goto out;
6538
6539 slot = path->slots[0];
6540 if (ret == 1) {
6541 if (slot == 0) {
6542 /* can't find the item, must cow */
6543 ret = 0;
6544 goto out;
6545 }
6546 slot--;
6547 }
6548 ret = 0;
6549 leaf = path->nodes[0];
6550 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08006551 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04006552 key.type != BTRFS_EXTENT_DATA_KEY) {
6553 /* not our file or wrong item type, must cow */
6554 goto out;
6555 }
6556
6557 if (key.offset > offset) {
6558 /* Wrong offset, must cow */
6559 goto out;
6560 }
6561
6562 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
6563 found_type = btrfs_file_extent_type(leaf, fi);
6564 if (found_type != BTRFS_FILE_EXTENT_REG &&
6565 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
6566 /* not a regular extent, must cow */
6567 goto out;
6568 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04006569
6570 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
6571 goto out;
6572
Miao Xiee77751a2013-12-27 21:11:50 +08006573 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
6574 if (extent_end <= offset)
6575 goto out;
6576
Chris Mason46bfbb52010-05-26 11:04:10 -04006577 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04006578 if (disk_bytenr == 0)
6579 goto out;
6580
6581 if (btrfs_file_extent_compression(leaf, fi) ||
6582 btrfs_file_extent_encryption(leaf, fi) ||
6583 btrfs_file_extent_other_encoding(leaf, fi))
6584 goto out;
6585
Chris Mason46bfbb52010-05-26 11:04:10 -04006586 backref_offset = btrfs_file_extent_offset(leaf, fi);
6587
Josef Bacik7ee9e442013-06-21 16:37:03 -04006588 if (orig_start) {
6589 *orig_start = key.offset - backref_offset;
6590 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
6591 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
6592 }
Josef Bacikeb384b52013-04-24 16:32:55 -04006593
Chris Mason46bfbb52010-05-26 11:04:10 -04006594 if (btrfs_extent_readonly(root, disk_bytenr))
6595 goto out;
Josef Bacik1bda19e2013-10-18 12:10:36 -04006596 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04006597
6598 /*
6599 * look for other files referencing this extent, if we
6600 * find any we must cow
6601 */
Josef Bacik00361582013-08-14 14:02:47 -04006602 trans = btrfs_join_transaction(root);
6603 if (IS_ERR(trans)) {
6604 ret = 0;
Chris Mason46bfbb52010-05-26 11:04:10 -04006605 goto out;
Josef Bacik00361582013-08-14 14:02:47 -04006606 }
6607
6608 ret = btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
6609 key.offset - backref_offset, disk_bytenr);
6610 btrfs_end_transaction(trans, root);
6611 if (ret) {
6612 ret = 0;
6613 goto out;
6614 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006615
6616 /*
6617 * adjust disk_bytenr and num_bytes to cover just the bytes
6618 * in this extent we are about to write. If there
6619 * are any csums in that range we have to cow in order
6620 * to keep the csums correct
6621 */
6622 disk_bytenr += backref_offset;
6623 disk_bytenr += offset - key.offset;
Josef Bacikeb384b52013-04-24 16:32:55 -04006624 num_bytes = min(offset + *len, extent_end) - offset;
Chris Mason46bfbb52010-05-26 11:04:10 -04006625 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
6626 goto out;
6627 /*
6628 * all of the above have passed, it is safe to overwrite this extent
6629 * without cow
6630 */
Josef Bacikeb384b52013-04-24 16:32:55 -04006631 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04006632 ret = 1;
6633out:
6634 btrfs_free_path(path);
6635 return ret;
6636}
6637
Josef Bacikeb838e72012-07-31 16:28:48 -04006638static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
6639 struct extent_state **cached_state, int writing)
6640{
6641 struct btrfs_ordered_extent *ordered;
6642 int ret = 0;
6643
6644 while (1) {
6645 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6646 0, cached_state);
6647 /*
6648 * We're concerned with the entire range that we're going to be
6649 * doing DIO to, so we need to make sure theres no ordered
6650 * extents in this range.
6651 */
6652 ordered = btrfs_lookup_ordered_range(inode, lockstart,
6653 lockend - lockstart + 1);
6654
6655 /*
6656 * We need to make sure there are no buffered pages in this
6657 * range either, we could have raced between the invalidate in
6658 * generic_file_direct_write and locking the extent. The
6659 * invalidate needs to happen so that reads after a write do not
6660 * get stale data.
6661 */
6662 if (!ordered && (!writing ||
6663 !test_range_bit(&BTRFS_I(inode)->io_tree,
6664 lockstart, lockend, EXTENT_UPTODATE, 0,
6665 *cached_state)))
6666 break;
6667
6668 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6669 cached_state, GFP_NOFS);
6670
6671 if (ordered) {
6672 btrfs_start_ordered_extent(inode, ordered, 1);
6673 btrfs_put_ordered_extent(ordered);
6674 } else {
6675 /* Screw you mmap */
6676 ret = filemap_write_and_wait_range(inode->i_mapping,
6677 lockstart,
6678 lockend);
6679 if (ret)
6680 break;
6681
6682 /*
6683 * If we found a page that couldn't be invalidated just
6684 * fall back to buffered.
6685 */
6686 ret = invalidate_inode_pages2_range(inode->i_mapping,
6687 lockstart >> PAGE_CACHE_SHIFT,
6688 lockend >> PAGE_CACHE_SHIFT);
6689 if (ret)
6690 break;
6691 }
6692
6693 cond_resched();
6694 }
6695
6696 return ret;
6697}
6698
Josef Bacik69ffb542012-09-11 15:40:07 -04006699static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
6700 u64 len, u64 orig_start,
6701 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006702 u64 orig_block_len, u64 ram_bytes,
6703 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04006704{
6705 struct extent_map_tree *em_tree;
6706 struct extent_map *em;
6707 struct btrfs_root *root = BTRFS_I(inode)->root;
6708 int ret;
6709
6710 em_tree = &BTRFS_I(inode)->extent_tree;
6711 em = alloc_extent_map();
6712 if (!em)
6713 return ERR_PTR(-ENOMEM);
6714
6715 em->start = start;
6716 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04006717 em->mod_start = start;
6718 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04006719 em->len = len;
6720 em->block_len = block_len;
6721 em->block_start = block_start;
6722 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05006723 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04006724 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04006725 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04006726 set_bit(EXTENT_FLAG_PINNED, &em->flags);
6727 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05006728 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04006729
6730 do {
6731 btrfs_drop_extent_cache(inode, em->start,
6732 em->start + em->len - 1, 0);
6733 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006734 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04006735 write_unlock(&em_tree->lock);
6736 } while (ret == -EEXIST);
6737
6738 if (ret) {
6739 free_extent_map(em);
6740 return ERR_PTR(ret);
6741 }
6742
6743 return em;
6744}
6745
6746
Josef Bacik4b46fce2010-05-23 11:00:55 -04006747static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
6748 struct buffer_head *bh_result, int create)
6749{
6750 struct extent_map *em;
6751 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04006752 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006753 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04006754 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006755 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04006756 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00006757 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006758
Miao Xie172a5042013-02-21 02:48:22 -07006759 if (create)
Josef Bacikeb838e72012-07-31 16:28:48 -04006760 unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07006761 else
Josef Bacikc3298612012-08-03 16:49:19 -04006762 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04006763
Josef Bacikc3298612012-08-03 16:49:19 -04006764 lockstart = start;
6765 lockend = start + len - 1;
6766
Josef Bacikeb838e72012-07-31 16:28:48 -04006767 /*
6768 * If this errors out it's because we couldn't invalidate pagecache for
6769 * this range and we need to fallback to buffered.
6770 */
6771 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
6772 return -ENOTBLK;
6773
Josef Bacik4b46fce2010-05-23 11:00:55 -04006774 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04006775 if (IS_ERR(em)) {
6776 ret = PTR_ERR(em);
6777 goto unlock_err;
6778 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006779
6780 /*
6781 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
6782 * io. INLINE is special, and we could probably kludge it in here, but
6783 * it's still buffered so for safety lets just fall back to the generic
6784 * buffered path.
6785 *
6786 * For COMPRESSED we _have_ to read the entire extent in so we can
6787 * decompress it, so there will be buffering required no matter what we
6788 * do, so go ahead and fallback to buffered.
6789 *
6790 * We return -ENOTBLK because thats what makes DIO go ahead and go back
6791 * to buffered IO. Don't blame me, this is the price we pay for using
6792 * the generic code.
6793 */
6794 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
6795 em->block_start == EXTENT_MAP_INLINE) {
6796 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006797 ret = -ENOTBLK;
6798 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006799 }
6800
6801 /* Just a good old fashioned hole, return */
6802 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
6803 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
6804 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006805 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006806 }
6807
6808 /*
6809 * We don't allocate a new extent in the following cases
6810 *
6811 * 1) The inode is marked as NODATACOW. In this case we'll just use the
6812 * existing extent.
6813 * 2) The extent is marked as PREALLOC. We're good to go here and can
6814 * just use the extent.
6815 *
6816 */
Chris Mason46bfbb52010-05-26 11:04:10 -04006817 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04006818 len = min(len, em->len - (start - em->start));
6819 lockstart = start + len;
6820 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04006821 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006822
6823 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
6824 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
6825 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04006826 int type;
6827 int ret;
Josef Bacikeb384b52013-04-24 16:32:55 -04006828 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006829
6830 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6831 type = BTRFS_ORDERED_PREALLOC;
6832 else
6833 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04006834 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04006835 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04006836
Josef Bacik00361582013-08-14 14:02:47 -04006837 if (can_nocow_extent(inode, start, &len, &orig_start,
Josef Bacik7ee9e442013-06-21 16:37:03 -04006838 &orig_block_len, &ram_bytes) == 1) {
Josef Bacik69ffb542012-09-11 15:40:07 -04006839 if (type == BTRFS_ORDERED_PREALLOC) {
6840 free_extent_map(em);
6841 em = create_pinned_em(inode, start, len,
6842 orig_start,
Josef Bacikb4939682012-12-03 10:31:19 -05006843 block_start, len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006844 orig_block_len,
6845 ram_bytes, type);
Josef Bacik00361582013-08-14 14:02:47 -04006846 if (IS_ERR(em))
Josef Bacik69ffb542012-09-11 15:40:07 -04006847 goto unlock_err;
Josef Bacik69ffb542012-09-11 15:40:07 -04006848 }
6849
Chris Mason46bfbb52010-05-26 11:04:10 -04006850 ret = btrfs_add_ordered_extent_dio(inode, start,
6851 block_start, len, len, type);
Chris Mason46bfbb52010-05-26 11:04:10 -04006852 if (ret) {
6853 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006854 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04006855 }
6856 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006857 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006858 }
Josef Bacik00361582013-08-14 14:02:47 -04006859
Chris Mason46bfbb52010-05-26 11:04:10 -04006860 /*
6861 * this will cow the extent, reset the len in case we changed
6862 * it above
6863 */
6864 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04006865 free_extent_map(em);
6866 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04006867 if (IS_ERR(em)) {
6868 ret = PTR_ERR(em);
6869 goto unlock_err;
6870 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006871 len = min(len, em->len - (start - em->start));
6872unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04006873 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
6874 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04006875 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006876 bh_result->b_bdev = em->bdev;
6877 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04006878 if (create) {
6879 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6880 set_buffer_new(bh_result);
6881
6882 /*
6883 * Need to update the i_size under the extent lock so buffered
6884 * readers will get the updated i_size when we unlock.
6885 */
6886 if (start + len > i_size_read(inode))
6887 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00006888
Miao Xie172a5042013-02-21 02:48:22 -07006889 spin_lock(&BTRFS_I(inode)->lock);
6890 BTRFS_I(inode)->outstanding_extents++;
6891 spin_unlock(&BTRFS_I(inode)->lock);
6892
Miao Xie09348562013-02-07 10:12:07 +00006893 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6894 lockstart + len - 1, EXTENT_DELALLOC, NULL,
6895 &cached_state, GFP_NOFS);
6896 BUG_ON(ret);
Josef Bacikc3473e82012-06-19 10:59:00 -04006897 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006898
Josef Bacikeb838e72012-07-31 16:28:48 -04006899 /*
6900 * In the case of write we need to clear and unlock the entire range,
6901 * in the case of read we need to unlock only the end area that we
6902 * aren't using if there is any left over space.
6903 */
Liu Bo24c03fa2012-08-22 20:10:38 -06006904 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00006905 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6906 lockend, unlock_bits, 1, 0,
6907 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06006908 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04006909 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06006910 }
Josef Bacikeb838e72012-07-31 16:28:48 -04006911
Josef Bacik4b46fce2010-05-23 11:00:55 -04006912 free_extent_map(em);
6913
6914 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006915
6916unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04006917 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6918 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
6919 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006920}
6921
Josef Bacik4b46fce2010-05-23 11:00:55 -04006922static void btrfs_endio_direct_read(struct bio *bio, int err)
6923{
Miao Xiee65e1532010-11-22 03:04:43 +00006924 struct btrfs_dio_private *dip = bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006925 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
6926 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006927 struct inode *inode = dip->inode;
6928 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason9be33952013-05-17 18:30:14 -04006929 struct bio *dio_bio;
Miao Xiefacc8a222013-07-25 19:22:34 +08006930 u32 *csums = (u32 *)dip->csum;
6931 int index = 0;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006932 u64 start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006933
6934 start = dip->logical_offset;
6935 do {
6936 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
6937 struct page *page = bvec->bv_page;
6938 char *kaddr;
6939 u32 csum = ~(u32)0;
6940 unsigned long flags;
6941
6942 local_irq_save(flags);
Cong Wang7ac687d2011-11-25 23:14:28 +08006943 kaddr = kmap_atomic(page);
Liu Bob0496682013-03-14 14:57:45 +00006944 csum = btrfs_csum_data(kaddr + bvec->bv_offset,
Josef Bacik4b46fce2010-05-23 11:00:55 -04006945 csum, bvec->bv_len);
6946 btrfs_csum_final(csum, (char *)&csum);
Cong Wang7ac687d2011-11-25 23:14:28 +08006947 kunmap_atomic(kaddr);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006948 local_irq_restore(flags);
6949
6950 flush_dcache_page(bvec->bv_page);
Miao Xiefacc8a222013-07-25 19:22:34 +08006951 if (csum != csums[index]) {
6952 btrfs_err(root->fs_info, "csum failed ino %llu off %llu csum %u expected csum %u",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006953 btrfs_ino(inode), start, csum,
6954 csums[index]);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006955 err = -EIO;
6956 }
6957 }
6958
6959 start += bvec->bv_len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006960 bvec++;
Miao Xiefacc8a222013-07-25 19:22:34 +08006961 index++;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006962 } while (bvec <= bvec_end);
6963
6964 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006965 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04006966 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006967
Josef Bacik4b46fce2010-05-23 11:00:55 -04006968 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006969
6970 /* If we had a csum failure make sure to clear the uptodate flag */
6971 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04006972 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
6973 dio_end_io(dio_bio, err);
6974 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006975}
6976
6977static void btrfs_endio_direct_write(struct bio *bio, int err)
6978{
6979 struct btrfs_dio_private *dip = bio->bi_private;
6980 struct inode *inode = dip->inode;
6981 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006982 struct btrfs_ordered_extent *ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05006983 u64 ordered_offset = dip->logical_offset;
6984 u64 ordered_bytes = dip->bytes;
Chris Mason9be33952013-05-17 18:30:14 -04006985 struct bio *dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006986 int ret;
6987
6988 if (err)
6989 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05006990again:
6991 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
6992 &ordered_offset,
Josef Bacik5fd02042012-05-02 14:00:54 -04006993 ordered_bytes, !err);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006994 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05006995 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006996
Josef Bacik5fd02042012-05-02 14:00:54 -04006997 ordered->work.func = finish_ordered_fn;
6998 ordered->work.flags = 0;
6999 btrfs_queue_worker(&root->fs_info->endio_write_workers,
7000 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05007001out_test:
7002 /*
7003 * our bio might span multiple ordered extents. If we haven't
7004 * completed the accounting for the whole dio, go back and try again
7005 */
7006 if (ordered_offset < dip->logical_offset + dip->bytes) {
7007 ordered_bytes = dip->logical_offset + dip->bytes -
7008 ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04007009 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05007010 goto again;
7011 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007012out_done:
Chris Mason9be33952013-05-17 18:30:14 -04007013 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007014
Josef Bacik4b46fce2010-05-23 11:00:55 -04007015 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04007016
7017 /* If we had an error make sure to clear the uptodate flag */
7018 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04007019 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
7020 dio_end_io(dio_bio, err);
7021 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007022}
7023
Chris Masoneaf25d92010-05-25 09:48:28 -04007024static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
7025 struct bio *bio, int mirror_num,
7026 unsigned long bio_flags, u64 offset)
7027{
7028 int ret;
7029 struct btrfs_root *root = BTRFS_I(inode)->root;
7030 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007031 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04007032 return 0;
7033}
7034
Miao Xiee65e1532010-11-22 03:04:43 +00007035static void btrfs_end_dio_bio(struct bio *bio, int err)
7036{
7037 struct btrfs_dio_private *dip = bio->bi_private;
7038
7039 if (err) {
Frank Holtonefe120a2013-12-20 11:37:06 -05007040 btrfs_err(BTRFS_I(dip->inode)->root->fs_info,
7041 "direct IO failed ino %llu rw %lu sector %#Lx len %u err no %d",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02007042 btrfs_ino(dip->inode), bio->bi_rw,
Jan Beulich3dd14622010-12-07 14:54:09 +00007043 (unsigned long long)bio->bi_sector, bio->bi_size, err);
Miao Xiee65e1532010-11-22 03:04:43 +00007044 dip->errors = 1;
7045
7046 /*
7047 * before atomic variable goto zero, we must make sure
7048 * dip->errors is perceived to be set.
7049 */
7050 smp_mb__before_atomic_dec();
7051 }
7052
7053 /* if there are more bios still pending for this dio, just exit */
7054 if (!atomic_dec_and_test(&dip->pending_bios))
7055 goto out;
7056
Chris Mason9be33952013-05-17 18:30:14 -04007057 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00007058 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04007059 } else {
7060 set_bit(BIO_UPTODATE, &dip->dio_bio->bi_flags);
Miao Xiee65e1532010-11-22 03:04:43 +00007061 bio_endio(dip->orig_bio, 0);
7062 }
7063out:
7064 bio_put(bio);
7065}
7066
7067static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
7068 u64 first_sector, gfp_t gfp_flags)
7069{
7070 int nr_vecs = bio_get_nr_vecs(bdev);
7071 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
7072}
7073
7074static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
7075 int rw, u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007076 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00007077{
Miao Xiefacc8a222013-07-25 19:22:34 +08007078 struct btrfs_dio_private *dip = bio->bi_private;
Miao Xiee65e1532010-11-22 03:04:43 +00007079 int write = rw & REQ_WRITE;
7080 struct btrfs_root *root = BTRFS_I(inode)->root;
7081 int ret;
7082
Josef Bacikb812ce22012-11-16 13:56:32 -05007083 if (async_submit)
7084 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
7085
Miao Xiee65e1532010-11-22 03:04:43 +00007086 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04007087
7088 if (!write) {
7089 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
7090 if (ret)
7091 goto err;
7092 }
Miao Xiee65e1532010-11-22 03:04:43 +00007093
Josef Bacik1ae39932011-04-06 14:41:34 -04007094 if (skip_sum)
7095 goto map;
7096
7097 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00007098 ret = btrfs_wq_submit_bio(root->fs_info,
7099 inode, rw, bio, 0, 0,
7100 file_offset,
7101 __btrfs_submit_bio_start_direct_io,
7102 __btrfs_submit_bio_done);
7103 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04007104 } else if (write) {
7105 /*
7106 * If we aren't doing async submit, calculate the csum of the
7107 * bio now.
7108 */
7109 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
7110 if (ret)
7111 goto err;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007112 } else if (!skip_sum) {
Miao Xiefacc8a222013-07-25 19:22:34 +08007113 ret = btrfs_lookup_bio_sums_dio(root, inode, dip, bio,
7114 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007115 if (ret)
7116 goto err;
7117 }
Miao Xiee65e1532010-11-22 03:04:43 +00007118
Josef Bacik1ae39932011-04-06 14:41:34 -04007119map:
7120 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007121err:
7122 bio_put(bio);
7123 return ret;
7124}
7125
7126static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
7127 int skip_sum)
7128{
7129 struct inode *inode = dip->inode;
7130 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00007131 struct bio *bio;
7132 struct bio *orig_bio = dip->orig_bio;
7133 struct bio_vec *bvec = orig_bio->bi_io_vec;
7134 u64 start_sector = orig_bio->bi_sector;
7135 u64 file_offset = dip->logical_offset;
7136 u64 submit_len = 0;
7137 u64 map_length;
7138 int nr_pages = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007139 int ret = 0;
Josef Bacik1ae39932011-04-06 14:41:34 -04007140 int async_submit = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007141
Miao Xiee65e1532010-11-22 03:04:43 +00007142 map_length = orig_bio->bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007143 ret = btrfs_map_block(root->fs_info, rw, start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007144 &map_length, NULL, 0);
7145 if (ret) {
Josef Bacik64728bb2011-04-25 19:43:52 -04007146 bio_put(orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00007147 return -EIO;
7148 }
Miao Xiefacc8a222013-07-25 19:22:34 +08007149
Josef Bacik02f57c72011-04-06 14:25:44 -04007150 if (map_length >= orig_bio->bi_size) {
7151 bio = orig_bio;
7152 goto submit;
7153 }
7154
David Woodhouse53b381b2013-01-29 18:40:14 -05007155 /* async crcs make it difficult to collect full stripe writes. */
7156 if (btrfs_get_alloc_profile(root, 1) &
7157 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6))
7158 async_submit = 0;
7159 else
7160 async_submit = 1;
7161
Josef Bacik02f57c72011-04-06 14:25:44 -04007162 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
7163 if (!bio)
7164 return -ENOMEM;
7165 bio->bi_private = dip;
7166 bio->bi_end_io = btrfs_end_dio_bio;
7167 atomic_inc(&dip->pending_bios);
7168
Miao Xiee65e1532010-11-22 03:04:43 +00007169 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
7170 if (unlikely(map_length < submit_len + bvec->bv_len ||
7171 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
7172 bvec->bv_offset) < bvec->bv_len)) {
7173 /*
7174 * inc the count before we submit the bio so
7175 * we know the end IO handler won't happen before
7176 * we inc the count. Otherwise, the dip might get freed
7177 * before we're done setting it up
7178 */
7179 atomic_inc(&dip->pending_bios);
7180 ret = __btrfs_submit_dio_bio(bio, inode, rw,
7181 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007182 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007183 if (ret) {
7184 bio_put(bio);
7185 atomic_dec(&dip->pending_bios);
7186 goto out_err;
7187 }
7188
Miao Xiee65e1532010-11-22 03:04:43 +00007189 start_sector += submit_len >> 9;
7190 file_offset += submit_len;
7191
7192 submit_len = 0;
7193 nr_pages = 0;
7194
7195 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
7196 start_sector, GFP_NOFS);
7197 if (!bio)
7198 goto out_err;
7199 bio->bi_private = dip;
7200 bio->bi_end_io = btrfs_end_dio_bio;
7201
7202 map_length = orig_bio->bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007203 ret = btrfs_map_block(root->fs_info, rw,
Stefan Behrens3ec706c2012-11-05 15:46:42 +01007204 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007205 &map_length, NULL, 0);
7206 if (ret) {
7207 bio_put(bio);
7208 goto out_err;
7209 }
7210 } else {
7211 submit_len += bvec->bv_len;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307212 nr_pages++;
Miao Xiee65e1532010-11-22 03:04:43 +00007213 bvec++;
7214 }
7215 }
7216
Josef Bacik02f57c72011-04-06 14:25:44 -04007217submit:
Miao Xiee65e1532010-11-22 03:04:43 +00007218 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007219 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007220 if (!ret)
7221 return 0;
7222
7223 bio_put(bio);
7224out_err:
7225 dip->errors = 1;
7226 /*
7227 * before atomic variable goto zero, we must
7228 * make sure dip->errors is perceived to be set.
7229 */
7230 smp_mb__before_atomic_dec();
7231 if (atomic_dec_and_test(&dip->pending_bios))
7232 bio_io_error(dip->orig_bio);
7233
7234 /* bio_end_io() will handle error, so we needn't return it */
7235 return 0;
7236}
7237
Chris Mason9be33952013-05-17 18:30:14 -04007238static void btrfs_submit_direct(int rw, struct bio *dio_bio,
7239 struct inode *inode, loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007240{
7241 struct btrfs_root *root = BTRFS_I(inode)->root;
7242 struct btrfs_dio_private *dip;
Chris Mason9be33952013-05-17 18:30:14 -04007243 struct bio *io_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007244 int skip_sum;
Miao Xiefacc8a222013-07-25 19:22:34 +08007245 int sum_len;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02007246 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007247 int ret = 0;
Miao Xiefacc8a222013-07-25 19:22:34 +08007248 u16 csum_size;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007249
7250 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
7251
Chris Mason9be33952013-05-17 18:30:14 -04007252 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04007253 if (!io_bio) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007254 ret = -ENOMEM;
7255 goto free_ordered;
7256 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007257
Miao Xiefacc8a222013-07-25 19:22:34 +08007258 if (!skip_sum && !write) {
7259 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
7260 sum_len = dio_bio->bi_size >> inode->i_sb->s_blocksize_bits;
7261 sum_len *= csum_size;
7262 } else {
7263 sum_len = 0;
7264 }
7265
7266 dip = kmalloc(sizeof(*dip) + sum_len, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04007267 if (!dip) {
7268 ret = -ENOMEM;
7269 goto free_io_bio;
7270 }
7271
7272 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007273 dip->inode = inode;
7274 dip->logical_offset = file_offset;
Miao Xiee6da5d2e2013-06-19 18:19:17 +08007275 dip->bytes = dio_bio->bi_size;
Chris Mason9be33952013-05-17 18:30:14 -04007276 dip->disk_bytenr = (u64)dio_bio->bi_sector << 9;
7277 io_bio->bi_private = dip;
Miao Xiee65e1532010-11-22 03:04:43 +00007278 dip->errors = 0;
Chris Mason9be33952013-05-17 18:30:14 -04007279 dip->orig_bio = io_bio;
7280 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00007281 atomic_set(&dip->pending_bios, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007282
7283 if (write)
Chris Mason9be33952013-05-17 18:30:14 -04007284 io_bio->bi_end_io = btrfs_endio_direct_write;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007285 else
Chris Mason9be33952013-05-17 18:30:14 -04007286 io_bio->bi_end_io = btrfs_endio_direct_read;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007287
Miao Xiee65e1532010-11-22 03:04:43 +00007288 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
7289 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04007290 return;
Chris Mason9be33952013-05-17 18:30:14 -04007291
7292free_io_bio:
7293 bio_put(io_bio);
7294
Josef Bacik4b46fce2010-05-23 11:00:55 -04007295free_ordered:
7296 /*
7297 * If this is a write, we need to clean up the reserved space and kill
7298 * the ordered extent.
7299 */
7300 if (write) {
7301 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05007302 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007303 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
7304 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
7305 btrfs_free_reserved_extent(root, ordered->start,
7306 ordered->disk_len);
7307 btrfs_put_ordered_extent(ordered);
7308 btrfs_put_ordered_extent(ordered);
7309 }
Chris Mason9be33952013-05-17 18:30:14 -04007310 bio_endio(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007311}
7312
Chris Mason5a5f79b2010-05-26 21:33:37 -04007313static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
7314 const struct iovec *iov, loff_t offset,
7315 unsigned long nr_segs)
7316{
7317 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00007318 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007319 size_t size;
7320 unsigned long addr;
7321 unsigned blocksize_mask = root->sectorsize - 1;
7322 ssize_t retval = -EINVAL;
7323 loff_t end = offset;
7324
7325 if (offset & blocksize_mask)
7326 goto out;
7327
7328 /* Check the memory alignment. Blocks cannot straddle pages */
7329 for (seg = 0; seg < nr_segs; seg++) {
7330 addr = (unsigned long)iov[seg].iov_base;
7331 size = iov[seg].iov_len;
7332 end += size;
Josef Bacika1b75f72011-04-08 15:51:18 +00007333 if ((addr & blocksize_mask) || (size & blocksize_mask))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007334 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00007335
7336 /* If this is a write we don't need to check anymore */
7337 if (rw & WRITE)
7338 continue;
7339
7340 /*
7341 * Check to make sure we don't have duplicate iov_base's in this
7342 * iovec, if so return EINVAL, otherwise we'll get csum errors
7343 * when reading back.
7344 */
7345 for (i = seg + 1; i < nr_segs; i++) {
7346 if (iov[seg].iov_base == iov[i].iov_base)
7347 goto out;
7348 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04007349 }
7350 retval = 0;
7351out:
7352 return retval;
7353}
Josef Bacikeb838e72012-07-31 16:28:48 -04007354
Chris Mason16432982008-04-10 10:23:21 -04007355static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
7356 const struct iovec *iov, loff_t offset,
7357 unsigned long nr_segs)
7358{
Josef Bacik4b46fce2010-05-23 11:00:55 -04007359 struct file *file = iocb->ki_filp;
7360 struct inode *inode = file->f_mapping->host;
Miao Xie09348562013-02-07 10:12:07 +00007361 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00007362 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00007363 bool wakeup = true;
7364 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00007365 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007366
Chris Mason5a5f79b2010-05-26 21:33:37 -04007367 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
Josef Bacikeb838e72012-07-31 16:28:48 -04007368 offset, nr_segs))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007369 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007370
Miao Xie38851cc2013-02-08 07:04:11 +00007371 atomic_inc(&inode->i_dio_count);
7372 smp_mb__after_atomic_inc();
7373
Josef Bacik0e267c42013-07-02 10:38:02 -04007374 /*
7375 * The generic stuff only does filemap_write_and_wait_range, which isn't
7376 * enough if we've written compressed pages to this area, so we need to
7377 * call btrfs_wait_ordered_range to make absolutely sure that any
7378 * outstanding dirty pages are on disk.
7379 */
7380 count = iov_length(iov, nr_segs);
Josef Bacik0ef8b722013-10-25 16:13:35 -04007381 ret = btrfs_wait_ordered_range(inode, offset, count);
7382 if (ret)
7383 return ret;
Josef Bacik0e267c42013-07-02 10:38:02 -04007384
Miao Xie09348562013-02-07 10:12:07 +00007385 if (rw & WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00007386 /*
7387 * If the write DIO is beyond the EOF, we need update
7388 * the isize, but it is protected by i_mutex. So we can
7389 * not unlock the i_mutex at this case.
7390 */
7391 if (offset + count <= inode->i_size) {
7392 mutex_unlock(&inode->i_mutex);
7393 relock = true;
7394 }
Miao Xie09348562013-02-07 10:12:07 +00007395 ret = btrfs_delalloc_reserve_space(inode, count);
7396 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00007397 goto out;
7398 } else if (unlikely(test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
7399 &BTRFS_I(inode)->runtime_flags))) {
7400 inode_dio_done(inode);
7401 flags = DIO_LOCKING | DIO_SKIP_HOLES;
7402 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00007403 }
7404
7405 ret = __blockdev_direct_IO(rw, iocb, inode,
7406 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
7407 iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
Miao Xie2e60a512013-02-08 07:01:08 +00007408 btrfs_submit_direct, flags);
Miao Xie09348562013-02-07 10:12:07 +00007409 if (rw & WRITE) {
7410 if (ret < 0 && ret != -EIOCBQUEUED)
7411 btrfs_delalloc_release_space(inode, count);
Miao Xie172a5042013-02-21 02:48:22 -07007412 else if (ret >= 0 && (size_t)ret < count)
Miao Xie09348562013-02-07 10:12:07 +00007413 btrfs_delalloc_release_space(inode,
7414 count - (size_t)ret);
Miao Xie172a5042013-02-21 02:48:22 -07007415 else
7416 btrfs_delalloc_release_metadata(inode, 0);
Miao Xie09348562013-02-07 10:12:07 +00007417 }
Miao Xie38851cc2013-02-08 07:04:11 +00007418out:
Miao Xie2e60a512013-02-08 07:01:08 +00007419 if (wakeup)
7420 inode_dio_done(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00007421 if (relock)
7422 mutex_lock(&inode->i_mutex);
Miao Xie09348562013-02-07 10:12:07 +00007423
7424 return ret;
Chris Mason16432982008-04-10 10:23:21 -04007425}
7426
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007427#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
7428
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007429static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
7430 __u64 start, __u64 len)
7431{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007432 int ret;
7433
7434 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
7435 if (ret)
7436 return ret;
7437
Chris Masonec29ed52011-02-23 16:23:20 -05007438 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007439}
7440
Chris Mason9ebefb182007-06-15 13:50:00 -04007441int btrfs_readpage(struct file *file, struct page *page)
7442{
Chris Masond1310b22008-01-24 16:13:08 -05007443 struct extent_io_tree *tree;
7444 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02007445 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04007446}
Chris Mason1832a6d2007-12-21 16:27:21 -05007447
Chris Mason39279cc2007-06-12 06:35:45 -04007448static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
7449{
Chris Masond1310b22008-01-24 16:13:08 -05007450 struct extent_io_tree *tree;
Chris Masonb888db2b2007-08-27 16:49:44 -04007451
7452
7453 if (current->flags & PF_MEMALLOC) {
7454 redirty_page_for_writepage(wbc, page);
7455 unlock_page(page);
7456 return 0;
7457 }
Chris Masond1310b22008-01-24 16:13:08 -05007458 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04007459 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
7460}
Chris Mason39279cc2007-06-12 06:35:45 -04007461
Eric Sandeen48a3b632013-04-25 20:41:01 +00007462static int btrfs_writepages(struct address_space *mapping,
7463 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04007464{
Chris Masond1310b22008-01-24 16:13:08 -05007465 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05007466
Chris Masond1310b22008-01-24 16:13:08 -05007467 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f02e2007-11-01 19:45:34 -04007468 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
7469}
7470
Chris Mason3ab2fb52007-11-08 10:59:22 -05007471static int
7472btrfs_readpages(struct file *file, struct address_space *mapping,
7473 struct list_head *pages, unsigned nr_pages)
7474{
Chris Masond1310b22008-01-24 16:13:08 -05007475 struct extent_io_tree *tree;
7476 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05007477 return extent_readpages(tree, mapping, pages, nr_pages,
7478 btrfs_get_extent);
7479}
Chris Masone6dcd2d2008-07-17 12:53:50 -04007480static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04007481{
Chris Masond1310b22008-01-24 16:13:08 -05007482 struct extent_io_tree *tree;
7483 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04007484 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04007485
Chris Masond1310b22008-01-24 16:13:08 -05007486 tree = &BTRFS_I(page->mapping->host)->io_tree;
7487 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05007488 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04007489 if (ret == 1) {
7490 ClearPagePrivate(page);
7491 set_page_private(page, 0);
7492 page_cache_release(page);
7493 }
7494 return ret;
7495}
Chris Mason39279cc2007-06-12 06:35:45 -04007496
Chris Masone6dcd2d2008-07-17 12:53:50 -04007497static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
7498{
Chris Mason98509cf2008-09-11 15:51:43 -04007499 if (PageWriteback(page) || PageDirty(page))
7500 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05007501 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007502}
7503
Lukas Czernerd47992f2013-05-21 23:17:23 -04007504static void btrfs_invalidatepage(struct page *page, unsigned int offset,
7505 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04007506{
Josef Bacik5fd02042012-05-02 14:00:54 -04007507 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05007508 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007509 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007510 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007511 u64 page_start = page_offset(page);
7512 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00007513 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04007514
Chris Mason8b62b722009-09-02 16:53:46 -04007515 /*
7516 * we have the page locked, so new writeback can't start,
7517 * and the dirty bit won't be cleared while we are here.
7518 *
7519 * Wait for IO on this page so that we can safely clear
7520 * the PagePrivate2 bit and do ordered accounting
7521 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007522 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04007523
Josef Bacik5fd02042012-05-02 14:00:54 -04007524 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007525 if (offset) {
7526 btrfs_releasepage(page, GFP_NOFS);
7527 return;
7528 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00007529
7530 if (!inode_evicting)
7531 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
7532 ordered = btrfs_lookup_ordered_extent(inode, page_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007533 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04007534 /*
7535 * IO on this page will never be started, so we need
7536 * to account for any ordered extents now
7537 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00007538 if (!inode_evicting)
7539 clear_extent_bit(tree, page_start, page_end,
7540 EXTENT_DIRTY | EXTENT_DELALLOC |
7541 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
7542 EXTENT_DEFRAG, 1, 0, &cached_state,
7543 GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04007544 /*
7545 * whoever cleared the private bit is responsible
7546 * for the finish_ordered_io
7547 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04007548 if (TestClearPagePrivate2(page)) {
7549 struct btrfs_ordered_inode_tree *tree;
7550 u64 new_len;
7551
7552 tree = &BTRFS_I(inode)->ordered_tree;
7553
7554 spin_lock_irq(&tree->lock);
7555 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
7556 new_len = page_start - ordered->file_offset;
7557 if (new_len < ordered->truncated_len)
7558 ordered->truncated_len = new_len;
7559 spin_unlock_irq(&tree->lock);
7560
7561 if (btrfs_dec_test_ordered_pending(inode, &ordered,
7562 page_start,
7563 PAGE_CACHE_SIZE, 1))
7564 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04007565 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007566 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00007567 if (!inode_evicting) {
7568 cached_state = NULL;
7569 lock_extent_bits(tree, page_start, page_end, 0,
7570 &cached_state);
7571 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007572 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00007573
7574 if (!inode_evicting) {
7575 clear_extent_bit(tree, page_start, page_end,
7576 EXTENT_LOCKED | EXTENT_DIRTY |
7577 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
7578 EXTENT_DEFRAG, 1, 1,
7579 &cached_state, GFP_NOFS);
7580
7581 __btrfs_releasepage(page, GFP_NOFS);
7582 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007583
Chris Mason4a096752008-07-21 10:29:44 -04007584 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04007585 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04007586 ClearPagePrivate(page);
7587 set_page_private(page, 0);
7588 page_cache_release(page);
7589 }
Chris Mason39279cc2007-06-12 06:35:45 -04007590}
7591
Chris Mason9ebefb182007-06-15 13:50:00 -04007592/*
7593 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
7594 * called from a page fault handler when a page is first dirtied. Hence we must
7595 * be careful to check for EOF conditions here. We set the page up correctly
7596 * for a written page which means we get ENOSPC checking when writing into
7597 * holes and correct delalloc and unwritten extent mapping on filesystems that
7598 * support these features.
7599 *
7600 * We are not allowed to take the i_mutex here so we have to play games to
7601 * protect against truncate races as the page could now be beyond EOF. Because
7602 * vmtruncate() writes the inode size before removing pages, once we have the
7603 * page lock we can determine safely if the page is beyond EOF. If it is not
7604 * beyond EOF, then the page is guaranteed safe against truncation until we
7605 * unlock the page.
7606 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07007607int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04007608{
Nick Pigginc2ec1752009-03-31 15:23:21 -07007609 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05007610 struct inode *inode = file_inode(vma->vm_file);
Chris Mason1832a6d2007-12-21 16:27:21 -05007611 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007612 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7613 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007614 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007615 char *kaddr;
7616 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04007617 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05007618 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05007619 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007620 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007621 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04007622
Jan Karab2b5ef52012-06-12 16:20:45 +02007623 sb_start_pagefault(inode->i_sb);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04007624 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007625 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04007626 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05007627 reserved = 1;
7628 }
Nick Piggin56a76f82009-03-31 15:23:23 -07007629 if (ret) {
7630 if (ret == -ENOMEM)
7631 ret = VM_FAULT_OOM;
7632 else /* -ENOSPC, -EIO, etc */
7633 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05007634 if (reserved)
7635 goto out;
7636 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07007637 }
Chris Mason1832a6d2007-12-21 16:27:21 -05007638
Nick Piggin56a76f82009-03-31 15:23:23 -07007639 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007640again:
Chris Mason9ebefb182007-06-15 13:50:00 -04007641 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04007642 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007643 page_start = page_offset(page);
7644 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04007645
Chris Mason9ebefb182007-06-15 13:50:00 -04007646 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04007647 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04007648 /* page got truncated out from underneath us */
7649 goto out_unlock;
7650 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007651 wait_on_page_writeback(page);
7652
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007653 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007654 set_page_extent_mapped(page);
7655
Chris Masoneb84ae02008-07-17 13:53:27 -04007656 /*
7657 * we can't set the delalloc bits if there are pending ordered
7658 * extents. Drop our locks and wait for them to finish
7659 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007660 ordered = btrfs_lookup_ordered_extent(inode, page_start);
7661 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007662 unlock_extent_cached(io_tree, page_start, page_end,
7663 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007664 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04007665 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007666 btrfs_put_ordered_extent(ordered);
7667 goto again;
7668 }
7669
Josef Bacikfbf19082009-10-01 17:10:23 -04007670 /*
7671 * XXX - page_mkwrite gets called every time the page is dirtied, even
7672 * if it was already dirty, so for space accounting reasons we need to
7673 * clear any delalloc bits for the range we are fixing to save. There
7674 * is probably a better way to do this, but for now keep consistent with
7675 * prepare_pages in the normal write path.
7676 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00007677 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06007678 EXTENT_DIRTY | EXTENT_DELALLOC |
7679 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00007680 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04007681
Josef Bacik2ac55d42010-02-03 19:33:23 +00007682 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
7683 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007684 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007685 unlock_extent_cached(io_tree, page_start, page_end,
7686 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007687 ret = VM_FAULT_SIGBUS;
7688 goto out_unlock;
7689 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007690 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04007691
7692 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04007693 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04007694 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04007695 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04007696 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04007697
Chris Masone6dcd2d2008-07-17 12:53:50 -04007698 if (zero_start != PAGE_CACHE_SIZE) {
7699 kaddr = kmap(page);
7700 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
7701 flush_dcache_page(page);
7702 kunmap(page);
7703 }
Chris Mason247e7432008-07-17 12:53:51 -04007704 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007705 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04007706 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007707
Chris Mason257c62e2009-10-13 13:21:08 -04007708 BTRFS_I(inode)->last_trans = root->fs_info->generation;
7709 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06007710 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04007711
Josef Bacik2ac55d42010-02-03 19:33:23 +00007712 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04007713
7714out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02007715 if (!ret) {
7716 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04007717 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02007718 }
Chris Mason9ebefb182007-06-15 13:50:00 -04007719 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05007720out:
Josef Bacikec39e182012-01-12 19:10:12 -05007721 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007722out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02007723 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04007724 return ret;
7725}
7726
Josef Bacika41ad392011-01-31 15:30:16 -05007727static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04007728{
7729 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04007730 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04007731 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05007732 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007733 struct btrfs_trans_handle *trans;
Chris Masondbe674a2008-07-17 12:54:05 -04007734 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04007735 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04007736
Josef Bacik0ef8b722013-10-25 16:13:35 -04007737 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
7738 (u64)-1);
7739 if (ret)
7740 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04007741
Josef Bacikfcb80c22011-05-03 10:40:22 -04007742 /*
7743 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
7744 * 3 things going on here
7745 *
7746 * 1) We need to reserve space for our orphan item and the space to
7747 * delete our orphan item. Lord knows we don't want to have a dangling
7748 * orphan item because we didn't reserve space to remove it.
7749 *
7750 * 2) We need to reserve space to update our inode.
7751 *
7752 * 3) We need to have something to cache all the space that is going to
7753 * be free'd up by the truncate operation, but also have some slack
7754 * space reserved in case it uses space during the truncate (thank you
7755 * very much snapshotting).
7756 *
7757 * And we need these to all be seperate. The fact is we can use alot of
7758 * space doing the truncate, and we have no earthly idea how much space
7759 * we will use, so we need the truncate reservation to be seperate so it
7760 * doesn't end up using space reserved for updating the inode or
7761 * removing the orphan item. We also need to be able to stop the
7762 * transaction and start a new one, which means we need to be able to
7763 * update the inode several times, and we have no idea of knowing how
7764 * many times that will be, so we can't just reserve 1 item for the
7765 * entirety of the opration, so that has to be done seperately as well.
7766 * Then there is the orphan item, which does indeed need to be held on
7767 * to for the whole operation, and we need nobody to touch this reserved
7768 * space except the orphan code.
7769 *
7770 * So that leaves us with
7771 *
7772 * 1) root->orphan_block_rsv - for the orphan deletion.
7773 * 2) rsv - for the truncate reservation, which we will steal from the
7774 * transaction reservation.
7775 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
7776 * updating the inode.
7777 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06007778 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007779 if (!rsv)
7780 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04007781 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04007782 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05007783
Josef Bacik907cbce2011-08-08 13:46:15 -04007784 /*
Josef Bacik07127182011-08-19 10:29:59 -04007785 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04007786 * 1 for updating the inode.
7787 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05007788 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007789 if (IS_ERR(trans)) {
7790 err = PTR_ERR(trans);
7791 goto out;
7792 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05007793
Josef Bacik907cbce2011-08-08 13:46:15 -04007794 /* Migrate the slack space for the truncate to our reserve */
7795 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
7796 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007797 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05007798
Chris Mason5a3f23d2009-03-31 13:27:11 -04007799 /*
7800 * setattr is responsible for setting the ordered_data_close flag,
7801 * but that is only tested during the last file release. That
7802 * could happen well after the next commit, leaving a great big
7803 * window where new writes may get lost if someone chooses to write
7804 * to this file after truncating to zero
7805 *
7806 * The inode doesn't have any dirty data here, and so if we commit
7807 * this is a noop. If someone immediately starts writing to the inode
7808 * it is very likely we'll catch some of their writes in this
7809 * transaction, and the commit will find this file on the ordered
7810 * data list with good things to send down.
7811 *
7812 * This is a best effort solution, there is still a window where
7813 * using truncate to replace the contents of the file will
7814 * end up with a zero length file after a crash.
7815 */
Josef Bacik72ac3c02012-05-23 14:13:11 -04007816 if (inode->i_size == 0 && test_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
7817 &BTRFS_I(inode)->runtime_flags))
Chris Mason5a3f23d2009-03-31 13:27:11 -04007818 btrfs_add_ordered_operation(trans, root, inode);
7819
Josef Bacik5dc562c2012-08-17 13:14:17 -04007820 /*
7821 * So if we truncate and then write and fsync we normally would just
7822 * write the extents that changed, which is a problem if we need to
7823 * first truncate that entire inode. So set this flag so we write out
7824 * all of the extents in the inode to the sync log so we're completely
7825 * safe.
7826 */
7827 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007828 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04007829
Yan, Zheng80825102009-11-12 09:35:36 +00007830 while (1) {
7831 ret = btrfs_truncate_inode_items(trans, root, inode,
7832 inode->i_size,
7833 BTRFS_EXTENT_DATA_KEY);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007834 if (ret != -ENOSPC) {
Josef Bacik3893e332011-01-31 16:03:11 -05007835 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007836 break;
Josef Bacik3893e332011-01-31 16:03:11 -05007837 }
Chris Mason39279cc2007-06-12 06:35:45 -04007838
Josef Bacikfcb80c22011-05-03 10:40:22 -04007839 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007840 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007841 if (ret) {
7842 err = ret;
7843 break;
7844 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04007845
Yan, Zheng80825102009-11-12 09:35:36 +00007846 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007847 btrfs_btree_balance_dirty(root);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007848
7849 trans = btrfs_start_transaction(root, 2);
7850 if (IS_ERR(trans)) {
7851 ret = err = PTR_ERR(trans);
7852 trans = NULL;
7853 break;
7854 }
7855
7856 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
7857 rsv, min_size);
7858 BUG_ON(ret); /* shouldn't happen */
7859 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007860 }
7861
7862 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04007863 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007864 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007865 if (ret)
7866 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007867 }
7868
Chris Mason917c16b2011-11-08 14:49:59 -05007869 if (trans) {
7870 trans->block_rsv = &root->fs_info->trans_block_rsv;
7871 ret = btrfs_update_inode(trans, root, inode);
7872 if (ret && !err)
7873 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04007874
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007875 ret = btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007876 btrfs_btree_balance_dirty(root);
Chris Mason917c16b2011-11-08 14:49:59 -05007877 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04007878
7879out:
7880 btrfs_free_block_rsv(root, rsv);
7881
Josef Bacik3893e332011-01-31 16:03:11 -05007882 if (ret && !err)
7883 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05007884
Josef Bacik3893e332011-01-31 16:03:11 -05007885 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007886}
7887
Sven Wegener3b963622008-06-09 21:57:42 -04007888/*
Chris Masond352ac62008-09-29 15:18:18 -04007889 * create a new subvolume directory/inode (helper for the ioctl).
7890 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05007891int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04007892 struct btrfs_root *new_root, u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04007893{
Chris Mason39279cc2007-06-12 06:35:45 -04007894 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04007895 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007896 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007897
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01007898 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
7899 new_dirid, new_dirid,
7900 S_IFDIR | (~current_umask() & S_IRWXUGO),
7901 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04007902 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04007903 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007904 inode->i_op = &btrfs_dir_inode_operations;
7905 inode->i_fop = &btrfs_dir_file_operations;
7906
Miklos Szeredibfe86842011-10-28 14:13:29 +02007907 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04007908 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04007909
Yan, Zheng76dda932009-09-21 16:00:26 -04007910 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04007911
Yan, Zheng76dda932009-09-21 16:00:26 -04007912 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07007913 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007914}
7915
Chris Mason39279cc2007-06-12 06:35:45 -04007916struct inode *btrfs_alloc_inode(struct super_block *sb)
7917{
7918 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007919 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007920
7921 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
7922 if (!ei)
7923 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007924
7925 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007926 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04007927 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04007928 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04007929 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007930 ei->delalloc_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007931 ei->disk_i_size = 0;
7932 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04007933 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007934 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08007935 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007936 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06007937 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007938
Josef Bacik9e0baf62011-07-15 15:16:44 +00007939 spin_lock_init(&ei->lock);
7940 ei->outstanding_extents = 0;
7941 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007942
Josef Bacik72ac3c02012-05-23 14:13:11 -04007943 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08007944 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007945
Miao Xie16cdcec2011-04-22 18:12:22 +08007946 ei->delayed_node = NULL;
7947
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007948 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02007949 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02007950 extent_io_tree_init(&ei->io_tree, &inode->i_data);
7951 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04007952 ei->io_tree.track_uptodate = 1;
7953 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05007954 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007955 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05007956 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007957 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007958 INIT_LIST_HEAD(&ei->delalloc_inodes);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007959 INIT_LIST_HEAD(&ei->ordered_operations);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007960 RB_CLEAR_NODE(&ei->rb_node);
7961
7962 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007963}
7964
Josef Bacikaaedb552013-10-11 14:44:09 -04007965#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
7966void btrfs_test_destroy_inode(struct inode *inode)
7967{
7968 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
7969 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
7970}
7971#endif
7972
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007973static void btrfs_i_callback(struct rcu_head *head)
7974{
7975 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007976 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
7977}
7978
Chris Mason39279cc2007-06-12 06:35:45 -04007979void btrfs_destroy_inode(struct inode *inode)
7980{
Chris Masone6dcd2d2008-07-17 12:53:50 -04007981 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007982 struct btrfs_root *root = BTRFS_I(inode)->root;
7983
Al Virob3d9b7a2012-06-09 13:51:19 -04007984 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04007985 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00007986 WARN_ON(BTRFS_I(inode)->outstanding_extents);
7987 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04007988 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
7989 WARN_ON(BTRFS_I(inode)->csum_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04007990
Chris Mason5a3f23d2009-03-31 13:27:11 -04007991 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05007992 * This can happen where we create an inode, but somebody else also
7993 * created the same inode and we need to destroy the one we already
7994 * created.
7995 */
7996 if (!root)
7997 goto free;
7998
7999 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04008000 * Make sure we're properly removed from the ordered operation
8001 * lists.
8002 */
8003 smp_mb();
8004 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
Miao Xie199c2a92013-05-15 07:48:23 +00008005 spin_lock(&root->fs_info->ordered_root_lock);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008006 list_del_init(&BTRFS_I(inode)->ordered_operations);
Miao Xie199c2a92013-05-15 07:48:23 +00008007 spin_unlock(&root->fs_info->ordered_root_lock);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008008 }
8009
Josef Bacik8a35d952012-05-23 14:26:42 -04008010 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
8011 &BTRFS_I(inode)->runtime_flags)) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008012 btrfs_info(root->fs_info, "inode %llu still on the orphan list",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02008013 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04008014 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04008015 }
Josef Bacik7b128762008-07-24 12:17:14 -04008016
Chris Masond3977122009-01-05 21:25:51 -05008017 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008018 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
8019 if (!ordered)
8020 break;
8021 else {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008022 btrfs_err(root->fs_info, "found ordered extent %llu %llu on inode cleanup",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02008023 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008024 btrfs_remove_ordered_extent(inode, ordered);
8025 btrfs_put_ordered_extent(ordered);
8026 btrfs_put_ordered_extent(ordered);
8027 }
8028 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008029 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008030 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05008031free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008032 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04008033}
8034
Al Viro45321ac2010-06-07 13:43:19 -04008035int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04008036{
8037 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04008038
Naohiro Aota6379ef92013-06-06 09:56:34 +00008039 if (root == NULL)
8040 return 1;
8041
Liu Bofa6ac872013-02-20 14:10:23 +00008042 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02008043 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04008044 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04008045 else
Al Viro45321ac2010-06-07 13:43:19 -04008046 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04008047}
8048
Sven Wegener0ee0fda2008-07-30 16:54:26 -04008049static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04008050{
8051 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
8052
8053 inode_init_once(&ei->vfs_inode);
8054}
8055
8056void btrfs_destroy_cachep(void)
8057{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10008058 /*
8059 * Make sure all delayed rcu free inodes are flushed before we
8060 * destroy cache.
8061 */
8062 rcu_barrier();
Chris Mason39279cc2007-06-12 06:35:45 -04008063 if (btrfs_inode_cachep)
8064 kmem_cache_destroy(btrfs_inode_cachep);
8065 if (btrfs_trans_handle_cachep)
8066 kmem_cache_destroy(btrfs_trans_handle_cachep);
8067 if (btrfs_transaction_cachep)
8068 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008069 if (btrfs_path_cachep)
8070 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05008071 if (btrfs_free_space_cachep)
8072 kmem_cache_destroy(btrfs_free_space_cachep);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008073 if (btrfs_delalloc_work_cachep)
8074 kmem_cache_destroy(btrfs_delalloc_work_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008075}
8076
8077int btrfs_init_cachep(void)
8078{
David Sterba837e1972012-09-07 03:00:48 -06008079 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008080 sizeof(struct btrfs_inode), 0,
8081 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04008082 if (!btrfs_inode_cachep)
8083 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008084
David Sterba837e1972012-09-07 03:00:48 -06008085 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008086 sizeof(struct btrfs_trans_handle), 0,
8087 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008088 if (!btrfs_trans_handle_cachep)
8089 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008090
David Sterba837e1972012-09-07 03:00:48 -06008091 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008092 sizeof(struct btrfs_transaction), 0,
8093 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008094 if (!btrfs_transaction_cachep)
8095 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008096
David Sterba837e1972012-09-07 03:00:48 -06008097 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008098 sizeof(struct btrfs_path), 0,
8099 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008100 if (!btrfs_path_cachep)
8101 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008102
David Sterba837e1972012-09-07 03:00:48 -06008103 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05008104 sizeof(struct btrfs_free_space), 0,
8105 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
8106 if (!btrfs_free_space_cachep)
8107 goto fail;
8108
Miao Xie8ccf6f192012-10-25 09:28:04 +00008109 btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
8110 sizeof(struct btrfs_delalloc_work), 0,
8111 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
8112 NULL);
8113 if (!btrfs_delalloc_work_cachep)
8114 goto fail;
8115
Chris Mason39279cc2007-06-12 06:35:45 -04008116 return 0;
8117fail:
8118 btrfs_destroy_cachep();
8119 return -ENOMEM;
8120}
8121
8122static int btrfs_getattr(struct vfsmount *mnt,
8123 struct dentry *dentry, struct kstat *stat)
8124{
Miao Xiedf0af1a2013-01-29 10:11:59 +00008125 u64 delalloc_bytes;
Chris Mason39279cc2007-06-12 06:35:45 -04008126 struct inode *inode = dentry->d_inode;
David Sterbafadc0d82011-11-20 07:33:38 -05008127 u32 blocksize = inode->i_sb->s_blocksize;
8128
Chris Mason39279cc2007-06-12 06:35:45 -04008129 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04008130 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05008131 stat->blksize = PAGE_CACHE_SIZE;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008132
8133 spin_lock(&BTRFS_I(inode)->lock);
8134 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
8135 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05008136 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00008137 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04008138 return 0;
8139}
8140
Chris Masond3977122009-01-05 21:25:51 -05008141static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
8142 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04008143{
8144 struct btrfs_trans_handle *trans;
8145 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008146 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04008147 struct inode *new_inode = new_dentry->d_inode;
8148 struct inode *old_inode = old_dentry->d_inode;
8149 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008150 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008151 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04008152 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08008153 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008154
Li Zefan33345d012011-04-20 10:31:50 +08008155 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04008156 return -EPERM;
8157
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008158 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08008159 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05008160 return -EXDEV;
8161
Li Zefan33345d012011-04-20 10:31:50 +08008162 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
8163 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008164 return -ENOTEMPTY;
8165
Chris Mason39279cc2007-06-12 06:35:45 -04008166 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008167 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04008168 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05008169
8170
8171 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04008172 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05008173 new_dentry->d_name.name,
8174 new_dentry->d_name.len);
8175
8176 if (ret) {
8177 if (ret == -EEXIST) {
8178 /* we shouldn't get
8179 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05308180 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05008181 return ret;
8182 }
8183 } else {
8184 /* maybe -EOVERFLOW */
8185 return ret;
8186 }
8187 }
8188 ret = 0;
8189
Chris Mason5a3f23d2009-03-31 13:27:11 -04008190 /*
8191 * we're using rename to replace one file with another.
8192 * and the replacement file is large. Start IO on it now so
8193 * we don't add too much work to the end of the transaction
8194 */
Bartlomiej Zolnierkiewicz4baf8c92009-08-07 13:47:08 -04008195 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
Chris Mason5a3f23d2009-03-31 13:27:11 -04008196 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
8197 filemap_flush(old_inode->i_mapping);
8198
Yan, Zheng76dda932009-09-21 16:00:26 -04008199 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08008200 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008201 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008202 /*
8203 * We want to reserve the absolute worst case amount of items. So if
8204 * both inodes are subvols and we need to unlink them then that would
8205 * require 4 item modifications, but if they are both normal inodes it
8206 * would require 5 item modifications, so we'll assume their normal
8207 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
8208 * should cover the worst case number of items we'll modify.
8209 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04008210 trans = btrfs_start_transaction(root, 11);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008211 if (IS_ERR(trans)) {
8212 ret = PTR_ERR(trans);
8213 goto out_notrans;
8214 }
Chris Mason5f39d392007-10-15 16:14:19 -04008215
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008216 if (dest != root)
8217 btrfs_record_root_in_trans(trans, dest);
8218
Yan, Zhenga5719522009-09-24 09:17:31 -04008219 ret = btrfs_set_inode_index(new_dir, &index);
8220 if (ret)
8221 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008222
Miao Xie67de1172013-12-26 13:07:06 +08008223 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08008224 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008225 /* force full log commit if subvolume involved. */
8226 root->fs_info->last_trans_log_full_commit = trans->transid;
8227 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04008228 ret = btrfs_insert_inode_ref(trans, dest,
8229 new_dentry->d_name.name,
8230 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08008231 old_ino,
8232 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04008233 if (ret)
8234 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008235 /*
8236 * this is an ugly little race, but the rename is required
8237 * to make sure that if we crash, the inode is either at the
8238 * old name or the new one. pinning the log transaction lets
8239 * us make sure we don't allow a log commit to come in after
8240 * we unlink the name but before we add the new name back in.
8241 */
8242 btrfs_pin_log_trans(root);
8243 }
Chris Mason12fcfd22009-03-24 10:24:20 -04008244 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04008245 * make sure the inode gets flushed if it is replacing
8246 * something.
8247 */
Li Zefan33345d012011-04-20 10:31:50 +08008248 if (new_inode && new_inode->i_size && S_ISREG(old_inode->i_mode))
Chris Mason5a3f23d2009-03-31 13:27:11 -04008249 btrfs_add_ordered_operation(trans, root, old_inode);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008250
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008251 inode_inc_iversion(old_dir);
8252 inode_inc_iversion(new_dir);
8253 inode_inc_iversion(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008254 old_dir->i_ctime = old_dir->i_mtime = ctime;
8255 new_dir->i_ctime = new_dir->i_mtime = ctime;
8256 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04008257
Chris Mason12fcfd22009-03-24 10:24:20 -04008258 if (old_dentry->d_parent != new_dentry->d_parent)
8259 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
8260
Li Zefan33345d012011-04-20 10:31:50 +08008261 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008262 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
8263 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
8264 old_dentry->d_name.name,
8265 old_dentry->d_name.len);
8266 } else {
Al Viro92986792011-03-04 17:14:37 +00008267 ret = __btrfs_unlink_inode(trans, root, old_dir,
8268 old_dentry->d_inode,
8269 old_dentry->d_name.name,
8270 old_dentry->d_name.len);
8271 if (!ret)
8272 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008273 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008274 if (ret) {
8275 btrfs_abort_transaction(trans, root, ret);
8276 goto out_fail;
8277 }
Chris Mason39279cc2007-06-12 06:35:45 -04008278
8279 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008280 inode_inc_iversion(new_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008281 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08008282 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008283 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
8284 root_objectid = BTRFS_I(new_inode)->location.objectid;
8285 ret = btrfs_unlink_subvol(trans, dest, new_dir,
8286 root_objectid,
8287 new_dentry->d_name.name,
8288 new_dentry->d_name.len);
8289 BUG_ON(new_inode->i_nlink == 0);
8290 } else {
8291 ret = btrfs_unlink_inode(trans, dest, new_dir,
8292 new_dentry->d_inode,
8293 new_dentry->d_name.name,
8294 new_dentry->d_name.len);
8295 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04008296 if (!ret && new_inode->i_nlink == 0)
Chris Masone02119d2008-09-05 16:13:11 -04008297 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008298 if (ret) {
8299 btrfs_abort_transaction(trans, root, ret);
8300 goto out_fail;
8301 }
Chris Mason39279cc2007-06-12 06:35:45 -04008302 }
Josef Bacikaec74772008-07-24 12:12:38 -04008303
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008304 ret = btrfs_add_link(trans, new_dir, old_inode,
8305 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04008306 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008307 if (ret) {
8308 btrfs_abort_transaction(trans, root, ret);
8309 goto out_fail;
8310 }
Chris Mason39279cc2007-06-12 06:35:45 -04008311
Miao Xie67de1172013-12-26 13:07:06 +08008312 if (old_inode->i_nlink == 1)
8313 BTRFS_I(old_inode)->dir_index = index;
8314
Li Zefan33345d012011-04-20 10:31:50 +08008315 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04008316 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00008317 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008318 btrfs_end_log_trans(root);
8319 }
Chris Mason39279cc2007-06-12 06:35:45 -04008320out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008321 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008322out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08008323 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008324 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008325
Chris Mason39279cc2007-06-12 06:35:45 -04008326 return ret;
8327}
8328
Miao Xie8ccf6f192012-10-25 09:28:04 +00008329static void btrfs_run_delalloc_work(struct btrfs_work *work)
8330{
8331 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04008332 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008333
8334 delalloc_work = container_of(work, struct btrfs_delalloc_work,
8335 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04008336 inode = delalloc_work->inode;
8337 if (delalloc_work->wait) {
8338 btrfs_wait_ordered_range(inode, 0, (u64)-1);
8339 } else {
8340 filemap_flush(inode->i_mapping);
8341 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8342 &BTRFS_I(inode)->runtime_flags))
8343 filemap_flush(inode->i_mapping);
8344 }
Miao Xie8ccf6f192012-10-25 09:28:04 +00008345
8346 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -04008347 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008348 else
Josef Bacik9f23e282013-10-28 15:03:41 -04008349 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008350 complete(&delalloc_work->completion);
8351}
8352
8353struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
8354 int wait, int delay_iput)
8355{
8356 struct btrfs_delalloc_work *work;
8357
8358 work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
8359 if (!work)
8360 return NULL;
8361
8362 init_completion(&work->completion);
8363 INIT_LIST_HEAD(&work->list);
8364 work->inode = inode;
8365 work->wait = wait;
8366 work->delay_iput = delay_iput;
8367 work->work.func = btrfs_run_delalloc_work;
8368
8369 return work;
8370}
8371
8372void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
8373{
8374 wait_for_completion(&work->completion);
8375 kmem_cache_free(btrfs_delalloc_work_cachep, work);
8376}
8377
Chris Masond352ac62008-09-29 15:18:18 -04008378/*
8379 * some fairly slow code that needs optimization. This walks the list
8380 * of all the inodes with pending delalloc and forces them to disk.
8381 */
Miao Xieeb73c1b2013-05-15 07:48:22 +00008382static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
Chris Masonea8c2812008-08-04 23:17:27 -04008383{
Chris Masonea8c2812008-08-04 23:17:27 -04008384 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008385 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008386 struct btrfs_delalloc_work *work, *next;
8387 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00008388 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008389 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04008390
Miao Xie8ccf6f192012-10-25 09:28:04 +00008391 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008392 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00008393
Miao Xieeb73c1b2013-05-15 07:48:22 +00008394 spin_lock(&root->delalloc_lock);
8395 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008396 while (!list_empty(&splice)) {
8397 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04008398 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008399
Miao Xieeb73c1b2013-05-15 07:48:22 +00008400 list_move_tail(&binode->delalloc_inodes,
8401 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008402 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00008403 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00008404 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008405 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008406 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008407 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008408
8409 work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
8410 if (unlikely(!work)) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -04008411 if (delay_iput)
8412 btrfs_add_delayed_iput(inode);
8413 else
8414 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008415 ret = -ENOMEM;
8416 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008417 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008418 list_add_tail(&work->list, &works);
8419 btrfs_queue_worker(&root->fs_info->flush_workers,
8420 &work->work);
8421
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008422 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00008423 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04008424 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008425 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04008426
Miao Xie1eafa6c2013-01-22 10:49:00 +00008427 list_for_each_entry_safe(work, next, &works, list) {
8428 list_del_init(&work->list);
8429 btrfs_wait_and_free_delalloc_work(work);
8430 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008431 return 0;
8432out:
8433 list_for_each_entry_safe(work, next, &works, list) {
8434 list_del_init(&work->list);
8435 btrfs_wait_and_free_delalloc_work(work);
8436 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008437
Miao Xieeb73c1b2013-05-15 07:48:22 +00008438 if (!list_empty_careful(&splice)) {
8439 spin_lock(&root->delalloc_lock);
8440 list_splice_tail(&splice, &root->delalloc_inodes);
8441 spin_unlock(&root->delalloc_lock);
8442 }
8443 return ret;
8444}
8445
8446int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
8447{
8448 int ret;
8449
8450 if (root->fs_info->sb->s_flags & MS_RDONLY)
8451 return -EROFS;
8452
8453 ret = __start_delalloc_inodes(root, delay_iput);
8454 /*
8455 * the filemap_flush will queue IO into the worker threads, but
Chris Mason8c8bee12008-09-29 11:19:10 -04008456 * we have to make sure the IO is actually started and that
8457 * ordered extents get created before we return
8458 */
8459 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05008460 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05008461 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04008462 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05008463 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
8464 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04008465 }
8466 atomic_dec(&root->fs_info->async_submit_draining);
Miao Xieeb73c1b2013-05-15 07:48:22 +00008467 return ret;
8468}
8469
Miao Xie91aef862013-11-04 23:13:26 +08008470int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput)
Miao Xieeb73c1b2013-05-15 07:48:22 +00008471{
8472 struct btrfs_root *root;
8473 struct list_head splice;
8474 int ret;
8475
8476 if (fs_info->sb->s_flags & MS_RDONLY)
8477 return -EROFS;
8478
8479 INIT_LIST_HEAD(&splice);
8480
8481 spin_lock(&fs_info->delalloc_root_lock);
8482 list_splice_init(&fs_info->delalloc_roots, &splice);
8483 while (!list_empty(&splice)) {
8484 root = list_first_entry(&splice, struct btrfs_root,
8485 delalloc_root);
8486 root = btrfs_grab_fs_root(root);
8487 BUG_ON(!root);
8488 list_move_tail(&root->delalloc_root,
8489 &fs_info->delalloc_roots);
8490 spin_unlock(&fs_info->delalloc_root_lock);
8491
8492 ret = __start_delalloc_inodes(root, delay_iput);
8493 btrfs_put_fs_root(root);
8494 if (ret)
8495 goto out;
8496
8497 spin_lock(&fs_info->delalloc_root_lock);
8498 }
8499 spin_unlock(&fs_info->delalloc_root_lock);
8500
8501 atomic_inc(&fs_info->async_submit_draining);
8502 while (atomic_read(&fs_info->nr_async_submits) ||
8503 atomic_read(&fs_info->async_delalloc_pages)) {
8504 wait_event(fs_info->async_submit_wait,
8505 (atomic_read(&fs_info->nr_async_submits) == 0 &&
8506 atomic_read(&fs_info->async_delalloc_pages) == 0));
8507 }
8508 atomic_dec(&fs_info->async_submit_draining);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008509 return 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008510out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00008511 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00008512 spin_lock(&fs_info->delalloc_root_lock);
8513 list_splice_tail(&splice, &fs_info->delalloc_roots);
8514 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008515 }
Miao Xie8ccf6f192012-10-25 09:28:04 +00008516 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04008517}
8518
Chris Mason39279cc2007-06-12 06:35:45 -04008519static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
8520 const char *symname)
8521{
8522 struct btrfs_trans_handle *trans;
8523 struct btrfs_root *root = BTRFS_I(dir)->root;
8524 struct btrfs_path *path;
8525 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05008526 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04008527 int err;
8528 int drop_inode = 0;
8529 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05308530 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008531 int name_len;
8532 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04008533 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04008534 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04008535 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04008536
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01008537 name_len = strlen(symname);
Chris Mason39279cc2007-06-12 06:35:45 -04008538 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
8539 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05008540
Josef Bacik9ed74f22009-09-11 16:12:44 -04008541 /*
8542 * 2 items for inode item and ref
8543 * 2 items for dir items
8544 * 1 item for xattr if selinux is on
8545 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04008546 trans = btrfs_start_transaction(root, 5);
8547 if (IS_ERR(trans))
8548 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05008549
Li Zefan581bb052011-04-20 10:06:11 +08008550 err = btrfs_find_free_ino(root, &objectid);
8551 if (err)
8552 goto out_unlock;
8553
Josef Bacikaec74772008-07-24 12:12:38 -04008554 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08008555 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04008556 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008557 if (IS_ERR(inode)) {
8558 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008559 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008560 }
Chris Mason39279cc2007-06-12 06:35:45 -04008561
Eric Paris2a7dba32011-02-01 11:05:39 -05008562 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04008563 if (err) {
8564 drop_inode = 1;
8565 goto out_unlock;
8566 }
8567
Casey Schauflerad19db72011-12-15 10:09:07 -05008568 /*
8569 * If the active LSM wants to access the inode during
8570 * d_instantiate it needs these. Smack checks to see
8571 * if the filesystem supports xattrs by looking at the
8572 * ops vector.
8573 */
8574 inode->i_fop = &btrfs_file_operations;
8575 inode->i_op = &btrfs_file_inode_operations;
8576
Josef Bacika1b075d2010-11-19 20:36:11 +00008577 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04008578 if (err)
8579 drop_inode = 1;
8580 else {
8581 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04008582 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05008583 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04008584 }
Chris Mason39279cc2007-06-12 06:35:45 -04008585 if (drop_inode)
8586 goto out_unlock;
8587
8588 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07008589 if (!path) {
8590 err = -ENOMEM;
8591 drop_inode = 1;
8592 goto out_unlock;
8593 }
Li Zefan33345d012011-04-20 10:31:50 +08008594 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008595 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008596 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
8597 datasize = btrfs_file_extent_calc_inline_size(name_len);
8598 err = btrfs_insert_empty_item(trans, root, path, &key,
8599 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04008600 if (err) {
8601 drop_inode = 1;
Julia Lawallb0839162011-05-14 07:10:51 +00008602 btrfs_free_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04008603 goto out_unlock;
8604 }
Chris Mason5f39d392007-10-15 16:14:19 -04008605 leaf = path->nodes[0];
8606 ei = btrfs_item_ptr(leaf, path->slots[0],
8607 struct btrfs_file_extent_item);
8608 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
8609 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04008610 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04008611 btrfs_set_file_extent_encryption(leaf, ei, 0);
8612 btrfs_set_file_extent_compression(leaf, ei, 0);
8613 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
8614 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
8615
Chris Mason39279cc2007-06-12 06:35:45 -04008616 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04008617 write_extent_buffer(leaf, symname, ptr, name_len);
8618 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04008619 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04008620
Chris Mason39279cc2007-06-12 06:35:45 -04008621 inode->i_op = &btrfs_symlink_inode_operations;
8622 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04008623 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04008624 inode_set_bytes(inode, name_len);
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01008625 btrfs_i_size_write(inode, name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -04008626 err = btrfs_update_inode(trans, root, inode);
8627 if (err)
8628 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04008629
8630out_unlock:
Al Viro08c422c2011-12-23 07:58:13 -05008631 if (!err)
8632 d_instantiate(dentry, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008633 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04008634 if (drop_inode) {
8635 inode_dec_link_count(inode);
8636 iput(inode);
8637 }
Liu Bob53d3f52012-11-14 14:34:34 +00008638 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04008639 return err;
8640}
Chris Mason16432982008-04-10 10:23:21 -04008641
Josef Bacik0af3d002010-06-21 14:48:16 -04008642static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
8643 u64 start, u64 num_bytes, u64 min_size,
8644 loff_t actual_len, u64 *alloc_hint,
8645 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04008646{
Josef Bacik5dc562c2012-08-17 13:14:17 -04008647 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
8648 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04008649 struct btrfs_root *root = BTRFS_I(inode)->root;
8650 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04008651 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00008652 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05008653 u64 cur_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04008654 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04008655 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04008656
Josef Bacik0af3d002010-06-21 14:48:16 -04008657 if (trans)
8658 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04008659 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008660 if (own_trans) {
8661 trans = btrfs_start_transaction(root, 3);
8662 if (IS_ERR(trans)) {
8663 ret = PTR_ERR(trans);
8664 break;
8665 }
Yan Zhengd899e052008-10-30 14:25:28 -04008666 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00008667
Chris Mason154ea282013-03-05 11:11:26 -05008668 cur_bytes = min(num_bytes, 256ULL * 1024 * 1024);
8669 cur_bytes = max(cur_bytes, min_size);
Josef Bacik00361582013-08-14 14:02:47 -04008670 ret = btrfs_reserve_extent(root, cur_bytes, min_size, 0,
8671 *alloc_hint, &ins, 1);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008672 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008673 if (own_trans)
8674 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008675 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00008676 }
8677
Yan Zhengd899e052008-10-30 14:25:28 -04008678 ret = insert_reserved_file_extent(trans, inode,
8679 cur_offset, ins.objectid,
8680 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00008681 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04008682 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008683 if (ret) {
Josef Bacik857cc2f2013-10-07 15:21:08 -04008684 btrfs_free_reserved_extent(root, ins.objectid,
8685 ins.offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008686 btrfs_abort_transaction(trans, root, ret);
8687 if (own_trans)
8688 btrfs_end_transaction(trans, root);
8689 break;
8690 }
Chris Masona1ed8352009-09-11 12:27:37 -04008691 btrfs_drop_extent_cache(inode, cur_offset,
8692 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008693
Josef Bacik5dc562c2012-08-17 13:14:17 -04008694 em = alloc_extent_map();
8695 if (!em) {
8696 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
8697 &BTRFS_I(inode)->runtime_flags);
8698 goto next;
8699 }
8700
8701 em->start = cur_offset;
8702 em->orig_start = cur_offset;
8703 em->len = ins.offset;
8704 em->block_start = ins.objectid;
8705 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05008706 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04008707 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008708 em->bdev = root->fs_info->fs_devices->latest_bdev;
8709 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
8710 em->generation = trans->transid;
8711
8712 while (1) {
8713 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04008714 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04008715 write_unlock(&em_tree->lock);
8716 if (ret != -EEXIST)
8717 break;
8718 btrfs_drop_extent_cache(inode, cur_offset,
8719 cur_offset + ins.offset - 1,
8720 0);
8721 }
8722 free_extent_map(em);
8723next:
Yan Zhengd899e052008-10-30 14:25:28 -04008724 num_bytes -= ins.offset;
8725 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04008726 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00008727
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008728 inode_inc_iversion(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04008729 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02008730 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04008731 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04008732 (actual_len > inode->i_size) &&
8733 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00008734 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00008735 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00008736 else
Josef Bacik55a61d12010-11-22 18:50:32 +00008737 i_size = cur_offset;
8738 i_size_write(inode, i_size);
8739 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008740 }
8741
Yan Zhengd899e052008-10-30 14:25:28 -04008742 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008743
8744 if (ret) {
8745 btrfs_abort_transaction(trans, root, ret);
8746 if (own_trans)
8747 btrfs_end_transaction(trans, root);
8748 break;
8749 }
Yan Zhengd899e052008-10-30 14:25:28 -04008750
Josef Bacik0af3d002010-06-21 14:48:16 -04008751 if (own_trans)
8752 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008753 }
Yan Zhengd899e052008-10-30 14:25:28 -04008754 return ret;
8755}
8756
Josef Bacik0af3d002010-06-21 14:48:16 -04008757int btrfs_prealloc_file_range(struct inode *inode, int mode,
8758 u64 start, u64 num_bytes, u64 min_size,
8759 loff_t actual_len, u64 *alloc_hint)
8760{
8761 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
8762 min_size, actual_len, alloc_hint,
8763 NULL);
8764}
8765
8766int btrfs_prealloc_file_range_trans(struct inode *inode,
8767 struct btrfs_trans_handle *trans, int mode,
8768 u64 start, u64 num_bytes, u64 min_size,
8769 loff_t actual_len, u64 *alloc_hint)
8770{
8771 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
8772 min_size, actual_len, alloc_hint, trans);
8773}
8774
Chris Masone6dcd2d2008-07-17 12:53:50 -04008775static int btrfs_set_page_dirty(struct page *page)
8776{
Chris Masone6dcd2d2008-07-17 12:53:50 -04008777 return __set_page_dirty_nobuffers(page);
8778}
8779
Al Viro10556cb2011-06-20 19:28:19 -04008780static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05008781{
Li Zefanb83cc962010-12-20 16:04:08 +08008782 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00008783 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08008784
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00008785 if (mask & MAY_WRITE &&
8786 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
8787 if (btrfs_root_readonly(root))
8788 return -EROFS;
8789 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
8790 return -EACCES;
8791 }
Al Viro2830ba72011-06-20 19:16:29 -04008792 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05008793}
Chris Mason39279cc2007-06-12 06:35:45 -04008794
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008795static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05008796 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04008797 .lookup = btrfs_lookup,
8798 .create = btrfs_create,
8799 .unlink = btrfs_unlink,
8800 .link = btrfs_link,
8801 .mkdir = btrfs_mkdir,
8802 .rmdir = btrfs_rmdir,
8803 .rename = btrfs_rename,
8804 .symlink = btrfs_symlink,
8805 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04008806 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008807 .setxattr = btrfs_setxattr,
8808 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05008809 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008810 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05008811 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008812 .get_acl = btrfs_get_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -07008813 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008814};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008815static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008816 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05008817 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008818 .get_acl = btrfs_get_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -07008819 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008820};
Yan, Zheng76dda932009-09-21 16:00:26 -04008821
Alexey Dobriyan828c0952009-10-01 15:43:56 -07008822static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008823 .llseek = generic_file_llseek,
8824 .read = generic_read_dir,
Al Viro9cdda8d2013-05-22 16:48:09 -04008825 .iterate = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04008826 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04008827#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04008828 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04008829#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04008830 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04008831 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04008832};
8833
Chris Masond1310b22008-01-24 16:13:08 -05008834static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04008835 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05008836 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04008837 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04008838 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008839 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04008840 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05008841 .set_bit_hook = btrfs_set_bit_hook,
8842 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04008843 .merge_extent_hook = btrfs_merge_extent_hook,
8844 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04008845};
8846
Chris Mason35054392009-01-21 13:11:13 -05008847/*
8848 * btrfs doesn't support the bmap operation because swapfiles
8849 * use bmap to make a mapping of extents in the file. They assume
8850 * these extents won't change over the life of the file and they
8851 * use the bmap result to do IO directly to the drive.
8852 *
8853 * the btrfs bmap call would return logical addresses that aren't
8854 * suitable for IO and they also will change frequently as COW
8855 * operations happen. So, swapfile + btrfs == corruption.
8856 *
8857 * For now we're avoiding this by dropping bmap.
8858 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008859static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008860 .readpage = btrfs_readpage,
8861 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -04008862 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05008863 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04008864 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04008865 .invalidatepage = btrfs_invalidatepage,
8866 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008867 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02008868 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04008869};
8870
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008871static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008872 .readpage = btrfs_readpage,
8873 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04008874 .invalidatepage = btrfs_invalidatepage,
8875 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04008876};
8877
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008878static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008879 .getattr = btrfs_getattr,
8880 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008881 .setxattr = btrfs_setxattr,
8882 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05008883 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008884 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05008885 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008886 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008887 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008888 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008889};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008890static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04008891 .getattr = btrfs_getattr,
8892 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008893 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008894 .setxattr = btrfs_setxattr,
8895 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04008896 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008897 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008898 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008899 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -04008900};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008901static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008902 .readlink = generic_readlink,
8903 .follow_link = page_follow_link_light,
8904 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00008905 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -05008906 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008907 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05008908 .setxattr = btrfs_setxattr,
8909 .getxattr = btrfs_getxattr,
8910 .listxattr = btrfs_listxattr,
8911 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008912 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008913 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008914};
Yan, Zheng76dda932009-09-21 16:00:26 -04008915
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04008916const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04008917 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -04008918 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -04008919};