blob: 8dba152883d300fa1c0d6092c9700757aa1df4aa [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 Masond3977122009-01-05 21:25:51 -0500128static noinline 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
Chris Mason42dc7ba2008-12-15 11:44:56 -0500397 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400398again:
399 will_compress = 0;
400 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
401 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
402
Chris Masonf03d9302009-02-04 09:31:06 -0500403 /*
404 * we don't want to send crud past the end of i_size through
405 * compression, that's just a waste of CPU time. So, if the
406 * end of the file is before the start of our current
407 * requested range of bytes, we bail out to the uncompressed
408 * cleanup code that can deal with all of this.
409 *
410 * It isn't really the fastest way to fix things, but this is a
411 * very uncommon corner.
412 */
413 if (actual_end <= start)
414 goto cleanup_and_bail_uncompressed;
415
Chris Masonc8b97812008-10-29 14:49:59 -0400416 total_compressed = actual_end - start;
417
418 /* we want to make sure that amount of ram required to uncompress
419 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500420 * of a compressed extent to 128k. This is a crucial number
421 * because it also controls how easily we can spread reads across
422 * cpus for decompression.
423 *
424 * We also want to make sure the amount of IO required to do
425 * a random read is reasonably small, so we limit the size of
426 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400427 */
428 total_compressed = min(total_compressed, max_uncompressed);
Qu Wenruofda28322013-02-26 08:10:22 +0000429 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Masonbe20aa92007-12-17 20:14:01 -0500430 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400431 total_in = 0;
432 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400433
Chris Mason771ed682008-11-06 22:02:51 -0500434 /*
435 * we do compression for mount -o compress and when the
436 * inode has not been flagged as nocompress. This flag can
437 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400438 */
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200439 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) &&
Chris Mason1e701a32010-03-11 09:42:04 -0500440 (btrfs_test_opt(root, COMPRESS) ||
Liu Bo75e7cb72011-03-22 10:12:20 +0000441 (BTRFS_I(inode)->force_compress) ||
442 (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS))) {
Chris Masonc8b97812008-10-29 14:49:59 -0400443 WARN_ON(pages);
Chris Masoncfbc2462008-10-30 13:22:14 -0400444 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800445 if (!pages) {
446 /* just bail out to the uncompressed code */
447 goto cont;
448 }
Chris Mason179e29e2007-11-01 11:28:41 -0400449
Li Zefan261507a02010-12-17 14:21:50 +0800450 if (BTRFS_I(inode)->force_compress)
451 compress_type = BTRFS_I(inode)->force_compress;
452
Chris Mason4adaa612013-03-26 13:07:00 -0400453 /*
454 * we need to call clear_page_dirty_for_io on each
455 * page in the range. Otherwise applications with the file
456 * mmap'd can wander in and change the page contents while
457 * we are compressing them.
458 *
459 * If the compression fails for any reason, we set the pages
460 * dirty again later on.
461 */
462 extent_range_clear_dirty_for_io(inode, start, end);
463 redirty = 1;
Li Zefan261507a02010-12-17 14:21:50 +0800464 ret = btrfs_compress_pages(compress_type,
465 inode->i_mapping, start,
466 total_compressed, pages,
467 nr_pages, &nr_pages_ret,
468 &total_in,
469 &total_compressed,
470 max_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400471
472 if (!ret) {
473 unsigned long offset = total_compressed &
474 (PAGE_CACHE_SIZE - 1);
475 struct page *page = pages[nr_pages_ret - 1];
476 char *kaddr;
477
478 /* zero the tail end of the last page, we might be
479 * sending it down to disk
480 */
481 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800482 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400483 memset(kaddr + offset, 0,
484 PAGE_CACHE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800485 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400486 }
487 will_compress = 1;
488 }
489 }
Li Zefan560f7d72011-09-08 10:22:01 +0800490cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400491 if (start == 0) {
492 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500493 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400494 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500495 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400496 */
Josef Bacik00361582013-08-14 14:02:47 -0400497 ret = cow_file_range_inline(root, inode, start, end,
498 0, 0, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400499 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500500 /* try making a compressed inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400501 ret = cow_file_range_inline(root, inode, start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000502 total_compressed,
503 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400504 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100505 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400506 unsigned long clear_flags = EXTENT_DELALLOC |
507 EXTENT_DEFRAG;
508 clear_flags |= (ret < 0) ? EXTENT_DO_ACCOUNTING : 0;
509
Chris Mason771ed682008-11-06 22:02:51 -0500510 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100511 * inline extent creation worked or returned error,
512 * we don't need to create any more async work items.
513 * Unlock and free up our temp pages.
Chris Mason771ed682008-11-06 22:02:51 -0500514 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400515 extent_clear_unlock_delalloc(inode, start, end, NULL,
Josef Bacik151a41b2013-07-29 13:22:24 -0400516 clear_flags, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400517 PAGE_CLEAR_DIRTY |
518 PAGE_SET_WRITEBACK |
519 PAGE_END_WRITEBACK);
Chris Masonc8b97812008-10-29 14:49:59 -0400520 goto free_pages_out;
521 }
522 }
523
524 if (will_compress) {
525 /*
526 * we aren't doing an inline extent round the compressed size
527 * up to a block size boundary so the allocator does sane
528 * things
529 */
Qu Wenruofda28322013-02-26 08:10:22 +0000530 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400531
532 /*
533 * one last check to make sure the compression is really a
534 * win, compare the page count read with the blocks on disk
535 */
Qu Wenruofda28322013-02-26 08:10:22 +0000536 total_in = ALIGN(total_in, PAGE_CACHE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400537 if (total_compressed >= total_in) {
538 will_compress = 0;
539 } else {
Chris Masonc8b97812008-10-29 14:49:59 -0400540 num_bytes = total_in;
541 }
542 }
543 if (!will_compress && pages) {
544 /*
545 * the compression code ran but failed to make things smaller,
546 * free any pages it allocated and our page pointer array
547 */
548 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400549 WARN_ON(pages[i]->mapping);
Chris Masonc8b97812008-10-29 14:49:59 -0400550 page_cache_release(pages[i]);
551 }
552 kfree(pages);
553 pages = NULL;
554 total_compressed = 0;
555 nr_pages_ret = 0;
556
557 /* flag the file so we don't compress in the future */
Chris Mason1e701a32010-03-11 09:42:04 -0500558 if (!btrfs_test_opt(root, FORCE_COMPRESS) &&
559 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500560 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500561 }
Chris Masonc8b97812008-10-29 14:49:59 -0400562 }
Chris Mason771ed682008-11-06 22:02:51 -0500563 if (will_compress) {
564 *num_added += 1;
565
566 /* the async work queues will take care of doing actual
567 * allocation on disk for these compressed pages,
568 * and will submit them to the elevator.
569 */
570 add_async_extent(async_cow, start, num_bytes,
Li Zefan261507a02010-12-17 14:21:50 +0800571 total_compressed, pages, nr_pages_ret,
572 compress_type);
Chris Mason771ed682008-11-06 22:02:51 -0500573
Yan, Zheng24ae6362010-12-06 07:02:36 +0000574 if (start + num_bytes < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500575 start += num_bytes;
576 pages = NULL;
577 cond_resched();
578 goto again;
579 }
580 } else {
Chris Masonf03d9302009-02-04 09:31:06 -0500581cleanup_and_bail_uncompressed:
Chris Mason771ed682008-11-06 22:02:51 -0500582 /*
583 * No compression, but we still need to write the pages in
584 * the file we've been given so far. redirty the locked
585 * page if it corresponds to our extent and set things up
586 * for the async work queue to run cow_file_range to do
587 * the normal delalloc dance
588 */
589 if (page_offset(locked_page) >= start &&
590 page_offset(locked_page) <= end) {
591 __set_page_dirty_nobuffers(locked_page);
592 /* unlocked later on in the async handlers */
593 }
Chris Mason4adaa612013-03-26 13:07:00 -0400594 if (redirty)
595 extent_range_redirty_for_io(inode, start, end);
Li Zefan261507a02010-12-17 14:21:50 +0800596 add_async_extent(async_cow, start, end - start + 1,
597 0, NULL, 0, BTRFS_COMPRESS_NONE);
Chris Mason771ed682008-11-06 22:02:51 -0500598 *num_added += 1;
599 }
600
601out:
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100602 return ret;
Chris Mason771ed682008-11-06 22:02:51 -0500603
604free_pages_out:
605 for (i = 0; i < nr_pages_ret; i++) {
606 WARN_ON(pages[i]->mapping);
607 page_cache_release(pages[i]);
608 }
Chris Masond3977122009-01-05 21:25:51 -0500609 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500610
611 goto out;
612}
613
614/*
615 * phase two of compressed writeback. This is the ordered portion
616 * of the code, which only gets called in the order the work was
617 * queued. We walk all the async extents created by compress_file_range
618 * and send them down to the disk.
619 */
620static noinline int submit_compressed_extents(struct inode *inode,
621 struct async_cow *async_cow)
622{
623 struct async_extent *async_extent;
624 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500625 struct btrfs_key ins;
626 struct extent_map *em;
627 struct btrfs_root *root = BTRFS_I(inode)->root;
628 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
629 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500630 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500631
632 if (list_empty(&async_cow->extents))
633 return 0;
634
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500635again:
Chris Masond3977122009-01-05 21:25:51 -0500636 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500637 async_extent = list_entry(async_cow->extents.next,
638 struct async_extent, list);
639 list_del(&async_extent->list);
640
641 io_tree = &BTRFS_I(inode)->io_tree;
642
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500643retry:
Chris Mason771ed682008-11-06 22:02:51 -0500644 /* did the compression code fall back to uncompressed IO? */
645 if (!async_extent->pages) {
646 int page_started = 0;
647 unsigned long nr_written = 0;
648
649 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000650 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100651 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500652
653 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500654 ret = cow_file_range(inode, async_cow->locked_page,
655 async_extent->start,
656 async_extent->start +
657 async_extent->ram_size - 1,
658 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500659
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100660 /* JDM XXX */
661
Chris Mason771ed682008-11-06 22:02:51 -0500662 /*
663 * if page_started, cow_file_range inserted an
664 * inline extent and took care of all the unlocking
665 * and IO for us. Otherwise, we need to submit
666 * all those pages down to the drive.
667 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500668 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500669 extent_write_locked_range(io_tree,
670 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500671 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500672 async_extent->ram_size - 1,
673 btrfs_get_extent,
674 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500675 else if (ret)
676 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500677 kfree(async_extent);
678 cond_resched();
679 continue;
680 }
681
682 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100683 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500684
Josef Bacik00361582013-08-14 14:02:47 -0400685 ret = btrfs_reserve_extent(root,
Chris Mason771ed682008-11-06 22:02:51 -0500686 async_extent->compressed_size,
687 async_extent->compressed_size,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500688 0, alloc_hint, &ins, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500689 if (ret) {
690 int i;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500691
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500692 for (i = 0; i < async_extent->nr_pages; i++) {
693 WARN_ON(async_extent->pages[i]->mapping);
694 page_cache_release(async_extent->pages[i]);
695 }
696 kfree(async_extent->pages);
697 async_extent->nr_pages = 0;
698 async_extent->pages = NULL;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500699
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400700 if (ret == -ENOSPC) {
701 unlock_extent(io_tree, async_extent->start,
702 async_extent->start +
703 async_extent->ram_size - 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100704 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400705 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500706 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500707 }
708
Yan, Zhengc2167752009-11-12 09:34:21 +0000709 /*
710 * here we're doing allocation and writeback of the
711 * compressed pages
712 */
713 btrfs_drop_extent_cache(inode, async_extent->start,
714 async_extent->start +
715 async_extent->ram_size - 1, 0);
716
David Sterba172ddd62011-04-21 00:48:27 +0200717 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000718 if (!em) {
719 ret = -ENOMEM;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500720 goto out_free_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000721 }
Chris Mason771ed682008-11-06 22:02:51 -0500722 em->start = async_extent->start;
723 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500724 em->orig_start = em->start;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400725 em->mod_start = em->start;
726 em->mod_len = em->len;
Chris Mason771ed682008-11-06 22:02:51 -0500727
728 em->block_start = ins.objectid;
729 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500730 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400731 em->ram_bytes = async_extent->ram_size;
Chris Mason771ed682008-11-06 22:02:51 -0500732 em->bdev = root->fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800733 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500734 set_bit(EXTENT_FLAG_PINNED, &em->flags);
735 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400736 em->generation = -1;
Chris Mason771ed682008-11-06 22:02:51 -0500737
Chris Masond3977122009-01-05 21:25:51 -0500738 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400739 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400740 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400741 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500742 if (ret != -EEXIST) {
743 free_extent_map(em);
744 break;
745 }
746 btrfs_drop_extent_cache(inode, async_extent->start,
747 async_extent->start +
748 async_extent->ram_size - 1, 0);
749 }
750
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500751 if (ret)
752 goto out_free_reserve;
753
Li Zefan261507a02010-12-17 14:21:50 +0800754 ret = btrfs_add_ordered_extent_compress(inode,
755 async_extent->start,
756 ins.objectid,
757 async_extent->ram_size,
758 ins.offset,
759 BTRFS_ORDERED_COMPRESSED,
760 async_extent->compress_type);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500761 if (ret)
762 goto out_free_reserve;
Chris Mason771ed682008-11-06 22:02:51 -0500763
Chris Mason771ed682008-11-06 22:02:51 -0500764 /*
765 * clear dirty, set writeback and unlock the pages.
766 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400767 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400768 async_extent->start +
769 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400770 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
771 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400772 PAGE_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500773 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500774 async_extent->start,
775 async_extent->ram_size,
776 ins.objectid,
777 ins.offset, async_extent->pages,
778 async_extent->nr_pages);
Chris Mason771ed682008-11-06 22:02:51 -0500779 alloc_hint = ins.objectid + ins.offset;
780 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500781 if (ret)
782 goto out;
Chris Mason771ed682008-11-06 22:02:51 -0500783 cond_resched();
784 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100785 ret = 0;
786out:
787 return ret;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500788out_free_reserve:
789 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100790out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400791 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500792 async_extent->start +
793 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400794 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400795 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
796 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
797 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100798 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500799 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500800}
801
Josef Bacik4b46fce2010-05-23 11:00:55 -0400802static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
803 u64 num_bytes)
804{
805 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
806 struct extent_map *em;
807 u64 alloc_hint = 0;
808
809 read_lock(&em_tree->lock);
810 em = search_extent_mapping(em_tree, start, num_bytes);
811 if (em) {
812 /*
813 * if block start isn't an actual block number then find the
814 * first block in this inode and use that as a hint. If that
815 * block is also bogus then just don't worry about it.
816 */
817 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
818 free_extent_map(em);
819 em = search_extent_mapping(em_tree, 0, 0);
820 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
821 alloc_hint = em->block_start;
822 if (em)
823 free_extent_map(em);
824 } else {
825 alloc_hint = em->block_start;
826 free_extent_map(em);
827 }
828 }
829 read_unlock(&em_tree->lock);
830
831 return alloc_hint;
832}
833
Chris Mason771ed682008-11-06 22:02:51 -0500834/*
835 * when extent_io.c finds a delayed allocation range in the file,
836 * the call backs end up in this code. The basic idea is to
837 * allocate extents on disk for the range, and create ordered data structs
838 * in ram to track those extents.
839 *
840 * locked_page is the page that writepage had locked already. We use
841 * it to make sure we don't do extra locks or unlocks.
842 *
843 * *page_started is set to one if we unlock locked_page and do everything
844 * required to start IO on it. It may be clean and already done with
845 * IO when we return.
846 */
Josef Bacik00361582013-08-14 14:02:47 -0400847static noinline int cow_file_range(struct inode *inode,
848 struct page *locked_page,
849 u64 start, u64 end, int *page_started,
850 unsigned long *nr_written,
851 int unlock)
Chris Mason771ed682008-11-06 22:02:51 -0500852{
Josef Bacik00361582013-08-14 14:02:47 -0400853 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500854 u64 alloc_hint = 0;
855 u64 num_bytes;
856 unsigned long ram_size;
857 u64 disk_num_bytes;
858 u64 cur_alloc_size;
859 u64 blocksize = root->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500860 struct btrfs_key ins;
861 struct extent_map *em;
862 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
863 int ret = 0;
864
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400865 if (btrfs_is_free_space_inode(inode)) {
866 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500867 ret = -EINVAL;
868 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400869 }
Chris Mason771ed682008-11-06 22:02:51 -0500870
Qu Wenruofda28322013-02-26 08:10:22 +0000871 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500872 num_bytes = max(blocksize, num_bytes);
873 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500874
Chris Mason4cb53002011-05-24 15:35:30 -0400875 /* if this is a small write inside eof, kick off defrag */
Liu Bo4cb13e52012-03-29 09:57:45 -0400876 if (num_bytes < 64 * 1024 &&
877 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Josef Bacik00361582013-08-14 14:02:47 -0400878 btrfs_add_inode_defrag(NULL, inode);
Chris Mason4cb53002011-05-24 15:35:30 -0400879
Chris Mason771ed682008-11-06 22:02:51 -0500880 if (start == 0) {
881 /* lets try to make an inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400882 ret = cow_file_range_inline(root, inode, start, end, 0, 0,
883 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500884 if (ret == 0) {
Josef Bacikc2790a22013-07-29 11:20:47 -0400885 extent_clear_unlock_delalloc(inode, start, end, NULL,
886 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400887 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400888 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
889 PAGE_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000890
Chris Mason771ed682008-11-06 22:02:51 -0500891 *nr_written = *nr_written +
892 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
893 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500894 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100895 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100896 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500897 }
898 }
Chris Masonc8b97812008-10-29 14:49:59 -0400899
900 BUG_ON(disk_num_bytes >
David Sterba6c417612011-04-13 15:41:04 +0200901 btrfs_super_total_bytes(root->fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400902
Josef Bacik4b46fce2010-05-23 11:00:55 -0400903 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400904 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400905
Chris Masond3977122009-01-05 21:25:51 -0500906 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400907 unsigned long op;
908
Josef Bacik287a0ab2010-03-19 18:07:23 +0000909 cur_alloc_size = disk_num_bytes;
Josef Bacik00361582013-08-14 14:02:47 -0400910 ret = btrfs_reserve_extent(root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500911 root->sectorsize, 0, alloc_hint,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500912 &ins, 1);
Josef Bacik00361582013-08-14 14:02:47 -0400913 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100914 goto out_unlock;
Chris Masond3977122009-01-05 21:25:51 -0500915
David Sterba172ddd62011-04-21 00:48:27 +0200916 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000917 if (!em) {
918 ret = -ENOMEM;
Liu Boace68ba2013-04-22 10:53:47 +0000919 goto out_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000920 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400921 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500922 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500923 ram_size = ins.offset;
924 em->len = ins.offset;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400925 em->mod_start = em->start;
926 em->mod_len = em->len;
Chris Masonc8b97812008-10-29 14:49:59 -0400927
Chris Masone6dcd2d2008-07-17 12:53:50 -0400928 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400929 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500930 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400931 em->ram_bytes = ram_size;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400932 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400933 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400934 em->generation = -1;
Chris Masonc8b97812008-10-29 14:49:59 -0400935
Chris Masond3977122009-01-05 21:25:51 -0500936 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400937 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400938 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400939 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400940 if (ret != -EEXIST) {
941 free_extent_map(em);
942 break;
943 }
944 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400945 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400946 }
Liu Boace68ba2013-04-22 10:53:47 +0000947 if (ret)
948 goto out_reserve;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400949
Chris Mason98d20f62008-04-14 09:46:10 -0400950 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400951 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -0500952 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +0000953 if (ret)
954 goto out_reserve;
Chris Masonc8b97812008-10-29 14:49:59 -0400955
Yan Zheng17d217f2008-12-12 10:03:38 -0500956 if (root->root_key.objectid ==
957 BTRFS_DATA_RELOC_TREE_OBJECTID) {
958 ret = btrfs_reloc_clone_csums(inode, start,
959 cur_alloc_size);
Josef Bacik00361582013-08-14 14:02:47 -0400960 if (ret)
Liu Boace68ba2013-04-22 10:53:47 +0000961 goto out_reserve;
Yan Zheng17d217f2008-12-12 10:03:38 -0500962 }
963
Chris Masond3977122009-01-05 21:25:51 -0500964 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -0400965 break;
Chris Masond3977122009-01-05 21:25:51 -0500966
Chris Masonc8b97812008-10-29 14:49:59 -0400967 /* we're not doing compressed IO, don't unlock the first
968 * page (which the caller expects to stay locked), don't
969 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -0400970 *
971 * Do set the Private2 bit so we know this page was properly
972 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -0400973 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400974 op = unlock ? PAGE_UNLOCK : 0;
975 op |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -0400976
Josef Bacikc2790a22013-07-29 11:20:47 -0400977 extent_clear_unlock_delalloc(inode, start,
978 start + ram_size - 1, locked_page,
979 EXTENT_LOCKED | EXTENT_DELALLOC,
980 op);
Chris Masonc8b97812008-10-29 14:49:59 -0400981 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -0500982 num_bytes -= cur_alloc_size;
983 alloc_hint = ins.objectid + ins.offset;
984 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400985 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100986out:
Chris Masonbe20aa92007-12-17 20:14:01 -0500987 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +0000988
Liu Boace68ba2013-04-22 10:53:47 +0000989out_reserve:
990 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100991out_unlock:
Josef Bacikc2790a22013-07-29 11:20:47 -0400992 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Josef Bacik151a41b2013-07-29 13:22:24 -0400993 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
994 EXTENT_DELALLOC | EXTENT_DEFRAG,
995 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
996 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100997 goto out;
Chris Mason771ed682008-11-06 22:02:51 -0500998}
Chris Masonc8b97812008-10-29 14:49:59 -0400999
Chris Mason771ed682008-11-06 22:02:51 -05001000/*
1001 * work queue call back to started compression on a file and pages
1002 */
1003static noinline void async_cow_start(struct btrfs_work *work)
1004{
1005 struct async_cow *async_cow;
1006 int num_added = 0;
1007 async_cow = container_of(work, struct async_cow, work);
1008
1009 compress_file_range(async_cow->inode, async_cow->locked_page,
1010 async_cow->start, async_cow->end, async_cow,
1011 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001012 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001013 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001014 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001015 }
Chris Mason771ed682008-11-06 22:02:51 -05001016}
1017
1018/*
1019 * work queue call back to submit previously compressed pages
1020 */
1021static noinline void async_cow_submit(struct btrfs_work *work)
1022{
1023 struct async_cow *async_cow;
1024 struct btrfs_root *root;
1025 unsigned long nr_pages;
1026
1027 async_cow = container_of(work, struct async_cow, work);
1028
1029 root = async_cow->root;
1030 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
1031 PAGE_CACHE_SHIFT;
1032
Josef Bacik66657b32012-08-01 15:36:24 -04001033 if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) <
Liu Bo287082b2012-06-28 04:02:24 -06001034 5 * 1024 * 1024 &&
Chris Mason771ed682008-11-06 22:02:51 -05001035 waitqueue_active(&root->fs_info->async_submit_wait))
1036 wake_up(&root->fs_info->async_submit_wait);
1037
Chris Masond3977122009-01-05 21:25:51 -05001038 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001039 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001040}
Chris Masonc8b97812008-10-29 14:49:59 -04001041
Chris Mason771ed682008-11-06 22:02:51 -05001042static noinline void async_cow_free(struct btrfs_work *work)
1043{
1044 struct async_cow *async_cow;
1045 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001046 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001047 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001048 kfree(async_cow);
1049}
1050
1051static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1052 u64 start, u64 end, int *page_started,
1053 unsigned long *nr_written)
1054{
1055 struct async_cow *async_cow;
1056 struct btrfs_root *root = BTRFS_I(inode)->root;
1057 unsigned long nr_pages;
1058 u64 cur_end;
Liu Bo287082b2012-06-28 04:02:24 -06001059 int limit = 10 * 1024 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -05001060
Chris Masona3429ab2009-10-08 12:30:20 -04001061 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1062 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001063 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001064 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001065 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001066 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001067 async_cow->root = root;
1068 async_cow->locked_page = locked_page;
1069 async_cow->start = start;
1070
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001071 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
Chris Mason771ed682008-11-06 22:02:51 -05001072 cur_end = end;
1073 else
1074 cur_end = min(end, start + 512 * 1024 - 1);
1075
1076 async_cow->end = cur_end;
1077 INIT_LIST_HEAD(&async_cow->extents);
1078
1079 async_cow->work.func = async_cow_start;
1080 async_cow->work.ordered_func = async_cow_submit;
1081 async_cow->work.ordered_free = async_cow_free;
1082 async_cow->work.flags = 0;
1083
Chris Mason771ed682008-11-06 22:02:51 -05001084 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
1085 PAGE_CACHE_SHIFT;
1086 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
1087
1088 btrfs_queue_worker(&root->fs_info->delalloc_workers,
1089 &async_cow->work);
1090
1091 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
1092 wait_event(root->fs_info->async_submit_wait,
1093 (atomic_read(&root->fs_info->async_delalloc_pages) <
1094 limit));
1095 }
1096
Chris Masond3977122009-01-05 21:25:51 -05001097 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -05001098 atomic_read(&root->fs_info->async_delalloc_pages)) {
1099 wait_event(root->fs_info->async_submit_wait,
1100 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1101 0));
1102 }
1103
1104 *nr_written += nr_pages;
1105 start = cur_end + 1;
1106 }
1107 *page_started = 1;
1108 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001109}
1110
Chris Masond3977122009-01-05 21:25:51 -05001111static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001112 u64 bytenr, u64 num_bytes)
1113{
1114 int ret;
1115 struct btrfs_ordered_sum *sums;
1116 LIST_HEAD(list);
1117
Yan Zheng07d400a2009-01-06 11:42:00 -05001118 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001119 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001120 if (ret == 0 && list_empty(&list))
1121 return 0;
1122
1123 while (!list_empty(&list)) {
1124 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1125 list_del(&sums->list);
1126 kfree(sums);
1127 }
1128 return 1;
1129}
1130
Chris Masond352ac62008-09-29 15:18:18 -04001131/*
1132 * when nowcow writeback call back. This checks for snapshots or COW copies
1133 * of the extents that exist in the file, and COWs the file as required.
1134 *
1135 * If no cow copies or snapshots exist, we write directly to the existing
1136 * blocks on disk
1137 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001138static noinline int run_delalloc_nocow(struct inode *inode,
1139 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001140 u64 start, u64 end, int *page_started, int force,
1141 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001142{
Chris Masonbe20aa92007-12-17 20:14:01 -05001143 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001144 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001145 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001146 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001147 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001148 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001149 u64 cow_start;
1150 u64 cur_offset;
1151 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001152 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001153 u64 disk_bytenr;
1154 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001155 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001156 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001157 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001158 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001159 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001160 int nocow;
1161 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001162 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001163 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001164
1165 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001166 if (!path) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001167 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1168 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001169 EXTENT_DO_ACCOUNTING |
1170 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001171 PAGE_CLEAR_DIRTY |
1172 PAGE_SET_WRITEBACK |
1173 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001174 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001175 }
Li Zefan82d59022011-04-20 10:33:24 +08001176
Liu Bo83eea1f2012-07-10 05:28:39 -06001177 nolock = btrfs_is_free_space_inode(inode);
Li Zefan82d59022011-04-20 10:33:24 +08001178
1179 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001180 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001181 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001182 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001183
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001184 if (IS_ERR(trans)) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001185 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1186 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001187 EXTENT_DO_ACCOUNTING |
1188 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001189 PAGE_CLEAR_DIRTY |
1190 PAGE_SET_WRITEBACK |
1191 PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001192 btrfs_free_path(path);
1193 return PTR_ERR(trans);
1194 }
1195
Josef Bacik74b21072011-04-13 12:02:53 -04001196 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001197
Yan Zheng80ff3852008-10-30 14:20:02 -04001198 cow_start = (u64)-1;
1199 cur_offset = start;
1200 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001201 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001202 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001203 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001204 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001205 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1206 leaf = path->nodes[0];
1207 btrfs_item_key_to_cpu(leaf, &found_key,
1208 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001209 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001210 found_key.type == BTRFS_EXTENT_DATA_KEY)
1211 path->slots[0]--;
1212 }
1213 check_prev = 0;
1214next_slot:
1215 leaf = path->nodes[0];
1216 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1217 ret = btrfs_next_leaf(root, path);
Josef Bacikd788a342013-10-25 16:55:08 -04001218 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001219 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001220 if (ret > 0)
1221 break;
1222 leaf = path->nodes[0];
1223 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001224
Yan Zheng80ff3852008-10-30 14:20:02 -04001225 nocow = 0;
1226 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001227 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001228 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001229
Li Zefan33345d012011-04-20 10:31:50 +08001230 if (found_key.objectid > ino ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001231 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1232 found_key.offset > end)
1233 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001234
Yan Zheng80ff3852008-10-30 14:20:02 -04001235 if (found_key.offset > cur_offset) {
1236 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001237 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001238 goto out_check;
1239 }
Chris Masonc31f8832008-01-08 15:46:31 -05001240
Yan Zheng80ff3852008-10-30 14:20:02 -04001241 fi = btrfs_item_ptr(leaf, path->slots[0],
1242 struct btrfs_file_extent_item);
1243 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001244
Josef Bacikcc95bef2013-04-04 14:31:27 -04001245 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001246 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1247 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001248 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001249 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001250 extent_end = found_key.offset +
1251 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001252 disk_num_bytes =
1253 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001254 if (extent_end <= start) {
1255 path->slots[0]++;
1256 goto next_slot;
1257 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001258 if (disk_bytenr == 0)
1259 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001260 if (btrfs_file_extent_compression(leaf, fi) ||
1261 btrfs_file_extent_encryption(leaf, fi) ||
1262 btrfs_file_extent_other_encoding(leaf, fi))
1263 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001264 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1265 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001266 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001267 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001268 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001269 found_key.offset -
1270 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001271 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001272 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001273 disk_bytenr += cur_offset - found_key.offset;
1274 num_bytes = min(end + 1, extent_end) - cur_offset;
1275 /*
1276 * force cow if csum exists in the range.
1277 * this ensure that csum for a given extent are
1278 * either valid or do not exist.
1279 */
1280 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1281 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001282 nocow = 1;
1283 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1284 extent_end = found_key.offset +
Chris Mason514ac8a2014-01-03 21:07:00 -08001285 btrfs_file_extent_inline_len(leaf,
1286 path->slots[0], fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001287 extent_end = ALIGN(extent_end, root->sectorsize);
1288 } else {
1289 BUG_ON(1);
1290 }
1291out_check:
1292 if (extent_end <= start) {
1293 path->slots[0]++;
1294 goto next_slot;
1295 }
1296 if (!nocow) {
1297 if (cow_start == (u64)-1)
1298 cow_start = cur_offset;
1299 cur_offset = extent_end;
1300 if (cur_offset > end)
1301 break;
1302 path->slots[0]++;
1303 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001304 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001305
David Sterbab3b4aa72011-04-21 01:20:15 +02001306 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001307 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001308 ret = cow_file_range(inode, locked_page,
1309 cow_start, found_key.offset - 1,
1310 page_started, nr_written, 1);
Josef Bacikd788a342013-10-25 16:55:08 -04001311 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001312 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001313 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001314 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001315
Yan Zhengd899e052008-10-30 14:25:28 -04001316 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1317 struct extent_map *em;
1318 struct extent_map_tree *em_tree;
1319 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001320 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001321 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001322 em->start = cur_offset;
Josef Bacik70c8a912012-10-11 16:54:30 -04001323 em->orig_start = found_key.offset - extent_offset;
Yan Zhengd899e052008-10-30 14:25:28 -04001324 em->len = num_bytes;
1325 em->block_len = num_bytes;
1326 em->block_start = disk_bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05001327 em->orig_block_len = disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001328 em->ram_bytes = ram_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04001329 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001330 em->mod_start = em->start;
1331 em->mod_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04001332 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacikb11e2342012-12-03 10:58:15 -05001333 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001334 em->generation = -1;
Yan Zhengd899e052008-10-30 14:25:28 -04001335 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001336 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001337 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001338 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001339 if (ret != -EEXIST) {
1340 free_extent_map(em);
1341 break;
1342 }
1343 btrfs_drop_extent_cache(inode, em->start,
1344 em->start + em->len - 1, 0);
1345 }
1346 type = BTRFS_ORDERED_PREALLOC;
1347 } else {
1348 type = BTRFS_ORDERED_NOCOW;
1349 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001350
1351 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001352 num_bytes, num_bytes, type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001353 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001354
Yan, Zhengefa56462010-05-16 10:49:59 -04001355 if (root->root_key.objectid ==
1356 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1357 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1358 num_bytes);
Josef Bacikd788a342013-10-25 16:55:08 -04001359 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001360 goto error;
Yan, Zhengefa56462010-05-16 10:49:59 -04001361 }
1362
Josef Bacikc2790a22013-07-29 11:20:47 -04001363 extent_clear_unlock_delalloc(inode, cur_offset,
1364 cur_offset + num_bytes - 1,
1365 locked_page, EXTENT_LOCKED |
1366 EXTENT_DELALLOC, PAGE_UNLOCK |
1367 PAGE_SET_PRIVATE2);
Yan Zheng80ff3852008-10-30 14:20:02 -04001368 cur_offset = extent_end;
1369 if (cur_offset > end)
1370 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001371 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001372 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001373
Josef Bacik17ca04a2012-05-31 15:58:55 -04001374 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001375 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001376 cur_offset = end;
1377 }
1378
Yan Zheng80ff3852008-10-30 14:20:02 -04001379 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001380 ret = cow_file_range(inode, locked_page, cow_start, end,
1381 page_started, nr_written, 1);
Josef Bacikd788a342013-10-25 16:55:08 -04001382 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001383 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001384 }
1385
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001386error:
Miao Xiea698d0752012-09-20 01:51:59 -06001387 err = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001388 if (!ret)
1389 ret = err;
1390
Josef Bacik17ca04a2012-05-31 15:58:55 -04001391 if (ret && cur_offset < end)
Josef Bacikc2790a22013-07-29 11:20:47 -04001392 extent_clear_unlock_delalloc(inode, cur_offset, end,
1393 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001394 EXTENT_DELALLOC | EXTENT_DEFRAG |
1395 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1396 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001397 PAGE_SET_WRITEBACK |
1398 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001399 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001400 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001401}
1402
Chris Masond352ac62008-09-29 15:18:18 -04001403/*
1404 * extent_io.c call back to do delayed allocation processing
1405 */
Chris Masonc8b97812008-10-29 14:49:59 -04001406static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001407 u64 start, u64 end, int *page_started,
1408 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001409{
Chris Masonbe20aa92007-12-17 20:14:01 -05001410 int ret;
Chris Mason7f366cf2009-03-12 20:12:45 -04001411 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona2135012008-06-25 16:01:30 -04001412
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001413 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) {
Chris Masonc8b97812008-10-29 14:49:59 -04001414 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001415 page_started, 1, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001416 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001417 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001418 page_started, 0, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001419 } else if (!btrfs_test_opt(root, COMPRESS) &&
1420 !(BTRFS_I(inode)->force_compress) &&
1421 !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS)) {
Chris Mason7f366cf2009-03-12 20:12:45 -04001422 ret = cow_file_range(inode, locked_page, start, end,
1423 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001424 } else {
1425 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1426 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001427 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001428 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001429 }
Chris Masonb888db22007-08-27 16:49:44 -04001430 return ret;
1431}
1432
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001433static void btrfs_split_extent_hook(struct inode *inode,
1434 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001435{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001436 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001437 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001438 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001439
Josef Bacik9e0baf62011-07-15 15:16:44 +00001440 spin_lock(&BTRFS_I(inode)->lock);
1441 BTRFS_I(inode)->outstanding_extents++;
1442 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001443}
1444
1445/*
1446 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1447 * extents so we can keep track of new extents that are just merged onto old
1448 * extents, such as when we are doing sequential writes, so we can properly
1449 * account for the metadata space we'll need.
1450 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001451static void btrfs_merge_extent_hook(struct inode *inode,
1452 struct extent_state *new,
1453 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001454{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001455 /* not delalloc, ignore it */
1456 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001457 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001458
Josef Bacik9e0baf62011-07-15 15:16:44 +00001459 spin_lock(&BTRFS_I(inode)->lock);
1460 BTRFS_I(inode)->outstanding_extents--;
1461 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001462}
1463
Miao Xieeb73c1b2013-05-15 07:48:22 +00001464static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1465 struct inode *inode)
1466{
1467 spin_lock(&root->delalloc_lock);
1468 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1469 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1470 &root->delalloc_inodes);
1471 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1472 &BTRFS_I(inode)->runtime_flags);
1473 root->nr_delalloc_inodes++;
1474 if (root->nr_delalloc_inodes == 1) {
1475 spin_lock(&root->fs_info->delalloc_root_lock);
1476 BUG_ON(!list_empty(&root->delalloc_root));
1477 list_add_tail(&root->delalloc_root,
1478 &root->fs_info->delalloc_roots);
1479 spin_unlock(&root->fs_info->delalloc_root_lock);
1480 }
1481 }
1482 spin_unlock(&root->delalloc_lock);
1483}
1484
1485static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1486 struct inode *inode)
1487{
1488 spin_lock(&root->delalloc_lock);
1489 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1490 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1491 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1492 &BTRFS_I(inode)->runtime_flags);
1493 root->nr_delalloc_inodes--;
1494 if (!root->nr_delalloc_inodes) {
1495 spin_lock(&root->fs_info->delalloc_root_lock);
1496 BUG_ON(list_empty(&root->delalloc_root));
1497 list_del_init(&root->delalloc_root);
1498 spin_unlock(&root->fs_info->delalloc_root_lock);
1499 }
1500 }
1501 spin_unlock(&root->delalloc_lock);
1502}
1503
Chris Masond352ac62008-09-29 15:18:18 -04001504/*
1505 * extent_io.c set_bit_hook, used to track delayed allocation
1506 * bytes in this file, and to maintain the list of inodes that
1507 * have pending delalloc work to be done.
1508 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001509static void btrfs_set_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001510 struct extent_state *state, unsigned long *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001511{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001512
Chris Mason75eff682008-12-15 15:54:40 -05001513 /*
1514 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001515 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001516 * bit, which is only set or cleared with irqs on
1517 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001518 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001519 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001520 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001521 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001522
Josef Bacik9e0baf62011-07-15 15:16:44 +00001523 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001524 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001525 } else {
1526 spin_lock(&BTRFS_I(inode)->lock);
1527 BTRFS_I(inode)->outstanding_extents++;
1528 spin_unlock(&BTRFS_I(inode)->lock);
1529 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001530
Miao Xie963d6782013-01-29 10:10:51 +00001531 __percpu_counter_add(&root->fs_info->delalloc_bytes, len,
1532 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001533 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001534 BTRFS_I(inode)->delalloc_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001535 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001536 &BTRFS_I(inode)->runtime_flags))
1537 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001538 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001539 }
Chris Mason291d6732008-01-29 15:55:23 -05001540}
1541
Chris Masond352ac62008-09-29 15:18:18 -04001542/*
1543 * extent_io.c clear_bit_hook, see set_bit_hook for why
1544 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001545static void btrfs_clear_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001546 struct extent_state *state,
1547 unsigned long *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001548{
Chris Mason75eff682008-12-15 15:54:40 -05001549 /*
1550 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001551 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001552 * bit, which is only set or cleared with irqs on
1553 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001554 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001555 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001556 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001557 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001558
Josef Bacik9e0baf62011-07-15 15:16:44 +00001559 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001560 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001561 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1562 spin_lock(&BTRFS_I(inode)->lock);
1563 BTRFS_I(inode)->outstanding_extents--;
1564 spin_unlock(&BTRFS_I(inode)->lock);
1565 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001566
Josef Bacikb6d08f02013-09-27 14:57:43 -04001567 /*
1568 * We don't reserve metadata space for space cache inodes so we
1569 * don't need to call dellalloc_release_metadata if there is an
1570 * error.
1571 */
1572 if (*bits & EXTENT_DO_ACCOUNTING &&
1573 root != root->fs_info->tree_root)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001574 btrfs_delalloc_release_metadata(inode, len);
1575
Josef Bacik0cb59c92010-07-02 12:14:14 -04001576 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Josef Bacik7ee9e442013-06-21 16:37:03 -04001577 && do_list && !(state->state & EXTENT_NORESERVE))
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001578 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001579
Miao Xie963d6782013-01-29 10:10:51 +00001580 __percpu_counter_add(&root->fs_info->delalloc_bytes, -len,
1581 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001582 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001583 BTRFS_I(inode)->delalloc_bytes -= len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001584 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001585 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001586 &BTRFS_I(inode)->runtime_flags))
1587 btrfs_del_delalloc_inode(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001588 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001589 }
Chris Mason291d6732008-01-29 15:55:23 -05001590}
1591
Chris Masond352ac62008-09-29 15:18:18 -04001592/*
1593 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1594 * we don't create bios that span stripes or chunks
1595 */
David Woodhouse64a16702009-07-15 23:29:37 +01001596int btrfs_merge_bio_hook(int rw, struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001597 size_t size, struct bio *bio,
1598 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001599{
1600 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Chris Masona62b9402008-10-03 16:31:08 -04001601 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001602 u64 length = 0;
1603 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001604 int ret;
1605
Chris Mason771ed682008-11-06 22:02:51 -05001606 if (bio_flags & EXTENT_BIO_COMPRESSED)
1607 return 0;
1608
Chris Masonf2d8d742008-04-21 10:03:05 -04001609 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001610 map_length = length;
David Woodhouse64a16702009-07-15 23:29:37 +01001611 ret = btrfs_map_block(root->fs_info, rw, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001612 &map_length, NULL, 0);
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001613 /* Will always return 0 with map_multi == NULL */
Jeff Mahoney3444a972011-10-03 23:23:13 -04001614 BUG_ON(ret < 0);
Chris Masond3977122009-01-05 21:25:51 -05001615 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001616 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001617 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001618}
1619
Chris Masond352ac62008-09-29 15:18:18 -04001620/*
1621 * in order to insert checksums into the metadata in large chunks,
1622 * we wait until bio submission time. All the pages in the bio are
1623 * checksummed and sums are attached onto the ordered extent record.
1624 *
1625 * At IO completion time the cums attached on the ordered extent record
1626 * are inserted into the btree
1627 */
Chris Masond3977122009-01-05 21:25:51 -05001628static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1629 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001630 unsigned long bio_flags,
1631 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001632{
Chris Mason065631f2008-02-20 12:07:25 -05001633 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001634 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001635
Chris Masond20f7042008-12-08 16:58:54 -05001636 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001637 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001638 return 0;
1639}
Chris Masone0156402008-04-16 11:15:20 -04001640
Chris Mason4a69a412008-11-06 22:03:00 -05001641/*
1642 * in order to insert checksums into the metadata in large chunks,
1643 * we wait until bio submission time. All the pages in the bio are
1644 * checksummed and sums are attached onto the ordered extent record.
1645 *
1646 * At IO completion time the cums attached on the ordered extent record
1647 * are inserted into the btree
1648 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001649static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001650 int mirror_num, unsigned long bio_flags,
1651 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001652{
1653 struct btrfs_root *root = BTRFS_I(inode)->root;
Stefan Behrens61891922012-11-05 18:51:52 +01001654 int ret;
1655
1656 ret = btrfs_map_bio(root, rw, bio, mirror_num, 1);
1657 if (ret)
1658 bio_endio(bio, ret);
1659 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001660}
1661
Chris Masond352ac62008-09-29 15:18:18 -04001662/*
Chris Masoncad321a2008-12-17 14:51:42 -05001663 * extent_io.c submission hook. This does the right thing for csum calculation
1664 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001665 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001666static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001667 int mirror_num, unsigned long bio_flags,
1668 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001669{
1670 struct btrfs_root *root = BTRFS_I(inode)->root;
1671 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001672 int skip_sum;
Jeff Mahoney04173412011-10-03 23:23:12 -04001673 int metadata = 0;
Josef Bacikb812ce22012-11-16 13:56:32 -05001674 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001675
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001676 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001677
Liu Bo83eea1f2012-07-10 05:28:39 -06001678 if (btrfs_is_free_space_inode(inode))
Jeff Mahoney04173412011-10-03 23:23:12 -04001679 metadata = 2;
1680
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001681 if (!(rw & REQ_WRITE)) {
Josef Bacik5fd02042012-05-02 14:00:54 -04001682 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1683 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001684 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001685
Chris Masond20f7042008-12-08 16:58:54 -05001686 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001687 ret = btrfs_submit_compressed_read(inode, bio,
1688 mirror_num,
1689 bio_flags);
1690 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001691 } else if (!skip_sum) {
1692 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1693 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001694 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001695 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001696 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001697 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001698 /* csum items have already been cloned */
1699 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1700 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001701 /* we're doing a write, do the async checksumming */
Stefan Behrens61891922012-11-05 18:51:52 +01001702 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001703 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001704 bio_flags, bio_offset,
1705 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001706 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001707 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001708 } else if (!skip_sum) {
1709 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1710 if (ret)
1711 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001712 }
1713
Chris Mason0b86a832008-03-24 15:01:56 -04001714mapit:
Stefan Behrens61891922012-11-05 18:51:52 +01001715 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
1716
1717out:
1718 if (ret < 0)
1719 bio_endio(bio, ret);
1720 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001721}
Chris Mason6885f302008-02-20 16:11:05 -05001722
Chris Masond352ac62008-09-29 15:18:18 -04001723/*
1724 * given a list of ordered sums record them in the inode. This happens
1725 * at IO completion time based on sums calculated at bio submission time.
1726 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001727static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001728 struct inode *inode, u64 file_offset,
1729 struct list_head *list)
1730{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001731 struct btrfs_ordered_sum *sum;
1732
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001733 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00001734 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05001735 btrfs_csum_file_blocks(trans,
1736 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00001737 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001738 }
1739 return 0;
1740}
1741
Josef Bacik2ac55d42010-02-03 19:33:23 +00001742int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1743 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001744{
Julia Lawall6c1500f2012-11-03 20:30:18 +00001745 WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04001746 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001747 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001748}
1749
Chris Masond352ac62008-09-29 15:18:18 -04001750/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001751struct btrfs_writepage_fixup {
1752 struct page *page;
1753 struct btrfs_work work;
1754};
1755
Christoph Hellwigb2950862008-12-02 09:54:17 -05001756static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001757{
1758 struct btrfs_writepage_fixup *fixup;
1759 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001760 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001761 struct page *page;
1762 struct inode *inode;
1763 u64 page_start;
1764 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001765 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001766
1767 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1768 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001769again:
Chris Mason247e7432008-07-17 12:53:51 -04001770 lock_page(page);
1771 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1772 ClearPageChecked(page);
1773 goto out_page;
1774 }
1775
1776 inode = page->mapping->host;
1777 page_start = page_offset(page);
1778 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1779
Josef Bacik2ac55d42010-02-03 19:33:23 +00001780 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001781 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001782
1783 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001784 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001785 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001786
1787 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1788 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001789 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1790 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001791 unlock_page(page);
1792 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001793 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04001794 goto again;
1795 }
Chris Mason247e7432008-07-17 12:53:51 -04001796
Jeff Mahoney87826df2012-02-15 16:23:57 +01001797 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
1798 if (ret) {
1799 mapping_set_error(page->mapping, ret);
1800 end_extent_writepage(page, ret, page_start, page_end);
1801 ClearPageChecked(page);
1802 goto out;
1803 }
1804
Josef Bacik2ac55d42010-02-03 19:33:23 +00001805 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001806 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001807 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04001808out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001809 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1810 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001811out_page:
1812 unlock_page(page);
1813 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08001814 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04001815}
1816
1817/*
1818 * There are a few paths in the higher layers of the kernel that directly
1819 * set the page dirty bit without asking the filesystem if it is a
1820 * good idea. This causes problems because we want to make sure COW
1821 * properly happens and the data=ordered rules are followed.
1822 *
Chris Masonc8b97812008-10-29 14:49:59 -04001823 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001824 * hasn't been properly setup for IO. We kick off an async process
1825 * to fix it up. The async helper will wait for ordered extents, set
1826 * the delalloc bit and make it safe to write the page.
1827 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001828static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001829{
1830 struct inode *inode = page->mapping->host;
1831 struct btrfs_writepage_fixup *fixup;
1832 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001833
Chris Mason8b62b722009-09-02 16:53:46 -04001834 /* this page is properly in the ordered list */
1835 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001836 return 0;
1837
1838 if (PageChecked(page))
1839 return -EAGAIN;
1840
1841 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1842 if (!fixup)
1843 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001844
Chris Mason247e7432008-07-17 12:53:51 -04001845 SetPageChecked(page);
1846 page_cache_get(page);
1847 fixup->work.func = btrfs_writepage_fixup_worker;
1848 fixup->page = page;
1849 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001850 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04001851}
1852
Yan Zhengd899e052008-10-30 14:25:28 -04001853static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1854 struct inode *inode, u64 file_pos,
1855 u64 disk_bytenr, u64 disk_num_bytes,
1856 u64 num_bytes, u64 ram_bytes,
1857 u8 compression, u8 encryption,
1858 u16 other_encoding, int extent_type)
1859{
1860 struct btrfs_root *root = BTRFS_I(inode)->root;
1861 struct btrfs_file_extent_item *fi;
1862 struct btrfs_path *path;
1863 struct extent_buffer *leaf;
1864 struct btrfs_key ins;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00001865 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04001866 int ret;
1867
1868 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07001869 if (!path)
1870 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04001871
Chris Masona1ed8352009-09-11 12:27:37 -04001872 /*
1873 * we may be replacing one extent in the tree with another.
1874 * The new extent is pinned in the extent map, and we don't want
1875 * to drop it from the cache until it is completely in the btree.
1876 *
1877 * So, tell btrfs_drop_extents to leave this extent in the cache.
1878 * the caller is expected to unpin it and allow it to be merged
1879 * with the others.
1880 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00001881 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
1882 file_pos + num_bytes, NULL, 0,
1883 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001884 if (ret)
1885 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001886
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00001887 if (!extent_inserted) {
1888 ins.objectid = btrfs_ino(inode);
1889 ins.offset = file_pos;
1890 ins.type = BTRFS_EXTENT_DATA_KEY;
1891
1892 path->leave_spinning = 1;
1893 ret = btrfs_insert_empty_item(trans, root, path, &ins,
1894 sizeof(*fi));
1895 if (ret)
1896 goto out;
1897 }
Yan Zhengd899e052008-10-30 14:25:28 -04001898 leaf = path->nodes[0];
1899 fi = btrfs_item_ptr(leaf, path->slots[0],
1900 struct btrfs_file_extent_item);
1901 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1902 btrfs_set_file_extent_type(leaf, fi, extent_type);
1903 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1904 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1905 btrfs_set_file_extent_offset(leaf, fi, 0);
1906 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1907 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1908 btrfs_set_file_extent_compression(leaf, fi, compression);
1909 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1910 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04001911
Yan Zhengd899e052008-10-30 14:25:28 -04001912 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04001913 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04001914
1915 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04001916
1917 ins.objectid = disk_bytenr;
1918 ins.offset = disk_num_bytes;
1919 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001920 ret = btrfs_alloc_reserved_file_extent(trans, root,
1921 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08001922 btrfs_ino(inode), file_pos, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001923out:
Yan Zhengd899e052008-10-30 14:25:28 -04001924 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04001925
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001926 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04001927}
1928
Liu Bo38c227d2013-01-29 03:18:40 +00001929/* snapshot-aware defrag */
1930struct sa_defrag_extent_backref {
1931 struct rb_node node;
1932 struct old_sa_defrag_extent *old;
1933 u64 root_id;
1934 u64 inum;
1935 u64 file_pos;
1936 u64 extent_offset;
1937 u64 num_bytes;
1938 u64 generation;
1939};
1940
1941struct old_sa_defrag_extent {
1942 struct list_head list;
1943 struct new_sa_defrag_extent *new;
1944
1945 u64 extent_offset;
1946 u64 bytenr;
1947 u64 offset;
1948 u64 len;
1949 int count;
1950};
1951
1952struct new_sa_defrag_extent {
1953 struct rb_root root;
1954 struct list_head head;
1955 struct btrfs_path *path;
1956 struct inode *inode;
1957 u64 file_pos;
1958 u64 len;
1959 u64 bytenr;
1960 u64 disk_len;
1961 u8 compress_type;
1962};
1963
1964static int backref_comp(struct sa_defrag_extent_backref *b1,
1965 struct sa_defrag_extent_backref *b2)
1966{
1967 if (b1->root_id < b2->root_id)
1968 return -1;
1969 else if (b1->root_id > b2->root_id)
1970 return 1;
1971
1972 if (b1->inum < b2->inum)
1973 return -1;
1974 else if (b1->inum > b2->inum)
1975 return 1;
1976
1977 if (b1->file_pos < b2->file_pos)
1978 return -1;
1979 else if (b1->file_pos > b2->file_pos)
1980 return 1;
1981
1982 /*
1983 * [------------------------------] ===> (a range of space)
1984 * |<--->| |<---->| =============> (fs/file tree A)
1985 * |<---------------------------->| ===> (fs/file tree B)
1986 *
1987 * A range of space can refer to two file extents in one tree while
1988 * refer to only one file extent in another tree.
1989 *
1990 * So we may process a disk offset more than one time(two extents in A)
1991 * and locate at the same extent(one extent in B), then insert two same
1992 * backrefs(both refer to the extent in B).
1993 */
1994 return 0;
1995}
1996
1997static void backref_insert(struct rb_root *root,
1998 struct sa_defrag_extent_backref *backref)
1999{
2000 struct rb_node **p = &root->rb_node;
2001 struct rb_node *parent = NULL;
2002 struct sa_defrag_extent_backref *entry;
2003 int ret;
2004
2005 while (*p) {
2006 parent = *p;
2007 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2008
2009 ret = backref_comp(backref, entry);
2010 if (ret < 0)
2011 p = &(*p)->rb_left;
2012 else
2013 p = &(*p)->rb_right;
2014 }
2015
2016 rb_link_node(&backref->node, parent, p);
2017 rb_insert_color(&backref->node, root);
2018}
2019
2020/*
2021 * Note the backref might has changed, and in this case we just return 0.
2022 */
2023static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2024 void *ctx)
2025{
2026 struct btrfs_file_extent_item *extent;
2027 struct btrfs_fs_info *fs_info;
2028 struct old_sa_defrag_extent *old = ctx;
2029 struct new_sa_defrag_extent *new = old->new;
2030 struct btrfs_path *path = new->path;
2031 struct btrfs_key key;
2032 struct btrfs_root *root;
2033 struct sa_defrag_extent_backref *backref;
2034 struct extent_buffer *leaf;
2035 struct inode *inode = new->inode;
2036 int slot;
2037 int ret;
2038 u64 extent_offset;
2039 u64 num_bytes;
2040
2041 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
2042 inum == btrfs_ino(inode))
2043 return 0;
2044
2045 key.objectid = root_id;
2046 key.type = BTRFS_ROOT_ITEM_KEY;
2047 key.offset = (u64)-1;
2048
2049 fs_info = BTRFS_I(inode)->root->fs_info;
2050 root = btrfs_read_fs_root_no_name(fs_info, &key);
2051 if (IS_ERR(root)) {
2052 if (PTR_ERR(root) == -ENOENT)
2053 return 0;
2054 WARN_ON(1);
2055 pr_debug("inum=%llu, offset=%llu, root_id=%llu\n",
2056 inum, offset, root_id);
2057 return PTR_ERR(root);
2058 }
2059
2060 key.objectid = inum;
2061 key.type = BTRFS_EXTENT_DATA_KEY;
2062 if (offset > (u64)-1 << 32)
2063 key.offset = 0;
2064 else
2065 key.offset = offset;
2066
2067 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302068 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002069 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002070 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002071
2072 while (1) {
2073 cond_resched();
2074
2075 leaf = path->nodes[0];
2076 slot = path->slots[0];
2077
2078 if (slot >= btrfs_header_nritems(leaf)) {
2079 ret = btrfs_next_leaf(root, path);
2080 if (ret < 0) {
2081 goto out;
2082 } else if (ret > 0) {
2083 ret = 0;
2084 goto out;
2085 }
2086 continue;
2087 }
2088
2089 path->slots[0]++;
2090
2091 btrfs_item_key_to_cpu(leaf, &key, slot);
2092
2093 if (key.objectid > inum)
2094 goto out;
2095
2096 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2097 continue;
2098
2099 extent = btrfs_item_ptr(leaf, slot,
2100 struct btrfs_file_extent_item);
2101
2102 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2103 continue;
2104
Liu Boe68afa42013-07-01 22:13:26 +08002105 /*
2106 * 'offset' refers to the exact key.offset,
2107 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2108 * (key.offset - extent_offset).
2109 */
2110 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002111 continue;
2112
Liu Boe68afa42013-07-01 22:13:26 +08002113 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002114 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002115
Liu Bo38c227d2013-01-29 03:18:40 +00002116 if (extent_offset >= old->extent_offset + old->offset +
2117 old->len || extent_offset + num_bytes <=
2118 old->extent_offset + old->offset)
2119 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002120 break;
2121 }
2122
2123 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2124 if (!backref) {
2125 ret = -ENOENT;
2126 goto out;
2127 }
2128
2129 backref->root_id = root_id;
2130 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002131 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002132 backref->num_bytes = num_bytes;
2133 backref->extent_offset = extent_offset;
2134 backref->generation = btrfs_file_extent_generation(leaf, extent);
2135 backref->old = old;
2136 backref_insert(&new->root, backref);
2137 old->count++;
2138out:
2139 btrfs_release_path(path);
2140 WARN_ON(ret);
2141 return ret;
2142}
2143
2144static noinline bool record_extent_backrefs(struct btrfs_path *path,
2145 struct new_sa_defrag_extent *new)
2146{
2147 struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info;
2148 struct old_sa_defrag_extent *old, *tmp;
2149 int ret;
2150
2151 new->path = path;
2152
2153 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002154 ret = iterate_inodes_from_logical(old->bytenr +
2155 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002156 path, record_one_backref,
2157 old);
Josef Bacik4724b102013-11-05 11:11:40 -05002158 if (ret < 0 && ret != -ENOENT)
2159 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002160
2161 /* no backref to be processed for this extent */
2162 if (!old->count) {
2163 list_del(&old->list);
2164 kfree(old);
2165 }
2166 }
2167
2168 if (list_empty(&new->head))
2169 return false;
2170
2171 return true;
2172}
2173
2174static int relink_is_mergable(struct extent_buffer *leaf,
2175 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002176 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002177{
Liu Bo116e0022013-08-02 16:30:40 +08002178 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002179 return 0;
2180
2181 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2182 return 0;
2183
Liu Bo116e0022013-08-02 16:30:40 +08002184 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2185 return 0;
2186
2187 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002188 btrfs_file_extent_other_encoding(leaf, fi))
2189 return 0;
2190
2191 return 1;
2192}
2193
2194/*
2195 * Note the backref might has changed, and in this case we just return 0.
2196 */
2197static noinline int relink_extent_backref(struct btrfs_path *path,
2198 struct sa_defrag_extent_backref *prev,
2199 struct sa_defrag_extent_backref *backref)
2200{
2201 struct btrfs_file_extent_item *extent;
2202 struct btrfs_file_extent_item *item;
2203 struct btrfs_ordered_extent *ordered;
2204 struct btrfs_trans_handle *trans;
2205 struct btrfs_fs_info *fs_info;
2206 struct btrfs_root *root;
2207 struct btrfs_key key;
2208 struct extent_buffer *leaf;
2209 struct old_sa_defrag_extent *old = backref->old;
2210 struct new_sa_defrag_extent *new = old->new;
2211 struct inode *src_inode = new->inode;
2212 struct inode *inode;
2213 struct extent_state *cached = NULL;
2214 int ret = 0;
2215 u64 start;
2216 u64 len;
2217 u64 lock_start;
2218 u64 lock_end;
2219 bool merge = false;
2220 int index;
2221
2222 if (prev && prev->root_id == backref->root_id &&
2223 prev->inum == backref->inum &&
2224 prev->file_pos + prev->num_bytes == backref->file_pos)
2225 merge = true;
2226
2227 /* step 1: get root */
2228 key.objectid = backref->root_id;
2229 key.type = BTRFS_ROOT_ITEM_KEY;
2230 key.offset = (u64)-1;
2231
2232 fs_info = BTRFS_I(src_inode)->root->fs_info;
2233 index = srcu_read_lock(&fs_info->subvol_srcu);
2234
2235 root = btrfs_read_fs_root_no_name(fs_info, &key);
2236 if (IS_ERR(root)) {
2237 srcu_read_unlock(&fs_info->subvol_srcu, index);
2238 if (PTR_ERR(root) == -ENOENT)
2239 return 0;
2240 return PTR_ERR(root);
2241 }
Liu Bo38c227d2013-01-29 03:18:40 +00002242
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002243 if (btrfs_root_readonly(root)) {
2244 srcu_read_unlock(&fs_info->subvol_srcu, index);
2245 return 0;
2246 }
2247
Liu Bo38c227d2013-01-29 03:18:40 +00002248 /* step 2: get inode */
2249 key.objectid = backref->inum;
2250 key.type = BTRFS_INODE_ITEM_KEY;
2251 key.offset = 0;
2252
2253 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2254 if (IS_ERR(inode)) {
2255 srcu_read_unlock(&fs_info->subvol_srcu, index);
2256 return 0;
2257 }
2258
2259 srcu_read_unlock(&fs_info->subvol_srcu, index);
2260
2261 /* step 3: relink backref */
2262 lock_start = backref->file_pos;
2263 lock_end = backref->file_pos + backref->num_bytes - 1;
2264 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2265 0, &cached);
2266
2267 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2268 if (ordered) {
2269 btrfs_put_ordered_extent(ordered);
2270 goto out_unlock;
2271 }
2272
2273 trans = btrfs_join_transaction(root);
2274 if (IS_ERR(trans)) {
2275 ret = PTR_ERR(trans);
2276 goto out_unlock;
2277 }
2278
2279 key.objectid = backref->inum;
2280 key.type = BTRFS_EXTENT_DATA_KEY;
2281 key.offset = backref->file_pos;
2282
2283 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2284 if (ret < 0) {
2285 goto out_free_path;
2286 } else if (ret > 0) {
2287 ret = 0;
2288 goto out_free_path;
2289 }
2290
2291 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2292 struct btrfs_file_extent_item);
2293
2294 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2295 backref->generation)
2296 goto out_free_path;
2297
2298 btrfs_release_path(path);
2299
2300 start = backref->file_pos;
2301 if (backref->extent_offset < old->extent_offset + old->offset)
2302 start += old->extent_offset + old->offset -
2303 backref->extent_offset;
2304
2305 len = min(backref->extent_offset + backref->num_bytes,
2306 old->extent_offset + old->offset + old->len);
2307 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2308
2309 ret = btrfs_drop_extents(trans, root, inode, start,
2310 start + len, 1);
2311 if (ret)
2312 goto out_free_path;
2313again:
2314 key.objectid = btrfs_ino(inode);
2315 key.type = BTRFS_EXTENT_DATA_KEY;
2316 key.offset = start;
2317
Liu Boa09a0a72013-03-11 09:20:58 +00002318 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002319 if (merge) {
2320 struct btrfs_file_extent_item *fi;
2321 u64 extent_len;
2322 struct btrfs_key found_key;
2323
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002324 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002325 if (ret < 0)
2326 goto out_free_path;
2327
2328 path->slots[0]--;
2329 leaf = path->nodes[0];
2330 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2331
2332 fi = btrfs_item_ptr(leaf, path->slots[0],
2333 struct btrfs_file_extent_item);
2334 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2335
Liu Bo116e0022013-08-02 16:30:40 +08002336 if (extent_len + found_key.offset == start &&
2337 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002338 btrfs_set_file_extent_num_bytes(leaf, fi,
2339 extent_len + len);
2340 btrfs_mark_buffer_dirty(leaf);
2341 inode_add_bytes(inode, len);
2342
2343 ret = 1;
2344 goto out_free_path;
2345 } else {
2346 merge = false;
2347 btrfs_release_path(path);
2348 goto again;
2349 }
2350 }
2351
2352 ret = btrfs_insert_empty_item(trans, root, path, &key,
2353 sizeof(*extent));
2354 if (ret) {
2355 btrfs_abort_transaction(trans, root, ret);
2356 goto out_free_path;
2357 }
2358
2359 leaf = path->nodes[0];
2360 item = btrfs_item_ptr(leaf, path->slots[0],
2361 struct btrfs_file_extent_item);
2362 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2363 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2364 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2365 btrfs_set_file_extent_num_bytes(leaf, item, len);
2366 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2367 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2368 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2369 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2370 btrfs_set_file_extent_encryption(leaf, item, 0);
2371 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2372
2373 btrfs_mark_buffer_dirty(leaf);
2374 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002375 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002376
2377 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
2378 new->disk_len, 0,
2379 backref->root_id, backref->inum,
2380 new->file_pos, 0); /* start - extent_offset */
2381 if (ret) {
2382 btrfs_abort_transaction(trans, root, ret);
2383 goto out_free_path;
2384 }
2385
2386 ret = 1;
2387out_free_path:
2388 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002389 path->leave_spinning = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002390 btrfs_end_transaction(trans, root);
2391out_unlock:
2392 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2393 &cached, GFP_NOFS);
2394 iput(inode);
2395 return ret;
2396}
2397
Liu Bo6f519562013-10-29 10:45:05 +08002398static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2399{
2400 struct old_sa_defrag_extent *old, *tmp;
2401
2402 if (!new)
2403 return;
2404
2405 list_for_each_entry_safe(old, tmp, &new->head, list) {
2406 list_del(&old->list);
2407 kfree(old);
2408 }
2409 kfree(new);
2410}
2411
Liu Bo38c227d2013-01-29 03:18:40 +00002412static void relink_file_extents(struct new_sa_defrag_extent *new)
2413{
2414 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002415 struct sa_defrag_extent_backref *backref;
2416 struct sa_defrag_extent_backref *prev = NULL;
2417 struct inode *inode;
2418 struct btrfs_root *root;
2419 struct rb_node *node;
2420 int ret;
2421
2422 inode = new->inode;
2423 root = BTRFS_I(inode)->root;
2424
2425 path = btrfs_alloc_path();
2426 if (!path)
2427 return;
2428
2429 if (!record_extent_backrefs(path, new)) {
2430 btrfs_free_path(path);
2431 goto out;
2432 }
2433 btrfs_release_path(path);
2434
2435 while (1) {
2436 node = rb_first(&new->root);
2437 if (!node)
2438 break;
2439 rb_erase(node, &new->root);
2440
2441 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2442
2443 ret = relink_extent_backref(path, prev, backref);
2444 WARN_ON(ret < 0);
2445
2446 kfree(prev);
2447
2448 if (ret == 1)
2449 prev = backref;
2450 else
2451 prev = NULL;
2452 cond_resched();
2453 }
2454 kfree(prev);
2455
2456 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002457out:
Liu Bo6f519562013-10-29 10:45:05 +08002458 free_sa_defrag_extent(new);
2459
Liu Bo38c227d2013-01-29 03:18:40 +00002460 atomic_dec(&root->fs_info->defrag_running);
2461 wake_up(&root->fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002462}
2463
2464static struct new_sa_defrag_extent *
2465record_old_file_extents(struct inode *inode,
2466 struct btrfs_ordered_extent *ordered)
2467{
2468 struct btrfs_root *root = BTRFS_I(inode)->root;
2469 struct btrfs_path *path;
2470 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002471 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002472 struct new_sa_defrag_extent *new;
2473 int ret;
2474
2475 new = kmalloc(sizeof(*new), GFP_NOFS);
2476 if (!new)
2477 return NULL;
2478
2479 new->inode = inode;
2480 new->file_pos = ordered->file_offset;
2481 new->len = ordered->len;
2482 new->bytenr = ordered->start;
2483 new->disk_len = ordered->disk_len;
2484 new->compress_type = ordered->compress_type;
2485 new->root = RB_ROOT;
2486 INIT_LIST_HEAD(&new->head);
2487
2488 path = btrfs_alloc_path();
2489 if (!path)
2490 goto out_kfree;
2491
2492 key.objectid = btrfs_ino(inode);
2493 key.type = BTRFS_EXTENT_DATA_KEY;
2494 key.offset = new->file_pos;
2495
2496 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2497 if (ret < 0)
2498 goto out_free_path;
2499 if (ret > 0 && path->slots[0] > 0)
2500 path->slots[0]--;
2501
2502 /* find out all the old extents for the file range */
2503 while (1) {
2504 struct btrfs_file_extent_item *extent;
2505 struct extent_buffer *l;
2506 int slot;
2507 u64 num_bytes;
2508 u64 offset;
2509 u64 end;
2510 u64 disk_bytenr;
2511 u64 extent_offset;
2512
2513 l = path->nodes[0];
2514 slot = path->slots[0];
2515
2516 if (slot >= btrfs_header_nritems(l)) {
2517 ret = btrfs_next_leaf(root, path);
2518 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002519 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002520 else if (ret > 0)
2521 break;
2522 continue;
2523 }
2524
2525 btrfs_item_key_to_cpu(l, &key, slot);
2526
2527 if (key.objectid != btrfs_ino(inode))
2528 break;
2529 if (key.type != BTRFS_EXTENT_DATA_KEY)
2530 break;
2531 if (key.offset >= new->file_pos + new->len)
2532 break;
2533
2534 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2535
2536 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2537 if (key.offset + num_bytes < new->file_pos)
2538 goto next;
2539
2540 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2541 if (!disk_bytenr)
2542 goto next;
2543
2544 extent_offset = btrfs_file_extent_offset(l, extent);
2545
2546 old = kmalloc(sizeof(*old), GFP_NOFS);
2547 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002548 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002549
2550 offset = max(new->file_pos, key.offset);
2551 end = min(new->file_pos + new->len, key.offset + num_bytes);
2552
2553 old->bytenr = disk_bytenr;
2554 old->extent_offset = extent_offset;
2555 old->offset = offset - key.offset;
2556 old->len = end - offset;
2557 old->new = new;
2558 old->count = 0;
2559 list_add_tail(&old->list, &new->head);
2560next:
2561 path->slots[0]++;
2562 cond_resched();
2563 }
2564
2565 btrfs_free_path(path);
2566 atomic_inc(&root->fs_info->defrag_running);
2567
2568 return new;
2569
Liu Bo38c227d2013-01-29 03:18:40 +00002570out_free_path:
2571 btrfs_free_path(path);
2572out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002573 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002574 return NULL;
2575}
2576
Chris Masond352ac62008-09-29 15:18:18 -04002577/* as ordered data IO finishes, this gets called so we can finish
2578 * an ordered extent if the range of bytes in the file it covers are
2579 * fully written.
2580 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002581static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002582{
Josef Bacik5fd02042012-05-02 14:00:54 -04002583 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002584 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002585 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002586 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002587 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002588 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002589 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002590 int ret = 0;
2591 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002592 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002593 bool truncated = false;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002594
Liu Bo83eea1f2012-07-10 05:28:39 -06002595 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002596
Josef Bacik5fd02042012-05-02 14:00:54 -04002597 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2598 ret = -EIO;
2599 goto out;
2600 }
2601
Josef Bacik77cef2e2013-08-29 13:57:21 -04002602 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2603 truncated = true;
2604 logical_len = ordered_extent->truncated_len;
2605 /* Truncated the entire extent, don't bother adding */
2606 if (!logical_len)
2607 goto out;
2608 }
2609
Yan, Zhengc2167752009-11-12 09:34:21 +00002610 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002611 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Josef Bacik6c760c02012-11-09 10:53:21 -05002612 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2613 if (nolock)
2614 trans = btrfs_join_transaction_nolock(root);
2615 else
2616 trans = btrfs_join_transaction(root);
2617 if (IS_ERR(trans)) {
2618 ret = PTR_ERR(trans);
2619 trans = NULL;
2620 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002621 }
Josef Bacik6c760c02012-11-09 10:53:21 -05002622 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
2623 ret = btrfs_update_inode_fallback(trans, root, inode);
2624 if (ret) /* -ENOMEM or corruption */
2625 btrfs_abort_transaction(trans, root, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002626 goto out;
2627 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002628
Josef Bacik2ac55d42010-02-03 19:33:23 +00002629 lock_extent_bits(io_tree, ordered_extent->file_offset,
2630 ordered_extent->file_offset + ordered_extent->len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002631 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002632
Liu Bo38c227d2013-01-29 03:18:40 +00002633 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2634 ordered_extent->file_offset + ordered_extent->len - 1,
2635 EXTENT_DEFRAG, 1, cached_state);
2636 if (ret) {
2637 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05002638 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00002639 /* the inode is shared */
2640 new = record_old_file_extents(inode, ordered_extent);
2641
2642 clear_extent_bit(io_tree, ordered_extent->file_offset,
2643 ordered_extent->file_offset + ordered_extent->len - 1,
2644 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2645 }
2646
Josef Bacik0cb59c92010-07-02 12:14:14 -04002647 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002648 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002649 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002650 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002651 if (IS_ERR(trans)) {
2652 ret = PTR_ERR(trans);
2653 trans = NULL;
2654 goto out_unlock;
2655 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002656 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002657
Chris Masonc8b97812008-10-29 14:49:59 -04002658 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002659 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002660 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002661 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00002662 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002663 ordered_extent->file_offset,
2664 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002665 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002666 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04002667 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002668 ret = insert_reserved_file_extent(trans, inode,
2669 ordered_extent->file_offset,
2670 ordered_extent->start,
2671 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04002672 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08002673 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002674 BTRFS_FILE_EXTENT_REG);
Yan Zhengd899e052008-10-30 14:25:28 -04002675 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002676 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2677 ordered_extent->file_offset, ordered_extent->len,
2678 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002679 if (ret < 0) {
2680 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002681 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002682 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002683
Chris Masone6dcd2d2008-07-17 12:53:50 -04002684 add_pending_csums(trans, inode, ordered_extent->file_offset,
2685 &ordered_extent->list);
2686
Josef Bacik6c760c02012-11-09 10:53:21 -05002687 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2688 ret = btrfs_update_inode_fallback(trans, root, inode);
2689 if (ret) { /* -ENOMEM or corruption */
2690 btrfs_abort_transaction(trans, root, ret);
2691 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002692 }
2693 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002694out_unlock:
2695 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2696 ordered_extent->file_offset +
2697 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00002698out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002699 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04002700 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002701 if (trans)
2702 btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002703
Josef Bacik77cef2e2013-08-29 13:57:21 -04002704 if (ret || truncated) {
2705 u64 start, end;
2706
2707 if (truncated)
2708 start = ordered_extent->file_offset + logical_len;
2709 else
2710 start = ordered_extent->file_offset;
2711 end = ordered_extent->file_offset + ordered_extent->len - 1;
2712 clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS);
2713
2714 /* Drop the cache for the part of the extent we didn't write. */
2715 btrfs_drop_extent_cache(inode, start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04002716
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002717 /*
2718 * If the ordered extent had an IOERR or something else went
2719 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04002720 * back to the allocator. We only free the extent in the
2721 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002722 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04002723 if ((ret || !logical_len) &&
2724 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002725 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
2726 btrfs_free_reserved_extent(root, ordered_extent->start,
2727 ordered_extent->disk_len);
2728 }
2729
2730
Josef Bacik5fd02042012-05-02 14:00:54 -04002731 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002732 * This needs to be done to make sure anybody waiting knows we are done
2733 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002734 */
2735 btrfs_remove_ordered_extent(inode, ordered_extent);
2736
Liu Bo38c227d2013-01-29 03:18:40 +00002737 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08002738 if (new) {
2739 if (ret) {
2740 free_sa_defrag_extent(new);
2741 atomic_dec(&root->fs_info->defrag_running);
2742 } else {
2743 relink_file_extents(new);
2744 }
2745 }
Liu Bo38c227d2013-01-29 03:18:40 +00002746
Chris Masone6dcd2d2008-07-17 12:53:50 -04002747 /* once for us */
2748 btrfs_put_ordered_extent(ordered_extent);
2749 /* once for the tree */
2750 btrfs_put_ordered_extent(ordered_extent);
2751
Josef Bacik5fd02042012-05-02 14:00:54 -04002752 return ret;
2753}
2754
2755static void finish_ordered_fn(struct btrfs_work *work)
2756{
2757 struct btrfs_ordered_extent *ordered_extent;
2758 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2759 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002760}
2761
Christoph Hellwigb2950862008-12-02 09:54:17 -05002762static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04002763 struct extent_state *state, int uptodate)
2764{
Josef Bacik5fd02042012-05-02 14:00:54 -04002765 struct inode *inode = page->mapping->host;
2766 struct btrfs_root *root = BTRFS_I(inode)->root;
2767 struct btrfs_ordered_extent *ordered_extent = NULL;
2768 struct btrfs_workers *workers;
2769
liubo1abe9b82011-03-24 11:18:59 +00002770 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2771
Chris Mason8b62b722009-09-02 16:53:46 -04002772 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002773 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2774 end - start + 1, uptodate))
2775 return 0;
2776
2777 ordered_extent->work.func = finish_ordered_fn;
2778 ordered_extent->work.flags = 0;
2779
Liu Bo83eea1f2012-07-10 05:28:39 -06002780 if (btrfs_is_free_space_inode(inode))
Josef Bacik5fd02042012-05-02 14:00:54 -04002781 workers = &root->fs_info->endio_freespace_worker;
2782 else
2783 workers = &root->fs_info->endio_write_workers;
2784 btrfs_queue_worker(workers, &ordered_extent->work);
2785
2786 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04002787}
2788
Chris Masond352ac62008-09-29 15:18:18 -04002789/*
Chris Masond352ac62008-09-29 15:18:18 -04002790 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002791 * if there's a match, we allow the bio to finish. If not, the code in
2792 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002793 */
Miao Xiefacc8a222013-07-25 19:22:34 +08002794static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
2795 u64 phy_offset, struct page *page,
2796 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002797{
Miao Xie4eee4fa2012-12-21 09:17:45 +00002798 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04002799 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002800 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04002801 char *kaddr;
Chris Masonff79f812007-10-15 16:22:25 -04002802 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiefacc8a222013-07-25 19:22:34 +08002803 u32 csum_expected;
Chris Masonff79f812007-10-15 16:22:25 -04002804 u32 csum = ~(u32)0;
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002805 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
2806 DEFAULT_RATELIMIT_BURST);
Chris Masond1310b22008-01-24 16:13:08 -05002807
Chris Masond20f7042008-12-08 16:58:54 -05002808 if (PageChecked(page)) {
2809 ClearPageChecked(page);
2810 goto good;
2811 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002812
2813 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Jan Schmidt08d2f342011-05-04 16:18:50 +02002814 goto good;
Chris Masond20f7042008-12-08 16:58:54 -05002815
Yan Zheng17d217f2008-12-12 10:03:38 -05002816 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002817 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002818 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
2819 GFP_NOFS);
2820 return 0;
2821 }
2822
Miao Xiefacc8a222013-07-25 19:22:34 +08002823 phy_offset >>= inode->i_sb->s_blocksize_bits;
2824 csum_expected = *(((u32 *)io_bio->csum) + phy_offset);
Chris Masond3977122009-01-05 21:25:51 -05002825
Miao Xiefacc8a222013-07-25 19:22:34 +08002826 kaddr = kmap_atomic(page);
Liu Bob0496682013-03-14 14:57:45 +00002827 csum = btrfs_csum_data(kaddr + offset, csum, end - start + 1);
Chris Masonff79f812007-10-15 16:22:25 -04002828 btrfs_csum_final(csum, (char *)&csum);
Miao Xiefacc8a222013-07-25 19:22:34 +08002829 if (csum != csum_expected)
Chris Mason07157aa2007-08-30 08:50:51 -04002830 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002831
Cong Wang7ac687d2011-11-25 23:14:28 +08002832 kunmap_atomic(kaddr);
Chris Masond20f7042008-12-08 16:58:54 -05002833good:
Chris Mason07157aa2007-08-30 08:50:51 -04002834 return 0;
2835
2836zeroit:
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002837 if (__ratelimit(&_rs))
Miao Xiefacc8a222013-07-25 19:22:34 +08002838 btrfs_info(root->fs_info, "csum failed ino %llu off %llu csum %u expected csum %u",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002839 btrfs_ino(page->mapping->host), start, csum, csum_expected);
Chris Masondb945352007-10-15 16:15:53 -04002840 memset(kaddr + offset, 1, end - start + 1);
2841 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002842 kunmap_atomic(kaddr);
Miao Xiefacc8a222013-07-25 19:22:34 +08002843 if (csum_expected == 0)
Chris Mason3b951512008-04-17 11:29:12 -04002844 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04002845 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04002846}
Chris Masonb888db22007-08-27 16:49:44 -04002847
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002848struct delayed_iput {
2849 struct list_head list;
2850 struct inode *inode;
2851};
2852
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002853/* JDM: If this is fs-wide, why can't we add a pointer to
2854 * btrfs_inode instead and avoid the allocation? */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002855void btrfs_add_delayed_iput(struct inode *inode)
2856{
2857 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2858 struct delayed_iput *delayed;
2859
2860 if (atomic_add_unless(&inode->i_count, -1, 1))
2861 return;
2862
2863 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2864 delayed->inode = inode;
2865
2866 spin_lock(&fs_info->delayed_iput_lock);
2867 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2868 spin_unlock(&fs_info->delayed_iput_lock);
2869}
2870
2871void btrfs_run_delayed_iputs(struct btrfs_root *root)
2872{
2873 LIST_HEAD(list);
2874 struct btrfs_fs_info *fs_info = root->fs_info;
2875 struct delayed_iput *delayed;
2876 int empty;
2877
2878 spin_lock(&fs_info->delayed_iput_lock);
2879 empty = list_empty(&fs_info->delayed_iputs);
2880 spin_unlock(&fs_info->delayed_iput_lock);
2881 if (empty)
2882 return;
2883
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002884 spin_lock(&fs_info->delayed_iput_lock);
2885 list_splice_init(&fs_info->delayed_iputs, &list);
2886 spin_unlock(&fs_info->delayed_iput_lock);
2887
2888 while (!list_empty(&list)) {
2889 delayed = list_entry(list.next, struct delayed_iput, list);
2890 list_del(&delayed->list);
2891 iput(delayed->inode);
2892 kfree(delayed);
2893 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002894}
2895
Yan, Zhengd68fc572010-05-16 10:49:58 -04002896/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08002897 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04002898 * files in the subvolume, it removes orphan item and frees block_rsv
2899 * structure.
2900 */
2901void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
2902 struct btrfs_root *root)
2903{
Josef Bacik90290e12011-12-02 15:44:12 -05002904 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002905 int ret;
2906
Josef Bacik8a35d952012-05-23 14:26:42 -04002907 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04002908 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
2909 return;
2910
Josef Bacik90290e12011-12-02 15:44:12 -05002911 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04002912 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05002913 spin_unlock(&root->orphan_lock);
2914 return;
2915 }
2916
2917 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
2918 spin_unlock(&root->orphan_lock);
2919 return;
2920 }
2921
2922 block_rsv = root->orphan_block_rsv;
2923 root->orphan_block_rsv = NULL;
2924 spin_unlock(&root->orphan_lock);
2925
Yan, Zhengd68fc572010-05-16 10:49:58 -04002926 if (root->orphan_item_inserted &&
2927 btrfs_root_refs(&root->root_item) > 0) {
2928 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
2929 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04002930 if (ret)
2931 btrfs_abort_transaction(trans, root, ret);
2932 else
2933 root->orphan_item_inserted = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002934 }
2935
Josef Bacik90290e12011-12-02 15:44:12 -05002936 if (block_rsv) {
2937 WARN_ON(block_rsv->size > 0);
2938 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002939 }
2940}
2941
2942/*
Josef Bacik7b128762008-07-24 12:17:14 -04002943 * This creates an orphan entry for the given inode in case something goes
2944 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04002945 *
2946 * NOTE: caller of this function should reserve 5 units of metadata for
2947 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04002948 */
2949int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2950{
2951 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002952 struct btrfs_block_rsv *block_rsv = NULL;
2953 int reserve = 0;
2954 int insert = 0;
2955 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002956
Yan, Zhengd68fc572010-05-16 10:49:58 -04002957 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06002958 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00002959 if (!block_rsv)
2960 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04002961 }
2962
Yan, Zhengd68fc572010-05-16 10:49:58 -04002963 spin_lock(&root->orphan_lock);
2964 if (!root->orphan_block_rsv) {
2965 root->orphan_block_rsv = block_rsv;
2966 } else if (block_rsv) {
2967 btrfs_free_block_rsv(root, block_rsv);
2968 block_rsv = NULL;
2969 }
Josef Bacik7b128762008-07-24 12:17:14 -04002970
Josef Bacik8a35d952012-05-23 14:26:42 -04002971 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2972 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04002973#if 0
2974 /*
2975 * For proper ENOSPC handling, we should do orphan
2976 * cleanup when mounting. But this introduces backward
2977 * compatibility issue.
2978 */
2979 if (!xchg(&root->orphan_item_inserted, 1))
2980 insert = 2;
2981 else
2982 insert = 1;
2983#endif
2984 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06002985 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002986 }
Josef Bacik7b128762008-07-24 12:17:14 -04002987
Josef Bacik72ac3c02012-05-23 14:13:11 -04002988 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
2989 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04002990 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002991 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002992
Yan, Zhengd68fc572010-05-16 10:49:58 -04002993 /* grab metadata reservation from transaction handle */
2994 if (reserve) {
2995 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002996 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04002997 }
2998
2999 /* insert an orphan item to track this unlinked/truncated file */
3000 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08003001 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003002 if (ret) {
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003003 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003004 if (reserve) {
3005 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3006 &BTRFS_I(inode)->runtime_flags);
3007 btrfs_orphan_release_metadata(inode);
3008 }
3009 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003010 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3011 &BTRFS_I(inode)->runtime_flags);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003012 btrfs_abort_transaction(trans, root, ret);
3013 return ret;
3014 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003015 }
3016 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003017 }
3018
3019 /* insert an orphan item to track subvolume contains orphan files */
3020 if (insert >= 2) {
3021 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
3022 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003023 if (ret && ret != -EEXIST) {
3024 btrfs_abort_transaction(trans, root, ret);
3025 return ret;
3026 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003027 }
3028 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003029}
3030
3031/*
3032 * We have done the truncate/delete so we can go ahead and remove the orphan
3033 * item for this particular inode.
3034 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003035static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3036 struct inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003037{
3038 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003039 int delete_item = 0;
3040 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003041 int ret = 0;
3042
Yan, Zhengd68fc572010-05-16 10:49:58 -04003043 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003044 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3045 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003046 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003047
Josef Bacik72ac3c02012-05-23 14:13:11 -04003048 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3049 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003050 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003051 spin_unlock(&root->orphan_lock);
3052
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003053 if (delete_item) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003054 atomic_dec(&root->orphan_inodes);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003055 if (trans)
3056 ret = btrfs_del_orphan_item(trans, root,
3057 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04003058 }
Josef Bacik7b128762008-07-24 12:17:14 -04003059
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003060 if (release_rsv)
3061 btrfs_orphan_release_metadata(inode);
3062
Josef Bacik4ef31a42013-08-13 14:10:08 -04003063 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003064}
3065
3066/*
3067 * this cleans up any orphans that may be left on the list from the last use
3068 * of this root.
3069 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003070int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003071{
3072 struct btrfs_path *path;
3073 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003074 struct btrfs_key key, found_key;
3075 struct btrfs_trans_handle *trans;
3076 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003077 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003078 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3079
Yan, Zhengd68fc572010-05-16 10:49:58 -04003080 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003081 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003082
3083 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003084 if (!path) {
3085 ret = -ENOMEM;
3086 goto out;
3087 }
Josef Bacik7b128762008-07-24 12:17:14 -04003088 path->reada = -1;
3089
3090 key.objectid = BTRFS_ORPHAN_OBJECTID;
3091 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
3092 key.offset = (u64)-1;
3093
Josef Bacik7b128762008-07-24 12:17:14 -04003094 while (1) {
3095 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003096 if (ret < 0)
3097 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003098
3099 /*
3100 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003101 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003102 * find the key and see if we have stuff that matches
3103 */
3104 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003105 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003106 if (path->slots[0] == 0)
3107 break;
3108 path->slots[0]--;
3109 }
3110
3111 /* pull out the item */
3112 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003113 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3114
3115 /* make sure the item matches what we want */
3116 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3117 break;
3118 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
3119 break;
3120
3121 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003122 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003123
3124 /*
3125 * this is where we are basically btrfs_lookup, without the
3126 * crossing root thing. we store the inode number in the
3127 * offset of the orphan item.
3128 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003129
3130 if (found_key.offset == last_objectid) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003131 btrfs_err(root->fs_info,
3132 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003133 ret = -EINVAL;
3134 goto out;
3135 }
3136
3137 last_objectid = found_key.offset;
3138
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003139 found_key.objectid = found_key.offset;
3140 found_key.type = BTRFS_INODE_ITEM_KEY;
3141 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00003142 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303143 ret = PTR_ERR_OR_ZERO(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003144 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003145 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003146
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003147 if (ret == -ESTALE && root == root->fs_info->tree_root) {
3148 struct btrfs_root *dead_root;
3149 struct btrfs_fs_info *fs_info = root->fs_info;
3150 int is_dead_root = 0;
3151
3152 /*
3153 * this is an orphan in the tree root. Currently these
3154 * could come from 2 sources:
3155 * a) a snapshot deletion in progress
3156 * b) a free space cache inode
3157 * We need to distinguish those two, as the snapshot
3158 * orphan must not get deleted.
3159 * find_dead_roots already ran before us, so if this
3160 * is a snapshot deletion, we should find the root
3161 * in the dead_roots list
3162 */
3163 spin_lock(&fs_info->trans_lock);
3164 list_for_each_entry(dead_root, &fs_info->dead_roots,
3165 root_list) {
3166 if (dead_root->root_key.objectid ==
3167 found_key.objectid) {
3168 is_dead_root = 1;
3169 break;
3170 }
3171 }
3172 spin_unlock(&fs_info->trans_lock);
3173 if (is_dead_root) {
3174 /* prevent this orphan from being found again */
3175 key.offset = found_key.objectid - 1;
3176 continue;
3177 }
3178 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003179 /*
3180 * Inode is already gone but the orphan item is still there,
3181 * kill the orphan item.
3182 */
3183 if (ret == -ESTALE) {
3184 trans = btrfs_start_transaction(root, 1);
3185 if (IS_ERR(trans)) {
3186 ret = PTR_ERR(trans);
3187 goto out;
3188 }
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003189 btrfs_debug(root->fs_info, "auto deleting %Lu",
3190 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003191 ret = btrfs_del_orphan_item(trans, root,
3192 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003193 btrfs_end_transaction(trans, root);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003194 if (ret)
3195 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003196 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003197 }
Josef Bacik7b128762008-07-24 12:17:14 -04003198
Josef Bacik7b128762008-07-24 12:17:14 -04003199 /*
3200 * add this inode to the orphan list so btrfs_orphan_del does
3201 * the proper thing when we hit it
3202 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003203 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3204 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003205 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003206
Josef Bacik7b128762008-07-24 12:17:14 -04003207 /* if we have links, this was a truncate, lets do that */
3208 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303209 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003210 iput(inode);
3211 continue;
3212 }
Josef Bacik7b128762008-07-24 12:17:14 -04003213 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003214
3215 /* 1 for the orphan item deletion. */
3216 trans = btrfs_start_transaction(root, 1);
3217 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003218 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003219 ret = PTR_ERR(trans);
3220 goto out;
3221 }
3222 ret = btrfs_orphan_add(trans, inode);
3223 btrfs_end_transaction(trans, root);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003224 if (ret) {
3225 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003226 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003227 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003228
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003229 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003230 if (ret)
3231 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003232 } else {
3233 nr_unlink++;
3234 }
3235
3236 /* this will do delete_inode and everything for us */
3237 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003238 if (ret)
3239 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003240 }
Miao Xie3254c872011-11-10 20:45:05 -05003241 /* release the path since we're done with it */
3242 btrfs_release_path(path);
3243
Yan, Zhengd68fc572010-05-16 10:49:58 -04003244 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3245
3246 if (root->orphan_block_rsv)
3247 btrfs_block_rsv_release(root, root->orphan_block_rsv,
3248 (u64)-1);
3249
3250 if (root->orphan_block_rsv || root->orphan_item_inserted) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003251 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003252 if (!IS_ERR(trans))
3253 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003254 }
Josef Bacik7b128762008-07-24 12:17:14 -04003255
3256 if (nr_unlink)
David Sterba4884b472013-03-20 13:31:27 +00003257 btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003258 if (nr_truncate)
David Sterba4884b472013-03-20 13:31:27 +00003259 btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003260
3261out:
3262 if (ret)
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003263 btrfs_crit(root->fs_info,
3264 "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003265 btrfs_free_path(path);
3266 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003267}
3268
Chris Masond352ac62008-09-29 15:18:18 -04003269/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003270 * very simple check to peek ahead in the leaf looking for xattrs. If we
3271 * don't find any xattrs, we know there can't be any acls.
3272 *
3273 * slot is the slot the inode is in, objectid is the objectid of the inode
3274 */
3275static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003276 int slot, u64 objectid,
3277 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003278{
3279 u32 nritems = btrfs_header_nritems(leaf);
3280 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003281 static u64 xattr_access = 0;
3282 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003283 int scanned = 0;
3284
Josef Bacikf23b5a52013-06-19 10:16:26 -04003285 if (!xattr_access) {
3286 xattr_access = btrfs_name_hash(POSIX_ACL_XATTR_ACCESS,
3287 strlen(POSIX_ACL_XATTR_ACCESS));
3288 xattr_default = btrfs_name_hash(POSIX_ACL_XATTR_DEFAULT,
3289 strlen(POSIX_ACL_XATTR_DEFAULT));
3290 }
3291
Chris Mason46a53cc2009-04-27 11:47:50 -04003292 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003293 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003294 while (slot < nritems) {
3295 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3296
3297 /* we found a different objectid, there must not be acls */
3298 if (found_key.objectid != objectid)
3299 return 0;
3300
3301 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003302 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003303 if (*first_xattr_slot == -1)
3304 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003305 if (found_key.offset == xattr_access ||
3306 found_key.offset == xattr_default)
3307 return 1;
3308 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003309
3310 /*
3311 * we found a key greater than an xattr key, there can't
3312 * be any acls later on
3313 */
3314 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3315 return 0;
3316
3317 slot++;
3318 scanned++;
3319
3320 /*
3321 * it goes inode, inode backrefs, xattrs, extents,
3322 * so if there are a ton of hard links to an inode there can
3323 * be a lot of backrefs. Don't waste time searching too hard,
3324 * this is just an optimization
3325 */
3326 if (scanned >= 8)
3327 break;
3328 }
3329 /* we hit the end of the leaf before we found an xattr or
3330 * something larger than an xattr. We have to assume the inode
3331 * has acls
3332 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003333 if (*first_xattr_slot == -1)
3334 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003335 return 1;
3336}
3337
3338/*
Chris Masond352ac62008-09-29 15:18:18 -04003339 * read an inode from the btree into the in-memory inode
3340 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003341static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003342{
3343 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003344 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003345 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04003346 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04003347 struct btrfs_root *root = BTRFS_I(inode)->root;
3348 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003349 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003350 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003351 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003352 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003353 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003354 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003355
3356 ret = btrfs_fill_inode(inode, &rdev);
3357 if (!ret)
3358 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003359
3360 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07003361 if (!path)
3362 goto make_bad;
3363
Chris Mason39279cc2007-06-12 06:35:45 -04003364 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003365
Chris Mason39279cc2007-06-12 06:35:45 -04003366 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003367 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04003368 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04003369
Chris Mason5f39d392007-10-15 16:14:19 -04003370 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003371
3372 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003373 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003374
Chris Mason5f39d392007-10-15 16:14:19 -04003375 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3376 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003377 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003378 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003379 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3380 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04003381 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003382
3383 tspec = btrfs_inode_atime(inode_item);
3384 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3385 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3386
3387 tspec = btrfs_inode_mtime(inode_item);
3388 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3389 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3390
3391 tspec = btrfs_inode_ctime(inode_item);
3392 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3393 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3394
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003395 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003396 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003397 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3398
3399 /*
3400 * If we were modified in the current generation and evicted from memory
3401 * and then re-read we need to do a full sync since we don't have any
3402 * idea about which extents were modified before we were evicted from
3403 * cache.
3404 */
3405 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
3406 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3407 &BTRFS_I(inode)->runtime_flags);
3408
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003409 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003410 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003411 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003412 rdev = btrfs_inode_rdev(leaf, inode_item);
3413
Josef Bacikaec74772008-07-24 12:12:38 -04003414 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003415 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003416
3417cache_index:
3418 path->slots[0]++;
3419 if (inode->i_nlink != 1 ||
3420 path->slots[0] >= btrfs_header_nritems(leaf))
3421 goto cache_acl;
3422
3423 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
3424 if (location.objectid != btrfs_ino(inode))
3425 goto cache_acl;
3426
3427 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3428 if (location.type == BTRFS_INODE_REF_KEY) {
3429 struct btrfs_inode_ref *ref;
3430
3431 ref = (struct btrfs_inode_ref *)ptr;
3432 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3433 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3434 struct btrfs_inode_extref *extref;
3435
3436 extref = (struct btrfs_inode_extref *)ptr;
3437 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3438 extref);
3439 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003440cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003441 /*
3442 * try to precache a NULL acl entry for files that don't have
3443 * any xattrs or acls
3444 */
Li Zefan33345d012011-04-20 10:31:50 +08003445 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003446 btrfs_ino(inode), &first_xattr_slot);
3447 if (first_xattr_slot != -1) {
3448 path->slots[0] = first_xattr_slot;
3449 ret = btrfs_load_inode_props(inode, path);
3450 if (ret)
3451 btrfs_err(root->fs_info,
3452 "error loading props for ino %llu (root %llu): %d\n",
3453 btrfs_ino(inode),
3454 root->root_key.objectid, ret);
3455 }
3456 btrfs_free_path(path);
3457
Al Viro72c04902009-06-24 16:58:48 -04003458 if (!maybe_acls)
3459 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003460
Chris Mason39279cc2007-06-12 06:35:45 -04003461 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003462 case S_IFREG:
3463 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003464 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05003465 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003466 inode->i_fop = &btrfs_file_operations;
3467 inode->i_op = &btrfs_file_inode_operations;
3468 break;
3469 case S_IFDIR:
3470 inode->i_fop = &btrfs_dir_file_operations;
3471 if (root == root->fs_info->tree_root)
3472 inode->i_op = &btrfs_dir_ro_inode_operations;
3473 else
3474 inode->i_op = &btrfs_dir_inode_operations;
3475 break;
3476 case S_IFLNK:
3477 inode->i_op = &btrfs_symlink_inode_operations;
3478 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04003479 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003480 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003481 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003482 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003483 init_special_inode(inode, inode->i_mode, rdev);
3484 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003485 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003486
3487 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003488 return;
3489
3490make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003491 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003492 make_bad_inode(inode);
3493}
3494
Chris Masond352ac62008-09-29 15:18:18 -04003495/*
3496 * given a leaf and an inode, copy the inode fields into the leaf
3497 */
Chris Masone02119d2008-09-05 16:13:11 -04003498static void fill_inode_item(struct btrfs_trans_handle *trans,
3499 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003500 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003501 struct inode *inode)
3502{
Liu Bo51fab692012-12-27 09:01:21 +00003503 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003504
Liu Bo51fab692012-12-27 09:01:21 +00003505 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003506
Liu Bo51fab692012-12-27 09:01:21 +00003507 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3508 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3509 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3510 &token);
3511 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3512 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003513
Liu Bo51fab692012-12-27 09:01:21 +00003514 btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item),
3515 inode->i_atime.tv_sec, &token);
3516 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item),
3517 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003518
Liu Bo51fab692012-12-27 09:01:21 +00003519 btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item),
3520 inode->i_mtime.tv_sec, &token);
3521 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item),
3522 inode->i_mtime.tv_nsec, &token);
3523
3524 btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item),
3525 inode->i_ctime.tv_sec, &token);
3526 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item),
3527 inode->i_ctime.tv_nsec, &token);
3528
3529 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3530 &token);
3531 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3532 &token);
3533 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3534 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3535 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3536 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3537 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003538}
3539
Chris Masond352ac62008-09-29 15:18:18 -04003540/*
3541 * copy everything in the in-memory inode into the btree.
3542 */
Chris Mason21151332011-11-10 20:39:08 -05003543static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003544 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003545{
3546 struct btrfs_inode_item *inode_item;
3547 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003548 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003549 int ret;
3550
3551 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003552 if (!path)
3553 return -ENOMEM;
3554
Chris Masonb9473432009-03-13 11:00:37 -04003555 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003556 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3557 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003558 if (ret) {
3559 if (ret > 0)
3560 ret = -ENOENT;
3561 goto failed;
3562 }
3563
Chris Mason5f39d392007-10-15 16:14:19 -04003564 leaf = path->nodes[0];
3565 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003566 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003567
Chris Masone02119d2008-09-05 16:13:11 -04003568 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003569 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003570 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003571 ret = 0;
3572failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003573 btrfs_free_path(path);
3574 return ret;
3575}
3576
Chris Masond352ac62008-09-29 15:18:18 -04003577/*
Chris Mason21151332011-11-10 20:39:08 -05003578 * copy everything in the in-memory inode into the btree.
3579 */
3580noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3581 struct btrfs_root *root, struct inode *inode)
3582{
3583 int ret;
3584
3585 /*
3586 * If the inode is a free space inode, we can deadlock during commit
3587 * if we put it into the delayed code.
3588 *
3589 * The data relocation inode should also be directly updated
3590 * without delay
3591 */
Liu Bo83eea1f2012-07-10 05:28:39 -06003592 if (!btrfs_is_free_space_inode(inode)
Chris Mason21151332011-11-10 20:39:08 -05003593 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003594 btrfs_update_root_times(trans, root);
3595
Chris Mason21151332011-11-10 20:39:08 -05003596 ret = btrfs_delayed_update_inode(trans, root, inode);
3597 if (!ret)
3598 btrfs_set_inode_last_trans(trans, inode);
3599 return ret;
3600 }
3601
3602 return btrfs_update_inode_item(trans, root, inode);
3603}
3604
Josef Bacikbe6aef62012-10-22 15:43:12 -04003605noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3606 struct btrfs_root *root,
3607 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003608{
3609 int ret;
3610
3611 ret = btrfs_update_inode(trans, root, inode);
3612 if (ret == -ENOSPC)
3613 return btrfs_update_inode_item(trans, root, inode);
3614 return ret;
3615}
3616
3617/*
Chris Masond352ac62008-09-29 15:18:18 -04003618 * unlink helper that gets used here in inode.c and in the tree logging
3619 * recovery code. It remove a link in a directory with a given name, and
3620 * also drops the back refs in the inode to the directory
3621 */
Al Viro92986792011-03-04 17:14:37 +00003622static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3623 struct btrfs_root *root,
3624 struct inode *dir, struct inode *inode,
3625 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003626{
3627 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003628 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003629 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003630 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003631 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003632 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003633 u64 ino = btrfs_ino(inode);
3634 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003635
3636 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003637 if (!path) {
3638 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003639 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003640 }
3641
Chris Masonb9473432009-03-13 11:00:37 -04003642 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003643 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003644 name, name_len, -1);
3645 if (IS_ERR(di)) {
3646 ret = PTR_ERR(di);
3647 goto err;
3648 }
3649 if (!di) {
3650 ret = -ENOENT;
3651 goto err;
3652 }
Chris Mason5f39d392007-10-15 16:14:19 -04003653 leaf = path->nodes[0];
3654 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003655 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003656 if (ret)
3657 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003658 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003659
Miao Xie67de1172013-12-26 13:07:06 +08003660 /*
3661 * If we don't have dir index, we have to get it by looking up
3662 * the inode ref, since we get the inode ref, remove it directly,
3663 * it is unnecessary to do delayed deletion.
3664 *
3665 * But if we have dir index, needn't search inode ref to get it.
3666 * Since the inode ref is close to the inode item, it is better
3667 * that we delay to delete it, and just do this deletion when
3668 * we update the inode item.
3669 */
3670 if (BTRFS_I(inode)->dir_index) {
3671 ret = btrfs_delayed_delete_inode_ref(inode);
3672 if (!ret) {
3673 index = BTRFS_I(inode)->dir_index;
3674 goto skip_backref;
3675 }
3676 }
3677
Li Zefan33345d012011-04-20 10:31:50 +08003678 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3679 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003680 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003681 btrfs_info(root->fs_info,
3682 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003683 name_len, name, ino, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003684 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003685 goto err;
3686 }
Miao Xie67de1172013-12-26 13:07:06 +08003687skip_backref:
Miao Xie16cdcec2011-04-22 18:12:22 +08003688 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003689 if (ret) {
3690 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003691 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003692 }
Chris Mason39279cc2007-06-12 06:35:45 -04003693
Chris Masone02119d2008-09-05 16:13:11 -04003694 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08003695 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003696 if (ret != 0 && ret != -ENOENT) {
3697 btrfs_abort_transaction(trans, root, ret);
3698 goto err;
3699 }
Chris Masone02119d2008-09-05 16:13:11 -04003700
3701 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
3702 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04003703 if (ret == -ENOENT)
3704 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003705 else if (ret)
3706 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003707err:
3708 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003709 if (ret)
3710 goto out;
3711
3712 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003713 inode_inc_iversion(inode);
3714 inode_inc_iversion(dir);
Chris Masone02119d2008-09-05 16:13:11 -04003715 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Tsutomu Itohb9959292012-06-25 21:25:22 -06003716 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04003717out:
Chris Mason39279cc2007-06-12 06:35:45 -04003718 return ret;
3719}
3720
Al Viro92986792011-03-04 17:14:37 +00003721int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3722 struct btrfs_root *root,
3723 struct inode *dir, struct inode *inode,
3724 const char *name, int name_len)
3725{
3726 int ret;
3727 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3728 if (!ret) {
Zach Brown8b558c52013-10-16 12:10:34 -07003729 drop_nlink(inode);
Al Viro92986792011-03-04 17:14:37 +00003730 ret = btrfs_update_inode(trans, root, inode);
3731 }
3732 return ret;
3733}
Yan, Zhenga22285a2010-05-16 10:48:46 -04003734
3735/*
3736 * helper to start transaction for unlink and rmdir.
3737 *
Josef Bacikd52be812013-05-29 14:54:47 -04003738 * unlink and rmdir are special in btrfs, they do not always free space, so
3739 * if we cannot make our reservations the normal way try and see if there is
3740 * plenty of slack room in the global reserve to migrate, otherwise we cannot
3741 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04003742 */
Josef Bacikd52be812013-05-29 14:54:47 -04003743static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003744{
3745 struct btrfs_trans_handle *trans;
3746 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003747 int ret;
3748
Josef Bacike70bea52011-10-11 14:18:24 -04003749 /*
3750 * 1 for the possible orphan item
3751 * 1 for the dir item
3752 * 1 for the dir index
3753 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04003754 * 1 for the inode
3755 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04003756 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003757 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
3758 return trans;
3759
Josef Bacikd52be812013-05-29 14:54:47 -04003760 if (PTR_ERR(trans) == -ENOSPC) {
3761 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003762
Josef Bacikd52be812013-05-29 14:54:47 -04003763 trans = btrfs_start_transaction(root, 0);
3764 if (IS_ERR(trans))
3765 return trans;
3766 ret = btrfs_cond_migrate_bytes(root->fs_info,
3767 &root->fs_info->trans_block_rsv,
3768 num_bytes, 5);
3769 if (ret) {
3770 btrfs_end_transaction(trans, root);
3771 return ERR_PTR(ret);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003772 }
Josef Bacik5a77d762011-11-01 14:32:23 -04003773 trans->block_rsv = &root->fs_info->trans_block_rsv;
Josef Bacikd52be812013-05-29 14:54:47 -04003774 trans->bytes_reserved = num_bytes;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003775 }
Josef Bacikd52be812013-05-29 14:54:47 -04003776 return trans;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003777}
3778
Chris Mason39279cc2007-06-12 06:35:45 -04003779static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3780{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003781 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003782 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04003783 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003784 int ret;
3785
Josef Bacikd52be812013-05-29 14:54:47 -04003786 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003787 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003788 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003789
Chris Mason12fcfd22009-03-24 10:24:20 -04003790 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
3791
Chris Masone02119d2008-09-05 16:13:11 -04003792 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3793 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003794 if (ret)
3795 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003796
Yan, Zhenga22285a2010-05-16 10:48:46 -04003797 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04003798 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003799 if (ret)
3800 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003801 }
Josef Bacik7b128762008-07-24 12:17:14 -04003802
Tsutomu Itohb5324022011-07-19 07:27:20 +00003803out:
Josef Bacikd52be812013-05-29 14:54:47 -04003804 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003805 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04003806 return ret;
3807}
3808
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003809int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3810 struct btrfs_root *root,
3811 struct inode *dir, u64 objectid,
3812 const char *name, int name_len)
3813{
3814 struct btrfs_path *path;
3815 struct extent_buffer *leaf;
3816 struct btrfs_dir_item *di;
3817 struct btrfs_key key;
3818 u64 index;
3819 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003820 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003821
3822 path = btrfs_alloc_path();
3823 if (!path)
3824 return -ENOMEM;
3825
Li Zefan33345d012011-04-20 10:31:50 +08003826 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003827 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003828 if (IS_ERR_OR_NULL(di)) {
3829 if (!di)
3830 ret = -ENOENT;
3831 else
3832 ret = PTR_ERR(di);
3833 goto out;
3834 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003835
3836 leaf = path->nodes[0];
3837 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3838 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3839 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003840 if (ret) {
3841 btrfs_abort_transaction(trans, root, ret);
3842 goto out;
3843 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003844 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003845
3846 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3847 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003848 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003849 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003850 if (ret != -ENOENT) {
3851 btrfs_abort_transaction(trans, root, ret);
3852 goto out;
3853 }
Li Zefan33345d012011-04-20 10:31:50 +08003854 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003855 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003856 if (IS_ERR_OR_NULL(di)) {
3857 if (!di)
3858 ret = -ENOENT;
3859 else
3860 ret = PTR_ERR(di);
3861 btrfs_abort_transaction(trans, root, ret);
3862 goto out;
3863 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003864
3865 leaf = path->nodes[0];
3866 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003867 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003868 index = key.offset;
3869 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003870 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003871
Miao Xie16cdcec2011-04-22 18:12:22 +08003872 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003873 if (ret) {
3874 btrfs_abort_transaction(trans, root, ret);
3875 goto out;
3876 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003877
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003878 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003879 inode_inc_iversion(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003880 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Josef Bacik5a24e842012-08-08 10:12:59 -06003881 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003882 if (ret)
3883 btrfs_abort_transaction(trans, root, ret);
3884out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04003885 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003886 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003887}
3888
Chris Mason39279cc2007-06-12 06:35:45 -04003889static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3890{
3891 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003892 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003893 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003894 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04003895
David Sterbab3ae2442012-09-13 16:04:34 -06003896 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04003897 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06003898 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
3899 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04003900
Josef Bacikd52be812013-05-29 14:54:47 -04003901 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003902 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003903 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003904
Li Zefan33345d012011-04-20 10:31:50 +08003905 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003906 err = btrfs_unlink_subvol(trans, root, dir,
3907 BTRFS_I(inode)->location.objectid,
3908 dentry->d_name.name,
3909 dentry->d_name.len);
3910 goto out;
3911 }
3912
Josef Bacik7b128762008-07-24 12:17:14 -04003913 err = btrfs_orphan_add(trans, inode);
3914 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003915 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003916
Chris Mason39279cc2007-06-12 06:35:45 -04003917 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04003918 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3919 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05003920 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04003921 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003922out:
Josef Bacikd52be812013-05-29 14:54:47 -04003923 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003924 btrfs_btree_balance_dirty(root);
Chris Mason39544012007-12-12 14:38:19 -05003925
Chris Mason39279cc2007-06-12 06:35:45 -04003926 return err;
3927}
3928
Chris Mason323ac952008-10-01 19:05:46 -04003929/*
Chris Mason39279cc2007-06-12 06:35:45 -04003930 * this can truncate away extent items, csum items and directory items.
3931 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04003932 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04003933 *
3934 * csum items that cross the new i_size are truncated to the new size
3935 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04003936 *
3937 * min_type is the minimum key type to truncate down to. If set to 0, this
3938 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04003939 */
Yan, Zheng80825102009-11-12 09:35:36 +00003940int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
3941 struct btrfs_root *root,
3942 struct inode *inode,
3943 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003944{
Chris Mason39279cc2007-06-12 06:35:45 -04003945 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003946 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003947 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00003948 struct btrfs_key key;
3949 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04003950 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04003951 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003952 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003953 u64 item_end = 0;
Josef Bacik7f4f6e02013-08-29 16:43:28 -04003954 u64 last_size = (u64)-1;
Yan, Zheng80825102009-11-12 09:35:36 +00003955 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04003956 int found_extent;
3957 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003958 int pending_del_nr = 0;
3959 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04003960 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00003961 int ret;
3962 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08003963 u64 ino = btrfs_ino(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003964
3965 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04003966
Mark Fasheh0eb0e192011-07-12 16:44:10 -07003967 path = btrfs_alloc_path();
3968 if (!path)
3969 return -ENOMEM;
3970 path->reada = -1;
3971
Josef Bacik5dc562c2012-08-17 13:14:17 -04003972 /*
3973 * We want to drop from the next block forward in case this new size is
3974 * not block aligned since we will be keeping the last block of the
3975 * extent just the way it is.
3976 */
Josef Bacik0af3d002010-06-21 14:48:16 -04003977 if (root->ref_cows || root == root->fs_info->tree_root)
Qu Wenruofda28322013-02-26 08:10:22 +00003978 btrfs_drop_extent_cache(inode, ALIGN(new_size,
3979 root->sectorsize), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003980
Miao Xie16cdcec2011-04-22 18:12:22 +08003981 /*
3982 * This function is also used to drop the items in the log tree before
3983 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
3984 * it is used to drop the loged items. So we shouldn't kill the delayed
3985 * items.
3986 */
3987 if (min_type == 0 && root == BTRFS_I(inode)->root)
3988 btrfs_kill_delayed_inode_items(inode);
3989
Li Zefan33345d012011-04-20 10:31:50 +08003990 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04003991 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04003992 key.type = (u8)-1;
3993
Chris Mason85e21ba2008-01-29 15:11:36 -05003994search_again:
Chris Masonb9473432009-03-13 11:00:37 -04003995 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05003996 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00003997 if (ret < 0) {
3998 err = ret;
3999 goto out;
4000 }
Chris Masond3977122009-01-05 21:25:51 -05004001
Chris Mason85e21ba2008-01-29 15:11:36 -05004002 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004003 /* there are no items in the tree for us to truncate, we're
4004 * done
4005 */
Yan, Zheng80825102009-11-12 09:35:36 +00004006 if (path->slots[0] == 0)
4007 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004008 path->slots[0]--;
4009 }
4010
Chris Masond3977122009-01-05 21:25:51 -05004011 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004012 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004013 leaf = path->nodes[0];
4014 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4015 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04004016
Li Zefan33345d012011-04-20 10:31:50 +08004017 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004018 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004019
Chris Mason85e21ba2008-01-29 15:11:36 -05004020 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004021 break;
4022
Chris Mason5f39d392007-10-15 16:14:19 -04004023 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004024 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004025 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004026 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004027 extent_type = btrfs_file_extent_type(leaf, fi);
4028 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004029 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004030 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04004031 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004032 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004033 path->slots[0], fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004034 }
Yan008630c2007-11-07 13:31:09 -05004035 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004036 }
Yan, Zheng80825102009-11-12 09:35:36 +00004037 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004038 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004039 } else {
4040 if (item_end < new_size)
4041 break;
4042 if (found_key.offset >= new_size)
4043 del_item = 1;
4044 else
4045 del_item = 0;
4046 }
Chris Mason39279cc2007-06-12 06:35:45 -04004047 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004048 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004049 if (found_type != BTRFS_EXTENT_DATA_KEY)
4050 goto delete;
4051
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004052 if (del_item)
4053 last_size = found_key.offset;
4054 else
4055 last_size = new_size;
4056
Chris Mason179e29e2007-11-01 11:28:41 -04004057 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004058 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004059 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004060 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004061 u64 orig_num_bytes =
4062 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004063 extent_num_bytes = ALIGN(new_size -
4064 found_key.offset,
4065 root->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004066 btrfs_set_file_extent_num_bytes(leaf, fi,
4067 extent_num_bytes);
4068 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004069 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04004070 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004071 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004072 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004073 } else {
Chris Masondb945352007-10-15 16:15:53 -04004074 extent_num_bytes =
4075 btrfs_file_extent_disk_num_bytes(leaf,
4076 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004077 extent_offset = found_key.offset -
4078 btrfs_file_extent_offset(leaf, fi);
4079
Chris Mason39279cc2007-06-12 06:35:45 -04004080 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004081 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004082 if (extent_start != 0) {
4083 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04004084 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004085 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004086 }
4087 }
Chris Mason90692182008-02-08 13:49:28 -05004088 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004089 /*
4090 * we can't truncate inline items that have had
4091 * special encodings
4092 */
4093 if (!del_item &&
4094 btrfs_file_extent_compression(leaf, fi) == 0 &&
4095 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4096 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004097 u32 size = new_size - found_key.offset;
4098
4099 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004100 inode_sub_bytes(inode, item_end + 1 -
4101 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04004102 }
Chris Mason514ac8a2014-01-03 21:07:00 -08004103
4104 /*
4105 * update the ram bytes to properly reflect
4106 * the new size of our item
4107 */
4108 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
Chris Masone02119d2008-09-05 16:13:11 -04004109 size =
4110 btrfs_file_extent_calc_inline_size(size);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004111 btrfs_truncate_item(root, path, size, 1);
Chris Masone02119d2008-09-05 16:13:11 -04004112 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004113 inode_sub_bytes(inode, item_end + 1 -
4114 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05004115 }
Chris Mason39279cc2007-06-12 06:35:45 -04004116 }
Chris Mason179e29e2007-11-01 11:28:41 -04004117delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004118 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004119 if (!pending_del_nr) {
4120 /* no pending yet, add ourselves */
4121 pending_del_slot = path->slots[0];
4122 pending_del_nr = 1;
4123 } else if (pending_del_nr &&
4124 path->slots[0] + 1 == pending_del_slot) {
4125 /* hop on the pending chunk */
4126 pending_del_nr++;
4127 pending_del_slot = path->slots[0];
4128 } else {
Chris Masond3977122009-01-05 21:25:51 -05004129 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004130 }
Chris Mason39279cc2007-06-12 06:35:45 -04004131 } else {
4132 break;
4133 }
Josef Bacik0af3d002010-06-21 14:48:16 -04004134 if (found_extent && (root->ref_cows ||
4135 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004136 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004137 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004138 extent_num_bytes, 0,
4139 btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02004140 ino, extent_offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004141 BUG_ON(ret);
4142 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004143
Yan, Zheng80825102009-11-12 09:35:36 +00004144 if (found_type == BTRFS_INODE_ITEM_KEY)
4145 break;
4146
4147 if (path->slots[0] == 0 ||
4148 path->slots[0] != pending_del_slot) {
Yan, Zheng80825102009-11-12 09:35:36 +00004149 if (pending_del_nr) {
4150 ret = btrfs_del_items(trans, root, path,
4151 pending_del_slot,
4152 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004153 if (ret) {
4154 btrfs_abort_transaction(trans,
4155 root, ret);
4156 goto error;
4157 }
Yan, Zheng80825102009-11-12 09:35:36 +00004158 pending_del_nr = 0;
4159 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004160 btrfs_release_path(path);
Chris Mason85e21ba2008-01-29 15:11:36 -05004161 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004162 } else {
4163 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004164 }
Chris Mason39279cc2007-06-12 06:35:45 -04004165 }
Yan, Zheng80825102009-11-12 09:35:36 +00004166out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004167 if (pending_del_nr) {
4168 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4169 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004170 if (ret)
4171 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004172 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004173error:
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004174 if (last_size != (u64)-1)
4175 btrfs_ordered_update_i_size(inode, last_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004176 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00004177 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004178}
4179
Chris Masona52d9a82007-08-27 16:49:44 -04004180/*
Josef Bacik2aaa6652012-08-29 14:27:18 -04004181 * btrfs_truncate_page - read, zero a chunk and write a page
4182 * @inode - inode that we're zeroing
4183 * @from - the offset to start zeroing
4184 * @len - the length to zero, 0 to zero the entire range respective to the
4185 * offset
4186 * @front - zero up to the offset instead of from the offset on
4187 *
4188 * This will find the page for the "from" offset and cow the page and zero the
4189 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004190 */
Josef Bacik2aaa6652012-08-29 14:27:18 -04004191int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
4192 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004193{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004194 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04004195 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004196 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4197 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004198 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004199 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04004200 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04004201 pgoff_t index = from >> PAGE_CACHE_SHIFT;
4202 unsigned offset = from & (PAGE_CACHE_SIZE-1);
4203 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004204 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004205 int ret = 0;
4206 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004207 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004208
Josef Bacik2aaa6652012-08-29 14:27:18 -04004209 if ((offset & (blocksize - 1)) == 0 &&
4210 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004211 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004212 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004213 if (ret)
4214 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004215
Chris Mason211c17f2008-05-15 09:13:45 -04004216again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004217 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004218 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004219 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Miao Xieac6a2b32012-12-05 10:56:13 +00004220 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004221 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004222 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004223
4224 page_start = page_offset(page);
4225 page_end = page_start + PAGE_CACHE_SIZE - 1;
4226
Chris Masona52d9a82007-08-27 16:49:44 -04004227 if (!PageUptodate(page)) {
4228 ret = btrfs_readpage(NULL, page);
4229 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004230 if (page->mapping != mapping) {
4231 unlock_page(page);
4232 page_cache_release(page);
4233 goto again;
4234 }
Chris Masona52d9a82007-08-27 16:49:44 -04004235 if (!PageUptodate(page)) {
4236 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004237 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004238 }
4239 }
Chris Mason211c17f2008-05-15 09:13:45 -04004240 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004241
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004242 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004243 set_page_extent_mapped(page);
4244
4245 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4246 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004247 unlock_extent_cached(io_tree, page_start, page_end,
4248 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004249 unlock_page(page);
4250 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004251 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004252 btrfs_put_ordered_extent(ordered);
4253 goto again;
4254 }
4255
Josef Bacik2ac55d42010-02-03 19:33:23 +00004256 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004257 EXTENT_DIRTY | EXTENT_DELALLOC |
4258 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004259 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004260
Josef Bacik2ac55d42010-02-03 19:33:23 +00004261 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
4262 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004263 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004264 unlock_extent_cached(io_tree, page_start, page_end,
4265 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004266 goto out_unlock;
4267 }
4268
Chris Masone6dcd2d2008-07-17 12:53:50 -04004269 if (offset != PAGE_CACHE_SIZE) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004270 if (!len)
4271 len = PAGE_CACHE_SIZE - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004272 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004273 if (front)
4274 memset(kaddr, 0, offset);
4275 else
4276 memset(kaddr + offset, 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004277 flush_dcache_page(page);
4278 kunmap(page);
4279 }
Chris Mason247e7432008-07-17 12:53:51 -04004280 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004281 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004282 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
4283 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004284
Chris Mason89642222008-07-24 09:41:53 -04004285out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004286 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004287 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04004288 unlock_page(page);
4289 page_cache_release(page);
4290out:
4291 return ret;
4292}
4293
Josef Bacik16e75492013-10-22 12:18:51 -04004294static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4295 u64 offset, u64 len)
4296{
4297 struct btrfs_trans_handle *trans;
4298 int ret;
4299
4300 /*
4301 * Still need to make sure the inode looks like it's been updated so
4302 * that any holes get logged if we fsync.
4303 */
4304 if (btrfs_fs_incompat(root->fs_info, NO_HOLES)) {
4305 BTRFS_I(inode)->last_trans = root->fs_info->generation;
4306 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4307 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4308 return 0;
4309 }
4310
4311 /*
4312 * 1 - for the one we're dropping
4313 * 1 - for the one we're adding
4314 * 1 - for updating the inode.
4315 */
4316 trans = btrfs_start_transaction(root, 3);
4317 if (IS_ERR(trans))
4318 return PTR_ERR(trans);
4319
4320 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4321 if (ret) {
4322 btrfs_abort_transaction(trans, root, ret);
4323 btrfs_end_transaction(trans, root);
4324 return ret;
4325 }
4326
4327 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset,
4328 0, 0, len, 0, len, 0, 0, 0);
4329 if (ret)
4330 btrfs_abort_transaction(trans, root, ret);
4331 else
4332 btrfs_update_inode(trans, root, inode);
4333 btrfs_end_transaction(trans, root);
4334 return ret;
4335}
4336
Josef Bacik695a0d02011-03-04 15:46:53 -05004337/*
4338 * This function puts in dummy file extents for the area we're creating a hole
4339 * for. So if we are truncating this file to a larger size we need to insert
4340 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4341 * the range between oldsize and size
4342 */
Josef Bacika41ad392011-01-31 15:30:16 -05004343int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004344{
Yan Zheng9036c102008-10-30 14:19:41 -04004345 struct btrfs_root *root = BTRFS_I(inode)->root;
4346 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004347 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004348 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004349 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Qu Wenruofda28322013-02-26 08:10:22 +00004350 u64 hole_start = ALIGN(oldsize, root->sectorsize);
4351 u64 block_end = ALIGN(size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004352 u64 last_byte;
4353 u64 cur_offset;
4354 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004355 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004356
Josef Bacika71754f2013-06-17 17:14:39 -04004357 /*
4358 * If our size started in the middle of a page we need to zero out the
4359 * rest of the page before we expand the i_size, otherwise we could
4360 * expose stale data.
4361 */
4362 err = btrfs_truncate_page(inode, oldsize, 0, 0);
4363 if (err)
4364 return err;
4365
Yan Zheng9036c102008-10-30 14:19:41 -04004366 if (size <= hole_start)
4367 return 0;
4368
Yan Zheng9036c102008-10-30 14:19:41 -04004369 while (1) {
4370 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004371
Josef Bacik2ac55d42010-02-03 19:33:23 +00004372 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004373 &cached_state);
Miao Xiefa7c1492013-09-26 13:15:27 +08004374 ordered = btrfs_lookup_ordered_range(inode, hole_start,
4375 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004376 if (!ordered)
4377 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004378 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4379 &cached_state, GFP_NOFS);
Miao Xiefa7c1492013-09-26 13:15:27 +08004380 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004381 btrfs_put_ordered_extent(ordered);
4382 }
4383
Yan Zheng9036c102008-10-30 14:19:41 -04004384 cur_offset = hole_start;
4385 while (1) {
4386 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4387 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004388 if (IS_ERR(em)) {
4389 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004390 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004391 break;
4392 }
Yan Zheng9036c102008-10-30 14:19:41 -04004393 last_byte = min(extent_map_end(em), block_end);
Qu Wenruofda28322013-02-26 08:10:22 +00004394 last_byte = ALIGN(last_byte , root->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004395 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004396 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004397 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004398
Josef Bacik16e75492013-10-22 12:18:51 -04004399 err = maybe_insert_hole(root, inode, cur_offset,
4400 hole_size);
4401 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004402 break;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004403 btrfs_drop_extent_cache(inode, cur_offset,
4404 cur_offset + hole_size - 1, 0);
4405 hole_em = alloc_extent_map();
4406 if (!hole_em) {
4407 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4408 &BTRFS_I(inode)->runtime_flags);
4409 goto next;
4410 }
4411 hole_em->start = cur_offset;
4412 hole_em->len = hole_size;
4413 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004414
Josef Bacik5dc562c2012-08-17 13:14:17 -04004415 hole_em->block_start = EXTENT_MAP_HOLE;
4416 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004417 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004418 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004419 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
4420 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Josef Bacik16e75492013-10-22 12:18:51 -04004421 hole_em->generation = root->fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004422
4423 while (1) {
4424 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004425 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004426 write_unlock(&em_tree->lock);
4427 if (err != -EEXIST)
4428 break;
4429 btrfs_drop_extent_cache(inode, cur_offset,
4430 cur_offset +
4431 hole_size - 1, 0);
4432 }
4433 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04004434 }
Josef Bacik16e75492013-10-22 12:18:51 -04004435next:
Yan Zheng9036c102008-10-30 14:19:41 -04004436 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004437 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004438 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004439 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004440 break;
4441 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004442 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004443 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4444 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004445 return err;
4446}
4447
Eric Sandeen3972f262013-01-12 02:57:22 +00004448static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004449{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004450 struct btrfs_root *root = BTRFS_I(inode)->root;
4451 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004452 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004453 loff_t newsize = attr->ia_size;
4454 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004455 int ret;
4456
Eric Sandeen3972f262013-01-12 02:57:22 +00004457 /*
4458 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4459 * special case where we need to update the times despite not having
4460 * these flags set. For all other operations the VFS set these flags
4461 * explicitly if it wants a timestamp update.
4462 */
4463 if (newsize != oldsize && (!(mask & (ATTR_CTIME | ATTR_MTIME))))
4464 inode->i_ctime = inode->i_mtime = current_fs_time(inode->i_sb);
4465
Josef Bacika41ad392011-01-31 15:30:16 -05004466 if (newsize > oldsize) {
Kirill A. Shutemov7caef262013-09-12 15:13:56 -07004467 truncate_pagecache(inode, newsize);
Josef Bacika41ad392011-01-31 15:30:16 -05004468 ret = btrfs_cont_expand(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004469 if (ret)
Yan, Zheng80825102009-11-12 09:35:36 +00004470 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004471
Miao Xief4a2f4c2011-12-14 20:12:01 -05004472 trans = btrfs_start_transaction(root, 1);
4473 if (IS_ERR(trans))
4474 return PTR_ERR(trans);
4475
4476 i_size_write(inode, newsize);
4477 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
4478 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004479 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05004480 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004481
Josef Bacika41ad392011-01-31 15:30:16 -05004482 /*
4483 * We're truncating a file that used to have good data down to
4484 * zero. Make sure it gets into the ordered flush list so that
4485 * any new writes get down to disk quickly.
4486 */
4487 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004488 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4489 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004490
Josef Bacikf3fe8202013-01-07 17:03:21 -05004491 /*
4492 * 1 for the orphan item we're going to add
4493 * 1 for the orphan item deletion.
4494 */
4495 trans = btrfs_start_transaction(root, 2);
4496 if (IS_ERR(trans))
4497 return PTR_ERR(trans);
4498
4499 /*
4500 * We need to do this in case we fail at _any_ point during the
4501 * actual truncate. Once we do the truncate_setsize we could
4502 * invalidate pages which forces any outstanding ordered io to
4503 * be instantly completed which will give us extents that need
4504 * to be truncated. If we fail to get an orphan inode down we
4505 * could have left over extents that were never meant to live,
4506 * so we need to garuntee from this point on that everything
4507 * will be consistent.
4508 */
4509 ret = btrfs_orphan_add(trans, inode);
4510 btrfs_end_transaction(trans, root);
4511 if (ret)
4512 return ret;
4513
Josef Bacika41ad392011-01-31 15:30:16 -05004514 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4515 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004516
4517 /* Disable nonlocked read DIO to avoid the end less truncate */
4518 btrfs_inode_block_unlocked_dio(inode);
4519 inode_dio_wait(inode);
4520 btrfs_inode_resume_unlocked_dio(inode);
4521
Josef Bacika41ad392011-01-31 15:30:16 -05004522 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004523 if (ret && inode->i_nlink) {
4524 int err;
4525
4526 /*
4527 * failed to truncate, disk_i_size is only adjusted down
4528 * as we remove extents, so it should represent the true
4529 * size of the inode, so reset the in memory size and
4530 * delete our orphan entry.
4531 */
4532 trans = btrfs_join_transaction(root);
4533 if (IS_ERR(trans)) {
4534 btrfs_orphan_del(NULL, inode);
4535 return ret;
4536 }
4537 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
4538 err = btrfs_orphan_del(trans, inode);
4539 if (err)
4540 btrfs_abort_transaction(trans, root, err);
4541 btrfs_end_transaction(trans, root);
4542 }
Yan, Zheng80825102009-11-12 09:35:36 +00004543 }
4544
Josef Bacika41ad392011-01-31 15:30:16 -05004545 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004546}
4547
Chris Mason39279cc2007-06-12 06:35:45 -04004548static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
4549{
4550 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08004551 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004552 int err;
4553
Li Zefanb83cc962010-12-20 16:04:08 +08004554 if (btrfs_root_readonly(root))
4555 return -EROFS;
4556
Chris Mason39279cc2007-06-12 06:35:45 -04004557 err = inode_change_ok(inode, attr);
4558 if (err)
4559 return err;
4560
Chris Mason5a3f23d2009-03-31 13:27:11 -04004561 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00004562 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00004563 if (err)
4564 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004565 }
Yan Zheng9036c102008-10-30 14:19:41 -04004566
Christoph Hellwig10257742010-06-04 11:30:02 +02004567 if (attr->ia_valid) {
4568 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004569 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004570 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04004571
Josef Bacik22c44fe2011-11-30 10:45:38 -05004572 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig10257742010-06-04 11:30:02 +02004573 err = btrfs_acl_chmod(inode);
4574 }
4575
Chris Mason39279cc2007-06-12 06:35:45 -04004576 return err;
4577}
Chris Mason61295eb2008-01-14 16:24:38 -05004578
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004579/*
4580 * While truncating the inode pages during eviction, we get the VFS calling
4581 * btrfs_invalidatepage() against each page of the inode. This is slow because
4582 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
4583 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
4584 * extent_state structures over and over, wasting lots of time.
4585 *
4586 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
4587 * those expensive operations on a per page basis and do only the ordered io
4588 * finishing, while we release here the extent_map and extent_state structures,
4589 * without the excessive merging and splitting.
4590 */
4591static void evict_inode_truncate_pages(struct inode *inode)
4592{
4593 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4594 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
4595 struct rb_node *node;
4596
4597 ASSERT(inode->i_state & I_FREEING);
4598 truncate_inode_pages(&inode->i_data, 0);
4599
4600 write_lock(&map_tree->lock);
4601 while (!RB_EMPTY_ROOT(&map_tree->map)) {
4602 struct extent_map *em;
4603
4604 node = rb_first(&map_tree->map);
4605 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08004606 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
4607 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004608 remove_extent_mapping(map_tree, em);
4609 free_extent_map(em);
4610 }
4611 write_unlock(&map_tree->lock);
4612
4613 spin_lock(&io_tree->lock);
4614 while (!RB_EMPTY_ROOT(&io_tree->state)) {
4615 struct extent_state *state;
4616 struct extent_state *cached_state = NULL;
4617
4618 node = rb_first(&io_tree->state);
4619 state = rb_entry(node, struct extent_state, rb_node);
4620 atomic_inc(&state->refs);
4621 spin_unlock(&io_tree->lock);
4622
4623 lock_extent_bits(io_tree, state->start, state->end,
4624 0, &cached_state);
4625 clear_extent_bit(io_tree, state->start, state->end,
4626 EXTENT_LOCKED | EXTENT_DIRTY |
4627 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
4628 EXTENT_DEFRAG, 1, 1,
4629 &cached_state, GFP_NOFS);
4630 free_extent_state(state);
4631
4632 spin_lock(&io_tree->lock);
4633 }
4634 spin_unlock(&io_tree->lock);
4635}
4636
Al Virobd555972010-06-07 11:35:40 -04004637void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004638{
4639 struct btrfs_trans_handle *trans;
4640 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04004641 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik07127182011-08-19 10:29:59 -04004642 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04004643 int ret;
4644
liubo1abe9b82011-03-24 11:18:59 +00004645 trace_btrfs_inode_evict(inode);
4646
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004647 evict_inode_truncate_pages(inode);
4648
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02004649 if (inode->i_nlink &&
4650 ((btrfs_root_refs(&root->root_item) != 0 &&
4651 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
4652 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04004653 goto no_delete;
4654
Chris Mason39279cc2007-06-12 06:35:45 -04004655 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04004656 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004657 goto no_delete;
4658 }
Al Virobd555972010-06-07 11:35:40 -04004659 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04004660 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04004661
Yan, Zhengc71bf092009-11-12 09:34:40 +00004662 if (root->fs_info->log_root_recovering) {
Liu Bo6bf02312012-06-25 21:59:09 -06004663 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04004664 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00004665 goto no_delete;
4666 }
4667
Yan, Zheng76dda932009-09-21 16:00:26 -04004668 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02004669 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
4670 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04004671 goto no_delete;
4672 }
4673
Miao Xie0e8c36a2012-12-19 06:59:51 +00004674 ret = btrfs_commit_inode_delayed_inode(inode);
4675 if (ret) {
4676 btrfs_orphan_del(NULL, inode);
4677 goto no_delete;
4678 }
4679
Miao Xie66d8f3d2012-09-06 04:02:28 -06004680 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04004681 if (!rsv) {
4682 btrfs_orphan_del(NULL, inode);
4683 goto no_delete;
4684 }
Josef Bacik4a338542011-08-29 11:01:31 -04004685 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04004686 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04004687 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04004688
Chris Masondbe674a2008-07-17 12:54:05 -04004689 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04004690
Josef Bacik4289a662011-08-05 13:22:24 -04004691 /*
Miao Xie8407aa42012-09-07 01:43:32 -06004692 * This is a bit simpler than btrfs_truncate since we've already
4693 * reserved our space for our orphan item in the unlink, so we just
4694 * need to reserve some slack space in case we add bytes and update
4695 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04004696 */
Yan, Zheng80825102009-11-12 09:35:36 +00004697 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00004698 ret = btrfs_block_rsv_refill(root, rsv, min_size,
4699 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00004700
Josef Bacik726c35f2011-09-26 15:46:06 -04004701 /*
4702 * Try and steal from the global reserve since we will
4703 * likely not use this space anyway, we want to try as
4704 * hard as possible to get this to work.
4705 */
4706 if (ret)
4707 ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
4708
Yan, Zhengd68fc572010-05-16 10:49:58 -04004709 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004710 btrfs_warn(root->fs_info,
4711 "Could not get space for a delete, will truncate on mount %d",
4712 ret);
Josef Bacik4289a662011-08-05 13:22:24 -04004713 btrfs_orphan_del(NULL, inode);
4714 btrfs_free_block_rsv(root, rsv);
4715 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04004716 }
4717
Miao Xie0e8c36a2012-12-19 06:59:51 +00004718 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04004719 if (IS_ERR(trans)) {
4720 btrfs_orphan_del(NULL, inode);
4721 btrfs_free_block_rsv(root, rsv);
4722 goto no_delete;
4723 }
4724
4725 trans->block_rsv = rsv;
4726
Yan, Zhengd68fc572010-05-16 10:49:58 -04004727 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04004728 if (ret != -ENOSPC)
Yan, Zheng80825102009-11-12 09:35:36 +00004729 break;
4730
Miao Xie8407aa42012-09-07 01:43:32 -06004731 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004732 btrfs_end_transaction(trans, root);
4733 trans = NULL;
Liu Bob53d3f52012-11-14 14:34:34 +00004734 btrfs_btree_balance_dirty(root);
Josef Bacik7b128762008-07-24 12:17:14 -04004735 }
4736
Josef Bacik4289a662011-08-05 13:22:24 -04004737 btrfs_free_block_rsv(root, rsv);
4738
Josef Bacik4ef31a42013-08-13 14:10:08 -04004739 /*
4740 * Errors here aren't a big deal, it just means we leave orphan items
4741 * in the tree. They will be cleaned up on the next mount.
4742 */
Yan, Zheng80825102009-11-12 09:35:36 +00004743 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04004744 trans->block_rsv = root->orphan_block_rsv;
Josef Bacik4ef31a42013-08-13 14:10:08 -04004745 btrfs_orphan_del(trans, inode);
4746 } else {
4747 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004748 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004749
Josef Bacik4289a662011-08-05 13:22:24 -04004750 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08004751 if (!(root == root->fs_info->tree_root ||
4752 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08004753 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08004754
Chris Mason54aa1f42007-06-22 14:16:25 -04004755 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004756 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04004757no_delete:
Miao Xie89042e52013-05-15 07:48:15 +00004758 btrfs_remove_delayed_node(inode);
Jan Karadbd57682012-05-03 14:48:02 +02004759 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004760 return;
Chris Mason39279cc2007-06-12 06:35:45 -04004761}
4762
4763/*
4764 * this returns the key found in the dir entry in the location pointer.
4765 * If no dir entries were found, location->objectid is 0.
4766 */
4767static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
4768 struct btrfs_key *location)
4769{
4770 const char *name = dentry->d_name.name;
4771 int namelen = dentry->d_name.len;
4772 struct btrfs_dir_item *di;
4773 struct btrfs_path *path;
4774 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04004775 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004776
4777 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07004778 if (!path)
4779 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05004780
Li Zefan33345d012011-04-20 10:31:50 +08004781 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04004782 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04004783 if (IS_ERR(di))
4784 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05004785
David Sterbac7040052011-04-19 18:00:01 +02004786 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05004787 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05004788
Chris Mason5f39d392007-10-15 16:14:19 -04004789 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04004790out:
Chris Mason39279cc2007-06-12 06:35:45 -04004791 btrfs_free_path(path);
4792 return ret;
Chris Mason39544012007-12-12 14:38:19 -05004793out_err:
4794 location->objectid = 0;
4795 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04004796}
4797
4798/*
4799 * when we hit a tree root in a directory, the btrfs part of the inode
4800 * needs to be changed to reflect the root directory of the tree root. This
4801 * is kind of like crossing a mount point.
4802 */
4803static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004804 struct inode *dir,
4805 struct dentry *dentry,
4806 struct btrfs_key *location,
4807 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04004808{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004809 struct btrfs_path *path;
4810 struct btrfs_root *new_root;
4811 struct btrfs_root_ref *ref;
4812 struct extent_buffer *leaf;
4813 int ret;
4814 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004815
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004816 path = btrfs_alloc_path();
4817 if (!path) {
4818 err = -ENOMEM;
4819 goto out;
4820 }
Chris Mason39279cc2007-06-12 06:35:45 -04004821
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004822 err = -ENOENT;
Kelley Nielsen75ac2dd2013-11-04 19:35:58 -08004823 ret = btrfs_find_item(root->fs_info->tree_root, path,
4824 BTRFS_I(dir)->root->root_key.objectid,
4825 location->objectid, BTRFS_ROOT_REF_KEY, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004826 if (ret) {
4827 if (ret < 0)
4828 err = ret;
4829 goto out;
4830 }
Chris Mason39279cc2007-06-12 06:35:45 -04004831
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004832 leaf = path->nodes[0];
4833 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08004834 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004835 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
4836 goto out;
4837
4838 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
4839 (unsigned long)(ref + 1),
4840 dentry->d_name.len);
4841 if (ret)
4842 goto out;
4843
David Sterbab3b4aa72011-04-21 01:20:15 +02004844 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004845
4846 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
4847 if (IS_ERR(new_root)) {
4848 err = PTR_ERR(new_root);
4849 goto out;
4850 }
4851
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004852 *sub_root = new_root;
4853 location->objectid = btrfs_root_dirid(&new_root->root_item);
4854 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04004855 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004856 err = 0;
4857out:
4858 btrfs_free_path(path);
4859 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004860}
4861
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004862static void inode_tree_add(struct inode *inode)
4863{
4864 struct btrfs_root *root = BTRFS_I(inode)->root;
4865 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004866 struct rb_node **p;
4867 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01004868 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Li Zefan33345d012011-04-20 10:31:50 +08004869 u64 ino = btrfs_ino(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004870
Al Viro1d3382cb2010-10-23 15:19:20 -04004871 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004872 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00004873 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004874 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00004875 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004876 while (*p) {
4877 parent = *p;
4878 entry = rb_entry(parent, struct btrfs_inode, rb_node);
4879
Li Zefan33345d012011-04-20 10:31:50 +08004880 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004881 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004882 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004883 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004884 else {
4885 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04004886 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01004887 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004888 RB_CLEAR_NODE(parent);
4889 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01004890 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004891 }
4892 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01004893 rb_link_node(new, parent, p);
4894 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004895 spin_unlock(&root->inode_lock);
4896}
4897
4898static void inode_tree_del(struct inode *inode)
4899{
4900 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04004901 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004902
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004903 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004904 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004905 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004906 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04004907 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004908 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004909 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04004910
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02004911 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004912 synchronize_srcu(&root->fs_info->subvol_srcu);
4913 spin_lock(&root->inode_lock);
4914 empty = RB_EMPTY_ROOT(&root->inode_tree);
4915 spin_unlock(&root->inode_lock);
4916 if (empty)
4917 btrfs_add_dead_root(root);
4918 }
4919}
4920
Jeff Mahoney143bede2012-03-01 14:56:26 +01004921void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04004922{
4923 struct rb_node *node;
4924 struct rb_node *prev;
4925 struct btrfs_inode *entry;
4926 struct inode *inode;
4927 u64 objectid = 0;
4928
Liu Bo7813b3d2014-02-10 17:37:25 +08004929 if (!test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
4930 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04004931
4932 spin_lock(&root->inode_lock);
4933again:
4934 node = root->inode_tree.rb_node;
4935 prev = NULL;
4936 while (node) {
4937 prev = node;
4938 entry = rb_entry(node, struct btrfs_inode, rb_node);
4939
Li Zefan33345d012011-04-20 10:31:50 +08004940 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004941 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004942 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004943 node = node->rb_right;
4944 else
4945 break;
4946 }
4947 if (!node) {
4948 while (prev) {
4949 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004950 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004951 node = prev;
4952 break;
4953 }
4954 prev = rb_next(prev);
4955 }
4956 }
4957 while (node) {
4958 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004959 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04004960 inode = igrab(&entry->vfs_inode);
4961 if (inode) {
4962 spin_unlock(&root->inode_lock);
4963 if (atomic_read(&inode->i_count) > 1)
4964 d_prune_aliases(inode);
4965 /*
Al Viro45321ac2010-06-07 13:43:19 -04004966 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04004967 * the inode cache when its usage count
4968 * hits zero.
4969 */
4970 iput(inode);
4971 cond_resched();
4972 spin_lock(&root->inode_lock);
4973 goto again;
4974 }
4975
4976 if (cond_resched_lock(&root->inode_lock))
4977 goto again;
4978
4979 node = rb_next(node);
4980 }
4981 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004982}
4983
Chris Masone02119d2008-09-05 16:13:11 -04004984static int btrfs_init_locked_inode(struct inode *inode, void *p)
4985{
4986 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08004987 inode->i_ino = args->location->objectid;
4988 memcpy(&BTRFS_I(inode)->location, args->location,
4989 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04004990 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004991 return 0;
4992}
4993
4994static int btrfs_find_actor(struct inode *inode, void *opaque)
4995{
4996 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08004997 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05004998 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004999}
5000
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005001static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005002 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005003 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005004{
5005 struct inode *inode;
5006 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005007 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005008
Chris Mason90d3e592014-01-09 17:28:00 -08005009 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005010 args.root = root;
5011
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005012 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005013 btrfs_init_locked_inode,
5014 (void *)&args);
5015 return inode;
5016}
5017
Balaji Rao1a54ef82008-07-21 02:01:04 +05305018/* Get an inode object given its location and corresponding root.
5019 * Returns in *is_new if the inode was read from disk
5020 */
5021struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005022 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305023{
5024 struct inode *inode;
5025
Chris Mason90d3e592014-01-09 17:28:00 -08005026 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305027 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005028 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305029
5030 if (inode->i_state & I_NEW) {
Balaji Rao1a54ef82008-07-21 02:01:04 +05305031 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005032 if (!is_bad_inode(inode)) {
5033 inode_tree_add(inode);
5034 unlock_new_inode(inode);
5035 if (new)
5036 *new = 1;
5037 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005038 unlock_new_inode(inode);
5039 iput(inode);
5040 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005041 }
5042 }
5043
Balaji Rao1a54ef82008-07-21 02:01:04 +05305044 return inode;
5045}
5046
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005047static struct inode *new_simple_dir(struct super_block *s,
5048 struct btrfs_key *key,
5049 struct btrfs_root *root)
5050{
5051 struct inode *inode = new_inode(s);
5052
5053 if (!inode)
5054 return ERR_PTR(-ENOMEM);
5055
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005056 BTRFS_I(inode)->root = root;
5057 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005058 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005059
5060 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005061 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005062 inode->i_fop = &simple_dir_operations;
5063 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
5064 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
5065
5066 return inode;
5067}
5068
Chris Mason3de45862008-11-17 21:02:50 -05005069struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005070{
Chris Masond3977122009-01-05 21:25:51 -05005071 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005072 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005073 struct btrfs_root *sub_root = root;
5074 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005075 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005076 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005077
5078 if (dentry->d_name.len > BTRFS_NAME_LEN)
5079 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005080
Jeff Layton39e3c952012-11-28 11:30:53 -05005081 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005082 if (ret < 0)
5083 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005084
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005085 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005086 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005087
5088 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005089 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005090 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005091 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005092
5093 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5094
Yan, Zheng76dda932009-09-21 16:00:26 -04005095 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005096 ret = fixup_tree_root_location(root, dir, dentry,
5097 &location, &sub_root);
5098 if (ret < 0) {
5099 if (ret != -ENOENT)
5100 inode = ERR_PTR(ret);
5101 else
5102 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5103 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005104 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005105 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005106 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
5107
Julia Lawall34d19ba2011-01-24 19:55:19 +00005108 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00005109 down_read(&root->fs_info->cleanup_work_sem);
5110 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005111 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005112 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005113 if (ret) {
5114 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005115 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005116 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005117 }
5118
Chris Mason3de45862008-11-17 21:02:50 -05005119 return inode;
5120}
5121
Nick Pigginfe15ce42011-01-07 17:49:23 +11005122static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005123{
5124 struct btrfs_root *root;
Li Zefan848cce02012-02-21 17:04:28 +08005125 struct inode *inode = dentry->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005126
Li Zefan848cce02012-02-21 17:04:28 +08005127 if (!inode && !IS_ROOT(dentry))
5128 inode = dentry->d_parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005129
Li Zefan848cce02012-02-21 17:04:28 +08005130 if (inode) {
5131 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005132 if (btrfs_root_refs(&root->root_item) == 0)
5133 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005134
5135 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5136 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005137 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005138 return 0;
5139}
5140
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005141static void btrfs_dentry_release(struct dentry *dentry)
5142{
5143 if (dentry->d_fsdata)
5144 kfree(dentry->d_fsdata);
5145}
5146
Chris Mason3de45862008-11-17 21:02:50 -05005147static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005148 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005149{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005150 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005151
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005152 inode = btrfs_lookup_dentry(dir, dentry);
5153 if (IS_ERR(inode)) {
5154 if (PTR_ERR(inode) == -ENOENT)
5155 inode = NULL;
5156 else
5157 return ERR_CAST(inode);
5158 }
5159
Josef Bacik3a0dfa62014-02-14 13:43:48 -05005160 return d_materialise_unique(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005161}
5162
Miao Xie16cdcec2011-04-22 18:12:22 +08005163unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005164 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5165};
5166
Al Viro9cdda8d2013-05-22 16:48:09 -04005167static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005168{
Al Viro9cdda8d2013-05-22 16:48:09 -04005169 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005170 struct btrfs_root *root = BTRFS_I(inode)->root;
5171 struct btrfs_item *item;
5172 struct btrfs_dir_item *di;
5173 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005174 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005175 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005176 struct list_head ins_list;
5177 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005178 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005179 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005180 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005181 unsigned char d_type;
5182 int over = 0;
5183 u32 di_cur;
5184 u32 di_total;
5185 u32 di_len;
5186 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005187 char tmp_name[32];
5188 char *name_ptr;
5189 int name_len;
Al Viro9cdda8d2013-05-22 16:48:09 -04005190 int is_curr = 0; /* ctx->pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04005191
5192 /* FIXME, use a real flag for deciding about the key type */
5193 if (root->fs_info->tree_root == root)
5194 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005195
Al Viro9cdda8d2013-05-22 16:48:09 -04005196 if (!dir_emit_dots(file, ctx))
5197 return 0;
5198
David Woodhouse49593bf2008-08-17 17:08:36 +01005199 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005200 if (!path)
5201 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005202
Josef Bacik026fd312011-05-13 10:32:11 -04005203 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005204
Miao Xie16cdcec2011-04-22 18:12:22 +08005205 if (key_type == BTRFS_DIR_INDEX_KEY) {
5206 INIT_LIST_HEAD(&ins_list);
5207 INIT_LIST_HEAD(&del_list);
5208 btrfs_get_delayed_items(inode, &ins_list, &del_list);
5209 }
5210
Chris Mason39279cc2007-06-12 06:35:45 -04005211 btrfs_set_key_type(&key, key_type);
Al Viro9cdda8d2013-05-22 16:48:09 -04005212 key.offset = ctx->pos;
Li Zefan33345d012011-04-20 10:31:50 +08005213 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005214
Chris Mason39279cc2007-06-12 06:35:45 -04005215 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5216 if (ret < 0)
5217 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005218
5219 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005220 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005221 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005222 if (slot >= btrfs_header_nritems(leaf)) {
5223 ret = btrfs_next_leaf(root, path);
5224 if (ret < 0)
5225 goto err;
5226 else if (ret > 0)
5227 break;
5228 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005229 }
Chris Mason3de45862008-11-17 21:02:50 -05005230
Ross Kirkdd3cc162013-09-16 15:58:09 +01005231 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04005232 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5233
5234 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005235 break;
Chris Mason5f39d392007-10-15 16:14:19 -04005236 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04005237 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005238 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005239 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08005240 if (key_type == BTRFS_DIR_INDEX_KEY &&
5241 btrfs_should_delete_dir_index(&del_list,
5242 found_key.offset))
5243 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005244
Al Viro9cdda8d2013-05-22 16:48:09 -04005245 ctx->pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08005246 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005247
Chris Mason39279cc2007-06-12 06:35:45 -04005248 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
5249 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005250 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01005251
5252 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04005253 struct btrfs_key location;
5254
Josef Bacik22a94d42011-03-16 16:47:17 -04005255 if (verify_dir_item(root, leaf, di))
5256 break;
5257
Chris Mason5f39d392007-10-15 16:14:19 -04005258 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01005259 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04005260 name_ptr = tmp_name;
5261 } else {
5262 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01005263 if (!name_ptr) {
5264 ret = -ENOMEM;
5265 goto err;
5266 }
Chris Mason5f39d392007-10-15 16:14:19 -04005267 }
5268 read_extent_buffer(leaf, name_ptr,
5269 (unsigned long)(di + 1), name_len);
5270
5271 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5272 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05005273
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005274
Chris Mason3de45862008-11-17 21:02:50 -05005275 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01005276 * skip it.
5277 *
5278 * In contrast to old kernels, we insert the snapshot's
5279 * dir item and dir index after it has been created, so
5280 * we won't find a reference to our own snapshot. We
5281 * still keep the following code for backward
5282 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05005283 */
5284 if (location.type == BTRFS_ROOT_ITEM_KEY &&
5285 location.objectid == root->root_key.objectid) {
5286 over = 0;
5287 goto skip;
5288 }
Al Viro9cdda8d2013-05-22 16:48:09 -04005289 over = !dir_emit(ctx, name_ptr, name_len,
5290 location.objectid, d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04005291
Chris Mason3de45862008-11-17 21:02:50 -05005292skip:
Chris Mason5f39d392007-10-15 16:14:19 -04005293 if (name_ptr != tmp_name)
5294 kfree(name_ptr);
5295
Chris Mason39279cc2007-06-12 06:35:45 -04005296 if (over)
5297 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05005298 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01005299 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04005300 di_cur += di_len;
5301 di = (struct btrfs_dir_item *)((char *)di + di_len);
5302 }
Li Zefanb9e03af2011-03-23 10:43:58 +08005303next:
5304 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005305 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005306
Miao Xie16cdcec2011-04-22 18:12:22 +08005307 if (key_type == BTRFS_DIR_INDEX_KEY) {
5308 if (is_curr)
Al Viro9cdda8d2013-05-22 16:48:09 -04005309 ctx->pos++;
5310 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005311 if (ret)
5312 goto nopos;
5313 }
5314
David Woodhouse49593bf2008-08-17 17:08:36 +01005315 /* Reached end of directory/root. Bump pos past the last item. */
Zach Browndb62efb2013-07-11 16:19:42 -07005316 ctx->pos++;
5317
5318 /*
5319 * Stop new entries from being returned after we return the last
5320 * entry.
5321 *
5322 * New directory entries are assigned a strictly increasing
5323 * offset. This means that new entries created during readdir
5324 * are *guaranteed* to be seen in the future by that readdir.
5325 * This has broken buggy programs which operate on names as
5326 * they're returned by readdir. Until we re-use freed offsets
5327 * we have this hack to stop new entries from being returned
5328 * under the assumption that they'll never reach this huge
5329 * offset.
5330 *
5331 * This is being careful not to overflow 32bit loff_t unless the
5332 * last entry requires it because doing so has broken 32bit apps
5333 * in the past.
5334 */
5335 if (key_type == BTRFS_DIR_INDEX_KEY) {
5336 if (ctx->pos >= INT_MAX)
5337 ctx->pos = LLONG_MAX;
5338 else
5339 ctx->pos = INT_MAX;
5340 }
Chris Mason39279cc2007-06-12 06:35:45 -04005341nopos:
5342 ret = 0;
5343err:
Miao Xie16cdcec2011-04-22 18:12:22 +08005344 if (key_type == BTRFS_DIR_INDEX_KEY)
5345 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005346 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005347 return ret;
5348}
5349
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005350int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005351{
5352 struct btrfs_root *root = BTRFS_I(inode)->root;
5353 struct btrfs_trans_handle *trans;
5354 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005355 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005356
Josef Bacik72ac3c02012-05-23 14:13:11 -04005357 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005358 return 0;
5359
Liu Bo83eea1f2012-07-10 05:28:39 -06005360 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08005361 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005362
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005363 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005364 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005365 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005366 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005367 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005368 if (IS_ERR(trans))
5369 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06005370 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005371 }
5372 return ret;
5373}
5374
5375/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005376 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005377 * inode changes. But, it is most likely to find the inode in cache.
5378 * FIXME, needs more benchmarking...there are no reasons other than performance
5379 * to keep or drop this code.
5380 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005381static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005382{
5383 struct btrfs_root *root = BTRFS_I(inode)->root;
5384 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005385 int ret;
5386
Josef Bacik72ac3c02012-05-23 14:13:11 -04005387 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005388 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005389
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005390 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005391 if (IS_ERR(trans))
5392 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005393
5394 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005395 if (ret && ret == -ENOSPC) {
5396 /* whoops, lets try again with the full transaction */
5397 btrfs_end_transaction(trans, root);
5398 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005399 if (IS_ERR(trans))
5400 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005401
Chris Mason94b60442010-05-26 11:02:00 -04005402 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005403 }
Chris Mason39279cc2007-06-12 06:35:45 -04005404 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08005405 if (BTRFS_I(inode)->delayed_node)
5406 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005407
5408 return ret;
5409}
5410
5411/*
5412 * This is a copy of file_update_time. We need this so we can return error on
5413 * ENOSPC for updating the inode in the case of file write and mmap writes.
5414 */
Josef Bacike41f9412012-03-26 09:46:47 -04005415static int btrfs_update_time(struct inode *inode, struct timespec *now,
5416 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005417{
Alexander Block2bc5565282012-06-15 09:49:33 +02005418 struct btrfs_root *root = BTRFS_I(inode)->root;
5419
5420 if (btrfs_root_readonly(root))
5421 return -EROFS;
5422
Josef Bacike41f9412012-03-26 09:46:47 -04005423 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005424 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04005425 if (flags & S_CTIME)
5426 inode->i_ctime = *now;
5427 if (flags & S_MTIME)
5428 inode->i_mtime = *now;
5429 if (flags & S_ATIME)
5430 inode->i_atime = *now;
5431 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005432}
5433
Chris Masond352ac62008-09-29 15:18:18 -04005434/*
5435 * find the highest existing sequence number in a directory
5436 * and then set the in-memory index_cnt variable to reflect
5437 * free sequence numbers
5438 */
Josef Bacikaec74772008-07-24 12:12:38 -04005439static int btrfs_set_inode_index_count(struct inode *inode)
5440{
5441 struct btrfs_root *root = BTRFS_I(inode)->root;
5442 struct btrfs_key key, found_key;
5443 struct btrfs_path *path;
5444 struct extent_buffer *leaf;
5445 int ret;
5446
Li Zefan33345d012011-04-20 10:31:50 +08005447 key.objectid = btrfs_ino(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005448 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
5449 key.offset = (u64)-1;
5450
5451 path = btrfs_alloc_path();
5452 if (!path)
5453 return -ENOMEM;
5454
5455 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5456 if (ret < 0)
5457 goto out;
5458 /* FIXME: we should be able to handle this */
5459 if (ret == 0)
5460 goto out;
5461 ret = 0;
5462
5463 /*
5464 * MAGIC NUMBER EXPLANATION:
5465 * since we search a directory based on f_pos we have to start at 2
5466 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5467 * else has to start at 2
5468 */
5469 if (path->slots[0] == 0) {
5470 BTRFS_I(inode)->index_cnt = 2;
5471 goto out;
5472 }
5473
5474 path->slots[0]--;
5475
5476 leaf = path->nodes[0];
5477 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5478
Li Zefan33345d012011-04-20 10:31:50 +08005479 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacikaec74772008-07-24 12:12:38 -04005480 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
5481 BTRFS_I(inode)->index_cnt = 2;
5482 goto out;
5483 }
5484
5485 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
5486out:
5487 btrfs_free_path(path);
5488 return ret;
5489}
5490
Chris Masond352ac62008-09-29 15:18:18 -04005491/*
5492 * helper to find a free sequence number in a given directory. This current
5493 * code is very simple, later versions will do smarter things in the btree
5494 */
Chris Mason3de45862008-11-17 21:02:50 -05005495int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04005496{
5497 int ret = 0;
5498
5499 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08005500 ret = btrfs_inode_delayed_dir_index_count(dir);
5501 if (ret) {
5502 ret = btrfs_set_inode_index_count(dir);
5503 if (ret)
5504 return ret;
5505 }
Josef Bacikaec74772008-07-24 12:12:38 -04005506 }
5507
Chris Mason00e4e6b2008-08-05 11:18:09 -04005508 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04005509 BTRFS_I(dir)->index_cnt++;
5510
5511 return ret;
5512}
5513
Chris Mason39279cc2007-06-12 06:35:45 -04005514static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5515 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04005516 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05005517 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04005518 u64 ref_objectid, u64 objectid,
5519 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04005520{
5521 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005522 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04005523 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04005524 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05005525 struct btrfs_inode_ref *ref;
5526 struct btrfs_key key[2];
5527 u32 sizes[2];
5528 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04005529 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005530
Chris Mason5f39d392007-10-15 16:14:19 -04005531 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005532 if (!path)
5533 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04005534
Chris Mason39279cc2007-06-12 06:35:45 -04005535 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005536 if (!inode) {
5537 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005538 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005539 }
Chris Mason39279cc2007-06-12 06:35:45 -04005540
Li Zefan581bb052011-04-20 10:06:11 +08005541 /*
5542 * we have to initialize this early, so we can reclaim the inode
5543 * number if we fail afterwards in this function.
5544 */
5545 inode->i_ino = objectid;
5546
Josef Bacikaec74772008-07-24 12:12:38 -04005547 if (dir) {
liubo1abe9b82011-03-24 11:18:59 +00005548 trace_btrfs_inode_request(dir);
5549
Chris Mason3de45862008-11-17 21:02:50 -05005550 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04005551 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005552 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005553 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005554 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04005555 }
Josef Bacikaec74772008-07-24 12:12:38 -04005556 }
5557 /*
5558 * index_cnt is ignored for everything but a dir,
5559 * btrfs_get_inode_index_count has an explanation for the magic
5560 * number
5561 */
5562 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08005563 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04005564 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04005565 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00005566 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04005567
Josef Bacik5dc562c2012-08-17 13:14:17 -04005568 /*
5569 * We could have gotten an inode number from somebody who was fsynced
5570 * and then removed in this same transaction, so let's just set full
5571 * sync since it will be a full sync anyway and this will blow away the
5572 * old info in the log.
5573 */
5574 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
5575
Chris Mason9c583092008-01-29 15:15:18 -05005576 key[0].objectid = objectid;
5577 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
5578 key[0].offset = 0;
5579
Mark Fashehf1863732012-08-08 11:32:27 -07005580 /*
5581 * Start new inodes with an inode_ref. This is slightly more
5582 * efficient for small numbers of hard links since they will
5583 * be packed into one item. Extended refs will kick in if we
5584 * add more hard links than can fit in the ref item.
5585 */
Chris Mason9c583092008-01-29 15:15:18 -05005586 key[1].objectid = objectid;
5587 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
5588 key[1].offset = ref_objectid;
5589
5590 sizes[0] = sizeof(struct btrfs_inode_item);
5591 sizes[1] = name_len + sizeof(*ref);
5592
Chris Masonb9473432009-03-13 11:00:37 -04005593 path->leave_spinning = 1;
Chris Mason9c583092008-01-29 15:15:18 -05005594 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
5595 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04005596 goto fail;
5597
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03005598 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04005599 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005600 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04005601 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
5602 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06005603 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
5604 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04005605 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05005606
5607 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
5608 struct btrfs_inode_ref);
5609 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04005610 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05005611 ptr = (unsigned long)(ref + 1);
5612 write_extent_buffer(path->nodes[0], name, ptr, name_len);
5613
Chris Mason5f39d392007-10-15 16:14:19 -04005614 btrfs_mark_buffer_dirty(path->nodes[0]);
5615 btrfs_free_path(path);
5616
Chris Mason39279cc2007-06-12 06:35:45 -04005617 location = &BTRFS_I(inode)->location;
5618 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04005619 location->offset = 0;
5620 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
5621
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005622 btrfs_inherit_iflags(inode, dir);
5623
Al Viro569254b2011-07-24 17:08:40 -04005624 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04005625 if (btrfs_test_opt(root, NODATASUM))
5626 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo213490b2012-09-11 08:33:50 -06005627 if (btrfs_test_opt(root, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05005628 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
5629 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04005630 }
5631
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005632 btrfs_insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005633 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00005634
5635 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04005636 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00005637
Alexander Block8ea05e32012-07-25 17:35:53 +02005638 btrfs_update_root_times(trans, root);
5639
Filipe David Borba Manana63541922014-01-07 11:47:46 +00005640 ret = btrfs_inode_inherit_props(trans, inode, dir);
5641 if (ret)
5642 btrfs_err(root->fs_info,
5643 "error inheriting props for ino %llu (root %llu): %d",
5644 btrfs_ino(inode), root->root_key.objectid, ret);
5645
Chris Mason39279cc2007-06-12 06:35:45 -04005646 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005647fail:
Josef Bacikaec74772008-07-24 12:12:38 -04005648 if (dir)
5649 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04005650 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005651 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005652 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005653}
5654
5655static inline u8 btrfs_inode_type(struct inode *inode)
5656{
5657 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
5658}
5659
Chris Masond352ac62008-09-29 15:18:18 -04005660/*
5661 * utility function to add 'inode' into 'parent_inode' with
5662 * a give name and a given sequence number.
5663 * if 'add_backref' is true, also insert a backref from the
5664 * inode to the parent directory.
5665 */
Chris Masone02119d2008-09-05 16:13:11 -04005666int btrfs_add_link(struct btrfs_trans_handle *trans,
5667 struct inode *parent_inode, struct inode *inode,
5668 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005669{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005670 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005671 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04005672 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08005673 u64 ino = btrfs_ino(inode);
5674 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005675
Li Zefan33345d012011-04-20 10:31:50 +08005676 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005677 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
5678 } else {
Li Zefan33345d012011-04-20 10:31:50 +08005679 key.objectid = ino;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005680 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
5681 key.offset = 0;
5682 }
Chris Mason39279cc2007-06-12 06:35:45 -04005683
Li Zefan33345d012011-04-20 10:31:50 +08005684 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005685 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
5686 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08005687 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005688 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08005689 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
5690 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005691 }
5692
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005693 /* Nothing to clean up yet */
5694 if (ret)
5695 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005696
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005697 ret = btrfs_insert_dir_item(trans, root, name, name_len,
5698 parent_inode, &key,
5699 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05005700 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005701 goto fail_dir_item;
5702 else if (ret) {
5703 btrfs_abort_transaction(trans, root, ret);
5704 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005705 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005706
5707 btrfs_i_size_write(parent_inode, parent_inode->i_size +
5708 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005709 inode_inc_iversion(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005710 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
5711 ret = btrfs_update_inode(trans, root, parent_inode);
5712 if (ret)
5713 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005714 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05005715
5716fail_dir_item:
5717 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
5718 u64 local_index;
5719 int err;
5720 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
5721 key.objectid, root->root_key.objectid,
5722 parent_ino, &local_index, name, name_len);
5723
5724 } else if (add_backref) {
5725 u64 local_index;
5726 int err;
5727
5728 err = btrfs_del_inode_ref(trans, root, name, name_len,
5729 ino, parent_ino, &local_index);
5730 }
5731 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005732}
5733
5734static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00005735 struct inode *dir, struct dentry *dentry,
5736 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005737{
Josef Bacika1b075d2010-11-19 20:36:11 +00005738 int err = btrfs_add_link(trans, dir, inode,
5739 dentry->d_name.name, dentry->d_name.len,
5740 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005741 if (err > 0)
5742 err = -EEXIST;
5743 return err;
5744}
5745
Josef Bacik618e21d2007-07-11 10:18:17 -04005746static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04005747 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04005748{
5749 struct btrfs_trans_handle *trans;
5750 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005751 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04005752 int err;
5753 int drop_inode = 0;
5754 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005755 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04005756
5757 if (!new_valid_dev(rdev))
5758 return -EINVAL;
5759
Josef Bacik9ed74f22009-09-11 16:12:44 -04005760 /*
5761 * 2 for inode item and ref
5762 * 2 for dir items
5763 * 1 for xattr if selinux is on
5764 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005765 trans = btrfs_start_transaction(root, 5);
5766 if (IS_ERR(trans))
5767 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05005768
Li Zefan581bb052011-04-20 10:06:11 +08005769 err = btrfs_find_free_ino(root, &objectid);
5770 if (err)
5771 goto out_unlock;
5772
Josef Bacikaec74772008-07-24 12:12:38 -04005773 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005774 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005775 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005776 if (IS_ERR(inode)) {
5777 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005778 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005779 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005780
Eric Paris2a7dba32011-02-01 11:05:39 -05005781 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005782 if (err) {
5783 drop_inode = 1;
5784 goto out_unlock;
5785 }
5786
Casey Schauflerad19db72011-12-15 10:09:07 -05005787 /*
5788 * If the active LSM wants to access the inode during
5789 * d_instantiate it needs these. Smack checks to see
5790 * if the filesystem supports xattrs by looking at the
5791 * ops vector.
5792 */
5793
5794 inode->i_op = &btrfs_special_inode_operations;
Josef Bacika1b075d2010-11-19 20:36:11 +00005795 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04005796 if (err)
5797 drop_inode = 1;
5798 else {
Josef Bacik618e21d2007-07-11 10:18:17 -04005799 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04005800 btrfs_update_inode(trans, root, inode);
Al Viro08c422c2011-12-23 07:58:13 -05005801 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005802 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005803out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005804 btrfs_end_transaction(trans, root);
Josef Bacikc581afc2014-02-06 16:06:06 -05005805 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00005806 btrfs_btree_balance_dirty(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04005807 if (drop_inode) {
5808 inode_dec_link_count(inode);
5809 iput(inode);
5810 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005811 return err;
5812}
5813
Chris Mason39279cc2007-06-12 06:35:45 -04005814static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04005815 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04005816{
5817 struct btrfs_trans_handle *trans;
5818 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005819 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005820 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04005821 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04005822 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005823 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005824
Josef Bacik9ed74f22009-09-11 16:12:44 -04005825 /*
5826 * 2 for inode item and ref
5827 * 2 for dir items
5828 * 1 for xattr if selinux is on
5829 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005830 trans = btrfs_start_transaction(root, 5);
5831 if (IS_ERR(trans))
5832 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005833
Li Zefan581bb052011-04-20 10:06:11 +08005834 err = btrfs_find_free_ino(root, &objectid);
5835 if (err)
5836 goto out_unlock;
5837
Josef Bacikaec74772008-07-24 12:12:38 -04005838 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005839 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005840 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005841 if (IS_ERR(inode)) {
5842 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005843 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005844 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005845 drop_inode_on_err = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04005846
Eric Paris2a7dba32011-02-01 11:05:39 -05005847 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005848 if (err)
Josef Bacik33268ea2008-07-24 12:16:36 -04005849 goto out_unlock;
Josef Bacik33268ea2008-07-24 12:16:36 -04005850
Filipe Brandenburger9185aa52012-11-30 03:40:08 +00005851 err = btrfs_update_inode(trans, root, inode);
5852 if (err)
5853 goto out_unlock;
5854
Casey Schauflerad19db72011-12-15 10:09:07 -05005855 /*
5856 * If the active LSM wants to access the inode during
5857 * d_instantiate it needs these. Smack checks to see
5858 * if the filesystem supports xattrs by looking at the
5859 * ops vector.
5860 */
5861 inode->i_fop = &btrfs_file_operations;
5862 inode->i_op = &btrfs_file_inode_operations;
5863
Josef Bacika1b075d2010-11-19 20:36:11 +00005864 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005865 if (err)
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005866 goto out_unlock;
5867
5868 inode->i_mapping->a_ops = &btrfs_aops;
5869 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
5870 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
5871 d_instantiate(dentry, inode);
5872
Chris Mason39279cc2007-06-12 06:35:45 -04005873out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005874 btrfs_end_transaction(trans, root);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005875 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005876 inode_dec_link_count(inode);
5877 iput(inode);
5878 }
Josef Bacikc581afc2014-02-06 16:06:06 -05005879 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00005880 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005881 return err;
5882}
5883
5884static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
5885 struct dentry *dentry)
5886{
5887 struct btrfs_trans_handle *trans;
5888 struct btrfs_root *root = BTRFS_I(dir)->root;
5889 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005890 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04005891 int err;
5892 int drop_inode = 0;
5893
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005894 /* do not allow sys_link's with other subvols of the same device */
5895 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00005896 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005897
Mark Fashehf1863732012-08-08 11:32:27 -07005898 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00005899 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04005900
Chris Mason3de45862008-11-17 21:02:50 -05005901 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04005902 if (err)
5903 goto fail;
5904
Yan, Zhenga22285a2010-05-16 10:48:46 -04005905 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00005906 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04005907 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00005908 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04005909 */
Miao Xie7e6b6462011-02-18 09:21:17 +00005910 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005911 if (IS_ERR(trans)) {
5912 err = PTR_ERR(trans);
5913 goto fail;
5914 }
Chris Mason5f39d392007-10-15 16:14:19 -04005915
Miao Xie67de1172013-12-26 13:07:06 +08005916 /* There are several dir indexes for this inode, clear the cache. */
5917 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07005918 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005919 inode_inc_iversion(inode);
Miao Xie31534952011-04-13 13:19:21 +08005920 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6ee2010-10-23 11:11:40 -04005921 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04005922 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04005923
Josef Bacika1b075d2010-11-19 20:36:11 +00005924 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04005925
Yan, Zhenga5719522009-09-24 09:17:31 -04005926 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005927 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04005928 } else {
Al Viro10d9f302011-07-16 23:09:10 -04005929 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04005930 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005931 if (err)
5932 goto fail;
Al Viro08c422c2011-12-23 07:58:13 -05005933 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00005934 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04005935 }
Chris Mason39279cc2007-06-12 06:35:45 -04005936
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005937 btrfs_end_transaction(trans, root);
Josef Bacikc581afc2014-02-06 16:06:06 -05005938 btrfs_balance_delayed_items(root);
Chris Mason1832a6d2007-12-21 16:27:21 -05005939fail:
Chris Mason39279cc2007-06-12 06:35:45 -04005940 if (drop_inode) {
5941 inode_dec_link_count(inode);
5942 iput(inode);
5943 }
Liu Bob53d3f52012-11-14 14:34:34 +00005944 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005945 return err;
5946}
5947
Al Viro18bb1db2011-07-26 01:41:39 -04005948static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04005949{
Chris Masonb9d86662008-05-02 16:13:49 -04005950 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04005951 struct btrfs_trans_handle *trans;
5952 struct btrfs_root *root = BTRFS_I(dir)->root;
5953 int err = 0;
5954 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04005955 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005956 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005957
Josef Bacik9ed74f22009-09-11 16:12:44 -04005958 /*
5959 * 2 items for inode and ref
5960 * 2 items for dir items
5961 * 1 for xattr if selinux is on
5962 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005963 trans = btrfs_start_transaction(root, 5);
5964 if (IS_ERR(trans))
5965 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04005966
Li Zefan581bb052011-04-20 10:06:11 +08005967 err = btrfs_find_free_ino(root, &objectid);
5968 if (err)
5969 goto out_fail;
5970
Josef Bacikaec74772008-07-24 12:12:38 -04005971 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005972 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005973 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04005974 if (IS_ERR(inode)) {
5975 err = PTR_ERR(inode);
5976 goto out_fail;
5977 }
Chris Mason5f39d392007-10-15 16:14:19 -04005978
Chris Mason39279cc2007-06-12 06:35:45 -04005979 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04005980
Eric Paris2a7dba32011-02-01 11:05:39 -05005981 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005982 if (err)
5983 goto out_fail;
5984
Chris Mason39279cc2007-06-12 06:35:45 -04005985 inode->i_op = &btrfs_dir_inode_operations;
5986 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04005987
Chris Masondbe674a2008-07-17 12:54:05 -04005988 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005989 err = btrfs_update_inode(trans, root, inode);
5990 if (err)
5991 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005992
Josef Bacika1b075d2010-11-19 20:36:11 +00005993 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
5994 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005995 if (err)
5996 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005997
Chris Mason39279cc2007-06-12 06:35:45 -04005998 d_instantiate(dentry, inode);
5999 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006000
6001out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006002 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04006003 if (drop_on_err)
6004 iput(inode);
Josef Bacikc581afc2014-02-06 16:06:06 -05006005 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006006 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006007 return err;
6008}
6009
Chris Masond352ac62008-09-29 15:18:18 -04006010/* helper for btfs_get_extent. Given an existing extent in the tree,
6011 * and an extent that you want to insert, deal with overlap and insert
6012 * the new extent into the tree.
6013 */
Chris Mason3b951512008-04-17 11:29:12 -04006014static int merge_extent_mapping(struct extent_map_tree *em_tree,
6015 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006016 struct extent_map *em,
6017 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04006018{
6019 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04006020
Chris Masone6dcd2d2008-07-17 12:53:50 -04006021 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
6022 start_diff = map_start - em->start;
6023 em->start = map_start;
6024 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04006025 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
6026 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006027 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04006028 em->block_len -= start_diff;
6029 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006030 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006031}
6032
Chris Masonc8b97812008-10-29 14:49:59 -04006033static noinline int uncompress_inline(struct btrfs_path *path,
6034 struct inode *inode, struct page *page,
6035 size_t pg_offset, u64 extent_offset,
6036 struct btrfs_file_extent_item *item)
6037{
6038 int ret;
6039 struct extent_buffer *leaf = path->nodes[0];
6040 char *tmp;
6041 size_t max_size;
6042 unsigned long inline_size;
6043 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006044 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006045
6046 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006047 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006048 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6049 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006050 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006051 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006052 if (!tmp)
6053 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006054 ptr = btrfs_file_extent_inline_start(item);
6055
6056 read_extent_buffer(leaf, tmp, ptr, inline_size);
6057
Chris Mason5b050f02008-11-11 09:34:41 -05006058 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006059 ret = btrfs_decompress(compress_type, tmp, page,
6060 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006061 if (ret) {
Cong Wang7ac687d2011-11-25 23:14:28 +08006062 char *kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04006063 unsigned long copy_size = min_t(u64,
6064 PAGE_CACHE_SIZE - pg_offset,
6065 max_size - extent_offset);
6066 memset(kaddr + pg_offset, 0, copy_size);
Cong Wang7ac687d2011-11-25 23:14:28 +08006067 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -04006068 }
6069 kfree(tmp);
6070 return 0;
6071}
6072
Chris Masond352ac62008-09-29 15:18:18 -04006073/*
6074 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006075 * the ugly parts come from merging extents from the disk with the in-ram
6076 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006077 * where the in-ram extents might be locked pending data=ordered completion.
6078 *
6079 * This also copies inline extents directly into the page.
6080 */
Chris Masond3977122009-01-05 21:25:51 -05006081
Chris Masona52d9a82007-08-27 16:49:44 -04006082struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05006083 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04006084 int create)
6085{
6086 int ret;
6087 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04006088 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04006089 u64 extent_start = 0;
6090 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08006091 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006092 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006093 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04006094 struct btrfs_root *root = BTRFS_I(inode)->root;
6095 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006096 struct extent_buffer *leaf;
6097 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006098 struct extent_map *em = NULL;
6099 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05006100 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006101 struct btrfs_trans_handle *trans = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08006102 int compress_type;
Chris Masona52d9a82007-08-27 16:49:44 -04006103
Chris Masona52d9a82007-08-27 16:49:44 -04006104again:
Chris Mason890871b2009-09-02 16:24:52 -04006105 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006106 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006107 if (em)
6108 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006109 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006110
Chris Masona52d9a82007-08-27 16:49:44 -04006111 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006112 if (em->start > start || em->start + em->len <= start)
6113 free_extent_map(em);
6114 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006115 free_extent_map(em);
6116 else
6117 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006118 }
David Sterba172ddd62011-04-21 00:48:27 +02006119 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006120 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006121 err = -ENOMEM;
6122 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006123 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006124 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006125 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006126 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006127 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006128 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006129
6130 if (!path) {
6131 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006132 if (!path) {
6133 err = -ENOMEM;
6134 goto out;
6135 }
6136 /*
6137 * Chances are we'll be called again, so go ahead and do
6138 * readahead
6139 */
6140 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04006141 }
6142
Chris Mason179e29e2007-11-01 11:28:41 -04006143 ret = btrfs_lookup_file_extent(trans, root, path,
6144 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006145 if (ret < 0) {
6146 err = ret;
6147 goto out;
6148 }
6149
6150 if (ret != 0) {
6151 if (path->slots[0] == 0)
6152 goto not_found;
6153 path->slots[0]--;
6154 }
6155
Chris Mason5f39d392007-10-15 16:14:19 -04006156 leaf = path->nodes[0];
6157 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006158 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006159 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006160 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6161 found_type = btrfs_key_type(&found_key);
6162 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006163 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006164 /*
6165 * If we backup past the first extent we want to move forward
6166 * and see if there is an extent in front of us, otherwise we'll
6167 * say there is a hole for our whole search range which can
6168 * cause problems.
6169 */
6170 extent_end = start;
6171 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006172 }
6173
Chris Mason5f39d392007-10-15 16:14:19 -04006174 found_type = btrfs_file_extent_type(leaf, item);
6175 extent_start = found_key.offset;
Li Zefan261507a02010-12-17 14:21:50 +08006176 compress_type = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04006177 if (found_type == BTRFS_FILE_EXTENT_REG ||
6178 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006179 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006180 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006181 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6182 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08006183 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Qu Wenruofda28322013-02-26 08:10:22 +00006184 extent_end = ALIGN(extent_start + size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04006185 }
Josef Bacik25a50342013-10-14 12:08:38 -04006186next:
Yan Zheng9036c102008-10-30 14:19:41 -04006187 if (start >= extent_end) {
6188 path->slots[0]++;
6189 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6190 ret = btrfs_next_leaf(root, path);
6191 if (ret < 0) {
6192 err = ret;
6193 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006194 }
Yan Zheng9036c102008-10-30 14:19:41 -04006195 if (ret > 0)
6196 goto not_found;
6197 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006198 }
Yan Zheng9036c102008-10-30 14:19:41 -04006199 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6200 if (found_key.objectid != objectid ||
6201 found_key.type != BTRFS_EXTENT_DATA_KEY)
6202 goto not_found;
6203 if (start + len <= found_key.offset)
6204 goto not_found;
6205 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006206 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006207 em->len = found_key.offset - start;
6208 goto not_found_em;
6209 }
6210
Josef Bacikcc95bef2013-04-04 14:31:27 -04006211 em->ram_bytes = btrfs_file_extent_ram_bytes(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04006212 if (found_type == BTRFS_FILE_EXTENT_REG ||
6213 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04006214 em->start = extent_start;
6215 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05006216 em->orig_start = extent_start -
6217 btrfs_file_extent_offset(leaf, item);
Josef Bacikb4939682012-12-03 10:31:19 -05006218 em->orig_block_len = btrfs_file_extent_disk_num_bytes(leaf,
6219 item);
Chris Masondb945352007-10-15 16:15:53 -04006220 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
6221 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04006222 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04006223 goto insert;
6224 }
Li Zefan261507a02010-12-17 14:21:50 +08006225 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006226 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08006227 em->compress_type = compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006228 em->block_start = bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05006229 em->block_len = em->orig_block_len;
Chris Masonc8b97812008-10-29 14:49:59 -04006230 } else {
6231 bytenr += btrfs_file_extent_offset(leaf, item);
6232 em->block_start = bytenr;
6233 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04006234 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
6235 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04006236 }
Chris Masona52d9a82007-08-27 16:49:44 -04006237 goto insert;
6238 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006239 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006240 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006241 size_t size;
6242 size_t extent_offset;
6243 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006244
Chris Masona52d9a82007-08-27 16:49:44 -04006245 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04006246 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04006247 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04006248 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04006249 goto out;
6250 }
6251
Chris Mason514ac8a2014-01-03 21:07:00 -08006252 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04006253 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05006254 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04006255 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006256 em->start = extent_start + extent_offset;
Qu Wenruofda28322013-02-26 08:10:22 +00006257 em->len = ALIGN(copy_size, root->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006258 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006259 em->orig_start = em->start;
Li Zefan261507a02010-12-17 14:21:50 +08006260 if (compress_type) {
Chris Masonc8b97812008-10-29 14:49:59 -04006261 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08006262 em->compress_type = compress_type;
6263 }
Yan689f9342007-10-29 11:41:07 -04006264 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006265 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006266 if (btrfs_file_extent_compression(leaf, item) !=
6267 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006268 ret = uncompress_inline(path, inode, page,
6269 pg_offset,
6270 extent_offset, item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006271 BUG_ON(ret); /* -ENOMEM */
Chris Masonc8b97812008-10-29 14:49:59 -04006272 } else {
6273 map = kmap(page);
6274 read_extent_buffer(leaf, map + pg_offset, ptr,
6275 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04006276 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
6277 memset(map + pg_offset + copy_size, 0,
6278 PAGE_CACHE_SIZE - pg_offset -
6279 copy_size);
6280 }
Chris Masonc8b97812008-10-29 14:49:59 -04006281 kunmap(page);
6282 }
Chris Mason179e29e2007-11-01 11:28:41 -04006283 flush_dcache_page(page);
6284 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006285 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006286 if (!trans) {
6287 kunmap(page);
6288 free_extent_map(em);
6289 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006290
David Sterbab3b4aa72011-04-21 01:20:15 +02006291 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006292 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006293
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006294 if (IS_ERR(trans))
6295 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006296 goto again;
6297 }
Chris Masonc8b97812008-10-29 14:49:59 -04006298 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006299 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006300 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006301 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006302 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006303 }
Chris Masond1310b22008-01-24 16:13:08 -05006304 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006305 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006306 goto insert;
6307 } else {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00006308 WARN(1, KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04006309 }
6310not_found:
6311 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006312 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006313 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006314not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04006315 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04006316 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006317insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006318 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006319 if (em->start > start || extent_map_end(em) <= start) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006320 btrfs_err(root->fs_info, "bad extent! em: [%llu %llu] passed [%llu %llu]",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006321 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04006322 err = -EIO;
6323 goto out;
6324 }
Chris Masond1310b22008-01-24 16:13:08 -05006325
6326 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006327 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006328 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006329 /* it is possible that someone inserted the extent into the tree
6330 * while we had the lock dropped. It is also possible that
6331 * an overlapping map exists in the tree
6332 */
Chris Masona52d9a82007-08-27 16:49:44 -04006333 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04006334 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006335
6336 ret = 0;
6337
Chris Mason3b951512008-04-17 11:29:12 -04006338 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04006339 if (existing && (existing->start > start ||
6340 existing->start + existing->len <= start)) {
6341 free_extent_map(existing);
6342 existing = NULL;
6343 }
Chris Mason3b951512008-04-17 11:29:12 -04006344 if (!existing) {
6345 existing = lookup_extent_mapping(em_tree, em->start,
6346 em->len);
6347 if (existing) {
6348 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006349 em, start,
6350 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04006351 free_extent_map(existing);
6352 if (err) {
6353 free_extent_map(em);
6354 em = NULL;
6355 }
6356 } else {
6357 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04006358 free_extent_map(em);
6359 em = NULL;
6360 }
6361 } else {
6362 free_extent_map(em);
6363 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006364 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006365 }
Chris Masona52d9a82007-08-27 16:49:44 -04006366 }
Chris Mason890871b2009-09-02 16:24:52 -04006367 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006368out:
liubo1abe9b82011-03-24 11:18:59 +00006369
Steven Rostedt4cd8587c2013-11-14 22:57:29 -05006370 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00006371
Chris Masonf4219502008-07-22 11:18:09 -04006372 if (path)
6373 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04006374 if (trans) {
6375 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05006376 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04006377 err = ret;
6378 }
Chris Masona52d9a82007-08-27 16:49:44 -04006379 if (err) {
6380 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04006381 return ERR_PTR(err);
6382 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006383 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04006384 return em;
6385}
6386
Chris Masonec29ed52011-02-23 16:23:20 -05006387struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
6388 size_t pg_offset, u64 start, u64 len,
6389 int create)
6390{
6391 struct extent_map *em;
6392 struct extent_map *hole_em = NULL;
6393 u64 range_start = start;
6394 u64 end;
6395 u64 found;
6396 u64 found_end;
6397 int err = 0;
6398
6399 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
6400 if (IS_ERR(em))
6401 return em;
6402 if (em) {
6403 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00006404 * if our em maps to
6405 * - a hole or
6406 * - a pre-alloc extent,
6407 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05006408 */
Liu Bof9e4fb52013-01-07 10:10:12 +00006409 if (em->block_start != EXTENT_MAP_HOLE &&
6410 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05006411 return em;
6412 else
6413 hole_em = em;
6414 }
6415
6416 /* check to see if we've wrapped (len == -1 or similar) */
6417 end = start + len;
6418 if (end < start)
6419 end = (u64)-1;
6420 else
6421 end -= 1;
6422
6423 em = NULL;
6424
6425 /* ok, we didn't find anything, lets look for delalloc */
6426 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
6427 end, len, EXTENT_DELALLOC, 1);
6428 found_end = range_start + found;
6429 if (found_end < range_start)
6430 found_end = (u64)-1;
6431
6432 /*
6433 * we didn't find anything useful, return
6434 * the original results from get_extent()
6435 */
6436 if (range_start > end || found_end <= start) {
6437 em = hole_em;
6438 hole_em = NULL;
6439 goto out;
6440 }
6441
6442 /* adjust the range_start to make sure it doesn't
6443 * go backwards from the start they passed in
6444 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05306445 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05006446 found = found_end - range_start;
6447
6448 if (found > 0) {
6449 u64 hole_start = start;
6450 u64 hole_len = len;
6451
David Sterba172ddd62011-04-21 00:48:27 +02006452 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05006453 if (!em) {
6454 err = -ENOMEM;
6455 goto out;
6456 }
6457 /*
6458 * when btrfs_get_extent can't find anything it
6459 * returns one huge hole
6460 *
6461 * make sure what it found really fits our range, and
6462 * adjust to make sure it is based on the start from
6463 * the caller
6464 */
6465 if (hole_em) {
6466 u64 calc_end = extent_map_end(hole_em);
6467
6468 if (calc_end <= start || (hole_em->start > end)) {
6469 free_extent_map(hole_em);
6470 hole_em = NULL;
6471 } else {
6472 hole_start = max(hole_em->start, start);
6473 hole_len = calc_end - hole_start;
6474 }
6475 }
6476 em->bdev = NULL;
6477 if (hole_em && range_start > hole_start) {
6478 /* our hole starts before our delalloc, so we
6479 * have to return just the parts of the hole
6480 * that go until the delalloc starts
6481 */
6482 em->len = min(hole_len,
6483 range_start - hole_start);
6484 em->start = hole_start;
6485 em->orig_start = hole_start;
6486 /*
6487 * don't adjust block start at all,
6488 * it is fixed at EXTENT_MAP_HOLE
6489 */
6490 em->block_start = hole_em->block_start;
6491 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00006492 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
6493 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05006494 } else {
6495 em->start = range_start;
6496 em->len = found;
6497 em->orig_start = range_start;
6498 em->block_start = EXTENT_MAP_DELALLOC;
6499 em->block_len = found;
6500 }
6501 } else if (hole_em) {
6502 return hole_em;
6503 }
6504out:
6505
6506 free_extent_map(hole_em);
6507 if (err) {
6508 free_extent_map(em);
6509 return ERR_PTR(err);
6510 }
6511 return em;
6512}
6513
Josef Bacik4b46fce2010-05-23 11:00:55 -04006514static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
6515 u64 start, u64 len)
6516{
6517 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04006518 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006519 struct btrfs_key ins;
6520 u64 alloc_hint;
6521 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006522
Josef Bacik4b46fce2010-05-23 11:00:55 -04006523 alloc_hint = get_extent_allocation_hint(inode, start, len);
Josef Bacik00361582013-08-14 14:02:47 -04006524 ret = btrfs_reserve_extent(root, len, root->sectorsize, 0,
Josef Bacik81c9ad22012-01-18 10:56:06 -05006525 alloc_hint, &ins, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006526 if (ret)
6527 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006528
Josef Bacik70c8a912012-10-11 16:54:30 -04006529 em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006530 ins.offset, ins.offset, ins.offset, 0);
Josef Bacik00361582013-08-14 14:02:47 -04006531 if (IS_ERR(em)) {
6532 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
6533 return em;
6534 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006535
6536 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
6537 ins.offset, ins.offset, 0);
6538 if (ret) {
6539 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
Josef Bacik00361582013-08-14 14:02:47 -04006540 free_extent_map(em);
6541 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006542 }
Josef Bacik00361582013-08-14 14:02:47 -04006543
Josef Bacik4b46fce2010-05-23 11:00:55 -04006544 return em;
6545}
6546
Chris Mason46bfbb52010-05-26 11:04:10 -04006547/*
6548 * returns 1 when the nocow is safe, < 1 on error, 0 if the
6549 * block must be cow'd
6550 */
Josef Bacik00361582013-08-14 14:02:47 -04006551noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04006552 u64 *orig_start, u64 *orig_block_len,
6553 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04006554{
Josef Bacik00361582013-08-14 14:02:47 -04006555 struct btrfs_trans_handle *trans;
Chris Mason46bfbb52010-05-26 11:04:10 -04006556 struct btrfs_path *path;
6557 int ret;
6558 struct extent_buffer *leaf;
6559 struct btrfs_root *root = BTRFS_I(inode)->root;
6560 struct btrfs_file_extent_item *fi;
6561 struct btrfs_key key;
6562 u64 disk_bytenr;
6563 u64 backref_offset;
6564 u64 extent_end;
6565 u64 num_bytes;
6566 int slot;
6567 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04006568 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08006569
Chris Mason46bfbb52010-05-26 11:04:10 -04006570 path = btrfs_alloc_path();
6571 if (!path)
6572 return -ENOMEM;
6573
Josef Bacik00361582013-08-14 14:02:47 -04006574 ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04006575 offset, 0);
6576 if (ret < 0)
6577 goto out;
6578
6579 slot = path->slots[0];
6580 if (ret == 1) {
6581 if (slot == 0) {
6582 /* can't find the item, must cow */
6583 ret = 0;
6584 goto out;
6585 }
6586 slot--;
6587 }
6588 ret = 0;
6589 leaf = path->nodes[0];
6590 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08006591 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04006592 key.type != BTRFS_EXTENT_DATA_KEY) {
6593 /* not our file or wrong item type, must cow */
6594 goto out;
6595 }
6596
6597 if (key.offset > offset) {
6598 /* Wrong offset, must cow */
6599 goto out;
6600 }
6601
6602 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
6603 found_type = btrfs_file_extent_type(leaf, fi);
6604 if (found_type != BTRFS_FILE_EXTENT_REG &&
6605 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
6606 /* not a regular extent, must cow */
6607 goto out;
6608 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04006609
6610 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
6611 goto out;
6612
Miao Xiee77751a2013-12-27 21:11:50 +08006613 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
6614 if (extent_end <= offset)
6615 goto out;
6616
Chris Mason46bfbb52010-05-26 11:04:10 -04006617 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04006618 if (disk_bytenr == 0)
6619 goto out;
6620
6621 if (btrfs_file_extent_compression(leaf, fi) ||
6622 btrfs_file_extent_encryption(leaf, fi) ||
6623 btrfs_file_extent_other_encoding(leaf, fi))
6624 goto out;
6625
Chris Mason46bfbb52010-05-26 11:04:10 -04006626 backref_offset = btrfs_file_extent_offset(leaf, fi);
6627
Josef Bacik7ee9e442013-06-21 16:37:03 -04006628 if (orig_start) {
6629 *orig_start = key.offset - backref_offset;
6630 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
6631 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
6632 }
Josef Bacikeb384b52013-04-24 16:32:55 -04006633
Chris Mason46bfbb52010-05-26 11:04:10 -04006634 if (btrfs_extent_readonly(root, disk_bytenr))
6635 goto out;
Josef Bacik1bda19e2013-10-18 12:10:36 -04006636 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04006637
6638 /*
6639 * look for other files referencing this extent, if we
6640 * find any we must cow
6641 */
Josef Bacik00361582013-08-14 14:02:47 -04006642 trans = btrfs_join_transaction(root);
6643 if (IS_ERR(trans)) {
6644 ret = 0;
Chris Mason46bfbb52010-05-26 11:04:10 -04006645 goto out;
Josef Bacik00361582013-08-14 14:02:47 -04006646 }
6647
6648 ret = btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
6649 key.offset - backref_offset, disk_bytenr);
6650 btrfs_end_transaction(trans, root);
6651 if (ret) {
6652 ret = 0;
6653 goto out;
6654 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006655
6656 /*
6657 * adjust disk_bytenr and num_bytes to cover just the bytes
6658 * in this extent we are about to write. If there
6659 * are any csums in that range we have to cow in order
6660 * to keep the csums correct
6661 */
6662 disk_bytenr += backref_offset;
6663 disk_bytenr += offset - key.offset;
Josef Bacikeb384b52013-04-24 16:32:55 -04006664 num_bytes = min(offset + *len, extent_end) - offset;
Chris Mason46bfbb52010-05-26 11:04:10 -04006665 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
6666 goto out;
6667 /*
6668 * all of the above have passed, it is safe to overwrite this extent
6669 * without cow
6670 */
Josef Bacikeb384b52013-04-24 16:32:55 -04006671 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04006672 ret = 1;
6673out:
6674 btrfs_free_path(path);
6675 return ret;
6676}
6677
Josef Bacikeb838e72012-07-31 16:28:48 -04006678static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
6679 struct extent_state **cached_state, int writing)
6680{
6681 struct btrfs_ordered_extent *ordered;
6682 int ret = 0;
6683
6684 while (1) {
6685 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6686 0, cached_state);
6687 /*
6688 * We're concerned with the entire range that we're going to be
6689 * doing DIO to, so we need to make sure theres no ordered
6690 * extents in this range.
6691 */
6692 ordered = btrfs_lookup_ordered_range(inode, lockstart,
6693 lockend - lockstart + 1);
6694
6695 /*
6696 * We need to make sure there are no buffered pages in this
6697 * range either, we could have raced between the invalidate in
6698 * generic_file_direct_write and locking the extent. The
6699 * invalidate needs to happen so that reads after a write do not
6700 * get stale data.
6701 */
6702 if (!ordered && (!writing ||
6703 !test_range_bit(&BTRFS_I(inode)->io_tree,
6704 lockstart, lockend, EXTENT_UPTODATE, 0,
6705 *cached_state)))
6706 break;
6707
6708 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6709 cached_state, GFP_NOFS);
6710
6711 if (ordered) {
6712 btrfs_start_ordered_extent(inode, ordered, 1);
6713 btrfs_put_ordered_extent(ordered);
6714 } else {
6715 /* Screw you mmap */
6716 ret = filemap_write_and_wait_range(inode->i_mapping,
6717 lockstart,
6718 lockend);
6719 if (ret)
6720 break;
6721
6722 /*
6723 * If we found a page that couldn't be invalidated just
6724 * fall back to buffered.
6725 */
6726 ret = invalidate_inode_pages2_range(inode->i_mapping,
6727 lockstart >> PAGE_CACHE_SHIFT,
6728 lockend >> PAGE_CACHE_SHIFT);
6729 if (ret)
6730 break;
6731 }
6732
6733 cond_resched();
6734 }
6735
6736 return ret;
6737}
6738
Josef Bacik69ffb542012-09-11 15:40:07 -04006739static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
6740 u64 len, u64 orig_start,
6741 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006742 u64 orig_block_len, u64 ram_bytes,
6743 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04006744{
6745 struct extent_map_tree *em_tree;
6746 struct extent_map *em;
6747 struct btrfs_root *root = BTRFS_I(inode)->root;
6748 int ret;
6749
6750 em_tree = &BTRFS_I(inode)->extent_tree;
6751 em = alloc_extent_map();
6752 if (!em)
6753 return ERR_PTR(-ENOMEM);
6754
6755 em->start = start;
6756 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04006757 em->mod_start = start;
6758 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04006759 em->len = len;
6760 em->block_len = block_len;
6761 em->block_start = block_start;
6762 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05006763 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04006764 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04006765 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04006766 set_bit(EXTENT_FLAG_PINNED, &em->flags);
6767 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05006768 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04006769
6770 do {
6771 btrfs_drop_extent_cache(inode, em->start,
6772 em->start + em->len - 1, 0);
6773 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006774 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04006775 write_unlock(&em_tree->lock);
6776 } while (ret == -EEXIST);
6777
6778 if (ret) {
6779 free_extent_map(em);
6780 return ERR_PTR(ret);
6781 }
6782
6783 return em;
6784}
6785
6786
Josef Bacik4b46fce2010-05-23 11:00:55 -04006787static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
6788 struct buffer_head *bh_result, int create)
6789{
6790 struct extent_map *em;
6791 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04006792 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006793 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04006794 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006795 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04006796 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00006797 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006798
Miao Xie172a5042013-02-21 02:48:22 -07006799 if (create)
Josef Bacikeb838e72012-07-31 16:28:48 -04006800 unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07006801 else
Josef Bacikc3298612012-08-03 16:49:19 -04006802 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04006803
Josef Bacikc3298612012-08-03 16:49:19 -04006804 lockstart = start;
6805 lockend = start + len - 1;
6806
Josef Bacikeb838e72012-07-31 16:28:48 -04006807 /*
6808 * If this errors out it's because we couldn't invalidate pagecache for
6809 * this range and we need to fallback to buffered.
6810 */
6811 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
6812 return -ENOTBLK;
6813
Josef Bacik4b46fce2010-05-23 11:00:55 -04006814 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04006815 if (IS_ERR(em)) {
6816 ret = PTR_ERR(em);
6817 goto unlock_err;
6818 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006819
6820 /*
6821 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
6822 * io. INLINE is special, and we could probably kludge it in here, but
6823 * it's still buffered so for safety lets just fall back to the generic
6824 * buffered path.
6825 *
6826 * For COMPRESSED we _have_ to read the entire extent in so we can
6827 * decompress it, so there will be buffering required no matter what we
6828 * do, so go ahead and fallback to buffered.
6829 *
6830 * We return -ENOTBLK because thats what makes DIO go ahead and go back
6831 * to buffered IO. Don't blame me, this is the price we pay for using
6832 * the generic code.
6833 */
6834 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
6835 em->block_start == EXTENT_MAP_INLINE) {
6836 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006837 ret = -ENOTBLK;
6838 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006839 }
6840
6841 /* Just a good old fashioned hole, return */
6842 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
6843 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
6844 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006845 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006846 }
6847
6848 /*
6849 * We don't allocate a new extent in the following cases
6850 *
6851 * 1) The inode is marked as NODATACOW. In this case we'll just use the
6852 * existing extent.
6853 * 2) The extent is marked as PREALLOC. We're good to go here and can
6854 * just use the extent.
6855 *
6856 */
Chris Mason46bfbb52010-05-26 11:04:10 -04006857 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04006858 len = min(len, em->len - (start - em->start));
6859 lockstart = start + len;
6860 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04006861 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006862
6863 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
6864 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
6865 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04006866 int type;
6867 int ret;
Josef Bacikeb384b52013-04-24 16:32:55 -04006868 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006869
6870 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6871 type = BTRFS_ORDERED_PREALLOC;
6872 else
6873 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04006874 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04006875 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04006876
Josef Bacik00361582013-08-14 14:02:47 -04006877 if (can_nocow_extent(inode, start, &len, &orig_start,
Josef Bacik7ee9e442013-06-21 16:37:03 -04006878 &orig_block_len, &ram_bytes) == 1) {
Josef Bacik69ffb542012-09-11 15:40:07 -04006879 if (type == BTRFS_ORDERED_PREALLOC) {
6880 free_extent_map(em);
6881 em = create_pinned_em(inode, start, len,
6882 orig_start,
Josef Bacikb4939682012-12-03 10:31:19 -05006883 block_start, len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006884 orig_block_len,
6885 ram_bytes, type);
Josef Bacik00361582013-08-14 14:02:47 -04006886 if (IS_ERR(em))
Josef Bacik69ffb542012-09-11 15:40:07 -04006887 goto unlock_err;
Josef Bacik69ffb542012-09-11 15:40:07 -04006888 }
6889
Chris Mason46bfbb52010-05-26 11:04:10 -04006890 ret = btrfs_add_ordered_extent_dio(inode, start,
6891 block_start, len, len, type);
Chris Mason46bfbb52010-05-26 11:04:10 -04006892 if (ret) {
6893 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006894 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04006895 }
6896 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006897 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006898 }
Josef Bacik00361582013-08-14 14:02:47 -04006899
Chris Mason46bfbb52010-05-26 11:04:10 -04006900 /*
6901 * this will cow the extent, reset the len in case we changed
6902 * it above
6903 */
6904 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04006905 free_extent_map(em);
6906 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04006907 if (IS_ERR(em)) {
6908 ret = PTR_ERR(em);
6909 goto unlock_err;
6910 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006911 len = min(len, em->len - (start - em->start));
6912unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04006913 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
6914 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04006915 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006916 bh_result->b_bdev = em->bdev;
6917 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04006918 if (create) {
6919 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6920 set_buffer_new(bh_result);
6921
6922 /*
6923 * Need to update the i_size under the extent lock so buffered
6924 * readers will get the updated i_size when we unlock.
6925 */
6926 if (start + len > i_size_read(inode))
6927 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00006928
Miao Xie172a5042013-02-21 02:48:22 -07006929 spin_lock(&BTRFS_I(inode)->lock);
6930 BTRFS_I(inode)->outstanding_extents++;
6931 spin_unlock(&BTRFS_I(inode)->lock);
6932
Miao Xie09348562013-02-07 10:12:07 +00006933 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6934 lockstart + len - 1, EXTENT_DELALLOC, NULL,
6935 &cached_state, GFP_NOFS);
6936 BUG_ON(ret);
Josef Bacikc3473e82012-06-19 10:59:00 -04006937 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006938
Josef Bacikeb838e72012-07-31 16:28:48 -04006939 /*
6940 * In the case of write we need to clear and unlock the entire range,
6941 * in the case of read we need to unlock only the end area that we
6942 * aren't using if there is any left over space.
6943 */
Liu Bo24c03fa2012-08-22 20:10:38 -06006944 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00006945 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6946 lockend, unlock_bits, 1, 0,
6947 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06006948 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04006949 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06006950 }
Josef Bacikeb838e72012-07-31 16:28:48 -04006951
Josef Bacik4b46fce2010-05-23 11:00:55 -04006952 free_extent_map(em);
6953
6954 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006955
6956unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04006957 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6958 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
6959 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006960}
6961
Josef Bacik4b46fce2010-05-23 11:00:55 -04006962static void btrfs_endio_direct_read(struct bio *bio, int err)
6963{
Miao Xiee65e1532010-11-22 03:04:43 +00006964 struct btrfs_dio_private *dip = bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006965 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
6966 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006967 struct inode *inode = dip->inode;
6968 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason9be33952013-05-17 18:30:14 -04006969 struct bio *dio_bio;
Miao Xiefacc8a222013-07-25 19:22:34 +08006970 u32 *csums = (u32 *)dip->csum;
6971 int index = 0;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006972 u64 start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006973
6974 start = dip->logical_offset;
6975 do {
6976 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
6977 struct page *page = bvec->bv_page;
6978 char *kaddr;
6979 u32 csum = ~(u32)0;
6980 unsigned long flags;
6981
6982 local_irq_save(flags);
Cong Wang7ac687d2011-11-25 23:14:28 +08006983 kaddr = kmap_atomic(page);
Liu Bob0496682013-03-14 14:57:45 +00006984 csum = btrfs_csum_data(kaddr + bvec->bv_offset,
Josef Bacik4b46fce2010-05-23 11:00:55 -04006985 csum, bvec->bv_len);
6986 btrfs_csum_final(csum, (char *)&csum);
Cong Wang7ac687d2011-11-25 23:14:28 +08006987 kunmap_atomic(kaddr);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006988 local_irq_restore(flags);
6989
6990 flush_dcache_page(bvec->bv_page);
Miao Xiefacc8a222013-07-25 19:22:34 +08006991 if (csum != csums[index]) {
6992 btrfs_err(root->fs_info, "csum failed ino %llu off %llu csum %u expected csum %u",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006993 btrfs_ino(inode), start, csum,
6994 csums[index]);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006995 err = -EIO;
6996 }
6997 }
6998
6999 start += bvec->bv_len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007000 bvec++;
Miao Xiefacc8a222013-07-25 19:22:34 +08007001 index++;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007002 } while (bvec <= bvec_end);
7003
7004 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007005 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04007006 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007007
Josef Bacik4b46fce2010-05-23 11:00:55 -04007008 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04007009
7010 /* If we had a csum failure make sure to clear the uptodate flag */
7011 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04007012 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
7013 dio_end_io(dio_bio, err);
7014 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007015}
7016
7017static void btrfs_endio_direct_write(struct bio *bio, int err)
7018{
7019 struct btrfs_dio_private *dip = bio->bi_private;
7020 struct inode *inode = dip->inode;
7021 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007022 struct btrfs_ordered_extent *ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05007023 u64 ordered_offset = dip->logical_offset;
7024 u64 ordered_bytes = dip->bytes;
Chris Mason9be33952013-05-17 18:30:14 -04007025 struct bio *dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007026 int ret;
7027
7028 if (err)
7029 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05007030again:
7031 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
7032 &ordered_offset,
Josef Bacik5fd02042012-05-02 14:00:54 -04007033 ordered_bytes, !err);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007034 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05007035 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007036
Josef Bacik5fd02042012-05-02 14:00:54 -04007037 ordered->work.func = finish_ordered_fn;
7038 ordered->work.flags = 0;
7039 btrfs_queue_worker(&root->fs_info->endio_write_workers,
7040 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05007041out_test:
7042 /*
7043 * our bio might span multiple ordered extents. If we haven't
7044 * completed the accounting for the whole dio, go back and try again
7045 */
7046 if (ordered_offset < dip->logical_offset + dip->bytes) {
7047 ordered_bytes = dip->logical_offset + dip->bytes -
7048 ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04007049 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05007050 goto again;
7051 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007052out_done:
Chris Mason9be33952013-05-17 18:30:14 -04007053 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007054
Josef Bacik4b46fce2010-05-23 11:00:55 -04007055 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04007056
7057 /* If we had an error make sure to clear the uptodate flag */
7058 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04007059 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
7060 dio_end_io(dio_bio, err);
7061 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007062}
7063
Chris Masoneaf25d92010-05-25 09:48:28 -04007064static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
7065 struct bio *bio, int mirror_num,
7066 unsigned long bio_flags, u64 offset)
7067{
7068 int ret;
7069 struct btrfs_root *root = BTRFS_I(inode)->root;
7070 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007071 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04007072 return 0;
7073}
7074
Miao Xiee65e1532010-11-22 03:04:43 +00007075static void btrfs_end_dio_bio(struct bio *bio, int err)
7076{
7077 struct btrfs_dio_private *dip = bio->bi_private;
7078
7079 if (err) {
Frank Holtonefe120a2013-12-20 11:37:06 -05007080 btrfs_err(BTRFS_I(dip->inode)->root->fs_info,
7081 "direct IO failed ino %llu rw %lu sector %#Lx len %u err no %d",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02007082 btrfs_ino(dip->inode), bio->bi_rw,
Jan Beulich3dd14622010-12-07 14:54:09 +00007083 (unsigned long long)bio->bi_sector, bio->bi_size, err);
Miao Xiee65e1532010-11-22 03:04:43 +00007084 dip->errors = 1;
7085
7086 /*
7087 * before atomic variable goto zero, we must make sure
7088 * dip->errors is perceived to be set.
7089 */
7090 smp_mb__before_atomic_dec();
7091 }
7092
7093 /* if there are more bios still pending for this dio, just exit */
7094 if (!atomic_dec_and_test(&dip->pending_bios))
7095 goto out;
7096
Chris Mason9be33952013-05-17 18:30:14 -04007097 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00007098 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04007099 } else {
7100 set_bit(BIO_UPTODATE, &dip->dio_bio->bi_flags);
Miao Xiee65e1532010-11-22 03:04:43 +00007101 bio_endio(dip->orig_bio, 0);
7102 }
7103out:
7104 bio_put(bio);
7105}
7106
7107static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
7108 u64 first_sector, gfp_t gfp_flags)
7109{
7110 int nr_vecs = bio_get_nr_vecs(bdev);
7111 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
7112}
7113
7114static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
7115 int rw, u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007116 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00007117{
Miao Xiefacc8a222013-07-25 19:22:34 +08007118 struct btrfs_dio_private *dip = bio->bi_private;
Miao Xiee65e1532010-11-22 03:04:43 +00007119 int write = rw & REQ_WRITE;
7120 struct btrfs_root *root = BTRFS_I(inode)->root;
7121 int ret;
7122
Josef Bacikb812ce22012-11-16 13:56:32 -05007123 if (async_submit)
7124 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
7125
Miao Xiee65e1532010-11-22 03:04:43 +00007126 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04007127
7128 if (!write) {
7129 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
7130 if (ret)
7131 goto err;
7132 }
Miao Xiee65e1532010-11-22 03:04:43 +00007133
Josef Bacik1ae39932011-04-06 14:41:34 -04007134 if (skip_sum)
7135 goto map;
7136
7137 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00007138 ret = btrfs_wq_submit_bio(root->fs_info,
7139 inode, rw, bio, 0, 0,
7140 file_offset,
7141 __btrfs_submit_bio_start_direct_io,
7142 __btrfs_submit_bio_done);
7143 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04007144 } else if (write) {
7145 /*
7146 * If we aren't doing async submit, calculate the csum of the
7147 * bio now.
7148 */
7149 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
7150 if (ret)
7151 goto err;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007152 } else if (!skip_sum) {
Miao Xiefacc8a222013-07-25 19:22:34 +08007153 ret = btrfs_lookup_bio_sums_dio(root, inode, dip, bio,
7154 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007155 if (ret)
7156 goto err;
7157 }
Miao Xiee65e1532010-11-22 03:04:43 +00007158
Josef Bacik1ae39932011-04-06 14:41:34 -04007159map:
7160 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007161err:
7162 bio_put(bio);
7163 return ret;
7164}
7165
7166static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
7167 int skip_sum)
7168{
7169 struct inode *inode = dip->inode;
7170 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00007171 struct bio *bio;
7172 struct bio *orig_bio = dip->orig_bio;
7173 struct bio_vec *bvec = orig_bio->bi_io_vec;
7174 u64 start_sector = orig_bio->bi_sector;
7175 u64 file_offset = dip->logical_offset;
7176 u64 submit_len = 0;
7177 u64 map_length;
7178 int nr_pages = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007179 int ret = 0;
Josef Bacik1ae39932011-04-06 14:41:34 -04007180 int async_submit = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007181
Miao Xiee65e1532010-11-22 03:04:43 +00007182 map_length = orig_bio->bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007183 ret = btrfs_map_block(root->fs_info, rw, start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007184 &map_length, NULL, 0);
7185 if (ret) {
Josef Bacik64728bb2011-04-25 19:43:52 -04007186 bio_put(orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00007187 return -EIO;
7188 }
Miao Xiefacc8a222013-07-25 19:22:34 +08007189
Josef Bacik02f57c72011-04-06 14:25:44 -04007190 if (map_length >= orig_bio->bi_size) {
7191 bio = orig_bio;
7192 goto submit;
7193 }
7194
David Woodhouse53b381b2013-01-29 18:40:14 -05007195 /* async crcs make it difficult to collect full stripe writes. */
7196 if (btrfs_get_alloc_profile(root, 1) &
7197 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6))
7198 async_submit = 0;
7199 else
7200 async_submit = 1;
7201
Josef Bacik02f57c72011-04-06 14:25:44 -04007202 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
7203 if (!bio)
7204 return -ENOMEM;
7205 bio->bi_private = dip;
7206 bio->bi_end_io = btrfs_end_dio_bio;
7207 atomic_inc(&dip->pending_bios);
7208
Miao Xiee65e1532010-11-22 03:04:43 +00007209 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
7210 if (unlikely(map_length < submit_len + bvec->bv_len ||
7211 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
7212 bvec->bv_offset) < bvec->bv_len)) {
7213 /*
7214 * inc the count before we submit the bio so
7215 * we know the end IO handler won't happen before
7216 * we inc the count. Otherwise, the dip might get freed
7217 * before we're done setting it up
7218 */
7219 atomic_inc(&dip->pending_bios);
7220 ret = __btrfs_submit_dio_bio(bio, inode, rw,
7221 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007222 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007223 if (ret) {
7224 bio_put(bio);
7225 atomic_dec(&dip->pending_bios);
7226 goto out_err;
7227 }
7228
Miao Xiee65e1532010-11-22 03:04:43 +00007229 start_sector += submit_len >> 9;
7230 file_offset += submit_len;
7231
7232 submit_len = 0;
7233 nr_pages = 0;
7234
7235 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
7236 start_sector, GFP_NOFS);
7237 if (!bio)
7238 goto out_err;
7239 bio->bi_private = dip;
7240 bio->bi_end_io = btrfs_end_dio_bio;
7241
7242 map_length = orig_bio->bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007243 ret = btrfs_map_block(root->fs_info, rw,
Stefan Behrens3ec706c2012-11-05 15:46:42 +01007244 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007245 &map_length, NULL, 0);
7246 if (ret) {
7247 bio_put(bio);
7248 goto out_err;
7249 }
7250 } else {
7251 submit_len += bvec->bv_len;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307252 nr_pages++;
Miao Xiee65e1532010-11-22 03:04:43 +00007253 bvec++;
7254 }
7255 }
7256
Josef Bacik02f57c72011-04-06 14:25:44 -04007257submit:
Miao Xiee65e1532010-11-22 03:04:43 +00007258 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007259 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007260 if (!ret)
7261 return 0;
7262
7263 bio_put(bio);
7264out_err:
7265 dip->errors = 1;
7266 /*
7267 * before atomic variable goto zero, we must
7268 * make sure dip->errors is perceived to be set.
7269 */
7270 smp_mb__before_atomic_dec();
7271 if (atomic_dec_and_test(&dip->pending_bios))
7272 bio_io_error(dip->orig_bio);
7273
7274 /* bio_end_io() will handle error, so we needn't return it */
7275 return 0;
7276}
7277
Chris Mason9be33952013-05-17 18:30:14 -04007278static void btrfs_submit_direct(int rw, struct bio *dio_bio,
7279 struct inode *inode, loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007280{
7281 struct btrfs_root *root = BTRFS_I(inode)->root;
7282 struct btrfs_dio_private *dip;
Chris Mason9be33952013-05-17 18:30:14 -04007283 struct bio *io_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007284 int skip_sum;
Miao Xiefacc8a222013-07-25 19:22:34 +08007285 int sum_len;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02007286 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007287 int ret = 0;
Miao Xiefacc8a222013-07-25 19:22:34 +08007288 u16 csum_size;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007289
7290 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
7291
Chris Mason9be33952013-05-17 18:30:14 -04007292 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04007293 if (!io_bio) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007294 ret = -ENOMEM;
7295 goto free_ordered;
7296 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007297
Miao Xiefacc8a222013-07-25 19:22:34 +08007298 if (!skip_sum && !write) {
7299 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
7300 sum_len = dio_bio->bi_size >> inode->i_sb->s_blocksize_bits;
7301 sum_len *= csum_size;
7302 } else {
7303 sum_len = 0;
7304 }
7305
7306 dip = kmalloc(sizeof(*dip) + sum_len, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04007307 if (!dip) {
7308 ret = -ENOMEM;
7309 goto free_io_bio;
7310 }
7311
7312 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007313 dip->inode = inode;
7314 dip->logical_offset = file_offset;
Miao Xiee6da5d22013-06-19 18:19:17 +08007315 dip->bytes = dio_bio->bi_size;
Chris Mason9be33952013-05-17 18:30:14 -04007316 dip->disk_bytenr = (u64)dio_bio->bi_sector << 9;
7317 io_bio->bi_private = dip;
Miao Xiee65e1532010-11-22 03:04:43 +00007318 dip->errors = 0;
Chris Mason9be33952013-05-17 18:30:14 -04007319 dip->orig_bio = io_bio;
7320 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00007321 atomic_set(&dip->pending_bios, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007322
7323 if (write)
Chris Mason9be33952013-05-17 18:30:14 -04007324 io_bio->bi_end_io = btrfs_endio_direct_write;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007325 else
Chris Mason9be33952013-05-17 18:30:14 -04007326 io_bio->bi_end_io = btrfs_endio_direct_read;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007327
Miao Xiee65e1532010-11-22 03:04:43 +00007328 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
7329 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04007330 return;
Chris Mason9be33952013-05-17 18:30:14 -04007331
7332free_io_bio:
7333 bio_put(io_bio);
7334
Josef Bacik4b46fce2010-05-23 11:00:55 -04007335free_ordered:
7336 /*
7337 * If this is a write, we need to clean up the reserved space and kill
7338 * the ordered extent.
7339 */
7340 if (write) {
7341 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05007342 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007343 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
7344 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
7345 btrfs_free_reserved_extent(root, ordered->start,
7346 ordered->disk_len);
7347 btrfs_put_ordered_extent(ordered);
7348 btrfs_put_ordered_extent(ordered);
7349 }
Chris Mason9be33952013-05-17 18:30:14 -04007350 bio_endio(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007351}
7352
Chris Mason5a5f79b2010-05-26 21:33:37 -04007353static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
7354 const struct iovec *iov, loff_t offset,
7355 unsigned long nr_segs)
7356{
7357 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00007358 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007359 size_t size;
7360 unsigned long addr;
7361 unsigned blocksize_mask = root->sectorsize - 1;
7362 ssize_t retval = -EINVAL;
7363 loff_t end = offset;
7364
7365 if (offset & blocksize_mask)
7366 goto out;
7367
7368 /* Check the memory alignment. Blocks cannot straddle pages */
7369 for (seg = 0; seg < nr_segs; seg++) {
7370 addr = (unsigned long)iov[seg].iov_base;
7371 size = iov[seg].iov_len;
7372 end += size;
Josef Bacika1b75f72011-04-08 15:51:18 +00007373 if ((addr & blocksize_mask) || (size & blocksize_mask))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007374 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00007375
7376 /* If this is a write we don't need to check anymore */
7377 if (rw & WRITE)
7378 continue;
7379
7380 /*
7381 * Check to make sure we don't have duplicate iov_base's in this
7382 * iovec, if so return EINVAL, otherwise we'll get csum errors
7383 * when reading back.
7384 */
7385 for (i = seg + 1; i < nr_segs; i++) {
7386 if (iov[seg].iov_base == iov[i].iov_base)
7387 goto out;
7388 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04007389 }
7390 retval = 0;
7391out:
7392 return retval;
7393}
Josef Bacikeb838e72012-07-31 16:28:48 -04007394
Chris Mason16432982008-04-10 10:23:21 -04007395static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
7396 const struct iovec *iov, loff_t offset,
7397 unsigned long nr_segs)
7398{
Josef Bacik4b46fce2010-05-23 11:00:55 -04007399 struct file *file = iocb->ki_filp;
7400 struct inode *inode = file->f_mapping->host;
Miao Xie09348562013-02-07 10:12:07 +00007401 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00007402 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00007403 bool wakeup = true;
7404 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00007405 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007406
Chris Mason5a5f79b2010-05-26 21:33:37 -04007407 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
Josef Bacikeb838e72012-07-31 16:28:48 -04007408 offset, nr_segs))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007409 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007410
Miao Xie38851cc2013-02-08 07:04:11 +00007411 atomic_inc(&inode->i_dio_count);
7412 smp_mb__after_atomic_inc();
7413
Josef Bacik0e267c42013-07-02 10:38:02 -04007414 /*
7415 * The generic stuff only does filemap_write_and_wait_range, which isn't
7416 * enough if we've written compressed pages to this area, so we need to
7417 * call btrfs_wait_ordered_range to make absolutely sure that any
7418 * outstanding dirty pages are on disk.
7419 */
7420 count = iov_length(iov, nr_segs);
Josef Bacik0ef8b722013-10-25 16:13:35 -04007421 ret = btrfs_wait_ordered_range(inode, offset, count);
7422 if (ret)
7423 return ret;
Josef Bacik0e267c42013-07-02 10:38:02 -04007424
Miao Xie09348562013-02-07 10:12:07 +00007425 if (rw & WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00007426 /*
7427 * If the write DIO is beyond the EOF, we need update
7428 * the isize, but it is protected by i_mutex. So we can
7429 * not unlock the i_mutex at this case.
7430 */
7431 if (offset + count <= inode->i_size) {
7432 mutex_unlock(&inode->i_mutex);
7433 relock = true;
7434 }
Miao Xie09348562013-02-07 10:12:07 +00007435 ret = btrfs_delalloc_reserve_space(inode, count);
7436 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00007437 goto out;
7438 } else if (unlikely(test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
7439 &BTRFS_I(inode)->runtime_flags))) {
7440 inode_dio_done(inode);
7441 flags = DIO_LOCKING | DIO_SKIP_HOLES;
7442 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00007443 }
7444
7445 ret = __blockdev_direct_IO(rw, iocb, inode,
7446 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
7447 iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
Miao Xie2e60a512013-02-08 07:01:08 +00007448 btrfs_submit_direct, flags);
Miao Xie09348562013-02-07 10:12:07 +00007449 if (rw & WRITE) {
7450 if (ret < 0 && ret != -EIOCBQUEUED)
7451 btrfs_delalloc_release_space(inode, count);
Miao Xie172a5042013-02-21 02:48:22 -07007452 else if (ret >= 0 && (size_t)ret < count)
Miao Xie09348562013-02-07 10:12:07 +00007453 btrfs_delalloc_release_space(inode,
7454 count - (size_t)ret);
Miao Xie172a5042013-02-21 02:48:22 -07007455 else
7456 btrfs_delalloc_release_metadata(inode, 0);
Miao Xie09348562013-02-07 10:12:07 +00007457 }
Miao Xie38851cc2013-02-08 07:04:11 +00007458out:
Miao Xie2e60a512013-02-08 07:01:08 +00007459 if (wakeup)
7460 inode_dio_done(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00007461 if (relock)
7462 mutex_lock(&inode->i_mutex);
Miao Xie09348562013-02-07 10:12:07 +00007463
7464 return ret;
Chris Mason16432982008-04-10 10:23:21 -04007465}
7466
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007467#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
7468
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007469static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
7470 __u64 start, __u64 len)
7471{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007472 int ret;
7473
7474 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
7475 if (ret)
7476 return ret;
7477
Chris Masonec29ed52011-02-23 16:23:20 -05007478 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007479}
7480
Chris Mason9ebefb182007-06-15 13:50:00 -04007481int btrfs_readpage(struct file *file, struct page *page)
7482{
Chris Masond1310b22008-01-24 16:13:08 -05007483 struct extent_io_tree *tree;
7484 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02007485 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04007486}
Chris Mason1832a6d2007-12-21 16:27:21 -05007487
Chris Mason39279cc2007-06-12 06:35:45 -04007488static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
7489{
Chris Masond1310b22008-01-24 16:13:08 -05007490 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04007491
7492
7493 if (current->flags & PF_MEMALLOC) {
7494 redirty_page_for_writepage(wbc, page);
7495 unlock_page(page);
7496 return 0;
7497 }
Chris Masond1310b22008-01-24 16:13:08 -05007498 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04007499 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
7500}
Chris Mason39279cc2007-06-12 06:35:45 -04007501
Eric Sandeen48a3b632013-04-25 20:41:01 +00007502static int btrfs_writepages(struct address_space *mapping,
7503 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04007504{
Chris Masond1310b22008-01-24 16:13:08 -05007505 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05007506
Chris Masond1310b22008-01-24 16:13:08 -05007507 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04007508 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
7509}
7510
Chris Mason3ab2fb52007-11-08 10:59:22 -05007511static int
7512btrfs_readpages(struct file *file, struct address_space *mapping,
7513 struct list_head *pages, unsigned nr_pages)
7514{
Chris Masond1310b22008-01-24 16:13:08 -05007515 struct extent_io_tree *tree;
7516 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05007517 return extent_readpages(tree, mapping, pages, nr_pages,
7518 btrfs_get_extent);
7519}
Chris Masone6dcd2d2008-07-17 12:53:50 -04007520static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04007521{
Chris Masond1310b22008-01-24 16:13:08 -05007522 struct extent_io_tree *tree;
7523 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04007524 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04007525
Chris Masond1310b22008-01-24 16:13:08 -05007526 tree = &BTRFS_I(page->mapping->host)->io_tree;
7527 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05007528 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04007529 if (ret == 1) {
7530 ClearPagePrivate(page);
7531 set_page_private(page, 0);
7532 page_cache_release(page);
7533 }
7534 return ret;
7535}
Chris Mason39279cc2007-06-12 06:35:45 -04007536
Chris Masone6dcd2d2008-07-17 12:53:50 -04007537static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
7538{
Chris Mason98509cf2008-09-11 15:51:43 -04007539 if (PageWriteback(page) || PageDirty(page))
7540 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05007541 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007542}
7543
Lukas Czernerd47992f2013-05-21 23:17:23 -04007544static void btrfs_invalidatepage(struct page *page, unsigned int offset,
7545 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04007546{
Josef Bacik5fd02042012-05-02 14:00:54 -04007547 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05007548 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007549 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007550 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007551 u64 page_start = page_offset(page);
7552 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00007553 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04007554
Chris Mason8b62b722009-09-02 16:53:46 -04007555 /*
7556 * we have the page locked, so new writeback can't start,
7557 * and the dirty bit won't be cleared while we are here.
7558 *
7559 * Wait for IO on this page so that we can safely clear
7560 * the PagePrivate2 bit and do ordered accounting
7561 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007562 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04007563
Josef Bacik5fd02042012-05-02 14:00:54 -04007564 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007565 if (offset) {
7566 btrfs_releasepage(page, GFP_NOFS);
7567 return;
7568 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00007569
7570 if (!inode_evicting)
7571 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
7572 ordered = btrfs_lookup_ordered_extent(inode, page_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007573 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04007574 /*
7575 * IO on this page will never be started, so we need
7576 * to account for any ordered extents now
7577 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00007578 if (!inode_evicting)
7579 clear_extent_bit(tree, page_start, page_end,
7580 EXTENT_DIRTY | EXTENT_DELALLOC |
7581 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
7582 EXTENT_DEFRAG, 1, 0, &cached_state,
7583 GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04007584 /*
7585 * whoever cleared the private bit is responsible
7586 * for the finish_ordered_io
7587 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04007588 if (TestClearPagePrivate2(page)) {
7589 struct btrfs_ordered_inode_tree *tree;
7590 u64 new_len;
7591
7592 tree = &BTRFS_I(inode)->ordered_tree;
7593
7594 spin_lock_irq(&tree->lock);
7595 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
7596 new_len = page_start - ordered->file_offset;
7597 if (new_len < ordered->truncated_len)
7598 ordered->truncated_len = new_len;
7599 spin_unlock_irq(&tree->lock);
7600
7601 if (btrfs_dec_test_ordered_pending(inode, &ordered,
7602 page_start,
7603 PAGE_CACHE_SIZE, 1))
7604 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04007605 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007606 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00007607 if (!inode_evicting) {
7608 cached_state = NULL;
7609 lock_extent_bits(tree, page_start, page_end, 0,
7610 &cached_state);
7611 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007612 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00007613
7614 if (!inode_evicting) {
7615 clear_extent_bit(tree, page_start, page_end,
7616 EXTENT_LOCKED | EXTENT_DIRTY |
7617 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
7618 EXTENT_DEFRAG, 1, 1,
7619 &cached_state, GFP_NOFS);
7620
7621 __btrfs_releasepage(page, GFP_NOFS);
7622 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007623
Chris Mason4a096752008-07-21 10:29:44 -04007624 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04007625 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04007626 ClearPagePrivate(page);
7627 set_page_private(page, 0);
7628 page_cache_release(page);
7629 }
Chris Mason39279cc2007-06-12 06:35:45 -04007630}
7631
Chris Mason9ebefb182007-06-15 13:50:00 -04007632/*
7633 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
7634 * called from a page fault handler when a page is first dirtied. Hence we must
7635 * be careful to check for EOF conditions here. We set the page up correctly
7636 * for a written page which means we get ENOSPC checking when writing into
7637 * holes and correct delalloc and unwritten extent mapping on filesystems that
7638 * support these features.
7639 *
7640 * We are not allowed to take the i_mutex here so we have to play games to
7641 * protect against truncate races as the page could now be beyond EOF. Because
7642 * vmtruncate() writes the inode size before removing pages, once we have the
7643 * page lock we can determine safely if the page is beyond EOF. If it is not
7644 * beyond EOF, then the page is guaranteed safe against truncation until we
7645 * unlock the page.
7646 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07007647int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04007648{
Nick Pigginc2ec1752009-03-31 15:23:21 -07007649 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05007650 struct inode *inode = file_inode(vma->vm_file);
Chris Mason1832a6d2007-12-21 16:27:21 -05007651 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007652 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7653 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007654 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007655 char *kaddr;
7656 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04007657 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05007658 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05007659 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007660 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007661 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04007662
Jan Karab2b5ef52012-06-12 16:20:45 +02007663 sb_start_pagefault(inode->i_sb);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04007664 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007665 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04007666 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05007667 reserved = 1;
7668 }
Nick Piggin56a76f82009-03-31 15:23:23 -07007669 if (ret) {
7670 if (ret == -ENOMEM)
7671 ret = VM_FAULT_OOM;
7672 else /* -ENOSPC, -EIO, etc */
7673 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05007674 if (reserved)
7675 goto out;
7676 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07007677 }
Chris Mason1832a6d2007-12-21 16:27:21 -05007678
Nick Piggin56a76f82009-03-31 15:23:23 -07007679 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007680again:
Chris Mason9ebefb182007-06-15 13:50:00 -04007681 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04007682 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007683 page_start = page_offset(page);
7684 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04007685
Chris Mason9ebefb182007-06-15 13:50:00 -04007686 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04007687 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04007688 /* page got truncated out from underneath us */
7689 goto out_unlock;
7690 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007691 wait_on_page_writeback(page);
7692
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007693 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007694 set_page_extent_mapped(page);
7695
Chris Masoneb84ae02008-07-17 13:53:27 -04007696 /*
7697 * we can't set the delalloc bits if there are pending ordered
7698 * extents. Drop our locks and wait for them to finish
7699 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007700 ordered = btrfs_lookup_ordered_extent(inode, page_start);
7701 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007702 unlock_extent_cached(io_tree, page_start, page_end,
7703 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007704 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04007705 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007706 btrfs_put_ordered_extent(ordered);
7707 goto again;
7708 }
7709
Josef Bacikfbf19082009-10-01 17:10:23 -04007710 /*
7711 * XXX - page_mkwrite gets called every time the page is dirtied, even
7712 * if it was already dirty, so for space accounting reasons we need to
7713 * clear any delalloc bits for the range we are fixing to save. There
7714 * is probably a better way to do this, but for now keep consistent with
7715 * prepare_pages in the normal write path.
7716 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00007717 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06007718 EXTENT_DIRTY | EXTENT_DELALLOC |
7719 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00007720 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04007721
Josef Bacik2ac55d42010-02-03 19:33:23 +00007722 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
7723 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007724 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007725 unlock_extent_cached(io_tree, page_start, page_end,
7726 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007727 ret = VM_FAULT_SIGBUS;
7728 goto out_unlock;
7729 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007730 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04007731
7732 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04007733 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04007734 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04007735 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04007736 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04007737
Chris Masone6dcd2d2008-07-17 12:53:50 -04007738 if (zero_start != PAGE_CACHE_SIZE) {
7739 kaddr = kmap(page);
7740 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
7741 flush_dcache_page(page);
7742 kunmap(page);
7743 }
Chris Mason247e7432008-07-17 12:53:51 -04007744 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007745 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04007746 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007747
Chris Mason257c62e2009-10-13 13:21:08 -04007748 BTRFS_I(inode)->last_trans = root->fs_info->generation;
7749 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06007750 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04007751
Josef Bacik2ac55d42010-02-03 19:33:23 +00007752 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04007753
7754out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02007755 if (!ret) {
7756 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04007757 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02007758 }
Chris Mason9ebefb182007-06-15 13:50:00 -04007759 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05007760out:
Josef Bacikec39e182012-01-12 19:10:12 -05007761 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007762out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02007763 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04007764 return ret;
7765}
7766
Josef Bacika41ad392011-01-31 15:30:16 -05007767static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04007768{
7769 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04007770 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04007771 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05007772 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007773 struct btrfs_trans_handle *trans;
Chris Masondbe674a2008-07-17 12:54:05 -04007774 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04007775 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04007776
Josef Bacik0ef8b722013-10-25 16:13:35 -04007777 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
7778 (u64)-1);
7779 if (ret)
7780 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04007781
Josef Bacikfcb80c22011-05-03 10:40:22 -04007782 /*
7783 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
7784 * 3 things going on here
7785 *
7786 * 1) We need to reserve space for our orphan item and the space to
7787 * delete our orphan item. Lord knows we don't want to have a dangling
7788 * orphan item because we didn't reserve space to remove it.
7789 *
7790 * 2) We need to reserve space to update our inode.
7791 *
7792 * 3) We need to have something to cache all the space that is going to
7793 * be free'd up by the truncate operation, but also have some slack
7794 * space reserved in case it uses space during the truncate (thank you
7795 * very much snapshotting).
7796 *
7797 * And we need these to all be seperate. The fact is we can use alot of
7798 * space doing the truncate, and we have no earthly idea how much space
7799 * we will use, so we need the truncate reservation to be seperate so it
7800 * doesn't end up using space reserved for updating the inode or
7801 * removing the orphan item. We also need to be able to stop the
7802 * transaction and start a new one, which means we need to be able to
7803 * update the inode several times, and we have no idea of knowing how
7804 * many times that will be, so we can't just reserve 1 item for the
7805 * entirety of the opration, so that has to be done seperately as well.
7806 * Then there is the orphan item, which does indeed need to be held on
7807 * to for the whole operation, and we need nobody to touch this reserved
7808 * space except the orphan code.
7809 *
7810 * So that leaves us with
7811 *
7812 * 1) root->orphan_block_rsv - for the orphan deletion.
7813 * 2) rsv - for the truncate reservation, which we will steal from the
7814 * transaction reservation.
7815 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
7816 * updating the inode.
7817 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06007818 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007819 if (!rsv)
7820 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04007821 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04007822 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05007823
Josef Bacik907cbce2011-08-08 13:46:15 -04007824 /*
Josef Bacik07127182011-08-19 10:29:59 -04007825 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04007826 * 1 for updating the inode.
7827 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05007828 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007829 if (IS_ERR(trans)) {
7830 err = PTR_ERR(trans);
7831 goto out;
7832 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05007833
Josef Bacik907cbce2011-08-08 13:46:15 -04007834 /* Migrate the slack space for the truncate to our reserve */
7835 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
7836 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007837 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05007838
Chris Mason5a3f23d2009-03-31 13:27:11 -04007839 /*
7840 * setattr is responsible for setting the ordered_data_close flag,
7841 * but that is only tested during the last file release. That
7842 * could happen well after the next commit, leaving a great big
7843 * window where new writes may get lost if someone chooses to write
7844 * to this file after truncating to zero
7845 *
7846 * The inode doesn't have any dirty data here, and so if we commit
7847 * this is a noop. If someone immediately starts writing to the inode
7848 * it is very likely we'll catch some of their writes in this
7849 * transaction, and the commit will find this file on the ordered
7850 * data list with good things to send down.
7851 *
7852 * This is a best effort solution, there is still a window where
7853 * using truncate to replace the contents of the file will
7854 * end up with a zero length file after a crash.
7855 */
Josef Bacik72ac3c02012-05-23 14:13:11 -04007856 if (inode->i_size == 0 && test_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
7857 &BTRFS_I(inode)->runtime_flags))
Chris Mason5a3f23d2009-03-31 13:27:11 -04007858 btrfs_add_ordered_operation(trans, root, inode);
7859
Josef Bacik5dc562c2012-08-17 13:14:17 -04007860 /*
7861 * So if we truncate and then write and fsync we normally would just
7862 * write the extents that changed, which is a problem if we need to
7863 * first truncate that entire inode. So set this flag so we write out
7864 * all of the extents in the inode to the sync log so we're completely
7865 * safe.
7866 */
7867 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007868 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04007869
Yan, Zheng80825102009-11-12 09:35:36 +00007870 while (1) {
7871 ret = btrfs_truncate_inode_items(trans, root, inode,
7872 inode->i_size,
7873 BTRFS_EXTENT_DATA_KEY);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007874 if (ret != -ENOSPC) {
Josef Bacik3893e332011-01-31 16:03:11 -05007875 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007876 break;
Josef Bacik3893e332011-01-31 16:03:11 -05007877 }
Chris Mason39279cc2007-06-12 06:35:45 -04007878
Josef Bacikfcb80c22011-05-03 10:40:22 -04007879 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007880 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007881 if (ret) {
7882 err = ret;
7883 break;
7884 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04007885
Yan, Zheng80825102009-11-12 09:35:36 +00007886 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007887 btrfs_btree_balance_dirty(root);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007888
7889 trans = btrfs_start_transaction(root, 2);
7890 if (IS_ERR(trans)) {
7891 ret = err = PTR_ERR(trans);
7892 trans = NULL;
7893 break;
7894 }
7895
7896 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
7897 rsv, min_size);
7898 BUG_ON(ret); /* shouldn't happen */
7899 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007900 }
7901
7902 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04007903 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007904 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007905 if (ret)
7906 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007907 }
7908
Chris Mason917c16b2011-11-08 14:49:59 -05007909 if (trans) {
7910 trans->block_rsv = &root->fs_info->trans_block_rsv;
7911 ret = btrfs_update_inode(trans, root, inode);
7912 if (ret && !err)
7913 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04007914
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007915 ret = btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007916 btrfs_btree_balance_dirty(root);
Chris Mason917c16b2011-11-08 14:49:59 -05007917 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04007918
7919out:
7920 btrfs_free_block_rsv(root, rsv);
7921
Josef Bacik3893e332011-01-31 16:03:11 -05007922 if (ret && !err)
7923 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05007924
Josef Bacik3893e332011-01-31 16:03:11 -05007925 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007926}
7927
Sven Wegener3b963622008-06-09 21:57:42 -04007928/*
Chris Masond352ac62008-09-29 15:18:18 -04007929 * create a new subvolume directory/inode (helper for the ioctl).
7930 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05007931int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00007932 struct btrfs_root *new_root,
7933 struct btrfs_root *parent_root,
7934 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04007935{
Chris Mason39279cc2007-06-12 06:35:45 -04007936 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04007937 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007938 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007939
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01007940 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
7941 new_dirid, new_dirid,
7942 S_IFDIR | (~current_umask() & S_IRWXUGO),
7943 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04007944 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04007945 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007946 inode->i_op = &btrfs_dir_inode_operations;
7947 inode->i_fop = &btrfs_dir_file_operations;
7948
Miklos Szeredibfe86842011-10-28 14:13:29 +02007949 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04007950 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04007951
Filipe David Borba Manana63541922014-01-07 11:47:46 +00007952 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
7953 if (err)
7954 btrfs_err(new_root->fs_info,
7955 "error inheriting subvolume %llu properties: %d\n",
7956 new_root->root_key.objectid, err);
7957
Yan, Zheng76dda932009-09-21 16:00:26 -04007958 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04007959
Yan, Zheng76dda932009-09-21 16:00:26 -04007960 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07007961 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007962}
7963
Chris Mason39279cc2007-06-12 06:35:45 -04007964struct inode *btrfs_alloc_inode(struct super_block *sb)
7965{
7966 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007967 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007968
7969 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
7970 if (!ei)
7971 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007972
7973 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007974 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04007975 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04007976 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04007977 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007978 ei->delalloc_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007979 ei->disk_i_size = 0;
7980 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04007981 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007982 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08007983 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007984 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06007985 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007986
Josef Bacik9e0baf62011-07-15 15:16:44 +00007987 spin_lock_init(&ei->lock);
7988 ei->outstanding_extents = 0;
7989 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007990
Josef Bacik72ac3c02012-05-23 14:13:11 -04007991 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08007992 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007993
Miao Xie16cdcec2011-04-22 18:12:22 +08007994 ei->delayed_node = NULL;
7995
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007996 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02007997 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02007998 extent_io_tree_init(&ei->io_tree, &inode->i_data);
7999 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04008000 ei->io_tree.track_uptodate = 1;
8001 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05008002 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008003 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05008004 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008005 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008006 INIT_LIST_HEAD(&ei->delalloc_inodes);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008007 INIT_LIST_HEAD(&ei->ordered_operations);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008008 RB_CLEAR_NODE(&ei->rb_node);
8009
8010 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008011}
8012
Josef Bacikaaedb552013-10-11 14:44:09 -04008013#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
8014void btrfs_test_destroy_inode(struct inode *inode)
8015{
8016 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
8017 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8018}
8019#endif
8020
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008021static void btrfs_i_callback(struct rcu_head *head)
8022{
8023 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008024 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8025}
8026
Chris Mason39279cc2007-06-12 06:35:45 -04008027void btrfs_destroy_inode(struct inode *inode)
8028{
Chris Masone6dcd2d2008-07-17 12:53:50 -04008029 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008030 struct btrfs_root *root = BTRFS_I(inode)->root;
8031
Al Virob3d9b7a2012-06-09 13:51:19 -04008032 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04008033 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00008034 WARN_ON(BTRFS_I(inode)->outstanding_extents);
8035 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04008036 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
8037 WARN_ON(BTRFS_I(inode)->csum_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04008038
Chris Mason5a3f23d2009-03-31 13:27:11 -04008039 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05008040 * This can happen where we create an inode, but somebody else also
8041 * created the same inode and we need to destroy the one we already
8042 * created.
8043 */
8044 if (!root)
8045 goto free;
8046
8047 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04008048 * Make sure we're properly removed from the ordered operation
8049 * lists.
8050 */
8051 smp_mb();
8052 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
Miao Xie199c2a92013-05-15 07:48:23 +00008053 spin_lock(&root->fs_info->ordered_root_lock);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008054 list_del_init(&BTRFS_I(inode)->ordered_operations);
Miao Xie199c2a92013-05-15 07:48:23 +00008055 spin_unlock(&root->fs_info->ordered_root_lock);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008056 }
8057
Josef Bacik8a35d952012-05-23 14:26:42 -04008058 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
8059 &BTRFS_I(inode)->runtime_flags)) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008060 btrfs_info(root->fs_info, "inode %llu still on the orphan list",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02008061 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04008062 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04008063 }
Josef Bacik7b128762008-07-24 12:17:14 -04008064
Chris Masond3977122009-01-05 21:25:51 -05008065 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008066 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
8067 if (!ordered)
8068 break;
8069 else {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008070 btrfs_err(root->fs_info, "found ordered extent %llu %llu on inode cleanup",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02008071 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008072 btrfs_remove_ordered_extent(inode, ordered);
8073 btrfs_put_ordered_extent(ordered);
8074 btrfs_put_ordered_extent(ordered);
8075 }
8076 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008077 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008078 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05008079free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008080 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04008081}
8082
Al Viro45321ac2010-06-07 13:43:19 -04008083int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04008084{
8085 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04008086
Naohiro Aota6379ef92013-06-06 09:56:34 +00008087 if (root == NULL)
8088 return 1;
8089
Liu Bofa6ac872013-02-20 14:10:23 +00008090 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02008091 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04008092 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04008093 else
Al Viro45321ac2010-06-07 13:43:19 -04008094 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04008095}
8096
Sven Wegener0ee0fda2008-07-30 16:54:26 -04008097static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04008098{
8099 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
8100
8101 inode_init_once(&ei->vfs_inode);
8102}
8103
8104void btrfs_destroy_cachep(void)
8105{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10008106 /*
8107 * Make sure all delayed rcu free inodes are flushed before we
8108 * destroy cache.
8109 */
8110 rcu_barrier();
Chris Mason39279cc2007-06-12 06:35:45 -04008111 if (btrfs_inode_cachep)
8112 kmem_cache_destroy(btrfs_inode_cachep);
8113 if (btrfs_trans_handle_cachep)
8114 kmem_cache_destroy(btrfs_trans_handle_cachep);
8115 if (btrfs_transaction_cachep)
8116 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008117 if (btrfs_path_cachep)
8118 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05008119 if (btrfs_free_space_cachep)
8120 kmem_cache_destroy(btrfs_free_space_cachep);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008121 if (btrfs_delalloc_work_cachep)
8122 kmem_cache_destroy(btrfs_delalloc_work_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008123}
8124
8125int btrfs_init_cachep(void)
8126{
David Sterba837e1972012-09-07 03:00:48 -06008127 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008128 sizeof(struct btrfs_inode), 0,
8129 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04008130 if (!btrfs_inode_cachep)
8131 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008132
David Sterba837e1972012-09-07 03:00:48 -06008133 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008134 sizeof(struct btrfs_trans_handle), 0,
8135 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008136 if (!btrfs_trans_handle_cachep)
8137 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008138
David Sterba837e1972012-09-07 03:00:48 -06008139 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008140 sizeof(struct btrfs_transaction), 0,
8141 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008142 if (!btrfs_transaction_cachep)
8143 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008144
David Sterba837e1972012-09-07 03:00:48 -06008145 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008146 sizeof(struct btrfs_path), 0,
8147 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008148 if (!btrfs_path_cachep)
8149 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008150
David Sterba837e1972012-09-07 03:00:48 -06008151 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05008152 sizeof(struct btrfs_free_space), 0,
8153 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
8154 if (!btrfs_free_space_cachep)
8155 goto fail;
8156
Miao Xie8ccf6f192012-10-25 09:28:04 +00008157 btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
8158 sizeof(struct btrfs_delalloc_work), 0,
8159 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
8160 NULL);
8161 if (!btrfs_delalloc_work_cachep)
8162 goto fail;
8163
Chris Mason39279cc2007-06-12 06:35:45 -04008164 return 0;
8165fail:
8166 btrfs_destroy_cachep();
8167 return -ENOMEM;
8168}
8169
8170static int btrfs_getattr(struct vfsmount *mnt,
8171 struct dentry *dentry, struct kstat *stat)
8172{
Miao Xiedf0af1a2013-01-29 10:11:59 +00008173 u64 delalloc_bytes;
Chris Mason39279cc2007-06-12 06:35:45 -04008174 struct inode *inode = dentry->d_inode;
David Sterbafadc0d82011-11-20 07:33:38 -05008175 u32 blocksize = inode->i_sb->s_blocksize;
8176
Chris Mason39279cc2007-06-12 06:35:45 -04008177 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04008178 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05008179 stat->blksize = PAGE_CACHE_SIZE;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008180
8181 spin_lock(&BTRFS_I(inode)->lock);
8182 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
8183 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05008184 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00008185 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04008186 return 0;
8187}
8188
Chris Masond3977122009-01-05 21:25:51 -05008189static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
8190 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04008191{
8192 struct btrfs_trans_handle *trans;
8193 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008194 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04008195 struct inode *new_inode = new_dentry->d_inode;
8196 struct inode *old_inode = old_dentry->d_inode;
8197 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008198 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008199 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04008200 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08008201 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008202
Li Zefan33345d012011-04-20 10:31:50 +08008203 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04008204 return -EPERM;
8205
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008206 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08008207 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05008208 return -EXDEV;
8209
Li Zefan33345d012011-04-20 10:31:50 +08008210 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
8211 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008212 return -ENOTEMPTY;
8213
Chris Mason39279cc2007-06-12 06:35:45 -04008214 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008215 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04008216 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05008217
8218
8219 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04008220 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05008221 new_dentry->d_name.name,
8222 new_dentry->d_name.len);
8223
8224 if (ret) {
8225 if (ret == -EEXIST) {
8226 /* we shouldn't get
8227 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05308228 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05008229 return ret;
8230 }
8231 } else {
8232 /* maybe -EOVERFLOW */
8233 return ret;
8234 }
8235 }
8236 ret = 0;
8237
Chris Mason5a3f23d2009-03-31 13:27:11 -04008238 /*
8239 * we're using rename to replace one file with another.
8240 * and the replacement file is large. Start IO on it now so
8241 * we don't add too much work to the end of the transaction
8242 */
Bartlomiej Zolnierkiewicz4baf8c92009-08-07 13:47:08 -04008243 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
Chris Mason5a3f23d2009-03-31 13:27:11 -04008244 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
8245 filemap_flush(old_inode->i_mapping);
8246
Yan, Zheng76dda932009-09-21 16:00:26 -04008247 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08008248 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008249 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008250 /*
8251 * We want to reserve the absolute worst case amount of items. So if
8252 * both inodes are subvols and we need to unlink them then that would
8253 * require 4 item modifications, but if they are both normal inodes it
8254 * would require 5 item modifications, so we'll assume their normal
8255 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
8256 * should cover the worst case number of items we'll modify.
8257 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04008258 trans = btrfs_start_transaction(root, 11);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008259 if (IS_ERR(trans)) {
8260 ret = PTR_ERR(trans);
8261 goto out_notrans;
8262 }
Chris Mason5f39d392007-10-15 16:14:19 -04008263
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008264 if (dest != root)
8265 btrfs_record_root_in_trans(trans, dest);
8266
Yan, Zhenga5719522009-09-24 09:17:31 -04008267 ret = btrfs_set_inode_index(new_dir, &index);
8268 if (ret)
8269 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008270
Miao Xie67de1172013-12-26 13:07:06 +08008271 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08008272 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008273 /* force full log commit if subvolume involved. */
8274 root->fs_info->last_trans_log_full_commit = trans->transid;
8275 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04008276 ret = btrfs_insert_inode_ref(trans, dest,
8277 new_dentry->d_name.name,
8278 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08008279 old_ino,
8280 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04008281 if (ret)
8282 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008283 /*
8284 * this is an ugly little race, but the rename is required
8285 * to make sure that if we crash, the inode is either at the
8286 * old name or the new one. pinning the log transaction lets
8287 * us make sure we don't allow a log commit to come in after
8288 * we unlink the name but before we add the new name back in.
8289 */
8290 btrfs_pin_log_trans(root);
8291 }
Chris Mason12fcfd22009-03-24 10:24:20 -04008292 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04008293 * make sure the inode gets flushed if it is replacing
8294 * something.
8295 */
Li Zefan33345d012011-04-20 10:31:50 +08008296 if (new_inode && new_inode->i_size && S_ISREG(old_inode->i_mode))
Chris Mason5a3f23d2009-03-31 13:27:11 -04008297 btrfs_add_ordered_operation(trans, root, old_inode);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008298
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008299 inode_inc_iversion(old_dir);
8300 inode_inc_iversion(new_dir);
8301 inode_inc_iversion(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008302 old_dir->i_ctime = old_dir->i_mtime = ctime;
8303 new_dir->i_ctime = new_dir->i_mtime = ctime;
8304 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04008305
Chris Mason12fcfd22009-03-24 10:24:20 -04008306 if (old_dentry->d_parent != new_dentry->d_parent)
8307 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
8308
Li Zefan33345d012011-04-20 10:31:50 +08008309 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008310 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
8311 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
8312 old_dentry->d_name.name,
8313 old_dentry->d_name.len);
8314 } else {
Al Viro92986792011-03-04 17:14:37 +00008315 ret = __btrfs_unlink_inode(trans, root, old_dir,
8316 old_dentry->d_inode,
8317 old_dentry->d_name.name,
8318 old_dentry->d_name.len);
8319 if (!ret)
8320 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008321 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008322 if (ret) {
8323 btrfs_abort_transaction(trans, root, ret);
8324 goto out_fail;
8325 }
Chris Mason39279cc2007-06-12 06:35:45 -04008326
8327 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008328 inode_inc_iversion(new_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008329 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08008330 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008331 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
8332 root_objectid = BTRFS_I(new_inode)->location.objectid;
8333 ret = btrfs_unlink_subvol(trans, dest, new_dir,
8334 root_objectid,
8335 new_dentry->d_name.name,
8336 new_dentry->d_name.len);
8337 BUG_ON(new_inode->i_nlink == 0);
8338 } else {
8339 ret = btrfs_unlink_inode(trans, dest, new_dir,
8340 new_dentry->d_inode,
8341 new_dentry->d_name.name,
8342 new_dentry->d_name.len);
8343 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04008344 if (!ret && new_inode->i_nlink == 0)
Chris Masone02119d2008-09-05 16:13:11 -04008345 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008346 if (ret) {
8347 btrfs_abort_transaction(trans, root, ret);
8348 goto out_fail;
8349 }
Chris Mason39279cc2007-06-12 06:35:45 -04008350 }
Josef Bacikaec74772008-07-24 12:12:38 -04008351
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008352 ret = btrfs_add_link(trans, new_dir, old_inode,
8353 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04008354 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008355 if (ret) {
8356 btrfs_abort_transaction(trans, root, ret);
8357 goto out_fail;
8358 }
Chris Mason39279cc2007-06-12 06:35:45 -04008359
Miao Xie67de1172013-12-26 13:07:06 +08008360 if (old_inode->i_nlink == 1)
8361 BTRFS_I(old_inode)->dir_index = index;
8362
Li Zefan33345d012011-04-20 10:31:50 +08008363 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04008364 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00008365 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008366 btrfs_end_log_trans(root);
8367 }
Chris Mason39279cc2007-06-12 06:35:45 -04008368out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008369 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008370out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08008371 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008372 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008373
Chris Mason39279cc2007-06-12 06:35:45 -04008374 return ret;
8375}
8376
Miao Xie8ccf6f192012-10-25 09:28:04 +00008377static void btrfs_run_delalloc_work(struct btrfs_work *work)
8378{
8379 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04008380 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008381
8382 delalloc_work = container_of(work, struct btrfs_delalloc_work,
8383 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04008384 inode = delalloc_work->inode;
8385 if (delalloc_work->wait) {
8386 btrfs_wait_ordered_range(inode, 0, (u64)-1);
8387 } else {
8388 filemap_flush(inode->i_mapping);
8389 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8390 &BTRFS_I(inode)->runtime_flags))
8391 filemap_flush(inode->i_mapping);
8392 }
Miao Xie8ccf6f192012-10-25 09:28:04 +00008393
8394 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -04008395 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008396 else
Josef Bacik9f23e282013-10-28 15:03:41 -04008397 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008398 complete(&delalloc_work->completion);
8399}
8400
8401struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
8402 int wait, int delay_iput)
8403{
8404 struct btrfs_delalloc_work *work;
8405
8406 work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
8407 if (!work)
8408 return NULL;
8409
8410 init_completion(&work->completion);
8411 INIT_LIST_HEAD(&work->list);
8412 work->inode = inode;
8413 work->wait = wait;
8414 work->delay_iput = delay_iput;
8415 work->work.func = btrfs_run_delalloc_work;
8416
8417 return work;
8418}
8419
8420void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
8421{
8422 wait_for_completion(&work->completion);
8423 kmem_cache_free(btrfs_delalloc_work_cachep, work);
8424}
8425
Chris Masond352ac62008-09-29 15:18:18 -04008426/*
8427 * some fairly slow code that needs optimization. This walks the list
8428 * of all the inodes with pending delalloc and forces them to disk.
8429 */
Miao Xieeb73c1b2013-05-15 07:48:22 +00008430static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
Chris Masonea8c2812008-08-04 23:17:27 -04008431{
Chris Masonea8c2812008-08-04 23:17:27 -04008432 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008433 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008434 struct btrfs_delalloc_work *work, *next;
8435 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00008436 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008437 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04008438
Miao Xie8ccf6f192012-10-25 09:28:04 +00008439 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008440 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00008441
Miao Xieeb73c1b2013-05-15 07:48:22 +00008442 spin_lock(&root->delalloc_lock);
8443 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008444 while (!list_empty(&splice)) {
8445 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04008446 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008447
Miao Xieeb73c1b2013-05-15 07:48:22 +00008448 list_move_tail(&binode->delalloc_inodes,
8449 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008450 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00008451 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00008452 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008453 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008454 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008455 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008456
8457 work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
8458 if (unlikely(!work)) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -04008459 if (delay_iput)
8460 btrfs_add_delayed_iput(inode);
8461 else
8462 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008463 ret = -ENOMEM;
8464 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008465 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008466 list_add_tail(&work->list, &works);
8467 btrfs_queue_worker(&root->fs_info->flush_workers,
8468 &work->work);
8469
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008470 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00008471 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04008472 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008473 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04008474
Miao Xie1eafa6c2013-01-22 10:49:00 +00008475 list_for_each_entry_safe(work, next, &works, list) {
8476 list_del_init(&work->list);
8477 btrfs_wait_and_free_delalloc_work(work);
8478 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008479 return 0;
8480out:
8481 list_for_each_entry_safe(work, next, &works, list) {
8482 list_del_init(&work->list);
8483 btrfs_wait_and_free_delalloc_work(work);
8484 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008485
Miao Xieeb73c1b2013-05-15 07:48:22 +00008486 if (!list_empty_careful(&splice)) {
8487 spin_lock(&root->delalloc_lock);
8488 list_splice_tail(&splice, &root->delalloc_inodes);
8489 spin_unlock(&root->delalloc_lock);
8490 }
8491 return ret;
8492}
8493
8494int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
8495{
8496 int ret;
8497
Wang Shilong2c21b4d2014-01-14 19:42:20 +08008498 if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00008499 return -EROFS;
8500
8501 ret = __start_delalloc_inodes(root, delay_iput);
8502 /*
8503 * the filemap_flush will queue IO into the worker threads, but
Chris Mason8c8bee12008-09-29 11:19:10 -04008504 * we have to make sure the IO is actually started and that
8505 * ordered extents get created before we return
8506 */
8507 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05008508 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05008509 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04008510 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05008511 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
8512 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04008513 }
8514 atomic_dec(&root->fs_info->async_submit_draining);
Miao Xieeb73c1b2013-05-15 07:48:22 +00008515 return ret;
8516}
8517
Miao Xie91aef862013-11-04 23:13:26 +08008518int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput)
Miao Xieeb73c1b2013-05-15 07:48:22 +00008519{
8520 struct btrfs_root *root;
8521 struct list_head splice;
8522 int ret;
8523
Wang Shilong2c21b4d2014-01-14 19:42:20 +08008524 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00008525 return -EROFS;
8526
8527 INIT_LIST_HEAD(&splice);
8528
8529 spin_lock(&fs_info->delalloc_root_lock);
8530 list_splice_init(&fs_info->delalloc_roots, &splice);
8531 while (!list_empty(&splice)) {
8532 root = list_first_entry(&splice, struct btrfs_root,
8533 delalloc_root);
8534 root = btrfs_grab_fs_root(root);
8535 BUG_ON(!root);
8536 list_move_tail(&root->delalloc_root,
8537 &fs_info->delalloc_roots);
8538 spin_unlock(&fs_info->delalloc_root_lock);
8539
8540 ret = __start_delalloc_inodes(root, delay_iput);
8541 btrfs_put_fs_root(root);
8542 if (ret)
8543 goto out;
8544
8545 spin_lock(&fs_info->delalloc_root_lock);
8546 }
8547 spin_unlock(&fs_info->delalloc_root_lock);
8548
8549 atomic_inc(&fs_info->async_submit_draining);
8550 while (atomic_read(&fs_info->nr_async_submits) ||
8551 atomic_read(&fs_info->async_delalloc_pages)) {
8552 wait_event(fs_info->async_submit_wait,
8553 (atomic_read(&fs_info->nr_async_submits) == 0 &&
8554 atomic_read(&fs_info->async_delalloc_pages) == 0));
8555 }
8556 atomic_dec(&fs_info->async_submit_draining);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008557 return 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008558out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00008559 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00008560 spin_lock(&fs_info->delalloc_root_lock);
8561 list_splice_tail(&splice, &fs_info->delalloc_roots);
8562 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008563 }
Miao Xie8ccf6f192012-10-25 09:28:04 +00008564 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04008565}
8566
Chris Mason39279cc2007-06-12 06:35:45 -04008567static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
8568 const char *symname)
8569{
8570 struct btrfs_trans_handle *trans;
8571 struct btrfs_root *root = BTRFS_I(dir)->root;
8572 struct btrfs_path *path;
8573 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05008574 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04008575 int err;
8576 int drop_inode = 0;
8577 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05308578 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008579 int name_len;
8580 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04008581 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04008582 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04008583 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04008584
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01008585 name_len = strlen(symname);
Chris Mason39279cc2007-06-12 06:35:45 -04008586 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
8587 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05008588
Josef Bacik9ed74f22009-09-11 16:12:44 -04008589 /*
8590 * 2 items for inode item and ref
8591 * 2 items for dir items
8592 * 1 item for xattr if selinux is on
8593 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04008594 trans = btrfs_start_transaction(root, 5);
8595 if (IS_ERR(trans))
8596 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05008597
Li Zefan581bb052011-04-20 10:06:11 +08008598 err = btrfs_find_free_ino(root, &objectid);
8599 if (err)
8600 goto out_unlock;
8601
Josef Bacikaec74772008-07-24 12:12:38 -04008602 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08008603 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04008604 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008605 if (IS_ERR(inode)) {
8606 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008607 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008608 }
Chris Mason39279cc2007-06-12 06:35:45 -04008609
Eric Paris2a7dba32011-02-01 11:05:39 -05008610 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04008611 if (err) {
8612 drop_inode = 1;
8613 goto out_unlock;
8614 }
8615
Casey Schauflerad19db72011-12-15 10:09:07 -05008616 /*
8617 * If the active LSM wants to access the inode during
8618 * d_instantiate it needs these. Smack checks to see
8619 * if the filesystem supports xattrs by looking at the
8620 * ops vector.
8621 */
8622 inode->i_fop = &btrfs_file_operations;
8623 inode->i_op = &btrfs_file_inode_operations;
8624
Josef Bacika1b075d2010-11-19 20:36:11 +00008625 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04008626 if (err)
8627 drop_inode = 1;
8628 else {
8629 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04008630 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05008631 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04008632 }
Chris Mason39279cc2007-06-12 06:35:45 -04008633 if (drop_inode)
8634 goto out_unlock;
8635
8636 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07008637 if (!path) {
8638 err = -ENOMEM;
8639 drop_inode = 1;
8640 goto out_unlock;
8641 }
Li Zefan33345d012011-04-20 10:31:50 +08008642 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008643 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008644 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
8645 datasize = btrfs_file_extent_calc_inline_size(name_len);
8646 err = btrfs_insert_empty_item(trans, root, path, &key,
8647 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04008648 if (err) {
8649 drop_inode = 1;
Julia Lawallb0839162011-05-14 07:10:51 +00008650 btrfs_free_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04008651 goto out_unlock;
8652 }
Chris Mason5f39d392007-10-15 16:14:19 -04008653 leaf = path->nodes[0];
8654 ei = btrfs_item_ptr(leaf, path->slots[0],
8655 struct btrfs_file_extent_item);
8656 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
8657 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04008658 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04008659 btrfs_set_file_extent_encryption(leaf, ei, 0);
8660 btrfs_set_file_extent_compression(leaf, ei, 0);
8661 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
8662 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
8663
Chris Mason39279cc2007-06-12 06:35:45 -04008664 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04008665 write_extent_buffer(leaf, symname, ptr, name_len);
8666 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04008667 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04008668
Chris Mason39279cc2007-06-12 06:35:45 -04008669 inode->i_op = &btrfs_symlink_inode_operations;
8670 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04008671 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04008672 inode_set_bytes(inode, name_len);
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01008673 btrfs_i_size_write(inode, name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -04008674 err = btrfs_update_inode(trans, root, inode);
8675 if (err)
8676 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04008677
8678out_unlock:
Al Viro08c422c2011-12-23 07:58:13 -05008679 if (!err)
8680 d_instantiate(dentry, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008681 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04008682 if (drop_inode) {
8683 inode_dec_link_count(inode);
8684 iput(inode);
8685 }
Liu Bob53d3f52012-11-14 14:34:34 +00008686 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04008687 return err;
8688}
Chris Mason16432982008-04-10 10:23:21 -04008689
Josef Bacik0af3d002010-06-21 14:48:16 -04008690static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
8691 u64 start, u64 num_bytes, u64 min_size,
8692 loff_t actual_len, u64 *alloc_hint,
8693 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04008694{
Josef Bacik5dc562c2012-08-17 13:14:17 -04008695 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
8696 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04008697 struct btrfs_root *root = BTRFS_I(inode)->root;
8698 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04008699 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00008700 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05008701 u64 cur_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04008702 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04008703 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04008704
Josef Bacik0af3d002010-06-21 14:48:16 -04008705 if (trans)
8706 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04008707 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008708 if (own_trans) {
8709 trans = btrfs_start_transaction(root, 3);
8710 if (IS_ERR(trans)) {
8711 ret = PTR_ERR(trans);
8712 break;
8713 }
Yan Zhengd899e052008-10-30 14:25:28 -04008714 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00008715
Chris Mason154ea282013-03-05 11:11:26 -05008716 cur_bytes = min(num_bytes, 256ULL * 1024 * 1024);
8717 cur_bytes = max(cur_bytes, min_size);
Josef Bacik00361582013-08-14 14:02:47 -04008718 ret = btrfs_reserve_extent(root, cur_bytes, min_size, 0,
8719 *alloc_hint, &ins, 1);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008720 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008721 if (own_trans)
8722 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008723 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00008724 }
8725
Yan Zhengd899e052008-10-30 14:25:28 -04008726 ret = insert_reserved_file_extent(trans, inode,
8727 cur_offset, ins.objectid,
8728 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00008729 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04008730 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008731 if (ret) {
Josef Bacik857cc2f2013-10-07 15:21:08 -04008732 btrfs_free_reserved_extent(root, ins.objectid,
8733 ins.offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008734 btrfs_abort_transaction(trans, root, ret);
8735 if (own_trans)
8736 btrfs_end_transaction(trans, root);
8737 break;
8738 }
Chris Masona1ed8352009-09-11 12:27:37 -04008739 btrfs_drop_extent_cache(inode, cur_offset,
8740 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008741
Josef Bacik5dc562c2012-08-17 13:14:17 -04008742 em = alloc_extent_map();
8743 if (!em) {
8744 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
8745 &BTRFS_I(inode)->runtime_flags);
8746 goto next;
8747 }
8748
8749 em->start = cur_offset;
8750 em->orig_start = cur_offset;
8751 em->len = ins.offset;
8752 em->block_start = ins.objectid;
8753 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05008754 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04008755 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008756 em->bdev = root->fs_info->fs_devices->latest_bdev;
8757 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
8758 em->generation = trans->transid;
8759
8760 while (1) {
8761 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04008762 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04008763 write_unlock(&em_tree->lock);
8764 if (ret != -EEXIST)
8765 break;
8766 btrfs_drop_extent_cache(inode, cur_offset,
8767 cur_offset + ins.offset - 1,
8768 0);
8769 }
8770 free_extent_map(em);
8771next:
Yan Zhengd899e052008-10-30 14:25:28 -04008772 num_bytes -= ins.offset;
8773 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04008774 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00008775
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008776 inode_inc_iversion(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04008777 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02008778 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04008779 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04008780 (actual_len > inode->i_size) &&
8781 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00008782 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00008783 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00008784 else
Josef Bacik55a61d12010-11-22 18:50:32 +00008785 i_size = cur_offset;
8786 i_size_write(inode, i_size);
8787 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008788 }
8789
Yan Zhengd899e052008-10-30 14:25:28 -04008790 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008791
8792 if (ret) {
8793 btrfs_abort_transaction(trans, root, ret);
8794 if (own_trans)
8795 btrfs_end_transaction(trans, root);
8796 break;
8797 }
Yan Zhengd899e052008-10-30 14:25:28 -04008798
Josef Bacik0af3d002010-06-21 14:48:16 -04008799 if (own_trans)
8800 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008801 }
Yan Zhengd899e052008-10-30 14:25:28 -04008802 return ret;
8803}
8804
Josef Bacik0af3d002010-06-21 14:48:16 -04008805int btrfs_prealloc_file_range(struct inode *inode, int mode,
8806 u64 start, u64 num_bytes, u64 min_size,
8807 loff_t actual_len, u64 *alloc_hint)
8808{
8809 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
8810 min_size, actual_len, alloc_hint,
8811 NULL);
8812}
8813
8814int btrfs_prealloc_file_range_trans(struct inode *inode,
8815 struct btrfs_trans_handle *trans, int mode,
8816 u64 start, u64 num_bytes, u64 min_size,
8817 loff_t actual_len, u64 *alloc_hint)
8818{
8819 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
8820 min_size, actual_len, alloc_hint, trans);
8821}
8822
Chris Masone6dcd2d2008-07-17 12:53:50 -04008823static int btrfs_set_page_dirty(struct page *page)
8824{
Chris Masone6dcd2d2008-07-17 12:53:50 -04008825 return __set_page_dirty_nobuffers(page);
8826}
8827
Al Viro10556cb2011-06-20 19:28:19 -04008828static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05008829{
Li Zefanb83cc962010-12-20 16:04:08 +08008830 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00008831 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08008832
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00008833 if (mask & MAY_WRITE &&
8834 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
8835 if (btrfs_root_readonly(root))
8836 return -EROFS;
8837 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
8838 return -EACCES;
8839 }
Al Viro2830ba72011-06-20 19:16:29 -04008840 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05008841}
Chris Mason39279cc2007-06-12 06:35:45 -04008842
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008843static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05008844 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04008845 .lookup = btrfs_lookup,
8846 .create = btrfs_create,
8847 .unlink = btrfs_unlink,
8848 .link = btrfs_link,
8849 .mkdir = btrfs_mkdir,
8850 .rmdir = btrfs_rmdir,
8851 .rename = btrfs_rename,
8852 .symlink = btrfs_symlink,
8853 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04008854 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008855 .setxattr = btrfs_setxattr,
8856 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05008857 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008858 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05008859 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008860 .get_acl = btrfs_get_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -07008861 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008862};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008863static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008864 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05008865 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008866 .get_acl = btrfs_get_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -07008867 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008868};
Yan, Zheng76dda932009-09-21 16:00:26 -04008869
Alexey Dobriyan828c0952009-10-01 15:43:56 -07008870static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008871 .llseek = generic_file_llseek,
8872 .read = generic_read_dir,
Al Viro9cdda8d2013-05-22 16:48:09 -04008873 .iterate = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04008874 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04008875#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04008876 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04008877#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04008878 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04008879 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04008880};
8881
Chris Masond1310b22008-01-24 16:13:08 -05008882static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04008883 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05008884 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04008885 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04008886 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008887 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04008888 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05008889 .set_bit_hook = btrfs_set_bit_hook,
8890 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04008891 .merge_extent_hook = btrfs_merge_extent_hook,
8892 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04008893};
8894
Chris Mason35054392009-01-21 13:11:13 -05008895/*
8896 * btrfs doesn't support the bmap operation because swapfiles
8897 * use bmap to make a mapping of extents in the file. They assume
8898 * these extents won't change over the life of the file and they
8899 * use the bmap result to do IO directly to the drive.
8900 *
8901 * the btrfs bmap call would return logical addresses that aren't
8902 * suitable for IO and they also will change frequently as COW
8903 * operations happen. So, swapfile + btrfs == corruption.
8904 *
8905 * For now we're avoiding this by dropping bmap.
8906 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008907static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008908 .readpage = btrfs_readpage,
8909 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04008910 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05008911 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04008912 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04008913 .invalidatepage = btrfs_invalidatepage,
8914 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008915 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02008916 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04008917};
8918
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008919static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008920 .readpage = btrfs_readpage,
8921 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04008922 .invalidatepage = btrfs_invalidatepage,
8923 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04008924};
8925
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008926static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008927 .getattr = btrfs_getattr,
8928 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008929 .setxattr = btrfs_setxattr,
8930 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05008931 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008932 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05008933 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008934 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008935 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008936 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008937};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008938static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04008939 .getattr = btrfs_getattr,
8940 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008941 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008942 .setxattr = btrfs_setxattr,
8943 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04008944 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008945 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008946 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008947 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -04008948};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008949static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008950 .readlink = generic_readlink,
8951 .follow_link = page_follow_link_light,
8952 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00008953 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -05008954 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008955 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05008956 .setxattr = btrfs_setxattr,
8957 .getxattr = btrfs_getxattr,
8958 .listxattr = btrfs_listxattr,
8959 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008960 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008961 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008962};
Yan, Zheng76dda932009-09-21 16:00:26 -04008963
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04008964const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04008965 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -04008966 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -04008967};