blob: ad961a598c99b38660a802d3afc8fd8839619ac8 [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 {
64 u64 ino;
65 struct btrfs_root *root;
66};
67
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070068static const struct inode_operations btrfs_dir_inode_operations;
69static const struct inode_operations btrfs_symlink_inode_operations;
70static const struct inode_operations btrfs_dir_ro_inode_operations;
71static const struct inode_operations btrfs_special_inode_operations;
72static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070073static const struct address_space_operations btrfs_aops;
74static const struct address_space_operations btrfs_symlink_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070075static const struct file_operations btrfs_dir_file_operations;
Chris Masond1310b22008-01-24 16:13:08 -050076static struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040077
78static struct kmem_cache *btrfs_inode_cachep;
Miao Xie8ccf6f192012-10-25 09:28:04 +000079static struct kmem_cache *btrfs_delalloc_work_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040080struct kmem_cache *btrfs_trans_handle_cachep;
81struct kmem_cache *btrfs_transaction_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040082struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050083struct kmem_cache *btrfs_free_space_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040084
85#define S_SHIFT 12
86static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
87 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
88 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
89 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
90 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
91 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
92 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
93 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
94};
95
Eric Sandeen3972f262013-01-12 02:57:22 +000096static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Josef Bacika41ad392011-01-31 15:30:16 -050097static int btrfs_truncate(struct inode *inode);
Josef Bacik5fd02042012-05-02 14:00:54 -040098static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Chris Mason771ed682008-11-06 22:02:51 -050099static noinline int cow_file_range(struct inode *inode,
100 struct page *locked_page,
101 u64 start, u64 end, int *page_started,
102 unsigned long *nr_written, int unlock);
Josef Bacik70c8a912012-10-11 16:54:30 -0400103static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
104 u64 len, u64 orig_start,
105 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -0400106 u64 orig_block_len, u64 ram_bytes,
107 int type);
Josef Bacik7b128762008-07-24 12:17:14 -0400108
Eric Sandeen48a3b632013-04-25 20:41:01 +0000109static int btrfs_dirty_inode(struct inode *inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400110
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000111static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500112 struct inode *inode, struct inode *dir,
113 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500114{
115 int err;
116
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000117 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500118 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500119 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500120 return err;
121}
122
Chris Masond352ac62008-09-29 15:18:18 -0400123/*
Chris Masonc8b97812008-10-29 14:49:59 -0400124 * this does all the hard work for inserting an inline extent into
125 * the btree. The caller should have done a btrfs_drop_extents so that
126 * no overlapping inline items exist in the btree
127 */
Chris Masond3977122009-01-05 21:25:51 -0500128static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000129 struct btrfs_path *path, int extent_inserted,
Chris Masonc8b97812008-10-29 14:49:59 -0400130 struct btrfs_root *root, struct inode *inode,
131 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000132 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400133 struct page **compressed_pages)
134{
Chris Masonc8b97812008-10-29 14:49:59 -0400135 struct extent_buffer *leaf;
136 struct page *page = NULL;
137 char *kaddr;
138 unsigned long ptr;
139 struct btrfs_file_extent_item *ei;
140 int err = 0;
141 int ret;
142 size_t cur_size = size;
Chris Masonc8b97812008-10-29 14:49:59 -0400143 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400144
Li Zefanfe3f5662011-03-28 08:30:38 +0000145 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400146 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400147
Chris Masonc8b97812008-10-29 14:49:59 -0400148 inode_add_bytes(inode, size);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000149
150 if (!extent_inserted) {
151 struct btrfs_key key;
152 size_t datasize;
153
154 key.objectid = btrfs_ino(inode);
155 key.offset = start;
156 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
157
158 datasize = btrfs_file_extent_calc_inline_size(cur_size);
159 path->leave_spinning = 1;
160 ret = btrfs_insert_empty_item(trans, root, path, &key,
161 datasize);
162 if (ret) {
163 err = ret;
164 goto fail;
165 }
Chris Masonc8b97812008-10-29 14:49:59 -0400166 }
167 leaf = path->nodes[0];
168 ei = btrfs_item_ptr(leaf, path->slots[0],
169 struct btrfs_file_extent_item);
170 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
171 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
172 btrfs_set_file_extent_encryption(leaf, ei, 0);
173 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
174 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
175 ptr = btrfs_file_extent_inline_start(ei);
176
Li Zefan261507a02010-12-17 14:21:50 +0800177 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400178 struct page *cpage;
179 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500180 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400181 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500182 cur_size = min_t(unsigned long, compressed_size,
Chris Masonc8b97812008-10-29 14:49:59 -0400183 PAGE_CACHE_SIZE);
184
Cong Wang7ac687d2011-11-25 23:14:28 +0800185 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400186 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800187 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400188
189 i++;
190 ptr += cur_size;
191 compressed_size -= cur_size;
192 }
193 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800194 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400195 } else {
196 page = find_get_page(inode->i_mapping,
197 start >> PAGE_CACHE_SHIFT);
198 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800199 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400200 offset = start & (PAGE_CACHE_SIZE - 1);
201 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800202 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400203 page_cache_release(page);
204 }
205 btrfs_mark_buffer_dirty(leaf);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000206 btrfs_release_path(path);
Chris Masonc8b97812008-10-29 14:49:59 -0400207
Yan, Zhengc2167752009-11-12 09:34:21 +0000208 /*
209 * we're an inline extent, so nobody can
210 * extend the file past i_size without locking
211 * a page we already have locked.
212 *
213 * We must do any isize and inode updates
214 * before we unlock the pages. Otherwise we
215 * could end up racing with unlink.
216 */
Chris Masonc8b97812008-10-29 14:49:59 -0400217 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100218 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000219
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100220 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400221fail:
Chris Masonc8b97812008-10-29 14:49:59 -0400222 return err;
223}
224
225
226/*
227 * conditionally insert an inline extent into the file. This
228 * does the checks required to make sure the data is small enough
229 * to fit as an inline extent.
230 */
Josef Bacik00361582013-08-14 14:02:47 -0400231static noinline int cow_file_range_inline(struct btrfs_root *root,
232 struct inode *inode, u64 start,
233 u64 end, size_t compressed_size,
234 int compress_type,
235 struct page **compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400236{
Josef Bacik00361582013-08-14 14:02:47 -0400237 struct btrfs_trans_handle *trans;
Chris Masonc8b97812008-10-29 14:49:59 -0400238 u64 isize = i_size_read(inode);
239 u64 actual_end = min(end + 1, isize);
240 u64 inline_len = actual_end - start;
Qu Wenruofda28322013-02-26 08:10:22 +0000241 u64 aligned_end = ALIGN(end, root->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400242 u64 data_len = inline_len;
243 int ret;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000244 struct btrfs_path *path;
245 int extent_inserted = 0;
246 u32 extent_item_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400247
248 if (compressed_size)
249 data_len = compressed_size;
250
251 if (start > 0 ||
Chris Mason70b99e62008-10-31 12:46:39 -0400252 actual_end >= PAGE_CACHE_SIZE ||
Chris Masonc8b97812008-10-29 14:49:59 -0400253 data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
254 (!compressed_size &&
255 (actual_end & (root->sectorsize - 1)) == 0) ||
256 end + 1 < isize ||
257 data_len > root->fs_info->max_inline) {
258 return 1;
259 }
260
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000261 path = btrfs_alloc_path();
262 if (!path)
263 return -ENOMEM;
264
Josef Bacik00361582013-08-14 14:02:47 -0400265 trans = btrfs_join_transaction(root);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000266 if (IS_ERR(trans)) {
267 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400268 return PTR_ERR(trans);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000269 }
Josef Bacik00361582013-08-14 14:02:47 -0400270 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
271
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000272 if (compressed_size && compressed_pages)
273 extent_item_size = btrfs_file_extent_calc_inline_size(
274 compressed_size);
275 else
276 extent_item_size = btrfs_file_extent_calc_inline_size(
277 inline_len);
278
279 ret = __btrfs_drop_extents(trans, root, inode, path,
280 start, aligned_end, NULL,
281 1, 1, extent_item_size, &extent_inserted);
Josef Bacik00361582013-08-14 14:02:47 -0400282 if (ret) {
283 btrfs_abort_transaction(trans, root, ret);
284 goto out;
285 }
Chris Masonc8b97812008-10-29 14:49:59 -0400286
287 if (isize > actual_end)
288 inline_len = min_t(u64, isize, actual_end);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000289 ret = insert_inline_extent(trans, path, extent_inserted,
290 root, inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400291 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000292 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400293 if (ret && ret != -ENOSPC) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100294 btrfs_abort_transaction(trans, root, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400295 goto out;
Josef Bacik2adcac12012-05-23 16:10:14 -0400296 } else if (ret == -ENOSPC) {
Josef Bacik00361582013-08-14 14:02:47 -0400297 ret = 1;
298 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100299 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400300
Josef Bacikbdc20e62013-02-28 13:23:38 -0500301 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400302 btrfs_delalloc_release_metadata(inode, end + 1 - start);
Chris Masona1ed8352009-09-11 12:27:37 -0400303 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
Josef Bacik00361582013-08-14 14:02:47 -0400304out:
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000305 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400306 btrfs_end_transaction(trans, root);
307 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400308}
309
Chris Mason771ed682008-11-06 22:02:51 -0500310struct async_extent {
311 u64 start;
312 u64 ram_size;
313 u64 compressed_size;
314 struct page **pages;
315 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800316 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500317 struct list_head list;
318};
319
320struct async_cow {
321 struct inode *inode;
322 struct btrfs_root *root;
323 struct page *locked_page;
324 u64 start;
325 u64 end;
326 struct list_head extents;
327 struct btrfs_work work;
328};
329
330static noinline int add_async_extent(struct async_cow *cow,
331 u64 start, u64 ram_size,
332 u64 compressed_size,
333 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800334 unsigned long nr_pages,
335 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500336{
337 struct async_extent *async_extent;
338
339 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100340 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500341 async_extent->start = start;
342 async_extent->ram_size = ram_size;
343 async_extent->compressed_size = compressed_size;
344 async_extent->pages = pages;
345 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800346 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500347 list_add_tail(&async_extent->list, &cow->extents);
348 return 0;
349}
350
Chris Masonc8b97812008-10-29 14:49:59 -0400351/*
Chris Mason771ed682008-11-06 22:02:51 -0500352 * we create compressed extents in two phases. The first
353 * phase compresses a range of pages that have already been
354 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400355 *
Chris Mason771ed682008-11-06 22:02:51 -0500356 * This is done inside an ordered work queue, and the compression
357 * is spread across many cpus. The actual IO submission is step
358 * two, and the ordered work queue takes care of making sure that
359 * happens in the same order things were put onto the queue by
360 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400361 *
Chris Mason771ed682008-11-06 22:02:51 -0500362 * If this code finds it can't get good compression, it puts an
363 * entry onto the work queue to write the uncompressed bytes. This
364 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300365 * are written in the same order that the flusher thread sent them
366 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400367 */
Chris Mason771ed682008-11-06 22:02:51 -0500368static noinline int compress_file_range(struct inode *inode,
369 struct page *locked_page,
370 u64 start, u64 end,
371 struct async_cow *async_cow,
372 int *num_added)
Chris 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);
867 return -EINVAL;
868 }
Chris Mason771ed682008-11-06 22:02:51 -0500869
Qu Wenruofda28322013-02-26 08:10:22 +0000870 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500871 num_bytes = max(blocksize, num_bytes);
872 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500873
Chris Mason4cb53002011-05-24 15:35:30 -0400874 /* if this is a small write inside eof, kick off defrag */
Liu Bo4cb13e52012-03-29 09:57:45 -0400875 if (num_bytes < 64 * 1024 &&
876 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Josef Bacik00361582013-08-14 14:02:47 -0400877 btrfs_add_inode_defrag(NULL, inode);
Chris Mason4cb53002011-05-24 15:35:30 -0400878
Chris Mason771ed682008-11-06 22:02:51 -0500879 if (start == 0) {
880 /* lets try to make an inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400881 ret = cow_file_range_inline(root, inode, start, end, 0, 0,
882 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500883 if (ret == 0) {
Josef Bacikc2790a22013-07-29 11:20:47 -0400884 extent_clear_unlock_delalloc(inode, start, end, NULL,
885 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400886 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400887 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
888 PAGE_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000889
Chris Mason771ed682008-11-06 22:02:51 -0500890 *nr_written = *nr_written +
891 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
892 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500893 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100894 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100895 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500896 }
897 }
Chris Masonc8b97812008-10-29 14:49:59 -0400898
899 BUG_ON(disk_num_bytes >
David Sterba6c417612011-04-13 15:41:04 +0200900 btrfs_super_total_bytes(root->fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400901
Josef Bacik4b46fce2010-05-23 11:00:55 -0400902 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400903 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400904
Chris Masond3977122009-01-05 21:25:51 -0500905 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400906 unsigned long op;
907
Josef Bacik287a0ab2010-03-19 18:07:23 +0000908 cur_alloc_size = disk_num_bytes;
Josef Bacik00361582013-08-14 14:02:47 -0400909 ret = btrfs_reserve_extent(root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500910 root->sectorsize, 0, alloc_hint,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500911 &ins, 1);
Josef Bacik00361582013-08-14 14:02:47 -0400912 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100913 goto out_unlock;
Chris Masond3977122009-01-05 21:25:51 -0500914
David Sterba172ddd62011-04-21 00:48:27 +0200915 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000916 if (!em) {
917 ret = -ENOMEM;
Liu Boace68ba2013-04-22 10:53:47 +0000918 goto out_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000919 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400920 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500921 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500922 ram_size = ins.offset;
923 em->len = ins.offset;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400924 em->mod_start = em->start;
925 em->mod_len = em->len;
Chris Masonc8b97812008-10-29 14:49:59 -0400926
Chris Masone6dcd2d2008-07-17 12:53:50 -0400927 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400928 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500929 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400930 em->ram_bytes = ram_size;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400931 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400932 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400933 em->generation = -1;
Chris Masonc8b97812008-10-29 14:49:59 -0400934
Chris Masond3977122009-01-05 21:25:51 -0500935 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400936 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400937 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400938 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400939 if (ret != -EEXIST) {
940 free_extent_map(em);
941 break;
942 }
943 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400944 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400945 }
Liu Boace68ba2013-04-22 10:53:47 +0000946 if (ret)
947 goto out_reserve;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400948
Chris Mason98d20f62008-04-14 09:46:10 -0400949 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400950 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -0500951 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +0000952 if (ret)
953 goto out_reserve;
Chris Masonc8b97812008-10-29 14:49:59 -0400954
Yan Zheng17d217f2008-12-12 10:03:38 -0500955 if (root->root_key.objectid ==
956 BTRFS_DATA_RELOC_TREE_OBJECTID) {
957 ret = btrfs_reloc_clone_csums(inode, start,
958 cur_alloc_size);
Josef Bacik00361582013-08-14 14:02:47 -0400959 if (ret)
Liu Boace68ba2013-04-22 10:53:47 +0000960 goto out_reserve;
Yan Zheng17d217f2008-12-12 10:03:38 -0500961 }
962
Chris Masond3977122009-01-05 21:25:51 -0500963 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -0400964 break;
Chris Masond3977122009-01-05 21:25:51 -0500965
Chris Masonc8b97812008-10-29 14:49:59 -0400966 /* we're not doing compressed IO, don't unlock the first
967 * page (which the caller expects to stay locked), don't
968 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -0400969 *
970 * Do set the Private2 bit so we know this page was properly
971 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -0400972 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400973 op = unlock ? PAGE_UNLOCK : 0;
974 op |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -0400975
Josef Bacikc2790a22013-07-29 11:20:47 -0400976 extent_clear_unlock_delalloc(inode, start,
977 start + ram_size - 1, locked_page,
978 EXTENT_LOCKED | EXTENT_DELALLOC,
979 op);
Chris Masonc8b97812008-10-29 14:49:59 -0400980 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -0500981 num_bytes -= cur_alloc_size;
982 alloc_hint = ins.objectid + ins.offset;
983 start += cur_alloc_size;
Chris Masonb888db2b2007-08-27 16:49:44 -0400984 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100985out:
Chris Masonbe20aa92007-12-17 20:14:01 -0500986 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +0000987
Liu Boace68ba2013-04-22 10:53:47 +0000988out_reserve:
989 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100990out_unlock:
Josef Bacikc2790a22013-07-29 11:20:47 -0400991 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Josef Bacik151a41b2013-07-29 13:22:24 -0400992 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
993 EXTENT_DELALLOC | EXTENT_DEFRAG,
994 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
995 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100996 goto out;
Chris Mason771ed682008-11-06 22:02:51 -0500997}
Chris Masonc8b97812008-10-29 14:49:59 -0400998
Chris Mason771ed682008-11-06 22:02:51 -0500999/*
1000 * work queue call back to started compression on a file and pages
1001 */
1002static noinline void async_cow_start(struct btrfs_work *work)
1003{
1004 struct async_cow *async_cow;
1005 int num_added = 0;
1006 async_cow = container_of(work, struct async_cow, work);
1007
1008 compress_file_range(async_cow->inode, async_cow->locked_page,
1009 async_cow->start, async_cow->end, async_cow,
1010 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001011 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001012 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001013 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001014 }
Chris Mason771ed682008-11-06 22:02:51 -05001015}
1016
1017/*
1018 * work queue call back to submit previously compressed pages
1019 */
1020static noinline void async_cow_submit(struct btrfs_work *work)
1021{
1022 struct async_cow *async_cow;
1023 struct btrfs_root *root;
1024 unsigned long nr_pages;
1025
1026 async_cow = container_of(work, struct async_cow, work);
1027
1028 root = async_cow->root;
1029 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
1030 PAGE_CACHE_SHIFT;
1031
Josef Bacik66657b32012-08-01 15:36:24 -04001032 if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) <
Liu Bo287082b2012-06-28 04:02:24 -06001033 5 * 1024 * 1024 &&
Chris Mason771ed682008-11-06 22:02:51 -05001034 waitqueue_active(&root->fs_info->async_submit_wait))
1035 wake_up(&root->fs_info->async_submit_wait);
1036
Chris Masond3977122009-01-05 21:25:51 -05001037 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001038 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001039}
Chris Masonc8b97812008-10-29 14:49:59 -04001040
Chris Mason771ed682008-11-06 22:02:51 -05001041static noinline void async_cow_free(struct btrfs_work *work)
1042{
1043 struct async_cow *async_cow;
1044 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001045 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001046 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001047 kfree(async_cow);
1048}
1049
1050static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1051 u64 start, u64 end, int *page_started,
1052 unsigned long *nr_written)
1053{
1054 struct async_cow *async_cow;
1055 struct btrfs_root *root = BTRFS_I(inode)->root;
1056 unsigned long nr_pages;
1057 u64 cur_end;
Liu Bo287082b2012-06-28 04:02:24 -06001058 int limit = 10 * 1024 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -05001059
Chris Masona3429ab2009-10-08 12:30:20 -04001060 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1061 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001062 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001063 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001064 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001065 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001066 async_cow->root = root;
1067 async_cow->locked_page = locked_page;
1068 async_cow->start = start;
1069
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001070 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
Chris Mason771ed682008-11-06 22:02:51 -05001071 cur_end = end;
1072 else
1073 cur_end = min(end, start + 512 * 1024 - 1);
1074
1075 async_cow->end = cur_end;
1076 INIT_LIST_HEAD(&async_cow->extents);
1077
1078 async_cow->work.func = async_cow_start;
1079 async_cow->work.ordered_func = async_cow_submit;
1080 async_cow->work.ordered_free = async_cow_free;
1081 async_cow->work.flags = 0;
1082
Chris Mason771ed682008-11-06 22:02:51 -05001083 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
1084 PAGE_CACHE_SHIFT;
1085 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
1086
1087 btrfs_queue_worker(&root->fs_info->delalloc_workers,
1088 &async_cow->work);
1089
1090 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
1091 wait_event(root->fs_info->async_submit_wait,
1092 (atomic_read(&root->fs_info->async_delalloc_pages) <
1093 limit));
1094 }
1095
Chris Masond3977122009-01-05 21:25:51 -05001096 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -05001097 atomic_read(&root->fs_info->async_delalloc_pages)) {
1098 wait_event(root->fs_info->async_submit_wait,
1099 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1100 0));
1101 }
1102
1103 *nr_written += nr_pages;
1104 start = cur_end + 1;
1105 }
1106 *page_started = 1;
1107 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001108}
1109
Chris Masond3977122009-01-05 21:25:51 -05001110static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001111 u64 bytenr, u64 num_bytes)
1112{
1113 int ret;
1114 struct btrfs_ordered_sum *sums;
1115 LIST_HEAD(list);
1116
Yan Zheng07d400a2009-01-06 11:42:00 -05001117 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001118 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001119 if (ret == 0 && list_empty(&list))
1120 return 0;
1121
1122 while (!list_empty(&list)) {
1123 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1124 list_del(&sums->list);
1125 kfree(sums);
1126 }
1127 return 1;
1128}
1129
Chris Masond352ac62008-09-29 15:18:18 -04001130/*
1131 * when nowcow writeback call back. This checks for snapshots or COW copies
1132 * of the extents that exist in the file, and COWs the file as required.
1133 *
1134 * If no cow copies or snapshots exist, we write directly to the existing
1135 * blocks on disk
1136 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001137static noinline int run_delalloc_nocow(struct inode *inode,
1138 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001139 u64 start, u64 end, int *page_started, int force,
1140 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001141{
Chris Masonbe20aa92007-12-17 20:14:01 -05001142 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001143 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001144 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001145 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001146 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001147 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001148 u64 cow_start;
1149 u64 cur_offset;
1150 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001151 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001152 u64 disk_bytenr;
1153 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001154 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001155 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001156 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001157 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001158 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001159 int nocow;
1160 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001161 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001162 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001163
1164 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001165 if (!path) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001166 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1167 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001168 EXTENT_DO_ACCOUNTING |
1169 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001170 PAGE_CLEAR_DIRTY |
1171 PAGE_SET_WRITEBACK |
1172 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001173 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001174 }
Li Zefan82d59022011-04-20 10:33:24 +08001175
Liu Bo83eea1f2012-07-10 05:28:39 -06001176 nolock = btrfs_is_free_space_inode(inode);
Li Zefan82d59022011-04-20 10:33:24 +08001177
1178 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001179 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001180 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001181 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001182
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001183 if (IS_ERR(trans)) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001184 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1185 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001186 EXTENT_DO_ACCOUNTING |
1187 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001188 PAGE_CLEAR_DIRTY |
1189 PAGE_SET_WRITEBACK |
1190 PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001191 btrfs_free_path(path);
1192 return PTR_ERR(trans);
1193 }
1194
Josef Bacik74b21072011-04-13 12:02:53 -04001195 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001196
Yan Zheng80ff3852008-10-30 14:20:02 -04001197 cow_start = (u64)-1;
1198 cur_offset = start;
1199 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001200 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001201 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001202 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001203 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001204 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1205 leaf = path->nodes[0];
1206 btrfs_item_key_to_cpu(leaf, &found_key,
1207 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001208 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001209 found_key.type == BTRFS_EXTENT_DATA_KEY)
1210 path->slots[0]--;
1211 }
1212 check_prev = 0;
1213next_slot:
1214 leaf = path->nodes[0];
1215 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1216 ret = btrfs_next_leaf(root, path);
Josef Bacikd788a342013-10-25 16:55:08 -04001217 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001218 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001219 if (ret > 0)
1220 break;
1221 leaf = path->nodes[0];
1222 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001223
Yan Zheng80ff3852008-10-30 14:20:02 -04001224 nocow = 0;
1225 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001226 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001227 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001228
Li Zefan33345d012011-04-20 10:31:50 +08001229 if (found_key.objectid > ino ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001230 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1231 found_key.offset > end)
1232 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001233
Yan Zheng80ff3852008-10-30 14:20:02 -04001234 if (found_key.offset > cur_offset) {
1235 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001236 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001237 goto out_check;
1238 }
Chris Masonc31f8832008-01-08 15:46:31 -05001239
Yan Zheng80ff3852008-10-30 14:20:02 -04001240 fi = btrfs_item_ptr(leaf, path->slots[0],
1241 struct btrfs_file_extent_item);
1242 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001243
Josef Bacikcc95bef2013-04-04 14:31:27 -04001244 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001245 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1246 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001247 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001248 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001249 extent_end = found_key.offset +
1250 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001251 disk_num_bytes =
1252 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001253 if (extent_end <= start) {
1254 path->slots[0]++;
1255 goto next_slot;
1256 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001257 if (disk_bytenr == 0)
1258 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001259 if (btrfs_file_extent_compression(leaf, fi) ||
1260 btrfs_file_extent_encryption(leaf, fi) ||
1261 btrfs_file_extent_other_encoding(leaf, fi))
1262 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001263 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1264 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001265 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001266 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001267 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001268 found_key.offset -
1269 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001270 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001271 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001272 disk_bytenr += cur_offset - found_key.offset;
1273 num_bytes = min(end + 1, extent_end) - cur_offset;
1274 /*
1275 * force cow if csum exists in the range.
1276 * this ensure that csum for a given extent are
1277 * either valid or do not exist.
1278 */
1279 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1280 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001281 nocow = 1;
1282 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1283 extent_end = found_key.offset +
Chris Mason514ac8a2014-01-03 21:07:00 -08001284 btrfs_file_extent_inline_len(leaf,
1285 path->slots[0], fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001286 extent_end = ALIGN(extent_end, root->sectorsize);
1287 } else {
1288 BUG_ON(1);
1289 }
1290out_check:
1291 if (extent_end <= start) {
1292 path->slots[0]++;
1293 goto next_slot;
1294 }
1295 if (!nocow) {
1296 if (cow_start == (u64)-1)
1297 cow_start = cur_offset;
1298 cur_offset = extent_end;
1299 if (cur_offset > end)
1300 break;
1301 path->slots[0]++;
1302 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001303 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001304
David Sterbab3b4aa72011-04-21 01:20:15 +02001305 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001306 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001307 ret = cow_file_range(inode, locked_page,
1308 cow_start, found_key.offset - 1,
1309 page_started, nr_written, 1);
Josef Bacikd788a342013-10-25 16:55:08 -04001310 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001311 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001312 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001313 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001314
Yan Zhengd899e052008-10-30 14:25:28 -04001315 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1316 struct extent_map *em;
1317 struct extent_map_tree *em_tree;
1318 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001319 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001320 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001321 em->start = cur_offset;
Josef Bacik70c8a912012-10-11 16:54:30 -04001322 em->orig_start = found_key.offset - extent_offset;
Yan Zhengd899e052008-10-30 14:25:28 -04001323 em->len = num_bytes;
1324 em->block_len = num_bytes;
1325 em->block_start = disk_bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05001326 em->orig_block_len = disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001327 em->ram_bytes = ram_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04001328 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001329 em->mod_start = em->start;
1330 em->mod_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04001331 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacikb11e2342012-12-03 10:58:15 -05001332 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001333 em->generation = -1;
Yan Zhengd899e052008-10-30 14:25:28 -04001334 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001335 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001336 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001337 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001338 if (ret != -EEXIST) {
1339 free_extent_map(em);
1340 break;
1341 }
1342 btrfs_drop_extent_cache(inode, em->start,
1343 em->start + em->len - 1, 0);
1344 }
1345 type = BTRFS_ORDERED_PREALLOC;
1346 } else {
1347 type = BTRFS_ORDERED_NOCOW;
1348 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001349
1350 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001351 num_bytes, num_bytes, type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001352 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001353
Yan, Zhengefa56462010-05-16 10:49:59 -04001354 if (root->root_key.objectid ==
1355 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1356 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1357 num_bytes);
Josef Bacikd788a342013-10-25 16:55:08 -04001358 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001359 goto error;
Yan, Zhengefa56462010-05-16 10:49:59 -04001360 }
1361
Josef Bacikc2790a22013-07-29 11:20:47 -04001362 extent_clear_unlock_delalloc(inode, cur_offset,
1363 cur_offset + num_bytes - 1,
1364 locked_page, EXTENT_LOCKED |
1365 EXTENT_DELALLOC, PAGE_UNLOCK |
1366 PAGE_SET_PRIVATE2);
Yan Zheng80ff3852008-10-30 14:20:02 -04001367 cur_offset = extent_end;
1368 if (cur_offset > end)
1369 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001370 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001371 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001372
Josef Bacik17ca04a2012-05-31 15:58:55 -04001373 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001374 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001375 cur_offset = end;
1376 }
1377
Yan Zheng80ff3852008-10-30 14:20:02 -04001378 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001379 ret = cow_file_range(inode, locked_page, cow_start, end,
1380 page_started, nr_written, 1);
Josef Bacikd788a342013-10-25 16:55:08 -04001381 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001382 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001383 }
1384
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001385error:
Miao Xiea698d0752012-09-20 01:51:59 -06001386 err = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001387 if (!ret)
1388 ret = err;
1389
Josef Bacik17ca04a2012-05-31 15:58:55 -04001390 if (ret && cur_offset < end)
Josef Bacikc2790a22013-07-29 11:20:47 -04001391 extent_clear_unlock_delalloc(inode, cur_offset, end,
1392 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001393 EXTENT_DELALLOC | EXTENT_DEFRAG |
1394 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1395 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001396 PAGE_SET_WRITEBACK |
1397 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001398 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001399 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001400}
1401
Chris Masond352ac62008-09-29 15:18:18 -04001402/*
1403 * extent_io.c call back to do delayed allocation processing
1404 */
Chris Masonc8b97812008-10-29 14:49:59 -04001405static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001406 u64 start, u64 end, int *page_started,
1407 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001408{
Chris Masonbe20aa92007-12-17 20:14:01 -05001409 int ret;
Chris Mason7f366cf2009-03-12 20:12:45 -04001410 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona2135012008-06-25 16:01:30 -04001411
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001412 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) {
Chris Masonc8b97812008-10-29 14:49:59 -04001413 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001414 page_started, 1, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001415 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001416 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001417 page_started, 0, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001418 } else if (!btrfs_test_opt(root, COMPRESS) &&
1419 !(BTRFS_I(inode)->force_compress) &&
1420 !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS)) {
Chris Mason7f366cf2009-03-12 20:12:45 -04001421 ret = cow_file_range(inode, locked_page, start, end,
1422 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001423 } else {
1424 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1425 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001426 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001427 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001428 }
Chris Masonb888db2b2007-08-27 16:49:44 -04001429 return ret;
1430}
1431
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001432static void btrfs_split_extent_hook(struct inode *inode,
1433 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001434{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001435 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001436 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001437 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001438
Josef Bacik9e0baf62011-07-15 15:16:44 +00001439 spin_lock(&BTRFS_I(inode)->lock);
1440 BTRFS_I(inode)->outstanding_extents++;
1441 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001442}
1443
1444/*
1445 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1446 * extents so we can keep track of new extents that are just merged onto old
1447 * extents, such as when we are doing sequential writes, so we can properly
1448 * account for the metadata space we'll need.
1449 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001450static void btrfs_merge_extent_hook(struct inode *inode,
1451 struct extent_state *new,
1452 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001453{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001454 /* not delalloc, ignore it */
1455 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001456 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001457
Josef Bacik9e0baf62011-07-15 15:16:44 +00001458 spin_lock(&BTRFS_I(inode)->lock);
1459 BTRFS_I(inode)->outstanding_extents--;
1460 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001461}
1462
Miao Xieeb73c1b2013-05-15 07:48:22 +00001463static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1464 struct inode *inode)
1465{
1466 spin_lock(&root->delalloc_lock);
1467 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1468 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1469 &root->delalloc_inodes);
1470 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1471 &BTRFS_I(inode)->runtime_flags);
1472 root->nr_delalloc_inodes++;
1473 if (root->nr_delalloc_inodes == 1) {
1474 spin_lock(&root->fs_info->delalloc_root_lock);
1475 BUG_ON(!list_empty(&root->delalloc_root));
1476 list_add_tail(&root->delalloc_root,
1477 &root->fs_info->delalloc_roots);
1478 spin_unlock(&root->fs_info->delalloc_root_lock);
1479 }
1480 }
1481 spin_unlock(&root->delalloc_lock);
1482}
1483
1484static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1485 struct inode *inode)
1486{
1487 spin_lock(&root->delalloc_lock);
1488 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1489 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1490 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1491 &BTRFS_I(inode)->runtime_flags);
1492 root->nr_delalloc_inodes--;
1493 if (!root->nr_delalloc_inodes) {
1494 spin_lock(&root->fs_info->delalloc_root_lock);
1495 BUG_ON(list_empty(&root->delalloc_root));
1496 list_del_init(&root->delalloc_root);
1497 spin_unlock(&root->fs_info->delalloc_root_lock);
1498 }
1499 }
1500 spin_unlock(&root->delalloc_lock);
1501}
1502
Chris Masond352ac62008-09-29 15:18:18 -04001503/*
1504 * extent_io.c set_bit_hook, used to track delayed allocation
1505 * bytes in this file, and to maintain the list of inodes that
1506 * have pending delalloc work to be done.
1507 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001508static void btrfs_set_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001509 struct extent_state *state, unsigned long *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001510{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001511
Chris Mason75eff682008-12-15 15:54:40 -05001512 /*
1513 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001514 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001515 * bit, which is only set or cleared with irqs on
1516 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001517 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001518 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001519 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001520 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001521
Josef Bacik9e0baf62011-07-15 15:16:44 +00001522 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001523 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001524 } else {
1525 spin_lock(&BTRFS_I(inode)->lock);
1526 BTRFS_I(inode)->outstanding_extents++;
1527 spin_unlock(&BTRFS_I(inode)->lock);
1528 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001529
Miao Xie963d6782013-01-29 10:10:51 +00001530 __percpu_counter_add(&root->fs_info->delalloc_bytes, len,
1531 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001532 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001533 BTRFS_I(inode)->delalloc_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001534 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001535 &BTRFS_I(inode)->runtime_flags))
1536 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001537 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001538 }
Chris Mason291d6732008-01-29 15:55:23 -05001539}
1540
Chris Masond352ac62008-09-29 15:18:18 -04001541/*
1542 * extent_io.c clear_bit_hook, see set_bit_hook for why
1543 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001544static void btrfs_clear_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001545 struct extent_state *state,
1546 unsigned long *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001547{
Chris Mason75eff682008-12-15 15:54:40 -05001548 /*
1549 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001550 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001551 * bit, which is only set or cleared with irqs on
1552 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001553 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001554 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001555 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001556 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001557
Josef Bacik9e0baf62011-07-15 15:16:44 +00001558 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001559 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001560 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1561 spin_lock(&BTRFS_I(inode)->lock);
1562 BTRFS_I(inode)->outstanding_extents--;
1563 spin_unlock(&BTRFS_I(inode)->lock);
1564 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001565
Josef Bacikb6d08f02013-09-27 14:57:43 -04001566 /*
1567 * We don't reserve metadata space for space cache inodes so we
1568 * don't need to call dellalloc_release_metadata if there is an
1569 * error.
1570 */
1571 if (*bits & EXTENT_DO_ACCOUNTING &&
1572 root != root->fs_info->tree_root)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001573 btrfs_delalloc_release_metadata(inode, len);
1574
Josef Bacik0cb59c92010-07-02 12:14:14 -04001575 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Josef Bacik7ee9e442013-06-21 16:37:03 -04001576 && do_list && !(state->state & EXTENT_NORESERVE))
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001577 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001578
Miao Xie963d6782013-01-29 10:10:51 +00001579 __percpu_counter_add(&root->fs_info->delalloc_bytes, -len,
1580 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001581 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001582 BTRFS_I(inode)->delalloc_bytes -= len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001583 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001584 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001585 &BTRFS_I(inode)->runtime_flags))
1586 btrfs_del_delalloc_inode(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001587 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001588 }
Chris Mason291d6732008-01-29 15:55:23 -05001589}
1590
Chris Masond352ac62008-09-29 15:18:18 -04001591/*
1592 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1593 * we don't create bios that span stripes or chunks
1594 */
David Woodhouse64a16702009-07-15 23:29:37 +01001595int btrfs_merge_bio_hook(int rw, struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001596 size_t size, struct bio *bio,
1597 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001598{
1599 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Chris Masona62b9402008-10-03 16:31:08 -04001600 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001601 u64 length = 0;
1602 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001603 int ret;
1604
Chris Mason771ed682008-11-06 22:02:51 -05001605 if (bio_flags & EXTENT_BIO_COMPRESSED)
1606 return 0;
1607
Chris Masonf2d8d742008-04-21 10:03:05 -04001608 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001609 map_length = length;
David Woodhouse64a16702009-07-15 23:29:37 +01001610 ret = btrfs_map_block(root->fs_info, rw, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001611 &map_length, NULL, 0);
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001612 /* Will always return 0 with map_multi == NULL */
Jeff Mahoney3444a972011-10-03 23:23:13 -04001613 BUG_ON(ret < 0);
Chris Masond3977122009-01-05 21:25:51 -05001614 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001615 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001616 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001617}
1618
Chris Masond352ac62008-09-29 15:18:18 -04001619/*
1620 * in order to insert checksums into the metadata in large chunks,
1621 * we wait until bio submission time. All the pages in the bio are
1622 * checksummed and sums are attached onto the ordered extent record.
1623 *
1624 * At IO completion time the cums attached on the ordered extent record
1625 * are inserted into the btree
1626 */
Chris Masond3977122009-01-05 21:25:51 -05001627static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1628 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001629 unsigned long bio_flags,
1630 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001631{
Chris Mason065631f2008-02-20 12:07:25 -05001632 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001633 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001634
Chris Masond20f7042008-12-08 16:58:54 -05001635 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001636 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001637 return 0;
1638}
Chris Masone0156402008-04-16 11:15:20 -04001639
Chris Mason4a69a412008-11-06 22:03:00 -05001640/*
1641 * in order to insert checksums into the metadata in large chunks,
1642 * we wait until bio submission time. All the pages in the bio are
1643 * checksummed and sums are attached onto the ordered extent record.
1644 *
1645 * At IO completion time the cums attached on the ordered extent record
1646 * are inserted into the btree
1647 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001648static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001649 int mirror_num, unsigned long bio_flags,
1650 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001651{
1652 struct btrfs_root *root = BTRFS_I(inode)->root;
Stefan Behrens61891922012-11-05 18:51:52 +01001653 int ret;
1654
1655 ret = btrfs_map_bio(root, rw, bio, mirror_num, 1);
1656 if (ret)
1657 bio_endio(bio, ret);
1658 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001659}
1660
Chris Masond352ac62008-09-29 15:18:18 -04001661/*
Chris Masoncad321a2008-12-17 14:51:42 -05001662 * extent_io.c submission hook. This does the right thing for csum calculation
1663 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001664 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001665static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001666 int mirror_num, unsigned long bio_flags,
1667 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001668{
1669 struct btrfs_root *root = BTRFS_I(inode)->root;
1670 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001671 int skip_sum;
Jeff Mahoney04173412011-10-03 23:23:12 -04001672 int metadata = 0;
Josef Bacikb812ce22012-11-16 13:56:32 -05001673 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001674
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001675 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001676
Liu Bo83eea1f2012-07-10 05:28:39 -06001677 if (btrfs_is_free_space_inode(inode))
Jeff Mahoney04173412011-10-03 23:23:12 -04001678 metadata = 2;
1679
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001680 if (!(rw & REQ_WRITE)) {
Josef Bacik5fd02042012-05-02 14:00:54 -04001681 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1682 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001683 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001684
Chris Masond20f7042008-12-08 16:58:54 -05001685 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001686 ret = btrfs_submit_compressed_read(inode, bio,
1687 mirror_num,
1688 bio_flags);
1689 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001690 } else if (!skip_sum) {
1691 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1692 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001693 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001694 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001695 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001696 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001697 /* csum items have already been cloned */
1698 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1699 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001700 /* we're doing a write, do the async checksumming */
Stefan Behrens61891922012-11-05 18:51:52 +01001701 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001702 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001703 bio_flags, bio_offset,
1704 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001705 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001706 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001707 } else if (!skip_sum) {
1708 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1709 if (ret)
1710 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001711 }
1712
Chris Mason0b86a832008-03-24 15:01:56 -04001713mapit:
Stefan Behrens61891922012-11-05 18:51:52 +01001714 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
1715
1716out:
1717 if (ret < 0)
1718 bio_endio(bio, ret);
1719 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001720}
Chris Mason6885f302008-02-20 16:11:05 -05001721
Chris Masond352ac62008-09-29 15:18:18 -04001722/*
1723 * given a list of ordered sums record them in the inode. This happens
1724 * at IO completion time based on sums calculated at bio submission time.
1725 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001726static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001727 struct inode *inode, u64 file_offset,
1728 struct list_head *list)
1729{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001730 struct btrfs_ordered_sum *sum;
1731
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001732 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00001733 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05001734 btrfs_csum_file_blocks(trans,
1735 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00001736 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001737 }
1738 return 0;
1739}
1740
Josef Bacik2ac55d42010-02-03 19:33:23 +00001741int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1742 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001743{
Julia Lawall6c1500f2012-11-03 20:30:18 +00001744 WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04001745 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001746 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001747}
1748
Chris Masond352ac62008-09-29 15:18:18 -04001749/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001750struct btrfs_writepage_fixup {
1751 struct page *page;
1752 struct btrfs_work work;
1753};
1754
Christoph Hellwigb2950862008-12-02 09:54:17 -05001755static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001756{
1757 struct btrfs_writepage_fixup *fixup;
1758 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001759 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001760 struct page *page;
1761 struct inode *inode;
1762 u64 page_start;
1763 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001764 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001765
1766 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1767 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001768again:
Chris Mason247e7432008-07-17 12:53:51 -04001769 lock_page(page);
1770 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1771 ClearPageChecked(page);
1772 goto out_page;
1773 }
1774
1775 inode = page->mapping->host;
1776 page_start = page_offset(page);
1777 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1778
Josef Bacik2ac55d42010-02-03 19:33:23 +00001779 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001780 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001781
1782 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001783 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001784 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001785
1786 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1787 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001788 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1789 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001790 unlock_page(page);
1791 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001792 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04001793 goto again;
1794 }
Chris Mason247e7432008-07-17 12:53:51 -04001795
Jeff Mahoney87826df2012-02-15 16:23:57 +01001796 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
1797 if (ret) {
1798 mapping_set_error(page->mapping, ret);
1799 end_extent_writepage(page, ret, page_start, page_end);
1800 ClearPageChecked(page);
1801 goto out;
1802 }
1803
Josef Bacik2ac55d42010-02-03 19:33:23 +00001804 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001805 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001806 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04001807out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001808 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1809 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001810out_page:
1811 unlock_page(page);
1812 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08001813 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04001814}
1815
1816/*
1817 * There are a few paths in the higher layers of the kernel that directly
1818 * set the page dirty bit without asking the filesystem if it is a
1819 * good idea. This causes problems because we want to make sure COW
1820 * properly happens and the data=ordered rules are followed.
1821 *
Chris Masonc8b97812008-10-29 14:49:59 -04001822 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001823 * hasn't been properly setup for IO. We kick off an async process
1824 * to fix it up. The async helper will wait for ordered extents, set
1825 * the delalloc bit and make it safe to write the page.
1826 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001827static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001828{
1829 struct inode *inode = page->mapping->host;
1830 struct btrfs_writepage_fixup *fixup;
1831 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001832
Chris Mason8b62b722009-09-02 16:53:46 -04001833 /* this page is properly in the ordered list */
1834 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001835 return 0;
1836
1837 if (PageChecked(page))
1838 return -EAGAIN;
1839
1840 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1841 if (!fixup)
1842 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001843
Chris Mason247e7432008-07-17 12:53:51 -04001844 SetPageChecked(page);
1845 page_cache_get(page);
1846 fixup->work.func = btrfs_writepage_fixup_worker;
1847 fixup->page = page;
1848 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001849 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04001850}
1851
Yan Zhengd899e052008-10-30 14:25:28 -04001852static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1853 struct inode *inode, u64 file_pos,
1854 u64 disk_bytenr, u64 disk_num_bytes,
1855 u64 num_bytes, u64 ram_bytes,
1856 u8 compression, u8 encryption,
1857 u16 other_encoding, int extent_type)
1858{
1859 struct btrfs_root *root = BTRFS_I(inode)->root;
1860 struct btrfs_file_extent_item *fi;
1861 struct btrfs_path *path;
1862 struct extent_buffer *leaf;
1863 struct btrfs_key ins;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00001864 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04001865 int ret;
1866
1867 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07001868 if (!path)
1869 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04001870
Chris Masona1ed8352009-09-11 12:27:37 -04001871 /*
1872 * we may be replacing one extent in the tree with another.
1873 * The new extent is pinned in the extent map, and we don't want
1874 * to drop it from the cache until it is completely in the btree.
1875 *
1876 * So, tell btrfs_drop_extents to leave this extent in the cache.
1877 * the caller is expected to unpin it and allow it to be merged
1878 * with the others.
1879 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00001880 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
1881 file_pos + num_bytes, NULL, 0,
1882 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001883 if (ret)
1884 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001885
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00001886 if (!extent_inserted) {
1887 ins.objectid = btrfs_ino(inode);
1888 ins.offset = file_pos;
1889 ins.type = BTRFS_EXTENT_DATA_KEY;
1890
1891 path->leave_spinning = 1;
1892 ret = btrfs_insert_empty_item(trans, root, path, &ins,
1893 sizeof(*fi));
1894 if (ret)
1895 goto out;
1896 }
Yan Zhengd899e052008-10-30 14:25:28 -04001897 leaf = path->nodes[0];
1898 fi = btrfs_item_ptr(leaf, path->slots[0],
1899 struct btrfs_file_extent_item);
1900 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1901 btrfs_set_file_extent_type(leaf, fi, extent_type);
1902 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1903 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1904 btrfs_set_file_extent_offset(leaf, fi, 0);
1905 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1906 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1907 btrfs_set_file_extent_compression(leaf, fi, compression);
1908 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1909 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04001910
Yan Zhengd899e052008-10-30 14:25:28 -04001911 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04001912 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04001913
1914 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04001915
1916 ins.objectid = disk_bytenr;
1917 ins.offset = disk_num_bytes;
1918 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001919 ret = btrfs_alloc_reserved_file_extent(trans, root,
1920 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08001921 btrfs_ino(inode), file_pos, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001922out:
Yan Zhengd899e052008-10-30 14:25:28 -04001923 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04001924
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001925 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04001926}
1927
Liu Bo38c227d2013-01-29 03:18:40 +00001928/* snapshot-aware defrag */
1929struct sa_defrag_extent_backref {
1930 struct rb_node node;
1931 struct old_sa_defrag_extent *old;
1932 u64 root_id;
1933 u64 inum;
1934 u64 file_pos;
1935 u64 extent_offset;
1936 u64 num_bytes;
1937 u64 generation;
1938};
1939
1940struct old_sa_defrag_extent {
1941 struct list_head list;
1942 struct new_sa_defrag_extent *new;
1943
1944 u64 extent_offset;
1945 u64 bytenr;
1946 u64 offset;
1947 u64 len;
1948 int count;
1949};
1950
1951struct new_sa_defrag_extent {
1952 struct rb_root root;
1953 struct list_head head;
1954 struct btrfs_path *path;
1955 struct inode *inode;
1956 u64 file_pos;
1957 u64 len;
1958 u64 bytenr;
1959 u64 disk_len;
1960 u8 compress_type;
1961};
1962
1963static int backref_comp(struct sa_defrag_extent_backref *b1,
1964 struct sa_defrag_extent_backref *b2)
1965{
1966 if (b1->root_id < b2->root_id)
1967 return -1;
1968 else if (b1->root_id > b2->root_id)
1969 return 1;
1970
1971 if (b1->inum < b2->inum)
1972 return -1;
1973 else if (b1->inum > b2->inum)
1974 return 1;
1975
1976 if (b1->file_pos < b2->file_pos)
1977 return -1;
1978 else if (b1->file_pos > b2->file_pos)
1979 return 1;
1980
1981 /*
1982 * [------------------------------] ===> (a range of space)
1983 * |<--->| |<---->| =============> (fs/file tree A)
1984 * |<---------------------------->| ===> (fs/file tree B)
1985 *
1986 * A range of space can refer to two file extents in one tree while
1987 * refer to only one file extent in another tree.
1988 *
1989 * So we may process a disk offset more than one time(two extents in A)
1990 * and locate at the same extent(one extent in B), then insert two same
1991 * backrefs(both refer to the extent in B).
1992 */
1993 return 0;
1994}
1995
1996static void backref_insert(struct rb_root *root,
1997 struct sa_defrag_extent_backref *backref)
1998{
1999 struct rb_node **p = &root->rb_node;
2000 struct rb_node *parent = NULL;
2001 struct sa_defrag_extent_backref *entry;
2002 int ret;
2003
2004 while (*p) {
2005 parent = *p;
2006 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2007
2008 ret = backref_comp(backref, entry);
2009 if (ret < 0)
2010 p = &(*p)->rb_left;
2011 else
2012 p = &(*p)->rb_right;
2013 }
2014
2015 rb_link_node(&backref->node, parent, p);
2016 rb_insert_color(&backref->node, root);
2017}
2018
2019/*
2020 * Note the backref might has changed, and in this case we just return 0.
2021 */
2022static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2023 void *ctx)
2024{
2025 struct btrfs_file_extent_item *extent;
2026 struct btrfs_fs_info *fs_info;
2027 struct old_sa_defrag_extent *old = ctx;
2028 struct new_sa_defrag_extent *new = old->new;
2029 struct btrfs_path *path = new->path;
2030 struct btrfs_key key;
2031 struct btrfs_root *root;
2032 struct sa_defrag_extent_backref *backref;
2033 struct extent_buffer *leaf;
2034 struct inode *inode = new->inode;
2035 int slot;
2036 int ret;
2037 u64 extent_offset;
2038 u64 num_bytes;
2039
2040 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
2041 inum == btrfs_ino(inode))
2042 return 0;
2043
2044 key.objectid = root_id;
2045 key.type = BTRFS_ROOT_ITEM_KEY;
2046 key.offset = (u64)-1;
2047
2048 fs_info = BTRFS_I(inode)->root->fs_info;
2049 root = btrfs_read_fs_root_no_name(fs_info, &key);
2050 if (IS_ERR(root)) {
2051 if (PTR_ERR(root) == -ENOENT)
2052 return 0;
2053 WARN_ON(1);
2054 pr_debug("inum=%llu, offset=%llu, root_id=%llu\n",
2055 inum, offset, root_id);
2056 return PTR_ERR(root);
2057 }
2058
2059 key.objectid = inum;
2060 key.type = BTRFS_EXTENT_DATA_KEY;
2061 if (offset > (u64)-1 << 32)
2062 key.offset = 0;
2063 else
2064 key.offset = offset;
2065
2066 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302067 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002068 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002069 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002070
2071 while (1) {
2072 cond_resched();
2073
2074 leaf = path->nodes[0];
2075 slot = path->slots[0];
2076
2077 if (slot >= btrfs_header_nritems(leaf)) {
2078 ret = btrfs_next_leaf(root, path);
2079 if (ret < 0) {
2080 goto out;
2081 } else if (ret > 0) {
2082 ret = 0;
2083 goto out;
2084 }
2085 continue;
2086 }
2087
2088 path->slots[0]++;
2089
2090 btrfs_item_key_to_cpu(leaf, &key, slot);
2091
2092 if (key.objectid > inum)
2093 goto out;
2094
2095 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2096 continue;
2097
2098 extent = btrfs_item_ptr(leaf, slot,
2099 struct btrfs_file_extent_item);
2100
2101 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2102 continue;
2103
Liu Boe68afa42013-07-01 22:13:26 +08002104 /*
2105 * 'offset' refers to the exact key.offset,
2106 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2107 * (key.offset - extent_offset).
2108 */
2109 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002110 continue;
2111
Liu Boe68afa42013-07-01 22:13:26 +08002112 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002113 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002114
Liu Bo38c227d2013-01-29 03:18:40 +00002115 if (extent_offset >= old->extent_offset + old->offset +
2116 old->len || extent_offset + num_bytes <=
2117 old->extent_offset + old->offset)
2118 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002119 break;
2120 }
2121
2122 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2123 if (!backref) {
2124 ret = -ENOENT;
2125 goto out;
2126 }
2127
2128 backref->root_id = root_id;
2129 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002130 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002131 backref->num_bytes = num_bytes;
2132 backref->extent_offset = extent_offset;
2133 backref->generation = btrfs_file_extent_generation(leaf, extent);
2134 backref->old = old;
2135 backref_insert(&new->root, backref);
2136 old->count++;
2137out:
2138 btrfs_release_path(path);
2139 WARN_ON(ret);
2140 return ret;
2141}
2142
2143static noinline bool record_extent_backrefs(struct btrfs_path *path,
2144 struct new_sa_defrag_extent *new)
2145{
2146 struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info;
2147 struct old_sa_defrag_extent *old, *tmp;
2148 int ret;
2149
2150 new->path = path;
2151
2152 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002153 ret = iterate_inodes_from_logical(old->bytenr +
2154 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002155 path, record_one_backref,
2156 old);
Josef Bacik4724b102013-11-05 11:11:40 -05002157 if (ret < 0 && ret != -ENOENT)
2158 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002159
2160 /* no backref to be processed for this extent */
2161 if (!old->count) {
2162 list_del(&old->list);
2163 kfree(old);
2164 }
2165 }
2166
2167 if (list_empty(&new->head))
2168 return false;
2169
2170 return true;
2171}
2172
2173static int relink_is_mergable(struct extent_buffer *leaf,
2174 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002175 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002176{
Liu Bo116e0022013-08-02 16:30:40 +08002177 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002178 return 0;
2179
2180 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2181 return 0;
2182
Liu Bo116e0022013-08-02 16:30:40 +08002183 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2184 return 0;
2185
2186 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002187 btrfs_file_extent_other_encoding(leaf, fi))
2188 return 0;
2189
2190 return 1;
2191}
2192
2193/*
2194 * Note the backref might has changed, and in this case we just return 0.
2195 */
2196static noinline int relink_extent_backref(struct btrfs_path *path,
2197 struct sa_defrag_extent_backref *prev,
2198 struct sa_defrag_extent_backref *backref)
2199{
2200 struct btrfs_file_extent_item *extent;
2201 struct btrfs_file_extent_item *item;
2202 struct btrfs_ordered_extent *ordered;
2203 struct btrfs_trans_handle *trans;
2204 struct btrfs_fs_info *fs_info;
2205 struct btrfs_root *root;
2206 struct btrfs_key key;
2207 struct extent_buffer *leaf;
2208 struct old_sa_defrag_extent *old = backref->old;
2209 struct new_sa_defrag_extent *new = old->new;
2210 struct inode *src_inode = new->inode;
2211 struct inode *inode;
2212 struct extent_state *cached = NULL;
2213 int ret = 0;
2214 u64 start;
2215 u64 len;
2216 u64 lock_start;
2217 u64 lock_end;
2218 bool merge = false;
2219 int index;
2220
2221 if (prev && prev->root_id == backref->root_id &&
2222 prev->inum == backref->inum &&
2223 prev->file_pos + prev->num_bytes == backref->file_pos)
2224 merge = true;
2225
2226 /* step 1: get root */
2227 key.objectid = backref->root_id;
2228 key.type = BTRFS_ROOT_ITEM_KEY;
2229 key.offset = (u64)-1;
2230
2231 fs_info = BTRFS_I(src_inode)->root->fs_info;
2232 index = srcu_read_lock(&fs_info->subvol_srcu);
2233
2234 root = btrfs_read_fs_root_no_name(fs_info, &key);
2235 if (IS_ERR(root)) {
2236 srcu_read_unlock(&fs_info->subvol_srcu, index);
2237 if (PTR_ERR(root) == -ENOENT)
2238 return 0;
2239 return PTR_ERR(root);
2240 }
Liu Bo38c227d2013-01-29 03:18:40 +00002241
2242 /* step 2: get inode */
2243 key.objectid = backref->inum;
2244 key.type = BTRFS_INODE_ITEM_KEY;
2245 key.offset = 0;
2246
2247 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2248 if (IS_ERR(inode)) {
2249 srcu_read_unlock(&fs_info->subvol_srcu, index);
2250 return 0;
2251 }
2252
2253 srcu_read_unlock(&fs_info->subvol_srcu, index);
2254
2255 /* step 3: relink backref */
2256 lock_start = backref->file_pos;
2257 lock_end = backref->file_pos + backref->num_bytes - 1;
2258 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2259 0, &cached);
2260
2261 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2262 if (ordered) {
2263 btrfs_put_ordered_extent(ordered);
2264 goto out_unlock;
2265 }
2266
2267 trans = btrfs_join_transaction(root);
2268 if (IS_ERR(trans)) {
2269 ret = PTR_ERR(trans);
2270 goto out_unlock;
2271 }
2272
2273 key.objectid = backref->inum;
2274 key.type = BTRFS_EXTENT_DATA_KEY;
2275 key.offset = backref->file_pos;
2276
2277 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2278 if (ret < 0) {
2279 goto out_free_path;
2280 } else if (ret > 0) {
2281 ret = 0;
2282 goto out_free_path;
2283 }
2284
2285 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2286 struct btrfs_file_extent_item);
2287
2288 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2289 backref->generation)
2290 goto out_free_path;
2291
2292 btrfs_release_path(path);
2293
2294 start = backref->file_pos;
2295 if (backref->extent_offset < old->extent_offset + old->offset)
2296 start += old->extent_offset + old->offset -
2297 backref->extent_offset;
2298
2299 len = min(backref->extent_offset + backref->num_bytes,
2300 old->extent_offset + old->offset + old->len);
2301 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2302
2303 ret = btrfs_drop_extents(trans, root, inode, start,
2304 start + len, 1);
2305 if (ret)
2306 goto out_free_path;
2307again:
2308 key.objectid = btrfs_ino(inode);
2309 key.type = BTRFS_EXTENT_DATA_KEY;
2310 key.offset = start;
2311
Liu Boa09a0a72013-03-11 09:20:58 +00002312 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002313 if (merge) {
2314 struct btrfs_file_extent_item *fi;
2315 u64 extent_len;
2316 struct btrfs_key found_key;
2317
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002318 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002319 if (ret < 0)
2320 goto out_free_path;
2321
2322 path->slots[0]--;
2323 leaf = path->nodes[0];
2324 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2325
2326 fi = btrfs_item_ptr(leaf, path->slots[0],
2327 struct btrfs_file_extent_item);
2328 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2329
Liu Bo116e0022013-08-02 16:30:40 +08002330 if (extent_len + found_key.offset == start &&
2331 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002332 btrfs_set_file_extent_num_bytes(leaf, fi,
2333 extent_len + len);
2334 btrfs_mark_buffer_dirty(leaf);
2335 inode_add_bytes(inode, len);
2336
2337 ret = 1;
2338 goto out_free_path;
2339 } else {
2340 merge = false;
2341 btrfs_release_path(path);
2342 goto again;
2343 }
2344 }
2345
2346 ret = btrfs_insert_empty_item(trans, root, path, &key,
2347 sizeof(*extent));
2348 if (ret) {
2349 btrfs_abort_transaction(trans, root, ret);
2350 goto out_free_path;
2351 }
2352
2353 leaf = path->nodes[0];
2354 item = btrfs_item_ptr(leaf, path->slots[0],
2355 struct btrfs_file_extent_item);
2356 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2357 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2358 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2359 btrfs_set_file_extent_num_bytes(leaf, item, len);
2360 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2361 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2362 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2363 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2364 btrfs_set_file_extent_encryption(leaf, item, 0);
2365 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2366
2367 btrfs_mark_buffer_dirty(leaf);
2368 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002369 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002370
2371 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
2372 new->disk_len, 0,
2373 backref->root_id, backref->inum,
2374 new->file_pos, 0); /* start - extent_offset */
2375 if (ret) {
2376 btrfs_abort_transaction(trans, root, ret);
2377 goto out_free_path;
2378 }
2379
2380 ret = 1;
2381out_free_path:
2382 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002383 path->leave_spinning = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002384 btrfs_end_transaction(trans, root);
2385out_unlock:
2386 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2387 &cached, GFP_NOFS);
2388 iput(inode);
2389 return ret;
2390}
2391
Liu Bo6f519562013-10-29 10:45:05 +08002392static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2393{
2394 struct old_sa_defrag_extent *old, *tmp;
2395
2396 if (!new)
2397 return;
2398
2399 list_for_each_entry_safe(old, tmp, &new->head, list) {
2400 list_del(&old->list);
2401 kfree(old);
2402 }
2403 kfree(new);
2404}
2405
Liu Bo38c227d2013-01-29 03:18:40 +00002406static void relink_file_extents(struct new_sa_defrag_extent *new)
2407{
2408 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002409 struct sa_defrag_extent_backref *backref;
2410 struct sa_defrag_extent_backref *prev = NULL;
2411 struct inode *inode;
2412 struct btrfs_root *root;
2413 struct rb_node *node;
2414 int ret;
2415
2416 inode = new->inode;
2417 root = BTRFS_I(inode)->root;
2418
2419 path = btrfs_alloc_path();
2420 if (!path)
2421 return;
2422
2423 if (!record_extent_backrefs(path, new)) {
2424 btrfs_free_path(path);
2425 goto out;
2426 }
2427 btrfs_release_path(path);
2428
2429 while (1) {
2430 node = rb_first(&new->root);
2431 if (!node)
2432 break;
2433 rb_erase(node, &new->root);
2434
2435 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2436
2437 ret = relink_extent_backref(path, prev, backref);
2438 WARN_ON(ret < 0);
2439
2440 kfree(prev);
2441
2442 if (ret == 1)
2443 prev = backref;
2444 else
2445 prev = NULL;
2446 cond_resched();
2447 }
2448 kfree(prev);
2449
2450 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002451out:
Liu Bo6f519562013-10-29 10:45:05 +08002452 free_sa_defrag_extent(new);
2453
Liu Bo38c227d2013-01-29 03:18:40 +00002454 atomic_dec(&root->fs_info->defrag_running);
2455 wake_up(&root->fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002456}
2457
2458static struct new_sa_defrag_extent *
2459record_old_file_extents(struct inode *inode,
2460 struct btrfs_ordered_extent *ordered)
2461{
2462 struct btrfs_root *root = BTRFS_I(inode)->root;
2463 struct btrfs_path *path;
2464 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002465 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002466 struct new_sa_defrag_extent *new;
2467 int ret;
2468
2469 new = kmalloc(sizeof(*new), GFP_NOFS);
2470 if (!new)
2471 return NULL;
2472
2473 new->inode = inode;
2474 new->file_pos = ordered->file_offset;
2475 new->len = ordered->len;
2476 new->bytenr = ordered->start;
2477 new->disk_len = ordered->disk_len;
2478 new->compress_type = ordered->compress_type;
2479 new->root = RB_ROOT;
2480 INIT_LIST_HEAD(&new->head);
2481
2482 path = btrfs_alloc_path();
2483 if (!path)
2484 goto out_kfree;
2485
2486 key.objectid = btrfs_ino(inode);
2487 key.type = BTRFS_EXTENT_DATA_KEY;
2488 key.offset = new->file_pos;
2489
2490 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2491 if (ret < 0)
2492 goto out_free_path;
2493 if (ret > 0 && path->slots[0] > 0)
2494 path->slots[0]--;
2495
2496 /* find out all the old extents for the file range */
2497 while (1) {
2498 struct btrfs_file_extent_item *extent;
2499 struct extent_buffer *l;
2500 int slot;
2501 u64 num_bytes;
2502 u64 offset;
2503 u64 end;
2504 u64 disk_bytenr;
2505 u64 extent_offset;
2506
2507 l = path->nodes[0];
2508 slot = path->slots[0];
2509
2510 if (slot >= btrfs_header_nritems(l)) {
2511 ret = btrfs_next_leaf(root, path);
2512 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002513 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002514 else if (ret > 0)
2515 break;
2516 continue;
2517 }
2518
2519 btrfs_item_key_to_cpu(l, &key, slot);
2520
2521 if (key.objectid != btrfs_ino(inode))
2522 break;
2523 if (key.type != BTRFS_EXTENT_DATA_KEY)
2524 break;
2525 if (key.offset >= new->file_pos + new->len)
2526 break;
2527
2528 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2529
2530 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2531 if (key.offset + num_bytes < new->file_pos)
2532 goto next;
2533
2534 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2535 if (!disk_bytenr)
2536 goto next;
2537
2538 extent_offset = btrfs_file_extent_offset(l, extent);
2539
2540 old = kmalloc(sizeof(*old), GFP_NOFS);
2541 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002542 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002543
2544 offset = max(new->file_pos, key.offset);
2545 end = min(new->file_pos + new->len, key.offset + num_bytes);
2546
2547 old->bytenr = disk_bytenr;
2548 old->extent_offset = extent_offset;
2549 old->offset = offset - key.offset;
2550 old->len = end - offset;
2551 old->new = new;
2552 old->count = 0;
2553 list_add_tail(&old->list, &new->head);
2554next:
2555 path->slots[0]++;
2556 cond_resched();
2557 }
2558
2559 btrfs_free_path(path);
2560 atomic_inc(&root->fs_info->defrag_running);
2561
2562 return new;
2563
Liu Bo38c227d2013-01-29 03:18:40 +00002564out_free_path:
2565 btrfs_free_path(path);
2566out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002567 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002568 return NULL;
2569}
2570
Chris Masond352ac62008-09-29 15:18:18 -04002571/* as ordered data IO finishes, this gets called so we can finish
2572 * an ordered extent if the range of bytes in the file it covers are
2573 * fully written.
2574 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002575static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002576{
Josef Bacik5fd02042012-05-02 14:00:54 -04002577 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002578 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002579 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002580 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002581 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002582 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002583 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002584 int ret = 0;
2585 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002586 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002587 bool truncated = false;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002588
Liu Bo83eea1f2012-07-10 05:28:39 -06002589 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002590
Josef Bacik5fd02042012-05-02 14:00:54 -04002591 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2592 ret = -EIO;
2593 goto out;
2594 }
2595
Josef Bacik77cef2e2013-08-29 13:57:21 -04002596 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2597 truncated = true;
2598 logical_len = ordered_extent->truncated_len;
2599 /* Truncated the entire extent, don't bother adding */
2600 if (!logical_len)
2601 goto out;
2602 }
2603
Yan, Zhengc2167752009-11-12 09:34:21 +00002604 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002605 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Josef Bacik6c760c02012-11-09 10:53:21 -05002606 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2607 if (nolock)
2608 trans = btrfs_join_transaction_nolock(root);
2609 else
2610 trans = btrfs_join_transaction(root);
2611 if (IS_ERR(trans)) {
2612 ret = PTR_ERR(trans);
2613 trans = NULL;
2614 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002615 }
Josef Bacik6c760c02012-11-09 10:53:21 -05002616 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
2617 ret = btrfs_update_inode_fallback(trans, root, inode);
2618 if (ret) /* -ENOMEM or corruption */
2619 btrfs_abort_transaction(trans, root, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002620 goto out;
2621 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002622
Josef Bacik2ac55d42010-02-03 19:33:23 +00002623 lock_extent_bits(io_tree, ordered_extent->file_offset,
2624 ordered_extent->file_offset + ordered_extent->len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002625 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002626
Liu Bo38c227d2013-01-29 03:18:40 +00002627 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2628 ordered_extent->file_offset + ordered_extent->len - 1,
2629 EXTENT_DEFRAG, 1, cached_state);
2630 if (ret) {
2631 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
2632 if (last_snapshot >= BTRFS_I(inode)->generation)
2633 /* the inode is shared */
2634 new = record_old_file_extents(inode, ordered_extent);
2635
2636 clear_extent_bit(io_tree, ordered_extent->file_offset,
2637 ordered_extent->file_offset + ordered_extent->len - 1,
2638 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2639 }
2640
Josef Bacik0cb59c92010-07-02 12:14:14 -04002641 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002642 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002643 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002644 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002645 if (IS_ERR(trans)) {
2646 ret = PTR_ERR(trans);
2647 trans = NULL;
2648 goto out_unlock;
2649 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002650 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002651
Chris Masonc8b97812008-10-29 14:49:59 -04002652 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002653 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002654 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002655 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00002656 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002657 ordered_extent->file_offset,
2658 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002659 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002660 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04002661 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002662 ret = insert_reserved_file_extent(trans, inode,
2663 ordered_extent->file_offset,
2664 ordered_extent->start,
2665 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04002666 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08002667 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002668 BTRFS_FILE_EXTENT_REG);
Yan Zhengd899e052008-10-30 14:25:28 -04002669 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002670 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2671 ordered_extent->file_offset, ordered_extent->len,
2672 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002673 if (ret < 0) {
2674 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002675 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002676 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002677
Chris Masone6dcd2d2008-07-17 12:53:50 -04002678 add_pending_csums(trans, inode, ordered_extent->file_offset,
2679 &ordered_extent->list);
2680
Josef Bacik6c760c02012-11-09 10:53:21 -05002681 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2682 ret = btrfs_update_inode_fallback(trans, root, inode);
2683 if (ret) { /* -ENOMEM or corruption */
2684 btrfs_abort_transaction(trans, root, ret);
2685 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002686 }
2687 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002688out_unlock:
2689 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2690 ordered_extent->file_offset +
2691 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00002692out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002693 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04002694 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002695 if (trans)
2696 btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002697
Josef Bacik77cef2e2013-08-29 13:57:21 -04002698 if (ret || truncated) {
2699 u64 start, end;
2700
2701 if (truncated)
2702 start = ordered_extent->file_offset + logical_len;
2703 else
2704 start = ordered_extent->file_offset;
2705 end = ordered_extent->file_offset + ordered_extent->len - 1;
2706 clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS);
2707
2708 /* Drop the cache for the part of the extent we didn't write. */
2709 btrfs_drop_extent_cache(inode, start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04002710
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002711 /*
2712 * If the ordered extent had an IOERR or something else went
2713 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04002714 * back to the allocator. We only free the extent in the
2715 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002716 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04002717 if ((ret || !logical_len) &&
2718 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002719 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
2720 btrfs_free_reserved_extent(root, ordered_extent->start,
2721 ordered_extent->disk_len);
2722 }
2723
2724
Josef Bacik5fd02042012-05-02 14:00:54 -04002725 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002726 * This needs to be done to make sure anybody waiting knows we are done
2727 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002728 */
2729 btrfs_remove_ordered_extent(inode, ordered_extent);
2730
Liu Bo38c227d2013-01-29 03:18:40 +00002731 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08002732 if (new) {
2733 if (ret) {
2734 free_sa_defrag_extent(new);
2735 atomic_dec(&root->fs_info->defrag_running);
2736 } else {
2737 relink_file_extents(new);
2738 }
2739 }
Liu Bo38c227d2013-01-29 03:18:40 +00002740
Chris Masone6dcd2d2008-07-17 12:53:50 -04002741 /* once for us */
2742 btrfs_put_ordered_extent(ordered_extent);
2743 /* once for the tree */
2744 btrfs_put_ordered_extent(ordered_extent);
2745
Josef Bacik5fd02042012-05-02 14:00:54 -04002746 return ret;
2747}
2748
2749static void finish_ordered_fn(struct btrfs_work *work)
2750{
2751 struct btrfs_ordered_extent *ordered_extent;
2752 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2753 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002754}
2755
Christoph Hellwigb2950862008-12-02 09:54:17 -05002756static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04002757 struct extent_state *state, int uptodate)
2758{
Josef Bacik5fd02042012-05-02 14:00:54 -04002759 struct inode *inode = page->mapping->host;
2760 struct btrfs_root *root = BTRFS_I(inode)->root;
2761 struct btrfs_ordered_extent *ordered_extent = NULL;
2762 struct btrfs_workers *workers;
2763
liubo1abe9b82011-03-24 11:18:59 +00002764 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2765
Chris Mason8b62b722009-09-02 16:53:46 -04002766 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002767 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2768 end - start + 1, uptodate))
2769 return 0;
2770
2771 ordered_extent->work.func = finish_ordered_fn;
2772 ordered_extent->work.flags = 0;
2773
Liu Bo83eea1f2012-07-10 05:28:39 -06002774 if (btrfs_is_free_space_inode(inode))
Josef Bacik5fd02042012-05-02 14:00:54 -04002775 workers = &root->fs_info->endio_freespace_worker;
2776 else
2777 workers = &root->fs_info->endio_write_workers;
2778 btrfs_queue_worker(workers, &ordered_extent->work);
2779
2780 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04002781}
2782
Chris Masond352ac62008-09-29 15:18:18 -04002783/*
Chris Masond352ac62008-09-29 15:18:18 -04002784 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002785 * if there's a match, we allow the bio to finish. If not, the code in
2786 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002787 */
Miao Xiefacc8a222013-07-25 19:22:34 +08002788static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
2789 u64 phy_offset, struct page *page,
2790 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002791{
Miao Xie4eee4fa2012-12-21 09:17:45 +00002792 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04002793 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002794 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04002795 char *kaddr;
Chris Masonff79f812007-10-15 16:22:25 -04002796 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiefacc8a222013-07-25 19:22:34 +08002797 u32 csum_expected;
Chris Masonff79f812007-10-15 16:22:25 -04002798 u32 csum = ~(u32)0;
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002799 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
2800 DEFAULT_RATELIMIT_BURST);
Chris Masond1310b22008-01-24 16:13:08 -05002801
Chris Masond20f7042008-12-08 16:58:54 -05002802 if (PageChecked(page)) {
2803 ClearPageChecked(page);
2804 goto good;
2805 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002806
2807 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Jan Schmidt08d2f342011-05-04 16:18:50 +02002808 goto good;
Chris Masond20f7042008-12-08 16:58:54 -05002809
Yan Zheng17d217f2008-12-12 10:03:38 -05002810 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002811 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002812 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
2813 GFP_NOFS);
2814 return 0;
2815 }
2816
Miao Xiefacc8a222013-07-25 19:22:34 +08002817 phy_offset >>= inode->i_sb->s_blocksize_bits;
2818 csum_expected = *(((u32 *)io_bio->csum) + phy_offset);
Chris Masond3977122009-01-05 21:25:51 -05002819
Miao Xiefacc8a222013-07-25 19:22:34 +08002820 kaddr = kmap_atomic(page);
Liu Bob0496682013-03-14 14:57:45 +00002821 csum = btrfs_csum_data(kaddr + offset, csum, end - start + 1);
Chris Masonff79f812007-10-15 16:22:25 -04002822 btrfs_csum_final(csum, (char *)&csum);
Miao Xiefacc8a222013-07-25 19:22:34 +08002823 if (csum != csum_expected)
Chris Mason07157aa2007-08-30 08:50:51 -04002824 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002825
Cong Wang7ac687d2011-11-25 23:14:28 +08002826 kunmap_atomic(kaddr);
Chris Masond20f7042008-12-08 16:58:54 -05002827good:
Chris Mason07157aa2007-08-30 08:50:51 -04002828 return 0;
2829
2830zeroit:
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002831 if (__ratelimit(&_rs))
Miao Xiefacc8a222013-07-25 19:22:34 +08002832 btrfs_info(root->fs_info, "csum failed ino %llu off %llu csum %u expected csum %u",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002833 btrfs_ino(page->mapping->host), start, csum, csum_expected);
Chris Masondb945352007-10-15 16:15:53 -04002834 memset(kaddr + offset, 1, end - start + 1);
2835 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002836 kunmap_atomic(kaddr);
Miao Xiefacc8a222013-07-25 19:22:34 +08002837 if (csum_expected == 0)
Chris Mason3b951512008-04-17 11:29:12 -04002838 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04002839 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04002840}
Chris Masonb888db2b2007-08-27 16:49:44 -04002841
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002842struct delayed_iput {
2843 struct list_head list;
2844 struct inode *inode;
2845};
2846
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002847/* JDM: If this is fs-wide, why can't we add a pointer to
2848 * btrfs_inode instead and avoid the allocation? */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002849void btrfs_add_delayed_iput(struct inode *inode)
2850{
2851 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2852 struct delayed_iput *delayed;
2853
2854 if (atomic_add_unless(&inode->i_count, -1, 1))
2855 return;
2856
2857 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2858 delayed->inode = inode;
2859
2860 spin_lock(&fs_info->delayed_iput_lock);
2861 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2862 spin_unlock(&fs_info->delayed_iput_lock);
2863}
2864
2865void btrfs_run_delayed_iputs(struct btrfs_root *root)
2866{
2867 LIST_HEAD(list);
2868 struct btrfs_fs_info *fs_info = root->fs_info;
2869 struct delayed_iput *delayed;
2870 int empty;
2871
2872 spin_lock(&fs_info->delayed_iput_lock);
2873 empty = list_empty(&fs_info->delayed_iputs);
2874 spin_unlock(&fs_info->delayed_iput_lock);
2875 if (empty)
2876 return;
2877
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002878 spin_lock(&fs_info->delayed_iput_lock);
2879 list_splice_init(&fs_info->delayed_iputs, &list);
2880 spin_unlock(&fs_info->delayed_iput_lock);
2881
2882 while (!list_empty(&list)) {
2883 delayed = list_entry(list.next, struct delayed_iput, list);
2884 list_del(&delayed->list);
2885 iput(delayed->inode);
2886 kfree(delayed);
2887 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002888}
2889
Yan, Zhengd68fc572010-05-16 10:49:58 -04002890/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08002891 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04002892 * files in the subvolume, it removes orphan item and frees block_rsv
2893 * structure.
2894 */
2895void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
2896 struct btrfs_root *root)
2897{
Josef Bacik90290e12011-12-02 15:44:12 -05002898 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002899 int ret;
2900
Josef Bacik8a35d952012-05-23 14:26:42 -04002901 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04002902 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
2903 return;
2904
Josef Bacik90290e12011-12-02 15:44:12 -05002905 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04002906 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05002907 spin_unlock(&root->orphan_lock);
2908 return;
2909 }
2910
2911 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
2912 spin_unlock(&root->orphan_lock);
2913 return;
2914 }
2915
2916 block_rsv = root->orphan_block_rsv;
2917 root->orphan_block_rsv = NULL;
2918 spin_unlock(&root->orphan_lock);
2919
Yan, Zhengd68fc572010-05-16 10:49:58 -04002920 if (root->orphan_item_inserted &&
2921 btrfs_root_refs(&root->root_item) > 0) {
2922 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
2923 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04002924 if (ret)
2925 btrfs_abort_transaction(trans, root, ret);
2926 else
2927 root->orphan_item_inserted = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002928 }
2929
Josef Bacik90290e12011-12-02 15:44:12 -05002930 if (block_rsv) {
2931 WARN_ON(block_rsv->size > 0);
2932 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002933 }
2934}
2935
2936/*
Josef Bacik7b128762008-07-24 12:17:14 -04002937 * This creates an orphan entry for the given inode in case something goes
2938 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04002939 *
2940 * NOTE: caller of this function should reserve 5 units of metadata for
2941 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04002942 */
2943int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2944{
2945 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002946 struct btrfs_block_rsv *block_rsv = NULL;
2947 int reserve = 0;
2948 int insert = 0;
2949 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002950
Yan, Zhengd68fc572010-05-16 10:49:58 -04002951 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06002952 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00002953 if (!block_rsv)
2954 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04002955 }
2956
Yan, Zhengd68fc572010-05-16 10:49:58 -04002957 spin_lock(&root->orphan_lock);
2958 if (!root->orphan_block_rsv) {
2959 root->orphan_block_rsv = block_rsv;
2960 } else if (block_rsv) {
2961 btrfs_free_block_rsv(root, block_rsv);
2962 block_rsv = NULL;
2963 }
Josef Bacik7b128762008-07-24 12:17:14 -04002964
Josef Bacik8a35d952012-05-23 14:26:42 -04002965 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2966 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04002967#if 0
2968 /*
2969 * For proper ENOSPC handling, we should do orphan
2970 * cleanup when mounting. But this introduces backward
2971 * compatibility issue.
2972 */
2973 if (!xchg(&root->orphan_item_inserted, 1))
2974 insert = 2;
2975 else
2976 insert = 1;
2977#endif
2978 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06002979 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002980 }
Josef Bacik7b128762008-07-24 12:17:14 -04002981
Josef Bacik72ac3c02012-05-23 14:13:11 -04002982 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
2983 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04002984 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002985 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002986
Yan, Zhengd68fc572010-05-16 10:49:58 -04002987 /* grab metadata reservation from transaction handle */
2988 if (reserve) {
2989 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002990 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04002991 }
2992
2993 /* insert an orphan item to track this unlinked/truncated file */
2994 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08002995 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04002996 if (ret) {
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01002997 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04002998 if (reserve) {
2999 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3000 &BTRFS_I(inode)->runtime_flags);
3001 btrfs_orphan_release_metadata(inode);
3002 }
3003 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003004 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3005 &BTRFS_I(inode)->runtime_flags);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003006 btrfs_abort_transaction(trans, root, ret);
3007 return ret;
3008 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003009 }
3010 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003011 }
3012
3013 /* insert an orphan item to track subvolume contains orphan files */
3014 if (insert >= 2) {
3015 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
3016 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003017 if (ret && ret != -EEXIST) {
3018 btrfs_abort_transaction(trans, root, ret);
3019 return ret;
3020 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003021 }
3022 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003023}
3024
3025/*
3026 * We have done the truncate/delete so we can go ahead and remove the orphan
3027 * item for this particular inode.
3028 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003029static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3030 struct inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003031{
3032 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003033 int delete_item = 0;
3034 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003035 int ret = 0;
3036
Yan, Zhengd68fc572010-05-16 10:49:58 -04003037 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003038 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3039 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003040 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003041
Josef Bacik72ac3c02012-05-23 14:13:11 -04003042 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3043 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003044 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003045 spin_unlock(&root->orphan_lock);
3046
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003047 if (delete_item) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003048 atomic_dec(&root->orphan_inodes);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003049 if (trans)
3050 ret = btrfs_del_orphan_item(trans, root,
3051 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04003052 }
Josef Bacik7b128762008-07-24 12:17:14 -04003053
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003054 if (release_rsv)
3055 btrfs_orphan_release_metadata(inode);
3056
Josef Bacik4ef31a42013-08-13 14:10:08 -04003057 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003058}
3059
3060/*
3061 * this cleans up any orphans that may be left on the list from the last use
3062 * of this root.
3063 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003064int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003065{
3066 struct btrfs_path *path;
3067 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003068 struct btrfs_key key, found_key;
3069 struct btrfs_trans_handle *trans;
3070 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003071 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003072 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3073
Yan, Zhengd68fc572010-05-16 10:49:58 -04003074 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003075 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003076
3077 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003078 if (!path) {
3079 ret = -ENOMEM;
3080 goto out;
3081 }
Josef Bacik7b128762008-07-24 12:17:14 -04003082 path->reada = -1;
3083
3084 key.objectid = BTRFS_ORPHAN_OBJECTID;
3085 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
3086 key.offset = (u64)-1;
3087
Josef Bacik7b128762008-07-24 12:17:14 -04003088 while (1) {
3089 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003090 if (ret < 0)
3091 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003092
3093 /*
3094 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003095 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003096 * find the key and see if we have stuff that matches
3097 */
3098 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003099 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003100 if (path->slots[0] == 0)
3101 break;
3102 path->slots[0]--;
3103 }
3104
3105 /* pull out the item */
3106 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003107 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3108
3109 /* make sure the item matches what we want */
3110 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3111 break;
3112 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
3113 break;
3114
3115 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003116 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003117
3118 /*
3119 * this is where we are basically btrfs_lookup, without the
3120 * crossing root thing. we store the inode number in the
3121 * offset of the orphan item.
3122 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003123
3124 if (found_key.offset == last_objectid) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003125 btrfs_err(root->fs_info,
3126 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003127 ret = -EINVAL;
3128 goto out;
3129 }
3130
3131 last_objectid = found_key.offset;
3132
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003133 found_key.objectid = found_key.offset;
3134 found_key.type = BTRFS_INODE_ITEM_KEY;
3135 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00003136 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303137 ret = PTR_ERR_OR_ZERO(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003138 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003139 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003140
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003141 if (ret == -ESTALE && root == root->fs_info->tree_root) {
3142 struct btrfs_root *dead_root;
3143 struct btrfs_fs_info *fs_info = root->fs_info;
3144 int is_dead_root = 0;
3145
3146 /*
3147 * this is an orphan in the tree root. Currently these
3148 * could come from 2 sources:
3149 * a) a snapshot deletion in progress
3150 * b) a free space cache inode
3151 * We need to distinguish those two, as the snapshot
3152 * orphan must not get deleted.
3153 * find_dead_roots already ran before us, so if this
3154 * is a snapshot deletion, we should find the root
3155 * in the dead_roots list
3156 */
3157 spin_lock(&fs_info->trans_lock);
3158 list_for_each_entry(dead_root, &fs_info->dead_roots,
3159 root_list) {
3160 if (dead_root->root_key.objectid ==
3161 found_key.objectid) {
3162 is_dead_root = 1;
3163 break;
3164 }
3165 }
3166 spin_unlock(&fs_info->trans_lock);
3167 if (is_dead_root) {
3168 /* prevent this orphan from being found again */
3169 key.offset = found_key.objectid - 1;
3170 continue;
3171 }
3172 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003173 /*
3174 * Inode is already gone but the orphan item is still there,
3175 * kill the orphan item.
3176 */
3177 if (ret == -ESTALE) {
3178 trans = btrfs_start_transaction(root, 1);
3179 if (IS_ERR(trans)) {
3180 ret = PTR_ERR(trans);
3181 goto out;
3182 }
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003183 btrfs_debug(root->fs_info, "auto deleting %Lu",
3184 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003185 ret = btrfs_del_orphan_item(trans, root,
3186 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003187 btrfs_end_transaction(trans, root);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003188 if (ret)
3189 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003190 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003191 }
Josef Bacik7b128762008-07-24 12:17:14 -04003192
Josef Bacik7b128762008-07-24 12:17:14 -04003193 /*
3194 * add this inode to the orphan list so btrfs_orphan_del does
3195 * the proper thing when we hit it
3196 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003197 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3198 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003199 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003200
Josef Bacik7b128762008-07-24 12:17:14 -04003201 /* if we have links, this was a truncate, lets do that */
3202 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303203 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003204 iput(inode);
3205 continue;
3206 }
Josef Bacik7b128762008-07-24 12:17:14 -04003207 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003208
3209 /* 1 for the orphan item deletion. */
3210 trans = btrfs_start_transaction(root, 1);
3211 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003212 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003213 ret = PTR_ERR(trans);
3214 goto out;
3215 }
3216 ret = btrfs_orphan_add(trans, inode);
3217 btrfs_end_transaction(trans, root);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003218 if (ret) {
3219 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003220 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003221 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003222
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003223 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003224 if (ret)
3225 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003226 } else {
3227 nr_unlink++;
3228 }
3229
3230 /* this will do delete_inode and everything for us */
3231 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003232 if (ret)
3233 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003234 }
Miao Xie3254c872011-11-10 20:45:05 -05003235 /* release the path since we're done with it */
3236 btrfs_release_path(path);
3237
Yan, Zhengd68fc572010-05-16 10:49:58 -04003238 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3239
3240 if (root->orphan_block_rsv)
3241 btrfs_block_rsv_release(root, root->orphan_block_rsv,
3242 (u64)-1);
3243
3244 if (root->orphan_block_rsv || root->orphan_item_inserted) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003245 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003246 if (!IS_ERR(trans))
3247 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003248 }
Josef Bacik7b128762008-07-24 12:17:14 -04003249
3250 if (nr_unlink)
David Sterba4884b472013-03-20 13:31:27 +00003251 btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003252 if (nr_truncate)
David Sterba4884b472013-03-20 13:31:27 +00003253 btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003254
3255out:
3256 if (ret)
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003257 btrfs_crit(root->fs_info,
3258 "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003259 btrfs_free_path(path);
3260 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003261}
3262
Chris Masond352ac62008-09-29 15:18:18 -04003263/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003264 * very simple check to peek ahead in the leaf looking for xattrs. If we
3265 * don't find any xattrs, we know there can't be any acls.
3266 *
3267 * slot is the slot the inode is in, objectid is the objectid of the inode
3268 */
3269static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003270 int slot, u64 objectid,
3271 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003272{
3273 u32 nritems = btrfs_header_nritems(leaf);
3274 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003275 static u64 xattr_access = 0;
3276 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003277 int scanned = 0;
3278
Josef Bacikf23b5a52013-06-19 10:16:26 -04003279 if (!xattr_access) {
3280 xattr_access = btrfs_name_hash(POSIX_ACL_XATTR_ACCESS,
3281 strlen(POSIX_ACL_XATTR_ACCESS));
3282 xattr_default = btrfs_name_hash(POSIX_ACL_XATTR_DEFAULT,
3283 strlen(POSIX_ACL_XATTR_DEFAULT));
3284 }
3285
Chris Mason46a53cc2009-04-27 11:47:50 -04003286 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003287 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003288 while (slot < nritems) {
3289 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3290
3291 /* we found a different objectid, there must not be acls */
3292 if (found_key.objectid != objectid)
3293 return 0;
3294
3295 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003296 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003297 if (*first_xattr_slot == -1)
3298 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003299 if (found_key.offset == xattr_access ||
3300 found_key.offset == xattr_default)
3301 return 1;
3302 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003303
3304 /*
3305 * we found a key greater than an xattr key, there can't
3306 * be any acls later on
3307 */
3308 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3309 return 0;
3310
3311 slot++;
3312 scanned++;
3313
3314 /*
3315 * it goes inode, inode backrefs, xattrs, extents,
3316 * so if there are a ton of hard links to an inode there can
3317 * be a lot of backrefs. Don't waste time searching too hard,
3318 * this is just an optimization
3319 */
3320 if (scanned >= 8)
3321 break;
3322 }
3323 /* we hit the end of the leaf before we found an xattr or
3324 * something larger than an xattr. We have to assume the inode
3325 * has acls
3326 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003327 if (*first_xattr_slot == -1)
3328 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003329 return 1;
3330}
3331
3332/*
Chris Masond352ac62008-09-29 15:18:18 -04003333 * read an inode from the btree into the in-memory inode
3334 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003335static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003336{
3337 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003338 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003339 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04003340 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04003341 struct btrfs_root *root = BTRFS_I(inode)->root;
3342 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003343 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003344 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003345 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003346 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003347 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003348 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003349
3350 ret = btrfs_fill_inode(inode, &rdev);
3351 if (!ret)
3352 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003353
3354 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07003355 if (!path)
3356 goto make_bad;
3357
Chris Mason39279cc2007-06-12 06:35:45 -04003358 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003359
Chris Mason39279cc2007-06-12 06:35:45 -04003360 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003361 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04003362 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04003363
Chris Mason5f39d392007-10-15 16:14:19 -04003364 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003365
3366 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003367 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003368
Chris Mason5f39d392007-10-15 16:14:19 -04003369 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3370 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003371 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003372 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003373 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3374 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04003375 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003376
3377 tspec = btrfs_inode_atime(inode_item);
3378 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3379 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3380
3381 tspec = btrfs_inode_mtime(inode_item);
3382 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3383 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3384
3385 tspec = btrfs_inode_ctime(inode_item);
3386 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3387 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3388
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003389 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003390 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003391 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3392
3393 /*
3394 * If we were modified in the current generation and evicted from memory
3395 * and then re-read we need to do a full sync since we don't have any
3396 * idea about which extents were modified before we were evicted from
3397 * cache.
3398 */
3399 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
3400 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3401 &BTRFS_I(inode)->runtime_flags);
3402
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003403 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003404 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003405 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003406 rdev = btrfs_inode_rdev(leaf, inode_item);
3407
Josef Bacikaec74772008-07-24 12:12:38 -04003408 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003409 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003410
3411cache_index:
3412 path->slots[0]++;
3413 if (inode->i_nlink != 1 ||
3414 path->slots[0] >= btrfs_header_nritems(leaf))
3415 goto cache_acl;
3416
3417 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
3418 if (location.objectid != btrfs_ino(inode))
3419 goto cache_acl;
3420
3421 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3422 if (location.type == BTRFS_INODE_REF_KEY) {
3423 struct btrfs_inode_ref *ref;
3424
3425 ref = (struct btrfs_inode_ref *)ptr;
3426 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3427 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3428 struct btrfs_inode_extref *extref;
3429
3430 extref = (struct btrfs_inode_extref *)ptr;
3431 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3432 extref);
3433 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003434cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003435 /*
3436 * try to precache a NULL acl entry for files that don't have
3437 * any xattrs or acls
3438 */
Li Zefan33345d012011-04-20 10:31:50 +08003439 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003440 btrfs_ino(inode), &first_xattr_slot);
3441 if (first_xattr_slot != -1) {
3442 path->slots[0] = first_xattr_slot;
3443 ret = btrfs_load_inode_props(inode, path);
3444 if (ret)
3445 btrfs_err(root->fs_info,
3446 "error loading props for ino %llu (root %llu): %d\n",
3447 btrfs_ino(inode),
3448 root->root_key.objectid, ret);
3449 }
3450 btrfs_free_path(path);
3451
Al Viro72c04902009-06-24 16:58:48 -04003452 if (!maybe_acls)
3453 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003454
Chris Mason39279cc2007-06-12 06:35:45 -04003455 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003456 case S_IFREG:
3457 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003458 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05003459 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003460 inode->i_fop = &btrfs_file_operations;
3461 inode->i_op = &btrfs_file_inode_operations;
3462 break;
3463 case S_IFDIR:
3464 inode->i_fop = &btrfs_dir_file_operations;
3465 if (root == root->fs_info->tree_root)
3466 inode->i_op = &btrfs_dir_ro_inode_operations;
3467 else
3468 inode->i_op = &btrfs_dir_inode_operations;
3469 break;
3470 case S_IFLNK:
3471 inode->i_op = &btrfs_symlink_inode_operations;
3472 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04003473 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003474 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003475 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003476 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003477 init_special_inode(inode, inode->i_mode, rdev);
3478 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003479 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003480
3481 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003482 return;
3483
3484make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003485 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003486 make_bad_inode(inode);
3487}
3488
Chris Masond352ac62008-09-29 15:18:18 -04003489/*
3490 * given a leaf and an inode, copy the inode fields into the leaf
3491 */
Chris Masone02119d2008-09-05 16:13:11 -04003492static void fill_inode_item(struct btrfs_trans_handle *trans,
3493 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003494 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003495 struct inode *inode)
3496{
Liu Bo51fab692012-12-27 09:01:21 +00003497 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003498
Liu Bo51fab692012-12-27 09:01:21 +00003499 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003500
Liu Bo51fab692012-12-27 09:01:21 +00003501 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3502 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3503 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3504 &token);
3505 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3506 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003507
Liu Bo51fab692012-12-27 09:01:21 +00003508 btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item),
3509 inode->i_atime.tv_sec, &token);
3510 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item),
3511 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003512
Liu Bo51fab692012-12-27 09:01:21 +00003513 btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item),
3514 inode->i_mtime.tv_sec, &token);
3515 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item),
3516 inode->i_mtime.tv_nsec, &token);
3517
3518 btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item),
3519 inode->i_ctime.tv_sec, &token);
3520 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item),
3521 inode->i_ctime.tv_nsec, &token);
3522
3523 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3524 &token);
3525 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3526 &token);
3527 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3528 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3529 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3530 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3531 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003532}
3533
Chris Masond352ac62008-09-29 15:18:18 -04003534/*
3535 * copy everything in the in-memory inode into the btree.
3536 */
Chris Mason21151332011-11-10 20:39:08 -05003537static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003538 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003539{
3540 struct btrfs_inode_item *inode_item;
3541 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003542 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003543 int ret;
3544
3545 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003546 if (!path)
3547 return -ENOMEM;
3548
Chris Masonb9473432009-03-13 11:00:37 -04003549 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003550 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3551 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003552 if (ret) {
3553 if (ret > 0)
3554 ret = -ENOENT;
3555 goto failed;
3556 }
3557
Chris Mason5f39d392007-10-15 16:14:19 -04003558 leaf = path->nodes[0];
3559 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003560 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003561
Chris Masone02119d2008-09-05 16:13:11 -04003562 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003563 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003564 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003565 ret = 0;
3566failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003567 btrfs_free_path(path);
3568 return ret;
3569}
3570
Chris Masond352ac62008-09-29 15:18:18 -04003571/*
Chris Mason21151332011-11-10 20:39:08 -05003572 * copy everything in the in-memory inode into the btree.
3573 */
3574noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3575 struct btrfs_root *root, struct inode *inode)
3576{
3577 int ret;
3578
3579 /*
3580 * If the inode is a free space inode, we can deadlock during commit
3581 * if we put it into the delayed code.
3582 *
3583 * The data relocation inode should also be directly updated
3584 * without delay
3585 */
Liu Bo83eea1f2012-07-10 05:28:39 -06003586 if (!btrfs_is_free_space_inode(inode)
Chris Mason21151332011-11-10 20:39:08 -05003587 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003588 btrfs_update_root_times(trans, root);
3589
Chris Mason21151332011-11-10 20:39:08 -05003590 ret = btrfs_delayed_update_inode(trans, root, inode);
3591 if (!ret)
3592 btrfs_set_inode_last_trans(trans, inode);
3593 return ret;
3594 }
3595
3596 return btrfs_update_inode_item(trans, root, inode);
3597}
3598
Josef Bacikbe6aef62012-10-22 15:43:12 -04003599noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3600 struct btrfs_root *root,
3601 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003602{
3603 int ret;
3604
3605 ret = btrfs_update_inode(trans, root, inode);
3606 if (ret == -ENOSPC)
3607 return btrfs_update_inode_item(trans, root, inode);
3608 return ret;
3609}
3610
3611/*
Chris Masond352ac62008-09-29 15:18:18 -04003612 * unlink helper that gets used here in inode.c and in the tree logging
3613 * recovery code. It remove a link in a directory with a given name, and
3614 * also drops the back refs in the inode to the directory
3615 */
Al Viro92986792011-03-04 17:14:37 +00003616static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3617 struct btrfs_root *root,
3618 struct inode *dir, struct inode *inode,
3619 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003620{
3621 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003622 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003623 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003624 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003625 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003626 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003627 u64 ino = btrfs_ino(inode);
3628 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003629
3630 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003631 if (!path) {
3632 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003633 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003634 }
3635
Chris Masonb9473432009-03-13 11:00:37 -04003636 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003637 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003638 name, name_len, -1);
3639 if (IS_ERR(di)) {
3640 ret = PTR_ERR(di);
3641 goto err;
3642 }
3643 if (!di) {
3644 ret = -ENOENT;
3645 goto err;
3646 }
Chris Mason5f39d392007-10-15 16:14:19 -04003647 leaf = path->nodes[0];
3648 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003649 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003650 if (ret)
3651 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003652 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003653
Miao Xie67de1172013-12-26 13:07:06 +08003654 /*
3655 * If we don't have dir index, we have to get it by looking up
3656 * the inode ref, since we get the inode ref, remove it directly,
3657 * it is unnecessary to do delayed deletion.
3658 *
3659 * But if we have dir index, needn't search inode ref to get it.
3660 * Since the inode ref is close to the inode item, it is better
3661 * that we delay to delete it, and just do this deletion when
3662 * we update the inode item.
3663 */
3664 if (BTRFS_I(inode)->dir_index) {
3665 ret = btrfs_delayed_delete_inode_ref(inode);
3666 if (!ret) {
3667 index = BTRFS_I(inode)->dir_index;
3668 goto skip_backref;
3669 }
3670 }
3671
Li Zefan33345d012011-04-20 10:31:50 +08003672 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3673 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003674 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003675 btrfs_info(root->fs_info,
3676 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003677 name_len, name, ino, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003678 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003679 goto err;
3680 }
Miao Xie67de1172013-12-26 13:07:06 +08003681skip_backref:
Miao Xie16cdcec2011-04-22 18:12:22 +08003682 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003683 if (ret) {
3684 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003685 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003686 }
Chris Mason39279cc2007-06-12 06:35:45 -04003687
Chris Masone02119d2008-09-05 16:13:11 -04003688 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08003689 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003690 if (ret != 0 && ret != -ENOENT) {
3691 btrfs_abort_transaction(trans, root, ret);
3692 goto err;
3693 }
Chris Masone02119d2008-09-05 16:13:11 -04003694
3695 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
3696 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04003697 if (ret == -ENOENT)
3698 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003699 else if (ret)
3700 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003701err:
3702 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003703 if (ret)
3704 goto out;
3705
3706 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003707 inode_inc_iversion(inode);
3708 inode_inc_iversion(dir);
Chris Masone02119d2008-09-05 16:13:11 -04003709 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Tsutomu Itohb9959292012-06-25 21:25:22 -06003710 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04003711out:
Chris Mason39279cc2007-06-12 06:35:45 -04003712 return ret;
3713}
3714
Al Viro92986792011-03-04 17:14:37 +00003715int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3716 struct btrfs_root *root,
3717 struct inode *dir, struct inode *inode,
3718 const char *name, int name_len)
3719{
3720 int ret;
3721 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3722 if (!ret) {
Zach Brown8b558c52013-10-16 12:10:34 -07003723 drop_nlink(inode);
Al Viro92986792011-03-04 17:14:37 +00003724 ret = btrfs_update_inode(trans, root, inode);
3725 }
3726 return ret;
3727}
Yan, Zhenga22285a2010-05-16 10:48:46 -04003728
3729/*
3730 * helper to start transaction for unlink and rmdir.
3731 *
Josef Bacikd52be812013-05-29 14:54:47 -04003732 * unlink and rmdir are special in btrfs, they do not always free space, so
3733 * if we cannot make our reservations the normal way try and see if there is
3734 * plenty of slack room in the global reserve to migrate, otherwise we cannot
3735 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04003736 */
Josef Bacikd52be812013-05-29 14:54:47 -04003737static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003738{
3739 struct btrfs_trans_handle *trans;
3740 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003741 int ret;
3742
Josef Bacike70bea52011-10-11 14:18:24 -04003743 /*
3744 * 1 for the possible orphan item
3745 * 1 for the dir item
3746 * 1 for the dir index
3747 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04003748 * 1 for the inode
3749 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04003750 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003751 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
3752 return trans;
3753
Josef Bacikd52be812013-05-29 14:54:47 -04003754 if (PTR_ERR(trans) == -ENOSPC) {
3755 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003756
Josef Bacikd52be812013-05-29 14:54:47 -04003757 trans = btrfs_start_transaction(root, 0);
3758 if (IS_ERR(trans))
3759 return trans;
3760 ret = btrfs_cond_migrate_bytes(root->fs_info,
3761 &root->fs_info->trans_block_rsv,
3762 num_bytes, 5);
3763 if (ret) {
3764 btrfs_end_transaction(trans, root);
3765 return ERR_PTR(ret);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003766 }
Josef Bacik5a77d762011-11-01 14:32:23 -04003767 trans->block_rsv = &root->fs_info->trans_block_rsv;
Josef Bacikd52be812013-05-29 14:54:47 -04003768 trans->bytes_reserved = num_bytes;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003769 }
Josef Bacikd52be812013-05-29 14:54:47 -04003770 return trans;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003771}
3772
Chris Mason39279cc2007-06-12 06:35:45 -04003773static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3774{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003775 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003776 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04003777 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003778 int ret;
3779
Josef Bacikd52be812013-05-29 14:54:47 -04003780 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003781 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003782 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003783
Chris Mason12fcfd22009-03-24 10:24:20 -04003784 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
3785
Chris Masone02119d2008-09-05 16:13:11 -04003786 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3787 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003788 if (ret)
3789 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003790
Yan, Zhenga22285a2010-05-16 10:48:46 -04003791 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04003792 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003793 if (ret)
3794 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003795 }
Josef Bacik7b128762008-07-24 12:17:14 -04003796
Tsutomu Itohb5324022011-07-19 07:27:20 +00003797out:
Josef Bacikd52be812013-05-29 14:54:47 -04003798 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003799 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04003800 return ret;
3801}
3802
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003803int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3804 struct btrfs_root *root,
3805 struct inode *dir, u64 objectid,
3806 const char *name, int name_len)
3807{
3808 struct btrfs_path *path;
3809 struct extent_buffer *leaf;
3810 struct btrfs_dir_item *di;
3811 struct btrfs_key key;
3812 u64 index;
3813 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003814 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003815
3816 path = btrfs_alloc_path();
3817 if (!path)
3818 return -ENOMEM;
3819
Li Zefan33345d012011-04-20 10:31:50 +08003820 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003821 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003822 if (IS_ERR_OR_NULL(di)) {
3823 if (!di)
3824 ret = -ENOENT;
3825 else
3826 ret = PTR_ERR(di);
3827 goto out;
3828 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003829
3830 leaf = path->nodes[0];
3831 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3832 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3833 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003834 if (ret) {
3835 btrfs_abort_transaction(trans, root, ret);
3836 goto out;
3837 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003838 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003839
3840 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3841 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003842 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003843 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003844 if (ret != -ENOENT) {
3845 btrfs_abort_transaction(trans, root, ret);
3846 goto out;
3847 }
Li Zefan33345d012011-04-20 10:31:50 +08003848 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003849 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003850 if (IS_ERR_OR_NULL(di)) {
3851 if (!di)
3852 ret = -ENOENT;
3853 else
3854 ret = PTR_ERR(di);
3855 btrfs_abort_transaction(trans, root, ret);
3856 goto out;
3857 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003858
3859 leaf = path->nodes[0];
3860 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003861 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003862 index = key.offset;
3863 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003864 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003865
Miao Xie16cdcec2011-04-22 18:12:22 +08003866 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003867 if (ret) {
3868 btrfs_abort_transaction(trans, root, ret);
3869 goto out;
3870 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003871
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003872 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003873 inode_inc_iversion(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003874 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Josef Bacik5a24e842012-08-08 10:12:59 -06003875 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003876 if (ret)
3877 btrfs_abort_transaction(trans, root, ret);
3878out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04003879 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003880 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003881}
3882
Chris Mason39279cc2007-06-12 06:35:45 -04003883static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3884{
3885 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003886 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003887 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003888 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04003889
David Sterbab3ae2442012-09-13 16:04:34 -06003890 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04003891 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06003892 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
3893 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04003894
Josef Bacikd52be812013-05-29 14:54:47 -04003895 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003896 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003897 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003898
Li Zefan33345d012011-04-20 10:31:50 +08003899 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003900 err = btrfs_unlink_subvol(trans, root, dir,
3901 BTRFS_I(inode)->location.objectid,
3902 dentry->d_name.name,
3903 dentry->d_name.len);
3904 goto out;
3905 }
3906
Josef Bacik7b128762008-07-24 12:17:14 -04003907 err = btrfs_orphan_add(trans, inode);
3908 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003909 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003910
Chris Mason39279cc2007-06-12 06:35:45 -04003911 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04003912 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3913 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05003914 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04003915 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003916out:
Josef Bacikd52be812013-05-29 14:54:47 -04003917 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003918 btrfs_btree_balance_dirty(root);
Chris Mason39544012007-12-12 14:38:19 -05003919
Chris Mason39279cc2007-06-12 06:35:45 -04003920 return err;
3921}
3922
Chris Mason323ac952008-10-01 19:05:46 -04003923/*
Chris Mason39279cc2007-06-12 06:35:45 -04003924 * this can truncate away extent items, csum items and directory items.
3925 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04003926 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04003927 *
3928 * csum items that cross the new i_size are truncated to the new size
3929 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04003930 *
3931 * min_type is the minimum key type to truncate down to. If set to 0, this
3932 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04003933 */
Yan, Zheng80825102009-11-12 09:35:36 +00003934int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
3935 struct btrfs_root *root,
3936 struct inode *inode,
3937 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003938{
Chris Mason39279cc2007-06-12 06:35:45 -04003939 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003940 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003941 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00003942 struct btrfs_key key;
3943 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04003944 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04003945 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003946 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003947 u64 item_end = 0;
Josef Bacik7f4f6e02013-08-29 16:43:28 -04003948 u64 last_size = (u64)-1;
Yan, Zheng80825102009-11-12 09:35:36 +00003949 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04003950 int found_extent;
3951 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003952 int pending_del_nr = 0;
3953 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04003954 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00003955 int ret;
3956 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08003957 u64 ino = btrfs_ino(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003958
3959 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04003960
Mark Fasheh0eb0e192011-07-12 16:44:10 -07003961 path = btrfs_alloc_path();
3962 if (!path)
3963 return -ENOMEM;
3964 path->reada = -1;
3965
Josef Bacik5dc562c2012-08-17 13:14:17 -04003966 /*
3967 * We want to drop from the next block forward in case this new size is
3968 * not block aligned since we will be keeping the last block of the
3969 * extent just the way it is.
3970 */
Josef Bacik0af3d002010-06-21 14:48:16 -04003971 if (root->ref_cows || root == root->fs_info->tree_root)
Qu Wenruofda28322013-02-26 08:10:22 +00003972 btrfs_drop_extent_cache(inode, ALIGN(new_size,
3973 root->sectorsize), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003974
Miao Xie16cdcec2011-04-22 18:12:22 +08003975 /*
3976 * This function is also used to drop the items in the log tree before
3977 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
3978 * it is used to drop the loged items. So we shouldn't kill the delayed
3979 * items.
3980 */
3981 if (min_type == 0 && root == BTRFS_I(inode)->root)
3982 btrfs_kill_delayed_inode_items(inode);
3983
Li Zefan33345d012011-04-20 10:31:50 +08003984 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04003985 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04003986 key.type = (u8)-1;
3987
Chris Mason85e21ba2008-01-29 15:11:36 -05003988search_again:
Chris Masonb9473432009-03-13 11:00:37 -04003989 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05003990 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00003991 if (ret < 0) {
3992 err = ret;
3993 goto out;
3994 }
Chris Masond3977122009-01-05 21:25:51 -05003995
Chris Mason85e21ba2008-01-29 15:11:36 -05003996 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003997 /* there are no items in the tree for us to truncate, we're
3998 * done
3999 */
Yan, Zheng80825102009-11-12 09:35:36 +00004000 if (path->slots[0] == 0)
4001 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004002 path->slots[0]--;
4003 }
4004
Chris Masond3977122009-01-05 21:25:51 -05004005 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004006 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004007 leaf = path->nodes[0];
4008 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4009 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04004010
Li Zefan33345d012011-04-20 10:31:50 +08004011 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004012 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004013
Chris Mason85e21ba2008-01-29 15:11:36 -05004014 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004015 break;
4016
Chris Mason5f39d392007-10-15 16:14:19 -04004017 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004018 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004019 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004020 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004021 extent_type = btrfs_file_extent_type(leaf, fi);
4022 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004023 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004024 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04004025 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004026 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004027 path->slots[0], fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004028 }
Yan008630c2007-11-07 13:31:09 -05004029 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004030 }
Yan, Zheng80825102009-11-12 09:35:36 +00004031 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004032 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004033 } else {
4034 if (item_end < new_size)
4035 break;
4036 if (found_key.offset >= new_size)
4037 del_item = 1;
4038 else
4039 del_item = 0;
4040 }
Chris Mason39279cc2007-06-12 06:35:45 -04004041 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004042 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004043 if (found_type != BTRFS_EXTENT_DATA_KEY)
4044 goto delete;
4045
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004046 if (del_item)
4047 last_size = found_key.offset;
4048 else
4049 last_size = new_size;
4050
Chris Mason179e29e2007-11-01 11:28:41 -04004051 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004052 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004053 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004054 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004055 u64 orig_num_bytes =
4056 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004057 extent_num_bytes = ALIGN(new_size -
4058 found_key.offset,
4059 root->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004060 btrfs_set_file_extent_num_bytes(leaf, fi,
4061 extent_num_bytes);
4062 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004063 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04004064 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004065 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004066 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004067 } else {
Chris Masondb945352007-10-15 16:15:53 -04004068 extent_num_bytes =
4069 btrfs_file_extent_disk_num_bytes(leaf,
4070 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004071 extent_offset = found_key.offset -
4072 btrfs_file_extent_offset(leaf, fi);
4073
Chris Mason39279cc2007-06-12 06:35:45 -04004074 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004075 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004076 if (extent_start != 0) {
4077 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04004078 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004079 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004080 }
4081 }
Chris Mason90692182008-02-08 13:49:28 -05004082 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004083 /*
4084 * we can't truncate inline items that have had
4085 * special encodings
4086 */
4087 if (!del_item &&
4088 btrfs_file_extent_compression(leaf, fi) == 0 &&
4089 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4090 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004091 u32 size = new_size - found_key.offset;
4092
4093 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004094 inode_sub_bytes(inode, item_end + 1 -
4095 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04004096 }
Chris Mason514ac8a2014-01-03 21:07:00 -08004097
4098 /*
4099 * update the ram bytes to properly reflect
4100 * the new size of our item
4101 */
4102 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
Chris Masone02119d2008-09-05 16:13:11 -04004103 size =
4104 btrfs_file_extent_calc_inline_size(size);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004105 btrfs_truncate_item(root, path, size, 1);
Chris Masone02119d2008-09-05 16:13:11 -04004106 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004107 inode_sub_bytes(inode, item_end + 1 -
4108 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05004109 }
Chris Mason39279cc2007-06-12 06:35:45 -04004110 }
Chris Mason179e29e2007-11-01 11:28:41 -04004111delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004112 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004113 if (!pending_del_nr) {
4114 /* no pending yet, add ourselves */
4115 pending_del_slot = path->slots[0];
4116 pending_del_nr = 1;
4117 } else if (pending_del_nr &&
4118 path->slots[0] + 1 == pending_del_slot) {
4119 /* hop on the pending chunk */
4120 pending_del_nr++;
4121 pending_del_slot = path->slots[0];
4122 } else {
Chris Masond3977122009-01-05 21:25:51 -05004123 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004124 }
Chris Mason39279cc2007-06-12 06:35:45 -04004125 } else {
4126 break;
4127 }
Josef Bacik0af3d002010-06-21 14:48:16 -04004128 if (found_extent && (root->ref_cows ||
4129 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004130 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004131 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004132 extent_num_bytes, 0,
4133 btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02004134 ino, extent_offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004135 BUG_ON(ret);
4136 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004137
Yan, Zheng80825102009-11-12 09:35:36 +00004138 if (found_type == BTRFS_INODE_ITEM_KEY)
4139 break;
4140
4141 if (path->slots[0] == 0 ||
4142 path->slots[0] != pending_del_slot) {
Yan, Zheng80825102009-11-12 09:35:36 +00004143 if (pending_del_nr) {
4144 ret = btrfs_del_items(trans, root, path,
4145 pending_del_slot,
4146 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004147 if (ret) {
4148 btrfs_abort_transaction(trans,
4149 root, ret);
4150 goto error;
4151 }
Yan, Zheng80825102009-11-12 09:35:36 +00004152 pending_del_nr = 0;
4153 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004154 btrfs_release_path(path);
Chris Mason85e21ba2008-01-29 15:11:36 -05004155 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004156 } else {
4157 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004158 }
Chris Mason39279cc2007-06-12 06:35:45 -04004159 }
Yan, Zheng80825102009-11-12 09:35:36 +00004160out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004161 if (pending_del_nr) {
4162 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4163 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004164 if (ret)
4165 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004166 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004167error:
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004168 if (last_size != (u64)-1)
4169 btrfs_ordered_update_i_size(inode, last_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004170 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00004171 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004172}
4173
Chris Masona52d9a82007-08-27 16:49:44 -04004174/*
Josef Bacik2aaa6652012-08-29 14:27:18 -04004175 * btrfs_truncate_page - read, zero a chunk and write a page
4176 * @inode - inode that we're zeroing
4177 * @from - the offset to start zeroing
4178 * @len - the length to zero, 0 to zero the entire range respective to the
4179 * offset
4180 * @front - zero up to the offset instead of from the offset on
4181 *
4182 * This will find the page for the "from" offset and cow the page and zero the
4183 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004184 */
Josef Bacik2aaa6652012-08-29 14:27:18 -04004185int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
4186 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004187{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004188 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04004189 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004190 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4191 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004192 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004193 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04004194 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04004195 pgoff_t index = from >> PAGE_CACHE_SHIFT;
4196 unsigned offset = from & (PAGE_CACHE_SIZE-1);
4197 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004198 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004199 int ret = 0;
4200 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004201 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004202
Josef Bacik2aaa6652012-08-29 14:27:18 -04004203 if ((offset & (blocksize - 1)) == 0 &&
4204 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004205 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004206 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004207 if (ret)
4208 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004209
Chris Mason211c17f2008-05-15 09:13:45 -04004210again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004211 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004212 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004213 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Miao Xieac6a2b32012-12-05 10:56:13 +00004214 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004215 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004216 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004217
4218 page_start = page_offset(page);
4219 page_end = page_start + PAGE_CACHE_SIZE - 1;
4220
Chris Masona52d9a82007-08-27 16:49:44 -04004221 if (!PageUptodate(page)) {
4222 ret = btrfs_readpage(NULL, page);
4223 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004224 if (page->mapping != mapping) {
4225 unlock_page(page);
4226 page_cache_release(page);
4227 goto again;
4228 }
Chris Masona52d9a82007-08-27 16:49:44 -04004229 if (!PageUptodate(page)) {
4230 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004231 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004232 }
4233 }
Chris Mason211c17f2008-05-15 09:13:45 -04004234 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004235
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004236 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004237 set_page_extent_mapped(page);
4238
4239 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4240 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004241 unlock_extent_cached(io_tree, page_start, page_end,
4242 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004243 unlock_page(page);
4244 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004245 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004246 btrfs_put_ordered_extent(ordered);
4247 goto again;
4248 }
4249
Josef Bacik2ac55d42010-02-03 19:33:23 +00004250 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004251 EXTENT_DIRTY | EXTENT_DELALLOC |
4252 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004253 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004254
Josef Bacik2ac55d42010-02-03 19:33:23 +00004255 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
4256 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004257 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004258 unlock_extent_cached(io_tree, page_start, page_end,
4259 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004260 goto out_unlock;
4261 }
4262
Chris Masone6dcd2d2008-07-17 12:53:50 -04004263 if (offset != PAGE_CACHE_SIZE) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004264 if (!len)
4265 len = PAGE_CACHE_SIZE - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004266 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004267 if (front)
4268 memset(kaddr, 0, offset);
4269 else
4270 memset(kaddr + offset, 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004271 flush_dcache_page(page);
4272 kunmap(page);
4273 }
Chris Mason247e7432008-07-17 12:53:51 -04004274 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004275 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004276 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
4277 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004278
Chris Mason89642222008-07-24 09:41:53 -04004279out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004280 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004281 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04004282 unlock_page(page);
4283 page_cache_release(page);
4284out:
4285 return ret;
4286}
4287
Josef Bacik16e75492013-10-22 12:18:51 -04004288static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4289 u64 offset, u64 len)
4290{
4291 struct btrfs_trans_handle *trans;
4292 int ret;
4293
4294 /*
4295 * Still need to make sure the inode looks like it's been updated so
4296 * that any holes get logged if we fsync.
4297 */
4298 if (btrfs_fs_incompat(root->fs_info, NO_HOLES)) {
4299 BTRFS_I(inode)->last_trans = root->fs_info->generation;
4300 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4301 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4302 return 0;
4303 }
4304
4305 /*
4306 * 1 - for the one we're dropping
4307 * 1 - for the one we're adding
4308 * 1 - for updating the inode.
4309 */
4310 trans = btrfs_start_transaction(root, 3);
4311 if (IS_ERR(trans))
4312 return PTR_ERR(trans);
4313
4314 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4315 if (ret) {
4316 btrfs_abort_transaction(trans, root, ret);
4317 btrfs_end_transaction(trans, root);
4318 return ret;
4319 }
4320
4321 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset,
4322 0, 0, len, 0, len, 0, 0, 0);
4323 if (ret)
4324 btrfs_abort_transaction(trans, root, ret);
4325 else
4326 btrfs_update_inode(trans, root, inode);
4327 btrfs_end_transaction(trans, root);
4328 return ret;
4329}
4330
Josef Bacik695a0d02011-03-04 15:46:53 -05004331/*
4332 * This function puts in dummy file extents for the area we're creating a hole
4333 * for. So if we are truncating this file to a larger size we need to insert
4334 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4335 * the range between oldsize and size
4336 */
Josef Bacika41ad392011-01-31 15:30:16 -05004337int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004338{
Yan Zheng9036c102008-10-30 14:19:41 -04004339 struct btrfs_root *root = BTRFS_I(inode)->root;
4340 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004341 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004342 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004343 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Qu Wenruofda28322013-02-26 08:10:22 +00004344 u64 hole_start = ALIGN(oldsize, root->sectorsize);
4345 u64 block_end = ALIGN(size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004346 u64 last_byte;
4347 u64 cur_offset;
4348 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004349 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004350
Josef Bacika71754f2013-06-17 17:14:39 -04004351 /*
4352 * If our size started in the middle of a page we need to zero out the
4353 * rest of the page before we expand the i_size, otherwise we could
4354 * expose stale data.
4355 */
4356 err = btrfs_truncate_page(inode, oldsize, 0, 0);
4357 if (err)
4358 return err;
4359
Yan Zheng9036c102008-10-30 14:19:41 -04004360 if (size <= hole_start)
4361 return 0;
4362
Yan Zheng9036c102008-10-30 14:19:41 -04004363 while (1) {
4364 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004365
Josef Bacik2ac55d42010-02-03 19:33:23 +00004366 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004367 &cached_state);
Miao Xiefa7c1492013-09-26 13:15:27 +08004368 ordered = btrfs_lookup_ordered_range(inode, hole_start,
4369 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004370 if (!ordered)
4371 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004372 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4373 &cached_state, GFP_NOFS);
Miao Xiefa7c1492013-09-26 13:15:27 +08004374 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004375 btrfs_put_ordered_extent(ordered);
4376 }
4377
Yan Zheng9036c102008-10-30 14:19:41 -04004378 cur_offset = hole_start;
4379 while (1) {
4380 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4381 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004382 if (IS_ERR(em)) {
4383 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004384 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004385 break;
4386 }
Yan Zheng9036c102008-10-30 14:19:41 -04004387 last_byte = min(extent_map_end(em), block_end);
Qu Wenruofda28322013-02-26 08:10:22 +00004388 last_byte = ALIGN(last_byte , root->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004389 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004390 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004391 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004392
Josef Bacik16e75492013-10-22 12:18:51 -04004393 err = maybe_insert_hole(root, inode, cur_offset,
4394 hole_size);
4395 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004396 break;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004397 btrfs_drop_extent_cache(inode, cur_offset,
4398 cur_offset + hole_size - 1, 0);
4399 hole_em = alloc_extent_map();
4400 if (!hole_em) {
4401 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4402 &BTRFS_I(inode)->runtime_flags);
4403 goto next;
4404 }
4405 hole_em->start = cur_offset;
4406 hole_em->len = hole_size;
4407 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004408
Josef Bacik5dc562c2012-08-17 13:14:17 -04004409 hole_em->block_start = EXTENT_MAP_HOLE;
4410 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004411 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004412 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004413 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
4414 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Josef Bacik16e75492013-10-22 12:18:51 -04004415 hole_em->generation = root->fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004416
4417 while (1) {
4418 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004419 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004420 write_unlock(&em_tree->lock);
4421 if (err != -EEXIST)
4422 break;
4423 btrfs_drop_extent_cache(inode, cur_offset,
4424 cur_offset +
4425 hole_size - 1, 0);
4426 }
4427 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04004428 }
Josef Bacik16e75492013-10-22 12:18:51 -04004429next:
Yan Zheng9036c102008-10-30 14:19:41 -04004430 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004431 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004432 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004433 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004434 break;
4435 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004436 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004437 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4438 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004439 return err;
4440}
4441
Eric Sandeen3972f262013-01-12 02:57:22 +00004442static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004443{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004444 struct btrfs_root *root = BTRFS_I(inode)->root;
4445 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004446 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004447 loff_t newsize = attr->ia_size;
4448 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004449 int ret;
4450
Eric Sandeen3972f262013-01-12 02:57:22 +00004451 /*
4452 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4453 * special case where we need to update the times despite not having
4454 * these flags set. For all other operations the VFS set these flags
4455 * explicitly if it wants a timestamp update.
4456 */
4457 if (newsize != oldsize && (!(mask & (ATTR_CTIME | ATTR_MTIME))))
4458 inode->i_ctime = inode->i_mtime = current_fs_time(inode->i_sb);
4459
Josef Bacika41ad392011-01-31 15:30:16 -05004460 if (newsize > oldsize) {
Kirill A. Shutemov7caef262013-09-12 15:13:56 -07004461 truncate_pagecache(inode, newsize);
Josef Bacika41ad392011-01-31 15:30:16 -05004462 ret = btrfs_cont_expand(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004463 if (ret)
Yan, Zheng80825102009-11-12 09:35:36 +00004464 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004465
Miao Xief4a2f4c2011-12-14 20:12:01 -05004466 trans = btrfs_start_transaction(root, 1);
4467 if (IS_ERR(trans))
4468 return PTR_ERR(trans);
4469
4470 i_size_write(inode, newsize);
4471 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
4472 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004473 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05004474 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004475
Josef Bacika41ad392011-01-31 15:30:16 -05004476 /*
4477 * We're truncating a file that used to have good data down to
4478 * zero. Make sure it gets into the ordered flush list so that
4479 * any new writes get down to disk quickly.
4480 */
4481 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004482 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4483 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004484
Josef Bacikf3fe8202013-01-07 17:03:21 -05004485 /*
4486 * 1 for the orphan item we're going to add
4487 * 1 for the orphan item deletion.
4488 */
4489 trans = btrfs_start_transaction(root, 2);
4490 if (IS_ERR(trans))
4491 return PTR_ERR(trans);
4492
4493 /*
4494 * We need to do this in case we fail at _any_ point during the
4495 * actual truncate. Once we do the truncate_setsize we could
4496 * invalidate pages which forces any outstanding ordered io to
4497 * be instantly completed which will give us extents that need
4498 * to be truncated. If we fail to get an orphan inode down we
4499 * could have left over extents that were never meant to live,
4500 * so we need to garuntee from this point on that everything
4501 * will be consistent.
4502 */
4503 ret = btrfs_orphan_add(trans, inode);
4504 btrfs_end_transaction(trans, root);
4505 if (ret)
4506 return ret;
4507
Josef Bacika41ad392011-01-31 15:30:16 -05004508 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4509 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004510
4511 /* Disable nonlocked read DIO to avoid the end less truncate */
4512 btrfs_inode_block_unlocked_dio(inode);
4513 inode_dio_wait(inode);
4514 btrfs_inode_resume_unlocked_dio(inode);
4515
Josef Bacika41ad392011-01-31 15:30:16 -05004516 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004517 if (ret && inode->i_nlink) {
4518 int err;
4519
4520 /*
4521 * failed to truncate, disk_i_size is only adjusted down
4522 * as we remove extents, so it should represent the true
4523 * size of the inode, so reset the in memory size and
4524 * delete our orphan entry.
4525 */
4526 trans = btrfs_join_transaction(root);
4527 if (IS_ERR(trans)) {
4528 btrfs_orphan_del(NULL, inode);
4529 return ret;
4530 }
4531 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
4532 err = btrfs_orphan_del(trans, inode);
4533 if (err)
4534 btrfs_abort_transaction(trans, root, err);
4535 btrfs_end_transaction(trans, root);
4536 }
Yan, Zheng80825102009-11-12 09:35:36 +00004537 }
4538
Josef Bacika41ad392011-01-31 15:30:16 -05004539 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004540}
4541
Chris Mason39279cc2007-06-12 06:35:45 -04004542static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
4543{
4544 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08004545 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004546 int err;
4547
Li Zefanb83cc962010-12-20 16:04:08 +08004548 if (btrfs_root_readonly(root))
4549 return -EROFS;
4550
Chris Mason39279cc2007-06-12 06:35:45 -04004551 err = inode_change_ok(inode, attr);
4552 if (err)
4553 return err;
4554
Chris Mason5a3f23d2009-03-31 13:27:11 -04004555 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00004556 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00004557 if (err)
4558 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004559 }
Yan Zheng9036c102008-10-30 14:19:41 -04004560
Christoph Hellwig10257742010-06-04 11:30:02 +02004561 if (attr->ia_valid) {
4562 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004563 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004564 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04004565
Josef Bacik22c44fe2011-11-30 10:45:38 -05004566 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig10257742010-06-04 11:30:02 +02004567 err = btrfs_acl_chmod(inode);
4568 }
4569
Chris Mason39279cc2007-06-12 06:35:45 -04004570 return err;
4571}
Chris Mason61295eb2008-01-14 16:24:38 -05004572
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004573/*
4574 * While truncating the inode pages during eviction, we get the VFS calling
4575 * btrfs_invalidatepage() against each page of the inode. This is slow because
4576 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
4577 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
4578 * extent_state structures over and over, wasting lots of time.
4579 *
4580 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
4581 * those expensive operations on a per page basis and do only the ordered io
4582 * finishing, while we release here the extent_map and extent_state structures,
4583 * without the excessive merging and splitting.
4584 */
4585static void evict_inode_truncate_pages(struct inode *inode)
4586{
4587 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4588 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
4589 struct rb_node *node;
4590
4591 ASSERT(inode->i_state & I_FREEING);
4592 truncate_inode_pages(&inode->i_data, 0);
4593
4594 write_lock(&map_tree->lock);
4595 while (!RB_EMPTY_ROOT(&map_tree->map)) {
4596 struct extent_map *em;
4597
4598 node = rb_first(&map_tree->map);
4599 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08004600 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
4601 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004602 remove_extent_mapping(map_tree, em);
4603 free_extent_map(em);
4604 }
4605 write_unlock(&map_tree->lock);
4606
4607 spin_lock(&io_tree->lock);
4608 while (!RB_EMPTY_ROOT(&io_tree->state)) {
4609 struct extent_state *state;
4610 struct extent_state *cached_state = NULL;
4611
4612 node = rb_first(&io_tree->state);
4613 state = rb_entry(node, struct extent_state, rb_node);
4614 atomic_inc(&state->refs);
4615 spin_unlock(&io_tree->lock);
4616
4617 lock_extent_bits(io_tree, state->start, state->end,
4618 0, &cached_state);
4619 clear_extent_bit(io_tree, state->start, state->end,
4620 EXTENT_LOCKED | EXTENT_DIRTY |
4621 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
4622 EXTENT_DEFRAG, 1, 1,
4623 &cached_state, GFP_NOFS);
4624 free_extent_state(state);
4625
4626 spin_lock(&io_tree->lock);
4627 }
4628 spin_unlock(&io_tree->lock);
4629}
4630
Al Virobd555972010-06-07 11:35:40 -04004631void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004632{
4633 struct btrfs_trans_handle *trans;
4634 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04004635 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik07127182011-08-19 10:29:59 -04004636 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04004637 int ret;
4638
liubo1abe9b82011-03-24 11:18:59 +00004639 trace_btrfs_inode_evict(inode);
4640
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004641 evict_inode_truncate_pages(inode);
4642
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02004643 if (inode->i_nlink &&
4644 ((btrfs_root_refs(&root->root_item) != 0 &&
4645 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
4646 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04004647 goto no_delete;
4648
Chris Mason39279cc2007-06-12 06:35:45 -04004649 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04004650 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004651 goto no_delete;
4652 }
Al Virobd555972010-06-07 11:35:40 -04004653 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04004654 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04004655
Yan, Zhengc71bf092009-11-12 09:34:40 +00004656 if (root->fs_info->log_root_recovering) {
Liu Bo6bf02312012-06-25 21:59:09 -06004657 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04004658 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00004659 goto no_delete;
4660 }
4661
Yan, Zheng76dda932009-09-21 16:00:26 -04004662 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02004663 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
4664 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04004665 goto no_delete;
4666 }
4667
Miao Xie0e8c36a2012-12-19 06:59:51 +00004668 ret = btrfs_commit_inode_delayed_inode(inode);
4669 if (ret) {
4670 btrfs_orphan_del(NULL, inode);
4671 goto no_delete;
4672 }
4673
Miao Xie66d8f3d2012-09-06 04:02:28 -06004674 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04004675 if (!rsv) {
4676 btrfs_orphan_del(NULL, inode);
4677 goto no_delete;
4678 }
Josef Bacik4a338542011-08-29 11:01:31 -04004679 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04004680 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04004681 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04004682
Chris Masondbe674a2008-07-17 12:54:05 -04004683 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04004684
Josef Bacik4289a662011-08-05 13:22:24 -04004685 /*
Miao Xie8407aa42012-09-07 01:43:32 -06004686 * This is a bit simpler than btrfs_truncate since we've already
4687 * reserved our space for our orphan item in the unlink, so we just
4688 * need to reserve some slack space in case we add bytes and update
4689 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04004690 */
Yan, Zheng80825102009-11-12 09:35:36 +00004691 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00004692 ret = btrfs_block_rsv_refill(root, rsv, min_size,
4693 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00004694
Josef Bacik726c35f2011-09-26 15:46:06 -04004695 /*
4696 * Try and steal from the global reserve since we will
4697 * likely not use this space anyway, we want to try as
4698 * hard as possible to get this to work.
4699 */
4700 if (ret)
4701 ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
4702
Yan, Zhengd68fc572010-05-16 10:49:58 -04004703 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004704 btrfs_warn(root->fs_info,
4705 "Could not get space for a delete, will truncate on mount %d",
4706 ret);
Josef Bacik4289a662011-08-05 13:22:24 -04004707 btrfs_orphan_del(NULL, inode);
4708 btrfs_free_block_rsv(root, rsv);
4709 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04004710 }
4711
Miao Xie0e8c36a2012-12-19 06:59:51 +00004712 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04004713 if (IS_ERR(trans)) {
4714 btrfs_orphan_del(NULL, inode);
4715 btrfs_free_block_rsv(root, rsv);
4716 goto no_delete;
4717 }
4718
4719 trans->block_rsv = rsv;
4720
Yan, Zhengd68fc572010-05-16 10:49:58 -04004721 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04004722 if (ret != -ENOSPC)
Yan, Zheng80825102009-11-12 09:35:36 +00004723 break;
4724
Miao Xie8407aa42012-09-07 01:43:32 -06004725 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004726 btrfs_end_transaction(trans, root);
4727 trans = NULL;
Liu Bob53d3f52012-11-14 14:34:34 +00004728 btrfs_btree_balance_dirty(root);
Josef Bacik7b128762008-07-24 12:17:14 -04004729 }
4730
Josef Bacik4289a662011-08-05 13:22:24 -04004731 btrfs_free_block_rsv(root, rsv);
4732
Josef Bacik4ef31a42013-08-13 14:10:08 -04004733 /*
4734 * Errors here aren't a big deal, it just means we leave orphan items
4735 * in the tree. They will be cleaned up on the next mount.
4736 */
Yan, Zheng80825102009-11-12 09:35:36 +00004737 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04004738 trans->block_rsv = root->orphan_block_rsv;
Josef Bacik4ef31a42013-08-13 14:10:08 -04004739 btrfs_orphan_del(trans, inode);
4740 } else {
4741 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004742 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004743
Josef Bacik4289a662011-08-05 13:22:24 -04004744 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08004745 if (!(root == root->fs_info->tree_root ||
4746 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08004747 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08004748
Chris Mason54aa1f42007-06-22 14:16:25 -04004749 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004750 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04004751no_delete:
Miao Xie89042e52013-05-15 07:48:15 +00004752 btrfs_remove_delayed_node(inode);
Jan Karadbd57682012-05-03 14:48:02 +02004753 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004754 return;
Chris Mason39279cc2007-06-12 06:35:45 -04004755}
4756
4757/*
4758 * this returns the key found in the dir entry in the location pointer.
4759 * If no dir entries were found, location->objectid is 0.
4760 */
4761static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
4762 struct btrfs_key *location)
4763{
4764 const char *name = dentry->d_name.name;
4765 int namelen = dentry->d_name.len;
4766 struct btrfs_dir_item *di;
4767 struct btrfs_path *path;
4768 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04004769 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004770
4771 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07004772 if (!path)
4773 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05004774
Li Zefan33345d012011-04-20 10:31:50 +08004775 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04004776 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04004777 if (IS_ERR(di))
4778 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05004779
David Sterbac7040052011-04-19 18:00:01 +02004780 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05004781 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05004782
Chris Mason5f39d392007-10-15 16:14:19 -04004783 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04004784out:
Chris Mason39279cc2007-06-12 06:35:45 -04004785 btrfs_free_path(path);
4786 return ret;
Chris Mason39544012007-12-12 14:38:19 -05004787out_err:
4788 location->objectid = 0;
4789 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04004790}
4791
4792/*
4793 * when we hit a tree root in a directory, the btrfs part of the inode
4794 * needs to be changed to reflect the root directory of the tree root. This
4795 * is kind of like crossing a mount point.
4796 */
4797static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004798 struct inode *dir,
4799 struct dentry *dentry,
4800 struct btrfs_key *location,
4801 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04004802{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004803 struct btrfs_path *path;
4804 struct btrfs_root *new_root;
4805 struct btrfs_root_ref *ref;
4806 struct extent_buffer *leaf;
4807 int ret;
4808 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004809
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004810 path = btrfs_alloc_path();
4811 if (!path) {
4812 err = -ENOMEM;
4813 goto out;
4814 }
Chris Mason39279cc2007-06-12 06:35:45 -04004815
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004816 err = -ENOENT;
Kelley Nielsen75ac2dd2013-11-04 19:35:58 -08004817 ret = btrfs_find_item(root->fs_info->tree_root, path,
4818 BTRFS_I(dir)->root->root_key.objectid,
4819 location->objectid, BTRFS_ROOT_REF_KEY, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004820 if (ret) {
4821 if (ret < 0)
4822 err = ret;
4823 goto out;
4824 }
Chris Mason39279cc2007-06-12 06:35:45 -04004825
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004826 leaf = path->nodes[0];
4827 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08004828 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004829 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
4830 goto out;
4831
4832 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
4833 (unsigned long)(ref + 1),
4834 dentry->d_name.len);
4835 if (ret)
4836 goto out;
4837
David Sterbab3b4aa72011-04-21 01:20:15 +02004838 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004839
4840 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
4841 if (IS_ERR(new_root)) {
4842 err = PTR_ERR(new_root);
4843 goto out;
4844 }
4845
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004846 *sub_root = new_root;
4847 location->objectid = btrfs_root_dirid(&new_root->root_item);
4848 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04004849 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004850 err = 0;
4851out:
4852 btrfs_free_path(path);
4853 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004854}
4855
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004856static void inode_tree_add(struct inode *inode)
4857{
4858 struct btrfs_root *root = BTRFS_I(inode)->root;
4859 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004860 struct rb_node **p;
4861 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01004862 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Li Zefan33345d012011-04-20 10:31:50 +08004863 u64 ino = btrfs_ino(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004864
Al Viro1d3382cb2010-10-23 15:19:20 -04004865 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004866 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00004867 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004868 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00004869 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004870 while (*p) {
4871 parent = *p;
4872 entry = rb_entry(parent, struct btrfs_inode, rb_node);
4873
Li Zefan33345d012011-04-20 10:31:50 +08004874 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004875 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004876 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004877 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004878 else {
4879 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04004880 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01004881 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004882 RB_CLEAR_NODE(parent);
4883 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01004884 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004885 }
4886 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01004887 rb_link_node(new, parent, p);
4888 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004889 spin_unlock(&root->inode_lock);
4890}
4891
4892static void inode_tree_del(struct inode *inode)
4893{
4894 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04004895 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004896
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004897 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004898 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004899 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004900 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04004901 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004902 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004903 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04004904
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02004905 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004906 synchronize_srcu(&root->fs_info->subvol_srcu);
4907 spin_lock(&root->inode_lock);
4908 empty = RB_EMPTY_ROOT(&root->inode_tree);
4909 spin_unlock(&root->inode_lock);
4910 if (empty)
4911 btrfs_add_dead_root(root);
4912 }
4913}
4914
Jeff Mahoney143bede2012-03-01 14:56:26 +01004915void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04004916{
4917 struct rb_node *node;
4918 struct rb_node *prev;
4919 struct btrfs_inode *entry;
4920 struct inode *inode;
4921 u64 objectid = 0;
4922
4923 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4924
4925 spin_lock(&root->inode_lock);
4926again:
4927 node = root->inode_tree.rb_node;
4928 prev = NULL;
4929 while (node) {
4930 prev = node;
4931 entry = rb_entry(node, struct btrfs_inode, rb_node);
4932
Li Zefan33345d012011-04-20 10:31:50 +08004933 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004934 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004935 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004936 node = node->rb_right;
4937 else
4938 break;
4939 }
4940 if (!node) {
4941 while (prev) {
4942 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004943 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004944 node = prev;
4945 break;
4946 }
4947 prev = rb_next(prev);
4948 }
4949 }
4950 while (node) {
4951 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004952 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04004953 inode = igrab(&entry->vfs_inode);
4954 if (inode) {
4955 spin_unlock(&root->inode_lock);
4956 if (atomic_read(&inode->i_count) > 1)
4957 d_prune_aliases(inode);
4958 /*
Al Viro45321ac2010-06-07 13:43:19 -04004959 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04004960 * the inode cache when its usage count
4961 * hits zero.
4962 */
4963 iput(inode);
4964 cond_resched();
4965 spin_lock(&root->inode_lock);
4966 goto again;
4967 }
4968
4969 if (cond_resched_lock(&root->inode_lock))
4970 goto again;
4971
4972 node = rb_next(node);
4973 }
4974 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004975}
4976
Chris Masone02119d2008-09-05 16:13:11 -04004977static int btrfs_init_locked_inode(struct inode *inode, void *p)
4978{
4979 struct btrfs_iget_args *args = p;
4980 inode->i_ino = args->ino;
Chris Masone02119d2008-09-05 16:13:11 -04004981 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004982 return 0;
4983}
4984
4985static int btrfs_find_actor(struct inode *inode, void *opaque)
4986{
4987 struct btrfs_iget_args *args = opaque;
Li Zefan33345d012011-04-20 10:31:50 +08004988 return args->ino == btrfs_ino(inode) &&
Chris Masond3977122009-01-05 21:25:51 -05004989 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004990}
4991
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004992static struct inode *btrfs_iget_locked(struct super_block *s,
4993 u64 objectid,
4994 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04004995{
4996 struct inode *inode;
4997 struct btrfs_iget_args args;
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01004998 unsigned long hashval = btrfs_inode_hash(objectid, root);
4999
Chris Mason39279cc2007-06-12 06:35:45 -04005000 args.ino = objectid;
5001 args.root = root;
5002
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005003 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005004 btrfs_init_locked_inode,
5005 (void *)&args);
5006 return inode;
5007}
5008
Balaji Rao1a54ef82008-07-21 02:01:04 +05305009/* Get an inode object given its location and corresponding root.
5010 * Returns in *is_new if the inode was read from disk
5011 */
5012struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005013 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305014{
5015 struct inode *inode;
5016
5017 inode = btrfs_iget_locked(s, location->objectid, root);
5018 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005019 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305020
5021 if (inode->i_state & I_NEW) {
5022 BTRFS_I(inode)->root = root;
5023 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
5024 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005025 if (!is_bad_inode(inode)) {
5026 inode_tree_add(inode);
5027 unlock_new_inode(inode);
5028 if (new)
5029 *new = 1;
5030 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005031 unlock_new_inode(inode);
5032 iput(inode);
5033 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005034 }
5035 }
5036
Balaji Rao1a54ef82008-07-21 02:01:04 +05305037 return inode;
5038}
5039
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005040static struct inode *new_simple_dir(struct super_block *s,
5041 struct btrfs_key *key,
5042 struct btrfs_root *root)
5043{
5044 struct inode *inode = new_inode(s);
5045
5046 if (!inode)
5047 return ERR_PTR(-ENOMEM);
5048
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005049 BTRFS_I(inode)->root = root;
5050 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005051 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005052
5053 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005054 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005055 inode->i_fop = &simple_dir_operations;
5056 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
5057 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
5058
5059 return inode;
5060}
5061
Chris Mason3de45862008-11-17 21:02:50 -05005062struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005063{
Chris Masond3977122009-01-05 21:25:51 -05005064 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005065 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005066 struct btrfs_root *sub_root = root;
5067 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005068 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005069 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005070
5071 if (dentry->d_name.len > BTRFS_NAME_LEN)
5072 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005073
Jeff Layton39e3c952012-11-28 11:30:53 -05005074 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005075 if (ret < 0)
5076 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005077
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005078 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005079 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005080
5081 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005082 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005083 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005084 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005085
5086 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5087
Yan, Zheng76dda932009-09-21 16:00:26 -04005088 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005089 ret = fixup_tree_root_location(root, dir, dentry,
5090 &location, &sub_root);
5091 if (ret < 0) {
5092 if (ret != -ENOENT)
5093 inode = ERR_PTR(ret);
5094 else
5095 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5096 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005097 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005098 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005099 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
5100
Julia Lawall34d19ba2011-01-24 19:55:19 +00005101 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00005102 down_read(&root->fs_info->cleanup_work_sem);
5103 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005104 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005105 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005106 if (ret) {
5107 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005108 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005109 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005110 }
5111
Chris Mason3de45862008-11-17 21:02:50 -05005112 return inode;
5113}
5114
Nick Pigginfe15ce42011-01-07 17:49:23 +11005115static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005116{
5117 struct btrfs_root *root;
Li Zefan848cce02012-02-21 17:04:28 +08005118 struct inode *inode = dentry->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005119
Li Zefan848cce02012-02-21 17:04:28 +08005120 if (!inode && !IS_ROOT(dentry))
5121 inode = dentry->d_parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005122
Li Zefan848cce02012-02-21 17:04:28 +08005123 if (inode) {
5124 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005125 if (btrfs_root_refs(&root->root_item) == 0)
5126 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005127
5128 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5129 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005130 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005131 return 0;
5132}
5133
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005134static void btrfs_dentry_release(struct dentry *dentry)
5135{
5136 if (dentry->d_fsdata)
5137 kfree(dentry->d_fsdata);
5138}
5139
Chris Mason3de45862008-11-17 21:02:50 -05005140static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005141 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005142{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005143 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005144
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005145 inode = btrfs_lookup_dentry(dir, dentry);
5146 if (IS_ERR(inode)) {
5147 if (PTR_ERR(inode) == -ENOENT)
5148 inode = NULL;
5149 else
5150 return ERR_CAST(inode);
5151 }
5152
5153 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005154}
5155
Miao Xie16cdcec2011-04-22 18:12:22 +08005156unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005157 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5158};
5159
Al Viro9cdda8d2013-05-22 16:48:09 -04005160static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005161{
Al Viro9cdda8d2013-05-22 16:48:09 -04005162 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005163 struct btrfs_root *root = BTRFS_I(inode)->root;
5164 struct btrfs_item *item;
5165 struct btrfs_dir_item *di;
5166 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005167 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005168 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005169 struct list_head ins_list;
5170 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005171 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005172 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005173 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005174 unsigned char d_type;
5175 int over = 0;
5176 u32 di_cur;
5177 u32 di_total;
5178 u32 di_len;
5179 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005180 char tmp_name[32];
5181 char *name_ptr;
5182 int name_len;
Al Viro9cdda8d2013-05-22 16:48:09 -04005183 int is_curr = 0; /* ctx->pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04005184
5185 /* FIXME, use a real flag for deciding about the key type */
5186 if (root->fs_info->tree_root == root)
5187 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005188
Al Viro9cdda8d2013-05-22 16:48:09 -04005189 if (!dir_emit_dots(file, ctx))
5190 return 0;
5191
David Woodhouse49593bf2008-08-17 17:08:36 +01005192 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005193 if (!path)
5194 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005195
Josef Bacik026fd312011-05-13 10:32:11 -04005196 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005197
Miao Xie16cdcec2011-04-22 18:12:22 +08005198 if (key_type == BTRFS_DIR_INDEX_KEY) {
5199 INIT_LIST_HEAD(&ins_list);
5200 INIT_LIST_HEAD(&del_list);
5201 btrfs_get_delayed_items(inode, &ins_list, &del_list);
5202 }
5203
Chris Mason39279cc2007-06-12 06:35:45 -04005204 btrfs_set_key_type(&key, key_type);
Al Viro9cdda8d2013-05-22 16:48:09 -04005205 key.offset = ctx->pos;
Li Zefan33345d012011-04-20 10:31:50 +08005206 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005207
Chris Mason39279cc2007-06-12 06:35:45 -04005208 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5209 if (ret < 0)
5210 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005211
5212 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005213 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005214 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005215 if (slot >= btrfs_header_nritems(leaf)) {
5216 ret = btrfs_next_leaf(root, path);
5217 if (ret < 0)
5218 goto err;
5219 else if (ret > 0)
5220 break;
5221 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005222 }
Chris Mason3de45862008-11-17 21:02:50 -05005223
Ross Kirkdd3cc162013-09-16 15:58:09 +01005224 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04005225 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5226
5227 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005228 break;
Chris Mason5f39d392007-10-15 16:14:19 -04005229 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04005230 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005231 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005232 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08005233 if (key_type == BTRFS_DIR_INDEX_KEY &&
5234 btrfs_should_delete_dir_index(&del_list,
5235 found_key.offset))
5236 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005237
Al Viro9cdda8d2013-05-22 16:48:09 -04005238 ctx->pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08005239 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005240
Chris Mason39279cc2007-06-12 06:35:45 -04005241 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
5242 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005243 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01005244
5245 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04005246 struct btrfs_key location;
5247
Josef Bacik22a94d42011-03-16 16:47:17 -04005248 if (verify_dir_item(root, leaf, di))
5249 break;
5250
Chris Mason5f39d392007-10-15 16:14:19 -04005251 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01005252 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04005253 name_ptr = tmp_name;
5254 } else {
5255 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01005256 if (!name_ptr) {
5257 ret = -ENOMEM;
5258 goto err;
5259 }
Chris Mason5f39d392007-10-15 16:14:19 -04005260 }
5261 read_extent_buffer(leaf, name_ptr,
5262 (unsigned long)(di + 1), name_len);
5263
5264 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5265 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05005266
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005267
Chris Mason3de45862008-11-17 21:02:50 -05005268 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01005269 * skip it.
5270 *
5271 * In contrast to old kernels, we insert the snapshot's
5272 * dir item and dir index after it has been created, so
5273 * we won't find a reference to our own snapshot. We
5274 * still keep the following code for backward
5275 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05005276 */
5277 if (location.type == BTRFS_ROOT_ITEM_KEY &&
5278 location.objectid == root->root_key.objectid) {
5279 over = 0;
5280 goto skip;
5281 }
Al Viro9cdda8d2013-05-22 16:48:09 -04005282 over = !dir_emit(ctx, name_ptr, name_len,
5283 location.objectid, d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04005284
Chris Mason3de45862008-11-17 21:02:50 -05005285skip:
Chris Mason5f39d392007-10-15 16:14:19 -04005286 if (name_ptr != tmp_name)
5287 kfree(name_ptr);
5288
Chris Mason39279cc2007-06-12 06:35:45 -04005289 if (over)
5290 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05005291 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01005292 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04005293 di_cur += di_len;
5294 di = (struct btrfs_dir_item *)((char *)di + di_len);
5295 }
Li Zefanb9e03af2011-03-23 10:43:58 +08005296next:
5297 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005298 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005299
Miao Xie16cdcec2011-04-22 18:12:22 +08005300 if (key_type == BTRFS_DIR_INDEX_KEY) {
5301 if (is_curr)
Al Viro9cdda8d2013-05-22 16:48:09 -04005302 ctx->pos++;
5303 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005304 if (ret)
5305 goto nopos;
5306 }
5307
David Woodhouse49593bf2008-08-17 17:08:36 +01005308 /* Reached end of directory/root. Bump pos past the last item. */
Zach Browndb62efb2013-07-11 16:19:42 -07005309 ctx->pos++;
5310
5311 /*
5312 * Stop new entries from being returned after we return the last
5313 * entry.
5314 *
5315 * New directory entries are assigned a strictly increasing
5316 * offset. This means that new entries created during readdir
5317 * are *guaranteed* to be seen in the future by that readdir.
5318 * This has broken buggy programs which operate on names as
5319 * they're returned by readdir. Until we re-use freed offsets
5320 * we have this hack to stop new entries from being returned
5321 * under the assumption that they'll never reach this huge
5322 * offset.
5323 *
5324 * This is being careful not to overflow 32bit loff_t unless the
5325 * last entry requires it because doing so has broken 32bit apps
5326 * in the past.
5327 */
5328 if (key_type == BTRFS_DIR_INDEX_KEY) {
5329 if (ctx->pos >= INT_MAX)
5330 ctx->pos = LLONG_MAX;
5331 else
5332 ctx->pos = INT_MAX;
5333 }
Chris Mason39279cc2007-06-12 06:35:45 -04005334nopos:
5335 ret = 0;
5336err:
Miao Xie16cdcec2011-04-22 18:12:22 +08005337 if (key_type == BTRFS_DIR_INDEX_KEY)
5338 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005339 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005340 return ret;
5341}
5342
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005343int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005344{
5345 struct btrfs_root *root = BTRFS_I(inode)->root;
5346 struct btrfs_trans_handle *trans;
5347 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005348 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005349
Josef Bacik72ac3c02012-05-23 14:13:11 -04005350 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005351 return 0;
5352
Liu Bo83eea1f2012-07-10 05:28:39 -06005353 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08005354 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005355
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005356 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005357 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005358 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005359 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005360 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005361 if (IS_ERR(trans))
5362 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06005363 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005364 }
5365 return ret;
5366}
5367
5368/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005369 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005370 * inode changes. But, it is most likely to find the inode in cache.
5371 * FIXME, needs more benchmarking...there are no reasons other than performance
5372 * to keep or drop this code.
5373 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005374static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005375{
5376 struct btrfs_root *root = BTRFS_I(inode)->root;
5377 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005378 int ret;
5379
Josef Bacik72ac3c02012-05-23 14:13:11 -04005380 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005381 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005382
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005383 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005384 if (IS_ERR(trans))
5385 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005386
5387 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005388 if (ret && ret == -ENOSPC) {
5389 /* whoops, lets try again with the full transaction */
5390 btrfs_end_transaction(trans, root);
5391 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005392 if (IS_ERR(trans))
5393 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005394
Chris Mason94b60442010-05-26 11:02:00 -04005395 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005396 }
Chris Mason39279cc2007-06-12 06:35:45 -04005397 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08005398 if (BTRFS_I(inode)->delayed_node)
5399 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005400
5401 return ret;
5402}
5403
5404/*
5405 * This is a copy of file_update_time. We need this so we can return error on
5406 * ENOSPC for updating the inode in the case of file write and mmap writes.
5407 */
Josef Bacike41f9412012-03-26 09:46:47 -04005408static int btrfs_update_time(struct inode *inode, struct timespec *now,
5409 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005410{
Alexander Block2bc5565282012-06-15 09:49:33 +02005411 struct btrfs_root *root = BTRFS_I(inode)->root;
5412
5413 if (btrfs_root_readonly(root))
5414 return -EROFS;
5415
Josef Bacike41f9412012-03-26 09:46:47 -04005416 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005417 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04005418 if (flags & S_CTIME)
5419 inode->i_ctime = *now;
5420 if (flags & S_MTIME)
5421 inode->i_mtime = *now;
5422 if (flags & S_ATIME)
5423 inode->i_atime = *now;
5424 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005425}
5426
Chris Masond352ac62008-09-29 15:18:18 -04005427/*
5428 * find the highest existing sequence number in a directory
5429 * and then set the in-memory index_cnt variable to reflect
5430 * free sequence numbers
5431 */
Josef Bacikaec74772008-07-24 12:12:38 -04005432static int btrfs_set_inode_index_count(struct inode *inode)
5433{
5434 struct btrfs_root *root = BTRFS_I(inode)->root;
5435 struct btrfs_key key, found_key;
5436 struct btrfs_path *path;
5437 struct extent_buffer *leaf;
5438 int ret;
5439
Li Zefan33345d012011-04-20 10:31:50 +08005440 key.objectid = btrfs_ino(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005441 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
5442 key.offset = (u64)-1;
5443
5444 path = btrfs_alloc_path();
5445 if (!path)
5446 return -ENOMEM;
5447
5448 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5449 if (ret < 0)
5450 goto out;
5451 /* FIXME: we should be able to handle this */
5452 if (ret == 0)
5453 goto out;
5454 ret = 0;
5455
5456 /*
5457 * MAGIC NUMBER EXPLANATION:
5458 * since we search a directory based on f_pos we have to start at 2
5459 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5460 * else has to start at 2
5461 */
5462 if (path->slots[0] == 0) {
5463 BTRFS_I(inode)->index_cnt = 2;
5464 goto out;
5465 }
5466
5467 path->slots[0]--;
5468
5469 leaf = path->nodes[0];
5470 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5471
Li Zefan33345d012011-04-20 10:31:50 +08005472 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacikaec74772008-07-24 12:12:38 -04005473 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
5474 BTRFS_I(inode)->index_cnt = 2;
5475 goto out;
5476 }
5477
5478 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
5479out:
5480 btrfs_free_path(path);
5481 return ret;
5482}
5483
Chris Masond352ac62008-09-29 15:18:18 -04005484/*
5485 * helper to find a free sequence number in a given directory. This current
5486 * code is very simple, later versions will do smarter things in the btree
5487 */
Chris Mason3de45862008-11-17 21:02:50 -05005488int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04005489{
5490 int ret = 0;
5491
5492 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08005493 ret = btrfs_inode_delayed_dir_index_count(dir);
5494 if (ret) {
5495 ret = btrfs_set_inode_index_count(dir);
5496 if (ret)
5497 return ret;
5498 }
Josef Bacikaec74772008-07-24 12:12:38 -04005499 }
5500
Chris Mason00e4e6b2008-08-05 11:18:09 -04005501 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04005502 BTRFS_I(dir)->index_cnt++;
5503
5504 return ret;
5505}
5506
Chris Mason39279cc2007-06-12 06:35:45 -04005507static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5508 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04005509 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05005510 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04005511 u64 ref_objectid, u64 objectid,
5512 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04005513{
5514 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005515 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04005516 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04005517 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05005518 struct btrfs_inode_ref *ref;
5519 struct btrfs_key key[2];
5520 u32 sizes[2];
5521 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04005522 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005523
Chris Mason5f39d392007-10-15 16:14:19 -04005524 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005525 if (!path)
5526 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04005527
Chris Mason39279cc2007-06-12 06:35:45 -04005528 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005529 if (!inode) {
5530 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005531 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005532 }
Chris Mason39279cc2007-06-12 06:35:45 -04005533
Li Zefan581bb052011-04-20 10:06:11 +08005534 /*
5535 * we have to initialize this early, so we can reclaim the inode
5536 * number if we fail afterwards in this function.
5537 */
5538 inode->i_ino = objectid;
5539
Josef Bacikaec74772008-07-24 12:12:38 -04005540 if (dir) {
liubo1abe9b82011-03-24 11:18:59 +00005541 trace_btrfs_inode_request(dir);
5542
Chris Mason3de45862008-11-17 21:02:50 -05005543 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04005544 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005545 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005546 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005547 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04005548 }
Josef Bacikaec74772008-07-24 12:12:38 -04005549 }
5550 /*
5551 * index_cnt is ignored for everything but a dir,
5552 * btrfs_get_inode_index_count has an explanation for the magic
5553 * number
5554 */
5555 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08005556 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04005557 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04005558 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00005559 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db2b2007-08-27 16:49:44 -04005560
Josef Bacik5dc562c2012-08-17 13:14:17 -04005561 /*
5562 * We could have gotten an inode number from somebody who was fsynced
5563 * and then removed in this same transaction, so let's just set full
5564 * sync since it will be a full sync anyway and this will blow away the
5565 * old info in the log.
5566 */
5567 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
5568
Chris Mason9c583092008-01-29 15:15:18 -05005569 key[0].objectid = objectid;
5570 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
5571 key[0].offset = 0;
5572
Mark Fashehf1863732012-08-08 11:32:27 -07005573 /*
5574 * Start new inodes with an inode_ref. This is slightly more
5575 * efficient for small numbers of hard links since they will
5576 * be packed into one item. Extended refs will kick in if we
5577 * add more hard links than can fit in the ref item.
5578 */
Chris Mason9c583092008-01-29 15:15:18 -05005579 key[1].objectid = objectid;
5580 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
5581 key[1].offset = ref_objectid;
5582
5583 sizes[0] = sizeof(struct btrfs_inode_item);
5584 sizes[1] = name_len + sizeof(*ref);
5585
Chris Masonb9473432009-03-13 11:00:37 -04005586 path->leave_spinning = 1;
Chris Mason9c583092008-01-29 15:15:18 -05005587 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
5588 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04005589 goto fail;
5590
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03005591 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04005592 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005593 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04005594 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
5595 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06005596 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
5597 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04005598 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05005599
5600 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
5601 struct btrfs_inode_ref);
5602 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04005603 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05005604 ptr = (unsigned long)(ref + 1);
5605 write_extent_buffer(path->nodes[0], name, ptr, name_len);
5606
Chris Mason5f39d392007-10-15 16:14:19 -04005607 btrfs_mark_buffer_dirty(path->nodes[0]);
5608 btrfs_free_path(path);
5609
Chris Mason39279cc2007-06-12 06:35:45 -04005610 location = &BTRFS_I(inode)->location;
5611 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04005612 location->offset = 0;
5613 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
5614
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005615 btrfs_inherit_iflags(inode, dir);
5616
Al Viro569254b2011-07-24 17:08:40 -04005617 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04005618 if (btrfs_test_opt(root, NODATASUM))
5619 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo213490b2012-09-11 08:33:50 -06005620 if (btrfs_test_opt(root, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05005621 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
5622 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04005623 }
5624
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005625 btrfs_insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005626 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00005627
5628 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04005629 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00005630
Alexander Block8ea05e32012-07-25 17:35:53 +02005631 btrfs_update_root_times(trans, root);
5632
Filipe David Borba Manana63541922014-01-07 11:47:46 +00005633 ret = btrfs_inode_inherit_props(trans, inode, dir);
5634 if (ret)
5635 btrfs_err(root->fs_info,
5636 "error inheriting props for ino %llu (root %llu): %d",
5637 btrfs_ino(inode), root->root_key.objectid, ret);
5638
Chris Mason39279cc2007-06-12 06:35:45 -04005639 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005640fail:
Josef Bacikaec74772008-07-24 12:12:38 -04005641 if (dir)
5642 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04005643 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005644 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005645 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005646}
5647
5648static inline u8 btrfs_inode_type(struct inode *inode)
5649{
5650 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
5651}
5652
Chris Masond352ac62008-09-29 15:18:18 -04005653/*
5654 * utility function to add 'inode' into 'parent_inode' with
5655 * a give name and a given sequence number.
5656 * if 'add_backref' is true, also insert a backref from the
5657 * inode to the parent directory.
5658 */
Chris Masone02119d2008-09-05 16:13:11 -04005659int btrfs_add_link(struct btrfs_trans_handle *trans,
5660 struct inode *parent_inode, struct inode *inode,
5661 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005662{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005663 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005664 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04005665 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08005666 u64 ino = btrfs_ino(inode);
5667 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005668
Li Zefan33345d012011-04-20 10:31:50 +08005669 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005670 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
5671 } else {
Li Zefan33345d012011-04-20 10:31:50 +08005672 key.objectid = ino;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005673 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
5674 key.offset = 0;
5675 }
Chris Mason39279cc2007-06-12 06:35:45 -04005676
Li Zefan33345d012011-04-20 10:31:50 +08005677 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005678 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
5679 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08005680 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005681 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08005682 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
5683 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005684 }
5685
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005686 /* Nothing to clean up yet */
5687 if (ret)
5688 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005689
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005690 ret = btrfs_insert_dir_item(trans, root, name, name_len,
5691 parent_inode, &key,
5692 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05005693 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005694 goto fail_dir_item;
5695 else if (ret) {
5696 btrfs_abort_transaction(trans, root, ret);
5697 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005698 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005699
5700 btrfs_i_size_write(parent_inode, parent_inode->i_size +
5701 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005702 inode_inc_iversion(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005703 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
5704 ret = btrfs_update_inode(trans, root, parent_inode);
5705 if (ret)
5706 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005707 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05005708
5709fail_dir_item:
5710 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
5711 u64 local_index;
5712 int err;
5713 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
5714 key.objectid, root->root_key.objectid,
5715 parent_ino, &local_index, name, name_len);
5716
5717 } else if (add_backref) {
5718 u64 local_index;
5719 int err;
5720
5721 err = btrfs_del_inode_ref(trans, root, name, name_len,
5722 ino, parent_ino, &local_index);
5723 }
5724 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005725}
5726
5727static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00005728 struct inode *dir, struct dentry *dentry,
5729 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005730{
Josef Bacika1b075d2010-11-19 20:36:11 +00005731 int err = btrfs_add_link(trans, dir, inode,
5732 dentry->d_name.name, dentry->d_name.len,
5733 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005734 if (err > 0)
5735 err = -EEXIST;
5736 return err;
5737}
5738
Josef Bacik618e21d2007-07-11 10:18:17 -04005739static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04005740 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04005741{
5742 struct btrfs_trans_handle *trans;
5743 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005744 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04005745 int err;
5746 int drop_inode = 0;
5747 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005748 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04005749
5750 if (!new_valid_dev(rdev))
5751 return -EINVAL;
5752
Josef Bacik9ed74f22009-09-11 16:12:44 -04005753 /*
5754 * 2 for inode item and ref
5755 * 2 for dir items
5756 * 1 for xattr if selinux is on
5757 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005758 trans = btrfs_start_transaction(root, 5);
5759 if (IS_ERR(trans))
5760 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05005761
Li Zefan581bb052011-04-20 10:06:11 +08005762 err = btrfs_find_free_ino(root, &objectid);
5763 if (err)
5764 goto out_unlock;
5765
Josef Bacikaec74772008-07-24 12:12:38 -04005766 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005767 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04005768 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005769 if (IS_ERR(inode)) {
5770 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005771 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005772 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005773
Eric Paris2a7dba32011-02-01 11:05:39 -05005774 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005775 if (err) {
5776 drop_inode = 1;
5777 goto out_unlock;
5778 }
5779
Casey Schauflerad19db72011-12-15 10:09:07 -05005780 /*
5781 * If the active LSM wants to access the inode during
5782 * d_instantiate it needs these. Smack checks to see
5783 * if the filesystem supports xattrs by looking at the
5784 * ops vector.
5785 */
5786
5787 inode->i_op = &btrfs_special_inode_operations;
Josef Bacika1b075d2010-11-19 20:36:11 +00005788 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04005789 if (err)
5790 drop_inode = 1;
5791 else {
Josef Bacik618e21d2007-07-11 10:18:17 -04005792 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04005793 btrfs_update_inode(trans, root, inode);
Al Viro08c422c2011-12-23 07:58:13 -05005794 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005795 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005796out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005797 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00005798 btrfs_btree_balance_dirty(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04005799 if (drop_inode) {
5800 inode_dec_link_count(inode);
5801 iput(inode);
5802 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005803 return err;
5804}
5805
Chris Mason39279cc2007-06-12 06:35:45 -04005806static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04005807 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04005808{
5809 struct btrfs_trans_handle *trans;
5810 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005811 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005812 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04005813 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04005814 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005815 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005816
Josef Bacik9ed74f22009-09-11 16:12:44 -04005817 /*
5818 * 2 for inode item and ref
5819 * 2 for dir items
5820 * 1 for xattr if selinux is on
5821 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005822 trans = btrfs_start_transaction(root, 5);
5823 if (IS_ERR(trans))
5824 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005825
Li Zefan581bb052011-04-20 10:06:11 +08005826 err = btrfs_find_free_ino(root, &objectid);
5827 if (err)
5828 goto out_unlock;
5829
Josef Bacikaec74772008-07-24 12:12:38 -04005830 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005831 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04005832 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005833 if (IS_ERR(inode)) {
5834 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005835 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005836 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005837 drop_inode_on_err = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04005838
Eric Paris2a7dba32011-02-01 11:05:39 -05005839 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005840 if (err)
Josef Bacik33268ea2008-07-24 12:16:36 -04005841 goto out_unlock;
Josef Bacik33268ea2008-07-24 12:16:36 -04005842
Filipe Brandenburger9185aa52012-11-30 03:40:08 +00005843 err = btrfs_update_inode(trans, root, inode);
5844 if (err)
5845 goto out_unlock;
5846
Casey Schauflerad19db72011-12-15 10:09:07 -05005847 /*
5848 * If the active LSM wants to access the inode during
5849 * d_instantiate it needs these. Smack checks to see
5850 * if the filesystem supports xattrs by looking at the
5851 * ops vector.
5852 */
5853 inode->i_fop = &btrfs_file_operations;
5854 inode->i_op = &btrfs_file_inode_operations;
5855
Josef Bacika1b075d2010-11-19 20:36:11 +00005856 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005857 if (err)
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005858 goto out_unlock;
5859
5860 inode->i_mapping->a_ops = &btrfs_aops;
5861 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
5862 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
5863 d_instantiate(dentry, inode);
5864
Chris Mason39279cc2007-06-12 06:35:45 -04005865out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005866 btrfs_end_transaction(trans, root);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005867 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005868 inode_dec_link_count(inode);
5869 iput(inode);
5870 }
Liu Bob53d3f52012-11-14 14:34:34 +00005871 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005872 return err;
5873}
5874
5875static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
5876 struct dentry *dentry)
5877{
5878 struct btrfs_trans_handle *trans;
5879 struct btrfs_root *root = BTRFS_I(dir)->root;
5880 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005881 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04005882 int err;
5883 int drop_inode = 0;
5884
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005885 /* do not allow sys_link's with other subvols of the same device */
5886 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00005887 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005888
Mark Fashehf1863732012-08-08 11:32:27 -07005889 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00005890 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04005891
Chris Mason3de45862008-11-17 21:02:50 -05005892 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04005893 if (err)
5894 goto fail;
5895
Yan, Zhenga22285a2010-05-16 10:48:46 -04005896 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00005897 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04005898 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00005899 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04005900 */
Miao Xie7e6b6462011-02-18 09:21:17 +00005901 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005902 if (IS_ERR(trans)) {
5903 err = PTR_ERR(trans);
5904 goto fail;
5905 }
Chris Mason5f39d392007-10-15 16:14:19 -04005906
Miao Xie67de1172013-12-26 13:07:06 +08005907 /* There are several dir indexes for this inode, clear the cache. */
5908 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07005909 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005910 inode_inc_iversion(inode);
Miao Xie31534952011-04-13 13:19:21 +08005911 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6ee2010-10-23 11:11:40 -04005912 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04005913 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04005914
Josef Bacika1b075d2010-11-19 20:36:11 +00005915 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04005916
Yan, Zhenga5719522009-09-24 09:17:31 -04005917 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005918 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04005919 } else {
Al Viro10d9f302011-07-16 23:09:10 -04005920 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04005921 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005922 if (err)
5923 goto fail;
Al Viro08c422c2011-12-23 07:58:13 -05005924 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00005925 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04005926 }
Chris Mason39279cc2007-06-12 06:35:45 -04005927
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005928 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05005929fail:
Chris Mason39279cc2007-06-12 06:35:45 -04005930 if (drop_inode) {
5931 inode_dec_link_count(inode);
5932 iput(inode);
5933 }
Liu Bob53d3f52012-11-14 14:34:34 +00005934 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005935 return err;
5936}
5937
Al Viro18bb1db2011-07-26 01:41:39 -04005938static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04005939{
Chris Masonb9d86662008-05-02 16:13:49 -04005940 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04005941 struct btrfs_trans_handle *trans;
5942 struct btrfs_root *root = BTRFS_I(dir)->root;
5943 int err = 0;
5944 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04005945 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005946 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005947
Josef Bacik9ed74f22009-09-11 16:12:44 -04005948 /*
5949 * 2 items for inode and ref
5950 * 2 items for dir items
5951 * 1 for xattr if selinux is on
5952 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005953 trans = btrfs_start_transaction(root, 5);
5954 if (IS_ERR(trans))
5955 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04005956
Li Zefan581bb052011-04-20 10:06:11 +08005957 err = btrfs_find_free_ino(root, &objectid);
5958 if (err)
5959 goto out_fail;
5960
Josef Bacikaec74772008-07-24 12:12:38 -04005961 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005962 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04005963 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04005964 if (IS_ERR(inode)) {
5965 err = PTR_ERR(inode);
5966 goto out_fail;
5967 }
Chris Mason5f39d392007-10-15 16:14:19 -04005968
Chris Mason39279cc2007-06-12 06:35:45 -04005969 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04005970
Eric Paris2a7dba32011-02-01 11:05:39 -05005971 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005972 if (err)
5973 goto out_fail;
5974
Chris Mason39279cc2007-06-12 06:35:45 -04005975 inode->i_op = &btrfs_dir_inode_operations;
5976 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04005977
Chris Masondbe674a2008-07-17 12:54:05 -04005978 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005979 err = btrfs_update_inode(trans, root, inode);
5980 if (err)
5981 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005982
Josef Bacika1b075d2010-11-19 20:36:11 +00005983 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
5984 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005985 if (err)
5986 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005987
Chris Mason39279cc2007-06-12 06:35:45 -04005988 d_instantiate(dentry, inode);
5989 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005990
5991out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005992 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005993 if (drop_on_err)
5994 iput(inode);
Liu Bob53d3f52012-11-14 14:34:34 +00005995 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005996 return err;
5997}
5998
Chris Masond352ac62008-09-29 15:18:18 -04005999/* helper for btfs_get_extent. Given an existing extent in the tree,
6000 * and an extent that you want to insert, deal with overlap and insert
6001 * the new extent into the tree.
6002 */
Chris Mason3b951512008-04-17 11:29:12 -04006003static int merge_extent_mapping(struct extent_map_tree *em_tree,
6004 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006005 struct extent_map *em,
6006 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04006007{
6008 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04006009
Chris Masone6dcd2d2008-07-17 12:53:50 -04006010 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
6011 start_diff = map_start - em->start;
6012 em->start = map_start;
6013 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04006014 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
6015 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006016 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04006017 em->block_len -= start_diff;
6018 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006019 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006020}
6021
Chris Masonc8b97812008-10-29 14:49:59 -04006022static noinline int uncompress_inline(struct btrfs_path *path,
6023 struct inode *inode, struct page *page,
6024 size_t pg_offset, u64 extent_offset,
6025 struct btrfs_file_extent_item *item)
6026{
6027 int ret;
6028 struct extent_buffer *leaf = path->nodes[0];
6029 char *tmp;
6030 size_t max_size;
6031 unsigned long inline_size;
6032 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006033 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006034
6035 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006036 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006037 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6038 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006039 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006040 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006041 if (!tmp)
6042 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006043 ptr = btrfs_file_extent_inline_start(item);
6044
6045 read_extent_buffer(leaf, tmp, ptr, inline_size);
6046
Chris Mason5b050f02008-11-11 09:34:41 -05006047 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006048 ret = btrfs_decompress(compress_type, tmp, page,
6049 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006050 if (ret) {
Cong Wang7ac687d2011-11-25 23:14:28 +08006051 char *kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04006052 unsigned long copy_size = min_t(u64,
6053 PAGE_CACHE_SIZE - pg_offset,
6054 max_size - extent_offset);
6055 memset(kaddr + pg_offset, 0, copy_size);
Cong Wang7ac687d2011-11-25 23:14:28 +08006056 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -04006057 }
6058 kfree(tmp);
6059 return 0;
6060}
6061
Chris Masond352ac62008-09-29 15:18:18 -04006062/*
6063 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006064 * the ugly parts come from merging extents from the disk with the in-ram
6065 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006066 * where the in-ram extents might be locked pending data=ordered completion.
6067 *
6068 * This also copies inline extents directly into the page.
6069 */
Chris Masond3977122009-01-05 21:25:51 -05006070
Chris Masona52d9a82007-08-27 16:49:44 -04006071struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05006072 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04006073 int create)
6074{
6075 int ret;
6076 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04006077 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04006078 u64 extent_start = 0;
6079 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08006080 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006081 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006082 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04006083 struct btrfs_root *root = BTRFS_I(inode)->root;
6084 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006085 struct extent_buffer *leaf;
6086 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006087 struct extent_map *em = NULL;
6088 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05006089 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006090 struct btrfs_trans_handle *trans = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08006091 int compress_type;
Chris Masona52d9a82007-08-27 16:49:44 -04006092
Chris Masona52d9a82007-08-27 16:49:44 -04006093again:
Chris Mason890871b2009-09-02 16:24:52 -04006094 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006095 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006096 if (em)
6097 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006098 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006099
Chris Masona52d9a82007-08-27 16:49:44 -04006100 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006101 if (em->start > start || em->start + em->len <= start)
6102 free_extent_map(em);
6103 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006104 free_extent_map(em);
6105 else
6106 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006107 }
David Sterba172ddd62011-04-21 00:48:27 +02006108 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006109 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006110 err = -ENOMEM;
6111 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006112 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006113 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006114 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006115 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006116 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006117 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006118
6119 if (!path) {
6120 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006121 if (!path) {
6122 err = -ENOMEM;
6123 goto out;
6124 }
6125 /*
6126 * Chances are we'll be called again, so go ahead and do
6127 * readahead
6128 */
6129 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04006130 }
6131
Chris Mason179e29e2007-11-01 11:28:41 -04006132 ret = btrfs_lookup_file_extent(trans, root, path,
6133 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006134 if (ret < 0) {
6135 err = ret;
6136 goto out;
6137 }
6138
6139 if (ret != 0) {
6140 if (path->slots[0] == 0)
6141 goto not_found;
6142 path->slots[0]--;
6143 }
6144
Chris Mason5f39d392007-10-15 16:14:19 -04006145 leaf = path->nodes[0];
6146 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006147 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006148 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006149 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6150 found_type = btrfs_key_type(&found_key);
6151 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006152 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006153 /*
6154 * If we backup past the first extent we want to move forward
6155 * and see if there is an extent in front of us, otherwise we'll
6156 * say there is a hole for our whole search range which can
6157 * cause problems.
6158 */
6159 extent_end = start;
6160 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006161 }
6162
Chris Mason5f39d392007-10-15 16:14:19 -04006163 found_type = btrfs_file_extent_type(leaf, item);
6164 extent_start = found_key.offset;
Li Zefan261507a02010-12-17 14:21:50 +08006165 compress_type = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04006166 if (found_type == BTRFS_FILE_EXTENT_REG ||
6167 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006168 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006169 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006170 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6171 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08006172 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Qu Wenruofda28322013-02-26 08:10:22 +00006173 extent_end = ALIGN(extent_start + size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04006174 }
Josef Bacik25a50342013-10-14 12:08:38 -04006175next:
Yan Zheng9036c102008-10-30 14:19:41 -04006176 if (start >= extent_end) {
6177 path->slots[0]++;
6178 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6179 ret = btrfs_next_leaf(root, path);
6180 if (ret < 0) {
6181 err = ret;
6182 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006183 }
Yan Zheng9036c102008-10-30 14:19:41 -04006184 if (ret > 0)
6185 goto not_found;
6186 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006187 }
Yan Zheng9036c102008-10-30 14:19:41 -04006188 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6189 if (found_key.objectid != objectid ||
6190 found_key.type != BTRFS_EXTENT_DATA_KEY)
6191 goto not_found;
6192 if (start + len <= found_key.offset)
6193 goto not_found;
6194 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006195 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006196 em->len = found_key.offset - start;
6197 goto not_found_em;
6198 }
6199
Josef Bacikcc95bef2013-04-04 14:31:27 -04006200 em->ram_bytes = btrfs_file_extent_ram_bytes(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04006201 if (found_type == BTRFS_FILE_EXTENT_REG ||
6202 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04006203 em->start = extent_start;
6204 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05006205 em->orig_start = extent_start -
6206 btrfs_file_extent_offset(leaf, item);
Josef Bacikb4939682012-12-03 10:31:19 -05006207 em->orig_block_len = btrfs_file_extent_disk_num_bytes(leaf,
6208 item);
Chris Masondb945352007-10-15 16:15:53 -04006209 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
6210 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04006211 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04006212 goto insert;
6213 }
Li Zefan261507a02010-12-17 14:21:50 +08006214 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006215 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08006216 em->compress_type = compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006217 em->block_start = bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05006218 em->block_len = em->orig_block_len;
Chris Masonc8b97812008-10-29 14:49:59 -04006219 } else {
6220 bytenr += btrfs_file_extent_offset(leaf, item);
6221 em->block_start = bytenr;
6222 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04006223 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
6224 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04006225 }
Chris Masona52d9a82007-08-27 16:49:44 -04006226 goto insert;
6227 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006228 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006229 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006230 size_t size;
6231 size_t extent_offset;
6232 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006233
Chris Masona52d9a82007-08-27 16:49:44 -04006234 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04006235 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04006236 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04006237 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04006238 goto out;
6239 }
6240
Chris Mason514ac8a2014-01-03 21:07:00 -08006241 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04006242 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05006243 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04006244 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006245 em->start = extent_start + extent_offset;
Qu Wenruofda28322013-02-26 08:10:22 +00006246 em->len = ALIGN(copy_size, root->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006247 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006248 em->orig_start = em->start;
Li Zefan261507a02010-12-17 14:21:50 +08006249 if (compress_type) {
Chris Masonc8b97812008-10-29 14:49:59 -04006250 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08006251 em->compress_type = compress_type;
6252 }
Yan689f9342007-10-29 11:41:07 -04006253 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006254 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006255 if (btrfs_file_extent_compression(leaf, item) !=
6256 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006257 ret = uncompress_inline(path, inode, page,
6258 pg_offset,
6259 extent_offset, item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006260 BUG_ON(ret); /* -ENOMEM */
Chris Masonc8b97812008-10-29 14:49:59 -04006261 } else {
6262 map = kmap(page);
6263 read_extent_buffer(leaf, map + pg_offset, ptr,
6264 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04006265 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
6266 memset(map + pg_offset + copy_size, 0,
6267 PAGE_CACHE_SIZE - pg_offset -
6268 copy_size);
6269 }
Chris Masonc8b97812008-10-29 14:49:59 -04006270 kunmap(page);
6271 }
Chris Mason179e29e2007-11-01 11:28:41 -04006272 flush_dcache_page(page);
6273 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006274 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006275 if (!trans) {
6276 kunmap(page);
6277 free_extent_map(em);
6278 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006279
David Sterbab3b4aa72011-04-21 01:20:15 +02006280 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006281 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006282
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006283 if (IS_ERR(trans))
6284 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006285 goto again;
6286 }
Chris Masonc8b97812008-10-29 14:49:59 -04006287 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006288 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006289 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006290 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006291 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006292 }
Chris Masond1310b22008-01-24 16:13:08 -05006293 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006294 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006295 goto insert;
6296 } else {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00006297 WARN(1, KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04006298 }
6299not_found:
6300 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006301 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006302 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006303not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04006304 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04006305 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006306insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006307 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006308 if (em->start > start || extent_map_end(em) <= start) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006309 btrfs_err(root->fs_info, "bad extent! em: [%llu %llu] passed [%llu %llu]",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006310 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04006311 err = -EIO;
6312 goto out;
6313 }
Chris Masond1310b22008-01-24 16:13:08 -05006314
6315 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006316 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006317 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006318 /* it is possible that someone inserted the extent into the tree
6319 * while we had the lock dropped. It is also possible that
6320 * an overlapping map exists in the tree
6321 */
Chris Masona52d9a82007-08-27 16:49:44 -04006322 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04006323 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006324
6325 ret = 0;
6326
Chris Mason3b951512008-04-17 11:29:12 -04006327 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04006328 if (existing && (existing->start > start ||
6329 existing->start + existing->len <= start)) {
6330 free_extent_map(existing);
6331 existing = NULL;
6332 }
Chris Mason3b951512008-04-17 11:29:12 -04006333 if (!existing) {
6334 existing = lookup_extent_mapping(em_tree, em->start,
6335 em->len);
6336 if (existing) {
6337 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006338 em, start,
6339 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04006340 free_extent_map(existing);
6341 if (err) {
6342 free_extent_map(em);
6343 em = NULL;
6344 }
6345 } else {
6346 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04006347 free_extent_map(em);
6348 em = NULL;
6349 }
6350 } else {
6351 free_extent_map(em);
6352 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006353 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006354 }
Chris Masona52d9a82007-08-27 16:49:44 -04006355 }
Chris Mason890871b2009-09-02 16:24:52 -04006356 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006357out:
liubo1abe9b82011-03-24 11:18:59 +00006358
Steven Rostedt4cd85872013-11-14 22:57:29 -05006359 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00006360
Chris Masonf4219502008-07-22 11:18:09 -04006361 if (path)
6362 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04006363 if (trans) {
6364 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05006365 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04006366 err = ret;
6367 }
Chris Masona52d9a82007-08-27 16:49:44 -04006368 if (err) {
6369 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04006370 return ERR_PTR(err);
6371 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006372 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04006373 return em;
6374}
6375
Chris Masonec29ed52011-02-23 16:23:20 -05006376struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
6377 size_t pg_offset, u64 start, u64 len,
6378 int create)
6379{
6380 struct extent_map *em;
6381 struct extent_map *hole_em = NULL;
6382 u64 range_start = start;
6383 u64 end;
6384 u64 found;
6385 u64 found_end;
6386 int err = 0;
6387
6388 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
6389 if (IS_ERR(em))
6390 return em;
6391 if (em) {
6392 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00006393 * if our em maps to
6394 * - a hole or
6395 * - a pre-alloc extent,
6396 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05006397 */
Liu Bof9e4fb52013-01-07 10:10:12 +00006398 if (em->block_start != EXTENT_MAP_HOLE &&
6399 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05006400 return em;
6401 else
6402 hole_em = em;
6403 }
6404
6405 /* check to see if we've wrapped (len == -1 or similar) */
6406 end = start + len;
6407 if (end < start)
6408 end = (u64)-1;
6409 else
6410 end -= 1;
6411
6412 em = NULL;
6413
6414 /* ok, we didn't find anything, lets look for delalloc */
6415 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
6416 end, len, EXTENT_DELALLOC, 1);
6417 found_end = range_start + found;
6418 if (found_end < range_start)
6419 found_end = (u64)-1;
6420
6421 /*
6422 * we didn't find anything useful, return
6423 * the original results from get_extent()
6424 */
6425 if (range_start > end || found_end <= start) {
6426 em = hole_em;
6427 hole_em = NULL;
6428 goto out;
6429 }
6430
6431 /* adjust the range_start to make sure it doesn't
6432 * go backwards from the start they passed in
6433 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05306434 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05006435 found = found_end - range_start;
6436
6437 if (found > 0) {
6438 u64 hole_start = start;
6439 u64 hole_len = len;
6440
David Sterba172ddd62011-04-21 00:48:27 +02006441 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05006442 if (!em) {
6443 err = -ENOMEM;
6444 goto out;
6445 }
6446 /*
6447 * when btrfs_get_extent can't find anything it
6448 * returns one huge hole
6449 *
6450 * make sure what it found really fits our range, and
6451 * adjust to make sure it is based on the start from
6452 * the caller
6453 */
6454 if (hole_em) {
6455 u64 calc_end = extent_map_end(hole_em);
6456
6457 if (calc_end <= start || (hole_em->start > end)) {
6458 free_extent_map(hole_em);
6459 hole_em = NULL;
6460 } else {
6461 hole_start = max(hole_em->start, start);
6462 hole_len = calc_end - hole_start;
6463 }
6464 }
6465 em->bdev = NULL;
6466 if (hole_em && range_start > hole_start) {
6467 /* our hole starts before our delalloc, so we
6468 * have to return just the parts of the hole
6469 * that go until the delalloc starts
6470 */
6471 em->len = min(hole_len,
6472 range_start - hole_start);
6473 em->start = hole_start;
6474 em->orig_start = hole_start;
6475 /*
6476 * don't adjust block start at all,
6477 * it is fixed at EXTENT_MAP_HOLE
6478 */
6479 em->block_start = hole_em->block_start;
6480 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00006481 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
6482 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05006483 } else {
6484 em->start = range_start;
6485 em->len = found;
6486 em->orig_start = range_start;
6487 em->block_start = EXTENT_MAP_DELALLOC;
6488 em->block_len = found;
6489 }
6490 } else if (hole_em) {
6491 return hole_em;
6492 }
6493out:
6494
6495 free_extent_map(hole_em);
6496 if (err) {
6497 free_extent_map(em);
6498 return ERR_PTR(err);
6499 }
6500 return em;
6501}
6502
Josef Bacik4b46fce2010-05-23 11:00:55 -04006503static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
6504 u64 start, u64 len)
6505{
6506 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04006507 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006508 struct btrfs_key ins;
6509 u64 alloc_hint;
6510 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006511
Josef Bacik4b46fce2010-05-23 11:00:55 -04006512 alloc_hint = get_extent_allocation_hint(inode, start, len);
Josef Bacik00361582013-08-14 14:02:47 -04006513 ret = btrfs_reserve_extent(root, len, root->sectorsize, 0,
Josef Bacik81c9ad22012-01-18 10:56:06 -05006514 alloc_hint, &ins, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006515 if (ret)
6516 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006517
Josef Bacik70c8a912012-10-11 16:54:30 -04006518 em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006519 ins.offset, ins.offset, ins.offset, 0);
Josef Bacik00361582013-08-14 14:02:47 -04006520 if (IS_ERR(em)) {
6521 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
6522 return em;
6523 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006524
6525 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
6526 ins.offset, ins.offset, 0);
6527 if (ret) {
6528 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
Josef Bacik00361582013-08-14 14:02:47 -04006529 free_extent_map(em);
6530 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006531 }
Josef Bacik00361582013-08-14 14:02:47 -04006532
Josef Bacik4b46fce2010-05-23 11:00:55 -04006533 return em;
6534}
6535
Chris Mason46bfbb52010-05-26 11:04:10 -04006536/*
6537 * returns 1 when the nocow is safe, < 1 on error, 0 if the
6538 * block must be cow'd
6539 */
Josef Bacik00361582013-08-14 14:02:47 -04006540noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04006541 u64 *orig_start, u64 *orig_block_len,
6542 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04006543{
Josef Bacik00361582013-08-14 14:02:47 -04006544 struct btrfs_trans_handle *trans;
Chris Mason46bfbb52010-05-26 11:04:10 -04006545 struct btrfs_path *path;
6546 int ret;
6547 struct extent_buffer *leaf;
6548 struct btrfs_root *root = BTRFS_I(inode)->root;
6549 struct btrfs_file_extent_item *fi;
6550 struct btrfs_key key;
6551 u64 disk_bytenr;
6552 u64 backref_offset;
6553 u64 extent_end;
6554 u64 num_bytes;
6555 int slot;
6556 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04006557 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08006558
Chris Mason46bfbb52010-05-26 11:04:10 -04006559 path = btrfs_alloc_path();
6560 if (!path)
6561 return -ENOMEM;
6562
Josef Bacik00361582013-08-14 14:02:47 -04006563 ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04006564 offset, 0);
6565 if (ret < 0)
6566 goto out;
6567
6568 slot = path->slots[0];
6569 if (ret == 1) {
6570 if (slot == 0) {
6571 /* can't find the item, must cow */
6572 ret = 0;
6573 goto out;
6574 }
6575 slot--;
6576 }
6577 ret = 0;
6578 leaf = path->nodes[0];
6579 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08006580 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04006581 key.type != BTRFS_EXTENT_DATA_KEY) {
6582 /* not our file or wrong item type, must cow */
6583 goto out;
6584 }
6585
6586 if (key.offset > offset) {
6587 /* Wrong offset, must cow */
6588 goto out;
6589 }
6590
6591 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
6592 found_type = btrfs_file_extent_type(leaf, fi);
6593 if (found_type != BTRFS_FILE_EXTENT_REG &&
6594 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
6595 /* not a regular extent, must cow */
6596 goto out;
6597 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04006598
6599 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
6600 goto out;
6601
Miao Xiee77751a2013-12-27 21:11:50 +08006602 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
6603 if (extent_end <= offset)
6604 goto out;
6605
Chris Mason46bfbb52010-05-26 11:04:10 -04006606 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04006607 if (disk_bytenr == 0)
6608 goto out;
6609
6610 if (btrfs_file_extent_compression(leaf, fi) ||
6611 btrfs_file_extent_encryption(leaf, fi) ||
6612 btrfs_file_extent_other_encoding(leaf, fi))
6613 goto out;
6614
Chris Mason46bfbb52010-05-26 11:04:10 -04006615 backref_offset = btrfs_file_extent_offset(leaf, fi);
6616
Josef Bacik7ee9e442013-06-21 16:37:03 -04006617 if (orig_start) {
6618 *orig_start = key.offset - backref_offset;
6619 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
6620 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
6621 }
Josef Bacikeb384b52013-04-24 16:32:55 -04006622
Chris Mason46bfbb52010-05-26 11:04:10 -04006623 if (btrfs_extent_readonly(root, disk_bytenr))
6624 goto out;
Josef Bacik1bda19e2013-10-18 12:10:36 -04006625 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04006626
6627 /*
6628 * look for other files referencing this extent, if we
6629 * find any we must cow
6630 */
Josef Bacik00361582013-08-14 14:02:47 -04006631 trans = btrfs_join_transaction(root);
6632 if (IS_ERR(trans)) {
6633 ret = 0;
Chris Mason46bfbb52010-05-26 11:04:10 -04006634 goto out;
Josef Bacik00361582013-08-14 14:02:47 -04006635 }
6636
6637 ret = btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
6638 key.offset - backref_offset, disk_bytenr);
6639 btrfs_end_transaction(trans, root);
6640 if (ret) {
6641 ret = 0;
6642 goto out;
6643 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006644
6645 /*
6646 * adjust disk_bytenr and num_bytes to cover just the bytes
6647 * in this extent we are about to write. If there
6648 * are any csums in that range we have to cow in order
6649 * to keep the csums correct
6650 */
6651 disk_bytenr += backref_offset;
6652 disk_bytenr += offset - key.offset;
Josef Bacikeb384b52013-04-24 16:32:55 -04006653 num_bytes = min(offset + *len, extent_end) - offset;
Chris Mason46bfbb52010-05-26 11:04:10 -04006654 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
6655 goto out;
6656 /*
6657 * all of the above have passed, it is safe to overwrite this extent
6658 * without cow
6659 */
Josef Bacikeb384b52013-04-24 16:32:55 -04006660 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04006661 ret = 1;
6662out:
6663 btrfs_free_path(path);
6664 return ret;
6665}
6666
Josef Bacikeb838e72012-07-31 16:28:48 -04006667static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
6668 struct extent_state **cached_state, int writing)
6669{
6670 struct btrfs_ordered_extent *ordered;
6671 int ret = 0;
6672
6673 while (1) {
6674 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6675 0, cached_state);
6676 /*
6677 * We're concerned with the entire range that we're going to be
6678 * doing DIO to, so we need to make sure theres no ordered
6679 * extents in this range.
6680 */
6681 ordered = btrfs_lookup_ordered_range(inode, lockstart,
6682 lockend - lockstart + 1);
6683
6684 /*
6685 * We need to make sure there are no buffered pages in this
6686 * range either, we could have raced between the invalidate in
6687 * generic_file_direct_write and locking the extent. The
6688 * invalidate needs to happen so that reads after a write do not
6689 * get stale data.
6690 */
6691 if (!ordered && (!writing ||
6692 !test_range_bit(&BTRFS_I(inode)->io_tree,
6693 lockstart, lockend, EXTENT_UPTODATE, 0,
6694 *cached_state)))
6695 break;
6696
6697 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6698 cached_state, GFP_NOFS);
6699
6700 if (ordered) {
6701 btrfs_start_ordered_extent(inode, ordered, 1);
6702 btrfs_put_ordered_extent(ordered);
6703 } else {
6704 /* Screw you mmap */
6705 ret = filemap_write_and_wait_range(inode->i_mapping,
6706 lockstart,
6707 lockend);
6708 if (ret)
6709 break;
6710
6711 /*
6712 * If we found a page that couldn't be invalidated just
6713 * fall back to buffered.
6714 */
6715 ret = invalidate_inode_pages2_range(inode->i_mapping,
6716 lockstart >> PAGE_CACHE_SHIFT,
6717 lockend >> PAGE_CACHE_SHIFT);
6718 if (ret)
6719 break;
6720 }
6721
6722 cond_resched();
6723 }
6724
6725 return ret;
6726}
6727
Josef Bacik69ffb542012-09-11 15:40:07 -04006728static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
6729 u64 len, u64 orig_start,
6730 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006731 u64 orig_block_len, u64 ram_bytes,
6732 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04006733{
6734 struct extent_map_tree *em_tree;
6735 struct extent_map *em;
6736 struct btrfs_root *root = BTRFS_I(inode)->root;
6737 int ret;
6738
6739 em_tree = &BTRFS_I(inode)->extent_tree;
6740 em = alloc_extent_map();
6741 if (!em)
6742 return ERR_PTR(-ENOMEM);
6743
6744 em->start = start;
6745 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04006746 em->mod_start = start;
6747 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04006748 em->len = len;
6749 em->block_len = block_len;
6750 em->block_start = block_start;
6751 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05006752 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04006753 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04006754 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04006755 set_bit(EXTENT_FLAG_PINNED, &em->flags);
6756 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05006757 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04006758
6759 do {
6760 btrfs_drop_extent_cache(inode, em->start,
6761 em->start + em->len - 1, 0);
6762 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006763 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04006764 write_unlock(&em_tree->lock);
6765 } while (ret == -EEXIST);
6766
6767 if (ret) {
6768 free_extent_map(em);
6769 return ERR_PTR(ret);
6770 }
6771
6772 return em;
6773}
6774
6775
Josef Bacik4b46fce2010-05-23 11:00:55 -04006776static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
6777 struct buffer_head *bh_result, int create)
6778{
6779 struct extent_map *em;
6780 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04006781 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006782 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04006783 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006784 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04006785 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00006786 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006787
Miao Xie172a5042013-02-21 02:48:22 -07006788 if (create)
Josef Bacikeb838e72012-07-31 16:28:48 -04006789 unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07006790 else
Josef Bacikc3298612012-08-03 16:49:19 -04006791 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04006792
Josef Bacikc3298612012-08-03 16:49:19 -04006793 lockstart = start;
6794 lockend = start + len - 1;
6795
Josef Bacikeb838e72012-07-31 16:28:48 -04006796 /*
6797 * If this errors out it's because we couldn't invalidate pagecache for
6798 * this range and we need to fallback to buffered.
6799 */
6800 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
6801 return -ENOTBLK;
6802
Josef Bacik4b46fce2010-05-23 11:00:55 -04006803 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04006804 if (IS_ERR(em)) {
6805 ret = PTR_ERR(em);
6806 goto unlock_err;
6807 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006808
6809 /*
6810 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
6811 * io. INLINE is special, and we could probably kludge it in here, but
6812 * it's still buffered so for safety lets just fall back to the generic
6813 * buffered path.
6814 *
6815 * For COMPRESSED we _have_ to read the entire extent in so we can
6816 * decompress it, so there will be buffering required no matter what we
6817 * do, so go ahead and fallback to buffered.
6818 *
6819 * We return -ENOTBLK because thats what makes DIO go ahead and go back
6820 * to buffered IO. Don't blame me, this is the price we pay for using
6821 * the generic code.
6822 */
6823 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
6824 em->block_start == EXTENT_MAP_INLINE) {
6825 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006826 ret = -ENOTBLK;
6827 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006828 }
6829
6830 /* Just a good old fashioned hole, return */
6831 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
6832 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
6833 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006834 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006835 }
6836
6837 /*
6838 * We don't allocate a new extent in the following cases
6839 *
6840 * 1) The inode is marked as NODATACOW. In this case we'll just use the
6841 * existing extent.
6842 * 2) The extent is marked as PREALLOC. We're good to go here and can
6843 * just use the extent.
6844 *
6845 */
Chris Mason46bfbb52010-05-26 11:04:10 -04006846 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04006847 len = min(len, em->len - (start - em->start));
6848 lockstart = start + len;
6849 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04006850 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006851
6852 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
6853 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
6854 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04006855 int type;
6856 int ret;
Josef Bacikeb384b52013-04-24 16:32:55 -04006857 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006858
6859 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6860 type = BTRFS_ORDERED_PREALLOC;
6861 else
6862 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04006863 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04006864 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04006865
Josef Bacik00361582013-08-14 14:02:47 -04006866 if (can_nocow_extent(inode, start, &len, &orig_start,
Josef Bacik7ee9e442013-06-21 16:37:03 -04006867 &orig_block_len, &ram_bytes) == 1) {
Josef Bacik69ffb542012-09-11 15:40:07 -04006868 if (type == BTRFS_ORDERED_PREALLOC) {
6869 free_extent_map(em);
6870 em = create_pinned_em(inode, start, len,
6871 orig_start,
Josef Bacikb4939682012-12-03 10:31:19 -05006872 block_start, len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006873 orig_block_len,
6874 ram_bytes, type);
Josef Bacik00361582013-08-14 14:02:47 -04006875 if (IS_ERR(em))
Josef Bacik69ffb542012-09-11 15:40:07 -04006876 goto unlock_err;
Josef Bacik69ffb542012-09-11 15:40:07 -04006877 }
6878
Chris Mason46bfbb52010-05-26 11:04:10 -04006879 ret = btrfs_add_ordered_extent_dio(inode, start,
6880 block_start, len, len, type);
Chris Mason46bfbb52010-05-26 11:04:10 -04006881 if (ret) {
6882 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006883 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04006884 }
6885 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006886 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006887 }
Josef Bacik00361582013-08-14 14:02:47 -04006888
Chris Mason46bfbb52010-05-26 11:04:10 -04006889 /*
6890 * this will cow the extent, reset the len in case we changed
6891 * it above
6892 */
6893 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04006894 free_extent_map(em);
6895 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04006896 if (IS_ERR(em)) {
6897 ret = PTR_ERR(em);
6898 goto unlock_err;
6899 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006900 len = min(len, em->len - (start - em->start));
6901unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04006902 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
6903 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04006904 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006905 bh_result->b_bdev = em->bdev;
6906 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04006907 if (create) {
6908 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6909 set_buffer_new(bh_result);
6910
6911 /*
6912 * Need to update the i_size under the extent lock so buffered
6913 * readers will get the updated i_size when we unlock.
6914 */
6915 if (start + len > i_size_read(inode))
6916 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00006917
Miao Xie172a5042013-02-21 02:48:22 -07006918 spin_lock(&BTRFS_I(inode)->lock);
6919 BTRFS_I(inode)->outstanding_extents++;
6920 spin_unlock(&BTRFS_I(inode)->lock);
6921
Miao Xie09348562013-02-07 10:12:07 +00006922 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6923 lockstart + len - 1, EXTENT_DELALLOC, NULL,
6924 &cached_state, GFP_NOFS);
6925 BUG_ON(ret);
Josef Bacikc3473e82012-06-19 10:59:00 -04006926 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006927
Josef Bacikeb838e72012-07-31 16:28:48 -04006928 /*
6929 * In the case of write we need to clear and unlock the entire range,
6930 * in the case of read we need to unlock only the end area that we
6931 * aren't using if there is any left over space.
6932 */
Liu Bo24c03fa2012-08-22 20:10:38 -06006933 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00006934 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6935 lockend, unlock_bits, 1, 0,
6936 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06006937 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04006938 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06006939 }
Josef Bacikeb838e72012-07-31 16:28:48 -04006940
Josef Bacik4b46fce2010-05-23 11:00:55 -04006941 free_extent_map(em);
6942
6943 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006944
6945unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04006946 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6947 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
6948 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006949}
6950
Josef Bacik4b46fce2010-05-23 11:00:55 -04006951static void btrfs_endio_direct_read(struct bio *bio, int err)
6952{
Miao Xiee65e1532010-11-22 03:04:43 +00006953 struct btrfs_dio_private *dip = bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006954 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
6955 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006956 struct inode *inode = dip->inode;
6957 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason9be33952013-05-17 18:30:14 -04006958 struct bio *dio_bio;
Miao Xiefacc8a222013-07-25 19:22:34 +08006959 u32 *csums = (u32 *)dip->csum;
6960 int index = 0;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006961 u64 start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006962
6963 start = dip->logical_offset;
6964 do {
6965 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
6966 struct page *page = bvec->bv_page;
6967 char *kaddr;
6968 u32 csum = ~(u32)0;
6969 unsigned long flags;
6970
6971 local_irq_save(flags);
Cong Wang7ac687d2011-11-25 23:14:28 +08006972 kaddr = kmap_atomic(page);
Liu Bob0496682013-03-14 14:57:45 +00006973 csum = btrfs_csum_data(kaddr + bvec->bv_offset,
Josef Bacik4b46fce2010-05-23 11:00:55 -04006974 csum, bvec->bv_len);
6975 btrfs_csum_final(csum, (char *)&csum);
Cong Wang7ac687d2011-11-25 23:14:28 +08006976 kunmap_atomic(kaddr);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006977 local_irq_restore(flags);
6978
6979 flush_dcache_page(bvec->bv_page);
Miao Xiefacc8a222013-07-25 19:22:34 +08006980 if (csum != csums[index]) {
6981 btrfs_err(root->fs_info, "csum failed ino %llu off %llu csum %u expected csum %u",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006982 btrfs_ino(inode), start, csum,
6983 csums[index]);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006984 err = -EIO;
6985 }
6986 }
6987
6988 start += bvec->bv_len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006989 bvec++;
Miao Xiefacc8a222013-07-25 19:22:34 +08006990 index++;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006991 } while (bvec <= bvec_end);
6992
6993 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006994 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04006995 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006996
Josef Bacik4b46fce2010-05-23 11:00:55 -04006997 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006998
6999 /* If we had a csum failure make sure to clear the uptodate flag */
7000 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04007001 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
7002 dio_end_io(dio_bio, err);
7003 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007004}
7005
7006static void btrfs_endio_direct_write(struct bio *bio, int err)
7007{
7008 struct btrfs_dio_private *dip = bio->bi_private;
7009 struct inode *inode = dip->inode;
7010 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007011 struct btrfs_ordered_extent *ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05007012 u64 ordered_offset = dip->logical_offset;
7013 u64 ordered_bytes = dip->bytes;
Chris Mason9be33952013-05-17 18:30:14 -04007014 struct bio *dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007015 int ret;
7016
7017 if (err)
7018 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05007019again:
7020 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
7021 &ordered_offset,
Josef Bacik5fd02042012-05-02 14:00:54 -04007022 ordered_bytes, !err);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007023 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05007024 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007025
Josef Bacik5fd02042012-05-02 14:00:54 -04007026 ordered->work.func = finish_ordered_fn;
7027 ordered->work.flags = 0;
7028 btrfs_queue_worker(&root->fs_info->endio_write_workers,
7029 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05007030out_test:
7031 /*
7032 * our bio might span multiple ordered extents. If we haven't
7033 * completed the accounting for the whole dio, go back and try again
7034 */
7035 if (ordered_offset < dip->logical_offset + dip->bytes) {
7036 ordered_bytes = dip->logical_offset + dip->bytes -
7037 ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04007038 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05007039 goto again;
7040 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007041out_done:
Chris Mason9be33952013-05-17 18:30:14 -04007042 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007043
Josef Bacik4b46fce2010-05-23 11:00:55 -04007044 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04007045
7046 /* If we had an error make sure to clear the uptodate flag */
7047 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04007048 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
7049 dio_end_io(dio_bio, err);
7050 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007051}
7052
Chris Masoneaf25d92010-05-25 09:48:28 -04007053static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
7054 struct bio *bio, int mirror_num,
7055 unsigned long bio_flags, u64 offset)
7056{
7057 int ret;
7058 struct btrfs_root *root = BTRFS_I(inode)->root;
7059 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007060 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04007061 return 0;
7062}
7063
Miao Xiee65e1532010-11-22 03:04:43 +00007064static void btrfs_end_dio_bio(struct bio *bio, int err)
7065{
7066 struct btrfs_dio_private *dip = bio->bi_private;
7067
7068 if (err) {
Frank Holtonefe120a2013-12-20 11:37:06 -05007069 btrfs_err(BTRFS_I(dip->inode)->root->fs_info,
7070 "direct IO failed ino %llu rw %lu sector %#Lx len %u err no %d",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02007071 btrfs_ino(dip->inode), bio->bi_rw,
Jan Beulich3dd14622010-12-07 14:54:09 +00007072 (unsigned long long)bio->bi_sector, bio->bi_size, err);
Miao Xiee65e1532010-11-22 03:04:43 +00007073 dip->errors = 1;
7074
7075 /*
7076 * before atomic variable goto zero, we must make sure
7077 * dip->errors is perceived to be set.
7078 */
7079 smp_mb__before_atomic_dec();
7080 }
7081
7082 /* if there are more bios still pending for this dio, just exit */
7083 if (!atomic_dec_and_test(&dip->pending_bios))
7084 goto out;
7085
Chris Mason9be33952013-05-17 18:30:14 -04007086 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00007087 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04007088 } else {
7089 set_bit(BIO_UPTODATE, &dip->dio_bio->bi_flags);
Miao Xiee65e1532010-11-22 03:04:43 +00007090 bio_endio(dip->orig_bio, 0);
7091 }
7092out:
7093 bio_put(bio);
7094}
7095
7096static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
7097 u64 first_sector, gfp_t gfp_flags)
7098{
7099 int nr_vecs = bio_get_nr_vecs(bdev);
7100 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
7101}
7102
7103static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
7104 int rw, u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007105 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00007106{
Miao Xiefacc8a222013-07-25 19:22:34 +08007107 struct btrfs_dio_private *dip = bio->bi_private;
Miao Xiee65e1532010-11-22 03:04:43 +00007108 int write = rw & REQ_WRITE;
7109 struct btrfs_root *root = BTRFS_I(inode)->root;
7110 int ret;
7111
Josef Bacikb812ce22012-11-16 13:56:32 -05007112 if (async_submit)
7113 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
7114
Miao Xiee65e1532010-11-22 03:04:43 +00007115 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04007116
7117 if (!write) {
7118 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
7119 if (ret)
7120 goto err;
7121 }
Miao Xiee65e1532010-11-22 03:04:43 +00007122
Josef Bacik1ae39932011-04-06 14:41:34 -04007123 if (skip_sum)
7124 goto map;
7125
7126 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00007127 ret = btrfs_wq_submit_bio(root->fs_info,
7128 inode, rw, bio, 0, 0,
7129 file_offset,
7130 __btrfs_submit_bio_start_direct_io,
7131 __btrfs_submit_bio_done);
7132 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04007133 } else if (write) {
7134 /*
7135 * If we aren't doing async submit, calculate the csum of the
7136 * bio now.
7137 */
7138 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
7139 if (ret)
7140 goto err;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007141 } else if (!skip_sum) {
Miao Xiefacc8a222013-07-25 19:22:34 +08007142 ret = btrfs_lookup_bio_sums_dio(root, inode, dip, bio,
7143 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007144 if (ret)
7145 goto err;
7146 }
Miao Xiee65e1532010-11-22 03:04:43 +00007147
Josef Bacik1ae39932011-04-06 14:41:34 -04007148map:
7149 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007150err:
7151 bio_put(bio);
7152 return ret;
7153}
7154
7155static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
7156 int skip_sum)
7157{
7158 struct inode *inode = dip->inode;
7159 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00007160 struct bio *bio;
7161 struct bio *orig_bio = dip->orig_bio;
7162 struct bio_vec *bvec = orig_bio->bi_io_vec;
7163 u64 start_sector = orig_bio->bi_sector;
7164 u64 file_offset = dip->logical_offset;
7165 u64 submit_len = 0;
7166 u64 map_length;
7167 int nr_pages = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007168 int ret = 0;
Josef Bacik1ae39932011-04-06 14:41:34 -04007169 int async_submit = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007170
Miao Xiee65e1532010-11-22 03:04:43 +00007171 map_length = orig_bio->bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007172 ret = btrfs_map_block(root->fs_info, rw, start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007173 &map_length, NULL, 0);
7174 if (ret) {
Josef Bacik64728bb2011-04-25 19:43:52 -04007175 bio_put(orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00007176 return -EIO;
7177 }
Miao Xiefacc8a222013-07-25 19:22:34 +08007178
Josef Bacik02f57c72011-04-06 14:25:44 -04007179 if (map_length >= orig_bio->bi_size) {
7180 bio = orig_bio;
7181 goto submit;
7182 }
7183
David Woodhouse53b381b2013-01-29 18:40:14 -05007184 /* async crcs make it difficult to collect full stripe writes. */
7185 if (btrfs_get_alloc_profile(root, 1) &
7186 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6))
7187 async_submit = 0;
7188 else
7189 async_submit = 1;
7190
Josef Bacik02f57c72011-04-06 14:25:44 -04007191 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
7192 if (!bio)
7193 return -ENOMEM;
7194 bio->bi_private = dip;
7195 bio->bi_end_io = btrfs_end_dio_bio;
7196 atomic_inc(&dip->pending_bios);
7197
Miao Xiee65e1532010-11-22 03:04:43 +00007198 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
7199 if (unlikely(map_length < submit_len + bvec->bv_len ||
7200 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
7201 bvec->bv_offset) < bvec->bv_len)) {
7202 /*
7203 * inc the count before we submit the bio so
7204 * we know the end IO handler won't happen before
7205 * we inc the count. Otherwise, the dip might get freed
7206 * before we're done setting it up
7207 */
7208 atomic_inc(&dip->pending_bios);
7209 ret = __btrfs_submit_dio_bio(bio, inode, rw,
7210 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007211 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007212 if (ret) {
7213 bio_put(bio);
7214 atomic_dec(&dip->pending_bios);
7215 goto out_err;
7216 }
7217
Miao Xiee65e1532010-11-22 03:04:43 +00007218 start_sector += submit_len >> 9;
7219 file_offset += submit_len;
7220
7221 submit_len = 0;
7222 nr_pages = 0;
7223
7224 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
7225 start_sector, GFP_NOFS);
7226 if (!bio)
7227 goto out_err;
7228 bio->bi_private = dip;
7229 bio->bi_end_io = btrfs_end_dio_bio;
7230
7231 map_length = orig_bio->bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007232 ret = btrfs_map_block(root->fs_info, rw,
Stefan Behrens3ec706c2012-11-05 15:46:42 +01007233 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007234 &map_length, NULL, 0);
7235 if (ret) {
7236 bio_put(bio);
7237 goto out_err;
7238 }
7239 } else {
7240 submit_len += bvec->bv_len;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307241 nr_pages++;
Miao Xiee65e1532010-11-22 03:04:43 +00007242 bvec++;
7243 }
7244 }
7245
Josef Bacik02f57c72011-04-06 14:25:44 -04007246submit:
Miao Xiee65e1532010-11-22 03:04:43 +00007247 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007248 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007249 if (!ret)
7250 return 0;
7251
7252 bio_put(bio);
7253out_err:
7254 dip->errors = 1;
7255 /*
7256 * before atomic variable goto zero, we must
7257 * make sure dip->errors is perceived to be set.
7258 */
7259 smp_mb__before_atomic_dec();
7260 if (atomic_dec_and_test(&dip->pending_bios))
7261 bio_io_error(dip->orig_bio);
7262
7263 /* bio_end_io() will handle error, so we needn't return it */
7264 return 0;
7265}
7266
Chris Mason9be33952013-05-17 18:30:14 -04007267static void btrfs_submit_direct(int rw, struct bio *dio_bio,
7268 struct inode *inode, loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007269{
7270 struct btrfs_root *root = BTRFS_I(inode)->root;
7271 struct btrfs_dio_private *dip;
Chris Mason9be33952013-05-17 18:30:14 -04007272 struct bio *io_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007273 int skip_sum;
Miao Xiefacc8a222013-07-25 19:22:34 +08007274 int sum_len;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02007275 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007276 int ret = 0;
Miao Xiefacc8a222013-07-25 19:22:34 +08007277 u16 csum_size;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007278
7279 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
7280
Chris Mason9be33952013-05-17 18:30:14 -04007281 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04007282 if (!io_bio) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007283 ret = -ENOMEM;
7284 goto free_ordered;
7285 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007286
Miao Xiefacc8a222013-07-25 19:22:34 +08007287 if (!skip_sum && !write) {
7288 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
7289 sum_len = dio_bio->bi_size >> inode->i_sb->s_blocksize_bits;
7290 sum_len *= csum_size;
7291 } else {
7292 sum_len = 0;
7293 }
7294
7295 dip = kmalloc(sizeof(*dip) + sum_len, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04007296 if (!dip) {
7297 ret = -ENOMEM;
7298 goto free_io_bio;
7299 }
7300
7301 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007302 dip->inode = inode;
7303 dip->logical_offset = file_offset;
Miao Xiee6da5d2e2013-06-19 18:19:17 +08007304 dip->bytes = dio_bio->bi_size;
Chris Mason9be33952013-05-17 18:30:14 -04007305 dip->disk_bytenr = (u64)dio_bio->bi_sector << 9;
7306 io_bio->bi_private = dip;
Miao Xiee65e1532010-11-22 03:04:43 +00007307 dip->errors = 0;
Chris Mason9be33952013-05-17 18:30:14 -04007308 dip->orig_bio = io_bio;
7309 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00007310 atomic_set(&dip->pending_bios, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007311
7312 if (write)
Chris Mason9be33952013-05-17 18:30:14 -04007313 io_bio->bi_end_io = btrfs_endio_direct_write;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007314 else
Chris Mason9be33952013-05-17 18:30:14 -04007315 io_bio->bi_end_io = btrfs_endio_direct_read;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007316
Miao Xiee65e1532010-11-22 03:04:43 +00007317 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
7318 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04007319 return;
Chris Mason9be33952013-05-17 18:30:14 -04007320
7321free_io_bio:
7322 bio_put(io_bio);
7323
Josef Bacik4b46fce2010-05-23 11:00:55 -04007324free_ordered:
7325 /*
7326 * If this is a write, we need to clean up the reserved space and kill
7327 * the ordered extent.
7328 */
7329 if (write) {
7330 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05007331 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007332 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
7333 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
7334 btrfs_free_reserved_extent(root, ordered->start,
7335 ordered->disk_len);
7336 btrfs_put_ordered_extent(ordered);
7337 btrfs_put_ordered_extent(ordered);
7338 }
Chris Mason9be33952013-05-17 18:30:14 -04007339 bio_endio(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007340}
7341
Chris Mason5a5f79b2010-05-26 21:33:37 -04007342static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
7343 const struct iovec *iov, loff_t offset,
7344 unsigned long nr_segs)
7345{
7346 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00007347 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007348 size_t size;
7349 unsigned long addr;
7350 unsigned blocksize_mask = root->sectorsize - 1;
7351 ssize_t retval = -EINVAL;
7352 loff_t end = offset;
7353
7354 if (offset & blocksize_mask)
7355 goto out;
7356
7357 /* Check the memory alignment. Blocks cannot straddle pages */
7358 for (seg = 0; seg < nr_segs; seg++) {
7359 addr = (unsigned long)iov[seg].iov_base;
7360 size = iov[seg].iov_len;
7361 end += size;
Josef Bacika1b75f72011-04-08 15:51:18 +00007362 if ((addr & blocksize_mask) || (size & blocksize_mask))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007363 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00007364
7365 /* If this is a write we don't need to check anymore */
7366 if (rw & WRITE)
7367 continue;
7368
7369 /*
7370 * Check to make sure we don't have duplicate iov_base's in this
7371 * iovec, if so return EINVAL, otherwise we'll get csum errors
7372 * when reading back.
7373 */
7374 for (i = seg + 1; i < nr_segs; i++) {
7375 if (iov[seg].iov_base == iov[i].iov_base)
7376 goto out;
7377 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04007378 }
7379 retval = 0;
7380out:
7381 return retval;
7382}
Josef Bacikeb838e72012-07-31 16:28:48 -04007383
Chris Mason16432982008-04-10 10:23:21 -04007384static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
7385 const struct iovec *iov, loff_t offset,
7386 unsigned long nr_segs)
7387{
Josef Bacik4b46fce2010-05-23 11:00:55 -04007388 struct file *file = iocb->ki_filp;
7389 struct inode *inode = file->f_mapping->host;
Miao Xie09348562013-02-07 10:12:07 +00007390 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00007391 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00007392 bool wakeup = true;
7393 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00007394 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007395
Chris Mason5a5f79b2010-05-26 21:33:37 -04007396 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
Josef Bacikeb838e72012-07-31 16:28:48 -04007397 offset, nr_segs))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007398 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007399
Miao Xie38851cc2013-02-08 07:04:11 +00007400 atomic_inc(&inode->i_dio_count);
7401 smp_mb__after_atomic_inc();
7402
Josef Bacik0e267c42013-07-02 10:38:02 -04007403 /*
7404 * The generic stuff only does filemap_write_and_wait_range, which isn't
7405 * enough if we've written compressed pages to this area, so we need to
7406 * call btrfs_wait_ordered_range to make absolutely sure that any
7407 * outstanding dirty pages are on disk.
7408 */
7409 count = iov_length(iov, nr_segs);
Josef Bacik0ef8b722013-10-25 16:13:35 -04007410 ret = btrfs_wait_ordered_range(inode, offset, count);
7411 if (ret)
7412 return ret;
Josef Bacik0e267c42013-07-02 10:38:02 -04007413
Miao Xie09348562013-02-07 10:12:07 +00007414 if (rw & WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00007415 /*
7416 * If the write DIO is beyond the EOF, we need update
7417 * the isize, but it is protected by i_mutex. So we can
7418 * not unlock the i_mutex at this case.
7419 */
7420 if (offset + count <= inode->i_size) {
7421 mutex_unlock(&inode->i_mutex);
7422 relock = true;
7423 }
Miao Xie09348562013-02-07 10:12:07 +00007424 ret = btrfs_delalloc_reserve_space(inode, count);
7425 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00007426 goto out;
7427 } else if (unlikely(test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
7428 &BTRFS_I(inode)->runtime_flags))) {
7429 inode_dio_done(inode);
7430 flags = DIO_LOCKING | DIO_SKIP_HOLES;
7431 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00007432 }
7433
7434 ret = __blockdev_direct_IO(rw, iocb, inode,
7435 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
7436 iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
Miao Xie2e60a512013-02-08 07:01:08 +00007437 btrfs_submit_direct, flags);
Miao Xie09348562013-02-07 10:12:07 +00007438 if (rw & WRITE) {
7439 if (ret < 0 && ret != -EIOCBQUEUED)
7440 btrfs_delalloc_release_space(inode, count);
Miao Xie172a5042013-02-21 02:48:22 -07007441 else if (ret >= 0 && (size_t)ret < count)
Miao Xie09348562013-02-07 10:12:07 +00007442 btrfs_delalloc_release_space(inode,
7443 count - (size_t)ret);
Miao Xie172a5042013-02-21 02:48:22 -07007444 else
7445 btrfs_delalloc_release_metadata(inode, 0);
Miao Xie09348562013-02-07 10:12:07 +00007446 }
Miao Xie38851cc2013-02-08 07:04:11 +00007447out:
Miao Xie2e60a512013-02-08 07:01:08 +00007448 if (wakeup)
7449 inode_dio_done(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00007450 if (relock)
7451 mutex_lock(&inode->i_mutex);
Miao Xie09348562013-02-07 10:12:07 +00007452
7453 return ret;
Chris Mason16432982008-04-10 10:23:21 -04007454}
7455
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007456#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
7457
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007458static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
7459 __u64 start, __u64 len)
7460{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007461 int ret;
7462
7463 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
7464 if (ret)
7465 return ret;
7466
Chris Masonec29ed52011-02-23 16:23:20 -05007467 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007468}
7469
Chris Mason9ebefb182007-06-15 13:50:00 -04007470int btrfs_readpage(struct file *file, struct page *page)
7471{
Chris Masond1310b22008-01-24 16:13:08 -05007472 struct extent_io_tree *tree;
7473 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02007474 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04007475}
Chris Mason1832a6d2007-12-21 16:27:21 -05007476
Chris Mason39279cc2007-06-12 06:35:45 -04007477static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
7478{
Chris Masond1310b22008-01-24 16:13:08 -05007479 struct extent_io_tree *tree;
Chris Masonb888db2b2007-08-27 16:49:44 -04007480
7481
7482 if (current->flags & PF_MEMALLOC) {
7483 redirty_page_for_writepage(wbc, page);
7484 unlock_page(page);
7485 return 0;
7486 }
Chris Masond1310b22008-01-24 16:13:08 -05007487 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04007488 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
7489}
Chris Mason39279cc2007-06-12 06:35:45 -04007490
Eric Sandeen48a3b632013-04-25 20:41:01 +00007491static int btrfs_writepages(struct address_space *mapping,
7492 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04007493{
Chris Masond1310b22008-01-24 16:13:08 -05007494 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05007495
Chris Masond1310b22008-01-24 16:13:08 -05007496 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f02e2007-11-01 19:45:34 -04007497 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
7498}
7499
Chris Mason3ab2fb52007-11-08 10:59:22 -05007500static int
7501btrfs_readpages(struct file *file, struct address_space *mapping,
7502 struct list_head *pages, unsigned nr_pages)
7503{
Chris Masond1310b22008-01-24 16:13:08 -05007504 struct extent_io_tree *tree;
7505 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05007506 return extent_readpages(tree, mapping, pages, nr_pages,
7507 btrfs_get_extent);
7508}
Chris Masone6dcd2d2008-07-17 12:53:50 -04007509static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04007510{
Chris Masond1310b22008-01-24 16:13:08 -05007511 struct extent_io_tree *tree;
7512 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04007513 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04007514
Chris Masond1310b22008-01-24 16:13:08 -05007515 tree = &BTRFS_I(page->mapping->host)->io_tree;
7516 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05007517 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04007518 if (ret == 1) {
7519 ClearPagePrivate(page);
7520 set_page_private(page, 0);
7521 page_cache_release(page);
7522 }
7523 return ret;
7524}
Chris Mason39279cc2007-06-12 06:35:45 -04007525
Chris Masone6dcd2d2008-07-17 12:53:50 -04007526static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
7527{
Chris Mason98509cf2008-09-11 15:51:43 -04007528 if (PageWriteback(page) || PageDirty(page))
7529 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05007530 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007531}
7532
Lukas Czernerd47992f2013-05-21 23:17:23 -04007533static void btrfs_invalidatepage(struct page *page, unsigned int offset,
7534 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04007535{
Josef Bacik5fd02042012-05-02 14:00:54 -04007536 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05007537 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007538 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007539 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007540 u64 page_start = page_offset(page);
7541 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00007542 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04007543
Chris Mason8b62b722009-09-02 16:53:46 -04007544 /*
7545 * we have the page locked, so new writeback can't start,
7546 * and the dirty bit won't be cleared while we are here.
7547 *
7548 * Wait for IO on this page so that we can safely clear
7549 * the PagePrivate2 bit and do ordered accounting
7550 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007551 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04007552
Josef Bacik5fd02042012-05-02 14:00:54 -04007553 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007554 if (offset) {
7555 btrfs_releasepage(page, GFP_NOFS);
7556 return;
7557 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00007558
7559 if (!inode_evicting)
7560 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
7561 ordered = btrfs_lookup_ordered_extent(inode, page_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007562 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04007563 /*
7564 * IO on this page will never be started, so we need
7565 * to account for any ordered extents now
7566 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00007567 if (!inode_evicting)
7568 clear_extent_bit(tree, page_start, page_end,
7569 EXTENT_DIRTY | EXTENT_DELALLOC |
7570 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
7571 EXTENT_DEFRAG, 1, 0, &cached_state,
7572 GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04007573 /*
7574 * whoever cleared the private bit is responsible
7575 * for the finish_ordered_io
7576 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04007577 if (TestClearPagePrivate2(page)) {
7578 struct btrfs_ordered_inode_tree *tree;
7579 u64 new_len;
7580
7581 tree = &BTRFS_I(inode)->ordered_tree;
7582
7583 spin_lock_irq(&tree->lock);
7584 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
7585 new_len = page_start - ordered->file_offset;
7586 if (new_len < ordered->truncated_len)
7587 ordered->truncated_len = new_len;
7588 spin_unlock_irq(&tree->lock);
7589
7590 if (btrfs_dec_test_ordered_pending(inode, &ordered,
7591 page_start,
7592 PAGE_CACHE_SIZE, 1))
7593 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04007594 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007595 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00007596 if (!inode_evicting) {
7597 cached_state = NULL;
7598 lock_extent_bits(tree, page_start, page_end, 0,
7599 &cached_state);
7600 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007601 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00007602
7603 if (!inode_evicting) {
7604 clear_extent_bit(tree, page_start, page_end,
7605 EXTENT_LOCKED | EXTENT_DIRTY |
7606 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
7607 EXTENT_DEFRAG, 1, 1,
7608 &cached_state, GFP_NOFS);
7609
7610 __btrfs_releasepage(page, GFP_NOFS);
7611 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007612
Chris Mason4a096752008-07-21 10:29:44 -04007613 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04007614 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04007615 ClearPagePrivate(page);
7616 set_page_private(page, 0);
7617 page_cache_release(page);
7618 }
Chris Mason39279cc2007-06-12 06:35:45 -04007619}
7620
Chris Mason9ebefb182007-06-15 13:50:00 -04007621/*
7622 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
7623 * called from a page fault handler when a page is first dirtied. Hence we must
7624 * be careful to check for EOF conditions here. We set the page up correctly
7625 * for a written page which means we get ENOSPC checking when writing into
7626 * holes and correct delalloc and unwritten extent mapping on filesystems that
7627 * support these features.
7628 *
7629 * We are not allowed to take the i_mutex here so we have to play games to
7630 * protect against truncate races as the page could now be beyond EOF. Because
7631 * vmtruncate() writes the inode size before removing pages, once we have the
7632 * page lock we can determine safely if the page is beyond EOF. If it is not
7633 * beyond EOF, then the page is guaranteed safe against truncation until we
7634 * unlock the page.
7635 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07007636int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04007637{
Nick Pigginc2ec1752009-03-31 15:23:21 -07007638 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05007639 struct inode *inode = file_inode(vma->vm_file);
Chris Mason1832a6d2007-12-21 16:27:21 -05007640 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007641 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7642 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007643 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007644 char *kaddr;
7645 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04007646 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05007647 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05007648 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007649 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007650 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04007651
Jan Karab2b5ef52012-06-12 16:20:45 +02007652 sb_start_pagefault(inode->i_sb);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04007653 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007654 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04007655 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05007656 reserved = 1;
7657 }
Nick Piggin56a76f82009-03-31 15:23:23 -07007658 if (ret) {
7659 if (ret == -ENOMEM)
7660 ret = VM_FAULT_OOM;
7661 else /* -ENOSPC, -EIO, etc */
7662 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05007663 if (reserved)
7664 goto out;
7665 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07007666 }
Chris Mason1832a6d2007-12-21 16:27:21 -05007667
Nick Piggin56a76f82009-03-31 15:23:23 -07007668 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007669again:
Chris Mason9ebefb182007-06-15 13:50:00 -04007670 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04007671 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007672 page_start = page_offset(page);
7673 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04007674
Chris Mason9ebefb182007-06-15 13:50:00 -04007675 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04007676 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04007677 /* page got truncated out from underneath us */
7678 goto out_unlock;
7679 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007680 wait_on_page_writeback(page);
7681
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007682 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007683 set_page_extent_mapped(page);
7684
Chris Masoneb84ae02008-07-17 13:53:27 -04007685 /*
7686 * we can't set the delalloc bits if there are pending ordered
7687 * extents. Drop our locks and wait for them to finish
7688 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007689 ordered = btrfs_lookup_ordered_extent(inode, page_start);
7690 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007691 unlock_extent_cached(io_tree, page_start, page_end,
7692 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007693 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04007694 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007695 btrfs_put_ordered_extent(ordered);
7696 goto again;
7697 }
7698
Josef Bacikfbf19082009-10-01 17:10:23 -04007699 /*
7700 * XXX - page_mkwrite gets called every time the page is dirtied, even
7701 * if it was already dirty, so for space accounting reasons we need to
7702 * clear any delalloc bits for the range we are fixing to save. There
7703 * is probably a better way to do this, but for now keep consistent with
7704 * prepare_pages in the normal write path.
7705 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00007706 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06007707 EXTENT_DIRTY | EXTENT_DELALLOC |
7708 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00007709 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04007710
Josef Bacik2ac55d42010-02-03 19:33:23 +00007711 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
7712 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007713 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007714 unlock_extent_cached(io_tree, page_start, page_end,
7715 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007716 ret = VM_FAULT_SIGBUS;
7717 goto out_unlock;
7718 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007719 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04007720
7721 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04007722 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04007723 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04007724 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04007725 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04007726
Chris Masone6dcd2d2008-07-17 12:53:50 -04007727 if (zero_start != PAGE_CACHE_SIZE) {
7728 kaddr = kmap(page);
7729 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
7730 flush_dcache_page(page);
7731 kunmap(page);
7732 }
Chris Mason247e7432008-07-17 12:53:51 -04007733 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007734 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04007735 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007736
Chris Mason257c62e2009-10-13 13:21:08 -04007737 BTRFS_I(inode)->last_trans = root->fs_info->generation;
7738 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06007739 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04007740
Josef Bacik2ac55d42010-02-03 19:33:23 +00007741 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04007742
7743out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02007744 if (!ret) {
7745 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04007746 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02007747 }
Chris Mason9ebefb182007-06-15 13:50:00 -04007748 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05007749out:
Josef Bacikec39e182012-01-12 19:10:12 -05007750 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007751out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02007752 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04007753 return ret;
7754}
7755
Josef Bacika41ad392011-01-31 15:30:16 -05007756static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04007757{
7758 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04007759 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04007760 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05007761 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007762 struct btrfs_trans_handle *trans;
Chris Masondbe674a2008-07-17 12:54:05 -04007763 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04007764 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04007765
Josef Bacik0ef8b722013-10-25 16:13:35 -04007766 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
7767 (u64)-1);
7768 if (ret)
7769 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04007770
Josef Bacikfcb80c22011-05-03 10:40:22 -04007771 /*
7772 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
7773 * 3 things going on here
7774 *
7775 * 1) We need to reserve space for our orphan item and the space to
7776 * delete our orphan item. Lord knows we don't want to have a dangling
7777 * orphan item because we didn't reserve space to remove it.
7778 *
7779 * 2) We need to reserve space to update our inode.
7780 *
7781 * 3) We need to have something to cache all the space that is going to
7782 * be free'd up by the truncate operation, but also have some slack
7783 * space reserved in case it uses space during the truncate (thank you
7784 * very much snapshotting).
7785 *
7786 * And we need these to all be seperate. The fact is we can use alot of
7787 * space doing the truncate, and we have no earthly idea how much space
7788 * we will use, so we need the truncate reservation to be seperate so it
7789 * doesn't end up using space reserved for updating the inode or
7790 * removing the orphan item. We also need to be able to stop the
7791 * transaction and start a new one, which means we need to be able to
7792 * update the inode several times, and we have no idea of knowing how
7793 * many times that will be, so we can't just reserve 1 item for the
7794 * entirety of the opration, so that has to be done seperately as well.
7795 * Then there is the orphan item, which does indeed need to be held on
7796 * to for the whole operation, and we need nobody to touch this reserved
7797 * space except the orphan code.
7798 *
7799 * So that leaves us with
7800 *
7801 * 1) root->orphan_block_rsv - for the orphan deletion.
7802 * 2) rsv - for the truncate reservation, which we will steal from the
7803 * transaction reservation.
7804 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
7805 * updating the inode.
7806 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06007807 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007808 if (!rsv)
7809 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04007810 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04007811 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05007812
Josef Bacik907cbce2011-08-08 13:46:15 -04007813 /*
Josef Bacik07127182011-08-19 10:29:59 -04007814 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04007815 * 1 for updating the inode.
7816 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05007817 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007818 if (IS_ERR(trans)) {
7819 err = PTR_ERR(trans);
7820 goto out;
7821 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05007822
Josef Bacik907cbce2011-08-08 13:46:15 -04007823 /* Migrate the slack space for the truncate to our reserve */
7824 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
7825 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007826 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05007827
Chris Mason5a3f23d2009-03-31 13:27:11 -04007828 /*
7829 * setattr is responsible for setting the ordered_data_close flag,
7830 * but that is only tested during the last file release. That
7831 * could happen well after the next commit, leaving a great big
7832 * window where new writes may get lost if someone chooses to write
7833 * to this file after truncating to zero
7834 *
7835 * The inode doesn't have any dirty data here, and so if we commit
7836 * this is a noop. If someone immediately starts writing to the inode
7837 * it is very likely we'll catch some of their writes in this
7838 * transaction, and the commit will find this file on the ordered
7839 * data list with good things to send down.
7840 *
7841 * This is a best effort solution, there is still a window where
7842 * using truncate to replace the contents of the file will
7843 * end up with a zero length file after a crash.
7844 */
Josef Bacik72ac3c02012-05-23 14:13:11 -04007845 if (inode->i_size == 0 && test_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
7846 &BTRFS_I(inode)->runtime_flags))
Chris Mason5a3f23d2009-03-31 13:27:11 -04007847 btrfs_add_ordered_operation(trans, root, inode);
7848
Josef Bacik5dc562c2012-08-17 13:14:17 -04007849 /*
7850 * So if we truncate and then write and fsync we normally would just
7851 * write the extents that changed, which is a problem if we need to
7852 * first truncate that entire inode. So set this flag so we write out
7853 * all of the extents in the inode to the sync log so we're completely
7854 * safe.
7855 */
7856 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007857 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04007858
Yan, Zheng80825102009-11-12 09:35:36 +00007859 while (1) {
7860 ret = btrfs_truncate_inode_items(trans, root, inode,
7861 inode->i_size,
7862 BTRFS_EXTENT_DATA_KEY);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007863 if (ret != -ENOSPC) {
Josef Bacik3893e332011-01-31 16:03:11 -05007864 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007865 break;
Josef Bacik3893e332011-01-31 16:03:11 -05007866 }
Chris Mason39279cc2007-06-12 06:35:45 -04007867
Josef Bacikfcb80c22011-05-03 10:40:22 -04007868 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007869 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007870 if (ret) {
7871 err = ret;
7872 break;
7873 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04007874
Yan, Zheng80825102009-11-12 09:35:36 +00007875 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007876 btrfs_btree_balance_dirty(root);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007877
7878 trans = btrfs_start_transaction(root, 2);
7879 if (IS_ERR(trans)) {
7880 ret = err = PTR_ERR(trans);
7881 trans = NULL;
7882 break;
7883 }
7884
7885 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
7886 rsv, min_size);
7887 BUG_ON(ret); /* shouldn't happen */
7888 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007889 }
7890
7891 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04007892 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007893 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007894 if (ret)
7895 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007896 }
7897
Chris Mason917c16b2011-11-08 14:49:59 -05007898 if (trans) {
7899 trans->block_rsv = &root->fs_info->trans_block_rsv;
7900 ret = btrfs_update_inode(trans, root, inode);
7901 if (ret && !err)
7902 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04007903
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007904 ret = btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007905 btrfs_btree_balance_dirty(root);
Chris Mason917c16b2011-11-08 14:49:59 -05007906 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04007907
7908out:
7909 btrfs_free_block_rsv(root, rsv);
7910
Josef Bacik3893e332011-01-31 16:03:11 -05007911 if (ret && !err)
7912 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05007913
Josef Bacik3893e332011-01-31 16:03:11 -05007914 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007915}
7916
Sven Wegener3b963622008-06-09 21:57:42 -04007917/*
Chris Masond352ac62008-09-29 15:18:18 -04007918 * create a new subvolume directory/inode (helper for the ioctl).
7919 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05007920int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00007921 struct btrfs_root *new_root,
7922 struct btrfs_root *parent_root,
7923 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04007924{
Chris Mason39279cc2007-06-12 06:35:45 -04007925 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04007926 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007927 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007928
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01007929 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
7930 new_dirid, new_dirid,
7931 S_IFDIR | (~current_umask() & S_IRWXUGO),
7932 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04007933 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04007934 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007935 inode->i_op = &btrfs_dir_inode_operations;
7936 inode->i_fop = &btrfs_dir_file_operations;
7937
Miklos Szeredibfe86842011-10-28 14:13:29 +02007938 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04007939 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04007940
Filipe David Borba Manana63541922014-01-07 11:47:46 +00007941 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
7942 if (err)
7943 btrfs_err(new_root->fs_info,
7944 "error inheriting subvolume %llu properties: %d\n",
7945 new_root->root_key.objectid, err);
7946
Yan, Zheng76dda932009-09-21 16:00:26 -04007947 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04007948
Yan, Zheng76dda932009-09-21 16:00:26 -04007949 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07007950 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007951}
7952
Chris Mason39279cc2007-06-12 06:35:45 -04007953struct inode *btrfs_alloc_inode(struct super_block *sb)
7954{
7955 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007956 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007957
7958 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
7959 if (!ei)
7960 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007961
7962 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007963 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04007964 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04007965 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04007966 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007967 ei->delalloc_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007968 ei->disk_i_size = 0;
7969 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04007970 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007971 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08007972 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007973 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06007974 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007975
Josef Bacik9e0baf62011-07-15 15:16:44 +00007976 spin_lock_init(&ei->lock);
7977 ei->outstanding_extents = 0;
7978 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007979
Josef Bacik72ac3c02012-05-23 14:13:11 -04007980 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08007981 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007982
Miao Xie16cdcec2011-04-22 18:12:22 +08007983 ei->delayed_node = NULL;
7984
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007985 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02007986 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02007987 extent_io_tree_init(&ei->io_tree, &inode->i_data);
7988 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04007989 ei->io_tree.track_uptodate = 1;
7990 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05007991 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007992 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05007993 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007994 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007995 INIT_LIST_HEAD(&ei->delalloc_inodes);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007996 INIT_LIST_HEAD(&ei->ordered_operations);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007997 RB_CLEAR_NODE(&ei->rb_node);
7998
7999 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008000}
8001
Josef Bacikaaedb552013-10-11 14:44:09 -04008002#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
8003void btrfs_test_destroy_inode(struct inode *inode)
8004{
8005 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
8006 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8007}
8008#endif
8009
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008010static void btrfs_i_callback(struct rcu_head *head)
8011{
8012 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008013 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8014}
8015
Chris Mason39279cc2007-06-12 06:35:45 -04008016void btrfs_destroy_inode(struct inode *inode)
8017{
Chris Masone6dcd2d2008-07-17 12:53:50 -04008018 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008019 struct btrfs_root *root = BTRFS_I(inode)->root;
8020
Al Virob3d9b7a2012-06-09 13:51:19 -04008021 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04008022 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00008023 WARN_ON(BTRFS_I(inode)->outstanding_extents);
8024 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04008025 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
8026 WARN_ON(BTRFS_I(inode)->csum_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04008027
Chris Mason5a3f23d2009-03-31 13:27:11 -04008028 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05008029 * This can happen where we create an inode, but somebody else also
8030 * created the same inode and we need to destroy the one we already
8031 * created.
8032 */
8033 if (!root)
8034 goto free;
8035
8036 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04008037 * Make sure we're properly removed from the ordered operation
8038 * lists.
8039 */
8040 smp_mb();
8041 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
Miao Xie199c2a92013-05-15 07:48:23 +00008042 spin_lock(&root->fs_info->ordered_root_lock);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008043 list_del_init(&BTRFS_I(inode)->ordered_operations);
Miao Xie199c2a92013-05-15 07:48:23 +00008044 spin_unlock(&root->fs_info->ordered_root_lock);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008045 }
8046
Josef Bacik8a35d952012-05-23 14:26:42 -04008047 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
8048 &BTRFS_I(inode)->runtime_flags)) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008049 btrfs_info(root->fs_info, "inode %llu still on the orphan list",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02008050 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04008051 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04008052 }
Josef Bacik7b128762008-07-24 12:17:14 -04008053
Chris Masond3977122009-01-05 21:25:51 -05008054 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008055 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
8056 if (!ordered)
8057 break;
8058 else {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008059 btrfs_err(root->fs_info, "found ordered extent %llu %llu on inode cleanup",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02008060 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008061 btrfs_remove_ordered_extent(inode, ordered);
8062 btrfs_put_ordered_extent(ordered);
8063 btrfs_put_ordered_extent(ordered);
8064 }
8065 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008066 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008067 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05008068free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008069 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04008070}
8071
Al Viro45321ac2010-06-07 13:43:19 -04008072int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04008073{
8074 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04008075
Naohiro Aota6379ef92013-06-06 09:56:34 +00008076 if (root == NULL)
8077 return 1;
8078
Liu Bofa6ac872013-02-20 14:10:23 +00008079 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02008080 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04008081 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04008082 else
Al Viro45321ac2010-06-07 13:43:19 -04008083 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04008084}
8085
Sven Wegener0ee0fda2008-07-30 16:54:26 -04008086static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04008087{
8088 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
8089
8090 inode_init_once(&ei->vfs_inode);
8091}
8092
8093void btrfs_destroy_cachep(void)
8094{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10008095 /*
8096 * Make sure all delayed rcu free inodes are flushed before we
8097 * destroy cache.
8098 */
8099 rcu_barrier();
Chris Mason39279cc2007-06-12 06:35:45 -04008100 if (btrfs_inode_cachep)
8101 kmem_cache_destroy(btrfs_inode_cachep);
8102 if (btrfs_trans_handle_cachep)
8103 kmem_cache_destroy(btrfs_trans_handle_cachep);
8104 if (btrfs_transaction_cachep)
8105 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008106 if (btrfs_path_cachep)
8107 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05008108 if (btrfs_free_space_cachep)
8109 kmem_cache_destroy(btrfs_free_space_cachep);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008110 if (btrfs_delalloc_work_cachep)
8111 kmem_cache_destroy(btrfs_delalloc_work_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008112}
8113
8114int btrfs_init_cachep(void)
8115{
David Sterba837e1972012-09-07 03:00:48 -06008116 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008117 sizeof(struct btrfs_inode), 0,
8118 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04008119 if (!btrfs_inode_cachep)
8120 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008121
David Sterba837e1972012-09-07 03:00:48 -06008122 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008123 sizeof(struct btrfs_trans_handle), 0,
8124 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008125 if (!btrfs_trans_handle_cachep)
8126 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008127
David Sterba837e1972012-09-07 03:00:48 -06008128 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008129 sizeof(struct btrfs_transaction), 0,
8130 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008131 if (!btrfs_transaction_cachep)
8132 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008133
David Sterba837e1972012-09-07 03:00:48 -06008134 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008135 sizeof(struct btrfs_path), 0,
8136 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008137 if (!btrfs_path_cachep)
8138 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008139
David Sterba837e1972012-09-07 03:00:48 -06008140 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05008141 sizeof(struct btrfs_free_space), 0,
8142 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
8143 if (!btrfs_free_space_cachep)
8144 goto fail;
8145
Miao Xie8ccf6f192012-10-25 09:28:04 +00008146 btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
8147 sizeof(struct btrfs_delalloc_work), 0,
8148 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
8149 NULL);
8150 if (!btrfs_delalloc_work_cachep)
8151 goto fail;
8152
Chris Mason39279cc2007-06-12 06:35:45 -04008153 return 0;
8154fail:
8155 btrfs_destroy_cachep();
8156 return -ENOMEM;
8157}
8158
8159static int btrfs_getattr(struct vfsmount *mnt,
8160 struct dentry *dentry, struct kstat *stat)
8161{
Miao Xiedf0af1a2013-01-29 10:11:59 +00008162 u64 delalloc_bytes;
Chris Mason39279cc2007-06-12 06:35:45 -04008163 struct inode *inode = dentry->d_inode;
David Sterbafadc0d82011-11-20 07:33:38 -05008164 u32 blocksize = inode->i_sb->s_blocksize;
8165
Chris Mason39279cc2007-06-12 06:35:45 -04008166 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04008167 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05008168 stat->blksize = PAGE_CACHE_SIZE;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008169
8170 spin_lock(&BTRFS_I(inode)->lock);
8171 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
8172 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05008173 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00008174 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04008175 return 0;
8176}
8177
Chris Masond3977122009-01-05 21:25:51 -05008178static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
8179 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04008180{
8181 struct btrfs_trans_handle *trans;
8182 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008183 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04008184 struct inode *new_inode = new_dentry->d_inode;
8185 struct inode *old_inode = old_dentry->d_inode;
8186 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008187 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008188 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04008189 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08008190 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008191
Li Zefan33345d012011-04-20 10:31:50 +08008192 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04008193 return -EPERM;
8194
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008195 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08008196 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05008197 return -EXDEV;
8198
Li Zefan33345d012011-04-20 10:31:50 +08008199 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
8200 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008201 return -ENOTEMPTY;
8202
Chris Mason39279cc2007-06-12 06:35:45 -04008203 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008204 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04008205 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05008206
8207
8208 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04008209 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05008210 new_dentry->d_name.name,
8211 new_dentry->d_name.len);
8212
8213 if (ret) {
8214 if (ret == -EEXIST) {
8215 /* we shouldn't get
8216 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05308217 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05008218 return ret;
8219 }
8220 } else {
8221 /* maybe -EOVERFLOW */
8222 return ret;
8223 }
8224 }
8225 ret = 0;
8226
Chris Mason5a3f23d2009-03-31 13:27:11 -04008227 /*
8228 * we're using rename to replace one file with another.
8229 * and the replacement file is large. Start IO on it now so
8230 * we don't add too much work to the end of the transaction
8231 */
Bartlomiej Zolnierkiewicz4baf8c92009-08-07 13:47:08 -04008232 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
Chris Mason5a3f23d2009-03-31 13:27:11 -04008233 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
8234 filemap_flush(old_inode->i_mapping);
8235
Yan, Zheng76dda932009-09-21 16:00:26 -04008236 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08008237 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008238 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008239 /*
8240 * We want to reserve the absolute worst case amount of items. So if
8241 * both inodes are subvols and we need to unlink them then that would
8242 * require 4 item modifications, but if they are both normal inodes it
8243 * would require 5 item modifications, so we'll assume their normal
8244 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
8245 * should cover the worst case number of items we'll modify.
8246 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04008247 trans = btrfs_start_transaction(root, 11);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008248 if (IS_ERR(trans)) {
8249 ret = PTR_ERR(trans);
8250 goto out_notrans;
8251 }
Chris Mason5f39d392007-10-15 16:14:19 -04008252
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008253 if (dest != root)
8254 btrfs_record_root_in_trans(trans, dest);
8255
Yan, Zhenga5719522009-09-24 09:17:31 -04008256 ret = btrfs_set_inode_index(new_dir, &index);
8257 if (ret)
8258 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008259
Miao Xie67de1172013-12-26 13:07:06 +08008260 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08008261 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008262 /* force full log commit if subvolume involved. */
8263 root->fs_info->last_trans_log_full_commit = trans->transid;
8264 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04008265 ret = btrfs_insert_inode_ref(trans, dest,
8266 new_dentry->d_name.name,
8267 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08008268 old_ino,
8269 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04008270 if (ret)
8271 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008272 /*
8273 * this is an ugly little race, but the rename is required
8274 * to make sure that if we crash, the inode is either at the
8275 * old name or the new one. pinning the log transaction lets
8276 * us make sure we don't allow a log commit to come in after
8277 * we unlink the name but before we add the new name back in.
8278 */
8279 btrfs_pin_log_trans(root);
8280 }
Chris Mason12fcfd22009-03-24 10:24:20 -04008281 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04008282 * make sure the inode gets flushed if it is replacing
8283 * something.
8284 */
Li Zefan33345d012011-04-20 10:31:50 +08008285 if (new_inode && new_inode->i_size && S_ISREG(old_inode->i_mode))
Chris Mason5a3f23d2009-03-31 13:27:11 -04008286 btrfs_add_ordered_operation(trans, root, old_inode);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008287
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008288 inode_inc_iversion(old_dir);
8289 inode_inc_iversion(new_dir);
8290 inode_inc_iversion(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008291 old_dir->i_ctime = old_dir->i_mtime = ctime;
8292 new_dir->i_ctime = new_dir->i_mtime = ctime;
8293 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04008294
Chris Mason12fcfd22009-03-24 10:24:20 -04008295 if (old_dentry->d_parent != new_dentry->d_parent)
8296 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
8297
Li Zefan33345d012011-04-20 10:31:50 +08008298 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008299 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
8300 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
8301 old_dentry->d_name.name,
8302 old_dentry->d_name.len);
8303 } else {
Al Viro92986792011-03-04 17:14:37 +00008304 ret = __btrfs_unlink_inode(trans, root, old_dir,
8305 old_dentry->d_inode,
8306 old_dentry->d_name.name,
8307 old_dentry->d_name.len);
8308 if (!ret)
8309 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008310 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008311 if (ret) {
8312 btrfs_abort_transaction(trans, root, ret);
8313 goto out_fail;
8314 }
Chris Mason39279cc2007-06-12 06:35:45 -04008315
8316 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008317 inode_inc_iversion(new_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008318 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08008319 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008320 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
8321 root_objectid = BTRFS_I(new_inode)->location.objectid;
8322 ret = btrfs_unlink_subvol(trans, dest, new_dir,
8323 root_objectid,
8324 new_dentry->d_name.name,
8325 new_dentry->d_name.len);
8326 BUG_ON(new_inode->i_nlink == 0);
8327 } else {
8328 ret = btrfs_unlink_inode(trans, dest, new_dir,
8329 new_dentry->d_inode,
8330 new_dentry->d_name.name,
8331 new_dentry->d_name.len);
8332 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04008333 if (!ret && new_inode->i_nlink == 0)
Chris Masone02119d2008-09-05 16:13:11 -04008334 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008335 if (ret) {
8336 btrfs_abort_transaction(trans, root, ret);
8337 goto out_fail;
8338 }
Chris Mason39279cc2007-06-12 06:35:45 -04008339 }
Josef Bacikaec74772008-07-24 12:12:38 -04008340
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008341 ret = btrfs_add_link(trans, new_dir, old_inode,
8342 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04008343 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008344 if (ret) {
8345 btrfs_abort_transaction(trans, root, ret);
8346 goto out_fail;
8347 }
Chris Mason39279cc2007-06-12 06:35:45 -04008348
Miao Xie67de1172013-12-26 13:07:06 +08008349 if (old_inode->i_nlink == 1)
8350 BTRFS_I(old_inode)->dir_index = index;
8351
Li Zefan33345d012011-04-20 10:31:50 +08008352 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04008353 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00008354 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008355 btrfs_end_log_trans(root);
8356 }
Chris Mason39279cc2007-06-12 06:35:45 -04008357out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008358 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008359out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08008360 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008361 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008362
Chris Mason39279cc2007-06-12 06:35:45 -04008363 return ret;
8364}
8365
Miao Xie8ccf6f192012-10-25 09:28:04 +00008366static void btrfs_run_delalloc_work(struct btrfs_work *work)
8367{
8368 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04008369 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008370
8371 delalloc_work = container_of(work, struct btrfs_delalloc_work,
8372 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04008373 inode = delalloc_work->inode;
8374 if (delalloc_work->wait) {
8375 btrfs_wait_ordered_range(inode, 0, (u64)-1);
8376 } else {
8377 filemap_flush(inode->i_mapping);
8378 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8379 &BTRFS_I(inode)->runtime_flags))
8380 filemap_flush(inode->i_mapping);
8381 }
Miao Xie8ccf6f192012-10-25 09:28:04 +00008382
8383 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -04008384 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008385 else
Josef Bacik9f23e282013-10-28 15:03:41 -04008386 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008387 complete(&delalloc_work->completion);
8388}
8389
8390struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
8391 int wait, int delay_iput)
8392{
8393 struct btrfs_delalloc_work *work;
8394
8395 work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
8396 if (!work)
8397 return NULL;
8398
8399 init_completion(&work->completion);
8400 INIT_LIST_HEAD(&work->list);
8401 work->inode = inode;
8402 work->wait = wait;
8403 work->delay_iput = delay_iput;
8404 work->work.func = btrfs_run_delalloc_work;
8405
8406 return work;
8407}
8408
8409void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
8410{
8411 wait_for_completion(&work->completion);
8412 kmem_cache_free(btrfs_delalloc_work_cachep, work);
8413}
8414
Chris Masond352ac62008-09-29 15:18:18 -04008415/*
8416 * some fairly slow code that needs optimization. This walks the list
8417 * of all the inodes with pending delalloc and forces them to disk.
8418 */
Miao Xieeb73c1b2013-05-15 07:48:22 +00008419static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
Chris Masonea8c2812008-08-04 23:17:27 -04008420{
Chris Masonea8c2812008-08-04 23:17:27 -04008421 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008422 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008423 struct btrfs_delalloc_work *work, *next;
8424 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00008425 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008426 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04008427
Miao Xie8ccf6f192012-10-25 09:28:04 +00008428 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008429 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00008430
Miao Xieeb73c1b2013-05-15 07:48:22 +00008431 spin_lock(&root->delalloc_lock);
8432 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008433 while (!list_empty(&splice)) {
8434 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04008435 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008436
Miao Xieeb73c1b2013-05-15 07:48:22 +00008437 list_move_tail(&binode->delalloc_inodes,
8438 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008439 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00008440 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00008441 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008442 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008443 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008444 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008445
8446 work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
8447 if (unlikely(!work)) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -04008448 if (delay_iput)
8449 btrfs_add_delayed_iput(inode);
8450 else
8451 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008452 ret = -ENOMEM;
8453 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008454 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008455 list_add_tail(&work->list, &works);
8456 btrfs_queue_worker(&root->fs_info->flush_workers,
8457 &work->work);
8458
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008459 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00008460 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04008461 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008462 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04008463
Miao Xie1eafa6c2013-01-22 10:49:00 +00008464 list_for_each_entry_safe(work, next, &works, list) {
8465 list_del_init(&work->list);
8466 btrfs_wait_and_free_delalloc_work(work);
8467 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008468 return 0;
8469out:
8470 list_for_each_entry_safe(work, next, &works, list) {
8471 list_del_init(&work->list);
8472 btrfs_wait_and_free_delalloc_work(work);
8473 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008474
Miao Xieeb73c1b2013-05-15 07:48:22 +00008475 if (!list_empty_careful(&splice)) {
8476 spin_lock(&root->delalloc_lock);
8477 list_splice_tail(&splice, &root->delalloc_inodes);
8478 spin_unlock(&root->delalloc_lock);
8479 }
8480 return ret;
8481}
8482
8483int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
8484{
8485 int ret;
8486
Wang Shilong2c21b4d2014-01-14 19:42:20 +08008487 if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00008488 return -EROFS;
8489
8490 ret = __start_delalloc_inodes(root, delay_iput);
8491 /*
8492 * the filemap_flush will queue IO into the worker threads, but
Chris Mason8c8bee12008-09-29 11:19:10 -04008493 * we have to make sure the IO is actually started and that
8494 * ordered extents get created before we return
8495 */
8496 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05008497 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05008498 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04008499 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05008500 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
8501 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04008502 }
8503 atomic_dec(&root->fs_info->async_submit_draining);
Miao Xieeb73c1b2013-05-15 07:48:22 +00008504 return ret;
8505}
8506
Miao Xie91aef862013-11-04 23:13:26 +08008507int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput)
Miao Xieeb73c1b2013-05-15 07:48:22 +00008508{
8509 struct btrfs_root *root;
8510 struct list_head splice;
8511 int ret;
8512
Wang Shilong2c21b4d2014-01-14 19:42:20 +08008513 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00008514 return -EROFS;
8515
8516 INIT_LIST_HEAD(&splice);
8517
8518 spin_lock(&fs_info->delalloc_root_lock);
8519 list_splice_init(&fs_info->delalloc_roots, &splice);
8520 while (!list_empty(&splice)) {
8521 root = list_first_entry(&splice, struct btrfs_root,
8522 delalloc_root);
8523 root = btrfs_grab_fs_root(root);
8524 BUG_ON(!root);
8525 list_move_tail(&root->delalloc_root,
8526 &fs_info->delalloc_roots);
8527 spin_unlock(&fs_info->delalloc_root_lock);
8528
8529 ret = __start_delalloc_inodes(root, delay_iput);
8530 btrfs_put_fs_root(root);
8531 if (ret)
8532 goto out;
8533
8534 spin_lock(&fs_info->delalloc_root_lock);
8535 }
8536 spin_unlock(&fs_info->delalloc_root_lock);
8537
8538 atomic_inc(&fs_info->async_submit_draining);
8539 while (atomic_read(&fs_info->nr_async_submits) ||
8540 atomic_read(&fs_info->async_delalloc_pages)) {
8541 wait_event(fs_info->async_submit_wait,
8542 (atomic_read(&fs_info->nr_async_submits) == 0 &&
8543 atomic_read(&fs_info->async_delalloc_pages) == 0));
8544 }
8545 atomic_dec(&fs_info->async_submit_draining);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008546 return 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008547out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00008548 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00008549 spin_lock(&fs_info->delalloc_root_lock);
8550 list_splice_tail(&splice, &fs_info->delalloc_roots);
8551 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008552 }
Miao Xie8ccf6f192012-10-25 09:28:04 +00008553 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04008554}
8555
Chris Mason39279cc2007-06-12 06:35:45 -04008556static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
8557 const char *symname)
8558{
8559 struct btrfs_trans_handle *trans;
8560 struct btrfs_root *root = BTRFS_I(dir)->root;
8561 struct btrfs_path *path;
8562 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05008563 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04008564 int err;
8565 int drop_inode = 0;
8566 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05308567 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008568 int name_len;
8569 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04008570 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04008571 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04008572 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04008573
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01008574 name_len = strlen(symname);
Chris Mason39279cc2007-06-12 06:35:45 -04008575 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
8576 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05008577
Josef Bacik9ed74f22009-09-11 16:12:44 -04008578 /*
8579 * 2 items for inode item and ref
8580 * 2 items for dir items
8581 * 1 item for xattr if selinux is on
8582 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04008583 trans = btrfs_start_transaction(root, 5);
8584 if (IS_ERR(trans))
8585 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05008586
Li Zefan581bb052011-04-20 10:06:11 +08008587 err = btrfs_find_free_ino(root, &objectid);
8588 if (err)
8589 goto out_unlock;
8590
Josef Bacikaec74772008-07-24 12:12:38 -04008591 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08008592 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04008593 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008594 if (IS_ERR(inode)) {
8595 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008596 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008597 }
Chris Mason39279cc2007-06-12 06:35:45 -04008598
Eric Paris2a7dba32011-02-01 11:05:39 -05008599 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04008600 if (err) {
8601 drop_inode = 1;
8602 goto out_unlock;
8603 }
8604
Casey Schauflerad19db72011-12-15 10:09:07 -05008605 /*
8606 * If the active LSM wants to access the inode during
8607 * d_instantiate it needs these. Smack checks to see
8608 * if the filesystem supports xattrs by looking at the
8609 * ops vector.
8610 */
8611 inode->i_fop = &btrfs_file_operations;
8612 inode->i_op = &btrfs_file_inode_operations;
8613
Josef Bacika1b075d2010-11-19 20:36:11 +00008614 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04008615 if (err)
8616 drop_inode = 1;
8617 else {
8618 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04008619 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05008620 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04008621 }
Chris Mason39279cc2007-06-12 06:35:45 -04008622 if (drop_inode)
8623 goto out_unlock;
8624
8625 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07008626 if (!path) {
8627 err = -ENOMEM;
8628 drop_inode = 1;
8629 goto out_unlock;
8630 }
Li Zefan33345d012011-04-20 10:31:50 +08008631 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008632 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008633 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
8634 datasize = btrfs_file_extent_calc_inline_size(name_len);
8635 err = btrfs_insert_empty_item(trans, root, path, &key,
8636 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04008637 if (err) {
8638 drop_inode = 1;
Julia Lawallb0839162011-05-14 07:10:51 +00008639 btrfs_free_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04008640 goto out_unlock;
8641 }
Chris Mason5f39d392007-10-15 16:14:19 -04008642 leaf = path->nodes[0];
8643 ei = btrfs_item_ptr(leaf, path->slots[0],
8644 struct btrfs_file_extent_item);
8645 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
8646 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04008647 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04008648 btrfs_set_file_extent_encryption(leaf, ei, 0);
8649 btrfs_set_file_extent_compression(leaf, ei, 0);
8650 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
8651 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
8652
Chris Mason39279cc2007-06-12 06:35:45 -04008653 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04008654 write_extent_buffer(leaf, symname, ptr, name_len);
8655 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04008656 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04008657
Chris Mason39279cc2007-06-12 06:35:45 -04008658 inode->i_op = &btrfs_symlink_inode_operations;
8659 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04008660 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04008661 inode_set_bytes(inode, name_len);
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01008662 btrfs_i_size_write(inode, name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -04008663 err = btrfs_update_inode(trans, root, inode);
8664 if (err)
8665 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04008666
8667out_unlock:
Al Viro08c422c2011-12-23 07:58:13 -05008668 if (!err)
8669 d_instantiate(dentry, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008670 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04008671 if (drop_inode) {
8672 inode_dec_link_count(inode);
8673 iput(inode);
8674 }
Liu Bob53d3f52012-11-14 14:34:34 +00008675 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04008676 return err;
8677}
Chris Mason16432982008-04-10 10:23:21 -04008678
Josef Bacik0af3d002010-06-21 14:48:16 -04008679static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
8680 u64 start, u64 num_bytes, u64 min_size,
8681 loff_t actual_len, u64 *alloc_hint,
8682 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04008683{
Josef Bacik5dc562c2012-08-17 13:14:17 -04008684 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
8685 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04008686 struct btrfs_root *root = BTRFS_I(inode)->root;
8687 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04008688 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00008689 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05008690 u64 cur_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04008691 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04008692 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04008693
Josef Bacik0af3d002010-06-21 14:48:16 -04008694 if (trans)
8695 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04008696 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008697 if (own_trans) {
8698 trans = btrfs_start_transaction(root, 3);
8699 if (IS_ERR(trans)) {
8700 ret = PTR_ERR(trans);
8701 break;
8702 }
Yan Zhengd899e052008-10-30 14:25:28 -04008703 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00008704
Chris Mason154ea282013-03-05 11:11:26 -05008705 cur_bytes = min(num_bytes, 256ULL * 1024 * 1024);
8706 cur_bytes = max(cur_bytes, min_size);
Josef Bacik00361582013-08-14 14:02:47 -04008707 ret = btrfs_reserve_extent(root, cur_bytes, min_size, 0,
8708 *alloc_hint, &ins, 1);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008709 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008710 if (own_trans)
8711 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008712 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00008713 }
8714
Yan Zhengd899e052008-10-30 14:25:28 -04008715 ret = insert_reserved_file_extent(trans, inode,
8716 cur_offset, ins.objectid,
8717 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00008718 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04008719 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008720 if (ret) {
Josef Bacik857cc2f2013-10-07 15:21:08 -04008721 btrfs_free_reserved_extent(root, ins.objectid,
8722 ins.offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008723 btrfs_abort_transaction(trans, root, ret);
8724 if (own_trans)
8725 btrfs_end_transaction(trans, root);
8726 break;
8727 }
Chris Masona1ed8352009-09-11 12:27:37 -04008728 btrfs_drop_extent_cache(inode, cur_offset,
8729 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008730
Josef Bacik5dc562c2012-08-17 13:14:17 -04008731 em = alloc_extent_map();
8732 if (!em) {
8733 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
8734 &BTRFS_I(inode)->runtime_flags);
8735 goto next;
8736 }
8737
8738 em->start = cur_offset;
8739 em->orig_start = cur_offset;
8740 em->len = ins.offset;
8741 em->block_start = ins.objectid;
8742 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05008743 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04008744 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008745 em->bdev = root->fs_info->fs_devices->latest_bdev;
8746 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
8747 em->generation = trans->transid;
8748
8749 while (1) {
8750 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04008751 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04008752 write_unlock(&em_tree->lock);
8753 if (ret != -EEXIST)
8754 break;
8755 btrfs_drop_extent_cache(inode, cur_offset,
8756 cur_offset + ins.offset - 1,
8757 0);
8758 }
8759 free_extent_map(em);
8760next:
Yan Zhengd899e052008-10-30 14:25:28 -04008761 num_bytes -= ins.offset;
8762 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04008763 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00008764
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008765 inode_inc_iversion(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04008766 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02008767 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04008768 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04008769 (actual_len > inode->i_size) &&
8770 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00008771 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00008772 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00008773 else
Josef Bacik55a61d12010-11-22 18:50:32 +00008774 i_size = cur_offset;
8775 i_size_write(inode, i_size);
8776 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008777 }
8778
Yan Zhengd899e052008-10-30 14:25:28 -04008779 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008780
8781 if (ret) {
8782 btrfs_abort_transaction(trans, root, ret);
8783 if (own_trans)
8784 btrfs_end_transaction(trans, root);
8785 break;
8786 }
Yan Zhengd899e052008-10-30 14:25:28 -04008787
Josef Bacik0af3d002010-06-21 14:48:16 -04008788 if (own_trans)
8789 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008790 }
Yan Zhengd899e052008-10-30 14:25:28 -04008791 return ret;
8792}
8793
Josef Bacik0af3d002010-06-21 14:48:16 -04008794int btrfs_prealloc_file_range(struct inode *inode, int mode,
8795 u64 start, u64 num_bytes, u64 min_size,
8796 loff_t actual_len, u64 *alloc_hint)
8797{
8798 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
8799 min_size, actual_len, alloc_hint,
8800 NULL);
8801}
8802
8803int btrfs_prealloc_file_range_trans(struct inode *inode,
8804 struct btrfs_trans_handle *trans, int mode,
8805 u64 start, u64 num_bytes, u64 min_size,
8806 loff_t actual_len, u64 *alloc_hint)
8807{
8808 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
8809 min_size, actual_len, alloc_hint, trans);
8810}
8811
Chris Masone6dcd2d2008-07-17 12:53:50 -04008812static int btrfs_set_page_dirty(struct page *page)
8813{
Chris Masone6dcd2d2008-07-17 12:53:50 -04008814 return __set_page_dirty_nobuffers(page);
8815}
8816
Al Viro10556cb2011-06-20 19:28:19 -04008817static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05008818{
Li Zefanb83cc962010-12-20 16:04:08 +08008819 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00008820 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08008821
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00008822 if (mask & MAY_WRITE &&
8823 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
8824 if (btrfs_root_readonly(root))
8825 return -EROFS;
8826 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
8827 return -EACCES;
8828 }
Al Viro2830ba72011-06-20 19:16:29 -04008829 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05008830}
Chris Mason39279cc2007-06-12 06:35:45 -04008831
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008832static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05008833 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04008834 .lookup = btrfs_lookup,
8835 .create = btrfs_create,
8836 .unlink = btrfs_unlink,
8837 .link = btrfs_link,
8838 .mkdir = btrfs_mkdir,
8839 .rmdir = btrfs_rmdir,
8840 .rename = btrfs_rename,
8841 .symlink = btrfs_symlink,
8842 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04008843 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008844 .setxattr = btrfs_setxattr,
8845 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05008846 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008847 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05008848 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008849 .get_acl = btrfs_get_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -07008850 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008851};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008852static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008853 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05008854 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008855 .get_acl = btrfs_get_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -07008856 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008857};
Yan, Zheng76dda932009-09-21 16:00:26 -04008858
Alexey Dobriyan828c0952009-10-01 15:43:56 -07008859static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008860 .llseek = generic_file_llseek,
8861 .read = generic_read_dir,
Al Viro9cdda8d2013-05-22 16:48:09 -04008862 .iterate = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04008863 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04008864#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04008865 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04008866#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04008867 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04008868 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04008869};
8870
Chris Masond1310b22008-01-24 16:13:08 -05008871static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04008872 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05008873 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04008874 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04008875 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008876 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04008877 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05008878 .set_bit_hook = btrfs_set_bit_hook,
8879 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04008880 .merge_extent_hook = btrfs_merge_extent_hook,
8881 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04008882};
8883
Chris Mason35054392009-01-21 13:11:13 -05008884/*
8885 * btrfs doesn't support the bmap operation because swapfiles
8886 * use bmap to make a mapping of extents in the file. They assume
8887 * these extents won't change over the life of the file and they
8888 * use the bmap result to do IO directly to the drive.
8889 *
8890 * the btrfs bmap call would return logical addresses that aren't
8891 * suitable for IO and they also will change frequently as COW
8892 * operations happen. So, swapfile + btrfs == corruption.
8893 *
8894 * For now we're avoiding this by dropping bmap.
8895 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008896static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008897 .readpage = btrfs_readpage,
8898 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -04008899 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05008900 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04008901 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04008902 .invalidatepage = btrfs_invalidatepage,
8903 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008904 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02008905 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04008906};
8907
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008908static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008909 .readpage = btrfs_readpage,
8910 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04008911 .invalidatepage = btrfs_invalidatepage,
8912 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04008913};
8914
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008915static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008916 .getattr = btrfs_getattr,
8917 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008918 .setxattr = btrfs_setxattr,
8919 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05008920 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008921 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05008922 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008923 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008924 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008925 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008926};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008927static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04008928 .getattr = btrfs_getattr,
8929 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008930 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008931 .setxattr = btrfs_setxattr,
8932 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04008933 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008934 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008935 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008936 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -04008937};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008938static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008939 .readlink = generic_readlink,
8940 .follow_link = page_follow_link_light,
8941 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00008942 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -05008943 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008944 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05008945 .setxattr = btrfs_setxattr,
8946 .getxattr = btrfs_getxattr,
8947 .listxattr = btrfs_listxattr,
8948 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008949 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008950 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008951};
Yan, Zheng76dda932009-09-21 16:00:26 -04008952
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04008953const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04008954 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -04008955 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -04008956};