blob: 95f06936bc6e2c5afb312e4ab15e2f7a11b380dd [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"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000061#include "props.h"
Chris Mason39279cc2007-06-12 06:35:45 -040062
63struct btrfs_iget_args {
Chris Mason90d3e592014-01-09 17:28:00 -080064 struct btrfs_key *location;
Chris Mason39279cc2007-06-12 06:35:45 -040065 struct btrfs_root *root;
66};
67
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070068static const struct inode_operations btrfs_dir_inode_operations;
69static const struct inode_operations btrfs_symlink_inode_operations;
70static const struct inode_operations btrfs_dir_ro_inode_operations;
71static const struct inode_operations btrfs_special_inode_operations;
72static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070073static const struct address_space_operations btrfs_aops;
74static const struct address_space_operations btrfs_symlink_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070075static const struct file_operations btrfs_dir_file_operations;
Chris Masond1310b22008-01-24 16:13:08 -050076static struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040077
78static struct kmem_cache *btrfs_inode_cachep;
Miao Xie8ccf6f192012-10-25 09:28:04 +000079static struct kmem_cache *btrfs_delalloc_work_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040080struct kmem_cache *btrfs_trans_handle_cachep;
81struct kmem_cache *btrfs_transaction_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040082struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050083struct kmem_cache *btrfs_free_space_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040084
85#define S_SHIFT 12
86static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
87 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
88 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
89 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
90 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
91 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
92 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
93 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
94};
95
Eric Sandeen3972f262013-01-12 02:57:22 +000096static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Josef Bacika41ad392011-01-31 15:30:16 -050097static int btrfs_truncate(struct inode *inode);
Josef Bacik5fd02042012-05-02 14:00:54 -040098static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Chris Mason771ed682008-11-06 22:02:51 -050099static noinline int cow_file_range(struct inode *inode,
100 struct page *locked_page,
101 u64 start, u64 end, int *page_started,
102 unsigned long *nr_written, int unlock);
Josef Bacik70c8a912012-10-11 16:54:30 -0400103static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
104 u64 len, u64 orig_start,
105 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -0400106 u64 orig_block_len, u64 ram_bytes,
107 int type);
Josef Bacik7b128762008-07-24 12:17:14 -0400108
Eric Sandeen48a3b632013-04-25 20:41:01 +0000109static int btrfs_dirty_inode(struct inode *inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400110
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000111static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500112 struct inode *inode, struct inode *dir,
113 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500114{
115 int err;
116
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000117 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500118 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500119 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500120 return err;
121}
122
Chris Masond352ac62008-09-29 15:18:18 -0400123/*
Chris Masonc8b97812008-10-29 14:49:59 -0400124 * this does all the hard work for inserting an inline extent into
125 * the btree. The caller should have done a btrfs_drop_extents so that
126 * no overlapping inline items exist in the btree
127 */
Chris Mason40f76582014-05-21 13:35:51 -0700128static int insert_inline_extent(struct btrfs_trans_handle *trans,
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000129 struct btrfs_path *path, int extent_inserted,
Chris Masonc8b97812008-10-29 14:49:59 -0400130 struct btrfs_root *root, struct inode *inode,
131 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000132 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400133 struct page **compressed_pages)
134{
Chris Masonc8b97812008-10-29 14:49:59 -0400135 struct extent_buffer *leaf;
136 struct page *page = NULL;
137 char *kaddr;
138 unsigned long ptr;
139 struct btrfs_file_extent_item *ei;
140 int err = 0;
141 int ret;
142 size_t cur_size = size;
Chris Masonc8b97812008-10-29 14:49:59 -0400143 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400144
Li Zefanfe3f5662011-03-28 08:30:38 +0000145 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400146 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400147
Chris Masonc8b97812008-10-29 14:49:59 -0400148 inode_add_bytes(inode, size);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000149
150 if (!extent_inserted) {
151 struct btrfs_key key;
152 size_t datasize;
153
154 key.objectid = btrfs_ino(inode);
155 key.offset = start;
David Sterba962a2982014-06-04 18:41:45 +0200156 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000157
158 datasize = btrfs_file_extent_calc_inline_size(cur_size);
159 path->leave_spinning = 1;
160 ret = btrfs_insert_empty_item(trans, root, path, &key,
161 datasize);
162 if (ret) {
163 err = ret;
164 goto fail;
165 }
Chris Masonc8b97812008-10-29 14:49:59 -0400166 }
167 leaf = path->nodes[0];
168 ei = btrfs_item_ptr(leaf, path->slots[0],
169 struct btrfs_file_extent_item);
170 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
171 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
172 btrfs_set_file_extent_encryption(leaf, ei, 0);
173 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
174 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
175 ptr = btrfs_file_extent_inline_start(ei);
176
Li Zefan261507a02010-12-17 14:21:50 +0800177 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400178 struct page *cpage;
179 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500180 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400181 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500182 cur_size = min_t(unsigned long, compressed_size,
Chris Masonc8b97812008-10-29 14:49:59 -0400183 PAGE_CACHE_SIZE);
184
Cong Wang7ac687d2011-11-25 23:14:28 +0800185 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400186 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800187 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400188
189 i++;
190 ptr += cur_size;
191 compressed_size -= cur_size;
192 }
193 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800194 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400195 } else {
196 page = find_get_page(inode->i_mapping,
197 start >> PAGE_CACHE_SHIFT);
198 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800199 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400200 offset = start & (PAGE_CACHE_SIZE - 1);
201 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800202 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400203 page_cache_release(page);
204 }
205 btrfs_mark_buffer_dirty(leaf);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000206 btrfs_release_path(path);
Chris Masonc8b97812008-10-29 14:49:59 -0400207
Yan, Zhengc2167752009-11-12 09:34:21 +0000208 /*
209 * we're an inline extent, so nobody can
210 * extend the file past i_size without locking
211 * a page we already have locked.
212 *
213 * We must do any isize and inode updates
214 * before we unlock the pages. Otherwise we
215 * could end up racing with unlink.
216 */
Chris Masonc8b97812008-10-29 14:49:59 -0400217 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100218 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000219
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100220 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400221fail:
Chris Masonc8b97812008-10-29 14:49:59 -0400222 return err;
223}
224
225
226/*
227 * conditionally insert an inline extent into the file. This
228 * does the checks required to make sure the data is small enough
229 * to fit as an inline extent.
230 */
Josef Bacik00361582013-08-14 14:02:47 -0400231static noinline int cow_file_range_inline(struct btrfs_root *root,
232 struct inode *inode, u64 start,
233 u64 end, size_t compressed_size,
234 int compress_type,
235 struct page **compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400236{
Josef Bacik00361582013-08-14 14:02:47 -0400237 struct btrfs_trans_handle *trans;
Chris Masonc8b97812008-10-29 14:49:59 -0400238 u64 isize = i_size_read(inode);
239 u64 actual_end = min(end + 1, isize);
240 u64 inline_len = actual_end - start;
Qu Wenruofda28322013-02-26 08:10:22 +0000241 u64 aligned_end = ALIGN(end, root->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400242 u64 data_len = inline_len;
243 int ret;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000244 struct btrfs_path *path;
245 int extent_inserted = 0;
246 u32 extent_item_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400247
248 if (compressed_size)
249 data_len = compressed_size;
250
251 if (start > 0 ||
Wang Shilong354877b2014-07-17 11:44:11 +0800252 actual_end > PAGE_CACHE_SIZE ||
253 data_len > BTRFS_MAX_INLINE_DATA_SIZE(root) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400254 (!compressed_size &&
255 (actual_end & (root->sectorsize - 1)) == 0) ||
256 end + 1 < isize ||
257 data_len > root->fs_info->max_inline) {
258 return 1;
259 }
260
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000261 path = btrfs_alloc_path();
262 if (!path)
263 return -ENOMEM;
264
Josef Bacik00361582013-08-14 14:02:47 -0400265 trans = btrfs_join_transaction(root);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000266 if (IS_ERR(trans)) {
267 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400268 return PTR_ERR(trans);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000269 }
Josef Bacik00361582013-08-14 14:02:47 -0400270 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
271
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000272 if (compressed_size && compressed_pages)
273 extent_item_size = btrfs_file_extent_calc_inline_size(
274 compressed_size);
275 else
276 extent_item_size = btrfs_file_extent_calc_inline_size(
277 inline_len);
278
279 ret = __btrfs_drop_extents(trans, root, inode, path,
280 start, aligned_end, NULL,
281 1, 1, extent_item_size, &extent_inserted);
Josef Bacik00361582013-08-14 14:02:47 -0400282 if (ret) {
283 btrfs_abort_transaction(trans, root, ret);
284 goto out;
285 }
Chris Masonc8b97812008-10-29 14:49:59 -0400286
287 if (isize > actual_end)
288 inline_len = min_t(u64, isize, actual_end);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000289 ret = insert_inline_extent(trans, path, extent_inserted,
290 root, inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400291 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000292 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400293 if (ret && ret != -ENOSPC) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100294 btrfs_abort_transaction(trans, root, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400295 goto out;
Josef Bacik2adcac12012-05-23 16:10:14 -0400296 } else if (ret == -ENOSPC) {
Josef Bacik00361582013-08-14 14:02:47 -0400297 ret = 1;
298 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100299 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400300
Josef Bacikbdc20e62013-02-28 13:23:38 -0500301 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400302 btrfs_delalloc_release_metadata(inode, end + 1 - start);
Chris Masona1ed8352009-09-11 12:27:37 -0400303 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
Josef Bacik00361582013-08-14 14:02:47 -0400304out:
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000305 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400306 btrfs_end_transaction(trans, root);
307 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400308}
309
Chris Mason771ed682008-11-06 22:02:51 -0500310struct async_extent {
311 u64 start;
312 u64 ram_size;
313 u64 compressed_size;
314 struct page **pages;
315 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800316 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500317 struct list_head list;
318};
319
320struct async_cow {
321 struct inode *inode;
322 struct btrfs_root *root;
323 struct page *locked_page;
324 u64 start;
325 u64 end;
326 struct list_head extents;
327 struct btrfs_work work;
328};
329
330static noinline int add_async_extent(struct async_cow *cow,
331 u64 start, u64 ram_size,
332 u64 compressed_size,
333 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800334 unsigned long nr_pages,
335 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500336{
337 struct async_extent *async_extent;
338
339 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100340 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500341 async_extent->start = start;
342 async_extent->ram_size = ram_size;
343 async_extent->compressed_size = compressed_size;
344 async_extent->pages = pages;
345 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800346 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500347 list_add_tail(&async_extent->list, &cow->extents);
348 return 0;
349}
350
Wang Shilongf79707b2014-07-17 11:44:09 +0800351static inline int inode_need_compress(struct inode *inode)
352{
353 struct btrfs_root *root = BTRFS_I(inode)->root;
354
355 /* force compress */
356 if (btrfs_test_opt(root, FORCE_COMPRESS))
357 return 1;
358 /* bad compression ratios */
359 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
360 return 0;
361 if (btrfs_test_opt(root, COMPRESS) ||
362 BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS ||
363 BTRFS_I(inode)->force_compress)
364 return 1;
365 return 0;
366}
367
Chris Masonc8b97812008-10-29 14:49:59 -0400368/*
Chris Mason771ed682008-11-06 22:02:51 -0500369 * we create compressed extents in two phases. The first
370 * phase compresses a range of pages that have already been
371 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400372 *
Chris Mason771ed682008-11-06 22:02:51 -0500373 * This is done inside an ordered work queue, and the compression
374 * is spread across many cpus. The actual IO submission is step
375 * two, and the ordered work queue takes care of making sure that
376 * happens in the same order things were put onto the queue by
377 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400378 *
Chris Mason771ed682008-11-06 22:02:51 -0500379 * If this code finds it can't get good compression, it puts an
380 * entry onto the work queue to write the uncompressed bytes. This
381 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300382 * are written in the same order that the flusher thread sent them
383 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400384 */
Filipe Mananac44f6492014-10-09 21:15:44 +0100385static noinline void compress_file_range(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500386 struct page *locked_page,
387 u64 start, u64 end,
388 struct async_cow *async_cow,
389 int *num_added)
Chris Masonb888db2b2007-08-27 16:49:44 -0400390{
391 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masondb945352007-10-15 16:15:53 -0400392 u64 num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400393 u64 blocksize = root->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400394 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500395 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400396 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400397 struct page **pages = NULL;
398 unsigned long nr_pages;
399 unsigned long nr_pages_ret = 0;
400 unsigned long total_compressed = 0;
401 unsigned long total_in = 0;
402 unsigned long max_compressed = 128 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -0500403 unsigned long max_uncompressed = 128 * 1024;
Chris Masonc8b97812008-10-29 14:49:59 -0400404 int i;
405 int will_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800406 int compress_type = root->fs_info->compress_type;
Chris Mason4adaa612013-03-26 13:07:00 -0400407 int redirty = 0;
Chris Masonb888db2b2007-08-27 16:49:44 -0400408
Liu Bo4cb13e52012-03-29 09:57:45 -0400409 /* if this is a small write inside eof, kick off a defrag */
410 if ((end - start + 1) < 16 * 1024 &&
411 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400412 btrfs_add_inode_defrag(NULL, inode);
413
Chris Mason42dc7ba2008-12-15 11:44:56 -0500414 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400415again:
416 will_compress = 0;
417 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
418 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
419
Chris Masonf03d9301f2009-02-04 09:31:06 -0500420 /*
421 * we don't want to send crud past the end of i_size through
422 * compression, that's just a waste of CPU time. So, if the
423 * end of the file is before the start of our current
424 * requested range of bytes, we bail out to the uncompressed
425 * cleanup code that can deal with all of this.
426 *
427 * It isn't really the fastest way to fix things, but this is a
428 * very uncommon corner.
429 */
430 if (actual_end <= start)
431 goto cleanup_and_bail_uncompressed;
432
Chris Masonc8b97812008-10-29 14:49:59 -0400433 total_compressed = actual_end - start;
434
Shilong Wang4bcbb332014-10-07 18:44:35 -0400435 /*
436 * skip compression for a small file range(<=blocksize) that
437 * isn't an inline extent, since it dosen't save disk space at all.
438 */
439 if (total_compressed <= blocksize &&
440 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
441 goto cleanup_and_bail_uncompressed;
442
Chris Masonc8b97812008-10-29 14:49:59 -0400443 /* we want to make sure that amount of ram required to uncompress
444 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500445 * of a compressed extent to 128k. This is a crucial number
446 * because it also controls how easily we can spread reads across
447 * cpus for decompression.
448 *
449 * We also want to make sure the amount of IO required to do
450 * a random read is reasonably small, so we limit the size of
451 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400452 */
453 total_compressed = min(total_compressed, max_uncompressed);
Qu Wenruofda28322013-02-26 08:10:22 +0000454 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Masonbe20aa92007-12-17 20:14:01 -0500455 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400456 total_in = 0;
457 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400458
Chris Mason771ed682008-11-06 22:02:51 -0500459 /*
460 * we do compression for mount -o compress and when the
461 * inode has not been flagged as nocompress. This flag can
462 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400463 */
Wang Shilongf79707b2014-07-17 11:44:09 +0800464 if (inode_need_compress(inode)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400465 WARN_ON(pages);
Chris Masoncfbc2462008-10-30 13:22:14 -0400466 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800467 if (!pages) {
468 /* just bail out to the uncompressed code */
469 goto cont;
470 }
Chris Mason179e29e2007-11-01 11:28:41 -0400471
Li Zefan261507a02010-12-17 14:21:50 +0800472 if (BTRFS_I(inode)->force_compress)
473 compress_type = BTRFS_I(inode)->force_compress;
474
Chris Mason4adaa612013-03-26 13:07:00 -0400475 /*
476 * we need to call clear_page_dirty_for_io on each
477 * page in the range. Otherwise applications with the file
478 * mmap'd can wander in and change the page contents while
479 * we are compressing them.
480 *
481 * If the compression fails for any reason, we set the pages
482 * dirty again later on.
483 */
484 extent_range_clear_dirty_for_io(inode, start, end);
485 redirty = 1;
Li Zefan261507a02010-12-17 14:21:50 +0800486 ret = btrfs_compress_pages(compress_type,
487 inode->i_mapping, start,
488 total_compressed, pages,
489 nr_pages, &nr_pages_ret,
490 &total_in,
491 &total_compressed,
492 max_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400493
494 if (!ret) {
495 unsigned long offset = total_compressed &
496 (PAGE_CACHE_SIZE - 1);
497 struct page *page = pages[nr_pages_ret - 1];
498 char *kaddr;
499
500 /* zero the tail end of the last page, we might be
501 * sending it down to disk
502 */
503 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800504 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400505 memset(kaddr + offset, 0,
506 PAGE_CACHE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800507 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400508 }
509 will_compress = 1;
510 }
511 }
Li Zefan560f7d72011-09-08 10:22:01 +0800512cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400513 if (start == 0) {
514 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500515 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400516 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500517 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400518 */
Josef Bacik00361582013-08-14 14:02:47 -0400519 ret = cow_file_range_inline(root, inode, start, end,
520 0, 0, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400521 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500522 /* try making a compressed inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400523 ret = cow_file_range_inline(root, inode, start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000524 total_compressed,
525 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400526 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100527 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400528 unsigned long clear_flags = EXTENT_DELALLOC |
529 EXTENT_DEFRAG;
530 clear_flags |= (ret < 0) ? EXTENT_DO_ACCOUNTING : 0;
531
Chris Mason771ed682008-11-06 22:02:51 -0500532 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100533 * inline extent creation worked or returned error,
534 * we don't need to create any more async work items.
535 * Unlock and free up our temp pages.
Chris Mason771ed682008-11-06 22:02:51 -0500536 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400537 extent_clear_unlock_delalloc(inode, start, end, NULL,
Josef Bacik151a41b2013-07-29 13:22:24 -0400538 clear_flags, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400539 PAGE_CLEAR_DIRTY |
540 PAGE_SET_WRITEBACK |
541 PAGE_END_WRITEBACK);
Chris Masonc8b97812008-10-29 14:49:59 -0400542 goto free_pages_out;
543 }
544 }
545
546 if (will_compress) {
547 /*
548 * we aren't doing an inline extent round the compressed size
549 * up to a block size boundary so the allocator does sane
550 * things
551 */
Qu Wenruofda28322013-02-26 08:10:22 +0000552 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400553
554 /*
555 * one last check to make sure the compression is really a
556 * win, compare the page count read with the blocks on disk
557 */
Qu Wenruofda28322013-02-26 08:10:22 +0000558 total_in = ALIGN(total_in, PAGE_CACHE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400559 if (total_compressed >= total_in) {
560 will_compress = 0;
561 } else {
Chris Masonc8b97812008-10-29 14:49:59 -0400562 num_bytes = total_in;
563 }
564 }
565 if (!will_compress && pages) {
566 /*
567 * the compression code ran but failed to make things smaller,
568 * free any pages it allocated and our page pointer array
569 */
570 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400571 WARN_ON(pages[i]->mapping);
Chris Masonc8b97812008-10-29 14:49:59 -0400572 page_cache_release(pages[i]);
573 }
574 kfree(pages);
575 pages = NULL;
576 total_compressed = 0;
577 nr_pages_ret = 0;
578
579 /* flag the file so we don't compress in the future */
Chris Mason1e701a32010-03-11 09:42:04 -0500580 if (!btrfs_test_opt(root, FORCE_COMPRESS) &&
581 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500582 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500583 }
Chris Masonc8b97812008-10-29 14:49:59 -0400584 }
Chris Mason771ed682008-11-06 22:02:51 -0500585 if (will_compress) {
586 *num_added += 1;
587
588 /* the async work queues will take care of doing actual
589 * allocation on disk for these compressed pages,
590 * and will submit them to the elevator.
591 */
592 add_async_extent(async_cow, start, num_bytes,
Li Zefan261507a02010-12-17 14:21:50 +0800593 total_compressed, pages, nr_pages_ret,
594 compress_type);
Chris Mason771ed682008-11-06 22:02:51 -0500595
Yan, Zheng24ae6362010-12-06 07:02:36 +0000596 if (start + num_bytes < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500597 start += num_bytes;
598 pages = NULL;
599 cond_resched();
600 goto again;
601 }
602 } else {
Chris Masonf03d9301f2009-02-04 09:31:06 -0500603cleanup_and_bail_uncompressed:
Chris Mason771ed682008-11-06 22:02:51 -0500604 /*
605 * No compression, but we still need to write the pages in
606 * the file we've been given so far. redirty the locked
607 * page if it corresponds to our extent and set things up
608 * for the async work queue to run cow_file_range to do
609 * the normal delalloc dance
610 */
611 if (page_offset(locked_page) >= start &&
612 page_offset(locked_page) <= end) {
613 __set_page_dirty_nobuffers(locked_page);
614 /* unlocked later on in the async handlers */
615 }
Chris Mason4adaa612013-03-26 13:07:00 -0400616 if (redirty)
617 extent_range_redirty_for_io(inode, start, end);
Li Zefan261507a02010-12-17 14:21:50 +0800618 add_async_extent(async_cow, start, end - start + 1,
619 0, NULL, 0, BTRFS_COMPRESS_NONE);
Chris Mason771ed682008-11-06 22:02:51 -0500620 *num_added += 1;
621 }
622
Filipe Mananac44f6492014-10-09 21:15:44 +0100623 return;
Chris Mason771ed682008-11-06 22:02:51 -0500624
625free_pages_out:
626 for (i = 0; i < nr_pages_ret; i++) {
627 WARN_ON(pages[i]->mapping);
628 page_cache_release(pages[i]);
629 }
Chris Masond3977122009-01-05 21:25:51 -0500630 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500631}
632
Filipe Manana40ae8372014-10-06 22:14:24 +0100633static void free_async_extent_pages(struct async_extent *async_extent)
634{
635 int i;
636
637 if (!async_extent->pages)
638 return;
639
640 for (i = 0; i < async_extent->nr_pages; i++) {
641 WARN_ON(async_extent->pages[i]->mapping);
642 page_cache_release(async_extent->pages[i]);
643 }
644 kfree(async_extent->pages);
645 async_extent->nr_pages = 0;
646 async_extent->pages = NULL;
647}
648
Chris Mason771ed682008-11-06 22:02:51 -0500649/*
650 * phase two of compressed writeback. This is the ordered portion
651 * of the code, which only gets called in the order the work was
652 * queued. We walk all the async extents created by compress_file_range
653 * and send them down to the disk.
654 */
Filipe Mananadec8f172014-10-06 22:14:26 +0100655static noinline void submit_compressed_extents(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500656 struct async_cow *async_cow)
657{
658 struct async_extent *async_extent;
659 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500660 struct btrfs_key ins;
661 struct extent_map *em;
662 struct btrfs_root *root = BTRFS_I(inode)->root;
663 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
664 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500665 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500666
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500667again:
Chris Masond3977122009-01-05 21:25:51 -0500668 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500669 async_extent = list_entry(async_cow->extents.next,
670 struct async_extent, list);
671 list_del(&async_extent->list);
672
673 io_tree = &BTRFS_I(inode)->io_tree;
674
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500675retry:
Chris Mason771ed682008-11-06 22:02:51 -0500676 /* did the compression code fall back to uncompressed IO? */
677 if (!async_extent->pages) {
678 int page_started = 0;
679 unsigned long nr_written = 0;
680
681 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000682 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100683 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500684
685 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500686 ret = cow_file_range(inode, async_cow->locked_page,
687 async_extent->start,
688 async_extent->start +
689 async_extent->ram_size - 1,
690 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500691
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100692 /* JDM XXX */
693
Chris Mason771ed682008-11-06 22:02:51 -0500694 /*
695 * if page_started, cow_file_range inserted an
696 * inline extent and took care of all the unlocking
697 * and IO for us. Otherwise, we need to submit
698 * all those pages down to the drive.
699 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500700 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500701 extent_write_locked_range(io_tree,
702 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500703 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500704 async_extent->ram_size - 1,
705 btrfs_get_extent,
706 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500707 else if (ret)
708 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500709 kfree(async_extent);
710 cond_resched();
711 continue;
712 }
713
714 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100715 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500716
Josef Bacik00361582013-08-14 14:02:47 -0400717 ret = btrfs_reserve_extent(root,
Chris Mason771ed682008-11-06 22:02:51 -0500718 async_extent->compressed_size,
719 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800720 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500721 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100722 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500723
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400724 if (ret == -ENOSPC) {
725 unlock_extent(io_tree, async_extent->start,
726 async_extent->start +
727 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800728
729 /*
730 * we need to redirty the pages if we decide to
731 * fallback to uncompressed IO, otherwise we
732 * will not submit these pages down to lower
733 * layers.
734 */
735 extent_range_redirty_for_io(inode,
736 async_extent->start,
737 async_extent->start +
738 async_extent->ram_size - 1);
739
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100740 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400741 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500742 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500743 }
744
Yan, Zhengc2167752009-11-12 09:34:21 +0000745 /*
746 * here we're doing allocation and writeback of the
747 * compressed pages
748 */
749 btrfs_drop_extent_cache(inode, async_extent->start,
750 async_extent->start +
751 async_extent->ram_size - 1, 0);
752
David Sterba172ddd62011-04-21 00:48:27 +0200753 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000754 if (!em) {
755 ret = -ENOMEM;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500756 goto out_free_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000757 }
Chris Mason771ed682008-11-06 22:02:51 -0500758 em->start = async_extent->start;
759 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500760 em->orig_start = em->start;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400761 em->mod_start = em->start;
762 em->mod_len = em->len;
Chris Mason771ed682008-11-06 22:02:51 -0500763
764 em->block_start = ins.objectid;
765 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500766 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400767 em->ram_bytes = async_extent->ram_size;
Chris Mason771ed682008-11-06 22:02:51 -0500768 em->bdev = root->fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800769 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500770 set_bit(EXTENT_FLAG_PINNED, &em->flags);
771 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400772 em->generation = -1;
Chris Mason771ed682008-11-06 22:02:51 -0500773
Chris Masond3977122009-01-05 21:25:51 -0500774 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400775 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400776 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400777 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500778 if (ret != -EEXIST) {
779 free_extent_map(em);
780 break;
781 }
782 btrfs_drop_extent_cache(inode, async_extent->start,
783 async_extent->start +
784 async_extent->ram_size - 1, 0);
785 }
786
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500787 if (ret)
788 goto out_free_reserve;
789
Li Zefan261507a02010-12-17 14:21:50 +0800790 ret = btrfs_add_ordered_extent_compress(inode,
791 async_extent->start,
792 ins.objectid,
793 async_extent->ram_size,
794 ins.offset,
795 BTRFS_ORDERED_COMPRESSED,
796 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100797 if (ret) {
798 btrfs_drop_extent_cache(inode, async_extent->start,
799 async_extent->start +
800 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500801 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100802 }
Chris Mason771ed682008-11-06 22:02:51 -0500803
Chris Mason771ed682008-11-06 22:02:51 -0500804 /*
805 * clear dirty, set writeback and unlock the pages.
806 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400807 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400808 async_extent->start +
809 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400810 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
811 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400812 PAGE_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500813 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500814 async_extent->start,
815 async_extent->ram_size,
816 ins.objectid,
817 ins.offset, async_extent->pages,
818 async_extent->nr_pages);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100819 if (ret) {
820 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
821 struct page *p = async_extent->pages[0];
822 const u64 start = async_extent->start;
823 const u64 end = start + async_extent->ram_size - 1;
824
825 p->mapping = inode->i_mapping;
826 tree->ops->writepage_end_io_hook(p, start, end,
827 NULL, 0);
828 p->mapping = NULL;
829 extent_clear_unlock_delalloc(inode, start, end, NULL, 0,
830 PAGE_END_WRITEBACK |
831 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100832 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100833 }
Chris Mason771ed682008-11-06 22:02:51 -0500834 alloc_hint = ins.objectid + ins.offset;
835 kfree(async_extent);
836 cond_resched();
837 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100838 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500839out_free_reserve:
Miao Xiee570fd22014-06-19 10:42:50 +0800840 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100841out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400842 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500843 async_extent->start +
844 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400845 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400846 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
847 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100848 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
849 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100850 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100851 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500852 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500853}
854
Josef Bacik4b46fce2010-05-23 11:00:55 -0400855static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
856 u64 num_bytes)
857{
858 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
859 struct extent_map *em;
860 u64 alloc_hint = 0;
861
862 read_lock(&em_tree->lock);
863 em = search_extent_mapping(em_tree, start, num_bytes);
864 if (em) {
865 /*
866 * if block start isn't an actual block number then find the
867 * first block in this inode and use that as a hint. If that
868 * block is also bogus then just don't worry about it.
869 */
870 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
871 free_extent_map(em);
872 em = search_extent_mapping(em_tree, 0, 0);
873 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
874 alloc_hint = em->block_start;
875 if (em)
876 free_extent_map(em);
877 } else {
878 alloc_hint = em->block_start;
879 free_extent_map(em);
880 }
881 }
882 read_unlock(&em_tree->lock);
883
884 return alloc_hint;
885}
886
Chris Mason771ed682008-11-06 22:02:51 -0500887/*
888 * when extent_io.c finds a delayed allocation range in the file,
889 * the call backs end up in this code. The basic idea is to
890 * allocate extents on disk for the range, and create ordered data structs
891 * in ram to track those extents.
892 *
893 * locked_page is the page that writepage had locked already. We use
894 * it to make sure we don't do extra locks or unlocks.
895 *
896 * *page_started is set to one if we unlock locked_page and do everything
897 * required to start IO on it. It may be clean and already done with
898 * IO when we return.
899 */
Josef Bacik00361582013-08-14 14:02:47 -0400900static noinline int cow_file_range(struct inode *inode,
901 struct page *locked_page,
902 u64 start, u64 end, int *page_started,
903 unsigned long *nr_written,
904 int unlock)
Chris Mason771ed682008-11-06 22:02:51 -0500905{
Josef Bacik00361582013-08-14 14:02:47 -0400906 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500907 u64 alloc_hint = 0;
908 u64 num_bytes;
909 unsigned long ram_size;
910 u64 disk_num_bytes;
911 u64 cur_alloc_size;
912 u64 blocksize = root->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500913 struct btrfs_key ins;
914 struct extent_map *em;
915 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
916 int ret = 0;
917
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400918 if (btrfs_is_free_space_inode(inode)) {
919 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500920 ret = -EINVAL;
921 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400922 }
Chris Mason771ed682008-11-06 22:02:51 -0500923
Qu Wenruofda28322013-02-26 08:10:22 +0000924 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500925 num_bytes = max(blocksize, num_bytes);
926 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500927
Chris Mason4cb53002011-05-24 15:35:30 -0400928 /* if this is a small write inside eof, kick off defrag */
Liu Bo4cb13e52012-03-29 09:57:45 -0400929 if (num_bytes < 64 * 1024 &&
930 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Josef Bacik00361582013-08-14 14:02:47 -0400931 btrfs_add_inode_defrag(NULL, inode);
Chris Mason4cb53002011-05-24 15:35:30 -0400932
Chris Mason771ed682008-11-06 22:02:51 -0500933 if (start == 0) {
934 /* lets try to make an inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400935 ret = cow_file_range_inline(root, inode, start, end, 0, 0,
936 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500937 if (ret == 0) {
Josef Bacikc2790a22013-07-29 11:20:47 -0400938 extent_clear_unlock_delalloc(inode, start, end, NULL,
939 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400940 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400941 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
942 PAGE_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000943
Chris Mason771ed682008-11-06 22:02:51 -0500944 *nr_written = *nr_written +
945 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
946 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500947 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100948 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100949 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500950 }
951 }
Chris Masonc8b97812008-10-29 14:49:59 -0400952
953 BUG_ON(disk_num_bytes >
David Sterba6c417612011-04-13 15:41:04 +0200954 btrfs_super_total_bytes(root->fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400955
Josef Bacik4b46fce2010-05-23 11:00:55 -0400956 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400957 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400958
Chris Masond3977122009-01-05 21:25:51 -0500959 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400960 unsigned long op;
961
Josef Bacik287a0ab2010-03-19 18:07:23 +0000962 cur_alloc_size = disk_num_bytes;
Josef Bacik00361582013-08-14 14:02:47 -0400963 ret = btrfs_reserve_extent(root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500964 root->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +0800965 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -0400966 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100967 goto out_unlock;
Chris Masond3977122009-01-05 21:25:51 -0500968
David Sterba172ddd62011-04-21 00:48:27 +0200969 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000970 if (!em) {
971 ret = -ENOMEM;
Liu Boace68ba2013-04-22 10:53:47 +0000972 goto out_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000973 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400974 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500975 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500976 ram_size = ins.offset;
977 em->len = ins.offset;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400978 em->mod_start = em->start;
979 em->mod_len = em->len;
Chris Masonc8b97812008-10-29 14:49:59 -0400980
Chris Masone6dcd2d2008-07-17 12:53:50 -0400981 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400982 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500983 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400984 em->ram_bytes = ram_size;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400985 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400986 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400987 em->generation = -1;
Chris Masonc8b97812008-10-29 14:49:59 -0400988
Chris Masond3977122009-01-05 21:25:51 -0500989 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400990 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400991 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400992 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400993 if (ret != -EEXIST) {
994 free_extent_map(em);
995 break;
996 }
997 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400998 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400999 }
Liu Boace68ba2013-04-22 10:53:47 +00001000 if (ret)
1001 goto out_reserve;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001002
Chris Mason98d20f62008-04-14 09:46:10 -04001003 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001004 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -05001005 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001006 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001007 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001008
Yan Zheng17d217f2008-12-12 10:03:38 -05001009 if (root->root_key.objectid ==
1010 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1011 ret = btrfs_reloc_clone_csums(inode, start,
1012 cur_alloc_size);
Josef Bacik00361582013-08-14 14:02:47 -04001013 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001014 goto out_drop_extent_cache;
Yan Zheng17d217f2008-12-12 10:03:38 -05001015 }
1016
Chris Masond3977122009-01-05 21:25:51 -05001017 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -04001018 break;
Chris Masond3977122009-01-05 21:25:51 -05001019
Chris Masonc8b97812008-10-29 14:49:59 -04001020 /* we're not doing compressed IO, don't unlock the first
1021 * page (which the caller expects to stay locked), don't
1022 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001023 *
1024 * Do set the Private2 bit so we know this page was properly
1025 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001026 */
Josef Bacikc2790a22013-07-29 11:20:47 -04001027 op = unlock ? PAGE_UNLOCK : 0;
1028 op |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001029
Josef Bacikc2790a22013-07-29 11:20:47 -04001030 extent_clear_unlock_delalloc(inode, start,
1031 start + ram_size - 1, locked_page,
1032 EXTENT_LOCKED | EXTENT_DELALLOC,
1033 op);
Chris Masonc8b97812008-10-29 14:49:59 -04001034 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001035 num_bytes -= cur_alloc_size;
1036 alloc_hint = ins.objectid + ins.offset;
1037 start += cur_alloc_size;
Chris Masonb888db2b2007-08-27 16:49:44 -04001038 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001039out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001040 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001041
Filipe Mananad9f85962014-08-25 10:43:00 +01001042out_drop_extent_cache:
1043 btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001044out_reserve:
Miao Xiee570fd22014-06-19 10:42:50 +08001045 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001046out_unlock:
Josef Bacikc2790a22013-07-29 11:20:47 -04001047 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Josef Bacik151a41b2013-07-29 13:22:24 -04001048 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
1049 EXTENT_DELALLOC | EXTENT_DEFRAG,
1050 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
1051 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001052 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001053}
Chris Masonc8b97812008-10-29 14:49:59 -04001054
Chris Mason771ed682008-11-06 22:02:51 -05001055/*
1056 * work queue call back to started compression on a file and pages
1057 */
1058static noinline void async_cow_start(struct btrfs_work *work)
1059{
1060 struct async_cow *async_cow;
1061 int num_added = 0;
1062 async_cow = container_of(work, struct async_cow, work);
1063
1064 compress_file_range(async_cow->inode, async_cow->locked_page,
1065 async_cow->start, async_cow->end, async_cow,
1066 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001067 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001068 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001069 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001070 }
Chris Mason771ed682008-11-06 22:02:51 -05001071}
1072
1073/*
1074 * work queue call back to submit previously compressed pages
1075 */
1076static noinline void async_cow_submit(struct btrfs_work *work)
1077{
1078 struct async_cow *async_cow;
1079 struct btrfs_root *root;
1080 unsigned long nr_pages;
1081
1082 async_cow = container_of(work, struct async_cow, work);
1083
1084 root = async_cow->root;
1085 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
1086 PAGE_CACHE_SHIFT;
1087
Josef Bacik66657b32012-08-01 15:36:24 -04001088 if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) <
Liu Bo287082b2012-06-28 04:02:24 -06001089 5 * 1024 * 1024 &&
Chris Mason771ed682008-11-06 22:02:51 -05001090 waitqueue_active(&root->fs_info->async_submit_wait))
1091 wake_up(&root->fs_info->async_submit_wait);
1092
Chris Masond3977122009-01-05 21:25:51 -05001093 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001094 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001095}
Chris Masonc8b97812008-10-29 14:49:59 -04001096
Chris Mason771ed682008-11-06 22:02:51 -05001097static noinline void async_cow_free(struct btrfs_work *work)
1098{
1099 struct async_cow *async_cow;
1100 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001101 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001102 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001103 kfree(async_cow);
1104}
1105
1106static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1107 u64 start, u64 end, int *page_started,
1108 unsigned long *nr_written)
1109{
1110 struct async_cow *async_cow;
1111 struct btrfs_root *root = BTRFS_I(inode)->root;
1112 unsigned long nr_pages;
1113 u64 cur_end;
Liu Bo287082b2012-06-28 04:02:24 -06001114 int limit = 10 * 1024 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -05001115
Chris Masona3429ab2009-10-08 12:30:20 -04001116 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1117 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001118 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001119 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001120 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001121 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001122 async_cow->root = root;
1123 async_cow->locked_page = locked_page;
1124 async_cow->start = start;
1125
Wang Shilongf79707b2014-07-17 11:44:09 +08001126 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
1127 !btrfs_test_opt(root, FORCE_COMPRESS))
Chris Mason771ed682008-11-06 22:02:51 -05001128 cur_end = end;
1129 else
1130 cur_end = min(end, start + 512 * 1024 - 1);
1131
1132 async_cow->end = cur_end;
1133 INIT_LIST_HEAD(&async_cow->extents);
1134
Liu Bo9e0af232014-08-15 23:36:53 +08001135 btrfs_init_work(&async_cow->work,
1136 btrfs_delalloc_helper,
1137 async_cow_start, async_cow_submit,
1138 async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001139
Chris Mason771ed682008-11-06 22:02:51 -05001140 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
1141 PAGE_CACHE_SHIFT;
1142 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
1143
Qu Wenruoafe3d242014-02-28 10:46:07 +08001144 btrfs_queue_work(root->fs_info->delalloc_workers,
1145 &async_cow->work);
Chris Mason771ed682008-11-06 22:02:51 -05001146
1147 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
1148 wait_event(root->fs_info->async_submit_wait,
1149 (atomic_read(&root->fs_info->async_delalloc_pages) <
1150 limit));
1151 }
1152
Chris Masond3977122009-01-05 21:25:51 -05001153 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -05001154 atomic_read(&root->fs_info->async_delalloc_pages)) {
1155 wait_event(root->fs_info->async_submit_wait,
1156 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1157 0));
1158 }
1159
1160 *nr_written += nr_pages;
1161 start = cur_end + 1;
1162 }
1163 *page_started = 1;
1164 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001165}
1166
Chris Masond3977122009-01-05 21:25:51 -05001167static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001168 u64 bytenr, u64 num_bytes)
1169{
1170 int ret;
1171 struct btrfs_ordered_sum *sums;
1172 LIST_HEAD(list);
1173
Yan Zheng07d400a2009-01-06 11:42:00 -05001174 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001175 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001176 if (ret == 0 && list_empty(&list))
1177 return 0;
1178
1179 while (!list_empty(&list)) {
1180 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1181 list_del(&sums->list);
1182 kfree(sums);
1183 }
1184 return 1;
1185}
1186
Chris Masond352ac62008-09-29 15:18:18 -04001187/*
1188 * when nowcow writeback call back. This checks for snapshots or COW copies
1189 * of the extents that exist in the file, and COWs the file as required.
1190 *
1191 * If no cow copies or snapshots exist, we write directly to the existing
1192 * blocks on disk
1193 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001194static noinline int run_delalloc_nocow(struct inode *inode,
1195 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001196 u64 start, u64 end, int *page_started, int force,
1197 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001198{
Chris Masonbe20aa92007-12-17 20:14:01 -05001199 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001200 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001201 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001202 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001203 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001204 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001205 u64 cow_start;
1206 u64 cur_offset;
1207 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001208 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001209 u64 disk_bytenr;
1210 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001211 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001212 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001213 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001214 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001215 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001216 int nocow;
1217 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001218 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001219 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001220
1221 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001222 if (!path) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001223 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1224 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001225 EXTENT_DO_ACCOUNTING |
1226 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001227 PAGE_CLEAR_DIRTY |
1228 PAGE_SET_WRITEBACK |
1229 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001230 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001231 }
Li Zefan82d59022011-04-20 10:33:24 +08001232
Liu Bo83eea1f2012-07-10 05:28:39 -06001233 nolock = btrfs_is_free_space_inode(inode);
Li Zefan82d59022011-04-20 10:33:24 +08001234
1235 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001236 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001237 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001238 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001239
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001240 if (IS_ERR(trans)) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001241 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1242 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001243 EXTENT_DO_ACCOUNTING |
1244 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001245 PAGE_CLEAR_DIRTY |
1246 PAGE_SET_WRITEBACK |
1247 PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001248 btrfs_free_path(path);
1249 return PTR_ERR(trans);
1250 }
1251
Josef Bacik74b21072011-04-13 12:02:53 -04001252 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001253
Yan Zheng80ff3852008-10-30 14:20:02 -04001254 cow_start = (u64)-1;
1255 cur_offset = start;
1256 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001257 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001258 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001259 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001260 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001261 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1262 leaf = path->nodes[0];
1263 btrfs_item_key_to_cpu(leaf, &found_key,
1264 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001265 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001266 found_key.type == BTRFS_EXTENT_DATA_KEY)
1267 path->slots[0]--;
1268 }
1269 check_prev = 0;
1270next_slot:
1271 leaf = path->nodes[0];
1272 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1273 ret = btrfs_next_leaf(root, path);
Josef Bacikd788a342013-10-25 16:55:08 -04001274 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001275 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001276 if (ret > 0)
1277 break;
1278 leaf = path->nodes[0];
1279 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001280
Yan Zheng80ff3852008-10-30 14:20:02 -04001281 nocow = 0;
1282 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001283 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001284 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001285
Li Zefan33345d012011-04-20 10:31:50 +08001286 if (found_key.objectid > ino ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001287 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1288 found_key.offset > end)
1289 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001290
Yan Zheng80ff3852008-10-30 14:20:02 -04001291 if (found_key.offset > cur_offset) {
1292 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001293 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001294 goto out_check;
1295 }
Chris Masonc31f8832008-01-08 15:46:31 -05001296
Yan Zheng80ff3852008-10-30 14:20:02 -04001297 fi = btrfs_item_ptr(leaf, path->slots[0],
1298 struct btrfs_file_extent_item);
1299 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001300
Josef Bacikcc95bef2013-04-04 14:31:27 -04001301 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001302 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1303 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001304 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001305 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001306 extent_end = found_key.offset +
1307 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001308 disk_num_bytes =
1309 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001310 if (extent_end <= start) {
1311 path->slots[0]++;
1312 goto next_slot;
1313 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001314 if (disk_bytenr == 0)
1315 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001316 if (btrfs_file_extent_compression(leaf, fi) ||
1317 btrfs_file_extent_encryption(leaf, fi) ||
1318 btrfs_file_extent_other_encoding(leaf, fi))
1319 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001320 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1321 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001322 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001323 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001324 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001325 found_key.offset -
1326 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001327 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001328 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001329 disk_bytenr += cur_offset - found_key.offset;
1330 num_bytes = min(end + 1, extent_end) - cur_offset;
1331 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001332 * if there are pending snapshots for this root,
1333 * we fall into common COW way.
1334 */
1335 if (!nolock) {
1336 err = btrfs_start_nocow_write(root);
1337 if (!err)
1338 goto out_check;
1339 }
1340 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001341 * force cow if csum exists in the range.
1342 * this ensure that csum for a given extent are
1343 * either valid or do not exist.
1344 */
1345 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1346 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001347 nocow = 1;
1348 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1349 extent_end = found_key.offset +
Chris Mason514ac8a2014-01-03 21:07:00 -08001350 btrfs_file_extent_inline_len(leaf,
1351 path->slots[0], fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001352 extent_end = ALIGN(extent_end, root->sectorsize);
1353 } else {
1354 BUG_ON(1);
1355 }
1356out_check:
1357 if (extent_end <= start) {
1358 path->slots[0]++;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001359 if (!nolock && nocow)
1360 btrfs_end_nocow_write(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001361 goto next_slot;
1362 }
1363 if (!nocow) {
1364 if (cow_start == (u64)-1)
1365 cow_start = cur_offset;
1366 cur_offset = extent_end;
1367 if (cur_offset > end)
1368 break;
1369 path->slots[0]++;
1370 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001371 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001372
David Sterbab3b4aa72011-04-21 01:20:15 +02001373 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001374 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001375 ret = cow_file_range(inode, locked_page,
1376 cow_start, found_key.offset - 1,
1377 page_started, nr_written, 1);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001378 if (ret) {
1379 if (!nolock && nocow)
1380 btrfs_end_nocow_write(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001381 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001382 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001383 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001384 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001385
Yan Zhengd899e052008-10-30 14:25:28 -04001386 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1387 struct extent_map *em;
1388 struct extent_map_tree *em_tree;
1389 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001390 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001391 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001392 em->start = cur_offset;
Josef Bacik70c8a912012-10-11 16:54:30 -04001393 em->orig_start = found_key.offset - extent_offset;
Yan Zhengd899e052008-10-30 14:25:28 -04001394 em->len = num_bytes;
1395 em->block_len = num_bytes;
1396 em->block_start = disk_bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05001397 em->orig_block_len = disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001398 em->ram_bytes = ram_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04001399 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001400 em->mod_start = em->start;
1401 em->mod_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04001402 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacikb11e2342012-12-03 10:58:15 -05001403 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001404 em->generation = -1;
Yan Zhengd899e052008-10-30 14:25:28 -04001405 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001406 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001407 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001408 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001409 if (ret != -EEXIST) {
1410 free_extent_map(em);
1411 break;
1412 }
1413 btrfs_drop_extent_cache(inode, em->start,
1414 em->start + em->len - 1, 0);
1415 }
1416 type = BTRFS_ORDERED_PREALLOC;
1417 } else {
1418 type = BTRFS_ORDERED_NOCOW;
1419 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001420
1421 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001422 num_bytes, num_bytes, type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001423 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001424
Yan, Zhengefa56462010-05-16 10:49:59 -04001425 if (root->root_key.objectid ==
1426 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1427 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1428 num_bytes);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001429 if (ret) {
1430 if (!nolock && nocow)
1431 btrfs_end_nocow_write(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001432 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001433 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001434 }
1435
Josef Bacikc2790a22013-07-29 11:20:47 -04001436 extent_clear_unlock_delalloc(inode, cur_offset,
1437 cur_offset + num_bytes - 1,
1438 locked_page, EXTENT_LOCKED |
1439 EXTENT_DELALLOC, PAGE_UNLOCK |
1440 PAGE_SET_PRIVATE2);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001441 if (!nolock && nocow)
1442 btrfs_end_nocow_write(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001443 cur_offset = extent_end;
1444 if (cur_offset > end)
1445 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001446 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001447 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001448
Josef Bacik17ca04a2012-05-31 15:58:55 -04001449 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001450 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001451 cur_offset = end;
1452 }
1453
Yan Zheng80ff3852008-10-30 14:20:02 -04001454 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001455 ret = cow_file_range(inode, locked_page, cow_start, end,
1456 page_started, nr_written, 1);
Josef Bacikd788a342013-10-25 16:55:08 -04001457 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001458 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001459 }
1460
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001461error:
Miao Xiea698d0752012-09-20 01:51:59 -06001462 err = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001463 if (!ret)
1464 ret = err;
1465
Josef Bacik17ca04a2012-05-31 15:58:55 -04001466 if (ret && cur_offset < end)
Josef Bacikc2790a22013-07-29 11:20:47 -04001467 extent_clear_unlock_delalloc(inode, cur_offset, end,
1468 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001469 EXTENT_DELALLOC | EXTENT_DEFRAG |
1470 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1471 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001472 PAGE_SET_WRITEBACK |
1473 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001474 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001475 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001476}
1477
Wang Shilong47059d92014-07-03 18:22:07 +08001478static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1479{
1480
1481 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1482 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1483 return 0;
1484
1485 /*
1486 * @defrag_bytes is a hint value, no spinlock held here,
1487 * if is not zero, it means the file is defragging.
1488 * Force cow if given extent needs to be defragged.
1489 */
1490 if (BTRFS_I(inode)->defrag_bytes &&
1491 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1492 EXTENT_DEFRAG, 0, NULL))
1493 return 1;
1494
1495 return 0;
1496}
1497
Chris Masond352ac62008-09-29 15:18:18 -04001498/*
1499 * extent_io.c call back to do delayed allocation processing
1500 */
Chris Masonc8b97812008-10-29 14:49:59 -04001501static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001502 u64 start, u64 end, int *page_started,
1503 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001504{
Chris Masonbe20aa92007-12-17 20:14:01 -05001505 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001506 int force_cow = need_force_cow(inode, start, end);
Chris Masona2135012008-06-25 16:01:30 -04001507
Wang Shilong47059d92014-07-03 18:22:07 +08001508 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001509 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001510 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001511 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001512 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001513 page_started, 0, nr_written);
Wang Shilong78160302014-07-17 11:44:10 +08001514 } else if (!inode_need_compress(inode)) {
Chris Mason7f366cf2009-03-12 20:12:45 -04001515 ret = cow_file_range(inode, locked_page, start, end,
1516 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001517 } else {
1518 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1519 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001520 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001521 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001522 }
Chris Masonb888db2b2007-08-27 16:49:44 -04001523 return ret;
1524}
1525
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001526static void btrfs_split_extent_hook(struct inode *inode,
1527 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001528{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001529 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001530 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001531 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001532
Josef Bacik9e0baf62011-07-15 15:16:44 +00001533 spin_lock(&BTRFS_I(inode)->lock);
1534 BTRFS_I(inode)->outstanding_extents++;
1535 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001536}
1537
1538/*
1539 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1540 * extents so we can keep track of new extents that are just merged onto old
1541 * extents, such as when we are doing sequential writes, so we can properly
1542 * account for the metadata space we'll need.
1543 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001544static void btrfs_merge_extent_hook(struct inode *inode,
1545 struct extent_state *new,
1546 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001547{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001548 /* not delalloc, ignore it */
1549 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001550 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001551
Josef Bacik9e0baf62011-07-15 15:16:44 +00001552 spin_lock(&BTRFS_I(inode)->lock);
1553 BTRFS_I(inode)->outstanding_extents--;
1554 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001555}
1556
Miao Xieeb73c1b2013-05-15 07:48:22 +00001557static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1558 struct inode *inode)
1559{
1560 spin_lock(&root->delalloc_lock);
1561 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1562 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1563 &root->delalloc_inodes);
1564 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1565 &BTRFS_I(inode)->runtime_flags);
1566 root->nr_delalloc_inodes++;
1567 if (root->nr_delalloc_inodes == 1) {
1568 spin_lock(&root->fs_info->delalloc_root_lock);
1569 BUG_ON(!list_empty(&root->delalloc_root));
1570 list_add_tail(&root->delalloc_root,
1571 &root->fs_info->delalloc_roots);
1572 spin_unlock(&root->fs_info->delalloc_root_lock);
1573 }
1574 }
1575 spin_unlock(&root->delalloc_lock);
1576}
1577
1578static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1579 struct inode *inode)
1580{
1581 spin_lock(&root->delalloc_lock);
1582 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1583 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1584 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1585 &BTRFS_I(inode)->runtime_flags);
1586 root->nr_delalloc_inodes--;
1587 if (!root->nr_delalloc_inodes) {
1588 spin_lock(&root->fs_info->delalloc_root_lock);
1589 BUG_ON(list_empty(&root->delalloc_root));
1590 list_del_init(&root->delalloc_root);
1591 spin_unlock(&root->fs_info->delalloc_root_lock);
1592 }
1593 }
1594 spin_unlock(&root->delalloc_lock);
1595}
1596
Chris Masond352ac62008-09-29 15:18:18 -04001597/*
1598 * extent_io.c set_bit_hook, used to track delayed allocation
1599 * bytes in this file, and to maintain the list of inodes that
1600 * have pending delalloc work to be done.
1601 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001602static void btrfs_set_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001603 struct extent_state *state, unsigned long *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001604{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001605
Wang Shilong47059d92014-07-03 18:22:07 +08001606 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1607 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001608 /*
1609 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001610 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001611 * bit, which is only set or cleared with irqs on
1612 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001613 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001614 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001615 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001616 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001617
Josef Bacik9e0baf62011-07-15 15:16:44 +00001618 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001619 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001620 } else {
1621 spin_lock(&BTRFS_I(inode)->lock);
1622 BTRFS_I(inode)->outstanding_extents++;
1623 spin_unlock(&BTRFS_I(inode)->lock);
1624 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001625
Miao Xie963d6782013-01-29 10:10:51 +00001626 __percpu_counter_add(&root->fs_info->delalloc_bytes, len,
1627 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001628 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001629 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001630 if (*bits & EXTENT_DEFRAG)
1631 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001632 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001633 &BTRFS_I(inode)->runtime_flags))
1634 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001635 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001636 }
Chris Mason291d6732008-01-29 15:55:23 -05001637}
1638
Chris Masond352ac62008-09-29 15:18:18 -04001639/*
1640 * extent_io.c clear_bit_hook, see set_bit_hook for why
1641 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001642static void btrfs_clear_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001643 struct extent_state *state,
1644 unsigned long *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001645{
Wang Shilong47059d92014-07-03 18:22:07 +08001646 u64 len = state->end + 1 - state->start;
1647
1648 spin_lock(&BTRFS_I(inode)->lock);
1649 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG))
1650 BTRFS_I(inode)->defrag_bytes -= len;
1651 spin_unlock(&BTRFS_I(inode)->lock);
1652
Chris Mason75eff682008-12-15 15:54:40 -05001653 /*
1654 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001655 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001656 * bit, which is only set or cleared with irqs on
1657 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001658 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001659 struct btrfs_root *root = BTRFS_I(inode)->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001660 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001661
Josef Bacik9e0baf62011-07-15 15:16:44 +00001662 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001663 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001664 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1665 spin_lock(&BTRFS_I(inode)->lock);
1666 BTRFS_I(inode)->outstanding_extents--;
1667 spin_unlock(&BTRFS_I(inode)->lock);
1668 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001669
Josef Bacikb6d08f02013-09-27 14:57:43 -04001670 /*
1671 * We don't reserve metadata space for space cache inodes so we
1672 * don't need to call dellalloc_release_metadata if there is an
1673 * error.
1674 */
1675 if (*bits & EXTENT_DO_ACCOUNTING &&
1676 root != root->fs_info->tree_root)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001677 btrfs_delalloc_release_metadata(inode, len);
1678
Josef Bacik0cb59c92010-07-02 12:14:14 -04001679 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Josef Bacik7ee9e442013-06-21 16:37:03 -04001680 && do_list && !(state->state & EXTENT_NORESERVE))
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001681 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001682
Miao Xie963d6782013-01-29 10:10:51 +00001683 __percpu_counter_add(&root->fs_info->delalloc_bytes, -len,
1684 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001685 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001686 BTRFS_I(inode)->delalloc_bytes -= len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001687 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001688 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001689 &BTRFS_I(inode)->runtime_flags))
1690 btrfs_del_delalloc_inode(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001691 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001692 }
Chris Mason291d6732008-01-29 15:55:23 -05001693}
1694
Chris Masond352ac62008-09-29 15:18:18 -04001695/*
1696 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1697 * we don't create bios that span stripes or chunks
1698 */
David Woodhouse64a16702009-07-15 23:29:37 +01001699int btrfs_merge_bio_hook(int rw, struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001700 size_t size, struct bio *bio,
1701 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001702{
1703 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001704 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001705 u64 length = 0;
1706 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001707 int ret;
1708
Chris Mason771ed682008-11-06 22:02:51 -05001709 if (bio_flags & EXTENT_BIO_COMPRESSED)
1710 return 0;
1711
Kent Overstreet4f024f32013-10-11 15:44:27 -07001712 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001713 map_length = length;
David Woodhouse64a16702009-07-15 23:29:37 +01001714 ret = btrfs_map_block(root->fs_info, rw, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001715 &map_length, NULL, 0);
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001716 /* Will always return 0 with map_multi == NULL */
Jeff Mahoney3444a972011-10-03 23:23:13 -04001717 BUG_ON(ret < 0);
Chris Masond3977122009-01-05 21:25:51 -05001718 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001719 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001720 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001721}
1722
Chris Masond352ac62008-09-29 15:18:18 -04001723/*
1724 * in order to insert checksums into the metadata in large chunks,
1725 * we wait until bio submission time. All the pages in the bio are
1726 * checksummed and sums are attached onto the ordered extent record.
1727 *
1728 * At IO completion time the cums attached on the ordered extent record
1729 * are inserted into the btree
1730 */
Chris Masond3977122009-01-05 21:25:51 -05001731static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1732 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001733 unsigned long bio_flags,
1734 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001735{
Chris Mason065631f2008-02-20 12:07:25 -05001736 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001737 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001738
Chris Masond20f7042008-12-08 16:58:54 -05001739 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001740 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001741 return 0;
1742}
Chris Masone0156402008-04-16 11:15:20 -04001743
Chris Mason4a69a412008-11-06 22:03:00 -05001744/*
1745 * in order to insert checksums into the metadata in large chunks,
1746 * we wait until bio submission time. All the pages in the bio are
1747 * checksummed and sums are attached onto the ordered extent record.
1748 *
1749 * At IO completion time the cums attached on the ordered extent record
1750 * are inserted into the btree
1751 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001752static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001753 int mirror_num, unsigned long bio_flags,
1754 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001755{
1756 struct btrfs_root *root = BTRFS_I(inode)->root;
Stefan Behrens61891922012-11-05 18:51:52 +01001757 int ret;
1758
1759 ret = btrfs_map_bio(root, rw, bio, mirror_num, 1);
1760 if (ret)
1761 bio_endio(bio, ret);
1762 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001763}
1764
Chris Masond352ac62008-09-29 15:18:18 -04001765/*
Chris Masoncad321a2008-12-17 14:51:42 -05001766 * extent_io.c submission hook. This does the right thing for csum calculation
1767 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001768 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001769static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001770 int mirror_num, unsigned long bio_flags,
1771 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001772{
1773 struct btrfs_root *root = BTRFS_I(inode)->root;
1774 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001775 int skip_sum;
Jeff Mahoney04173412011-10-03 23:23:12 -04001776 int metadata = 0;
Josef Bacikb812ce22012-11-16 13:56:32 -05001777 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001778
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001779 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001780
Liu Bo83eea1f2012-07-10 05:28:39 -06001781 if (btrfs_is_free_space_inode(inode))
Jeff Mahoney04173412011-10-03 23:23:12 -04001782 metadata = 2;
1783
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001784 if (!(rw & REQ_WRITE)) {
Josef Bacik5fd02042012-05-02 14:00:54 -04001785 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1786 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001787 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001788
Chris Masond20f7042008-12-08 16:58:54 -05001789 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001790 ret = btrfs_submit_compressed_read(inode, bio,
1791 mirror_num,
1792 bio_flags);
1793 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001794 } else if (!skip_sum) {
1795 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1796 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001797 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001798 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001799 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001800 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001801 /* csum items have already been cloned */
1802 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1803 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001804 /* we're doing a write, do the async checksumming */
Stefan Behrens61891922012-11-05 18:51:52 +01001805 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001806 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001807 bio_flags, bio_offset,
1808 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001809 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001810 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001811 } else if (!skip_sum) {
1812 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1813 if (ret)
1814 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001815 }
1816
Chris Mason0b86a832008-03-24 15:01:56 -04001817mapit:
Stefan Behrens61891922012-11-05 18:51:52 +01001818 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
1819
1820out:
1821 if (ret < 0)
1822 bio_endio(bio, ret);
1823 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001824}
Chris Mason6885f302008-02-20 16:11:05 -05001825
Chris Masond352ac62008-09-29 15:18:18 -04001826/*
1827 * given a list of ordered sums record them in the inode. This happens
1828 * at IO completion time based on sums calculated at bio submission time.
1829 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001830static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001831 struct inode *inode, u64 file_offset,
1832 struct list_head *list)
1833{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001834 struct btrfs_ordered_sum *sum;
1835
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001836 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00001837 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05001838 btrfs_csum_file_blocks(trans,
1839 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00001840 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001841 }
1842 return 0;
1843}
1844
Josef Bacik2ac55d42010-02-03 19:33:23 +00001845int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1846 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001847{
Julia Lawall6c1500f2012-11-03 20:30:18 +00001848 WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04001849 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001850 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001851}
1852
Chris Masond352ac62008-09-29 15:18:18 -04001853/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001854struct btrfs_writepage_fixup {
1855 struct page *page;
1856 struct btrfs_work work;
1857};
1858
Christoph Hellwigb2950862008-12-02 09:54:17 -05001859static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001860{
1861 struct btrfs_writepage_fixup *fixup;
1862 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001863 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001864 struct page *page;
1865 struct inode *inode;
1866 u64 page_start;
1867 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001868 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001869
1870 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1871 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001872again:
Chris Mason247e7432008-07-17 12:53:51 -04001873 lock_page(page);
1874 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1875 ClearPageChecked(page);
1876 goto out_page;
1877 }
1878
1879 inode = page->mapping->host;
1880 page_start = page_offset(page);
1881 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1882
Josef Bacik2ac55d42010-02-03 19:33:23 +00001883 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001884 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001885
1886 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001887 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001888 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001889
1890 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1891 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001892 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1893 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001894 unlock_page(page);
1895 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001896 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04001897 goto again;
1898 }
Chris Mason247e7432008-07-17 12:53:51 -04001899
Jeff Mahoney87826df2012-02-15 16:23:57 +01001900 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
1901 if (ret) {
1902 mapping_set_error(page->mapping, ret);
1903 end_extent_writepage(page, ret, page_start, page_end);
1904 ClearPageChecked(page);
1905 goto out;
1906 }
1907
Josef Bacik2ac55d42010-02-03 19:33:23 +00001908 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001909 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001910 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04001911out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001912 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1913 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001914out_page:
1915 unlock_page(page);
1916 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08001917 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04001918}
1919
1920/*
1921 * There are a few paths in the higher layers of the kernel that directly
1922 * set the page dirty bit without asking the filesystem if it is a
1923 * good idea. This causes problems because we want to make sure COW
1924 * properly happens and the data=ordered rules are followed.
1925 *
Chris Masonc8b97812008-10-29 14:49:59 -04001926 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001927 * hasn't been properly setup for IO. We kick off an async process
1928 * to fix it up. The async helper will wait for ordered extents, set
1929 * the delalloc bit and make it safe to write the page.
1930 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001931static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001932{
1933 struct inode *inode = page->mapping->host;
1934 struct btrfs_writepage_fixup *fixup;
1935 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001936
Chris Mason8b62b722009-09-02 16:53:46 -04001937 /* this page is properly in the ordered list */
1938 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001939 return 0;
1940
1941 if (PageChecked(page))
1942 return -EAGAIN;
1943
1944 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1945 if (!fixup)
1946 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001947
Chris Mason247e7432008-07-17 12:53:51 -04001948 SetPageChecked(page);
1949 page_cache_get(page);
Liu Bo9e0af232014-08-15 23:36:53 +08001950 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
1951 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04001952 fixup->page = page;
Qu Wenruodc6e3202014-02-28 10:46:14 +08001953 btrfs_queue_work(root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001954 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04001955}
1956
Yan Zhengd899e052008-10-30 14:25:28 -04001957static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1958 struct inode *inode, u64 file_pos,
1959 u64 disk_bytenr, u64 disk_num_bytes,
1960 u64 num_bytes, u64 ram_bytes,
1961 u8 compression, u8 encryption,
1962 u16 other_encoding, int extent_type)
1963{
1964 struct btrfs_root *root = BTRFS_I(inode)->root;
1965 struct btrfs_file_extent_item *fi;
1966 struct btrfs_path *path;
1967 struct extent_buffer *leaf;
1968 struct btrfs_key ins;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00001969 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04001970 int ret;
1971
1972 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07001973 if (!path)
1974 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04001975
Chris Masona1ed8352009-09-11 12:27:37 -04001976 /*
1977 * we may be replacing one extent in the tree with another.
1978 * The new extent is pinned in the extent map, and we don't want
1979 * to drop it from the cache until it is completely in the btree.
1980 *
1981 * So, tell btrfs_drop_extents to leave this extent in the cache.
1982 * the caller is expected to unpin it and allow it to be merged
1983 * with the others.
1984 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00001985 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
1986 file_pos + num_bytes, NULL, 0,
1987 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001988 if (ret)
1989 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001990
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00001991 if (!extent_inserted) {
1992 ins.objectid = btrfs_ino(inode);
1993 ins.offset = file_pos;
1994 ins.type = BTRFS_EXTENT_DATA_KEY;
1995
1996 path->leave_spinning = 1;
1997 ret = btrfs_insert_empty_item(trans, root, path, &ins,
1998 sizeof(*fi));
1999 if (ret)
2000 goto out;
2001 }
Yan Zhengd899e052008-10-30 14:25:28 -04002002 leaf = path->nodes[0];
2003 fi = btrfs_item_ptr(leaf, path->slots[0],
2004 struct btrfs_file_extent_item);
2005 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2006 btrfs_set_file_extent_type(leaf, fi, extent_type);
2007 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2008 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2009 btrfs_set_file_extent_offset(leaf, fi, 0);
2010 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2011 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2012 btrfs_set_file_extent_compression(leaf, fi, compression);
2013 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2014 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002015
Yan Zhengd899e052008-10-30 14:25:28 -04002016 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002017 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002018
2019 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002020
2021 ins.objectid = disk_bytenr;
2022 ins.offset = disk_num_bytes;
2023 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002024 ret = btrfs_alloc_reserved_file_extent(trans, root,
2025 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08002026 btrfs_ino(inode), file_pos, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002027out:
Yan Zhengd899e052008-10-30 14:25:28 -04002028 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002029
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002030 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002031}
2032
Liu Bo38c227d2013-01-29 03:18:40 +00002033/* snapshot-aware defrag */
2034struct sa_defrag_extent_backref {
2035 struct rb_node node;
2036 struct old_sa_defrag_extent *old;
2037 u64 root_id;
2038 u64 inum;
2039 u64 file_pos;
2040 u64 extent_offset;
2041 u64 num_bytes;
2042 u64 generation;
2043};
2044
2045struct old_sa_defrag_extent {
2046 struct list_head list;
2047 struct new_sa_defrag_extent *new;
2048
2049 u64 extent_offset;
2050 u64 bytenr;
2051 u64 offset;
2052 u64 len;
2053 int count;
2054};
2055
2056struct new_sa_defrag_extent {
2057 struct rb_root root;
2058 struct list_head head;
2059 struct btrfs_path *path;
2060 struct inode *inode;
2061 u64 file_pos;
2062 u64 len;
2063 u64 bytenr;
2064 u64 disk_len;
2065 u8 compress_type;
2066};
2067
2068static int backref_comp(struct sa_defrag_extent_backref *b1,
2069 struct sa_defrag_extent_backref *b2)
2070{
2071 if (b1->root_id < b2->root_id)
2072 return -1;
2073 else if (b1->root_id > b2->root_id)
2074 return 1;
2075
2076 if (b1->inum < b2->inum)
2077 return -1;
2078 else if (b1->inum > b2->inum)
2079 return 1;
2080
2081 if (b1->file_pos < b2->file_pos)
2082 return -1;
2083 else if (b1->file_pos > b2->file_pos)
2084 return 1;
2085
2086 /*
2087 * [------------------------------] ===> (a range of space)
2088 * |<--->| |<---->| =============> (fs/file tree A)
2089 * |<---------------------------->| ===> (fs/file tree B)
2090 *
2091 * A range of space can refer to two file extents in one tree while
2092 * refer to only one file extent in another tree.
2093 *
2094 * So we may process a disk offset more than one time(two extents in A)
2095 * and locate at the same extent(one extent in B), then insert two same
2096 * backrefs(both refer to the extent in B).
2097 */
2098 return 0;
2099}
2100
2101static void backref_insert(struct rb_root *root,
2102 struct sa_defrag_extent_backref *backref)
2103{
2104 struct rb_node **p = &root->rb_node;
2105 struct rb_node *parent = NULL;
2106 struct sa_defrag_extent_backref *entry;
2107 int ret;
2108
2109 while (*p) {
2110 parent = *p;
2111 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2112
2113 ret = backref_comp(backref, entry);
2114 if (ret < 0)
2115 p = &(*p)->rb_left;
2116 else
2117 p = &(*p)->rb_right;
2118 }
2119
2120 rb_link_node(&backref->node, parent, p);
2121 rb_insert_color(&backref->node, root);
2122}
2123
2124/*
2125 * Note the backref might has changed, and in this case we just return 0.
2126 */
2127static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2128 void *ctx)
2129{
2130 struct btrfs_file_extent_item *extent;
2131 struct btrfs_fs_info *fs_info;
2132 struct old_sa_defrag_extent *old = ctx;
2133 struct new_sa_defrag_extent *new = old->new;
2134 struct btrfs_path *path = new->path;
2135 struct btrfs_key key;
2136 struct btrfs_root *root;
2137 struct sa_defrag_extent_backref *backref;
2138 struct extent_buffer *leaf;
2139 struct inode *inode = new->inode;
2140 int slot;
2141 int ret;
2142 u64 extent_offset;
2143 u64 num_bytes;
2144
2145 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
2146 inum == btrfs_ino(inode))
2147 return 0;
2148
2149 key.objectid = root_id;
2150 key.type = BTRFS_ROOT_ITEM_KEY;
2151 key.offset = (u64)-1;
2152
2153 fs_info = BTRFS_I(inode)->root->fs_info;
2154 root = btrfs_read_fs_root_no_name(fs_info, &key);
2155 if (IS_ERR(root)) {
2156 if (PTR_ERR(root) == -ENOENT)
2157 return 0;
2158 WARN_ON(1);
2159 pr_debug("inum=%llu, offset=%llu, root_id=%llu\n",
2160 inum, offset, root_id);
2161 return PTR_ERR(root);
2162 }
2163
2164 key.objectid = inum;
2165 key.type = BTRFS_EXTENT_DATA_KEY;
2166 if (offset > (u64)-1 << 32)
2167 key.offset = 0;
2168 else
2169 key.offset = offset;
2170
2171 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302172 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002173 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002174 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002175
2176 while (1) {
2177 cond_resched();
2178
2179 leaf = path->nodes[0];
2180 slot = path->slots[0];
2181
2182 if (slot >= btrfs_header_nritems(leaf)) {
2183 ret = btrfs_next_leaf(root, path);
2184 if (ret < 0) {
2185 goto out;
2186 } else if (ret > 0) {
2187 ret = 0;
2188 goto out;
2189 }
2190 continue;
2191 }
2192
2193 path->slots[0]++;
2194
2195 btrfs_item_key_to_cpu(leaf, &key, slot);
2196
2197 if (key.objectid > inum)
2198 goto out;
2199
2200 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2201 continue;
2202
2203 extent = btrfs_item_ptr(leaf, slot,
2204 struct btrfs_file_extent_item);
2205
2206 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2207 continue;
2208
Liu Boe68afa42013-07-01 22:13:26 +08002209 /*
2210 * 'offset' refers to the exact key.offset,
2211 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2212 * (key.offset - extent_offset).
2213 */
2214 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002215 continue;
2216
Liu Boe68afa42013-07-01 22:13:26 +08002217 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002218 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002219
Liu Bo38c227d2013-01-29 03:18:40 +00002220 if (extent_offset >= old->extent_offset + old->offset +
2221 old->len || extent_offset + num_bytes <=
2222 old->extent_offset + old->offset)
2223 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002224 break;
2225 }
2226
2227 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2228 if (!backref) {
2229 ret = -ENOENT;
2230 goto out;
2231 }
2232
2233 backref->root_id = root_id;
2234 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002235 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002236 backref->num_bytes = num_bytes;
2237 backref->extent_offset = extent_offset;
2238 backref->generation = btrfs_file_extent_generation(leaf, extent);
2239 backref->old = old;
2240 backref_insert(&new->root, backref);
2241 old->count++;
2242out:
2243 btrfs_release_path(path);
2244 WARN_ON(ret);
2245 return ret;
2246}
2247
2248static noinline bool record_extent_backrefs(struct btrfs_path *path,
2249 struct new_sa_defrag_extent *new)
2250{
2251 struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info;
2252 struct old_sa_defrag_extent *old, *tmp;
2253 int ret;
2254
2255 new->path = path;
2256
2257 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002258 ret = iterate_inodes_from_logical(old->bytenr +
2259 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002260 path, record_one_backref,
2261 old);
Josef Bacik4724b102013-11-05 11:11:40 -05002262 if (ret < 0 && ret != -ENOENT)
2263 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002264
2265 /* no backref to be processed for this extent */
2266 if (!old->count) {
2267 list_del(&old->list);
2268 kfree(old);
2269 }
2270 }
2271
2272 if (list_empty(&new->head))
2273 return false;
2274
2275 return true;
2276}
2277
2278static int relink_is_mergable(struct extent_buffer *leaf,
2279 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002280 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002281{
Liu Bo116e0022013-08-02 16:30:40 +08002282 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002283 return 0;
2284
2285 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2286 return 0;
2287
Liu Bo116e0022013-08-02 16:30:40 +08002288 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2289 return 0;
2290
2291 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002292 btrfs_file_extent_other_encoding(leaf, fi))
2293 return 0;
2294
2295 return 1;
2296}
2297
2298/*
2299 * Note the backref might has changed, and in this case we just return 0.
2300 */
2301static noinline int relink_extent_backref(struct btrfs_path *path,
2302 struct sa_defrag_extent_backref *prev,
2303 struct sa_defrag_extent_backref *backref)
2304{
2305 struct btrfs_file_extent_item *extent;
2306 struct btrfs_file_extent_item *item;
2307 struct btrfs_ordered_extent *ordered;
2308 struct btrfs_trans_handle *trans;
2309 struct btrfs_fs_info *fs_info;
2310 struct btrfs_root *root;
2311 struct btrfs_key key;
2312 struct extent_buffer *leaf;
2313 struct old_sa_defrag_extent *old = backref->old;
2314 struct new_sa_defrag_extent *new = old->new;
2315 struct inode *src_inode = new->inode;
2316 struct inode *inode;
2317 struct extent_state *cached = NULL;
2318 int ret = 0;
2319 u64 start;
2320 u64 len;
2321 u64 lock_start;
2322 u64 lock_end;
2323 bool merge = false;
2324 int index;
2325
2326 if (prev && prev->root_id == backref->root_id &&
2327 prev->inum == backref->inum &&
2328 prev->file_pos + prev->num_bytes == backref->file_pos)
2329 merge = true;
2330
2331 /* step 1: get root */
2332 key.objectid = backref->root_id;
2333 key.type = BTRFS_ROOT_ITEM_KEY;
2334 key.offset = (u64)-1;
2335
2336 fs_info = BTRFS_I(src_inode)->root->fs_info;
2337 index = srcu_read_lock(&fs_info->subvol_srcu);
2338
2339 root = btrfs_read_fs_root_no_name(fs_info, &key);
2340 if (IS_ERR(root)) {
2341 srcu_read_unlock(&fs_info->subvol_srcu, index);
2342 if (PTR_ERR(root) == -ENOENT)
2343 return 0;
2344 return PTR_ERR(root);
2345 }
Liu Bo38c227d2013-01-29 03:18:40 +00002346
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002347 if (btrfs_root_readonly(root)) {
2348 srcu_read_unlock(&fs_info->subvol_srcu, index);
2349 return 0;
2350 }
2351
Liu Bo38c227d2013-01-29 03:18:40 +00002352 /* step 2: get inode */
2353 key.objectid = backref->inum;
2354 key.type = BTRFS_INODE_ITEM_KEY;
2355 key.offset = 0;
2356
2357 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2358 if (IS_ERR(inode)) {
2359 srcu_read_unlock(&fs_info->subvol_srcu, index);
2360 return 0;
2361 }
2362
2363 srcu_read_unlock(&fs_info->subvol_srcu, index);
2364
2365 /* step 3: relink backref */
2366 lock_start = backref->file_pos;
2367 lock_end = backref->file_pos + backref->num_bytes - 1;
2368 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2369 0, &cached);
2370
2371 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2372 if (ordered) {
2373 btrfs_put_ordered_extent(ordered);
2374 goto out_unlock;
2375 }
2376
2377 trans = btrfs_join_transaction(root);
2378 if (IS_ERR(trans)) {
2379 ret = PTR_ERR(trans);
2380 goto out_unlock;
2381 }
2382
2383 key.objectid = backref->inum;
2384 key.type = BTRFS_EXTENT_DATA_KEY;
2385 key.offset = backref->file_pos;
2386
2387 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2388 if (ret < 0) {
2389 goto out_free_path;
2390 } else if (ret > 0) {
2391 ret = 0;
2392 goto out_free_path;
2393 }
2394
2395 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2396 struct btrfs_file_extent_item);
2397
2398 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2399 backref->generation)
2400 goto out_free_path;
2401
2402 btrfs_release_path(path);
2403
2404 start = backref->file_pos;
2405 if (backref->extent_offset < old->extent_offset + old->offset)
2406 start += old->extent_offset + old->offset -
2407 backref->extent_offset;
2408
2409 len = min(backref->extent_offset + backref->num_bytes,
2410 old->extent_offset + old->offset + old->len);
2411 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2412
2413 ret = btrfs_drop_extents(trans, root, inode, start,
2414 start + len, 1);
2415 if (ret)
2416 goto out_free_path;
2417again:
2418 key.objectid = btrfs_ino(inode);
2419 key.type = BTRFS_EXTENT_DATA_KEY;
2420 key.offset = start;
2421
Liu Boa09a0a72013-03-11 09:20:58 +00002422 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002423 if (merge) {
2424 struct btrfs_file_extent_item *fi;
2425 u64 extent_len;
2426 struct btrfs_key found_key;
2427
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002428 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002429 if (ret < 0)
2430 goto out_free_path;
2431
2432 path->slots[0]--;
2433 leaf = path->nodes[0];
2434 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2435
2436 fi = btrfs_item_ptr(leaf, path->slots[0],
2437 struct btrfs_file_extent_item);
2438 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2439
Liu Bo116e0022013-08-02 16:30:40 +08002440 if (extent_len + found_key.offset == start &&
2441 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002442 btrfs_set_file_extent_num_bytes(leaf, fi,
2443 extent_len + len);
2444 btrfs_mark_buffer_dirty(leaf);
2445 inode_add_bytes(inode, len);
2446
2447 ret = 1;
2448 goto out_free_path;
2449 } else {
2450 merge = false;
2451 btrfs_release_path(path);
2452 goto again;
2453 }
2454 }
2455
2456 ret = btrfs_insert_empty_item(trans, root, path, &key,
2457 sizeof(*extent));
2458 if (ret) {
2459 btrfs_abort_transaction(trans, root, ret);
2460 goto out_free_path;
2461 }
2462
2463 leaf = path->nodes[0];
2464 item = btrfs_item_ptr(leaf, path->slots[0],
2465 struct btrfs_file_extent_item);
2466 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2467 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2468 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2469 btrfs_set_file_extent_num_bytes(leaf, item, len);
2470 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2471 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2472 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2473 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2474 btrfs_set_file_extent_encryption(leaf, item, 0);
2475 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2476
2477 btrfs_mark_buffer_dirty(leaf);
2478 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002479 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002480
2481 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
2482 new->disk_len, 0,
2483 backref->root_id, backref->inum,
2484 new->file_pos, 0); /* start - extent_offset */
2485 if (ret) {
2486 btrfs_abort_transaction(trans, root, ret);
2487 goto out_free_path;
2488 }
2489
2490 ret = 1;
2491out_free_path:
2492 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002493 path->leave_spinning = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002494 btrfs_end_transaction(trans, root);
2495out_unlock:
2496 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2497 &cached, GFP_NOFS);
2498 iput(inode);
2499 return ret;
2500}
2501
Liu Bo6f519562013-10-29 10:45:05 +08002502static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2503{
2504 struct old_sa_defrag_extent *old, *tmp;
2505
2506 if (!new)
2507 return;
2508
2509 list_for_each_entry_safe(old, tmp, &new->head, list) {
2510 list_del(&old->list);
2511 kfree(old);
2512 }
2513 kfree(new);
2514}
2515
Liu Bo38c227d2013-01-29 03:18:40 +00002516static void relink_file_extents(struct new_sa_defrag_extent *new)
2517{
2518 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002519 struct sa_defrag_extent_backref *backref;
2520 struct sa_defrag_extent_backref *prev = NULL;
2521 struct inode *inode;
2522 struct btrfs_root *root;
2523 struct rb_node *node;
2524 int ret;
2525
2526 inode = new->inode;
2527 root = BTRFS_I(inode)->root;
2528
2529 path = btrfs_alloc_path();
2530 if (!path)
2531 return;
2532
2533 if (!record_extent_backrefs(path, new)) {
2534 btrfs_free_path(path);
2535 goto out;
2536 }
2537 btrfs_release_path(path);
2538
2539 while (1) {
2540 node = rb_first(&new->root);
2541 if (!node)
2542 break;
2543 rb_erase(node, &new->root);
2544
2545 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2546
2547 ret = relink_extent_backref(path, prev, backref);
2548 WARN_ON(ret < 0);
2549
2550 kfree(prev);
2551
2552 if (ret == 1)
2553 prev = backref;
2554 else
2555 prev = NULL;
2556 cond_resched();
2557 }
2558 kfree(prev);
2559
2560 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002561out:
Liu Bo6f519562013-10-29 10:45:05 +08002562 free_sa_defrag_extent(new);
2563
Liu Bo38c227d2013-01-29 03:18:40 +00002564 atomic_dec(&root->fs_info->defrag_running);
2565 wake_up(&root->fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002566}
2567
2568static struct new_sa_defrag_extent *
2569record_old_file_extents(struct inode *inode,
2570 struct btrfs_ordered_extent *ordered)
2571{
2572 struct btrfs_root *root = BTRFS_I(inode)->root;
2573 struct btrfs_path *path;
2574 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002575 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002576 struct new_sa_defrag_extent *new;
2577 int ret;
2578
2579 new = kmalloc(sizeof(*new), GFP_NOFS);
2580 if (!new)
2581 return NULL;
2582
2583 new->inode = inode;
2584 new->file_pos = ordered->file_offset;
2585 new->len = ordered->len;
2586 new->bytenr = ordered->start;
2587 new->disk_len = ordered->disk_len;
2588 new->compress_type = ordered->compress_type;
2589 new->root = RB_ROOT;
2590 INIT_LIST_HEAD(&new->head);
2591
2592 path = btrfs_alloc_path();
2593 if (!path)
2594 goto out_kfree;
2595
2596 key.objectid = btrfs_ino(inode);
2597 key.type = BTRFS_EXTENT_DATA_KEY;
2598 key.offset = new->file_pos;
2599
2600 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2601 if (ret < 0)
2602 goto out_free_path;
2603 if (ret > 0 && path->slots[0] > 0)
2604 path->slots[0]--;
2605
2606 /* find out all the old extents for the file range */
2607 while (1) {
2608 struct btrfs_file_extent_item *extent;
2609 struct extent_buffer *l;
2610 int slot;
2611 u64 num_bytes;
2612 u64 offset;
2613 u64 end;
2614 u64 disk_bytenr;
2615 u64 extent_offset;
2616
2617 l = path->nodes[0];
2618 slot = path->slots[0];
2619
2620 if (slot >= btrfs_header_nritems(l)) {
2621 ret = btrfs_next_leaf(root, path);
2622 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002623 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002624 else if (ret > 0)
2625 break;
2626 continue;
2627 }
2628
2629 btrfs_item_key_to_cpu(l, &key, slot);
2630
2631 if (key.objectid != btrfs_ino(inode))
2632 break;
2633 if (key.type != BTRFS_EXTENT_DATA_KEY)
2634 break;
2635 if (key.offset >= new->file_pos + new->len)
2636 break;
2637
2638 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2639
2640 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2641 if (key.offset + num_bytes < new->file_pos)
2642 goto next;
2643
2644 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2645 if (!disk_bytenr)
2646 goto next;
2647
2648 extent_offset = btrfs_file_extent_offset(l, extent);
2649
2650 old = kmalloc(sizeof(*old), GFP_NOFS);
2651 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002652 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002653
2654 offset = max(new->file_pos, key.offset);
2655 end = min(new->file_pos + new->len, key.offset + num_bytes);
2656
2657 old->bytenr = disk_bytenr;
2658 old->extent_offset = extent_offset;
2659 old->offset = offset - key.offset;
2660 old->len = end - offset;
2661 old->new = new;
2662 old->count = 0;
2663 list_add_tail(&old->list, &new->head);
2664next:
2665 path->slots[0]++;
2666 cond_resched();
2667 }
2668
2669 btrfs_free_path(path);
2670 atomic_inc(&root->fs_info->defrag_running);
2671
2672 return new;
2673
Liu Bo38c227d2013-01-29 03:18:40 +00002674out_free_path:
2675 btrfs_free_path(path);
2676out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002677 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002678 return NULL;
2679}
2680
Miao Xiee570fd22014-06-19 10:42:50 +08002681static void btrfs_release_delalloc_bytes(struct btrfs_root *root,
2682 u64 start, u64 len)
2683{
2684 struct btrfs_block_group_cache *cache;
2685
2686 cache = btrfs_lookup_block_group(root->fs_info, start);
2687 ASSERT(cache);
2688
2689 spin_lock(&cache->lock);
2690 cache->delalloc_bytes -= len;
2691 spin_unlock(&cache->lock);
2692
2693 btrfs_put_block_group(cache);
2694}
2695
Chris Masond352ac62008-09-29 15:18:18 -04002696/* as ordered data IO finishes, this gets called so we can finish
2697 * an ordered extent if the range of bytes in the file it covers are
2698 * fully written.
2699 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002700static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002701{
Josef Bacik5fd02042012-05-02 14:00:54 -04002702 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002703 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002704 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002705 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002706 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002707 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002708 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002709 int ret = 0;
2710 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002711 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002712 bool truncated = false;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002713
Liu Bo83eea1f2012-07-10 05:28:39 -06002714 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002715
Josef Bacik5fd02042012-05-02 14:00:54 -04002716 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2717 ret = -EIO;
2718 goto out;
2719 }
2720
Miao Xief6124962014-09-12 18:44:04 +08002721 btrfs_free_io_failure_record(inode, ordered_extent->file_offset,
2722 ordered_extent->file_offset +
2723 ordered_extent->len - 1);
2724
Josef Bacik77cef2e2013-08-29 13:57:21 -04002725 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2726 truncated = true;
2727 logical_len = ordered_extent->truncated_len;
2728 /* Truncated the entire extent, don't bother adding */
2729 if (!logical_len)
2730 goto out;
2731 }
2732
Yan, Zhengc2167752009-11-12 09:34:21 +00002733 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002734 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Josef Bacik6c760c02012-11-09 10:53:21 -05002735 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2736 if (nolock)
2737 trans = btrfs_join_transaction_nolock(root);
2738 else
2739 trans = btrfs_join_transaction(root);
2740 if (IS_ERR(trans)) {
2741 ret = PTR_ERR(trans);
2742 trans = NULL;
2743 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002744 }
Josef Bacik6c760c02012-11-09 10:53:21 -05002745 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
2746 ret = btrfs_update_inode_fallback(trans, root, inode);
2747 if (ret) /* -ENOMEM or corruption */
2748 btrfs_abort_transaction(trans, root, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002749 goto out;
2750 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002751
Josef Bacik2ac55d42010-02-03 19:33:23 +00002752 lock_extent_bits(io_tree, ordered_extent->file_offset,
2753 ordered_extent->file_offset + ordered_extent->len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002754 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002755
Liu Bo38c227d2013-01-29 03:18:40 +00002756 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2757 ordered_extent->file_offset + ordered_extent->len - 1,
2758 EXTENT_DEFRAG, 1, cached_state);
2759 if (ret) {
2760 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05002761 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00002762 /* the inode is shared */
2763 new = record_old_file_extents(inode, ordered_extent);
2764
2765 clear_extent_bit(io_tree, ordered_extent->file_offset,
2766 ordered_extent->file_offset + ordered_extent->len - 1,
2767 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2768 }
2769
Josef Bacik0cb59c92010-07-02 12:14:14 -04002770 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002771 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002772 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002773 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002774 if (IS_ERR(trans)) {
2775 ret = PTR_ERR(trans);
2776 trans = NULL;
2777 goto out_unlock;
2778 }
Chris Masona79b7d42014-05-22 16:18:52 -07002779
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002780 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002781
Chris Masonc8b97812008-10-29 14:49:59 -04002782 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002783 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002784 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002785 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00002786 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002787 ordered_extent->file_offset,
2788 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002789 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002790 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04002791 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002792 ret = insert_reserved_file_extent(trans, inode,
2793 ordered_extent->file_offset,
2794 ordered_extent->start,
2795 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04002796 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08002797 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002798 BTRFS_FILE_EXTENT_REG);
Miao Xiee570fd22014-06-19 10:42:50 +08002799 if (!ret)
2800 btrfs_release_delalloc_bytes(root,
2801 ordered_extent->start,
2802 ordered_extent->disk_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002803 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002804 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2805 ordered_extent->file_offset, ordered_extent->len,
2806 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002807 if (ret < 0) {
2808 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002809 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002810 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002811
Chris Masone6dcd2d2008-07-17 12:53:50 -04002812 add_pending_csums(trans, inode, ordered_extent->file_offset,
2813 &ordered_extent->list);
2814
Josef Bacik6c760c02012-11-09 10:53:21 -05002815 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2816 ret = btrfs_update_inode_fallback(trans, root, inode);
2817 if (ret) { /* -ENOMEM or corruption */
2818 btrfs_abort_transaction(trans, root, ret);
2819 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002820 }
2821 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002822out_unlock:
2823 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2824 ordered_extent->file_offset +
2825 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00002826out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002827 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04002828 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002829 if (trans)
2830 btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002831
Josef Bacik77cef2e2013-08-29 13:57:21 -04002832 if (ret || truncated) {
2833 u64 start, end;
2834
2835 if (truncated)
2836 start = ordered_extent->file_offset + logical_len;
2837 else
2838 start = ordered_extent->file_offset;
2839 end = ordered_extent->file_offset + ordered_extent->len - 1;
2840 clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS);
2841
2842 /* Drop the cache for the part of the extent we didn't write. */
2843 btrfs_drop_extent_cache(inode, start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04002844
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002845 /*
2846 * If the ordered extent had an IOERR or something else went
2847 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04002848 * back to the allocator. We only free the extent in the
2849 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002850 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04002851 if ((ret || !logical_len) &&
2852 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002853 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
2854 btrfs_free_reserved_extent(root, ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08002855 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002856 }
2857
2858
Josef Bacik5fd02042012-05-02 14:00:54 -04002859 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002860 * This needs to be done to make sure anybody waiting knows we are done
2861 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002862 */
2863 btrfs_remove_ordered_extent(inode, ordered_extent);
2864
Liu Bo38c227d2013-01-29 03:18:40 +00002865 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08002866 if (new) {
2867 if (ret) {
2868 free_sa_defrag_extent(new);
2869 atomic_dec(&root->fs_info->defrag_running);
2870 } else {
2871 relink_file_extents(new);
2872 }
2873 }
Liu Bo38c227d2013-01-29 03:18:40 +00002874
Chris Masone6dcd2d2008-07-17 12:53:50 -04002875 /* once for us */
2876 btrfs_put_ordered_extent(ordered_extent);
2877 /* once for the tree */
2878 btrfs_put_ordered_extent(ordered_extent);
2879
Josef Bacik5fd02042012-05-02 14:00:54 -04002880 return ret;
2881}
2882
2883static void finish_ordered_fn(struct btrfs_work *work)
2884{
2885 struct btrfs_ordered_extent *ordered_extent;
2886 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2887 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002888}
2889
Christoph Hellwigb2950862008-12-02 09:54:17 -05002890static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04002891 struct extent_state *state, int uptodate)
2892{
Josef Bacik5fd02042012-05-02 14:00:54 -04002893 struct inode *inode = page->mapping->host;
2894 struct btrfs_root *root = BTRFS_I(inode)->root;
2895 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08002896 struct btrfs_workqueue *wq;
2897 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04002898
liubo1abe9b82011-03-24 11:18:59 +00002899 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2900
Chris Mason8b62b722009-09-02 16:53:46 -04002901 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002902 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2903 end - start + 1, uptodate))
2904 return 0;
2905
Liu Bo9e0af232014-08-15 23:36:53 +08002906 if (btrfs_is_free_space_inode(inode)) {
2907 wq = root->fs_info->endio_freespace_worker;
2908 func = btrfs_freespace_write_helper;
2909 } else {
2910 wq = root->fs_info->endio_write_workers;
2911 func = btrfs_endio_write_helper;
2912 }
Josef Bacik5fd02042012-05-02 14:00:54 -04002913
Liu Bo9e0af232014-08-15 23:36:53 +08002914 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
2915 NULL);
2916 btrfs_queue_work(wq, &ordered_extent->work);
Josef Bacik5fd02042012-05-02 14:00:54 -04002917
2918 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04002919}
2920
Miao Xiedc380ae2014-09-12 18:43:55 +08002921static int __readpage_endio_check(struct inode *inode,
2922 struct btrfs_io_bio *io_bio,
2923 int icsum, struct page *page,
2924 int pgoff, u64 start, size_t len)
2925{
2926 char *kaddr;
2927 u32 csum_expected;
2928 u32 csum = ~(u32)0;
2929 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
2930 DEFAULT_RATELIMIT_BURST);
2931
2932 csum_expected = *(((u32 *)io_bio->csum) + icsum);
2933
2934 kaddr = kmap_atomic(page);
2935 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
2936 btrfs_csum_final(csum, (char *)&csum);
2937 if (csum != csum_expected)
2938 goto zeroit;
2939
2940 kunmap_atomic(kaddr);
2941 return 0;
2942zeroit:
2943 if (__ratelimit(&_rs))
2944 btrfs_info(BTRFS_I(inode)->root->fs_info,
2945 "csum failed ino %llu off %llu csum %u expected csum %u",
2946 btrfs_ino(inode), start, csum, csum_expected);
2947 memset(kaddr + pgoff, 1, len);
2948 flush_dcache_page(page);
2949 kunmap_atomic(kaddr);
2950 if (csum_expected == 0)
2951 return 0;
2952 return -EIO;
2953}
2954
Chris Masond352ac62008-09-29 15:18:18 -04002955/*
Chris Masond352ac62008-09-29 15:18:18 -04002956 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002957 * if there's a match, we allow the bio to finish. If not, the code in
2958 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002959 */
Miao Xiefacc8a222013-07-25 19:22:34 +08002960static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
2961 u64 phy_offset, struct page *page,
2962 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002963{
Miao Xie4eee4fa2012-12-21 09:17:45 +00002964 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04002965 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002966 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04002967 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05002968
Chris Masond20f7042008-12-08 16:58:54 -05002969 if (PageChecked(page)) {
2970 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08002971 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05002972 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002973
2974 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08002975 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05002976
Yan Zheng17d217f2008-12-12 10:03:38 -05002977 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002978 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002979 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
2980 GFP_NOFS);
2981 return 0;
2982 }
2983
Miao Xiefacc8a222013-07-25 19:22:34 +08002984 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08002985 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
2986 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04002987}
Chris Masonb888db2b2007-08-27 16:49:44 -04002988
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002989struct delayed_iput {
2990 struct list_head list;
2991 struct inode *inode;
2992};
2993
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002994/* JDM: If this is fs-wide, why can't we add a pointer to
2995 * btrfs_inode instead and avoid the allocation? */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002996void btrfs_add_delayed_iput(struct inode *inode)
2997{
2998 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2999 struct delayed_iput *delayed;
3000
3001 if (atomic_add_unless(&inode->i_count, -1, 1))
3002 return;
3003
3004 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
3005 delayed->inode = inode;
3006
3007 spin_lock(&fs_info->delayed_iput_lock);
3008 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
3009 spin_unlock(&fs_info->delayed_iput_lock);
3010}
3011
3012void btrfs_run_delayed_iputs(struct btrfs_root *root)
3013{
3014 LIST_HEAD(list);
3015 struct btrfs_fs_info *fs_info = root->fs_info;
3016 struct delayed_iput *delayed;
3017 int empty;
3018
3019 spin_lock(&fs_info->delayed_iput_lock);
3020 empty = list_empty(&fs_info->delayed_iputs);
3021 spin_unlock(&fs_info->delayed_iput_lock);
3022 if (empty)
3023 return;
3024
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003025 spin_lock(&fs_info->delayed_iput_lock);
3026 list_splice_init(&fs_info->delayed_iputs, &list);
3027 spin_unlock(&fs_info->delayed_iput_lock);
3028
3029 while (!list_empty(&list)) {
3030 delayed = list_entry(list.next, struct delayed_iput, list);
3031 list_del(&delayed->list);
3032 iput(delayed->inode);
3033 kfree(delayed);
3034 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003035}
3036
Yan, Zhengd68fc572010-05-16 10:49:58 -04003037/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08003038 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04003039 * files in the subvolume, it removes orphan item and frees block_rsv
3040 * structure.
3041 */
3042void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3043 struct btrfs_root *root)
3044{
Josef Bacik90290e12011-12-02 15:44:12 -05003045 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003046 int ret;
3047
Josef Bacik8a35d952012-05-23 14:26:42 -04003048 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04003049 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
3050 return;
3051
Josef Bacik90290e12011-12-02 15:44:12 -05003052 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003053 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05003054 spin_unlock(&root->orphan_lock);
3055 return;
3056 }
3057
3058 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
3059 spin_unlock(&root->orphan_lock);
3060 return;
3061 }
3062
3063 block_rsv = root->orphan_block_rsv;
3064 root->orphan_block_rsv = NULL;
3065 spin_unlock(&root->orphan_lock);
3066
Miao Xie27cdeb72014-04-02 19:51:05 +08003067 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
Yan, Zhengd68fc572010-05-16 10:49:58 -04003068 btrfs_root_refs(&root->root_item) > 0) {
3069 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
3070 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003071 if (ret)
3072 btrfs_abort_transaction(trans, root, ret);
3073 else
Miao Xie27cdeb72014-04-02 19:51:05 +08003074 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
3075 &root->state);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003076 }
3077
Josef Bacik90290e12011-12-02 15:44:12 -05003078 if (block_rsv) {
3079 WARN_ON(block_rsv->size > 0);
3080 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003081 }
3082}
3083
3084/*
Josef Bacik7b128762008-07-24 12:17:14 -04003085 * This creates an orphan entry for the given inode in case something goes
3086 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003087 *
3088 * NOTE: caller of this function should reserve 5 units of metadata for
3089 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04003090 */
3091int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
3092{
3093 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003094 struct btrfs_block_rsv *block_rsv = NULL;
3095 int reserve = 0;
3096 int insert = 0;
3097 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003098
Yan, Zhengd68fc572010-05-16 10:49:58 -04003099 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06003100 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003101 if (!block_rsv)
3102 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04003103 }
3104
Yan, Zhengd68fc572010-05-16 10:49:58 -04003105 spin_lock(&root->orphan_lock);
3106 if (!root->orphan_block_rsv) {
3107 root->orphan_block_rsv = block_rsv;
3108 } else if (block_rsv) {
3109 btrfs_free_block_rsv(root, block_rsv);
3110 block_rsv = NULL;
3111 }
Josef Bacik7b128762008-07-24 12:17:14 -04003112
Josef Bacik8a35d952012-05-23 14:26:42 -04003113 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3114 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003115#if 0
3116 /*
3117 * For proper ENOSPC handling, we should do orphan
3118 * cleanup when mounting. But this introduces backward
3119 * compatibility issue.
3120 */
3121 if (!xchg(&root->orphan_item_inserted, 1))
3122 insert = 2;
3123 else
3124 insert = 1;
3125#endif
3126 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003127 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003128 }
Josef Bacik7b128762008-07-24 12:17:14 -04003129
Josef Bacik72ac3c02012-05-23 14:13:11 -04003130 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3131 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003132 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003133 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003134
Yan, Zhengd68fc572010-05-16 10:49:58 -04003135 /* grab metadata reservation from transaction handle */
3136 if (reserve) {
3137 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003138 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04003139 }
3140
3141 /* insert an orphan item to track this unlinked/truncated file */
3142 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08003143 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003144 if (ret) {
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003145 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003146 if (reserve) {
3147 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3148 &BTRFS_I(inode)->runtime_flags);
3149 btrfs_orphan_release_metadata(inode);
3150 }
3151 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003152 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3153 &BTRFS_I(inode)->runtime_flags);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003154 btrfs_abort_transaction(trans, root, ret);
3155 return ret;
3156 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003157 }
3158 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003159 }
3160
3161 /* insert an orphan item to track subvolume contains orphan files */
3162 if (insert >= 2) {
3163 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
3164 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003165 if (ret && ret != -EEXIST) {
3166 btrfs_abort_transaction(trans, root, ret);
3167 return ret;
3168 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003169 }
3170 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003171}
3172
3173/*
3174 * We have done the truncate/delete so we can go ahead and remove the orphan
3175 * item for this particular inode.
3176 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003177static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3178 struct inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003179{
3180 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003181 int delete_item = 0;
3182 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003183 int ret = 0;
3184
Yan, Zhengd68fc572010-05-16 10:49:58 -04003185 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003186 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3187 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003188 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003189
Josef Bacik72ac3c02012-05-23 14:13:11 -04003190 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3191 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003192 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003193 spin_unlock(&root->orphan_lock);
3194
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003195 if (delete_item) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003196 atomic_dec(&root->orphan_inodes);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003197 if (trans)
3198 ret = btrfs_del_orphan_item(trans, root,
3199 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04003200 }
Josef Bacik7b128762008-07-24 12:17:14 -04003201
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003202 if (release_rsv)
3203 btrfs_orphan_release_metadata(inode);
3204
Josef Bacik4ef31a42013-08-13 14:10:08 -04003205 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003206}
3207
3208/*
3209 * this cleans up any orphans that may be left on the list from the last use
3210 * of this root.
3211 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003212int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003213{
3214 struct btrfs_path *path;
3215 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003216 struct btrfs_key key, found_key;
3217 struct btrfs_trans_handle *trans;
3218 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003219 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003220 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3221
Yan, Zhengd68fc572010-05-16 10:49:58 -04003222 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003223 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003224
3225 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003226 if (!path) {
3227 ret = -ENOMEM;
3228 goto out;
3229 }
Josef Bacik7b128762008-07-24 12:17:14 -04003230 path->reada = -1;
3231
3232 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003233 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003234 key.offset = (u64)-1;
3235
Josef Bacik7b128762008-07-24 12:17:14 -04003236 while (1) {
3237 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003238 if (ret < 0)
3239 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003240
3241 /*
3242 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003243 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003244 * find the key and see if we have stuff that matches
3245 */
3246 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003247 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003248 if (path->slots[0] == 0)
3249 break;
3250 path->slots[0]--;
3251 }
3252
3253 /* pull out the item */
3254 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003255 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3256
3257 /* make sure the item matches what we want */
3258 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3259 break;
David Sterba962a2982014-06-04 18:41:45 +02003260 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003261 break;
3262
3263 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003264 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003265
3266 /*
3267 * this is where we are basically btrfs_lookup, without the
3268 * crossing root thing. we store the inode number in the
3269 * offset of the orphan item.
3270 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003271
3272 if (found_key.offset == last_objectid) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003273 btrfs_err(root->fs_info,
3274 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003275 ret = -EINVAL;
3276 goto out;
3277 }
3278
3279 last_objectid = found_key.offset;
3280
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003281 found_key.objectid = found_key.offset;
3282 found_key.type = BTRFS_INODE_ITEM_KEY;
3283 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00003284 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303285 ret = PTR_ERR_OR_ZERO(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003286 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003287 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003288
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003289 if (ret == -ESTALE && root == root->fs_info->tree_root) {
3290 struct btrfs_root *dead_root;
3291 struct btrfs_fs_info *fs_info = root->fs_info;
3292 int is_dead_root = 0;
3293
3294 /*
3295 * this is an orphan in the tree root. Currently these
3296 * could come from 2 sources:
3297 * a) a snapshot deletion in progress
3298 * b) a free space cache inode
3299 * We need to distinguish those two, as the snapshot
3300 * orphan must not get deleted.
3301 * find_dead_roots already ran before us, so if this
3302 * is a snapshot deletion, we should find the root
3303 * in the dead_roots list
3304 */
3305 spin_lock(&fs_info->trans_lock);
3306 list_for_each_entry(dead_root, &fs_info->dead_roots,
3307 root_list) {
3308 if (dead_root->root_key.objectid ==
3309 found_key.objectid) {
3310 is_dead_root = 1;
3311 break;
3312 }
3313 }
3314 spin_unlock(&fs_info->trans_lock);
3315 if (is_dead_root) {
3316 /* prevent this orphan from being found again */
3317 key.offset = found_key.objectid - 1;
3318 continue;
3319 }
3320 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003321 /*
3322 * Inode is already gone but the orphan item is still there,
3323 * kill the orphan item.
3324 */
3325 if (ret == -ESTALE) {
3326 trans = btrfs_start_transaction(root, 1);
3327 if (IS_ERR(trans)) {
3328 ret = PTR_ERR(trans);
3329 goto out;
3330 }
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003331 btrfs_debug(root->fs_info, "auto deleting %Lu",
3332 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003333 ret = btrfs_del_orphan_item(trans, root,
3334 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003335 btrfs_end_transaction(trans, root);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003336 if (ret)
3337 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003338 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003339 }
Josef Bacik7b128762008-07-24 12:17:14 -04003340
Josef Bacik7b128762008-07-24 12:17:14 -04003341 /*
3342 * add this inode to the orphan list so btrfs_orphan_del does
3343 * the proper thing when we hit it
3344 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003345 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3346 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003347 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003348
Josef Bacik7b128762008-07-24 12:17:14 -04003349 /* if we have links, this was a truncate, lets do that */
3350 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303351 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003352 iput(inode);
3353 continue;
3354 }
Josef Bacik7b128762008-07-24 12:17:14 -04003355 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003356
3357 /* 1 for the orphan item deletion. */
3358 trans = btrfs_start_transaction(root, 1);
3359 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003360 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003361 ret = PTR_ERR(trans);
3362 goto out;
3363 }
3364 ret = btrfs_orphan_add(trans, inode);
3365 btrfs_end_transaction(trans, root);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003366 if (ret) {
3367 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003368 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003369 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003370
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003371 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003372 if (ret)
3373 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003374 } else {
3375 nr_unlink++;
3376 }
3377
3378 /* this will do delete_inode and everything for us */
3379 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003380 if (ret)
3381 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003382 }
Miao Xie3254c872011-11-10 20:45:05 -05003383 /* release the path since we're done with it */
3384 btrfs_release_path(path);
3385
Yan, Zhengd68fc572010-05-16 10:49:58 -04003386 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3387
3388 if (root->orphan_block_rsv)
3389 btrfs_block_rsv_release(root, root->orphan_block_rsv,
3390 (u64)-1);
3391
Miao Xie27cdeb72014-04-02 19:51:05 +08003392 if (root->orphan_block_rsv ||
3393 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003394 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003395 if (!IS_ERR(trans))
3396 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003397 }
Josef Bacik7b128762008-07-24 12:17:14 -04003398
3399 if (nr_unlink)
David Sterba4884b472013-03-20 13:31:27 +00003400 btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003401 if (nr_truncate)
David Sterba4884b472013-03-20 13:31:27 +00003402 btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003403
3404out:
3405 if (ret)
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003406 btrfs_crit(root->fs_info,
3407 "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003408 btrfs_free_path(path);
3409 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003410}
3411
Chris Masond352ac62008-09-29 15:18:18 -04003412/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003413 * very simple check to peek ahead in the leaf looking for xattrs. If we
3414 * don't find any xattrs, we know there can't be any acls.
3415 *
3416 * slot is the slot the inode is in, objectid is the objectid of the inode
3417 */
3418static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003419 int slot, u64 objectid,
3420 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003421{
3422 u32 nritems = btrfs_header_nritems(leaf);
3423 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003424 static u64 xattr_access = 0;
3425 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003426 int scanned = 0;
3427
Josef Bacikf23b5a52013-06-19 10:16:26 -04003428 if (!xattr_access) {
3429 xattr_access = btrfs_name_hash(POSIX_ACL_XATTR_ACCESS,
3430 strlen(POSIX_ACL_XATTR_ACCESS));
3431 xattr_default = btrfs_name_hash(POSIX_ACL_XATTR_DEFAULT,
3432 strlen(POSIX_ACL_XATTR_DEFAULT));
3433 }
3434
Chris Mason46a53cc2009-04-27 11:47:50 -04003435 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003436 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003437 while (slot < nritems) {
3438 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3439
3440 /* we found a different objectid, there must not be acls */
3441 if (found_key.objectid != objectid)
3442 return 0;
3443
3444 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003445 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003446 if (*first_xattr_slot == -1)
3447 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003448 if (found_key.offset == xattr_access ||
3449 found_key.offset == xattr_default)
3450 return 1;
3451 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003452
3453 /*
3454 * we found a key greater than an xattr key, there can't
3455 * be any acls later on
3456 */
3457 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3458 return 0;
3459
3460 slot++;
3461 scanned++;
3462
3463 /*
3464 * it goes inode, inode backrefs, xattrs, extents,
3465 * so if there are a ton of hard links to an inode there can
3466 * be a lot of backrefs. Don't waste time searching too hard,
3467 * this is just an optimization
3468 */
3469 if (scanned >= 8)
3470 break;
3471 }
3472 /* we hit the end of the leaf before we found an xattr or
3473 * something larger than an xattr. We have to assume the inode
3474 * has acls
3475 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003476 if (*first_xattr_slot == -1)
3477 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003478 return 1;
3479}
3480
3481/*
Chris Masond352ac62008-09-29 15:18:18 -04003482 * read an inode from the btree into the in-memory inode
3483 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003484static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003485{
3486 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003487 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003488 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04003489 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04003490 struct btrfs_root *root = BTRFS_I(inode)->root;
3491 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003492 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003493 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003494 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003495 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003496 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003497 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003498
3499 ret = btrfs_fill_inode(inode, &rdev);
3500 if (!ret)
3501 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003502
3503 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07003504 if (!path)
3505 goto make_bad;
3506
Chris Mason39279cc2007-06-12 06:35:45 -04003507 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003508
Chris Mason39279cc2007-06-12 06:35:45 -04003509 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003510 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04003511 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04003512
Chris Mason5f39d392007-10-15 16:14:19 -04003513 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003514
3515 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003516 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003517
Chris Mason5f39d392007-10-15 16:14:19 -04003518 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3519 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003520 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003521 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003522 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3523 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04003524 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003525
3526 tspec = btrfs_inode_atime(inode_item);
3527 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3528 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3529
3530 tspec = btrfs_inode_mtime(inode_item);
3531 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3532 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3533
3534 tspec = btrfs_inode_ctime(inode_item);
3535 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3536 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3537
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003538 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003539 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003540 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3541
3542 /*
3543 * If we were modified in the current generation and evicted from memory
3544 * and then re-read we need to do a full sync since we don't have any
3545 * idea about which extents were modified before we were evicted from
3546 * cache.
3547 */
3548 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
3549 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3550 &BTRFS_I(inode)->runtime_flags);
3551
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003552 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003553 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003554 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003555 rdev = btrfs_inode_rdev(leaf, inode_item);
3556
Josef Bacikaec74772008-07-24 12:12:38 -04003557 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003558 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003559
3560cache_index:
3561 path->slots[0]++;
3562 if (inode->i_nlink != 1 ||
3563 path->slots[0] >= btrfs_header_nritems(leaf))
3564 goto cache_acl;
3565
3566 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
3567 if (location.objectid != btrfs_ino(inode))
3568 goto cache_acl;
3569
3570 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3571 if (location.type == BTRFS_INODE_REF_KEY) {
3572 struct btrfs_inode_ref *ref;
3573
3574 ref = (struct btrfs_inode_ref *)ptr;
3575 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3576 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3577 struct btrfs_inode_extref *extref;
3578
3579 extref = (struct btrfs_inode_extref *)ptr;
3580 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3581 extref);
3582 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003583cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003584 /*
3585 * try to precache a NULL acl entry for files that don't have
3586 * any xattrs or acls
3587 */
Li Zefan33345d012011-04-20 10:31:50 +08003588 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003589 btrfs_ino(inode), &first_xattr_slot);
3590 if (first_xattr_slot != -1) {
3591 path->slots[0] = first_xattr_slot;
3592 ret = btrfs_load_inode_props(inode, path);
3593 if (ret)
3594 btrfs_err(root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003595 "error loading props for ino %llu (root %llu): %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003596 btrfs_ino(inode),
3597 root->root_key.objectid, ret);
3598 }
3599 btrfs_free_path(path);
3600
Al Viro72c04902009-06-24 16:58:48 -04003601 if (!maybe_acls)
3602 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003603
Chris Mason39279cc2007-06-12 06:35:45 -04003604 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003605 case S_IFREG:
3606 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003607 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05003608 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003609 inode->i_fop = &btrfs_file_operations;
3610 inode->i_op = &btrfs_file_inode_operations;
3611 break;
3612 case S_IFDIR:
3613 inode->i_fop = &btrfs_dir_file_operations;
3614 if (root == root->fs_info->tree_root)
3615 inode->i_op = &btrfs_dir_ro_inode_operations;
3616 else
3617 inode->i_op = &btrfs_dir_inode_operations;
3618 break;
3619 case S_IFLNK:
3620 inode->i_op = &btrfs_symlink_inode_operations;
3621 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04003622 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003623 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003624 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003625 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003626 init_special_inode(inode, inode->i_mode, rdev);
3627 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003628 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003629
3630 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003631 return;
3632
3633make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003634 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003635 make_bad_inode(inode);
3636}
3637
Chris Masond352ac62008-09-29 15:18:18 -04003638/*
3639 * given a leaf and an inode, copy the inode fields into the leaf
3640 */
Chris Masone02119d2008-09-05 16:13:11 -04003641static void fill_inode_item(struct btrfs_trans_handle *trans,
3642 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003643 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003644 struct inode *inode)
3645{
Liu Bo51fab692012-12-27 09:01:21 +00003646 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003647
Liu Bo51fab692012-12-27 09:01:21 +00003648 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003649
Liu Bo51fab692012-12-27 09:01:21 +00003650 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3651 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3652 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3653 &token);
3654 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3655 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003656
Liu Bo51fab692012-12-27 09:01:21 +00003657 btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item),
3658 inode->i_atime.tv_sec, &token);
3659 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item),
3660 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003661
Liu Bo51fab692012-12-27 09:01:21 +00003662 btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item),
3663 inode->i_mtime.tv_sec, &token);
3664 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item),
3665 inode->i_mtime.tv_nsec, &token);
3666
3667 btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item),
3668 inode->i_ctime.tv_sec, &token);
3669 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item),
3670 inode->i_ctime.tv_nsec, &token);
3671
3672 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3673 &token);
3674 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3675 &token);
3676 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3677 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3678 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3679 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3680 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003681}
3682
Chris Masond352ac62008-09-29 15:18:18 -04003683/*
3684 * copy everything in the in-memory inode into the btree.
3685 */
Chris Mason21151332011-11-10 20:39:08 -05003686static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003687 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003688{
3689 struct btrfs_inode_item *inode_item;
3690 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003691 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003692 int ret;
3693
3694 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003695 if (!path)
3696 return -ENOMEM;
3697
Chris Masonb9473432009-03-13 11:00:37 -04003698 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003699 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3700 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003701 if (ret) {
3702 if (ret > 0)
3703 ret = -ENOENT;
3704 goto failed;
3705 }
3706
Chris Mason5f39d392007-10-15 16:14:19 -04003707 leaf = path->nodes[0];
3708 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003709 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003710
Chris Masone02119d2008-09-05 16:13:11 -04003711 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003712 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003713 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003714 ret = 0;
3715failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003716 btrfs_free_path(path);
3717 return ret;
3718}
3719
Chris Masond352ac62008-09-29 15:18:18 -04003720/*
Chris Mason21151332011-11-10 20:39:08 -05003721 * copy everything in the in-memory inode into the btree.
3722 */
3723noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3724 struct btrfs_root *root, struct inode *inode)
3725{
3726 int ret;
3727
3728 /*
3729 * If the inode is a free space inode, we can deadlock during commit
3730 * if we put it into the delayed code.
3731 *
3732 * The data relocation inode should also be directly updated
3733 * without delay
3734 */
Liu Bo83eea1f2012-07-10 05:28:39 -06003735 if (!btrfs_is_free_space_inode(inode)
Josef Bacik1d52c782014-09-18 11:30:44 -04003736 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
3737 && !root->fs_info->log_root_recovering) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003738 btrfs_update_root_times(trans, root);
3739
Chris Mason21151332011-11-10 20:39:08 -05003740 ret = btrfs_delayed_update_inode(trans, root, inode);
3741 if (!ret)
3742 btrfs_set_inode_last_trans(trans, inode);
3743 return ret;
3744 }
3745
3746 return btrfs_update_inode_item(trans, root, inode);
3747}
3748
Josef Bacikbe6aef62012-10-22 15:43:12 -04003749noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3750 struct btrfs_root *root,
3751 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003752{
3753 int ret;
3754
3755 ret = btrfs_update_inode(trans, root, inode);
3756 if (ret == -ENOSPC)
3757 return btrfs_update_inode_item(trans, root, inode);
3758 return ret;
3759}
3760
3761/*
Chris Masond352ac62008-09-29 15:18:18 -04003762 * unlink helper that gets used here in inode.c and in the tree logging
3763 * recovery code. It remove a link in a directory with a given name, and
3764 * also drops the back refs in the inode to the directory
3765 */
Al Viro92986792011-03-04 17:14:37 +00003766static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3767 struct btrfs_root *root,
3768 struct inode *dir, struct inode *inode,
3769 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003770{
3771 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003772 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003773 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003774 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003775 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003776 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003777 u64 ino = btrfs_ino(inode);
3778 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003779
3780 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003781 if (!path) {
3782 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003783 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003784 }
3785
Chris Masonb9473432009-03-13 11:00:37 -04003786 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003787 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003788 name, name_len, -1);
3789 if (IS_ERR(di)) {
3790 ret = PTR_ERR(di);
3791 goto err;
3792 }
3793 if (!di) {
3794 ret = -ENOENT;
3795 goto err;
3796 }
Chris Mason5f39d392007-10-15 16:14:19 -04003797 leaf = path->nodes[0];
3798 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003799 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003800 if (ret)
3801 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003802 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003803
Miao Xie67de1172013-12-26 13:07:06 +08003804 /*
3805 * If we don't have dir index, we have to get it by looking up
3806 * the inode ref, since we get the inode ref, remove it directly,
3807 * it is unnecessary to do delayed deletion.
3808 *
3809 * But if we have dir index, needn't search inode ref to get it.
3810 * Since the inode ref is close to the inode item, it is better
3811 * that we delay to delete it, and just do this deletion when
3812 * we update the inode item.
3813 */
3814 if (BTRFS_I(inode)->dir_index) {
3815 ret = btrfs_delayed_delete_inode_ref(inode);
3816 if (!ret) {
3817 index = BTRFS_I(inode)->dir_index;
3818 goto skip_backref;
3819 }
3820 }
3821
Li Zefan33345d012011-04-20 10:31:50 +08003822 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3823 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003824 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003825 btrfs_info(root->fs_info,
3826 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003827 name_len, name, ino, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003828 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003829 goto err;
3830 }
Miao Xie67de1172013-12-26 13:07:06 +08003831skip_backref:
Miao Xie16cdcec2011-04-22 18:12:22 +08003832 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003833 if (ret) {
3834 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003835 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003836 }
Chris Mason39279cc2007-06-12 06:35:45 -04003837
Chris Masone02119d2008-09-05 16:13:11 -04003838 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08003839 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003840 if (ret != 0 && ret != -ENOENT) {
3841 btrfs_abort_transaction(trans, root, ret);
3842 goto err;
3843 }
Chris Masone02119d2008-09-05 16:13:11 -04003844
3845 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
3846 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04003847 if (ret == -ENOENT)
3848 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003849 else if (ret)
3850 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003851err:
3852 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003853 if (ret)
3854 goto out;
3855
3856 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003857 inode_inc_iversion(inode);
3858 inode_inc_iversion(dir);
Chris Masone02119d2008-09-05 16:13:11 -04003859 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Tsutomu Itohb9959292012-06-25 21:25:22 -06003860 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04003861out:
Chris Mason39279cc2007-06-12 06:35:45 -04003862 return ret;
3863}
3864
Al Viro92986792011-03-04 17:14:37 +00003865int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3866 struct btrfs_root *root,
3867 struct inode *dir, struct inode *inode,
3868 const char *name, int name_len)
3869{
3870 int ret;
3871 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3872 if (!ret) {
Zach Brown8b558c52013-10-16 12:10:34 -07003873 drop_nlink(inode);
Al Viro92986792011-03-04 17:14:37 +00003874 ret = btrfs_update_inode(trans, root, inode);
3875 }
3876 return ret;
3877}
Yan, Zhenga22285a2010-05-16 10:48:46 -04003878
3879/*
3880 * helper to start transaction for unlink and rmdir.
3881 *
Josef Bacikd52be812013-05-29 14:54:47 -04003882 * unlink and rmdir are special in btrfs, they do not always free space, so
3883 * if we cannot make our reservations the normal way try and see if there is
3884 * plenty of slack room in the global reserve to migrate, otherwise we cannot
3885 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04003886 */
Josef Bacikd52be812013-05-29 14:54:47 -04003887static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003888{
3889 struct btrfs_trans_handle *trans;
3890 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003891 int ret;
3892
Josef Bacike70bea52011-10-11 14:18:24 -04003893 /*
3894 * 1 for the possible orphan item
3895 * 1 for the dir item
3896 * 1 for the dir index
3897 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04003898 * 1 for the inode
3899 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04003900 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003901 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
3902 return trans;
3903
Josef Bacikd52be812013-05-29 14:54:47 -04003904 if (PTR_ERR(trans) == -ENOSPC) {
3905 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003906
Josef Bacikd52be812013-05-29 14:54:47 -04003907 trans = btrfs_start_transaction(root, 0);
3908 if (IS_ERR(trans))
3909 return trans;
3910 ret = btrfs_cond_migrate_bytes(root->fs_info,
3911 &root->fs_info->trans_block_rsv,
3912 num_bytes, 5);
3913 if (ret) {
3914 btrfs_end_transaction(trans, root);
3915 return ERR_PTR(ret);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003916 }
Josef Bacik5a77d762011-11-01 14:32:23 -04003917 trans->block_rsv = &root->fs_info->trans_block_rsv;
Josef Bacikd52be812013-05-29 14:54:47 -04003918 trans->bytes_reserved = num_bytes;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003919 }
Josef Bacikd52be812013-05-29 14:54:47 -04003920 return trans;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003921}
3922
Chris Mason39279cc2007-06-12 06:35:45 -04003923static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3924{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003925 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003926 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04003927 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003928 int ret;
3929
Josef Bacikd52be812013-05-29 14:54:47 -04003930 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003931 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003932 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003933
Chris Mason12fcfd22009-03-24 10:24:20 -04003934 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
3935
Chris Masone02119d2008-09-05 16:13:11 -04003936 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3937 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003938 if (ret)
3939 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003940
Yan, Zhenga22285a2010-05-16 10:48:46 -04003941 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04003942 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003943 if (ret)
3944 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003945 }
Josef Bacik7b128762008-07-24 12:17:14 -04003946
Tsutomu Itohb5324022011-07-19 07:27:20 +00003947out:
Josef Bacikd52be812013-05-29 14:54:47 -04003948 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003949 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04003950 return ret;
3951}
3952
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003953int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3954 struct btrfs_root *root,
3955 struct inode *dir, u64 objectid,
3956 const char *name, int name_len)
3957{
3958 struct btrfs_path *path;
3959 struct extent_buffer *leaf;
3960 struct btrfs_dir_item *di;
3961 struct btrfs_key key;
3962 u64 index;
3963 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003964 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003965
3966 path = btrfs_alloc_path();
3967 if (!path)
3968 return -ENOMEM;
3969
Li Zefan33345d012011-04-20 10:31:50 +08003970 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003971 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003972 if (IS_ERR_OR_NULL(di)) {
3973 if (!di)
3974 ret = -ENOENT;
3975 else
3976 ret = PTR_ERR(di);
3977 goto out;
3978 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003979
3980 leaf = path->nodes[0];
3981 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3982 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3983 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003984 if (ret) {
3985 btrfs_abort_transaction(trans, root, ret);
3986 goto out;
3987 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003988 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003989
3990 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3991 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003992 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003993 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003994 if (ret != -ENOENT) {
3995 btrfs_abort_transaction(trans, root, ret);
3996 goto out;
3997 }
Li Zefan33345d012011-04-20 10:31:50 +08003998 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003999 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004000 if (IS_ERR_OR_NULL(di)) {
4001 if (!di)
4002 ret = -ENOENT;
4003 else
4004 ret = PTR_ERR(di);
4005 btrfs_abort_transaction(trans, root, ret);
4006 goto out;
4007 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004008
4009 leaf = path->nodes[0];
4010 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004011 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004012 index = key.offset;
4013 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004014 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004015
Miao Xie16cdcec2011-04-22 18:12:22 +08004016 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004017 if (ret) {
4018 btrfs_abort_transaction(trans, root, ret);
4019 goto out;
4020 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004021
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004022 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004023 inode_inc_iversion(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004024 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Josef Bacik5a24e842012-08-08 10:12:59 -06004025 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004026 if (ret)
4027 btrfs_abort_transaction(trans, root, ret);
4028out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004029 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004030 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004031}
4032
Chris Mason39279cc2007-06-12 06:35:45 -04004033static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4034{
4035 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05004036 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004037 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004038 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004039
David Sterbab3ae2442012-09-13 16:04:34 -06004040 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004041 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06004042 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
4043 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04004044
Josef Bacikd52be812013-05-29 14:54:47 -04004045 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004046 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004047 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004048
Li Zefan33345d012011-04-20 10:31:50 +08004049 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004050 err = btrfs_unlink_subvol(trans, root, dir,
4051 BTRFS_I(inode)->location.objectid,
4052 dentry->d_name.name,
4053 dentry->d_name.len);
4054 goto out;
4055 }
4056
Josef Bacik7b128762008-07-24 12:17:14 -04004057 err = btrfs_orphan_add(trans, inode);
4058 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004059 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004060
Chris Mason39279cc2007-06-12 06:35:45 -04004061 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04004062 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
4063 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05004064 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04004065 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004066out:
Josef Bacikd52be812013-05-29 14:54:47 -04004067 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004068 btrfs_btree_balance_dirty(root);
Chris Mason39544012007-12-12 14:38:19 -05004069
Chris Mason39279cc2007-06-12 06:35:45 -04004070 return err;
4071}
4072
Chris Mason323ac952008-10-01 19:05:46 -04004073/*
Chris Mason39279cc2007-06-12 06:35:45 -04004074 * this can truncate away extent items, csum items and directory items.
4075 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004076 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004077 *
4078 * csum items that cross the new i_size are truncated to the new size
4079 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004080 *
4081 * min_type is the minimum key type to truncate down to. If set to 0, this
4082 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004083 */
Yan, Zheng80825102009-11-12 09:35:36 +00004084int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4085 struct btrfs_root *root,
4086 struct inode *inode,
4087 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004088{
Chris Mason39279cc2007-06-12 06:35:45 -04004089 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004090 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004091 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004092 struct btrfs_key key;
4093 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004094 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004095 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004096 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004097 u64 item_end = 0;
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004098 u64 last_size = (u64)-1;
Yan, Zheng80825102009-11-12 09:35:36 +00004099 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004100 int found_extent;
4101 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004102 int pending_del_nr = 0;
4103 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004104 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004105 int ret;
4106 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08004107 u64 ino = btrfs_ino(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004108
4109 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004110
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004111 path = btrfs_alloc_path();
4112 if (!path)
4113 return -ENOMEM;
4114 path->reada = -1;
4115
Josef Bacik5dc562c2012-08-17 13:14:17 -04004116 /*
4117 * We want to drop from the next block forward in case this new size is
4118 * not block aligned since we will be keeping the last block of the
4119 * extent just the way it is.
4120 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004121 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4122 root == root->fs_info->tree_root)
Qu Wenruofda28322013-02-26 08:10:22 +00004123 btrfs_drop_extent_cache(inode, ALIGN(new_size,
4124 root->sectorsize), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004125
Miao Xie16cdcec2011-04-22 18:12:22 +08004126 /*
4127 * This function is also used to drop the items in the log tree before
4128 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4129 * it is used to drop the loged items. So we shouldn't kill the delayed
4130 * items.
4131 */
4132 if (min_type == 0 && root == BTRFS_I(inode)->root)
4133 btrfs_kill_delayed_inode_items(inode);
4134
Li Zefan33345d012011-04-20 10:31:50 +08004135 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004136 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004137 key.type = (u8)-1;
4138
Chris Mason85e21ba2008-01-29 15:11:36 -05004139search_again:
Chris Masonb9473432009-03-13 11:00:37 -04004140 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004141 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004142 if (ret < 0) {
4143 err = ret;
4144 goto out;
4145 }
Chris Masond3977122009-01-05 21:25:51 -05004146
Chris Mason85e21ba2008-01-29 15:11:36 -05004147 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004148 /* there are no items in the tree for us to truncate, we're
4149 * done
4150 */
Yan, Zheng80825102009-11-12 09:35:36 +00004151 if (path->slots[0] == 0)
4152 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004153 path->slots[0]--;
4154 }
4155
Chris Masond3977122009-01-05 21:25:51 -05004156 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004157 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004158 leaf = path->nodes[0];
4159 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004160 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004161
Li Zefan33345d012011-04-20 10:31:50 +08004162 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004163 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004164
Chris Mason85e21ba2008-01-29 15:11:36 -05004165 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004166 break;
4167
Chris Mason5f39d392007-10-15 16:14:19 -04004168 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004169 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004170 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004171 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004172 extent_type = btrfs_file_extent_type(leaf, fi);
4173 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004174 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004175 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04004176 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004177 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004178 path->slots[0], fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004179 }
Yan008630c2007-11-07 13:31:09 -05004180 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004181 }
Yan, Zheng80825102009-11-12 09:35:36 +00004182 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004183 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004184 } else {
4185 if (item_end < new_size)
4186 break;
4187 if (found_key.offset >= new_size)
4188 del_item = 1;
4189 else
4190 del_item = 0;
4191 }
Chris Mason39279cc2007-06-12 06:35:45 -04004192 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004193 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004194 if (found_type != BTRFS_EXTENT_DATA_KEY)
4195 goto delete;
4196
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004197 if (del_item)
4198 last_size = found_key.offset;
4199 else
4200 last_size = new_size;
4201
Chris Mason179e29e2007-11-01 11:28:41 -04004202 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004203 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004204 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004205 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004206 u64 orig_num_bytes =
4207 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004208 extent_num_bytes = ALIGN(new_size -
4209 found_key.offset,
4210 root->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004211 btrfs_set_file_extent_num_bytes(leaf, fi,
4212 extent_num_bytes);
4213 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004214 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004215 if (test_bit(BTRFS_ROOT_REF_COWS,
4216 &root->state) &&
4217 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004218 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004219 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004220 } else {
Chris Masondb945352007-10-15 16:15:53 -04004221 extent_num_bytes =
4222 btrfs_file_extent_disk_num_bytes(leaf,
4223 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004224 extent_offset = found_key.offset -
4225 btrfs_file_extent_offset(leaf, fi);
4226
Chris Mason39279cc2007-06-12 06:35:45 -04004227 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004228 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004229 if (extent_start != 0) {
4230 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004231 if (test_bit(BTRFS_ROOT_REF_COWS,
4232 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004233 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004234 }
4235 }
Chris Mason90692182008-02-08 13:49:28 -05004236 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004237 /*
4238 * we can't truncate inline items that have had
4239 * special encodings
4240 */
4241 if (!del_item &&
4242 btrfs_file_extent_compression(leaf, fi) == 0 &&
4243 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4244 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004245 u32 size = new_size - found_key.offset;
4246
Miao Xie27cdeb72014-04-02 19:51:05 +08004247 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004248 inode_sub_bytes(inode, item_end + 1 -
4249 new_size);
Chris Mason514ac8a2014-01-03 21:07:00 -08004250
4251 /*
4252 * update the ram bytes to properly reflect
4253 * the new size of our item
4254 */
4255 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
Chris Masone02119d2008-09-05 16:13:11 -04004256 size =
4257 btrfs_file_extent_calc_inline_size(size);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004258 btrfs_truncate_item(root, path, size, 1);
Miao Xie27cdeb72014-04-02 19:51:05 +08004259 } else if (test_bit(BTRFS_ROOT_REF_COWS,
4260 &root->state)) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004261 inode_sub_bytes(inode, item_end + 1 -
4262 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05004263 }
Chris Mason39279cc2007-06-12 06:35:45 -04004264 }
Chris Mason179e29e2007-11-01 11:28:41 -04004265delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004266 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004267 if (!pending_del_nr) {
4268 /* no pending yet, add ourselves */
4269 pending_del_slot = path->slots[0];
4270 pending_del_nr = 1;
4271 } else if (pending_del_nr &&
4272 path->slots[0] + 1 == pending_del_slot) {
4273 /* hop on the pending chunk */
4274 pending_del_nr++;
4275 pending_del_slot = path->slots[0];
4276 } else {
Chris Masond3977122009-01-05 21:25:51 -05004277 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004278 }
Chris Mason39279cc2007-06-12 06:35:45 -04004279 } else {
4280 break;
4281 }
Miao Xie27cdeb72014-04-02 19:51:05 +08004282 if (found_extent &&
4283 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4284 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004285 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004286 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004287 extent_num_bytes, 0,
4288 btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02004289 ino, extent_offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004290 BUG_ON(ret);
4291 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004292
Yan, Zheng80825102009-11-12 09:35:36 +00004293 if (found_type == BTRFS_INODE_ITEM_KEY)
4294 break;
4295
4296 if (path->slots[0] == 0 ||
4297 path->slots[0] != pending_del_slot) {
Yan, Zheng80825102009-11-12 09:35:36 +00004298 if (pending_del_nr) {
4299 ret = btrfs_del_items(trans, root, path,
4300 pending_del_slot,
4301 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004302 if (ret) {
4303 btrfs_abort_transaction(trans,
4304 root, ret);
4305 goto error;
4306 }
Yan, Zheng80825102009-11-12 09:35:36 +00004307 pending_del_nr = 0;
4308 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004309 btrfs_release_path(path);
Chris Mason85e21ba2008-01-29 15:11:36 -05004310 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004311 } else {
4312 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004313 }
Chris Mason39279cc2007-06-12 06:35:45 -04004314 }
Yan, Zheng80825102009-11-12 09:35:36 +00004315out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004316 if (pending_del_nr) {
4317 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4318 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004319 if (ret)
4320 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004321 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004322error:
Filipe Mananadac57052014-08-29 20:54:26 +01004323 if (last_size != (u64)-1 &&
4324 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004325 btrfs_ordered_update_i_size(inode, last_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004326 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00004327 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004328}
4329
Chris Masona52d9a82007-08-27 16:49:44 -04004330/*
Josef Bacik2aaa6652012-08-29 14:27:18 -04004331 * btrfs_truncate_page - read, zero a chunk and write a page
4332 * @inode - inode that we're zeroing
4333 * @from - the offset to start zeroing
4334 * @len - the length to zero, 0 to zero the entire range respective to the
4335 * offset
4336 * @front - zero up to the offset instead of from the offset on
4337 *
4338 * This will find the page for the "from" offset and cow the page and zero the
4339 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004340 */
Josef Bacik2aaa6652012-08-29 14:27:18 -04004341int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
4342 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004343{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004344 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04004345 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004346 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4347 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004348 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004349 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04004350 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04004351 pgoff_t index = from >> PAGE_CACHE_SHIFT;
4352 unsigned offset = from & (PAGE_CACHE_SIZE-1);
4353 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004354 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004355 int ret = 0;
4356 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004357 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004358
Josef Bacik2aaa6652012-08-29 14:27:18 -04004359 if ((offset & (blocksize - 1)) == 0 &&
4360 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004361 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004362 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004363 if (ret)
4364 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004365
Chris Mason211c17f2008-05-15 09:13:45 -04004366again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004367 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004368 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004369 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Miao Xieac6a2b32012-12-05 10:56:13 +00004370 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004371 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004372 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004373
4374 page_start = page_offset(page);
4375 page_end = page_start + PAGE_CACHE_SIZE - 1;
4376
Chris Masona52d9a82007-08-27 16:49:44 -04004377 if (!PageUptodate(page)) {
4378 ret = btrfs_readpage(NULL, page);
4379 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004380 if (page->mapping != mapping) {
4381 unlock_page(page);
4382 page_cache_release(page);
4383 goto again;
4384 }
Chris Masona52d9a82007-08-27 16:49:44 -04004385 if (!PageUptodate(page)) {
4386 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004387 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004388 }
4389 }
Chris Mason211c17f2008-05-15 09:13:45 -04004390 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004391
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004392 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004393 set_page_extent_mapped(page);
4394
4395 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4396 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004397 unlock_extent_cached(io_tree, page_start, page_end,
4398 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004399 unlock_page(page);
4400 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004401 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004402 btrfs_put_ordered_extent(ordered);
4403 goto again;
4404 }
4405
Josef Bacik2ac55d42010-02-03 19:33:23 +00004406 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004407 EXTENT_DIRTY | EXTENT_DELALLOC |
4408 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004409 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004410
Josef Bacik2ac55d42010-02-03 19:33:23 +00004411 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
4412 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004413 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004414 unlock_extent_cached(io_tree, page_start, page_end,
4415 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004416 goto out_unlock;
4417 }
4418
Chris Masone6dcd2d2008-07-17 12:53:50 -04004419 if (offset != PAGE_CACHE_SIZE) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004420 if (!len)
4421 len = PAGE_CACHE_SIZE - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004422 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004423 if (front)
4424 memset(kaddr, 0, offset);
4425 else
4426 memset(kaddr + offset, 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004427 flush_dcache_page(page);
4428 kunmap(page);
4429 }
Chris Mason247e7432008-07-17 12:53:51 -04004430 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004431 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004432 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
4433 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004434
Chris Mason89642222008-07-24 09:41:53 -04004435out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004436 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004437 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04004438 unlock_page(page);
4439 page_cache_release(page);
4440out:
4441 return ret;
4442}
4443
Josef Bacik16e75492013-10-22 12:18:51 -04004444static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4445 u64 offset, u64 len)
4446{
4447 struct btrfs_trans_handle *trans;
4448 int ret;
4449
4450 /*
4451 * Still need to make sure the inode looks like it's been updated so
4452 * that any holes get logged if we fsync.
4453 */
4454 if (btrfs_fs_incompat(root->fs_info, NO_HOLES)) {
4455 BTRFS_I(inode)->last_trans = root->fs_info->generation;
4456 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4457 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4458 return 0;
4459 }
4460
4461 /*
4462 * 1 - for the one we're dropping
4463 * 1 - for the one we're adding
4464 * 1 - for updating the inode.
4465 */
4466 trans = btrfs_start_transaction(root, 3);
4467 if (IS_ERR(trans))
4468 return PTR_ERR(trans);
4469
4470 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4471 if (ret) {
4472 btrfs_abort_transaction(trans, root, ret);
4473 btrfs_end_transaction(trans, root);
4474 return ret;
4475 }
4476
4477 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset,
4478 0, 0, len, 0, len, 0, 0, 0);
4479 if (ret)
4480 btrfs_abort_transaction(trans, root, ret);
4481 else
4482 btrfs_update_inode(trans, root, inode);
4483 btrfs_end_transaction(trans, root);
4484 return ret;
4485}
4486
Josef Bacik695a0d02011-03-04 15:46:53 -05004487/*
4488 * This function puts in dummy file extents for the area we're creating a hole
4489 * for. So if we are truncating this file to a larger size we need to insert
4490 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4491 * the range between oldsize and size
4492 */
Josef Bacika41ad392011-01-31 15:30:16 -05004493int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004494{
Yan Zheng9036c102008-10-30 14:19:41 -04004495 struct btrfs_root *root = BTRFS_I(inode)->root;
4496 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004497 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004498 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004499 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Qu Wenruofda28322013-02-26 08:10:22 +00004500 u64 hole_start = ALIGN(oldsize, root->sectorsize);
4501 u64 block_end = ALIGN(size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004502 u64 last_byte;
4503 u64 cur_offset;
4504 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004505 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004506
Josef Bacika71754f2013-06-17 17:14:39 -04004507 /*
4508 * If our size started in the middle of a page we need to zero out the
4509 * rest of the page before we expand the i_size, otherwise we could
4510 * expose stale data.
4511 */
4512 err = btrfs_truncate_page(inode, oldsize, 0, 0);
4513 if (err)
4514 return err;
4515
Yan Zheng9036c102008-10-30 14:19:41 -04004516 if (size <= hole_start)
4517 return 0;
4518
Yan Zheng9036c102008-10-30 14:19:41 -04004519 while (1) {
4520 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004521
Josef Bacik2ac55d42010-02-03 19:33:23 +00004522 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004523 &cached_state);
Miao Xiefa7c1492013-09-26 13:15:27 +08004524 ordered = btrfs_lookup_ordered_range(inode, hole_start,
4525 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004526 if (!ordered)
4527 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004528 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4529 &cached_state, GFP_NOFS);
Miao Xiefa7c1492013-09-26 13:15:27 +08004530 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004531 btrfs_put_ordered_extent(ordered);
4532 }
4533
Yan Zheng9036c102008-10-30 14:19:41 -04004534 cur_offset = hole_start;
4535 while (1) {
4536 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4537 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004538 if (IS_ERR(em)) {
4539 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004540 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004541 break;
4542 }
Yan Zheng9036c102008-10-30 14:19:41 -04004543 last_byte = min(extent_map_end(em), block_end);
Qu Wenruofda28322013-02-26 08:10:22 +00004544 last_byte = ALIGN(last_byte , root->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004545 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004546 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004547 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004548
Josef Bacik16e75492013-10-22 12:18:51 -04004549 err = maybe_insert_hole(root, inode, cur_offset,
4550 hole_size);
4551 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004552 break;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004553 btrfs_drop_extent_cache(inode, cur_offset,
4554 cur_offset + hole_size - 1, 0);
4555 hole_em = alloc_extent_map();
4556 if (!hole_em) {
4557 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4558 &BTRFS_I(inode)->runtime_flags);
4559 goto next;
4560 }
4561 hole_em->start = cur_offset;
4562 hole_em->len = hole_size;
4563 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004564
Josef Bacik5dc562c2012-08-17 13:14:17 -04004565 hole_em->block_start = EXTENT_MAP_HOLE;
4566 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004567 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004568 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004569 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
4570 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Josef Bacik16e75492013-10-22 12:18:51 -04004571 hole_em->generation = root->fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004572
4573 while (1) {
4574 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004575 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004576 write_unlock(&em_tree->lock);
4577 if (err != -EEXIST)
4578 break;
4579 btrfs_drop_extent_cache(inode, cur_offset,
4580 cur_offset +
4581 hole_size - 1, 0);
4582 }
4583 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04004584 }
Josef Bacik16e75492013-10-22 12:18:51 -04004585next:
Yan Zheng9036c102008-10-30 14:19:41 -04004586 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004587 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004588 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004589 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004590 break;
4591 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004592 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004593 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4594 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004595 return err;
4596}
4597
Eric Sandeen3972f262013-01-12 02:57:22 +00004598static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004599{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004600 struct btrfs_root *root = BTRFS_I(inode)->root;
4601 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004602 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004603 loff_t newsize = attr->ia_size;
4604 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004605 int ret;
4606
Eric Sandeen3972f262013-01-12 02:57:22 +00004607 /*
4608 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4609 * special case where we need to update the times despite not having
4610 * these flags set. For all other operations the VFS set these flags
4611 * explicitly if it wants a timestamp update.
4612 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004613 if (newsize != oldsize) {
4614 inode_inc_iversion(inode);
4615 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
4616 inode->i_ctime = inode->i_mtime =
4617 current_fs_time(inode->i_sb);
4618 }
Eric Sandeen3972f262013-01-12 02:57:22 +00004619
Josef Bacika41ad392011-01-31 15:30:16 -05004620 if (newsize > oldsize) {
Kirill A. Shutemov7caef262013-09-12 15:13:56 -07004621 truncate_pagecache(inode, newsize);
Josef Bacika41ad392011-01-31 15:30:16 -05004622 ret = btrfs_cont_expand(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004623 if (ret)
Yan, Zheng80825102009-11-12 09:35:36 +00004624 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004625
Miao Xief4a2f4c2011-12-14 20:12:01 -05004626 trans = btrfs_start_transaction(root, 1);
4627 if (IS_ERR(trans))
4628 return PTR_ERR(trans);
4629
4630 i_size_write(inode, newsize);
4631 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
4632 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004633 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05004634 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004635
Josef Bacika41ad392011-01-31 15:30:16 -05004636 /*
4637 * We're truncating a file that used to have good data down to
4638 * zero. Make sure it gets into the ordered flush list so that
4639 * any new writes get down to disk quickly.
4640 */
4641 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004642 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4643 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004644
Josef Bacikf3fe8202013-01-07 17:03:21 -05004645 /*
4646 * 1 for the orphan item we're going to add
4647 * 1 for the orphan item deletion.
4648 */
4649 trans = btrfs_start_transaction(root, 2);
4650 if (IS_ERR(trans))
4651 return PTR_ERR(trans);
4652
4653 /*
4654 * We need to do this in case we fail at _any_ point during the
4655 * actual truncate. Once we do the truncate_setsize we could
4656 * invalidate pages which forces any outstanding ordered io to
4657 * be instantly completed which will give us extents that need
4658 * to be truncated. If we fail to get an orphan inode down we
4659 * could have left over extents that were never meant to live,
4660 * so we need to garuntee from this point on that everything
4661 * will be consistent.
4662 */
4663 ret = btrfs_orphan_add(trans, inode);
4664 btrfs_end_transaction(trans, root);
4665 if (ret)
4666 return ret;
4667
Josef Bacika41ad392011-01-31 15:30:16 -05004668 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4669 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004670
4671 /* Disable nonlocked read DIO to avoid the end less truncate */
4672 btrfs_inode_block_unlocked_dio(inode);
4673 inode_dio_wait(inode);
4674 btrfs_inode_resume_unlocked_dio(inode);
4675
Josef Bacika41ad392011-01-31 15:30:16 -05004676 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004677 if (ret && inode->i_nlink) {
4678 int err;
4679
4680 /*
4681 * failed to truncate, disk_i_size is only adjusted down
4682 * as we remove extents, so it should represent the true
4683 * size of the inode, so reset the in memory size and
4684 * delete our orphan entry.
4685 */
4686 trans = btrfs_join_transaction(root);
4687 if (IS_ERR(trans)) {
4688 btrfs_orphan_del(NULL, inode);
4689 return ret;
4690 }
4691 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
4692 err = btrfs_orphan_del(trans, inode);
4693 if (err)
4694 btrfs_abort_transaction(trans, root, err);
4695 btrfs_end_transaction(trans, root);
4696 }
Yan, Zheng80825102009-11-12 09:35:36 +00004697 }
4698
Josef Bacika41ad392011-01-31 15:30:16 -05004699 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004700}
4701
Chris Mason39279cc2007-06-12 06:35:45 -04004702static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
4703{
4704 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08004705 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004706 int err;
4707
Li Zefanb83cc962010-12-20 16:04:08 +08004708 if (btrfs_root_readonly(root))
4709 return -EROFS;
4710
Chris Mason39279cc2007-06-12 06:35:45 -04004711 err = inode_change_ok(inode, attr);
4712 if (err)
4713 return err;
4714
Chris Mason5a3f23d2009-03-31 13:27:11 -04004715 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00004716 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00004717 if (err)
4718 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004719 }
Yan Zheng9036c102008-10-30 14:19:41 -04004720
Christoph Hellwig10257742010-06-04 11:30:02 +02004721 if (attr->ia_valid) {
4722 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004723 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004724 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04004725
Josef Bacik22c44fe2011-11-30 10:45:38 -05004726 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08004727 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02004728 }
4729
Chris Mason39279cc2007-06-12 06:35:45 -04004730 return err;
4731}
Chris Mason61295eb2008-01-14 16:24:38 -05004732
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004733/*
4734 * While truncating the inode pages during eviction, we get the VFS calling
4735 * btrfs_invalidatepage() against each page of the inode. This is slow because
4736 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
4737 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
4738 * extent_state structures over and over, wasting lots of time.
4739 *
4740 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
4741 * those expensive operations on a per page basis and do only the ordered io
4742 * finishing, while we release here the extent_map and extent_state structures,
4743 * without the excessive merging and splitting.
4744 */
4745static void evict_inode_truncate_pages(struct inode *inode)
4746{
4747 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4748 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
4749 struct rb_node *node;
4750
4751 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07004752 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004753
4754 write_lock(&map_tree->lock);
4755 while (!RB_EMPTY_ROOT(&map_tree->map)) {
4756 struct extent_map *em;
4757
4758 node = rb_first(&map_tree->map);
4759 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08004760 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
4761 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004762 remove_extent_mapping(map_tree, em);
4763 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01004764 if (need_resched()) {
4765 write_unlock(&map_tree->lock);
4766 cond_resched();
4767 write_lock(&map_tree->lock);
4768 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004769 }
4770 write_unlock(&map_tree->lock);
4771
4772 spin_lock(&io_tree->lock);
4773 while (!RB_EMPTY_ROOT(&io_tree->state)) {
4774 struct extent_state *state;
4775 struct extent_state *cached_state = NULL;
4776
4777 node = rb_first(&io_tree->state);
4778 state = rb_entry(node, struct extent_state, rb_node);
4779 atomic_inc(&state->refs);
4780 spin_unlock(&io_tree->lock);
4781
4782 lock_extent_bits(io_tree, state->start, state->end,
4783 0, &cached_state);
4784 clear_extent_bit(io_tree, state->start, state->end,
4785 EXTENT_LOCKED | EXTENT_DIRTY |
4786 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
4787 EXTENT_DEFRAG, 1, 1,
4788 &cached_state, GFP_NOFS);
4789 free_extent_state(state);
4790
Filipe Manana7064dd52014-08-08 02:47:05 +01004791 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004792 spin_lock(&io_tree->lock);
4793 }
4794 spin_unlock(&io_tree->lock);
4795}
4796
Al Virobd555972010-06-07 11:35:40 -04004797void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004798{
4799 struct btrfs_trans_handle *trans;
4800 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04004801 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik07127182011-08-19 10:29:59 -04004802 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04004803 int ret;
4804
liubo1abe9b82011-03-24 11:18:59 +00004805 trace_btrfs_inode_evict(inode);
4806
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004807 evict_inode_truncate_pages(inode);
4808
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02004809 if (inode->i_nlink &&
4810 ((btrfs_root_refs(&root->root_item) != 0 &&
4811 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
4812 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04004813 goto no_delete;
4814
Chris Mason39279cc2007-06-12 06:35:45 -04004815 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04004816 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004817 goto no_delete;
4818 }
Al Virobd555972010-06-07 11:35:40 -04004819 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04004820 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04004821
Miao Xief6124962014-09-12 18:44:04 +08004822 btrfs_free_io_failure_record(inode, 0, (u64)-1);
4823
Yan, Zhengc71bf092009-11-12 09:34:40 +00004824 if (root->fs_info->log_root_recovering) {
Liu Bo6bf02312012-06-25 21:59:09 -06004825 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04004826 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00004827 goto no_delete;
4828 }
4829
Yan, Zheng76dda932009-09-21 16:00:26 -04004830 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02004831 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
4832 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04004833 goto no_delete;
4834 }
4835
Miao Xie0e8c36a2012-12-19 06:59:51 +00004836 ret = btrfs_commit_inode_delayed_inode(inode);
4837 if (ret) {
4838 btrfs_orphan_del(NULL, inode);
4839 goto no_delete;
4840 }
4841
Miao Xie66d8f3d2012-09-06 04:02:28 -06004842 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04004843 if (!rsv) {
4844 btrfs_orphan_del(NULL, inode);
4845 goto no_delete;
4846 }
Josef Bacik4a338542011-08-29 11:01:31 -04004847 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04004848 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04004849 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04004850
Chris Masondbe674a2008-07-17 12:54:05 -04004851 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04004852
Josef Bacik4289a662011-08-05 13:22:24 -04004853 /*
Miao Xie8407aa42012-09-07 01:43:32 -06004854 * This is a bit simpler than btrfs_truncate since we've already
4855 * reserved our space for our orphan item in the unlink, so we just
4856 * need to reserve some slack space in case we add bytes and update
4857 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04004858 */
Yan, Zheng80825102009-11-12 09:35:36 +00004859 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00004860 ret = btrfs_block_rsv_refill(root, rsv, min_size,
4861 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00004862
Josef Bacik726c35f2011-09-26 15:46:06 -04004863 /*
4864 * Try and steal from the global reserve since we will
4865 * likely not use this space anyway, we want to try as
4866 * hard as possible to get this to work.
4867 */
4868 if (ret)
4869 ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
4870
Yan, Zhengd68fc572010-05-16 10:49:58 -04004871 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004872 btrfs_warn(root->fs_info,
4873 "Could not get space for a delete, will truncate on mount %d",
4874 ret);
Josef Bacik4289a662011-08-05 13:22:24 -04004875 btrfs_orphan_del(NULL, inode);
4876 btrfs_free_block_rsv(root, rsv);
4877 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04004878 }
4879
Miao Xie0e8c36a2012-12-19 06:59:51 +00004880 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04004881 if (IS_ERR(trans)) {
4882 btrfs_orphan_del(NULL, inode);
4883 btrfs_free_block_rsv(root, rsv);
4884 goto no_delete;
4885 }
4886
4887 trans->block_rsv = rsv;
4888
Yan, Zhengd68fc572010-05-16 10:49:58 -04004889 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04004890 if (ret != -ENOSPC)
Yan, Zheng80825102009-11-12 09:35:36 +00004891 break;
4892
Miao Xie8407aa42012-09-07 01:43:32 -06004893 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004894 btrfs_end_transaction(trans, root);
4895 trans = NULL;
Liu Bob53d3f52012-11-14 14:34:34 +00004896 btrfs_btree_balance_dirty(root);
Josef Bacik7b128762008-07-24 12:17:14 -04004897 }
4898
Josef Bacik4289a662011-08-05 13:22:24 -04004899 btrfs_free_block_rsv(root, rsv);
4900
Josef Bacik4ef31a42013-08-13 14:10:08 -04004901 /*
4902 * Errors here aren't a big deal, it just means we leave orphan items
4903 * in the tree. They will be cleaned up on the next mount.
4904 */
Yan, Zheng80825102009-11-12 09:35:36 +00004905 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04004906 trans->block_rsv = root->orphan_block_rsv;
Josef Bacik4ef31a42013-08-13 14:10:08 -04004907 btrfs_orphan_del(trans, inode);
4908 } else {
4909 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004910 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004911
Josef Bacik4289a662011-08-05 13:22:24 -04004912 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08004913 if (!(root == root->fs_info->tree_root ||
4914 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08004915 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08004916
Chris Mason54aa1f42007-06-22 14:16:25 -04004917 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004918 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04004919no_delete:
Miao Xie89042e52013-05-15 07:48:15 +00004920 btrfs_remove_delayed_node(inode);
Jan Karadbd57682012-05-03 14:48:02 +02004921 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004922 return;
Chris Mason39279cc2007-06-12 06:35:45 -04004923}
4924
4925/*
4926 * this returns the key found in the dir entry in the location pointer.
4927 * If no dir entries were found, location->objectid is 0.
4928 */
4929static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
4930 struct btrfs_key *location)
4931{
4932 const char *name = dentry->d_name.name;
4933 int namelen = dentry->d_name.len;
4934 struct btrfs_dir_item *di;
4935 struct btrfs_path *path;
4936 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04004937 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004938
4939 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07004940 if (!path)
4941 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05004942
Li Zefan33345d012011-04-20 10:31:50 +08004943 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04004944 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04004945 if (IS_ERR(di))
4946 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05004947
David Sterbac7040052011-04-19 18:00:01 +02004948 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05004949 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05004950
Chris Mason5f39d392007-10-15 16:14:19 -04004951 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04004952out:
Chris Mason39279cc2007-06-12 06:35:45 -04004953 btrfs_free_path(path);
4954 return ret;
Chris Mason39544012007-12-12 14:38:19 -05004955out_err:
4956 location->objectid = 0;
4957 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04004958}
4959
4960/*
4961 * when we hit a tree root in a directory, the btrfs part of the inode
4962 * needs to be changed to reflect the root directory of the tree root. This
4963 * is kind of like crossing a mount point.
4964 */
4965static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004966 struct inode *dir,
4967 struct dentry *dentry,
4968 struct btrfs_key *location,
4969 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04004970{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004971 struct btrfs_path *path;
4972 struct btrfs_root *new_root;
4973 struct btrfs_root_ref *ref;
4974 struct extent_buffer *leaf;
4975 int ret;
4976 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004977
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004978 path = btrfs_alloc_path();
4979 if (!path) {
4980 err = -ENOMEM;
4981 goto out;
4982 }
Chris Mason39279cc2007-06-12 06:35:45 -04004983
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004984 err = -ENOENT;
Kelley Nielsen75ac2dd2013-11-04 19:35:58 -08004985 ret = btrfs_find_item(root->fs_info->tree_root, path,
4986 BTRFS_I(dir)->root->root_key.objectid,
4987 location->objectid, BTRFS_ROOT_REF_KEY, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004988 if (ret) {
4989 if (ret < 0)
4990 err = ret;
4991 goto out;
4992 }
Chris Mason39279cc2007-06-12 06:35:45 -04004993
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004994 leaf = path->nodes[0];
4995 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08004996 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004997 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
4998 goto out;
4999
5000 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5001 (unsigned long)(ref + 1),
5002 dentry->d_name.len);
5003 if (ret)
5004 goto out;
5005
David Sterbab3b4aa72011-04-21 01:20:15 +02005006 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005007
5008 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
5009 if (IS_ERR(new_root)) {
5010 err = PTR_ERR(new_root);
5011 goto out;
5012 }
5013
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005014 *sub_root = new_root;
5015 location->objectid = btrfs_root_dirid(&new_root->root_item);
5016 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005017 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005018 err = 0;
5019out:
5020 btrfs_free_path(path);
5021 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005022}
5023
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005024static void inode_tree_add(struct inode *inode)
5025{
5026 struct btrfs_root *root = BTRFS_I(inode)->root;
5027 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005028 struct rb_node **p;
5029 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005030 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Li Zefan33345d012011-04-20 10:31:50 +08005031 u64 ino = btrfs_ino(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005032
Al Viro1d3382cb2010-10-23 15:19:20 -04005033 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005034 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005035 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005036 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005037 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005038 while (*p) {
5039 parent = *p;
5040 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5041
Li Zefan33345d012011-04-20 10:31:50 +08005042 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005043 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08005044 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005045 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005046 else {
5047 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005048 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005049 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005050 RB_CLEAR_NODE(parent);
5051 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005052 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005053 }
5054 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005055 rb_link_node(new, parent, p);
5056 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005057 spin_unlock(&root->inode_lock);
5058}
5059
5060static void inode_tree_del(struct inode *inode)
5061{
5062 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005063 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005064
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005065 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005066 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005067 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005068 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005069 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005070 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005071 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005072
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005073 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005074 synchronize_srcu(&root->fs_info->subvol_srcu);
5075 spin_lock(&root->inode_lock);
5076 empty = RB_EMPTY_ROOT(&root->inode_tree);
5077 spin_unlock(&root->inode_lock);
5078 if (empty)
5079 btrfs_add_dead_root(root);
5080 }
5081}
5082
Jeff Mahoney143bede2012-03-01 14:56:26 +01005083void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005084{
5085 struct rb_node *node;
5086 struct rb_node *prev;
5087 struct btrfs_inode *entry;
5088 struct inode *inode;
5089 u64 objectid = 0;
5090
Liu Bo7813b3d2014-02-10 17:37:25 +08005091 if (!test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
5092 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04005093
5094 spin_lock(&root->inode_lock);
5095again:
5096 node = root->inode_tree.rb_node;
5097 prev = NULL;
5098 while (node) {
5099 prev = node;
5100 entry = rb_entry(node, struct btrfs_inode, rb_node);
5101
Li Zefan33345d012011-04-20 10:31:50 +08005102 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005103 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08005104 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005105 node = node->rb_right;
5106 else
5107 break;
5108 }
5109 if (!node) {
5110 while (prev) {
5111 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08005112 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005113 node = prev;
5114 break;
5115 }
5116 prev = rb_next(prev);
5117 }
5118 }
5119 while (node) {
5120 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08005121 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04005122 inode = igrab(&entry->vfs_inode);
5123 if (inode) {
5124 spin_unlock(&root->inode_lock);
5125 if (atomic_read(&inode->i_count) > 1)
5126 d_prune_aliases(inode);
5127 /*
Al Viro45321ac2010-06-07 13:43:19 -04005128 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04005129 * the inode cache when its usage count
5130 * hits zero.
5131 */
5132 iput(inode);
5133 cond_resched();
5134 spin_lock(&root->inode_lock);
5135 goto again;
5136 }
5137
5138 if (cond_resched_lock(&root->inode_lock))
5139 goto again;
5140
5141 node = rb_next(node);
5142 }
5143 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005144}
5145
Chris Masone02119d2008-09-05 16:13:11 -04005146static int btrfs_init_locked_inode(struct inode *inode, void *p)
5147{
5148 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005149 inode->i_ino = args->location->objectid;
5150 memcpy(&BTRFS_I(inode)->location, args->location,
5151 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005152 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005153 return 0;
5154}
5155
5156static int btrfs_find_actor(struct inode *inode, void *opaque)
5157{
5158 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005159 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005160 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005161}
5162
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005163static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005164 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005165 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005166{
5167 struct inode *inode;
5168 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005169 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005170
Chris Mason90d3e592014-01-09 17:28:00 -08005171 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005172 args.root = root;
5173
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005174 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005175 btrfs_init_locked_inode,
5176 (void *)&args);
5177 return inode;
5178}
5179
Balaji Rao1a54ef82008-07-21 02:01:04 +05305180/* Get an inode object given its location and corresponding root.
5181 * Returns in *is_new if the inode was read from disk
5182 */
5183struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005184 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305185{
5186 struct inode *inode;
5187
Chris Mason90d3e592014-01-09 17:28:00 -08005188 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305189 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005190 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305191
5192 if (inode->i_state & I_NEW) {
Balaji Rao1a54ef82008-07-21 02:01:04 +05305193 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005194 if (!is_bad_inode(inode)) {
5195 inode_tree_add(inode);
5196 unlock_new_inode(inode);
5197 if (new)
5198 *new = 1;
5199 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005200 unlock_new_inode(inode);
5201 iput(inode);
5202 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005203 }
5204 }
5205
Balaji Rao1a54ef82008-07-21 02:01:04 +05305206 return inode;
5207}
5208
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005209static struct inode *new_simple_dir(struct super_block *s,
5210 struct btrfs_key *key,
5211 struct btrfs_root *root)
5212{
5213 struct inode *inode = new_inode(s);
5214
5215 if (!inode)
5216 return ERR_PTR(-ENOMEM);
5217
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005218 BTRFS_I(inode)->root = root;
5219 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005220 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005221
5222 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005223 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005224 inode->i_fop = &simple_dir_operations;
5225 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
5226 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
5227
5228 return inode;
5229}
5230
Chris Mason3de45862008-11-17 21:02:50 -05005231struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005232{
Chris Masond3977122009-01-05 21:25:51 -05005233 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005234 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005235 struct btrfs_root *sub_root = root;
5236 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005237 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005238 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005239
5240 if (dentry->d_name.len > BTRFS_NAME_LEN)
5241 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005242
Jeff Layton39e3c952012-11-28 11:30:53 -05005243 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005244 if (ret < 0)
5245 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005246
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005247 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005248 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005249
5250 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005251 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005252 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005253 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005254
5255 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5256
Yan, Zheng76dda932009-09-21 16:00:26 -04005257 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005258 ret = fixup_tree_root_location(root, dir, dentry,
5259 &location, &sub_root);
5260 if (ret < 0) {
5261 if (ret != -ENOENT)
5262 inode = ERR_PTR(ret);
5263 else
5264 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5265 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005266 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005267 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005268 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
5269
Julia Lawall34d19ba2011-01-24 19:55:19 +00005270 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00005271 down_read(&root->fs_info->cleanup_work_sem);
5272 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005273 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005274 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005275 if (ret) {
5276 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005277 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005278 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005279 }
5280
Chris Mason3de45862008-11-17 21:02:50 -05005281 return inode;
5282}
5283
Nick Pigginfe15ce42011-01-07 17:49:23 +11005284static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005285{
5286 struct btrfs_root *root;
Li Zefan848cce02012-02-21 17:04:28 +08005287 struct inode *inode = dentry->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005288
Li Zefan848cce02012-02-21 17:04:28 +08005289 if (!inode && !IS_ROOT(dentry))
5290 inode = dentry->d_parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005291
Li Zefan848cce02012-02-21 17:04:28 +08005292 if (inode) {
5293 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005294 if (btrfs_root_refs(&root->root_item) == 0)
5295 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005296
5297 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5298 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005299 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005300 return 0;
5301}
5302
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005303static void btrfs_dentry_release(struct dentry *dentry)
5304{
Daeseok Youn944a4512014-04-14 15:37:02 +09005305 kfree(dentry->d_fsdata);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005306}
5307
Chris Mason3de45862008-11-17 21:02:50 -05005308static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005309 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005310{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005311 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005312
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005313 inode = btrfs_lookup_dentry(dir, dentry);
5314 if (IS_ERR(inode)) {
5315 if (PTR_ERR(inode) == -ENOENT)
5316 inode = NULL;
5317 else
5318 return ERR_CAST(inode);
5319 }
5320
Josef Bacik3a0dfa62014-02-14 13:43:48 -05005321 return d_materialise_unique(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005322}
5323
Miao Xie16cdcec2011-04-22 18:12:22 +08005324unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005325 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5326};
5327
Al Viro9cdda8d2013-05-22 16:48:09 -04005328static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005329{
Al Viro9cdda8d2013-05-22 16:48:09 -04005330 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005331 struct btrfs_root *root = BTRFS_I(inode)->root;
5332 struct btrfs_item *item;
5333 struct btrfs_dir_item *di;
5334 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005335 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005336 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005337 struct list_head ins_list;
5338 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005339 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005340 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005341 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005342 unsigned char d_type;
5343 int over = 0;
5344 u32 di_cur;
5345 u32 di_total;
5346 u32 di_len;
5347 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005348 char tmp_name[32];
5349 char *name_ptr;
5350 int name_len;
Al Viro9cdda8d2013-05-22 16:48:09 -04005351 int is_curr = 0; /* ctx->pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04005352
5353 /* FIXME, use a real flag for deciding about the key type */
5354 if (root->fs_info->tree_root == root)
5355 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005356
Al Viro9cdda8d2013-05-22 16:48:09 -04005357 if (!dir_emit_dots(file, ctx))
5358 return 0;
5359
David Woodhouse49593bf2008-08-17 17:08:36 +01005360 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005361 if (!path)
5362 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005363
Josef Bacik026fd312011-05-13 10:32:11 -04005364 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005365
Miao Xie16cdcec2011-04-22 18:12:22 +08005366 if (key_type == BTRFS_DIR_INDEX_KEY) {
5367 INIT_LIST_HEAD(&ins_list);
5368 INIT_LIST_HEAD(&del_list);
5369 btrfs_get_delayed_items(inode, &ins_list, &del_list);
5370 }
5371
David Sterba962a2982014-06-04 18:41:45 +02005372 key.type = key_type;
Al Viro9cdda8d2013-05-22 16:48:09 -04005373 key.offset = ctx->pos;
Li Zefan33345d012011-04-20 10:31:50 +08005374 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005375
Chris Mason39279cc2007-06-12 06:35:45 -04005376 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5377 if (ret < 0)
5378 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005379
5380 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005381 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005382 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005383 if (slot >= btrfs_header_nritems(leaf)) {
5384 ret = btrfs_next_leaf(root, path);
5385 if (ret < 0)
5386 goto err;
5387 else if (ret > 0)
5388 break;
5389 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005390 }
Chris Mason3de45862008-11-17 21:02:50 -05005391
Ross Kirkdd3cc162013-09-16 15:58:09 +01005392 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04005393 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5394
5395 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005396 break;
David Sterba962a2982014-06-04 18:41:45 +02005397 if (found_key.type != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04005398 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005399 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005400 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08005401 if (key_type == BTRFS_DIR_INDEX_KEY &&
5402 btrfs_should_delete_dir_index(&del_list,
5403 found_key.offset))
5404 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005405
Al Viro9cdda8d2013-05-22 16:48:09 -04005406 ctx->pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08005407 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005408
Chris Mason39279cc2007-06-12 06:35:45 -04005409 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
5410 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005411 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01005412
5413 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04005414 struct btrfs_key location;
5415
Josef Bacik22a94d42011-03-16 16:47:17 -04005416 if (verify_dir_item(root, leaf, di))
5417 break;
5418
Chris Mason5f39d392007-10-15 16:14:19 -04005419 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01005420 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04005421 name_ptr = tmp_name;
5422 } else {
5423 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01005424 if (!name_ptr) {
5425 ret = -ENOMEM;
5426 goto err;
5427 }
Chris Mason5f39d392007-10-15 16:14:19 -04005428 }
5429 read_extent_buffer(leaf, name_ptr,
5430 (unsigned long)(di + 1), name_len);
5431
5432 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5433 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05005434
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005435
Chris Mason3de45862008-11-17 21:02:50 -05005436 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01005437 * skip it.
5438 *
5439 * In contrast to old kernels, we insert the snapshot's
5440 * dir item and dir index after it has been created, so
5441 * we won't find a reference to our own snapshot. We
5442 * still keep the following code for backward
5443 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05005444 */
5445 if (location.type == BTRFS_ROOT_ITEM_KEY &&
5446 location.objectid == root->root_key.objectid) {
5447 over = 0;
5448 goto skip;
5449 }
Al Viro9cdda8d2013-05-22 16:48:09 -04005450 over = !dir_emit(ctx, name_ptr, name_len,
5451 location.objectid, d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04005452
Chris Mason3de45862008-11-17 21:02:50 -05005453skip:
Chris Mason5f39d392007-10-15 16:14:19 -04005454 if (name_ptr != tmp_name)
5455 kfree(name_ptr);
5456
Chris Mason39279cc2007-06-12 06:35:45 -04005457 if (over)
5458 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05005459 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01005460 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04005461 di_cur += di_len;
5462 di = (struct btrfs_dir_item *)((char *)di + di_len);
5463 }
Li Zefanb9e03af2011-03-23 10:43:58 +08005464next:
5465 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005466 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005467
Miao Xie16cdcec2011-04-22 18:12:22 +08005468 if (key_type == BTRFS_DIR_INDEX_KEY) {
5469 if (is_curr)
Al Viro9cdda8d2013-05-22 16:48:09 -04005470 ctx->pos++;
5471 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005472 if (ret)
5473 goto nopos;
5474 }
5475
David Woodhouse49593bf2008-08-17 17:08:36 +01005476 /* Reached end of directory/root. Bump pos past the last item. */
Zach Browndb62efb2013-07-11 16:19:42 -07005477 ctx->pos++;
5478
5479 /*
5480 * Stop new entries from being returned after we return the last
5481 * entry.
5482 *
5483 * New directory entries are assigned a strictly increasing
5484 * offset. This means that new entries created during readdir
5485 * are *guaranteed* to be seen in the future by that readdir.
5486 * This has broken buggy programs which operate on names as
5487 * they're returned by readdir. Until we re-use freed offsets
5488 * we have this hack to stop new entries from being returned
5489 * under the assumption that they'll never reach this huge
5490 * offset.
5491 *
5492 * This is being careful not to overflow 32bit loff_t unless the
5493 * last entry requires it because doing so has broken 32bit apps
5494 * in the past.
5495 */
5496 if (key_type == BTRFS_DIR_INDEX_KEY) {
5497 if (ctx->pos >= INT_MAX)
5498 ctx->pos = LLONG_MAX;
5499 else
5500 ctx->pos = INT_MAX;
5501 }
Chris Mason39279cc2007-06-12 06:35:45 -04005502nopos:
5503 ret = 0;
5504err:
Miao Xie16cdcec2011-04-22 18:12:22 +08005505 if (key_type == BTRFS_DIR_INDEX_KEY)
5506 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005507 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005508 return ret;
5509}
5510
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005511int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005512{
5513 struct btrfs_root *root = BTRFS_I(inode)->root;
5514 struct btrfs_trans_handle *trans;
5515 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005516 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005517
Josef Bacik72ac3c02012-05-23 14:13:11 -04005518 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005519 return 0;
5520
Liu Bo83eea1f2012-07-10 05:28:39 -06005521 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08005522 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005523
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005524 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005525 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005526 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005527 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005528 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005529 if (IS_ERR(trans))
5530 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06005531 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005532 }
5533 return ret;
5534}
5535
5536/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005537 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005538 * inode changes. But, it is most likely to find the inode in cache.
5539 * FIXME, needs more benchmarking...there are no reasons other than performance
5540 * to keep or drop this code.
5541 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005542static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005543{
5544 struct btrfs_root *root = BTRFS_I(inode)->root;
5545 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005546 int ret;
5547
Josef Bacik72ac3c02012-05-23 14:13:11 -04005548 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005549 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005550
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005551 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005552 if (IS_ERR(trans))
5553 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005554
5555 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005556 if (ret && ret == -ENOSPC) {
5557 /* whoops, lets try again with the full transaction */
5558 btrfs_end_transaction(trans, root);
5559 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005560 if (IS_ERR(trans))
5561 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005562
Chris Mason94b60442010-05-26 11:02:00 -04005563 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005564 }
Chris Mason39279cc2007-06-12 06:35:45 -04005565 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08005566 if (BTRFS_I(inode)->delayed_node)
5567 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005568
5569 return ret;
5570}
5571
5572/*
5573 * This is a copy of file_update_time. We need this so we can return error on
5574 * ENOSPC for updating the inode in the case of file write and mmap writes.
5575 */
Josef Bacike41f9412012-03-26 09:46:47 -04005576static int btrfs_update_time(struct inode *inode, struct timespec *now,
5577 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005578{
Alexander Block2bc5565282012-06-15 09:49:33 +02005579 struct btrfs_root *root = BTRFS_I(inode)->root;
5580
5581 if (btrfs_root_readonly(root))
5582 return -EROFS;
5583
Josef Bacike41f9412012-03-26 09:46:47 -04005584 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005585 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04005586 if (flags & S_CTIME)
5587 inode->i_ctime = *now;
5588 if (flags & S_MTIME)
5589 inode->i_mtime = *now;
5590 if (flags & S_ATIME)
5591 inode->i_atime = *now;
5592 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005593}
5594
Chris Masond352ac62008-09-29 15:18:18 -04005595/*
5596 * find the highest existing sequence number in a directory
5597 * and then set the in-memory index_cnt variable to reflect
5598 * free sequence numbers
5599 */
Josef Bacikaec74772008-07-24 12:12:38 -04005600static int btrfs_set_inode_index_count(struct inode *inode)
5601{
5602 struct btrfs_root *root = BTRFS_I(inode)->root;
5603 struct btrfs_key key, found_key;
5604 struct btrfs_path *path;
5605 struct extent_buffer *leaf;
5606 int ret;
5607
Li Zefan33345d012011-04-20 10:31:50 +08005608 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02005609 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04005610 key.offset = (u64)-1;
5611
5612 path = btrfs_alloc_path();
5613 if (!path)
5614 return -ENOMEM;
5615
5616 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5617 if (ret < 0)
5618 goto out;
5619 /* FIXME: we should be able to handle this */
5620 if (ret == 0)
5621 goto out;
5622 ret = 0;
5623
5624 /*
5625 * MAGIC NUMBER EXPLANATION:
5626 * since we search a directory based on f_pos we have to start at 2
5627 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5628 * else has to start at 2
5629 */
5630 if (path->slots[0] == 0) {
5631 BTRFS_I(inode)->index_cnt = 2;
5632 goto out;
5633 }
5634
5635 path->slots[0]--;
5636
5637 leaf = path->nodes[0];
5638 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5639
Li Zefan33345d012011-04-20 10:31:50 +08005640 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02005641 found_key.type != BTRFS_DIR_INDEX_KEY) {
Josef Bacikaec74772008-07-24 12:12:38 -04005642 BTRFS_I(inode)->index_cnt = 2;
5643 goto out;
5644 }
5645
5646 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
5647out:
5648 btrfs_free_path(path);
5649 return ret;
5650}
5651
Chris Masond352ac62008-09-29 15:18:18 -04005652/*
5653 * helper to find a free sequence number in a given directory. This current
5654 * code is very simple, later versions will do smarter things in the btree
5655 */
Chris Mason3de45862008-11-17 21:02:50 -05005656int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04005657{
5658 int ret = 0;
5659
5660 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08005661 ret = btrfs_inode_delayed_dir_index_count(dir);
5662 if (ret) {
5663 ret = btrfs_set_inode_index_count(dir);
5664 if (ret)
5665 return ret;
5666 }
Josef Bacikaec74772008-07-24 12:12:38 -04005667 }
5668
Chris Mason00e4e6b2008-08-05 11:18:09 -04005669 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04005670 BTRFS_I(dir)->index_cnt++;
5671
5672 return ret;
5673}
5674
Chris Masonb0d5d102014-09-08 13:08:51 -07005675static int btrfs_insert_inode_locked(struct inode *inode)
5676{
5677 struct btrfs_iget_args args;
5678 args.location = &BTRFS_I(inode)->location;
5679 args.root = BTRFS_I(inode)->root;
5680
5681 return insert_inode_locked4(inode,
5682 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
5683 btrfs_find_actor, &args);
5684}
5685
Chris Mason39279cc2007-06-12 06:35:45 -04005686static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5687 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04005688 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05005689 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04005690 u64 ref_objectid, u64 objectid,
5691 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04005692{
5693 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005694 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04005695 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04005696 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05005697 struct btrfs_inode_ref *ref;
5698 struct btrfs_key key[2];
5699 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005700 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05005701 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04005702 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005703
Chris Mason5f39d392007-10-15 16:14:19 -04005704 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005705 if (!path)
5706 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04005707
Chris Mason39279cc2007-06-12 06:35:45 -04005708 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005709 if (!inode) {
5710 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005711 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005712 }
Chris Mason39279cc2007-06-12 06:35:45 -04005713
Li Zefan581bb052011-04-20 10:06:11 +08005714 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01005715 * O_TMPFILE, set link count to 0, so that after this point,
5716 * we fill in an inode item with the correct link count.
5717 */
5718 if (!name)
5719 set_nlink(inode, 0);
5720
5721 /*
Li Zefan581bb052011-04-20 10:06:11 +08005722 * we have to initialize this early, so we can reclaim the inode
5723 * number if we fail afterwards in this function.
5724 */
5725 inode->i_ino = objectid;
5726
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005727 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00005728 trace_btrfs_inode_request(dir);
5729
Chris Mason3de45862008-11-17 21:02:50 -05005730 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04005731 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005732 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005733 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005734 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04005735 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005736 } else if (dir) {
5737 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04005738 }
5739 /*
5740 * index_cnt is ignored for everything but a dir,
5741 * btrfs_get_inode_index_count has an explanation for the magic
5742 * number
5743 */
5744 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08005745 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04005746 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04005747 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00005748 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db2b2007-08-27 16:49:44 -04005749
Josef Bacik5dc562c2012-08-17 13:14:17 -04005750 /*
5751 * We could have gotten an inode number from somebody who was fsynced
5752 * and then removed in this same transaction, so let's just set full
5753 * sync since it will be a full sync anyway and this will blow away the
5754 * old info in the log.
5755 */
5756 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
5757
Chris Mason9c583092008-01-29 15:15:18 -05005758 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02005759 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05005760 key[0].offset = 0;
5761
Chris Mason9c583092008-01-29 15:15:18 -05005762 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005763
5764 if (name) {
5765 /*
5766 * Start new inodes with an inode_ref. This is slightly more
5767 * efficient for small numbers of hard links since they will
5768 * be packed into one item. Extended refs will kick in if we
5769 * add more hard links than can fit in the ref item.
5770 */
5771 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02005772 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005773 key[1].offset = ref_objectid;
5774
5775 sizes[1] = name_len + sizeof(*ref);
5776 }
Chris Mason9c583092008-01-29 15:15:18 -05005777
Chris Masonb0d5d102014-09-08 13:08:51 -07005778 location = &BTRFS_I(inode)->location;
5779 location->objectid = objectid;
5780 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02005781 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07005782
5783 ret = btrfs_insert_inode_locked(inode);
5784 if (ret < 0)
5785 goto fail;
5786
Chris Masonb9473432009-03-13 11:00:37 -04005787 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005788 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05005789 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07005790 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04005791
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03005792 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04005793 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005794 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04005795 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
5796 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06005797 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
5798 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04005799 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05005800
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005801 if (name) {
5802 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
5803 struct btrfs_inode_ref);
5804 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
5805 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
5806 ptr = (unsigned long)(ref + 1);
5807 write_extent_buffer(path->nodes[0], name, ptr, name_len);
5808 }
Chris Mason9c583092008-01-29 15:15:18 -05005809
Chris Mason5f39d392007-10-15 16:14:19 -04005810 btrfs_mark_buffer_dirty(path->nodes[0]);
5811 btrfs_free_path(path);
5812
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005813 btrfs_inherit_iflags(inode, dir);
5814
Al Viro569254b2011-07-24 17:08:40 -04005815 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04005816 if (btrfs_test_opt(root, NODATASUM))
5817 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo213490b2012-09-11 08:33:50 -06005818 if (btrfs_test_opt(root, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05005819 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
5820 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04005821 }
5822
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005823 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00005824
5825 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04005826 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00005827
Alexander Block8ea05e32012-07-25 17:35:53 +02005828 btrfs_update_root_times(trans, root);
5829
Filipe David Borba Manana63541922014-01-07 11:47:46 +00005830 ret = btrfs_inode_inherit_props(trans, inode, dir);
5831 if (ret)
5832 btrfs_err(root->fs_info,
5833 "error inheriting props for ino %llu (root %llu): %d",
5834 btrfs_ino(inode), root->root_key.objectid, ret);
5835
Chris Mason39279cc2007-06-12 06:35:45 -04005836 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07005837
5838fail_unlock:
5839 unlock_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005840fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005841 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04005842 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04005843 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005844 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005845 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005846}
5847
5848static inline u8 btrfs_inode_type(struct inode *inode)
5849{
5850 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
5851}
5852
Chris Masond352ac62008-09-29 15:18:18 -04005853/*
5854 * utility function to add 'inode' into 'parent_inode' with
5855 * a give name and a given sequence number.
5856 * if 'add_backref' is true, also insert a backref from the
5857 * inode to the parent directory.
5858 */
Chris Masone02119d2008-09-05 16:13:11 -04005859int btrfs_add_link(struct btrfs_trans_handle *trans,
5860 struct inode *parent_inode, struct inode *inode,
5861 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005862{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005863 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005864 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04005865 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08005866 u64 ino = btrfs_ino(inode);
5867 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005868
Li Zefan33345d012011-04-20 10:31:50 +08005869 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005870 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
5871 } else {
Li Zefan33345d012011-04-20 10:31:50 +08005872 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02005873 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005874 key.offset = 0;
5875 }
Chris Mason39279cc2007-06-12 06:35:45 -04005876
Li Zefan33345d012011-04-20 10:31:50 +08005877 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005878 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
5879 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08005880 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005881 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08005882 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
5883 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005884 }
5885
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005886 /* Nothing to clean up yet */
5887 if (ret)
5888 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005889
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005890 ret = btrfs_insert_dir_item(trans, root, name, name_len,
5891 parent_inode, &key,
5892 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05005893 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005894 goto fail_dir_item;
5895 else if (ret) {
5896 btrfs_abort_transaction(trans, root, ret);
5897 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005898 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005899
5900 btrfs_i_size_write(parent_inode, parent_inode->i_size +
5901 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005902 inode_inc_iversion(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005903 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
5904 ret = btrfs_update_inode(trans, root, parent_inode);
5905 if (ret)
5906 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005907 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05005908
5909fail_dir_item:
5910 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
5911 u64 local_index;
5912 int err;
5913 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
5914 key.objectid, root->root_key.objectid,
5915 parent_ino, &local_index, name, name_len);
5916
5917 } else if (add_backref) {
5918 u64 local_index;
5919 int err;
5920
5921 err = btrfs_del_inode_ref(trans, root, name, name_len,
5922 ino, parent_ino, &local_index);
5923 }
5924 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005925}
5926
5927static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00005928 struct inode *dir, struct dentry *dentry,
5929 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005930{
Josef Bacika1b075d2010-11-19 20:36:11 +00005931 int err = btrfs_add_link(trans, dir, inode,
5932 dentry->d_name.name, dentry->d_name.len,
5933 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005934 if (err > 0)
5935 err = -EEXIST;
5936 return err;
5937}
5938
Josef Bacik618e21d2007-07-11 10:18:17 -04005939static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04005940 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04005941{
5942 struct btrfs_trans_handle *trans;
5943 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005944 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04005945 int err;
5946 int drop_inode = 0;
5947 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005948 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04005949
5950 if (!new_valid_dev(rdev))
5951 return -EINVAL;
5952
Josef Bacik9ed74f22009-09-11 16:12:44 -04005953 /*
5954 * 2 for inode item and ref
5955 * 2 for dir items
5956 * 1 for xattr if selinux is on
5957 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005958 trans = btrfs_start_transaction(root, 5);
5959 if (IS_ERR(trans))
5960 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05005961
Li Zefan581bb052011-04-20 10:06:11 +08005962 err = btrfs_find_free_ino(root, &objectid);
5963 if (err)
5964 goto out_unlock;
5965
Josef Bacikaec74772008-07-24 12:12:38 -04005966 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005967 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04005968 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005969 if (IS_ERR(inode)) {
5970 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005971 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005972 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005973
Casey Schauflerad19db72011-12-15 10:09:07 -05005974 /*
5975 * If the active LSM wants to access the inode during
5976 * d_instantiate it needs these. Smack checks to see
5977 * if the filesystem supports xattrs by looking at the
5978 * ops vector.
5979 */
Casey Schauflerad19db72011-12-15 10:09:07 -05005980 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07005981 init_special_inode(inode, inode->i_mode, rdev);
5982
5983 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04005984 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07005985 goto out_unlock_inode;
5986
5987 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
5988 if (err) {
5989 goto out_unlock_inode;
5990 } else {
Yan1b4ab1b2007-08-29 09:11:44 -04005991 btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07005992 unlock_new_inode(inode);
Al Viro08c422c2011-12-23 07:58:13 -05005993 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005994 }
Chris Masonb0d5d102014-09-08 13:08:51 -07005995
Josef Bacik618e21d2007-07-11 10:18:17 -04005996out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005997 btrfs_end_transaction(trans, root);
Josef Bacikc581afc2014-02-06 16:06:06 -05005998 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00005999 btrfs_btree_balance_dirty(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04006000 if (drop_inode) {
6001 inode_dec_link_count(inode);
6002 iput(inode);
6003 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006004 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006005
6006out_unlock_inode:
6007 drop_inode = 1;
6008 unlock_new_inode(inode);
6009 goto out_unlock;
6010
Josef Bacik618e21d2007-07-11 10:18:17 -04006011}
6012
Chris Mason39279cc2007-06-12 06:35:45 -04006013static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006014 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006015{
6016 struct btrfs_trans_handle *trans;
6017 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006018 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006019 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006020 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006021 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006022 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006023
Josef Bacik9ed74f22009-09-11 16:12:44 -04006024 /*
6025 * 2 for inode item and ref
6026 * 2 for dir items
6027 * 1 for xattr if selinux is on
6028 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006029 trans = btrfs_start_transaction(root, 5);
6030 if (IS_ERR(trans))
6031 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006032
Li Zefan581bb052011-04-20 10:06:11 +08006033 err = btrfs_find_free_ino(root, &objectid);
6034 if (err)
6035 goto out_unlock;
6036
Josef Bacikaec74772008-07-24 12:12:38 -04006037 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006038 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04006039 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006040 if (IS_ERR(inode)) {
6041 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006042 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006043 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006044 drop_inode_on_err = 1;
Casey Schauflerad19db72011-12-15 10:09:07 -05006045 /*
6046 * If the active LSM wants to access the inode during
6047 * d_instantiate it needs these. Smack checks to see
6048 * if the filesystem supports xattrs by looking at the
6049 * ops vector.
6050 */
6051 inode->i_fop = &btrfs_file_operations;
6052 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006053 inode->i_mapping->a_ops = &btrfs_aops;
6054 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
6055
6056 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6057 if (err)
6058 goto out_unlock_inode;
6059
6060 err = btrfs_update_inode(trans, root, inode);
6061 if (err)
6062 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05006063
Josef Bacika1b075d2010-11-19 20:36:11 +00006064 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006065 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006066 goto out_unlock_inode;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006067
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006068 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006069 unlock_new_inode(inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006070 d_instantiate(dentry, inode);
6071
Chris Mason39279cc2007-06-12 06:35:45 -04006072out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006073 btrfs_end_transaction(trans, root);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006074 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006075 inode_dec_link_count(inode);
6076 iput(inode);
6077 }
Josef Bacikc581afc2014-02-06 16:06:06 -05006078 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006079 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006080 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006081
6082out_unlock_inode:
6083 unlock_new_inode(inode);
6084 goto out_unlock;
6085
Chris Mason39279cc2007-06-12 06:35:45 -04006086}
6087
6088static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6089 struct dentry *dentry)
6090{
6091 struct btrfs_trans_handle *trans;
6092 struct btrfs_root *root = BTRFS_I(dir)->root;
6093 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006094 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006095 int err;
6096 int drop_inode = 0;
6097
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006098 /* do not allow sys_link's with other subvols of the same device */
6099 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006100 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006101
Mark Fashehf1863732012-08-08 11:32:27 -07006102 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006103 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006104
Chris Mason3de45862008-11-17 21:02:50 -05006105 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006106 if (err)
6107 goto fail;
6108
Yan, Zhenga22285a2010-05-16 10:48:46 -04006109 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006110 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006111 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006112 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04006113 */
Miao Xie7e6b6462011-02-18 09:21:17 +00006114 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006115 if (IS_ERR(trans)) {
6116 err = PTR_ERR(trans);
6117 goto fail;
6118 }
Chris Mason5f39d392007-10-15 16:14:19 -04006119
Miao Xie67de1172013-12-26 13:07:06 +08006120 /* There are several dir indexes for this inode, clear the cache. */
6121 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006122 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006123 inode_inc_iversion(inode);
Miao Xie31534952011-04-13 13:19:21 +08006124 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6ee2010-10-23 11:11:40 -04006125 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006126 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006127
Josef Bacika1b075d2010-11-19 20:36:11 +00006128 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006129
Yan, Zhenga5719522009-09-24 09:17:31 -04006130 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006131 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006132 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006133 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006134 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006135 if (err)
6136 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006137 if (inode->i_nlink == 1) {
6138 /*
6139 * If new hard link count is 1, it's a file created
6140 * with open(2) O_TMPFILE flag.
6141 */
6142 err = btrfs_orphan_del(trans, inode);
6143 if (err)
6144 goto fail;
6145 }
Al Viro08c422c2011-12-23 07:58:13 -05006146 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00006147 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006148 }
Chris Mason39279cc2007-06-12 06:35:45 -04006149
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006150 btrfs_end_transaction(trans, root);
Josef Bacikc581afc2014-02-06 16:06:06 -05006151 btrfs_balance_delayed_items(root);
Chris Mason1832a6d2007-12-21 16:27:21 -05006152fail:
Chris Mason39279cc2007-06-12 06:35:45 -04006153 if (drop_inode) {
6154 inode_dec_link_count(inode);
6155 iput(inode);
6156 }
Liu Bob53d3f52012-11-14 14:34:34 +00006157 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006158 return err;
6159}
6160
Al Viro18bb1db2011-07-26 01:41:39 -04006161static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006162{
Chris Masonb9d86662008-05-02 16:13:49 -04006163 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006164 struct btrfs_trans_handle *trans;
6165 struct btrfs_root *root = BTRFS_I(dir)->root;
6166 int err = 0;
6167 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006168 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006169 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006170
Josef Bacik9ed74f22009-09-11 16:12:44 -04006171 /*
6172 * 2 items for inode and ref
6173 * 2 items for dir items
6174 * 1 for xattr if selinux is on
6175 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006176 trans = btrfs_start_transaction(root, 5);
6177 if (IS_ERR(trans))
6178 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006179
Li Zefan581bb052011-04-20 10:06:11 +08006180 err = btrfs_find_free_ino(root, &objectid);
6181 if (err)
6182 goto out_fail;
6183
Josef Bacikaec74772008-07-24 12:12:38 -04006184 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006185 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04006186 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006187 if (IS_ERR(inode)) {
6188 err = PTR_ERR(inode);
6189 goto out_fail;
6190 }
Chris Mason5f39d392007-10-15 16:14:19 -04006191
Chris Mason39279cc2007-06-12 06:35:45 -04006192 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006193 /* these must be set before we unlock the inode */
6194 inode->i_op = &btrfs_dir_inode_operations;
6195 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006196
Eric Paris2a7dba32011-02-01 11:05:39 -05006197 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006198 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006199 goto out_fail_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006200
Chris Masondbe674a2008-07-17 12:54:05 -04006201 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006202 err = btrfs_update_inode(trans, root, inode);
6203 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006204 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006205
Josef Bacika1b075d2010-11-19 20:36:11 +00006206 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
6207 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006208 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006209 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006210
Chris Mason39279cc2007-06-12 06:35:45 -04006211 d_instantiate(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006212 /*
6213 * mkdir is special. We're unlocking after we call d_instantiate
6214 * to avoid a race with nfsd calling d_instantiate.
6215 */
6216 unlock_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006217 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006218
6219out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006220 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04006221 if (drop_on_err)
6222 iput(inode);
Josef Bacikc581afc2014-02-06 16:06:06 -05006223 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006224 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006225 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006226
6227out_fail_inode:
6228 unlock_new_inode(inode);
6229 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006230}
6231
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006232/* Find next extent map of a given extent map, caller needs to ensure locks */
6233static struct extent_map *next_extent_map(struct extent_map *em)
6234{
6235 struct rb_node *next;
6236
6237 next = rb_next(&em->rb_node);
6238 if (!next)
6239 return NULL;
6240 return container_of(next, struct extent_map, rb_node);
6241}
6242
6243static struct extent_map *prev_extent_map(struct extent_map *em)
6244{
6245 struct rb_node *prev;
6246
6247 prev = rb_prev(&em->rb_node);
6248 if (!prev)
6249 return NULL;
6250 return container_of(prev, struct extent_map, rb_node);
6251}
6252
Chris Masond352ac62008-09-29 15:18:18 -04006253/* helper for btfs_get_extent. Given an existing extent in the tree,
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006254 * the existing extent is the nearest extent to map_start,
Chris Masond352ac62008-09-29 15:18:18 -04006255 * and an extent that you want to insert, deal with overlap and insert
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006256 * the best fitted new extent into the tree.
Chris Masond352ac62008-09-29 15:18:18 -04006257 */
Chris Mason3b951512008-04-17 11:29:12 -04006258static int merge_extent_mapping(struct extent_map_tree *em_tree,
6259 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006260 struct extent_map *em,
Qu Wenruo51f395a2014-08-08 13:06:20 +08006261 u64 map_start)
Chris Mason3b951512008-04-17 11:29:12 -04006262{
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006263 struct extent_map *prev;
6264 struct extent_map *next;
6265 u64 start;
6266 u64 end;
Chris Mason3b951512008-04-17 11:29:12 -04006267 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04006268
Chris Masone6dcd2d2008-07-17 12:53:50 -04006269 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006270
6271 if (existing->start > map_start) {
6272 next = existing;
6273 prev = prev_extent_map(next);
6274 } else {
6275 prev = existing;
6276 next = next_extent_map(prev);
6277 }
6278
6279 start = prev ? extent_map_end(prev) : em->start;
6280 start = max_t(u64, start, em->start);
6281 end = next ? next->start : extent_map_end(em);
6282 end = min_t(u64, end, extent_map_end(em));
6283 start_diff = start - em->start;
6284 em->start = start;
6285 em->len = end - start;
Chris Masonc8b97812008-10-29 14:49:59 -04006286 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
6287 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006288 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04006289 em->block_len -= start_diff;
6290 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006291 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006292}
6293
Chris Masonc8b97812008-10-29 14:49:59 -04006294static noinline int uncompress_inline(struct btrfs_path *path,
6295 struct inode *inode, struct page *page,
6296 size_t pg_offset, u64 extent_offset,
6297 struct btrfs_file_extent_item *item)
6298{
6299 int ret;
6300 struct extent_buffer *leaf = path->nodes[0];
6301 char *tmp;
6302 size_t max_size;
6303 unsigned long inline_size;
6304 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006305 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006306
6307 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006308 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006309 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6310 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006311 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006312 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006313 if (!tmp)
6314 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006315 ptr = btrfs_file_extent_inline_start(item);
6316
6317 read_extent_buffer(leaf, tmp, ptr, inline_size);
6318
Chris Mason5b050f02008-11-11 09:34:41 -05006319 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006320 ret = btrfs_decompress(compress_type, tmp, page,
6321 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006322 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006323 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006324}
6325
Chris Masond352ac62008-09-29 15:18:18 -04006326/*
6327 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006328 * the ugly parts come from merging extents from the disk with the in-ram
6329 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006330 * where the in-ram extents might be locked pending data=ordered completion.
6331 *
6332 * This also copies inline extents directly into the page.
6333 */
Chris Masond3977122009-01-05 21:25:51 -05006334
Chris Masona52d9a82007-08-27 16:49:44 -04006335struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05006336 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04006337 int create)
6338{
6339 int ret;
6340 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006341 u64 extent_start = 0;
6342 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08006343 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006344 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006345 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04006346 struct btrfs_root *root = BTRFS_I(inode)->root;
6347 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006348 struct extent_buffer *leaf;
6349 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006350 struct extent_map *em = NULL;
6351 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05006352 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006353 struct btrfs_trans_handle *trans = NULL;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006354 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006355
Chris Masona52d9a82007-08-27 16:49:44 -04006356again:
Chris Mason890871b2009-09-02 16:24:52 -04006357 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006358 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006359 if (em)
6360 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006361 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006362
Chris Masona52d9a82007-08-27 16:49:44 -04006363 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006364 if (em->start > start || em->start + em->len <= start)
6365 free_extent_map(em);
6366 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006367 free_extent_map(em);
6368 else
6369 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006370 }
David Sterba172ddd62011-04-21 00:48:27 +02006371 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006372 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006373 err = -ENOMEM;
6374 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006375 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006376 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006377 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006378 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006379 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006380 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006381
6382 if (!path) {
6383 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006384 if (!path) {
6385 err = -ENOMEM;
6386 goto out;
6387 }
6388 /*
6389 * Chances are we'll be called again, so go ahead and do
6390 * readahead
6391 */
6392 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04006393 }
6394
Chris Mason179e29e2007-11-01 11:28:41 -04006395 ret = btrfs_lookup_file_extent(trans, root, path,
6396 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006397 if (ret < 0) {
6398 err = ret;
6399 goto out;
6400 }
6401
6402 if (ret != 0) {
6403 if (path->slots[0] == 0)
6404 goto not_found;
6405 path->slots[0]--;
6406 }
6407
Chris Mason5f39d392007-10-15 16:14:19 -04006408 leaf = path->nodes[0];
6409 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006410 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006411 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006412 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02006413 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04006414 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006415 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006416 /*
6417 * If we backup past the first extent we want to move forward
6418 * and see if there is an extent in front of us, otherwise we'll
6419 * say there is a hole for our whole search range which can
6420 * cause problems.
6421 */
6422 extent_end = start;
6423 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006424 }
6425
Chris Mason5f39d392007-10-15 16:14:19 -04006426 found_type = btrfs_file_extent_type(leaf, item);
6427 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04006428 if (found_type == BTRFS_FILE_EXTENT_REG ||
6429 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006430 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006431 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006432 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6433 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08006434 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Qu Wenruofda28322013-02-26 08:10:22 +00006435 extent_end = ALIGN(extent_start + size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04006436 }
Josef Bacik25a50342013-10-14 12:08:38 -04006437next:
Yan Zheng9036c102008-10-30 14:19:41 -04006438 if (start >= extent_end) {
6439 path->slots[0]++;
6440 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6441 ret = btrfs_next_leaf(root, path);
6442 if (ret < 0) {
6443 err = ret;
6444 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006445 }
Yan Zheng9036c102008-10-30 14:19:41 -04006446 if (ret > 0)
6447 goto not_found;
6448 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006449 }
Yan Zheng9036c102008-10-30 14:19:41 -04006450 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6451 if (found_key.objectid != objectid ||
6452 found_key.type != BTRFS_EXTENT_DATA_KEY)
6453 goto not_found;
6454 if (start + len <= found_key.offset)
6455 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006456 if (start > found_key.offset)
6457 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04006458 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006459 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006460 em->len = found_key.offset - start;
6461 goto not_found_em;
6462 }
6463
Filipe Manana7ffbb592014-06-09 03:48:05 +01006464 btrfs_extent_item_to_extent_map(inode, path, item, new_inline, em);
6465
Yan Zhengd899e052008-10-30 14:25:28 -04006466 if (found_type == BTRFS_FILE_EXTENT_REG ||
6467 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006468 goto insert;
6469 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006470 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006471 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006472 size_t size;
6473 size_t extent_offset;
6474 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006475
Filipe Manana7ffbb592014-06-09 03:48:05 +01006476 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04006477 goto out;
Yan689f9342007-10-29 11:41:07 -04006478
Chris Mason514ac8a2014-01-03 21:07:00 -08006479 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04006480 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05006481 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04006482 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006483 em->start = extent_start + extent_offset;
Qu Wenruofda28322013-02-26 08:10:22 +00006484 em->len = ALIGN(copy_size, root->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006485 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006486 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006487 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006488 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006489 if (btrfs_file_extent_compression(leaf, item) !=
6490 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006491 ret = uncompress_inline(path, inode, page,
6492 pg_offset,
6493 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04006494 if (ret) {
6495 err = ret;
6496 goto out;
6497 }
Chris Masonc8b97812008-10-29 14:49:59 -04006498 } else {
6499 map = kmap(page);
6500 read_extent_buffer(leaf, map + pg_offset, ptr,
6501 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04006502 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
6503 memset(map + pg_offset + copy_size, 0,
6504 PAGE_CACHE_SIZE - pg_offset -
6505 copy_size);
6506 }
Chris Masonc8b97812008-10-29 14:49:59 -04006507 kunmap(page);
6508 }
Chris Mason179e29e2007-11-01 11:28:41 -04006509 flush_dcache_page(page);
6510 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006511 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006512 if (!trans) {
6513 kunmap(page);
6514 free_extent_map(em);
6515 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006516
David Sterbab3b4aa72011-04-21 01:20:15 +02006517 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006518 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006519
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006520 if (IS_ERR(trans))
6521 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006522 goto again;
6523 }
Chris Masonc8b97812008-10-29 14:49:59 -04006524 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006525 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006526 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006527 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006528 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006529 }
Chris Masond1310b22008-01-24 16:13:08 -05006530 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006531 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006532 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04006533 }
6534not_found:
6535 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006536 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006537 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006538not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04006539 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04006540 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006541insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006542 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006543 if (em->start > start || extent_map_end(em) <= start) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006544 btrfs_err(root->fs_info, "bad extent! em: [%llu %llu] passed [%llu %llu]",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006545 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04006546 err = -EIO;
6547 goto out;
6548 }
Chris Masond1310b22008-01-24 16:13:08 -05006549
6550 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006551 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006552 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006553 /* it is possible that someone inserted the extent into the tree
6554 * while we had the lock dropped. It is also possible that
6555 * an overlapping map exists in the tree
6556 */
Chris Masona52d9a82007-08-27 16:49:44 -04006557 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04006558 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006559
6560 ret = 0;
6561
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006562 existing = search_extent_mapping(em_tree, start, len);
6563 /*
6564 * existing will always be non-NULL, since there must be
6565 * extent causing the -EEXIST.
6566 */
6567 if (start >= extent_map_end(existing) ||
Qu Wenruo32be3a12014-09-22 09:13:03 +08006568 start <= existing->start) {
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006569 /*
6570 * The existing extent map is the one nearest to
6571 * the [start, start + len) range which overlaps
6572 */
6573 err = merge_extent_mapping(em_tree, existing,
6574 em, start);
Chris Masone1c4b742008-04-22 13:26:46 -04006575 free_extent_map(existing);
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006576 if (err) {
Chris Mason3b951512008-04-17 11:29:12 -04006577 free_extent_map(em);
6578 em = NULL;
6579 }
6580 } else {
6581 free_extent_map(em);
6582 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006583 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006584 }
Chris Masona52d9a82007-08-27 16:49:44 -04006585 }
Chris Mason890871b2009-09-02 16:24:52 -04006586 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006587out:
liubo1abe9b82011-03-24 11:18:59 +00006588
Steven Rostedt4cd85872013-11-14 22:57:29 -05006589 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00006590
Chris Masonf4219502008-07-22 11:18:09 -04006591 if (path)
6592 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04006593 if (trans) {
6594 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05006595 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04006596 err = ret;
6597 }
Chris Masona52d9a82007-08-27 16:49:44 -04006598 if (err) {
6599 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04006600 return ERR_PTR(err);
6601 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006602 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04006603 return em;
6604}
6605
Chris Masonec29ed52011-02-23 16:23:20 -05006606struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
6607 size_t pg_offset, u64 start, u64 len,
6608 int create)
6609{
6610 struct extent_map *em;
6611 struct extent_map *hole_em = NULL;
6612 u64 range_start = start;
6613 u64 end;
6614 u64 found;
6615 u64 found_end;
6616 int err = 0;
6617
6618 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
6619 if (IS_ERR(em))
6620 return em;
6621 if (em) {
6622 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00006623 * if our em maps to
6624 * - a hole or
6625 * - a pre-alloc extent,
6626 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05006627 */
Liu Bof9e4fb52013-01-07 10:10:12 +00006628 if (em->block_start != EXTENT_MAP_HOLE &&
6629 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05006630 return em;
6631 else
6632 hole_em = em;
6633 }
6634
6635 /* check to see if we've wrapped (len == -1 or similar) */
6636 end = start + len;
6637 if (end < start)
6638 end = (u64)-1;
6639 else
6640 end -= 1;
6641
6642 em = NULL;
6643
6644 /* ok, we didn't find anything, lets look for delalloc */
6645 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
6646 end, len, EXTENT_DELALLOC, 1);
6647 found_end = range_start + found;
6648 if (found_end < range_start)
6649 found_end = (u64)-1;
6650
6651 /*
6652 * we didn't find anything useful, return
6653 * the original results from get_extent()
6654 */
6655 if (range_start > end || found_end <= start) {
6656 em = hole_em;
6657 hole_em = NULL;
6658 goto out;
6659 }
6660
6661 /* adjust the range_start to make sure it doesn't
6662 * go backwards from the start they passed in
6663 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05306664 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05006665 found = found_end - range_start;
6666
6667 if (found > 0) {
6668 u64 hole_start = start;
6669 u64 hole_len = len;
6670
David Sterba172ddd62011-04-21 00:48:27 +02006671 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05006672 if (!em) {
6673 err = -ENOMEM;
6674 goto out;
6675 }
6676 /*
6677 * when btrfs_get_extent can't find anything it
6678 * returns one huge hole
6679 *
6680 * make sure what it found really fits our range, and
6681 * adjust to make sure it is based on the start from
6682 * the caller
6683 */
6684 if (hole_em) {
6685 u64 calc_end = extent_map_end(hole_em);
6686
6687 if (calc_end <= start || (hole_em->start > end)) {
6688 free_extent_map(hole_em);
6689 hole_em = NULL;
6690 } else {
6691 hole_start = max(hole_em->start, start);
6692 hole_len = calc_end - hole_start;
6693 }
6694 }
6695 em->bdev = NULL;
6696 if (hole_em && range_start > hole_start) {
6697 /* our hole starts before our delalloc, so we
6698 * have to return just the parts of the hole
6699 * that go until the delalloc starts
6700 */
6701 em->len = min(hole_len,
6702 range_start - hole_start);
6703 em->start = hole_start;
6704 em->orig_start = hole_start;
6705 /*
6706 * don't adjust block start at all,
6707 * it is fixed at EXTENT_MAP_HOLE
6708 */
6709 em->block_start = hole_em->block_start;
6710 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00006711 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
6712 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05006713 } else {
6714 em->start = range_start;
6715 em->len = found;
6716 em->orig_start = range_start;
6717 em->block_start = EXTENT_MAP_DELALLOC;
6718 em->block_len = found;
6719 }
6720 } else if (hole_em) {
6721 return hole_em;
6722 }
6723out:
6724
6725 free_extent_map(hole_em);
6726 if (err) {
6727 free_extent_map(em);
6728 return ERR_PTR(err);
6729 }
6730 return em;
6731}
6732
Josef Bacik4b46fce2010-05-23 11:00:55 -04006733static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
6734 u64 start, u64 len)
6735{
6736 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04006737 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006738 struct btrfs_key ins;
6739 u64 alloc_hint;
6740 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006741
Josef Bacik4b46fce2010-05-23 11:00:55 -04006742 alloc_hint = get_extent_allocation_hint(inode, start, len);
Josef Bacik00361582013-08-14 14:02:47 -04006743 ret = btrfs_reserve_extent(root, len, root->sectorsize, 0,
Miao Xiee570fd22014-06-19 10:42:50 +08006744 alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006745 if (ret)
6746 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006747
Josef Bacik70c8a912012-10-11 16:54:30 -04006748 em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006749 ins.offset, ins.offset, ins.offset, 0);
Josef Bacik00361582013-08-14 14:02:47 -04006750 if (IS_ERR(em)) {
Miao Xiee570fd22014-06-19 10:42:50 +08006751 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006752 return em;
6753 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006754
6755 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
6756 ins.offset, ins.offset, 0);
6757 if (ret) {
Miao Xiee570fd22014-06-19 10:42:50 +08006758 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006759 free_extent_map(em);
6760 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006761 }
Josef Bacik00361582013-08-14 14:02:47 -04006762
Josef Bacik4b46fce2010-05-23 11:00:55 -04006763 return em;
6764}
6765
Chris Mason46bfbb52010-05-26 11:04:10 -04006766/*
6767 * returns 1 when the nocow is safe, < 1 on error, 0 if the
6768 * block must be cow'd
6769 */
Josef Bacik00361582013-08-14 14:02:47 -04006770noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04006771 u64 *orig_start, u64 *orig_block_len,
6772 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04006773{
Josef Bacik00361582013-08-14 14:02:47 -04006774 struct btrfs_trans_handle *trans;
Chris Mason46bfbb52010-05-26 11:04:10 -04006775 struct btrfs_path *path;
6776 int ret;
6777 struct extent_buffer *leaf;
6778 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08006779 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04006780 struct btrfs_file_extent_item *fi;
6781 struct btrfs_key key;
6782 u64 disk_bytenr;
6783 u64 backref_offset;
6784 u64 extent_end;
6785 u64 num_bytes;
6786 int slot;
6787 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04006788 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08006789
Chris Mason46bfbb52010-05-26 11:04:10 -04006790 path = btrfs_alloc_path();
6791 if (!path)
6792 return -ENOMEM;
6793
Josef Bacik00361582013-08-14 14:02:47 -04006794 ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04006795 offset, 0);
6796 if (ret < 0)
6797 goto out;
6798
6799 slot = path->slots[0];
6800 if (ret == 1) {
6801 if (slot == 0) {
6802 /* can't find the item, must cow */
6803 ret = 0;
6804 goto out;
6805 }
6806 slot--;
6807 }
6808 ret = 0;
6809 leaf = path->nodes[0];
6810 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08006811 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04006812 key.type != BTRFS_EXTENT_DATA_KEY) {
6813 /* not our file or wrong item type, must cow */
6814 goto out;
6815 }
6816
6817 if (key.offset > offset) {
6818 /* Wrong offset, must cow */
6819 goto out;
6820 }
6821
6822 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
6823 found_type = btrfs_file_extent_type(leaf, fi);
6824 if (found_type != BTRFS_FILE_EXTENT_REG &&
6825 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
6826 /* not a regular extent, must cow */
6827 goto out;
6828 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04006829
6830 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
6831 goto out;
6832
Miao Xiee77751a2013-12-27 21:11:50 +08006833 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
6834 if (extent_end <= offset)
6835 goto out;
6836
Chris Mason46bfbb52010-05-26 11:04:10 -04006837 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04006838 if (disk_bytenr == 0)
6839 goto out;
6840
6841 if (btrfs_file_extent_compression(leaf, fi) ||
6842 btrfs_file_extent_encryption(leaf, fi) ||
6843 btrfs_file_extent_other_encoding(leaf, fi))
6844 goto out;
6845
Chris Mason46bfbb52010-05-26 11:04:10 -04006846 backref_offset = btrfs_file_extent_offset(leaf, fi);
6847
Josef Bacik7ee9e442013-06-21 16:37:03 -04006848 if (orig_start) {
6849 *orig_start = key.offset - backref_offset;
6850 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
6851 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
6852 }
Josef Bacikeb384b52013-04-24 16:32:55 -04006853
Chris Mason46bfbb52010-05-26 11:04:10 -04006854 if (btrfs_extent_readonly(root, disk_bytenr))
6855 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08006856
6857 num_bytes = min(offset + *len, extent_end) - offset;
6858 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
6859 u64 range_end;
6860
6861 range_end = round_up(offset + num_bytes, root->sectorsize) - 1;
6862 ret = test_range_bit(io_tree, offset, range_end,
6863 EXTENT_DELALLOC, 0, NULL);
6864 if (ret) {
6865 ret = -EAGAIN;
6866 goto out;
6867 }
6868 }
6869
Josef Bacik1bda19e2013-10-18 12:10:36 -04006870 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04006871
6872 /*
6873 * look for other files referencing this extent, if we
6874 * find any we must cow
6875 */
Josef Bacik00361582013-08-14 14:02:47 -04006876 trans = btrfs_join_transaction(root);
6877 if (IS_ERR(trans)) {
6878 ret = 0;
Chris Mason46bfbb52010-05-26 11:04:10 -04006879 goto out;
Josef Bacik00361582013-08-14 14:02:47 -04006880 }
6881
6882 ret = btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
6883 key.offset - backref_offset, disk_bytenr);
6884 btrfs_end_transaction(trans, root);
6885 if (ret) {
6886 ret = 0;
6887 goto out;
6888 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006889
6890 /*
6891 * adjust disk_bytenr and num_bytes to cover just the bytes
6892 * in this extent we are about to write. If there
6893 * are any csums in that range we have to cow in order
6894 * to keep the csums correct
6895 */
6896 disk_bytenr += backref_offset;
6897 disk_bytenr += offset - key.offset;
Chris Mason46bfbb52010-05-26 11:04:10 -04006898 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
6899 goto out;
6900 /*
6901 * all of the above have passed, it is safe to overwrite this extent
6902 * without cow
6903 */
Josef Bacikeb384b52013-04-24 16:32:55 -04006904 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04006905 ret = 1;
6906out:
6907 btrfs_free_path(path);
6908 return ret;
6909}
6910
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07006911bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
6912{
6913 struct radix_tree_root *root = &inode->i_mapping->page_tree;
6914 int found = false;
6915 void **pagep = NULL;
6916 struct page *page = NULL;
6917 int start_idx;
6918 int end_idx;
6919
6920 start_idx = start >> PAGE_CACHE_SHIFT;
6921
6922 /*
6923 * end is the last byte in the last page. end == start is legal
6924 */
6925 end_idx = end >> PAGE_CACHE_SHIFT;
6926
6927 rcu_read_lock();
6928
6929 /* Most of the code in this while loop is lifted from
6930 * find_get_page. It's been modified to begin searching from a
6931 * page and return just the first page found in that range. If the
6932 * found idx is less than or equal to the end idx then we know that
6933 * a page exists. If no pages are found or if those pages are
6934 * outside of the range then we're fine (yay!) */
6935 while (page == NULL &&
6936 radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
6937 page = radix_tree_deref_slot(pagep);
6938 if (unlikely(!page))
6939 break;
6940
6941 if (radix_tree_exception(page)) {
Filipe Manana809f9012014-06-05 13:22:25 +01006942 if (radix_tree_deref_retry(page)) {
6943 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07006944 continue;
Filipe Manana809f9012014-06-05 13:22:25 +01006945 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07006946 /*
6947 * Otherwise, shmem/tmpfs must be storing a swap entry
6948 * here as an exceptional entry: so return it without
6949 * attempting to raise page count.
6950 */
Filipe Manana6fdef6d2014-06-05 13:22:26 +01006951 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07006952 break; /* TODO: Is this relevant for this use case? */
6953 }
6954
Filipe Manana91405152014-06-05 13:22:24 +01006955 if (!page_cache_get_speculative(page)) {
6956 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07006957 continue;
Filipe Manana91405152014-06-05 13:22:24 +01006958 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07006959
6960 /*
6961 * Has the page moved?
6962 * This is part of the lockless pagecache protocol. See
6963 * include/linux/pagemap.h for details.
6964 */
6965 if (unlikely(page != *pagep)) {
6966 page_cache_release(page);
6967 page = NULL;
6968 }
6969 }
6970
6971 if (page) {
6972 if (page->index <= end_idx)
6973 found = true;
6974 page_cache_release(page);
6975 }
6976
6977 rcu_read_unlock();
6978 return found;
6979}
6980
Josef Bacikeb838e72012-07-31 16:28:48 -04006981static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
6982 struct extent_state **cached_state, int writing)
6983{
6984 struct btrfs_ordered_extent *ordered;
6985 int ret = 0;
6986
6987 while (1) {
6988 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6989 0, cached_state);
6990 /*
6991 * We're concerned with the entire range that we're going to be
6992 * doing DIO to, so we need to make sure theres no ordered
6993 * extents in this range.
6994 */
6995 ordered = btrfs_lookup_ordered_range(inode, lockstart,
6996 lockend - lockstart + 1);
6997
6998 /*
6999 * We need to make sure there are no buffered pages in this
7000 * range either, we could have raced between the invalidate in
7001 * generic_file_direct_write and locking the extent. The
7002 * invalidate needs to happen so that reads after a write do not
7003 * get stale data.
7004 */
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007005 if (!ordered &&
7006 (!writing ||
7007 !btrfs_page_exists_in_range(inode, lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007008 break;
7009
7010 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7011 cached_state, GFP_NOFS);
7012
7013 if (ordered) {
7014 btrfs_start_ordered_extent(inode, ordered, 1);
7015 btrfs_put_ordered_extent(ordered);
7016 } else {
7017 /* Screw you mmap */
Filipe Manana728404d2014-10-10 09:43:11 +01007018 ret = btrfs_fdatawrite_range(inode, lockstart, lockend);
Filipe Manana075bdbd2014-10-09 21:18:55 +01007019 if (ret)
7020 break;
7021 ret = filemap_fdatawait_range(inode->i_mapping,
7022 lockstart,
7023 lockend);
Josef Bacikeb838e72012-07-31 16:28:48 -04007024 if (ret)
7025 break;
7026
7027 /*
7028 * If we found a page that couldn't be invalidated just
7029 * fall back to buffered.
7030 */
7031 ret = invalidate_inode_pages2_range(inode->i_mapping,
7032 lockstart >> PAGE_CACHE_SHIFT,
7033 lockend >> PAGE_CACHE_SHIFT);
7034 if (ret)
7035 break;
7036 }
7037
7038 cond_resched();
7039 }
7040
7041 return ret;
7042}
7043
Josef Bacik69ffb542012-09-11 15:40:07 -04007044static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
7045 u64 len, u64 orig_start,
7046 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04007047 u64 orig_block_len, u64 ram_bytes,
7048 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007049{
7050 struct extent_map_tree *em_tree;
7051 struct extent_map *em;
7052 struct btrfs_root *root = BTRFS_I(inode)->root;
7053 int ret;
7054
7055 em_tree = &BTRFS_I(inode)->extent_tree;
7056 em = alloc_extent_map();
7057 if (!em)
7058 return ERR_PTR(-ENOMEM);
7059
7060 em->start = start;
7061 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04007062 em->mod_start = start;
7063 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04007064 em->len = len;
7065 em->block_len = block_len;
7066 em->block_start = block_start;
7067 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007068 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007069 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007070 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007071 set_bit(EXTENT_FLAG_PINNED, &em->flags);
7072 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05007073 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04007074
7075 do {
7076 btrfs_drop_extent_cache(inode, em->start,
7077 em->start + em->len - 1, 0);
7078 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007079 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007080 write_unlock(&em_tree->lock);
7081 } while (ret == -EEXIST);
7082
7083 if (ret) {
7084 free_extent_map(em);
7085 return ERR_PTR(ret);
7086 }
7087
7088 return em;
7089}
7090
7091
Josef Bacik4b46fce2010-05-23 11:00:55 -04007092static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7093 struct buffer_head *bh_result, int create)
7094{
7095 struct extent_map *em;
7096 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04007097 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007098 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007099 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007100 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007101 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007102 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007103
Miao Xie172a5042013-02-21 02:48:22 -07007104 if (create)
Josef Bacikeb838e72012-07-31 16:28:48 -04007105 unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007106 else
Josef Bacikc3298612012-08-03 16:49:19 -04007107 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007108
Josef Bacikc3298612012-08-03 16:49:19 -04007109 lockstart = start;
7110 lockend = start + len - 1;
7111
Josef Bacikeb838e72012-07-31 16:28:48 -04007112 /*
7113 * If this errors out it's because we couldn't invalidate pagecache for
7114 * this range and we need to fallback to buffered.
7115 */
7116 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
7117 return -ENOTBLK;
7118
Josef Bacik4b46fce2010-05-23 11:00:55 -04007119 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007120 if (IS_ERR(em)) {
7121 ret = PTR_ERR(em);
7122 goto unlock_err;
7123 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007124
7125 /*
7126 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7127 * io. INLINE is special, and we could probably kludge it in here, but
7128 * it's still buffered so for safety lets just fall back to the generic
7129 * buffered path.
7130 *
7131 * For COMPRESSED we _have_ to read the entire extent in so we can
7132 * decompress it, so there will be buffering required no matter what we
7133 * do, so go ahead and fallback to buffered.
7134 *
7135 * We return -ENOTBLK because thats what makes DIO go ahead and go back
7136 * to buffered IO. Don't blame me, this is the price we pay for using
7137 * the generic code.
7138 */
7139 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7140 em->block_start == EXTENT_MAP_INLINE) {
7141 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007142 ret = -ENOTBLK;
7143 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007144 }
7145
7146 /* Just a good old fashioned hole, return */
7147 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7148 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7149 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007150 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007151 }
7152
7153 /*
7154 * We don't allocate a new extent in the following cases
7155 *
7156 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7157 * existing extent.
7158 * 2) The extent is marked as PREALLOC. We're good to go here and can
7159 * just use the extent.
7160 *
7161 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007162 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007163 len = min(len, em->len - (start - em->start));
7164 lockstart = start + len;
7165 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007166 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007167
7168 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7169 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7170 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007171 int type;
7172 int ret;
Josef Bacikeb384b52013-04-24 16:32:55 -04007173 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007174
7175 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7176 type = BTRFS_ORDERED_PREALLOC;
7177 else
7178 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007179 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007180 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007181
Josef Bacik00361582013-08-14 14:02:47 -04007182 if (can_nocow_extent(inode, start, &len, &orig_start,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007183 &orig_block_len, &ram_bytes) == 1) {
Josef Bacik69ffb542012-09-11 15:40:07 -04007184 if (type == BTRFS_ORDERED_PREALLOC) {
7185 free_extent_map(em);
7186 em = create_pinned_em(inode, start, len,
7187 orig_start,
Josef Bacikb4939682012-12-03 10:31:19 -05007188 block_start, len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04007189 orig_block_len,
7190 ram_bytes, type);
Filipe Manana555e1282014-07-07 12:35:21 +01007191 if (IS_ERR(em)) {
7192 ret = PTR_ERR(em);
Josef Bacik69ffb542012-09-11 15:40:07 -04007193 goto unlock_err;
Filipe Manana555e1282014-07-07 12:35:21 +01007194 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007195 }
7196
Chris Mason46bfbb52010-05-26 11:04:10 -04007197 ret = btrfs_add_ordered_extent_dio(inode, start,
7198 block_start, len, len, type);
Chris Mason46bfbb52010-05-26 11:04:10 -04007199 if (ret) {
7200 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007201 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007202 }
7203 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007204 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007205 }
Josef Bacik00361582013-08-14 14:02:47 -04007206
Chris Mason46bfbb52010-05-26 11:04:10 -04007207 /*
7208 * this will cow the extent, reset the len in case we changed
7209 * it above
7210 */
7211 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007212 free_extent_map(em);
7213 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007214 if (IS_ERR(em)) {
7215 ret = PTR_ERR(em);
7216 goto unlock_err;
7217 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007218 len = min(len, em->len - (start - em->start));
7219unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007220 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7221 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007222 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007223 bh_result->b_bdev = em->bdev;
7224 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007225 if (create) {
7226 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7227 set_buffer_new(bh_result);
7228
7229 /*
7230 * Need to update the i_size under the extent lock so buffered
7231 * readers will get the updated i_size when we unlock.
7232 */
7233 if (start + len > i_size_read(inode))
7234 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007235
Miao Xie172a5042013-02-21 02:48:22 -07007236 spin_lock(&BTRFS_I(inode)->lock);
7237 BTRFS_I(inode)->outstanding_extents++;
7238 spin_unlock(&BTRFS_I(inode)->lock);
7239
Miao Xie09348562013-02-07 10:12:07 +00007240 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7241 lockstart + len - 1, EXTENT_DELALLOC, NULL,
7242 &cached_state, GFP_NOFS);
7243 BUG_ON(ret);
Josef Bacikc3473e82012-06-19 10:59:00 -04007244 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007245
Josef Bacikeb838e72012-07-31 16:28:48 -04007246 /*
7247 * In the case of write we need to clear and unlock the entire range,
7248 * in the case of read we need to unlock only the end area that we
7249 * aren't using if there is any left over space.
7250 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007251 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007252 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7253 lockend, unlock_bits, 1, 0,
7254 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06007255 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007256 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007257 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007258
Josef Bacik4b46fce2010-05-23 11:00:55 -04007259 free_extent_map(em);
7260
7261 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007262
7263unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007264 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7265 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
7266 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007267}
7268
Miao Xie8b110e32014-09-12 18:44:03 +08007269static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio,
7270 int rw, int mirror_num)
7271{
7272 struct btrfs_root *root = BTRFS_I(inode)->root;
7273 int ret;
7274
7275 BUG_ON(rw & REQ_WRITE);
7276
7277 bio_get(bio);
7278
7279 ret = btrfs_bio_wq_end_io(root->fs_info, bio,
7280 BTRFS_WQ_ENDIO_DIO_REPAIR);
7281 if (ret)
7282 goto err;
7283
7284 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
7285err:
7286 bio_put(bio);
7287 return ret;
7288}
7289
7290static int btrfs_check_dio_repairable(struct inode *inode,
7291 struct bio *failed_bio,
7292 struct io_failure_record *failrec,
7293 int failed_mirror)
7294{
7295 int num_copies;
7296
7297 num_copies = btrfs_num_copies(BTRFS_I(inode)->root->fs_info,
7298 failrec->logical, failrec->len);
7299 if (num_copies == 1) {
7300 /*
7301 * we only have a single copy of the data, so don't bother with
7302 * all the retry and error correction code that follows. no
7303 * matter what the error is, it is very likely to persist.
7304 */
7305 pr_debug("Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d\n",
7306 num_copies, failrec->this_mirror, failed_mirror);
7307 return 0;
7308 }
7309
7310 failrec->failed_mirror = failed_mirror;
7311 failrec->this_mirror++;
7312 if (failrec->this_mirror == failed_mirror)
7313 failrec->this_mirror++;
7314
7315 if (failrec->this_mirror > num_copies) {
7316 pr_debug("Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d\n",
7317 num_copies, failrec->this_mirror, failed_mirror);
7318 return 0;
7319 }
7320
7321 return 1;
7322}
7323
7324static int dio_read_error(struct inode *inode, struct bio *failed_bio,
7325 struct page *page, u64 start, u64 end,
7326 int failed_mirror, bio_end_io_t *repair_endio,
7327 void *repair_arg)
7328{
7329 struct io_failure_record *failrec;
7330 struct bio *bio;
7331 int isector;
7332 int read_mode;
7333 int ret;
7334
7335 BUG_ON(failed_bio->bi_rw & REQ_WRITE);
7336
7337 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7338 if (ret)
7339 return ret;
7340
7341 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7342 failed_mirror);
7343 if (!ret) {
7344 free_io_failure(inode, failrec);
7345 return -EIO;
7346 }
7347
7348 if (failed_bio->bi_vcnt > 1)
7349 read_mode = READ_SYNC | REQ_FAILFAST_DEV;
7350 else
7351 read_mode = READ_SYNC;
7352
7353 isector = start - btrfs_io_bio(failed_bio)->logical;
7354 isector >>= inode->i_sb->s_blocksize_bits;
7355 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
7356 0, isector, repair_endio, repair_arg);
7357 if (!bio) {
7358 free_io_failure(inode, failrec);
7359 return -EIO;
7360 }
7361
7362 btrfs_debug(BTRFS_I(inode)->root->fs_info,
7363 "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n",
7364 read_mode, failrec->this_mirror, failrec->in_validation);
7365
7366 ret = submit_dio_repair_bio(inode, bio, read_mode,
7367 failrec->this_mirror);
7368 if (ret) {
7369 free_io_failure(inode, failrec);
7370 bio_put(bio);
7371 }
7372
7373 return ret;
7374}
7375
7376struct btrfs_retry_complete {
7377 struct completion done;
7378 struct inode *inode;
7379 u64 start;
7380 int uptodate;
7381};
7382
7383static void btrfs_retry_endio_nocsum(struct bio *bio, int err)
7384{
7385 struct btrfs_retry_complete *done = bio->bi_private;
7386 struct bio_vec *bvec;
7387 int i;
7388
7389 if (err)
7390 goto end;
7391
7392 done->uptodate = 1;
7393 bio_for_each_segment_all(bvec, bio, i)
7394 clean_io_failure(done->inode, done->start, bvec->bv_page, 0);
7395end:
7396 complete(&done->done);
7397 bio_put(bio);
7398}
7399
7400static int __btrfs_correct_data_nocsum(struct inode *inode,
7401 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007402{
Kent Overstreet2c30c712013-11-07 12:20:26 -08007403 struct bio_vec *bvec;
Miao Xie8b110e32014-09-12 18:44:03 +08007404 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007405 u64 start;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007406 int i;
Miao Xiec1dc0892014-09-12 18:43:56 +08007407 int ret;
Miao Xiedc380ae2014-09-12 18:43:55 +08007408
Miao Xiec1dc0892014-09-12 18:43:56 +08007409 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08007410 done.inode = inode;
7411
7412 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
7413try_again:
7414 done.uptodate = 0;
7415 done.start = start;
7416 init_completion(&done.done);
7417
7418 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page, start,
7419 start + bvec->bv_len - 1,
7420 io_bio->mirror_num,
7421 btrfs_retry_endio_nocsum, &done);
7422 if (ret)
7423 return ret;
7424
7425 wait_for_completion(&done.done);
7426
7427 if (!done.uptodate) {
7428 /* We might have another mirror, so try again */
7429 goto try_again;
7430 }
7431
7432 start += bvec->bv_len;
7433 }
7434
7435 return 0;
7436}
7437
7438static void btrfs_retry_endio(struct bio *bio, int err)
7439{
7440 struct btrfs_retry_complete *done = bio->bi_private;
7441 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
7442 struct bio_vec *bvec;
7443 int uptodate;
7444 int ret;
7445 int i;
7446
7447 if (err)
7448 goto end;
7449
7450 uptodate = 1;
7451 bio_for_each_segment_all(bvec, bio, i) {
7452 ret = __readpage_endio_check(done->inode, io_bio, i,
7453 bvec->bv_page, 0,
7454 done->start, bvec->bv_len);
7455 if (!ret)
7456 clean_io_failure(done->inode, done->start,
7457 bvec->bv_page, 0);
7458 else
7459 uptodate = 0;
7460 }
7461
7462 done->uptodate = uptodate;
7463end:
7464 complete(&done->done);
7465 bio_put(bio);
7466}
7467
7468static int __btrfs_subio_endio_read(struct inode *inode,
7469 struct btrfs_io_bio *io_bio, int err)
7470{
7471 struct bio_vec *bvec;
7472 struct btrfs_retry_complete done;
7473 u64 start;
7474 u64 offset = 0;
7475 int i;
7476 int ret;
7477
7478 err = 0;
7479 start = io_bio->logical;
7480 done.inode = inode;
7481
Miao Xiec1dc0892014-09-12 18:43:56 +08007482 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
Miao Xiedc380ae2014-09-12 18:43:55 +08007483 ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page,
7484 0, start, bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08007485 if (likely(!ret))
7486 goto next;
7487try_again:
7488 done.uptodate = 0;
7489 done.start = start;
7490 init_completion(&done.done);
7491
7492 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page, start,
7493 start + bvec->bv_len - 1,
7494 io_bio->mirror_num,
7495 btrfs_retry_endio, &done);
7496 if (ret) {
7497 err = ret;
7498 goto next;
7499 }
7500
7501 wait_for_completion(&done.done);
7502
7503 if (!done.uptodate) {
7504 /* We might have another mirror, so try again */
7505 goto try_again;
7506 }
7507next:
7508 offset += bvec->bv_len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007509 start += bvec->bv_len;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007510 }
Miao Xiec1dc0892014-09-12 18:43:56 +08007511
7512 return err;
7513}
7514
Miao Xie8b110e32014-09-12 18:44:03 +08007515static int btrfs_subio_endio_read(struct inode *inode,
7516 struct btrfs_io_bio *io_bio, int err)
7517{
7518 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
7519
7520 if (skip_csum) {
7521 if (unlikely(err))
7522 return __btrfs_correct_data_nocsum(inode, io_bio);
7523 else
7524 return 0;
7525 } else {
7526 return __btrfs_subio_endio_read(inode, io_bio, err);
7527 }
7528}
7529
Miao Xiec1dc0892014-09-12 18:43:56 +08007530static void btrfs_endio_direct_read(struct bio *bio, int err)
7531{
7532 struct btrfs_dio_private *dip = bio->bi_private;
7533 struct inode *inode = dip->inode;
7534 struct bio *dio_bio;
7535 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
7536
Miao Xie8b110e32014-09-12 18:44:03 +08007537 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
7538 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08007539
Josef Bacik4b46fce2010-05-23 11:00:55 -04007540 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007541 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04007542 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007543
Josef Bacik4b46fce2010-05-23 11:00:55 -04007544 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04007545
7546 /* If we had a csum failure make sure to clear the uptodate flag */
7547 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04007548 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
7549 dio_end_io(dio_bio, err);
Miao Xie23ea8e52014-09-12 18:43:54 +08007550
7551 if (io_bio->end_io)
7552 io_bio->end_io(io_bio, err);
Chris Mason9be33952013-05-17 18:30:14 -04007553 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007554}
7555
7556static void btrfs_endio_direct_write(struct bio *bio, int err)
7557{
7558 struct btrfs_dio_private *dip = bio->bi_private;
7559 struct inode *inode = dip->inode;
7560 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007561 struct btrfs_ordered_extent *ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05007562 u64 ordered_offset = dip->logical_offset;
7563 u64 ordered_bytes = dip->bytes;
Chris Mason9be33952013-05-17 18:30:14 -04007564 struct bio *dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007565 int ret;
7566
7567 if (err)
7568 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05007569again:
7570 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
7571 &ordered_offset,
Josef Bacik5fd02042012-05-02 14:00:54 -04007572 ordered_bytes, !err);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007573 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05007574 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007575
Liu Bo9e0af232014-08-15 23:36:53 +08007576 btrfs_init_work(&ordered->work, btrfs_endio_write_helper,
7577 finish_ordered_fn, NULL, NULL);
Qu Wenruofccb5d82014-02-28 10:46:10 +08007578 btrfs_queue_work(root->fs_info->endio_write_workers,
7579 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05007580out_test:
7581 /*
7582 * our bio might span multiple ordered extents. If we haven't
7583 * completed the accounting for the whole dio, go back and try again
7584 */
7585 if (ordered_offset < dip->logical_offset + dip->bytes) {
7586 ordered_bytes = dip->logical_offset + dip->bytes -
7587 ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04007588 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05007589 goto again;
7590 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007591out_done:
Chris Mason9be33952013-05-17 18:30:14 -04007592 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007593
Josef Bacik4b46fce2010-05-23 11:00:55 -04007594 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04007595
7596 /* If we had an error make sure to clear the uptodate flag */
7597 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04007598 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
7599 dio_end_io(dio_bio, err);
7600 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007601}
7602
Chris Masoneaf25d92010-05-25 09:48:28 -04007603static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
7604 struct bio *bio, int mirror_num,
7605 unsigned long bio_flags, u64 offset)
7606{
7607 int ret;
7608 struct btrfs_root *root = BTRFS_I(inode)->root;
7609 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007610 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04007611 return 0;
7612}
7613
Miao Xiee65e1532010-11-22 03:04:43 +00007614static void btrfs_end_dio_bio(struct bio *bio, int err)
7615{
7616 struct btrfs_dio_private *dip = bio->bi_private;
7617
Miao Xie8b110e32014-09-12 18:44:03 +08007618 if (err)
7619 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
7620 "direct IO failed ino %llu rw %lu sector %#Lx len %u err no %d",
7621 btrfs_ino(dip->inode), bio->bi_rw,
7622 (unsigned long long)bio->bi_iter.bi_sector,
7623 bio->bi_iter.bi_size, err);
7624
7625 if (dip->subio_endio)
7626 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08007627
7628 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00007629 dip->errors = 1;
7630
7631 /*
7632 * before atomic variable goto zero, we must make sure
7633 * dip->errors is perceived to be set.
7634 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01007635 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00007636 }
7637
7638 /* if there are more bios still pending for this dio, just exit */
7639 if (!atomic_dec_and_test(&dip->pending_bios))
7640 goto out;
7641
Chris Mason9be33952013-05-17 18:30:14 -04007642 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00007643 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04007644 } else {
7645 set_bit(BIO_UPTODATE, &dip->dio_bio->bi_flags);
Miao Xiee65e1532010-11-22 03:04:43 +00007646 bio_endio(dip->orig_bio, 0);
7647 }
7648out:
7649 bio_put(bio);
7650}
7651
7652static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
7653 u64 first_sector, gfp_t gfp_flags)
7654{
7655 int nr_vecs = bio_get_nr_vecs(bdev);
7656 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
7657}
7658
Miao Xiec1dc0892014-09-12 18:43:56 +08007659static inline int btrfs_lookup_and_bind_dio_csum(struct btrfs_root *root,
7660 struct inode *inode,
7661 struct btrfs_dio_private *dip,
7662 struct bio *bio,
7663 u64 file_offset)
7664{
7665 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
7666 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
7667 int ret;
7668
7669 /*
7670 * We load all the csum data we need when we submit
7671 * the first bio to reduce the csum tree search and
7672 * contention.
7673 */
7674 if (dip->logical_offset == file_offset) {
7675 ret = btrfs_lookup_bio_sums_dio(root, inode, dip->orig_bio,
7676 file_offset);
7677 if (ret)
7678 return ret;
7679 }
7680
7681 if (bio == dip->orig_bio)
7682 return 0;
7683
7684 file_offset -= dip->logical_offset;
7685 file_offset >>= inode->i_sb->s_blocksize_bits;
7686 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
7687
7688 return 0;
7689}
7690
Miao Xiee65e1532010-11-22 03:04:43 +00007691static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
7692 int rw, u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007693 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00007694{
Miao Xiefacc8a222013-07-25 19:22:34 +08007695 struct btrfs_dio_private *dip = bio->bi_private;
Miao Xiee65e1532010-11-22 03:04:43 +00007696 int write = rw & REQ_WRITE;
7697 struct btrfs_root *root = BTRFS_I(inode)->root;
7698 int ret;
7699
Josef Bacikb812ce22012-11-16 13:56:32 -05007700 if (async_submit)
7701 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
7702
Miao Xiee65e1532010-11-22 03:04:43 +00007703 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04007704
7705 if (!write) {
David Sterbabfebd8b2014-07-30 00:25:45 +02007706 ret = btrfs_bio_wq_end_io(root->fs_info, bio,
7707 BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04007708 if (ret)
7709 goto err;
7710 }
Miao Xiee65e1532010-11-22 03:04:43 +00007711
Josef Bacik1ae39932011-04-06 14:41:34 -04007712 if (skip_sum)
7713 goto map;
7714
7715 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00007716 ret = btrfs_wq_submit_bio(root->fs_info,
7717 inode, rw, bio, 0, 0,
7718 file_offset,
7719 __btrfs_submit_bio_start_direct_io,
7720 __btrfs_submit_bio_done);
7721 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04007722 } else if (write) {
7723 /*
7724 * If we aren't doing async submit, calculate the csum of the
7725 * bio now.
7726 */
7727 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
7728 if (ret)
7729 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08007730 } else {
Miao Xiec1dc0892014-09-12 18:43:56 +08007731 ret = btrfs_lookup_and_bind_dio_csum(root, inode, dip, bio,
7732 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007733 if (ret)
7734 goto err;
7735 }
Josef Bacik1ae39932011-04-06 14:41:34 -04007736map:
7737 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007738err:
7739 bio_put(bio);
7740 return ret;
7741}
7742
7743static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
7744 int skip_sum)
7745{
7746 struct inode *inode = dip->inode;
7747 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00007748 struct bio *bio;
7749 struct bio *orig_bio = dip->orig_bio;
7750 struct bio_vec *bvec = orig_bio->bi_io_vec;
Kent Overstreet4f024f32013-10-11 15:44:27 -07007751 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00007752 u64 file_offset = dip->logical_offset;
7753 u64 submit_len = 0;
7754 u64 map_length;
7755 int nr_pages = 0;
Miao Xie23ea8e52014-09-12 18:43:54 +08007756 int ret;
Josef Bacik1ae39932011-04-06 14:41:34 -04007757 int async_submit = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007758
Kent Overstreet4f024f32013-10-11 15:44:27 -07007759 map_length = orig_bio->bi_iter.bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007760 ret = btrfs_map_block(root->fs_info, rw, start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007761 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08007762 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00007763 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08007764
Kent Overstreet4f024f32013-10-11 15:44:27 -07007765 if (map_length >= orig_bio->bi_iter.bi_size) {
Josef Bacik02f57c72011-04-06 14:25:44 -04007766 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08007767 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04007768 goto submit;
7769 }
7770
David Woodhouse53b381b2013-01-29 18:40:14 -05007771 /* async crcs make it difficult to collect full stripe writes. */
7772 if (btrfs_get_alloc_profile(root, 1) &
7773 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6))
7774 async_submit = 0;
7775 else
7776 async_submit = 1;
7777
Josef Bacik02f57c72011-04-06 14:25:44 -04007778 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
7779 if (!bio)
7780 return -ENOMEM;
Miao Xie7a5c3c92014-06-17 18:58:59 +08007781
Josef Bacik02f57c72011-04-06 14:25:44 -04007782 bio->bi_private = dip;
7783 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08007784 btrfs_io_bio(bio)->logical = file_offset;
Josef Bacik02f57c72011-04-06 14:25:44 -04007785 atomic_inc(&dip->pending_bios);
7786
Miao Xiee65e1532010-11-22 03:04:43 +00007787 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
David Sterbaee39b432014-09-30 01:33:33 +02007788 if (map_length < submit_len + bvec->bv_len ||
Miao Xiee65e1532010-11-22 03:04:43 +00007789 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
David Sterbaee39b432014-09-30 01:33:33 +02007790 bvec->bv_offset) < bvec->bv_len) {
Miao Xiee65e1532010-11-22 03:04:43 +00007791 /*
7792 * inc the count before we submit the bio so
7793 * we know the end IO handler won't happen before
7794 * we inc the count. Otherwise, the dip might get freed
7795 * before we're done setting it up
7796 */
7797 atomic_inc(&dip->pending_bios);
7798 ret = __btrfs_submit_dio_bio(bio, inode, rw,
7799 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007800 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007801 if (ret) {
7802 bio_put(bio);
7803 atomic_dec(&dip->pending_bios);
7804 goto out_err;
7805 }
7806
Miao Xiee65e1532010-11-22 03:04:43 +00007807 start_sector += submit_len >> 9;
7808 file_offset += submit_len;
7809
7810 submit_len = 0;
7811 nr_pages = 0;
7812
7813 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
7814 start_sector, GFP_NOFS);
7815 if (!bio)
7816 goto out_err;
7817 bio->bi_private = dip;
7818 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08007819 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00007820
Kent Overstreet4f024f32013-10-11 15:44:27 -07007821 map_length = orig_bio->bi_iter.bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007822 ret = btrfs_map_block(root->fs_info, rw,
Stefan Behrens3ec706c2012-11-05 15:46:42 +01007823 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007824 &map_length, NULL, 0);
7825 if (ret) {
7826 bio_put(bio);
7827 goto out_err;
7828 }
7829 } else {
7830 submit_len += bvec->bv_len;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307831 nr_pages++;
Miao Xiee65e1532010-11-22 03:04:43 +00007832 bvec++;
7833 }
7834 }
7835
Josef Bacik02f57c72011-04-06 14:25:44 -04007836submit:
Miao Xiee65e1532010-11-22 03:04:43 +00007837 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007838 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007839 if (!ret)
7840 return 0;
7841
7842 bio_put(bio);
7843out_err:
7844 dip->errors = 1;
7845 /*
7846 * before atomic variable goto zero, we must
7847 * make sure dip->errors is perceived to be set.
7848 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01007849 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00007850 if (atomic_dec_and_test(&dip->pending_bios))
7851 bio_io_error(dip->orig_bio);
7852
7853 /* bio_end_io() will handle error, so we needn't return it */
7854 return 0;
7855}
7856
Chris Mason9be33952013-05-17 18:30:14 -04007857static void btrfs_submit_direct(int rw, struct bio *dio_bio,
7858 struct inode *inode, loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007859{
7860 struct btrfs_root *root = BTRFS_I(inode)->root;
7861 struct btrfs_dio_private *dip;
Chris Mason9be33952013-05-17 18:30:14 -04007862 struct bio *io_bio;
Miao Xie23ea8e52014-09-12 18:43:54 +08007863 struct btrfs_io_bio *btrfs_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007864 int skip_sum;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02007865 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007866 int ret = 0;
7867
7868 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
7869
Chris Mason9be33952013-05-17 18:30:14 -04007870 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04007871 if (!io_bio) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007872 ret = -ENOMEM;
7873 goto free_ordered;
7874 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007875
Miao Xiec1dc0892014-09-12 18:43:56 +08007876 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04007877 if (!dip) {
7878 ret = -ENOMEM;
7879 goto free_io_bio;
7880 }
7881
7882 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007883 dip->inode = inode;
7884 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07007885 dip->bytes = dio_bio->bi_iter.bi_size;
7886 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Chris Mason9be33952013-05-17 18:30:14 -04007887 io_bio->bi_private = dip;
Chris Mason9be33952013-05-17 18:30:14 -04007888 dip->orig_bio = io_bio;
7889 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00007890 atomic_set(&dip->pending_bios, 0);
Miao Xiec1dc0892014-09-12 18:43:56 +08007891 btrfs_bio = btrfs_io_bio(io_bio);
7892 btrfs_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007893
Miao Xiec1dc0892014-09-12 18:43:56 +08007894 if (write) {
Chris Mason9be33952013-05-17 18:30:14 -04007895 io_bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08007896 } else {
Chris Mason9be33952013-05-17 18:30:14 -04007897 io_bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08007898 dip->subio_endio = btrfs_subio_endio_read;
7899 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007900
Miao Xiee65e1532010-11-22 03:04:43 +00007901 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
7902 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04007903 return;
Chris Mason9be33952013-05-17 18:30:14 -04007904
Miao Xie23ea8e52014-09-12 18:43:54 +08007905 if (btrfs_bio->end_io)
7906 btrfs_bio->end_io(btrfs_bio, ret);
Chris Mason9be33952013-05-17 18:30:14 -04007907free_io_bio:
7908 bio_put(io_bio);
7909
Josef Bacik4b46fce2010-05-23 11:00:55 -04007910free_ordered:
7911 /*
7912 * If this is a write, we need to clean up the reserved space and kill
7913 * the ordered extent.
7914 */
7915 if (write) {
7916 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05007917 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007918 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
7919 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
7920 btrfs_free_reserved_extent(root, ordered->start,
Miao Xiee570fd22014-06-19 10:42:50 +08007921 ordered->disk_len, 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007922 btrfs_put_ordered_extent(ordered);
7923 btrfs_put_ordered_extent(ordered);
7924 }
Chris Mason9be33952013-05-17 18:30:14 -04007925 bio_endio(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007926}
7927
Chris Mason5a5f79b2010-05-26 21:33:37 -04007928static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
Al Viro28060d52014-03-22 05:15:17 -04007929 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04007930{
7931 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00007932 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007933 unsigned blocksize_mask = root->sectorsize - 1;
7934 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007935
7936 if (offset & blocksize_mask)
7937 goto out;
7938
Al Viro28060d52014-03-22 05:15:17 -04007939 if (iov_iter_alignment(iter) & blocksize_mask)
7940 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00007941
Al Viro28060d52014-03-22 05:15:17 -04007942 /* If this is a write we don't need to check anymore */
7943 if (rw & WRITE)
7944 return 0;
7945 /*
7946 * Check to make sure we don't have duplicate iov_base's in this
7947 * iovec, if so return EINVAL, otherwise we'll get csum errors
7948 * when reading back.
7949 */
7950 for (seg = 0; seg < iter->nr_segs; seg++) {
7951 for (i = seg + 1; i < iter->nr_segs; i++) {
7952 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00007953 goto out;
7954 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04007955 }
7956 retval = 0;
7957out:
7958 return retval;
7959}
Josef Bacikeb838e72012-07-31 16:28:48 -04007960
Chris Mason16432982008-04-10 10:23:21 -04007961static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
Al Virod8d3d942014-03-04 21:27:34 -05007962 struct iov_iter *iter, loff_t offset)
Chris Mason16432982008-04-10 10:23:21 -04007963{
Josef Bacik4b46fce2010-05-23 11:00:55 -04007964 struct file *file = iocb->ki_filp;
7965 struct inode *inode = file->f_mapping->host;
Miao Xie09348562013-02-07 10:12:07 +00007966 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00007967 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00007968 bool wakeup = true;
7969 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00007970 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007971
Al Viro28060d52014-03-22 05:15:17 -04007972 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007973 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007974
Miao Xie38851cc2013-02-08 07:04:11 +00007975 atomic_inc(&inode->i_dio_count);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01007976 smp_mb__after_atomic();
Miao Xie38851cc2013-02-08 07:04:11 +00007977
Josef Bacik0e267c42013-07-02 10:38:02 -04007978 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08007979 * The generic stuff only does filemap_write_and_wait_range, which
7980 * isn't enough if we've written compressed pages to this area, so
7981 * we need to flush the dirty pages again to make absolutely sure
7982 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04007983 */
Al Viroa6cbcd42014-03-04 22:38:00 -05007984 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08007985 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
7986 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08007987 filemap_fdatawrite_range(inode->i_mapping, offset,
7988 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04007989
Miao Xie09348562013-02-07 10:12:07 +00007990 if (rw & WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00007991 /*
7992 * If the write DIO is beyond the EOF, we need update
7993 * the isize, but it is protected by i_mutex. So we can
7994 * not unlock the i_mutex at this case.
7995 */
7996 if (offset + count <= inode->i_size) {
7997 mutex_unlock(&inode->i_mutex);
7998 relock = true;
7999 }
Miao Xie09348562013-02-07 10:12:07 +00008000 ret = btrfs_delalloc_reserve_space(inode, count);
8001 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008002 goto out;
David Sterbaee39b432014-09-30 01:33:33 +02008003 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8004 &BTRFS_I(inode)->runtime_flags)) {
Miao Xie38851cc2013-02-08 07:04:11 +00008005 inode_dio_done(inode);
8006 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8007 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008008 }
8009
8010 ret = __blockdev_direct_IO(rw, iocb, inode,
8011 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
Al Viro31b14032014-03-05 01:33:16 -05008012 iter, offset, btrfs_get_blocks_direct, NULL,
Miao Xie2e60a512013-02-08 07:01:08 +00008013 btrfs_submit_direct, flags);
Miao Xie09348562013-02-07 10:12:07 +00008014 if (rw & WRITE) {
8015 if (ret < 0 && ret != -EIOCBQUEUED)
8016 btrfs_delalloc_release_space(inode, count);
Miao Xie172a5042013-02-21 02:48:22 -07008017 else if (ret >= 0 && (size_t)ret < count)
Miao Xie09348562013-02-07 10:12:07 +00008018 btrfs_delalloc_release_space(inode,
8019 count - (size_t)ret);
Miao Xie172a5042013-02-21 02:48:22 -07008020 else
8021 btrfs_delalloc_release_metadata(inode, 0);
Miao Xie09348562013-02-07 10:12:07 +00008022 }
Miao Xie38851cc2013-02-08 07:04:11 +00008023out:
Miao Xie2e60a512013-02-08 07:01:08 +00008024 if (wakeup)
8025 inode_dio_done(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008026 if (relock)
8027 mutex_lock(&inode->i_mutex);
Miao Xie09348562013-02-07 10:12:07 +00008028
8029 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008030}
8031
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008032#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8033
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008034static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8035 __u64 start, __u64 len)
8036{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008037 int ret;
8038
8039 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8040 if (ret)
8041 return ret;
8042
Chris Masonec29ed52011-02-23 16:23:20 -05008043 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008044}
8045
Chris Mason9ebefb182007-06-15 13:50:00 -04008046int btrfs_readpage(struct file *file, struct page *page)
8047{
Chris Masond1310b22008-01-24 16:13:08 -05008048 struct extent_io_tree *tree;
8049 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008050 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008051}
Chris Mason1832a6d2007-12-21 16:27:21 -05008052
Chris Mason39279cc2007-06-12 06:35:45 -04008053static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8054{
Chris Masond1310b22008-01-24 16:13:08 -05008055 struct extent_io_tree *tree;
Chris Masonb888db2b2007-08-27 16:49:44 -04008056
8057
8058 if (current->flags & PF_MEMALLOC) {
8059 redirty_page_for_writepage(wbc, page);
8060 unlock_page(page);
8061 return 0;
8062 }
Chris Masond1310b22008-01-24 16:13:08 -05008063 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04008064 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
8065}
Chris Mason39279cc2007-06-12 06:35:45 -04008066
Eric Sandeen48a3b632013-04-25 20:41:01 +00008067static int btrfs_writepages(struct address_space *mapping,
8068 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008069{
Chris Masond1310b22008-01-24 16:13:08 -05008070 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05008071
Chris Masond1310b22008-01-24 16:13:08 -05008072 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f02e2007-11-01 19:45:34 -04008073 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
8074}
8075
Chris Mason3ab2fb52007-11-08 10:59:22 -05008076static int
8077btrfs_readpages(struct file *file, struct address_space *mapping,
8078 struct list_head *pages, unsigned nr_pages)
8079{
Chris Masond1310b22008-01-24 16:13:08 -05008080 struct extent_io_tree *tree;
8081 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05008082 return extent_readpages(tree, mapping, pages, nr_pages,
8083 btrfs_get_extent);
8084}
Chris Masone6dcd2d2008-07-17 12:53:50 -04008085static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008086{
Chris Masond1310b22008-01-24 16:13:08 -05008087 struct extent_io_tree *tree;
8088 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04008089 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008090
Chris Masond1310b22008-01-24 16:13:08 -05008091 tree = &BTRFS_I(page->mapping->host)->io_tree;
8092 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05008093 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008094 if (ret == 1) {
8095 ClearPagePrivate(page);
8096 set_page_private(page, 0);
8097 page_cache_release(page);
8098 }
8099 return ret;
8100}
Chris Mason39279cc2007-06-12 06:35:45 -04008101
Chris Masone6dcd2d2008-07-17 12:53:50 -04008102static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8103{
Chris Mason98509cf2008-09-11 15:51:43 -04008104 if (PageWriteback(page) || PageDirty(page))
8105 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05008106 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008107}
8108
Lukas Czernerd47992f2013-05-21 23:17:23 -04008109static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8110 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008111{
Josef Bacik5fd02042012-05-02 14:00:54 -04008112 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008113 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008114 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008115 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008116 u64 page_start = page_offset(page);
8117 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008118 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008119
Chris Mason8b62b722009-09-02 16:53:46 -04008120 /*
8121 * we have the page locked, so new writeback can't start,
8122 * and the dirty bit won't be cleared while we are here.
8123 *
8124 * Wait for IO on this page so that we can safely clear
8125 * the PagePrivate2 bit and do ordered accounting
8126 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008127 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008128
Josef Bacik5fd02042012-05-02 14:00:54 -04008129 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008130 if (offset) {
8131 btrfs_releasepage(page, GFP_NOFS);
8132 return;
8133 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008134
8135 if (!inode_evicting)
8136 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
8137 ordered = btrfs_lookup_ordered_extent(inode, page_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008138 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04008139 /*
8140 * IO on this page will never be started, so we need
8141 * to account for any ordered extents now
8142 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008143 if (!inode_evicting)
8144 clear_extent_bit(tree, page_start, page_end,
8145 EXTENT_DIRTY | EXTENT_DELALLOC |
8146 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
8147 EXTENT_DEFRAG, 1, 0, &cached_state,
8148 GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04008149 /*
8150 * whoever cleared the private bit is responsible
8151 * for the finish_ordered_io
8152 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008153 if (TestClearPagePrivate2(page)) {
8154 struct btrfs_ordered_inode_tree *tree;
8155 u64 new_len;
8156
8157 tree = &BTRFS_I(inode)->ordered_tree;
8158
8159 spin_lock_irq(&tree->lock);
8160 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
8161 new_len = page_start - ordered->file_offset;
8162 if (new_len < ordered->truncated_len)
8163 ordered->truncated_len = new_len;
8164 spin_unlock_irq(&tree->lock);
8165
8166 if (btrfs_dec_test_ordered_pending(inode, &ordered,
8167 page_start,
8168 PAGE_CACHE_SIZE, 1))
8169 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008170 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008171 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008172 if (!inode_evicting) {
8173 cached_state = NULL;
8174 lock_extent_bits(tree, page_start, page_end, 0,
8175 &cached_state);
8176 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008177 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008178
8179 if (!inode_evicting) {
8180 clear_extent_bit(tree, page_start, page_end,
8181 EXTENT_LOCKED | EXTENT_DIRTY |
8182 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8183 EXTENT_DEFRAG, 1, 1,
8184 &cached_state, GFP_NOFS);
8185
8186 __btrfs_releasepage(page, GFP_NOFS);
8187 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008188
Chris Mason4a096752008-07-21 10:29:44 -04008189 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008190 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008191 ClearPagePrivate(page);
8192 set_page_private(page, 0);
8193 page_cache_release(page);
8194 }
Chris Mason39279cc2007-06-12 06:35:45 -04008195}
8196
Chris Mason9ebefb182007-06-15 13:50:00 -04008197/*
8198 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8199 * called from a page fault handler when a page is first dirtied. Hence we must
8200 * be careful to check for EOF conditions here. We set the page up correctly
8201 * for a written page which means we get ENOSPC checking when writing into
8202 * holes and correct delalloc and unwritten extent mapping on filesystems that
8203 * support these features.
8204 *
8205 * We are not allowed to take the i_mutex here so we have to play games to
8206 * protect against truncate races as the page could now be beyond EOF. Because
8207 * vmtruncate() writes the inode size before removing pages, once we have the
8208 * page lock we can determine safely if the page is beyond EOF. If it is not
8209 * beyond EOF, then the page is guaranteed safe against truncation until we
8210 * unlock the page.
8211 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07008212int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008213{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008214 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05008215 struct inode *inode = file_inode(vma->vm_file);
Chris Mason1832a6d2007-12-21 16:27:21 -05008216 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008217 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8218 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008219 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008220 char *kaddr;
8221 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008222 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05008223 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05008224 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04008225 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008226 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04008227
Jan Karab2b5ef52012-06-12 16:20:45 +02008228 sb_start_pagefault(inode->i_sb);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04008229 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05008230 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04008231 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008232 reserved = 1;
8233 }
Nick Piggin56a76f82009-03-31 15:23:23 -07008234 if (ret) {
8235 if (ret == -ENOMEM)
8236 ret = VM_FAULT_OOM;
8237 else /* -ENOSPC, -EIO, etc */
8238 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05008239 if (reserved)
8240 goto out;
8241 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008242 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008243
Nick Piggin56a76f82009-03-31 15:23:23 -07008244 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008245again:
Chris Mason9ebefb182007-06-15 13:50:00 -04008246 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008247 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008248 page_start = page_offset(page);
8249 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04008250
Chris Mason9ebefb182007-06-15 13:50:00 -04008251 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008252 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008253 /* page got truncated out from underneath us */
8254 goto out_unlock;
8255 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008256 wait_on_page_writeback(page);
8257
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008258 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008259 set_page_extent_mapped(page);
8260
Chris Masoneb84ae02008-07-17 13:53:27 -04008261 /*
8262 * we can't set the delalloc bits if there are pending ordered
8263 * extents. Drop our locks and wait for them to finish
8264 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008265 ordered = btrfs_lookup_ordered_extent(inode, page_start);
8266 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008267 unlock_extent_cached(io_tree, page_start, page_end,
8268 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008269 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04008270 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008271 btrfs_put_ordered_extent(ordered);
8272 goto again;
8273 }
8274
Josef Bacikfbf19082009-10-01 17:10:23 -04008275 /*
8276 * XXX - page_mkwrite gets called every time the page is dirtied, even
8277 * if it was already dirty, so for space accounting reasons we need to
8278 * clear any delalloc bits for the range we are fixing to save. There
8279 * is probably a better way to do this, but for now keep consistent with
8280 * prepare_pages in the normal write path.
8281 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00008282 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06008283 EXTENT_DIRTY | EXTENT_DELALLOC |
8284 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00008285 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04008286
Josef Bacik2ac55d42010-02-03 19:33:23 +00008287 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
8288 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008289 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008290 unlock_extent_cached(io_tree, page_start, page_end,
8291 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008292 ret = VM_FAULT_SIGBUS;
8293 goto out_unlock;
8294 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008295 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04008296
8297 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04008298 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04008299 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04008300 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04008301 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008302
Chris Masone6dcd2d2008-07-17 12:53:50 -04008303 if (zero_start != PAGE_CACHE_SIZE) {
8304 kaddr = kmap(page);
8305 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
8306 flush_dcache_page(page);
8307 kunmap(page);
8308 }
Chris Mason247e7432008-07-17 12:53:51 -04008309 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008310 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04008311 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008312
Chris Mason257c62e2009-10-13 13:21:08 -04008313 BTRFS_I(inode)->last_trans = root->fs_info->generation;
8314 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06008315 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04008316
Josef Bacik2ac55d42010-02-03 19:33:23 +00008317 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04008318
8319out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02008320 if (!ret) {
8321 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04008322 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02008323 }
Chris Mason9ebefb182007-06-15 13:50:00 -04008324 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05008325out:
Josef Bacikec39e182012-01-12 19:10:12 -05008326 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05008327out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02008328 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04008329 return ret;
8330}
8331
Josef Bacika41ad392011-01-31 15:30:16 -05008332static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04008333{
8334 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04008335 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04008336 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05008337 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008338 struct btrfs_trans_handle *trans;
Chris Masondbe674a2008-07-17 12:54:05 -04008339 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04008340 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04008341
Josef Bacik0ef8b722013-10-25 16:13:35 -04008342 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
8343 (u64)-1);
8344 if (ret)
8345 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008346
Josef Bacikfcb80c22011-05-03 10:40:22 -04008347 /*
8348 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
8349 * 3 things going on here
8350 *
8351 * 1) We need to reserve space for our orphan item and the space to
8352 * delete our orphan item. Lord knows we don't want to have a dangling
8353 * orphan item because we didn't reserve space to remove it.
8354 *
8355 * 2) We need to reserve space to update our inode.
8356 *
8357 * 3) We need to have something to cache all the space that is going to
8358 * be free'd up by the truncate operation, but also have some slack
8359 * space reserved in case it uses space during the truncate (thank you
8360 * very much snapshotting).
8361 *
8362 * And we need these to all be seperate. The fact is we can use alot of
8363 * space doing the truncate, and we have no earthly idea how much space
8364 * we will use, so we need the truncate reservation to be seperate so it
8365 * doesn't end up using space reserved for updating the inode or
8366 * removing the orphan item. We also need to be able to stop the
8367 * transaction and start a new one, which means we need to be able to
8368 * update the inode several times, and we have no idea of knowing how
8369 * many times that will be, so we can't just reserve 1 item for the
8370 * entirety of the opration, so that has to be done seperately as well.
8371 * Then there is the orphan item, which does indeed need to be held on
8372 * to for the whole operation, and we need nobody to touch this reserved
8373 * space except the orphan code.
8374 *
8375 * So that leaves us with
8376 *
8377 * 1) root->orphan_block_rsv - for the orphan deletion.
8378 * 2) rsv - for the truncate reservation, which we will steal from the
8379 * transaction reservation.
8380 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
8381 * updating the inode.
8382 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06008383 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008384 if (!rsv)
8385 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04008386 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008387 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05008388
Josef Bacik907cbce2011-08-08 13:46:15 -04008389 /*
Josef Bacik07127182011-08-19 10:29:59 -04008390 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04008391 * 1 for updating the inode.
8392 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05008393 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008394 if (IS_ERR(trans)) {
8395 err = PTR_ERR(trans);
8396 goto out;
8397 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05008398
Josef Bacik907cbce2011-08-08 13:46:15 -04008399 /* Migrate the slack space for the truncate to our reserve */
8400 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
8401 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008402 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05008403
Chris Mason5a3f23d2009-03-31 13:27:11 -04008404 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04008405 * So if we truncate and then write and fsync we normally would just
8406 * write the extents that changed, which is a problem if we need to
8407 * first truncate that entire inode. So set this flag so we write out
8408 * all of the extents in the inode to the sync log so we're completely
8409 * safe.
8410 */
8411 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008412 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008413
Yan, Zheng80825102009-11-12 09:35:36 +00008414 while (1) {
8415 ret = btrfs_truncate_inode_items(trans, root, inode,
8416 inode->i_size,
8417 BTRFS_EXTENT_DATA_KEY);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008418 if (ret != -ENOSPC) {
Josef Bacik3893e332011-01-31 16:03:11 -05008419 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00008420 break;
Josef Bacik3893e332011-01-31 16:03:11 -05008421 }
Chris Mason39279cc2007-06-12 06:35:45 -04008422
Josef Bacikfcb80c22011-05-03 10:40:22 -04008423 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008424 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05008425 if (ret) {
8426 err = ret;
8427 break;
8428 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04008429
Yan, Zheng80825102009-11-12 09:35:36 +00008430 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00008431 btrfs_btree_balance_dirty(root);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008432
8433 trans = btrfs_start_transaction(root, 2);
8434 if (IS_ERR(trans)) {
8435 ret = err = PTR_ERR(trans);
8436 trans = NULL;
8437 break;
8438 }
8439
8440 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
8441 rsv, min_size);
8442 BUG_ON(ret); /* shouldn't happen */
8443 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008444 }
8445
8446 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04008447 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008448 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05008449 if (ret)
8450 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00008451 }
8452
Chris Mason917c16b2011-11-08 14:49:59 -05008453 if (trans) {
8454 trans->block_rsv = &root->fs_info->trans_block_rsv;
8455 ret = btrfs_update_inode(trans, root, inode);
8456 if (ret && !err)
8457 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04008458
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008459 ret = btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00008460 btrfs_btree_balance_dirty(root);
Chris Mason917c16b2011-11-08 14:49:59 -05008461 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04008462
8463out:
8464 btrfs_free_block_rsv(root, rsv);
8465
Josef Bacik3893e332011-01-31 16:03:11 -05008466 if (ret && !err)
8467 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05008468
Josef Bacik3893e332011-01-31 16:03:11 -05008469 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04008470}
8471
Sven Wegener3b963622008-06-09 21:57:42 -04008472/*
Chris Masond352ac62008-09-29 15:18:18 -04008473 * create a new subvolume directory/inode (helper for the ioctl).
8474 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05008475int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008476 struct btrfs_root *new_root,
8477 struct btrfs_root *parent_root,
8478 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04008479{
Chris Mason39279cc2007-06-12 06:35:45 -04008480 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04008481 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008482 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008483
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01008484 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
8485 new_dirid, new_dirid,
8486 S_IFDIR | (~current_umask() & S_IRWXUGO),
8487 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04008488 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04008489 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008490 inode->i_op = &btrfs_dir_inode_operations;
8491 inode->i_fop = &btrfs_dir_file_operations;
8492
Miklos Szeredibfe86842011-10-28 14:13:29 +02008493 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04008494 btrfs_i_size_write(inode, 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07008495 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04008496
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008497 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
8498 if (err)
8499 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02008500 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008501 new_root->root_key.objectid, err);
8502
Yan, Zheng76dda932009-09-21 16:00:26 -04008503 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04008504
Yan, Zheng76dda932009-09-21 16:00:26 -04008505 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07008506 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04008507}
8508
Chris Mason39279cc2007-06-12 06:35:45 -04008509struct inode *btrfs_alloc_inode(struct super_block *sb)
8510{
8511 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008512 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008513
8514 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
8515 if (!ei)
8516 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008517
8518 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008519 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04008520 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04008521 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04008522 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008523 ei->delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08008524 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008525 ei->disk_i_size = 0;
8526 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04008527 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008528 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08008529 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008530 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06008531 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008532
Josef Bacik9e0baf62011-07-15 15:16:44 +00008533 spin_lock_init(&ei->lock);
8534 ei->outstanding_extents = 0;
8535 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008536
Josef Bacik72ac3c02012-05-23 14:13:11 -04008537 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08008538 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008539
Miao Xie16cdcec2011-04-22 18:12:22 +08008540 ei->delayed_node = NULL;
8541
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008542 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02008543 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02008544 extent_io_tree_init(&ei->io_tree, &inode->i_data);
8545 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04008546 ei->io_tree.track_uptodate = 1;
8547 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05008548 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008549 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05008550 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008551 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008552 INIT_LIST_HEAD(&ei->delalloc_inodes);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008553 RB_CLEAR_NODE(&ei->rb_node);
8554
8555 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008556}
8557
Josef Bacikaaedb552013-10-11 14:44:09 -04008558#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
8559void btrfs_test_destroy_inode(struct inode *inode)
8560{
8561 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
8562 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8563}
8564#endif
8565
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008566static void btrfs_i_callback(struct rcu_head *head)
8567{
8568 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008569 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8570}
8571
Chris Mason39279cc2007-06-12 06:35:45 -04008572void btrfs_destroy_inode(struct inode *inode)
8573{
Chris Masone6dcd2d2008-07-17 12:53:50 -04008574 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008575 struct btrfs_root *root = BTRFS_I(inode)->root;
8576
Al Virob3d9b7a2012-06-09 13:51:19 -04008577 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04008578 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00008579 WARN_ON(BTRFS_I(inode)->outstanding_extents);
8580 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04008581 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
8582 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08008583 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04008584
Chris Mason5a3f23d2009-03-31 13:27:11 -04008585 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05008586 * This can happen where we create an inode, but somebody else also
8587 * created the same inode and we need to destroy the one we already
8588 * created.
8589 */
8590 if (!root)
8591 goto free;
8592
Josef Bacik8a35d952012-05-23 14:26:42 -04008593 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
8594 &BTRFS_I(inode)->runtime_flags)) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008595 btrfs_info(root->fs_info, "inode %llu still on the orphan list",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02008596 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04008597 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04008598 }
Josef Bacik7b128762008-07-24 12:17:14 -04008599
Chris Masond3977122009-01-05 21:25:51 -05008600 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008601 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
8602 if (!ordered)
8603 break;
8604 else {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008605 btrfs_err(root->fs_info, "found ordered extent %llu %llu on inode cleanup",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02008606 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008607 btrfs_remove_ordered_extent(inode, ordered);
8608 btrfs_put_ordered_extent(ordered);
8609 btrfs_put_ordered_extent(ordered);
8610 }
8611 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008612 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008613 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05008614free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008615 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04008616}
8617
Al Viro45321ac2010-06-07 13:43:19 -04008618int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04008619{
8620 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04008621
Naohiro Aota6379ef92013-06-06 09:56:34 +00008622 if (root == NULL)
8623 return 1;
8624
Liu Bofa6ac872013-02-20 14:10:23 +00008625 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02008626 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04008627 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04008628 else
Al Viro45321ac2010-06-07 13:43:19 -04008629 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04008630}
8631
Sven Wegener0ee0fda2008-07-30 16:54:26 -04008632static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04008633{
8634 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
8635
8636 inode_init_once(&ei->vfs_inode);
8637}
8638
8639void btrfs_destroy_cachep(void)
8640{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10008641 /*
8642 * Make sure all delayed rcu free inodes are flushed before we
8643 * destroy cache.
8644 */
8645 rcu_barrier();
Chris Mason39279cc2007-06-12 06:35:45 -04008646 if (btrfs_inode_cachep)
8647 kmem_cache_destroy(btrfs_inode_cachep);
8648 if (btrfs_trans_handle_cachep)
8649 kmem_cache_destroy(btrfs_trans_handle_cachep);
8650 if (btrfs_transaction_cachep)
8651 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008652 if (btrfs_path_cachep)
8653 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05008654 if (btrfs_free_space_cachep)
8655 kmem_cache_destroy(btrfs_free_space_cachep);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008656 if (btrfs_delalloc_work_cachep)
8657 kmem_cache_destroy(btrfs_delalloc_work_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008658}
8659
8660int btrfs_init_cachep(void)
8661{
David Sterba837e1972012-09-07 03:00:48 -06008662 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008663 sizeof(struct btrfs_inode), 0,
8664 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04008665 if (!btrfs_inode_cachep)
8666 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008667
David Sterba837e1972012-09-07 03:00:48 -06008668 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008669 sizeof(struct btrfs_trans_handle), 0,
8670 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008671 if (!btrfs_trans_handle_cachep)
8672 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008673
David Sterba837e1972012-09-07 03:00:48 -06008674 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008675 sizeof(struct btrfs_transaction), 0,
8676 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008677 if (!btrfs_transaction_cachep)
8678 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008679
David Sterba837e1972012-09-07 03:00:48 -06008680 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008681 sizeof(struct btrfs_path), 0,
8682 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008683 if (!btrfs_path_cachep)
8684 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008685
David Sterba837e1972012-09-07 03:00:48 -06008686 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05008687 sizeof(struct btrfs_free_space), 0,
8688 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
8689 if (!btrfs_free_space_cachep)
8690 goto fail;
8691
Miao Xie8ccf6f192012-10-25 09:28:04 +00008692 btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
8693 sizeof(struct btrfs_delalloc_work), 0,
8694 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
8695 NULL);
8696 if (!btrfs_delalloc_work_cachep)
8697 goto fail;
8698
Chris Mason39279cc2007-06-12 06:35:45 -04008699 return 0;
8700fail:
8701 btrfs_destroy_cachep();
8702 return -ENOMEM;
8703}
8704
8705static int btrfs_getattr(struct vfsmount *mnt,
8706 struct dentry *dentry, struct kstat *stat)
8707{
Miao Xiedf0af1a2013-01-29 10:11:59 +00008708 u64 delalloc_bytes;
Chris Mason39279cc2007-06-12 06:35:45 -04008709 struct inode *inode = dentry->d_inode;
David Sterbafadc0d82011-11-20 07:33:38 -05008710 u32 blocksize = inode->i_sb->s_blocksize;
8711
Chris Mason39279cc2007-06-12 06:35:45 -04008712 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04008713 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05008714 stat->blksize = PAGE_CACHE_SIZE;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008715
8716 spin_lock(&BTRFS_I(inode)->lock);
8717 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
8718 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05008719 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00008720 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04008721 return 0;
8722}
8723
Chris Masond3977122009-01-05 21:25:51 -05008724static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
8725 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04008726{
8727 struct btrfs_trans_handle *trans;
8728 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008729 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04008730 struct inode *new_inode = new_dentry->d_inode;
8731 struct inode *old_inode = old_dentry->d_inode;
8732 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008733 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008734 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04008735 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08008736 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008737
Li Zefan33345d012011-04-20 10:31:50 +08008738 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04008739 return -EPERM;
8740
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008741 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08008742 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05008743 return -EXDEV;
8744
Li Zefan33345d012011-04-20 10:31:50 +08008745 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
8746 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008747 return -ENOTEMPTY;
8748
Chris Mason39279cc2007-06-12 06:35:45 -04008749 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008750 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04008751 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05008752
8753
8754 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04008755 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05008756 new_dentry->d_name.name,
8757 new_dentry->d_name.len);
8758
8759 if (ret) {
8760 if (ret == -EEXIST) {
8761 /* we shouldn't get
8762 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05308763 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05008764 return ret;
8765 }
8766 } else {
8767 /* maybe -EOVERFLOW */
8768 return ret;
8769 }
8770 }
8771 ret = 0;
8772
Chris Mason5a3f23d2009-03-31 13:27:11 -04008773 /*
Chris Mason8d875f92014-08-12 10:47:42 -07008774 * we're using rename to replace one file with another. Start IO on it
8775 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04008776 */
Chris Mason8d875f92014-08-12 10:47:42 -07008777 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04008778 filemap_flush(old_inode->i_mapping);
8779
Yan, Zheng76dda932009-09-21 16:00:26 -04008780 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08008781 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008782 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008783 /*
8784 * We want to reserve the absolute worst case amount of items. So if
8785 * both inodes are subvols and we need to unlink them then that would
8786 * require 4 item modifications, but if they are both normal inodes it
8787 * would require 5 item modifications, so we'll assume their normal
8788 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
8789 * should cover the worst case number of items we'll modify.
8790 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04008791 trans = btrfs_start_transaction(root, 11);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008792 if (IS_ERR(trans)) {
8793 ret = PTR_ERR(trans);
8794 goto out_notrans;
8795 }
Chris Mason5f39d392007-10-15 16:14:19 -04008796
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008797 if (dest != root)
8798 btrfs_record_root_in_trans(trans, dest);
8799
Yan, Zhenga5719522009-09-24 09:17:31 -04008800 ret = btrfs_set_inode_index(new_dir, &index);
8801 if (ret)
8802 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008803
Miao Xie67de1172013-12-26 13:07:06 +08008804 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08008805 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008806 /* force full log commit if subvolume involved. */
Miao Xie995946d2014-04-02 19:51:06 +08008807 btrfs_set_log_full_commit(root->fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008808 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04008809 ret = btrfs_insert_inode_ref(trans, dest,
8810 new_dentry->d_name.name,
8811 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08008812 old_ino,
8813 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04008814 if (ret)
8815 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008816 /*
8817 * this is an ugly little race, but the rename is required
8818 * to make sure that if we crash, the inode is either at the
8819 * old name or the new one. pinning the log transaction lets
8820 * us make sure we don't allow a log commit to come in after
8821 * we unlink the name but before we add the new name back in.
8822 */
8823 btrfs_pin_log_trans(root);
8824 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04008825
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008826 inode_inc_iversion(old_dir);
8827 inode_inc_iversion(new_dir);
8828 inode_inc_iversion(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008829 old_dir->i_ctime = old_dir->i_mtime = ctime;
8830 new_dir->i_ctime = new_dir->i_mtime = ctime;
8831 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04008832
Chris Mason12fcfd22009-03-24 10:24:20 -04008833 if (old_dentry->d_parent != new_dentry->d_parent)
8834 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
8835
Li Zefan33345d012011-04-20 10:31:50 +08008836 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008837 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
8838 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
8839 old_dentry->d_name.name,
8840 old_dentry->d_name.len);
8841 } else {
Al Viro92986792011-03-04 17:14:37 +00008842 ret = __btrfs_unlink_inode(trans, root, old_dir,
8843 old_dentry->d_inode,
8844 old_dentry->d_name.name,
8845 old_dentry->d_name.len);
8846 if (!ret)
8847 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008848 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008849 if (ret) {
8850 btrfs_abort_transaction(trans, root, ret);
8851 goto out_fail;
8852 }
Chris Mason39279cc2007-06-12 06:35:45 -04008853
8854 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008855 inode_inc_iversion(new_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008856 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08008857 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008858 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
8859 root_objectid = BTRFS_I(new_inode)->location.objectid;
8860 ret = btrfs_unlink_subvol(trans, dest, new_dir,
8861 root_objectid,
8862 new_dentry->d_name.name,
8863 new_dentry->d_name.len);
8864 BUG_ON(new_inode->i_nlink == 0);
8865 } else {
8866 ret = btrfs_unlink_inode(trans, dest, new_dir,
8867 new_dentry->d_inode,
8868 new_dentry->d_name.name,
8869 new_dentry->d_name.len);
8870 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04008871 if (!ret && new_inode->i_nlink == 0)
Chris Masone02119d2008-09-05 16:13:11 -04008872 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008873 if (ret) {
8874 btrfs_abort_transaction(trans, root, ret);
8875 goto out_fail;
8876 }
Chris Mason39279cc2007-06-12 06:35:45 -04008877 }
Josef Bacikaec74772008-07-24 12:12:38 -04008878
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008879 ret = btrfs_add_link(trans, new_dir, old_inode,
8880 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04008881 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008882 if (ret) {
8883 btrfs_abort_transaction(trans, root, ret);
8884 goto out_fail;
8885 }
Chris Mason39279cc2007-06-12 06:35:45 -04008886
Miao Xie67de1172013-12-26 13:07:06 +08008887 if (old_inode->i_nlink == 1)
8888 BTRFS_I(old_inode)->dir_index = index;
8889
Li Zefan33345d012011-04-20 10:31:50 +08008890 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04008891 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00008892 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008893 btrfs_end_log_trans(root);
8894 }
Chris Mason39279cc2007-06-12 06:35:45 -04008895out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008896 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008897out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08008898 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008899 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008900
Chris Mason39279cc2007-06-12 06:35:45 -04008901 return ret;
8902}
8903
Miklos Szeredi80ace852014-07-23 15:15:32 +02008904static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
8905 struct inode *new_dir, struct dentry *new_dentry,
8906 unsigned int flags)
8907{
8908 if (flags & ~RENAME_NOREPLACE)
8909 return -EINVAL;
8910
8911 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry);
8912}
8913
Miao Xie8ccf6f192012-10-25 09:28:04 +00008914static void btrfs_run_delalloc_work(struct btrfs_work *work)
8915{
8916 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04008917 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008918
8919 delalloc_work = container_of(work, struct btrfs_delalloc_work,
8920 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04008921 inode = delalloc_work->inode;
8922 if (delalloc_work->wait) {
8923 btrfs_wait_ordered_range(inode, 0, (u64)-1);
8924 } else {
8925 filemap_flush(inode->i_mapping);
8926 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8927 &BTRFS_I(inode)->runtime_flags))
8928 filemap_flush(inode->i_mapping);
8929 }
Miao Xie8ccf6f192012-10-25 09:28:04 +00008930
8931 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -04008932 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008933 else
Josef Bacik9f23e282013-10-28 15:03:41 -04008934 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008935 complete(&delalloc_work->completion);
8936}
8937
8938struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
8939 int wait, int delay_iput)
8940{
8941 struct btrfs_delalloc_work *work;
8942
8943 work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
8944 if (!work)
8945 return NULL;
8946
8947 init_completion(&work->completion);
8948 INIT_LIST_HEAD(&work->list);
8949 work->inode = inode;
8950 work->wait = wait;
8951 work->delay_iput = delay_iput;
Liu Bo9e0af232014-08-15 23:36:53 +08008952 WARN_ON_ONCE(!inode);
8953 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
8954 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008955
8956 return work;
8957}
8958
8959void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
8960{
8961 wait_for_completion(&work->completion);
8962 kmem_cache_free(btrfs_delalloc_work_cachep, work);
8963}
8964
Chris Masond352ac62008-09-29 15:18:18 -04008965/*
8966 * some fairly slow code that needs optimization. This walks the list
8967 * of all the inodes with pending delalloc and forces them to disk.
8968 */
Miao Xie6c255e62014-03-06 13:55:01 +08008969static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
8970 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -04008971{
Chris Masonea8c2812008-08-04 23:17:27 -04008972 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008973 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008974 struct btrfs_delalloc_work *work, *next;
8975 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00008976 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008977 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04008978
Miao Xie8ccf6f192012-10-25 09:28:04 +00008979 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008980 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00008981
Miao Xie573bfb72014-03-06 13:55:03 +08008982 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00008983 spin_lock(&root->delalloc_lock);
8984 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008985 while (!list_empty(&splice)) {
8986 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04008987 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008988
Miao Xieeb73c1b2013-05-15 07:48:22 +00008989 list_move_tail(&binode->delalloc_inodes,
8990 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008991 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00008992 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00008993 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008994 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008995 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008996 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008997
8998 work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
David Sterba5d99a9982014-09-29 19:20:37 +02008999 if (!work) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -04009000 if (delay_iput)
9001 btrfs_add_delayed_iput(inode);
9002 else
9003 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009004 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009005 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009006 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00009007 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +08009008 btrfs_queue_work(root->fs_info->flush_workers,
9009 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +08009010 ret++;
9011 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009012 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009013 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00009014 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04009015 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009016 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04009017
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009018out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00009019 list_for_each_entry_safe(work, next, &works, list) {
9020 list_del_init(&work->list);
9021 btrfs_wait_and_free_delalloc_work(work);
9022 }
9023
Miao Xieeb73c1b2013-05-15 07:48:22 +00009024 if (!list_empty_careful(&splice)) {
9025 spin_lock(&root->delalloc_lock);
9026 list_splice_tail(&splice, &root->delalloc_inodes);
9027 spin_unlock(&root->delalloc_lock);
9028 }
Miao Xie573bfb72014-03-06 13:55:03 +08009029 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009030 return ret;
9031}
9032
9033int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
9034{
9035 int ret;
9036
Wang Shilong2c21b4d2014-01-14 19:42:20 +08009037 if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009038 return -EROFS;
9039
Miao Xie6c255e62014-03-06 13:55:01 +08009040 ret = __start_delalloc_inodes(root, delay_iput, -1);
9041 if (ret > 0)
9042 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009043 /*
9044 * the filemap_flush will queue IO into the worker threads, but
Chris Mason8c8bee12008-09-29 11:19:10 -04009045 * we have to make sure the IO is actually started and that
9046 * ordered extents get created before we return
9047 */
9048 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05009049 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05009050 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04009051 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05009052 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
9053 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04009054 }
9055 atomic_dec(&root->fs_info->async_submit_draining);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009056 return ret;
9057}
9058
Miao Xie6c255e62014-03-06 13:55:01 +08009059int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
9060 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009061{
9062 struct btrfs_root *root;
9063 struct list_head splice;
9064 int ret;
9065
Wang Shilong2c21b4d2014-01-14 19:42:20 +08009066 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009067 return -EROFS;
9068
9069 INIT_LIST_HEAD(&splice);
9070
Miao Xie573bfb72014-03-06 13:55:03 +08009071 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009072 spin_lock(&fs_info->delalloc_root_lock);
9073 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +08009074 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009075 root = list_first_entry(&splice, struct btrfs_root,
9076 delalloc_root);
9077 root = btrfs_grab_fs_root(root);
9078 BUG_ON(!root);
9079 list_move_tail(&root->delalloc_root,
9080 &fs_info->delalloc_roots);
9081 spin_unlock(&fs_info->delalloc_root_lock);
9082
Miao Xie6c255e62014-03-06 13:55:01 +08009083 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009084 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +08009085 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009086 goto out;
9087
Miao Xie6c255e62014-03-06 13:55:01 +08009088 if (nr != -1) {
9089 nr -= ret;
9090 WARN_ON(nr < 0);
9091 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009092 spin_lock(&fs_info->delalloc_root_lock);
9093 }
9094 spin_unlock(&fs_info->delalloc_root_lock);
9095
Miao Xie6c255e62014-03-06 13:55:01 +08009096 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009097 atomic_inc(&fs_info->async_submit_draining);
9098 while (atomic_read(&fs_info->nr_async_submits) ||
9099 atomic_read(&fs_info->async_delalloc_pages)) {
9100 wait_event(fs_info->async_submit_wait,
9101 (atomic_read(&fs_info->nr_async_submits) == 0 &&
9102 atomic_read(&fs_info->async_delalloc_pages) == 0));
9103 }
9104 atomic_dec(&fs_info->async_submit_draining);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009105out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00009106 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009107 spin_lock(&fs_info->delalloc_root_lock);
9108 list_splice_tail(&splice, &fs_info->delalloc_roots);
9109 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009110 }
Miao Xie573bfb72014-03-06 13:55:03 +08009111 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009112 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04009113}
9114
Chris Mason39279cc2007-06-12 06:35:45 -04009115static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
9116 const char *symname)
9117{
9118 struct btrfs_trans_handle *trans;
9119 struct btrfs_root *root = BTRFS_I(dir)->root;
9120 struct btrfs_path *path;
9121 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05009122 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04009123 int err;
9124 int drop_inode = 0;
9125 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05309126 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009127 int name_len;
9128 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04009129 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04009130 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04009131 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04009132
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01009133 name_len = strlen(symname);
Chris Mason39279cc2007-06-12 06:35:45 -04009134 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
9135 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05009136
Josef Bacik9ed74f22009-09-11 16:12:44 -04009137 /*
9138 * 2 items for inode item and ref
9139 * 2 items for dir items
9140 * 1 item for xattr if selinux is on
9141 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04009142 trans = btrfs_start_transaction(root, 5);
9143 if (IS_ERR(trans))
9144 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05009145
Li Zefan581bb052011-04-20 10:06:11 +08009146 err = btrfs_find_free_ino(root, &objectid);
9147 if (err)
9148 goto out_unlock;
9149
Josef Bacikaec74772008-07-24 12:12:38 -04009150 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08009151 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04009152 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009153 if (IS_ERR(inode)) {
9154 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009155 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009156 }
Chris Mason39279cc2007-06-12 06:35:45 -04009157
Casey Schauflerad19db72011-12-15 10:09:07 -05009158 /*
9159 * If the active LSM wants to access the inode during
9160 * d_instantiate it needs these. Smack checks to see
9161 * if the filesystem supports xattrs by looking at the
9162 * ops vector.
9163 */
9164 inode->i_fop = &btrfs_file_operations;
9165 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07009166 inode->i_mapping->a_ops = &btrfs_aops;
9167 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
9168 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
9169
9170 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
9171 if (err)
9172 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05009173
Josef Bacika1b075d2010-11-19 20:36:11 +00009174 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04009175 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07009176 goto out_unlock_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009177
9178 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07009179 if (!path) {
9180 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -07009181 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -07009182 }
Li Zefan33345d012011-04-20 10:31:50 +08009183 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009184 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02009185 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04009186 datasize = btrfs_file_extent_calc_inline_size(name_len);
9187 err = btrfs_insert_empty_item(trans, root, path, &key,
9188 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04009189 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +00009190 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -07009191 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -04009192 }
Chris Mason5f39d392007-10-15 16:14:19 -04009193 leaf = path->nodes[0];
9194 ei = btrfs_item_ptr(leaf, path->slots[0],
9195 struct btrfs_file_extent_item);
9196 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
9197 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04009198 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04009199 btrfs_set_file_extent_encryption(leaf, ei, 0);
9200 btrfs_set_file_extent_compression(leaf, ei, 0);
9201 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
9202 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
9203
Chris Mason39279cc2007-06-12 06:35:45 -04009204 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04009205 write_extent_buffer(leaf, symname, ptr, name_len);
9206 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04009207 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04009208
Chris Mason39279cc2007-06-12 06:35:45 -04009209 inode->i_op = &btrfs_symlink_inode_operations;
9210 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04009211 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04009212 inode_set_bytes(inode, name_len);
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01009213 btrfs_i_size_write(inode, name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -04009214 err = btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07009215 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -04009216 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07009217 goto out_unlock_inode;
9218 }
9219
9220 unlock_new_inode(inode);
9221 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009222
9223out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05009224 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04009225 if (drop_inode) {
9226 inode_dec_link_count(inode);
9227 iput(inode);
9228 }
Liu Bob53d3f52012-11-14 14:34:34 +00009229 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04009230 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07009231
9232out_unlock_inode:
9233 drop_inode = 1;
9234 unlock_new_inode(inode);
9235 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -04009236}
Chris Mason16432982008-04-10 10:23:21 -04009237
Josef Bacik0af3d002010-06-21 14:48:16 -04009238static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
9239 u64 start, u64 num_bytes, u64 min_size,
9240 loff_t actual_len, u64 *alloc_hint,
9241 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04009242{
Josef Bacik5dc562c2012-08-17 13:14:17 -04009243 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
9244 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04009245 struct btrfs_root *root = BTRFS_I(inode)->root;
9246 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04009247 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00009248 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05009249 u64 cur_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04009250 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04009251 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04009252
Josef Bacik0af3d002010-06-21 14:48:16 -04009253 if (trans)
9254 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04009255 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04009256 if (own_trans) {
9257 trans = btrfs_start_transaction(root, 3);
9258 if (IS_ERR(trans)) {
9259 ret = PTR_ERR(trans);
9260 break;
9261 }
Yan Zhengd899e052008-10-30 14:25:28 -04009262 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00009263
Chris Mason154ea282013-03-05 11:11:26 -05009264 cur_bytes = min(num_bytes, 256ULL * 1024 * 1024);
9265 cur_bytes = max(cur_bytes, min_size);
Josef Bacik00361582013-08-14 14:02:47 -04009266 ret = btrfs_reserve_extent(root, cur_bytes, min_size, 0,
Miao Xiee570fd22014-06-19 10:42:50 +08009267 *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009268 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04009269 if (own_trans)
9270 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009271 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009272 }
9273
Yan Zhengd899e052008-10-30 14:25:28 -04009274 ret = insert_reserved_file_extent(trans, inode,
9275 cur_offset, ins.objectid,
9276 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00009277 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04009278 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009279 if (ret) {
Josef Bacik857cc2f2013-10-07 15:21:08 -04009280 btrfs_free_reserved_extent(root, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +08009281 ins.offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009282 btrfs_abort_transaction(trans, root, ret);
9283 if (own_trans)
9284 btrfs_end_transaction(trans, root);
9285 break;
9286 }
Chris Masona1ed8352009-09-11 12:27:37 -04009287 btrfs_drop_extent_cache(inode, cur_offset,
9288 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009289
Josef Bacik5dc562c2012-08-17 13:14:17 -04009290 em = alloc_extent_map();
9291 if (!em) {
9292 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
9293 &BTRFS_I(inode)->runtime_flags);
9294 goto next;
9295 }
9296
9297 em->start = cur_offset;
9298 em->orig_start = cur_offset;
9299 em->len = ins.offset;
9300 em->block_start = ins.objectid;
9301 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05009302 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04009303 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009304 em->bdev = root->fs_info->fs_devices->latest_bdev;
9305 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
9306 em->generation = trans->transid;
9307
9308 while (1) {
9309 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04009310 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04009311 write_unlock(&em_tree->lock);
9312 if (ret != -EEXIST)
9313 break;
9314 btrfs_drop_extent_cache(inode, cur_offset,
9315 cur_offset + ins.offset - 1,
9316 0);
9317 }
9318 free_extent_map(em);
9319next:
Yan Zhengd899e052008-10-30 14:25:28 -04009320 num_bytes -= ins.offset;
9321 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04009322 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009323
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009324 inode_inc_iversion(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04009325 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02009326 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04009327 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04009328 (actual_len > inode->i_size) &&
9329 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009330 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00009331 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009332 else
Josef Bacik55a61d12010-11-22 18:50:32 +00009333 i_size = cur_offset;
9334 i_size_write(inode, i_size);
9335 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009336 }
9337
Yan Zhengd899e052008-10-30 14:25:28 -04009338 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009339
9340 if (ret) {
9341 btrfs_abort_transaction(trans, root, ret);
9342 if (own_trans)
9343 btrfs_end_transaction(trans, root);
9344 break;
9345 }
Yan Zhengd899e052008-10-30 14:25:28 -04009346
Josef Bacik0af3d002010-06-21 14:48:16 -04009347 if (own_trans)
9348 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009349 }
Yan Zhengd899e052008-10-30 14:25:28 -04009350 return ret;
9351}
9352
Josef Bacik0af3d002010-06-21 14:48:16 -04009353int btrfs_prealloc_file_range(struct inode *inode, int mode,
9354 u64 start, u64 num_bytes, u64 min_size,
9355 loff_t actual_len, u64 *alloc_hint)
9356{
9357 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9358 min_size, actual_len, alloc_hint,
9359 NULL);
9360}
9361
9362int btrfs_prealloc_file_range_trans(struct inode *inode,
9363 struct btrfs_trans_handle *trans, int mode,
9364 u64 start, u64 num_bytes, u64 min_size,
9365 loff_t actual_len, u64 *alloc_hint)
9366{
9367 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9368 min_size, actual_len, alloc_hint, trans);
9369}
9370
Chris Masone6dcd2d2008-07-17 12:53:50 -04009371static int btrfs_set_page_dirty(struct page *page)
9372{
Chris Masone6dcd2d2008-07-17 12:53:50 -04009373 return __set_page_dirty_nobuffers(page);
9374}
9375
Al Viro10556cb2011-06-20 19:28:19 -04009376static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05009377{
Li Zefanb83cc962010-12-20 16:04:08 +08009378 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009379 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08009380
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009381 if (mask & MAY_WRITE &&
9382 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
9383 if (btrfs_root_readonly(root))
9384 return -EROFS;
9385 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
9386 return -EACCES;
9387 }
Al Viro2830ba72011-06-20 19:16:29 -04009388 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05009389}
Chris Mason39279cc2007-06-12 06:35:45 -04009390
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009391static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
9392{
9393 struct btrfs_trans_handle *trans;
9394 struct btrfs_root *root = BTRFS_I(dir)->root;
9395 struct inode *inode = NULL;
9396 u64 objectid;
9397 u64 index;
9398 int ret = 0;
9399
9400 /*
9401 * 5 units required for adding orphan entry
9402 */
9403 trans = btrfs_start_transaction(root, 5);
9404 if (IS_ERR(trans))
9405 return PTR_ERR(trans);
9406
9407 ret = btrfs_find_free_ino(root, &objectid);
9408 if (ret)
9409 goto out;
9410
9411 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
9412 btrfs_ino(dir), objectid, mode, &index);
9413 if (IS_ERR(inode)) {
9414 ret = PTR_ERR(inode);
9415 inode = NULL;
9416 goto out;
9417 }
9418
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009419 inode->i_fop = &btrfs_file_operations;
9420 inode->i_op = &btrfs_file_inode_operations;
9421
9422 inode->i_mapping->a_ops = &btrfs_aops;
9423 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
9424 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
9425
Chris Masonb0d5d102014-09-08 13:08:51 -07009426 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
9427 if (ret)
9428 goto out_inode;
9429
9430 ret = btrfs_update_inode(trans, root, inode);
9431 if (ret)
9432 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009433 ret = btrfs_orphan_add(trans, inode);
9434 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -07009435 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009436
Filipe Manana5762b5c2014-08-01 00:10:32 +01009437 /*
9438 * We set number of links to 0 in btrfs_new_inode(), and here we set
9439 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
9440 * through:
9441 *
9442 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
9443 */
9444 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -07009445 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009446 d_tmpfile(dentry, inode);
9447 mark_inode_dirty(inode);
9448
9449out:
9450 btrfs_end_transaction(trans, root);
9451 if (ret)
9452 iput(inode);
9453 btrfs_balance_delayed_items(root);
9454 btrfs_btree_balance_dirty(root);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009455 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -07009456
9457out_inode:
9458 unlock_new_inode(inode);
9459 goto out;
9460
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009461}
9462
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009463static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05009464 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04009465 .lookup = btrfs_lookup,
9466 .create = btrfs_create,
9467 .unlink = btrfs_unlink,
9468 .link = btrfs_link,
9469 .mkdir = btrfs_mkdir,
9470 .rmdir = btrfs_rmdir,
Miklos Szeredi80ace852014-07-23 15:15:32 +02009471 .rename2 = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -04009472 .symlink = btrfs_symlink,
9473 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04009474 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009475 .setxattr = btrfs_setxattr,
9476 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05009477 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009478 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05009479 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009480 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009481 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -07009482 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009483 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -04009484};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009485static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009486 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05009487 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009488 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009489 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -07009490 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04009491};
Yan, Zheng76dda932009-09-21 16:00:26 -04009492
Alexey Dobriyan828c0952009-10-01 15:43:56 -07009493static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009494 .llseek = generic_file_llseek,
9495 .read = generic_read_dir,
Al Viro9cdda8d2013-05-22 16:48:09 -04009496 .iterate = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04009497 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04009498#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04009499 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04009500#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04009501 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04009502 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04009503};
9504
Chris Masond1310b22008-01-24 16:13:08 -05009505static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04009506 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05009507 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04009508 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04009509 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04009510 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04009511 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05009512 .set_bit_hook = btrfs_set_bit_hook,
9513 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04009514 .merge_extent_hook = btrfs_merge_extent_hook,
9515 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04009516};
9517
Chris Mason35054392009-01-21 13:11:13 -05009518/*
9519 * btrfs doesn't support the bmap operation because swapfiles
9520 * use bmap to make a mapping of extents in the file. They assume
9521 * these extents won't change over the life of the file and they
9522 * use the bmap result to do IO directly to the drive.
9523 *
9524 * the btrfs bmap call would return logical addresses that aren't
9525 * suitable for IO and they also will change frequently as COW
9526 * operations happen. So, swapfile + btrfs == corruption.
9527 *
9528 * For now we're avoiding this by dropping bmap.
9529 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07009530static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04009531 .readpage = btrfs_readpage,
9532 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -04009533 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05009534 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04009535 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04009536 .invalidatepage = btrfs_invalidatepage,
9537 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04009538 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02009539 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04009540};
9541
Alexey Dobriyan7f094102009-09-21 17:01:10 -07009542static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04009543 .readpage = btrfs_readpage,
9544 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04009545 .invalidatepage = btrfs_invalidatepage,
9546 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04009547};
9548
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009549static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009550 .getattr = btrfs_getattr,
9551 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009552 .setxattr = btrfs_setxattr,
9553 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05009554 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009555 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05009556 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05009557 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009558 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009559 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04009560 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04009561};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009562static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04009563 .getattr = btrfs_getattr,
9564 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05009565 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009566 .setxattr = btrfs_setxattr,
9567 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04009568 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009569 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009570 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009571 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04009572 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -04009573};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009574static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009575 .readlink = generic_readlink,
9576 .follow_link = page_follow_link_light,
9577 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00009578 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -05009579 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05009580 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05009581 .setxattr = btrfs_setxattr,
9582 .getxattr = btrfs_getxattr,
9583 .listxattr = btrfs_listxattr,
9584 .removexattr = btrfs_removexattr,
Josef Bacike41f9412012-03-26 09:46:47 -04009585 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04009586};
Yan, Zheng76dda932009-09-21 16:00:26 -04009587
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04009588const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04009589 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -04009590 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -04009591};