blob: ae98df67950ff083bb97f9b9d11d66443c6ea4c3 [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;
156 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
157
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);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500781 if (ret)
782 goto out_free_reserve;
Chris Mason771ed682008-11-06 22:02:51 -0500783
Chris Mason771ed682008-11-06 22:02:51 -0500784 /*
785 * clear dirty, set writeback and unlock the pages.
786 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400787 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400788 async_extent->start +
789 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400790 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
791 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400792 PAGE_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500793 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500794 async_extent->start,
795 async_extent->ram_size,
796 ins.objectid,
797 ins.offset, async_extent->pages,
798 async_extent->nr_pages);
Chris Mason771ed682008-11-06 22:02:51 -0500799 alloc_hint = ins.objectid + ins.offset;
800 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500801 if (ret)
802 goto out;
Chris Mason771ed682008-11-06 22:02:51 -0500803 cond_resched();
804 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100805 ret = 0;
806out:
807 return ret;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500808out_free_reserve:
Miao Xiee570fd22014-06-19 10:42:50 +0800809 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100810out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400811 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500812 async_extent->start +
813 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400814 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400815 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
816 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
817 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100818 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500819 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500820}
821
Josef Bacik4b46fce2010-05-23 11:00:55 -0400822static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
823 u64 num_bytes)
824{
825 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
826 struct extent_map *em;
827 u64 alloc_hint = 0;
828
829 read_lock(&em_tree->lock);
830 em = search_extent_mapping(em_tree, start, num_bytes);
831 if (em) {
832 /*
833 * if block start isn't an actual block number then find the
834 * first block in this inode and use that as a hint. If that
835 * block is also bogus then just don't worry about it.
836 */
837 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
838 free_extent_map(em);
839 em = search_extent_mapping(em_tree, 0, 0);
840 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
841 alloc_hint = em->block_start;
842 if (em)
843 free_extent_map(em);
844 } else {
845 alloc_hint = em->block_start;
846 free_extent_map(em);
847 }
848 }
849 read_unlock(&em_tree->lock);
850
851 return alloc_hint;
852}
853
Chris Mason771ed682008-11-06 22:02:51 -0500854/*
855 * when extent_io.c finds a delayed allocation range in the file,
856 * the call backs end up in this code. The basic idea is to
857 * allocate extents on disk for the range, and create ordered data structs
858 * in ram to track those extents.
859 *
860 * locked_page is the page that writepage had locked already. We use
861 * it to make sure we don't do extra locks or unlocks.
862 *
863 * *page_started is set to one if we unlock locked_page and do everything
864 * required to start IO on it. It may be clean and already done with
865 * IO when we return.
866 */
Josef Bacik00361582013-08-14 14:02:47 -0400867static noinline int cow_file_range(struct inode *inode,
868 struct page *locked_page,
869 u64 start, u64 end, int *page_started,
870 unsigned long *nr_written,
871 int unlock)
Chris Mason771ed682008-11-06 22:02:51 -0500872{
Josef Bacik00361582013-08-14 14:02:47 -0400873 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500874 u64 alloc_hint = 0;
875 u64 num_bytes;
876 unsigned long ram_size;
877 u64 disk_num_bytes;
878 u64 cur_alloc_size;
879 u64 blocksize = root->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500880 struct btrfs_key ins;
881 struct extent_map *em;
882 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
883 int ret = 0;
884
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400885 if (btrfs_is_free_space_inode(inode)) {
886 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500887 ret = -EINVAL;
888 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400889 }
Chris Mason771ed682008-11-06 22:02:51 -0500890
Qu Wenruofda28322013-02-26 08:10:22 +0000891 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500892 num_bytes = max(blocksize, num_bytes);
893 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500894
Chris Mason4cb53002011-05-24 15:35:30 -0400895 /* if this is a small write inside eof, kick off defrag */
Liu Bo4cb13e52012-03-29 09:57:45 -0400896 if (num_bytes < 64 * 1024 &&
897 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Josef Bacik00361582013-08-14 14:02:47 -0400898 btrfs_add_inode_defrag(NULL, inode);
Chris Mason4cb53002011-05-24 15:35:30 -0400899
Chris Mason771ed682008-11-06 22:02:51 -0500900 if (start == 0) {
901 /* lets try to make an inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400902 ret = cow_file_range_inline(root, inode, start, end, 0, 0,
903 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500904 if (ret == 0) {
Josef Bacikc2790a22013-07-29 11:20:47 -0400905 extent_clear_unlock_delalloc(inode, start, end, NULL,
906 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400907 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400908 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
909 PAGE_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000910
Chris Mason771ed682008-11-06 22:02:51 -0500911 *nr_written = *nr_written +
912 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
913 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500914 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100915 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100916 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500917 }
918 }
Chris Masonc8b97812008-10-29 14:49:59 -0400919
920 BUG_ON(disk_num_bytes >
David Sterba6c417612011-04-13 15:41:04 +0200921 btrfs_super_total_bytes(root->fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400922
Josef Bacik4b46fce2010-05-23 11:00:55 -0400923 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400924 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400925
Chris Masond3977122009-01-05 21:25:51 -0500926 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400927 unsigned long op;
928
Josef Bacik287a0ab2010-03-19 18:07:23 +0000929 cur_alloc_size = disk_num_bytes;
Josef Bacik00361582013-08-14 14:02:47 -0400930 ret = btrfs_reserve_extent(root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500931 root->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +0800932 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -0400933 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100934 goto out_unlock;
Chris Masond3977122009-01-05 21:25:51 -0500935
David Sterba172ddd62011-04-21 00:48:27 +0200936 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000937 if (!em) {
938 ret = -ENOMEM;
Liu Boace68ba2013-04-22 10:53:47 +0000939 goto out_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000940 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400941 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500942 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500943 ram_size = ins.offset;
944 em->len = ins.offset;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400945 em->mod_start = em->start;
946 em->mod_len = em->len;
Chris Masonc8b97812008-10-29 14:49:59 -0400947
Chris Masone6dcd2d2008-07-17 12:53:50 -0400948 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400949 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500950 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400951 em->ram_bytes = ram_size;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400952 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400953 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400954 em->generation = -1;
Chris Masonc8b97812008-10-29 14:49:59 -0400955
Chris Masond3977122009-01-05 21:25:51 -0500956 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400957 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400958 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400959 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400960 if (ret != -EEXIST) {
961 free_extent_map(em);
962 break;
963 }
964 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400965 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400966 }
Liu Boace68ba2013-04-22 10:53:47 +0000967 if (ret)
968 goto out_reserve;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400969
Chris Mason98d20f62008-04-14 09:46:10 -0400970 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400971 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -0500972 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +0000973 if (ret)
974 goto out_reserve;
Chris Masonc8b97812008-10-29 14:49:59 -0400975
Yan Zheng17d217f2008-12-12 10:03:38 -0500976 if (root->root_key.objectid ==
977 BTRFS_DATA_RELOC_TREE_OBJECTID) {
978 ret = btrfs_reloc_clone_csums(inode, start,
979 cur_alloc_size);
Josef Bacik00361582013-08-14 14:02:47 -0400980 if (ret)
Liu Boace68ba2013-04-22 10:53:47 +0000981 goto out_reserve;
Yan Zheng17d217f2008-12-12 10:03:38 -0500982 }
983
Chris Masond3977122009-01-05 21:25:51 -0500984 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -0400985 break;
Chris Masond3977122009-01-05 21:25:51 -0500986
Chris Masonc8b97812008-10-29 14:49:59 -0400987 /* we're not doing compressed IO, don't unlock the first
988 * page (which the caller expects to stay locked), don't
989 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -0400990 *
991 * Do set the Private2 bit so we know this page was properly
992 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -0400993 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400994 op = unlock ? PAGE_UNLOCK : 0;
995 op |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -0400996
Josef Bacikc2790a22013-07-29 11:20:47 -0400997 extent_clear_unlock_delalloc(inode, start,
998 start + ram_size - 1, locked_page,
999 EXTENT_LOCKED | EXTENT_DELALLOC,
1000 op);
Chris Masonc8b97812008-10-29 14:49:59 -04001001 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001002 num_bytes -= cur_alloc_size;
1003 alloc_hint = ins.objectid + ins.offset;
1004 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -04001005 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001006out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001007 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001008
Liu Boace68ba2013-04-22 10:53:47 +00001009out_reserve:
Miao Xiee570fd22014-06-19 10:42:50 +08001010 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001011out_unlock:
Josef Bacikc2790a22013-07-29 11:20:47 -04001012 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Josef Bacik151a41b2013-07-29 13:22:24 -04001013 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
1014 EXTENT_DELALLOC | EXTENT_DEFRAG,
1015 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
1016 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001017 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001018}
Chris Masonc8b97812008-10-29 14:49:59 -04001019
Chris Mason771ed682008-11-06 22:02:51 -05001020/*
1021 * work queue call back to started compression on a file and pages
1022 */
1023static noinline void async_cow_start(struct btrfs_work *work)
1024{
1025 struct async_cow *async_cow;
1026 int num_added = 0;
1027 async_cow = container_of(work, struct async_cow, work);
1028
1029 compress_file_range(async_cow->inode, async_cow->locked_page,
1030 async_cow->start, async_cow->end, async_cow,
1031 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001032 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001033 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001034 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001035 }
Chris Mason771ed682008-11-06 22:02:51 -05001036}
1037
1038/*
1039 * work queue call back to submit previously compressed pages
1040 */
1041static noinline void async_cow_submit(struct btrfs_work *work)
1042{
1043 struct async_cow *async_cow;
1044 struct btrfs_root *root;
1045 unsigned long nr_pages;
1046
1047 async_cow = container_of(work, struct async_cow, work);
1048
1049 root = async_cow->root;
1050 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
1051 PAGE_CACHE_SHIFT;
1052
Josef Bacik66657b32012-08-01 15:36:24 -04001053 if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) <
Liu Bo287082b2012-06-28 04:02:24 -06001054 5 * 1024 * 1024 &&
Chris Mason771ed682008-11-06 22:02:51 -05001055 waitqueue_active(&root->fs_info->async_submit_wait))
1056 wake_up(&root->fs_info->async_submit_wait);
1057
Chris Masond3977122009-01-05 21:25:51 -05001058 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001059 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001060}
Chris Masonc8b97812008-10-29 14:49:59 -04001061
Chris Mason771ed682008-11-06 22:02:51 -05001062static noinline void async_cow_free(struct btrfs_work *work)
1063{
1064 struct async_cow *async_cow;
1065 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001066 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001067 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001068 kfree(async_cow);
1069}
1070
1071static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1072 u64 start, u64 end, int *page_started,
1073 unsigned long *nr_written)
1074{
1075 struct async_cow *async_cow;
1076 struct btrfs_root *root = BTRFS_I(inode)->root;
1077 unsigned long nr_pages;
1078 u64 cur_end;
Liu Bo287082b2012-06-28 04:02:24 -06001079 int limit = 10 * 1024 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -05001080
Chris Masona3429ab2009-10-08 12:30:20 -04001081 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1082 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001083 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001084 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001085 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001086 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001087 async_cow->root = root;
1088 async_cow->locked_page = locked_page;
1089 async_cow->start = start;
1090
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001091 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
Chris Mason771ed682008-11-06 22:02:51 -05001092 cur_end = end;
1093 else
1094 cur_end = min(end, start + 512 * 1024 - 1);
1095
1096 async_cow->end = cur_end;
1097 INIT_LIST_HEAD(&async_cow->extents);
1098
Qu Wenruoafe3d242014-02-28 10:46:07 +08001099 btrfs_init_work(&async_cow->work, async_cow_start,
1100 async_cow_submit, async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001101
Chris Mason771ed682008-11-06 22:02:51 -05001102 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
1103 PAGE_CACHE_SHIFT;
1104 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
1105
Qu Wenruoafe3d242014-02-28 10:46:07 +08001106 btrfs_queue_work(root->fs_info->delalloc_workers,
1107 &async_cow->work);
Chris Mason771ed682008-11-06 22:02:51 -05001108
1109 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
1110 wait_event(root->fs_info->async_submit_wait,
1111 (atomic_read(&root->fs_info->async_delalloc_pages) <
1112 limit));
1113 }
1114
Chris Masond3977122009-01-05 21:25:51 -05001115 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -05001116 atomic_read(&root->fs_info->async_delalloc_pages)) {
1117 wait_event(root->fs_info->async_submit_wait,
1118 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1119 0));
1120 }
1121
1122 *nr_written += nr_pages;
1123 start = cur_end + 1;
1124 }
1125 *page_started = 1;
1126 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001127}
1128
Chris Masond3977122009-01-05 21:25:51 -05001129static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001130 u64 bytenr, u64 num_bytes)
1131{
1132 int ret;
1133 struct btrfs_ordered_sum *sums;
1134 LIST_HEAD(list);
1135
Yan Zheng07d400a2009-01-06 11:42:00 -05001136 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001137 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001138 if (ret == 0 && list_empty(&list))
1139 return 0;
1140
1141 while (!list_empty(&list)) {
1142 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1143 list_del(&sums->list);
1144 kfree(sums);
1145 }
1146 return 1;
1147}
1148
Chris Masond352ac62008-09-29 15:18:18 -04001149/*
1150 * when nowcow writeback call back. This checks for snapshots or COW copies
1151 * of the extents that exist in the file, and COWs the file as required.
1152 *
1153 * If no cow copies or snapshots exist, we write directly to the existing
1154 * blocks on disk
1155 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001156static noinline int run_delalloc_nocow(struct inode *inode,
1157 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001158 u64 start, u64 end, int *page_started, int force,
1159 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001160{
Chris Masonbe20aa92007-12-17 20:14:01 -05001161 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001162 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001163 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001164 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001165 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001166 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001167 u64 cow_start;
1168 u64 cur_offset;
1169 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001170 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001171 u64 disk_bytenr;
1172 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001173 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001174 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001175 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001176 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001177 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001178 int nocow;
1179 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001180 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001181 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001182
1183 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001184 if (!path) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001185 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1186 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001187 EXTENT_DO_ACCOUNTING |
1188 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001189 PAGE_CLEAR_DIRTY |
1190 PAGE_SET_WRITEBACK |
1191 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001192 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001193 }
Li Zefan82d59022011-04-20 10:33:24 +08001194
Liu Bo83eea1f2012-07-10 05:28:39 -06001195 nolock = btrfs_is_free_space_inode(inode);
Li Zefan82d59022011-04-20 10:33:24 +08001196
1197 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001198 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001199 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001200 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001201
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001202 if (IS_ERR(trans)) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001203 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1204 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001205 EXTENT_DO_ACCOUNTING |
1206 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001207 PAGE_CLEAR_DIRTY |
1208 PAGE_SET_WRITEBACK |
1209 PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001210 btrfs_free_path(path);
1211 return PTR_ERR(trans);
1212 }
1213
Josef Bacik74b21072011-04-13 12:02:53 -04001214 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001215
Yan Zheng80ff3852008-10-30 14:20:02 -04001216 cow_start = (u64)-1;
1217 cur_offset = start;
1218 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001219 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001220 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001221 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001222 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001223 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1224 leaf = path->nodes[0];
1225 btrfs_item_key_to_cpu(leaf, &found_key,
1226 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001227 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001228 found_key.type == BTRFS_EXTENT_DATA_KEY)
1229 path->slots[0]--;
1230 }
1231 check_prev = 0;
1232next_slot:
1233 leaf = path->nodes[0];
1234 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1235 ret = btrfs_next_leaf(root, path);
Josef Bacikd788a342013-10-25 16:55:08 -04001236 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001237 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001238 if (ret > 0)
1239 break;
1240 leaf = path->nodes[0];
1241 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001242
Yan Zheng80ff3852008-10-30 14:20:02 -04001243 nocow = 0;
1244 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001245 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001246 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001247
Li Zefan33345d012011-04-20 10:31:50 +08001248 if (found_key.objectid > ino ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001249 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1250 found_key.offset > end)
1251 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001252
Yan Zheng80ff3852008-10-30 14:20:02 -04001253 if (found_key.offset > cur_offset) {
1254 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001255 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001256 goto out_check;
1257 }
Chris Masonc31f8832008-01-08 15:46:31 -05001258
Yan Zheng80ff3852008-10-30 14:20:02 -04001259 fi = btrfs_item_ptr(leaf, path->slots[0],
1260 struct btrfs_file_extent_item);
1261 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001262
Josef Bacikcc95bef2013-04-04 14:31:27 -04001263 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001264 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1265 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001266 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001267 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001268 extent_end = found_key.offset +
1269 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001270 disk_num_bytes =
1271 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001272 if (extent_end <= start) {
1273 path->slots[0]++;
1274 goto next_slot;
1275 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001276 if (disk_bytenr == 0)
1277 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001278 if (btrfs_file_extent_compression(leaf, fi) ||
1279 btrfs_file_extent_encryption(leaf, fi) ||
1280 btrfs_file_extent_other_encoding(leaf, fi))
1281 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001282 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1283 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001284 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001285 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001286 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001287 found_key.offset -
1288 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001289 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001290 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001291 disk_bytenr += cur_offset - found_key.offset;
1292 num_bytes = min(end + 1, extent_end) - cur_offset;
1293 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001294 * if there are pending snapshots for this root,
1295 * we fall into common COW way.
1296 */
1297 if (!nolock) {
1298 err = btrfs_start_nocow_write(root);
1299 if (!err)
1300 goto out_check;
1301 }
1302 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001303 * force cow if csum exists in the range.
1304 * this ensure that csum for a given extent are
1305 * either valid or do not exist.
1306 */
1307 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1308 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001309 nocow = 1;
1310 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1311 extent_end = found_key.offset +
Chris Mason514ac8a2014-01-03 21:07:00 -08001312 btrfs_file_extent_inline_len(leaf,
1313 path->slots[0], fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001314 extent_end = ALIGN(extent_end, root->sectorsize);
1315 } else {
1316 BUG_ON(1);
1317 }
1318out_check:
1319 if (extent_end <= start) {
1320 path->slots[0]++;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001321 if (!nolock && nocow)
1322 btrfs_end_nocow_write(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001323 goto next_slot;
1324 }
1325 if (!nocow) {
1326 if (cow_start == (u64)-1)
1327 cow_start = cur_offset;
1328 cur_offset = extent_end;
1329 if (cur_offset > end)
1330 break;
1331 path->slots[0]++;
1332 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001333 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001334
David Sterbab3b4aa72011-04-21 01:20:15 +02001335 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001336 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001337 ret = cow_file_range(inode, locked_page,
1338 cow_start, found_key.offset - 1,
1339 page_started, nr_written, 1);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001340 if (ret) {
1341 if (!nolock && nocow)
1342 btrfs_end_nocow_write(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001343 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001344 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001345 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001346 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001347
Yan Zhengd899e052008-10-30 14:25:28 -04001348 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1349 struct extent_map *em;
1350 struct extent_map_tree *em_tree;
1351 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001352 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001353 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001354 em->start = cur_offset;
Josef Bacik70c8a912012-10-11 16:54:30 -04001355 em->orig_start = found_key.offset - extent_offset;
Yan Zhengd899e052008-10-30 14:25:28 -04001356 em->len = num_bytes;
1357 em->block_len = num_bytes;
1358 em->block_start = disk_bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05001359 em->orig_block_len = disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001360 em->ram_bytes = ram_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04001361 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001362 em->mod_start = em->start;
1363 em->mod_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04001364 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacikb11e2342012-12-03 10:58:15 -05001365 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001366 em->generation = -1;
Yan Zhengd899e052008-10-30 14:25:28 -04001367 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001368 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001369 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001370 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001371 if (ret != -EEXIST) {
1372 free_extent_map(em);
1373 break;
1374 }
1375 btrfs_drop_extent_cache(inode, em->start,
1376 em->start + em->len - 1, 0);
1377 }
1378 type = BTRFS_ORDERED_PREALLOC;
1379 } else {
1380 type = BTRFS_ORDERED_NOCOW;
1381 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001382
1383 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001384 num_bytes, num_bytes, type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001385 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001386
Yan, Zhengefa56462010-05-16 10:49:59 -04001387 if (root->root_key.objectid ==
1388 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1389 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1390 num_bytes);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001391 if (ret) {
1392 if (!nolock && nocow)
1393 btrfs_end_nocow_write(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001394 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001395 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001396 }
1397
Josef Bacikc2790a22013-07-29 11:20:47 -04001398 extent_clear_unlock_delalloc(inode, cur_offset,
1399 cur_offset + num_bytes - 1,
1400 locked_page, EXTENT_LOCKED |
1401 EXTENT_DELALLOC, PAGE_UNLOCK |
1402 PAGE_SET_PRIVATE2);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001403 if (!nolock && nocow)
1404 btrfs_end_nocow_write(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001405 cur_offset = extent_end;
1406 if (cur_offset > end)
1407 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001408 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001409 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001410
Josef Bacik17ca04a2012-05-31 15:58:55 -04001411 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001412 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001413 cur_offset = end;
1414 }
1415
Yan Zheng80ff3852008-10-30 14:20:02 -04001416 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001417 ret = cow_file_range(inode, locked_page, cow_start, end,
1418 page_started, nr_written, 1);
Josef Bacikd788a342013-10-25 16:55:08 -04001419 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001420 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001421 }
1422
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001423error:
Miao Xiea698d0752012-09-20 01:51:59 -06001424 err = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001425 if (!ret)
1426 ret = err;
1427
Josef Bacik17ca04a2012-05-31 15:58:55 -04001428 if (ret && cur_offset < end)
Josef Bacikc2790a22013-07-29 11:20:47 -04001429 extent_clear_unlock_delalloc(inode, cur_offset, end,
1430 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001431 EXTENT_DELALLOC | EXTENT_DEFRAG |
1432 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1433 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001434 PAGE_SET_WRITEBACK |
1435 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001436 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001437 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001438}
1439
Chris Masond352ac62008-09-29 15:18:18 -04001440/*
1441 * extent_io.c call back to do delayed allocation processing
1442 */
Chris Masonc8b97812008-10-29 14:49:59 -04001443static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001444 u64 start, u64 end, int *page_started,
1445 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001446{
Chris Masonbe20aa92007-12-17 20:14:01 -05001447 int ret;
Chris Mason7f366cf2009-03-12 20:12:45 -04001448 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona2135012008-06-25 16:01:30 -04001449
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001450 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) {
Chris Masonc8b97812008-10-29 14:49:59 -04001451 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001452 page_started, 1, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001453 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001454 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001455 page_started, 0, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001456 } else if (!btrfs_test_opt(root, COMPRESS) &&
1457 !(BTRFS_I(inode)->force_compress) &&
1458 !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS)) {
Chris Mason7f366cf2009-03-12 20:12:45 -04001459 ret = cow_file_range(inode, locked_page, start, end,
1460 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001461 } else {
1462 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1463 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001464 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001465 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001466 }
Chris Masonb888db22007-08-27 16:49:44 -04001467 return ret;
1468}
1469
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001470static void btrfs_split_extent_hook(struct inode *inode,
1471 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001472{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001473 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001474 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001475 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001476
Josef Bacik9e0baf62011-07-15 15:16:44 +00001477 spin_lock(&BTRFS_I(inode)->lock);
1478 BTRFS_I(inode)->outstanding_extents++;
1479 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001480}
1481
1482/*
1483 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1484 * extents so we can keep track of new extents that are just merged onto old
1485 * extents, such as when we are doing sequential writes, so we can properly
1486 * account for the metadata space we'll need.
1487 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001488static void btrfs_merge_extent_hook(struct inode *inode,
1489 struct extent_state *new,
1490 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001491{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001492 /* not delalloc, ignore it */
1493 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001494 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001495
Josef Bacik9e0baf62011-07-15 15:16:44 +00001496 spin_lock(&BTRFS_I(inode)->lock);
1497 BTRFS_I(inode)->outstanding_extents--;
1498 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001499}
1500
Miao Xieeb73c1b2013-05-15 07:48:22 +00001501static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1502 struct inode *inode)
1503{
1504 spin_lock(&root->delalloc_lock);
1505 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1506 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1507 &root->delalloc_inodes);
1508 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1509 &BTRFS_I(inode)->runtime_flags);
1510 root->nr_delalloc_inodes++;
1511 if (root->nr_delalloc_inodes == 1) {
1512 spin_lock(&root->fs_info->delalloc_root_lock);
1513 BUG_ON(!list_empty(&root->delalloc_root));
1514 list_add_tail(&root->delalloc_root,
1515 &root->fs_info->delalloc_roots);
1516 spin_unlock(&root->fs_info->delalloc_root_lock);
1517 }
1518 }
1519 spin_unlock(&root->delalloc_lock);
1520}
1521
1522static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1523 struct inode *inode)
1524{
1525 spin_lock(&root->delalloc_lock);
1526 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1527 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1528 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1529 &BTRFS_I(inode)->runtime_flags);
1530 root->nr_delalloc_inodes--;
1531 if (!root->nr_delalloc_inodes) {
1532 spin_lock(&root->fs_info->delalloc_root_lock);
1533 BUG_ON(list_empty(&root->delalloc_root));
1534 list_del_init(&root->delalloc_root);
1535 spin_unlock(&root->fs_info->delalloc_root_lock);
1536 }
1537 }
1538 spin_unlock(&root->delalloc_lock);
1539}
1540
Chris Masond352ac62008-09-29 15:18:18 -04001541/*
1542 * extent_io.c set_bit_hook, used to track delayed allocation
1543 * bytes in this file, and to maintain the list of inodes that
1544 * have pending delalloc work to be done.
1545 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001546static void btrfs_set_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001547 struct extent_state *state, unsigned long *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001548{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001549
Chris Mason75eff682008-12-15 15:54:40 -05001550 /*
1551 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001552 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001553 * bit, which is only set or cleared with irqs on
1554 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001555 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001556 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001557 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001558 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001559
Josef Bacik9e0baf62011-07-15 15:16:44 +00001560 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001561 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001562 } else {
1563 spin_lock(&BTRFS_I(inode)->lock);
1564 BTRFS_I(inode)->outstanding_extents++;
1565 spin_unlock(&BTRFS_I(inode)->lock);
1566 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001567
Miao Xie963d6782013-01-29 10:10:51 +00001568 __percpu_counter_add(&root->fs_info->delalloc_bytes, len,
1569 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001570 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001571 BTRFS_I(inode)->delalloc_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001572 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001573 &BTRFS_I(inode)->runtime_flags))
1574 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001575 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001576 }
Chris Mason291d6732008-01-29 15:55:23 -05001577}
1578
Chris Masond352ac62008-09-29 15:18:18 -04001579/*
1580 * extent_io.c clear_bit_hook, see set_bit_hook for why
1581 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001582static void btrfs_clear_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001583 struct extent_state *state,
1584 unsigned long *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001585{
Chris Mason75eff682008-12-15 15:54:40 -05001586 /*
1587 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001588 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001589 * bit, which is only set or cleared with irqs on
1590 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001591 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001592 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001593 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001594 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001595
Josef Bacik9e0baf62011-07-15 15:16:44 +00001596 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001597 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001598 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1599 spin_lock(&BTRFS_I(inode)->lock);
1600 BTRFS_I(inode)->outstanding_extents--;
1601 spin_unlock(&BTRFS_I(inode)->lock);
1602 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001603
Josef Bacikb6d08f02013-09-27 14:57:43 -04001604 /*
1605 * We don't reserve metadata space for space cache inodes so we
1606 * don't need to call dellalloc_release_metadata if there is an
1607 * error.
1608 */
1609 if (*bits & EXTENT_DO_ACCOUNTING &&
1610 root != root->fs_info->tree_root)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001611 btrfs_delalloc_release_metadata(inode, len);
1612
Josef Bacik0cb59c92010-07-02 12:14:14 -04001613 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Josef Bacik7ee9e442013-06-21 16:37:03 -04001614 && do_list && !(state->state & EXTENT_NORESERVE))
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001615 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001616
Miao Xie963d6782013-01-29 10:10:51 +00001617 __percpu_counter_add(&root->fs_info->delalloc_bytes, -len,
1618 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001619 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001620 BTRFS_I(inode)->delalloc_bytes -= len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001621 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001622 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001623 &BTRFS_I(inode)->runtime_flags))
1624 btrfs_del_delalloc_inode(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001625 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001626 }
Chris Mason291d6732008-01-29 15:55:23 -05001627}
1628
Chris Masond352ac62008-09-29 15:18:18 -04001629/*
1630 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1631 * we don't create bios that span stripes or chunks
1632 */
David Woodhouse64a16702009-07-15 23:29:37 +01001633int btrfs_merge_bio_hook(int rw, struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001634 size_t size, struct bio *bio,
1635 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001636{
1637 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001638 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001639 u64 length = 0;
1640 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001641 int ret;
1642
Chris Mason771ed682008-11-06 22:02:51 -05001643 if (bio_flags & EXTENT_BIO_COMPRESSED)
1644 return 0;
1645
Kent Overstreet4f024f32013-10-11 15:44:27 -07001646 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001647 map_length = length;
David Woodhouse64a16702009-07-15 23:29:37 +01001648 ret = btrfs_map_block(root->fs_info, rw, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001649 &map_length, NULL, 0);
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001650 /* Will always return 0 with map_multi == NULL */
Jeff Mahoney3444a972011-10-03 23:23:13 -04001651 BUG_ON(ret < 0);
Chris Masond3977122009-01-05 21:25:51 -05001652 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001653 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001654 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001655}
1656
Chris Masond352ac62008-09-29 15:18:18 -04001657/*
1658 * in order to insert checksums into the metadata in large chunks,
1659 * we wait until bio submission time. All the pages in the bio are
1660 * checksummed and sums are attached onto the ordered extent record.
1661 *
1662 * At IO completion time the cums attached on the ordered extent record
1663 * are inserted into the btree
1664 */
Chris Masond3977122009-01-05 21:25:51 -05001665static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1666 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001667 unsigned long bio_flags,
1668 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001669{
Chris Mason065631f2008-02-20 12:07:25 -05001670 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001671 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001672
Chris Masond20f7042008-12-08 16:58:54 -05001673 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001674 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001675 return 0;
1676}
Chris Masone0156402008-04-16 11:15:20 -04001677
Chris Mason4a69a412008-11-06 22:03:00 -05001678/*
1679 * in order to insert checksums into the metadata in large chunks,
1680 * we wait until bio submission time. All the pages in the bio are
1681 * checksummed and sums are attached onto the ordered extent record.
1682 *
1683 * At IO completion time the cums attached on the ordered extent record
1684 * are inserted into the btree
1685 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001686static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001687 int mirror_num, unsigned long bio_flags,
1688 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001689{
1690 struct btrfs_root *root = BTRFS_I(inode)->root;
Stefan Behrens61891922012-11-05 18:51:52 +01001691 int ret;
1692
1693 ret = btrfs_map_bio(root, rw, bio, mirror_num, 1);
1694 if (ret)
1695 bio_endio(bio, ret);
1696 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001697}
1698
Chris Masond352ac62008-09-29 15:18:18 -04001699/*
Chris Masoncad321a2008-12-17 14:51:42 -05001700 * extent_io.c submission hook. This does the right thing for csum calculation
1701 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001702 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001703static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001704 int mirror_num, unsigned long bio_flags,
1705 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001706{
1707 struct btrfs_root *root = BTRFS_I(inode)->root;
1708 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001709 int skip_sum;
Jeff Mahoney04173412011-10-03 23:23:12 -04001710 int metadata = 0;
Josef Bacikb812ce22012-11-16 13:56:32 -05001711 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001712
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001713 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001714
Liu Bo83eea1f2012-07-10 05:28:39 -06001715 if (btrfs_is_free_space_inode(inode))
Jeff Mahoney04173412011-10-03 23:23:12 -04001716 metadata = 2;
1717
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001718 if (!(rw & REQ_WRITE)) {
Josef Bacik5fd02042012-05-02 14:00:54 -04001719 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1720 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001721 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001722
Chris Masond20f7042008-12-08 16:58:54 -05001723 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001724 ret = btrfs_submit_compressed_read(inode, bio,
1725 mirror_num,
1726 bio_flags);
1727 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001728 } else if (!skip_sum) {
1729 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1730 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001731 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001732 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001733 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001734 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001735 /* csum items have already been cloned */
1736 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1737 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001738 /* we're doing a write, do the async checksumming */
Stefan Behrens61891922012-11-05 18:51:52 +01001739 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001740 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001741 bio_flags, bio_offset,
1742 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001743 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001744 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001745 } else if (!skip_sum) {
1746 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1747 if (ret)
1748 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001749 }
1750
Chris Mason0b86a832008-03-24 15:01:56 -04001751mapit:
Stefan Behrens61891922012-11-05 18:51:52 +01001752 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
1753
1754out:
1755 if (ret < 0)
1756 bio_endio(bio, ret);
1757 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001758}
Chris Mason6885f302008-02-20 16:11:05 -05001759
Chris Masond352ac62008-09-29 15:18:18 -04001760/*
1761 * given a list of ordered sums record them in the inode. This happens
1762 * at IO completion time based on sums calculated at bio submission time.
1763 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001764static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001765 struct inode *inode, u64 file_offset,
1766 struct list_head *list)
1767{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001768 struct btrfs_ordered_sum *sum;
1769
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001770 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00001771 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05001772 btrfs_csum_file_blocks(trans,
1773 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00001774 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001775 }
1776 return 0;
1777}
1778
Josef Bacik2ac55d42010-02-03 19:33:23 +00001779int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1780 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001781{
Julia Lawall6c1500f2012-11-03 20:30:18 +00001782 WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04001783 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001784 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001785}
1786
Chris Masond352ac62008-09-29 15:18:18 -04001787/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001788struct btrfs_writepage_fixup {
1789 struct page *page;
1790 struct btrfs_work work;
1791};
1792
Christoph Hellwigb2950862008-12-02 09:54:17 -05001793static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001794{
1795 struct btrfs_writepage_fixup *fixup;
1796 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001797 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001798 struct page *page;
1799 struct inode *inode;
1800 u64 page_start;
1801 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001802 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001803
1804 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1805 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001806again:
Chris Mason247e7432008-07-17 12:53:51 -04001807 lock_page(page);
1808 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1809 ClearPageChecked(page);
1810 goto out_page;
1811 }
1812
1813 inode = page->mapping->host;
1814 page_start = page_offset(page);
1815 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1816
Josef Bacik2ac55d42010-02-03 19:33:23 +00001817 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001818 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001819
1820 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001821 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001822 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001823
1824 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1825 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001826 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1827 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001828 unlock_page(page);
1829 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001830 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04001831 goto again;
1832 }
Chris Mason247e7432008-07-17 12:53:51 -04001833
Jeff Mahoney87826df2012-02-15 16:23:57 +01001834 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
1835 if (ret) {
1836 mapping_set_error(page->mapping, ret);
1837 end_extent_writepage(page, ret, page_start, page_end);
1838 ClearPageChecked(page);
1839 goto out;
1840 }
1841
Josef Bacik2ac55d42010-02-03 19:33:23 +00001842 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001843 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001844 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04001845out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001846 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1847 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001848out_page:
1849 unlock_page(page);
1850 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08001851 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04001852}
1853
1854/*
1855 * There are a few paths in the higher layers of the kernel that directly
1856 * set the page dirty bit without asking the filesystem if it is a
1857 * good idea. This causes problems because we want to make sure COW
1858 * properly happens and the data=ordered rules are followed.
1859 *
Chris Masonc8b97812008-10-29 14:49:59 -04001860 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001861 * hasn't been properly setup for IO. We kick off an async process
1862 * to fix it up. The async helper will wait for ordered extents, set
1863 * the delalloc bit and make it safe to write the page.
1864 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001865static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001866{
1867 struct inode *inode = page->mapping->host;
1868 struct btrfs_writepage_fixup *fixup;
1869 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001870
Chris Mason8b62b722009-09-02 16:53:46 -04001871 /* this page is properly in the ordered list */
1872 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001873 return 0;
1874
1875 if (PageChecked(page))
1876 return -EAGAIN;
1877
1878 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1879 if (!fixup)
1880 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001881
Chris Mason247e7432008-07-17 12:53:51 -04001882 SetPageChecked(page);
1883 page_cache_get(page);
Qu Wenruodc6e3202014-02-28 10:46:14 +08001884 btrfs_init_work(&fixup->work, btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04001885 fixup->page = page;
Qu Wenruodc6e3202014-02-28 10:46:14 +08001886 btrfs_queue_work(root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001887 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04001888}
1889
Yan Zhengd899e052008-10-30 14:25:28 -04001890static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1891 struct inode *inode, u64 file_pos,
1892 u64 disk_bytenr, u64 disk_num_bytes,
1893 u64 num_bytes, u64 ram_bytes,
1894 u8 compression, u8 encryption,
1895 u16 other_encoding, int extent_type)
1896{
1897 struct btrfs_root *root = BTRFS_I(inode)->root;
1898 struct btrfs_file_extent_item *fi;
1899 struct btrfs_path *path;
1900 struct extent_buffer *leaf;
1901 struct btrfs_key ins;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00001902 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04001903 int ret;
1904
1905 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07001906 if (!path)
1907 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04001908
Chris Masona1ed8352009-09-11 12:27:37 -04001909 /*
1910 * we may be replacing one extent in the tree with another.
1911 * The new extent is pinned in the extent map, and we don't want
1912 * to drop it from the cache until it is completely in the btree.
1913 *
1914 * So, tell btrfs_drop_extents to leave this extent in the cache.
1915 * the caller is expected to unpin it and allow it to be merged
1916 * with the others.
1917 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00001918 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
1919 file_pos + num_bytes, NULL, 0,
1920 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001921 if (ret)
1922 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001923
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00001924 if (!extent_inserted) {
1925 ins.objectid = btrfs_ino(inode);
1926 ins.offset = file_pos;
1927 ins.type = BTRFS_EXTENT_DATA_KEY;
1928
1929 path->leave_spinning = 1;
1930 ret = btrfs_insert_empty_item(trans, root, path, &ins,
1931 sizeof(*fi));
1932 if (ret)
1933 goto out;
1934 }
Yan Zhengd899e052008-10-30 14:25:28 -04001935 leaf = path->nodes[0];
1936 fi = btrfs_item_ptr(leaf, path->slots[0],
1937 struct btrfs_file_extent_item);
1938 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1939 btrfs_set_file_extent_type(leaf, fi, extent_type);
1940 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1941 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1942 btrfs_set_file_extent_offset(leaf, fi, 0);
1943 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1944 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1945 btrfs_set_file_extent_compression(leaf, fi, compression);
1946 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1947 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04001948
Yan Zhengd899e052008-10-30 14:25:28 -04001949 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04001950 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04001951
1952 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04001953
1954 ins.objectid = disk_bytenr;
1955 ins.offset = disk_num_bytes;
1956 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001957 ret = btrfs_alloc_reserved_file_extent(trans, root,
1958 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08001959 btrfs_ino(inode), file_pos, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001960out:
Yan Zhengd899e052008-10-30 14:25:28 -04001961 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04001962
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001963 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04001964}
1965
Liu Bo38c227d2013-01-29 03:18:40 +00001966/* snapshot-aware defrag */
1967struct sa_defrag_extent_backref {
1968 struct rb_node node;
1969 struct old_sa_defrag_extent *old;
1970 u64 root_id;
1971 u64 inum;
1972 u64 file_pos;
1973 u64 extent_offset;
1974 u64 num_bytes;
1975 u64 generation;
1976};
1977
1978struct old_sa_defrag_extent {
1979 struct list_head list;
1980 struct new_sa_defrag_extent *new;
1981
1982 u64 extent_offset;
1983 u64 bytenr;
1984 u64 offset;
1985 u64 len;
1986 int count;
1987};
1988
1989struct new_sa_defrag_extent {
1990 struct rb_root root;
1991 struct list_head head;
1992 struct btrfs_path *path;
1993 struct inode *inode;
1994 u64 file_pos;
1995 u64 len;
1996 u64 bytenr;
1997 u64 disk_len;
1998 u8 compress_type;
1999};
2000
2001static int backref_comp(struct sa_defrag_extent_backref *b1,
2002 struct sa_defrag_extent_backref *b2)
2003{
2004 if (b1->root_id < b2->root_id)
2005 return -1;
2006 else if (b1->root_id > b2->root_id)
2007 return 1;
2008
2009 if (b1->inum < b2->inum)
2010 return -1;
2011 else if (b1->inum > b2->inum)
2012 return 1;
2013
2014 if (b1->file_pos < b2->file_pos)
2015 return -1;
2016 else if (b1->file_pos > b2->file_pos)
2017 return 1;
2018
2019 /*
2020 * [------------------------------] ===> (a range of space)
2021 * |<--->| |<---->| =============> (fs/file tree A)
2022 * |<---------------------------->| ===> (fs/file tree B)
2023 *
2024 * A range of space can refer to two file extents in one tree while
2025 * refer to only one file extent in another tree.
2026 *
2027 * So we may process a disk offset more than one time(two extents in A)
2028 * and locate at the same extent(one extent in B), then insert two same
2029 * backrefs(both refer to the extent in B).
2030 */
2031 return 0;
2032}
2033
2034static void backref_insert(struct rb_root *root,
2035 struct sa_defrag_extent_backref *backref)
2036{
2037 struct rb_node **p = &root->rb_node;
2038 struct rb_node *parent = NULL;
2039 struct sa_defrag_extent_backref *entry;
2040 int ret;
2041
2042 while (*p) {
2043 parent = *p;
2044 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2045
2046 ret = backref_comp(backref, entry);
2047 if (ret < 0)
2048 p = &(*p)->rb_left;
2049 else
2050 p = &(*p)->rb_right;
2051 }
2052
2053 rb_link_node(&backref->node, parent, p);
2054 rb_insert_color(&backref->node, root);
2055}
2056
2057/*
2058 * Note the backref might has changed, and in this case we just return 0.
2059 */
2060static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2061 void *ctx)
2062{
2063 struct btrfs_file_extent_item *extent;
2064 struct btrfs_fs_info *fs_info;
2065 struct old_sa_defrag_extent *old = ctx;
2066 struct new_sa_defrag_extent *new = old->new;
2067 struct btrfs_path *path = new->path;
2068 struct btrfs_key key;
2069 struct btrfs_root *root;
2070 struct sa_defrag_extent_backref *backref;
2071 struct extent_buffer *leaf;
2072 struct inode *inode = new->inode;
2073 int slot;
2074 int ret;
2075 u64 extent_offset;
2076 u64 num_bytes;
2077
2078 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
2079 inum == btrfs_ino(inode))
2080 return 0;
2081
2082 key.objectid = root_id;
2083 key.type = BTRFS_ROOT_ITEM_KEY;
2084 key.offset = (u64)-1;
2085
2086 fs_info = BTRFS_I(inode)->root->fs_info;
2087 root = btrfs_read_fs_root_no_name(fs_info, &key);
2088 if (IS_ERR(root)) {
2089 if (PTR_ERR(root) == -ENOENT)
2090 return 0;
2091 WARN_ON(1);
2092 pr_debug("inum=%llu, offset=%llu, root_id=%llu\n",
2093 inum, offset, root_id);
2094 return PTR_ERR(root);
2095 }
2096
2097 key.objectid = inum;
2098 key.type = BTRFS_EXTENT_DATA_KEY;
2099 if (offset > (u64)-1 << 32)
2100 key.offset = 0;
2101 else
2102 key.offset = offset;
2103
2104 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302105 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002106 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002107 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002108
2109 while (1) {
2110 cond_resched();
2111
2112 leaf = path->nodes[0];
2113 slot = path->slots[0];
2114
2115 if (slot >= btrfs_header_nritems(leaf)) {
2116 ret = btrfs_next_leaf(root, path);
2117 if (ret < 0) {
2118 goto out;
2119 } else if (ret > 0) {
2120 ret = 0;
2121 goto out;
2122 }
2123 continue;
2124 }
2125
2126 path->slots[0]++;
2127
2128 btrfs_item_key_to_cpu(leaf, &key, slot);
2129
2130 if (key.objectid > inum)
2131 goto out;
2132
2133 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2134 continue;
2135
2136 extent = btrfs_item_ptr(leaf, slot,
2137 struct btrfs_file_extent_item);
2138
2139 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2140 continue;
2141
Liu Boe68afa42013-07-01 22:13:26 +08002142 /*
2143 * 'offset' refers to the exact key.offset,
2144 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2145 * (key.offset - extent_offset).
2146 */
2147 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002148 continue;
2149
Liu Boe68afa42013-07-01 22:13:26 +08002150 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002151 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002152
Liu Bo38c227d2013-01-29 03:18:40 +00002153 if (extent_offset >= old->extent_offset + old->offset +
2154 old->len || extent_offset + num_bytes <=
2155 old->extent_offset + old->offset)
2156 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002157 break;
2158 }
2159
2160 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2161 if (!backref) {
2162 ret = -ENOENT;
2163 goto out;
2164 }
2165
2166 backref->root_id = root_id;
2167 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002168 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002169 backref->num_bytes = num_bytes;
2170 backref->extent_offset = extent_offset;
2171 backref->generation = btrfs_file_extent_generation(leaf, extent);
2172 backref->old = old;
2173 backref_insert(&new->root, backref);
2174 old->count++;
2175out:
2176 btrfs_release_path(path);
2177 WARN_ON(ret);
2178 return ret;
2179}
2180
2181static noinline bool record_extent_backrefs(struct btrfs_path *path,
2182 struct new_sa_defrag_extent *new)
2183{
2184 struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info;
2185 struct old_sa_defrag_extent *old, *tmp;
2186 int ret;
2187
2188 new->path = path;
2189
2190 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002191 ret = iterate_inodes_from_logical(old->bytenr +
2192 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002193 path, record_one_backref,
2194 old);
Josef Bacik4724b102013-11-05 11:11:40 -05002195 if (ret < 0 && ret != -ENOENT)
2196 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002197
2198 /* no backref to be processed for this extent */
2199 if (!old->count) {
2200 list_del(&old->list);
2201 kfree(old);
2202 }
2203 }
2204
2205 if (list_empty(&new->head))
2206 return false;
2207
2208 return true;
2209}
2210
2211static int relink_is_mergable(struct extent_buffer *leaf,
2212 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002213 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002214{
Liu Bo116e0022013-08-02 16:30:40 +08002215 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002216 return 0;
2217
2218 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2219 return 0;
2220
Liu Bo116e0022013-08-02 16:30:40 +08002221 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2222 return 0;
2223
2224 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002225 btrfs_file_extent_other_encoding(leaf, fi))
2226 return 0;
2227
2228 return 1;
2229}
2230
2231/*
2232 * Note the backref might has changed, and in this case we just return 0.
2233 */
2234static noinline int relink_extent_backref(struct btrfs_path *path,
2235 struct sa_defrag_extent_backref *prev,
2236 struct sa_defrag_extent_backref *backref)
2237{
2238 struct btrfs_file_extent_item *extent;
2239 struct btrfs_file_extent_item *item;
2240 struct btrfs_ordered_extent *ordered;
2241 struct btrfs_trans_handle *trans;
2242 struct btrfs_fs_info *fs_info;
2243 struct btrfs_root *root;
2244 struct btrfs_key key;
2245 struct extent_buffer *leaf;
2246 struct old_sa_defrag_extent *old = backref->old;
2247 struct new_sa_defrag_extent *new = old->new;
2248 struct inode *src_inode = new->inode;
2249 struct inode *inode;
2250 struct extent_state *cached = NULL;
2251 int ret = 0;
2252 u64 start;
2253 u64 len;
2254 u64 lock_start;
2255 u64 lock_end;
2256 bool merge = false;
2257 int index;
2258
2259 if (prev && prev->root_id == backref->root_id &&
2260 prev->inum == backref->inum &&
2261 prev->file_pos + prev->num_bytes == backref->file_pos)
2262 merge = true;
2263
2264 /* step 1: get root */
2265 key.objectid = backref->root_id;
2266 key.type = BTRFS_ROOT_ITEM_KEY;
2267 key.offset = (u64)-1;
2268
2269 fs_info = BTRFS_I(src_inode)->root->fs_info;
2270 index = srcu_read_lock(&fs_info->subvol_srcu);
2271
2272 root = btrfs_read_fs_root_no_name(fs_info, &key);
2273 if (IS_ERR(root)) {
2274 srcu_read_unlock(&fs_info->subvol_srcu, index);
2275 if (PTR_ERR(root) == -ENOENT)
2276 return 0;
2277 return PTR_ERR(root);
2278 }
Liu Bo38c227d2013-01-29 03:18:40 +00002279
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002280 if (btrfs_root_readonly(root)) {
2281 srcu_read_unlock(&fs_info->subvol_srcu, index);
2282 return 0;
2283 }
2284
Liu Bo38c227d2013-01-29 03:18:40 +00002285 /* step 2: get inode */
2286 key.objectid = backref->inum;
2287 key.type = BTRFS_INODE_ITEM_KEY;
2288 key.offset = 0;
2289
2290 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2291 if (IS_ERR(inode)) {
2292 srcu_read_unlock(&fs_info->subvol_srcu, index);
2293 return 0;
2294 }
2295
2296 srcu_read_unlock(&fs_info->subvol_srcu, index);
2297
2298 /* step 3: relink backref */
2299 lock_start = backref->file_pos;
2300 lock_end = backref->file_pos + backref->num_bytes - 1;
2301 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2302 0, &cached);
2303
2304 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2305 if (ordered) {
2306 btrfs_put_ordered_extent(ordered);
2307 goto out_unlock;
2308 }
2309
2310 trans = btrfs_join_transaction(root);
2311 if (IS_ERR(trans)) {
2312 ret = PTR_ERR(trans);
2313 goto out_unlock;
2314 }
2315
2316 key.objectid = backref->inum;
2317 key.type = BTRFS_EXTENT_DATA_KEY;
2318 key.offset = backref->file_pos;
2319
2320 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2321 if (ret < 0) {
2322 goto out_free_path;
2323 } else if (ret > 0) {
2324 ret = 0;
2325 goto out_free_path;
2326 }
2327
2328 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2329 struct btrfs_file_extent_item);
2330
2331 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2332 backref->generation)
2333 goto out_free_path;
2334
2335 btrfs_release_path(path);
2336
2337 start = backref->file_pos;
2338 if (backref->extent_offset < old->extent_offset + old->offset)
2339 start += old->extent_offset + old->offset -
2340 backref->extent_offset;
2341
2342 len = min(backref->extent_offset + backref->num_bytes,
2343 old->extent_offset + old->offset + old->len);
2344 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2345
2346 ret = btrfs_drop_extents(trans, root, inode, start,
2347 start + len, 1);
2348 if (ret)
2349 goto out_free_path;
2350again:
2351 key.objectid = btrfs_ino(inode);
2352 key.type = BTRFS_EXTENT_DATA_KEY;
2353 key.offset = start;
2354
Liu Boa09a0a72013-03-11 09:20:58 +00002355 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002356 if (merge) {
2357 struct btrfs_file_extent_item *fi;
2358 u64 extent_len;
2359 struct btrfs_key found_key;
2360
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002361 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002362 if (ret < 0)
2363 goto out_free_path;
2364
2365 path->slots[0]--;
2366 leaf = path->nodes[0];
2367 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2368
2369 fi = btrfs_item_ptr(leaf, path->slots[0],
2370 struct btrfs_file_extent_item);
2371 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2372
Liu Bo116e0022013-08-02 16:30:40 +08002373 if (extent_len + found_key.offset == start &&
2374 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002375 btrfs_set_file_extent_num_bytes(leaf, fi,
2376 extent_len + len);
2377 btrfs_mark_buffer_dirty(leaf);
2378 inode_add_bytes(inode, len);
2379
2380 ret = 1;
2381 goto out_free_path;
2382 } else {
2383 merge = false;
2384 btrfs_release_path(path);
2385 goto again;
2386 }
2387 }
2388
2389 ret = btrfs_insert_empty_item(trans, root, path, &key,
2390 sizeof(*extent));
2391 if (ret) {
2392 btrfs_abort_transaction(trans, root, ret);
2393 goto out_free_path;
2394 }
2395
2396 leaf = path->nodes[0];
2397 item = btrfs_item_ptr(leaf, path->slots[0],
2398 struct btrfs_file_extent_item);
2399 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2400 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2401 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2402 btrfs_set_file_extent_num_bytes(leaf, item, len);
2403 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2404 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2405 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2406 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2407 btrfs_set_file_extent_encryption(leaf, item, 0);
2408 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2409
2410 btrfs_mark_buffer_dirty(leaf);
2411 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002412 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002413
2414 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
2415 new->disk_len, 0,
2416 backref->root_id, backref->inum,
2417 new->file_pos, 0); /* start - extent_offset */
2418 if (ret) {
2419 btrfs_abort_transaction(trans, root, ret);
2420 goto out_free_path;
2421 }
2422
2423 ret = 1;
2424out_free_path:
2425 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002426 path->leave_spinning = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002427 btrfs_end_transaction(trans, root);
2428out_unlock:
2429 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2430 &cached, GFP_NOFS);
2431 iput(inode);
2432 return ret;
2433}
2434
Liu Bo6f519562013-10-29 10:45:05 +08002435static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2436{
2437 struct old_sa_defrag_extent *old, *tmp;
2438
2439 if (!new)
2440 return;
2441
2442 list_for_each_entry_safe(old, tmp, &new->head, list) {
2443 list_del(&old->list);
2444 kfree(old);
2445 }
2446 kfree(new);
2447}
2448
Liu Bo38c227d2013-01-29 03:18:40 +00002449static void relink_file_extents(struct new_sa_defrag_extent *new)
2450{
2451 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002452 struct sa_defrag_extent_backref *backref;
2453 struct sa_defrag_extent_backref *prev = NULL;
2454 struct inode *inode;
2455 struct btrfs_root *root;
2456 struct rb_node *node;
2457 int ret;
2458
2459 inode = new->inode;
2460 root = BTRFS_I(inode)->root;
2461
2462 path = btrfs_alloc_path();
2463 if (!path)
2464 return;
2465
2466 if (!record_extent_backrefs(path, new)) {
2467 btrfs_free_path(path);
2468 goto out;
2469 }
2470 btrfs_release_path(path);
2471
2472 while (1) {
2473 node = rb_first(&new->root);
2474 if (!node)
2475 break;
2476 rb_erase(node, &new->root);
2477
2478 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2479
2480 ret = relink_extent_backref(path, prev, backref);
2481 WARN_ON(ret < 0);
2482
2483 kfree(prev);
2484
2485 if (ret == 1)
2486 prev = backref;
2487 else
2488 prev = NULL;
2489 cond_resched();
2490 }
2491 kfree(prev);
2492
2493 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002494out:
Liu Bo6f519562013-10-29 10:45:05 +08002495 free_sa_defrag_extent(new);
2496
Liu Bo38c227d2013-01-29 03:18:40 +00002497 atomic_dec(&root->fs_info->defrag_running);
2498 wake_up(&root->fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002499}
2500
2501static struct new_sa_defrag_extent *
2502record_old_file_extents(struct inode *inode,
2503 struct btrfs_ordered_extent *ordered)
2504{
2505 struct btrfs_root *root = BTRFS_I(inode)->root;
2506 struct btrfs_path *path;
2507 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002508 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002509 struct new_sa_defrag_extent *new;
2510 int ret;
2511
2512 new = kmalloc(sizeof(*new), GFP_NOFS);
2513 if (!new)
2514 return NULL;
2515
2516 new->inode = inode;
2517 new->file_pos = ordered->file_offset;
2518 new->len = ordered->len;
2519 new->bytenr = ordered->start;
2520 new->disk_len = ordered->disk_len;
2521 new->compress_type = ordered->compress_type;
2522 new->root = RB_ROOT;
2523 INIT_LIST_HEAD(&new->head);
2524
2525 path = btrfs_alloc_path();
2526 if (!path)
2527 goto out_kfree;
2528
2529 key.objectid = btrfs_ino(inode);
2530 key.type = BTRFS_EXTENT_DATA_KEY;
2531 key.offset = new->file_pos;
2532
2533 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2534 if (ret < 0)
2535 goto out_free_path;
2536 if (ret > 0 && path->slots[0] > 0)
2537 path->slots[0]--;
2538
2539 /* find out all the old extents for the file range */
2540 while (1) {
2541 struct btrfs_file_extent_item *extent;
2542 struct extent_buffer *l;
2543 int slot;
2544 u64 num_bytes;
2545 u64 offset;
2546 u64 end;
2547 u64 disk_bytenr;
2548 u64 extent_offset;
2549
2550 l = path->nodes[0];
2551 slot = path->slots[0];
2552
2553 if (slot >= btrfs_header_nritems(l)) {
2554 ret = btrfs_next_leaf(root, path);
2555 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002556 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002557 else if (ret > 0)
2558 break;
2559 continue;
2560 }
2561
2562 btrfs_item_key_to_cpu(l, &key, slot);
2563
2564 if (key.objectid != btrfs_ino(inode))
2565 break;
2566 if (key.type != BTRFS_EXTENT_DATA_KEY)
2567 break;
2568 if (key.offset >= new->file_pos + new->len)
2569 break;
2570
2571 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2572
2573 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2574 if (key.offset + num_bytes < new->file_pos)
2575 goto next;
2576
2577 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2578 if (!disk_bytenr)
2579 goto next;
2580
2581 extent_offset = btrfs_file_extent_offset(l, extent);
2582
2583 old = kmalloc(sizeof(*old), GFP_NOFS);
2584 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002585 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002586
2587 offset = max(new->file_pos, key.offset);
2588 end = min(new->file_pos + new->len, key.offset + num_bytes);
2589
2590 old->bytenr = disk_bytenr;
2591 old->extent_offset = extent_offset;
2592 old->offset = offset - key.offset;
2593 old->len = end - offset;
2594 old->new = new;
2595 old->count = 0;
2596 list_add_tail(&old->list, &new->head);
2597next:
2598 path->slots[0]++;
2599 cond_resched();
2600 }
2601
2602 btrfs_free_path(path);
2603 atomic_inc(&root->fs_info->defrag_running);
2604
2605 return new;
2606
Liu Bo38c227d2013-01-29 03:18:40 +00002607out_free_path:
2608 btrfs_free_path(path);
2609out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002610 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002611 return NULL;
2612}
2613
Miao Xiee570fd22014-06-19 10:42:50 +08002614static void btrfs_release_delalloc_bytes(struct btrfs_root *root,
2615 u64 start, u64 len)
2616{
2617 struct btrfs_block_group_cache *cache;
2618
2619 cache = btrfs_lookup_block_group(root->fs_info, start);
2620 ASSERT(cache);
2621
2622 spin_lock(&cache->lock);
2623 cache->delalloc_bytes -= len;
2624 spin_unlock(&cache->lock);
2625
2626 btrfs_put_block_group(cache);
2627}
2628
Chris Masond352ac62008-09-29 15:18:18 -04002629/* as ordered data IO finishes, this gets called so we can finish
2630 * an ordered extent if the range of bytes in the file it covers are
2631 * fully written.
2632 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002633static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002634{
Josef Bacik5fd02042012-05-02 14:00:54 -04002635 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002636 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002637 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002638 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002639 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002640 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002641 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002642 int ret = 0;
2643 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002644 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002645 bool truncated = false;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002646
Liu Bo83eea1f2012-07-10 05:28:39 -06002647 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002648
Josef Bacik5fd02042012-05-02 14:00:54 -04002649 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2650 ret = -EIO;
2651 goto out;
2652 }
2653
Josef Bacik77cef2e2013-08-29 13:57:21 -04002654 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2655 truncated = true;
2656 logical_len = ordered_extent->truncated_len;
2657 /* Truncated the entire extent, don't bother adding */
2658 if (!logical_len)
2659 goto out;
2660 }
2661
Yan, Zhengc2167752009-11-12 09:34:21 +00002662 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002663 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Josef Bacik6c760c02012-11-09 10:53:21 -05002664 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2665 if (nolock)
2666 trans = btrfs_join_transaction_nolock(root);
2667 else
2668 trans = btrfs_join_transaction(root);
2669 if (IS_ERR(trans)) {
2670 ret = PTR_ERR(trans);
2671 trans = NULL;
2672 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002673 }
Josef Bacik6c760c02012-11-09 10:53:21 -05002674 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
2675 ret = btrfs_update_inode_fallback(trans, root, inode);
2676 if (ret) /* -ENOMEM or corruption */
2677 btrfs_abort_transaction(trans, root, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002678 goto out;
2679 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002680
Josef Bacik2ac55d42010-02-03 19:33:23 +00002681 lock_extent_bits(io_tree, ordered_extent->file_offset,
2682 ordered_extent->file_offset + ordered_extent->len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002683 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002684
Liu Bo38c227d2013-01-29 03:18:40 +00002685 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2686 ordered_extent->file_offset + ordered_extent->len - 1,
2687 EXTENT_DEFRAG, 1, cached_state);
2688 if (ret) {
2689 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05002690 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00002691 /* the inode is shared */
2692 new = record_old_file_extents(inode, ordered_extent);
2693
2694 clear_extent_bit(io_tree, ordered_extent->file_offset,
2695 ordered_extent->file_offset + ordered_extent->len - 1,
2696 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2697 }
2698
Josef Bacik0cb59c92010-07-02 12:14:14 -04002699 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002700 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002701 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002702 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002703 if (IS_ERR(trans)) {
2704 ret = PTR_ERR(trans);
2705 trans = NULL;
2706 goto out_unlock;
2707 }
Chris Masona79b7d42014-05-22 16:18:52 -07002708
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002709 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002710
Chris Masonc8b97812008-10-29 14:49:59 -04002711 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002712 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002713 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002714 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00002715 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002716 ordered_extent->file_offset,
2717 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002718 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002719 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04002720 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002721 ret = insert_reserved_file_extent(trans, inode,
2722 ordered_extent->file_offset,
2723 ordered_extent->start,
2724 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04002725 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08002726 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002727 BTRFS_FILE_EXTENT_REG);
Miao Xiee570fd22014-06-19 10:42:50 +08002728 if (!ret)
2729 btrfs_release_delalloc_bytes(root,
2730 ordered_extent->start,
2731 ordered_extent->disk_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002732 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002733 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2734 ordered_extent->file_offset, ordered_extent->len,
2735 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002736 if (ret < 0) {
2737 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002738 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002739 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002740
Chris Masone6dcd2d2008-07-17 12:53:50 -04002741 add_pending_csums(trans, inode, ordered_extent->file_offset,
2742 &ordered_extent->list);
2743
Josef Bacik6c760c02012-11-09 10:53:21 -05002744 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2745 ret = btrfs_update_inode_fallback(trans, root, inode);
2746 if (ret) { /* -ENOMEM or corruption */
2747 btrfs_abort_transaction(trans, root, ret);
2748 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002749 }
2750 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002751out_unlock:
2752 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2753 ordered_extent->file_offset +
2754 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00002755out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002756 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04002757 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002758 if (trans)
2759 btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002760
Josef Bacik77cef2e2013-08-29 13:57:21 -04002761 if (ret || truncated) {
2762 u64 start, end;
2763
2764 if (truncated)
2765 start = ordered_extent->file_offset + logical_len;
2766 else
2767 start = ordered_extent->file_offset;
2768 end = ordered_extent->file_offset + ordered_extent->len - 1;
2769 clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS);
2770
2771 /* Drop the cache for the part of the extent we didn't write. */
2772 btrfs_drop_extent_cache(inode, start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04002773
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002774 /*
2775 * If the ordered extent had an IOERR or something else went
2776 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04002777 * back to the allocator. We only free the extent in the
2778 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002779 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04002780 if ((ret || !logical_len) &&
2781 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002782 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
2783 btrfs_free_reserved_extent(root, ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08002784 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002785 }
2786
2787
Josef Bacik5fd02042012-05-02 14:00:54 -04002788 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002789 * This needs to be done to make sure anybody waiting knows we are done
2790 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002791 */
2792 btrfs_remove_ordered_extent(inode, ordered_extent);
2793
Liu Bo38c227d2013-01-29 03:18:40 +00002794 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08002795 if (new) {
2796 if (ret) {
2797 free_sa_defrag_extent(new);
2798 atomic_dec(&root->fs_info->defrag_running);
2799 } else {
2800 relink_file_extents(new);
2801 }
2802 }
Liu Bo38c227d2013-01-29 03:18:40 +00002803
Chris Masone6dcd2d2008-07-17 12:53:50 -04002804 /* once for us */
2805 btrfs_put_ordered_extent(ordered_extent);
2806 /* once for the tree */
2807 btrfs_put_ordered_extent(ordered_extent);
2808
Josef Bacik5fd02042012-05-02 14:00:54 -04002809 return ret;
2810}
2811
2812static void finish_ordered_fn(struct btrfs_work *work)
2813{
2814 struct btrfs_ordered_extent *ordered_extent;
2815 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2816 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002817}
2818
Christoph Hellwigb2950862008-12-02 09:54:17 -05002819static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04002820 struct extent_state *state, int uptodate)
2821{
Josef Bacik5fd02042012-05-02 14:00:54 -04002822 struct inode *inode = page->mapping->host;
2823 struct btrfs_root *root = BTRFS_I(inode)->root;
2824 struct btrfs_ordered_extent *ordered_extent = NULL;
Qu Wenruod458b052014-02-28 10:46:19 +08002825 struct btrfs_workqueue *workers;
Josef Bacik5fd02042012-05-02 14:00:54 -04002826
liubo1abe9b82011-03-24 11:18:59 +00002827 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2828
Chris Mason8b62b722009-09-02 16:53:46 -04002829 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002830 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2831 end - start + 1, uptodate))
2832 return 0;
2833
Qu Wenruofccb5d82014-02-28 10:46:10 +08002834 btrfs_init_work(&ordered_extent->work, finish_ordered_fn, NULL, NULL);
Josef Bacik5fd02042012-05-02 14:00:54 -04002835
Liu Bo83eea1f2012-07-10 05:28:39 -06002836 if (btrfs_is_free_space_inode(inode))
Qu Wenruofccb5d82014-02-28 10:46:10 +08002837 workers = root->fs_info->endio_freespace_worker;
Josef Bacik5fd02042012-05-02 14:00:54 -04002838 else
Qu Wenruofccb5d82014-02-28 10:46:10 +08002839 workers = root->fs_info->endio_write_workers;
2840 btrfs_queue_work(workers, &ordered_extent->work);
Josef Bacik5fd02042012-05-02 14:00:54 -04002841
2842 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04002843}
2844
Chris Masond352ac62008-09-29 15:18:18 -04002845/*
Chris Masond352ac62008-09-29 15:18:18 -04002846 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002847 * if there's a match, we allow the bio to finish. If not, the code in
2848 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002849 */
Miao Xiefacc8a222013-07-25 19:22:34 +08002850static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
2851 u64 phy_offset, struct page *page,
2852 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002853{
Miao Xie4eee4fa2012-12-21 09:17:45 +00002854 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04002855 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002856 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04002857 char *kaddr;
Chris Masonff79f812007-10-15 16:22:25 -04002858 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiefacc8a222013-07-25 19:22:34 +08002859 u32 csum_expected;
Chris Masonff79f812007-10-15 16:22:25 -04002860 u32 csum = ~(u32)0;
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002861 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
2862 DEFAULT_RATELIMIT_BURST);
Chris Masond1310b22008-01-24 16:13:08 -05002863
Chris Masond20f7042008-12-08 16:58:54 -05002864 if (PageChecked(page)) {
2865 ClearPageChecked(page);
2866 goto good;
2867 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002868
2869 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Jan Schmidt08d2f342011-05-04 16:18:50 +02002870 goto good;
Chris Masond20f7042008-12-08 16:58:54 -05002871
Yan Zheng17d217f2008-12-12 10:03:38 -05002872 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002873 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002874 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
2875 GFP_NOFS);
2876 return 0;
2877 }
2878
Miao Xiefacc8a222013-07-25 19:22:34 +08002879 phy_offset >>= inode->i_sb->s_blocksize_bits;
2880 csum_expected = *(((u32 *)io_bio->csum) + phy_offset);
Chris Masond3977122009-01-05 21:25:51 -05002881
Miao Xiefacc8a222013-07-25 19:22:34 +08002882 kaddr = kmap_atomic(page);
Liu Bob0496682013-03-14 14:57:45 +00002883 csum = btrfs_csum_data(kaddr + offset, csum, end - start + 1);
Chris Masonff79f812007-10-15 16:22:25 -04002884 btrfs_csum_final(csum, (char *)&csum);
Miao Xiefacc8a222013-07-25 19:22:34 +08002885 if (csum != csum_expected)
Chris Mason07157aa2007-08-30 08:50:51 -04002886 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002887
Cong Wang7ac687d2011-11-25 23:14:28 +08002888 kunmap_atomic(kaddr);
Chris Masond20f7042008-12-08 16:58:54 -05002889good:
Chris Mason07157aa2007-08-30 08:50:51 -04002890 return 0;
2891
2892zeroit:
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002893 if (__ratelimit(&_rs))
Miao Xiefacc8a222013-07-25 19:22:34 +08002894 btrfs_info(root->fs_info, "csum failed ino %llu off %llu csum %u expected csum %u",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002895 btrfs_ino(page->mapping->host), start, csum, csum_expected);
Chris Masondb945352007-10-15 16:15:53 -04002896 memset(kaddr + offset, 1, end - start + 1);
2897 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002898 kunmap_atomic(kaddr);
Miao Xiefacc8a222013-07-25 19:22:34 +08002899 if (csum_expected == 0)
Chris Mason3b951512008-04-17 11:29:12 -04002900 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04002901 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04002902}
Chris Masonb888db22007-08-27 16:49:44 -04002903
Yan, Zheng24bbcf042009-11-12 09:36:34 +00002904struct delayed_iput {
2905 struct list_head list;
2906 struct inode *inode;
2907};
2908
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002909/* JDM: If this is fs-wide, why can't we add a pointer to
2910 * btrfs_inode instead and avoid the allocation? */
Yan, Zheng24bbcf042009-11-12 09:36:34 +00002911void btrfs_add_delayed_iput(struct inode *inode)
2912{
2913 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2914 struct delayed_iput *delayed;
2915
2916 if (atomic_add_unless(&inode->i_count, -1, 1))
2917 return;
2918
2919 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2920 delayed->inode = inode;
2921
2922 spin_lock(&fs_info->delayed_iput_lock);
2923 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2924 spin_unlock(&fs_info->delayed_iput_lock);
2925}
2926
2927void btrfs_run_delayed_iputs(struct btrfs_root *root)
2928{
2929 LIST_HEAD(list);
2930 struct btrfs_fs_info *fs_info = root->fs_info;
2931 struct delayed_iput *delayed;
2932 int empty;
2933
2934 spin_lock(&fs_info->delayed_iput_lock);
2935 empty = list_empty(&fs_info->delayed_iputs);
2936 spin_unlock(&fs_info->delayed_iput_lock);
2937 if (empty)
2938 return;
2939
Yan, Zheng24bbcf042009-11-12 09:36:34 +00002940 spin_lock(&fs_info->delayed_iput_lock);
2941 list_splice_init(&fs_info->delayed_iputs, &list);
2942 spin_unlock(&fs_info->delayed_iput_lock);
2943
2944 while (!list_empty(&list)) {
2945 delayed = list_entry(list.next, struct delayed_iput, list);
2946 list_del(&delayed->list);
2947 iput(delayed->inode);
2948 kfree(delayed);
2949 }
Yan, Zheng24bbcf042009-11-12 09:36:34 +00002950}
2951
Yan, Zhengd68fc572010-05-16 10:49:58 -04002952/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08002953 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04002954 * files in the subvolume, it removes orphan item and frees block_rsv
2955 * structure.
2956 */
2957void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
2958 struct btrfs_root *root)
2959{
Josef Bacik90290e12011-12-02 15:44:12 -05002960 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002961 int ret;
2962
Josef Bacik8a35d952012-05-23 14:26:42 -04002963 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04002964 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
2965 return;
2966
Josef Bacik90290e12011-12-02 15:44:12 -05002967 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04002968 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05002969 spin_unlock(&root->orphan_lock);
2970 return;
2971 }
2972
2973 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
2974 spin_unlock(&root->orphan_lock);
2975 return;
2976 }
2977
2978 block_rsv = root->orphan_block_rsv;
2979 root->orphan_block_rsv = NULL;
2980 spin_unlock(&root->orphan_lock);
2981
Miao Xie27cdeb72014-04-02 19:51:05 +08002982 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
Yan, Zhengd68fc572010-05-16 10:49:58 -04002983 btrfs_root_refs(&root->root_item) > 0) {
2984 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
2985 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04002986 if (ret)
2987 btrfs_abort_transaction(trans, root, ret);
2988 else
Miao Xie27cdeb72014-04-02 19:51:05 +08002989 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
2990 &root->state);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002991 }
2992
Josef Bacik90290e12011-12-02 15:44:12 -05002993 if (block_rsv) {
2994 WARN_ON(block_rsv->size > 0);
2995 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002996 }
2997}
2998
2999/*
Josef Bacik7b128762008-07-24 12:17:14 -04003000 * This creates an orphan entry for the given inode in case something goes
3001 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003002 *
3003 * NOTE: caller of this function should reserve 5 units of metadata for
3004 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04003005 */
3006int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
3007{
3008 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003009 struct btrfs_block_rsv *block_rsv = NULL;
3010 int reserve = 0;
3011 int insert = 0;
3012 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003013
Yan, Zhengd68fc572010-05-16 10:49:58 -04003014 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06003015 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003016 if (!block_rsv)
3017 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04003018 }
3019
Yan, Zhengd68fc572010-05-16 10:49:58 -04003020 spin_lock(&root->orphan_lock);
3021 if (!root->orphan_block_rsv) {
3022 root->orphan_block_rsv = block_rsv;
3023 } else if (block_rsv) {
3024 btrfs_free_block_rsv(root, block_rsv);
3025 block_rsv = NULL;
3026 }
Josef Bacik7b128762008-07-24 12:17:14 -04003027
Josef Bacik8a35d952012-05-23 14:26:42 -04003028 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3029 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003030#if 0
3031 /*
3032 * For proper ENOSPC handling, we should do orphan
3033 * cleanup when mounting. But this introduces backward
3034 * compatibility issue.
3035 */
3036 if (!xchg(&root->orphan_item_inserted, 1))
3037 insert = 2;
3038 else
3039 insert = 1;
3040#endif
3041 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003042 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003043 }
Josef Bacik7b128762008-07-24 12:17:14 -04003044
Josef Bacik72ac3c02012-05-23 14:13:11 -04003045 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3046 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003047 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003048 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003049
Yan, Zhengd68fc572010-05-16 10:49:58 -04003050 /* grab metadata reservation from transaction handle */
3051 if (reserve) {
3052 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003053 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04003054 }
3055
3056 /* insert an orphan item to track this unlinked/truncated file */
3057 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08003058 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003059 if (ret) {
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003060 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003061 if (reserve) {
3062 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3063 &BTRFS_I(inode)->runtime_flags);
3064 btrfs_orphan_release_metadata(inode);
3065 }
3066 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003067 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3068 &BTRFS_I(inode)->runtime_flags);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003069 btrfs_abort_transaction(trans, root, ret);
3070 return ret;
3071 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003072 }
3073 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003074 }
3075
3076 /* insert an orphan item to track subvolume contains orphan files */
3077 if (insert >= 2) {
3078 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
3079 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003080 if (ret && ret != -EEXIST) {
3081 btrfs_abort_transaction(trans, root, ret);
3082 return ret;
3083 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003084 }
3085 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003086}
3087
3088/*
3089 * We have done the truncate/delete so we can go ahead and remove the orphan
3090 * item for this particular inode.
3091 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003092static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3093 struct inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003094{
3095 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003096 int delete_item = 0;
3097 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003098 int ret = 0;
3099
Yan, Zhengd68fc572010-05-16 10:49:58 -04003100 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003101 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3102 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003103 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003104
Josef Bacik72ac3c02012-05-23 14:13:11 -04003105 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3106 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003107 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003108 spin_unlock(&root->orphan_lock);
3109
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003110 if (delete_item) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003111 atomic_dec(&root->orphan_inodes);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003112 if (trans)
3113 ret = btrfs_del_orphan_item(trans, root,
3114 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04003115 }
Josef Bacik7b128762008-07-24 12:17:14 -04003116
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003117 if (release_rsv)
3118 btrfs_orphan_release_metadata(inode);
3119
Josef Bacik4ef31a42013-08-13 14:10:08 -04003120 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003121}
3122
3123/*
3124 * this cleans up any orphans that may be left on the list from the last use
3125 * of this root.
3126 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003127int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003128{
3129 struct btrfs_path *path;
3130 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003131 struct btrfs_key key, found_key;
3132 struct btrfs_trans_handle *trans;
3133 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003134 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003135 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3136
Yan, Zhengd68fc572010-05-16 10:49:58 -04003137 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003138 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003139
3140 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003141 if (!path) {
3142 ret = -ENOMEM;
3143 goto out;
3144 }
Josef Bacik7b128762008-07-24 12:17:14 -04003145 path->reada = -1;
3146
3147 key.objectid = BTRFS_ORPHAN_OBJECTID;
3148 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
3149 key.offset = (u64)-1;
3150
Josef Bacik7b128762008-07-24 12:17:14 -04003151 while (1) {
3152 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003153 if (ret < 0)
3154 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003155
3156 /*
3157 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003158 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003159 * find the key and see if we have stuff that matches
3160 */
3161 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003162 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003163 if (path->slots[0] == 0)
3164 break;
3165 path->slots[0]--;
3166 }
3167
3168 /* pull out the item */
3169 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003170 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3171
3172 /* make sure the item matches what we want */
3173 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3174 break;
3175 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
3176 break;
3177
3178 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003179 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003180
3181 /*
3182 * this is where we are basically btrfs_lookup, without the
3183 * crossing root thing. we store the inode number in the
3184 * offset of the orphan item.
3185 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003186
3187 if (found_key.offset == last_objectid) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003188 btrfs_err(root->fs_info,
3189 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003190 ret = -EINVAL;
3191 goto out;
3192 }
3193
3194 last_objectid = found_key.offset;
3195
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003196 found_key.objectid = found_key.offset;
3197 found_key.type = BTRFS_INODE_ITEM_KEY;
3198 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00003199 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303200 ret = PTR_ERR_OR_ZERO(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003201 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003202 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003203
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003204 if (ret == -ESTALE && root == root->fs_info->tree_root) {
3205 struct btrfs_root *dead_root;
3206 struct btrfs_fs_info *fs_info = root->fs_info;
3207 int is_dead_root = 0;
3208
3209 /*
3210 * this is an orphan in the tree root. Currently these
3211 * could come from 2 sources:
3212 * a) a snapshot deletion in progress
3213 * b) a free space cache inode
3214 * We need to distinguish those two, as the snapshot
3215 * orphan must not get deleted.
3216 * find_dead_roots already ran before us, so if this
3217 * is a snapshot deletion, we should find the root
3218 * in the dead_roots list
3219 */
3220 spin_lock(&fs_info->trans_lock);
3221 list_for_each_entry(dead_root, &fs_info->dead_roots,
3222 root_list) {
3223 if (dead_root->root_key.objectid ==
3224 found_key.objectid) {
3225 is_dead_root = 1;
3226 break;
3227 }
3228 }
3229 spin_unlock(&fs_info->trans_lock);
3230 if (is_dead_root) {
3231 /* prevent this orphan from being found again */
3232 key.offset = found_key.objectid - 1;
3233 continue;
3234 }
3235 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003236 /*
3237 * Inode is already gone but the orphan item is still there,
3238 * kill the orphan item.
3239 */
3240 if (ret == -ESTALE) {
3241 trans = btrfs_start_transaction(root, 1);
3242 if (IS_ERR(trans)) {
3243 ret = PTR_ERR(trans);
3244 goto out;
3245 }
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003246 btrfs_debug(root->fs_info, "auto deleting %Lu",
3247 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003248 ret = btrfs_del_orphan_item(trans, root,
3249 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003250 btrfs_end_transaction(trans, root);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003251 if (ret)
3252 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003253 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003254 }
Josef Bacik7b128762008-07-24 12:17:14 -04003255
Josef Bacik7b128762008-07-24 12:17:14 -04003256 /*
3257 * add this inode to the orphan list so btrfs_orphan_del does
3258 * the proper thing when we hit it
3259 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003260 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3261 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003262 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003263
Josef Bacik7b128762008-07-24 12:17:14 -04003264 /* if we have links, this was a truncate, lets do that */
3265 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303266 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003267 iput(inode);
3268 continue;
3269 }
Josef Bacik7b128762008-07-24 12:17:14 -04003270 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003271
3272 /* 1 for the orphan item deletion. */
3273 trans = btrfs_start_transaction(root, 1);
3274 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003275 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003276 ret = PTR_ERR(trans);
3277 goto out;
3278 }
3279 ret = btrfs_orphan_add(trans, inode);
3280 btrfs_end_transaction(trans, root);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003281 if (ret) {
3282 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003283 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003284 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003285
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003286 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003287 if (ret)
3288 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003289 } else {
3290 nr_unlink++;
3291 }
3292
3293 /* this will do delete_inode and everything for us */
3294 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003295 if (ret)
3296 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003297 }
Miao Xie3254c872011-11-10 20:45:05 -05003298 /* release the path since we're done with it */
3299 btrfs_release_path(path);
3300
Yan, Zhengd68fc572010-05-16 10:49:58 -04003301 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3302
3303 if (root->orphan_block_rsv)
3304 btrfs_block_rsv_release(root, root->orphan_block_rsv,
3305 (u64)-1);
3306
Miao Xie27cdeb72014-04-02 19:51:05 +08003307 if (root->orphan_block_rsv ||
3308 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003309 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003310 if (!IS_ERR(trans))
3311 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003312 }
Josef Bacik7b128762008-07-24 12:17:14 -04003313
3314 if (nr_unlink)
David Sterba4884b472013-03-20 13:31:27 +00003315 btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003316 if (nr_truncate)
David Sterba4884b472013-03-20 13:31:27 +00003317 btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003318
3319out:
3320 if (ret)
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003321 btrfs_crit(root->fs_info,
3322 "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003323 btrfs_free_path(path);
3324 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003325}
3326
Chris Masond352ac62008-09-29 15:18:18 -04003327/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003328 * very simple check to peek ahead in the leaf looking for xattrs. If we
3329 * don't find any xattrs, we know there can't be any acls.
3330 *
3331 * slot is the slot the inode is in, objectid is the objectid of the inode
3332 */
3333static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003334 int slot, u64 objectid,
3335 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003336{
3337 u32 nritems = btrfs_header_nritems(leaf);
3338 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003339 static u64 xattr_access = 0;
3340 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003341 int scanned = 0;
3342
Josef Bacikf23b5a52013-06-19 10:16:26 -04003343 if (!xattr_access) {
3344 xattr_access = btrfs_name_hash(POSIX_ACL_XATTR_ACCESS,
3345 strlen(POSIX_ACL_XATTR_ACCESS));
3346 xattr_default = btrfs_name_hash(POSIX_ACL_XATTR_DEFAULT,
3347 strlen(POSIX_ACL_XATTR_DEFAULT));
3348 }
3349
Chris Mason46a53cc2009-04-27 11:47:50 -04003350 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003351 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003352 while (slot < nritems) {
3353 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3354
3355 /* we found a different objectid, there must not be acls */
3356 if (found_key.objectid != objectid)
3357 return 0;
3358
3359 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003360 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003361 if (*first_xattr_slot == -1)
3362 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003363 if (found_key.offset == xattr_access ||
3364 found_key.offset == xattr_default)
3365 return 1;
3366 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003367
3368 /*
3369 * we found a key greater than an xattr key, there can't
3370 * be any acls later on
3371 */
3372 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3373 return 0;
3374
3375 slot++;
3376 scanned++;
3377
3378 /*
3379 * it goes inode, inode backrefs, xattrs, extents,
3380 * so if there are a ton of hard links to an inode there can
3381 * be a lot of backrefs. Don't waste time searching too hard,
3382 * this is just an optimization
3383 */
3384 if (scanned >= 8)
3385 break;
3386 }
3387 /* we hit the end of the leaf before we found an xattr or
3388 * something larger than an xattr. We have to assume the inode
3389 * has acls
3390 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003391 if (*first_xattr_slot == -1)
3392 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003393 return 1;
3394}
3395
3396/*
Chris Masond352ac62008-09-29 15:18:18 -04003397 * read an inode from the btree into the in-memory inode
3398 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003399static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003400{
3401 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003402 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003403 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04003404 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04003405 struct btrfs_root *root = BTRFS_I(inode)->root;
3406 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003407 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003408 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003409 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003410 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003411 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003412 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003413
3414 ret = btrfs_fill_inode(inode, &rdev);
3415 if (!ret)
3416 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003417
3418 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07003419 if (!path)
3420 goto make_bad;
3421
Chris Mason39279cc2007-06-12 06:35:45 -04003422 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003423
Chris Mason39279cc2007-06-12 06:35:45 -04003424 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003425 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04003426 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04003427
Chris Mason5f39d392007-10-15 16:14:19 -04003428 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003429
3430 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003431 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003432
Chris Mason5f39d392007-10-15 16:14:19 -04003433 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3434 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003435 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003436 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003437 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3438 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04003439 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003440
3441 tspec = btrfs_inode_atime(inode_item);
3442 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3443 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3444
3445 tspec = btrfs_inode_mtime(inode_item);
3446 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3447 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3448
3449 tspec = btrfs_inode_ctime(inode_item);
3450 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3451 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3452
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003453 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003454 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003455 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3456
3457 /*
3458 * If we were modified in the current generation and evicted from memory
3459 * and then re-read we need to do a full sync since we don't have any
3460 * idea about which extents were modified before we were evicted from
3461 * cache.
3462 */
3463 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
3464 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3465 &BTRFS_I(inode)->runtime_flags);
3466
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003467 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003468 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003469 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003470 rdev = btrfs_inode_rdev(leaf, inode_item);
3471
Josef Bacikaec74772008-07-24 12:12:38 -04003472 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003473 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003474
3475cache_index:
3476 path->slots[0]++;
3477 if (inode->i_nlink != 1 ||
3478 path->slots[0] >= btrfs_header_nritems(leaf))
3479 goto cache_acl;
3480
3481 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
3482 if (location.objectid != btrfs_ino(inode))
3483 goto cache_acl;
3484
3485 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3486 if (location.type == BTRFS_INODE_REF_KEY) {
3487 struct btrfs_inode_ref *ref;
3488
3489 ref = (struct btrfs_inode_ref *)ptr;
3490 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3491 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3492 struct btrfs_inode_extref *extref;
3493
3494 extref = (struct btrfs_inode_extref *)ptr;
3495 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3496 extref);
3497 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003498cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003499 /*
3500 * try to precache a NULL acl entry for files that don't have
3501 * any xattrs or acls
3502 */
Li Zefan33345d012011-04-20 10:31:50 +08003503 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003504 btrfs_ino(inode), &first_xattr_slot);
3505 if (first_xattr_slot != -1) {
3506 path->slots[0] = first_xattr_slot;
3507 ret = btrfs_load_inode_props(inode, path);
3508 if (ret)
3509 btrfs_err(root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003510 "error loading props for ino %llu (root %llu): %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003511 btrfs_ino(inode),
3512 root->root_key.objectid, ret);
3513 }
3514 btrfs_free_path(path);
3515
Al Viro72c04902009-06-24 16:58:48 -04003516 if (!maybe_acls)
3517 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003518
Chris Mason39279cc2007-06-12 06:35:45 -04003519 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003520 case S_IFREG:
3521 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003522 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05003523 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003524 inode->i_fop = &btrfs_file_operations;
3525 inode->i_op = &btrfs_file_inode_operations;
3526 break;
3527 case S_IFDIR:
3528 inode->i_fop = &btrfs_dir_file_operations;
3529 if (root == root->fs_info->tree_root)
3530 inode->i_op = &btrfs_dir_ro_inode_operations;
3531 else
3532 inode->i_op = &btrfs_dir_inode_operations;
3533 break;
3534 case S_IFLNK:
3535 inode->i_op = &btrfs_symlink_inode_operations;
3536 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04003537 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003538 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003539 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003540 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003541 init_special_inode(inode, inode->i_mode, rdev);
3542 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003543 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003544
3545 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003546 return;
3547
3548make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003549 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003550 make_bad_inode(inode);
3551}
3552
Chris Masond352ac62008-09-29 15:18:18 -04003553/*
3554 * given a leaf and an inode, copy the inode fields into the leaf
3555 */
Chris Masone02119d2008-09-05 16:13:11 -04003556static void fill_inode_item(struct btrfs_trans_handle *trans,
3557 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003558 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003559 struct inode *inode)
3560{
Liu Bo51fab692012-12-27 09:01:21 +00003561 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003562
Liu Bo51fab692012-12-27 09:01:21 +00003563 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003564
Liu Bo51fab692012-12-27 09:01:21 +00003565 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3566 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3567 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3568 &token);
3569 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3570 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003571
Liu Bo51fab692012-12-27 09:01:21 +00003572 btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item),
3573 inode->i_atime.tv_sec, &token);
3574 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item),
3575 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003576
Liu Bo51fab692012-12-27 09:01:21 +00003577 btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item),
3578 inode->i_mtime.tv_sec, &token);
3579 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item),
3580 inode->i_mtime.tv_nsec, &token);
3581
3582 btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item),
3583 inode->i_ctime.tv_sec, &token);
3584 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item),
3585 inode->i_ctime.tv_nsec, &token);
3586
3587 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3588 &token);
3589 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3590 &token);
3591 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3592 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3593 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3594 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3595 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003596}
3597
Chris Masond352ac62008-09-29 15:18:18 -04003598/*
3599 * copy everything in the in-memory inode into the btree.
3600 */
Chris Mason21151332011-11-10 20:39:08 -05003601static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003602 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003603{
3604 struct btrfs_inode_item *inode_item;
3605 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003606 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003607 int ret;
3608
3609 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003610 if (!path)
3611 return -ENOMEM;
3612
Chris Masonb9473432009-03-13 11:00:37 -04003613 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003614 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3615 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003616 if (ret) {
3617 if (ret > 0)
3618 ret = -ENOENT;
3619 goto failed;
3620 }
3621
Chris Mason5f39d392007-10-15 16:14:19 -04003622 leaf = path->nodes[0];
3623 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003624 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003625
Chris Masone02119d2008-09-05 16:13:11 -04003626 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003627 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003628 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003629 ret = 0;
3630failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003631 btrfs_free_path(path);
3632 return ret;
3633}
3634
Chris Masond352ac62008-09-29 15:18:18 -04003635/*
Chris Mason21151332011-11-10 20:39:08 -05003636 * copy everything in the in-memory inode into the btree.
3637 */
3638noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3639 struct btrfs_root *root, struct inode *inode)
3640{
3641 int ret;
3642
3643 /*
3644 * If the inode is a free space inode, we can deadlock during commit
3645 * if we put it into the delayed code.
3646 *
3647 * The data relocation inode should also be directly updated
3648 * without delay
3649 */
Liu Bo83eea1f2012-07-10 05:28:39 -06003650 if (!btrfs_is_free_space_inode(inode)
Chris Mason21151332011-11-10 20:39:08 -05003651 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003652 btrfs_update_root_times(trans, root);
3653
Chris Mason21151332011-11-10 20:39:08 -05003654 ret = btrfs_delayed_update_inode(trans, root, inode);
3655 if (!ret)
3656 btrfs_set_inode_last_trans(trans, inode);
3657 return ret;
3658 }
3659
3660 return btrfs_update_inode_item(trans, root, inode);
3661}
3662
Josef Bacikbe6aef62012-10-22 15:43:12 -04003663noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3664 struct btrfs_root *root,
3665 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003666{
3667 int ret;
3668
3669 ret = btrfs_update_inode(trans, root, inode);
3670 if (ret == -ENOSPC)
3671 return btrfs_update_inode_item(trans, root, inode);
3672 return ret;
3673}
3674
3675/*
Chris Masond352ac62008-09-29 15:18:18 -04003676 * unlink helper that gets used here in inode.c and in the tree logging
3677 * recovery code. It remove a link in a directory with a given name, and
3678 * also drops the back refs in the inode to the directory
3679 */
Al Viro92986792011-03-04 17:14:37 +00003680static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3681 struct btrfs_root *root,
3682 struct inode *dir, struct inode *inode,
3683 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003684{
3685 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003686 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003687 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003688 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003689 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003690 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003691 u64 ino = btrfs_ino(inode);
3692 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003693
3694 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003695 if (!path) {
3696 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003697 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003698 }
3699
Chris Masonb9473432009-03-13 11:00:37 -04003700 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003701 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003702 name, name_len, -1);
3703 if (IS_ERR(di)) {
3704 ret = PTR_ERR(di);
3705 goto err;
3706 }
3707 if (!di) {
3708 ret = -ENOENT;
3709 goto err;
3710 }
Chris Mason5f39d392007-10-15 16:14:19 -04003711 leaf = path->nodes[0];
3712 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003713 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003714 if (ret)
3715 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003716 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003717
Miao Xie67de1172013-12-26 13:07:06 +08003718 /*
3719 * If we don't have dir index, we have to get it by looking up
3720 * the inode ref, since we get the inode ref, remove it directly,
3721 * it is unnecessary to do delayed deletion.
3722 *
3723 * But if we have dir index, needn't search inode ref to get it.
3724 * Since the inode ref is close to the inode item, it is better
3725 * that we delay to delete it, and just do this deletion when
3726 * we update the inode item.
3727 */
3728 if (BTRFS_I(inode)->dir_index) {
3729 ret = btrfs_delayed_delete_inode_ref(inode);
3730 if (!ret) {
3731 index = BTRFS_I(inode)->dir_index;
3732 goto skip_backref;
3733 }
3734 }
3735
Li Zefan33345d012011-04-20 10:31:50 +08003736 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3737 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003738 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003739 btrfs_info(root->fs_info,
3740 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003741 name_len, name, ino, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003742 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003743 goto err;
3744 }
Miao Xie67de1172013-12-26 13:07:06 +08003745skip_backref:
Miao Xie16cdcec2011-04-22 18:12:22 +08003746 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003747 if (ret) {
3748 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003749 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003750 }
Chris Mason39279cc2007-06-12 06:35:45 -04003751
Chris Masone02119d2008-09-05 16:13:11 -04003752 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08003753 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003754 if (ret != 0 && ret != -ENOENT) {
3755 btrfs_abort_transaction(trans, root, ret);
3756 goto err;
3757 }
Chris Masone02119d2008-09-05 16:13:11 -04003758
3759 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
3760 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04003761 if (ret == -ENOENT)
3762 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003763 else if (ret)
3764 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003765err:
3766 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003767 if (ret)
3768 goto out;
3769
3770 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003771 inode_inc_iversion(inode);
3772 inode_inc_iversion(dir);
Chris Masone02119d2008-09-05 16:13:11 -04003773 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Tsutomu Itohb9959292012-06-25 21:25:22 -06003774 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04003775out:
Chris Mason39279cc2007-06-12 06:35:45 -04003776 return ret;
3777}
3778
Al Viro92986792011-03-04 17:14:37 +00003779int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3780 struct btrfs_root *root,
3781 struct inode *dir, struct inode *inode,
3782 const char *name, int name_len)
3783{
3784 int ret;
3785 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3786 if (!ret) {
Zach Brown8b558c52013-10-16 12:10:34 -07003787 drop_nlink(inode);
Al Viro92986792011-03-04 17:14:37 +00003788 ret = btrfs_update_inode(trans, root, inode);
3789 }
3790 return ret;
3791}
Yan, Zhenga22285a2010-05-16 10:48:46 -04003792
3793/*
3794 * helper to start transaction for unlink and rmdir.
3795 *
Josef Bacikd52be812013-05-29 14:54:47 -04003796 * unlink and rmdir are special in btrfs, they do not always free space, so
3797 * if we cannot make our reservations the normal way try and see if there is
3798 * plenty of slack room in the global reserve to migrate, otherwise we cannot
3799 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04003800 */
Josef Bacikd52be812013-05-29 14:54:47 -04003801static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003802{
3803 struct btrfs_trans_handle *trans;
3804 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003805 int ret;
3806
Josef Bacike70bea52011-10-11 14:18:24 -04003807 /*
3808 * 1 for the possible orphan item
3809 * 1 for the dir item
3810 * 1 for the dir index
3811 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04003812 * 1 for the inode
3813 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04003814 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003815 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
3816 return trans;
3817
Josef Bacikd52be812013-05-29 14:54:47 -04003818 if (PTR_ERR(trans) == -ENOSPC) {
3819 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003820
Josef Bacikd52be812013-05-29 14:54:47 -04003821 trans = btrfs_start_transaction(root, 0);
3822 if (IS_ERR(trans))
3823 return trans;
3824 ret = btrfs_cond_migrate_bytes(root->fs_info,
3825 &root->fs_info->trans_block_rsv,
3826 num_bytes, 5);
3827 if (ret) {
3828 btrfs_end_transaction(trans, root);
3829 return ERR_PTR(ret);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003830 }
Josef Bacik5a77d762011-11-01 14:32:23 -04003831 trans->block_rsv = &root->fs_info->trans_block_rsv;
Josef Bacikd52be812013-05-29 14:54:47 -04003832 trans->bytes_reserved = num_bytes;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003833 }
Josef Bacikd52be812013-05-29 14:54:47 -04003834 return trans;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003835}
3836
Chris Mason39279cc2007-06-12 06:35:45 -04003837static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3838{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003839 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003840 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04003841 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003842 int ret;
3843
Josef Bacikd52be812013-05-29 14:54:47 -04003844 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003845 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003846 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003847
Chris Mason12fcfd22009-03-24 10:24:20 -04003848 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
3849
Chris Masone02119d2008-09-05 16:13:11 -04003850 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3851 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003852 if (ret)
3853 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003854
Yan, Zhenga22285a2010-05-16 10:48:46 -04003855 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04003856 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003857 if (ret)
3858 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003859 }
Josef Bacik7b128762008-07-24 12:17:14 -04003860
Tsutomu Itohb5324022011-07-19 07:27:20 +00003861out:
Josef Bacikd52be812013-05-29 14:54:47 -04003862 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003863 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04003864 return ret;
3865}
3866
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003867int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3868 struct btrfs_root *root,
3869 struct inode *dir, u64 objectid,
3870 const char *name, int name_len)
3871{
3872 struct btrfs_path *path;
3873 struct extent_buffer *leaf;
3874 struct btrfs_dir_item *di;
3875 struct btrfs_key key;
3876 u64 index;
3877 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003878 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003879
3880 path = btrfs_alloc_path();
3881 if (!path)
3882 return -ENOMEM;
3883
Li Zefan33345d012011-04-20 10:31:50 +08003884 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003885 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003886 if (IS_ERR_OR_NULL(di)) {
3887 if (!di)
3888 ret = -ENOENT;
3889 else
3890 ret = PTR_ERR(di);
3891 goto out;
3892 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003893
3894 leaf = path->nodes[0];
3895 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3896 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3897 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003898 if (ret) {
3899 btrfs_abort_transaction(trans, root, ret);
3900 goto out;
3901 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003902 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003903
3904 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3905 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003906 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003907 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003908 if (ret != -ENOENT) {
3909 btrfs_abort_transaction(trans, root, ret);
3910 goto out;
3911 }
Li Zefan33345d012011-04-20 10:31:50 +08003912 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003913 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003914 if (IS_ERR_OR_NULL(di)) {
3915 if (!di)
3916 ret = -ENOENT;
3917 else
3918 ret = PTR_ERR(di);
3919 btrfs_abort_transaction(trans, root, ret);
3920 goto out;
3921 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003922
3923 leaf = path->nodes[0];
3924 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003925 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003926 index = key.offset;
3927 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003928 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003929
Miao Xie16cdcec2011-04-22 18:12:22 +08003930 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003931 if (ret) {
3932 btrfs_abort_transaction(trans, root, ret);
3933 goto out;
3934 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003935
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003936 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003937 inode_inc_iversion(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003938 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Josef Bacik5a24e842012-08-08 10:12:59 -06003939 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003940 if (ret)
3941 btrfs_abort_transaction(trans, root, ret);
3942out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04003943 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003944 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003945}
3946
Chris Mason39279cc2007-06-12 06:35:45 -04003947static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3948{
3949 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003950 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003951 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003952 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04003953
David Sterbab3ae2442012-09-13 16:04:34 -06003954 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04003955 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06003956 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
3957 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04003958
Josef Bacikd52be812013-05-29 14:54:47 -04003959 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003960 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003961 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003962
Li Zefan33345d012011-04-20 10:31:50 +08003963 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003964 err = btrfs_unlink_subvol(trans, root, dir,
3965 BTRFS_I(inode)->location.objectid,
3966 dentry->d_name.name,
3967 dentry->d_name.len);
3968 goto out;
3969 }
3970
Josef Bacik7b128762008-07-24 12:17:14 -04003971 err = btrfs_orphan_add(trans, inode);
3972 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003973 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003974
Chris Mason39279cc2007-06-12 06:35:45 -04003975 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04003976 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3977 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05003978 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04003979 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003980out:
Josef Bacikd52be812013-05-29 14:54:47 -04003981 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003982 btrfs_btree_balance_dirty(root);
Chris Mason39544012007-12-12 14:38:19 -05003983
Chris Mason39279cc2007-06-12 06:35:45 -04003984 return err;
3985}
3986
Chris Mason323ac952008-10-01 19:05:46 -04003987/*
Chris Mason39279cc2007-06-12 06:35:45 -04003988 * this can truncate away extent items, csum items and directory items.
3989 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04003990 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04003991 *
3992 * csum items that cross the new i_size are truncated to the new size
3993 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04003994 *
3995 * min_type is the minimum key type to truncate down to. If set to 0, this
3996 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04003997 */
Yan, Zheng80825102009-11-12 09:35:36 +00003998int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
3999 struct btrfs_root *root,
4000 struct inode *inode,
4001 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004002{
Chris Mason39279cc2007-06-12 06:35:45 -04004003 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004004 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004005 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004006 struct btrfs_key key;
4007 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004008 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004009 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004010 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004011 u64 item_end = 0;
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004012 u64 last_size = (u64)-1;
Yan, Zheng80825102009-11-12 09:35:36 +00004013 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004014 int found_extent;
4015 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004016 int pending_del_nr = 0;
4017 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004018 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004019 int ret;
4020 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08004021 u64 ino = btrfs_ino(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004022
4023 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004024
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004025 path = btrfs_alloc_path();
4026 if (!path)
4027 return -ENOMEM;
4028 path->reada = -1;
4029
Josef Bacik5dc562c2012-08-17 13:14:17 -04004030 /*
4031 * We want to drop from the next block forward in case this new size is
4032 * not block aligned since we will be keeping the last block of the
4033 * extent just the way it is.
4034 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004035 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4036 root == root->fs_info->tree_root)
Qu Wenruofda28322013-02-26 08:10:22 +00004037 btrfs_drop_extent_cache(inode, ALIGN(new_size,
4038 root->sectorsize), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004039
Miao Xie16cdcec2011-04-22 18:12:22 +08004040 /*
4041 * This function is also used to drop the items in the log tree before
4042 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4043 * it is used to drop the loged items. So we shouldn't kill the delayed
4044 * items.
4045 */
4046 if (min_type == 0 && root == BTRFS_I(inode)->root)
4047 btrfs_kill_delayed_inode_items(inode);
4048
Li Zefan33345d012011-04-20 10:31:50 +08004049 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004050 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004051 key.type = (u8)-1;
4052
Chris Mason85e21ba2008-01-29 15:11:36 -05004053search_again:
Chris Masonb9473432009-03-13 11:00:37 -04004054 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004055 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004056 if (ret < 0) {
4057 err = ret;
4058 goto out;
4059 }
Chris Masond3977122009-01-05 21:25:51 -05004060
Chris Mason85e21ba2008-01-29 15:11:36 -05004061 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004062 /* there are no items in the tree for us to truncate, we're
4063 * done
4064 */
Yan, Zheng80825102009-11-12 09:35:36 +00004065 if (path->slots[0] == 0)
4066 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004067 path->slots[0]--;
4068 }
4069
Chris Masond3977122009-01-05 21:25:51 -05004070 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004071 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004072 leaf = path->nodes[0];
4073 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4074 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04004075
Li Zefan33345d012011-04-20 10:31:50 +08004076 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004077 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004078
Chris Mason85e21ba2008-01-29 15:11:36 -05004079 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004080 break;
4081
Chris Mason5f39d392007-10-15 16:14:19 -04004082 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004083 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004084 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004085 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004086 extent_type = btrfs_file_extent_type(leaf, fi);
4087 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004088 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004089 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04004090 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004091 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004092 path->slots[0], fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004093 }
Yan008630c2007-11-07 13:31:09 -05004094 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004095 }
Yan, Zheng80825102009-11-12 09:35:36 +00004096 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004097 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004098 } else {
4099 if (item_end < new_size)
4100 break;
4101 if (found_key.offset >= new_size)
4102 del_item = 1;
4103 else
4104 del_item = 0;
4105 }
Chris Mason39279cc2007-06-12 06:35:45 -04004106 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004107 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004108 if (found_type != BTRFS_EXTENT_DATA_KEY)
4109 goto delete;
4110
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004111 if (del_item)
4112 last_size = found_key.offset;
4113 else
4114 last_size = new_size;
4115
Chris Mason179e29e2007-11-01 11:28:41 -04004116 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004117 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004118 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004119 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004120 u64 orig_num_bytes =
4121 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004122 extent_num_bytes = ALIGN(new_size -
4123 found_key.offset,
4124 root->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004125 btrfs_set_file_extent_num_bytes(leaf, fi,
4126 extent_num_bytes);
4127 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004128 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004129 if (test_bit(BTRFS_ROOT_REF_COWS,
4130 &root->state) &&
4131 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004132 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004133 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004134 } else {
Chris Masondb945352007-10-15 16:15:53 -04004135 extent_num_bytes =
4136 btrfs_file_extent_disk_num_bytes(leaf,
4137 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004138 extent_offset = found_key.offset -
4139 btrfs_file_extent_offset(leaf, fi);
4140
Chris Mason39279cc2007-06-12 06:35:45 -04004141 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004142 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004143 if (extent_start != 0) {
4144 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004145 if (test_bit(BTRFS_ROOT_REF_COWS,
4146 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004147 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004148 }
4149 }
Chris Mason90692182008-02-08 13:49:28 -05004150 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004151 /*
4152 * we can't truncate inline items that have had
4153 * special encodings
4154 */
4155 if (!del_item &&
4156 btrfs_file_extent_compression(leaf, fi) == 0 &&
4157 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4158 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004159 u32 size = new_size - found_key.offset;
4160
Miao Xie27cdeb72014-04-02 19:51:05 +08004161 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004162 inode_sub_bytes(inode, item_end + 1 -
4163 new_size);
Chris Mason514ac8a2014-01-03 21:07:00 -08004164
4165 /*
4166 * update the ram bytes to properly reflect
4167 * the new size of our item
4168 */
4169 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
Chris Masone02119d2008-09-05 16:13:11 -04004170 size =
4171 btrfs_file_extent_calc_inline_size(size);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004172 btrfs_truncate_item(root, path, size, 1);
Miao Xie27cdeb72014-04-02 19:51:05 +08004173 } else if (test_bit(BTRFS_ROOT_REF_COWS,
4174 &root->state)) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004175 inode_sub_bytes(inode, item_end + 1 -
4176 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05004177 }
Chris Mason39279cc2007-06-12 06:35:45 -04004178 }
Chris Mason179e29e2007-11-01 11:28:41 -04004179delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004180 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004181 if (!pending_del_nr) {
4182 /* no pending yet, add ourselves */
4183 pending_del_slot = path->slots[0];
4184 pending_del_nr = 1;
4185 } else if (pending_del_nr &&
4186 path->slots[0] + 1 == pending_del_slot) {
4187 /* hop on the pending chunk */
4188 pending_del_nr++;
4189 pending_del_slot = path->slots[0];
4190 } else {
Chris Masond3977122009-01-05 21:25:51 -05004191 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004192 }
Chris Mason39279cc2007-06-12 06:35:45 -04004193 } else {
4194 break;
4195 }
Miao Xie27cdeb72014-04-02 19:51:05 +08004196 if (found_extent &&
4197 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4198 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004199 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004200 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004201 extent_num_bytes, 0,
4202 btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02004203 ino, extent_offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004204 BUG_ON(ret);
4205 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004206
Yan, Zheng80825102009-11-12 09:35:36 +00004207 if (found_type == BTRFS_INODE_ITEM_KEY)
4208 break;
4209
4210 if (path->slots[0] == 0 ||
4211 path->slots[0] != pending_del_slot) {
Yan, Zheng80825102009-11-12 09:35:36 +00004212 if (pending_del_nr) {
4213 ret = btrfs_del_items(trans, root, path,
4214 pending_del_slot,
4215 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004216 if (ret) {
4217 btrfs_abort_transaction(trans,
4218 root, ret);
4219 goto error;
4220 }
Yan, Zheng80825102009-11-12 09:35:36 +00004221 pending_del_nr = 0;
4222 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004223 btrfs_release_path(path);
Chris Mason85e21ba2008-01-29 15:11:36 -05004224 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004225 } else {
4226 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004227 }
Chris Mason39279cc2007-06-12 06:35:45 -04004228 }
Yan, Zheng80825102009-11-12 09:35:36 +00004229out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004230 if (pending_del_nr) {
4231 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4232 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004233 if (ret)
4234 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004235 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004236error:
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004237 if (last_size != (u64)-1)
4238 btrfs_ordered_update_i_size(inode, last_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004239 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00004240 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004241}
4242
Chris Masona52d9a82007-08-27 16:49:44 -04004243/*
Josef Bacik2aaa6652012-08-29 14:27:18 -04004244 * btrfs_truncate_page - read, zero a chunk and write a page
4245 * @inode - inode that we're zeroing
4246 * @from - the offset to start zeroing
4247 * @len - the length to zero, 0 to zero the entire range respective to the
4248 * offset
4249 * @front - zero up to the offset instead of from the offset on
4250 *
4251 * This will find the page for the "from" offset and cow the page and zero the
4252 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004253 */
Josef Bacik2aaa6652012-08-29 14:27:18 -04004254int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
4255 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004256{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004257 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04004258 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004259 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4260 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004261 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004262 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04004263 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04004264 pgoff_t index = from >> PAGE_CACHE_SHIFT;
4265 unsigned offset = from & (PAGE_CACHE_SIZE-1);
4266 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004267 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004268 int ret = 0;
4269 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004270 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004271
Josef Bacik2aaa6652012-08-29 14:27:18 -04004272 if ((offset & (blocksize - 1)) == 0 &&
4273 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004274 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004275 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004276 if (ret)
4277 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004278
Chris Mason211c17f2008-05-15 09:13:45 -04004279again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004280 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004281 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004282 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Miao Xieac6a2b32012-12-05 10:56:13 +00004283 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004284 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004285 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004286
4287 page_start = page_offset(page);
4288 page_end = page_start + PAGE_CACHE_SIZE - 1;
4289
Chris Masona52d9a82007-08-27 16:49:44 -04004290 if (!PageUptodate(page)) {
4291 ret = btrfs_readpage(NULL, page);
4292 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004293 if (page->mapping != mapping) {
4294 unlock_page(page);
4295 page_cache_release(page);
4296 goto again;
4297 }
Chris Masona52d9a82007-08-27 16:49:44 -04004298 if (!PageUptodate(page)) {
4299 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004300 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004301 }
4302 }
Chris Mason211c17f2008-05-15 09:13:45 -04004303 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004304
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004305 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004306 set_page_extent_mapped(page);
4307
4308 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4309 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004310 unlock_extent_cached(io_tree, page_start, page_end,
4311 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004312 unlock_page(page);
4313 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004314 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004315 btrfs_put_ordered_extent(ordered);
4316 goto again;
4317 }
4318
Josef Bacik2ac55d42010-02-03 19:33:23 +00004319 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004320 EXTENT_DIRTY | EXTENT_DELALLOC |
4321 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004322 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004323
Josef Bacik2ac55d42010-02-03 19:33:23 +00004324 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
4325 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004326 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004327 unlock_extent_cached(io_tree, page_start, page_end,
4328 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004329 goto out_unlock;
4330 }
4331
Chris Masone6dcd2d2008-07-17 12:53:50 -04004332 if (offset != PAGE_CACHE_SIZE) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004333 if (!len)
4334 len = PAGE_CACHE_SIZE - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004335 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004336 if (front)
4337 memset(kaddr, 0, offset);
4338 else
4339 memset(kaddr + offset, 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004340 flush_dcache_page(page);
4341 kunmap(page);
4342 }
Chris Mason247e7432008-07-17 12:53:51 -04004343 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004344 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004345 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
4346 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004347
Chris Mason89642222008-07-24 09:41:53 -04004348out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004349 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004350 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04004351 unlock_page(page);
4352 page_cache_release(page);
4353out:
4354 return ret;
4355}
4356
Josef Bacik16e75492013-10-22 12:18:51 -04004357static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4358 u64 offset, u64 len)
4359{
4360 struct btrfs_trans_handle *trans;
4361 int ret;
4362
4363 /*
4364 * Still need to make sure the inode looks like it's been updated so
4365 * that any holes get logged if we fsync.
4366 */
4367 if (btrfs_fs_incompat(root->fs_info, NO_HOLES)) {
4368 BTRFS_I(inode)->last_trans = root->fs_info->generation;
4369 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4370 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4371 return 0;
4372 }
4373
4374 /*
4375 * 1 - for the one we're dropping
4376 * 1 - for the one we're adding
4377 * 1 - for updating the inode.
4378 */
4379 trans = btrfs_start_transaction(root, 3);
4380 if (IS_ERR(trans))
4381 return PTR_ERR(trans);
4382
4383 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4384 if (ret) {
4385 btrfs_abort_transaction(trans, root, ret);
4386 btrfs_end_transaction(trans, root);
4387 return ret;
4388 }
4389
4390 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset,
4391 0, 0, len, 0, len, 0, 0, 0);
4392 if (ret)
4393 btrfs_abort_transaction(trans, root, ret);
4394 else
4395 btrfs_update_inode(trans, root, inode);
4396 btrfs_end_transaction(trans, root);
4397 return ret;
4398}
4399
Josef Bacik695a0d02011-03-04 15:46:53 -05004400/*
4401 * This function puts in dummy file extents for the area we're creating a hole
4402 * for. So if we are truncating this file to a larger size we need to insert
4403 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4404 * the range between oldsize and size
4405 */
Josef Bacika41ad392011-01-31 15:30:16 -05004406int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004407{
Yan Zheng9036c102008-10-30 14:19:41 -04004408 struct btrfs_root *root = BTRFS_I(inode)->root;
4409 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004410 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004411 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004412 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Qu Wenruofda28322013-02-26 08:10:22 +00004413 u64 hole_start = ALIGN(oldsize, root->sectorsize);
4414 u64 block_end = ALIGN(size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004415 u64 last_byte;
4416 u64 cur_offset;
4417 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004418 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004419
Josef Bacika71754f2013-06-17 17:14:39 -04004420 /*
4421 * If our size started in the middle of a page we need to zero out the
4422 * rest of the page before we expand the i_size, otherwise we could
4423 * expose stale data.
4424 */
4425 err = btrfs_truncate_page(inode, oldsize, 0, 0);
4426 if (err)
4427 return err;
4428
Yan Zheng9036c102008-10-30 14:19:41 -04004429 if (size <= hole_start)
4430 return 0;
4431
Yan Zheng9036c102008-10-30 14:19:41 -04004432 while (1) {
4433 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004434
Josef Bacik2ac55d42010-02-03 19:33:23 +00004435 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004436 &cached_state);
Miao Xiefa7c1492013-09-26 13:15:27 +08004437 ordered = btrfs_lookup_ordered_range(inode, hole_start,
4438 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004439 if (!ordered)
4440 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004441 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4442 &cached_state, GFP_NOFS);
Miao Xiefa7c1492013-09-26 13:15:27 +08004443 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004444 btrfs_put_ordered_extent(ordered);
4445 }
4446
Yan Zheng9036c102008-10-30 14:19:41 -04004447 cur_offset = hole_start;
4448 while (1) {
4449 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4450 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004451 if (IS_ERR(em)) {
4452 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004453 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004454 break;
4455 }
Yan Zheng9036c102008-10-30 14:19:41 -04004456 last_byte = min(extent_map_end(em), block_end);
Qu Wenruofda28322013-02-26 08:10:22 +00004457 last_byte = ALIGN(last_byte , root->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004458 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004459 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004460 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004461
Josef Bacik16e75492013-10-22 12:18:51 -04004462 err = maybe_insert_hole(root, inode, cur_offset,
4463 hole_size);
4464 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004465 break;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004466 btrfs_drop_extent_cache(inode, cur_offset,
4467 cur_offset + hole_size - 1, 0);
4468 hole_em = alloc_extent_map();
4469 if (!hole_em) {
4470 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4471 &BTRFS_I(inode)->runtime_flags);
4472 goto next;
4473 }
4474 hole_em->start = cur_offset;
4475 hole_em->len = hole_size;
4476 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004477
Josef Bacik5dc562c2012-08-17 13:14:17 -04004478 hole_em->block_start = EXTENT_MAP_HOLE;
4479 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004480 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004481 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004482 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
4483 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Josef Bacik16e75492013-10-22 12:18:51 -04004484 hole_em->generation = root->fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004485
4486 while (1) {
4487 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004488 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004489 write_unlock(&em_tree->lock);
4490 if (err != -EEXIST)
4491 break;
4492 btrfs_drop_extent_cache(inode, cur_offset,
4493 cur_offset +
4494 hole_size - 1, 0);
4495 }
4496 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04004497 }
Josef Bacik16e75492013-10-22 12:18:51 -04004498next:
Yan Zheng9036c102008-10-30 14:19:41 -04004499 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004500 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004501 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004502 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004503 break;
4504 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004505 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004506 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4507 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004508 return err;
4509}
4510
Eric Sandeen3972f262013-01-12 02:57:22 +00004511static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004512{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004513 struct btrfs_root *root = BTRFS_I(inode)->root;
4514 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004515 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004516 loff_t newsize = attr->ia_size;
4517 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004518 int ret;
4519
Eric Sandeen3972f262013-01-12 02:57:22 +00004520 /*
4521 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4522 * special case where we need to update the times despite not having
4523 * these flags set. For all other operations the VFS set these flags
4524 * explicitly if it wants a timestamp update.
4525 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004526 if (newsize != oldsize) {
4527 inode_inc_iversion(inode);
4528 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
4529 inode->i_ctime = inode->i_mtime =
4530 current_fs_time(inode->i_sb);
4531 }
Eric Sandeen3972f262013-01-12 02:57:22 +00004532
Josef Bacika41ad392011-01-31 15:30:16 -05004533 if (newsize > oldsize) {
Kirill A. Shutemov7caef262013-09-12 15:13:56 -07004534 truncate_pagecache(inode, newsize);
Josef Bacika41ad392011-01-31 15:30:16 -05004535 ret = btrfs_cont_expand(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004536 if (ret)
Yan, Zheng80825102009-11-12 09:35:36 +00004537 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004538
Miao Xief4a2f4c2011-12-14 20:12:01 -05004539 trans = btrfs_start_transaction(root, 1);
4540 if (IS_ERR(trans))
4541 return PTR_ERR(trans);
4542
4543 i_size_write(inode, newsize);
4544 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
4545 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004546 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05004547 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004548
Josef Bacika41ad392011-01-31 15:30:16 -05004549 /*
4550 * We're truncating a file that used to have good data down to
4551 * zero. Make sure it gets into the ordered flush list so that
4552 * any new writes get down to disk quickly.
4553 */
4554 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004555 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4556 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004557
Josef Bacikf3fe8202013-01-07 17:03:21 -05004558 /*
4559 * 1 for the orphan item we're going to add
4560 * 1 for the orphan item deletion.
4561 */
4562 trans = btrfs_start_transaction(root, 2);
4563 if (IS_ERR(trans))
4564 return PTR_ERR(trans);
4565
4566 /*
4567 * We need to do this in case we fail at _any_ point during the
4568 * actual truncate. Once we do the truncate_setsize we could
4569 * invalidate pages which forces any outstanding ordered io to
4570 * be instantly completed which will give us extents that need
4571 * to be truncated. If we fail to get an orphan inode down we
4572 * could have left over extents that were never meant to live,
4573 * so we need to garuntee from this point on that everything
4574 * will be consistent.
4575 */
4576 ret = btrfs_orphan_add(trans, inode);
4577 btrfs_end_transaction(trans, root);
4578 if (ret)
4579 return ret;
4580
Josef Bacika41ad392011-01-31 15:30:16 -05004581 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4582 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004583
4584 /* Disable nonlocked read DIO to avoid the end less truncate */
4585 btrfs_inode_block_unlocked_dio(inode);
4586 inode_dio_wait(inode);
4587 btrfs_inode_resume_unlocked_dio(inode);
4588
Josef Bacika41ad392011-01-31 15:30:16 -05004589 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004590 if (ret && inode->i_nlink) {
4591 int err;
4592
4593 /*
4594 * failed to truncate, disk_i_size is only adjusted down
4595 * as we remove extents, so it should represent the true
4596 * size of the inode, so reset the in memory size and
4597 * delete our orphan entry.
4598 */
4599 trans = btrfs_join_transaction(root);
4600 if (IS_ERR(trans)) {
4601 btrfs_orphan_del(NULL, inode);
4602 return ret;
4603 }
4604 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
4605 err = btrfs_orphan_del(trans, inode);
4606 if (err)
4607 btrfs_abort_transaction(trans, root, err);
4608 btrfs_end_transaction(trans, root);
4609 }
Yan, Zheng80825102009-11-12 09:35:36 +00004610 }
4611
Josef Bacika41ad392011-01-31 15:30:16 -05004612 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004613}
4614
Chris Mason39279cc2007-06-12 06:35:45 -04004615static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
4616{
4617 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08004618 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004619 int err;
4620
Li Zefanb83cc962010-12-20 16:04:08 +08004621 if (btrfs_root_readonly(root))
4622 return -EROFS;
4623
Chris Mason39279cc2007-06-12 06:35:45 -04004624 err = inode_change_ok(inode, attr);
4625 if (err)
4626 return err;
4627
Chris Mason5a3f23d2009-03-31 13:27:11 -04004628 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00004629 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00004630 if (err)
4631 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004632 }
Yan Zheng9036c102008-10-30 14:19:41 -04004633
Christoph Hellwig10257742010-06-04 11:30:02 +02004634 if (attr->ia_valid) {
4635 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004636 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004637 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04004638
Josef Bacik22c44fe2011-11-30 10:45:38 -05004639 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08004640 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02004641 }
4642
Chris Mason39279cc2007-06-12 06:35:45 -04004643 return err;
4644}
Chris Mason61295eb2008-01-14 16:24:38 -05004645
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004646/*
4647 * While truncating the inode pages during eviction, we get the VFS calling
4648 * btrfs_invalidatepage() against each page of the inode. This is slow because
4649 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
4650 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
4651 * extent_state structures over and over, wasting lots of time.
4652 *
4653 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
4654 * those expensive operations on a per page basis and do only the ordered io
4655 * finishing, while we release here the extent_map and extent_state structures,
4656 * without the excessive merging and splitting.
4657 */
4658static void evict_inode_truncate_pages(struct inode *inode)
4659{
4660 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4661 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
4662 struct rb_node *node;
4663
4664 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07004665 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004666
4667 write_lock(&map_tree->lock);
4668 while (!RB_EMPTY_ROOT(&map_tree->map)) {
4669 struct extent_map *em;
4670
4671 node = rb_first(&map_tree->map);
4672 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08004673 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
4674 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004675 remove_extent_mapping(map_tree, em);
4676 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01004677 if (need_resched()) {
4678 write_unlock(&map_tree->lock);
4679 cond_resched();
4680 write_lock(&map_tree->lock);
4681 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004682 }
4683 write_unlock(&map_tree->lock);
4684
4685 spin_lock(&io_tree->lock);
4686 while (!RB_EMPTY_ROOT(&io_tree->state)) {
4687 struct extent_state *state;
4688 struct extent_state *cached_state = NULL;
4689
4690 node = rb_first(&io_tree->state);
4691 state = rb_entry(node, struct extent_state, rb_node);
4692 atomic_inc(&state->refs);
4693 spin_unlock(&io_tree->lock);
4694
4695 lock_extent_bits(io_tree, state->start, state->end,
4696 0, &cached_state);
4697 clear_extent_bit(io_tree, state->start, state->end,
4698 EXTENT_LOCKED | EXTENT_DIRTY |
4699 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
4700 EXTENT_DEFRAG, 1, 1,
4701 &cached_state, GFP_NOFS);
4702 free_extent_state(state);
4703
Filipe Manana7064dd52014-08-08 02:47:05 +01004704 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004705 spin_lock(&io_tree->lock);
4706 }
4707 spin_unlock(&io_tree->lock);
4708}
4709
Al Virobd555972010-06-07 11:35:40 -04004710void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004711{
4712 struct btrfs_trans_handle *trans;
4713 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04004714 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik07127182011-08-19 10:29:59 -04004715 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04004716 int ret;
4717
liubo1abe9b82011-03-24 11:18:59 +00004718 trace_btrfs_inode_evict(inode);
4719
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004720 evict_inode_truncate_pages(inode);
4721
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02004722 if (inode->i_nlink &&
4723 ((btrfs_root_refs(&root->root_item) != 0 &&
4724 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
4725 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04004726 goto no_delete;
4727
Chris Mason39279cc2007-06-12 06:35:45 -04004728 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04004729 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004730 goto no_delete;
4731 }
Al Virobd555972010-06-07 11:35:40 -04004732 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04004733 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04004734
Yan, Zhengc71bf092009-11-12 09:34:40 +00004735 if (root->fs_info->log_root_recovering) {
Liu Bo6bf02312012-06-25 21:59:09 -06004736 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04004737 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00004738 goto no_delete;
4739 }
4740
Yan, Zheng76dda932009-09-21 16:00:26 -04004741 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02004742 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
4743 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04004744 goto no_delete;
4745 }
4746
Miao Xie0e8c36a2012-12-19 06:59:51 +00004747 ret = btrfs_commit_inode_delayed_inode(inode);
4748 if (ret) {
4749 btrfs_orphan_del(NULL, inode);
4750 goto no_delete;
4751 }
4752
Miao Xie66d8f3d2012-09-06 04:02:28 -06004753 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04004754 if (!rsv) {
4755 btrfs_orphan_del(NULL, inode);
4756 goto no_delete;
4757 }
Josef Bacik4a338542011-08-29 11:01:31 -04004758 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04004759 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04004760 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04004761
Chris Masondbe674a2008-07-17 12:54:05 -04004762 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04004763
Josef Bacik4289a662011-08-05 13:22:24 -04004764 /*
Miao Xie8407aa42012-09-07 01:43:32 -06004765 * This is a bit simpler than btrfs_truncate since we've already
4766 * reserved our space for our orphan item in the unlink, so we just
4767 * need to reserve some slack space in case we add bytes and update
4768 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04004769 */
Yan, Zheng80825102009-11-12 09:35:36 +00004770 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00004771 ret = btrfs_block_rsv_refill(root, rsv, min_size,
4772 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00004773
Josef Bacik726c35f2011-09-26 15:46:06 -04004774 /*
4775 * Try and steal from the global reserve since we will
4776 * likely not use this space anyway, we want to try as
4777 * hard as possible to get this to work.
4778 */
4779 if (ret)
4780 ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
4781
Yan, Zhengd68fc572010-05-16 10:49:58 -04004782 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004783 btrfs_warn(root->fs_info,
4784 "Could not get space for a delete, will truncate on mount %d",
4785 ret);
Josef Bacik4289a662011-08-05 13:22:24 -04004786 btrfs_orphan_del(NULL, inode);
4787 btrfs_free_block_rsv(root, rsv);
4788 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04004789 }
4790
Miao Xie0e8c36a2012-12-19 06:59:51 +00004791 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04004792 if (IS_ERR(trans)) {
4793 btrfs_orphan_del(NULL, inode);
4794 btrfs_free_block_rsv(root, rsv);
4795 goto no_delete;
4796 }
4797
4798 trans->block_rsv = rsv;
4799
Yan, Zhengd68fc572010-05-16 10:49:58 -04004800 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04004801 if (ret != -ENOSPC)
Yan, Zheng80825102009-11-12 09:35:36 +00004802 break;
4803
Miao Xie8407aa42012-09-07 01:43:32 -06004804 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004805 btrfs_end_transaction(trans, root);
4806 trans = NULL;
Liu Bob53d3f52012-11-14 14:34:34 +00004807 btrfs_btree_balance_dirty(root);
Josef Bacik7b128762008-07-24 12:17:14 -04004808 }
4809
Josef Bacik4289a662011-08-05 13:22:24 -04004810 btrfs_free_block_rsv(root, rsv);
4811
Josef Bacik4ef31a42013-08-13 14:10:08 -04004812 /*
4813 * Errors here aren't a big deal, it just means we leave orphan items
4814 * in the tree. They will be cleaned up on the next mount.
4815 */
Yan, Zheng80825102009-11-12 09:35:36 +00004816 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04004817 trans->block_rsv = root->orphan_block_rsv;
Josef Bacik4ef31a42013-08-13 14:10:08 -04004818 btrfs_orphan_del(trans, inode);
4819 } else {
4820 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004821 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004822
Josef Bacik4289a662011-08-05 13:22:24 -04004823 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08004824 if (!(root == root->fs_info->tree_root ||
4825 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08004826 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08004827
Chris Mason54aa1f42007-06-22 14:16:25 -04004828 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004829 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04004830no_delete:
Miao Xie89042e52013-05-15 07:48:15 +00004831 btrfs_remove_delayed_node(inode);
Jan Karadbd57682012-05-03 14:48:02 +02004832 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004833 return;
Chris Mason39279cc2007-06-12 06:35:45 -04004834}
4835
4836/*
4837 * this returns the key found in the dir entry in the location pointer.
4838 * If no dir entries were found, location->objectid is 0.
4839 */
4840static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
4841 struct btrfs_key *location)
4842{
4843 const char *name = dentry->d_name.name;
4844 int namelen = dentry->d_name.len;
4845 struct btrfs_dir_item *di;
4846 struct btrfs_path *path;
4847 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04004848 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004849
4850 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07004851 if (!path)
4852 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05004853
Li Zefan33345d012011-04-20 10:31:50 +08004854 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04004855 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04004856 if (IS_ERR(di))
4857 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05004858
David Sterbac7040052011-04-19 18:00:01 +02004859 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05004860 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05004861
Chris Mason5f39d392007-10-15 16:14:19 -04004862 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04004863out:
Chris Mason39279cc2007-06-12 06:35:45 -04004864 btrfs_free_path(path);
4865 return ret;
Chris Mason39544012007-12-12 14:38:19 -05004866out_err:
4867 location->objectid = 0;
4868 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04004869}
4870
4871/*
4872 * when we hit a tree root in a directory, the btrfs part of the inode
4873 * needs to be changed to reflect the root directory of the tree root. This
4874 * is kind of like crossing a mount point.
4875 */
4876static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004877 struct inode *dir,
4878 struct dentry *dentry,
4879 struct btrfs_key *location,
4880 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04004881{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004882 struct btrfs_path *path;
4883 struct btrfs_root *new_root;
4884 struct btrfs_root_ref *ref;
4885 struct extent_buffer *leaf;
4886 int ret;
4887 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004888
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004889 path = btrfs_alloc_path();
4890 if (!path) {
4891 err = -ENOMEM;
4892 goto out;
4893 }
Chris Mason39279cc2007-06-12 06:35:45 -04004894
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004895 err = -ENOENT;
Kelley Nielsen75ac2dd2013-11-04 19:35:58 -08004896 ret = btrfs_find_item(root->fs_info->tree_root, path,
4897 BTRFS_I(dir)->root->root_key.objectid,
4898 location->objectid, BTRFS_ROOT_REF_KEY, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004899 if (ret) {
4900 if (ret < 0)
4901 err = ret;
4902 goto out;
4903 }
Chris Mason39279cc2007-06-12 06:35:45 -04004904
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004905 leaf = path->nodes[0];
4906 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08004907 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004908 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
4909 goto out;
4910
4911 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
4912 (unsigned long)(ref + 1),
4913 dentry->d_name.len);
4914 if (ret)
4915 goto out;
4916
David Sterbab3b4aa72011-04-21 01:20:15 +02004917 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004918
4919 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
4920 if (IS_ERR(new_root)) {
4921 err = PTR_ERR(new_root);
4922 goto out;
4923 }
4924
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004925 *sub_root = new_root;
4926 location->objectid = btrfs_root_dirid(&new_root->root_item);
4927 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04004928 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004929 err = 0;
4930out:
4931 btrfs_free_path(path);
4932 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004933}
4934
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004935static void inode_tree_add(struct inode *inode)
4936{
4937 struct btrfs_root *root = BTRFS_I(inode)->root;
4938 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004939 struct rb_node **p;
4940 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01004941 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Li Zefan33345d012011-04-20 10:31:50 +08004942 u64 ino = btrfs_ino(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004943
Al Viro1d3382cb2010-10-23 15:19:20 -04004944 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004945 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00004946 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004947 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00004948 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004949 while (*p) {
4950 parent = *p;
4951 entry = rb_entry(parent, struct btrfs_inode, rb_node);
4952
Li Zefan33345d012011-04-20 10:31:50 +08004953 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004954 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004955 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004956 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004957 else {
4958 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04004959 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01004960 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004961 RB_CLEAR_NODE(parent);
4962 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01004963 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004964 }
4965 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01004966 rb_link_node(new, parent, p);
4967 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004968 spin_unlock(&root->inode_lock);
4969}
4970
4971static void inode_tree_del(struct inode *inode)
4972{
4973 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04004974 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004975
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004976 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004977 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004978 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004979 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04004980 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004981 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004982 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04004983
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02004984 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004985 synchronize_srcu(&root->fs_info->subvol_srcu);
4986 spin_lock(&root->inode_lock);
4987 empty = RB_EMPTY_ROOT(&root->inode_tree);
4988 spin_unlock(&root->inode_lock);
4989 if (empty)
4990 btrfs_add_dead_root(root);
4991 }
4992}
4993
Jeff Mahoney143bede2012-03-01 14:56:26 +01004994void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04004995{
4996 struct rb_node *node;
4997 struct rb_node *prev;
4998 struct btrfs_inode *entry;
4999 struct inode *inode;
5000 u64 objectid = 0;
5001
Liu Bo7813b3d2014-02-10 17:37:25 +08005002 if (!test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
5003 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04005004
5005 spin_lock(&root->inode_lock);
5006again:
5007 node = root->inode_tree.rb_node;
5008 prev = NULL;
5009 while (node) {
5010 prev = node;
5011 entry = rb_entry(node, struct btrfs_inode, rb_node);
5012
Li Zefan33345d012011-04-20 10:31:50 +08005013 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005014 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08005015 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005016 node = node->rb_right;
5017 else
5018 break;
5019 }
5020 if (!node) {
5021 while (prev) {
5022 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08005023 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005024 node = prev;
5025 break;
5026 }
5027 prev = rb_next(prev);
5028 }
5029 }
5030 while (node) {
5031 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08005032 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04005033 inode = igrab(&entry->vfs_inode);
5034 if (inode) {
5035 spin_unlock(&root->inode_lock);
5036 if (atomic_read(&inode->i_count) > 1)
5037 d_prune_aliases(inode);
5038 /*
Al Viro45321ac2010-06-07 13:43:19 -04005039 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04005040 * the inode cache when its usage count
5041 * hits zero.
5042 */
5043 iput(inode);
5044 cond_resched();
5045 spin_lock(&root->inode_lock);
5046 goto again;
5047 }
5048
5049 if (cond_resched_lock(&root->inode_lock))
5050 goto again;
5051
5052 node = rb_next(node);
5053 }
5054 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005055}
5056
Chris Masone02119d2008-09-05 16:13:11 -04005057static int btrfs_init_locked_inode(struct inode *inode, void *p)
5058{
5059 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005060 inode->i_ino = args->location->objectid;
5061 memcpy(&BTRFS_I(inode)->location, args->location,
5062 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005063 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005064 return 0;
5065}
5066
5067static int btrfs_find_actor(struct inode *inode, void *opaque)
5068{
5069 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005070 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005071 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005072}
5073
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005074static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005075 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005076 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005077{
5078 struct inode *inode;
5079 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005080 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005081
Chris Mason90d3e592014-01-09 17:28:00 -08005082 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005083 args.root = root;
5084
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005085 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005086 btrfs_init_locked_inode,
5087 (void *)&args);
5088 return inode;
5089}
5090
Balaji Rao1a54ef82008-07-21 02:01:04 +05305091/* Get an inode object given its location and corresponding root.
5092 * Returns in *is_new if the inode was read from disk
5093 */
5094struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005095 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305096{
5097 struct inode *inode;
5098
Chris Mason90d3e592014-01-09 17:28:00 -08005099 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305100 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005101 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305102
5103 if (inode->i_state & I_NEW) {
Balaji Rao1a54ef82008-07-21 02:01:04 +05305104 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005105 if (!is_bad_inode(inode)) {
5106 inode_tree_add(inode);
5107 unlock_new_inode(inode);
5108 if (new)
5109 *new = 1;
5110 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005111 unlock_new_inode(inode);
5112 iput(inode);
5113 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005114 }
5115 }
5116
Balaji Rao1a54ef82008-07-21 02:01:04 +05305117 return inode;
5118}
5119
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005120static struct inode *new_simple_dir(struct super_block *s,
5121 struct btrfs_key *key,
5122 struct btrfs_root *root)
5123{
5124 struct inode *inode = new_inode(s);
5125
5126 if (!inode)
5127 return ERR_PTR(-ENOMEM);
5128
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005129 BTRFS_I(inode)->root = root;
5130 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005131 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005132
5133 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005134 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005135 inode->i_fop = &simple_dir_operations;
5136 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
5137 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
5138
5139 return inode;
5140}
5141
Chris Mason3de45862008-11-17 21:02:50 -05005142struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005143{
Chris Masond3977122009-01-05 21:25:51 -05005144 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005145 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005146 struct btrfs_root *sub_root = root;
5147 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005148 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005149 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005150
5151 if (dentry->d_name.len > BTRFS_NAME_LEN)
5152 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005153
Jeff Layton39e3c952012-11-28 11:30:53 -05005154 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005155 if (ret < 0)
5156 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005157
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005158 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005159 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005160
5161 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005162 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005163 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005164 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005165
5166 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5167
Yan, Zheng76dda932009-09-21 16:00:26 -04005168 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005169 ret = fixup_tree_root_location(root, dir, dentry,
5170 &location, &sub_root);
5171 if (ret < 0) {
5172 if (ret != -ENOENT)
5173 inode = ERR_PTR(ret);
5174 else
5175 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5176 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005177 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005178 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005179 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
5180
Julia Lawall34d19ba2011-01-24 19:55:19 +00005181 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00005182 down_read(&root->fs_info->cleanup_work_sem);
5183 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005184 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005185 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005186 if (ret) {
5187 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005188 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005189 }
Filipe Manana9c3b3062014-07-31 14:41:07 +01005190 /*
5191 * If orphan cleanup did remove any orphans, it means the tree
5192 * was modified and therefore the commit root is not the same as
5193 * the current root anymore. This is a problem, because send
5194 * uses the commit root and therefore can see inode items that
5195 * don't exist in the current root anymore, and for example make
5196 * calls to btrfs_iget, which will do tree lookups based on the
5197 * current root and not on the commit root. Those lookups will
5198 * fail, returning a -ESTALE error, and making send fail with
5199 * that error. So make sure a send does not see any orphans we
5200 * have just removed, and that it will see the same inodes
5201 * regardless of whether a transaction commit happened before
5202 * it started (meaning that the commit root will be the same as
5203 * the current root) or not.
5204 */
5205 if (sub_root->node != sub_root->commit_root) {
5206 u64 sub_flags = btrfs_root_flags(&sub_root->root_item);
5207
5208 if (sub_flags & BTRFS_ROOT_SUBVOL_RDONLY) {
5209 struct extent_buffer *eb;
5210
5211 /*
5212 * Assert we can't have races between dentry
5213 * lookup called through the snapshot creation
5214 * ioctl and the VFS.
5215 */
5216 ASSERT(mutex_is_locked(&dir->i_mutex));
5217
5218 down_write(&root->fs_info->commit_root_sem);
5219 eb = sub_root->commit_root;
5220 sub_root->commit_root =
5221 btrfs_root_node(sub_root);
5222 up_write(&root->fs_info->commit_root_sem);
5223 free_extent_buffer(eb);
5224 }
5225 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005226 }
5227
Chris Mason3de45862008-11-17 21:02:50 -05005228 return inode;
5229}
5230
Nick Pigginfe15ce42011-01-07 17:49:23 +11005231static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005232{
5233 struct btrfs_root *root;
Li Zefan848cce02012-02-21 17:04:28 +08005234 struct inode *inode = dentry->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005235
Li Zefan848cce02012-02-21 17:04:28 +08005236 if (!inode && !IS_ROOT(dentry))
5237 inode = dentry->d_parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005238
Li Zefan848cce02012-02-21 17:04:28 +08005239 if (inode) {
5240 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005241 if (btrfs_root_refs(&root->root_item) == 0)
5242 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005243
5244 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5245 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005246 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005247 return 0;
5248}
5249
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005250static void btrfs_dentry_release(struct dentry *dentry)
5251{
Daeseok Youn944a4512014-04-14 15:37:02 +09005252 kfree(dentry->d_fsdata);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005253}
5254
Chris Mason3de45862008-11-17 21:02:50 -05005255static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005256 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005257{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005258 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005259
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005260 inode = btrfs_lookup_dentry(dir, dentry);
5261 if (IS_ERR(inode)) {
5262 if (PTR_ERR(inode) == -ENOENT)
5263 inode = NULL;
5264 else
5265 return ERR_CAST(inode);
5266 }
5267
Josef Bacik3a0dfa62014-02-14 13:43:48 -05005268 return d_materialise_unique(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005269}
5270
Miao Xie16cdcec2011-04-22 18:12:22 +08005271unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005272 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5273};
5274
Al Viro9cdda8d2013-05-22 16:48:09 -04005275static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005276{
Al Viro9cdda8d2013-05-22 16:48:09 -04005277 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005278 struct btrfs_root *root = BTRFS_I(inode)->root;
5279 struct btrfs_item *item;
5280 struct btrfs_dir_item *di;
5281 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005282 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005283 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005284 struct list_head ins_list;
5285 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005286 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005287 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005288 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005289 unsigned char d_type;
5290 int over = 0;
5291 u32 di_cur;
5292 u32 di_total;
5293 u32 di_len;
5294 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005295 char tmp_name[32];
5296 char *name_ptr;
5297 int name_len;
Al Viro9cdda8d2013-05-22 16:48:09 -04005298 int is_curr = 0; /* ctx->pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04005299
5300 /* FIXME, use a real flag for deciding about the key type */
5301 if (root->fs_info->tree_root == root)
5302 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005303
Al Viro9cdda8d2013-05-22 16:48:09 -04005304 if (!dir_emit_dots(file, ctx))
5305 return 0;
5306
David Woodhouse49593bf2008-08-17 17:08:36 +01005307 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005308 if (!path)
5309 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005310
Josef Bacik026fd312011-05-13 10:32:11 -04005311 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005312
Miao Xie16cdcec2011-04-22 18:12:22 +08005313 if (key_type == BTRFS_DIR_INDEX_KEY) {
5314 INIT_LIST_HEAD(&ins_list);
5315 INIT_LIST_HEAD(&del_list);
5316 btrfs_get_delayed_items(inode, &ins_list, &del_list);
5317 }
5318
Chris Mason39279cc2007-06-12 06:35:45 -04005319 btrfs_set_key_type(&key, key_type);
Al Viro9cdda8d2013-05-22 16:48:09 -04005320 key.offset = ctx->pos;
Li Zefan33345d012011-04-20 10:31:50 +08005321 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005322
Chris Mason39279cc2007-06-12 06:35:45 -04005323 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5324 if (ret < 0)
5325 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005326
5327 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005328 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005329 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005330 if (slot >= btrfs_header_nritems(leaf)) {
5331 ret = btrfs_next_leaf(root, path);
5332 if (ret < 0)
5333 goto err;
5334 else if (ret > 0)
5335 break;
5336 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005337 }
Chris Mason3de45862008-11-17 21:02:50 -05005338
Ross Kirkdd3cc162013-09-16 15:58:09 +01005339 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04005340 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5341
5342 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005343 break;
Chris Mason5f39d392007-10-15 16:14:19 -04005344 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04005345 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005346 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005347 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08005348 if (key_type == BTRFS_DIR_INDEX_KEY &&
5349 btrfs_should_delete_dir_index(&del_list,
5350 found_key.offset))
5351 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005352
Al Viro9cdda8d2013-05-22 16:48:09 -04005353 ctx->pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08005354 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005355
Chris Mason39279cc2007-06-12 06:35:45 -04005356 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
5357 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005358 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01005359
5360 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04005361 struct btrfs_key location;
5362
Josef Bacik22a94d42011-03-16 16:47:17 -04005363 if (verify_dir_item(root, leaf, di))
5364 break;
5365
Chris Mason5f39d392007-10-15 16:14:19 -04005366 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01005367 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04005368 name_ptr = tmp_name;
5369 } else {
5370 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01005371 if (!name_ptr) {
5372 ret = -ENOMEM;
5373 goto err;
5374 }
Chris Mason5f39d392007-10-15 16:14:19 -04005375 }
5376 read_extent_buffer(leaf, name_ptr,
5377 (unsigned long)(di + 1), name_len);
5378
5379 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5380 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05005381
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005382
Chris Mason3de45862008-11-17 21:02:50 -05005383 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01005384 * skip it.
5385 *
5386 * In contrast to old kernels, we insert the snapshot's
5387 * dir item and dir index after it has been created, so
5388 * we won't find a reference to our own snapshot. We
5389 * still keep the following code for backward
5390 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05005391 */
5392 if (location.type == BTRFS_ROOT_ITEM_KEY &&
5393 location.objectid == root->root_key.objectid) {
5394 over = 0;
5395 goto skip;
5396 }
Al Viro9cdda8d2013-05-22 16:48:09 -04005397 over = !dir_emit(ctx, name_ptr, name_len,
5398 location.objectid, d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04005399
Chris Mason3de45862008-11-17 21:02:50 -05005400skip:
Chris Mason5f39d392007-10-15 16:14:19 -04005401 if (name_ptr != tmp_name)
5402 kfree(name_ptr);
5403
Chris Mason39279cc2007-06-12 06:35:45 -04005404 if (over)
5405 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05005406 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01005407 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04005408 di_cur += di_len;
5409 di = (struct btrfs_dir_item *)((char *)di + di_len);
5410 }
Li Zefanb9e03af2011-03-23 10:43:58 +08005411next:
5412 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005413 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005414
Miao Xie16cdcec2011-04-22 18:12:22 +08005415 if (key_type == BTRFS_DIR_INDEX_KEY) {
5416 if (is_curr)
Al Viro9cdda8d2013-05-22 16:48:09 -04005417 ctx->pos++;
5418 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005419 if (ret)
5420 goto nopos;
5421 }
5422
David Woodhouse49593bf2008-08-17 17:08:36 +01005423 /* Reached end of directory/root. Bump pos past the last item. */
Zach Browndb62efb2013-07-11 16:19:42 -07005424 ctx->pos++;
5425
5426 /*
5427 * Stop new entries from being returned after we return the last
5428 * entry.
5429 *
5430 * New directory entries are assigned a strictly increasing
5431 * offset. This means that new entries created during readdir
5432 * are *guaranteed* to be seen in the future by that readdir.
5433 * This has broken buggy programs which operate on names as
5434 * they're returned by readdir. Until we re-use freed offsets
5435 * we have this hack to stop new entries from being returned
5436 * under the assumption that they'll never reach this huge
5437 * offset.
5438 *
5439 * This is being careful not to overflow 32bit loff_t unless the
5440 * last entry requires it because doing so has broken 32bit apps
5441 * in the past.
5442 */
5443 if (key_type == BTRFS_DIR_INDEX_KEY) {
5444 if (ctx->pos >= INT_MAX)
5445 ctx->pos = LLONG_MAX;
5446 else
5447 ctx->pos = INT_MAX;
5448 }
Chris Mason39279cc2007-06-12 06:35:45 -04005449nopos:
5450 ret = 0;
5451err:
Miao Xie16cdcec2011-04-22 18:12:22 +08005452 if (key_type == BTRFS_DIR_INDEX_KEY)
5453 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005454 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005455 return ret;
5456}
5457
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005458int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005459{
5460 struct btrfs_root *root = BTRFS_I(inode)->root;
5461 struct btrfs_trans_handle *trans;
5462 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005463 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005464
Josef Bacik72ac3c02012-05-23 14:13:11 -04005465 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005466 return 0;
5467
Liu Bo83eea1f2012-07-10 05:28:39 -06005468 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08005469 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005470
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005471 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005472 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005473 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005474 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005475 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005476 if (IS_ERR(trans))
5477 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06005478 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005479 }
5480 return ret;
5481}
5482
5483/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005484 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005485 * inode changes. But, it is most likely to find the inode in cache.
5486 * FIXME, needs more benchmarking...there are no reasons other than performance
5487 * to keep or drop this code.
5488 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005489static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005490{
5491 struct btrfs_root *root = BTRFS_I(inode)->root;
5492 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005493 int ret;
5494
Josef Bacik72ac3c02012-05-23 14:13:11 -04005495 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005496 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005497
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005498 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005499 if (IS_ERR(trans))
5500 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005501
5502 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005503 if (ret && ret == -ENOSPC) {
5504 /* whoops, lets try again with the full transaction */
5505 btrfs_end_transaction(trans, root);
5506 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005507 if (IS_ERR(trans))
5508 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005509
Chris Mason94b60442010-05-26 11:02:00 -04005510 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005511 }
Chris Mason39279cc2007-06-12 06:35:45 -04005512 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08005513 if (BTRFS_I(inode)->delayed_node)
5514 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005515
5516 return ret;
5517}
5518
5519/*
5520 * This is a copy of file_update_time. We need this so we can return error on
5521 * ENOSPC for updating the inode in the case of file write and mmap writes.
5522 */
Josef Bacike41f9412012-03-26 09:46:47 -04005523static int btrfs_update_time(struct inode *inode, struct timespec *now,
5524 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005525{
Alexander Block2bc5565282012-06-15 09:49:33 +02005526 struct btrfs_root *root = BTRFS_I(inode)->root;
5527
5528 if (btrfs_root_readonly(root))
5529 return -EROFS;
5530
Josef Bacike41f9412012-03-26 09:46:47 -04005531 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005532 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04005533 if (flags & S_CTIME)
5534 inode->i_ctime = *now;
5535 if (flags & S_MTIME)
5536 inode->i_mtime = *now;
5537 if (flags & S_ATIME)
5538 inode->i_atime = *now;
5539 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005540}
5541
Chris Masond352ac62008-09-29 15:18:18 -04005542/*
5543 * find the highest existing sequence number in a directory
5544 * and then set the in-memory index_cnt variable to reflect
5545 * free sequence numbers
5546 */
Josef Bacikaec74772008-07-24 12:12:38 -04005547static int btrfs_set_inode_index_count(struct inode *inode)
5548{
5549 struct btrfs_root *root = BTRFS_I(inode)->root;
5550 struct btrfs_key key, found_key;
5551 struct btrfs_path *path;
5552 struct extent_buffer *leaf;
5553 int ret;
5554
Li Zefan33345d012011-04-20 10:31:50 +08005555 key.objectid = btrfs_ino(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005556 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
5557 key.offset = (u64)-1;
5558
5559 path = btrfs_alloc_path();
5560 if (!path)
5561 return -ENOMEM;
5562
5563 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5564 if (ret < 0)
5565 goto out;
5566 /* FIXME: we should be able to handle this */
5567 if (ret == 0)
5568 goto out;
5569 ret = 0;
5570
5571 /*
5572 * MAGIC NUMBER EXPLANATION:
5573 * since we search a directory based on f_pos we have to start at 2
5574 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5575 * else has to start at 2
5576 */
5577 if (path->slots[0] == 0) {
5578 BTRFS_I(inode)->index_cnt = 2;
5579 goto out;
5580 }
5581
5582 path->slots[0]--;
5583
5584 leaf = path->nodes[0];
5585 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5586
Li Zefan33345d012011-04-20 10:31:50 +08005587 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacikaec74772008-07-24 12:12:38 -04005588 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
5589 BTRFS_I(inode)->index_cnt = 2;
5590 goto out;
5591 }
5592
5593 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
5594out:
5595 btrfs_free_path(path);
5596 return ret;
5597}
5598
Chris Masond352ac62008-09-29 15:18:18 -04005599/*
5600 * helper to find a free sequence number in a given directory. This current
5601 * code is very simple, later versions will do smarter things in the btree
5602 */
Chris Mason3de45862008-11-17 21:02:50 -05005603int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04005604{
5605 int ret = 0;
5606
5607 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08005608 ret = btrfs_inode_delayed_dir_index_count(dir);
5609 if (ret) {
5610 ret = btrfs_set_inode_index_count(dir);
5611 if (ret)
5612 return ret;
5613 }
Josef Bacikaec74772008-07-24 12:12:38 -04005614 }
5615
Chris Mason00e4e6b2008-08-05 11:18:09 -04005616 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04005617 BTRFS_I(dir)->index_cnt++;
5618
5619 return ret;
5620}
5621
Chris Mason39279cc2007-06-12 06:35:45 -04005622static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5623 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04005624 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05005625 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04005626 u64 ref_objectid, u64 objectid,
5627 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04005628{
5629 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005630 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04005631 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04005632 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05005633 struct btrfs_inode_ref *ref;
5634 struct btrfs_key key[2];
5635 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005636 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05005637 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04005638 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005639
Chris Mason5f39d392007-10-15 16:14:19 -04005640 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005641 if (!path)
5642 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04005643
Chris Mason39279cc2007-06-12 06:35:45 -04005644 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005645 if (!inode) {
5646 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005647 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005648 }
Chris Mason39279cc2007-06-12 06:35:45 -04005649
Li Zefan581bb052011-04-20 10:06:11 +08005650 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01005651 * O_TMPFILE, set link count to 0, so that after this point,
5652 * we fill in an inode item with the correct link count.
5653 */
5654 if (!name)
5655 set_nlink(inode, 0);
5656
5657 /*
Li Zefan581bb052011-04-20 10:06:11 +08005658 * we have to initialize this early, so we can reclaim the inode
5659 * number if we fail afterwards in this function.
5660 */
5661 inode->i_ino = objectid;
5662
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005663 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00005664 trace_btrfs_inode_request(dir);
5665
Chris Mason3de45862008-11-17 21:02:50 -05005666 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04005667 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005668 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005669 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005670 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04005671 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005672 } else if (dir) {
5673 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04005674 }
5675 /*
5676 * index_cnt is ignored for everything but a dir,
5677 * btrfs_get_inode_index_count has an explanation for the magic
5678 * number
5679 */
5680 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08005681 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04005682 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04005683 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00005684 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04005685
Josef Bacik5dc562c2012-08-17 13:14:17 -04005686 /*
5687 * We could have gotten an inode number from somebody who was fsynced
5688 * and then removed in this same transaction, so let's just set full
5689 * sync since it will be a full sync anyway and this will blow away the
5690 * old info in the log.
5691 */
5692 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
5693
Chris Mason9c583092008-01-29 15:15:18 -05005694 key[0].objectid = objectid;
5695 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
5696 key[0].offset = 0;
5697
Chris Mason9c583092008-01-29 15:15:18 -05005698 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005699
5700 if (name) {
5701 /*
5702 * Start new inodes with an inode_ref. This is slightly more
5703 * efficient for small numbers of hard links since they will
5704 * be packed into one item. Extended refs will kick in if we
5705 * add more hard links than can fit in the ref item.
5706 */
5707 key[1].objectid = objectid;
5708 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
5709 key[1].offset = ref_objectid;
5710
5711 sizes[1] = name_len + sizeof(*ref);
5712 }
Chris Mason9c583092008-01-29 15:15:18 -05005713
Chris Masonb9473432009-03-13 11:00:37 -04005714 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005715 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05005716 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04005717 goto fail;
5718
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03005719 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04005720 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005721 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04005722 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
5723 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06005724 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
5725 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04005726 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05005727
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005728 if (name) {
5729 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
5730 struct btrfs_inode_ref);
5731 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
5732 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
5733 ptr = (unsigned long)(ref + 1);
5734 write_extent_buffer(path->nodes[0], name, ptr, name_len);
5735 }
Chris Mason9c583092008-01-29 15:15:18 -05005736
Chris Mason5f39d392007-10-15 16:14:19 -04005737 btrfs_mark_buffer_dirty(path->nodes[0]);
5738 btrfs_free_path(path);
5739
Chris Mason39279cc2007-06-12 06:35:45 -04005740 location = &BTRFS_I(inode)->location;
5741 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04005742 location->offset = 0;
5743 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
5744
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005745 btrfs_inherit_iflags(inode, dir);
5746
Al Viro569254b2011-07-24 17:08:40 -04005747 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04005748 if (btrfs_test_opt(root, NODATASUM))
5749 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo213490b2012-09-11 08:33:50 -06005750 if (btrfs_test_opt(root, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05005751 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
5752 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04005753 }
5754
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005755 btrfs_insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005756 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00005757
5758 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04005759 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00005760
Alexander Block8ea05e32012-07-25 17:35:53 +02005761 btrfs_update_root_times(trans, root);
5762
Filipe David Borba Manana63541922014-01-07 11:47:46 +00005763 ret = btrfs_inode_inherit_props(trans, inode, dir);
5764 if (ret)
5765 btrfs_err(root->fs_info,
5766 "error inheriting props for ino %llu (root %llu): %d",
5767 btrfs_ino(inode), root->root_key.objectid, ret);
5768
Chris Mason39279cc2007-06-12 06:35:45 -04005769 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005770fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005771 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04005772 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04005773 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005774 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005775 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005776}
5777
5778static inline u8 btrfs_inode_type(struct inode *inode)
5779{
5780 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
5781}
5782
Chris Masond352ac62008-09-29 15:18:18 -04005783/*
5784 * utility function to add 'inode' into 'parent_inode' with
5785 * a give name and a given sequence number.
5786 * if 'add_backref' is true, also insert a backref from the
5787 * inode to the parent directory.
5788 */
Chris Masone02119d2008-09-05 16:13:11 -04005789int btrfs_add_link(struct btrfs_trans_handle *trans,
5790 struct inode *parent_inode, struct inode *inode,
5791 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005792{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005793 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005794 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04005795 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08005796 u64 ino = btrfs_ino(inode);
5797 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005798
Li Zefan33345d012011-04-20 10:31:50 +08005799 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005800 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
5801 } else {
Li Zefan33345d012011-04-20 10:31:50 +08005802 key.objectid = ino;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005803 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
5804 key.offset = 0;
5805 }
Chris Mason39279cc2007-06-12 06:35:45 -04005806
Li Zefan33345d012011-04-20 10:31:50 +08005807 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005808 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
5809 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08005810 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005811 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08005812 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
5813 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005814 }
5815
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005816 /* Nothing to clean up yet */
5817 if (ret)
5818 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005819
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005820 ret = btrfs_insert_dir_item(trans, root, name, name_len,
5821 parent_inode, &key,
5822 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05005823 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005824 goto fail_dir_item;
5825 else if (ret) {
5826 btrfs_abort_transaction(trans, root, ret);
5827 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005828 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005829
5830 btrfs_i_size_write(parent_inode, parent_inode->i_size +
5831 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005832 inode_inc_iversion(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005833 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
5834 ret = btrfs_update_inode(trans, root, parent_inode);
5835 if (ret)
5836 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005837 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05005838
5839fail_dir_item:
5840 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
5841 u64 local_index;
5842 int err;
5843 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
5844 key.objectid, root->root_key.objectid,
5845 parent_ino, &local_index, name, name_len);
5846
5847 } else if (add_backref) {
5848 u64 local_index;
5849 int err;
5850
5851 err = btrfs_del_inode_ref(trans, root, name, name_len,
5852 ino, parent_ino, &local_index);
5853 }
5854 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005855}
5856
5857static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00005858 struct inode *dir, struct dentry *dentry,
5859 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005860{
Josef Bacika1b075d2010-11-19 20:36:11 +00005861 int err = btrfs_add_link(trans, dir, inode,
5862 dentry->d_name.name, dentry->d_name.len,
5863 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005864 if (err > 0)
5865 err = -EEXIST;
5866 return err;
5867}
5868
Josef Bacik618e21d2007-07-11 10:18:17 -04005869static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04005870 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04005871{
5872 struct btrfs_trans_handle *trans;
5873 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005874 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04005875 int err;
5876 int drop_inode = 0;
5877 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005878 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04005879
5880 if (!new_valid_dev(rdev))
5881 return -EINVAL;
5882
Josef Bacik9ed74f22009-09-11 16:12:44 -04005883 /*
5884 * 2 for inode item and ref
5885 * 2 for dir items
5886 * 1 for xattr if selinux is on
5887 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005888 trans = btrfs_start_transaction(root, 5);
5889 if (IS_ERR(trans))
5890 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05005891
Li Zefan581bb052011-04-20 10:06:11 +08005892 err = btrfs_find_free_ino(root, &objectid);
5893 if (err)
5894 goto out_unlock;
5895
Josef Bacikaec74772008-07-24 12:12:38 -04005896 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005897 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005898 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005899 if (IS_ERR(inode)) {
5900 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005901 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005902 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005903
Eric Paris2a7dba32011-02-01 11:05:39 -05005904 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005905 if (err) {
5906 drop_inode = 1;
5907 goto out_unlock;
5908 }
5909
Casey Schauflerad19db72011-12-15 10:09:07 -05005910 /*
5911 * If the active LSM wants to access the inode during
5912 * d_instantiate it needs these. Smack checks to see
5913 * if the filesystem supports xattrs by looking at the
5914 * ops vector.
5915 */
5916
5917 inode->i_op = &btrfs_special_inode_operations;
Josef Bacika1b075d2010-11-19 20:36:11 +00005918 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04005919 if (err)
5920 drop_inode = 1;
5921 else {
Josef Bacik618e21d2007-07-11 10:18:17 -04005922 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04005923 btrfs_update_inode(trans, root, inode);
Al Viro08c422c2011-12-23 07:58:13 -05005924 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005925 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005926out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005927 btrfs_end_transaction(trans, root);
Josef Bacikc581afc2014-02-06 16:06:06 -05005928 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00005929 btrfs_btree_balance_dirty(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04005930 if (drop_inode) {
5931 inode_dec_link_count(inode);
5932 iput(inode);
5933 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005934 return err;
5935}
5936
Chris Mason39279cc2007-06-12 06:35:45 -04005937static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04005938 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04005939{
5940 struct btrfs_trans_handle *trans;
5941 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005942 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005943 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04005944 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04005945 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005946 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005947
Josef Bacik9ed74f22009-09-11 16:12:44 -04005948 /*
5949 * 2 for inode item and ref
5950 * 2 for dir items
5951 * 1 for xattr if selinux is on
5952 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005953 trans = btrfs_start_transaction(root, 5);
5954 if (IS_ERR(trans))
5955 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005956
Li Zefan581bb052011-04-20 10:06:11 +08005957 err = btrfs_find_free_ino(root, &objectid);
5958 if (err)
5959 goto out_unlock;
5960
Josef Bacikaec74772008-07-24 12:12:38 -04005961 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005962 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005963 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005964 if (IS_ERR(inode)) {
5965 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005966 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005967 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005968 drop_inode_on_err = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04005969
Eric Paris2a7dba32011-02-01 11:05:39 -05005970 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005971 if (err)
Josef Bacik33268ea2008-07-24 12:16:36 -04005972 goto out_unlock;
Josef Bacik33268ea2008-07-24 12:16:36 -04005973
Filipe Brandenburger9185aa52012-11-30 03:40:08 +00005974 err = btrfs_update_inode(trans, root, inode);
5975 if (err)
5976 goto out_unlock;
5977
Casey Schauflerad19db72011-12-15 10:09:07 -05005978 /*
5979 * If the active LSM wants to access the inode during
5980 * d_instantiate it needs these. Smack checks to see
5981 * if the filesystem supports xattrs by looking at the
5982 * ops vector.
5983 */
5984 inode->i_fop = &btrfs_file_operations;
5985 inode->i_op = &btrfs_file_inode_operations;
5986
Josef Bacika1b075d2010-11-19 20:36:11 +00005987 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005988 if (err)
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005989 goto out_unlock;
5990
5991 inode->i_mapping->a_ops = &btrfs_aops;
5992 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
5993 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
5994 d_instantiate(dentry, inode);
5995
Chris Mason39279cc2007-06-12 06:35:45 -04005996out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005997 btrfs_end_transaction(trans, root);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005998 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005999 inode_dec_link_count(inode);
6000 iput(inode);
6001 }
Josef Bacikc581afc2014-02-06 16:06:06 -05006002 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006003 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006004 return err;
6005}
6006
6007static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6008 struct dentry *dentry)
6009{
6010 struct btrfs_trans_handle *trans;
6011 struct btrfs_root *root = BTRFS_I(dir)->root;
6012 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006013 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006014 int err;
6015 int drop_inode = 0;
6016
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006017 /* do not allow sys_link's with other subvols of the same device */
6018 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006019 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006020
Mark Fashehf1863732012-08-08 11:32:27 -07006021 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006022 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006023
Chris Mason3de45862008-11-17 21:02:50 -05006024 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006025 if (err)
6026 goto fail;
6027
Yan, Zhenga22285a2010-05-16 10:48:46 -04006028 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006029 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006030 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006031 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04006032 */
Miao Xie7e6b6462011-02-18 09:21:17 +00006033 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006034 if (IS_ERR(trans)) {
6035 err = PTR_ERR(trans);
6036 goto fail;
6037 }
Chris Mason5f39d392007-10-15 16:14:19 -04006038
Miao Xie67de1172013-12-26 13:07:06 +08006039 /* There are several dir indexes for this inode, clear the cache. */
6040 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006041 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006042 inode_inc_iversion(inode);
Miao Xie31534952011-04-13 13:19:21 +08006043 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6ee2010-10-23 11:11:40 -04006044 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006045 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006046
Josef Bacika1b075d2010-11-19 20:36:11 +00006047 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006048
Yan, Zhenga5719522009-09-24 09:17:31 -04006049 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006050 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006051 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006052 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006053 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006054 if (err)
6055 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006056 if (inode->i_nlink == 1) {
6057 /*
6058 * If new hard link count is 1, it's a file created
6059 * with open(2) O_TMPFILE flag.
6060 */
6061 err = btrfs_orphan_del(trans, inode);
6062 if (err)
6063 goto fail;
6064 }
Al Viro08c422c2011-12-23 07:58:13 -05006065 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00006066 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006067 }
Chris Mason39279cc2007-06-12 06:35:45 -04006068
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006069 btrfs_end_transaction(trans, root);
Josef Bacikc581afc2014-02-06 16:06:06 -05006070 btrfs_balance_delayed_items(root);
Chris Mason1832a6d2007-12-21 16:27:21 -05006071fail:
Chris Mason39279cc2007-06-12 06:35:45 -04006072 if (drop_inode) {
6073 inode_dec_link_count(inode);
6074 iput(inode);
6075 }
Liu Bob53d3f52012-11-14 14:34:34 +00006076 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006077 return err;
6078}
6079
Al Viro18bb1db2011-07-26 01:41:39 -04006080static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006081{
Chris Masonb9d86662008-05-02 16:13:49 -04006082 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006083 struct btrfs_trans_handle *trans;
6084 struct btrfs_root *root = BTRFS_I(dir)->root;
6085 int err = 0;
6086 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006087 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006088 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006089
Josef Bacik9ed74f22009-09-11 16:12:44 -04006090 /*
6091 * 2 items for inode and ref
6092 * 2 items for dir items
6093 * 1 for xattr if selinux is on
6094 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006095 trans = btrfs_start_transaction(root, 5);
6096 if (IS_ERR(trans))
6097 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006098
Li Zefan581bb052011-04-20 10:06:11 +08006099 err = btrfs_find_free_ino(root, &objectid);
6100 if (err)
6101 goto out_fail;
6102
Josef Bacikaec74772008-07-24 12:12:38 -04006103 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006104 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04006105 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006106 if (IS_ERR(inode)) {
6107 err = PTR_ERR(inode);
6108 goto out_fail;
6109 }
Chris Mason5f39d392007-10-15 16:14:19 -04006110
Chris Mason39279cc2007-06-12 06:35:45 -04006111 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04006112
Eric Paris2a7dba32011-02-01 11:05:39 -05006113 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006114 if (err)
6115 goto out_fail;
6116
Chris Mason39279cc2007-06-12 06:35:45 -04006117 inode->i_op = &btrfs_dir_inode_operations;
6118 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04006119
Chris Masondbe674a2008-07-17 12:54:05 -04006120 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006121 err = btrfs_update_inode(trans, root, inode);
6122 if (err)
6123 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006124
Josef Bacika1b075d2010-11-19 20:36:11 +00006125 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
6126 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006127 if (err)
6128 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006129
Chris Mason39279cc2007-06-12 06:35:45 -04006130 d_instantiate(dentry, inode);
6131 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006132
6133out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006134 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04006135 if (drop_on_err)
6136 iput(inode);
Josef Bacikc581afc2014-02-06 16:06:06 -05006137 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006138 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006139 return err;
6140}
6141
Chris Masond352ac62008-09-29 15:18:18 -04006142/* helper for btfs_get_extent. Given an existing extent in the tree,
6143 * and an extent that you want to insert, deal with overlap and insert
6144 * the new extent into the tree.
6145 */
Chris Mason3b951512008-04-17 11:29:12 -04006146static int merge_extent_mapping(struct extent_map_tree *em_tree,
6147 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006148 struct extent_map *em,
Qu Wenruo51f395a2014-08-08 13:06:20 +08006149 u64 map_start)
Chris Mason3b951512008-04-17 11:29:12 -04006150{
6151 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04006152
Chris Masone6dcd2d2008-07-17 12:53:50 -04006153 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
6154 start_diff = map_start - em->start;
6155 em->start = map_start;
Qu Wenruo51f395a2014-08-08 13:06:20 +08006156 em->len = existing->start - em->start;
Chris Masonc8b97812008-10-29 14:49:59 -04006157 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
6158 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006159 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04006160 em->block_len -= start_diff;
6161 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006162 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006163}
6164
Chris Masonc8b97812008-10-29 14:49:59 -04006165static noinline int uncompress_inline(struct btrfs_path *path,
6166 struct inode *inode, struct page *page,
6167 size_t pg_offset, u64 extent_offset,
6168 struct btrfs_file_extent_item *item)
6169{
6170 int ret;
6171 struct extent_buffer *leaf = path->nodes[0];
6172 char *tmp;
6173 size_t max_size;
6174 unsigned long inline_size;
6175 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006176 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006177
6178 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006179 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006180 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6181 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006182 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006183 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006184 if (!tmp)
6185 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006186 ptr = btrfs_file_extent_inline_start(item);
6187
6188 read_extent_buffer(leaf, tmp, ptr, inline_size);
6189
Chris Mason5b050f02008-11-11 09:34:41 -05006190 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006191 ret = btrfs_decompress(compress_type, tmp, page,
6192 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006193 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006194 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006195}
6196
Chris Masond352ac62008-09-29 15:18:18 -04006197/*
6198 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006199 * the ugly parts come from merging extents from the disk with the in-ram
6200 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006201 * where the in-ram extents might be locked pending data=ordered completion.
6202 *
6203 * This also copies inline extents directly into the page.
6204 */
Chris Masond3977122009-01-05 21:25:51 -05006205
Chris Masona52d9a82007-08-27 16:49:44 -04006206struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05006207 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04006208 int create)
6209{
6210 int ret;
6211 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006212 u64 extent_start = 0;
6213 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08006214 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006215 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006216 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04006217 struct btrfs_root *root = BTRFS_I(inode)->root;
6218 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006219 struct extent_buffer *leaf;
6220 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006221 struct extent_map *em = NULL;
6222 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05006223 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006224 struct btrfs_trans_handle *trans = NULL;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006225 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006226
Chris Masona52d9a82007-08-27 16:49:44 -04006227again:
Chris Mason890871b2009-09-02 16:24:52 -04006228 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006229 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006230 if (em)
6231 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006232 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006233
Chris Masona52d9a82007-08-27 16:49:44 -04006234 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006235 if (em->start > start || em->start + em->len <= start)
6236 free_extent_map(em);
6237 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006238 free_extent_map(em);
6239 else
6240 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006241 }
David Sterba172ddd62011-04-21 00:48:27 +02006242 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006243 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006244 err = -ENOMEM;
6245 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006246 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006247 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006248 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006249 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006250 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006251 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006252
6253 if (!path) {
6254 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006255 if (!path) {
6256 err = -ENOMEM;
6257 goto out;
6258 }
6259 /*
6260 * Chances are we'll be called again, so go ahead and do
6261 * readahead
6262 */
6263 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04006264 }
6265
Chris Mason179e29e2007-11-01 11:28:41 -04006266 ret = btrfs_lookup_file_extent(trans, root, path,
6267 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006268 if (ret < 0) {
6269 err = ret;
6270 goto out;
6271 }
6272
6273 if (ret != 0) {
6274 if (path->slots[0] == 0)
6275 goto not_found;
6276 path->slots[0]--;
6277 }
6278
Chris Mason5f39d392007-10-15 16:14:19 -04006279 leaf = path->nodes[0];
6280 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006281 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006282 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006283 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6284 found_type = btrfs_key_type(&found_key);
6285 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006286 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006287 /*
6288 * If we backup past the first extent we want to move forward
6289 * and see if there is an extent in front of us, otherwise we'll
6290 * say there is a hole for our whole search range which can
6291 * cause problems.
6292 */
6293 extent_end = start;
6294 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006295 }
6296
Chris Mason5f39d392007-10-15 16:14:19 -04006297 found_type = btrfs_file_extent_type(leaf, item);
6298 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04006299 if (found_type == BTRFS_FILE_EXTENT_REG ||
6300 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006301 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006302 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006303 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6304 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08006305 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Qu Wenruofda28322013-02-26 08:10:22 +00006306 extent_end = ALIGN(extent_start + size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04006307 }
Josef Bacik25a50342013-10-14 12:08:38 -04006308next:
Yan Zheng9036c102008-10-30 14:19:41 -04006309 if (start >= extent_end) {
6310 path->slots[0]++;
6311 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6312 ret = btrfs_next_leaf(root, path);
6313 if (ret < 0) {
6314 err = ret;
6315 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006316 }
Yan Zheng9036c102008-10-30 14:19:41 -04006317 if (ret > 0)
6318 goto not_found;
6319 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006320 }
Yan Zheng9036c102008-10-30 14:19:41 -04006321 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6322 if (found_key.objectid != objectid ||
6323 found_key.type != BTRFS_EXTENT_DATA_KEY)
6324 goto not_found;
6325 if (start + len <= found_key.offset)
6326 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006327 if (start > found_key.offset)
6328 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04006329 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006330 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006331 em->len = found_key.offset - start;
6332 goto not_found_em;
6333 }
6334
Filipe Manana7ffbb592014-06-09 03:48:05 +01006335 btrfs_extent_item_to_extent_map(inode, path, item, new_inline, em);
6336
Yan Zhengd899e052008-10-30 14:25:28 -04006337 if (found_type == BTRFS_FILE_EXTENT_REG ||
6338 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006339 goto insert;
6340 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006341 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006342 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006343 size_t size;
6344 size_t extent_offset;
6345 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006346
Filipe Manana7ffbb592014-06-09 03:48:05 +01006347 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04006348 goto out;
Yan689f9342007-10-29 11:41:07 -04006349
Chris Mason514ac8a2014-01-03 21:07:00 -08006350 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04006351 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05006352 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04006353 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006354 em->start = extent_start + extent_offset;
Qu Wenruofda28322013-02-26 08:10:22 +00006355 em->len = ALIGN(copy_size, root->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006356 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006357 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006358 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006359 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006360 if (btrfs_file_extent_compression(leaf, item) !=
6361 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006362 ret = uncompress_inline(path, inode, page,
6363 pg_offset,
6364 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04006365 if (ret) {
6366 err = ret;
6367 goto out;
6368 }
Chris Masonc8b97812008-10-29 14:49:59 -04006369 } else {
6370 map = kmap(page);
6371 read_extent_buffer(leaf, map + pg_offset, ptr,
6372 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04006373 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
6374 memset(map + pg_offset + copy_size, 0,
6375 PAGE_CACHE_SIZE - pg_offset -
6376 copy_size);
6377 }
Chris Masonc8b97812008-10-29 14:49:59 -04006378 kunmap(page);
6379 }
Chris Mason179e29e2007-11-01 11:28:41 -04006380 flush_dcache_page(page);
6381 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006382 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006383 if (!trans) {
6384 kunmap(page);
6385 free_extent_map(em);
6386 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006387
David Sterbab3b4aa72011-04-21 01:20:15 +02006388 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006389 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006390
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006391 if (IS_ERR(trans))
6392 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006393 goto again;
6394 }
Chris Masonc8b97812008-10-29 14:49:59 -04006395 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006396 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006397 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006398 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006399 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006400 }
Chris Masond1310b22008-01-24 16:13:08 -05006401 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006402 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006403 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04006404 }
6405not_found:
6406 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006407 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006408 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006409not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04006410 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04006411 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006412insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006413 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006414 if (em->start > start || extent_map_end(em) <= start) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006415 btrfs_err(root->fs_info, "bad extent! em: [%llu %llu] passed [%llu %llu]",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006416 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04006417 err = -EIO;
6418 goto out;
6419 }
Chris Masond1310b22008-01-24 16:13:08 -05006420
6421 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006422 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006423 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006424 /* it is possible that someone inserted the extent into the tree
6425 * while we had the lock dropped. It is also possible that
6426 * an overlapping map exists in the tree
6427 */
Chris Masona52d9a82007-08-27 16:49:44 -04006428 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04006429 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006430
6431 ret = 0;
6432
Chris Mason3b951512008-04-17 11:29:12 -04006433 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04006434 if (existing && (existing->start > start ||
6435 existing->start + existing->len <= start)) {
6436 free_extent_map(existing);
6437 existing = NULL;
6438 }
Chris Mason3b951512008-04-17 11:29:12 -04006439 if (!existing) {
6440 existing = lookup_extent_mapping(em_tree, em->start,
6441 em->len);
6442 if (existing) {
6443 err = merge_extent_mapping(em_tree, existing,
Qu Wenruo51f395a2014-08-08 13:06:20 +08006444 em, start);
Chris Mason3b951512008-04-17 11:29:12 -04006445 free_extent_map(existing);
6446 if (err) {
6447 free_extent_map(em);
6448 em = NULL;
6449 }
6450 } else {
6451 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04006452 free_extent_map(em);
6453 em = NULL;
6454 }
6455 } else {
6456 free_extent_map(em);
6457 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006458 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006459 }
Chris Masona52d9a82007-08-27 16:49:44 -04006460 }
Chris Mason890871b2009-09-02 16:24:52 -04006461 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006462out:
liubo1abe9b82011-03-24 11:18:59 +00006463
Steven Rostedt4cd8587c2013-11-14 22:57:29 -05006464 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00006465
Chris Masonf4219502008-07-22 11:18:09 -04006466 if (path)
6467 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04006468 if (trans) {
6469 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05006470 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04006471 err = ret;
6472 }
Chris Masona52d9a82007-08-27 16:49:44 -04006473 if (err) {
6474 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04006475 return ERR_PTR(err);
6476 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006477 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04006478 return em;
6479}
6480
Chris Masonec29ed52011-02-23 16:23:20 -05006481struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
6482 size_t pg_offset, u64 start, u64 len,
6483 int create)
6484{
6485 struct extent_map *em;
6486 struct extent_map *hole_em = NULL;
6487 u64 range_start = start;
6488 u64 end;
6489 u64 found;
6490 u64 found_end;
6491 int err = 0;
6492
6493 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
6494 if (IS_ERR(em))
6495 return em;
6496 if (em) {
6497 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00006498 * if our em maps to
6499 * - a hole or
6500 * - a pre-alloc extent,
6501 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05006502 */
Liu Bof9e4fb52013-01-07 10:10:12 +00006503 if (em->block_start != EXTENT_MAP_HOLE &&
6504 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05006505 return em;
6506 else
6507 hole_em = em;
6508 }
6509
6510 /* check to see if we've wrapped (len == -1 or similar) */
6511 end = start + len;
6512 if (end < start)
6513 end = (u64)-1;
6514 else
6515 end -= 1;
6516
6517 em = NULL;
6518
6519 /* ok, we didn't find anything, lets look for delalloc */
6520 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
6521 end, len, EXTENT_DELALLOC, 1);
6522 found_end = range_start + found;
6523 if (found_end < range_start)
6524 found_end = (u64)-1;
6525
6526 /*
6527 * we didn't find anything useful, return
6528 * the original results from get_extent()
6529 */
6530 if (range_start > end || found_end <= start) {
6531 em = hole_em;
6532 hole_em = NULL;
6533 goto out;
6534 }
6535
6536 /* adjust the range_start to make sure it doesn't
6537 * go backwards from the start they passed in
6538 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05306539 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05006540 found = found_end - range_start;
6541
6542 if (found > 0) {
6543 u64 hole_start = start;
6544 u64 hole_len = len;
6545
David Sterba172ddd62011-04-21 00:48:27 +02006546 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05006547 if (!em) {
6548 err = -ENOMEM;
6549 goto out;
6550 }
6551 /*
6552 * when btrfs_get_extent can't find anything it
6553 * returns one huge hole
6554 *
6555 * make sure what it found really fits our range, and
6556 * adjust to make sure it is based on the start from
6557 * the caller
6558 */
6559 if (hole_em) {
6560 u64 calc_end = extent_map_end(hole_em);
6561
6562 if (calc_end <= start || (hole_em->start > end)) {
6563 free_extent_map(hole_em);
6564 hole_em = NULL;
6565 } else {
6566 hole_start = max(hole_em->start, start);
6567 hole_len = calc_end - hole_start;
6568 }
6569 }
6570 em->bdev = NULL;
6571 if (hole_em && range_start > hole_start) {
6572 /* our hole starts before our delalloc, so we
6573 * have to return just the parts of the hole
6574 * that go until the delalloc starts
6575 */
6576 em->len = min(hole_len,
6577 range_start - hole_start);
6578 em->start = hole_start;
6579 em->orig_start = hole_start;
6580 /*
6581 * don't adjust block start at all,
6582 * it is fixed at EXTENT_MAP_HOLE
6583 */
6584 em->block_start = hole_em->block_start;
6585 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00006586 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
6587 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05006588 } else {
6589 em->start = range_start;
6590 em->len = found;
6591 em->orig_start = range_start;
6592 em->block_start = EXTENT_MAP_DELALLOC;
6593 em->block_len = found;
6594 }
6595 } else if (hole_em) {
6596 return hole_em;
6597 }
6598out:
6599
6600 free_extent_map(hole_em);
6601 if (err) {
6602 free_extent_map(em);
6603 return ERR_PTR(err);
6604 }
6605 return em;
6606}
6607
Josef Bacik4b46fce2010-05-23 11:00:55 -04006608static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
6609 u64 start, u64 len)
6610{
6611 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04006612 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006613 struct btrfs_key ins;
6614 u64 alloc_hint;
6615 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006616
Josef Bacik4b46fce2010-05-23 11:00:55 -04006617 alloc_hint = get_extent_allocation_hint(inode, start, len);
Josef Bacik00361582013-08-14 14:02:47 -04006618 ret = btrfs_reserve_extent(root, len, root->sectorsize, 0,
Miao Xiee570fd22014-06-19 10:42:50 +08006619 alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006620 if (ret)
6621 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006622
Josef Bacik70c8a912012-10-11 16:54:30 -04006623 em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006624 ins.offset, ins.offset, ins.offset, 0);
Josef Bacik00361582013-08-14 14:02:47 -04006625 if (IS_ERR(em)) {
Miao Xiee570fd22014-06-19 10:42:50 +08006626 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006627 return em;
6628 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006629
6630 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
6631 ins.offset, ins.offset, 0);
6632 if (ret) {
Miao Xiee570fd22014-06-19 10:42:50 +08006633 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006634 free_extent_map(em);
6635 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006636 }
Josef Bacik00361582013-08-14 14:02:47 -04006637
Josef Bacik4b46fce2010-05-23 11:00:55 -04006638 return em;
6639}
6640
Chris Mason46bfbb52010-05-26 11:04:10 -04006641/*
6642 * returns 1 when the nocow is safe, < 1 on error, 0 if the
6643 * block must be cow'd
6644 */
Josef Bacik00361582013-08-14 14:02:47 -04006645noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04006646 u64 *orig_start, u64 *orig_block_len,
6647 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04006648{
Josef Bacik00361582013-08-14 14:02:47 -04006649 struct btrfs_trans_handle *trans;
Chris Mason46bfbb52010-05-26 11:04:10 -04006650 struct btrfs_path *path;
6651 int ret;
6652 struct extent_buffer *leaf;
6653 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08006654 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04006655 struct btrfs_file_extent_item *fi;
6656 struct btrfs_key key;
6657 u64 disk_bytenr;
6658 u64 backref_offset;
6659 u64 extent_end;
6660 u64 num_bytes;
6661 int slot;
6662 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04006663 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08006664
Chris Mason46bfbb52010-05-26 11:04:10 -04006665 path = btrfs_alloc_path();
6666 if (!path)
6667 return -ENOMEM;
6668
Josef Bacik00361582013-08-14 14:02:47 -04006669 ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04006670 offset, 0);
6671 if (ret < 0)
6672 goto out;
6673
6674 slot = path->slots[0];
6675 if (ret == 1) {
6676 if (slot == 0) {
6677 /* can't find the item, must cow */
6678 ret = 0;
6679 goto out;
6680 }
6681 slot--;
6682 }
6683 ret = 0;
6684 leaf = path->nodes[0];
6685 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08006686 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04006687 key.type != BTRFS_EXTENT_DATA_KEY) {
6688 /* not our file or wrong item type, must cow */
6689 goto out;
6690 }
6691
6692 if (key.offset > offset) {
6693 /* Wrong offset, must cow */
6694 goto out;
6695 }
6696
6697 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
6698 found_type = btrfs_file_extent_type(leaf, fi);
6699 if (found_type != BTRFS_FILE_EXTENT_REG &&
6700 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
6701 /* not a regular extent, must cow */
6702 goto out;
6703 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04006704
6705 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
6706 goto out;
6707
Miao Xiee77751a2013-12-27 21:11:50 +08006708 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
6709 if (extent_end <= offset)
6710 goto out;
6711
Chris Mason46bfbb52010-05-26 11:04:10 -04006712 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04006713 if (disk_bytenr == 0)
6714 goto out;
6715
6716 if (btrfs_file_extent_compression(leaf, fi) ||
6717 btrfs_file_extent_encryption(leaf, fi) ||
6718 btrfs_file_extent_other_encoding(leaf, fi))
6719 goto out;
6720
Chris Mason46bfbb52010-05-26 11:04:10 -04006721 backref_offset = btrfs_file_extent_offset(leaf, fi);
6722
Josef Bacik7ee9e442013-06-21 16:37:03 -04006723 if (orig_start) {
6724 *orig_start = key.offset - backref_offset;
6725 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
6726 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
6727 }
Josef Bacikeb384b52013-04-24 16:32:55 -04006728
Chris Mason46bfbb52010-05-26 11:04:10 -04006729 if (btrfs_extent_readonly(root, disk_bytenr))
6730 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08006731
6732 num_bytes = min(offset + *len, extent_end) - offset;
6733 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
6734 u64 range_end;
6735
6736 range_end = round_up(offset + num_bytes, root->sectorsize) - 1;
6737 ret = test_range_bit(io_tree, offset, range_end,
6738 EXTENT_DELALLOC, 0, NULL);
6739 if (ret) {
6740 ret = -EAGAIN;
6741 goto out;
6742 }
6743 }
6744
Josef Bacik1bda19e2013-10-18 12:10:36 -04006745 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04006746
6747 /*
6748 * look for other files referencing this extent, if we
6749 * find any we must cow
6750 */
Josef Bacik00361582013-08-14 14:02:47 -04006751 trans = btrfs_join_transaction(root);
6752 if (IS_ERR(trans)) {
6753 ret = 0;
Chris Mason46bfbb52010-05-26 11:04:10 -04006754 goto out;
Josef Bacik00361582013-08-14 14:02:47 -04006755 }
6756
6757 ret = btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
6758 key.offset - backref_offset, disk_bytenr);
6759 btrfs_end_transaction(trans, root);
6760 if (ret) {
6761 ret = 0;
6762 goto out;
6763 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006764
6765 /*
6766 * adjust disk_bytenr and num_bytes to cover just the bytes
6767 * in this extent we are about to write. If there
6768 * are any csums in that range we have to cow in order
6769 * to keep the csums correct
6770 */
6771 disk_bytenr += backref_offset;
6772 disk_bytenr += offset - key.offset;
Chris Mason46bfbb52010-05-26 11:04:10 -04006773 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
6774 goto out;
6775 /*
6776 * all of the above have passed, it is safe to overwrite this extent
6777 * without cow
6778 */
Josef Bacikeb384b52013-04-24 16:32:55 -04006779 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04006780 ret = 1;
6781out:
6782 btrfs_free_path(path);
6783 return ret;
6784}
6785
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07006786bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
6787{
6788 struct radix_tree_root *root = &inode->i_mapping->page_tree;
6789 int found = false;
6790 void **pagep = NULL;
6791 struct page *page = NULL;
6792 int start_idx;
6793 int end_idx;
6794
6795 start_idx = start >> PAGE_CACHE_SHIFT;
6796
6797 /*
6798 * end is the last byte in the last page. end == start is legal
6799 */
6800 end_idx = end >> PAGE_CACHE_SHIFT;
6801
6802 rcu_read_lock();
6803
6804 /* Most of the code in this while loop is lifted from
6805 * find_get_page. It's been modified to begin searching from a
6806 * page and return just the first page found in that range. If the
6807 * found idx is less than or equal to the end idx then we know that
6808 * a page exists. If no pages are found or if those pages are
6809 * outside of the range then we're fine (yay!) */
6810 while (page == NULL &&
6811 radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
6812 page = radix_tree_deref_slot(pagep);
6813 if (unlikely(!page))
6814 break;
6815
6816 if (radix_tree_exception(page)) {
Filipe Manana809f9012014-06-05 13:22:25 +01006817 if (radix_tree_deref_retry(page)) {
6818 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07006819 continue;
Filipe Manana809f9012014-06-05 13:22:25 +01006820 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07006821 /*
6822 * Otherwise, shmem/tmpfs must be storing a swap entry
6823 * here as an exceptional entry: so return it without
6824 * attempting to raise page count.
6825 */
Filipe Manana6fdef6d2014-06-05 13:22:26 +01006826 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07006827 break; /* TODO: Is this relevant for this use case? */
6828 }
6829
Filipe Manana91405152014-06-05 13:22:24 +01006830 if (!page_cache_get_speculative(page)) {
6831 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07006832 continue;
Filipe Manana91405152014-06-05 13:22:24 +01006833 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07006834
6835 /*
6836 * Has the page moved?
6837 * This is part of the lockless pagecache protocol. See
6838 * include/linux/pagemap.h for details.
6839 */
6840 if (unlikely(page != *pagep)) {
6841 page_cache_release(page);
6842 page = NULL;
6843 }
6844 }
6845
6846 if (page) {
6847 if (page->index <= end_idx)
6848 found = true;
6849 page_cache_release(page);
6850 }
6851
6852 rcu_read_unlock();
6853 return found;
6854}
6855
Josef Bacikeb838e72012-07-31 16:28:48 -04006856static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
6857 struct extent_state **cached_state, int writing)
6858{
6859 struct btrfs_ordered_extent *ordered;
6860 int ret = 0;
6861
6862 while (1) {
6863 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6864 0, cached_state);
6865 /*
6866 * We're concerned with the entire range that we're going to be
6867 * doing DIO to, so we need to make sure theres no ordered
6868 * extents in this range.
6869 */
6870 ordered = btrfs_lookup_ordered_range(inode, lockstart,
6871 lockend - lockstart + 1);
6872
6873 /*
6874 * We need to make sure there are no buffered pages in this
6875 * range either, we could have raced between the invalidate in
6876 * generic_file_direct_write and locking the extent. The
6877 * invalidate needs to happen so that reads after a write do not
6878 * get stale data.
6879 */
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07006880 if (!ordered &&
6881 (!writing ||
6882 !btrfs_page_exists_in_range(inode, lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04006883 break;
6884
6885 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6886 cached_state, GFP_NOFS);
6887
6888 if (ordered) {
6889 btrfs_start_ordered_extent(inode, ordered, 1);
6890 btrfs_put_ordered_extent(ordered);
6891 } else {
6892 /* Screw you mmap */
6893 ret = filemap_write_and_wait_range(inode->i_mapping,
6894 lockstart,
6895 lockend);
6896 if (ret)
6897 break;
6898
6899 /*
6900 * If we found a page that couldn't be invalidated just
6901 * fall back to buffered.
6902 */
6903 ret = invalidate_inode_pages2_range(inode->i_mapping,
6904 lockstart >> PAGE_CACHE_SHIFT,
6905 lockend >> PAGE_CACHE_SHIFT);
6906 if (ret)
6907 break;
6908 }
6909
6910 cond_resched();
6911 }
6912
6913 return ret;
6914}
6915
Josef Bacik69ffb542012-09-11 15:40:07 -04006916static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
6917 u64 len, u64 orig_start,
6918 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006919 u64 orig_block_len, u64 ram_bytes,
6920 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04006921{
6922 struct extent_map_tree *em_tree;
6923 struct extent_map *em;
6924 struct btrfs_root *root = BTRFS_I(inode)->root;
6925 int ret;
6926
6927 em_tree = &BTRFS_I(inode)->extent_tree;
6928 em = alloc_extent_map();
6929 if (!em)
6930 return ERR_PTR(-ENOMEM);
6931
6932 em->start = start;
6933 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04006934 em->mod_start = start;
6935 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04006936 em->len = len;
6937 em->block_len = block_len;
6938 em->block_start = block_start;
6939 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05006940 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04006941 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04006942 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04006943 set_bit(EXTENT_FLAG_PINNED, &em->flags);
6944 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05006945 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04006946
6947 do {
6948 btrfs_drop_extent_cache(inode, em->start,
6949 em->start + em->len - 1, 0);
6950 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006951 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04006952 write_unlock(&em_tree->lock);
6953 } while (ret == -EEXIST);
6954
6955 if (ret) {
6956 free_extent_map(em);
6957 return ERR_PTR(ret);
6958 }
6959
6960 return em;
6961}
6962
6963
Josef Bacik4b46fce2010-05-23 11:00:55 -04006964static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
6965 struct buffer_head *bh_result, int create)
6966{
6967 struct extent_map *em;
6968 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04006969 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006970 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04006971 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006972 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04006973 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00006974 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006975
Miao Xie172a5042013-02-21 02:48:22 -07006976 if (create)
Josef Bacikeb838e72012-07-31 16:28:48 -04006977 unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07006978 else
Josef Bacikc3298612012-08-03 16:49:19 -04006979 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04006980
Josef Bacikc3298612012-08-03 16:49:19 -04006981 lockstart = start;
6982 lockend = start + len - 1;
6983
Josef Bacikeb838e72012-07-31 16:28:48 -04006984 /*
6985 * If this errors out it's because we couldn't invalidate pagecache for
6986 * this range and we need to fallback to buffered.
6987 */
6988 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
6989 return -ENOTBLK;
6990
Josef Bacik4b46fce2010-05-23 11:00:55 -04006991 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04006992 if (IS_ERR(em)) {
6993 ret = PTR_ERR(em);
6994 goto unlock_err;
6995 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006996
6997 /*
6998 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
6999 * io. INLINE is special, and we could probably kludge it in here, but
7000 * it's still buffered so for safety lets just fall back to the generic
7001 * buffered path.
7002 *
7003 * For COMPRESSED we _have_ to read the entire extent in so we can
7004 * decompress it, so there will be buffering required no matter what we
7005 * do, so go ahead and fallback to buffered.
7006 *
7007 * We return -ENOTBLK because thats what makes DIO go ahead and go back
7008 * to buffered IO. Don't blame me, this is the price we pay for using
7009 * the generic code.
7010 */
7011 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7012 em->block_start == EXTENT_MAP_INLINE) {
7013 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007014 ret = -ENOTBLK;
7015 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007016 }
7017
7018 /* Just a good old fashioned hole, return */
7019 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7020 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7021 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007022 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007023 }
7024
7025 /*
7026 * We don't allocate a new extent in the following cases
7027 *
7028 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7029 * existing extent.
7030 * 2) The extent is marked as PREALLOC. We're good to go here and can
7031 * just use the extent.
7032 *
7033 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007034 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007035 len = min(len, em->len - (start - em->start));
7036 lockstart = start + len;
7037 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007038 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007039
7040 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7041 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7042 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007043 int type;
7044 int ret;
Josef Bacikeb384b52013-04-24 16:32:55 -04007045 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007046
7047 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7048 type = BTRFS_ORDERED_PREALLOC;
7049 else
7050 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007051 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007052 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007053
Josef Bacik00361582013-08-14 14:02:47 -04007054 if (can_nocow_extent(inode, start, &len, &orig_start,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007055 &orig_block_len, &ram_bytes) == 1) {
Josef Bacik69ffb542012-09-11 15:40:07 -04007056 if (type == BTRFS_ORDERED_PREALLOC) {
7057 free_extent_map(em);
7058 em = create_pinned_em(inode, start, len,
7059 orig_start,
Josef Bacikb4939682012-12-03 10:31:19 -05007060 block_start, len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04007061 orig_block_len,
7062 ram_bytes, type);
Josef Bacik00361582013-08-14 14:02:47 -04007063 if (IS_ERR(em))
Josef Bacik69ffb542012-09-11 15:40:07 -04007064 goto unlock_err;
Josef Bacik69ffb542012-09-11 15:40:07 -04007065 }
7066
Chris Mason46bfbb52010-05-26 11:04:10 -04007067 ret = btrfs_add_ordered_extent_dio(inode, start,
7068 block_start, len, len, type);
Chris Mason46bfbb52010-05-26 11:04:10 -04007069 if (ret) {
7070 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007071 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007072 }
7073 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007074 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007075 }
Josef Bacik00361582013-08-14 14:02:47 -04007076
Chris Mason46bfbb52010-05-26 11:04:10 -04007077 /*
7078 * this will cow the extent, reset the len in case we changed
7079 * it above
7080 */
7081 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007082 free_extent_map(em);
7083 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007084 if (IS_ERR(em)) {
7085 ret = PTR_ERR(em);
7086 goto unlock_err;
7087 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007088 len = min(len, em->len - (start - em->start));
7089unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007090 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7091 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007092 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007093 bh_result->b_bdev = em->bdev;
7094 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007095 if (create) {
7096 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7097 set_buffer_new(bh_result);
7098
7099 /*
7100 * Need to update the i_size under the extent lock so buffered
7101 * readers will get the updated i_size when we unlock.
7102 */
7103 if (start + len > i_size_read(inode))
7104 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007105
Miao Xie172a5042013-02-21 02:48:22 -07007106 spin_lock(&BTRFS_I(inode)->lock);
7107 BTRFS_I(inode)->outstanding_extents++;
7108 spin_unlock(&BTRFS_I(inode)->lock);
7109
Miao Xie09348562013-02-07 10:12:07 +00007110 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7111 lockstart + len - 1, EXTENT_DELALLOC, NULL,
7112 &cached_state, GFP_NOFS);
7113 BUG_ON(ret);
Josef Bacikc3473e82012-06-19 10:59:00 -04007114 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007115
Josef Bacikeb838e72012-07-31 16:28:48 -04007116 /*
7117 * In the case of write we need to clear and unlock the entire range,
7118 * in the case of read we need to unlock only the end area that we
7119 * aren't using if there is any left over space.
7120 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007121 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007122 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7123 lockend, unlock_bits, 1, 0,
7124 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06007125 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007126 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007127 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007128
Josef Bacik4b46fce2010-05-23 11:00:55 -04007129 free_extent_map(em);
7130
7131 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007132
7133unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007134 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7135 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
7136 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007137}
7138
Josef Bacik4b46fce2010-05-23 11:00:55 -04007139static void btrfs_endio_direct_read(struct bio *bio, int err)
7140{
Miao Xiee65e1532010-11-22 03:04:43 +00007141 struct btrfs_dio_private *dip = bio->bi_private;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007142 struct bio_vec *bvec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007143 struct inode *inode = dip->inode;
7144 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason9be33952013-05-17 18:30:14 -04007145 struct bio *dio_bio;
Miao Xiefacc8a222013-07-25 19:22:34 +08007146 u32 *csums = (u32 *)dip->csum;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007147 u64 start;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007148 int i;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007149
7150 start = dip->logical_offset;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007151 bio_for_each_segment_all(bvec, bio, i) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007152 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
7153 struct page *page = bvec->bv_page;
7154 char *kaddr;
7155 u32 csum = ~(u32)0;
7156 unsigned long flags;
7157
7158 local_irq_save(flags);
Cong Wang7ac687d2011-11-25 23:14:28 +08007159 kaddr = kmap_atomic(page);
Liu Bob0496682013-03-14 14:57:45 +00007160 csum = btrfs_csum_data(kaddr + bvec->bv_offset,
Josef Bacik4b46fce2010-05-23 11:00:55 -04007161 csum, bvec->bv_len);
7162 btrfs_csum_final(csum, (char *)&csum);
Cong Wang7ac687d2011-11-25 23:14:28 +08007163 kunmap_atomic(kaddr);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007164 local_irq_restore(flags);
7165
7166 flush_dcache_page(bvec->bv_page);
Kent Overstreet2c30c712013-11-07 12:20:26 -08007167 if (csum != csums[i]) {
Miao Xiefacc8a222013-07-25 19:22:34 +08007168 btrfs_err(root->fs_info, "csum failed ino %llu off %llu csum %u expected csum %u",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02007169 btrfs_ino(inode), start, csum,
Kent Overstreet2c30c712013-11-07 12:20:26 -08007170 csums[i]);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007171 err = -EIO;
7172 }
7173 }
7174
7175 start += bvec->bv_len;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007176 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007177
7178 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007179 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04007180 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007181
Josef Bacik4b46fce2010-05-23 11:00:55 -04007182 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04007183
7184 /* If we had a csum failure make sure to clear the uptodate flag */
7185 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04007186 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
7187 dio_end_io(dio_bio, err);
7188 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007189}
7190
7191static void btrfs_endio_direct_write(struct bio *bio, int err)
7192{
7193 struct btrfs_dio_private *dip = bio->bi_private;
7194 struct inode *inode = dip->inode;
7195 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007196 struct btrfs_ordered_extent *ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05007197 u64 ordered_offset = dip->logical_offset;
7198 u64 ordered_bytes = dip->bytes;
Chris Mason9be33952013-05-17 18:30:14 -04007199 struct bio *dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007200 int ret;
7201
7202 if (err)
7203 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05007204again:
7205 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
7206 &ordered_offset,
Josef Bacik5fd02042012-05-02 14:00:54 -04007207 ordered_bytes, !err);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007208 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05007209 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007210
Qu Wenruofccb5d82014-02-28 10:46:10 +08007211 btrfs_init_work(&ordered->work, finish_ordered_fn, NULL, NULL);
7212 btrfs_queue_work(root->fs_info->endio_write_workers,
7213 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05007214out_test:
7215 /*
7216 * our bio might span multiple ordered extents. If we haven't
7217 * completed the accounting for the whole dio, go back and try again
7218 */
7219 if (ordered_offset < dip->logical_offset + dip->bytes) {
7220 ordered_bytes = dip->logical_offset + dip->bytes -
7221 ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04007222 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05007223 goto again;
7224 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007225out_done:
Chris Mason9be33952013-05-17 18:30:14 -04007226 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007227
Josef Bacik4b46fce2010-05-23 11:00:55 -04007228 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04007229
7230 /* If we had an error make sure to clear the uptodate flag */
7231 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04007232 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
7233 dio_end_io(dio_bio, err);
7234 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007235}
7236
Chris Masoneaf25d92010-05-25 09:48:28 -04007237static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
7238 struct bio *bio, int mirror_num,
7239 unsigned long bio_flags, u64 offset)
7240{
7241 int ret;
7242 struct btrfs_root *root = BTRFS_I(inode)->root;
7243 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007244 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04007245 return 0;
7246}
7247
Miao Xiee65e1532010-11-22 03:04:43 +00007248static void btrfs_end_dio_bio(struct bio *bio, int err)
7249{
7250 struct btrfs_dio_private *dip = bio->bi_private;
7251
7252 if (err) {
Frank Holtonefe120a2013-12-20 11:37:06 -05007253 btrfs_err(BTRFS_I(dip->inode)->root->fs_info,
7254 "direct IO failed ino %llu rw %lu sector %#Lx len %u err no %d",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02007255 btrfs_ino(dip->inode), bio->bi_rw,
Kent Overstreet4f024f32013-10-11 15:44:27 -07007256 (unsigned long long)bio->bi_iter.bi_sector,
7257 bio->bi_iter.bi_size, err);
Miao Xiee65e1532010-11-22 03:04:43 +00007258 dip->errors = 1;
7259
7260 /*
7261 * before atomic variable goto zero, we must make sure
7262 * dip->errors is perceived to be set.
7263 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01007264 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00007265 }
7266
7267 /* if there are more bios still pending for this dio, just exit */
7268 if (!atomic_dec_and_test(&dip->pending_bios))
7269 goto out;
7270
Chris Mason9be33952013-05-17 18:30:14 -04007271 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00007272 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04007273 } else {
7274 set_bit(BIO_UPTODATE, &dip->dio_bio->bi_flags);
Miao Xiee65e1532010-11-22 03:04:43 +00007275 bio_endio(dip->orig_bio, 0);
7276 }
7277out:
7278 bio_put(bio);
7279}
7280
7281static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
7282 u64 first_sector, gfp_t gfp_flags)
7283{
7284 int nr_vecs = bio_get_nr_vecs(bdev);
7285 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
7286}
7287
7288static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
7289 int rw, u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007290 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00007291{
Miao Xiefacc8a222013-07-25 19:22:34 +08007292 struct btrfs_dio_private *dip = bio->bi_private;
Miao Xiee65e1532010-11-22 03:04:43 +00007293 int write = rw & REQ_WRITE;
7294 struct btrfs_root *root = BTRFS_I(inode)->root;
7295 int ret;
7296
Josef Bacikb812ce22012-11-16 13:56:32 -05007297 if (async_submit)
7298 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
7299
Miao Xiee65e1532010-11-22 03:04:43 +00007300 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04007301
7302 if (!write) {
7303 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
7304 if (ret)
7305 goto err;
7306 }
Miao Xiee65e1532010-11-22 03:04:43 +00007307
Josef Bacik1ae39932011-04-06 14:41:34 -04007308 if (skip_sum)
7309 goto map;
7310
7311 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00007312 ret = btrfs_wq_submit_bio(root->fs_info,
7313 inode, rw, bio, 0, 0,
7314 file_offset,
7315 __btrfs_submit_bio_start_direct_io,
7316 __btrfs_submit_bio_done);
7317 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04007318 } else if (write) {
7319 /*
7320 * If we aren't doing async submit, calculate the csum of the
7321 * bio now.
7322 */
7323 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
7324 if (ret)
7325 goto err;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007326 } else if (!skip_sum) {
Miao Xiefacc8a222013-07-25 19:22:34 +08007327 ret = btrfs_lookup_bio_sums_dio(root, inode, dip, bio,
7328 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007329 if (ret)
7330 goto err;
7331 }
Miao Xiee65e1532010-11-22 03:04:43 +00007332
Josef Bacik1ae39932011-04-06 14:41:34 -04007333map:
7334 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007335err:
7336 bio_put(bio);
7337 return ret;
7338}
7339
7340static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
7341 int skip_sum)
7342{
7343 struct inode *inode = dip->inode;
7344 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00007345 struct bio *bio;
7346 struct bio *orig_bio = dip->orig_bio;
7347 struct bio_vec *bvec = orig_bio->bi_io_vec;
Kent Overstreet4f024f32013-10-11 15:44:27 -07007348 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00007349 u64 file_offset = dip->logical_offset;
7350 u64 submit_len = 0;
7351 u64 map_length;
7352 int nr_pages = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007353 int ret = 0;
Josef Bacik1ae39932011-04-06 14:41:34 -04007354 int async_submit = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007355
Kent Overstreet4f024f32013-10-11 15:44:27 -07007356 map_length = orig_bio->bi_iter.bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007357 ret = btrfs_map_block(root->fs_info, rw, start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007358 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08007359 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00007360 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08007361
Kent Overstreet4f024f32013-10-11 15:44:27 -07007362 if (map_length >= orig_bio->bi_iter.bi_size) {
Josef Bacik02f57c72011-04-06 14:25:44 -04007363 bio = orig_bio;
7364 goto submit;
7365 }
7366
David Woodhouse53b381b2013-01-29 18:40:14 -05007367 /* async crcs make it difficult to collect full stripe writes. */
7368 if (btrfs_get_alloc_profile(root, 1) &
7369 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6))
7370 async_submit = 0;
7371 else
7372 async_submit = 1;
7373
Josef Bacik02f57c72011-04-06 14:25:44 -04007374 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
7375 if (!bio)
7376 return -ENOMEM;
Miao Xie7a5c3c92014-06-17 18:58:59 +08007377
Josef Bacik02f57c72011-04-06 14:25:44 -04007378 bio->bi_private = dip;
7379 bio->bi_end_io = btrfs_end_dio_bio;
7380 atomic_inc(&dip->pending_bios);
7381
Miao Xiee65e1532010-11-22 03:04:43 +00007382 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
7383 if (unlikely(map_length < submit_len + bvec->bv_len ||
7384 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
7385 bvec->bv_offset) < bvec->bv_len)) {
7386 /*
7387 * inc the count before we submit the bio so
7388 * we know the end IO handler won't happen before
7389 * we inc the count. Otherwise, the dip might get freed
7390 * before we're done setting it up
7391 */
7392 atomic_inc(&dip->pending_bios);
7393 ret = __btrfs_submit_dio_bio(bio, inode, rw,
7394 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007395 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007396 if (ret) {
7397 bio_put(bio);
7398 atomic_dec(&dip->pending_bios);
7399 goto out_err;
7400 }
7401
Miao Xiee65e1532010-11-22 03:04:43 +00007402 start_sector += submit_len >> 9;
7403 file_offset += submit_len;
7404
7405 submit_len = 0;
7406 nr_pages = 0;
7407
7408 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
7409 start_sector, GFP_NOFS);
7410 if (!bio)
7411 goto out_err;
7412 bio->bi_private = dip;
7413 bio->bi_end_io = btrfs_end_dio_bio;
7414
Kent Overstreet4f024f32013-10-11 15:44:27 -07007415 map_length = orig_bio->bi_iter.bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007416 ret = btrfs_map_block(root->fs_info, rw,
Stefan Behrens3ec706c2012-11-05 15:46:42 +01007417 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007418 &map_length, NULL, 0);
7419 if (ret) {
7420 bio_put(bio);
7421 goto out_err;
7422 }
7423 } else {
7424 submit_len += bvec->bv_len;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307425 nr_pages++;
Miao Xiee65e1532010-11-22 03:04:43 +00007426 bvec++;
7427 }
7428 }
7429
Josef Bacik02f57c72011-04-06 14:25:44 -04007430submit:
Miao Xiee65e1532010-11-22 03:04:43 +00007431 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007432 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007433 if (!ret)
7434 return 0;
7435
7436 bio_put(bio);
7437out_err:
7438 dip->errors = 1;
7439 /*
7440 * before atomic variable goto zero, we must
7441 * make sure dip->errors is perceived to be set.
7442 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01007443 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00007444 if (atomic_dec_and_test(&dip->pending_bios))
7445 bio_io_error(dip->orig_bio);
7446
7447 /* bio_end_io() will handle error, so we needn't return it */
7448 return 0;
7449}
7450
Chris Mason9be33952013-05-17 18:30:14 -04007451static void btrfs_submit_direct(int rw, struct bio *dio_bio,
7452 struct inode *inode, loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007453{
7454 struct btrfs_root *root = BTRFS_I(inode)->root;
7455 struct btrfs_dio_private *dip;
Chris Mason9be33952013-05-17 18:30:14 -04007456 struct bio *io_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007457 int skip_sum;
Miao Xiefacc8a222013-07-25 19:22:34 +08007458 int sum_len;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02007459 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007460 int ret = 0;
Miao Xiefacc8a222013-07-25 19:22:34 +08007461 u16 csum_size;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007462
7463 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
7464
Chris Mason9be33952013-05-17 18:30:14 -04007465 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04007466 if (!io_bio) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007467 ret = -ENOMEM;
7468 goto free_ordered;
7469 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007470
Miao Xiefacc8a222013-07-25 19:22:34 +08007471 if (!skip_sum && !write) {
7472 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
Kent Overstreet4f024f32013-10-11 15:44:27 -07007473 sum_len = dio_bio->bi_iter.bi_size >>
7474 inode->i_sb->s_blocksize_bits;
Miao Xiefacc8a222013-07-25 19:22:34 +08007475 sum_len *= csum_size;
7476 } else {
7477 sum_len = 0;
7478 }
7479
7480 dip = kmalloc(sizeof(*dip) + sum_len, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04007481 if (!dip) {
7482 ret = -ENOMEM;
7483 goto free_io_bio;
7484 }
7485
7486 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007487 dip->inode = inode;
7488 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07007489 dip->bytes = dio_bio->bi_iter.bi_size;
7490 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Chris Mason9be33952013-05-17 18:30:14 -04007491 io_bio->bi_private = dip;
Miao Xiee65e1532010-11-22 03:04:43 +00007492 dip->errors = 0;
Chris Mason9be33952013-05-17 18:30:14 -04007493 dip->orig_bio = io_bio;
7494 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00007495 atomic_set(&dip->pending_bios, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007496
7497 if (write)
Chris Mason9be33952013-05-17 18:30:14 -04007498 io_bio->bi_end_io = btrfs_endio_direct_write;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007499 else
Chris Mason9be33952013-05-17 18:30:14 -04007500 io_bio->bi_end_io = btrfs_endio_direct_read;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007501
Miao Xiee65e1532010-11-22 03:04:43 +00007502 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
7503 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04007504 return;
Chris Mason9be33952013-05-17 18:30:14 -04007505
7506free_io_bio:
7507 bio_put(io_bio);
7508
Josef Bacik4b46fce2010-05-23 11:00:55 -04007509free_ordered:
7510 /*
7511 * If this is a write, we need to clean up the reserved space and kill
7512 * the ordered extent.
7513 */
7514 if (write) {
7515 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05007516 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007517 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
7518 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
7519 btrfs_free_reserved_extent(root, ordered->start,
Miao Xiee570fd22014-06-19 10:42:50 +08007520 ordered->disk_len, 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007521 btrfs_put_ordered_extent(ordered);
7522 btrfs_put_ordered_extent(ordered);
7523 }
Chris Mason9be33952013-05-17 18:30:14 -04007524 bio_endio(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007525}
7526
Chris Mason5a5f79b2010-05-26 21:33:37 -04007527static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
Al Viro28060d52014-03-22 05:15:17 -04007528 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04007529{
7530 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00007531 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007532 unsigned blocksize_mask = root->sectorsize - 1;
7533 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007534
7535 if (offset & blocksize_mask)
7536 goto out;
7537
Al Viro28060d52014-03-22 05:15:17 -04007538 if (iov_iter_alignment(iter) & blocksize_mask)
7539 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00007540
Al Viro28060d52014-03-22 05:15:17 -04007541 /* If this is a write we don't need to check anymore */
7542 if (rw & WRITE)
7543 return 0;
7544 /*
7545 * Check to make sure we don't have duplicate iov_base's in this
7546 * iovec, if so return EINVAL, otherwise we'll get csum errors
7547 * when reading back.
7548 */
7549 for (seg = 0; seg < iter->nr_segs; seg++) {
7550 for (i = seg + 1; i < iter->nr_segs; i++) {
7551 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00007552 goto out;
7553 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04007554 }
7555 retval = 0;
7556out:
7557 return retval;
7558}
Josef Bacikeb838e72012-07-31 16:28:48 -04007559
Chris Mason16432982008-04-10 10:23:21 -04007560static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
Al Virod8d3d942014-03-04 21:27:34 -05007561 struct iov_iter *iter, loff_t offset)
Chris Mason16432982008-04-10 10:23:21 -04007562{
Josef Bacik4b46fce2010-05-23 11:00:55 -04007563 struct file *file = iocb->ki_filp;
7564 struct inode *inode = file->f_mapping->host;
Miao Xie09348562013-02-07 10:12:07 +00007565 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00007566 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00007567 bool wakeup = true;
7568 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00007569 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007570
Al Viro28060d52014-03-22 05:15:17 -04007571 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007572 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007573
Miao Xie38851cc2013-02-08 07:04:11 +00007574 atomic_inc(&inode->i_dio_count);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01007575 smp_mb__after_atomic();
Miao Xie38851cc2013-02-08 07:04:11 +00007576
Josef Bacik0e267c42013-07-02 10:38:02 -04007577 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08007578 * The generic stuff only does filemap_write_and_wait_range, which
7579 * isn't enough if we've written compressed pages to this area, so
7580 * we need to flush the dirty pages again to make absolutely sure
7581 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04007582 */
Al Viroa6cbcd42014-03-04 22:38:00 -05007583 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08007584 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
7585 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08007586 filemap_fdatawrite_range(inode->i_mapping, offset,
7587 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04007588
Miao Xie09348562013-02-07 10:12:07 +00007589 if (rw & WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00007590 /*
7591 * If the write DIO is beyond the EOF, we need update
7592 * the isize, but it is protected by i_mutex. So we can
7593 * not unlock the i_mutex at this case.
7594 */
7595 if (offset + count <= inode->i_size) {
7596 mutex_unlock(&inode->i_mutex);
7597 relock = true;
7598 }
Miao Xie09348562013-02-07 10:12:07 +00007599 ret = btrfs_delalloc_reserve_space(inode, count);
7600 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00007601 goto out;
7602 } else if (unlikely(test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
7603 &BTRFS_I(inode)->runtime_flags))) {
7604 inode_dio_done(inode);
7605 flags = DIO_LOCKING | DIO_SKIP_HOLES;
7606 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00007607 }
7608
7609 ret = __blockdev_direct_IO(rw, iocb, inode,
7610 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
Al Viro31b14032014-03-05 01:33:16 -05007611 iter, offset, btrfs_get_blocks_direct, NULL,
Miao Xie2e60a512013-02-08 07:01:08 +00007612 btrfs_submit_direct, flags);
Miao Xie09348562013-02-07 10:12:07 +00007613 if (rw & WRITE) {
7614 if (ret < 0 && ret != -EIOCBQUEUED)
7615 btrfs_delalloc_release_space(inode, count);
Miao Xie172a5042013-02-21 02:48:22 -07007616 else if (ret >= 0 && (size_t)ret < count)
Miao Xie09348562013-02-07 10:12:07 +00007617 btrfs_delalloc_release_space(inode,
7618 count - (size_t)ret);
Miao Xie172a5042013-02-21 02:48:22 -07007619 else
7620 btrfs_delalloc_release_metadata(inode, 0);
Miao Xie09348562013-02-07 10:12:07 +00007621 }
Miao Xie38851cc2013-02-08 07:04:11 +00007622out:
Miao Xie2e60a512013-02-08 07:01:08 +00007623 if (wakeup)
7624 inode_dio_done(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00007625 if (relock)
7626 mutex_lock(&inode->i_mutex);
Miao Xie09348562013-02-07 10:12:07 +00007627
7628 return ret;
Chris Mason16432982008-04-10 10:23:21 -04007629}
7630
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007631#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
7632
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007633static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
7634 __u64 start, __u64 len)
7635{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007636 int ret;
7637
7638 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
7639 if (ret)
7640 return ret;
7641
Chris Masonec29ed52011-02-23 16:23:20 -05007642 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007643}
7644
Chris Mason9ebefb182007-06-15 13:50:00 -04007645int btrfs_readpage(struct file *file, struct page *page)
7646{
Chris Masond1310b22008-01-24 16:13:08 -05007647 struct extent_io_tree *tree;
7648 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02007649 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04007650}
Chris Mason1832a6d2007-12-21 16:27:21 -05007651
Chris Mason39279cc2007-06-12 06:35:45 -04007652static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
7653{
Chris Masond1310b22008-01-24 16:13:08 -05007654 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04007655
7656
7657 if (current->flags & PF_MEMALLOC) {
7658 redirty_page_for_writepage(wbc, page);
7659 unlock_page(page);
7660 return 0;
7661 }
Chris Masond1310b22008-01-24 16:13:08 -05007662 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04007663 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
7664}
Chris Mason39279cc2007-06-12 06:35:45 -04007665
Eric Sandeen48a3b632013-04-25 20:41:01 +00007666static int btrfs_writepages(struct address_space *mapping,
7667 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04007668{
Chris Masond1310b22008-01-24 16:13:08 -05007669 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05007670
Chris Masond1310b22008-01-24 16:13:08 -05007671 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04007672 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
7673}
7674
Chris Mason3ab2fb52007-11-08 10:59:22 -05007675static int
7676btrfs_readpages(struct file *file, struct address_space *mapping,
7677 struct list_head *pages, unsigned nr_pages)
7678{
Chris Masond1310b22008-01-24 16:13:08 -05007679 struct extent_io_tree *tree;
7680 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05007681 return extent_readpages(tree, mapping, pages, nr_pages,
7682 btrfs_get_extent);
7683}
Chris Masone6dcd2d2008-07-17 12:53:50 -04007684static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04007685{
Chris Masond1310b22008-01-24 16:13:08 -05007686 struct extent_io_tree *tree;
7687 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04007688 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04007689
Chris Masond1310b22008-01-24 16:13:08 -05007690 tree = &BTRFS_I(page->mapping->host)->io_tree;
7691 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05007692 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04007693 if (ret == 1) {
7694 ClearPagePrivate(page);
7695 set_page_private(page, 0);
7696 page_cache_release(page);
7697 }
7698 return ret;
7699}
Chris Mason39279cc2007-06-12 06:35:45 -04007700
Chris Masone6dcd2d2008-07-17 12:53:50 -04007701static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
7702{
Chris Mason98509cfc2008-09-11 15:51:43 -04007703 if (PageWriteback(page) || PageDirty(page))
7704 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05007705 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007706}
7707
Lukas Czernerd47992f2013-05-21 23:17:23 -04007708static void btrfs_invalidatepage(struct page *page, unsigned int offset,
7709 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04007710{
Josef Bacik5fd02042012-05-02 14:00:54 -04007711 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05007712 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007713 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007714 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007715 u64 page_start = page_offset(page);
7716 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00007717 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04007718
Chris Mason8b62b722009-09-02 16:53:46 -04007719 /*
7720 * we have the page locked, so new writeback can't start,
7721 * and the dirty bit won't be cleared while we are here.
7722 *
7723 * Wait for IO on this page so that we can safely clear
7724 * the PagePrivate2 bit and do ordered accounting
7725 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007726 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04007727
Josef Bacik5fd02042012-05-02 14:00:54 -04007728 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007729 if (offset) {
7730 btrfs_releasepage(page, GFP_NOFS);
7731 return;
7732 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00007733
7734 if (!inode_evicting)
7735 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
7736 ordered = btrfs_lookup_ordered_extent(inode, page_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007737 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04007738 /*
7739 * IO on this page will never be started, so we need
7740 * to account for any ordered extents now
7741 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00007742 if (!inode_evicting)
7743 clear_extent_bit(tree, page_start, page_end,
7744 EXTENT_DIRTY | EXTENT_DELALLOC |
7745 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
7746 EXTENT_DEFRAG, 1, 0, &cached_state,
7747 GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04007748 /*
7749 * whoever cleared the private bit is responsible
7750 * for the finish_ordered_io
7751 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04007752 if (TestClearPagePrivate2(page)) {
7753 struct btrfs_ordered_inode_tree *tree;
7754 u64 new_len;
7755
7756 tree = &BTRFS_I(inode)->ordered_tree;
7757
7758 spin_lock_irq(&tree->lock);
7759 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
7760 new_len = page_start - ordered->file_offset;
7761 if (new_len < ordered->truncated_len)
7762 ordered->truncated_len = new_len;
7763 spin_unlock_irq(&tree->lock);
7764
7765 if (btrfs_dec_test_ordered_pending(inode, &ordered,
7766 page_start,
7767 PAGE_CACHE_SIZE, 1))
7768 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04007769 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007770 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00007771 if (!inode_evicting) {
7772 cached_state = NULL;
7773 lock_extent_bits(tree, page_start, page_end, 0,
7774 &cached_state);
7775 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007776 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00007777
7778 if (!inode_evicting) {
7779 clear_extent_bit(tree, page_start, page_end,
7780 EXTENT_LOCKED | EXTENT_DIRTY |
7781 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
7782 EXTENT_DEFRAG, 1, 1,
7783 &cached_state, GFP_NOFS);
7784
7785 __btrfs_releasepage(page, GFP_NOFS);
7786 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007787
Chris Mason4a096752008-07-21 10:29:44 -04007788 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04007789 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04007790 ClearPagePrivate(page);
7791 set_page_private(page, 0);
7792 page_cache_release(page);
7793 }
Chris Mason39279cc2007-06-12 06:35:45 -04007794}
7795
Chris Mason9ebefb182007-06-15 13:50:00 -04007796/*
7797 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
7798 * called from a page fault handler when a page is first dirtied. Hence we must
7799 * be careful to check for EOF conditions here. We set the page up correctly
7800 * for a written page which means we get ENOSPC checking when writing into
7801 * holes and correct delalloc and unwritten extent mapping on filesystems that
7802 * support these features.
7803 *
7804 * We are not allowed to take the i_mutex here so we have to play games to
7805 * protect against truncate races as the page could now be beyond EOF. Because
7806 * vmtruncate() writes the inode size before removing pages, once we have the
7807 * page lock we can determine safely if the page is beyond EOF. If it is not
7808 * beyond EOF, then the page is guaranteed safe against truncation until we
7809 * unlock the page.
7810 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07007811int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04007812{
Nick Pigginc2ec1752009-03-31 15:23:21 -07007813 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05007814 struct inode *inode = file_inode(vma->vm_file);
Chris Mason1832a6d2007-12-21 16:27:21 -05007815 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007816 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7817 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007818 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007819 char *kaddr;
7820 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04007821 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05007822 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05007823 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007824 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007825 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04007826
Jan Karab2b5ef52012-06-12 16:20:45 +02007827 sb_start_pagefault(inode->i_sb);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04007828 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007829 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04007830 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05007831 reserved = 1;
7832 }
Nick Piggin56a76f82009-03-31 15:23:23 -07007833 if (ret) {
7834 if (ret == -ENOMEM)
7835 ret = VM_FAULT_OOM;
7836 else /* -ENOSPC, -EIO, etc */
7837 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05007838 if (reserved)
7839 goto out;
7840 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07007841 }
Chris Mason1832a6d2007-12-21 16:27:21 -05007842
Nick Piggin56a76f82009-03-31 15:23:23 -07007843 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007844again:
Chris Mason9ebefb182007-06-15 13:50:00 -04007845 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04007846 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007847 page_start = page_offset(page);
7848 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04007849
Chris Mason9ebefb182007-06-15 13:50:00 -04007850 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04007851 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04007852 /* page got truncated out from underneath us */
7853 goto out_unlock;
7854 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007855 wait_on_page_writeback(page);
7856
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007857 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007858 set_page_extent_mapped(page);
7859
Chris Masoneb84ae02008-07-17 13:53:27 -04007860 /*
7861 * we can't set the delalloc bits if there are pending ordered
7862 * extents. Drop our locks and wait for them to finish
7863 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007864 ordered = btrfs_lookup_ordered_extent(inode, page_start);
7865 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007866 unlock_extent_cached(io_tree, page_start, page_end,
7867 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007868 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04007869 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007870 btrfs_put_ordered_extent(ordered);
7871 goto again;
7872 }
7873
Josef Bacikfbf19082009-10-01 17:10:23 -04007874 /*
7875 * XXX - page_mkwrite gets called every time the page is dirtied, even
7876 * if it was already dirty, so for space accounting reasons we need to
7877 * clear any delalloc bits for the range we are fixing to save. There
7878 * is probably a better way to do this, but for now keep consistent with
7879 * prepare_pages in the normal write path.
7880 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00007881 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06007882 EXTENT_DIRTY | EXTENT_DELALLOC |
7883 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00007884 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04007885
Josef Bacik2ac55d42010-02-03 19:33:23 +00007886 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
7887 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007888 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007889 unlock_extent_cached(io_tree, page_start, page_end,
7890 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007891 ret = VM_FAULT_SIGBUS;
7892 goto out_unlock;
7893 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007894 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04007895
7896 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04007897 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04007898 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04007899 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04007900 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04007901
Chris Masone6dcd2d2008-07-17 12:53:50 -04007902 if (zero_start != PAGE_CACHE_SIZE) {
7903 kaddr = kmap(page);
7904 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
7905 flush_dcache_page(page);
7906 kunmap(page);
7907 }
Chris Mason247e7432008-07-17 12:53:51 -04007908 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007909 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04007910 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007911
Chris Mason257c62e2009-10-13 13:21:08 -04007912 BTRFS_I(inode)->last_trans = root->fs_info->generation;
7913 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06007914 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04007915
Josef Bacik2ac55d42010-02-03 19:33:23 +00007916 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04007917
7918out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02007919 if (!ret) {
7920 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04007921 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02007922 }
Chris Mason9ebefb182007-06-15 13:50:00 -04007923 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05007924out:
Josef Bacikec39e182012-01-12 19:10:12 -05007925 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007926out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02007927 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04007928 return ret;
7929}
7930
Josef Bacika41ad392011-01-31 15:30:16 -05007931static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04007932{
7933 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04007934 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04007935 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05007936 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007937 struct btrfs_trans_handle *trans;
Chris Masondbe674a2008-07-17 12:54:05 -04007938 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04007939 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04007940
Josef Bacik0ef8b722013-10-25 16:13:35 -04007941 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
7942 (u64)-1);
7943 if (ret)
7944 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04007945
Josef Bacikfcb80c22011-05-03 10:40:22 -04007946 /*
7947 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
7948 * 3 things going on here
7949 *
7950 * 1) We need to reserve space for our orphan item and the space to
7951 * delete our orphan item. Lord knows we don't want to have a dangling
7952 * orphan item because we didn't reserve space to remove it.
7953 *
7954 * 2) We need to reserve space to update our inode.
7955 *
7956 * 3) We need to have something to cache all the space that is going to
7957 * be free'd up by the truncate operation, but also have some slack
7958 * space reserved in case it uses space during the truncate (thank you
7959 * very much snapshotting).
7960 *
7961 * And we need these to all be seperate. The fact is we can use alot of
7962 * space doing the truncate, and we have no earthly idea how much space
7963 * we will use, so we need the truncate reservation to be seperate so it
7964 * doesn't end up using space reserved for updating the inode or
7965 * removing the orphan item. We also need to be able to stop the
7966 * transaction and start a new one, which means we need to be able to
7967 * update the inode several times, and we have no idea of knowing how
7968 * many times that will be, so we can't just reserve 1 item for the
7969 * entirety of the opration, so that has to be done seperately as well.
7970 * Then there is the orphan item, which does indeed need to be held on
7971 * to for the whole operation, and we need nobody to touch this reserved
7972 * space except the orphan code.
7973 *
7974 * So that leaves us with
7975 *
7976 * 1) root->orphan_block_rsv - for the orphan deletion.
7977 * 2) rsv - for the truncate reservation, which we will steal from the
7978 * transaction reservation.
7979 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
7980 * updating the inode.
7981 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06007982 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007983 if (!rsv)
7984 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04007985 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04007986 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05007987
Josef Bacik907cbce2011-08-08 13:46:15 -04007988 /*
Josef Bacik07127182011-08-19 10:29:59 -04007989 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04007990 * 1 for updating the inode.
7991 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05007992 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007993 if (IS_ERR(trans)) {
7994 err = PTR_ERR(trans);
7995 goto out;
7996 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05007997
Josef Bacik907cbce2011-08-08 13:46:15 -04007998 /* Migrate the slack space for the truncate to our reserve */
7999 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
8000 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008001 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05008002
Chris Mason5a3f23d2009-03-31 13:27:11 -04008003 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04008004 * So if we truncate and then write and fsync we normally would just
8005 * write the extents that changed, which is a problem if we need to
8006 * first truncate that entire inode. So set this flag so we write out
8007 * all of the extents in the inode to the sync log so we're completely
8008 * safe.
8009 */
8010 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008011 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008012
Yan, Zheng80825102009-11-12 09:35:36 +00008013 while (1) {
8014 ret = btrfs_truncate_inode_items(trans, root, inode,
8015 inode->i_size,
8016 BTRFS_EXTENT_DATA_KEY);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008017 if (ret != -ENOSPC) {
Josef Bacik3893e332011-01-31 16:03:11 -05008018 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00008019 break;
Josef Bacik3893e332011-01-31 16:03:11 -05008020 }
Chris Mason39279cc2007-06-12 06:35:45 -04008021
Josef Bacikfcb80c22011-05-03 10:40:22 -04008022 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008023 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05008024 if (ret) {
8025 err = ret;
8026 break;
8027 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04008028
Yan, Zheng80825102009-11-12 09:35:36 +00008029 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00008030 btrfs_btree_balance_dirty(root);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008031
8032 trans = btrfs_start_transaction(root, 2);
8033 if (IS_ERR(trans)) {
8034 ret = err = PTR_ERR(trans);
8035 trans = NULL;
8036 break;
8037 }
8038
8039 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
8040 rsv, min_size);
8041 BUG_ON(ret); /* shouldn't happen */
8042 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008043 }
8044
8045 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04008046 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008047 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05008048 if (ret)
8049 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00008050 }
8051
Chris Mason917c16b2011-11-08 14:49:59 -05008052 if (trans) {
8053 trans->block_rsv = &root->fs_info->trans_block_rsv;
8054 ret = btrfs_update_inode(trans, root, inode);
8055 if (ret && !err)
8056 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04008057
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008058 ret = btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00008059 btrfs_btree_balance_dirty(root);
Chris Mason917c16b2011-11-08 14:49:59 -05008060 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04008061
8062out:
8063 btrfs_free_block_rsv(root, rsv);
8064
Josef Bacik3893e332011-01-31 16:03:11 -05008065 if (ret && !err)
8066 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05008067
Josef Bacik3893e332011-01-31 16:03:11 -05008068 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04008069}
8070
Sven Wegener3b963622008-06-09 21:57:42 -04008071/*
Chris Masond352ac62008-09-29 15:18:18 -04008072 * create a new subvolume directory/inode (helper for the ioctl).
8073 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05008074int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008075 struct btrfs_root *new_root,
8076 struct btrfs_root *parent_root,
8077 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04008078{
Chris Mason39279cc2007-06-12 06:35:45 -04008079 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04008080 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008081 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008082
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01008083 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
8084 new_dirid, new_dirid,
8085 S_IFDIR | (~current_umask() & S_IRWXUGO),
8086 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04008087 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04008088 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008089 inode->i_op = &btrfs_dir_inode_operations;
8090 inode->i_fop = &btrfs_dir_file_operations;
8091
Miklos Szeredibfe86842011-10-28 14:13:29 +02008092 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04008093 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04008094
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008095 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
8096 if (err)
8097 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02008098 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008099 new_root->root_key.objectid, err);
8100
Yan, Zheng76dda932009-09-21 16:00:26 -04008101 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04008102
Yan, Zheng76dda932009-09-21 16:00:26 -04008103 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07008104 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04008105}
8106
Chris Mason39279cc2007-06-12 06:35:45 -04008107struct inode *btrfs_alloc_inode(struct super_block *sb)
8108{
8109 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008110 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008111
8112 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
8113 if (!ei)
8114 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008115
8116 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008117 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04008118 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04008119 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04008120 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008121 ei->delalloc_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008122 ei->disk_i_size = 0;
8123 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04008124 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008125 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08008126 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008127 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06008128 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008129
Josef Bacik9e0baf62011-07-15 15:16:44 +00008130 spin_lock_init(&ei->lock);
8131 ei->outstanding_extents = 0;
8132 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008133
Josef Bacik72ac3c02012-05-23 14:13:11 -04008134 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08008135 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008136
Miao Xie16cdcec2011-04-22 18:12:22 +08008137 ei->delayed_node = NULL;
8138
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008139 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02008140 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02008141 extent_io_tree_init(&ei->io_tree, &inode->i_data);
8142 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04008143 ei->io_tree.track_uptodate = 1;
8144 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05008145 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008146 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05008147 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008148 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008149 INIT_LIST_HEAD(&ei->delalloc_inodes);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008150 RB_CLEAR_NODE(&ei->rb_node);
8151
8152 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008153}
8154
Josef Bacikaaedb552013-10-11 14:44:09 -04008155#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
8156void btrfs_test_destroy_inode(struct inode *inode)
8157{
8158 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
8159 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8160}
8161#endif
8162
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008163static void btrfs_i_callback(struct rcu_head *head)
8164{
8165 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008166 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8167}
8168
Chris Mason39279cc2007-06-12 06:35:45 -04008169void btrfs_destroy_inode(struct inode *inode)
8170{
Chris Masone6dcd2d2008-07-17 12:53:50 -04008171 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008172 struct btrfs_root *root = BTRFS_I(inode)->root;
8173
Al Virob3d9b7a2012-06-09 13:51:19 -04008174 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04008175 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00008176 WARN_ON(BTRFS_I(inode)->outstanding_extents);
8177 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04008178 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
8179 WARN_ON(BTRFS_I(inode)->csum_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04008180
Chris Mason5a3f23d2009-03-31 13:27:11 -04008181 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05008182 * This can happen where we create an inode, but somebody else also
8183 * created the same inode and we need to destroy the one we already
8184 * created.
8185 */
8186 if (!root)
8187 goto free;
8188
Josef Bacik8a35d952012-05-23 14:26:42 -04008189 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
8190 &BTRFS_I(inode)->runtime_flags)) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008191 btrfs_info(root->fs_info, "inode %llu still on the orphan list",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02008192 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04008193 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04008194 }
Josef Bacik7b128762008-07-24 12:17:14 -04008195
Chris Masond3977122009-01-05 21:25:51 -05008196 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008197 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
8198 if (!ordered)
8199 break;
8200 else {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008201 btrfs_err(root->fs_info, "found ordered extent %llu %llu on inode cleanup",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02008202 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008203 btrfs_remove_ordered_extent(inode, ordered);
8204 btrfs_put_ordered_extent(ordered);
8205 btrfs_put_ordered_extent(ordered);
8206 }
8207 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008208 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008209 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05008210free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008211 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04008212}
8213
Al Viro45321ac2010-06-07 13:43:19 -04008214int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04008215{
8216 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04008217
Naohiro Aota6379ef92013-06-06 09:56:34 +00008218 if (root == NULL)
8219 return 1;
8220
Liu Bofa6ac872013-02-20 14:10:23 +00008221 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02008222 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04008223 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04008224 else
Al Viro45321ac2010-06-07 13:43:19 -04008225 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04008226}
8227
Sven Wegener0ee0fda2008-07-30 16:54:26 -04008228static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04008229{
8230 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
8231
8232 inode_init_once(&ei->vfs_inode);
8233}
8234
8235void btrfs_destroy_cachep(void)
8236{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10008237 /*
8238 * Make sure all delayed rcu free inodes are flushed before we
8239 * destroy cache.
8240 */
8241 rcu_barrier();
Chris Mason39279cc2007-06-12 06:35:45 -04008242 if (btrfs_inode_cachep)
8243 kmem_cache_destroy(btrfs_inode_cachep);
8244 if (btrfs_trans_handle_cachep)
8245 kmem_cache_destroy(btrfs_trans_handle_cachep);
8246 if (btrfs_transaction_cachep)
8247 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008248 if (btrfs_path_cachep)
8249 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05008250 if (btrfs_free_space_cachep)
8251 kmem_cache_destroy(btrfs_free_space_cachep);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008252 if (btrfs_delalloc_work_cachep)
8253 kmem_cache_destroy(btrfs_delalloc_work_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008254}
8255
8256int btrfs_init_cachep(void)
8257{
David Sterba837e1972012-09-07 03:00:48 -06008258 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008259 sizeof(struct btrfs_inode), 0,
8260 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04008261 if (!btrfs_inode_cachep)
8262 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008263
David Sterba837e1972012-09-07 03:00:48 -06008264 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008265 sizeof(struct btrfs_trans_handle), 0,
8266 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008267 if (!btrfs_trans_handle_cachep)
8268 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008269
David Sterba837e1972012-09-07 03:00:48 -06008270 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008271 sizeof(struct btrfs_transaction), 0,
8272 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008273 if (!btrfs_transaction_cachep)
8274 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008275
David Sterba837e1972012-09-07 03:00:48 -06008276 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008277 sizeof(struct btrfs_path), 0,
8278 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008279 if (!btrfs_path_cachep)
8280 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008281
David Sterba837e1972012-09-07 03:00:48 -06008282 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05008283 sizeof(struct btrfs_free_space), 0,
8284 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
8285 if (!btrfs_free_space_cachep)
8286 goto fail;
8287
Miao Xie8ccf6f192012-10-25 09:28:04 +00008288 btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
8289 sizeof(struct btrfs_delalloc_work), 0,
8290 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
8291 NULL);
8292 if (!btrfs_delalloc_work_cachep)
8293 goto fail;
8294
Chris Mason39279cc2007-06-12 06:35:45 -04008295 return 0;
8296fail:
8297 btrfs_destroy_cachep();
8298 return -ENOMEM;
8299}
8300
8301static int btrfs_getattr(struct vfsmount *mnt,
8302 struct dentry *dentry, struct kstat *stat)
8303{
Miao Xiedf0af1a2013-01-29 10:11:59 +00008304 u64 delalloc_bytes;
Chris Mason39279cc2007-06-12 06:35:45 -04008305 struct inode *inode = dentry->d_inode;
David Sterbafadc0d82011-11-20 07:33:38 -05008306 u32 blocksize = inode->i_sb->s_blocksize;
8307
Chris Mason39279cc2007-06-12 06:35:45 -04008308 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04008309 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05008310 stat->blksize = PAGE_CACHE_SIZE;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008311
8312 spin_lock(&BTRFS_I(inode)->lock);
8313 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
8314 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05008315 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00008316 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04008317 return 0;
8318}
8319
Chris Masond3977122009-01-05 21:25:51 -05008320static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
8321 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04008322{
8323 struct btrfs_trans_handle *trans;
8324 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008325 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04008326 struct inode *new_inode = new_dentry->d_inode;
8327 struct inode *old_inode = old_dentry->d_inode;
8328 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008329 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008330 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04008331 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08008332 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008333
Li Zefan33345d012011-04-20 10:31:50 +08008334 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04008335 return -EPERM;
8336
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008337 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08008338 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05008339 return -EXDEV;
8340
Li Zefan33345d012011-04-20 10:31:50 +08008341 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
8342 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008343 return -ENOTEMPTY;
8344
Chris Mason39279cc2007-06-12 06:35:45 -04008345 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008346 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04008347 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05008348
8349
8350 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04008351 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05008352 new_dentry->d_name.name,
8353 new_dentry->d_name.len);
8354
8355 if (ret) {
8356 if (ret == -EEXIST) {
8357 /* we shouldn't get
8358 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05308359 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05008360 return ret;
8361 }
8362 } else {
8363 /* maybe -EOVERFLOW */
8364 return ret;
8365 }
8366 }
8367 ret = 0;
8368
Chris Mason5a3f23d2009-03-31 13:27:11 -04008369 /*
Chris Mason8d875f92014-08-12 10:47:42 -07008370 * we're using rename to replace one file with another. Start IO on it
8371 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04008372 */
Chris Mason8d875f92014-08-12 10:47:42 -07008373 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04008374 filemap_flush(old_inode->i_mapping);
8375
Yan, Zheng76dda932009-09-21 16:00:26 -04008376 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08008377 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008378 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008379 /*
8380 * We want to reserve the absolute worst case amount of items. So if
8381 * both inodes are subvols and we need to unlink them then that would
8382 * require 4 item modifications, but if they are both normal inodes it
8383 * would require 5 item modifications, so we'll assume their normal
8384 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
8385 * should cover the worst case number of items we'll modify.
8386 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04008387 trans = btrfs_start_transaction(root, 11);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008388 if (IS_ERR(trans)) {
8389 ret = PTR_ERR(trans);
8390 goto out_notrans;
8391 }
Chris Mason5f39d392007-10-15 16:14:19 -04008392
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008393 if (dest != root)
8394 btrfs_record_root_in_trans(trans, dest);
8395
Yan, Zhenga5719522009-09-24 09:17:31 -04008396 ret = btrfs_set_inode_index(new_dir, &index);
8397 if (ret)
8398 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008399
Miao Xie67de1172013-12-26 13:07:06 +08008400 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08008401 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008402 /* force full log commit if subvolume involved. */
Miao Xie995946d2014-04-02 19:51:06 +08008403 btrfs_set_log_full_commit(root->fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008404 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04008405 ret = btrfs_insert_inode_ref(trans, dest,
8406 new_dentry->d_name.name,
8407 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08008408 old_ino,
8409 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04008410 if (ret)
8411 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008412 /*
8413 * this is an ugly little race, but the rename is required
8414 * to make sure that if we crash, the inode is either at the
8415 * old name or the new one. pinning the log transaction lets
8416 * us make sure we don't allow a log commit to come in after
8417 * we unlink the name but before we add the new name back in.
8418 */
8419 btrfs_pin_log_trans(root);
8420 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04008421
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008422 inode_inc_iversion(old_dir);
8423 inode_inc_iversion(new_dir);
8424 inode_inc_iversion(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008425 old_dir->i_ctime = old_dir->i_mtime = ctime;
8426 new_dir->i_ctime = new_dir->i_mtime = ctime;
8427 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04008428
Chris Mason12fcfd22009-03-24 10:24:20 -04008429 if (old_dentry->d_parent != new_dentry->d_parent)
8430 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
8431
Li Zefan33345d012011-04-20 10:31:50 +08008432 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008433 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
8434 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
8435 old_dentry->d_name.name,
8436 old_dentry->d_name.len);
8437 } else {
Al Viro92986792011-03-04 17:14:37 +00008438 ret = __btrfs_unlink_inode(trans, root, old_dir,
8439 old_dentry->d_inode,
8440 old_dentry->d_name.name,
8441 old_dentry->d_name.len);
8442 if (!ret)
8443 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008444 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008445 if (ret) {
8446 btrfs_abort_transaction(trans, root, ret);
8447 goto out_fail;
8448 }
Chris Mason39279cc2007-06-12 06:35:45 -04008449
8450 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008451 inode_inc_iversion(new_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008452 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08008453 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008454 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
8455 root_objectid = BTRFS_I(new_inode)->location.objectid;
8456 ret = btrfs_unlink_subvol(trans, dest, new_dir,
8457 root_objectid,
8458 new_dentry->d_name.name,
8459 new_dentry->d_name.len);
8460 BUG_ON(new_inode->i_nlink == 0);
8461 } else {
8462 ret = btrfs_unlink_inode(trans, dest, new_dir,
8463 new_dentry->d_inode,
8464 new_dentry->d_name.name,
8465 new_dentry->d_name.len);
8466 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04008467 if (!ret && new_inode->i_nlink == 0)
Chris Masone02119d2008-09-05 16:13:11 -04008468 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008469 if (ret) {
8470 btrfs_abort_transaction(trans, root, ret);
8471 goto out_fail;
8472 }
Chris Mason39279cc2007-06-12 06:35:45 -04008473 }
Josef Bacikaec74772008-07-24 12:12:38 -04008474
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008475 ret = btrfs_add_link(trans, new_dir, old_inode,
8476 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04008477 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008478 if (ret) {
8479 btrfs_abort_transaction(trans, root, ret);
8480 goto out_fail;
8481 }
Chris Mason39279cc2007-06-12 06:35:45 -04008482
Miao Xie67de1172013-12-26 13:07:06 +08008483 if (old_inode->i_nlink == 1)
8484 BTRFS_I(old_inode)->dir_index = index;
8485
Li Zefan33345d012011-04-20 10:31:50 +08008486 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04008487 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00008488 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008489 btrfs_end_log_trans(root);
8490 }
Chris Mason39279cc2007-06-12 06:35:45 -04008491out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008492 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008493out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08008494 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008495 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008496
Chris Mason39279cc2007-06-12 06:35:45 -04008497 return ret;
8498}
8499
Miao Xie8ccf6f192012-10-25 09:28:04 +00008500static void btrfs_run_delalloc_work(struct btrfs_work *work)
8501{
8502 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04008503 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008504
8505 delalloc_work = container_of(work, struct btrfs_delalloc_work,
8506 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04008507 inode = delalloc_work->inode;
8508 if (delalloc_work->wait) {
8509 btrfs_wait_ordered_range(inode, 0, (u64)-1);
8510 } else {
8511 filemap_flush(inode->i_mapping);
8512 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8513 &BTRFS_I(inode)->runtime_flags))
8514 filemap_flush(inode->i_mapping);
8515 }
Miao Xie8ccf6f192012-10-25 09:28:04 +00008516
8517 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -04008518 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008519 else
Josef Bacik9f23e282013-10-28 15:03:41 -04008520 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008521 complete(&delalloc_work->completion);
8522}
8523
8524struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
8525 int wait, int delay_iput)
8526{
8527 struct btrfs_delalloc_work *work;
8528
8529 work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
8530 if (!work)
8531 return NULL;
8532
8533 init_completion(&work->completion);
8534 INIT_LIST_HEAD(&work->list);
8535 work->inode = inode;
8536 work->wait = wait;
8537 work->delay_iput = delay_iput;
Qu Wenruoa44903a2014-02-28 10:46:09 +08008538 btrfs_init_work(&work->work, btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008539
8540 return work;
8541}
8542
8543void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
8544{
8545 wait_for_completion(&work->completion);
8546 kmem_cache_free(btrfs_delalloc_work_cachep, work);
8547}
8548
Chris Masond352ac62008-09-29 15:18:18 -04008549/*
8550 * some fairly slow code that needs optimization. This walks the list
8551 * of all the inodes with pending delalloc and forces them to disk.
8552 */
Miao Xie6c255e62014-03-06 13:55:01 +08008553static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
8554 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -04008555{
Chris Masonea8c2812008-08-04 23:17:27 -04008556 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008557 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008558 struct btrfs_delalloc_work *work, *next;
8559 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00008560 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008561 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04008562
Miao Xie8ccf6f192012-10-25 09:28:04 +00008563 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008564 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00008565
Miao Xie573bfb72014-03-06 13:55:03 +08008566 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00008567 spin_lock(&root->delalloc_lock);
8568 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008569 while (!list_empty(&splice)) {
8570 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04008571 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008572
Miao Xieeb73c1b2013-05-15 07:48:22 +00008573 list_move_tail(&binode->delalloc_inodes,
8574 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008575 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00008576 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00008577 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008578 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008579 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008580 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008581
8582 work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
8583 if (unlikely(!work)) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -04008584 if (delay_iput)
8585 btrfs_add_delayed_iput(inode);
8586 else
8587 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008588 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +08008589 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008590 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008591 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +08008592 btrfs_queue_work(root->fs_info->flush_workers,
8593 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +08008594 ret++;
8595 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +08008596 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008597 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00008598 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04008599 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008600 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04008601
Wang Shilonga1ecaab2014-04-02 19:53:32 +08008602out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00008603 list_for_each_entry_safe(work, next, &works, list) {
8604 list_del_init(&work->list);
8605 btrfs_wait_and_free_delalloc_work(work);
8606 }
8607
Miao Xieeb73c1b2013-05-15 07:48:22 +00008608 if (!list_empty_careful(&splice)) {
8609 spin_lock(&root->delalloc_lock);
8610 list_splice_tail(&splice, &root->delalloc_inodes);
8611 spin_unlock(&root->delalloc_lock);
8612 }
Miao Xie573bfb72014-03-06 13:55:03 +08008613 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00008614 return ret;
8615}
8616
8617int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
8618{
8619 int ret;
8620
Wang Shilong2c21b4d2014-01-14 19:42:20 +08008621 if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00008622 return -EROFS;
8623
Miao Xie6c255e62014-03-06 13:55:01 +08008624 ret = __start_delalloc_inodes(root, delay_iput, -1);
8625 if (ret > 0)
8626 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +00008627 /*
8628 * the filemap_flush will queue IO into the worker threads, but
Chris Mason8c8bee12008-09-29 11:19:10 -04008629 * we have to make sure the IO is actually started and that
8630 * ordered extents get created before we return
8631 */
8632 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05008633 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05008634 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04008635 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05008636 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
8637 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04008638 }
8639 atomic_dec(&root->fs_info->async_submit_draining);
Miao Xieeb73c1b2013-05-15 07:48:22 +00008640 return ret;
8641}
8642
Miao Xie6c255e62014-03-06 13:55:01 +08008643int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
8644 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +00008645{
8646 struct btrfs_root *root;
8647 struct list_head splice;
8648 int ret;
8649
Wang Shilong2c21b4d2014-01-14 19:42:20 +08008650 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00008651 return -EROFS;
8652
8653 INIT_LIST_HEAD(&splice);
8654
Miao Xie573bfb72014-03-06 13:55:03 +08008655 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00008656 spin_lock(&fs_info->delalloc_root_lock);
8657 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +08008658 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00008659 root = list_first_entry(&splice, struct btrfs_root,
8660 delalloc_root);
8661 root = btrfs_grab_fs_root(root);
8662 BUG_ON(!root);
8663 list_move_tail(&root->delalloc_root,
8664 &fs_info->delalloc_roots);
8665 spin_unlock(&fs_info->delalloc_root_lock);
8666
Miao Xie6c255e62014-03-06 13:55:01 +08008667 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +00008668 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +08008669 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +00008670 goto out;
8671
Miao Xie6c255e62014-03-06 13:55:01 +08008672 if (nr != -1) {
8673 nr -= ret;
8674 WARN_ON(nr < 0);
8675 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008676 spin_lock(&fs_info->delalloc_root_lock);
8677 }
8678 spin_unlock(&fs_info->delalloc_root_lock);
8679
Miao Xie6c255e62014-03-06 13:55:01 +08008680 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +00008681 atomic_inc(&fs_info->async_submit_draining);
8682 while (atomic_read(&fs_info->nr_async_submits) ||
8683 atomic_read(&fs_info->async_delalloc_pages)) {
8684 wait_event(fs_info->async_submit_wait,
8685 (atomic_read(&fs_info->nr_async_submits) == 0 &&
8686 atomic_read(&fs_info->async_delalloc_pages) == 0));
8687 }
8688 atomic_dec(&fs_info->async_submit_draining);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008689out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00008690 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00008691 spin_lock(&fs_info->delalloc_root_lock);
8692 list_splice_tail(&splice, &fs_info->delalloc_roots);
8693 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008694 }
Miao Xie573bfb72014-03-06 13:55:03 +08008695 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008696 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04008697}
8698
Chris Mason39279cc2007-06-12 06:35:45 -04008699static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
8700 const char *symname)
8701{
8702 struct btrfs_trans_handle *trans;
8703 struct btrfs_root *root = BTRFS_I(dir)->root;
8704 struct btrfs_path *path;
8705 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05008706 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04008707 int err;
8708 int drop_inode = 0;
8709 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05308710 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008711 int name_len;
8712 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04008713 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04008714 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04008715 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04008716
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01008717 name_len = strlen(symname);
Chris Mason39279cc2007-06-12 06:35:45 -04008718 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
8719 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05008720
Josef Bacik9ed74f22009-09-11 16:12:44 -04008721 /*
8722 * 2 items for inode item and ref
8723 * 2 items for dir items
8724 * 1 item for xattr if selinux is on
8725 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04008726 trans = btrfs_start_transaction(root, 5);
8727 if (IS_ERR(trans))
8728 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05008729
Li Zefan581bb052011-04-20 10:06:11 +08008730 err = btrfs_find_free_ino(root, &objectid);
8731 if (err)
8732 goto out_unlock;
8733
Josef Bacikaec74772008-07-24 12:12:38 -04008734 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08008735 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04008736 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008737 if (IS_ERR(inode)) {
8738 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008739 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008740 }
Chris Mason39279cc2007-06-12 06:35:45 -04008741
Eric Paris2a7dba32011-02-01 11:05:39 -05008742 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04008743 if (err) {
8744 drop_inode = 1;
8745 goto out_unlock;
8746 }
8747
Casey Schauflerad19db72011-12-15 10:09:07 -05008748 /*
8749 * If the active LSM wants to access the inode during
8750 * d_instantiate it needs these. Smack checks to see
8751 * if the filesystem supports xattrs by looking at the
8752 * ops vector.
8753 */
8754 inode->i_fop = &btrfs_file_operations;
8755 inode->i_op = &btrfs_file_inode_operations;
8756
Josef Bacika1b075d2010-11-19 20:36:11 +00008757 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04008758 if (err)
8759 drop_inode = 1;
8760 else {
8761 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04008762 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05008763 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04008764 }
Chris Mason39279cc2007-06-12 06:35:45 -04008765 if (drop_inode)
8766 goto out_unlock;
8767
8768 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07008769 if (!path) {
8770 err = -ENOMEM;
8771 drop_inode = 1;
8772 goto out_unlock;
8773 }
Li Zefan33345d012011-04-20 10:31:50 +08008774 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008775 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008776 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
8777 datasize = btrfs_file_extent_calc_inline_size(name_len);
8778 err = btrfs_insert_empty_item(trans, root, path, &key,
8779 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04008780 if (err) {
8781 drop_inode = 1;
Julia Lawallb0839162011-05-14 07:10:51 +00008782 btrfs_free_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04008783 goto out_unlock;
8784 }
Chris Mason5f39d392007-10-15 16:14:19 -04008785 leaf = path->nodes[0];
8786 ei = btrfs_item_ptr(leaf, path->slots[0],
8787 struct btrfs_file_extent_item);
8788 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
8789 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04008790 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04008791 btrfs_set_file_extent_encryption(leaf, ei, 0);
8792 btrfs_set_file_extent_compression(leaf, ei, 0);
8793 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
8794 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
8795
Chris Mason39279cc2007-06-12 06:35:45 -04008796 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04008797 write_extent_buffer(leaf, symname, ptr, name_len);
8798 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04008799 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04008800
Chris Mason39279cc2007-06-12 06:35:45 -04008801 inode->i_op = &btrfs_symlink_inode_operations;
8802 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04008803 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04008804 inode_set_bytes(inode, name_len);
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01008805 btrfs_i_size_write(inode, name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -04008806 err = btrfs_update_inode(trans, root, inode);
8807 if (err)
8808 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04008809
8810out_unlock:
Al Viro08c422c2011-12-23 07:58:13 -05008811 if (!err)
8812 d_instantiate(dentry, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008813 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04008814 if (drop_inode) {
8815 inode_dec_link_count(inode);
8816 iput(inode);
8817 }
Liu Bob53d3f52012-11-14 14:34:34 +00008818 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04008819 return err;
8820}
Chris Mason16432982008-04-10 10:23:21 -04008821
Josef Bacik0af3d002010-06-21 14:48:16 -04008822static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
8823 u64 start, u64 num_bytes, u64 min_size,
8824 loff_t actual_len, u64 *alloc_hint,
8825 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04008826{
Josef Bacik5dc562c2012-08-17 13:14:17 -04008827 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
8828 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04008829 struct btrfs_root *root = BTRFS_I(inode)->root;
8830 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04008831 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00008832 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05008833 u64 cur_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04008834 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04008835 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04008836
Josef Bacik0af3d002010-06-21 14:48:16 -04008837 if (trans)
8838 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04008839 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008840 if (own_trans) {
8841 trans = btrfs_start_transaction(root, 3);
8842 if (IS_ERR(trans)) {
8843 ret = PTR_ERR(trans);
8844 break;
8845 }
Yan Zhengd899e052008-10-30 14:25:28 -04008846 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00008847
Chris Mason154ea282013-03-05 11:11:26 -05008848 cur_bytes = min(num_bytes, 256ULL * 1024 * 1024);
8849 cur_bytes = max(cur_bytes, min_size);
Josef Bacik00361582013-08-14 14:02:47 -04008850 ret = btrfs_reserve_extent(root, cur_bytes, min_size, 0,
Miao Xiee570fd22014-06-19 10:42:50 +08008851 *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008852 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008853 if (own_trans)
8854 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008855 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00008856 }
8857
Yan Zhengd899e052008-10-30 14:25:28 -04008858 ret = insert_reserved_file_extent(trans, inode,
8859 cur_offset, ins.objectid,
8860 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00008861 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04008862 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008863 if (ret) {
Josef Bacik857cc2f2013-10-07 15:21:08 -04008864 btrfs_free_reserved_extent(root, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +08008865 ins.offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008866 btrfs_abort_transaction(trans, root, ret);
8867 if (own_trans)
8868 btrfs_end_transaction(trans, root);
8869 break;
8870 }
Chris Masona1ed8352009-09-11 12:27:37 -04008871 btrfs_drop_extent_cache(inode, cur_offset,
8872 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008873
Josef Bacik5dc562c2012-08-17 13:14:17 -04008874 em = alloc_extent_map();
8875 if (!em) {
8876 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
8877 &BTRFS_I(inode)->runtime_flags);
8878 goto next;
8879 }
8880
8881 em->start = cur_offset;
8882 em->orig_start = cur_offset;
8883 em->len = ins.offset;
8884 em->block_start = ins.objectid;
8885 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05008886 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04008887 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008888 em->bdev = root->fs_info->fs_devices->latest_bdev;
8889 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
8890 em->generation = trans->transid;
8891
8892 while (1) {
8893 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04008894 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04008895 write_unlock(&em_tree->lock);
8896 if (ret != -EEXIST)
8897 break;
8898 btrfs_drop_extent_cache(inode, cur_offset,
8899 cur_offset + ins.offset - 1,
8900 0);
8901 }
8902 free_extent_map(em);
8903next:
Yan Zhengd899e052008-10-30 14:25:28 -04008904 num_bytes -= ins.offset;
8905 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04008906 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00008907
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008908 inode_inc_iversion(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04008909 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02008910 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04008911 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04008912 (actual_len > inode->i_size) &&
8913 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00008914 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00008915 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00008916 else
Josef Bacik55a61d12010-11-22 18:50:32 +00008917 i_size = cur_offset;
8918 i_size_write(inode, i_size);
8919 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008920 }
8921
Yan Zhengd899e052008-10-30 14:25:28 -04008922 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008923
8924 if (ret) {
8925 btrfs_abort_transaction(trans, root, ret);
8926 if (own_trans)
8927 btrfs_end_transaction(trans, root);
8928 break;
8929 }
Yan Zhengd899e052008-10-30 14:25:28 -04008930
Josef Bacik0af3d002010-06-21 14:48:16 -04008931 if (own_trans)
8932 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008933 }
Yan Zhengd899e052008-10-30 14:25:28 -04008934 return ret;
8935}
8936
Josef Bacik0af3d002010-06-21 14:48:16 -04008937int btrfs_prealloc_file_range(struct inode *inode, int mode,
8938 u64 start, u64 num_bytes, u64 min_size,
8939 loff_t actual_len, u64 *alloc_hint)
8940{
8941 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
8942 min_size, actual_len, alloc_hint,
8943 NULL);
8944}
8945
8946int btrfs_prealloc_file_range_trans(struct inode *inode,
8947 struct btrfs_trans_handle *trans, int mode,
8948 u64 start, u64 num_bytes, u64 min_size,
8949 loff_t actual_len, u64 *alloc_hint)
8950{
8951 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
8952 min_size, actual_len, alloc_hint, trans);
8953}
8954
Chris Masone6dcd2d2008-07-17 12:53:50 -04008955static int btrfs_set_page_dirty(struct page *page)
8956{
Chris Masone6dcd2d2008-07-17 12:53:50 -04008957 return __set_page_dirty_nobuffers(page);
8958}
8959
Al Viro10556cb2011-06-20 19:28:19 -04008960static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05008961{
Li Zefanb83cc962010-12-20 16:04:08 +08008962 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00008963 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08008964
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00008965 if (mask & MAY_WRITE &&
8966 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
8967 if (btrfs_root_readonly(root))
8968 return -EROFS;
8969 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
8970 return -EACCES;
8971 }
Al Viro2830ba72011-06-20 19:16:29 -04008972 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05008973}
Chris Mason39279cc2007-06-12 06:35:45 -04008974
Filipe Mananaef3b9af2014-04-27 20:40:45 +01008975static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
8976{
8977 struct btrfs_trans_handle *trans;
8978 struct btrfs_root *root = BTRFS_I(dir)->root;
8979 struct inode *inode = NULL;
8980 u64 objectid;
8981 u64 index;
8982 int ret = 0;
8983
8984 /*
8985 * 5 units required for adding orphan entry
8986 */
8987 trans = btrfs_start_transaction(root, 5);
8988 if (IS_ERR(trans))
8989 return PTR_ERR(trans);
8990
8991 ret = btrfs_find_free_ino(root, &objectid);
8992 if (ret)
8993 goto out;
8994
8995 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
8996 btrfs_ino(dir), objectid, mode, &index);
8997 if (IS_ERR(inode)) {
8998 ret = PTR_ERR(inode);
8999 inode = NULL;
9000 goto out;
9001 }
9002
9003 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
9004 if (ret)
9005 goto out;
9006
9007 ret = btrfs_update_inode(trans, root, inode);
9008 if (ret)
9009 goto out;
9010
9011 inode->i_fop = &btrfs_file_operations;
9012 inode->i_op = &btrfs_file_inode_operations;
9013
9014 inode->i_mapping->a_ops = &btrfs_aops;
9015 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
9016 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
9017
9018 ret = btrfs_orphan_add(trans, inode);
9019 if (ret)
9020 goto out;
9021
Filipe Manana5762b5c2014-08-01 00:10:32 +01009022 /*
9023 * We set number of links to 0 in btrfs_new_inode(), and here we set
9024 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
9025 * through:
9026 *
9027 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
9028 */
9029 set_nlink(inode, 1);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009030 d_tmpfile(dentry, inode);
9031 mark_inode_dirty(inode);
9032
9033out:
9034 btrfs_end_transaction(trans, root);
9035 if (ret)
9036 iput(inode);
9037 btrfs_balance_delayed_items(root);
9038 btrfs_btree_balance_dirty(root);
9039
9040 return ret;
9041}
9042
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009043static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05009044 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04009045 .lookup = btrfs_lookup,
9046 .create = btrfs_create,
9047 .unlink = btrfs_unlink,
9048 .link = btrfs_link,
9049 .mkdir = btrfs_mkdir,
9050 .rmdir = btrfs_rmdir,
9051 .rename = btrfs_rename,
9052 .symlink = btrfs_symlink,
9053 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04009054 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009055 .setxattr = btrfs_setxattr,
9056 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05009057 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009058 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05009059 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009060 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009061 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -07009062 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009063 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -04009064};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009065static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009066 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05009067 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009068 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009069 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -07009070 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04009071};
Yan, Zheng76dda932009-09-21 16:00:26 -04009072
Alexey Dobriyan828c0952009-10-01 15:43:56 -07009073static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009074 .llseek = generic_file_llseek,
9075 .read = generic_read_dir,
Al Viro9cdda8d2013-05-22 16:48:09 -04009076 .iterate = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04009077 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04009078#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04009079 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04009080#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04009081 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04009082 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04009083};
9084
Chris Masond1310b22008-01-24 16:13:08 -05009085static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04009086 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05009087 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04009088 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04009089 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04009090 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04009091 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05009092 .set_bit_hook = btrfs_set_bit_hook,
9093 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04009094 .merge_extent_hook = btrfs_merge_extent_hook,
9095 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04009096};
9097
Chris Mason35054392009-01-21 13:11:13 -05009098/*
9099 * btrfs doesn't support the bmap operation because swapfiles
9100 * use bmap to make a mapping of extents in the file. They assume
9101 * these extents won't change over the life of the file and they
9102 * use the bmap result to do IO directly to the drive.
9103 *
9104 * the btrfs bmap call would return logical addresses that aren't
9105 * suitable for IO and they also will change frequently as COW
9106 * operations happen. So, swapfile + btrfs == corruption.
9107 *
9108 * For now we're avoiding this by dropping bmap.
9109 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07009110static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04009111 .readpage = btrfs_readpage,
9112 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04009113 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05009114 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04009115 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04009116 .invalidatepage = btrfs_invalidatepage,
9117 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04009118 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02009119 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04009120};
9121
Alexey Dobriyan7f094102009-09-21 17:01:10 -07009122static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04009123 .readpage = btrfs_readpage,
9124 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04009125 .invalidatepage = btrfs_invalidatepage,
9126 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04009127};
9128
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009129static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009130 .getattr = btrfs_getattr,
9131 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009132 .setxattr = btrfs_setxattr,
9133 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05009134 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009135 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05009136 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05009137 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009138 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009139 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04009140 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04009141};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009142static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04009143 .getattr = btrfs_getattr,
9144 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05009145 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009146 .setxattr = btrfs_setxattr,
9147 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04009148 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009149 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009150 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009151 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04009152 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -04009153};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009154static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009155 .readlink = generic_readlink,
9156 .follow_link = page_follow_link_light,
9157 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00009158 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -05009159 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05009160 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05009161 .setxattr = btrfs_setxattr,
9162 .getxattr = btrfs_getxattr,
9163 .listxattr = btrfs_listxattr,
9164 .removexattr = btrfs_removexattr,
Josef Bacike41f9412012-03-26 09:46:47 -04009165 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04009166};
Yan, Zheng76dda932009-09-21 16:00:26 -04009167
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04009168const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04009169 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -04009170 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -04009171};