blob: 7313571e18603953b766168de972b1f57fd02620 [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);
Filipe Mananad9f85962014-08-25 10:43:00 +0100781 if (ret) {
782 btrfs_drop_extent_cache(inode, async_extent->start,
783 async_extent->start +
784 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500785 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100786 }
Chris Mason771ed682008-11-06 22:02:51 -0500787
Chris Mason771ed682008-11-06 22:02:51 -0500788 /*
789 * clear dirty, set writeback and unlock the pages.
790 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400791 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400792 async_extent->start +
793 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400794 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
795 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400796 PAGE_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500797 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500798 async_extent->start,
799 async_extent->ram_size,
800 ins.objectid,
801 ins.offset, async_extent->pages,
802 async_extent->nr_pages);
Chris Mason771ed682008-11-06 22:02:51 -0500803 alloc_hint = ins.objectid + ins.offset;
804 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500805 if (ret)
806 goto out;
Chris Mason771ed682008-11-06 22:02:51 -0500807 cond_resched();
808 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100809 ret = 0;
810out:
811 return ret;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500812out_free_reserve:
Miao Xiee570fd22014-06-19 10:42:50 +0800813 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100814out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400815 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500816 async_extent->start +
817 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400818 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400819 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
820 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
821 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100822 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500823 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500824}
825
Josef Bacik4b46fce2010-05-23 11:00:55 -0400826static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
827 u64 num_bytes)
828{
829 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
830 struct extent_map *em;
831 u64 alloc_hint = 0;
832
833 read_lock(&em_tree->lock);
834 em = search_extent_mapping(em_tree, start, num_bytes);
835 if (em) {
836 /*
837 * if block start isn't an actual block number then find the
838 * first block in this inode and use that as a hint. If that
839 * block is also bogus then just don't worry about it.
840 */
841 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
842 free_extent_map(em);
843 em = search_extent_mapping(em_tree, 0, 0);
844 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
845 alloc_hint = em->block_start;
846 if (em)
847 free_extent_map(em);
848 } else {
849 alloc_hint = em->block_start;
850 free_extent_map(em);
851 }
852 }
853 read_unlock(&em_tree->lock);
854
855 return alloc_hint;
856}
857
Chris Mason771ed682008-11-06 22:02:51 -0500858/*
859 * when extent_io.c finds a delayed allocation range in the file,
860 * the call backs end up in this code. The basic idea is to
861 * allocate extents on disk for the range, and create ordered data structs
862 * in ram to track those extents.
863 *
864 * locked_page is the page that writepage had locked already. We use
865 * it to make sure we don't do extra locks or unlocks.
866 *
867 * *page_started is set to one if we unlock locked_page and do everything
868 * required to start IO on it. It may be clean and already done with
869 * IO when we return.
870 */
Josef Bacik00361582013-08-14 14:02:47 -0400871static noinline int cow_file_range(struct inode *inode,
872 struct page *locked_page,
873 u64 start, u64 end, int *page_started,
874 unsigned long *nr_written,
875 int unlock)
Chris Mason771ed682008-11-06 22:02:51 -0500876{
Josef Bacik00361582013-08-14 14:02:47 -0400877 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500878 u64 alloc_hint = 0;
879 u64 num_bytes;
880 unsigned long ram_size;
881 u64 disk_num_bytes;
882 u64 cur_alloc_size;
883 u64 blocksize = root->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500884 struct btrfs_key ins;
885 struct extent_map *em;
886 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
887 int ret = 0;
888
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400889 if (btrfs_is_free_space_inode(inode)) {
890 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500891 ret = -EINVAL;
892 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400893 }
Chris Mason771ed682008-11-06 22:02:51 -0500894
Qu Wenruofda28322013-02-26 08:10:22 +0000895 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500896 num_bytes = max(blocksize, num_bytes);
897 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500898
Chris Mason4cb53002011-05-24 15:35:30 -0400899 /* if this is a small write inside eof, kick off defrag */
Liu Bo4cb13e52012-03-29 09:57:45 -0400900 if (num_bytes < 64 * 1024 &&
901 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Josef Bacik00361582013-08-14 14:02:47 -0400902 btrfs_add_inode_defrag(NULL, inode);
Chris Mason4cb53002011-05-24 15:35:30 -0400903
Chris Mason771ed682008-11-06 22:02:51 -0500904 if (start == 0) {
905 /* lets try to make an inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400906 ret = cow_file_range_inline(root, inode, start, end, 0, 0,
907 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500908 if (ret == 0) {
Josef Bacikc2790a22013-07-29 11:20:47 -0400909 extent_clear_unlock_delalloc(inode, start, end, NULL,
910 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400911 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400912 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
913 PAGE_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000914
Chris Mason771ed682008-11-06 22:02:51 -0500915 *nr_written = *nr_written +
916 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
917 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500918 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100919 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100920 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500921 }
922 }
Chris Masonc8b97812008-10-29 14:49:59 -0400923
924 BUG_ON(disk_num_bytes >
David Sterba6c417612011-04-13 15:41:04 +0200925 btrfs_super_total_bytes(root->fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400926
Josef Bacik4b46fce2010-05-23 11:00:55 -0400927 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400928 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400929
Chris Masond3977122009-01-05 21:25:51 -0500930 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400931 unsigned long op;
932
Josef Bacik287a0ab2010-03-19 18:07:23 +0000933 cur_alloc_size = disk_num_bytes;
Josef Bacik00361582013-08-14 14:02:47 -0400934 ret = btrfs_reserve_extent(root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500935 root->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +0800936 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -0400937 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100938 goto out_unlock;
Chris Masond3977122009-01-05 21:25:51 -0500939
David Sterba172ddd62011-04-21 00:48:27 +0200940 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000941 if (!em) {
942 ret = -ENOMEM;
Liu Boace68ba2013-04-22 10:53:47 +0000943 goto out_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000944 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400945 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500946 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500947 ram_size = ins.offset;
948 em->len = ins.offset;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400949 em->mod_start = em->start;
950 em->mod_len = em->len;
Chris Masonc8b97812008-10-29 14:49:59 -0400951
Chris Masone6dcd2d2008-07-17 12:53:50 -0400952 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400953 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500954 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400955 em->ram_bytes = ram_size;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400956 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400957 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400958 em->generation = -1;
Chris Masonc8b97812008-10-29 14:49:59 -0400959
Chris Masond3977122009-01-05 21:25:51 -0500960 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400961 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400962 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400963 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400964 if (ret != -EEXIST) {
965 free_extent_map(em);
966 break;
967 }
968 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400969 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400970 }
Liu Boace68ba2013-04-22 10:53:47 +0000971 if (ret)
972 goto out_reserve;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400973
Chris Mason98d20f62008-04-14 09:46:10 -0400974 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400975 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -0500976 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +0000977 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +0100978 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -0400979
Yan Zheng17d217f2008-12-12 10:03:38 -0500980 if (root->root_key.objectid ==
981 BTRFS_DATA_RELOC_TREE_OBJECTID) {
982 ret = btrfs_reloc_clone_csums(inode, start,
983 cur_alloc_size);
Josef Bacik00361582013-08-14 14:02:47 -0400984 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +0100985 goto out_drop_extent_cache;
Yan Zheng17d217f2008-12-12 10:03:38 -0500986 }
987
Chris Masond3977122009-01-05 21:25:51 -0500988 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -0400989 break;
Chris Masond3977122009-01-05 21:25:51 -0500990
Chris Masonc8b97812008-10-29 14:49:59 -0400991 /* we're not doing compressed IO, don't unlock the first
992 * page (which the caller expects to stay locked), don't
993 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -0400994 *
995 * Do set the Private2 bit so we know this page was properly
996 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -0400997 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400998 op = unlock ? PAGE_UNLOCK : 0;
999 op |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001000
Josef Bacikc2790a22013-07-29 11:20:47 -04001001 extent_clear_unlock_delalloc(inode, start,
1002 start + ram_size - 1, locked_page,
1003 EXTENT_LOCKED | EXTENT_DELALLOC,
1004 op);
Chris Masonc8b97812008-10-29 14:49:59 -04001005 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001006 num_bytes -= cur_alloc_size;
1007 alloc_hint = ins.objectid + ins.offset;
1008 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -04001009 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001010out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001011 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001012
Filipe Mananad9f85962014-08-25 10:43:00 +01001013out_drop_extent_cache:
1014 btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001015out_reserve:
Miao Xiee570fd22014-06-19 10:42:50 +08001016 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001017out_unlock:
Josef Bacikc2790a22013-07-29 11:20:47 -04001018 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Josef Bacik151a41b2013-07-29 13:22:24 -04001019 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
1020 EXTENT_DELALLOC | EXTENT_DEFRAG,
1021 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
1022 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001023 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001024}
Chris Masonc8b97812008-10-29 14:49:59 -04001025
Chris Mason771ed682008-11-06 22:02:51 -05001026/*
1027 * work queue call back to started compression on a file and pages
1028 */
1029static noinline void async_cow_start(struct btrfs_work *work)
1030{
1031 struct async_cow *async_cow;
1032 int num_added = 0;
1033 async_cow = container_of(work, struct async_cow, work);
1034
1035 compress_file_range(async_cow->inode, async_cow->locked_page,
1036 async_cow->start, async_cow->end, async_cow,
1037 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001038 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001039 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001040 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001041 }
Chris Mason771ed682008-11-06 22:02:51 -05001042}
1043
1044/*
1045 * work queue call back to submit previously compressed pages
1046 */
1047static noinline void async_cow_submit(struct btrfs_work *work)
1048{
1049 struct async_cow *async_cow;
1050 struct btrfs_root *root;
1051 unsigned long nr_pages;
1052
1053 async_cow = container_of(work, struct async_cow, work);
1054
1055 root = async_cow->root;
1056 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
1057 PAGE_CACHE_SHIFT;
1058
Josef Bacik66657b32012-08-01 15:36:24 -04001059 if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) <
Liu Bo287082b2012-06-28 04:02:24 -06001060 5 * 1024 * 1024 &&
Chris Mason771ed682008-11-06 22:02:51 -05001061 waitqueue_active(&root->fs_info->async_submit_wait))
1062 wake_up(&root->fs_info->async_submit_wait);
1063
Chris Masond3977122009-01-05 21:25:51 -05001064 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001065 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001066}
Chris Masonc8b97812008-10-29 14:49:59 -04001067
Chris Mason771ed682008-11-06 22:02:51 -05001068static noinline void async_cow_free(struct btrfs_work *work)
1069{
1070 struct async_cow *async_cow;
1071 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001072 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001073 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001074 kfree(async_cow);
1075}
1076
1077static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1078 u64 start, u64 end, int *page_started,
1079 unsigned long *nr_written)
1080{
1081 struct async_cow *async_cow;
1082 struct btrfs_root *root = BTRFS_I(inode)->root;
1083 unsigned long nr_pages;
1084 u64 cur_end;
Liu Bo287082b2012-06-28 04:02:24 -06001085 int limit = 10 * 1024 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -05001086
Chris Masona3429ab2009-10-08 12:30:20 -04001087 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1088 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001089 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001090 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001091 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001092 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001093 async_cow->root = root;
1094 async_cow->locked_page = locked_page;
1095 async_cow->start = start;
1096
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001097 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
Chris Mason771ed682008-11-06 22:02:51 -05001098 cur_end = end;
1099 else
1100 cur_end = min(end, start + 512 * 1024 - 1);
1101
1102 async_cow->end = cur_end;
1103 INIT_LIST_HEAD(&async_cow->extents);
1104
Liu Bo9e0af232014-08-15 23:36:53 +08001105 btrfs_init_work(&async_cow->work,
1106 btrfs_delalloc_helper,
1107 async_cow_start, async_cow_submit,
1108 async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001109
Chris Mason771ed682008-11-06 22:02:51 -05001110 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
1111 PAGE_CACHE_SHIFT;
1112 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
1113
Qu Wenruoafe3d242014-02-28 10:46:07 +08001114 btrfs_queue_work(root->fs_info->delalloc_workers,
1115 &async_cow->work);
Chris Mason771ed682008-11-06 22:02:51 -05001116
1117 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
1118 wait_event(root->fs_info->async_submit_wait,
1119 (atomic_read(&root->fs_info->async_delalloc_pages) <
1120 limit));
1121 }
1122
Chris Masond3977122009-01-05 21:25:51 -05001123 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -05001124 atomic_read(&root->fs_info->async_delalloc_pages)) {
1125 wait_event(root->fs_info->async_submit_wait,
1126 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1127 0));
1128 }
1129
1130 *nr_written += nr_pages;
1131 start = cur_end + 1;
1132 }
1133 *page_started = 1;
1134 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001135}
1136
Chris Masond3977122009-01-05 21:25:51 -05001137static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001138 u64 bytenr, u64 num_bytes)
1139{
1140 int ret;
1141 struct btrfs_ordered_sum *sums;
1142 LIST_HEAD(list);
1143
Yan Zheng07d400a2009-01-06 11:42:00 -05001144 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001145 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001146 if (ret == 0 && list_empty(&list))
1147 return 0;
1148
1149 while (!list_empty(&list)) {
1150 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1151 list_del(&sums->list);
1152 kfree(sums);
1153 }
1154 return 1;
1155}
1156
Chris Masond352ac62008-09-29 15:18:18 -04001157/*
1158 * when nowcow writeback call back. This checks for snapshots or COW copies
1159 * of the extents that exist in the file, and COWs the file as required.
1160 *
1161 * If no cow copies or snapshots exist, we write directly to the existing
1162 * blocks on disk
1163 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001164static noinline int run_delalloc_nocow(struct inode *inode,
1165 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001166 u64 start, u64 end, int *page_started, int force,
1167 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001168{
Chris Masonbe20aa92007-12-17 20:14:01 -05001169 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001170 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001171 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001172 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001173 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001174 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001175 u64 cow_start;
1176 u64 cur_offset;
1177 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001178 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001179 u64 disk_bytenr;
1180 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001181 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001182 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001183 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001184 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001185 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001186 int nocow;
1187 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001188 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001189 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001190
1191 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001192 if (!path) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001193 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1194 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001195 EXTENT_DO_ACCOUNTING |
1196 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001197 PAGE_CLEAR_DIRTY |
1198 PAGE_SET_WRITEBACK |
1199 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001200 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001201 }
Li Zefan82d59022011-04-20 10:33:24 +08001202
Liu Bo83eea1f2012-07-10 05:28:39 -06001203 nolock = btrfs_is_free_space_inode(inode);
Li Zefan82d59022011-04-20 10:33:24 +08001204
1205 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001206 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001207 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001208 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001209
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001210 if (IS_ERR(trans)) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001211 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1212 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001213 EXTENT_DO_ACCOUNTING |
1214 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001215 PAGE_CLEAR_DIRTY |
1216 PAGE_SET_WRITEBACK |
1217 PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001218 btrfs_free_path(path);
1219 return PTR_ERR(trans);
1220 }
1221
Josef Bacik74b21072011-04-13 12:02:53 -04001222 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001223
Yan Zheng80ff3852008-10-30 14:20:02 -04001224 cow_start = (u64)-1;
1225 cur_offset = start;
1226 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001227 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001228 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001229 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001230 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001231 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1232 leaf = path->nodes[0];
1233 btrfs_item_key_to_cpu(leaf, &found_key,
1234 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001235 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001236 found_key.type == BTRFS_EXTENT_DATA_KEY)
1237 path->slots[0]--;
1238 }
1239 check_prev = 0;
1240next_slot:
1241 leaf = path->nodes[0];
1242 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1243 ret = btrfs_next_leaf(root, path);
Josef Bacikd788a342013-10-25 16:55:08 -04001244 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001245 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001246 if (ret > 0)
1247 break;
1248 leaf = path->nodes[0];
1249 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001250
Yan Zheng80ff3852008-10-30 14:20:02 -04001251 nocow = 0;
1252 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001253 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001254 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001255
Li Zefan33345d012011-04-20 10:31:50 +08001256 if (found_key.objectid > ino ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001257 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1258 found_key.offset > end)
1259 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001260
Yan Zheng80ff3852008-10-30 14:20:02 -04001261 if (found_key.offset > cur_offset) {
1262 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001263 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001264 goto out_check;
1265 }
Chris Masonc31f8832008-01-08 15:46:31 -05001266
Yan Zheng80ff3852008-10-30 14:20:02 -04001267 fi = btrfs_item_ptr(leaf, path->slots[0],
1268 struct btrfs_file_extent_item);
1269 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001270
Josef Bacikcc95bef2013-04-04 14:31:27 -04001271 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001272 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1273 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001274 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001275 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001276 extent_end = found_key.offset +
1277 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001278 disk_num_bytes =
1279 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001280 if (extent_end <= start) {
1281 path->slots[0]++;
1282 goto next_slot;
1283 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001284 if (disk_bytenr == 0)
1285 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001286 if (btrfs_file_extent_compression(leaf, fi) ||
1287 btrfs_file_extent_encryption(leaf, fi) ||
1288 btrfs_file_extent_other_encoding(leaf, fi))
1289 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001290 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1291 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001292 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001293 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001294 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001295 found_key.offset -
1296 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001297 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001298 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001299 disk_bytenr += cur_offset - found_key.offset;
1300 num_bytes = min(end + 1, extent_end) - cur_offset;
1301 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001302 * if there are pending snapshots for this root,
1303 * we fall into common COW way.
1304 */
1305 if (!nolock) {
1306 err = btrfs_start_nocow_write(root);
1307 if (!err)
1308 goto out_check;
1309 }
1310 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001311 * force cow if csum exists in the range.
1312 * this ensure that csum for a given extent are
1313 * either valid or do not exist.
1314 */
1315 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1316 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001317 nocow = 1;
1318 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1319 extent_end = found_key.offset +
Chris Mason514ac8a2014-01-03 21:07:00 -08001320 btrfs_file_extent_inline_len(leaf,
1321 path->slots[0], fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001322 extent_end = ALIGN(extent_end, root->sectorsize);
1323 } else {
1324 BUG_ON(1);
1325 }
1326out_check:
1327 if (extent_end <= start) {
1328 path->slots[0]++;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001329 if (!nolock && nocow)
1330 btrfs_end_nocow_write(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001331 goto next_slot;
1332 }
1333 if (!nocow) {
1334 if (cow_start == (u64)-1)
1335 cow_start = cur_offset;
1336 cur_offset = extent_end;
1337 if (cur_offset > end)
1338 break;
1339 path->slots[0]++;
1340 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001341 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001342
David Sterbab3b4aa72011-04-21 01:20:15 +02001343 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001344 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001345 ret = cow_file_range(inode, locked_page,
1346 cow_start, found_key.offset - 1,
1347 page_started, nr_written, 1);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001348 if (ret) {
1349 if (!nolock && nocow)
1350 btrfs_end_nocow_write(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001351 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001352 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001353 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001354 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001355
Yan Zhengd899e052008-10-30 14:25:28 -04001356 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1357 struct extent_map *em;
1358 struct extent_map_tree *em_tree;
1359 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001360 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001361 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001362 em->start = cur_offset;
Josef Bacik70c8a912012-10-11 16:54:30 -04001363 em->orig_start = found_key.offset - extent_offset;
Yan Zhengd899e052008-10-30 14:25:28 -04001364 em->len = num_bytes;
1365 em->block_len = num_bytes;
1366 em->block_start = disk_bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05001367 em->orig_block_len = disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001368 em->ram_bytes = ram_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04001369 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001370 em->mod_start = em->start;
1371 em->mod_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04001372 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacikb11e2342012-12-03 10:58:15 -05001373 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001374 em->generation = -1;
Yan Zhengd899e052008-10-30 14:25:28 -04001375 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001376 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001377 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001378 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001379 if (ret != -EEXIST) {
1380 free_extent_map(em);
1381 break;
1382 }
1383 btrfs_drop_extent_cache(inode, em->start,
1384 em->start + em->len - 1, 0);
1385 }
1386 type = BTRFS_ORDERED_PREALLOC;
1387 } else {
1388 type = BTRFS_ORDERED_NOCOW;
1389 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001390
1391 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001392 num_bytes, num_bytes, type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001393 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001394
Yan, Zhengefa56462010-05-16 10:49:59 -04001395 if (root->root_key.objectid ==
1396 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1397 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1398 num_bytes);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001399 if (ret) {
1400 if (!nolock && nocow)
1401 btrfs_end_nocow_write(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001402 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001403 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001404 }
1405
Josef Bacikc2790a22013-07-29 11:20:47 -04001406 extent_clear_unlock_delalloc(inode, cur_offset,
1407 cur_offset + num_bytes - 1,
1408 locked_page, EXTENT_LOCKED |
1409 EXTENT_DELALLOC, PAGE_UNLOCK |
1410 PAGE_SET_PRIVATE2);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001411 if (!nolock && nocow)
1412 btrfs_end_nocow_write(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001413 cur_offset = extent_end;
1414 if (cur_offset > end)
1415 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001416 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001417 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001418
Josef Bacik17ca04a2012-05-31 15:58:55 -04001419 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001420 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001421 cur_offset = end;
1422 }
1423
Yan Zheng80ff3852008-10-30 14:20:02 -04001424 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001425 ret = cow_file_range(inode, locked_page, cow_start, end,
1426 page_started, nr_written, 1);
Josef Bacikd788a342013-10-25 16:55:08 -04001427 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001428 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001429 }
1430
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001431error:
Miao Xiea698d0752012-09-20 01:51:59 -06001432 err = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001433 if (!ret)
1434 ret = err;
1435
Josef Bacik17ca04a2012-05-31 15:58:55 -04001436 if (ret && cur_offset < end)
Josef Bacikc2790a22013-07-29 11:20:47 -04001437 extent_clear_unlock_delalloc(inode, cur_offset, end,
1438 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001439 EXTENT_DELALLOC | EXTENT_DEFRAG |
1440 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1441 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001442 PAGE_SET_WRITEBACK |
1443 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001444 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001445 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001446}
1447
Chris Masond352ac62008-09-29 15:18:18 -04001448/*
1449 * extent_io.c call back to do delayed allocation processing
1450 */
Chris Masonc8b97812008-10-29 14:49:59 -04001451static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001452 u64 start, u64 end, int *page_started,
1453 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001454{
Chris Masonbe20aa92007-12-17 20:14:01 -05001455 int ret;
Chris Mason7f366cf2009-03-12 20:12:45 -04001456 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona2135012008-06-25 16:01:30 -04001457
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001458 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) {
Chris Masonc8b97812008-10-29 14:49:59 -04001459 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001460 page_started, 1, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001461 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001462 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001463 page_started, 0, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001464 } else if (!btrfs_test_opt(root, COMPRESS) &&
1465 !(BTRFS_I(inode)->force_compress) &&
1466 !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS)) {
Chris Mason7f366cf2009-03-12 20:12:45 -04001467 ret = cow_file_range(inode, locked_page, start, end,
1468 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001469 } else {
1470 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1471 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001472 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001473 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001474 }
Chris Masonb888db22007-08-27 16:49:44 -04001475 return ret;
1476}
1477
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001478static void btrfs_split_extent_hook(struct inode *inode,
1479 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001480{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001481 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001482 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001483 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001484
Josef Bacik9e0baf62011-07-15 15:16:44 +00001485 spin_lock(&BTRFS_I(inode)->lock);
1486 BTRFS_I(inode)->outstanding_extents++;
1487 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001488}
1489
1490/*
1491 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1492 * extents so we can keep track of new extents that are just merged onto old
1493 * extents, such as when we are doing sequential writes, so we can properly
1494 * account for the metadata space we'll need.
1495 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001496static void btrfs_merge_extent_hook(struct inode *inode,
1497 struct extent_state *new,
1498 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001499{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001500 /* not delalloc, ignore it */
1501 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001502 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001503
Josef Bacik9e0baf62011-07-15 15:16:44 +00001504 spin_lock(&BTRFS_I(inode)->lock);
1505 BTRFS_I(inode)->outstanding_extents--;
1506 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001507}
1508
Miao Xieeb73c1b2013-05-15 07:48:22 +00001509static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1510 struct inode *inode)
1511{
1512 spin_lock(&root->delalloc_lock);
1513 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1514 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1515 &root->delalloc_inodes);
1516 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1517 &BTRFS_I(inode)->runtime_flags);
1518 root->nr_delalloc_inodes++;
1519 if (root->nr_delalloc_inodes == 1) {
1520 spin_lock(&root->fs_info->delalloc_root_lock);
1521 BUG_ON(!list_empty(&root->delalloc_root));
1522 list_add_tail(&root->delalloc_root,
1523 &root->fs_info->delalloc_roots);
1524 spin_unlock(&root->fs_info->delalloc_root_lock);
1525 }
1526 }
1527 spin_unlock(&root->delalloc_lock);
1528}
1529
1530static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1531 struct inode *inode)
1532{
1533 spin_lock(&root->delalloc_lock);
1534 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1535 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1536 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1537 &BTRFS_I(inode)->runtime_flags);
1538 root->nr_delalloc_inodes--;
1539 if (!root->nr_delalloc_inodes) {
1540 spin_lock(&root->fs_info->delalloc_root_lock);
1541 BUG_ON(list_empty(&root->delalloc_root));
1542 list_del_init(&root->delalloc_root);
1543 spin_unlock(&root->fs_info->delalloc_root_lock);
1544 }
1545 }
1546 spin_unlock(&root->delalloc_lock);
1547}
1548
Chris Masond352ac62008-09-29 15:18:18 -04001549/*
1550 * extent_io.c set_bit_hook, used to track delayed allocation
1551 * bytes in this file, and to maintain the list of inodes that
1552 * have pending delalloc work to be done.
1553 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001554static void btrfs_set_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001555 struct extent_state *state, unsigned long *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001556{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001557
Chris Mason75eff682008-12-15 15:54:40 -05001558 /*
1559 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001560 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001561 * bit, which is only set or cleared with irqs on
1562 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001563 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001564 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001565 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001566 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001567
Josef Bacik9e0baf62011-07-15 15:16:44 +00001568 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001569 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001570 } else {
1571 spin_lock(&BTRFS_I(inode)->lock);
1572 BTRFS_I(inode)->outstanding_extents++;
1573 spin_unlock(&BTRFS_I(inode)->lock);
1574 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001575
Miao Xie963d6782013-01-29 10:10:51 +00001576 __percpu_counter_add(&root->fs_info->delalloc_bytes, len,
1577 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001578 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001579 BTRFS_I(inode)->delalloc_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001580 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001581 &BTRFS_I(inode)->runtime_flags))
1582 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001583 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001584 }
Chris Mason291d6732008-01-29 15:55:23 -05001585}
1586
Chris Masond352ac62008-09-29 15:18:18 -04001587/*
1588 * extent_io.c clear_bit_hook, see set_bit_hook for why
1589 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001590static void btrfs_clear_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001591 struct extent_state *state,
1592 unsigned long *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001593{
Chris Mason75eff682008-12-15 15:54:40 -05001594 /*
1595 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001596 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001597 * bit, which is only set or cleared with irqs on
1598 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001599 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001600 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001601 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001602 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001603
Josef Bacik9e0baf62011-07-15 15:16:44 +00001604 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001605 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001606 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1607 spin_lock(&BTRFS_I(inode)->lock);
1608 BTRFS_I(inode)->outstanding_extents--;
1609 spin_unlock(&BTRFS_I(inode)->lock);
1610 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001611
Josef Bacikb6d08f02013-09-27 14:57:43 -04001612 /*
1613 * We don't reserve metadata space for space cache inodes so we
1614 * don't need to call dellalloc_release_metadata if there is an
1615 * error.
1616 */
1617 if (*bits & EXTENT_DO_ACCOUNTING &&
1618 root != root->fs_info->tree_root)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001619 btrfs_delalloc_release_metadata(inode, len);
1620
Josef Bacik0cb59c92010-07-02 12:14:14 -04001621 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Josef Bacik7ee9e442013-06-21 16:37:03 -04001622 && do_list && !(state->state & EXTENT_NORESERVE))
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001623 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001624
Miao Xie963d6782013-01-29 10:10:51 +00001625 __percpu_counter_add(&root->fs_info->delalloc_bytes, -len,
1626 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001627 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001628 BTRFS_I(inode)->delalloc_bytes -= len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001629 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001630 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001631 &BTRFS_I(inode)->runtime_flags))
1632 btrfs_del_delalloc_inode(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001633 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001634 }
Chris Mason291d6732008-01-29 15:55:23 -05001635}
1636
Chris Masond352ac62008-09-29 15:18:18 -04001637/*
1638 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1639 * we don't create bios that span stripes or chunks
1640 */
David Woodhouse64a16702009-07-15 23:29:37 +01001641int btrfs_merge_bio_hook(int rw, struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001642 size_t size, struct bio *bio,
1643 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001644{
1645 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001646 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001647 u64 length = 0;
1648 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001649 int ret;
1650
Chris Mason771ed682008-11-06 22:02:51 -05001651 if (bio_flags & EXTENT_BIO_COMPRESSED)
1652 return 0;
1653
Kent Overstreet4f024f32013-10-11 15:44:27 -07001654 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001655 map_length = length;
David Woodhouse64a16702009-07-15 23:29:37 +01001656 ret = btrfs_map_block(root->fs_info, rw, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001657 &map_length, NULL, 0);
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001658 /* Will always return 0 with map_multi == NULL */
Jeff Mahoney3444a972011-10-03 23:23:13 -04001659 BUG_ON(ret < 0);
Chris Masond3977122009-01-05 21:25:51 -05001660 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001661 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001662 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001663}
1664
Chris Masond352ac62008-09-29 15:18:18 -04001665/*
1666 * in order to insert checksums into the metadata in large chunks,
1667 * we wait until bio submission time. All the pages in the bio are
1668 * checksummed and sums are attached onto the ordered extent record.
1669 *
1670 * At IO completion time the cums attached on the ordered extent record
1671 * are inserted into the btree
1672 */
Chris Masond3977122009-01-05 21:25:51 -05001673static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1674 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001675 unsigned long bio_flags,
1676 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001677{
Chris Mason065631f2008-02-20 12:07:25 -05001678 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001679 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001680
Chris Masond20f7042008-12-08 16:58:54 -05001681 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001682 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001683 return 0;
1684}
Chris Masone0156402008-04-16 11:15:20 -04001685
Chris Mason4a69a412008-11-06 22:03:00 -05001686/*
1687 * in order to insert checksums into the metadata in large chunks,
1688 * we wait until bio submission time. All the pages in the bio are
1689 * checksummed and sums are attached onto the ordered extent record.
1690 *
1691 * At IO completion time the cums attached on the ordered extent record
1692 * are inserted into the btree
1693 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001694static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001695 int mirror_num, unsigned long bio_flags,
1696 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001697{
1698 struct btrfs_root *root = BTRFS_I(inode)->root;
Stefan Behrens61891922012-11-05 18:51:52 +01001699 int ret;
1700
1701 ret = btrfs_map_bio(root, rw, bio, mirror_num, 1);
1702 if (ret)
1703 bio_endio(bio, ret);
1704 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001705}
1706
Chris Masond352ac62008-09-29 15:18:18 -04001707/*
Chris Masoncad321a2008-12-17 14:51:42 -05001708 * extent_io.c submission hook. This does the right thing for csum calculation
1709 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001710 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001711static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001712 int mirror_num, unsigned long bio_flags,
1713 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001714{
1715 struct btrfs_root *root = BTRFS_I(inode)->root;
1716 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001717 int skip_sum;
Jeff Mahoney04173412011-10-03 23:23:12 -04001718 int metadata = 0;
Josef Bacikb812ce22012-11-16 13:56:32 -05001719 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001720
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001721 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001722
Liu Bo83eea1f2012-07-10 05:28:39 -06001723 if (btrfs_is_free_space_inode(inode))
Jeff Mahoney04173412011-10-03 23:23:12 -04001724 metadata = 2;
1725
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001726 if (!(rw & REQ_WRITE)) {
Josef Bacik5fd02042012-05-02 14:00:54 -04001727 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1728 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001729 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001730
Chris Masond20f7042008-12-08 16:58:54 -05001731 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001732 ret = btrfs_submit_compressed_read(inode, bio,
1733 mirror_num,
1734 bio_flags);
1735 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001736 } else if (!skip_sum) {
1737 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1738 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001739 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001740 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001741 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001742 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001743 /* csum items have already been cloned */
1744 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1745 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001746 /* we're doing a write, do the async checksumming */
Stefan Behrens61891922012-11-05 18:51:52 +01001747 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001748 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001749 bio_flags, bio_offset,
1750 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001751 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001752 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001753 } else if (!skip_sum) {
1754 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1755 if (ret)
1756 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001757 }
1758
Chris Mason0b86a832008-03-24 15:01:56 -04001759mapit:
Stefan Behrens61891922012-11-05 18:51:52 +01001760 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
1761
1762out:
1763 if (ret < 0)
1764 bio_endio(bio, ret);
1765 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001766}
Chris Mason6885f302008-02-20 16:11:05 -05001767
Chris Masond352ac62008-09-29 15:18:18 -04001768/*
1769 * given a list of ordered sums record them in the inode. This happens
1770 * at IO completion time based on sums calculated at bio submission time.
1771 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001772static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001773 struct inode *inode, u64 file_offset,
1774 struct list_head *list)
1775{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001776 struct btrfs_ordered_sum *sum;
1777
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001778 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00001779 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05001780 btrfs_csum_file_blocks(trans,
1781 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00001782 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001783 }
1784 return 0;
1785}
1786
Josef Bacik2ac55d42010-02-03 19:33:23 +00001787int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1788 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001789{
Julia Lawall6c1500f2012-11-03 20:30:18 +00001790 WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04001791 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001792 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001793}
1794
Chris Masond352ac62008-09-29 15:18:18 -04001795/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001796struct btrfs_writepage_fixup {
1797 struct page *page;
1798 struct btrfs_work work;
1799};
1800
Christoph Hellwigb2950862008-12-02 09:54:17 -05001801static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001802{
1803 struct btrfs_writepage_fixup *fixup;
1804 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001805 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001806 struct page *page;
1807 struct inode *inode;
1808 u64 page_start;
1809 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001810 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001811
1812 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1813 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001814again:
Chris Mason247e7432008-07-17 12:53:51 -04001815 lock_page(page);
1816 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1817 ClearPageChecked(page);
1818 goto out_page;
1819 }
1820
1821 inode = page->mapping->host;
1822 page_start = page_offset(page);
1823 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1824
Josef Bacik2ac55d42010-02-03 19:33:23 +00001825 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001826 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001827
1828 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001829 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001830 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001831
1832 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1833 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001834 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1835 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001836 unlock_page(page);
1837 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001838 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04001839 goto again;
1840 }
Chris Mason247e7432008-07-17 12:53:51 -04001841
Jeff Mahoney87826df2012-02-15 16:23:57 +01001842 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
1843 if (ret) {
1844 mapping_set_error(page->mapping, ret);
1845 end_extent_writepage(page, ret, page_start, page_end);
1846 ClearPageChecked(page);
1847 goto out;
1848 }
1849
Josef Bacik2ac55d42010-02-03 19:33:23 +00001850 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001851 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001852 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04001853out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001854 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1855 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001856out_page:
1857 unlock_page(page);
1858 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08001859 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04001860}
1861
1862/*
1863 * There are a few paths in the higher layers of the kernel that directly
1864 * set the page dirty bit without asking the filesystem if it is a
1865 * good idea. This causes problems because we want to make sure COW
1866 * properly happens and the data=ordered rules are followed.
1867 *
Chris Masonc8b97812008-10-29 14:49:59 -04001868 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001869 * hasn't been properly setup for IO. We kick off an async process
1870 * to fix it up. The async helper will wait for ordered extents, set
1871 * the delalloc bit and make it safe to write the page.
1872 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001873static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001874{
1875 struct inode *inode = page->mapping->host;
1876 struct btrfs_writepage_fixup *fixup;
1877 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001878
Chris Mason8b62b722009-09-02 16:53:46 -04001879 /* this page is properly in the ordered list */
1880 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001881 return 0;
1882
1883 if (PageChecked(page))
1884 return -EAGAIN;
1885
1886 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1887 if (!fixup)
1888 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001889
Chris Mason247e7432008-07-17 12:53:51 -04001890 SetPageChecked(page);
1891 page_cache_get(page);
Liu Bo9e0af232014-08-15 23:36:53 +08001892 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
1893 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04001894 fixup->page = page;
Qu Wenruodc6e3202014-02-28 10:46:14 +08001895 btrfs_queue_work(root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001896 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04001897}
1898
Yan Zhengd899e052008-10-30 14:25:28 -04001899static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1900 struct inode *inode, u64 file_pos,
1901 u64 disk_bytenr, u64 disk_num_bytes,
1902 u64 num_bytes, u64 ram_bytes,
1903 u8 compression, u8 encryption,
1904 u16 other_encoding, int extent_type)
1905{
1906 struct btrfs_root *root = BTRFS_I(inode)->root;
1907 struct btrfs_file_extent_item *fi;
1908 struct btrfs_path *path;
1909 struct extent_buffer *leaf;
1910 struct btrfs_key ins;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00001911 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04001912 int ret;
1913
1914 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07001915 if (!path)
1916 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04001917
Chris Masona1ed8352009-09-11 12:27:37 -04001918 /*
1919 * we may be replacing one extent in the tree with another.
1920 * The new extent is pinned in the extent map, and we don't want
1921 * to drop it from the cache until it is completely in the btree.
1922 *
1923 * So, tell btrfs_drop_extents to leave this extent in the cache.
1924 * the caller is expected to unpin it and allow it to be merged
1925 * with the others.
1926 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00001927 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
1928 file_pos + num_bytes, NULL, 0,
1929 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001930 if (ret)
1931 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001932
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00001933 if (!extent_inserted) {
1934 ins.objectid = btrfs_ino(inode);
1935 ins.offset = file_pos;
1936 ins.type = BTRFS_EXTENT_DATA_KEY;
1937
1938 path->leave_spinning = 1;
1939 ret = btrfs_insert_empty_item(trans, root, path, &ins,
1940 sizeof(*fi));
1941 if (ret)
1942 goto out;
1943 }
Yan Zhengd899e052008-10-30 14:25:28 -04001944 leaf = path->nodes[0];
1945 fi = btrfs_item_ptr(leaf, path->slots[0],
1946 struct btrfs_file_extent_item);
1947 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1948 btrfs_set_file_extent_type(leaf, fi, extent_type);
1949 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1950 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1951 btrfs_set_file_extent_offset(leaf, fi, 0);
1952 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1953 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1954 btrfs_set_file_extent_compression(leaf, fi, compression);
1955 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1956 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04001957
Yan Zhengd899e052008-10-30 14:25:28 -04001958 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04001959 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04001960
1961 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04001962
1963 ins.objectid = disk_bytenr;
1964 ins.offset = disk_num_bytes;
1965 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001966 ret = btrfs_alloc_reserved_file_extent(trans, root,
1967 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08001968 btrfs_ino(inode), file_pos, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001969out:
Yan Zhengd899e052008-10-30 14:25:28 -04001970 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04001971
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001972 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04001973}
1974
Liu Bo38c227d2013-01-29 03:18:40 +00001975/* snapshot-aware defrag */
1976struct sa_defrag_extent_backref {
1977 struct rb_node node;
1978 struct old_sa_defrag_extent *old;
1979 u64 root_id;
1980 u64 inum;
1981 u64 file_pos;
1982 u64 extent_offset;
1983 u64 num_bytes;
1984 u64 generation;
1985};
1986
1987struct old_sa_defrag_extent {
1988 struct list_head list;
1989 struct new_sa_defrag_extent *new;
1990
1991 u64 extent_offset;
1992 u64 bytenr;
1993 u64 offset;
1994 u64 len;
1995 int count;
1996};
1997
1998struct new_sa_defrag_extent {
1999 struct rb_root root;
2000 struct list_head head;
2001 struct btrfs_path *path;
2002 struct inode *inode;
2003 u64 file_pos;
2004 u64 len;
2005 u64 bytenr;
2006 u64 disk_len;
2007 u8 compress_type;
2008};
2009
2010static int backref_comp(struct sa_defrag_extent_backref *b1,
2011 struct sa_defrag_extent_backref *b2)
2012{
2013 if (b1->root_id < b2->root_id)
2014 return -1;
2015 else if (b1->root_id > b2->root_id)
2016 return 1;
2017
2018 if (b1->inum < b2->inum)
2019 return -1;
2020 else if (b1->inum > b2->inum)
2021 return 1;
2022
2023 if (b1->file_pos < b2->file_pos)
2024 return -1;
2025 else if (b1->file_pos > b2->file_pos)
2026 return 1;
2027
2028 /*
2029 * [------------------------------] ===> (a range of space)
2030 * |<--->| |<---->| =============> (fs/file tree A)
2031 * |<---------------------------->| ===> (fs/file tree B)
2032 *
2033 * A range of space can refer to two file extents in one tree while
2034 * refer to only one file extent in another tree.
2035 *
2036 * So we may process a disk offset more than one time(two extents in A)
2037 * and locate at the same extent(one extent in B), then insert two same
2038 * backrefs(both refer to the extent in B).
2039 */
2040 return 0;
2041}
2042
2043static void backref_insert(struct rb_root *root,
2044 struct sa_defrag_extent_backref *backref)
2045{
2046 struct rb_node **p = &root->rb_node;
2047 struct rb_node *parent = NULL;
2048 struct sa_defrag_extent_backref *entry;
2049 int ret;
2050
2051 while (*p) {
2052 parent = *p;
2053 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2054
2055 ret = backref_comp(backref, entry);
2056 if (ret < 0)
2057 p = &(*p)->rb_left;
2058 else
2059 p = &(*p)->rb_right;
2060 }
2061
2062 rb_link_node(&backref->node, parent, p);
2063 rb_insert_color(&backref->node, root);
2064}
2065
2066/*
2067 * Note the backref might has changed, and in this case we just return 0.
2068 */
2069static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2070 void *ctx)
2071{
2072 struct btrfs_file_extent_item *extent;
2073 struct btrfs_fs_info *fs_info;
2074 struct old_sa_defrag_extent *old = ctx;
2075 struct new_sa_defrag_extent *new = old->new;
2076 struct btrfs_path *path = new->path;
2077 struct btrfs_key key;
2078 struct btrfs_root *root;
2079 struct sa_defrag_extent_backref *backref;
2080 struct extent_buffer *leaf;
2081 struct inode *inode = new->inode;
2082 int slot;
2083 int ret;
2084 u64 extent_offset;
2085 u64 num_bytes;
2086
2087 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
2088 inum == btrfs_ino(inode))
2089 return 0;
2090
2091 key.objectid = root_id;
2092 key.type = BTRFS_ROOT_ITEM_KEY;
2093 key.offset = (u64)-1;
2094
2095 fs_info = BTRFS_I(inode)->root->fs_info;
2096 root = btrfs_read_fs_root_no_name(fs_info, &key);
2097 if (IS_ERR(root)) {
2098 if (PTR_ERR(root) == -ENOENT)
2099 return 0;
2100 WARN_ON(1);
2101 pr_debug("inum=%llu, offset=%llu, root_id=%llu\n",
2102 inum, offset, root_id);
2103 return PTR_ERR(root);
2104 }
2105
2106 key.objectid = inum;
2107 key.type = BTRFS_EXTENT_DATA_KEY;
2108 if (offset > (u64)-1 << 32)
2109 key.offset = 0;
2110 else
2111 key.offset = offset;
2112
2113 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302114 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002115 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002116 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002117
2118 while (1) {
2119 cond_resched();
2120
2121 leaf = path->nodes[0];
2122 slot = path->slots[0];
2123
2124 if (slot >= btrfs_header_nritems(leaf)) {
2125 ret = btrfs_next_leaf(root, path);
2126 if (ret < 0) {
2127 goto out;
2128 } else if (ret > 0) {
2129 ret = 0;
2130 goto out;
2131 }
2132 continue;
2133 }
2134
2135 path->slots[0]++;
2136
2137 btrfs_item_key_to_cpu(leaf, &key, slot);
2138
2139 if (key.objectid > inum)
2140 goto out;
2141
2142 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2143 continue;
2144
2145 extent = btrfs_item_ptr(leaf, slot,
2146 struct btrfs_file_extent_item);
2147
2148 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2149 continue;
2150
Liu Boe68afa42013-07-01 22:13:26 +08002151 /*
2152 * 'offset' refers to the exact key.offset,
2153 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2154 * (key.offset - extent_offset).
2155 */
2156 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002157 continue;
2158
Liu Boe68afa42013-07-01 22:13:26 +08002159 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002160 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002161
Liu Bo38c227d2013-01-29 03:18:40 +00002162 if (extent_offset >= old->extent_offset + old->offset +
2163 old->len || extent_offset + num_bytes <=
2164 old->extent_offset + old->offset)
2165 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002166 break;
2167 }
2168
2169 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2170 if (!backref) {
2171 ret = -ENOENT;
2172 goto out;
2173 }
2174
2175 backref->root_id = root_id;
2176 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002177 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002178 backref->num_bytes = num_bytes;
2179 backref->extent_offset = extent_offset;
2180 backref->generation = btrfs_file_extent_generation(leaf, extent);
2181 backref->old = old;
2182 backref_insert(&new->root, backref);
2183 old->count++;
2184out:
2185 btrfs_release_path(path);
2186 WARN_ON(ret);
2187 return ret;
2188}
2189
2190static noinline bool record_extent_backrefs(struct btrfs_path *path,
2191 struct new_sa_defrag_extent *new)
2192{
2193 struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info;
2194 struct old_sa_defrag_extent *old, *tmp;
2195 int ret;
2196
2197 new->path = path;
2198
2199 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002200 ret = iterate_inodes_from_logical(old->bytenr +
2201 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002202 path, record_one_backref,
2203 old);
Josef Bacik4724b102013-11-05 11:11:40 -05002204 if (ret < 0 && ret != -ENOENT)
2205 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002206
2207 /* no backref to be processed for this extent */
2208 if (!old->count) {
2209 list_del(&old->list);
2210 kfree(old);
2211 }
2212 }
2213
2214 if (list_empty(&new->head))
2215 return false;
2216
2217 return true;
2218}
2219
2220static int relink_is_mergable(struct extent_buffer *leaf,
2221 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002222 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002223{
Liu Bo116e0022013-08-02 16:30:40 +08002224 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002225 return 0;
2226
2227 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2228 return 0;
2229
Liu Bo116e0022013-08-02 16:30:40 +08002230 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2231 return 0;
2232
2233 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002234 btrfs_file_extent_other_encoding(leaf, fi))
2235 return 0;
2236
2237 return 1;
2238}
2239
2240/*
2241 * Note the backref might has changed, and in this case we just return 0.
2242 */
2243static noinline int relink_extent_backref(struct btrfs_path *path,
2244 struct sa_defrag_extent_backref *prev,
2245 struct sa_defrag_extent_backref *backref)
2246{
2247 struct btrfs_file_extent_item *extent;
2248 struct btrfs_file_extent_item *item;
2249 struct btrfs_ordered_extent *ordered;
2250 struct btrfs_trans_handle *trans;
2251 struct btrfs_fs_info *fs_info;
2252 struct btrfs_root *root;
2253 struct btrfs_key key;
2254 struct extent_buffer *leaf;
2255 struct old_sa_defrag_extent *old = backref->old;
2256 struct new_sa_defrag_extent *new = old->new;
2257 struct inode *src_inode = new->inode;
2258 struct inode *inode;
2259 struct extent_state *cached = NULL;
2260 int ret = 0;
2261 u64 start;
2262 u64 len;
2263 u64 lock_start;
2264 u64 lock_end;
2265 bool merge = false;
2266 int index;
2267
2268 if (prev && prev->root_id == backref->root_id &&
2269 prev->inum == backref->inum &&
2270 prev->file_pos + prev->num_bytes == backref->file_pos)
2271 merge = true;
2272
2273 /* step 1: get root */
2274 key.objectid = backref->root_id;
2275 key.type = BTRFS_ROOT_ITEM_KEY;
2276 key.offset = (u64)-1;
2277
2278 fs_info = BTRFS_I(src_inode)->root->fs_info;
2279 index = srcu_read_lock(&fs_info->subvol_srcu);
2280
2281 root = btrfs_read_fs_root_no_name(fs_info, &key);
2282 if (IS_ERR(root)) {
2283 srcu_read_unlock(&fs_info->subvol_srcu, index);
2284 if (PTR_ERR(root) == -ENOENT)
2285 return 0;
2286 return PTR_ERR(root);
2287 }
Liu Bo38c227d2013-01-29 03:18:40 +00002288
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002289 if (btrfs_root_readonly(root)) {
2290 srcu_read_unlock(&fs_info->subvol_srcu, index);
2291 return 0;
2292 }
2293
Liu Bo38c227d2013-01-29 03:18:40 +00002294 /* step 2: get inode */
2295 key.objectid = backref->inum;
2296 key.type = BTRFS_INODE_ITEM_KEY;
2297 key.offset = 0;
2298
2299 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2300 if (IS_ERR(inode)) {
2301 srcu_read_unlock(&fs_info->subvol_srcu, index);
2302 return 0;
2303 }
2304
2305 srcu_read_unlock(&fs_info->subvol_srcu, index);
2306
2307 /* step 3: relink backref */
2308 lock_start = backref->file_pos;
2309 lock_end = backref->file_pos + backref->num_bytes - 1;
2310 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2311 0, &cached);
2312
2313 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2314 if (ordered) {
2315 btrfs_put_ordered_extent(ordered);
2316 goto out_unlock;
2317 }
2318
2319 trans = btrfs_join_transaction(root);
2320 if (IS_ERR(trans)) {
2321 ret = PTR_ERR(trans);
2322 goto out_unlock;
2323 }
2324
2325 key.objectid = backref->inum;
2326 key.type = BTRFS_EXTENT_DATA_KEY;
2327 key.offset = backref->file_pos;
2328
2329 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2330 if (ret < 0) {
2331 goto out_free_path;
2332 } else if (ret > 0) {
2333 ret = 0;
2334 goto out_free_path;
2335 }
2336
2337 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2338 struct btrfs_file_extent_item);
2339
2340 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2341 backref->generation)
2342 goto out_free_path;
2343
2344 btrfs_release_path(path);
2345
2346 start = backref->file_pos;
2347 if (backref->extent_offset < old->extent_offset + old->offset)
2348 start += old->extent_offset + old->offset -
2349 backref->extent_offset;
2350
2351 len = min(backref->extent_offset + backref->num_bytes,
2352 old->extent_offset + old->offset + old->len);
2353 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2354
2355 ret = btrfs_drop_extents(trans, root, inode, start,
2356 start + len, 1);
2357 if (ret)
2358 goto out_free_path;
2359again:
2360 key.objectid = btrfs_ino(inode);
2361 key.type = BTRFS_EXTENT_DATA_KEY;
2362 key.offset = start;
2363
Liu Boa09a0a72013-03-11 09:20:58 +00002364 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002365 if (merge) {
2366 struct btrfs_file_extent_item *fi;
2367 u64 extent_len;
2368 struct btrfs_key found_key;
2369
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002370 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002371 if (ret < 0)
2372 goto out_free_path;
2373
2374 path->slots[0]--;
2375 leaf = path->nodes[0];
2376 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2377
2378 fi = btrfs_item_ptr(leaf, path->slots[0],
2379 struct btrfs_file_extent_item);
2380 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2381
Liu Bo116e0022013-08-02 16:30:40 +08002382 if (extent_len + found_key.offset == start &&
2383 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002384 btrfs_set_file_extent_num_bytes(leaf, fi,
2385 extent_len + len);
2386 btrfs_mark_buffer_dirty(leaf);
2387 inode_add_bytes(inode, len);
2388
2389 ret = 1;
2390 goto out_free_path;
2391 } else {
2392 merge = false;
2393 btrfs_release_path(path);
2394 goto again;
2395 }
2396 }
2397
2398 ret = btrfs_insert_empty_item(trans, root, path, &key,
2399 sizeof(*extent));
2400 if (ret) {
2401 btrfs_abort_transaction(trans, root, ret);
2402 goto out_free_path;
2403 }
2404
2405 leaf = path->nodes[0];
2406 item = btrfs_item_ptr(leaf, path->slots[0],
2407 struct btrfs_file_extent_item);
2408 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2409 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2410 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2411 btrfs_set_file_extent_num_bytes(leaf, item, len);
2412 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2413 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2414 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2415 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2416 btrfs_set_file_extent_encryption(leaf, item, 0);
2417 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2418
2419 btrfs_mark_buffer_dirty(leaf);
2420 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002421 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002422
2423 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
2424 new->disk_len, 0,
2425 backref->root_id, backref->inum,
2426 new->file_pos, 0); /* start - extent_offset */
2427 if (ret) {
2428 btrfs_abort_transaction(trans, root, ret);
2429 goto out_free_path;
2430 }
2431
2432 ret = 1;
2433out_free_path:
2434 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002435 path->leave_spinning = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002436 btrfs_end_transaction(trans, root);
2437out_unlock:
2438 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2439 &cached, GFP_NOFS);
2440 iput(inode);
2441 return ret;
2442}
2443
Liu Bo6f519562013-10-29 10:45:05 +08002444static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2445{
2446 struct old_sa_defrag_extent *old, *tmp;
2447
2448 if (!new)
2449 return;
2450
2451 list_for_each_entry_safe(old, tmp, &new->head, list) {
2452 list_del(&old->list);
2453 kfree(old);
2454 }
2455 kfree(new);
2456}
2457
Liu Bo38c227d2013-01-29 03:18:40 +00002458static void relink_file_extents(struct new_sa_defrag_extent *new)
2459{
2460 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002461 struct sa_defrag_extent_backref *backref;
2462 struct sa_defrag_extent_backref *prev = NULL;
2463 struct inode *inode;
2464 struct btrfs_root *root;
2465 struct rb_node *node;
2466 int ret;
2467
2468 inode = new->inode;
2469 root = BTRFS_I(inode)->root;
2470
2471 path = btrfs_alloc_path();
2472 if (!path)
2473 return;
2474
2475 if (!record_extent_backrefs(path, new)) {
2476 btrfs_free_path(path);
2477 goto out;
2478 }
2479 btrfs_release_path(path);
2480
2481 while (1) {
2482 node = rb_first(&new->root);
2483 if (!node)
2484 break;
2485 rb_erase(node, &new->root);
2486
2487 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2488
2489 ret = relink_extent_backref(path, prev, backref);
2490 WARN_ON(ret < 0);
2491
2492 kfree(prev);
2493
2494 if (ret == 1)
2495 prev = backref;
2496 else
2497 prev = NULL;
2498 cond_resched();
2499 }
2500 kfree(prev);
2501
2502 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002503out:
Liu Bo6f519562013-10-29 10:45:05 +08002504 free_sa_defrag_extent(new);
2505
Liu Bo38c227d2013-01-29 03:18:40 +00002506 atomic_dec(&root->fs_info->defrag_running);
2507 wake_up(&root->fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002508}
2509
2510static struct new_sa_defrag_extent *
2511record_old_file_extents(struct inode *inode,
2512 struct btrfs_ordered_extent *ordered)
2513{
2514 struct btrfs_root *root = BTRFS_I(inode)->root;
2515 struct btrfs_path *path;
2516 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002517 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002518 struct new_sa_defrag_extent *new;
2519 int ret;
2520
2521 new = kmalloc(sizeof(*new), GFP_NOFS);
2522 if (!new)
2523 return NULL;
2524
2525 new->inode = inode;
2526 new->file_pos = ordered->file_offset;
2527 new->len = ordered->len;
2528 new->bytenr = ordered->start;
2529 new->disk_len = ordered->disk_len;
2530 new->compress_type = ordered->compress_type;
2531 new->root = RB_ROOT;
2532 INIT_LIST_HEAD(&new->head);
2533
2534 path = btrfs_alloc_path();
2535 if (!path)
2536 goto out_kfree;
2537
2538 key.objectid = btrfs_ino(inode);
2539 key.type = BTRFS_EXTENT_DATA_KEY;
2540 key.offset = new->file_pos;
2541
2542 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2543 if (ret < 0)
2544 goto out_free_path;
2545 if (ret > 0 && path->slots[0] > 0)
2546 path->slots[0]--;
2547
2548 /* find out all the old extents for the file range */
2549 while (1) {
2550 struct btrfs_file_extent_item *extent;
2551 struct extent_buffer *l;
2552 int slot;
2553 u64 num_bytes;
2554 u64 offset;
2555 u64 end;
2556 u64 disk_bytenr;
2557 u64 extent_offset;
2558
2559 l = path->nodes[0];
2560 slot = path->slots[0];
2561
2562 if (slot >= btrfs_header_nritems(l)) {
2563 ret = btrfs_next_leaf(root, path);
2564 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002565 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002566 else if (ret > 0)
2567 break;
2568 continue;
2569 }
2570
2571 btrfs_item_key_to_cpu(l, &key, slot);
2572
2573 if (key.objectid != btrfs_ino(inode))
2574 break;
2575 if (key.type != BTRFS_EXTENT_DATA_KEY)
2576 break;
2577 if (key.offset >= new->file_pos + new->len)
2578 break;
2579
2580 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2581
2582 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2583 if (key.offset + num_bytes < new->file_pos)
2584 goto next;
2585
2586 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2587 if (!disk_bytenr)
2588 goto next;
2589
2590 extent_offset = btrfs_file_extent_offset(l, extent);
2591
2592 old = kmalloc(sizeof(*old), GFP_NOFS);
2593 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002594 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002595
2596 offset = max(new->file_pos, key.offset);
2597 end = min(new->file_pos + new->len, key.offset + num_bytes);
2598
2599 old->bytenr = disk_bytenr;
2600 old->extent_offset = extent_offset;
2601 old->offset = offset - key.offset;
2602 old->len = end - offset;
2603 old->new = new;
2604 old->count = 0;
2605 list_add_tail(&old->list, &new->head);
2606next:
2607 path->slots[0]++;
2608 cond_resched();
2609 }
2610
2611 btrfs_free_path(path);
2612 atomic_inc(&root->fs_info->defrag_running);
2613
2614 return new;
2615
Liu Bo38c227d2013-01-29 03:18:40 +00002616out_free_path:
2617 btrfs_free_path(path);
2618out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002619 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002620 return NULL;
2621}
2622
Miao Xiee570fd22014-06-19 10:42:50 +08002623static void btrfs_release_delalloc_bytes(struct btrfs_root *root,
2624 u64 start, u64 len)
2625{
2626 struct btrfs_block_group_cache *cache;
2627
2628 cache = btrfs_lookup_block_group(root->fs_info, start);
2629 ASSERT(cache);
2630
2631 spin_lock(&cache->lock);
2632 cache->delalloc_bytes -= len;
2633 spin_unlock(&cache->lock);
2634
2635 btrfs_put_block_group(cache);
2636}
2637
Chris Masond352ac62008-09-29 15:18:18 -04002638/* as ordered data IO finishes, this gets called so we can finish
2639 * an ordered extent if the range of bytes in the file it covers are
2640 * fully written.
2641 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002642static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002643{
Josef Bacik5fd02042012-05-02 14:00:54 -04002644 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002645 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002646 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002647 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002648 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002649 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002650 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002651 int ret = 0;
2652 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002653 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002654 bool truncated = false;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002655
Liu Bo83eea1f2012-07-10 05:28:39 -06002656 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002657
Josef Bacik5fd02042012-05-02 14:00:54 -04002658 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2659 ret = -EIO;
2660 goto out;
2661 }
2662
Josef Bacik77cef2e2013-08-29 13:57:21 -04002663 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2664 truncated = true;
2665 logical_len = ordered_extent->truncated_len;
2666 /* Truncated the entire extent, don't bother adding */
2667 if (!logical_len)
2668 goto out;
2669 }
2670
Yan, Zhengc2167752009-11-12 09:34:21 +00002671 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002672 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Josef Bacik6c760c02012-11-09 10:53:21 -05002673 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2674 if (nolock)
2675 trans = btrfs_join_transaction_nolock(root);
2676 else
2677 trans = btrfs_join_transaction(root);
2678 if (IS_ERR(trans)) {
2679 ret = PTR_ERR(trans);
2680 trans = NULL;
2681 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002682 }
Josef Bacik6c760c02012-11-09 10:53:21 -05002683 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
2684 ret = btrfs_update_inode_fallback(trans, root, inode);
2685 if (ret) /* -ENOMEM or corruption */
2686 btrfs_abort_transaction(trans, root, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002687 goto out;
2688 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002689
Josef Bacik2ac55d42010-02-03 19:33:23 +00002690 lock_extent_bits(io_tree, ordered_extent->file_offset,
2691 ordered_extent->file_offset + ordered_extent->len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002692 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002693
Liu Bo38c227d2013-01-29 03:18:40 +00002694 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2695 ordered_extent->file_offset + ordered_extent->len - 1,
2696 EXTENT_DEFRAG, 1, cached_state);
2697 if (ret) {
2698 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05002699 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00002700 /* the inode is shared */
2701 new = record_old_file_extents(inode, ordered_extent);
2702
2703 clear_extent_bit(io_tree, ordered_extent->file_offset,
2704 ordered_extent->file_offset + ordered_extent->len - 1,
2705 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2706 }
2707
Josef Bacik0cb59c92010-07-02 12:14:14 -04002708 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002709 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002710 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002711 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002712 if (IS_ERR(trans)) {
2713 ret = PTR_ERR(trans);
2714 trans = NULL;
2715 goto out_unlock;
2716 }
Chris Masona79b7d42014-05-22 16:18:52 -07002717
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002718 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002719
Chris Masonc8b97812008-10-29 14:49:59 -04002720 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002721 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002722 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002723 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00002724 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002725 ordered_extent->file_offset,
2726 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002727 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002728 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04002729 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002730 ret = insert_reserved_file_extent(trans, inode,
2731 ordered_extent->file_offset,
2732 ordered_extent->start,
2733 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04002734 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08002735 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002736 BTRFS_FILE_EXTENT_REG);
Miao Xiee570fd22014-06-19 10:42:50 +08002737 if (!ret)
2738 btrfs_release_delalloc_bytes(root,
2739 ordered_extent->start,
2740 ordered_extent->disk_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002741 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002742 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2743 ordered_extent->file_offset, ordered_extent->len,
2744 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002745 if (ret < 0) {
2746 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002747 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002748 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002749
Chris Masone6dcd2d2008-07-17 12:53:50 -04002750 add_pending_csums(trans, inode, ordered_extent->file_offset,
2751 &ordered_extent->list);
2752
Josef Bacik6c760c02012-11-09 10:53:21 -05002753 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2754 ret = btrfs_update_inode_fallback(trans, root, inode);
2755 if (ret) { /* -ENOMEM or corruption */
2756 btrfs_abort_transaction(trans, root, ret);
2757 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002758 }
2759 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002760out_unlock:
2761 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2762 ordered_extent->file_offset +
2763 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00002764out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002765 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04002766 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002767 if (trans)
2768 btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002769
Josef Bacik77cef2e2013-08-29 13:57:21 -04002770 if (ret || truncated) {
2771 u64 start, end;
2772
2773 if (truncated)
2774 start = ordered_extent->file_offset + logical_len;
2775 else
2776 start = ordered_extent->file_offset;
2777 end = ordered_extent->file_offset + ordered_extent->len - 1;
2778 clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS);
2779
2780 /* Drop the cache for the part of the extent we didn't write. */
2781 btrfs_drop_extent_cache(inode, start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04002782
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002783 /*
2784 * If the ordered extent had an IOERR or something else went
2785 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04002786 * back to the allocator. We only free the extent in the
2787 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002788 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04002789 if ((ret || !logical_len) &&
2790 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002791 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
2792 btrfs_free_reserved_extent(root, ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08002793 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002794 }
2795
2796
Josef Bacik5fd02042012-05-02 14:00:54 -04002797 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002798 * This needs to be done to make sure anybody waiting knows we are done
2799 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002800 */
2801 btrfs_remove_ordered_extent(inode, ordered_extent);
2802
Liu Bo38c227d2013-01-29 03:18:40 +00002803 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08002804 if (new) {
2805 if (ret) {
2806 free_sa_defrag_extent(new);
2807 atomic_dec(&root->fs_info->defrag_running);
2808 } else {
2809 relink_file_extents(new);
2810 }
2811 }
Liu Bo38c227d2013-01-29 03:18:40 +00002812
Chris Masone6dcd2d2008-07-17 12:53:50 -04002813 /* once for us */
2814 btrfs_put_ordered_extent(ordered_extent);
2815 /* once for the tree */
2816 btrfs_put_ordered_extent(ordered_extent);
2817
Josef Bacik5fd02042012-05-02 14:00:54 -04002818 return ret;
2819}
2820
2821static void finish_ordered_fn(struct btrfs_work *work)
2822{
2823 struct btrfs_ordered_extent *ordered_extent;
2824 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2825 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002826}
2827
Christoph Hellwigb2950862008-12-02 09:54:17 -05002828static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04002829 struct extent_state *state, int uptodate)
2830{
Josef Bacik5fd02042012-05-02 14:00:54 -04002831 struct inode *inode = page->mapping->host;
2832 struct btrfs_root *root = BTRFS_I(inode)->root;
2833 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08002834 struct btrfs_workqueue *wq;
2835 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04002836
liubo1abe9b82011-03-24 11:18:59 +00002837 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2838
Chris Mason8b62b722009-09-02 16:53:46 -04002839 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002840 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2841 end - start + 1, uptodate))
2842 return 0;
2843
Liu Bo9e0af232014-08-15 23:36:53 +08002844 if (btrfs_is_free_space_inode(inode)) {
2845 wq = root->fs_info->endio_freespace_worker;
2846 func = btrfs_freespace_write_helper;
2847 } else {
2848 wq = root->fs_info->endio_write_workers;
2849 func = btrfs_endio_write_helper;
2850 }
Josef Bacik5fd02042012-05-02 14:00:54 -04002851
Liu Bo9e0af232014-08-15 23:36:53 +08002852 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
2853 NULL);
2854 btrfs_queue_work(wq, &ordered_extent->work);
Josef Bacik5fd02042012-05-02 14:00:54 -04002855
2856 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04002857}
2858
Chris Masond352ac62008-09-29 15:18:18 -04002859/*
Chris Masond352ac62008-09-29 15:18:18 -04002860 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002861 * if there's a match, we allow the bio to finish. If not, the code in
2862 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002863 */
Miao Xiefacc8a222013-07-25 19:22:34 +08002864static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
2865 u64 phy_offset, struct page *page,
2866 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002867{
Miao Xie4eee4fa2012-12-21 09:17:45 +00002868 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04002869 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002870 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04002871 char *kaddr;
Chris Masonff79f812007-10-15 16:22:25 -04002872 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiefacc8a222013-07-25 19:22:34 +08002873 u32 csum_expected;
Chris Masonff79f812007-10-15 16:22:25 -04002874 u32 csum = ~(u32)0;
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002875 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
2876 DEFAULT_RATELIMIT_BURST);
Chris Masond1310b22008-01-24 16:13:08 -05002877
Chris Masond20f7042008-12-08 16:58:54 -05002878 if (PageChecked(page)) {
2879 ClearPageChecked(page);
2880 goto good;
2881 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002882
2883 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Jan Schmidt08d2f342011-05-04 16:18:50 +02002884 goto good;
Chris Masond20f7042008-12-08 16:58:54 -05002885
Yan Zheng17d217f2008-12-12 10:03:38 -05002886 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002887 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002888 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
2889 GFP_NOFS);
2890 return 0;
2891 }
2892
Miao Xiefacc8a222013-07-25 19:22:34 +08002893 phy_offset >>= inode->i_sb->s_blocksize_bits;
2894 csum_expected = *(((u32 *)io_bio->csum) + phy_offset);
Chris Masond3977122009-01-05 21:25:51 -05002895
Miao Xiefacc8a222013-07-25 19:22:34 +08002896 kaddr = kmap_atomic(page);
Liu Bob0496682013-03-14 14:57:45 +00002897 csum = btrfs_csum_data(kaddr + offset, csum, end - start + 1);
Chris Masonff79f812007-10-15 16:22:25 -04002898 btrfs_csum_final(csum, (char *)&csum);
Miao Xiefacc8a222013-07-25 19:22:34 +08002899 if (csum != csum_expected)
Chris Mason07157aa2007-08-30 08:50:51 -04002900 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002901
Cong Wang7ac687d2011-11-25 23:14:28 +08002902 kunmap_atomic(kaddr);
Chris Masond20f7042008-12-08 16:58:54 -05002903good:
Chris Mason07157aa2007-08-30 08:50:51 -04002904 return 0;
2905
2906zeroit:
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002907 if (__ratelimit(&_rs))
Miao Xiefacc8a222013-07-25 19:22:34 +08002908 btrfs_info(root->fs_info, "csum failed ino %llu off %llu csum %u expected csum %u",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002909 btrfs_ino(page->mapping->host), start, csum, csum_expected);
Chris Masondb945352007-10-15 16:15:53 -04002910 memset(kaddr + offset, 1, end - start + 1);
2911 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002912 kunmap_atomic(kaddr);
Miao Xiefacc8a222013-07-25 19:22:34 +08002913 if (csum_expected == 0)
Chris Mason3b951512008-04-17 11:29:12 -04002914 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04002915 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04002916}
Chris Masonb888db22007-08-27 16:49:44 -04002917
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002918struct delayed_iput {
2919 struct list_head list;
2920 struct inode *inode;
2921};
2922
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002923/* JDM: If this is fs-wide, why can't we add a pointer to
2924 * btrfs_inode instead and avoid the allocation? */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002925void btrfs_add_delayed_iput(struct inode *inode)
2926{
2927 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2928 struct delayed_iput *delayed;
2929
2930 if (atomic_add_unless(&inode->i_count, -1, 1))
2931 return;
2932
2933 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2934 delayed->inode = inode;
2935
2936 spin_lock(&fs_info->delayed_iput_lock);
2937 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2938 spin_unlock(&fs_info->delayed_iput_lock);
2939}
2940
2941void btrfs_run_delayed_iputs(struct btrfs_root *root)
2942{
2943 LIST_HEAD(list);
2944 struct btrfs_fs_info *fs_info = root->fs_info;
2945 struct delayed_iput *delayed;
2946 int empty;
2947
2948 spin_lock(&fs_info->delayed_iput_lock);
2949 empty = list_empty(&fs_info->delayed_iputs);
2950 spin_unlock(&fs_info->delayed_iput_lock);
2951 if (empty)
2952 return;
2953
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002954 spin_lock(&fs_info->delayed_iput_lock);
2955 list_splice_init(&fs_info->delayed_iputs, &list);
2956 spin_unlock(&fs_info->delayed_iput_lock);
2957
2958 while (!list_empty(&list)) {
2959 delayed = list_entry(list.next, struct delayed_iput, list);
2960 list_del(&delayed->list);
2961 iput(delayed->inode);
2962 kfree(delayed);
2963 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002964}
2965
Yan, Zhengd68fc572010-05-16 10:49:58 -04002966/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08002967 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04002968 * files in the subvolume, it removes orphan item and frees block_rsv
2969 * structure.
2970 */
2971void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
2972 struct btrfs_root *root)
2973{
Josef Bacik90290e12011-12-02 15:44:12 -05002974 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002975 int ret;
2976
Josef Bacik8a35d952012-05-23 14:26:42 -04002977 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04002978 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
2979 return;
2980
Josef Bacik90290e12011-12-02 15:44:12 -05002981 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04002982 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05002983 spin_unlock(&root->orphan_lock);
2984 return;
2985 }
2986
2987 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
2988 spin_unlock(&root->orphan_lock);
2989 return;
2990 }
2991
2992 block_rsv = root->orphan_block_rsv;
2993 root->orphan_block_rsv = NULL;
2994 spin_unlock(&root->orphan_lock);
2995
Miao Xie27cdeb72014-04-02 19:51:05 +08002996 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
Yan, Zhengd68fc572010-05-16 10:49:58 -04002997 btrfs_root_refs(&root->root_item) > 0) {
2998 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
2999 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003000 if (ret)
3001 btrfs_abort_transaction(trans, root, ret);
3002 else
Miao Xie27cdeb72014-04-02 19:51:05 +08003003 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
3004 &root->state);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003005 }
3006
Josef Bacik90290e12011-12-02 15:44:12 -05003007 if (block_rsv) {
3008 WARN_ON(block_rsv->size > 0);
3009 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003010 }
3011}
3012
3013/*
Josef Bacik7b128762008-07-24 12:17:14 -04003014 * This creates an orphan entry for the given inode in case something goes
3015 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003016 *
3017 * NOTE: caller of this function should reserve 5 units of metadata for
3018 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04003019 */
3020int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
3021{
3022 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003023 struct btrfs_block_rsv *block_rsv = NULL;
3024 int reserve = 0;
3025 int insert = 0;
3026 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003027
Yan, Zhengd68fc572010-05-16 10:49:58 -04003028 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06003029 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003030 if (!block_rsv)
3031 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04003032 }
3033
Yan, Zhengd68fc572010-05-16 10:49:58 -04003034 spin_lock(&root->orphan_lock);
3035 if (!root->orphan_block_rsv) {
3036 root->orphan_block_rsv = block_rsv;
3037 } else if (block_rsv) {
3038 btrfs_free_block_rsv(root, block_rsv);
3039 block_rsv = NULL;
3040 }
Josef Bacik7b128762008-07-24 12:17:14 -04003041
Josef Bacik8a35d952012-05-23 14:26:42 -04003042 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3043 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003044#if 0
3045 /*
3046 * For proper ENOSPC handling, we should do orphan
3047 * cleanup when mounting. But this introduces backward
3048 * compatibility issue.
3049 */
3050 if (!xchg(&root->orphan_item_inserted, 1))
3051 insert = 2;
3052 else
3053 insert = 1;
3054#endif
3055 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003056 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003057 }
Josef Bacik7b128762008-07-24 12:17:14 -04003058
Josef Bacik72ac3c02012-05-23 14:13:11 -04003059 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3060 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003061 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003062 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003063
Yan, Zhengd68fc572010-05-16 10:49:58 -04003064 /* grab metadata reservation from transaction handle */
3065 if (reserve) {
3066 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003067 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04003068 }
3069
3070 /* insert an orphan item to track this unlinked/truncated file */
3071 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08003072 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003073 if (ret) {
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003074 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003075 if (reserve) {
3076 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3077 &BTRFS_I(inode)->runtime_flags);
3078 btrfs_orphan_release_metadata(inode);
3079 }
3080 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003081 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3082 &BTRFS_I(inode)->runtime_flags);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003083 btrfs_abort_transaction(trans, root, ret);
3084 return ret;
3085 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003086 }
3087 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003088 }
3089
3090 /* insert an orphan item to track subvolume contains orphan files */
3091 if (insert >= 2) {
3092 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
3093 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003094 if (ret && ret != -EEXIST) {
3095 btrfs_abort_transaction(trans, root, ret);
3096 return ret;
3097 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003098 }
3099 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003100}
3101
3102/*
3103 * We have done the truncate/delete so we can go ahead and remove the orphan
3104 * item for this particular inode.
3105 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003106static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3107 struct inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003108{
3109 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003110 int delete_item = 0;
3111 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003112 int ret = 0;
3113
Yan, Zhengd68fc572010-05-16 10:49:58 -04003114 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003115 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3116 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003117 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003118
Josef Bacik72ac3c02012-05-23 14:13:11 -04003119 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3120 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003121 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003122 spin_unlock(&root->orphan_lock);
3123
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003124 if (delete_item) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003125 atomic_dec(&root->orphan_inodes);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003126 if (trans)
3127 ret = btrfs_del_orphan_item(trans, root,
3128 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04003129 }
Josef Bacik7b128762008-07-24 12:17:14 -04003130
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003131 if (release_rsv)
3132 btrfs_orphan_release_metadata(inode);
3133
Josef Bacik4ef31a42013-08-13 14:10:08 -04003134 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003135}
3136
3137/*
3138 * this cleans up any orphans that may be left on the list from the last use
3139 * of this root.
3140 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003141int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003142{
3143 struct btrfs_path *path;
3144 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003145 struct btrfs_key key, found_key;
3146 struct btrfs_trans_handle *trans;
3147 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003148 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003149 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3150
Yan, Zhengd68fc572010-05-16 10:49:58 -04003151 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003152 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003153
3154 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003155 if (!path) {
3156 ret = -ENOMEM;
3157 goto out;
3158 }
Josef Bacik7b128762008-07-24 12:17:14 -04003159 path->reada = -1;
3160
3161 key.objectid = BTRFS_ORPHAN_OBJECTID;
3162 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
3163 key.offset = (u64)-1;
3164
Josef Bacik7b128762008-07-24 12:17:14 -04003165 while (1) {
3166 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003167 if (ret < 0)
3168 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003169
3170 /*
3171 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003172 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003173 * find the key and see if we have stuff that matches
3174 */
3175 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003176 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003177 if (path->slots[0] == 0)
3178 break;
3179 path->slots[0]--;
3180 }
3181
3182 /* pull out the item */
3183 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003184 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3185
3186 /* make sure the item matches what we want */
3187 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3188 break;
3189 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
3190 break;
3191
3192 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003193 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003194
3195 /*
3196 * this is where we are basically btrfs_lookup, without the
3197 * crossing root thing. we store the inode number in the
3198 * offset of the orphan item.
3199 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003200
3201 if (found_key.offset == last_objectid) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003202 btrfs_err(root->fs_info,
3203 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003204 ret = -EINVAL;
3205 goto out;
3206 }
3207
3208 last_objectid = found_key.offset;
3209
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003210 found_key.objectid = found_key.offset;
3211 found_key.type = BTRFS_INODE_ITEM_KEY;
3212 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00003213 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303214 ret = PTR_ERR_OR_ZERO(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003215 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003216 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003217
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003218 if (ret == -ESTALE && root == root->fs_info->tree_root) {
3219 struct btrfs_root *dead_root;
3220 struct btrfs_fs_info *fs_info = root->fs_info;
3221 int is_dead_root = 0;
3222
3223 /*
3224 * this is an orphan in the tree root. Currently these
3225 * could come from 2 sources:
3226 * a) a snapshot deletion in progress
3227 * b) a free space cache inode
3228 * We need to distinguish those two, as the snapshot
3229 * orphan must not get deleted.
3230 * find_dead_roots already ran before us, so if this
3231 * is a snapshot deletion, we should find the root
3232 * in the dead_roots list
3233 */
3234 spin_lock(&fs_info->trans_lock);
3235 list_for_each_entry(dead_root, &fs_info->dead_roots,
3236 root_list) {
3237 if (dead_root->root_key.objectid ==
3238 found_key.objectid) {
3239 is_dead_root = 1;
3240 break;
3241 }
3242 }
3243 spin_unlock(&fs_info->trans_lock);
3244 if (is_dead_root) {
3245 /* prevent this orphan from being found again */
3246 key.offset = found_key.objectid - 1;
3247 continue;
3248 }
3249 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003250 /*
3251 * Inode is already gone but the orphan item is still there,
3252 * kill the orphan item.
3253 */
3254 if (ret == -ESTALE) {
3255 trans = btrfs_start_transaction(root, 1);
3256 if (IS_ERR(trans)) {
3257 ret = PTR_ERR(trans);
3258 goto out;
3259 }
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003260 btrfs_debug(root->fs_info, "auto deleting %Lu",
3261 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003262 ret = btrfs_del_orphan_item(trans, root,
3263 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003264 btrfs_end_transaction(trans, root);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003265 if (ret)
3266 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003267 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003268 }
Josef Bacik7b128762008-07-24 12:17:14 -04003269
Josef Bacik7b128762008-07-24 12:17:14 -04003270 /*
3271 * add this inode to the orphan list so btrfs_orphan_del does
3272 * the proper thing when we hit it
3273 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003274 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3275 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003276 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003277
Josef Bacik7b128762008-07-24 12:17:14 -04003278 /* if we have links, this was a truncate, lets do that */
3279 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303280 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003281 iput(inode);
3282 continue;
3283 }
Josef Bacik7b128762008-07-24 12:17:14 -04003284 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003285
3286 /* 1 for the orphan item deletion. */
3287 trans = btrfs_start_transaction(root, 1);
3288 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003289 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003290 ret = PTR_ERR(trans);
3291 goto out;
3292 }
3293 ret = btrfs_orphan_add(trans, inode);
3294 btrfs_end_transaction(trans, root);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003295 if (ret) {
3296 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003297 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003298 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003299
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003300 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003301 if (ret)
3302 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003303 } else {
3304 nr_unlink++;
3305 }
3306
3307 /* this will do delete_inode and everything for us */
3308 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003309 if (ret)
3310 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003311 }
Miao Xie3254c872011-11-10 20:45:05 -05003312 /* release the path since we're done with it */
3313 btrfs_release_path(path);
3314
Yan, Zhengd68fc572010-05-16 10:49:58 -04003315 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3316
3317 if (root->orphan_block_rsv)
3318 btrfs_block_rsv_release(root, root->orphan_block_rsv,
3319 (u64)-1);
3320
Miao Xie27cdeb72014-04-02 19:51:05 +08003321 if (root->orphan_block_rsv ||
3322 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003323 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003324 if (!IS_ERR(trans))
3325 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003326 }
Josef Bacik7b128762008-07-24 12:17:14 -04003327
3328 if (nr_unlink)
David Sterba4884b472013-03-20 13:31:27 +00003329 btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003330 if (nr_truncate)
David Sterba4884b472013-03-20 13:31:27 +00003331 btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003332
3333out:
3334 if (ret)
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003335 btrfs_crit(root->fs_info,
3336 "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003337 btrfs_free_path(path);
3338 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003339}
3340
Chris Masond352ac62008-09-29 15:18:18 -04003341/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003342 * very simple check to peek ahead in the leaf looking for xattrs. If we
3343 * don't find any xattrs, we know there can't be any acls.
3344 *
3345 * slot is the slot the inode is in, objectid is the objectid of the inode
3346 */
3347static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003348 int slot, u64 objectid,
3349 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003350{
3351 u32 nritems = btrfs_header_nritems(leaf);
3352 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003353 static u64 xattr_access = 0;
3354 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003355 int scanned = 0;
3356
Josef Bacikf23b5a52013-06-19 10:16:26 -04003357 if (!xattr_access) {
3358 xattr_access = btrfs_name_hash(POSIX_ACL_XATTR_ACCESS,
3359 strlen(POSIX_ACL_XATTR_ACCESS));
3360 xattr_default = btrfs_name_hash(POSIX_ACL_XATTR_DEFAULT,
3361 strlen(POSIX_ACL_XATTR_DEFAULT));
3362 }
3363
Chris Mason46a53cc2009-04-27 11:47:50 -04003364 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003365 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003366 while (slot < nritems) {
3367 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3368
3369 /* we found a different objectid, there must not be acls */
3370 if (found_key.objectid != objectid)
3371 return 0;
3372
3373 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003374 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003375 if (*first_xattr_slot == -1)
3376 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003377 if (found_key.offset == xattr_access ||
3378 found_key.offset == xattr_default)
3379 return 1;
3380 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003381
3382 /*
3383 * we found a key greater than an xattr key, there can't
3384 * be any acls later on
3385 */
3386 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3387 return 0;
3388
3389 slot++;
3390 scanned++;
3391
3392 /*
3393 * it goes inode, inode backrefs, xattrs, extents,
3394 * so if there are a ton of hard links to an inode there can
3395 * be a lot of backrefs. Don't waste time searching too hard,
3396 * this is just an optimization
3397 */
3398 if (scanned >= 8)
3399 break;
3400 }
3401 /* we hit the end of the leaf before we found an xattr or
3402 * something larger than an xattr. We have to assume the inode
3403 * has acls
3404 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003405 if (*first_xattr_slot == -1)
3406 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003407 return 1;
3408}
3409
3410/*
Chris Masond352ac62008-09-29 15:18:18 -04003411 * read an inode from the btree into the in-memory inode
3412 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003413static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003414{
3415 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003416 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003417 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04003418 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04003419 struct btrfs_root *root = BTRFS_I(inode)->root;
3420 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003421 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003422 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003423 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003424 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003425 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003426 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003427
3428 ret = btrfs_fill_inode(inode, &rdev);
3429 if (!ret)
3430 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003431
3432 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07003433 if (!path)
3434 goto make_bad;
3435
Chris Mason39279cc2007-06-12 06:35:45 -04003436 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003437
Chris Mason39279cc2007-06-12 06:35:45 -04003438 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003439 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04003440 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04003441
Chris Mason5f39d392007-10-15 16:14:19 -04003442 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003443
3444 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003445 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003446
Chris Mason5f39d392007-10-15 16:14:19 -04003447 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3448 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003449 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003450 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003451 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3452 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04003453 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003454
3455 tspec = btrfs_inode_atime(inode_item);
3456 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3457 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3458
3459 tspec = btrfs_inode_mtime(inode_item);
3460 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3461 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3462
3463 tspec = btrfs_inode_ctime(inode_item);
3464 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3465 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3466
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003467 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003468 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003469 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3470
3471 /*
3472 * If we were modified in the current generation and evicted from memory
3473 * and then re-read we need to do a full sync since we don't have any
3474 * idea about which extents were modified before we were evicted from
3475 * cache.
3476 */
3477 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
3478 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3479 &BTRFS_I(inode)->runtime_flags);
3480
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003481 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003482 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003483 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003484 rdev = btrfs_inode_rdev(leaf, inode_item);
3485
Josef Bacikaec74772008-07-24 12:12:38 -04003486 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003487 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003488
3489cache_index:
3490 path->slots[0]++;
3491 if (inode->i_nlink != 1 ||
3492 path->slots[0] >= btrfs_header_nritems(leaf))
3493 goto cache_acl;
3494
3495 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
3496 if (location.objectid != btrfs_ino(inode))
3497 goto cache_acl;
3498
3499 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3500 if (location.type == BTRFS_INODE_REF_KEY) {
3501 struct btrfs_inode_ref *ref;
3502
3503 ref = (struct btrfs_inode_ref *)ptr;
3504 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3505 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3506 struct btrfs_inode_extref *extref;
3507
3508 extref = (struct btrfs_inode_extref *)ptr;
3509 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3510 extref);
3511 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003512cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003513 /*
3514 * try to precache a NULL acl entry for files that don't have
3515 * any xattrs or acls
3516 */
Li Zefan33345d012011-04-20 10:31:50 +08003517 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003518 btrfs_ino(inode), &first_xattr_slot);
3519 if (first_xattr_slot != -1) {
3520 path->slots[0] = first_xattr_slot;
3521 ret = btrfs_load_inode_props(inode, path);
3522 if (ret)
3523 btrfs_err(root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003524 "error loading props for ino %llu (root %llu): %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003525 btrfs_ino(inode),
3526 root->root_key.objectid, ret);
3527 }
3528 btrfs_free_path(path);
3529
Al Viro72c04902009-06-24 16:58:48 -04003530 if (!maybe_acls)
3531 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003532
Chris Mason39279cc2007-06-12 06:35:45 -04003533 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003534 case S_IFREG:
3535 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003536 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05003537 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003538 inode->i_fop = &btrfs_file_operations;
3539 inode->i_op = &btrfs_file_inode_operations;
3540 break;
3541 case S_IFDIR:
3542 inode->i_fop = &btrfs_dir_file_operations;
3543 if (root == root->fs_info->tree_root)
3544 inode->i_op = &btrfs_dir_ro_inode_operations;
3545 else
3546 inode->i_op = &btrfs_dir_inode_operations;
3547 break;
3548 case S_IFLNK:
3549 inode->i_op = &btrfs_symlink_inode_operations;
3550 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04003551 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003552 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003553 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003554 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003555 init_special_inode(inode, inode->i_mode, rdev);
3556 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003557 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003558
3559 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003560 return;
3561
3562make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003563 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003564 make_bad_inode(inode);
3565}
3566
Chris Masond352ac62008-09-29 15:18:18 -04003567/*
3568 * given a leaf and an inode, copy the inode fields into the leaf
3569 */
Chris Masone02119d2008-09-05 16:13:11 -04003570static void fill_inode_item(struct btrfs_trans_handle *trans,
3571 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003572 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003573 struct inode *inode)
3574{
Liu Bo51fab692012-12-27 09:01:21 +00003575 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003576
Liu Bo51fab692012-12-27 09:01:21 +00003577 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003578
Liu Bo51fab692012-12-27 09:01:21 +00003579 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3580 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3581 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3582 &token);
3583 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3584 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003585
Liu Bo51fab692012-12-27 09:01:21 +00003586 btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item),
3587 inode->i_atime.tv_sec, &token);
3588 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item),
3589 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003590
Liu Bo51fab692012-12-27 09:01:21 +00003591 btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item),
3592 inode->i_mtime.tv_sec, &token);
3593 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item),
3594 inode->i_mtime.tv_nsec, &token);
3595
3596 btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item),
3597 inode->i_ctime.tv_sec, &token);
3598 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item),
3599 inode->i_ctime.tv_nsec, &token);
3600
3601 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3602 &token);
3603 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3604 &token);
3605 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3606 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3607 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3608 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3609 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003610}
3611
Chris Masond352ac62008-09-29 15:18:18 -04003612/*
3613 * copy everything in the in-memory inode into the btree.
3614 */
Chris Mason21151332011-11-10 20:39:08 -05003615static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003616 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003617{
3618 struct btrfs_inode_item *inode_item;
3619 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003620 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003621 int ret;
3622
3623 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003624 if (!path)
3625 return -ENOMEM;
3626
Chris Masonb9473432009-03-13 11:00:37 -04003627 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003628 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3629 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003630 if (ret) {
3631 if (ret > 0)
3632 ret = -ENOENT;
3633 goto failed;
3634 }
3635
Chris Mason5f39d392007-10-15 16:14:19 -04003636 leaf = path->nodes[0];
3637 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003638 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003639
Chris Masone02119d2008-09-05 16:13:11 -04003640 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003641 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003642 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003643 ret = 0;
3644failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003645 btrfs_free_path(path);
3646 return ret;
3647}
3648
Chris Masond352ac62008-09-29 15:18:18 -04003649/*
Chris Mason21151332011-11-10 20:39:08 -05003650 * copy everything in the in-memory inode into the btree.
3651 */
3652noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3653 struct btrfs_root *root, struct inode *inode)
3654{
3655 int ret;
3656
3657 /*
3658 * If the inode is a free space inode, we can deadlock during commit
3659 * if we put it into the delayed code.
3660 *
3661 * The data relocation inode should also be directly updated
3662 * without delay
3663 */
Liu Bo83eea1f2012-07-10 05:28:39 -06003664 if (!btrfs_is_free_space_inode(inode)
Chris Mason21151332011-11-10 20:39:08 -05003665 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003666 btrfs_update_root_times(trans, root);
3667
Chris Mason21151332011-11-10 20:39:08 -05003668 ret = btrfs_delayed_update_inode(trans, root, inode);
3669 if (!ret)
3670 btrfs_set_inode_last_trans(trans, inode);
3671 return ret;
3672 }
3673
3674 return btrfs_update_inode_item(trans, root, inode);
3675}
3676
Josef Bacikbe6aef62012-10-22 15:43:12 -04003677noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3678 struct btrfs_root *root,
3679 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003680{
3681 int ret;
3682
3683 ret = btrfs_update_inode(trans, root, inode);
3684 if (ret == -ENOSPC)
3685 return btrfs_update_inode_item(trans, root, inode);
3686 return ret;
3687}
3688
3689/*
Chris Masond352ac62008-09-29 15:18:18 -04003690 * unlink helper that gets used here in inode.c and in the tree logging
3691 * recovery code. It remove a link in a directory with a given name, and
3692 * also drops the back refs in the inode to the directory
3693 */
Al Viro92986792011-03-04 17:14:37 +00003694static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3695 struct btrfs_root *root,
3696 struct inode *dir, struct inode *inode,
3697 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003698{
3699 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003700 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003701 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003702 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003703 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003704 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003705 u64 ino = btrfs_ino(inode);
3706 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003707
3708 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003709 if (!path) {
3710 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003711 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003712 }
3713
Chris Masonb9473432009-03-13 11:00:37 -04003714 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003715 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003716 name, name_len, -1);
3717 if (IS_ERR(di)) {
3718 ret = PTR_ERR(di);
3719 goto err;
3720 }
3721 if (!di) {
3722 ret = -ENOENT;
3723 goto err;
3724 }
Chris Mason5f39d392007-10-15 16:14:19 -04003725 leaf = path->nodes[0];
3726 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003727 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003728 if (ret)
3729 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003730 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003731
Miao Xie67de1172013-12-26 13:07:06 +08003732 /*
3733 * If we don't have dir index, we have to get it by looking up
3734 * the inode ref, since we get the inode ref, remove it directly,
3735 * it is unnecessary to do delayed deletion.
3736 *
3737 * But if we have dir index, needn't search inode ref to get it.
3738 * Since the inode ref is close to the inode item, it is better
3739 * that we delay to delete it, and just do this deletion when
3740 * we update the inode item.
3741 */
3742 if (BTRFS_I(inode)->dir_index) {
3743 ret = btrfs_delayed_delete_inode_ref(inode);
3744 if (!ret) {
3745 index = BTRFS_I(inode)->dir_index;
3746 goto skip_backref;
3747 }
3748 }
3749
Li Zefan33345d012011-04-20 10:31:50 +08003750 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3751 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003752 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003753 btrfs_info(root->fs_info,
3754 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003755 name_len, name, ino, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003756 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003757 goto err;
3758 }
Miao Xie67de1172013-12-26 13:07:06 +08003759skip_backref:
Miao Xie16cdcec2011-04-22 18:12:22 +08003760 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003761 if (ret) {
3762 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003763 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003764 }
Chris Mason39279cc2007-06-12 06:35:45 -04003765
Chris Masone02119d2008-09-05 16:13:11 -04003766 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08003767 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003768 if (ret != 0 && ret != -ENOENT) {
3769 btrfs_abort_transaction(trans, root, ret);
3770 goto err;
3771 }
Chris Masone02119d2008-09-05 16:13:11 -04003772
3773 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
3774 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04003775 if (ret == -ENOENT)
3776 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003777 else if (ret)
3778 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003779err:
3780 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003781 if (ret)
3782 goto out;
3783
3784 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003785 inode_inc_iversion(inode);
3786 inode_inc_iversion(dir);
Chris Masone02119d2008-09-05 16:13:11 -04003787 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Tsutomu Itohb9959292012-06-25 21:25:22 -06003788 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04003789out:
Chris Mason39279cc2007-06-12 06:35:45 -04003790 return ret;
3791}
3792
Al Viro92986792011-03-04 17:14:37 +00003793int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3794 struct btrfs_root *root,
3795 struct inode *dir, struct inode *inode,
3796 const char *name, int name_len)
3797{
3798 int ret;
3799 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3800 if (!ret) {
Zach Brown8b558c52013-10-16 12:10:34 -07003801 drop_nlink(inode);
Al Viro92986792011-03-04 17:14:37 +00003802 ret = btrfs_update_inode(trans, root, inode);
3803 }
3804 return ret;
3805}
Yan, Zhenga22285a2010-05-16 10:48:46 -04003806
3807/*
3808 * helper to start transaction for unlink and rmdir.
3809 *
Josef Bacikd52be812013-05-29 14:54:47 -04003810 * unlink and rmdir are special in btrfs, they do not always free space, so
3811 * if we cannot make our reservations the normal way try and see if there is
3812 * plenty of slack room in the global reserve to migrate, otherwise we cannot
3813 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04003814 */
Josef Bacikd52be812013-05-29 14:54:47 -04003815static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003816{
3817 struct btrfs_trans_handle *trans;
3818 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003819 int ret;
3820
Josef Bacike70bea52011-10-11 14:18:24 -04003821 /*
3822 * 1 for the possible orphan item
3823 * 1 for the dir item
3824 * 1 for the dir index
3825 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04003826 * 1 for the inode
3827 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04003828 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003829 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
3830 return trans;
3831
Josef Bacikd52be812013-05-29 14:54:47 -04003832 if (PTR_ERR(trans) == -ENOSPC) {
3833 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003834
Josef Bacikd52be812013-05-29 14:54:47 -04003835 trans = btrfs_start_transaction(root, 0);
3836 if (IS_ERR(trans))
3837 return trans;
3838 ret = btrfs_cond_migrate_bytes(root->fs_info,
3839 &root->fs_info->trans_block_rsv,
3840 num_bytes, 5);
3841 if (ret) {
3842 btrfs_end_transaction(trans, root);
3843 return ERR_PTR(ret);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003844 }
Josef Bacik5a77d762011-11-01 14:32:23 -04003845 trans->block_rsv = &root->fs_info->trans_block_rsv;
Josef Bacikd52be812013-05-29 14:54:47 -04003846 trans->bytes_reserved = num_bytes;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003847 }
Josef Bacikd52be812013-05-29 14:54:47 -04003848 return trans;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003849}
3850
Chris Mason39279cc2007-06-12 06:35:45 -04003851static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3852{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003853 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003854 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04003855 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003856 int ret;
3857
Josef Bacikd52be812013-05-29 14:54:47 -04003858 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003859 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003860 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003861
Chris Mason12fcfd22009-03-24 10:24:20 -04003862 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
3863
Chris Masone02119d2008-09-05 16:13:11 -04003864 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3865 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003866 if (ret)
3867 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003868
Yan, Zhenga22285a2010-05-16 10:48:46 -04003869 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04003870 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003871 if (ret)
3872 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003873 }
Josef Bacik7b128762008-07-24 12:17:14 -04003874
Tsutomu Itohb5324022011-07-19 07:27:20 +00003875out:
Josef Bacikd52be812013-05-29 14:54:47 -04003876 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003877 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04003878 return ret;
3879}
3880
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003881int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3882 struct btrfs_root *root,
3883 struct inode *dir, u64 objectid,
3884 const char *name, int name_len)
3885{
3886 struct btrfs_path *path;
3887 struct extent_buffer *leaf;
3888 struct btrfs_dir_item *di;
3889 struct btrfs_key key;
3890 u64 index;
3891 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003892 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003893
3894 path = btrfs_alloc_path();
3895 if (!path)
3896 return -ENOMEM;
3897
Li Zefan33345d012011-04-20 10:31:50 +08003898 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003899 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003900 if (IS_ERR_OR_NULL(di)) {
3901 if (!di)
3902 ret = -ENOENT;
3903 else
3904 ret = PTR_ERR(di);
3905 goto out;
3906 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003907
3908 leaf = path->nodes[0];
3909 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3910 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3911 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003912 if (ret) {
3913 btrfs_abort_transaction(trans, root, ret);
3914 goto out;
3915 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003916 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003917
3918 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3919 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003920 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003921 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003922 if (ret != -ENOENT) {
3923 btrfs_abort_transaction(trans, root, ret);
3924 goto out;
3925 }
Li Zefan33345d012011-04-20 10:31:50 +08003926 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003927 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003928 if (IS_ERR_OR_NULL(di)) {
3929 if (!di)
3930 ret = -ENOENT;
3931 else
3932 ret = PTR_ERR(di);
3933 btrfs_abort_transaction(trans, root, ret);
3934 goto out;
3935 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003936
3937 leaf = path->nodes[0];
3938 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003939 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003940 index = key.offset;
3941 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003942 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003943
Miao Xie16cdcec2011-04-22 18:12:22 +08003944 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003945 if (ret) {
3946 btrfs_abort_transaction(trans, root, ret);
3947 goto out;
3948 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003949
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003950 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003951 inode_inc_iversion(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003952 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Josef Bacik5a24e842012-08-08 10:12:59 -06003953 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003954 if (ret)
3955 btrfs_abort_transaction(trans, root, ret);
3956out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04003957 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003958 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003959}
3960
Chris Mason39279cc2007-06-12 06:35:45 -04003961static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3962{
3963 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003964 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003965 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003966 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04003967
David Sterbab3ae2442012-09-13 16:04:34 -06003968 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04003969 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06003970 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
3971 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04003972
Josef Bacikd52be812013-05-29 14:54:47 -04003973 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003974 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003975 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003976
Li Zefan33345d012011-04-20 10:31:50 +08003977 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003978 err = btrfs_unlink_subvol(trans, root, dir,
3979 BTRFS_I(inode)->location.objectid,
3980 dentry->d_name.name,
3981 dentry->d_name.len);
3982 goto out;
3983 }
3984
Josef Bacik7b128762008-07-24 12:17:14 -04003985 err = btrfs_orphan_add(trans, inode);
3986 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003987 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003988
Chris Mason39279cc2007-06-12 06:35:45 -04003989 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04003990 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3991 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05003992 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04003993 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003994out:
Josef Bacikd52be812013-05-29 14:54:47 -04003995 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003996 btrfs_btree_balance_dirty(root);
Chris Mason39544012007-12-12 14:38:19 -05003997
Chris Mason39279cc2007-06-12 06:35:45 -04003998 return err;
3999}
4000
Chris Mason323ac952008-10-01 19:05:46 -04004001/*
Chris Mason39279cc2007-06-12 06:35:45 -04004002 * this can truncate away extent items, csum items and directory items.
4003 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004004 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004005 *
4006 * csum items that cross the new i_size are truncated to the new size
4007 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004008 *
4009 * min_type is the minimum key type to truncate down to. If set to 0, this
4010 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004011 */
Yan, Zheng80825102009-11-12 09:35:36 +00004012int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4013 struct btrfs_root *root,
4014 struct inode *inode,
4015 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004016{
Chris Mason39279cc2007-06-12 06:35:45 -04004017 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004018 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004019 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004020 struct btrfs_key key;
4021 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004022 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004023 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004024 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004025 u64 item_end = 0;
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004026 u64 last_size = (u64)-1;
Yan, Zheng80825102009-11-12 09:35:36 +00004027 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004028 int found_extent;
4029 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004030 int pending_del_nr = 0;
4031 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004032 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004033 int ret;
4034 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08004035 u64 ino = btrfs_ino(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004036
4037 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004038
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004039 path = btrfs_alloc_path();
4040 if (!path)
4041 return -ENOMEM;
4042 path->reada = -1;
4043
Josef Bacik5dc562c2012-08-17 13:14:17 -04004044 /*
4045 * We want to drop from the next block forward in case this new size is
4046 * not block aligned since we will be keeping the last block of the
4047 * extent just the way it is.
4048 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004049 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4050 root == root->fs_info->tree_root)
Qu Wenruofda28322013-02-26 08:10:22 +00004051 btrfs_drop_extent_cache(inode, ALIGN(new_size,
4052 root->sectorsize), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004053
Miao Xie16cdcec2011-04-22 18:12:22 +08004054 /*
4055 * This function is also used to drop the items in the log tree before
4056 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4057 * it is used to drop the loged items. So we shouldn't kill the delayed
4058 * items.
4059 */
4060 if (min_type == 0 && root == BTRFS_I(inode)->root)
4061 btrfs_kill_delayed_inode_items(inode);
4062
Li Zefan33345d012011-04-20 10:31:50 +08004063 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004064 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004065 key.type = (u8)-1;
4066
Chris Mason85e21ba2008-01-29 15:11:36 -05004067search_again:
Chris Masonb9473432009-03-13 11:00:37 -04004068 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004069 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004070 if (ret < 0) {
4071 err = ret;
4072 goto out;
4073 }
Chris Masond3977122009-01-05 21:25:51 -05004074
Chris Mason85e21ba2008-01-29 15:11:36 -05004075 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004076 /* there are no items in the tree for us to truncate, we're
4077 * done
4078 */
Yan, Zheng80825102009-11-12 09:35:36 +00004079 if (path->slots[0] == 0)
4080 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004081 path->slots[0]--;
4082 }
4083
Chris Masond3977122009-01-05 21:25:51 -05004084 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004085 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004086 leaf = path->nodes[0];
4087 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4088 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04004089
Li Zefan33345d012011-04-20 10:31:50 +08004090 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004091 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004092
Chris Mason85e21ba2008-01-29 15:11:36 -05004093 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004094 break;
4095
Chris Mason5f39d392007-10-15 16:14:19 -04004096 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004097 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004098 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004099 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004100 extent_type = btrfs_file_extent_type(leaf, fi);
4101 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004102 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004103 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04004104 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004105 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004106 path->slots[0], fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004107 }
Yan008630c2007-11-07 13:31:09 -05004108 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004109 }
Yan, Zheng80825102009-11-12 09:35:36 +00004110 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004111 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004112 } else {
4113 if (item_end < new_size)
4114 break;
4115 if (found_key.offset >= new_size)
4116 del_item = 1;
4117 else
4118 del_item = 0;
4119 }
Chris Mason39279cc2007-06-12 06:35:45 -04004120 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004121 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004122 if (found_type != BTRFS_EXTENT_DATA_KEY)
4123 goto delete;
4124
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004125 if (del_item)
4126 last_size = found_key.offset;
4127 else
4128 last_size = new_size;
4129
Chris Mason179e29e2007-11-01 11:28:41 -04004130 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004131 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004132 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004133 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004134 u64 orig_num_bytes =
4135 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004136 extent_num_bytes = ALIGN(new_size -
4137 found_key.offset,
4138 root->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004139 btrfs_set_file_extent_num_bytes(leaf, fi,
4140 extent_num_bytes);
4141 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004142 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004143 if (test_bit(BTRFS_ROOT_REF_COWS,
4144 &root->state) &&
4145 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004146 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004147 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004148 } else {
Chris Masondb945352007-10-15 16:15:53 -04004149 extent_num_bytes =
4150 btrfs_file_extent_disk_num_bytes(leaf,
4151 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004152 extent_offset = found_key.offset -
4153 btrfs_file_extent_offset(leaf, fi);
4154
Chris Mason39279cc2007-06-12 06:35:45 -04004155 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004156 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004157 if (extent_start != 0) {
4158 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004159 if (test_bit(BTRFS_ROOT_REF_COWS,
4160 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004161 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004162 }
4163 }
Chris Mason90692182008-02-08 13:49:28 -05004164 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004165 /*
4166 * we can't truncate inline items that have had
4167 * special encodings
4168 */
4169 if (!del_item &&
4170 btrfs_file_extent_compression(leaf, fi) == 0 &&
4171 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4172 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004173 u32 size = new_size - found_key.offset;
4174
Miao Xie27cdeb72014-04-02 19:51:05 +08004175 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004176 inode_sub_bytes(inode, item_end + 1 -
4177 new_size);
Chris Mason514ac8a2014-01-03 21:07:00 -08004178
4179 /*
4180 * update the ram bytes to properly reflect
4181 * the new size of our item
4182 */
4183 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
Chris Masone02119d2008-09-05 16:13:11 -04004184 size =
4185 btrfs_file_extent_calc_inline_size(size);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004186 btrfs_truncate_item(root, path, size, 1);
Miao Xie27cdeb72014-04-02 19:51:05 +08004187 } else if (test_bit(BTRFS_ROOT_REF_COWS,
4188 &root->state)) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004189 inode_sub_bytes(inode, item_end + 1 -
4190 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05004191 }
Chris Mason39279cc2007-06-12 06:35:45 -04004192 }
Chris Mason179e29e2007-11-01 11:28:41 -04004193delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004194 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004195 if (!pending_del_nr) {
4196 /* no pending yet, add ourselves */
4197 pending_del_slot = path->slots[0];
4198 pending_del_nr = 1;
4199 } else if (pending_del_nr &&
4200 path->slots[0] + 1 == pending_del_slot) {
4201 /* hop on the pending chunk */
4202 pending_del_nr++;
4203 pending_del_slot = path->slots[0];
4204 } else {
Chris Masond3977122009-01-05 21:25:51 -05004205 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004206 }
Chris Mason39279cc2007-06-12 06:35:45 -04004207 } else {
4208 break;
4209 }
Miao Xie27cdeb72014-04-02 19:51:05 +08004210 if (found_extent &&
4211 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4212 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004213 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004214 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004215 extent_num_bytes, 0,
4216 btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02004217 ino, extent_offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004218 BUG_ON(ret);
4219 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004220
Yan, Zheng80825102009-11-12 09:35:36 +00004221 if (found_type == BTRFS_INODE_ITEM_KEY)
4222 break;
4223
4224 if (path->slots[0] == 0 ||
4225 path->slots[0] != pending_del_slot) {
Yan, Zheng80825102009-11-12 09:35:36 +00004226 if (pending_del_nr) {
4227 ret = btrfs_del_items(trans, root, path,
4228 pending_del_slot,
4229 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004230 if (ret) {
4231 btrfs_abort_transaction(trans,
4232 root, ret);
4233 goto error;
4234 }
Yan, Zheng80825102009-11-12 09:35:36 +00004235 pending_del_nr = 0;
4236 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004237 btrfs_release_path(path);
Chris Mason85e21ba2008-01-29 15:11:36 -05004238 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004239 } else {
4240 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004241 }
Chris Mason39279cc2007-06-12 06:35:45 -04004242 }
Yan, Zheng80825102009-11-12 09:35:36 +00004243out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004244 if (pending_del_nr) {
4245 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4246 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004247 if (ret)
4248 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004249 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004250error:
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004251 if (last_size != (u64)-1)
4252 btrfs_ordered_update_i_size(inode, last_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004253 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00004254 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004255}
4256
Chris Masona52d9a82007-08-27 16:49:44 -04004257/*
Josef Bacik2aaa6652012-08-29 14:27:18 -04004258 * btrfs_truncate_page - read, zero a chunk and write a page
4259 * @inode - inode that we're zeroing
4260 * @from - the offset to start zeroing
4261 * @len - the length to zero, 0 to zero the entire range respective to the
4262 * offset
4263 * @front - zero up to the offset instead of from the offset on
4264 *
4265 * This will find the page for the "from" offset and cow the page and zero the
4266 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004267 */
Josef Bacik2aaa6652012-08-29 14:27:18 -04004268int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
4269 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004270{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004271 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04004272 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004273 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4274 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004275 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004276 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04004277 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04004278 pgoff_t index = from >> PAGE_CACHE_SHIFT;
4279 unsigned offset = from & (PAGE_CACHE_SIZE-1);
4280 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004281 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004282 int ret = 0;
4283 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004284 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004285
Josef Bacik2aaa6652012-08-29 14:27:18 -04004286 if ((offset & (blocksize - 1)) == 0 &&
4287 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004288 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004289 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004290 if (ret)
4291 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004292
Chris Mason211c17f2008-05-15 09:13:45 -04004293again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004294 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004295 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004296 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Miao Xieac6a2b32012-12-05 10:56:13 +00004297 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004298 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004299 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004300
4301 page_start = page_offset(page);
4302 page_end = page_start + PAGE_CACHE_SIZE - 1;
4303
Chris Masona52d9a82007-08-27 16:49:44 -04004304 if (!PageUptodate(page)) {
4305 ret = btrfs_readpage(NULL, page);
4306 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004307 if (page->mapping != mapping) {
4308 unlock_page(page);
4309 page_cache_release(page);
4310 goto again;
4311 }
Chris Masona52d9a82007-08-27 16:49:44 -04004312 if (!PageUptodate(page)) {
4313 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004314 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004315 }
4316 }
Chris Mason211c17f2008-05-15 09:13:45 -04004317 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004318
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004319 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004320 set_page_extent_mapped(page);
4321
4322 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4323 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004324 unlock_extent_cached(io_tree, page_start, page_end,
4325 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004326 unlock_page(page);
4327 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004328 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004329 btrfs_put_ordered_extent(ordered);
4330 goto again;
4331 }
4332
Josef Bacik2ac55d42010-02-03 19:33:23 +00004333 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004334 EXTENT_DIRTY | EXTENT_DELALLOC |
4335 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004336 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004337
Josef Bacik2ac55d42010-02-03 19:33:23 +00004338 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
4339 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004340 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004341 unlock_extent_cached(io_tree, page_start, page_end,
4342 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004343 goto out_unlock;
4344 }
4345
Chris Masone6dcd2d2008-07-17 12:53:50 -04004346 if (offset != PAGE_CACHE_SIZE) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004347 if (!len)
4348 len = PAGE_CACHE_SIZE - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004349 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004350 if (front)
4351 memset(kaddr, 0, offset);
4352 else
4353 memset(kaddr + offset, 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004354 flush_dcache_page(page);
4355 kunmap(page);
4356 }
Chris Mason247e7432008-07-17 12:53:51 -04004357 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004358 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004359 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
4360 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004361
Chris Mason89642222008-07-24 09:41:53 -04004362out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004363 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004364 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04004365 unlock_page(page);
4366 page_cache_release(page);
4367out:
4368 return ret;
4369}
4370
Josef Bacik16e75492013-10-22 12:18:51 -04004371static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4372 u64 offset, u64 len)
4373{
4374 struct btrfs_trans_handle *trans;
4375 int ret;
4376
4377 /*
4378 * Still need to make sure the inode looks like it's been updated so
4379 * that any holes get logged if we fsync.
4380 */
4381 if (btrfs_fs_incompat(root->fs_info, NO_HOLES)) {
4382 BTRFS_I(inode)->last_trans = root->fs_info->generation;
4383 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4384 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4385 return 0;
4386 }
4387
4388 /*
4389 * 1 - for the one we're dropping
4390 * 1 - for the one we're adding
4391 * 1 - for updating the inode.
4392 */
4393 trans = btrfs_start_transaction(root, 3);
4394 if (IS_ERR(trans))
4395 return PTR_ERR(trans);
4396
4397 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4398 if (ret) {
4399 btrfs_abort_transaction(trans, root, ret);
4400 btrfs_end_transaction(trans, root);
4401 return ret;
4402 }
4403
4404 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset,
4405 0, 0, len, 0, len, 0, 0, 0);
4406 if (ret)
4407 btrfs_abort_transaction(trans, root, ret);
4408 else
4409 btrfs_update_inode(trans, root, inode);
4410 btrfs_end_transaction(trans, root);
4411 return ret;
4412}
4413
Josef Bacik695a0d02011-03-04 15:46:53 -05004414/*
4415 * This function puts in dummy file extents for the area we're creating a hole
4416 * for. So if we are truncating this file to a larger size we need to insert
4417 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4418 * the range between oldsize and size
4419 */
Josef Bacika41ad392011-01-31 15:30:16 -05004420int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004421{
Yan Zheng9036c102008-10-30 14:19:41 -04004422 struct btrfs_root *root = BTRFS_I(inode)->root;
4423 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004424 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004425 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004426 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Qu Wenruofda28322013-02-26 08:10:22 +00004427 u64 hole_start = ALIGN(oldsize, root->sectorsize);
4428 u64 block_end = ALIGN(size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004429 u64 last_byte;
4430 u64 cur_offset;
4431 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004432 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004433
Josef Bacika71754f2013-06-17 17:14:39 -04004434 /*
4435 * If our size started in the middle of a page we need to zero out the
4436 * rest of the page before we expand the i_size, otherwise we could
4437 * expose stale data.
4438 */
4439 err = btrfs_truncate_page(inode, oldsize, 0, 0);
4440 if (err)
4441 return err;
4442
Yan Zheng9036c102008-10-30 14:19:41 -04004443 if (size <= hole_start)
4444 return 0;
4445
Yan Zheng9036c102008-10-30 14:19:41 -04004446 while (1) {
4447 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004448
Josef Bacik2ac55d42010-02-03 19:33:23 +00004449 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004450 &cached_state);
Miao Xiefa7c1492013-09-26 13:15:27 +08004451 ordered = btrfs_lookup_ordered_range(inode, hole_start,
4452 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004453 if (!ordered)
4454 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004455 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4456 &cached_state, GFP_NOFS);
Miao Xiefa7c1492013-09-26 13:15:27 +08004457 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004458 btrfs_put_ordered_extent(ordered);
4459 }
4460
Yan Zheng9036c102008-10-30 14:19:41 -04004461 cur_offset = hole_start;
4462 while (1) {
4463 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4464 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004465 if (IS_ERR(em)) {
4466 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004467 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004468 break;
4469 }
Yan Zheng9036c102008-10-30 14:19:41 -04004470 last_byte = min(extent_map_end(em), block_end);
Qu Wenruofda28322013-02-26 08:10:22 +00004471 last_byte = ALIGN(last_byte , root->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004472 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004473 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004474 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004475
Josef Bacik16e75492013-10-22 12:18:51 -04004476 err = maybe_insert_hole(root, inode, cur_offset,
4477 hole_size);
4478 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004479 break;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004480 btrfs_drop_extent_cache(inode, cur_offset,
4481 cur_offset + hole_size - 1, 0);
4482 hole_em = alloc_extent_map();
4483 if (!hole_em) {
4484 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4485 &BTRFS_I(inode)->runtime_flags);
4486 goto next;
4487 }
4488 hole_em->start = cur_offset;
4489 hole_em->len = hole_size;
4490 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004491
Josef Bacik5dc562c2012-08-17 13:14:17 -04004492 hole_em->block_start = EXTENT_MAP_HOLE;
4493 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004494 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004495 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004496 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
4497 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Josef Bacik16e75492013-10-22 12:18:51 -04004498 hole_em->generation = root->fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004499
4500 while (1) {
4501 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004502 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004503 write_unlock(&em_tree->lock);
4504 if (err != -EEXIST)
4505 break;
4506 btrfs_drop_extent_cache(inode, cur_offset,
4507 cur_offset +
4508 hole_size - 1, 0);
4509 }
4510 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04004511 }
Josef Bacik16e75492013-10-22 12:18:51 -04004512next:
Yan Zheng9036c102008-10-30 14:19:41 -04004513 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004514 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004515 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004516 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004517 break;
4518 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004519 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004520 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4521 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004522 return err;
4523}
4524
Eric Sandeen3972f262013-01-12 02:57:22 +00004525static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004526{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004527 struct btrfs_root *root = BTRFS_I(inode)->root;
4528 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004529 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004530 loff_t newsize = attr->ia_size;
4531 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004532 int ret;
4533
Eric Sandeen3972f262013-01-12 02:57:22 +00004534 /*
4535 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4536 * special case where we need to update the times despite not having
4537 * these flags set. For all other operations the VFS set these flags
4538 * explicitly if it wants a timestamp update.
4539 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004540 if (newsize != oldsize) {
4541 inode_inc_iversion(inode);
4542 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
4543 inode->i_ctime = inode->i_mtime =
4544 current_fs_time(inode->i_sb);
4545 }
Eric Sandeen3972f262013-01-12 02:57:22 +00004546
Josef Bacika41ad392011-01-31 15:30:16 -05004547 if (newsize > oldsize) {
Kirill A. Shutemov7caef262013-09-12 15:13:56 -07004548 truncate_pagecache(inode, newsize);
Josef Bacika41ad392011-01-31 15:30:16 -05004549 ret = btrfs_cont_expand(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004550 if (ret)
Yan, Zheng80825102009-11-12 09:35:36 +00004551 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004552
Miao Xief4a2f4c2011-12-14 20:12:01 -05004553 trans = btrfs_start_transaction(root, 1);
4554 if (IS_ERR(trans))
4555 return PTR_ERR(trans);
4556
4557 i_size_write(inode, newsize);
4558 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
4559 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004560 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05004561 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004562
Josef Bacika41ad392011-01-31 15:30:16 -05004563 /*
4564 * We're truncating a file that used to have good data down to
4565 * zero. Make sure it gets into the ordered flush list so that
4566 * any new writes get down to disk quickly.
4567 */
4568 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004569 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4570 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004571
Josef Bacikf3fe8202013-01-07 17:03:21 -05004572 /*
4573 * 1 for the orphan item we're going to add
4574 * 1 for the orphan item deletion.
4575 */
4576 trans = btrfs_start_transaction(root, 2);
4577 if (IS_ERR(trans))
4578 return PTR_ERR(trans);
4579
4580 /*
4581 * We need to do this in case we fail at _any_ point during the
4582 * actual truncate. Once we do the truncate_setsize we could
4583 * invalidate pages which forces any outstanding ordered io to
4584 * be instantly completed which will give us extents that need
4585 * to be truncated. If we fail to get an orphan inode down we
4586 * could have left over extents that were never meant to live,
4587 * so we need to garuntee from this point on that everything
4588 * will be consistent.
4589 */
4590 ret = btrfs_orphan_add(trans, inode);
4591 btrfs_end_transaction(trans, root);
4592 if (ret)
4593 return ret;
4594
Josef Bacika41ad392011-01-31 15:30:16 -05004595 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4596 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004597
4598 /* Disable nonlocked read DIO to avoid the end less truncate */
4599 btrfs_inode_block_unlocked_dio(inode);
4600 inode_dio_wait(inode);
4601 btrfs_inode_resume_unlocked_dio(inode);
4602
Josef Bacika41ad392011-01-31 15:30:16 -05004603 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004604 if (ret && inode->i_nlink) {
4605 int err;
4606
4607 /*
4608 * failed to truncate, disk_i_size is only adjusted down
4609 * as we remove extents, so it should represent the true
4610 * size of the inode, so reset the in memory size and
4611 * delete our orphan entry.
4612 */
4613 trans = btrfs_join_transaction(root);
4614 if (IS_ERR(trans)) {
4615 btrfs_orphan_del(NULL, inode);
4616 return ret;
4617 }
4618 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
4619 err = btrfs_orphan_del(trans, inode);
4620 if (err)
4621 btrfs_abort_transaction(trans, root, err);
4622 btrfs_end_transaction(trans, root);
4623 }
Yan, Zheng80825102009-11-12 09:35:36 +00004624 }
4625
Josef Bacika41ad392011-01-31 15:30:16 -05004626 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004627}
4628
Chris Mason39279cc2007-06-12 06:35:45 -04004629static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
4630{
4631 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08004632 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004633 int err;
4634
Li Zefanb83cc962010-12-20 16:04:08 +08004635 if (btrfs_root_readonly(root))
4636 return -EROFS;
4637
Chris Mason39279cc2007-06-12 06:35:45 -04004638 err = inode_change_ok(inode, attr);
4639 if (err)
4640 return err;
4641
Chris Mason5a3f23d2009-03-31 13:27:11 -04004642 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00004643 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00004644 if (err)
4645 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004646 }
Yan Zheng9036c102008-10-30 14:19:41 -04004647
Christoph Hellwig10257742010-06-04 11:30:02 +02004648 if (attr->ia_valid) {
4649 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004650 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004651 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04004652
Josef Bacik22c44fe2011-11-30 10:45:38 -05004653 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08004654 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02004655 }
4656
Chris Mason39279cc2007-06-12 06:35:45 -04004657 return err;
4658}
Chris Mason61295eb2008-01-14 16:24:38 -05004659
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004660/*
4661 * While truncating the inode pages during eviction, we get the VFS calling
4662 * btrfs_invalidatepage() against each page of the inode. This is slow because
4663 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
4664 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
4665 * extent_state structures over and over, wasting lots of time.
4666 *
4667 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
4668 * those expensive operations on a per page basis and do only the ordered io
4669 * finishing, while we release here the extent_map and extent_state structures,
4670 * without the excessive merging and splitting.
4671 */
4672static void evict_inode_truncate_pages(struct inode *inode)
4673{
4674 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4675 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
4676 struct rb_node *node;
4677
4678 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07004679 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004680
4681 write_lock(&map_tree->lock);
4682 while (!RB_EMPTY_ROOT(&map_tree->map)) {
4683 struct extent_map *em;
4684
4685 node = rb_first(&map_tree->map);
4686 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08004687 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
4688 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004689 remove_extent_mapping(map_tree, em);
4690 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01004691 if (need_resched()) {
4692 write_unlock(&map_tree->lock);
4693 cond_resched();
4694 write_lock(&map_tree->lock);
4695 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004696 }
4697 write_unlock(&map_tree->lock);
4698
4699 spin_lock(&io_tree->lock);
4700 while (!RB_EMPTY_ROOT(&io_tree->state)) {
4701 struct extent_state *state;
4702 struct extent_state *cached_state = NULL;
4703
4704 node = rb_first(&io_tree->state);
4705 state = rb_entry(node, struct extent_state, rb_node);
4706 atomic_inc(&state->refs);
4707 spin_unlock(&io_tree->lock);
4708
4709 lock_extent_bits(io_tree, state->start, state->end,
4710 0, &cached_state);
4711 clear_extent_bit(io_tree, state->start, state->end,
4712 EXTENT_LOCKED | EXTENT_DIRTY |
4713 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
4714 EXTENT_DEFRAG, 1, 1,
4715 &cached_state, GFP_NOFS);
4716 free_extent_state(state);
4717
Filipe Manana7064dd52014-08-08 02:47:05 +01004718 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004719 spin_lock(&io_tree->lock);
4720 }
4721 spin_unlock(&io_tree->lock);
4722}
4723
Al Virobd555972010-06-07 11:35:40 -04004724void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004725{
4726 struct btrfs_trans_handle *trans;
4727 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04004728 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik07127182011-08-19 10:29:59 -04004729 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04004730 int ret;
4731
liubo1abe9b82011-03-24 11:18:59 +00004732 trace_btrfs_inode_evict(inode);
4733
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004734 evict_inode_truncate_pages(inode);
4735
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02004736 if (inode->i_nlink &&
4737 ((btrfs_root_refs(&root->root_item) != 0 &&
4738 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
4739 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04004740 goto no_delete;
4741
Chris Mason39279cc2007-06-12 06:35:45 -04004742 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04004743 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004744 goto no_delete;
4745 }
Al Virobd555972010-06-07 11:35:40 -04004746 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04004747 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04004748
Yan, Zhengc71bf092009-11-12 09:34:40 +00004749 if (root->fs_info->log_root_recovering) {
Liu Bo6bf02312012-06-25 21:59:09 -06004750 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04004751 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00004752 goto no_delete;
4753 }
4754
Yan, Zheng76dda932009-09-21 16:00:26 -04004755 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02004756 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
4757 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04004758 goto no_delete;
4759 }
4760
Miao Xie0e8c36a2012-12-19 06:59:51 +00004761 ret = btrfs_commit_inode_delayed_inode(inode);
4762 if (ret) {
4763 btrfs_orphan_del(NULL, inode);
4764 goto no_delete;
4765 }
4766
Miao Xie66d8f3d2012-09-06 04:02:28 -06004767 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04004768 if (!rsv) {
4769 btrfs_orphan_del(NULL, inode);
4770 goto no_delete;
4771 }
Josef Bacik4a338542011-08-29 11:01:31 -04004772 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04004773 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04004774 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04004775
Chris Masondbe674a2008-07-17 12:54:05 -04004776 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04004777
Josef Bacik4289a662011-08-05 13:22:24 -04004778 /*
Miao Xie8407aa42012-09-07 01:43:32 -06004779 * This is a bit simpler than btrfs_truncate since we've already
4780 * reserved our space for our orphan item in the unlink, so we just
4781 * need to reserve some slack space in case we add bytes and update
4782 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04004783 */
Yan, Zheng80825102009-11-12 09:35:36 +00004784 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00004785 ret = btrfs_block_rsv_refill(root, rsv, min_size,
4786 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00004787
Josef Bacik726c35f2011-09-26 15:46:06 -04004788 /*
4789 * Try and steal from the global reserve since we will
4790 * likely not use this space anyway, we want to try as
4791 * hard as possible to get this to work.
4792 */
4793 if (ret)
4794 ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
4795
Yan, Zhengd68fc572010-05-16 10:49:58 -04004796 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004797 btrfs_warn(root->fs_info,
4798 "Could not get space for a delete, will truncate on mount %d",
4799 ret);
Josef Bacik4289a662011-08-05 13:22:24 -04004800 btrfs_orphan_del(NULL, inode);
4801 btrfs_free_block_rsv(root, rsv);
4802 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04004803 }
4804
Miao Xie0e8c36a2012-12-19 06:59:51 +00004805 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04004806 if (IS_ERR(trans)) {
4807 btrfs_orphan_del(NULL, inode);
4808 btrfs_free_block_rsv(root, rsv);
4809 goto no_delete;
4810 }
4811
4812 trans->block_rsv = rsv;
4813
Yan, Zhengd68fc572010-05-16 10:49:58 -04004814 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04004815 if (ret != -ENOSPC)
Yan, Zheng80825102009-11-12 09:35:36 +00004816 break;
4817
Miao Xie8407aa42012-09-07 01:43:32 -06004818 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004819 btrfs_end_transaction(trans, root);
4820 trans = NULL;
Liu Bob53d3f52012-11-14 14:34:34 +00004821 btrfs_btree_balance_dirty(root);
Josef Bacik7b128762008-07-24 12:17:14 -04004822 }
4823
Josef Bacik4289a662011-08-05 13:22:24 -04004824 btrfs_free_block_rsv(root, rsv);
4825
Josef Bacik4ef31a42013-08-13 14:10:08 -04004826 /*
4827 * Errors here aren't a big deal, it just means we leave orphan items
4828 * in the tree. They will be cleaned up on the next mount.
4829 */
Yan, Zheng80825102009-11-12 09:35:36 +00004830 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04004831 trans->block_rsv = root->orphan_block_rsv;
Josef Bacik4ef31a42013-08-13 14:10:08 -04004832 btrfs_orphan_del(trans, inode);
4833 } else {
4834 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004835 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004836
Josef Bacik4289a662011-08-05 13:22:24 -04004837 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08004838 if (!(root == root->fs_info->tree_root ||
4839 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08004840 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08004841
Chris Mason54aa1f42007-06-22 14:16:25 -04004842 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004843 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04004844no_delete:
Miao Xie89042e52013-05-15 07:48:15 +00004845 btrfs_remove_delayed_node(inode);
Jan Karadbd57682012-05-03 14:48:02 +02004846 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004847 return;
Chris Mason39279cc2007-06-12 06:35:45 -04004848}
4849
4850/*
4851 * this returns the key found in the dir entry in the location pointer.
4852 * If no dir entries were found, location->objectid is 0.
4853 */
4854static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
4855 struct btrfs_key *location)
4856{
4857 const char *name = dentry->d_name.name;
4858 int namelen = dentry->d_name.len;
4859 struct btrfs_dir_item *di;
4860 struct btrfs_path *path;
4861 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04004862 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004863
4864 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07004865 if (!path)
4866 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05004867
Li Zefan33345d012011-04-20 10:31:50 +08004868 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04004869 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04004870 if (IS_ERR(di))
4871 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05004872
David Sterbac7040052011-04-19 18:00:01 +02004873 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05004874 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05004875
Chris Mason5f39d392007-10-15 16:14:19 -04004876 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04004877out:
Chris Mason39279cc2007-06-12 06:35:45 -04004878 btrfs_free_path(path);
4879 return ret;
Chris Mason39544012007-12-12 14:38:19 -05004880out_err:
4881 location->objectid = 0;
4882 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04004883}
4884
4885/*
4886 * when we hit a tree root in a directory, the btrfs part of the inode
4887 * needs to be changed to reflect the root directory of the tree root. This
4888 * is kind of like crossing a mount point.
4889 */
4890static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004891 struct inode *dir,
4892 struct dentry *dentry,
4893 struct btrfs_key *location,
4894 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04004895{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004896 struct btrfs_path *path;
4897 struct btrfs_root *new_root;
4898 struct btrfs_root_ref *ref;
4899 struct extent_buffer *leaf;
4900 int ret;
4901 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004902
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004903 path = btrfs_alloc_path();
4904 if (!path) {
4905 err = -ENOMEM;
4906 goto out;
4907 }
Chris Mason39279cc2007-06-12 06:35:45 -04004908
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004909 err = -ENOENT;
Kelley Nielsen75ac2dd2013-11-04 19:35:58 -08004910 ret = btrfs_find_item(root->fs_info->tree_root, path,
4911 BTRFS_I(dir)->root->root_key.objectid,
4912 location->objectid, BTRFS_ROOT_REF_KEY, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004913 if (ret) {
4914 if (ret < 0)
4915 err = ret;
4916 goto out;
4917 }
Chris Mason39279cc2007-06-12 06:35:45 -04004918
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004919 leaf = path->nodes[0];
4920 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08004921 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004922 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
4923 goto out;
4924
4925 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
4926 (unsigned long)(ref + 1),
4927 dentry->d_name.len);
4928 if (ret)
4929 goto out;
4930
David Sterbab3b4aa72011-04-21 01:20:15 +02004931 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004932
4933 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
4934 if (IS_ERR(new_root)) {
4935 err = PTR_ERR(new_root);
4936 goto out;
4937 }
4938
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004939 *sub_root = new_root;
4940 location->objectid = btrfs_root_dirid(&new_root->root_item);
4941 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04004942 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004943 err = 0;
4944out:
4945 btrfs_free_path(path);
4946 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004947}
4948
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004949static void inode_tree_add(struct inode *inode)
4950{
4951 struct btrfs_root *root = BTRFS_I(inode)->root;
4952 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004953 struct rb_node **p;
4954 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01004955 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Li Zefan33345d012011-04-20 10:31:50 +08004956 u64 ino = btrfs_ino(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004957
Al Viro1d3382cb2010-10-23 15:19:20 -04004958 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004959 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00004960 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004961 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00004962 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004963 while (*p) {
4964 parent = *p;
4965 entry = rb_entry(parent, struct btrfs_inode, rb_node);
4966
Li Zefan33345d012011-04-20 10:31:50 +08004967 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004968 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004969 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004970 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004971 else {
4972 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04004973 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01004974 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004975 RB_CLEAR_NODE(parent);
4976 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01004977 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004978 }
4979 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01004980 rb_link_node(new, parent, p);
4981 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004982 spin_unlock(&root->inode_lock);
4983}
4984
4985static void inode_tree_del(struct inode *inode)
4986{
4987 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04004988 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004989
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004990 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004991 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004992 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004993 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04004994 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004995 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004996 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04004997
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02004998 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004999 synchronize_srcu(&root->fs_info->subvol_srcu);
5000 spin_lock(&root->inode_lock);
5001 empty = RB_EMPTY_ROOT(&root->inode_tree);
5002 spin_unlock(&root->inode_lock);
5003 if (empty)
5004 btrfs_add_dead_root(root);
5005 }
5006}
5007
Jeff Mahoney143bede2012-03-01 14:56:26 +01005008void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005009{
5010 struct rb_node *node;
5011 struct rb_node *prev;
5012 struct btrfs_inode *entry;
5013 struct inode *inode;
5014 u64 objectid = 0;
5015
Liu Bo7813b3d2014-02-10 17:37:25 +08005016 if (!test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
5017 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04005018
5019 spin_lock(&root->inode_lock);
5020again:
5021 node = root->inode_tree.rb_node;
5022 prev = NULL;
5023 while (node) {
5024 prev = node;
5025 entry = rb_entry(node, struct btrfs_inode, rb_node);
5026
Li Zefan33345d012011-04-20 10:31:50 +08005027 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005028 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08005029 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005030 node = node->rb_right;
5031 else
5032 break;
5033 }
5034 if (!node) {
5035 while (prev) {
5036 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08005037 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005038 node = prev;
5039 break;
5040 }
5041 prev = rb_next(prev);
5042 }
5043 }
5044 while (node) {
5045 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08005046 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04005047 inode = igrab(&entry->vfs_inode);
5048 if (inode) {
5049 spin_unlock(&root->inode_lock);
5050 if (atomic_read(&inode->i_count) > 1)
5051 d_prune_aliases(inode);
5052 /*
Al Viro45321ac2010-06-07 13:43:19 -04005053 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04005054 * the inode cache when its usage count
5055 * hits zero.
5056 */
5057 iput(inode);
5058 cond_resched();
5059 spin_lock(&root->inode_lock);
5060 goto again;
5061 }
5062
5063 if (cond_resched_lock(&root->inode_lock))
5064 goto again;
5065
5066 node = rb_next(node);
5067 }
5068 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005069}
5070
Chris Masone02119d2008-09-05 16:13:11 -04005071static int btrfs_init_locked_inode(struct inode *inode, void *p)
5072{
5073 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005074 inode->i_ino = args->location->objectid;
5075 memcpy(&BTRFS_I(inode)->location, args->location,
5076 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005077 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005078 return 0;
5079}
5080
5081static int btrfs_find_actor(struct inode *inode, void *opaque)
5082{
5083 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005084 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005085 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005086}
5087
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005088static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005089 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005090 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005091{
5092 struct inode *inode;
5093 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005094 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005095
Chris Mason90d3e592014-01-09 17:28:00 -08005096 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005097 args.root = root;
5098
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005099 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005100 btrfs_init_locked_inode,
5101 (void *)&args);
5102 return inode;
5103}
5104
Balaji Rao1a54ef82008-07-21 02:01:04 +05305105/* Get an inode object given its location and corresponding root.
5106 * Returns in *is_new if the inode was read from disk
5107 */
5108struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005109 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305110{
5111 struct inode *inode;
5112
Chris Mason90d3e592014-01-09 17:28:00 -08005113 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305114 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005115 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305116
5117 if (inode->i_state & I_NEW) {
Balaji Rao1a54ef82008-07-21 02:01:04 +05305118 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005119 if (!is_bad_inode(inode)) {
5120 inode_tree_add(inode);
5121 unlock_new_inode(inode);
5122 if (new)
5123 *new = 1;
5124 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005125 unlock_new_inode(inode);
5126 iput(inode);
5127 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005128 }
5129 }
5130
Balaji Rao1a54ef82008-07-21 02:01:04 +05305131 return inode;
5132}
5133
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005134static struct inode *new_simple_dir(struct super_block *s,
5135 struct btrfs_key *key,
5136 struct btrfs_root *root)
5137{
5138 struct inode *inode = new_inode(s);
5139
5140 if (!inode)
5141 return ERR_PTR(-ENOMEM);
5142
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005143 BTRFS_I(inode)->root = root;
5144 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005145 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005146
5147 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005148 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005149 inode->i_fop = &simple_dir_operations;
5150 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
5151 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
5152
5153 return inode;
5154}
5155
Chris Mason3de45862008-11-17 21:02:50 -05005156struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005157{
Chris Masond3977122009-01-05 21:25:51 -05005158 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005159 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005160 struct btrfs_root *sub_root = root;
5161 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005162 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005163 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005164
5165 if (dentry->d_name.len > BTRFS_NAME_LEN)
5166 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005167
Jeff Layton39e3c952012-11-28 11:30:53 -05005168 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005169 if (ret < 0)
5170 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005171
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005172 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005173 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005174
5175 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005176 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005177 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005178 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005179
5180 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5181
Yan, Zheng76dda932009-09-21 16:00:26 -04005182 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005183 ret = fixup_tree_root_location(root, dir, dentry,
5184 &location, &sub_root);
5185 if (ret < 0) {
5186 if (ret != -ENOENT)
5187 inode = ERR_PTR(ret);
5188 else
5189 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5190 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005191 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005192 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005193 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
5194
Julia Lawall34d19ba2011-01-24 19:55:19 +00005195 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00005196 down_read(&root->fs_info->cleanup_work_sem);
5197 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005198 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005199 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005200 if (ret) {
5201 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005202 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005203 }
Filipe Manana9c3b3062014-07-31 14:41:07 +01005204 /*
5205 * If orphan cleanup did remove any orphans, it means the tree
5206 * was modified and therefore the commit root is not the same as
5207 * the current root anymore. This is a problem, because send
5208 * uses the commit root and therefore can see inode items that
5209 * don't exist in the current root anymore, and for example make
5210 * calls to btrfs_iget, which will do tree lookups based on the
5211 * current root and not on the commit root. Those lookups will
5212 * fail, returning a -ESTALE error, and making send fail with
5213 * that error. So make sure a send does not see any orphans we
5214 * have just removed, and that it will see the same inodes
5215 * regardless of whether a transaction commit happened before
5216 * it started (meaning that the commit root will be the same as
5217 * the current root) or not.
5218 */
5219 if (sub_root->node != sub_root->commit_root) {
5220 u64 sub_flags = btrfs_root_flags(&sub_root->root_item);
5221
5222 if (sub_flags & BTRFS_ROOT_SUBVOL_RDONLY) {
5223 struct extent_buffer *eb;
5224
5225 /*
5226 * Assert we can't have races between dentry
5227 * lookup called through the snapshot creation
5228 * ioctl and the VFS.
5229 */
5230 ASSERT(mutex_is_locked(&dir->i_mutex));
5231
5232 down_write(&root->fs_info->commit_root_sem);
5233 eb = sub_root->commit_root;
5234 sub_root->commit_root =
5235 btrfs_root_node(sub_root);
5236 up_write(&root->fs_info->commit_root_sem);
5237 free_extent_buffer(eb);
5238 }
5239 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005240 }
5241
Chris Mason3de45862008-11-17 21:02:50 -05005242 return inode;
5243}
5244
Nick Pigginfe15ce42011-01-07 17:49:23 +11005245static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005246{
5247 struct btrfs_root *root;
Li Zefan848cce02012-02-21 17:04:28 +08005248 struct inode *inode = dentry->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005249
Li Zefan848cce02012-02-21 17:04:28 +08005250 if (!inode && !IS_ROOT(dentry))
5251 inode = dentry->d_parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005252
Li Zefan848cce02012-02-21 17:04:28 +08005253 if (inode) {
5254 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005255 if (btrfs_root_refs(&root->root_item) == 0)
5256 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005257
5258 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5259 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005260 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005261 return 0;
5262}
5263
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005264static void btrfs_dentry_release(struct dentry *dentry)
5265{
Daeseok Youn944a4512014-04-14 15:37:02 +09005266 kfree(dentry->d_fsdata);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005267}
5268
Chris Mason3de45862008-11-17 21:02:50 -05005269static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005270 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005271{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005272 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005273
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005274 inode = btrfs_lookup_dentry(dir, dentry);
5275 if (IS_ERR(inode)) {
5276 if (PTR_ERR(inode) == -ENOENT)
5277 inode = NULL;
5278 else
5279 return ERR_CAST(inode);
5280 }
5281
Josef Bacik3a0dfa62014-02-14 13:43:48 -05005282 return d_materialise_unique(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005283}
5284
Miao Xie16cdcec2011-04-22 18:12:22 +08005285unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005286 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5287};
5288
Al Viro9cdda8d2013-05-22 16:48:09 -04005289static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005290{
Al Viro9cdda8d2013-05-22 16:48:09 -04005291 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005292 struct btrfs_root *root = BTRFS_I(inode)->root;
5293 struct btrfs_item *item;
5294 struct btrfs_dir_item *di;
5295 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005296 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005297 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005298 struct list_head ins_list;
5299 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005300 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005301 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005302 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005303 unsigned char d_type;
5304 int over = 0;
5305 u32 di_cur;
5306 u32 di_total;
5307 u32 di_len;
5308 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005309 char tmp_name[32];
5310 char *name_ptr;
5311 int name_len;
Al Viro9cdda8d2013-05-22 16:48:09 -04005312 int is_curr = 0; /* ctx->pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04005313
5314 /* FIXME, use a real flag for deciding about the key type */
5315 if (root->fs_info->tree_root == root)
5316 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005317
Al Viro9cdda8d2013-05-22 16:48:09 -04005318 if (!dir_emit_dots(file, ctx))
5319 return 0;
5320
David Woodhouse49593bf2008-08-17 17:08:36 +01005321 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005322 if (!path)
5323 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005324
Josef Bacik026fd312011-05-13 10:32:11 -04005325 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005326
Miao Xie16cdcec2011-04-22 18:12:22 +08005327 if (key_type == BTRFS_DIR_INDEX_KEY) {
5328 INIT_LIST_HEAD(&ins_list);
5329 INIT_LIST_HEAD(&del_list);
5330 btrfs_get_delayed_items(inode, &ins_list, &del_list);
5331 }
5332
Chris Mason39279cc2007-06-12 06:35:45 -04005333 btrfs_set_key_type(&key, key_type);
Al Viro9cdda8d2013-05-22 16:48:09 -04005334 key.offset = ctx->pos;
Li Zefan33345d012011-04-20 10:31:50 +08005335 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005336
Chris Mason39279cc2007-06-12 06:35:45 -04005337 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5338 if (ret < 0)
5339 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005340
5341 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005342 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005343 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005344 if (slot >= btrfs_header_nritems(leaf)) {
5345 ret = btrfs_next_leaf(root, path);
5346 if (ret < 0)
5347 goto err;
5348 else if (ret > 0)
5349 break;
5350 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005351 }
Chris Mason3de45862008-11-17 21:02:50 -05005352
Ross Kirkdd3cc162013-09-16 15:58:09 +01005353 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04005354 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5355
5356 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005357 break;
Chris Mason5f39d392007-10-15 16:14:19 -04005358 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04005359 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005360 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005361 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08005362 if (key_type == BTRFS_DIR_INDEX_KEY &&
5363 btrfs_should_delete_dir_index(&del_list,
5364 found_key.offset))
5365 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005366
Al Viro9cdda8d2013-05-22 16:48:09 -04005367 ctx->pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08005368 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005369
Chris Mason39279cc2007-06-12 06:35:45 -04005370 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
5371 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005372 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01005373
5374 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04005375 struct btrfs_key location;
5376
Josef Bacik22a94d42011-03-16 16:47:17 -04005377 if (verify_dir_item(root, leaf, di))
5378 break;
5379
Chris Mason5f39d392007-10-15 16:14:19 -04005380 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01005381 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04005382 name_ptr = tmp_name;
5383 } else {
5384 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01005385 if (!name_ptr) {
5386 ret = -ENOMEM;
5387 goto err;
5388 }
Chris Mason5f39d392007-10-15 16:14:19 -04005389 }
5390 read_extent_buffer(leaf, name_ptr,
5391 (unsigned long)(di + 1), name_len);
5392
5393 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5394 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05005395
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005396
Chris Mason3de45862008-11-17 21:02:50 -05005397 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01005398 * skip it.
5399 *
5400 * In contrast to old kernels, we insert the snapshot's
5401 * dir item and dir index after it has been created, so
5402 * we won't find a reference to our own snapshot. We
5403 * still keep the following code for backward
5404 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05005405 */
5406 if (location.type == BTRFS_ROOT_ITEM_KEY &&
5407 location.objectid == root->root_key.objectid) {
5408 over = 0;
5409 goto skip;
5410 }
Al Viro9cdda8d2013-05-22 16:48:09 -04005411 over = !dir_emit(ctx, name_ptr, name_len,
5412 location.objectid, d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04005413
Chris Mason3de45862008-11-17 21:02:50 -05005414skip:
Chris Mason5f39d392007-10-15 16:14:19 -04005415 if (name_ptr != tmp_name)
5416 kfree(name_ptr);
5417
Chris Mason39279cc2007-06-12 06:35:45 -04005418 if (over)
5419 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05005420 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01005421 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04005422 di_cur += di_len;
5423 di = (struct btrfs_dir_item *)((char *)di + di_len);
5424 }
Li Zefanb9e03af2011-03-23 10:43:58 +08005425next:
5426 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005427 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005428
Miao Xie16cdcec2011-04-22 18:12:22 +08005429 if (key_type == BTRFS_DIR_INDEX_KEY) {
5430 if (is_curr)
Al Viro9cdda8d2013-05-22 16:48:09 -04005431 ctx->pos++;
5432 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005433 if (ret)
5434 goto nopos;
5435 }
5436
David Woodhouse49593bf2008-08-17 17:08:36 +01005437 /* Reached end of directory/root. Bump pos past the last item. */
Zach Browndb62efb2013-07-11 16:19:42 -07005438 ctx->pos++;
5439
5440 /*
5441 * Stop new entries from being returned after we return the last
5442 * entry.
5443 *
5444 * New directory entries are assigned a strictly increasing
5445 * offset. This means that new entries created during readdir
5446 * are *guaranteed* to be seen in the future by that readdir.
5447 * This has broken buggy programs which operate on names as
5448 * they're returned by readdir. Until we re-use freed offsets
5449 * we have this hack to stop new entries from being returned
5450 * under the assumption that they'll never reach this huge
5451 * offset.
5452 *
5453 * This is being careful not to overflow 32bit loff_t unless the
5454 * last entry requires it because doing so has broken 32bit apps
5455 * in the past.
5456 */
5457 if (key_type == BTRFS_DIR_INDEX_KEY) {
5458 if (ctx->pos >= INT_MAX)
5459 ctx->pos = LLONG_MAX;
5460 else
5461 ctx->pos = INT_MAX;
5462 }
Chris Mason39279cc2007-06-12 06:35:45 -04005463nopos:
5464 ret = 0;
5465err:
Miao Xie16cdcec2011-04-22 18:12:22 +08005466 if (key_type == BTRFS_DIR_INDEX_KEY)
5467 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005468 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005469 return ret;
5470}
5471
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005472int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005473{
5474 struct btrfs_root *root = BTRFS_I(inode)->root;
5475 struct btrfs_trans_handle *trans;
5476 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005477 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005478
Josef Bacik72ac3c02012-05-23 14:13:11 -04005479 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005480 return 0;
5481
Liu Bo83eea1f2012-07-10 05:28:39 -06005482 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08005483 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005484
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005485 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005486 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005487 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005488 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005489 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005490 if (IS_ERR(trans))
5491 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06005492 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005493 }
5494 return ret;
5495}
5496
5497/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005498 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005499 * inode changes. But, it is most likely to find the inode in cache.
5500 * FIXME, needs more benchmarking...there are no reasons other than performance
5501 * to keep or drop this code.
5502 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005503static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005504{
5505 struct btrfs_root *root = BTRFS_I(inode)->root;
5506 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005507 int ret;
5508
Josef Bacik72ac3c02012-05-23 14:13:11 -04005509 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005510 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005511
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005512 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005513 if (IS_ERR(trans))
5514 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005515
5516 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005517 if (ret && ret == -ENOSPC) {
5518 /* whoops, lets try again with the full transaction */
5519 btrfs_end_transaction(trans, root);
5520 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005521 if (IS_ERR(trans))
5522 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005523
Chris Mason94b60442010-05-26 11:02:00 -04005524 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005525 }
Chris Mason39279cc2007-06-12 06:35:45 -04005526 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08005527 if (BTRFS_I(inode)->delayed_node)
5528 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005529
5530 return ret;
5531}
5532
5533/*
5534 * This is a copy of file_update_time. We need this so we can return error on
5535 * ENOSPC for updating the inode in the case of file write and mmap writes.
5536 */
Josef Bacike41f9412012-03-26 09:46:47 -04005537static int btrfs_update_time(struct inode *inode, struct timespec *now,
5538 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005539{
Alexander Block2bc5565282012-06-15 09:49:33 +02005540 struct btrfs_root *root = BTRFS_I(inode)->root;
5541
5542 if (btrfs_root_readonly(root))
5543 return -EROFS;
5544
Josef Bacike41f9412012-03-26 09:46:47 -04005545 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005546 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04005547 if (flags & S_CTIME)
5548 inode->i_ctime = *now;
5549 if (flags & S_MTIME)
5550 inode->i_mtime = *now;
5551 if (flags & S_ATIME)
5552 inode->i_atime = *now;
5553 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005554}
5555
Chris Masond352ac62008-09-29 15:18:18 -04005556/*
5557 * find the highest existing sequence number in a directory
5558 * and then set the in-memory index_cnt variable to reflect
5559 * free sequence numbers
5560 */
Josef Bacikaec74772008-07-24 12:12:38 -04005561static int btrfs_set_inode_index_count(struct inode *inode)
5562{
5563 struct btrfs_root *root = BTRFS_I(inode)->root;
5564 struct btrfs_key key, found_key;
5565 struct btrfs_path *path;
5566 struct extent_buffer *leaf;
5567 int ret;
5568
Li Zefan33345d012011-04-20 10:31:50 +08005569 key.objectid = btrfs_ino(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005570 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
5571 key.offset = (u64)-1;
5572
5573 path = btrfs_alloc_path();
5574 if (!path)
5575 return -ENOMEM;
5576
5577 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5578 if (ret < 0)
5579 goto out;
5580 /* FIXME: we should be able to handle this */
5581 if (ret == 0)
5582 goto out;
5583 ret = 0;
5584
5585 /*
5586 * MAGIC NUMBER EXPLANATION:
5587 * since we search a directory based on f_pos we have to start at 2
5588 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5589 * else has to start at 2
5590 */
5591 if (path->slots[0] == 0) {
5592 BTRFS_I(inode)->index_cnt = 2;
5593 goto out;
5594 }
5595
5596 path->slots[0]--;
5597
5598 leaf = path->nodes[0];
5599 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5600
Li Zefan33345d012011-04-20 10:31:50 +08005601 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacikaec74772008-07-24 12:12:38 -04005602 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
5603 BTRFS_I(inode)->index_cnt = 2;
5604 goto out;
5605 }
5606
5607 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
5608out:
5609 btrfs_free_path(path);
5610 return ret;
5611}
5612
Chris Masond352ac62008-09-29 15:18:18 -04005613/*
5614 * helper to find a free sequence number in a given directory. This current
5615 * code is very simple, later versions will do smarter things in the btree
5616 */
Chris Mason3de45862008-11-17 21:02:50 -05005617int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04005618{
5619 int ret = 0;
5620
5621 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08005622 ret = btrfs_inode_delayed_dir_index_count(dir);
5623 if (ret) {
5624 ret = btrfs_set_inode_index_count(dir);
5625 if (ret)
5626 return ret;
5627 }
Josef Bacikaec74772008-07-24 12:12:38 -04005628 }
5629
Chris Mason00e4e6b2008-08-05 11:18:09 -04005630 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04005631 BTRFS_I(dir)->index_cnt++;
5632
5633 return ret;
5634}
5635
Chris Mason39279cc2007-06-12 06:35:45 -04005636static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5637 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04005638 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05005639 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04005640 u64 ref_objectid, u64 objectid,
5641 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04005642{
5643 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005644 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04005645 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04005646 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05005647 struct btrfs_inode_ref *ref;
5648 struct btrfs_key key[2];
5649 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005650 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05005651 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04005652 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005653
Chris Mason5f39d392007-10-15 16:14:19 -04005654 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005655 if (!path)
5656 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04005657
Chris Mason39279cc2007-06-12 06:35:45 -04005658 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005659 if (!inode) {
5660 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005661 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005662 }
Chris Mason39279cc2007-06-12 06:35:45 -04005663
Li Zefan581bb052011-04-20 10:06:11 +08005664 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01005665 * O_TMPFILE, set link count to 0, so that after this point,
5666 * we fill in an inode item with the correct link count.
5667 */
5668 if (!name)
5669 set_nlink(inode, 0);
5670
5671 /*
Li Zefan581bb052011-04-20 10:06:11 +08005672 * we have to initialize this early, so we can reclaim the inode
5673 * number if we fail afterwards in this function.
5674 */
5675 inode->i_ino = objectid;
5676
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005677 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00005678 trace_btrfs_inode_request(dir);
5679
Chris Mason3de45862008-11-17 21:02:50 -05005680 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04005681 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005682 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005683 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005684 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04005685 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005686 } else if (dir) {
5687 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04005688 }
5689 /*
5690 * index_cnt is ignored for everything but a dir,
5691 * btrfs_get_inode_index_count has an explanation for the magic
5692 * number
5693 */
5694 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08005695 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04005696 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04005697 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00005698 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04005699
Josef Bacik5dc562c2012-08-17 13:14:17 -04005700 /*
5701 * We could have gotten an inode number from somebody who was fsynced
5702 * and then removed in this same transaction, so let's just set full
5703 * sync since it will be a full sync anyway and this will blow away the
5704 * old info in the log.
5705 */
5706 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
5707
Chris Mason9c583092008-01-29 15:15:18 -05005708 key[0].objectid = objectid;
5709 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
5710 key[0].offset = 0;
5711
Chris Mason9c583092008-01-29 15:15:18 -05005712 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005713
5714 if (name) {
5715 /*
5716 * Start new inodes with an inode_ref. This is slightly more
5717 * efficient for small numbers of hard links since they will
5718 * be packed into one item. Extended refs will kick in if we
5719 * add more hard links than can fit in the ref item.
5720 */
5721 key[1].objectid = objectid;
5722 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
5723 key[1].offset = ref_objectid;
5724
5725 sizes[1] = name_len + sizeof(*ref);
5726 }
Chris Mason9c583092008-01-29 15:15:18 -05005727
Chris Masonb9473432009-03-13 11:00:37 -04005728 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005729 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05005730 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04005731 goto fail;
5732
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03005733 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04005734 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005735 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04005736 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
5737 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06005738 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
5739 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04005740 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05005741
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005742 if (name) {
5743 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
5744 struct btrfs_inode_ref);
5745 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
5746 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
5747 ptr = (unsigned long)(ref + 1);
5748 write_extent_buffer(path->nodes[0], name, ptr, name_len);
5749 }
Chris Mason9c583092008-01-29 15:15:18 -05005750
Chris Mason5f39d392007-10-15 16:14:19 -04005751 btrfs_mark_buffer_dirty(path->nodes[0]);
5752 btrfs_free_path(path);
5753
Chris Mason39279cc2007-06-12 06:35:45 -04005754 location = &BTRFS_I(inode)->location;
5755 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04005756 location->offset = 0;
5757 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
5758
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005759 btrfs_inherit_iflags(inode, dir);
5760
Al Viro569254b2011-07-24 17:08:40 -04005761 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04005762 if (btrfs_test_opt(root, NODATASUM))
5763 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo213490b2012-09-11 08:33:50 -06005764 if (btrfs_test_opt(root, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05005765 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
5766 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04005767 }
5768
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005769 btrfs_insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005770 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00005771
5772 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04005773 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00005774
Alexander Block8ea05e32012-07-25 17:35:53 +02005775 btrfs_update_root_times(trans, root);
5776
Filipe David Borba Manana63541922014-01-07 11:47:46 +00005777 ret = btrfs_inode_inherit_props(trans, inode, dir);
5778 if (ret)
5779 btrfs_err(root->fs_info,
5780 "error inheriting props for ino %llu (root %llu): %d",
5781 btrfs_ino(inode), root->root_key.objectid, ret);
5782
Chris Mason39279cc2007-06-12 06:35:45 -04005783 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005784fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005785 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04005786 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04005787 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005788 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005789 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005790}
5791
5792static inline u8 btrfs_inode_type(struct inode *inode)
5793{
5794 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
5795}
5796
Chris Masond352ac62008-09-29 15:18:18 -04005797/*
5798 * utility function to add 'inode' into 'parent_inode' with
5799 * a give name and a given sequence number.
5800 * if 'add_backref' is true, also insert a backref from the
5801 * inode to the parent directory.
5802 */
Chris Masone02119d2008-09-05 16:13:11 -04005803int btrfs_add_link(struct btrfs_trans_handle *trans,
5804 struct inode *parent_inode, struct inode *inode,
5805 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005806{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005807 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005808 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04005809 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08005810 u64 ino = btrfs_ino(inode);
5811 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005812
Li Zefan33345d012011-04-20 10:31:50 +08005813 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005814 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
5815 } else {
Li Zefan33345d012011-04-20 10:31:50 +08005816 key.objectid = ino;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005817 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
5818 key.offset = 0;
5819 }
Chris Mason39279cc2007-06-12 06:35:45 -04005820
Li Zefan33345d012011-04-20 10:31:50 +08005821 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005822 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
5823 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08005824 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005825 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08005826 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
5827 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005828 }
5829
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005830 /* Nothing to clean up yet */
5831 if (ret)
5832 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005833
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005834 ret = btrfs_insert_dir_item(trans, root, name, name_len,
5835 parent_inode, &key,
5836 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05005837 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005838 goto fail_dir_item;
5839 else if (ret) {
5840 btrfs_abort_transaction(trans, root, ret);
5841 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005842 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005843
5844 btrfs_i_size_write(parent_inode, parent_inode->i_size +
5845 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005846 inode_inc_iversion(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005847 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
5848 ret = btrfs_update_inode(trans, root, parent_inode);
5849 if (ret)
5850 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005851 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05005852
5853fail_dir_item:
5854 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
5855 u64 local_index;
5856 int err;
5857 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
5858 key.objectid, root->root_key.objectid,
5859 parent_ino, &local_index, name, name_len);
5860
5861 } else if (add_backref) {
5862 u64 local_index;
5863 int err;
5864
5865 err = btrfs_del_inode_ref(trans, root, name, name_len,
5866 ino, parent_ino, &local_index);
5867 }
5868 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005869}
5870
5871static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00005872 struct inode *dir, struct dentry *dentry,
5873 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005874{
Josef Bacika1b075d2010-11-19 20:36:11 +00005875 int err = btrfs_add_link(trans, dir, inode,
5876 dentry->d_name.name, dentry->d_name.len,
5877 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005878 if (err > 0)
5879 err = -EEXIST;
5880 return err;
5881}
5882
Josef Bacik618e21d2007-07-11 10:18:17 -04005883static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04005884 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04005885{
5886 struct btrfs_trans_handle *trans;
5887 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005888 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04005889 int err;
5890 int drop_inode = 0;
5891 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005892 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04005893
5894 if (!new_valid_dev(rdev))
5895 return -EINVAL;
5896
Josef Bacik9ed74f22009-09-11 16:12:44 -04005897 /*
5898 * 2 for inode item and ref
5899 * 2 for dir items
5900 * 1 for xattr if selinux is on
5901 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005902 trans = btrfs_start_transaction(root, 5);
5903 if (IS_ERR(trans))
5904 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05005905
Li Zefan581bb052011-04-20 10:06:11 +08005906 err = btrfs_find_free_ino(root, &objectid);
5907 if (err)
5908 goto out_unlock;
5909
Josef Bacikaec74772008-07-24 12:12:38 -04005910 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005911 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04005912 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005913 if (IS_ERR(inode)) {
5914 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005915 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005916 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005917
Eric Paris2a7dba32011-02-01 11:05:39 -05005918 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005919 if (err) {
5920 drop_inode = 1;
5921 goto out_unlock;
5922 }
5923
Casey Schauflerad19db72011-12-15 10:09:07 -05005924 /*
5925 * If the active LSM wants to access the inode during
5926 * d_instantiate it needs these. Smack checks to see
5927 * if the filesystem supports xattrs by looking at the
5928 * ops vector.
5929 */
5930
5931 inode->i_op = &btrfs_special_inode_operations;
Josef Bacika1b075d2010-11-19 20:36:11 +00005932 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04005933 if (err)
5934 drop_inode = 1;
5935 else {
Josef Bacik618e21d2007-07-11 10:18:17 -04005936 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04005937 btrfs_update_inode(trans, root, inode);
Al Viro08c422c2011-12-23 07:58:13 -05005938 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005939 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005940out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005941 btrfs_end_transaction(trans, root);
Josef Bacikc581afc2014-02-06 16:06:06 -05005942 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00005943 btrfs_btree_balance_dirty(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04005944 if (drop_inode) {
5945 inode_dec_link_count(inode);
5946 iput(inode);
5947 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005948 return err;
5949}
5950
Chris Mason39279cc2007-06-12 06:35:45 -04005951static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04005952 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04005953{
5954 struct btrfs_trans_handle *trans;
5955 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005956 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005957 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04005958 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04005959 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005960 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005961
Josef Bacik9ed74f22009-09-11 16:12:44 -04005962 /*
5963 * 2 for inode item and ref
5964 * 2 for dir items
5965 * 1 for xattr if selinux is on
5966 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005967 trans = btrfs_start_transaction(root, 5);
5968 if (IS_ERR(trans))
5969 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005970
Li Zefan581bb052011-04-20 10:06:11 +08005971 err = btrfs_find_free_ino(root, &objectid);
5972 if (err)
5973 goto out_unlock;
5974
Josef Bacikaec74772008-07-24 12:12:38 -04005975 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005976 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04005977 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005978 if (IS_ERR(inode)) {
5979 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005980 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005981 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005982 drop_inode_on_err = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04005983
Eric Paris2a7dba32011-02-01 11:05:39 -05005984 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005985 if (err)
Josef Bacik33268ea2008-07-24 12:16:36 -04005986 goto out_unlock;
Josef Bacik33268ea2008-07-24 12:16:36 -04005987
Filipe Brandenburger9185aa52012-11-30 03:40:08 +00005988 err = btrfs_update_inode(trans, root, inode);
5989 if (err)
5990 goto out_unlock;
5991
Casey Schauflerad19db72011-12-15 10:09:07 -05005992 /*
5993 * If the active LSM wants to access the inode during
5994 * d_instantiate it needs these. Smack checks to see
5995 * if the filesystem supports xattrs by looking at the
5996 * ops vector.
5997 */
5998 inode->i_fop = &btrfs_file_operations;
5999 inode->i_op = &btrfs_file_inode_operations;
6000
Josef Bacika1b075d2010-11-19 20:36:11 +00006001 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006002 if (err)
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006003 goto out_unlock;
6004
6005 inode->i_mapping->a_ops = &btrfs_aops;
6006 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
6007 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
6008 d_instantiate(dentry, inode);
6009
Chris Mason39279cc2007-06-12 06:35:45 -04006010out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006011 btrfs_end_transaction(trans, root);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006012 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006013 inode_dec_link_count(inode);
6014 iput(inode);
6015 }
Josef Bacikc581afc2014-02-06 16:06:06 -05006016 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006017 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006018 return err;
6019}
6020
6021static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6022 struct dentry *dentry)
6023{
6024 struct btrfs_trans_handle *trans;
6025 struct btrfs_root *root = BTRFS_I(dir)->root;
6026 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006027 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006028 int err;
6029 int drop_inode = 0;
6030
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006031 /* do not allow sys_link's with other subvols of the same device */
6032 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006033 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006034
Mark Fashehf1863732012-08-08 11:32:27 -07006035 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006036 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006037
Chris Mason3de45862008-11-17 21:02:50 -05006038 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006039 if (err)
6040 goto fail;
6041
Yan, Zhenga22285a2010-05-16 10:48:46 -04006042 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006043 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006044 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006045 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04006046 */
Miao Xie7e6b6462011-02-18 09:21:17 +00006047 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006048 if (IS_ERR(trans)) {
6049 err = PTR_ERR(trans);
6050 goto fail;
6051 }
Chris Mason5f39d392007-10-15 16:14:19 -04006052
Miao Xie67de1172013-12-26 13:07:06 +08006053 /* There are several dir indexes for this inode, clear the cache. */
6054 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006055 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006056 inode_inc_iversion(inode);
Miao Xie31534952011-04-13 13:19:21 +08006057 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6ee2010-10-23 11:11:40 -04006058 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006059 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006060
Josef Bacika1b075d2010-11-19 20:36:11 +00006061 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006062
Yan, Zhenga5719522009-09-24 09:17:31 -04006063 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006064 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006065 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006066 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006067 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006068 if (err)
6069 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006070 if (inode->i_nlink == 1) {
6071 /*
6072 * If new hard link count is 1, it's a file created
6073 * with open(2) O_TMPFILE flag.
6074 */
6075 err = btrfs_orphan_del(trans, inode);
6076 if (err)
6077 goto fail;
6078 }
Al Viro08c422c2011-12-23 07:58:13 -05006079 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00006080 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006081 }
Chris Mason39279cc2007-06-12 06:35:45 -04006082
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006083 btrfs_end_transaction(trans, root);
Josef Bacikc581afc2014-02-06 16:06:06 -05006084 btrfs_balance_delayed_items(root);
Chris Mason1832a6d2007-12-21 16:27:21 -05006085fail:
Chris Mason39279cc2007-06-12 06:35:45 -04006086 if (drop_inode) {
6087 inode_dec_link_count(inode);
6088 iput(inode);
6089 }
Liu Bob53d3f52012-11-14 14:34:34 +00006090 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006091 return err;
6092}
6093
Al Viro18bb1db2011-07-26 01:41:39 -04006094static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006095{
Chris Masonb9d86662008-05-02 16:13:49 -04006096 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006097 struct btrfs_trans_handle *trans;
6098 struct btrfs_root *root = BTRFS_I(dir)->root;
6099 int err = 0;
6100 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006101 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006102 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006103
Josef Bacik9ed74f22009-09-11 16:12:44 -04006104 /*
6105 * 2 items for inode and ref
6106 * 2 items for dir items
6107 * 1 for xattr if selinux is on
6108 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006109 trans = btrfs_start_transaction(root, 5);
6110 if (IS_ERR(trans))
6111 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006112
Li Zefan581bb052011-04-20 10:06:11 +08006113 err = btrfs_find_free_ino(root, &objectid);
6114 if (err)
6115 goto out_fail;
6116
Josef Bacikaec74772008-07-24 12:12:38 -04006117 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006118 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04006119 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006120 if (IS_ERR(inode)) {
6121 err = PTR_ERR(inode);
6122 goto out_fail;
6123 }
Chris Mason5f39d392007-10-15 16:14:19 -04006124
Chris Mason39279cc2007-06-12 06:35:45 -04006125 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04006126
Eric Paris2a7dba32011-02-01 11:05:39 -05006127 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006128 if (err)
6129 goto out_fail;
6130
Chris Mason39279cc2007-06-12 06:35:45 -04006131 inode->i_op = &btrfs_dir_inode_operations;
6132 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04006133
Chris Masondbe674a2008-07-17 12:54:05 -04006134 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006135 err = btrfs_update_inode(trans, root, inode);
6136 if (err)
6137 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006138
Josef Bacika1b075d2010-11-19 20:36:11 +00006139 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
6140 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006141 if (err)
6142 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006143
Chris Mason39279cc2007-06-12 06:35:45 -04006144 d_instantiate(dentry, inode);
6145 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006146
6147out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006148 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04006149 if (drop_on_err)
6150 iput(inode);
Josef Bacikc581afc2014-02-06 16:06:06 -05006151 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006152 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006153 return err;
6154}
6155
Chris Masond352ac62008-09-29 15:18:18 -04006156/* helper for btfs_get_extent. Given an existing extent in the tree,
6157 * and an extent that you want to insert, deal with overlap and insert
6158 * the new extent into the tree.
6159 */
Chris Mason3b951512008-04-17 11:29:12 -04006160static int merge_extent_mapping(struct extent_map_tree *em_tree,
6161 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006162 struct extent_map *em,
Qu Wenruo51f395a2014-08-08 13:06:20 +08006163 u64 map_start)
Chris Mason3b951512008-04-17 11:29:12 -04006164{
6165 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04006166
Chris Masone6dcd2d2008-07-17 12:53:50 -04006167 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
6168 start_diff = map_start - em->start;
6169 em->start = map_start;
Qu Wenruo51f395a2014-08-08 13:06:20 +08006170 em->len = existing->start - em->start;
Chris Masonc8b97812008-10-29 14:49:59 -04006171 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
6172 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006173 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04006174 em->block_len -= start_diff;
6175 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006176 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006177}
6178
Chris Masonc8b97812008-10-29 14:49:59 -04006179static noinline int uncompress_inline(struct btrfs_path *path,
6180 struct inode *inode, struct page *page,
6181 size_t pg_offset, u64 extent_offset,
6182 struct btrfs_file_extent_item *item)
6183{
6184 int ret;
6185 struct extent_buffer *leaf = path->nodes[0];
6186 char *tmp;
6187 size_t max_size;
6188 unsigned long inline_size;
6189 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006190 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006191
6192 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006193 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006194 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6195 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006196 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006197 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006198 if (!tmp)
6199 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006200 ptr = btrfs_file_extent_inline_start(item);
6201
6202 read_extent_buffer(leaf, tmp, ptr, inline_size);
6203
Chris Mason5b050f02008-11-11 09:34:41 -05006204 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006205 ret = btrfs_decompress(compress_type, tmp, page,
6206 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006207 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006208 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006209}
6210
Chris Masond352ac62008-09-29 15:18:18 -04006211/*
6212 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006213 * the ugly parts come from merging extents from the disk with the in-ram
6214 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006215 * where the in-ram extents might be locked pending data=ordered completion.
6216 *
6217 * This also copies inline extents directly into the page.
6218 */
Chris Masond3977122009-01-05 21:25:51 -05006219
Chris Masona52d9a82007-08-27 16:49:44 -04006220struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05006221 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04006222 int create)
6223{
6224 int ret;
6225 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006226 u64 extent_start = 0;
6227 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08006228 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006229 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006230 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04006231 struct btrfs_root *root = BTRFS_I(inode)->root;
6232 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006233 struct extent_buffer *leaf;
6234 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006235 struct extent_map *em = NULL;
6236 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05006237 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006238 struct btrfs_trans_handle *trans = NULL;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006239 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006240
Chris Masona52d9a82007-08-27 16:49:44 -04006241again:
Chris Mason890871b2009-09-02 16:24:52 -04006242 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006243 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006244 if (em)
6245 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006246 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006247
Chris Masona52d9a82007-08-27 16:49:44 -04006248 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006249 if (em->start > start || em->start + em->len <= start)
6250 free_extent_map(em);
6251 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006252 free_extent_map(em);
6253 else
6254 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006255 }
David Sterba172ddd62011-04-21 00:48:27 +02006256 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006257 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006258 err = -ENOMEM;
6259 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006260 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006261 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006262 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006263 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006264 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006265 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006266
6267 if (!path) {
6268 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006269 if (!path) {
6270 err = -ENOMEM;
6271 goto out;
6272 }
6273 /*
6274 * Chances are we'll be called again, so go ahead and do
6275 * readahead
6276 */
6277 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04006278 }
6279
Chris Mason179e29e2007-11-01 11:28:41 -04006280 ret = btrfs_lookup_file_extent(trans, root, path,
6281 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006282 if (ret < 0) {
6283 err = ret;
6284 goto out;
6285 }
6286
6287 if (ret != 0) {
6288 if (path->slots[0] == 0)
6289 goto not_found;
6290 path->slots[0]--;
6291 }
6292
Chris Mason5f39d392007-10-15 16:14:19 -04006293 leaf = path->nodes[0];
6294 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006295 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006296 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006297 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6298 found_type = btrfs_key_type(&found_key);
6299 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006300 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006301 /*
6302 * If we backup past the first extent we want to move forward
6303 * and see if there is an extent in front of us, otherwise we'll
6304 * say there is a hole for our whole search range which can
6305 * cause problems.
6306 */
6307 extent_end = start;
6308 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006309 }
6310
Chris Mason5f39d392007-10-15 16:14:19 -04006311 found_type = btrfs_file_extent_type(leaf, item);
6312 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04006313 if (found_type == BTRFS_FILE_EXTENT_REG ||
6314 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006315 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006316 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006317 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6318 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08006319 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Qu Wenruofda28322013-02-26 08:10:22 +00006320 extent_end = ALIGN(extent_start + size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04006321 }
Josef Bacik25a50342013-10-14 12:08:38 -04006322next:
Yan Zheng9036c102008-10-30 14:19:41 -04006323 if (start >= extent_end) {
6324 path->slots[0]++;
6325 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6326 ret = btrfs_next_leaf(root, path);
6327 if (ret < 0) {
6328 err = ret;
6329 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006330 }
Yan Zheng9036c102008-10-30 14:19:41 -04006331 if (ret > 0)
6332 goto not_found;
6333 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006334 }
Yan Zheng9036c102008-10-30 14:19:41 -04006335 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6336 if (found_key.objectid != objectid ||
6337 found_key.type != BTRFS_EXTENT_DATA_KEY)
6338 goto not_found;
6339 if (start + len <= found_key.offset)
6340 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006341 if (start > found_key.offset)
6342 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04006343 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006344 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006345 em->len = found_key.offset - start;
6346 goto not_found_em;
6347 }
6348
Filipe Manana7ffbb592014-06-09 03:48:05 +01006349 btrfs_extent_item_to_extent_map(inode, path, item, new_inline, em);
6350
Yan Zhengd899e052008-10-30 14:25:28 -04006351 if (found_type == BTRFS_FILE_EXTENT_REG ||
6352 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006353 goto insert;
6354 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006355 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006356 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006357 size_t size;
6358 size_t extent_offset;
6359 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006360
Filipe Manana7ffbb592014-06-09 03:48:05 +01006361 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04006362 goto out;
Yan689f9342007-10-29 11:41:07 -04006363
Chris Mason514ac8a2014-01-03 21:07:00 -08006364 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04006365 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05006366 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04006367 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006368 em->start = extent_start + extent_offset;
Qu Wenruofda28322013-02-26 08:10:22 +00006369 em->len = ALIGN(copy_size, root->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006370 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006371 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006372 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006373 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006374 if (btrfs_file_extent_compression(leaf, item) !=
6375 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006376 ret = uncompress_inline(path, inode, page,
6377 pg_offset,
6378 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04006379 if (ret) {
6380 err = ret;
6381 goto out;
6382 }
Chris Masonc8b97812008-10-29 14:49:59 -04006383 } else {
6384 map = kmap(page);
6385 read_extent_buffer(leaf, map + pg_offset, ptr,
6386 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04006387 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
6388 memset(map + pg_offset + copy_size, 0,
6389 PAGE_CACHE_SIZE - pg_offset -
6390 copy_size);
6391 }
Chris Masonc8b97812008-10-29 14:49:59 -04006392 kunmap(page);
6393 }
Chris Mason179e29e2007-11-01 11:28:41 -04006394 flush_dcache_page(page);
6395 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006396 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006397 if (!trans) {
6398 kunmap(page);
6399 free_extent_map(em);
6400 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006401
David Sterbab3b4aa72011-04-21 01:20:15 +02006402 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006403 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006404
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006405 if (IS_ERR(trans))
6406 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006407 goto again;
6408 }
Chris Masonc8b97812008-10-29 14:49:59 -04006409 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006410 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006411 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006412 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006413 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006414 }
Chris Masond1310b22008-01-24 16:13:08 -05006415 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006416 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006417 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04006418 }
6419not_found:
6420 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006421 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006422 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006423not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04006424 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04006425 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006426insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006427 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006428 if (em->start > start || extent_map_end(em) <= start) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006429 btrfs_err(root->fs_info, "bad extent! em: [%llu %llu] passed [%llu %llu]",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006430 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04006431 err = -EIO;
6432 goto out;
6433 }
Chris Masond1310b22008-01-24 16:13:08 -05006434
6435 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006436 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006437 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006438 /* it is possible that someone inserted the extent into the tree
6439 * while we had the lock dropped. It is also possible that
6440 * an overlapping map exists in the tree
6441 */
Chris Masona52d9a82007-08-27 16:49:44 -04006442 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04006443 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006444
6445 ret = 0;
6446
Chris Mason3b951512008-04-17 11:29:12 -04006447 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04006448 if (existing && (existing->start > start ||
6449 existing->start + existing->len <= start)) {
6450 free_extent_map(existing);
6451 existing = NULL;
6452 }
Chris Mason3b951512008-04-17 11:29:12 -04006453 if (!existing) {
6454 existing = lookup_extent_mapping(em_tree, em->start,
6455 em->len);
6456 if (existing) {
6457 err = merge_extent_mapping(em_tree, existing,
Qu Wenruo51f395a2014-08-08 13:06:20 +08006458 em, start);
Chris Mason3b951512008-04-17 11:29:12 -04006459 free_extent_map(existing);
6460 if (err) {
6461 free_extent_map(em);
6462 em = NULL;
6463 }
6464 } else {
6465 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04006466 free_extent_map(em);
6467 em = NULL;
6468 }
6469 } else {
6470 free_extent_map(em);
6471 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006472 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006473 }
Chris Masona52d9a82007-08-27 16:49:44 -04006474 }
Chris Mason890871b2009-09-02 16:24:52 -04006475 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006476out:
liubo1abe9b82011-03-24 11:18:59 +00006477
Steven Rostedt4cd85872013-11-14 22:57:29 -05006478 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00006479
Chris Masonf4219502008-07-22 11:18:09 -04006480 if (path)
6481 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04006482 if (trans) {
6483 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05006484 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04006485 err = ret;
6486 }
Chris Masona52d9a82007-08-27 16:49:44 -04006487 if (err) {
6488 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04006489 return ERR_PTR(err);
6490 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006491 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04006492 return em;
6493}
6494
Chris Masonec29ed52011-02-23 16:23:20 -05006495struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
6496 size_t pg_offset, u64 start, u64 len,
6497 int create)
6498{
6499 struct extent_map *em;
6500 struct extent_map *hole_em = NULL;
6501 u64 range_start = start;
6502 u64 end;
6503 u64 found;
6504 u64 found_end;
6505 int err = 0;
6506
6507 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
6508 if (IS_ERR(em))
6509 return em;
6510 if (em) {
6511 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00006512 * if our em maps to
6513 * - a hole or
6514 * - a pre-alloc extent,
6515 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05006516 */
Liu Bof9e4fb52013-01-07 10:10:12 +00006517 if (em->block_start != EXTENT_MAP_HOLE &&
6518 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05006519 return em;
6520 else
6521 hole_em = em;
6522 }
6523
6524 /* check to see if we've wrapped (len == -1 or similar) */
6525 end = start + len;
6526 if (end < start)
6527 end = (u64)-1;
6528 else
6529 end -= 1;
6530
6531 em = NULL;
6532
6533 /* ok, we didn't find anything, lets look for delalloc */
6534 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
6535 end, len, EXTENT_DELALLOC, 1);
6536 found_end = range_start + found;
6537 if (found_end < range_start)
6538 found_end = (u64)-1;
6539
6540 /*
6541 * we didn't find anything useful, return
6542 * the original results from get_extent()
6543 */
6544 if (range_start > end || found_end <= start) {
6545 em = hole_em;
6546 hole_em = NULL;
6547 goto out;
6548 }
6549
6550 /* adjust the range_start to make sure it doesn't
6551 * go backwards from the start they passed in
6552 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05306553 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05006554 found = found_end - range_start;
6555
6556 if (found > 0) {
6557 u64 hole_start = start;
6558 u64 hole_len = len;
6559
David Sterba172ddd62011-04-21 00:48:27 +02006560 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05006561 if (!em) {
6562 err = -ENOMEM;
6563 goto out;
6564 }
6565 /*
6566 * when btrfs_get_extent can't find anything it
6567 * returns one huge hole
6568 *
6569 * make sure what it found really fits our range, and
6570 * adjust to make sure it is based on the start from
6571 * the caller
6572 */
6573 if (hole_em) {
6574 u64 calc_end = extent_map_end(hole_em);
6575
6576 if (calc_end <= start || (hole_em->start > end)) {
6577 free_extent_map(hole_em);
6578 hole_em = NULL;
6579 } else {
6580 hole_start = max(hole_em->start, start);
6581 hole_len = calc_end - hole_start;
6582 }
6583 }
6584 em->bdev = NULL;
6585 if (hole_em && range_start > hole_start) {
6586 /* our hole starts before our delalloc, so we
6587 * have to return just the parts of the hole
6588 * that go until the delalloc starts
6589 */
6590 em->len = min(hole_len,
6591 range_start - hole_start);
6592 em->start = hole_start;
6593 em->orig_start = hole_start;
6594 /*
6595 * don't adjust block start at all,
6596 * it is fixed at EXTENT_MAP_HOLE
6597 */
6598 em->block_start = hole_em->block_start;
6599 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00006600 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
6601 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05006602 } else {
6603 em->start = range_start;
6604 em->len = found;
6605 em->orig_start = range_start;
6606 em->block_start = EXTENT_MAP_DELALLOC;
6607 em->block_len = found;
6608 }
6609 } else if (hole_em) {
6610 return hole_em;
6611 }
6612out:
6613
6614 free_extent_map(hole_em);
6615 if (err) {
6616 free_extent_map(em);
6617 return ERR_PTR(err);
6618 }
6619 return em;
6620}
6621
Josef Bacik4b46fce2010-05-23 11:00:55 -04006622static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
6623 u64 start, u64 len)
6624{
6625 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04006626 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006627 struct btrfs_key ins;
6628 u64 alloc_hint;
6629 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006630
Josef Bacik4b46fce2010-05-23 11:00:55 -04006631 alloc_hint = get_extent_allocation_hint(inode, start, len);
Josef Bacik00361582013-08-14 14:02:47 -04006632 ret = btrfs_reserve_extent(root, len, root->sectorsize, 0,
Miao Xiee570fd22014-06-19 10:42:50 +08006633 alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006634 if (ret)
6635 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006636
Josef Bacik70c8a912012-10-11 16:54:30 -04006637 em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006638 ins.offset, ins.offset, ins.offset, 0);
Josef Bacik00361582013-08-14 14:02:47 -04006639 if (IS_ERR(em)) {
Miao Xiee570fd22014-06-19 10:42:50 +08006640 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006641 return em;
6642 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006643
6644 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
6645 ins.offset, ins.offset, 0);
6646 if (ret) {
Miao Xiee570fd22014-06-19 10:42:50 +08006647 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006648 free_extent_map(em);
6649 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006650 }
Josef Bacik00361582013-08-14 14:02:47 -04006651
Josef Bacik4b46fce2010-05-23 11:00:55 -04006652 return em;
6653}
6654
Chris Mason46bfbb52010-05-26 11:04:10 -04006655/*
6656 * returns 1 when the nocow is safe, < 1 on error, 0 if the
6657 * block must be cow'd
6658 */
Josef Bacik00361582013-08-14 14:02:47 -04006659noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04006660 u64 *orig_start, u64 *orig_block_len,
6661 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04006662{
Josef Bacik00361582013-08-14 14:02:47 -04006663 struct btrfs_trans_handle *trans;
Chris Mason46bfbb52010-05-26 11:04:10 -04006664 struct btrfs_path *path;
6665 int ret;
6666 struct extent_buffer *leaf;
6667 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08006668 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04006669 struct btrfs_file_extent_item *fi;
6670 struct btrfs_key key;
6671 u64 disk_bytenr;
6672 u64 backref_offset;
6673 u64 extent_end;
6674 u64 num_bytes;
6675 int slot;
6676 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04006677 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08006678
Chris Mason46bfbb52010-05-26 11:04:10 -04006679 path = btrfs_alloc_path();
6680 if (!path)
6681 return -ENOMEM;
6682
Josef Bacik00361582013-08-14 14:02:47 -04006683 ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04006684 offset, 0);
6685 if (ret < 0)
6686 goto out;
6687
6688 slot = path->slots[0];
6689 if (ret == 1) {
6690 if (slot == 0) {
6691 /* can't find the item, must cow */
6692 ret = 0;
6693 goto out;
6694 }
6695 slot--;
6696 }
6697 ret = 0;
6698 leaf = path->nodes[0];
6699 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08006700 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04006701 key.type != BTRFS_EXTENT_DATA_KEY) {
6702 /* not our file or wrong item type, must cow */
6703 goto out;
6704 }
6705
6706 if (key.offset > offset) {
6707 /* Wrong offset, must cow */
6708 goto out;
6709 }
6710
6711 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
6712 found_type = btrfs_file_extent_type(leaf, fi);
6713 if (found_type != BTRFS_FILE_EXTENT_REG &&
6714 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
6715 /* not a regular extent, must cow */
6716 goto out;
6717 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04006718
6719 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
6720 goto out;
6721
Miao Xiee77751a2013-12-27 21:11:50 +08006722 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
6723 if (extent_end <= offset)
6724 goto out;
6725
Chris Mason46bfbb52010-05-26 11:04:10 -04006726 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04006727 if (disk_bytenr == 0)
6728 goto out;
6729
6730 if (btrfs_file_extent_compression(leaf, fi) ||
6731 btrfs_file_extent_encryption(leaf, fi) ||
6732 btrfs_file_extent_other_encoding(leaf, fi))
6733 goto out;
6734
Chris Mason46bfbb52010-05-26 11:04:10 -04006735 backref_offset = btrfs_file_extent_offset(leaf, fi);
6736
Josef Bacik7ee9e442013-06-21 16:37:03 -04006737 if (orig_start) {
6738 *orig_start = key.offset - backref_offset;
6739 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
6740 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
6741 }
Josef Bacikeb384b52013-04-24 16:32:55 -04006742
Chris Mason46bfbb52010-05-26 11:04:10 -04006743 if (btrfs_extent_readonly(root, disk_bytenr))
6744 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08006745
6746 num_bytes = min(offset + *len, extent_end) - offset;
6747 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
6748 u64 range_end;
6749
6750 range_end = round_up(offset + num_bytes, root->sectorsize) - 1;
6751 ret = test_range_bit(io_tree, offset, range_end,
6752 EXTENT_DELALLOC, 0, NULL);
6753 if (ret) {
6754 ret = -EAGAIN;
6755 goto out;
6756 }
6757 }
6758
Josef Bacik1bda19e2013-10-18 12:10:36 -04006759 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04006760
6761 /*
6762 * look for other files referencing this extent, if we
6763 * find any we must cow
6764 */
Josef Bacik00361582013-08-14 14:02:47 -04006765 trans = btrfs_join_transaction(root);
6766 if (IS_ERR(trans)) {
6767 ret = 0;
Chris Mason46bfbb52010-05-26 11:04:10 -04006768 goto out;
Josef Bacik00361582013-08-14 14:02:47 -04006769 }
6770
6771 ret = btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
6772 key.offset - backref_offset, disk_bytenr);
6773 btrfs_end_transaction(trans, root);
6774 if (ret) {
6775 ret = 0;
6776 goto out;
6777 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006778
6779 /*
6780 * adjust disk_bytenr and num_bytes to cover just the bytes
6781 * in this extent we are about to write. If there
6782 * are any csums in that range we have to cow in order
6783 * to keep the csums correct
6784 */
6785 disk_bytenr += backref_offset;
6786 disk_bytenr += offset - key.offset;
Chris Mason46bfbb52010-05-26 11:04:10 -04006787 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
6788 goto out;
6789 /*
6790 * all of the above have passed, it is safe to overwrite this extent
6791 * without cow
6792 */
Josef Bacikeb384b52013-04-24 16:32:55 -04006793 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04006794 ret = 1;
6795out:
6796 btrfs_free_path(path);
6797 return ret;
6798}
6799
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07006800bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
6801{
6802 struct radix_tree_root *root = &inode->i_mapping->page_tree;
6803 int found = false;
6804 void **pagep = NULL;
6805 struct page *page = NULL;
6806 int start_idx;
6807 int end_idx;
6808
6809 start_idx = start >> PAGE_CACHE_SHIFT;
6810
6811 /*
6812 * end is the last byte in the last page. end == start is legal
6813 */
6814 end_idx = end >> PAGE_CACHE_SHIFT;
6815
6816 rcu_read_lock();
6817
6818 /* Most of the code in this while loop is lifted from
6819 * find_get_page. It's been modified to begin searching from a
6820 * page and return just the first page found in that range. If the
6821 * found idx is less than or equal to the end idx then we know that
6822 * a page exists. If no pages are found or if those pages are
6823 * outside of the range then we're fine (yay!) */
6824 while (page == NULL &&
6825 radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
6826 page = radix_tree_deref_slot(pagep);
6827 if (unlikely(!page))
6828 break;
6829
6830 if (radix_tree_exception(page)) {
Filipe Manana809f9012014-06-05 13:22:25 +01006831 if (radix_tree_deref_retry(page)) {
6832 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07006833 continue;
Filipe Manana809f9012014-06-05 13:22:25 +01006834 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07006835 /*
6836 * Otherwise, shmem/tmpfs must be storing a swap entry
6837 * here as an exceptional entry: so return it without
6838 * attempting to raise page count.
6839 */
Filipe Manana6fdef6d2014-06-05 13:22:26 +01006840 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07006841 break; /* TODO: Is this relevant for this use case? */
6842 }
6843
Filipe Manana91405152014-06-05 13:22:24 +01006844 if (!page_cache_get_speculative(page)) {
6845 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07006846 continue;
Filipe Manana91405152014-06-05 13:22:24 +01006847 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07006848
6849 /*
6850 * Has the page moved?
6851 * This is part of the lockless pagecache protocol. See
6852 * include/linux/pagemap.h for details.
6853 */
6854 if (unlikely(page != *pagep)) {
6855 page_cache_release(page);
6856 page = NULL;
6857 }
6858 }
6859
6860 if (page) {
6861 if (page->index <= end_idx)
6862 found = true;
6863 page_cache_release(page);
6864 }
6865
6866 rcu_read_unlock();
6867 return found;
6868}
6869
Josef Bacikeb838e72012-07-31 16:28:48 -04006870static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
6871 struct extent_state **cached_state, int writing)
6872{
6873 struct btrfs_ordered_extent *ordered;
6874 int ret = 0;
6875
6876 while (1) {
6877 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6878 0, cached_state);
6879 /*
6880 * We're concerned with the entire range that we're going to be
6881 * doing DIO to, so we need to make sure theres no ordered
6882 * extents in this range.
6883 */
6884 ordered = btrfs_lookup_ordered_range(inode, lockstart,
6885 lockend - lockstart + 1);
6886
6887 /*
6888 * We need to make sure there are no buffered pages in this
6889 * range either, we could have raced between the invalidate in
6890 * generic_file_direct_write and locking the extent. The
6891 * invalidate needs to happen so that reads after a write do not
6892 * get stale data.
6893 */
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07006894 if (!ordered &&
6895 (!writing ||
6896 !btrfs_page_exists_in_range(inode, lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04006897 break;
6898
6899 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6900 cached_state, GFP_NOFS);
6901
6902 if (ordered) {
6903 btrfs_start_ordered_extent(inode, ordered, 1);
6904 btrfs_put_ordered_extent(ordered);
6905 } else {
6906 /* Screw you mmap */
6907 ret = filemap_write_and_wait_range(inode->i_mapping,
6908 lockstart,
6909 lockend);
6910 if (ret)
6911 break;
6912
6913 /*
6914 * If we found a page that couldn't be invalidated just
6915 * fall back to buffered.
6916 */
6917 ret = invalidate_inode_pages2_range(inode->i_mapping,
6918 lockstart >> PAGE_CACHE_SHIFT,
6919 lockend >> PAGE_CACHE_SHIFT);
6920 if (ret)
6921 break;
6922 }
6923
6924 cond_resched();
6925 }
6926
6927 return ret;
6928}
6929
Josef Bacik69ffb542012-09-11 15:40:07 -04006930static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
6931 u64 len, u64 orig_start,
6932 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006933 u64 orig_block_len, u64 ram_bytes,
6934 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04006935{
6936 struct extent_map_tree *em_tree;
6937 struct extent_map *em;
6938 struct btrfs_root *root = BTRFS_I(inode)->root;
6939 int ret;
6940
6941 em_tree = &BTRFS_I(inode)->extent_tree;
6942 em = alloc_extent_map();
6943 if (!em)
6944 return ERR_PTR(-ENOMEM);
6945
6946 em->start = start;
6947 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04006948 em->mod_start = start;
6949 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04006950 em->len = len;
6951 em->block_len = block_len;
6952 em->block_start = block_start;
6953 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05006954 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04006955 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04006956 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04006957 set_bit(EXTENT_FLAG_PINNED, &em->flags);
6958 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05006959 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04006960
6961 do {
6962 btrfs_drop_extent_cache(inode, em->start,
6963 em->start + em->len - 1, 0);
6964 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006965 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04006966 write_unlock(&em_tree->lock);
6967 } while (ret == -EEXIST);
6968
6969 if (ret) {
6970 free_extent_map(em);
6971 return ERR_PTR(ret);
6972 }
6973
6974 return em;
6975}
6976
6977
Josef Bacik4b46fce2010-05-23 11:00:55 -04006978static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
6979 struct buffer_head *bh_result, int create)
6980{
6981 struct extent_map *em;
6982 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04006983 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006984 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04006985 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006986 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04006987 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00006988 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006989
Miao Xie172a5042013-02-21 02:48:22 -07006990 if (create)
Josef Bacikeb838e72012-07-31 16:28:48 -04006991 unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07006992 else
Josef Bacikc3298612012-08-03 16:49:19 -04006993 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04006994
Josef Bacikc3298612012-08-03 16:49:19 -04006995 lockstart = start;
6996 lockend = start + len - 1;
6997
Josef Bacikeb838e72012-07-31 16:28:48 -04006998 /*
6999 * If this errors out it's because we couldn't invalidate pagecache for
7000 * this range and we need to fallback to buffered.
7001 */
7002 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
7003 return -ENOTBLK;
7004
Josef Bacik4b46fce2010-05-23 11:00:55 -04007005 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007006 if (IS_ERR(em)) {
7007 ret = PTR_ERR(em);
7008 goto unlock_err;
7009 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007010
7011 /*
7012 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7013 * io. INLINE is special, and we could probably kludge it in here, but
7014 * it's still buffered so for safety lets just fall back to the generic
7015 * buffered path.
7016 *
7017 * For COMPRESSED we _have_ to read the entire extent in so we can
7018 * decompress it, so there will be buffering required no matter what we
7019 * do, so go ahead and fallback to buffered.
7020 *
7021 * We return -ENOTBLK because thats what makes DIO go ahead and go back
7022 * to buffered IO. Don't blame me, this is the price we pay for using
7023 * the generic code.
7024 */
7025 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7026 em->block_start == EXTENT_MAP_INLINE) {
7027 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007028 ret = -ENOTBLK;
7029 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007030 }
7031
7032 /* Just a good old fashioned hole, return */
7033 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7034 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7035 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007036 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007037 }
7038
7039 /*
7040 * We don't allocate a new extent in the following cases
7041 *
7042 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7043 * existing extent.
7044 * 2) The extent is marked as PREALLOC. We're good to go here and can
7045 * just use the extent.
7046 *
7047 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007048 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007049 len = min(len, em->len - (start - em->start));
7050 lockstart = start + len;
7051 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007052 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007053
7054 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7055 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7056 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007057 int type;
7058 int ret;
Josef Bacikeb384b52013-04-24 16:32:55 -04007059 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007060
7061 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7062 type = BTRFS_ORDERED_PREALLOC;
7063 else
7064 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007065 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007066 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007067
Josef Bacik00361582013-08-14 14:02:47 -04007068 if (can_nocow_extent(inode, start, &len, &orig_start,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007069 &orig_block_len, &ram_bytes) == 1) {
Josef Bacik69ffb542012-09-11 15:40:07 -04007070 if (type == BTRFS_ORDERED_PREALLOC) {
7071 free_extent_map(em);
7072 em = create_pinned_em(inode, start, len,
7073 orig_start,
Josef Bacikb4939682012-12-03 10:31:19 -05007074 block_start, len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04007075 orig_block_len,
7076 ram_bytes, type);
Josef Bacik00361582013-08-14 14:02:47 -04007077 if (IS_ERR(em))
Josef Bacik69ffb542012-09-11 15:40:07 -04007078 goto unlock_err;
Josef Bacik69ffb542012-09-11 15:40:07 -04007079 }
7080
Chris Mason46bfbb52010-05-26 11:04:10 -04007081 ret = btrfs_add_ordered_extent_dio(inode, start,
7082 block_start, len, len, type);
Chris Mason46bfbb52010-05-26 11:04:10 -04007083 if (ret) {
7084 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007085 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007086 }
7087 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007088 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007089 }
Josef Bacik00361582013-08-14 14:02:47 -04007090
Chris Mason46bfbb52010-05-26 11:04:10 -04007091 /*
7092 * this will cow the extent, reset the len in case we changed
7093 * it above
7094 */
7095 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007096 free_extent_map(em);
7097 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007098 if (IS_ERR(em)) {
7099 ret = PTR_ERR(em);
7100 goto unlock_err;
7101 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007102 len = min(len, em->len - (start - em->start));
7103unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007104 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7105 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007106 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007107 bh_result->b_bdev = em->bdev;
7108 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007109 if (create) {
7110 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7111 set_buffer_new(bh_result);
7112
7113 /*
7114 * Need to update the i_size under the extent lock so buffered
7115 * readers will get the updated i_size when we unlock.
7116 */
7117 if (start + len > i_size_read(inode))
7118 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007119
Miao Xie172a5042013-02-21 02:48:22 -07007120 spin_lock(&BTRFS_I(inode)->lock);
7121 BTRFS_I(inode)->outstanding_extents++;
7122 spin_unlock(&BTRFS_I(inode)->lock);
7123
Miao Xie09348562013-02-07 10:12:07 +00007124 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7125 lockstart + len - 1, EXTENT_DELALLOC, NULL,
7126 &cached_state, GFP_NOFS);
7127 BUG_ON(ret);
Josef Bacikc3473e82012-06-19 10:59:00 -04007128 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007129
Josef Bacikeb838e72012-07-31 16:28:48 -04007130 /*
7131 * In the case of write we need to clear and unlock the entire range,
7132 * in the case of read we need to unlock only the end area that we
7133 * aren't using if there is any left over space.
7134 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007135 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007136 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7137 lockend, unlock_bits, 1, 0,
7138 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06007139 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007140 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007141 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007142
Josef Bacik4b46fce2010-05-23 11:00:55 -04007143 free_extent_map(em);
7144
7145 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007146
7147unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007148 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7149 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
7150 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007151}
7152
Josef Bacik4b46fce2010-05-23 11:00:55 -04007153static void btrfs_endio_direct_read(struct bio *bio, int err)
7154{
Miao Xiee65e1532010-11-22 03:04:43 +00007155 struct btrfs_dio_private *dip = bio->bi_private;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007156 struct bio_vec *bvec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007157 struct inode *inode = dip->inode;
7158 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason9be33952013-05-17 18:30:14 -04007159 struct bio *dio_bio;
Miao Xiefacc8a222013-07-25 19:22:34 +08007160 u32 *csums = (u32 *)dip->csum;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007161 u64 start;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007162 int i;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007163
7164 start = dip->logical_offset;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007165 bio_for_each_segment_all(bvec, bio, i) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007166 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
7167 struct page *page = bvec->bv_page;
7168 char *kaddr;
7169 u32 csum = ~(u32)0;
7170 unsigned long flags;
7171
7172 local_irq_save(flags);
Cong Wang7ac687d2011-11-25 23:14:28 +08007173 kaddr = kmap_atomic(page);
Liu Bob0496682013-03-14 14:57:45 +00007174 csum = btrfs_csum_data(kaddr + bvec->bv_offset,
Josef Bacik4b46fce2010-05-23 11:00:55 -04007175 csum, bvec->bv_len);
7176 btrfs_csum_final(csum, (char *)&csum);
Cong Wang7ac687d2011-11-25 23:14:28 +08007177 kunmap_atomic(kaddr);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007178 local_irq_restore(flags);
7179
7180 flush_dcache_page(bvec->bv_page);
Kent Overstreet2c30c712013-11-07 12:20:26 -08007181 if (csum != csums[i]) {
Miao Xiefacc8a222013-07-25 19:22:34 +08007182 btrfs_err(root->fs_info, "csum failed ino %llu off %llu csum %u expected csum %u",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02007183 btrfs_ino(inode), start, csum,
Kent Overstreet2c30c712013-11-07 12:20:26 -08007184 csums[i]);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007185 err = -EIO;
7186 }
7187 }
7188
7189 start += bvec->bv_len;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007190 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007191
7192 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007193 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04007194 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007195
Josef Bacik4b46fce2010-05-23 11:00:55 -04007196 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04007197
7198 /* If we had a csum failure make sure to clear the uptodate flag */
7199 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04007200 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
7201 dio_end_io(dio_bio, err);
7202 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007203}
7204
7205static void btrfs_endio_direct_write(struct bio *bio, int err)
7206{
7207 struct btrfs_dio_private *dip = bio->bi_private;
7208 struct inode *inode = dip->inode;
7209 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007210 struct btrfs_ordered_extent *ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05007211 u64 ordered_offset = dip->logical_offset;
7212 u64 ordered_bytes = dip->bytes;
Chris Mason9be33952013-05-17 18:30:14 -04007213 struct bio *dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007214 int ret;
7215
7216 if (err)
7217 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05007218again:
7219 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
7220 &ordered_offset,
Josef Bacik5fd02042012-05-02 14:00:54 -04007221 ordered_bytes, !err);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007222 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05007223 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007224
Liu Bo9e0af232014-08-15 23:36:53 +08007225 btrfs_init_work(&ordered->work, btrfs_endio_write_helper,
7226 finish_ordered_fn, NULL, NULL);
Qu Wenruofccb5d82014-02-28 10:46:10 +08007227 btrfs_queue_work(root->fs_info->endio_write_workers,
7228 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05007229out_test:
7230 /*
7231 * our bio might span multiple ordered extents. If we haven't
7232 * completed the accounting for the whole dio, go back and try again
7233 */
7234 if (ordered_offset < dip->logical_offset + dip->bytes) {
7235 ordered_bytes = dip->logical_offset + dip->bytes -
7236 ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04007237 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05007238 goto again;
7239 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007240out_done:
Chris Mason9be33952013-05-17 18:30:14 -04007241 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007242
Josef Bacik4b46fce2010-05-23 11:00:55 -04007243 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04007244
7245 /* If we had an error make sure to clear the uptodate flag */
7246 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04007247 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
7248 dio_end_io(dio_bio, err);
7249 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007250}
7251
Chris Masoneaf25d92010-05-25 09:48:28 -04007252static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
7253 struct bio *bio, int mirror_num,
7254 unsigned long bio_flags, u64 offset)
7255{
7256 int ret;
7257 struct btrfs_root *root = BTRFS_I(inode)->root;
7258 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007259 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04007260 return 0;
7261}
7262
Miao Xiee65e1532010-11-22 03:04:43 +00007263static void btrfs_end_dio_bio(struct bio *bio, int err)
7264{
7265 struct btrfs_dio_private *dip = bio->bi_private;
7266
7267 if (err) {
Frank Holtonefe120a2013-12-20 11:37:06 -05007268 btrfs_err(BTRFS_I(dip->inode)->root->fs_info,
7269 "direct IO failed ino %llu rw %lu sector %#Lx len %u err no %d",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02007270 btrfs_ino(dip->inode), bio->bi_rw,
Kent Overstreet4f024f32013-10-11 15:44:27 -07007271 (unsigned long long)bio->bi_iter.bi_sector,
7272 bio->bi_iter.bi_size, err);
Miao Xiee65e1532010-11-22 03:04:43 +00007273 dip->errors = 1;
7274
7275 /*
7276 * before atomic variable goto zero, we must make sure
7277 * dip->errors is perceived to be set.
7278 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01007279 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00007280 }
7281
7282 /* if there are more bios still pending for this dio, just exit */
7283 if (!atomic_dec_and_test(&dip->pending_bios))
7284 goto out;
7285
Chris Mason9be33952013-05-17 18:30:14 -04007286 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00007287 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04007288 } else {
7289 set_bit(BIO_UPTODATE, &dip->dio_bio->bi_flags);
Miao Xiee65e1532010-11-22 03:04:43 +00007290 bio_endio(dip->orig_bio, 0);
7291 }
7292out:
7293 bio_put(bio);
7294}
7295
7296static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
7297 u64 first_sector, gfp_t gfp_flags)
7298{
7299 int nr_vecs = bio_get_nr_vecs(bdev);
7300 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
7301}
7302
7303static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
7304 int rw, u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007305 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00007306{
Miao Xiefacc8a222013-07-25 19:22:34 +08007307 struct btrfs_dio_private *dip = bio->bi_private;
Miao Xiee65e1532010-11-22 03:04:43 +00007308 int write = rw & REQ_WRITE;
7309 struct btrfs_root *root = BTRFS_I(inode)->root;
7310 int ret;
7311
Josef Bacikb812ce22012-11-16 13:56:32 -05007312 if (async_submit)
7313 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
7314
Miao Xiee65e1532010-11-22 03:04:43 +00007315 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04007316
7317 if (!write) {
7318 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
7319 if (ret)
7320 goto err;
7321 }
Miao Xiee65e1532010-11-22 03:04:43 +00007322
Josef Bacik1ae39932011-04-06 14:41:34 -04007323 if (skip_sum)
7324 goto map;
7325
7326 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00007327 ret = btrfs_wq_submit_bio(root->fs_info,
7328 inode, rw, bio, 0, 0,
7329 file_offset,
7330 __btrfs_submit_bio_start_direct_io,
7331 __btrfs_submit_bio_done);
7332 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04007333 } else if (write) {
7334 /*
7335 * If we aren't doing async submit, calculate the csum of the
7336 * bio now.
7337 */
7338 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
7339 if (ret)
7340 goto err;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007341 } else if (!skip_sum) {
Miao Xiefacc8a222013-07-25 19:22:34 +08007342 ret = btrfs_lookup_bio_sums_dio(root, inode, dip, bio,
7343 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007344 if (ret)
7345 goto err;
7346 }
Miao Xiee65e1532010-11-22 03:04:43 +00007347
Josef Bacik1ae39932011-04-06 14:41:34 -04007348map:
7349 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007350err:
7351 bio_put(bio);
7352 return ret;
7353}
7354
7355static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
7356 int skip_sum)
7357{
7358 struct inode *inode = dip->inode;
7359 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00007360 struct bio *bio;
7361 struct bio *orig_bio = dip->orig_bio;
7362 struct bio_vec *bvec = orig_bio->bi_io_vec;
Kent Overstreet4f024f32013-10-11 15:44:27 -07007363 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00007364 u64 file_offset = dip->logical_offset;
7365 u64 submit_len = 0;
7366 u64 map_length;
7367 int nr_pages = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007368 int ret = 0;
Josef Bacik1ae39932011-04-06 14:41:34 -04007369 int async_submit = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007370
Kent Overstreet4f024f32013-10-11 15:44:27 -07007371 map_length = orig_bio->bi_iter.bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007372 ret = btrfs_map_block(root->fs_info, rw, start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007373 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08007374 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00007375 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08007376
Kent Overstreet4f024f32013-10-11 15:44:27 -07007377 if (map_length >= orig_bio->bi_iter.bi_size) {
Josef Bacik02f57c72011-04-06 14:25:44 -04007378 bio = orig_bio;
7379 goto submit;
7380 }
7381
David Woodhouse53b381b2013-01-29 18:40:14 -05007382 /* async crcs make it difficult to collect full stripe writes. */
7383 if (btrfs_get_alloc_profile(root, 1) &
7384 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6))
7385 async_submit = 0;
7386 else
7387 async_submit = 1;
7388
Josef Bacik02f57c72011-04-06 14:25:44 -04007389 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
7390 if (!bio)
7391 return -ENOMEM;
Miao Xie7a5c3c92014-06-17 18:58:59 +08007392
Josef Bacik02f57c72011-04-06 14:25:44 -04007393 bio->bi_private = dip;
7394 bio->bi_end_io = btrfs_end_dio_bio;
7395 atomic_inc(&dip->pending_bios);
7396
Miao Xiee65e1532010-11-22 03:04:43 +00007397 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
7398 if (unlikely(map_length < submit_len + bvec->bv_len ||
7399 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
7400 bvec->bv_offset) < bvec->bv_len)) {
7401 /*
7402 * inc the count before we submit the bio so
7403 * we know the end IO handler won't happen before
7404 * we inc the count. Otherwise, the dip might get freed
7405 * before we're done setting it up
7406 */
7407 atomic_inc(&dip->pending_bios);
7408 ret = __btrfs_submit_dio_bio(bio, inode, rw,
7409 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007410 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007411 if (ret) {
7412 bio_put(bio);
7413 atomic_dec(&dip->pending_bios);
7414 goto out_err;
7415 }
7416
Miao Xiee65e1532010-11-22 03:04:43 +00007417 start_sector += submit_len >> 9;
7418 file_offset += submit_len;
7419
7420 submit_len = 0;
7421 nr_pages = 0;
7422
7423 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
7424 start_sector, GFP_NOFS);
7425 if (!bio)
7426 goto out_err;
7427 bio->bi_private = dip;
7428 bio->bi_end_io = btrfs_end_dio_bio;
7429
Kent Overstreet4f024f32013-10-11 15:44:27 -07007430 map_length = orig_bio->bi_iter.bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007431 ret = btrfs_map_block(root->fs_info, rw,
Stefan Behrens3ec706c2012-11-05 15:46:42 +01007432 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007433 &map_length, NULL, 0);
7434 if (ret) {
7435 bio_put(bio);
7436 goto out_err;
7437 }
7438 } else {
7439 submit_len += bvec->bv_len;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307440 nr_pages++;
Miao Xiee65e1532010-11-22 03:04:43 +00007441 bvec++;
7442 }
7443 }
7444
Josef Bacik02f57c72011-04-06 14:25:44 -04007445submit:
Miao Xiee65e1532010-11-22 03:04:43 +00007446 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007447 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007448 if (!ret)
7449 return 0;
7450
7451 bio_put(bio);
7452out_err:
7453 dip->errors = 1;
7454 /*
7455 * before atomic variable goto zero, we must
7456 * make sure dip->errors is perceived to be set.
7457 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01007458 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00007459 if (atomic_dec_and_test(&dip->pending_bios))
7460 bio_io_error(dip->orig_bio);
7461
7462 /* bio_end_io() will handle error, so we needn't return it */
7463 return 0;
7464}
7465
Chris Mason9be33952013-05-17 18:30:14 -04007466static void btrfs_submit_direct(int rw, struct bio *dio_bio,
7467 struct inode *inode, loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007468{
7469 struct btrfs_root *root = BTRFS_I(inode)->root;
7470 struct btrfs_dio_private *dip;
Chris Mason9be33952013-05-17 18:30:14 -04007471 struct bio *io_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007472 int skip_sum;
Miao Xiefacc8a222013-07-25 19:22:34 +08007473 int sum_len;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02007474 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007475 int ret = 0;
Miao Xiefacc8a222013-07-25 19:22:34 +08007476 u16 csum_size;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007477
7478 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
7479
Chris Mason9be33952013-05-17 18:30:14 -04007480 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04007481 if (!io_bio) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007482 ret = -ENOMEM;
7483 goto free_ordered;
7484 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007485
Miao Xiefacc8a222013-07-25 19:22:34 +08007486 if (!skip_sum && !write) {
7487 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
Kent Overstreet4f024f32013-10-11 15:44:27 -07007488 sum_len = dio_bio->bi_iter.bi_size >>
7489 inode->i_sb->s_blocksize_bits;
Miao Xiefacc8a222013-07-25 19:22:34 +08007490 sum_len *= csum_size;
7491 } else {
7492 sum_len = 0;
7493 }
7494
7495 dip = kmalloc(sizeof(*dip) + sum_len, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04007496 if (!dip) {
7497 ret = -ENOMEM;
7498 goto free_io_bio;
7499 }
7500
7501 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007502 dip->inode = inode;
7503 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07007504 dip->bytes = dio_bio->bi_iter.bi_size;
7505 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Chris Mason9be33952013-05-17 18:30:14 -04007506 io_bio->bi_private = dip;
Miao Xiee65e1532010-11-22 03:04:43 +00007507 dip->errors = 0;
Chris Mason9be33952013-05-17 18:30:14 -04007508 dip->orig_bio = io_bio;
7509 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00007510 atomic_set(&dip->pending_bios, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007511
7512 if (write)
Chris Mason9be33952013-05-17 18:30:14 -04007513 io_bio->bi_end_io = btrfs_endio_direct_write;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007514 else
Chris Mason9be33952013-05-17 18:30:14 -04007515 io_bio->bi_end_io = btrfs_endio_direct_read;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007516
Miao Xiee65e1532010-11-22 03:04:43 +00007517 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
7518 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04007519 return;
Chris Mason9be33952013-05-17 18:30:14 -04007520
7521free_io_bio:
7522 bio_put(io_bio);
7523
Josef Bacik4b46fce2010-05-23 11:00:55 -04007524free_ordered:
7525 /*
7526 * If this is a write, we need to clean up the reserved space and kill
7527 * the ordered extent.
7528 */
7529 if (write) {
7530 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05007531 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007532 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
7533 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
7534 btrfs_free_reserved_extent(root, ordered->start,
Miao Xiee570fd22014-06-19 10:42:50 +08007535 ordered->disk_len, 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007536 btrfs_put_ordered_extent(ordered);
7537 btrfs_put_ordered_extent(ordered);
7538 }
Chris Mason9be33952013-05-17 18:30:14 -04007539 bio_endio(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007540}
7541
Chris Mason5a5f79b2010-05-26 21:33:37 -04007542static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
Al Viro28060d52014-03-22 05:15:17 -04007543 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04007544{
7545 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00007546 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007547 unsigned blocksize_mask = root->sectorsize - 1;
7548 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007549
7550 if (offset & blocksize_mask)
7551 goto out;
7552
Al Viro28060d52014-03-22 05:15:17 -04007553 if (iov_iter_alignment(iter) & blocksize_mask)
7554 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00007555
Al Viro28060d52014-03-22 05:15:17 -04007556 /* If this is a write we don't need to check anymore */
7557 if (rw & WRITE)
7558 return 0;
7559 /*
7560 * Check to make sure we don't have duplicate iov_base's in this
7561 * iovec, if so return EINVAL, otherwise we'll get csum errors
7562 * when reading back.
7563 */
7564 for (seg = 0; seg < iter->nr_segs; seg++) {
7565 for (i = seg + 1; i < iter->nr_segs; i++) {
7566 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00007567 goto out;
7568 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04007569 }
7570 retval = 0;
7571out:
7572 return retval;
7573}
Josef Bacikeb838e72012-07-31 16:28:48 -04007574
Chris Mason16432982008-04-10 10:23:21 -04007575static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
Al Virod8d3d942014-03-04 21:27:34 -05007576 struct iov_iter *iter, loff_t offset)
Chris Mason16432982008-04-10 10:23:21 -04007577{
Josef Bacik4b46fce2010-05-23 11:00:55 -04007578 struct file *file = iocb->ki_filp;
7579 struct inode *inode = file->f_mapping->host;
Miao Xie09348562013-02-07 10:12:07 +00007580 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00007581 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00007582 bool wakeup = true;
7583 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00007584 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007585
Al Viro28060d52014-03-22 05:15:17 -04007586 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007587 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007588
Miao Xie38851cc2013-02-08 07:04:11 +00007589 atomic_inc(&inode->i_dio_count);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01007590 smp_mb__after_atomic();
Miao Xie38851cc2013-02-08 07:04:11 +00007591
Josef Bacik0e267c42013-07-02 10:38:02 -04007592 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08007593 * The generic stuff only does filemap_write_and_wait_range, which
7594 * isn't enough if we've written compressed pages to this area, so
7595 * we need to flush the dirty pages again to make absolutely sure
7596 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04007597 */
Al Viroa6cbcd42014-03-04 22:38:00 -05007598 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08007599 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
7600 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08007601 filemap_fdatawrite_range(inode->i_mapping, offset,
7602 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04007603
Miao Xie09348562013-02-07 10:12:07 +00007604 if (rw & WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00007605 /*
7606 * If the write DIO is beyond the EOF, we need update
7607 * the isize, but it is protected by i_mutex. So we can
7608 * not unlock the i_mutex at this case.
7609 */
7610 if (offset + count <= inode->i_size) {
7611 mutex_unlock(&inode->i_mutex);
7612 relock = true;
7613 }
Miao Xie09348562013-02-07 10:12:07 +00007614 ret = btrfs_delalloc_reserve_space(inode, count);
7615 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00007616 goto out;
7617 } else if (unlikely(test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
7618 &BTRFS_I(inode)->runtime_flags))) {
7619 inode_dio_done(inode);
7620 flags = DIO_LOCKING | DIO_SKIP_HOLES;
7621 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00007622 }
7623
7624 ret = __blockdev_direct_IO(rw, iocb, inode,
7625 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
Al Viro31b14032014-03-05 01:33:16 -05007626 iter, offset, btrfs_get_blocks_direct, NULL,
Miao Xie2e60a512013-02-08 07:01:08 +00007627 btrfs_submit_direct, flags);
Miao Xie09348562013-02-07 10:12:07 +00007628 if (rw & WRITE) {
7629 if (ret < 0 && ret != -EIOCBQUEUED)
7630 btrfs_delalloc_release_space(inode, count);
Miao Xie172a5042013-02-21 02:48:22 -07007631 else if (ret >= 0 && (size_t)ret < count)
Miao Xie09348562013-02-07 10:12:07 +00007632 btrfs_delalloc_release_space(inode,
7633 count - (size_t)ret);
Miao Xie172a5042013-02-21 02:48:22 -07007634 else
7635 btrfs_delalloc_release_metadata(inode, 0);
Miao Xie09348562013-02-07 10:12:07 +00007636 }
Miao Xie38851cc2013-02-08 07:04:11 +00007637out:
Miao Xie2e60a512013-02-08 07:01:08 +00007638 if (wakeup)
7639 inode_dio_done(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00007640 if (relock)
7641 mutex_lock(&inode->i_mutex);
Miao Xie09348562013-02-07 10:12:07 +00007642
7643 return ret;
Chris Mason16432982008-04-10 10:23:21 -04007644}
7645
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007646#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
7647
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007648static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
7649 __u64 start, __u64 len)
7650{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007651 int ret;
7652
7653 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
7654 if (ret)
7655 return ret;
7656
Chris Masonec29ed52011-02-23 16:23:20 -05007657 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007658}
7659
Chris Mason9ebefb182007-06-15 13:50:00 -04007660int btrfs_readpage(struct file *file, struct page *page)
7661{
Chris Masond1310b22008-01-24 16:13:08 -05007662 struct extent_io_tree *tree;
7663 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02007664 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04007665}
Chris Mason1832a6d2007-12-21 16:27:21 -05007666
Chris Mason39279cc2007-06-12 06:35:45 -04007667static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
7668{
Chris Masond1310b22008-01-24 16:13:08 -05007669 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04007670
7671
7672 if (current->flags & PF_MEMALLOC) {
7673 redirty_page_for_writepage(wbc, page);
7674 unlock_page(page);
7675 return 0;
7676 }
Chris Masond1310b22008-01-24 16:13:08 -05007677 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04007678 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
7679}
Chris Mason39279cc2007-06-12 06:35:45 -04007680
Eric Sandeen48a3b632013-04-25 20:41:01 +00007681static int btrfs_writepages(struct address_space *mapping,
7682 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04007683{
Chris Masond1310b22008-01-24 16:13:08 -05007684 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05007685
Chris Masond1310b22008-01-24 16:13:08 -05007686 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04007687 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
7688}
7689
Chris Mason3ab2fb52007-11-08 10:59:22 -05007690static int
7691btrfs_readpages(struct file *file, struct address_space *mapping,
7692 struct list_head *pages, unsigned nr_pages)
7693{
Chris Masond1310b22008-01-24 16:13:08 -05007694 struct extent_io_tree *tree;
7695 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05007696 return extent_readpages(tree, mapping, pages, nr_pages,
7697 btrfs_get_extent);
7698}
Chris Masone6dcd2d2008-07-17 12:53:50 -04007699static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04007700{
Chris Masond1310b22008-01-24 16:13:08 -05007701 struct extent_io_tree *tree;
7702 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04007703 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04007704
Chris Masond1310b22008-01-24 16:13:08 -05007705 tree = &BTRFS_I(page->mapping->host)->io_tree;
7706 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05007707 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04007708 if (ret == 1) {
7709 ClearPagePrivate(page);
7710 set_page_private(page, 0);
7711 page_cache_release(page);
7712 }
7713 return ret;
7714}
Chris Mason39279cc2007-06-12 06:35:45 -04007715
Chris Masone6dcd2d2008-07-17 12:53:50 -04007716static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
7717{
Chris Mason98509cf2008-09-11 15:51:43 -04007718 if (PageWriteback(page) || PageDirty(page))
7719 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05007720 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007721}
7722
Lukas Czernerd47992f2013-05-21 23:17:23 -04007723static void btrfs_invalidatepage(struct page *page, unsigned int offset,
7724 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04007725{
Josef Bacik5fd02042012-05-02 14:00:54 -04007726 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05007727 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007728 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007729 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007730 u64 page_start = page_offset(page);
7731 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00007732 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04007733
Chris Mason8b62b722009-09-02 16:53:46 -04007734 /*
7735 * we have the page locked, so new writeback can't start,
7736 * and the dirty bit won't be cleared while we are here.
7737 *
7738 * Wait for IO on this page so that we can safely clear
7739 * the PagePrivate2 bit and do ordered accounting
7740 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007741 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04007742
Josef Bacik5fd02042012-05-02 14:00:54 -04007743 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007744 if (offset) {
7745 btrfs_releasepage(page, GFP_NOFS);
7746 return;
7747 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00007748
7749 if (!inode_evicting)
7750 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
7751 ordered = btrfs_lookup_ordered_extent(inode, page_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007752 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04007753 /*
7754 * IO on this page will never be started, so we need
7755 * to account for any ordered extents now
7756 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00007757 if (!inode_evicting)
7758 clear_extent_bit(tree, page_start, page_end,
7759 EXTENT_DIRTY | EXTENT_DELALLOC |
7760 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
7761 EXTENT_DEFRAG, 1, 0, &cached_state,
7762 GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04007763 /*
7764 * whoever cleared the private bit is responsible
7765 * for the finish_ordered_io
7766 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04007767 if (TestClearPagePrivate2(page)) {
7768 struct btrfs_ordered_inode_tree *tree;
7769 u64 new_len;
7770
7771 tree = &BTRFS_I(inode)->ordered_tree;
7772
7773 spin_lock_irq(&tree->lock);
7774 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
7775 new_len = page_start - ordered->file_offset;
7776 if (new_len < ordered->truncated_len)
7777 ordered->truncated_len = new_len;
7778 spin_unlock_irq(&tree->lock);
7779
7780 if (btrfs_dec_test_ordered_pending(inode, &ordered,
7781 page_start,
7782 PAGE_CACHE_SIZE, 1))
7783 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04007784 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007785 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00007786 if (!inode_evicting) {
7787 cached_state = NULL;
7788 lock_extent_bits(tree, page_start, page_end, 0,
7789 &cached_state);
7790 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007791 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00007792
7793 if (!inode_evicting) {
7794 clear_extent_bit(tree, page_start, page_end,
7795 EXTENT_LOCKED | EXTENT_DIRTY |
7796 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
7797 EXTENT_DEFRAG, 1, 1,
7798 &cached_state, GFP_NOFS);
7799
7800 __btrfs_releasepage(page, GFP_NOFS);
7801 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007802
Chris Mason4a096752008-07-21 10:29:44 -04007803 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04007804 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04007805 ClearPagePrivate(page);
7806 set_page_private(page, 0);
7807 page_cache_release(page);
7808 }
Chris Mason39279cc2007-06-12 06:35:45 -04007809}
7810
Chris Mason9ebefb182007-06-15 13:50:00 -04007811/*
7812 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
7813 * called from a page fault handler when a page is first dirtied. Hence we must
7814 * be careful to check for EOF conditions here. We set the page up correctly
7815 * for a written page which means we get ENOSPC checking when writing into
7816 * holes and correct delalloc and unwritten extent mapping on filesystems that
7817 * support these features.
7818 *
7819 * We are not allowed to take the i_mutex here so we have to play games to
7820 * protect against truncate races as the page could now be beyond EOF. Because
7821 * vmtruncate() writes the inode size before removing pages, once we have the
7822 * page lock we can determine safely if the page is beyond EOF. If it is not
7823 * beyond EOF, then the page is guaranteed safe against truncation until we
7824 * unlock the page.
7825 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07007826int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04007827{
Nick Pigginc2ec1752009-03-31 15:23:21 -07007828 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05007829 struct inode *inode = file_inode(vma->vm_file);
Chris Mason1832a6d2007-12-21 16:27:21 -05007830 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007831 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7832 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007833 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007834 char *kaddr;
7835 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04007836 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05007837 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05007838 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007839 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007840 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04007841
Jan Karab2b5ef52012-06-12 16:20:45 +02007842 sb_start_pagefault(inode->i_sb);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04007843 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007844 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04007845 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05007846 reserved = 1;
7847 }
Nick Piggin56a76f82009-03-31 15:23:23 -07007848 if (ret) {
7849 if (ret == -ENOMEM)
7850 ret = VM_FAULT_OOM;
7851 else /* -ENOSPC, -EIO, etc */
7852 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05007853 if (reserved)
7854 goto out;
7855 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07007856 }
Chris Mason1832a6d2007-12-21 16:27:21 -05007857
Nick Piggin56a76f82009-03-31 15:23:23 -07007858 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007859again:
Chris Mason9ebefb182007-06-15 13:50:00 -04007860 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04007861 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007862 page_start = page_offset(page);
7863 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04007864
Chris Mason9ebefb182007-06-15 13:50:00 -04007865 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04007866 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04007867 /* page got truncated out from underneath us */
7868 goto out_unlock;
7869 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007870 wait_on_page_writeback(page);
7871
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007872 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007873 set_page_extent_mapped(page);
7874
Chris Masoneb84ae02008-07-17 13:53:27 -04007875 /*
7876 * we can't set the delalloc bits if there are pending ordered
7877 * extents. Drop our locks and wait for them to finish
7878 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007879 ordered = btrfs_lookup_ordered_extent(inode, page_start);
7880 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007881 unlock_extent_cached(io_tree, page_start, page_end,
7882 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007883 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04007884 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007885 btrfs_put_ordered_extent(ordered);
7886 goto again;
7887 }
7888
Josef Bacikfbf19082009-10-01 17:10:23 -04007889 /*
7890 * XXX - page_mkwrite gets called every time the page is dirtied, even
7891 * if it was already dirty, so for space accounting reasons we need to
7892 * clear any delalloc bits for the range we are fixing to save. There
7893 * is probably a better way to do this, but for now keep consistent with
7894 * prepare_pages in the normal write path.
7895 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00007896 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06007897 EXTENT_DIRTY | EXTENT_DELALLOC |
7898 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00007899 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04007900
Josef Bacik2ac55d42010-02-03 19:33:23 +00007901 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
7902 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007903 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007904 unlock_extent_cached(io_tree, page_start, page_end,
7905 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007906 ret = VM_FAULT_SIGBUS;
7907 goto out_unlock;
7908 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007909 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04007910
7911 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04007912 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04007913 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04007914 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04007915 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04007916
Chris Masone6dcd2d2008-07-17 12:53:50 -04007917 if (zero_start != PAGE_CACHE_SIZE) {
7918 kaddr = kmap(page);
7919 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
7920 flush_dcache_page(page);
7921 kunmap(page);
7922 }
Chris Mason247e7432008-07-17 12:53:51 -04007923 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007924 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04007925 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007926
Chris Mason257c62e2009-10-13 13:21:08 -04007927 BTRFS_I(inode)->last_trans = root->fs_info->generation;
7928 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06007929 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04007930
Josef Bacik2ac55d42010-02-03 19:33:23 +00007931 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04007932
7933out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02007934 if (!ret) {
7935 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04007936 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02007937 }
Chris Mason9ebefb182007-06-15 13:50:00 -04007938 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05007939out:
Josef Bacikec39e182012-01-12 19:10:12 -05007940 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007941out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02007942 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04007943 return ret;
7944}
7945
Josef Bacika41ad392011-01-31 15:30:16 -05007946static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04007947{
7948 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04007949 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04007950 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05007951 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007952 struct btrfs_trans_handle *trans;
Chris Masondbe674a2008-07-17 12:54:05 -04007953 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04007954 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04007955
Josef Bacik0ef8b722013-10-25 16:13:35 -04007956 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
7957 (u64)-1);
7958 if (ret)
7959 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04007960
Josef Bacikfcb80c22011-05-03 10:40:22 -04007961 /*
7962 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
7963 * 3 things going on here
7964 *
7965 * 1) We need to reserve space for our orphan item and the space to
7966 * delete our orphan item. Lord knows we don't want to have a dangling
7967 * orphan item because we didn't reserve space to remove it.
7968 *
7969 * 2) We need to reserve space to update our inode.
7970 *
7971 * 3) We need to have something to cache all the space that is going to
7972 * be free'd up by the truncate operation, but also have some slack
7973 * space reserved in case it uses space during the truncate (thank you
7974 * very much snapshotting).
7975 *
7976 * And we need these to all be seperate. The fact is we can use alot of
7977 * space doing the truncate, and we have no earthly idea how much space
7978 * we will use, so we need the truncate reservation to be seperate so it
7979 * doesn't end up using space reserved for updating the inode or
7980 * removing the orphan item. We also need to be able to stop the
7981 * transaction and start a new one, which means we need to be able to
7982 * update the inode several times, and we have no idea of knowing how
7983 * many times that will be, so we can't just reserve 1 item for the
7984 * entirety of the opration, so that has to be done seperately as well.
7985 * Then there is the orphan item, which does indeed need to be held on
7986 * to for the whole operation, and we need nobody to touch this reserved
7987 * space except the orphan code.
7988 *
7989 * So that leaves us with
7990 *
7991 * 1) root->orphan_block_rsv - for the orphan deletion.
7992 * 2) rsv - for the truncate reservation, which we will steal from the
7993 * transaction reservation.
7994 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
7995 * updating the inode.
7996 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06007997 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007998 if (!rsv)
7999 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04008000 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008001 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05008002
Josef Bacik907cbce2011-08-08 13:46:15 -04008003 /*
Josef Bacik07127182011-08-19 10:29:59 -04008004 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04008005 * 1 for updating the inode.
8006 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05008007 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008008 if (IS_ERR(trans)) {
8009 err = PTR_ERR(trans);
8010 goto out;
8011 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05008012
Josef Bacik907cbce2011-08-08 13:46:15 -04008013 /* Migrate the slack space for the truncate to our reserve */
8014 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
8015 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008016 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05008017
Chris Mason5a3f23d2009-03-31 13:27:11 -04008018 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04008019 * So if we truncate and then write and fsync we normally would just
8020 * write the extents that changed, which is a problem if we need to
8021 * first truncate that entire inode. So set this flag so we write out
8022 * all of the extents in the inode to the sync log so we're completely
8023 * safe.
8024 */
8025 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008026 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008027
Yan, Zheng80825102009-11-12 09:35:36 +00008028 while (1) {
8029 ret = btrfs_truncate_inode_items(trans, root, inode,
8030 inode->i_size,
8031 BTRFS_EXTENT_DATA_KEY);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008032 if (ret != -ENOSPC) {
Josef Bacik3893e332011-01-31 16:03:11 -05008033 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00008034 break;
Josef Bacik3893e332011-01-31 16:03:11 -05008035 }
Chris Mason39279cc2007-06-12 06:35:45 -04008036
Josef Bacikfcb80c22011-05-03 10:40:22 -04008037 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008038 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05008039 if (ret) {
8040 err = ret;
8041 break;
8042 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04008043
Yan, Zheng80825102009-11-12 09:35:36 +00008044 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00008045 btrfs_btree_balance_dirty(root);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008046
8047 trans = btrfs_start_transaction(root, 2);
8048 if (IS_ERR(trans)) {
8049 ret = err = PTR_ERR(trans);
8050 trans = NULL;
8051 break;
8052 }
8053
8054 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
8055 rsv, min_size);
8056 BUG_ON(ret); /* shouldn't happen */
8057 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008058 }
8059
8060 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04008061 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008062 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05008063 if (ret)
8064 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00008065 }
8066
Chris Mason917c16b2011-11-08 14:49:59 -05008067 if (trans) {
8068 trans->block_rsv = &root->fs_info->trans_block_rsv;
8069 ret = btrfs_update_inode(trans, root, inode);
8070 if (ret && !err)
8071 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04008072
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008073 ret = btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00008074 btrfs_btree_balance_dirty(root);
Chris Mason917c16b2011-11-08 14:49:59 -05008075 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04008076
8077out:
8078 btrfs_free_block_rsv(root, rsv);
8079
Josef Bacik3893e332011-01-31 16:03:11 -05008080 if (ret && !err)
8081 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05008082
Josef Bacik3893e332011-01-31 16:03:11 -05008083 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04008084}
8085
Sven Wegener3b963622008-06-09 21:57:42 -04008086/*
Chris Masond352ac62008-09-29 15:18:18 -04008087 * create a new subvolume directory/inode (helper for the ioctl).
8088 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05008089int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008090 struct btrfs_root *new_root,
8091 struct btrfs_root *parent_root,
8092 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04008093{
Chris Mason39279cc2007-06-12 06:35:45 -04008094 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04008095 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008096 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008097
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01008098 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
8099 new_dirid, new_dirid,
8100 S_IFDIR | (~current_umask() & S_IRWXUGO),
8101 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04008102 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04008103 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008104 inode->i_op = &btrfs_dir_inode_operations;
8105 inode->i_fop = &btrfs_dir_file_operations;
8106
Miklos Szeredibfe86842011-10-28 14:13:29 +02008107 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04008108 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04008109
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008110 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
8111 if (err)
8112 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02008113 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008114 new_root->root_key.objectid, err);
8115
Yan, Zheng76dda932009-09-21 16:00:26 -04008116 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04008117
Yan, Zheng76dda932009-09-21 16:00:26 -04008118 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07008119 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04008120}
8121
Chris Mason39279cc2007-06-12 06:35:45 -04008122struct inode *btrfs_alloc_inode(struct super_block *sb)
8123{
8124 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008125 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008126
8127 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
8128 if (!ei)
8129 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008130
8131 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008132 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04008133 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04008134 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04008135 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008136 ei->delalloc_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008137 ei->disk_i_size = 0;
8138 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04008139 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008140 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08008141 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008142 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06008143 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008144
Josef Bacik9e0baf62011-07-15 15:16:44 +00008145 spin_lock_init(&ei->lock);
8146 ei->outstanding_extents = 0;
8147 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008148
Josef Bacik72ac3c02012-05-23 14:13:11 -04008149 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08008150 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008151
Miao Xie16cdcec2011-04-22 18:12:22 +08008152 ei->delayed_node = NULL;
8153
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008154 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02008155 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02008156 extent_io_tree_init(&ei->io_tree, &inode->i_data);
8157 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04008158 ei->io_tree.track_uptodate = 1;
8159 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05008160 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008161 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05008162 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008163 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008164 INIT_LIST_HEAD(&ei->delalloc_inodes);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008165 RB_CLEAR_NODE(&ei->rb_node);
8166
8167 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008168}
8169
Josef Bacikaaedb552013-10-11 14:44:09 -04008170#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
8171void btrfs_test_destroy_inode(struct inode *inode)
8172{
8173 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
8174 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8175}
8176#endif
8177
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008178static void btrfs_i_callback(struct rcu_head *head)
8179{
8180 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008181 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8182}
8183
Chris Mason39279cc2007-06-12 06:35:45 -04008184void btrfs_destroy_inode(struct inode *inode)
8185{
Chris Masone6dcd2d2008-07-17 12:53:50 -04008186 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008187 struct btrfs_root *root = BTRFS_I(inode)->root;
8188
Al Virob3d9b7a2012-06-09 13:51:19 -04008189 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04008190 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00008191 WARN_ON(BTRFS_I(inode)->outstanding_extents);
8192 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04008193 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
8194 WARN_ON(BTRFS_I(inode)->csum_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04008195
Chris Mason5a3f23d2009-03-31 13:27:11 -04008196 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05008197 * This can happen where we create an inode, but somebody else also
8198 * created the same inode and we need to destroy the one we already
8199 * created.
8200 */
8201 if (!root)
8202 goto free;
8203
Josef Bacik8a35d952012-05-23 14:26:42 -04008204 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
8205 &BTRFS_I(inode)->runtime_flags)) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008206 btrfs_info(root->fs_info, "inode %llu still on the orphan list",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02008207 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04008208 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04008209 }
Josef Bacik7b128762008-07-24 12:17:14 -04008210
Chris Masond3977122009-01-05 21:25:51 -05008211 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008212 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
8213 if (!ordered)
8214 break;
8215 else {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008216 btrfs_err(root->fs_info, "found ordered extent %llu %llu on inode cleanup",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02008217 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008218 btrfs_remove_ordered_extent(inode, ordered);
8219 btrfs_put_ordered_extent(ordered);
8220 btrfs_put_ordered_extent(ordered);
8221 }
8222 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008223 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008224 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05008225free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008226 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04008227}
8228
Al Viro45321ac2010-06-07 13:43:19 -04008229int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04008230{
8231 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04008232
Naohiro Aota6379ef92013-06-06 09:56:34 +00008233 if (root == NULL)
8234 return 1;
8235
Liu Bofa6ac872013-02-20 14:10:23 +00008236 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02008237 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04008238 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04008239 else
Al Viro45321ac2010-06-07 13:43:19 -04008240 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04008241}
8242
Sven Wegener0ee0fda2008-07-30 16:54:26 -04008243static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04008244{
8245 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
8246
8247 inode_init_once(&ei->vfs_inode);
8248}
8249
8250void btrfs_destroy_cachep(void)
8251{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10008252 /*
8253 * Make sure all delayed rcu free inodes are flushed before we
8254 * destroy cache.
8255 */
8256 rcu_barrier();
Chris Mason39279cc2007-06-12 06:35:45 -04008257 if (btrfs_inode_cachep)
8258 kmem_cache_destroy(btrfs_inode_cachep);
8259 if (btrfs_trans_handle_cachep)
8260 kmem_cache_destroy(btrfs_trans_handle_cachep);
8261 if (btrfs_transaction_cachep)
8262 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008263 if (btrfs_path_cachep)
8264 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05008265 if (btrfs_free_space_cachep)
8266 kmem_cache_destroy(btrfs_free_space_cachep);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008267 if (btrfs_delalloc_work_cachep)
8268 kmem_cache_destroy(btrfs_delalloc_work_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008269}
8270
8271int btrfs_init_cachep(void)
8272{
David Sterba837e1972012-09-07 03:00:48 -06008273 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008274 sizeof(struct btrfs_inode), 0,
8275 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04008276 if (!btrfs_inode_cachep)
8277 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008278
David Sterba837e1972012-09-07 03:00:48 -06008279 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008280 sizeof(struct btrfs_trans_handle), 0,
8281 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008282 if (!btrfs_trans_handle_cachep)
8283 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008284
David Sterba837e1972012-09-07 03:00:48 -06008285 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008286 sizeof(struct btrfs_transaction), 0,
8287 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008288 if (!btrfs_transaction_cachep)
8289 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008290
David Sterba837e1972012-09-07 03:00:48 -06008291 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008292 sizeof(struct btrfs_path), 0,
8293 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008294 if (!btrfs_path_cachep)
8295 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008296
David Sterba837e1972012-09-07 03:00:48 -06008297 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05008298 sizeof(struct btrfs_free_space), 0,
8299 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
8300 if (!btrfs_free_space_cachep)
8301 goto fail;
8302
Miao Xie8ccf6f192012-10-25 09:28:04 +00008303 btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
8304 sizeof(struct btrfs_delalloc_work), 0,
8305 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
8306 NULL);
8307 if (!btrfs_delalloc_work_cachep)
8308 goto fail;
8309
Chris Mason39279cc2007-06-12 06:35:45 -04008310 return 0;
8311fail:
8312 btrfs_destroy_cachep();
8313 return -ENOMEM;
8314}
8315
8316static int btrfs_getattr(struct vfsmount *mnt,
8317 struct dentry *dentry, struct kstat *stat)
8318{
Miao Xiedf0af1a2013-01-29 10:11:59 +00008319 u64 delalloc_bytes;
Chris Mason39279cc2007-06-12 06:35:45 -04008320 struct inode *inode = dentry->d_inode;
David Sterbafadc0d82011-11-20 07:33:38 -05008321 u32 blocksize = inode->i_sb->s_blocksize;
8322
Chris Mason39279cc2007-06-12 06:35:45 -04008323 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04008324 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05008325 stat->blksize = PAGE_CACHE_SIZE;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008326
8327 spin_lock(&BTRFS_I(inode)->lock);
8328 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
8329 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05008330 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00008331 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04008332 return 0;
8333}
8334
Chris Masond3977122009-01-05 21:25:51 -05008335static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
8336 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04008337{
8338 struct btrfs_trans_handle *trans;
8339 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008340 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04008341 struct inode *new_inode = new_dentry->d_inode;
8342 struct inode *old_inode = old_dentry->d_inode;
8343 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008344 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008345 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04008346 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08008347 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008348
Li Zefan33345d012011-04-20 10:31:50 +08008349 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04008350 return -EPERM;
8351
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008352 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08008353 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05008354 return -EXDEV;
8355
Li Zefan33345d012011-04-20 10:31:50 +08008356 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
8357 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008358 return -ENOTEMPTY;
8359
Chris Mason39279cc2007-06-12 06:35:45 -04008360 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008361 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04008362 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05008363
8364
8365 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04008366 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05008367 new_dentry->d_name.name,
8368 new_dentry->d_name.len);
8369
8370 if (ret) {
8371 if (ret == -EEXIST) {
8372 /* we shouldn't get
8373 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05308374 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05008375 return ret;
8376 }
8377 } else {
8378 /* maybe -EOVERFLOW */
8379 return ret;
8380 }
8381 }
8382 ret = 0;
8383
Chris Mason5a3f23d2009-03-31 13:27:11 -04008384 /*
Chris Mason8d875f92014-08-12 10:47:42 -07008385 * we're using rename to replace one file with another. Start IO on it
8386 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04008387 */
Chris Mason8d875f92014-08-12 10:47:42 -07008388 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04008389 filemap_flush(old_inode->i_mapping);
8390
Yan, Zheng76dda932009-09-21 16:00:26 -04008391 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08008392 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008393 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008394 /*
8395 * We want to reserve the absolute worst case amount of items. So if
8396 * both inodes are subvols and we need to unlink them then that would
8397 * require 4 item modifications, but if they are both normal inodes it
8398 * would require 5 item modifications, so we'll assume their normal
8399 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
8400 * should cover the worst case number of items we'll modify.
8401 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04008402 trans = btrfs_start_transaction(root, 11);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008403 if (IS_ERR(trans)) {
8404 ret = PTR_ERR(trans);
8405 goto out_notrans;
8406 }
Chris Mason5f39d392007-10-15 16:14:19 -04008407
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008408 if (dest != root)
8409 btrfs_record_root_in_trans(trans, dest);
8410
Yan, Zhenga5719522009-09-24 09:17:31 -04008411 ret = btrfs_set_inode_index(new_dir, &index);
8412 if (ret)
8413 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008414
Miao Xie67de1172013-12-26 13:07:06 +08008415 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08008416 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008417 /* force full log commit if subvolume involved. */
Miao Xie995946d2014-04-02 19:51:06 +08008418 btrfs_set_log_full_commit(root->fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008419 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04008420 ret = btrfs_insert_inode_ref(trans, dest,
8421 new_dentry->d_name.name,
8422 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08008423 old_ino,
8424 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04008425 if (ret)
8426 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008427 /*
8428 * this is an ugly little race, but the rename is required
8429 * to make sure that if we crash, the inode is either at the
8430 * old name or the new one. pinning the log transaction lets
8431 * us make sure we don't allow a log commit to come in after
8432 * we unlink the name but before we add the new name back in.
8433 */
8434 btrfs_pin_log_trans(root);
8435 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04008436
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008437 inode_inc_iversion(old_dir);
8438 inode_inc_iversion(new_dir);
8439 inode_inc_iversion(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008440 old_dir->i_ctime = old_dir->i_mtime = ctime;
8441 new_dir->i_ctime = new_dir->i_mtime = ctime;
8442 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04008443
Chris Mason12fcfd22009-03-24 10:24:20 -04008444 if (old_dentry->d_parent != new_dentry->d_parent)
8445 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
8446
Li Zefan33345d012011-04-20 10:31:50 +08008447 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008448 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
8449 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
8450 old_dentry->d_name.name,
8451 old_dentry->d_name.len);
8452 } else {
Al Viro92986792011-03-04 17:14:37 +00008453 ret = __btrfs_unlink_inode(trans, root, old_dir,
8454 old_dentry->d_inode,
8455 old_dentry->d_name.name,
8456 old_dentry->d_name.len);
8457 if (!ret)
8458 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008459 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008460 if (ret) {
8461 btrfs_abort_transaction(trans, root, ret);
8462 goto out_fail;
8463 }
Chris Mason39279cc2007-06-12 06:35:45 -04008464
8465 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008466 inode_inc_iversion(new_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008467 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08008468 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008469 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
8470 root_objectid = BTRFS_I(new_inode)->location.objectid;
8471 ret = btrfs_unlink_subvol(trans, dest, new_dir,
8472 root_objectid,
8473 new_dentry->d_name.name,
8474 new_dentry->d_name.len);
8475 BUG_ON(new_inode->i_nlink == 0);
8476 } else {
8477 ret = btrfs_unlink_inode(trans, dest, new_dir,
8478 new_dentry->d_inode,
8479 new_dentry->d_name.name,
8480 new_dentry->d_name.len);
8481 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04008482 if (!ret && new_inode->i_nlink == 0)
Chris Masone02119d2008-09-05 16:13:11 -04008483 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008484 if (ret) {
8485 btrfs_abort_transaction(trans, root, ret);
8486 goto out_fail;
8487 }
Chris Mason39279cc2007-06-12 06:35:45 -04008488 }
Josef Bacikaec74772008-07-24 12:12:38 -04008489
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008490 ret = btrfs_add_link(trans, new_dir, old_inode,
8491 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04008492 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008493 if (ret) {
8494 btrfs_abort_transaction(trans, root, ret);
8495 goto out_fail;
8496 }
Chris Mason39279cc2007-06-12 06:35:45 -04008497
Miao Xie67de1172013-12-26 13:07:06 +08008498 if (old_inode->i_nlink == 1)
8499 BTRFS_I(old_inode)->dir_index = index;
8500
Li Zefan33345d012011-04-20 10:31:50 +08008501 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04008502 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00008503 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008504 btrfs_end_log_trans(root);
8505 }
Chris Mason39279cc2007-06-12 06:35:45 -04008506out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008507 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008508out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08008509 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008510 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008511
Chris Mason39279cc2007-06-12 06:35:45 -04008512 return ret;
8513}
8514
Miao Xie8ccf6f192012-10-25 09:28:04 +00008515static void btrfs_run_delalloc_work(struct btrfs_work *work)
8516{
8517 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04008518 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008519
8520 delalloc_work = container_of(work, struct btrfs_delalloc_work,
8521 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04008522 inode = delalloc_work->inode;
8523 if (delalloc_work->wait) {
8524 btrfs_wait_ordered_range(inode, 0, (u64)-1);
8525 } else {
8526 filemap_flush(inode->i_mapping);
8527 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8528 &BTRFS_I(inode)->runtime_flags))
8529 filemap_flush(inode->i_mapping);
8530 }
Miao Xie8ccf6f192012-10-25 09:28:04 +00008531
8532 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -04008533 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008534 else
Josef Bacik9f23e282013-10-28 15:03:41 -04008535 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008536 complete(&delalloc_work->completion);
8537}
8538
8539struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
8540 int wait, int delay_iput)
8541{
8542 struct btrfs_delalloc_work *work;
8543
8544 work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
8545 if (!work)
8546 return NULL;
8547
8548 init_completion(&work->completion);
8549 INIT_LIST_HEAD(&work->list);
8550 work->inode = inode;
8551 work->wait = wait;
8552 work->delay_iput = delay_iput;
Liu Bo9e0af232014-08-15 23:36:53 +08008553 WARN_ON_ONCE(!inode);
8554 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
8555 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008556
8557 return work;
8558}
8559
8560void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
8561{
8562 wait_for_completion(&work->completion);
8563 kmem_cache_free(btrfs_delalloc_work_cachep, work);
8564}
8565
Chris Masond352ac62008-09-29 15:18:18 -04008566/*
8567 * some fairly slow code that needs optimization. This walks the list
8568 * of all the inodes with pending delalloc and forces them to disk.
8569 */
Miao Xie6c255e62014-03-06 13:55:01 +08008570static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
8571 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -04008572{
Chris Masonea8c2812008-08-04 23:17:27 -04008573 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008574 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008575 struct btrfs_delalloc_work *work, *next;
8576 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00008577 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008578 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04008579
Miao Xie8ccf6f192012-10-25 09:28:04 +00008580 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008581 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00008582
Miao Xie573bfb72014-03-06 13:55:03 +08008583 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00008584 spin_lock(&root->delalloc_lock);
8585 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008586 while (!list_empty(&splice)) {
8587 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04008588 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008589
Miao Xieeb73c1b2013-05-15 07:48:22 +00008590 list_move_tail(&binode->delalloc_inodes,
8591 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008592 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00008593 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00008594 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008595 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008596 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008597 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008598
8599 work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
8600 if (unlikely(!work)) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -04008601 if (delay_iput)
8602 btrfs_add_delayed_iput(inode);
8603 else
8604 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008605 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +08008606 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008607 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008608 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +08008609 btrfs_queue_work(root->fs_info->flush_workers,
8610 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +08008611 ret++;
8612 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +08008613 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008614 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00008615 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04008616 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008617 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04008618
Wang Shilonga1ecaab2014-04-02 19:53:32 +08008619out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00008620 list_for_each_entry_safe(work, next, &works, list) {
8621 list_del_init(&work->list);
8622 btrfs_wait_and_free_delalloc_work(work);
8623 }
8624
Miao Xieeb73c1b2013-05-15 07:48:22 +00008625 if (!list_empty_careful(&splice)) {
8626 spin_lock(&root->delalloc_lock);
8627 list_splice_tail(&splice, &root->delalloc_inodes);
8628 spin_unlock(&root->delalloc_lock);
8629 }
Miao Xie573bfb72014-03-06 13:55:03 +08008630 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00008631 return ret;
8632}
8633
8634int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
8635{
8636 int ret;
8637
Wang Shilong2c21b4d2014-01-14 19:42:20 +08008638 if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00008639 return -EROFS;
8640
Miao Xie6c255e62014-03-06 13:55:01 +08008641 ret = __start_delalloc_inodes(root, delay_iput, -1);
8642 if (ret > 0)
8643 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +00008644 /*
8645 * the filemap_flush will queue IO into the worker threads, but
Chris Mason8c8bee12008-09-29 11:19:10 -04008646 * we have to make sure the IO is actually started and that
8647 * ordered extents get created before we return
8648 */
8649 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05008650 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05008651 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04008652 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05008653 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
8654 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04008655 }
8656 atomic_dec(&root->fs_info->async_submit_draining);
Miao Xieeb73c1b2013-05-15 07:48:22 +00008657 return ret;
8658}
8659
Miao Xie6c255e62014-03-06 13:55:01 +08008660int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
8661 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +00008662{
8663 struct btrfs_root *root;
8664 struct list_head splice;
8665 int ret;
8666
Wang Shilong2c21b4d2014-01-14 19:42:20 +08008667 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00008668 return -EROFS;
8669
8670 INIT_LIST_HEAD(&splice);
8671
Miao Xie573bfb72014-03-06 13:55:03 +08008672 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00008673 spin_lock(&fs_info->delalloc_root_lock);
8674 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +08008675 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00008676 root = list_first_entry(&splice, struct btrfs_root,
8677 delalloc_root);
8678 root = btrfs_grab_fs_root(root);
8679 BUG_ON(!root);
8680 list_move_tail(&root->delalloc_root,
8681 &fs_info->delalloc_roots);
8682 spin_unlock(&fs_info->delalloc_root_lock);
8683
Miao Xie6c255e62014-03-06 13:55:01 +08008684 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +00008685 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +08008686 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +00008687 goto out;
8688
Miao Xie6c255e62014-03-06 13:55:01 +08008689 if (nr != -1) {
8690 nr -= ret;
8691 WARN_ON(nr < 0);
8692 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008693 spin_lock(&fs_info->delalloc_root_lock);
8694 }
8695 spin_unlock(&fs_info->delalloc_root_lock);
8696
Miao Xie6c255e62014-03-06 13:55:01 +08008697 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +00008698 atomic_inc(&fs_info->async_submit_draining);
8699 while (atomic_read(&fs_info->nr_async_submits) ||
8700 atomic_read(&fs_info->async_delalloc_pages)) {
8701 wait_event(fs_info->async_submit_wait,
8702 (atomic_read(&fs_info->nr_async_submits) == 0 &&
8703 atomic_read(&fs_info->async_delalloc_pages) == 0));
8704 }
8705 atomic_dec(&fs_info->async_submit_draining);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008706out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00008707 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00008708 spin_lock(&fs_info->delalloc_root_lock);
8709 list_splice_tail(&splice, &fs_info->delalloc_roots);
8710 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008711 }
Miao Xie573bfb72014-03-06 13:55:03 +08008712 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008713 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04008714}
8715
Chris Mason39279cc2007-06-12 06:35:45 -04008716static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
8717 const char *symname)
8718{
8719 struct btrfs_trans_handle *trans;
8720 struct btrfs_root *root = BTRFS_I(dir)->root;
8721 struct btrfs_path *path;
8722 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05008723 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04008724 int err;
8725 int drop_inode = 0;
8726 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05308727 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008728 int name_len;
8729 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04008730 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04008731 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04008732 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04008733
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01008734 name_len = strlen(symname);
Chris Mason39279cc2007-06-12 06:35:45 -04008735 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
8736 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05008737
Josef Bacik9ed74f22009-09-11 16:12:44 -04008738 /*
8739 * 2 items for inode item and ref
8740 * 2 items for dir items
8741 * 1 item for xattr if selinux is on
8742 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04008743 trans = btrfs_start_transaction(root, 5);
8744 if (IS_ERR(trans))
8745 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05008746
Li Zefan581bb052011-04-20 10:06:11 +08008747 err = btrfs_find_free_ino(root, &objectid);
8748 if (err)
8749 goto out_unlock;
8750
Josef Bacikaec74772008-07-24 12:12:38 -04008751 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08008752 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04008753 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008754 if (IS_ERR(inode)) {
8755 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008756 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008757 }
Chris Mason39279cc2007-06-12 06:35:45 -04008758
Eric Paris2a7dba32011-02-01 11:05:39 -05008759 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04008760 if (err) {
8761 drop_inode = 1;
8762 goto out_unlock;
8763 }
8764
Casey Schauflerad19db72011-12-15 10:09:07 -05008765 /*
8766 * If the active LSM wants to access the inode during
8767 * d_instantiate it needs these. Smack checks to see
8768 * if the filesystem supports xattrs by looking at the
8769 * ops vector.
8770 */
8771 inode->i_fop = &btrfs_file_operations;
8772 inode->i_op = &btrfs_file_inode_operations;
8773
Josef Bacika1b075d2010-11-19 20:36:11 +00008774 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04008775 if (err)
8776 drop_inode = 1;
8777 else {
8778 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04008779 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05008780 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04008781 }
Chris Mason39279cc2007-06-12 06:35:45 -04008782 if (drop_inode)
8783 goto out_unlock;
8784
8785 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07008786 if (!path) {
8787 err = -ENOMEM;
8788 drop_inode = 1;
8789 goto out_unlock;
8790 }
Li Zefan33345d012011-04-20 10:31:50 +08008791 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008792 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008793 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
8794 datasize = btrfs_file_extent_calc_inline_size(name_len);
8795 err = btrfs_insert_empty_item(trans, root, path, &key,
8796 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04008797 if (err) {
8798 drop_inode = 1;
Julia Lawallb0839162011-05-14 07:10:51 +00008799 btrfs_free_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04008800 goto out_unlock;
8801 }
Chris Mason5f39d392007-10-15 16:14:19 -04008802 leaf = path->nodes[0];
8803 ei = btrfs_item_ptr(leaf, path->slots[0],
8804 struct btrfs_file_extent_item);
8805 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
8806 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04008807 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04008808 btrfs_set_file_extent_encryption(leaf, ei, 0);
8809 btrfs_set_file_extent_compression(leaf, ei, 0);
8810 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
8811 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
8812
Chris Mason39279cc2007-06-12 06:35:45 -04008813 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04008814 write_extent_buffer(leaf, symname, ptr, name_len);
8815 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04008816 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04008817
Chris Mason39279cc2007-06-12 06:35:45 -04008818 inode->i_op = &btrfs_symlink_inode_operations;
8819 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04008820 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04008821 inode_set_bytes(inode, name_len);
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01008822 btrfs_i_size_write(inode, name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -04008823 err = btrfs_update_inode(trans, root, inode);
8824 if (err)
8825 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04008826
8827out_unlock:
Al Viro08c422c2011-12-23 07:58:13 -05008828 if (!err)
8829 d_instantiate(dentry, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008830 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04008831 if (drop_inode) {
8832 inode_dec_link_count(inode);
8833 iput(inode);
8834 }
Liu Bob53d3f52012-11-14 14:34:34 +00008835 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04008836 return err;
8837}
Chris Mason16432982008-04-10 10:23:21 -04008838
Josef Bacik0af3d002010-06-21 14:48:16 -04008839static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
8840 u64 start, u64 num_bytes, u64 min_size,
8841 loff_t actual_len, u64 *alloc_hint,
8842 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04008843{
Josef Bacik5dc562c2012-08-17 13:14:17 -04008844 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
8845 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04008846 struct btrfs_root *root = BTRFS_I(inode)->root;
8847 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04008848 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00008849 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05008850 u64 cur_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04008851 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04008852 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04008853
Josef Bacik0af3d002010-06-21 14:48:16 -04008854 if (trans)
8855 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04008856 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008857 if (own_trans) {
8858 trans = btrfs_start_transaction(root, 3);
8859 if (IS_ERR(trans)) {
8860 ret = PTR_ERR(trans);
8861 break;
8862 }
Yan Zhengd899e052008-10-30 14:25:28 -04008863 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00008864
Chris Mason154ea282013-03-05 11:11:26 -05008865 cur_bytes = min(num_bytes, 256ULL * 1024 * 1024);
8866 cur_bytes = max(cur_bytes, min_size);
Josef Bacik00361582013-08-14 14:02:47 -04008867 ret = btrfs_reserve_extent(root, cur_bytes, min_size, 0,
Miao Xiee570fd22014-06-19 10:42:50 +08008868 *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008869 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008870 if (own_trans)
8871 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008872 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00008873 }
8874
Yan Zhengd899e052008-10-30 14:25:28 -04008875 ret = insert_reserved_file_extent(trans, inode,
8876 cur_offset, ins.objectid,
8877 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00008878 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04008879 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008880 if (ret) {
Josef Bacik857cc2f2013-10-07 15:21:08 -04008881 btrfs_free_reserved_extent(root, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +08008882 ins.offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008883 btrfs_abort_transaction(trans, root, ret);
8884 if (own_trans)
8885 btrfs_end_transaction(trans, root);
8886 break;
8887 }
Chris Masona1ed8352009-09-11 12:27:37 -04008888 btrfs_drop_extent_cache(inode, cur_offset,
8889 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008890
Josef Bacik5dc562c2012-08-17 13:14:17 -04008891 em = alloc_extent_map();
8892 if (!em) {
8893 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
8894 &BTRFS_I(inode)->runtime_flags);
8895 goto next;
8896 }
8897
8898 em->start = cur_offset;
8899 em->orig_start = cur_offset;
8900 em->len = ins.offset;
8901 em->block_start = ins.objectid;
8902 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05008903 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04008904 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008905 em->bdev = root->fs_info->fs_devices->latest_bdev;
8906 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
8907 em->generation = trans->transid;
8908
8909 while (1) {
8910 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04008911 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04008912 write_unlock(&em_tree->lock);
8913 if (ret != -EEXIST)
8914 break;
8915 btrfs_drop_extent_cache(inode, cur_offset,
8916 cur_offset + ins.offset - 1,
8917 0);
8918 }
8919 free_extent_map(em);
8920next:
Yan Zhengd899e052008-10-30 14:25:28 -04008921 num_bytes -= ins.offset;
8922 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04008923 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00008924
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008925 inode_inc_iversion(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04008926 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02008927 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04008928 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04008929 (actual_len > inode->i_size) &&
8930 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00008931 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00008932 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00008933 else
Josef Bacik55a61d12010-11-22 18:50:32 +00008934 i_size = cur_offset;
8935 i_size_write(inode, i_size);
8936 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008937 }
8938
Yan Zhengd899e052008-10-30 14:25:28 -04008939 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008940
8941 if (ret) {
8942 btrfs_abort_transaction(trans, root, ret);
8943 if (own_trans)
8944 btrfs_end_transaction(trans, root);
8945 break;
8946 }
Yan Zhengd899e052008-10-30 14:25:28 -04008947
Josef Bacik0af3d002010-06-21 14:48:16 -04008948 if (own_trans)
8949 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008950 }
Yan Zhengd899e052008-10-30 14:25:28 -04008951 return ret;
8952}
8953
Josef Bacik0af3d002010-06-21 14:48:16 -04008954int btrfs_prealloc_file_range(struct inode *inode, int mode,
8955 u64 start, u64 num_bytes, u64 min_size,
8956 loff_t actual_len, u64 *alloc_hint)
8957{
8958 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
8959 min_size, actual_len, alloc_hint,
8960 NULL);
8961}
8962
8963int btrfs_prealloc_file_range_trans(struct inode *inode,
8964 struct btrfs_trans_handle *trans, int mode,
8965 u64 start, u64 num_bytes, u64 min_size,
8966 loff_t actual_len, u64 *alloc_hint)
8967{
8968 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
8969 min_size, actual_len, alloc_hint, trans);
8970}
8971
Chris Masone6dcd2d2008-07-17 12:53:50 -04008972static int btrfs_set_page_dirty(struct page *page)
8973{
Chris Masone6dcd2d2008-07-17 12:53:50 -04008974 return __set_page_dirty_nobuffers(page);
8975}
8976
Al Viro10556cb2011-06-20 19:28:19 -04008977static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05008978{
Li Zefanb83cc962010-12-20 16:04:08 +08008979 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00008980 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08008981
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00008982 if (mask & MAY_WRITE &&
8983 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
8984 if (btrfs_root_readonly(root))
8985 return -EROFS;
8986 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
8987 return -EACCES;
8988 }
Al Viro2830ba72011-06-20 19:16:29 -04008989 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05008990}
Chris Mason39279cc2007-06-12 06:35:45 -04008991
Filipe Mananaef3b9af2014-04-27 20:40:45 +01008992static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
8993{
8994 struct btrfs_trans_handle *trans;
8995 struct btrfs_root *root = BTRFS_I(dir)->root;
8996 struct inode *inode = NULL;
8997 u64 objectid;
8998 u64 index;
8999 int ret = 0;
9000
9001 /*
9002 * 5 units required for adding orphan entry
9003 */
9004 trans = btrfs_start_transaction(root, 5);
9005 if (IS_ERR(trans))
9006 return PTR_ERR(trans);
9007
9008 ret = btrfs_find_free_ino(root, &objectid);
9009 if (ret)
9010 goto out;
9011
9012 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
9013 btrfs_ino(dir), objectid, mode, &index);
9014 if (IS_ERR(inode)) {
9015 ret = PTR_ERR(inode);
9016 inode = NULL;
9017 goto out;
9018 }
9019
9020 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
9021 if (ret)
9022 goto out;
9023
9024 ret = btrfs_update_inode(trans, root, inode);
9025 if (ret)
9026 goto out;
9027
9028 inode->i_fop = &btrfs_file_operations;
9029 inode->i_op = &btrfs_file_inode_operations;
9030
9031 inode->i_mapping->a_ops = &btrfs_aops;
9032 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
9033 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
9034
9035 ret = btrfs_orphan_add(trans, inode);
9036 if (ret)
9037 goto out;
9038
Filipe Manana5762b5c2014-08-01 00:10:32 +01009039 /*
9040 * We set number of links to 0 in btrfs_new_inode(), and here we set
9041 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
9042 * through:
9043 *
9044 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
9045 */
9046 set_nlink(inode, 1);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009047 d_tmpfile(dentry, inode);
9048 mark_inode_dirty(inode);
9049
9050out:
9051 btrfs_end_transaction(trans, root);
9052 if (ret)
9053 iput(inode);
9054 btrfs_balance_delayed_items(root);
9055 btrfs_btree_balance_dirty(root);
9056
9057 return ret;
9058}
9059
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009060static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05009061 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04009062 .lookup = btrfs_lookup,
9063 .create = btrfs_create,
9064 .unlink = btrfs_unlink,
9065 .link = btrfs_link,
9066 .mkdir = btrfs_mkdir,
9067 .rmdir = btrfs_rmdir,
9068 .rename = btrfs_rename,
9069 .symlink = btrfs_symlink,
9070 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04009071 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009072 .setxattr = btrfs_setxattr,
9073 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05009074 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009075 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05009076 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009077 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009078 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -07009079 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009080 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -04009081};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009082static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009083 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05009084 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009085 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009086 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -07009087 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04009088};
Yan, Zheng76dda932009-09-21 16:00:26 -04009089
Alexey Dobriyan828c0952009-10-01 15:43:56 -07009090static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009091 .llseek = generic_file_llseek,
9092 .read = generic_read_dir,
Al Viro9cdda8d2013-05-22 16:48:09 -04009093 .iterate = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04009094 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04009095#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04009096 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04009097#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04009098 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04009099 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04009100};
9101
Chris Masond1310b22008-01-24 16:13:08 -05009102static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04009103 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05009104 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04009105 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04009106 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04009107 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04009108 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05009109 .set_bit_hook = btrfs_set_bit_hook,
9110 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04009111 .merge_extent_hook = btrfs_merge_extent_hook,
9112 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04009113};
9114
Chris Mason35054392009-01-21 13:11:13 -05009115/*
9116 * btrfs doesn't support the bmap operation because swapfiles
9117 * use bmap to make a mapping of extents in the file. They assume
9118 * these extents won't change over the life of the file and they
9119 * use the bmap result to do IO directly to the drive.
9120 *
9121 * the btrfs bmap call would return logical addresses that aren't
9122 * suitable for IO and they also will change frequently as COW
9123 * operations happen. So, swapfile + btrfs == corruption.
9124 *
9125 * For now we're avoiding this by dropping bmap.
9126 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07009127static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04009128 .readpage = btrfs_readpage,
9129 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04009130 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05009131 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04009132 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04009133 .invalidatepage = btrfs_invalidatepage,
9134 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04009135 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02009136 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04009137};
9138
Alexey Dobriyan7f094102009-09-21 17:01:10 -07009139static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04009140 .readpage = btrfs_readpage,
9141 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04009142 .invalidatepage = btrfs_invalidatepage,
9143 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04009144};
9145
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009146static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009147 .getattr = btrfs_getattr,
9148 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009149 .setxattr = btrfs_setxattr,
9150 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05009151 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009152 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05009153 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05009154 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009155 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009156 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04009157 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04009158};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009159static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04009160 .getattr = btrfs_getattr,
9161 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05009162 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009163 .setxattr = btrfs_setxattr,
9164 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04009165 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009166 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009167 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009168 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04009169 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -04009170};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009171static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009172 .readlink = generic_readlink,
9173 .follow_link = page_follow_link_light,
9174 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00009175 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -05009176 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05009177 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05009178 .setxattr = btrfs_setxattr,
9179 .getxattr = btrfs_getxattr,
9180 .listxattr = btrfs_listxattr,
9181 .removexattr = btrfs_removexattr,
Josef Bacike41f9412012-03-26 09:46:47 -04009182 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04009183};
Yan, Zheng76dda932009-09-21 16:00:26 -04009184
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04009185const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04009186 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -04009187 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -04009188};