blob: 3b957921ba5929075c44867150565ef28da450d9 [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 Masonb888db22007-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 Masonb888db22007-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 Masonf03d9302009-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;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100530 unsigned long page_error_op;
531
Josef Bacik151a41b2013-07-29 13:22:24 -0400532 clear_flags |= (ret < 0) ? EXTENT_DO_ACCOUNTING : 0;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100533 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400534
Chris Mason771ed682008-11-06 22:02:51 -0500535 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100536 * inline extent creation worked or returned error,
537 * we don't need to create any more async work items.
538 * Unlock and free up our temp pages.
Chris Mason771ed682008-11-06 22:02:51 -0500539 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400540 extent_clear_unlock_delalloc(inode, start, end, NULL,
Josef Bacik151a41b2013-07-29 13:22:24 -0400541 clear_flags, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400542 PAGE_CLEAR_DIRTY |
543 PAGE_SET_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100544 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400545 PAGE_END_WRITEBACK);
Chris Masonc8b97812008-10-29 14:49:59 -0400546 goto free_pages_out;
547 }
548 }
549
550 if (will_compress) {
551 /*
552 * we aren't doing an inline extent round the compressed size
553 * up to a block size boundary so the allocator does sane
554 * things
555 */
Qu Wenruofda28322013-02-26 08:10:22 +0000556 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400557
558 /*
559 * one last check to make sure the compression is really a
560 * win, compare the page count read with the blocks on disk
561 */
Qu Wenruofda28322013-02-26 08:10:22 +0000562 total_in = ALIGN(total_in, PAGE_CACHE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400563 if (total_compressed >= total_in) {
564 will_compress = 0;
565 } else {
Chris Masonc8b97812008-10-29 14:49:59 -0400566 num_bytes = total_in;
567 }
568 }
569 if (!will_compress && pages) {
570 /*
571 * the compression code ran but failed to make things smaller,
572 * free any pages it allocated and our page pointer array
573 */
574 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400575 WARN_ON(pages[i]->mapping);
Chris Masonc8b97812008-10-29 14:49:59 -0400576 page_cache_release(pages[i]);
577 }
578 kfree(pages);
579 pages = NULL;
580 total_compressed = 0;
581 nr_pages_ret = 0;
582
583 /* flag the file so we don't compress in the future */
Chris Mason1e701a32010-03-11 09:42:04 -0500584 if (!btrfs_test_opt(root, FORCE_COMPRESS) &&
585 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500586 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500587 }
Chris Masonc8b97812008-10-29 14:49:59 -0400588 }
Chris Mason771ed682008-11-06 22:02:51 -0500589 if (will_compress) {
590 *num_added += 1;
591
592 /* the async work queues will take care of doing actual
593 * allocation on disk for these compressed pages,
594 * and will submit them to the elevator.
595 */
596 add_async_extent(async_cow, start, num_bytes,
Li Zefan261507a02010-12-17 14:21:50 +0800597 total_compressed, pages, nr_pages_ret,
598 compress_type);
Chris Mason771ed682008-11-06 22:02:51 -0500599
Yan, Zheng24ae6362010-12-06 07:02:36 +0000600 if (start + num_bytes < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500601 start += num_bytes;
602 pages = NULL;
603 cond_resched();
604 goto again;
605 }
606 } else {
Chris Masonf03d9302009-02-04 09:31:06 -0500607cleanup_and_bail_uncompressed:
Chris Mason771ed682008-11-06 22:02:51 -0500608 /*
609 * No compression, but we still need to write the pages in
610 * the file we've been given so far. redirty the locked
611 * page if it corresponds to our extent and set things up
612 * for the async work queue to run cow_file_range to do
613 * the normal delalloc dance
614 */
615 if (page_offset(locked_page) >= start &&
616 page_offset(locked_page) <= end) {
617 __set_page_dirty_nobuffers(locked_page);
618 /* unlocked later on in the async handlers */
619 }
Chris Mason4adaa612013-03-26 13:07:00 -0400620 if (redirty)
621 extent_range_redirty_for_io(inode, start, end);
Li Zefan261507a02010-12-17 14:21:50 +0800622 add_async_extent(async_cow, start, end - start + 1,
623 0, NULL, 0, BTRFS_COMPRESS_NONE);
Chris Mason771ed682008-11-06 22:02:51 -0500624 *num_added += 1;
625 }
626
Filipe Mananac44f6492014-10-09 21:15:44 +0100627 return;
Chris Mason771ed682008-11-06 22:02:51 -0500628
629free_pages_out:
630 for (i = 0; i < nr_pages_ret; i++) {
631 WARN_ON(pages[i]->mapping);
632 page_cache_release(pages[i]);
633 }
Chris Masond3977122009-01-05 21:25:51 -0500634 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500635}
Chris Mason771ed682008-11-06 22:02:51 -0500636
Filipe Manana40ae8372014-10-06 22:14:24 +0100637static void free_async_extent_pages(struct async_extent *async_extent)
638{
639 int i;
640
641 if (!async_extent->pages)
642 return;
643
644 for (i = 0; i < async_extent->nr_pages; i++) {
645 WARN_ON(async_extent->pages[i]->mapping);
646 page_cache_release(async_extent->pages[i]);
647 }
648 kfree(async_extent->pages);
649 async_extent->nr_pages = 0;
650 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500651}
652
653/*
654 * phase two of compressed writeback. This is the ordered portion
655 * of the code, which only gets called in the order the work was
656 * queued. We walk all the async extents created by compress_file_range
657 * and send them down to the disk.
658 */
Filipe Mananadec8f172014-10-06 22:14:26 +0100659static noinline void submit_compressed_extents(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500660 struct async_cow *async_cow)
661{
662 struct async_extent *async_extent;
663 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500664 struct btrfs_key ins;
665 struct extent_map *em;
666 struct btrfs_root *root = BTRFS_I(inode)->root;
667 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
668 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500669 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500670
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500671again:
Chris Masond3977122009-01-05 21:25:51 -0500672 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500673 async_extent = list_entry(async_cow->extents.next,
674 struct async_extent, list);
675 list_del(&async_extent->list);
676
677 io_tree = &BTRFS_I(inode)->io_tree;
678
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500679retry:
Chris Mason771ed682008-11-06 22:02:51 -0500680 /* did the compression code fall back to uncompressed IO? */
681 if (!async_extent->pages) {
682 int page_started = 0;
683 unsigned long nr_written = 0;
684
685 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000686 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100687 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500688
689 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500690 ret = cow_file_range(inode, async_cow->locked_page,
691 async_extent->start,
692 async_extent->start +
693 async_extent->ram_size - 1,
694 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500695
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100696 /* JDM XXX */
697
Chris Mason771ed682008-11-06 22:02:51 -0500698 /*
699 * if page_started, cow_file_range inserted an
700 * inline extent and took care of all the unlocking
701 * and IO for us. Otherwise, we need to submit
702 * all those pages down to the drive.
703 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500704 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500705 extent_write_locked_range(io_tree,
706 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500707 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500708 async_extent->ram_size - 1,
709 btrfs_get_extent,
710 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500711 else if (ret)
712 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500713 kfree(async_extent);
714 cond_resched();
715 continue;
716 }
717
718 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100719 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500720
Josef Bacik00361582013-08-14 14:02:47 -0400721 ret = btrfs_reserve_extent(root,
Chris Mason771ed682008-11-06 22:02:51 -0500722 async_extent->compressed_size,
723 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800724 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500725 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100726 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500727
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400728 if (ret == -ENOSPC) {
729 unlock_extent(io_tree, async_extent->start,
730 async_extent->start +
731 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800732
733 /*
734 * we need to redirty the pages if we decide to
735 * fallback to uncompressed IO, otherwise we
736 * will not submit these pages down to lower
737 * layers.
738 */
739 extent_range_redirty_for_io(inode,
740 async_extent->start,
741 async_extent->start +
742 async_extent->ram_size - 1);
743
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100744 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400745 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500746 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500747 }
748
Yan, Zhengc2167752009-11-12 09:34:21 +0000749 /*
750 * here we're doing allocation and writeback of the
751 * compressed pages
752 */
753 btrfs_drop_extent_cache(inode, async_extent->start,
754 async_extent->start +
755 async_extent->ram_size - 1, 0);
756
David Sterba172ddd62011-04-21 00:48:27 +0200757 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000758 if (!em) {
759 ret = -ENOMEM;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500760 goto out_free_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000761 }
Chris Mason771ed682008-11-06 22:02:51 -0500762 em->start = async_extent->start;
763 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500764 em->orig_start = em->start;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400765 em->mod_start = em->start;
766 em->mod_len = em->len;
Chris Mason771ed682008-11-06 22:02:51 -0500767
768 em->block_start = ins.objectid;
769 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500770 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400771 em->ram_bytes = async_extent->ram_size;
Chris Mason771ed682008-11-06 22:02:51 -0500772 em->bdev = root->fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800773 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500774 set_bit(EXTENT_FLAG_PINNED, &em->flags);
775 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400776 em->generation = -1;
Chris Mason771ed682008-11-06 22:02:51 -0500777
Chris Masond3977122009-01-05 21:25:51 -0500778 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400779 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400780 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400781 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500782 if (ret != -EEXIST) {
783 free_extent_map(em);
784 break;
785 }
786 btrfs_drop_extent_cache(inode, async_extent->start,
787 async_extent->start +
788 async_extent->ram_size - 1, 0);
789 }
790
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500791 if (ret)
792 goto out_free_reserve;
793
Li Zefan261507a02010-12-17 14:21:50 +0800794 ret = btrfs_add_ordered_extent_compress(inode,
795 async_extent->start,
796 ins.objectid,
797 async_extent->ram_size,
798 ins.offset,
799 BTRFS_ORDERED_COMPRESSED,
800 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100801 if (ret) {
802 btrfs_drop_extent_cache(inode, async_extent->start,
803 async_extent->start +
804 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500805 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100806 }
Chris Mason771ed682008-11-06 22:02:51 -0500807
Chris Mason771ed682008-11-06 22:02:51 -0500808 /*
809 * clear dirty, set writeback and unlock the pages.
810 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400811 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400812 async_extent->start +
813 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400814 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
815 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400816 PAGE_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500817 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500818 async_extent->start,
819 async_extent->ram_size,
820 ins.objectid,
821 ins.offset, async_extent->pages,
822 async_extent->nr_pages);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100823 if (ret) {
824 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
825 struct page *p = async_extent->pages[0];
826 const u64 start = async_extent->start;
827 const u64 end = start + async_extent->ram_size - 1;
828
829 p->mapping = inode->i_mapping;
830 tree->ops->writepage_end_io_hook(p, start, end,
831 NULL, 0);
832 p->mapping = NULL;
833 extent_clear_unlock_delalloc(inode, start, end, NULL, 0,
834 PAGE_END_WRITEBACK |
835 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100836 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100837 }
Chris Mason771ed682008-11-06 22:02:51 -0500838 alloc_hint = ins.objectid + ins.offset;
839 kfree(async_extent);
840 cond_resched();
841 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100842 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500843out_free_reserve:
Miao Xiee570fd22014-06-19 10:42:50 +0800844 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100845out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400846 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500847 async_extent->start +
848 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400849 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400850 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
851 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100852 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
853 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100854 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100855 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500856 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500857}
858
Josef Bacik4b46fce2010-05-23 11:00:55 -0400859static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
860 u64 num_bytes)
861{
862 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
863 struct extent_map *em;
864 u64 alloc_hint = 0;
865
866 read_lock(&em_tree->lock);
867 em = search_extent_mapping(em_tree, start, num_bytes);
868 if (em) {
869 /*
870 * if block start isn't an actual block number then find the
871 * first block in this inode and use that as a hint. If that
872 * block is also bogus then just don't worry about it.
873 */
874 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
875 free_extent_map(em);
876 em = search_extent_mapping(em_tree, 0, 0);
877 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
878 alloc_hint = em->block_start;
879 if (em)
880 free_extent_map(em);
881 } else {
882 alloc_hint = em->block_start;
883 free_extent_map(em);
884 }
885 }
886 read_unlock(&em_tree->lock);
887
888 return alloc_hint;
889}
890
Chris Mason771ed682008-11-06 22:02:51 -0500891/*
892 * when extent_io.c finds a delayed allocation range in the file,
893 * the call backs end up in this code. The basic idea is to
894 * allocate extents on disk for the range, and create ordered data structs
895 * in ram to track those extents.
896 *
897 * locked_page is the page that writepage had locked already. We use
898 * it to make sure we don't do extra locks or unlocks.
899 *
900 * *page_started is set to one if we unlock locked_page and do everything
901 * required to start IO on it. It may be clean and already done with
902 * IO when we return.
903 */
Josef Bacik00361582013-08-14 14:02:47 -0400904static noinline int cow_file_range(struct inode *inode,
905 struct page *locked_page,
906 u64 start, u64 end, int *page_started,
907 unsigned long *nr_written,
908 int unlock)
Chris Mason771ed682008-11-06 22:02:51 -0500909{
Josef Bacik00361582013-08-14 14:02:47 -0400910 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500911 u64 alloc_hint = 0;
912 u64 num_bytes;
913 unsigned long ram_size;
914 u64 disk_num_bytes;
915 u64 cur_alloc_size;
916 u64 blocksize = root->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500917 struct btrfs_key ins;
918 struct extent_map *em;
919 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
920 int ret = 0;
921
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400922 if (btrfs_is_free_space_inode(inode)) {
923 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500924 ret = -EINVAL;
925 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400926 }
Chris Mason771ed682008-11-06 22:02:51 -0500927
Qu Wenruofda28322013-02-26 08:10:22 +0000928 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500929 num_bytes = max(blocksize, num_bytes);
930 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500931
Chris Mason4cb53002011-05-24 15:35:30 -0400932 /* if this is a small write inside eof, kick off defrag */
Liu Bo4cb13e52012-03-29 09:57:45 -0400933 if (num_bytes < 64 * 1024 &&
934 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Josef Bacik00361582013-08-14 14:02:47 -0400935 btrfs_add_inode_defrag(NULL, inode);
Chris Mason4cb53002011-05-24 15:35:30 -0400936
Chris Mason771ed682008-11-06 22:02:51 -0500937 if (start == 0) {
938 /* lets try to make an inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400939 ret = cow_file_range_inline(root, inode, start, end, 0, 0,
940 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500941 if (ret == 0) {
Josef Bacikc2790a22013-07-29 11:20:47 -0400942 extent_clear_unlock_delalloc(inode, start, end, NULL,
943 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400944 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400945 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
946 PAGE_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000947
Chris Mason771ed682008-11-06 22:02:51 -0500948 *nr_written = *nr_written +
949 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
950 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500951 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100952 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100953 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500954 }
955 }
Chris Masonc8b97812008-10-29 14:49:59 -0400956
957 BUG_ON(disk_num_bytes >
David Sterba6c417612011-04-13 15:41:04 +0200958 btrfs_super_total_bytes(root->fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400959
Josef Bacik4b46fce2010-05-23 11:00:55 -0400960 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400961 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400962
Chris Masond3977122009-01-05 21:25:51 -0500963 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400964 unsigned long op;
965
Josef Bacik287a0ab2010-03-19 18:07:23 +0000966 cur_alloc_size = disk_num_bytes;
Josef Bacik00361582013-08-14 14:02:47 -0400967 ret = btrfs_reserve_extent(root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500968 root->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +0800969 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -0400970 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100971 goto out_unlock;
Chris Masond3977122009-01-05 21:25:51 -0500972
David Sterba172ddd62011-04-21 00:48:27 +0200973 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000974 if (!em) {
975 ret = -ENOMEM;
Liu Boace68ba2013-04-22 10:53:47 +0000976 goto out_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000977 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400978 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500979 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500980 ram_size = ins.offset;
981 em->len = ins.offset;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400982 em->mod_start = em->start;
983 em->mod_len = em->len;
Chris Masonc8b97812008-10-29 14:49:59 -0400984
Chris Masone6dcd2d2008-07-17 12:53:50 -0400985 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400986 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500987 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400988 em->ram_bytes = ram_size;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400989 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400990 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400991 em->generation = -1;
Chris Masonc8b97812008-10-29 14:49:59 -0400992
Chris Masond3977122009-01-05 21:25:51 -0500993 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400994 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400995 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400996 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400997 if (ret != -EEXIST) {
998 free_extent_map(em);
999 break;
1000 }
1001 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -04001002 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001003 }
Liu Boace68ba2013-04-22 10:53:47 +00001004 if (ret)
1005 goto out_reserve;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001006
Chris Mason98d20f62008-04-14 09:46:10 -04001007 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001008 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -05001009 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001010 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001011 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001012
Yan Zheng17d217f2008-12-12 10:03:38 -05001013 if (root->root_key.objectid ==
1014 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1015 ret = btrfs_reloc_clone_csums(inode, start,
1016 cur_alloc_size);
Josef Bacik00361582013-08-14 14:02:47 -04001017 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001018 goto out_drop_extent_cache;
Yan Zheng17d217f2008-12-12 10:03:38 -05001019 }
1020
Chris Masond3977122009-01-05 21:25:51 -05001021 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -04001022 break;
Chris Masond3977122009-01-05 21:25:51 -05001023
Chris Masonc8b97812008-10-29 14:49:59 -04001024 /* we're not doing compressed IO, don't unlock the first
1025 * page (which the caller expects to stay locked), don't
1026 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001027 *
1028 * Do set the Private2 bit so we know this page was properly
1029 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001030 */
Josef Bacikc2790a22013-07-29 11:20:47 -04001031 op = unlock ? PAGE_UNLOCK : 0;
1032 op |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001033
Josef Bacikc2790a22013-07-29 11:20:47 -04001034 extent_clear_unlock_delalloc(inode, start,
1035 start + ram_size - 1, locked_page,
1036 EXTENT_LOCKED | EXTENT_DELALLOC,
1037 op);
Chris Masonc8b97812008-10-29 14:49:59 -04001038 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001039 num_bytes -= cur_alloc_size;
1040 alloc_hint = ins.objectid + ins.offset;
1041 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -04001042 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001043out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001044 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001045
Filipe Mananad9f85962014-08-25 10:43:00 +01001046out_drop_extent_cache:
1047 btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001048out_reserve:
Miao Xiee570fd22014-06-19 10:42:50 +08001049 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001050out_unlock:
Josef Bacikc2790a22013-07-29 11:20:47 -04001051 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Josef Bacik151a41b2013-07-29 13:22:24 -04001052 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
1053 EXTENT_DELALLOC | EXTENT_DEFRAG,
1054 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
1055 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001056 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001057}
Chris Masonc8b97812008-10-29 14:49:59 -04001058
Chris Mason771ed682008-11-06 22:02:51 -05001059/*
1060 * work queue call back to started compression on a file and pages
1061 */
1062static noinline void async_cow_start(struct btrfs_work *work)
1063{
1064 struct async_cow *async_cow;
1065 int num_added = 0;
1066 async_cow = container_of(work, struct async_cow, work);
1067
1068 compress_file_range(async_cow->inode, async_cow->locked_page,
1069 async_cow->start, async_cow->end, async_cow,
1070 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001071 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001072 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001073 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001074 }
Chris Mason771ed682008-11-06 22:02:51 -05001075}
1076
1077/*
1078 * work queue call back to submit previously compressed pages
1079 */
1080static noinline void async_cow_submit(struct btrfs_work *work)
1081{
1082 struct async_cow *async_cow;
1083 struct btrfs_root *root;
1084 unsigned long nr_pages;
1085
1086 async_cow = container_of(work, struct async_cow, work);
1087
1088 root = async_cow->root;
1089 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
1090 PAGE_CACHE_SHIFT;
1091
Josef Bacik66657b32012-08-01 15:36:24 -04001092 if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) <
Liu Bo287082b2012-06-28 04:02:24 -06001093 5 * 1024 * 1024 &&
Chris Mason771ed682008-11-06 22:02:51 -05001094 waitqueue_active(&root->fs_info->async_submit_wait))
1095 wake_up(&root->fs_info->async_submit_wait);
1096
Chris Masond3977122009-01-05 21:25:51 -05001097 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001098 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001099}
Chris Masonc8b97812008-10-29 14:49:59 -04001100
Chris Mason771ed682008-11-06 22:02:51 -05001101static noinline void async_cow_free(struct btrfs_work *work)
1102{
1103 struct async_cow *async_cow;
1104 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001105 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001106 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001107 kfree(async_cow);
1108}
1109
1110static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1111 u64 start, u64 end, int *page_started,
1112 unsigned long *nr_written)
1113{
1114 struct async_cow *async_cow;
1115 struct btrfs_root *root = BTRFS_I(inode)->root;
1116 unsigned long nr_pages;
1117 u64 cur_end;
Liu Bo287082b2012-06-28 04:02:24 -06001118 int limit = 10 * 1024 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -05001119
Chris Masona3429ab2009-10-08 12:30:20 -04001120 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1121 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001122 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001123 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001124 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001125 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001126 async_cow->root = root;
1127 async_cow->locked_page = locked_page;
1128 async_cow->start = start;
1129
Wang Shilongf79707b2014-07-17 11:44:09 +08001130 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
1131 !btrfs_test_opt(root, FORCE_COMPRESS))
Chris Mason771ed682008-11-06 22:02:51 -05001132 cur_end = end;
1133 else
1134 cur_end = min(end, start + 512 * 1024 - 1);
1135
1136 async_cow->end = cur_end;
1137 INIT_LIST_HEAD(&async_cow->extents);
1138
Liu Bo9e0af232014-08-15 23:36:53 +08001139 btrfs_init_work(&async_cow->work,
1140 btrfs_delalloc_helper,
1141 async_cow_start, async_cow_submit,
1142 async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001143
Chris Mason771ed682008-11-06 22:02:51 -05001144 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
1145 PAGE_CACHE_SHIFT;
1146 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
1147
Qu Wenruoafe3d242014-02-28 10:46:07 +08001148 btrfs_queue_work(root->fs_info->delalloc_workers,
1149 &async_cow->work);
Chris Mason771ed682008-11-06 22:02:51 -05001150
1151 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
1152 wait_event(root->fs_info->async_submit_wait,
1153 (atomic_read(&root->fs_info->async_delalloc_pages) <
1154 limit));
1155 }
1156
Chris Masond3977122009-01-05 21:25:51 -05001157 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -05001158 atomic_read(&root->fs_info->async_delalloc_pages)) {
1159 wait_event(root->fs_info->async_submit_wait,
1160 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1161 0));
1162 }
1163
1164 *nr_written += nr_pages;
1165 start = cur_end + 1;
1166 }
1167 *page_started = 1;
1168 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001169}
1170
Chris Masond3977122009-01-05 21:25:51 -05001171static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001172 u64 bytenr, u64 num_bytes)
1173{
1174 int ret;
1175 struct btrfs_ordered_sum *sums;
1176 LIST_HEAD(list);
1177
Yan Zheng07d400a2009-01-06 11:42:00 -05001178 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001179 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001180 if (ret == 0 && list_empty(&list))
1181 return 0;
1182
1183 while (!list_empty(&list)) {
1184 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1185 list_del(&sums->list);
1186 kfree(sums);
1187 }
1188 return 1;
1189}
1190
Chris Masond352ac62008-09-29 15:18:18 -04001191/*
1192 * when nowcow writeback call back. This checks for snapshots or COW copies
1193 * of the extents that exist in the file, and COWs the file as required.
1194 *
1195 * If no cow copies or snapshots exist, we write directly to the existing
1196 * blocks on disk
1197 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001198static noinline int run_delalloc_nocow(struct inode *inode,
1199 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001200 u64 start, u64 end, int *page_started, int force,
1201 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001202{
Chris Masonbe20aa92007-12-17 20:14:01 -05001203 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001204 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001205 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001206 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001207 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001208 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001209 u64 cow_start;
1210 u64 cur_offset;
1211 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001212 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001213 u64 disk_bytenr;
1214 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001215 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001216 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001217 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001218 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001219 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001220 int nocow;
1221 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001222 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001223 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001224
1225 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001226 if (!path) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001227 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1228 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001229 EXTENT_DO_ACCOUNTING |
1230 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001231 PAGE_CLEAR_DIRTY |
1232 PAGE_SET_WRITEBACK |
1233 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001234 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001235 }
Li Zefan82d59022011-04-20 10:33:24 +08001236
Liu Bo83eea1f2012-07-10 05:28:39 -06001237 nolock = btrfs_is_free_space_inode(inode);
Li Zefan82d59022011-04-20 10:33:24 +08001238
1239 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001240 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001241 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001242 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001243
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001244 if (IS_ERR(trans)) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001245 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1246 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001247 EXTENT_DO_ACCOUNTING |
1248 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001249 PAGE_CLEAR_DIRTY |
1250 PAGE_SET_WRITEBACK |
1251 PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001252 btrfs_free_path(path);
1253 return PTR_ERR(trans);
1254 }
1255
Josef Bacik74b21072011-04-13 12:02:53 -04001256 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001257
Yan Zheng80ff3852008-10-30 14:20:02 -04001258 cow_start = (u64)-1;
1259 cur_offset = start;
1260 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001261 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001262 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001263 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001264 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001265 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1266 leaf = path->nodes[0];
1267 btrfs_item_key_to_cpu(leaf, &found_key,
1268 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001269 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001270 found_key.type == BTRFS_EXTENT_DATA_KEY)
1271 path->slots[0]--;
1272 }
1273 check_prev = 0;
1274next_slot:
1275 leaf = path->nodes[0];
1276 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1277 ret = btrfs_next_leaf(root, path);
Josef Bacikd788a342013-10-25 16:55:08 -04001278 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001279 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001280 if (ret > 0)
1281 break;
1282 leaf = path->nodes[0];
1283 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001284
Yan Zheng80ff3852008-10-30 14:20:02 -04001285 nocow = 0;
1286 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001287 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001288 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001289
Li Zefan33345d012011-04-20 10:31:50 +08001290 if (found_key.objectid > ino ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001291 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1292 found_key.offset > end)
1293 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001294
Yan Zheng80ff3852008-10-30 14:20:02 -04001295 if (found_key.offset > cur_offset) {
1296 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001297 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001298 goto out_check;
1299 }
Chris Masonc31f8832008-01-08 15:46:31 -05001300
Yan Zheng80ff3852008-10-30 14:20:02 -04001301 fi = btrfs_item_ptr(leaf, path->slots[0],
1302 struct btrfs_file_extent_item);
1303 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001304
Josef Bacikcc95bef2013-04-04 14:31:27 -04001305 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001306 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1307 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001308 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001309 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001310 extent_end = found_key.offset +
1311 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001312 disk_num_bytes =
1313 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001314 if (extent_end <= start) {
1315 path->slots[0]++;
1316 goto next_slot;
1317 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001318 if (disk_bytenr == 0)
1319 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001320 if (btrfs_file_extent_compression(leaf, fi) ||
1321 btrfs_file_extent_encryption(leaf, fi) ||
1322 btrfs_file_extent_other_encoding(leaf, fi))
1323 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001324 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1325 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001326 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001327 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001328 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001329 found_key.offset -
1330 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001331 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001332 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001333 disk_bytenr += cur_offset - found_key.offset;
1334 num_bytes = min(end + 1, extent_end) - cur_offset;
1335 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001336 * if there are pending snapshots for this root,
1337 * we fall into common COW way.
1338 */
1339 if (!nolock) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001340 err = btrfs_start_write_no_snapshoting(root);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001341 if (!err)
1342 goto out_check;
1343 }
1344 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001345 * force cow if csum exists in the range.
1346 * this ensure that csum for a given extent are
1347 * either valid or do not exist.
1348 */
1349 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1350 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001351 nocow = 1;
1352 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1353 extent_end = found_key.offset +
Chris Mason514ac8a2014-01-03 21:07:00 -08001354 btrfs_file_extent_inline_len(leaf,
1355 path->slots[0], fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001356 extent_end = ALIGN(extent_end, root->sectorsize);
1357 } else {
1358 BUG_ON(1);
1359 }
1360out_check:
1361 if (extent_end <= start) {
1362 path->slots[0]++;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001363 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001364 btrfs_end_write_no_snapshoting(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001365 goto next_slot;
1366 }
1367 if (!nocow) {
1368 if (cow_start == (u64)-1)
1369 cow_start = cur_offset;
1370 cur_offset = extent_end;
1371 if (cur_offset > end)
1372 break;
1373 path->slots[0]++;
1374 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001375 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001376
David Sterbab3b4aa72011-04-21 01:20:15 +02001377 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001378 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001379 ret = cow_file_range(inode, locked_page,
1380 cow_start, found_key.offset - 1,
1381 page_started, nr_written, 1);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001382 if (ret) {
1383 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001384 btrfs_end_write_no_snapshoting(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001385 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001386 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001387 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001388 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001389
Yan Zhengd899e052008-10-30 14:25:28 -04001390 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1391 struct extent_map *em;
1392 struct extent_map_tree *em_tree;
1393 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001394 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001395 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001396 em->start = cur_offset;
Josef Bacik70c8a912012-10-11 16:54:30 -04001397 em->orig_start = found_key.offset - extent_offset;
Yan Zhengd899e052008-10-30 14:25:28 -04001398 em->len = num_bytes;
1399 em->block_len = num_bytes;
1400 em->block_start = disk_bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05001401 em->orig_block_len = disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001402 em->ram_bytes = ram_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04001403 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001404 em->mod_start = em->start;
1405 em->mod_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04001406 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacikb11e2342012-12-03 10:58:15 -05001407 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001408 em->generation = -1;
Yan Zhengd899e052008-10-30 14:25:28 -04001409 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001410 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001411 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001412 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001413 if (ret != -EEXIST) {
1414 free_extent_map(em);
1415 break;
1416 }
1417 btrfs_drop_extent_cache(inode, em->start,
1418 em->start + em->len - 1, 0);
1419 }
1420 type = BTRFS_ORDERED_PREALLOC;
1421 } else {
1422 type = BTRFS_ORDERED_NOCOW;
1423 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001424
1425 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001426 num_bytes, num_bytes, type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001427 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001428
Yan, Zhengefa56462010-05-16 10:49:59 -04001429 if (root->root_key.objectid ==
1430 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1431 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1432 num_bytes);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001433 if (ret) {
1434 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001435 btrfs_end_write_no_snapshoting(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001436 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001437 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001438 }
1439
Josef Bacikc2790a22013-07-29 11:20:47 -04001440 extent_clear_unlock_delalloc(inode, cur_offset,
1441 cur_offset + num_bytes - 1,
1442 locked_page, EXTENT_LOCKED |
1443 EXTENT_DELALLOC, PAGE_UNLOCK |
1444 PAGE_SET_PRIVATE2);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001445 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001446 btrfs_end_write_no_snapshoting(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001447 cur_offset = extent_end;
1448 if (cur_offset > end)
1449 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001450 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001451 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001452
Josef Bacik17ca04a2012-05-31 15:58:55 -04001453 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001454 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001455 cur_offset = end;
1456 }
1457
Yan Zheng80ff3852008-10-30 14:20:02 -04001458 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001459 ret = cow_file_range(inode, locked_page, cow_start, end,
1460 page_started, nr_written, 1);
Josef Bacikd788a342013-10-25 16:55:08 -04001461 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001462 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001463 }
1464
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001465error:
Miao Xiea698d0752012-09-20 01:51:59 -06001466 err = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001467 if (!ret)
1468 ret = err;
1469
Josef Bacik17ca04a2012-05-31 15:58:55 -04001470 if (ret && cur_offset < end)
Josef Bacikc2790a22013-07-29 11:20:47 -04001471 extent_clear_unlock_delalloc(inode, cur_offset, end,
1472 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001473 EXTENT_DELALLOC | EXTENT_DEFRAG |
1474 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1475 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001476 PAGE_SET_WRITEBACK |
1477 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001478 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001479 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001480}
1481
Wang Shilong47059d92014-07-03 18:22:07 +08001482static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1483{
1484
1485 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1486 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1487 return 0;
1488
1489 /*
1490 * @defrag_bytes is a hint value, no spinlock held here,
1491 * if is not zero, it means the file is defragging.
1492 * Force cow if given extent needs to be defragged.
1493 */
1494 if (BTRFS_I(inode)->defrag_bytes &&
1495 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1496 EXTENT_DEFRAG, 0, NULL))
1497 return 1;
1498
1499 return 0;
1500}
1501
Chris Masond352ac62008-09-29 15:18:18 -04001502/*
1503 * extent_io.c call back to do delayed allocation processing
1504 */
Chris Masonc8b97812008-10-29 14:49:59 -04001505static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001506 u64 start, u64 end, int *page_started,
1507 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001508{
Chris Masonbe20aa92007-12-17 20:14:01 -05001509 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001510 int force_cow = need_force_cow(inode, start, end);
Chris Masona2135012008-06-25 16:01:30 -04001511
Wang Shilong47059d92014-07-03 18:22:07 +08001512 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001513 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001514 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001515 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001516 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001517 page_started, 0, nr_written);
Wang Shilong78160302014-07-17 11:44:10 +08001518 } else if (!inode_need_compress(inode)) {
Chris Mason7f366cf2009-03-12 20:12:45 -04001519 ret = cow_file_range(inode, locked_page, start, end,
1520 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001521 } else {
1522 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1523 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001524 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001525 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001526 }
Chris Masonb888db22007-08-27 16:49:44 -04001527 return ret;
1528}
1529
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001530static void btrfs_split_extent_hook(struct inode *inode,
1531 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001532{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001533 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001534 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001535 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001536
Josef Bacik9e0baf62011-07-15 15:16:44 +00001537 spin_lock(&BTRFS_I(inode)->lock);
1538 BTRFS_I(inode)->outstanding_extents++;
1539 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001540}
1541
1542/*
1543 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1544 * extents so we can keep track of new extents that are just merged onto old
1545 * extents, such as when we are doing sequential writes, so we can properly
1546 * account for the metadata space we'll need.
1547 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001548static void btrfs_merge_extent_hook(struct inode *inode,
1549 struct extent_state *new,
1550 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001551{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001552 /* not delalloc, ignore it */
1553 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001554 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001555
Josef Bacik9e0baf62011-07-15 15:16:44 +00001556 spin_lock(&BTRFS_I(inode)->lock);
1557 BTRFS_I(inode)->outstanding_extents--;
1558 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001559}
1560
Miao Xieeb73c1b2013-05-15 07:48:22 +00001561static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1562 struct inode *inode)
1563{
1564 spin_lock(&root->delalloc_lock);
1565 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1566 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1567 &root->delalloc_inodes);
1568 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1569 &BTRFS_I(inode)->runtime_flags);
1570 root->nr_delalloc_inodes++;
1571 if (root->nr_delalloc_inodes == 1) {
1572 spin_lock(&root->fs_info->delalloc_root_lock);
1573 BUG_ON(!list_empty(&root->delalloc_root));
1574 list_add_tail(&root->delalloc_root,
1575 &root->fs_info->delalloc_roots);
1576 spin_unlock(&root->fs_info->delalloc_root_lock);
1577 }
1578 }
1579 spin_unlock(&root->delalloc_lock);
1580}
1581
1582static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1583 struct inode *inode)
1584{
1585 spin_lock(&root->delalloc_lock);
1586 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1587 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1588 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1589 &BTRFS_I(inode)->runtime_flags);
1590 root->nr_delalloc_inodes--;
1591 if (!root->nr_delalloc_inodes) {
1592 spin_lock(&root->fs_info->delalloc_root_lock);
1593 BUG_ON(list_empty(&root->delalloc_root));
1594 list_del_init(&root->delalloc_root);
1595 spin_unlock(&root->fs_info->delalloc_root_lock);
1596 }
1597 }
1598 spin_unlock(&root->delalloc_lock);
1599}
1600
Chris Masond352ac62008-09-29 15:18:18 -04001601/*
1602 * extent_io.c set_bit_hook, used to track delayed allocation
1603 * bytes in this file, and to maintain the list of inodes that
1604 * have pending delalloc work to be done.
1605 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001606static void btrfs_set_bit_hook(struct inode *inode,
David Sterba9ee49a042015-01-14 19:52:13 +01001607 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001608{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001609
Wang Shilong47059d92014-07-03 18:22:07 +08001610 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1611 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001612 /*
1613 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001614 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001615 * bit, which is only set or cleared with irqs on
1616 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001617 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001618 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001619 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001620 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001621
Josef Bacik9e0baf62011-07-15 15:16:44 +00001622 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001623 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001624 } else {
1625 spin_lock(&BTRFS_I(inode)->lock);
1626 BTRFS_I(inode)->outstanding_extents++;
1627 spin_unlock(&BTRFS_I(inode)->lock);
1628 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001629
Miao Xie963d6782013-01-29 10:10:51 +00001630 __percpu_counter_add(&root->fs_info->delalloc_bytes, len,
1631 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001632 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001633 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001634 if (*bits & EXTENT_DEFRAG)
1635 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001636 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001637 &BTRFS_I(inode)->runtime_flags))
1638 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001639 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001640 }
Chris Mason291d6732008-01-29 15:55:23 -05001641}
1642
Chris Masond352ac62008-09-29 15:18:18 -04001643/*
1644 * extent_io.c clear_bit_hook, see set_bit_hook for why
1645 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001646static void btrfs_clear_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001647 struct extent_state *state,
David Sterba9ee49a042015-01-14 19:52:13 +01001648 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001649{
Wang Shilong47059d92014-07-03 18:22:07 +08001650 u64 len = state->end + 1 - state->start;
1651
1652 spin_lock(&BTRFS_I(inode)->lock);
1653 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG))
1654 BTRFS_I(inode)->defrag_bytes -= len;
1655 spin_unlock(&BTRFS_I(inode)->lock);
1656
Chris Mason75eff682008-12-15 15:54:40 -05001657 /*
1658 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001659 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001660 * bit, which is only set or cleared with irqs on
1661 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001662 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001663 struct btrfs_root *root = BTRFS_I(inode)->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001664 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001665
Josef Bacik9e0baf62011-07-15 15:16:44 +00001666 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001667 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001668 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1669 spin_lock(&BTRFS_I(inode)->lock);
1670 BTRFS_I(inode)->outstanding_extents--;
1671 spin_unlock(&BTRFS_I(inode)->lock);
1672 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001673
Josef Bacikb6d08f02013-09-27 14:57:43 -04001674 /*
1675 * We don't reserve metadata space for space cache inodes so we
1676 * don't need to call dellalloc_release_metadata if there is an
1677 * error.
1678 */
1679 if (*bits & EXTENT_DO_ACCOUNTING &&
1680 root != root->fs_info->tree_root)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001681 btrfs_delalloc_release_metadata(inode, len);
1682
Josef Bacik0cb59c92010-07-02 12:14:14 -04001683 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Josef Bacik7ee9e442013-06-21 16:37:03 -04001684 && do_list && !(state->state & EXTENT_NORESERVE))
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001685 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001686
Miao Xie963d6782013-01-29 10:10:51 +00001687 __percpu_counter_add(&root->fs_info->delalloc_bytes, -len,
1688 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001689 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001690 BTRFS_I(inode)->delalloc_bytes -= len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001691 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001692 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001693 &BTRFS_I(inode)->runtime_flags))
1694 btrfs_del_delalloc_inode(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001695 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001696 }
Chris Mason291d6732008-01-29 15:55:23 -05001697}
1698
Chris Masond352ac62008-09-29 15:18:18 -04001699/*
1700 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1701 * we don't create bios that span stripes or chunks
1702 */
David Woodhouse64a16702009-07-15 23:29:37 +01001703int btrfs_merge_bio_hook(int rw, struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001704 size_t size, struct bio *bio,
1705 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001706{
1707 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001708 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001709 u64 length = 0;
1710 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001711 int ret;
1712
Chris Mason771ed682008-11-06 22:02:51 -05001713 if (bio_flags & EXTENT_BIO_COMPRESSED)
1714 return 0;
1715
Kent Overstreet4f024f32013-10-11 15:44:27 -07001716 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001717 map_length = length;
David Woodhouse64a16702009-07-15 23:29:37 +01001718 ret = btrfs_map_block(root->fs_info, rw, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001719 &map_length, NULL, 0);
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001720 /* Will always return 0 with map_multi == NULL */
Jeff Mahoney3444a972011-10-03 23:23:13 -04001721 BUG_ON(ret < 0);
Chris Masond3977122009-01-05 21:25:51 -05001722 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001723 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001724 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001725}
1726
Chris Masond352ac62008-09-29 15:18:18 -04001727/*
1728 * in order to insert checksums into the metadata in large chunks,
1729 * we wait until bio submission time. All the pages in the bio are
1730 * checksummed and sums are attached onto the ordered extent record.
1731 *
1732 * At IO completion time the cums attached on the ordered extent record
1733 * are inserted into the btree
1734 */
Chris Masond3977122009-01-05 21:25:51 -05001735static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1736 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001737 unsigned long bio_flags,
1738 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001739{
Chris Mason065631f2008-02-20 12:07:25 -05001740 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001741 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001742
Chris Masond20f7042008-12-08 16:58:54 -05001743 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001744 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001745 return 0;
1746}
Chris Masone0156402008-04-16 11:15:20 -04001747
Chris Mason4a69a412008-11-06 22:03:00 -05001748/*
1749 * in order to insert checksums into the metadata in large chunks,
1750 * we wait until bio submission time. All the pages in the bio are
1751 * checksummed and sums are attached onto the ordered extent record.
1752 *
1753 * At IO completion time the cums attached on the ordered extent record
1754 * are inserted into the btree
1755 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001756static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001757 int mirror_num, unsigned long bio_flags,
1758 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001759{
1760 struct btrfs_root *root = BTRFS_I(inode)->root;
Stefan Behrens61891922012-11-05 18:51:52 +01001761 int ret;
1762
1763 ret = btrfs_map_bio(root, rw, bio, mirror_num, 1);
1764 if (ret)
1765 bio_endio(bio, ret);
1766 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001767}
1768
Chris Masond352ac62008-09-29 15:18:18 -04001769/*
Chris Masoncad321a2008-12-17 14:51:42 -05001770 * extent_io.c submission hook. This does the right thing for csum calculation
1771 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001772 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001773static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001774 int mirror_num, unsigned long bio_flags,
1775 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001776{
1777 struct btrfs_root *root = BTRFS_I(inode)->root;
1778 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001779 int skip_sum;
Jeff Mahoney04173412011-10-03 23:23:12 -04001780 int metadata = 0;
Josef Bacikb812ce22012-11-16 13:56:32 -05001781 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001782
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001783 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001784
Liu Bo83eea1f2012-07-10 05:28:39 -06001785 if (btrfs_is_free_space_inode(inode))
Jeff Mahoney04173412011-10-03 23:23:12 -04001786 metadata = 2;
1787
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001788 if (!(rw & REQ_WRITE)) {
Josef Bacik5fd02042012-05-02 14:00:54 -04001789 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1790 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001791 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001792
Chris Masond20f7042008-12-08 16:58:54 -05001793 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001794 ret = btrfs_submit_compressed_read(inode, bio,
1795 mirror_num,
1796 bio_flags);
1797 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001798 } else if (!skip_sum) {
1799 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1800 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001801 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001802 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001803 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001804 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001805 /* csum items have already been cloned */
1806 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1807 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001808 /* we're doing a write, do the async checksumming */
Stefan Behrens61891922012-11-05 18:51:52 +01001809 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001810 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001811 bio_flags, bio_offset,
1812 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001813 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001814 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001815 } else if (!skip_sum) {
1816 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1817 if (ret)
1818 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001819 }
1820
Chris Mason0b86a832008-03-24 15:01:56 -04001821mapit:
Stefan Behrens61891922012-11-05 18:51:52 +01001822 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
1823
1824out:
1825 if (ret < 0)
1826 bio_endio(bio, ret);
1827 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001828}
Chris Mason6885f302008-02-20 16:11:05 -05001829
Chris Masond352ac62008-09-29 15:18:18 -04001830/*
1831 * given a list of ordered sums record them in the inode. This happens
1832 * at IO completion time based on sums calculated at bio submission time.
1833 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001834static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001835 struct inode *inode, u64 file_offset,
1836 struct list_head *list)
1837{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001838 struct btrfs_ordered_sum *sum;
1839
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001840 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00001841 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05001842 btrfs_csum_file_blocks(trans,
1843 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00001844 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001845 }
1846 return 0;
1847}
1848
Josef Bacik2ac55d42010-02-03 19:33:23 +00001849int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1850 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001851{
Julia Lawall6c1500f2012-11-03 20:30:18 +00001852 WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04001853 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001854 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001855}
1856
Chris Masond352ac62008-09-29 15:18:18 -04001857/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001858struct btrfs_writepage_fixup {
1859 struct page *page;
1860 struct btrfs_work work;
1861};
1862
Christoph Hellwigb2950862008-12-02 09:54:17 -05001863static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001864{
1865 struct btrfs_writepage_fixup *fixup;
1866 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001867 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001868 struct page *page;
1869 struct inode *inode;
1870 u64 page_start;
1871 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001872 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001873
1874 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1875 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001876again:
Chris Mason247e7432008-07-17 12:53:51 -04001877 lock_page(page);
1878 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1879 ClearPageChecked(page);
1880 goto out_page;
1881 }
1882
1883 inode = page->mapping->host;
1884 page_start = page_offset(page);
1885 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1886
Josef Bacik2ac55d42010-02-03 19:33:23 +00001887 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001888 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001889
1890 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001891 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001892 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001893
1894 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1895 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001896 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1897 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001898 unlock_page(page);
1899 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001900 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04001901 goto again;
1902 }
Chris Mason247e7432008-07-17 12:53:51 -04001903
Jeff Mahoney87826df2012-02-15 16:23:57 +01001904 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
1905 if (ret) {
1906 mapping_set_error(page->mapping, ret);
1907 end_extent_writepage(page, ret, page_start, page_end);
1908 ClearPageChecked(page);
1909 goto out;
1910 }
1911
Josef Bacik2ac55d42010-02-03 19:33:23 +00001912 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001913 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001914 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04001915out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001916 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1917 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001918out_page:
1919 unlock_page(page);
1920 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08001921 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04001922}
1923
1924/*
1925 * There are a few paths in the higher layers of the kernel that directly
1926 * set the page dirty bit without asking the filesystem if it is a
1927 * good idea. This causes problems because we want to make sure COW
1928 * properly happens and the data=ordered rules are followed.
1929 *
Chris Masonc8b97812008-10-29 14:49:59 -04001930 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001931 * hasn't been properly setup for IO. We kick off an async process
1932 * to fix it up. The async helper will wait for ordered extents, set
1933 * the delalloc bit and make it safe to write the page.
1934 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001935static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001936{
1937 struct inode *inode = page->mapping->host;
1938 struct btrfs_writepage_fixup *fixup;
1939 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001940
Chris Mason8b62b722009-09-02 16:53:46 -04001941 /* this page is properly in the ordered list */
1942 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001943 return 0;
1944
1945 if (PageChecked(page))
1946 return -EAGAIN;
1947
1948 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1949 if (!fixup)
1950 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001951
Chris Mason247e7432008-07-17 12:53:51 -04001952 SetPageChecked(page);
1953 page_cache_get(page);
Liu Bo9e0af232014-08-15 23:36:53 +08001954 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
1955 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04001956 fixup->page = page;
Qu Wenruodc6e3202014-02-28 10:46:14 +08001957 btrfs_queue_work(root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001958 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04001959}
1960
Yan Zhengd899e052008-10-30 14:25:28 -04001961static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1962 struct inode *inode, u64 file_pos,
1963 u64 disk_bytenr, u64 disk_num_bytes,
1964 u64 num_bytes, u64 ram_bytes,
1965 u8 compression, u8 encryption,
1966 u16 other_encoding, int extent_type)
1967{
1968 struct btrfs_root *root = BTRFS_I(inode)->root;
1969 struct btrfs_file_extent_item *fi;
1970 struct btrfs_path *path;
1971 struct extent_buffer *leaf;
1972 struct btrfs_key ins;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00001973 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04001974 int ret;
1975
1976 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07001977 if (!path)
1978 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04001979
Chris Masona1ed8352009-09-11 12:27:37 -04001980 /*
1981 * we may be replacing one extent in the tree with another.
1982 * The new extent is pinned in the extent map, and we don't want
1983 * to drop it from the cache until it is completely in the btree.
1984 *
1985 * So, tell btrfs_drop_extents to leave this extent in the cache.
1986 * the caller is expected to unpin it and allow it to be merged
1987 * with the others.
1988 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00001989 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
1990 file_pos + num_bytes, NULL, 0,
1991 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001992 if (ret)
1993 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001994
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00001995 if (!extent_inserted) {
1996 ins.objectid = btrfs_ino(inode);
1997 ins.offset = file_pos;
1998 ins.type = BTRFS_EXTENT_DATA_KEY;
1999
2000 path->leave_spinning = 1;
2001 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2002 sizeof(*fi));
2003 if (ret)
2004 goto out;
2005 }
Yan Zhengd899e052008-10-30 14:25:28 -04002006 leaf = path->nodes[0];
2007 fi = btrfs_item_ptr(leaf, path->slots[0],
2008 struct btrfs_file_extent_item);
2009 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2010 btrfs_set_file_extent_type(leaf, fi, extent_type);
2011 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2012 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2013 btrfs_set_file_extent_offset(leaf, fi, 0);
2014 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2015 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2016 btrfs_set_file_extent_compression(leaf, fi, compression);
2017 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2018 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002019
Yan Zhengd899e052008-10-30 14:25:28 -04002020 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002021 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002022
2023 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002024
2025 ins.objectid = disk_bytenr;
2026 ins.offset = disk_num_bytes;
2027 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002028 ret = btrfs_alloc_reserved_file_extent(trans, root,
2029 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08002030 btrfs_ino(inode), file_pos, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002031out:
Yan Zhengd899e052008-10-30 14:25:28 -04002032 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002033
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002034 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002035}
2036
Liu Bo38c227d2013-01-29 03:18:40 +00002037/* snapshot-aware defrag */
2038struct sa_defrag_extent_backref {
2039 struct rb_node node;
2040 struct old_sa_defrag_extent *old;
2041 u64 root_id;
2042 u64 inum;
2043 u64 file_pos;
2044 u64 extent_offset;
2045 u64 num_bytes;
2046 u64 generation;
2047};
2048
2049struct old_sa_defrag_extent {
2050 struct list_head list;
2051 struct new_sa_defrag_extent *new;
2052
2053 u64 extent_offset;
2054 u64 bytenr;
2055 u64 offset;
2056 u64 len;
2057 int count;
2058};
2059
2060struct new_sa_defrag_extent {
2061 struct rb_root root;
2062 struct list_head head;
2063 struct btrfs_path *path;
2064 struct inode *inode;
2065 u64 file_pos;
2066 u64 len;
2067 u64 bytenr;
2068 u64 disk_len;
2069 u8 compress_type;
2070};
2071
2072static int backref_comp(struct sa_defrag_extent_backref *b1,
2073 struct sa_defrag_extent_backref *b2)
2074{
2075 if (b1->root_id < b2->root_id)
2076 return -1;
2077 else if (b1->root_id > b2->root_id)
2078 return 1;
2079
2080 if (b1->inum < b2->inum)
2081 return -1;
2082 else if (b1->inum > b2->inum)
2083 return 1;
2084
2085 if (b1->file_pos < b2->file_pos)
2086 return -1;
2087 else if (b1->file_pos > b2->file_pos)
2088 return 1;
2089
2090 /*
2091 * [------------------------------] ===> (a range of space)
2092 * |<--->| |<---->| =============> (fs/file tree A)
2093 * |<---------------------------->| ===> (fs/file tree B)
2094 *
2095 * A range of space can refer to two file extents in one tree while
2096 * refer to only one file extent in another tree.
2097 *
2098 * So we may process a disk offset more than one time(two extents in A)
2099 * and locate at the same extent(one extent in B), then insert two same
2100 * backrefs(both refer to the extent in B).
2101 */
2102 return 0;
2103}
2104
2105static void backref_insert(struct rb_root *root,
2106 struct sa_defrag_extent_backref *backref)
2107{
2108 struct rb_node **p = &root->rb_node;
2109 struct rb_node *parent = NULL;
2110 struct sa_defrag_extent_backref *entry;
2111 int ret;
2112
2113 while (*p) {
2114 parent = *p;
2115 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2116
2117 ret = backref_comp(backref, entry);
2118 if (ret < 0)
2119 p = &(*p)->rb_left;
2120 else
2121 p = &(*p)->rb_right;
2122 }
2123
2124 rb_link_node(&backref->node, parent, p);
2125 rb_insert_color(&backref->node, root);
2126}
2127
2128/*
2129 * Note the backref might has changed, and in this case we just return 0.
2130 */
2131static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2132 void *ctx)
2133{
2134 struct btrfs_file_extent_item *extent;
2135 struct btrfs_fs_info *fs_info;
2136 struct old_sa_defrag_extent *old = ctx;
2137 struct new_sa_defrag_extent *new = old->new;
2138 struct btrfs_path *path = new->path;
2139 struct btrfs_key key;
2140 struct btrfs_root *root;
2141 struct sa_defrag_extent_backref *backref;
2142 struct extent_buffer *leaf;
2143 struct inode *inode = new->inode;
2144 int slot;
2145 int ret;
2146 u64 extent_offset;
2147 u64 num_bytes;
2148
2149 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
2150 inum == btrfs_ino(inode))
2151 return 0;
2152
2153 key.objectid = root_id;
2154 key.type = BTRFS_ROOT_ITEM_KEY;
2155 key.offset = (u64)-1;
2156
2157 fs_info = BTRFS_I(inode)->root->fs_info;
2158 root = btrfs_read_fs_root_no_name(fs_info, &key);
2159 if (IS_ERR(root)) {
2160 if (PTR_ERR(root) == -ENOENT)
2161 return 0;
2162 WARN_ON(1);
2163 pr_debug("inum=%llu, offset=%llu, root_id=%llu\n",
2164 inum, offset, root_id);
2165 return PTR_ERR(root);
2166 }
2167
2168 key.objectid = inum;
2169 key.type = BTRFS_EXTENT_DATA_KEY;
2170 if (offset > (u64)-1 << 32)
2171 key.offset = 0;
2172 else
2173 key.offset = offset;
2174
2175 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302176 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002177 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002178 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002179
2180 while (1) {
2181 cond_resched();
2182
2183 leaf = path->nodes[0];
2184 slot = path->slots[0];
2185
2186 if (slot >= btrfs_header_nritems(leaf)) {
2187 ret = btrfs_next_leaf(root, path);
2188 if (ret < 0) {
2189 goto out;
2190 } else if (ret > 0) {
2191 ret = 0;
2192 goto out;
2193 }
2194 continue;
2195 }
2196
2197 path->slots[0]++;
2198
2199 btrfs_item_key_to_cpu(leaf, &key, slot);
2200
2201 if (key.objectid > inum)
2202 goto out;
2203
2204 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2205 continue;
2206
2207 extent = btrfs_item_ptr(leaf, slot,
2208 struct btrfs_file_extent_item);
2209
2210 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2211 continue;
2212
Liu Boe68afa42013-07-01 22:13:26 +08002213 /*
2214 * 'offset' refers to the exact key.offset,
2215 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2216 * (key.offset - extent_offset).
2217 */
2218 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002219 continue;
2220
Liu Boe68afa42013-07-01 22:13:26 +08002221 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002222 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002223
Liu Bo38c227d2013-01-29 03:18:40 +00002224 if (extent_offset >= old->extent_offset + old->offset +
2225 old->len || extent_offset + num_bytes <=
2226 old->extent_offset + old->offset)
2227 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002228 break;
2229 }
2230
2231 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2232 if (!backref) {
2233 ret = -ENOENT;
2234 goto out;
2235 }
2236
2237 backref->root_id = root_id;
2238 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002239 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002240 backref->num_bytes = num_bytes;
2241 backref->extent_offset = extent_offset;
2242 backref->generation = btrfs_file_extent_generation(leaf, extent);
2243 backref->old = old;
2244 backref_insert(&new->root, backref);
2245 old->count++;
2246out:
2247 btrfs_release_path(path);
2248 WARN_ON(ret);
2249 return ret;
2250}
2251
2252static noinline bool record_extent_backrefs(struct btrfs_path *path,
2253 struct new_sa_defrag_extent *new)
2254{
2255 struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info;
2256 struct old_sa_defrag_extent *old, *tmp;
2257 int ret;
2258
2259 new->path = path;
2260
2261 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002262 ret = iterate_inodes_from_logical(old->bytenr +
2263 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002264 path, record_one_backref,
2265 old);
Josef Bacik4724b102013-11-05 11:11:40 -05002266 if (ret < 0 && ret != -ENOENT)
2267 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002268
2269 /* no backref to be processed for this extent */
2270 if (!old->count) {
2271 list_del(&old->list);
2272 kfree(old);
2273 }
2274 }
2275
2276 if (list_empty(&new->head))
2277 return false;
2278
2279 return true;
2280}
2281
2282static int relink_is_mergable(struct extent_buffer *leaf,
2283 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002284 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002285{
Liu Bo116e0022013-08-02 16:30:40 +08002286 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002287 return 0;
2288
2289 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2290 return 0;
2291
Liu Bo116e0022013-08-02 16:30:40 +08002292 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2293 return 0;
2294
2295 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002296 btrfs_file_extent_other_encoding(leaf, fi))
2297 return 0;
2298
2299 return 1;
2300}
2301
2302/*
2303 * Note the backref might has changed, and in this case we just return 0.
2304 */
2305static noinline int relink_extent_backref(struct btrfs_path *path,
2306 struct sa_defrag_extent_backref *prev,
2307 struct sa_defrag_extent_backref *backref)
2308{
2309 struct btrfs_file_extent_item *extent;
2310 struct btrfs_file_extent_item *item;
2311 struct btrfs_ordered_extent *ordered;
2312 struct btrfs_trans_handle *trans;
2313 struct btrfs_fs_info *fs_info;
2314 struct btrfs_root *root;
2315 struct btrfs_key key;
2316 struct extent_buffer *leaf;
2317 struct old_sa_defrag_extent *old = backref->old;
2318 struct new_sa_defrag_extent *new = old->new;
2319 struct inode *src_inode = new->inode;
2320 struct inode *inode;
2321 struct extent_state *cached = NULL;
2322 int ret = 0;
2323 u64 start;
2324 u64 len;
2325 u64 lock_start;
2326 u64 lock_end;
2327 bool merge = false;
2328 int index;
2329
2330 if (prev && prev->root_id == backref->root_id &&
2331 prev->inum == backref->inum &&
2332 prev->file_pos + prev->num_bytes == backref->file_pos)
2333 merge = true;
2334
2335 /* step 1: get root */
2336 key.objectid = backref->root_id;
2337 key.type = BTRFS_ROOT_ITEM_KEY;
2338 key.offset = (u64)-1;
2339
2340 fs_info = BTRFS_I(src_inode)->root->fs_info;
2341 index = srcu_read_lock(&fs_info->subvol_srcu);
2342
2343 root = btrfs_read_fs_root_no_name(fs_info, &key);
2344 if (IS_ERR(root)) {
2345 srcu_read_unlock(&fs_info->subvol_srcu, index);
2346 if (PTR_ERR(root) == -ENOENT)
2347 return 0;
2348 return PTR_ERR(root);
2349 }
Liu Bo38c227d2013-01-29 03:18:40 +00002350
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002351 if (btrfs_root_readonly(root)) {
2352 srcu_read_unlock(&fs_info->subvol_srcu, index);
2353 return 0;
2354 }
2355
Liu Bo38c227d2013-01-29 03:18:40 +00002356 /* step 2: get inode */
2357 key.objectid = backref->inum;
2358 key.type = BTRFS_INODE_ITEM_KEY;
2359 key.offset = 0;
2360
2361 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2362 if (IS_ERR(inode)) {
2363 srcu_read_unlock(&fs_info->subvol_srcu, index);
2364 return 0;
2365 }
2366
2367 srcu_read_unlock(&fs_info->subvol_srcu, index);
2368
2369 /* step 3: relink backref */
2370 lock_start = backref->file_pos;
2371 lock_end = backref->file_pos + backref->num_bytes - 1;
2372 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2373 0, &cached);
2374
2375 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2376 if (ordered) {
2377 btrfs_put_ordered_extent(ordered);
2378 goto out_unlock;
2379 }
2380
2381 trans = btrfs_join_transaction(root);
2382 if (IS_ERR(trans)) {
2383 ret = PTR_ERR(trans);
2384 goto out_unlock;
2385 }
2386
2387 key.objectid = backref->inum;
2388 key.type = BTRFS_EXTENT_DATA_KEY;
2389 key.offset = backref->file_pos;
2390
2391 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2392 if (ret < 0) {
2393 goto out_free_path;
2394 } else if (ret > 0) {
2395 ret = 0;
2396 goto out_free_path;
2397 }
2398
2399 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2400 struct btrfs_file_extent_item);
2401
2402 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2403 backref->generation)
2404 goto out_free_path;
2405
2406 btrfs_release_path(path);
2407
2408 start = backref->file_pos;
2409 if (backref->extent_offset < old->extent_offset + old->offset)
2410 start += old->extent_offset + old->offset -
2411 backref->extent_offset;
2412
2413 len = min(backref->extent_offset + backref->num_bytes,
2414 old->extent_offset + old->offset + old->len);
2415 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2416
2417 ret = btrfs_drop_extents(trans, root, inode, start,
2418 start + len, 1);
2419 if (ret)
2420 goto out_free_path;
2421again:
2422 key.objectid = btrfs_ino(inode);
2423 key.type = BTRFS_EXTENT_DATA_KEY;
2424 key.offset = start;
2425
Liu Boa09a0a72013-03-11 09:20:58 +00002426 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002427 if (merge) {
2428 struct btrfs_file_extent_item *fi;
2429 u64 extent_len;
2430 struct btrfs_key found_key;
2431
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002432 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002433 if (ret < 0)
2434 goto out_free_path;
2435
2436 path->slots[0]--;
2437 leaf = path->nodes[0];
2438 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2439
2440 fi = btrfs_item_ptr(leaf, path->slots[0],
2441 struct btrfs_file_extent_item);
2442 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2443
Liu Bo116e0022013-08-02 16:30:40 +08002444 if (extent_len + found_key.offset == start &&
2445 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002446 btrfs_set_file_extent_num_bytes(leaf, fi,
2447 extent_len + len);
2448 btrfs_mark_buffer_dirty(leaf);
2449 inode_add_bytes(inode, len);
2450
2451 ret = 1;
2452 goto out_free_path;
2453 } else {
2454 merge = false;
2455 btrfs_release_path(path);
2456 goto again;
2457 }
2458 }
2459
2460 ret = btrfs_insert_empty_item(trans, root, path, &key,
2461 sizeof(*extent));
2462 if (ret) {
2463 btrfs_abort_transaction(trans, root, ret);
2464 goto out_free_path;
2465 }
2466
2467 leaf = path->nodes[0];
2468 item = btrfs_item_ptr(leaf, path->slots[0],
2469 struct btrfs_file_extent_item);
2470 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2471 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2472 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2473 btrfs_set_file_extent_num_bytes(leaf, item, len);
2474 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2475 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2476 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2477 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2478 btrfs_set_file_extent_encryption(leaf, item, 0);
2479 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2480
2481 btrfs_mark_buffer_dirty(leaf);
2482 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002483 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002484
2485 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
2486 new->disk_len, 0,
2487 backref->root_id, backref->inum,
2488 new->file_pos, 0); /* start - extent_offset */
2489 if (ret) {
2490 btrfs_abort_transaction(trans, root, ret);
2491 goto out_free_path;
2492 }
2493
2494 ret = 1;
2495out_free_path:
2496 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002497 path->leave_spinning = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002498 btrfs_end_transaction(trans, root);
2499out_unlock:
2500 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2501 &cached, GFP_NOFS);
2502 iput(inode);
2503 return ret;
2504}
2505
Liu Bo6f519562013-10-29 10:45:05 +08002506static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2507{
2508 struct old_sa_defrag_extent *old, *tmp;
2509
2510 if (!new)
2511 return;
2512
2513 list_for_each_entry_safe(old, tmp, &new->head, list) {
2514 list_del(&old->list);
2515 kfree(old);
2516 }
2517 kfree(new);
2518}
2519
Liu Bo38c227d2013-01-29 03:18:40 +00002520static void relink_file_extents(struct new_sa_defrag_extent *new)
2521{
2522 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002523 struct sa_defrag_extent_backref *backref;
2524 struct sa_defrag_extent_backref *prev = NULL;
2525 struct inode *inode;
2526 struct btrfs_root *root;
2527 struct rb_node *node;
2528 int ret;
2529
2530 inode = new->inode;
2531 root = BTRFS_I(inode)->root;
2532
2533 path = btrfs_alloc_path();
2534 if (!path)
2535 return;
2536
2537 if (!record_extent_backrefs(path, new)) {
2538 btrfs_free_path(path);
2539 goto out;
2540 }
2541 btrfs_release_path(path);
2542
2543 while (1) {
2544 node = rb_first(&new->root);
2545 if (!node)
2546 break;
2547 rb_erase(node, &new->root);
2548
2549 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2550
2551 ret = relink_extent_backref(path, prev, backref);
2552 WARN_ON(ret < 0);
2553
2554 kfree(prev);
2555
2556 if (ret == 1)
2557 prev = backref;
2558 else
2559 prev = NULL;
2560 cond_resched();
2561 }
2562 kfree(prev);
2563
2564 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002565out:
Liu Bo6f519562013-10-29 10:45:05 +08002566 free_sa_defrag_extent(new);
2567
Liu Bo38c227d2013-01-29 03:18:40 +00002568 atomic_dec(&root->fs_info->defrag_running);
2569 wake_up(&root->fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002570}
2571
2572static struct new_sa_defrag_extent *
2573record_old_file_extents(struct inode *inode,
2574 struct btrfs_ordered_extent *ordered)
2575{
2576 struct btrfs_root *root = BTRFS_I(inode)->root;
2577 struct btrfs_path *path;
2578 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002579 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002580 struct new_sa_defrag_extent *new;
2581 int ret;
2582
2583 new = kmalloc(sizeof(*new), GFP_NOFS);
2584 if (!new)
2585 return NULL;
2586
2587 new->inode = inode;
2588 new->file_pos = ordered->file_offset;
2589 new->len = ordered->len;
2590 new->bytenr = ordered->start;
2591 new->disk_len = ordered->disk_len;
2592 new->compress_type = ordered->compress_type;
2593 new->root = RB_ROOT;
2594 INIT_LIST_HEAD(&new->head);
2595
2596 path = btrfs_alloc_path();
2597 if (!path)
2598 goto out_kfree;
2599
2600 key.objectid = btrfs_ino(inode);
2601 key.type = BTRFS_EXTENT_DATA_KEY;
2602 key.offset = new->file_pos;
2603
2604 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2605 if (ret < 0)
2606 goto out_free_path;
2607 if (ret > 0 && path->slots[0] > 0)
2608 path->slots[0]--;
2609
2610 /* find out all the old extents for the file range */
2611 while (1) {
2612 struct btrfs_file_extent_item *extent;
2613 struct extent_buffer *l;
2614 int slot;
2615 u64 num_bytes;
2616 u64 offset;
2617 u64 end;
2618 u64 disk_bytenr;
2619 u64 extent_offset;
2620
2621 l = path->nodes[0];
2622 slot = path->slots[0];
2623
2624 if (slot >= btrfs_header_nritems(l)) {
2625 ret = btrfs_next_leaf(root, path);
2626 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002627 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002628 else if (ret > 0)
2629 break;
2630 continue;
2631 }
2632
2633 btrfs_item_key_to_cpu(l, &key, slot);
2634
2635 if (key.objectid != btrfs_ino(inode))
2636 break;
2637 if (key.type != BTRFS_EXTENT_DATA_KEY)
2638 break;
2639 if (key.offset >= new->file_pos + new->len)
2640 break;
2641
2642 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2643
2644 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2645 if (key.offset + num_bytes < new->file_pos)
2646 goto next;
2647
2648 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2649 if (!disk_bytenr)
2650 goto next;
2651
2652 extent_offset = btrfs_file_extent_offset(l, extent);
2653
2654 old = kmalloc(sizeof(*old), GFP_NOFS);
2655 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002656 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002657
2658 offset = max(new->file_pos, key.offset);
2659 end = min(new->file_pos + new->len, key.offset + num_bytes);
2660
2661 old->bytenr = disk_bytenr;
2662 old->extent_offset = extent_offset;
2663 old->offset = offset - key.offset;
2664 old->len = end - offset;
2665 old->new = new;
2666 old->count = 0;
2667 list_add_tail(&old->list, &new->head);
2668next:
2669 path->slots[0]++;
2670 cond_resched();
2671 }
2672
2673 btrfs_free_path(path);
2674 atomic_inc(&root->fs_info->defrag_running);
2675
2676 return new;
2677
Liu Bo38c227d2013-01-29 03:18:40 +00002678out_free_path:
2679 btrfs_free_path(path);
2680out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002681 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002682 return NULL;
2683}
2684
Miao Xiee570fd22014-06-19 10:42:50 +08002685static void btrfs_release_delalloc_bytes(struct btrfs_root *root,
2686 u64 start, u64 len)
2687{
2688 struct btrfs_block_group_cache *cache;
2689
2690 cache = btrfs_lookup_block_group(root->fs_info, start);
2691 ASSERT(cache);
2692
2693 spin_lock(&cache->lock);
2694 cache->delalloc_bytes -= len;
2695 spin_unlock(&cache->lock);
2696
2697 btrfs_put_block_group(cache);
2698}
2699
Chris Masond352ac62008-09-29 15:18:18 -04002700/* as ordered data IO finishes, this gets called so we can finish
2701 * an ordered extent if the range of bytes in the file it covers are
2702 * fully written.
2703 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002704static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002705{
Josef Bacik5fd02042012-05-02 14:00:54 -04002706 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002707 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002708 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002709 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002710 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002711 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002712 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002713 int ret = 0;
2714 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002715 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002716 bool truncated = false;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002717
Liu Bo83eea1f2012-07-10 05:28:39 -06002718 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002719
Josef Bacik5fd02042012-05-02 14:00:54 -04002720 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2721 ret = -EIO;
2722 goto out;
2723 }
2724
Miao Xief6124962014-09-12 18:44:04 +08002725 btrfs_free_io_failure_record(inode, ordered_extent->file_offset,
2726 ordered_extent->file_offset +
2727 ordered_extent->len - 1);
2728
Josef Bacik77cef2e2013-08-29 13:57:21 -04002729 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2730 truncated = true;
2731 logical_len = ordered_extent->truncated_len;
2732 /* Truncated the entire extent, don't bother adding */
2733 if (!logical_len)
2734 goto out;
2735 }
2736
Yan, Zhengc2167752009-11-12 09:34:21 +00002737 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002738 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Josef Bacik6c760c02012-11-09 10:53:21 -05002739 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2740 if (nolock)
2741 trans = btrfs_join_transaction_nolock(root);
2742 else
2743 trans = btrfs_join_transaction(root);
2744 if (IS_ERR(trans)) {
2745 ret = PTR_ERR(trans);
2746 trans = NULL;
2747 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002748 }
Josef Bacik6c760c02012-11-09 10:53:21 -05002749 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
2750 ret = btrfs_update_inode_fallback(trans, root, inode);
2751 if (ret) /* -ENOMEM or corruption */
2752 btrfs_abort_transaction(trans, root, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002753 goto out;
2754 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002755
Josef Bacik2ac55d42010-02-03 19:33:23 +00002756 lock_extent_bits(io_tree, ordered_extent->file_offset,
2757 ordered_extent->file_offset + ordered_extent->len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002758 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002759
Liu Bo38c227d2013-01-29 03:18:40 +00002760 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2761 ordered_extent->file_offset + ordered_extent->len - 1,
2762 EXTENT_DEFRAG, 1, cached_state);
2763 if (ret) {
2764 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05002765 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00002766 /* the inode is shared */
2767 new = record_old_file_extents(inode, ordered_extent);
2768
2769 clear_extent_bit(io_tree, ordered_extent->file_offset,
2770 ordered_extent->file_offset + ordered_extent->len - 1,
2771 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2772 }
2773
Josef Bacik0cb59c92010-07-02 12:14:14 -04002774 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002775 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002776 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002777 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002778 if (IS_ERR(trans)) {
2779 ret = PTR_ERR(trans);
2780 trans = NULL;
2781 goto out_unlock;
2782 }
Chris Masona79b7d42014-05-22 16:18:52 -07002783
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002784 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002785
Chris Masonc8b97812008-10-29 14:49:59 -04002786 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002787 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002788 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002789 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00002790 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002791 ordered_extent->file_offset,
2792 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002793 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002794 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04002795 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002796 ret = insert_reserved_file_extent(trans, inode,
2797 ordered_extent->file_offset,
2798 ordered_extent->start,
2799 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04002800 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08002801 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002802 BTRFS_FILE_EXTENT_REG);
Miao Xiee570fd22014-06-19 10:42:50 +08002803 if (!ret)
2804 btrfs_release_delalloc_bytes(root,
2805 ordered_extent->start,
2806 ordered_extent->disk_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002807 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002808 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2809 ordered_extent->file_offset, ordered_extent->len,
2810 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002811 if (ret < 0) {
2812 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002813 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002814 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002815
Chris Masone6dcd2d2008-07-17 12:53:50 -04002816 add_pending_csums(trans, inode, ordered_extent->file_offset,
2817 &ordered_extent->list);
2818
Josef Bacik6c760c02012-11-09 10:53:21 -05002819 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2820 ret = btrfs_update_inode_fallback(trans, root, inode);
2821 if (ret) { /* -ENOMEM or corruption */
2822 btrfs_abort_transaction(trans, root, ret);
2823 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002824 }
2825 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002826out_unlock:
2827 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2828 ordered_extent->file_offset +
2829 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00002830out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002831 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04002832 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002833 if (trans)
2834 btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002835
Josef Bacik77cef2e2013-08-29 13:57:21 -04002836 if (ret || truncated) {
2837 u64 start, end;
2838
2839 if (truncated)
2840 start = ordered_extent->file_offset + logical_len;
2841 else
2842 start = ordered_extent->file_offset;
2843 end = ordered_extent->file_offset + ordered_extent->len - 1;
2844 clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS);
2845
2846 /* Drop the cache for the part of the extent we didn't write. */
2847 btrfs_drop_extent_cache(inode, start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04002848
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002849 /*
2850 * If the ordered extent had an IOERR or something else went
2851 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04002852 * back to the allocator. We only free the extent in the
2853 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002854 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04002855 if ((ret || !logical_len) &&
2856 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002857 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
2858 btrfs_free_reserved_extent(root, ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08002859 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002860 }
2861
2862
Josef Bacik5fd02042012-05-02 14:00:54 -04002863 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002864 * This needs to be done to make sure anybody waiting knows we are done
2865 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002866 */
2867 btrfs_remove_ordered_extent(inode, ordered_extent);
2868
Liu Bo38c227d2013-01-29 03:18:40 +00002869 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08002870 if (new) {
2871 if (ret) {
2872 free_sa_defrag_extent(new);
2873 atomic_dec(&root->fs_info->defrag_running);
2874 } else {
2875 relink_file_extents(new);
2876 }
2877 }
Liu Bo38c227d2013-01-29 03:18:40 +00002878
Chris Masone6dcd2d2008-07-17 12:53:50 -04002879 /* once for us */
2880 btrfs_put_ordered_extent(ordered_extent);
2881 /* once for the tree */
2882 btrfs_put_ordered_extent(ordered_extent);
2883
Josef Bacik5fd02042012-05-02 14:00:54 -04002884 return ret;
2885}
2886
2887static void finish_ordered_fn(struct btrfs_work *work)
2888{
2889 struct btrfs_ordered_extent *ordered_extent;
2890 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2891 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002892}
2893
Christoph Hellwigb2950862008-12-02 09:54:17 -05002894static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04002895 struct extent_state *state, int uptodate)
2896{
Josef Bacik5fd02042012-05-02 14:00:54 -04002897 struct inode *inode = page->mapping->host;
2898 struct btrfs_root *root = BTRFS_I(inode)->root;
2899 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08002900 struct btrfs_workqueue *wq;
2901 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04002902
liubo1abe9b82011-03-24 11:18:59 +00002903 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2904
Chris Mason8b62b722009-09-02 16:53:46 -04002905 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002906 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2907 end - start + 1, uptodate))
2908 return 0;
2909
Liu Bo9e0af232014-08-15 23:36:53 +08002910 if (btrfs_is_free_space_inode(inode)) {
2911 wq = root->fs_info->endio_freespace_worker;
2912 func = btrfs_freespace_write_helper;
2913 } else {
2914 wq = root->fs_info->endio_write_workers;
2915 func = btrfs_endio_write_helper;
2916 }
Josef Bacik5fd02042012-05-02 14:00:54 -04002917
Liu Bo9e0af232014-08-15 23:36:53 +08002918 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
2919 NULL);
2920 btrfs_queue_work(wq, &ordered_extent->work);
Josef Bacik5fd02042012-05-02 14:00:54 -04002921
2922 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04002923}
2924
Miao Xiedc380ae2014-09-12 18:43:55 +08002925static int __readpage_endio_check(struct inode *inode,
2926 struct btrfs_io_bio *io_bio,
2927 int icsum, struct page *page,
2928 int pgoff, u64 start, size_t len)
2929{
2930 char *kaddr;
2931 u32 csum_expected;
2932 u32 csum = ~(u32)0;
2933 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
2934 DEFAULT_RATELIMIT_BURST);
2935
2936 csum_expected = *(((u32 *)io_bio->csum) + icsum);
2937
2938 kaddr = kmap_atomic(page);
2939 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
2940 btrfs_csum_final(csum, (char *)&csum);
2941 if (csum != csum_expected)
2942 goto zeroit;
2943
2944 kunmap_atomic(kaddr);
2945 return 0;
2946zeroit:
2947 if (__ratelimit(&_rs))
David Sterbaf0954c62014-12-19 18:38:44 +01002948 btrfs_warn(BTRFS_I(inode)->root->fs_info,
Miao Xiedc380ae2014-09-12 18:43:55 +08002949 "csum failed ino %llu off %llu csum %u expected csum %u",
2950 btrfs_ino(inode), start, csum, csum_expected);
2951 memset(kaddr + pgoff, 1, len);
2952 flush_dcache_page(page);
2953 kunmap_atomic(kaddr);
2954 if (csum_expected == 0)
2955 return 0;
2956 return -EIO;
2957}
2958
Chris Masond352ac62008-09-29 15:18:18 -04002959/*
Chris Masond352ac62008-09-29 15:18:18 -04002960 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002961 * if there's a match, we allow the bio to finish. If not, the code in
2962 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002963 */
Miao Xiefacc8a222013-07-25 19:22:34 +08002964static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
2965 u64 phy_offset, struct page *page,
2966 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002967{
Miao Xie4eee4fa2012-12-21 09:17:45 +00002968 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04002969 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002970 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04002971 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05002972
Chris Masond20f7042008-12-08 16:58:54 -05002973 if (PageChecked(page)) {
2974 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08002975 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05002976 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002977
2978 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08002979 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05002980
Yan Zheng17d217f2008-12-12 10:03:38 -05002981 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002982 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002983 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
2984 GFP_NOFS);
2985 return 0;
2986 }
2987
Miao Xiefacc8a222013-07-25 19:22:34 +08002988 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08002989 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
2990 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04002991}
Chris Masonb888db22007-08-27 16:49:44 -04002992
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002993struct delayed_iput {
2994 struct list_head list;
2995 struct inode *inode;
2996};
2997
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002998/* JDM: If this is fs-wide, why can't we add a pointer to
2999 * btrfs_inode instead and avoid the allocation? */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003000void btrfs_add_delayed_iput(struct inode *inode)
3001{
3002 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
3003 struct delayed_iput *delayed;
3004
3005 if (atomic_add_unless(&inode->i_count, -1, 1))
3006 return;
3007
3008 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
3009 delayed->inode = inode;
3010
3011 spin_lock(&fs_info->delayed_iput_lock);
3012 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
3013 spin_unlock(&fs_info->delayed_iput_lock);
3014}
3015
3016void btrfs_run_delayed_iputs(struct btrfs_root *root)
3017{
3018 LIST_HEAD(list);
3019 struct btrfs_fs_info *fs_info = root->fs_info;
3020 struct delayed_iput *delayed;
3021 int empty;
3022
3023 spin_lock(&fs_info->delayed_iput_lock);
3024 empty = list_empty(&fs_info->delayed_iputs);
3025 spin_unlock(&fs_info->delayed_iput_lock);
3026 if (empty)
3027 return;
3028
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003029 spin_lock(&fs_info->delayed_iput_lock);
3030 list_splice_init(&fs_info->delayed_iputs, &list);
3031 spin_unlock(&fs_info->delayed_iput_lock);
3032
3033 while (!list_empty(&list)) {
3034 delayed = list_entry(list.next, struct delayed_iput, list);
3035 list_del(&delayed->list);
3036 iput(delayed->inode);
3037 kfree(delayed);
3038 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003039}
3040
Yan, Zhengd68fc572010-05-16 10:49:58 -04003041/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08003042 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04003043 * files in the subvolume, it removes orphan item and frees block_rsv
3044 * structure.
3045 */
3046void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3047 struct btrfs_root *root)
3048{
Josef Bacik90290e12011-12-02 15:44:12 -05003049 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003050 int ret;
3051
Josef Bacik8a35d952012-05-23 14:26:42 -04003052 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04003053 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
3054 return;
3055
Josef Bacik90290e12011-12-02 15:44:12 -05003056 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003057 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05003058 spin_unlock(&root->orphan_lock);
3059 return;
3060 }
3061
3062 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
3063 spin_unlock(&root->orphan_lock);
3064 return;
3065 }
3066
3067 block_rsv = root->orphan_block_rsv;
3068 root->orphan_block_rsv = NULL;
3069 spin_unlock(&root->orphan_lock);
3070
Miao Xie27cdeb72014-04-02 19:51:05 +08003071 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
Yan, Zhengd68fc572010-05-16 10:49:58 -04003072 btrfs_root_refs(&root->root_item) > 0) {
3073 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
3074 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003075 if (ret)
3076 btrfs_abort_transaction(trans, root, ret);
3077 else
Miao Xie27cdeb72014-04-02 19:51:05 +08003078 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
3079 &root->state);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003080 }
3081
Josef Bacik90290e12011-12-02 15:44:12 -05003082 if (block_rsv) {
3083 WARN_ON(block_rsv->size > 0);
3084 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003085 }
3086}
3087
3088/*
Josef Bacik7b128762008-07-24 12:17:14 -04003089 * This creates an orphan entry for the given inode in case something goes
3090 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003091 *
3092 * NOTE: caller of this function should reserve 5 units of metadata for
3093 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04003094 */
3095int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
3096{
3097 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003098 struct btrfs_block_rsv *block_rsv = NULL;
3099 int reserve = 0;
3100 int insert = 0;
3101 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003102
Yan, Zhengd68fc572010-05-16 10:49:58 -04003103 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06003104 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003105 if (!block_rsv)
3106 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04003107 }
3108
Yan, Zhengd68fc572010-05-16 10:49:58 -04003109 spin_lock(&root->orphan_lock);
3110 if (!root->orphan_block_rsv) {
3111 root->orphan_block_rsv = block_rsv;
3112 } else if (block_rsv) {
3113 btrfs_free_block_rsv(root, block_rsv);
3114 block_rsv = NULL;
3115 }
Josef Bacik7b128762008-07-24 12:17:14 -04003116
Josef Bacik8a35d952012-05-23 14:26:42 -04003117 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3118 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003119#if 0
3120 /*
3121 * For proper ENOSPC handling, we should do orphan
3122 * cleanup when mounting. But this introduces backward
3123 * compatibility issue.
3124 */
3125 if (!xchg(&root->orphan_item_inserted, 1))
3126 insert = 2;
3127 else
3128 insert = 1;
3129#endif
3130 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003131 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003132 }
Josef Bacik7b128762008-07-24 12:17:14 -04003133
Josef Bacik72ac3c02012-05-23 14:13:11 -04003134 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3135 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003136 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003137 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003138
Yan, Zhengd68fc572010-05-16 10:49:58 -04003139 /* grab metadata reservation from transaction handle */
3140 if (reserve) {
3141 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003142 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04003143 }
3144
3145 /* insert an orphan item to track this unlinked/truncated file */
3146 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08003147 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003148 if (ret) {
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003149 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003150 if (reserve) {
3151 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3152 &BTRFS_I(inode)->runtime_flags);
3153 btrfs_orphan_release_metadata(inode);
3154 }
3155 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003156 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3157 &BTRFS_I(inode)->runtime_flags);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003158 btrfs_abort_transaction(trans, root, ret);
3159 return ret;
3160 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003161 }
3162 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003163 }
3164
3165 /* insert an orphan item to track subvolume contains orphan files */
3166 if (insert >= 2) {
3167 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
3168 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003169 if (ret && ret != -EEXIST) {
3170 btrfs_abort_transaction(trans, root, ret);
3171 return ret;
3172 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003173 }
3174 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003175}
3176
3177/*
3178 * We have done the truncate/delete so we can go ahead and remove the orphan
3179 * item for this particular inode.
3180 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003181static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3182 struct inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003183{
3184 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003185 int delete_item = 0;
3186 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003187 int ret = 0;
3188
Yan, Zhengd68fc572010-05-16 10:49:58 -04003189 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003190 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3191 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003192 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003193
Josef Bacik72ac3c02012-05-23 14:13:11 -04003194 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3195 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003196 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003197 spin_unlock(&root->orphan_lock);
3198
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003199 if (delete_item) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003200 atomic_dec(&root->orphan_inodes);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003201 if (trans)
3202 ret = btrfs_del_orphan_item(trans, root,
3203 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04003204 }
Josef Bacik7b128762008-07-24 12:17:14 -04003205
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003206 if (release_rsv)
3207 btrfs_orphan_release_metadata(inode);
3208
Josef Bacik4ef31a42013-08-13 14:10:08 -04003209 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003210}
3211
3212/*
3213 * this cleans up any orphans that may be left on the list from the last use
3214 * of this root.
3215 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003216int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003217{
3218 struct btrfs_path *path;
3219 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003220 struct btrfs_key key, found_key;
3221 struct btrfs_trans_handle *trans;
3222 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003223 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003224 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3225
Yan, Zhengd68fc572010-05-16 10:49:58 -04003226 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003227 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003228
3229 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003230 if (!path) {
3231 ret = -ENOMEM;
3232 goto out;
3233 }
Josef Bacik7b128762008-07-24 12:17:14 -04003234 path->reada = -1;
3235
3236 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003237 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003238 key.offset = (u64)-1;
3239
Josef Bacik7b128762008-07-24 12:17:14 -04003240 while (1) {
3241 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003242 if (ret < 0)
3243 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003244
3245 /*
3246 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003247 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003248 * find the key and see if we have stuff that matches
3249 */
3250 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003251 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003252 if (path->slots[0] == 0)
3253 break;
3254 path->slots[0]--;
3255 }
3256
3257 /* pull out the item */
3258 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003259 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3260
3261 /* make sure the item matches what we want */
3262 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3263 break;
David Sterba962a2982014-06-04 18:41:45 +02003264 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003265 break;
3266
3267 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003268 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003269
3270 /*
3271 * this is where we are basically btrfs_lookup, without the
3272 * crossing root thing. we store the inode number in the
3273 * offset of the orphan item.
3274 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003275
3276 if (found_key.offset == last_objectid) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003277 btrfs_err(root->fs_info,
3278 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003279 ret = -EINVAL;
3280 goto out;
3281 }
3282
3283 last_objectid = found_key.offset;
3284
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003285 found_key.objectid = found_key.offset;
3286 found_key.type = BTRFS_INODE_ITEM_KEY;
3287 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00003288 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303289 ret = PTR_ERR_OR_ZERO(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003290 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003291 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003292
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003293 if (ret == -ESTALE && root == root->fs_info->tree_root) {
3294 struct btrfs_root *dead_root;
3295 struct btrfs_fs_info *fs_info = root->fs_info;
3296 int is_dead_root = 0;
3297
3298 /*
3299 * this is an orphan in the tree root. Currently these
3300 * could come from 2 sources:
3301 * a) a snapshot deletion in progress
3302 * b) a free space cache inode
3303 * We need to distinguish those two, as the snapshot
3304 * orphan must not get deleted.
3305 * find_dead_roots already ran before us, so if this
3306 * is a snapshot deletion, we should find the root
3307 * in the dead_roots list
3308 */
3309 spin_lock(&fs_info->trans_lock);
3310 list_for_each_entry(dead_root, &fs_info->dead_roots,
3311 root_list) {
3312 if (dead_root->root_key.objectid ==
3313 found_key.objectid) {
3314 is_dead_root = 1;
3315 break;
3316 }
3317 }
3318 spin_unlock(&fs_info->trans_lock);
3319 if (is_dead_root) {
3320 /* prevent this orphan from being found again */
3321 key.offset = found_key.objectid - 1;
3322 continue;
3323 }
3324 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003325 /*
3326 * Inode is already gone but the orphan item is still there,
3327 * kill the orphan item.
3328 */
3329 if (ret == -ESTALE) {
3330 trans = btrfs_start_transaction(root, 1);
3331 if (IS_ERR(trans)) {
3332 ret = PTR_ERR(trans);
3333 goto out;
3334 }
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003335 btrfs_debug(root->fs_info, "auto deleting %Lu",
3336 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003337 ret = btrfs_del_orphan_item(trans, root,
3338 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003339 btrfs_end_transaction(trans, root);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003340 if (ret)
3341 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003342 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003343 }
Josef Bacik7b128762008-07-24 12:17:14 -04003344
Josef Bacik7b128762008-07-24 12:17:14 -04003345 /*
3346 * add this inode to the orphan list so btrfs_orphan_del does
3347 * the proper thing when we hit it
3348 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003349 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3350 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003351 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003352
Josef Bacik7b128762008-07-24 12:17:14 -04003353 /* if we have links, this was a truncate, lets do that */
3354 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303355 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003356 iput(inode);
3357 continue;
3358 }
Josef Bacik7b128762008-07-24 12:17:14 -04003359 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003360
3361 /* 1 for the orphan item deletion. */
3362 trans = btrfs_start_transaction(root, 1);
3363 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003364 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003365 ret = PTR_ERR(trans);
3366 goto out;
3367 }
3368 ret = btrfs_orphan_add(trans, inode);
3369 btrfs_end_transaction(trans, root);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003370 if (ret) {
3371 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003372 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003373 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003374
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003375 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003376 if (ret)
3377 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003378 } else {
3379 nr_unlink++;
3380 }
3381
3382 /* this will do delete_inode and everything for us */
3383 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003384 if (ret)
3385 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003386 }
Miao Xie3254c872011-11-10 20:45:05 -05003387 /* release the path since we're done with it */
3388 btrfs_release_path(path);
3389
Yan, Zhengd68fc572010-05-16 10:49:58 -04003390 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3391
3392 if (root->orphan_block_rsv)
3393 btrfs_block_rsv_release(root, root->orphan_block_rsv,
3394 (u64)-1);
3395
Miao Xie27cdeb72014-04-02 19:51:05 +08003396 if (root->orphan_block_rsv ||
3397 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003398 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003399 if (!IS_ERR(trans))
3400 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003401 }
Josef Bacik7b128762008-07-24 12:17:14 -04003402
3403 if (nr_unlink)
David Sterba4884b472013-03-20 13:31:27 +00003404 btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003405 if (nr_truncate)
David Sterba4884b472013-03-20 13:31:27 +00003406 btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003407
3408out:
3409 if (ret)
David Sterba68b663d2014-12-19 18:38:37 +01003410 btrfs_err(root->fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003411 "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003412 btrfs_free_path(path);
3413 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003414}
3415
Chris Masond352ac62008-09-29 15:18:18 -04003416/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003417 * very simple check to peek ahead in the leaf looking for xattrs. If we
3418 * don't find any xattrs, we know there can't be any acls.
3419 *
3420 * slot is the slot the inode is in, objectid is the objectid of the inode
3421 */
3422static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003423 int slot, u64 objectid,
3424 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003425{
3426 u32 nritems = btrfs_header_nritems(leaf);
3427 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003428 static u64 xattr_access = 0;
3429 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003430 int scanned = 0;
3431
Josef Bacikf23b5a52013-06-19 10:16:26 -04003432 if (!xattr_access) {
3433 xattr_access = btrfs_name_hash(POSIX_ACL_XATTR_ACCESS,
3434 strlen(POSIX_ACL_XATTR_ACCESS));
3435 xattr_default = btrfs_name_hash(POSIX_ACL_XATTR_DEFAULT,
3436 strlen(POSIX_ACL_XATTR_DEFAULT));
3437 }
3438
Chris Mason46a53cc2009-04-27 11:47:50 -04003439 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003440 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003441 while (slot < nritems) {
3442 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3443
3444 /* we found a different objectid, there must not be acls */
3445 if (found_key.objectid != objectid)
3446 return 0;
3447
3448 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003449 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003450 if (*first_xattr_slot == -1)
3451 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003452 if (found_key.offset == xattr_access ||
3453 found_key.offset == xattr_default)
3454 return 1;
3455 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003456
3457 /*
3458 * we found a key greater than an xattr key, there can't
3459 * be any acls later on
3460 */
3461 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3462 return 0;
3463
3464 slot++;
3465 scanned++;
3466
3467 /*
3468 * it goes inode, inode backrefs, xattrs, extents,
3469 * so if there are a ton of hard links to an inode there can
3470 * be a lot of backrefs. Don't waste time searching too hard,
3471 * this is just an optimization
3472 */
3473 if (scanned >= 8)
3474 break;
3475 }
3476 /* we hit the end of the leaf before we found an xattr or
3477 * something larger than an xattr. We have to assume the inode
3478 * has acls
3479 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003480 if (*first_xattr_slot == -1)
3481 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003482 return 1;
3483}
3484
3485/*
Chris Masond352ac62008-09-29 15:18:18 -04003486 * read an inode from the btree into the in-memory inode
3487 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003488static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003489{
3490 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003491 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003492 struct btrfs_inode_item *inode_item;
3493 struct btrfs_root *root = BTRFS_I(inode)->root;
3494 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003495 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003496 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003497 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003498 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003499 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003500 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003501
3502 ret = btrfs_fill_inode(inode, &rdev);
3503 if (!ret)
3504 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003505
3506 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07003507 if (!path)
3508 goto make_bad;
3509
Chris Mason39279cc2007-06-12 06:35:45 -04003510 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003511
Chris Mason39279cc2007-06-12 06:35:45 -04003512 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003513 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04003514 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04003515
Chris Mason5f39d392007-10-15 16:14:19 -04003516 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003517
3518 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003519 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003520
Chris Mason5f39d392007-10-15 16:14:19 -04003521 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3522 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003523 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003524 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003525 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3526 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04003527 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003528
David Sterbaa937b972014-12-12 17:39:12 +01003529 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3530 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003531
David Sterbaa937b972014-12-12 17:39:12 +01003532 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3533 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003534
David Sterbaa937b972014-12-12 17:39:12 +01003535 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3536 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003537
chandan r9cc97d62012-07-04 12:48:07 +05303538 BTRFS_I(inode)->i_otime.tv_sec =
3539 btrfs_timespec_sec(leaf, &inode_item->otime);
3540 BTRFS_I(inode)->i_otime.tv_nsec =
3541 btrfs_timespec_nsec(leaf, &inode_item->otime);
3542
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003543 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003544 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003545 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3546
3547 /*
3548 * If we were modified in the current generation and evicted from memory
3549 * and then re-read we need to do a full sync since we don't have any
3550 * idea about which extents were modified before we were evicted from
3551 * cache.
3552 */
3553 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
3554 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3555 &BTRFS_I(inode)->runtime_flags);
3556
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003557 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003558 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003559 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003560 rdev = btrfs_inode_rdev(leaf, inode_item);
3561
Josef Bacikaec74772008-07-24 12:12:38 -04003562 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003563 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003564
3565cache_index:
3566 path->slots[0]++;
3567 if (inode->i_nlink != 1 ||
3568 path->slots[0] >= btrfs_header_nritems(leaf))
3569 goto cache_acl;
3570
3571 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
3572 if (location.objectid != btrfs_ino(inode))
3573 goto cache_acl;
3574
3575 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3576 if (location.type == BTRFS_INODE_REF_KEY) {
3577 struct btrfs_inode_ref *ref;
3578
3579 ref = (struct btrfs_inode_ref *)ptr;
3580 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3581 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3582 struct btrfs_inode_extref *extref;
3583
3584 extref = (struct btrfs_inode_extref *)ptr;
3585 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3586 extref);
3587 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003588cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003589 /*
3590 * try to precache a NULL acl entry for files that don't have
3591 * any xattrs or acls
3592 */
Li Zefan33345d012011-04-20 10:31:50 +08003593 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003594 btrfs_ino(inode), &first_xattr_slot);
3595 if (first_xattr_slot != -1) {
3596 path->slots[0] = first_xattr_slot;
3597 ret = btrfs_load_inode_props(inode, path);
3598 if (ret)
3599 btrfs_err(root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003600 "error loading props for ino %llu (root %llu): %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003601 btrfs_ino(inode),
3602 root->root_key.objectid, ret);
3603 }
3604 btrfs_free_path(path);
3605
Al Viro72c04902009-06-24 16:58:48 -04003606 if (!maybe_acls)
3607 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003608
Chris Mason39279cc2007-06-12 06:35:45 -04003609 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003610 case S_IFREG:
3611 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003612 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05003613 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003614 inode->i_fop = &btrfs_file_operations;
3615 inode->i_op = &btrfs_file_inode_operations;
3616 break;
3617 case S_IFDIR:
3618 inode->i_fop = &btrfs_dir_file_operations;
3619 if (root == root->fs_info->tree_root)
3620 inode->i_op = &btrfs_dir_ro_inode_operations;
3621 else
3622 inode->i_op = &btrfs_dir_inode_operations;
3623 break;
3624 case S_IFLNK:
3625 inode->i_op = &btrfs_symlink_inode_operations;
3626 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04003627 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003628 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003629 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003630 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003631 init_special_inode(inode, inode->i_mode, rdev);
3632 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003633 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003634
3635 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003636 return;
3637
3638make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003639 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003640 make_bad_inode(inode);
3641}
3642
Chris Masond352ac62008-09-29 15:18:18 -04003643/*
3644 * given a leaf and an inode, copy the inode fields into the leaf
3645 */
Chris Masone02119d2008-09-05 16:13:11 -04003646static void fill_inode_item(struct btrfs_trans_handle *trans,
3647 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003648 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003649 struct inode *inode)
3650{
Liu Bo51fab692012-12-27 09:01:21 +00003651 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003652
Liu Bo51fab692012-12-27 09:01:21 +00003653 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003654
Liu Bo51fab692012-12-27 09:01:21 +00003655 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3656 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3657 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3658 &token);
3659 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3660 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003661
David Sterbaa937b972014-12-12 17:39:12 +01003662 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003663 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003664 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003665 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003666
David Sterbaa937b972014-12-12 17:39:12 +01003667 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003668 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003669 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003670 inode->i_mtime.tv_nsec, &token);
3671
David Sterbaa937b972014-12-12 17:39:12 +01003672 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003673 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003674 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003675 inode->i_ctime.tv_nsec, &token);
3676
chandan r9cc97d62012-07-04 12:48:07 +05303677 btrfs_set_token_timespec_sec(leaf, &item->otime,
3678 BTRFS_I(inode)->i_otime.tv_sec, &token);
3679 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3680 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3681
Liu Bo51fab692012-12-27 09:01:21 +00003682 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3683 &token);
3684 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3685 &token);
3686 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3687 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3688 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3689 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3690 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003691}
3692
Chris Masond352ac62008-09-29 15:18:18 -04003693/*
3694 * copy everything in the in-memory inode into the btree.
3695 */
Chris Mason21151332011-11-10 20:39:08 -05003696static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003697 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003698{
3699 struct btrfs_inode_item *inode_item;
3700 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003701 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003702 int ret;
3703
3704 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003705 if (!path)
3706 return -ENOMEM;
3707
Chris Masonb9473432009-03-13 11:00:37 -04003708 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003709 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3710 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003711 if (ret) {
3712 if (ret > 0)
3713 ret = -ENOENT;
3714 goto failed;
3715 }
3716
Chris Mason5f39d392007-10-15 16:14:19 -04003717 leaf = path->nodes[0];
3718 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003719 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003720
Chris Masone02119d2008-09-05 16:13:11 -04003721 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003722 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003723 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003724 ret = 0;
3725failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003726 btrfs_free_path(path);
3727 return ret;
3728}
3729
Chris Masond352ac62008-09-29 15:18:18 -04003730/*
Chris Mason21151332011-11-10 20:39:08 -05003731 * copy everything in the in-memory inode into the btree.
3732 */
3733noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3734 struct btrfs_root *root, struct inode *inode)
3735{
3736 int ret;
3737
3738 /*
3739 * If the inode is a free space inode, we can deadlock during commit
3740 * if we put it into the delayed code.
3741 *
3742 * The data relocation inode should also be directly updated
3743 * without delay
3744 */
Liu Bo83eea1f2012-07-10 05:28:39 -06003745 if (!btrfs_is_free_space_inode(inode)
Josef Bacik1d52c782014-09-18 11:30:44 -04003746 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
3747 && !root->fs_info->log_root_recovering) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003748 btrfs_update_root_times(trans, root);
3749
Chris Mason21151332011-11-10 20:39:08 -05003750 ret = btrfs_delayed_update_inode(trans, root, inode);
3751 if (!ret)
3752 btrfs_set_inode_last_trans(trans, inode);
3753 return ret;
3754 }
3755
3756 return btrfs_update_inode_item(trans, root, inode);
3757}
3758
Josef Bacikbe6aef62012-10-22 15:43:12 -04003759noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3760 struct btrfs_root *root,
3761 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003762{
3763 int ret;
3764
3765 ret = btrfs_update_inode(trans, root, inode);
3766 if (ret == -ENOSPC)
3767 return btrfs_update_inode_item(trans, root, inode);
3768 return ret;
3769}
3770
3771/*
Chris Masond352ac62008-09-29 15:18:18 -04003772 * unlink helper that gets used here in inode.c and in the tree logging
3773 * recovery code. It remove a link in a directory with a given name, and
3774 * also drops the back refs in the inode to the directory
3775 */
Al Viro92986792011-03-04 17:14:37 +00003776static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3777 struct btrfs_root *root,
3778 struct inode *dir, struct inode *inode,
3779 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003780{
3781 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003782 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003783 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003784 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003785 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003786 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003787 u64 ino = btrfs_ino(inode);
3788 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003789
3790 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003791 if (!path) {
3792 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003793 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003794 }
3795
Chris Masonb9473432009-03-13 11:00:37 -04003796 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003797 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003798 name, name_len, -1);
3799 if (IS_ERR(di)) {
3800 ret = PTR_ERR(di);
3801 goto err;
3802 }
3803 if (!di) {
3804 ret = -ENOENT;
3805 goto err;
3806 }
Chris Mason5f39d392007-10-15 16:14:19 -04003807 leaf = path->nodes[0];
3808 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003809 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003810 if (ret)
3811 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003812 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003813
Miao Xie67de1172013-12-26 13:07:06 +08003814 /*
3815 * If we don't have dir index, we have to get it by looking up
3816 * the inode ref, since we get the inode ref, remove it directly,
3817 * it is unnecessary to do delayed deletion.
3818 *
3819 * But if we have dir index, needn't search inode ref to get it.
3820 * Since the inode ref is close to the inode item, it is better
3821 * that we delay to delete it, and just do this deletion when
3822 * we update the inode item.
3823 */
3824 if (BTRFS_I(inode)->dir_index) {
3825 ret = btrfs_delayed_delete_inode_ref(inode);
3826 if (!ret) {
3827 index = BTRFS_I(inode)->dir_index;
3828 goto skip_backref;
3829 }
3830 }
3831
Li Zefan33345d012011-04-20 10:31:50 +08003832 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3833 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003834 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003835 btrfs_info(root->fs_info,
3836 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003837 name_len, name, ino, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003838 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003839 goto err;
3840 }
Miao Xie67de1172013-12-26 13:07:06 +08003841skip_backref:
Miao Xie16cdcec2011-04-22 18:12:22 +08003842 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003843 if (ret) {
3844 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003845 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003846 }
Chris Mason39279cc2007-06-12 06:35:45 -04003847
Chris Masone02119d2008-09-05 16:13:11 -04003848 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08003849 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003850 if (ret != 0 && ret != -ENOENT) {
3851 btrfs_abort_transaction(trans, root, ret);
3852 goto err;
3853 }
Chris Masone02119d2008-09-05 16:13:11 -04003854
3855 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
3856 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04003857 if (ret == -ENOENT)
3858 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003859 else if (ret)
3860 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003861err:
3862 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003863 if (ret)
3864 goto out;
3865
3866 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003867 inode_inc_iversion(inode);
3868 inode_inc_iversion(dir);
Chris Masone02119d2008-09-05 16:13:11 -04003869 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Tsutomu Itohb9959292012-06-25 21:25:22 -06003870 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04003871out:
Chris Mason39279cc2007-06-12 06:35:45 -04003872 return ret;
3873}
3874
Al Viro92986792011-03-04 17:14:37 +00003875int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3876 struct btrfs_root *root,
3877 struct inode *dir, struct inode *inode,
3878 const char *name, int name_len)
3879{
3880 int ret;
3881 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3882 if (!ret) {
Zach Brown8b558c52013-10-16 12:10:34 -07003883 drop_nlink(inode);
Al Viro92986792011-03-04 17:14:37 +00003884 ret = btrfs_update_inode(trans, root, inode);
3885 }
3886 return ret;
3887}
Yan, Zhenga22285a2010-05-16 10:48:46 -04003888
3889/*
3890 * helper to start transaction for unlink and rmdir.
3891 *
Josef Bacikd52be812013-05-29 14:54:47 -04003892 * unlink and rmdir are special in btrfs, they do not always free space, so
3893 * if we cannot make our reservations the normal way try and see if there is
3894 * plenty of slack room in the global reserve to migrate, otherwise we cannot
3895 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04003896 */
Josef Bacikd52be812013-05-29 14:54:47 -04003897static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003898{
3899 struct btrfs_trans_handle *trans;
3900 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003901 int ret;
3902
Josef Bacike70bea52011-10-11 14:18:24 -04003903 /*
3904 * 1 for the possible orphan item
3905 * 1 for the dir item
3906 * 1 for the dir index
3907 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04003908 * 1 for the inode
3909 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04003910 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003911 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
3912 return trans;
3913
Josef Bacikd52be812013-05-29 14:54:47 -04003914 if (PTR_ERR(trans) == -ENOSPC) {
3915 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003916
Josef Bacikd52be812013-05-29 14:54:47 -04003917 trans = btrfs_start_transaction(root, 0);
3918 if (IS_ERR(trans))
3919 return trans;
3920 ret = btrfs_cond_migrate_bytes(root->fs_info,
3921 &root->fs_info->trans_block_rsv,
3922 num_bytes, 5);
3923 if (ret) {
3924 btrfs_end_transaction(trans, root);
3925 return ERR_PTR(ret);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003926 }
Josef Bacik5a77d762011-11-01 14:32:23 -04003927 trans->block_rsv = &root->fs_info->trans_block_rsv;
Josef Bacikd52be812013-05-29 14:54:47 -04003928 trans->bytes_reserved = num_bytes;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003929 }
Josef Bacikd52be812013-05-29 14:54:47 -04003930 return trans;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003931}
3932
Chris Mason39279cc2007-06-12 06:35:45 -04003933static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3934{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003935 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003936 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04003937 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003938 int ret;
3939
Josef Bacikd52be812013-05-29 14:54:47 -04003940 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003941 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003942 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003943
Chris Mason12fcfd22009-03-24 10:24:20 -04003944 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
3945
Chris Masone02119d2008-09-05 16:13:11 -04003946 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3947 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003948 if (ret)
3949 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003950
Yan, Zhenga22285a2010-05-16 10:48:46 -04003951 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04003952 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003953 if (ret)
3954 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003955 }
Josef Bacik7b128762008-07-24 12:17:14 -04003956
Tsutomu Itohb5324022011-07-19 07:27:20 +00003957out:
Josef Bacikd52be812013-05-29 14:54:47 -04003958 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003959 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04003960 return ret;
3961}
3962
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003963int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3964 struct btrfs_root *root,
3965 struct inode *dir, u64 objectid,
3966 const char *name, int name_len)
3967{
3968 struct btrfs_path *path;
3969 struct extent_buffer *leaf;
3970 struct btrfs_dir_item *di;
3971 struct btrfs_key key;
3972 u64 index;
3973 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003974 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003975
3976 path = btrfs_alloc_path();
3977 if (!path)
3978 return -ENOMEM;
3979
Li Zefan33345d012011-04-20 10:31:50 +08003980 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003981 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003982 if (IS_ERR_OR_NULL(di)) {
3983 if (!di)
3984 ret = -ENOENT;
3985 else
3986 ret = PTR_ERR(di);
3987 goto out;
3988 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003989
3990 leaf = path->nodes[0];
3991 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3992 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3993 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003994 if (ret) {
3995 btrfs_abort_transaction(trans, root, ret);
3996 goto out;
3997 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003998 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003999
4000 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
4001 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08004002 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004003 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004004 if (ret != -ENOENT) {
4005 btrfs_abort_transaction(trans, root, ret);
4006 goto out;
4007 }
Li Zefan33345d012011-04-20 10:31:50 +08004008 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004009 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004010 if (IS_ERR_OR_NULL(di)) {
4011 if (!di)
4012 ret = -ENOENT;
4013 else
4014 ret = PTR_ERR(di);
4015 btrfs_abort_transaction(trans, root, ret);
4016 goto out;
4017 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004018
4019 leaf = path->nodes[0];
4020 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004021 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004022 index = key.offset;
4023 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004024 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004025
Miao Xie16cdcec2011-04-22 18:12:22 +08004026 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004027 if (ret) {
4028 btrfs_abort_transaction(trans, root, ret);
4029 goto out;
4030 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004031
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004032 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004033 inode_inc_iversion(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004034 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Josef Bacik5a24e842012-08-08 10:12:59 -06004035 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004036 if (ret)
4037 btrfs_abort_transaction(trans, root, ret);
4038out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004039 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004040 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004041}
4042
Chris Mason39279cc2007-06-12 06:35:45 -04004043static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4044{
4045 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05004046 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004047 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004048 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004049
David Sterbab3ae2442012-09-13 16:04:34 -06004050 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004051 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06004052 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
4053 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04004054
Josef Bacikd52be812013-05-29 14:54:47 -04004055 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004056 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004057 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004058
Li Zefan33345d012011-04-20 10:31:50 +08004059 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004060 err = btrfs_unlink_subvol(trans, root, dir,
4061 BTRFS_I(inode)->location.objectid,
4062 dentry->d_name.name,
4063 dentry->d_name.len);
4064 goto out;
4065 }
4066
Josef Bacik7b128762008-07-24 12:17:14 -04004067 err = btrfs_orphan_add(trans, inode);
4068 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004069 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004070
Chris Mason39279cc2007-06-12 06:35:45 -04004071 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04004072 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
4073 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05004074 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04004075 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004076out:
Josef Bacikd52be812013-05-29 14:54:47 -04004077 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004078 btrfs_btree_balance_dirty(root);
Chris Mason39544012007-12-12 14:38:19 -05004079
Chris Mason39279cc2007-06-12 06:35:45 -04004080 return err;
4081}
4082
Chris Mason323ac952008-10-01 19:05:46 -04004083/*
Chris Mason39279cc2007-06-12 06:35:45 -04004084 * this can truncate away extent items, csum items and directory items.
4085 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004086 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004087 *
4088 * csum items that cross the new i_size are truncated to the new size
4089 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004090 *
4091 * min_type is the minimum key type to truncate down to. If set to 0, this
4092 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004093 */
Yan, Zheng80825102009-11-12 09:35:36 +00004094int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4095 struct btrfs_root *root,
4096 struct inode *inode,
4097 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004098{
Chris Mason39279cc2007-06-12 06:35:45 -04004099 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004100 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004101 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004102 struct btrfs_key key;
4103 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004104 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004105 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004106 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004107 u64 item_end = 0;
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004108 u64 last_size = (u64)-1;
Yan, Zheng80825102009-11-12 09:35:36 +00004109 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004110 int found_extent;
4111 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004112 int pending_del_nr = 0;
4113 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004114 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004115 int ret;
4116 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08004117 u64 ino = btrfs_ino(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004118
4119 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004120
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004121 path = btrfs_alloc_path();
4122 if (!path)
4123 return -ENOMEM;
4124 path->reada = -1;
4125
Josef Bacik5dc562c2012-08-17 13:14:17 -04004126 /*
4127 * We want to drop from the next block forward in case this new size is
4128 * not block aligned since we will be keeping the last block of the
4129 * extent just the way it is.
4130 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004131 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4132 root == root->fs_info->tree_root)
Qu Wenruofda28322013-02-26 08:10:22 +00004133 btrfs_drop_extent_cache(inode, ALIGN(new_size,
4134 root->sectorsize), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004135
Miao Xie16cdcec2011-04-22 18:12:22 +08004136 /*
4137 * This function is also used to drop the items in the log tree before
4138 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4139 * it is used to drop the loged items. So we shouldn't kill the delayed
4140 * items.
4141 */
4142 if (min_type == 0 && root == BTRFS_I(inode)->root)
4143 btrfs_kill_delayed_inode_items(inode);
4144
Li Zefan33345d012011-04-20 10:31:50 +08004145 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004146 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004147 key.type = (u8)-1;
4148
Chris Mason85e21ba2008-01-29 15:11:36 -05004149search_again:
Chris Masonb9473432009-03-13 11:00:37 -04004150 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004151 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004152 if (ret < 0) {
4153 err = ret;
4154 goto out;
4155 }
Chris Masond3977122009-01-05 21:25:51 -05004156
Chris Mason85e21ba2008-01-29 15:11:36 -05004157 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004158 /* there are no items in the tree for us to truncate, we're
4159 * done
4160 */
Yan, Zheng80825102009-11-12 09:35:36 +00004161 if (path->slots[0] == 0)
4162 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004163 path->slots[0]--;
4164 }
4165
Chris Masond3977122009-01-05 21:25:51 -05004166 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004167 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004168 leaf = path->nodes[0];
4169 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004170 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004171
Li Zefan33345d012011-04-20 10:31:50 +08004172 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004173 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004174
Chris Mason85e21ba2008-01-29 15:11:36 -05004175 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004176 break;
4177
Chris Mason5f39d392007-10-15 16:14:19 -04004178 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004179 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004180 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004181 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004182 extent_type = btrfs_file_extent_type(leaf, fi);
4183 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004184 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004185 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04004186 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004187 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004188 path->slots[0], fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004189 }
Yan008630c2007-11-07 13:31:09 -05004190 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004191 }
Yan, Zheng80825102009-11-12 09:35:36 +00004192 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004193 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004194 } else {
4195 if (item_end < new_size)
4196 break;
4197 if (found_key.offset >= new_size)
4198 del_item = 1;
4199 else
4200 del_item = 0;
4201 }
Chris Mason39279cc2007-06-12 06:35:45 -04004202 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004203 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004204 if (found_type != BTRFS_EXTENT_DATA_KEY)
4205 goto delete;
4206
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004207 if (del_item)
4208 last_size = found_key.offset;
4209 else
4210 last_size = new_size;
4211
Chris Mason179e29e2007-11-01 11:28:41 -04004212 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004213 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004214 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004215 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004216 u64 orig_num_bytes =
4217 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004218 extent_num_bytes = ALIGN(new_size -
4219 found_key.offset,
4220 root->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004221 btrfs_set_file_extent_num_bytes(leaf, fi,
4222 extent_num_bytes);
4223 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004224 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004225 if (test_bit(BTRFS_ROOT_REF_COWS,
4226 &root->state) &&
4227 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004228 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004229 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004230 } else {
Chris Masondb945352007-10-15 16:15:53 -04004231 extent_num_bytes =
4232 btrfs_file_extent_disk_num_bytes(leaf,
4233 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004234 extent_offset = found_key.offset -
4235 btrfs_file_extent_offset(leaf, fi);
4236
Chris Mason39279cc2007-06-12 06:35:45 -04004237 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004238 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004239 if (extent_start != 0) {
4240 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004241 if (test_bit(BTRFS_ROOT_REF_COWS,
4242 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004243 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004244 }
4245 }
Chris Mason90692182008-02-08 13:49:28 -05004246 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004247 /*
4248 * we can't truncate inline items that have had
4249 * special encodings
4250 */
4251 if (!del_item &&
4252 btrfs_file_extent_compression(leaf, fi) == 0 &&
4253 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4254 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004255 u32 size = new_size - found_key.offset;
4256
Miao Xie27cdeb72014-04-02 19:51:05 +08004257 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004258 inode_sub_bytes(inode, item_end + 1 -
4259 new_size);
Chris Mason514ac8a2014-01-03 21:07:00 -08004260
4261 /*
4262 * update the ram bytes to properly reflect
4263 * the new size of our item
4264 */
4265 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
Chris Masone02119d2008-09-05 16:13:11 -04004266 size =
4267 btrfs_file_extent_calc_inline_size(size);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004268 btrfs_truncate_item(root, path, size, 1);
Miao Xie27cdeb72014-04-02 19:51:05 +08004269 } else if (test_bit(BTRFS_ROOT_REF_COWS,
4270 &root->state)) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004271 inode_sub_bytes(inode, item_end + 1 -
4272 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05004273 }
Chris Mason39279cc2007-06-12 06:35:45 -04004274 }
Chris Mason179e29e2007-11-01 11:28:41 -04004275delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004276 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004277 if (!pending_del_nr) {
4278 /* no pending yet, add ourselves */
4279 pending_del_slot = path->slots[0];
4280 pending_del_nr = 1;
4281 } else if (pending_del_nr &&
4282 path->slots[0] + 1 == pending_del_slot) {
4283 /* hop on the pending chunk */
4284 pending_del_nr++;
4285 pending_del_slot = path->slots[0];
4286 } else {
Chris Masond3977122009-01-05 21:25:51 -05004287 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004288 }
Chris Mason39279cc2007-06-12 06:35:45 -04004289 } else {
4290 break;
4291 }
Miao Xie27cdeb72014-04-02 19:51:05 +08004292 if (found_extent &&
4293 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4294 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004295 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004296 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004297 extent_num_bytes, 0,
4298 btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02004299 ino, extent_offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004300 BUG_ON(ret);
4301 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004302
Yan, Zheng80825102009-11-12 09:35:36 +00004303 if (found_type == BTRFS_INODE_ITEM_KEY)
4304 break;
4305
4306 if (path->slots[0] == 0 ||
4307 path->slots[0] != pending_del_slot) {
Yan, Zheng80825102009-11-12 09:35:36 +00004308 if (pending_del_nr) {
4309 ret = btrfs_del_items(trans, root, path,
4310 pending_del_slot,
4311 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004312 if (ret) {
4313 btrfs_abort_transaction(trans,
4314 root, ret);
4315 goto error;
4316 }
Yan, Zheng80825102009-11-12 09:35:36 +00004317 pending_del_nr = 0;
4318 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004319 btrfs_release_path(path);
Chris Mason85e21ba2008-01-29 15:11:36 -05004320 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004321 } else {
4322 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004323 }
Chris Mason39279cc2007-06-12 06:35:45 -04004324 }
Yan, Zheng80825102009-11-12 09:35:36 +00004325out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004326 if (pending_del_nr) {
4327 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4328 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004329 if (ret)
4330 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004331 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004332error:
Filipe Mananadac57052014-08-29 20:54:26 +01004333 if (last_size != (u64)-1 &&
4334 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004335 btrfs_ordered_update_i_size(inode, last_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004336 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00004337 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004338}
4339
Chris Masona52d9a82007-08-27 16:49:44 -04004340/*
Josef Bacik2aaa6652012-08-29 14:27:18 -04004341 * btrfs_truncate_page - read, zero a chunk and write a page
4342 * @inode - inode that we're zeroing
4343 * @from - the offset to start zeroing
4344 * @len - the length to zero, 0 to zero the entire range respective to the
4345 * offset
4346 * @front - zero up to the offset instead of from the offset on
4347 *
4348 * This will find the page for the "from" offset and cow the page and zero the
4349 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004350 */
Josef Bacik2aaa6652012-08-29 14:27:18 -04004351int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
4352 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004353{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004354 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04004355 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004356 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4357 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004358 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004359 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04004360 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04004361 pgoff_t index = from >> PAGE_CACHE_SHIFT;
4362 unsigned offset = from & (PAGE_CACHE_SIZE-1);
4363 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004364 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004365 int ret = 0;
4366 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004367 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004368
Josef Bacik2aaa6652012-08-29 14:27:18 -04004369 if ((offset & (blocksize - 1)) == 0 &&
4370 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004371 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004372 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004373 if (ret)
4374 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004375
Chris Mason211c17f2008-05-15 09:13:45 -04004376again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004377 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004378 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004379 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Miao Xieac6a2b32012-12-05 10:56:13 +00004380 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004381 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004382 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004383
4384 page_start = page_offset(page);
4385 page_end = page_start + PAGE_CACHE_SIZE - 1;
4386
Chris Masona52d9a82007-08-27 16:49:44 -04004387 if (!PageUptodate(page)) {
4388 ret = btrfs_readpage(NULL, page);
4389 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004390 if (page->mapping != mapping) {
4391 unlock_page(page);
4392 page_cache_release(page);
4393 goto again;
4394 }
Chris Masona52d9a82007-08-27 16:49:44 -04004395 if (!PageUptodate(page)) {
4396 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004397 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004398 }
4399 }
Chris Mason211c17f2008-05-15 09:13:45 -04004400 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004401
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004402 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004403 set_page_extent_mapped(page);
4404
4405 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4406 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004407 unlock_extent_cached(io_tree, page_start, page_end,
4408 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004409 unlock_page(page);
4410 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004411 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004412 btrfs_put_ordered_extent(ordered);
4413 goto again;
4414 }
4415
Josef Bacik2ac55d42010-02-03 19:33:23 +00004416 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004417 EXTENT_DIRTY | EXTENT_DELALLOC |
4418 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004419 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004420
Josef Bacik2ac55d42010-02-03 19:33:23 +00004421 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
4422 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004423 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004424 unlock_extent_cached(io_tree, page_start, page_end,
4425 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004426 goto out_unlock;
4427 }
4428
Chris Masone6dcd2d2008-07-17 12:53:50 -04004429 if (offset != PAGE_CACHE_SIZE) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004430 if (!len)
4431 len = PAGE_CACHE_SIZE - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004432 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004433 if (front)
4434 memset(kaddr, 0, offset);
4435 else
4436 memset(kaddr + offset, 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004437 flush_dcache_page(page);
4438 kunmap(page);
4439 }
Chris Mason247e7432008-07-17 12:53:51 -04004440 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004441 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004442 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
4443 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004444
Chris Mason89642222008-07-24 09:41:53 -04004445out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004446 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004447 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04004448 unlock_page(page);
4449 page_cache_release(page);
4450out:
4451 return ret;
4452}
4453
Josef Bacik16e75492013-10-22 12:18:51 -04004454static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4455 u64 offset, u64 len)
4456{
4457 struct btrfs_trans_handle *trans;
4458 int ret;
4459
4460 /*
4461 * Still need to make sure the inode looks like it's been updated so
4462 * that any holes get logged if we fsync.
4463 */
4464 if (btrfs_fs_incompat(root->fs_info, NO_HOLES)) {
4465 BTRFS_I(inode)->last_trans = root->fs_info->generation;
4466 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4467 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4468 return 0;
4469 }
4470
4471 /*
4472 * 1 - for the one we're dropping
4473 * 1 - for the one we're adding
4474 * 1 - for updating the inode.
4475 */
4476 trans = btrfs_start_transaction(root, 3);
4477 if (IS_ERR(trans))
4478 return PTR_ERR(trans);
4479
4480 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4481 if (ret) {
4482 btrfs_abort_transaction(trans, root, ret);
4483 btrfs_end_transaction(trans, root);
4484 return ret;
4485 }
4486
4487 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset,
4488 0, 0, len, 0, len, 0, 0, 0);
4489 if (ret)
4490 btrfs_abort_transaction(trans, root, ret);
4491 else
4492 btrfs_update_inode(trans, root, inode);
4493 btrfs_end_transaction(trans, root);
4494 return ret;
4495}
4496
Josef Bacik695a0d02011-03-04 15:46:53 -05004497/*
4498 * This function puts in dummy file extents for the area we're creating a hole
4499 * for. So if we are truncating this file to a larger size we need to insert
4500 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4501 * the range between oldsize and size
4502 */
Josef Bacika41ad392011-01-31 15:30:16 -05004503int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004504{
Yan Zheng9036c102008-10-30 14:19:41 -04004505 struct btrfs_root *root = BTRFS_I(inode)->root;
4506 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004507 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004508 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004509 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Qu Wenruofda28322013-02-26 08:10:22 +00004510 u64 hole_start = ALIGN(oldsize, root->sectorsize);
4511 u64 block_end = ALIGN(size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004512 u64 last_byte;
4513 u64 cur_offset;
4514 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004515 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004516
Josef Bacika71754f2013-06-17 17:14:39 -04004517 /*
4518 * If our size started in the middle of a page we need to zero out the
4519 * rest of the page before we expand the i_size, otherwise we could
4520 * expose stale data.
4521 */
4522 err = btrfs_truncate_page(inode, oldsize, 0, 0);
4523 if (err)
4524 return err;
4525
Yan Zheng9036c102008-10-30 14:19:41 -04004526 if (size <= hole_start)
4527 return 0;
4528
Yan Zheng9036c102008-10-30 14:19:41 -04004529 while (1) {
4530 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004531
Josef Bacik2ac55d42010-02-03 19:33:23 +00004532 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004533 &cached_state);
Miao Xiefa7c1492013-09-26 13:15:27 +08004534 ordered = btrfs_lookup_ordered_range(inode, hole_start,
4535 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004536 if (!ordered)
4537 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004538 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4539 &cached_state, GFP_NOFS);
Miao Xiefa7c1492013-09-26 13:15:27 +08004540 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004541 btrfs_put_ordered_extent(ordered);
4542 }
4543
Yan Zheng9036c102008-10-30 14:19:41 -04004544 cur_offset = hole_start;
4545 while (1) {
4546 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4547 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004548 if (IS_ERR(em)) {
4549 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004550 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004551 break;
4552 }
Yan Zheng9036c102008-10-30 14:19:41 -04004553 last_byte = min(extent_map_end(em), block_end);
Qu Wenruofda28322013-02-26 08:10:22 +00004554 last_byte = ALIGN(last_byte , root->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004555 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004556 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004557 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004558
Josef Bacik16e75492013-10-22 12:18:51 -04004559 err = maybe_insert_hole(root, inode, cur_offset,
4560 hole_size);
4561 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004562 break;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004563 btrfs_drop_extent_cache(inode, cur_offset,
4564 cur_offset + hole_size - 1, 0);
4565 hole_em = alloc_extent_map();
4566 if (!hole_em) {
4567 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4568 &BTRFS_I(inode)->runtime_flags);
4569 goto next;
4570 }
4571 hole_em->start = cur_offset;
4572 hole_em->len = hole_size;
4573 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004574
Josef Bacik5dc562c2012-08-17 13:14:17 -04004575 hole_em->block_start = EXTENT_MAP_HOLE;
4576 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004577 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004578 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004579 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
4580 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Josef Bacik16e75492013-10-22 12:18:51 -04004581 hole_em->generation = root->fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004582
4583 while (1) {
4584 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004585 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004586 write_unlock(&em_tree->lock);
4587 if (err != -EEXIST)
4588 break;
4589 btrfs_drop_extent_cache(inode, cur_offset,
4590 cur_offset +
4591 hole_size - 1, 0);
4592 }
4593 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04004594 }
Josef Bacik16e75492013-10-22 12:18:51 -04004595next:
Yan Zheng9036c102008-10-30 14:19:41 -04004596 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004597 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004598 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004599 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004600 break;
4601 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004602 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004603 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4604 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004605 return err;
4606}
4607
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004608static int wait_snapshoting_atomic_t(atomic_t *a)
4609{
4610 schedule();
4611 return 0;
4612}
4613
4614static void wait_for_snapshot_creation(struct btrfs_root *root)
4615{
4616 while (true) {
4617 int ret;
4618
4619 ret = btrfs_start_write_no_snapshoting(root);
4620 if (ret)
4621 break;
4622 wait_on_atomic_t(&root->will_be_snapshoted,
4623 wait_snapshoting_atomic_t,
4624 TASK_UNINTERRUPTIBLE);
4625 }
4626}
4627
Eric Sandeen3972f262013-01-12 02:57:22 +00004628static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004629{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004630 struct btrfs_root *root = BTRFS_I(inode)->root;
4631 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004632 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004633 loff_t newsize = attr->ia_size;
4634 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004635 int ret;
4636
Eric Sandeen3972f262013-01-12 02:57:22 +00004637 /*
4638 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4639 * special case where we need to update the times despite not having
4640 * these flags set. For all other operations the VFS set these flags
4641 * explicitly if it wants a timestamp update.
4642 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004643 if (newsize != oldsize) {
4644 inode_inc_iversion(inode);
4645 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
4646 inode->i_ctime = inode->i_mtime =
4647 current_fs_time(inode->i_sb);
4648 }
Eric Sandeen3972f262013-01-12 02:57:22 +00004649
Josef Bacika41ad392011-01-31 15:30:16 -05004650 if (newsize > oldsize) {
Kirill A. Shutemov7caef262013-09-12 15:13:56 -07004651 truncate_pagecache(inode, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004652 /*
4653 * Don't do an expanding truncate while snapshoting is ongoing.
4654 * This is to ensure the snapshot captures a fully consistent
4655 * state of this file - if the snapshot captures this expanding
4656 * truncation, it must capture all writes that happened before
4657 * this truncation.
4658 */
4659 wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05004660 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004661 if (ret) {
4662 btrfs_end_write_no_snapshoting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00004663 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004664 }
Yan, Zheng80825102009-11-12 09:35:36 +00004665
Miao Xief4a2f4c2011-12-14 20:12:01 -05004666 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004667 if (IS_ERR(trans)) {
4668 btrfs_end_write_no_snapshoting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004669 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004670 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05004671
4672 i_size_write(inode, newsize);
4673 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
4674 ret = btrfs_update_inode(trans, root, inode);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004675 btrfs_end_write_no_snapshoting(root);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004676 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05004677 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004678
Josef Bacika41ad392011-01-31 15:30:16 -05004679 /*
4680 * We're truncating a file that used to have good data down to
4681 * zero. Make sure it gets into the ordered flush list so that
4682 * any new writes get down to disk quickly.
4683 */
4684 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004685 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4686 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004687
Josef Bacikf3fe8202013-01-07 17:03:21 -05004688 /*
4689 * 1 for the orphan item we're going to add
4690 * 1 for the orphan item deletion.
4691 */
4692 trans = btrfs_start_transaction(root, 2);
4693 if (IS_ERR(trans))
4694 return PTR_ERR(trans);
4695
4696 /*
4697 * We need to do this in case we fail at _any_ point during the
4698 * actual truncate. Once we do the truncate_setsize we could
4699 * invalidate pages which forces any outstanding ordered io to
4700 * be instantly completed which will give us extents that need
4701 * to be truncated. If we fail to get an orphan inode down we
4702 * could have left over extents that were never meant to live,
4703 * so we need to garuntee from this point on that everything
4704 * will be consistent.
4705 */
4706 ret = btrfs_orphan_add(trans, inode);
4707 btrfs_end_transaction(trans, root);
4708 if (ret)
4709 return ret;
4710
Josef Bacika41ad392011-01-31 15:30:16 -05004711 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4712 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004713
4714 /* Disable nonlocked read DIO to avoid the end less truncate */
4715 btrfs_inode_block_unlocked_dio(inode);
4716 inode_dio_wait(inode);
4717 btrfs_inode_resume_unlocked_dio(inode);
4718
Josef Bacika41ad392011-01-31 15:30:16 -05004719 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004720 if (ret && inode->i_nlink) {
4721 int err;
4722
4723 /*
4724 * failed to truncate, disk_i_size is only adjusted down
4725 * as we remove extents, so it should represent the true
4726 * size of the inode, so reset the in memory size and
4727 * delete our orphan entry.
4728 */
4729 trans = btrfs_join_transaction(root);
4730 if (IS_ERR(trans)) {
4731 btrfs_orphan_del(NULL, inode);
4732 return ret;
4733 }
4734 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
4735 err = btrfs_orphan_del(trans, inode);
4736 if (err)
4737 btrfs_abort_transaction(trans, root, err);
4738 btrfs_end_transaction(trans, root);
4739 }
Yan, Zheng80825102009-11-12 09:35:36 +00004740 }
4741
Josef Bacika41ad392011-01-31 15:30:16 -05004742 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004743}
4744
Chris Mason39279cc2007-06-12 06:35:45 -04004745static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
4746{
4747 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08004748 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004749 int err;
4750
Li Zefanb83cc962010-12-20 16:04:08 +08004751 if (btrfs_root_readonly(root))
4752 return -EROFS;
4753
Chris Mason39279cc2007-06-12 06:35:45 -04004754 err = inode_change_ok(inode, attr);
4755 if (err)
4756 return err;
4757
Chris Mason5a3f23d2009-03-31 13:27:11 -04004758 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00004759 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00004760 if (err)
4761 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004762 }
Yan Zheng9036c102008-10-30 14:19:41 -04004763
Christoph Hellwig10257742010-06-04 11:30:02 +02004764 if (attr->ia_valid) {
4765 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004766 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004767 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04004768
Josef Bacik22c44fe2011-11-30 10:45:38 -05004769 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08004770 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02004771 }
4772
Chris Mason39279cc2007-06-12 06:35:45 -04004773 return err;
4774}
Chris Mason61295eb2008-01-14 16:24:38 -05004775
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004776/*
4777 * While truncating the inode pages during eviction, we get the VFS calling
4778 * btrfs_invalidatepage() against each page of the inode. This is slow because
4779 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
4780 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
4781 * extent_state structures over and over, wasting lots of time.
4782 *
4783 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
4784 * those expensive operations on a per page basis and do only the ordered io
4785 * finishing, while we release here the extent_map and extent_state structures,
4786 * without the excessive merging and splitting.
4787 */
4788static void evict_inode_truncate_pages(struct inode *inode)
4789{
4790 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4791 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
4792 struct rb_node *node;
4793
4794 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07004795 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004796
4797 write_lock(&map_tree->lock);
4798 while (!RB_EMPTY_ROOT(&map_tree->map)) {
4799 struct extent_map *em;
4800
4801 node = rb_first(&map_tree->map);
4802 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08004803 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
4804 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004805 remove_extent_mapping(map_tree, em);
4806 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01004807 if (need_resched()) {
4808 write_unlock(&map_tree->lock);
4809 cond_resched();
4810 write_lock(&map_tree->lock);
4811 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004812 }
4813 write_unlock(&map_tree->lock);
4814
4815 spin_lock(&io_tree->lock);
4816 while (!RB_EMPTY_ROOT(&io_tree->state)) {
4817 struct extent_state *state;
4818 struct extent_state *cached_state = NULL;
4819
4820 node = rb_first(&io_tree->state);
4821 state = rb_entry(node, struct extent_state, rb_node);
4822 atomic_inc(&state->refs);
4823 spin_unlock(&io_tree->lock);
4824
4825 lock_extent_bits(io_tree, state->start, state->end,
4826 0, &cached_state);
4827 clear_extent_bit(io_tree, state->start, state->end,
4828 EXTENT_LOCKED | EXTENT_DIRTY |
4829 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
4830 EXTENT_DEFRAG, 1, 1,
4831 &cached_state, GFP_NOFS);
4832 free_extent_state(state);
4833
Filipe Manana7064dd52014-08-08 02:47:05 +01004834 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004835 spin_lock(&io_tree->lock);
4836 }
4837 spin_unlock(&io_tree->lock);
4838}
4839
Al Virobd555972010-06-07 11:35:40 -04004840void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004841{
4842 struct btrfs_trans_handle *trans;
4843 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04004844 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik07127182011-08-19 10:29:59 -04004845 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04004846 int ret;
4847
liubo1abe9b82011-03-24 11:18:59 +00004848 trace_btrfs_inode_evict(inode);
4849
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004850 evict_inode_truncate_pages(inode);
4851
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02004852 if (inode->i_nlink &&
4853 ((btrfs_root_refs(&root->root_item) != 0 &&
4854 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
4855 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04004856 goto no_delete;
4857
Chris Mason39279cc2007-06-12 06:35:45 -04004858 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04004859 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004860 goto no_delete;
4861 }
Al Virobd555972010-06-07 11:35:40 -04004862 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04004863 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04004864
Miao Xief6124962014-09-12 18:44:04 +08004865 btrfs_free_io_failure_record(inode, 0, (u64)-1);
4866
Yan, Zhengc71bf092009-11-12 09:34:40 +00004867 if (root->fs_info->log_root_recovering) {
Liu Bo6bf02312012-06-25 21:59:09 -06004868 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04004869 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00004870 goto no_delete;
4871 }
4872
Yan, Zheng76dda932009-09-21 16:00:26 -04004873 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02004874 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
4875 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04004876 goto no_delete;
4877 }
4878
Miao Xie0e8c36a2012-12-19 06:59:51 +00004879 ret = btrfs_commit_inode_delayed_inode(inode);
4880 if (ret) {
4881 btrfs_orphan_del(NULL, inode);
4882 goto no_delete;
4883 }
4884
Miao Xie66d8f3d2012-09-06 04:02:28 -06004885 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04004886 if (!rsv) {
4887 btrfs_orphan_del(NULL, inode);
4888 goto no_delete;
4889 }
Josef Bacik4a338542011-08-29 11:01:31 -04004890 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04004891 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04004892 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04004893
Chris Masondbe674a2008-07-17 12:54:05 -04004894 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04004895
Josef Bacik4289a662011-08-05 13:22:24 -04004896 /*
Miao Xie8407aa42012-09-07 01:43:32 -06004897 * This is a bit simpler than btrfs_truncate since we've already
4898 * reserved our space for our orphan item in the unlink, so we just
4899 * need to reserve some slack space in case we add bytes and update
4900 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04004901 */
Yan, Zheng80825102009-11-12 09:35:36 +00004902 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00004903 ret = btrfs_block_rsv_refill(root, rsv, min_size,
4904 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00004905
Josef Bacik726c35f2011-09-26 15:46:06 -04004906 /*
4907 * Try and steal from the global reserve since we will
4908 * likely not use this space anyway, we want to try as
4909 * hard as possible to get this to work.
4910 */
4911 if (ret)
4912 ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
4913
Yan, Zhengd68fc572010-05-16 10:49:58 -04004914 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004915 btrfs_warn(root->fs_info,
4916 "Could not get space for a delete, will truncate on mount %d",
4917 ret);
Josef Bacik4289a662011-08-05 13:22:24 -04004918 btrfs_orphan_del(NULL, inode);
4919 btrfs_free_block_rsv(root, rsv);
4920 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04004921 }
4922
Miao Xie0e8c36a2012-12-19 06:59:51 +00004923 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04004924 if (IS_ERR(trans)) {
4925 btrfs_orphan_del(NULL, inode);
4926 btrfs_free_block_rsv(root, rsv);
4927 goto no_delete;
4928 }
4929
4930 trans->block_rsv = rsv;
4931
Yan, Zhengd68fc572010-05-16 10:49:58 -04004932 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04004933 if (ret != -ENOSPC)
Yan, Zheng80825102009-11-12 09:35:36 +00004934 break;
4935
Miao Xie8407aa42012-09-07 01:43:32 -06004936 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004937 btrfs_end_transaction(trans, root);
4938 trans = NULL;
Liu Bob53d3f52012-11-14 14:34:34 +00004939 btrfs_btree_balance_dirty(root);
Josef Bacik7b128762008-07-24 12:17:14 -04004940 }
4941
Josef Bacik4289a662011-08-05 13:22:24 -04004942 btrfs_free_block_rsv(root, rsv);
4943
Josef Bacik4ef31a42013-08-13 14:10:08 -04004944 /*
4945 * Errors here aren't a big deal, it just means we leave orphan items
4946 * in the tree. They will be cleaned up on the next mount.
4947 */
Yan, Zheng80825102009-11-12 09:35:36 +00004948 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04004949 trans->block_rsv = root->orphan_block_rsv;
Josef Bacik4ef31a42013-08-13 14:10:08 -04004950 btrfs_orphan_del(trans, inode);
4951 } else {
4952 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004953 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004954
Josef Bacik4289a662011-08-05 13:22:24 -04004955 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08004956 if (!(root == root->fs_info->tree_root ||
4957 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08004958 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08004959
Chris Mason54aa1f42007-06-22 14:16:25 -04004960 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004961 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04004962no_delete:
Miao Xie89042e52013-05-15 07:48:15 +00004963 btrfs_remove_delayed_node(inode);
Jan Karadbd57682012-05-03 14:48:02 +02004964 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004965 return;
Chris Mason39279cc2007-06-12 06:35:45 -04004966}
4967
4968/*
4969 * this returns the key found in the dir entry in the location pointer.
4970 * If no dir entries were found, location->objectid is 0.
4971 */
4972static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
4973 struct btrfs_key *location)
4974{
4975 const char *name = dentry->d_name.name;
4976 int namelen = dentry->d_name.len;
4977 struct btrfs_dir_item *di;
4978 struct btrfs_path *path;
4979 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04004980 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004981
4982 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07004983 if (!path)
4984 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05004985
Li Zefan33345d012011-04-20 10:31:50 +08004986 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04004987 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04004988 if (IS_ERR(di))
4989 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05004990
David Sterbac7040052011-04-19 18:00:01 +02004991 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05004992 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05004993
Chris Mason5f39d392007-10-15 16:14:19 -04004994 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04004995out:
Chris Mason39279cc2007-06-12 06:35:45 -04004996 btrfs_free_path(path);
4997 return ret;
Chris Mason39544012007-12-12 14:38:19 -05004998out_err:
4999 location->objectid = 0;
5000 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04005001}
5002
5003/*
5004 * when we hit a tree root in a directory, the btrfs part of the inode
5005 * needs to be changed to reflect the root directory of the tree root. This
5006 * is kind of like crossing a mount point.
5007 */
5008static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005009 struct inode *dir,
5010 struct dentry *dentry,
5011 struct btrfs_key *location,
5012 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005013{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005014 struct btrfs_path *path;
5015 struct btrfs_root *new_root;
5016 struct btrfs_root_ref *ref;
5017 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005018 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005019 int ret;
5020 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005021
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005022 path = btrfs_alloc_path();
5023 if (!path) {
5024 err = -ENOMEM;
5025 goto out;
5026 }
Chris Mason39279cc2007-06-12 06:35:45 -04005027
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005028 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005029 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5030 key.type = BTRFS_ROOT_REF_KEY;
5031 key.offset = location->objectid;
5032
5033 ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key, path,
5034 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005035 if (ret) {
5036 if (ret < 0)
5037 err = ret;
5038 goto out;
5039 }
Chris Mason39279cc2007-06-12 06:35:45 -04005040
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005041 leaf = path->nodes[0];
5042 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08005043 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005044 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5045 goto out;
5046
5047 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5048 (unsigned long)(ref + 1),
5049 dentry->d_name.len);
5050 if (ret)
5051 goto out;
5052
David Sterbab3b4aa72011-04-21 01:20:15 +02005053 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005054
5055 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
5056 if (IS_ERR(new_root)) {
5057 err = PTR_ERR(new_root);
5058 goto out;
5059 }
5060
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005061 *sub_root = new_root;
5062 location->objectid = btrfs_root_dirid(&new_root->root_item);
5063 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005064 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005065 err = 0;
5066out:
5067 btrfs_free_path(path);
5068 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005069}
5070
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005071static void inode_tree_add(struct inode *inode)
5072{
5073 struct btrfs_root *root = BTRFS_I(inode)->root;
5074 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005075 struct rb_node **p;
5076 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005077 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Li Zefan33345d012011-04-20 10:31:50 +08005078 u64 ino = btrfs_ino(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005079
Al Viro1d3382cb2010-10-23 15:19:20 -04005080 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005081 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005082 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005083 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005084 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005085 while (*p) {
5086 parent = *p;
5087 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5088
Li Zefan33345d012011-04-20 10:31:50 +08005089 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005090 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08005091 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005092 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005093 else {
5094 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005095 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005096 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005097 RB_CLEAR_NODE(parent);
5098 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005099 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005100 }
5101 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005102 rb_link_node(new, parent, p);
5103 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005104 spin_unlock(&root->inode_lock);
5105}
5106
5107static void inode_tree_del(struct inode *inode)
5108{
5109 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005110 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005111
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005112 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005113 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005114 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005115 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005116 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005117 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005118 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005119
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005120 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005121 synchronize_srcu(&root->fs_info->subvol_srcu);
5122 spin_lock(&root->inode_lock);
5123 empty = RB_EMPTY_ROOT(&root->inode_tree);
5124 spin_unlock(&root->inode_lock);
5125 if (empty)
5126 btrfs_add_dead_root(root);
5127 }
5128}
5129
Jeff Mahoney143bede2012-03-01 14:56:26 +01005130void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005131{
5132 struct rb_node *node;
5133 struct rb_node *prev;
5134 struct btrfs_inode *entry;
5135 struct inode *inode;
5136 u64 objectid = 0;
5137
Liu Bo7813b3d2014-02-10 17:37:25 +08005138 if (!test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
5139 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04005140
5141 spin_lock(&root->inode_lock);
5142again:
5143 node = root->inode_tree.rb_node;
5144 prev = NULL;
5145 while (node) {
5146 prev = node;
5147 entry = rb_entry(node, struct btrfs_inode, rb_node);
5148
Li Zefan33345d012011-04-20 10:31:50 +08005149 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005150 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08005151 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005152 node = node->rb_right;
5153 else
5154 break;
5155 }
5156 if (!node) {
5157 while (prev) {
5158 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08005159 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005160 node = prev;
5161 break;
5162 }
5163 prev = rb_next(prev);
5164 }
5165 }
5166 while (node) {
5167 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08005168 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04005169 inode = igrab(&entry->vfs_inode);
5170 if (inode) {
5171 spin_unlock(&root->inode_lock);
5172 if (atomic_read(&inode->i_count) > 1)
5173 d_prune_aliases(inode);
5174 /*
Al Viro45321ac2010-06-07 13:43:19 -04005175 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04005176 * the inode cache when its usage count
5177 * hits zero.
5178 */
5179 iput(inode);
5180 cond_resched();
5181 spin_lock(&root->inode_lock);
5182 goto again;
5183 }
5184
5185 if (cond_resched_lock(&root->inode_lock))
5186 goto again;
5187
5188 node = rb_next(node);
5189 }
5190 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005191}
5192
Chris Masone02119d2008-09-05 16:13:11 -04005193static int btrfs_init_locked_inode(struct inode *inode, void *p)
5194{
5195 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005196 inode->i_ino = args->location->objectid;
5197 memcpy(&BTRFS_I(inode)->location, args->location,
5198 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005199 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005200 return 0;
5201}
5202
5203static int btrfs_find_actor(struct inode *inode, void *opaque)
5204{
5205 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005206 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005207 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005208}
5209
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005210static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005211 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005212 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005213{
5214 struct inode *inode;
5215 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005216 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005217
Chris Mason90d3e592014-01-09 17:28:00 -08005218 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005219 args.root = root;
5220
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005221 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005222 btrfs_init_locked_inode,
5223 (void *)&args);
5224 return inode;
5225}
5226
Balaji Rao1a54ef82008-07-21 02:01:04 +05305227/* Get an inode object given its location and corresponding root.
5228 * Returns in *is_new if the inode was read from disk
5229 */
5230struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005231 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305232{
5233 struct inode *inode;
5234
Chris Mason90d3e592014-01-09 17:28:00 -08005235 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305236 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005237 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305238
5239 if (inode->i_state & I_NEW) {
Balaji Rao1a54ef82008-07-21 02:01:04 +05305240 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005241 if (!is_bad_inode(inode)) {
5242 inode_tree_add(inode);
5243 unlock_new_inode(inode);
5244 if (new)
5245 *new = 1;
5246 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005247 unlock_new_inode(inode);
5248 iput(inode);
5249 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005250 }
5251 }
5252
Balaji Rao1a54ef82008-07-21 02:01:04 +05305253 return inode;
5254}
5255
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005256static struct inode *new_simple_dir(struct super_block *s,
5257 struct btrfs_key *key,
5258 struct btrfs_root *root)
5259{
5260 struct inode *inode = new_inode(s);
5261
5262 if (!inode)
5263 return ERR_PTR(-ENOMEM);
5264
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005265 BTRFS_I(inode)->root = root;
5266 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005267 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005268
5269 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005270 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005271 inode->i_fop = &simple_dir_operations;
5272 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
chandan r9cc97d62012-07-04 12:48:07 +05305273 inode->i_mtime = CURRENT_TIME;
5274 inode->i_atime = inode->i_mtime;
5275 inode->i_ctime = inode->i_mtime;
5276 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005277
5278 return inode;
5279}
5280
Chris Mason3de45862008-11-17 21:02:50 -05005281struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005282{
Chris Masond3977122009-01-05 21:25:51 -05005283 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005284 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005285 struct btrfs_root *sub_root = root;
5286 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005287 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005288 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005289
5290 if (dentry->d_name.len > BTRFS_NAME_LEN)
5291 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005292
Jeff Layton39e3c952012-11-28 11:30:53 -05005293 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005294 if (ret < 0)
5295 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005296
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005297 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005298 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005299
5300 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005301 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005302 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005303 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005304
5305 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5306
Yan, Zheng76dda932009-09-21 16:00:26 -04005307 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005308 ret = fixup_tree_root_location(root, dir, dentry,
5309 &location, &sub_root);
5310 if (ret < 0) {
5311 if (ret != -ENOENT)
5312 inode = ERR_PTR(ret);
5313 else
5314 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5315 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005316 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005317 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005318 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
5319
Julia Lawall34d19ba2011-01-24 19:55:19 +00005320 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00005321 down_read(&root->fs_info->cleanup_work_sem);
5322 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005323 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005324 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005325 if (ret) {
5326 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005327 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005328 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005329 }
5330
Chris Mason3de45862008-11-17 21:02:50 -05005331 return inode;
5332}
5333
Nick Pigginfe15ce42011-01-07 17:49:23 +11005334static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005335{
5336 struct btrfs_root *root;
Li Zefan848cce02012-02-21 17:04:28 +08005337 struct inode *inode = dentry->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005338
Li Zefan848cce02012-02-21 17:04:28 +08005339 if (!inode && !IS_ROOT(dentry))
5340 inode = dentry->d_parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005341
Li Zefan848cce02012-02-21 17:04:28 +08005342 if (inode) {
5343 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005344 if (btrfs_root_refs(&root->root_item) == 0)
5345 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005346
5347 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5348 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005349 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005350 return 0;
5351}
5352
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005353static void btrfs_dentry_release(struct dentry *dentry)
5354{
Daeseok Youn944a4512014-04-14 15:37:02 +09005355 kfree(dentry->d_fsdata);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005356}
5357
Chris Mason3de45862008-11-17 21:02:50 -05005358static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005359 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005360{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005361 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005362
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005363 inode = btrfs_lookup_dentry(dir, dentry);
5364 if (IS_ERR(inode)) {
5365 if (PTR_ERR(inode) == -ENOENT)
5366 inode = NULL;
5367 else
5368 return ERR_CAST(inode);
5369 }
5370
Al Viro41d28bc2014-10-12 22:24:21 -04005371 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005372}
5373
Miao Xie16cdcec2011-04-22 18:12:22 +08005374unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005375 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5376};
5377
Al Viro9cdda8d2013-05-22 16:48:09 -04005378static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005379{
Al Viro9cdda8d2013-05-22 16:48:09 -04005380 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005381 struct btrfs_root *root = BTRFS_I(inode)->root;
5382 struct btrfs_item *item;
5383 struct btrfs_dir_item *di;
5384 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005385 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005386 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005387 struct list_head ins_list;
5388 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005389 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005390 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005391 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005392 unsigned char d_type;
5393 int over = 0;
5394 u32 di_cur;
5395 u32 di_total;
5396 u32 di_len;
5397 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005398 char tmp_name[32];
5399 char *name_ptr;
5400 int name_len;
Al Viro9cdda8d2013-05-22 16:48:09 -04005401 int is_curr = 0; /* ctx->pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04005402
5403 /* FIXME, use a real flag for deciding about the key type */
5404 if (root->fs_info->tree_root == root)
5405 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005406
Al Viro9cdda8d2013-05-22 16:48:09 -04005407 if (!dir_emit_dots(file, ctx))
5408 return 0;
5409
David Woodhouse49593bf2008-08-17 17:08:36 +01005410 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005411 if (!path)
5412 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005413
Josef Bacik026fd312011-05-13 10:32:11 -04005414 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005415
Miao Xie16cdcec2011-04-22 18:12:22 +08005416 if (key_type == BTRFS_DIR_INDEX_KEY) {
5417 INIT_LIST_HEAD(&ins_list);
5418 INIT_LIST_HEAD(&del_list);
5419 btrfs_get_delayed_items(inode, &ins_list, &del_list);
5420 }
5421
David Sterba962a2982014-06-04 18:41:45 +02005422 key.type = key_type;
Al Viro9cdda8d2013-05-22 16:48:09 -04005423 key.offset = ctx->pos;
Li Zefan33345d012011-04-20 10:31:50 +08005424 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005425
Chris Mason39279cc2007-06-12 06:35:45 -04005426 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5427 if (ret < 0)
5428 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005429
5430 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005431 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005432 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005433 if (slot >= btrfs_header_nritems(leaf)) {
5434 ret = btrfs_next_leaf(root, path);
5435 if (ret < 0)
5436 goto err;
5437 else if (ret > 0)
5438 break;
5439 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005440 }
Chris Mason3de45862008-11-17 21:02:50 -05005441
Ross Kirkdd3cc162013-09-16 15:58:09 +01005442 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04005443 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5444
5445 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005446 break;
David Sterba962a2982014-06-04 18:41:45 +02005447 if (found_key.type != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04005448 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005449 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005450 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08005451 if (key_type == BTRFS_DIR_INDEX_KEY &&
5452 btrfs_should_delete_dir_index(&del_list,
5453 found_key.offset))
5454 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005455
Al Viro9cdda8d2013-05-22 16:48:09 -04005456 ctx->pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08005457 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005458
Chris Mason39279cc2007-06-12 06:35:45 -04005459 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
5460 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005461 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01005462
5463 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04005464 struct btrfs_key location;
5465
Josef Bacik22a94d42011-03-16 16:47:17 -04005466 if (verify_dir_item(root, leaf, di))
5467 break;
5468
Chris Mason5f39d392007-10-15 16:14:19 -04005469 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01005470 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04005471 name_ptr = tmp_name;
5472 } else {
5473 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01005474 if (!name_ptr) {
5475 ret = -ENOMEM;
5476 goto err;
5477 }
Chris Mason5f39d392007-10-15 16:14:19 -04005478 }
5479 read_extent_buffer(leaf, name_ptr,
5480 (unsigned long)(di + 1), name_len);
5481
5482 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5483 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05005484
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005485
Chris Mason3de45862008-11-17 21:02:50 -05005486 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01005487 * skip it.
5488 *
5489 * In contrast to old kernels, we insert the snapshot's
5490 * dir item and dir index after it has been created, so
5491 * we won't find a reference to our own snapshot. We
5492 * still keep the following code for backward
5493 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05005494 */
5495 if (location.type == BTRFS_ROOT_ITEM_KEY &&
5496 location.objectid == root->root_key.objectid) {
5497 over = 0;
5498 goto skip;
5499 }
Al Viro9cdda8d2013-05-22 16:48:09 -04005500 over = !dir_emit(ctx, name_ptr, name_len,
5501 location.objectid, d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04005502
Chris Mason3de45862008-11-17 21:02:50 -05005503skip:
Chris Mason5f39d392007-10-15 16:14:19 -04005504 if (name_ptr != tmp_name)
5505 kfree(name_ptr);
5506
Chris Mason39279cc2007-06-12 06:35:45 -04005507 if (over)
5508 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05005509 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01005510 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04005511 di_cur += di_len;
5512 di = (struct btrfs_dir_item *)((char *)di + di_len);
5513 }
Li Zefanb9e03af2011-03-23 10:43:58 +08005514next:
5515 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005516 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005517
Miao Xie16cdcec2011-04-22 18:12:22 +08005518 if (key_type == BTRFS_DIR_INDEX_KEY) {
5519 if (is_curr)
Al Viro9cdda8d2013-05-22 16:48:09 -04005520 ctx->pos++;
5521 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005522 if (ret)
5523 goto nopos;
5524 }
5525
David Woodhouse49593bf2008-08-17 17:08:36 +01005526 /* Reached end of directory/root. Bump pos past the last item. */
Zach Browndb62efb2013-07-11 16:19:42 -07005527 ctx->pos++;
5528
5529 /*
5530 * Stop new entries from being returned after we return the last
5531 * entry.
5532 *
5533 * New directory entries are assigned a strictly increasing
5534 * offset. This means that new entries created during readdir
5535 * are *guaranteed* to be seen in the future by that readdir.
5536 * This has broken buggy programs which operate on names as
5537 * they're returned by readdir. Until we re-use freed offsets
5538 * we have this hack to stop new entries from being returned
5539 * under the assumption that they'll never reach this huge
5540 * offset.
5541 *
5542 * This is being careful not to overflow 32bit loff_t unless the
5543 * last entry requires it because doing so has broken 32bit apps
5544 * in the past.
5545 */
5546 if (key_type == BTRFS_DIR_INDEX_KEY) {
5547 if (ctx->pos >= INT_MAX)
5548 ctx->pos = LLONG_MAX;
5549 else
5550 ctx->pos = INT_MAX;
5551 }
Chris Mason39279cc2007-06-12 06:35:45 -04005552nopos:
5553 ret = 0;
5554err:
Miao Xie16cdcec2011-04-22 18:12:22 +08005555 if (key_type == BTRFS_DIR_INDEX_KEY)
5556 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005557 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005558 return ret;
5559}
5560
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005561int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005562{
5563 struct btrfs_root *root = BTRFS_I(inode)->root;
5564 struct btrfs_trans_handle *trans;
5565 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005566 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005567
Josef Bacik72ac3c02012-05-23 14:13:11 -04005568 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005569 return 0;
5570
Liu Bo83eea1f2012-07-10 05:28:39 -06005571 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08005572 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005573
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005574 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005575 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005576 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005577 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005578 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005579 if (IS_ERR(trans))
5580 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06005581 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005582 }
5583 return ret;
5584}
5585
5586/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005587 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005588 * inode changes. But, it is most likely to find the inode in cache.
5589 * FIXME, needs more benchmarking...there are no reasons other than performance
5590 * to keep or drop this code.
5591 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005592static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005593{
5594 struct btrfs_root *root = BTRFS_I(inode)->root;
5595 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005596 int ret;
5597
Josef Bacik72ac3c02012-05-23 14:13:11 -04005598 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005599 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005600
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005601 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005602 if (IS_ERR(trans))
5603 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005604
5605 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005606 if (ret && ret == -ENOSPC) {
5607 /* whoops, lets try again with the full transaction */
5608 btrfs_end_transaction(trans, root);
5609 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005610 if (IS_ERR(trans))
5611 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005612
Chris Mason94b60442010-05-26 11:02:00 -04005613 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005614 }
Chris Mason39279cc2007-06-12 06:35:45 -04005615 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08005616 if (BTRFS_I(inode)->delayed_node)
5617 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005618
5619 return ret;
5620}
5621
5622/*
5623 * This is a copy of file_update_time. We need this so we can return error on
5624 * ENOSPC for updating the inode in the case of file write and mmap writes.
5625 */
Josef Bacike41f9412012-03-26 09:46:47 -04005626static int btrfs_update_time(struct inode *inode, struct timespec *now,
5627 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005628{
Alexander Block2bc5565282012-06-15 09:49:33 +02005629 struct btrfs_root *root = BTRFS_I(inode)->root;
5630
5631 if (btrfs_root_readonly(root))
5632 return -EROFS;
5633
Josef Bacike41f9412012-03-26 09:46:47 -04005634 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005635 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04005636 if (flags & S_CTIME)
5637 inode->i_ctime = *now;
5638 if (flags & S_MTIME)
5639 inode->i_mtime = *now;
5640 if (flags & S_ATIME)
5641 inode->i_atime = *now;
5642 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005643}
5644
Chris Masond352ac62008-09-29 15:18:18 -04005645/*
5646 * find the highest existing sequence number in a directory
5647 * and then set the in-memory index_cnt variable to reflect
5648 * free sequence numbers
5649 */
Josef Bacikaec74772008-07-24 12:12:38 -04005650static int btrfs_set_inode_index_count(struct inode *inode)
5651{
5652 struct btrfs_root *root = BTRFS_I(inode)->root;
5653 struct btrfs_key key, found_key;
5654 struct btrfs_path *path;
5655 struct extent_buffer *leaf;
5656 int ret;
5657
Li Zefan33345d012011-04-20 10:31:50 +08005658 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02005659 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04005660 key.offset = (u64)-1;
5661
5662 path = btrfs_alloc_path();
5663 if (!path)
5664 return -ENOMEM;
5665
5666 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5667 if (ret < 0)
5668 goto out;
5669 /* FIXME: we should be able to handle this */
5670 if (ret == 0)
5671 goto out;
5672 ret = 0;
5673
5674 /*
5675 * MAGIC NUMBER EXPLANATION:
5676 * since we search a directory based on f_pos we have to start at 2
5677 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5678 * else has to start at 2
5679 */
5680 if (path->slots[0] == 0) {
5681 BTRFS_I(inode)->index_cnt = 2;
5682 goto out;
5683 }
5684
5685 path->slots[0]--;
5686
5687 leaf = path->nodes[0];
5688 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5689
Li Zefan33345d012011-04-20 10:31:50 +08005690 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02005691 found_key.type != BTRFS_DIR_INDEX_KEY) {
Josef Bacikaec74772008-07-24 12:12:38 -04005692 BTRFS_I(inode)->index_cnt = 2;
5693 goto out;
5694 }
5695
5696 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
5697out:
5698 btrfs_free_path(path);
5699 return ret;
5700}
5701
Chris Masond352ac62008-09-29 15:18:18 -04005702/*
5703 * helper to find a free sequence number in a given directory. This current
5704 * code is very simple, later versions will do smarter things in the btree
5705 */
Chris Mason3de45862008-11-17 21:02:50 -05005706int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04005707{
5708 int ret = 0;
5709
5710 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08005711 ret = btrfs_inode_delayed_dir_index_count(dir);
5712 if (ret) {
5713 ret = btrfs_set_inode_index_count(dir);
5714 if (ret)
5715 return ret;
5716 }
Josef Bacikaec74772008-07-24 12:12:38 -04005717 }
5718
Chris Mason00e4e6b2008-08-05 11:18:09 -04005719 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04005720 BTRFS_I(dir)->index_cnt++;
5721
5722 return ret;
5723}
5724
Chris Masonb0d5d102014-09-08 13:08:51 -07005725static int btrfs_insert_inode_locked(struct inode *inode)
5726{
5727 struct btrfs_iget_args args;
5728 args.location = &BTRFS_I(inode)->location;
5729 args.root = BTRFS_I(inode)->root;
5730
5731 return insert_inode_locked4(inode,
5732 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
5733 btrfs_find_actor, &args);
5734}
5735
Chris Mason39279cc2007-06-12 06:35:45 -04005736static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5737 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04005738 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05005739 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04005740 u64 ref_objectid, u64 objectid,
5741 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04005742{
5743 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005744 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04005745 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04005746 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05005747 struct btrfs_inode_ref *ref;
5748 struct btrfs_key key[2];
5749 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005750 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05005751 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04005752 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005753
Chris Mason5f39d392007-10-15 16:14:19 -04005754 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005755 if (!path)
5756 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04005757
Chris Mason39279cc2007-06-12 06:35:45 -04005758 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005759 if (!inode) {
5760 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005761 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005762 }
Chris Mason39279cc2007-06-12 06:35:45 -04005763
Li Zefan581bb052011-04-20 10:06:11 +08005764 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01005765 * O_TMPFILE, set link count to 0, so that after this point,
5766 * we fill in an inode item with the correct link count.
5767 */
5768 if (!name)
5769 set_nlink(inode, 0);
5770
5771 /*
Li Zefan581bb052011-04-20 10:06:11 +08005772 * we have to initialize this early, so we can reclaim the inode
5773 * number if we fail afterwards in this function.
5774 */
5775 inode->i_ino = objectid;
5776
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005777 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00005778 trace_btrfs_inode_request(dir);
5779
Chris Mason3de45862008-11-17 21:02:50 -05005780 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04005781 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005782 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005783 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005784 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04005785 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005786 } else if (dir) {
5787 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04005788 }
5789 /*
5790 * index_cnt is ignored for everything but a dir,
5791 * btrfs_get_inode_index_count has an explanation for the magic
5792 * number
5793 */
5794 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08005795 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04005796 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04005797 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00005798 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04005799
Josef Bacik5dc562c2012-08-17 13:14:17 -04005800 /*
5801 * We could have gotten an inode number from somebody who was fsynced
5802 * and then removed in this same transaction, so let's just set full
5803 * sync since it will be a full sync anyway and this will blow away the
5804 * old info in the log.
5805 */
5806 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
5807
Chris Mason9c583092008-01-29 15:15:18 -05005808 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02005809 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05005810 key[0].offset = 0;
5811
Chris Mason9c583092008-01-29 15:15:18 -05005812 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005813
5814 if (name) {
5815 /*
5816 * Start new inodes with an inode_ref. This is slightly more
5817 * efficient for small numbers of hard links since they will
5818 * be packed into one item. Extended refs will kick in if we
5819 * add more hard links than can fit in the ref item.
5820 */
5821 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02005822 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005823 key[1].offset = ref_objectid;
5824
5825 sizes[1] = name_len + sizeof(*ref);
5826 }
Chris Mason9c583092008-01-29 15:15:18 -05005827
Chris Masonb0d5d102014-09-08 13:08:51 -07005828 location = &BTRFS_I(inode)->location;
5829 location->objectid = objectid;
5830 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02005831 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07005832
5833 ret = btrfs_insert_inode_locked(inode);
5834 if (ret < 0)
5835 goto fail;
5836
Chris Masonb9473432009-03-13 11:00:37 -04005837 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005838 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05005839 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07005840 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04005841
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03005842 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04005843 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05305844
5845 inode->i_mtime = CURRENT_TIME;
5846 inode->i_atime = inode->i_mtime;
5847 inode->i_ctime = inode->i_mtime;
5848 BTRFS_I(inode)->i_otime = inode->i_mtime;
5849
Chris Mason5f39d392007-10-15 16:14:19 -04005850 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
5851 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06005852 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
5853 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04005854 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05005855
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005856 if (name) {
5857 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
5858 struct btrfs_inode_ref);
5859 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
5860 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
5861 ptr = (unsigned long)(ref + 1);
5862 write_extent_buffer(path->nodes[0], name, ptr, name_len);
5863 }
Chris Mason9c583092008-01-29 15:15:18 -05005864
Chris Mason5f39d392007-10-15 16:14:19 -04005865 btrfs_mark_buffer_dirty(path->nodes[0]);
5866 btrfs_free_path(path);
5867
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005868 btrfs_inherit_iflags(inode, dir);
5869
Al Viro569254b2011-07-24 17:08:40 -04005870 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04005871 if (btrfs_test_opt(root, NODATASUM))
5872 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo213490b2012-09-11 08:33:50 -06005873 if (btrfs_test_opt(root, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05005874 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
5875 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04005876 }
5877
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005878 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00005879
5880 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04005881 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00005882
Alexander Block8ea05e32012-07-25 17:35:53 +02005883 btrfs_update_root_times(trans, root);
5884
Filipe David Borba Manana63541922014-01-07 11:47:46 +00005885 ret = btrfs_inode_inherit_props(trans, inode, dir);
5886 if (ret)
5887 btrfs_err(root->fs_info,
5888 "error inheriting props for ino %llu (root %llu): %d",
5889 btrfs_ino(inode), root->root_key.objectid, ret);
5890
Chris Mason39279cc2007-06-12 06:35:45 -04005891 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07005892
5893fail_unlock:
5894 unlock_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005895fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005896 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04005897 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04005898 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005899 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005900 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005901}
5902
5903static inline u8 btrfs_inode_type(struct inode *inode)
5904{
5905 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
5906}
5907
Chris Masond352ac62008-09-29 15:18:18 -04005908/*
5909 * utility function to add 'inode' into 'parent_inode' with
5910 * a give name and a given sequence number.
5911 * if 'add_backref' is true, also insert a backref from the
5912 * inode to the parent directory.
5913 */
Chris Masone02119d2008-09-05 16:13:11 -04005914int btrfs_add_link(struct btrfs_trans_handle *trans,
5915 struct inode *parent_inode, struct inode *inode,
5916 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005917{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005918 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005919 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04005920 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08005921 u64 ino = btrfs_ino(inode);
5922 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005923
Li Zefan33345d012011-04-20 10:31:50 +08005924 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005925 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
5926 } else {
Li Zefan33345d012011-04-20 10:31:50 +08005927 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02005928 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005929 key.offset = 0;
5930 }
Chris Mason39279cc2007-06-12 06:35:45 -04005931
Li Zefan33345d012011-04-20 10:31:50 +08005932 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005933 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
5934 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08005935 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005936 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08005937 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
5938 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005939 }
5940
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005941 /* Nothing to clean up yet */
5942 if (ret)
5943 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005944
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005945 ret = btrfs_insert_dir_item(trans, root, name, name_len,
5946 parent_inode, &key,
5947 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05005948 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005949 goto fail_dir_item;
5950 else if (ret) {
5951 btrfs_abort_transaction(trans, root, ret);
5952 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005953 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005954
5955 btrfs_i_size_write(parent_inode, parent_inode->i_size +
5956 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005957 inode_inc_iversion(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005958 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
5959 ret = btrfs_update_inode(trans, root, parent_inode);
5960 if (ret)
5961 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005962 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05005963
5964fail_dir_item:
5965 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
5966 u64 local_index;
5967 int err;
5968 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
5969 key.objectid, root->root_key.objectid,
5970 parent_ino, &local_index, name, name_len);
5971
5972 } else if (add_backref) {
5973 u64 local_index;
5974 int err;
5975
5976 err = btrfs_del_inode_ref(trans, root, name, name_len,
5977 ino, parent_ino, &local_index);
5978 }
5979 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005980}
5981
5982static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00005983 struct inode *dir, struct dentry *dentry,
5984 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005985{
Josef Bacika1b075d2010-11-19 20:36:11 +00005986 int err = btrfs_add_link(trans, dir, inode,
5987 dentry->d_name.name, dentry->d_name.len,
5988 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005989 if (err > 0)
5990 err = -EEXIST;
5991 return err;
5992}
5993
Josef Bacik618e21d2007-07-11 10:18:17 -04005994static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04005995 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04005996{
5997 struct btrfs_trans_handle *trans;
5998 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005999 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006000 int err;
6001 int drop_inode = 0;
6002 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006003 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006004
6005 if (!new_valid_dev(rdev))
6006 return -EINVAL;
6007
Josef Bacik9ed74f22009-09-11 16:12:44 -04006008 /*
6009 * 2 for inode item and ref
6010 * 2 for dir items
6011 * 1 for xattr if selinux is on
6012 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006013 trans = btrfs_start_transaction(root, 5);
6014 if (IS_ERR(trans))
6015 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006016
Li Zefan581bb052011-04-20 10:06:11 +08006017 err = btrfs_find_free_ino(root, &objectid);
6018 if (err)
6019 goto out_unlock;
6020
Josef Bacikaec74772008-07-24 12:12:38 -04006021 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006022 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04006023 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006024 if (IS_ERR(inode)) {
6025 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006026 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006027 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006028
Casey Schauflerad19db72011-12-15 10:09:07 -05006029 /*
6030 * If the active LSM wants to access the inode during
6031 * d_instantiate it needs these. Smack checks to see
6032 * if the filesystem supports xattrs by looking at the
6033 * ops vector.
6034 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006035 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006036 init_special_inode(inode, inode->i_mode, rdev);
6037
6038 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006039 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006040 goto out_unlock_inode;
6041
6042 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
6043 if (err) {
6044 goto out_unlock_inode;
6045 } else {
Yan1b4ab1b2007-08-29 09:11:44 -04006046 btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006047 unlock_new_inode(inode);
Al Viro08c422c2011-12-23 07:58:13 -05006048 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006049 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006050
Josef Bacik618e21d2007-07-11 10:18:17 -04006051out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006052 btrfs_end_transaction(trans, root);
Josef Bacikc581afc2014-02-06 16:06:06 -05006053 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006054 btrfs_btree_balance_dirty(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04006055 if (drop_inode) {
6056 inode_dec_link_count(inode);
6057 iput(inode);
6058 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006059 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006060
6061out_unlock_inode:
6062 drop_inode = 1;
6063 unlock_new_inode(inode);
6064 goto out_unlock;
6065
Josef Bacik618e21d2007-07-11 10:18:17 -04006066}
6067
Chris Mason39279cc2007-06-12 06:35:45 -04006068static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006069 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006070{
6071 struct btrfs_trans_handle *trans;
6072 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006073 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006074 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006075 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006076 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006077 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006078
Josef Bacik9ed74f22009-09-11 16:12:44 -04006079 /*
6080 * 2 for inode item and ref
6081 * 2 for dir items
6082 * 1 for xattr if selinux is on
6083 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006084 trans = btrfs_start_transaction(root, 5);
6085 if (IS_ERR(trans))
6086 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006087
Li Zefan581bb052011-04-20 10:06:11 +08006088 err = btrfs_find_free_ino(root, &objectid);
6089 if (err)
6090 goto out_unlock;
6091
Josef Bacikaec74772008-07-24 12:12:38 -04006092 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006093 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04006094 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006095 if (IS_ERR(inode)) {
6096 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006097 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006098 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006099 drop_inode_on_err = 1;
Casey Schauflerad19db72011-12-15 10:09:07 -05006100 /*
6101 * If the active LSM wants to access the inode during
6102 * d_instantiate it needs these. Smack checks to see
6103 * if the filesystem supports xattrs by looking at the
6104 * ops vector.
6105 */
6106 inode->i_fop = &btrfs_file_operations;
6107 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006108 inode->i_mapping->a_ops = &btrfs_aops;
6109 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
6110
6111 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6112 if (err)
6113 goto out_unlock_inode;
6114
6115 err = btrfs_update_inode(trans, root, inode);
6116 if (err)
6117 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05006118
Josef Bacika1b075d2010-11-19 20:36:11 +00006119 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006120 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006121 goto out_unlock_inode;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006122
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006123 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006124 unlock_new_inode(inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006125 d_instantiate(dentry, inode);
6126
Chris Mason39279cc2007-06-12 06:35:45 -04006127out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006128 btrfs_end_transaction(trans, root);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006129 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006130 inode_dec_link_count(inode);
6131 iput(inode);
6132 }
Josef Bacikc581afc2014-02-06 16:06:06 -05006133 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006134 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006135 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006136
6137out_unlock_inode:
6138 unlock_new_inode(inode);
6139 goto out_unlock;
6140
Chris Mason39279cc2007-06-12 06:35:45 -04006141}
6142
6143static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6144 struct dentry *dentry)
6145{
6146 struct btrfs_trans_handle *trans;
6147 struct btrfs_root *root = BTRFS_I(dir)->root;
6148 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006149 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006150 int err;
6151 int drop_inode = 0;
6152
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006153 /* do not allow sys_link's with other subvols of the same device */
6154 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006155 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006156
Mark Fashehf1863732012-08-08 11:32:27 -07006157 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006158 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006159
Chris Mason3de45862008-11-17 21:02:50 -05006160 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006161 if (err)
6162 goto fail;
6163
Yan, Zhenga22285a2010-05-16 10:48:46 -04006164 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006165 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006166 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006167 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04006168 */
Miao Xie7e6b6462011-02-18 09:21:17 +00006169 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006170 if (IS_ERR(trans)) {
6171 err = PTR_ERR(trans);
6172 goto fail;
6173 }
Chris Mason5f39d392007-10-15 16:14:19 -04006174
Miao Xie67de1172013-12-26 13:07:06 +08006175 /* There are several dir indexes for this inode, clear the cache. */
6176 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006177 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006178 inode_inc_iversion(inode);
Miao Xie31534952011-04-13 13:19:21 +08006179 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6ee2010-10-23 11:11:40 -04006180 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006181 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006182
Josef Bacika1b075d2010-11-19 20:36:11 +00006183 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006184
Yan, Zhenga5719522009-09-24 09:17:31 -04006185 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006186 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006187 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006188 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006189 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006190 if (err)
6191 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006192 if (inode->i_nlink == 1) {
6193 /*
6194 * If new hard link count is 1, it's a file created
6195 * with open(2) O_TMPFILE flag.
6196 */
6197 err = btrfs_orphan_del(trans, inode);
6198 if (err)
6199 goto fail;
6200 }
Al Viro08c422c2011-12-23 07:58:13 -05006201 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00006202 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006203 }
Chris Mason39279cc2007-06-12 06:35:45 -04006204
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006205 btrfs_end_transaction(trans, root);
Josef Bacikc581afc2014-02-06 16:06:06 -05006206 btrfs_balance_delayed_items(root);
Chris Mason1832a6d2007-12-21 16:27:21 -05006207fail:
Chris Mason39279cc2007-06-12 06:35:45 -04006208 if (drop_inode) {
6209 inode_dec_link_count(inode);
6210 iput(inode);
6211 }
Liu Bob53d3f52012-11-14 14:34:34 +00006212 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006213 return err;
6214}
6215
Al Viro18bb1db2011-07-26 01:41:39 -04006216static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006217{
Chris Masonb9d86662008-05-02 16:13:49 -04006218 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006219 struct btrfs_trans_handle *trans;
6220 struct btrfs_root *root = BTRFS_I(dir)->root;
6221 int err = 0;
6222 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006223 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006224 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006225
Josef Bacik9ed74f22009-09-11 16:12:44 -04006226 /*
6227 * 2 items for inode and ref
6228 * 2 items for dir items
6229 * 1 for xattr if selinux is on
6230 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006231 trans = btrfs_start_transaction(root, 5);
6232 if (IS_ERR(trans))
6233 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006234
Li Zefan581bb052011-04-20 10:06:11 +08006235 err = btrfs_find_free_ino(root, &objectid);
6236 if (err)
6237 goto out_fail;
6238
Josef Bacikaec74772008-07-24 12:12:38 -04006239 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006240 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04006241 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006242 if (IS_ERR(inode)) {
6243 err = PTR_ERR(inode);
6244 goto out_fail;
6245 }
Chris Mason5f39d392007-10-15 16:14:19 -04006246
Chris Mason39279cc2007-06-12 06:35:45 -04006247 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006248 /* these must be set before we unlock the inode */
6249 inode->i_op = &btrfs_dir_inode_operations;
6250 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006251
Eric Paris2a7dba32011-02-01 11:05:39 -05006252 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006253 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006254 goto out_fail_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006255
Chris Masondbe674a2008-07-17 12:54:05 -04006256 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006257 err = btrfs_update_inode(trans, root, inode);
6258 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006259 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006260
Josef Bacika1b075d2010-11-19 20:36:11 +00006261 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
6262 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006263 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006264 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006265
Chris Mason39279cc2007-06-12 06:35:45 -04006266 d_instantiate(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006267 /*
6268 * mkdir is special. We're unlocking after we call d_instantiate
6269 * to avoid a race with nfsd calling d_instantiate.
6270 */
6271 unlock_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006272 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006273
6274out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006275 btrfs_end_transaction(trans, root);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006276 if (drop_on_err) {
6277 inode_dec_link_count(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006278 iput(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006279 }
Josef Bacikc581afc2014-02-06 16:06:06 -05006280 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006281 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006282 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006283
6284out_fail_inode:
6285 unlock_new_inode(inode);
6286 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006287}
6288
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006289/* Find next extent map of a given extent map, caller needs to ensure locks */
6290static struct extent_map *next_extent_map(struct extent_map *em)
6291{
6292 struct rb_node *next;
6293
6294 next = rb_next(&em->rb_node);
6295 if (!next)
6296 return NULL;
6297 return container_of(next, struct extent_map, rb_node);
6298}
6299
6300static struct extent_map *prev_extent_map(struct extent_map *em)
6301{
6302 struct rb_node *prev;
6303
6304 prev = rb_prev(&em->rb_node);
6305 if (!prev)
6306 return NULL;
6307 return container_of(prev, struct extent_map, rb_node);
6308}
6309
Chris Masond352ac62008-09-29 15:18:18 -04006310/* helper for btfs_get_extent. Given an existing extent in the tree,
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006311 * the existing extent is the nearest extent to map_start,
Chris Masond352ac62008-09-29 15:18:18 -04006312 * and an extent that you want to insert, deal with overlap and insert
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006313 * the best fitted new extent into the tree.
Chris Masond352ac62008-09-29 15:18:18 -04006314 */
Chris Mason3b951512008-04-17 11:29:12 -04006315static int merge_extent_mapping(struct extent_map_tree *em_tree,
6316 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006317 struct extent_map *em,
Qu Wenruo51f395a2014-08-08 13:06:20 +08006318 u64 map_start)
Chris Mason3b951512008-04-17 11:29:12 -04006319{
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006320 struct extent_map *prev;
6321 struct extent_map *next;
6322 u64 start;
6323 u64 end;
Chris Mason3b951512008-04-17 11:29:12 -04006324 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04006325
Chris Masone6dcd2d2008-07-17 12:53:50 -04006326 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006327
6328 if (existing->start > map_start) {
6329 next = existing;
6330 prev = prev_extent_map(next);
6331 } else {
6332 prev = existing;
6333 next = next_extent_map(prev);
6334 }
6335
6336 start = prev ? extent_map_end(prev) : em->start;
6337 start = max_t(u64, start, em->start);
6338 end = next ? next->start : extent_map_end(em);
6339 end = min_t(u64, end, extent_map_end(em));
6340 start_diff = start - em->start;
6341 em->start = start;
6342 em->len = end - start;
Chris Masonc8b97812008-10-29 14:49:59 -04006343 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
6344 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006345 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04006346 em->block_len -= start_diff;
6347 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006348 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006349}
6350
Chris Masonc8b97812008-10-29 14:49:59 -04006351static noinline int uncompress_inline(struct btrfs_path *path,
6352 struct inode *inode, struct page *page,
6353 size_t pg_offset, u64 extent_offset,
6354 struct btrfs_file_extent_item *item)
6355{
6356 int ret;
6357 struct extent_buffer *leaf = path->nodes[0];
6358 char *tmp;
6359 size_t max_size;
6360 unsigned long inline_size;
6361 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006362 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006363
6364 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006365 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006366 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6367 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006368 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006369 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006370 if (!tmp)
6371 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006372 ptr = btrfs_file_extent_inline_start(item);
6373
6374 read_extent_buffer(leaf, tmp, ptr, inline_size);
6375
Chris Mason5b050f02008-11-11 09:34:41 -05006376 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006377 ret = btrfs_decompress(compress_type, tmp, page,
6378 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006379 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006380 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006381}
6382
Chris Masond352ac62008-09-29 15:18:18 -04006383/*
6384 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006385 * the ugly parts come from merging extents from the disk with the in-ram
6386 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006387 * where the in-ram extents might be locked pending data=ordered completion.
6388 *
6389 * This also copies inline extents directly into the page.
6390 */
Chris Masond3977122009-01-05 21:25:51 -05006391
Chris Masona52d9a82007-08-27 16:49:44 -04006392struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05006393 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04006394 int create)
6395{
6396 int ret;
6397 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006398 u64 extent_start = 0;
6399 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08006400 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006401 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006402 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04006403 struct btrfs_root *root = BTRFS_I(inode)->root;
6404 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006405 struct extent_buffer *leaf;
6406 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006407 struct extent_map *em = NULL;
6408 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05006409 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006410 struct btrfs_trans_handle *trans = NULL;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006411 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006412
Chris Masona52d9a82007-08-27 16:49:44 -04006413again:
Chris Mason890871b2009-09-02 16:24:52 -04006414 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006415 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006416 if (em)
6417 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006418 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006419
Chris Masona52d9a82007-08-27 16:49:44 -04006420 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006421 if (em->start > start || em->start + em->len <= start)
6422 free_extent_map(em);
6423 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006424 free_extent_map(em);
6425 else
6426 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006427 }
David Sterba172ddd62011-04-21 00:48:27 +02006428 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006429 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006430 err = -ENOMEM;
6431 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006432 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006433 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006434 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006435 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006436 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006437 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006438
6439 if (!path) {
6440 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006441 if (!path) {
6442 err = -ENOMEM;
6443 goto out;
6444 }
6445 /*
6446 * Chances are we'll be called again, so go ahead and do
6447 * readahead
6448 */
6449 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04006450 }
6451
Chris Mason179e29e2007-11-01 11:28:41 -04006452 ret = btrfs_lookup_file_extent(trans, root, path,
6453 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006454 if (ret < 0) {
6455 err = ret;
6456 goto out;
6457 }
6458
6459 if (ret != 0) {
6460 if (path->slots[0] == 0)
6461 goto not_found;
6462 path->slots[0]--;
6463 }
6464
Chris Mason5f39d392007-10-15 16:14:19 -04006465 leaf = path->nodes[0];
6466 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006467 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006468 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006469 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02006470 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04006471 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006472 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006473 /*
6474 * If we backup past the first extent we want to move forward
6475 * and see if there is an extent in front of us, otherwise we'll
6476 * say there is a hole for our whole search range which can
6477 * cause problems.
6478 */
6479 extent_end = start;
6480 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006481 }
6482
Chris Mason5f39d392007-10-15 16:14:19 -04006483 found_type = btrfs_file_extent_type(leaf, item);
6484 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04006485 if (found_type == BTRFS_FILE_EXTENT_REG ||
6486 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006487 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006488 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006489 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6490 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08006491 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Qu Wenruofda28322013-02-26 08:10:22 +00006492 extent_end = ALIGN(extent_start + size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04006493 }
Josef Bacik25a50342013-10-14 12:08:38 -04006494next:
Yan Zheng9036c102008-10-30 14:19:41 -04006495 if (start >= extent_end) {
6496 path->slots[0]++;
6497 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6498 ret = btrfs_next_leaf(root, path);
6499 if (ret < 0) {
6500 err = ret;
6501 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006502 }
Yan Zheng9036c102008-10-30 14:19:41 -04006503 if (ret > 0)
6504 goto not_found;
6505 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006506 }
Yan Zheng9036c102008-10-30 14:19:41 -04006507 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6508 if (found_key.objectid != objectid ||
6509 found_key.type != BTRFS_EXTENT_DATA_KEY)
6510 goto not_found;
6511 if (start + len <= found_key.offset)
6512 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006513 if (start > found_key.offset)
6514 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04006515 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006516 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006517 em->len = found_key.offset - start;
6518 goto not_found_em;
6519 }
6520
Filipe Manana7ffbb592014-06-09 03:48:05 +01006521 btrfs_extent_item_to_extent_map(inode, path, item, new_inline, em);
6522
Yan Zhengd899e052008-10-30 14:25:28 -04006523 if (found_type == BTRFS_FILE_EXTENT_REG ||
6524 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006525 goto insert;
6526 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006527 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006528 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006529 size_t size;
6530 size_t extent_offset;
6531 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006532
Filipe Manana7ffbb592014-06-09 03:48:05 +01006533 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04006534 goto out;
Yan689f9342007-10-29 11:41:07 -04006535
Chris Mason514ac8a2014-01-03 21:07:00 -08006536 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04006537 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05006538 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04006539 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006540 em->start = extent_start + extent_offset;
Qu Wenruofda28322013-02-26 08:10:22 +00006541 em->len = ALIGN(copy_size, root->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006542 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006543 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006544 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006545 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006546 if (btrfs_file_extent_compression(leaf, item) !=
6547 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006548 ret = uncompress_inline(path, inode, page,
6549 pg_offset,
6550 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04006551 if (ret) {
6552 err = ret;
6553 goto out;
6554 }
Chris Masonc8b97812008-10-29 14:49:59 -04006555 } else {
6556 map = kmap(page);
6557 read_extent_buffer(leaf, map + pg_offset, ptr,
6558 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04006559 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
6560 memset(map + pg_offset + copy_size, 0,
6561 PAGE_CACHE_SIZE - pg_offset -
6562 copy_size);
6563 }
Chris Masonc8b97812008-10-29 14:49:59 -04006564 kunmap(page);
6565 }
Chris Mason179e29e2007-11-01 11:28:41 -04006566 flush_dcache_page(page);
6567 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006568 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006569 if (!trans) {
6570 kunmap(page);
6571 free_extent_map(em);
6572 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006573
David Sterbab3b4aa72011-04-21 01:20:15 +02006574 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006575 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006576
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006577 if (IS_ERR(trans))
6578 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006579 goto again;
6580 }
Chris Masonc8b97812008-10-29 14:49:59 -04006581 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006582 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006583 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006584 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006585 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006586 }
Chris Masond1310b22008-01-24 16:13:08 -05006587 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006588 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006589 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04006590 }
6591not_found:
6592 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006593 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006594 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006595not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04006596 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04006597 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006598insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006599 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006600 if (em->start > start || extent_map_end(em) <= start) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006601 btrfs_err(root->fs_info, "bad extent! em: [%llu %llu] passed [%llu %llu]",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006602 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04006603 err = -EIO;
6604 goto out;
6605 }
Chris Masond1310b22008-01-24 16:13:08 -05006606
6607 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006608 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006609 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006610 /* it is possible that someone inserted the extent into the tree
6611 * while we had the lock dropped. It is also possible that
6612 * an overlapping map exists in the tree
6613 */
Chris Masona52d9a82007-08-27 16:49:44 -04006614 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04006615 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006616
6617 ret = 0;
6618
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006619 existing = search_extent_mapping(em_tree, start, len);
6620 /*
6621 * existing will always be non-NULL, since there must be
6622 * extent causing the -EEXIST.
6623 */
6624 if (start >= extent_map_end(existing) ||
Qu Wenruo32be3a12014-09-22 09:13:03 +08006625 start <= existing->start) {
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006626 /*
6627 * The existing extent map is the one nearest to
6628 * the [start, start + len) range which overlaps
6629 */
6630 err = merge_extent_mapping(em_tree, existing,
6631 em, start);
Chris Masone1c4b742008-04-22 13:26:46 -04006632 free_extent_map(existing);
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006633 if (err) {
Chris Mason3b951512008-04-17 11:29:12 -04006634 free_extent_map(em);
6635 em = NULL;
6636 }
6637 } else {
6638 free_extent_map(em);
6639 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006640 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006641 }
Chris Masona52d9a82007-08-27 16:49:44 -04006642 }
Chris Mason890871b2009-09-02 16:24:52 -04006643 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006644out:
liubo1abe9b82011-03-24 11:18:59 +00006645
Steven Rostedt4cd8587c2013-11-14 22:57:29 -05006646 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00006647
Chris Masonf4219502008-07-22 11:18:09 -04006648 if (path)
6649 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04006650 if (trans) {
6651 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05006652 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04006653 err = ret;
6654 }
Chris Masona52d9a82007-08-27 16:49:44 -04006655 if (err) {
6656 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04006657 return ERR_PTR(err);
6658 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006659 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04006660 return em;
6661}
6662
Chris Masonec29ed52011-02-23 16:23:20 -05006663struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
6664 size_t pg_offset, u64 start, u64 len,
6665 int create)
6666{
6667 struct extent_map *em;
6668 struct extent_map *hole_em = NULL;
6669 u64 range_start = start;
6670 u64 end;
6671 u64 found;
6672 u64 found_end;
6673 int err = 0;
6674
6675 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
6676 if (IS_ERR(em))
6677 return em;
6678 if (em) {
6679 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00006680 * if our em maps to
6681 * - a hole or
6682 * - a pre-alloc extent,
6683 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05006684 */
Liu Bof9e4fb52013-01-07 10:10:12 +00006685 if (em->block_start != EXTENT_MAP_HOLE &&
6686 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05006687 return em;
6688 else
6689 hole_em = em;
6690 }
6691
6692 /* check to see if we've wrapped (len == -1 or similar) */
6693 end = start + len;
6694 if (end < start)
6695 end = (u64)-1;
6696 else
6697 end -= 1;
6698
6699 em = NULL;
6700
6701 /* ok, we didn't find anything, lets look for delalloc */
6702 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
6703 end, len, EXTENT_DELALLOC, 1);
6704 found_end = range_start + found;
6705 if (found_end < range_start)
6706 found_end = (u64)-1;
6707
6708 /*
6709 * we didn't find anything useful, return
6710 * the original results from get_extent()
6711 */
6712 if (range_start > end || found_end <= start) {
6713 em = hole_em;
6714 hole_em = NULL;
6715 goto out;
6716 }
6717
6718 /* adjust the range_start to make sure it doesn't
6719 * go backwards from the start they passed in
6720 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05306721 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05006722 found = found_end - range_start;
6723
6724 if (found > 0) {
6725 u64 hole_start = start;
6726 u64 hole_len = len;
6727
David Sterba172ddd62011-04-21 00:48:27 +02006728 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05006729 if (!em) {
6730 err = -ENOMEM;
6731 goto out;
6732 }
6733 /*
6734 * when btrfs_get_extent can't find anything it
6735 * returns one huge hole
6736 *
6737 * make sure what it found really fits our range, and
6738 * adjust to make sure it is based on the start from
6739 * the caller
6740 */
6741 if (hole_em) {
6742 u64 calc_end = extent_map_end(hole_em);
6743
6744 if (calc_end <= start || (hole_em->start > end)) {
6745 free_extent_map(hole_em);
6746 hole_em = NULL;
6747 } else {
6748 hole_start = max(hole_em->start, start);
6749 hole_len = calc_end - hole_start;
6750 }
6751 }
6752 em->bdev = NULL;
6753 if (hole_em && range_start > hole_start) {
6754 /* our hole starts before our delalloc, so we
6755 * have to return just the parts of the hole
6756 * that go until the delalloc starts
6757 */
6758 em->len = min(hole_len,
6759 range_start - hole_start);
6760 em->start = hole_start;
6761 em->orig_start = hole_start;
6762 /*
6763 * don't adjust block start at all,
6764 * it is fixed at EXTENT_MAP_HOLE
6765 */
6766 em->block_start = hole_em->block_start;
6767 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00006768 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
6769 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05006770 } else {
6771 em->start = range_start;
6772 em->len = found;
6773 em->orig_start = range_start;
6774 em->block_start = EXTENT_MAP_DELALLOC;
6775 em->block_len = found;
6776 }
6777 } else if (hole_em) {
6778 return hole_em;
6779 }
6780out:
6781
6782 free_extent_map(hole_em);
6783 if (err) {
6784 free_extent_map(em);
6785 return ERR_PTR(err);
6786 }
6787 return em;
6788}
6789
Josef Bacik4b46fce2010-05-23 11:00:55 -04006790static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
6791 u64 start, u64 len)
6792{
6793 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04006794 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006795 struct btrfs_key ins;
6796 u64 alloc_hint;
6797 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006798
Josef Bacik4b46fce2010-05-23 11:00:55 -04006799 alloc_hint = get_extent_allocation_hint(inode, start, len);
Josef Bacik00361582013-08-14 14:02:47 -04006800 ret = btrfs_reserve_extent(root, len, root->sectorsize, 0,
Miao Xiee570fd22014-06-19 10:42:50 +08006801 alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006802 if (ret)
6803 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006804
Josef Bacik70c8a912012-10-11 16:54:30 -04006805 em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006806 ins.offset, ins.offset, ins.offset, 0);
Josef Bacik00361582013-08-14 14:02:47 -04006807 if (IS_ERR(em)) {
Miao Xiee570fd22014-06-19 10:42:50 +08006808 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006809 return em;
6810 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006811
6812 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
6813 ins.offset, ins.offset, 0);
6814 if (ret) {
Miao Xiee570fd22014-06-19 10:42:50 +08006815 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006816 free_extent_map(em);
6817 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006818 }
Josef Bacik00361582013-08-14 14:02:47 -04006819
Josef Bacik4b46fce2010-05-23 11:00:55 -04006820 return em;
6821}
6822
Chris Mason46bfbb52010-05-26 11:04:10 -04006823/*
6824 * returns 1 when the nocow is safe, < 1 on error, 0 if the
6825 * block must be cow'd
6826 */
Josef Bacik00361582013-08-14 14:02:47 -04006827noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04006828 u64 *orig_start, u64 *orig_block_len,
6829 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04006830{
Josef Bacik00361582013-08-14 14:02:47 -04006831 struct btrfs_trans_handle *trans;
Chris Mason46bfbb52010-05-26 11:04:10 -04006832 struct btrfs_path *path;
6833 int ret;
6834 struct extent_buffer *leaf;
6835 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08006836 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04006837 struct btrfs_file_extent_item *fi;
6838 struct btrfs_key key;
6839 u64 disk_bytenr;
6840 u64 backref_offset;
6841 u64 extent_end;
6842 u64 num_bytes;
6843 int slot;
6844 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04006845 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08006846
Chris Mason46bfbb52010-05-26 11:04:10 -04006847 path = btrfs_alloc_path();
6848 if (!path)
6849 return -ENOMEM;
6850
Josef Bacik00361582013-08-14 14:02:47 -04006851 ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04006852 offset, 0);
6853 if (ret < 0)
6854 goto out;
6855
6856 slot = path->slots[0];
6857 if (ret == 1) {
6858 if (slot == 0) {
6859 /* can't find the item, must cow */
6860 ret = 0;
6861 goto out;
6862 }
6863 slot--;
6864 }
6865 ret = 0;
6866 leaf = path->nodes[0];
6867 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08006868 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04006869 key.type != BTRFS_EXTENT_DATA_KEY) {
6870 /* not our file or wrong item type, must cow */
6871 goto out;
6872 }
6873
6874 if (key.offset > offset) {
6875 /* Wrong offset, must cow */
6876 goto out;
6877 }
6878
6879 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
6880 found_type = btrfs_file_extent_type(leaf, fi);
6881 if (found_type != BTRFS_FILE_EXTENT_REG &&
6882 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
6883 /* not a regular extent, must cow */
6884 goto out;
6885 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04006886
6887 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
6888 goto out;
6889
Miao Xiee77751a2013-12-27 21:11:50 +08006890 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
6891 if (extent_end <= offset)
6892 goto out;
6893
Chris Mason46bfbb52010-05-26 11:04:10 -04006894 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04006895 if (disk_bytenr == 0)
6896 goto out;
6897
6898 if (btrfs_file_extent_compression(leaf, fi) ||
6899 btrfs_file_extent_encryption(leaf, fi) ||
6900 btrfs_file_extent_other_encoding(leaf, fi))
6901 goto out;
6902
Chris Mason46bfbb52010-05-26 11:04:10 -04006903 backref_offset = btrfs_file_extent_offset(leaf, fi);
6904
Josef Bacik7ee9e442013-06-21 16:37:03 -04006905 if (orig_start) {
6906 *orig_start = key.offset - backref_offset;
6907 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
6908 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
6909 }
Josef Bacikeb384b52013-04-24 16:32:55 -04006910
Chris Mason46bfbb52010-05-26 11:04:10 -04006911 if (btrfs_extent_readonly(root, disk_bytenr))
6912 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08006913
6914 num_bytes = min(offset + *len, extent_end) - offset;
6915 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
6916 u64 range_end;
6917
6918 range_end = round_up(offset + num_bytes, root->sectorsize) - 1;
6919 ret = test_range_bit(io_tree, offset, range_end,
6920 EXTENT_DELALLOC, 0, NULL);
6921 if (ret) {
6922 ret = -EAGAIN;
6923 goto out;
6924 }
6925 }
6926
Josef Bacik1bda19e2013-10-18 12:10:36 -04006927 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04006928
6929 /*
6930 * look for other files referencing this extent, if we
6931 * find any we must cow
6932 */
Josef Bacik00361582013-08-14 14:02:47 -04006933 trans = btrfs_join_transaction(root);
6934 if (IS_ERR(trans)) {
6935 ret = 0;
Chris Mason46bfbb52010-05-26 11:04:10 -04006936 goto out;
Josef Bacik00361582013-08-14 14:02:47 -04006937 }
6938
6939 ret = btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
6940 key.offset - backref_offset, disk_bytenr);
6941 btrfs_end_transaction(trans, root);
6942 if (ret) {
6943 ret = 0;
6944 goto out;
6945 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006946
6947 /*
6948 * adjust disk_bytenr and num_bytes to cover just the bytes
6949 * in this extent we are about to write. If there
6950 * are any csums in that range we have to cow in order
6951 * to keep the csums correct
6952 */
6953 disk_bytenr += backref_offset;
6954 disk_bytenr += offset - key.offset;
Chris Mason46bfbb52010-05-26 11:04:10 -04006955 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
6956 goto out;
6957 /*
6958 * all of the above have passed, it is safe to overwrite this extent
6959 * without cow
6960 */
Josef Bacikeb384b52013-04-24 16:32:55 -04006961 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04006962 ret = 1;
6963out:
6964 btrfs_free_path(path);
6965 return ret;
6966}
6967
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07006968bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
6969{
6970 struct radix_tree_root *root = &inode->i_mapping->page_tree;
6971 int found = false;
6972 void **pagep = NULL;
6973 struct page *page = NULL;
6974 int start_idx;
6975 int end_idx;
6976
6977 start_idx = start >> PAGE_CACHE_SHIFT;
6978
6979 /*
6980 * end is the last byte in the last page. end == start is legal
6981 */
6982 end_idx = end >> PAGE_CACHE_SHIFT;
6983
6984 rcu_read_lock();
6985
6986 /* Most of the code in this while loop is lifted from
6987 * find_get_page. It's been modified to begin searching from a
6988 * page and return just the first page found in that range. If the
6989 * found idx is less than or equal to the end idx then we know that
6990 * a page exists. If no pages are found or if those pages are
6991 * outside of the range then we're fine (yay!) */
6992 while (page == NULL &&
6993 radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
6994 page = radix_tree_deref_slot(pagep);
6995 if (unlikely(!page))
6996 break;
6997
6998 if (radix_tree_exception(page)) {
Filipe Manana809f9012014-06-05 13:22:25 +01006999 if (radix_tree_deref_retry(page)) {
7000 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007001 continue;
Filipe Manana809f9012014-06-05 13:22:25 +01007002 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007003 /*
7004 * Otherwise, shmem/tmpfs must be storing a swap entry
7005 * here as an exceptional entry: so return it without
7006 * attempting to raise page count.
7007 */
Filipe Manana6fdef6d2014-06-05 13:22:26 +01007008 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007009 break; /* TODO: Is this relevant for this use case? */
7010 }
7011
Filipe Manana91405152014-06-05 13:22:24 +01007012 if (!page_cache_get_speculative(page)) {
7013 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007014 continue;
Filipe Manana91405152014-06-05 13:22:24 +01007015 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007016
7017 /*
7018 * Has the page moved?
7019 * This is part of the lockless pagecache protocol. See
7020 * include/linux/pagemap.h for details.
7021 */
7022 if (unlikely(page != *pagep)) {
7023 page_cache_release(page);
7024 page = NULL;
7025 }
7026 }
7027
7028 if (page) {
7029 if (page->index <= end_idx)
7030 found = true;
7031 page_cache_release(page);
7032 }
7033
7034 rcu_read_unlock();
7035 return found;
7036}
7037
Josef Bacikeb838e72012-07-31 16:28:48 -04007038static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7039 struct extent_state **cached_state, int writing)
7040{
7041 struct btrfs_ordered_extent *ordered;
7042 int ret = 0;
7043
7044 while (1) {
7045 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7046 0, cached_state);
7047 /*
7048 * We're concerned with the entire range that we're going to be
7049 * doing DIO to, so we need to make sure theres no ordered
7050 * extents in this range.
7051 */
7052 ordered = btrfs_lookup_ordered_range(inode, lockstart,
7053 lockend - lockstart + 1);
7054
7055 /*
7056 * We need to make sure there are no buffered pages in this
7057 * range either, we could have raced between the invalidate in
7058 * generic_file_direct_write and locking the extent. The
7059 * invalidate needs to happen so that reads after a write do not
7060 * get stale data.
7061 */
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007062 if (!ordered &&
7063 (!writing ||
7064 !btrfs_page_exists_in_range(inode, lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007065 break;
7066
7067 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7068 cached_state, GFP_NOFS);
7069
7070 if (ordered) {
7071 btrfs_start_ordered_extent(inode, ordered, 1);
7072 btrfs_put_ordered_extent(ordered);
7073 } else {
7074 /* Screw you mmap */
Filipe Manana728404d2014-10-10 09:43:11 +01007075 ret = btrfs_fdatawrite_range(inode, lockstart, lockend);
Filipe Manana075bdbd2014-10-09 21:18:55 +01007076 if (ret)
7077 break;
7078 ret = filemap_fdatawait_range(inode->i_mapping,
7079 lockstart,
7080 lockend);
Josef Bacikeb838e72012-07-31 16:28:48 -04007081 if (ret)
7082 break;
7083
7084 /*
7085 * If we found a page that couldn't be invalidated just
7086 * fall back to buffered.
7087 */
7088 ret = invalidate_inode_pages2_range(inode->i_mapping,
7089 lockstart >> PAGE_CACHE_SHIFT,
7090 lockend >> PAGE_CACHE_SHIFT);
7091 if (ret)
7092 break;
7093 }
7094
7095 cond_resched();
7096 }
7097
7098 return ret;
7099}
7100
Josef Bacik69ffb542012-09-11 15:40:07 -04007101static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
7102 u64 len, u64 orig_start,
7103 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04007104 u64 orig_block_len, u64 ram_bytes,
7105 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007106{
7107 struct extent_map_tree *em_tree;
7108 struct extent_map *em;
7109 struct btrfs_root *root = BTRFS_I(inode)->root;
7110 int ret;
7111
7112 em_tree = &BTRFS_I(inode)->extent_tree;
7113 em = alloc_extent_map();
7114 if (!em)
7115 return ERR_PTR(-ENOMEM);
7116
7117 em->start = start;
7118 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04007119 em->mod_start = start;
7120 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04007121 em->len = len;
7122 em->block_len = block_len;
7123 em->block_start = block_start;
7124 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007125 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007126 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007127 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007128 set_bit(EXTENT_FLAG_PINNED, &em->flags);
7129 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05007130 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04007131
7132 do {
7133 btrfs_drop_extent_cache(inode, em->start,
7134 em->start + em->len - 1, 0);
7135 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007136 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007137 write_unlock(&em_tree->lock);
7138 } while (ret == -EEXIST);
7139
7140 if (ret) {
7141 free_extent_map(em);
7142 return ERR_PTR(ret);
7143 }
7144
7145 return em;
7146}
7147
7148
Josef Bacik4b46fce2010-05-23 11:00:55 -04007149static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7150 struct buffer_head *bh_result, int create)
7151{
7152 struct extent_map *em;
7153 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04007154 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007155 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007156 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007157 u64 len = bh_result->b_size;
Josef Bacik3e05bde2015-02-11 15:08:57 -05007158 u64 orig_len = len;
Josef Bacikeb838e72012-07-31 16:28:48 -04007159 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007160 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007161
Miao Xie172a5042013-02-21 02:48:22 -07007162 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007163 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007164 else
Josef Bacikc3298612012-08-03 16:49:19 -04007165 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007166
Josef Bacikc3298612012-08-03 16:49:19 -04007167 lockstart = start;
7168 lockend = start + len - 1;
7169
Josef Bacikeb838e72012-07-31 16:28:48 -04007170 /*
7171 * If this errors out it's because we couldn't invalidate pagecache for
7172 * this range and we need to fallback to buffered.
7173 */
7174 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
7175 return -ENOTBLK;
7176
Josef Bacik4b46fce2010-05-23 11:00:55 -04007177 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007178 if (IS_ERR(em)) {
7179 ret = PTR_ERR(em);
7180 goto unlock_err;
7181 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007182
7183 /*
7184 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7185 * io. INLINE is special, and we could probably kludge it in here, but
7186 * it's still buffered so for safety lets just fall back to the generic
7187 * buffered path.
7188 *
7189 * For COMPRESSED we _have_ to read the entire extent in so we can
7190 * decompress it, so there will be buffering required no matter what we
7191 * do, so go ahead and fallback to buffered.
7192 *
7193 * We return -ENOTBLK because thats what makes DIO go ahead and go back
7194 * to buffered IO. Don't blame me, this is the price we pay for using
7195 * the generic code.
7196 */
7197 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7198 em->block_start == EXTENT_MAP_INLINE) {
7199 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007200 ret = -ENOTBLK;
7201 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007202 }
7203
7204 /* Just a good old fashioned hole, return */
7205 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7206 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7207 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007208 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007209 }
7210
7211 /*
7212 * We don't allocate a new extent in the following cases
7213 *
7214 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7215 * existing extent.
7216 * 2) The extent is marked as PREALLOC. We're good to go here and can
7217 * just use the extent.
7218 *
7219 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007220 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007221 len = min(len, em->len - (start - em->start));
7222 lockstart = start + len;
7223 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007224 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007225
7226 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7227 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7228 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007229 int type;
7230 int ret;
Josef Bacikeb384b52013-04-24 16:32:55 -04007231 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007232
7233 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7234 type = BTRFS_ORDERED_PREALLOC;
7235 else
7236 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007237 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007238 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007239
Josef Bacik00361582013-08-14 14:02:47 -04007240 if (can_nocow_extent(inode, start, &len, &orig_start,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007241 &orig_block_len, &ram_bytes) == 1) {
Josef Bacik69ffb542012-09-11 15:40:07 -04007242 if (type == BTRFS_ORDERED_PREALLOC) {
7243 free_extent_map(em);
7244 em = create_pinned_em(inode, start, len,
7245 orig_start,
Josef Bacikb4939682012-12-03 10:31:19 -05007246 block_start, len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04007247 orig_block_len,
7248 ram_bytes, type);
Filipe Manana555e1282014-07-07 12:35:21 +01007249 if (IS_ERR(em)) {
7250 ret = PTR_ERR(em);
Josef Bacik69ffb542012-09-11 15:40:07 -04007251 goto unlock_err;
Filipe Manana555e1282014-07-07 12:35:21 +01007252 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007253 }
7254
Chris Mason46bfbb52010-05-26 11:04:10 -04007255 ret = btrfs_add_ordered_extent_dio(inode, start,
7256 block_start, len, len, type);
Chris Mason46bfbb52010-05-26 11:04:10 -04007257 if (ret) {
7258 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007259 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007260 }
7261 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007262 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007263 }
Josef Bacik00361582013-08-14 14:02:47 -04007264
Chris Mason46bfbb52010-05-26 11:04:10 -04007265 /*
7266 * this will cow the extent, reset the len in case we changed
7267 * it above
7268 */
7269 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007270 free_extent_map(em);
7271 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007272 if (IS_ERR(em)) {
7273 ret = PTR_ERR(em);
7274 goto unlock_err;
7275 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007276 len = min(len, em->len - (start - em->start));
7277unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007278 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7279 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007280 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007281 bh_result->b_bdev = em->bdev;
7282 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007283 if (create) {
7284 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7285 set_buffer_new(bh_result);
7286
7287 /*
7288 * Need to update the i_size under the extent lock so buffered
7289 * readers will get the updated i_size when we unlock.
7290 */
7291 if (start + len > i_size_read(inode))
7292 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007293
Josef Bacik3e05bde2015-02-11 15:08:57 -05007294 if (len < orig_len) {
7295 spin_lock(&BTRFS_I(inode)->lock);
7296 BTRFS_I(inode)->outstanding_extents++;
7297 spin_unlock(&BTRFS_I(inode)->lock);
7298 }
Josef Bacik32667892015-02-11 15:08:58 -05007299 btrfs_free_reserved_data_space(inode, len);
Josef Bacikc3473e82012-06-19 10:59:00 -04007300 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007301
Josef Bacikeb838e72012-07-31 16:28:48 -04007302 /*
7303 * In the case of write we need to clear and unlock the entire range,
7304 * in the case of read we need to unlock only the end area that we
7305 * aren't using if there is any left over space.
7306 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007307 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007308 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7309 lockend, unlock_bits, 1, 0,
7310 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06007311 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007312 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007313 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007314
Josef Bacik4b46fce2010-05-23 11:00:55 -04007315 free_extent_map(em);
7316
7317 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007318
7319unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007320 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7321 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
7322 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007323}
7324
Miao Xie8b110e32014-09-12 18:44:03 +08007325static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio,
7326 int rw, int mirror_num)
7327{
7328 struct btrfs_root *root = BTRFS_I(inode)->root;
7329 int ret;
7330
7331 BUG_ON(rw & REQ_WRITE);
7332
7333 bio_get(bio);
7334
7335 ret = btrfs_bio_wq_end_io(root->fs_info, bio,
7336 BTRFS_WQ_ENDIO_DIO_REPAIR);
7337 if (ret)
7338 goto err;
7339
7340 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
7341err:
7342 bio_put(bio);
7343 return ret;
7344}
7345
7346static int btrfs_check_dio_repairable(struct inode *inode,
7347 struct bio *failed_bio,
7348 struct io_failure_record *failrec,
7349 int failed_mirror)
7350{
7351 int num_copies;
7352
7353 num_copies = btrfs_num_copies(BTRFS_I(inode)->root->fs_info,
7354 failrec->logical, failrec->len);
7355 if (num_copies == 1) {
7356 /*
7357 * we only have a single copy of the data, so don't bother with
7358 * all the retry and error correction code that follows. no
7359 * matter what the error is, it is very likely to persist.
7360 */
7361 pr_debug("Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d\n",
7362 num_copies, failrec->this_mirror, failed_mirror);
7363 return 0;
7364 }
7365
7366 failrec->failed_mirror = failed_mirror;
7367 failrec->this_mirror++;
7368 if (failrec->this_mirror == failed_mirror)
7369 failrec->this_mirror++;
7370
7371 if (failrec->this_mirror > num_copies) {
7372 pr_debug("Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d\n",
7373 num_copies, failrec->this_mirror, failed_mirror);
7374 return 0;
7375 }
7376
7377 return 1;
7378}
7379
7380static int dio_read_error(struct inode *inode, struct bio *failed_bio,
7381 struct page *page, u64 start, u64 end,
7382 int failed_mirror, bio_end_io_t *repair_endio,
7383 void *repair_arg)
7384{
7385 struct io_failure_record *failrec;
7386 struct bio *bio;
7387 int isector;
7388 int read_mode;
7389 int ret;
7390
7391 BUG_ON(failed_bio->bi_rw & REQ_WRITE);
7392
7393 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7394 if (ret)
7395 return ret;
7396
7397 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7398 failed_mirror);
7399 if (!ret) {
7400 free_io_failure(inode, failrec);
7401 return -EIO;
7402 }
7403
7404 if (failed_bio->bi_vcnt > 1)
7405 read_mode = READ_SYNC | REQ_FAILFAST_DEV;
7406 else
7407 read_mode = READ_SYNC;
7408
7409 isector = start - btrfs_io_bio(failed_bio)->logical;
7410 isector >>= inode->i_sb->s_blocksize_bits;
7411 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
7412 0, isector, repair_endio, repair_arg);
7413 if (!bio) {
7414 free_io_failure(inode, failrec);
7415 return -EIO;
7416 }
7417
7418 btrfs_debug(BTRFS_I(inode)->root->fs_info,
7419 "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n",
7420 read_mode, failrec->this_mirror, failrec->in_validation);
7421
7422 ret = submit_dio_repair_bio(inode, bio, read_mode,
7423 failrec->this_mirror);
7424 if (ret) {
7425 free_io_failure(inode, failrec);
7426 bio_put(bio);
7427 }
7428
7429 return ret;
7430}
7431
7432struct btrfs_retry_complete {
7433 struct completion done;
7434 struct inode *inode;
7435 u64 start;
7436 int uptodate;
7437};
7438
7439static void btrfs_retry_endio_nocsum(struct bio *bio, int err)
7440{
7441 struct btrfs_retry_complete *done = bio->bi_private;
7442 struct bio_vec *bvec;
7443 int i;
7444
7445 if (err)
7446 goto end;
7447
7448 done->uptodate = 1;
7449 bio_for_each_segment_all(bvec, bio, i)
7450 clean_io_failure(done->inode, done->start, bvec->bv_page, 0);
7451end:
7452 complete(&done->done);
7453 bio_put(bio);
7454}
7455
7456static int __btrfs_correct_data_nocsum(struct inode *inode,
7457 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007458{
Kent Overstreet2c30c712013-11-07 12:20:26 -08007459 struct bio_vec *bvec;
Miao Xie8b110e32014-09-12 18:44:03 +08007460 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007461 u64 start;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007462 int i;
Miao Xiec1dc0892014-09-12 18:43:56 +08007463 int ret;
Miao Xiedc380ae2014-09-12 18:43:55 +08007464
Miao Xiec1dc0892014-09-12 18:43:56 +08007465 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08007466 done.inode = inode;
7467
7468 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
7469try_again:
7470 done.uptodate = 0;
7471 done.start = start;
7472 init_completion(&done.done);
7473
7474 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page, start,
7475 start + bvec->bv_len - 1,
7476 io_bio->mirror_num,
7477 btrfs_retry_endio_nocsum, &done);
7478 if (ret)
7479 return ret;
7480
7481 wait_for_completion(&done.done);
7482
7483 if (!done.uptodate) {
7484 /* We might have another mirror, so try again */
7485 goto try_again;
7486 }
7487
7488 start += bvec->bv_len;
7489 }
7490
7491 return 0;
7492}
7493
7494static void btrfs_retry_endio(struct bio *bio, int err)
7495{
7496 struct btrfs_retry_complete *done = bio->bi_private;
7497 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
7498 struct bio_vec *bvec;
7499 int uptodate;
7500 int ret;
7501 int i;
7502
7503 if (err)
7504 goto end;
7505
7506 uptodate = 1;
7507 bio_for_each_segment_all(bvec, bio, i) {
7508 ret = __readpage_endio_check(done->inode, io_bio, i,
7509 bvec->bv_page, 0,
7510 done->start, bvec->bv_len);
7511 if (!ret)
7512 clean_io_failure(done->inode, done->start,
7513 bvec->bv_page, 0);
7514 else
7515 uptodate = 0;
7516 }
7517
7518 done->uptodate = uptodate;
7519end:
7520 complete(&done->done);
7521 bio_put(bio);
7522}
7523
7524static int __btrfs_subio_endio_read(struct inode *inode,
7525 struct btrfs_io_bio *io_bio, int err)
7526{
7527 struct bio_vec *bvec;
7528 struct btrfs_retry_complete done;
7529 u64 start;
7530 u64 offset = 0;
7531 int i;
7532 int ret;
7533
7534 err = 0;
7535 start = io_bio->logical;
7536 done.inode = inode;
7537
Miao Xiec1dc0892014-09-12 18:43:56 +08007538 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
Miao Xiedc380ae2014-09-12 18:43:55 +08007539 ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page,
7540 0, start, bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08007541 if (likely(!ret))
7542 goto next;
7543try_again:
7544 done.uptodate = 0;
7545 done.start = start;
7546 init_completion(&done.done);
7547
7548 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page, start,
7549 start + bvec->bv_len - 1,
7550 io_bio->mirror_num,
7551 btrfs_retry_endio, &done);
7552 if (ret) {
7553 err = ret;
7554 goto next;
7555 }
7556
7557 wait_for_completion(&done.done);
7558
7559 if (!done.uptodate) {
7560 /* We might have another mirror, so try again */
7561 goto try_again;
7562 }
7563next:
7564 offset += bvec->bv_len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007565 start += bvec->bv_len;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007566 }
Miao Xiec1dc0892014-09-12 18:43:56 +08007567
7568 return err;
7569}
7570
Miao Xie8b110e32014-09-12 18:44:03 +08007571static int btrfs_subio_endio_read(struct inode *inode,
7572 struct btrfs_io_bio *io_bio, int err)
7573{
7574 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
7575
7576 if (skip_csum) {
7577 if (unlikely(err))
7578 return __btrfs_correct_data_nocsum(inode, io_bio);
7579 else
7580 return 0;
7581 } else {
7582 return __btrfs_subio_endio_read(inode, io_bio, err);
7583 }
7584}
7585
Miao Xiec1dc0892014-09-12 18:43:56 +08007586static void btrfs_endio_direct_read(struct bio *bio, int err)
7587{
7588 struct btrfs_dio_private *dip = bio->bi_private;
7589 struct inode *inode = dip->inode;
7590 struct bio *dio_bio;
7591 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
7592
Miao Xie8b110e32014-09-12 18:44:03 +08007593 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
7594 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08007595
Josef Bacik4b46fce2010-05-23 11:00:55 -04007596 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007597 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04007598 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007599
Josef Bacik4b46fce2010-05-23 11:00:55 -04007600 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04007601
7602 /* If we had a csum failure make sure to clear the uptodate flag */
7603 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04007604 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
7605 dio_end_io(dio_bio, err);
Miao Xie23ea8e52014-09-12 18:43:54 +08007606
7607 if (io_bio->end_io)
7608 io_bio->end_io(io_bio, err);
Chris Mason9be33952013-05-17 18:30:14 -04007609 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007610}
7611
7612static void btrfs_endio_direct_write(struct bio *bio, int err)
7613{
7614 struct btrfs_dio_private *dip = bio->bi_private;
7615 struct inode *inode = dip->inode;
7616 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007617 struct btrfs_ordered_extent *ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05007618 u64 ordered_offset = dip->logical_offset;
7619 u64 ordered_bytes = dip->bytes;
Chris Mason9be33952013-05-17 18:30:14 -04007620 struct bio *dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007621 int ret;
7622
7623 if (err)
7624 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05007625again:
7626 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
7627 &ordered_offset,
Josef Bacik5fd02042012-05-02 14:00:54 -04007628 ordered_bytes, !err);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007629 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05007630 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007631
Liu Bo9e0af232014-08-15 23:36:53 +08007632 btrfs_init_work(&ordered->work, btrfs_endio_write_helper,
7633 finish_ordered_fn, NULL, NULL);
Qu Wenruofccb5d82014-02-28 10:46:10 +08007634 btrfs_queue_work(root->fs_info->endio_write_workers,
7635 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05007636out_test:
7637 /*
7638 * our bio might span multiple ordered extents. If we haven't
7639 * completed the accounting for the whole dio, go back and try again
7640 */
7641 if (ordered_offset < dip->logical_offset + dip->bytes) {
7642 ordered_bytes = dip->logical_offset + dip->bytes -
7643 ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04007644 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05007645 goto again;
7646 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007647out_done:
Chris Mason9be33952013-05-17 18:30:14 -04007648 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007649
Josef Bacik4b46fce2010-05-23 11:00:55 -04007650 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04007651
7652 /* If we had an error make sure to clear the uptodate flag */
7653 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04007654 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
7655 dio_end_io(dio_bio, err);
7656 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007657}
7658
Chris Masoneaf25d92010-05-25 09:48:28 -04007659static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
7660 struct bio *bio, int mirror_num,
7661 unsigned long bio_flags, u64 offset)
7662{
7663 int ret;
7664 struct btrfs_root *root = BTRFS_I(inode)->root;
7665 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007666 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04007667 return 0;
7668}
7669
Miao Xiee65e1532010-11-22 03:04:43 +00007670static void btrfs_end_dio_bio(struct bio *bio, int err)
7671{
7672 struct btrfs_dio_private *dip = bio->bi_private;
7673
Miao Xie8b110e32014-09-12 18:44:03 +08007674 if (err)
7675 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
7676 "direct IO failed ino %llu rw %lu sector %#Lx len %u err no %d",
7677 btrfs_ino(dip->inode), bio->bi_rw,
7678 (unsigned long long)bio->bi_iter.bi_sector,
7679 bio->bi_iter.bi_size, err);
7680
7681 if (dip->subio_endio)
7682 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08007683
7684 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00007685 dip->errors = 1;
7686
7687 /*
7688 * before atomic variable goto zero, we must make sure
7689 * dip->errors is perceived to be set.
7690 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01007691 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00007692 }
7693
7694 /* if there are more bios still pending for this dio, just exit */
7695 if (!atomic_dec_and_test(&dip->pending_bios))
7696 goto out;
7697
Chris Mason9be33952013-05-17 18:30:14 -04007698 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00007699 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04007700 } else {
7701 set_bit(BIO_UPTODATE, &dip->dio_bio->bi_flags);
Miao Xiee65e1532010-11-22 03:04:43 +00007702 bio_endio(dip->orig_bio, 0);
7703 }
7704out:
7705 bio_put(bio);
7706}
7707
7708static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
7709 u64 first_sector, gfp_t gfp_flags)
7710{
7711 int nr_vecs = bio_get_nr_vecs(bdev);
7712 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
7713}
7714
Miao Xiec1dc0892014-09-12 18:43:56 +08007715static inline int btrfs_lookup_and_bind_dio_csum(struct btrfs_root *root,
7716 struct inode *inode,
7717 struct btrfs_dio_private *dip,
7718 struct bio *bio,
7719 u64 file_offset)
7720{
7721 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
7722 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
7723 int ret;
7724
7725 /*
7726 * We load all the csum data we need when we submit
7727 * the first bio to reduce the csum tree search and
7728 * contention.
7729 */
7730 if (dip->logical_offset == file_offset) {
7731 ret = btrfs_lookup_bio_sums_dio(root, inode, dip->orig_bio,
7732 file_offset);
7733 if (ret)
7734 return ret;
7735 }
7736
7737 if (bio == dip->orig_bio)
7738 return 0;
7739
7740 file_offset -= dip->logical_offset;
7741 file_offset >>= inode->i_sb->s_blocksize_bits;
7742 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
7743
7744 return 0;
7745}
7746
Miao Xiee65e1532010-11-22 03:04:43 +00007747static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
7748 int rw, u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007749 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00007750{
Miao Xiefacc8a222013-07-25 19:22:34 +08007751 struct btrfs_dio_private *dip = bio->bi_private;
Miao Xiee65e1532010-11-22 03:04:43 +00007752 int write = rw & REQ_WRITE;
7753 struct btrfs_root *root = BTRFS_I(inode)->root;
7754 int ret;
7755
Josef Bacikb812ce22012-11-16 13:56:32 -05007756 if (async_submit)
7757 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
7758
Miao Xiee65e1532010-11-22 03:04:43 +00007759 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04007760
7761 if (!write) {
David Sterbabfebd8b2014-07-30 00:25:45 +02007762 ret = btrfs_bio_wq_end_io(root->fs_info, bio,
7763 BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04007764 if (ret)
7765 goto err;
7766 }
Miao Xiee65e1532010-11-22 03:04:43 +00007767
Josef Bacik1ae39932011-04-06 14:41:34 -04007768 if (skip_sum)
7769 goto map;
7770
7771 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00007772 ret = btrfs_wq_submit_bio(root->fs_info,
7773 inode, rw, bio, 0, 0,
7774 file_offset,
7775 __btrfs_submit_bio_start_direct_io,
7776 __btrfs_submit_bio_done);
7777 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04007778 } else if (write) {
7779 /*
7780 * If we aren't doing async submit, calculate the csum of the
7781 * bio now.
7782 */
7783 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
7784 if (ret)
7785 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08007786 } else {
Miao Xiec1dc0892014-09-12 18:43:56 +08007787 ret = btrfs_lookup_and_bind_dio_csum(root, inode, dip, bio,
7788 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007789 if (ret)
7790 goto err;
7791 }
Josef Bacik1ae39932011-04-06 14:41:34 -04007792map:
7793 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007794err:
7795 bio_put(bio);
7796 return ret;
7797}
7798
7799static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
7800 int skip_sum)
7801{
7802 struct inode *inode = dip->inode;
7803 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00007804 struct bio *bio;
7805 struct bio *orig_bio = dip->orig_bio;
7806 struct bio_vec *bvec = orig_bio->bi_io_vec;
Kent Overstreet4f024f32013-10-11 15:44:27 -07007807 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00007808 u64 file_offset = dip->logical_offset;
7809 u64 submit_len = 0;
7810 u64 map_length;
7811 int nr_pages = 0;
Miao Xie23ea8e52014-09-12 18:43:54 +08007812 int ret;
Josef Bacik1ae39932011-04-06 14:41:34 -04007813 int async_submit = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007814
Kent Overstreet4f024f32013-10-11 15:44:27 -07007815 map_length = orig_bio->bi_iter.bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007816 ret = btrfs_map_block(root->fs_info, rw, start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007817 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08007818 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00007819 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08007820
Kent Overstreet4f024f32013-10-11 15:44:27 -07007821 if (map_length >= orig_bio->bi_iter.bi_size) {
Josef Bacik02f57c72011-04-06 14:25:44 -04007822 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08007823 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04007824 goto submit;
7825 }
7826
David Woodhouse53b381b2013-01-29 18:40:14 -05007827 /* async crcs make it difficult to collect full stripe writes. */
Zhao Leiffe2d202015-01-20 15:11:44 +08007828 if (btrfs_get_alloc_profile(root, 1) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05007829 async_submit = 0;
7830 else
7831 async_submit = 1;
7832
Josef Bacik02f57c72011-04-06 14:25:44 -04007833 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
7834 if (!bio)
7835 return -ENOMEM;
Miao Xie7a5c3c92014-06-17 18:58:59 +08007836
Josef Bacik02f57c72011-04-06 14:25:44 -04007837 bio->bi_private = dip;
7838 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08007839 btrfs_io_bio(bio)->logical = file_offset;
Josef Bacik02f57c72011-04-06 14:25:44 -04007840 atomic_inc(&dip->pending_bios);
7841
Miao Xiee65e1532010-11-22 03:04:43 +00007842 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
David Sterbaee39b432014-09-30 01:33:33 +02007843 if (map_length < submit_len + bvec->bv_len ||
Miao Xiee65e1532010-11-22 03:04:43 +00007844 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
David Sterbaee39b432014-09-30 01:33:33 +02007845 bvec->bv_offset) < bvec->bv_len) {
Miao Xiee65e1532010-11-22 03:04:43 +00007846 /*
7847 * inc the count before we submit the bio so
7848 * we know the end IO handler won't happen before
7849 * we inc the count. Otherwise, the dip might get freed
7850 * before we're done setting it up
7851 */
7852 atomic_inc(&dip->pending_bios);
7853 ret = __btrfs_submit_dio_bio(bio, inode, rw,
7854 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007855 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007856 if (ret) {
7857 bio_put(bio);
7858 atomic_dec(&dip->pending_bios);
7859 goto out_err;
7860 }
7861
Miao Xiee65e1532010-11-22 03:04:43 +00007862 start_sector += submit_len >> 9;
7863 file_offset += submit_len;
7864
7865 submit_len = 0;
7866 nr_pages = 0;
7867
7868 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
7869 start_sector, GFP_NOFS);
7870 if (!bio)
7871 goto out_err;
7872 bio->bi_private = dip;
7873 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08007874 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00007875
Kent Overstreet4f024f32013-10-11 15:44:27 -07007876 map_length = orig_bio->bi_iter.bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007877 ret = btrfs_map_block(root->fs_info, rw,
Stefan Behrens3ec706c2012-11-05 15:46:42 +01007878 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007879 &map_length, NULL, 0);
7880 if (ret) {
7881 bio_put(bio);
7882 goto out_err;
7883 }
7884 } else {
7885 submit_len += bvec->bv_len;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307886 nr_pages++;
Miao Xiee65e1532010-11-22 03:04:43 +00007887 bvec++;
7888 }
7889 }
7890
Josef Bacik02f57c72011-04-06 14:25:44 -04007891submit:
Miao Xiee65e1532010-11-22 03:04:43 +00007892 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007893 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007894 if (!ret)
7895 return 0;
7896
7897 bio_put(bio);
7898out_err:
7899 dip->errors = 1;
7900 /*
7901 * before atomic variable goto zero, we must
7902 * make sure dip->errors is perceived to be set.
7903 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01007904 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00007905 if (atomic_dec_and_test(&dip->pending_bios))
7906 bio_io_error(dip->orig_bio);
7907
7908 /* bio_end_io() will handle error, so we needn't return it */
7909 return 0;
7910}
7911
Chris Mason9be33952013-05-17 18:30:14 -04007912static void btrfs_submit_direct(int rw, struct bio *dio_bio,
7913 struct inode *inode, loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007914{
7915 struct btrfs_root *root = BTRFS_I(inode)->root;
7916 struct btrfs_dio_private *dip;
Chris Mason9be33952013-05-17 18:30:14 -04007917 struct bio *io_bio;
Miao Xie23ea8e52014-09-12 18:43:54 +08007918 struct btrfs_io_bio *btrfs_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007919 int skip_sum;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02007920 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007921 int ret = 0;
7922
7923 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
7924
Chris Mason9be33952013-05-17 18:30:14 -04007925 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04007926 if (!io_bio) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007927 ret = -ENOMEM;
7928 goto free_ordered;
7929 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007930
Miao Xiec1dc0892014-09-12 18:43:56 +08007931 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04007932 if (!dip) {
7933 ret = -ENOMEM;
7934 goto free_io_bio;
7935 }
7936
7937 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007938 dip->inode = inode;
7939 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07007940 dip->bytes = dio_bio->bi_iter.bi_size;
7941 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Chris Mason9be33952013-05-17 18:30:14 -04007942 io_bio->bi_private = dip;
Chris Mason9be33952013-05-17 18:30:14 -04007943 dip->orig_bio = io_bio;
7944 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00007945 atomic_set(&dip->pending_bios, 0);
Miao Xiec1dc0892014-09-12 18:43:56 +08007946 btrfs_bio = btrfs_io_bio(io_bio);
7947 btrfs_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007948
Miao Xiec1dc0892014-09-12 18:43:56 +08007949 if (write) {
Chris Mason9be33952013-05-17 18:30:14 -04007950 io_bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08007951 } else {
Chris Mason9be33952013-05-17 18:30:14 -04007952 io_bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08007953 dip->subio_endio = btrfs_subio_endio_read;
7954 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007955
Miao Xiee65e1532010-11-22 03:04:43 +00007956 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
7957 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04007958 return;
Chris Mason9be33952013-05-17 18:30:14 -04007959
Miao Xie23ea8e52014-09-12 18:43:54 +08007960 if (btrfs_bio->end_io)
7961 btrfs_bio->end_io(btrfs_bio, ret);
Chris Mason9be33952013-05-17 18:30:14 -04007962free_io_bio:
7963 bio_put(io_bio);
7964
Josef Bacik4b46fce2010-05-23 11:00:55 -04007965free_ordered:
7966 /*
7967 * If this is a write, we need to clean up the reserved space and kill
7968 * the ordered extent.
7969 */
7970 if (write) {
7971 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05007972 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007973 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
7974 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
7975 btrfs_free_reserved_extent(root, ordered->start,
Miao Xiee570fd22014-06-19 10:42:50 +08007976 ordered->disk_len, 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007977 btrfs_put_ordered_extent(ordered);
7978 btrfs_put_ordered_extent(ordered);
7979 }
Chris Mason9be33952013-05-17 18:30:14 -04007980 bio_endio(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007981}
7982
Chris Mason5a5f79b2010-05-26 21:33:37 -04007983static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
Al Viro28060d52014-03-22 05:15:17 -04007984 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04007985{
7986 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00007987 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007988 unsigned blocksize_mask = root->sectorsize - 1;
7989 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007990
7991 if (offset & blocksize_mask)
7992 goto out;
7993
Al Viro28060d52014-03-22 05:15:17 -04007994 if (iov_iter_alignment(iter) & blocksize_mask)
7995 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00007996
Al Viro28060d52014-03-22 05:15:17 -04007997 /* If this is a write we don't need to check anymore */
7998 if (rw & WRITE)
7999 return 0;
8000 /*
8001 * Check to make sure we don't have duplicate iov_base's in this
8002 * iovec, if so return EINVAL, otherwise we'll get csum errors
8003 * when reading back.
8004 */
8005 for (seg = 0; seg < iter->nr_segs; seg++) {
8006 for (i = seg + 1; i < iter->nr_segs; i++) {
8007 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008008 goto out;
8009 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008010 }
8011 retval = 0;
8012out:
8013 return retval;
8014}
Josef Bacikeb838e72012-07-31 16:28:48 -04008015
Chris Mason16432982008-04-10 10:23:21 -04008016static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
Al Virod8d3d942014-03-04 21:27:34 -05008017 struct iov_iter *iter, loff_t offset)
Chris Mason16432982008-04-10 10:23:21 -04008018{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008019 struct file *file = iocb->ki_filp;
8020 struct inode *inode = file->f_mapping->host;
Miao Xie09348562013-02-07 10:12:07 +00008021 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008022 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008023 bool wakeup = true;
8024 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008025 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008026
Al Viro28060d52014-03-22 05:15:17 -04008027 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008028 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008029
Miao Xie38851cc2013-02-08 07:04:11 +00008030 atomic_inc(&inode->i_dio_count);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008031 smp_mb__after_atomic();
Miao Xie38851cc2013-02-08 07:04:11 +00008032
Josef Bacik0e267c42013-07-02 10:38:02 -04008033 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008034 * The generic stuff only does filemap_write_and_wait_range, which
8035 * isn't enough if we've written compressed pages to this area, so
8036 * we need to flush the dirty pages again to make absolutely sure
8037 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008038 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008039 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008040 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8041 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008042 filemap_fdatawrite_range(inode->i_mapping, offset,
8043 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008044
Miao Xie09348562013-02-07 10:12:07 +00008045 if (rw & WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008046 /*
8047 * If the write DIO is beyond the EOF, we need update
8048 * the isize, but it is protected by i_mutex. So we can
8049 * not unlock the i_mutex at this case.
8050 */
8051 if (offset + count <= inode->i_size) {
8052 mutex_unlock(&inode->i_mutex);
8053 relock = true;
8054 }
Miao Xie09348562013-02-07 10:12:07 +00008055 ret = btrfs_delalloc_reserve_space(inode, count);
8056 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008057 goto out;
David Sterbaee39b432014-09-30 01:33:33 +02008058 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8059 &BTRFS_I(inode)->runtime_flags)) {
Miao Xie38851cc2013-02-08 07:04:11 +00008060 inode_dio_done(inode);
8061 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8062 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008063 }
8064
8065 ret = __blockdev_direct_IO(rw, iocb, inode,
8066 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
Al Viro31b14032014-03-05 01:33:16 -05008067 iter, offset, btrfs_get_blocks_direct, NULL,
Miao Xie2e60a512013-02-08 07:01:08 +00008068 btrfs_submit_direct, flags);
Miao Xie09348562013-02-07 10:12:07 +00008069 if (rw & WRITE) {
8070 if (ret < 0 && ret != -EIOCBQUEUED)
8071 btrfs_delalloc_release_space(inode, count);
Miao Xie172a5042013-02-21 02:48:22 -07008072 else if (ret >= 0 && (size_t)ret < count)
Miao Xie09348562013-02-07 10:12:07 +00008073 btrfs_delalloc_release_space(inode,
8074 count - (size_t)ret);
Miao Xie09348562013-02-07 10:12:07 +00008075 }
Miao Xie38851cc2013-02-08 07:04:11 +00008076out:
Miao Xie2e60a512013-02-08 07:01:08 +00008077 if (wakeup)
8078 inode_dio_done(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008079 if (relock)
8080 mutex_lock(&inode->i_mutex);
Miao Xie09348562013-02-07 10:12:07 +00008081
8082 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008083}
8084
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008085#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8086
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008087static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8088 __u64 start, __u64 len)
8089{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008090 int ret;
8091
8092 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8093 if (ret)
8094 return ret;
8095
Chris Masonec29ed52011-02-23 16:23:20 -05008096 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008097}
8098
Chris Mason9ebefb182007-06-15 13:50:00 -04008099int btrfs_readpage(struct file *file, struct page *page)
8100{
Chris Masond1310b22008-01-24 16:13:08 -05008101 struct extent_io_tree *tree;
8102 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008103 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008104}
Chris Mason1832a6d2007-12-21 16:27:21 -05008105
Chris Mason39279cc2007-06-12 06:35:45 -04008106static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8107{
Chris Masond1310b22008-01-24 16:13:08 -05008108 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04008109
8110
8111 if (current->flags & PF_MEMALLOC) {
8112 redirty_page_for_writepage(wbc, page);
8113 unlock_page(page);
8114 return 0;
8115 }
Chris Masond1310b22008-01-24 16:13:08 -05008116 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04008117 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
8118}
Chris Mason39279cc2007-06-12 06:35:45 -04008119
Eric Sandeen48a3b632013-04-25 20:41:01 +00008120static int btrfs_writepages(struct address_space *mapping,
8121 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04008122{
Chris Masond1310b22008-01-24 16:13:08 -05008123 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05008124
Chris Masond1310b22008-01-24 16:13:08 -05008125 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04008126 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
8127}
8128
Chris Mason3ab2fb52007-11-08 10:59:22 -05008129static int
8130btrfs_readpages(struct file *file, struct address_space *mapping,
8131 struct list_head *pages, unsigned nr_pages)
8132{
Chris Masond1310b22008-01-24 16:13:08 -05008133 struct extent_io_tree *tree;
8134 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05008135 return extent_readpages(tree, mapping, pages, nr_pages,
8136 btrfs_get_extent);
8137}
Chris Masone6dcd2d2008-07-17 12:53:50 -04008138static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008139{
Chris Masond1310b22008-01-24 16:13:08 -05008140 struct extent_io_tree *tree;
8141 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04008142 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008143
Chris Masond1310b22008-01-24 16:13:08 -05008144 tree = &BTRFS_I(page->mapping->host)->io_tree;
8145 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05008146 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008147 if (ret == 1) {
8148 ClearPagePrivate(page);
8149 set_page_private(page, 0);
8150 page_cache_release(page);
8151 }
8152 return ret;
8153}
Chris Mason39279cc2007-06-12 06:35:45 -04008154
Chris Masone6dcd2d2008-07-17 12:53:50 -04008155static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8156{
Chris Mason98509cf2008-09-11 15:51:43 -04008157 if (PageWriteback(page) || PageDirty(page))
8158 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05008159 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008160}
8161
Lukas Czernerd47992f2013-05-21 23:17:23 -04008162static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8163 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008164{
Josef Bacik5fd02042012-05-02 14:00:54 -04008165 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008166 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008167 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008168 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008169 u64 page_start = page_offset(page);
8170 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008171 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008172
Chris Mason8b62b722009-09-02 16:53:46 -04008173 /*
8174 * we have the page locked, so new writeback can't start,
8175 * and the dirty bit won't be cleared while we are here.
8176 *
8177 * Wait for IO on this page so that we can safely clear
8178 * the PagePrivate2 bit and do ordered accounting
8179 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008180 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008181
Josef Bacik5fd02042012-05-02 14:00:54 -04008182 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008183 if (offset) {
8184 btrfs_releasepage(page, GFP_NOFS);
8185 return;
8186 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008187
8188 if (!inode_evicting)
8189 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
8190 ordered = btrfs_lookup_ordered_extent(inode, page_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008191 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04008192 /*
8193 * IO on this page will never be started, so we need
8194 * to account for any ordered extents now
8195 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008196 if (!inode_evicting)
8197 clear_extent_bit(tree, page_start, page_end,
8198 EXTENT_DIRTY | EXTENT_DELALLOC |
8199 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
8200 EXTENT_DEFRAG, 1, 0, &cached_state,
8201 GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04008202 /*
8203 * whoever cleared the private bit is responsible
8204 * for the finish_ordered_io
8205 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008206 if (TestClearPagePrivate2(page)) {
8207 struct btrfs_ordered_inode_tree *tree;
8208 u64 new_len;
8209
8210 tree = &BTRFS_I(inode)->ordered_tree;
8211
8212 spin_lock_irq(&tree->lock);
8213 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
8214 new_len = page_start - ordered->file_offset;
8215 if (new_len < ordered->truncated_len)
8216 ordered->truncated_len = new_len;
8217 spin_unlock_irq(&tree->lock);
8218
8219 if (btrfs_dec_test_ordered_pending(inode, &ordered,
8220 page_start,
8221 PAGE_CACHE_SIZE, 1))
8222 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008223 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008224 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008225 if (!inode_evicting) {
8226 cached_state = NULL;
8227 lock_extent_bits(tree, page_start, page_end, 0,
8228 &cached_state);
8229 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008230 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008231
8232 if (!inode_evicting) {
8233 clear_extent_bit(tree, page_start, page_end,
8234 EXTENT_LOCKED | EXTENT_DIRTY |
8235 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8236 EXTENT_DEFRAG, 1, 1,
8237 &cached_state, GFP_NOFS);
8238
8239 __btrfs_releasepage(page, GFP_NOFS);
8240 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008241
Chris Mason4a096752008-07-21 10:29:44 -04008242 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008243 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008244 ClearPagePrivate(page);
8245 set_page_private(page, 0);
8246 page_cache_release(page);
8247 }
Chris Mason39279cc2007-06-12 06:35:45 -04008248}
8249
Chris Mason9ebefb182007-06-15 13:50:00 -04008250/*
8251 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8252 * called from a page fault handler when a page is first dirtied. Hence we must
8253 * be careful to check for EOF conditions here. We set the page up correctly
8254 * for a written page which means we get ENOSPC checking when writing into
8255 * holes and correct delalloc and unwritten extent mapping on filesystems that
8256 * support these features.
8257 *
8258 * We are not allowed to take the i_mutex here so we have to play games to
8259 * protect against truncate races as the page could now be beyond EOF. Because
8260 * vmtruncate() writes the inode size before removing pages, once we have the
8261 * page lock we can determine safely if the page is beyond EOF. If it is not
8262 * beyond EOF, then the page is guaranteed safe against truncation until we
8263 * unlock the page.
8264 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07008265int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008266{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008267 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05008268 struct inode *inode = file_inode(vma->vm_file);
Chris Mason1832a6d2007-12-21 16:27:21 -05008269 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008270 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8271 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008272 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008273 char *kaddr;
8274 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008275 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05008276 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05008277 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04008278 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008279 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04008280
Jan Karab2b5ef52012-06-12 16:20:45 +02008281 sb_start_pagefault(inode->i_sb);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04008282 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05008283 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04008284 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008285 reserved = 1;
8286 }
Nick Piggin56a76f82009-03-31 15:23:23 -07008287 if (ret) {
8288 if (ret == -ENOMEM)
8289 ret = VM_FAULT_OOM;
8290 else /* -ENOSPC, -EIO, etc */
8291 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05008292 if (reserved)
8293 goto out;
8294 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008295 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008296
Nick Piggin56a76f82009-03-31 15:23:23 -07008297 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008298again:
Chris Mason9ebefb182007-06-15 13:50:00 -04008299 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008300 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008301 page_start = page_offset(page);
8302 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04008303
Chris Mason9ebefb182007-06-15 13:50:00 -04008304 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008305 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008306 /* page got truncated out from underneath us */
8307 goto out_unlock;
8308 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008309 wait_on_page_writeback(page);
8310
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008311 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008312 set_page_extent_mapped(page);
8313
Chris Masoneb84ae02008-07-17 13:53:27 -04008314 /*
8315 * we can't set the delalloc bits if there are pending ordered
8316 * extents. Drop our locks and wait for them to finish
8317 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008318 ordered = btrfs_lookup_ordered_extent(inode, page_start);
8319 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008320 unlock_extent_cached(io_tree, page_start, page_end,
8321 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008322 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04008323 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008324 btrfs_put_ordered_extent(ordered);
8325 goto again;
8326 }
8327
Josef Bacikfbf19082009-10-01 17:10:23 -04008328 /*
8329 * XXX - page_mkwrite gets called every time the page is dirtied, even
8330 * if it was already dirty, so for space accounting reasons we need to
8331 * clear any delalloc bits for the range we are fixing to save. There
8332 * is probably a better way to do this, but for now keep consistent with
8333 * prepare_pages in the normal write path.
8334 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00008335 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06008336 EXTENT_DIRTY | EXTENT_DELALLOC |
8337 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00008338 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04008339
Josef Bacik2ac55d42010-02-03 19:33:23 +00008340 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
8341 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008342 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008343 unlock_extent_cached(io_tree, page_start, page_end,
8344 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008345 ret = VM_FAULT_SIGBUS;
8346 goto out_unlock;
8347 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008348 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04008349
8350 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04008351 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04008352 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04008353 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04008354 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008355
Chris Masone6dcd2d2008-07-17 12:53:50 -04008356 if (zero_start != PAGE_CACHE_SIZE) {
8357 kaddr = kmap(page);
8358 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
8359 flush_dcache_page(page);
8360 kunmap(page);
8361 }
Chris Mason247e7432008-07-17 12:53:51 -04008362 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008363 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04008364 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008365
Chris Mason257c62e2009-10-13 13:21:08 -04008366 BTRFS_I(inode)->last_trans = root->fs_info->generation;
8367 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06008368 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04008369
Josef Bacik2ac55d42010-02-03 19:33:23 +00008370 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04008371
8372out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02008373 if (!ret) {
8374 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04008375 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02008376 }
Chris Mason9ebefb182007-06-15 13:50:00 -04008377 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05008378out:
Josef Bacikec39e182012-01-12 19:10:12 -05008379 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05008380out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02008381 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04008382 return ret;
8383}
8384
Josef Bacika41ad392011-01-31 15:30:16 -05008385static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04008386{
8387 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04008388 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04008389 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05008390 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008391 struct btrfs_trans_handle *trans;
Chris Masondbe674a2008-07-17 12:54:05 -04008392 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04008393 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04008394
Josef Bacik0ef8b722013-10-25 16:13:35 -04008395 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
8396 (u64)-1);
8397 if (ret)
8398 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008399
Josef Bacikfcb80c22011-05-03 10:40:22 -04008400 /*
8401 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
8402 * 3 things going on here
8403 *
8404 * 1) We need to reserve space for our orphan item and the space to
8405 * delete our orphan item. Lord knows we don't want to have a dangling
8406 * orphan item because we didn't reserve space to remove it.
8407 *
8408 * 2) We need to reserve space to update our inode.
8409 *
8410 * 3) We need to have something to cache all the space that is going to
8411 * be free'd up by the truncate operation, but also have some slack
8412 * space reserved in case it uses space during the truncate (thank you
8413 * very much snapshotting).
8414 *
8415 * And we need these to all be seperate. The fact is we can use alot of
8416 * space doing the truncate, and we have no earthly idea how much space
8417 * we will use, so we need the truncate reservation to be seperate so it
8418 * doesn't end up using space reserved for updating the inode or
8419 * removing the orphan item. We also need to be able to stop the
8420 * transaction and start a new one, which means we need to be able to
8421 * update the inode several times, and we have no idea of knowing how
8422 * many times that will be, so we can't just reserve 1 item for the
8423 * entirety of the opration, so that has to be done seperately as well.
8424 * Then there is the orphan item, which does indeed need to be held on
8425 * to for the whole operation, and we need nobody to touch this reserved
8426 * space except the orphan code.
8427 *
8428 * So that leaves us with
8429 *
8430 * 1) root->orphan_block_rsv - for the orphan deletion.
8431 * 2) rsv - for the truncate reservation, which we will steal from the
8432 * transaction reservation.
8433 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
8434 * updating the inode.
8435 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06008436 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008437 if (!rsv)
8438 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04008439 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008440 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05008441
Josef Bacik907cbce2011-08-08 13:46:15 -04008442 /*
Josef Bacik07127182011-08-19 10:29:59 -04008443 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04008444 * 1 for updating the inode.
8445 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05008446 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008447 if (IS_ERR(trans)) {
8448 err = PTR_ERR(trans);
8449 goto out;
8450 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05008451
Josef Bacik907cbce2011-08-08 13:46:15 -04008452 /* Migrate the slack space for the truncate to our reserve */
8453 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
8454 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008455 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05008456
Chris Mason5a3f23d2009-03-31 13:27:11 -04008457 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04008458 * So if we truncate and then write and fsync we normally would just
8459 * write the extents that changed, which is a problem if we need to
8460 * first truncate that entire inode. So set this flag so we write out
8461 * all of the extents in the inode to the sync log so we're completely
8462 * safe.
8463 */
8464 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008465 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008466
Yan, Zheng80825102009-11-12 09:35:36 +00008467 while (1) {
8468 ret = btrfs_truncate_inode_items(trans, root, inode,
8469 inode->i_size,
8470 BTRFS_EXTENT_DATA_KEY);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008471 if (ret != -ENOSPC) {
Josef Bacik3893e332011-01-31 16:03:11 -05008472 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00008473 break;
Josef Bacik3893e332011-01-31 16:03:11 -05008474 }
Chris Mason39279cc2007-06-12 06:35:45 -04008475
Josef Bacikfcb80c22011-05-03 10:40:22 -04008476 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008477 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05008478 if (ret) {
8479 err = ret;
8480 break;
8481 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04008482
Yan, Zheng80825102009-11-12 09:35:36 +00008483 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00008484 btrfs_btree_balance_dirty(root);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008485
8486 trans = btrfs_start_transaction(root, 2);
8487 if (IS_ERR(trans)) {
8488 ret = err = PTR_ERR(trans);
8489 trans = NULL;
8490 break;
8491 }
8492
8493 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
8494 rsv, min_size);
8495 BUG_ON(ret); /* shouldn't happen */
8496 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008497 }
8498
8499 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04008500 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008501 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05008502 if (ret)
8503 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00008504 }
8505
Chris Mason917c16b2011-11-08 14:49:59 -05008506 if (trans) {
8507 trans->block_rsv = &root->fs_info->trans_block_rsv;
8508 ret = btrfs_update_inode(trans, root, inode);
8509 if (ret && !err)
8510 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04008511
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008512 ret = btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00008513 btrfs_btree_balance_dirty(root);
Chris Mason917c16b2011-11-08 14:49:59 -05008514 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04008515
8516out:
8517 btrfs_free_block_rsv(root, rsv);
8518
Josef Bacik3893e332011-01-31 16:03:11 -05008519 if (ret && !err)
8520 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05008521
Josef Bacik3893e332011-01-31 16:03:11 -05008522 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04008523}
8524
Sven Wegener3b963622008-06-09 21:57:42 -04008525/*
Chris Masond352ac62008-09-29 15:18:18 -04008526 * create a new subvolume directory/inode (helper for the ioctl).
8527 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05008528int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008529 struct btrfs_root *new_root,
8530 struct btrfs_root *parent_root,
8531 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04008532{
Chris Mason39279cc2007-06-12 06:35:45 -04008533 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04008534 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008535 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008536
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01008537 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
8538 new_dirid, new_dirid,
8539 S_IFDIR | (~current_umask() & S_IRWXUGO),
8540 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04008541 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04008542 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008543 inode->i_op = &btrfs_dir_inode_operations;
8544 inode->i_fop = &btrfs_dir_file_operations;
8545
Miklos Szeredibfe86842011-10-28 14:13:29 +02008546 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04008547 btrfs_i_size_write(inode, 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07008548 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04008549
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008550 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
8551 if (err)
8552 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02008553 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008554 new_root->root_key.objectid, err);
8555
Yan, Zheng76dda932009-09-21 16:00:26 -04008556 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04008557
Yan, Zheng76dda932009-09-21 16:00:26 -04008558 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07008559 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04008560}
8561
Chris Mason39279cc2007-06-12 06:35:45 -04008562struct inode *btrfs_alloc_inode(struct super_block *sb)
8563{
8564 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008565 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008566
8567 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
8568 if (!ei)
8569 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008570
8571 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008572 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04008573 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04008574 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04008575 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008576 ei->delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08008577 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008578 ei->disk_i_size = 0;
8579 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04008580 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008581 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08008582 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008583 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06008584 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008585
Josef Bacik9e0baf62011-07-15 15:16:44 +00008586 spin_lock_init(&ei->lock);
8587 ei->outstanding_extents = 0;
8588 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008589
Josef Bacik72ac3c02012-05-23 14:13:11 -04008590 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08008591 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008592
Miao Xie16cdcec2011-04-22 18:12:22 +08008593 ei->delayed_node = NULL;
8594
chandan r9cc97d62012-07-04 12:48:07 +05308595 ei->i_otime.tv_sec = 0;
8596 ei->i_otime.tv_nsec = 0;
8597
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008598 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02008599 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02008600 extent_io_tree_init(&ei->io_tree, &inode->i_data);
8601 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04008602 ei->io_tree.track_uptodate = 1;
8603 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05008604 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008605 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05008606 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008607 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008608 INIT_LIST_HEAD(&ei->delalloc_inodes);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008609 RB_CLEAR_NODE(&ei->rb_node);
8610
8611 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008612}
8613
Josef Bacikaaedb552013-10-11 14:44:09 -04008614#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
8615void btrfs_test_destroy_inode(struct inode *inode)
8616{
8617 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
8618 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8619}
8620#endif
8621
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008622static void btrfs_i_callback(struct rcu_head *head)
8623{
8624 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008625 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8626}
8627
Chris Mason39279cc2007-06-12 06:35:45 -04008628void btrfs_destroy_inode(struct inode *inode)
8629{
Chris Masone6dcd2d2008-07-17 12:53:50 -04008630 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008631 struct btrfs_root *root = BTRFS_I(inode)->root;
8632
Al Virob3d9b7a2012-06-09 13:51:19 -04008633 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04008634 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00008635 WARN_ON(BTRFS_I(inode)->outstanding_extents);
8636 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04008637 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
8638 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08008639 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04008640
Chris Mason5a3f23d2009-03-31 13:27:11 -04008641 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05008642 * This can happen where we create an inode, but somebody else also
8643 * created the same inode and we need to destroy the one we already
8644 * created.
8645 */
8646 if (!root)
8647 goto free;
8648
Josef Bacik8a35d952012-05-23 14:26:42 -04008649 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
8650 &BTRFS_I(inode)->runtime_flags)) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008651 btrfs_info(root->fs_info, "inode %llu still on the orphan list",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02008652 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04008653 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04008654 }
Josef Bacik7b128762008-07-24 12:17:14 -04008655
Chris Masond3977122009-01-05 21:25:51 -05008656 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008657 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
8658 if (!ordered)
8659 break;
8660 else {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008661 btrfs_err(root->fs_info, "found ordered extent %llu %llu on inode cleanup",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02008662 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008663 btrfs_remove_ordered_extent(inode, ordered);
8664 btrfs_put_ordered_extent(ordered);
8665 btrfs_put_ordered_extent(ordered);
8666 }
8667 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008668 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008669 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05008670free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008671 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04008672}
8673
Al Viro45321ac2010-06-07 13:43:19 -04008674int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04008675{
8676 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04008677
Naohiro Aota6379ef92013-06-06 09:56:34 +00008678 if (root == NULL)
8679 return 1;
8680
Liu Bofa6ac872013-02-20 14:10:23 +00008681 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02008682 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04008683 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04008684 else
Al Viro45321ac2010-06-07 13:43:19 -04008685 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04008686}
8687
Sven Wegener0ee0fda2008-07-30 16:54:26 -04008688static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04008689{
8690 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
8691
8692 inode_init_once(&ei->vfs_inode);
8693}
8694
8695void btrfs_destroy_cachep(void)
8696{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10008697 /*
8698 * Make sure all delayed rcu free inodes are flushed before we
8699 * destroy cache.
8700 */
8701 rcu_barrier();
Chris Mason39279cc2007-06-12 06:35:45 -04008702 if (btrfs_inode_cachep)
8703 kmem_cache_destroy(btrfs_inode_cachep);
8704 if (btrfs_trans_handle_cachep)
8705 kmem_cache_destroy(btrfs_trans_handle_cachep);
8706 if (btrfs_transaction_cachep)
8707 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008708 if (btrfs_path_cachep)
8709 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05008710 if (btrfs_free_space_cachep)
8711 kmem_cache_destroy(btrfs_free_space_cachep);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008712 if (btrfs_delalloc_work_cachep)
8713 kmem_cache_destroy(btrfs_delalloc_work_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008714}
8715
8716int btrfs_init_cachep(void)
8717{
David Sterba837e1972012-09-07 03:00:48 -06008718 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008719 sizeof(struct btrfs_inode), 0,
8720 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04008721 if (!btrfs_inode_cachep)
8722 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008723
David Sterba837e1972012-09-07 03:00:48 -06008724 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008725 sizeof(struct btrfs_trans_handle), 0,
8726 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008727 if (!btrfs_trans_handle_cachep)
8728 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008729
David Sterba837e1972012-09-07 03:00:48 -06008730 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008731 sizeof(struct btrfs_transaction), 0,
8732 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008733 if (!btrfs_transaction_cachep)
8734 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008735
David Sterba837e1972012-09-07 03:00:48 -06008736 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008737 sizeof(struct btrfs_path), 0,
8738 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008739 if (!btrfs_path_cachep)
8740 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008741
David Sterba837e1972012-09-07 03:00:48 -06008742 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05008743 sizeof(struct btrfs_free_space), 0,
8744 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
8745 if (!btrfs_free_space_cachep)
8746 goto fail;
8747
Miao Xie8ccf6f192012-10-25 09:28:04 +00008748 btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
8749 sizeof(struct btrfs_delalloc_work), 0,
8750 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
8751 NULL);
8752 if (!btrfs_delalloc_work_cachep)
8753 goto fail;
8754
Chris Mason39279cc2007-06-12 06:35:45 -04008755 return 0;
8756fail:
8757 btrfs_destroy_cachep();
8758 return -ENOMEM;
8759}
8760
8761static int btrfs_getattr(struct vfsmount *mnt,
8762 struct dentry *dentry, struct kstat *stat)
8763{
Miao Xiedf0af1a2013-01-29 10:11:59 +00008764 u64 delalloc_bytes;
Chris Mason39279cc2007-06-12 06:35:45 -04008765 struct inode *inode = dentry->d_inode;
David Sterbafadc0d82011-11-20 07:33:38 -05008766 u32 blocksize = inode->i_sb->s_blocksize;
8767
Chris Mason39279cc2007-06-12 06:35:45 -04008768 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04008769 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05008770 stat->blksize = PAGE_CACHE_SIZE;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008771
8772 spin_lock(&BTRFS_I(inode)->lock);
8773 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
8774 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05008775 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00008776 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04008777 return 0;
8778}
8779
Chris Masond3977122009-01-05 21:25:51 -05008780static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
8781 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04008782{
8783 struct btrfs_trans_handle *trans;
8784 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008785 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04008786 struct inode *new_inode = new_dentry->d_inode;
8787 struct inode *old_inode = old_dentry->d_inode;
8788 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008789 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008790 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04008791 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08008792 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008793
Li Zefan33345d012011-04-20 10:31:50 +08008794 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04008795 return -EPERM;
8796
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008797 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08008798 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05008799 return -EXDEV;
8800
Li Zefan33345d012011-04-20 10:31:50 +08008801 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
8802 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008803 return -ENOTEMPTY;
8804
Chris Mason39279cc2007-06-12 06:35:45 -04008805 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008806 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04008807 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05008808
8809
8810 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04008811 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05008812 new_dentry->d_name.name,
8813 new_dentry->d_name.len);
8814
8815 if (ret) {
8816 if (ret == -EEXIST) {
8817 /* we shouldn't get
8818 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05308819 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05008820 return ret;
8821 }
8822 } else {
8823 /* maybe -EOVERFLOW */
8824 return ret;
8825 }
8826 }
8827 ret = 0;
8828
Chris Mason5a3f23d2009-03-31 13:27:11 -04008829 /*
Chris Mason8d875f92014-08-12 10:47:42 -07008830 * we're using rename to replace one file with another. Start IO on it
8831 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04008832 */
Chris Mason8d875f92014-08-12 10:47:42 -07008833 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04008834 filemap_flush(old_inode->i_mapping);
8835
Yan, Zheng76dda932009-09-21 16:00:26 -04008836 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08008837 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008838 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008839 /*
8840 * We want to reserve the absolute worst case amount of items. So if
8841 * both inodes are subvols and we need to unlink them then that would
8842 * require 4 item modifications, but if they are both normal inodes it
8843 * would require 5 item modifications, so we'll assume their normal
8844 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
8845 * should cover the worst case number of items we'll modify.
8846 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04008847 trans = btrfs_start_transaction(root, 11);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008848 if (IS_ERR(trans)) {
8849 ret = PTR_ERR(trans);
8850 goto out_notrans;
8851 }
Chris Mason5f39d392007-10-15 16:14:19 -04008852
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008853 if (dest != root)
8854 btrfs_record_root_in_trans(trans, dest);
8855
Yan, Zhenga5719522009-09-24 09:17:31 -04008856 ret = btrfs_set_inode_index(new_dir, &index);
8857 if (ret)
8858 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008859
Miao Xie67de1172013-12-26 13:07:06 +08008860 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08008861 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008862 /* force full log commit if subvolume involved. */
Miao Xie995946d2014-04-02 19:51:06 +08008863 btrfs_set_log_full_commit(root->fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008864 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04008865 ret = btrfs_insert_inode_ref(trans, dest,
8866 new_dentry->d_name.name,
8867 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08008868 old_ino,
8869 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04008870 if (ret)
8871 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008872 /*
8873 * this is an ugly little race, but the rename is required
8874 * to make sure that if we crash, the inode is either at the
8875 * old name or the new one. pinning the log transaction lets
8876 * us make sure we don't allow a log commit to come in after
8877 * we unlink the name but before we add the new name back in.
8878 */
8879 btrfs_pin_log_trans(root);
8880 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04008881
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008882 inode_inc_iversion(old_dir);
8883 inode_inc_iversion(new_dir);
8884 inode_inc_iversion(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008885 old_dir->i_ctime = old_dir->i_mtime = ctime;
8886 new_dir->i_ctime = new_dir->i_mtime = ctime;
8887 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04008888
Chris Mason12fcfd22009-03-24 10:24:20 -04008889 if (old_dentry->d_parent != new_dentry->d_parent)
8890 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
8891
Li Zefan33345d012011-04-20 10:31:50 +08008892 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008893 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
8894 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
8895 old_dentry->d_name.name,
8896 old_dentry->d_name.len);
8897 } else {
Al Viro92986792011-03-04 17:14:37 +00008898 ret = __btrfs_unlink_inode(trans, root, old_dir,
8899 old_dentry->d_inode,
8900 old_dentry->d_name.name,
8901 old_dentry->d_name.len);
8902 if (!ret)
8903 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008904 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008905 if (ret) {
8906 btrfs_abort_transaction(trans, root, ret);
8907 goto out_fail;
8908 }
Chris Mason39279cc2007-06-12 06:35:45 -04008909
8910 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008911 inode_inc_iversion(new_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008912 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08008913 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008914 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
8915 root_objectid = BTRFS_I(new_inode)->location.objectid;
8916 ret = btrfs_unlink_subvol(trans, dest, new_dir,
8917 root_objectid,
8918 new_dentry->d_name.name,
8919 new_dentry->d_name.len);
8920 BUG_ON(new_inode->i_nlink == 0);
8921 } else {
8922 ret = btrfs_unlink_inode(trans, dest, new_dir,
8923 new_dentry->d_inode,
8924 new_dentry->d_name.name,
8925 new_dentry->d_name.len);
8926 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04008927 if (!ret && new_inode->i_nlink == 0)
Chris Masone02119d2008-09-05 16:13:11 -04008928 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008929 if (ret) {
8930 btrfs_abort_transaction(trans, root, ret);
8931 goto out_fail;
8932 }
Chris Mason39279cc2007-06-12 06:35:45 -04008933 }
Josef Bacikaec74772008-07-24 12:12:38 -04008934
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008935 ret = btrfs_add_link(trans, new_dir, old_inode,
8936 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04008937 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008938 if (ret) {
8939 btrfs_abort_transaction(trans, root, ret);
8940 goto out_fail;
8941 }
Chris Mason39279cc2007-06-12 06:35:45 -04008942
Miao Xie67de1172013-12-26 13:07:06 +08008943 if (old_inode->i_nlink == 1)
8944 BTRFS_I(old_inode)->dir_index = index;
8945
Li Zefan33345d012011-04-20 10:31:50 +08008946 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04008947 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00008948 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008949 btrfs_end_log_trans(root);
8950 }
Chris Mason39279cc2007-06-12 06:35:45 -04008951out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008952 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008953out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08008954 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008955 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008956
Chris Mason39279cc2007-06-12 06:35:45 -04008957 return ret;
8958}
8959
Miklos Szeredi80ace852014-07-23 15:15:32 +02008960static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
8961 struct inode *new_dir, struct dentry *new_dentry,
8962 unsigned int flags)
8963{
8964 if (flags & ~RENAME_NOREPLACE)
8965 return -EINVAL;
8966
8967 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry);
8968}
8969
Miao Xie8ccf6f192012-10-25 09:28:04 +00008970static void btrfs_run_delalloc_work(struct btrfs_work *work)
8971{
8972 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04008973 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008974
8975 delalloc_work = container_of(work, struct btrfs_delalloc_work,
8976 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04008977 inode = delalloc_work->inode;
8978 if (delalloc_work->wait) {
8979 btrfs_wait_ordered_range(inode, 0, (u64)-1);
8980 } else {
8981 filemap_flush(inode->i_mapping);
8982 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8983 &BTRFS_I(inode)->runtime_flags))
8984 filemap_flush(inode->i_mapping);
8985 }
Miao Xie8ccf6f192012-10-25 09:28:04 +00008986
8987 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -04008988 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008989 else
Josef Bacik9f23e282013-10-28 15:03:41 -04008990 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008991 complete(&delalloc_work->completion);
8992}
8993
8994struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
8995 int wait, int delay_iput)
8996{
8997 struct btrfs_delalloc_work *work;
8998
8999 work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
9000 if (!work)
9001 return NULL;
9002
9003 init_completion(&work->completion);
9004 INIT_LIST_HEAD(&work->list);
9005 work->inode = inode;
9006 work->wait = wait;
9007 work->delay_iput = delay_iput;
Liu Bo9e0af232014-08-15 23:36:53 +08009008 WARN_ON_ONCE(!inode);
9009 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
9010 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009011
9012 return work;
9013}
9014
9015void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
9016{
9017 wait_for_completion(&work->completion);
9018 kmem_cache_free(btrfs_delalloc_work_cachep, work);
9019}
9020
Chris Masond352ac62008-09-29 15:18:18 -04009021/*
9022 * some fairly slow code that needs optimization. This walks the list
9023 * of all the inodes with pending delalloc and forces them to disk.
9024 */
Miao Xie6c255e62014-03-06 13:55:01 +08009025static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
9026 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -04009027{
Chris Masonea8c2812008-08-04 23:17:27 -04009028 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009029 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009030 struct btrfs_delalloc_work *work, *next;
9031 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00009032 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009033 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04009034
Miao Xie8ccf6f192012-10-25 09:28:04 +00009035 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009036 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00009037
Miao Xie573bfb72014-03-06 13:55:03 +08009038 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009039 spin_lock(&root->delalloc_lock);
9040 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009041 while (!list_empty(&splice)) {
9042 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04009043 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009044
Miao Xieeb73c1b2013-05-15 07:48:22 +00009045 list_move_tail(&binode->delalloc_inodes,
9046 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009047 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00009048 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009049 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009050 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009051 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009052 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009053
9054 work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
David Sterba5d99a9982014-09-29 19:20:37 +02009055 if (!work) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -04009056 if (delay_iput)
9057 btrfs_add_delayed_iput(inode);
9058 else
9059 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009060 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009061 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009062 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00009063 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +08009064 btrfs_queue_work(root->fs_info->flush_workers,
9065 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +08009066 ret++;
9067 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009068 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009069 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00009070 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04009071 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009072 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04009073
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009074out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00009075 list_for_each_entry_safe(work, next, &works, list) {
9076 list_del_init(&work->list);
9077 btrfs_wait_and_free_delalloc_work(work);
9078 }
9079
Miao Xieeb73c1b2013-05-15 07:48:22 +00009080 if (!list_empty_careful(&splice)) {
9081 spin_lock(&root->delalloc_lock);
9082 list_splice_tail(&splice, &root->delalloc_inodes);
9083 spin_unlock(&root->delalloc_lock);
9084 }
Miao Xie573bfb72014-03-06 13:55:03 +08009085 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009086 return ret;
9087}
9088
9089int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
9090{
9091 int ret;
9092
Wang Shilong2c21b4d2014-01-14 19:42:20 +08009093 if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009094 return -EROFS;
9095
Miao Xie6c255e62014-03-06 13:55:01 +08009096 ret = __start_delalloc_inodes(root, delay_iput, -1);
9097 if (ret > 0)
9098 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009099 /*
9100 * the filemap_flush will queue IO into the worker threads, but
Chris Mason8c8bee12008-09-29 11:19:10 -04009101 * we have to make sure the IO is actually started and that
9102 * ordered extents get created before we return
9103 */
9104 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05009105 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05009106 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04009107 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05009108 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
9109 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04009110 }
9111 atomic_dec(&root->fs_info->async_submit_draining);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009112 return ret;
9113}
9114
Miao Xie6c255e62014-03-06 13:55:01 +08009115int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
9116 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009117{
9118 struct btrfs_root *root;
9119 struct list_head splice;
9120 int ret;
9121
Wang Shilong2c21b4d2014-01-14 19:42:20 +08009122 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009123 return -EROFS;
9124
9125 INIT_LIST_HEAD(&splice);
9126
Miao Xie573bfb72014-03-06 13:55:03 +08009127 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009128 spin_lock(&fs_info->delalloc_root_lock);
9129 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +08009130 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009131 root = list_first_entry(&splice, struct btrfs_root,
9132 delalloc_root);
9133 root = btrfs_grab_fs_root(root);
9134 BUG_ON(!root);
9135 list_move_tail(&root->delalloc_root,
9136 &fs_info->delalloc_roots);
9137 spin_unlock(&fs_info->delalloc_root_lock);
9138
Miao Xie6c255e62014-03-06 13:55:01 +08009139 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009140 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +08009141 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009142 goto out;
9143
Miao Xie6c255e62014-03-06 13:55:01 +08009144 if (nr != -1) {
9145 nr -= ret;
9146 WARN_ON(nr < 0);
9147 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009148 spin_lock(&fs_info->delalloc_root_lock);
9149 }
9150 spin_unlock(&fs_info->delalloc_root_lock);
9151
Miao Xie6c255e62014-03-06 13:55:01 +08009152 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009153 atomic_inc(&fs_info->async_submit_draining);
9154 while (atomic_read(&fs_info->nr_async_submits) ||
9155 atomic_read(&fs_info->async_delalloc_pages)) {
9156 wait_event(fs_info->async_submit_wait,
9157 (atomic_read(&fs_info->nr_async_submits) == 0 &&
9158 atomic_read(&fs_info->async_delalloc_pages) == 0));
9159 }
9160 atomic_dec(&fs_info->async_submit_draining);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009161out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00009162 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009163 spin_lock(&fs_info->delalloc_root_lock);
9164 list_splice_tail(&splice, &fs_info->delalloc_roots);
9165 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009166 }
Miao Xie573bfb72014-03-06 13:55:03 +08009167 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009168 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04009169}
9170
Chris Mason39279cc2007-06-12 06:35:45 -04009171static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
9172 const char *symname)
9173{
9174 struct btrfs_trans_handle *trans;
9175 struct btrfs_root *root = BTRFS_I(dir)->root;
9176 struct btrfs_path *path;
9177 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05009178 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04009179 int err;
9180 int drop_inode = 0;
9181 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05309182 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009183 int name_len;
9184 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04009185 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04009186 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04009187 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04009188
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01009189 name_len = strlen(symname);
Chris Mason39279cc2007-06-12 06:35:45 -04009190 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
9191 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05009192
Josef Bacik9ed74f22009-09-11 16:12:44 -04009193 /*
9194 * 2 items for inode item and ref
9195 * 2 items for dir items
9196 * 1 item for xattr if selinux is on
9197 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04009198 trans = btrfs_start_transaction(root, 5);
9199 if (IS_ERR(trans))
9200 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05009201
Li Zefan581bb052011-04-20 10:06:11 +08009202 err = btrfs_find_free_ino(root, &objectid);
9203 if (err)
9204 goto out_unlock;
9205
Josef Bacikaec74772008-07-24 12:12:38 -04009206 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08009207 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04009208 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009209 if (IS_ERR(inode)) {
9210 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009211 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009212 }
Chris Mason39279cc2007-06-12 06:35:45 -04009213
Casey Schauflerad19db72011-12-15 10:09:07 -05009214 /*
9215 * If the active LSM wants to access the inode during
9216 * d_instantiate it needs these. Smack checks to see
9217 * if the filesystem supports xattrs by looking at the
9218 * ops vector.
9219 */
9220 inode->i_fop = &btrfs_file_operations;
9221 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07009222 inode->i_mapping->a_ops = &btrfs_aops;
9223 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
9224 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
9225
9226 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
9227 if (err)
9228 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05009229
Josef Bacika1b075d2010-11-19 20:36:11 +00009230 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04009231 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07009232 goto out_unlock_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009233
9234 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07009235 if (!path) {
9236 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -07009237 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -07009238 }
Li Zefan33345d012011-04-20 10:31:50 +08009239 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009240 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02009241 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04009242 datasize = btrfs_file_extent_calc_inline_size(name_len);
9243 err = btrfs_insert_empty_item(trans, root, path, &key,
9244 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04009245 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +00009246 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -07009247 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -04009248 }
Chris Mason5f39d392007-10-15 16:14:19 -04009249 leaf = path->nodes[0];
9250 ei = btrfs_item_ptr(leaf, path->slots[0],
9251 struct btrfs_file_extent_item);
9252 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
9253 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04009254 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04009255 btrfs_set_file_extent_encryption(leaf, ei, 0);
9256 btrfs_set_file_extent_compression(leaf, ei, 0);
9257 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
9258 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
9259
Chris Mason39279cc2007-06-12 06:35:45 -04009260 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04009261 write_extent_buffer(leaf, symname, ptr, name_len);
9262 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04009263 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04009264
Chris Mason39279cc2007-06-12 06:35:45 -04009265 inode->i_op = &btrfs_symlink_inode_operations;
9266 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04009267 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04009268 inode_set_bytes(inode, name_len);
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01009269 btrfs_i_size_write(inode, name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -04009270 err = btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07009271 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -04009272 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07009273 goto out_unlock_inode;
9274 }
9275
9276 unlock_new_inode(inode);
9277 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009278
9279out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05009280 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04009281 if (drop_inode) {
9282 inode_dec_link_count(inode);
9283 iput(inode);
9284 }
Liu Bob53d3f52012-11-14 14:34:34 +00009285 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04009286 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07009287
9288out_unlock_inode:
9289 drop_inode = 1;
9290 unlock_new_inode(inode);
9291 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -04009292}
Chris Mason16432982008-04-10 10:23:21 -04009293
Josef Bacik0af3d002010-06-21 14:48:16 -04009294static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
9295 u64 start, u64 num_bytes, u64 min_size,
9296 loff_t actual_len, u64 *alloc_hint,
9297 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04009298{
Josef Bacik5dc562c2012-08-17 13:14:17 -04009299 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
9300 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04009301 struct btrfs_root *root = BTRFS_I(inode)->root;
9302 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04009303 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00009304 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05009305 u64 cur_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04009306 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04009307 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04009308
Josef Bacik0af3d002010-06-21 14:48:16 -04009309 if (trans)
9310 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04009311 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04009312 if (own_trans) {
9313 trans = btrfs_start_transaction(root, 3);
9314 if (IS_ERR(trans)) {
9315 ret = PTR_ERR(trans);
9316 break;
9317 }
Yan Zhengd899e052008-10-30 14:25:28 -04009318 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00009319
Chris Mason154ea282013-03-05 11:11:26 -05009320 cur_bytes = min(num_bytes, 256ULL * 1024 * 1024);
9321 cur_bytes = max(cur_bytes, min_size);
Josef Bacik00361582013-08-14 14:02:47 -04009322 ret = btrfs_reserve_extent(root, cur_bytes, min_size, 0,
Miao Xiee570fd22014-06-19 10:42:50 +08009323 *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009324 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04009325 if (own_trans)
9326 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009327 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009328 }
9329
Yan Zhengd899e052008-10-30 14:25:28 -04009330 ret = insert_reserved_file_extent(trans, inode,
9331 cur_offset, ins.objectid,
9332 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00009333 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04009334 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009335 if (ret) {
Josef Bacik857cc2f2013-10-07 15:21:08 -04009336 btrfs_free_reserved_extent(root, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +08009337 ins.offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009338 btrfs_abort_transaction(trans, root, ret);
9339 if (own_trans)
9340 btrfs_end_transaction(trans, root);
9341 break;
9342 }
Chris Masona1ed8352009-09-11 12:27:37 -04009343 btrfs_drop_extent_cache(inode, cur_offset,
9344 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009345
Josef Bacik5dc562c2012-08-17 13:14:17 -04009346 em = alloc_extent_map();
9347 if (!em) {
9348 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
9349 &BTRFS_I(inode)->runtime_flags);
9350 goto next;
9351 }
9352
9353 em->start = cur_offset;
9354 em->orig_start = cur_offset;
9355 em->len = ins.offset;
9356 em->block_start = ins.objectid;
9357 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05009358 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04009359 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009360 em->bdev = root->fs_info->fs_devices->latest_bdev;
9361 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
9362 em->generation = trans->transid;
9363
9364 while (1) {
9365 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04009366 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04009367 write_unlock(&em_tree->lock);
9368 if (ret != -EEXIST)
9369 break;
9370 btrfs_drop_extent_cache(inode, cur_offset,
9371 cur_offset + ins.offset - 1,
9372 0);
9373 }
9374 free_extent_map(em);
9375next:
Yan Zhengd899e052008-10-30 14:25:28 -04009376 num_bytes -= ins.offset;
9377 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04009378 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009379
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009380 inode_inc_iversion(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04009381 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02009382 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04009383 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04009384 (actual_len > inode->i_size) &&
9385 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009386 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00009387 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009388 else
Josef Bacik55a61d12010-11-22 18:50:32 +00009389 i_size = cur_offset;
9390 i_size_write(inode, i_size);
9391 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009392 }
9393
Yan Zhengd899e052008-10-30 14:25:28 -04009394 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009395
9396 if (ret) {
9397 btrfs_abort_transaction(trans, root, ret);
9398 if (own_trans)
9399 btrfs_end_transaction(trans, root);
9400 break;
9401 }
Yan Zhengd899e052008-10-30 14:25:28 -04009402
Josef Bacik0af3d002010-06-21 14:48:16 -04009403 if (own_trans)
9404 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009405 }
Yan Zhengd899e052008-10-30 14:25:28 -04009406 return ret;
9407}
9408
Josef Bacik0af3d002010-06-21 14:48:16 -04009409int btrfs_prealloc_file_range(struct inode *inode, int mode,
9410 u64 start, u64 num_bytes, u64 min_size,
9411 loff_t actual_len, u64 *alloc_hint)
9412{
9413 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9414 min_size, actual_len, alloc_hint,
9415 NULL);
9416}
9417
9418int btrfs_prealloc_file_range_trans(struct inode *inode,
9419 struct btrfs_trans_handle *trans, int mode,
9420 u64 start, u64 num_bytes, u64 min_size,
9421 loff_t actual_len, u64 *alloc_hint)
9422{
9423 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9424 min_size, actual_len, alloc_hint, trans);
9425}
9426
Chris Masone6dcd2d2008-07-17 12:53:50 -04009427static int btrfs_set_page_dirty(struct page *page)
9428{
Chris Masone6dcd2d2008-07-17 12:53:50 -04009429 return __set_page_dirty_nobuffers(page);
9430}
9431
Al Viro10556cb2011-06-20 19:28:19 -04009432static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05009433{
Li Zefanb83cc962010-12-20 16:04:08 +08009434 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009435 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08009436
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009437 if (mask & MAY_WRITE &&
9438 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
9439 if (btrfs_root_readonly(root))
9440 return -EROFS;
9441 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
9442 return -EACCES;
9443 }
Al Viro2830ba72011-06-20 19:16:29 -04009444 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05009445}
Chris Mason39279cc2007-06-12 06:35:45 -04009446
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009447static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
9448{
9449 struct btrfs_trans_handle *trans;
9450 struct btrfs_root *root = BTRFS_I(dir)->root;
9451 struct inode *inode = NULL;
9452 u64 objectid;
9453 u64 index;
9454 int ret = 0;
9455
9456 /*
9457 * 5 units required for adding orphan entry
9458 */
9459 trans = btrfs_start_transaction(root, 5);
9460 if (IS_ERR(trans))
9461 return PTR_ERR(trans);
9462
9463 ret = btrfs_find_free_ino(root, &objectid);
9464 if (ret)
9465 goto out;
9466
9467 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
9468 btrfs_ino(dir), objectid, mode, &index);
9469 if (IS_ERR(inode)) {
9470 ret = PTR_ERR(inode);
9471 inode = NULL;
9472 goto out;
9473 }
9474
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009475 inode->i_fop = &btrfs_file_operations;
9476 inode->i_op = &btrfs_file_inode_operations;
9477
9478 inode->i_mapping->a_ops = &btrfs_aops;
9479 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
9480 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
9481
Chris Masonb0d5d102014-09-08 13:08:51 -07009482 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
9483 if (ret)
9484 goto out_inode;
9485
9486 ret = btrfs_update_inode(trans, root, inode);
9487 if (ret)
9488 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009489 ret = btrfs_orphan_add(trans, inode);
9490 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -07009491 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009492
Filipe Manana5762b5c2014-08-01 00:10:32 +01009493 /*
9494 * We set number of links to 0 in btrfs_new_inode(), and here we set
9495 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
9496 * through:
9497 *
9498 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
9499 */
9500 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -07009501 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009502 d_tmpfile(dentry, inode);
9503 mark_inode_dirty(inode);
9504
9505out:
9506 btrfs_end_transaction(trans, root);
9507 if (ret)
9508 iput(inode);
9509 btrfs_balance_delayed_items(root);
9510 btrfs_btree_balance_dirty(root);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009511 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -07009512
9513out_inode:
9514 unlock_new_inode(inode);
9515 goto out;
9516
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009517}
9518
Filipe Mananab38ef712014-11-13 17:01:45 +00009519/* Inspired by filemap_check_errors() */
9520int btrfs_inode_check_errors(struct inode *inode)
9521{
9522 int ret = 0;
9523
9524 if (test_bit(AS_ENOSPC, &inode->i_mapping->flags) &&
9525 test_and_clear_bit(AS_ENOSPC, &inode->i_mapping->flags))
9526 ret = -ENOSPC;
9527 if (test_bit(AS_EIO, &inode->i_mapping->flags) &&
9528 test_and_clear_bit(AS_EIO, &inode->i_mapping->flags))
9529 ret = -EIO;
9530
9531 return ret;
9532}
9533
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009534static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05009535 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04009536 .lookup = btrfs_lookup,
9537 .create = btrfs_create,
9538 .unlink = btrfs_unlink,
9539 .link = btrfs_link,
9540 .mkdir = btrfs_mkdir,
9541 .rmdir = btrfs_rmdir,
Miklos Szeredi80ace852014-07-23 15:15:32 +02009542 .rename2 = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -04009543 .symlink = btrfs_symlink,
9544 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04009545 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009546 .setxattr = btrfs_setxattr,
9547 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05009548 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009549 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05009550 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009551 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009552 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -07009553 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009554 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -04009555};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009556static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009557 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05009558 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009559 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009560 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -07009561 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04009562};
Yan, Zheng76dda932009-09-21 16:00:26 -04009563
Alexey Dobriyan828c0952009-10-01 15:43:56 -07009564static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009565 .llseek = generic_file_llseek,
9566 .read = generic_read_dir,
Al Viro9cdda8d2013-05-22 16:48:09 -04009567 .iterate = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04009568 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04009569#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04009570 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04009571#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04009572 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04009573 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04009574};
9575
Chris Masond1310b22008-01-24 16:13:08 -05009576static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04009577 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05009578 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04009579 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04009580 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04009581 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04009582 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05009583 .set_bit_hook = btrfs_set_bit_hook,
9584 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04009585 .merge_extent_hook = btrfs_merge_extent_hook,
9586 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04009587};
9588
Chris Mason35054392009-01-21 13:11:13 -05009589/*
9590 * btrfs doesn't support the bmap operation because swapfiles
9591 * use bmap to make a mapping of extents in the file. They assume
9592 * these extents won't change over the life of the file and they
9593 * use the bmap result to do IO directly to the drive.
9594 *
9595 * the btrfs bmap call would return logical addresses that aren't
9596 * suitable for IO and they also will change frequently as COW
9597 * operations happen. So, swapfile + btrfs == corruption.
9598 *
9599 * For now we're avoiding this by dropping bmap.
9600 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07009601static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04009602 .readpage = btrfs_readpage,
9603 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04009604 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05009605 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04009606 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04009607 .invalidatepage = btrfs_invalidatepage,
9608 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04009609 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02009610 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04009611};
9612
Alexey Dobriyan7f094102009-09-21 17:01:10 -07009613static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04009614 .readpage = btrfs_readpage,
9615 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04009616 .invalidatepage = btrfs_invalidatepage,
9617 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04009618};
9619
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009620static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009621 .getattr = btrfs_getattr,
9622 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009623 .setxattr = btrfs_setxattr,
9624 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05009625 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009626 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05009627 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05009628 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009629 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009630 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04009631 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04009632};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009633static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04009634 .getattr = btrfs_getattr,
9635 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05009636 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009637 .setxattr = btrfs_setxattr,
9638 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04009639 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009640 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009641 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009642 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04009643 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -04009644};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009645static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009646 .readlink = generic_readlink,
9647 .follow_link = page_follow_link_light,
9648 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00009649 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -05009650 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05009651 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05009652 .setxattr = btrfs_setxattr,
9653 .getxattr = btrfs_getxattr,
9654 .listxattr = btrfs_listxattr,
9655 .removexattr = btrfs_removexattr,
Josef Bacike41f9412012-03-26 09:46:47 -04009656 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04009657};
Yan, Zheng76dda932009-09-21 16:00:26 -04009658
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04009659const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04009660 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -04009661 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -04009662};