blob: ce3f7304660573a2467627cdad3a75950c1982c1 [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;
Qu Wenruoafe3d242014-02-28 10:46:07 +0800327 struct btrfs_work_struct work;
Chris Mason771ed682008-11-06 22:02:51 -0500328};
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 */
Qu Wenruoafe3d242014-02-28 10:46:07 +08001003static noinline void async_cow_start(struct btrfs_work_struct *work)
Chris Mason771ed682008-11-06 22:02:51 -05001004{
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 */
Qu Wenruoafe3d242014-02-28 10:46:07 +08001021static noinline void async_cow_submit(struct btrfs_work_struct *work)
Chris Mason771ed682008-11-06 22:02:51 -05001022{
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
Qu Wenruoafe3d242014-02-28 10:46:07 +08001042static noinline void async_cow_free(struct btrfs_work_struct *work)
Chris Mason771ed682008-11-06 22:02:51 -05001043{
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
Qu Wenruoafe3d242014-02-28 10:46:07 +08001079 btrfs_init_work(&async_cow->work, async_cow_start,
1080 async_cow_submit, async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001081
Chris Mason771ed682008-11-06 22:02:51 -05001082 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
1083 PAGE_CACHE_SHIFT;
1084 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
1085
Qu Wenruoafe3d242014-02-28 10:46:07 +08001086 btrfs_queue_work(root->fs_info->delalloc_workers,
1087 &async_cow->work);
Chris Mason771ed682008-11-06 22:02:51 -05001088
1089 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
1090 wait_event(root->fs_info->async_submit_wait,
1091 (atomic_read(&root->fs_info->async_delalloc_pages) <
1092 limit));
1093 }
1094
Chris Masond3977122009-01-05 21:25:51 -05001095 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -05001096 atomic_read(&root->fs_info->async_delalloc_pages)) {
1097 wait_event(root->fs_info->async_submit_wait,
1098 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1099 0));
1100 }
1101
1102 *nr_written += nr_pages;
1103 start = cur_end + 1;
1104 }
1105 *page_started = 1;
1106 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001107}
1108
Chris Masond3977122009-01-05 21:25:51 -05001109static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001110 u64 bytenr, u64 num_bytes)
1111{
1112 int ret;
1113 struct btrfs_ordered_sum *sums;
1114 LIST_HEAD(list);
1115
Yan Zheng07d400a2009-01-06 11:42:00 -05001116 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001117 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001118 if (ret == 0 && list_empty(&list))
1119 return 0;
1120
1121 while (!list_empty(&list)) {
1122 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1123 list_del(&sums->list);
1124 kfree(sums);
1125 }
1126 return 1;
1127}
1128
Chris Masond352ac62008-09-29 15:18:18 -04001129/*
1130 * when nowcow writeback call back. This checks for snapshots or COW copies
1131 * of the extents that exist in the file, and COWs the file as required.
1132 *
1133 * If no cow copies or snapshots exist, we write directly to the existing
1134 * blocks on disk
1135 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001136static noinline int run_delalloc_nocow(struct inode *inode,
1137 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001138 u64 start, u64 end, int *page_started, int force,
1139 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001140{
Chris Masonbe20aa92007-12-17 20:14:01 -05001141 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001142 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001143 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001144 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001145 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001146 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001147 u64 cow_start;
1148 u64 cur_offset;
1149 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001150 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001151 u64 disk_bytenr;
1152 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001153 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001154 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001155 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001156 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001157 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001158 int nocow;
1159 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001160 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001161 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001162
1163 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001164 if (!path) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001165 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1166 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001167 EXTENT_DO_ACCOUNTING |
1168 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001169 PAGE_CLEAR_DIRTY |
1170 PAGE_SET_WRITEBACK |
1171 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001172 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001173 }
Li Zefan82d59022011-04-20 10:33:24 +08001174
Liu Bo83eea1f2012-07-10 05:28:39 -06001175 nolock = btrfs_is_free_space_inode(inode);
Li Zefan82d59022011-04-20 10:33:24 +08001176
1177 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001178 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001179 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001180 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001181
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001182 if (IS_ERR(trans)) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001183 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1184 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001185 EXTENT_DO_ACCOUNTING |
1186 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001187 PAGE_CLEAR_DIRTY |
1188 PAGE_SET_WRITEBACK |
1189 PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001190 btrfs_free_path(path);
1191 return PTR_ERR(trans);
1192 }
1193
Josef Bacik74b21072011-04-13 12:02:53 -04001194 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001195
Yan Zheng80ff3852008-10-30 14:20:02 -04001196 cow_start = (u64)-1;
1197 cur_offset = start;
1198 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001199 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001200 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001201 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001202 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001203 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1204 leaf = path->nodes[0];
1205 btrfs_item_key_to_cpu(leaf, &found_key,
1206 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001207 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001208 found_key.type == BTRFS_EXTENT_DATA_KEY)
1209 path->slots[0]--;
1210 }
1211 check_prev = 0;
1212next_slot:
1213 leaf = path->nodes[0];
1214 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1215 ret = btrfs_next_leaf(root, path);
Josef Bacikd788a342013-10-25 16:55:08 -04001216 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001217 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001218 if (ret > 0)
1219 break;
1220 leaf = path->nodes[0];
1221 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001222
Yan Zheng80ff3852008-10-30 14:20:02 -04001223 nocow = 0;
1224 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001225 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001226 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001227
Li Zefan33345d012011-04-20 10:31:50 +08001228 if (found_key.objectid > ino ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001229 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1230 found_key.offset > end)
1231 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001232
Yan Zheng80ff3852008-10-30 14:20:02 -04001233 if (found_key.offset > cur_offset) {
1234 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001235 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001236 goto out_check;
1237 }
Chris Masonc31f8832008-01-08 15:46:31 -05001238
Yan Zheng80ff3852008-10-30 14:20:02 -04001239 fi = btrfs_item_ptr(leaf, path->slots[0],
1240 struct btrfs_file_extent_item);
1241 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001242
Josef Bacikcc95bef2013-04-04 14:31:27 -04001243 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001244 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1245 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001246 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001247 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001248 extent_end = found_key.offset +
1249 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001250 disk_num_bytes =
1251 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001252 if (extent_end <= start) {
1253 path->slots[0]++;
1254 goto next_slot;
1255 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001256 if (disk_bytenr == 0)
1257 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001258 if (btrfs_file_extent_compression(leaf, fi) ||
1259 btrfs_file_extent_encryption(leaf, fi) ||
1260 btrfs_file_extent_other_encoding(leaf, fi))
1261 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001262 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1263 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001264 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001265 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001266 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001267 found_key.offset -
1268 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001269 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001270 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001271 disk_bytenr += cur_offset - found_key.offset;
1272 num_bytes = min(end + 1, extent_end) - cur_offset;
1273 /*
1274 * force cow if csum exists in the range.
1275 * this ensure that csum for a given extent are
1276 * either valid or do not exist.
1277 */
1278 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1279 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001280 nocow = 1;
1281 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1282 extent_end = found_key.offset +
Chris Mason514ac8a2014-01-03 21:07:00 -08001283 btrfs_file_extent_inline_len(leaf,
1284 path->slots[0], fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001285 extent_end = ALIGN(extent_end, root->sectorsize);
1286 } else {
1287 BUG_ON(1);
1288 }
1289out_check:
1290 if (extent_end <= start) {
1291 path->slots[0]++;
1292 goto next_slot;
1293 }
1294 if (!nocow) {
1295 if (cow_start == (u64)-1)
1296 cow_start = cur_offset;
1297 cur_offset = extent_end;
1298 if (cur_offset > end)
1299 break;
1300 path->slots[0]++;
1301 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001302 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001303
David Sterbab3b4aa72011-04-21 01:20:15 +02001304 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001305 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001306 ret = cow_file_range(inode, locked_page,
1307 cow_start, found_key.offset - 1,
1308 page_started, nr_written, 1);
Josef Bacikd788a342013-10-25 16:55:08 -04001309 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001310 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001311 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001312 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001313
Yan Zhengd899e052008-10-30 14:25:28 -04001314 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1315 struct extent_map *em;
1316 struct extent_map_tree *em_tree;
1317 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001318 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001319 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001320 em->start = cur_offset;
Josef Bacik70c8a912012-10-11 16:54:30 -04001321 em->orig_start = found_key.offset - extent_offset;
Yan Zhengd899e052008-10-30 14:25:28 -04001322 em->len = num_bytes;
1323 em->block_len = num_bytes;
1324 em->block_start = disk_bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05001325 em->orig_block_len = disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001326 em->ram_bytes = ram_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04001327 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001328 em->mod_start = em->start;
1329 em->mod_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04001330 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacikb11e2342012-12-03 10:58:15 -05001331 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001332 em->generation = -1;
Yan Zhengd899e052008-10-30 14:25:28 -04001333 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001334 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001335 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001336 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001337 if (ret != -EEXIST) {
1338 free_extent_map(em);
1339 break;
1340 }
1341 btrfs_drop_extent_cache(inode, em->start,
1342 em->start + em->len - 1, 0);
1343 }
1344 type = BTRFS_ORDERED_PREALLOC;
1345 } else {
1346 type = BTRFS_ORDERED_NOCOW;
1347 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001348
1349 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001350 num_bytes, num_bytes, type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001351 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001352
Yan, Zhengefa56462010-05-16 10:49:59 -04001353 if (root->root_key.objectid ==
1354 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1355 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1356 num_bytes);
Josef Bacikd788a342013-10-25 16:55:08 -04001357 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001358 goto error;
Yan, Zhengefa56462010-05-16 10:49:59 -04001359 }
1360
Josef Bacikc2790a22013-07-29 11:20:47 -04001361 extent_clear_unlock_delalloc(inode, cur_offset,
1362 cur_offset + num_bytes - 1,
1363 locked_page, EXTENT_LOCKED |
1364 EXTENT_DELALLOC, PAGE_UNLOCK |
1365 PAGE_SET_PRIVATE2);
Yan Zheng80ff3852008-10-30 14:20:02 -04001366 cur_offset = extent_end;
1367 if (cur_offset > end)
1368 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001369 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001370 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001371
Josef Bacik17ca04a2012-05-31 15:58:55 -04001372 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001373 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001374 cur_offset = end;
1375 }
1376
Yan Zheng80ff3852008-10-30 14:20:02 -04001377 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001378 ret = cow_file_range(inode, locked_page, cow_start, end,
1379 page_started, nr_written, 1);
Josef Bacikd788a342013-10-25 16:55:08 -04001380 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001381 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001382 }
1383
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001384error:
Miao Xiea698d0752012-09-20 01:51:59 -06001385 err = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001386 if (!ret)
1387 ret = err;
1388
Josef Bacik17ca04a2012-05-31 15:58:55 -04001389 if (ret && cur_offset < end)
Josef Bacikc2790a22013-07-29 11:20:47 -04001390 extent_clear_unlock_delalloc(inode, cur_offset, end,
1391 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001392 EXTENT_DELALLOC | EXTENT_DEFRAG |
1393 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1394 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001395 PAGE_SET_WRITEBACK |
1396 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001397 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001398 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001399}
1400
Chris Masond352ac62008-09-29 15:18:18 -04001401/*
1402 * extent_io.c call back to do delayed allocation processing
1403 */
Chris Masonc8b97812008-10-29 14:49:59 -04001404static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001405 u64 start, u64 end, int *page_started,
1406 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001407{
Chris Masonbe20aa92007-12-17 20:14:01 -05001408 int ret;
Chris Mason7f366cf2009-03-12 20:12:45 -04001409 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona2135012008-06-25 16:01:30 -04001410
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001411 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) {
Chris Masonc8b97812008-10-29 14:49:59 -04001412 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001413 page_started, 1, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001414 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001415 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001416 page_started, 0, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001417 } else if (!btrfs_test_opt(root, COMPRESS) &&
1418 !(BTRFS_I(inode)->force_compress) &&
1419 !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS)) {
Chris Mason7f366cf2009-03-12 20:12:45 -04001420 ret = cow_file_range(inode, locked_page, start, end,
1421 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001422 } else {
1423 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1424 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001425 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001426 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001427 }
Chris Masonb888db22007-08-27 16:49:44 -04001428 return ret;
1429}
1430
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001431static void btrfs_split_extent_hook(struct inode *inode,
1432 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001433{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001434 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001435 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001436 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001437
Josef Bacik9e0baf62011-07-15 15:16:44 +00001438 spin_lock(&BTRFS_I(inode)->lock);
1439 BTRFS_I(inode)->outstanding_extents++;
1440 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001441}
1442
1443/*
1444 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1445 * extents so we can keep track of new extents that are just merged onto old
1446 * extents, such as when we are doing sequential writes, so we can properly
1447 * account for the metadata space we'll need.
1448 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001449static void btrfs_merge_extent_hook(struct inode *inode,
1450 struct extent_state *new,
1451 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001452{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001453 /* not delalloc, ignore it */
1454 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001455 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001456
Josef Bacik9e0baf62011-07-15 15:16:44 +00001457 spin_lock(&BTRFS_I(inode)->lock);
1458 BTRFS_I(inode)->outstanding_extents--;
1459 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001460}
1461
Miao Xieeb73c1b2013-05-15 07:48:22 +00001462static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1463 struct inode *inode)
1464{
1465 spin_lock(&root->delalloc_lock);
1466 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1467 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1468 &root->delalloc_inodes);
1469 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1470 &BTRFS_I(inode)->runtime_flags);
1471 root->nr_delalloc_inodes++;
1472 if (root->nr_delalloc_inodes == 1) {
1473 spin_lock(&root->fs_info->delalloc_root_lock);
1474 BUG_ON(!list_empty(&root->delalloc_root));
1475 list_add_tail(&root->delalloc_root,
1476 &root->fs_info->delalloc_roots);
1477 spin_unlock(&root->fs_info->delalloc_root_lock);
1478 }
1479 }
1480 spin_unlock(&root->delalloc_lock);
1481}
1482
1483static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1484 struct inode *inode)
1485{
1486 spin_lock(&root->delalloc_lock);
1487 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1488 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1489 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1490 &BTRFS_I(inode)->runtime_flags);
1491 root->nr_delalloc_inodes--;
1492 if (!root->nr_delalloc_inodes) {
1493 spin_lock(&root->fs_info->delalloc_root_lock);
1494 BUG_ON(list_empty(&root->delalloc_root));
1495 list_del_init(&root->delalloc_root);
1496 spin_unlock(&root->fs_info->delalloc_root_lock);
1497 }
1498 }
1499 spin_unlock(&root->delalloc_lock);
1500}
1501
Chris Masond352ac62008-09-29 15:18:18 -04001502/*
1503 * extent_io.c set_bit_hook, used to track delayed allocation
1504 * bytes in this file, and to maintain the list of inodes that
1505 * have pending delalloc work to be done.
1506 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001507static void btrfs_set_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001508 struct extent_state *state, unsigned long *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001509{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001510
Chris Mason75eff682008-12-15 15:54:40 -05001511 /*
1512 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001513 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001514 * bit, which is only set or cleared with irqs on
1515 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001516 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001517 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001518 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001519 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001520
Josef Bacik9e0baf62011-07-15 15:16:44 +00001521 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001522 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001523 } else {
1524 spin_lock(&BTRFS_I(inode)->lock);
1525 BTRFS_I(inode)->outstanding_extents++;
1526 spin_unlock(&BTRFS_I(inode)->lock);
1527 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001528
Miao Xie963d6782013-01-29 10:10:51 +00001529 __percpu_counter_add(&root->fs_info->delalloc_bytes, len,
1530 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001531 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001532 BTRFS_I(inode)->delalloc_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001533 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001534 &BTRFS_I(inode)->runtime_flags))
1535 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001536 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001537 }
Chris Mason291d6732008-01-29 15:55:23 -05001538}
1539
Chris Masond352ac62008-09-29 15:18:18 -04001540/*
1541 * extent_io.c clear_bit_hook, see set_bit_hook for why
1542 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001543static void btrfs_clear_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001544 struct extent_state *state,
1545 unsigned long *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001546{
Chris Mason75eff682008-12-15 15:54:40 -05001547 /*
1548 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001549 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001550 * bit, which is only set or cleared with irqs on
1551 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001552 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001553 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001554 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001555 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001556
Josef Bacik9e0baf62011-07-15 15:16:44 +00001557 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001558 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001559 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1560 spin_lock(&BTRFS_I(inode)->lock);
1561 BTRFS_I(inode)->outstanding_extents--;
1562 spin_unlock(&BTRFS_I(inode)->lock);
1563 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001564
Josef Bacikb6d08f02013-09-27 14:57:43 -04001565 /*
1566 * We don't reserve metadata space for space cache inodes so we
1567 * don't need to call dellalloc_release_metadata if there is an
1568 * error.
1569 */
1570 if (*bits & EXTENT_DO_ACCOUNTING &&
1571 root != root->fs_info->tree_root)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001572 btrfs_delalloc_release_metadata(inode, len);
1573
Josef Bacik0cb59c92010-07-02 12:14:14 -04001574 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Josef Bacik7ee9e442013-06-21 16:37:03 -04001575 && do_list && !(state->state & EXTENT_NORESERVE))
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001576 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001577
Miao Xie963d6782013-01-29 10:10:51 +00001578 __percpu_counter_add(&root->fs_info->delalloc_bytes, -len,
1579 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001580 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001581 BTRFS_I(inode)->delalloc_bytes -= len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001582 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001583 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001584 &BTRFS_I(inode)->runtime_flags))
1585 btrfs_del_delalloc_inode(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001586 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001587 }
Chris Mason291d6732008-01-29 15:55:23 -05001588}
1589
Chris Masond352ac62008-09-29 15:18:18 -04001590/*
1591 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1592 * we don't create bios that span stripes or chunks
1593 */
David Woodhouse64a16702009-07-15 23:29:37 +01001594int btrfs_merge_bio_hook(int rw, struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001595 size_t size, struct bio *bio,
1596 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001597{
1598 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Chris Masona62b9402008-10-03 16:31:08 -04001599 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001600 u64 length = 0;
1601 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001602 int ret;
1603
Chris Mason771ed682008-11-06 22:02:51 -05001604 if (bio_flags & EXTENT_BIO_COMPRESSED)
1605 return 0;
1606
Chris Masonf2d8d742008-04-21 10:03:05 -04001607 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001608 map_length = length;
David Woodhouse64a16702009-07-15 23:29:37 +01001609 ret = btrfs_map_block(root->fs_info, rw, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001610 &map_length, NULL, 0);
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001611 /* Will always return 0 with map_multi == NULL */
Jeff Mahoney3444a972011-10-03 23:23:13 -04001612 BUG_ON(ret < 0);
Chris Masond3977122009-01-05 21:25:51 -05001613 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001614 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001615 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001616}
1617
Chris Masond352ac62008-09-29 15:18:18 -04001618/*
1619 * in order to insert checksums into the metadata in large chunks,
1620 * we wait until bio submission time. All the pages in the bio are
1621 * checksummed and sums are attached onto the ordered extent record.
1622 *
1623 * At IO completion time the cums attached on the ordered extent record
1624 * are inserted into the btree
1625 */
Chris Masond3977122009-01-05 21:25:51 -05001626static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1627 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001628 unsigned long bio_flags,
1629 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001630{
Chris Mason065631f2008-02-20 12:07:25 -05001631 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001632 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001633
Chris Masond20f7042008-12-08 16:58:54 -05001634 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001635 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001636 return 0;
1637}
Chris Masone0156402008-04-16 11:15:20 -04001638
Chris Mason4a69a412008-11-06 22:03:00 -05001639/*
1640 * in order to insert checksums into the metadata in large chunks,
1641 * we wait until bio submission time. All the pages in the bio are
1642 * checksummed and sums are attached onto the ordered extent record.
1643 *
1644 * At IO completion time the cums attached on the ordered extent record
1645 * are inserted into the btree
1646 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001647static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001648 int mirror_num, unsigned long bio_flags,
1649 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001650{
1651 struct btrfs_root *root = BTRFS_I(inode)->root;
Stefan Behrens61891922012-11-05 18:51:52 +01001652 int ret;
1653
1654 ret = btrfs_map_bio(root, rw, bio, mirror_num, 1);
1655 if (ret)
1656 bio_endio(bio, ret);
1657 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001658}
1659
Chris Masond352ac62008-09-29 15:18:18 -04001660/*
Chris Masoncad321a2008-12-17 14:51:42 -05001661 * extent_io.c submission hook. This does the right thing for csum calculation
1662 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001663 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001664static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001665 int mirror_num, unsigned long bio_flags,
1666 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001667{
1668 struct btrfs_root *root = BTRFS_I(inode)->root;
1669 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001670 int skip_sum;
Jeff Mahoney04173412011-10-03 23:23:12 -04001671 int metadata = 0;
Josef Bacikb812ce22012-11-16 13:56:32 -05001672 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001673
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001674 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001675
Liu Bo83eea1f2012-07-10 05:28:39 -06001676 if (btrfs_is_free_space_inode(inode))
Jeff Mahoney04173412011-10-03 23:23:12 -04001677 metadata = 2;
1678
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001679 if (!(rw & REQ_WRITE)) {
Josef Bacik5fd02042012-05-02 14:00:54 -04001680 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1681 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001682 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001683
Chris Masond20f7042008-12-08 16:58:54 -05001684 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001685 ret = btrfs_submit_compressed_read(inode, bio,
1686 mirror_num,
1687 bio_flags);
1688 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001689 } else if (!skip_sum) {
1690 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1691 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001692 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001693 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001694 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001695 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001696 /* csum items have already been cloned */
1697 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1698 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001699 /* we're doing a write, do the async checksumming */
Stefan Behrens61891922012-11-05 18:51:52 +01001700 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001701 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001702 bio_flags, bio_offset,
1703 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001704 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001705 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001706 } else if (!skip_sum) {
1707 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1708 if (ret)
1709 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001710 }
1711
Chris Mason0b86a832008-03-24 15:01:56 -04001712mapit:
Stefan Behrens61891922012-11-05 18:51:52 +01001713 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
1714
1715out:
1716 if (ret < 0)
1717 bio_endio(bio, ret);
1718 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001719}
Chris Mason6885f302008-02-20 16:11:05 -05001720
Chris Masond352ac62008-09-29 15:18:18 -04001721/*
1722 * given a list of ordered sums record them in the inode. This happens
1723 * at IO completion time based on sums calculated at bio submission time.
1724 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001725static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001726 struct inode *inode, u64 file_offset,
1727 struct list_head *list)
1728{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001729 struct btrfs_ordered_sum *sum;
1730
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001731 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00001732 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05001733 btrfs_csum_file_blocks(trans,
1734 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00001735 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001736 }
1737 return 0;
1738}
1739
Josef Bacik2ac55d42010-02-03 19:33:23 +00001740int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1741 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001742{
Julia Lawall6c1500f2012-11-03 20:30:18 +00001743 WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04001744 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001745 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001746}
1747
Chris Masond352ac62008-09-29 15:18:18 -04001748/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001749struct btrfs_writepage_fixup {
1750 struct page *page;
1751 struct btrfs_work work;
1752};
1753
Christoph Hellwigb2950862008-12-02 09:54:17 -05001754static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001755{
1756 struct btrfs_writepage_fixup *fixup;
1757 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001758 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001759 struct page *page;
1760 struct inode *inode;
1761 u64 page_start;
1762 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001763 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001764
1765 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1766 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001767again:
Chris Mason247e7432008-07-17 12:53:51 -04001768 lock_page(page);
1769 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1770 ClearPageChecked(page);
1771 goto out_page;
1772 }
1773
1774 inode = page->mapping->host;
1775 page_start = page_offset(page);
1776 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1777
Josef Bacik2ac55d42010-02-03 19:33:23 +00001778 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001779 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001780
1781 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001782 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001783 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001784
1785 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1786 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001787 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1788 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001789 unlock_page(page);
1790 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001791 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04001792 goto again;
1793 }
Chris Mason247e7432008-07-17 12:53:51 -04001794
Jeff Mahoney87826df2012-02-15 16:23:57 +01001795 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
1796 if (ret) {
1797 mapping_set_error(page->mapping, ret);
1798 end_extent_writepage(page, ret, page_start, page_end);
1799 ClearPageChecked(page);
1800 goto out;
1801 }
1802
Josef Bacik2ac55d42010-02-03 19:33:23 +00001803 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001804 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001805 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04001806out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001807 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1808 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001809out_page:
1810 unlock_page(page);
1811 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08001812 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04001813}
1814
1815/*
1816 * There are a few paths in the higher layers of the kernel that directly
1817 * set the page dirty bit without asking the filesystem if it is a
1818 * good idea. This causes problems because we want to make sure COW
1819 * properly happens and the data=ordered rules are followed.
1820 *
Chris Masonc8b97812008-10-29 14:49:59 -04001821 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001822 * hasn't been properly setup for IO. We kick off an async process
1823 * to fix it up. The async helper will wait for ordered extents, set
1824 * the delalloc bit and make it safe to write the page.
1825 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001826static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001827{
1828 struct inode *inode = page->mapping->host;
1829 struct btrfs_writepage_fixup *fixup;
1830 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001831
Chris Mason8b62b722009-09-02 16:53:46 -04001832 /* this page is properly in the ordered list */
1833 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001834 return 0;
1835
1836 if (PageChecked(page))
1837 return -EAGAIN;
1838
1839 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1840 if (!fixup)
1841 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001842
Chris Mason247e7432008-07-17 12:53:51 -04001843 SetPageChecked(page);
1844 page_cache_get(page);
1845 fixup->work.func = btrfs_writepage_fixup_worker;
1846 fixup->page = page;
1847 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001848 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04001849}
1850
Yan Zhengd899e052008-10-30 14:25:28 -04001851static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1852 struct inode *inode, u64 file_pos,
1853 u64 disk_bytenr, u64 disk_num_bytes,
1854 u64 num_bytes, u64 ram_bytes,
1855 u8 compression, u8 encryption,
1856 u16 other_encoding, int extent_type)
1857{
1858 struct btrfs_root *root = BTRFS_I(inode)->root;
1859 struct btrfs_file_extent_item *fi;
1860 struct btrfs_path *path;
1861 struct extent_buffer *leaf;
1862 struct btrfs_key ins;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00001863 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04001864 int ret;
1865
1866 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07001867 if (!path)
1868 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04001869
Chris Masona1ed8352009-09-11 12:27:37 -04001870 /*
1871 * we may be replacing one extent in the tree with another.
1872 * The new extent is pinned in the extent map, and we don't want
1873 * to drop it from the cache until it is completely in the btree.
1874 *
1875 * So, tell btrfs_drop_extents to leave this extent in the cache.
1876 * the caller is expected to unpin it and allow it to be merged
1877 * with the others.
1878 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00001879 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
1880 file_pos + num_bytes, NULL, 0,
1881 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001882 if (ret)
1883 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001884
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00001885 if (!extent_inserted) {
1886 ins.objectid = btrfs_ino(inode);
1887 ins.offset = file_pos;
1888 ins.type = BTRFS_EXTENT_DATA_KEY;
1889
1890 path->leave_spinning = 1;
1891 ret = btrfs_insert_empty_item(trans, root, path, &ins,
1892 sizeof(*fi));
1893 if (ret)
1894 goto out;
1895 }
Yan Zhengd899e052008-10-30 14:25:28 -04001896 leaf = path->nodes[0];
1897 fi = btrfs_item_ptr(leaf, path->slots[0],
1898 struct btrfs_file_extent_item);
1899 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1900 btrfs_set_file_extent_type(leaf, fi, extent_type);
1901 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1902 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1903 btrfs_set_file_extent_offset(leaf, fi, 0);
1904 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1905 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1906 btrfs_set_file_extent_compression(leaf, fi, compression);
1907 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1908 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04001909
Yan Zhengd899e052008-10-30 14:25:28 -04001910 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04001911 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04001912
1913 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04001914
1915 ins.objectid = disk_bytenr;
1916 ins.offset = disk_num_bytes;
1917 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001918 ret = btrfs_alloc_reserved_file_extent(trans, root,
1919 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08001920 btrfs_ino(inode), file_pos, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001921out:
Yan Zhengd899e052008-10-30 14:25:28 -04001922 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04001923
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001924 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04001925}
1926
Liu Bo38c227d2013-01-29 03:18:40 +00001927/* snapshot-aware defrag */
1928struct sa_defrag_extent_backref {
1929 struct rb_node node;
1930 struct old_sa_defrag_extent *old;
1931 u64 root_id;
1932 u64 inum;
1933 u64 file_pos;
1934 u64 extent_offset;
1935 u64 num_bytes;
1936 u64 generation;
1937};
1938
1939struct old_sa_defrag_extent {
1940 struct list_head list;
1941 struct new_sa_defrag_extent *new;
1942
1943 u64 extent_offset;
1944 u64 bytenr;
1945 u64 offset;
1946 u64 len;
1947 int count;
1948};
1949
1950struct new_sa_defrag_extent {
1951 struct rb_root root;
1952 struct list_head head;
1953 struct btrfs_path *path;
1954 struct inode *inode;
1955 u64 file_pos;
1956 u64 len;
1957 u64 bytenr;
1958 u64 disk_len;
1959 u8 compress_type;
1960};
1961
1962static int backref_comp(struct sa_defrag_extent_backref *b1,
1963 struct sa_defrag_extent_backref *b2)
1964{
1965 if (b1->root_id < b2->root_id)
1966 return -1;
1967 else if (b1->root_id > b2->root_id)
1968 return 1;
1969
1970 if (b1->inum < b2->inum)
1971 return -1;
1972 else if (b1->inum > b2->inum)
1973 return 1;
1974
1975 if (b1->file_pos < b2->file_pos)
1976 return -1;
1977 else if (b1->file_pos > b2->file_pos)
1978 return 1;
1979
1980 /*
1981 * [------------------------------] ===> (a range of space)
1982 * |<--->| |<---->| =============> (fs/file tree A)
1983 * |<---------------------------->| ===> (fs/file tree B)
1984 *
1985 * A range of space can refer to two file extents in one tree while
1986 * refer to only one file extent in another tree.
1987 *
1988 * So we may process a disk offset more than one time(two extents in A)
1989 * and locate at the same extent(one extent in B), then insert two same
1990 * backrefs(both refer to the extent in B).
1991 */
1992 return 0;
1993}
1994
1995static void backref_insert(struct rb_root *root,
1996 struct sa_defrag_extent_backref *backref)
1997{
1998 struct rb_node **p = &root->rb_node;
1999 struct rb_node *parent = NULL;
2000 struct sa_defrag_extent_backref *entry;
2001 int ret;
2002
2003 while (*p) {
2004 parent = *p;
2005 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2006
2007 ret = backref_comp(backref, entry);
2008 if (ret < 0)
2009 p = &(*p)->rb_left;
2010 else
2011 p = &(*p)->rb_right;
2012 }
2013
2014 rb_link_node(&backref->node, parent, p);
2015 rb_insert_color(&backref->node, root);
2016}
2017
2018/*
2019 * Note the backref might has changed, and in this case we just return 0.
2020 */
2021static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2022 void *ctx)
2023{
2024 struct btrfs_file_extent_item *extent;
2025 struct btrfs_fs_info *fs_info;
2026 struct old_sa_defrag_extent *old = ctx;
2027 struct new_sa_defrag_extent *new = old->new;
2028 struct btrfs_path *path = new->path;
2029 struct btrfs_key key;
2030 struct btrfs_root *root;
2031 struct sa_defrag_extent_backref *backref;
2032 struct extent_buffer *leaf;
2033 struct inode *inode = new->inode;
2034 int slot;
2035 int ret;
2036 u64 extent_offset;
2037 u64 num_bytes;
2038
2039 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
2040 inum == btrfs_ino(inode))
2041 return 0;
2042
2043 key.objectid = root_id;
2044 key.type = BTRFS_ROOT_ITEM_KEY;
2045 key.offset = (u64)-1;
2046
2047 fs_info = BTRFS_I(inode)->root->fs_info;
2048 root = btrfs_read_fs_root_no_name(fs_info, &key);
2049 if (IS_ERR(root)) {
2050 if (PTR_ERR(root) == -ENOENT)
2051 return 0;
2052 WARN_ON(1);
2053 pr_debug("inum=%llu, offset=%llu, root_id=%llu\n",
2054 inum, offset, root_id);
2055 return PTR_ERR(root);
2056 }
2057
2058 key.objectid = inum;
2059 key.type = BTRFS_EXTENT_DATA_KEY;
2060 if (offset > (u64)-1 << 32)
2061 key.offset = 0;
2062 else
2063 key.offset = offset;
2064
2065 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302066 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002067 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002068 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002069
2070 while (1) {
2071 cond_resched();
2072
2073 leaf = path->nodes[0];
2074 slot = path->slots[0];
2075
2076 if (slot >= btrfs_header_nritems(leaf)) {
2077 ret = btrfs_next_leaf(root, path);
2078 if (ret < 0) {
2079 goto out;
2080 } else if (ret > 0) {
2081 ret = 0;
2082 goto out;
2083 }
2084 continue;
2085 }
2086
2087 path->slots[0]++;
2088
2089 btrfs_item_key_to_cpu(leaf, &key, slot);
2090
2091 if (key.objectid > inum)
2092 goto out;
2093
2094 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2095 continue;
2096
2097 extent = btrfs_item_ptr(leaf, slot,
2098 struct btrfs_file_extent_item);
2099
2100 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2101 continue;
2102
Liu Boe68afa42013-07-01 22:13:26 +08002103 /*
2104 * 'offset' refers to the exact key.offset,
2105 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2106 * (key.offset - extent_offset).
2107 */
2108 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002109 continue;
2110
Liu Boe68afa42013-07-01 22:13:26 +08002111 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002112 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002113
Liu Bo38c227d2013-01-29 03:18:40 +00002114 if (extent_offset >= old->extent_offset + old->offset +
2115 old->len || extent_offset + num_bytes <=
2116 old->extent_offset + old->offset)
2117 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002118 break;
2119 }
2120
2121 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2122 if (!backref) {
2123 ret = -ENOENT;
2124 goto out;
2125 }
2126
2127 backref->root_id = root_id;
2128 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002129 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002130 backref->num_bytes = num_bytes;
2131 backref->extent_offset = extent_offset;
2132 backref->generation = btrfs_file_extent_generation(leaf, extent);
2133 backref->old = old;
2134 backref_insert(&new->root, backref);
2135 old->count++;
2136out:
2137 btrfs_release_path(path);
2138 WARN_ON(ret);
2139 return ret;
2140}
2141
2142static noinline bool record_extent_backrefs(struct btrfs_path *path,
2143 struct new_sa_defrag_extent *new)
2144{
2145 struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info;
2146 struct old_sa_defrag_extent *old, *tmp;
2147 int ret;
2148
2149 new->path = path;
2150
2151 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002152 ret = iterate_inodes_from_logical(old->bytenr +
2153 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002154 path, record_one_backref,
2155 old);
Josef Bacik4724b102013-11-05 11:11:40 -05002156 if (ret < 0 && ret != -ENOENT)
2157 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002158
2159 /* no backref to be processed for this extent */
2160 if (!old->count) {
2161 list_del(&old->list);
2162 kfree(old);
2163 }
2164 }
2165
2166 if (list_empty(&new->head))
2167 return false;
2168
2169 return true;
2170}
2171
2172static int relink_is_mergable(struct extent_buffer *leaf,
2173 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002174 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002175{
Liu Bo116e0022013-08-02 16:30:40 +08002176 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002177 return 0;
2178
2179 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2180 return 0;
2181
Liu Bo116e0022013-08-02 16:30:40 +08002182 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2183 return 0;
2184
2185 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002186 btrfs_file_extent_other_encoding(leaf, fi))
2187 return 0;
2188
2189 return 1;
2190}
2191
2192/*
2193 * Note the backref might has changed, and in this case we just return 0.
2194 */
2195static noinline int relink_extent_backref(struct btrfs_path *path,
2196 struct sa_defrag_extent_backref *prev,
2197 struct sa_defrag_extent_backref *backref)
2198{
2199 struct btrfs_file_extent_item *extent;
2200 struct btrfs_file_extent_item *item;
2201 struct btrfs_ordered_extent *ordered;
2202 struct btrfs_trans_handle *trans;
2203 struct btrfs_fs_info *fs_info;
2204 struct btrfs_root *root;
2205 struct btrfs_key key;
2206 struct extent_buffer *leaf;
2207 struct old_sa_defrag_extent *old = backref->old;
2208 struct new_sa_defrag_extent *new = old->new;
2209 struct inode *src_inode = new->inode;
2210 struct inode *inode;
2211 struct extent_state *cached = NULL;
2212 int ret = 0;
2213 u64 start;
2214 u64 len;
2215 u64 lock_start;
2216 u64 lock_end;
2217 bool merge = false;
2218 int index;
2219
2220 if (prev && prev->root_id == backref->root_id &&
2221 prev->inum == backref->inum &&
2222 prev->file_pos + prev->num_bytes == backref->file_pos)
2223 merge = true;
2224
2225 /* step 1: get root */
2226 key.objectid = backref->root_id;
2227 key.type = BTRFS_ROOT_ITEM_KEY;
2228 key.offset = (u64)-1;
2229
2230 fs_info = BTRFS_I(src_inode)->root->fs_info;
2231 index = srcu_read_lock(&fs_info->subvol_srcu);
2232
2233 root = btrfs_read_fs_root_no_name(fs_info, &key);
2234 if (IS_ERR(root)) {
2235 srcu_read_unlock(&fs_info->subvol_srcu, index);
2236 if (PTR_ERR(root) == -ENOENT)
2237 return 0;
2238 return PTR_ERR(root);
2239 }
Liu Bo38c227d2013-01-29 03:18:40 +00002240
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002241 if (btrfs_root_readonly(root)) {
2242 srcu_read_unlock(&fs_info->subvol_srcu, index);
2243 return 0;
2244 }
2245
Liu Bo38c227d2013-01-29 03:18:40 +00002246 /* step 2: get inode */
2247 key.objectid = backref->inum;
2248 key.type = BTRFS_INODE_ITEM_KEY;
2249 key.offset = 0;
2250
2251 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2252 if (IS_ERR(inode)) {
2253 srcu_read_unlock(&fs_info->subvol_srcu, index);
2254 return 0;
2255 }
2256
2257 srcu_read_unlock(&fs_info->subvol_srcu, index);
2258
2259 /* step 3: relink backref */
2260 lock_start = backref->file_pos;
2261 lock_end = backref->file_pos + backref->num_bytes - 1;
2262 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2263 0, &cached);
2264
2265 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2266 if (ordered) {
2267 btrfs_put_ordered_extent(ordered);
2268 goto out_unlock;
2269 }
2270
2271 trans = btrfs_join_transaction(root);
2272 if (IS_ERR(trans)) {
2273 ret = PTR_ERR(trans);
2274 goto out_unlock;
2275 }
2276
2277 key.objectid = backref->inum;
2278 key.type = BTRFS_EXTENT_DATA_KEY;
2279 key.offset = backref->file_pos;
2280
2281 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2282 if (ret < 0) {
2283 goto out_free_path;
2284 } else if (ret > 0) {
2285 ret = 0;
2286 goto out_free_path;
2287 }
2288
2289 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2290 struct btrfs_file_extent_item);
2291
2292 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2293 backref->generation)
2294 goto out_free_path;
2295
2296 btrfs_release_path(path);
2297
2298 start = backref->file_pos;
2299 if (backref->extent_offset < old->extent_offset + old->offset)
2300 start += old->extent_offset + old->offset -
2301 backref->extent_offset;
2302
2303 len = min(backref->extent_offset + backref->num_bytes,
2304 old->extent_offset + old->offset + old->len);
2305 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2306
2307 ret = btrfs_drop_extents(trans, root, inode, start,
2308 start + len, 1);
2309 if (ret)
2310 goto out_free_path;
2311again:
2312 key.objectid = btrfs_ino(inode);
2313 key.type = BTRFS_EXTENT_DATA_KEY;
2314 key.offset = start;
2315
Liu Boa09a0a72013-03-11 09:20:58 +00002316 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002317 if (merge) {
2318 struct btrfs_file_extent_item *fi;
2319 u64 extent_len;
2320 struct btrfs_key found_key;
2321
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002322 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002323 if (ret < 0)
2324 goto out_free_path;
2325
2326 path->slots[0]--;
2327 leaf = path->nodes[0];
2328 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2329
2330 fi = btrfs_item_ptr(leaf, path->slots[0],
2331 struct btrfs_file_extent_item);
2332 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2333
Liu Bo116e0022013-08-02 16:30:40 +08002334 if (extent_len + found_key.offset == start &&
2335 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002336 btrfs_set_file_extent_num_bytes(leaf, fi,
2337 extent_len + len);
2338 btrfs_mark_buffer_dirty(leaf);
2339 inode_add_bytes(inode, len);
2340
2341 ret = 1;
2342 goto out_free_path;
2343 } else {
2344 merge = false;
2345 btrfs_release_path(path);
2346 goto again;
2347 }
2348 }
2349
2350 ret = btrfs_insert_empty_item(trans, root, path, &key,
2351 sizeof(*extent));
2352 if (ret) {
2353 btrfs_abort_transaction(trans, root, ret);
2354 goto out_free_path;
2355 }
2356
2357 leaf = path->nodes[0];
2358 item = btrfs_item_ptr(leaf, path->slots[0],
2359 struct btrfs_file_extent_item);
2360 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2361 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2362 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2363 btrfs_set_file_extent_num_bytes(leaf, item, len);
2364 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2365 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2366 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2367 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2368 btrfs_set_file_extent_encryption(leaf, item, 0);
2369 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2370
2371 btrfs_mark_buffer_dirty(leaf);
2372 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002373 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002374
2375 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
2376 new->disk_len, 0,
2377 backref->root_id, backref->inum,
2378 new->file_pos, 0); /* start - extent_offset */
2379 if (ret) {
2380 btrfs_abort_transaction(trans, root, ret);
2381 goto out_free_path;
2382 }
2383
2384 ret = 1;
2385out_free_path:
2386 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002387 path->leave_spinning = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002388 btrfs_end_transaction(trans, root);
2389out_unlock:
2390 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2391 &cached, GFP_NOFS);
2392 iput(inode);
2393 return ret;
2394}
2395
Liu Bo6f519562013-10-29 10:45:05 +08002396static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2397{
2398 struct old_sa_defrag_extent *old, *tmp;
2399
2400 if (!new)
2401 return;
2402
2403 list_for_each_entry_safe(old, tmp, &new->head, list) {
2404 list_del(&old->list);
2405 kfree(old);
2406 }
2407 kfree(new);
2408}
2409
Liu Bo38c227d2013-01-29 03:18:40 +00002410static void relink_file_extents(struct new_sa_defrag_extent *new)
2411{
2412 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002413 struct sa_defrag_extent_backref *backref;
2414 struct sa_defrag_extent_backref *prev = NULL;
2415 struct inode *inode;
2416 struct btrfs_root *root;
2417 struct rb_node *node;
2418 int ret;
2419
2420 inode = new->inode;
2421 root = BTRFS_I(inode)->root;
2422
2423 path = btrfs_alloc_path();
2424 if (!path)
2425 return;
2426
2427 if (!record_extent_backrefs(path, new)) {
2428 btrfs_free_path(path);
2429 goto out;
2430 }
2431 btrfs_release_path(path);
2432
2433 while (1) {
2434 node = rb_first(&new->root);
2435 if (!node)
2436 break;
2437 rb_erase(node, &new->root);
2438
2439 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2440
2441 ret = relink_extent_backref(path, prev, backref);
2442 WARN_ON(ret < 0);
2443
2444 kfree(prev);
2445
2446 if (ret == 1)
2447 prev = backref;
2448 else
2449 prev = NULL;
2450 cond_resched();
2451 }
2452 kfree(prev);
2453
2454 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002455out:
Liu Bo6f519562013-10-29 10:45:05 +08002456 free_sa_defrag_extent(new);
2457
Liu Bo38c227d2013-01-29 03:18:40 +00002458 atomic_dec(&root->fs_info->defrag_running);
2459 wake_up(&root->fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002460}
2461
2462static struct new_sa_defrag_extent *
2463record_old_file_extents(struct inode *inode,
2464 struct btrfs_ordered_extent *ordered)
2465{
2466 struct btrfs_root *root = BTRFS_I(inode)->root;
2467 struct btrfs_path *path;
2468 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002469 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002470 struct new_sa_defrag_extent *new;
2471 int ret;
2472
2473 new = kmalloc(sizeof(*new), GFP_NOFS);
2474 if (!new)
2475 return NULL;
2476
2477 new->inode = inode;
2478 new->file_pos = ordered->file_offset;
2479 new->len = ordered->len;
2480 new->bytenr = ordered->start;
2481 new->disk_len = ordered->disk_len;
2482 new->compress_type = ordered->compress_type;
2483 new->root = RB_ROOT;
2484 INIT_LIST_HEAD(&new->head);
2485
2486 path = btrfs_alloc_path();
2487 if (!path)
2488 goto out_kfree;
2489
2490 key.objectid = btrfs_ino(inode);
2491 key.type = BTRFS_EXTENT_DATA_KEY;
2492 key.offset = new->file_pos;
2493
2494 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2495 if (ret < 0)
2496 goto out_free_path;
2497 if (ret > 0 && path->slots[0] > 0)
2498 path->slots[0]--;
2499
2500 /* find out all the old extents for the file range */
2501 while (1) {
2502 struct btrfs_file_extent_item *extent;
2503 struct extent_buffer *l;
2504 int slot;
2505 u64 num_bytes;
2506 u64 offset;
2507 u64 end;
2508 u64 disk_bytenr;
2509 u64 extent_offset;
2510
2511 l = path->nodes[0];
2512 slot = path->slots[0];
2513
2514 if (slot >= btrfs_header_nritems(l)) {
2515 ret = btrfs_next_leaf(root, path);
2516 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002517 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002518 else if (ret > 0)
2519 break;
2520 continue;
2521 }
2522
2523 btrfs_item_key_to_cpu(l, &key, slot);
2524
2525 if (key.objectid != btrfs_ino(inode))
2526 break;
2527 if (key.type != BTRFS_EXTENT_DATA_KEY)
2528 break;
2529 if (key.offset >= new->file_pos + new->len)
2530 break;
2531
2532 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2533
2534 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2535 if (key.offset + num_bytes < new->file_pos)
2536 goto next;
2537
2538 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2539 if (!disk_bytenr)
2540 goto next;
2541
2542 extent_offset = btrfs_file_extent_offset(l, extent);
2543
2544 old = kmalloc(sizeof(*old), GFP_NOFS);
2545 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002546 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002547
2548 offset = max(new->file_pos, key.offset);
2549 end = min(new->file_pos + new->len, key.offset + num_bytes);
2550
2551 old->bytenr = disk_bytenr;
2552 old->extent_offset = extent_offset;
2553 old->offset = offset - key.offset;
2554 old->len = end - offset;
2555 old->new = new;
2556 old->count = 0;
2557 list_add_tail(&old->list, &new->head);
2558next:
2559 path->slots[0]++;
2560 cond_resched();
2561 }
2562
2563 btrfs_free_path(path);
2564 atomic_inc(&root->fs_info->defrag_running);
2565
2566 return new;
2567
Liu Bo38c227d2013-01-29 03:18:40 +00002568out_free_path:
2569 btrfs_free_path(path);
2570out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002571 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002572 return NULL;
2573}
2574
Chris Masond352ac62008-09-29 15:18:18 -04002575/* as ordered data IO finishes, this gets called so we can finish
2576 * an ordered extent if the range of bytes in the file it covers are
2577 * fully written.
2578 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002579static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002580{
Josef Bacik5fd02042012-05-02 14:00:54 -04002581 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002582 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002583 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002584 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002585 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002586 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002587 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002588 int ret = 0;
2589 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002590 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002591 bool truncated = false;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002592
Liu Bo83eea1f2012-07-10 05:28:39 -06002593 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002594
Josef Bacik5fd02042012-05-02 14:00:54 -04002595 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2596 ret = -EIO;
2597 goto out;
2598 }
2599
Josef Bacik77cef2e2013-08-29 13:57:21 -04002600 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2601 truncated = true;
2602 logical_len = ordered_extent->truncated_len;
2603 /* Truncated the entire extent, don't bother adding */
2604 if (!logical_len)
2605 goto out;
2606 }
2607
Yan, Zhengc2167752009-11-12 09:34:21 +00002608 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002609 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Josef Bacik6c760c02012-11-09 10:53:21 -05002610 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2611 if (nolock)
2612 trans = btrfs_join_transaction_nolock(root);
2613 else
2614 trans = btrfs_join_transaction(root);
2615 if (IS_ERR(trans)) {
2616 ret = PTR_ERR(trans);
2617 trans = NULL;
2618 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002619 }
Josef Bacik6c760c02012-11-09 10:53:21 -05002620 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
2621 ret = btrfs_update_inode_fallback(trans, root, inode);
2622 if (ret) /* -ENOMEM or corruption */
2623 btrfs_abort_transaction(trans, root, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002624 goto out;
2625 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002626
Josef Bacik2ac55d42010-02-03 19:33:23 +00002627 lock_extent_bits(io_tree, ordered_extent->file_offset,
2628 ordered_extent->file_offset + ordered_extent->len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002629 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002630
Liu Bo38c227d2013-01-29 03:18:40 +00002631 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2632 ordered_extent->file_offset + ordered_extent->len - 1,
2633 EXTENT_DEFRAG, 1, cached_state);
2634 if (ret) {
2635 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05002636 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00002637 /* the inode is shared */
2638 new = record_old_file_extents(inode, ordered_extent);
2639
2640 clear_extent_bit(io_tree, ordered_extent->file_offset,
2641 ordered_extent->file_offset + ordered_extent->len - 1,
2642 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2643 }
2644
Josef Bacik0cb59c92010-07-02 12:14:14 -04002645 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002646 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002647 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002648 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002649 if (IS_ERR(trans)) {
2650 ret = PTR_ERR(trans);
2651 trans = NULL;
2652 goto out_unlock;
2653 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002654 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002655
Chris Masonc8b97812008-10-29 14:49:59 -04002656 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002657 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002658 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002659 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00002660 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002661 ordered_extent->file_offset,
2662 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002663 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002664 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04002665 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002666 ret = insert_reserved_file_extent(trans, inode,
2667 ordered_extent->file_offset,
2668 ordered_extent->start,
2669 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04002670 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08002671 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002672 BTRFS_FILE_EXTENT_REG);
Yan Zhengd899e052008-10-30 14:25:28 -04002673 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002674 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2675 ordered_extent->file_offset, ordered_extent->len,
2676 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002677 if (ret < 0) {
2678 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002679 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002680 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002681
Chris Masone6dcd2d2008-07-17 12:53:50 -04002682 add_pending_csums(trans, inode, ordered_extent->file_offset,
2683 &ordered_extent->list);
2684
Josef Bacik6c760c02012-11-09 10:53:21 -05002685 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2686 ret = btrfs_update_inode_fallback(trans, root, inode);
2687 if (ret) { /* -ENOMEM or corruption */
2688 btrfs_abort_transaction(trans, root, ret);
2689 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002690 }
2691 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002692out_unlock:
2693 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2694 ordered_extent->file_offset +
2695 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00002696out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002697 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04002698 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002699 if (trans)
2700 btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002701
Josef Bacik77cef2e2013-08-29 13:57:21 -04002702 if (ret || truncated) {
2703 u64 start, end;
2704
2705 if (truncated)
2706 start = ordered_extent->file_offset + logical_len;
2707 else
2708 start = ordered_extent->file_offset;
2709 end = ordered_extent->file_offset + ordered_extent->len - 1;
2710 clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS);
2711
2712 /* Drop the cache for the part of the extent we didn't write. */
2713 btrfs_drop_extent_cache(inode, start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04002714
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002715 /*
2716 * If the ordered extent had an IOERR or something else went
2717 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04002718 * back to the allocator. We only free the extent in the
2719 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002720 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04002721 if ((ret || !logical_len) &&
2722 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002723 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
2724 btrfs_free_reserved_extent(root, ordered_extent->start,
2725 ordered_extent->disk_len);
2726 }
2727
2728
Josef Bacik5fd02042012-05-02 14:00:54 -04002729 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002730 * This needs to be done to make sure anybody waiting knows we are done
2731 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002732 */
2733 btrfs_remove_ordered_extent(inode, ordered_extent);
2734
Liu Bo38c227d2013-01-29 03:18:40 +00002735 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08002736 if (new) {
2737 if (ret) {
2738 free_sa_defrag_extent(new);
2739 atomic_dec(&root->fs_info->defrag_running);
2740 } else {
2741 relink_file_extents(new);
2742 }
2743 }
Liu Bo38c227d2013-01-29 03:18:40 +00002744
Chris Masone6dcd2d2008-07-17 12:53:50 -04002745 /* once for us */
2746 btrfs_put_ordered_extent(ordered_extent);
2747 /* once for the tree */
2748 btrfs_put_ordered_extent(ordered_extent);
2749
Josef Bacik5fd02042012-05-02 14:00:54 -04002750 return ret;
2751}
2752
Qu Wenruofccb5d82014-02-28 10:46:10 +08002753static void finish_ordered_fn(struct btrfs_work_struct *work)
Josef Bacik5fd02042012-05-02 14:00:54 -04002754{
2755 struct btrfs_ordered_extent *ordered_extent;
2756 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2757 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002758}
2759
Christoph Hellwigb2950862008-12-02 09:54:17 -05002760static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04002761 struct extent_state *state, int uptodate)
2762{
Josef Bacik5fd02042012-05-02 14:00:54 -04002763 struct inode *inode = page->mapping->host;
2764 struct btrfs_root *root = BTRFS_I(inode)->root;
2765 struct btrfs_ordered_extent *ordered_extent = NULL;
Qu Wenruofccb5d82014-02-28 10:46:10 +08002766 struct btrfs_workqueue_struct *workers;
Josef Bacik5fd02042012-05-02 14:00:54 -04002767
liubo1abe9b82011-03-24 11:18:59 +00002768 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2769
Chris Mason8b62b722009-09-02 16:53:46 -04002770 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002771 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2772 end - start + 1, uptodate))
2773 return 0;
2774
Qu Wenruofccb5d82014-02-28 10:46:10 +08002775 btrfs_init_work(&ordered_extent->work, finish_ordered_fn, NULL, NULL);
Josef Bacik5fd02042012-05-02 14:00:54 -04002776
Liu Bo83eea1f2012-07-10 05:28:39 -06002777 if (btrfs_is_free_space_inode(inode))
Qu Wenruofccb5d82014-02-28 10:46:10 +08002778 workers = root->fs_info->endio_freespace_worker;
Josef Bacik5fd02042012-05-02 14:00:54 -04002779 else
Qu Wenruofccb5d82014-02-28 10:46:10 +08002780 workers = root->fs_info->endio_write_workers;
2781 btrfs_queue_work(workers, &ordered_extent->work);
Josef Bacik5fd02042012-05-02 14:00:54 -04002782
2783 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04002784}
2785
Chris Masond352ac62008-09-29 15:18:18 -04002786/*
Chris Masond352ac62008-09-29 15:18:18 -04002787 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002788 * if there's a match, we allow the bio to finish. If not, the code in
2789 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002790 */
Miao Xiefacc8a22013-07-25 19:22:34 +08002791static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
2792 u64 phy_offset, struct page *page,
2793 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002794{
Miao Xie4eee4fa2012-12-21 09:17:45 +00002795 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04002796 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002797 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04002798 char *kaddr;
Chris Masonff79f812007-10-15 16:22:25 -04002799 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiefacc8a22013-07-25 19:22:34 +08002800 u32 csum_expected;
Chris Masonff79f812007-10-15 16:22:25 -04002801 u32 csum = ~(u32)0;
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002802 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
2803 DEFAULT_RATELIMIT_BURST);
Chris Masond1310b22008-01-24 16:13:08 -05002804
Chris Masond20f7042008-12-08 16:58:54 -05002805 if (PageChecked(page)) {
2806 ClearPageChecked(page);
2807 goto good;
2808 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002809
2810 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Jan Schmidt08d2f342011-05-04 16:18:50 +02002811 goto good;
Chris Masond20f7042008-12-08 16:58:54 -05002812
Yan Zheng17d217f2008-12-12 10:03:38 -05002813 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002814 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002815 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
2816 GFP_NOFS);
2817 return 0;
2818 }
2819
Miao Xiefacc8a22013-07-25 19:22:34 +08002820 phy_offset >>= inode->i_sb->s_blocksize_bits;
2821 csum_expected = *(((u32 *)io_bio->csum) + phy_offset);
Chris Masond3977122009-01-05 21:25:51 -05002822
Miao Xiefacc8a22013-07-25 19:22:34 +08002823 kaddr = kmap_atomic(page);
Liu Bob0496682013-03-14 14:57:45 +00002824 csum = btrfs_csum_data(kaddr + offset, csum, end - start + 1);
Chris Masonff79f812007-10-15 16:22:25 -04002825 btrfs_csum_final(csum, (char *)&csum);
Miao Xiefacc8a22013-07-25 19:22:34 +08002826 if (csum != csum_expected)
Chris Mason07157aa2007-08-30 08:50:51 -04002827 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002828
Cong Wang7ac687d2011-11-25 23:14:28 +08002829 kunmap_atomic(kaddr);
Chris Masond20f7042008-12-08 16:58:54 -05002830good:
Chris Mason07157aa2007-08-30 08:50:51 -04002831 return 0;
2832
2833zeroit:
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002834 if (__ratelimit(&_rs))
Miao Xiefacc8a22013-07-25 19:22:34 +08002835 btrfs_info(root->fs_info, "csum failed ino %llu off %llu csum %u expected csum %u",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002836 btrfs_ino(page->mapping->host), start, csum, csum_expected);
Chris Masondb945352007-10-15 16:15:53 -04002837 memset(kaddr + offset, 1, end - start + 1);
2838 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002839 kunmap_atomic(kaddr);
Miao Xiefacc8a22013-07-25 19:22:34 +08002840 if (csum_expected == 0)
Chris Mason3b951512008-04-17 11:29:12 -04002841 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04002842 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04002843}
Chris Masonb888db22007-08-27 16:49:44 -04002844
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002845struct delayed_iput {
2846 struct list_head list;
2847 struct inode *inode;
2848};
2849
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002850/* JDM: If this is fs-wide, why can't we add a pointer to
2851 * btrfs_inode instead and avoid the allocation? */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002852void btrfs_add_delayed_iput(struct inode *inode)
2853{
2854 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2855 struct delayed_iput *delayed;
2856
2857 if (atomic_add_unless(&inode->i_count, -1, 1))
2858 return;
2859
2860 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2861 delayed->inode = inode;
2862
2863 spin_lock(&fs_info->delayed_iput_lock);
2864 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2865 spin_unlock(&fs_info->delayed_iput_lock);
2866}
2867
2868void btrfs_run_delayed_iputs(struct btrfs_root *root)
2869{
2870 LIST_HEAD(list);
2871 struct btrfs_fs_info *fs_info = root->fs_info;
2872 struct delayed_iput *delayed;
2873 int empty;
2874
2875 spin_lock(&fs_info->delayed_iput_lock);
2876 empty = list_empty(&fs_info->delayed_iputs);
2877 spin_unlock(&fs_info->delayed_iput_lock);
2878 if (empty)
2879 return;
2880
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002881 spin_lock(&fs_info->delayed_iput_lock);
2882 list_splice_init(&fs_info->delayed_iputs, &list);
2883 spin_unlock(&fs_info->delayed_iput_lock);
2884
2885 while (!list_empty(&list)) {
2886 delayed = list_entry(list.next, struct delayed_iput, list);
2887 list_del(&delayed->list);
2888 iput(delayed->inode);
2889 kfree(delayed);
2890 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002891}
2892
Yan, Zhengd68fc572010-05-16 10:49:58 -04002893/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08002894 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04002895 * files in the subvolume, it removes orphan item and frees block_rsv
2896 * structure.
2897 */
2898void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
2899 struct btrfs_root *root)
2900{
Josef Bacik90290e12011-12-02 15:44:12 -05002901 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002902 int ret;
2903
Josef Bacik8a35d952012-05-23 14:26:42 -04002904 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04002905 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
2906 return;
2907
Josef Bacik90290e12011-12-02 15:44:12 -05002908 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04002909 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05002910 spin_unlock(&root->orphan_lock);
2911 return;
2912 }
2913
2914 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
2915 spin_unlock(&root->orphan_lock);
2916 return;
2917 }
2918
2919 block_rsv = root->orphan_block_rsv;
2920 root->orphan_block_rsv = NULL;
2921 spin_unlock(&root->orphan_lock);
2922
Yan, Zhengd68fc572010-05-16 10:49:58 -04002923 if (root->orphan_item_inserted &&
2924 btrfs_root_refs(&root->root_item) > 0) {
2925 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
2926 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04002927 if (ret)
2928 btrfs_abort_transaction(trans, root, ret);
2929 else
2930 root->orphan_item_inserted = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002931 }
2932
Josef Bacik90290e12011-12-02 15:44:12 -05002933 if (block_rsv) {
2934 WARN_ON(block_rsv->size > 0);
2935 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002936 }
2937}
2938
2939/*
Josef Bacik7b128762008-07-24 12:17:14 -04002940 * This creates an orphan entry for the given inode in case something goes
2941 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04002942 *
2943 * NOTE: caller of this function should reserve 5 units of metadata for
2944 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04002945 */
2946int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2947{
2948 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002949 struct btrfs_block_rsv *block_rsv = NULL;
2950 int reserve = 0;
2951 int insert = 0;
2952 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002953
Yan, Zhengd68fc572010-05-16 10:49:58 -04002954 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06002955 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00002956 if (!block_rsv)
2957 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04002958 }
2959
Yan, Zhengd68fc572010-05-16 10:49:58 -04002960 spin_lock(&root->orphan_lock);
2961 if (!root->orphan_block_rsv) {
2962 root->orphan_block_rsv = block_rsv;
2963 } else if (block_rsv) {
2964 btrfs_free_block_rsv(root, block_rsv);
2965 block_rsv = NULL;
2966 }
Josef Bacik7b128762008-07-24 12:17:14 -04002967
Josef Bacik8a35d952012-05-23 14:26:42 -04002968 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2969 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04002970#if 0
2971 /*
2972 * For proper ENOSPC handling, we should do orphan
2973 * cleanup when mounting. But this introduces backward
2974 * compatibility issue.
2975 */
2976 if (!xchg(&root->orphan_item_inserted, 1))
2977 insert = 2;
2978 else
2979 insert = 1;
2980#endif
2981 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06002982 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002983 }
Josef Bacik7b128762008-07-24 12:17:14 -04002984
Josef Bacik72ac3c02012-05-23 14:13:11 -04002985 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
2986 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04002987 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002988 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002989
Yan, Zhengd68fc572010-05-16 10:49:58 -04002990 /* grab metadata reservation from transaction handle */
2991 if (reserve) {
2992 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002993 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04002994 }
2995
2996 /* insert an orphan item to track this unlinked/truncated file */
2997 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08002998 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04002999 if (ret) {
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003000 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003001 if (reserve) {
3002 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3003 &BTRFS_I(inode)->runtime_flags);
3004 btrfs_orphan_release_metadata(inode);
3005 }
3006 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003007 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3008 &BTRFS_I(inode)->runtime_flags);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003009 btrfs_abort_transaction(trans, root, ret);
3010 return ret;
3011 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003012 }
3013 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003014 }
3015
3016 /* insert an orphan item to track subvolume contains orphan files */
3017 if (insert >= 2) {
3018 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
3019 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003020 if (ret && ret != -EEXIST) {
3021 btrfs_abort_transaction(trans, root, ret);
3022 return ret;
3023 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003024 }
3025 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003026}
3027
3028/*
3029 * We have done the truncate/delete so we can go ahead and remove the orphan
3030 * item for this particular inode.
3031 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003032static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3033 struct inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003034{
3035 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003036 int delete_item = 0;
3037 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003038 int ret = 0;
3039
Yan, Zhengd68fc572010-05-16 10:49:58 -04003040 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003041 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3042 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003043 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003044
Josef Bacik72ac3c02012-05-23 14:13:11 -04003045 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3046 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003047 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003048 spin_unlock(&root->orphan_lock);
3049
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003050 if (delete_item) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003051 atomic_dec(&root->orphan_inodes);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003052 if (trans)
3053 ret = btrfs_del_orphan_item(trans, root,
3054 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04003055 }
Josef Bacik7b128762008-07-24 12:17:14 -04003056
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003057 if (release_rsv)
3058 btrfs_orphan_release_metadata(inode);
3059
Josef Bacik4ef31a42013-08-13 14:10:08 -04003060 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003061}
3062
3063/*
3064 * this cleans up any orphans that may be left on the list from the last use
3065 * of this root.
3066 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003067int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003068{
3069 struct btrfs_path *path;
3070 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003071 struct btrfs_key key, found_key;
3072 struct btrfs_trans_handle *trans;
3073 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003074 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003075 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3076
Yan, Zhengd68fc572010-05-16 10:49:58 -04003077 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003078 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003079
3080 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003081 if (!path) {
3082 ret = -ENOMEM;
3083 goto out;
3084 }
Josef Bacik7b128762008-07-24 12:17:14 -04003085 path->reada = -1;
3086
3087 key.objectid = BTRFS_ORPHAN_OBJECTID;
3088 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
3089 key.offset = (u64)-1;
3090
Josef Bacik7b128762008-07-24 12:17:14 -04003091 while (1) {
3092 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003093 if (ret < 0)
3094 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003095
3096 /*
3097 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003098 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003099 * find the key and see if we have stuff that matches
3100 */
3101 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003102 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003103 if (path->slots[0] == 0)
3104 break;
3105 path->slots[0]--;
3106 }
3107
3108 /* pull out the item */
3109 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003110 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3111
3112 /* make sure the item matches what we want */
3113 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3114 break;
3115 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
3116 break;
3117
3118 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003119 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003120
3121 /*
3122 * this is where we are basically btrfs_lookup, without the
3123 * crossing root thing. we store the inode number in the
3124 * offset of the orphan item.
3125 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003126
3127 if (found_key.offset == last_objectid) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003128 btrfs_err(root->fs_info,
3129 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003130 ret = -EINVAL;
3131 goto out;
3132 }
3133
3134 last_objectid = found_key.offset;
3135
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003136 found_key.objectid = found_key.offset;
3137 found_key.type = BTRFS_INODE_ITEM_KEY;
3138 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00003139 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303140 ret = PTR_ERR_OR_ZERO(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003141 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003142 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003143
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003144 if (ret == -ESTALE && root == root->fs_info->tree_root) {
3145 struct btrfs_root *dead_root;
3146 struct btrfs_fs_info *fs_info = root->fs_info;
3147 int is_dead_root = 0;
3148
3149 /*
3150 * this is an orphan in the tree root. Currently these
3151 * could come from 2 sources:
3152 * a) a snapshot deletion in progress
3153 * b) a free space cache inode
3154 * We need to distinguish those two, as the snapshot
3155 * orphan must not get deleted.
3156 * find_dead_roots already ran before us, so if this
3157 * is a snapshot deletion, we should find the root
3158 * in the dead_roots list
3159 */
3160 spin_lock(&fs_info->trans_lock);
3161 list_for_each_entry(dead_root, &fs_info->dead_roots,
3162 root_list) {
3163 if (dead_root->root_key.objectid ==
3164 found_key.objectid) {
3165 is_dead_root = 1;
3166 break;
3167 }
3168 }
3169 spin_unlock(&fs_info->trans_lock);
3170 if (is_dead_root) {
3171 /* prevent this orphan from being found again */
3172 key.offset = found_key.objectid - 1;
3173 continue;
3174 }
3175 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003176 /*
3177 * Inode is already gone but the orphan item is still there,
3178 * kill the orphan item.
3179 */
3180 if (ret == -ESTALE) {
3181 trans = btrfs_start_transaction(root, 1);
3182 if (IS_ERR(trans)) {
3183 ret = PTR_ERR(trans);
3184 goto out;
3185 }
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003186 btrfs_debug(root->fs_info, "auto deleting %Lu",
3187 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003188 ret = btrfs_del_orphan_item(trans, root,
3189 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003190 btrfs_end_transaction(trans, root);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003191 if (ret)
3192 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003193 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003194 }
Josef Bacik7b128762008-07-24 12:17:14 -04003195
Josef Bacik7b128762008-07-24 12:17:14 -04003196 /*
3197 * add this inode to the orphan list so btrfs_orphan_del does
3198 * the proper thing when we hit it
3199 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003200 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3201 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003202 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003203
Josef Bacik7b128762008-07-24 12:17:14 -04003204 /* if we have links, this was a truncate, lets do that */
3205 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303206 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003207 iput(inode);
3208 continue;
3209 }
Josef Bacik7b128762008-07-24 12:17:14 -04003210 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003211
3212 /* 1 for the orphan item deletion. */
3213 trans = btrfs_start_transaction(root, 1);
3214 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003215 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003216 ret = PTR_ERR(trans);
3217 goto out;
3218 }
3219 ret = btrfs_orphan_add(trans, inode);
3220 btrfs_end_transaction(trans, root);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003221 if (ret) {
3222 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003223 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003224 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003225
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003226 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003227 if (ret)
3228 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003229 } else {
3230 nr_unlink++;
3231 }
3232
3233 /* this will do delete_inode and everything for us */
3234 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003235 if (ret)
3236 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003237 }
Miao Xie3254c872011-11-10 20:45:05 -05003238 /* release the path since we're done with it */
3239 btrfs_release_path(path);
3240
Yan, Zhengd68fc572010-05-16 10:49:58 -04003241 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3242
3243 if (root->orphan_block_rsv)
3244 btrfs_block_rsv_release(root, root->orphan_block_rsv,
3245 (u64)-1);
3246
3247 if (root->orphan_block_rsv || root->orphan_item_inserted) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003248 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003249 if (!IS_ERR(trans))
3250 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003251 }
Josef Bacik7b128762008-07-24 12:17:14 -04003252
3253 if (nr_unlink)
David Sterba4884b472013-03-20 13:31:27 +00003254 btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003255 if (nr_truncate)
David Sterba4884b472013-03-20 13:31:27 +00003256 btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003257
3258out:
3259 if (ret)
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003260 btrfs_crit(root->fs_info,
3261 "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003262 btrfs_free_path(path);
3263 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003264}
3265
Chris Masond352ac62008-09-29 15:18:18 -04003266/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003267 * very simple check to peek ahead in the leaf looking for xattrs. If we
3268 * don't find any xattrs, we know there can't be any acls.
3269 *
3270 * slot is the slot the inode is in, objectid is the objectid of the inode
3271 */
3272static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003273 int slot, u64 objectid,
3274 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003275{
3276 u32 nritems = btrfs_header_nritems(leaf);
3277 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003278 static u64 xattr_access = 0;
3279 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003280 int scanned = 0;
3281
Josef Bacikf23b5a52013-06-19 10:16:26 -04003282 if (!xattr_access) {
3283 xattr_access = btrfs_name_hash(POSIX_ACL_XATTR_ACCESS,
3284 strlen(POSIX_ACL_XATTR_ACCESS));
3285 xattr_default = btrfs_name_hash(POSIX_ACL_XATTR_DEFAULT,
3286 strlen(POSIX_ACL_XATTR_DEFAULT));
3287 }
3288
Chris Mason46a53cc2009-04-27 11:47:50 -04003289 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003290 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003291 while (slot < nritems) {
3292 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3293
3294 /* we found a different objectid, there must not be acls */
3295 if (found_key.objectid != objectid)
3296 return 0;
3297
3298 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003299 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003300 if (*first_xattr_slot == -1)
3301 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003302 if (found_key.offset == xattr_access ||
3303 found_key.offset == xattr_default)
3304 return 1;
3305 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003306
3307 /*
3308 * we found a key greater than an xattr key, there can't
3309 * be any acls later on
3310 */
3311 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3312 return 0;
3313
3314 slot++;
3315 scanned++;
3316
3317 /*
3318 * it goes inode, inode backrefs, xattrs, extents,
3319 * so if there are a ton of hard links to an inode there can
3320 * be a lot of backrefs. Don't waste time searching too hard,
3321 * this is just an optimization
3322 */
3323 if (scanned >= 8)
3324 break;
3325 }
3326 /* we hit the end of the leaf before we found an xattr or
3327 * something larger than an xattr. We have to assume the inode
3328 * has acls
3329 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003330 if (*first_xattr_slot == -1)
3331 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003332 return 1;
3333}
3334
3335/*
Chris Masond352ac62008-09-29 15:18:18 -04003336 * read an inode from the btree into the in-memory inode
3337 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003338static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003339{
3340 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003341 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003342 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04003343 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04003344 struct btrfs_root *root = BTRFS_I(inode)->root;
3345 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003346 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003347 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003348 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003349 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003350 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003351 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003352
3353 ret = btrfs_fill_inode(inode, &rdev);
3354 if (!ret)
3355 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003356
3357 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07003358 if (!path)
3359 goto make_bad;
3360
Chris Mason39279cc2007-06-12 06:35:45 -04003361 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003362
Chris Mason39279cc2007-06-12 06:35:45 -04003363 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003364 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04003365 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04003366
Chris Mason5f39d392007-10-15 16:14:19 -04003367 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003368
3369 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003370 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003371
Chris Mason5f39d392007-10-15 16:14:19 -04003372 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3373 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003374 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003375 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003376 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3377 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04003378 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003379
3380 tspec = btrfs_inode_atime(inode_item);
3381 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3382 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3383
3384 tspec = btrfs_inode_mtime(inode_item);
3385 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3386 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3387
3388 tspec = btrfs_inode_ctime(inode_item);
3389 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3390 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3391
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003392 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003393 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003394 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3395
3396 /*
3397 * If we were modified in the current generation and evicted from memory
3398 * and then re-read we need to do a full sync since we don't have any
3399 * idea about which extents were modified before we were evicted from
3400 * cache.
3401 */
3402 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
3403 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3404 &BTRFS_I(inode)->runtime_flags);
3405
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003406 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003407 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003408 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003409 rdev = btrfs_inode_rdev(leaf, inode_item);
3410
Josef Bacikaec74772008-07-24 12:12:38 -04003411 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003412 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003413
3414cache_index:
3415 path->slots[0]++;
3416 if (inode->i_nlink != 1 ||
3417 path->slots[0] >= btrfs_header_nritems(leaf))
3418 goto cache_acl;
3419
3420 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
3421 if (location.objectid != btrfs_ino(inode))
3422 goto cache_acl;
3423
3424 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3425 if (location.type == BTRFS_INODE_REF_KEY) {
3426 struct btrfs_inode_ref *ref;
3427
3428 ref = (struct btrfs_inode_ref *)ptr;
3429 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3430 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3431 struct btrfs_inode_extref *extref;
3432
3433 extref = (struct btrfs_inode_extref *)ptr;
3434 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3435 extref);
3436 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003437cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003438 /*
3439 * try to precache a NULL acl entry for files that don't have
3440 * any xattrs or acls
3441 */
Li Zefan33345d012011-04-20 10:31:50 +08003442 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003443 btrfs_ino(inode), &first_xattr_slot);
3444 if (first_xattr_slot != -1) {
3445 path->slots[0] = first_xattr_slot;
3446 ret = btrfs_load_inode_props(inode, path);
3447 if (ret)
3448 btrfs_err(root->fs_info,
3449 "error loading props for ino %llu (root %llu): %d\n",
3450 btrfs_ino(inode),
3451 root->root_key.objectid, ret);
3452 }
3453 btrfs_free_path(path);
3454
Al Viro72c04902009-06-24 16:58:48 -04003455 if (!maybe_acls)
3456 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003457
Chris Mason39279cc2007-06-12 06:35:45 -04003458 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003459 case S_IFREG:
3460 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003461 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05003462 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003463 inode->i_fop = &btrfs_file_operations;
3464 inode->i_op = &btrfs_file_inode_operations;
3465 break;
3466 case S_IFDIR:
3467 inode->i_fop = &btrfs_dir_file_operations;
3468 if (root == root->fs_info->tree_root)
3469 inode->i_op = &btrfs_dir_ro_inode_operations;
3470 else
3471 inode->i_op = &btrfs_dir_inode_operations;
3472 break;
3473 case S_IFLNK:
3474 inode->i_op = &btrfs_symlink_inode_operations;
3475 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04003476 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003477 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003478 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003479 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003480 init_special_inode(inode, inode->i_mode, rdev);
3481 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003482 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003483
3484 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003485 return;
3486
3487make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003488 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003489 make_bad_inode(inode);
3490}
3491
Chris Masond352ac62008-09-29 15:18:18 -04003492/*
3493 * given a leaf and an inode, copy the inode fields into the leaf
3494 */
Chris Masone02119d2008-09-05 16:13:11 -04003495static void fill_inode_item(struct btrfs_trans_handle *trans,
3496 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003497 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003498 struct inode *inode)
3499{
Liu Bo51fab692012-12-27 09:01:21 +00003500 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003501
Liu Bo51fab692012-12-27 09:01:21 +00003502 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003503
Liu Bo51fab692012-12-27 09:01:21 +00003504 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3505 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3506 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3507 &token);
3508 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3509 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003510
Liu Bo51fab692012-12-27 09:01:21 +00003511 btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item),
3512 inode->i_atime.tv_sec, &token);
3513 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item),
3514 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003515
Liu Bo51fab692012-12-27 09:01:21 +00003516 btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item),
3517 inode->i_mtime.tv_sec, &token);
3518 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item),
3519 inode->i_mtime.tv_nsec, &token);
3520
3521 btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item),
3522 inode->i_ctime.tv_sec, &token);
3523 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item),
3524 inode->i_ctime.tv_nsec, &token);
3525
3526 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3527 &token);
3528 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3529 &token);
3530 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3531 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3532 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3533 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3534 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003535}
3536
Chris Masond352ac62008-09-29 15:18:18 -04003537/*
3538 * copy everything in the in-memory inode into the btree.
3539 */
Chris Mason21151332011-11-10 20:39:08 -05003540static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003541 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003542{
3543 struct btrfs_inode_item *inode_item;
3544 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003545 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003546 int ret;
3547
3548 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003549 if (!path)
3550 return -ENOMEM;
3551
Chris Masonb9473432009-03-13 11:00:37 -04003552 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003553 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3554 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003555 if (ret) {
3556 if (ret > 0)
3557 ret = -ENOENT;
3558 goto failed;
3559 }
3560
Chris Mason5f39d392007-10-15 16:14:19 -04003561 leaf = path->nodes[0];
3562 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003563 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003564
Chris Masone02119d2008-09-05 16:13:11 -04003565 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003566 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003567 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003568 ret = 0;
3569failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003570 btrfs_free_path(path);
3571 return ret;
3572}
3573
Chris Masond352ac62008-09-29 15:18:18 -04003574/*
Chris Mason21151332011-11-10 20:39:08 -05003575 * copy everything in the in-memory inode into the btree.
3576 */
3577noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3578 struct btrfs_root *root, struct inode *inode)
3579{
3580 int ret;
3581
3582 /*
3583 * If the inode is a free space inode, we can deadlock during commit
3584 * if we put it into the delayed code.
3585 *
3586 * The data relocation inode should also be directly updated
3587 * without delay
3588 */
Liu Bo83eea1f2012-07-10 05:28:39 -06003589 if (!btrfs_is_free_space_inode(inode)
Chris Mason21151332011-11-10 20:39:08 -05003590 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003591 btrfs_update_root_times(trans, root);
3592
Chris Mason21151332011-11-10 20:39:08 -05003593 ret = btrfs_delayed_update_inode(trans, root, inode);
3594 if (!ret)
3595 btrfs_set_inode_last_trans(trans, inode);
3596 return ret;
3597 }
3598
3599 return btrfs_update_inode_item(trans, root, inode);
3600}
3601
Josef Bacikbe6aef62012-10-22 15:43:12 -04003602noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3603 struct btrfs_root *root,
3604 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003605{
3606 int ret;
3607
3608 ret = btrfs_update_inode(trans, root, inode);
3609 if (ret == -ENOSPC)
3610 return btrfs_update_inode_item(trans, root, inode);
3611 return ret;
3612}
3613
3614/*
Chris Masond352ac62008-09-29 15:18:18 -04003615 * unlink helper that gets used here in inode.c and in the tree logging
3616 * recovery code. It remove a link in a directory with a given name, and
3617 * also drops the back refs in the inode to the directory
3618 */
Al Viro92986792011-03-04 17:14:37 +00003619static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3620 struct btrfs_root *root,
3621 struct inode *dir, struct inode *inode,
3622 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003623{
3624 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003625 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003626 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003627 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003628 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003629 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003630 u64 ino = btrfs_ino(inode);
3631 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003632
3633 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003634 if (!path) {
3635 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003636 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003637 }
3638
Chris Masonb9473432009-03-13 11:00:37 -04003639 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003640 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003641 name, name_len, -1);
3642 if (IS_ERR(di)) {
3643 ret = PTR_ERR(di);
3644 goto err;
3645 }
3646 if (!di) {
3647 ret = -ENOENT;
3648 goto err;
3649 }
Chris Mason5f39d392007-10-15 16:14:19 -04003650 leaf = path->nodes[0];
3651 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003652 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003653 if (ret)
3654 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003655 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003656
Miao Xie67de1172013-12-26 13:07:06 +08003657 /*
3658 * If we don't have dir index, we have to get it by looking up
3659 * the inode ref, since we get the inode ref, remove it directly,
3660 * it is unnecessary to do delayed deletion.
3661 *
3662 * But if we have dir index, needn't search inode ref to get it.
3663 * Since the inode ref is close to the inode item, it is better
3664 * that we delay to delete it, and just do this deletion when
3665 * we update the inode item.
3666 */
3667 if (BTRFS_I(inode)->dir_index) {
3668 ret = btrfs_delayed_delete_inode_ref(inode);
3669 if (!ret) {
3670 index = BTRFS_I(inode)->dir_index;
3671 goto skip_backref;
3672 }
3673 }
3674
Li Zefan33345d012011-04-20 10:31:50 +08003675 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3676 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003677 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003678 btrfs_info(root->fs_info,
3679 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003680 name_len, name, ino, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003681 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003682 goto err;
3683 }
Miao Xie67de1172013-12-26 13:07:06 +08003684skip_backref:
Miao Xie16cdcec2011-04-22 18:12:22 +08003685 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003686 if (ret) {
3687 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003688 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003689 }
Chris Mason39279cc2007-06-12 06:35:45 -04003690
Chris Masone02119d2008-09-05 16:13:11 -04003691 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08003692 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003693 if (ret != 0 && ret != -ENOENT) {
3694 btrfs_abort_transaction(trans, root, ret);
3695 goto err;
3696 }
Chris Masone02119d2008-09-05 16:13:11 -04003697
3698 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
3699 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04003700 if (ret == -ENOENT)
3701 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003702 else if (ret)
3703 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003704err:
3705 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003706 if (ret)
3707 goto out;
3708
3709 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003710 inode_inc_iversion(inode);
3711 inode_inc_iversion(dir);
Chris Masone02119d2008-09-05 16:13:11 -04003712 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Tsutomu Itohb9959292012-06-25 21:25:22 -06003713 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04003714out:
Chris Mason39279cc2007-06-12 06:35:45 -04003715 return ret;
3716}
3717
Al Viro92986792011-03-04 17:14:37 +00003718int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3719 struct btrfs_root *root,
3720 struct inode *dir, struct inode *inode,
3721 const char *name, int name_len)
3722{
3723 int ret;
3724 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3725 if (!ret) {
Zach Brown8b558c52013-10-16 12:10:34 -07003726 drop_nlink(inode);
Al Viro92986792011-03-04 17:14:37 +00003727 ret = btrfs_update_inode(trans, root, inode);
3728 }
3729 return ret;
3730}
Yan, Zhenga22285a2010-05-16 10:48:46 -04003731
3732/*
3733 * helper to start transaction for unlink and rmdir.
3734 *
Josef Bacikd52be812013-05-29 14:54:47 -04003735 * unlink and rmdir are special in btrfs, they do not always free space, so
3736 * if we cannot make our reservations the normal way try and see if there is
3737 * plenty of slack room in the global reserve to migrate, otherwise we cannot
3738 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04003739 */
Josef Bacikd52be812013-05-29 14:54:47 -04003740static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003741{
3742 struct btrfs_trans_handle *trans;
3743 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003744 int ret;
3745
Josef Bacike70bea52011-10-11 14:18:24 -04003746 /*
3747 * 1 for the possible orphan item
3748 * 1 for the dir item
3749 * 1 for the dir index
3750 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04003751 * 1 for the inode
3752 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04003753 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003754 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
3755 return trans;
3756
Josef Bacikd52be812013-05-29 14:54:47 -04003757 if (PTR_ERR(trans) == -ENOSPC) {
3758 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003759
Josef Bacikd52be812013-05-29 14:54:47 -04003760 trans = btrfs_start_transaction(root, 0);
3761 if (IS_ERR(trans))
3762 return trans;
3763 ret = btrfs_cond_migrate_bytes(root->fs_info,
3764 &root->fs_info->trans_block_rsv,
3765 num_bytes, 5);
3766 if (ret) {
3767 btrfs_end_transaction(trans, root);
3768 return ERR_PTR(ret);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003769 }
Josef Bacik5a77d762011-11-01 14:32:23 -04003770 trans->block_rsv = &root->fs_info->trans_block_rsv;
Josef Bacikd52be812013-05-29 14:54:47 -04003771 trans->bytes_reserved = num_bytes;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003772 }
Josef Bacikd52be812013-05-29 14:54:47 -04003773 return trans;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003774}
3775
Chris Mason39279cc2007-06-12 06:35:45 -04003776static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3777{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003778 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003779 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04003780 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003781 int ret;
3782
Josef Bacikd52be812013-05-29 14:54:47 -04003783 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003784 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003785 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003786
Chris Mason12fcfd22009-03-24 10:24:20 -04003787 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
3788
Chris Masone02119d2008-09-05 16:13:11 -04003789 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3790 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003791 if (ret)
3792 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003793
Yan, Zhenga22285a2010-05-16 10:48:46 -04003794 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04003795 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003796 if (ret)
3797 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003798 }
Josef Bacik7b128762008-07-24 12:17:14 -04003799
Tsutomu Itohb5324022011-07-19 07:27:20 +00003800out:
Josef Bacikd52be812013-05-29 14:54:47 -04003801 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003802 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04003803 return ret;
3804}
3805
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003806int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3807 struct btrfs_root *root,
3808 struct inode *dir, u64 objectid,
3809 const char *name, int name_len)
3810{
3811 struct btrfs_path *path;
3812 struct extent_buffer *leaf;
3813 struct btrfs_dir_item *di;
3814 struct btrfs_key key;
3815 u64 index;
3816 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003817 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003818
3819 path = btrfs_alloc_path();
3820 if (!path)
3821 return -ENOMEM;
3822
Li Zefan33345d012011-04-20 10:31:50 +08003823 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003824 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003825 if (IS_ERR_OR_NULL(di)) {
3826 if (!di)
3827 ret = -ENOENT;
3828 else
3829 ret = PTR_ERR(di);
3830 goto out;
3831 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003832
3833 leaf = path->nodes[0];
3834 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3835 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3836 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003837 if (ret) {
3838 btrfs_abort_transaction(trans, root, ret);
3839 goto out;
3840 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003841 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003842
3843 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3844 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003845 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003846 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003847 if (ret != -ENOENT) {
3848 btrfs_abort_transaction(trans, root, ret);
3849 goto out;
3850 }
Li Zefan33345d012011-04-20 10:31:50 +08003851 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003852 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003853 if (IS_ERR_OR_NULL(di)) {
3854 if (!di)
3855 ret = -ENOENT;
3856 else
3857 ret = PTR_ERR(di);
3858 btrfs_abort_transaction(trans, root, ret);
3859 goto out;
3860 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003861
3862 leaf = path->nodes[0];
3863 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003864 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003865 index = key.offset;
3866 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003867 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003868
Miao Xie16cdcec2011-04-22 18:12:22 +08003869 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003870 if (ret) {
3871 btrfs_abort_transaction(trans, root, ret);
3872 goto out;
3873 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003874
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003875 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003876 inode_inc_iversion(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003877 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Josef Bacik5a24e842012-08-08 10:12:59 -06003878 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003879 if (ret)
3880 btrfs_abort_transaction(trans, root, ret);
3881out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04003882 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003883 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003884}
3885
Chris Mason39279cc2007-06-12 06:35:45 -04003886static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3887{
3888 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003889 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003890 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003891 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04003892
David Sterbab3ae2442012-09-13 16:04:34 -06003893 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04003894 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06003895 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
3896 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04003897
Josef Bacikd52be812013-05-29 14:54:47 -04003898 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003899 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003900 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003901
Li Zefan33345d012011-04-20 10:31:50 +08003902 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003903 err = btrfs_unlink_subvol(trans, root, dir,
3904 BTRFS_I(inode)->location.objectid,
3905 dentry->d_name.name,
3906 dentry->d_name.len);
3907 goto out;
3908 }
3909
Josef Bacik7b128762008-07-24 12:17:14 -04003910 err = btrfs_orphan_add(trans, inode);
3911 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003912 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003913
Chris Mason39279cc2007-06-12 06:35:45 -04003914 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04003915 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3916 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05003917 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04003918 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003919out:
Josef Bacikd52be812013-05-29 14:54:47 -04003920 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003921 btrfs_btree_balance_dirty(root);
Chris Mason39544012007-12-12 14:38:19 -05003922
Chris Mason39279cc2007-06-12 06:35:45 -04003923 return err;
3924}
3925
Chris Mason323ac952008-10-01 19:05:46 -04003926/*
Chris Mason39279cc2007-06-12 06:35:45 -04003927 * this can truncate away extent items, csum items and directory items.
3928 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04003929 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04003930 *
3931 * csum items that cross the new i_size are truncated to the new size
3932 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04003933 *
3934 * min_type is the minimum key type to truncate down to. If set to 0, this
3935 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04003936 */
Yan, Zheng80825102009-11-12 09:35:36 +00003937int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
3938 struct btrfs_root *root,
3939 struct inode *inode,
3940 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003941{
Chris Mason39279cc2007-06-12 06:35:45 -04003942 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003943 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003944 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00003945 struct btrfs_key key;
3946 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04003947 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04003948 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003949 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003950 u64 item_end = 0;
Josef Bacik7f4f6e02013-08-29 16:43:28 -04003951 u64 last_size = (u64)-1;
Yan, Zheng80825102009-11-12 09:35:36 +00003952 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04003953 int found_extent;
3954 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003955 int pending_del_nr = 0;
3956 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04003957 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00003958 int ret;
3959 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08003960 u64 ino = btrfs_ino(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003961
3962 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04003963
Mark Fasheh0eb0e192011-07-12 16:44:10 -07003964 path = btrfs_alloc_path();
3965 if (!path)
3966 return -ENOMEM;
3967 path->reada = -1;
3968
Josef Bacik5dc562c2012-08-17 13:14:17 -04003969 /*
3970 * We want to drop from the next block forward in case this new size is
3971 * not block aligned since we will be keeping the last block of the
3972 * extent just the way it is.
3973 */
Josef Bacik0af3d002010-06-21 14:48:16 -04003974 if (root->ref_cows || root == root->fs_info->tree_root)
Qu Wenruofda28322013-02-26 08:10:22 +00003975 btrfs_drop_extent_cache(inode, ALIGN(new_size,
3976 root->sectorsize), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003977
Miao Xie16cdcec2011-04-22 18:12:22 +08003978 /*
3979 * This function is also used to drop the items in the log tree before
3980 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
3981 * it is used to drop the loged items. So we shouldn't kill the delayed
3982 * items.
3983 */
3984 if (min_type == 0 && root == BTRFS_I(inode)->root)
3985 btrfs_kill_delayed_inode_items(inode);
3986
Li Zefan33345d012011-04-20 10:31:50 +08003987 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04003988 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04003989 key.type = (u8)-1;
3990
Chris Mason85e21ba2008-01-29 15:11:36 -05003991search_again:
Chris Masonb9473432009-03-13 11:00:37 -04003992 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05003993 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00003994 if (ret < 0) {
3995 err = ret;
3996 goto out;
3997 }
Chris Masond3977122009-01-05 21:25:51 -05003998
Chris Mason85e21ba2008-01-29 15:11:36 -05003999 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004000 /* there are no items in the tree for us to truncate, we're
4001 * done
4002 */
Yan, Zheng80825102009-11-12 09:35:36 +00004003 if (path->slots[0] == 0)
4004 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004005 path->slots[0]--;
4006 }
4007
Chris Masond3977122009-01-05 21:25:51 -05004008 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004009 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004010 leaf = path->nodes[0];
4011 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4012 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04004013
Li Zefan33345d012011-04-20 10:31:50 +08004014 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004015 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004016
Chris Mason85e21ba2008-01-29 15:11:36 -05004017 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004018 break;
4019
Chris Mason5f39d392007-10-15 16:14:19 -04004020 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004021 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004022 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004023 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004024 extent_type = btrfs_file_extent_type(leaf, fi);
4025 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004026 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004027 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04004028 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004029 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004030 path->slots[0], fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004031 }
Yan008630c2007-11-07 13:31:09 -05004032 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004033 }
Yan, Zheng80825102009-11-12 09:35:36 +00004034 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004035 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004036 } else {
4037 if (item_end < new_size)
4038 break;
4039 if (found_key.offset >= new_size)
4040 del_item = 1;
4041 else
4042 del_item = 0;
4043 }
Chris Mason39279cc2007-06-12 06:35:45 -04004044 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004045 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004046 if (found_type != BTRFS_EXTENT_DATA_KEY)
4047 goto delete;
4048
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004049 if (del_item)
4050 last_size = found_key.offset;
4051 else
4052 last_size = new_size;
4053
Chris Mason179e29e2007-11-01 11:28:41 -04004054 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004055 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004056 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004057 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004058 u64 orig_num_bytes =
4059 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004060 extent_num_bytes = ALIGN(new_size -
4061 found_key.offset,
4062 root->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004063 btrfs_set_file_extent_num_bytes(leaf, fi,
4064 extent_num_bytes);
4065 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004066 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04004067 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004068 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004069 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004070 } else {
Chris Masondb945352007-10-15 16:15:53 -04004071 extent_num_bytes =
4072 btrfs_file_extent_disk_num_bytes(leaf,
4073 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004074 extent_offset = found_key.offset -
4075 btrfs_file_extent_offset(leaf, fi);
4076
Chris Mason39279cc2007-06-12 06:35:45 -04004077 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004078 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004079 if (extent_start != 0) {
4080 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04004081 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004082 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004083 }
4084 }
Chris Mason90692182008-02-08 13:49:28 -05004085 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004086 /*
4087 * we can't truncate inline items that have had
4088 * special encodings
4089 */
4090 if (!del_item &&
4091 btrfs_file_extent_compression(leaf, fi) == 0 &&
4092 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4093 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004094 u32 size = new_size - found_key.offset;
4095
4096 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004097 inode_sub_bytes(inode, item_end + 1 -
4098 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04004099 }
Chris Mason514ac8a2014-01-03 21:07:00 -08004100
4101 /*
4102 * update the ram bytes to properly reflect
4103 * the new size of our item
4104 */
4105 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
Chris Masone02119d2008-09-05 16:13:11 -04004106 size =
4107 btrfs_file_extent_calc_inline_size(size);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004108 btrfs_truncate_item(root, path, size, 1);
Chris Masone02119d2008-09-05 16:13:11 -04004109 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004110 inode_sub_bytes(inode, item_end + 1 -
4111 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05004112 }
Chris Mason39279cc2007-06-12 06:35:45 -04004113 }
Chris Mason179e29e2007-11-01 11:28:41 -04004114delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004115 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004116 if (!pending_del_nr) {
4117 /* no pending yet, add ourselves */
4118 pending_del_slot = path->slots[0];
4119 pending_del_nr = 1;
4120 } else if (pending_del_nr &&
4121 path->slots[0] + 1 == pending_del_slot) {
4122 /* hop on the pending chunk */
4123 pending_del_nr++;
4124 pending_del_slot = path->slots[0];
4125 } else {
Chris Masond3977122009-01-05 21:25:51 -05004126 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004127 }
Chris Mason39279cc2007-06-12 06:35:45 -04004128 } else {
4129 break;
4130 }
Josef Bacik0af3d002010-06-21 14:48:16 -04004131 if (found_extent && (root->ref_cows ||
4132 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004133 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004134 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004135 extent_num_bytes, 0,
4136 btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02004137 ino, extent_offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004138 BUG_ON(ret);
4139 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004140
Yan, Zheng80825102009-11-12 09:35:36 +00004141 if (found_type == BTRFS_INODE_ITEM_KEY)
4142 break;
4143
4144 if (path->slots[0] == 0 ||
4145 path->slots[0] != pending_del_slot) {
Yan, Zheng80825102009-11-12 09:35:36 +00004146 if (pending_del_nr) {
4147 ret = btrfs_del_items(trans, root, path,
4148 pending_del_slot,
4149 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004150 if (ret) {
4151 btrfs_abort_transaction(trans,
4152 root, ret);
4153 goto error;
4154 }
Yan, Zheng80825102009-11-12 09:35:36 +00004155 pending_del_nr = 0;
4156 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004157 btrfs_release_path(path);
Chris Mason85e21ba2008-01-29 15:11:36 -05004158 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004159 } else {
4160 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004161 }
Chris Mason39279cc2007-06-12 06:35:45 -04004162 }
Yan, Zheng80825102009-11-12 09:35:36 +00004163out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004164 if (pending_del_nr) {
4165 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4166 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004167 if (ret)
4168 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004169 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004170error:
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004171 if (last_size != (u64)-1)
4172 btrfs_ordered_update_i_size(inode, last_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004173 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00004174 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004175}
4176
Chris Masona52d9a82007-08-27 16:49:44 -04004177/*
Josef Bacik2aaa6652012-08-29 14:27:18 -04004178 * btrfs_truncate_page - read, zero a chunk and write a page
4179 * @inode - inode that we're zeroing
4180 * @from - the offset to start zeroing
4181 * @len - the length to zero, 0 to zero the entire range respective to the
4182 * offset
4183 * @front - zero up to the offset instead of from the offset on
4184 *
4185 * This will find the page for the "from" offset and cow the page and zero the
4186 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004187 */
Josef Bacik2aaa6652012-08-29 14:27:18 -04004188int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
4189 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004190{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004191 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04004192 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004193 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4194 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004195 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004196 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04004197 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04004198 pgoff_t index = from >> PAGE_CACHE_SHIFT;
4199 unsigned offset = from & (PAGE_CACHE_SIZE-1);
4200 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004201 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004202 int ret = 0;
4203 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004204 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004205
Josef Bacik2aaa6652012-08-29 14:27:18 -04004206 if ((offset & (blocksize - 1)) == 0 &&
4207 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004208 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004209 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004210 if (ret)
4211 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004212
Chris Mason211c17f2008-05-15 09:13:45 -04004213again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004214 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004215 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004216 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Miao Xieac6a2b32012-12-05 10:56:13 +00004217 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004218 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004219 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004220
4221 page_start = page_offset(page);
4222 page_end = page_start + PAGE_CACHE_SIZE - 1;
4223
Chris Masona52d9a82007-08-27 16:49:44 -04004224 if (!PageUptodate(page)) {
4225 ret = btrfs_readpage(NULL, page);
4226 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004227 if (page->mapping != mapping) {
4228 unlock_page(page);
4229 page_cache_release(page);
4230 goto again;
4231 }
Chris Masona52d9a82007-08-27 16:49:44 -04004232 if (!PageUptodate(page)) {
4233 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004234 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004235 }
4236 }
Chris Mason211c17f2008-05-15 09:13:45 -04004237 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004238
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004239 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004240 set_page_extent_mapped(page);
4241
4242 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4243 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004244 unlock_extent_cached(io_tree, page_start, page_end,
4245 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004246 unlock_page(page);
4247 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004248 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004249 btrfs_put_ordered_extent(ordered);
4250 goto again;
4251 }
4252
Josef Bacik2ac55d42010-02-03 19:33:23 +00004253 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004254 EXTENT_DIRTY | EXTENT_DELALLOC |
4255 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004256 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004257
Josef Bacik2ac55d42010-02-03 19:33:23 +00004258 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
4259 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004260 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004261 unlock_extent_cached(io_tree, page_start, page_end,
4262 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004263 goto out_unlock;
4264 }
4265
Chris Masone6dcd2d2008-07-17 12:53:50 -04004266 if (offset != PAGE_CACHE_SIZE) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004267 if (!len)
4268 len = PAGE_CACHE_SIZE - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004269 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004270 if (front)
4271 memset(kaddr, 0, offset);
4272 else
4273 memset(kaddr + offset, 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004274 flush_dcache_page(page);
4275 kunmap(page);
4276 }
Chris Mason247e7432008-07-17 12:53:51 -04004277 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004278 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004279 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
4280 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004281
Chris Mason89642222008-07-24 09:41:53 -04004282out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004283 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004284 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04004285 unlock_page(page);
4286 page_cache_release(page);
4287out:
4288 return ret;
4289}
4290
Josef Bacik16e75492013-10-22 12:18:51 -04004291static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4292 u64 offset, u64 len)
4293{
4294 struct btrfs_trans_handle *trans;
4295 int ret;
4296
4297 /*
4298 * Still need to make sure the inode looks like it's been updated so
4299 * that any holes get logged if we fsync.
4300 */
4301 if (btrfs_fs_incompat(root->fs_info, NO_HOLES)) {
4302 BTRFS_I(inode)->last_trans = root->fs_info->generation;
4303 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4304 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4305 return 0;
4306 }
4307
4308 /*
4309 * 1 - for the one we're dropping
4310 * 1 - for the one we're adding
4311 * 1 - for updating the inode.
4312 */
4313 trans = btrfs_start_transaction(root, 3);
4314 if (IS_ERR(trans))
4315 return PTR_ERR(trans);
4316
4317 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4318 if (ret) {
4319 btrfs_abort_transaction(trans, root, ret);
4320 btrfs_end_transaction(trans, root);
4321 return ret;
4322 }
4323
4324 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset,
4325 0, 0, len, 0, len, 0, 0, 0);
4326 if (ret)
4327 btrfs_abort_transaction(trans, root, ret);
4328 else
4329 btrfs_update_inode(trans, root, inode);
4330 btrfs_end_transaction(trans, root);
4331 return ret;
4332}
4333
Josef Bacik695a0d02011-03-04 15:46:53 -05004334/*
4335 * This function puts in dummy file extents for the area we're creating a hole
4336 * for. So if we are truncating this file to a larger size we need to insert
4337 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4338 * the range between oldsize and size
4339 */
Josef Bacika41ad392011-01-31 15:30:16 -05004340int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004341{
Yan Zheng9036c102008-10-30 14:19:41 -04004342 struct btrfs_root *root = BTRFS_I(inode)->root;
4343 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004344 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004345 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004346 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Qu Wenruofda28322013-02-26 08:10:22 +00004347 u64 hole_start = ALIGN(oldsize, root->sectorsize);
4348 u64 block_end = ALIGN(size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004349 u64 last_byte;
4350 u64 cur_offset;
4351 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004352 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004353
Josef Bacika71754f2013-06-17 17:14:39 -04004354 /*
4355 * If our size started in the middle of a page we need to zero out the
4356 * rest of the page before we expand the i_size, otherwise we could
4357 * expose stale data.
4358 */
4359 err = btrfs_truncate_page(inode, oldsize, 0, 0);
4360 if (err)
4361 return err;
4362
Yan Zheng9036c102008-10-30 14:19:41 -04004363 if (size <= hole_start)
4364 return 0;
4365
Yan Zheng9036c102008-10-30 14:19:41 -04004366 while (1) {
4367 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004368
Josef Bacik2ac55d42010-02-03 19:33:23 +00004369 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004370 &cached_state);
Miao Xiefa7c1492013-09-26 13:15:27 +08004371 ordered = btrfs_lookup_ordered_range(inode, hole_start,
4372 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004373 if (!ordered)
4374 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004375 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4376 &cached_state, GFP_NOFS);
Miao Xiefa7c1492013-09-26 13:15:27 +08004377 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004378 btrfs_put_ordered_extent(ordered);
4379 }
4380
Yan Zheng9036c102008-10-30 14:19:41 -04004381 cur_offset = hole_start;
4382 while (1) {
4383 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4384 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004385 if (IS_ERR(em)) {
4386 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004387 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004388 break;
4389 }
Yan Zheng9036c102008-10-30 14:19:41 -04004390 last_byte = min(extent_map_end(em), block_end);
Qu Wenruofda28322013-02-26 08:10:22 +00004391 last_byte = ALIGN(last_byte , root->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004392 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004393 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004394 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004395
Josef Bacik16e75492013-10-22 12:18:51 -04004396 err = maybe_insert_hole(root, inode, cur_offset,
4397 hole_size);
4398 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004399 break;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004400 btrfs_drop_extent_cache(inode, cur_offset,
4401 cur_offset + hole_size - 1, 0);
4402 hole_em = alloc_extent_map();
4403 if (!hole_em) {
4404 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4405 &BTRFS_I(inode)->runtime_flags);
4406 goto next;
4407 }
4408 hole_em->start = cur_offset;
4409 hole_em->len = hole_size;
4410 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004411
Josef Bacik5dc562c2012-08-17 13:14:17 -04004412 hole_em->block_start = EXTENT_MAP_HOLE;
4413 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004414 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004415 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004416 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
4417 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Josef Bacik16e75492013-10-22 12:18:51 -04004418 hole_em->generation = root->fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004419
4420 while (1) {
4421 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004422 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004423 write_unlock(&em_tree->lock);
4424 if (err != -EEXIST)
4425 break;
4426 btrfs_drop_extent_cache(inode, cur_offset,
4427 cur_offset +
4428 hole_size - 1, 0);
4429 }
4430 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04004431 }
Josef Bacik16e75492013-10-22 12:18:51 -04004432next:
Yan Zheng9036c102008-10-30 14:19:41 -04004433 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004434 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004435 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004436 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004437 break;
4438 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004439 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004440 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4441 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004442 return err;
4443}
4444
Eric Sandeen3972f262013-01-12 02:57:22 +00004445static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004446{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004447 struct btrfs_root *root = BTRFS_I(inode)->root;
4448 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004449 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004450 loff_t newsize = attr->ia_size;
4451 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004452 int ret;
4453
Eric Sandeen3972f262013-01-12 02:57:22 +00004454 /*
4455 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4456 * special case where we need to update the times despite not having
4457 * these flags set. For all other operations the VFS set these flags
4458 * explicitly if it wants a timestamp update.
4459 */
4460 if (newsize != oldsize && (!(mask & (ATTR_CTIME | ATTR_MTIME))))
4461 inode->i_ctime = inode->i_mtime = current_fs_time(inode->i_sb);
4462
Josef Bacika41ad392011-01-31 15:30:16 -05004463 if (newsize > oldsize) {
Kirill A. Shutemov7caef262013-09-12 15:13:56 -07004464 truncate_pagecache(inode, newsize);
Josef Bacika41ad392011-01-31 15:30:16 -05004465 ret = btrfs_cont_expand(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004466 if (ret)
Yan, Zheng80825102009-11-12 09:35:36 +00004467 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004468
Miao Xief4a2f4c2011-12-14 20:12:01 -05004469 trans = btrfs_start_transaction(root, 1);
4470 if (IS_ERR(trans))
4471 return PTR_ERR(trans);
4472
4473 i_size_write(inode, newsize);
4474 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
4475 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004476 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05004477 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004478
Josef Bacika41ad392011-01-31 15:30:16 -05004479 /*
4480 * We're truncating a file that used to have good data down to
4481 * zero. Make sure it gets into the ordered flush list so that
4482 * any new writes get down to disk quickly.
4483 */
4484 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004485 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4486 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004487
Josef Bacikf3fe8202013-01-07 17:03:21 -05004488 /*
4489 * 1 for the orphan item we're going to add
4490 * 1 for the orphan item deletion.
4491 */
4492 trans = btrfs_start_transaction(root, 2);
4493 if (IS_ERR(trans))
4494 return PTR_ERR(trans);
4495
4496 /*
4497 * We need to do this in case we fail at _any_ point during the
4498 * actual truncate. Once we do the truncate_setsize we could
4499 * invalidate pages which forces any outstanding ordered io to
4500 * be instantly completed which will give us extents that need
4501 * to be truncated. If we fail to get an orphan inode down we
4502 * could have left over extents that were never meant to live,
4503 * so we need to garuntee from this point on that everything
4504 * will be consistent.
4505 */
4506 ret = btrfs_orphan_add(trans, inode);
4507 btrfs_end_transaction(trans, root);
4508 if (ret)
4509 return ret;
4510
Josef Bacika41ad392011-01-31 15:30:16 -05004511 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4512 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004513
4514 /* Disable nonlocked read DIO to avoid the end less truncate */
4515 btrfs_inode_block_unlocked_dio(inode);
4516 inode_dio_wait(inode);
4517 btrfs_inode_resume_unlocked_dio(inode);
4518
Josef Bacika41ad392011-01-31 15:30:16 -05004519 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004520 if (ret && inode->i_nlink) {
4521 int err;
4522
4523 /*
4524 * failed to truncate, disk_i_size is only adjusted down
4525 * as we remove extents, so it should represent the true
4526 * size of the inode, so reset the in memory size and
4527 * delete our orphan entry.
4528 */
4529 trans = btrfs_join_transaction(root);
4530 if (IS_ERR(trans)) {
4531 btrfs_orphan_del(NULL, inode);
4532 return ret;
4533 }
4534 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
4535 err = btrfs_orphan_del(trans, inode);
4536 if (err)
4537 btrfs_abort_transaction(trans, root, err);
4538 btrfs_end_transaction(trans, root);
4539 }
Yan, Zheng80825102009-11-12 09:35:36 +00004540 }
4541
Josef Bacika41ad392011-01-31 15:30:16 -05004542 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004543}
4544
Chris Mason39279cc2007-06-12 06:35:45 -04004545static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
4546{
4547 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08004548 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004549 int err;
4550
Li Zefanb83cc962010-12-20 16:04:08 +08004551 if (btrfs_root_readonly(root))
4552 return -EROFS;
4553
Chris Mason39279cc2007-06-12 06:35:45 -04004554 err = inode_change_ok(inode, attr);
4555 if (err)
4556 return err;
4557
Chris Mason5a3f23d2009-03-31 13:27:11 -04004558 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00004559 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00004560 if (err)
4561 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004562 }
Yan Zheng9036c102008-10-30 14:19:41 -04004563
Christoph Hellwig10257742010-06-04 11:30:02 +02004564 if (attr->ia_valid) {
4565 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004566 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004567 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04004568
Josef Bacik22c44fe2011-11-30 10:45:38 -05004569 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig10257742010-06-04 11:30:02 +02004570 err = btrfs_acl_chmod(inode);
4571 }
4572
Chris Mason39279cc2007-06-12 06:35:45 -04004573 return err;
4574}
Chris Mason61295eb2008-01-14 16:24:38 -05004575
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004576/*
4577 * While truncating the inode pages during eviction, we get the VFS calling
4578 * btrfs_invalidatepage() against each page of the inode. This is slow because
4579 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
4580 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
4581 * extent_state structures over and over, wasting lots of time.
4582 *
4583 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
4584 * those expensive operations on a per page basis and do only the ordered io
4585 * finishing, while we release here the extent_map and extent_state structures,
4586 * without the excessive merging and splitting.
4587 */
4588static void evict_inode_truncate_pages(struct inode *inode)
4589{
4590 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4591 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
4592 struct rb_node *node;
4593
4594 ASSERT(inode->i_state & I_FREEING);
4595 truncate_inode_pages(&inode->i_data, 0);
4596
4597 write_lock(&map_tree->lock);
4598 while (!RB_EMPTY_ROOT(&map_tree->map)) {
4599 struct extent_map *em;
4600
4601 node = rb_first(&map_tree->map);
4602 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08004603 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
4604 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004605 remove_extent_mapping(map_tree, em);
4606 free_extent_map(em);
4607 }
4608 write_unlock(&map_tree->lock);
4609
4610 spin_lock(&io_tree->lock);
4611 while (!RB_EMPTY_ROOT(&io_tree->state)) {
4612 struct extent_state *state;
4613 struct extent_state *cached_state = NULL;
4614
4615 node = rb_first(&io_tree->state);
4616 state = rb_entry(node, struct extent_state, rb_node);
4617 atomic_inc(&state->refs);
4618 spin_unlock(&io_tree->lock);
4619
4620 lock_extent_bits(io_tree, state->start, state->end,
4621 0, &cached_state);
4622 clear_extent_bit(io_tree, state->start, state->end,
4623 EXTENT_LOCKED | EXTENT_DIRTY |
4624 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
4625 EXTENT_DEFRAG, 1, 1,
4626 &cached_state, GFP_NOFS);
4627 free_extent_state(state);
4628
4629 spin_lock(&io_tree->lock);
4630 }
4631 spin_unlock(&io_tree->lock);
4632}
4633
Al Virobd555972010-06-07 11:35:40 -04004634void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004635{
4636 struct btrfs_trans_handle *trans;
4637 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04004638 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik07127182011-08-19 10:29:59 -04004639 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04004640 int ret;
4641
liubo1abe9b82011-03-24 11:18:59 +00004642 trace_btrfs_inode_evict(inode);
4643
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004644 evict_inode_truncate_pages(inode);
4645
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02004646 if (inode->i_nlink &&
4647 ((btrfs_root_refs(&root->root_item) != 0 &&
4648 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
4649 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04004650 goto no_delete;
4651
Chris Mason39279cc2007-06-12 06:35:45 -04004652 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04004653 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004654 goto no_delete;
4655 }
Al Virobd555972010-06-07 11:35:40 -04004656 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04004657 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04004658
Yan, Zhengc71bf092009-11-12 09:34:40 +00004659 if (root->fs_info->log_root_recovering) {
Liu Bo6bf02312012-06-25 21:59:09 -06004660 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04004661 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00004662 goto no_delete;
4663 }
4664
Yan, Zheng76dda932009-09-21 16:00:26 -04004665 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02004666 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
4667 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04004668 goto no_delete;
4669 }
4670
Miao Xie0e8c36a2012-12-19 06:59:51 +00004671 ret = btrfs_commit_inode_delayed_inode(inode);
4672 if (ret) {
4673 btrfs_orphan_del(NULL, inode);
4674 goto no_delete;
4675 }
4676
Miao Xie66d8f3d2012-09-06 04:02:28 -06004677 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04004678 if (!rsv) {
4679 btrfs_orphan_del(NULL, inode);
4680 goto no_delete;
4681 }
Josef Bacik4a338542011-08-29 11:01:31 -04004682 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04004683 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04004684 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04004685
Chris Masondbe674a2008-07-17 12:54:05 -04004686 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04004687
Josef Bacik4289a662011-08-05 13:22:24 -04004688 /*
Miao Xie8407aa42012-09-07 01:43:32 -06004689 * This is a bit simpler than btrfs_truncate since we've already
4690 * reserved our space for our orphan item in the unlink, so we just
4691 * need to reserve some slack space in case we add bytes and update
4692 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04004693 */
Yan, Zheng80825102009-11-12 09:35:36 +00004694 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00004695 ret = btrfs_block_rsv_refill(root, rsv, min_size,
4696 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00004697
Josef Bacik726c35f2011-09-26 15:46:06 -04004698 /*
4699 * Try and steal from the global reserve since we will
4700 * likely not use this space anyway, we want to try as
4701 * hard as possible to get this to work.
4702 */
4703 if (ret)
4704 ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
4705
Yan, Zhengd68fc572010-05-16 10:49:58 -04004706 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004707 btrfs_warn(root->fs_info,
4708 "Could not get space for a delete, will truncate on mount %d",
4709 ret);
Josef Bacik4289a662011-08-05 13:22:24 -04004710 btrfs_orphan_del(NULL, inode);
4711 btrfs_free_block_rsv(root, rsv);
4712 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04004713 }
4714
Miao Xie0e8c36a2012-12-19 06:59:51 +00004715 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04004716 if (IS_ERR(trans)) {
4717 btrfs_orphan_del(NULL, inode);
4718 btrfs_free_block_rsv(root, rsv);
4719 goto no_delete;
4720 }
4721
4722 trans->block_rsv = rsv;
4723
Yan, Zhengd68fc572010-05-16 10:49:58 -04004724 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04004725 if (ret != -ENOSPC)
Yan, Zheng80825102009-11-12 09:35:36 +00004726 break;
4727
Miao Xie8407aa42012-09-07 01:43:32 -06004728 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004729 btrfs_end_transaction(trans, root);
4730 trans = NULL;
Liu Bob53d3f52012-11-14 14:34:34 +00004731 btrfs_btree_balance_dirty(root);
Josef Bacik7b128762008-07-24 12:17:14 -04004732 }
4733
Josef Bacik4289a662011-08-05 13:22:24 -04004734 btrfs_free_block_rsv(root, rsv);
4735
Josef Bacik4ef31a42013-08-13 14:10:08 -04004736 /*
4737 * Errors here aren't a big deal, it just means we leave orphan items
4738 * in the tree. They will be cleaned up on the next mount.
4739 */
Yan, Zheng80825102009-11-12 09:35:36 +00004740 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04004741 trans->block_rsv = root->orphan_block_rsv;
Josef Bacik4ef31a42013-08-13 14:10:08 -04004742 btrfs_orphan_del(trans, inode);
4743 } else {
4744 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004745 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004746
Josef Bacik4289a662011-08-05 13:22:24 -04004747 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08004748 if (!(root == root->fs_info->tree_root ||
4749 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08004750 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08004751
Chris Mason54aa1f42007-06-22 14:16:25 -04004752 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004753 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04004754no_delete:
Miao Xie89042e52013-05-15 07:48:15 +00004755 btrfs_remove_delayed_node(inode);
Jan Karadbd57682012-05-03 14:48:02 +02004756 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004757 return;
Chris Mason39279cc2007-06-12 06:35:45 -04004758}
4759
4760/*
4761 * this returns the key found in the dir entry in the location pointer.
4762 * If no dir entries were found, location->objectid is 0.
4763 */
4764static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
4765 struct btrfs_key *location)
4766{
4767 const char *name = dentry->d_name.name;
4768 int namelen = dentry->d_name.len;
4769 struct btrfs_dir_item *di;
4770 struct btrfs_path *path;
4771 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04004772 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004773
4774 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07004775 if (!path)
4776 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05004777
Li Zefan33345d012011-04-20 10:31:50 +08004778 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04004779 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04004780 if (IS_ERR(di))
4781 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05004782
David Sterbac7040052011-04-19 18:00:01 +02004783 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05004784 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05004785
Chris Mason5f39d392007-10-15 16:14:19 -04004786 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04004787out:
Chris Mason39279cc2007-06-12 06:35:45 -04004788 btrfs_free_path(path);
4789 return ret;
Chris Mason39544012007-12-12 14:38:19 -05004790out_err:
4791 location->objectid = 0;
4792 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04004793}
4794
4795/*
4796 * when we hit a tree root in a directory, the btrfs part of the inode
4797 * needs to be changed to reflect the root directory of the tree root. This
4798 * is kind of like crossing a mount point.
4799 */
4800static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004801 struct inode *dir,
4802 struct dentry *dentry,
4803 struct btrfs_key *location,
4804 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04004805{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004806 struct btrfs_path *path;
4807 struct btrfs_root *new_root;
4808 struct btrfs_root_ref *ref;
4809 struct extent_buffer *leaf;
4810 int ret;
4811 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004812
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004813 path = btrfs_alloc_path();
4814 if (!path) {
4815 err = -ENOMEM;
4816 goto out;
4817 }
Chris Mason39279cc2007-06-12 06:35:45 -04004818
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004819 err = -ENOENT;
Kelley Nielsen75ac2dd2013-11-04 19:35:58 -08004820 ret = btrfs_find_item(root->fs_info->tree_root, path,
4821 BTRFS_I(dir)->root->root_key.objectid,
4822 location->objectid, BTRFS_ROOT_REF_KEY, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004823 if (ret) {
4824 if (ret < 0)
4825 err = ret;
4826 goto out;
4827 }
Chris Mason39279cc2007-06-12 06:35:45 -04004828
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004829 leaf = path->nodes[0];
4830 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08004831 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004832 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
4833 goto out;
4834
4835 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
4836 (unsigned long)(ref + 1),
4837 dentry->d_name.len);
4838 if (ret)
4839 goto out;
4840
David Sterbab3b4aa72011-04-21 01:20:15 +02004841 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004842
4843 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
4844 if (IS_ERR(new_root)) {
4845 err = PTR_ERR(new_root);
4846 goto out;
4847 }
4848
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004849 *sub_root = new_root;
4850 location->objectid = btrfs_root_dirid(&new_root->root_item);
4851 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04004852 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004853 err = 0;
4854out:
4855 btrfs_free_path(path);
4856 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004857}
4858
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004859static void inode_tree_add(struct inode *inode)
4860{
4861 struct btrfs_root *root = BTRFS_I(inode)->root;
4862 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004863 struct rb_node **p;
4864 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01004865 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Li Zefan33345d012011-04-20 10:31:50 +08004866 u64 ino = btrfs_ino(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004867
Al Viro1d3382cb2010-10-23 15:19:20 -04004868 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004869 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00004870 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004871 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00004872 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004873 while (*p) {
4874 parent = *p;
4875 entry = rb_entry(parent, struct btrfs_inode, rb_node);
4876
Li Zefan33345d012011-04-20 10:31:50 +08004877 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004878 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004879 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004880 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004881 else {
4882 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04004883 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01004884 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004885 RB_CLEAR_NODE(parent);
4886 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01004887 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004888 }
4889 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01004890 rb_link_node(new, parent, p);
4891 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004892 spin_unlock(&root->inode_lock);
4893}
4894
4895static void inode_tree_del(struct inode *inode)
4896{
4897 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04004898 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004899
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004900 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004901 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004902 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004903 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04004904 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004905 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004906 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04004907
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02004908 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004909 synchronize_srcu(&root->fs_info->subvol_srcu);
4910 spin_lock(&root->inode_lock);
4911 empty = RB_EMPTY_ROOT(&root->inode_tree);
4912 spin_unlock(&root->inode_lock);
4913 if (empty)
4914 btrfs_add_dead_root(root);
4915 }
4916}
4917
Jeff Mahoney143bede2012-03-01 14:56:26 +01004918void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04004919{
4920 struct rb_node *node;
4921 struct rb_node *prev;
4922 struct btrfs_inode *entry;
4923 struct inode *inode;
4924 u64 objectid = 0;
4925
Liu Bo7813b3d2014-02-10 17:37:25 +08004926 if (!test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
4927 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04004928
4929 spin_lock(&root->inode_lock);
4930again:
4931 node = root->inode_tree.rb_node;
4932 prev = NULL;
4933 while (node) {
4934 prev = node;
4935 entry = rb_entry(node, struct btrfs_inode, rb_node);
4936
Li Zefan33345d012011-04-20 10:31:50 +08004937 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004938 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004939 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004940 node = node->rb_right;
4941 else
4942 break;
4943 }
4944 if (!node) {
4945 while (prev) {
4946 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004947 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004948 node = prev;
4949 break;
4950 }
4951 prev = rb_next(prev);
4952 }
4953 }
4954 while (node) {
4955 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004956 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04004957 inode = igrab(&entry->vfs_inode);
4958 if (inode) {
4959 spin_unlock(&root->inode_lock);
4960 if (atomic_read(&inode->i_count) > 1)
4961 d_prune_aliases(inode);
4962 /*
Al Viro45321ac2010-06-07 13:43:19 -04004963 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04004964 * the inode cache when its usage count
4965 * hits zero.
4966 */
4967 iput(inode);
4968 cond_resched();
4969 spin_lock(&root->inode_lock);
4970 goto again;
4971 }
4972
4973 if (cond_resched_lock(&root->inode_lock))
4974 goto again;
4975
4976 node = rb_next(node);
4977 }
4978 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004979}
4980
Chris Masone02119d2008-09-05 16:13:11 -04004981static int btrfs_init_locked_inode(struct inode *inode, void *p)
4982{
4983 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08004984 inode->i_ino = args->location->objectid;
4985 memcpy(&BTRFS_I(inode)->location, args->location,
4986 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04004987 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004988 return 0;
4989}
4990
4991static int btrfs_find_actor(struct inode *inode, void *opaque)
4992{
4993 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08004994 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05004995 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004996}
4997
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004998static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08004999 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005000 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005001{
5002 struct inode *inode;
5003 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005004 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005005
Chris Mason90d3e592014-01-09 17:28:00 -08005006 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005007 args.root = root;
5008
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005009 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005010 btrfs_init_locked_inode,
5011 (void *)&args);
5012 return inode;
5013}
5014
Balaji Rao1a54ef82008-07-21 02:01:04 +05305015/* Get an inode object given its location and corresponding root.
5016 * Returns in *is_new if the inode was read from disk
5017 */
5018struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005019 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305020{
5021 struct inode *inode;
5022
Chris Mason90d3e592014-01-09 17:28:00 -08005023 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305024 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005025 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305026
5027 if (inode->i_state & I_NEW) {
Balaji Rao1a54ef82008-07-21 02:01:04 +05305028 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005029 if (!is_bad_inode(inode)) {
5030 inode_tree_add(inode);
5031 unlock_new_inode(inode);
5032 if (new)
5033 *new = 1;
5034 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005035 unlock_new_inode(inode);
5036 iput(inode);
5037 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005038 }
5039 }
5040
Balaji Rao1a54ef82008-07-21 02:01:04 +05305041 return inode;
5042}
5043
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005044static struct inode *new_simple_dir(struct super_block *s,
5045 struct btrfs_key *key,
5046 struct btrfs_root *root)
5047{
5048 struct inode *inode = new_inode(s);
5049
5050 if (!inode)
5051 return ERR_PTR(-ENOMEM);
5052
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005053 BTRFS_I(inode)->root = root;
5054 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005055 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005056
5057 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005058 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005059 inode->i_fop = &simple_dir_operations;
5060 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
5061 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
5062
5063 return inode;
5064}
5065
Chris Mason3de45862008-11-17 21:02:50 -05005066struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005067{
Chris Masond3977122009-01-05 21:25:51 -05005068 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005069 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005070 struct btrfs_root *sub_root = root;
5071 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005072 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005073 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005074
5075 if (dentry->d_name.len > BTRFS_NAME_LEN)
5076 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005077
Jeff Layton39e3c952012-11-28 11:30:53 -05005078 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005079 if (ret < 0)
5080 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005081
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005082 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005083 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005084
5085 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005086 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005087 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005088 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005089
5090 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5091
Yan, Zheng76dda932009-09-21 16:00:26 -04005092 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005093 ret = fixup_tree_root_location(root, dir, dentry,
5094 &location, &sub_root);
5095 if (ret < 0) {
5096 if (ret != -ENOENT)
5097 inode = ERR_PTR(ret);
5098 else
5099 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5100 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005101 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005102 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005103 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
5104
Julia Lawall34d19ba2011-01-24 19:55:19 +00005105 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00005106 down_read(&root->fs_info->cleanup_work_sem);
5107 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005108 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005109 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005110 if (ret) {
5111 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005112 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005113 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005114 }
5115
Chris Mason3de45862008-11-17 21:02:50 -05005116 return inode;
5117}
5118
Nick Pigginfe15ce42011-01-07 17:49:23 +11005119static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005120{
5121 struct btrfs_root *root;
Li Zefan848cce02012-02-21 17:04:28 +08005122 struct inode *inode = dentry->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005123
Li Zefan848cce02012-02-21 17:04:28 +08005124 if (!inode && !IS_ROOT(dentry))
5125 inode = dentry->d_parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005126
Li Zefan848cce02012-02-21 17:04:28 +08005127 if (inode) {
5128 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005129 if (btrfs_root_refs(&root->root_item) == 0)
5130 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005131
5132 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5133 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005134 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005135 return 0;
5136}
5137
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005138static void btrfs_dentry_release(struct dentry *dentry)
5139{
5140 if (dentry->d_fsdata)
5141 kfree(dentry->d_fsdata);
5142}
5143
Chris Mason3de45862008-11-17 21:02:50 -05005144static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005145 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005146{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005147 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005148
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005149 inode = btrfs_lookup_dentry(dir, dentry);
5150 if (IS_ERR(inode)) {
5151 if (PTR_ERR(inode) == -ENOENT)
5152 inode = NULL;
5153 else
5154 return ERR_CAST(inode);
5155 }
5156
Josef Bacik3a0dfa62014-02-14 13:43:48 -05005157 return d_materialise_unique(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005158}
5159
Miao Xie16cdcec2011-04-22 18:12:22 +08005160unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005161 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5162};
5163
Al Viro9cdda8d2013-05-22 16:48:09 -04005164static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005165{
Al Viro9cdda8d2013-05-22 16:48:09 -04005166 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005167 struct btrfs_root *root = BTRFS_I(inode)->root;
5168 struct btrfs_item *item;
5169 struct btrfs_dir_item *di;
5170 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005171 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005172 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005173 struct list_head ins_list;
5174 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005175 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005176 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005177 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005178 unsigned char d_type;
5179 int over = 0;
5180 u32 di_cur;
5181 u32 di_total;
5182 u32 di_len;
5183 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005184 char tmp_name[32];
5185 char *name_ptr;
5186 int name_len;
Al Viro9cdda8d2013-05-22 16:48:09 -04005187 int is_curr = 0; /* ctx->pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04005188
5189 /* FIXME, use a real flag for deciding about the key type */
5190 if (root->fs_info->tree_root == root)
5191 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005192
Al Viro9cdda8d2013-05-22 16:48:09 -04005193 if (!dir_emit_dots(file, ctx))
5194 return 0;
5195
David Woodhouse49593bf2008-08-17 17:08:36 +01005196 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005197 if (!path)
5198 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005199
Josef Bacik026fd312011-05-13 10:32:11 -04005200 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005201
Miao Xie16cdcec2011-04-22 18:12:22 +08005202 if (key_type == BTRFS_DIR_INDEX_KEY) {
5203 INIT_LIST_HEAD(&ins_list);
5204 INIT_LIST_HEAD(&del_list);
5205 btrfs_get_delayed_items(inode, &ins_list, &del_list);
5206 }
5207
Chris Mason39279cc2007-06-12 06:35:45 -04005208 btrfs_set_key_type(&key, key_type);
Al Viro9cdda8d2013-05-22 16:48:09 -04005209 key.offset = ctx->pos;
Li Zefan33345d012011-04-20 10:31:50 +08005210 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005211
Chris Mason39279cc2007-06-12 06:35:45 -04005212 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5213 if (ret < 0)
5214 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005215
5216 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005217 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005218 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005219 if (slot >= btrfs_header_nritems(leaf)) {
5220 ret = btrfs_next_leaf(root, path);
5221 if (ret < 0)
5222 goto err;
5223 else if (ret > 0)
5224 break;
5225 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005226 }
Chris Mason3de45862008-11-17 21:02:50 -05005227
Ross Kirkdd3cc162013-09-16 15:58:09 +01005228 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04005229 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5230
5231 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005232 break;
Chris Mason5f39d392007-10-15 16:14:19 -04005233 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04005234 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005235 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005236 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08005237 if (key_type == BTRFS_DIR_INDEX_KEY &&
5238 btrfs_should_delete_dir_index(&del_list,
5239 found_key.offset))
5240 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005241
Al Viro9cdda8d2013-05-22 16:48:09 -04005242 ctx->pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08005243 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005244
Chris Mason39279cc2007-06-12 06:35:45 -04005245 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
5246 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005247 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01005248
5249 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04005250 struct btrfs_key location;
5251
Josef Bacik22a94d42011-03-16 16:47:17 -04005252 if (verify_dir_item(root, leaf, di))
5253 break;
5254
Chris Mason5f39d392007-10-15 16:14:19 -04005255 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01005256 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04005257 name_ptr = tmp_name;
5258 } else {
5259 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01005260 if (!name_ptr) {
5261 ret = -ENOMEM;
5262 goto err;
5263 }
Chris Mason5f39d392007-10-15 16:14:19 -04005264 }
5265 read_extent_buffer(leaf, name_ptr,
5266 (unsigned long)(di + 1), name_len);
5267
5268 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5269 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05005270
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005271
Chris Mason3de45862008-11-17 21:02:50 -05005272 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01005273 * skip it.
5274 *
5275 * In contrast to old kernels, we insert the snapshot's
5276 * dir item and dir index after it has been created, so
5277 * we won't find a reference to our own snapshot. We
5278 * still keep the following code for backward
5279 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05005280 */
5281 if (location.type == BTRFS_ROOT_ITEM_KEY &&
5282 location.objectid == root->root_key.objectid) {
5283 over = 0;
5284 goto skip;
5285 }
Al Viro9cdda8d2013-05-22 16:48:09 -04005286 over = !dir_emit(ctx, name_ptr, name_len,
5287 location.objectid, d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04005288
Chris Mason3de45862008-11-17 21:02:50 -05005289skip:
Chris Mason5f39d392007-10-15 16:14:19 -04005290 if (name_ptr != tmp_name)
5291 kfree(name_ptr);
5292
Chris Mason39279cc2007-06-12 06:35:45 -04005293 if (over)
5294 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05005295 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01005296 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04005297 di_cur += di_len;
5298 di = (struct btrfs_dir_item *)((char *)di + di_len);
5299 }
Li Zefanb9e03af2011-03-23 10:43:58 +08005300next:
5301 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005302 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005303
Miao Xie16cdcec2011-04-22 18:12:22 +08005304 if (key_type == BTRFS_DIR_INDEX_KEY) {
5305 if (is_curr)
Al Viro9cdda8d2013-05-22 16:48:09 -04005306 ctx->pos++;
5307 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005308 if (ret)
5309 goto nopos;
5310 }
5311
David Woodhouse49593bf2008-08-17 17:08:36 +01005312 /* Reached end of directory/root. Bump pos past the last item. */
Zach Browndb62efb2013-07-11 16:19:42 -07005313 ctx->pos++;
5314
5315 /*
5316 * Stop new entries from being returned after we return the last
5317 * entry.
5318 *
5319 * New directory entries are assigned a strictly increasing
5320 * offset. This means that new entries created during readdir
5321 * are *guaranteed* to be seen in the future by that readdir.
5322 * This has broken buggy programs which operate on names as
5323 * they're returned by readdir. Until we re-use freed offsets
5324 * we have this hack to stop new entries from being returned
5325 * under the assumption that they'll never reach this huge
5326 * offset.
5327 *
5328 * This is being careful not to overflow 32bit loff_t unless the
5329 * last entry requires it because doing so has broken 32bit apps
5330 * in the past.
5331 */
5332 if (key_type == BTRFS_DIR_INDEX_KEY) {
5333 if (ctx->pos >= INT_MAX)
5334 ctx->pos = LLONG_MAX;
5335 else
5336 ctx->pos = INT_MAX;
5337 }
Chris Mason39279cc2007-06-12 06:35:45 -04005338nopos:
5339 ret = 0;
5340err:
Miao Xie16cdcec2011-04-22 18:12:22 +08005341 if (key_type == BTRFS_DIR_INDEX_KEY)
5342 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005343 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005344 return ret;
5345}
5346
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005347int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005348{
5349 struct btrfs_root *root = BTRFS_I(inode)->root;
5350 struct btrfs_trans_handle *trans;
5351 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005352 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005353
Josef Bacik72ac3c02012-05-23 14:13:11 -04005354 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005355 return 0;
5356
Liu Bo83eea1f2012-07-10 05:28:39 -06005357 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08005358 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005359
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005360 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005361 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005362 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005363 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005364 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005365 if (IS_ERR(trans))
5366 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06005367 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005368 }
5369 return ret;
5370}
5371
5372/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005373 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005374 * inode changes. But, it is most likely to find the inode in cache.
5375 * FIXME, needs more benchmarking...there are no reasons other than performance
5376 * to keep or drop this code.
5377 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005378static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005379{
5380 struct btrfs_root *root = BTRFS_I(inode)->root;
5381 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005382 int ret;
5383
Josef Bacik72ac3c02012-05-23 14:13:11 -04005384 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005385 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005386
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005387 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005388 if (IS_ERR(trans))
5389 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005390
5391 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005392 if (ret && ret == -ENOSPC) {
5393 /* whoops, lets try again with the full transaction */
5394 btrfs_end_transaction(trans, root);
5395 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005396 if (IS_ERR(trans))
5397 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005398
Chris Mason94b60442010-05-26 11:02:00 -04005399 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005400 }
Chris Mason39279cc2007-06-12 06:35:45 -04005401 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08005402 if (BTRFS_I(inode)->delayed_node)
5403 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005404
5405 return ret;
5406}
5407
5408/*
5409 * This is a copy of file_update_time. We need this so we can return error on
5410 * ENOSPC for updating the inode in the case of file write and mmap writes.
5411 */
Josef Bacike41f9412012-03-26 09:46:47 -04005412static int btrfs_update_time(struct inode *inode, struct timespec *now,
5413 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005414{
Alexander Block2bc5565282012-06-15 09:49:33 +02005415 struct btrfs_root *root = BTRFS_I(inode)->root;
5416
5417 if (btrfs_root_readonly(root))
5418 return -EROFS;
5419
Josef Bacike41f9412012-03-26 09:46:47 -04005420 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005421 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04005422 if (flags & S_CTIME)
5423 inode->i_ctime = *now;
5424 if (flags & S_MTIME)
5425 inode->i_mtime = *now;
5426 if (flags & S_ATIME)
5427 inode->i_atime = *now;
5428 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005429}
5430
Chris Masond352ac62008-09-29 15:18:18 -04005431/*
5432 * find the highest existing sequence number in a directory
5433 * and then set the in-memory index_cnt variable to reflect
5434 * free sequence numbers
5435 */
Josef Bacikaec74772008-07-24 12:12:38 -04005436static int btrfs_set_inode_index_count(struct inode *inode)
5437{
5438 struct btrfs_root *root = BTRFS_I(inode)->root;
5439 struct btrfs_key key, found_key;
5440 struct btrfs_path *path;
5441 struct extent_buffer *leaf;
5442 int ret;
5443
Li Zefan33345d012011-04-20 10:31:50 +08005444 key.objectid = btrfs_ino(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005445 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
5446 key.offset = (u64)-1;
5447
5448 path = btrfs_alloc_path();
5449 if (!path)
5450 return -ENOMEM;
5451
5452 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5453 if (ret < 0)
5454 goto out;
5455 /* FIXME: we should be able to handle this */
5456 if (ret == 0)
5457 goto out;
5458 ret = 0;
5459
5460 /*
5461 * MAGIC NUMBER EXPLANATION:
5462 * since we search a directory based on f_pos we have to start at 2
5463 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5464 * else has to start at 2
5465 */
5466 if (path->slots[0] == 0) {
5467 BTRFS_I(inode)->index_cnt = 2;
5468 goto out;
5469 }
5470
5471 path->slots[0]--;
5472
5473 leaf = path->nodes[0];
5474 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5475
Li Zefan33345d012011-04-20 10:31:50 +08005476 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacikaec74772008-07-24 12:12:38 -04005477 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
5478 BTRFS_I(inode)->index_cnt = 2;
5479 goto out;
5480 }
5481
5482 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
5483out:
5484 btrfs_free_path(path);
5485 return ret;
5486}
5487
Chris Masond352ac62008-09-29 15:18:18 -04005488/*
5489 * helper to find a free sequence number in a given directory. This current
5490 * code is very simple, later versions will do smarter things in the btree
5491 */
Chris Mason3de45862008-11-17 21:02:50 -05005492int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04005493{
5494 int ret = 0;
5495
5496 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08005497 ret = btrfs_inode_delayed_dir_index_count(dir);
5498 if (ret) {
5499 ret = btrfs_set_inode_index_count(dir);
5500 if (ret)
5501 return ret;
5502 }
Josef Bacikaec74772008-07-24 12:12:38 -04005503 }
5504
Chris Mason00e4e6b2008-08-05 11:18:09 -04005505 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04005506 BTRFS_I(dir)->index_cnt++;
5507
5508 return ret;
5509}
5510
Chris Mason39279cc2007-06-12 06:35:45 -04005511static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5512 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04005513 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05005514 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04005515 u64 ref_objectid, u64 objectid,
5516 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04005517{
5518 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005519 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04005520 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04005521 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05005522 struct btrfs_inode_ref *ref;
5523 struct btrfs_key key[2];
5524 u32 sizes[2];
5525 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04005526 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005527
Chris Mason5f39d392007-10-15 16:14:19 -04005528 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005529 if (!path)
5530 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04005531
Chris Mason39279cc2007-06-12 06:35:45 -04005532 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005533 if (!inode) {
5534 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005535 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005536 }
Chris Mason39279cc2007-06-12 06:35:45 -04005537
Li Zefan581bb052011-04-20 10:06:11 +08005538 /*
5539 * we have to initialize this early, so we can reclaim the inode
5540 * number if we fail afterwards in this function.
5541 */
5542 inode->i_ino = objectid;
5543
Josef Bacikaec74772008-07-24 12:12:38 -04005544 if (dir) {
liubo1abe9b82011-03-24 11:18:59 +00005545 trace_btrfs_inode_request(dir);
5546
Chris Mason3de45862008-11-17 21:02:50 -05005547 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04005548 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005549 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005550 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005551 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04005552 }
Josef Bacikaec74772008-07-24 12:12:38 -04005553 }
5554 /*
5555 * index_cnt is ignored for everything but a dir,
5556 * btrfs_get_inode_index_count has an explanation for the magic
5557 * number
5558 */
5559 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08005560 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04005561 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04005562 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00005563 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04005564
Josef Bacik5dc562c2012-08-17 13:14:17 -04005565 /*
5566 * We could have gotten an inode number from somebody who was fsynced
5567 * and then removed in this same transaction, so let's just set full
5568 * sync since it will be a full sync anyway and this will blow away the
5569 * old info in the log.
5570 */
5571 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
5572
Chris Mason9c583092008-01-29 15:15:18 -05005573 key[0].objectid = objectid;
5574 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
5575 key[0].offset = 0;
5576
Mark Fashehf1863732012-08-08 11:32:27 -07005577 /*
5578 * Start new inodes with an inode_ref. This is slightly more
5579 * efficient for small numbers of hard links since they will
5580 * be packed into one item. Extended refs will kick in if we
5581 * add more hard links than can fit in the ref item.
5582 */
Chris Mason9c583092008-01-29 15:15:18 -05005583 key[1].objectid = objectid;
5584 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
5585 key[1].offset = ref_objectid;
5586
5587 sizes[0] = sizeof(struct btrfs_inode_item);
5588 sizes[1] = name_len + sizeof(*ref);
5589
Chris Masonb9473432009-03-13 11:00:37 -04005590 path->leave_spinning = 1;
Chris Mason9c583092008-01-29 15:15:18 -05005591 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
5592 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04005593 goto fail;
5594
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03005595 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04005596 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005597 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04005598 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
5599 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06005600 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
5601 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04005602 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05005603
5604 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
5605 struct btrfs_inode_ref);
5606 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04005607 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05005608 ptr = (unsigned long)(ref + 1);
5609 write_extent_buffer(path->nodes[0], name, ptr, name_len);
5610
Chris Mason5f39d392007-10-15 16:14:19 -04005611 btrfs_mark_buffer_dirty(path->nodes[0]);
5612 btrfs_free_path(path);
5613
Chris Mason39279cc2007-06-12 06:35:45 -04005614 location = &BTRFS_I(inode)->location;
5615 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04005616 location->offset = 0;
5617 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
5618
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005619 btrfs_inherit_iflags(inode, dir);
5620
Al Viro569254b2011-07-24 17:08:40 -04005621 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04005622 if (btrfs_test_opt(root, NODATASUM))
5623 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo213490b2012-09-11 08:33:50 -06005624 if (btrfs_test_opt(root, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05005625 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
5626 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04005627 }
5628
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005629 btrfs_insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005630 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00005631
5632 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04005633 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00005634
Alexander Block8ea05e32012-07-25 17:35:53 +02005635 btrfs_update_root_times(trans, root);
5636
Filipe David Borba Manana63541922014-01-07 11:47:46 +00005637 ret = btrfs_inode_inherit_props(trans, inode, dir);
5638 if (ret)
5639 btrfs_err(root->fs_info,
5640 "error inheriting props for ino %llu (root %llu): %d",
5641 btrfs_ino(inode), root->root_key.objectid, ret);
5642
Chris Mason39279cc2007-06-12 06:35:45 -04005643 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005644fail:
Josef Bacikaec74772008-07-24 12:12:38 -04005645 if (dir)
5646 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04005647 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005648 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005649 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005650}
5651
5652static inline u8 btrfs_inode_type(struct inode *inode)
5653{
5654 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
5655}
5656
Chris Masond352ac62008-09-29 15:18:18 -04005657/*
5658 * utility function to add 'inode' into 'parent_inode' with
5659 * a give name and a given sequence number.
5660 * if 'add_backref' is true, also insert a backref from the
5661 * inode to the parent directory.
5662 */
Chris Masone02119d2008-09-05 16:13:11 -04005663int btrfs_add_link(struct btrfs_trans_handle *trans,
5664 struct inode *parent_inode, struct inode *inode,
5665 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005666{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005667 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005668 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04005669 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08005670 u64 ino = btrfs_ino(inode);
5671 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005672
Li Zefan33345d012011-04-20 10:31:50 +08005673 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005674 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
5675 } else {
Li Zefan33345d012011-04-20 10:31:50 +08005676 key.objectid = ino;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005677 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
5678 key.offset = 0;
5679 }
Chris Mason39279cc2007-06-12 06:35:45 -04005680
Li Zefan33345d012011-04-20 10:31:50 +08005681 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005682 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
5683 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08005684 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005685 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08005686 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
5687 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005688 }
5689
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005690 /* Nothing to clean up yet */
5691 if (ret)
5692 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005693
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005694 ret = btrfs_insert_dir_item(trans, root, name, name_len,
5695 parent_inode, &key,
5696 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05005697 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005698 goto fail_dir_item;
5699 else if (ret) {
5700 btrfs_abort_transaction(trans, root, ret);
5701 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005702 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005703
5704 btrfs_i_size_write(parent_inode, parent_inode->i_size +
5705 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005706 inode_inc_iversion(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005707 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
5708 ret = btrfs_update_inode(trans, root, parent_inode);
5709 if (ret)
5710 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005711 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05005712
5713fail_dir_item:
5714 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
5715 u64 local_index;
5716 int err;
5717 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
5718 key.objectid, root->root_key.objectid,
5719 parent_ino, &local_index, name, name_len);
5720
5721 } else if (add_backref) {
5722 u64 local_index;
5723 int err;
5724
5725 err = btrfs_del_inode_ref(trans, root, name, name_len,
5726 ino, parent_ino, &local_index);
5727 }
5728 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005729}
5730
5731static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00005732 struct inode *dir, struct dentry *dentry,
5733 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005734{
Josef Bacika1b075d2010-11-19 20:36:11 +00005735 int err = btrfs_add_link(trans, dir, inode,
5736 dentry->d_name.name, dentry->d_name.len,
5737 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005738 if (err > 0)
5739 err = -EEXIST;
5740 return err;
5741}
5742
Josef Bacik618e21d2007-07-11 10:18:17 -04005743static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04005744 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04005745{
5746 struct btrfs_trans_handle *trans;
5747 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005748 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04005749 int err;
5750 int drop_inode = 0;
5751 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005752 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04005753
5754 if (!new_valid_dev(rdev))
5755 return -EINVAL;
5756
Josef Bacik9ed74f22009-09-11 16:12:44 -04005757 /*
5758 * 2 for inode item and ref
5759 * 2 for dir items
5760 * 1 for xattr if selinux is on
5761 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005762 trans = btrfs_start_transaction(root, 5);
5763 if (IS_ERR(trans))
5764 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05005765
Li Zefan581bb052011-04-20 10:06:11 +08005766 err = btrfs_find_free_ino(root, &objectid);
5767 if (err)
5768 goto out_unlock;
5769
Josef Bacikaec74772008-07-24 12:12:38 -04005770 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005771 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005772 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005773 if (IS_ERR(inode)) {
5774 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005775 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005776 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005777
Eric Paris2a7dba32011-02-01 11:05:39 -05005778 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005779 if (err) {
5780 drop_inode = 1;
5781 goto out_unlock;
5782 }
5783
Casey Schauflerad19db72011-12-15 10:09:07 -05005784 /*
5785 * If the active LSM wants to access the inode during
5786 * d_instantiate it needs these. Smack checks to see
5787 * if the filesystem supports xattrs by looking at the
5788 * ops vector.
5789 */
5790
5791 inode->i_op = &btrfs_special_inode_operations;
Josef Bacika1b075d2010-11-19 20:36:11 +00005792 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04005793 if (err)
5794 drop_inode = 1;
5795 else {
Josef Bacik618e21d2007-07-11 10:18:17 -04005796 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04005797 btrfs_update_inode(trans, root, inode);
Al Viro08c422c2011-12-23 07:58:13 -05005798 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005799 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005800out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005801 btrfs_end_transaction(trans, root);
Josef Bacikc581afc2014-02-06 16:06:06 -05005802 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00005803 btrfs_btree_balance_dirty(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04005804 if (drop_inode) {
5805 inode_dec_link_count(inode);
5806 iput(inode);
5807 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005808 return err;
5809}
5810
Chris Mason39279cc2007-06-12 06:35:45 -04005811static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04005812 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04005813{
5814 struct btrfs_trans_handle *trans;
5815 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005816 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005817 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04005818 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04005819 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005820 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005821
Josef Bacik9ed74f22009-09-11 16:12:44 -04005822 /*
5823 * 2 for inode item and ref
5824 * 2 for dir items
5825 * 1 for xattr if selinux is on
5826 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005827 trans = btrfs_start_transaction(root, 5);
5828 if (IS_ERR(trans))
5829 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005830
Li Zefan581bb052011-04-20 10:06:11 +08005831 err = btrfs_find_free_ino(root, &objectid);
5832 if (err)
5833 goto out_unlock;
5834
Josef Bacikaec74772008-07-24 12:12:38 -04005835 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005836 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005837 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005838 if (IS_ERR(inode)) {
5839 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005840 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005841 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005842 drop_inode_on_err = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04005843
Eric Paris2a7dba32011-02-01 11:05:39 -05005844 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005845 if (err)
Josef Bacik33268ea2008-07-24 12:16:36 -04005846 goto out_unlock;
Josef Bacik33268ea2008-07-24 12:16:36 -04005847
Filipe Brandenburger9185aa52012-11-30 03:40:08 +00005848 err = btrfs_update_inode(trans, root, inode);
5849 if (err)
5850 goto out_unlock;
5851
Casey Schauflerad19db72011-12-15 10:09:07 -05005852 /*
5853 * If the active LSM wants to access the inode during
5854 * d_instantiate it needs these. Smack checks to see
5855 * if the filesystem supports xattrs by looking at the
5856 * ops vector.
5857 */
5858 inode->i_fop = &btrfs_file_operations;
5859 inode->i_op = &btrfs_file_inode_operations;
5860
Josef Bacika1b075d2010-11-19 20:36:11 +00005861 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005862 if (err)
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005863 goto out_unlock;
5864
5865 inode->i_mapping->a_ops = &btrfs_aops;
5866 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
5867 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
5868 d_instantiate(dentry, inode);
5869
Chris Mason39279cc2007-06-12 06:35:45 -04005870out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005871 btrfs_end_transaction(trans, root);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005872 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005873 inode_dec_link_count(inode);
5874 iput(inode);
5875 }
Josef Bacikc581afc2014-02-06 16:06:06 -05005876 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00005877 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005878 return err;
5879}
5880
5881static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
5882 struct dentry *dentry)
5883{
5884 struct btrfs_trans_handle *trans;
5885 struct btrfs_root *root = BTRFS_I(dir)->root;
5886 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005887 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04005888 int err;
5889 int drop_inode = 0;
5890
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005891 /* do not allow sys_link's with other subvols of the same device */
5892 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00005893 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005894
Mark Fashehf1863732012-08-08 11:32:27 -07005895 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00005896 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04005897
Chris Mason3de45862008-11-17 21:02:50 -05005898 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04005899 if (err)
5900 goto fail;
5901
Yan, Zhenga22285a2010-05-16 10:48:46 -04005902 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00005903 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04005904 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00005905 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04005906 */
Miao Xie7e6b6462011-02-18 09:21:17 +00005907 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005908 if (IS_ERR(trans)) {
5909 err = PTR_ERR(trans);
5910 goto fail;
5911 }
Chris Mason5f39d392007-10-15 16:14:19 -04005912
Miao Xie67de1172013-12-26 13:07:06 +08005913 /* There are several dir indexes for this inode, clear the cache. */
5914 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07005915 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005916 inode_inc_iversion(inode);
Miao Xie31534952011-04-13 13:19:21 +08005917 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6ee2010-10-23 11:11:40 -04005918 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04005919 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04005920
Josef Bacika1b075d2010-11-19 20:36:11 +00005921 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04005922
Yan, Zhenga5719522009-09-24 09:17:31 -04005923 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005924 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04005925 } else {
Al Viro10d9f302011-07-16 23:09:10 -04005926 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04005927 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005928 if (err)
5929 goto fail;
Al Viro08c422c2011-12-23 07:58:13 -05005930 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00005931 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04005932 }
Chris Mason39279cc2007-06-12 06:35:45 -04005933
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005934 btrfs_end_transaction(trans, root);
Josef Bacikc581afc2014-02-06 16:06:06 -05005935 btrfs_balance_delayed_items(root);
Chris Mason1832a6d2007-12-21 16:27:21 -05005936fail:
Chris Mason39279cc2007-06-12 06:35:45 -04005937 if (drop_inode) {
5938 inode_dec_link_count(inode);
5939 iput(inode);
5940 }
Liu Bob53d3f52012-11-14 14:34:34 +00005941 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005942 return err;
5943}
5944
Al Viro18bb1db2011-07-26 01:41:39 -04005945static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04005946{
Chris Masonb9d86662008-05-02 16:13:49 -04005947 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04005948 struct btrfs_trans_handle *trans;
5949 struct btrfs_root *root = BTRFS_I(dir)->root;
5950 int err = 0;
5951 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04005952 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005953 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005954
Josef Bacik9ed74f22009-09-11 16:12:44 -04005955 /*
5956 * 2 items for inode and ref
5957 * 2 items for dir items
5958 * 1 for xattr if selinux is on
5959 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005960 trans = btrfs_start_transaction(root, 5);
5961 if (IS_ERR(trans))
5962 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04005963
Li Zefan581bb052011-04-20 10:06:11 +08005964 err = btrfs_find_free_ino(root, &objectid);
5965 if (err)
5966 goto out_fail;
5967
Josef Bacikaec74772008-07-24 12:12:38 -04005968 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005969 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005970 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04005971 if (IS_ERR(inode)) {
5972 err = PTR_ERR(inode);
5973 goto out_fail;
5974 }
Chris Mason5f39d392007-10-15 16:14:19 -04005975
Chris Mason39279cc2007-06-12 06:35:45 -04005976 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04005977
Eric Paris2a7dba32011-02-01 11:05:39 -05005978 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005979 if (err)
5980 goto out_fail;
5981
Chris Mason39279cc2007-06-12 06:35:45 -04005982 inode->i_op = &btrfs_dir_inode_operations;
5983 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04005984
Chris Masondbe674a2008-07-17 12:54:05 -04005985 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005986 err = btrfs_update_inode(trans, root, inode);
5987 if (err)
5988 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005989
Josef Bacika1b075d2010-11-19 20:36:11 +00005990 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
5991 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005992 if (err)
5993 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005994
Chris Mason39279cc2007-06-12 06:35:45 -04005995 d_instantiate(dentry, inode);
5996 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005997
5998out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005999 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04006000 if (drop_on_err)
6001 iput(inode);
Josef Bacikc581afc2014-02-06 16:06:06 -05006002 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006003 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006004 return err;
6005}
6006
Chris Masond352ac62008-09-29 15:18:18 -04006007/* helper for btfs_get_extent. Given an existing extent in the tree,
6008 * and an extent that you want to insert, deal with overlap and insert
6009 * the new extent into the tree.
6010 */
Chris Mason3b951512008-04-17 11:29:12 -04006011static int merge_extent_mapping(struct extent_map_tree *em_tree,
6012 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006013 struct extent_map *em,
6014 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04006015{
6016 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04006017
Chris Masone6dcd2d2008-07-17 12:53:50 -04006018 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
6019 start_diff = map_start - em->start;
6020 em->start = map_start;
6021 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04006022 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
6023 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006024 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04006025 em->block_len -= start_diff;
6026 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006027 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006028}
6029
Chris Masonc8b97812008-10-29 14:49:59 -04006030static noinline int uncompress_inline(struct btrfs_path *path,
6031 struct inode *inode, struct page *page,
6032 size_t pg_offset, u64 extent_offset,
6033 struct btrfs_file_extent_item *item)
6034{
6035 int ret;
6036 struct extent_buffer *leaf = path->nodes[0];
6037 char *tmp;
6038 size_t max_size;
6039 unsigned long inline_size;
6040 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006041 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006042
6043 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006044 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006045 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6046 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006047 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006048 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006049 if (!tmp)
6050 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006051 ptr = btrfs_file_extent_inline_start(item);
6052
6053 read_extent_buffer(leaf, tmp, ptr, inline_size);
6054
Chris Mason5b050f02008-11-11 09:34:41 -05006055 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006056 ret = btrfs_decompress(compress_type, tmp, page,
6057 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006058 if (ret) {
Cong Wang7ac687d2011-11-25 23:14:28 +08006059 char *kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04006060 unsigned long copy_size = min_t(u64,
6061 PAGE_CACHE_SIZE - pg_offset,
6062 max_size - extent_offset);
6063 memset(kaddr + pg_offset, 0, copy_size);
Cong Wang7ac687d2011-11-25 23:14:28 +08006064 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -04006065 }
6066 kfree(tmp);
6067 return 0;
6068}
6069
Chris Masond352ac62008-09-29 15:18:18 -04006070/*
6071 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006072 * the ugly parts come from merging extents from the disk with the in-ram
6073 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006074 * where the in-ram extents might be locked pending data=ordered completion.
6075 *
6076 * This also copies inline extents directly into the page.
6077 */
Chris Masond3977122009-01-05 21:25:51 -05006078
Chris Masona52d9a82007-08-27 16:49:44 -04006079struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05006080 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04006081 int create)
6082{
6083 int ret;
6084 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04006085 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04006086 u64 extent_start = 0;
6087 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08006088 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006089 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006090 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04006091 struct btrfs_root *root = BTRFS_I(inode)->root;
6092 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006093 struct extent_buffer *leaf;
6094 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006095 struct extent_map *em = NULL;
6096 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05006097 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006098 struct btrfs_trans_handle *trans = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08006099 int compress_type;
Chris Masona52d9a82007-08-27 16:49:44 -04006100
Chris Masona52d9a82007-08-27 16:49:44 -04006101again:
Chris Mason890871b2009-09-02 16:24:52 -04006102 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006103 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006104 if (em)
6105 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006106 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006107
Chris Masona52d9a82007-08-27 16:49:44 -04006108 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006109 if (em->start > start || em->start + em->len <= start)
6110 free_extent_map(em);
6111 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006112 free_extent_map(em);
6113 else
6114 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006115 }
David Sterba172ddd62011-04-21 00:48:27 +02006116 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006117 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006118 err = -ENOMEM;
6119 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006120 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006121 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006122 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006123 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006124 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006125 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006126
6127 if (!path) {
6128 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006129 if (!path) {
6130 err = -ENOMEM;
6131 goto out;
6132 }
6133 /*
6134 * Chances are we'll be called again, so go ahead and do
6135 * readahead
6136 */
6137 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04006138 }
6139
Chris Mason179e29e2007-11-01 11:28:41 -04006140 ret = btrfs_lookup_file_extent(trans, root, path,
6141 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006142 if (ret < 0) {
6143 err = ret;
6144 goto out;
6145 }
6146
6147 if (ret != 0) {
6148 if (path->slots[0] == 0)
6149 goto not_found;
6150 path->slots[0]--;
6151 }
6152
Chris Mason5f39d392007-10-15 16:14:19 -04006153 leaf = path->nodes[0];
6154 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006155 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006156 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006157 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6158 found_type = btrfs_key_type(&found_key);
6159 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006160 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006161 /*
6162 * If we backup past the first extent we want to move forward
6163 * and see if there is an extent in front of us, otherwise we'll
6164 * say there is a hole for our whole search range which can
6165 * cause problems.
6166 */
6167 extent_end = start;
6168 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006169 }
6170
Chris Mason5f39d392007-10-15 16:14:19 -04006171 found_type = btrfs_file_extent_type(leaf, item);
6172 extent_start = found_key.offset;
Li Zefan261507a02010-12-17 14:21:50 +08006173 compress_type = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04006174 if (found_type == BTRFS_FILE_EXTENT_REG ||
6175 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006176 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006177 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006178 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6179 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08006180 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Qu Wenruofda28322013-02-26 08:10:22 +00006181 extent_end = ALIGN(extent_start + size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04006182 }
Josef Bacik25a50342013-10-14 12:08:38 -04006183next:
Yan Zheng9036c102008-10-30 14:19:41 -04006184 if (start >= extent_end) {
6185 path->slots[0]++;
6186 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6187 ret = btrfs_next_leaf(root, path);
6188 if (ret < 0) {
6189 err = ret;
6190 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006191 }
Yan Zheng9036c102008-10-30 14:19:41 -04006192 if (ret > 0)
6193 goto not_found;
6194 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006195 }
Yan Zheng9036c102008-10-30 14:19:41 -04006196 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6197 if (found_key.objectid != objectid ||
6198 found_key.type != BTRFS_EXTENT_DATA_KEY)
6199 goto not_found;
6200 if (start + len <= found_key.offset)
6201 goto not_found;
6202 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006203 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006204 em->len = found_key.offset - start;
6205 goto not_found_em;
6206 }
6207
Josef Bacikcc95bef2013-04-04 14:31:27 -04006208 em->ram_bytes = btrfs_file_extent_ram_bytes(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04006209 if (found_type == BTRFS_FILE_EXTENT_REG ||
6210 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04006211 em->start = extent_start;
6212 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05006213 em->orig_start = extent_start -
6214 btrfs_file_extent_offset(leaf, item);
Josef Bacikb4939682012-12-03 10:31:19 -05006215 em->orig_block_len = btrfs_file_extent_disk_num_bytes(leaf,
6216 item);
Chris Masondb945352007-10-15 16:15:53 -04006217 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
6218 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04006219 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04006220 goto insert;
6221 }
Li Zefan261507a02010-12-17 14:21:50 +08006222 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006223 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08006224 em->compress_type = compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006225 em->block_start = bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05006226 em->block_len = em->orig_block_len;
Chris Masonc8b97812008-10-29 14:49:59 -04006227 } else {
6228 bytenr += btrfs_file_extent_offset(leaf, item);
6229 em->block_start = bytenr;
6230 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04006231 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
6232 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04006233 }
Chris Masona52d9a82007-08-27 16:49:44 -04006234 goto insert;
6235 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006236 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006237 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006238 size_t size;
6239 size_t extent_offset;
6240 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006241
Chris Masona52d9a82007-08-27 16:49:44 -04006242 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04006243 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04006244 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04006245 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04006246 goto out;
6247 }
6248
Chris Mason514ac8a2014-01-03 21:07:00 -08006249 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04006250 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05006251 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04006252 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006253 em->start = extent_start + extent_offset;
Qu Wenruofda28322013-02-26 08:10:22 +00006254 em->len = ALIGN(copy_size, root->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006255 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006256 em->orig_start = em->start;
Li Zefan261507a02010-12-17 14:21:50 +08006257 if (compress_type) {
Chris Masonc8b97812008-10-29 14:49:59 -04006258 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08006259 em->compress_type = compress_type;
6260 }
Yan689f9342007-10-29 11:41:07 -04006261 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006262 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006263 if (btrfs_file_extent_compression(leaf, item) !=
6264 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006265 ret = uncompress_inline(path, inode, page,
6266 pg_offset,
6267 extent_offset, item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006268 BUG_ON(ret); /* -ENOMEM */
Chris Masonc8b97812008-10-29 14:49:59 -04006269 } else {
6270 map = kmap(page);
6271 read_extent_buffer(leaf, map + pg_offset, ptr,
6272 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04006273 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
6274 memset(map + pg_offset + copy_size, 0,
6275 PAGE_CACHE_SIZE - pg_offset -
6276 copy_size);
6277 }
Chris Masonc8b97812008-10-29 14:49:59 -04006278 kunmap(page);
6279 }
Chris Mason179e29e2007-11-01 11:28:41 -04006280 flush_dcache_page(page);
6281 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006282 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006283 if (!trans) {
6284 kunmap(page);
6285 free_extent_map(em);
6286 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006287
David Sterbab3b4aa72011-04-21 01:20:15 +02006288 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006289 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006290
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006291 if (IS_ERR(trans))
6292 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006293 goto again;
6294 }
Chris Masonc8b97812008-10-29 14:49:59 -04006295 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006296 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006297 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006298 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006299 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006300 }
Chris Masond1310b22008-01-24 16:13:08 -05006301 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006302 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006303 goto insert;
6304 } else {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00006305 WARN(1, KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04006306 }
6307not_found:
6308 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006309 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006310 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006311not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04006312 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04006313 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006314insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006315 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006316 if (em->start > start || extent_map_end(em) <= start) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006317 btrfs_err(root->fs_info, "bad extent! em: [%llu %llu] passed [%llu %llu]",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006318 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04006319 err = -EIO;
6320 goto out;
6321 }
Chris Masond1310b22008-01-24 16:13:08 -05006322
6323 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006324 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006325 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006326 /* it is possible that someone inserted the extent into the tree
6327 * while we had the lock dropped. It is also possible that
6328 * an overlapping map exists in the tree
6329 */
Chris Masona52d9a82007-08-27 16:49:44 -04006330 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04006331 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006332
6333 ret = 0;
6334
Chris Mason3b951512008-04-17 11:29:12 -04006335 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04006336 if (existing && (existing->start > start ||
6337 existing->start + existing->len <= start)) {
6338 free_extent_map(existing);
6339 existing = NULL;
6340 }
Chris Mason3b951512008-04-17 11:29:12 -04006341 if (!existing) {
6342 existing = lookup_extent_mapping(em_tree, em->start,
6343 em->len);
6344 if (existing) {
6345 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006346 em, start,
6347 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04006348 free_extent_map(existing);
6349 if (err) {
6350 free_extent_map(em);
6351 em = NULL;
6352 }
6353 } else {
6354 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04006355 free_extent_map(em);
6356 em = NULL;
6357 }
6358 } else {
6359 free_extent_map(em);
6360 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006361 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006362 }
Chris Masona52d9a82007-08-27 16:49:44 -04006363 }
Chris Mason890871b2009-09-02 16:24:52 -04006364 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006365out:
liubo1abe9b82011-03-24 11:18:59 +00006366
Steven Rostedt4cd85872013-11-14 22:57:29 -05006367 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00006368
Chris Masonf4219502008-07-22 11:18:09 -04006369 if (path)
6370 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04006371 if (trans) {
6372 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05006373 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04006374 err = ret;
6375 }
Chris Masona52d9a82007-08-27 16:49:44 -04006376 if (err) {
6377 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04006378 return ERR_PTR(err);
6379 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006380 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04006381 return em;
6382}
6383
Chris Masonec29ed52011-02-23 16:23:20 -05006384struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
6385 size_t pg_offset, u64 start, u64 len,
6386 int create)
6387{
6388 struct extent_map *em;
6389 struct extent_map *hole_em = NULL;
6390 u64 range_start = start;
6391 u64 end;
6392 u64 found;
6393 u64 found_end;
6394 int err = 0;
6395
6396 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
6397 if (IS_ERR(em))
6398 return em;
6399 if (em) {
6400 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00006401 * if our em maps to
6402 * - a hole or
6403 * - a pre-alloc extent,
6404 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05006405 */
Liu Bof9e4fb52013-01-07 10:10:12 +00006406 if (em->block_start != EXTENT_MAP_HOLE &&
6407 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05006408 return em;
6409 else
6410 hole_em = em;
6411 }
6412
6413 /* check to see if we've wrapped (len == -1 or similar) */
6414 end = start + len;
6415 if (end < start)
6416 end = (u64)-1;
6417 else
6418 end -= 1;
6419
6420 em = NULL;
6421
6422 /* ok, we didn't find anything, lets look for delalloc */
6423 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
6424 end, len, EXTENT_DELALLOC, 1);
6425 found_end = range_start + found;
6426 if (found_end < range_start)
6427 found_end = (u64)-1;
6428
6429 /*
6430 * we didn't find anything useful, return
6431 * the original results from get_extent()
6432 */
6433 if (range_start > end || found_end <= start) {
6434 em = hole_em;
6435 hole_em = NULL;
6436 goto out;
6437 }
6438
6439 /* adjust the range_start to make sure it doesn't
6440 * go backwards from the start they passed in
6441 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05306442 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05006443 found = found_end - range_start;
6444
6445 if (found > 0) {
6446 u64 hole_start = start;
6447 u64 hole_len = len;
6448
David Sterba172ddd62011-04-21 00:48:27 +02006449 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05006450 if (!em) {
6451 err = -ENOMEM;
6452 goto out;
6453 }
6454 /*
6455 * when btrfs_get_extent can't find anything it
6456 * returns one huge hole
6457 *
6458 * make sure what it found really fits our range, and
6459 * adjust to make sure it is based on the start from
6460 * the caller
6461 */
6462 if (hole_em) {
6463 u64 calc_end = extent_map_end(hole_em);
6464
6465 if (calc_end <= start || (hole_em->start > end)) {
6466 free_extent_map(hole_em);
6467 hole_em = NULL;
6468 } else {
6469 hole_start = max(hole_em->start, start);
6470 hole_len = calc_end - hole_start;
6471 }
6472 }
6473 em->bdev = NULL;
6474 if (hole_em && range_start > hole_start) {
6475 /* our hole starts before our delalloc, so we
6476 * have to return just the parts of the hole
6477 * that go until the delalloc starts
6478 */
6479 em->len = min(hole_len,
6480 range_start - hole_start);
6481 em->start = hole_start;
6482 em->orig_start = hole_start;
6483 /*
6484 * don't adjust block start at all,
6485 * it is fixed at EXTENT_MAP_HOLE
6486 */
6487 em->block_start = hole_em->block_start;
6488 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00006489 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
6490 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05006491 } else {
6492 em->start = range_start;
6493 em->len = found;
6494 em->orig_start = range_start;
6495 em->block_start = EXTENT_MAP_DELALLOC;
6496 em->block_len = found;
6497 }
6498 } else if (hole_em) {
6499 return hole_em;
6500 }
6501out:
6502
6503 free_extent_map(hole_em);
6504 if (err) {
6505 free_extent_map(em);
6506 return ERR_PTR(err);
6507 }
6508 return em;
6509}
6510
Josef Bacik4b46fce2010-05-23 11:00:55 -04006511static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
6512 u64 start, u64 len)
6513{
6514 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04006515 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006516 struct btrfs_key ins;
6517 u64 alloc_hint;
6518 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006519
Josef Bacik4b46fce2010-05-23 11:00:55 -04006520 alloc_hint = get_extent_allocation_hint(inode, start, len);
Josef Bacik00361582013-08-14 14:02:47 -04006521 ret = btrfs_reserve_extent(root, len, root->sectorsize, 0,
Josef Bacik81c9ad22012-01-18 10:56:06 -05006522 alloc_hint, &ins, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006523 if (ret)
6524 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006525
Josef Bacik70c8a912012-10-11 16:54:30 -04006526 em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006527 ins.offset, ins.offset, ins.offset, 0);
Josef Bacik00361582013-08-14 14:02:47 -04006528 if (IS_ERR(em)) {
6529 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
6530 return em;
6531 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006532
6533 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
6534 ins.offset, ins.offset, 0);
6535 if (ret) {
6536 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
Josef Bacik00361582013-08-14 14:02:47 -04006537 free_extent_map(em);
6538 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006539 }
Josef Bacik00361582013-08-14 14:02:47 -04006540
Josef Bacik4b46fce2010-05-23 11:00:55 -04006541 return em;
6542}
6543
Chris Mason46bfbb52010-05-26 11:04:10 -04006544/*
6545 * returns 1 when the nocow is safe, < 1 on error, 0 if the
6546 * block must be cow'd
6547 */
Josef Bacik00361582013-08-14 14:02:47 -04006548noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04006549 u64 *orig_start, u64 *orig_block_len,
6550 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04006551{
Josef Bacik00361582013-08-14 14:02:47 -04006552 struct btrfs_trans_handle *trans;
Chris Mason46bfbb52010-05-26 11:04:10 -04006553 struct btrfs_path *path;
6554 int ret;
6555 struct extent_buffer *leaf;
6556 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08006557 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04006558 struct btrfs_file_extent_item *fi;
6559 struct btrfs_key key;
6560 u64 disk_bytenr;
6561 u64 backref_offset;
6562 u64 extent_end;
6563 u64 num_bytes;
6564 int slot;
6565 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04006566 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08006567
Chris Mason46bfbb52010-05-26 11:04:10 -04006568 path = btrfs_alloc_path();
6569 if (!path)
6570 return -ENOMEM;
6571
Josef Bacik00361582013-08-14 14:02:47 -04006572 ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04006573 offset, 0);
6574 if (ret < 0)
6575 goto out;
6576
6577 slot = path->slots[0];
6578 if (ret == 1) {
6579 if (slot == 0) {
6580 /* can't find the item, must cow */
6581 ret = 0;
6582 goto out;
6583 }
6584 slot--;
6585 }
6586 ret = 0;
6587 leaf = path->nodes[0];
6588 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08006589 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04006590 key.type != BTRFS_EXTENT_DATA_KEY) {
6591 /* not our file or wrong item type, must cow */
6592 goto out;
6593 }
6594
6595 if (key.offset > offset) {
6596 /* Wrong offset, must cow */
6597 goto out;
6598 }
6599
6600 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
6601 found_type = btrfs_file_extent_type(leaf, fi);
6602 if (found_type != BTRFS_FILE_EXTENT_REG &&
6603 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
6604 /* not a regular extent, must cow */
6605 goto out;
6606 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04006607
6608 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
6609 goto out;
6610
Miao Xiee77751a2013-12-27 21:11:50 +08006611 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
6612 if (extent_end <= offset)
6613 goto out;
6614
Chris Mason46bfbb52010-05-26 11:04:10 -04006615 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04006616 if (disk_bytenr == 0)
6617 goto out;
6618
6619 if (btrfs_file_extent_compression(leaf, fi) ||
6620 btrfs_file_extent_encryption(leaf, fi) ||
6621 btrfs_file_extent_other_encoding(leaf, fi))
6622 goto out;
6623
Chris Mason46bfbb52010-05-26 11:04:10 -04006624 backref_offset = btrfs_file_extent_offset(leaf, fi);
6625
Josef Bacik7ee9e442013-06-21 16:37:03 -04006626 if (orig_start) {
6627 *orig_start = key.offset - backref_offset;
6628 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
6629 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
6630 }
Josef Bacikeb384b52013-04-24 16:32:55 -04006631
Chris Mason46bfbb52010-05-26 11:04:10 -04006632 if (btrfs_extent_readonly(root, disk_bytenr))
6633 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08006634
6635 num_bytes = min(offset + *len, extent_end) - offset;
6636 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
6637 u64 range_end;
6638
6639 range_end = round_up(offset + num_bytes, root->sectorsize) - 1;
6640 ret = test_range_bit(io_tree, offset, range_end,
6641 EXTENT_DELALLOC, 0, NULL);
6642 if (ret) {
6643 ret = -EAGAIN;
6644 goto out;
6645 }
6646 }
6647
Josef Bacik1bda19e2013-10-18 12:10:36 -04006648 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04006649
6650 /*
6651 * look for other files referencing this extent, if we
6652 * find any we must cow
6653 */
Josef Bacik00361582013-08-14 14:02:47 -04006654 trans = btrfs_join_transaction(root);
6655 if (IS_ERR(trans)) {
6656 ret = 0;
Chris Mason46bfbb52010-05-26 11:04:10 -04006657 goto out;
Josef Bacik00361582013-08-14 14:02:47 -04006658 }
6659
6660 ret = btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
6661 key.offset - backref_offset, disk_bytenr);
6662 btrfs_end_transaction(trans, root);
6663 if (ret) {
6664 ret = 0;
6665 goto out;
6666 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006667
6668 /*
6669 * adjust disk_bytenr and num_bytes to cover just the bytes
6670 * in this extent we are about to write. If there
6671 * are any csums in that range we have to cow in order
6672 * to keep the csums correct
6673 */
6674 disk_bytenr += backref_offset;
6675 disk_bytenr += offset - key.offset;
Chris Mason46bfbb52010-05-26 11:04:10 -04006676 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
6677 goto out;
6678 /*
6679 * all of the above have passed, it is safe to overwrite this extent
6680 * without cow
6681 */
Josef Bacikeb384b52013-04-24 16:32:55 -04006682 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04006683 ret = 1;
6684out:
6685 btrfs_free_path(path);
6686 return ret;
6687}
6688
Josef Bacikeb838e72012-07-31 16:28:48 -04006689static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
6690 struct extent_state **cached_state, int writing)
6691{
6692 struct btrfs_ordered_extent *ordered;
6693 int ret = 0;
6694
6695 while (1) {
6696 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6697 0, cached_state);
6698 /*
6699 * We're concerned with the entire range that we're going to be
6700 * doing DIO to, so we need to make sure theres no ordered
6701 * extents in this range.
6702 */
6703 ordered = btrfs_lookup_ordered_range(inode, lockstart,
6704 lockend - lockstart + 1);
6705
6706 /*
6707 * We need to make sure there are no buffered pages in this
6708 * range either, we could have raced between the invalidate in
6709 * generic_file_direct_write and locking the extent. The
6710 * invalidate needs to happen so that reads after a write do not
6711 * get stale data.
6712 */
6713 if (!ordered && (!writing ||
6714 !test_range_bit(&BTRFS_I(inode)->io_tree,
6715 lockstart, lockend, EXTENT_UPTODATE, 0,
6716 *cached_state)))
6717 break;
6718
6719 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6720 cached_state, GFP_NOFS);
6721
6722 if (ordered) {
6723 btrfs_start_ordered_extent(inode, ordered, 1);
6724 btrfs_put_ordered_extent(ordered);
6725 } else {
6726 /* Screw you mmap */
6727 ret = filemap_write_and_wait_range(inode->i_mapping,
6728 lockstart,
6729 lockend);
6730 if (ret)
6731 break;
6732
6733 /*
6734 * If we found a page that couldn't be invalidated just
6735 * fall back to buffered.
6736 */
6737 ret = invalidate_inode_pages2_range(inode->i_mapping,
6738 lockstart >> PAGE_CACHE_SHIFT,
6739 lockend >> PAGE_CACHE_SHIFT);
6740 if (ret)
6741 break;
6742 }
6743
6744 cond_resched();
6745 }
6746
6747 return ret;
6748}
6749
Josef Bacik69ffb542012-09-11 15:40:07 -04006750static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
6751 u64 len, u64 orig_start,
6752 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006753 u64 orig_block_len, u64 ram_bytes,
6754 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04006755{
6756 struct extent_map_tree *em_tree;
6757 struct extent_map *em;
6758 struct btrfs_root *root = BTRFS_I(inode)->root;
6759 int ret;
6760
6761 em_tree = &BTRFS_I(inode)->extent_tree;
6762 em = alloc_extent_map();
6763 if (!em)
6764 return ERR_PTR(-ENOMEM);
6765
6766 em->start = start;
6767 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04006768 em->mod_start = start;
6769 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04006770 em->len = len;
6771 em->block_len = block_len;
6772 em->block_start = block_start;
6773 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05006774 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04006775 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04006776 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04006777 set_bit(EXTENT_FLAG_PINNED, &em->flags);
6778 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05006779 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04006780
6781 do {
6782 btrfs_drop_extent_cache(inode, em->start,
6783 em->start + em->len - 1, 0);
6784 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006785 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04006786 write_unlock(&em_tree->lock);
6787 } while (ret == -EEXIST);
6788
6789 if (ret) {
6790 free_extent_map(em);
6791 return ERR_PTR(ret);
6792 }
6793
6794 return em;
6795}
6796
6797
Josef Bacik4b46fce2010-05-23 11:00:55 -04006798static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
6799 struct buffer_head *bh_result, int create)
6800{
6801 struct extent_map *em;
6802 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04006803 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006804 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04006805 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006806 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04006807 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00006808 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006809
Miao Xie172a5042013-02-21 02:48:22 -07006810 if (create)
Josef Bacikeb838e72012-07-31 16:28:48 -04006811 unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07006812 else
Josef Bacikc3298612012-08-03 16:49:19 -04006813 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04006814
Josef Bacikc3298612012-08-03 16:49:19 -04006815 lockstart = start;
6816 lockend = start + len - 1;
6817
Josef Bacikeb838e72012-07-31 16:28:48 -04006818 /*
6819 * If this errors out it's because we couldn't invalidate pagecache for
6820 * this range and we need to fallback to buffered.
6821 */
6822 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
6823 return -ENOTBLK;
6824
Josef Bacik4b46fce2010-05-23 11:00:55 -04006825 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04006826 if (IS_ERR(em)) {
6827 ret = PTR_ERR(em);
6828 goto unlock_err;
6829 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006830
6831 /*
6832 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
6833 * io. INLINE is special, and we could probably kludge it in here, but
6834 * it's still buffered so for safety lets just fall back to the generic
6835 * buffered path.
6836 *
6837 * For COMPRESSED we _have_ to read the entire extent in so we can
6838 * decompress it, so there will be buffering required no matter what we
6839 * do, so go ahead and fallback to buffered.
6840 *
6841 * We return -ENOTBLK because thats what makes DIO go ahead and go back
6842 * to buffered IO. Don't blame me, this is the price we pay for using
6843 * the generic code.
6844 */
6845 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
6846 em->block_start == EXTENT_MAP_INLINE) {
6847 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006848 ret = -ENOTBLK;
6849 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006850 }
6851
6852 /* Just a good old fashioned hole, return */
6853 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
6854 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
6855 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006856 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006857 }
6858
6859 /*
6860 * We don't allocate a new extent in the following cases
6861 *
6862 * 1) The inode is marked as NODATACOW. In this case we'll just use the
6863 * existing extent.
6864 * 2) The extent is marked as PREALLOC. We're good to go here and can
6865 * just use the extent.
6866 *
6867 */
Chris Mason46bfbb52010-05-26 11:04:10 -04006868 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04006869 len = min(len, em->len - (start - em->start));
6870 lockstart = start + len;
6871 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04006872 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006873
6874 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
6875 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
6876 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04006877 int type;
6878 int ret;
Josef Bacikeb384b52013-04-24 16:32:55 -04006879 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006880
6881 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6882 type = BTRFS_ORDERED_PREALLOC;
6883 else
6884 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04006885 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04006886 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04006887
Josef Bacik00361582013-08-14 14:02:47 -04006888 if (can_nocow_extent(inode, start, &len, &orig_start,
Josef Bacik7ee9e442013-06-21 16:37:03 -04006889 &orig_block_len, &ram_bytes) == 1) {
Josef Bacik69ffb542012-09-11 15:40:07 -04006890 if (type == BTRFS_ORDERED_PREALLOC) {
6891 free_extent_map(em);
6892 em = create_pinned_em(inode, start, len,
6893 orig_start,
Josef Bacikb4939682012-12-03 10:31:19 -05006894 block_start, len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006895 orig_block_len,
6896 ram_bytes, type);
Josef Bacik00361582013-08-14 14:02:47 -04006897 if (IS_ERR(em))
Josef Bacik69ffb542012-09-11 15:40:07 -04006898 goto unlock_err;
Josef Bacik69ffb542012-09-11 15:40:07 -04006899 }
6900
Chris Mason46bfbb52010-05-26 11:04:10 -04006901 ret = btrfs_add_ordered_extent_dio(inode, start,
6902 block_start, len, len, type);
Chris Mason46bfbb52010-05-26 11:04:10 -04006903 if (ret) {
6904 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006905 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04006906 }
6907 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006908 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006909 }
Josef Bacik00361582013-08-14 14:02:47 -04006910
Chris Mason46bfbb52010-05-26 11:04:10 -04006911 /*
6912 * this will cow the extent, reset the len in case we changed
6913 * it above
6914 */
6915 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04006916 free_extent_map(em);
6917 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04006918 if (IS_ERR(em)) {
6919 ret = PTR_ERR(em);
6920 goto unlock_err;
6921 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006922 len = min(len, em->len - (start - em->start));
6923unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04006924 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
6925 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04006926 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006927 bh_result->b_bdev = em->bdev;
6928 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04006929 if (create) {
6930 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6931 set_buffer_new(bh_result);
6932
6933 /*
6934 * Need to update the i_size under the extent lock so buffered
6935 * readers will get the updated i_size when we unlock.
6936 */
6937 if (start + len > i_size_read(inode))
6938 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00006939
Miao Xie172a5042013-02-21 02:48:22 -07006940 spin_lock(&BTRFS_I(inode)->lock);
6941 BTRFS_I(inode)->outstanding_extents++;
6942 spin_unlock(&BTRFS_I(inode)->lock);
6943
Miao Xie09348562013-02-07 10:12:07 +00006944 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6945 lockstart + len - 1, EXTENT_DELALLOC, NULL,
6946 &cached_state, GFP_NOFS);
6947 BUG_ON(ret);
Josef Bacikc3473e82012-06-19 10:59:00 -04006948 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006949
Josef Bacikeb838e72012-07-31 16:28:48 -04006950 /*
6951 * In the case of write we need to clear and unlock the entire range,
6952 * in the case of read we need to unlock only the end area that we
6953 * aren't using if there is any left over space.
6954 */
Liu Bo24c03fa2012-08-22 20:10:38 -06006955 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00006956 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6957 lockend, unlock_bits, 1, 0,
6958 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06006959 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04006960 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06006961 }
Josef Bacikeb838e72012-07-31 16:28:48 -04006962
Josef Bacik4b46fce2010-05-23 11:00:55 -04006963 free_extent_map(em);
6964
6965 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006966
6967unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04006968 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6969 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
6970 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006971}
6972
Josef Bacik4b46fce2010-05-23 11:00:55 -04006973static void btrfs_endio_direct_read(struct bio *bio, int err)
6974{
Miao Xiee65e1532010-11-22 03:04:43 +00006975 struct btrfs_dio_private *dip = bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006976 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
6977 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006978 struct inode *inode = dip->inode;
6979 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason9be33952013-05-17 18:30:14 -04006980 struct bio *dio_bio;
Miao Xiefacc8a22013-07-25 19:22:34 +08006981 u32 *csums = (u32 *)dip->csum;
6982 int index = 0;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006983 u64 start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006984
6985 start = dip->logical_offset;
6986 do {
6987 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
6988 struct page *page = bvec->bv_page;
6989 char *kaddr;
6990 u32 csum = ~(u32)0;
6991 unsigned long flags;
6992
6993 local_irq_save(flags);
Cong Wang7ac687d2011-11-25 23:14:28 +08006994 kaddr = kmap_atomic(page);
Liu Bob0496682013-03-14 14:57:45 +00006995 csum = btrfs_csum_data(kaddr + bvec->bv_offset,
Josef Bacik4b46fce2010-05-23 11:00:55 -04006996 csum, bvec->bv_len);
6997 btrfs_csum_final(csum, (char *)&csum);
Cong Wang7ac687d2011-11-25 23:14:28 +08006998 kunmap_atomic(kaddr);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006999 local_irq_restore(flags);
7000
7001 flush_dcache_page(bvec->bv_page);
Miao Xiefacc8a22013-07-25 19:22:34 +08007002 if (csum != csums[index]) {
7003 btrfs_err(root->fs_info, "csum failed ino %llu off %llu csum %u expected csum %u",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02007004 btrfs_ino(inode), start, csum,
7005 csums[index]);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007006 err = -EIO;
7007 }
7008 }
7009
7010 start += bvec->bv_len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007011 bvec++;
Miao Xiefacc8a22013-07-25 19:22:34 +08007012 index++;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007013 } while (bvec <= bvec_end);
7014
7015 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007016 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04007017 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007018
Josef Bacik4b46fce2010-05-23 11:00:55 -04007019 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04007020
7021 /* If we had a csum failure make sure to clear the uptodate flag */
7022 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04007023 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
7024 dio_end_io(dio_bio, err);
7025 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007026}
7027
7028static void btrfs_endio_direct_write(struct bio *bio, int err)
7029{
7030 struct btrfs_dio_private *dip = bio->bi_private;
7031 struct inode *inode = dip->inode;
7032 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007033 struct btrfs_ordered_extent *ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05007034 u64 ordered_offset = dip->logical_offset;
7035 u64 ordered_bytes = dip->bytes;
Chris Mason9be33952013-05-17 18:30:14 -04007036 struct bio *dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007037 int ret;
7038
7039 if (err)
7040 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05007041again:
7042 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
7043 &ordered_offset,
Josef Bacik5fd02042012-05-02 14:00:54 -04007044 ordered_bytes, !err);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007045 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05007046 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007047
Qu Wenruofccb5d82014-02-28 10:46:10 +08007048 btrfs_init_work(&ordered->work, finish_ordered_fn, NULL, NULL);
7049 btrfs_queue_work(root->fs_info->endio_write_workers,
7050 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05007051out_test:
7052 /*
7053 * our bio might span multiple ordered extents. If we haven't
7054 * completed the accounting for the whole dio, go back and try again
7055 */
7056 if (ordered_offset < dip->logical_offset + dip->bytes) {
7057 ordered_bytes = dip->logical_offset + dip->bytes -
7058 ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04007059 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05007060 goto again;
7061 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007062out_done:
Chris Mason9be33952013-05-17 18:30:14 -04007063 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007064
Josef Bacik4b46fce2010-05-23 11:00:55 -04007065 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04007066
7067 /* If we had an error make sure to clear the uptodate flag */
7068 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04007069 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
7070 dio_end_io(dio_bio, err);
7071 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007072}
7073
Chris Masoneaf25d92010-05-25 09:48:28 -04007074static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
7075 struct bio *bio, int mirror_num,
7076 unsigned long bio_flags, u64 offset)
7077{
7078 int ret;
7079 struct btrfs_root *root = BTRFS_I(inode)->root;
7080 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007081 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04007082 return 0;
7083}
7084
Miao Xiee65e1532010-11-22 03:04:43 +00007085static void btrfs_end_dio_bio(struct bio *bio, int err)
7086{
7087 struct btrfs_dio_private *dip = bio->bi_private;
7088
7089 if (err) {
Frank Holtonefe120a2013-12-20 11:37:06 -05007090 btrfs_err(BTRFS_I(dip->inode)->root->fs_info,
7091 "direct IO failed ino %llu rw %lu sector %#Lx len %u err no %d",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02007092 btrfs_ino(dip->inode), bio->bi_rw,
Jan Beulich3dd14622010-12-07 14:54:09 +00007093 (unsigned long long)bio->bi_sector, bio->bi_size, err);
Miao Xiee65e1532010-11-22 03:04:43 +00007094 dip->errors = 1;
7095
7096 /*
7097 * before atomic variable goto zero, we must make sure
7098 * dip->errors is perceived to be set.
7099 */
7100 smp_mb__before_atomic_dec();
7101 }
7102
7103 /* if there are more bios still pending for this dio, just exit */
7104 if (!atomic_dec_and_test(&dip->pending_bios))
7105 goto out;
7106
Chris Mason9be33952013-05-17 18:30:14 -04007107 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00007108 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04007109 } else {
7110 set_bit(BIO_UPTODATE, &dip->dio_bio->bi_flags);
Miao Xiee65e1532010-11-22 03:04:43 +00007111 bio_endio(dip->orig_bio, 0);
7112 }
7113out:
7114 bio_put(bio);
7115}
7116
7117static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
7118 u64 first_sector, gfp_t gfp_flags)
7119{
7120 int nr_vecs = bio_get_nr_vecs(bdev);
7121 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
7122}
7123
7124static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
7125 int rw, u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007126 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00007127{
Miao Xiefacc8a22013-07-25 19:22:34 +08007128 struct btrfs_dio_private *dip = bio->bi_private;
Miao Xiee65e1532010-11-22 03:04:43 +00007129 int write = rw & REQ_WRITE;
7130 struct btrfs_root *root = BTRFS_I(inode)->root;
7131 int ret;
7132
Josef Bacikb812ce22012-11-16 13:56:32 -05007133 if (async_submit)
7134 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
7135
Miao Xiee65e1532010-11-22 03:04:43 +00007136 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04007137
7138 if (!write) {
7139 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
7140 if (ret)
7141 goto err;
7142 }
Miao Xiee65e1532010-11-22 03:04:43 +00007143
Josef Bacik1ae39932011-04-06 14:41:34 -04007144 if (skip_sum)
7145 goto map;
7146
7147 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00007148 ret = btrfs_wq_submit_bio(root->fs_info,
7149 inode, rw, bio, 0, 0,
7150 file_offset,
7151 __btrfs_submit_bio_start_direct_io,
7152 __btrfs_submit_bio_done);
7153 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04007154 } else if (write) {
7155 /*
7156 * If we aren't doing async submit, calculate the csum of the
7157 * bio now.
7158 */
7159 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
7160 if (ret)
7161 goto err;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007162 } else if (!skip_sum) {
Miao Xiefacc8a22013-07-25 19:22:34 +08007163 ret = btrfs_lookup_bio_sums_dio(root, inode, dip, bio,
7164 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007165 if (ret)
7166 goto err;
7167 }
Miao Xiee65e1532010-11-22 03:04:43 +00007168
Josef Bacik1ae39932011-04-06 14:41:34 -04007169map:
7170 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007171err:
7172 bio_put(bio);
7173 return ret;
7174}
7175
7176static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
7177 int skip_sum)
7178{
7179 struct inode *inode = dip->inode;
7180 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00007181 struct bio *bio;
7182 struct bio *orig_bio = dip->orig_bio;
7183 struct bio_vec *bvec = orig_bio->bi_io_vec;
7184 u64 start_sector = orig_bio->bi_sector;
7185 u64 file_offset = dip->logical_offset;
7186 u64 submit_len = 0;
7187 u64 map_length;
7188 int nr_pages = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007189 int ret = 0;
Josef Bacik1ae39932011-04-06 14:41:34 -04007190 int async_submit = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007191
Miao Xiee65e1532010-11-22 03:04:43 +00007192 map_length = orig_bio->bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007193 ret = btrfs_map_block(root->fs_info, rw, start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007194 &map_length, NULL, 0);
7195 if (ret) {
Josef Bacik64728bb2011-04-25 19:43:52 -04007196 bio_put(orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00007197 return -EIO;
7198 }
Miao Xiefacc8a22013-07-25 19:22:34 +08007199
Josef Bacik02f57c72011-04-06 14:25:44 -04007200 if (map_length >= orig_bio->bi_size) {
7201 bio = orig_bio;
7202 goto submit;
7203 }
7204
David Woodhouse53b381b2013-01-29 18:40:14 -05007205 /* async crcs make it difficult to collect full stripe writes. */
7206 if (btrfs_get_alloc_profile(root, 1) &
7207 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6))
7208 async_submit = 0;
7209 else
7210 async_submit = 1;
7211
Josef Bacik02f57c72011-04-06 14:25:44 -04007212 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
7213 if (!bio)
7214 return -ENOMEM;
7215 bio->bi_private = dip;
7216 bio->bi_end_io = btrfs_end_dio_bio;
7217 atomic_inc(&dip->pending_bios);
7218
Miao Xiee65e1532010-11-22 03:04:43 +00007219 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
7220 if (unlikely(map_length < submit_len + bvec->bv_len ||
7221 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
7222 bvec->bv_offset) < bvec->bv_len)) {
7223 /*
7224 * inc the count before we submit the bio so
7225 * we know the end IO handler won't happen before
7226 * we inc the count. Otherwise, the dip might get freed
7227 * before we're done setting it up
7228 */
7229 atomic_inc(&dip->pending_bios);
7230 ret = __btrfs_submit_dio_bio(bio, inode, rw,
7231 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007232 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007233 if (ret) {
7234 bio_put(bio);
7235 atomic_dec(&dip->pending_bios);
7236 goto out_err;
7237 }
7238
Miao Xiee65e1532010-11-22 03:04:43 +00007239 start_sector += submit_len >> 9;
7240 file_offset += submit_len;
7241
7242 submit_len = 0;
7243 nr_pages = 0;
7244
7245 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
7246 start_sector, GFP_NOFS);
7247 if (!bio)
7248 goto out_err;
7249 bio->bi_private = dip;
7250 bio->bi_end_io = btrfs_end_dio_bio;
7251
7252 map_length = orig_bio->bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007253 ret = btrfs_map_block(root->fs_info, rw,
Stefan Behrens3ec706c2012-11-05 15:46:42 +01007254 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007255 &map_length, NULL, 0);
7256 if (ret) {
7257 bio_put(bio);
7258 goto out_err;
7259 }
7260 } else {
7261 submit_len += bvec->bv_len;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307262 nr_pages++;
Miao Xiee65e1532010-11-22 03:04:43 +00007263 bvec++;
7264 }
7265 }
7266
Josef Bacik02f57c72011-04-06 14:25:44 -04007267submit:
Miao Xiee65e1532010-11-22 03:04:43 +00007268 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007269 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007270 if (!ret)
7271 return 0;
7272
7273 bio_put(bio);
7274out_err:
7275 dip->errors = 1;
7276 /*
7277 * before atomic variable goto zero, we must
7278 * make sure dip->errors is perceived to be set.
7279 */
7280 smp_mb__before_atomic_dec();
7281 if (atomic_dec_and_test(&dip->pending_bios))
7282 bio_io_error(dip->orig_bio);
7283
7284 /* bio_end_io() will handle error, so we needn't return it */
7285 return 0;
7286}
7287
Chris Mason9be33952013-05-17 18:30:14 -04007288static void btrfs_submit_direct(int rw, struct bio *dio_bio,
7289 struct inode *inode, loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007290{
7291 struct btrfs_root *root = BTRFS_I(inode)->root;
7292 struct btrfs_dio_private *dip;
Chris Mason9be33952013-05-17 18:30:14 -04007293 struct bio *io_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007294 int skip_sum;
Miao Xiefacc8a22013-07-25 19:22:34 +08007295 int sum_len;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02007296 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007297 int ret = 0;
Miao Xiefacc8a22013-07-25 19:22:34 +08007298 u16 csum_size;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007299
7300 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
7301
Chris Mason9be33952013-05-17 18:30:14 -04007302 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04007303 if (!io_bio) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007304 ret = -ENOMEM;
7305 goto free_ordered;
7306 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007307
Miao Xiefacc8a22013-07-25 19:22:34 +08007308 if (!skip_sum && !write) {
7309 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
7310 sum_len = dio_bio->bi_size >> inode->i_sb->s_blocksize_bits;
7311 sum_len *= csum_size;
7312 } else {
7313 sum_len = 0;
7314 }
7315
7316 dip = kmalloc(sizeof(*dip) + sum_len, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04007317 if (!dip) {
7318 ret = -ENOMEM;
7319 goto free_io_bio;
7320 }
7321
7322 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007323 dip->inode = inode;
7324 dip->logical_offset = file_offset;
Miao Xiee6da5d22013-06-19 18:19:17 +08007325 dip->bytes = dio_bio->bi_size;
Chris Mason9be33952013-05-17 18:30:14 -04007326 dip->disk_bytenr = (u64)dio_bio->bi_sector << 9;
7327 io_bio->bi_private = dip;
Miao Xiee65e1532010-11-22 03:04:43 +00007328 dip->errors = 0;
Chris Mason9be33952013-05-17 18:30:14 -04007329 dip->orig_bio = io_bio;
7330 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00007331 atomic_set(&dip->pending_bios, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007332
7333 if (write)
Chris Mason9be33952013-05-17 18:30:14 -04007334 io_bio->bi_end_io = btrfs_endio_direct_write;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007335 else
Chris Mason9be33952013-05-17 18:30:14 -04007336 io_bio->bi_end_io = btrfs_endio_direct_read;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007337
Miao Xiee65e1532010-11-22 03:04:43 +00007338 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
7339 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04007340 return;
Chris Mason9be33952013-05-17 18:30:14 -04007341
7342free_io_bio:
7343 bio_put(io_bio);
7344
Josef Bacik4b46fce2010-05-23 11:00:55 -04007345free_ordered:
7346 /*
7347 * If this is a write, we need to clean up the reserved space and kill
7348 * the ordered extent.
7349 */
7350 if (write) {
7351 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05007352 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007353 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
7354 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
7355 btrfs_free_reserved_extent(root, ordered->start,
7356 ordered->disk_len);
7357 btrfs_put_ordered_extent(ordered);
7358 btrfs_put_ordered_extent(ordered);
7359 }
Chris Mason9be33952013-05-17 18:30:14 -04007360 bio_endio(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007361}
7362
Chris Mason5a5f79b2010-05-26 21:33:37 -04007363static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
7364 const struct iovec *iov, loff_t offset,
7365 unsigned long nr_segs)
7366{
7367 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00007368 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007369 size_t size;
7370 unsigned long addr;
7371 unsigned blocksize_mask = root->sectorsize - 1;
7372 ssize_t retval = -EINVAL;
7373 loff_t end = offset;
7374
7375 if (offset & blocksize_mask)
7376 goto out;
7377
7378 /* Check the memory alignment. Blocks cannot straddle pages */
7379 for (seg = 0; seg < nr_segs; seg++) {
7380 addr = (unsigned long)iov[seg].iov_base;
7381 size = iov[seg].iov_len;
7382 end += size;
Josef Bacika1b75f72011-04-08 15:51:18 +00007383 if ((addr & blocksize_mask) || (size & blocksize_mask))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007384 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00007385
7386 /* If this is a write we don't need to check anymore */
7387 if (rw & WRITE)
7388 continue;
7389
7390 /*
7391 * Check to make sure we don't have duplicate iov_base's in this
7392 * iovec, if so return EINVAL, otherwise we'll get csum errors
7393 * when reading back.
7394 */
7395 for (i = seg + 1; i < nr_segs; i++) {
7396 if (iov[seg].iov_base == iov[i].iov_base)
7397 goto out;
7398 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04007399 }
7400 retval = 0;
7401out:
7402 return retval;
7403}
Josef Bacikeb838e72012-07-31 16:28:48 -04007404
Chris Mason16432982008-04-10 10:23:21 -04007405static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
7406 const struct iovec *iov, loff_t offset,
7407 unsigned long nr_segs)
7408{
Josef Bacik4b46fce2010-05-23 11:00:55 -04007409 struct file *file = iocb->ki_filp;
7410 struct inode *inode = file->f_mapping->host;
Miao Xie09348562013-02-07 10:12:07 +00007411 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00007412 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00007413 bool wakeup = true;
7414 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00007415 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007416
Chris Mason5a5f79b2010-05-26 21:33:37 -04007417 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
Josef Bacikeb838e72012-07-31 16:28:48 -04007418 offset, nr_segs))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007419 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007420
Miao Xie38851cc2013-02-08 07:04:11 +00007421 atomic_inc(&inode->i_dio_count);
7422 smp_mb__after_atomic_inc();
7423
Josef Bacik0e267c42013-07-02 10:38:02 -04007424 /*
7425 * The generic stuff only does filemap_write_and_wait_range, which isn't
7426 * enough if we've written compressed pages to this area, so we need to
7427 * call btrfs_wait_ordered_range to make absolutely sure that any
7428 * outstanding dirty pages are on disk.
7429 */
7430 count = iov_length(iov, nr_segs);
Josef Bacik0ef8b722013-10-25 16:13:35 -04007431 ret = btrfs_wait_ordered_range(inode, offset, count);
7432 if (ret)
7433 return ret;
Josef Bacik0e267c42013-07-02 10:38:02 -04007434
Miao Xie09348562013-02-07 10:12:07 +00007435 if (rw & WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00007436 /*
7437 * If the write DIO is beyond the EOF, we need update
7438 * the isize, but it is protected by i_mutex. So we can
7439 * not unlock the i_mutex at this case.
7440 */
7441 if (offset + count <= inode->i_size) {
7442 mutex_unlock(&inode->i_mutex);
7443 relock = true;
7444 }
Miao Xie09348562013-02-07 10:12:07 +00007445 ret = btrfs_delalloc_reserve_space(inode, count);
7446 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00007447 goto out;
7448 } else if (unlikely(test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
7449 &BTRFS_I(inode)->runtime_flags))) {
7450 inode_dio_done(inode);
7451 flags = DIO_LOCKING | DIO_SKIP_HOLES;
7452 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00007453 }
7454
7455 ret = __blockdev_direct_IO(rw, iocb, inode,
7456 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
7457 iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
Miao Xie2e60a512013-02-08 07:01:08 +00007458 btrfs_submit_direct, flags);
Miao Xie09348562013-02-07 10:12:07 +00007459 if (rw & WRITE) {
7460 if (ret < 0 && ret != -EIOCBQUEUED)
7461 btrfs_delalloc_release_space(inode, count);
Miao Xie172a5042013-02-21 02:48:22 -07007462 else if (ret >= 0 && (size_t)ret < count)
Miao Xie09348562013-02-07 10:12:07 +00007463 btrfs_delalloc_release_space(inode,
7464 count - (size_t)ret);
Miao Xie172a5042013-02-21 02:48:22 -07007465 else
7466 btrfs_delalloc_release_metadata(inode, 0);
Miao Xie09348562013-02-07 10:12:07 +00007467 }
Miao Xie38851cc2013-02-08 07:04:11 +00007468out:
Miao Xie2e60a512013-02-08 07:01:08 +00007469 if (wakeup)
7470 inode_dio_done(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00007471 if (relock)
7472 mutex_lock(&inode->i_mutex);
Miao Xie09348562013-02-07 10:12:07 +00007473
7474 return ret;
Chris Mason16432982008-04-10 10:23:21 -04007475}
7476
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007477#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
7478
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007479static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
7480 __u64 start, __u64 len)
7481{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007482 int ret;
7483
7484 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
7485 if (ret)
7486 return ret;
7487
Chris Masonec29ed52011-02-23 16:23:20 -05007488 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007489}
7490
Chris Mason9ebefb182007-06-15 13:50:00 -04007491int btrfs_readpage(struct file *file, struct page *page)
7492{
Chris Masond1310b22008-01-24 16:13:08 -05007493 struct extent_io_tree *tree;
7494 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02007495 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04007496}
Chris Mason1832a6d2007-12-21 16:27:21 -05007497
Chris Mason39279cc2007-06-12 06:35:45 -04007498static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
7499{
Chris Masond1310b22008-01-24 16:13:08 -05007500 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04007501
7502
7503 if (current->flags & PF_MEMALLOC) {
7504 redirty_page_for_writepage(wbc, page);
7505 unlock_page(page);
7506 return 0;
7507 }
Chris Masond1310b22008-01-24 16:13:08 -05007508 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04007509 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
7510}
Chris Mason39279cc2007-06-12 06:35:45 -04007511
Eric Sandeen48a3b632013-04-25 20:41:01 +00007512static int btrfs_writepages(struct address_space *mapping,
7513 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04007514{
Chris Masond1310b22008-01-24 16:13:08 -05007515 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05007516
Chris Masond1310b22008-01-24 16:13:08 -05007517 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04007518 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
7519}
7520
Chris Mason3ab2fb52007-11-08 10:59:22 -05007521static int
7522btrfs_readpages(struct file *file, struct address_space *mapping,
7523 struct list_head *pages, unsigned nr_pages)
7524{
Chris Masond1310b22008-01-24 16:13:08 -05007525 struct extent_io_tree *tree;
7526 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05007527 return extent_readpages(tree, mapping, pages, nr_pages,
7528 btrfs_get_extent);
7529}
Chris Masone6dcd2d2008-07-17 12:53:50 -04007530static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04007531{
Chris Masond1310b22008-01-24 16:13:08 -05007532 struct extent_io_tree *tree;
7533 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04007534 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04007535
Chris Masond1310b22008-01-24 16:13:08 -05007536 tree = &BTRFS_I(page->mapping->host)->io_tree;
7537 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05007538 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04007539 if (ret == 1) {
7540 ClearPagePrivate(page);
7541 set_page_private(page, 0);
7542 page_cache_release(page);
7543 }
7544 return ret;
7545}
Chris Mason39279cc2007-06-12 06:35:45 -04007546
Chris Masone6dcd2d2008-07-17 12:53:50 -04007547static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
7548{
Chris Mason98509cf2008-09-11 15:51:43 -04007549 if (PageWriteback(page) || PageDirty(page))
7550 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05007551 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007552}
7553
Lukas Czernerd47992f2013-05-21 23:17:23 -04007554static void btrfs_invalidatepage(struct page *page, unsigned int offset,
7555 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04007556{
Josef Bacik5fd02042012-05-02 14:00:54 -04007557 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05007558 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007559 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007560 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007561 u64 page_start = page_offset(page);
7562 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00007563 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04007564
Chris Mason8b62b722009-09-02 16:53:46 -04007565 /*
7566 * we have the page locked, so new writeback can't start,
7567 * and the dirty bit won't be cleared while we are here.
7568 *
7569 * Wait for IO on this page so that we can safely clear
7570 * the PagePrivate2 bit and do ordered accounting
7571 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007572 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04007573
Josef Bacik5fd02042012-05-02 14:00:54 -04007574 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007575 if (offset) {
7576 btrfs_releasepage(page, GFP_NOFS);
7577 return;
7578 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00007579
7580 if (!inode_evicting)
7581 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
7582 ordered = btrfs_lookup_ordered_extent(inode, page_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007583 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04007584 /*
7585 * IO on this page will never be started, so we need
7586 * to account for any ordered extents now
7587 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00007588 if (!inode_evicting)
7589 clear_extent_bit(tree, page_start, page_end,
7590 EXTENT_DIRTY | EXTENT_DELALLOC |
7591 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
7592 EXTENT_DEFRAG, 1, 0, &cached_state,
7593 GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04007594 /*
7595 * whoever cleared the private bit is responsible
7596 * for the finish_ordered_io
7597 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04007598 if (TestClearPagePrivate2(page)) {
7599 struct btrfs_ordered_inode_tree *tree;
7600 u64 new_len;
7601
7602 tree = &BTRFS_I(inode)->ordered_tree;
7603
7604 spin_lock_irq(&tree->lock);
7605 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
7606 new_len = page_start - ordered->file_offset;
7607 if (new_len < ordered->truncated_len)
7608 ordered->truncated_len = new_len;
7609 spin_unlock_irq(&tree->lock);
7610
7611 if (btrfs_dec_test_ordered_pending(inode, &ordered,
7612 page_start,
7613 PAGE_CACHE_SIZE, 1))
7614 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04007615 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007616 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00007617 if (!inode_evicting) {
7618 cached_state = NULL;
7619 lock_extent_bits(tree, page_start, page_end, 0,
7620 &cached_state);
7621 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007622 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00007623
7624 if (!inode_evicting) {
7625 clear_extent_bit(tree, page_start, page_end,
7626 EXTENT_LOCKED | EXTENT_DIRTY |
7627 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
7628 EXTENT_DEFRAG, 1, 1,
7629 &cached_state, GFP_NOFS);
7630
7631 __btrfs_releasepage(page, GFP_NOFS);
7632 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007633
Chris Mason4a096752008-07-21 10:29:44 -04007634 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04007635 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04007636 ClearPagePrivate(page);
7637 set_page_private(page, 0);
7638 page_cache_release(page);
7639 }
Chris Mason39279cc2007-06-12 06:35:45 -04007640}
7641
Chris Mason9ebefb182007-06-15 13:50:00 -04007642/*
7643 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
7644 * called from a page fault handler when a page is first dirtied. Hence we must
7645 * be careful to check for EOF conditions here. We set the page up correctly
7646 * for a written page which means we get ENOSPC checking when writing into
7647 * holes and correct delalloc and unwritten extent mapping on filesystems that
7648 * support these features.
7649 *
7650 * We are not allowed to take the i_mutex here so we have to play games to
7651 * protect against truncate races as the page could now be beyond EOF. Because
7652 * vmtruncate() writes the inode size before removing pages, once we have the
7653 * page lock we can determine safely if the page is beyond EOF. If it is not
7654 * beyond EOF, then the page is guaranteed safe against truncation until we
7655 * unlock the page.
7656 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07007657int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04007658{
Nick Pigginc2ec1752009-03-31 15:23:21 -07007659 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05007660 struct inode *inode = file_inode(vma->vm_file);
Chris Mason1832a6d2007-12-21 16:27:21 -05007661 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007662 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7663 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007664 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007665 char *kaddr;
7666 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04007667 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05007668 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05007669 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007670 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007671 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04007672
Jan Karab2b5ef52012-06-12 16:20:45 +02007673 sb_start_pagefault(inode->i_sb);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04007674 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007675 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04007676 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05007677 reserved = 1;
7678 }
Nick Piggin56a76f82009-03-31 15:23:23 -07007679 if (ret) {
7680 if (ret == -ENOMEM)
7681 ret = VM_FAULT_OOM;
7682 else /* -ENOSPC, -EIO, etc */
7683 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05007684 if (reserved)
7685 goto out;
7686 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07007687 }
Chris Mason1832a6d2007-12-21 16:27:21 -05007688
Nick Piggin56a76f82009-03-31 15:23:23 -07007689 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007690again:
Chris Mason9ebefb182007-06-15 13:50:00 -04007691 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04007692 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007693 page_start = page_offset(page);
7694 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04007695
Chris Mason9ebefb182007-06-15 13:50:00 -04007696 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04007697 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04007698 /* page got truncated out from underneath us */
7699 goto out_unlock;
7700 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007701 wait_on_page_writeback(page);
7702
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007703 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007704 set_page_extent_mapped(page);
7705
Chris Masoneb84ae02008-07-17 13:53:27 -04007706 /*
7707 * we can't set the delalloc bits if there are pending ordered
7708 * extents. Drop our locks and wait for them to finish
7709 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007710 ordered = btrfs_lookup_ordered_extent(inode, page_start);
7711 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007712 unlock_extent_cached(io_tree, page_start, page_end,
7713 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007714 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04007715 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007716 btrfs_put_ordered_extent(ordered);
7717 goto again;
7718 }
7719
Josef Bacikfbf19082009-10-01 17:10:23 -04007720 /*
7721 * XXX - page_mkwrite gets called every time the page is dirtied, even
7722 * if it was already dirty, so for space accounting reasons we need to
7723 * clear any delalloc bits for the range we are fixing to save. There
7724 * is probably a better way to do this, but for now keep consistent with
7725 * prepare_pages in the normal write path.
7726 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00007727 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06007728 EXTENT_DIRTY | EXTENT_DELALLOC |
7729 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00007730 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04007731
Josef Bacik2ac55d42010-02-03 19:33:23 +00007732 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
7733 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007734 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007735 unlock_extent_cached(io_tree, page_start, page_end,
7736 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007737 ret = VM_FAULT_SIGBUS;
7738 goto out_unlock;
7739 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007740 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04007741
7742 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04007743 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04007744 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04007745 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04007746 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04007747
Chris Masone6dcd2d2008-07-17 12:53:50 -04007748 if (zero_start != PAGE_CACHE_SIZE) {
7749 kaddr = kmap(page);
7750 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
7751 flush_dcache_page(page);
7752 kunmap(page);
7753 }
Chris Mason247e7432008-07-17 12:53:51 -04007754 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007755 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04007756 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007757
Chris Mason257c62e2009-10-13 13:21:08 -04007758 BTRFS_I(inode)->last_trans = root->fs_info->generation;
7759 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06007760 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04007761
Josef Bacik2ac55d42010-02-03 19:33:23 +00007762 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04007763
7764out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02007765 if (!ret) {
7766 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04007767 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02007768 }
Chris Mason9ebefb182007-06-15 13:50:00 -04007769 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05007770out:
Josef Bacikec39e182012-01-12 19:10:12 -05007771 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007772out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02007773 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04007774 return ret;
7775}
7776
Josef Bacika41ad392011-01-31 15:30:16 -05007777static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04007778{
7779 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04007780 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04007781 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05007782 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007783 struct btrfs_trans_handle *trans;
Chris Masondbe674a2008-07-17 12:54:05 -04007784 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04007785 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04007786
Josef Bacik0ef8b722013-10-25 16:13:35 -04007787 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
7788 (u64)-1);
7789 if (ret)
7790 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04007791
Josef Bacikfcb80c22011-05-03 10:40:22 -04007792 /*
7793 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
7794 * 3 things going on here
7795 *
7796 * 1) We need to reserve space for our orphan item and the space to
7797 * delete our orphan item. Lord knows we don't want to have a dangling
7798 * orphan item because we didn't reserve space to remove it.
7799 *
7800 * 2) We need to reserve space to update our inode.
7801 *
7802 * 3) We need to have something to cache all the space that is going to
7803 * be free'd up by the truncate operation, but also have some slack
7804 * space reserved in case it uses space during the truncate (thank you
7805 * very much snapshotting).
7806 *
7807 * And we need these to all be seperate. The fact is we can use alot of
7808 * space doing the truncate, and we have no earthly idea how much space
7809 * we will use, so we need the truncate reservation to be seperate so it
7810 * doesn't end up using space reserved for updating the inode or
7811 * removing the orphan item. We also need to be able to stop the
7812 * transaction and start a new one, which means we need to be able to
7813 * update the inode several times, and we have no idea of knowing how
7814 * many times that will be, so we can't just reserve 1 item for the
7815 * entirety of the opration, so that has to be done seperately as well.
7816 * Then there is the orphan item, which does indeed need to be held on
7817 * to for the whole operation, and we need nobody to touch this reserved
7818 * space except the orphan code.
7819 *
7820 * So that leaves us with
7821 *
7822 * 1) root->orphan_block_rsv - for the orphan deletion.
7823 * 2) rsv - for the truncate reservation, which we will steal from the
7824 * transaction reservation.
7825 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
7826 * updating the inode.
7827 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06007828 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007829 if (!rsv)
7830 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04007831 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04007832 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05007833
Josef Bacik907cbce2011-08-08 13:46:15 -04007834 /*
Josef Bacik07127182011-08-19 10:29:59 -04007835 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04007836 * 1 for updating the inode.
7837 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05007838 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007839 if (IS_ERR(trans)) {
7840 err = PTR_ERR(trans);
7841 goto out;
7842 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05007843
Josef Bacik907cbce2011-08-08 13:46:15 -04007844 /* Migrate the slack space for the truncate to our reserve */
7845 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
7846 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007847 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05007848
Chris Mason5a3f23d2009-03-31 13:27:11 -04007849 /*
7850 * setattr is responsible for setting the ordered_data_close flag,
7851 * but that is only tested during the last file release. That
7852 * could happen well after the next commit, leaving a great big
7853 * window where new writes may get lost if someone chooses to write
7854 * to this file after truncating to zero
7855 *
7856 * The inode doesn't have any dirty data here, and so if we commit
7857 * this is a noop. If someone immediately starts writing to the inode
7858 * it is very likely we'll catch some of their writes in this
7859 * transaction, and the commit will find this file on the ordered
7860 * data list with good things to send down.
7861 *
7862 * This is a best effort solution, there is still a window where
7863 * using truncate to replace the contents of the file will
7864 * end up with a zero length file after a crash.
7865 */
Josef Bacik72ac3c02012-05-23 14:13:11 -04007866 if (inode->i_size == 0 && test_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
7867 &BTRFS_I(inode)->runtime_flags))
Chris Mason5a3f23d2009-03-31 13:27:11 -04007868 btrfs_add_ordered_operation(trans, root, inode);
7869
Josef Bacik5dc562c2012-08-17 13:14:17 -04007870 /*
7871 * So if we truncate and then write and fsync we normally would just
7872 * write the extents that changed, which is a problem if we need to
7873 * first truncate that entire inode. So set this flag so we write out
7874 * all of the extents in the inode to the sync log so we're completely
7875 * safe.
7876 */
7877 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007878 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04007879
Yan, Zheng80825102009-11-12 09:35:36 +00007880 while (1) {
7881 ret = btrfs_truncate_inode_items(trans, root, inode,
7882 inode->i_size,
7883 BTRFS_EXTENT_DATA_KEY);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007884 if (ret != -ENOSPC) {
Josef Bacik3893e332011-01-31 16:03:11 -05007885 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007886 break;
Josef Bacik3893e332011-01-31 16:03:11 -05007887 }
Chris Mason39279cc2007-06-12 06:35:45 -04007888
Josef Bacikfcb80c22011-05-03 10:40:22 -04007889 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007890 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007891 if (ret) {
7892 err = ret;
7893 break;
7894 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04007895
Yan, Zheng80825102009-11-12 09:35:36 +00007896 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007897 btrfs_btree_balance_dirty(root);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007898
7899 trans = btrfs_start_transaction(root, 2);
7900 if (IS_ERR(trans)) {
7901 ret = err = PTR_ERR(trans);
7902 trans = NULL;
7903 break;
7904 }
7905
7906 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
7907 rsv, min_size);
7908 BUG_ON(ret); /* shouldn't happen */
7909 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007910 }
7911
7912 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04007913 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007914 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007915 if (ret)
7916 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007917 }
7918
Chris Mason917c16b2011-11-08 14:49:59 -05007919 if (trans) {
7920 trans->block_rsv = &root->fs_info->trans_block_rsv;
7921 ret = btrfs_update_inode(trans, root, inode);
7922 if (ret && !err)
7923 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04007924
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007925 ret = btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007926 btrfs_btree_balance_dirty(root);
Chris Mason917c16b2011-11-08 14:49:59 -05007927 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04007928
7929out:
7930 btrfs_free_block_rsv(root, rsv);
7931
Josef Bacik3893e332011-01-31 16:03:11 -05007932 if (ret && !err)
7933 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05007934
Josef Bacik3893e332011-01-31 16:03:11 -05007935 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007936}
7937
Sven Wegener3b963622008-06-09 21:57:42 -04007938/*
Chris Masond352ac62008-09-29 15:18:18 -04007939 * create a new subvolume directory/inode (helper for the ioctl).
7940 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05007941int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00007942 struct btrfs_root *new_root,
7943 struct btrfs_root *parent_root,
7944 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04007945{
Chris Mason39279cc2007-06-12 06:35:45 -04007946 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04007947 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007948 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007949
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01007950 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
7951 new_dirid, new_dirid,
7952 S_IFDIR | (~current_umask() & S_IRWXUGO),
7953 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04007954 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04007955 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007956 inode->i_op = &btrfs_dir_inode_operations;
7957 inode->i_fop = &btrfs_dir_file_operations;
7958
Miklos Szeredibfe86842011-10-28 14:13:29 +02007959 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04007960 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04007961
Filipe David Borba Manana63541922014-01-07 11:47:46 +00007962 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
7963 if (err)
7964 btrfs_err(new_root->fs_info,
7965 "error inheriting subvolume %llu properties: %d\n",
7966 new_root->root_key.objectid, err);
7967
Yan, Zheng76dda932009-09-21 16:00:26 -04007968 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04007969
Yan, Zheng76dda932009-09-21 16:00:26 -04007970 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07007971 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007972}
7973
Chris Mason39279cc2007-06-12 06:35:45 -04007974struct inode *btrfs_alloc_inode(struct super_block *sb)
7975{
7976 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007977 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007978
7979 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
7980 if (!ei)
7981 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007982
7983 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007984 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04007985 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04007986 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04007987 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007988 ei->delalloc_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007989 ei->disk_i_size = 0;
7990 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04007991 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007992 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08007993 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007994 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06007995 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007996
Josef Bacik9e0baf62011-07-15 15:16:44 +00007997 spin_lock_init(&ei->lock);
7998 ei->outstanding_extents = 0;
7999 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008000
Josef Bacik72ac3c02012-05-23 14:13:11 -04008001 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08008002 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008003
Miao Xie16cdcec2011-04-22 18:12:22 +08008004 ei->delayed_node = NULL;
8005
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008006 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02008007 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02008008 extent_io_tree_init(&ei->io_tree, &inode->i_data);
8009 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04008010 ei->io_tree.track_uptodate = 1;
8011 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05008012 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008013 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05008014 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008015 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008016 INIT_LIST_HEAD(&ei->delalloc_inodes);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008017 INIT_LIST_HEAD(&ei->ordered_operations);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008018 RB_CLEAR_NODE(&ei->rb_node);
8019
8020 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008021}
8022
Josef Bacikaaedb552013-10-11 14:44:09 -04008023#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
8024void btrfs_test_destroy_inode(struct inode *inode)
8025{
8026 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
8027 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8028}
8029#endif
8030
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008031static void btrfs_i_callback(struct rcu_head *head)
8032{
8033 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008034 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8035}
8036
Chris Mason39279cc2007-06-12 06:35:45 -04008037void btrfs_destroy_inode(struct inode *inode)
8038{
Chris Masone6dcd2d2008-07-17 12:53:50 -04008039 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008040 struct btrfs_root *root = BTRFS_I(inode)->root;
8041
Al Virob3d9b7a2012-06-09 13:51:19 -04008042 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04008043 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00008044 WARN_ON(BTRFS_I(inode)->outstanding_extents);
8045 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04008046 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
8047 WARN_ON(BTRFS_I(inode)->csum_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04008048
Chris Mason5a3f23d2009-03-31 13:27:11 -04008049 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05008050 * This can happen where we create an inode, but somebody else also
8051 * created the same inode and we need to destroy the one we already
8052 * created.
8053 */
8054 if (!root)
8055 goto free;
8056
8057 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04008058 * Make sure we're properly removed from the ordered operation
8059 * lists.
8060 */
8061 smp_mb();
8062 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
Miao Xie199c2a92013-05-15 07:48:23 +00008063 spin_lock(&root->fs_info->ordered_root_lock);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008064 list_del_init(&BTRFS_I(inode)->ordered_operations);
Miao Xie199c2a92013-05-15 07:48:23 +00008065 spin_unlock(&root->fs_info->ordered_root_lock);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008066 }
8067
Josef Bacik8a35d952012-05-23 14:26:42 -04008068 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
8069 &BTRFS_I(inode)->runtime_flags)) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008070 btrfs_info(root->fs_info, "inode %llu still on the orphan list",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02008071 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04008072 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04008073 }
Josef Bacik7b128762008-07-24 12:17:14 -04008074
Chris Masond3977122009-01-05 21:25:51 -05008075 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008076 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
8077 if (!ordered)
8078 break;
8079 else {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008080 btrfs_err(root->fs_info, "found ordered extent %llu %llu on inode cleanup",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02008081 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008082 btrfs_remove_ordered_extent(inode, ordered);
8083 btrfs_put_ordered_extent(ordered);
8084 btrfs_put_ordered_extent(ordered);
8085 }
8086 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008087 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008088 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05008089free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008090 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04008091}
8092
Al Viro45321ac2010-06-07 13:43:19 -04008093int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04008094{
8095 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04008096
Naohiro Aota6379ef92013-06-06 09:56:34 +00008097 if (root == NULL)
8098 return 1;
8099
Liu Bofa6ac872013-02-20 14:10:23 +00008100 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02008101 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04008102 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04008103 else
Al Viro45321ac2010-06-07 13:43:19 -04008104 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04008105}
8106
Sven Wegener0ee0fda2008-07-30 16:54:26 -04008107static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04008108{
8109 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
8110
8111 inode_init_once(&ei->vfs_inode);
8112}
8113
8114void btrfs_destroy_cachep(void)
8115{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10008116 /*
8117 * Make sure all delayed rcu free inodes are flushed before we
8118 * destroy cache.
8119 */
8120 rcu_barrier();
Chris Mason39279cc2007-06-12 06:35:45 -04008121 if (btrfs_inode_cachep)
8122 kmem_cache_destroy(btrfs_inode_cachep);
8123 if (btrfs_trans_handle_cachep)
8124 kmem_cache_destroy(btrfs_trans_handle_cachep);
8125 if (btrfs_transaction_cachep)
8126 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008127 if (btrfs_path_cachep)
8128 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05008129 if (btrfs_free_space_cachep)
8130 kmem_cache_destroy(btrfs_free_space_cachep);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008131 if (btrfs_delalloc_work_cachep)
8132 kmem_cache_destroy(btrfs_delalloc_work_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008133}
8134
8135int btrfs_init_cachep(void)
8136{
David Sterba837e1972012-09-07 03:00:48 -06008137 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008138 sizeof(struct btrfs_inode), 0,
8139 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04008140 if (!btrfs_inode_cachep)
8141 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008142
David Sterba837e1972012-09-07 03:00:48 -06008143 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008144 sizeof(struct btrfs_trans_handle), 0,
8145 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008146 if (!btrfs_trans_handle_cachep)
8147 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008148
David Sterba837e1972012-09-07 03:00:48 -06008149 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008150 sizeof(struct btrfs_transaction), 0,
8151 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008152 if (!btrfs_transaction_cachep)
8153 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008154
David Sterba837e1972012-09-07 03:00:48 -06008155 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008156 sizeof(struct btrfs_path), 0,
8157 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008158 if (!btrfs_path_cachep)
8159 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008160
David Sterba837e1972012-09-07 03:00:48 -06008161 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05008162 sizeof(struct btrfs_free_space), 0,
8163 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
8164 if (!btrfs_free_space_cachep)
8165 goto fail;
8166
Miao Xie8ccf6f192012-10-25 09:28:04 +00008167 btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
8168 sizeof(struct btrfs_delalloc_work), 0,
8169 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
8170 NULL);
8171 if (!btrfs_delalloc_work_cachep)
8172 goto fail;
8173
Chris Mason39279cc2007-06-12 06:35:45 -04008174 return 0;
8175fail:
8176 btrfs_destroy_cachep();
8177 return -ENOMEM;
8178}
8179
8180static int btrfs_getattr(struct vfsmount *mnt,
8181 struct dentry *dentry, struct kstat *stat)
8182{
Miao Xiedf0af1a2013-01-29 10:11:59 +00008183 u64 delalloc_bytes;
Chris Mason39279cc2007-06-12 06:35:45 -04008184 struct inode *inode = dentry->d_inode;
David Sterbafadc0d82011-11-20 07:33:38 -05008185 u32 blocksize = inode->i_sb->s_blocksize;
8186
Chris Mason39279cc2007-06-12 06:35:45 -04008187 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04008188 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05008189 stat->blksize = PAGE_CACHE_SIZE;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008190
8191 spin_lock(&BTRFS_I(inode)->lock);
8192 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
8193 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05008194 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00008195 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04008196 return 0;
8197}
8198
Chris Masond3977122009-01-05 21:25:51 -05008199static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
8200 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04008201{
8202 struct btrfs_trans_handle *trans;
8203 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008204 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04008205 struct inode *new_inode = new_dentry->d_inode;
8206 struct inode *old_inode = old_dentry->d_inode;
8207 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008208 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008209 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04008210 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08008211 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008212
Li Zefan33345d012011-04-20 10:31:50 +08008213 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04008214 return -EPERM;
8215
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008216 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08008217 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05008218 return -EXDEV;
8219
Li Zefan33345d012011-04-20 10:31:50 +08008220 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
8221 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008222 return -ENOTEMPTY;
8223
Chris Mason39279cc2007-06-12 06:35:45 -04008224 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008225 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04008226 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05008227
8228
8229 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04008230 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05008231 new_dentry->d_name.name,
8232 new_dentry->d_name.len);
8233
8234 if (ret) {
8235 if (ret == -EEXIST) {
8236 /* we shouldn't get
8237 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05308238 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05008239 return ret;
8240 }
8241 } else {
8242 /* maybe -EOVERFLOW */
8243 return ret;
8244 }
8245 }
8246 ret = 0;
8247
Chris Mason5a3f23d2009-03-31 13:27:11 -04008248 /*
8249 * we're using rename to replace one file with another.
8250 * and the replacement file is large. Start IO on it now so
8251 * we don't add too much work to the end of the transaction
8252 */
Bartlomiej Zolnierkiewicz4baf8c92009-08-07 13:47:08 -04008253 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
Chris Mason5a3f23d2009-03-31 13:27:11 -04008254 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
8255 filemap_flush(old_inode->i_mapping);
8256
Yan, Zheng76dda932009-09-21 16:00:26 -04008257 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08008258 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008259 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008260 /*
8261 * We want to reserve the absolute worst case amount of items. So if
8262 * both inodes are subvols and we need to unlink them then that would
8263 * require 4 item modifications, but if they are both normal inodes it
8264 * would require 5 item modifications, so we'll assume their normal
8265 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
8266 * should cover the worst case number of items we'll modify.
8267 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04008268 trans = btrfs_start_transaction(root, 11);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008269 if (IS_ERR(trans)) {
8270 ret = PTR_ERR(trans);
8271 goto out_notrans;
8272 }
Chris Mason5f39d392007-10-15 16:14:19 -04008273
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008274 if (dest != root)
8275 btrfs_record_root_in_trans(trans, dest);
8276
Yan, Zhenga5719522009-09-24 09:17:31 -04008277 ret = btrfs_set_inode_index(new_dir, &index);
8278 if (ret)
8279 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008280
Miao Xie67de1172013-12-26 13:07:06 +08008281 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08008282 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008283 /* force full log commit if subvolume involved. */
8284 root->fs_info->last_trans_log_full_commit = trans->transid;
8285 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04008286 ret = btrfs_insert_inode_ref(trans, dest,
8287 new_dentry->d_name.name,
8288 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08008289 old_ino,
8290 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04008291 if (ret)
8292 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008293 /*
8294 * this is an ugly little race, but the rename is required
8295 * to make sure that if we crash, the inode is either at the
8296 * old name or the new one. pinning the log transaction lets
8297 * us make sure we don't allow a log commit to come in after
8298 * we unlink the name but before we add the new name back in.
8299 */
8300 btrfs_pin_log_trans(root);
8301 }
Chris Mason12fcfd22009-03-24 10:24:20 -04008302 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04008303 * make sure the inode gets flushed if it is replacing
8304 * something.
8305 */
Li Zefan33345d012011-04-20 10:31:50 +08008306 if (new_inode && new_inode->i_size && S_ISREG(old_inode->i_mode))
Chris Mason5a3f23d2009-03-31 13:27:11 -04008307 btrfs_add_ordered_operation(trans, root, old_inode);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008308
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008309 inode_inc_iversion(old_dir);
8310 inode_inc_iversion(new_dir);
8311 inode_inc_iversion(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008312 old_dir->i_ctime = old_dir->i_mtime = ctime;
8313 new_dir->i_ctime = new_dir->i_mtime = ctime;
8314 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04008315
Chris Mason12fcfd22009-03-24 10:24:20 -04008316 if (old_dentry->d_parent != new_dentry->d_parent)
8317 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
8318
Li Zefan33345d012011-04-20 10:31:50 +08008319 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008320 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
8321 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
8322 old_dentry->d_name.name,
8323 old_dentry->d_name.len);
8324 } else {
Al Viro92986792011-03-04 17:14:37 +00008325 ret = __btrfs_unlink_inode(trans, root, old_dir,
8326 old_dentry->d_inode,
8327 old_dentry->d_name.name,
8328 old_dentry->d_name.len);
8329 if (!ret)
8330 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008331 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008332 if (ret) {
8333 btrfs_abort_transaction(trans, root, ret);
8334 goto out_fail;
8335 }
Chris Mason39279cc2007-06-12 06:35:45 -04008336
8337 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008338 inode_inc_iversion(new_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008339 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08008340 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008341 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
8342 root_objectid = BTRFS_I(new_inode)->location.objectid;
8343 ret = btrfs_unlink_subvol(trans, dest, new_dir,
8344 root_objectid,
8345 new_dentry->d_name.name,
8346 new_dentry->d_name.len);
8347 BUG_ON(new_inode->i_nlink == 0);
8348 } else {
8349 ret = btrfs_unlink_inode(trans, dest, new_dir,
8350 new_dentry->d_inode,
8351 new_dentry->d_name.name,
8352 new_dentry->d_name.len);
8353 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04008354 if (!ret && new_inode->i_nlink == 0)
Chris Masone02119d2008-09-05 16:13:11 -04008355 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008356 if (ret) {
8357 btrfs_abort_transaction(trans, root, ret);
8358 goto out_fail;
8359 }
Chris Mason39279cc2007-06-12 06:35:45 -04008360 }
Josef Bacikaec74772008-07-24 12:12:38 -04008361
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008362 ret = btrfs_add_link(trans, new_dir, old_inode,
8363 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04008364 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008365 if (ret) {
8366 btrfs_abort_transaction(trans, root, ret);
8367 goto out_fail;
8368 }
Chris Mason39279cc2007-06-12 06:35:45 -04008369
Miao Xie67de1172013-12-26 13:07:06 +08008370 if (old_inode->i_nlink == 1)
8371 BTRFS_I(old_inode)->dir_index = index;
8372
Li Zefan33345d012011-04-20 10:31:50 +08008373 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04008374 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00008375 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008376 btrfs_end_log_trans(root);
8377 }
Chris Mason39279cc2007-06-12 06:35:45 -04008378out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008379 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008380out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08008381 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008382 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008383
Chris Mason39279cc2007-06-12 06:35:45 -04008384 return ret;
8385}
8386
Qu Wenruoa44903a2014-02-28 10:46:09 +08008387static void btrfs_run_delalloc_work(struct btrfs_work_struct *work)
Miao Xie8ccf6f192012-10-25 09:28:04 +00008388{
8389 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04008390 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008391
8392 delalloc_work = container_of(work, struct btrfs_delalloc_work,
8393 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04008394 inode = delalloc_work->inode;
8395 if (delalloc_work->wait) {
8396 btrfs_wait_ordered_range(inode, 0, (u64)-1);
8397 } else {
8398 filemap_flush(inode->i_mapping);
8399 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8400 &BTRFS_I(inode)->runtime_flags))
8401 filemap_flush(inode->i_mapping);
8402 }
Miao Xie8ccf6f192012-10-25 09:28:04 +00008403
8404 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -04008405 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008406 else
Josef Bacik9f23e282013-10-28 15:03:41 -04008407 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008408 complete(&delalloc_work->completion);
8409}
8410
8411struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
8412 int wait, int delay_iput)
8413{
8414 struct btrfs_delalloc_work *work;
8415
8416 work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
8417 if (!work)
8418 return NULL;
8419
8420 init_completion(&work->completion);
8421 INIT_LIST_HEAD(&work->list);
8422 work->inode = inode;
8423 work->wait = wait;
8424 work->delay_iput = delay_iput;
Qu Wenruoa44903a2014-02-28 10:46:09 +08008425 btrfs_init_work(&work->work, btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008426
8427 return work;
8428}
8429
8430void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
8431{
8432 wait_for_completion(&work->completion);
8433 kmem_cache_free(btrfs_delalloc_work_cachep, work);
8434}
8435
Chris Masond352ac62008-09-29 15:18:18 -04008436/*
8437 * some fairly slow code that needs optimization. This walks the list
8438 * of all the inodes with pending delalloc and forces them to disk.
8439 */
Miao Xieeb73c1b2013-05-15 07:48:22 +00008440static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
Chris Masonea8c2812008-08-04 23:17:27 -04008441{
Chris Masonea8c2812008-08-04 23:17:27 -04008442 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008443 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008444 struct btrfs_delalloc_work *work, *next;
8445 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00008446 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008447 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04008448
Miao Xie8ccf6f192012-10-25 09:28:04 +00008449 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008450 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00008451
Miao Xieeb73c1b2013-05-15 07:48:22 +00008452 spin_lock(&root->delalloc_lock);
8453 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008454 while (!list_empty(&splice)) {
8455 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04008456 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008457
Miao Xieeb73c1b2013-05-15 07:48:22 +00008458 list_move_tail(&binode->delalloc_inodes,
8459 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008460 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00008461 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00008462 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008463 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008464 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008465 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008466
8467 work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
8468 if (unlikely(!work)) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -04008469 if (delay_iput)
8470 btrfs_add_delayed_iput(inode);
8471 else
8472 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008473 ret = -ENOMEM;
8474 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008475 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008476 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +08008477 btrfs_queue_work(root->fs_info->flush_workers,
8478 &work->work);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008479
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008480 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00008481 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04008482 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008483 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee1d2008-09-29 11:19:10 -04008484
Miao Xie1eafa6c2013-01-22 10:49:00 +00008485 list_for_each_entry_safe(work, next, &works, list) {
8486 list_del_init(&work->list);
8487 btrfs_wait_and_free_delalloc_work(work);
8488 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008489 return 0;
8490out:
8491 list_for_each_entry_safe(work, next, &works, list) {
8492 list_del_init(&work->list);
8493 btrfs_wait_and_free_delalloc_work(work);
8494 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008495
Miao Xieeb73c1b2013-05-15 07:48:22 +00008496 if (!list_empty_careful(&splice)) {
8497 spin_lock(&root->delalloc_lock);
8498 list_splice_tail(&splice, &root->delalloc_inodes);
8499 spin_unlock(&root->delalloc_lock);
8500 }
8501 return ret;
8502}
8503
8504int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
8505{
8506 int ret;
8507
Wang Shilong2c21b4d2014-01-14 19:42:20 +08008508 if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00008509 return -EROFS;
8510
8511 ret = __start_delalloc_inodes(root, delay_iput);
8512 /*
8513 * the filemap_flush will queue IO into the worker threads, but
Chris Mason8c8bee1d2008-09-29 11:19:10 -04008514 * we have to make sure the IO is actually started and that
8515 * ordered extents get created before we return
8516 */
8517 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05008518 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05008519 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee1d2008-09-29 11:19:10 -04008520 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05008521 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
8522 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee1d2008-09-29 11:19:10 -04008523 }
8524 atomic_dec(&root->fs_info->async_submit_draining);
Miao Xieeb73c1b2013-05-15 07:48:22 +00008525 return ret;
8526}
8527
Miao Xie91aef862013-11-04 23:13:26 +08008528int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput)
Miao Xieeb73c1b2013-05-15 07:48:22 +00008529{
8530 struct btrfs_root *root;
8531 struct list_head splice;
8532 int ret;
8533
Wang Shilong2c21b4d2014-01-14 19:42:20 +08008534 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00008535 return -EROFS;
8536
8537 INIT_LIST_HEAD(&splice);
8538
8539 spin_lock(&fs_info->delalloc_root_lock);
8540 list_splice_init(&fs_info->delalloc_roots, &splice);
8541 while (!list_empty(&splice)) {
8542 root = list_first_entry(&splice, struct btrfs_root,
8543 delalloc_root);
8544 root = btrfs_grab_fs_root(root);
8545 BUG_ON(!root);
8546 list_move_tail(&root->delalloc_root,
8547 &fs_info->delalloc_roots);
8548 spin_unlock(&fs_info->delalloc_root_lock);
8549
8550 ret = __start_delalloc_inodes(root, delay_iput);
8551 btrfs_put_fs_root(root);
8552 if (ret)
8553 goto out;
8554
8555 spin_lock(&fs_info->delalloc_root_lock);
8556 }
8557 spin_unlock(&fs_info->delalloc_root_lock);
8558
8559 atomic_inc(&fs_info->async_submit_draining);
8560 while (atomic_read(&fs_info->nr_async_submits) ||
8561 atomic_read(&fs_info->async_delalloc_pages)) {
8562 wait_event(fs_info->async_submit_wait,
8563 (atomic_read(&fs_info->nr_async_submits) == 0 &&
8564 atomic_read(&fs_info->async_delalloc_pages) == 0));
8565 }
8566 atomic_dec(&fs_info->async_submit_draining);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008567 return 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008568out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00008569 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00008570 spin_lock(&fs_info->delalloc_root_lock);
8571 list_splice_tail(&splice, &fs_info->delalloc_roots);
8572 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008573 }
Miao Xie8ccf6f192012-10-25 09:28:04 +00008574 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04008575}
8576
Chris Mason39279cc2007-06-12 06:35:45 -04008577static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
8578 const char *symname)
8579{
8580 struct btrfs_trans_handle *trans;
8581 struct btrfs_root *root = BTRFS_I(dir)->root;
8582 struct btrfs_path *path;
8583 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05008584 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04008585 int err;
8586 int drop_inode = 0;
8587 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05308588 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008589 int name_len;
8590 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04008591 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04008592 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04008593 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04008594
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01008595 name_len = strlen(symname);
Chris Mason39279cc2007-06-12 06:35:45 -04008596 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
8597 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05008598
Josef Bacik9ed74f22009-09-11 16:12:44 -04008599 /*
8600 * 2 items for inode item and ref
8601 * 2 items for dir items
8602 * 1 item for xattr if selinux is on
8603 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04008604 trans = btrfs_start_transaction(root, 5);
8605 if (IS_ERR(trans))
8606 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05008607
Li Zefan581bb052011-04-20 10:06:11 +08008608 err = btrfs_find_free_ino(root, &objectid);
8609 if (err)
8610 goto out_unlock;
8611
Josef Bacikaec74772008-07-24 12:12:38 -04008612 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08008613 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04008614 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008615 if (IS_ERR(inode)) {
8616 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008617 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008618 }
Chris Mason39279cc2007-06-12 06:35:45 -04008619
Eric Paris2a7dba32011-02-01 11:05:39 -05008620 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04008621 if (err) {
8622 drop_inode = 1;
8623 goto out_unlock;
8624 }
8625
Casey Schauflerad19db72011-12-15 10:09:07 -05008626 /*
8627 * If the active LSM wants to access the inode during
8628 * d_instantiate it needs these. Smack checks to see
8629 * if the filesystem supports xattrs by looking at the
8630 * ops vector.
8631 */
8632 inode->i_fop = &btrfs_file_operations;
8633 inode->i_op = &btrfs_file_inode_operations;
8634
Josef Bacika1b075d2010-11-19 20:36:11 +00008635 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04008636 if (err)
8637 drop_inode = 1;
8638 else {
8639 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04008640 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05008641 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04008642 }
Chris Mason39279cc2007-06-12 06:35:45 -04008643 if (drop_inode)
8644 goto out_unlock;
8645
8646 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07008647 if (!path) {
8648 err = -ENOMEM;
8649 drop_inode = 1;
8650 goto out_unlock;
8651 }
Li Zefan33345d012011-04-20 10:31:50 +08008652 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008653 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008654 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
8655 datasize = btrfs_file_extent_calc_inline_size(name_len);
8656 err = btrfs_insert_empty_item(trans, root, path, &key,
8657 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04008658 if (err) {
8659 drop_inode = 1;
Julia Lawallb0839162011-05-14 07:10:51 +00008660 btrfs_free_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04008661 goto out_unlock;
8662 }
Chris Mason5f39d392007-10-15 16:14:19 -04008663 leaf = path->nodes[0];
8664 ei = btrfs_item_ptr(leaf, path->slots[0],
8665 struct btrfs_file_extent_item);
8666 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
8667 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04008668 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04008669 btrfs_set_file_extent_encryption(leaf, ei, 0);
8670 btrfs_set_file_extent_compression(leaf, ei, 0);
8671 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
8672 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
8673
Chris Mason39279cc2007-06-12 06:35:45 -04008674 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04008675 write_extent_buffer(leaf, symname, ptr, name_len);
8676 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04008677 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04008678
Chris Mason39279cc2007-06-12 06:35:45 -04008679 inode->i_op = &btrfs_symlink_inode_operations;
8680 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04008681 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04008682 inode_set_bytes(inode, name_len);
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01008683 btrfs_i_size_write(inode, name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -04008684 err = btrfs_update_inode(trans, root, inode);
8685 if (err)
8686 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04008687
8688out_unlock:
Al Viro08c422c2011-12-23 07:58:13 -05008689 if (!err)
8690 d_instantiate(dentry, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008691 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04008692 if (drop_inode) {
8693 inode_dec_link_count(inode);
8694 iput(inode);
8695 }
Liu Bob53d3f52012-11-14 14:34:34 +00008696 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04008697 return err;
8698}
Chris Mason16432982008-04-10 10:23:21 -04008699
Josef Bacik0af3d002010-06-21 14:48:16 -04008700static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
8701 u64 start, u64 num_bytes, u64 min_size,
8702 loff_t actual_len, u64 *alloc_hint,
8703 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04008704{
Josef Bacik5dc562c2012-08-17 13:14:17 -04008705 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
8706 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04008707 struct btrfs_root *root = BTRFS_I(inode)->root;
8708 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04008709 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00008710 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05008711 u64 cur_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04008712 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04008713 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04008714
Josef Bacik0af3d002010-06-21 14:48:16 -04008715 if (trans)
8716 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04008717 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008718 if (own_trans) {
8719 trans = btrfs_start_transaction(root, 3);
8720 if (IS_ERR(trans)) {
8721 ret = PTR_ERR(trans);
8722 break;
8723 }
Yan Zhengd899e052008-10-30 14:25:28 -04008724 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00008725
Chris Mason154ea282013-03-05 11:11:26 -05008726 cur_bytes = min(num_bytes, 256ULL * 1024 * 1024);
8727 cur_bytes = max(cur_bytes, min_size);
Josef Bacik00361582013-08-14 14:02:47 -04008728 ret = btrfs_reserve_extent(root, cur_bytes, min_size, 0,
8729 *alloc_hint, &ins, 1);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008730 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008731 if (own_trans)
8732 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008733 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00008734 }
8735
Yan Zhengd899e052008-10-30 14:25:28 -04008736 ret = insert_reserved_file_extent(trans, inode,
8737 cur_offset, ins.objectid,
8738 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00008739 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04008740 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008741 if (ret) {
Josef Bacik857cc2f2013-10-07 15:21:08 -04008742 btrfs_free_reserved_extent(root, ins.objectid,
8743 ins.offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008744 btrfs_abort_transaction(trans, root, ret);
8745 if (own_trans)
8746 btrfs_end_transaction(trans, root);
8747 break;
8748 }
Chris Masona1ed8352009-09-11 12:27:37 -04008749 btrfs_drop_extent_cache(inode, cur_offset,
8750 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008751
Josef Bacik5dc562c2012-08-17 13:14:17 -04008752 em = alloc_extent_map();
8753 if (!em) {
8754 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
8755 &BTRFS_I(inode)->runtime_flags);
8756 goto next;
8757 }
8758
8759 em->start = cur_offset;
8760 em->orig_start = cur_offset;
8761 em->len = ins.offset;
8762 em->block_start = ins.objectid;
8763 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05008764 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04008765 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008766 em->bdev = root->fs_info->fs_devices->latest_bdev;
8767 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
8768 em->generation = trans->transid;
8769
8770 while (1) {
8771 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04008772 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04008773 write_unlock(&em_tree->lock);
8774 if (ret != -EEXIST)
8775 break;
8776 btrfs_drop_extent_cache(inode, cur_offset,
8777 cur_offset + ins.offset - 1,
8778 0);
8779 }
8780 free_extent_map(em);
8781next:
Yan Zhengd899e052008-10-30 14:25:28 -04008782 num_bytes -= ins.offset;
8783 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04008784 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00008785
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008786 inode_inc_iversion(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04008787 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02008788 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04008789 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04008790 (actual_len > inode->i_size) &&
8791 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00008792 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00008793 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00008794 else
Josef Bacik55a61d12010-11-22 18:50:32 +00008795 i_size = cur_offset;
8796 i_size_write(inode, i_size);
8797 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008798 }
8799
Yan Zhengd899e052008-10-30 14:25:28 -04008800 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008801
8802 if (ret) {
8803 btrfs_abort_transaction(trans, root, ret);
8804 if (own_trans)
8805 btrfs_end_transaction(trans, root);
8806 break;
8807 }
Yan Zhengd899e052008-10-30 14:25:28 -04008808
Josef Bacik0af3d002010-06-21 14:48:16 -04008809 if (own_trans)
8810 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008811 }
Yan Zhengd899e052008-10-30 14:25:28 -04008812 return ret;
8813}
8814
Josef Bacik0af3d002010-06-21 14:48:16 -04008815int btrfs_prealloc_file_range(struct inode *inode, 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,
8821 NULL);
8822}
8823
8824int btrfs_prealloc_file_range_trans(struct inode *inode,
8825 struct btrfs_trans_handle *trans, int mode,
8826 u64 start, u64 num_bytes, u64 min_size,
8827 loff_t actual_len, u64 *alloc_hint)
8828{
8829 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
8830 min_size, actual_len, alloc_hint, trans);
8831}
8832
Chris Masone6dcd2d2008-07-17 12:53:50 -04008833static int btrfs_set_page_dirty(struct page *page)
8834{
Chris Masone6dcd2d2008-07-17 12:53:50 -04008835 return __set_page_dirty_nobuffers(page);
8836}
8837
Al Viro10556cb2011-06-20 19:28:19 -04008838static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05008839{
Li Zefanb83cc962010-12-20 16:04:08 +08008840 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00008841 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08008842
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00008843 if (mask & MAY_WRITE &&
8844 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
8845 if (btrfs_root_readonly(root))
8846 return -EROFS;
8847 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
8848 return -EACCES;
8849 }
Al Viro2830ba72011-06-20 19:16:29 -04008850 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05008851}
Chris Mason39279cc2007-06-12 06:35:45 -04008852
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008853static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05008854 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04008855 .lookup = btrfs_lookup,
8856 .create = btrfs_create,
8857 .unlink = btrfs_unlink,
8858 .link = btrfs_link,
8859 .mkdir = btrfs_mkdir,
8860 .rmdir = btrfs_rmdir,
8861 .rename = btrfs_rename,
8862 .symlink = btrfs_symlink,
8863 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04008864 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008865 .setxattr = btrfs_setxattr,
8866 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05008867 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008868 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05008869 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008870 .get_acl = btrfs_get_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -07008871 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008872};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008873static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008874 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05008875 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008876 .get_acl = btrfs_get_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -07008877 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008878};
Yan, Zheng76dda932009-09-21 16:00:26 -04008879
Alexey Dobriyan828c0952009-10-01 15:43:56 -07008880static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008881 .llseek = generic_file_llseek,
8882 .read = generic_read_dir,
Al Viro9cdda8d2013-05-22 16:48:09 -04008883 .iterate = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04008884 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04008885#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04008886 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04008887#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04008888 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04008889 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04008890};
8891
Chris Masond1310b22008-01-24 16:13:08 -05008892static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04008893 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05008894 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04008895 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04008896 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008897 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04008898 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05008899 .set_bit_hook = btrfs_set_bit_hook,
8900 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04008901 .merge_extent_hook = btrfs_merge_extent_hook,
8902 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04008903};
8904
Chris Mason35054392009-01-21 13:11:13 -05008905/*
8906 * btrfs doesn't support the bmap operation because swapfiles
8907 * use bmap to make a mapping of extents in the file. They assume
8908 * these extents won't change over the life of the file and they
8909 * use the bmap result to do IO directly to the drive.
8910 *
8911 * the btrfs bmap call would return logical addresses that aren't
8912 * suitable for IO and they also will change frequently as COW
8913 * operations happen. So, swapfile + btrfs == corruption.
8914 *
8915 * For now we're avoiding this by dropping bmap.
8916 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008917static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008918 .readpage = btrfs_readpage,
8919 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04008920 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05008921 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04008922 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04008923 .invalidatepage = btrfs_invalidatepage,
8924 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008925 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02008926 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04008927};
8928
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008929static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008930 .readpage = btrfs_readpage,
8931 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04008932 .invalidatepage = btrfs_invalidatepage,
8933 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04008934};
8935
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008936static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008937 .getattr = btrfs_getattr,
8938 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008939 .setxattr = btrfs_setxattr,
8940 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05008941 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008942 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05008943 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008944 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008945 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008946 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008947};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008948static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04008949 .getattr = btrfs_getattr,
8950 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008951 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008952 .setxattr = btrfs_setxattr,
8953 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04008954 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008955 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008956 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008957 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -04008958};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008959static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008960 .readlink = generic_readlink,
8961 .follow_link = page_follow_link_light,
8962 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00008963 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -05008964 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008965 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05008966 .setxattr = btrfs_setxattr,
8967 .getxattr = btrfs_getxattr,
8968 .listxattr = btrfs_listxattr,
8969 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008970 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008971 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008972};
Yan, Zheng76dda932009-09-21 16:00:26 -04008973
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04008974const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04008975 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -04008976 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -04008977};