blob: 251db68148b23a39187653f7f77e979fd282435d [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 Wenruod458b052014-02-28 10:46:19 +0800327 struct btrfs_work 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 Masonb888db2b2007-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 Masonb888db2b2007-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 Masonf03d9301f2009-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 Masonf03d9301f2009-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 Masonb888db2b2007-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 Wenruod458b052014-02-28 10:46:19 +08001003static noinline void async_cow_start(struct btrfs_work *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 Wenruod458b052014-02-28 10:46:19 +08001021static noinline void async_cow_submit(struct btrfs_work *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 Wenruod458b052014-02-28 10:46:19 +08001042static noinline void async_cow_free(struct btrfs_work *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 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001274 * if there are pending snapshots for this root,
1275 * we fall into common COW way.
1276 */
1277 if (!nolock) {
1278 err = btrfs_start_nocow_write(root);
1279 if (!err)
1280 goto out_check;
1281 }
1282 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001283 * force cow if csum exists in the range.
1284 * this ensure that csum for a given extent are
1285 * either valid or do not exist.
1286 */
1287 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1288 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001289 nocow = 1;
1290 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1291 extent_end = found_key.offset +
Chris Mason514ac8a2014-01-03 21:07:00 -08001292 btrfs_file_extent_inline_len(leaf,
1293 path->slots[0], fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001294 extent_end = ALIGN(extent_end, root->sectorsize);
1295 } else {
1296 BUG_ON(1);
1297 }
1298out_check:
1299 if (extent_end <= start) {
1300 path->slots[0]++;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001301 if (!nolock && nocow)
1302 btrfs_end_nocow_write(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001303 goto next_slot;
1304 }
1305 if (!nocow) {
1306 if (cow_start == (u64)-1)
1307 cow_start = cur_offset;
1308 cur_offset = extent_end;
1309 if (cur_offset > end)
1310 break;
1311 path->slots[0]++;
1312 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001313 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001314
David Sterbab3b4aa72011-04-21 01:20:15 +02001315 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001316 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001317 ret = cow_file_range(inode, locked_page,
1318 cow_start, found_key.offset - 1,
1319 page_started, nr_written, 1);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001320 if (ret) {
1321 if (!nolock && nocow)
1322 btrfs_end_nocow_write(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001323 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001324 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001325 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001326 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001327
Yan Zhengd899e052008-10-30 14:25:28 -04001328 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1329 struct extent_map *em;
1330 struct extent_map_tree *em_tree;
1331 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001332 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001333 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001334 em->start = cur_offset;
Josef Bacik70c8a912012-10-11 16:54:30 -04001335 em->orig_start = found_key.offset - extent_offset;
Yan Zhengd899e052008-10-30 14:25:28 -04001336 em->len = num_bytes;
1337 em->block_len = num_bytes;
1338 em->block_start = disk_bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05001339 em->orig_block_len = disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001340 em->ram_bytes = ram_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04001341 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001342 em->mod_start = em->start;
1343 em->mod_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04001344 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacikb11e2342012-12-03 10:58:15 -05001345 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001346 em->generation = -1;
Yan Zhengd899e052008-10-30 14:25:28 -04001347 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001348 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001349 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001350 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001351 if (ret != -EEXIST) {
1352 free_extent_map(em);
1353 break;
1354 }
1355 btrfs_drop_extent_cache(inode, em->start,
1356 em->start + em->len - 1, 0);
1357 }
1358 type = BTRFS_ORDERED_PREALLOC;
1359 } else {
1360 type = BTRFS_ORDERED_NOCOW;
1361 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001362
1363 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001364 num_bytes, num_bytes, type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001365 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001366
Yan, Zhengefa56462010-05-16 10:49:59 -04001367 if (root->root_key.objectid ==
1368 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1369 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1370 num_bytes);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001371 if (ret) {
1372 if (!nolock && nocow)
1373 btrfs_end_nocow_write(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001374 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001375 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001376 }
1377
Josef Bacikc2790a22013-07-29 11:20:47 -04001378 extent_clear_unlock_delalloc(inode, cur_offset,
1379 cur_offset + num_bytes - 1,
1380 locked_page, EXTENT_LOCKED |
1381 EXTENT_DELALLOC, PAGE_UNLOCK |
1382 PAGE_SET_PRIVATE2);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001383 if (!nolock && nocow)
1384 btrfs_end_nocow_write(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001385 cur_offset = extent_end;
1386 if (cur_offset > end)
1387 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001388 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001389 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001390
Josef Bacik17ca04a2012-05-31 15:58:55 -04001391 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001392 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001393 cur_offset = end;
1394 }
1395
Yan Zheng80ff3852008-10-30 14:20:02 -04001396 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001397 ret = cow_file_range(inode, locked_page, cow_start, end,
1398 page_started, nr_written, 1);
Josef Bacikd788a342013-10-25 16:55:08 -04001399 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001400 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001401 }
1402
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001403error:
Miao Xiea698d0752012-09-20 01:51:59 -06001404 err = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001405 if (!ret)
1406 ret = err;
1407
Josef Bacik17ca04a2012-05-31 15:58:55 -04001408 if (ret && cur_offset < end)
Josef Bacikc2790a22013-07-29 11:20:47 -04001409 extent_clear_unlock_delalloc(inode, cur_offset, end,
1410 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001411 EXTENT_DELALLOC | EXTENT_DEFRAG |
1412 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1413 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001414 PAGE_SET_WRITEBACK |
1415 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001416 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001417 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001418}
1419
Chris Masond352ac62008-09-29 15:18:18 -04001420/*
1421 * extent_io.c call back to do delayed allocation processing
1422 */
Chris Masonc8b97812008-10-29 14:49:59 -04001423static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001424 u64 start, u64 end, int *page_started,
1425 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001426{
Chris Masonbe20aa92007-12-17 20:14:01 -05001427 int ret;
Chris Mason7f366cf2009-03-12 20:12:45 -04001428 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona2135012008-06-25 16:01:30 -04001429
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001430 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) {
Chris Masonc8b97812008-10-29 14:49:59 -04001431 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001432 page_started, 1, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001433 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001434 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001435 page_started, 0, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001436 } else if (!btrfs_test_opt(root, COMPRESS) &&
1437 !(BTRFS_I(inode)->force_compress) &&
1438 !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS)) {
Chris Mason7f366cf2009-03-12 20:12:45 -04001439 ret = cow_file_range(inode, locked_page, start, end,
1440 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001441 } else {
1442 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1443 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001444 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001445 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001446 }
Chris Masonb888db2b2007-08-27 16:49:44 -04001447 return ret;
1448}
1449
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001450static void btrfs_split_extent_hook(struct inode *inode,
1451 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001452{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001453 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001454 if (!(orig->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
1462/*
1463 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1464 * extents so we can keep track of new extents that are just merged onto old
1465 * extents, such as when we are doing sequential writes, so we can properly
1466 * account for the metadata space we'll need.
1467 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001468static void btrfs_merge_extent_hook(struct inode *inode,
1469 struct extent_state *new,
1470 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001471{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001472 /* not delalloc, ignore it */
1473 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001474 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001475
Josef Bacik9e0baf62011-07-15 15:16:44 +00001476 spin_lock(&BTRFS_I(inode)->lock);
1477 BTRFS_I(inode)->outstanding_extents--;
1478 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001479}
1480
Miao Xieeb73c1b2013-05-15 07:48:22 +00001481static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1482 struct inode *inode)
1483{
1484 spin_lock(&root->delalloc_lock);
1485 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1486 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1487 &root->delalloc_inodes);
1488 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1489 &BTRFS_I(inode)->runtime_flags);
1490 root->nr_delalloc_inodes++;
1491 if (root->nr_delalloc_inodes == 1) {
1492 spin_lock(&root->fs_info->delalloc_root_lock);
1493 BUG_ON(!list_empty(&root->delalloc_root));
1494 list_add_tail(&root->delalloc_root,
1495 &root->fs_info->delalloc_roots);
1496 spin_unlock(&root->fs_info->delalloc_root_lock);
1497 }
1498 }
1499 spin_unlock(&root->delalloc_lock);
1500}
1501
1502static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1503 struct inode *inode)
1504{
1505 spin_lock(&root->delalloc_lock);
1506 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1507 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1508 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1509 &BTRFS_I(inode)->runtime_flags);
1510 root->nr_delalloc_inodes--;
1511 if (!root->nr_delalloc_inodes) {
1512 spin_lock(&root->fs_info->delalloc_root_lock);
1513 BUG_ON(list_empty(&root->delalloc_root));
1514 list_del_init(&root->delalloc_root);
1515 spin_unlock(&root->fs_info->delalloc_root_lock);
1516 }
1517 }
1518 spin_unlock(&root->delalloc_lock);
1519}
1520
Chris Masond352ac62008-09-29 15:18:18 -04001521/*
1522 * extent_io.c set_bit_hook, used to track delayed allocation
1523 * bytes in this file, and to maintain the list of inodes that
1524 * have pending delalloc work to be done.
1525 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001526static void btrfs_set_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001527 struct extent_state *state, unsigned long *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001528{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001529
Chris Mason75eff682008-12-15 15:54:40 -05001530 /*
1531 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001532 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001533 * bit, which is only set or cleared with irqs on
1534 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001535 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001536 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001537 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001538 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001539
Josef Bacik9e0baf62011-07-15 15:16:44 +00001540 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001541 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001542 } else {
1543 spin_lock(&BTRFS_I(inode)->lock);
1544 BTRFS_I(inode)->outstanding_extents++;
1545 spin_unlock(&BTRFS_I(inode)->lock);
1546 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001547
Miao Xie963d6782013-01-29 10:10:51 +00001548 __percpu_counter_add(&root->fs_info->delalloc_bytes, len,
1549 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001550 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001551 BTRFS_I(inode)->delalloc_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001552 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001553 &BTRFS_I(inode)->runtime_flags))
1554 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001555 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001556 }
Chris Mason291d6732008-01-29 15:55:23 -05001557}
1558
Chris Masond352ac62008-09-29 15:18:18 -04001559/*
1560 * extent_io.c clear_bit_hook, see set_bit_hook for why
1561 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001562static void btrfs_clear_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001563 struct extent_state *state,
1564 unsigned long *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001565{
Chris Mason75eff682008-12-15 15:54:40 -05001566 /*
1567 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001568 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001569 * bit, which is only set or cleared with irqs on
1570 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001571 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001572 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001573 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001574 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001575
Josef Bacik9e0baf62011-07-15 15:16:44 +00001576 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001577 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001578 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1579 spin_lock(&BTRFS_I(inode)->lock);
1580 BTRFS_I(inode)->outstanding_extents--;
1581 spin_unlock(&BTRFS_I(inode)->lock);
1582 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001583
Josef Bacikb6d08f02013-09-27 14:57:43 -04001584 /*
1585 * We don't reserve metadata space for space cache inodes so we
1586 * don't need to call dellalloc_release_metadata if there is an
1587 * error.
1588 */
1589 if (*bits & EXTENT_DO_ACCOUNTING &&
1590 root != root->fs_info->tree_root)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001591 btrfs_delalloc_release_metadata(inode, len);
1592
Josef Bacik0cb59c92010-07-02 12:14:14 -04001593 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Josef Bacik7ee9e442013-06-21 16:37:03 -04001594 && do_list && !(state->state & EXTENT_NORESERVE))
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001595 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001596
Miao Xie963d6782013-01-29 10:10:51 +00001597 __percpu_counter_add(&root->fs_info->delalloc_bytes, -len,
1598 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001599 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001600 BTRFS_I(inode)->delalloc_bytes -= len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001601 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001602 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001603 &BTRFS_I(inode)->runtime_flags))
1604 btrfs_del_delalloc_inode(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001605 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001606 }
Chris Mason291d6732008-01-29 15:55:23 -05001607}
1608
Chris Masond352ac62008-09-29 15:18:18 -04001609/*
1610 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1611 * we don't create bios that span stripes or chunks
1612 */
David Woodhouse64a16702009-07-15 23:29:37 +01001613int btrfs_merge_bio_hook(int rw, struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001614 size_t size, struct bio *bio,
1615 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001616{
1617 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Chris Masona62b9402008-10-03 16:31:08 -04001618 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001619 u64 length = 0;
1620 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001621 int ret;
1622
Chris Mason771ed682008-11-06 22:02:51 -05001623 if (bio_flags & EXTENT_BIO_COMPRESSED)
1624 return 0;
1625
Chris Masonf2d8d742008-04-21 10:03:05 -04001626 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001627 map_length = length;
David Woodhouse64a16702009-07-15 23:29:37 +01001628 ret = btrfs_map_block(root->fs_info, rw, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001629 &map_length, NULL, 0);
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001630 /* Will always return 0 with map_multi == NULL */
Jeff Mahoney3444a972011-10-03 23:23:13 -04001631 BUG_ON(ret < 0);
Chris Masond3977122009-01-05 21:25:51 -05001632 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001633 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001634 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001635}
1636
Chris Masond352ac62008-09-29 15:18:18 -04001637/*
1638 * in order to insert checksums into the metadata in large chunks,
1639 * we wait until bio submission time. All the pages in the bio are
1640 * checksummed and sums are attached onto the ordered extent record.
1641 *
1642 * At IO completion time the cums attached on the ordered extent record
1643 * are inserted into the btree
1644 */
Chris Masond3977122009-01-05 21:25:51 -05001645static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1646 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001647 unsigned long bio_flags,
1648 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001649{
Chris Mason065631f2008-02-20 12:07:25 -05001650 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001651 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001652
Chris Masond20f7042008-12-08 16:58:54 -05001653 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001654 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001655 return 0;
1656}
Chris Masone0156402008-04-16 11:15:20 -04001657
Chris Mason4a69a412008-11-06 22:03:00 -05001658/*
1659 * in order to insert checksums into the metadata in large chunks,
1660 * we wait until bio submission time. All the pages in the bio are
1661 * checksummed and sums are attached onto the ordered extent record.
1662 *
1663 * At IO completion time the cums attached on the ordered extent record
1664 * are inserted into the btree
1665 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001666static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001667 int mirror_num, unsigned long bio_flags,
1668 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001669{
1670 struct btrfs_root *root = BTRFS_I(inode)->root;
Stefan Behrens61891922012-11-05 18:51:52 +01001671 int ret;
1672
1673 ret = btrfs_map_bio(root, rw, bio, mirror_num, 1);
1674 if (ret)
1675 bio_endio(bio, ret);
1676 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001677}
1678
Chris Masond352ac62008-09-29 15:18:18 -04001679/*
Chris Masoncad321a2008-12-17 14:51:42 -05001680 * extent_io.c submission hook. This does the right thing for csum calculation
1681 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001682 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001683static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001684 int mirror_num, unsigned long bio_flags,
1685 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001686{
1687 struct btrfs_root *root = BTRFS_I(inode)->root;
1688 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001689 int skip_sum;
Jeff Mahoney04173412011-10-03 23:23:12 -04001690 int metadata = 0;
Josef Bacikb812ce22012-11-16 13:56:32 -05001691 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001692
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001693 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001694
Liu Bo83eea1f2012-07-10 05:28:39 -06001695 if (btrfs_is_free_space_inode(inode))
Jeff Mahoney04173412011-10-03 23:23:12 -04001696 metadata = 2;
1697
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001698 if (!(rw & REQ_WRITE)) {
Josef Bacik5fd02042012-05-02 14:00:54 -04001699 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1700 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001701 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001702
Chris Masond20f7042008-12-08 16:58:54 -05001703 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001704 ret = btrfs_submit_compressed_read(inode, bio,
1705 mirror_num,
1706 bio_flags);
1707 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001708 } else if (!skip_sum) {
1709 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1710 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001711 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001712 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001713 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001714 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001715 /* csum items have already been cloned */
1716 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1717 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001718 /* we're doing a write, do the async checksumming */
Stefan Behrens61891922012-11-05 18:51:52 +01001719 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001720 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001721 bio_flags, bio_offset,
1722 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001723 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001724 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001725 } else if (!skip_sum) {
1726 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1727 if (ret)
1728 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001729 }
1730
Chris Mason0b86a832008-03-24 15:01:56 -04001731mapit:
Stefan Behrens61891922012-11-05 18:51:52 +01001732 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
1733
1734out:
1735 if (ret < 0)
1736 bio_endio(bio, ret);
1737 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001738}
Chris Mason6885f302008-02-20 16:11:05 -05001739
Chris Masond352ac62008-09-29 15:18:18 -04001740/*
1741 * given a list of ordered sums record them in the inode. This happens
1742 * at IO completion time based on sums calculated at bio submission time.
1743 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001744static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001745 struct inode *inode, u64 file_offset,
1746 struct list_head *list)
1747{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001748 struct btrfs_ordered_sum *sum;
1749
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001750 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00001751 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05001752 btrfs_csum_file_blocks(trans,
1753 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00001754 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001755 }
1756 return 0;
1757}
1758
Josef Bacik2ac55d42010-02-03 19:33:23 +00001759int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1760 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001761{
Julia Lawall6c1500f2012-11-03 20:30:18 +00001762 WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04001763 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001764 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001765}
1766
Chris Masond352ac62008-09-29 15:18:18 -04001767/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001768struct btrfs_writepage_fixup {
1769 struct page *page;
Qu Wenruod458b052014-02-28 10:46:19 +08001770 struct btrfs_work work;
Chris Mason247e7432008-07-17 12:53:51 -04001771};
1772
Qu Wenruod458b052014-02-28 10:46:19 +08001773static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001774{
1775 struct btrfs_writepage_fixup *fixup;
1776 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001777 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001778 struct page *page;
1779 struct inode *inode;
1780 u64 page_start;
1781 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001782 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001783
1784 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1785 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001786again:
Chris Mason247e7432008-07-17 12:53:51 -04001787 lock_page(page);
1788 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1789 ClearPageChecked(page);
1790 goto out_page;
1791 }
1792
1793 inode = page->mapping->host;
1794 page_start = page_offset(page);
1795 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1796
Josef Bacik2ac55d42010-02-03 19:33:23 +00001797 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001798 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001799
1800 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001801 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001802 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001803
1804 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1805 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001806 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1807 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001808 unlock_page(page);
1809 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001810 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04001811 goto again;
1812 }
Chris Mason247e7432008-07-17 12:53:51 -04001813
Jeff Mahoney87826df2012-02-15 16:23:57 +01001814 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
1815 if (ret) {
1816 mapping_set_error(page->mapping, ret);
1817 end_extent_writepage(page, ret, page_start, page_end);
1818 ClearPageChecked(page);
1819 goto out;
1820 }
1821
Josef Bacik2ac55d42010-02-03 19:33:23 +00001822 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001823 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001824 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04001825out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001826 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1827 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001828out_page:
1829 unlock_page(page);
1830 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08001831 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04001832}
1833
1834/*
1835 * There are a few paths in the higher layers of the kernel that directly
1836 * set the page dirty bit without asking the filesystem if it is a
1837 * good idea. This causes problems because we want to make sure COW
1838 * properly happens and the data=ordered rules are followed.
1839 *
Chris Masonc8b97812008-10-29 14:49:59 -04001840 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001841 * hasn't been properly setup for IO. We kick off an async process
1842 * to fix it up. The async helper will wait for ordered extents, set
1843 * the delalloc bit and make it safe to write the page.
1844 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001845static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001846{
1847 struct inode *inode = page->mapping->host;
1848 struct btrfs_writepage_fixup *fixup;
1849 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001850
Chris Mason8b62b722009-09-02 16:53:46 -04001851 /* this page is properly in the ordered list */
1852 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001853 return 0;
1854
1855 if (PageChecked(page))
1856 return -EAGAIN;
1857
1858 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1859 if (!fixup)
1860 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001861
Chris Mason247e7432008-07-17 12:53:51 -04001862 SetPageChecked(page);
1863 page_cache_get(page);
Qu Wenruodc6e3202014-02-28 10:46:14 +08001864 btrfs_init_work(&fixup->work, btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04001865 fixup->page = page;
Qu Wenruodc6e3202014-02-28 10:46:14 +08001866 btrfs_queue_work(root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001867 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04001868}
1869
Yan Zhengd899e052008-10-30 14:25:28 -04001870static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1871 struct inode *inode, u64 file_pos,
1872 u64 disk_bytenr, u64 disk_num_bytes,
1873 u64 num_bytes, u64 ram_bytes,
1874 u8 compression, u8 encryption,
1875 u16 other_encoding, int extent_type)
1876{
1877 struct btrfs_root *root = BTRFS_I(inode)->root;
1878 struct btrfs_file_extent_item *fi;
1879 struct btrfs_path *path;
1880 struct extent_buffer *leaf;
1881 struct btrfs_key ins;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00001882 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04001883 int ret;
1884
1885 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07001886 if (!path)
1887 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04001888
Chris Masona1ed8352009-09-11 12:27:37 -04001889 /*
1890 * we may be replacing one extent in the tree with another.
1891 * The new extent is pinned in the extent map, and we don't want
1892 * to drop it from the cache until it is completely in the btree.
1893 *
1894 * So, tell btrfs_drop_extents to leave this extent in the cache.
1895 * the caller is expected to unpin it and allow it to be merged
1896 * with the others.
1897 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00001898 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
1899 file_pos + num_bytes, NULL, 0,
1900 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001901 if (ret)
1902 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001903
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00001904 if (!extent_inserted) {
1905 ins.objectid = btrfs_ino(inode);
1906 ins.offset = file_pos;
1907 ins.type = BTRFS_EXTENT_DATA_KEY;
1908
1909 path->leave_spinning = 1;
1910 ret = btrfs_insert_empty_item(trans, root, path, &ins,
1911 sizeof(*fi));
1912 if (ret)
1913 goto out;
1914 }
Yan Zhengd899e052008-10-30 14:25:28 -04001915 leaf = path->nodes[0];
1916 fi = btrfs_item_ptr(leaf, path->slots[0],
1917 struct btrfs_file_extent_item);
1918 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1919 btrfs_set_file_extent_type(leaf, fi, extent_type);
1920 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1921 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1922 btrfs_set_file_extent_offset(leaf, fi, 0);
1923 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1924 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1925 btrfs_set_file_extent_compression(leaf, fi, compression);
1926 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1927 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04001928
Yan Zhengd899e052008-10-30 14:25:28 -04001929 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04001930 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04001931
1932 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04001933
1934 ins.objectid = disk_bytenr;
1935 ins.offset = disk_num_bytes;
1936 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001937 ret = btrfs_alloc_reserved_file_extent(trans, root,
1938 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08001939 btrfs_ino(inode), file_pos, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001940out:
Yan Zhengd899e052008-10-30 14:25:28 -04001941 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04001942
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001943 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04001944}
1945
Liu Bo38c227d2013-01-29 03:18:40 +00001946/* snapshot-aware defrag */
1947struct sa_defrag_extent_backref {
1948 struct rb_node node;
1949 struct old_sa_defrag_extent *old;
1950 u64 root_id;
1951 u64 inum;
1952 u64 file_pos;
1953 u64 extent_offset;
1954 u64 num_bytes;
1955 u64 generation;
1956};
1957
1958struct old_sa_defrag_extent {
1959 struct list_head list;
1960 struct new_sa_defrag_extent *new;
1961
1962 u64 extent_offset;
1963 u64 bytenr;
1964 u64 offset;
1965 u64 len;
1966 int count;
1967};
1968
1969struct new_sa_defrag_extent {
1970 struct rb_root root;
1971 struct list_head head;
1972 struct btrfs_path *path;
1973 struct inode *inode;
1974 u64 file_pos;
1975 u64 len;
1976 u64 bytenr;
1977 u64 disk_len;
1978 u8 compress_type;
1979};
1980
1981static int backref_comp(struct sa_defrag_extent_backref *b1,
1982 struct sa_defrag_extent_backref *b2)
1983{
1984 if (b1->root_id < b2->root_id)
1985 return -1;
1986 else if (b1->root_id > b2->root_id)
1987 return 1;
1988
1989 if (b1->inum < b2->inum)
1990 return -1;
1991 else if (b1->inum > b2->inum)
1992 return 1;
1993
1994 if (b1->file_pos < b2->file_pos)
1995 return -1;
1996 else if (b1->file_pos > b2->file_pos)
1997 return 1;
1998
1999 /*
2000 * [------------------------------] ===> (a range of space)
2001 * |<--->| |<---->| =============> (fs/file tree A)
2002 * |<---------------------------->| ===> (fs/file tree B)
2003 *
2004 * A range of space can refer to two file extents in one tree while
2005 * refer to only one file extent in another tree.
2006 *
2007 * So we may process a disk offset more than one time(two extents in A)
2008 * and locate at the same extent(one extent in B), then insert two same
2009 * backrefs(both refer to the extent in B).
2010 */
2011 return 0;
2012}
2013
2014static void backref_insert(struct rb_root *root,
2015 struct sa_defrag_extent_backref *backref)
2016{
2017 struct rb_node **p = &root->rb_node;
2018 struct rb_node *parent = NULL;
2019 struct sa_defrag_extent_backref *entry;
2020 int ret;
2021
2022 while (*p) {
2023 parent = *p;
2024 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2025
2026 ret = backref_comp(backref, entry);
2027 if (ret < 0)
2028 p = &(*p)->rb_left;
2029 else
2030 p = &(*p)->rb_right;
2031 }
2032
2033 rb_link_node(&backref->node, parent, p);
2034 rb_insert_color(&backref->node, root);
2035}
2036
2037/*
2038 * Note the backref might has changed, and in this case we just return 0.
2039 */
2040static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2041 void *ctx)
2042{
2043 struct btrfs_file_extent_item *extent;
2044 struct btrfs_fs_info *fs_info;
2045 struct old_sa_defrag_extent *old = ctx;
2046 struct new_sa_defrag_extent *new = old->new;
2047 struct btrfs_path *path = new->path;
2048 struct btrfs_key key;
2049 struct btrfs_root *root;
2050 struct sa_defrag_extent_backref *backref;
2051 struct extent_buffer *leaf;
2052 struct inode *inode = new->inode;
2053 int slot;
2054 int ret;
2055 u64 extent_offset;
2056 u64 num_bytes;
2057
2058 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
2059 inum == btrfs_ino(inode))
2060 return 0;
2061
2062 key.objectid = root_id;
2063 key.type = BTRFS_ROOT_ITEM_KEY;
2064 key.offset = (u64)-1;
2065
2066 fs_info = BTRFS_I(inode)->root->fs_info;
2067 root = btrfs_read_fs_root_no_name(fs_info, &key);
2068 if (IS_ERR(root)) {
2069 if (PTR_ERR(root) == -ENOENT)
2070 return 0;
2071 WARN_ON(1);
2072 pr_debug("inum=%llu, offset=%llu, root_id=%llu\n",
2073 inum, offset, root_id);
2074 return PTR_ERR(root);
2075 }
2076
2077 key.objectid = inum;
2078 key.type = BTRFS_EXTENT_DATA_KEY;
2079 if (offset > (u64)-1 << 32)
2080 key.offset = 0;
2081 else
2082 key.offset = offset;
2083
2084 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302085 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002086 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002087 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002088
2089 while (1) {
2090 cond_resched();
2091
2092 leaf = path->nodes[0];
2093 slot = path->slots[0];
2094
2095 if (slot >= btrfs_header_nritems(leaf)) {
2096 ret = btrfs_next_leaf(root, path);
2097 if (ret < 0) {
2098 goto out;
2099 } else if (ret > 0) {
2100 ret = 0;
2101 goto out;
2102 }
2103 continue;
2104 }
2105
2106 path->slots[0]++;
2107
2108 btrfs_item_key_to_cpu(leaf, &key, slot);
2109
2110 if (key.objectid > inum)
2111 goto out;
2112
2113 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2114 continue;
2115
2116 extent = btrfs_item_ptr(leaf, slot,
2117 struct btrfs_file_extent_item);
2118
2119 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2120 continue;
2121
Liu Boe68afa42013-07-01 22:13:26 +08002122 /*
2123 * 'offset' refers to the exact key.offset,
2124 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2125 * (key.offset - extent_offset).
2126 */
2127 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002128 continue;
2129
Liu Boe68afa42013-07-01 22:13:26 +08002130 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002131 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002132
Liu Bo38c227d2013-01-29 03:18:40 +00002133 if (extent_offset >= old->extent_offset + old->offset +
2134 old->len || extent_offset + num_bytes <=
2135 old->extent_offset + old->offset)
2136 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002137 break;
2138 }
2139
2140 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2141 if (!backref) {
2142 ret = -ENOENT;
2143 goto out;
2144 }
2145
2146 backref->root_id = root_id;
2147 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002148 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002149 backref->num_bytes = num_bytes;
2150 backref->extent_offset = extent_offset;
2151 backref->generation = btrfs_file_extent_generation(leaf, extent);
2152 backref->old = old;
2153 backref_insert(&new->root, backref);
2154 old->count++;
2155out:
2156 btrfs_release_path(path);
2157 WARN_ON(ret);
2158 return ret;
2159}
2160
2161static noinline bool record_extent_backrefs(struct btrfs_path *path,
2162 struct new_sa_defrag_extent *new)
2163{
2164 struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info;
2165 struct old_sa_defrag_extent *old, *tmp;
2166 int ret;
2167
2168 new->path = path;
2169
2170 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002171 ret = iterate_inodes_from_logical(old->bytenr +
2172 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002173 path, record_one_backref,
2174 old);
Josef Bacik4724b102013-11-05 11:11:40 -05002175 if (ret < 0 && ret != -ENOENT)
2176 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002177
2178 /* no backref to be processed for this extent */
2179 if (!old->count) {
2180 list_del(&old->list);
2181 kfree(old);
2182 }
2183 }
2184
2185 if (list_empty(&new->head))
2186 return false;
2187
2188 return true;
2189}
2190
2191static int relink_is_mergable(struct extent_buffer *leaf,
2192 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002193 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002194{
Liu Bo116e0022013-08-02 16:30:40 +08002195 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002196 return 0;
2197
2198 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2199 return 0;
2200
Liu Bo116e0022013-08-02 16:30:40 +08002201 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2202 return 0;
2203
2204 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002205 btrfs_file_extent_other_encoding(leaf, fi))
2206 return 0;
2207
2208 return 1;
2209}
2210
2211/*
2212 * Note the backref might has changed, and in this case we just return 0.
2213 */
2214static noinline int relink_extent_backref(struct btrfs_path *path,
2215 struct sa_defrag_extent_backref *prev,
2216 struct sa_defrag_extent_backref *backref)
2217{
2218 struct btrfs_file_extent_item *extent;
2219 struct btrfs_file_extent_item *item;
2220 struct btrfs_ordered_extent *ordered;
2221 struct btrfs_trans_handle *trans;
2222 struct btrfs_fs_info *fs_info;
2223 struct btrfs_root *root;
2224 struct btrfs_key key;
2225 struct extent_buffer *leaf;
2226 struct old_sa_defrag_extent *old = backref->old;
2227 struct new_sa_defrag_extent *new = old->new;
2228 struct inode *src_inode = new->inode;
2229 struct inode *inode;
2230 struct extent_state *cached = NULL;
2231 int ret = 0;
2232 u64 start;
2233 u64 len;
2234 u64 lock_start;
2235 u64 lock_end;
2236 bool merge = false;
2237 int index;
2238
2239 if (prev && prev->root_id == backref->root_id &&
2240 prev->inum == backref->inum &&
2241 prev->file_pos + prev->num_bytes == backref->file_pos)
2242 merge = true;
2243
2244 /* step 1: get root */
2245 key.objectid = backref->root_id;
2246 key.type = BTRFS_ROOT_ITEM_KEY;
2247 key.offset = (u64)-1;
2248
2249 fs_info = BTRFS_I(src_inode)->root->fs_info;
2250 index = srcu_read_lock(&fs_info->subvol_srcu);
2251
2252 root = btrfs_read_fs_root_no_name(fs_info, &key);
2253 if (IS_ERR(root)) {
2254 srcu_read_unlock(&fs_info->subvol_srcu, index);
2255 if (PTR_ERR(root) == -ENOENT)
2256 return 0;
2257 return PTR_ERR(root);
2258 }
Liu Bo38c227d2013-01-29 03:18:40 +00002259
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002260 if (btrfs_root_readonly(root)) {
2261 srcu_read_unlock(&fs_info->subvol_srcu, index);
2262 return 0;
2263 }
2264
Liu Bo38c227d2013-01-29 03:18:40 +00002265 /* step 2: get inode */
2266 key.objectid = backref->inum;
2267 key.type = BTRFS_INODE_ITEM_KEY;
2268 key.offset = 0;
2269
2270 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2271 if (IS_ERR(inode)) {
2272 srcu_read_unlock(&fs_info->subvol_srcu, index);
2273 return 0;
2274 }
2275
2276 srcu_read_unlock(&fs_info->subvol_srcu, index);
2277
2278 /* step 3: relink backref */
2279 lock_start = backref->file_pos;
2280 lock_end = backref->file_pos + backref->num_bytes - 1;
2281 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2282 0, &cached);
2283
2284 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2285 if (ordered) {
2286 btrfs_put_ordered_extent(ordered);
2287 goto out_unlock;
2288 }
2289
2290 trans = btrfs_join_transaction(root);
2291 if (IS_ERR(trans)) {
2292 ret = PTR_ERR(trans);
2293 goto out_unlock;
2294 }
2295
2296 key.objectid = backref->inum;
2297 key.type = BTRFS_EXTENT_DATA_KEY;
2298 key.offset = backref->file_pos;
2299
2300 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2301 if (ret < 0) {
2302 goto out_free_path;
2303 } else if (ret > 0) {
2304 ret = 0;
2305 goto out_free_path;
2306 }
2307
2308 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2309 struct btrfs_file_extent_item);
2310
2311 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2312 backref->generation)
2313 goto out_free_path;
2314
2315 btrfs_release_path(path);
2316
2317 start = backref->file_pos;
2318 if (backref->extent_offset < old->extent_offset + old->offset)
2319 start += old->extent_offset + old->offset -
2320 backref->extent_offset;
2321
2322 len = min(backref->extent_offset + backref->num_bytes,
2323 old->extent_offset + old->offset + old->len);
2324 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2325
2326 ret = btrfs_drop_extents(trans, root, inode, start,
2327 start + len, 1);
2328 if (ret)
2329 goto out_free_path;
2330again:
2331 key.objectid = btrfs_ino(inode);
2332 key.type = BTRFS_EXTENT_DATA_KEY;
2333 key.offset = start;
2334
Liu Boa09a0a72013-03-11 09:20:58 +00002335 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002336 if (merge) {
2337 struct btrfs_file_extent_item *fi;
2338 u64 extent_len;
2339 struct btrfs_key found_key;
2340
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002341 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002342 if (ret < 0)
2343 goto out_free_path;
2344
2345 path->slots[0]--;
2346 leaf = path->nodes[0];
2347 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2348
2349 fi = btrfs_item_ptr(leaf, path->slots[0],
2350 struct btrfs_file_extent_item);
2351 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2352
Liu Bo116e0022013-08-02 16:30:40 +08002353 if (extent_len + found_key.offset == start &&
2354 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002355 btrfs_set_file_extent_num_bytes(leaf, fi,
2356 extent_len + len);
2357 btrfs_mark_buffer_dirty(leaf);
2358 inode_add_bytes(inode, len);
2359
2360 ret = 1;
2361 goto out_free_path;
2362 } else {
2363 merge = false;
2364 btrfs_release_path(path);
2365 goto again;
2366 }
2367 }
2368
2369 ret = btrfs_insert_empty_item(trans, root, path, &key,
2370 sizeof(*extent));
2371 if (ret) {
2372 btrfs_abort_transaction(trans, root, ret);
2373 goto out_free_path;
2374 }
2375
2376 leaf = path->nodes[0];
2377 item = btrfs_item_ptr(leaf, path->slots[0],
2378 struct btrfs_file_extent_item);
2379 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2380 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2381 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2382 btrfs_set_file_extent_num_bytes(leaf, item, len);
2383 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2384 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2385 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2386 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2387 btrfs_set_file_extent_encryption(leaf, item, 0);
2388 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2389
2390 btrfs_mark_buffer_dirty(leaf);
2391 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002392 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002393
2394 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
2395 new->disk_len, 0,
2396 backref->root_id, backref->inum,
2397 new->file_pos, 0); /* start - extent_offset */
2398 if (ret) {
2399 btrfs_abort_transaction(trans, root, ret);
2400 goto out_free_path;
2401 }
2402
2403 ret = 1;
2404out_free_path:
2405 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002406 path->leave_spinning = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002407 btrfs_end_transaction(trans, root);
2408out_unlock:
2409 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2410 &cached, GFP_NOFS);
2411 iput(inode);
2412 return ret;
2413}
2414
Liu Bo6f519562013-10-29 10:45:05 +08002415static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2416{
2417 struct old_sa_defrag_extent *old, *tmp;
2418
2419 if (!new)
2420 return;
2421
2422 list_for_each_entry_safe(old, tmp, &new->head, list) {
2423 list_del(&old->list);
2424 kfree(old);
2425 }
2426 kfree(new);
2427}
2428
Liu Bo38c227d2013-01-29 03:18:40 +00002429static void relink_file_extents(struct new_sa_defrag_extent *new)
2430{
2431 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002432 struct sa_defrag_extent_backref *backref;
2433 struct sa_defrag_extent_backref *prev = NULL;
2434 struct inode *inode;
2435 struct btrfs_root *root;
2436 struct rb_node *node;
2437 int ret;
2438
2439 inode = new->inode;
2440 root = BTRFS_I(inode)->root;
2441
2442 path = btrfs_alloc_path();
2443 if (!path)
2444 return;
2445
2446 if (!record_extent_backrefs(path, new)) {
2447 btrfs_free_path(path);
2448 goto out;
2449 }
2450 btrfs_release_path(path);
2451
2452 while (1) {
2453 node = rb_first(&new->root);
2454 if (!node)
2455 break;
2456 rb_erase(node, &new->root);
2457
2458 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2459
2460 ret = relink_extent_backref(path, prev, backref);
2461 WARN_ON(ret < 0);
2462
2463 kfree(prev);
2464
2465 if (ret == 1)
2466 prev = backref;
2467 else
2468 prev = NULL;
2469 cond_resched();
2470 }
2471 kfree(prev);
2472
2473 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002474out:
Liu Bo6f519562013-10-29 10:45:05 +08002475 free_sa_defrag_extent(new);
2476
Liu Bo38c227d2013-01-29 03:18:40 +00002477 atomic_dec(&root->fs_info->defrag_running);
2478 wake_up(&root->fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002479}
2480
2481static struct new_sa_defrag_extent *
2482record_old_file_extents(struct inode *inode,
2483 struct btrfs_ordered_extent *ordered)
2484{
2485 struct btrfs_root *root = BTRFS_I(inode)->root;
2486 struct btrfs_path *path;
2487 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002488 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002489 struct new_sa_defrag_extent *new;
2490 int ret;
2491
2492 new = kmalloc(sizeof(*new), GFP_NOFS);
2493 if (!new)
2494 return NULL;
2495
2496 new->inode = inode;
2497 new->file_pos = ordered->file_offset;
2498 new->len = ordered->len;
2499 new->bytenr = ordered->start;
2500 new->disk_len = ordered->disk_len;
2501 new->compress_type = ordered->compress_type;
2502 new->root = RB_ROOT;
2503 INIT_LIST_HEAD(&new->head);
2504
2505 path = btrfs_alloc_path();
2506 if (!path)
2507 goto out_kfree;
2508
2509 key.objectid = btrfs_ino(inode);
2510 key.type = BTRFS_EXTENT_DATA_KEY;
2511 key.offset = new->file_pos;
2512
2513 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2514 if (ret < 0)
2515 goto out_free_path;
2516 if (ret > 0 && path->slots[0] > 0)
2517 path->slots[0]--;
2518
2519 /* find out all the old extents for the file range */
2520 while (1) {
2521 struct btrfs_file_extent_item *extent;
2522 struct extent_buffer *l;
2523 int slot;
2524 u64 num_bytes;
2525 u64 offset;
2526 u64 end;
2527 u64 disk_bytenr;
2528 u64 extent_offset;
2529
2530 l = path->nodes[0];
2531 slot = path->slots[0];
2532
2533 if (slot >= btrfs_header_nritems(l)) {
2534 ret = btrfs_next_leaf(root, path);
2535 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002536 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002537 else if (ret > 0)
2538 break;
2539 continue;
2540 }
2541
2542 btrfs_item_key_to_cpu(l, &key, slot);
2543
2544 if (key.objectid != btrfs_ino(inode))
2545 break;
2546 if (key.type != BTRFS_EXTENT_DATA_KEY)
2547 break;
2548 if (key.offset >= new->file_pos + new->len)
2549 break;
2550
2551 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2552
2553 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2554 if (key.offset + num_bytes < new->file_pos)
2555 goto next;
2556
2557 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2558 if (!disk_bytenr)
2559 goto next;
2560
2561 extent_offset = btrfs_file_extent_offset(l, extent);
2562
2563 old = kmalloc(sizeof(*old), GFP_NOFS);
2564 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002565 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002566
2567 offset = max(new->file_pos, key.offset);
2568 end = min(new->file_pos + new->len, key.offset + num_bytes);
2569
2570 old->bytenr = disk_bytenr;
2571 old->extent_offset = extent_offset;
2572 old->offset = offset - key.offset;
2573 old->len = end - offset;
2574 old->new = new;
2575 old->count = 0;
2576 list_add_tail(&old->list, &new->head);
2577next:
2578 path->slots[0]++;
2579 cond_resched();
2580 }
2581
2582 btrfs_free_path(path);
2583 atomic_inc(&root->fs_info->defrag_running);
2584
2585 return new;
2586
Liu Bo38c227d2013-01-29 03:18:40 +00002587out_free_path:
2588 btrfs_free_path(path);
2589out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002590 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002591 return NULL;
2592}
2593
Chris Masond352ac62008-09-29 15:18:18 -04002594/* as ordered data IO finishes, this gets called so we can finish
2595 * an ordered extent if the range of bytes in the file it covers are
2596 * fully written.
2597 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002598static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002599{
Josef Bacik5fd02042012-05-02 14:00:54 -04002600 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002601 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002602 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002603 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002604 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002605 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002606 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002607 int ret = 0;
2608 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002609 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002610 bool truncated = false;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002611
Liu Bo83eea1f2012-07-10 05:28:39 -06002612 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002613
Josef Bacik5fd02042012-05-02 14:00:54 -04002614 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2615 ret = -EIO;
2616 goto out;
2617 }
2618
Josef Bacik77cef2e2013-08-29 13:57:21 -04002619 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2620 truncated = true;
2621 logical_len = ordered_extent->truncated_len;
2622 /* Truncated the entire extent, don't bother adding */
2623 if (!logical_len)
2624 goto out;
2625 }
2626
Yan, Zhengc2167752009-11-12 09:34:21 +00002627 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002628 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Josef Bacik6c760c02012-11-09 10:53:21 -05002629 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2630 if (nolock)
2631 trans = btrfs_join_transaction_nolock(root);
2632 else
2633 trans = btrfs_join_transaction(root);
2634 if (IS_ERR(trans)) {
2635 ret = PTR_ERR(trans);
2636 trans = NULL;
2637 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002638 }
Josef Bacik6c760c02012-11-09 10:53:21 -05002639 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
2640 ret = btrfs_update_inode_fallback(trans, root, inode);
2641 if (ret) /* -ENOMEM or corruption */
2642 btrfs_abort_transaction(trans, root, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002643 goto out;
2644 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002645
Josef Bacik2ac55d42010-02-03 19:33:23 +00002646 lock_extent_bits(io_tree, ordered_extent->file_offset,
2647 ordered_extent->file_offset + ordered_extent->len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002648 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002649
Liu Bo38c227d2013-01-29 03:18:40 +00002650 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2651 ordered_extent->file_offset + ordered_extent->len - 1,
2652 EXTENT_DEFRAG, 1, cached_state);
2653 if (ret) {
2654 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05002655 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00002656 /* the inode is shared */
2657 new = record_old_file_extents(inode, ordered_extent);
2658
2659 clear_extent_bit(io_tree, ordered_extent->file_offset,
2660 ordered_extent->file_offset + ordered_extent->len - 1,
2661 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2662 }
2663
Josef Bacik0cb59c92010-07-02 12:14:14 -04002664 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002665 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002666 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002667 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002668 if (IS_ERR(trans)) {
2669 ret = PTR_ERR(trans);
2670 trans = NULL;
2671 goto out_unlock;
2672 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002673 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002674
Chris Masonc8b97812008-10-29 14:49:59 -04002675 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002676 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002677 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002678 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00002679 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002680 ordered_extent->file_offset,
2681 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002682 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002683 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04002684 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002685 ret = insert_reserved_file_extent(trans, inode,
2686 ordered_extent->file_offset,
2687 ordered_extent->start,
2688 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04002689 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08002690 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002691 BTRFS_FILE_EXTENT_REG);
Yan Zhengd899e052008-10-30 14:25:28 -04002692 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002693 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2694 ordered_extent->file_offset, ordered_extent->len,
2695 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002696 if (ret < 0) {
2697 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002698 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002699 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002700
Chris Masone6dcd2d2008-07-17 12:53:50 -04002701 add_pending_csums(trans, inode, ordered_extent->file_offset,
2702 &ordered_extent->list);
2703
Josef Bacik6c760c02012-11-09 10:53:21 -05002704 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2705 ret = btrfs_update_inode_fallback(trans, root, inode);
2706 if (ret) { /* -ENOMEM or corruption */
2707 btrfs_abort_transaction(trans, root, ret);
2708 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002709 }
2710 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002711out_unlock:
2712 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2713 ordered_extent->file_offset +
2714 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00002715out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002716 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04002717 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002718 if (trans)
2719 btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002720
Josef Bacik77cef2e2013-08-29 13:57:21 -04002721 if (ret || truncated) {
2722 u64 start, end;
2723
2724 if (truncated)
2725 start = ordered_extent->file_offset + logical_len;
2726 else
2727 start = ordered_extent->file_offset;
2728 end = ordered_extent->file_offset + ordered_extent->len - 1;
2729 clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS);
2730
2731 /* Drop the cache for the part of the extent we didn't write. */
2732 btrfs_drop_extent_cache(inode, start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04002733
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002734 /*
2735 * If the ordered extent had an IOERR or something else went
2736 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04002737 * back to the allocator. We only free the extent in the
2738 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002739 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04002740 if ((ret || !logical_len) &&
2741 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002742 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
2743 btrfs_free_reserved_extent(root, ordered_extent->start,
2744 ordered_extent->disk_len);
2745 }
2746
2747
Josef Bacik5fd02042012-05-02 14:00:54 -04002748 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002749 * This needs to be done to make sure anybody waiting knows we are done
2750 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002751 */
2752 btrfs_remove_ordered_extent(inode, ordered_extent);
2753
Liu Bo38c227d2013-01-29 03:18:40 +00002754 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08002755 if (new) {
2756 if (ret) {
2757 free_sa_defrag_extent(new);
2758 atomic_dec(&root->fs_info->defrag_running);
2759 } else {
2760 relink_file_extents(new);
2761 }
2762 }
Liu Bo38c227d2013-01-29 03:18:40 +00002763
Chris Masone6dcd2d2008-07-17 12:53:50 -04002764 /* once for us */
2765 btrfs_put_ordered_extent(ordered_extent);
2766 /* once for the tree */
2767 btrfs_put_ordered_extent(ordered_extent);
2768
Josef Bacik5fd02042012-05-02 14:00:54 -04002769 return ret;
2770}
2771
Qu Wenruod458b052014-02-28 10:46:19 +08002772static void finish_ordered_fn(struct btrfs_work *work)
Josef Bacik5fd02042012-05-02 14:00:54 -04002773{
2774 struct btrfs_ordered_extent *ordered_extent;
2775 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2776 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002777}
2778
Christoph Hellwigb2950862008-12-02 09:54:17 -05002779static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04002780 struct extent_state *state, int uptodate)
2781{
Josef Bacik5fd02042012-05-02 14:00:54 -04002782 struct inode *inode = page->mapping->host;
2783 struct btrfs_root *root = BTRFS_I(inode)->root;
2784 struct btrfs_ordered_extent *ordered_extent = NULL;
Qu Wenruod458b052014-02-28 10:46:19 +08002785 struct btrfs_workqueue *workers;
Josef Bacik5fd02042012-05-02 14:00:54 -04002786
liubo1abe9b82011-03-24 11:18:59 +00002787 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2788
Chris Mason8b62b722009-09-02 16:53:46 -04002789 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002790 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2791 end - start + 1, uptodate))
2792 return 0;
2793
Qu Wenruofccb5d82014-02-28 10:46:10 +08002794 btrfs_init_work(&ordered_extent->work, finish_ordered_fn, NULL, NULL);
Josef Bacik5fd02042012-05-02 14:00:54 -04002795
Liu Bo83eea1f2012-07-10 05:28:39 -06002796 if (btrfs_is_free_space_inode(inode))
Qu Wenruofccb5d82014-02-28 10:46:10 +08002797 workers = root->fs_info->endio_freespace_worker;
Josef Bacik5fd02042012-05-02 14:00:54 -04002798 else
Qu Wenruofccb5d82014-02-28 10:46:10 +08002799 workers = root->fs_info->endio_write_workers;
2800 btrfs_queue_work(workers, &ordered_extent->work);
Josef Bacik5fd02042012-05-02 14:00:54 -04002801
2802 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04002803}
2804
Chris Masond352ac62008-09-29 15:18:18 -04002805/*
Chris Masond352ac62008-09-29 15:18:18 -04002806 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002807 * if there's a match, we allow the bio to finish. If not, the code in
2808 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002809 */
Miao Xiefacc8a222013-07-25 19:22:34 +08002810static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
2811 u64 phy_offset, struct page *page,
2812 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002813{
Miao Xie4eee4fa2012-12-21 09:17:45 +00002814 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04002815 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002816 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04002817 char *kaddr;
Chris Masonff79f812007-10-15 16:22:25 -04002818 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiefacc8a222013-07-25 19:22:34 +08002819 u32 csum_expected;
Chris Masonff79f812007-10-15 16:22:25 -04002820 u32 csum = ~(u32)0;
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002821 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
2822 DEFAULT_RATELIMIT_BURST);
Chris Masond1310b22008-01-24 16:13:08 -05002823
Chris Masond20f7042008-12-08 16:58:54 -05002824 if (PageChecked(page)) {
2825 ClearPageChecked(page);
2826 goto good;
2827 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002828
2829 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Jan Schmidt08d2f342011-05-04 16:18:50 +02002830 goto good;
Chris Masond20f7042008-12-08 16:58:54 -05002831
Yan Zheng17d217f2008-12-12 10:03:38 -05002832 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002833 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002834 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
2835 GFP_NOFS);
2836 return 0;
2837 }
2838
Miao Xiefacc8a222013-07-25 19:22:34 +08002839 phy_offset >>= inode->i_sb->s_blocksize_bits;
2840 csum_expected = *(((u32 *)io_bio->csum) + phy_offset);
Chris Masond3977122009-01-05 21:25:51 -05002841
Miao Xiefacc8a222013-07-25 19:22:34 +08002842 kaddr = kmap_atomic(page);
Liu Bob0496682013-03-14 14:57:45 +00002843 csum = btrfs_csum_data(kaddr + offset, csum, end - start + 1);
Chris Masonff79f812007-10-15 16:22:25 -04002844 btrfs_csum_final(csum, (char *)&csum);
Miao Xiefacc8a222013-07-25 19:22:34 +08002845 if (csum != csum_expected)
Chris Mason07157aa2007-08-30 08:50:51 -04002846 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002847
Cong Wang7ac687d2011-11-25 23:14:28 +08002848 kunmap_atomic(kaddr);
Chris Masond20f7042008-12-08 16:58:54 -05002849good:
Chris Mason07157aa2007-08-30 08:50:51 -04002850 return 0;
2851
2852zeroit:
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002853 if (__ratelimit(&_rs))
Miao Xiefacc8a222013-07-25 19:22:34 +08002854 btrfs_info(root->fs_info, "csum failed ino %llu off %llu csum %u expected csum %u",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002855 btrfs_ino(page->mapping->host), start, csum, csum_expected);
Chris Masondb945352007-10-15 16:15:53 -04002856 memset(kaddr + offset, 1, end - start + 1);
2857 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002858 kunmap_atomic(kaddr);
Miao Xiefacc8a222013-07-25 19:22:34 +08002859 if (csum_expected == 0)
Chris Mason3b951512008-04-17 11:29:12 -04002860 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04002861 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04002862}
Chris Masonb888db2b2007-08-27 16:49:44 -04002863
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002864struct delayed_iput {
2865 struct list_head list;
2866 struct inode *inode;
2867};
2868
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002869/* JDM: If this is fs-wide, why can't we add a pointer to
2870 * btrfs_inode instead and avoid the allocation? */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002871void btrfs_add_delayed_iput(struct inode *inode)
2872{
2873 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2874 struct delayed_iput *delayed;
2875
2876 if (atomic_add_unless(&inode->i_count, -1, 1))
2877 return;
2878
2879 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2880 delayed->inode = inode;
2881
2882 spin_lock(&fs_info->delayed_iput_lock);
2883 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2884 spin_unlock(&fs_info->delayed_iput_lock);
2885}
2886
2887void btrfs_run_delayed_iputs(struct btrfs_root *root)
2888{
2889 LIST_HEAD(list);
2890 struct btrfs_fs_info *fs_info = root->fs_info;
2891 struct delayed_iput *delayed;
2892 int empty;
2893
2894 spin_lock(&fs_info->delayed_iput_lock);
2895 empty = list_empty(&fs_info->delayed_iputs);
2896 spin_unlock(&fs_info->delayed_iput_lock);
2897 if (empty)
2898 return;
2899
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002900 spin_lock(&fs_info->delayed_iput_lock);
2901 list_splice_init(&fs_info->delayed_iputs, &list);
2902 spin_unlock(&fs_info->delayed_iput_lock);
2903
2904 while (!list_empty(&list)) {
2905 delayed = list_entry(list.next, struct delayed_iput, list);
2906 list_del(&delayed->list);
2907 iput(delayed->inode);
2908 kfree(delayed);
2909 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002910}
2911
Yan, Zhengd68fc572010-05-16 10:49:58 -04002912/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08002913 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04002914 * files in the subvolume, it removes orphan item and frees block_rsv
2915 * structure.
2916 */
2917void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
2918 struct btrfs_root *root)
2919{
Josef Bacik90290e12011-12-02 15:44:12 -05002920 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002921 int ret;
2922
Josef Bacik8a35d952012-05-23 14:26:42 -04002923 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04002924 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
2925 return;
2926
Josef Bacik90290e12011-12-02 15:44:12 -05002927 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04002928 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05002929 spin_unlock(&root->orphan_lock);
2930 return;
2931 }
2932
2933 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
2934 spin_unlock(&root->orphan_lock);
2935 return;
2936 }
2937
2938 block_rsv = root->orphan_block_rsv;
2939 root->orphan_block_rsv = NULL;
2940 spin_unlock(&root->orphan_lock);
2941
Yan, Zhengd68fc572010-05-16 10:49:58 -04002942 if (root->orphan_item_inserted &&
2943 btrfs_root_refs(&root->root_item) > 0) {
2944 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
2945 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04002946 if (ret)
2947 btrfs_abort_transaction(trans, root, ret);
2948 else
2949 root->orphan_item_inserted = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002950 }
2951
Josef Bacik90290e12011-12-02 15:44:12 -05002952 if (block_rsv) {
2953 WARN_ON(block_rsv->size > 0);
2954 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002955 }
2956}
2957
2958/*
Josef Bacik7b128762008-07-24 12:17:14 -04002959 * This creates an orphan entry for the given inode in case something goes
2960 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04002961 *
2962 * NOTE: caller of this function should reserve 5 units of metadata for
2963 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04002964 */
2965int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2966{
2967 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002968 struct btrfs_block_rsv *block_rsv = NULL;
2969 int reserve = 0;
2970 int insert = 0;
2971 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002972
Yan, Zhengd68fc572010-05-16 10:49:58 -04002973 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06002974 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00002975 if (!block_rsv)
2976 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04002977 }
2978
Yan, Zhengd68fc572010-05-16 10:49:58 -04002979 spin_lock(&root->orphan_lock);
2980 if (!root->orphan_block_rsv) {
2981 root->orphan_block_rsv = block_rsv;
2982 } else if (block_rsv) {
2983 btrfs_free_block_rsv(root, block_rsv);
2984 block_rsv = NULL;
2985 }
Josef Bacik7b128762008-07-24 12:17:14 -04002986
Josef Bacik8a35d952012-05-23 14:26:42 -04002987 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2988 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04002989#if 0
2990 /*
2991 * For proper ENOSPC handling, we should do orphan
2992 * cleanup when mounting. But this introduces backward
2993 * compatibility issue.
2994 */
2995 if (!xchg(&root->orphan_item_inserted, 1))
2996 insert = 2;
2997 else
2998 insert = 1;
2999#endif
3000 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003001 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003002 }
Josef Bacik7b128762008-07-24 12:17:14 -04003003
Josef Bacik72ac3c02012-05-23 14:13:11 -04003004 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3005 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003006 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003007 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003008
Yan, Zhengd68fc572010-05-16 10:49:58 -04003009 /* grab metadata reservation from transaction handle */
3010 if (reserve) {
3011 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003012 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04003013 }
3014
3015 /* insert an orphan item to track this unlinked/truncated file */
3016 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08003017 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003018 if (ret) {
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003019 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003020 if (reserve) {
3021 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3022 &BTRFS_I(inode)->runtime_flags);
3023 btrfs_orphan_release_metadata(inode);
3024 }
3025 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003026 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3027 &BTRFS_I(inode)->runtime_flags);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003028 btrfs_abort_transaction(trans, root, ret);
3029 return ret;
3030 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003031 }
3032 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003033 }
3034
3035 /* insert an orphan item to track subvolume contains orphan files */
3036 if (insert >= 2) {
3037 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
3038 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003039 if (ret && ret != -EEXIST) {
3040 btrfs_abort_transaction(trans, root, ret);
3041 return ret;
3042 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003043 }
3044 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003045}
3046
3047/*
3048 * We have done the truncate/delete so we can go ahead and remove the orphan
3049 * item for this particular inode.
3050 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003051static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3052 struct inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003053{
3054 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003055 int delete_item = 0;
3056 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003057 int ret = 0;
3058
Yan, Zhengd68fc572010-05-16 10:49:58 -04003059 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003060 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3061 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003062 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003063
Josef Bacik72ac3c02012-05-23 14:13:11 -04003064 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3065 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003066 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003067 spin_unlock(&root->orphan_lock);
3068
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003069 if (delete_item) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003070 atomic_dec(&root->orphan_inodes);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003071 if (trans)
3072 ret = btrfs_del_orphan_item(trans, root,
3073 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04003074 }
Josef Bacik7b128762008-07-24 12:17:14 -04003075
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003076 if (release_rsv)
3077 btrfs_orphan_release_metadata(inode);
3078
Josef Bacik4ef31a42013-08-13 14:10:08 -04003079 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003080}
3081
3082/*
3083 * this cleans up any orphans that may be left on the list from the last use
3084 * of this root.
3085 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003086int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003087{
3088 struct btrfs_path *path;
3089 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003090 struct btrfs_key key, found_key;
3091 struct btrfs_trans_handle *trans;
3092 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003093 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003094 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3095
Yan, Zhengd68fc572010-05-16 10:49:58 -04003096 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003097 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003098
3099 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003100 if (!path) {
3101 ret = -ENOMEM;
3102 goto out;
3103 }
Josef Bacik7b128762008-07-24 12:17:14 -04003104 path->reada = -1;
3105
3106 key.objectid = BTRFS_ORPHAN_OBJECTID;
3107 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
3108 key.offset = (u64)-1;
3109
Josef Bacik7b128762008-07-24 12:17:14 -04003110 while (1) {
3111 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003112 if (ret < 0)
3113 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003114
3115 /*
3116 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003117 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003118 * find the key and see if we have stuff that matches
3119 */
3120 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003121 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003122 if (path->slots[0] == 0)
3123 break;
3124 path->slots[0]--;
3125 }
3126
3127 /* pull out the item */
3128 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003129 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3130
3131 /* make sure the item matches what we want */
3132 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3133 break;
3134 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
3135 break;
3136
3137 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003138 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003139
3140 /*
3141 * this is where we are basically btrfs_lookup, without the
3142 * crossing root thing. we store the inode number in the
3143 * offset of the orphan item.
3144 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003145
3146 if (found_key.offset == last_objectid) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003147 btrfs_err(root->fs_info,
3148 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003149 ret = -EINVAL;
3150 goto out;
3151 }
3152
3153 last_objectid = found_key.offset;
3154
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003155 found_key.objectid = found_key.offset;
3156 found_key.type = BTRFS_INODE_ITEM_KEY;
3157 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00003158 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303159 ret = PTR_ERR_OR_ZERO(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003160 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003161 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003162
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003163 if (ret == -ESTALE && root == root->fs_info->tree_root) {
3164 struct btrfs_root *dead_root;
3165 struct btrfs_fs_info *fs_info = root->fs_info;
3166 int is_dead_root = 0;
3167
3168 /*
3169 * this is an orphan in the tree root. Currently these
3170 * could come from 2 sources:
3171 * a) a snapshot deletion in progress
3172 * b) a free space cache inode
3173 * We need to distinguish those two, as the snapshot
3174 * orphan must not get deleted.
3175 * find_dead_roots already ran before us, so if this
3176 * is a snapshot deletion, we should find the root
3177 * in the dead_roots list
3178 */
3179 spin_lock(&fs_info->trans_lock);
3180 list_for_each_entry(dead_root, &fs_info->dead_roots,
3181 root_list) {
3182 if (dead_root->root_key.objectid ==
3183 found_key.objectid) {
3184 is_dead_root = 1;
3185 break;
3186 }
3187 }
3188 spin_unlock(&fs_info->trans_lock);
3189 if (is_dead_root) {
3190 /* prevent this orphan from being found again */
3191 key.offset = found_key.objectid - 1;
3192 continue;
3193 }
3194 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003195 /*
3196 * Inode is already gone but the orphan item is still there,
3197 * kill the orphan item.
3198 */
3199 if (ret == -ESTALE) {
3200 trans = btrfs_start_transaction(root, 1);
3201 if (IS_ERR(trans)) {
3202 ret = PTR_ERR(trans);
3203 goto out;
3204 }
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003205 btrfs_debug(root->fs_info, "auto deleting %Lu",
3206 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003207 ret = btrfs_del_orphan_item(trans, root,
3208 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003209 btrfs_end_transaction(trans, root);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003210 if (ret)
3211 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003212 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003213 }
Josef Bacik7b128762008-07-24 12:17:14 -04003214
Josef Bacik7b128762008-07-24 12:17:14 -04003215 /*
3216 * add this inode to the orphan list so btrfs_orphan_del does
3217 * the proper thing when we hit it
3218 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003219 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3220 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003221 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003222
Josef Bacik7b128762008-07-24 12:17:14 -04003223 /* if we have links, this was a truncate, lets do that */
3224 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303225 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003226 iput(inode);
3227 continue;
3228 }
Josef Bacik7b128762008-07-24 12:17:14 -04003229 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003230
3231 /* 1 for the orphan item deletion. */
3232 trans = btrfs_start_transaction(root, 1);
3233 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003234 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003235 ret = PTR_ERR(trans);
3236 goto out;
3237 }
3238 ret = btrfs_orphan_add(trans, inode);
3239 btrfs_end_transaction(trans, root);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003240 if (ret) {
3241 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003242 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003243 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003244
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003245 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003246 if (ret)
3247 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003248 } else {
3249 nr_unlink++;
3250 }
3251
3252 /* this will do delete_inode and everything for us */
3253 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003254 if (ret)
3255 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003256 }
Miao Xie3254c872011-11-10 20:45:05 -05003257 /* release the path since we're done with it */
3258 btrfs_release_path(path);
3259
Yan, Zhengd68fc572010-05-16 10:49:58 -04003260 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3261
3262 if (root->orphan_block_rsv)
3263 btrfs_block_rsv_release(root, root->orphan_block_rsv,
3264 (u64)-1);
3265
3266 if (root->orphan_block_rsv || root->orphan_item_inserted) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003267 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003268 if (!IS_ERR(trans))
3269 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003270 }
Josef Bacik7b128762008-07-24 12:17:14 -04003271
3272 if (nr_unlink)
David Sterba4884b472013-03-20 13:31:27 +00003273 btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003274 if (nr_truncate)
David Sterba4884b472013-03-20 13:31:27 +00003275 btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003276
3277out:
3278 if (ret)
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003279 btrfs_crit(root->fs_info,
3280 "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003281 btrfs_free_path(path);
3282 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003283}
3284
Chris Masond352ac62008-09-29 15:18:18 -04003285/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003286 * very simple check to peek ahead in the leaf looking for xattrs. If we
3287 * don't find any xattrs, we know there can't be any acls.
3288 *
3289 * slot is the slot the inode is in, objectid is the objectid of the inode
3290 */
3291static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003292 int slot, u64 objectid,
3293 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003294{
3295 u32 nritems = btrfs_header_nritems(leaf);
3296 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003297 static u64 xattr_access = 0;
3298 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003299 int scanned = 0;
3300
Josef Bacikf23b5a52013-06-19 10:16:26 -04003301 if (!xattr_access) {
3302 xattr_access = btrfs_name_hash(POSIX_ACL_XATTR_ACCESS,
3303 strlen(POSIX_ACL_XATTR_ACCESS));
3304 xattr_default = btrfs_name_hash(POSIX_ACL_XATTR_DEFAULT,
3305 strlen(POSIX_ACL_XATTR_DEFAULT));
3306 }
3307
Chris Mason46a53cc2009-04-27 11:47:50 -04003308 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003309 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003310 while (slot < nritems) {
3311 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3312
3313 /* we found a different objectid, there must not be acls */
3314 if (found_key.objectid != objectid)
3315 return 0;
3316
3317 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003318 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003319 if (*first_xattr_slot == -1)
3320 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003321 if (found_key.offset == xattr_access ||
3322 found_key.offset == xattr_default)
3323 return 1;
3324 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003325
3326 /*
3327 * we found a key greater than an xattr key, there can't
3328 * be any acls later on
3329 */
3330 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3331 return 0;
3332
3333 slot++;
3334 scanned++;
3335
3336 /*
3337 * it goes inode, inode backrefs, xattrs, extents,
3338 * so if there are a ton of hard links to an inode there can
3339 * be a lot of backrefs. Don't waste time searching too hard,
3340 * this is just an optimization
3341 */
3342 if (scanned >= 8)
3343 break;
3344 }
3345 /* we hit the end of the leaf before we found an xattr or
3346 * something larger than an xattr. We have to assume the inode
3347 * has acls
3348 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003349 if (*first_xattr_slot == -1)
3350 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003351 return 1;
3352}
3353
3354/*
Chris Masond352ac62008-09-29 15:18:18 -04003355 * read an inode from the btree into the in-memory inode
3356 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003357static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003358{
3359 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003360 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003361 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04003362 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04003363 struct btrfs_root *root = BTRFS_I(inode)->root;
3364 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003365 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003366 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003367 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003368 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003369 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003370 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003371
3372 ret = btrfs_fill_inode(inode, &rdev);
3373 if (!ret)
3374 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003375
3376 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07003377 if (!path)
3378 goto make_bad;
3379
Chris Mason39279cc2007-06-12 06:35:45 -04003380 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003381
Chris Mason39279cc2007-06-12 06:35:45 -04003382 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003383 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04003384 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04003385
Chris Mason5f39d392007-10-15 16:14:19 -04003386 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003387
3388 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003389 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003390
Chris Mason5f39d392007-10-15 16:14:19 -04003391 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3392 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003393 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003394 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003395 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3396 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04003397 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003398
3399 tspec = btrfs_inode_atime(inode_item);
3400 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3401 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3402
3403 tspec = btrfs_inode_mtime(inode_item);
3404 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3405 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3406
3407 tspec = btrfs_inode_ctime(inode_item);
3408 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3409 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3410
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003411 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003412 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003413 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3414
3415 /*
3416 * If we were modified in the current generation and evicted from memory
3417 * and then re-read we need to do a full sync since we don't have any
3418 * idea about which extents were modified before we were evicted from
3419 * cache.
3420 */
3421 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
3422 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3423 &BTRFS_I(inode)->runtime_flags);
3424
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003425 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003426 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003427 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003428 rdev = btrfs_inode_rdev(leaf, inode_item);
3429
Josef Bacikaec74772008-07-24 12:12:38 -04003430 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003431 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003432
3433cache_index:
3434 path->slots[0]++;
3435 if (inode->i_nlink != 1 ||
3436 path->slots[0] >= btrfs_header_nritems(leaf))
3437 goto cache_acl;
3438
3439 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
3440 if (location.objectid != btrfs_ino(inode))
3441 goto cache_acl;
3442
3443 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3444 if (location.type == BTRFS_INODE_REF_KEY) {
3445 struct btrfs_inode_ref *ref;
3446
3447 ref = (struct btrfs_inode_ref *)ptr;
3448 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3449 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3450 struct btrfs_inode_extref *extref;
3451
3452 extref = (struct btrfs_inode_extref *)ptr;
3453 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3454 extref);
3455 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003456cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003457 /*
3458 * try to precache a NULL acl entry for files that don't have
3459 * any xattrs or acls
3460 */
Li Zefan33345d012011-04-20 10:31:50 +08003461 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003462 btrfs_ino(inode), &first_xattr_slot);
3463 if (first_xattr_slot != -1) {
3464 path->slots[0] = first_xattr_slot;
3465 ret = btrfs_load_inode_props(inode, path);
3466 if (ret)
3467 btrfs_err(root->fs_info,
3468 "error loading props for ino %llu (root %llu): %d\n",
3469 btrfs_ino(inode),
3470 root->root_key.objectid, ret);
3471 }
3472 btrfs_free_path(path);
3473
Al Viro72c04902009-06-24 16:58:48 -04003474 if (!maybe_acls)
3475 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003476
Chris Mason39279cc2007-06-12 06:35:45 -04003477 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003478 case S_IFREG:
3479 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003480 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05003481 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003482 inode->i_fop = &btrfs_file_operations;
3483 inode->i_op = &btrfs_file_inode_operations;
3484 break;
3485 case S_IFDIR:
3486 inode->i_fop = &btrfs_dir_file_operations;
3487 if (root == root->fs_info->tree_root)
3488 inode->i_op = &btrfs_dir_ro_inode_operations;
3489 else
3490 inode->i_op = &btrfs_dir_inode_operations;
3491 break;
3492 case S_IFLNK:
3493 inode->i_op = &btrfs_symlink_inode_operations;
3494 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04003495 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003496 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003497 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003498 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003499 init_special_inode(inode, inode->i_mode, rdev);
3500 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003501 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003502
3503 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003504 return;
3505
3506make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003507 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003508 make_bad_inode(inode);
3509}
3510
Chris Masond352ac62008-09-29 15:18:18 -04003511/*
3512 * given a leaf and an inode, copy the inode fields into the leaf
3513 */
Chris Masone02119d2008-09-05 16:13:11 -04003514static void fill_inode_item(struct btrfs_trans_handle *trans,
3515 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003516 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003517 struct inode *inode)
3518{
Liu Bo51fab692012-12-27 09:01:21 +00003519 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003520
Liu Bo51fab692012-12-27 09:01:21 +00003521 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003522
Liu Bo51fab692012-12-27 09:01:21 +00003523 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3524 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3525 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3526 &token);
3527 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3528 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003529
Liu Bo51fab692012-12-27 09:01:21 +00003530 btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item),
3531 inode->i_atime.tv_sec, &token);
3532 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item),
3533 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003534
Liu Bo51fab692012-12-27 09:01:21 +00003535 btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item),
3536 inode->i_mtime.tv_sec, &token);
3537 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item),
3538 inode->i_mtime.tv_nsec, &token);
3539
3540 btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item),
3541 inode->i_ctime.tv_sec, &token);
3542 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item),
3543 inode->i_ctime.tv_nsec, &token);
3544
3545 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3546 &token);
3547 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3548 &token);
3549 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3550 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3551 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3552 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3553 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003554}
3555
Chris Masond352ac62008-09-29 15:18:18 -04003556/*
3557 * copy everything in the in-memory inode into the btree.
3558 */
Chris Mason21151332011-11-10 20:39:08 -05003559static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003560 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003561{
3562 struct btrfs_inode_item *inode_item;
3563 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003564 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003565 int ret;
3566
3567 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003568 if (!path)
3569 return -ENOMEM;
3570
Chris Masonb9473432009-03-13 11:00:37 -04003571 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003572 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3573 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003574 if (ret) {
3575 if (ret > 0)
3576 ret = -ENOENT;
3577 goto failed;
3578 }
3579
Chris Mason5f39d392007-10-15 16:14:19 -04003580 leaf = path->nodes[0];
3581 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003582 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003583
Chris Masone02119d2008-09-05 16:13:11 -04003584 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003585 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003586 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003587 ret = 0;
3588failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003589 btrfs_free_path(path);
3590 return ret;
3591}
3592
Chris Masond352ac62008-09-29 15:18:18 -04003593/*
Chris Mason21151332011-11-10 20:39:08 -05003594 * copy everything in the in-memory inode into the btree.
3595 */
3596noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3597 struct btrfs_root *root, struct inode *inode)
3598{
3599 int ret;
3600
3601 /*
3602 * If the inode is a free space inode, we can deadlock during commit
3603 * if we put it into the delayed code.
3604 *
3605 * The data relocation inode should also be directly updated
3606 * without delay
3607 */
Liu Bo83eea1f2012-07-10 05:28:39 -06003608 if (!btrfs_is_free_space_inode(inode)
Chris Mason21151332011-11-10 20:39:08 -05003609 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003610 btrfs_update_root_times(trans, root);
3611
Chris Mason21151332011-11-10 20:39:08 -05003612 ret = btrfs_delayed_update_inode(trans, root, inode);
3613 if (!ret)
3614 btrfs_set_inode_last_trans(trans, inode);
3615 return ret;
3616 }
3617
3618 return btrfs_update_inode_item(trans, root, inode);
3619}
3620
Josef Bacikbe6aef62012-10-22 15:43:12 -04003621noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3622 struct btrfs_root *root,
3623 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003624{
3625 int ret;
3626
3627 ret = btrfs_update_inode(trans, root, inode);
3628 if (ret == -ENOSPC)
3629 return btrfs_update_inode_item(trans, root, inode);
3630 return ret;
3631}
3632
3633/*
Chris Masond352ac62008-09-29 15:18:18 -04003634 * unlink helper that gets used here in inode.c and in the tree logging
3635 * recovery code. It remove a link in a directory with a given name, and
3636 * also drops the back refs in the inode to the directory
3637 */
Al Viro92986792011-03-04 17:14:37 +00003638static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3639 struct btrfs_root *root,
3640 struct inode *dir, struct inode *inode,
3641 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003642{
3643 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003644 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003645 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003646 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003647 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003648 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003649 u64 ino = btrfs_ino(inode);
3650 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003651
3652 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003653 if (!path) {
3654 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003655 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003656 }
3657
Chris Masonb9473432009-03-13 11:00:37 -04003658 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003659 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003660 name, name_len, -1);
3661 if (IS_ERR(di)) {
3662 ret = PTR_ERR(di);
3663 goto err;
3664 }
3665 if (!di) {
3666 ret = -ENOENT;
3667 goto err;
3668 }
Chris Mason5f39d392007-10-15 16:14:19 -04003669 leaf = path->nodes[0];
3670 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003671 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003672 if (ret)
3673 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003674 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003675
Miao Xie67de1172013-12-26 13:07:06 +08003676 /*
3677 * If we don't have dir index, we have to get it by looking up
3678 * the inode ref, since we get the inode ref, remove it directly,
3679 * it is unnecessary to do delayed deletion.
3680 *
3681 * But if we have dir index, needn't search inode ref to get it.
3682 * Since the inode ref is close to the inode item, it is better
3683 * that we delay to delete it, and just do this deletion when
3684 * we update the inode item.
3685 */
3686 if (BTRFS_I(inode)->dir_index) {
3687 ret = btrfs_delayed_delete_inode_ref(inode);
3688 if (!ret) {
3689 index = BTRFS_I(inode)->dir_index;
3690 goto skip_backref;
3691 }
3692 }
3693
Li Zefan33345d012011-04-20 10:31:50 +08003694 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3695 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003696 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003697 btrfs_info(root->fs_info,
3698 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003699 name_len, name, ino, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003700 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003701 goto err;
3702 }
Miao Xie67de1172013-12-26 13:07:06 +08003703skip_backref:
Miao Xie16cdcec2011-04-22 18:12:22 +08003704 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003705 if (ret) {
3706 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003707 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003708 }
Chris Mason39279cc2007-06-12 06:35:45 -04003709
Chris Masone02119d2008-09-05 16:13:11 -04003710 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08003711 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003712 if (ret != 0 && ret != -ENOENT) {
3713 btrfs_abort_transaction(trans, root, ret);
3714 goto err;
3715 }
Chris Masone02119d2008-09-05 16:13:11 -04003716
3717 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
3718 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04003719 if (ret == -ENOENT)
3720 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003721 else if (ret)
3722 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003723err:
3724 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003725 if (ret)
3726 goto out;
3727
3728 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003729 inode_inc_iversion(inode);
3730 inode_inc_iversion(dir);
Chris Masone02119d2008-09-05 16:13:11 -04003731 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Tsutomu Itohb9959292012-06-25 21:25:22 -06003732 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04003733out:
Chris Mason39279cc2007-06-12 06:35:45 -04003734 return ret;
3735}
3736
Al Viro92986792011-03-04 17:14:37 +00003737int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3738 struct btrfs_root *root,
3739 struct inode *dir, struct inode *inode,
3740 const char *name, int name_len)
3741{
3742 int ret;
3743 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3744 if (!ret) {
Zach Brown8b558c52013-10-16 12:10:34 -07003745 drop_nlink(inode);
Al Viro92986792011-03-04 17:14:37 +00003746 ret = btrfs_update_inode(trans, root, inode);
3747 }
3748 return ret;
3749}
Yan, Zhenga22285a2010-05-16 10:48:46 -04003750
3751/*
3752 * helper to start transaction for unlink and rmdir.
3753 *
Josef Bacikd52be812013-05-29 14:54:47 -04003754 * unlink and rmdir are special in btrfs, they do not always free space, so
3755 * if we cannot make our reservations the normal way try and see if there is
3756 * plenty of slack room in the global reserve to migrate, otherwise we cannot
3757 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04003758 */
Josef Bacikd52be812013-05-29 14:54:47 -04003759static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003760{
3761 struct btrfs_trans_handle *trans;
3762 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003763 int ret;
3764
Josef Bacike70bea52011-10-11 14:18:24 -04003765 /*
3766 * 1 for the possible orphan item
3767 * 1 for the dir item
3768 * 1 for the dir index
3769 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04003770 * 1 for the inode
3771 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04003772 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003773 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
3774 return trans;
3775
Josef Bacikd52be812013-05-29 14:54:47 -04003776 if (PTR_ERR(trans) == -ENOSPC) {
3777 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003778
Josef Bacikd52be812013-05-29 14:54:47 -04003779 trans = btrfs_start_transaction(root, 0);
3780 if (IS_ERR(trans))
3781 return trans;
3782 ret = btrfs_cond_migrate_bytes(root->fs_info,
3783 &root->fs_info->trans_block_rsv,
3784 num_bytes, 5);
3785 if (ret) {
3786 btrfs_end_transaction(trans, root);
3787 return ERR_PTR(ret);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003788 }
Josef Bacik5a77d762011-11-01 14:32:23 -04003789 trans->block_rsv = &root->fs_info->trans_block_rsv;
Josef Bacikd52be812013-05-29 14:54:47 -04003790 trans->bytes_reserved = num_bytes;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003791 }
Josef Bacikd52be812013-05-29 14:54:47 -04003792 return trans;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003793}
3794
Chris Mason39279cc2007-06-12 06:35:45 -04003795static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3796{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003797 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003798 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04003799 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003800 int ret;
3801
Josef Bacikd52be812013-05-29 14:54:47 -04003802 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003803 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003804 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003805
Chris Mason12fcfd22009-03-24 10:24:20 -04003806 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
3807
Chris Masone02119d2008-09-05 16:13:11 -04003808 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3809 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003810 if (ret)
3811 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003812
Yan, Zhenga22285a2010-05-16 10:48:46 -04003813 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04003814 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003815 if (ret)
3816 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003817 }
Josef Bacik7b128762008-07-24 12:17:14 -04003818
Tsutomu Itohb5324022011-07-19 07:27:20 +00003819out:
Josef Bacikd52be812013-05-29 14:54:47 -04003820 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003821 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04003822 return ret;
3823}
3824
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003825int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3826 struct btrfs_root *root,
3827 struct inode *dir, u64 objectid,
3828 const char *name, int name_len)
3829{
3830 struct btrfs_path *path;
3831 struct extent_buffer *leaf;
3832 struct btrfs_dir_item *di;
3833 struct btrfs_key key;
3834 u64 index;
3835 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003836 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003837
3838 path = btrfs_alloc_path();
3839 if (!path)
3840 return -ENOMEM;
3841
Li Zefan33345d012011-04-20 10:31:50 +08003842 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003843 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003844 if (IS_ERR_OR_NULL(di)) {
3845 if (!di)
3846 ret = -ENOENT;
3847 else
3848 ret = PTR_ERR(di);
3849 goto out;
3850 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003851
3852 leaf = path->nodes[0];
3853 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3854 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3855 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003856 if (ret) {
3857 btrfs_abort_transaction(trans, root, ret);
3858 goto out;
3859 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003860 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003861
3862 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3863 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003864 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003865 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003866 if (ret != -ENOENT) {
3867 btrfs_abort_transaction(trans, root, ret);
3868 goto out;
3869 }
Li Zefan33345d012011-04-20 10:31:50 +08003870 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003871 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003872 if (IS_ERR_OR_NULL(di)) {
3873 if (!di)
3874 ret = -ENOENT;
3875 else
3876 ret = PTR_ERR(di);
3877 btrfs_abort_transaction(trans, root, ret);
3878 goto out;
3879 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003880
3881 leaf = path->nodes[0];
3882 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003883 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003884 index = key.offset;
3885 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003886 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003887
Miao Xie16cdcec2011-04-22 18:12:22 +08003888 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003889 if (ret) {
3890 btrfs_abort_transaction(trans, root, ret);
3891 goto out;
3892 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003893
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003894 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003895 inode_inc_iversion(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003896 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Josef Bacik5a24e842012-08-08 10:12:59 -06003897 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003898 if (ret)
3899 btrfs_abort_transaction(trans, root, ret);
3900out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04003901 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003902 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003903}
3904
Chris Mason39279cc2007-06-12 06:35:45 -04003905static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3906{
3907 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003908 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003909 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003910 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04003911
David Sterbab3ae2442012-09-13 16:04:34 -06003912 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04003913 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06003914 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
3915 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04003916
Josef Bacikd52be812013-05-29 14:54:47 -04003917 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003918 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003919 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003920
Li Zefan33345d012011-04-20 10:31:50 +08003921 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003922 err = btrfs_unlink_subvol(trans, root, dir,
3923 BTRFS_I(inode)->location.objectid,
3924 dentry->d_name.name,
3925 dentry->d_name.len);
3926 goto out;
3927 }
3928
Josef Bacik7b128762008-07-24 12:17:14 -04003929 err = btrfs_orphan_add(trans, inode);
3930 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003931 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003932
Chris Mason39279cc2007-06-12 06:35:45 -04003933 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04003934 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3935 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05003936 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04003937 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003938out:
Josef Bacikd52be812013-05-29 14:54:47 -04003939 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003940 btrfs_btree_balance_dirty(root);
Chris Mason39544012007-12-12 14:38:19 -05003941
Chris Mason39279cc2007-06-12 06:35:45 -04003942 return err;
3943}
3944
Chris Mason323ac952008-10-01 19:05:46 -04003945/*
Chris Mason39279cc2007-06-12 06:35:45 -04003946 * this can truncate away extent items, csum items and directory items.
3947 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04003948 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04003949 *
3950 * csum items that cross the new i_size are truncated to the new size
3951 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04003952 *
3953 * min_type is the minimum key type to truncate down to. If set to 0, this
3954 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04003955 */
Yan, Zheng80825102009-11-12 09:35:36 +00003956int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
3957 struct btrfs_root *root,
3958 struct inode *inode,
3959 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003960{
Chris Mason39279cc2007-06-12 06:35:45 -04003961 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003962 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003963 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00003964 struct btrfs_key key;
3965 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04003966 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04003967 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003968 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003969 u64 item_end = 0;
Josef Bacik7f4f6e02013-08-29 16:43:28 -04003970 u64 last_size = (u64)-1;
Yan, Zheng80825102009-11-12 09:35:36 +00003971 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04003972 int found_extent;
3973 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003974 int pending_del_nr = 0;
3975 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04003976 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00003977 int ret;
3978 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08003979 u64 ino = btrfs_ino(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003980
3981 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04003982
Mark Fasheh0eb0e192011-07-12 16:44:10 -07003983 path = btrfs_alloc_path();
3984 if (!path)
3985 return -ENOMEM;
3986 path->reada = -1;
3987
Josef Bacik5dc562c2012-08-17 13:14:17 -04003988 /*
3989 * We want to drop from the next block forward in case this new size is
3990 * not block aligned since we will be keeping the last block of the
3991 * extent just the way it is.
3992 */
Josef Bacik0af3d002010-06-21 14:48:16 -04003993 if (root->ref_cows || root == root->fs_info->tree_root)
Qu Wenruofda28322013-02-26 08:10:22 +00003994 btrfs_drop_extent_cache(inode, ALIGN(new_size,
3995 root->sectorsize), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003996
Miao Xie16cdcec2011-04-22 18:12:22 +08003997 /*
3998 * This function is also used to drop the items in the log tree before
3999 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4000 * it is used to drop the loged items. So we shouldn't kill the delayed
4001 * items.
4002 */
4003 if (min_type == 0 && root == BTRFS_I(inode)->root)
4004 btrfs_kill_delayed_inode_items(inode);
4005
Li Zefan33345d012011-04-20 10:31:50 +08004006 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004007 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004008 key.type = (u8)-1;
4009
Chris Mason85e21ba2008-01-29 15:11:36 -05004010search_again:
Chris Masonb9473432009-03-13 11:00:37 -04004011 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004012 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004013 if (ret < 0) {
4014 err = ret;
4015 goto out;
4016 }
Chris Masond3977122009-01-05 21:25:51 -05004017
Chris Mason85e21ba2008-01-29 15:11:36 -05004018 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004019 /* there are no items in the tree for us to truncate, we're
4020 * done
4021 */
Yan, Zheng80825102009-11-12 09:35:36 +00004022 if (path->slots[0] == 0)
4023 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004024 path->slots[0]--;
4025 }
4026
Chris Masond3977122009-01-05 21:25:51 -05004027 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004028 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004029 leaf = path->nodes[0];
4030 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4031 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04004032
Li Zefan33345d012011-04-20 10:31:50 +08004033 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004034 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004035
Chris Mason85e21ba2008-01-29 15:11:36 -05004036 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004037 break;
4038
Chris Mason5f39d392007-10-15 16:14:19 -04004039 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004040 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004041 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004042 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004043 extent_type = btrfs_file_extent_type(leaf, fi);
4044 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004045 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004046 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04004047 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004048 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004049 path->slots[0], fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004050 }
Yan008630c2007-11-07 13:31:09 -05004051 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004052 }
Yan, Zheng80825102009-11-12 09:35:36 +00004053 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004054 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004055 } else {
4056 if (item_end < new_size)
4057 break;
4058 if (found_key.offset >= new_size)
4059 del_item = 1;
4060 else
4061 del_item = 0;
4062 }
Chris Mason39279cc2007-06-12 06:35:45 -04004063 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004064 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004065 if (found_type != BTRFS_EXTENT_DATA_KEY)
4066 goto delete;
4067
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004068 if (del_item)
4069 last_size = found_key.offset;
4070 else
4071 last_size = new_size;
4072
Chris Mason179e29e2007-11-01 11:28:41 -04004073 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004074 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004075 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004076 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004077 u64 orig_num_bytes =
4078 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004079 extent_num_bytes = ALIGN(new_size -
4080 found_key.offset,
4081 root->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004082 btrfs_set_file_extent_num_bytes(leaf, fi,
4083 extent_num_bytes);
4084 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004085 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04004086 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004087 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004088 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004089 } else {
Chris Masondb945352007-10-15 16:15:53 -04004090 extent_num_bytes =
4091 btrfs_file_extent_disk_num_bytes(leaf,
4092 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004093 extent_offset = found_key.offset -
4094 btrfs_file_extent_offset(leaf, fi);
4095
Chris Mason39279cc2007-06-12 06:35:45 -04004096 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004097 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004098 if (extent_start != 0) {
4099 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04004100 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004101 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004102 }
4103 }
Chris Mason90692182008-02-08 13:49:28 -05004104 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004105 /*
4106 * we can't truncate inline items that have had
4107 * special encodings
4108 */
4109 if (!del_item &&
4110 btrfs_file_extent_compression(leaf, fi) == 0 &&
4111 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4112 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004113 u32 size = new_size - found_key.offset;
4114
4115 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004116 inode_sub_bytes(inode, item_end + 1 -
4117 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04004118 }
Chris Mason514ac8a2014-01-03 21:07:00 -08004119
4120 /*
4121 * update the ram bytes to properly reflect
4122 * the new size of our item
4123 */
4124 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
Chris Masone02119d2008-09-05 16:13:11 -04004125 size =
4126 btrfs_file_extent_calc_inline_size(size);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004127 btrfs_truncate_item(root, path, size, 1);
Chris Masone02119d2008-09-05 16:13:11 -04004128 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004129 inode_sub_bytes(inode, item_end + 1 -
4130 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05004131 }
Chris Mason39279cc2007-06-12 06:35:45 -04004132 }
Chris Mason179e29e2007-11-01 11:28:41 -04004133delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004134 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004135 if (!pending_del_nr) {
4136 /* no pending yet, add ourselves */
4137 pending_del_slot = path->slots[0];
4138 pending_del_nr = 1;
4139 } else if (pending_del_nr &&
4140 path->slots[0] + 1 == pending_del_slot) {
4141 /* hop on the pending chunk */
4142 pending_del_nr++;
4143 pending_del_slot = path->slots[0];
4144 } else {
Chris Masond3977122009-01-05 21:25:51 -05004145 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004146 }
Chris Mason39279cc2007-06-12 06:35:45 -04004147 } else {
4148 break;
4149 }
Josef Bacik0af3d002010-06-21 14:48:16 -04004150 if (found_extent && (root->ref_cows ||
4151 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004152 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004153 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004154 extent_num_bytes, 0,
4155 btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02004156 ino, extent_offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004157 BUG_ON(ret);
4158 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004159
Yan, Zheng80825102009-11-12 09:35:36 +00004160 if (found_type == BTRFS_INODE_ITEM_KEY)
4161 break;
4162
4163 if (path->slots[0] == 0 ||
4164 path->slots[0] != pending_del_slot) {
Yan, Zheng80825102009-11-12 09:35:36 +00004165 if (pending_del_nr) {
4166 ret = btrfs_del_items(trans, root, path,
4167 pending_del_slot,
4168 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004169 if (ret) {
4170 btrfs_abort_transaction(trans,
4171 root, ret);
4172 goto error;
4173 }
Yan, Zheng80825102009-11-12 09:35:36 +00004174 pending_del_nr = 0;
4175 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004176 btrfs_release_path(path);
Chris Mason85e21ba2008-01-29 15:11:36 -05004177 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004178 } else {
4179 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004180 }
Chris Mason39279cc2007-06-12 06:35:45 -04004181 }
Yan, Zheng80825102009-11-12 09:35:36 +00004182out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004183 if (pending_del_nr) {
4184 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4185 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004186 if (ret)
4187 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004188 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004189error:
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004190 if (last_size != (u64)-1)
4191 btrfs_ordered_update_i_size(inode, last_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004192 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00004193 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004194}
4195
Chris Masona52d9a82007-08-27 16:49:44 -04004196/*
Josef Bacik2aaa6652012-08-29 14:27:18 -04004197 * btrfs_truncate_page - read, zero a chunk and write a page
4198 * @inode - inode that we're zeroing
4199 * @from - the offset to start zeroing
4200 * @len - the length to zero, 0 to zero the entire range respective to the
4201 * offset
4202 * @front - zero up to the offset instead of from the offset on
4203 *
4204 * This will find the page for the "from" offset and cow the page and zero the
4205 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004206 */
Josef Bacik2aaa6652012-08-29 14:27:18 -04004207int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
4208 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004209{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004210 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04004211 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004212 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4213 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004214 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004215 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04004216 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04004217 pgoff_t index = from >> PAGE_CACHE_SHIFT;
4218 unsigned offset = from & (PAGE_CACHE_SIZE-1);
4219 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004220 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004221 int ret = 0;
4222 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004223 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004224
Josef Bacik2aaa6652012-08-29 14:27:18 -04004225 if ((offset & (blocksize - 1)) == 0 &&
4226 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004227 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004228 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004229 if (ret)
4230 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004231
Chris Mason211c17f2008-05-15 09:13:45 -04004232again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004233 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004234 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004235 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Miao Xieac6a2b32012-12-05 10:56:13 +00004236 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004237 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004238 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004239
4240 page_start = page_offset(page);
4241 page_end = page_start + PAGE_CACHE_SIZE - 1;
4242
Chris Masona52d9a82007-08-27 16:49:44 -04004243 if (!PageUptodate(page)) {
4244 ret = btrfs_readpage(NULL, page);
4245 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004246 if (page->mapping != mapping) {
4247 unlock_page(page);
4248 page_cache_release(page);
4249 goto again;
4250 }
Chris Masona52d9a82007-08-27 16:49:44 -04004251 if (!PageUptodate(page)) {
4252 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004253 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004254 }
4255 }
Chris Mason211c17f2008-05-15 09:13:45 -04004256 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004257
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004258 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004259 set_page_extent_mapped(page);
4260
4261 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4262 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004263 unlock_extent_cached(io_tree, page_start, page_end,
4264 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004265 unlock_page(page);
4266 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004267 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004268 btrfs_put_ordered_extent(ordered);
4269 goto again;
4270 }
4271
Josef Bacik2ac55d42010-02-03 19:33:23 +00004272 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004273 EXTENT_DIRTY | EXTENT_DELALLOC |
4274 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004275 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004276
Josef Bacik2ac55d42010-02-03 19:33:23 +00004277 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
4278 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004279 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004280 unlock_extent_cached(io_tree, page_start, page_end,
4281 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004282 goto out_unlock;
4283 }
4284
Chris Masone6dcd2d2008-07-17 12:53:50 -04004285 if (offset != PAGE_CACHE_SIZE) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004286 if (!len)
4287 len = PAGE_CACHE_SIZE - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004288 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004289 if (front)
4290 memset(kaddr, 0, offset);
4291 else
4292 memset(kaddr + offset, 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004293 flush_dcache_page(page);
4294 kunmap(page);
4295 }
Chris Mason247e7432008-07-17 12:53:51 -04004296 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004297 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004298 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
4299 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004300
Chris Mason89642222008-07-24 09:41:53 -04004301out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004302 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004303 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04004304 unlock_page(page);
4305 page_cache_release(page);
4306out:
4307 return ret;
4308}
4309
Josef Bacik16e75492013-10-22 12:18:51 -04004310static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4311 u64 offset, u64 len)
4312{
4313 struct btrfs_trans_handle *trans;
4314 int ret;
4315
4316 /*
4317 * Still need to make sure the inode looks like it's been updated so
4318 * that any holes get logged if we fsync.
4319 */
4320 if (btrfs_fs_incompat(root->fs_info, NO_HOLES)) {
4321 BTRFS_I(inode)->last_trans = root->fs_info->generation;
4322 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4323 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4324 return 0;
4325 }
4326
4327 /*
4328 * 1 - for the one we're dropping
4329 * 1 - for the one we're adding
4330 * 1 - for updating the inode.
4331 */
4332 trans = btrfs_start_transaction(root, 3);
4333 if (IS_ERR(trans))
4334 return PTR_ERR(trans);
4335
4336 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4337 if (ret) {
4338 btrfs_abort_transaction(trans, root, ret);
4339 btrfs_end_transaction(trans, root);
4340 return ret;
4341 }
4342
4343 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset,
4344 0, 0, len, 0, len, 0, 0, 0);
4345 if (ret)
4346 btrfs_abort_transaction(trans, root, ret);
4347 else
4348 btrfs_update_inode(trans, root, inode);
4349 btrfs_end_transaction(trans, root);
4350 return ret;
4351}
4352
Josef Bacik695a0d02011-03-04 15:46:53 -05004353/*
4354 * This function puts in dummy file extents for the area we're creating a hole
4355 * for. So if we are truncating this file to a larger size we need to insert
4356 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4357 * the range between oldsize and size
4358 */
Josef Bacika41ad392011-01-31 15:30:16 -05004359int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004360{
Yan Zheng9036c102008-10-30 14:19:41 -04004361 struct btrfs_root *root = BTRFS_I(inode)->root;
4362 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004363 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004364 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004365 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Qu Wenruofda28322013-02-26 08:10:22 +00004366 u64 hole_start = ALIGN(oldsize, root->sectorsize);
4367 u64 block_end = ALIGN(size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004368 u64 last_byte;
4369 u64 cur_offset;
4370 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004371 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004372
Josef Bacika71754f2013-06-17 17:14:39 -04004373 /*
4374 * If our size started in the middle of a page we need to zero out the
4375 * rest of the page before we expand the i_size, otherwise we could
4376 * expose stale data.
4377 */
4378 err = btrfs_truncate_page(inode, oldsize, 0, 0);
4379 if (err)
4380 return err;
4381
Yan Zheng9036c102008-10-30 14:19:41 -04004382 if (size <= hole_start)
4383 return 0;
4384
Yan Zheng9036c102008-10-30 14:19:41 -04004385 while (1) {
4386 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004387
Josef Bacik2ac55d42010-02-03 19:33:23 +00004388 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004389 &cached_state);
Miao Xiefa7c1492013-09-26 13:15:27 +08004390 ordered = btrfs_lookup_ordered_range(inode, hole_start,
4391 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004392 if (!ordered)
4393 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004394 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4395 &cached_state, GFP_NOFS);
Miao Xiefa7c1492013-09-26 13:15:27 +08004396 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004397 btrfs_put_ordered_extent(ordered);
4398 }
4399
Yan Zheng9036c102008-10-30 14:19:41 -04004400 cur_offset = hole_start;
4401 while (1) {
4402 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4403 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004404 if (IS_ERR(em)) {
4405 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004406 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004407 break;
4408 }
Yan Zheng9036c102008-10-30 14:19:41 -04004409 last_byte = min(extent_map_end(em), block_end);
Qu Wenruofda28322013-02-26 08:10:22 +00004410 last_byte = ALIGN(last_byte , root->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004411 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004412 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004413 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004414
Josef Bacik16e75492013-10-22 12:18:51 -04004415 err = maybe_insert_hole(root, inode, cur_offset,
4416 hole_size);
4417 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004418 break;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004419 btrfs_drop_extent_cache(inode, cur_offset,
4420 cur_offset + hole_size - 1, 0);
4421 hole_em = alloc_extent_map();
4422 if (!hole_em) {
4423 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4424 &BTRFS_I(inode)->runtime_flags);
4425 goto next;
4426 }
4427 hole_em->start = cur_offset;
4428 hole_em->len = hole_size;
4429 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004430
Josef Bacik5dc562c2012-08-17 13:14:17 -04004431 hole_em->block_start = EXTENT_MAP_HOLE;
4432 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004433 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004434 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004435 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
4436 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Josef Bacik16e75492013-10-22 12:18:51 -04004437 hole_em->generation = root->fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004438
4439 while (1) {
4440 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004441 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004442 write_unlock(&em_tree->lock);
4443 if (err != -EEXIST)
4444 break;
4445 btrfs_drop_extent_cache(inode, cur_offset,
4446 cur_offset +
4447 hole_size - 1, 0);
4448 }
4449 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04004450 }
Josef Bacik16e75492013-10-22 12:18:51 -04004451next:
Yan Zheng9036c102008-10-30 14:19:41 -04004452 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004453 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004454 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004455 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004456 break;
4457 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004458 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004459 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4460 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004461 return err;
4462}
4463
Eric Sandeen3972f262013-01-12 02:57:22 +00004464static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004465{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004466 struct btrfs_root *root = BTRFS_I(inode)->root;
4467 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004468 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004469 loff_t newsize = attr->ia_size;
4470 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004471 int ret;
4472
Eric Sandeen3972f262013-01-12 02:57:22 +00004473 /*
4474 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4475 * special case where we need to update the times despite not having
4476 * these flags set. For all other operations the VFS set these flags
4477 * explicitly if it wants a timestamp update.
4478 */
4479 if (newsize != oldsize && (!(mask & (ATTR_CTIME | ATTR_MTIME))))
4480 inode->i_ctime = inode->i_mtime = current_fs_time(inode->i_sb);
4481
Josef Bacika41ad392011-01-31 15:30:16 -05004482 if (newsize > oldsize) {
Kirill A. Shutemov7caef262013-09-12 15:13:56 -07004483 truncate_pagecache(inode, newsize);
Josef Bacika41ad392011-01-31 15:30:16 -05004484 ret = btrfs_cont_expand(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004485 if (ret)
Yan, Zheng80825102009-11-12 09:35:36 +00004486 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004487
Miao Xief4a2f4c2011-12-14 20:12:01 -05004488 trans = btrfs_start_transaction(root, 1);
4489 if (IS_ERR(trans))
4490 return PTR_ERR(trans);
4491
4492 i_size_write(inode, newsize);
4493 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
4494 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004495 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05004496 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004497
Josef Bacika41ad392011-01-31 15:30:16 -05004498 /*
4499 * We're truncating a file that used to have good data down to
4500 * zero. Make sure it gets into the ordered flush list so that
4501 * any new writes get down to disk quickly.
4502 */
4503 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004504 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4505 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004506
Josef Bacikf3fe8202013-01-07 17:03:21 -05004507 /*
4508 * 1 for the orphan item we're going to add
4509 * 1 for the orphan item deletion.
4510 */
4511 trans = btrfs_start_transaction(root, 2);
4512 if (IS_ERR(trans))
4513 return PTR_ERR(trans);
4514
4515 /*
4516 * We need to do this in case we fail at _any_ point during the
4517 * actual truncate. Once we do the truncate_setsize we could
4518 * invalidate pages which forces any outstanding ordered io to
4519 * be instantly completed which will give us extents that need
4520 * to be truncated. If we fail to get an orphan inode down we
4521 * could have left over extents that were never meant to live,
4522 * so we need to garuntee from this point on that everything
4523 * will be consistent.
4524 */
4525 ret = btrfs_orphan_add(trans, inode);
4526 btrfs_end_transaction(trans, root);
4527 if (ret)
4528 return ret;
4529
Josef Bacika41ad392011-01-31 15:30:16 -05004530 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4531 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004532
4533 /* Disable nonlocked read DIO to avoid the end less truncate */
4534 btrfs_inode_block_unlocked_dio(inode);
4535 inode_dio_wait(inode);
4536 btrfs_inode_resume_unlocked_dio(inode);
4537
Josef Bacika41ad392011-01-31 15:30:16 -05004538 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004539 if (ret && inode->i_nlink) {
4540 int err;
4541
4542 /*
4543 * failed to truncate, disk_i_size is only adjusted down
4544 * as we remove extents, so it should represent the true
4545 * size of the inode, so reset the in memory size and
4546 * delete our orphan entry.
4547 */
4548 trans = btrfs_join_transaction(root);
4549 if (IS_ERR(trans)) {
4550 btrfs_orphan_del(NULL, inode);
4551 return ret;
4552 }
4553 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
4554 err = btrfs_orphan_del(trans, inode);
4555 if (err)
4556 btrfs_abort_transaction(trans, root, err);
4557 btrfs_end_transaction(trans, root);
4558 }
Yan, Zheng80825102009-11-12 09:35:36 +00004559 }
4560
Josef Bacika41ad392011-01-31 15:30:16 -05004561 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004562}
4563
Chris Mason39279cc2007-06-12 06:35:45 -04004564static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
4565{
4566 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08004567 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004568 int err;
4569
Li Zefanb83cc962010-12-20 16:04:08 +08004570 if (btrfs_root_readonly(root))
4571 return -EROFS;
4572
Chris Mason39279cc2007-06-12 06:35:45 -04004573 err = inode_change_ok(inode, attr);
4574 if (err)
4575 return err;
4576
Chris Mason5a3f23d2009-03-31 13:27:11 -04004577 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00004578 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00004579 if (err)
4580 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004581 }
Yan Zheng9036c102008-10-30 14:19:41 -04004582
Christoph Hellwig10257742010-06-04 11:30:02 +02004583 if (attr->ia_valid) {
4584 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004585 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004586 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04004587
Josef Bacik22c44fe2011-11-30 10:45:38 -05004588 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig10257742010-06-04 11:30:02 +02004589 err = btrfs_acl_chmod(inode);
4590 }
4591
Chris Mason39279cc2007-06-12 06:35:45 -04004592 return err;
4593}
Chris Mason61295eb2008-01-14 16:24:38 -05004594
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004595/*
4596 * While truncating the inode pages during eviction, we get the VFS calling
4597 * btrfs_invalidatepage() against each page of the inode. This is slow because
4598 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
4599 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
4600 * extent_state structures over and over, wasting lots of time.
4601 *
4602 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
4603 * those expensive operations on a per page basis and do only the ordered io
4604 * finishing, while we release here the extent_map and extent_state structures,
4605 * without the excessive merging and splitting.
4606 */
4607static void evict_inode_truncate_pages(struct inode *inode)
4608{
4609 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4610 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
4611 struct rb_node *node;
4612
4613 ASSERT(inode->i_state & I_FREEING);
4614 truncate_inode_pages(&inode->i_data, 0);
4615
4616 write_lock(&map_tree->lock);
4617 while (!RB_EMPTY_ROOT(&map_tree->map)) {
4618 struct extent_map *em;
4619
4620 node = rb_first(&map_tree->map);
4621 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08004622 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
4623 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004624 remove_extent_mapping(map_tree, em);
4625 free_extent_map(em);
4626 }
4627 write_unlock(&map_tree->lock);
4628
4629 spin_lock(&io_tree->lock);
4630 while (!RB_EMPTY_ROOT(&io_tree->state)) {
4631 struct extent_state *state;
4632 struct extent_state *cached_state = NULL;
4633
4634 node = rb_first(&io_tree->state);
4635 state = rb_entry(node, struct extent_state, rb_node);
4636 atomic_inc(&state->refs);
4637 spin_unlock(&io_tree->lock);
4638
4639 lock_extent_bits(io_tree, state->start, state->end,
4640 0, &cached_state);
4641 clear_extent_bit(io_tree, state->start, state->end,
4642 EXTENT_LOCKED | EXTENT_DIRTY |
4643 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
4644 EXTENT_DEFRAG, 1, 1,
4645 &cached_state, GFP_NOFS);
4646 free_extent_state(state);
4647
4648 spin_lock(&io_tree->lock);
4649 }
4650 spin_unlock(&io_tree->lock);
4651}
4652
Al Virobd555972010-06-07 11:35:40 -04004653void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004654{
4655 struct btrfs_trans_handle *trans;
4656 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04004657 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik07127182011-08-19 10:29:59 -04004658 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04004659 int ret;
4660
liubo1abe9b82011-03-24 11:18:59 +00004661 trace_btrfs_inode_evict(inode);
4662
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004663 evict_inode_truncate_pages(inode);
4664
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02004665 if (inode->i_nlink &&
4666 ((btrfs_root_refs(&root->root_item) != 0 &&
4667 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
4668 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04004669 goto no_delete;
4670
Chris Mason39279cc2007-06-12 06:35:45 -04004671 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04004672 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004673 goto no_delete;
4674 }
Al Virobd555972010-06-07 11:35:40 -04004675 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04004676 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04004677
Yan, Zhengc71bf092009-11-12 09:34:40 +00004678 if (root->fs_info->log_root_recovering) {
Liu Bo6bf02312012-06-25 21:59:09 -06004679 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04004680 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00004681 goto no_delete;
4682 }
4683
Yan, Zheng76dda932009-09-21 16:00:26 -04004684 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02004685 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
4686 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04004687 goto no_delete;
4688 }
4689
Miao Xie0e8c36a2012-12-19 06:59:51 +00004690 ret = btrfs_commit_inode_delayed_inode(inode);
4691 if (ret) {
4692 btrfs_orphan_del(NULL, inode);
4693 goto no_delete;
4694 }
4695
Miao Xie66d8f3d2012-09-06 04:02:28 -06004696 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04004697 if (!rsv) {
4698 btrfs_orphan_del(NULL, inode);
4699 goto no_delete;
4700 }
Josef Bacik4a338542011-08-29 11:01:31 -04004701 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04004702 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04004703 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04004704
Chris Masondbe674a2008-07-17 12:54:05 -04004705 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04004706
Josef Bacik4289a662011-08-05 13:22:24 -04004707 /*
Miao Xie8407aa42012-09-07 01:43:32 -06004708 * This is a bit simpler than btrfs_truncate since we've already
4709 * reserved our space for our orphan item in the unlink, so we just
4710 * need to reserve some slack space in case we add bytes and update
4711 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04004712 */
Yan, Zheng80825102009-11-12 09:35:36 +00004713 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00004714 ret = btrfs_block_rsv_refill(root, rsv, min_size,
4715 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00004716
Josef Bacik726c35f2011-09-26 15:46:06 -04004717 /*
4718 * Try and steal from the global reserve since we will
4719 * likely not use this space anyway, we want to try as
4720 * hard as possible to get this to work.
4721 */
4722 if (ret)
4723 ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
4724
Yan, Zhengd68fc572010-05-16 10:49:58 -04004725 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004726 btrfs_warn(root->fs_info,
4727 "Could not get space for a delete, will truncate on mount %d",
4728 ret);
Josef Bacik4289a662011-08-05 13:22:24 -04004729 btrfs_orphan_del(NULL, inode);
4730 btrfs_free_block_rsv(root, rsv);
4731 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04004732 }
4733
Miao Xie0e8c36a2012-12-19 06:59:51 +00004734 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04004735 if (IS_ERR(trans)) {
4736 btrfs_orphan_del(NULL, inode);
4737 btrfs_free_block_rsv(root, rsv);
4738 goto no_delete;
4739 }
4740
4741 trans->block_rsv = rsv;
4742
Yan, Zhengd68fc572010-05-16 10:49:58 -04004743 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04004744 if (ret != -ENOSPC)
Yan, Zheng80825102009-11-12 09:35:36 +00004745 break;
4746
Miao Xie8407aa42012-09-07 01:43:32 -06004747 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004748 btrfs_end_transaction(trans, root);
4749 trans = NULL;
Liu Bob53d3f52012-11-14 14:34:34 +00004750 btrfs_btree_balance_dirty(root);
Josef Bacik7b128762008-07-24 12:17:14 -04004751 }
4752
Josef Bacik4289a662011-08-05 13:22:24 -04004753 btrfs_free_block_rsv(root, rsv);
4754
Josef Bacik4ef31a42013-08-13 14:10:08 -04004755 /*
4756 * Errors here aren't a big deal, it just means we leave orphan items
4757 * in the tree. They will be cleaned up on the next mount.
4758 */
Yan, Zheng80825102009-11-12 09:35:36 +00004759 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04004760 trans->block_rsv = root->orphan_block_rsv;
Josef Bacik4ef31a42013-08-13 14:10:08 -04004761 btrfs_orphan_del(trans, inode);
4762 } else {
4763 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004764 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004765
Josef Bacik4289a662011-08-05 13:22:24 -04004766 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08004767 if (!(root == root->fs_info->tree_root ||
4768 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08004769 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08004770
Chris Mason54aa1f42007-06-22 14:16:25 -04004771 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004772 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04004773no_delete:
Miao Xie89042e52013-05-15 07:48:15 +00004774 btrfs_remove_delayed_node(inode);
Jan Karadbd57682012-05-03 14:48:02 +02004775 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004776 return;
Chris Mason39279cc2007-06-12 06:35:45 -04004777}
4778
4779/*
4780 * this returns the key found in the dir entry in the location pointer.
4781 * If no dir entries were found, location->objectid is 0.
4782 */
4783static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
4784 struct btrfs_key *location)
4785{
4786 const char *name = dentry->d_name.name;
4787 int namelen = dentry->d_name.len;
4788 struct btrfs_dir_item *di;
4789 struct btrfs_path *path;
4790 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04004791 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004792
4793 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07004794 if (!path)
4795 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05004796
Li Zefan33345d012011-04-20 10:31:50 +08004797 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04004798 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04004799 if (IS_ERR(di))
4800 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05004801
David Sterbac7040052011-04-19 18:00:01 +02004802 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05004803 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05004804
Chris Mason5f39d392007-10-15 16:14:19 -04004805 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04004806out:
Chris Mason39279cc2007-06-12 06:35:45 -04004807 btrfs_free_path(path);
4808 return ret;
Chris Mason39544012007-12-12 14:38:19 -05004809out_err:
4810 location->objectid = 0;
4811 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04004812}
4813
4814/*
4815 * when we hit a tree root in a directory, the btrfs part of the inode
4816 * needs to be changed to reflect the root directory of the tree root. This
4817 * is kind of like crossing a mount point.
4818 */
4819static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004820 struct inode *dir,
4821 struct dentry *dentry,
4822 struct btrfs_key *location,
4823 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04004824{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004825 struct btrfs_path *path;
4826 struct btrfs_root *new_root;
4827 struct btrfs_root_ref *ref;
4828 struct extent_buffer *leaf;
4829 int ret;
4830 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004831
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004832 path = btrfs_alloc_path();
4833 if (!path) {
4834 err = -ENOMEM;
4835 goto out;
4836 }
Chris Mason39279cc2007-06-12 06:35:45 -04004837
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004838 err = -ENOENT;
Kelley Nielsen75ac2dd2013-11-04 19:35:58 -08004839 ret = btrfs_find_item(root->fs_info->tree_root, path,
4840 BTRFS_I(dir)->root->root_key.objectid,
4841 location->objectid, BTRFS_ROOT_REF_KEY, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004842 if (ret) {
4843 if (ret < 0)
4844 err = ret;
4845 goto out;
4846 }
Chris Mason39279cc2007-06-12 06:35:45 -04004847
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004848 leaf = path->nodes[0];
4849 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08004850 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004851 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
4852 goto out;
4853
4854 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
4855 (unsigned long)(ref + 1),
4856 dentry->d_name.len);
4857 if (ret)
4858 goto out;
4859
David Sterbab3b4aa72011-04-21 01:20:15 +02004860 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004861
4862 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
4863 if (IS_ERR(new_root)) {
4864 err = PTR_ERR(new_root);
4865 goto out;
4866 }
4867
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004868 *sub_root = new_root;
4869 location->objectid = btrfs_root_dirid(&new_root->root_item);
4870 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04004871 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004872 err = 0;
4873out:
4874 btrfs_free_path(path);
4875 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004876}
4877
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004878static void inode_tree_add(struct inode *inode)
4879{
4880 struct btrfs_root *root = BTRFS_I(inode)->root;
4881 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004882 struct rb_node **p;
4883 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01004884 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Li Zefan33345d012011-04-20 10:31:50 +08004885 u64 ino = btrfs_ino(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004886
Al Viro1d3382cb2010-10-23 15:19:20 -04004887 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004888 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00004889 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004890 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00004891 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004892 while (*p) {
4893 parent = *p;
4894 entry = rb_entry(parent, struct btrfs_inode, rb_node);
4895
Li Zefan33345d012011-04-20 10:31:50 +08004896 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004897 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004898 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004899 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004900 else {
4901 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04004902 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01004903 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004904 RB_CLEAR_NODE(parent);
4905 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01004906 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004907 }
4908 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01004909 rb_link_node(new, parent, p);
4910 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004911 spin_unlock(&root->inode_lock);
4912}
4913
4914static void inode_tree_del(struct inode *inode)
4915{
4916 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04004917 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004918
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004919 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004920 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004921 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004922 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04004923 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004924 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004925 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04004926
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02004927 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004928 synchronize_srcu(&root->fs_info->subvol_srcu);
4929 spin_lock(&root->inode_lock);
4930 empty = RB_EMPTY_ROOT(&root->inode_tree);
4931 spin_unlock(&root->inode_lock);
4932 if (empty)
4933 btrfs_add_dead_root(root);
4934 }
4935}
4936
Jeff Mahoney143bede2012-03-01 14:56:26 +01004937void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04004938{
4939 struct rb_node *node;
4940 struct rb_node *prev;
4941 struct btrfs_inode *entry;
4942 struct inode *inode;
4943 u64 objectid = 0;
4944
Liu Bo7813b3d2014-02-10 17:37:25 +08004945 if (!test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
4946 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04004947
4948 spin_lock(&root->inode_lock);
4949again:
4950 node = root->inode_tree.rb_node;
4951 prev = NULL;
4952 while (node) {
4953 prev = node;
4954 entry = rb_entry(node, struct btrfs_inode, rb_node);
4955
Li Zefan33345d012011-04-20 10:31:50 +08004956 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004957 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004958 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004959 node = node->rb_right;
4960 else
4961 break;
4962 }
4963 if (!node) {
4964 while (prev) {
4965 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004966 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004967 node = prev;
4968 break;
4969 }
4970 prev = rb_next(prev);
4971 }
4972 }
4973 while (node) {
4974 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004975 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04004976 inode = igrab(&entry->vfs_inode);
4977 if (inode) {
4978 spin_unlock(&root->inode_lock);
4979 if (atomic_read(&inode->i_count) > 1)
4980 d_prune_aliases(inode);
4981 /*
Al Viro45321ac2010-06-07 13:43:19 -04004982 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04004983 * the inode cache when its usage count
4984 * hits zero.
4985 */
4986 iput(inode);
4987 cond_resched();
4988 spin_lock(&root->inode_lock);
4989 goto again;
4990 }
4991
4992 if (cond_resched_lock(&root->inode_lock))
4993 goto again;
4994
4995 node = rb_next(node);
4996 }
4997 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004998}
4999
Chris Masone02119d2008-09-05 16:13:11 -04005000static int btrfs_init_locked_inode(struct inode *inode, void *p)
5001{
5002 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005003 inode->i_ino = args->location->objectid;
5004 memcpy(&BTRFS_I(inode)->location, args->location,
5005 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005006 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005007 return 0;
5008}
5009
5010static int btrfs_find_actor(struct inode *inode, void *opaque)
5011{
5012 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005013 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005014 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005015}
5016
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005017static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005018 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005019 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005020{
5021 struct inode *inode;
5022 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005023 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005024
Chris Mason90d3e592014-01-09 17:28:00 -08005025 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005026 args.root = root;
5027
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005028 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005029 btrfs_init_locked_inode,
5030 (void *)&args);
5031 return inode;
5032}
5033
Balaji Rao1a54ef82008-07-21 02:01:04 +05305034/* Get an inode object given its location and corresponding root.
5035 * Returns in *is_new if the inode was read from disk
5036 */
5037struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005038 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305039{
5040 struct inode *inode;
5041
Chris Mason90d3e592014-01-09 17:28:00 -08005042 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305043 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005044 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305045
5046 if (inode->i_state & I_NEW) {
Balaji Rao1a54ef82008-07-21 02:01:04 +05305047 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005048 if (!is_bad_inode(inode)) {
5049 inode_tree_add(inode);
5050 unlock_new_inode(inode);
5051 if (new)
5052 *new = 1;
5053 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005054 unlock_new_inode(inode);
5055 iput(inode);
5056 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005057 }
5058 }
5059
Balaji Rao1a54ef82008-07-21 02:01:04 +05305060 return inode;
5061}
5062
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005063static struct inode *new_simple_dir(struct super_block *s,
5064 struct btrfs_key *key,
5065 struct btrfs_root *root)
5066{
5067 struct inode *inode = new_inode(s);
5068
5069 if (!inode)
5070 return ERR_PTR(-ENOMEM);
5071
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005072 BTRFS_I(inode)->root = root;
5073 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005074 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005075
5076 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005077 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005078 inode->i_fop = &simple_dir_operations;
5079 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
5080 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
5081
5082 return inode;
5083}
5084
Chris Mason3de45862008-11-17 21:02:50 -05005085struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005086{
Chris Masond3977122009-01-05 21:25:51 -05005087 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005088 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005089 struct btrfs_root *sub_root = root;
5090 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005091 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005092 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005093
5094 if (dentry->d_name.len > BTRFS_NAME_LEN)
5095 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005096
Jeff Layton39e3c952012-11-28 11:30:53 -05005097 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005098 if (ret < 0)
5099 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005100
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005101 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005102 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005103
5104 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005105 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005106 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005107 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005108
5109 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5110
Yan, Zheng76dda932009-09-21 16:00:26 -04005111 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005112 ret = fixup_tree_root_location(root, dir, dentry,
5113 &location, &sub_root);
5114 if (ret < 0) {
5115 if (ret != -ENOENT)
5116 inode = ERR_PTR(ret);
5117 else
5118 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5119 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005120 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005121 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005122 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
5123
Julia Lawall34d19ba2011-01-24 19:55:19 +00005124 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00005125 down_read(&root->fs_info->cleanup_work_sem);
5126 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005127 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005128 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005129 if (ret) {
5130 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005131 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005132 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005133 }
5134
Chris Mason3de45862008-11-17 21:02:50 -05005135 return inode;
5136}
5137
Nick Pigginfe15ce42011-01-07 17:49:23 +11005138static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005139{
5140 struct btrfs_root *root;
Li Zefan848cce02012-02-21 17:04:28 +08005141 struct inode *inode = dentry->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005142
Li Zefan848cce02012-02-21 17:04:28 +08005143 if (!inode && !IS_ROOT(dentry))
5144 inode = dentry->d_parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005145
Li Zefan848cce02012-02-21 17:04:28 +08005146 if (inode) {
5147 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005148 if (btrfs_root_refs(&root->root_item) == 0)
5149 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005150
5151 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5152 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005153 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005154 return 0;
5155}
5156
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005157static void btrfs_dentry_release(struct dentry *dentry)
5158{
5159 if (dentry->d_fsdata)
5160 kfree(dentry->d_fsdata);
5161}
5162
Chris Mason3de45862008-11-17 21:02:50 -05005163static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005164 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005165{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005166 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005167
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005168 inode = btrfs_lookup_dentry(dir, dentry);
5169 if (IS_ERR(inode)) {
5170 if (PTR_ERR(inode) == -ENOENT)
5171 inode = NULL;
5172 else
5173 return ERR_CAST(inode);
5174 }
5175
Josef Bacik3a0dfa62014-02-14 13:43:48 -05005176 return d_materialise_unique(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005177}
5178
Miao Xie16cdcec2011-04-22 18:12:22 +08005179unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005180 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5181};
5182
Al Viro9cdda8d2013-05-22 16:48:09 -04005183static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005184{
Al Viro9cdda8d2013-05-22 16:48:09 -04005185 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005186 struct btrfs_root *root = BTRFS_I(inode)->root;
5187 struct btrfs_item *item;
5188 struct btrfs_dir_item *di;
5189 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005190 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005191 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005192 struct list_head ins_list;
5193 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005194 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005195 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005196 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005197 unsigned char d_type;
5198 int over = 0;
5199 u32 di_cur;
5200 u32 di_total;
5201 u32 di_len;
5202 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005203 char tmp_name[32];
5204 char *name_ptr;
5205 int name_len;
Al Viro9cdda8d2013-05-22 16:48:09 -04005206 int is_curr = 0; /* ctx->pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04005207
5208 /* FIXME, use a real flag for deciding about the key type */
5209 if (root->fs_info->tree_root == root)
5210 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005211
Al Viro9cdda8d2013-05-22 16:48:09 -04005212 if (!dir_emit_dots(file, ctx))
5213 return 0;
5214
David Woodhouse49593bf2008-08-17 17:08:36 +01005215 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005216 if (!path)
5217 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005218
Josef Bacik026fd312011-05-13 10:32:11 -04005219 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005220
Miao Xie16cdcec2011-04-22 18:12:22 +08005221 if (key_type == BTRFS_DIR_INDEX_KEY) {
5222 INIT_LIST_HEAD(&ins_list);
5223 INIT_LIST_HEAD(&del_list);
5224 btrfs_get_delayed_items(inode, &ins_list, &del_list);
5225 }
5226
Chris Mason39279cc2007-06-12 06:35:45 -04005227 btrfs_set_key_type(&key, key_type);
Al Viro9cdda8d2013-05-22 16:48:09 -04005228 key.offset = ctx->pos;
Li Zefan33345d012011-04-20 10:31:50 +08005229 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005230
Chris Mason39279cc2007-06-12 06:35:45 -04005231 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5232 if (ret < 0)
5233 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005234
5235 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005236 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005237 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005238 if (slot >= btrfs_header_nritems(leaf)) {
5239 ret = btrfs_next_leaf(root, path);
5240 if (ret < 0)
5241 goto err;
5242 else if (ret > 0)
5243 break;
5244 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005245 }
Chris Mason3de45862008-11-17 21:02:50 -05005246
Ross Kirkdd3cc162013-09-16 15:58:09 +01005247 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04005248 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5249
5250 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005251 break;
Chris Mason5f39d392007-10-15 16:14:19 -04005252 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04005253 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005254 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005255 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08005256 if (key_type == BTRFS_DIR_INDEX_KEY &&
5257 btrfs_should_delete_dir_index(&del_list,
5258 found_key.offset))
5259 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005260
Al Viro9cdda8d2013-05-22 16:48:09 -04005261 ctx->pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08005262 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005263
Chris Mason39279cc2007-06-12 06:35:45 -04005264 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
5265 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005266 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01005267
5268 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04005269 struct btrfs_key location;
5270
Josef Bacik22a94d42011-03-16 16:47:17 -04005271 if (verify_dir_item(root, leaf, di))
5272 break;
5273
Chris Mason5f39d392007-10-15 16:14:19 -04005274 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01005275 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04005276 name_ptr = tmp_name;
5277 } else {
5278 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01005279 if (!name_ptr) {
5280 ret = -ENOMEM;
5281 goto err;
5282 }
Chris Mason5f39d392007-10-15 16:14:19 -04005283 }
5284 read_extent_buffer(leaf, name_ptr,
5285 (unsigned long)(di + 1), name_len);
5286
5287 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5288 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05005289
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005290
Chris Mason3de45862008-11-17 21:02:50 -05005291 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01005292 * skip it.
5293 *
5294 * In contrast to old kernels, we insert the snapshot's
5295 * dir item and dir index after it has been created, so
5296 * we won't find a reference to our own snapshot. We
5297 * still keep the following code for backward
5298 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05005299 */
5300 if (location.type == BTRFS_ROOT_ITEM_KEY &&
5301 location.objectid == root->root_key.objectid) {
5302 over = 0;
5303 goto skip;
5304 }
Al Viro9cdda8d2013-05-22 16:48:09 -04005305 over = !dir_emit(ctx, name_ptr, name_len,
5306 location.objectid, d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04005307
Chris Mason3de45862008-11-17 21:02:50 -05005308skip:
Chris Mason5f39d392007-10-15 16:14:19 -04005309 if (name_ptr != tmp_name)
5310 kfree(name_ptr);
5311
Chris Mason39279cc2007-06-12 06:35:45 -04005312 if (over)
5313 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05005314 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01005315 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04005316 di_cur += di_len;
5317 di = (struct btrfs_dir_item *)((char *)di + di_len);
5318 }
Li Zefanb9e03af2011-03-23 10:43:58 +08005319next:
5320 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005321 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005322
Miao Xie16cdcec2011-04-22 18:12:22 +08005323 if (key_type == BTRFS_DIR_INDEX_KEY) {
5324 if (is_curr)
Al Viro9cdda8d2013-05-22 16:48:09 -04005325 ctx->pos++;
5326 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005327 if (ret)
5328 goto nopos;
5329 }
5330
David Woodhouse49593bf2008-08-17 17:08:36 +01005331 /* Reached end of directory/root. Bump pos past the last item. */
Zach Browndb62efb2013-07-11 16:19:42 -07005332 ctx->pos++;
5333
5334 /*
5335 * Stop new entries from being returned after we return the last
5336 * entry.
5337 *
5338 * New directory entries are assigned a strictly increasing
5339 * offset. This means that new entries created during readdir
5340 * are *guaranteed* to be seen in the future by that readdir.
5341 * This has broken buggy programs which operate on names as
5342 * they're returned by readdir. Until we re-use freed offsets
5343 * we have this hack to stop new entries from being returned
5344 * under the assumption that they'll never reach this huge
5345 * offset.
5346 *
5347 * This is being careful not to overflow 32bit loff_t unless the
5348 * last entry requires it because doing so has broken 32bit apps
5349 * in the past.
5350 */
5351 if (key_type == BTRFS_DIR_INDEX_KEY) {
5352 if (ctx->pos >= INT_MAX)
5353 ctx->pos = LLONG_MAX;
5354 else
5355 ctx->pos = INT_MAX;
5356 }
Chris Mason39279cc2007-06-12 06:35:45 -04005357nopos:
5358 ret = 0;
5359err:
Miao Xie16cdcec2011-04-22 18:12:22 +08005360 if (key_type == BTRFS_DIR_INDEX_KEY)
5361 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005362 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005363 return ret;
5364}
5365
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005366int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005367{
5368 struct btrfs_root *root = BTRFS_I(inode)->root;
5369 struct btrfs_trans_handle *trans;
5370 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005371 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005372
Josef Bacik72ac3c02012-05-23 14:13:11 -04005373 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005374 return 0;
5375
Liu Bo83eea1f2012-07-10 05:28:39 -06005376 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08005377 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005378
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005379 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005380 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005381 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005382 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005383 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005384 if (IS_ERR(trans))
5385 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06005386 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005387 }
5388 return ret;
5389}
5390
5391/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005392 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005393 * inode changes. But, it is most likely to find the inode in cache.
5394 * FIXME, needs more benchmarking...there are no reasons other than performance
5395 * to keep or drop this code.
5396 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005397static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005398{
5399 struct btrfs_root *root = BTRFS_I(inode)->root;
5400 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005401 int ret;
5402
Josef Bacik72ac3c02012-05-23 14:13:11 -04005403 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005404 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005405
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005406 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005407 if (IS_ERR(trans))
5408 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005409
5410 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005411 if (ret && ret == -ENOSPC) {
5412 /* whoops, lets try again with the full transaction */
5413 btrfs_end_transaction(trans, root);
5414 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005415 if (IS_ERR(trans))
5416 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005417
Chris Mason94b60442010-05-26 11:02:00 -04005418 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005419 }
Chris Mason39279cc2007-06-12 06:35:45 -04005420 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08005421 if (BTRFS_I(inode)->delayed_node)
5422 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005423
5424 return ret;
5425}
5426
5427/*
5428 * This is a copy of file_update_time. We need this so we can return error on
5429 * ENOSPC for updating the inode in the case of file write and mmap writes.
5430 */
Josef Bacike41f9412012-03-26 09:46:47 -04005431static int btrfs_update_time(struct inode *inode, struct timespec *now,
5432 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005433{
Alexander Block2bc5565282012-06-15 09:49:33 +02005434 struct btrfs_root *root = BTRFS_I(inode)->root;
5435
5436 if (btrfs_root_readonly(root))
5437 return -EROFS;
5438
Josef Bacike41f9412012-03-26 09:46:47 -04005439 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005440 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04005441 if (flags & S_CTIME)
5442 inode->i_ctime = *now;
5443 if (flags & S_MTIME)
5444 inode->i_mtime = *now;
5445 if (flags & S_ATIME)
5446 inode->i_atime = *now;
5447 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005448}
5449
Chris Masond352ac62008-09-29 15:18:18 -04005450/*
5451 * find the highest existing sequence number in a directory
5452 * and then set the in-memory index_cnt variable to reflect
5453 * free sequence numbers
5454 */
Josef Bacikaec74772008-07-24 12:12:38 -04005455static int btrfs_set_inode_index_count(struct inode *inode)
5456{
5457 struct btrfs_root *root = BTRFS_I(inode)->root;
5458 struct btrfs_key key, found_key;
5459 struct btrfs_path *path;
5460 struct extent_buffer *leaf;
5461 int ret;
5462
Li Zefan33345d012011-04-20 10:31:50 +08005463 key.objectid = btrfs_ino(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005464 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
5465 key.offset = (u64)-1;
5466
5467 path = btrfs_alloc_path();
5468 if (!path)
5469 return -ENOMEM;
5470
5471 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5472 if (ret < 0)
5473 goto out;
5474 /* FIXME: we should be able to handle this */
5475 if (ret == 0)
5476 goto out;
5477 ret = 0;
5478
5479 /*
5480 * MAGIC NUMBER EXPLANATION:
5481 * since we search a directory based on f_pos we have to start at 2
5482 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5483 * else has to start at 2
5484 */
5485 if (path->slots[0] == 0) {
5486 BTRFS_I(inode)->index_cnt = 2;
5487 goto out;
5488 }
5489
5490 path->slots[0]--;
5491
5492 leaf = path->nodes[0];
5493 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5494
Li Zefan33345d012011-04-20 10:31:50 +08005495 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacikaec74772008-07-24 12:12:38 -04005496 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
5497 BTRFS_I(inode)->index_cnt = 2;
5498 goto out;
5499 }
5500
5501 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
5502out:
5503 btrfs_free_path(path);
5504 return ret;
5505}
5506
Chris Masond352ac62008-09-29 15:18:18 -04005507/*
5508 * helper to find a free sequence number in a given directory. This current
5509 * code is very simple, later versions will do smarter things in the btree
5510 */
Chris Mason3de45862008-11-17 21:02:50 -05005511int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04005512{
5513 int ret = 0;
5514
5515 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08005516 ret = btrfs_inode_delayed_dir_index_count(dir);
5517 if (ret) {
5518 ret = btrfs_set_inode_index_count(dir);
5519 if (ret)
5520 return ret;
5521 }
Josef Bacikaec74772008-07-24 12:12:38 -04005522 }
5523
Chris Mason00e4e6b2008-08-05 11:18:09 -04005524 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04005525 BTRFS_I(dir)->index_cnt++;
5526
5527 return ret;
5528}
5529
Chris Mason39279cc2007-06-12 06:35:45 -04005530static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5531 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04005532 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05005533 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04005534 u64 ref_objectid, u64 objectid,
5535 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04005536{
5537 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005538 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04005539 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04005540 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05005541 struct btrfs_inode_ref *ref;
5542 struct btrfs_key key[2];
5543 u32 sizes[2];
5544 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04005545 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005546
Chris Mason5f39d392007-10-15 16:14:19 -04005547 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005548 if (!path)
5549 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04005550
Chris Mason39279cc2007-06-12 06:35:45 -04005551 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005552 if (!inode) {
5553 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005554 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005555 }
Chris Mason39279cc2007-06-12 06:35:45 -04005556
Li Zefan581bb052011-04-20 10:06:11 +08005557 /*
5558 * we have to initialize this early, so we can reclaim the inode
5559 * number if we fail afterwards in this function.
5560 */
5561 inode->i_ino = objectid;
5562
Josef Bacikaec74772008-07-24 12:12:38 -04005563 if (dir) {
liubo1abe9b82011-03-24 11:18:59 +00005564 trace_btrfs_inode_request(dir);
5565
Chris Mason3de45862008-11-17 21:02:50 -05005566 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04005567 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005568 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005569 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005570 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04005571 }
Josef Bacikaec74772008-07-24 12:12:38 -04005572 }
5573 /*
5574 * index_cnt is ignored for everything but a dir,
5575 * btrfs_get_inode_index_count has an explanation for the magic
5576 * number
5577 */
5578 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08005579 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04005580 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04005581 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00005582 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db2b2007-08-27 16:49:44 -04005583
Josef Bacik5dc562c2012-08-17 13:14:17 -04005584 /*
5585 * We could have gotten an inode number from somebody who was fsynced
5586 * and then removed in this same transaction, so let's just set full
5587 * sync since it will be a full sync anyway and this will blow away the
5588 * old info in the log.
5589 */
5590 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
5591
Chris Mason9c583092008-01-29 15:15:18 -05005592 key[0].objectid = objectid;
5593 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
5594 key[0].offset = 0;
5595
Mark Fashehf1863732012-08-08 11:32:27 -07005596 /*
5597 * Start new inodes with an inode_ref. This is slightly more
5598 * efficient for small numbers of hard links since they will
5599 * be packed into one item. Extended refs will kick in if we
5600 * add more hard links than can fit in the ref item.
5601 */
Chris Mason9c583092008-01-29 15:15:18 -05005602 key[1].objectid = objectid;
5603 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
5604 key[1].offset = ref_objectid;
5605
5606 sizes[0] = sizeof(struct btrfs_inode_item);
5607 sizes[1] = name_len + sizeof(*ref);
5608
Chris Masonb9473432009-03-13 11:00:37 -04005609 path->leave_spinning = 1;
Chris Mason9c583092008-01-29 15:15:18 -05005610 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
5611 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04005612 goto fail;
5613
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03005614 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04005615 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005616 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04005617 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
5618 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06005619 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
5620 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04005621 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05005622
5623 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
5624 struct btrfs_inode_ref);
5625 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04005626 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05005627 ptr = (unsigned long)(ref + 1);
5628 write_extent_buffer(path->nodes[0], name, ptr, name_len);
5629
Chris Mason5f39d392007-10-15 16:14:19 -04005630 btrfs_mark_buffer_dirty(path->nodes[0]);
5631 btrfs_free_path(path);
5632
Chris Mason39279cc2007-06-12 06:35:45 -04005633 location = &BTRFS_I(inode)->location;
5634 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04005635 location->offset = 0;
5636 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
5637
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005638 btrfs_inherit_iflags(inode, dir);
5639
Al Viro569254b2011-07-24 17:08:40 -04005640 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04005641 if (btrfs_test_opt(root, NODATASUM))
5642 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo213490b2012-09-11 08:33:50 -06005643 if (btrfs_test_opt(root, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05005644 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
5645 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04005646 }
5647
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005648 btrfs_insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005649 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00005650
5651 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04005652 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00005653
Alexander Block8ea05e32012-07-25 17:35:53 +02005654 btrfs_update_root_times(trans, root);
5655
Filipe David Borba Manana63541922014-01-07 11:47:46 +00005656 ret = btrfs_inode_inherit_props(trans, inode, dir);
5657 if (ret)
5658 btrfs_err(root->fs_info,
5659 "error inheriting props for ino %llu (root %llu): %d",
5660 btrfs_ino(inode), root->root_key.objectid, ret);
5661
Chris Mason39279cc2007-06-12 06:35:45 -04005662 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005663fail:
Josef Bacikaec74772008-07-24 12:12:38 -04005664 if (dir)
5665 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04005666 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005667 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005668 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005669}
5670
5671static inline u8 btrfs_inode_type(struct inode *inode)
5672{
5673 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
5674}
5675
Chris Masond352ac62008-09-29 15:18:18 -04005676/*
5677 * utility function to add 'inode' into 'parent_inode' with
5678 * a give name and a given sequence number.
5679 * if 'add_backref' is true, also insert a backref from the
5680 * inode to the parent directory.
5681 */
Chris Masone02119d2008-09-05 16:13:11 -04005682int btrfs_add_link(struct btrfs_trans_handle *trans,
5683 struct inode *parent_inode, struct inode *inode,
5684 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005685{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005686 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005687 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04005688 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08005689 u64 ino = btrfs_ino(inode);
5690 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005691
Li Zefan33345d012011-04-20 10:31:50 +08005692 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005693 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
5694 } else {
Li Zefan33345d012011-04-20 10:31:50 +08005695 key.objectid = ino;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005696 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
5697 key.offset = 0;
5698 }
Chris Mason39279cc2007-06-12 06:35:45 -04005699
Li Zefan33345d012011-04-20 10:31:50 +08005700 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005701 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
5702 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08005703 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005704 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08005705 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
5706 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005707 }
5708
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005709 /* Nothing to clean up yet */
5710 if (ret)
5711 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005712
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005713 ret = btrfs_insert_dir_item(trans, root, name, name_len,
5714 parent_inode, &key,
5715 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05005716 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005717 goto fail_dir_item;
5718 else if (ret) {
5719 btrfs_abort_transaction(trans, root, ret);
5720 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005721 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005722
5723 btrfs_i_size_write(parent_inode, parent_inode->i_size +
5724 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005725 inode_inc_iversion(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005726 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
5727 ret = btrfs_update_inode(trans, root, parent_inode);
5728 if (ret)
5729 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005730 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05005731
5732fail_dir_item:
5733 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
5734 u64 local_index;
5735 int err;
5736 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
5737 key.objectid, root->root_key.objectid,
5738 parent_ino, &local_index, name, name_len);
5739
5740 } else if (add_backref) {
5741 u64 local_index;
5742 int err;
5743
5744 err = btrfs_del_inode_ref(trans, root, name, name_len,
5745 ino, parent_ino, &local_index);
5746 }
5747 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005748}
5749
5750static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00005751 struct inode *dir, struct dentry *dentry,
5752 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005753{
Josef Bacika1b075d2010-11-19 20:36:11 +00005754 int err = btrfs_add_link(trans, dir, inode,
5755 dentry->d_name.name, dentry->d_name.len,
5756 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005757 if (err > 0)
5758 err = -EEXIST;
5759 return err;
5760}
5761
Josef Bacik618e21d2007-07-11 10:18:17 -04005762static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04005763 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04005764{
5765 struct btrfs_trans_handle *trans;
5766 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005767 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04005768 int err;
5769 int drop_inode = 0;
5770 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005771 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04005772
5773 if (!new_valid_dev(rdev))
5774 return -EINVAL;
5775
Josef Bacik9ed74f22009-09-11 16:12:44 -04005776 /*
5777 * 2 for inode item and ref
5778 * 2 for dir items
5779 * 1 for xattr if selinux is on
5780 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005781 trans = btrfs_start_transaction(root, 5);
5782 if (IS_ERR(trans))
5783 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05005784
Li Zefan581bb052011-04-20 10:06:11 +08005785 err = btrfs_find_free_ino(root, &objectid);
5786 if (err)
5787 goto out_unlock;
5788
Josef Bacikaec74772008-07-24 12:12:38 -04005789 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005790 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04005791 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005792 if (IS_ERR(inode)) {
5793 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005794 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005795 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005796
Eric Paris2a7dba32011-02-01 11:05:39 -05005797 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005798 if (err) {
5799 drop_inode = 1;
5800 goto out_unlock;
5801 }
5802
Casey Schauflerad19db72011-12-15 10:09:07 -05005803 /*
5804 * If the active LSM wants to access the inode during
5805 * d_instantiate it needs these. Smack checks to see
5806 * if the filesystem supports xattrs by looking at the
5807 * ops vector.
5808 */
5809
5810 inode->i_op = &btrfs_special_inode_operations;
Josef Bacika1b075d2010-11-19 20:36:11 +00005811 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04005812 if (err)
5813 drop_inode = 1;
5814 else {
Josef Bacik618e21d2007-07-11 10:18:17 -04005815 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04005816 btrfs_update_inode(trans, root, inode);
Al Viro08c422c2011-12-23 07:58:13 -05005817 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005818 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005819out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005820 btrfs_end_transaction(trans, root);
Josef Bacikc581afc2014-02-06 16:06:06 -05005821 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00005822 btrfs_btree_balance_dirty(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04005823 if (drop_inode) {
5824 inode_dec_link_count(inode);
5825 iput(inode);
5826 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005827 return err;
5828}
5829
Chris Mason39279cc2007-06-12 06:35:45 -04005830static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04005831 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04005832{
5833 struct btrfs_trans_handle *trans;
5834 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005835 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005836 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04005837 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04005838 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005839 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005840
Josef Bacik9ed74f22009-09-11 16:12:44 -04005841 /*
5842 * 2 for inode item and ref
5843 * 2 for dir items
5844 * 1 for xattr if selinux is on
5845 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005846 trans = btrfs_start_transaction(root, 5);
5847 if (IS_ERR(trans))
5848 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005849
Li Zefan581bb052011-04-20 10:06:11 +08005850 err = btrfs_find_free_ino(root, &objectid);
5851 if (err)
5852 goto out_unlock;
5853
Josef Bacikaec74772008-07-24 12:12:38 -04005854 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005855 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04005856 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005857 if (IS_ERR(inode)) {
5858 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005859 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005860 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005861 drop_inode_on_err = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04005862
Eric Paris2a7dba32011-02-01 11:05:39 -05005863 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005864 if (err)
Josef Bacik33268ea2008-07-24 12:16:36 -04005865 goto out_unlock;
Josef Bacik33268ea2008-07-24 12:16:36 -04005866
Filipe Brandenburger9185aa52012-11-30 03:40:08 +00005867 err = btrfs_update_inode(trans, root, inode);
5868 if (err)
5869 goto out_unlock;
5870
Casey Schauflerad19db72011-12-15 10:09:07 -05005871 /*
5872 * If the active LSM wants to access the inode during
5873 * d_instantiate it needs these. Smack checks to see
5874 * if the filesystem supports xattrs by looking at the
5875 * ops vector.
5876 */
5877 inode->i_fop = &btrfs_file_operations;
5878 inode->i_op = &btrfs_file_inode_operations;
5879
Josef Bacika1b075d2010-11-19 20:36:11 +00005880 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005881 if (err)
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005882 goto out_unlock;
5883
5884 inode->i_mapping->a_ops = &btrfs_aops;
5885 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
5886 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
5887 d_instantiate(dentry, inode);
5888
Chris Mason39279cc2007-06-12 06:35:45 -04005889out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005890 btrfs_end_transaction(trans, root);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005891 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005892 inode_dec_link_count(inode);
5893 iput(inode);
5894 }
Josef Bacikc581afc2014-02-06 16:06:06 -05005895 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00005896 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005897 return err;
5898}
5899
5900static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
5901 struct dentry *dentry)
5902{
5903 struct btrfs_trans_handle *trans;
5904 struct btrfs_root *root = BTRFS_I(dir)->root;
5905 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005906 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04005907 int err;
5908 int drop_inode = 0;
5909
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005910 /* do not allow sys_link's with other subvols of the same device */
5911 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00005912 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005913
Mark Fashehf1863732012-08-08 11:32:27 -07005914 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00005915 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04005916
Chris Mason3de45862008-11-17 21:02:50 -05005917 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04005918 if (err)
5919 goto fail;
5920
Yan, Zhenga22285a2010-05-16 10:48:46 -04005921 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00005922 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04005923 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00005924 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04005925 */
Miao Xie7e6b6462011-02-18 09:21:17 +00005926 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005927 if (IS_ERR(trans)) {
5928 err = PTR_ERR(trans);
5929 goto fail;
5930 }
Chris Mason5f39d392007-10-15 16:14:19 -04005931
Miao Xie67de1172013-12-26 13:07:06 +08005932 /* There are several dir indexes for this inode, clear the cache. */
5933 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07005934 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005935 inode_inc_iversion(inode);
Miao Xie31534952011-04-13 13:19:21 +08005936 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6ee2010-10-23 11:11:40 -04005937 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04005938 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04005939
Josef Bacika1b075d2010-11-19 20:36:11 +00005940 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04005941
Yan, Zhenga5719522009-09-24 09:17:31 -04005942 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005943 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04005944 } else {
Al Viro10d9f302011-07-16 23:09:10 -04005945 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04005946 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005947 if (err)
5948 goto fail;
Al Viro08c422c2011-12-23 07:58:13 -05005949 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00005950 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04005951 }
Chris Mason39279cc2007-06-12 06:35:45 -04005952
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005953 btrfs_end_transaction(trans, root);
Josef Bacikc581afc2014-02-06 16:06:06 -05005954 btrfs_balance_delayed_items(root);
Chris Mason1832a6d2007-12-21 16:27:21 -05005955fail:
Chris Mason39279cc2007-06-12 06:35:45 -04005956 if (drop_inode) {
5957 inode_dec_link_count(inode);
5958 iput(inode);
5959 }
Liu Bob53d3f52012-11-14 14:34:34 +00005960 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005961 return err;
5962}
5963
Al Viro18bb1db2011-07-26 01:41:39 -04005964static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04005965{
Chris Masonb9d86662008-05-02 16:13:49 -04005966 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04005967 struct btrfs_trans_handle *trans;
5968 struct btrfs_root *root = BTRFS_I(dir)->root;
5969 int err = 0;
5970 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04005971 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005972 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005973
Josef Bacik9ed74f22009-09-11 16:12:44 -04005974 /*
5975 * 2 items for inode and ref
5976 * 2 items for dir items
5977 * 1 for xattr if selinux is on
5978 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005979 trans = btrfs_start_transaction(root, 5);
5980 if (IS_ERR(trans))
5981 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04005982
Li Zefan581bb052011-04-20 10:06:11 +08005983 err = btrfs_find_free_ino(root, &objectid);
5984 if (err)
5985 goto out_fail;
5986
Josef Bacikaec74772008-07-24 12:12:38 -04005987 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005988 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04005989 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04005990 if (IS_ERR(inode)) {
5991 err = PTR_ERR(inode);
5992 goto out_fail;
5993 }
Chris Mason5f39d392007-10-15 16:14:19 -04005994
Chris Mason39279cc2007-06-12 06:35:45 -04005995 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04005996
Eric Paris2a7dba32011-02-01 11:05:39 -05005997 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005998 if (err)
5999 goto out_fail;
6000
Chris Mason39279cc2007-06-12 06:35:45 -04006001 inode->i_op = &btrfs_dir_inode_operations;
6002 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04006003
Chris Masondbe674a2008-07-17 12:54:05 -04006004 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006005 err = btrfs_update_inode(trans, root, inode);
6006 if (err)
6007 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006008
Josef Bacika1b075d2010-11-19 20:36:11 +00006009 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
6010 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006011 if (err)
6012 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006013
Chris Mason39279cc2007-06-12 06:35:45 -04006014 d_instantiate(dentry, inode);
6015 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006016
6017out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006018 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04006019 if (drop_on_err)
6020 iput(inode);
Josef Bacikc581afc2014-02-06 16:06:06 -05006021 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006022 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006023 return err;
6024}
6025
Chris Masond352ac62008-09-29 15:18:18 -04006026/* helper for btfs_get_extent. Given an existing extent in the tree,
6027 * and an extent that you want to insert, deal with overlap and insert
6028 * the new extent into the tree.
6029 */
Chris Mason3b951512008-04-17 11:29:12 -04006030static int merge_extent_mapping(struct extent_map_tree *em_tree,
6031 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006032 struct extent_map *em,
6033 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04006034{
6035 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04006036
Chris Masone6dcd2d2008-07-17 12:53:50 -04006037 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
6038 start_diff = map_start - em->start;
6039 em->start = map_start;
6040 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04006041 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
6042 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006043 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04006044 em->block_len -= start_diff;
6045 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006046 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006047}
6048
Chris Masonc8b97812008-10-29 14:49:59 -04006049static noinline int uncompress_inline(struct btrfs_path *path,
6050 struct inode *inode, struct page *page,
6051 size_t pg_offset, u64 extent_offset,
6052 struct btrfs_file_extent_item *item)
6053{
6054 int ret;
6055 struct extent_buffer *leaf = path->nodes[0];
6056 char *tmp;
6057 size_t max_size;
6058 unsigned long inline_size;
6059 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006060 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006061
6062 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006063 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006064 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6065 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006066 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006067 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006068 if (!tmp)
6069 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006070 ptr = btrfs_file_extent_inline_start(item);
6071
6072 read_extent_buffer(leaf, tmp, ptr, inline_size);
6073
Chris Mason5b050f02008-11-11 09:34:41 -05006074 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006075 ret = btrfs_decompress(compress_type, tmp, page,
6076 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006077 if (ret) {
Cong Wang7ac687d2011-11-25 23:14:28 +08006078 char *kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04006079 unsigned long copy_size = min_t(u64,
6080 PAGE_CACHE_SIZE - pg_offset,
6081 max_size - extent_offset);
6082 memset(kaddr + pg_offset, 0, copy_size);
Cong Wang7ac687d2011-11-25 23:14:28 +08006083 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -04006084 }
6085 kfree(tmp);
6086 return 0;
6087}
6088
Chris Masond352ac62008-09-29 15:18:18 -04006089/*
6090 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006091 * the ugly parts come from merging extents from the disk with the in-ram
6092 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006093 * where the in-ram extents might be locked pending data=ordered completion.
6094 *
6095 * This also copies inline extents directly into the page.
6096 */
Chris Masond3977122009-01-05 21:25:51 -05006097
Chris Masona52d9a82007-08-27 16:49:44 -04006098struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05006099 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04006100 int create)
6101{
6102 int ret;
6103 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04006104 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04006105 u64 extent_start = 0;
6106 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08006107 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006108 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006109 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04006110 struct btrfs_root *root = BTRFS_I(inode)->root;
6111 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006112 struct extent_buffer *leaf;
6113 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006114 struct extent_map *em = NULL;
6115 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05006116 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006117 struct btrfs_trans_handle *trans = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08006118 int compress_type;
Chris Masona52d9a82007-08-27 16:49:44 -04006119
Chris Masona52d9a82007-08-27 16:49:44 -04006120again:
Chris Mason890871b2009-09-02 16:24:52 -04006121 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006122 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006123 if (em)
6124 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006125 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006126
Chris Masona52d9a82007-08-27 16:49:44 -04006127 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006128 if (em->start > start || em->start + em->len <= start)
6129 free_extent_map(em);
6130 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006131 free_extent_map(em);
6132 else
6133 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006134 }
David Sterba172ddd62011-04-21 00:48:27 +02006135 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006136 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006137 err = -ENOMEM;
6138 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006139 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006140 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006141 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006142 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006143 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006144 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006145
6146 if (!path) {
6147 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006148 if (!path) {
6149 err = -ENOMEM;
6150 goto out;
6151 }
6152 /*
6153 * Chances are we'll be called again, so go ahead and do
6154 * readahead
6155 */
6156 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04006157 }
6158
Chris Mason179e29e2007-11-01 11:28:41 -04006159 ret = btrfs_lookup_file_extent(trans, root, path,
6160 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006161 if (ret < 0) {
6162 err = ret;
6163 goto out;
6164 }
6165
6166 if (ret != 0) {
6167 if (path->slots[0] == 0)
6168 goto not_found;
6169 path->slots[0]--;
6170 }
6171
Chris Mason5f39d392007-10-15 16:14:19 -04006172 leaf = path->nodes[0];
6173 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006174 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006175 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006176 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6177 found_type = btrfs_key_type(&found_key);
6178 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006179 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006180 /*
6181 * If we backup past the first extent we want to move forward
6182 * and see if there is an extent in front of us, otherwise we'll
6183 * say there is a hole for our whole search range which can
6184 * cause problems.
6185 */
6186 extent_end = start;
6187 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006188 }
6189
Chris Mason5f39d392007-10-15 16:14:19 -04006190 found_type = btrfs_file_extent_type(leaf, item);
6191 extent_start = found_key.offset;
Li Zefan261507a02010-12-17 14:21:50 +08006192 compress_type = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04006193 if (found_type == BTRFS_FILE_EXTENT_REG ||
6194 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006195 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006196 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006197 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6198 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08006199 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Qu Wenruofda28322013-02-26 08:10:22 +00006200 extent_end = ALIGN(extent_start + size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04006201 }
Josef Bacik25a50342013-10-14 12:08:38 -04006202next:
Yan Zheng9036c102008-10-30 14:19:41 -04006203 if (start >= extent_end) {
6204 path->slots[0]++;
6205 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6206 ret = btrfs_next_leaf(root, path);
6207 if (ret < 0) {
6208 err = ret;
6209 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006210 }
Yan Zheng9036c102008-10-30 14:19:41 -04006211 if (ret > 0)
6212 goto not_found;
6213 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006214 }
Yan Zheng9036c102008-10-30 14:19:41 -04006215 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6216 if (found_key.objectid != objectid ||
6217 found_key.type != BTRFS_EXTENT_DATA_KEY)
6218 goto not_found;
6219 if (start + len <= found_key.offset)
6220 goto not_found;
6221 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006222 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006223 em->len = found_key.offset - start;
6224 goto not_found_em;
6225 }
6226
Josef Bacikcc95bef2013-04-04 14:31:27 -04006227 em->ram_bytes = btrfs_file_extent_ram_bytes(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04006228 if (found_type == BTRFS_FILE_EXTENT_REG ||
6229 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04006230 em->start = extent_start;
6231 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05006232 em->orig_start = extent_start -
6233 btrfs_file_extent_offset(leaf, item);
Josef Bacikb4939682012-12-03 10:31:19 -05006234 em->orig_block_len = btrfs_file_extent_disk_num_bytes(leaf,
6235 item);
Chris Masondb945352007-10-15 16:15:53 -04006236 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
6237 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04006238 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04006239 goto insert;
6240 }
Li Zefan261507a02010-12-17 14:21:50 +08006241 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006242 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08006243 em->compress_type = compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006244 em->block_start = bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05006245 em->block_len = em->orig_block_len;
Chris Masonc8b97812008-10-29 14:49:59 -04006246 } else {
6247 bytenr += btrfs_file_extent_offset(leaf, item);
6248 em->block_start = bytenr;
6249 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04006250 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
6251 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04006252 }
Chris Masona52d9a82007-08-27 16:49:44 -04006253 goto insert;
6254 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006255 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006256 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006257 size_t size;
6258 size_t extent_offset;
6259 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006260
Chris Masona52d9a82007-08-27 16:49:44 -04006261 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04006262 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04006263 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04006264 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04006265 goto out;
6266 }
6267
Chris Mason514ac8a2014-01-03 21:07:00 -08006268 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04006269 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05006270 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04006271 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006272 em->start = extent_start + extent_offset;
Qu Wenruofda28322013-02-26 08:10:22 +00006273 em->len = ALIGN(copy_size, root->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006274 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006275 em->orig_start = em->start;
Li Zefan261507a02010-12-17 14:21:50 +08006276 if (compress_type) {
Chris Masonc8b97812008-10-29 14:49:59 -04006277 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08006278 em->compress_type = compress_type;
6279 }
Yan689f9342007-10-29 11:41:07 -04006280 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006281 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006282 if (btrfs_file_extent_compression(leaf, item) !=
6283 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006284 ret = uncompress_inline(path, inode, page,
6285 pg_offset,
6286 extent_offset, item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006287 BUG_ON(ret); /* -ENOMEM */
Chris Masonc8b97812008-10-29 14:49:59 -04006288 } else {
6289 map = kmap(page);
6290 read_extent_buffer(leaf, map + pg_offset, ptr,
6291 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04006292 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
6293 memset(map + pg_offset + copy_size, 0,
6294 PAGE_CACHE_SIZE - pg_offset -
6295 copy_size);
6296 }
Chris Masonc8b97812008-10-29 14:49:59 -04006297 kunmap(page);
6298 }
Chris Mason179e29e2007-11-01 11:28:41 -04006299 flush_dcache_page(page);
6300 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006301 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006302 if (!trans) {
6303 kunmap(page);
6304 free_extent_map(em);
6305 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006306
David Sterbab3b4aa72011-04-21 01:20:15 +02006307 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006308 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006309
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006310 if (IS_ERR(trans))
6311 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006312 goto again;
6313 }
Chris Masonc8b97812008-10-29 14:49:59 -04006314 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006315 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006316 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006317 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006318 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006319 }
Chris Masond1310b22008-01-24 16:13:08 -05006320 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006321 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006322 goto insert;
6323 } else {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00006324 WARN(1, KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04006325 }
6326not_found:
6327 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006328 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006329 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006330not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04006331 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04006332 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006333insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006334 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006335 if (em->start > start || extent_map_end(em) <= start) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006336 btrfs_err(root->fs_info, "bad extent! em: [%llu %llu] passed [%llu %llu]",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006337 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04006338 err = -EIO;
6339 goto out;
6340 }
Chris Masond1310b22008-01-24 16:13:08 -05006341
6342 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006343 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006344 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006345 /* it is possible that someone inserted the extent into the tree
6346 * while we had the lock dropped. It is also possible that
6347 * an overlapping map exists in the tree
6348 */
Chris Masona52d9a82007-08-27 16:49:44 -04006349 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04006350 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006351
6352 ret = 0;
6353
Chris Mason3b951512008-04-17 11:29:12 -04006354 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04006355 if (existing && (existing->start > start ||
6356 existing->start + existing->len <= start)) {
6357 free_extent_map(existing);
6358 existing = NULL;
6359 }
Chris Mason3b951512008-04-17 11:29:12 -04006360 if (!existing) {
6361 existing = lookup_extent_mapping(em_tree, em->start,
6362 em->len);
6363 if (existing) {
6364 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006365 em, start,
6366 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04006367 free_extent_map(existing);
6368 if (err) {
6369 free_extent_map(em);
6370 em = NULL;
6371 }
6372 } else {
6373 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04006374 free_extent_map(em);
6375 em = NULL;
6376 }
6377 } else {
6378 free_extent_map(em);
6379 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006380 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006381 }
Chris Masona52d9a82007-08-27 16:49:44 -04006382 }
Chris Mason890871b2009-09-02 16:24:52 -04006383 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006384out:
liubo1abe9b82011-03-24 11:18:59 +00006385
Steven Rostedt4cd85872013-11-14 22:57:29 -05006386 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00006387
Chris Masonf4219502008-07-22 11:18:09 -04006388 if (path)
6389 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04006390 if (trans) {
6391 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05006392 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04006393 err = ret;
6394 }
Chris Masona52d9a82007-08-27 16:49:44 -04006395 if (err) {
6396 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04006397 return ERR_PTR(err);
6398 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006399 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04006400 return em;
6401}
6402
Chris Masonec29ed52011-02-23 16:23:20 -05006403struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
6404 size_t pg_offset, u64 start, u64 len,
6405 int create)
6406{
6407 struct extent_map *em;
6408 struct extent_map *hole_em = NULL;
6409 u64 range_start = start;
6410 u64 end;
6411 u64 found;
6412 u64 found_end;
6413 int err = 0;
6414
6415 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
6416 if (IS_ERR(em))
6417 return em;
6418 if (em) {
6419 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00006420 * if our em maps to
6421 * - a hole or
6422 * - a pre-alloc extent,
6423 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05006424 */
Liu Bof9e4fb52013-01-07 10:10:12 +00006425 if (em->block_start != EXTENT_MAP_HOLE &&
6426 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05006427 return em;
6428 else
6429 hole_em = em;
6430 }
6431
6432 /* check to see if we've wrapped (len == -1 or similar) */
6433 end = start + len;
6434 if (end < start)
6435 end = (u64)-1;
6436 else
6437 end -= 1;
6438
6439 em = NULL;
6440
6441 /* ok, we didn't find anything, lets look for delalloc */
6442 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
6443 end, len, EXTENT_DELALLOC, 1);
6444 found_end = range_start + found;
6445 if (found_end < range_start)
6446 found_end = (u64)-1;
6447
6448 /*
6449 * we didn't find anything useful, return
6450 * the original results from get_extent()
6451 */
6452 if (range_start > end || found_end <= start) {
6453 em = hole_em;
6454 hole_em = NULL;
6455 goto out;
6456 }
6457
6458 /* adjust the range_start to make sure it doesn't
6459 * go backwards from the start they passed in
6460 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05306461 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05006462 found = found_end - range_start;
6463
6464 if (found > 0) {
6465 u64 hole_start = start;
6466 u64 hole_len = len;
6467
David Sterba172ddd62011-04-21 00:48:27 +02006468 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05006469 if (!em) {
6470 err = -ENOMEM;
6471 goto out;
6472 }
6473 /*
6474 * when btrfs_get_extent can't find anything it
6475 * returns one huge hole
6476 *
6477 * make sure what it found really fits our range, and
6478 * adjust to make sure it is based on the start from
6479 * the caller
6480 */
6481 if (hole_em) {
6482 u64 calc_end = extent_map_end(hole_em);
6483
6484 if (calc_end <= start || (hole_em->start > end)) {
6485 free_extent_map(hole_em);
6486 hole_em = NULL;
6487 } else {
6488 hole_start = max(hole_em->start, start);
6489 hole_len = calc_end - hole_start;
6490 }
6491 }
6492 em->bdev = NULL;
6493 if (hole_em && range_start > hole_start) {
6494 /* our hole starts before our delalloc, so we
6495 * have to return just the parts of the hole
6496 * that go until the delalloc starts
6497 */
6498 em->len = min(hole_len,
6499 range_start - hole_start);
6500 em->start = hole_start;
6501 em->orig_start = hole_start;
6502 /*
6503 * don't adjust block start at all,
6504 * it is fixed at EXTENT_MAP_HOLE
6505 */
6506 em->block_start = hole_em->block_start;
6507 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00006508 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
6509 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05006510 } else {
6511 em->start = range_start;
6512 em->len = found;
6513 em->orig_start = range_start;
6514 em->block_start = EXTENT_MAP_DELALLOC;
6515 em->block_len = found;
6516 }
6517 } else if (hole_em) {
6518 return hole_em;
6519 }
6520out:
6521
6522 free_extent_map(hole_em);
6523 if (err) {
6524 free_extent_map(em);
6525 return ERR_PTR(err);
6526 }
6527 return em;
6528}
6529
Josef Bacik4b46fce2010-05-23 11:00:55 -04006530static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
6531 u64 start, u64 len)
6532{
6533 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04006534 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006535 struct btrfs_key ins;
6536 u64 alloc_hint;
6537 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006538
Josef Bacik4b46fce2010-05-23 11:00:55 -04006539 alloc_hint = get_extent_allocation_hint(inode, start, len);
Josef Bacik00361582013-08-14 14:02:47 -04006540 ret = btrfs_reserve_extent(root, len, root->sectorsize, 0,
Josef Bacik81c9ad22012-01-18 10:56:06 -05006541 alloc_hint, &ins, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006542 if (ret)
6543 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006544
Josef Bacik70c8a912012-10-11 16:54:30 -04006545 em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006546 ins.offset, ins.offset, ins.offset, 0);
Josef Bacik00361582013-08-14 14:02:47 -04006547 if (IS_ERR(em)) {
6548 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
6549 return em;
6550 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006551
6552 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
6553 ins.offset, ins.offset, 0);
6554 if (ret) {
6555 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
Josef Bacik00361582013-08-14 14:02:47 -04006556 free_extent_map(em);
6557 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006558 }
Josef Bacik00361582013-08-14 14:02:47 -04006559
Josef Bacik4b46fce2010-05-23 11:00:55 -04006560 return em;
6561}
6562
Chris Mason46bfbb52010-05-26 11:04:10 -04006563/*
6564 * returns 1 when the nocow is safe, < 1 on error, 0 if the
6565 * block must be cow'd
6566 */
Josef Bacik00361582013-08-14 14:02:47 -04006567noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04006568 u64 *orig_start, u64 *orig_block_len,
6569 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04006570{
Josef Bacik00361582013-08-14 14:02:47 -04006571 struct btrfs_trans_handle *trans;
Chris Mason46bfbb52010-05-26 11:04:10 -04006572 struct btrfs_path *path;
6573 int ret;
6574 struct extent_buffer *leaf;
6575 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08006576 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04006577 struct btrfs_file_extent_item *fi;
6578 struct btrfs_key key;
6579 u64 disk_bytenr;
6580 u64 backref_offset;
6581 u64 extent_end;
6582 u64 num_bytes;
6583 int slot;
6584 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04006585 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08006586
Chris Mason46bfbb52010-05-26 11:04:10 -04006587 path = btrfs_alloc_path();
6588 if (!path)
6589 return -ENOMEM;
6590
Josef Bacik00361582013-08-14 14:02:47 -04006591 ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04006592 offset, 0);
6593 if (ret < 0)
6594 goto out;
6595
6596 slot = path->slots[0];
6597 if (ret == 1) {
6598 if (slot == 0) {
6599 /* can't find the item, must cow */
6600 ret = 0;
6601 goto out;
6602 }
6603 slot--;
6604 }
6605 ret = 0;
6606 leaf = path->nodes[0];
6607 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08006608 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04006609 key.type != BTRFS_EXTENT_DATA_KEY) {
6610 /* not our file or wrong item type, must cow */
6611 goto out;
6612 }
6613
6614 if (key.offset > offset) {
6615 /* Wrong offset, must cow */
6616 goto out;
6617 }
6618
6619 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
6620 found_type = btrfs_file_extent_type(leaf, fi);
6621 if (found_type != BTRFS_FILE_EXTENT_REG &&
6622 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
6623 /* not a regular extent, must cow */
6624 goto out;
6625 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04006626
6627 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
6628 goto out;
6629
Miao Xiee77751a2013-12-27 21:11:50 +08006630 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
6631 if (extent_end <= offset)
6632 goto out;
6633
Chris Mason46bfbb52010-05-26 11:04:10 -04006634 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04006635 if (disk_bytenr == 0)
6636 goto out;
6637
6638 if (btrfs_file_extent_compression(leaf, fi) ||
6639 btrfs_file_extent_encryption(leaf, fi) ||
6640 btrfs_file_extent_other_encoding(leaf, fi))
6641 goto out;
6642
Chris Mason46bfbb52010-05-26 11:04:10 -04006643 backref_offset = btrfs_file_extent_offset(leaf, fi);
6644
Josef Bacik7ee9e442013-06-21 16:37:03 -04006645 if (orig_start) {
6646 *orig_start = key.offset - backref_offset;
6647 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
6648 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
6649 }
Josef Bacikeb384b52013-04-24 16:32:55 -04006650
Chris Mason46bfbb52010-05-26 11:04:10 -04006651 if (btrfs_extent_readonly(root, disk_bytenr))
6652 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08006653
6654 num_bytes = min(offset + *len, extent_end) - offset;
6655 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
6656 u64 range_end;
6657
6658 range_end = round_up(offset + num_bytes, root->sectorsize) - 1;
6659 ret = test_range_bit(io_tree, offset, range_end,
6660 EXTENT_DELALLOC, 0, NULL);
6661 if (ret) {
6662 ret = -EAGAIN;
6663 goto out;
6664 }
6665 }
6666
Josef Bacik1bda19e2013-10-18 12:10:36 -04006667 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04006668
6669 /*
6670 * look for other files referencing this extent, if we
6671 * find any we must cow
6672 */
Josef Bacik00361582013-08-14 14:02:47 -04006673 trans = btrfs_join_transaction(root);
6674 if (IS_ERR(trans)) {
6675 ret = 0;
Chris Mason46bfbb52010-05-26 11:04:10 -04006676 goto out;
Josef Bacik00361582013-08-14 14:02:47 -04006677 }
6678
6679 ret = btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
6680 key.offset - backref_offset, disk_bytenr);
6681 btrfs_end_transaction(trans, root);
6682 if (ret) {
6683 ret = 0;
6684 goto out;
6685 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006686
6687 /*
6688 * adjust disk_bytenr and num_bytes to cover just the bytes
6689 * in this extent we are about to write. If there
6690 * are any csums in that range we have to cow in order
6691 * to keep the csums correct
6692 */
6693 disk_bytenr += backref_offset;
6694 disk_bytenr += offset - key.offset;
Chris Mason46bfbb52010-05-26 11:04:10 -04006695 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
6696 goto out;
6697 /*
6698 * all of the above have passed, it is safe to overwrite this extent
6699 * without cow
6700 */
Josef Bacikeb384b52013-04-24 16:32:55 -04006701 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04006702 ret = 1;
6703out:
6704 btrfs_free_path(path);
6705 return ret;
6706}
6707
Josef Bacikeb838e72012-07-31 16:28:48 -04006708static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
6709 struct extent_state **cached_state, int writing)
6710{
6711 struct btrfs_ordered_extent *ordered;
6712 int ret = 0;
6713
6714 while (1) {
6715 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6716 0, cached_state);
6717 /*
6718 * We're concerned with the entire range that we're going to be
6719 * doing DIO to, so we need to make sure theres no ordered
6720 * extents in this range.
6721 */
6722 ordered = btrfs_lookup_ordered_range(inode, lockstart,
6723 lockend - lockstart + 1);
6724
6725 /*
6726 * We need to make sure there are no buffered pages in this
6727 * range either, we could have raced between the invalidate in
6728 * generic_file_direct_write and locking the extent. The
6729 * invalidate needs to happen so that reads after a write do not
6730 * get stale data.
6731 */
6732 if (!ordered && (!writing ||
6733 !test_range_bit(&BTRFS_I(inode)->io_tree,
6734 lockstart, lockend, EXTENT_UPTODATE, 0,
6735 *cached_state)))
6736 break;
6737
6738 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6739 cached_state, GFP_NOFS);
6740
6741 if (ordered) {
6742 btrfs_start_ordered_extent(inode, ordered, 1);
6743 btrfs_put_ordered_extent(ordered);
6744 } else {
6745 /* Screw you mmap */
6746 ret = filemap_write_and_wait_range(inode->i_mapping,
6747 lockstart,
6748 lockend);
6749 if (ret)
6750 break;
6751
6752 /*
6753 * If we found a page that couldn't be invalidated just
6754 * fall back to buffered.
6755 */
6756 ret = invalidate_inode_pages2_range(inode->i_mapping,
6757 lockstart >> PAGE_CACHE_SHIFT,
6758 lockend >> PAGE_CACHE_SHIFT);
6759 if (ret)
6760 break;
6761 }
6762
6763 cond_resched();
6764 }
6765
6766 return ret;
6767}
6768
Josef Bacik69ffb542012-09-11 15:40:07 -04006769static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
6770 u64 len, u64 orig_start,
6771 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006772 u64 orig_block_len, u64 ram_bytes,
6773 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04006774{
6775 struct extent_map_tree *em_tree;
6776 struct extent_map *em;
6777 struct btrfs_root *root = BTRFS_I(inode)->root;
6778 int ret;
6779
6780 em_tree = &BTRFS_I(inode)->extent_tree;
6781 em = alloc_extent_map();
6782 if (!em)
6783 return ERR_PTR(-ENOMEM);
6784
6785 em->start = start;
6786 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04006787 em->mod_start = start;
6788 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04006789 em->len = len;
6790 em->block_len = block_len;
6791 em->block_start = block_start;
6792 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05006793 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04006794 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04006795 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04006796 set_bit(EXTENT_FLAG_PINNED, &em->flags);
6797 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05006798 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04006799
6800 do {
6801 btrfs_drop_extent_cache(inode, em->start,
6802 em->start + em->len - 1, 0);
6803 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006804 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04006805 write_unlock(&em_tree->lock);
6806 } while (ret == -EEXIST);
6807
6808 if (ret) {
6809 free_extent_map(em);
6810 return ERR_PTR(ret);
6811 }
6812
6813 return em;
6814}
6815
6816
Josef Bacik4b46fce2010-05-23 11:00:55 -04006817static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
6818 struct buffer_head *bh_result, int create)
6819{
6820 struct extent_map *em;
6821 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04006822 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006823 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04006824 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006825 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04006826 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00006827 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006828
Miao Xie172a5042013-02-21 02:48:22 -07006829 if (create)
Josef Bacikeb838e72012-07-31 16:28:48 -04006830 unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07006831 else
Josef Bacikc3298612012-08-03 16:49:19 -04006832 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04006833
Josef Bacikc3298612012-08-03 16:49:19 -04006834 lockstart = start;
6835 lockend = start + len - 1;
6836
Josef Bacikeb838e72012-07-31 16:28:48 -04006837 /*
6838 * If this errors out it's because we couldn't invalidate pagecache for
6839 * this range and we need to fallback to buffered.
6840 */
6841 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
6842 return -ENOTBLK;
6843
Josef Bacik4b46fce2010-05-23 11:00:55 -04006844 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04006845 if (IS_ERR(em)) {
6846 ret = PTR_ERR(em);
6847 goto unlock_err;
6848 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006849
6850 /*
6851 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
6852 * io. INLINE is special, and we could probably kludge it in here, but
6853 * it's still buffered so for safety lets just fall back to the generic
6854 * buffered path.
6855 *
6856 * For COMPRESSED we _have_ to read the entire extent in so we can
6857 * decompress it, so there will be buffering required no matter what we
6858 * do, so go ahead and fallback to buffered.
6859 *
6860 * We return -ENOTBLK because thats what makes DIO go ahead and go back
6861 * to buffered IO. Don't blame me, this is the price we pay for using
6862 * the generic code.
6863 */
6864 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
6865 em->block_start == EXTENT_MAP_INLINE) {
6866 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006867 ret = -ENOTBLK;
6868 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006869 }
6870
6871 /* Just a good old fashioned hole, return */
6872 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
6873 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
6874 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006875 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006876 }
6877
6878 /*
6879 * We don't allocate a new extent in the following cases
6880 *
6881 * 1) The inode is marked as NODATACOW. In this case we'll just use the
6882 * existing extent.
6883 * 2) The extent is marked as PREALLOC. We're good to go here and can
6884 * just use the extent.
6885 *
6886 */
Chris Mason46bfbb52010-05-26 11:04:10 -04006887 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04006888 len = min(len, em->len - (start - em->start));
6889 lockstart = start + len;
6890 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04006891 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006892
6893 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
6894 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
6895 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04006896 int type;
6897 int ret;
Josef Bacikeb384b52013-04-24 16:32:55 -04006898 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006899
6900 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6901 type = BTRFS_ORDERED_PREALLOC;
6902 else
6903 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04006904 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04006905 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04006906
Josef Bacik00361582013-08-14 14:02:47 -04006907 if (can_nocow_extent(inode, start, &len, &orig_start,
Josef Bacik7ee9e442013-06-21 16:37:03 -04006908 &orig_block_len, &ram_bytes) == 1) {
Josef Bacik69ffb542012-09-11 15:40:07 -04006909 if (type == BTRFS_ORDERED_PREALLOC) {
6910 free_extent_map(em);
6911 em = create_pinned_em(inode, start, len,
6912 orig_start,
Josef Bacikb4939682012-12-03 10:31:19 -05006913 block_start, len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006914 orig_block_len,
6915 ram_bytes, type);
Josef Bacik00361582013-08-14 14:02:47 -04006916 if (IS_ERR(em))
Josef Bacik69ffb542012-09-11 15:40:07 -04006917 goto unlock_err;
Josef Bacik69ffb542012-09-11 15:40:07 -04006918 }
6919
Chris Mason46bfbb52010-05-26 11:04:10 -04006920 ret = btrfs_add_ordered_extent_dio(inode, start,
6921 block_start, len, len, type);
Chris Mason46bfbb52010-05-26 11:04:10 -04006922 if (ret) {
6923 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006924 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04006925 }
6926 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006927 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006928 }
Josef Bacik00361582013-08-14 14:02:47 -04006929
Chris Mason46bfbb52010-05-26 11:04:10 -04006930 /*
6931 * this will cow the extent, reset the len in case we changed
6932 * it above
6933 */
6934 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04006935 free_extent_map(em);
6936 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04006937 if (IS_ERR(em)) {
6938 ret = PTR_ERR(em);
6939 goto unlock_err;
6940 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006941 len = min(len, em->len - (start - em->start));
6942unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04006943 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
6944 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04006945 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006946 bh_result->b_bdev = em->bdev;
6947 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04006948 if (create) {
6949 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6950 set_buffer_new(bh_result);
6951
6952 /*
6953 * Need to update the i_size under the extent lock so buffered
6954 * readers will get the updated i_size when we unlock.
6955 */
6956 if (start + len > i_size_read(inode))
6957 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00006958
Miao Xie172a5042013-02-21 02:48:22 -07006959 spin_lock(&BTRFS_I(inode)->lock);
6960 BTRFS_I(inode)->outstanding_extents++;
6961 spin_unlock(&BTRFS_I(inode)->lock);
6962
Miao Xie09348562013-02-07 10:12:07 +00006963 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6964 lockstart + len - 1, EXTENT_DELALLOC, NULL,
6965 &cached_state, GFP_NOFS);
6966 BUG_ON(ret);
Josef Bacikc3473e82012-06-19 10:59:00 -04006967 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006968
Josef Bacikeb838e72012-07-31 16:28:48 -04006969 /*
6970 * In the case of write we need to clear and unlock the entire range,
6971 * in the case of read we need to unlock only the end area that we
6972 * aren't using if there is any left over space.
6973 */
Liu Bo24c03fa2012-08-22 20:10:38 -06006974 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00006975 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6976 lockend, unlock_bits, 1, 0,
6977 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06006978 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04006979 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06006980 }
Josef Bacikeb838e72012-07-31 16:28:48 -04006981
Josef Bacik4b46fce2010-05-23 11:00:55 -04006982 free_extent_map(em);
6983
6984 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006985
6986unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04006987 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6988 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
6989 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006990}
6991
Josef Bacik4b46fce2010-05-23 11:00:55 -04006992static void btrfs_endio_direct_read(struct bio *bio, int err)
6993{
Miao Xiee65e1532010-11-22 03:04:43 +00006994 struct btrfs_dio_private *dip = bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006995 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
6996 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006997 struct inode *inode = dip->inode;
6998 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason9be33952013-05-17 18:30:14 -04006999 struct bio *dio_bio;
Miao Xiefacc8a222013-07-25 19:22:34 +08007000 u32 *csums = (u32 *)dip->csum;
7001 int index = 0;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007002 u64 start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007003
7004 start = dip->logical_offset;
7005 do {
7006 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
7007 struct page *page = bvec->bv_page;
7008 char *kaddr;
7009 u32 csum = ~(u32)0;
7010 unsigned long flags;
7011
7012 local_irq_save(flags);
Cong Wang7ac687d2011-11-25 23:14:28 +08007013 kaddr = kmap_atomic(page);
Liu Bob0496682013-03-14 14:57:45 +00007014 csum = btrfs_csum_data(kaddr + bvec->bv_offset,
Josef Bacik4b46fce2010-05-23 11:00:55 -04007015 csum, bvec->bv_len);
7016 btrfs_csum_final(csum, (char *)&csum);
Cong Wang7ac687d2011-11-25 23:14:28 +08007017 kunmap_atomic(kaddr);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007018 local_irq_restore(flags);
7019
7020 flush_dcache_page(bvec->bv_page);
Miao Xiefacc8a222013-07-25 19:22:34 +08007021 if (csum != csums[index]) {
7022 btrfs_err(root->fs_info, "csum failed ino %llu off %llu csum %u expected csum %u",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02007023 btrfs_ino(inode), start, csum,
7024 csums[index]);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007025 err = -EIO;
7026 }
7027 }
7028
7029 start += bvec->bv_len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007030 bvec++;
Miao Xiefacc8a222013-07-25 19:22:34 +08007031 index++;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007032 } while (bvec <= bvec_end);
7033
7034 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007035 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04007036 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007037
Josef Bacik4b46fce2010-05-23 11:00:55 -04007038 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04007039
7040 /* If we had a csum failure make sure to clear the uptodate flag */
7041 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04007042 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
7043 dio_end_io(dio_bio, err);
7044 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007045}
7046
7047static void btrfs_endio_direct_write(struct bio *bio, int err)
7048{
7049 struct btrfs_dio_private *dip = bio->bi_private;
7050 struct inode *inode = dip->inode;
7051 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007052 struct btrfs_ordered_extent *ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05007053 u64 ordered_offset = dip->logical_offset;
7054 u64 ordered_bytes = dip->bytes;
Chris Mason9be33952013-05-17 18:30:14 -04007055 struct bio *dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007056 int ret;
7057
7058 if (err)
7059 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05007060again:
7061 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
7062 &ordered_offset,
Josef Bacik5fd02042012-05-02 14:00:54 -04007063 ordered_bytes, !err);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007064 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05007065 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007066
Qu Wenruofccb5d82014-02-28 10:46:10 +08007067 btrfs_init_work(&ordered->work, finish_ordered_fn, NULL, NULL);
7068 btrfs_queue_work(root->fs_info->endio_write_workers,
7069 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05007070out_test:
7071 /*
7072 * our bio might span multiple ordered extents. If we haven't
7073 * completed the accounting for the whole dio, go back and try again
7074 */
7075 if (ordered_offset < dip->logical_offset + dip->bytes) {
7076 ordered_bytes = dip->logical_offset + dip->bytes -
7077 ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04007078 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05007079 goto again;
7080 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007081out_done:
Chris Mason9be33952013-05-17 18:30:14 -04007082 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007083
Josef Bacik4b46fce2010-05-23 11:00:55 -04007084 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04007085
7086 /* If we had an error make sure to clear the uptodate flag */
7087 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04007088 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
7089 dio_end_io(dio_bio, err);
7090 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007091}
7092
Chris Masoneaf25d92010-05-25 09:48:28 -04007093static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
7094 struct bio *bio, int mirror_num,
7095 unsigned long bio_flags, u64 offset)
7096{
7097 int ret;
7098 struct btrfs_root *root = BTRFS_I(inode)->root;
7099 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007100 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04007101 return 0;
7102}
7103
Miao Xiee65e1532010-11-22 03:04:43 +00007104static void btrfs_end_dio_bio(struct bio *bio, int err)
7105{
7106 struct btrfs_dio_private *dip = bio->bi_private;
7107
7108 if (err) {
Frank Holtonefe120a2013-12-20 11:37:06 -05007109 btrfs_err(BTRFS_I(dip->inode)->root->fs_info,
7110 "direct IO failed ino %llu rw %lu sector %#Lx len %u err no %d",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02007111 btrfs_ino(dip->inode), bio->bi_rw,
Jan Beulich3dd14622010-12-07 14:54:09 +00007112 (unsigned long long)bio->bi_sector, bio->bi_size, err);
Miao Xiee65e1532010-11-22 03:04:43 +00007113 dip->errors = 1;
7114
7115 /*
7116 * before atomic variable goto zero, we must make sure
7117 * dip->errors is perceived to be set.
7118 */
7119 smp_mb__before_atomic_dec();
7120 }
7121
7122 /* if there are more bios still pending for this dio, just exit */
7123 if (!atomic_dec_and_test(&dip->pending_bios))
7124 goto out;
7125
Chris Mason9be33952013-05-17 18:30:14 -04007126 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00007127 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04007128 } else {
7129 set_bit(BIO_UPTODATE, &dip->dio_bio->bi_flags);
Miao Xiee65e1532010-11-22 03:04:43 +00007130 bio_endio(dip->orig_bio, 0);
7131 }
7132out:
7133 bio_put(bio);
7134}
7135
7136static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
7137 u64 first_sector, gfp_t gfp_flags)
7138{
7139 int nr_vecs = bio_get_nr_vecs(bdev);
7140 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
7141}
7142
7143static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
7144 int rw, u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007145 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00007146{
Miao Xiefacc8a222013-07-25 19:22:34 +08007147 struct btrfs_dio_private *dip = bio->bi_private;
Miao Xiee65e1532010-11-22 03:04:43 +00007148 int write = rw & REQ_WRITE;
7149 struct btrfs_root *root = BTRFS_I(inode)->root;
7150 int ret;
7151
Josef Bacikb812ce22012-11-16 13:56:32 -05007152 if (async_submit)
7153 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
7154
Miao Xiee65e1532010-11-22 03:04:43 +00007155 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04007156
7157 if (!write) {
7158 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
7159 if (ret)
7160 goto err;
7161 }
Miao Xiee65e1532010-11-22 03:04:43 +00007162
Josef Bacik1ae39932011-04-06 14:41:34 -04007163 if (skip_sum)
7164 goto map;
7165
7166 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00007167 ret = btrfs_wq_submit_bio(root->fs_info,
7168 inode, rw, bio, 0, 0,
7169 file_offset,
7170 __btrfs_submit_bio_start_direct_io,
7171 __btrfs_submit_bio_done);
7172 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04007173 } else if (write) {
7174 /*
7175 * If we aren't doing async submit, calculate the csum of the
7176 * bio now.
7177 */
7178 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
7179 if (ret)
7180 goto err;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007181 } else if (!skip_sum) {
Miao Xiefacc8a222013-07-25 19:22:34 +08007182 ret = btrfs_lookup_bio_sums_dio(root, inode, dip, bio,
7183 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007184 if (ret)
7185 goto err;
7186 }
Miao Xiee65e1532010-11-22 03:04:43 +00007187
Josef Bacik1ae39932011-04-06 14:41:34 -04007188map:
7189 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007190err:
7191 bio_put(bio);
7192 return ret;
7193}
7194
7195static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
7196 int skip_sum)
7197{
7198 struct inode *inode = dip->inode;
7199 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00007200 struct bio *bio;
7201 struct bio *orig_bio = dip->orig_bio;
7202 struct bio_vec *bvec = orig_bio->bi_io_vec;
7203 u64 start_sector = orig_bio->bi_sector;
7204 u64 file_offset = dip->logical_offset;
7205 u64 submit_len = 0;
7206 u64 map_length;
7207 int nr_pages = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007208 int ret = 0;
Josef Bacik1ae39932011-04-06 14:41:34 -04007209 int async_submit = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007210
Miao Xiee65e1532010-11-22 03:04:43 +00007211 map_length = orig_bio->bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007212 ret = btrfs_map_block(root->fs_info, rw, start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007213 &map_length, NULL, 0);
7214 if (ret) {
Josef Bacik64728bb2011-04-25 19:43:52 -04007215 bio_put(orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00007216 return -EIO;
7217 }
Miao Xiefacc8a222013-07-25 19:22:34 +08007218
Josef Bacik02f57c72011-04-06 14:25:44 -04007219 if (map_length >= orig_bio->bi_size) {
7220 bio = orig_bio;
7221 goto submit;
7222 }
7223
David Woodhouse53b381b2013-01-29 18:40:14 -05007224 /* async crcs make it difficult to collect full stripe writes. */
7225 if (btrfs_get_alloc_profile(root, 1) &
7226 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6))
7227 async_submit = 0;
7228 else
7229 async_submit = 1;
7230
Josef Bacik02f57c72011-04-06 14:25:44 -04007231 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
7232 if (!bio)
7233 return -ENOMEM;
7234 bio->bi_private = dip;
7235 bio->bi_end_io = btrfs_end_dio_bio;
7236 atomic_inc(&dip->pending_bios);
7237
Miao Xiee65e1532010-11-22 03:04:43 +00007238 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
7239 if (unlikely(map_length < submit_len + bvec->bv_len ||
7240 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
7241 bvec->bv_offset) < bvec->bv_len)) {
7242 /*
7243 * inc the count before we submit the bio so
7244 * we know the end IO handler won't happen before
7245 * we inc the count. Otherwise, the dip might get freed
7246 * before we're done setting it up
7247 */
7248 atomic_inc(&dip->pending_bios);
7249 ret = __btrfs_submit_dio_bio(bio, inode, rw,
7250 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007251 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007252 if (ret) {
7253 bio_put(bio);
7254 atomic_dec(&dip->pending_bios);
7255 goto out_err;
7256 }
7257
Miao Xiee65e1532010-11-22 03:04:43 +00007258 start_sector += submit_len >> 9;
7259 file_offset += submit_len;
7260
7261 submit_len = 0;
7262 nr_pages = 0;
7263
7264 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
7265 start_sector, GFP_NOFS);
7266 if (!bio)
7267 goto out_err;
7268 bio->bi_private = dip;
7269 bio->bi_end_io = btrfs_end_dio_bio;
7270
7271 map_length = orig_bio->bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007272 ret = btrfs_map_block(root->fs_info, rw,
Stefan Behrens3ec706c2012-11-05 15:46:42 +01007273 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007274 &map_length, NULL, 0);
7275 if (ret) {
7276 bio_put(bio);
7277 goto out_err;
7278 }
7279 } else {
7280 submit_len += bvec->bv_len;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307281 nr_pages++;
Miao Xiee65e1532010-11-22 03:04:43 +00007282 bvec++;
7283 }
7284 }
7285
Josef Bacik02f57c72011-04-06 14:25:44 -04007286submit:
Miao Xiee65e1532010-11-22 03:04:43 +00007287 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007288 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007289 if (!ret)
7290 return 0;
7291
7292 bio_put(bio);
7293out_err:
7294 dip->errors = 1;
7295 /*
7296 * before atomic variable goto zero, we must
7297 * make sure dip->errors is perceived to be set.
7298 */
7299 smp_mb__before_atomic_dec();
7300 if (atomic_dec_and_test(&dip->pending_bios))
7301 bio_io_error(dip->orig_bio);
7302
7303 /* bio_end_io() will handle error, so we needn't return it */
7304 return 0;
7305}
7306
Chris Mason9be33952013-05-17 18:30:14 -04007307static void btrfs_submit_direct(int rw, struct bio *dio_bio,
7308 struct inode *inode, loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007309{
7310 struct btrfs_root *root = BTRFS_I(inode)->root;
7311 struct btrfs_dio_private *dip;
Chris Mason9be33952013-05-17 18:30:14 -04007312 struct bio *io_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007313 int skip_sum;
Miao Xiefacc8a222013-07-25 19:22:34 +08007314 int sum_len;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02007315 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007316 int ret = 0;
Miao Xiefacc8a222013-07-25 19:22:34 +08007317 u16 csum_size;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007318
7319 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
7320
Chris Mason9be33952013-05-17 18:30:14 -04007321 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04007322 if (!io_bio) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007323 ret = -ENOMEM;
7324 goto free_ordered;
7325 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007326
Miao Xiefacc8a222013-07-25 19:22:34 +08007327 if (!skip_sum && !write) {
7328 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
7329 sum_len = dio_bio->bi_size >> inode->i_sb->s_blocksize_bits;
7330 sum_len *= csum_size;
7331 } else {
7332 sum_len = 0;
7333 }
7334
7335 dip = kmalloc(sizeof(*dip) + sum_len, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04007336 if (!dip) {
7337 ret = -ENOMEM;
7338 goto free_io_bio;
7339 }
7340
7341 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007342 dip->inode = inode;
7343 dip->logical_offset = file_offset;
Miao Xiee6da5d2e2013-06-19 18:19:17 +08007344 dip->bytes = dio_bio->bi_size;
Chris Mason9be33952013-05-17 18:30:14 -04007345 dip->disk_bytenr = (u64)dio_bio->bi_sector << 9;
7346 io_bio->bi_private = dip;
Miao Xiee65e1532010-11-22 03:04:43 +00007347 dip->errors = 0;
Chris Mason9be33952013-05-17 18:30:14 -04007348 dip->orig_bio = io_bio;
7349 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00007350 atomic_set(&dip->pending_bios, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007351
7352 if (write)
Chris Mason9be33952013-05-17 18:30:14 -04007353 io_bio->bi_end_io = btrfs_endio_direct_write;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007354 else
Chris Mason9be33952013-05-17 18:30:14 -04007355 io_bio->bi_end_io = btrfs_endio_direct_read;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007356
Miao Xiee65e1532010-11-22 03:04:43 +00007357 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
7358 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04007359 return;
Chris Mason9be33952013-05-17 18:30:14 -04007360
7361free_io_bio:
7362 bio_put(io_bio);
7363
Josef Bacik4b46fce2010-05-23 11:00:55 -04007364free_ordered:
7365 /*
7366 * If this is a write, we need to clean up the reserved space and kill
7367 * the ordered extent.
7368 */
7369 if (write) {
7370 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05007371 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007372 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
7373 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
7374 btrfs_free_reserved_extent(root, ordered->start,
7375 ordered->disk_len);
7376 btrfs_put_ordered_extent(ordered);
7377 btrfs_put_ordered_extent(ordered);
7378 }
Chris Mason9be33952013-05-17 18:30:14 -04007379 bio_endio(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007380}
7381
Chris Mason5a5f79b2010-05-26 21:33:37 -04007382static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
7383 const struct iovec *iov, loff_t offset,
7384 unsigned long nr_segs)
7385{
7386 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00007387 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007388 size_t size;
7389 unsigned long addr;
7390 unsigned blocksize_mask = root->sectorsize - 1;
7391 ssize_t retval = -EINVAL;
7392 loff_t end = offset;
7393
7394 if (offset & blocksize_mask)
7395 goto out;
7396
7397 /* Check the memory alignment. Blocks cannot straddle pages */
7398 for (seg = 0; seg < nr_segs; seg++) {
7399 addr = (unsigned long)iov[seg].iov_base;
7400 size = iov[seg].iov_len;
7401 end += size;
Josef Bacika1b75f72011-04-08 15:51:18 +00007402 if ((addr & blocksize_mask) || (size & blocksize_mask))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007403 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00007404
7405 /* If this is a write we don't need to check anymore */
7406 if (rw & WRITE)
7407 continue;
7408
7409 /*
7410 * Check to make sure we don't have duplicate iov_base's in this
7411 * iovec, if so return EINVAL, otherwise we'll get csum errors
7412 * when reading back.
7413 */
7414 for (i = seg + 1; i < nr_segs; i++) {
7415 if (iov[seg].iov_base == iov[i].iov_base)
7416 goto out;
7417 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04007418 }
7419 retval = 0;
7420out:
7421 return retval;
7422}
Josef Bacikeb838e72012-07-31 16:28:48 -04007423
Chris Mason16432982008-04-10 10:23:21 -04007424static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
7425 const struct iovec *iov, loff_t offset,
7426 unsigned long nr_segs)
7427{
Josef Bacik4b46fce2010-05-23 11:00:55 -04007428 struct file *file = iocb->ki_filp;
7429 struct inode *inode = file->f_mapping->host;
Miao Xie09348562013-02-07 10:12:07 +00007430 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00007431 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00007432 bool wakeup = true;
7433 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00007434 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007435
Chris Mason5a5f79b2010-05-26 21:33:37 -04007436 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
Josef Bacikeb838e72012-07-31 16:28:48 -04007437 offset, nr_segs))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007438 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007439
Miao Xie38851cc2013-02-08 07:04:11 +00007440 atomic_inc(&inode->i_dio_count);
7441 smp_mb__after_atomic_inc();
7442
Josef Bacik0e267c42013-07-02 10:38:02 -04007443 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08007444 * The generic stuff only does filemap_write_and_wait_range, which
7445 * isn't enough if we've written compressed pages to this area, so
7446 * we need to flush the dirty pages again to make absolutely sure
7447 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04007448 */
7449 count = iov_length(iov, nr_segs);
Miao Xie41bd9ca2014-03-06 13:54:57 +08007450 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
7451 &BTRFS_I(inode)->runtime_flags))
7452 filemap_fdatawrite_range(inode->i_mapping, offset, count);
Josef Bacik0e267c42013-07-02 10:38:02 -04007453
Miao Xie09348562013-02-07 10:12:07 +00007454 if (rw & WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00007455 /*
7456 * If the write DIO is beyond the EOF, we need update
7457 * the isize, but it is protected by i_mutex. So we can
7458 * not unlock the i_mutex at this case.
7459 */
7460 if (offset + count <= inode->i_size) {
7461 mutex_unlock(&inode->i_mutex);
7462 relock = true;
7463 }
Miao Xie09348562013-02-07 10:12:07 +00007464 ret = btrfs_delalloc_reserve_space(inode, count);
7465 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00007466 goto out;
7467 } else if (unlikely(test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
7468 &BTRFS_I(inode)->runtime_flags))) {
7469 inode_dio_done(inode);
7470 flags = DIO_LOCKING | DIO_SKIP_HOLES;
7471 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00007472 }
7473
7474 ret = __blockdev_direct_IO(rw, iocb, inode,
7475 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
7476 iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
Miao Xie2e60a512013-02-08 07:01:08 +00007477 btrfs_submit_direct, flags);
Miao Xie09348562013-02-07 10:12:07 +00007478 if (rw & WRITE) {
7479 if (ret < 0 && ret != -EIOCBQUEUED)
7480 btrfs_delalloc_release_space(inode, count);
Miao Xie172a5042013-02-21 02:48:22 -07007481 else if (ret >= 0 && (size_t)ret < count)
Miao Xie09348562013-02-07 10:12:07 +00007482 btrfs_delalloc_release_space(inode,
7483 count - (size_t)ret);
Miao Xie172a5042013-02-21 02:48:22 -07007484 else
7485 btrfs_delalloc_release_metadata(inode, 0);
Miao Xie09348562013-02-07 10:12:07 +00007486 }
Miao Xie38851cc2013-02-08 07:04:11 +00007487out:
Miao Xie2e60a512013-02-08 07:01:08 +00007488 if (wakeup)
7489 inode_dio_done(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00007490 if (relock)
7491 mutex_lock(&inode->i_mutex);
Miao Xie09348562013-02-07 10:12:07 +00007492
7493 return ret;
Chris Mason16432982008-04-10 10:23:21 -04007494}
7495
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007496#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
7497
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007498static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
7499 __u64 start, __u64 len)
7500{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007501 int ret;
7502
7503 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
7504 if (ret)
7505 return ret;
7506
Chris Masonec29ed52011-02-23 16:23:20 -05007507 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007508}
7509
Chris Mason9ebefb182007-06-15 13:50:00 -04007510int btrfs_readpage(struct file *file, struct page *page)
7511{
Chris Masond1310b22008-01-24 16:13:08 -05007512 struct extent_io_tree *tree;
7513 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02007514 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04007515}
Chris Mason1832a6d2007-12-21 16:27:21 -05007516
Chris Mason39279cc2007-06-12 06:35:45 -04007517static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
7518{
Chris Masond1310b22008-01-24 16:13:08 -05007519 struct extent_io_tree *tree;
Chris Masonb888db2b2007-08-27 16:49:44 -04007520
7521
7522 if (current->flags & PF_MEMALLOC) {
7523 redirty_page_for_writepage(wbc, page);
7524 unlock_page(page);
7525 return 0;
7526 }
Chris Masond1310b22008-01-24 16:13:08 -05007527 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04007528 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
7529}
Chris Mason39279cc2007-06-12 06:35:45 -04007530
Eric Sandeen48a3b632013-04-25 20:41:01 +00007531static int btrfs_writepages(struct address_space *mapping,
7532 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04007533{
Chris Masond1310b22008-01-24 16:13:08 -05007534 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05007535
Chris Masond1310b22008-01-24 16:13:08 -05007536 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f02e2007-11-01 19:45:34 -04007537 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
7538}
7539
Chris Mason3ab2fb52007-11-08 10:59:22 -05007540static int
7541btrfs_readpages(struct file *file, struct address_space *mapping,
7542 struct list_head *pages, unsigned nr_pages)
7543{
Chris Masond1310b22008-01-24 16:13:08 -05007544 struct extent_io_tree *tree;
7545 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05007546 return extent_readpages(tree, mapping, pages, nr_pages,
7547 btrfs_get_extent);
7548}
Chris Masone6dcd2d2008-07-17 12:53:50 -04007549static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04007550{
Chris Masond1310b22008-01-24 16:13:08 -05007551 struct extent_io_tree *tree;
7552 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04007553 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04007554
Chris Masond1310b22008-01-24 16:13:08 -05007555 tree = &BTRFS_I(page->mapping->host)->io_tree;
7556 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05007557 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04007558 if (ret == 1) {
7559 ClearPagePrivate(page);
7560 set_page_private(page, 0);
7561 page_cache_release(page);
7562 }
7563 return ret;
7564}
Chris Mason39279cc2007-06-12 06:35:45 -04007565
Chris Masone6dcd2d2008-07-17 12:53:50 -04007566static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
7567{
Chris Mason98509cf2008-09-11 15:51:43 -04007568 if (PageWriteback(page) || PageDirty(page))
7569 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05007570 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007571}
7572
Lukas Czernerd47992f2013-05-21 23:17:23 -04007573static void btrfs_invalidatepage(struct page *page, unsigned int offset,
7574 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04007575{
Josef Bacik5fd02042012-05-02 14:00:54 -04007576 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05007577 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007578 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007579 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007580 u64 page_start = page_offset(page);
7581 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00007582 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04007583
Chris Mason8b62b722009-09-02 16:53:46 -04007584 /*
7585 * we have the page locked, so new writeback can't start,
7586 * and the dirty bit won't be cleared while we are here.
7587 *
7588 * Wait for IO on this page so that we can safely clear
7589 * the PagePrivate2 bit and do ordered accounting
7590 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007591 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04007592
Josef Bacik5fd02042012-05-02 14:00:54 -04007593 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007594 if (offset) {
7595 btrfs_releasepage(page, GFP_NOFS);
7596 return;
7597 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00007598
7599 if (!inode_evicting)
7600 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
7601 ordered = btrfs_lookup_ordered_extent(inode, page_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007602 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04007603 /*
7604 * IO on this page will never be started, so we need
7605 * to account for any ordered extents now
7606 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00007607 if (!inode_evicting)
7608 clear_extent_bit(tree, page_start, page_end,
7609 EXTENT_DIRTY | EXTENT_DELALLOC |
7610 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
7611 EXTENT_DEFRAG, 1, 0, &cached_state,
7612 GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04007613 /*
7614 * whoever cleared the private bit is responsible
7615 * for the finish_ordered_io
7616 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04007617 if (TestClearPagePrivate2(page)) {
7618 struct btrfs_ordered_inode_tree *tree;
7619 u64 new_len;
7620
7621 tree = &BTRFS_I(inode)->ordered_tree;
7622
7623 spin_lock_irq(&tree->lock);
7624 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
7625 new_len = page_start - ordered->file_offset;
7626 if (new_len < ordered->truncated_len)
7627 ordered->truncated_len = new_len;
7628 spin_unlock_irq(&tree->lock);
7629
7630 if (btrfs_dec_test_ordered_pending(inode, &ordered,
7631 page_start,
7632 PAGE_CACHE_SIZE, 1))
7633 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04007634 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007635 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00007636 if (!inode_evicting) {
7637 cached_state = NULL;
7638 lock_extent_bits(tree, page_start, page_end, 0,
7639 &cached_state);
7640 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007641 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00007642
7643 if (!inode_evicting) {
7644 clear_extent_bit(tree, page_start, page_end,
7645 EXTENT_LOCKED | EXTENT_DIRTY |
7646 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
7647 EXTENT_DEFRAG, 1, 1,
7648 &cached_state, GFP_NOFS);
7649
7650 __btrfs_releasepage(page, GFP_NOFS);
7651 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007652
Chris Mason4a096752008-07-21 10:29:44 -04007653 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04007654 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04007655 ClearPagePrivate(page);
7656 set_page_private(page, 0);
7657 page_cache_release(page);
7658 }
Chris Mason39279cc2007-06-12 06:35:45 -04007659}
7660
Chris Mason9ebefb182007-06-15 13:50:00 -04007661/*
7662 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
7663 * called from a page fault handler when a page is first dirtied. Hence we must
7664 * be careful to check for EOF conditions here. We set the page up correctly
7665 * for a written page which means we get ENOSPC checking when writing into
7666 * holes and correct delalloc and unwritten extent mapping on filesystems that
7667 * support these features.
7668 *
7669 * We are not allowed to take the i_mutex here so we have to play games to
7670 * protect against truncate races as the page could now be beyond EOF. Because
7671 * vmtruncate() writes the inode size before removing pages, once we have the
7672 * page lock we can determine safely if the page is beyond EOF. If it is not
7673 * beyond EOF, then the page is guaranteed safe against truncation until we
7674 * unlock the page.
7675 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07007676int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04007677{
Nick Pigginc2ec1752009-03-31 15:23:21 -07007678 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05007679 struct inode *inode = file_inode(vma->vm_file);
Chris Mason1832a6d2007-12-21 16:27:21 -05007680 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007681 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7682 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007683 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007684 char *kaddr;
7685 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04007686 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05007687 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05007688 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007689 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007690 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04007691
Jan Karab2b5ef52012-06-12 16:20:45 +02007692 sb_start_pagefault(inode->i_sb);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04007693 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007694 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04007695 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05007696 reserved = 1;
7697 }
Nick Piggin56a76f82009-03-31 15:23:23 -07007698 if (ret) {
7699 if (ret == -ENOMEM)
7700 ret = VM_FAULT_OOM;
7701 else /* -ENOSPC, -EIO, etc */
7702 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05007703 if (reserved)
7704 goto out;
7705 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07007706 }
Chris Mason1832a6d2007-12-21 16:27:21 -05007707
Nick Piggin56a76f82009-03-31 15:23:23 -07007708 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007709again:
Chris Mason9ebefb182007-06-15 13:50:00 -04007710 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04007711 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007712 page_start = page_offset(page);
7713 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04007714
Chris Mason9ebefb182007-06-15 13:50:00 -04007715 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04007716 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04007717 /* page got truncated out from underneath us */
7718 goto out_unlock;
7719 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007720 wait_on_page_writeback(page);
7721
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007722 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007723 set_page_extent_mapped(page);
7724
Chris Masoneb84ae02008-07-17 13:53:27 -04007725 /*
7726 * we can't set the delalloc bits if there are pending ordered
7727 * extents. Drop our locks and wait for them to finish
7728 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007729 ordered = btrfs_lookup_ordered_extent(inode, page_start);
7730 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007731 unlock_extent_cached(io_tree, page_start, page_end,
7732 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007733 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04007734 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007735 btrfs_put_ordered_extent(ordered);
7736 goto again;
7737 }
7738
Josef Bacikfbf19082009-10-01 17:10:23 -04007739 /*
7740 * XXX - page_mkwrite gets called every time the page is dirtied, even
7741 * if it was already dirty, so for space accounting reasons we need to
7742 * clear any delalloc bits for the range we are fixing to save. There
7743 * is probably a better way to do this, but for now keep consistent with
7744 * prepare_pages in the normal write path.
7745 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00007746 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06007747 EXTENT_DIRTY | EXTENT_DELALLOC |
7748 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00007749 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04007750
Josef Bacik2ac55d42010-02-03 19:33:23 +00007751 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
7752 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007753 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007754 unlock_extent_cached(io_tree, page_start, page_end,
7755 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007756 ret = VM_FAULT_SIGBUS;
7757 goto out_unlock;
7758 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007759 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04007760
7761 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04007762 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04007763 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04007764 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04007765 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04007766
Chris Masone6dcd2d2008-07-17 12:53:50 -04007767 if (zero_start != PAGE_CACHE_SIZE) {
7768 kaddr = kmap(page);
7769 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
7770 flush_dcache_page(page);
7771 kunmap(page);
7772 }
Chris Mason247e7432008-07-17 12:53:51 -04007773 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007774 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04007775 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007776
Chris Mason257c62e2009-10-13 13:21:08 -04007777 BTRFS_I(inode)->last_trans = root->fs_info->generation;
7778 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06007779 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04007780
Josef Bacik2ac55d42010-02-03 19:33:23 +00007781 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04007782
7783out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02007784 if (!ret) {
7785 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04007786 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02007787 }
Chris Mason9ebefb182007-06-15 13:50:00 -04007788 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05007789out:
Josef Bacikec39e182012-01-12 19:10:12 -05007790 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007791out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02007792 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04007793 return ret;
7794}
7795
Josef Bacika41ad392011-01-31 15:30:16 -05007796static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04007797{
7798 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04007799 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04007800 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05007801 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007802 struct btrfs_trans_handle *trans;
Chris Masondbe674a2008-07-17 12:54:05 -04007803 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04007804 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04007805
Josef Bacik0ef8b722013-10-25 16:13:35 -04007806 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
7807 (u64)-1);
7808 if (ret)
7809 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04007810
Josef Bacikfcb80c22011-05-03 10:40:22 -04007811 /*
7812 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
7813 * 3 things going on here
7814 *
7815 * 1) We need to reserve space for our orphan item and the space to
7816 * delete our orphan item. Lord knows we don't want to have a dangling
7817 * orphan item because we didn't reserve space to remove it.
7818 *
7819 * 2) We need to reserve space to update our inode.
7820 *
7821 * 3) We need to have something to cache all the space that is going to
7822 * be free'd up by the truncate operation, but also have some slack
7823 * space reserved in case it uses space during the truncate (thank you
7824 * very much snapshotting).
7825 *
7826 * And we need these to all be seperate. The fact is we can use alot of
7827 * space doing the truncate, and we have no earthly idea how much space
7828 * we will use, so we need the truncate reservation to be seperate so it
7829 * doesn't end up using space reserved for updating the inode or
7830 * removing the orphan item. We also need to be able to stop the
7831 * transaction and start a new one, which means we need to be able to
7832 * update the inode several times, and we have no idea of knowing how
7833 * many times that will be, so we can't just reserve 1 item for the
7834 * entirety of the opration, so that has to be done seperately as well.
7835 * Then there is the orphan item, which does indeed need to be held on
7836 * to for the whole operation, and we need nobody to touch this reserved
7837 * space except the orphan code.
7838 *
7839 * So that leaves us with
7840 *
7841 * 1) root->orphan_block_rsv - for the orphan deletion.
7842 * 2) rsv - for the truncate reservation, which we will steal from the
7843 * transaction reservation.
7844 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
7845 * updating the inode.
7846 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06007847 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007848 if (!rsv)
7849 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04007850 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04007851 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05007852
Josef Bacik907cbce2011-08-08 13:46:15 -04007853 /*
Josef Bacik07127182011-08-19 10:29:59 -04007854 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04007855 * 1 for updating the inode.
7856 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05007857 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007858 if (IS_ERR(trans)) {
7859 err = PTR_ERR(trans);
7860 goto out;
7861 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05007862
Josef Bacik907cbce2011-08-08 13:46:15 -04007863 /* Migrate the slack space for the truncate to our reserve */
7864 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
7865 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007866 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05007867
Chris Mason5a3f23d2009-03-31 13:27:11 -04007868 /*
7869 * setattr is responsible for setting the ordered_data_close flag,
7870 * but that is only tested during the last file release. That
7871 * could happen well after the next commit, leaving a great big
7872 * window where new writes may get lost if someone chooses to write
7873 * to this file after truncating to zero
7874 *
7875 * The inode doesn't have any dirty data here, and so if we commit
7876 * this is a noop. If someone immediately starts writing to the inode
7877 * it is very likely we'll catch some of their writes in this
7878 * transaction, and the commit will find this file on the ordered
7879 * data list with good things to send down.
7880 *
7881 * This is a best effort solution, there is still a window where
7882 * using truncate to replace the contents of the file will
7883 * end up with a zero length file after a crash.
7884 */
Josef Bacik72ac3c02012-05-23 14:13:11 -04007885 if (inode->i_size == 0 && test_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
7886 &BTRFS_I(inode)->runtime_flags))
Chris Mason5a3f23d2009-03-31 13:27:11 -04007887 btrfs_add_ordered_operation(trans, root, inode);
7888
Josef Bacik5dc562c2012-08-17 13:14:17 -04007889 /*
7890 * So if we truncate and then write and fsync we normally would just
7891 * write the extents that changed, which is a problem if we need to
7892 * first truncate that entire inode. So set this flag so we write out
7893 * all of the extents in the inode to the sync log so we're completely
7894 * safe.
7895 */
7896 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007897 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04007898
Yan, Zheng80825102009-11-12 09:35:36 +00007899 while (1) {
7900 ret = btrfs_truncate_inode_items(trans, root, inode,
7901 inode->i_size,
7902 BTRFS_EXTENT_DATA_KEY);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007903 if (ret != -ENOSPC) {
Josef Bacik3893e332011-01-31 16:03:11 -05007904 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007905 break;
Josef Bacik3893e332011-01-31 16:03:11 -05007906 }
Chris Mason39279cc2007-06-12 06:35:45 -04007907
Josef Bacikfcb80c22011-05-03 10:40:22 -04007908 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007909 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007910 if (ret) {
7911 err = ret;
7912 break;
7913 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04007914
Yan, Zheng80825102009-11-12 09:35:36 +00007915 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007916 btrfs_btree_balance_dirty(root);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007917
7918 trans = btrfs_start_transaction(root, 2);
7919 if (IS_ERR(trans)) {
7920 ret = err = PTR_ERR(trans);
7921 trans = NULL;
7922 break;
7923 }
7924
7925 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
7926 rsv, min_size);
7927 BUG_ON(ret); /* shouldn't happen */
7928 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007929 }
7930
7931 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04007932 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007933 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007934 if (ret)
7935 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007936 }
7937
Chris Mason917c16b2011-11-08 14:49:59 -05007938 if (trans) {
7939 trans->block_rsv = &root->fs_info->trans_block_rsv;
7940 ret = btrfs_update_inode(trans, root, inode);
7941 if (ret && !err)
7942 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04007943
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007944 ret = btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007945 btrfs_btree_balance_dirty(root);
Chris Mason917c16b2011-11-08 14:49:59 -05007946 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04007947
7948out:
7949 btrfs_free_block_rsv(root, rsv);
7950
Josef Bacik3893e332011-01-31 16:03:11 -05007951 if (ret && !err)
7952 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05007953
Josef Bacik3893e332011-01-31 16:03:11 -05007954 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007955}
7956
Sven Wegener3b963622008-06-09 21:57:42 -04007957/*
Chris Masond352ac62008-09-29 15:18:18 -04007958 * create a new subvolume directory/inode (helper for the ioctl).
7959 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05007960int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00007961 struct btrfs_root *new_root,
7962 struct btrfs_root *parent_root,
7963 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04007964{
Chris Mason39279cc2007-06-12 06:35:45 -04007965 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04007966 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007967 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007968
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01007969 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
7970 new_dirid, new_dirid,
7971 S_IFDIR | (~current_umask() & S_IRWXUGO),
7972 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04007973 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04007974 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007975 inode->i_op = &btrfs_dir_inode_operations;
7976 inode->i_fop = &btrfs_dir_file_operations;
7977
Miklos Szeredibfe86842011-10-28 14:13:29 +02007978 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04007979 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04007980
Filipe David Borba Manana63541922014-01-07 11:47:46 +00007981 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
7982 if (err)
7983 btrfs_err(new_root->fs_info,
7984 "error inheriting subvolume %llu properties: %d\n",
7985 new_root->root_key.objectid, err);
7986
Yan, Zheng76dda932009-09-21 16:00:26 -04007987 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04007988
Yan, Zheng76dda932009-09-21 16:00:26 -04007989 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07007990 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007991}
7992
Chris Mason39279cc2007-06-12 06:35:45 -04007993struct inode *btrfs_alloc_inode(struct super_block *sb)
7994{
7995 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007996 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007997
7998 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
7999 if (!ei)
8000 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008001
8002 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008003 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04008004 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04008005 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04008006 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008007 ei->delalloc_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008008 ei->disk_i_size = 0;
8009 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04008010 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008011 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08008012 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008013 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06008014 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008015
Josef Bacik9e0baf62011-07-15 15:16:44 +00008016 spin_lock_init(&ei->lock);
8017 ei->outstanding_extents = 0;
8018 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008019
Josef Bacik72ac3c02012-05-23 14:13:11 -04008020 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08008021 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008022
Miao Xie16cdcec2011-04-22 18:12:22 +08008023 ei->delayed_node = NULL;
8024
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008025 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02008026 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02008027 extent_io_tree_init(&ei->io_tree, &inode->i_data);
8028 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04008029 ei->io_tree.track_uptodate = 1;
8030 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05008031 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008032 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05008033 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008034 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008035 INIT_LIST_HEAD(&ei->delalloc_inodes);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008036 INIT_LIST_HEAD(&ei->ordered_operations);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008037 RB_CLEAR_NODE(&ei->rb_node);
8038
8039 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008040}
8041
Josef Bacikaaedb552013-10-11 14:44:09 -04008042#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
8043void btrfs_test_destroy_inode(struct inode *inode)
8044{
8045 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
8046 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8047}
8048#endif
8049
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008050static void btrfs_i_callback(struct rcu_head *head)
8051{
8052 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008053 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8054}
8055
Chris Mason39279cc2007-06-12 06:35:45 -04008056void btrfs_destroy_inode(struct inode *inode)
8057{
Chris Masone6dcd2d2008-07-17 12:53:50 -04008058 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008059 struct btrfs_root *root = BTRFS_I(inode)->root;
8060
Al Virob3d9b7a2012-06-09 13:51:19 -04008061 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04008062 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00008063 WARN_ON(BTRFS_I(inode)->outstanding_extents);
8064 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04008065 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
8066 WARN_ON(BTRFS_I(inode)->csum_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04008067
Chris Mason5a3f23d2009-03-31 13:27:11 -04008068 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05008069 * This can happen where we create an inode, but somebody else also
8070 * created the same inode and we need to destroy the one we already
8071 * created.
8072 */
8073 if (!root)
8074 goto free;
8075
8076 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04008077 * Make sure we're properly removed from the ordered operation
8078 * lists.
8079 */
8080 smp_mb();
8081 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
Miao Xie199c2a92013-05-15 07:48:23 +00008082 spin_lock(&root->fs_info->ordered_root_lock);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008083 list_del_init(&BTRFS_I(inode)->ordered_operations);
Miao Xie199c2a92013-05-15 07:48:23 +00008084 spin_unlock(&root->fs_info->ordered_root_lock);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008085 }
8086
Josef Bacik8a35d952012-05-23 14:26:42 -04008087 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
8088 &BTRFS_I(inode)->runtime_flags)) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008089 btrfs_info(root->fs_info, "inode %llu still on the orphan list",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02008090 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04008091 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04008092 }
Josef Bacik7b128762008-07-24 12:17:14 -04008093
Chris Masond3977122009-01-05 21:25:51 -05008094 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008095 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
8096 if (!ordered)
8097 break;
8098 else {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008099 btrfs_err(root->fs_info, "found ordered extent %llu %llu on inode cleanup",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02008100 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008101 btrfs_remove_ordered_extent(inode, ordered);
8102 btrfs_put_ordered_extent(ordered);
8103 btrfs_put_ordered_extent(ordered);
8104 }
8105 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008106 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008107 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05008108free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008109 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04008110}
8111
Al Viro45321ac2010-06-07 13:43:19 -04008112int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04008113{
8114 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04008115
Naohiro Aota6379ef92013-06-06 09:56:34 +00008116 if (root == NULL)
8117 return 1;
8118
Liu Bofa6ac872013-02-20 14:10:23 +00008119 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02008120 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04008121 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04008122 else
Al Viro45321ac2010-06-07 13:43:19 -04008123 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04008124}
8125
Sven Wegener0ee0fda2008-07-30 16:54:26 -04008126static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04008127{
8128 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
8129
8130 inode_init_once(&ei->vfs_inode);
8131}
8132
8133void btrfs_destroy_cachep(void)
8134{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10008135 /*
8136 * Make sure all delayed rcu free inodes are flushed before we
8137 * destroy cache.
8138 */
8139 rcu_barrier();
Chris Mason39279cc2007-06-12 06:35:45 -04008140 if (btrfs_inode_cachep)
8141 kmem_cache_destroy(btrfs_inode_cachep);
8142 if (btrfs_trans_handle_cachep)
8143 kmem_cache_destroy(btrfs_trans_handle_cachep);
8144 if (btrfs_transaction_cachep)
8145 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008146 if (btrfs_path_cachep)
8147 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05008148 if (btrfs_free_space_cachep)
8149 kmem_cache_destroy(btrfs_free_space_cachep);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008150 if (btrfs_delalloc_work_cachep)
8151 kmem_cache_destroy(btrfs_delalloc_work_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008152}
8153
8154int btrfs_init_cachep(void)
8155{
David Sterba837e1972012-09-07 03:00:48 -06008156 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008157 sizeof(struct btrfs_inode), 0,
8158 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04008159 if (!btrfs_inode_cachep)
8160 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008161
David Sterba837e1972012-09-07 03:00:48 -06008162 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008163 sizeof(struct btrfs_trans_handle), 0,
8164 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008165 if (!btrfs_trans_handle_cachep)
8166 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008167
David Sterba837e1972012-09-07 03:00:48 -06008168 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008169 sizeof(struct btrfs_transaction), 0,
8170 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008171 if (!btrfs_transaction_cachep)
8172 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008173
David Sterba837e1972012-09-07 03:00:48 -06008174 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008175 sizeof(struct btrfs_path), 0,
8176 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008177 if (!btrfs_path_cachep)
8178 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008179
David Sterba837e1972012-09-07 03:00:48 -06008180 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05008181 sizeof(struct btrfs_free_space), 0,
8182 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
8183 if (!btrfs_free_space_cachep)
8184 goto fail;
8185
Miao Xie8ccf6f192012-10-25 09:28:04 +00008186 btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
8187 sizeof(struct btrfs_delalloc_work), 0,
8188 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
8189 NULL);
8190 if (!btrfs_delalloc_work_cachep)
8191 goto fail;
8192
Chris Mason39279cc2007-06-12 06:35:45 -04008193 return 0;
8194fail:
8195 btrfs_destroy_cachep();
8196 return -ENOMEM;
8197}
8198
8199static int btrfs_getattr(struct vfsmount *mnt,
8200 struct dentry *dentry, struct kstat *stat)
8201{
Miao Xiedf0af1a2013-01-29 10:11:59 +00008202 u64 delalloc_bytes;
Chris Mason39279cc2007-06-12 06:35:45 -04008203 struct inode *inode = dentry->d_inode;
David Sterbafadc0d82011-11-20 07:33:38 -05008204 u32 blocksize = inode->i_sb->s_blocksize;
8205
Chris Mason39279cc2007-06-12 06:35:45 -04008206 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04008207 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05008208 stat->blksize = PAGE_CACHE_SIZE;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008209
8210 spin_lock(&BTRFS_I(inode)->lock);
8211 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
8212 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05008213 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00008214 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04008215 return 0;
8216}
8217
Chris Masond3977122009-01-05 21:25:51 -05008218static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
8219 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04008220{
8221 struct btrfs_trans_handle *trans;
8222 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008223 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04008224 struct inode *new_inode = new_dentry->d_inode;
8225 struct inode *old_inode = old_dentry->d_inode;
8226 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008227 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008228 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04008229 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08008230 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008231
Li Zefan33345d012011-04-20 10:31:50 +08008232 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04008233 return -EPERM;
8234
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008235 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08008236 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05008237 return -EXDEV;
8238
Li Zefan33345d012011-04-20 10:31:50 +08008239 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
8240 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008241 return -ENOTEMPTY;
8242
Chris Mason39279cc2007-06-12 06:35:45 -04008243 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008244 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04008245 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05008246
8247
8248 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04008249 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05008250 new_dentry->d_name.name,
8251 new_dentry->d_name.len);
8252
8253 if (ret) {
8254 if (ret == -EEXIST) {
8255 /* we shouldn't get
8256 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05308257 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05008258 return ret;
8259 }
8260 } else {
8261 /* maybe -EOVERFLOW */
8262 return ret;
8263 }
8264 }
8265 ret = 0;
8266
Chris Mason5a3f23d2009-03-31 13:27:11 -04008267 /*
8268 * we're using rename to replace one file with another.
8269 * and the replacement file is large. Start IO on it now so
8270 * we don't add too much work to the end of the transaction
8271 */
Bartlomiej Zolnierkiewicz4baf8c92009-08-07 13:47:08 -04008272 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
Chris Mason5a3f23d2009-03-31 13:27:11 -04008273 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
8274 filemap_flush(old_inode->i_mapping);
8275
Yan, Zheng76dda932009-09-21 16:00:26 -04008276 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08008277 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008278 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008279 /*
8280 * We want to reserve the absolute worst case amount of items. So if
8281 * both inodes are subvols and we need to unlink them then that would
8282 * require 4 item modifications, but if they are both normal inodes it
8283 * would require 5 item modifications, so we'll assume their normal
8284 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
8285 * should cover the worst case number of items we'll modify.
8286 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04008287 trans = btrfs_start_transaction(root, 11);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008288 if (IS_ERR(trans)) {
8289 ret = PTR_ERR(trans);
8290 goto out_notrans;
8291 }
Chris Mason5f39d392007-10-15 16:14:19 -04008292
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008293 if (dest != root)
8294 btrfs_record_root_in_trans(trans, dest);
8295
Yan, Zhenga5719522009-09-24 09:17:31 -04008296 ret = btrfs_set_inode_index(new_dir, &index);
8297 if (ret)
8298 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008299
Miao Xie67de1172013-12-26 13:07:06 +08008300 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08008301 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008302 /* force full log commit if subvolume involved. */
8303 root->fs_info->last_trans_log_full_commit = trans->transid;
8304 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04008305 ret = btrfs_insert_inode_ref(trans, dest,
8306 new_dentry->d_name.name,
8307 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08008308 old_ino,
8309 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04008310 if (ret)
8311 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008312 /*
8313 * this is an ugly little race, but the rename is required
8314 * to make sure that if we crash, the inode is either at the
8315 * old name or the new one. pinning the log transaction lets
8316 * us make sure we don't allow a log commit to come in after
8317 * we unlink the name but before we add the new name back in.
8318 */
8319 btrfs_pin_log_trans(root);
8320 }
Chris Mason12fcfd22009-03-24 10:24:20 -04008321 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04008322 * make sure the inode gets flushed if it is replacing
8323 * something.
8324 */
Li Zefan33345d012011-04-20 10:31:50 +08008325 if (new_inode && new_inode->i_size && S_ISREG(old_inode->i_mode))
Chris Mason5a3f23d2009-03-31 13:27:11 -04008326 btrfs_add_ordered_operation(trans, root, old_inode);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008327
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008328 inode_inc_iversion(old_dir);
8329 inode_inc_iversion(new_dir);
8330 inode_inc_iversion(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008331 old_dir->i_ctime = old_dir->i_mtime = ctime;
8332 new_dir->i_ctime = new_dir->i_mtime = ctime;
8333 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04008334
Chris Mason12fcfd22009-03-24 10:24:20 -04008335 if (old_dentry->d_parent != new_dentry->d_parent)
8336 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
8337
Li Zefan33345d012011-04-20 10:31:50 +08008338 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008339 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
8340 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
8341 old_dentry->d_name.name,
8342 old_dentry->d_name.len);
8343 } else {
Al Viro92986792011-03-04 17:14:37 +00008344 ret = __btrfs_unlink_inode(trans, root, old_dir,
8345 old_dentry->d_inode,
8346 old_dentry->d_name.name,
8347 old_dentry->d_name.len);
8348 if (!ret)
8349 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008350 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008351 if (ret) {
8352 btrfs_abort_transaction(trans, root, ret);
8353 goto out_fail;
8354 }
Chris Mason39279cc2007-06-12 06:35:45 -04008355
8356 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008357 inode_inc_iversion(new_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008358 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08008359 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008360 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
8361 root_objectid = BTRFS_I(new_inode)->location.objectid;
8362 ret = btrfs_unlink_subvol(trans, dest, new_dir,
8363 root_objectid,
8364 new_dentry->d_name.name,
8365 new_dentry->d_name.len);
8366 BUG_ON(new_inode->i_nlink == 0);
8367 } else {
8368 ret = btrfs_unlink_inode(trans, dest, new_dir,
8369 new_dentry->d_inode,
8370 new_dentry->d_name.name,
8371 new_dentry->d_name.len);
8372 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04008373 if (!ret && new_inode->i_nlink == 0)
Chris Masone02119d2008-09-05 16:13:11 -04008374 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008375 if (ret) {
8376 btrfs_abort_transaction(trans, root, ret);
8377 goto out_fail;
8378 }
Chris Mason39279cc2007-06-12 06:35:45 -04008379 }
Josef Bacikaec74772008-07-24 12:12:38 -04008380
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008381 ret = btrfs_add_link(trans, new_dir, old_inode,
8382 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04008383 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008384 if (ret) {
8385 btrfs_abort_transaction(trans, root, ret);
8386 goto out_fail;
8387 }
Chris Mason39279cc2007-06-12 06:35:45 -04008388
Miao Xie67de1172013-12-26 13:07:06 +08008389 if (old_inode->i_nlink == 1)
8390 BTRFS_I(old_inode)->dir_index = index;
8391
Li Zefan33345d012011-04-20 10:31:50 +08008392 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04008393 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00008394 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008395 btrfs_end_log_trans(root);
8396 }
Chris Mason39279cc2007-06-12 06:35:45 -04008397out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008398 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008399out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08008400 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008401 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008402
Chris Mason39279cc2007-06-12 06:35:45 -04008403 return ret;
8404}
8405
Qu Wenruod458b052014-02-28 10:46:19 +08008406static void btrfs_run_delalloc_work(struct btrfs_work *work)
Miao Xie8ccf6f192012-10-25 09:28:04 +00008407{
8408 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04008409 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008410
8411 delalloc_work = container_of(work, struct btrfs_delalloc_work,
8412 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04008413 inode = delalloc_work->inode;
8414 if (delalloc_work->wait) {
8415 btrfs_wait_ordered_range(inode, 0, (u64)-1);
8416 } else {
8417 filemap_flush(inode->i_mapping);
8418 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8419 &BTRFS_I(inode)->runtime_flags))
8420 filemap_flush(inode->i_mapping);
8421 }
Miao Xie8ccf6f192012-10-25 09:28:04 +00008422
8423 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -04008424 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008425 else
Josef Bacik9f23e282013-10-28 15:03:41 -04008426 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008427 complete(&delalloc_work->completion);
8428}
8429
8430struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
8431 int wait, int delay_iput)
8432{
8433 struct btrfs_delalloc_work *work;
8434
8435 work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
8436 if (!work)
8437 return NULL;
8438
8439 init_completion(&work->completion);
8440 INIT_LIST_HEAD(&work->list);
8441 work->inode = inode;
8442 work->wait = wait;
8443 work->delay_iput = delay_iput;
Qu Wenruoa44903a2014-02-28 10:46:09 +08008444 btrfs_init_work(&work->work, btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008445
8446 return work;
8447}
8448
8449void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
8450{
8451 wait_for_completion(&work->completion);
8452 kmem_cache_free(btrfs_delalloc_work_cachep, work);
8453}
8454
Chris Masond352ac62008-09-29 15:18:18 -04008455/*
8456 * some fairly slow code that needs optimization. This walks the list
8457 * of all the inodes with pending delalloc and forces them to disk.
8458 */
Miao Xie6c255e62014-03-06 13:55:01 +08008459static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
8460 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -04008461{
Chris Masonea8c2812008-08-04 23:17:27 -04008462 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008463 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008464 struct btrfs_delalloc_work *work, *next;
8465 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00008466 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008467 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04008468
Miao Xie8ccf6f192012-10-25 09:28:04 +00008469 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008470 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00008471
Miao Xie573bfb72014-03-06 13:55:03 +08008472 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00008473 spin_lock(&root->delalloc_lock);
8474 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008475 while (!list_empty(&splice)) {
8476 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04008477 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008478
Miao Xieeb73c1b2013-05-15 07:48:22 +00008479 list_move_tail(&binode->delalloc_inodes,
8480 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008481 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00008482 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00008483 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008484 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008485 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008486 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008487
8488 work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
8489 if (unlikely(!work)) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -04008490 if (delay_iput)
8491 btrfs_add_delayed_iput(inode);
8492 else
8493 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008494 ret = -ENOMEM;
Miao Xie6c255e62014-03-06 13:55:01 +08008495 break;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008496 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008497 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +08008498 btrfs_queue_work(root->fs_info->flush_workers,
8499 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +08008500 ret++;
8501 if (nr != -1 && ret >= nr)
8502 break;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008503 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00008504 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04008505 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008506 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04008507
Miao Xie1eafa6c2013-01-22 10:49:00 +00008508 list_for_each_entry_safe(work, next, &works, list) {
8509 list_del_init(&work->list);
8510 btrfs_wait_and_free_delalloc_work(work);
8511 }
8512
Miao Xieeb73c1b2013-05-15 07:48:22 +00008513 if (!list_empty_careful(&splice)) {
8514 spin_lock(&root->delalloc_lock);
8515 list_splice_tail(&splice, &root->delalloc_inodes);
8516 spin_unlock(&root->delalloc_lock);
8517 }
Miao Xie573bfb72014-03-06 13:55:03 +08008518 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00008519 return ret;
8520}
8521
8522int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
8523{
8524 int ret;
8525
Wang Shilong2c21b4d2014-01-14 19:42:20 +08008526 if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00008527 return -EROFS;
8528
Miao Xie6c255e62014-03-06 13:55:01 +08008529 ret = __start_delalloc_inodes(root, delay_iput, -1);
8530 if (ret > 0)
8531 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +00008532 /*
8533 * the filemap_flush will queue IO into the worker threads, but
Chris Mason8c8bee12008-09-29 11:19:10 -04008534 * we have to make sure the IO is actually started and that
8535 * ordered extents get created before we return
8536 */
8537 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05008538 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05008539 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04008540 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05008541 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
8542 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04008543 }
8544 atomic_dec(&root->fs_info->async_submit_draining);
Miao Xieeb73c1b2013-05-15 07:48:22 +00008545 return ret;
8546}
8547
Miao Xie6c255e62014-03-06 13:55:01 +08008548int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
8549 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +00008550{
8551 struct btrfs_root *root;
8552 struct list_head splice;
8553 int ret;
8554
Wang Shilong2c21b4d2014-01-14 19:42:20 +08008555 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00008556 return -EROFS;
8557
8558 INIT_LIST_HEAD(&splice);
8559
Miao Xie573bfb72014-03-06 13:55:03 +08008560 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00008561 spin_lock(&fs_info->delalloc_root_lock);
8562 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +08008563 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00008564 root = list_first_entry(&splice, struct btrfs_root,
8565 delalloc_root);
8566 root = btrfs_grab_fs_root(root);
8567 BUG_ON(!root);
8568 list_move_tail(&root->delalloc_root,
8569 &fs_info->delalloc_roots);
8570 spin_unlock(&fs_info->delalloc_root_lock);
8571
Miao Xie6c255e62014-03-06 13:55:01 +08008572 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +00008573 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +08008574 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +00008575 goto out;
8576
Miao Xie6c255e62014-03-06 13:55:01 +08008577 if (nr != -1) {
8578 nr -= ret;
8579 WARN_ON(nr < 0);
8580 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008581 spin_lock(&fs_info->delalloc_root_lock);
8582 }
8583 spin_unlock(&fs_info->delalloc_root_lock);
8584
Miao Xie6c255e62014-03-06 13:55:01 +08008585 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +00008586 atomic_inc(&fs_info->async_submit_draining);
8587 while (atomic_read(&fs_info->nr_async_submits) ||
8588 atomic_read(&fs_info->async_delalloc_pages)) {
8589 wait_event(fs_info->async_submit_wait,
8590 (atomic_read(&fs_info->nr_async_submits) == 0 &&
8591 atomic_read(&fs_info->async_delalloc_pages) == 0));
8592 }
8593 atomic_dec(&fs_info->async_submit_draining);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008594out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00008595 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00008596 spin_lock(&fs_info->delalloc_root_lock);
8597 list_splice_tail(&splice, &fs_info->delalloc_roots);
8598 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008599 }
Miao Xie573bfb72014-03-06 13:55:03 +08008600 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008601 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04008602}
8603
Chris Mason39279cc2007-06-12 06:35:45 -04008604static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
8605 const char *symname)
8606{
8607 struct btrfs_trans_handle *trans;
8608 struct btrfs_root *root = BTRFS_I(dir)->root;
8609 struct btrfs_path *path;
8610 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05008611 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04008612 int err;
8613 int drop_inode = 0;
8614 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05308615 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008616 int name_len;
8617 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04008618 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04008619 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04008620 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04008621
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01008622 name_len = strlen(symname);
Chris Mason39279cc2007-06-12 06:35:45 -04008623 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
8624 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05008625
Josef Bacik9ed74f22009-09-11 16:12:44 -04008626 /*
8627 * 2 items for inode item and ref
8628 * 2 items for dir items
8629 * 1 item for xattr if selinux is on
8630 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04008631 trans = btrfs_start_transaction(root, 5);
8632 if (IS_ERR(trans))
8633 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05008634
Li Zefan581bb052011-04-20 10:06:11 +08008635 err = btrfs_find_free_ino(root, &objectid);
8636 if (err)
8637 goto out_unlock;
8638
Josef Bacikaec74772008-07-24 12:12:38 -04008639 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08008640 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04008641 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008642 if (IS_ERR(inode)) {
8643 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008644 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008645 }
Chris Mason39279cc2007-06-12 06:35:45 -04008646
Eric Paris2a7dba32011-02-01 11:05:39 -05008647 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04008648 if (err) {
8649 drop_inode = 1;
8650 goto out_unlock;
8651 }
8652
Casey Schauflerad19db72011-12-15 10:09:07 -05008653 /*
8654 * If the active LSM wants to access the inode during
8655 * d_instantiate it needs these. Smack checks to see
8656 * if the filesystem supports xattrs by looking at the
8657 * ops vector.
8658 */
8659 inode->i_fop = &btrfs_file_operations;
8660 inode->i_op = &btrfs_file_inode_operations;
8661
Josef Bacika1b075d2010-11-19 20:36:11 +00008662 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04008663 if (err)
8664 drop_inode = 1;
8665 else {
8666 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04008667 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05008668 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04008669 }
Chris Mason39279cc2007-06-12 06:35:45 -04008670 if (drop_inode)
8671 goto out_unlock;
8672
8673 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07008674 if (!path) {
8675 err = -ENOMEM;
8676 drop_inode = 1;
8677 goto out_unlock;
8678 }
Li Zefan33345d012011-04-20 10:31:50 +08008679 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008680 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008681 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
8682 datasize = btrfs_file_extent_calc_inline_size(name_len);
8683 err = btrfs_insert_empty_item(trans, root, path, &key,
8684 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04008685 if (err) {
8686 drop_inode = 1;
Julia Lawallb0839162011-05-14 07:10:51 +00008687 btrfs_free_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04008688 goto out_unlock;
8689 }
Chris Mason5f39d392007-10-15 16:14:19 -04008690 leaf = path->nodes[0];
8691 ei = btrfs_item_ptr(leaf, path->slots[0],
8692 struct btrfs_file_extent_item);
8693 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
8694 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04008695 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04008696 btrfs_set_file_extent_encryption(leaf, ei, 0);
8697 btrfs_set_file_extent_compression(leaf, ei, 0);
8698 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
8699 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
8700
Chris Mason39279cc2007-06-12 06:35:45 -04008701 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04008702 write_extent_buffer(leaf, symname, ptr, name_len);
8703 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04008704 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04008705
Chris Mason39279cc2007-06-12 06:35:45 -04008706 inode->i_op = &btrfs_symlink_inode_operations;
8707 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04008708 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04008709 inode_set_bytes(inode, name_len);
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01008710 btrfs_i_size_write(inode, name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -04008711 err = btrfs_update_inode(trans, root, inode);
8712 if (err)
8713 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04008714
8715out_unlock:
Al Viro08c422c2011-12-23 07:58:13 -05008716 if (!err)
8717 d_instantiate(dentry, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008718 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04008719 if (drop_inode) {
8720 inode_dec_link_count(inode);
8721 iput(inode);
8722 }
Liu Bob53d3f52012-11-14 14:34:34 +00008723 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04008724 return err;
8725}
Chris Mason16432982008-04-10 10:23:21 -04008726
Josef Bacik0af3d002010-06-21 14:48:16 -04008727static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
8728 u64 start, u64 num_bytes, u64 min_size,
8729 loff_t actual_len, u64 *alloc_hint,
8730 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04008731{
Josef Bacik5dc562c2012-08-17 13:14:17 -04008732 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
8733 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04008734 struct btrfs_root *root = BTRFS_I(inode)->root;
8735 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04008736 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00008737 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05008738 u64 cur_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04008739 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04008740 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04008741
Josef Bacik0af3d002010-06-21 14:48:16 -04008742 if (trans)
8743 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04008744 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008745 if (own_trans) {
8746 trans = btrfs_start_transaction(root, 3);
8747 if (IS_ERR(trans)) {
8748 ret = PTR_ERR(trans);
8749 break;
8750 }
Yan Zhengd899e052008-10-30 14:25:28 -04008751 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00008752
Chris Mason154ea282013-03-05 11:11:26 -05008753 cur_bytes = min(num_bytes, 256ULL * 1024 * 1024);
8754 cur_bytes = max(cur_bytes, min_size);
Josef Bacik00361582013-08-14 14:02:47 -04008755 ret = btrfs_reserve_extent(root, cur_bytes, min_size, 0,
8756 *alloc_hint, &ins, 1);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008757 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008758 if (own_trans)
8759 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008760 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00008761 }
8762
Yan Zhengd899e052008-10-30 14:25:28 -04008763 ret = insert_reserved_file_extent(trans, inode,
8764 cur_offset, ins.objectid,
8765 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00008766 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04008767 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008768 if (ret) {
Josef Bacik857cc2f2013-10-07 15:21:08 -04008769 btrfs_free_reserved_extent(root, ins.objectid,
8770 ins.offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008771 btrfs_abort_transaction(trans, root, ret);
8772 if (own_trans)
8773 btrfs_end_transaction(trans, root);
8774 break;
8775 }
Chris Masona1ed8352009-09-11 12:27:37 -04008776 btrfs_drop_extent_cache(inode, cur_offset,
8777 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008778
Josef Bacik5dc562c2012-08-17 13:14:17 -04008779 em = alloc_extent_map();
8780 if (!em) {
8781 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
8782 &BTRFS_I(inode)->runtime_flags);
8783 goto next;
8784 }
8785
8786 em->start = cur_offset;
8787 em->orig_start = cur_offset;
8788 em->len = ins.offset;
8789 em->block_start = ins.objectid;
8790 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05008791 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04008792 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008793 em->bdev = root->fs_info->fs_devices->latest_bdev;
8794 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
8795 em->generation = trans->transid;
8796
8797 while (1) {
8798 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04008799 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04008800 write_unlock(&em_tree->lock);
8801 if (ret != -EEXIST)
8802 break;
8803 btrfs_drop_extent_cache(inode, cur_offset,
8804 cur_offset + ins.offset - 1,
8805 0);
8806 }
8807 free_extent_map(em);
8808next:
Yan Zhengd899e052008-10-30 14:25:28 -04008809 num_bytes -= ins.offset;
8810 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04008811 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00008812
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008813 inode_inc_iversion(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04008814 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02008815 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04008816 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04008817 (actual_len > inode->i_size) &&
8818 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00008819 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00008820 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00008821 else
Josef Bacik55a61d12010-11-22 18:50:32 +00008822 i_size = cur_offset;
8823 i_size_write(inode, i_size);
8824 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008825 }
8826
Yan Zhengd899e052008-10-30 14:25:28 -04008827 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008828
8829 if (ret) {
8830 btrfs_abort_transaction(trans, root, ret);
8831 if (own_trans)
8832 btrfs_end_transaction(trans, root);
8833 break;
8834 }
Yan Zhengd899e052008-10-30 14:25:28 -04008835
Josef Bacik0af3d002010-06-21 14:48:16 -04008836 if (own_trans)
8837 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008838 }
Yan Zhengd899e052008-10-30 14:25:28 -04008839 return ret;
8840}
8841
Josef Bacik0af3d002010-06-21 14:48:16 -04008842int btrfs_prealloc_file_range(struct inode *inode, int mode,
8843 u64 start, u64 num_bytes, u64 min_size,
8844 loff_t actual_len, u64 *alloc_hint)
8845{
8846 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
8847 min_size, actual_len, alloc_hint,
8848 NULL);
8849}
8850
8851int btrfs_prealloc_file_range_trans(struct inode *inode,
8852 struct btrfs_trans_handle *trans, int mode,
8853 u64 start, u64 num_bytes, u64 min_size,
8854 loff_t actual_len, u64 *alloc_hint)
8855{
8856 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
8857 min_size, actual_len, alloc_hint, trans);
8858}
8859
Chris Masone6dcd2d2008-07-17 12:53:50 -04008860static int btrfs_set_page_dirty(struct page *page)
8861{
Chris Masone6dcd2d2008-07-17 12:53:50 -04008862 return __set_page_dirty_nobuffers(page);
8863}
8864
Al Viro10556cb2011-06-20 19:28:19 -04008865static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05008866{
Li Zefanb83cc962010-12-20 16:04:08 +08008867 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00008868 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08008869
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00008870 if (mask & MAY_WRITE &&
8871 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
8872 if (btrfs_root_readonly(root))
8873 return -EROFS;
8874 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
8875 return -EACCES;
8876 }
Al Viro2830ba72011-06-20 19:16:29 -04008877 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05008878}
Chris Mason39279cc2007-06-12 06:35:45 -04008879
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008880static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05008881 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04008882 .lookup = btrfs_lookup,
8883 .create = btrfs_create,
8884 .unlink = btrfs_unlink,
8885 .link = btrfs_link,
8886 .mkdir = btrfs_mkdir,
8887 .rmdir = btrfs_rmdir,
8888 .rename = btrfs_rename,
8889 .symlink = btrfs_symlink,
8890 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04008891 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008892 .setxattr = btrfs_setxattr,
8893 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05008894 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008895 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05008896 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008897 .get_acl = btrfs_get_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -07008898 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008899};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008900static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008901 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05008902 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008903 .get_acl = btrfs_get_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -07008904 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008905};
Yan, Zheng76dda932009-09-21 16:00:26 -04008906
Alexey Dobriyan828c0952009-10-01 15:43:56 -07008907static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008908 .llseek = generic_file_llseek,
8909 .read = generic_read_dir,
Al Viro9cdda8d2013-05-22 16:48:09 -04008910 .iterate = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04008911 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04008912#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04008913 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04008914#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04008915 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04008916 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04008917};
8918
Chris Masond1310b22008-01-24 16:13:08 -05008919static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04008920 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05008921 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04008922 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04008923 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008924 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04008925 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05008926 .set_bit_hook = btrfs_set_bit_hook,
8927 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04008928 .merge_extent_hook = btrfs_merge_extent_hook,
8929 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04008930};
8931
Chris Mason35054392009-01-21 13:11:13 -05008932/*
8933 * btrfs doesn't support the bmap operation because swapfiles
8934 * use bmap to make a mapping of extents in the file. They assume
8935 * these extents won't change over the life of the file and they
8936 * use the bmap result to do IO directly to the drive.
8937 *
8938 * the btrfs bmap call would return logical addresses that aren't
8939 * suitable for IO and they also will change frequently as COW
8940 * operations happen. So, swapfile + btrfs == corruption.
8941 *
8942 * For now we're avoiding this by dropping bmap.
8943 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008944static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008945 .readpage = btrfs_readpage,
8946 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -04008947 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05008948 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04008949 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04008950 .invalidatepage = btrfs_invalidatepage,
8951 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008952 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02008953 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04008954};
8955
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008956static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008957 .readpage = btrfs_readpage,
8958 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04008959 .invalidatepage = btrfs_invalidatepage,
8960 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04008961};
8962
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008963static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008964 .getattr = btrfs_getattr,
8965 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008966 .setxattr = btrfs_setxattr,
8967 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05008968 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008969 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05008970 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008971 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008972 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008973 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008974};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008975static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04008976 .getattr = btrfs_getattr,
8977 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008978 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008979 .setxattr = btrfs_setxattr,
8980 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04008981 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008982 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008983 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008984 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -04008985};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008986static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008987 .readlink = generic_readlink,
8988 .follow_link = page_follow_link_light,
8989 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00008990 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -05008991 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008992 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05008993 .setxattr = btrfs_setxattr,
8994 .getxattr = btrfs_getxattr,
8995 .listxattr = btrfs_listxattr,
8996 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008997 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008998 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008999};
Yan, Zheng76dda932009-09-21 16:00:26 -04009000
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04009001const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04009002 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -04009003 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -04009004};