blob: 3d020d6d9aceffb115a2322cfdd070d8f8c2d62f [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Mason8f18cf12008-04-25 16:53:30 -040019#include <linux/kernel.h>
Chris Mason065631f2008-02-20 12:07:25 -050020#include <linux/bio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040021#include <linux/buffer_head.h>
Sage Weilf2eb0a22008-05-02 14:43:14 -040022#include <linux/file.h>
Chris Mason39279cc2007-06-12 06:35:45 -040023#include <linux/fs.h>
24#include <linux/pagemap.h>
25#include <linux/highmem.h>
26#include <linux/time.h>
27#include <linux/init.h>
28#include <linux/string.h>
Chris Mason39279cc2007-06-12 06:35:45 -040029#include <linux/backing-dev.h>
30#include <linux/mpage.h>
31#include <linux/swap.h>
32#include <linux/writeback.h>
33#include <linux/statfs.h>
34#include <linux/compat.h>
Kent Overstreeta27bb332013-05-07 16:19:08 -070035#include <linux/aio.h>
Chris Mason9ebefb182007-06-15 13:50:00 -040036#include <linux/bit_spinlock.h>
Josef Bacik5103e942007-11-16 11:45:54 -050037#include <linux/xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040038#include <linux/posix_acl.h>
Yan Zhengd899e052008-10-30 14:25:28 -040039#include <linux/falloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090040#include <linux/slab.h>
David Sterba7a36dde2011-05-06 15:33:15 +020041#include <linux/ratelimit.h>
Josef Bacik22c44fe2011-11-30 10:45:38 -050042#include <linux/mount.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000043#include <linux/btrfs.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050044#include <linux/blkdev.h>
Josef Bacikf23b5a52013-06-19 10:16:26 -040045#include <linux/posix_acl_xattr.h>
Chris Mason39279cc2007-06-12 06:35:45 -040046#include "ctree.h"
47#include "disk-io.h"
48#include "transaction.h"
49#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040050#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040051#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040052#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040053#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020054#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040055#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050056#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050057#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080058#include "inode-map.h"
Liu Bo38c227d2013-01-29 03:18:40 +000059#include "backref.h"
Josef Bacikf23b5a52013-06-19 10:16:26 -040060#include "hash.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000061#include "props.h"
Chris Mason39279cc2007-06-12 06:35:45 -040062
63struct btrfs_iget_args {
Chris Mason90d3e592014-01-09 17:28:00 -080064 struct btrfs_key *location;
Chris Mason39279cc2007-06-12 06:35:45 -040065 struct btrfs_root *root;
66};
67
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070068static const struct inode_operations btrfs_dir_inode_operations;
69static const struct inode_operations btrfs_symlink_inode_operations;
70static const struct inode_operations btrfs_dir_ro_inode_operations;
71static const struct inode_operations btrfs_special_inode_operations;
72static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070073static const struct address_space_operations btrfs_aops;
74static const struct address_space_operations btrfs_symlink_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070075static const struct file_operations btrfs_dir_file_operations;
Chris Masond1310b22008-01-24 16:13:08 -050076static struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040077
78static struct kmem_cache *btrfs_inode_cachep;
Miao Xie8ccf6f192012-10-25 09:28:04 +000079static struct kmem_cache *btrfs_delalloc_work_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040080struct kmem_cache *btrfs_trans_handle_cachep;
81struct kmem_cache *btrfs_transaction_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040082struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050083struct kmem_cache *btrfs_free_space_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040084
85#define S_SHIFT 12
86static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
87 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
88 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
89 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
90 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
91 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
92 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
93 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
94};
95
Eric Sandeen3972f262013-01-12 02:57:22 +000096static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Josef Bacika41ad392011-01-31 15:30:16 -050097static int btrfs_truncate(struct inode *inode);
Josef Bacik5fd02042012-05-02 14:00:54 -040098static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Chris Mason771ed682008-11-06 22:02:51 -050099static noinline int cow_file_range(struct inode *inode,
100 struct page *locked_page,
101 u64 start, u64 end, int *page_started,
102 unsigned long *nr_written, int unlock);
Josef Bacik70c8a912012-10-11 16:54:30 -0400103static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
104 u64 len, u64 orig_start,
105 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -0400106 u64 orig_block_len, u64 ram_bytes,
107 int type);
Josef Bacik7b128762008-07-24 12:17:14 -0400108
Eric Sandeen48a3b632013-04-25 20:41:01 +0000109static int btrfs_dirty_inode(struct inode *inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400110
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000111static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500112 struct inode *inode, struct inode *dir,
113 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500114{
115 int err;
116
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000117 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500118 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500119 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500120 return err;
121}
122
Chris Masond352ac62008-09-29 15:18:18 -0400123/*
Chris Masonc8b97812008-10-29 14:49:59 -0400124 * this does all the hard work for inserting an inline extent into
125 * the btree. The caller should have done a btrfs_drop_extents so that
126 * no overlapping inline items exist in the btree
127 */
Chris Mason40f76582014-05-21 13:35:51 -0700128static int insert_inline_extent(struct btrfs_trans_handle *trans,
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000129 struct btrfs_path *path, int extent_inserted,
Chris Masonc8b97812008-10-29 14:49:59 -0400130 struct btrfs_root *root, struct inode *inode,
131 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000132 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400133 struct page **compressed_pages)
134{
Chris Masonc8b97812008-10-29 14:49:59 -0400135 struct extent_buffer *leaf;
136 struct page *page = NULL;
137 char *kaddr;
138 unsigned long ptr;
139 struct btrfs_file_extent_item *ei;
140 int err = 0;
141 int ret;
142 size_t cur_size = size;
Chris Masonc8b97812008-10-29 14:49:59 -0400143 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400144
Li Zefanfe3f5662011-03-28 08:30:38 +0000145 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400146 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400147
Chris Masonc8b97812008-10-29 14:49:59 -0400148 inode_add_bytes(inode, size);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000149
150 if (!extent_inserted) {
151 struct btrfs_key key;
152 size_t datasize;
153
154 key.objectid = btrfs_ino(inode);
155 key.offset = start;
156 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
157
158 datasize = btrfs_file_extent_calc_inline_size(cur_size);
159 path->leave_spinning = 1;
160 ret = btrfs_insert_empty_item(trans, root, path, &key,
161 datasize);
162 if (ret) {
163 err = ret;
164 goto fail;
165 }
Chris Masonc8b97812008-10-29 14:49:59 -0400166 }
167 leaf = path->nodes[0];
168 ei = btrfs_item_ptr(leaf, path->slots[0],
169 struct btrfs_file_extent_item);
170 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
171 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
172 btrfs_set_file_extent_encryption(leaf, ei, 0);
173 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
174 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
175 ptr = btrfs_file_extent_inline_start(ei);
176
Li Zefan261507a02010-12-17 14:21:50 +0800177 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400178 struct page *cpage;
179 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500180 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400181 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500182 cur_size = min_t(unsigned long, compressed_size,
Chris Masonc8b97812008-10-29 14:49:59 -0400183 PAGE_CACHE_SIZE);
184
Cong Wang7ac687d2011-11-25 23:14:28 +0800185 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400186 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800187 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400188
189 i++;
190 ptr += cur_size;
191 compressed_size -= cur_size;
192 }
193 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800194 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400195 } else {
196 page = find_get_page(inode->i_mapping,
197 start >> PAGE_CACHE_SHIFT);
198 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800199 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400200 offset = start & (PAGE_CACHE_SIZE - 1);
201 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800202 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400203 page_cache_release(page);
204 }
205 btrfs_mark_buffer_dirty(leaf);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000206 btrfs_release_path(path);
Chris Masonc8b97812008-10-29 14:49:59 -0400207
Yan, Zhengc2167752009-11-12 09:34:21 +0000208 /*
209 * we're an inline extent, so nobody can
210 * extend the file past i_size without locking
211 * a page we already have locked.
212 *
213 * We must do any isize and inode updates
214 * before we unlock the pages. Otherwise we
215 * could end up racing with unlink.
216 */
Chris Masonc8b97812008-10-29 14:49:59 -0400217 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100218 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000219
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100220 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400221fail:
Chris Masonc8b97812008-10-29 14:49:59 -0400222 return err;
223}
224
225
226/*
227 * conditionally insert an inline extent into the file. This
228 * does the checks required to make sure the data is small enough
229 * to fit as an inline extent.
230 */
Josef Bacik00361582013-08-14 14:02:47 -0400231static noinline int cow_file_range_inline(struct btrfs_root *root,
232 struct inode *inode, u64 start,
233 u64 end, size_t compressed_size,
234 int compress_type,
235 struct page **compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400236{
Josef Bacik00361582013-08-14 14:02:47 -0400237 struct btrfs_trans_handle *trans;
Chris Masonc8b97812008-10-29 14:49:59 -0400238 u64 isize = i_size_read(inode);
239 u64 actual_end = min(end + 1, isize);
240 u64 inline_len = actual_end - start;
Qu Wenruofda28322013-02-26 08:10:22 +0000241 u64 aligned_end = ALIGN(end, root->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400242 u64 data_len = inline_len;
243 int ret;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000244 struct btrfs_path *path;
245 int extent_inserted = 0;
246 u32 extent_item_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400247
248 if (compressed_size)
249 data_len = compressed_size;
250
251 if (start > 0 ||
Chris Mason70b99e62008-10-31 12:46:39 -0400252 actual_end >= PAGE_CACHE_SIZE ||
Chris Masonc8b97812008-10-29 14:49:59 -0400253 data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
254 (!compressed_size &&
255 (actual_end & (root->sectorsize - 1)) == 0) ||
256 end + 1 < isize ||
257 data_len > root->fs_info->max_inline) {
258 return 1;
259 }
260
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000261 path = btrfs_alloc_path();
262 if (!path)
263 return -ENOMEM;
264
Josef Bacik00361582013-08-14 14:02:47 -0400265 trans = btrfs_join_transaction(root);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000266 if (IS_ERR(trans)) {
267 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400268 return PTR_ERR(trans);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000269 }
Josef Bacik00361582013-08-14 14:02:47 -0400270 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
271
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000272 if (compressed_size && compressed_pages)
273 extent_item_size = btrfs_file_extent_calc_inline_size(
274 compressed_size);
275 else
276 extent_item_size = btrfs_file_extent_calc_inline_size(
277 inline_len);
278
279 ret = __btrfs_drop_extents(trans, root, inode, path,
280 start, aligned_end, NULL,
281 1, 1, extent_item_size, &extent_inserted);
Josef Bacik00361582013-08-14 14:02:47 -0400282 if (ret) {
283 btrfs_abort_transaction(trans, root, ret);
284 goto out;
285 }
Chris Masonc8b97812008-10-29 14:49:59 -0400286
287 if (isize > actual_end)
288 inline_len = min_t(u64, isize, actual_end);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000289 ret = insert_inline_extent(trans, path, extent_inserted,
290 root, inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400291 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000292 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400293 if (ret && ret != -ENOSPC) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100294 btrfs_abort_transaction(trans, root, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400295 goto out;
Josef Bacik2adcac12012-05-23 16:10:14 -0400296 } else if (ret == -ENOSPC) {
Josef Bacik00361582013-08-14 14:02:47 -0400297 ret = 1;
298 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100299 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400300
Josef Bacikbdc20e62013-02-28 13:23:38 -0500301 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400302 btrfs_delalloc_release_metadata(inode, end + 1 - start);
Chris Masona1ed8352009-09-11 12:27:37 -0400303 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
Josef Bacik00361582013-08-14 14:02:47 -0400304out:
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000305 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400306 btrfs_end_transaction(trans, root);
307 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400308}
309
Chris Mason771ed682008-11-06 22:02:51 -0500310struct async_extent {
311 u64 start;
312 u64 ram_size;
313 u64 compressed_size;
314 struct page **pages;
315 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800316 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500317 struct list_head list;
318};
319
320struct async_cow {
321 struct inode *inode;
322 struct btrfs_root *root;
323 struct page *locked_page;
324 u64 start;
325 u64 end;
326 struct list_head extents;
327 struct btrfs_work work;
328};
329
330static noinline int add_async_extent(struct async_cow *cow,
331 u64 start, u64 ram_size,
332 u64 compressed_size,
333 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800334 unsigned long nr_pages,
335 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500336{
337 struct async_extent *async_extent;
338
339 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100340 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500341 async_extent->start = start;
342 async_extent->ram_size = ram_size;
343 async_extent->compressed_size = compressed_size;
344 async_extent->pages = pages;
345 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800346 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500347 list_add_tail(&async_extent->list, &cow->extents);
348 return 0;
349}
350
Chris Masonc8b97812008-10-29 14:49:59 -0400351/*
Chris Mason771ed682008-11-06 22:02:51 -0500352 * we create compressed extents in two phases. The first
353 * phase compresses a range of pages that have already been
354 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400355 *
Chris Mason771ed682008-11-06 22:02:51 -0500356 * This is done inside an ordered work queue, and the compression
357 * is spread across many cpus. The actual IO submission is step
358 * two, and the ordered work queue takes care of making sure that
359 * happens in the same order things were put onto the queue by
360 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400361 *
Chris Mason771ed682008-11-06 22:02:51 -0500362 * If this code finds it can't get good compression, it puts an
363 * entry onto the work queue to write the uncompressed bytes. This
364 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300365 * are written in the same order that the flusher thread sent them
366 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400367 */
Chris Mason771ed682008-11-06 22:02:51 -0500368static noinline int compress_file_range(struct inode *inode,
369 struct page *locked_page,
370 u64 start, u64 end,
371 struct async_cow *async_cow,
372 int *num_added)
Chris Masonb888db22007-08-27 16:49:44 -0400373{
374 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masondb945352007-10-15 16:15:53 -0400375 u64 num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400376 u64 blocksize = root->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400377 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500378 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400379 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400380 struct page **pages = NULL;
381 unsigned long nr_pages;
382 unsigned long nr_pages_ret = 0;
383 unsigned long total_compressed = 0;
384 unsigned long total_in = 0;
385 unsigned long max_compressed = 128 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -0500386 unsigned long max_uncompressed = 128 * 1024;
Chris Masonc8b97812008-10-29 14:49:59 -0400387 int i;
388 int will_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800389 int compress_type = root->fs_info->compress_type;
Chris Mason4adaa612013-03-26 13:07:00 -0400390 int redirty = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400391
Liu Bo4cb13e52012-03-29 09:57:45 -0400392 /* if this is a small write inside eof, kick off a defrag */
393 if ((end - start + 1) < 16 * 1024 &&
394 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400395 btrfs_add_inode_defrag(NULL, inode);
396
Wang Shilong68bb4622014-04-01 18:01:42 +0800397 /*
398 * skip compression for a small file range(<=blocksize) that
399 * isn't an inline extent, since it dosen't save disk space at all.
400 */
401 if ((end - start + 1) <= blocksize &&
402 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
403 goto cleanup_and_bail_uncompressed;
404
Chris Mason42dc7ba2008-12-15 11:44:56 -0500405 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400406again:
407 will_compress = 0;
408 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
409 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
410
Chris Masonf03d9301f2009-02-04 09:31:06 -0500411 /*
412 * we don't want to send crud past the end of i_size through
413 * compression, that's just a waste of CPU time. So, if the
414 * end of the file is before the start of our current
415 * requested range of bytes, we bail out to the uncompressed
416 * cleanup code that can deal with all of this.
417 *
418 * It isn't really the fastest way to fix things, but this is a
419 * very uncommon corner.
420 */
421 if (actual_end <= start)
422 goto cleanup_and_bail_uncompressed;
423
Chris Masonc8b97812008-10-29 14:49:59 -0400424 total_compressed = actual_end - start;
425
426 /* we want to make sure that amount of ram required to uncompress
427 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500428 * of a compressed extent to 128k. This is a crucial number
429 * because it also controls how easily we can spread reads across
430 * cpus for decompression.
431 *
432 * We also want to make sure the amount of IO required to do
433 * a random read is reasonably small, so we limit the size of
434 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400435 */
436 total_compressed = min(total_compressed, max_uncompressed);
Qu Wenruofda28322013-02-26 08:10:22 +0000437 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Masonbe20aa92007-12-17 20:14:01 -0500438 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400439 total_in = 0;
440 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400441
Chris Mason771ed682008-11-06 22:02:51 -0500442 /*
443 * we do compression for mount -o compress and when the
444 * inode has not been flagged as nocompress. This flag can
445 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400446 */
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200447 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) &&
Chris Mason1e701a32010-03-11 09:42:04 -0500448 (btrfs_test_opt(root, COMPRESS) ||
Liu Bo75e7cb72011-03-22 10:12:20 +0000449 (BTRFS_I(inode)->force_compress) ||
450 (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS))) {
Chris Masonc8b97812008-10-29 14:49:59 -0400451 WARN_ON(pages);
Chris Masoncfbc2462008-10-30 13:22:14 -0400452 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800453 if (!pages) {
454 /* just bail out to the uncompressed code */
455 goto cont;
456 }
Chris Mason179e29e2007-11-01 11:28:41 -0400457
Li Zefan261507a02010-12-17 14:21:50 +0800458 if (BTRFS_I(inode)->force_compress)
459 compress_type = BTRFS_I(inode)->force_compress;
460
Chris Mason4adaa612013-03-26 13:07:00 -0400461 /*
462 * we need to call clear_page_dirty_for_io on each
463 * page in the range. Otherwise applications with the file
464 * mmap'd can wander in and change the page contents while
465 * we are compressing them.
466 *
467 * If the compression fails for any reason, we set the pages
468 * dirty again later on.
469 */
470 extent_range_clear_dirty_for_io(inode, start, end);
471 redirty = 1;
Li Zefan261507a02010-12-17 14:21:50 +0800472 ret = btrfs_compress_pages(compress_type,
473 inode->i_mapping, start,
474 total_compressed, pages,
475 nr_pages, &nr_pages_ret,
476 &total_in,
477 &total_compressed,
478 max_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400479
480 if (!ret) {
481 unsigned long offset = total_compressed &
482 (PAGE_CACHE_SIZE - 1);
483 struct page *page = pages[nr_pages_ret - 1];
484 char *kaddr;
485
486 /* zero the tail end of the last page, we might be
487 * sending it down to disk
488 */
489 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800490 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400491 memset(kaddr + offset, 0,
492 PAGE_CACHE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800493 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400494 }
495 will_compress = 1;
496 }
497 }
Li Zefan560f7d72011-09-08 10:22:01 +0800498cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400499 if (start == 0) {
500 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500501 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400502 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500503 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400504 */
Josef Bacik00361582013-08-14 14:02:47 -0400505 ret = cow_file_range_inline(root, inode, start, end,
506 0, 0, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400507 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500508 /* try making a compressed inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400509 ret = cow_file_range_inline(root, inode, start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000510 total_compressed,
511 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400512 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100513 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400514 unsigned long clear_flags = EXTENT_DELALLOC |
515 EXTENT_DEFRAG;
516 clear_flags |= (ret < 0) ? EXTENT_DO_ACCOUNTING : 0;
517
Chris Mason771ed682008-11-06 22:02:51 -0500518 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100519 * inline extent creation worked or returned error,
520 * we don't need to create any more async work items.
521 * Unlock and free up our temp pages.
Chris Mason771ed682008-11-06 22:02:51 -0500522 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400523 extent_clear_unlock_delalloc(inode, start, end, NULL,
Josef Bacik151a41b2013-07-29 13:22:24 -0400524 clear_flags, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400525 PAGE_CLEAR_DIRTY |
526 PAGE_SET_WRITEBACK |
527 PAGE_END_WRITEBACK);
Chris Masonc8b97812008-10-29 14:49:59 -0400528 goto free_pages_out;
529 }
530 }
531
532 if (will_compress) {
533 /*
534 * we aren't doing an inline extent round the compressed size
535 * up to a block size boundary so the allocator does sane
536 * things
537 */
Qu Wenruofda28322013-02-26 08:10:22 +0000538 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400539
540 /*
541 * one last check to make sure the compression is really a
542 * win, compare the page count read with the blocks on disk
543 */
Qu Wenruofda28322013-02-26 08:10:22 +0000544 total_in = ALIGN(total_in, PAGE_CACHE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400545 if (total_compressed >= total_in) {
546 will_compress = 0;
547 } else {
Chris Masonc8b97812008-10-29 14:49:59 -0400548 num_bytes = total_in;
549 }
550 }
551 if (!will_compress && pages) {
552 /*
553 * the compression code ran but failed to make things smaller,
554 * free any pages it allocated and our page pointer array
555 */
556 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400557 WARN_ON(pages[i]->mapping);
Chris Masonc8b97812008-10-29 14:49:59 -0400558 page_cache_release(pages[i]);
559 }
560 kfree(pages);
561 pages = NULL;
562 total_compressed = 0;
563 nr_pages_ret = 0;
564
565 /* flag the file so we don't compress in the future */
Chris Mason1e701a32010-03-11 09:42:04 -0500566 if (!btrfs_test_opt(root, FORCE_COMPRESS) &&
567 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500568 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500569 }
Chris Masonc8b97812008-10-29 14:49:59 -0400570 }
Chris Mason771ed682008-11-06 22:02:51 -0500571 if (will_compress) {
572 *num_added += 1;
573
574 /* the async work queues will take care of doing actual
575 * allocation on disk for these compressed pages,
576 * and will submit them to the elevator.
577 */
578 add_async_extent(async_cow, start, num_bytes,
Li Zefan261507a02010-12-17 14:21:50 +0800579 total_compressed, pages, nr_pages_ret,
580 compress_type);
Chris Mason771ed682008-11-06 22:02:51 -0500581
Yan, Zheng24ae6362010-12-06 07:02:36 +0000582 if (start + num_bytes < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500583 start += num_bytes;
584 pages = NULL;
585 cond_resched();
586 goto again;
587 }
588 } else {
Chris Masonf03d9301f2009-02-04 09:31:06 -0500589cleanup_and_bail_uncompressed:
Chris Mason771ed682008-11-06 22:02:51 -0500590 /*
591 * No compression, but we still need to write the pages in
592 * the file we've been given so far. redirty the locked
593 * page if it corresponds to our extent and set things up
594 * for the async work queue to run cow_file_range to do
595 * the normal delalloc dance
596 */
597 if (page_offset(locked_page) >= start &&
598 page_offset(locked_page) <= end) {
599 __set_page_dirty_nobuffers(locked_page);
600 /* unlocked later on in the async handlers */
601 }
Chris Mason4adaa612013-03-26 13:07:00 -0400602 if (redirty)
603 extent_range_redirty_for_io(inode, start, end);
Li Zefan261507a02010-12-17 14:21:50 +0800604 add_async_extent(async_cow, start, end - start + 1,
605 0, NULL, 0, BTRFS_COMPRESS_NONE);
Chris Mason771ed682008-11-06 22:02:51 -0500606 *num_added += 1;
607 }
608
609out:
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100610 return ret;
Chris Mason771ed682008-11-06 22:02:51 -0500611
612free_pages_out:
613 for (i = 0; i < nr_pages_ret; i++) {
614 WARN_ON(pages[i]->mapping);
615 page_cache_release(pages[i]);
616 }
Chris Masond3977122009-01-05 21:25:51 -0500617 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500618
619 goto out;
620}
621
622/*
623 * phase two of compressed writeback. This is the ordered portion
624 * of the code, which only gets called in the order the work was
625 * queued. We walk all the async extents created by compress_file_range
626 * and send them down to the disk.
627 */
628static noinline int submit_compressed_extents(struct inode *inode,
629 struct async_cow *async_cow)
630{
631 struct async_extent *async_extent;
632 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500633 struct btrfs_key ins;
634 struct extent_map *em;
635 struct btrfs_root *root = BTRFS_I(inode)->root;
636 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
637 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500638 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500639
640 if (list_empty(&async_cow->extents))
641 return 0;
642
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500643again:
Chris Masond3977122009-01-05 21:25:51 -0500644 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500645 async_extent = list_entry(async_cow->extents.next,
646 struct async_extent, list);
647 list_del(&async_extent->list);
648
649 io_tree = &BTRFS_I(inode)->io_tree;
650
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500651retry:
Chris Mason771ed682008-11-06 22:02:51 -0500652 /* did the compression code fall back to uncompressed IO? */
653 if (!async_extent->pages) {
654 int page_started = 0;
655 unsigned long nr_written = 0;
656
657 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000658 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100659 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500660
661 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500662 ret = cow_file_range(inode, async_cow->locked_page,
663 async_extent->start,
664 async_extent->start +
665 async_extent->ram_size - 1,
666 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500667
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100668 /* JDM XXX */
669
Chris Mason771ed682008-11-06 22:02:51 -0500670 /*
671 * if page_started, cow_file_range inserted an
672 * inline extent and took care of all the unlocking
673 * and IO for us. Otherwise, we need to submit
674 * all those pages down to the drive.
675 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500676 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500677 extent_write_locked_range(io_tree,
678 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500679 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500680 async_extent->ram_size - 1,
681 btrfs_get_extent,
682 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500683 else if (ret)
684 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500685 kfree(async_extent);
686 cond_resched();
687 continue;
688 }
689
690 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100691 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500692
Josef Bacik00361582013-08-14 14:02:47 -0400693 ret = btrfs_reserve_extent(root,
Chris Mason771ed682008-11-06 22:02:51 -0500694 async_extent->compressed_size,
695 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800696 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500697 if (ret) {
698 int i;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500699
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500700 for (i = 0; i < async_extent->nr_pages; i++) {
701 WARN_ON(async_extent->pages[i]->mapping);
702 page_cache_release(async_extent->pages[i]);
703 }
704 kfree(async_extent->pages);
705 async_extent->nr_pages = 0;
706 async_extent->pages = NULL;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500707
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400708 if (ret == -ENOSPC) {
709 unlock_extent(io_tree, async_extent->start,
710 async_extent->start +
711 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800712
713 /*
714 * we need to redirty the pages if we decide to
715 * fallback to uncompressed IO, otherwise we
716 * will not submit these pages down to lower
717 * layers.
718 */
719 extent_range_redirty_for_io(inode,
720 async_extent->start,
721 async_extent->start +
722 async_extent->ram_size - 1);
723
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100724 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400725 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500726 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500727 }
728
Yan, Zhengc2167752009-11-12 09:34:21 +0000729 /*
730 * here we're doing allocation and writeback of the
731 * compressed pages
732 */
733 btrfs_drop_extent_cache(inode, async_extent->start,
734 async_extent->start +
735 async_extent->ram_size - 1, 0);
736
David Sterba172ddd62011-04-21 00:48:27 +0200737 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000738 if (!em) {
739 ret = -ENOMEM;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500740 goto out_free_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000741 }
Chris Mason771ed682008-11-06 22:02:51 -0500742 em->start = async_extent->start;
743 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500744 em->orig_start = em->start;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400745 em->mod_start = em->start;
746 em->mod_len = em->len;
Chris Mason771ed682008-11-06 22:02:51 -0500747
748 em->block_start = ins.objectid;
749 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500750 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400751 em->ram_bytes = async_extent->ram_size;
Chris Mason771ed682008-11-06 22:02:51 -0500752 em->bdev = root->fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800753 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500754 set_bit(EXTENT_FLAG_PINNED, &em->flags);
755 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400756 em->generation = -1;
Chris Mason771ed682008-11-06 22:02:51 -0500757
Chris Masond3977122009-01-05 21:25:51 -0500758 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400759 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400760 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400761 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500762 if (ret != -EEXIST) {
763 free_extent_map(em);
764 break;
765 }
766 btrfs_drop_extent_cache(inode, async_extent->start,
767 async_extent->start +
768 async_extent->ram_size - 1, 0);
769 }
770
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500771 if (ret)
772 goto out_free_reserve;
773
Li Zefan261507a02010-12-17 14:21:50 +0800774 ret = btrfs_add_ordered_extent_compress(inode,
775 async_extent->start,
776 ins.objectid,
777 async_extent->ram_size,
778 ins.offset,
779 BTRFS_ORDERED_COMPRESSED,
780 async_extent->compress_type);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500781 if (ret)
782 goto out_free_reserve;
Chris Mason771ed682008-11-06 22:02:51 -0500783
Chris Mason771ed682008-11-06 22:02:51 -0500784 /*
785 * clear dirty, set writeback and unlock the pages.
786 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400787 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400788 async_extent->start +
789 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400790 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
791 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400792 PAGE_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500793 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500794 async_extent->start,
795 async_extent->ram_size,
796 ins.objectid,
797 ins.offset, async_extent->pages,
798 async_extent->nr_pages);
Chris Mason771ed682008-11-06 22:02:51 -0500799 alloc_hint = ins.objectid + ins.offset;
800 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500801 if (ret)
802 goto out;
Chris Mason771ed682008-11-06 22:02:51 -0500803 cond_resched();
804 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100805 ret = 0;
806out:
807 return ret;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500808out_free_reserve:
Miao Xiee570fd22014-06-19 10:42:50 +0800809 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100810out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400811 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500812 async_extent->start +
813 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400814 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400815 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
816 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
817 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100818 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500819 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500820}
821
Josef Bacik4b46fce2010-05-23 11:00:55 -0400822static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
823 u64 num_bytes)
824{
825 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
826 struct extent_map *em;
827 u64 alloc_hint = 0;
828
829 read_lock(&em_tree->lock);
830 em = search_extent_mapping(em_tree, start, num_bytes);
831 if (em) {
832 /*
833 * if block start isn't an actual block number then find the
834 * first block in this inode and use that as a hint. If that
835 * block is also bogus then just don't worry about it.
836 */
837 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
838 free_extent_map(em);
839 em = search_extent_mapping(em_tree, 0, 0);
840 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
841 alloc_hint = em->block_start;
842 if (em)
843 free_extent_map(em);
844 } else {
845 alloc_hint = em->block_start;
846 free_extent_map(em);
847 }
848 }
849 read_unlock(&em_tree->lock);
850
851 return alloc_hint;
852}
853
Chris Mason771ed682008-11-06 22:02:51 -0500854/*
855 * when extent_io.c finds a delayed allocation range in the file,
856 * the call backs end up in this code. The basic idea is to
857 * allocate extents on disk for the range, and create ordered data structs
858 * in ram to track those extents.
859 *
860 * locked_page is the page that writepage had locked already. We use
861 * it to make sure we don't do extra locks or unlocks.
862 *
863 * *page_started is set to one if we unlock locked_page and do everything
864 * required to start IO on it. It may be clean and already done with
865 * IO when we return.
866 */
Josef Bacik00361582013-08-14 14:02:47 -0400867static noinline int cow_file_range(struct inode *inode,
868 struct page *locked_page,
869 u64 start, u64 end, int *page_started,
870 unsigned long *nr_written,
871 int unlock)
Chris Mason771ed682008-11-06 22:02:51 -0500872{
Josef Bacik00361582013-08-14 14:02:47 -0400873 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500874 u64 alloc_hint = 0;
875 u64 num_bytes;
876 unsigned long ram_size;
877 u64 disk_num_bytes;
878 u64 cur_alloc_size;
879 u64 blocksize = root->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500880 struct btrfs_key ins;
881 struct extent_map *em;
882 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
883 int ret = 0;
884
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400885 if (btrfs_is_free_space_inode(inode)) {
886 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500887 ret = -EINVAL;
888 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400889 }
Chris Mason771ed682008-11-06 22:02:51 -0500890
Qu Wenruofda28322013-02-26 08:10:22 +0000891 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500892 num_bytes = max(blocksize, num_bytes);
893 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500894
Chris Mason4cb53002011-05-24 15:35:30 -0400895 /* if this is a small write inside eof, kick off defrag */
Liu Bo4cb13e52012-03-29 09:57:45 -0400896 if (num_bytes < 64 * 1024 &&
897 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Josef Bacik00361582013-08-14 14:02:47 -0400898 btrfs_add_inode_defrag(NULL, inode);
Chris Mason4cb53002011-05-24 15:35:30 -0400899
Chris Mason771ed682008-11-06 22:02:51 -0500900 if (start == 0) {
901 /* lets try to make an inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400902 ret = cow_file_range_inline(root, inode, start, end, 0, 0,
903 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500904 if (ret == 0) {
Josef Bacikc2790a22013-07-29 11:20:47 -0400905 extent_clear_unlock_delalloc(inode, start, end, NULL,
906 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400907 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400908 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
909 PAGE_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000910
Chris Mason771ed682008-11-06 22:02:51 -0500911 *nr_written = *nr_written +
912 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
913 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500914 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100915 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100916 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500917 }
918 }
Chris Masonc8b97812008-10-29 14:49:59 -0400919
920 BUG_ON(disk_num_bytes >
David Sterba6c417612011-04-13 15:41:04 +0200921 btrfs_super_total_bytes(root->fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400922
Josef Bacik4b46fce2010-05-23 11:00:55 -0400923 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400924 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400925
Chris Masond3977122009-01-05 21:25:51 -0500926 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400927 unsigned long op;
928
Josef Bacik287a0ab2010-03-19 18:07:23 +0000929 cur_alloc_size = disk_num_bytes;
Josef Bacik00361582013-08-14 14:02:47 -0400930 ret = btrfs_reserve_extent(root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500931 root->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +0800932 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -0400933 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100934 goto out_unlock;
Chris Masond3977122009-01-05 21:25:51 -0500935
David Sterba172ddd62011-04-21 00:48:27 +0200936 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000937 if (!em) {
938 ret = -ENOMEM;
Liu Boace68ba2013-04-22 10:53:47 +0000939 goto out_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000940 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400941 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500942 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500943 ram_size = ins.offset;
944 em->len = ins.offset;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400945 em->mod_start = em->start;
946 em->mod_len = em->len;
Chris Masonc8b97812008-10-29 14:49:59 -0400947
Chris Masone6dcd2d2008-07-17 12:53:50 -0400948 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400949 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500950 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400951 em->ram_bytes = ram_size;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400952 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400953 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400954 em->generation = -1;
Chris Masonc8b97812008-10-29 14:49:59 -0400955
Chris Masond3977122009-01-05 21:25:51 -0500956 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400957 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400958 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400959 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400960 if (ret != -EEXIST) {
961 free_extent_map(em);
962 break;
963 }
964 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400965 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400966 }
Liu Boace68ba2013-04-22 10:53:47 +0000967 if (ret)
968 goto out_reserve;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400969
Chris Mason98d20f62008-04-14 09:46:10 -0400970 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400971 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -0500972 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +0000973 if (ret)
974 goto out_reserve;
Chris Masonc8b97812008-10-29 14:49:59 -0400975
Yan Zheng17d217f2008-12-12 10:03:38 -0500976 if (root->root_key.objectid ==
977 BTRFS_DATA_RELOC_TREE_OBJECTID) {
978 ret = btrfs_reloc_clone_csums(inode, start,
979 cur_alloc_size);
Josef Bacik00361582013-08-14 14:02:47 -0400980 if (ret)
Liu Boace68ba2013-04-22 10:53:47 +0000981 goto out_reserve;
Yan Zheng17d217f2008-12-12 10:03:38 -0500982 }
983
Chris Masond3977122009-01-05 21:25:51 -0500984 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -0400985 break;
Chris Masond3977122009-01-05 21:25:51 -0500986
Chris Masonc8b97812008-10-29 14:49:59 -0400987 /* we're not doing compressed IO, don't unlock the first
988 * page (which the caller expects to stay locked), don't
989 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -0400990 *
991 * Do set the Private2 bit so we know this page was properly
992 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -0400993 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400994 op = unlock ? PAGE_UNLOCK : 0;
995 op |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -0400996
Josef Bacikc2790a22013-07-29 11:20:47 -0400997 extent_clear_unlock_delalloc(inode, start,
998 start + ram_size - 1, locked_page,
999 EXTENT_LOCKED | EXTENT_DELALLOC,
1000 op);
Chris Masonc8b97812008-10-29 14:49:59 -04001001 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001002 num_bytes -= cur_alloc_size;
1003 alloc_hint = ins.objectid + ins.offset;
1004 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -04001005 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001006out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001007 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001008
Liu Boace68ba2013-04-22 10:53:47 +00001009out_reserve:
Miao Xiee570fd22014-06-19 10:42:50 +08001010 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001011out_unlock:
Josef Bacikc2790a22013-07-29 11:20:47 -04001012 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Josef Bacik151a41b2013-07-29 13:22:24 -04001013 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
1014 EXTENT_DELALLOC | EXTENT_DEFRAG,
1015 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
1016 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001017 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001018}
Chris Masonc8b97812008-10-29 14:49:59 -04001019
Chris Mason771ed682008-11-06 22:02:51 -05001020/*
1021 * work queue call back to started compression on a file and pages
1022 */
1023static noinline void async_cow_start(struct btrfs_work *work)
1024{
1025 struct async_cow *async_cow;
1026 int num_added = 0;
1027 async_cow = container_of(work, struct async_cow, work);
1028
1029 compress_file_range(async_cow->inode, async_cow->locked_page,
1030 async_cow->start, async_cow->end, async_cow,
1031 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001032 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001033 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001034 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001035 }
Chris Mason771ed682008-11-06 22:02:51 -05001036}
1037
1038/*
1039 * work queue call back to submit previously compressed pages
1040 */
1041static noinline void async_cow_submit(struct btrfs_work *work)
1042{
1043 struct async_cow *async_cow;
1044 struct btrfs_root *root;
1045 unsigned long nr_pages;
1046
1047 async_cow = container_of(work, struct async_cow, work);
1048
1049 root = async_cow->root;
1050 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
1051 PAGE_CACHE_SHIFT;
1052
Josef Bacik66657b32012-08-01 15:36:24 -04001053 if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) <
Liu Bo287082b2012-06-28 04:02:24 -06001054 5 * 1024 * 1024 &&
Chris Mason771ed682008-11-06 22:02:51 -05001055 waitqueue_active(&root->fs_info->async_submit_wait))
1056 wake_up(&root->fs_info->async_submit_wait);
1057
Chris Masond3977122009-01-05 21:25:51 -05001058 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001059 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001060}
Chris Masonc8b97812008-10-29 14:49:59 -04001061
Chris Mason771ed682008-11-06 22:02:51 -05001062static noinline void async_cow_free(struct btrfs_work *work)
1063{
1064 struct async_cow *async_cow;
1065 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001066 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001067 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001068 kfree(async_cow);
1069}
1070
1071static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1072 u64 start, u64 end, int *page_started,
1073 unsigned long *nr_written)
1074{
1075 struct async_cow *async_cow;
1076 struct btrfs_root *root = BTRFS_I(inode)->root;
1077 unsigned long nr_pages;
1078 u64 cur_end;
Liu Bo287082b2012-06-28 04:02:24 -06001079 int limit = 10 * 1024 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -05001080
Chris Masona3429ab2009-10-08 12:30:20 -04001081 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1082 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001083 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001084 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001085 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001086 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001087 async_cow->root = root;
1088 async_cow->locked_page = locked_page;
1089 async_cow->start = start;
1090
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001091 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
Chris Mason771ed682008-11-06 22:02:51 -05001092 cur_end = end;
1093 else
1094 cur_end = min(end, start + 512 * 1024 - 1);
1095
1096 async_cow->end = cur_end;
1097 INIT_LIST_HEAD(&async_cow->extents);
1098
Liu Bo9e0af232014-08-15 23:36:53 +08001099 btrfs_init_work(&async_cow->work,
1100 btrfs_delalloc_helper,
1101 async_cow_start, async_cow_submit,
1102 async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001103
Chris Mason771ed682008-11-06 22:02:51 -05001104 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
1105 PAGE_CACHE_SHIFT;
1106 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
1107
Qu Wenruoafe3d242014-02-28 10:46:07 +08001108 btrfs_queue_work(root->fs_info->delalloc_workers,
1109 &async_cow->work);
Chris Mason771ed682008-11-06 22:02:51 -05001110
1111 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
1112 wait_event(root->fs_info->async_submit_wait,
1113 (atomic_read(&root->fs_info->async_delalloc_pages) <
1114 limit));
1115 }
1116
Chris Masond3977122009-01-05 21:25:51 -05001117 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -05001118 atomic_read(&root->fs_info->async_delalloc_pages)) {
1119 wait_event(root->fs_info->async_submit_wait,
1120 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1121 0));
1122 }
1123
1124 *nr_written += nr_pages;
1125 start = cur_end + 1;
1126 }
1127 *page_started = 1;
1128 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001129}
1130
Chris Masond3977122009-01-05 21:25:51 -05001131static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001132 u64 bytenr, u64 num_bytes)
1133{
1134 int ret;
1135 struct btrfs_ordered_sum *sums;
1136 LIST_HEAD(list);
1137
Yan Zheng07d400a2009-01-06 11:42:00 -05001138 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001139 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001140 if (ret == 0 && list_empty(&list))
1141 return 0;
1142
1143 while (!list_empty(&list)) {
1144 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1145 list_del(&sums->list);
1146 kfree(sums);
1147 }
1148 return 1;
1149}
1150
Chris Masond352ac62008-09-29 15:18:18 -04001151/*
1152 * when nowcow writeback call back. This checks for snapshots or COW copies
1153 * of the extents that exist in the file, and COWs the file as required.
1154 *
1155 * If no cow copies or snapshots exist, we write directly to the existing
1156 * blocks on disk
1157 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001158static noinline int run_delalloc_nocow(struct inode *inode,
1159 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001160 u64 start, u64 end, int *page_started, int force,
1161 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001162{
Chris Masonbe20aa92007-12-17 20:14:01 -05001163 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001164 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001165 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001166 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001167 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001168 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001169 u64 cow_start;
1170 u64 cur_offset;
1171 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001172 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001173 u64 disk_bytenr;
1174 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001175 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001176 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001177 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001178 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001179 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001180 int nocow;
1181 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001182 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001183 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001184
1185 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001186 if (!path) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001187 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1188 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001189 EXTENT_DO_ACCOUNTING |
1190 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001191 PAGE_CLEAR_DIRTY |
1192 PAGE_SET_WRITEBACK |
1193 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001194 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001195 }
Li Zefan82d59022011-04-20 10:33:24 +08001196
Liu Bo83eea1f2012-07-10 05:28:39 -06001197 nolock = btrfs_is_free_space_inode(inode);
Li Zefan82d59022011-04-20 10:33:24 +08001198
1199 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001200 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001201 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001202 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001203
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001204 if (IS_ERR(trans)) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001205 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1206 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001207 EXTENT_DO_ACCOUNTING |
1208 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001209 PAGE_CLEAR_DIRTY |
1210 PAGE_SET_WRITEBACK |
1211 PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001212 btrfs_free_path(path);
1213 return PTR_ERR(trans);
1214 }
1215
Josef Bacik74b21072011-04-13 12:02:53 -04001216 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001217
Yan Zheng80ff3852008-10-30 14:20:02 -04001218 cow_start = (u64)-1;
1219 cur_offset = start;
1220 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001221 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001222 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001223 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001224 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001225 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1226 leaf = path->nodes[0];
1227 btrfs_item_key_to_cpu(leaf, &found_key,
1228 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001229 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001230 found_key.type == BTRFS_EXTENT_DATA_KEY)
1231 path->slots[0]--;
1232 }
1233 check_prev = 0;
1234next_slot:
1235 leaf = path->nodes[0];
1236 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1237 ret = btrfs_next_leaf(root, path);
Josef Bacikd788a342013-10-25 16:55:08 -04001238 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001239 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001240 if (ret > 0)
1241 break;
1242 leaf = path->nodes[0];
1243 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001244
Yan Zheng80ff3852008-10-30 14:20:02 -04001245 nocow = 0;
1246 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001247 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001248 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001249
Li Zefan33345d012011-04-20 10:31:50 +08001250 if (found_key.objectid > ino ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001251 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1252 found_key.offset > end)
1253 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001254
Yan Zheng80ff3852008-10-30 14:20:02 -04001255 if (found_key.offset > cur_offset) {
1256 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001257 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001258 goto out_check;
1259 }
Chris Masonc31f8832008-01-08 15:46:31 -05001260
Yan Zheng80ff3852008-10-30 14:20:02 -04001261 fi = btrfs_item_ptr(leaf, path->slots[0],
1262 struct btrfs_file_extent_item);
1263 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001264
Josef Bacikcc95bef2013-04-04 14:31:27 -04001265 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001266 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1267 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001268 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001269 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001270 extent_end = found_key.offset +
1271 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001272 disk_num_bytes =
1273 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001274 if (extent_end <= start) {
1275 path->slots[0]++;
1276 goto next_slot;
1277 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001278 if (disk_bytenr == 0)
1279 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001280 if (btrfs_file_extent_compression(leaf, fi) ||
1281 btrfs_file_extent_encryption(leaf, fi) ||
1282 btrfs_file_extent_other_encoding(leaf, fi))
1283 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001284 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1285 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001286 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001287 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001288 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001289 found_key.offset -
1290 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001291 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001292 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001293 disk_bytenr += cur_offset - found_key.offset;
1294 num_bytes = min(end + 1, extent_end) - cur_offset;
1295 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001296 * if there are pending snapshots for this root,
1297 * we fall into common COW way.
1298 */
1299 if (!nolock) {
1300 err = btrfs_start_nocow_write(root);
1301 if (!err)
1302 goto out_check;
1303 }
1304 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001305 * force cow if csum exists in the range.
1306 * this ensure that csum for a given extent are
1307 * either valid or do not exist.
1308 */
1309 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1310 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001311 nocow = 1;
1312 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1313 extent_end = found_key.offset +
Chris Mason514ac8a2014-01-03 21:07:00 -08001314 btrfs_file_extent_inline_len(leaf,
1315 path->slots[0], fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001316 extent_end = ALIGN(extent_end, root->sectorsize);
1317 } else {
1318 BUG_ON(1);
1319 }
1320out_check:
1321 if (extent_end <= start) {
1322 path->slots[0]++;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001323 if (!nolock && nocow)
1324 btrfs_end_nocow_write(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001325 goto next_slot;
1326 }
1327 if (!nocow) {
1328 if (cow_start == (u64)-1)
1329 cow_start = cur_offset;
1330 cur_offset = extent_end;
1331 if (cur_offset > end)
1332 break;
1333 path->slots[0]++;
1334 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001335 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001336
David Sterbab3b4aa72011-04-21 01:20:15 +02001337 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001338 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001339 ret = cow_file_range(inode, locked_page,
1340 cow_start, found_key.offset - 1,
1341 page_started, nr_written, 1);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001342 if (ret) {
1343 if (!nolock && nocow)
1344 btrfs_end_nocow_write(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001345 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001346 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001347 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001348 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001349
Yan Zhengd899e052008-10-30 14:25:28 -04001350 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1351 struct extent_map *em;
1352 struct extent_map_tree *em_tree;
1353 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001354 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001355 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001356 em->start = cur_offset;
Josef Bacik70c8a912012-10-11 16:54:30 -04001357 em->orig_start = found_key.offset - extent_offset;
Yan Zhengd899e052008-10-30 14:25:28 -04001358 em->len = num_bytes;
1359 em->block_len = num_bytes;
1360 em->block_start = disk_bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05001361 em->orig_block_len = disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001362 em->ram_bytes = ram_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04001363 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001364 em->mod_start = em->start;
1365 em->mod_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04001366 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacikb11e2342012-12-03 10:58:15 -05001367 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001368 em->generation = -1;
Yan Zhengd899e052008-10-30 14:25:28 -04001369 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001370 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001371 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001372 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001373 if (ret != -EEXIST) {
1374 free_extent_map(em);
1375 break;
1376 }
1377 btrfs_drop_extent_cache(inode, em->start,
1378 em->start + em->len - 1, 0);
1379 }
1380 type = BTRFS_ORDERED_PREALLOC;
1381 } else {
1382 type = BTRFS_ORDERED_NOCOW;
1383 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001384
1385 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001386 num_bytes, num_bytes, type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001387 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001388
Yan, Zhengefa56462010-05-16 10:49:59 -04001389 if (root->root_key.objectid ==
1390 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1391 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1392 num_bytes);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001393 if (ret) {
1394 if (!nolock && nocow)
1395 btrfs_end_nocow_write(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001396 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001397 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001398 }
1399
Josef Bacikc2790a22013-07-29 11:20:47 -04001400 extent_clear_unlock_delalloc(inode, cur_offset,
1401 cur_offset + num_bytes - 1,
1402 locked_page, EXTENT_LOCKED |
1403 EXTENT_DELALLOC, PAGE_UNLOCK |
1404 PAGE_SET_PRIVATE2);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001405 if (!nolock && nocow)
1406 btrfs_end_nocow_write(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001407 cur_offset = extent_end;
1408 if (cur_offset > end)
1409 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001410 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001411 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001412
Josef Bacik17ca04a2012-05-31 15:58:55 -04001413 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001414 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001415 cur_offset = end;
1416 }
1417
Yan Zheng80ff3852008-10-30 14:20:02 -04001418 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001419 ret = cow_file_range(inode, locked_page, cow_start, end,
1420 page_started, nr_written, 1);
Josef Bacikd788a342013-10-25 16:55:08 -04001421 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001422 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001423 }
1424
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001425error:
Miao Xiea698d0752012-09-20 01:51:59 -06001426 err = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001427 if (!ret)
1428 ret = err;
1429
Josef Bacik17ca04a2012-05-31 15:58:55 -04001430 if (ret && cur_offset < end)
Josef Bacikc2790a22013-07-29 11:20:47 -04001431 extent_clear_unlock_delalloc(inode, cur_offset, end,
1432 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001433 EXTENT_DELALLOC | EXTENT_DEFRAG |
1434 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1435 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001436 PAGE_SET_WRITEBACK |
1437 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001438 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001439 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001440}
1441
Chris Masond352ac62008-09-29 15:18:18 -04001442/*
1443 * extent_io.c call back to do delayed allocation processing
1444 */
Chris Masonc8b97812008-10-29 14:49:59 -04001445static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001446 u64 start, u64 end, int *page_started,
1447 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001448{
Chris Masonbe20aa92007-12-17 20:14:01 -05001449 int ret;
Chris Mason7f366cf2009-03-12 20:12:45 -04001450 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona2135012008-06-25 16:01:30 -04001451
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001452 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) {
Chris Masonc8b97812008-10-29 14:49:59 -04001453 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001454 page_started, 1, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001455 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001456 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001457 page_started, 0, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001458 } else if (!btrfs_test_opt(root, COMPRESS) &&
1459 !(BTRFS_I(inode)->force_compress) &&
1460 !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS)) {
Chris Mason7f366cf2009-03-12 20:12:45 -04001461 ret = cow_file_range(inode, locked_page, start, end,
1462 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001463 } else {
1464 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1465 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001466 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001467 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001468 }
Chris Masonb888db22007-08-27 16:49:44 -04001469 return ret;
1470}
1471
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001472static void btrfs_split_extent_hook(struct inode *inode,
1473 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001474{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001475 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001476 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001477 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001478
Josef Bacik9e0baf62011-07-15 15:16:44 +00001479 spin_lock(&BTRFS_I(inode)->lock);
1480 BTRFS_I(inode)->outstanding_extents++;
1481 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001482}
1483
1484/*
1485 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1486 * extents so we can keep track of new extents that are just merged onto old
1487 * extents, such as when we are doing sequential writes, so we can properly
1488 * account for the metadata space we'll need.
1489 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001490static void btrfs_merge_extent_hook(struct inode *inode,
1491 struct extent_state *new,
1492 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001493{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001494 /* not delalloc, ignore it */
1495 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001496 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001497
Josef Bacik9e0baf62011-07-15 15:16:44 +00001498 spin_lock(&BTRFS_I(inode)->lock);
1499 BTRFS_I(inode)->outstanding_extents--;
1500 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001501}
1502
Miao Xieeb73c1b2013-05-15 07:48:22 +00001503static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1504 struct inode *inode)
1505{
1506 spin_lock(&root->delalloc_lock);
1507 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1508 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1509 &root->delalloc_inodes);
1510 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1511 &BTRFS_I(inode)->runtime_flags);
1512 root->nr_delalloc_inodes++;
1513 if (root->nr_delalloc_inodes == 1) {
1514 spin_lock(&root->fs_info->delalloc_root_lock);
1515 BUG_ON(!list_empty(&root->delalloc_root));
1516 list_add_tail(&root->delalloc_root,
1517 &root->fs_info->delalloc_roots);
1518 spin_unlock(&root->fs_info->delalloc_root_lock);
1519 }
1520 }
1521 spin_unlock(&root->delalloc_lock);
1522}
1523
1524static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1525 struct inode *inode)
1526{
1527 spin_lock(&root->delalloc_lock);
1528 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1529 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1530 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1531 &BTRFS_I(inode)->runtime_flags);
1532 root->nr_delalloc_inodes--;
1533 if (!root->nr_delalloc_inodes) {
1534 spin_lock(&root->fs_info->delalloc_root_lock);
1535 BUG_ON(list_empty(&root->delalloc_root));
1536 list_del_init(&root->delalloc_root);
1537 spin_unlock(&root->fs_info->delalloc_root_lock);
1538 }
1539 }
1540 spin_unlock(&root->delalloc_lock);
1541}
1542
Chris Masond352ac62008-09-29 15:18:18 -04001543/*
1544 * extent_io.c set_bit_hook, used to track delayed allocation
1545 * bytes in this file, and to maintain the list of inodes that
1546 * have pending delalloc work to be done.
1547 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001548static void btrfs_set_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001549 struct extent_state *state, unsigned long *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001550{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001551
Chris Mason75eff682008-12-15 15:54:40 -05001552 /*
1553 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001554 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001555 * bit, which is only set or cleared with irqs on
1556 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001557 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001558 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001559 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001560 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001561
Josef Bacik9e0baf62011-07-15 15:16:44 +00001562 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001563 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001564 } else {
1565 spin_lock(&BTRFS_I(inode)->lock);
1566 BTRFS_I(inode)->outstanding_extents++;
1567 spin_unlock(&BTRFS_I(inode)->lock);
1568 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001569
Miao Xie963d6782013-01-29 10:10:51 +00001570 __percpu_counter_add(&root->fs_info->delalloc_bytes, len,
1571 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001572 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001573 BTRFS_I(inode)->delalloc_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001574 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001575 &BTRFS_I(inode)->runtime_flags))
1576 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001577 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001578 }
Chris Mason291d6732008-01-29 15:55:23 -05001579}
1580
Chris Masond352ac62008-09-29 15:18:18 -04001581/*
1582 * extent_io.c clear_bit_hook, see set_bit_hook for why
1583 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001584static void btrfs_clear_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001585 struct extent_state *state,
1586 unsigned long *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001587{
Chris Mason75eff682008-12-15 15:54:40 -05001588 /*
1589 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001590 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001591 * bit, which is only set or cleared with irqs on
1592 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001593 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001594 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001595 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001596 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001597
Josef Bacik9e0baf62011-07-15 15:16:44 +00001598 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001599 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001600 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1601 spin_lock(&BTRFS_I(inode)->lock);
1602 BTRFS_I(inode)->outstanding_extents--;
1603 spin_unlock(&BTRFS_I(inode)->lock);
1604 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001605
Josef Bacikb6d08f02013-09-27 14:57:43 -04001606 /*
1607 * We don't reserve metadata space for space cache inodes so we
1608 * don't need to call dellalloc_release_metadata if there is an
1609 * error.
1610 */
1611 if (*bits & EXTENT_DO_ACCOUNTING &&
1612 root != root->fs_info->tree_root)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001613 btrfs_delalloc_release_metadata(inode, len);
1614
Josef Bacik0cb59c92010-07-02 12:14:14 -04001615 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Josef Bacik7ee9e442013-06-21 16:37:03 -04001616 && do_list && !(state->state & EXTENT_NORESERVE))
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001617 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001618
Miao Xie963d6782013-01-29 10:10:51 +00001619 __percpu_counter_add(&root->fs_info->delalloc_bytes, -len,
1620 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001621 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001622 BTRFS_I(inode)->delalloc_bytes -= len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001623 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001624 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001625 &BTRFS_I(inode)->runtime_flags))
1626 btrfs_del_delalloc_inode(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001627 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001628 }
Chris Mason291d6732008-01-29 15:55:23 -05001629}
1630
Chris Masond352ac62008-09-29 15:18:18 -04001631/*
1632 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1633 * we don't create bios that span stripes or chunks
1634 */
David Woodhouse64a16702009-07-15 23:29:37 +01001635int btrfs_merge_bio_hook(int rw, struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001636 size_t size, struct bio *bio,
1637 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001638{
1639 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001640 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001641 u64 length = 0;
1642 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001643 int ret;
1644
Chris Mason771ed682008-11-06 22:02:51 -05001645 if (bio_flags & EXTENT_BIO_COMPRESSED)
1646 return 0;
1647
Kent Overstreet4f024f32013-10-11 15:44:27 -07001648 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001649 map_length = length;
David Woodhouse64a16702009-07-15 23:29:37 +01001650 ret = btrfs_map_block(root->fs_info, rw, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001651 &map_length, NULL, 0);
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001652 /* Will always return 0 with map_multi == NULL */
Jeff Mahoney3444a972011-10-03 23:23:13 -04001653 BUG_ON(ret < 0);
Chris Masond3977122009-01-05 21:25:51 -05001654 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001655 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001656 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001657}
1658
Chris Masond352ac62008-09-29 15:18:18 -04001659/*
1660 * in order to insert checksums into the metadata in large chunks,
1661 * we wait until bio submission time. All the pages in the bio are
1662 * checksummed and sums are attached onto the ordered extent record.
1663 *
1664 * At IO completion time the cums attached on the ordered extent record
1665 * are inserted into the btree
1666 */
Chris Masond3977122009-01-05 21:25:51 -05001667static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1668 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001669 unsigned long bio_flags,
1670 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001671{
Chris Mason065631f2008-02-20 12:07:25 -05001672 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001673 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001674
Chris Masond20f7042008-12-08 16:58:54 -05001675 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001676 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001677 return 0;
1678}
Chris Masone0156402008-04-16 11:15:20 -04001679
Chris Mason4a69a412008-11-06 22:03:00 -05001680/*
1681 * in order to insert checksums into the metadata in large chunks,
1682 * we wait until bio submission time. All the pages in the bio are
1683 * checksummed and sums are attached onto the ordered extent record.
1684 *
1685 * At IO completion time the cums attached on the ordered extent record
1686 * are inserted into the btree
1687 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001688static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001689 int mirror_num, unsigned long bio_flags,
1690 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001691{
1692 struct btrfs_root *root = BTRFS_I(inode)->root;
Stefan Behrens61891922012-11-05 18:51:52 +01001693 int ret;
1694
1695 ret = btrfs_map_bio(root, rw, bio, mirror_num, 1);
1696 if (ret)
1697 bio_endio(bio, ret);
1698 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001699}
1700
Chris Masond352ac62008-09-29 15:18:18 -04001701/*
Chris Masoncad321a2008-12-17 14:51:42 -05001702 * extent_io.c submission hook. This does the right thing for csum calculation
1703 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001704 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001705static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001706 int mirror_num, unsigned long bio_flags,
1707 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001708{
1709 struct btrfs_root *root = BTRFS_I(inode)->root;
1710 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001711 int skip_sum;
Jeff Mahoney04173412011-10-03 23:23:12 -04001712 int metadata = 0;
Josef Bacikb812ce22012-11-16 13:56:32 -05001713 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001714
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001715 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001716
Liu Bo83eea1f2012-07-10 05:28:39 -06001717 if (btrfs_is_free_space_inode(inode))
Jeff Mahoney04173412011-10-03 23:23:12 -04001718 metadata = 2;
1719
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001720 if (!(rw & REQ_WRITE)) {
Josef Bacik5fd02042012-05-02 14:00:54 -04001721 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1722 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001723 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001724
Chris Masond20f7042008-12-08 16:58:54 -05001725 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001726 ret = btrfs_submit_compressed_read(inode, bio,
1727 mirror_num,
1728 bio_flags);
1729 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001730 } else if (!skip_sum) {
1731 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1732 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001733 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001734 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001735 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001736 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001737 /* csum items have already been cloned */
1738 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1739 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001740 /* we're doing a write, do the async checksumming */
Stefan Behrens61891922012-11-05 18:51:52 +01001741 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001742 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001743 bio_flags, bio_offset,
1744 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001745 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001746 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001747 } else if (!skip_sum) {
1748 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1749 if (ret)
1750 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001751 }
1752
Chris Mason0b86a832008-03-24 15:01:56 -04001753mapit:
Stefan Behrens61891922012-11-05 18:51:52 +01001754 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
1755
1756out:
1757 if (ret < 0)
1758 bio_endio(bio, ret);
1759 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001760}
Chris Mason6885f302008-02-20 16:11:05 -05001761
Chris Masond352ac62008-09-29 15:18:18 -04001762/*
1763 * given a list of ordered sums record them in the inode. This happens
1764 * at IO completion time based on sums calculated at bio submission time.
1765 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001766static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001767 struct inode *inode, u64 file_offset,
1768 struct list_head *list)
1769{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001770 struct btrfs_ordered_sum *sum;
1771
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001772 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00001773 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05001774 btrfs_csum_file_blocks(trans,
1775 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00001776 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001777 }
1778 return 0;
1779}
1780
Josef Bacik2ac55d42010-02-03 19:33:23 +00001781int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1782 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001783{
Julia Lawall6c1500f2012-11-03 20:30:18 +00001784 WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04001785 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001786 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001787}
1788
Chris Masond352ac62008-09-29 15:18:18 -04001789/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001790struct btrfs_writepage_fixup {
1791 struct page *page;
1792 struct btrfs_work work;
1793};
1794
Christoph Hellwigb2950862008-12-02 09:54:17 -05001795static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001796{
1797 struct btrfs_writepage_fixup *fixup;
1798 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001799 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001800 struct page *page;
1801 struct inode *inode;
1802 u64 page_start;
1803 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001804 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001805
1806 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1807 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001808again:
Chris Mason247e7432008-07-17 12:53:51 -04001809 lock_page(page);
1810 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1811 ClearPageChecked(page);
1812 goto out_page;
1813 }
1814
1815 inode = page->mapping->host;
1816 page_start = page_offset(page);
1817 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1818
Josef Bacik2ac55d42010-02-03 19:33:23 +00001819 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001820 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001821
1822 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001823 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001824 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001825
1826 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1827 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001828 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1829 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001830 unlock_page(page);
1831 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001832 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04001833 goto again;
1834 }
Chris Mason247e7432008-07-17 12:53:51 -04001835
Jeff Mahoney87826df2012-02-15 16:23:57 +01001836 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
1837 if (ret) {
1838 mapping_set_error(page->mapping, ret);
1839 end_extent_writepage(page, ret, page_start, page_end);
1840 ClearPageChecked(page);
1841 goto out;
1842 }
1843
Josef Bacik2ac55d42010-02-03 19:33:23 +00001844 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001845 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001846 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04001847out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001848 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1849 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001850out_page:
1851 unlock_page(page);
1852 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08001853 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04001854}
1855
1856/*
1857 * There are a few paths in the higher layers of the kernel that directly
1858 * set the page dirty bit without asking the filesystem if it is a
1859 * good idea. This causes problems because we want to make sure COW
1860 * properly happens and the data=ordered rules are followed.
1861 *
Chris Masonc8b97812008-10-29 14:49:59 -04001862 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001863 * hasn't been properly setup for IO. We kick off an async process
1864 * to fix it up. The async helper will wait for ordered extents, set
1865 * the delalloc bit and make it safe to write the page.
1866 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001867static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001868{
1869 struct inode *inode = page->mapping->host;
1870 struct btrfs_writepage_fixup *fixup;
1871 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001872
Chris Mason8b62b722009-09-02 16:53:46 -04001873 /* this page is properly in the ordered list */
1874 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001875 return 0;
1876
1877 if (PageChecked(page))
1878 return -EAGAIN;
1879
1880 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1881 if (!fixup)
1882 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001883
Chris Mason247e7432008-07-17 12:53:51 -04001884 SetPageChecked(page);
1885 page_cache_get(page);
Liu Bo9e0af232014-08-15 23:36:53 +08001886 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
1887 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04001888 fixup->page = page;
Qu Wenruodc6e3202014-02-28 10:46:14 +08001889 btrfs_queue_work(root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001890 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04001891}
1892
Yan Zhengd899e052008-10-30 14:25:28 -04001893static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1894 struct inode *inode, u64 file_pos,
1895 u64 disk_bytenr, u64 disk_num_bytes,
1896 u64 num_bytes, u64 ram_bytes,
1897 u8 compression, u8 encryption,
1898 u16 other_encoding, int extent_type)
1899{
1900 struct btrfs_root *root = BTRFS_I(inode)->root;
1901 struct btrfs_file_extent_item *fi;
1902 struct btrfs_path *path;
1903 struct extent_buffer *leaf;
1904 struct btrfs_key ins;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00001905 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04001906 int ret;
1907
1908 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07001909 if (!path)
1910 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04001911
Chris Masona1ed8352009-09-11 12:27:37 -04001912 /*
1913 * we may be replacing one extent in the tree with another.
1914 * The new extent is pinned in the extent map, and we don't want
1915 * to drop it from the cache until it is completely in the btree.
1916 *
1917 * So, tell btrfs_drop_extents to leave this extent in the cache.
1918 * the caller is expected to unpin it and allow it to be merged
1919 * with the others.
1920 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00001921 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
1922 file_pos + num_bytes, NULL, 0,
1923 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001924 if (ret)
1925 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001926
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00001927 if (!extent_inserted) {
1928 ins.objectid = btrfs_ino(inode);
1929 ins.offset = file_pos;
1930 ins.type = BTRFS_EXTENT_DATA_KEY;
1931
1932 path->leave_spinning = 1;
1933 ret = btrfs_insert_empty_item(trans, root, path, &ins,
1934 sizeof(*fi));
1935 if (ret)
1936 goto out;
1937 }
Yan Zhengd899e052008-10-30 14:25:28 -04001938 leaf = path->nodes[0];
1939 fi = btrfs_item_ptr(leaf, path->slots[0],
1940 struct btrfs_file_extent_item);
1941 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1942 btrfs_set_file_extent_type(leaf, fi, extent_type);
1943 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1944 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1945 btrfs_set_file_extent_offset(leaf, fi, 0);
1946 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1947 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1948 btrfs_set_file_extent_compression(leaf, fi, compression);
1949 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1950 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04001951
Yan Zhengd899e052008-10-30 14:25:28 -04001952 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04001953 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04001954
1955 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04001956
1957 ins.objectid = disk_bytenr;
1958 ins.offset = disk_num_bytes;
1959 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001960 ret = btrfs_alloc_reserved_file_extent(trans, root,
1961 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08001962 btrfs_ino(inode), file_pos, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001963out:
Yan Zhengd899e052008-10-30 14:25:28 -04001964 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04001965
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001966 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04001967}
1968
Liu Bo38c227d2013-01-29 03:18:40 +00001969/* snapshot-aware defrag */
1970struct sa_defrag_extent_backref {
1971 struct rb_node node;
1972 struct old_sa_defrag_extent *old;
1973 u64 root_id;
1974 u64 inum;
1975 u64 file_pos;
1976 u64 extent_offset;
1977 u64 num_bytes;
1978 u64 generation;
1979};
1980
1981struct old_sa_defrag_extent {
1982 struct list_head list;
1983 struct new_sa_defrag_extent *new;
1984
1985 u64 extent_offset;
1986 u64 bytenr;
1987 u64 offset;
1988 u64 len;
1989 int count;
1990};
1991
1992struct new_sa_defrag_extent {
1993 struct rb_root root;
1994 struct list_head head;
1995 struct btrfs_path *path;
1996 struct inode *inode;
1997 u64 file_pos;
1998 u64 len;
1999 u64 bytenr;
2000 u64 disk_len;
2001 u8 compress_type;
2002};
2003
2004static int backref_comp(struct sa_defrag_extent_backref *b1,
2005 struct sa_defrag_extent_backref *b2)
2006{
2007 if (b1->root_id < b2->root_id)
2008 return -1;
2009 else if (b1->root_id > b2->root_id)
2010 return 1;
2011
2012 if (b1->inum < b2->inum)
2013 return -1;
2014 else if (b1->inum > b2->inum)
2015 return 1;
2016
2017 if (b1->file_pos < b2->file_pos)
2018 return -1;
2019 else if (b1->file_pos > b2->file_pos)
2020 return 1;
2021
2022 /*
2023 * [------------------------------] ===> (a range of space)
2024 * |<--->| |<---->| =============> (fs/file tree A)
2025 * |<---------------------------->| ===> (fs/file tree B)
2026 *
2027 * A range of space can refer to two file extents in one tree while
2028 * refer to only one file extent in another tree.
2029 *
2030 * So we may process a disk offset more than one time(two extents in A)
2031 * and locate at the same extent(one extent in B), then insert two same
2032 * backrefs(both refer to the extent in B).
2033 */
2034 return 0;
2035}
2036
2037static void backref_insert(struct rb_root *root,
2038 struct sa_defrag_extent_backref *backref)
2039{
2040 struct rb_node **p = &root->rb_node;
2041 struct rb_node *parent = NULL;
2042 struct sa_defrag_extent_backref *entry;
2043 int ret;
2044
2045 while (*p) {
2046 parent = *p;
2047 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2048
2049 ret = backref_comp(backref, entry);
2050 if (ret < 0)
2051 p = &(*p)->rb_left;
2052 else
2053 p = &(*p)->rb_right;
2054 }
2055
2056 rb_link_node(&backref->node, parent, p);
2057 rb_insert_color(&backref->node, root);
2058}
2059
2060/*
2061 * Note the backref might has changed, and in this case we just return 0.
2062 */
2063static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2064 void *ctx)
2065{
2066 struct btrfs_file_extent_item *extent;
2067 struct btrfs_fs_info *fs_info;
2068 struct old_sa_defrag_extent *old = ctx;
2069 struct new_sa_defrag_extent *new = old->new;
2070 struct btrfs_path *path = new->path;
2071 struct btrfs_key key;
2072 struct btrfs_root *root;
2073 struct sa_defrag_extent_backref *backref;
2074 struct extent_buffer *leaf;
2075 struct inode *inode = new->inode;
2076 int slot;
2077 int ret;
2078 u64 extent_offset;
2079 u64 num_bytes;
2080
2081 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
2082 inum == btrfs_ino(inode))
2083 return 0;
2084
2085 key.objectid = root_id;
2086 key.type = BTRFS_ROOT_ITEM_KEY;
2087 key.offset = (u64)-1;
2088
2089 fs_info = BTRFS_I(inode)->root->fs_info;
2090 root = btrfs_read_fs_root_no_name(fs_info, &key);
2091 if (IS_ERR(root)) {
2092 if (PTR_ERR(root) == -ENOENT)
2093 return 0;
2094 WARN_ON(1);
2095 pr_debug("inum=%llu, offset=%llu, root_id=%llu\n",
2096 inum, offset, root_id);
2097 return PTR_ERR(root);
2098 }
2099
2100 key.objectid = inum;
2101 key.type = BTRFS_EXTENT_DATA_KEY;
2102 if (offset > (u64)-1 << 32)
2103 key.offset = 0;
2104 else
2105 key.offset = offset;
2106
2107 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302108 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002109 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002110 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002111
2112 while (1) {
2113 cond_resched();
2114
2115 leaf = path->nodes[0];
2116 slot = path->slots[0];
2117
2118 if (slot >= btrfs_header_nritems(leaf)) {
2119 ret = btrfs_next_leaf(root, path);
2120 if (ret < 0) {
2121 goto out;
2122 } else if (ret > 0) {
2123 ret = 0;
2124 goto out;
2125 }
2126 continue;
2127 }
2128
2129 path->slots[0]++;
2130
2131 btrfs_item_key_to_cpu(leaf, &key, slot);
2132
2133 if (key.objectid > inum)
2134 goto out;
2135
2136 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2137 continue;
2138
2139 extent = btrfs_item_ptr(leaf, slot,
2140 struct btrfs_file_extent_item);
2141
2142 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2143 continue;
2144
Liu Boe68afa42013-07-01 22:13:26 +08002145 /*
2146 * 'offset' refers to the exact key.offset,
2147 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2148 * (key.offset - extent_offset).
2149 */
2150 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002151 continue;
2152
Liu Boe68afa42013-07-01 22:13:26 +08002153 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002154 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002155
Liu Bo38c227d2013-01-29 03:18:40 +00002156 if (extent_offset >= old->extent_offset + old->offset +
2157 old->len || extent_offset + num_bytes <=
2158 old->extent_offset + old->offset)
2159 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002160 break;
2161 }
2162
2163 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2164 if (!backref) {
2165 ret = -ENOENT;
2166 goto out;
2167 }
2168
2169 backref->root_id = root_id;
2170 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002171 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002172 backref->num_bytes = num_bytes;
2173 backref->extent_offset = extent_offset;
2174 backref->generation = btrfs_file_extent_generation(leaf, extent);
2175 backref->old = old;
2176 backref_insert(&new->root, backref);
2177 old->count++;
2178out:
2179 btrfs_release_path(path);
2180 WARN_ON(ret);
2181 return ret;
2182}
2183
2184static noinline bool record_extent_backrefs(struct btrfs_path *path,
2185 struct new_sa_defrag_extent *new)
2186{
2187 struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info;
2188 struct old_sa_defrag_extent *old, *tmp;
2189 int ret;
2190
2191 new->path = path;
2192
2193 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002194 ret = iterate_inodes_from_logical(old->bytenr +
2195 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002196 path, record_one_backref,
2197 old);
Josef Bacik4724b102013-11-05 11:11:40 -05002198 if (ret < 0 && ret != -ENOENT)
2199 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002200
2201 /* no backref to be processed for this extent */
2202 if (!old->count) {
2203 list_del(&old->list);
2204 kfree(old);
2205 }
2206 }
2207
2208 if (list_empty(&new->head))
2209 return false;
2210
2211 return true;
2212}
2213
2214static int relink_is_mergable(struct extent_buffer *leaf,
2215 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002216 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002217{
Liu Bo116e0022013-08-02 16:30:40 +08002218 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002219 return 0;
2220
2221 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2222 return 0;
2223
Liu Bo116e0022013-08-02 16:30:40 +08002224 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2225 return 0;
2226
2227 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002228 btrfs_file_extent_other_encoding(leaf, fi))
2229 return 0;
2230
2231 return 1;
2232}
2233
2234/*
2235 * Note the backref might has changed, and in this case we just return 0.
2236 */
2237static noinline int relink_extent_backref(struct btrfs_path *path,
2238 struct sa_defrag_extent_backref *prev,
2239 struct sa_defrag_extent_backref *backref)
2240{
2241 struct btrfs_file_extent_item *extent;
2242 struct btrfs_file_extent_item *item;
2243 struct btrfs_ordered_extent *ordered;
2244 struct btrfs_trans_handle *trans;
2245 struct btrfs_fs_info *fs_info;
2246 struct btrfs_root *root;
2247 struct btrfs_key key;
2248 struct extent_buffer *leaf;
2249 struct old_sa_defrag_extent *old = backref->old;
2250 struct new_sa_defrag_extent *new = old->new;
2251 struct inode *src_inode = new->inode;
2252 struct inode *inode;
2253 struct extent_state *cached = NULL;
2254 int ret = 0;
2255 u64 start;
2256 u64 len;
2257 u64 lock_start;
2258 u64 lock_end;
2259 bool merge = false;
2260 int index;
2261
2262 if (prev && prev->root_id == backref->root_id &&
2263 prev->inum == backref->inum &&
2264 prev->file_pos + prev->num_bytes == backref->file_pos)
2265 merge = true;
2266
2267 /* step 1: get root */
2268 key.objectid = backref->root_id;
2269 key.type = BTRFS_ROOT_ITEM_KEY;
2270 key.offset = (u64)-1;
2271
2272 fs_info = BTRFS_I(src_inode)->root->fs_info;
2273 index = srcu_read_lock(&fs_info->subvol_srcu);
2274
2275 root = btrfs_read_fs_root_no_name(fs_info, &key);
2276 if (IS_ERR(root)) {
2277 srcu_read_unlock(&fs_info->subvol_srcu, index);
2278 if (PTR_ERR(root) == -ENOENT)
2279 return 0;
2280 return PTR_ERR(root);
2281 }
Liu Bo38c227d2013-01-29 03:18:40 +00002282
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002283 if (btrfs_root_readonly(root)) {
2284 srcu_read_unlock(&fs_info->subvol_srcu, index);
2285 return 0;
2286 }
2287
Liu Bo38c227d2013-01-29 03:18:40 +00002288 /* step 2: get inode */
2289 key.objectid = backref->inum;
2290 key.type = BTRFS_INODE_ITEM_KEY;
2291 key.offset = 0;
2292
2293 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2294 if (IS_ERR(inode)) {
2295 srcu_read_unlock(&fs_info->subvol_srcu, index);
2296 return 0;
2297 }
2298
2299 srcu_read_unlock(&fs_info->subvol_srcu, index);
2300
2301 /* step 3: relink backref */
2302 lock_start = backref->file_pos;
2303 lock_end = backref->file_pos + backref->num_bytes - 1;
2304 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2305 0, &cached);
2306
2307 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2308 if (ordered) {
2309 btrfs_put_ordered_extent(ordered);
2310 goto out_unlock;
2311 }
2312
2313 trans = btrfs_join_transaction(root);
2314 if (IS_ERR(trans)) {
2315 ret = PTR_ERR(trans);
2316 goto out_unlock;
2317 }
2318
2319 key.objectid = backref->inum;
2320 key.type = BTRFS_EXTENT_DATA_KEY;
2321 key.offset = backref->file_pos;
2322
2323 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2324 if (ret < 0) {
2325 goto out_free_path;
2326 } else if (ret > 0) {
2327 ret = 0;
2328 goto out_free_path;
2329 }
2330
2331 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2332 struct btrfs_file_extent_item);
2333
2334 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2335 backref->generation)
2336 goto out_free_path;
2337
2338 btrfs_release_path(path);
2339
2340 start = backref->file_pos;
2341 if (backref->extent_offset < old->extent_offset + old->offset)
2342 start += old->extent_offset + old->offset -
2343 backref->extent_offset;
2344
2345 len = min(backref->extent_offset + backref->num_bytes,
2346 old->extent_offset + old->offset + old->len);
2347 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2348
2349 ret = btrfs_drop_extents(trans, root, inode, start,
2350 start + len, 1);
2351 if (ret)
2352 goto out_free_path;
2353again:
2354 key.objectid = btrfs_ino(inode);
2355 key.type = BTRFS_EXTENT_DATA_KEY;
2356 key.offset = start;
2357
Liu Boa09a0a72013-03-11 09:20:58 +00002358 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002359 if (merge) {
2360 struct btrfs_file_extent_item *fi;
2361 u64 extent_len;
2362 struct btrfs_key found_key;
2363
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002364 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002365 if (ret < 0)
2366 goto out_free_path;
2367
2368 path->slots[0]--;
2369 leaf = path->nodes[0];
2370 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2371
2372 fi = btrfs_item_ptr(leaf, path->slots[0],
2373 struct btrfs_file_extent_item);
2374 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2375
Liu Bo116e0022013-08-02 16:30:40 +08002376 if (extent_len + found_key.offset == start &&
2377 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002378 btrfs_set_file_extent_num_bytes(leaf, fi,
2379 extent_len + len);
2380 btrfs_mark_buffer_dirty(leaf);
2381 inode_add_bytes(inode, len);
2382
2383 ret = 1;
2384 goto out_free_path;
2385 } else {
2386 merge = false;
2387 btrfs_release_path(path);
2388 goto again;
2389 }
2390 }
2391
2392 ret = btrfs_insert_empty_item(trans, root, path, &key,
2393 sizeof(*extent));
2394 if (ret) {
2395 btrfs_abort_transaction(trans, root, ret);
2396 goto out_free_path;
2397 }
2398
2399 leaf = path->nodes[0];
2400 item = btrfs_item_ptr(leaf, path->slots[0],
2401 struct btrfs_file_extent_item);
2402 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2403 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2404 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2405 btrfs_set_file_extent_num_bytes(leaf, item, len);
2406 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2407 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2408 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2409 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2410 btrfs_set_file_extent_encryption(leaf, item, 0);
2411 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2412
2413 btrfs_mark_buffer_dirty(leaf);
2414 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002415 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002416
2417 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
2418 new->disk_len, 0,
2419 backref->root_id, backref->inum,
2420 new->file_pos, 0); /* start - extent_offset */
2421 if (ret) {
2422 btrfs_abort_transaction(trans, root, ret);
2423 goto out_free_path;
2424 }
2425
2426 ret = 1;
2427out_free_path:
2428 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002429 path->leave_spinning = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002430 btrfs_end_transaction(trans, root);
2431out_unlock:
2432 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2433 &cached, GFP_NOFS);
2434 iput(inode);
2435 return ret;
2436}
2437
Liu Bo6f519562013-10-29 10:45:05 +08002438static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2439{
2440 struct old_sa_defrag_extent *old, *tmp;
2441
2442 if (!new)
2443 return;
2444
2445 list_for_each_entry_safe(old, tmp, &new->head, list) {
2446 list_del(&old->list);
2447 kfree(old);
2448 }
2449 kfree(new);
2450}
2451
Liu Bo38c227d2013-01-29 03:18:40 +00002452static void relink_file_extents(struct new_sa_defrag_extent *new)
2453{
2454 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002455 struct sa_defrag_extent_backref *backref;
2456 struct sa_defrag_extent_backref *prev = NULL;
2457 struct inode *inode;
2458 struct btrfs_root *root;
2459 struct rb_node *node;
2460 int ret;
2461
2462 inode = new->inode;
2463 root = BTRFS_I(inode)->root;
2464
2465 path = btrfs_alloc_path();
2466 if (!path)
2467 return;
2468
2469 if (!record_extent_backrefs(path, new)) {
2470 btrfs_free_path(path);
2471 goto out;
2472 }
2473 btrfs_release_path(path);
2474
2475 while (1) {
2476 node = rb_first(&new->root);
2477 if (!node)
2478 break;
2479 rb_erase(node, &new->root);
2480
2481 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2482
2483 ret = relink_extent_backref(path, prev, backref);
2484 WARN_ON(ret < 0);
2485
2486 kfree(prev);
2487
2488 if (ret == 1)
2489 prev = backref;
2490 else
2491 prev = NULL;
2492 cond_resched();
2493 }
2494 kfree(prev);
2495
2496 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002497out:
Liu Bo6f519562013-10-29 10:45:05 +08002498 free_sa_defrag_extent(new);
2499
Liu Bo38c227d2013-01-29 03:18:40 +00002500 atomic_dec(&root->fs_info->defrag_running);
2501 wake_up(&root->fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002502}
2503
2504static struct new_sa_defrag_extent *
2505record_old_file_extents(struct inode *inode,
2506 struct btrfs_ordered_extent *ordered)
2507{
2508 struct btrfs_root *root = BTRFS_I(inode)->root;
2509 struct btrfs_path *path;
2510 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002511 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002512 struct new_sa_defrag_extent *new;
2513 int ret;
2514
2515 new = kmalloc(sizeof(*new), GFP_NOFS);
2516 if (!new)
2517 return NULL;
2518
2519 new->inode = inode;
2520 new->file_pos = ordered->file_offset;
2521 new->len = ordered->len;
2522 new->bytenr = ordered->start;
2523 new->disk_len = ordered->disk_len;
2524 new->compress_type = ordered->compress_type;
2525 new->root = RB_ROOT;
2526 INIT_LIST_HEAD(&new->head);
2527
2528 path = btrfs_alloc_path();
2529 if (!path)
2530 goto out_kfree;
2531
2532 key.objectid = btrfs_ino(inode);
2533 key.type = BTRFS_EXTENT_DATA_KEY;
2534 key.offset = new->file_pos;
2535
2536 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2537 if (ret < 0)
2538 goto out_free_path;
2539 if (ret > 0 && path->slots[0] > 0)
2540 path->slots[0]--;
2541
2542 /* find out all the old extents for the file range */
2543 while (1) {
2544 struct btrfs_file_extent_item *extent;
2545 struct extent_buffer *l;
2546 int slot;
2547 u64 num_bytes;
2548 u64 offset;
2549 u64 end;
2550 u64 disk_bytenr;
2551 u64 extent_offset;
2552
2553 l = path->nodes[0];
2554 slot = path->slots[0];
2555
2556 if (slot >= btrfs_header_nritems(l)) {
2557 ret = btrfs_next_leaf(root, path);
2558 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002559 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002560 else if (ret > 0)
2561 break;
2562 continue;
2563 }
2564
2565 btrfs_item_key_to_cpu(l, &key, slot);
2566
2567 if (key.objectid != btrfs_ino(inode))
2568 break;
2569 if (key.type != BTRFS_EXTENT_DATA_KEY)
2570 break;
2571 if (key.offset >= new->file_pos + new->len)
2572 break;
2573
2574 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2575
2576 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2577 if (key.offset + num_bytes < new->file_pos)
2578 goto next;
2579
2580 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2581 if (!disk_bytenr)
2582 goto next;
2583
2584 extent_offset = btrfs_file_extent_offset(l, extent);
2585
2586 old = kmalloc(sizeof(*old), GFP_NOFS);
2587 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002588 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002589
2590 offset = max(new->file_pos, key.offset);
2591 end = min(new->file_pos + new->len, key.offset + num_bytes);
2592
2593 old->bytenr = disk_bytenr;
2594 old->extent_offset = extent_offset;
2595 old->offset = offset - key.offset;
2596 old->len = end - offset;
2597 old->new = new;
2598 old->count = 0;
2599 list_add_tail(&old->list, &new->head);
2600next:
2601 path->slots[0]++;
2602 cond_resched();
2603 }
2604
2605 btrfs_free_path(path);
2606 atomic_inc(&root->fs_info->defrag_running);
2607
2608 return new;
2609
Liu Bo38c227d2013-01-29 03:18:40 +00002610out_free_path:
2611 btrfs_free_path(path);
2612out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002613 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002614 return NULL;
2615}
2616
Miao Xiee570fd22014-06-19 10:42:50 +08002617static void btrfs_release_delalloc_bytes(struct btrfs_root *root,
2618 u64 start, u64 len)
2619{
2620 struct btrfs_block_group_cache *cache;
2621
2622 cache = btrfs_lookup_block_group(root->fs_info, start);
2623 ASSERT(cache);
2624
2625 spin_lock(&cache->lock);
2626 cache->delalloc_bytes -= len;
2627 spin_unlock(&cache->lock);
2628
2629 btrfs_put_block_group(cache);
2630}
2631
Chris Masond352ac62008-09-29 15:18:18 -04002632/* as ordered data IO finishes, this gets called so we can finish
2633 * an ordered extent if the range of bytes in the file it covers are
2634 * fully written.
2635 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002636static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002637{
Josef Bacik5fd02042012-05-02 14:00:54 -04002638 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002639 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002640 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002641 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002642 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002643 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002644 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002645 int ret = 0;
2646 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002647 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002648 bool truncated = false;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002649
Liu Bo83eea1f2012-07-10 05:28:39 -06002650 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002651
Josef Bacik5fd02042012-05-02 14:00:54 -04002652 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2653 ret = -EIO;
2654 goto out;
2655 }
2656
Josef Bacik77cef2e2013-08-29 13:57:21 -04002657 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2658 truncated = true;
2659 logical_len = ordered_extent->truncated_len;
2660 /* Truncated the entire extent, don't bother adding */
2661 if (!logical_len)
2662 goto out;
2663 }
2664
Yan, Zhengc2167752009-11-12 09:34:21 +00002665 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002666 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Josef Bacik6c760c02012-11-09 10:53:21 -05002667 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2668 if (nolock)
2669 trans = btrfs_join_transaction_nolock(root);
2670 else
2671 trans = btrfs_join_transaction(root);
2672 if (IS_ERR(trans)) {
2673 ret = PTR_ERR(trans);
2674 trans = NULL;
2675 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002676 }
Josef Bacik6c760c02012-11-09 10:53:21 -05002677 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
2678 ret = btrfs_update_inode_fallback(trans, root, inode);
2679 if (ret) /* -ENOMEM or corruption */
2680 btrfs_abort_transaction(trans, root, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002681 goto out;
2682 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002683
Josef Bacik2ac55d42010-02-03 19:33:23 +00002684 lock_extent_bits(io_tree, ordered_extent->file_offset,
2685 ordered_extent->file_offset + ordered_extent->len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002686 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002687
Liu Bo38c227d2013-01-29 03:18:40 +00002688 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2689 ordered_extent->file_offset + ordered_extent->len - 1,
2690 EXTENT_DEFRAG, 1, cached_state);
2691 if (ret) {
2692 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05002693 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00002694 /* the inode is shared */
2695 new = record_old_file_extents(inode, ordered_extent);
2696
2697 clear_extent_bit(io_tree, ordered_extent->file_offset,
2698 ordered_extent->file_offset + ordered_extent->len - 1,
2699 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2700 }
2701
Josef Bacik0cb59c92010-07-02 12:14:14 -04002702 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002703 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002704 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002705 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002706 if (IS_ERR(trans)) {
2707 ret = PTR_ERR(trans);
2708 trans = NULL;
2709 goto out_unlock;
2710 }
Chris Masona79b7d42014-05-22 16:18:52 -07002711
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002712 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002713
Chris Masonc8b97812008-10-29 14:49:59 -04002714 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002715 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002716 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002717 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00002718 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002719 ordered_extent->file_offset,
2720 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002721 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002722 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04002723 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002724 ret = insert_reserved_file_extent(trans, inode,
2725 ordered_extent->file_offset,
2726 ordered_extent->start,
2727 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04002728 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08002729 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002730 BTRFS_FILE_EXTENT_REG);
Miao Xiee570fd22014-06-19 10:42:50 +08002731 if (!ret)
2732 btrfs_release_delalloc_bytes(root,
2733 ordered_extent->start,
2734 ordered_extent->disk_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002735 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002736 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2737 ordered_extent->file_offset, ordered_extent->len,
2738 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002739 if (ret < 0) {
2740 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002741 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002742 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002743
Chris Masone6dcd2d2008-07-17 12:53:50 -04002744 add_pending_csums(trans, inode, ordered_extent->file_offset,
2745 &ordered_extent->list);
2746
Josef Bacik6c760c02012-11-09 10:53:21 -05002747 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2748 ret = btrfs_update_inode_fallback(trans, root, inode);
2749 if (ret) { /* -ENOMEM or corruption */
2750 btrfs_abort_transaction(trans, root, ret);
2751 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002752 }
2753 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002754out_unlock:
2755 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2756 ordered_extent->file_offset +
2757 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00002758out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002759 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04002760 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002761 if (trans)
2762 btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002763
Josef Bacik77cef2e2013-08-29 13:57:21 -04002764 if (ret || truncated) {
2765 u64 start, end;
2766
2767 if (truncated)
2768 start = ordered_extent->file_offset + logical_len;
2769 else
2770 start = ordered_extent->file_offset;
2771 end = ordered_extent->file_offset + ordered_extent->len - 1;
2772 clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS);
2773
2774 /* Drop the cache for the part of the extent we didn't write. */
2775 btrfs_drop_extent_cache(inode, start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04002776
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002777 /*
2778 * If the ordered extent had an IOERR or something else went
2779 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04002780 * back to the allocator. We only free the extent in the
2781 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002782 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04002783 if ((ret || !logical_len) &&
2784 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002785 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
2786 btrfs_free_reserved_extent(root, ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08002787 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002788 }
2789
2790
Josef Bacik5fd02042012-05-02 14:00:54 -04002791 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002792 * This needs to be done to make sure anybody waiting knows we are done
2793 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002794 */
2795 btrfs_remove_ordered_extent(inode, ordered_extent);
2796
Liu Bo38c227d2013-01-29 03:18:40 +00002797 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08002798 if (new) {
2799 if (ret) {
2800 free_sa_defrag_extent(new);
2801 atomic_dec(&root->fs_info->defrag_running);
2802 } else {
2803 relink_file_extents(new);
2804 }
2805 }
Liu Bo38c227d2013-01-29 03:18:40 +00002806
Chris Masone6dcd2d2008-07-17 12:53:50 -04002807 /* once for us */
2808 btrfs_put_ordered_extent(ordered_extent);
2809 /* once for the tree */
2810 btrfs_put_ordered_extent(ordered_extent);
2811
Josef Bacik5fd02042012-05-02 14:00:54 -04002812 return ret;
2813}
2814
2815static void finish_ordered_fn(struct btrfs_work *work)
2816{
2817 struct btrfs_ordered_extent *ordered_extent;
2818 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2819 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002820}
2821
Christoph Hellwigb2950862008-12-02 09:54:17 -05002822static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04002823 struct extent_state *state, int uptodate)
2824{
Josef Bacik5fd02042012-05-02 14:00:54 -04002825 struct inode *inode = page->mapping->host;
2826 struct btrfs_root *root = BTRFS_I(inode)->root;
2827 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08002828 struct btrfs_workqueue *wq;
2829 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04002830
liubo1abe9b82011-03-24 11:18:59 +00002831 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2832
Chris Mason8b62b722009-09-02 16:53:46 -04002833 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002834 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2835 end - start + 1, uptodate))
2836 return 0;
2837
Liu Bo9e0af232014-08-15 23:36:53 +08002838 if (btrfs_is_free_space_inode(inode)) {
2839 wq = root->fs_info->endio_freespace_worker;
2840 func = btrfs_freespace_write_helper;
2841 } else {
2842 wq = root->fs_info->endio_write_workers;
2843 func = btrfs_endio_write_helper;
2844 }
Josef Bacik5fd02042012-05-02 14:00:54 -04002845
Liu Bo9e0af232014-08-15 23:36:53 +08002846 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
2847 NULL);
2848 btrfs_queue_work(wq, &ordered_extent->work);
Josef Bacik5fd02042012-05-02 14:00:54 -04002849
2850 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04002851}
2852
Chris Masond352ac62008-09-29 15:18:18 -04002853/*
Chris Masond352ac62008-09-29 15:18:18 -04002854 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002855 * if there's a match, we allow the bio to finish. If not, the code in
2856 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002857 */
Miao Xiefacc8a222013-07-25 19:22:34 +08002858static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
2859 u64 phy_offset, struct page *page,
2860 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002861{
Miao Xie4eee4fa2012-12-21 09:17:45 +00002862 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04002863 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002864 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04002865 char *kaddr;
Chris Masonff79f812007-10-15 16:22:25 -04002866 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiefacc8a222013-07-25 19:22:34 +08002867 u32 csum_expected;
Chris Masonff79f812007-10-15 16:22:25 -04002868 u32 csum = ~(u32)0;
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002869 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
2870 DEFAULT_RATELIMIT_BURST);
Chris Masond1310b22008-01-24 16:13:08 -05002871
Chris Masond20f7042008-12-08 16:58:54 -05002872 if (PageChecked(page)) {
2873 ClearPageChecked(page);
2874 goto good;
2875 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002876
2877 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Jan Schmidt08d2f342011-05-04 16:18:50 +02002878 goto good;
Chris Masond20f7042008-12-08 16:58:54 -05002879
Yan Zheng17d217f2008-12-12 10:03:38 -05002880 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002881 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002882 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
2883 GFP_NOFS);
2884 return 0;
2885 }
2886
Miao Xiefacc8a222013-07-25 19:22:34 +08002887 phy_offset >>= inode->i_sb->s_blocksize_bits;
2888 csum_expected = *(((u32 *)io_bio->csum) + phy_offset);
Chris Masond3977122009-01-05 21:25:51 -05002889
Miao Xiefacc8a222013-07-25 19:22:34 +08002890 kaddr = kmap_atomic(page);
Liu Bob0496682013-03-14 14:57:45 +00002891 csum = btrfs_csum_data(kaddr + offset, csum, end - start + 1);
Chris Masonff79f812007-10-15 16:22:25 -04002892 btrfs_csum_final(csum, (char *)&csum);
Miao Xiefacc8a222013-07-25 19:22:34 +08002893 if (csum != csum_expected)
Chris Mason07157aa2007-08-30 08:50:51 -04002894 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002895
Cong Wang7ac687d2011-11-25 23:14:28 +08002896 kunmap_atomic(kaddr);
Chris Masond20f7042008-12-08 16:58:54 -05002897good:
Chris Mason07157aa2007-08-30 08:50:51 -04002898 return 0;
2899
2900zeroit:
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002901 if (__ratelimit(&_rs))
Miao Xiefacc8a222013-07-25 19:22:34 +08002902 btrfs_info(root->fs_info, "csum failed ino %llu off %llu csum %u expected csum %u",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002903 btrfs_ino(page->mapping->host), start, csum, csum_expected);
Chris Masondb945352007-10-15 16:15:53 -04002904 memset(kaddr + offset, 1, end - start + 1);
2905 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002906 kunmap_atomic(kaddr);
Miao Xiefacc8a222013-07-25 19:22:34 +08002907 if (csum_expected == 0)
Chris Mason3b951512008-04-17 11:29:12 -04002908 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04002909 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04002910}
Chris Masonb888db22007-08-27 16:49:44 -04002911
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002912struct delayed_iput {
2913 struct list_head list;
2914 struct inode *inode;
2915};
2916
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002917/* JDM: If this is fs-wide, why can't we add a pointer to
2918 * btrfs_inode instead and avoid the allocation? */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002919void btrfs_add_delayed_iput(struct inode *inode)
2920{
2921 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2922 struct delayed_iput *delayed;
2923
2924 if (atomic_add_unless(&inode->i_count, -1, 1))
2925 return;
2926
2927 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2928 delayed->inode = inode;
2929
2930 spin_lock(&fs_info->delayed_iput_lock);
2931 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2932 spin_unlock(&fs_info->delayed_iput_lock);
2933}
2934
2935void btrfs_run_delayed_iputs(struct btrfs_root *root)
2936{
2937 LIST_HEAD(list);
2938 struct btrfs_fs_info *fs_info = root->fs_info;
2939 struct delayed_iput *delayed;
2940 int empty;
2941
2942 spin_lock(&fs_info->delayed_iput_lock);
2943 empty = list_empty(&fs_info->delayed_iputs);
2944 spin_unlock(&fs_info->delayed_iput_lock);
2945 if (empty)
2946 return;
2947
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002948 spin_lock(&fs_info->delayed_iput_lock);
2949 list_splice_init(&fs_info->delayed_iputs, &list);
2950 spin_unlock(&fs_info->delayed_iput_lock);
2951
2952 while (!list_empty(&list)) {
2953 delayed = list_entry(list.next, struct delayed_iput, list);
2954 list_del(&delayed->list);
2955 iput(delayed->inode);
2956 kfree(delayed);
2957 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002958}
2959
Yan, Zhengd68fc572010-05-16 10:49:58 -04002960/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08002961 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04002962 * files in the subvolume, it removes orphan item and frees block_rsv
2963 * structure.
2964 */
2965void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
2966 struct btrfs_root *root)
2967{
Josef Bacik90290e12011-12-02 15:44:12 -05002968 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002969 int ret;
2970
Josef Bacik8a35d952012-05-23 14:26:42 -04002971 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04002972 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
2973 return;
2974
Josef Bacik90290e12011-12-02 15:44:12 -05002975 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04002976 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05002977 spin_unlock(&root->orphan_lock);
2978 return;
2979 }
2980
2981 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
2982 spin_unlock(&root->orphan_lock);
2983 return;
2984 }
2985
2986 block_rsv = root->orphan_block_rsv;
2987 root->orphan_block_rsv = NULL;
2988 spin_unlock(&root->orphan_lock);
2989
Miao Xie27cdeb72014-04-02 19:51:05 +08002990 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
Yan, Zhengd68fc572010-05-16 10:49:58 -04002991 btrfs_root_refs(&root->root_item) > 0) {
2992 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
2993 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04002994 if (ret)
2995 btrfs_abort_transaction(trans, root, ret);
2996 else
Miao Xie27cdeb72014-04-02 19:51:05 +08002997 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
2998 &root->state);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002999 }
3000
Josef Bacik90290e12011-12-02 15:44:12 -05003001 if (block_rsv) {
3002 WARN_ON(block_rsv->size > 0);
3003 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003004 }
3005}
3006
3007/*
Josef Bacik7b128762008-07-24 12:17:14 -04003008 * This creates an orphan entry for the given inode in case something goes
3009 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003010 *
3011 * NOTE: caller of this function should reserve 5 units of metadata for
3012 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04003013 */
3014int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
3015{
3016 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003017 struct btrfs_block_rsv *block_rsv = NULL;
3018 int reserve = 0;
3019 int insert = 0;
3020 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003021
Yan, Zhengd68fc572010-05-16 10:49:58 -04003022 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06003023 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003024 if (!block_rsv)
3025 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04003026 }
3027
Yan, Zhengd68fc572010-05-16 10:49:58 -04003028 spin_lock(&root->orphan_lock);
3029 if (!root->orphan_block_rsv) {
3030 root->orphan_block_rsv = block_rsv;
3031 } else if (block_rsv) {
3032 btrfs_free_block_rsv(root, block_rsv);
3033 block_rsv = NULL;
3034 }
Josef Bacik7b128762008-07-24 12:17:14 -04003035
Josef Bacik8a35d952012-05-23 14:26:42 -04003036 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3037 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003038#if 0
3039 /*
3040 * For proper ENOSPC handling, we should do orphan
3041 * cleanup when mounting. But this introduces backward
3042 * compatibility issue.
3043 */
3044 if (!xchg(&root->orphan_item_inserted, 1))
3045 insert = 2;
3046 else
3047 insert = 1;
3048#endif
3049 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003050 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003051 }
Josef Bacik7b128762008-07-24 12:17:14 -04003052
Josef Bacik72ac3c02012-05-23 14:13:11 -04003053 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3054 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003055 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003056 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003057
Yan, Zhengd68fc572010-05-16 10:49:58 -04003058 /* grab metadata reservation from transaction handle */
3059 if (reserve) {
3060 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003061 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04003062 }
3063
3064 /* insert an orphan item to track this unlinked/truncated file */
3065 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08003066 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003067 if (ret) {
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003068 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003069 if (reserve) {
3070 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3071 &BTRFS_I(inode)->runtime_flags);
3072 btrfs_orphan_release_metadata(inode);
3073 }
3074 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003075 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3076 &BTRFS_I(inode)->runtime_flags);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003077 btrfs_abort_transaction(trans, root, ret);
3078 return ret;
3079 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003080 }
3081 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003082 }
3083
3084 /* insert an orphan item to track subvolume contains orphan files */
3085 if (insert >= 2) {
3086 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
3087 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003088 if (ret && ret != -EEXIST) {
3089 btrfs_abort_transaction(trans, root, ret);
3090 return ret;
3091 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003092 }
3093 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003094}
3095
3096/*
3097 * We have done the truncate/delete so we can go ahead and remove the orphan
3098 * item for this particular inode.
3099 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003100static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3101 struct inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003102{
3103 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003104 int delete_item = 0;
3105 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003106 int ret = 0;
3107
Yan, Zhengd68fc572010-05-16 10:49:58 -04003108 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003109 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3110 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003111 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003112
Josef Bacik72ac3c02012-05-23 14:13:11 -04003113 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3114 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003115 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003116 spin_unlock(&root->orphan_lock);
3117
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003118 if (delete_item) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003119 atomic_dec(&root->orphan_inodes);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003120 if (trans)
3121 ret = btrfs_del_orphan_item(trans, root,
3122 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04003123 }
Josef Bacik7b128762008-07-24 12:17:14 -04003124
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003125 if (release_rsv)
3126 btrfs_orphan_release_metadata(inode);
3127
Josef Bacik4ef31a42013-08-13 14:10:08 -04003128 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003129}
3130
3131/*
3132 * this cleans up any orphans that may be left on the list from the last use
3133 * of this root.
3134 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003135int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003136{
3137 struct btrfs_path *path;
3138 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003139 struct btrfs_key key, found_key;
3140 struct btrfs_trans_handle *trans;
3141 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003142 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003143 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3144
Yan, Zhengd68fc572010-05-16 10:49:58 -04003145 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003146 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003147
3148 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003149 if (!path) {
3150 ret = -ENOMEM;
3151 goto out;
3152 }
Josef Bacik7b128762008-07-24 12:17:14 -04003153 path->reada = -1;
3154
3155 key.objectid = BTRFS_ORPHAN_OBJECTID;
3156 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
3157 key.offset = (u64)-1;
3158
Josef Bacik7b128762008-07-24 12:17:14 -04003159 while (1) {
3160 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003161 if (ret < 0)
3162 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003163
3164 /*
3165 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003166 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003167 * find the key and see if we have stuff that matches
3168 */
3169 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003170 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003171 if (path->slots[0] == 0)
3172 break;
3173 path->slots[0]--;
3174 }
3175
3176 /* pull out the item */
3177 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003178 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3179
3180 /* make sure the item matches what we want */
3181 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3182 break;
3183 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
3184 break;
3185
3186 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003187 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003188
3189 /*
3190 * this is where we are basically btrfs_lookup, without the
3191 * crossing root thing. we store the inode number in the
3192 * offset of the orphan item.
3193 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003194
3195 if (found_key.offset == last_objectid) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003196 btrfs_err(root->fs_info,
3197 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003198 ret = -EINVAL;
3199 goto out;
3200 }
3201
3202 last_objectid = found_key.offset;
3203
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003204 found_key.objectid = found_key.offset;
3205 found_key.type = BTRFS_INODE_ITEM_KEY;
3206 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00003207 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303208 ret = PTR_ERR_OR_ZERO(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003209 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003210 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003211
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003212 if (ret == -ESTALE && root == root->fs_info->tree_root) {
3213 struct btrfs_root *dead_root;
3214 struct btrfs_fs_info *fs_info = root->fs_info;
3215 int is_dead_root = 0;
3216
3217 /*
3218 * this is an orphan in the tree root. Currently these
3219 * could come from 2 sources:
3220 * a) a snapshot deletion in progress
3221 * b) a free space cache inode
3222 * We need to distinguish those two, as the snapshot
3223 * orphan must not get deleted.
3224 * find_dead_roots already ran before us, so if this
3225 * is a snapshot deletion, we should find the root
3226 * in the dead_roots list
3227 */
3228 spin_lock(&fs_info->trans_lock);
3229 list_for_each_entry(dead_root, &fs_info->dead_roots,
3230 root_list) {
3231 if (dead_root->root_key.objectid ==
3232 found_key.objectid) {
3233 is_dead_root = 1;
3234 break;
3235 }
3236 }
3237 spin_unlock(&fs_info->trans_lock);
3238 if (is_dead_root) {
3239 /* prevent this orphan from being found again */
3240 key.offset = found_key.objectid - 1;
3241 continue;
3242 }
3243 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003244 /*
3245 * Inode is already gone but the orphan item is still there,
3246 * kill the orphan item.
3247 */
3248 if (ret == -ESTALE) {
3249 trans = btrfs_start_transaction(root, 1);
3250 if (IS_ERR(trans)) {
3251 ret = PTR_ERR(trans);
3252 goto out;
3253 }
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003254 btrfs_debug(root->fs_info, "auto deleting %Lu",
3255 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003256 ret = btrfs_del_orphan_item(trans, root,
3257 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003258 btrfs_end_transaction(trans, root);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003259 if (ret)
3260 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003261 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003262 }
Josef Bacik7b128762008-07-24 12:17:14 -04003263
Josef Bacik7b128762008-07-24 12:17:14 -04003264 /*
3265 * add this inode to the orphan list so btrfs_orphan_del does
3266 * the proper thing when we hit it
3267 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003268 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3269 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003270 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003271
Josef Bacik7b128762008-07-24 12:17:14 -04003272 /* if we have links, this was a truncate, lets do that */
3273 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303274 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003275 iput(inode);
3276 continue;
3277 }
Josef Bacik7b128762008-07-24 12:17:14 -04003278 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003279
3280 /* 1 for the orphan item deletion. */
3281 trans = btrfs_start_transaction(root, 1);
3282 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003283 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003284 ret = PTR_ERR(trans);
3285 goto out;
3286 }
3287 ret = btrfs_orphan_add(trans, inode);
3288 btrfs_end_transaction(trans, root);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003289 if (ret) {
3290 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003291 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003292 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003293
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003294 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003295 if (ret)
3296 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003297 } else {
3298 nr_unlink++;
3299 }
3300
3301 /* this will do delete_inode and everything for us */
3302 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003303 if (ret)
3304 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003305 }
Miao Xie3254c872011-11-10 20:45:05 -05003306 /* release the path since we're done with it */
3307 btrfs_release_path(path);
3308
Yan, Zhengd68fc572010-05-16 10:49:58 -04003309 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3310
3311 if (root->orphan_block_rsv)
3312 btrfs_block_rsv_release(root, root->orphan_block_rsv,
3313 (u64)-1);
3314
Miao Xie27cdeb72014-04-02 19:51:05 +08003315 if (root->orphan_block_rsv ||
3316 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003317 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003318 if (!IS_ERR(trans))
3319 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003320 }
Josef Bacik7b128762008-07-24 12:17:14 -04003321
3322 if (nr_unlink)
David Sterba4884b472013-03-20 13:31:27 +00003323 btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003324 if (nr_truncate)
David Sterba4884b472013-03-20 13:31:27 +00003325 btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003326
3327out:
3328 if (ret)
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003329 btrfs_crit(root->fs_info,
3330 "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003331 btrfs_free_path(path);
3332 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003333}
3334
Chris Masond352ac62008-09-29 15:18:18 -04003335/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003336 * very simple check to peek ahead in the leaf looking for xattrs. If we
3337 * don't find any xattrs, we know there can't be any acls.
3338 *
3339 * slot is the slot the inode is in, objectid is the objectid of the inode
3340 */
3341static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003342 int slot, u64 objectid,
3343 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003344{
3345 u32 nritems = btrfs_header_nritems(leaf);
3346 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003347 static u64 xattr_access = 0;
3348 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003349 int scanned = 0;
3350
Josef Bacikf23b5a52013-06-19 10:16:26 -04003351 if (!xattr_access) {
3352 xattr_access = btrfs_name_hash(POSIX_ACL_XATTR_ACCESS,
3353 strlen(POSIX_ACL_XATTR_ACCESS));
3354 xattr_default = btrfs_name_hash(POSIX_ACL_XATTR_DEFAULT,
3355 strlen(POSIX_ACL_XATTR_DEFAULT));
3356 }
3357
Chris Mason46a53cc2009-04-27 11:47:50 -04003358 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003359 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003360 while (slot < nritems) {
3361 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3362
3363 /* we found a different objectid, there must not be acls */
3364 if (found_key.objectid != objectid)
3365 return 0;
3366
3367 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003368 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003369 if (*first_xattr_slot == -1)
3370 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003371 if (found_key.offset == xattr_access ||
3372 found_key.offset == xattr_default)
3373 return 1;
3374 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003375
3376 /*
3377 * we found a key greater than an xattr key, there can't
3378 * be any acls later on
3379 */
3380 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3381 return 0;
3382
3383 slot++;
3384 scanned++;
3385
3386 /*
3387 * it goes inode, inode backrefs, xattrs, extents,
3388 * so if there are a ton of hard links to an inode there can
3389 * be a lot of backrefs. Don't waste time searching too hard,
3390 * this is just an optimization
3391 */
3392 if (scanned >= 8)
3393 break;
3394 }
3395 /* we hit the end of the leaf before we found an xattr or
3396 * something larger than an xattr. We have to assume the inode
3397 * has acls
3398 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003399 if (*first_xattr_slot == -1)
3400 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003401 return 1;
3402}
3403
3404/*
Chris Masond352ac62008-09-29 15:18:18 -04003405 * read an inode from the btree into the in-memory inode
3406 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003407static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003408{
3409 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003410 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003411 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04003412 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04003413 struct btrfs_root *root = BTRFS_I(inode)->root;
3414 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003415 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003416 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003417 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003418 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003419 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003420 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003421
3422 ret = btrfs_fill_inode(inode, &rdev);
3423 if (!ret)
3424 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003425
3426 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07003427 if (!path)
3428 goto make_bad;
3429
Chris Mason39279cc2007-06-12 06:35:45 -04003430 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003431
Chris Mason39279cc2007-06-12 06:35:45 -04003432 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003433 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04003434 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04003435
Chris Mason5f39d392007-10-15 16:14:19 -04003436 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003437
3438 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003439 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003440
Chris Mason5f39d392007-10-15 16:14:19 -04003441 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3442 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003443 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003444 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003445 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3446 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04003447 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003448
3449 tspec = btrfs_inode_atime(inode_item);
3450 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3451 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3452
3453 tspec = btrfs_inode_mtime(inode_item);
3454 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3455 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3456
3457 tspec = btrfs_inode_ctime(inode_item);
3458 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3459 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3460
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003461 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003462 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003463 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3464
3465 /*
3466 * If we were modified in the current generation and evicted from memory
3467 * and then re-read we need to do a full sync since we don't have any
3468 * idea about which extents were modified before we were evicted from
3469 * cache.
3470 */
3471 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
3472 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3473 &BTRFS_I(inode)->runtime_flags);
3474
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003475 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003476 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003477 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003478 rdev = btrfs_inode_rdev(leaf, inode_item);
3479
Josef Bacikaec74772008-07-24 12:12:38 -04003480 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003481 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003482
3483cache_index:
3484 path->slots[0]++;
3485 if (inode->i_nlink != 1 ||
3486 path->slots[0] >= btrfs_header_nritems(leaf))
3487 goto cache_acl;
3488
3489 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
3490 if (location.objectid != btrfs_ino(inode))
3491 goto cache_acl;
3492
3493 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3494 if (location.type == BTRFS_INODE_REF_KEY) {
3495 struct btrfs_inode_ref *ref;
3496
3497 ref = (struct btrfs_inode_ref *)ptr;
3498 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3499 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3500 struct btrfs_inode_extref *extref;
3501
3502 extref = (struct btrfs_inode_extref *)ptr;
3503 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3504 extref);
3505 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003506cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003507 /*
3508 * try to precache a NULL acl entry for files that don't have
3509 * any xattrs or acls
3510 */
Li Zefan33345d012011-04-20 10:31:50 +08003511 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003512 btrfs_ino(inode), &first_xattr_slot);
3513 if (first_xattr_slot != -1) {
3514 path->slots[0] = first_xattr_slot;
3515 ret = btrfs_load_inode_props(inode, path);
3516 if (ret)
3517 btrfs_err(root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003518 "error loading props for ino %llu (root %llu): %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003519 btrfs_ino(inode),
3520 root->root_key.objectid, ret);
3521 }
3522 btrfs_free_path(path);
3523
Al Viro72c04902009-06-24 16:58:48 -04003524 if (!maybe_acls)
3525 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003526
Chris Mason39279cc2007-06-12 06:35:45 -04003527 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003528 case S_IFREG:
3529 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003530 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05003531 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003532 inode->i_fop = &btrfs_file_operations;
3533 inode->i_op = &btrfs_file_inode_operations;
3534 break;
3535 case S_IFDIR:
3536 inode->i_fop = &btrfs_dir_file_operations;
3537 if (root == root->fs_info->tree_root)
3538 inode->i_op = &btrfs_dir_ro_inode_operations;
3539 else
3540 inode->i_op = &btrfs_dir_inode_operations;
3541 break;
3542 case S_IFLNK:
3543 inode->i_op = &btrfs_symlink_inode_operations;
3544 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04003545 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003546 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003547 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003548 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003549 init_special_inode(inode, inode->i_mode, rdev);
3550 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003551 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003552
3553 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003554 return;
3555
3556make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003557 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003558 make_bad_inode(inode);
3559}
3560
Chris Masond352ac62008-09-29 15:18:18 -04003561/*
3562 * given a leaf and an inode, copy the inode fields into the leaf
3563 */
Chris Masone02119d2008-09-05 16:13:11 -04003564static void fill_inode_item(struct btrfs_trans_handle *trans,
3565 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003566 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003567 struct inode *inode)
3568{
Liu Bo51fab692012-12-27 09:01:21 +00003569 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003570
Liu Bo51fab692012-12-27 09:01:21 +00003571 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003572
Liu Bo51fab692012-12-27 09:01:21 +00003573 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3574 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3575 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3576 &token);
3577 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3578 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003579
Liu Bo51fab692012-12-27 09:01:21 +00003580 btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item),
3581 inode->i_atime.tv_sec, &token);
3582 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item),
3583 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003584
Liu Bo51fab692012-12-27 09:01:21 +00003585 btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item),
3586 inode->i_mtime.tv_sec, &token);
3587 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item),
3588 inode->i_mtime.tv_nsec, &token);
3589
3590 btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item),
3591 inode->i_ctime.tv_sec, &token);
3592 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item),
3593 inode->i_ctime.tv_nsec, &token);
3594
3595 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3596 &token);
3597 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3598 &token);
3599 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3600 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3601 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3602 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3603 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003604}
3605
Chris Masond352ac62008-09-29 15:18:18 -04003606/*
3607 * copy everything in the in-memory inode into the btree.
3608 */
Chris Mason21151332011-11-10 20:39:08 -05003609static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003610 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003611{
3612 struct btrfs_inode_item *inode_item;
3613 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003614 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003615 int ret;
3616
3617 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003618 if (!path)
3619 return -ENOMEM;
3620
Chris Masonb9473432009-03-13 11:00:37 -04003621 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003622 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3623 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003624 if (ret) {
3625 if (ret > 0)
3626 ret = -ENOENT;
3627 goto failed;
3628 }
3629
Chris Mason5f39d392007-10-15 16:14:19 -04003630 leaf = path->nodes[0];
3631 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003632 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003633
Chris Masone02119d2008-09-05 16:13:11 -04003634 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003635 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003636 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003637 ret = 0;
3638failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003639 btrfs_free_path(path);
3640 return ret;
3641}
3642
Chris Masond352ac62008-09-29 15:18:18 -04003643/*
Chris Mason21151332011-11-10 20:39:08 -05003644 * copy everything in the in-memory inode into the btree.
3645 */
3646noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3647 struct btrfs_root *root, struct inode *inode)
3648{
3649 int ret;
3650
3651 /*
3652 * If the inode is a free space inode, we can deadlock during commit
3653 * if we put it into the delayed code.
3654 *
3655 * The data relocation inode should also be directly updated
3656 * without delay
3657 */
Liu Bo83eea1f2012-07-10 05:28:39 -06003658 if (!btrfs_is_free_space_inode(inode)
Chris Mason21151332011-11-10 20:39:08 -05003659 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003660 btrfs_update_root_times(trans, root);
3661
Chris Mason21151332011-11-10 20:39:08 -05003662 ret = btrfs_delayed_update_inode(trans, root, inode);
3663 if (!ret)
3664 btrfs_set_inode_last_trans(trans, inode);
3665 return ret;
3666 }
3667
3668 return btrfs_update_inode_item(trans, root, inode);
3669}
3670
Josef Bacikbe6aef62012-10-22 15:43:12 -04003671noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3672 struct btrfs_root *root,
3673 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003674{
3675 int ret;
3676
3677 ret = btrfs_update_inode(trans, root, inode);
3678 if (ret == -ENOSPC)
3679 return btrfs_update_inode_item(trans, root, inode);
3680 return ret;
3681}
3682
3683/*
Chris Masond352ac62008-09-29 15:18:18 -04003684 * unlink helper that gets used here in inode.c and in the tree logging
3685 * recovery code. It remove a link in a directory with a given name, and
3686 * also drops the back refs in the inode to the directory
3687 */
Al Viro92986792011-03-04 17:14:37 +00003688static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3689 struct btrfs_root *root,
3690 struct inode *dir, struct inode *inode,
3691 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003692{
3693 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003694 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003695 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003696 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003697 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003698 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003699 u64 ino = btrfs_ino(inode);
3700 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003701
3702 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003703 if (!path) {
3704 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003705 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003706 }
3707
Chris Masonb9473432009-03-13 11:00:37 -04003708 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003709 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003710 name, name_len, -1);
3711 if (IS_ERR(di)) {
3712 ret = PTR_ERR(di);
3713 goto err;
3714 }
3715 if (!di) {
3716 ret = -ENOENT;
3717 goto err;
3718 }
Chris Mason5f39d392007-10-15 16:14:19 -04003719 leaf = path->nodes[0];
3720 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003721 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003722 if (ret)
3723 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003724 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003725
Miao Xie67de1172013-12-26 13:07:06 +08003726 /*
3727 * If we don't have dir index, we have to get it by looking up
3728 * the inode ref, since we get the inode ref, remove it directly,
3729 * it is unnecessary to do delayed deletion.
3730 *
3731 * But if we have dir index, needn't search inode ref to get it.
3732 * Since the inode ref is close to the inode item, it is better
3733 * that we delay to delete it, and just do this deletion when
3734 * we update the inode item.
3735 */
3736 if (BTRFS_I(inode)->dir_index) {
3737 ret = btrfs_delayed_delete_inode_ref(inode);
3738 if (!ret) {
3739 index = BTRFS_I(inode)->dir_index;
3740 goto skip_backref;
3741 }
3742 }
3743
Li Zefan33345d012011-04-20 10:31:50 +08003744 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3745 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003746 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003747 btrfs_info(root->fs_info,
3748 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003749 name_len, name, ino, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003750 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003751 goto err;
3752 }
Miao Xie67de1172013-12-26 13:07:06 +08003753skip_backref:
Miao Xie16cdcec2011-04-22 18:12:22 +08003754 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003755 if (ret) {
3756 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003757 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003758 }
Chris Mason39279cc2007-06-12 06:35:45 -04003759
Chris Masone02119d2008-09-05 16:13:11 -04003760 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08003761 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003762 if (ret != 0 && ret != -ENOENT) {
3763 btrfs_abort_transaction(trans, root, ret);
3764 goto err;
3765 }
Chris Masone02119d2008-09-05 16:13:11 -04003766
3767 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
3768 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04003769 if (ret == -ENOENT)
3770 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003771 else if (ret)
3772 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003773err:
3774 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003775 if (ret)
3776 goto out;
3777
3778 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003779 inode_inc_iversion(inode);
3780 inode_inc_iversion(dir);
Chris Masone02119d2008-09-05 16:13:11 -04003781 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Tsutomu Itohb9959292012-06-25 21:25:22 -06003782 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04003783out:
Chris Mason39279cc2007-06-12 06:35:45 -04003784 return ret;
3785}
3786
Al Viro92986792011-03-04 17:14:37 +00003787int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3788 struct btrfs_root *root,
3789 struct inode *dir, struct inode *inode,
3790 const char *name, int name_len)
3791{
3792 int ret;
3793 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3794 if (!ret) {
Zach Brown8b558c52013-10-16 12:10:34 -07003795 drop_nlink(inode);
Al Viro92986792011-03-04 17:14:37 +00003796 ret = btrfs_update_inode(trans, root, inode);
3797 }
3798 return ret;
3799}
Yan, Zhenga22285a2010-05-16 10:48:46 -04003800
3801/*
3802 * helper to start transaction for unlink and rmdir.
3803 *
Josef Bacikd52be812013-05-29 14:54:47 -04003804 * unlink and rmdir are special in btrfs, they do not always free space, so
3805 * if we cannot make our reservations the normal way try and see if there is
3806 * plenty of slack room in the global reserve to migrate, otherwise we cannot
3807 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04003808 */
Josef Bacikd52be812013-05-29 14:54:47 -04003809static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003810{
3811 struct btrfs_trans_handle *trans;
3812 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003813 int ret;
3814
Josef Bacike70bea52011-10-11 14:18:24 -04003815 /*
3816 * 1 for the possible orphan item
3817 * 1 for the dir item
3818 * 1 for the dir index
3819 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04003820 * 1 for the inode
3821 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04003822 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003823 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
3824 return trans;
3825
Josef Bacikd52be812013-05-29 14:54:47 -04003826 if (PTR_ERR(trans) == -ENOSPC) {
3827 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003828
Josef Bacikd52be812013-05-29 14:54:47 -04003829 trans = btrfs_start_transaction(root, 0);
3830 if (IS_ERR(trans))
3831 return trans;
3832 ret = btrfs_cond_migrate_bytes(root->fs_info,
3833 &root->fs_info->trans_block_rsv,
3834 num_bytes, 5);
3835 if (ret) {
3836 btrfs_end_transaction(trans, root);
3837 return ERR_PTR(ret);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003838 }
Josef Bacik5a77d762011-11-01 14:32:23 -04003839 trans->block_rsv = &root->fs_info->trans_block_rsv;
Josef Bacikd52be812013-05-29 14:54:47 -04003840 trans->bytes_reserved = num_bytes;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003841 }
Josef Bacikd52be812013-05-29 14:54:47 -04003842 return trans;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003843}
3844
Chris Mason39279cc2007-06-12 06:35:45 -04003845static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3846{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003847 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003848 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04003849 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003850 int ret;
3851
Josef Bacikd52be812013-05-29 14:54:47 -04003852 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003853 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003854 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003855
Chris Mason12fcfd22009-03-24 10:24:20 -04003856 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
3857
Chris Masone02119d2008-09-05 16:13:11 -04003858 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3859 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003860 if (ret)
3861 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003862
Yan, Zhenga22285a2010-05-16 10:48:46 -04003863 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04003864 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003865 if (ret)
3866 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003867 }
Josef Bacik7b128762008-07-24 12:17:14 -04003868
Tsutomu Itohb5324022011-07-19 07:27:20 +00003869out:
Josef Bacikd52be812013-05-29 14:54:47 -04003870 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003871 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04003872 return ret;
3873}
3874
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003875int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3876 struct btrfs_root *root,
3877 struct inode *dir, u64 objectid,
3878 const char *name, int name_len)
3879{
3880 struct btrfs_path *path;
3881 struct extent_buffer *leaf;
3882 struct btrfs_dir_item *di;
3883 struct btrfs_key key;
3884 u64 index;
3885 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003886 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003887
3888 path = btrfs_alloc_path();
3889 if (!path)
3890 return -ENOMEM;
3891
Li Zefan33345d012011-04-20 10:31:50 +08003892 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003893 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003894 if (IS_ERR_OR_NULL(di)) {
3895 if (!di)
3896 ret = -ENOENT;
3897 else
3898 ret = PTR_ERR(di);
3899 goto out;
3900 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003901
3902 leaf = path->nodes[0];
3903 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3904 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3905 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003906 if (ret) {
3907 btrfs_abort_transaction(trans, root, ret);
3908 goto out;
3909 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003910 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003911
3912 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3913 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003914 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003915 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003916 if (ret != -ENOENT) {
3917 btrfs_abort_transaction(trans, root, ret);
3918 goto out;
3919 }
Li Zefan33345d012011-04-20 10:31:50 +08003920 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003921 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003922 if (IS_ERR_OR_NULL(di)) {
3923 if (!di)
3924 ret = -ENOENT;
3925 else
3926 ret = PTR_ERR(di);
3927 btrfs_abort_transaction(trans, root, ret);
3928 goto out;
3929 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003930
3931 leaf = path->nodes[0];
3932 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003933 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003934 index = key.offset;
3935 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003936 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003937
Miao Xie16cdcec2011-04-22 18:12:22 +08003938 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003939 if (ret) {
3940 btrfs_abort_transaction(trans, root, ret);
3941 goto out;
3942 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003943
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003944 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003945 inode_inc_iversion(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003946 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Josef Bacik5a24e842012-08-08 10:12:59 -06003947 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003948 if (ret)
3949 btrfs_abort_transaction(trans, root, ret);
3950out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04003951 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003952 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003953}
3954
Chris Mason39279cc2007-06-12 06:35:45 -04003955static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3956{
3957 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003958 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003959 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003960 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04003961
David Sterbab3ae2442012-09-13 16:04:34 -06003962 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04003963 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06003964 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
3965 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04003966
Josef Bacikd52be812013-05-29 14:54:47 -04003967 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003968 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003969 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003970
Li Zefan33345d012011-04-20 10:31:50 +08003971 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003972 err = btrfs_unlink_subvol(trans, root, dir,
3973 BTRFS_I(inode)->location.objectid,
3974 dentry->d_name.name,
3975 dentry->d_name.len);
3976 goto out;
3977 }
3978
Josef Bacik7b128762008-07-24 12:17:14 -04003979 err = btrfs_orphan_add(trans, inode);
3980 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003981 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003982
Chris Mason39279cc2007-06-12 06:35:45 -04003983 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04003984 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3985 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05003986 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04003987 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003988out:
Josef Bacikd52be812013-05-29 14:54:47 -04003989 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003990 btrfs_btree_balance_dirty(root);
Chris Mason39544012007-12-12 14:38:19 -05003991
Chris Mason39279cc2007-06-12 06:35:45 -04003992 return err;
3993}
3994
Chris Mason323ac952008-10-01 19:05:46 -04003995/*
Chris Mason39279cc2007-06-12 06:35:45 -04003996 * this can truncate away extent items, csum items and directory items.
3997 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04003998 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04003999 *
4000 * csum items that cross the new i_size are truncated to the new size
4001 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004002 *
4003 * min_type is the minimum key type to truncate down to. If set to 0, this
4004 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004005 */
Yan, Zheng80825102009-11-12 09:35:36 +00004006int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4007 struct btrfs_root *root,
4008 struct inode *inode,
4009 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004010{
Chris Mason39279cc2007-06-12 06:35:45 -04004011 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004012 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004013 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004014 struct btrfs_key key;
4015 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004016 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004017 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004018 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004019 u64 item_end = 0;
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004020 u64 last_size = (u64)-1;
Yan, Zheng80825102009-11-12 09:35:36 +00004021 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004022 int found_extent;
4023 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004024 int pending_del_nr = 0;
4025 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004026 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004027 int ret;
4028 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08004029 u64 ino = btrfs_ino(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004030
4031 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004032
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004033 path = btrfs_alloc_path();
4034 if (!path)
4035 return -ENOMEM;
4036 path->reada = -1;
4037
Josef Bacik5dc562c2012-08-17 13:14:17 -04004038 /*
4039 * We want to drop from the next block forward in case this new size is
4040 * not block aligned since we will be keeping the last block of the
4041 * extent just the way it is.
4042 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004043 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4044 root == root->fs_info->tree_root)
Qu Wenruofda28322013-02-26 08:10:22 +00004045 btrfs_drop_extent_cache(inode, ALIGN(new_size,
4046 root->sectorsize), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004047
Miao Xie16cdcec2011-04-22 18:12:22 +08004048 /*
4049 * This function is also used to drop the items in the log tree before
4050 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4051 * it is used to drop the loged items. So we shouldn't kill the delayed
4052 * items.
4053 */
4054 if (min_type == 0 && root == BTRFS_I(inode)->root)
4055 btrfs_kill_delayed_inode_items(inode);
4056
Li Zefan33345d012011-04-20 10:31:50 +08004057 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004058 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004059 key.type = (u8)-1;
4060
Chris Mason85e21ba2008-01-29 15:11:36 -05004061search_again:
Chris Masonb9473432009-03-13 11:00:37 -04004062 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004063 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004064 if (ret < 0) {
4065 err = ret;
4066 goto out;
4067 }
Chris Masond3977122009-01-05 21:25:51 -05004068
Chris Mason85e21ba2008-01-29 15:11:36 -05004069 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004070 /* there are no items in the tree for us to truncate, we're
4071 * done
4072 */
Yan, Zheng80825102009-11-12 09:35:36 +00004073 if (path->slots[0] == 0)
4074 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004075 path->slots[0]--;
4076 }
4077
Chris Masond3977122009-01-05 21:25:51 -05004078 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004079 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004080 leaf = path->nodes[0];
4081 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4082 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04004083
Li Zefan33345d012011-04-20 10:31:50 +08004084 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004085 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004086
Chris Mason85e21ba2008-01-29 15:11:36 -05004087 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004088 break;
4089
Chris Mason5f39d392007-10-15 16:14:19 -04004090 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004091 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004092 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004093 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004094 extent_type = btrfs_file_extent_type(leaf, fi);
4095 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004096 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004097 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04004098 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004099 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004100 path->slots[0], fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004101 }
Yan008630c2007-11-07 13:31:09 -05004102 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004103 }
Yan, Zheng80825102009-11-12 09:35:36 +00004104 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004105 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004106 } else {
4107 if (item_end < new_size)
4108 break;
4109 if (found_key.offset >= new_size)
4110 del_item = 1;
4111 else
4112 del_item = 0;
4113 }
Chris Mason39279cc2007-06-12 06:35:45 -04004114 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004115 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004116 if (found_type != BTRFS_EXTENT_DATA_KEY)
4117 goto delete;
4118
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004119 if (del_item)
4120 last_size = found_key.offset;
4121 else
4122 last_size = new_size;
4123
Chris Mason179e29e2007-11-01 11:28:41 -04004124 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004125 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004126 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004127 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004128 u64 orig_num_bytes =
4129 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004130 extent_num_bytes = ALIGN(new_size -
4131 found_key.offset,
4132 root->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004133 btrfs_set_file_extent_num_bytes(leaf, fi,
4134 extent_num_bytes);
4135 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004136 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004137 if (test_bit(BTRFS_ROOT_REF_COWS,
4138 &root->state) &&
4139 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004140 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004141 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004142 } else {
Chris Masondb945352007-10-15 16:15:53 -04004143 extent_num_bytes =
4144 btrfs_file_extent_disk_num_bytes(leaf,
4145 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004146 extent_offset = found_key.offset -
4147 btrfs_file_extent_offset(leaf, fi);
4148
Chris Mason39279cc2007-06-12 06:35:45 -04004149 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004150 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004151 if (extent_start != 0) {
4152 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004153 if (test_bit(BTRFS_ROOT_REF_COWS,
4154 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004155 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004156 }
4157 }
Chris Mason90692182008-02-08 13:49:28 -05004158 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004159 /*
4160 * we can't truncate inline items that have had
4161 * special encodings
4162 */
4163 if (!del_item &&
4164 btrfs_file_extent_compression(leaf, fi) == 0 &&
4165 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4166 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004167 u32 size = new_size - found_key.offset;
4168
Miao Xie27cdeb72014-04-02 19:51:05 +08004169 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004170 inode_sub_bytes(inode, item_end + 1 -
4171 new_size);
Chris Mason514ac8a2014-01-03 21:07:00 -08004172
4173 /*
4174 * update the ram bytes to properly reflect
4175 * the new size of our item
4176 */
4177 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
Chris Masone02119d2008-09-05 16:13:11 -04004178 size =
4179 btrfs_file_extent_calc_inline_size(size);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004180 btrfs_truncate_item(root, path, size, 1);
Miao Xie27cdeb72014-04-02 19:51:05 +08004181 } else if (test_bit(BTRFS_ROOT_REF_COWS,
4182 &root->state)) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004183 inode_sub_bytes(inode, item_end + 1 -
4184 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05004185 }
Chris Mason39279cc2007-06-12 06:35:45 -04004186 }
Chris Mason179e29e2007-11-01 11:28:41 -04004187delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004188 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004189 if (!pending_del_nr) {
4190 /* no pending yet, add ourselves */
4191 pending_del_slot = path->slots[0];
4192 pending_del_nr = 1;
4193 } else if (pending_del_nr &&
4194 path->slots[0] + 1 == pending_del_slot) {
4195 /* hop on the pending chunk */
4196 pending_del_nr++;
4197 pending_del_slot = path->slots[0];
4198 } else {
Chris Masond3977122009-01-05 21:25:51 -05004199 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004200 }
Chris Mason39279cc2007-06-12 06:35:45 -04004201 } else {
4202 break;
4203 }
Miao Xie27cdeb72014-04-02 19:51:05 +08004204 if (found_extent &&
4205 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4206 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004207 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004208 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004209 extent_num_bytes, 0,
4210 btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02004211 ino, extent_offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004212 BUG_ON(ret);
4213 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004214
Yan, Zheng80825102009-11-12 09:35:36 +00004215 if (found_type == BTRFS_INODE_ITEM_KEY)
4216 break;
4217
4218 if (path->slots[0] == 0 ||
4219 path->slots[0] != pending_del_slot) {
Yan, Zheng80825102009-11-12 09:35:36 +00004220 if (pending_del_nr) {
4221 ret = btrfs_del_items(trans, root, path,
4222 pending_del_slot,
4223 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004224 if (ret) {
4225 btrfs_abort_transaction(trans,
4226 root, ret);
4227 goto error;
4228 }
Yan, Zheng80825102009-11-12 09:35:36 +00004229 pending_del_nr = 0;
4230 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004231 btrfs_release_path(path);
Chris Mason85e21ba2008-01-29 15:11:36 -05004232 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004233 } else {
4234 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004235 }
Chris Mason39279cc2007-06-12 06:35:45 -04004236 }
Yan, Zheng80825102009-11-12 09:35:36 +00004237out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004238 if (pending_del_nr) {
4239 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4240 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004241 if (ret)
4242 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004243 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004244error:
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004245 if (last_size != (u64)-1)
4246 btrfs_ordered_update_i_size(inode, last_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004247 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00004248 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004249}
4250
Chris Masona52d9a82007-08-27 16:49:44 -04004251/*
Josef Bacik2aaa6652012-08-29 14:27:18 -04004252 * btrfs_truncate_page - read, zero a chunk and write a page
4253 * @inode - inode that we're zeroing
4254 * @from - the offset to start zeroing
4255 * @len - the length to zero, 0 to zero the entire range respective to the
4256 * offset
4257 * @front - zero up to the offset instead of from the offset on
4258 *
4259 * This will find the page for the "from" offset and cow the page and zero the
4260 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004261 */
Josef Bacik2aaa6652012-08-29 14:27:18 -04004262int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
4263 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004264{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004265 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04004266 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004267 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4268 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004269 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004270 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04004271 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04004272 pgoff_t index = from >> PAGE_CACHE_SHIFT;
4273 unsigned offset = from & (PAGE_CACHE_SIZE-1);
4274 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004275 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004276 int ret = 0;
4277 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004278 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004279
Josef Bacik2aaa6652012-08-29 14:27:18 -04004280 if ((offset & (blocksize - 1)) == 0 &&
4281 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004282 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004283 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004284 if (ret)
4285 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004286
Chris Mason211c17f2008-05-15 09:13:45 -04004287again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004288 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004289 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004290 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Miao Xieac6a2b32012-12-05 10:56:13 +00004291 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004292 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004293 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004294
4295 page_start = page_offset(page);
4296 page_end = page_start + PAGE_CACHE_SIZE - 1;
4297
Chris Masona52d9a82007-08-27 16:49:44 -04004298 if (!PageUptodate(page)) {
4299 ret = btrfs_readpage(NULL, page);
4300 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004301 if (page->mapping != mapping) {
4302 unlock_page(page);
4303 page_cache_release(page);
4304 goto again;
4305 }
Chris Masona52d9a82007-08-27 16:49:44 -04004306 if (!PageUptodate(page)) {
4307 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004308 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004309 }
4310 }
Chris Mason211c17f2008-05-15 09:13:45 -04004311 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004312
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004313 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004314 set_page_extent_mapped(page);
4315
4316 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4317 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004318 unlock_extent_cached(io_tree, page_start, page_end,
4319 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004320 unlock_page(page);
4321 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004322 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004323 btrfs_put_ordered_extent(ordered);
4324 goto again;
4325 }
4326
Josef Bacik2ac55d42010-02-03 19:33:23 +00004327 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004328 EXTENT_DIRTY | EXTENT_DELALLOC |
4329 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004330 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004331
Josef Bacik2ac55d42010-02-03 19:33:23 +00004332 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
4333 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004334 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004335 unlock_extent_cached(io_tree, page_start, page_end,
4336 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004337 goto out_unlock;
4338 }
4339
Chris Masone6dcd2d2008-07-17 12:53:50 -04004340 if (offset != PAGE_CACHE_SIZE) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004341 if (!len)
4342 len = PAGE_CACHE_SIZE - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004343 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004344 if (front)
4345 memset(kaddr, 0, offset);
4346 else
4347 memset(kaddr + offset, 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004348 flush_dcache_page(page);
4349 kunmap(page);
4350 }
Chris Mason247e7432008-07-17 12:53:51 -04004351 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004352 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004353 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
4354 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004355
Chris Mason89642222008-07-24 09:41:53 -04004356out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004357 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004358 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04004359 unlock_page(page);
4360 page_cache_release(page);
4361out:
4362 return ret;
4363}
4364
Josef Bacik16e75492013-10-22 12:18:51 -04004365static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4366 u64 offset, u64 len)
4367{
4368 struct btrfs_trans_handle *trans;
4369 int ret;
4370
4371 /*
4372 * Still need to make sure the inode looks like it's been updated so
4373 * that any holes get logged if we fsync.
4374 */
4375 if (btrfs_fs_incompat(root->fs_info, NO_HOLES)) {
4376 BTRFS_I(inode)->last_trans = root->fs_info->generation;
4377 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4378 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4379 return 0;
4380 }
4381
4382 /*
4383 * 1 - for the one we're dropping
4384 * 1 - for the one we're adding
4385 * 1 - for updating the inode.
4386 */
4387 trans = btrfs_start_transaction(root, 3);
4388 if (IS_ERR(trans))
4389 return PTR_ERR(trans);
4390
4391 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4392 if (ret) {
4393 btrfs_abort_transaction(trans, root, ret);
4394 btrfs_end_transaction(trans, root);
4395 return ret;
4396 }
4397
4398 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset,
4399 0, 0, len, 0, len, 0, 0, 0);
4400 if (ret)
4401 btrfs_abort_transaction(trans, root, ret);
4402 else
4403 btrfs_update_inode(trans, root, inode);
4404 btrfs_end_transaction(trans, root);
4405 return ret;
4406}
4407
Josef Bacik695a0d02011-03-04 15:46:53 -05004408/*
4409 * This function puts in dummy file extents for the area we're creating a hole
4410 * for. So if we are truncating this file to a larger size we need to insert
4411 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4412 * the range between oldsize and size
4413 */
Josef Bacika41ad392011-01-31 15:30:16 -05004414int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004415{
Yan Zheng9036c102008-10-30 14:19:41 -04004416 struct btrfs_root *root = BTRFS_I(inode)->root;
4417 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004418 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004419 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004420 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Qu Wenruofda28322013-02-26 08:10:22 +00004421 u64 hole_start = ALIGN(oldsize, root->sectorsize);
4422 u64 block_end = ALIGN(size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004423 u64 last_byte;
4424 u64 cur_offset;
4425 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004426 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004427
Josef Bacika71754f2013-06-17 17:14:39 -04004428 /*
4429 * If our size started in the middle of a page we need to zero out the
4430 * rest of the page before we expand the i_size, otherwise we could
4431 * expose stale data.
4432 */
4433 err = btrfs_truncate_page(inode, oldsize, 0, 0);
4434 if (err)
4435 return err;
4436
Yan Zheng9036c102008-10-30 14:19:41 -04004437 if (size <= hole_start)
4438 return 0;
4439
Yan Zheng9036c102008-10-30 14:19:41 -04004440 while (1) {
4441 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004442
Josef Bacik2ac55d42010-02-03 19:33:23 +00004443 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004444 &cached_state);
Miao Xiefa7c1492013-09-26 13:15:27 +08004445 ordered = btrfs_lookup_ordered_range(inode, hole_start,
4446 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004447 if (!ordered)
4448 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004449 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4450 &cached_state, GFP_NOFS);
Miao Xiefa7c1492013-09-26 13:15:27 +08004451 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004452 btrfs_put_ordered_extent(ordered);
4453 }
4454
Yan Zheng9036c102008-10-30 14:19:41 -04004455 cur_offset = hole_start;
4456 while (1) {
4457 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4458 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004459 if (IS_ERR(em)) {
4460 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004461 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004462 break;
4463 }
Yan Zheng9036c102008-10-30 14:19:41 -04004464 last_byte = min(extent_map_end(em), block_end);
Qu Wenruofda28322013-02-26 08:10:22 +00004465 last_byte = ALIGN(last_byte , root->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004466 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004467 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004468 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004469
Josef Bacik16e75492013-10-22 12:18:51 -04004470 err = maybe_insert_hole(root, inode, cur_offset,
4471 hole_size);
4472 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004473 break;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004474 btrfs_drop_extent_cache(inode, cur_offset,
4475 cur_offset + hole_size - 1, 0);
4476 hole_em = alloc_extent_map();
4477 if (!hole_em) {
4478 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4479 &BTRFS_I(inode)->runtime_flags);
4480 goto next;
4481 }
4482 hole_em->start = cur_offset;
4483 hole_em->len = hole_size;
4484 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004485
Josef Bacik5dc562c2012-08-17 13:14:17 -04004486 hole_em->block_start = EXTENT_MAP_HOLE;
4487 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004488 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004489 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004490 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
4491 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Josef Bacik16e75492013-10-22 12:18:51 -04004492 hole_em->generation = root->fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004493
4494 while (1) {
4495 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004496 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004497 write_unlock(&em_tree->lock);
4498 if (err != -EEXIST)
4499 break;
4500 btrfs_drop_extent_cache(inode, cur_offset,
4501 cur_offset +
4502 hole_size - 1, 0);
4503 }
4504 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04004505 }
Josef Bacik16e75492013-10-22 12:18:51 -04004506next:
Yan Zheng9036c102008-10-30 14:19:41 -04004507 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004508 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004509 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004510 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004511 break;
4512 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004513 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004514 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4515 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004516 return err;
4517}
4518
Eric Sandeen3972f262013-01-12 02:57:22 +00004519static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004520{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004521 struct btrfs_root *root = BTRFS_I(inode)->root;
4522 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004523 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004524 loff_t newsize = attr->ia_size;
4525 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004526 int ret;
4527
Eric Sandeen3972f262013-01-12 02:57:22 +00004528 /*
4529 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4530 * special case where we need to update the times despite not having
4531 * these flags set. For all other operations the VFS set these flags
4532 * explicitly if it wants a timestamp update.
4533 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004534 if (newsize != oldsize) {
4535 inode_inc_iversion(inode);
4536 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
4537 inode->i_ctime = inode->i_mtime =
4538 current_fs_time(inode->i_sb);
4539 }
Eric Sandeen3972f262013-01-12 02:57:22 +00004540
Josef Bacika41ad392011-01-31 15:30:16 -05004541 if (newsize > oldsize) {
Kirill A. Shutemov7caef262013-09-12 15:13:56 -07004542 truncate_pagecache(inode, newsize);
Josef Bacika41ad392011-01-31 15:30:16 -05004543 ret = btrfs_cont_expand(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004544 if (ret)
Yan, Zheng80825102009-11-12 09:35:36 +00004545 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004546
Miao Xief4a2f4c2011-12-14 20:12:01 -05004547 trans = btrfs_start_transaction(root, 1);
4548 if (IS_ERR(trans))
4549 return PTR_ERR(trans);
4550
4551 i_size_write(inode, newsize);
4552 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
4553 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004554 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05004555 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004556
Josef Bacika41ad392011-01-31 15:30:16 -05004557 /*
4558 * We're truncating a file that used to have good data down to
4559 * zero. Make sure it gets into the ordered flush list so that
4560 * any new writes get down to disk quickly.
4561 */
4562 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004563 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4564 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004565
Josef Bacikf3fe8202013-01-07 17:03:21 -05004566 /*
4567 * 1 for the orphan item we're going to add
4568 * 1 for the orphan item deletion.
4569 */
4570 trans = btrfs_start_transaction(root, 2);
4571 if (IS_ERR(trans))
4572 return PTR_ERR(trans);
4573
4574 /*
4575 * We need to do this in case we fail at _any_ point during the
4576 * actual truncate. Once we do the truncate_setsize we could
4577 * invalidate pages which forces any outstanding ordered io to
4578 * be instantly completed which will give us extents that need
4579 * to be truncated. If we fail to get an orphan inode down we
4580 * could have left over extents that were never meant to live,
4581 * so we need to garuntee from this point on that everything
4582 * will be consistent.
4583 */
4584 ret = btrfs_orphan_add(trans, inode);
4585 btrfs_end_transaction(trans, root);
4586 if (ret)
4587 return ret;
4588
Josef Bacika41ad392011-01-31 15:30:16 -05004589 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4590 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004591
4592 /* Disable nonlocked read DIO to avoid the end less truncate */
4593 btrfs_inode_block_unlocked_dio(inode);
4594 inode_dio_wait(inode);
4595 btrfs_inode_resume_unlocked_dio(inode);
4596
Josef Bacika41ad392011-01-31 15:30:16 -05004597 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004598 if (ret && inode->i_nlink) {
4599 int err;
4600
4601 /*
4602 * failed to truncate, disk_i_size is only adjusted down
4603 * as we remove extents, so it should represent the true
4604 * size of the inode, so reset the in memory size and
4605 * delete our orphan entry.
4606 */
4607 trans = btrfs_join_transaction(root);
4608 if (IS_ERR(trans)) {
4609 btrfs_orphan_del(NULL, inode);
4610 return ret;
4611 }
4612 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
4613 err = btrfs_orphan_del(trans, inode);
4614 if (err)
4615 btrfs_abort_transaction(trans, root, err);
4616 btrfs_end_transaction(trans, root);
4617 }
Yan, Zheng80825102009-11-12 09:35:36 +00004618 }
4619
Josef Bacika41ad392011-01-31 15:30:16 -05004620 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004621}
4622
Chris Mason39279cc2007-06-12 06:35:45 -04004623static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
4624{
4625 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08004626 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004627 int err;
4628
Li Zefanb83cc962010-12-20 16:04:08 +08004629 if (btrfs_root_readonly(root))
4630 return -EROFS;
4631
Chris Mason39279cc2007-06-12 06:35:45 -04004632 err = inode_change_ok(inode, attr);
4633 if (err)
4634 return err;
4635
Chris Mason5a3f23d2009-03-31 13:27:11 -04004636 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00004637 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00004638 if (err)
4639 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004640 }
Yan Zheng9036c102008-10-30 14:19:41 -04004641
Christoph Hellwig10257742010-06-04 11:30:02 +02004642 if (attr->ia_valid) {
4643 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004644 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004645 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04004646
Josef Bacik22c44fe2011-11-30 10:45:38 -05004647 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08004648 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02004649 }
4650
Chris Mason39279cc2007-06-12 06:35:45 -04004651 return err;
4652}
Chris Mason61295eb2008-01-14 16:24:38 -05004653
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004654/*
4655 * While truncating the inode pages during eviction, we get the VFS calling
4656 * btrfs_invalidatepage() against each page of the inode. This is slow because
4657 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
4658 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
4659 * extent_state structures over and over, wasting lots of time.
4660 *
4661 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
4662 * those expensive operations on a per page basis and do only the ordered io
4663 * finishing, while we release here the extent_map and extent_state structures,
4664 * without the excessive merging and splitting.
4665 */
4666static void evict_inode_truncate_pages(struct inode *inode)
4667{
4668 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4669 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
4670 struct rb_node *node;
4671
4672 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07004673 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004674
4675 write_lock(&map_tree->lock);
4676 while (!RB_EMPTY_ROOT(&map_tree->map)) {
4677 struct extent_map *em;
4678
4679 node = rb_first(&map_tree->map);
4680 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08004681 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
4682 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004683 remove_extent_mapping(map_tree, em);
4684 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01004685 if (need_resched()) {
4686 write_unlock(&map_tree->lock);
4687 cond_resched();
4688 write_lock(&map_tree->lock);
4689 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004690 }
4691 write_unlock(&map_tree->lock);
4692
4693 spin_lock(&io_tree->lock);
4694 while (!RB_EMPTY_ROOT(&io_tree->state)) {
4695 struct extent_state *state;
4696 struct extent_state *cached_state = NULL;
4697
4698 node = rb_first(&io_tree->state);
4699 state = rb_entry(node, struct extent_state, rb_node);
4700 atomic_inc(&state->refs);
4701 spin_unlock(&io_tree->lock);
4702
4703 lock_extent_bits(io_tree, state->start, state->end,
4704 0, &cached_state);
4705 clear_extent_bit(io_tree, state->start, state->end,
4706 EXTENT_LOCKED | EXTENT_DIRTY |
4707 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
4708 EXTENT_DEFRAG, 1, 1,
4709 &cached_state, GFP_NOFS);
4710 free_extent_state(state);
4711
Filipe Manana7064dd52014-08-08 02:47:05 +01004712 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004713 spin_lock(&io_tree->lock);
4714 }
4715 spin_unlock(&io_tree->lock);
4716}
4717
Al Virobd555972010-06-07 11:35:40 -04004718void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004719{
4720 struct btrfs_trans_handle *trans;
4721 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04004722 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik07127182011-08-19 10:29:59 -04004723 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04004724 int ret;
4725
liubo1abe9b82011-03-24 11:18:59 +00004726 trace_btrfs_inode_evict(inode);
4727
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004728 evict_inode_truncate_pages(inode);
4729
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02004730 if (inode->i_nlink &&
4731 ((btrfs_root_refs(&root->root_item) != 0 &&
4732 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
4733 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04004734 goto no_delete;
4735
Chris Mason39279cc2007-06-12 06:35:45 -04004736 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04004737 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004738 goto no_delete;
4739 }
Al Virobd555972010-06-07 11:35:40 -04004740 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04004741 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04004742
Yan, Zhengc71bf092009-11-12 09:34:40 +00004743 if (root->fs_info->log_root_recovering) {
Liu Bo6bf02312012-06-25 21:59:09 -06004744 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04004745 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00004746 goto no_delete;
4747 }
4748
Yan, Zheng76dda932009-09-21 16:00:26 -04004749 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02004750 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
4751 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04004752 goto no_delete;
4753 }
4754
Miao Xie0e8c36a2012-12-19 06:59:51 +00004755 ret = btrfs_commit_inode_delayed_inode(inode);
4756 if (ret) {
4757 btrfs_orphan_del(NULL, inode);
4758 goto no_delete;
4759 }
4760
Miao Xie66d8f3d2012-09-06 04:02:28 -06004761 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04004762 if (!rsv) {
4763 btrfs_orphan_del(NULL, inode);
4764 goto no_delete;
4765 }
Josef Bacik4a338542011-08-29 11:01:31 -04004766 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04004767 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04004768 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04004769
Chris Masondbe674a2008-07-17 12:54:05 -04004770 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04004771
Josef Bacik4289a662011-08-05 13:22:24 -04004772 /*
Miao Xie8407aa42012-09-07 01:43:32 -06004773 * This is a bit simpler than btrfs_truncate since we've already
4774 * reserved our space for our orphan item in the unlink, so we just
4775 * need to reserve some slack space in case we add bytes and update
4776 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04004777 */
Yan, Zheng80825102009-11-12 09:35:36 +00004778 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00004779 ret = btrfs_block_rsv_refill(root, rsv, min_size,
4780 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00004781
Josef Bacik726c35f2011-09-26 15:46:06 -04004782 /*
4783 * Try and steal from the global reserve since we will
4784 * likely not use this space anyway, we want to try as
4785 * hard as possible to get this to work.
4786 */
4787 if (ret)
4788 ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
4789
Yan, Zhengd68fc572010-05-16 10:49:58 -04004790 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004791 btrfs_warn(root->fs_info,
4792 "Could not get space for a delete, will truncate on mount %d",
4793 ret);
Josef Bacik4289a662011-08-05 13:22:24 -04004794 btrfs_orphan_del(NULL, inode);
4795 btrfs_free_block_rsv(root, rsv);
4796 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04004797 }
4798
Miao Xie0e8c36a2012-12-19 06:59:51 +00004799 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04004800 if (IS_ERR(trans)) {
4801 btrfs_orphan_del(NULL, inode);
4802 btrfs_free_block_rsv(root, rsv);
4803 goto no_delete;
4804 }
4805
4806 trans->block_rsv = rsv;
4807
Yan, Zhengd68fc572010-05-16 10:49:58 -04004808 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04004809 if (ret != -ENOSPC)
Yan, Zheng80825102009-11-12 09:35:36 +00004810 break;
4811
Miao Xie8407aa42012-09-07 01:43:32 -06004812 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004813 btrfs_end_transaction(trans, root);
4814 trans = NULL;
Liu Bob53d3f52012-11-14 14:34:34 +00004815 btrfs_btree_balance_dirty(root);
Josef Bacik7b128762008-07-24 12:17:14 -04004816 }
4817
Josef Bacik4289a662011-08-05 13:22:24 -04004818 btrfs_free_block_rsv(root, rsv);
4819
Josef Bacik4ef31a42013-08-13 14:10:08 -04004820 /*
4821 * Errors here aren't a big deal, it just means we leave orphan items
4822 * in the tree. They will be cleaned up on the next mount.
4823 */
Yan, Zheng80825102009-11-12 09:35:36 +00004824 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04004825 trans->block_rsv = root->orphan_block_rsv;
Josef Bacik4ef31a42013-08-13 14:10:08 -04004826 btrfs_orphan_del(trans, inode);
4827 } else {
4828 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004829 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004830
Josef Bacik4289a662011-08-05 13:22:24 -04004831 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08004832 if (!(root == root->fs_info->tree_root ||
4833 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08004834 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08004835
Chris Mason54aa1f42007-06-22 14:16:25 -04004836 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004837 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04004838no_delete:
Miao Xie89042e52013-05-15 07:48:15 +00004839 btrfs_remove_delayed_node(inode);
Jan Karadbd57682012-05-03 14:48:02 +02004840 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004841 return;
Chris Mason39279cc2007-06-12 06:35:45 -04004842}
4843
4844/*
4845 * this returns the key found in the dir entry in the location pointer.
4846 * If no dir entries were found, location->objectid is 0.
4847 */
4848static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
4849 struct btrfs_key *location)
4850{
4851 const char *name = dentry->d_name.name;
4852 int namelen = dentry->d_name.len;
4853 struct btrfs_dir_item *di;
4854 struct btrfs_path *path;
4855 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04004856 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004857
4858 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07004859 if (!path)
4860 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05004861
Li Zefan33345d012011-04-20 10:31:50 +08004862 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04004863 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04004864 if (IS_ERR(di))
4865 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05004866
David Sterbac7040052011-04-19 18:00:01 +02004867 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05004868 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05004869
Chris Mason5f39d392007-10-15 16:14:19 -04004870 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04004871out:
Chris Mason39279cc2007-06-12 06:35:45 -04004872 btrfs_free_path(path);
4873 return ret;
Chris Mason39544012007-12-12 14:38:19 -05004874out_err:
4875 location->objectid = 0;
4876 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04004877}
4878
4879/*
4880 * when we hit a tree root in a directory, the btrfs part of the inode
4881 * needs to be changed to reflect the root directory of the tree root. This
4882 * is kind of like crossing a mount point.
4883 */
4884static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004885 struct inode *dir,
4886 struct dentry *dentry,
4887 struct btrfs_key *location,
4888 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04004889{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004890 struct btrfs_path *path;
4891 struct btrfs_root *new_root;
4892 struct btrfs_root_ref *ref;
4893 struct extent_buffer *leaf;
4894 int ret;
4895 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004896
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004897 path = btrfs_alloc_path();
4898 if (!path) {
4899 err = -ENOMEM;
4900 goto out;
4901 }
Chris Mason39279cc2007-06-12 06:35:45 -04004902
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004903 err = -ENOENT;
Kelley Nielsen75ac2dd2013-11-04 19:35:58 -08004904 ret = btrfs_find_item(root->fs_info->tree_root, path,
4905 BTRFS_I(dir)->root->root_key.objectid,
4906 location->objectid, BTRFS_ROOT_REF_KEY, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004907 if (ret) {
4908 if (ret < 0)
4909 err = ret;
4910 goto out;
4911 }
Chris Mason39279cc2007-06-12 06:35:45 -04004912
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004913 leaf = path->nodes[0];
4914 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08004915 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004916 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
4917 goto out;
4918
4919 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
4920 (unsigned long)(ref + 1),
4921 dentry->d_name.len);
4922 if (ret)
4923 goto out;
4924
David Sterbab3b4aa72011-04-21 01:20:15 +02004925 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004926
4927 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
4928 if (IS_ERR(new_root)) {
4929 err = PTR_ERR(new_root);
4930 goto out;
4931 }
4932
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004933 *sub_root = new_root;
4934 location->objectid = btrfs_root_dirid(&new_root->root_item);
4935 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04004936 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004937 err = 0;
4938out:
4939 btrfs_free_path(path);
4940 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004941}
4942
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004943static void inode_tree_add(struct inode *inode)
4944{
4945 struct btrfs_root *root = BTRFS_I(inode)->root;
4946 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004947 struct rb_node **p;
4948 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01004949 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Li Zefan33345d012011-04-20 10:31:50 +08004950 u64 ino = btrfs_ino(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004951
Al Viro1d3382cb2010-10-23 15:19:20 -04004952 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004953 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00004954 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004955 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00004956 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004957 while (*p) {
4958 parent = *p;
4959 entry = rb_entry(parent, struct btrfs_inode, rb_node);
4960
Li Zefan33345d012011-04-20 10:31:50 +08004961 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004962 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004963 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004964 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004965 else {
4966 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04004967 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01004968 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004969 RB_CLEAR_NODE(parent);
4970 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01004971 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004972 }
4973 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01004974 rb_link_node(new, parent, p);
4975 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004976 spin_unlock(&root->inode_lock);
4977}
4978
4979static void inode_tree_del(struct inode *inode)
4980{
4981 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04004982 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004983
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004984 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004985 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004986 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004987 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04004988 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004989 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004990 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04004991
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02004992 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004993 synchronize_srcu(&root->fs_info->subvol_srcu);
4994 spin_lock(&root->inode_lock);
4995 empty = RB_EMPTY_ROOT(&root->inode_tree);
4996 spin_unlock(&root->inode_lock);
4997 if (empty)
4998 btrfs_add_dead_root(root);
4999 }
5000}
5001
Jeff Mahoney143bede2012-03-01 14:56:26 +01005002void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005003{
5004 struct rb_node *node;
5005 struct rb_node *prev;
5006 struct btrfs_inode *entry;
5007 struct inode *inode;
5008 u64 objectid = 0;
5009
Liu Bo7813b3d2014-02-10 17:37:25 +08005010 if (!test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
5011 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04005012
5013 spin_lock(&root->inode_lock);
5014again:
5015 node = root->inode_tree.rb_node;
5016 prev = NULL;
5017 while (node) {
5018 prev = node;
5019 entry = rb_entry(node, struct btrfs_inode, rb_node);
5020
Li Zefan33345d012011-04-20 10:31:50 +08005021 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005022 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08005023 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005024 node = node->rb_right;
5025 else
5026 break;
5027 }
5028 if (!node) {
5029 while (prev) {
5030 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08005031 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005032 node = prev;
5033 break;
5034 }
5035 prev = rb_next(prev);
5036 }
5037 }
5038 while (node) {
5039 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08005040 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04005041 inode = igrab(&entry->vfs_inode);
5042 if (inode) {
5043 spin_unlock(&root->inode_lock);
5044 if (atomic_read(&inode->i_count) > 1)
5045 d_prune_aliases(inode);
5046 /*
Al Viro45321ac2010-06-07 13:43:19 -04005047 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04005048 * the inode cache when its usage count
5049 * hits zero.
5050 */
5051 iput(inode);
5052 cond_resched();
5053 spin_lock(&root->inode_lock);
5054 goto again;
5055 }
5056
5057 if (cond_resched_lock(&root->inode_lock))
5058 goto again;
5059
5060 node = rb_next(node);
5061 }
5062 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005063}
5064
Chris Masone02119d2008-09-05 16:13:11 -04005065static int btrfs_init_locked_inode(struct inode *inode, void *p)
5066{
5067 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005068 inode->i_ino = args->location->objectid;
5069 memcpy(&BTRFS_I(inode)->location, args->location,
5070 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005071 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005072 return 0;
5073}
5074
5075static int btrfs_find_actor(struct inode *inode, void *opaque)
5076{
5077 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005078 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005079 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005080}
5081
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005082static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005083 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005084 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005085{
5086 struct inode *inode;
5087 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005088 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005089
Chris Mason90d3e592014-01-09 17:28:00 -08005090 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005091 args.root = root;
5092
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005093 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005094 btrfs_init_locked_inode,
5095 (void *)&args);
5096 return inode;
5097}
5098
Balaji Rao1a54ef82008-07-21 02:01:04 +05305099/* Get an inode object given its location and corresponding root.
5100 * Returns in *is_new if the inode was read from disk
5101 */
5102struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005103 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305104{
5105 struct inode *inode;
5106
Chris Mason90d3e592014-01-09 17:28:00 -08005107 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305108 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005109 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305110
5111 if (inode->i_state & I_NEW) {
Balaji Rao1a54ef82008-07-21 02:01:04 +05305112 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005113 if (!is_bad_inode(inode)) {
5114 inode_tree_add(inode);
5115 unlock_new_inode(inode);
5116 if (new)
5117 *new = 1;
5118 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005119 unlock_new_inode(inode);
5120 iput(inode);
5121 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005122 }
5123 }
5124
Balaji Rao1a54ef82008-07-21 02:01:04 +05305125 return inode;
5126}
5127
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005128static struct inode *new_simple_dir(struct super_block *s,
5129 struct btrfs_key *key,
5130 struct btrfs_root *root)
5131{
5132 struct inode *inode = new_inode(s);
5133
5134 if (!inode)
5135 return ERR_PTR(-ENOMEM);
5136
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005137 BTRFS_I(inode)->root = root;
5138 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005139 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005140
5141 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005142 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005143 inode->i_fop = &simple_dir_operations;
5144 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
5145 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
5146
5147 return inode;
5148}
5149
Chris Mason3de45862008-11-17 21:02:50 -05005150struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005151{
Chris Masond3977122009-01-05 21:25:51 -05005152 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005153 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005154 struct btrfs_root *sub_root = root;
5155 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005156 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005157 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005158
5159 if (dentry->d_name.len > BTRFS_NAME_LEN)
5160 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005161
Jeff Layton39e3c952012-11-28 11:30:53 -05005162 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005163 if (ret < 0)
5164 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005165
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005166 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005167 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005168
5169 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005170 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005171 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005172 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005173
5174 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5175
Yan, Zheng76dda932009-09-21 16:00:26 -04005176 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005177 ret = fixup_tree_root_location(root, dir, dentry,
5178 &location, &sub_root);
5179 if (ret < 0) {
5180 if (ret != -ENOENT)
5181 inode = ERR_PTR(ret);
5182 else
5183 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5184 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005185 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005186 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005187 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
5188
Julia Lawall34d19ba2011-01-24 19:55:19 +00005189 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00005190 down_read(&root->fs_info->cleanup_work_sem);
5191 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005192 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005193 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005194 if (ret) {
5195 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005196 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005197 }
Filipe Manana9c3b3062014-07-31 14:41:07 +01005198 /*
5199 * If orphan cleanup did remove any orphans, it means the tree
5200 * was modified and therefore the commit root is not the same as
5201 * the current root anymore. This is a problem, because send
5202 * uses the commit root and therefore can see inode items that
5203 * don't exist in the current root anymore, and for example make
5204 * calls to btrfs_iget, which will do tree lookups based on the
5205 * current root and not on the commit root. Those lookups will
5206 * fail, returning a -ESTALE error, and making send fail with
5207 * that error. So make sure a send does not see any orphans we
5208 * have just removed, and that it will see the same inodes
5209 * regardless of whether a transaction commit happened before
5210 * it started (meaning that the commit root will be the same as
5211 * the current root) or not.
5212 */
5213 if (sub_root->node != sub_root->commit_root) {
5214 u64 sub_flags = btrfs_root_flags(&sub_root->root_item);
5215
5216 if (sub_flags & BTRFS_ROOT_SUBVOL_RDONLY) {
5217 struct extent_buffer *eb;
5218
5219 /*
5220 * Assert we can't have races between dentry
5221 * lookup called through the snapshot creation
5222 * ioctl and the VFS.
5223 */
5224 ASSERT(mutex_is_locked(&dir->i_mutex));
5225
5226 down_write(&root->fs_info->commit_root_sem);
5227 eb = sub_root->commit_root;
5228 sub_root->commit_root =
5229 btrfs_root_node(sub_root);
5230 up_write(&root->fs_info->commit_root_sem);
5231 free_extent_buffer(eb);
5232 }
5233 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005234 }
5235
Chris Mason3de45862008-11-17 21:02:50 -05005236 return inode;
5237}
5238
Nick Pigginfe15ce42011-01-07 17:49:23 +11005239static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005240{
5241 struct btrfs_root *root;
Li Zefan848cce02012-02-21 17:04:28 +08005242 struct inode *inode = dentry->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005243
Li Zefan848cce02012-02-21 17:04:28 +08005244 if (!inode && !IS_ROOT(dentry))
5245 inode = dentry->d_parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005246
Li Zefan848cce02012-02-21 17:04:28 +08005247 if (inode) {
5248 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005249 if (btrfs_root_refs(&root->root_item) == 0)
5250 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005251
5252 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5253 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005254 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005255 return 0;
5256}
5257
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005258static void btrfs_dentry_release(struct dentry *dentry)
5259{
Daeseok Youn944a4512014-04-14 15:37:02 +09005260 kfree(dentry->d_fsdata);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005261}
5262
Chris Mason3de45862008-11-17 21:02:50 -05005263static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005264 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005265{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005266 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005267
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005268 inode = btrfs_lookup_dentry(dir, dentry);
5269 if (IS_ERR(inode)) {
5270 if (PTR_ERR(inode) == -ENOENT)
5271 inode = NULL;
5272 else
5273 return ERR_CAST(inode);
5274 }
5275
Josef Bacik3a0dfa62014-02-14 13:43:48 -05005276 return d_materialise_unique(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005277}
5278
Miao Xie16cdcec2011-04-22 18:12:22 +08005279unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005280 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5281};
5282
Al Viro9cdda8d2013-05-22 16:48:09 -04005283static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005284{
Al Viro9cdda8d2013-05-22 16:48:09 -04005285 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005286 struct btrfs_root *root = BTRFS_I(inode)->root;
5287 struct btrfs_item *item;
5288 struct btrfs_dir_item *di;
5289 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005290 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005291 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005292 struct list_head ins_list;
5293 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005294 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005295 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005296 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005297 unsigned char d_type;
5298 int over = 0;
5299 u32 di_cur;
5300 u32 di_total;
5301 u32 di_len;
5302 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005303 char tmp_name[32];
5304 char *name_ptr;
5305 int name_len;
Al Viro9cdda8d2013-05-22 16:48:09 -04005306 int is_curr = 0; /* ctx->pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04005307
5308 /* FIXME, use a real flag for deciding about the key type */
5309 if (root->fs_info->tree_root == root)
5310 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005311
Al Viro9cdda8d2013-05-22 16:48:09 -04005312 if (!dir_emit_dots(file, ctx))
5313 return 0;
5314
David Woodhouse49593bf2008-08-17 17:08:36 +01005315 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005316 if (!path)
5317 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005318
Josef Bacik026fd312011-05-13 10:32:11 -04005319 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005320
Miao Xie16cdcec2011-04-22 18:12:22 +08005321 if (key_type == BTRFS_DIR_INDEX_KEY) {
5322 INIT_LIST_HEAD(&ins_list);
5323 INIT_LIST_HEAD(&del_list);
5324 btrfs_get_delayed_items(inode, &ins_list, &del_list);
5325 }
5326
Chris Mason39279cc2007-06-12 06:35:45 -04005327 btrfs_set_key_type(&key, key_type);
Al Viro9cdda8d2013-05-22 16:48:09 -04005328 key.offset = ctx->pos;
Li Zefan33345d012011-04-20 10:31:50 +08005329 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005330
Chris Mason39279cc2007-06-12 06:35:45 -04005331 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5332 if (ret < 0)
5333 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005334
5335 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005336 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005337 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005338 if (slot >= btrfs_header_nritems(leaf)) {
5339 ret = btrfs_next_leaf(root, path);
5340 if (ret < 0)
5341 goto err;
5342 else if (ret > 0)
5343 break;
5344 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005345 }
Chris Mason3de45862008-11-17 21:02:50 -05005346
Ross Kirkdd3cc162013-09-16 15:58:09 +01005347 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04005348 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5349
5350 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005351 break;
Chris Mason5f39d392007-10-15 16:14:19 -04005352 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04005353 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005354 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005355 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08005356 if (key_type == BTRFS_DIR_INDEX_KEY &&
5357 btrfs_should_delete_dir_index(&del_list,
5358 found_key.offset))
5359 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005360
Al Viro9cdda8d2013-05-22 16:48:09 -04005361 ctx->pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08005362 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005363
Chris Mason39279cc2007-06-12 06:35:45 -04005364 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
5365 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005366 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01005367
5368 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04005369 struct btrfs_key location;
5370
Josef Bacik22a94d42011-03-16 16:47:17 -04005371 if (verify_dir_item(root, leaf, di))
5372 break;
5373
Chris Mason5f39d392007-10-15 16:14:19 -04005374 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01005375 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04005376 name_ptr = tmp_name;
5377 } else {
5378 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01005379 if (!name_ptr) {
5380 ret = -ENOMEM;
5381 goto err;
5382 }
Chris Mason5f39d392007-10-15 16:14:19 -04005383 }
5384 read_extent_buffer(leaf, name_ptr,
5385 (unsigned long)(di + 1), name_len);
5386
5387 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5388 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05005389
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005390
Chris Mason3de45862008-11-17 21:02:50 -05005391 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01005392 * skip it.
5393 *
5394 * In contrast to old kernels, we insert the snapshot's
5395 * dir item and dir index after it has been created, so
5396 * we won't find a reference to our own snapshot. We
5397 * still keep the following code for backward
5398 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05005399 */
5400 if (location.type == BTRFS_ROOT_ITEM_KEY &&
5401 location.objectid == root->root_key.objectid) {
5402 over = 0;
5403 goto skip;
5404 }
Al Viro9cdda8d2013-05-22 16:48:09 -04005405 over = !dir_emit(ctx, name_ptr, name_len,
5406 location.objectid, d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04005407
Chris Mason3de45862008-11-17 21:02:50 -05005408skip:
Chris Mason5f39d392007-10-15 16:14:19 -04005409 if (name_ptr != tmp_name)
5410 kfree(name_ptr);
5411
Chris Mason39279cc2007-06-12 06:35:45 -04005412 if (over)
5413 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05005414 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01005415 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04005416 di_cur += di_len;
5417 di = (struct btrfs_dir_item *)((char *)di + di_len);
5418 }
Li Zefanb9e03af2011-03-23 10:43:58 +08005419next:
5420 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005421 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005422
Miao Xie16cdcec2011-04-22 18:12:22 +08005423 if (key_type == BTRFS_DIR_INDEX_KEY) {
5424 if (is_curr)
Al Viro9cdda8d2013-05-22 16:48:09 -04005425 ctx->pos++;
5426 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005427 if (ret)
5428 goto nopos;
5429 }
5430
David Woodhouse49593bf2008-08-17 17:08:36 +01005431 /* Reached end of directory/root. Bump pos past the last item. */
Zach Browndb62efb2013-07-11 16:19:42 -07005432 ctx->pos++;
5433
5434 /*
5435 * Stop new entries from being returned after we return the last
5436 * entry.
5437 *
5438 * New directory entries are assigned a strictly increasing
5439 * offset. This means that new entries created during readdir
5440 * are *guaranteed* to be seen in the future by that readdir.
5441 * This has broken buggy programs which operate on names as
5442 * they're returned by readdir. Until we re-use freed offsets
5443 * we have this hack to stop new entries from being returned
5444 * under the assumption that they'll never reach this huge
5445 * offset.
5446 *
5447 * This is being careful not to overflow 32bit loff_t unless the
5448 * last entry requires it because doing so has broken 32bit apps
5449 * in the past.
5450 */
5451 if (key_type == BTRFS_DIR_INDEX_KEY) {
5452 if (ctx->pos >= INT_MAX)
5453 ctx->pos = LLONG_MAX;
5454 else
5455 ctx->pos = INT_MAX;
5456 }
Chris Mason39279cc2007-06-12 06:35:45 -04005457nopos:
5458 ret = 0;
5459err:
Miao Xie16cdcec2011-04-22 18:12:22 +08005460 if (key_type == BTRFS_DIR_INDEX_KEY)
5461 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005462 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005463 return ret;
5464}
5465
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005466int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005467{
5468 struct btrfs_root *root = BTRFS_I(inode)->root;
5469 struct btrfs_trans_handle *trans;
5470 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005471 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005472
Josef Bacik72ac3c02012-05-23 14:13:11 -04005473 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005474 return 0;
5475
Liu Bo83eea1f2012-07-10 05:28:39 -06005476 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08005477 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005478
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005479 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005480 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005481 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005482 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005483 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005484 if (IS_ERR(trans))
5485 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06005486 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005487 }
5488 return ret;
5489}
5490
5491/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005492 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005493 * inode changes. But, it is most likely to find the inode in cache.
5494 * FIXME, needs more benchmarking...there are no reasons other than performance
5495 * to keep or drop this code.
5496 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005497static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005498{
5499 struct btrfs_root *root = BTRFS_I(inode)->root;
5500 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005501 int ret;
5502
Josef Bacik72ac3c02012-05-23 14:13:11 -04005503 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005504 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005505
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005506 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005507 if (IS_ERR(trans))
5508 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005509
5510 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005511 if (ret && ret == -ENOSPC) {
5512 /* whoops, lets try again with the full transaction */
5513 btrfs_end_transaction(trans, root);
5514 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005515 if (IS_ERR(trans))
5516 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005517
Chris Mason94b60442010-05-26 11:02:00 -04005518 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005519 }
Chris Mason39279cc2007-06-12 06:35:45 -04005520 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08005521 if (BTRFS_I(inode)->delayed_node)
5522 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005523
5524 return ret;
5525}
5526
5527/*
5528 * This is a copy of file_update_time. We need this so we can return error on
5529 * ENOSPC for updating the inode in the case of file write and mmap writes.
5530 */
Josef Bacike41f9412012-03-26 09:46:47 -04005531static int btrfs_update_time(struct inode *inode, struct timespec *now,
5532 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005533{
Alexander Block2bc5565282012-06-15 09:49:33 +02005534 struct btrfs_root *root = BTRFS_I(inode)->root;
5535
5536 if (btrfs_root_readonly(root))
5537 return -EROFS;
5538
Josef Bacike41f9412012-03-26 09:46:47 -04005539 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005540 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04005541 if (flags & S_CTIME)
5542 inode->i_ctime = *now;
5543 if (flags & S_MTIME)
5544 inode->i_mtime = *now;
5545 if (flags & S_ATIME)
5546 inode->i_atime = *now;
5547 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005548}
5549
Chris Masond352ac62008-09-29 15:18:18 -04005550/*
5551 * find the highest existing sequence number in a directory
5552 * and then set the in-memory index_cnt variable to reflect
5553 * free sequence numbers
5554 */
Josef Bacikaec74772008-07-24 12:12:38 -04005555static int btrfs_set_inode_index_count(struct inode *inode)
5556{
5557 struct btrfs_root *root = BTRFS_I(inode)->root;
5558 struct btrfs_key key, found_key;
5559 struct btrfs_path *path;
5560 struct extent_buffer *leaf;
5561 int ret;
5562
Li Zefan33345d012011-04-20 10:31:50 +08005563 key.objectid = btrfs_ino(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005564 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
5565 key.offset = (u64)-1;
5566
5567 path = btrfs_alloc_path();
5568 if (!path)
5569 return -ENOMEM;
5570
5571 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5572 if (ret < 0)
5573 goto out;
5574 /* FIXME: we should be able to handle this */
5575 if (ret == 0)
5576 goto out;
5577 ret = 0;
5578
5579 /*
5580 * MAGIC NUMBER EXPLANATION:
5581 * since we search a directory based on f_pos we have to start at 2
5582 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5583 * else has to start at 2
5584 */
5585 if (path->slots[0] == 0) {
5586 BTRFS_I(inode)->index_cnt = 2;
5587 goto out;
5588 }
5589
5590 path->slots[0]--;
5591
5592 leaf = path->nodes[0];
5593 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5594
Li Zefan33345d012011-04-20 10:31:50 +08005595 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacikaec74772008-07-24 12:12:38 -04005596 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
5597 BTRFS_I(inode)->index_cnt = 2;
5598 goto out;
5599 }
5600
5601 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
5602out:
5603 btrfs_free_path(path);
5604 return ret;
5605}
5606
Chris Masond352ac62008-09-29 15:18:18 -04005607/*
5608 * helper to find a free sequence number in a given directory. This current
5609 * code is very simple, later versions will do smarter things in the btree
5610 */
Chris Mason3de45862008-11-17 21:02:50 -05005611int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04005612{
5613 int ret = 0;
5614
5615 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08005616 ret = btrfs_inode_delayed_dir_index_count(dir);
5617 if (ret) {
5618 ret = btrfs_set_inode_index_count(dir);
5619 if (ret)
5620 return ret;
5621 }
Josef Bacikaec74772008-07-24 12:12:38 -04005622 }
5623
Chris Mason00e4e6b2008-08-05 11:18:09 -04005624 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04005625 BTRFS_I(dir)->index_cnt++;
5626
5627 return ret;
5628}
5629
Chris Mason39279cc2007-06-12 06:35:45 -04005630static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5631 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04005632 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05005633 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04005634 u64 ref_objectid, u64 objectid,
5635 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04005636{
5637 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005638 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04005639 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04005640 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05005641 struct btrfs_inode_ref *ref;
5642 struct btrfs_key key[2];
5643 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005644 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05005645 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04005646 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005647
Chris Mason5f39d392007-10-15 16:14:19 -04005648 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005649 if (!path)
5650 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04005651
Chris Mason39279cc2007-06-12 06:35:45 -04005652 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005653 if (!inode) {
5654 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005655 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005656 }
Chris Mason39279cc2007-06-12 06:35:45 -04005657
Li Zefan581bb052011-04-20 10:06:11 +08005658 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01005659 * O_TMPFILE, set link count to 0, so that after this point,
5660 * we fill in an inode item with the correct link count.
5661 */
5662 if (!name)
5663 set_nlink(inode, 0);
5664
5665 /*
Li Zefan581bb052011-04-20 10:06:11 +08005666 * we have to initialize this early, so we can reclaim the inode
5667 * number if we fail afterwards in this function.
5668 */
5669 inode->i_ino = objectid;
5670
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005671 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00005672 trace_btrfs_inode_request(dir);
5673
Chris Mason3de45862008-11-17 21:02:50 -05005674 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04005675 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005676 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005677 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005678 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04005679 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005680 } else if (dir) {
5681 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04005682 }
5683 /*
5684 * index_cnt is ignored for everything but a dir,
5685 * btrfs_get_inode_index_count has an explanation for the magic
5686 * number
5687 */
5688 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08005689 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04005690 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04005691 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00005692 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04005693
Josef Bacik5dc562c2012-08-17 13:14:17 -04005694 /*
5695 * We could have gotten an inode number from somebody who was fsynced
5696 * and then removed in this same transaction, so let's just set full
5697 * sync since it will be a full sync anyway and this will blow away the
5698 * old info in the log.
5699 */
5700 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
5701
Chris Mason9c583092008-01-29 15:15:18 -05005702 key[0].objectid = objectid;
5703 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
5704 key[0].offset = 0;
5705
Chris Mason9c583092008-01-29 15:15:18 -05005706 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005707
5708 if (name) {
5709 /*
5710 * Start new inodes with an inode_ref. This is slightly more
5711 * efficient for small numbers of hard links since they will
5712 * be packed into one item. Extended refs will kick in if we
5713 * add more hard links than can fit in the ref item.
5714 */
5715 key[1].objectid = objectid;
5716 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
5717 key[1].offset = ref_objectid;
5718
5719 sizes[1] = name_len + sizeof(*ref);
5720 }
Chris Mason9c583092008-01-29 15:15:18 -05005721
Chris Masonb9473432009-03-13 11:00:37 -04005722 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005723 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05005724 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04005725 goto fail;
5726
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03005727 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04005728 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005729 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04005730 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
5731 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06005732 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
5733 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04005734 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05005735
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005736 if (name) {
5737 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
5738 struct btrfs_inode_ref);
5739 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
5740 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
5741 ptr = (unsigned long)(ref + 1);
5742 write_extent_buffer(path->nodes[0], name, ptr, name_len);
5743 }
Chris Mason9c583092008-01-29 15:15:18 -05005744
Chris Mason5f39d392007-10-15 16:14:19 -04005745 btrfs_mark_buffer_dirty(path->nodes[0]);
5746 btrfs_free_path(path);
5747
Chris Mason39279cc2007-06-12 06:35:45 -04005748 location = &BTRFS_I(inode)->location;
5749 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04005750 location->offset = 0;
5751 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
5752
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005753 btrfs_inherit_iflags(inode, dir);
5754
Al Viro569254b2011-07-24 17:08:40 -04005755 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04005756 if (btrfs_test_opt(root, NODATASUM))
5757 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo213490b2012-09-11 08:33:50 -06005758 if (btrfs_test_opt(root, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05005759 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
5760 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04005761 }
5762
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005763 btrfs_insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005764 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00005765
5766 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04005767 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00005768
Alexander Block8ea05e32012-07-25 17:35:53 +02005769 btrfs_update_root_times(trans, root);
5770
Filipe David Borba Manana63541922014-01-07 11:47:46 +00005771 ret = btrfs_inode_inherit_props(trans, inode, dir);
5772 if (ret)
5773 btrfs_err(root->fs_info,
5774 "error inheriting props for ino %llu (root %llu): %d",
5775 btrfs_ino(inode), root->root_key.objectid, ret);
5776
Chris Mason39279cc2007-06-12 06:35:45 -04005777 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005778fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005779 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04005780 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04005781 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005782 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005783 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005784}
5785
5786static inline u8 btrfs_inode_type(struct inode *inode)
5787{
5788 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
5789}
5790
Chris Masond352ac62008-09-29 15:18:18 -04005791/*
5792 * utility function to add 'inode' into 'parent_inode' with
5793 * a give name and a given sequence number.
5794 * if 'add_backref' is true, also insert a backref from the
5795 * inode to the parent directory.
5796 */
Chris Masone02119d2008-09-05 16:13:11 -04005797int btrfs_add_link(struct btrfs_trans_handle *trans,
5798 struct inode *parent_inode, struct inode *inode,
5799 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005800{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005801 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005802 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04005803 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08005804 u64 ino = btrfs_ino(inode);
5805 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005806
Li Zefan33345d012011-04-20 10:31:50 +08005807 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005808 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
5809 } else {
Li Zefan33345d012011-04-20 10:31:50 +08005810 key.objectid = ino;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005811 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
5812 key.offset = 0;
5813 }
Chris Mason39279cc2007-06-12 06:35:45 -04005814
Li Zefan33345d012011-04-20 10:31:50 +08005815 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005816 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
5817 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08005818 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005819 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08005820 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
5821 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005822 }
5823
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005824 /* Nothing to clean up yet */
5825 if (ret)
5826 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005827
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005828 ret = btrfs_insert_dir_item(trans, root, name, name_len,
5829 parent_inode, &key,
5830 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05005831 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005832 goto fail_dir_item;
5833 else if (ret) {
5834 btrfs_abort_transaction(trans, root, ret);
5835 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005836 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005837
5838 btrfs_i_size_write(parent_inode, parent_inode->i_size +
5839 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005840 inode_inc_iversion(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005841 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
5842 ret = btrfs_update_inode(trans, root, parent_inode);
5843 if (ret)
5844 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005845 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05005846
5847fail_dir_item:
5848 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
5849 u64 local_index;
5850 int err;
5851 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
5852 key.objectid, root->root_key.objectid,
5853 parent_ino, &local_index, name, name_len);
5854
5855 } else if (add_backref) {
5856 u64 local_index;
5857 int err;
5858
5859 err = btrfs_del_inode_ref(trans, root, name, name_len,
5860 ino, parent_ino, &local_index);
5861 }
5862 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005863}
5864
5865static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00005866 struct inode *dir, struct dentry *dentry,
5867 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005868{
Josef Bacika1b075d2010-11-19 20:36:11 +00005869 int err = btrfs_add_link(trans, dir, inode,
5870 dentry->d_name.name, dentry->d_name.len,
5871 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005872 if (err > 0)
5873 err = -EEXIST;
5874 return err;
5875}
5876
Josef Bacik618e21d2007-07-11 10:18:17 -04005877static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04005878 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04005879{
5880 struct btrfs_trans_handle *trans;
5881 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005882 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04005883 int err;
5884 int drop_inode = 0;
5885 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005886 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04005887
5888 if (!new_valid_dev(rdev))
5889 return -EINVAL;
5890
Josef Bacik9ed74f22009-09-11 16:12:44 -04005891 /*
5892 * 2 for inode item and ref
5893 * 2 for dir items
5894 * 1 for xattr if selinux is on
5895 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005896 trans = btrfs_start_transaction(root, 5);
5897 if (IS_ERR(trans))
5898 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05005899
Li Zefan581bb052011-04-20 10:06:11 +08005900 err = btrfs_find_free_ino(root, &objectid);
5901 if (err)
5902 goto out_unlock;
5903
Josef Bacikaec74772008-07-24 12:12:38 -04005904 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005905 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04005906 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005907 if (IS_ERR(inode)) {
5908 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005909 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005910 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005911
Eric Paris2a7dba32011-02-01 11:05:39 -05005912 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005913 if (err) {
5914 drop_inode = 1;
5915 goto out_unlock;
5916 }
5917
Casey Schauflerad19db72011-12-15 10:09:07 -05005918 /*
5919 * If the active LSM wants to access the inode during
5920 * d_instantiate it needs these. Smack checks to see
5921 * if the filesystem supports xattrs by looking at the
5922 * ops vector.
5923 */
5924
5925 inode->i_op = &btrfs_special_inode_operations;
Josef Bacika1b075d2010-11-19 20:36:11 +00005926 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04005927 if (err)
5928 drop_inode = 1;
5929 else {
Josef Bacik618e21d2007-07-11 10:18:17 -04005930 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04005931 btrfs_update_inode(trans, root, inode);
Al Viro08c422c2011-12-23 07:58:13 -05005932 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005933 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005934out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005935 btrfs_end_transaction(trans, root);
Josef Bacikc581afc2014-02-06 16:06:06 -05005936 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00005937 btrfs_btree_balance_dirty(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04005938 if (drop_inode) {
5939 inode_dec_link_count(inode);
5940 iput(inode);
5941 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005942 return err;
5943}
5944
Chris Mason39279cc2007-06-12 06:35:45 -04005945static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04005946 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04005947{
5948 struct btrfs_trans_handle *trans;
5949 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005950 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005951 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04005952 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04005953 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005954 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005955
Josef Bacik9ed74f22009-09-11 16:12:44 -04005956 /*
5957 * 2 for inode item and ref
5958 * 2 for dir items
5959 * 1 for xattr if selinux is on
5960 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005961 trans = btrfs_start_transaction(root, 5);
5962 if (IS_ERR(trans))
5963 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005964
Li Zefan581bb052011-04-20 10:06:11 +08005965 err = btrfs_find_free_ino(root, &objectid);
5966 if (err)
5967 goto out_unlock;
5968
Josef Bacikaec74772008-07-24 12:12:38 -04005969 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005970 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04005971 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005972 if (IS_ERR(inode)) {
5973 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005974 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005975 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005976 drop_inode_on_err = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04005977
Eric Paris2a7dba32011-02-01 11:05:39 -05005978 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005979 if (err)
Josef Bacik33268ea2008-07-24 12:16:36 -04005980 goto out_unlock;
Josef Bacik33268ea2008-07-24 12:16:36 -04005981
Filipe Brandenburger9185aa52012-11-30 03:40:08 +00005982 err = btrfs_update_inode(trans, root, inode);
5983 if (err)
5984 goto out_unlock;
5985
Casey Schauflerad19db72011-12-15 10:09:07 -05005986 /*
5987 * If the active LSM wants to access the inode during
5988 * d_instantiate it needs these. Smack checks to see
5989 * if the filesystem supports xattrs by looking at the
5990 * ops vector.
5991 */
5992 inode->i_fop = &btrfs_file_operations;
5993 inode->i_op = &btrfs_file_inode_operations;
5994
Josef Bacika1b075d2010-11-19 20:36:11 +00005995 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005996 if (err)
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005997 goto out_unlock;
5998
5999 inode->i_mapping->a_ops = &btrfs_aops;
6000 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
6001 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
6002 d_instantiate(dentry, inode);
6003
Chris Mason39279cc2007-06-12 06:35:45 -04006004out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006005 btrfs_end_transaction(trans, root);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006006 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006007 inode_dec_link_count(inode);
6008 iput(inode);
6009 }
Josef Bacikc581afc2014-02-06 16:06:06 -05006010 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006011 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006012 return err;
6013}
6014
6015static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6016 struct dentry *dentry)
6017{
6018 struct btrfs_trans_handle *trans;
6019 struct btrfs_root *root = BTRFS_I(dir)->root;
6020 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006021 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006022 int err;
6023 int drop_inode = 0;
6024
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006025 /* do not allow sys_link's with other subvols of the same device */
6026 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006027 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006028
Mark Fashehf1863732012-08-08 11:32:27 -07006029 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006030 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006031
Chris Mason3de45862008-11-17 21:02:50 -05006032 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006033 if (err)
6034 goto fail;
6035
Yan, Zhenga22285a2010-05-16 10:48:46 -04006036 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006037 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006038 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006039 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04006040 */
Miao Xie7e6b6462011-02-18 09:21:17 +00006041 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006042 if (IS_ERR(trans)) {
6043 err = PTR_ERR(trans);
6044 goto fail;
6045 }
Chris Mason5f39d392007-10-15 16:14:19 -04006046
Miao Xie67de1172013-12-26 13:07:06 +08006047 /* There are several dir indexes for this inode, clear the cache. */
6048 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006049 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006050 inode_inc_iversion(inode);
Miao Xie31534952011-04-13 13:19:21 +08006051 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6ee2010-10-23 11:11:40 -04006052 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006053 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006054
Josef Bacika1b075d2010-11-19 20:36:11 +00006055 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006056
Yan, Zhenga5719522009-09-24 09:17:31 -04006057 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006058 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006059 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006060 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006061 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006062 if (err)
6063 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006064 if (inode->i_nlink == 1) {
6065 /*
6066 * If new hard link count is 1, it's a file created
6067 * with open(2) O_TMPFILE flag.
6068 */
6069 err = btrfs_orphan_del(trans, inode);
6070 if (err)
6071 goto fail;
6072 }
Al Viro08c422c2011-12-23 07:58:13 -05006073 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00006074 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006075 }
Chris Mason39279cc2007-06-12 06:35:45 -04006076
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006077 btrfs_end_transaction(trans, root);
Josef Bacikc581afc2014-02-06 16:06:06 -05006078 btrfs_balance_delayed_items(root);
Chris Mason1832a6d2007-12-21 16:27:21 -05006079fail:
Chris Mason39279cc2007-06-12 06:35:45 -04006080 if (drop_inode) {
6081 inode_dec_link_count(inode);
6082 iput(inode);
6083 }
Liu Bob53d3f52012-11-14 14:34:34 +00006084 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006085 return err;
6086}
6087
Al Viro18bb1db2011-07-26 01:41:39 -04006088static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006089{
Chris Masonb9d86662008-05-02 16:13:49 -04006090 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006091 struct btrfs_trans_handle *trans;
6092 struct btrfs_root *root = BTRFS_I(dir)->root;
6093 int err = 0;
6094 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006095 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006096 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006097
Josef Bacik9ed74f22009-09-11 16:12:44 -04006098 /*
6099 * 2 items for inode and ref
6100 * 2 items for dir items
6101 * 1 for xattr if selinux is on
6102 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006103 trans = btrfs_start_transaction(root, 5);
6104 if (IS_ERR(trans))
6105 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006106
Li Zefan581bb052011-04-20 10:06:11 +08006107 err = btrfs_find_free_ino(root, &objectid);
6108 if (err)
6109 goto out_fail;
6110
Josef Bacikaec74772008-07-24 12:12:38 -04006111 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006112 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04006113 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006114 if (IS_ERR(inode)) {
6115 err = PTR_ERR(inode);
6116 goto out_fail;
6117 }
Chris Mason5f39d392007-10-15 16:14:19 -04006118
Chris Mason39279cc2007-06-12 06:35:45 -04006119 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04006120
Eric Paris2a7dba32011-02-01 11:05:39 -05006121 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006122 if (err)
6123 goto out_fail;
6124
Chris Mason39279cc2007-06-12 06:35:45 -04006125 inode->i_op = &btrfs_dir_inode_operations;
6126 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04006127
Chris Masondbe674a2008-07-17 12:54:05 -04006128 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006129 err = btrfs_update_inode(trans, root, inode);
6130 if (err)
6131 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006132
Josef Bacika1b075d2010-11-19 20:36:11 +00006133 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
6134 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006135 if (err)
6136 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006137
Chris Mason39279cc2007-06-12 06:35:45 -04006138 d_instantiate(dentry, inode);
6139 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006140
6141out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006142 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04006143 if (drop_on_err)
6144 iput(inode);
Josef Bacikc581afc2014-02-06 16:06:06 -05006145 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006146 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006147 return err;
6148}
6149
Chris Masond352ac62008-09-29 15:18:18 -04006150/* helper for btfs_get_extent. Given an existing extent in the tree,
6151 * and an extent that you want to insert, deal with overlap and insert
6152 * the new extent into the tree.
6153 */
Chris Mason3b951512008-04-17 11:29:12 -04006154static int merge_extent_mapping(struct extent_map_tree *em_tree,
6155 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006156 struct extent_map *em,
Qu Wenruo51f395a2014-08-08 13:06:20 +08006157 u64 map_start)
Chris Mason3b951512008-04-17 11:29:12 -04006158{
6159 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04006160
Chris Masone6dcd2d2008-07-17 12:53:50 -04006161 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
6162 start_diff = map_start - em->start;
6163 em->start = map_start;
Qu Wenruo51f395a2014-08-08 13:06:20 +08006164 em->len = existing->start - em->start;
Chris Masonc8b97812008-10-29 14:49:59 -04006165 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
6166 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006167 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04006168 em->block_len -= start_diff;
6169 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006170 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006171}
6172
Chris Masonc8b97812008-10-29 14:49:59 -04006173static noinline int uncompress_inline(struct btrfs_path *path,
6174 struct inode *inode, struct page *page,
6175 size_t pg_offset, u64 extent_offset,
6176 struct btrfs_file_extent_item *item)
6177{
6178 int ret;
6179 struct extent_buffer *leaf = path->nodes[0];
6180 char *tmp;
6181 size_t max_size;
6182 unsigned long inline_size;
6183 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006184 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006185
6186 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006187 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006188 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6189 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006190 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006191 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006192 if (!tmp)
6193 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006194 ptr = btrfs_file_extent_inline_start(item);
6195
6196 read_extent_buffer(leaf, tmp, ptr, inline_size);
6197
Chris Mason5b050f02008-11-11 09:34:41 -05006198 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006199 ret = btrfs_decompress(compress_type, tmp, page,
6200 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006201 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006202 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006203}
6204
Chris Masond352ac62008-09-29 15:18:18 -04006205/*
6206 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006207 * the ugly parts come from merging extents from the disk with the in-ram
6208 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006209 * where the in-ram extents might be locked pending data=ordered completion.
6210 *
6211 * This also copies inline extents directly into the page.
6212 */
Chris Masond3977122009-01-05 21:25:51 -05006213
Chris Masona52d9a82007-08-27 16:49:44 -04006214struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05006215 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04006216 int create)
6217{
6218 int ret;
6219 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006220 u64 extent_start = 0;
6221 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08006222 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006223 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006224 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04006225 struct btrfs_root *root = BTRFS_I(inode)->root;
6226 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006227 struct extent_buffer *leaf;
6228 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006229 struct extent_map *em = NULL;
6230 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05006231 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006232 struct btrfs_trans_handle *trans = NULL;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006233 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006234
Chris Masona52d9a82007-08-27 16:49:44 -04006235again:
Chris Mason890871b2009-09-02 16:24:52 -04006236 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006237 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006238 if (em)
6239 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006240 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006241
Chris Masona52d9a82007-08-27 16:49:44 -04006242 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006243 if (em->start > start || em->start + em->len <= start)
6244 free_extent_map(em);
6245 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006246 free_extent_map(em);
6247 else
6248 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006249 }
David Sterba172ddd62011-04-21 00:48:27 +02006250 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006251 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006252 err = -ENOMEM;
6253 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006254 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006255 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006256 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006257 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006258 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006259 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006260
6261 if (!path) {
6262 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006263 if (!path) {
6264 err = -ENOMEM;
6265 goto out;
6266 }
6267 /*
6268 * Chances are we'll be called again, so go ahead and do
6269 * readahead
6270 */
6271 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04006272 }
6273
Chris Mason179e29e2007-11-01 11:28:41 -04006274 ret = btrfs_lookup_file_extent(trans, root, path,
6275 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006276 if (ret < 0) {
6277 err = ret;
6278 goto out;
6279 }
6280
6281 if (ret != 0) {
6282 if (path->slots[0] == 0)
6283 goto not_found;
6284 path->slots[0]--;
6285 }
6286
Chris Mason5f39d392007-10-15 16:14:19 -04006287 leaf = path->nodes[0];
6288 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006289 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006290 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006291 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6292 found_type = btrfs_key_type(&found_key);
6293 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006294 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006295 /*
6296 * If we backup past the first extent we want to move forward
6297 * and see if there is an extent in front of us, otherwise we'll
6298 * say there is a hole for our whole search range which can
6299 * cause problems.
6300 */
6301 extent_end = start;
6302 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006303 }
6304
Chris Mason5f39d392007-10-15 16:14:19 -04006305 found_type = btrfs_file_extent_type(leaf, item);
6306 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04006307 if (found_type == BTRFS_FILE_EXTENT_REG ||
6308 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006309 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006310 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006311 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6312 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08006313 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Qu Wenruofda28322013-02-26 08:10:22 +00006314 extent_end = ALIGN(extent_start + size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04006315 }
Josef Bacik25a50342013-10-14 12:08:38 -04006316next:
Yan Zheng9036c102008-10-30 14:19:41 -04006317 if (start >= extent_end) {
6318 path->slots[0]++;
6319 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6320 ret = btrfs_next_leaf(root, path);
6321 if (ret < 0) {
6322 err = ret;
6323 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006324 }
Yan Zheng9036c102008-10-30 14:19:41 -04006325 if (ret > 0)
6326 goto not_found;
6327 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006328 }
Yan Zheng9036c102008-10-30 14:19:41 -04006329 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6330 if (found_key.objectid != objectid ||
6331 found_key.type != BTRFS_EXTENT_DATA_KEY)
6332 goto not_found;
6333 if (start + len <= found_key.offset)
6334 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006335 if (start > found_key.offset)
6336 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04006337 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006338 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006339 em->len = found_key.offset - start;
6340 goto not_found_em;
6341 }
6342
Filipe Manana7ffbb592014-06-09 03:48:05 +01006343 btrfs_extent_item_to_extent_map(inode, path, item, new_inline, em);
6344
Yan Zhengd899e052008-10-30 14:25:28 -04006345 if (found_type == BTRFS_FILE_EXTENT_REG ||
6346 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006347 goto insert;
6348 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006349 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006350 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006351 size_t size;
6352 size_t extent_offset;
6353 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006354
Filipe Manana7ffbb592014-06-09 03:48:05 +01006355 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04006356 goto out;
Yan689f9342007-10-29 11:41:07 -04006357
Chris Mason514ac8a2014-01-03 21:07:00 -08006358 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04006359 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05006360 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04006361 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006362 em->start = extent_start + extent_offset;
Qu Wenruofda28322013-02-26 08:10:22 +00006363 em->len = ALIGN(copy_size, root->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006364 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006365 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006366 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006367 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006368 if (btrfs_file_extent_compression(leaf, item) !=
6369 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006370 ret = uncompress_inline(path, inode, page,
6371 pg_offset,
6372 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04006373 if (ret) {
6374 err = ret;
6375 goto out;
6376 }
Chris Masonc8b97812008-10-29 14:49:59 -04006377 } else {
6378 map = kmap(page);
6379 read_extent_buffer(leaf, map + pg_offset, ptr,
6380 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04006381 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
6382 memset(map + pg_offset + copy_size, 0,
6383 PAGE_CACHE_SIZE - pg_offset -
6384 copy_size);
6385 }
Chris Masonc8b97812008-10-29 14:49:59 -04006386 kunmap(page);
6387 }
Chris Mason179e29e2007-11-01 11:28:41 -04006388 flush_dcache_page(page);
6389 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006390 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006391 if (!trans) {
6392 kunmap(page);
6393 free_extent_map(em);
6394 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006395
David Sterbab3b4aa72011-04-21 01:20:15 +02006396 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006397 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006398
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006399 if (IS_ERR(trans))
6400 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006401 goto again;
6402 }
Chris Masonc8b97812008-10-29 14:49:59 -04006403 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006404 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006405 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006406 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006407 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006408 }
Chris Masond1310b22008-01-24 16:13:08 -05006409 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006410 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006411 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04006412 }
6413not_found:
6414 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006415 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006416 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006417not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04006418 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04006419 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006420insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006421 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006422 if (em->start > start || extent_map_end(em) <= start) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006423 btrfs_err(root->fs_info, "bad extent! em: [%llu %llu] passed [%llu %llu]",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006424 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04006425 err = -EIO;
6426 goto out;
6427 }
Chris Masond1310b22008-01-24 16:13:08 -05006428
6429 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006430 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006431 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006432 /* it is possible that someone inserted the extent into the tree
6433 * while we had the lock dropped. It is also possible that
6434 * an overlapping map exists in the tree
6435 */
Chris Masona52d9a82007-08-27 16:49:44 -04006436 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04006437 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006438
6439 ret = 0;
6440
Chris Mason3b951512008-04-17 11:29:12 -04006441 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04006442 if (existing && (existing->start > start ||
6443 existing->start + existing->len <= start)) {
6444 free_extent_map(existing);
6445 existing = NULL;
6446 }
Chris Mason3b951512008-04-17 11:29:12 -04006447 if (!existing) {
6448 existing = lookup_extent_mapping(em_tree, em->start,
6449 em->len);
6450 if (existing) {
6451 err = merge_extent_mapping(em_tree, existing,
Qu Wenruo51f395a2014-08-08 13:06:20 +08006452 em, start);
Chris Mason3b951512008-04-17 11:29:12 -04006453 free_extent_map(existing);
6454 if (err) {
6455 free_extent_map(em);
6456 em = NULL;
6457 }
6458 } else {
6459 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04006460 free_extent_map(em);
6461 em = NULL;
6462 }
6463 } else {
6464 free_extent_map(em);
6465 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006466 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006467 }
Chris Masona52d9a82007-08-27 16:49:44 -04006468 }
Chris Mason890871b2009-09-02 16:24:52 -04006469 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006470out:
liubo1abe9b82011-03-24 11:18:59 +00006471
Steven Rostedt4cd85872013-11-14 22:57:29 -05006472 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00006473
Chris Masonf4219502008-07-22 11:18:09 -04006474 if (path)
6475 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04006476 if (trans) {
6477 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05006478 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04006479 err = ret;
6480 }
Chris Masona52d9a82007-08-27 16:49:44 -04006481 if (err) {
6482 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04006483 return ERR_PTR(err);
6484 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006485 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04006486 return em;
6487}
6488
Chris Masonec29ed52011-02-23 16:23:20 -05006489struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
6490 size_t pg_offset, u64 start, u64 len,
6491 int create)
6492{
6493 struct extent_map *em;
6494 struct extent_map *hole_em = NULL;
6495 u64 range_start = start;
6496 u64 end;
6497 u64 found;
6498 u64 found_end;
6499 int err = 0;
6500
6501 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
6502 if (IS_ERR(em))
6503 return em;
6504 if (em) {
6505 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00006506 * if our em maps to
6507 * - a hole or
6508 * - a pre-alloc extent,
6509 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05006510 */
Liu Bof9e4fb52013-01-07 10:10:12 +00006511 if (em->block_start != EXTENT_MAP_HOLE &&
6512 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05006513 return em;
6514 else
6515 hole_em = em;
6516 }
6517
6518 /* check to see if we've wrapped (len == -1 or similar) */
6519 end = start + len;
6520 if (end < start)
6521 end = (u64)-1;
6522 else
6523 end -= 1;
6524
6525 em = NULL;
6526
6527 /* ok, we didn't find anything, lets look for delalloc */
6528 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
6529 end, len, EXTENT_DELALLOC, 1);
6530 found_end = range_start + found;
6531 if (found_end < range_start)
6532 found_end = (u64)-1;
6533
6534 /*
6535 * we didn't find anything useful, return
6536 * the original results from get_extent()
6537 */
6538 if (range_start > end || found_end <= start) {
6539 em = hole_em;
6540 hole_em = NULL;
6541 goto out;
6542 }
6543
6544 /* adjust the range_start to make sure it doesn't
6545 * go backwards from the start they passed in
6546 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05306547 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05006548 found = found_end - range_start;
6549
6550 if (found > 0) {
6551 u64 hole_start = start;
6552 u64 hole_len = len;
6553
David Sterba172ddd62011-04-21 00:48:27 +02006554 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05006555 if (!em) {
6556 err = -ENOMEM;
6557 goto out;
6558 }
6559 /*
6560 * when btrfs_get_extent can't find anything it
6561 * returns one huge hole
6562 *
6563 * make sure what it found really fits our range, and
6564 * adjust to make sure it is based on the start from
6565 * the caller
6566 */
6567 if (hole_em) {
6568 u64 calc_end = extent_map_end(hole_em);
6569
6570 if (calc_end <= start || (hole_em->start > end)) {
6571 free_extent_map(hole_em);
6572 hole_em = NULL;
6573 } else {
6574 hole_start = max(hole_em->start, start);
6575 hole_len = calc_end - hole_start;
6576 }
6577 }
6578 em->bdev = NULL;
6579 if (hole_em && range_start > hole_start) {
6580 /* our hole starts before our delalloc, so we
6581 * have to return just the parts of the hole
6582 * that go until the delalloc starts
6583 */
6584 em->len = min(hole_len,
6585 range_start - hole_start);
6586 em->start = hole_start;
6587 em->orig_start = hole_start;
6588 /*
6589 * don't adjust block start at all,
6590 * it is fixed at EXTENT_MAP_HOLE
6591 */
6592 em->block_start = hole_em->block_start;
6593 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00006594 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
6595 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05006596 } else {
6597 em->start = range_start;
6598 em->len = found;
6599 em->orig_start = range_start;
6600 em->block_start = EXTENT_MAP_DELALLOC;
6601 em->block_len = found;
6602 }
6603 } else if (hole_em) {
6604 return hole_em;
6605 }
6606out:
6607
6608 free_extent_map(hole_em);
6609 if (err) {
6610 free_extent_map(em);
6611 return ERR_PTR(err);
6612 }
6613 return em;
6614}
6615
Josef Bacik4b46fce2010-05-23 11:00:55 -04006616static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
6617 u64 start, u64 len)
6618{
6619 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04006620 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006621 struct btrfs_key ins;
6622 u64 alloc_hint;
6623 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006624
Josef Bacik4b46fce2010-05-23 11:00:55 -04006625 alloc_hint = get_extent_allocation_hint(inode, start, len);
Josef Bacik00361582013-08-14 14:02:47 -04006626 ret = btrfs_reserve_extent(root, len, root->sectorsize, 0,
Miao Xiee570fd22014-06-19 10:42:50 +08006627 alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006628 if (ret)
6629 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006630
Josef Bacik70c8a912012-10-11 16:54:30 -04006631 em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006632 ins.offset, ins.offset, ins.offset, 0);
Josef Bacik00361582013-08-14 14:02:47 -04006633 if (IS_ERR(em)) {
Miao Xiee570fd22014-06-19 10:42:50 +08006634 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006635 return em;
6636 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006637
6638 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
6639 ins.offset, ins.offset, 0);
6640 if (ret) {
Miao Xiee570fd22014-06-19 10:42:50 +08006641 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006642 free_extent_map(em);
6643 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006644 }
Josef Bacik00361582013-08-14 14:02:47 -04006645
Josef Bacik4b46fce2010-05-23 11:00:55 -04006646 return em;
6647}
6648
Chris Mason46bfbb52010-05-26 11:04:10 -04006649/*
6650 * returns 1 when the nocow is safe, < 1 on error, 0 if the
6651 * block must be cow'd
6652 */
Josef Bacik00361582013-08-14 14:02:47 -04006653noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04006654 u64 *orig_start, u64 *orig_block_len,
6655 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04006656{
Josef Bacik00361582013-08-14 14:02:47 -04006657 struct btrfs_trans_handle *trans;
Chris Mason46bfbb52010-05-26 11:04:10 -04006658 struct btrfs_path *path;
6659 int ret;
6660 struct extent_buffer *leaf;
6661 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08006662 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04006663 struct btrfs_file_extent_item *fi;
6664 struct btrfs_key key;
6665 u64 disk_bytenr;
6666 u64 backref_offset;
6667 u64 extent_end;
6668 u64 num_bytes;
6669 int slot;
6670 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04006671 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08006672
Chris Mason46bfbb52010-05-26 11:04:10 -04006673 path = btrfs_alloc_path();
6674 if (!path)
6675 return -ENOMEM;
6676
Josef Bacik00361582013-08-14 14:02:47 -04006677 ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04006678 offset, 0);
6679 if (ret < 0)
6680 goto out;
6681
6682 slot = path->slots[0];
6683 if (ret == 1) {
6684 if (slot == 0) {
6685 /* can't find the item, must cow */
6686 ret = 0;
6687 goto out;
6688 }
6689 slot--;
6690 }
6691 ret = 0;
6692 leaf = path->nodes[0];
6693 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08006694 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04006695 key.type != BTRFS_EXTENT_DATA_KEY) {
6696 /* not our file or wrong item type, must cow */
6697 goto out;
6698 }
6699
6700 if (key.offset > offset) {
6701 /* Wrong offset, must cow */
6702 goto out;
6703 }
6704
6705 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
6706 found_type = btrfs_file_extent_type(leaf, fi);
6707 if (found_type != BTRFS_FILE_EXTENT_REG &&
6708 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
6709 /* not a regular extent, must cow */
6710 goto out;
6711 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04006712
6713 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
6714 goto out;
6715
Miao Xiee77751a2013-12-27 21:11:50 +08006716 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
6717 if (extent_end <= offset)
6718 goto out;
6719
Chris Mason46bfbb52010-05-26 11:04:10 -04006720 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04006721 if (disk_bytenr == 0)
6722 goto out;
6723
6724 if (btrfs_file_extent_compression(leaf, fi) ||
6725 btrfs_file_extent_encryption(leaf, fi) ||
6726 btrfs_file_extent_other_encoding(leaf, fi))
6727 goto out;
6728
Chris Mason46bfbb52010-05-26 11:04:10 -04006729 backref_offset = btrfs_file_extent_offset(leaf, fi);
6730
Josef Bacik7ee9e442013-06-21 16:37:03 -04006731 if (orig_start) {
6732 *orig_start = key.offset - backref_offset;
6733 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
6734 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
6735 }
Josef Bacikeb384b52013-04-24 16:32:55 -04006736
Chris Mason46bfbb52010-05-26 11:04:10 -04006737 if (btrfs_extent_readonly(root, disk_bytenr))
6738 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08006739
6740 num_bytes = min(offset + *len, extent_end) - offset;
6741 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
6742 u64 range_end;
6743
6744 range_end = round_up(offset + num_bytes, root->sectorsize) - 1;
6745 ret = test_range_bit(io_tree, offset, range_end,
6746 EXTENT_DELALLOC, 0, NULL);
6747 if (ret) {
6748 ret = -EAGAIN;
6749 goto out;
6750 }
6751 }
6752
Josef Bacik1bda19e2013-10-18 12:10:36 -04006753 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04006754
6755 /*
6756 * look for other files referencing this extent, if we
6757 * find any we must cow
6758 */
Josef Bacik00361582013-08-14 14:02:47 -04006759 trans = btrfs_join_transaction(root);
6760 if (IS_ERR(trans)) {
6761 ret = 0;
Chris Mason46bfbb52010-05-26 11:04:10 -04006762 goto out;
Josef Bacik00361582013-08-14 14:02:47 -04006763 }
6764
6765 ret = btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
6766 key.offset - backref_offset, disk_bytenr);
6767 btrfs_end_transaction(trans, root);
6768 if (ret) {
6769 ret = 0;
6770 goto out;
6771 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006772
6773 /*
6774 * adjust disk_bytenr and num_bytes to cover just the bytes
6775 * in this extent we are about to write. If there
6776 * are any csums in that range we have to cow in order
6777 * to keep the csums correct
6778 */
6779 disk_bytenr += backref_offset;
6780 disk_bytenr += offset - key.offset;
Chris Mason46bfbb52010-05-26 11:04:10 -04006781 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
6782 goto out;
6783 /*
6784 * all of the above have passed, it is safe to overwrite this extent
6785 * without cow
6786 */
Josef Bacikeb384b52013-04-24 16:32:55 -04006787 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04006788 ret = 1;
6789out:
6790 btrfs_free_path(path);
6791 return ret;
6792}
6793
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07006794bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
6795{
6796 struct radix_tree_root *root = &inode->i_mapping->page_tree;
6797 int found = false;
6798 void **pagep = NULL;
6799 struct page *page = NULL;
6800 int start_idx;
6801 int end_idx;
6802
6803 start_idx = start >> PAGE_CACHE_SHIFT;
6804
6805 /*
6806 * end is the last byte in the last page. end == start is legal
6807 */
6808 end_idx = end >> PAGE_CACHE_SHIFT;
6809
6810 rcu_read_lock();
6811
6812 /* Most of the code in this while loop is lifted from
6813 * find_get_page. It's been modified to begin searching from a
6814 * page and return just the first page found in that range. If the
6815 * found idx is less than or equal to the end idx then we know that
6816 * a page exists. If no pages are found or if those pages are
6817 * outside of the range then we're fine (yay!) */
6818 while (page == NULL &&
6819 radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
6820 page = radix_tree_deref_slot(pagep);
6821 if (unlikely(!page))
6822 break;
6823
6824 if (radix_tree_exception(page)) {
Filipe Manana809f9012014-06-05 13:22:25 +01006825 if (radix_tree_deref_retry(page)) {
6826 page = NULL;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07006827 continue;
Filipe Manana809f9012014-06-05 13:22:25 +01006828 }
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07006829 /*
6830 * Otherwise, shmem/tmpfs must be storing a swap entry
6831 * here as an exceptional entry: so return it without
6832 * attempting to raise page count.
6833 */
Filipe Manana6fdef6d2014-06-05 13:22:26 +01006834 page = NULL;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07006835 break; /* TODO: Is this relevant for this use case? */
6836 }
6837
Filipe Manana91405152014-06-05 13:22:24 +01006838 if (!page_cache_get_speculative(page)) {
6839 page = NULL;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07006840 continue;
Filipe Manana91405152014-06-05 13:22:24 +01006841 }
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07006842
6843 /*
6844 * Has the page moved?
6845 * This is part of the lockless pagecache protocol. See
6846 * include/linux/pagemap.h for details.
6847 */
6848 if (unlikely(page != *pagep)) {
6849 page_cache_release(page);
6850 page = NULL;
6851 }
6852 }
6853
6854 if (page) {
6855 if (page->index <= end_idx)
6856 found = true;
6857 page_cache_release(page);
6858 }
6859
6860 rcu_read_unlock();
6861 return found;
6862}
6863
Josef Bacikeb838e72012-07-31 16:28:48 -04006864static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
6865 struct extent_state **cached_state, int writing)
6866{
6867 struct btrfs_ordered_extent *ordered;
6868 int ret = 0;
6869
6870 while (1) {
6871 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6872 0, cached_state);
6873 /*
6874 * We're concerned with the entire range that we're going to be
6875 * doing DIO to, so we need to make sure theres no ordered
6876 * extents in this range.
6877 */
6878 ordered = btrfs_lookup_ordered_range(inode, lockstart,
6879 lockend - lockstart + 1);
6880
6881 /*
6882 * We need to make sure there are no buffered pages in this
6883 * range either, we could have raced between the invalidate in
6884 * generic_file_direct_write and locking the extent. The
6885 * invalidate needs to happen so that reads after a write do not
6886 * get stale data.
6887 */
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07006888 if (!ordered &&
6889 (!writing ||
6890 !btrfs_page_exists_in_range(inode, lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04006891 break;
6892
6893 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6894 cached_state, GFP_NOFS);
6895
6896 if (ordered) {
6897 btrfs_start_ordered_extent(inode, ordered, 1);
6898 btrfs_put_ordered_extent(ordered);
6899 } else {
6900 /* Screw you mmap */
6901 ret = filemap_write_and_wait_range(inode->i_mapping,
6902 lockstart,
6903 lockend);
6904 if (ret)
6905 break;
6906
6907 /*
6908 * If we found a page that couldn't be invalidated just
6909 * fall back to buffered.
6910 */
6911 ret = invalidate_inode_pages2_range(inode->i_mapping,
6912 lockstart >> PAGE_CACHE_SHIFT,
6913 lockend >> PAGE_CACHE_SHIFT);
6914 if (ret)
6915 break;
6916 }
6917
6918 cond_resched();
6919 }
6920
6921 return ret;
6922}
6923
Josef Bacik69ffb542012-09-11 15:40:07 -04006924static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
6925 u64 len, u64 orig_start,
6926 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006927 u64 orig_block_len, u64 ram_bytes,
6928 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04006929{
6930 struct extent_map_tree *em_tree;
6931 struct extent_map *em;
6932 struct btrfs_root *root = BTRFS_I(inode)->root;
6933 int ret;
6934
6935 em_tree = &BTRFS_I(inode)->extent_tree;
6936 em = alloc_extent_map();
6937 if (!em)
6938 return ERR_PTR(-ENOMEM);
6939
6940 em->start = start;
6941 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04006942 em->mod_start = start;
6943 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04006944 em->len = len;
6945 em->block_len = block_len;
6946 em->block_start = block_start;
6947 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05006948 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04006949 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04006950 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04006951 set_bit(EXTENT_FLAG_PINNED, &em->flags);
6952 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05006953 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04006954
6955 do {
6956 btrfs_drop_extent_cache(inode, em->start,
6957 em->start + em->len - 1, 0);
6958 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006959 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04006960 write_unlock(&em_tree->lock);
6961 } while (ret == -EEXIST);
6962
6963 if (ret) {
6964 free_extent_map(em);
6965 return ERR_PTR(ret);
6966 }
6967
6968 return em;
6969}
6970
6971
Josef Bacik4b46fce2010-05-23 11:00:55 -04006972static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
6973 struct buffer_head *bh_result, int create)
6974{
6975 struct extent_map *em;
6976 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04006977 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006978 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04006979 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006980 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04006981 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00006982 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006983
Miao Xie172a5042013-02-21 02:48:22 -07006984 if (create)
Josef Bacikeb838e72012-07-31 16:28:48 -04006985 unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07006986 else
Josef Bacikc3298612012-08-03 16:49:19 -04006987 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04006988
Josef Bacikc3298612012-08-03 16:49:19 -04006989 lockstart = start;
6990 lockend = start + len - 1;
6991
Josef Bacikeb838e72012-07-31 16:28:48 -04006992 /*
6993 * If this errors out it's because we couldn't invalidate pagecache for
6994 * this range and we need to fallback to buffered.
6995 */
6996 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
6997 return -ENOTBLK;
6998
Josef Bacik4b46fce2010-05-23 11:00:55 -04006999 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007000 if (IS_ERR(em)) {
7001 ret = PTR_ERR(em);
7002 goto unlock_err;
7003 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007004
7005 /*
7006 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7007 * io. INLINE is special, and we could probably kludge it in here, but
7008 * it's still buffered so for safety lets just fall back to the generic
7009 * buffered path.
7010 *
7011 * For COMPRESSED we _have_ to read the entire extent in so we can
7012 * decompress it, so there will be buffering required no matter what we
7013 * do, so go ahead and fallback to buffered.
7014 *
7015 * We return -ENOTBLK because thats what makes DIO go ahead and go back
7016 * to buffered IO. Don't blame me, this is the price we pay for using
7017 * the generic code.
7018 */
7019 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7020 em->block_start == EXTENT_MAP_INLINE) {
7021 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007022 ret = -ENOTBLK;
7023 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007024 }
7025
7026 /* Just a good old fashioned hole, return */
7027 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7028 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7029 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007030 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007031 }
7032
7033 /*
7034 * We don't allocate a new extent in the following cases
7035 *
7036 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7037 * existing extent.
7038 * 2) The extent is marked as PREALLOC. We're good to go here and can
7039 * just use the extent.
7040 *
7041 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007042 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007043 len = min(len, em->len - (start - em->start));
7044 lockstart = start + len;
7045 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007046 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007047
7048 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7049 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7050 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007051 int type;
7052 int ret;
Josef Bacikeb384b52013-04-24 16:32:55 -04007053 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007054
7055 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7056 type = BTRFS_ORDERED_PREALLOC;
7057 else
7058 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007059 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007060 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007061
Josef Bacik00361582013-08-14 14:02:47 -04007062 if (can_nocow_extent(inode, start, &len, &orig_start,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007063 &orig_block_len, &ram_bytes) == 1) {
Josef Bacik69ffb542012-09-11 15:40:07 -04007064 if (type == BTRFS_ORDERED_PREALLOC) {
7065 free_extent_map(em);
7066 em = create_pinned_em(inode, start, len,
7067 orig_start,
Josef Bacikb4939682012-12-03 10:31:19 -05007068 block_start, len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04007069 orig_block_len,
7070 ram_bytes, type);
Josef Bacik00361582013-08-14 14:02:47 -04007071 if (IS_ERR(em))
Josef Bacik69ffb542012-09-11 15:40:07 -04007072 goto unlock_err;
Josef Bacik69ffb542012-09-11 15:40:07 -04007073 }
7074
Chris Mason46bfbb52010-05-26 11:04:10 -04007075 ret = btrfs_add_ordered_extent_dio(inode, start,
7076 block_start, len, len, type);
Chris Mason46bfbb52010-05-26 11:04:10 -04007077 if (ret) {
7078 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007079 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007080 }
7081 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007082 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007083 }
Josef Bacik00361582013-08-14 14:02:47 -04007084
Chris Mason46bfbb52010-05-26 11:04:10 -04007085 /*
7086 * this will cow the extent, reset the len in case we changed
7087 * it above
7088 */
7089 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007090 free_extent_map(em);
7091 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007092 if (IS_ERR(em)) {
7093 ret = PTR_ERR(em);
7094 goto unlock_err;
7095 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007096 len = min(len, em->len - (start - em->start));
7097unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007098 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7099 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007100 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007101 bh_result->b_bdev = em->bdev;
7102 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007103 if (create) {
7104 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7105 set_buffer_new(bh_result);
7106
7107 /*
7108 * Need to update the i_size under the extent lock so buffered
7109 * readers will get the updated i_size when we unlock.
7110 */
7111 if (start + len > i_size_read(inode))
7112 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007113
Miao Xie172a5042013-02-21 02:48:22 -07007114 spin_lock(&BTRFS_I(inode)->lock);
7115 BTRFS_I(inode)->outstanding_extents++;
7116 spin_unlock(&BTRFS_I(inode)->lock);
7117
Miao Xie09348562013-02-07 10:12:07 +00007118 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7119 lockstart + len - 1, EXTENT_DELALLOC, NULL,
7120 &cached_state, GFP_NOFS);
7121 BUG_ON(ret);
Josef Bacikc3473e82012-06-19 10:59:00 -04007122 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007123
Josef Bacikeb838e72012-07-31 16:28:48 -04007124 /*
7125 * In the case of write we need to clear and unlock the entire range,
7126 * in the case of read we need to unlock only the end area that we
7127 * aren't using if there is any left over space.
7128 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007129 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007130 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7131 lockend, unlock_bits, 1, 0,
7132 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06007133 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007134 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007135 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007136
Josef Bacik4b46fce2010-05-23 11:00:55 -04007137 free_extent_map(em);
7138
7139 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007140
7141unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007142 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7143 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
7144 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007145}
7146
Josef Bacik4b46fce2010-05-23 11:00:55 -04007147static void btrfs_endio_direct_read(struct bio *bio, int err)
7148{
Miao Xiee65e1532010-11-22 03:04:43 +00007149 struct btrfs_dio_private *dip = bio->bi_private;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007150 struct bio_vec *bvec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007151 struct inode *inode = dip->inode;
7152 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason9be33952013-05-17 18:30:14 -04007153 struct bio *dio_bio;
Miao Xiefacc8a222013-07-25 19:22:34 +08007154 u32 *csums = (u32 *)dip->csum;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007155 u64 start;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007156 int i;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007157
7158 start = dip->logical_offset;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007159 bio_for_each_segment_all(bvec, bio, i) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007160 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
7161 struct page *page = bvec->bv_page;
7162 char *kaddr;
7163 u32 csum = ~(u32)0;
7164 unsigned long flags;
7165
7166 local_irq_save(flags);
Cong Wang7ac687d2011-11-25 23:14:28 +08007167 kaddr = kmap_atomic(page);
Liu Bob0496682013-03-14 14:57:45 +00007168 csum = btrfs_csum_data(kaddr + bvec->bv_offset,
Josef Bacik4b46fce2010-05-23 11:00:55 -04007169 csum, bvec->bv_len);
7170 btrfs_csum_final(csum, (char *)&csum);
Cong Wang7ac687d2011-11-25 23:14:28 +08007171 kunmap_atomic(kaddr);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007172 local_irq_restore(flags);
7173
7174 flush_dcache_page(bvec->bv_page);
Kent Overstreet2c30c712013-11-07 12:20:26 -08007175 if (csum != csums[i]) {
Miao Xiefacc8a222013-07-25 19:22:34 +08007176 btrfs_err(root->fs_info, "csum failed ino %llu off %llu csum %u expected csum %u",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02007177 btrfs_ino(inode), start, csum,
Kent Overstreet2c30c712013-11-07 12:20:26 -08007178 csums[i]);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007179 err = -EIO;
7180 }
7181 }
7182
7183 start += bvec->bv_len;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007184 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007185
7186 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007187 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04007188 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007189
Josef Bacik4b46fce2010-05-23 11:00:55 -04007190 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04007191
7192 /* If we had a csum failure make sure to clear the uptodate flag */
7193 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04007194 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
7195 dio_end_io(dio_bio, err);
7196 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007197}
7198
7199static void btrfs_endio_direct_write(struct bio *bio, int err)
7200{
7201 struct btrfs_dio_private *dip = bio->bi_private;
7202 struct inode *inode = dip->inode;
7203 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007204 struct btrfs_ordered_extent *ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05007205 u64 ordered_offset = dip->logical_offset;
7206 u64 ordered_bytes = dip->bytes;
Chris Mason9be33952013-05-17 18:30:14 -04007207 struct bio *dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007208 int ret;
7209
7210 if (err)
7211 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05007212again:
7213 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
7214 &ordered_offset,
Josef Bacik5fd02042012-05-02 14:00:54 -04007215 ordered_bytes, !err);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007216 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05007217 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007218
Liu Bo9e0af232014-08-15 23:36:53 +08007219 btrfs_init_work(&ordered->work, btrfs_endio_write_helper,
7220 finish_ordered_fn, NULL, NULL);
Qu Wenruofccb5d82014-02-28 10:46:10 +08007221 btrfs_queue_work(root->fs_info->endio_write_workers,
7222 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05007223out_test:
7224 /*
7225 * our bio might span multiple ordered extents. If we haven't
7226 * completed the accounting for the whole dio, go back and try again
7227 */
7228 if (ordered_offset < dip->logical_offset + dip->bytes) {
7229 ordered_bytes = dip->logical_offset + dip->bytes -
7230 ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04007231 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05007232 goto again;
7233 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007234out_done:
Chris Mason9be33952013-05-17 18:30:14 -04007235 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007236
Josef Bacik4b46fce2010-05-23 11:00:55 -04007237 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04007238
7239 /* If we had an error make sure to clear the uptodate flag */
7240 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04007241 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
7242 dio_end_io(dio_bio, err);
7243 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007244}
7245
Chris Masoneaf25d92010-05-25 09:48:28 -04007246static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
7247 struct bio *bio, int mirror_num,
7248 unsigned long bio_flags, u64 offset)
7249{
7250 int ret;
7251 struct btrfs_root *root = BTRFS_I(inode)->root;
7252 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007253 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04007254 return 0;
7255}
7256
Miao Xiee65e1532010-11-22 03:04:43 +00007257static void btrfs_end_dio_bio(struct bio *bio, int err)
7258{
7259 struct btrfs_dio_private *dip = bio->bi_private;
7260
7261 if (err) {
Frank Holtonefe120a2013-12-20 11:37:06 -05007262 btrfs_err(BTRFS_I(dip->inode)->root->fs_info,
7263 "direct IO failed ino %llu rw %lu sector %#Lx len %u err no %d",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02007264 btrfs_ino(dip->inode), bio->bi_rw,
Kent Overstreet4f024f32013-10-11 15:44:27 -07007265 (unsigned long long)bio->bi_iter.bi_sector,
7266 bio->bi_iter.bi_size, err);
Miao Xiee65e1532010-11-22 03:04:43 +00007267 dip->errors = 1;
7268
7269 /*
7270 * before atomic variable goto zero, we must make sure
7271 * dip->errors is perceived to be set.
7272 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01007273 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00007274 }
7275
7276 /* if there are more bios still pending for this dio, just exit */
7277 if (!atomic_dec_and_test(&dip->pending_bios))
7278 goto out;
7279
Chris Mason9be33952013-05-17 18:30:14 -04007280 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00007281 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04007282 } else {
7283 set_bit(BIO_UPTODATE, &dip->dio_bio->bi_flags);
Miao Xiee65e1532010-11-22 03:04:43 +00007284 bio_endio(dip->orig_bio, 0);
7285 }
7286out:
7287 bio_put(bio);
7288}
7289
7290static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
7291 u64 first_sector, gfp_t gfp_flags)
7292{
7293 int nr_vecs = bio_get_nr_vecs(bdev);
7294 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
7295}
7296
7297static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
7298 int rw, u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007299 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00007300{
Miao Xiefacc8a222013-07-25 19:22:34 +08007301 struct btrfs_dio_private *dip = bio->bi_private;
Miao Xiee65e1532010-11-22 03:04:43 +00007302 int write = rw & REQ_WRITE;
7303 struct btrfs_root *root = BTRFS_I(inode)->root;
7304 int ret;
7305
Josef Bacikb812ce22012-11-16 13:56:32 -05007306 if (async_submit)
7307 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
7308
Miao Xiee65e1532010-11-22 03:04:43 +00007309 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04007310
7311 if (!write) {
7312 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
7313 if (ret)
7314 goto err;
7315 }
Miao Xiee65e1532010-11-22 03:04:43 +00007316
Josef Bacik1ae39932011-04-06 14:41:34 -04007317 if (skip_sum)
7318 goto map;
7319
7320 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00007321 ret = btrfs_wq_submit_bio(root->fs_info,
7322 inode, rw, bio, 0, 0,
7323 file_offset,
7324 __btrfs_submit_bio_start_direct_io,
7325 __btrfs_submit_bio_done);
7326 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04007327 } else if (write) {
7328 /*
7329 * If we aren't doing async submit, calculate the csum of the
7330 * bio now.
7331 */
7332 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
7333 if (ret)
7334 goto err;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007335 } else if (!skip_sum) {
Miao Xiefacc8a222013-07-25 19:22:34 +08007336 ret = btrfs_lookup_bio_sums_dio(root, inode, dip, bio,
7337 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007338 if (ret)
7339 goto err;
7340 }
Miao Xiee65e1532010-11-22 03:04:43 +00007341
Josef Bacik1ae39932011-04-06 14:41:34 -04007342map:
7343 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007344err:
7345 bio_put(bio);
7346 return ret;
7347}
7348
7349static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
7350 int skip_sum)
7351{
7352 struct inode *inode = dip->inode;
7353 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00007354 struct bio *bio;
7355 struct bio *orig_bio = dip->orig_bio;
7356 struct bio_vec *bvec = orig_bio->bi_io_vec;
Kent Overstreet4f024f32013-10-11 15:44:27 -07007357 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00007358 u64 file_offset = dip->logical_offset;
7359 u64 submit_len = 0;
7360 u64 map_length;
7361 int nr_pages = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007362 int ret = 0;
Josef Bacik1ae39932011-04-06 14:41:34 -04007363 int async_submit = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007364
Kent Overstreet4f024f32013-10-11 15:44:27 -07007365 map_length = orig_bio->bi_iter.bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007366 ret = btrfs_map_block(root->fs_info, rw, start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007367 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08007368 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00007369 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08007370
Kent Overstreet4f024f32013-10-11 15:44:27 -07007371 if (map_length >= orig_bio->bi_iter.bi_size) {
Josef Bacik02f57c72011-04-06 14:25:44 -04007372 bio = orig_bio;
7373 goto submit;
7374 }
7375
David Woodhouse53b381b2013-01-29 18:40:14 -05007376 /* async crcs make it difficult to collect full stripe writes. */
7377 if (btrfs_get_alloc_profile(root, 1) &
7378 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6))
7379 async_submit = 0;
7380 else
7381 async_submit = 1;
7382
Josef Bacik02f57c72011-04-06 14:25:44 -04007383 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
7384 if (!bio)
7385 return -ENOMEM;
Miao Xie7a5c3c92014-06-17 18:58:59 +08007386
Josef Bacik02f57c72011-04-06 14:25:44 -04007387 bio->bi_private = dip;
7388 bio->bi_end_io = btrfs_end_dio_bio;
7389 atomic_inc(&dip->pending_bios);
7390
Miao Xiee65e1532010-11-22 03:04:43 +00007391 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
7392 if (unlikely(map_length < submit_len + bvec->bv_len ||
7393 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
7394 bvec->bv_offset) < bvec->bv_len)) {
7395 /*
7396 * inc the count before we submit the bio so
7397 * we know the end IO handler won't happen before
7398 * we inc the count. Otherwise, the dip might get freed
7399 * before we're done setting it up
7400 */
7401 atomic_inc(&dip->pending_bios);
7402 ret = __btrfs_submit_dio_bio(bio, inode, rw,
7403 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007404 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007405 if (ret) {
7406 bio_put(bio);
7407 atomic_dec(&dip->pending_bios);
7408 goto out_err;
7409 }
7410
Miao Xiee65e1532010-11-22 03:04:43 +00007411 start_sector += submit_len >> 9;
7412 file_offset += submit_len;
7413
7414 submit_len = 0;
7415 nr_pages = 0;
7416
7417 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
7418 start_sector, GFP_NOFS);
7419 if (!bio)
7420 goto out_err;
7421 bio->bi_private = dip;
7422 bio->bi_end_io = btrfs_end_dio_bio;
7423
Kent Overstreet4f024f32013-10-11 15:44:27 -07007424 map_length = orig_bio->bi_iter.bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007425 ret = btrfs_map_block(root->fs_info, rw,
Stefan Behrens3ec706c2012-11-05 15:46:42 +01007426 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007427 &map_length, NULL, 0);
7428 if (ret) {
7429 bio_put(bio);
7430 goto out_err;
7431 }
7432 } else {
7433 submit_len += bvec->bv_len;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307434 nr_pages++;
Miao Xiee65e1532010-11-22 03:04:43 +00007435 bvec++;
7436 }
7437 }
7438
Josef Bacik02f57c72011-04-06 14:25:44 -04007439submit:
Miao Xiee65e1532010-11-22 03:04:43 +00007440 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007441 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007442 if (!ret)
7443 return 0;
7444
7445 bio_put(bio);
7446out_err:
7447 dip->errors = 1;
7448 /*
7449 * before atomic variable goto zero, we must
7450 * make sure dip->errors is perceived to be set.
7451 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01007452 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00007453 if (atomic_dec_and_test(&dip->pending_bios))
7454 bio_io_error(dip->orig_bio);
7455
7456 /* bio_end_io() will handle error, so we needn't return it */
7457 return 0;
7458}
7459
Chris Mason9be33952013-05-17 18:30:14 -04007460static void btrfs_submit_direct(int rw, struct bio *dio_bio,
7461 struct inode *inode, loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007462{
7463 struct btrfs_root *root = BTRFS_I(inode)->root;
7464 struct btrfs_dio_private *dip;
Chris Mason9be33952013-05-17 18:30:14 -04007465 struct bio *io_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007466 int skip_sum;
Miao Xiefacc8a222013-07-25 19:22:34 +08007467 int sum_len;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02007468 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007469 int ret = 0;
Miao Xiefacc8a222013-07-25 19:22:34 +08007470 u16 csum_size;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007471
7472 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
7473
Chris Mason9be33952013-05-17 18:30:14 -04007474 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04007475 if (!io_bio) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007476 ret = -ENOMEM;
7477 goto free_ordered;
7478 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007479
Miao Xiefacc8a222013-07-25 19:22:34 +08007480 if (!skip_sum && !write) {
7481 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
Kent Overstreet4f024f32013-10-11 15:44:27 -07007482 sum_len = dio_bio->bi_iter.bi_size >>
7483 inode->i_sb->s_blocksize_bits;
Miao Xiefacc8a222013-07-25 19:22:34 +08007484 sum_len *= csum_size;
7485 } else {
7486 sum_len = 0;
7487 }
7488
7489 dip = kmalloc(sizeof(*dip) + sum_len, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04007490 if (!dip) {
7491 ret = -ENOMEM;
7492 goto free_io_bio;
7493 }
7494
7495 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007496 dip->inode = inode;
7497 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07007498 dip->bytes = dio_bio->bi_iter.bi_size;
7499 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Chris Mason9be33952013-05-17 18:30:14 -04007500 io_bio->bi_private = dip;
Miao Xiee65e1532010-11-22 03:04:43 +00007501 dip->errors = 0;
Chris Mason9be33952013-05-17 18:30:14 -04007502 dip->orig_bio = io_bio;
7503 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00007504 atomic_set(&dip->pending_bios, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007505
7506 if (write)
Chris Mason9be33952013-05-17 18:30:14 -04007507 io_bio->bi_end_io = btrfs_endio_direct_write;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007508 else
Chris Mason9be33952013-05-17 18:30:14 -04007509 io_bio->bi_end_io = btrfs_endio_direct_read;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007510
Miao Xiee65e1532010-11-22 03:04:43 +00007511 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
7512 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04007513 return;
Chris Mason9be33952013-05-17 18:30:14 -04007514
7515free_io_bio:
7516 bio_put(io_bio);
7517
Josef Bacik4b46fce2010-05-23 11:00:55 -04007518free_ordered:
7519 /*
7520 * If this is a write, we need to clean up the reserved space and kill
7521 * the ordered extent.
7522 */
7523 if (write) {
7524 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05007525 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007526 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
7527 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
7528 btrfs_free_reserved_extent(root, ordered->start,
Miao Xiee570fd22014-06-19 10:42:50 +08007529 ordered->disk_len, 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007530 btrfs_put_ordered_extent(ordered);
7531 btrfs_put_ordered_extent(ordered);
7532 }
Chris Mason9be33952013-05-17 18:30:14 -04007533 bio_endio(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007534}
7535
Chris Mason5a5f79b2010-05-26 21:33:37 -04007536static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
Al Viro28060d52014-03-22 05:15:17 -04007537 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04007538{
7539 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00007540 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007541 unsigned blocksize_mask = root->sectorsize - 1;
7542 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007543
7544 if (offset & blocksize_mask)
7545 goto out;
7546
Al Viro28060d52014-03-22 05:15:17 -04007547 if (iov_iter_alignment(iter) & blocksize_mask)
7548 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00007549
Al Viro28060d52014-03-22 05:15:17 -04007550 /* If this is a write we don't need to check anymore */
7551 if (rw & WRITE)
7552 return 0;
7553 /*
7554 * Check to make sure we don't have duplicate iov_base's in this
7555 * iovec, if so return EINVAL, otherwise we'll get csum errors
7556 * when reading back.
7557 */
7558 for (seg = 0; seg < iter->nr_segs; seg++) {
7559 for (i = seg + 1; i < iter->nr_segs; i++) {
7560 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00007561 goto out;
7562 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04007563 }
7564 retval = 0;
7565out:
7566 return retval;
7567}
Josef Bacikeb838e72012-07-31 16:28:48 -04007568
Chris Mason16432982008-04-10 10:23:21 -04007569static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
Al Virod8d3d942014-03-04 21:27:34 -05007570 struct iov_iter *iter, loff_t offset)
Chris Mason16432982008-04-10 10:23:21 -04007571{
Josef Bacik4b46fce2010-05-23 11:00:55 -04007572 struct file *file = iocb->ki_filp;
7573 struct inode *inode = file->f_mapping->host;
Miao Xie09348562013-02-07 10:12:07 +00007574 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00007575 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00007576 bool wakeup = true;
7577 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00007578 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007579
Al Viro28060d52014-03-22 05:15:17 -04007580 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007581 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007582
Miao Xie38851cc2013-02-08 07:04:11 +00007583 atomic_inc(&inode->i_dio_count);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01007584 smp_mb__after_atomic();
Miao Xie38851cc2013-02-08 07:04:11 +00007585
Josef Bacik0e267c42013-07-02 10:38:02 -04007586 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08007587 * The generic stuff only does filemap_write_and_wait_range, which
7588 * isn't enough if we've written compressed pages to this area, so
7589 * we need to flush the dirty pages again to make absolutely sure
7590 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04007591 */
Al Viroa6cbcd42014-03-04 22:38:00 -05007592 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08007593 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
7594 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08007595 filemap_fdatawrite_range(inode->i_mapping, offset,
7596 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04007597
Miao Xie09348562013-02-07 10:12:07 +00007598 if (rw & WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00007599 /*
7600 * If the write DIO is beyond the EOF, we need update
7601 * the isize, but it is protected by i_mutex. So we can
7602 * not unlock the i_mutex at this case.
7603 */
7604 if (offset + count <= inode->i_size) {
7605 mutex_unlock(&inode->i_mutex);
7606 relock = true;
7607 }
Miao Xie09348562013-02-07 10:12:07 +00007608 ret = btrfs_delalloc_reserve_space(inode, count);
7609 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00007610 goto out;
7611 } else if (unlikely(test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
7612 &BTRFS_I(inode)->runtime_flags))) {
7613 inode_dio_done(inode);
7614 flags = DIO_LOCKING | DIO_SKIP_HOLES;
7615 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00007616 }
7617
7618 ret = __blockdev_direct_IO(rw, iocb, inode,
7619 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
Al Viro31b14032014-03-05 01:33:16 -05007620 iter, offset, btrfs_get_blocks_direct, NULL,
Miao Xie2e60a512013-02-08 07:01:08 +00007621 btrfs_submit_direct, flags);
Miao Xie09348562013-02-07 10:12:07 +00007622 if (rw & WRITE) {
7623 if (ret < 0 && ret != -EIOCBQUEUED)
7624 btrfs_delalloc_release_space(inode, count);
Miao Xie172a5042013-02-21 02:48:22 -07007625 else if (ret >= 0 && (size_t)ret < count)
Miao Xie09348562013-02-07 10:12:07 +00007626 btrfs_delalloc_release_space(inode,
7627 count - (size_t)ret);
Miao Xie172a5042013-02-21 02:48:22 -07007628 else
7629 btrfs_delalloc_release_metadata(inode, 0);
Miao Xie09348562013-02-07 10:12:07 +00007630 }
Miao Xie38851cc2013-02-08 07:04:11 +00007631out:
Miao Xie2e60a512013-02-08 07:01:08 +00007632 if (wakeup)
7633 inode_dio_done(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00007634 if (relock)
7635 mutex_lock(&inode->i_mutex);
Miao Xie09348562013-02-07 10:12:07 +00007636
7637 return ret;
Chris Mason16432982008-04-10 10:23:21 -04007638}
7639
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007640#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
7641
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007642static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
7643 __u64 start, __u64 len)
7644{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007645 int ret;
7646
7647 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
7648 if (ret)
7649 return ret;
7650
Chris Masonec29ed52011-02-23 16:23:20 -05007651 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007652}
7653
Chris Mason9ebefb182007-06-15 13:50:00 -04007654int btrfs_readpage(struct file *file, struct page *page)
7655{
Chris Masond1310b22008-01-24 16:13:08 -05007656 struct extent_io_tree *tree;
7657 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02007658 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04007659}
Chris Mason1832a6d2007-12-21 16:27:21 -05007660
Chris Mason39279cc2007-06-12 06:35:45 -04007661static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
7662{
Chris Masond1310b22008-01-24 16:13:08 -05007663 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04007664
7665
7666 if (current->flags & PF_MEMALLOC) {
7667 redirty_page_for_writepage(wbc, page);
7668 unlock_page(page);
7669 return 0;
7670 }
Chris Masond1310b22008-01-24 16:13:08 -05007671 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04007672 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
7673}
Chris Mason39279cc2007-06-12 06:35:45 -04007674
Eric Sandeen48a3b632013-04-25 20:41:01 +00007675static int btrfs_writepages(struct address_space *mapping,
7676 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04007677{
Chris Masond1310b22008-01-24 16:13:08 -05007678 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05007679
Chris Masond1310b22008-01-24 16:13:08 -05007680 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f02e2007-11-01 19:45:34 -04007681 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
7682}
7683
Chris Mason3ab2fb52007-11-08 10:59:22 -05007684static int
7685btrfs_readpages(struct file *file, struct address_space *mapping,
7686 struct list_head *pages, unsigned nr_pages)
7687{
Chris Masond1310b22008-01-24 16:13:08 -05007688 struct extent_io_tree *tree;
7689 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05007690 return extent_readpages(tree, mapping, pages, nr_pages,
7691 btrfs_get_extent);
7692}
Chris Masone6dcd2d2008-07-17 12:53:50 -04007693static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04007694{
Chris Masond1310b22008-01-24 16:13:08 -05007695 struct extent_io_tree *tree;
7696 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04007697 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04007698
Chris Masond1310b22008-01-24 16:13:08 -05007699 tree = &BTRFS_I(page->mapping->host)->io_tree;
7700 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05007701 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04007702 if (ret == 1) {
7703 ClearPagePrivate(page);
7704 set_page_private(page, 0);
7705 page_cache_release(page);
7706 }
7707 return ret;
7708}
Chris Mason39279cc2007-06-12 06:35:45 -04007709
Chris Masone6dcd2d2008-07-17 12:53:50 -04007710static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
7711{
Chris Mason98509cf2008-09-11 15:51:43 -04007712 if (PageWriteback(page) || PageDirty(page))
7713 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05007714 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007715}
7716
Lukas Czernerd47992f2013-05-21 23:17:23 -04007717static void btrfs_invalidatepage(struct page *page, unsigned int offset,
7718 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04007719{
Josef Bacik5fd02042012-05-02 14:00:54 -04007720 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05007721 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007722 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007723 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007724 u64 page_start = page_offset(page);
7725 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00007726 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04007727
Chris Mason8b62b722009-09-02 16:53:46 -04007728 /*
7729 * we have the page locked, so new writeback can't start,
7730 * and the dirty bit won't be cleared while we are here.
7731 *
7732 * Wait for IO on this page so that we can safely clear
7733 * the PagePrivate2 bit and do ordered accounting
7734 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007735 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04007736
Josef Bacik5fd02042012-05-02 14:00:54 -04007737 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007738 if (offset) {
7739 btrfs_releasepage(page, GFP_NOFS);
7740 return;
7741 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00007742
7743 if (!inode_evicting)
7744 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
7745 ordered = btrfs_lookup_ordered_extent(inode, page_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007746 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04007747 /*
7748 * IO on this page will never be started, so we need
7749 * to account for any ordered extents now
7750 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00007751 if (!inode_evicting)
7752 clear_extent_bit(tree, page_start, page_end,
7753 EXTENT_DIRTY | EXTENT_DELALLOC |
7754 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
7755 EXTENT_DEFRAG, 1, 0, &cached_state,
7756 GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04007757 /*
7758 * whoever cleared the private bit is responsible
7759 * for the finish_ordered_io
7760 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04007761 if (TestClearPagePrivate2(page)) {
7762 struct btrfs_ordered_inode_tree *tree;
7763 u64 new_len;
7764
7765 tree = &BTRFS_I(inode)->ordered_tree;
7766
7767 spin_lock_irq(&tree->lock);
7768 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
7769 new_len = page_start - ordered->file_offset;
7770 if (new_len < ordered->truncated_len)
7771 ordered->truncated_len = new_len;
7772 spin_unlock_irq(&tree->lock);
7773
7774 if (btrfs_dec_test_ordered_pending(inode, &ordered,
7775 page_start,
7776 PAGE_CACHE_SIZE, 1))
7777 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04007778 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007779 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00007780 if (!inode_evicting) {
7781 cached_state = NULL;
7782 lock_extent_bits(tree, page_start, page_end, 0,
7783 &cached_state);
7784 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007785 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00007786
7787 if (!inode_evicting) {
7788 clear_extent_bit(tree, page_start, page_end,
7789 EXTENT_LOCKED | EXTENT_DIRTY |
7790 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
7791 EXTENT_DEFRAG, 1, 1,
7792 &cached_state, GFP_NOFS);
7793
7794 __btrfs_releasepage(page, GFP_NOFS);
7795 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007796
Chris Mason4a096752008-07-21 10:29:44 -04007797 ClearPageChecked(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04007798 if (PagePrivate(page)) {
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04007799 ClearPagePrivate(page);
7800 set_page_private(page, 0);
7801 page_cache_release(page);
7802 }
Chris Mason39279cc2007-06-12 06:35:45 -04007803}
7804
Chris Mason9ebefb182007-06-15 13:50:00 -04007805/*
7806 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
7807 * called from a page fault handler when a page is first dirtied. Hence we must
7808 * be careful to check for EOF conditions here. We set the page up correctly
7809 * for a written page which means we get ENOSPC checking when writing into
7810 * holes and correct delalloc and unwritten extent mapping on filesystems that
7811 * support these features.
7812 *
7813 * We are not allowed to take the i_mutex here so we have to play games to
7814 * protect against truncate races as the page could now be beyond EOF. Because
7815 * vmtruncate() writes the inode size before removing pages, once we have the
7816 * page lock we can determine safely if the page is beyond EOF. If it is not
7817 * beyond EOF, then the page is guaranteed safe against truncation until we
7818 * unlock the page.
7819 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07007820int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04007821{
Nick Pigginc2ec1752009-03-31 15:23:21 -07007822 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05007823 struct inode *inode = file_inode(vma->vm_file);
Chris Mason1832a6d2007-12-21 16:27:21 -05007824 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007825 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7826 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007827 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007828 char *kaddr;
7829 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04007830 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05007831 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05007832 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007833 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007834 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04007835
Jan Karab2b5ef52012-06-12 16:20:45 +02007836 sb_start_pagefault(inode->i_sb);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04007837 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007838 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04007839 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05007840 reserved = 1;
7841 }
Nick Piggin56a76f82009-03-31 15:23:23 -07007842 if (ret) {
7843 if (ret == -ENOMEM)
7844 ret = VM_FAULT_OOM;
7845 else /* -ENOSPC, -EIO, etc */
7846 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05007847 if (reserved)
7848 goto out;
7849 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07007850 }
Chris Mason1832a6d2007-12-21 16:27:21 -05007851
Nick Piggin56a76f82009-03-31 15:23:23 -07007852 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007853again:
Chris Mason9ebefb182007-06-15 13:50:00 -04007854 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04007855 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007856 page_start = page_offset(page);
7857 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04007858
Chris Mason9ebefb182007-06-15 13:50:00 -04007859 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04007860 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04007861 /* page got truncated out from underneath us */
7862 goto out_unlock;
7863 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007864 wait_on_page_writeback(page);
7865
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007866 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007867 set_page_extent_mapped(page);
7868
Chris Masoneb84ae02008-07-17 13:53:27 -04007869 /*
7870 * we can't set the delalloc bits if there are pending ordered
7871 * extents. Drop our locks and wait for them to finish
7872 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007873 ordered = btrfs_lookup_ordered_extent(inode, page_start);
7874 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007875 unlock_extent_cached(io_tree, page_start, page_end,
7876 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007877 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04007878 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007879 btrfs_put_ordered_extent(ordered);
7880 goto again;
7881 }
7882
Josef Bacikfbf19082009-10-01 17:10:23 -04007883 /*
7884 * XXX - page_mkwrite gets called every time the page is dirtied, even
7885 * if it was already dirty, so for space accounting reasons we need to
7886 * clear any delalloc bits for the range we are fixing to save. There
7887 * is probably a better way to do this, but for now keep consistent with
7888 * prepare_pages in the normal write path.
7889 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00007890 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06007891 EXTENT_DIRTY | EXTENT_DELALLOC |
7892 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00007893 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04007894
Josef Bacik2ac55d42010-02-03 19:33:23 +00007895 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
7896 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007897 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007898 unlock_extent_cached(io_tree, page_start, page_end,
7899 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007900 ret = VM_FAULT_SIGBUS;
7901 goto out_unlock;
7902 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007903 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04007904
7905 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04007906 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04007907 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04007908 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04007909 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04007910
Chris Masone6dcd2d2008-07-17 12:53:50 -04007911 if (zero_start != PAGE_CACHE_SIZE) {
7912 kaddr = kmap(page);
7913 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
7914 flush_dcache_page(page);
7915 kunmap(page);
7916 }
Chris Mason247e7432008-07-17 12:53:51 -04007917 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007918 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04007919 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007920
Chris Mason257c62e2009-10-13 13:21:08 -04007921 BTRFS_I(inode)->last_trans = root->fs_info->generation;
7922 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06007923 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04007924
Josef Bacik2ac55d42010-02-03 19:33:23 +00007925 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04007926
7927out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02007928 if (!ret) {
7929 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04007930 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02007931 }
Chris Mason9ebefb182007-06-15 13:50:00 -04007932 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05007933out:
Josef Bacikec39e182012-01-12 19:10:12 -05007934 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007935out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02007936 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04007937 return ret;
7938}
7939
Josef Bacika41ad392011-01-31 15:30:16 -05007940static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04007941{
7942 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04007943 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04007944 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05007945 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007946 struct btrfs_trans_handle *trans;
Chris Masondbe674a2008-07-17 12:54:05 -04007947 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04007948 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04007949
Josef Bacik0ef8b722013-10-25 16:13:35 -04007950 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
7951 (u64)-1);
7952 if (ret)
7953 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04007954
Josef Bacikfcb80c22011-05-03 10:40:22 -04007955 /*
7956 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
7957 * 3 things going on here
7958 *
7959 * 1) We need to reserve space for our orphan item and the space to
7960 * delete our orphan item. Lord knows we don't want to have a dangling
7961 * orphan item because we didn't reserve space to remove it.
7962 *
7963 * 2) We need to reserve space to update our inode.
7964 *
7965 * 3) We need to have something to cache all the space that is going to
7966 * be free'd up by the truncate operation, but also have some slack
7967 * space reserved in case it uses space during the truncate (thank you
7968 * very much snapshotting).
7969 *
7970 * And we need these to all be seperate. The fact is we can use alot of
7971 * space doing the truncate, and we have no earthly idea how much space
7972 * we will use, so we need the truncate reservation to be seperate so it
7973 * doesn't end up using space reserved for updating the inode or
7974 * removing the orphan item. We also need to be able to stop the
7975 * transaction and start a new one, which means we need to be able to
7976 * update the inode several times, and we have no idea of knowing how
7977 * many times that will be, so we can't just reserve 1 item for the
7978 * entirety of the opration, so that has to be done seperately as well.
7979 * Then there is the orphan item, which does indeed need to be held on
7980 * to for the whole operation, and we need nobody to touch this reserved
7981 * space except the orphan code.
7982 *
7983 * So that leaves us with
7984 *
7985 * 1) root->orphan_block_rsv - for the orphan deletion.
7986 * 2) rsv - for the truncate reservation, which we will steal from the
7987 * transaction reservation.
7988 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
7989 * updating the inode.
7990 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06007991 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007992 if (!rsv)
7993 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04007994 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04007995 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05007996
Josef Bacik907cbce2011-08-08 13:46:15 -04007997 /*
Josef Bacik07127182011-08-19 10:29:59 -04007998 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04007999 * 1 for updating the inode.
8000 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05008001 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008002 if (IS_ERR(trans)) {
8003 err = PTR_ERR(trans);
8004 goto out;
8005 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05008006
Josef Bacik907cbce2011-08-08 13:46:15 -04008007 /* Migrate the slack space for the truncate to our reserve */
8008 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
8009 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008010 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05008011
Chris Mason5a3f23d2009-03-31 13:27:11 -04008012 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04008013 * So if we truncate and then write and fsync we normally would just
8014 * write the extents that changed, which is a problem if we need to
8015 * first truncate that entire inode. So set this flag so we write out
8016 * all of the extents in the inode to the sync log so we're completely
8017 * safe.
8018 */
8019 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008020 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008021
Yan, Zheng80825102009-11-12 09:35:36 +00008022 while (1) {
8023 ret = btrfs_truncate_inode_items(trans, root, inode,
8024 inode->i_size,
8025 BTRFS_EXTENT_DATA_KEY);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008026 if (ret != -ENOSPC) {
Josef Bacik3893e332011-01-31 16:03:11 -05008027 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00008028 break;
Josef Bacik3893e332011-01-31 16:03:11 -05008029 }
Chris Mason39279cc2007-06-12 06:35:45 -04008030
Josef Bacikfcb80c22011-05-03 10:40:22 -04008031 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008032 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05008033 if (ret) {
8034 err = ret;
8035 break;
8036 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04008037
Yan, Zheng80825102009-11-12 09:35:36 +00008038 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00008039 btrfs_btree_balance_dirty(root);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008040
8041 trans = btrfs_start_transaction(root, 2);
8042 if (IS_ERR(trans)) {
8043 ret = err = PTR_ERR(trans);
8044 trans = NULL;
8045 break;
8046 }
8047
8048 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
8049 rsv, min_size);
8050 BUG_ON(ret); /* shouldn't happen */
8051 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008052 }
8053
8054 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04008055 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008056 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05008057 if (ret)
8058 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00008059 }
8060
Chris Mason917c16b2011-11-08 14:49:59 -05008061 if (trans) {
8062 trans->block_rsv = &root->fs_info->trans_block_rsv;
8063 ret = btrfs_update_inode(trans, root, inode);
8064 if (ret && !err)
8065 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04008066
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008067 ret = btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00008068 btrfs_btree_balance_dirty(root);
Chris Mason917c16b2011-11-08 14:49:59 -05008069 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04008070
8071out:
8072 btrfs_free_block_rsv(root, rsv);
8073
Josef Bacik3893e332011-01-31 16:03:11 -05008074 if (ret && !err)
8075 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05008076
Josef Bacik3893e332011-01-31 16:03:11 -05008077 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04008078}
8079
Sven Wegener3b963622008-06-09 21:57:42 -04008080/*
Chris Masond352ac62008-09-29 15:18:18 -04008081 * create a new subvolume directory/inode (helper for the ioctl).
8082 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05008083int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008084 struct btrfs_root *new_root,
8085 struct btrfs_root *parent_root,
8086 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04008087{
Chris Mason39279cc2007-06-12 06:35:45 -04008088 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04008089 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008090 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008091
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01008092 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
8093 new_dirid, new_dirid,
8094 S_IFDIR | (~current_umask() & S_IRWXUGO),
8095 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04008096 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04008097 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008098 inode->i_op = &btrfs_dir_inode_operations;
8099 inode->i_fop = &btrfs_dir_file_operations;
8100
Miklos Szeredibfe86842011-10-28 14:13:29 +02008101 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04008102 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04008103
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008104 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
8105 if (err)
8106 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02008107 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008108 new_root->root_key.objectid, err);
8109
Yan, Zheng76dda932009-09-21 16:00:26 -04008110 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04008111
Yan, Zheng76dda932009-09-21 16:00:26 -04008112 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07008113 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04008114}
8115
Chris Mason39279cc2007-06-12 06:35:45 -04008116struct inode *btrfs_alloc_inode(struct super_block *sb)
8117{
8118 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008119 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008120
8121 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
8122 if (!ei)
8123 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008124
8125 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008126 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04008127 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04008128 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04008129 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008130 ei->delalloc_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008131 ei->disk_i_size = 0;
8132 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04008133 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008134 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08008135 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008136 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06008137 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008138
Josef Bacik9e0baf62011-07-15 15:16:44 +00008139 spin_lock_init(&ei->lock);
8140 ei->outstanding_extents = 0;
8141 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008142
Josef Bacik72ac3c02012-05-23 14:13:11 -04008143 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08008144 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008145
Miao Xie16cdcec2011-04-22 18:12:22 +08008146 ei->delayed_node = NULL;
8147
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008148 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02008149 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02008150 extent_io_tree_init(&ei->io_tree, &inode->i_data);
8151 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04008152 ei->io_tree.track_uptodate = 1;
8153 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05008154 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008155 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05008156 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008157 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008158 INIT_LIST_HEAD(&ei->delalloc_inodes);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008159 RB_CLEAR_NODE(&ei->rb_node);
8160
8161 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008162}
8163
Josef Bacikaaedb552013-10-11 14:44:09 -04008164#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
8165void btrfs_test_destroy_inode(struct inode *inode)
8166{
8167 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
8168 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8169}
8170#endif
8171
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008172static void btrfs_i_callback(struct rcu_head *head)
8173{
8174 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008175 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8176}
8177
Chris Mason39279cc2007-06-12 06:35:45 -04008178void btrfs_destroy_inode(struct inode *inode)
8179{
Chris Masone6dcd2d2008-07-17 12:53:50 -04008180 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008181 struct btrfs_root *root = BTRFS_I(inode)->root;
8182
Al Virob3d9b7a2012-06-09 13:51:19 -04008183 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04008184 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00008185 WARN_ON(BTRFS_I(inode)->outstanding_extents);
8186 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04008187 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
8188 WARN_ON(BTRFS_I(inode)->csum_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04008189
Chris Mason5a3f23d2009-03-31 13:27:11 -04008190 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05008191 * This can happen where we create an inode, but somebody else also
8192 * created the same inode and we need to destroy the one we already
8193 * created.
8194 */
8195 if (!root)
8196 goto free;
8197
Josef Bacik8a35d952012-05-23 14:26:42 -04008198 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
8199 &BTRFS_I(inode)->runtime_flags)) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008200 btrfs_info(root->fs_info, "inode %llu still on the orphan list",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02008201 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04008202 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04008203 }
Josef Bacik7b128762008-07-24 12:17:14 -04008204
Chris Masond3977122009-01-05 21:25:51 -05008205 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008206 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
8207 if (!ordered)
8208 break;
8209 else {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008210 btrfs_err(root->fs_info, "found ordered extent %llu %llu on inode cleanup",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02008211 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008212 btrfs_remove_ordered_extent(inode, ordered);
8213 btrfs_put_ordered_extent(ordered);
8214 btrfs_put_ordered_extent(ordered);
8215 }
8216 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008217 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008218 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05008219free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008220 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04008221}
8222
Al Viro45321ac2010-06-07 13:43:19 -04008223int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04008224{
8225 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04008226
Naohiro Aota6379ef92013-06-06 09:56:34 +00008227 if (root == NULL)
8228 return 1;
8229
Liu Bofa6ac872013-02-20 14:10:23 +00008230 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02008231 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04008232 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04008233 else
Al Viro45321ac2010-06-07 13:43:19 -04008234 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04008235}
8236
Sven Wegener0ee0fda2008-07-30 16:54:26 -04008237static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04008238{
8239 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
8240
8241 inode_init_once(&ei->vfs_inode);
8242}
8243
8244void btrfs_destroy_cachep(void)
8245{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10008246 /*
8247 * Make sure all delayed rcu free inodes are flushed before we
8248 * destroy cache.
8249 */
8250 rcu_barrier();
Chris Mason39279cc2007-06-12 06:35:45 -04008251 if (btrfs_inode_cachep)
8252 kmem_cache_destroy(btrfs_inode_cachep);
8253 if (btrfs_trans_handle_cachep)
8254 kmem_cache_destroy(btrfs_trans_handle_cachep);
8255 if (btrfs_transaction_cachep)
8256 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008257 if (btrfs_path_cachep)
8258 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05008259 if (btrfs_free_space_cachep)
8260 kmem_cache_destroy(btrfs_free_space_cachep);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008261 if (btrfs_delalloc_work_cachep)
8262 kmem_cache_destroy(btrfs_delalloc_work_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008263}
8264
8265int btrfs_init_cachep(void)
8266{
David Sterba837e1972012-09-07 03:00:48 -06008267 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008268 sizeof(struct btrfs_inode), 0,
8269 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04008270 if (!btrfs_inode_cachep)
8271 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008272
David Sterba837e1972012-09-07 03:00:48 -06008273 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008274 sizeof(struct btrfs_trans_handle), 0,
8275 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008276 if (!btrfs_trans_handle_cachep)
8277 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008278
David Sterba837e1972012-09-07 03:00:48 -06008279 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008280 sizeof(struct btrfs_transaction), 0,
8281 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008282 if (!btrfs_transaction_cachep)
8283 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008284
David Sterba837e1972012-09-07 03:00:48 -06008285 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008286 sizeof(struct btrfs_path), 0,
8287 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008288 if (!btrfs_path_cachep)
8289 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008290
David Sterba837e1972012-09-07 03:00:48 -06008291 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05008292 sizeof(struct btrfs_free_space), 0,
8293 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
8294 if (!btrfs_free_space_cachep)
8295 goto fail;
8296
Miao Xie8ccf6f192012-10-25 09:28:04 +00008297 btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
8298 sizeof(struct btrfs_delalloc_work), 0,
8299 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
8300 NULL);
8301 if (!btrfs_delalloc_work_cachep)
8302 goto fail;
8303
Chris Mason39279cc2007-06-12 06:35:45 -04008304 return 0;
8305fail:
8306 btrfs_destroy_cachep();
8307 return -ENOMEM;
8308}
8309
8310static int btrfs_getattr(struct vfsmount *mnt,
8311 struct dentry *dentry, struct kstat *stat)
8312{
Miao Xiedf0af1a2013-01-29 10:11:59 +00008313 u64 delalloc_bytes;
Chris Mason39279cc2007-06-12 06:35:45 -04008314 struct inode *inode = dentry->d_inode;
David Sterbafadc0d82011-11-20 07:33:38 -05008315 u32 blocksize = inode->i_sb->s_blocksize;
8316
Chris Mason39279cc2007-06-12 06:35:45 -04008317 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04008318 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05008319 stat->blksize = PAGE_CACHE_SIZE;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008320
8321 spin_lock(&BTRFS_I(inode)->lock);
8322 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
8323 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05008324 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00008325 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04008326 return 0;
8327}
8328
Chris Masond3977122009-01-05 21:25:51 -05008329static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
8330 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04008331{
8332 struct btrfs_trans_handle *trans;
8333 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008334 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04008335 struct inode *new_inode = new_dentry->d_inode;
8336 struct inode *old_inode = old_dentry->d_inode;
8337 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008338 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008339 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04008340 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08008341 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008342
Li Zefan33345d012011-04-20 10:31:50 +08008343 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04008344 return -EPERM;
8345
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008346 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08008347 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05008348 return -EXDEV;
8349
Li Zefan33345d012011-04-20 10:31:50 +08008350 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
8351 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008352 return -ENOTEMPTY;
8353
Chris Mason39279cc2007-06-12 06:35:45 -04008354 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008355 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04008356 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05008357
8358
8359 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04008360 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05008361 new_dentry->d_name.name,
8362 new_dentry->d_name.len);
8363
8364 if (ret) {
8365 if (ret == -EEXIST) {
8366 /* we shouldn't get
8367 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05308368 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05008369 return ret;
8370 }
8371 } else {
8372 /* maybe -EOVERFLOW */
8373 return ret;
8374 }
8375 }
8376 ret = 0;
8377
Chris Mason5a3f23d2009-03-31 13:27:11 -04008378 /*
Chris Mason8d875f92014-08-12 10:47:42 -07008379 * we're using rename to replace one file with another. Start IO on it
8380 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04008381 */
Chris Mason8d875f92014-08-12 10:47:42 -07008382 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04008383 filemap_flush(old_inode->i_mapping);
8384
Yan, Zheng76dda932009-09-21 16:00:26 -04008385 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08008386 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008387 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008388 /*
8389 * We want to reserve the absolute worst case amount of items. So if
8390 * both inodes are subvols and we need to unlink them then that would
8391 * require 4 item modifications, but if they are both normal inodes it
8392 * would require 5 item modifications, so we'll assume their normal
8393 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
8394 * should cover the worst case number of items we'll modify.
8395 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04008396 trans = btrfs_start_transaction(root, 11);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008397 if (IS_ERR(trans)) {
8398 ret = PTR_ERR(trans);
8399 goto out_notrans;
8400 }
Chris Mason5f39d392007-10-15 16:14:19 -04008401
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008402 if (dest != root)
8403 btrfs_record_root_in_trans(trans, dest);
8404
Yan, Zhenga5719522009-09-24 09:17:31 -04008405 ret = btrfs_set_inode_index(new_dir, &index);
8406 if (ret)
8407 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008408
Miao Xie67de1172013-12-26 13:07:06 +08008409 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08008410 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008411 /* force full log commit if subvolume involved. */
Miao Xie995946d2014-04-02 19:51:06 +08008412 btrfs_set_log_full_commit(root->fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008413 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04008414 ret = btrfs_insert_inode_ref(trans, dest,
8415 new_dentry->d_name.name,
8416 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08008417 old_ino,
8418 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04008419 if (ret)
8420 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008421 /*
8422 * this is an ugly little race, but the rename is required
8423 * to make sure that if we crash, the inode is either at the
8424 * old name or the new one. pinning the log transaction lets
8425 * us make sure we don't allow a log commit to come in after
8426 * we unlink the name but before we add the new name back in.
8427 */
8428 btrfs_pin_log_trans(root);
8429 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04008430
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008431 inode_inc_iversion(old_dir);
8432 inode_inc_iversion(new_dir);
8433 inode_inc_iversion(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008434 old_dir->i_ctime = old_dir->i_mtime = ctime;
8435 new_dir->i_ctime = new_dir->i_mtime = ctime;
8436 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04008437
Chris Mason12fcfd22009-03-24 10:24:20 -04008438 if (old_dentry->d_parent != new_dentry->d_parent)
8439 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
8440
Li Zefan33345d012011-04-20 10:31:50 +08008441 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008442 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
8443 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
8444 old_dentry->d_name.name,
8445 old_dentry->d_name.len);
8446 } else {
Al Viro92986792011-03-04 17:14:37 +00008447 ret = __btrfs_unlink_inode(trans, root, old_dir,
8448 old_dentry->d_inode,
8449 old_dentry->d_name.name,
8450 old_dentry->d_name.len);
8451 if (!ret)
8452 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008453 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008454 if (ret) {
8455 btrfs_abort_transaction(trans, root, ret);
8456 goto out_fail;
8457 }
Chris Mason39279cc2007-06-12 06:35:45 -04008458
8459 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008460 inode_inc_iversion(new_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008461 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08008462 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008463 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
8464 root_objectid = BTRFS_I(new_inode)->location.objectid;
8465 ret = btrfs_unlink_subvol(trans, dest, new_dir,
8466 root_objectid,
8467 new_dentry->d_name.name,
8468 new_dentry->d_name.len);
8469 BUG_ON(new_inode->i_nlink == 0);
8470 } else {
8471 ret = btrfs_unlink_inode(trans, dest, new_dir,
8472 new_dentry->d_inode,
8473 new_dentry->d_name.name,
8474 new_dentry->d_name.len);
8475 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04008476 if (!ret && new_inode->i_nlink == 0)
Chris Masone02119d2008-09-05 16:13:11 -04008477 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008478 if (ret) {
8479 btrfs_abort_transaction(trans, root, ret);
8480 goto out_fail;
8481 }
Chris Mason39279cc2007-06-12 06:35:45 -04008482 }
Josef Bacikaec74772008-07-24 12:12:38 -04008483
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008484 ret = btrfs_add_link(trans, new_dir, old_inode,
8485 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04008486 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008487 if (ret) {
8488 btrfs_abort_transaction(trans, root, ret);
8489 goto out_fail;
8490 }
Chris Mason39279cc2007-06-12 06:35:45 -04008491
Miao Xie67de1172013-12-26 13:07:06 +08008492 if (old_inode->i_nlink == 1)
8493 BTRFS_I(old_inode)->dir_index = index;
8494
Li Zefan33345d012011-04-20 10:31:50 +08008495 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04008496 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00008497 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008498 btrfs_end_log_trans(root);
8499 }
Chris Mason39279cc2007-06-12 06:35:45 -04008500out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008501 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008502out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08008503 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008504 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008505
Chris Mason39279cc2007-06-12 06:35:45 -04008506 return ret;
8507}
8508
Miao Xie8ccf6f192012-10-25 09:28:04 +00008509static void btrfs_run_delalloc_work(struct btrfs_work *work)
8510{
8511 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04008512 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008513
8514 delalloc_work = container_of(work, struct btrfs_delalloc_work,
8515 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04008516 inode = delalloc_work->inode;
8517 if (delalloc_work->wait) {
8518 btrfs_wait_ordered_range(inode, 0, (u64)-1);
8519 } else {
8520 filemap_flush(inode->i_mapping);
8521 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8522 &BTRFS_I(inode)->runtime_flags))
8523 filemap_flush(inode->i_mapping);
8524 }
Miao Xie8ccf6f192012-10-25 09:28:04 +00008525
8526 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -04008527 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008528 else
Josef Bacik9f23e282013-10-28 15:03:41 -04008529 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008530 complete(&delalloc_work->completion);
8531}
8532
8533struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
8534 int wait, int delay_iput)
8535{
8536 struct btrfs_delalloc_work *work;
8537
8538 work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
8539 if (!work)
8540 return NULL;
8541
8542 init_completion(&work->completion);
8543 INIT_LIST_HEAD(&work->list);
8544 work->inode = inode;
8545 work->wait = wait;
8546 work->delay_iput = delay_iput;
Liu Bo9e0af232014-08-15 23:36:53 +08008547 WARN_ON_ONCE(!inode);
8548 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
8549 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008550
8551 return work;
8552}
8553
8554void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
8555{
8556 wait_for_completion(&work->completion);
8557 kmem_cache_free(btrfs_delalloc_work_cachep, work);
8558}
8559
Chris Masond352ac62008-09-29 15:18:18 -04008560/*
8561 * some fairly slow code that needs optimization. This walks the list
8562 * of all the inodes with pending delalloc and forces them to disk.
8563 */
Miao Xie6c255e62014-03-06 13:55:01 +08008564static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
8565 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -04008566{
Chris Masonea8c2812008-08-04 23:17:27 -04008567 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008568 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008569 struct btrfs_delalloc_work *work, *next;
8570 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00008571 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008572 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04008573
Miao Xie8ccf6f192012-10-25 09:28:04 +00008574 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008575 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00008576
Miao Xie573bfb72014-03-06 13:55:03 +08008577 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00008578 spin_lock(&root->delalloc_lock);
8579 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008580 while (!list_empty(&splice)) {
8581 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04008582 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008583
Miao Xieeb73c1b2013-05-15 07:48:22 +00008584 list_move_tail(&binode->delalloc_inodes,
8585 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008586 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00008587 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00008588 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008589 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008590 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008591 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008592
8593 work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
8594 if (unlikely(!work)) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -04008595 if (delay_iput)
8596 btrfs_add_delayed_iput(inode);
8597 else
8598 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008599 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +08008600 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008601 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008602 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +08008603 btrfs_queue_work(root->fs_info->flush_workers,
8604 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +08008605 ret++;
8606 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +08008607 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008608 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00008609 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04008610 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008611 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04008612
Wang Shilonga1ecaab2014-04-02 19:53:32 +08008613out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00008614 list_for_each_entry_safe(work, next, &works, list) {
8615 list_del_init(&work->list);
8616 btrfs_wait_and_free_delalloc_work(work);
8617 }
8618
Miao Xieeb73c1b2013-05-15 07:48:22 +00008619 if (!list_empty_careful(&splice)) {
8620 spin_lock(&root->delalloc_lock);
8621 list_splice_tail(&splice, &root->delalloc_inodes);
8622 spin_unlock(&root->delalloc_lock);
8623 }
Miao Xie573bfb72014-03-06 13:55:03 +08008624 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00008625 return ret;
8626}
8627
8628int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
8629{
8630 int ret;
8631
Wang Shilong2c21b4d2014-01-14 19:42:20 +08008632 if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00008633 return -EROFS;
8634
Miao Xie6c255e62014-03-06 13:55:01 +08008635 ret = __start_delalloc_inodes(root, delay_iput, -1);
8636 if (ret > 0)
8637 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +00008638 /*
8639 * the filemap_flush will queue IO into the worker threads, but
Chris Mason8c8bee12008-09-29 11:19:10 -04008640 * we have to make sure the IO is actually started and that
8641 * ordered extents get created before we return
8642 */
8643 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05008644 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05008645 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04008646 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05008647 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
8648 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04008649 }
8650 atomic_dec(&root->fs_info->async_submit_draining);
Miao Xieeb73c1b2013-05-15 07:48:22 +00008651 return ret;
8652}
8653
Miao Xie6c255e62014-03-06 13:55:01 +08008654int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
8655 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +00008656{
8657 struct btrfs_root *root;
8658 struct list_head splice;
8659 int ret;
8660
Wang Shilong2c21b4d2014-01-14 19:42:20 +08008661 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00008662 return -EROFS;
8663
8664 INIT_LIST_HEAD(&splice);
8665
Miao Xie573bfb72014-03-06 13:55:03 +08008666 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00008667 spin_lock(&fs_info->delalloc_root_lock);
8668 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +08008669 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00008670 root = list_first_entry(&splice, struct btrfs_root,
8671 delalloc_root);
8672 root = btrfs_grab_fs_root(root);
8673 BUG_ON(!root);
8674 list_move_tail(&root->delalloc_root,
8675 &fs_info->delalloc_roots);
8676 spin_unlock(&fs_info->delalloc_root_lock);
8677
Miao Xie6c255e62014-03-06 13:55:01 +08008678 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +00008679 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +08008680 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +00008681 goto out;
8682
Miao Xie6c255e62014-03-06 13:55:01 +08008683 if (nr != -1) {
8684 nr -= ret;
8685 WARN_ON(nr < 0);
8686 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008687 spin_lock(&fs_info->delalloc_root_lock);
8688 }
8689 spin_unlock(&fs_info->delalloc_root_lock);
8690
Miao Xie6c255e62014-03-06 13:55:01 +08008691 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +00008692 atomic_inc(&fs_info->async_submit_draining);
8693 while (atomic_read(&fs_info->nr_async_submits) ||
8694 atomic_read(&fs_info->async_delalloc_pages)) {
8695 wait_event(fs_info->async_submit_wait,
8696 (atomic_read(&fs_info->nr_async_submits) == 0 &&
8697 atomic_read(&fs_info->async_delalloc_pages) == 0));
8698 }
8699 atomic_dec(&fs_info->async_submit_draining);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008700out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00008701 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00008702 spin_lock(&fs_info->delalloc_root_lock);
8703 list_splice_tail(&splice, &fs_info->delalloc_roots);
8704 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008705 }
Miao Xie573bfb72014-03-06 13:55:03 +08008706 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008707 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04008708}
8709
Chris Mason39279cc2007-06-12 06:35:45 -04008710static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
8711 const char *symname)
8712{
8713 struct btrfs_trans_handle *trans;
8714 struct btrfs_root *root = BTRFS_I(dir)->root;
8715 struct btrfs_path *path;
8716 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05008717 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04008718 int err;
8719 int drop_inode = 0;
8720 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05308721 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008722 int name_len;
8723 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04008724 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04008725 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04008726 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04008727
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01008728 name_len = strlen(symname);
Chris Mason39279cc2007-06-12 06:35:45 -04008729 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
8730 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05008731
Josef Bacik9ed74f22009-09-11 16:12:44 -04008732 /*
8733 * 2 items for inode item and ref
8734 * 2 items for dir items
8735 * 1 item for xattr if selinux is on
8736 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04008737 trans = btrfs_start_transaction(root, 5);
8738 if (IS_ERR(trans))
8739 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05008740
Li Zefan581bb052011-04-20 10:06:11 +08008741 err = btrfs_find_free_ino(root, &objectid);
8742 if (err)
8743 goto out_unlock;
8744
Josef Bacikaec74772008-07-24 12:12:38 -04008745 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08008746 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04008747 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008748 if (IS_ERR(inode)) {
8749 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008750 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008751 }
Chris Mason39279cc2007-06-12 06:35:45 -04008752
Eric Paris2a7dba32011-02-01 11:05:39 -05008753 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04008754 if (err) {
8755 drop_inode = 1;
8756 goto out_unlock;
8757 }
8758
Casey Schauflerad19db72011-12-15 10:09:07 -05008759 /*
8760 * If the active LSM wants to access the inode during
8761 * d_instantiate it needs these. Smack checks to see
8762 * if the filesystem supports xattrs by looking at the
8763 * ops vector.
8764 */
8765 inode->i_fop = &btrfs_file_operations;
8766 inode->i_op = &btrfs_file_inode_operations;
8767
Josef Bacika1b075d2010-11-19 20:36:11 +00008768 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04008769 if (err)
8770 drop_inode = 1;
8771 else {
8772 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04008773 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05008774 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04008775 }
Chris Mason39279cc2007-06-12 06:35:45 -04008776 if (drop_inode)
8777 goto out_unlock;
8778
8779 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07008780 if (!path) {
8781 err = -ENOMEM;
8782 drop_inode = 1;
8783 goto out_unlock;
8784 }
Li Zefan33345d012011-04-20 10:31:50 +08008785 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008786 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008787 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
8788 datasize = btrfs_file_extent_calc_inline_size(name_len);
8789 err = btrfs_insert_empty_item(trans, root, path, &key,
8790 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04008791 if (err) {
8792 drop_inode = 1;
Julia Lawallb0839162011-05-14 07:10:51 +00008793 btrfs_free_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04008794 goto out_unlock;
8795 }
Chris Mason5f39d392007-10-15 16:14:19 -04008796 leaf = path->nodes[0];
8797 ei = btrfs_item_ptr(leaf, path->slots[0],
8798 struct btrfs_file_extent_item);
8799 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
8800 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04008801 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04008802 btrfs_set_file_extent_encryption(leaf, ei, 0);
8803 btrfs_set_file_extent_compression(leaf, ei, 0);
8804 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
8805 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
8806
Chris Mason39279cc2007-06-12 06:35:45 -04008807 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04008808 write_extent_buffer(leaf, symname, ptr, name_len);
8809 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04008810 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04008811
Chris Mason39279cc2007-06-12 06:35:45 -04008812 inode->i_op = &btrfs_symlink_inode_operations;
8813 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04008814 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04008815 inode_set_bytes(inode, name_len);
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01008816 btrfs_i_size_write(inode, name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -04008817 err = btrfs_update_inode(trans, root, inode);
8818 if (err)
8819 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04008820
8821out_unlock:
Al Viro08c422c2011-12-23 07:58:13 -05008822 if (!err)
8823 d_instantiate(dentry, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008824 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04008825 if (drop_inode) {
8826 inode_dec_link_count(inode);
8827 iput(inode);
8828 }
Liu Bob53d3f52012-11-14 14:34:34 +00008829 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04008830 return err;
8831}
Chris Mason16432982008-04-10 10:23:21 -04008832
Josef Bacik0af3d002010-06-21 14:48:16 -04008833static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
8834 u64 start, u64 num_bytes, u64 min_size,
8835 loff_t actual_len, u64 *alloc_hint,
8836 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04008837{
Josef Bacik5dc562c2012-08-17 13:14:17 -04008838 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
8839 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04008840 struct btrfs_root *root = BTRFS_I(inode)->root;
8841 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04008842 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00008843 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05008844 u64 cur_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04008845 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04008846 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04008847
Josef Bacik0af3d002010-06-21 14:48:16 -04008848 if (trans)
8849 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04008850 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008851 if (own_trans) {
8852 trans = btrfs_start_transaction(root, 3);
8853 if (IS_ERR(trans)) {
8854 ret = PTR_ERR(trans);
8855 break;
8856 }
Yan Zhengd899e052008-10-30 14:25:28 -04008857 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00008858
Chris Mason154ea282013-03-05 11:11:26 -05008859 cur_bytes = min(num_bytes, 256ULL * 1024 * 1024);
8860 cur_bytes = max(cur_bytes, min_size);
Josef Bacik00361582013-08-14 14:02:47 -04008861 ret = btrfs_reserve_extent(root, cur_bytes, min_size, 0,
Miao Xiee570fd22014-06-19 10:42:50 +08008862 *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008863 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008864 if (own_trans)
8865 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008866 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00008867 }
8868
Yan Zhengd899e052008-10-30 14:25:28 -04008869 ret = insert_reserved_file_extent(trans, inode,
8870 cur_offset, ins.objectid,
8871 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00008872 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04008873 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008874 if (ret) {
Josef Bacik857cc2f2013-10-07 15:21:08 -04008875 btrfs_free_reserved_extent(root, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +08008876 ins.offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008877 btrfs_abort_transaction(trans, root, ret);
8878 if (own_trans)
8879 btrfs_end_transaction(trans, root);
8880 break;
8881 }
Chris Masona1ed8352009-09-11 12:27:37 -04008882 btrfs_drop_extent_cache(inode, cur_offset,
8883 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008884
Josef Bacik5dc562c2012-08-17 13:14:17 -04008885 em = alloc_extent_map();
8886 if (!em) {
8887 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
8888 &BTRFS_I(inode)->runtime_flags);
8889 goto next;
8890 }
8891
8892 em->start = cur_offset;
8893 em->orig_start = cur_offset;
8894 em->len = ins.offset;
8895 em->block_start = ins.objectid;
8896 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05008897 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04008898 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008899 em->bdev = root->fs_info->fs_devices->latest_bdev;
8900 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
8901 em->generation = trans->transid;
8902
8903 while (1) {
8904 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04008905 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04008906 write_unlock(&em_tree->lock);
8907 if (ret != -EEXIST)
8908 break;
8909 btrfs_drop_extent_cache(inode, cur_offset,
8910 cur_offset + ins.offset - 1,
8911 0);
8912 }
8913 free_extent_map(em);
8914next:
Yan Zhengd899e052008-10-30 14:25:28 -04008915 num_bytes -= ins.offset;
8916 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04008917 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00008918
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008919 inode_inc_iversion(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04008920 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02008921 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04008922 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04008923 (actual_len > inode->i_size) &&
8924 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00008925 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00008926 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00008927 else
Josef Bacik55a61d12010-11-22 18:50:32 +00008928 i_size = cur_offset;
8929 i_size_write(inode, i_size);
8930 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008931 }
8932
Yan Zhengd899e052008-10-30 14:25:28 -04008933 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008934
8935 if (ret) {
8936 btrfs_abort_transaction(trans, root, ret);
8937 if (own_trans)
8938 btrfs_end_transaction(trans, root);
8939 break;
8940 }
Yan Zhengd899e052008-10-30 14:25:28 -04008941
Josef Bacik0af3d002010-06-21 14:48:16 -04008942 if (own_trans)
8943 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008944 }
Yan Zhengd899e052008-10-30 14:25:28 -04008945 return ret;
8946}
8947
Josef Bacik0af3d002010-06-21 14:48:16 -04008948int btrfs_prealloc_file_range(struct inode *inode, int mode,
8949 u64 start, u64 num_bytes, u64 min_size,
8950 loff_t actual_len, u64 *alloc_hint)
8951{
8952 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
8953 min_size, actual_len, alloc_hint,
8954 NULL);
8955}
8956
8957int btrfs_prealloc_file_range_trans(struct inode *inode,
8958 struct btrfs_trans_handle *trans, int mode,
8959 u64 start, u64 num_bytes, u64 min_size,
8960 loff_t actual_len, u64 *alloc_hint)
8961{
8962 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
8963 min_size, actual_len, alloc_hint, trans);
8964}
8965
Chris Masone6dcd2d2008-07-17 12:53:50 -04008966static int btrfs_set_page_dirty(struct page *page)
8967{
Chris Masone6dcd2d2008-07-17 12:53:50 -04008968 return __set_page_dirty_nobuffers(page);
8969}
8970
Al Viro10556cb22011-06-20 19:28:19 -04008971static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05008972{
Li Zefanb83cc962010-12-20 16:04:08 +08008973 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00008974 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08008975
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00008976 if (mask & MAY_WRITE &&
8977 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
8978 if (btrfs_root_readonly(root))
8979 return -EROFS;
8980 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
8981 return -EACCES;
8982 }
Al Viro2830ba72011-06-20 19:16:29 -04008983 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05008984}
Chris Mason39279cc2007-06-12 06:35:45 -04008985
Filipe Mananaef3b9af2014-04-27 20:40:45 +01008986static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
8987{
8988 struct btrfs_trans_handle *trans;
8989 struct btrfs_root *root = BTRFS_I(dir)->root;
8990 struct inode *inode = NULL;
8991 u64 objectid;
8992 u64 index;
8993 int ret = 0;
8994
8995 /*
8996 * 5 units required for adding orphan entry
8997 */
8998 trans = btrfs_start_transaction(root, 5);
8999 if (IS_ERR(trans))
9000 return PTR_ERR(trans);
9001
9002 ret = btrfs_find_free_ino(root, &objectid);
9003 if (ret)
9004 goto out;
9005
9006 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
9007 btrfs_ino(dir), objectid, mode, &index);
9008 if (IS_ERR(inode)) {
9009 ret = PTR_ERR(inode);
9010 inode = NULL;
9011 goto out;
9012 }
9013
9014 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
9015 if (ret)
9016 goto out;
9017
9018 ret = btrfs_update_inode(trans, root, inode);
9019 if (ret)
9020 goto out;
9021
9022 inode->i_fop = &btrfs_file_operations;
9023 inode->i_op = &btrfs_file_inode_operations;
9024
9025 inode->i_mapping->a_ops = &btrfs_aops;
9026 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
9027 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
9028
9029 ret = btrfs_orphan_add(trans, inode);
9030 if (ret)
9031 goto out;
9032
Filipe Manana5762b5c2014-08-01 00:10:32 +01009033 /*
9034 * We set number of links to 0 in btrfs_new_inode(), and here we set
9035 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
9036 * through:
9037 *
9038 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
9039 */
9040 set_nlink(inode, 1);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009041 d_tmpfile(dentry, inode);
9042 mark_inode_dirty(inode);
9043
9044out:
9045 btrfs_end_transaction(trans, root);
9046 if (ret)
9047 iput(inode);
9048 btrfs_balance_delayed_items(root);
9049 btrfs_btree_balance_dirty(root);
9050
9051 return ret;
9052}
9053
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009054static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05009055 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04009056 .lookup = btrfs_lookup,
9057 .create = btrfs_create,
9058 .unlink = btrfs_unlink,
9059 .link = btrfs_link,
9060 .mkdir = btrfs_mkdir,
9061 .rmdir = btrfs_rmdir,
9062 .rename = btrfs_rename,
9063 .symlink = btrfs_symlink,
9064 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04009065 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009066 .setxattr = btrfs_setxattr,
9067 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05009068 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009069 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05009070 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009071 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009072 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -07009073 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009074 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -04009075};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009076static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009077 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05009078 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009079 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009080 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -07009081 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04009082};
Yan, Zheng76dda932009-09-21 16:00:26 -04009083
Alexey Dobriyan828c0952009-10-01 15:43:56 -07009084static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009085 .llseek = generic_file_llseek,
9086 .read = generic_read_dir,
Al Viro9cdda8d2013-05-22 16:48:09 -04009087 .iterate = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04009088 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04009089#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04009090 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04009091#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04009092 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04009093 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04009094};
9095
Chris Masond1310b22008-01-24 16:13:08 -05009096static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04009097 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05009098 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04009099 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04009100 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04009101 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04009102 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05009103 .set_bit_hook = btrfs_set_bit_hook,
9104 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04009105 .merge_extent_hook = btrfs_merge_extent_hook,
9106 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04009107};
9108
Chris Mason35054392009-01-21 13:11:13 -05009109/*
9110 * btrfs doesn't support the bmap operation because swapfiles
9111 * use bmap to make a mapping of extents in the file. They assume
9112 * these extents won't change over the life of the file and they
9113 * use the bmap result to do IO directly to the drive.
9114 *
9115 * the btrfs bmap call would return logical addresses that aren't
9116 * suitable for IO and they also will change frequently as COW
9117 * operations happen. So, swapfile + btrfs == corruption.
9118 *
9119 * For now we're avoiding this by dropping bmap.
9120 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07009121static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04009122 .readpage = btrfs_readpage,
9123 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -04009124 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05009125 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04009126 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04009127 .invalidatepage = btrfs_invalidatepage,
9128 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04009129 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02009130 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04009131};
9132
Alexey Dobriyan7f094102009-09-21 17:01:10 -07009133static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04009134 .readpage = btrfs_readpage,
9135 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04009136 .invalidatepage = btrfs_invalidatepage,
9137 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04009138};
9139
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009140static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009141 .getattr = btrfs_getattr,
9142 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009143 .setxattr = btrfs_setxattr,
9144 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05009145 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009146 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05009147 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05009148 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009149 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009150 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04009151 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04009152};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009153static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04009154 .getattr = btrfs_getattr,
9155 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05009156 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009157 .setxattr = btrfs_setxattr,
9158 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04009159 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009160 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009161 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009162 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04009163 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -04009164};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009165static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009166 .readlink = generic_readlink,
9167 .follow_link = page_follow_link_light,
9168 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00009169 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -05009170 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05009171 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05009172 .setxattr = btrfs_setxattr,
9173 .getxattr = btrfs_getxattr,
9174 .listxattr = btrfs_listxattr,
9175 .removexattr = btrfs_removexattr,
Josef Bacike41f9412012-03-26 09:46:47 -04009176 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04009177};
Yan, Zheng76dda932009-09-21 16:00:26 -04009178
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04009179const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04009180 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -04009181 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -04009182};