blob: 177508abb865d433be19acd96813bdb15f1da157 [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 ||
Chris Mason70b99e62008-10-31 12:46:39 -0400252 actual_end >= PAGE_CACHE_SIZE ||
Chris Masonc8b97812008-10-29 14:49:59 -0400253 data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
254 (!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
Chris Masonc8b97812008-10-29 14:49:59 -0400351/*
Chris Mason771ed682008-11-06 22:02:51 -0500352 * we create compressed extents in two phases. The first
353 * phase compresses a range of pages that have already been
354 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400355 *
Chris Mason771ed682008-11-06 22:02:51 -0500356 * This is done inside an ordered work queue, and the compression
357 * is spread across many cpus. The actual IO submission is step
358 * two, and the ordered work queue takes care of making sure that
359 * happens in the same order things were put onto the queue by
360 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400361 *
Chris Mason771ed682008-11-06 22:02:51 -0500362 * If this code finds it can't get good compression, it puts an
363 * entry onto the work queue to write the uncompressed bytes. This
364 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300365 * are written in the same order that the flusher thread sent them
366 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400367 */
Chris Mason771ed682008-11-06 22:02:51 -0500368static noinline int compress_file_range(struct inode *inode,
369 struct page *locked_page,
370 u64 start, u64 end,
371 struct async_cow *async_cow,
372 int *num_added)
Chris Masonb888db22007-08-27 16:49:44 -0400373{
374 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masondb945352007-10-15 16:15:53 -0400375 u64 num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400376 u64 blocksize = root->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400377 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500378 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400379 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400380 struct page **pages = NULL;
381 unsigned long nr_pages;
382 unsigned long nr_pages_ret = 0;
383 unsigned long total_compressed = 0;
384 unsigned long total_in = 0;
385 unsigned long max_compressed = 128 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -0500386 unsigned long max_uncompressed = 128 * 1024;
Chris Masonc8b97812008-10-29 14:49:59 -0400387 int i;
388 int will_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800389 int compress_type = root->fs_info->compress_type;
Chris Mason4adaa612013-03-26 13:07:00 -0400390 int redirty = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400391
Liu Bo4cb13e52012-03-29 09:57:45 -0400392 /* if this is a small write inside eof, kick off a defrag */
393 if ((end - start + 1) < 16 * 1024 &&
394 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400395 btrfs_add_inode_defrag(NULL, inode);
396
Wang Shilong68bb4622014-04-01 18:01:42 +0800397 /*
398 * skip compression for a small file range(<=blocksize) that
399 * isn't an inline extent, since it dosen't save disk space at all.
400 */
401 if ((end - start + 1) <= blocksize &&
402 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
403 goto cleanup_and_bail_uncompressed;
404
Chris Mason42dc7ba2008-12-15 11:44:56 -0500405 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400406again:
407 will_compress = 0;
408 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
409 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
410
Chris Masonf03d9301f2009-02-04 09:31:06 -0500411 /*
412 * we don't want to send crud past the end of i_size through
413 * compression, that's just a waste of CPU time. So, if the
414 * end of the file is before the start of our current
415 * requested range of bytes, we bail out to the uncompressed
416 * cleanup code that can deal with all of this.
417 *
418 * It isn't really the fastest way to fix things, but this is a
419 * very uncommon corner.
420 */
421 if (actual_end <= start)
422 goto cleanup_and_bail_uncompressed;
423
Chris Masonc8b97812008-10-29 14:49:59 -0400424 total_compressed = actual_end - start;
425
426 /* we want to make sure that amount of ram required to uncompress
427 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500428 * of a compressed extent to 128k. This is a crucial number
429 * because it also controls how easily we can spread reads across
430 * cpus for decompression.
431 *
432 * We also want to make sure the amount of IO required to do
433 * a random read is reasonably small, so we limit the size of
434 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400435 */
436 total_compressed = min(total_compressed, max_uncompressed);
Qu Wenruofda28322013-02-26 08:10:22 +0000437 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Masonbe20aa92007-12-17 20:14:01 -0500438 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400439 total_in = 0;
440 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400441
Chris Mason771ed682008-11-06 22:02:51 -0500442 /*
443 * we do compression for mount -o compress and when the
444 * inode has not been flagged as nocompress. This flag can
445 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400446 */
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200447 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) &&
Chris Mason1e701a32010-03-11 09:42:04 -0500448 (btrfs_test_opt(root, COMPRESS) ||
Liu Bo75e7cb72011-03-22 10:12:20 +0000449 (BTRFS_I(inode)->force_compress) ||
450 (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS))) {
Chris Masonc8b97812008-10-29 14:49:59 -0400451 WARN_ON(pages);
Chris Masoncfbc2462008-10-30 13:22:14 -0400452 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800453 if (!pages) {
454 /* just bail out to the uncompressed code */
455 goto cont;
456 }
Chris Mason179e29e2007-11-01 11:28:41 -0400457
Li Zefan261507a02010-12-17 14:21:50 +0800458 if (BTRFS_I(inode)->force_compress)
459 compress_type = BTRFS_I(inode)->force_compress;
460
Chris Mason4adaa612013-03-26 13:07:00 -0400461 /*
462 * we need to call clear_page_dirty_for_io on each
463 * page in the range. Otherwise applications with the file
464 * mmap'd can wander in and change the page contents while
465 * we are compressing them.
466 *
467 * If the compression fails for any reason, we set the pages
468 * dirty again later on.
469 */
470 extent_range_clear_dirty_for_io(inode, start, end);
471 redirty = 1;
Li Zefan261507a02010-12-17 14:21:50 +0800472 ret = btrfs_compress_pages(compress_type,
473 inode->i_mapping, start,
474 total_compressed, pages,
475 nr_pages, &nr_pages_ret,
476 &total_in,
477 &total_compressed,
478 max_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400479
480 if (!ret) {
481 unsigned long offset = total_compressed &
482 (PAGE_CACHE_SIZE - 1);
483 struct page *page = pages[nr_pages_ret - 1];
484 char *kaddr;
485
486 /* zero the tail end of the last page, we might be
487 * sending it down to disk
488 */
489 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800490 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400491 memset(kaddr + offset, 0,
492 PAGE_CACHE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800493 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400494 }
495 will_compress = 1;
496 }
497 }
Li Zefan560f7d72011-09-08 10:22:01 +0800498cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400499 if (start == 0) {
500 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500501 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400502 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500503 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400504 */
Josef Bacik00361582013-08-14 14:02:47 -0400505 ret = cow_file_range_inline(root, inode, start, end,
506 0, 0, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400507 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500508 /* try making a compressed inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400509 ret = cow_file_range_inline(root, inode, start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000510 total_compressed,
511 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400512 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100513 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400514 unsigned long clear_flags = EXTENT_DELALLOC |
515 EXTENT_DEFRAG;
516 clear_flags |= (ret < 0) ? EXTENT_DO_ACCOUNTING : 0;
517
Chris Mason771ed682008-11-06 22:02:51 -0500518 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100519 * inline extent creation worked or returned error,
520 * we don't need to create any more async work items.
521 * Unlock and free up our temp pages.
Chris Mason771ed682008-11-06 22:02:51 -0500522 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400523 extent_clear_unlock_delalloc(inode, start, end, NULL,
Josef Bacik151a41b2013-07-29 13:22:24 -0400524 clear_flags, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400525 PAGE_CLEAR_DIRTY |
526 PAGE_SET_WRITEBACK |
527 PAGE_END_WRITEBACK);
Chris Masonc8b97812008-10-29 14:49:59 -0400528 goto free_pages_out;
529 }
530 }
531
532 if (will_compress) {
533 /*
534 * we aren't doing an inline extent round the compressed size
535 * up to a block size boundary so the allocator does sane
536 * things
537 */
Qu Wenruofda28322013-02-26 08:10:22 +0000538 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400539
540 /*
541 * one last check to make sure the compression is really a
542 * win, compare the page count read with the blocks on disk
543 */
Qu Wenruofda28322013-02-26 08:10:22 +0000544 total_in = ALIGN(total_in, PAGE_CACHE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400545 if (total_compressed >= total_in) {
546 will_compress = 0;
547 } else {
Chris Masonc8b97812008-10-29 14:49:59 -0400548 num_bytes = total_in;
549 }
550 }
551 if (!will_compress && pages) {
552 /*
553 * the compression code ran but failed to make things smaller,
554 * free any pages it allocated and our page pointer array
555 */
556 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400557 WARN_ON(pages[i]->mapping);
Chris Masonc8b97812008-10-29 14:49:59 -0400558 page_cache_release(pages[i]);
559 }
560 kfree(pages);
561 pages = NULL;
562 total_compressed = 0;
563 nr_pages_ret = 0;
564
565 /* flag the file so we don't compress in the future */
Chris Mason1e701a32010-03-11 09:42:04 -0500566 if (!btrfs_test_opt(root, FORCE_COMPRESS) &&
567 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500568 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500569 }
Chris Masonc8b97812008-10-29 14:49:59 -0400570 }
Chris Mason771ed682008-11-06 22:02:51 -0500571 if (will_compress) {
572 *num_added += 1;
573
574 /* the async work queues will take care of doing actual
575 * allocation on disk for these compressed pages,
576 * and will submit them to the elevator.
577 */
578 add_async_extent(async_cow, start, num_bytes,
Li Zefan261507a02010-12-17 14:21:50 +0800579 total_compressed, pages, nr_pages_ret,
580 compress_type);
Chris Mason771ed682008-11-06 22:02:51 -0500581
Yan, Zheng24ae6362010-12-06 07:02:36 +0000582 if (start + num_bytes < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500583 start += num_bytes;
584 pages = NULL;
585 cond_resched();
586 goto again;
587 }
588 } else {
Chris Masonf03d9301f2009-02-04 09:31:06 -0500589cleanup_and_bail_uncompressed:
Chris Mason771ed682008-11-06 22:02:51 -0500590 /*
591 * No compression, but we still need to write the pages in
592 * the file we've been given so far. redirty the locked
593 * page if it corresponds to our extent and set things up
594 * for the async work queue to run cow_file_range to do
595 * the normal delalloc dance
596 */
597 if (page_offset(locked_page) >= start &&
598 page_offset(locked_page) <= end) {
599 __set_page_dirty_nobuffers(locked_page);
600 /* unlocked later on in the async handlers */
601 }
Chris Mason4adaa612013-03-26 13:07:00 -0400602 if (redirty)
603 extent_range_redirty_for_io(inode, start, end);
Li Zefan261507a02010-12-17 14:21:50 +0800604 add_async_extent(async_cow, start, end - start + 1,
605 0, NULL, 0, BTRFS_COMPRESS_NONE);
Chris Mason771ed682008-11-06 22:02:51 -0500606 *num_added += 1;
607 }
608
609out:
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100610 return ret;
Chris Mason771ed682008-11-06 22:02:51 -0500611
612free_pages_out:
613 for (i = 0; i < nr_pages_ret; i++) {
614 WARN_ON(pages[i]->mapping);
615 page_cache_release(pages[i]);
616 }
Chris Masond3977122009-01-05 21:25:51 -0500617 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500618
619 goto out;
620}
621
622/*
623 * phase two of compressed writeback. This is the ordered portion
624 * of the code, which only gets called in the order the work was
625 * queued. We walk all the async extents created by compress_file_range
626 * and send them down to the disk.
627 */
628static noinline int submit_compressed_extents(struct inode *inode,
629 struct async_cow *async_cow)
630{
631 struct async_extent *async_extent;
632 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500633 struct btrfs_key ins;
634 struct extent_map *em;
635 struct btrfs_root *root = BTRFS_I(inode)->root;
636 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
637 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500638 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500639
640 if (list_empty(&async_cow->extents))
641 return 0;
642
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500643again:
Chris Masond3977122009-01-05 21:25:51 -0500644 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500645 async_extent = list_entry(async_cow->extents.next,
646 struct async_extent, list);
647 list_del(&async_extent->list);
648
649 io_tree = &BTRFS_I(inode)->io_tree;
650
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500651retry:
Chris Mason771ed682008-11-06 22:02:51 -0500652 /* did the compression code fall back to uncompressed IO? */
653 if (!async_extent->pages) {
654 int page_started = 0;
655 unsigned long nr_written = 0;
656
657 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000658 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100659 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500660
661 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500662 ret = cow_file_range(inode, async_cow->locked_page,
663 async_extent->start,
664 async_extent->start +
665 async_extent->ram_size - 1,
666 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500667
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100668 /* JDM XXX */
669
Chris Mason771ed682008-11-06 22:02:51 -0500670 /*
671 * if page_started, cow_file_range inserted an
672 * inline extent and took care of all the unlocking
673 * and IO for us. Otherwise, we need to submit
674 * all those pages down to the drive.
675 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500676 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500677 extent_write_locked_range(io_tree,
678 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500679 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500680 async_extent->ram_size - 1,
681 btrfs_get_extent,
682 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500683 else if (ret)
684 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500685 kfree(async_extent);
686 cond_resched();
687 continue;
688 }
689
690 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100691 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500692
Josef Bacik00361582013-08-14 14:02:47 -0400693 ret = btrfs_reserve_extent(root,
Chris Mason771ed682008-11-06 22:02:51 -0500694 async_extent->compressed_size,
695 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800696 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500697 if (ret) {
698 int i;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500699
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500700 for (i = 0; i < async_extent->nr_pages; i++) {
701 WARN_ON(async_extent->pages[i]->mapping);
702 page_cache_release(async_extent->pages[i]);
703 }
704 kfree(async_extent->pages);
705 async_extent->nr_pages = 0;
706 async_extent->pages = NULL;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500707
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400708 if (ret == -ENOSPC) {
709 unlock_extent(io_tree, async_extent->start,
710 async_extent->start +
711 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800712
713 /*
714 * we need to redirty the pages if we decide to
715 * fallback to uncompressed IO, otherwise we
716 * will not submit these pages down to lower
717 * layers.
718 */
719 extent_range_redirty_for_io(inode,
720 async_extent->start,
721 async_extent->start +
722 async_extent->ram_size - 1);
723
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100724 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400725 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500726 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500727 }
728
Yan, Zhengc2167752009-11-12 09:34:21 +0000729 /*
730 * here we're doing allocation and writeback of the
731 * compressed pages
732 */
733 btrfs_drop_extent_cache(inode, async_extent->start,
734 async_extent->start +
735 async_extent->ram_size - 1, 0);
736
David Sterba172ddd62011-04-21 00:48:27 +0200737 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000738 if (!em) {
739 ret = -ENOMEM;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500740 goto out_free_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000741 }
Chris Mason771ed682008-11-06 22:02:51 -0500742 em->start = async_extent->start;
743 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500744 em->orig_start = em->start;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400745 em->mod_start = em->start;
746 em->mod_len = em->len;
Chris Mason771ed682008-11-06 22:02:51 -0500747
748 em->block_start = ins.objectid;
749 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500750 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400751 em->ram_bytes = async_extent->ram_size;
Chris Mason771ed682008-11-06 22:02:51 -0500752 em->bdev = root->fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800753 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500754 set_bit(EXTENT_FLAG_PINNED, &em->flags);
755 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400756 em->generation = -1;
Chris Mason771ed682008-11-06 22:02:51 -0500757
Chris Masond3977122009-01-05 21:25:51 -0500758 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400759 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400760 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400761 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500762 if (ret != -EEXIST) {
763 free_extent_map(em);
764 break;
765 }
766 btrfs_drop_extent_cache(inode, async_extent->start,
767 async_extent->start +
768 async_extent->ram_size - 1, 0);
769 }
770
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500771 if (ret)
772 goto out_free_reserve;
773
Li Zefan261507a02010-12-17 14:21:50 +0800774 ret = btrfs_add_ordered_extent_compress(inode,
775 async_extent->start,
776 ins.objectid,
777 async_extent->ram_size,
778 ins.offset,
779 BTRFS_ORDERED_COMPRESSED,
780 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100781 if (ret) {
782 btrfs_drop_extent_cache(inode, async_extent->start,
783 async_extent->start +
784 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500785 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100786 }
Chris Mason771ed682008-11-06 22:02:51 -0500787
Chris Mason771ed682008-11-06 22:02:51 -0500788 /*
789 * clear dirty, set writeback and unlock the pages.
790 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400791 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400792 async_extent->start +
793 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400794 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
795 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400796 PAGE_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500797 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500798 async_extent->start,
799 async_extent->ram_size,
800 ins.objectid,
801 ins.offset, async_extent->pages,
802 async_extent->nr_pages);
Chris Mason771ed682008-11-06 22:02:51 -0500803 alloc_hint = ins.objectid + ins.offset;
804 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500805 if (ret)
806 goto out;
Chris Mason771ed682008-11-06 22:02:51 -0500807 cond_resched();
808 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100809 ret = 0;
810out:
811 return ret;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500812out_free_reserve:
Miao Xiee570fd22014-06-19 10:42:50 +0800813 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100814out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400815 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500816 async_extent->start +
817 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400818 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400819 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
820 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
821 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100822 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500823 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500824}
825
Josef Bacik4b46fce2010-05-23 11:00:55 -0400826static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
827 u64 num_bytes)
828{
829 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
830 struct extent_map *em;
831 u64 alloc_hint = 0;
832
833 read_lock(&em_tree->lock);
834 em = search_extent_mapping(em_tree, start, num_bytes);
835 if (em) {
836 /*
837 * if block start isn't an actual block number then find the
838 * first block in this inode and use that as a hint. If that
839 * block is also bogus then just don't worry about it.
840 */
841 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
842 free_extent_map(em);
843 em = search_extent_mapping(em_tree, 0, 0);
844 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
845 alloc_hint = em->block_start;
846 if (em)
847 free_extent_map(em);
848 } else {
849 alloc_hint = em->block_start;
850 free_extent_map(em);
851 }
852 }
853 read_unlock(&em_tree->lock);
854
855 return alloc_hint;
856}
857
Chris Mason771ed682008-11-06 22:02:51 -0500858/*
859 * when extent_io.c finds a delayed allocation range in the file,
860 * the call backs end up in this code. The basic idea is to
861 * allocate extents on disk for the range, and create ordered data structs
862 * in ram to track those extents.
863 *
864 * locked_page is the page that writepage had locked already. We use
865 * it to make sure we don't do extra locks or unlocks.
866 *
867 * *page_started is set to one if we unlock locked_page and do everything
868 * required to start IO on it. It may be clean and already done with
869 * IO when we return.
870 */
Josef Bacik00361582013-08-14 14:02:47 -0400871static noinline int cow_file_range(struct inode *inode,
872 struct page *locked_page,
873 u64 start, u64 end, int *page_started,
874 unsigned long *nr_written,
875 int unlock)
Chris Mason771ed682008-11-06 22:02:51 -0500876{
Josef Bacik00361582013-08-14 14:02:47 -0400877 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500878 u64 alloc_hint = 0;
879 u64 num_bytes;
880 unsigned long ram_size;
881 u64 disk_num_bytes;
882 u64 cur_alloc_size;
883 u64 blocksize = root->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500884 struct btrfs_key ins;
885 struct extent_map *em;
886 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
887 int ret = 0;
888
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400889 if (btrfs_is_free_space_inode(inode)) {
890 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500891 ret = -EINVAL;
892 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400893 }
Chris Mason771ed682008-11-06 22:02:51 -0500894
Qu Wenruofda28322013-02-26 08:10:22 +0000895 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500896 num_bytes = max(blocksize, num_bytes);
897 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500898
Chris Mason4cb53002011-05-24 15:35:30 -0400899 /* if this is a small write inside eof, kick off defrag */
Liu Bo4cb13e52012-03-29 09:57:45 -0400900 if (num_bytes < 64 * 1024 &&
901 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Josef Bacik00361582013-08-14 14:02:47 -0400902 btrfs_add_inode_defrag(NULL, inode);
Chris Mason4cb53002011-05-24 15:35:30 -0400903
Chris Mason771ed682008-11-06 22:02:51 -0500904 if (start == 0) {
905 /* lets try to make an inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400906 ret = cow_file_range_inline(root, inode, start, end, 0, 0,
907 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500908 if (ret == 0) {
Josef Bacikc2790a22013-07-29 11:20:47 -0400909 extent_clear_unlock_delalloc(inode, start, end, NULL,
910 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400911 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400912 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
913 PAGE_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000914
Chris Mason771ed682008-11-06 22:02:51 -0500915 *nr_written = *nr_written +
916 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
917 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500918 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100919 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100920 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500921 }
922 }
Chris Masonc8b97812008-10-29 14:49:59 -0400923
924 BUG_ON(disk_num_bytes >
David Sterba6c417612011-04-13 15:41:04 +0200925 btrfs_super_total_bytes(root->fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400926
Josef Bacik4b46fce2010-05-23 11:00:55 -0400927 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400928 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400929
Chris Masond3977122009-01-05 21:25:51 -0500930 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400931 unsigned long op;
932
Josef Bacik287a0ab2010-03-19 18:07:23 +0000933 cur_alloc_size = disk_num_bytes;
Josef Bacik00361582013-08-14 14:02:47 -0400934 ret = btrfs_reserve_extent(root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500935 root->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +0800936 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -0400937 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100938 goto out_unlock;
Chris Masond3977122009-01-05 21:25:51 -0500939
David Sterba172ddd62011-04-21 00:48:27 +0200940 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000941 if (!em) {
942 ret = -ENOMEM;
Liu Boace68ba2013-04-22 10:53:47 +0000943 goto out_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000944 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400945 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500946 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500947 ram_size = ins.offset;
948 em->len = ins.offset;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400949 em->mod_start = em->start;
950 em->mod_len = em->len;
Chris Masonc8b97812008-10-29 14:49:59 -0400951
Chris Masone6dcd2d2008-07-17 12:53:50 -0400952 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400953 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500954 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400955 em->ram_bytes = ram_size;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400956 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400957 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400958 em->generation = -1;
Chris Masonc8b97812008-10-29 14:49:59 -0400959
Chris Masond3977122009-01-05 21:25:51 -0500960 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400961 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400962 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400963 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400964 if (ret != -EEXIST) {
965 free_extent_map(em);
966 break;
967 }
968 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400969 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400970 }
Liu Boace68ba2013-04-22 10:53:47 +0000971 if (ret)
972 goto out_reserve;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400973
Chris Mason98d20f62008-04-14 09:46:10 -0400974 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400975 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -0500976 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +0000977 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +0100978 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -0400979
Yan Zheng17d217f2008-12-12 10:03:38 -0500980 if (root->root_key.objectid ==
981 BTRFS_DATA_RELOC_TREE_OBJECTID) {
982 ret = btrfs_reloc_clone_csums(inode, start,
983 cur_alloc_size);
Josef Bacik00361582013-08-14 14:02:47 -0400984 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +0100985 goto out_drop_extent_cache;
Yan Zheng17d217f2008-12-12 10:03:38 -0500986 }
987
Chris Masond3977122009-01-05 21:25:51 -0500988 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -0400989 break;
Chris Masond3977122009-01-05 21:25:51 -0500990
Chris Masonc8b97812008-10-29 14:49:59 -0400991 /* we're not doing compressed IO, don't unlock the first
992 * page (which the caller expects to stay locked), don't
993 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -0400994 *
995 * Do set the Private2 bit so we know this page was properly
996 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -0400997 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400998 op = unlock ? PAGE_UNLOCK : 0;
999 op |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001000
Josef Bacikc2790a22013-07-29 11:20:47 -04001001 extent_clear_unlock_delalloc(inode, start,
1002 start + ram_size - 1, locked_page,
1003 EXTENT_LOCKED | EXTENT_DELALLOC,
1004 op);
Chris Masonc8b97812008-10-29 14:49:59 -04001005 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001006 num_bytes -= cur_alloc_size;
1007 alloc_hint = ins.objectid + ins.offset;
1008 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -04001009 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001010out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001011 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001012
Filipe Mananad9f85962014-08-25 10:43:00 +01001013out_drop_extent_cache:
1014 btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001015out_reserve:
Miao Xiee570fd22014-06-19 10:42:50 +08001016 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001017out_unlock:
Josef Bacikc2790a22013-07-29 11:20:47 -04001018 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Josef Bacik151a41b2013-07-29 13:22:24 -04001019 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
1020 EXTENT_DELALLOC | EXTENT_DEFRAG,
1021 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
1022 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001023 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001024}
Chris Masonc8b97812008-10-29 14:49:59 -04001025
Chris Mason771ed682008-11-06 22:02:51 -05001026/*
1027 * work queue call back to started compression on a file and pages
1028 */
1029static noinline void async_cow_start(struct btrfs_work *work)
1030{
1031 struct async_cow *async_cow;
1032 int num_added = 0;
1033 async_cow = container_of(work, struct async_cow, work);
1034
1035 compress_file_range(async_cow->inode, async_cow->locked_page,
1036 async_cow->start, async_cow->end, async_cow,
1037 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001038 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001039 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001040 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001041 }
Chris Mason771ed682008-11-06 22:02:51 -05001042}
1043
1044/*
1045 * work queue call back to submit previously compressed pages
1046 */
1047static noinline void async_cow_submit(struct btrfs_work *work)
1048{
1049 struct async_cow *async_cow;
1050 struct btrfs_root *root;
1051 unsigned long nr_pages;
1052
1053 async_cow = container_of(work, struct async_cow, work);
1054
1055 root = async_cow->root;
1056 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
1057 PAGE_CACHE_SHIFT;
1058
Josef Bacik66657b32012-08-01 15:36:24 -04001059 if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) <
Liu Bo287082b2012-06-28 04:02:24 -06001060 5 * 1024 * 1024 &&
Chris Mason771ed682008-11-06 22:02:51 -05001061 waitqueue_active(&root->fs_info->async_submit_wait))
1062 wake_up(&root->fs_info->async_submit_wait);
1063
Chris Masond3977122009-01-05 21:25:51 -05001064 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001065 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001066}
Chris Masonc8b97812008-10-29 14:49:59 -04001067
Chris Mason771ed682008-11-06 22:02:51 -05001068static noinline void async_cow_free(struct btrfs_work *work)
1069{
1070 struct async_cow *async_cow;
1071 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001072 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001073 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001074 kfree(async_cow);
1075}
1076
1077static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1078 u64 start, u64 end, int *page_started,
1079 unsigned long *nr_written)
1080{
1081 struct async_cow *async_cow;
1082 struct btrfs_root *root = BTRFS_I(inode)->root;
1083 unsigned long nr_pages;
1084 u64 cur_end;
Liu Bo287082b2012-06-28 04:02:24 -06001085 int limit = 10 * 1024 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -05001086
Chris Masona3429ab2009-10-08 12:30:20 -04001087 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1088 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001089 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001090 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001091 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001092 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001093 async_cow->root = root;
1094 async_cow->locked_page = locked_page;
1095 async_cow->start = start;
1096
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001097 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
Chris Mason771ed682008-11-06 22:02:51 -05001098 cur_end = end;
1099 else
1100 cur_end = min(end, start + 512 * 1024 - 1);
1101
1102 async_cow->end = cur_end;
1103 INIT_LIST_HEAD(&async_cow->extents);
1104
Liu Bo9e0af232014-08-15 23:36:53 +08001105 btrfs_init_work(&async_cow->work,
1106 btrfs_delalloc_helper,
1107 async_cow_start, async_cow_submit,
1108 async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001109
Chris Mason771ed682008-11-06 22:02:51 -05001110 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
1111 PAGE_CACHE_SHIFT;
1112 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
1113
Qu Wenruoafe3d242014-02-28 10:46:07 +08001114 btrfs_queue_work(root->fs_info->delalloc_workers,
1115 &async_cow->work);
Chris Mason771ed682008-11-06 22:02:51 -05001116
1117 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
1118 wait_event(root->fs_info->async_submit_wait,
1119 (atomic_read(&root->fs_info->async_delalloc_pages) <
1120 limit));
1121 }
1122
Chris Masond3977122009-01-05 21:25:51 -05001123 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -05001124 atomic_read(&root->fs_info->async_delalloc_pages)) {
1125 wait_event(root->fs_info->async_submit_wait,
1126 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1127 0));
1128 }
1129
1130 *nr_written += nr_pages;
1131 start = cur_end + 1;
1132 }
1133 *page_started = 1;
1134 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001135}
1136
Chris Masond3977122009-01-05 21:25:51 -05001137static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001138 u64 bytenr, u64 num_bytes)
1139{
1140 int ret;
1141 struct btrfs_ordered_sum *sums;
1142 LIST_HEAD(list);
1143
Yan Zheng07d400a2009-01-06 11:42:00 -05001144 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001145 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001146 if (ret == 0 && list_empty(&list))
1147 return 0;
1148
1149 while (!list_empty(&list)) {
1150 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1151 list_del(&sums->list);
1152 kfree(sums);
1153 }
1154 return 1;
1155}
1156
Chris Masond352ac62008-09-29 15:18:18 -04001157/*
1158 * when nowcow writeback call back. This checks for snapshots or COW copies
1159 * of the extents that exist in the file, and COWs the file as required.
1160 *
1161 * If no cow copies or snapshots exist, we write directly to the existing
1162 * blocks on disk
1163 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001164static noinline int run_delalloc_nocow(struct inode *inode,
1165 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001166 u64 start, u64 end, int *page_started, int force,
1167 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001168{
Chris Masonbe20aa92007-12-17 20:14:01 -05001169 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001170 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001171 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001172 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001173 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001174 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001175 u64 cow_start;
1176 u64 cur_offset;
1177 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001178 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001179 u64 disk_bytenr;
1180 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001181 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001182 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001183 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001184 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001185 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001186 int nocow;
1187 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001188 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001189 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001190
1191 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001192 if (!path) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001193 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1194 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001195 EXTENT_DO_ACCOUNTING |
1196 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001197 PAGE_CLEAR_DIRTY |
1198 PAGE_SET_WRITEBACK |
1199 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001200 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001201 }
Li Zefan82d59022011-04-20 10:33:24 +08001202
Liu Bo83eea1f2012-07-10 05:28:39 -06001203 nolock = btrfs_is_free_space_inode(inode);
Li Zefan82d59022011-04-20 10:33:24 +08001204
1205 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001206 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001207 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001208 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001209
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001210 if (IS_ERR(trans)) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001211 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1212 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001213 EXTENT_DO_ACCOUNTING |
1214 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001215 PAGE_CLEAR_DIRTY |
1216 PAGE_SET_WRITEBACK |
1217 PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001218 btrfs_free_path(path);
1219 return PTR_ERR(trans);
1220 }
1221
Josef Bacik74b21072011-04-13 12:02:53 -04001222 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001223
Yan Zheng80ff3852008-10-30 14:20:02 -04001224 cow_start = (u64)-1;
1225 cur_offset = start;
1226 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001227 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001228 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001229 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001230 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001231 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1232 leaf = path->nodes[0];
1233 btrfs_item_key_to_cpu(leaf, &found_key,
1234 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001235 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001236 found_key.type == BTRFS_EXTENT_DATA_KEY)
1237 path->slots[0]--;
1238 }
1239 check_prev = 0;
1240next_slot:
1241 leaf = path->nodes[0];
1242 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1243 ret = btrfs_next_leaf(root, path);
Josef Bacikd788a342013-10-25 16:55:08 -04001244 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001245 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001246 if (ret > 0)
1247 break;
1248 leaf = path->nodes[0];
1249 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001250
Yan Zheng80ff3852008-10-30 14:20:02 -04001251 nocow = 0;
1252 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001253 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001254 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001255
Li Zefan33345d012011-04-20 10:31:50 +08001256 if (found_key.objectid > ino ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001257 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1258 found_key.offset > end)
1259 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001260
Yan Zheng80ff3852008-10-30 14:20:02 -04001261 if (found_key.offset > cur_offset) {
1262 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001263 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001264 goto out_check;
1265 }
Chris Masonc31f8832008-01-08 15:46:31 -05001266
Yan Zheng80ff3852008-10-30 14:20:02 -04001267 fi = btrfs_item_ptr(leaf, path->slots[0],
1268 struct btrfs_file_extent_item);
1269 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001270
Josef Bacikcc95bef2013-04-04 14:31:27 -04001271 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001272 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1273 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001274 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001275 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001276 extent_end = found_key.offset +
1277 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001278 disk_num_bytes =
1279 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001280 if (extent_end <= start) {
1281 path->slots[0]++;
1282 goto next_slot;
1283 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001284 if (disk_bytenr == 0)
1285 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001286 if (btrfs_file_extent_compression(leaf, fi) ||
1287 btrfs_file_extent_encryption(leaf, fi) ||
1288 btrfs_file_extent_other_encoding(leaf, fi))
1289 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001290 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1291 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001292 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001293 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001294 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001295 found_key.offset -
1296 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001297 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001298 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001299 disk_bytenr += cur_offset - found_key.offset;
1300 num_bytes = min(end + 1, extent_end) - cur_offset;
1301 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001302 * if there are pending snapshots for this root,
1303 * we fall into common COW way.
1304 */
1305 if (!nolock) {
1306 err = btrfs_start_nocow_write(root);
1307 if (!err)
1308 goto out_check;
1309 }
1310 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001311 * force cow if csum exists in the range.
1312 * this ensure that csum for a given extent are
1313 * either valid or do not exist.
1314 */
1315 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1316 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001317 nocow = 1;
1318 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1319 extent_end = found_key.offset +
Chris Mason514ac8a2014-01-03 21:07:00 -08001320 btrfs_file_extent_inline_len(leaf,
1321 path->slots[0], fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001322 extent_end = ALIGN(extent_end, root->sectorsize);
1323 } else {
1324 BUG_ON(1);
1325 }
1326out_check:
1327 if (extent_end <= start) {
1328 path->slots[0]++;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001329 if (!nolock && nocow)
1330 btrfs_end_nocow_write(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001331 goto next_slot;
1332 }
1333 if (!nocow) {
1334 if (cow_start == (u64)-1)
1335 cow_start = cur_offset;
1336 cur_offset = extent_end;
1337 if (cur_offset > end)
1338 break;
1339 path->slots[0]++;
1340 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001341 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001342
David Sterbab3b4aa72011-04-21 01:20:15 +02001343 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001344 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001345 ret = cow_file_range(inode, locked_page,
1346 cow_start, found_key.offset - 1,
1347 page_started, nr_written, 1);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001348 if (ret) {
1349 if (!nolock && nocow)
1350 btrfs_end_nocow_write(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001351 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001352 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001353 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001354 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001355
Yan Zhengd899e052008-10-30 14:25:28 -04001356 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1357 struct extent_map *em;
1358 struct extent_map_tree *em_tree;
1359 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001360 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001361 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001362 em->start = cur_offset;
Josef Bacik70c8a912012-10-11 16:54:30 -04001363 em->orig_start = found_key.offset - extent_offset;
Yan Zhengd899e052008-10-30 14:25:28 -04001364 em->len = num_bytes;
1365 em->block_len = num_bytes;
1366 em->block_start = disk_bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05001367 em->orig_block_len = disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001368 em->ram_bytes = ram_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04001369 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001370 em->mod_start = em->start;
1371 em->mod_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04001372 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacikb11e2342012-12-03 10:58:15 -05001373 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001374 em->generation = -1;
Yan Zhengd899e052008-10-30 14:25:28 -04001375 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001376 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001377 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001378 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001379 if (ret != -EEXIST) {
1380 free_extent_map(em);
1381 break;
1382 }
1383 btrfs_drop_extent_cache(inode, em->start,
1384 em->start + em->len - 1, 0);
1385 }
1386 type = BTRFS_ORDERED_PREALLOC;
1387 } else {
1388 type = BTRFS_ORDERED_NOCOW;
1389 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001390
1391 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001392 num_bytes, num_bytes, type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001393 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001394
Yan, Zhengefa56462010-05-16 10:49:59 -04001395 if (root->root_key.objectid ==
1396 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1397 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1398 num_bytes);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001399 if (ret) {
1400 if (!nolock && nocow)
1401 btrfs_end_nocow_write(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001402 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001403 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001404 }
1405
Josef Bacikc2790a22013-07-29 11:20:47 -04001406 extent_clear_unlock_delalloc(inode, cur_offset,
1407 cur_offset + num_bytes - 1,
1408 locked_page, EXTENT_LOCKED |
1409 EXTENT_DELALLOC, PAGE_UNLOCK |
1410 PAGE_SET_PRIVATE2);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001411 if (!nolock && nocow)
1412 btrfs_end_nocow_write(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001413 cur_offset = extent_end;
1414 if (cur_offset > end)
1415 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001416 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001417 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001418
Josef Bacik17ca04a2012-05-31 15:58:55 -04001419 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001420 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001421 cur_offset = end;
1422 }
1423
Yan Zheng80ff3852008-10-30 14:20:02 -04001424 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001425 ret = cow_file_range(inode, locked_page, cow_start, end,
1426 page_started, nr_written, 1);
Josef Bacikd788a342013-10-25 16:55:08 -04001427 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001428 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001429 }
1430
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001431error:
Miao Xiea698d0752012-09-20 01:51:59 -06001432 err = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001433 if (!ret)
1434 ret = err;
1435
Josef Bacik17ca04a2012-05-31 15:58:55 -04001436 if (ret && cur_offset < end)
Josef Bacikc2790a22013-07-29 11:20:47 -04001437 extent_clear_unlock_delalloc(inode, cur_offset, end,
1438 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001439 EXTENT_DELALLOC | EXTENT_DEFRAG |
1440 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1441 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001442 PAGE_SET_WRITEBACK |
1443 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001444 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001445 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001446}
1447
Wang Shilong47059d92014-07-03 18:22:07 +08001448static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1449{
1450
1451 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1452 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1453 return 0;
1454
1455 /*
1456 * @defrag_bytes is a hint value, no spinlock held here,
1457 * if is not zero, it means the file is defragging.
1458 * Force cow if given extent needs to be defragged.
1459 */
1460 if (BTRFS_I(inode)->defrag_bytes &&
1461 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1462 EXTENT_DEFRAG, 0, NULL))
1463 return 1;
1464
1465 return 0;
1466}
1467
Chris Masond352ac62008-09-29 15:18:18 -04001468/*
1469 * extent_io.c call back to do delayed allocation processing
1470 */
Chris Masonc8b97812008-10-29 14:49:59 -04001471static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001472 u64 start, u64 end, int *page_started,
1473 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001474{
Chris Masonbe20aa92007-12-17 20:14:01 -05001475 int ret;
Chris Mason7f366cf2009-03-12 20:12:45 -04001476 struct btrfs_root *root = BTRFS_I(inode)->root;
Wang Shilong47059d92014-07-03 18:22:07 +08001477 int force_cow = need_force_cow(inode, start, end);
Chris Masona2135012008-06-25 16:01:30 -04001478
Wang Shilong47059d92014-07-03 18:22:07 +08001479 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001480 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001481 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001482 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001483 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001484 page_started, 0, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001485 } else if (!btrfs_test_opt(root, COMPRESS) &&
1486 !(BTRFS_I(inode)->force_compress) &&
1487 !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS)) {
Chris Mason7f366cf2009-03-12 20:12:45 -04001488 ret = cow_file_range(inode, locked_page, start, end,
1489 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001490 } else {
1491 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1492 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001493 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001494 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001495 }
Chris Masonb888db22007-08-27 16:49:44 -04001496 return ret;
1497}
1498
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001499static void btrfs_split_extent_hook(struct inode *inode,
1500 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001501{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001502 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001503 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001504 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001505
Josef Bacik9e0baf62011-07-15 15:16:44 +00001506 spin_lock(&BTRFS_I(inode)->lock);
1507 BTRFS_I(inode)->outstanding_extents++;
1508 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001509}
1510
1511/*
1512 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1513 * extents so we can keep track of new extents that are just merged onto old
1514 * extents, such as when we are doing sequential writes, so we can properly
1515 * account for the metadata space we'll need.
1516 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001517static void btrfs_merge_extent_hook(struct inode *inode,
1518 struct extent_state *new,
1519 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001520{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001521 /* not delalloc, ignore it */
1522 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001523 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001524
Josef Bacik9e0baf62011-07-15 15:16:44 +00001525 spin_lock(&BTRFS_I(inode)->lock);
1526 BTRFS_I(inode)->outstanding_extents--;
1527 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001528}
1529
Miao Xieeb73c1b2013-05-15 07:48:22 +00001530static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1531 struct inode *inode)
1532{
1533 spin_lock(&root->delalloc_lock);
1534 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1535 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1536 &root->delalloc_inodes);
1537 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1538 &BTRFS_I(inode)->runtime_flags);
1539 root->nr_delalloc_inodes++;
1540 if (root->nr_delalloc_inodes == 1) {
1541 spin_lock(&root->fs_info->delalloc_root_lock);
1542 BUG_ON(!list_empty(&root->delalloc_root));
1543 list_add_tail(&root->delalloc_root,
1544 &root->fs_info->delalloc_roots);
1545 spin_unlock(&root->fs_info->delalloc_root_lock);
1546 }
1547 }
1548 spin_unlock(&root->delalloc_lock);
1549}
1550
1551static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1552 struct inode *inode)
1553{
1554 spin_lock(&root->delalloc_lock);
1555 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1556 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1557 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1558 &BTRFS_I(inode)->runtime_flags);
1559 root->nr_delalloc_inodes--;
1560 if (!root->nr_delalloc_inodes) {
1561 spin_lock(&root->fs_info->delalloc_root_lock);
1562 BUG_ON(list_empty(&root->delalloc_root));
1563 list_del_init(&root->delalloc_root);
1564 spin_unlock(&root->fs_info->delalloc_root_lock);
1565 }
1566 }
1567 spin_unlock(&root->delalloc_lock);
1568}
1569
Chris Masond352ac62008-09-29 15:18:18 -04001570/*
1571 * extent_io.c set_bit_hook, used to track delayed allocation
1572 * bytes in this file, and to maintain the list of inodes that
1573 * have pending delalloc work to be done.
1574 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001575static void btrfs_set_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001576 struct extent_state *state, unsigned long *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001577{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001578
Wang Shilong47059d92014-07-03 18:22:07 +08001579 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1580 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001581 /*
1582 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001583 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001584 * bit, which is only set or cleared with irqs on
1585 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001586 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001587 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001588 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001589 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001590
Josef Bacik9e0baf62011-07-15 15:16:44 +00001591 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001592 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001593 } else {
1594 spin_lock(&BTRFS_I(inode)->lock);
1595 BTRFS_I(inode)->outstanding_extents++;
1596 spin_unlock(&BTRFS_I(inode)->lock);
1597 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001598
Miao Xie963d6782013-01-29 10:10:51 +00001599 __percpu_counter_add(&root->fs_info->delalloc_bytes, len,
1600 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001601 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001602 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001603 if (*bits & EXTENT_DEFRAG)
1604 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001605 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001606 &BTRFS_I(inode)->runtime_flags))
1607 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001608 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001609 }
Chris Mason291d6732008-01-29 15:55:23 -05001610}
1611
Chris Masond352ac62008-09-29 15:18:18 -04001612/*
1613 * extent_io.c clear_bit_hook, see set_bit_hook for why
1614 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001615static void btrfs_clear_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001616 struct extent_state *state,
1617 unsigned long *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001618{
Wang Shilong47059d92014-07-03 18:22:07 +08001619 u64 len = state->end + 1 - state->start;
1620
1621 spin_lock(&BTRFS_I(inode)->lock);
1622 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG))
1623 BTRFS_I(inode)->defrag_bytes -= len;
1624 spin_unlock(&BTRFS_I(inode)->lock);
1625
Chris Mason75eff682008-12-15 15:54:40 -05001626 /*
1627 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001628 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001629 * bit, which is only set or cleared with irqs on
1630 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001631 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001632 struct btrfs_root *root = BTRFS_I(inode)->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001633 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001634
Josef Bacik9e0baf62011-07-15 15:16:44 +00001635 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001636 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001637 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1638 spin_lock(&BTRFS_I(inode)->lock);
1639 BTRFS_I(inode)->outstanding_extents--;
1640 spin_unlock(&BTRFS_I(inode)->lock);
1641 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001642
Josef Bacikb6d08f02013-09-27 14:57:43 -04001643 /*
1644 * We don't reserve metadata space for space cache inodes so we
1645 * don't need to call dellalloc_release_metadata if there is an
1646 * error.
1647 */
1648 if (*bits & EXTENT_DO_ACCOUNTING &&
1649 root != root->fs_info->tree_root)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001650 btrfs_delalloc_release_metadata(inode, len);
1651
Josef Bacik0cb59c92010-07-02 12:14:14 -04001652 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Josef Bacik7ee9e442013-06-21 16:37:03 -04001653 && do_list && !(state->state & EXTENT_NORESERVE))
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001654 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001655
Miao Xie963d6782013-01-29 10:10:51 +00001656 __percpu_counter_add(&root->fs_info->delalloc_bytes, -len,
1657 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001658 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001659 BTRFS_I(inode)->delalloc_bytes -= len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001660 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001661 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001662 &BTRFS_I(inode)->runtime_flags))
1663 btrfs_del_delalloc_inode(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001664 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001665 }
Chris Mason291d6732008-01-29 15:55:23 -05001666}
1667
Chris Masond352ac62008-09-29 15:18:18 -04001668/*
1669 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1670 * we don't create bios that span stripes or chunks
1671 */
David Woodhouse64a16702009-07-15 23:29:37 +01001672int btrfs_merge_bio_hook(int rw, struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001673 size_t size, struct bio *bio,
1674 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001675{
1676 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001677 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001678 u64 length = 0;
1679 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001680 int ret;
1681
Chris Mason771ed682008-11-06 22:02:51 -05001682 if (bio_flags & EXTENT_BIO_COMPRESSED)
1683 return 0;
1684
Kent Overstreet4f024f32013-10-11 15:44:27 -07001685 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001686 map_length = length;
David Woodhouse64a16702009-07-15 23:29:37 +01001687 ret = btrfs_map_block(root->fs_info, rw, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001688 &map_length, NULL, 0);
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001689 /* Will always return 0 with map_multi == NULL */
Jeff Mahoney3444a972011-10-03 23:23:13 -04001690 BUG_ON(ret < 0);
Chris Masond3977122009-01-05 21:25:51 -05001691 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001692 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001693 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001694}
1695
Chris Masond352ac62008-09-29 15:18:18 -04001696/*
1697 * in order to insert checksums into the metadata in large chunks,
1698 * we wait until bio submission time. All the pages in the bio are
1699 * checksummed and sums are attached onto the ordered extent record.
1700 *
1701 * At IO completion time the cums attached on the ordered extent record
1702 * are inserted into the btree
1703 */
Chris Masond3977122009-01-05 21:25:51 -05001704static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1705 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001706 unsigned long bio_flags,
1707 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001708{
Chris Mason065631f2008-02-20 12:07:25 -05001709 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001710 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001711
Chris Masond20f7042008-12-08 16:58:54 -05001712 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001713 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001714 return 0;
1715}
Chris Masone0156402008-04-16 11:15:20 -04001716
Chris Mason4a69a412008-11-06 22:03:00 -05001717/*
1718 * in order to insert checksums into the metadata in large chunks,
1719 * we wait until bio submission time. All the pages in the bio are
1720 * checksummed and sums are attached onto the ordered extent record.
1721 *
1722 * At IO completion time the cums attached on the ordered extent record
1723 * are inserted into the btree
1724 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001725static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001726 int mirror_num, unsigned long bio_flags,
1727 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001728{
1729 struct btrfs_root *root = BTRFS_I(inode)->root;
Stefan Behrens61891922012-11-05 18:51:52 +01001730 int ret;
1731
1732 ret = btrfs_map_bio(root, rw, bio, mirror_num, 1);
1733 if (ret)
1734 bio_endio(bio, ret);
1735 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001736}
1737
Chris Masond352ac62008-09-29 15:18:18 -04001738/*
Chris Masoncad321a2008-12-17 14:51:42 -05001739 * extent_io.c submission hook. This does the right thing for csum calculation
1740 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001741 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001742static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001743 int mirror_num, unsigned long bio_flags,
1744 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001745{
1746 struct btrfs_root *root = BTRFS_I(inode)->root;
1747 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001748 int skip_sum;
Jeff Mahoney04173412011-10-03 23:23:12 -04001749 int metadata = 0;
Josef Bacikb812ce22012-11-16 13:56:32 -05001750 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001751
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001752 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001753
Liu Bo83eea1f2012-07-10 05:28:39 -06001754 if (btrfs_is_free_space_inode(inode))
Jeff Mahoney04173412011-10-03 23:23:12 -04001755 metadata = 2;
1756
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001757 if (!(rw & REQ_WRITE)) {
Josef Bacik5fd02042012-05-02 14:00:54 -04001758 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1759 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001760 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001761
Chris Masond20f7042008-12-08 16:58:54 -05001762 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001763 ret = btrfs_submit_compressed_read(inode, bio,
1764 mirror_num,
1765 bio_flags);
1766 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001767 } else if (!skip_sum) {
1768 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1769 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001770 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001771 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001772 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001773 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001774 /* csum items have already been cloned */
1775 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1776 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001777 /* we're doing a write, do the async checksumming */
Stefan Behrens61891922012-11-05 18:51:52 +01001778 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001779 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001780 bio_flags, bio_offset,
1781 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001782 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001783 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001784 } else if (!skip_sum) {
1785 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1786 if (ret)
1787 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001788 }
1789
Chris Mason0b86a832008-03-24 15:01:56 -04001790mapit:
Stefan Behrens61891922012-11-05 18:51:52 +01001791 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
1792
1793out:
1794 if (ret < 0)
1795 bio_endio(bio, ret);
1796 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001797}
Chris Mason6885f302008-02-20 16:11:05 -05001798
Chris Masond352ac62008-09-29 15:18:18 -04001799/*
1800 * given a list of ordered sums record them in the inode. This happens
1801 * at IO completion time based on sums calculated at bio submission time.
1802 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001803static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001804 struct inode *inode, u64 file_offset,
1805 struct list_head *list)
1806{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001807 struct btrfs_ordered_sum *sum;
1808
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001809 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00001810 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05001811 btrfs_csum_file_blocks(trans,
1812 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00001813 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001814 }
1815 return 0;
1816}
1817
Josef Bacik2ac55d42010-02-03 19:33:23 +00001818int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1819 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001820{
Julia Lawall6c1500f2012-11-03 20:30:18 +00001821 WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04001822 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001823 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001824}
1825
Chris Masond352ac62008-09-29 15:18:18 -04001826/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001827struct btrfs_writepage_fixup {
1828 struct page *page;
1829 struct btrfs_work work;
1830};
1831
Christoph Hellwigb2950862008-12-02 09:54:17 -05001832static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001833{
1834 struct btrfs_writepage_fixup *fixup;
1835 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001836 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001837 struct page *page;
1838 struct inode *inode;
1839 u64 page_start;
1840 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001841 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001842
1843 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1844 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001845again:
Chris Mason247e7432008-07-17 12:53:51 -04001846 lock_page(page);
1847 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1848 ClearPageChecked(page);
1849 goto out_page;
1850 }
1851
1852 inode = page->mapping->host;
1853 page_start = page_offset(page);
1854 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1855
Josef Bacik2ac55d42010-02-03 19:33:23 +00001856 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001857 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001858
1859 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001860 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001861 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001862
1863 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1864 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001865 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1866 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001867 unlock_page(page);
1868 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001869 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04001870 goto again;
1871 }
Chris Mason247e7432008-07-17 12:53:51 -04001872
Jeff Mahoney87826df2012-02-15 16:23:57 +01001873 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
1874 if (ret) {
1875 mapping_set_error(page->mapping, ret);
1876 end_extent_writepage(page, ret, page_start, page_end);
1877 ClearPageChecked(page);
1878 goto out;
1879 }
1880
Josef Bacik2ac55d42010-02-03 19:33:23 +00001881 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001882 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001883 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04001884out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001885 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1886 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001887out_page:
1888 unlock_page(page);
1889 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08001890 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04001891}
1892
1893/*
1894 * There are a few paths in the higher layers of the kernel that directly
1895 * set the page dirty bit without asking the filesystem if it is a
1896 * good idea. This causes problems because we want to make sure COW
1897 * properly happens and the data=ordered rules are followed.
1898 *
Chris Masonc8b97812008-10-29 14:49:59 -04001899 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001900 * hasn't been properly setup for IO. We kick off an async process
1901 * to fix it up. The async helper will wait for ordered extents, set
1902 * the delalloc bit and make it safe to write the page.
1903 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001904static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001905{
1906 struct inode *inode = page->mapping->host;
1907 struct btrfs_writepage_fixup *fixup;
1908 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001909
Chris Mason8b62b722009-09-02 16:53:46 -04001910 /* this page is properly in the ordered list */
1911 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001912 return 0;
1913
1914 if (PageChecked(page))
1915 return -EAGAIN;
1916
1917 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1918 if (!fixup)
1919 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001920
Chris Mason247e7432008-07-17 12:53:51 -04001921 SetPageChecked(page);
1922 page_cache_get(page);
Liu Bo9e0af232014-08-15 23:36:53 +08001923 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
1924 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04001925 fixup->page = page;
Qu Wenruodc6e3202014-02-28 10:46:14 +08001926 btrfs_queue_work(root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001927 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04001928}
1929
Yan Zhengd899e052008-10-30 14:25:28 -04001930static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1931 struct inode *inode, u64 file_pos,
1932 u64 disk_bytenr, u64 disk_num_bytes,
1933 u64 num_bytes, u64 ram_bytes,
1934 u8 compression, u8 encryption,
1935 u16 other_encoding, int extent_type)
1936{
1937 struct btrfs_root *root = BTRFS_I(inode)->root;
1938 struct btrfs_file_extent_item *fi;
1939 struct btrfs_path *path;
1940 struct extent_buffer *leaf;
1941 struct btrfs_key ins;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00001942 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04001943 int ret;
1944
1945 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07001946 if (!path)
1947 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04001948
Chris Masona1ed8352009-09-11 12:27:37 -04001949 /*
1950 * we may be replacing one extent in the tree with another.
1951 * The new extent is pinned in the extent map, and we don't want
1952 * to drop it from the cache until it is completely in the btree.
1953 *
1954 * So, tell btrfs_drop_extents to leave this extent in the cache.
1955 * the caller is expected to unpin it and allow it to be merged
1956 * with the others.
1957 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00001958 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
1959 file_pos + num_bytes, NULL, 0,
1960 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001961 if (ret)
1962 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001963
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00001964 if (!extent_inserted) {
1965 ins.objectid = btrfs_ino(inode);
1966 ins.offset = file_pos;
1967 ins.type = BTRFS_EXTENT_DATA_KEY;
1968
1969 path->leave_spinning = 1;
1970 ret = btrfs_insert_empty_item(trans, root, path, &ins,
1971 sizeof(*fi));
1972 if (ret)
1973 goto out;
1974 }
Yan Zhengd899e052008-10-30 14:25:28 -04001975 leaf = path->nodes[0];
1976 fi = btrfs_item_ptr(leaf, path->slots[0],
1977 struct btrfs_file_extent_item);
1978 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1979 btrfs_set_file_extent_type(leaf, fi, extent_type);
1980 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1981 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1982 btrfs_set_file_extent_offset(leaf, fi, 0);
1983 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1984 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1985 btrfs_set_file_extent_compression(leaf, fi, compression);
1986 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1987 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04001988
Yan Zhengd899e052008-10-30 14:25:28 -04001989 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04001990 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04001991
1992 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04001993
1994 ins.objectid = disk_bytenr;
1995 ins.offset = disk_num_bytes;
1996 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001997 ret = btrfs_alloc_reserved_file_extent(trans, root,
1998 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08001999 btrfs_ino(inode), file_pos, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002000out:
Yan Zhengd899e052008-10-30 14:25:28 -04002001 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002002
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002003 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002004}
2005
Liu Bo38c227d2013-01-29 03:18:40 +00002006/* snapshot-aware defrag */
2007struct sa_defrag_extent_backref {
2008 struct rb_node node;
2009 struct old_sa_defrag_extent *old;
2010 u64 root_id;
2011 u64 inum;
2012 u64 file_pos;
2013 u64 extent_offset;
2014 u64 num_bytes;
2015 u64 generation;
2016};
2017
2018struct old_sa_defrag_extent {
2019 struct list_head list;
2020 struct new_sa_defrag_extent *new;
2021
2022 u64 extent_offset;
2023 u64 bytenr;
2024 u64 offset;
2025 u64 len;
2026 int count;
2027};
2028
2029struct new_sa_defrag_extent {
2030 struct rb_root root;
2031 struct list_head head;
2032 struct btrfs_path *path;
2033 struct inode *inode;
2034 u64 file_pos;
2035 u64 len;
2036 u64 bytenr;
2037 u64 disk_len;
2038 u8 compress_type;
2039};
2040
2041static int backref_comp(struct sa_defrag_extent_backref *b1,
2042 struct sa_defrag_extent_backref *b2)
2043{
2044 if (b1->root_id < b2->root_id)
2045 return -1;
2046 else if (b1->root_id > b2->root_id)
2047 return 1;
2048
2049 if (b1->inum < b2->inum)
2050 return -1;
2051 else if (b1->inum > b2->inum)
2052 return 1;
2053
2054 if (b1->file_pos < b2->file_pos)
2055 return -1;
2056 else if (b1->file_pos > b2->file_pos)
2057 return 1;
2058
2059 /*
2060 * [------------------------------] ===> (a range of space)
2061 * |<--->| |<---->| =============> (fs/file tree A)
2062 * |<---------------------------->| ===> (fs/file tree B)
2063 *
2064 * A range of space can refer to two file extents in one tree while
2065 * refer to only one file extent in another tree.
2066 *
2067 * So we may process a disk offset more than one time(two extents in A)
2068 * and locate at the same extent(one extent in B), then insert two same
2069 * backrefs(both refer to the extent in B).
2070 */
2071 return 0;
2072}
2073
2074static void backref_insert(struct rb_root *root,
2075 struct sa_defrag_extent_backref *backref)
2076{
2077 struct rb_node **p = &root->rb_node;
2078 struct rb_node *parent = NULL;
2079 struct sa_defrag_extent_backref *entry;
2080 int ret;
2081
2082 while (*p) {
2083 parent = *p;
2084 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2085
2086 ret = backref_comp(backref, entry);
2087 if (ret < 0)
2088 p = &(*p)->rb_left;
2089 else
2090 p = &(*p)->rb_right;
2091 }
2092
2093 rb_link_node(&backref->node, parent, p);
2094 rb_insert_color(&backref->node, root);
2095}
2096
2097/*
2098 * Note the backref might has changed, and in this case we just return 0.
2099 */
2100static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2101 void *ctx)
2102{
2103 struct btrfs_file_extent_item *extent;
2104 struct btrfs_fs_info *fs_info;
2105 struct old_sa_defrag_extent *old = ctx;
2106 struct new_sa_defrag_extent *new = old->new;
2107 struct btrfs_path *path = new->path;
2108 struct btrfs_key key;
2109 struct btrfs_root *root;
2110 struct sa_defrag_extent_backref *backref;
2111 struct extent_buffer *leaf;
2112 struct inode *inode = new->inode;
2113 int slot;
2114 int ret;
2115 u64 extent_offset;
2116 u64 num_bytes;
2117
2118 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
2119 inum == btrfs_ino(inode))
2120 return 0;
2121
2122 key.objectid = root_id;
2123 key.type = BTRFS_ROOT_ITEM_KEY;
2124 key.offset = (u64)-1;
2125
2126 fs_info = BTRFS_I(inode)->root->fs_info;
2127 root = btrfs_read_fs_root_no_name(fs_info, &key);
2128 if (IS_ERR(root)) {
2129 if (PTR_ERR(root) == -ENOENT)
2130 return 0;
2131 WARN_ON(1);
2132 pr_debug("inum=%llu, offset=%llu, root_id=%llu\n",
2133 inum, offset, root_id);
2134 return PTR_ERR(root);
2135 }
2136
2137 key.objectid = inum;
2138 key.type = BTRFS_EXTENT_DATA_KEY;
2139 if (offset > (u64)-1 << 32)
2140 key.offset = 0;
2141 else
2142 key.offset = offset;
2143
2144 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302145 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002146 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002147 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002148
2149 while (1) {
2150 cond_resched();
2151
2152 leaf = path->nodes[0];
2153 slot = path->slots[0];
2154
2155 if (slot >= btrfs_header_nritems(leaf)) {
2156 ret = btrfs_next_leaf(root, path);
2157 if (ret < 0) {
2158 goto out;
2159 } else if (ret > 0) {
2160 ret = 0;
2161 goto out;
2162 }
2163 continue;
2164 }
2165
2166 path->slots[0]++;
2167
2168 btrfs_item_key_to_cpu(leaf, &key, slot);
2169
2170 if (key.objectid > inum)
2171 goto out;
2172
2173 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2174 continue;
2175
2176 extent = btrfs_item_ptr(leaf, slot,
2177 struct btrfs_file_extent_item);
2178
2179 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2180 continue;
2181
Liu Boe68afa42013-07-01 22:13:26 +08002182 /*
2183 * 'offset' refers to the exact key.offset,
2184 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2185 * (key.offset - extent_offset).
2186 */
2187 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002188 continue;
2189
Liu Boe68afa42013-07-01 22:13:26 +08002190 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002191 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002192
Liu Bo38c227d2013-01-29 03:18:40 +00002193 if (extent_offset >= old->extent_offset + old->offset +
2194 old->len || extent_offset + num_bytes <=
2195 old->extent_offset + old->offset)
2196 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002197 break;
2198 }
2199
2200 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2201 if (!backref) {
2202 ret = -ENOENT;
2203 goto out;
2204 }
2205
2206 backref->root_id = root_id;
2207 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002208 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002209 backref->num_bytes = num_bytes;
2210 backref->extent_offset = extent_offset;
2211 backref->generation = btrfs_file_extent_generation(leaf, extent);
2212 backref->old = old;
2213 backref_insert(&new->root, backref);
2214 old->count++;
2215out:
2216 btrfs_release_path(path);
2217 WARN_ON(ret);
2218 return ret;
2219}
2220
2221static noinline bool record_extent_backrefs(struct btrfs_path *path,
2222 struct new_sa_defrag_extent *new)
2223{
2224 struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info;
2225 struct old_sa_defrag_extent *old, *tmp;
2226 int ret;
2227
2228 new->path = path;
2229
2230 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002231 ret = iterate_inodes_from_logical(old->bytenr +
2232 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002233 path, record_one_backref,
2234 old);
Josef Bacik4724b102013-11-05 11:11:40 -05002235 if (ret < 0 && ret != -ENOENT)
2236 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002237
2238 /* no backref to be processed for this extent */
2239 if (!old->count) {
2240 list_del(&old->list);
2241 kfree(old);
2242 }
2243 }
2244
2245 if (list_empty(&new->head))
2246 return false;
2247
2248 return true;
2249}
2250
2251static int relink_is_mergable(struct extent_buffer *leaf,
2252 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002253 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002254{
Liu Bo116e0022013-08-02 16:30:40 +08002255 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002256 return 0;
2257
2258 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2259 return 0;
2260
Liu Bo116e0022013-08-02 16:30:40 +08002261 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2262 return 0;
2263
2264 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002265 btrfs_file_extent_other_encoding(leaf, fi))
2266 return 0;
2267
2268 return 1;
2269}
2270
2271/*
2272 * Note the backref might has changed, and in this case we just return 0.
2273 */
2274static noinline int relink_extent_backref(struct btrfs_path *path,
2275 struct sa_defrag_extent_backref *prev,
2276 struct sa_defrag_extent_backref *backref)
2277{
2278 struct btrfs_file_extent_item *extent;
2279 struct btrfs_file_extent_item *item;
2280 struct btrfs_ordered_extent *ordered;
2281 struct btrfs_trans_handle *trans;
2282 struct btrfs_fs_info *fs_info;
2283 struct btrfs_root *root;
2284 struct btrfs_key key;
2285 struct extent_buffer *leaf;
2286 struct old_sa_defrag_extent *old = backref->old;
2287 struct new_sa_defrag_extent *new = old->new;
2288 struct inode *src_inode = new->inode;
2289 struct inode *inode;
2290 struct extent_state *cached = NULL;
2291 int ret = 0;
2292 u64 start;
2293 u64 len;
2294 u64 lock_start;
2295 u64 lock_end;
2296 bool merge = false;
2297 int index;
2298
2299 if (prev && prev->root_id == backref->root_id &&
2300 prev->inum == backref->inum &&
2301 prev->file_pos + prev->num_bytes == backref->file_pos)
2302 merge = true;
2303
2304 /* step 1: get root */
2305 key.objectid = backref->root_id;
2306 key.type = BTRFS_ROOT_ITEM_KEY;
2307 key.offset = (u64)-1;
2308
2309 fs_info = BTRFS_I(src_inode)->root->fs_info;
2310 index = srcu_read_lock(&fs_info->subvol_srcu);
2311
2312 root = btrfs_read_fs_root_no_name(fs_info, &key);
2313 if (IS_ERR(root)) {
2314 srcu_read_unlock(&fs_info->subvol_srcu, index);
2315 if (PTR_ERR(root) == -ENOENT)
2316 return 0;
2317 return PTR_ERR(root);
2318 }
Liu Bo38c227d2013-01-29 03:18:40 +00002319
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002320 if (btrfs_root_readonly(root)) {
2321 srcu_read_unlock(&fs_info->subvol_srcu, index);
2322 return 0;
2323 }
2324
Liu Bo38c227d2013-01-29 03:18:40 +00002325 /* step 2: get inode */
2326 key.objectid = backref->inum;
2327 key.type = BTRFS_INODE_ITEM_KEY;
2328 key.offset = 0;
2329
2330 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2331 if (IS_ERR(inode)) {
2332 srcu_read_unlock(&fs_info->subvol_srcu, index);
2333 return 0;
2334 }
2335
2336 srcu_read_unlock(&fs_info->subvol_srcu, index);
2337
2338 /* step 3: relink backref */
2339 lock_start = backref->file_pos;
2340 lock_end = backref->file_pos + backref->num_bytes - 1;
2341 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2342 0, &cached);
2343
2344 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2345 if (ordered) {
2346 btrfs_put_ordered_extent(ordered);
2347 goto out_unlock;
2348 }
2349
2350 trans = btrfs_join_transaction(root);
2351 if (IS_ERR(trans)) {
2352 ret = PTR_ERR(trans);
2353 goto out_unlock;
2354 }
2355
2356 key.objectid = backref->inum;
2357 key.type = BTRFS_EXTENT_DATA_KEY;
2358 key.offset = backref->file_pos;
2359
2360 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2361 if (ret < 0) {
2362 goto out_free_path;
2363 } else if (ret > 0) {
2364 ret = 0;
2365 goto out_free_path;
2366 }
2367
2368 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2369 struct btrfs_file_extent_item);
2370
2371 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2372 backref->generation)
2373 goto out_free_path;
2374
2375 btrfs_release_path(path);
2376
2377 start = backref->file_pos;
2378 if (backref->extent_offset < old->extent_offset + old->offset)
2379 start += old->extent_offset + old->offset -
2380 backref->extent_offset;
2381
2382 len = min(backref->extent_offset + backref->num_bytes,
2383 old->extent_offset + old->offset + old->len);
2384 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2385
2386 ret = btrfs_drop_extents(trans, root, inode, start,
2387 start + len, 1);
2388 if (ret)
2389 goto out_free_path;
2390again:
2391 key.objectid = btrfs_ino(inode);
2392 key.type = BTRFS_EXTENT_DATA_KEY;
2393 key.offset = start;
2394
Liu Boa09a0a72013-03-11 09:20:58 +00002395 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002396 if (merge) {
2397 struct btrfs_file_extent_item *fi;
2398 u64 extent_len;
2399 struct btrfs_key found_key;
2400
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002401 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002402 if (ret < 0)
2403 goto out_free_path;
2404
2405 path->slots[0]--;
2406 leaf = path->nodes[0];
2407 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2408
2409 fi = btrfs_item_ptr(leaf, path->slots[0],
2410 struct btrfs_file_extent_item);
2411 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2412
Liu Bo116e0022013-08-02 16:30:40 +08002413 if (extent_len + found_key.offset == start &&
2414 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002415 btrfs_set_file_extent_num_bytes(leaf, fi,
2416 extent_len + len);
2417 btrfs_mark_buffer_dirty(leaf);
2418 inode_add_bytes(inode, len);
2419
2420 ret = 1;
2421 goto out_free_path;
2422 } else {
2423 merge = false;
2424 btrfs_release_path(path);
2425 goto again;
2426 }
2427 }
2428
2429 ret = btrfs_insert_empty_item(trans, root, path, &key,
2430 sizeof(*extent));
2431 if (ret) {
2432 btrfs_abort_transaction(trans, root, ret);
2433 goto out_free_path;
2434 }
2435
2436 leaf = path->nodes[0];
2437 item = btrfs_item_ptr(leaf, path->slots[0],
2438 struct btrfs_file_extent_item);
2439 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2440 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2441 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2442 btrfs_set_file_extent_num_bytes(leaf, item, len);
2443 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2444 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2445 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2446 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2447 btrfs_set_file_extent_encryption(leaf, item, 0);
2448 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2449
2450 btrfs_mark_buffer_dirty(leaf);
2451 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002452 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002453
2454 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
2455 new->disk_len, 0,
2456 backref->root_id, backref->inum,
2457 new->file_pos, 0); /* start - extent_offset */
2458 if (ret) {
2459 btrfs_abort_transaction(trans, root, ret);
2460 goto out_free_path;
2461 }
2462
2463 ret = 1;
2464out_free_path:
2465 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002466 path->leave_spinning = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002467 btrfs_end_transaction(trans, root);
2468out_unlock:
2469 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2470 &cached, GFP_NOFS);
2471 iput(inode);
2472 return ret;
2473}
2474
Liu Bo6f519562013-10-29 10:45:05 +08002475static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2476{
2477 struct old_sa_defrag_extent *old, *tmp;
2478
2479 if (!new)
2480 return;
2481
2482 list_for_each_entry_safe(old, tmp, &new->head, list) {
2483 list_del(&old->list);
2484 kfree(old);
2485 }
2486 kfree(new);
2487}
2488
Liu Bo38c227d2013-01-29 03:18:40 +00002489static void relink_file_extents(struct new_sa_defrag_extent *new)
2490{
2491 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002492 struct sa_defrag_extent_backref *backref;
2493 struct sa_defrag_extent_backref *prev = NULL;
2494 struct inode *inode;
2495 struct btrfs_root *root;
2496 struct rb_node *node;
2497 int ret;
2498
2499 inode = new->inode;
2500 root = BTRFS_I(inode)->root;
2501
2502 path = btrfs_alloc_path();
2503 if (!path)
2504 return;
2505
2506 if (!record_extent_backrefs(path, new)) {
2507 btrfs_free_path(path);
2508 goto out;
2509 }
2510 btrfs_release_path(path);
2511
2512 while (1) {
2513 node = rb_first(&new->root);
2514 if (!node)
2515 break;
2516 rb_erase(node, &new->root);
2517
2518 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2519
2520 ret = relink_extent_backref(path, prev, backref);
2521 WARN_ON(ret < 0);
2522
2523 kfree(prev);
2524
2525 if (ret == 1)
2526 prev = backref;
2527 else
2528 prev = NULL;
2529 cond_resched();
2530 }
2531 kfree(prev);
2532
2533 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002534out:
Liu Bo6f519562013-10-29 10:45:05 +08002535 free_sa_defrag_extent(new);
2536
Liu Bo38c227d2013-01-29 03:18:40 +00002537 atomic_dec(&root->fs_info->defrag_running);
2538 wake_up(&root->fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002539}
2540
2541static struct new_sa_defrag_extent *
2542record_old_file_extents(struct inode *inode,
2543 struct btrfs_ordered_extent *ordered)
2544{
2545 struct btrfs_root *root = BTRFS_I(inode)->root;
2546 struct btrfs_path *path;
2547 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002548 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002549 struct new_sa_defrag_extent *new;
2550 int ret;
2551
2552 new = kmalloc(sizeof(*new), GFP_NOFS);
2553 if (!new)
2554 return NULL;
2555
2556 new->inode = inode;
2557 new->file_pos = ordered->file_offset;
2558 new->len = ordered->len;
2559 new->bytenr = ordered->start;
2560 new->disk_len = ordered->disk_len;
2561 new->compress_type = ordered->compress_type;
2562 new->root = RB_ROOT;
2563 INIT_LIST_HEAD(&new->head);
2564
2565 path = btrfs_alloc_path();
2566 if (!path)
2567 goto out_kfree;
2568
2569 key.objectid = btrfs_ino(inode);
2570 key.type = BTRFS_EXTENT_DATA_KEY;
2571 key.offset = new->file_pos;
2572
2573 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2574 if (ret < 0)
2575 goto out_free_path;
2576 if (ret > 0 && path->slots[0] > 0)
2577 path->slots[0]--;
2578
2579 /* find out all the old extents for the file range */
2580 while (1) {
2581 struct btrfs_file_extent_item *extent;
2582 struct extent_buffer *l;
2583 int slot;
2584 u64 num_bytes;
2585 u64 offset;
2586 u64 end;
2587 u64 disk_bytenr;
2588 u64 extent_offset;
2589
2590 l = path->nodes[0];
2591 slot = path->slots[0];
2592
2593 if (slot >= btrfs_header_nritems(l)) {
2594 ret = btrfs_next_leaf(root, path);
2595 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002596 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002597 else if (ret > 0)
2598 break;
2599 continue;
2600 }
2601
2602 btrfs_item_key_to_cpu(l, &key, slot);
2603
2604 if (key.objectid != btrfs_ino(inode))
2605 break;
2606 if (key.type != BTRFS_EXTENT_DATA_KEY)
2607 break;
2608 if (key.offset >= new->file_pos + new->len)
2609 break;
2610
2611 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2612
2613 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2614 if (key.offset + num_bytes < new->file_pos)
2615 goto next;
2616
2617 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2618 if (!disk_bytenr)
2619 goto next;
2620
2621 extent_offset = btrfs_file_extent_offset(l, extent);
2622
2623 old = kmalloc(sizeof(*old), GFP_NOFS);
2624 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002625 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002626
2627 offset = max(new->file_pos, key.offset);
2628 end = min(new->file_pos + new->len, key.offset + num_bytes);
2629
2630 old->bytenr = disk_bytenr;
2631 old->extent_offset = extent_offset;
2632 old->offset = offset - key.offset;
2633 old->len = end - offset;
2634 old->new = new;
2635 old->count = 0;
2636 list_add_tail(&old->list, &new->head);
2637next:
2638 path->slots[0]++;
2639 cond_resched();
2640 }
2641
2642 btrfs_free_path(path);
2643 atomic_inc(&root->fs_info->defrag_running);
2644
2645 return new;
2646
Liu Bo38c227d2013-01-29 03:18:40 +00002647out_free_path:
2648 btrfs_free_path(path);
2649out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002650 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002651 return NULL;
2652}
2653
Miao Xiee570fd22014-06-19 10:42:50 +08002654static void btrfs_release_delalloc_bytes(struct btrfs_root *root,
2655 u64 start, u64 len)
2656{
2657 struct btrfs_block_group_cache *cache;
2658
2659 cache = btrfs_lookup_block_group(root->fs_info, start);
2660 ASSERT(cache);
2661
2662 spin_lock(&cache->lock);
2663 cache->delalloc_bytes -= len;
2664 spin_unlock(&cache->lock);
2665
2666 btrfs_put_block_group(cache);
2667}
2668
Chris Masond352ac62008-09-29 15:18:18 -04002669/* as ordered data IO finishes, this gets called so we can finish
2670 * an ordered extent if the range of bytes in the file it covers are
2671 * fully written.
2672 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002673static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002674{
Josef Bacik5fd02042012-05-02 14:00:54 -04002675 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002676 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002677 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002678 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002679 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002680 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002681 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002682 int ret = 0;
2683 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002684 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002685 bool truncated = false;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002686
Liu Bo83eea1f2012-07-10 05:28:39 -06002687 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002688
Josef Bacik5fd02042012-05-02 14:00:54 -04002689 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2690 ret = -EIO;
2691 goto out;
2692 }
2693
Josef Bacik77cef2e2013-08-29 13:57:21 -04002694 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2695 truncated = true;
2696 logical_len = ordered_extent->truncated_len;
2697 /* Truncated the entire extent, don't bother adding */
2698 if (!logical_len)
2699 goto out;
2700 }
2701
Yan, Zhengc2167752009-11-12 09:34:21 +00002702 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002703 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Josef Bacik6c760c02012-11-09 10:53:21 -05002704 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2705 if (nolock)
2706 trans = btrfs_join_transaction_nolock(root);
2707 else
2708 trans = btrfs_join_transaction(root);
2709 if (IS_ERR(trans)) {
2710 ret = PTR_ERR(trans);
2711 trans = NULL;
2712 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002713 }
Josef Bacik6c760c02012-11-09 10:53:21 -05002714 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
2715 ret = btrfs_update_inode_fallback(trans, root, inode);
2716 if (ret) /* -ENOMEM or corruption */
2717 btrfs_abort_transaction(trans, root, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002718 goto out;
2719 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002720
Josef Bacik2ac55d42010-02-03 19:33:23 +00002721 lock_extent_bits(io_tree, ordered_extent->file_offset,
2722 ordered_extent->file_offset + ordered_extent->len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002723 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002724
Liu Bo38c227d2013-01-29 03:18:40 +00002725 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2726 ordered_extent->file_offset + ordered_extent->len - 1,
2727 EXTENT_DEFRAG, 1, cached_state);
2728 if (ret) {
2729 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05002730 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00002731 /* the inode is shared */
2732 new = record_old_file_extents(inode, ordered_extent);
2733
2734 clear_extent_bit(io_tree, ordered_extent->file_offset,
2735 ordered_extent->file_offset + ordered_extent->len - 1,
2736 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2737 }
2738
Josef Bacik0cb59c92010-07-02 12:14:14 -04002739 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002740 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002741 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002742 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002743 if (IS_ERR(trans)) {
2744 ret = PTR_ERR(trans);
2745 trans = NULL;
2746 goto out_unlock;
2747 }
Chris Masona79b7d42014-05-22 16:18:52 -07002748
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002749 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002750
Chris Masonc8b97812008-10-29 14:49:59 -04002751 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002752 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002753 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002754 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00002755 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002756 ordered_extent->file_offset,
2757 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002758 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002759 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04002760 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002761 ret = insert_reserved_file_extent(trans, inode,
2762 ordered_extent->file_offset,
2763 ordered_extent->start,
2764 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04002765 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08002766 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002767 BTRFS_FILE_EXTENT_REG);
Miao Xiee570fd22014-06-19 10:42:50 +08002768 if (!ret)
2769 btrfs_release_delalloc_bytes(root,
2770 ordered_extent->start,
2771 ordered_extent->disk_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002772 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002773 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2774 ordered_extent->file_offset, ordered_extent->len,
2775 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002776 if (ret < 0) {
2777 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002778 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002779 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002780
Chris Masone6dcd2d2008-07-17 12:53:50 -04002781 add_pending_csums(trans, inode, ordered_extent->file_offset,
2782 &ordered_extent->list);
2783
Josef Bacik6c760c02012-11-09 10:53:21 -05002784 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2785 ret = btrfs_update_inode_fallback(trans, root, inode);
2786 if (ret) { /* -ENOMEM or corruption */
2787 btrfs_abort_transaction(trans, root, ret);
2788 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002789 }
2790 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002791out_unlock:
2792 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2793 ordered_extent->file_offset +
2794 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00002795out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002796 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04002797 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002798 if (trans)
2799 btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002800
Josef Bacik77cef2e2013-08-29 13:57:21 -04002801 if (ret || truncated) {
2802 u64 start, end;
2803
2804 if (truncated)
2805 start = ordered_extent->file_offset + logical_len;
2806 else
2807 start = ordered_extent->file_offset;
2808 end = ordered_extent->file_offset + ordered_extent->len - 1;
2809 clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS);
2810
2811 /* Drop the cache for the part of the extent we didn't write. */
2812 btrfs_drop_extent_cache(inode, start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04002813
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002814 /*
2815 * If the ordered extent had an IOERR or something else went
2816 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04002817 * back to the allocator. We only free the extent in the
2818 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002819 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04002820 if ((ret || !logical_len) &&
2821 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002822 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
2823 btrfs_free_reserved_extent(root, ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08002824 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002825 }
2826
2827
Josef Bacik5fd02042012-05-02 14:00:54 -04002828 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002829 * This needs to be done to make sure anybody waiting knows we are done
2830 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002831 */
2832 btrfs_remove_ordered_extent(inode, ordered_extent);
2833
Liu Bo38c227d2013-01-29 03:18:40 +00002834 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08002835 if (new) {
2836 if (ret) {
2837 free_sa_defrag_extent(new);
2838 atomic_dec(&root->fs_info->defrag_running);
2839 } else {
2840 relink_file_extents(new);
2841 }
2842 }
Liu Bo38c227d2013-01-29 03:18:40 +00002843
Chris Masone6dcd2d2008-07-17 12:53:50 -04002844 /* once for us */
2845 btrfs_put_ordered_extent(ordered_extent);
2846 /* once for the tree */
2847 btrfs_put_ordered_extent(ordered_extent);
2848
Josef Bacik5fd02042012-05-02 14:00:54 -04002849 return ret;
2850}
2851
2852static void finish_ordered_fn(struct btrfs_work *work)
2853{
2854 struct btrfs_ordered_extent *ordered_extent;
2855 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2856 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002857}
2858
Christoph Hellwigb2950862008-12-02 09:54:17 -05002859static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04002860 struct extent_state *state, int uptodate)
2861{
Josef Bacik5fd02042012-05-02 14:00:54 -04002862 struct inode *inode = page->mapping->host;
2863 struct btrfs_root *root = BTRFS_I(inode)->root;
2864 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08002865 struct btrfs_workqueue *wq;
2866 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04002867
liubo1abe9b82011-03-24 11:18:59 +00002868 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2869
Chris Mason8b62b722009-09-02 16:53:46 -04002870 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002871 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2872 end - start + 1, uptodate))
2873 return 0;
2874
Liu Bo9e0af232014-08-15 23:36:53 +08002875 if (btrfs_is_free_space_inode(inode)) {
2876 wq = root->fs_info->endio_freespace_worker;
2877 func = btrfs_freespace_write_helper;
2878 } else {
2879 wq = root->fs_info->endio_write_workers;
2880 func = btrfs_endio_write_helper;
2881 }
Josef Bacik5fd02042012-05-02 14:00:54 -04002882
Liu Bo9e0af232014-08-15 23:36:53 +08002883 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
2884 NULL);
2885 btrfs_queue_work(wq, &ordered_extent->work);
Josef Bacik5fd02042012-05-02 14:00:54 -04002886
2887 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04002888}
2889
Chris Masond352ac62008-09-29 15:18:18 -04002890/*
Chris Masond352ac62008-09-29 15:18:18 -04002891 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002892 * if there's a match, we allow the bio to finish. If not, the code in
2893 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002894 */
Miao Xiefacc8a222013-07-25 19:22:34 +08002895static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
2896 u64 phy_offset, struct page *page,
2897 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002898{
Miao Xie4eee4fa2012-12-21 09:17:45 +00002899 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04002900 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002901 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04002902 char *kaddr;
Chris Masonff79f812007-10-15 16:22:25 -04002903 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiefacc8a222013-07-25 19:22:34 +08002904 u32 csum_expected;
Chris Masonff79f812007-10-15 16:22:25 -04002905 u32 csum = ~(u32)0;
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002906 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
2907 DEFAULT_RATELIMIT_BURST);
Chris Masond1310b22008-01-24 16:13:08 -05002908
Chris Masond20f7042008-12-08 16:58:54 -05002909 if (PageChecked(page)) {
2910 ClearPageChecked(page);
2911 goto good;
2912 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002913
2914 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Jan Schmidt08d2f342011-05-04 16:18:50 +02002915 goto good;
Chris Masond20f7042008-12-08 16:58:54 -05002916
Yan Zheng17d217f2008-12-12 10:03:38 -05002917 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002918 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002919 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
2920 GFP_NOFS);
2921 return 0;
2922 }
2923
Miao Xiefacc8a222013-07-25 19:22:34 +08002924 phy_offset >>= inode->i_sb->s_blocksize_bits;
2925 csum_expected = *(((u32 *)io_bio->csum) + phy_offset);
Chris Masond3977122009-01-05 21:25:51 -05002926
Miao Xiefacc8a222013-07-25 19:22:34 +08002927 kaddr = kmap_atomic(page);
Liu Bob0496682013-03-14 14:57:45 +00002928 csum = btrfs_csum_data(kaddr + offset, csum, end - start + 1);
Chris Masonff79f812007-10-15 16:22:25 -04002929 btrfs_csum_final(csum, (char *)&csum);
Miao Xiefacc8a222013-07-25 19:22:34 +08002930 if (csum != csum_expected)
Chris Mason07157aa2007-08-30 08:50:51 -04002931 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002932
Cong Wang7ac687d2011-11-25 23:14:28 +08002933 kunmap_atomic(kaddr);
Chris Masond20f7042008-12-08 16:58:54 -05002934good:
Chris Mason07157aa2007-08-30 08:50:51 -04002935 return 0;
2936
2937zeroit:
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002938 if (__ratelimit(&_rs))
Miao Xiefacc8a222013-07-25 19:22:34 +08002939 btrfs_info(root->fs_info, "csum failed ino %llu off %llu csum %u expected csum %u",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002940 btrfs_ino(page->mapping->host), start, csum, csum_expected);
Chris Masondb945352007-10-15 16:15:53 -04002941 memset(kaddr + offset, 1, end - start + 1);
2942 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002943 kunmap_atomic(kaddr);
Miao Xiefacc8a222013-07-25 19:22:34 +08002944 if (csum_expected == 0)
Chris Mason3b951512008-04-17 11:29:12 -04002945 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04002946 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04002947}
Chris Masonb888db22007-08-27 16:49:44 -04002948
Yan, Zheng24bbcf042009-11-12 09:36:34 +00002949struct delayed_iput {
2950 struct list_head list;
2951 struct inode *inode;
2952};
2953
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002954/* JDM: If this is fs-wide, why can't we add a pointer to
2955 * btrfs_inode instead and avoid the allocation? */
Yan, Zheng24bbcf042009-11-12 09:36:34 +00002956void btrfs_add_delayed_iput(struct inode *inode)
2957{
2958 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2959 struct delayed_iput *delayed;
2960
2961 if (atomic_add_unless(&inode->i_count, -1, 1))
2962 return;
2963
2964 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2965 delayed->inode = inode;
2966
2967 spin_lock(&fs_info->delayed_iput_lock);
2968 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2969 spin_unlock(&fs_info->delayed_iput_lock);
2970}
2971
2972void btrfs_run_delayed_iputs(struct btrfs_root *root)
2973{
2974 LIST_HEAD(list);
2975 struct btrfs_fs_info *fs_info = root->fs_info;
2976 struct delayed_iput *delayed;
2977 int empty;
2978
2979 spin_lock(&fs_info->delayed_iput_lock);
2980 empty = list_empty(&fs_info->delayed_iputs);
2981 spin_unlock(&fs_info->delayed_iput_lock);
2982 if (empty)
2983 return;
2984
Yan, Zheng24bbcf042009-11-12 09:36:34 +00002985 spin_lock(&fs_info->delayed_iput_lock);
2986 list_splice_init(&fs_info->delayed_iputs, &list);
2987 spin_unlock(&fs_info->delayed_iput_lock);
2988
2989 while (!list_empty(&list)) {
2990 delayed = list_entry(list.next, struct delayed_iput, list);
2991 list_del(&delayed->list);
2992 iput(delayed->inode);
2993 kfree(delayed);
2994 }
Yan, Zheng24bbcf042009-11-12 09:36:34 +00002995}
2996
Yan, Zhengd68fc572010-05-16 10:49:58 -04002997/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08002998 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04002999 * files in the subvolume, it removes orphan item and frees block_rsv
3000 * structure.
3001 */
3002void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3003 struct btrfs_root *root)
3004{
Josef Bacik90290e12011-12-02 15:44:12 -05003005 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003006 int ret;
3007
Josef Bacik8a35d952012-05-23 14:26:42 -04003008 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04003009 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
3010 return;
3011
Josef Bacik90290e12011-12-02 15:44:12 -05003012 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003013 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05003014 spin_unlock(&root->orphan_lock);
3015 return;
3016 }
3017
3018 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
3019 spin_unlock(&root->orphan_lock);
3020 return;
3021 }
3022
3023 block_rsv = root->orphan_block_rsv;
3024 root->orphan_block_rsv = NULL;
3025 spin_unlock(&root->orphan_lock);
3026
Miao Xie27cdeb72014-04-02 19:51:05 +08003027 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
Yan, Zhengd68fc572010-05-16 10:49:58 -04003028 btrfs_root_refs(&root->root_item) > 0) {
3029 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
3030 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003031 if (ret)
3032 btrfs_abort_transaction(trans, root, ret);
3033 else
Miao Xie27cdeb72014-04-02 19:51:05 +08003034 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
3035 &root->state);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003036 }
3037
Josef Bacik90290e12011-12-02 15:44:12 -05003038 if (block_rsv) {
3039 WARN_ON(block_rsv->size > 0);
3040 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003041 }
3042}
3043
3044/*
Josef Bacik7b128762008-07-24 12:17:14 -04003045 * This creates an orphan entry for the given inode in case something goes
3046 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003047 *
3048 * NOTE: caller of this function should reserve 5 units of metadata for
3049 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04003050 */
3051int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
3052{
3053 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003054 struct btrfs_block_rsv *block_rsv = NULL;
3055 int reserve = 0;
3056 int insert = 0;
3057 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003058
Yan, Zhengd68fc572010-05-16 10:49:58 -04003059 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06003060 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003061 if (!block_rsv)
3062 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04003063 }
3064
Yan, Zhengd68fc572010-05-16 10:49:58 -04003065 spin_lock(&root->orphan_lock);
3066 if (!root->orphan_block_rsv) {
3067 root->orphan_block_rsv = block_rsv;
3068 } else if (block_rsv) {
3069 btrfs_free_block_rsv(root, block_rsv);
3070 block_rsv = NULL;
3071 }
Josef Bacik7b128762008-07-24 12:17:14 -04003072
Josef Bacik8a35d952012-05-23 14:26:42 -04003073 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3074 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003075#if 0
3076 /*
3077 * For proper ENOSPC handling, we should do orphan
3078 * cleanup when mounting. But this introduces backward
3079 * compatibility issue.
3080 */
3081 if (!xchg(&root->orphan_item_inserted, 1))
3082 insert = 2;
3083 else
3084 insert = 1;
3085#endif
3086 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003087 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003088 }
Josef Bacik7b128762008-07-24 12:17:14 -04003089
Josef Bacik72ac3c02012-05-23 14:13:11 -04003090 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3091 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003092 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003093 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003094
Yan, Zhengd68fc572010-05-16 10:49:58 -04003095 /* grab metadata reservation from transaction handle */
3096 if (reserve) {
3097 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003098 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04003099 }
3100
3101 /* insert an orphan item to track this unlinked/truncated file */
3102 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08003103 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003104 if (ret) {
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003105 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003106 if (reserve) {
3107 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3108 &BTRFS_I(inode)->runtime_flags);
3109 btrfs_orphan_release_metadata(inode);
3110 }
3111 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003112 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3113 &BTRFS_I(inode)->runtime_flags);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003114 btrfs_abort_transaction(trans, root, ret);
3115 return ret;
3116 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003117 }
3118 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003119 }
3120
3121 /* insert an orphan item to track subvolume contains orphan files */
3122 if (insert >= 2) {
3123 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
3124 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003125 if (ret && ret != -EEXIST) {
3126 btrfs_abort_transaction(trans, root, ret);
3127 return ret;
3128 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003129 }
3130 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003131}
3132
3133/*
3134 * We have done the truncate/delete so we can go ahead and remove the orphan
3135 * item for this particular inode.
3136 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003137static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3138 struct inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003139{
3140 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003141 int delete_item = 0;
3142 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003143 int ret = 0;
3144
Yan, Zhengd68fc572010-05-16 10:49:58 -04003145 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003146 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3147 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003148 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003149
Josef Bacik72ac3c02012-05-23 14:13:11 -04003150 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3151 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003152 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003153 spin_unlock(&root->orphan_lock);
3154
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003155 if (delete_item) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003156 atomic_dec(&root->orphan_inodes);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003157 if (trans)
3158 ret = btrfs_del_orphan_item(trans, root,
3159 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04003160 }
Josef Bacik7b128762008-07-24 12:17:14 -04003161
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003162 if (release_rsv)
3163 btrfs_orphan_release_metadata(inode);
3164
Josef Bacik4ef31a42013-08-13 14:10:08 -04003165 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003166}
3167
3168/*
3169 * this cleans up any orphans that may be left on the list from the last use
3170 * of this root.
3171 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003172int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003173{
3174 struct btrfs_path *path;
3175 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003176 struct btrfs_key key, found_key;
3177 struct btrfs_trans_handle *trans;
3178 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003179 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003180 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3181
Yan, Zhengd68fc572010-05-16 10:49:58 -04003182 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003183 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003184
3185 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003186 if (!path) {
3187 ret = -ENOMEM;
3188 goto out;
3189 }
Josef Bacik7b128762008-07-24 12:17:14 -04003190 path->reada = -1;
3191
3192 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003193 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003194 key.offset = (u64)-1;
3195
Josef Bacik7b128762008-07-24 12:17:14 -04003196 while (1) {
3197 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003198 if (ret < 0)
3199 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003200
3201 /*
3202 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003203 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003204 * find the key and see if we have stuff that matches
3205 */
3206 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003207 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003208 if (path->slots[0] == 0)
3209 break;
3210 path->slots[0]--;
3211 }
3212
3213 /* pull out the item */
3214 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003215 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3216
3217 /* make sure the item matches what we want */
3218 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3219 break;
David Sterba962a2982014-06-04 18:41:45 +02003220 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003221 break;
3222
3223 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003224 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003225
3226 /*
3227 * this is where we are basically btrfs_lookup, without the
3228 * crossing root thing. we store the inode number in the
3229 * offset of the orphan item.
3230 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003231
3232 if (found_key.offset == last_objectid) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003233 btrfs_err(root->fs_info,
3234 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003235 ret = -EINVAL;
3236 goto out;
3237 }
3238
3239 last_objectid = found_key.offset;
3240
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003241 found_key.objectid = found_key.offset;
3242 found_key.type = BTRFS_INODE_ITEM_KEY;
3243 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00003244 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303245 ret = PTR_ERR_OR_ZERO(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003246 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003247 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003248
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003249 if (ret == -ESTALE && root == root->fs_info->tree_root) {
3250 struct btrfs_root *dead_root;
3251 struct btrfs_fs_info *fs_info = root->fs_info;
3252 int is_dead_root = 0;
3253
3254 /*
3255 * this is an orphan in the tree root. Currently these
3256 * could come from 2 sources:
3257 * a) a snapshot deletion in progress
3258 * b) a free space cache inode
3259 * We need to distinguish those two, as the snapshot
3260 * orphan must not get deleted.
3261 * find_dead_roots already ran before us, so if this
3262 * is a snapshot deletion, we should find the root
3263 * in the dead_roots list
3264 */
3265 spin_lock(&fs_info->trans_lock);
3266 list_for_each_entry(dead_root, &fs_info->dead_roots,
3267 root_list) {
3268 if (dead_root->root_key.objectid ==
3269 found_key.objectid) {
3270 is_dead_root = 1;
3271 break;
3272 }
3273 }
3274 spin_unlock(&fs_info->trans_lock);
3275 if (is_dead_root) {
3276 /* prevent this orphan from being found again */
3277 key.offset = found_key.objectid - 1;
3278 continue;
3279 }
3280 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003281 /*
3282 * Inode is already gone but the orphan item is still there,
3283 * kill the orphan item.
3284 */
3285 if (ret == -ESTALE) {
3286 trans = btrfs_start_transaction(root, 1);
3287 if (IS_ERR(trans)) {
3288 ret = PTR_ERR(trans);
3289 goto out;
3290 }
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003291 btrfs_debug(root->fs_info, "auto deleting %Lu",
3292 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003293 ret = btrfs_del_orphan_item(trans, root,
3294 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003295 btrfs_end_transaction(trans, root);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003296 if (ret)
3297 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003298 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003299 }
Josef Bacik7b128762008-07-24 12:17:14 -04003300
Josef Bacik7b128762008-07-24 12:17:14 -04003301 /*
3302 * add this inode to the orphan list so btrfs_orphan_del does
3303 * the proper thing when we hit it
3304 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003305 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3306 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003307 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003308
Josef Bacik7b128762008-07-24 12:17:14 -04003309 /* if we have links, this was a truncate, lets do that */
3310 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303311 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003312 iput(inode);
3313 continue;
3314 }
Josef Bacik7b128762008-07-24 12:17:14 -04003315 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003316
3317 /* 1 for the orphan item deletion. */
3318 trans = btrfs_start_transaction(root, 1);
3319 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003320 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003321 ret = PTR_ERR(trans);
3322 goto out;
3323 }
3324 ret = btrfs_orphan_add(trans, inode);
3325 btrfs_end_transaction(trans, root);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003326 if (ret) {
3327 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003328 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003329 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003330
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003331 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003332 if (ret)
3333 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003334 } else {
3335 nr_unlink++;
3336 }
3337
3338 /* this will do delete_inode and everything for us */
3339 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003340 if (ret)
3341 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003342 }
Miao Xie3254c872011-11-10 20:45:05 -05003343 /* release the path since we're done with it */
3344 btrfs_release_path(path);
3345
Yan, Zhengd68fc572010-05-16 10:49:58 -04003346 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3347
3348 if (root->orphan_block_rsv)
3349 btrfs_block_rsv_release(root, root->orphan_block_rsv,
3350 (u64)-1);
3351
Miao Xie27cdeb72014-04-02 19:51:05 +08003352 if (root->orphan_block_rsv ||
3353 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003354 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003355 if (!IS_ERR(trans))
3356 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003357 }
Josef Bacik7b128762008-07-24 12:17:14 -04003358
3359 if (nr_unlink)
David Sterba4884b472013-03-20 13:31:27 +00003360 btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003361 if (nr_truncate)
David Sterba4884b472013-03-20 13:31:27 +00003362 btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003363
3364out:
3365 if (ret)
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003366 btrfs_crit(root->fs_info,
3367 "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003368 btrfs_free_path(path);
3369 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003370}
3371
Chris Masond352ac62008-09-29 15:18:18 -04003372/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003373 * very simple check to peek ahead in the leaf looking for xattrs. If we
3374 * don't find any xattrs, we know there can't be any acls.
3375 *
3376 * slot is the slot the inode is in, objectid is the objectid of the inode
3377 */
3378static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003379 int slot, u64 objectid,
3380 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003381{
3382 u32 nritems = btrfs_header_nritems(leaf);
3383 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003384 static u64 xattr_access = 0;
3385 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003386 int scanned = 0;
3387
Josef Bacikf23b5a52013-06-19 10:16:26 -04003388 if (!xattr_access) {
3389 xattr_access = btrfs_name_hash(POSIX_ACL_XATTR_ACCESS,
3390 strlen(POSIX_ACL_XATTR_ACCESS));
3391 xattr_default = btrfs_name_hash(POSIX_ACL_XATTR_DEFAULT,
3392 strlen(POSIX_ACL_XATTR_DEFAULT));
3393 }
3394
Chris Mason46a53cc2009-04-27 11:47:50 -04003395 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003396 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003397 while (slot < nritems) {
3398 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3399
3400 /* we found a different objectid, there must not be acls */
3401 if (found_key.objectid != objectid)
3402 return 0;
3403
3404 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003405 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003406 if (*first_xattr_slot == -1)
3407 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003408 if (found_key.offset == xattr_access ||
3409 found_key.offset == xattr_default)
3410 return 1;
3411 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003412
3413 /*
3414 * we found a key greater than an xattr key, there can't
3415 * be any acls later on
3416 */
3417 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3418 return 0;
3419
3420 slot++;
3421 scanned++;
3422
3423 /*
3424 * it goes inode, inode backrefs, xattrs, extents,
3425 * so if there are a ton of hard links to an inode there can
3426 * be a lot of backrefs. Don't waste time searching too hard,
3427 * this is just an optimization
3428 */
3429 if (scanned >= 8)
3430 break;
3431 }
3432 /* we hit the end of the leaf before we found an xattr or
3433 * something larger than an xattr. We have to assume the inode
3434 * has acls
3435 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003436 if (*first_xattr_slot == -1)
3437 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003438 return 1;
3439}
3440
3441/*
Chris Masond352ac62008-09-29 15:18:18 -04003442 * read an inode from the btree into the in-memory inode
3443 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003444static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003445{
3446 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003447 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003448 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04003449 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04003450 struct btrfs_root *root = BTRFS_I(inode)->root;
3451 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003452 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003453 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003454 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003455 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003456 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003457 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003458
3459 ret = btrfs_fill_inode(inode, &rdev);
3460 if (!ret)
3461 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003462
3463 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07003464 if (!path)
3465 goto make_bad;
3466
Chris Mason39279cc2007-06-12 06:35:45 -04003467 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003468
Chris Mason39279cc2007-06-12 06:35:45 -04003469 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003470 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04003471 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04003472
Chris Mason5f39d392007-10-15 16:14:19 -04003473 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003474
3475 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003476 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003477
Chris Mason5f39d392007-10-15 16:14:19 -04003478 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3479 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003480 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003481 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003482 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3483 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04003484 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003485
3486 tspec = btrfs_inode_atime(inode_item);
3487 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3488 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3489
3490 tspec = btrfs_inode_mtime(inode_item);
3491 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3492 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3493
3494 tspec = btrfs_inode_ctime(inode_item);
3495 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3496 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3497
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003498 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003499 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003500 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3501
3502 /*
3503 * If we were modified in the current generation and evicted from memory
3504 * and then re-read we need to do a full sync since we don't have any
3505 * idea about which extents were modified before we were evicted from
3506 * cache.
3507 */
3508 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
3509 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3510 &BTRFS_I(inode)->runtime_flags);
3511
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003512 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003513 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003514 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003515 rdev = btrfs_inode_rdev(leaf, inode_item);
3516
Josef Bacikaec74772008-07-24 12:12:38 -04003517 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003518 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003519
3520cache_index:
3521 path->slots[0]++;
3522 if (inode->i_nlink != 1 ||
3523 path->slots[0] >= btrfs_header_nritems(leaf))
3524 goto cache_acl;
3525
3526 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
3527 if (location.objectid != btrfs_ino(inode))
3528 goto cache_acl;
3529
3530 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3531 if (location.type == BTRFS_INODE_REF_KEY) {
3532 struct btrfs_inode_ref *ref;
3533
3534 ref = (struct btrfs_inode_ref *)ptr;
3535 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3536 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3537 struct btrfs_inode_extref *extref;
3538
3539 extref = (struct btrfs_inode_extref *)ptr;
3540 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3541 extref);
3542 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003543cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003544 /*
3545 * try to precache a NULL acl entry for files that don't have
3546 * any xattrs or acls
3547 */
Li Zefan33345d012011-04-20 10:31:50 +08003548 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003549 btrfs_ino(inode), &first_xattr_slot);
3550 if (first_xattr_slot != -1) {
3551 path->slots[0] = first_xattr_slot;
3552 ret = btrfs_load_inode_props(inode, path);
3553 if (ret)
3554 btrfs_err(root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003555 "error loading props for ino %llu (root %llu): %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003556 btrfs_ino(inode),
3557 root->root_key.objectid, ret);
3558 }
3559 btrfs_free_path(path);
3560
Al Viro72c04902009-06-24 16:58:48 -04003561 if (!maybe_acls)
3562 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003563
Chris Mason39279cc2007-06-12 06:35:45 -04003564 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003565 case S_IFREG:
3566 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003567 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05003568 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003569 inode->i_fop = &btrfs_file_operations;
3570 inode->i_op = &btrfs_file_inode_operations;
3571 break;
3572 case S_IFDIR:
3573 inode->i_fop = &btrfs_dir_file_operations;
3574 if (root == root->fs_info->tree_root)
3575 inode->i_op = &btrfs_dir_ro_inode_operations;
3576 else
3577 inode->i_op = &btrfs_dir_inode_operations;
3578 break;
3579 case S_IFLNK:
3580 inode->i_op = &btrfs_symlink_inode_operations;
3581 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04003582 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003583 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003584 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003585 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003586 init_special_inode(inode, inode->i_mode, rdev);
3587 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003588 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003589
3590 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003591 return;
3592
3593make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003594 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003595 make_bad_inode(inode);
3596}
3597
Chris Masond352ac62008-09-29 15:18:18 -04003598/*
3599 * given a leaf and an inode, copy the inode fields into the leaf
3600 */
Chris Masone02119d2008-09-05 16:13:11 -04003601static void fill_inode_item(struct btrfs_trans_handle *trans,
3602 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003603 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003604 struct inode *inode)
3605{
Liu Bo51fab692012-12-27 09:01:21 +00003606 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003607
Liu Bo51fab692012-12-27 09:01:21 +00003608 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003609
Liu Bo51fab692012-12-27 09:01:21 +00003610 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3611 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3612 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3613 &token);
3614 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3615 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003616
Liu Bo51fab692012-12-27 09:01:21 +00003617 btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item),
3618 inode->i_atime.tv_sec, &token);
3619 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item),
3620 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003621
Liu Bo51fab692012-12-27 09:01:21 +00003622 btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item),
3623 inode->i_mtime.tv_sec, &token);
3624 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item),
3625 inode->i_mtime.tv_nsec, &token);
3626
3627 btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item),
3628 inode->i_ctime.tv_sec, &token);
3629 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item),
3630 inode->i_ctime.tv_nsec, &token);
3631
3632 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3633 &token);
3634 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3635 &token);
3636 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3637 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3638 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3639 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3640 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003641}
3642
Chris Masond352ac62008-09-29 15:18:18 -04003643/*
3644 * copy everything in the in-memory inode into the btree.
3645 */
Chris Mason21151332011-11-10 20:39:08 -05003646static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003647 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003648{
3649 struct btrfs_inode_item *inode_item;
3650 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003651 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003652 int ret;
3653
3654 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003655 if (!path)
3656 return -ENOMEM;
3657
Chris Masonb9473432009-03-13 11:00:37 -04003658 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003659 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3660 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003661 if (ret) {
3662 if (ret > 0)
3663 ret = -ENOENT;
3664 goto failed;
3665 }
3666
Chris Mason5f39d392007-10-15 16:14:19 -04003667 leaf = path->nodes[0];
3668 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003669 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003670
Chris Masone02119d2008-09-05 16:13:11 -04003671 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003672 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003673 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003674 ret = 0;
3675failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003676 btrfs_free_path(path);
3677 return ret;
3678}
3679
Chris Masond352ac62008-09-29 15:18:18 -04003680/*
Chris Mason21151332011-11-10 20:39:08 -05003681 * copy everything in the in-memory inode into the btree.
3682 */
3683noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3684 struct btrfs_root *root, struct inode *inode)
3685{
3686 int ret;
3687
3688 /*
3689 * If the inode is a free space inode, we can deadlock during commit
3690 * if we put it into the delayed code.
3691 *
3692 * The data relocation inode should also be directly updated
3693 * without delay
3694 */
Liu Bo83eea1f2012-07-10 05:28:39 -06003695 if (!btrfs_is_free_space_inode(inode)
Chris Mason21151332011-11-10 20:39:08 -05003696 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003697 btrfs_update_root_times(trans, root);
3698
Chris Mason21151332011-11-10 20:39:08 -05003699 ret = btrfs_delayed_update_inode(trans, root, inode);
3700 if (!ret)
3701 btrfs_set_inode_last_trans(trans, inode);
3702 return ret;
3703 }
3704
3705 return btrfs_update_inode_item(trans, root, inode);
3706}
3707
Josef Bacikbe6aef62012-10-22 15:43:12 -04003708noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3709 struct btrfs_root *root,
3710 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003711{
3712 int ret;
3713
3714 ret = btrfs_update_inode(trans, root, inode);
3715 if (ret == -ENOSPC)
3716 return btrfs_update_inode_item(trans, root, inode);
3717 return ret;
3718}
3719
3720/*
Chris Masond352ac62008-09-29 15:18:18 -04003721 * unlink helper that gets used here in inode.c and in the tree logging
3722 * recovery code. It remove a link in a directory with a given name, and
3723 * also drops the back refs in the inode to the directory
3724 */
Al Viro92986792011-03-04 17:14:37 +00003725static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3726 struct btrfs_root *root,
3727 struct inode *dir, struct inode *inode,
3728 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003729{
3730 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003731 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003732 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003733 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003734 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003735 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003736 u64 ino = btrfs_ino(inode);
3737 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003738
3739 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003740 if (!path) {
3741 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003742 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003743 }
3744
Chris Masonb9473432009-03-13 11:00:37 -04003745 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003746 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003747 name, name_len, -1);
3748 if (IS_ERR(di)) {
3749 ret = PTR_ERR(di);
3750 goto err;
3751 }
3752 if (!di) {
3753 ret = -ENOENT;
3754 goto err;
3755 }
Chris Mason5f39d392007-10-15 16:14:19 -04003756 leaf = path->nodes[0];
3757 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003758 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003759 if (ret)
3760 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003761 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003762
Miao Xie67de1172013-12-26 13:07:06 +08003763 /*
3764 * If we don't have dir index, we have to get it by looking up
3765 * the inode ref, since we get the inode ref, remove it directly,
3766 * it is unnecessary to do delayed deletion.
3767 *
3768 * But if we have dir index, needn't search inode ref to get it.
3769 * Since the inode ref is close to the inode item, it is better
3770 * that we delay to delete it, and just do this deletion when
3771 * we update the inode item.
3772 */
3773 if (BTRFS_I(inode)->dir_index) {
3774 ret = btrfs_delayed_delete_inode_ref(inode);
3775 if (!ret) {
3776 index = BTRFS_I(inode)->dir_index;
3777 goto skip_backref;
3778 }
3779 }
3780
Li Zefan33345d012011-04-20 10:31:50 +08003781 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3782 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003783 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003784 btrfs_info(root->fs_info,
3785 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003786 name_len, name, ino, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003787 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003788 goto err;
3789 }
Miao Xie67de1172013-12-26 13:07:06 +08003790skip_backref:
Miao Xie16cdcec2011-04-22 18:12:22 +08003791 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003792 if (ret) {
3793 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003794 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003795 }
Chris Mason39279cc2007-06-12 06:35:45 -04003796
Chris Masone02119d2008-09-05 16:13:11 -04003797 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08003798 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003799 if (ret != 0 && ret != -ENOENT) {
3800 btrfs_abort_transaction(trans, root, ret);
3801 goto err;
3802 }
Chris Masone02119d2008-09-05 16:13:11 -04003803
3804 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
3805 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04003806 if (ret == -ENOENT)
3807 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003808 else if (ret)
3809 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003810err:
3811 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003812 if (ret)
3813 goto out;
3814
3815 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003816 inode_inc_iversion(inode);
3817 inode_inc_iversion(dir);
Chris Masone02119d2008-09-05 16:13:11 -04003818 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Tsutomu Itohb9959292012-06-25 21:25:22 -06003819 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04003820out:
Chris Mason39279cc2007-06-12 06:35:45 -04003821 return ret;
3822}
3823
Al Viro92986792011-03-04 17:14:37 +00003824int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3825 struct btrfs_root *root,
3826 struct inode *dir, struct inode *inode,
3827 const char *name, int name_len)
3828{
3829 int ret;
3830 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3831 if (!ret) {
Zach Brown8b558c52013-10-16 12:10:34 -07003832 drop_nlink(inode);
Al Viro92986792011-03-04 17:14:37 +00003833 ret = btrfs_update_inode(trans, root, inode);
3834 }
3835 return ret;
3836}
Yan, Zhenga22285a2010-05-16 10:48:46 -04003837
3838/*
3839 * helper to start transaction for unlink and rmdir.
3840 *
Josef Bacikd52be812013-05-29 14:54:47 -04003841 * unlink and rmdir are special in btrfs, they do not always free space, so
3842 * if we cannot make our reservations the normal way try and see if there is
3843 * plenty of slack room in the global reserve to migrate, otherwise we cannot
3844 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04003845 */
Josef Bacikd52be812013-05-29 14:54:47 -04003846static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003847{
3848 struct btrfs_trans_handle *trans;
3849 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003850 int ret;
3851
Josef Bacike70bea52011-10-11 14:18:24 -04003852 /*
3853 * 1 for the possible orphan item
3854 * 1 for the dir item
3855 * 1 for the dir index
3856 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04003857 * 1 for the inode
3858 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04003859 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003860 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
3861 return trans;
3862
Josef Bacikd52be812013-05-29 14:54:47 -04003863 if (PTR_ERR(trans) == -ENOSPC) {
3864 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003865
Josef Bacikd52be812013-05-29 14:54:47 -04003866 trans = btrfs_start_transaction(root, 0);
3867 if (IS_ERR(trans))
3868 return trans;
3869 ret = btrfs_cond_migrate_bytes(root->fs_info,
3870 &root->fs_info->trans_block_rsv,
3871 num_bytes, 5);
3872 if (ret) {
3873 btrfs_end_transaction(trans, root);
3874 return ERR_PTR(ret);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003875 }
Josef Bacik5a77d762011-11-01 14:32:23 -04003876 trans->block_rsv = &root->fs_info->trans_block_rsv;
Josef Bacikd52be812013-05-29 14:54:47 -04003877 trans->bytes_reserved = num_bytes;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003878 }
Josef Bacikd52be812013-05-29 14:54:47 -04003879 return trans;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003880}
3881
Chris Mason39279cc2007-06-12 06:35:45 -04003882static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3883{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003884 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003885 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04003886 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003887 int ret;
3888
Josef Bacikd52be812013-05-29 14:54:47 -04003889 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003890 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003891 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003892
Chris Mason12fcfd22009-03-24 10:24:20 -04003893 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
3894
Chris Masone02119d2008-09-05 16:13:11 -04003895 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3896 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003897 if (ret)
3898 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003899
Yan, Zhenga22285a2010-05-16 10:48:46 -04003900 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04003901 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003902 if (ret)
3903 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003904 }
Josef Bacik7b128762008-07-24 12:17:14 -04003905
Tsutomu Itohb5324022011-07-19 07:27:20 +00003906out:
Josef Bacikd52be812013-05-29 14:54:47 -04003907 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003908 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04003909 return ret;
3910}
3911
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003912int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3913 struct btrfs_root *root,
3914 struct inode *dir, u64 objectid,
3915 const char *name, int name_len)
3916{
3917 struct btrfs_path *path;
3918 struct extent_buffer *leaf;
3919 struct btrfs_dir_item *di;
3920 struct btrfs_key key;
3921 u64 index;
3922 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003923 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003924
3925 path = btrfs_alloc_path();
3926 if (!path)
3927 return -ENOMEM;
3928
Li Zefan33345d012011-04-20 10:31:50 +08003929 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003930 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003931 if (IS_ERR_OR_NULL(di)) {
3932 if (!di)
3933 ret = -ENOENT;
3934 else
3935 ret = PTR_ERR(di);
3936 goto out;
3937 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003938
3939 leaf = path->nodes[0];
3940 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3941 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3942 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003943 if (ret) {
3944 btrfs_abort_transaction(trans, root, ret);
3945 goto out;
3946 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003947 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003948
3949 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3950 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003951 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003952 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003953 if (ret != -ENOENT) {
3954 btrfs_abort_transaction(trans, root, ret);
3955 goto out;
3956 }
Li Zefan33345d012011-04-20 10:31:50 +08003957 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003958 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003959 if (IS_ERR_OR_NULL(di)) {
3960 if (!di)
3961 ret = -ENOENT;
3962 else
3963 ret = PTR_ERR(di);
3964 btrfs_abort_transaction(trans, root, ret);
3965 goto out;
3966 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003967
3968 leaf = path->nodes[0];
3969 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003970 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003971 index = key.offset;
3972 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003973 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003974
Miao Xie16cdcec2011-04-22 18:12:22 +08003975 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003976 if (ret) {
3977 btrfs_abort_transaction(trans, root, ret);
3978 goto out;
3979 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003980
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003981 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003982 inode_inc_iversion(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003983 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Josef Bacik5a24e842012-08-08 10:12:59 -06003984 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003985 if (ret)
3986 btrfs_abort_transaction(trans, root, ret);
3987out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04003988 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003989 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003990}
3991
Chris Mason39279cc2007-06-12 06:35:45 -04003992static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3993{
3994 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003995 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003996 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003997 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04003998
David Sterbab3ae2442012-09-13 16:04:34 -06003999 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004000 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06004001 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
4002 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04004003
Josef Bacikd52be812013-05-29 14:54:47 -04004004 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004005 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004006 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004007
Li Zefan33345d012011-04-20 10:31:50 +08004008 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004009 err = btrfs_unlink_subvol(trans, root, dir,
4010 BTRFS_I(inode)->location.objectid,
4011 dentry->d_name.name,
4012 dentry->d_name.len);
4013 goto out;
4014 }
4015
Josef Bacik7b128762008-07-24 12:17:14 -04004016 err = btrfs_orphan_add(trans, inode);
4017 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004018 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004019
Chris Mason39279cc2007-06-12 06:35:45 -04004020 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04004021 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
4022 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05004023 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04004024 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004025out:
Josef Bacikd52be812013-05-29 14:54:47 -04004026 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004027 btrfs_btree_balance_dirty(root);
Chris Mason39544012007-12-12 14:38:19 -05004028
Chris Mason39279cc2007-06-12 06:35:45 -04004029 return err;
4030}
4031
Chris Mason323ac952008-10-01 19:05:46 -04004032/*
Chris Mason39279cc2007-06-12 06:35:45 -04004033 * this can truncate away extent items, csum items and directory items.
4034 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004035 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004036 *
4037 * csum items that cross the new i_size are truncated to the new size
4038 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004039 *
4040 * min_type is the minimum key type to truncate down to. If set to 0, this
4041 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004042 */
Yan, Zheng80825102009-11-12 09:35:36 +00004043int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4044 struct btrfs_root *root,
4045 struct inode *inode,
4046 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004047{
Chris Mason39279cc2007-06-12 06:35:45 -04004048 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004049 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004050 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004051 struct btrfs_key key;
4052 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004053 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004054 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004055 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004056 u64 item_end = 0;
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004057 u64 last_size = (u64)-1;
Yan, Zheng80825102009-11-12 09:35:36 +00004058 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004059 int found_extent;
4060 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004061 int pending_del_nr = 0;
4062 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004063 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004064 int ret;
4065 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08004066 u64 ino = btrfs_ino(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004067
4068 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004069
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004070 path = btrfs_alloc_path();
4071 if (!path)
4072 return -ENOMEM;
4073 path->reada = -1;
4074
Josef Bacik5dc562c2012-08-17 13:14:17 -04004075 /*
4076 * We want to drop from the next block forward in case this new size is
4077 * not block aligned since we will be keeping the last block of the
4078 * extent just the way it is.
4079 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004080 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4081 root == root->fs_info->tree_root)
Qu Wenruofda28322013-02-26 08:10:22 +00004082 btrfs_drop_extent_cache(inode, ALIGN(new_size,
4083 root->sectorsize), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004084
Miao Xie16cdcec2011-04-22 18:12:22 +08004085 /*
4086 * This function is also used to drop the items in the log tree before
4087 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4088 * it is used to drop the loged items. So we shouldn't kill the delayed
4089 * items.
4090 */
4091 if (min_type == 0 && root == BTRFS_I(inode)->root)
4092 btrfs_kill_delayed_inode_items(inode);
4093
Li Zefan33345d012011-04-20 10:31:50 +08004094 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004095 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004096 key.type = (u8)-1;
4097
Chris Mason85e21ba2008-01-29 15:11:36 -05004098search_again:
Chris Masonb9473432009-03-13 11:00:37 -04004099 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004100 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004101 if (ret < 0) {
4102 err = ret;
4103 goto out;
4104 }
Chris Masond3977122009-01-05 21:25:51 -05004105
Chris Mason85e21ba2008-01-29 15:11:36 -05004106 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004107 /* there are no items in the tree for us to truncate, we're
4108 * done
4109 */
Yan, Zheng80825102009-11-12 09:35:36 +00004110 if (path->slots[0] == 0)
4111 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004112 path->slots[0]--;
4113 }
4114
Chris Masond3977122009-01-05 21:25:51 -05004115 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004116 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004117 leaf = path->nodes[0];
4118 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004119 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004120
Li Zefan33345d012011-04-20 10:31:50 +08004121 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004122 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004123
Chris Mason85e21ba2008-01-29 15:11:36 -05004124 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004125 break;
4126
Chris Mason5f39d392007-10-15 16:14:19 -04004127 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004128 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004129 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004130 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004131 extent_type = btrfs_file_extent_type(leaf, fi);
4132 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004133 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004134 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04004135 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004136 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004137 path->slots[0], fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004138 }
Yan008630c2007-11-07 13:31:09 -05004139 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004140 }
Yan, Zheng80825102009-11-12 09:35:36 +00004141 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004142 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004143 } else {
4144 if (item_end < new_size)
4145 break;
4146 if (found_key.offset >= new_size)
4147 del_item = 1;
4148 else
4149 del_item = 0;
4150 }
Chris Mason39279cc2007-06-12 06:35:45 -04004151 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004152 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004153 if (found_type != BTRFS_EXTENT_DATA_KEY)
4154 goto delete;
4155
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004156 if (del_item)
4157 last_size = found_key.offset;
4158 else
4159 last_size = new_size;
4160
Chris Mason179e29e2007-11-01 11:28:41 -04004161 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004162 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004163 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004164 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004165 u64 orig_num_bytes =
4166 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004167 extent_num_bytes = ALIGN(new_size -
4168 found_key.offset,
4169 root->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004170 btrfs_set_file_extent_num_bytes(leaf, fi,
4171 extent_num_bytes);
4172 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004173 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004174 if (test_bit(BTRFS_ROOT_REF_COWS,
4175 &root->state) &&
4176 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004177 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004178 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004179 } else {
Chris Masondb945352007-10-15 16:15:53 -04004180 extent_num_bytes =
4181 btrfs_file_extent_disk_num_bytes(leaf,
4182 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004183 extent_offset = found_key.offset -
4184 btrfs_file_extent_offset(leaf, fi);
4185
Chris Mason39279cc2007-06-12 06:35:45 -04004186 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004187 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004188 if (extent_start != 0) {
4189 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004190 if (test_bit(BTRFS_ROOT_REF_COWS,
4191 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004192 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004193 }
4194 }
Chris Mason90692182008-02-08 13:49:28 -05004195 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004196 /*
4197 * we can't truncate inline items that have had
4198 * special encodings
4199 */
4200 if (!del_item &&
4201 btrfs_file_extent_compression(leaf, fi) == 0 &&
4202 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4203 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004204 u32 size = new_size - found_key.offset;
4205
Miao Xie27cdeb72014-04-02 19:51:05 +08004206 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004207 inode_sub_bytes(inode, item_end + 1 -
4208 new_size);
Chris Mason514ac8a2014-01-03 21:07:00 -08004209
4210 /*
4211 * update the ram bytes to properly reflect
4212 * the new size of our item
4213 */
4214 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
Chris Masone02119d2008-09-05 16:13:11 -04004215 size =
4216 btrfs_file_extent_calc_inline_size(size);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004217 btrfs_truncate_item(root, path, size, 1);
Miao Xie27cdeb72014-04-02 19:51:05 +08004218 } else if (test_bit(BTRFS_ROOT_REF_COWS,
4219 &root->state)) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004220 inode_sub_bytes(inode, item_end + 1 -
4221 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05004222 }
Chris Mason39279cc2007-06-12 06:35:45 -04004223 }
Chris Mason179e29e2007-11-01 11:28:41 -04004224delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004225 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004226 if (!pending_del_nr) {
4227 /* no pending yet, add ourselves */
4228 pending_del_slot = path->slots[0];
4229 pending_del_nr = 1;
4230 } else if (pending_del_nr &&
4231 path->slots[0] + 1 == pending_del_slot) {
4232 /* hop on the pending chunk */
4233 pending_del_nr++;
4234 pending_del_slot = path->slots[0];
4235 } else {
Chris Masond3977122009-01-05 21:25:51 -05004236 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004237 }
Chris Mason39279cc2007-06-12 06:35:45 -04004238 } else {
4239 break;
4240 }
Miao Xie27cdeb72014-04-02 19:51:05 +08004241 if (found_extent &&
4242 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4243 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004244 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004245 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004246 extent_num_bytes, 0,
4247 btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02004248 ino, extent_offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004249 BUG_ON(ret);
4250 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004251
Yan, Zheng80825102009-11-12 09:35:36 +00004252 if (found_type == BTRFS_INODE_ITEM_KEY)
4253 break;
4254
4255 if (path->slots[0] == 0 ||
4256 path->slots[0] != pending_del_slot) {
Yan, Zheng80825102009-11-12 09:35:36 +00004257 if (pending_del_nr) {
4258 ret = btrfs_del_items(trans, root, path,
4259 pending_del_slot,
4260 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004261 if (ret) {
4262 btrfs_abort_transaction(trans,
4263 root, ret);
4264 goto error;
4265 }
Yan, Zheng80825102009-11-12 09:35:36 +00004266 pending_del_nr = 0;
4267 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004268 btrfs_release_path(path);
Chris Mason85e21ba2008-01-29 15:11:36 -05004269 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004270 } else {
4271 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004272 }
Chris Mason39279cc2007-06-12 06:35:45 -04004273 }
Yan, Zheng80825102009-11-12 09:35:36 +00004274out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004275 if (pending_del_nr) {
4276 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4277 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004278 if (ret)
4279 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004280 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004281error:
Filipe Mananadac57052014-08-29 20:54:26 +01004282 if (last_size != (u64)-1 &&
4283 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004284 btrfs_ordered_update_i_size(inode, last_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004285 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00004286 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004287}
4288
Chris Masona52d9a82007-08-27 16:49:44 -04004289/*
Josef Bacik2aaa6652012-08-29 14:27:18 -04004290 * btrfs_truncate_page - read, zero a chunk and write a page
4291 * @inode - inode that we're zeroing
4292 * @from - the offset to start zeroing
4293 * @len - the length to zero, 0 to zero the entire range respective to the
4294 * offset
4295 * @front - zero up to the offset instead of from the offset on
4296 *
4297 * This will find the page for the "from" offset and cow the page and zero the
4298 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004299 */
Josef Bacik2aaa6652012-08-29 14:27:18 -04004300int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
4301 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004302{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004303 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04004304 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004305 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4306 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004307 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004308 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04004309 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04004310 pgoff_t index = from >> PAGE_CACHE_SHIFT;
4311 unsigned offset = from & (PAGE_CACHE_SIZE-1);
4312 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004313 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004314 int ret = 0;
4315 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004316 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004317
Josef Bacik2aaa6652012-08-29 14:27:18 -04004318 if ((offset & (blocksize - 1)) == 0 &&
4319 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004320 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004321 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004322 if (ret)
4323 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004324
Chris Mason211c17f2008-05-15 09:13:45 -04004325again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004326 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004327 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004328 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Miao Xieac6a2b32012-12-05 10:56:13 +00004329 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004330 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004331 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004332
4333 page_start = page_offset(page);
4334 page_end = page_start + PAGE_CACHE_SIZE - 1;
4335
Chris Masona52d9a82007-08-27 16:49:44 -04004336 if (!PageUptodate(page)) {
4337 ret = btrfs_readpage(NULL, page);
4338 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004339 if (page->mapping != mapping) {
4340 unlock_page(page);
4341 page_cache_release(page);
4342 goto again;
4343 }
Chris Masona52d9a82007-08-27 16:49:44 -04004344 if (!PageUptodate(page)) {
4345 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004346 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004347 }
4348 }
Chris Mason211c17f2008-05-15 09:13:45 -04004349 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004350
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004351 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004352 set_page_extent_mapped(page);
4353
4354 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4355 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004356 unlock_extent_cached(io_tree, page_start, page_end,
4357 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004358 unlock_page(page);
4359 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004360 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004361 btrfs_put_ordered_extent(ordered);
4362 goto again;
4363 }
4364
Josef Bacik2ac55d42010-02-03 19:33:23 +00004365 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004366 EXTENT_DIRTY | EXTENT_DELALLOC |
4367 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004368 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004369
Josef Bacik2ac55d42010-02-03 19:33:23 +00004370 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
4371 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004372 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004373 unlock_extent_cached(io_tree, page_start, page_end,
4374 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004375 goto out_unlock;
4376 }
4377
Chris Masone6dcd2d2008-07-17 12:53:50 -04004378 if (offset != PAGE_CACHE_SIZE) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004379 if (!len)
4380 len = PAGE_CACHE_SIZE - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004381 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004382 if (front)
4383 memset(kaddr, 0, offset);
4384 else
4385 memset(kaddr + offset, 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004386 flush_dcache_page(page);
4387 kunmap(page);
4388 }
Chris Mason247e7432008-07-17 12:53:51 -04004389 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004390 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004391 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
4392 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004393
Chris Mason89642222008-07-24 09:41:53 -04004394out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004395 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004396 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04004397 unlock_page(page);
4398 page_cache_release(page);
4399out:
4400 return ret;
4401}
4402
Josef Bacik16e75492013-10-22 12:18:51 -04004403static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4404 u64 offset, u64 len)
4405{
4406 struct btrfs_trans_handle *trans;
4407 int ret;
4408
4409 /*
4410 * Still need to make sure the inode looks like it's been updated so
4411 * that any holes get logged if we fsync.
4412 */
4413 if (btrfs_fs_incompat(root->fs_info, NO_HOLES)) {
4414 BTRFS_I(inode)->last_trans = root->fs_info->generation;
4415 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4416 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4417 return 0;
4418 }
4419
4420 /*
4421 * 1 - for the one we're dropping
4422 * 1 - for the one we're adding
4423 * 1 - for updating the inode.
4424 */
4425 trans = btrfs_start_transaction(root, 3);
4426 if (IS_ERR(trans))
4427 return PTR_ERR(trans);
4428
4429 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4430 if (ret) {
4431 btrfs_abort_transaction(trans, root, ret);
4432 btrfs_end_transaction(trans, root);
4433 return ret;
4434 }
4435
4436 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset,
4437 0, 0, len, 0, len, 0, 0, 0);
4438 if (ret)
4439 btrfs_abort_transaction(trans, root, ret);
4440 else
4441 btrfs_update_inode(trans, root, inode);
4442 btrfs_end_transaction(trans, root);
4443 return ret;
4444}
4445
Josef Bacik695a0d02011-03-04 15:46:53 -05004446/*
4447 * This function puts in dummy file extents for the area we're creating a hole
4448 * for. So if we are truncating this file to a larger size we need to insert
4449 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4450 * the range between oldsize and size
4451 */
Josef Bacika41ad392011-01-31 15:30:16 -05004452int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004453{
Yan Zheng9036c102008-10-30 14:19:41 -04004454 struct btrfs_root *root = BTRFS_I(inode)->root;
4455 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004456 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004457 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004458 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Qu Wenruofda28322013-02-26 08:10:22 +00004459 u64 hole_start = ALIGN(oldsize, root->sectorsize);
4460 u64 block_end = ALIGN(size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004461 u64 last_byte;
4462 u64 cur_offset;
4463 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004464 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004465
Josef Bacika71754f2013-06-17 17:14:39 -04004466 /*
4467 * If our size started in the middle of a page we need to zero out the
4468 * rest of the page before we expand the i_size, otherwise we could
4469 * expose stale data.
4470 */
4471 err = btrfs_truncate_page(inode, oldsize, 0, 0);
4472 if (err)
4473 return err;
4474
Yan Zheng9036c102008-10-30 14:19:41 -04004475 if (size <= hole_start)
4476 return 0;
4477
Yan Zheng9036c102008-10-30 14:19:41 -04004478 while (1) {
4479 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004480
Josef Bacik2ac55d42010-02-03 19:33:23 +00004481 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004482 &cached_state);
Miao Xiefa7c1492013-09-26 13:15:27 +08004483 ordered = btrfs_lookup_ordered_range(inode, hole_start,
4484 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004485 if (!ordered)
4486 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004487 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4488 &cached_state, GFP_NOFS);
Miao Xiefa7c1492013-09-26 13:15:27 +08004489 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004490 btrfs_put_ordered_extent(ordered);
4491 }
4492
Yan Zheng9036c102008-10-30 14:19:41 -04004493 cur_offset = hole_start;
4494 while (1) {
4495 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4496 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004497 if (IS_ERR(em)) {
4498 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004499 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004500 break;
4501 }
Yan Zheng9036c102008-10-30 14:19:41 -04004502 last_byte = min(extent_map_end(em), block_end);
Qu Wenruofda28322013-02-26 08:10:22 +00004503 last_byte = ALIGN(last_byte , root->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004504 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004505 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004506 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004507
Josef Bacik16e75492013-10-22 12:18:51 -04004508 err = maybe_insert_hole(root, inode, cur_offset,
4509 hole_size);
4510 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004511 break;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004512 btrfs_drop_extent_cache(inode, cur_offset,
4513 cur_offset + hole_size - 1, 0);
4514 hole_em = alloc_extent_map();
4515 if (!hole_em) {
4516 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4517 &BTRFS_I(inode)->runtime_flags);
4518 goto next;
4519 }
4520 hole_em->start = cur_offset;
4521 hole_em->len = hole_size;
4522 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004523
Josef Bacik5dc562c2012-08-17 13:14:17 -04004524 hole_em->block_start = EXTENT_MAP_HOLE;
4525 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004526 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004527 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004528 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
4529 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Josef Bacik16e75492013-10-22 12:18:51 -04004530 hole_em->generation = root->fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004531
4532 while (1) {
4533 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004534 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004535 write_unlock(&em_tree->lock);
4536 if (err != -EEXIST)
4537 break;
4538 btrfs_drop_extent_cache(inode, cur_offset,
4539 cur_offset +
4540 hole_size - 1, 0);
4541 }
4542 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04004543 }
Josef Bacik16e75492013-10-22 12:18:51 -04004544next:
Yan Zheng9036c102008-10-30 14:19:41 -04004545 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004546 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004547 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004548 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004549 break;
4550 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004551 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004552 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4553 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004554 return err;
4555}
4556
Eric Sandeen3972f262013-01-12 02:57:22 +00004557static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004558{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004559 struct btrfs_root *root = BTRFS_I(inode)->root;
4560 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004561 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004562 loff_t newsize = attr->ia_size;
4563 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004564 int ret;
4565
Eric Sandeen3972f262013-01-12 02:57:22 +00004566 /*
4567 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4568 * special case where we need to update the times despite not having
4569 * these flags set. For all other operations the VFS set these flags
4570 * explicitly if it wants a timestamp update.
4571 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004572 if (newsize != oldsize) {
4573 inode_inc_iversion(inode);
4574 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
4575 inode->i_ctime = inode->i_mtime =
4576 current_fs_time(inode->i_sb);
4577 }
Eric Sandeen3972f262013-01-12 02:57:22 +00004578
Josef Bacika41ad392011-01-31 15:30:16 -05004579 if (newsize > oldsize) {
Kirill A. Shutemov7caef262013-09-12 15:13:56 -07004580 truncate_pagecache(inode, newsize);
Josef Bacika41ad392011-01-31 15:30:16 -05004581 ret = btrfs_cont_expand(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004582 if (ret)
Yan, Zheng80825102009-11-12 09:35:36 +00004583 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004584
Miao Xief4a2f4c2011-12-14 20:12:01 -05004585 trans = btrfs_start_transaction(root, 1);
4586 if (IS_ERR(trans))
4587 return PTR_ERR(trans);
4588
4589 i_size_write(inode, newsize);
4590 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
4591 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004592 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05004593 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004594
Josef Bacika41ad392011-01-31 15:30:16 -05004595 /*
4596 * We're truncating a file that used to have good data down to
4597 * zero. Make sure it gets into the ordered flush list so that
4598 * any new writes get down to disk quickly.
4599 */
4600 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004601 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4602 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004603
Josef Bacikf3fe8202013-01-07 17:03:21 -05004604 /*
4605 * 1 for the orphan item we're going to add
4606 * 1 for the orphan item deletion.
4607 */
4608 trans = btrfs_start_transaction(root, 2);
4609 if (IS_ERR(trans))
4610 return PTR_ERR(trans);
4611
4612 /*
4613 * We need to do this in case we fail at _any_ point during the
4614 * actual truncate. Once we do the truncate_setsize we could
4615 * invalidate pages which forces any outstanding ordered io to
4616 * be instantly completed which will give us extents that need
4617 * to be truncated. If we fail to get an orphan inode down we
4618 * could have left over extents that were never meant to live,
4619 * so we need to garuntee from this point on that everything
4620 * will be consistent.
4621 */
4622 ret = btrfs_orphan_add(trans, inode);
4623 btrfs_end_transaction(trans, root);
4624 if (ret)
4625 return ret;
4626
Josef Bacika41ad392011-01-31 15:30:16 -05004627 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4628 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004629
4630 /* Disable nonlocked read DIO to avoid the end less truncate */
4631 btrfs_inode_block_unlocked_dio(inode);
4632 inode_dio_wait(inode);
4633 btrfs_inode_resume_unlocked_dio(inode);
4634
Josef Bacika41ad392011-01-31 15:30:16 -05004635 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004636 if (ret && inode->i_nlink) {
4637 int err;
4638
4639 /*
4640 * failed to truncate, disk_i_size is only adjusted down
4641 * as we remove extents, so it should represent the true
4642 * size of the inode, so reset the in memory size and
4643 * delete our orphan entry.
4644 */
4645 trans = btrfs_join_transaction(root);
4646 if (IS_ERR(trans)) {
4647 btrfs_orphan_del(NULL, inode);
4648 return ret;
4649 }
4650 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
4651 err = btrfs_orphan_del(trans, inode);
4652 if (err)
4653 btrfs_abort_transaction(trans, root, err);
4654 btrfs_end_transaction(trans, root);
4655 }
Yan, Zheng80825102009-11-12 09:35:36 +00004656 }
4657
Josef Bacika41ad392011-01-31 15:30:16 -05004658 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004659}
4660
Chris Mason39279cc2007-06-12 06:35:45 -04004661static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
4662{
4663 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08004664 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004665 int err;
4666
Li Zefanb83cc962010-12-20 16:04:08 +08004667 if (btrfs_root_readonly(root))
4668 return -EROFS;
4669
Chris Mason39279cc2007-06-12 06:35:45 -04004670 err = inode_change_ok(inode, attr);
4671 if (err)
4672 return err;
4673
Chris Mason5a3f23d2009-03-31 13:27:11 -04004674 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00004675 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00004676 if (err)
4677 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004678 }
Yan Zheng9036c102008-10-30 14:19:41 -04004679
Christoph Hellwig10257742010-06-04 11:30:02 +02004680 if (attr->ia_valid) {
4681 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004682 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004683 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04004684
Josef Bacik22c44fe2011-11-30 10:45:38 -05004685 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08004686 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02004687 }
4688
Chris Mason39279cc2007-06-12 06:35:45 -04004689 return err;
4690}
Chris Mason61295eb2008-01-14 16:24:38 -05004691
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004692/*
4693 * While truncating the inode pages during eviction, we get the VFS calling
4694 * btrfs_invalidatepage() against each page of the inode. This is slow because
4695 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
4696 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
4697 * extent_state structures over and over, wasting lots of time.
4698 *
4699 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
4700 * those expensive operations on a per page basis and do only the ordered io
4701 * finishing, while we release here the extent_map and extent_state structures,
4702 * without the excessive merging and splitting.
4703 */
4704static void evict_inode_truncate_pages(struct inode *inode)
4705{
4706 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4707 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
4708 struct rb_node *node;
4709
4710 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07004711 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004712
4713 write_lock(&map_tree->lock);
4714 while (!RB_EMPTY_ROOT(&map_tree->map)) {
4715 struct extent_map *em;
4716
4717 node = rb_first(&map_tree->map);
4718 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08004719 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
4720 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004721 remove_extent_mapping(map_tree, em);
4722 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01004723 if (need_resched()) {
4724 write_unlock(&map_tree->lock);
4725 cond_resched();
4726 write_lock(&map_tree->lock);
4727 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004728 }
4729 write_unlock(&map_tree->lock);
4730
4731 spin_lock(&io_tree->lock);
4732 while (!RB_EMPTY_ROOT(&io_tree->state)) {
4733 struct extent_state *state;
4734 struct extent_state *cached_state = NULL;
4735
4736 node = rb_first(&io_tree->state);
4737 state = rb_entry(node, struct extent_state, rb_node);
4738 atomic_inc(&state->refs);
4739 spin_unlock(&io_tree->lock);
4740
4741 lock_extent_bits(io_tree, state->start, state->end,
4742 0, &cached_state);
4743 clear_extent_bit(io_tree, state->start, state->end,
4744 EXTENT_LOCKED | EXTENT_DIRTY |
4745 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
4746 EXTENT_DEFRAG, 1, 1,
4747 &cached_state, GFP_NOFS);
4748 free_extent_state(state);
4749
Filipe Manana7064dd52014-08-08 02:47:05 +01004750 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004751 spin_lock(&io_tree->lock);
4752 }
4753 spin_unlock(&io_tree->lock);
4754}
4755
Al Virobd555972010-06-07 11:35:40 -04004756void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004757{
4758 struct btrfs_trans_handle *trans;
4759 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04004760 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik07127182011-08-19 10:29:59 -04004761 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04004762 int ret;
4763
liubo1abe9b82011-03-24 11:18:59 +00004764 trace_btrfs_inode_evict(inode);
4765
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004766 evict_inode_truncate_pages(inode);
4767
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02004768 if (inode->i_nlink &&
4769 ((btrfs_root_refs(&root->root_item) != 0 &&
4770 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
4771 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04004772 goto no_delete;
4773
Chris Mason39279cc2007-06-12 06:35:45 -04004774 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04004775 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004776 goto no_delete;
4777 }
Al Virobd555972010-06-07 11:35:40 -04004778 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04004779 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04004780
Yan, Zhengc71bf092009-11-12 09:34:40 +00004781 if (root->fs_info->log_root_recovering) {
Liu Bo6bf02312012-06-25 21:59:09 -06004782 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04004783 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00004784 goto no_delete;
4785 }
4786
Yan, Zheng76dda932009-09-21 16:00:26 -04004787 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02004788 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
4789 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04004790 goto no_delete;
4791 }
4792
Miao Xie0e8c36a2012-12-19 06:59:51 +00004793 ret = btrfs_commit_inode_delayed_inode(inode);
4794 if (ret) {
4795 btrfs_orphan_del(NULL, inode);
4796 goto no_delete;
4797 }
4798
Miao Xie66d8f3d2012-09-06 04:02:28 -06004799 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04004800 if (!rsv) {
4801 btrfs_orphan_del(NULL, inode);
4802 goto no_delete;
4803 }
Josef Bacik4a338542011-08-29 11:01:31 -04004804 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04004805 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04004806 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04004807
Chris Masondbe674a2008-07-17 12:54:05 -04004808 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04004809
Josef Bacik4289a662011-08-05 13:22:24 -04004810 /*
Miao Xie8407aa42012-09-07 01:43:32 -06004811 * This is a bit simpler than btrfs_truncate since we've already
4812 * reserved our space for our orphan item in the unlink, so we just
4813 * need to reserve some slack space in case we add bytes and update
4814 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04004815 */
Yan, Zheng80825102009-11-12 09:35:36 +00004816 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00004817 ret = btrfs_block_rsv_refill(root, rsv, min_size,
4818 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00004819
Josef Bacik726c35f2011-09-26 15:46:06 -04004820 /*
4821 * Try and steal from the global reserve since we will
4822 * likely not use this space anyway, we want to try as
4823 * hard as possible to get this to work.
4824 */
4825 if (ret)
4826 ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
4827
Yan, Zhengd68fc572010-05-16 10:49:58 -04004828 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004829 btrfs_warn(root->fs_info,
4830 "Could not get space for a delete, will truncate on mount %d",
4831 ret);
Josef Bacik4289a662011-08-05 13:22:24 -04004832 btrfs_orphan_del(NULL, inode);
4833 btrfs_free_block_rsv(root, rsv);
4834 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04004835 }
4836
Miao Xie0e8c36a2012-12-19 06:59:51 +00004837 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04004838 if (IS_ERR(trans)) {
4839 btrfs_orphan_del(NULL, inode);
4840 btrfs_free_block_rsv(root, rsv);
4841 goto no_delete;
4842 }
4843
4844 trans->block_rsv = rsv;
4845
Yan, Zhengd68fc572010-05-16 10:49:58 -04004846 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04004847 if (ret != -ENOSPC)
Yan, Zheng80825102009-11-12 09:35:36 +00004848 break;
4849
Miao Xie8407aa42012-09-07 01:43:32 -06004850 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004851 btrfs_end_transaction(trans, root);
4852 trans = NULL;
Liu Bob53d3f52012-11-14 14:34:34 +00004853 btrfs_btree_balance_dirty(root);
Josef Bacik7b128762008-07-24 12:17:14 -04004854 }
4855
Josef Bacik4289a662011-08-05 13:22:24 -04004856 btrfs_free_block_rsv(root, rsv);
4857
Josef Bacik4ef31a42013-08-13 14:10:08 -04004858 /*
4859 * Errors here aren't a big deal, it just means we leave orphan items
4860 * in the tree. They will be cleaned up on the next mount.
4861 */
Yan, Zheng80825102009-11-12 09:35:36 +00004862 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04004863 trans->block_rsv = root->orphan_block_rsv;
Josef Bacik4ef31a42013-08-13 14:10:08 -04004864 btrfs_orphan_del(trans, inode);
4865 } else {
4866 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004867 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004868
Josef Bacik4289a662011-08-05 13:22:24 -04004869 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08004870 if (!(root == root->fs_info->tree_root ||
4871 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08004872 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08004873
Chris Mason54aa1f42007-06-22 14:16:25 -04004874 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004875 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04004876no_delete:
Miao Xie89042e52013-05-15 07:48:15 +00004877 btrfs_remove_delayed_node(inode);
Jan Karadbd57682012-05-03 14:48:02 +02004878 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004879 return;
Chris Mason39279cc2007-06-12 06:35:45 -04004880}
4881
4882/*
4883 * this returns the key found in the dir entry in the location pointer.
4884 * If no dir entries were found, location->objectid is 0.
4885 */
4886static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
4887 struct btrfs_key *location)
4888{
4889 const char *name = dentry->d_name.name;
4890 int namelen = dentry->d_name.len;
4891 struct btrfs_dir_item *di;
4892 struct btrfs_path *path;
4893 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04004894 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004895
4896 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07004897 if (!path)
4898 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05004899
Li Zefan33345d012011-04-20 10:31:50 +08004900 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04004901 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04004902 if (IS_ERR(di))
4903 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05004904
David Sterbac7040052011-04-19 18:00:01 +02004905 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05004906 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05004907
Chris Mason5f39d392007-10-15 16:14:19 -04004908 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04004909out:
Chris Mason39279cc2007-06-12 06:35:45 -04004910 btrfs_free_path(path);
4911 return ret;
Chris Mason39544012007-12-12 14:38:19 -05004912out_err:
4913 location->objectid = 0;
4914 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04004915}
4916
4917/*
4918 * when we hit a tree root in a directory, the btrfs part of the inode
4919 * needs to be changed to reflect the root directory of the tree root. This
4920 * is kind of like crossing a mount point.
4921 */
4922static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004923 struct inode *dir,
4924 struct dentry *dentry,
4925 struct btrfs_key *location,
4926 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04004927{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004928 struct btrfs_path *path;
4929 struct btrfs_root *new_root;
4930 struct btrfs_root_ref *ref;
4931 struct extent_buffer *leaf;
4932 int ret;
4933 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004934
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004935 path = btrfs_alloc_path();
4936 if (!path) {
4937 err = -ENOMEM;
4938 goto out;
4939 }
Chris Mason39279cc2007-06-12 06:35:45 -04004940
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004941 err = -ENOENT;
Kelley Nielsen75ac2dd2013-11-04 19:35:58 -08004942 ret = btrfs_find_item(root->fs_info->tree_root, path,
4943 BTRFS_I(dir)->root->root_key.objectid,
4944 location->objectid, BTRFS_ROOT_REF_KEY, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004945 if (ret) {
4946 if (ret < 0)
4947 err = ret;
4948 goto out;
4949 }
Chris Mason39279cc2007-06-12 06:35:45 -04004950
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004951 leaf = path->nodes[0];
4952 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08004953 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004954 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
4955 goto out;
4956
4957 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
4958 (unsigned long)(ref + 1),
4959 dentry->d_name.len);
4960 if (ret)
4961 goto out;
4962
David Sterbab3b4aa72011-04-21 01:20:15 +02004963 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004964
4965 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
4966 if (IS_ERR(new_root)) {
4967 err = PTR_ERR(new_root);
4968 goto out;
4969 }
4970
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004971 *sub_root = new_root;
4972 location->objectid = btrfs_root_dirid(&new_root->root_item);
4973 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04004974 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004975 err = 0;
4976out:
4977 btrfs_free_path(path);
4978 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004979}
4980
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004981static void inode_tree_add(struct inode *inode)
4982{
4983 struct btrfs_root *root = BTRFS_I(inode)->root;
4984 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004985 struct rb_node **p;
4986 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01004987 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Li Zefan33345d012011-04-20 10:31:50 +08004988 u64 ino = btrfs_ino(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004989
Al Viro1d3382cb2010-10-23 15:19:20 -04004990 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004991 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00004992 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004993 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00004994 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004995 while (*p) {
4996 parent = *p;
4997 entry = rb_entry(parent, struct btrfs_inode, rb_node);
4998
Li Zefan33345d012011-04-20 10:31:50 +08004999 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005000 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08005001 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005002 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005003 else {
5004 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005005 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005006 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005007 RB_CLEAR_NODE(parent);
5008 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005009 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005010 }
5011 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005012 rb_link_node(new, parent, p);
5013 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005014 spin_unlock(&root->inode_lock);
5015}
5016
5017static void inode_tree_del(struct inode *inode)
5018{
5019 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005020 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005021
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005022 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005023 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005024 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005025 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005026 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005027 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005028 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005029
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005030 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005031 synchronize_srcu(&root->fs_info->subvol_srcu);
5032 spin_lock(&root->inode_lock);
5033 empty = RB_EMPTY_ROOT(&root->inode_tree);
5034 spin_unlock(&root->inode_lock);
5035 if (empty)
5036 btrfs_add_dead_root(root);
5037 }
5038}
5039
Jeff Mahoney143bede2012-03-01 14:56:26 +01005040void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005041{
5042 struct rb_node *node;
5043 struct rb_node *prev;
5044 struct btrfs_inode *entry;
5045 struct inode *inode;
5046 u64 objectid = 0;
5047
Liu Bo7813b3d2014-02-10 17:37:25 +08005048 if (!test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
5049 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04005050
5051 spin_lock(&root->inode_lock);
5052again:
5053 node = root->inode_tree.rb_node;
5054 prev = NULL;
5055 while (node) {
5056 prev = node;
5057 entry = rb_entry(node, struct btrfs_inode, rb_node);
5058
Li Zefan33345d012011-04-20 10:31:50 +08005059 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005060 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08005061 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005062 node = node->rb_right;
5063 else
5064 break;
5065 }
5066 if (!node) {
5067 while (prev) {
5068 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08005069 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005070 node = prev;
5071 break;
5072 }
5073 prev = rb_next(prev);
5074 }
5075 }
5076 while (node) {
5077 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08005078 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04005079 inode = igrab(&entry->vfs_inode);
5080 if (inode) {
5081 spin_unlock(&root->inode_lock);
5082 if (atomic_read(&inode->i_count) > 1)
5083 d_prune_aliases(inode);
5084 /*
Al Viro45321ac2010-06-07 13:43:19 -04005085 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04005086 * the inode cache when its usage count
5087 * hits zero.
5088 */
5089 iput(inode);
5090 cond_resched();
5091 spin_lock(&root->inode_lock);
5092 goto again;
5093 }
5094
5095 if (cond_resched_lock(&root->inode_lock))
5096 goto again;
5097
5098 node = rb_next(node);
5099 }
5100 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005101}
5102
Chris Masone02119d2008-09-05 16:13:11 -04005103static int btrfs_init_locked_inode(struct inode *inode, void *p)
5104{
5105 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005106 inode->i_ino = args->location->objectid;
5107 memcpy(&BTRFS_I(inode)->location, args->location,
5108 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005109 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005110 return 0;
5111}
5112
5113static int btrfs_find_actor(struct inode *inode, void *opaque)
5114{
5115 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005116 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005117 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005118}
5119
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005120static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005121 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005122 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005123{
5124 struct inode *inode;
5125 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005126 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005127
Chris Mason90d3e592014-01-09 17:28:00 -08005128 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005129 args.root = root;
5130
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005131 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005132 btrfs_init_locked_inode,
5133 (void *)&args);
5134 return inode;
5135}
5136
Balaji Rao1a54ef82008-07-21 02:01:04 +05305137/* Get an inode object given its location and corresponding root.
5138 * Returns in *is_new if the inode was read from disk
5139 */
5140struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005141 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305142{
5143 struct inode *inode;
5144
Chris Mason90d3e592014-01-09 17:28:00 -08005145 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305146 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005147 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305148
5149 if (inode->i_state & I_NEW) {
Balaji Rao1a54ef82008-07-21 02:01:04 +05305150 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005151 if (!is_bad_inode(inode)) {
5152 inode_tree_add(inode);
5153 unlock_new_inode(inode);
5154 if (new)
5155 *new = 1;
5156 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005157 unlock_new_inode(inode);
5158 iput(inode);
5159 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005160 }
5161 }
5162
Balaji Rao1a54ef82008-07-21 02:01:04 +05305163 return inode;
5164}
5165
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005166static struct inode *new_simple_dir(struct super_block *s,
5167 struct btrfs_key *key,
5168 struct btrfs_root *root)
5169{
5170 struct inode *inode = new_inode(s);
5171
5172 if (!inode)
5173 return ERR_PTR(-ENOMEM);
5174
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005175 BTRFS_I(inode)->root = root;
5176 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005177 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005178
5179 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005180 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005181 inode->i_fop = &simple_dir_operations;
5182 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
5183 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
5184
5185 return inode;
5186}
5187
Chris Mason3de45862008-11-17 21:02:50 -05005188struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005189{
Chris Masond3977122009-01-05 21:25:51 -05005190 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005191 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005192 struct btrfs_root *sub_root = root;
5193 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005194 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005195 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005196
5197 if (dentry->d_name.len > BTRFS_NAME_LEN)
5198 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005199
Jeff Layton39e3c952012-11-28 11:30:53 -05005200 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005201 if (ret < 0)
5202 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005203
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005204 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005205 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005206
5207 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005208 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005209 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005210 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005211
5212 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5213
Yan, Zheng76dda932009-09-21 16:00:26 -04005214 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005215 ret = fixup_tree_root_location(root, dir, dentry,
5216 &location, &sub_root);
5217 if (ret < 0) {
5218 if (ret != -ENOENT)
5219 inode = ERR_PTR(ret);
5220 else
5221 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5222 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005223 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005224 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005225 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
5226
Julia Lawall34d19ba2011-01-24 19:55:19 +00005227 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00005228 down_read(&root->fs_info->cleanup_work_sem);
5229 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005230 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005231 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005232 if (ret) {
5233 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005234 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005235 }
Filipe Manana9c3b3062014-07-31 14:41:07 +01005236 /*
5237 * If orphan cleanup did remove any orphans, it means the tree
5238 * was modified and therefore the commit root is not the same as
5239 * the current root anymore. This is a problem, because send
5240 * uses the commit root and therefore can see inode items that
5241 * don't exist in the current root anymore, and for example make
5242 * calls to btrfs_iget, which will do tree lookups based on the
5243 * current root and not on the commit root. Those lookups will
5244 * fail, returning a -ESTALE error, and making send fail with
5245 * that error. So make sure a send does not see any orphans we
5246 * have just removed, and that it will see the same inodes
5247 * regardless of whether a transaction commit happened before
5248 * it started (meaning that the commit root will be the same as
5249 * the current root) or not.
5250 */
5251 if (sub_root->node != sub_root->commit_root) {
5252 u64 sub_flags = btrfs_root_flags(&sub_root->root_item);
5253
5254 if (sub_flags & BTRFS_ROOT_SUBVOL_RDONLY) {
5255 struct extent_buffer *eb;
5256
5257 /*
5258 * Assert we can't have races between dentry
5259 * lookup called through the snapshot creation
5260 * ioctl and the VFS.
5261 */
5262 ASSERT(mutex_is_locked(&dir->i_mutex));
5263
5264 down_write(&root->fs_info->commit_root_sem);
5265 eb = sub_root->commit_root;
5266 sub_root->commit_root =
5267 btrfs_root_node(sub_root);
5268 up_write(&root->fs_info->commit_root_sem);
5269 free_extent_buffer(eb);
5270 }
5271 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005272 }
5273
Chris Mason3de45862008-11-17 21:02:50 -05005274 return inode;
5275}
5276
Nick Pigginfe15ce42011-01-07 17:49:23 +11005277static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005278{
5279 struct btrfs_root *root;
Li Zefan848cce02012-02-21 17:04:28 +08005280 struct inode *inode = dentry->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005281
Li Zefan848cce02012-02-21 17:04:28 +08005282 if (!inode && !IS_ROOT(dentry))
5283 inode = dentry->d_parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005284
Li Zefan848cce02012-02-21 17:04:28 +08005285 if (inode) {
5286 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005287 if (btrfs_root_refs(&root->root_item) == 0)
5288 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005289
5290 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5291 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005292 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005293 return 0;
5294}
5295
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005296static void btrfs_dentry_release(struct dentry *dentry)
5297{
Daeseok Youn944a4512014-04-14 15:37:02 +09005298 kfree(dentry->d_fsdata);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005299}
5300
Chris Mason3de45862008-11-17 21:02:50 -05005301static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005302 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005303{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005304 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005305
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005306 inode = btrfs_lookup_dentry(dir, dentry);
5307 if (IS_ERR(inode)) {
5308 if (PTR_ERR(inode) == -ENOENT)
5309 inode = NULL;
5310 else
5311 return ERR_CAST(inode);
5312 }
5313
Josef Bacik3a0dfa62014-02-14 13:43:48 -05005314 return d_materialise_unique(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005315}
5316
Miao Xie16cdcec2011-04-22 18:12:22 +08005317unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005318 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5319};
5320
Al Viro9cdda8d2013-05-22 16:48:09 -04005321static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005322{
Al Viro9cdda8d2013-05-22 16:48:09 -04005323 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005324 struct btrfs_root *root = BTRFS_I(inode)->root;
5325 struct btrfs_item *item;
5326 struct btrfs_dir_item *di;
5327 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005328 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005329 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005330 struct list_head ins_list;
5331 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005332 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005333 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005334 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005335 unsigned char d_type;
5336 int over = 0;
5337 u32 di_cur;
5338 u32 di_total;
5339 u32 di_len;
5340 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005341 char tmp_name[32];
5342 char *name_ptr;
5343 int name_len;
Al Viro9cdda8d2013-05-22 16:48:09 -04005344 int is_curr = 0; /* ctx->pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04005345
5346 /* FIXME, use a real flag for deciding about the key type */
5347 if (root->fs_info->tree_root == root)
5348 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005349
Al Viro9cdda8d2013-05-22 16:48:09 -04005350 if (!dir_emit_dots(file, ctx))
5351 return 0;
5352
David Woodhouse49593bf2008-08-17 17:08:36 +01005353 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005354 if (!path)
5355 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005356
Josef Bacik026fd312011-05-13 10:32:11 -04005357 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005358
Miao Xie16cdcec2011-04-22 18:12:22 +08005359 if (key_type == BTRFS_DIR_INDEX_KEY) {
5360 INIT_LIST_HEAD(&ins_list);
5361 INIT_LIST_HEAD(&del_list);
5362 btrfs_get_delayed_items(inode, &ins_list, &del_list);
5363 }
5364
David Sterba962a2982014-06-04 18:41:45 +02005365 key.type = key_type;
Al Viro9cdda8d2013-05-22 16:48:09 -04005366 key.offset = ctx->pos;
Li Zefan33345d012011-04-20 10:31:50 +08005367 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005368
Chris Mason39279cc2007-06-12 06:35:45 -04005369 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5370 if (ret < 0)
5371 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005372
5373 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005374 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005375 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005376 if (slot >= btrfs_header_nritems(leaf)) {
5377 ret = btrfs_next_leaf(root, path);
5378 if (ret < 0)
5379 goto err;
5380 else if (ret > 0)
5381 break;
5382 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005383 }
Chris Mason3de45862008-11-17 21:02:50 -05005384
Ross Kirkdd3cc162013-09-16 15:58:09 +01005385 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04005386 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5387
5388 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005389 break;
David Sterba962a2982014-06-04 18:41:45 +02005390 if (found_key.type != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04005391 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005392 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005393 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08005394 if (key_type == BTRFS_DIR_INDEX_KEY &&
5395 btrfs_should_delete_dir_index(&del_list,
5396 found_key.offset))
5397 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005398
Al Viro9cdda8d2013-05-22 16:48:09 -04005399 ctx->pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08005400 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005401
Chris Mason39279cc2007-06-12 06:35:45 -04005402 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
5403 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005404 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01005405
5406 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04005407 struct btrfs_key location;
5408
Josef Bacik22a94d42011-03-16 16:47:17 -04005409 if (verify_dir_item(root, leaf, di))
5410 break;
5411
Chris Mason5f39d392007-10-15 16:14:19 -04005412 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01005413 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04005414 name_ptr = tmp_name;
5415 } else {
5416 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01005417 if (!name_ptr) {
5418 ret = -ENOMEM;
5419 goto err;
5420 }
Chris Mason5f39d392007-10-15 16:14:19 -04005421 }
5422 read_extent_buffer(leaf, name_ptr,
5423 (unsigned long)(di + 1), name_len);
5424
5425 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5426 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05005427
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005428
Chris Mason3de45862008-11-17 21:02:50 -05005429 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01005430 * skip it.
5431 *
5432 * In contrast to old kernels, we insert the snapshot's
5433 * dir item and dir index after it has been created, so
5434 * we won't find a reference to our own snapshot. We
5435 * still keep the following code for backward
5436 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05005437 */
5438 if (location.type == BTRFS_ROOT_ITEM_KEY &&
5439 location.objectid == root->root_key.objectid) {
5440 over = 0;
5441 goto skip;
5442 }
Al Viro9cdda8d2013-05-22 16:48:09 -04005443 over = !dir_emit(ctx, name_ptr, name_len,
5444 location.objectid, d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04005445
Chris Mason3de45862008-11-17 21:02:50 -05005446skip:
Chris Mason5f39d392007-10-15 16:14:19 -04005447 if (name_ptr != tmp_name)
5448 kfree(name_ptr);
5449
Chris Mason39279cc2007-06-12 06:35:45 -04005450 if (over)
5451 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05005452 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01005453 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04005454 di_cur += di_len;
5455 di = (struct btrfs_dir_item *)((char *)di + di_len);
5456 }
Li Zefanb9e03af2011-03-23 10:43:58 +08005457next:
5458 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005459 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005460
Miao Xie16cdcec2011-04-22 18:12:22 +08005461 if (key_type == BTRFS_DIR_INDEX_KEY) {
5462 if (is_curr)
Al Viro9cdda8d2013-05-22 16:48:09 -04005463 ctx->pos++;
5464 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005465 if (ret)
5466 goto nopos;
5467 }
5468
David Woodhouse49593bf2008-08-17 17:08:36 +01005469 /* Reached end of directory/root. Bump pos past the last item. */
Zach Browndb62efb2013-07-11 16:19:42 -07005470 ctx->pos++;
5471
5472 /*
5473 * Stop new entries from being returned after we return the last
5474 * entry.
5475 *
5476 * New directory entries are assigned a strictly increasing
5477 * offset. This means that new entries created during readdir
5478 * are *guaranteed* to be seen in the future by that readdir.
5479 * This has broken buggy programs which operate on names as
5480 * they're returned by readdir. Until we re-use freed offsets
5481 * we have this hack to stop new entries from being returned
5482 * under the assumption that they'll never reach this huge
5483 * offset.
5484 *
5485 * This is being careful not to overflow 32bit loff_t unless the
5486 * last entry requires it because doing so has broken 32bit apps
5487 * in the past.
5488 */
5489 if (key_type == BTRFS_DIR_INDEX_KEY) {
5490 if (ctx->pos >= INT_MAX)
5491 ctx->pos = LLONG_MAX;
5492 else
5493 ctx->pos = INT_MAX;
5494 }
Chris Mason39279cc2007-06-12 06:35:45 -04005495nopos:
5496 ret = 0;
5497err:
Miao Xie16cdcec2011-04-22 18:12:22 +08005498 if (key_type == BTRFS_DIR_INDEX_KEY)
5499 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005500 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005501 return ret;
5502}
5503
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005504int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005505{
5506 struct btrfs_root *root = BTRFS_I(inode)->root;
5507 struct btrfs_trans_handle *trans;
5508 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005509 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005510
Josef Bacik72ac3c02012-05-23 14:13:11 -04005511 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005512 return 0;
5513
Liu Bo83eea1f2012-07-10 05:28:39 -06005514 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08005515 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005516
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005517 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005518 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005519 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005520 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005521 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005522 if (IS_ERR(trans))
5523 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06005524 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005525 }
5526 return ret;
5527}
5528
5529/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005530 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005531 * inode changes. But, it is most likely to find the inode in cache.
5532 * FIXME, needs more benchmarking...there are no reasons other than performance
5533 * to keep or drop this code.
5534 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005535static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005536{
5537 struct btrfs_root *root = BTRFS_I(inode)->root;
5538 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005539 int ret;
5540
Josef Bacik72ac3c02012-05-23 14:13:11 -04005541 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005542 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005543
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005544 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005545 if (IS_ERR(trans))
5546 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005547
5548 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005549 if (ret && ret == -ENOSPC) {
5550 /* whoops, lets try again with the full transaction */
5551 btrfs_end_transaction(trans, root);
5552 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005553 if (IS_ERR(trans))
5554 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005555
Chris Mason94b60442010-05-26 11:02:00 -04005556 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005557 }
Chris Mason39279cc2007-06-12 06:35:45 -04005558 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08005559 if (BTRFS_I(inode)->delayed_node)
5560 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005561
5562 return ret;
5563}
5564
5565/*
5566 * This is a copy of file_update_time. We need this so we can return error on
5567 * ENOSPC for updating the inode in the case of file write and mmap writes.
5568 */
Josef Bacike41f9412012-03-26 09:46:47 -04005569static int btrfs_update_time(struct inode *inode, struct timespec *now,
5570 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005571{
Alexander Block2bc5565282012-06-15 09:49:33 +02005572 struct btrfs_root *root = BTRFS_I(inode)->root;
5573
5574 if (btrfs_root_readonly(root))
5575 return -EROFS;
5576
Josef Bacike41f9412012-03-26 09:46:47 -04005577 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005578 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04005579 if (flags & S_CTIME)
5580 inode->i_ctime = *now;
5581 if (flags & S_MTIME)
5582 inode->i_mtime = *now;
5583 if (flags & S_ATIME)
5584 inode->i_atime = *now;
5585 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005586}
5587
Chris Masond352ac62008-09-29 15:18:18 -04005588/*
5589 * find the highest existing sequence number in a directory
5590 * and then set the in-memory index_cnt variable to reflect
5591 * free sequence numbers
5592 */
Josef Bacikaec74772008-07-24 12:12:38 -04005593static int btrfs_set_inode_index_count(struct inode *inode)
5594{
5595 struct btrfs_root *root = BTRFS_I(inode)->root;
5596 struct btrfs_key key, found_key;
5597 struct btrfs_path *path;
5598 struct extent_buffer *leaf;
5599 int ret;
5600
Li Zefan33345d012011-04-20 10:31:50 +08005601 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02005602 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04005603 key.offset = (u64)-1;
5604
5605 path = btrfs_alloc_path();
5606 if (!path)
5607 return -ENOMEM;
5608
5609 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5610 if (ret < 0)
5611 goto out;
5612 /* FIXME: we should be able to handle this */
5613 if (ret == 0)
5614 goto out;
5615 ret = 0;
5616
5617 /*
5618 * MAGIC NUMBER EXPLANATION:
5619 * since we search a directory based on f_pos we have to start at 2
5620 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5621 * else has to start at 2
5622 */
5623 if (path->slots[0] == 0) {
5624 BTRFS_I(inode)->index_cnt = 2;
5625 goto out;
5626 }
5627
5628 path->slots[0]--;
5629
5630 leaf = path->nodes[0];
5631 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5632
Li Zefan33345d012011-04-20 10:31:50 +08005633 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02005634 found_key.type != BTRFS_DIR_INDEX_KEY) {
Josef Bacikaec74772008-07-24 12:12:38 -04005635 BTRFS_I(inode)->index_cnt = 2;
5636 goto out;
5637 }
5638
5639 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
5640out:
5641 btrfs_free_path(path);
5642 return ret;
5643}
5644
Chris Masond352ac62008-09-29 15:18:18 -04005645/*
5646 * helper to find a free sequence number in a given directory. This current
5647 * code is very simple, later versions will do smarter things in the btree
5648 */
Chris Mason3de45862008-11-17 21:02:50 -05005649int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04005650{
5651 int ret = 0;
5652
5653 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08005654 ret = btrfs_inode_delayed_dir_index_count(dir);
5655 if (ret) {
5656 ret = btrfs_set_inode_index_count(dir);
5657 if (ret)
5658 return ret;
5659 }
Josef Bacikaec74772008-07-24 12:12:38 -04005660 }
5661
Chris Mason00e4e6b2008-08-05 11:18:09 -04005662 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04005663 BTRFS_I(dir)->index_cnt++;
5664
5665 return ret;
5666}
5667
Chris Masonb0d5d102014-09-08 13:08:51 -07005668static int btrfs_insert_inode_locked(struct inode *inode)
5669{
5670 struct btrfs_iget_args args;
5671 args.location = &BTRFS_I(inode)->location;
5672 args.root = BTRFS_I(inode)->root;
5673
5674 return insert_inode_locked4(inode,
5675 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
5676 btrfs_find_actor, &args);
5677}
5678
Chris Mason39279cc2007-06-12 06:35:45 -04005679static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5680 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04005681 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05005682 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04005683 u64 ref_objectid, u64 objectid,
5684 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04005685{
5686 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005687 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04005688 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04005689 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05005690 struct btrfs_inode_ref *ref;
5691 struct btrfs_key key[2];
5692 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005693 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05005694 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04005695 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005696
Chris Mason5f39d392007-10-15 16:14:19 -04005697 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005698 if (!path)
5699 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04005700
Chris Mason39279cc2007-06-12 06:35:45 -04005701 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005702 if (!inode) {
5703 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005704 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005705 }
Chris Mason39279cc2007-06-12 06:35:45 -04005706
Li Zefan581bb052011-04-20 10:06:11 +08005707 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01005708 * O_TMPFILE, set link count to 0, so that after this point,
5709 * we fill in an inode item with the correct link count.
5710 */
5711 if (!name)
5712 set_nlink(inode, 0);
5713
5714 /*
Li Zefan581bb052011-04-20 10:06:11 +08005715 * we have to initialize this early, so we can reclaim the inode
5716 * number if we fail afterwards in this function.
5717 */
5718 inode->i_ino = objectid;
5719
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005720 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00005721 trace_btrfs_inode_request(dir);
5722
Chris Mason3de45862008-11-17 21:02:50 -05005723 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04005724 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005725 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005726 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005727 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04005728 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005729 } else if (dir) {
5730 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04005731 }
5732 /*
5733 * index_cnt is ignored for everything but a dir,
5734 * btrfs_get_inode_index_count has an explanation for the magic
5735 * number
5736 */
5737 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08005738 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04005739 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04005740 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00005741 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04005742
Josef Bacik5dc562c2012-08-17 13:14:17 -04005743 /*
5744 * We could have gotten an inode number from somebody who was fsynced
5745 * and then removed in this same transaction, so let's just set full
5746 * sync since it will be a full sync anyway and this will blow away the
5747 * old info in the log.
5748 */
5749 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
5750
Chris Mason9c583092008-01-29 15:15:18 -05005751 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02005752 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05005753 key[0].offset = 0;
5754
Chris Mason9c583092008-01-29 15:15:18 -05005755 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005756
5757 if (name) {
5758 /*
5759 * Start new inodes with an inode_ref. This is slightly more
5760 * efficient for small numbers of hard links since they will
5761 * be packed into one item. Extended refs will kick in if we
5762 * add more hard links than can fit in the ref item.
5763 */
5764 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02005765 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005766 key[1].offset = ref_objectid;
5767
5768 sizes[1] = name_len + sizeof(*ref);
5769 }
Chris Mason9c583092008-01-29 15:15:18 -05005770
Chris Masonb0d5d102014-09-08 13:08:51 -07005771 location = &BTRFS_I(inode)->location;
5772 location->objectid = objectid;
5773 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02005774 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07005775
5776 ret = btrfs_insert_inode_locked(inode);
5777 if (ret < 0)
5778 goto fail;
5779
Chris Masonb9473432009-03-13 11:00:37 -04005780 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005781 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05005782 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07005783 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04005784
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03005785 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04005786 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005787 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04005788 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
5789 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06005790 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
5791 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04005792 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05005793
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005794 if (name) {
5795 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
5796 struct btrfs_inode_ref);
5797 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
5798 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
5799 ptr = (unsigned long)(ref + 1);
5800 write_extent_buffer(path->nodes[0], name, ptr, name_len);
5801 }
Chris Mason9c583092008-01-29 15:15:18 -05005802
Chris Mason5f39d392007-10-15 16:14:19 -04005803 btrfs_mark_buffer_dirty(path->nodes[0]);
5804 btrfs_free_path(path);
5805
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005806 btrfs_inherit_iflags(inode, dir);
5807
Al Viro569254b2011-07-24 17:08:40 -04005808 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04005809 if (btrfs_test_opt(root, NODATASUM))
5810 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo213490b2012-09-11 08:33:50 -06005811 if (btrfs_test_opt(root, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05005812 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
5813 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04005814 }
5815
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005816 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00005817
5818 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04005819 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00005820
Alexander Block8ea05e32012-07-25 17:35:53 +02005821 btrfs_update_root_times(trans, root);
5822
Filipe David Borba Manana63541922014-01-07 11:47:46 +00005823 ret = btrfs_inode_inherit_props(trans, inode, dir);
5824 if (ret)
5825 btrfs_err(root->fs_info,
5826 "error inheriting props for ino %llu (root %llu): %d",
5827 btrfs_ino(inode), root->root_key.objectid, ret);
5828
Chris Mason39279cc2007-06-12 06:35:45 -04005829 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07005830
5831fail_unlock:
5832 unlock_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005833fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005834 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04005835 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04005836 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005837 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005838 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005839}
5840
5841static inline u8 btrfs_inode_type(struct inode *inode)
5842{
5843 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
5844}
5845
Chris Masond352ac62008-09-29 15:18:18 -04005846/*
5847 * utility function to add 'inode' into 'parent_inode' with
5848 * a give name and a given sequence number.
5849 * if 'add_backref' is true, also insert a backref from the
5850 * inode to the parent directory.
5851 */
Chris Masone02119d2008-09-05 16:13:11 -04005852int btrfs_add_link(struct btrfs_trans_handle *trans,
5853 struct inode *parent_inode, struct inode *inode,
5854 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005855{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005856 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005857 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04005858 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08005859 u64 ino = btrfs_ino(inode);
5860 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005861
Li Zefan33345d012011-04-20 10:31:50 +08005862 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005863 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
5864 } else {
Li Zefan33345d012011-04-20 10:31:50 +08005865 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02005866 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005867 key.offset = 0;
5868 }
Chris Mason39279cc2007-06-12 06:35:45 -04005869
Li Zefan33345d012011-04-20 10:31:50 +08005870 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005871 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
5872 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08005873 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005874 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08005875 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
5876 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005877 }
5878
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005879 /* Nothing to clean up yet */
5880 if (ret)
5881 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005882
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005883 ret = btrfs_insert_dir_item(trans, root, name, name_len,
5884 parent_inode, &key,
5885 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05005886 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005887 goto fail_dir_item;
5888 else if (ret) {
5889 btrfs_abort_transaction(trans, root, ret);
5890 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005891 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005892
5893 btrfs_i_size_write(parent_inode, parent_inode->i_size +
5894 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005895 inode_inc_iversion(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005896 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
5897 ret = btrfs_update_inode(trans, root, parent_inode);
5898 if (ret)
5899 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005900 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05005901
5902fail_dir_item:
5903 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
5904 u64 local_index;
5905 int err;
5906 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
5907 key.objectid, root->root_key.objectid,
5908 parent_ino, &local_index, name, name_len);
5909
5910 } else if (add_backref) {
5911 u64 local_index;
5912 int err;
5913
5914 err = btrfs_del_inode_ref(trans, root, name, name_len,
5915 ino, parent_ino, &local_index);
5916 }
5917 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005918}
5919
5920static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00005921 struct inode *dir, struct dentry *dentry,
5922 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005923{
Josef Bacika1b075d2010-11-19 20:36:11 +00005924 int err = btrfs_add_link(trans, dir, inode,
5925 dentry->d_name.name, dentry->d_name.len,
5926 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005927 if (err > 0)
5928 err = -EEXIST;
5929 return err;
5930}
5931
Josef Bacik618e21d2007-07-11 10:18:17 -04005932static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04005933 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04005934{
5935 struct btrfs_trans_handle *trans;
5936 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005937 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04005938 int err;
5939 int drop_inode = 0;
5940 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005941 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04005942
5943 if (!new_valid_dev(rdev))
5944 return -EINVAL;
5945
Josef Bacik9ed74f22009-09-11 16:12:44 -04005946 /*
5947 * 2 for inode item and ref
5948 * 2 for dir items
5949 * 1 for xattr if selinux is on
5950 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005951 trans = btrfs_start_transaction(root, 5);
5952 if (IS_ERR(trans))
5953 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05005954
Li Zefan581bb052011-04-20 10:06:11 +08005955 err = btrfs_find_free_ino(root, &objectid);
5956 if (err)
5957 goto out_unlock;
5958
Josef Bacikaec74772008-07-24 12:12:38 -04005959 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005960 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005961 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005962 if (IS_ERR(inode)) {
5963 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005964 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005965 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005966
Casey Schauflerad19db72011-12-15 10:09:07 -05005967 /*
5968 * If the active LSM wants to access the inode during
5969 * d_instantiate it needs these. Smack checks to see
5970 * if the filesystem supports xattrs by looking at the
5971 * ops vector.
5972 */
Casey Schauflerad19db72011-12-15 10:09:07 -05005973 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07005974 init_special_inode(inode, inode->i_mode, rdev);
5975
5976 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04005977 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07005978 goto out_unlock_inode;
5979
5980 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
5981 if (err) {
5982 goto out_unlock_inode;
5983 } else {
Yan1b4ab1b2007-08-29 09:11:44 -04005984 btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07005985 unlock_new_inode(inode);
Al Viro08c422c2011-12-23 07:58:13 -05005986 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005987 }
Chris Masonb0d5d102014-09-08 13:08:51 -07005988
Josef Bacik618e21d2007-07-11 10:18:17 -04005989out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005990 btrfs_end_transaction(trans, root);
Josef Bacikc581afc2014-02-06 16:06:06 -05005991 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00005992 btrfs_btree_balance_dirty(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04005993 if (drop_inode) {
5994 inode_dec_link_count(inode);
5995 iput(inode);
5996 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005997 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07005998
5999out_unlock_inode:
6000 drop_inode = 1;
6001 unlock_new_inode(inode);
6002 goto out_unlock;
6003
Josef Bacik618e21d2007-07-11 10:18:17 -04006004}
6005
Chris Mason39279cc2007-06-12 06:35:45 -04006006static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006007 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006008{
6009 struct btrfs_trans_handle *trans;
6010 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006011 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006012 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006013 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006014 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006015 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006016
Josef Bacik9ed74f22009-09-11 16:12:44 -04006017 /*
6018 * 2 for inode item and ref
6019 * 2 for dir items
6020 * 1 for xattr if selinux is on
6021 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006022 trans = btrfs_start_transaction(root, 5);
6023 if (IS_ERR(trans))
6024 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006025
Li Zefan581bb052011-04-20 10:06:11 +08006026 err = btrfs_find_free_ino(root, &objectid);
6027 if (err)
6028 goto out_unlock;
6029
Josef Bacikaec74772008-07-24 12:12:38 -04006030 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006031 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04006032 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006033 if (IS_ERR(inode)) {
6034 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006035 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006036 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006037 drop_inode_on_err = 1;
Casey Schauflerad19db72011-12-15 10:09:07 -05006038 /*
6039 * If the active LSM wants to access the inode during
6040 * d_instantiate it needs these. Smack checks to see
6041 * if the filesystem supports xattrs by looking at the
6042 * ops vector.
6043 */
6044 inode->i_fop = &btrfs_file_operations;
6045 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006046 inode->i_mapping->a_ops = &btrfs_aops;
6047 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
6048
6049 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6050 if (err)
6051 goto out_unlock_inode;
6052
6053 err = btrfs_update_inode(trans, root, inode);
6054 if (err)
6055 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05006056
Josef Bacika1b075d2010-11-19 20:36:11 +00006057 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006058 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006059 goto out_unlock_inode;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006060
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006061 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006062 unlock_new_inode(inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006063 d_instantiate(dentry, inode);
6064
Chris Mason39279cc2007-06-12 06:35:45 -04006065out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006066 btrfs_end_transaction(trans, root);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006067 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006068 inode_dec_link_count(inode);
6069 iput(inode);
6070 }
Josef Bacikc581afc2014-02-06 16:06:06 -05006071 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006072 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006073 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006074
6075out_unlock_inode:
6076 unlock_new_inode(inode);
6077 goto out_unlock;
6078
Chris Mason39279cc2007-06-12 06:35:45 -04006079}
6080
6081static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6082 struct dentry *dentry)
6083{
6084 struct btrfs_trans_handle *trans;
6085 struct btrfs_root *root = BTRFS_I(dir)->root;
6086 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006087 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006088 int err;
6089 int drop_inode = 0;
6090
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006091 /* do not allow sys_link's with other subvols of the same device */
6092 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006093 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006094
Mark Fashehf1863732012-08-08 11:32:27 -07006095 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006096 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006097
Chris Mason3de45862008-11-17 21:02:50 -05006098 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006099 if (err)
6100 goto fail;
6101
Yan, Zhenga22285a2010-05-16 10:48:46 -04006102 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006103 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006104 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006105 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04006106 */
Miao Xie7e6b6462011-02-18 09:21:17 +00006107 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006108 if (IS_ERR(trans)) {
6109 err = PTR_ERR(trans);
6110 goto fail;
6111 }
Chris Mason5f39d392007-10-15 16:14:19 -04006112
Miao Xie67de1172013-12-26 13:07:06 +08006113 /* There are several dir indexes for this inode, clear the cache. */
6114 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006115 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006116 inode_inc_iversion(inode);
Miao Xie31534952011-04-13 13:19:21 +08006117 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6ee2010-10-23 11:11:40 -04006118 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006119 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006120
Josef Bacika1b075d2010-11-19 20:36:11 +00006121 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006122
Yan, Zhenga5719522009-09-24 09:17:31 -04006123 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006124 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006125 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006126 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006127 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006128 if (err)
6129 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006130 if (inode->i_nlink == 1) {
6131 /*
6132 * If new hard link count is 1, it's a file created
6133 * with open(2) O_TMPFILE flag.
6134 */
6135 err = btrfs_orphan_del(trans, inode);
6136 if (err)
6137 goto fail;
6138 }
Al Viro08c422c2011-12-23 07:58:13 -05006139 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00006140 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006141 }
Chris Mason39279cc2007-06-12 06:35:45 -04006142
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006143 btrfs_end_transaction(trans, root);
Josef Bacikc581afc2014-02-06 16:06:06 -05006144 btrfs_balance_delayed_items(root);
Chris Mason1832a6d2007-12-21 16:27:21 -05006145fail:
Chris Mason39279cc2007-06-12 06:35:45 -04006146 if (drop_inode) {
6147 inode_dec_link_count(inode);
6148 iput(inode);
6149 }
Liu Bob53d3f52012-11-14 14:34:34 +00006150 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006151 return err;
6152}
6153
Al Viro18bb1db2011-07-26 01:41:39 -04006154static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006155{
Chris Masonb9d86662008-05-02 16:13:49 -04006156 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006157 struct btrfs_trans_handle *trans;
6158 struct btrfs_root *root = BTRFS_I(dir)->root;
6159 int err = 0;
6160 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006161 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006162 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006163
Josef Bacik9ed74f22009-09-11 16:12:44 -04006164 /*
6165 * 2 items for inode and ref
6166 * 2 items for dir items
6167 * 1 for xattr if selinux is on
6168 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006169 trans = btrfs_start_transaction(root, 5);
6170 if (IS_ERR(trans))
6171 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006172
Li Zefan581bb052011-04-20 10:06:11 +08006173 err = btrfs_find_free_ino(root, &objectid);
6174 if (err)
6175 goto out_fail;
6176
Josef Bacikaec74772008-07-24 12:12:38 -04006177 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006178 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04006179 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006180 if (IS_ERR(inode)) {
6181 err = PTR_ERR(inode);
6182 goto out_fail;
6183 }
Chris Mason5f39d392007-10-15 16:14:19 -04006184
Chris Mason39279cc2007-06-12 06:35:45 -04006185 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006186 /* these must be set before we unlock the inode */
6187 inode->i_op = &btrfs_dir_inode_operations;
6188 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006189
Eric Paris2a7dba32011-02-01 11:05:39 -05006190 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006191 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006192 goto out_fail_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006193
Chris Masondbe674a2008-07-17 12:54:05 -04006194 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006195 err = btrfs_update_inode(trans, root, inode);
6196 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006197 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006198
Josef Bacika1b075d2010-11-19 20:36:11 +00006199 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
6200 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006201 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006202 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006203
Chris Mason39279cc2007-06-12 06:35:45 -04006204 d_instantiate(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006205 /*
6206 * mkdir is special. We're unlocking after we call d_instantiate
6207 * to avoid a race with nfsd calling d_instantiate.
6208 */
6209 unlock_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006210 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006211
6212out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006213 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04006214 if (drop_on_err)
6215 iput(inode);
Josef Bacikc581afc2014-02-06 16:06:06 -05006216 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006217 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006218 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006219
6220out_fail_inode:
6221 unlock_new_inode(inode);
6222 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006223}
6224
Chris Masond352ac62008-09-29 15:18:18 -04006225/* helper for btfs_get_extent. Given an existing extent in the tree,
6226 * and an extent that you want to insert, deal with overlap and insert
6227 * the new extent into the tree.
6228 */
Chris Mason3b951512008-04-17 11:29:12 -04006229static int merge_extent_mapping(struct extent_map_tree *em_tree,
6230 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006231 struct extent_map *em,
Qu Wenruo51f395a2014-08-08 13:06:20 +08006232 u64 map_start)
Chris Mason3b951512008-04-17 11:29:12 -04006233{
6234 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04006235
Chris Masone6dcd2d2008-07-17 12:53:50 -04006236 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
6237 start_diff = map_start - em->start;
6238 em->start = map_start;
Qu Wenruo51f395a2014-08-08 13:06:20 +08006239 em->len = existing->start - em->start;
Chris Masonc8b97812008-10-29 14:49:59 -04006240 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
6241 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006242 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04006243 em->block_len -= start_diff;
6244 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006245 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006246}
6247
Chris Masonc8b97812008-10-29 14:49:59 -04006248static noinline int uncompress_inline(struct btrfs_path *path,
6249 struct inode *inode, struct page *page,
6250 size_t pg_offset, u64 extent_offset,
6251 struct btrfs_file_extent_item *item)
6252{
6253 int ret;
6254 struct extent_buffer *leaf = path->nodes[0];
6255 char *tmp;
6256 size_t max_size;
6257 unsigned long inline_size;
6258 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006259 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006260
6261 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006262 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006263 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6264 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006265 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006266 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006267 if (!tmp)
6268 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006269 ptr = btrfs_file_extent_inline_start(item);
6270
6271 read_extent_buffer(leaf, tmp, ptr, inline_size);
6272
Chris Mason5b050f02008-11-11 09:34:41 -05006273 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006274 ret = btrfs_decompress(compress_type, tmp, page,
6275 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006276 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006277 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006278}
6279
Chris Masond352ac62008-09-29 15:18:18 -04006280/*
6281 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006282 * the ugly parts come from merging extents from the disk with the in-ram
6283 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006284 * where the in-ram extents might be locked pending data=ordered completion.
6285 *
6286 * This also copies inline extents directly into the page.
6287 */
Chris Masond3977122009-01-05 21:25:51 -05006288
Chris Masona52d9a82007-08-27 16:49:44 -04006289struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05006290 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04006291 int create)
6292{
6293 int ret;
6294 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006295 u64 extent_start = 0;
6296 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08006297 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006298 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006299 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04006300 struct btrfs_root *root = BTRFS_I(inode)->root;
6301 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006302 struct extent_buffer *leaf;
6303 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006304 struct extent_map *em = NULL;
6305 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05006306 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006307 struct btrfs_trans_handle *trans = NULL;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006308 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006309
Chris Masona52d9a82007-08-27 16:49:44 -04006310again:
Chris Mason890871b2009-09-02 16:24:52 -04006311 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006312 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006313 if (em)
6314 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006315 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006316
Chris Masona52d9a82007-08-27 16:49:44 -04006317 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006318 if (em->start > start || em->start + em->len <= start)
6319 free_extent_map(em);
6320 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006321 free_extent_map(em);
6322 else
6323 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006324 }
David Sterba172ddd62011-04-21 00:48:27 +02006325 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006326 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006327 err = -ENOMEM;
6328 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006329 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006330 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006331 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006332 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006333 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006334 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006335
6336 if (!path) {
6337 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006338 if (!path) {
6339 err = -ENOMEM;
6340 goto out;
6341 }
6342 /*
6343 * Chances are we'll be called again, so go ahead and do
6344 * readahead
6345 */
6346 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04006347 }
6348
Chris Mason179e29e2007-11-01 11:28:41 -04006349 ret = btrfs_lookup_file_extent(trans, root, path,
6350 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006351 if (ret < 0) {
6352 err = ret;
6353 goto out;
6354 }
6355
6356 if (ret != 0) {
6357 if (path->slots[0] == 0)
6358 goto not_found;
6359 path->slots[0]--;
6360 }
6361
Chris Mason5f39d392007-10-15 16:14:19 -04006362 leaf = path->nodes[0];
6363 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006364 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006365 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006366 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02006367 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04006368 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006369 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006370 /*
6371 * If we backup past the first extent we want to move forward
6372 * and see if there is an extent in front of us, otherwise we'll
6373 * say there is a hole for our whole search range which can
6374 * cause problems.
6375 */
6376 extent_end = start;
6377 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006378 }
6379
Chris Mason5f39d392007-10-15 16:14:19 -04006380 found_type = btrfs_file_extent_type(leaf, item);
6381 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04006382 if (found_type == BTRFS_FILE_EXTENT_REG ||
6383 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006384 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006385 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006386 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6387 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08006388 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Qu Wenruofda28322013-02-26 08:10:22 +00006389 extent_end = ALIGN(extent_start + size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04006390 }
Josef Bacik25a50342013-10-14 12:08:38 -04006391next:
Yan Zheng9036c102008-10-30 14:19:41 -04006392 if (start >= extent_end) {
6393 path->slots[0]++;
6394 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6395 ret = btrfs_next_leaf(root, path);
6396 if (ret < 0) {
6397 err = ret;
6398 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006399 }
Yan Zheng9036c102008-10-30 14:19:41 -04006400 if (ret > 0)
6401 goto not_found;
6402 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006403 }
Yan Zheng9036c102008-10-30 14:19:41 -04006404 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6405 if (found_key.objectid != objectid ||
6406 found_key.type != BTRFS_EXTENT_DATA_KEY)
6407 goto not_found;
6408 if (start + len <= found_key.offset)
6409 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006410 if (start > found_key.offset)
6411 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04006412 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006413 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006414 em->len = found_key.offset - start;
6415 goto not_found_em;
6416 }
6417
Filipe Manana7ffbb592014-06-09 03:48:05 +01006418 btrfs_extent_item_to_extent_map(inode, path, item, new_inline, em);
6419
Yan Zhengd899e052008-10-30 14:25:28 -04006420 if (found_type == BTRFS_FILE_EXTENT_REG ||
6421 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006422 goto insert;
6423 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006424 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006425 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006426 size_t size;
6427 size_t extent_offset;
6428 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006429
Filipe Manana7ffbb592014-06-09 03:48:05 +01006430 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04006431 goto out;
Yan689f9342007-10-29 11:41:07 -04006432
Chris Mason514ac8a2014-01-03 21:07:00 -08006433 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04006434 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05006435 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04006436 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006437 em->start = extent_start + extent_offset;
Qu Wenruofda28322013-02-26 08:10:22 +00006438 em->len = ALIGN(copy_size, root->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006439 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006440 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006441 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006442 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006443 if (btrfs_file_extent_compression(leaf, item) !=
6444 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006445 ret = uncompress_inline(path, inode, page,
6446 pg_offset,
6447 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04006448 if (ret) {
6449 err = ret;
6450 goto out;
6451 }
Chris Masonc8b97812008-10-29 14:49:59 -04006452 } else {
6453 map = kmap(page);
6454 read_extent_buffer(leaf, map + pg_offset, ptr,
6455 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04006456 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
6457 memset(map + pg_offset + copy_size, 0,
6458 PAGE_CACHE_SIZE - pg_offset -
6459 copy_size);
6460 }
Chris Masonc8b97812008-10-29 14:49:59 -04006461 kunmap(page);
6462 }
Chris Mason179e29e2007-11-01 11:28:41 -04006463 flush_dcache_page(page);
6464 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006465 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006466 if (!trans) {
6467 kunmap(page);
6468 free_extent_map(em);
6469 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006470
David Sterbab3b4aa72011-04-21 01:20:15 +02006471 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006472 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006473
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006474 if (IS_ERR(trans))
6475 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006476 goto again;
6477 }
Chris Masonc8b97812008-10-29 14:49:59 -04006478 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006479 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006480 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006481 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006482 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006483 }
Chris Masond1310b22008-01-24 16:13:08 -05006484 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006485 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006486 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04006487 }
6488not_found:
6489 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006490 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006491 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006492not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04006493 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04006494 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006495insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006496 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006497 if (em->start > start || extent_map_end(em) <= start) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006498 btrfs_err(root->fs_info, "bad extent! em: [%llu %llu] passed [%llu %llu]",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006499 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04006500 err = -EIO;
6501 goto out;
6502 }
Chris Masond1310b22008-01-24 16:13:08 -05006503
6504 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006505 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006506 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006507 /* it is possible that someone inserted the extent into the tree
6508 * while we had the lock dropped. It is also possible that
6509 * an overlapping map exists in the tree
6510 */
Chris Masona52d9a82007-08-27 16:49:44 -04006511 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04006512 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006513
6514 ret = 0;
6515
Chris Mason3b951512008-04-17 11:29:12 -04006516 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04006517 if (existing && (existing->start > start ||
6518 existing->start + existing->len <= start)) {
6519 free_extent_map(existing);
6520 existing = NULL;
6521 }
Chris Mason3b951512008-04-17 11:29:12 -04006522 if (!existing) {
6523 existing = lookup_extent_mapping(em_tree, em->start,
6524 em->len);
6525 if (existing) {
6526 err = merge_extent_mapping(em_tree, existing,
Qu Wenruo51f395a2014-08-08 13:06:20 +08006527 em, start);
Chris Mason3b951512008-04-17 11:29:12 -04006528 free_extent_map(existing);
6529 if (err) {
6530 free_extent_map(em);
6531 em = NULL;
6532 }
6533 } else {
6534 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04006535 free_extent_map(em);
6536 em = NULL;
6537 }
6538 } else {
6539 free_extent_map(em);
6540 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006541 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006542 }
Chris Masona52d9a82007-08-27 16:49:44 -04006543 }
Chris Mason890871b2009-09-02 16:24:52 -04006544 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006545out:
liubo1abe9b82011-03-24 11:18:59 +00006546
Steven Rostedt4cd8587c2013-11-14 22:57:29 -05006547 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00006548
Chris Masonf4219502008-07-22 11:18:09 -04006549 if (path)
6550 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04006551 if (trans) {
6552 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05006553 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04006554 err = ret;
6555 }
Chris Masona52d9a82007-08-27 16:49:44 -04006556 if (err) {
6557 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04006558 return ERR_PTR(err);
6559 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006560 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04006561 return em;
6562}
6563
Chris Masonec29ed52011-02-23 16:23:20 -05006564struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
6565 size_t pg_offset, u64 start, u64 len,
6566 int create)
6567{
6568 struct extent_map *em;
6569 struct extent_map *hole_em = NULL;
6570 u64 range_start = start;
6571 u64 end;
6572 u64 found;
6573 u64 found_end;
6574 int err = 0;
6575
6576 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
6577 if (IS_ERR(em))
6578 return em;
6579 if (em) {
6580 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00006581 * if our em maps to
6582 * - a hole or
6583 * - a pre-alloc extent,
6584 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05006585 */
Liu Bof9e4fb52013-01-07 10:10:12 +00006586 if (em->block_start != EXTENT_MAP_HOLE &&
6587 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05006588 return em;
6589 else
6590 hole_em = em;
6591 }
6592
6593 /* check to see if we've wrapped (len == -1 or similar) */
6594 end = start + len;
6595 if (end < start)
6596 end = (u64)-1;
6597 else
6598 end -= 1;
6599
6600 em = NULL;
6601
6602 /* ok, we didn't find anything, lets look for delalloc */
6603 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
6604 end, len, EXTENT_DELALLOC, 1);
6605 found_end = range_start + found;
6606 if (found_end < range_start)
6607 found_end = (u64)-1;
6608
6609 /*
6610 * we didn't find anything useful, return
6611 * the original results from get_extent()
6612 */
6613 if (range_start > end || found_end <= start) {
6614 em = hole_em;
6615 hole_em = NULL;
6616 goto out;
6617 }
6618
6619 /* adjust the range_start to make sure it doesn't
6620 * go backwards from the start they passed in
6621 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05306622 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05006623 found = found_end - range_start;
6624
6625 if (found > 0) {
6626 u64 hole_start = start;
6627 u64 hole_len = len;
6628
David Sterba172ddd62011-04-21 00:48:27 +02006629 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05006630 if (!em) {
6631 err = -ENOMEM;
6632 goto out;
6633 }
6634 /*
6635 * when btrfs_get_extent can't find anything it
6636 * returns one huge hole
6637 *
6638 * make sure what it found really fits our range, and
6639 * adjust to make sure it is based on the start from
6640 * the caller
6641 */
6642 if (hole_em) {
6643 u64 calc_end = extent_map_end(hole_em);
6644
6645 if (calc_end <= start || (hole_em->start > end)) {
6646 free_extent_map(hole_em);
6647 hole_em = NULL;
6648 } else {
6649 hole_start = max(hole_em->start, start);
6650 hole_len = calc_end - hole_start;
6651 }
6652 }
6653 em->bdev = NULL;
6654 if (hole_em && range_start > hole_start) {
6655 /* our hole starts before our delalloc, so we
6656 * have to return just the parts of the hole
6657 * that go until the delalloc starts
6658 */
6659 em->len = min(hole_len,
6660 range_start - hole_start);
6661 em->start = hole_start;
6662 em->orig_start = hole_start;
6663 /*
6664 * don't adjust block start at all,
6665 * it is fixed at EXTENT_MAP_HOLE
6666 */
6667 em->block_start = hole_em->block_start;
6668 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00006669 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
6670 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05006671 } else {
6672 em->start = range_start;
6673 em->len = found;
6674 em->orig_start = range_start;
6675 em->block_start = EXTENT_MAP_DELALLOC;
6676 em->block_len = found;
6677 }
6678 } else if (hole_em) {
6679 return hole_em;
6680 }
6681out:
6682
6683 free_extent_map(hole_em);
6684 if (err) {
6685 free_extent_map(em);
6686 return ERR_PTR(err);
6687 }
6688 return em;
6689}
6690
Josef Bacik4b46fce2010-05-23 11:00:55 -04006691static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
6692 u64 start, u64 len)
6693{
6694 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04006695 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006696 struct btrfs_key ins;
6697 u64 alloc_hint;
6698 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006699
Josef Bacik4b46fce2010-05-23 11:00:55 -04006700 alloc_hint = get_extent_allocation_hint(inode, start, len);
Josef Bacik00361582013-08-14 14:02:47 -04006701 ret = btrfs_reserve_extent(root, len, root->sectorsize, 0,
Miao Xiee570fd22014-06-19 10:42:50 +08006702 alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006703 if (ret)
6704 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006705
Josef Bacik70c8a912012-10-11 16:54:30 -04006706 em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006707 ins.offset, ins.offset, ins.offset, 0);
Josef Bacik00361582013-08-14 14:02:47 -04006708 if (IS_ERR(em)) {
Miao Xiee570fd22014-06-19 10:42:50 +08006709 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006710 return em;
6711 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006712
6713 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
6714 ins.offset, ins.offset, 0);
6715 if (ret) {
Miao Xiee570fd22014-06-19 10:42:50 +08006716 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006717 free_extent_map(em);
6718 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006719 }
Josef Bacik00361582013-08-14 14:02:47 -04006720
Josef Bacik4b46fce2010-05-23 11:00:55 -04006721 return em;
6722}
6723
Chris Mason46bfbb52010-05-26 11:04:10 -04006724/*
6725 * returns 1 when the nocow is safe, < 1 on error, 0 if the
6726 * block must be cow'd
6727 */
Josef Bacik00361582013-08-14 14:02:47 -04006728noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04006729 u64 *orig_start, u64 *orig_block_len,
6730 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04006731{
Josef Bacik00361582013-08-14 14:02:47 -04006732 struct btrfs_trans_handle *trans;
Chris Mason46bfbb52010-05-26 11:04:10 -04006733 struct btrfs_path *path;
6734 int ret;
6735 struct extent_buffer *leaf;
6736 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08006737 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04006738 struct btrfs_file_extent_item *fi;
6739 struct btrfs_key key;
6740 u64 disk_bytenr;
6741 u64 backref_offset;
6742 u64 extent_end;
6743 u64 num_bytes;
6744 int slot;
6745 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04006746 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08006747
Chris Mason46bfbb52010-05-26 11:04:10 -04006748 path = btrfs_alloc_path();
6749 if (!path)
6750 return -ENOMEM;
6751
Josef Bacik00361582013-08-14 14:02:47 -04006752 ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04006753 offset, 0);
6754 if (ret < 0)
6755 goto out;
6756
6757 slot = path->slots[0];
6758 if (ret == 1) {
6759 if (slot == 0) {
6760 /* can't find the item, must cow */
6761 ret = 0;
6762 goto out;
6763 }
6764 slot--;
6765 }
6766 ret = 0;
6767 leaf = path->nodes[0];
6768 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08006769 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04006770 key.type != BTRFS_EXTENT_DATA_KEY) {
6771 /* not our file or wrong item type, must cow */
6772 goto out;
6773 }
6774
6775 if (key.offset > offset) {
6776 /* Wrong offset, must cow */
6777 goto out;
6778 }
6779
6780 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
6781 found_type = btrfs_file_extent_type(leaf, fi);
6782 if (found_type != BTRFS_FILE_EXTENT_REG &&
6783 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
6784 /* not a regular extent, must cow */
6785 goto out;
6786 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04006787
6788 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
6789 goto out;
6790
Miao Xiee77751a2013-12-27 21:11:50 +08006791 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
6792 if (extent_end <= offset)
6793 goto out;
6794
Chris Mason46bfbb52010-05-26 11:04:10 -04006795 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04006796 if (disk_bytenr == 0)
6797 goto out;
6798
6799 if (btrfs_file_extent_compression(leaf, fi) ||
6800 btrfs_file_extent_encryption(leaf, fi) ||
6801 btrfs_file_extent_other_encoding(leaf, fi))
6802 goto out;
6803
Chris Mason46bfbb52010-05-26 11:04:10 -04006804 backref_offset = btrfs_file_extent_offset(leaf, fi);
6805
Josef Bacik7ee9e442013-06-21 16:37:03 -04006806 if (orig_start) {
6807 *orig_start = key.offset - backref_offset;
6808 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
6809 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
6810 }
Josef Bacikeb384b52013-04-24 16:32:55 -04006811
Chris Mason46bfbb52010-05-26 11:04:10 -04006812 if (btrfs_extent_readonly(root, disk_bytenr))
6813 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08006814
6815 num_bytes = min(offset + *len, extent_end) - offset;
6816 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
6817 u64 range_end;
6818
6819 range_end = round_up(offset + num_bytes, root->sectorsize) - 1;
6820 ret = test_range_bit(io_tree, offset, range_end,
6821 EXTENT_DELALLOC, 0, NULL);
6822 if (ret) {
6823 ret = -EAGAIN;
6824 goto out;
6825 }
6826 }
6827
Josef Bacik1bda19e2013-10-18 12:10:36 -04006828 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04006829
6830 /*
6831 * look for other files referencing this extent, if we
6832 * find any we must cow
6833 */
Josef Bacik00361582013-08-14 14:02:47 -04006834 trans = btrfs_join_transaction(root);
6835 if (IS_ERR(trans)) {
6836 ret = 0;
Chris Mason46bfbb52010-05-26 11:04:10 -04006837 goto out;
Josef Bacik00361582013-08-14 14:02:47 -04006838 }
6839
6840 ret = btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
6841 key.offset - backref_offset, disk_bytenr);
6842 btrfs_end_transaction(trans, root);
6843 if (ret) {
6844 ret = 0;
6845 goto out;
6846 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006847
6848 /*
6849 * adjust disk_bytenr and num_bytes to cover just the bytes
6850 * in this extent we are about to write. If there
6851 * are any csums in that range we have to cow in order
6852 * to keep the csums correct
6853 */
6854 disk_bytenr += backref_offset;
6855 disk_bytenr += offset - key.offset;
Chris Mason46bfbb52010-05-26 11:04:10 -04006856 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
6857 goto out;
6858 /*
6859 * all of the above have passed, it is safe to overwrite this extent
6860 * without cow
6861 */
Josef Bacikeb384b52013-04-24 16:32:55 -04006862 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04006863 ret = 1;
6864out:
6865 btrfs_free_path(path);
6866 return ret;
6867}
6868
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07006869bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
6870{
6871 struct radix_tree_root *root = &inode->i_mapping->page_tree;
6872 int found = false;
6873 void **pagep = NULL;
6874 struct page *page = NULL;
6875 int start_idx;
6876 int end_idx;
6877
6878 start_idx = start >> PAGE_CACHE_SHIFT;
6879
6880 /*
6881 * end is the last byte in the last page. end == start is legal
6882 */
6883 end_idx = end >> PAGE_CACHE_SHIFT;
6884
6885 rcu_read_lock();
6886
6887 /* Most of the code in this while loop is lifted from
6888 * find_get_page. It's been modified to begin searching from a
6889 * page and return just the first page found in that range. If the
6890 * found idx is less than or equal to the end idx then we know that
6891 * a page exists. If no pages are found or if those pages are
6892 * outside of the range then we're fine (yay!) */
6893 while (page == NULL &&
6894 radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
6895 page = radix_tree_deref_slot(pagep);
6896 if (unlikely(!page))
6897 break;
6898
6899 if (radix_tree_exception(page)) {
Filipe Manana809f9012014-06-05 13:22:25 +01006900 if (radix_tree_deref_retry(page)) {
6901 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07006902 continue;
Filipe Manana809f9012014-06-05 13:22:25 +01006903 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07006904 /*
6905 * Otherwise, shmem/tmpfs must be storing a swap entry
6906 * here as an exceptional entry: so return it without
6907 * attempting to raise page count.
6908 */
Filipe Manana6fdef6d2014-06-05 13:22:26 +01006909 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07006910 break; /* TODO: Is this relevant for this use case? */
6911 }
6912
Filipe Manana91405152014-06-05 13:22:24 +01006913 if (!page_cache_get_speculative(page)) {
6914 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07006915 continue;
Filipe Manana91405152014-06-05 13:22:24 +01006916 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07006917
6918 /*
6919 * Has the page moved?
6920 * This is part of the lockless pagecache protocol. See
6921 * include/linux/pagemap.h for details.
6922 */
6923 if (unlikely(page != *pagep)) {
6924 page_cache_release(page);
6925 page = NULL;
6926 }
6927 }
6928
6929 if (page) {
6930 if (page->index <= end_idx)
6931 found = true;
6932 page_cache_release(page);
6933 }
6934
6935 rcu_read_unlock();
6936 return found;
6937}
6938
Josef Bacikeb838e72012-07-31 16:28:48 -04006939static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
6940 struct extent_state **cached_state, int writing)
6941{
6942 struct btrfs_ordered_extent *ordered;
6943 int ret = 0;
6944
6945 while (1) {
6946 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6947 0, cached_state);
6948 /*
6949 * We're concerned with the entire range that we're going to be
6950 * doing DIO to, so we need to make sure theres no ordered
6951 * extents in this range.
6952 */
6953 ordered = btrfs_lookup_ordered_range(inode, lockstart,
6954 lockend - lockstart + 1);
6955
6956 /*
6957 * We need to make sure there are no buffered pages in this
6958 * range either, we could have raced between the invalidate in
6959 * generic_file_direct_write and locking the extent. The
6960 * invalidate needs to happen so that reads after a write do not
6961 * get stale data.
6962 */
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07006963 if (!ordered &&
6964 (!writing ||
6965 !btrfs_page_exists_in_range(inode, lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04006966 break;
6967
6968 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6969 cached_state, GFP_NOFS);
6970
6971 if (ordered) {
6972 btrfs_start_ordered_extent(inode, ordered, 1);
6973 btrfs_put_ordered_extent(ordered);
6974 } else {
6975 /* Screw you mmap */
6976 ret = filemap_write_and_wait_range(inode->i_mapping,
6977 lockstart,
6978 lockend);
6979 if (ret)
6980 break;
6981
6982 /*
6983 * If we found a page that couldn't be invalidated just
6984 * fall back to buffered.
6985 */
6986 ret = invalidate_inode_pages2_range(inode->i_mapping,
6987 lockstart >> PAGE_CACHE_SHIFT,
6988 lockend >> PAGE_CACHE_SHIFT);
6989 if (ret)
6990 break;
6991 }
6992
6993 cond_resched();
6994 }
6995
6996 return ret;
6997}
6998
Josef Bacik69ffb542012-09-11 15:40:07 -04006999static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
7000 u64 len, u64 orig_start,
7001 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04007002 u64 orig_block_len, u64 ram_bytes,
7003 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007004{
7005 struct extent_map_tree *em_tree;
7006 struct extent_map *em;
7007 struct btrfs_root *root = BTRFS_I(inode)->root;
7008 int ret;
7009
7010 em_tree = &BTRFS_I(inode)->extent_tree;
7011 em = alloc_extent_map();
7012 if (!em)
7013 return ERR_PTR(-ENOMEM);
7014
7015 em->start = start;
7016 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04007017 em->mod_start = start;
7018 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04007019 em->len = len;
7020 em->block_len = block_len;
7021 em->block_start = block_start;
7022 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007023 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007024 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007025 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007026 set_bit(EXTENT_FLAG_PINNED, &em->flags);
7027 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05007028 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04007029
7030 do {
7031 btrfs_drop_extent_cache(inode, em->start,
7032 em->start + em->len - 1, 0);
7033 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007034 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007035 write_unlock(&em_tree->lock);
7036 } while (ret == -EEXIST);
7037
7038 if (ret) {
7039 free_extent_map(em);
7040 return ERR_PTR(ret);
7041 }
7042
7043 return em;
7044}
7045
7046
Josef Bacik4b46fce2010-05-23 11:00:55 -04007047static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7048 struct buffer_head *bh_result, int create)
7049{
7050 struct extent_map *em;
7051 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04007052 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007053 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007054 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007055 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007056 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007057 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007058
Miao Xie172a5042013-02-21 02:48:22 -07007059 if (create)
Josef Bacikeb838e72012-07-31 16:28:48 -04007060 unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007061 else
Josef Bacikc3298612012-08-03 16:49:19 -04007062 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007063
Josef Bacikc3298612012-08-03 16:49:19 -04007064 lockstart = start;
7065 lockend = start + len - 1;
7066
Josef Bacikeb838e72012-07-31 16:28:48 -04007067 /*
7068 * If this errors out it's because we couldn't invalidate pagecache for
7069 * this range and we need to fallback to buffered.
7070 */
7071 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
7072 return -ENOTBLK;
7073
Josef Bacik4b46fce2010-05-23 11:00:55 -04007074 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007075 if (IS_ERR(em)) {
7076 ret = PTR_ERR(em);
7077 goto unlock_err;
7078 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007079
7080 /*
7081 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7082 * io. INLINE is special, and we could probably kludge it in here, but
7083 * it's still buffered so for safety lets just fall back to the generic
7084 * buffered path.
7085 *
7086 * For COMPRESSED we _have_ to read the entire extent in so we can
7087 * decompress it, so there will be buffering required no matter what we
7088 * do, so go ahead and fallback to buffered.
7089 *
7090 * We return -ENOTBLK because thats what makes DIO go ahead and go back
7091 * to buffered IO. Don't blame me, this is the price we pay for using
7092 * the generic code.
7093 */
7094 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7095 em->block_start == EXTENT_MAP_INLINE) {
7096 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007097 ret = -ENOTBLK;
7098 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007099 }
7100
7101 /* Just a good old fashioned hole, return */
7102 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7103 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7104 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007105 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007106 }
7107
7108 /*
7109 * We don't allocate a new extent in the following cases
7110 *
7111 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7112 * existing extent.
7113 * 2) The extent is marked as PREALLOC. We're good to go here and can
7114 * just use the extent.
7115 *
7116 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007117 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007118 len = min(len, em->len - (start - em->start));
7119 lockstart = start + len;
7120 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007121 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007122
7123 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7124 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7125 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007126 int type;
7127 int ret;
Josef Bacikeb384b52013-04-24 16:32:55 -04007128 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007129
7130 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7131 type = BTRFS_ORDERED_PREALLOC;
7132 else
7133 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007134 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007135 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007136
Josef Bacik00361582013-08-14 14:02:47 -04007137 if (can_nocow_extent(inode, start, &len, &orig_start,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007138 &orig_block_len, &ram_bytes) == 1) {
Josef Bacik69ffb542012-09-11 15:40:07 -04007139 if (type == BTRFS_ORDERED_PREALLOC) {
7140 free_extent_map(em);
7141 em = create_pinned_em(inode, start, len,
7142 orig_start,
Josef Bacikb4939682012-12-03 10:31:19 -05007143 block_start, len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04007144 orig_block_len,
7145 ram_bytes, type);
Filipe Manana555e1282014-07-07 12:35:21 +01007146 if (IS_ERR(em)) {
7147 ret = PTR_ERR(em);
Josef Bacik69ffb542012-09-11 15:40:07 -04007148 goto unlock_err;
Filipe Manana555e1282014-07-07 12:35:21 +01007149 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007150 }
7151
Chris Mason46bfbb52010-05-26 11:04:10 -04007152 ret = btrfs_add_ordered_extent_dio(inode, start,
7153 block_start, len, len, type);
Chris Mason46bfbb52010-05-26 11:04:10 -04007154 if (ret) {
7155 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007156 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007157 }
7158 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007159 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007160 }
Josef Bacik00361582013-08-14 14:02:47 -04007161
Chris Mason46bfbb52010-05-26 11:04:10 -04007162 /*
7163 * this will cow the extent, reset the len in case we changed
7164 * it above
7165 */
7166 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007167 free_extent_map(em);
7168 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007169 if (IS_ERR(em)) {
7170 ret = PTR_ERR(em);
7171 goto unlock_err;
7172 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007173 len = min(len, em->len - (start - em->start));
7174unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007175 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7176 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007177 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007178 bh_result->b_bdev = em->bdev;
7179 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007180 if (create) {
7181 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7182 set_buffer_new(bh_result);
7183
7184 /*
7185 * Need to update the i_size under the extent lock so buffered
7186 * readers will get the updated i_size when we unlock.
7187 */
7188 if (start + len > i_size_read(inode))
7189 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007190
Miao Xie172a5042013-02-21 02:48:22 -07007191 spin_lock(&BTRFS_I(inode)->lock);
7192 BTRFS_I(inode)->outstanding_extents++;
7193 spin_unlock(&BTRFS_I(inode)->lock);
7194
Miao Xie09348562013-02-07 10:12:07 +00007195 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7196 lockstart + len - 1, EXTENT_DELALLOC, NULL,
7197 &cached_state, GFP_NOFS);
7198 BUG_ON(ret);
Josef Bacikc3473e82012-06-19 10:59:00 -04007199 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007200
Josef Bacikeb838e72012-07-31 16:28:48 -04007201 /*
7202 * In the case of write we need to clear and unlock the entire range,
7203 * in the case of read we need to unlock only the end area that we
7204 * aren't using if there is any left over space.
7205 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007206 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007207 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7208 lockend, unlock_bits, 1, 0,
7209 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06007210 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007211 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007212 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007213
Josef Bacik4b46fce2010-05-23 11:00:55 -04007214 free_extent_map(em);
7215
7216 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007217
7218unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007219 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7220 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
7221 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007222}
7223
Josef Bacik4b46fce2010-05-23 11:00:55 -04007224static void btrfs_endio_direct_read(struct bio *bio, int err)
7225{
Miao Xiee65e1532010-11-22 03:04:43 +00007226 struct btrfs_dio_private *dip = bio->bi_private;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007227 struct bio_vec *bvec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007228 struct inode *inode = dip->inode;
7229 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason9be33952013-05-17 18:30:14 -04007230 struct bio *dio_bio;
Miao Xiefacc8a222013-07-25 19:22:34 +08007231 u32 *csums = (u32 *)dip->csum;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007232 u64 start;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007233 int i;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007234
7235 start = dip->logical_offset;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007236 bio_for_each_segment_all(bvec, bio, i) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007237 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
7238 struct page *page = bvec->bv_page;
7239 char *kaddr;
7240 u32 csum = ~(u32)0;
7241 unsigned long flags;
7242
7243 local_irq_save(flags);
Cong Wang7ac687d2011-11-25 23:14:28 +08007244 kaddr = kmap_atomic(page);
Liu Bob0496682013-03-14 14:57:45 +00007245 csum = btrfs_csum_data(kaddr + bvec->bv_offset,
Josef Bacik4b46fce2010-05-23 11:00:55 -04007246 csum, bvec->bv_len);
7247 btrfs_csum_final(csum, (char *)&csum);
Cong Wang7ac687d2011-11-25 23:14:28 +08007248 kunmap_atomic(kaddr);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007249 local_irq_restore(flags);
7250
7251 flush_dcache_page(bvec->bv_page);
Kent Overstreet2c30c712013-11-07 12:20:26 -08007252 if (csum != csums[i]) {
Miao Xiefacc8a222013-07-25 19:22:34 +08007253 btrfs_err(root->fs_info, "csum failed ino %llu off %llu csum %u expected csum %u",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02007254 btrfs_ino(inode), start, csum,
Kent Overstreet2c30c712013-11-07 12:20:26 -08007255 csums[i]);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007256 err = -EIO;
7257 }
7258 }
7259
7260 start += bvec->bv_len;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007261 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007262
7263 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007264 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04007265 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007266
Josef Bacik4b46fce2010-05-23 11:00:55 -04007267 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04007268
7269 /* If we had a csum failure make sure to clear the uptodate flag */
7270 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04007271 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
7272 dio_end_io(dio_bio, err);
7273 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007274}
7275
7276static void btrfs_endio_direct_write(struct bio *bio, int err)
7277{
7278 struct btrfs_dio_private *dip = bio->bi_private;
7279 struct inode *inode = dip->inode;
7280 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007281 struct btrfs_ordered_extent *ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05007282 u64 ordered_offset = dip->logical_offset;
7283 u64 ordered_bytes = dip->bytes;
Chris Mason9be33952013-05-17 18:30:14 -04007284 struct bio *dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007285 int ret;
7286
7287 if (err)
7288 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05007289again:
7290 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
7291 &ordered_offset,
Josef Bacik5fd02042012-05-02 14:00:54 -04007292 ordered_bytes, !err);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007293 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05007294 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007295
Liu Bo9e0af232014-08-15 23:36:53 +08007296 btrfs_init_work(&ordered->work, btrfs_endio_write_helper,
7297 finish_ordered_fn, NULL, NULL);
Qu Wenruofccb5d82014-02-28 10:46:10 +08007298 btrfs_queue_work(root->fs_info->endio_write_workers,
7299 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05007300out_test:
7301 /*
7302 * our bio might span multiple ordered extents. If we haven't
7303 * completed the accounting for the whole dio, go back and try again
7304 */
7305 if (ordered_offset < dip->logical_offset + dip->bytes) {
7306 ordered_bytes = dip->logical_offset + dip->bytes -
7307 ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04007308 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05007309 goto again;
7310 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007311out_done:
Chris Mason9be33952013-05-17 18:30:14 -04007312 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007313
Josef Bacik4b46fce2010-05-23 11:00:55 -04007314 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04007315
7316 /* If we had an error make sure to clear the uptodate flag */
7317 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04007318 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
7319 dio_end_io(dio_bio, err);
7320 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007321}
7322
Chris Masoneaf25d92010-05-25 09:48:28 -04007323static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
7324 struct bio *bio, int mirror_num,
7325 unsigned long bio_flags, u64 offset)
7326{
7327 int ret;
7328 struct btrfs_root *root = BTRFS_I(inode)->root;
7329 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007330 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04007331 return 0;
7332}
7333
Miao Xiee65e1532010-11-22 03:04:43 +00007334static void btrfs_end_dio_bio(struct bio *bio, int err)
7335{
7336 struct btrfs_dio_private *dip = bio->bi_private;
7337
7338 if (err) {
Frank Holtonefe120a2013-12-20 11:37:06 -05007339 btrfs_err(BTRFS_I(dip->inode)->root->fs_info,
7340 "direct IO failed ino %llu rw %lu sector %#Lx len %u err no %d",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02007341 btrfs_ino(dip->inode), bio->bi_rw,
Kent Overstreet4f024f32013-10-11 15:44:27 -07007342 (unsigned long long)bio->bi_iter.bi_sector,
7343 bio->bi_iter.bi_size, err);
Miao Xiee65e1532010-11-22 03:04:43 +00007344 dip->errors = 1;
7345
7346 /*
7347 * before atomic variable goto zero, we must make sure
7348 * dip->errors is perceived to be set.
7349 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01007350 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00007351 }
7352
7353 /* if there are more bios still pending for this dio, just exit */
7354 if (!atomic_dec_and_test(&dip->pending_bios))
7355 goto out;
7356
Chris Mason9be33952013-05-17 18:30:14 -04007357 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00007358 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04007359 } else {
7360 set_bit(BIO_UPTODATE, &dip->dio_bio->bi_flags);
Miao Xiee65e1532010-11-22 03:04:43 +00007361 bio_endio(dip->orig_bio, 0);
7362 }
7363out:
7364 bio_put(bio);
7365}
7366
7367static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
7368 u64 first_sector, gfp_t gfp_flags)
7369{
7370 int nr_vecs = bio_get_nr_vecs(bdev);
7371 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
7372}
7373
7374static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
7375 int rw, u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007376 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00007377{
Miao Xiefacc8a222013-07-25 19:22:34 +08007378 struct btrfs_dio_private *dip = bio->bi_private;
Miao Xiee65e1532010-11-22 03:04:43 +00007379 int write = rw & REQ_WRITE;
7380 struct btrfs_root *root = BTRFS_I(inode)->root;
7381 int ret;
7382
Josef Bacikb812ce22012-11-16 13:56:32 -05007383 if (async_submit)
7384 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
7385
Miao Xiee65e1532010-11-22 03:04:43 +00007386 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04007387
7388 if (!write) {
7389 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
7390 if (ret)
7391 goto err;
7392 }
Miao Xiee65e1532010-11-22 03:04:43 +00007393
Josef Bacik1ae39932011-04-06 14:41:34 -04007394 if (skip_sum)
7395 goto map;
7396
7397 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00007398 ret = btrfs_wq_submit_bio(root->fs_info,
7399 inode, rw, bio, 0, 0,
7400 file_offset,
7401 __btrfs_submit_bio_start_direct_io,
7402 __btrfs_submit_bio_done);
7403 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04007404 } else if (write) {
7405 /*
7406 * If we aren't doing async submit, calculate the csum of the
7407 * bio now.
7408 */
7409 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
7410 if (ret)
7411 goto err;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007412 } else if (!skip_sum) {
Miao Xiefacc8a222013-07-25 19:22:34 +08007413 ret = btrfs_lookup_bio_sums_dio(root, inode, dip, bio,
7414 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007415 if (ret)
7416 goto err;
7417 }
Miao Xiee65e1532010-11-22 03:04:43 +00007418
Josef Bacik1ae39932011-04-06 14:41:34 -04007419map:
7420 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007421err:
7422 bio_put(bio);
7423 return ret;
7424}
7425
7426static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
7427 int skip_sum)
7428{
7429 struct inode *inode = dip->inode;
7430 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00007431 struct bio *bio;
7432 struct bio *orig_bio = dip->orig_bio;
7433 struct bio_vec *bvec = orig_bio->bi_io_vec;
Kent Overstreet4f024f32013-10-11 15:44:27 -07007434 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00007435 u64 file_offset = dip->logical_offset;
7436 u64 submit_len = 0;
7437 u64 map_length;
7438 int nr_pages = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007439 int ret = 0;
Josef Bacik1ae39932011-04-06 14:41:34 -04007440 int async_submit = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007441
Kent Overstreet4f024f32013-10-11 15:44:27 -07007442 map_length = orig_bio->bi_iter.bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007443 ret = btrfs_map_block(root->fs_info, rw, start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007444 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08007445 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00007446 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08007447
Kent Overstreet4f024f32013-10-11 15:44:27 -07007448 if (map_length >= orig_bio->bi_iter.bi_size) {
Josef Bacik02f57c72011-04-06 14:25:44 -04007449 bio = orig_bio;
7450 goto submit;
7451 }
7452
David Woodhouse53b381b2013-01-29 18:40:14 -05007453 /* async crcs make it difficult to collect full stripe writes. */
7454 if (btrfs_get_alloc_profile(root, 1) &
7455 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6))
7456 async_submit = 0;
7457 else
7458 async_submit = 1;
7459
Josef Bacik02f57c72011-04-06 14:25:44 -04007460 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
7461 if (!bio)
7462 return -ENOMEM;
Miao Xie7a5c3c92014-06-17 18:58:59 +08007463
Josef Bacik02f57c72011-04-06 14:25:44 -04007464 bio->bi_private = dip;
7465 bio->bi_end_io = btrfs_end_dio_bio;
7466 atomic_inc(&dip->pending_bios);
7467
Miao Xiee65e1532010-11-22 03:04:43 +00007468 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
7469 if (unlikely(map_length < submit_len + bvec->bv_len ||
7470 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
7471 bvec->bv_offset) < bvec->bv_len)) {
7472 /*
7473 * inc the count before we submit the bio so
7474 * we know the end IO handler won't happen before
7475 * we inc the count. Otherwise, the dip might get freed
7476 * before we're done setting it up
7477 */
7478 atomic_inc(&dip->pending_bios);
7479 ret = __btrfs_submit_dio_bio(bio, inode, rw,
7480 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007481 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007482 if (ret) {
7483 bio_put(bio);
7484 atomic_dec(&dip->pending_bios);
7485 goto out_err;
7486 }
7487
Miao Xiee65e1532010-11-22 03:04:43 +00007488 start_sector += submit_len >> 9;
7489 file_offset += submit_len;
7490
7491 submit_len = 0;
7492 nr_pages = 0;
7493
7494 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
7495 start_sector, GFP_NOFS);
7496 if (!bio)
7497 goto out_err;
7498 bio->bi_private = dip;
7499 bio->bi_end_io = btrfs_end_dio_bio;
7500
Kent Overstreet4f024f32013-10-11 15:44:27 -07007501 map_length = orig_bio->bi_iter.bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007502 ret = btrfs_map_block(root->fs_info, rw,
Stefan Behrens3ec706c2012-11-05 15:46:42 +01007503 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007504 &map_length, NULL, 0);
7505 if (ret) {
7506 bio_put(bio);
7507 goto out_err;
7508 }
7509 } else {
7510 submit_len += bvec->bv_len;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307511 nr_pages++;
Miao Xiee65e1532010-11-22 03:04:43 +00007512 bvec++;
7513 }
7514 }
7515
Josef Bacik02f57c72011-04-06 14:25:44 -04007516submit:
Miao Xiee65e1532010-11-22 03:04:43 +00007517 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007518 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007519 if (!ret)
7520 return 0;
7521
7522 bio_put(bio);
7523out_err:
7524 dip->errors = 1;
7525 /*
7526 * before atomic variable goto zero, we must
7527 * make sure dip->errors is perceived to be set.
7528 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01007529 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00007530 if (atomic_dec_and_test(&dip->pending_bios))
7531 bio_io_error(dip->orig_bio);
7532
7533 /* bio_end_io() will handle error, so we needn't return it */
7534 return 0;
7535}
7536
Chris Mason9be33952013-05-17 18:30:14 -04007537static void btrfs_submit_direct(int rw, struct bio *dio_bio,
7538 struct inode *inode, loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007539{
7540 struct btrfs_root *root = BTRFS_I(inode)->root;
7541 struct btrfs_dio_private *dip;
Chris Mason9be33952013-05-17 18:30:14 -04007542 struct bio *io_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007543 int skip_sum;
Miao Xiefacc8a222013-07-25 19:22:34 +08007544 int sum_len;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02007545 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007546 int ret = 0;
Miao Xiefacc8a222013-07-25 19:22:34 +08007547 u16 csum_size;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007548
7549 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
7550
Chris Mason9be33952013-05-17 18:30:14 -04007551 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04007552 if (!io_bio) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007553 ret = -ENOMEM;
7554 goto free_ordered;
7555 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007556
Miao Xiefacc8a222013-07-25 19:22:34 +08007557 if (!skip_sum && !write) {
7558 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
Kent Overstreet4f024f32013-10-11 15:44:27 -07007559 sum_len = dio_bio->bi_iter.bi_size >>
7560 inode->i_sb->s_blocksize_bits;
Miao Xiefacc8a222013-07-25 19:22:34 +08007561 sum_len *= csum_size;
7562 } else {
7563 sum_len = 0;
7564 }
7565
7566 dip = kmalloc(sizeof(*dip) + sum_len, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04007567 if (!dip) {
7568 ret = -ENOMEM;
7569 goto free_io_bio;
7570 }
7571
7572 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007573 dip->inode = inode;
7574 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07007575 dip->bytes = dio_bio->bi_iter.bi_size;
7576 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Chris Mason9be33952013-05-17 18:30:14 -04007577 io_bio->bi_private = dip;
Miao Xiee65e1532010-11-22 03:04:43 +00007578 dip->errors = 0;
Chris Mason9be33952013-05-17 18:30:14 -04007579 dip->orig_bio = io_bio;
7580 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00007581 atomic_set(&dip->pending_bios, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007582
7583 if (write)
Chris Mason9be33952013-05-17 18:30:14 -04007584 io_bio->bi_end_io = btrfs_endio_direct_write;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007585 else
Chris Mason9be33952013-05-17 18:30:14 -04007586 io_bio->bi_end_io = btrfs_endio_direct_read;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007587
Miao Xiee65e1532010-11-22 03:04:43 +00007588 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
7589 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04007590 return;
Chris Mason9be33952013-05-17 18:30:14 -04007591
7592free_io_bio:
7593 bio_put(io_bio);
7594
Josef Bacik4b46fce2010-05-23 11:00:55 -04007595free_ordered:
7596 /*
7597 * If this is a write, we need to clean up the reserved space and kill
7598 * the ordered extent.
7599 */
7600 if (write) {
7601 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05007602 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007603 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
7604 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
7605 btrfs_free_reserved_extent(root, ordered->start,
Miao Xiee570fd22014-06-19 10:42:50 +08007606 ordered->disk_len, 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007607 btrfs_put_ordered_extent(ordered);
7608 btrfs_put_ordered_extent(ordered);
7609 }
Chris Mason9be33952013-05-17 18:30:14 -04007610 bio_endio(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007611}
7612
Chris Mason5a5f79b2010-05-26 21:33:37 -04007613static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
Al Viro28060d52014-03-22 05:15:17 -04007614 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04007615{
7616 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00007617 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007618 unsigned blocksize_mask = root->sectorsize - 1;
7619 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007620
7621 if (offset & blocksize_mask)
7622 goto out;
7623
Al Viro28060d52014-03-22 05:15:17 -04007624 if (iov_iter_alignment(iter) & blocksize_mask)
7625 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00007626
Al Viro28060d52014-03-22 05:15:17 -04007627 /* If this is a write we don't need to check anymore */
7628 if (rw & WRITE)
7629 return 0;
7630 /*
7631 * Check to make sure we don't have duplicate iov_base's in this
7632 * iovec, if so return EINVAL, otherwise we'll get csum errors
7633 * when reading back.
7634 */
7635 for (seg = 0; seg < iter->nr_segs; seg++) {
7636 for (i = seg + 1; i < iter->nr_segs; i++) {
7637 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00007638 goto out;
7639 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04007640 }
7641 retval = 0;
7642out:
7643 return retval;
7644}
Josef Bacikeb838e72012-07-31 16:28:48 -04007645
Chris Mason16432982008-04-10 10:23:21 -04007646static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
Al Virod8d3d942014-03-04 21:27:34 -05007647 struct iov_iter *iter, loff_t offset)
Chris Mason16432982008-04-10 10:23:21 -04007648{
Josef Bacik4b46fce2010-05-23 11:00:55 -04007649 struct file *file = iocb->ki_filp;
7650 struct inode *inode = file->f_mapping->host;
Miao Xie09348562013-02-07 10:12:07 +00007651 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00007652 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00007653 bool wakeup = true;
7654 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00007655 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007656
Al Viro28060d52014-03-22 05:15:17 -04007657 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007658 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007659
Miao Xie38851cc2013-02-08 07:04:11 +00007660 atomic_inc(&inode->i_dio_count);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01007661 smp_mb__after_atomic();
Miao Xie38851cc2013-02-08 07:04:11 +00007662
Josef Bacik0e267c42013-07-02 10:38:02 -04007663 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08007664 * The generic stuff only does filemap_write_and_wait_range, which
7665 * isn't enough if we've written compressed pages to this area, so
7666 * we need to flush the dirty pages again to make absolutely sure
7667 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04007668 */
Al Viroa6cbcd42014-03-04 22:38:00 -05007669 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08007670 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
7671 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08007672 filemap_fdatawrite_range(inode->i_mapping, offset,
7673 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04007674
Miao Xie09348562013-02-07 10:12:07 +00007675 if (rw & WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00007676 /*
7677 * If the write DIO is beyond the EOF, we need update
7678 * the isize, but it is protected by i_mutex. So we can
7679 * not unlock the i_mutex at this case.
7680 */
7681 if (offset + count <= inode->i_size) {
7682 mutex_unlock(&inode->i_mutex);
7683 relock = true;
7684 }
Miao Xie09348562013-02-07 10:12:07 +00007685 ret = btrfs_delalloc_reserve_space(inode, count);
7686 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00007687 goto out;
7688 } else if (unlikely(test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
7689 &BTRFS_I(inode)->runtime_flags))) {
7690 inode_dio_done(inode);
7691 flags = DIO_LOCKING | DIO_SKIP_HOLES;
7692 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00007693 }
7694
7695 ret = __blockdev_direct_IO(rw, iocb, inode,
7696 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
Al Viro31b14032014-03-05 01:33:16 -05007697 iter, offset, btrfs_get_blocks_direct, NULL,
Miao Xie2e60a512013-02-08 07:01:08 +00007698 btrfs_submit_direct, flags);
Miao Xie09348562013-02-07 10:12:07 +00007699 if (rw & WRITE) {
7700 if (ret < 0 && ret != -EIOCBQUEUED)
7701 btrfs_delalloc_release_space(inode, count);
Miao Xie172a5042013-02-21 02:48:22 -07007702 else if (ret >= 0 && (size_t)ret < count)
Miao Xie09348562013-02-07 10:12:07 +00007703 btrfs_delalloc_release_space(inode,
7704 count - (size_t)ret);
Miao Xie172a5042013-02-21 02:48:22 -07007705 else
7706 btrfs_delalloc_release_metadata(inode, 0);
Miao Xie09348562013-02-07 10:12:07 +00007707 }
Miao Xie38851cc2013-02-08 07:04:11 +00007708out:
Miao Xie2e60a512013-02-08 07:01:08 +00007709 if (wakeup)
7710 inode_dio_done(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00007711 if (relock)
7712 mutex_lock(&inode->i_mutex);
Miao Xie09348562013-02-07 10:12:07 +00007713
7714 return ret;
Chris Mason16432982008-04-10 10:23:21 -04007715}
7716
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007717#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
7718
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007719static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
7720 __u64 start, __u64 len)
7721{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007722 int ret;
7723
7724 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
7725 if (ret)
7726 return ret;
7727
Chris Masonec29ed52011-02-23 16:23:20 -05007728 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007729}
7730
Chris Mason9ebefb182007-06-15 13:50:00 -04007731int btrfs_readpage(struct file *file, struct page *page)
7732{
Chris Masond1310b22008-01-24 16:13:08 -05007733 struct extent_io_tree *tree;
7734 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02007735 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04007736}
Chris Mason1832a6d2007-12-21 16:27:21 -05007737
Chris Mason39279cc2007-06-12 06:35:45 -04007738static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
7739{
Chris Masond1310b22008-01-24 16:13:08 -05007740 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04007741
7742
7743 if (current->flags & PF_MEMALLOC) {
7744 redirty_page_for_writepage(wbc, page);
7745 unlock_page(page);
7746 return 0;
7747 }
Chris Masond1310b22008-01-24 16:13:08 -05007748 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04007749 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
7750}
Chris Mason39279cc2007-06-12 06:35:45 -04007751
Eric Sandeen48a3b632013-04-25 20:41:01 +00007752static int btrfs_writepages(struct address_space *mapping,
7753 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04007754{
Chris Masond1310b22008-01-24 16:13:08 -05007755 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05007756
Chris Masond1310b22008-01-24 16:13:08 -05007757 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04007758 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
7759}
7760
Chris Mason3ab2fb52007-11-08 10:59:22 -05007761static int
7762btrfs_readpages(struct file *file, struct address_space *mapping,
7763 struct list_head *pages, unsigned nr_pages)
7764{
Chris Masond1310b22008-01-24 16:13:08 -05007765 struct extent_io_tree *tree;
7766 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05007767 return extent_readpages(tree, mapping, pages, nr_pages,
7768 btrfs_get_extent);
7769}
Chris Masone6dcd2d2008-07-17 12:53:50 -04007770static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04007771{
Chris Masond1310b22008-01-24 16:13:08 -05007772 struct extent_io_tree *tree;
7773 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04007774 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04007775
Chris Masond1310b22008-01-24 16:13:08 -05007776 tree = &BTRFS_I(page->mapping->host)->io_tree;
7777 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05007778 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04007779 if (ret == 1) {
7780 ClearPagePrivate(page);
7781 set_page_private(page, 0);
7782 page_cache_release(page);
7783 }
7784 return ret;
7785}
Chris Mason39279cc2007-06-12 06:35:45 -04007786
Chris Masone6dcd2d2008-07-17 12:53:50 -04007787static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
7788{
Chris Mason98509cfc2008-09-11 15:51:43 -04007789 if (PageWriteback(page) || PageDirty(page))
7790 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05007791 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007792}
7793
Lukas Czernerd47992f2013-05-21 23:17:23 -04007794static void btrfs_invalidatepage(struct page *page, unsigned int offset,
7795 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04007796{
Josef Bacik5fd02042012-05-02 14:00:54 -04007797 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05007798 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007799 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007800 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007801 u64 page_start = page_offset(page);
7802 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00007803 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04007804
Chris Mason8b62b722009-09-02 16:53:46 -04007805 /*
7806 * we have the page locked, so new writeback can't start,
7807 * and the dirty bit won't be cleared while we are here.
7808 *
7809 * Wait for IO on this page so that we can safely clear
7810 * the PagePrivate2 bit and do ordered accounting
7811 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007812 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04007813
Josef Bacik5fd02042012-05-02 14:00:54 -04007814 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007815 if (offset) {
7816 btrfs_releasepage(page, GFP_NOFS);
7817 return;
7818 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00007819
7820 if (!inode_evicting)
7821 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
7822 ordered = btrfs_lookup_ordered_extent(inode, page_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007823 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04007824 /*
7825 * IO on this page will never be started, so we need
7826 * to account for any ordered extents now
7827 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00007828 if (!inode_evicting)
7829 clear_extent_bit(tree, page_start, page_end,
7830 EXTENT_DIRTY | EXTENT_DELALLOC |
7831 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
7832 EXTENT_DEFRAG, 1, 0, &cached_state,
7833 GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04007834 /*
7835 * whoever cleared the private bit is responsible
7836 * for the finish_ordered_io
7837 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04007838 if (TestClearPagePrivate2(page)) {
7839 struct btrfs_ordered_inode_tree *tree;
7840 u64 new_len;
7841
7842 tree = &BTRFS_I(inode)->ordered_tree;
7843
7844 spin_lock_irq(&tree->lock);
7845 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
7846 new_len = page_start - ordered->file_offset;
7847 if (new_len < ordered->truncated_len)
7848 ordered->truncated_len = new_len;
7849 spin_unlock_irq(&tree->lock);
7850
7851 if (btrfs_dec_test_ordered_pending(inode, &ordered,
7852 page_start,
7853 PAGE_CACHE_SIZE, 1))
7854 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04007855 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007856 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00007857 if (!inode_evicting) {
7858 cached_state = NULL;
7859 lock_extent_bits(tree, page_start, page_end, 0,
7860 &cached_state);
7861 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007862 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00007863
7864 if (!inode_evicting) {
7865 clear_extent_bit(tree, page_start, page_end,
7866 EXTENT_LOCKED | EXTENT_DIRTY |
7867 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
7868 EXTENT_DEFRAG, 1, 1,
7869 &cached_state, GFP_NOFS);
7870
7871 __btrfs_releasepage(page, GFP_NOFS);
7872 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007873
Chris Mason4a096752008-07-21 10:29:44 -04007874 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04007875 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04007876 ClearPagePrivate(page);
7877 set_page_private(page, 0);
7878 page_cache_release(page);
7879 }
Chris Mason39279cc2007-06-12 06:35:45 -04007880}
7881
Chris Mason9ebefb182007-06-15 13:50:00 -04007882/*
7883 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
7884 * called from a page fault handler when a page is first dirtied. Hence we must
7885 * be careful to check for EOF conditions here. We set the page up correctly
7886 * for a written page which means we get ENOSPC checking when writing into
7887 * holes and correct delalloc and unwritten extent mapping on filesystems that
7888 * support these features.
7889 *
7890 * We are not allowed to take the i_mutex here so we have to play games to
7891 * protect against truncate races as the page could now be beyond EOF. Because
7892 * vmtruncate() writes the inode size before removing pages, once we have the
7893 * page lock we can determine safely if the page is beyond EOF. If it is not
7894 * beyond EOF, then the page is guaranteed safe against truncation until we
7895 * unlock the page.
7896 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07007897int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04007898{
Nick Pigginc2ec1752009-03-31 15:23:21 -07007899 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05007900 struct inode *inode = file_inode(vma->vm_file);
Chris Mason1832a6d2007-12-21 16:27:21 -05007901 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007902 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7903 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007904 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007905 char *kaddr;
7906 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04007907 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05007908 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05007909 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007910 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007911 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04007912
Jan Karab2b5ef52012-06-12 16:20:45 +02007913 sb_start_pagefault(inode->i_sb);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04007914 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007915 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04007916 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05007917 reserved = 1;
7918 }
Nick Piggin56a76f82009-03-31 15:23:23 -07007919 if (ret) {
7920 if (ret == -ENOMEM)
7921 ret = VM_FAULT_OOM;
7922 else /* -ENOSPC, -EIO, etc */
7923 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05007924 if (reserved)
7925 goto out;
7926 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07007927 }
Chris Mason1832a6d2007-12-21 16:27:21 -05007928
Nick Piggin56a76f82009-03-31 15:23:23 -07007929 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007930again:
Chris Mason9ebefb182007-06-15 13:50:00 -04007931 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04007932 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007933 page_start = page_offset(page);
7934 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04007935
Chris Mason9ebefb182007-06-15 13:50:00 -04007936 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04007937 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04007938 /* page got truncated out from underneath us */
7939 goto out_unlock;
7940 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007941 wait_on_page_writeback(page);
7942
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007943 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007944 set_page_extent_mapped(page);
7945
Chris Masoneb84ae02008-07-17 13:53:27 -04007946 /*
7947 * we can't set the delalloc bits if there are pending ordered
7948 * extents. Drop our locks and wait for them to finish
7949 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007950 ordered = btrfs_lookup_ordered_extent(inode, page_start);
7951 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007952 unlock_extent_cached(io_tree, page_start, page_end,
7953 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007954 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04007955 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007956 btrfs_put_ordered_extent(ordered);
7957 goto again;
7958 }
7959
Josef Bacikfbf19082009-10-01 17:10:23 -04007960 /*
7961 * XXX - page_mkwrite gets called every time the page is dirtied, even
7962 * if it was already dirty, so for space accounting reasons we need to
7963 * clear any delalloc bits for the range we are fixing to save. There
7964 * is probably a better way to do this, but for now keep consistent with
7965 * prepare_pages in the normal write path.
7966 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00007967 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06007968 EXTENT_DIRTY | EXTENT_DELALLOC |
7969 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00007970 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04007971
Josef Bacik2ac55d42010-02-03 19:33:23 +00007972 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
7973 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007974 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007975 unlock_extent_cached(io_tree, page_start, page_end,
7976 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007977 ret = VM_FAULT_SIGBUS;
7978 goto out_unlock;
7979 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007980 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04007981
7982 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04007983 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04007984 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04007985 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04007986 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04007987
Chris Masone6dcd2d2008-07-17 12:53:50 -04007988 if (zero_start != PAGE_CACHE_SIZE) {
7989 kaddr = kmap(page);
7990 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
7991 flush_dcache_page(page);
7992 kunmap(page);
7993 }
Chris Mason247e7432008-07-17 12:53:51 -04007994 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007995 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04007996 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007997
Chris Mason257c62e2009-10-13 13:21:08 -04007998 BTRFS_I(inode)->last_trans = root->fs_info->generation;
7999 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06008000 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04008001
Josef Bacik2ac55d42010-02-03 19:33:23 +00008002 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04008003
8004out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02008005 if (!ret) {
8006 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04008007 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02008008 }
Chris Mason9ebefb182007-06-15 13:50:00 -04008009 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05008010out:
Josef Bacikec39e182012-01-12 19:10:12 -05008011 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05008012out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02008013 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04008014 return ret;
8015}
8016
Josef Bacika41ad392011-01-31 15:30:16 -05008017static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04008018{
8019 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04008020 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04008021 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05008022 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008023 struct btrfs_trans_handle *trans;
Chris Masondbe674a2008-07-17 12:54:05 -04008024 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04008025 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04008026
Josef Bacik0ef8b722013-10-25 16:13:35 -04008027 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
8028 (u64)-1);
8029 if (ret)
8030 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008031
Josef Bacikfcb80c22011-05-03 10:40:22 -04008032 /*
8033 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
8034 * 3 things going on here
8035 *
8036 * 1) We need to reserve space for our orphan item and the space to
8037 * delete our orphan item. Lord knows we don't want to have a dangling
8038 * orphan item because we didn't reserve space to remove it.
8039 *
8040 * 2) We need to reserve space to update our inode.
8041 *
8042 * 3) We need to have something to cache all the space that is going to
8043 * be free'd up by the truncate operation, but also have some slack
8044 * space reserved in case it uses space during the truncate (thank you
8045 * very much snapshotting).
8046 *
8047 * And we need these to all be seperate. The fact is we can use alot of
8048 * space doing the truncate, and we have no earthly idea how much space
8049 * we will use, so we need the truncate reservation to be seperate so it
8050 * doesn't end up using space reserved for updating the inode or
8051 * removing the orphan item. We also need to be able to stop the
8052 * transaction and start a new one, which means we need to be able to
8053 * update the inode several times, and we have no idea of knowing how
8054 * many times that will be, so we can't just reserve 1 item for the
8055 * entirety of the opration, so that has to be done seperately as well.
8056 * Then there is the orphan item, which does indeed need to be held on
8057 * to for the whole operation, and we need nobody to touch this reserved
8058 * space except the orphan code.
8059 *
8060 * So that leaves us with
8061 *
8062 * 1) root->orphan_block_rsv - for the orphan deletion.
8063 * 2) rsv - for the truncate reservation, which we will steal from the
8064 * transaction reservation.
8065 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
8066 * updating the inode.
8067 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06008068 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008069 if (!rsv)
8070 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04008071 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008072 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05008073
Josef Bacik907cbce2011-08-08 13:46:15 -04008074 /*
Josef Bacik07127182011-08-19 10:29:59 -04008075 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04008076 * 1 for updating the inode.
8077 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05008078 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008079 if (IS_ERR(trans)) {
8080 err = PTR_ERR(trans);
8081 goto out;
8082 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05008083
Josef Bacik907cbce2011-08-08 13:46:15 -04008084 /* Migrate the slack space for the truncate to our reserve */
8085 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
8086 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008087 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05008088
Chris Mason5a3f23d2009-03-31 13:27:11 -04008089 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04008090 * So if we truncate and then write and fsync we normally would just
8091 * write the extents that changed, which is a problem if we need to
8092 * first truncate that entire inode. So set this flag so we write out
8093 * all of the extents in the inode to the sync log so we're completely
8094 * safe.
8095 */
8096 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008097 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008098
Yan, Zheng80825102009-11-12 09:35:36 +00008099 while (1) {
8100 ret = btrfs_truncate_inode_items(trans, root, inode,
8101 inode->i_size,
8102 BTRFS_EXTENT_DATA_KEY);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008103 if (ret != -ENOSPC) {
Josef Bacik3893e332011-01-31 16:03:11 -05008104 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00008105 break;
Josef Bacik3893e332011-01-31 16:03:11 -05008106 }
Chris Mason39279cc2007-06-12 06:35:45 -04008107
Josef Bacikfcb80c22011-05-03 10:40:22 -04008108 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008109 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05008110 if (ret) {
8111 err = ret;
8112 break;
8113 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04008114
Yan, Zheng80825102009-11-12 09:35:36 +00008115 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00008116 btrfs_btree_balance_dirty(root);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008117
8118 trans = btrfs_start_transaction(root, 2);
8119 if (IS_ERR(trans)) {
8120 ret = err = PTR_ERR(trans);
8121 trans = NULL;
8122 break;
8123 }
8124
8125 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
8126 rsv, min_size);
8127 BUG_ON(ret); /* shouldn't happen */
8128 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008129 }
8130
8131 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04008132 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008133 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05008134 if (ret)
8135 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00008136 }
8137
Chris Mason917c16b2011-11-08 14:49:59 -05008138 if (trans) {
8139 trans->block_rsv = &root->fs_info->trans_block_rsv;
8140 ret = btrfs_update_inode(trans, root, inode);
8141 if (ret && !err)
8142 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04008143
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008144 ret = btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00008145 btrfs_btree_balance_dirty(root);
Chris Mason917c16b2011-11-08 14:49:59 -05008146 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04008147
8148out:
8149 btrfs_free_block_rsv(root, rsv);
8150
Josef Bacik3893e332011-01-31 16:03:11 -05008151 if (ret && !err)
8152 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05008153
Josef Bacik3893e332011-01-31 16:03:11 -05008154 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04008155}
8156
Sven Wegener3b963622008-06-09 21:57:42 -04008157/*
Chris Masond352ac62008-09-29 15:18:18 -04008158 * create a new subvolume directory/inode (helper for the ioctl).
8159 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05008160int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008161 struct btrfs_root *new_root,
8162 struct btrfs_root *parent_root,
8163 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04008164{
Chris Mason39279cc2007-06-12 06:35:45 -04008165 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04008166 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008167 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008168
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01008169 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
8170 new_dirid, new_dirid,
8171 S_IFDIR | (~current_umask() & S_IRWXUGO),
8172 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04008173 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04008174 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008175 inode->i_op = &btrfs_dir_inode_operations;
8176 inode->i_fop = &btrfs_dir_file_operations;
8177
Miklos Szeredibfe86842011-10-28 14:13:29 +02008178 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04008179 btrfs_i_size_write(inode, 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07008180 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04008181
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008182 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
8183 if (err)
8184 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02008185 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008186 new_root->root_key.objectid, err);
8187
Yan, Zheng76dda932009-09-21 16:00:26 -04008188 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04008189
Yan, Zheng76dda932009-09-21 16:00:26 -04008190 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07008191 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04008192}
8193
Chris Mason39279cc2007-06-12 06:35:45 -04008194struct inode *btrfs_alloc_inode(struct super_block *sb)
8195{
8196 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008197 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008198
8199 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
8200 if (!ei)
8201 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008202
8203 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008204 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04008205 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04008206 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04008207 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008208 ei->delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08008209 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008210 ei->disk_i_size = 0;
8211 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04008212 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008213 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08008214 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008215 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06008216 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008217
Josef Bacik9e0baf62011-07-15 15:16:44 +00008218 spin_lock_init(&ei->lock);
8219 ei->outstanding_extents = 0;
8220 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008221
Josef Bacik72ac3c02012-05-23 14:13:11 -04008222 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08008223 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008224
Miao Xie16cdcec2011-04-22 18:12:22 +08008225 ei->delayed_node = NULL;
8226
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008227 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02008228 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02008229 extent_io_tree_init(&ei->io_tree, &inode->i_data);
8230 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04008231 ei->io_tree.track_uptodate = 1;
8232 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05008233 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008234 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05008235 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008236 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008237 INIT_LIST_HEAD(&ei->delalloc_inodes);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008238 RB_CLEAR_NODE(&ei->rb_node);
8239
8240 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008241}
8242
Josef Bacikaaedb552013-10-11 14:44:09 -04008243#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
8244void btrfs_test_destroy_inode(struct inode *inode)
8245{
8246 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
8247 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8248}
8249#endif
8250
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008251static void btrfs_i_callback(struct rcu_head *head)
8252{
8253 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008254 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8255}
8256
Chris Mason39279cc2007-06-12 06:35:45 -04008257void btrfs_destroy_inode(struct inode *inode)
8258{
Chris Masone6dcd2d2008-07-17 12:53:50 -04008259 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008260 struct btrfs_root *root = BTRFS_I(inode)->root;
8261
Al Virob3d9b7a2012-06-09 13:51:19 -04008262 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04008263 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00008264 WARN_ON(BTRFS_I(inode)->outstanding_extents);
8265 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04008266 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
8267 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08008268 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04008269
Chris Mason5a3f23d2009-03-31 13:27:11 -04008270 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05008271 * This can happen where we create an inode, but somebody else also
8272 * created the same inode and we need to destroy the one we already
8273 * created.
8274 */
8275 if (!root)
8276 goto free;
8277
Josef Bacik8a35d952012-05-23 14:26:42 -04008278 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
8279 &BTRFS_I(inode)->runtime_flags)) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008280 btrfs_info(root->fs_info, "inode %llu still on the orphan list",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02008281 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04008282 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04008283 }
Josef Bacik7b128762008-07-24 12:17:14 -04008284
Chris Masond3977122009-01-05 21:25:51 -05008285 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008286 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
8287 if (!ordered)
8288 break;
8289 else {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008290 btrfs_err(root->fs_info, "found ordered extent %llu %llu on inode cleanup",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02008291 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008292 btrfs_remove_ordered_extent(inode, ordered);
8293 btrfs_put_ordered_extent(ordered);
8294 btrfs_put_ordered_extent(ordered);
8295 }
8296 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008297 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008298 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05008299free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008300 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04008301}
8302
Al Viro45321ac2010-06-07 13:43:19 -04008303int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04008304{
8305 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04008306
Naohiro Aota6379ef92013-06-06 09:56:34 +00008307 if (root == NULL)
8308 return 1;
8309
Liu Bofa6ac872013-02-20 14:10:23 +00008310 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02008311 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04008312 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04008313 else
Al Viro45321ac2010-06-07 13:43:19 -04008314 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04008315}
8316
Sven Wegener0ee0fda2008-07-30 16:54:26 -04008317static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04008318{
8319 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
8320
8321 inode_init_once(&ei->vfs_inode);
8322}
8323
8324void btrfs_destroy_cachep(void)
8325{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10008326 /*
8327 * Make sure all delayed rcu free inodes are flushed before we
8328 * destroy cache.
8329 */
8330 rcu_barrier();
Chris Mason39279cc2007-06-12 06:35:45 -04008331 if (btrfs_inode_cachep)
8332 kmem_cache_destroy(btrfs_inode_cachep);
8333 if (btrfs_trans_handle_cachep)
8334 kmem_cache_destroy(btrfs_trans_handle_cachep);
8335 if (btrfs_transaction_cachep)
8336 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008337 if (btrfs_path_cachep)
8338 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05008339 if (btrfs_free_space_cachep)
8340 kmem_cache_destroy(btrfs_free_space_cachep);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008341 if (btrfs_delalloc_work_cachep)
8342 kmem_cache_destroy(btrfs_delalloc_work_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008343}
8344
8345int btrfs_init_cachep(void)
8346{
David Sterba837e1972012-09-07 03:00:48 -06008347 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008348 sizeof(struct btrfs_inode), 0,
8349 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04008350 if (!btrfs_inode_cachep)
8351 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008352
David Sterba837e1972012-09-07 03:00:48 -06008353 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008354 sizeof(struct btrfs_trans_handle), 0,
8355 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008356 if (!btrfs_trans_handle_cachep)
8357 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008358
David Sterba837e1972012-09-07 03:00:48 -06008359 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008360 sizeof(struct btrfs_transaction), 0,
8361 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008362 if (!btrfs_transaction_cachep)
8363 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008364
David Sterba837e1972012-09-07 03:00:48 -06008365 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008366 sizeof(struct btrfs_path), 0,
8367 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008368 if (!btrfs_path_cachep)
8369 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008370
David Sterba837e1972012-09-07 03:00:48 -06008371 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05008372 sizeof(struct btrfs_free_space), 0,
8373 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
8374 if (!btrfs_free_space_cachep)
8375 goto fail;
8376
Miao Xie8ccf6f192012-10-25 09:28:04 +00008377 btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
8378 sizeof(struct btrfs_delalloc_work), 0,
8379 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
8380 NULL);
8381 if (!btrfs_delalloc_work_cachep)
8382 goto fail;
8383
Chris Mason39279cc2007-06-12 06:35:45 -04008384 return 0;
8385fail:
8386 btrfs_destroy_cachep();
8387 return -ENOMEM;
8388}
8389
8390static int btrfs_getattr(struct vfsmount *mnt,
8391 struct dentry *dentry, struct kstat *stat)
8392{
Miao Xiedf0af1a2013-01-29 10:11:59 +00008393 u64 delalloc_bytes;
Chris Mason39279cc2007-06-12 06:35:45 -04008394 struct inode *inode = dentry->d_inode;
David Sterbafadc0d82011-11-20 07:33:38 -05008395 u32 blocksize = inode->i_sb->s_blocksize;
8396
Chris Mason39279cc2007-06-12 06:35:45 -04008397 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04008398 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05008399 stat->blksize = PAGE_CACHE_SIZE;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008400
8401 spin_lock(&BTRFS_I(inode)->lock);
8402 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
8403 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05008404 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00008405 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04008406 return 0;
8407}
8408
Chris Masond3977122009-01-05 21:25:51 -05008409static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
8410 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04008411{
8412 struct btrfs_trans_handle *trans;
8413 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008414 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04008415 struct inode *new_inode = new_dentry->d_inode;
8416 struct inode *old_inode = old_dentry->d_inode;
8417 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008418 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008419 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04008420 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08008421 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008422
Li Zefan33345d012011-04-20 10:31:50 +08008423 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04008424 return -EPERM;
8425
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008426 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08008427 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05008428 return -EXDEV;
8429
Li Zefan33345d012011-04-20 10:31:50 +08008430 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
8431 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008432 return -ENOTEMPTY;
8433
Chris Mason39279cc2007-06-12 06:35:45 -04008434 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008435 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04008436 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05008437
8438
8439 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04008440 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05008441 new_dentry->d_name.name,
8442 new_dentry->d_name.len);
8443
8444 if (ret) {
8445 if (ret == -EEXIST) {
8446 /* we shouldn't get
8447 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05308448 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05008449 return ret;
8450 }
8451 } else {
8452 /* maybe -EOVERFLOW */
8453 return ret;
8454 }
8455 }
8456 ret = 0;
8457
Chris Mason5a3f23d2009-03-31 13:27:11 -04008458 /*
Chris Mason8d875f92014-08-12 10:47:42 -07008459 * we're using rename to replace one file with another. Start IO on it
8460 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04008461 */
Chris Mason8d875f92014-08-12 10:47:42 -07008462 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04008463 filemap_flush(old_inode->i_mapping);
8464
Yan, Zheng76dda932009-09-21 16:00:26 -04008465 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08008466 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008467 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008468 /*
8469 * We want to reserve the absolute worst case amount of items. So if
8470 * both inodes are subvols and we need to unlink them then that would
8471 * require 4 item modifications, but if they are both normal inodes it
8472 * would require 5 item modifications, so we'll assume their normal
8473 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
8474 * should cover the worst case number of items we'll modify.
8475 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04008476 trans = btrfs_start_transaction(root, 11);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008477 if (IS_ERR(trans)) {
8478 ret = PTR_ERR(trans);
8479 goto out_notrans;
8480 }
Chris Mason5f39d392007-10-15 16:14:19 -04008481
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008482 if (dest != root)
8483 btrfs_record_root_in_trans(trans, dest);
8484
Yan, Zhenga5719522009-09-24 09:17:31 -04008485 ret = btrfs_set_inode_index(new_dir, &index);
8486 if (ret)
8487 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008488
Miao Xie67de1172013-12-26 13:07:06 +08008489 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08008490 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008491 /* force full log commit if subvolume involved. */
Miao Xie995946d2014-04-02 19:51:06 +08008492 btrfs_set_log_full_commit(root->fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008493 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04008494 ret = btrfs_insert_inode_ref(trans, dest,
8495 new_dentry->d_name.name,
8496 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08008497 old_ino,
8498 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04008499 if (ret)
8500 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008501 /*
8502 * this is an ugly little race, but the rename is required
8503 * to make sure that if we crash, the inode is either at the
8504 * old name or the new one. pinning the log transaction lets
8505 * us make sure we don't allow a log commit to come in after
8506 * we unlink the name but before we add the new name back in.
8507 */
8508 btrfs_pin_log_trans(root);
8509 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04008510
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008511 inode_inc_iversion(old_dir);
8512 inode_inc_iversion(new_dir);
8513 inode_inc_iversion(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008514 old_dir->i_ctime = old_dir->i_mtime = ctime;
8515 new_dir->i_ctime = new_dir->i_mtime = ctime;
8516 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04008517
Chris Mason12fcfd22009-03-24 10:24:20 -04008518 if (old_dentry->d_parent != new_dentry->d_parent)
8519 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
8520
Li Zefan33345d012011-04-20 10:31:50 +08008521 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008522 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
8523 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
8524 old_dentry->d_name.name,
8525 old_dentry->d_name.len);
8526 } else {
Al Viro92986792011-03-04 17:14:37 +00008527 ret = __btrfs_unlink_inode(trans, root, old_dir,
8528 old_dentry->d_inode,
8529 old_dentry->d_name.name,
8530 old_dentry->d_name.len);
8531 if (!ret)
8532 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008533 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008534 if (ret) {
8535 btrfs_abort_transaction(trans, root, ret);
8536 goto out_fail;
8537 }
Chris Mason39279cc2007-06-12 06:35:45 -04008538
8539 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008540 inode_inc_iversion(new_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008541 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08008542 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008543 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
8544 root_objectid = BTRFS_I(new_inode)->location.objectid;
8545 ret = btrfs_unlink_subvol(trans, dest, new_dir,
8546 root_objectid,
8547 new_dentry->d_name.name,
8548 new_dentry->d_name.len);
8549 BUG_ON(new_inode->i_nlink == 0);
8550 } else {
8551 ret = btrfs_unlink_inode(trans, dest, new_dir,
8552 new_dentry->d_inode,
8553 new_dentry->d_name.name,
8554 new_dentry->d_name.len);
8555 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04008556 if (!ret && new_inode->i_nlink == 0)
Chris Masone02119d2008-09-05 16:13:11 -04008557 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008558 if (ret) {
8559 btrfs_abort_transaction(trans, root, ret);
8560 goto out_fail;
8561 }
Chris Mason39279cc2007-06-12 06:35:45 -04008562 }
Josef Bacikaec74772008-07-24 12:12:38 -04008563
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008564 ret = btrfs_add_link(trans, new_dir, old_inode,
8565 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04008566 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008567 if (ret) {
8568 btrfs_abort_transaction(trans, root, ret);
8569 goto out_fail;
8570 }
Chris Mason39279cc2007-06-12 06:35:45 -04008571
Miao Xie67de1172013-12-26 13:07:06 +08008572 if (old_inode->i_nlink == 1)
8573 BTRFS_I(old_inode)->dir_index = index;
8574
Li Zefan33345d012011-04-20 10:31:50 +08008575 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04008576 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00008577 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008578 btrfs_end_log_trans(root);
8579 }
Chris Mason39279cc2007-06-12 06:35:45 -04008580out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008581 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008582out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08008583 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008584 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008585
Chris Mason39279cc2007-06-12 06:35:45 -04008586 return ret;
8587}
8588
Miklos Szeredi80ace852014-07-23 15:15:32 +02008589static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
8590 struct inode *new_dir, struct dentry *new_dentry,
8591 unsigned int flags)
8592{
8593 if (flags & ~RENAME_NOREPLACE)
8594 return -EINVAL;
8595
8596 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry);
8597}
8598
Miao Xie8ccf6f192012-10-25 09:28:04 +00008599static void btrfs_run_delalloc_work(struct btrfs_work *work)
8600{
8601 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04008602 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008603
8604 delalloc_work = container_of(work, struct btrfs_delalloc_work,
8605 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04008606 inode = delalloc_work->inode;
8607 if (delalloc_work->wait) {
8608 btrfs_wait_ordered_range(inode, 0, (u64)-1);
8609 } else {
8610 filemap_flush(inode->i_mapping);
8611 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8612 &BTRFS_I(inode)->runtime_flags))
8613 filemap_flush(inode->i_mapping);
8614 }
Miao Xie8ccf6f192012-10-25 09:28:04 +00008615
8616 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -04008617 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008618 else
Josef Bacik9f23e282013-10-28 15:03:41 -04008619 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008620 complete(&delalloc_work->completion);
8621}
8622
8623struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
8624 int wait, int delay_iput)
8625{
8626 struct btrfs_delalloc_work *work;
8627
8628 work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
8629 if (!work)
8630 return NULL;
8631
8632 init_completion(&work->completion);
8633 INIT_LIST_HEAD(&work->list);
8634 work->inode = inode;
8635 work->wait = wait;
8636 work->delay_iput = delay_iput;
Liu Bo9e0af232014-08-15 23:36:53 +08008637 WARN_ON_ONCE(!inode);
8638 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
8639 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008640
8641 return work;
8642}
8643
8644void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
8645{
8646 wait_for_completion(&work->completion);
8647 kmem_cache_free(btrfs_delalloc_work_cachep, work);
8648}
8649
Chris Masond352ac62008-09-29 15:18:18 -04008650/*
8651 * some fairly slow code that needs optimization. This walks the list
8652 * of all the inodes with pending delalloc and forces them to disk.
8653 */
Miao Xie6c255e62014-03-06 13:55:01 +08008654static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
8655 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -04008656{
Chris Masonea8c2812008-08-04 23:17:27 -04008657 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008658 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008659 struct btrfs_delalloc_work *work, *next;
8660 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00008661 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008662 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04008663
Miao Xie8ccf6f192012-10-25 09:28:04 +00008664 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008665 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00008666
Miao Xie573bfb72014-03-06 13:55:03 +08008667 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00008668 spin_lock(&root->delalloc_lock);
8669 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008670 while (!list_empty(&splice)) {
8671 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04008672 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008673
Miao Xieeb73c1b2013-05-15 07:48:22 +00008674 list_move_tail(&binode->delalloc_inodes,
8675 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008676 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00008677 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00008678 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008679 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008680 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008681 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008682
8683 work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
8684 if (unlikely(!work)) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -04008685 if (delay_iput)
8686 btrfs_add_delayed_iput(inode);
8687 else
8688 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008689 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +08008690 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008691 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008692 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +08008693 btrfs_queue_work(root->fs_info->flush_workers,
8694 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +08008695 ret++;
8696 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +08008697 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008698 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00008699 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04008700 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008701 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04008702
Wang Shilonga1ecaab2014-04-02 19:53:32 +08008703out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00008704 list_for_each_entry_safe(work, next, &works, list) {
8705 list_del_init(&work->list);
8706 btrfs_wait_and_free_delalloc_work(work);
8707 }
8708
Miao Xieeb73c1b2013-05-15 07:48:22 +00008709 if (!list_empty_careful(&splice)) {
8710 spin_lock(&root->delalloc_lock);
8711 list_splice_tail(&splice, &root->delalloc_inodes);
8712 spin_unlock(&root->delalloc_lock);
8713 }
Miao Xie573bfb72014-03-06 13:55:03 +08008714 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00008715 return ret;
8716}
8717
8718int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
8719{
8720 int ret;
8721
Wang Shilong2c21b4d2014-01-14 19:42:20 +08008722 if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00008723 return -EROFS;
8724
Miao Xie6c255e62014-03-06 13:55:01 +08008725 ret = __start_delalloc_inodes(root, delay_iput, -1);
8726 if (ret > 0)
8727 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +00008728 /*
8729 * the filemap_flush will queue IO into the worker threads, but
Chris Mason8c8bee12008-09-29 11:19:10 -04008730 * we have to make sure the IO is actually started and that
8731 * ordered extents get created before we return
8732 */
8733 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05008734 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05008735 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04008736 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05008737 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
8738 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04008739 }
8740 atomic_dec(&root->fs_info->async_submit_draining);
Miao Xieeb73c1b2013-05-15 07:48:22 +00008741 return ret;
8742}
8743
Miao Xie6c255e62014-03-06 13:55:01 +08008744int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
8745 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +00008746{
8747 struct btrfs_root *root;
8748 struct list_head splice;
8749 int ret;
8750
Wang Shilong2c21b4d2014-01-14 19:42:20 +08008751 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00008752 return -EROFS;
8753
8754 INIT_LIST_HEAD(&splice);
8755
Miao Xie573bfb72014-03-06 13:55:03 +08008756 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00008757 spin_lock(&fs_info->delalloc_root_lock);
8758 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +08008759 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00008760 root = list_first_entry(&splice, struct btrfs_root,
8761 delalloc_root);
8762 root = btrfs_grab_fs_root(root);
8763 BUG_ON(!root);
8764 list_move_tail(&root->delalloc_root,
8765 &fs_info->delalloc_roots);
8766 spin_unlock(&fs_info->delalloc_root_lock);
8767
Miao Xie6c255e62014-03-06 13:55:01 +08008768 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +00008769 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +08008770 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +00008771 goto out;
8772
Miao Xie6c255e62014-03-06 13:55:01 +08008773 if (nr != -1) {
8774 nr -= ret;
8775 WARN_ON(nr < 0);
8776 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008777 spin_lock(&fs_info->delalloc_root_lock);
8778 }
8779 spin_unlock(&fs_info->delalloc_root_lock);
8780
Miao Xie6c255e62014-03-06 13:55:01 +08008781 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +00008782 atomic_inc(&fs_info->async_submit_draining);
8783 while (atomic_read(&fs_info->nr_async_submits) ||
8784 atomic_read(&fs_info->async_delalloc_pages)) {
8785 wait_event(fs_info->async_submit_wait,
8786 (atomic_read(&fs_info->nr_async_submits) == 0 &&
8787 atomic_read(&fs_info->async_delalloc_pages) == 0));
8788 }
8789 atomic_dec(&fs_info->async_submit_draining);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008790out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00008791 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00008792 spin_lock(&fs_info->delalloc_root_lock);
8793 list_splice_tail(&splice, &fs_info->delalloc_roots);
8794 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008795 }
Miao Xie573bfb72014-03-06 13:55:03 +08008796 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008797 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04008798}
8799
Chris Mason39279cc2007-06-12 06:35:45 -04008800static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
8801 const char *symname)
8802{
8803 struct btrfs_trans_handle *trans;
8804 struct btrfs_root *root = BTRFS_I(dir)->root;
8805 struct btrfs_path *path;
8806 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05008807 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04008808 int err;
8809 int drop_inode = 0;
8810 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05308811 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008812 int name_len;
8813 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04008814 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04008815 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04008816 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04008817
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01008818 name_len = strlen(symname);
Chris Mason39279cc2007-06-12 06:35:45 -04008819 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
8820 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05008821
Josef Bacik9ed74f22009-09-11 16:12:44 -04008822 /*
8823 * 2 items for inode item and ref
8824 * 2 items for dir items
8825 * 1 item for xattr if selinux is on
8826 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04008827 trans = btrfs_start_transaction(root, 5);
8828 if (IS_ERR(trans))
8829 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05008830
Li Zefan581bb052011-04-20 10:06:11 +08008831 err = btrfs_find_free_ino(root, &objectid);
8832 if (err)
8833 goto out_unlock;
8834
Josef Bacikaec74772008-07-24 12:12:38 -04008835 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08008836 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04008837 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008838 if (IS_ERR(inode)) {
8839 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008840 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008841 }
Chris Mason39279cc2007-06-12 06:35:45 -04008842
Casey Schauflerad19db72011-12-15 10:09:07 -05008843 /*
8844 * If the active LSM wants to access the inode during
8845 * d_instantiate it needs these. Smack checks to see
8846 * if the filesystem supports xattrs by looking at the
8847 * ops vector.
8848 */
8849 inode->i_fop = &btrfs_file_operations;
8850 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07008851 inode->i_mapping->a_ops = &btrfs_aops;
8852 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
8853 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
8854
8855 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
8856 if (err)
8857 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05008858
Josef Bacika1b075d2010-11-19 20:36:11 +00008859 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04008860 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07008861 goto out_unlock_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008862
8863 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07008864 if (!path) {
8865 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -07008866 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -07008867 }
Li Zefan33345d012011-04-20 10:31:50 +08008868 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008869 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02008870 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04008871 datasize = btrfs_file_extent_calc_inline_size(name_len);
8872 err = btrfs_insert_empty_item(trans, root, path, &key,
8873 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04008874 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +00008875 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -07008876 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -04008877 }
Chris Mason5f39d392007-10-15 16:14:19 -04008878 leaf = path->nodes[0];
8879 ei = btrfs_item_ptr(leaf, path->slots[0],
8880 struct btrfs_file_extent_item);
8881 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
8882 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04008883 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04008884 btrfs_set_file_extent_encryption(leaf, ei, 0);
8885 btrfs_set_file_extent_compression(leaf, ei, 0);
8886 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
8887 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
8888
Chris Mason39279cc2007-06-12 06:35:45 -04008889 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04008890 write_extent_buffer(leaf, symname, ptr, name_len);
8891 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04008892 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04008893
Chris Mason39279cc2007-06-12 06:35:45 -04008894 inode->i_op = &btrfs_symlink_inode_operations;
8895 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04008896 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04008897 inode_set_bytes(inode, name_len);
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01008898 btrfs_i_size_write(inode, name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -04008899 err = btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07008900 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -04008901 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07008902 goto out_unlock_inode;
8903 }
8904
8905 unlock_new_inode(inode);
8906 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008907
8908out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008909 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04008910 if (drop_inode) {
8911 inode_dec_link_count(inode);
8912 iput(inode);
8913 }
Liu Bob53d3f52012-11-14 14:34:34 +00008914 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04008915 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07008916
8917out_unlock_inode:
8918 drop_inode = 1;
8919 unlock_new_inode(inode);
8920 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -04008921}
Chris Mason16432982008-04-10 10:23:21 -04008922
Josef Bacik0af3d002010-06-21 14:48:16 -04008923static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
8924 u64 start, u64 num_bytes, u64 min_size,
8925 loff_t actual_len, u64 *alloc_hint,
8926 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04008927{
Josef Bacik5dc562c2012-08-17 13:14:17 -04008928 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
8929 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04008930 struct btrfs_root *root = BTRFS_I(inode)->root;
8931 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04008932 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00008933 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05008934 u64 cur_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04008935 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04008936 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04008937
Josef Bacik0af3d002010-06-21 14:48:16 -04008938 if (trans)
8939 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04008940 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008941 if (own_trans) {
8942 trans = btrfs_start_transaction(root, 3);
8943 if (IS_ERR(trans)) {
8944 ret = PTR_ERR(trans);
8945 break;
8946 }
Yan Zhengd899e052008-10-30 14:25:28 -04008947 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00008948
Chris Mason154ea282013-03-05 11:11:26 -05008949 cur_bytes = min(num_bytes, 256ULL * 1024 * 1024);
8950 cur_bytes = max(cur_bytes, min_size);
Josef Bacik00361582013-08-14 14:02:47 -04008951 ret = btrfs_reserve_extent(root, cur_bytes, min_size, 0,
Miao Xiee570fd22014-06-19 10:42:50 +08008952 *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008953 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008954 if (own_trans)
8955 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008956 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00008957 }
8958
Yan Zhengd899e052008-10-30 14:25:28 -04008959 ret = insert_reserved_file_extent(trans, inode,
8960 cur_offset, ins.objectid,
8961 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00008962 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04008963 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008964 if (ret) {
Josef Bacik857cc2f2013-10-07 15:21:08 -04008965 btrfs_free_reserved_extent(root, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +08008966 ins.offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008967 btrfs_abort_transaction(trans, root, ret);
8968 if (own_trans)
8969 btrfs_end_transaction(trans, root);
8970 break;
8971 }
Chris Masona1ed8352009-09-11 12:27:37 -04008972 btrfs_drop_extent_cache(inode, cur_offset,
8973 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008974
Josef Bacik5dc562c2012-08-17 13:14:17 -04008975 em = alloc_extent_map();
8976 if (!em) {
8977 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
8978 &BTRFS_I(inode)->runtime_flags);
8979 goto next;
8980 }
8981
8982 em->start = cur_offset;
8983 em->orig_start = cur_offset;
8984 em->len = ins.offset;
8985 em->block_start = ins.objectid;
8986 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05008987 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04008988 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008989 em->bdev = root->fs_info->fs_devices->latest_bdev;
8990 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
8991 em->generation = trans->transid;
8992
8993 while (1) {
8994 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04008995 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04008996 write_unlock(&em_tree->lock);
8997 if (ret != -EEXIST)
8998 break;
8999 btrfs_drop_extent_cache(inode, cur_offset,
9000 cur_offset + ins.offset - 1,
9001 0);
9002 }
9003 free_extent_map(em);
9004next:
Yan Zhengd899e052008-10-30 14:25:28 -04009005 num_bytes -= ins.offset;
9006 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04009007 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009008
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009009 inode_inc_iversion(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04009010 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02009011 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04009012 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04009013 (actual_len > inode->i_size) &&
9014 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009015 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00009016 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009017 else
Josef Bacik55a61d12010-11-22 18:50:32 +00009018 i_size = cur_offset;
9019 i_size_write(inode, i_size);
9020 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009021 }
9022
Yan Zhengd899e052008-10-30 14:25:28 -04009023 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009024
9025 if (ret) {
9026 btrfs_abort_transaction(trans, root, ret);
9027 if (own_trans)
9028 btrfs_end_transaction(trans, root);
9029 break;
9030 }
Yan Zhengd899e052008-10-30 14:25:28 -04009031
Josef Bacik0af3d002010-06-21 14:48:16 -04009032 if (own_trans)
9033 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009034 }
Yan Zhengd899e052008-10-30 14:25:28 -04009035 return ret;
9036}
9037
Josef Bacik0af3d002010-06-21 14:48:16 -04009038int btrfs_prealloc_file_range(struct inode *inode, int mode,
9039 u64 start, u64 num_bytes, u64 min_size,
9040 loff_t actual_len, u64 *alloc_hint)
9041{
9042 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9043 min_size, actual_len, alloc_hint,
9044 NULL);
9045}
9046
9047int btrfs_prealloc_file_range_trans(struct inode *inode,
9048 struct btrfs_trans_handle *trans, int mode,
9049 u64 start, u64 num_bytes, u64 min_size,
9050 loff_t actual_len, u64 *alloc_hint)
9051{
9052 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9053 min_size, actual_len, alloc_hint, trans);
9054}
9055
Chris Masone6dcd2d2008-07-17 12:53:50 -04009056static int btrfs_set_page_dirty(struct page *page)
9057{
Chris Masone6dcd2d2008-07-17 12:53:50 -04009058 return __set_page_dirty_nobuffers(page);
9059}
9060
Al Viro10556cb2011-06-20 19:28:19 -04009061static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05009062{
Li Zefanb83cc962010-12-20 16:04:08 +08009063 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009064 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08009065
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009066 if (mask & MAY_WRITE &&
9067 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
9068 if (btrfs_root_readonly(root))
9069 return -EROFS;
9070 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
9071 return -EACCES;
9072 }
Al Viro2830ba72011-06-20 19:16:29 -04009073 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05009074}
Chris Mason39279cc2007-06-12 06:35:45 -04009075
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009076static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
9077{
9078 struct btrfs_trans_handle *trans;
9079 struct btrfs_root *root = BTRFS_I(dir)->root;
9080 struct inode *inode = NULL;
9081 u64 objectid;
9082 u64 index;
9083 int ret = 0;
9084
9085 /*
9086 * 5 units required for adding orphan entry
9087 */
9088 trans = btrfs_start_transaction(root, 5);
9089 if (IS_ERR(trans))
9090 return PTR_ERR(trans);
9091
9092 ret = btrfs_find_free_ino(root, &objectid);
9093 if (ret)
9094 goto out;
9095
9096 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
9097 btrfs_ino(dir), objectid, mode, &index);
9098 if (IS_ERR(inode)) {
9099 ret = PTR_ERR(inode);
9100 inode = NULL;
9101 goto out;
9102 }
9103
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009104 inode->i_fop = &btrfs_file_operations;
9105 inode->i_op = &btrfs_file_inode_operations;
9106
9107 inode->i_mapping->a_ops = &btrfs_aops;
9108 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
9109 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
9110
Chris Masonb0d5d102014-09-08 13:08:51 -07009111 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
9112 if (ret)
9113 goto out_inode;
9114
9115 ret = btrfs_update_inode(trans, root, inode);
9116 if (ret)
9117 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009118 ret = btrfs_orphan_add(trans, inode);
9119 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -07009120 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009121
Filipe Manana5762b5c2014-08-01 00:10:32 +01009122 /*
9123 * We set number of links to 0 in btrfs_new_inode(), and here we set
9124 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
9125 * through:
9126 *
9127 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
9128 */
9129 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -07009130 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009131 d_tmpfile(dentry, inode);
9132 mark_inode_dirty(inode);
9133
9134out:
9135 btrfs_end_transaction(trans, root);
9136 if (ret)
9137 iput(inode);
9138 btrfs_balance_delayed_items(root);
9139 btrfs_btree_balance_dirty(root);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009140 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -07009141
9142out_inode:
9143 unlock_new_inode(inode);
9144 goto out;
9145
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009146}
9147
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009148static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05009149 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04009150 .lookup = btrfs_lookup,
9151 .create = btrfs_create,
9152 .unlink = btrfs_unlink,
9153 .link = btrfs_link,
9154 .mkdir = btrfs_mkdir,
9155 .rmdir = btrfs_rmdir,
Miklos Szeredi80ace852014-07-23 15:15:32 +02009156 .rename2 = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -04009157 .symlink = btrfs_symlink,
9158 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04009159 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009160 .setxattr = btrfs_setxattr,
9161 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05009162 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009163 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05009164 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009165 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009166 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -07009167 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009168 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -04009169};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009170static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009171 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05009172 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009173 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009174 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -07009175 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04009176};
Yan, Zheng76dda932009-09-21 16:00:26 -04009177
Alexey Dobriyan828c0952009-10-01 15:43:56 -07009178static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009179 .llseek = generic_file_llseek,
9180 .read = generic_read_dir,
Al Viro9cdda8d2013-05-22 16:48:09 -04009181 .iterate = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04009182 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04009183#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04009184 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04009185#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04009186 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04009187 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04009188};
9189
Chris Masond1310b22008-01-24 16:13:08 -05009190static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04009191 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05009192 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04009193 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04009194 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04009195 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04009196 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05009197 .set_bit_hook = btrfs_set_bit_hook,
9198 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04009199 .merge_extent_hook = btrfs_merge_extent_hook,
9200 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04009201};
9202
Chris Mason35054392009-01-21 13:11:13 -05009203/*
9204 * btrfs doesn't support the bmap operation because swapfiles
9205 * use bmap to make a mapping of extents in the file. They assume
9206 * these extents won't change over the life of the file and they
9207 * use the bmap result to do IO directly to the drive.
9208 *
9209 * the btrfs bmap call would return logical addresses that aren't
9210 * suitable for IO and they also will change frequently as COW
9211 * operations happen. So, swapfile + btrfs == corruption.
9212 *
9213 * For now we're avoiding this by dropping bmap.
9214 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07009215static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04009216 .readpage = btrfs_readpage,
9217 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04009218 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05009219 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04009220 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04009221 .invalidatepage = btrfs_invalidatepage,
9222 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04009223 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02009224 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04009225};
9226
Alexey Dobriyan7f094102009-09-21 17:01:10 -07009227static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04009228 .readpage = btrfs_readpage,
9229 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04009230 .invalidatepage = btrfs_invalidatepage,
9231 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04009232};
9233
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009234static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009235 .getattr = btrfs_getattr,
9236 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009237 .setxattr = btrfs_setxattr,
9238 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05009239 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009240 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05009241 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05009242 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009243 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009244 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04009245 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04009246};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009247static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04009248 .getattr = btrfs_getattr,
9249 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05009250 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009251 .setxattr = btrfs_setxattr,
9252 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04009253 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009254 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009255 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009256 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04009257 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -04009258};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009259static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009260 .readlink = generic_readlink,
9261 .follow_link = page_follow_link_light,
9262 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00009263 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -05009264 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05009265 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05009266 .setxattr = btrfs_setxattr,
9267 .getxattr = btrfs_getxattr,
9268 .listxattr = btrfs_listxattr,
9269 .removexattr = btrfs_removexattr,
Josef Bacike41f9412012-03-26 09:46:47 -04009270 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04009271};
Yan, Zheng76dda932009-09-21 16:00:26 -04009272
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04009273const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04009274 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -04009275 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -04009276};