blob: 36c13e391ae3fe5c4a47c0b842f0c6c6f2a53e7f [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Mason8f18cf12008-04-25 16:53:30 -040019#include <linux/kernel.h>
Chris Mason065631f2008-02-20 12:07:25 -050020#include <linux/bio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040021#include <linux/buffer_head.h>
Sage Weilf2eb0a22008-05-02 14:43:14 -040022#include <linux/file.h>
Chris Mason39279cc2007-06-12 06:35:45 -040023#include <linux/fs.h>
24#include <linux/pagemap.h>
25#include <linux/highmem.h>
26#include <linux/time.h>
27#include <linux/init.h>
28#include <linux/string.h>
Chris Mason39279cc2007-06-12 06:35:45 -040029#include <linux/backing-dev.h>
30#include <linux/mpage.h>
31#include <linux/swap.h>
32#include <linux/writeback.h>
33#include <linux/statfs.h>
34#include <linux/compat.h>
Kent Overstreeta27bb332013-05-07 16:19:08 -070035#include <linux/aio.h>
Chris Mason9ebefb182007-06-15 13:50:00 -040036#include <linux/bit_spinlock.h>
Josef Bacik5103e942007-11-16 11:45:54 -050037#include <linux/xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040038#include <linux/posix_acl.h>
Yan Zhengd899e052008-10-30 14:25:28 -040039#include <linux/falloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090040#include <linux/slab.h>
David Sterba7a36dde2011-05-06 15:33:15 +020041#include <linux/ratelimit.h>
Josef Bacik22c44fe2011-11-30 10:45:38 -050042#include <linux/mount.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000043#include <linux/btrfs.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050044#include <linux/blkdev.h>
Josef Bacikf23b5a52013-06-19 10:16:26 -040045#include <linux/posix_acl_xattr.h>
Chris Mason39279cc2007-06-12 06:35:45 -040046#include "ctree.h"
47#include "disk-io.h"
48#include "transaction.h"
49#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040050#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040051#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040052#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040053#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020054#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040055#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050056#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050057#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080058#include "inode-map.h"
Liu Bo38c227d2013-01-29 03:18:40 +000059#include "backref.h"
Josef Bacikf23b5a52013-06-19 10:16:26 -040060#include "hash.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000061#include "props.h"
Chris Mason39279cc2007-06-12 06:35:45 -040062
63struct btrfs_iget_args {
Chris Mason90d3e592014-01-09 17:28:00 -080064 struct btrfs_key *location;
Chris Mason39279cc2007-06-12 06:35:45 -040065 struct btrfs_root *root;
66};
67
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070068static const struct inode_operations btrfs_dir_inode_operations;
69static const struct inode_operations btrfs_symlink_inode_operations;
70static const struct inode_operations btrfs_dir_ro_inode_operations;
71static const struct inode_operations btrfs_special_inode_operations;
72static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070073static const struct address_space_operations btrfs_aops;
74static const struct address_space_operations btrfs_symlink_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070075static const struct file_operations btrfs_dir_file_operations;
Chris Masond1310b22008-01-24 16:13:08 -050076static struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040077
78static struct kmem_cache *btrfs_inode_cachep;
Miao Xie8ccf6f192012-10-25 09:28:04 +000079static struct kmem_cache *btrfs_delalloc_work_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040080struct kmem_cache *btrfs_trans_handle_cachep;
81struct kmem_cache *btrfs_transaction_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040082struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050083struct kmem_cache *btrfs_free_space_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040084
85#define S_SHIFT 12
86static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
87 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
88 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
89 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
90 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
91 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
92 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
93 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
94};
95
Eric Sandeen3972f262013-01-12 02:57:22 +000096static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Josef Bacika41ad392011-01-31 15:30:16 -050097static int btrfs_truncate(struct inode *inode);
Josef Bacik5fd02042012-05-02 14:00:54 -040098static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Chris Mason771ed682008-11-06 22:02:51 -050099static noinline int cow_file_range(struct inode *inode,
100 struct page *locked_page,
101 u64 start, u64 end, int *page_started,
102 unsigned long *nr_written, int unlock);
Josef Bacik70c8a912012-10-11 16:54:30 -0400103static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
104 u64 len, u64 orig_start,
105 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -0400106 u64 orig_block_len, u64 ram_bytes,
107 int type);
Josef Bacik7b128762008-07-24 12:17:14 -0400108
Eric Sandeen48a3b632013-04-25 20:41:01 +0000109static int btrfs_dirty_inode(struct inode *inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400110
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000111static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500112 struct inode *inode, struct inode *dir,
113 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500114{
115 int err;
116
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000117 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500118 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500119 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500120 return err;
121}
122
Chris Masond352ac62008-09-29 15:18:18 -0400123/*
Chris Masonc8b97812008-10-29 14:49:59 -0400124 * this does all the hard work for inserting an inline extent into
125 * the btree. The caller should have done a btrfs_drop_extents so that
126 * no overlapping inline items exist in the btree
127 */
Chris Masond3977122009-01-05 21:25:51 -0500128static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000129 struct btrfs_path *path, int extent_inserted,
Chris Masonc8b97812008-10-29 14:49:59 -0400130 struct btrfs_root *root, struct inode *inode,
131 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000132 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400133 struct page **compressed_pages)
134{
Chris Masonc8b97812008-10-29 14:49:59 -0400135 struct extent_buffer *leaf;
136 struct page *page = NULL;
137 char *kaddr;
138 unsigned long ptr;
139 struct btrfs_file_extent_item *ei;
140 int err = 0;
141 int ret;
142 size_t cur_size = size;
Chris Masonc8b97812008-10-29 14:49:59 -0400143 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400144
Li Zefanfe3f5662011-03-28 08:30:38 +0000145 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400146 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400147
Chris Masonc8b97812008-10-29 14:49:59 -0400148 inode_add_bytes(inode, size);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000149
150 if (!extent_inserted) {
151 struct btrfs_key key;
152 size_t datasize;
153
154 key.objectid = btrfs_ino(inode);
155 key.offset = start;
156 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
157
158 datasize = btrfs_file_extent_calc_inline_size(cur_size);
159 path->leave_spinning = 1;
160 ret = btrfs_insert_empty_item(trans, root, path, &key,
161 datasize);
162 if (ret) {
163 err = ret;
164 goto fail;
165 }
Chris Masonc8b97812008-10-29 14:49:59 -0400166 }
167 leaf = path->nodes[0];
168 ei = btrfs_item_ptr(leaf, path->slots[0],
169 struct btrfs_file_extent_item);
170 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
171 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
172 btrfs_set_file_extent_encryption(leaf, ei, 0);
173 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
174 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
175 ptr = btrfs_file_extent_inline_start(ei);
176
Li Zefan261507a02010-12-17 14:21:50 +0800177 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400178 struct page *cpage;
179 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500180 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400181 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500182 cur_size = min_t(unsigned long, compressed_size,
Chris Masonc8b97812008-10-29 14:49:59 -0400183 PAGE_CACHE_SIZE);
184
Cong Wang7ac687d2011-11-25 23:14:28 +0800185 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400186 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800187 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400188
189 i++;
190 ptr += cur_size;
191 compressed_size -= cur_size;
192 }
193 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800194 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400195 } else {
196 page = find_get_page(inode->i_mapping,
197 start >> PAGE_CACHE_SHIFT);
198 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800199 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400200 offset = start & (PAGE_CACHE_SIZE - 1);
201 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800202 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400203 page_cache_release(page);
204 }
205 btrfs_mark_buffer_dirty(leaf);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000206 btrfs_release_path(path);
Chris Masonc8b97812008-10-29 14:49:59 -0400207
Yan, Zhengc2167752009-11-12 09:34:21 +0000208 /*
209 * we're an inline extent, so nobody can
210 * extend the file past i_size without locking
211 * a page we already have locked.
212 *
213 * We must do any isize and inode updates
214 * before we unlock the pages. Otherwise we
215 * could end up racing with unlink.
216 */
Chris Masonc8b97812008-10-29 14:49:59 -0400217 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100218 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000219
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100220 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400221fail:
Chris Masonc8b97812008-10-29 14:49:59 -0400222 return err;
223}
224
225
226/*
227 * conditionally insert an inline extent into the file. This
228 * does the checks required to make sure the data is small enough
229 * to fit as an inline extent.
230 */
Josef Bacik00361582013-08-14 14:02:47 -0400231static noinline int cow_file_range_inline(struct btrfs_root *root,
232 struct inode *inode, u64 start,
233 u64 end, size_t compressed_size,
234 int compress_type,
235 struct page **compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400236{
Josef Bacik00361582013-08-14 14:02:47 -0400237 struct btrfs_trans_handle *trans;
Chris Masonc8b97812008-10-29 14:49:59 -0400238 u64 isize = i_size_read(inode);
239 u64 actual_end = min(end + 1, isize);
240 u64 inline_len = actual_end - start;
Qu Wenruofda28322013-02-26 08:10:22 +0000241 u64 aligned_end = ALIGN(end, root->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400242 u64 data_len = inline_len;
243 int ret;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000244 struct btrfs_path *path;
245 int extent_inserted = 0;
246 u32 extent_item_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400247
248 if (compressed_size)
249 data_len = compressed_size;
250
251 if (start > 0 ||
Chris Mason70b99e62008-10-31 12:46:39 -0400252 actual_end >= PAGE_CACHE_SIZE ||
Chris Masonc8b97812008-10-29 14:49:59 -0400253 data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
254 (!compressed_size &&
255 (actual_end & (root->sectorsize - 1)) == 0) ||
256 end + 1 < isize ||
257 data_len > root->fs_info->max_inline) {
258 return 1;
259 }
260
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000261 path = btrfs_alloc_path();
262 if (!path)
263 return -ENOMEM;
264
Josef Bacik00361582013-08-14 14:02:47 -0400265 trans = btrfs_join_transaction(root);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000266 if (IS_ERR(trans)) {
267 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400268 return PTR_ERR(trans);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000269 }
Josef Bacik00361582013-08-14 14:02:47 -0400270 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
271
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000272 if (compressed_size && compressed_pages)
273 extent_item_size = btrfs_file_extent_calc_inline_size(
274 compressed_size);
275 else
276 extent_item_size = btrfs_file_extent_calc_inline_size(
277 inline_len);
278
279 ret = __btrfs_drop_extents(trans, root, inode, path,
280 start, aligned_end, NULL,
281 1, 1, extent_item_size, &extent_inserted);
Josef Bacik00361582013-08-14 14:02:47 -0400282 if (ret) {
283 btrfs_abort_transaction(trans, root, ret);
284 goto out;
285 }
Chris Masonc8b97812008-10-29 14:49:59 -0400286
287 if (isize > actual_end)
288 inline_len = min_t(u64, isize, actual_end);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000289 ret = insert_inline_extent(trans, path, extent_inserted,
290 root, inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400291 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000292 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400293 if (ret && ret != -ENOSPC) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100294 btrfs_abort_transaction(trans, root, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400295 goto out;
Josef Bacik2adcac12012-05-23 16:10:14 -0400296 } else if (ret == -ENOSPC) {
Josef Bacik00361582013-08-14 14:02:47 -0400297 ret = 1;
298 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100299 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400300
Josef Bacikbdc20e62013-02-28 13:23:38 -0500301 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400302 btrfs_delalloc_release_metadata(inode, end + 1 - start);
Chris Masona1ed8352009-09-11 12:27:37 -0400303 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
Josef Bacik00361582013-08-14 14:02:47 -0400304out:
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000305 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400306 btrfs_end_transaction(trans, root);
307 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400308}
309
Chris Mason771ed682008-11-06 22:02:51 -0500310struct async_extent {
311 u64 start;
312 u64 ram_size;
313 u64 compressed_size;
314 struct page **pages;
315 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800316 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500317 struct list_head list;
318};
319
320struct async_cow {
321 struct inode *inode;
322 struct btrfs_root *root;
323 struct page *locked_page;
324 u64 start;
325 u64 end;
326 struct list_head extents;
327 struct btrfs_work work;
328};
329
330static noinline int add_async_extent(struct async_cow *cow,
331 u64 start, u64 ram_size,
332 u64 compressed_size,
333 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800334 unsigned long nr_pages,
335 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500336{
337 struct async_extent *async_extent;
338
339 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100340 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500341 async_extent->start = start;
342 async_extent->ram_size = ram_size;
343 async_extent->compressed_size = compressed_size;
344 async_extent->pages = pages;
345 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800346 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500347 list_add_tail(&async_extent->list, &cow->extents);
348 return 0;
349}
350
Chris Masonc8b97812008-10-29 14:49:59 -0400351/*
Chris Mason771ed682008-11-06 22:02:51 -0500352 * we create compressed extents in two phases. The first
353 * phase compresses a range of pages that have already been
354 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400355 *
Chris Mason771ed682008-11-06 22:02:51 -0500356 * This is done inside an ordered work queue, and the compression
357 * is spread across many cpus. The actual IO submission is step
358 * two, and the ordered work queue takes care of making sure that
359 * happens in the same order things were put onto the queue by
360 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400361 *
Chris Mason771ed682008-11-06 22:02:51 -0500362 * If this code finds it can't get good compression, it puts an
363 * entry onto the work queue to write the uncompressed bytes. This
364 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300365 * are written in the same order that the flusher thread sent them
366 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400367 */
Chris Mason771ed682008-11-06 22:02:51 -0500368static noinline int compress_file_range(struct inode *inode,
369 struct page *locked_page,
370 u64 start, u64 end,
371 struct async_cow *async_cow,
372 int *num_added)
Chris Masonb888db22007-08-27 16:49:44 -0400373{
374 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masondb945352007-10-15 16:15:53 -0400375 u64 num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400376 u64 blocksize = root->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400377 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500378 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400379 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400380 struct page **pages = NULL;
381 unsigned long nr_pages;
382 unsigned long nr_pages_ret = 0;
383 unsigned long total_compressed = 0;
384 unsigned long total_in = 0;
385 unsigned long max_compressed = 128 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -0500386 unsigned long max_uncompressed = 128 * 1024;
Chris Masonc8b97812008-10-29 14:49:59 -0400387 int i;
388 int will_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800389 int compress_type = root->fs_info->compress_type;
Chris Mason4adaa612013-03-26 13:07:00 -0400390 int redirty = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400391
Liu Bo4cb13e52012-03-29 09:57:45 -0400392 /* if this is a small write inside eof, kick off a defrag */
393 if ((end - start + 1) < 16 * 1024 &&
394 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400395 btrfs_add_inode_defrag(NULL, inode);
396
Wang Shilong68bb4622014-04-01 18:01:42 +0800397 /*
398 * skip compression for a small file range(<=blocksize) that
399 * isn't an inline extent, since it dosen't save disk space at all.
400 */
401 if ((end - start + 1) <= blocksize &&
402 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
403 goto cleanup_and_bail_uncompressed;
404
Chris Mason42dc7ba2008-12-15 11:44:56 -0500405 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400406again:
407 will_compress = 0;
408 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
409 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
410
Chris Masonf03d9302009-02-04 09:31:06 -0500411 /*
412 * we don't want to send crud past the end of i_size through
413 * compression, that's just a waste of CPU time. So, if the
414 * end of the file is before the start of our current
415 * requested range of bytes, we bail out to the uncompressed
416 * cleanup code that can deal with all of this.
417 *
418 * It isn't really the fastest way to fix things, but this is a
419 * very uncommon corner.
420 */
421 if (actual_end <= start)
422 goto cleanup_and_bail_uncompressed;
423
Chris Masonc8b97812008-10-29 14:49:59 -0400424 total_compressed = actual_end - start;
425
426 /* we want to make sure that amount of ram required to uncompress
427 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500428 * of a compressed extent to 128k. This is a crucial number
429 * because it also controls how easily we can spread reads across
430 * cpus for decompression.
431 *
432 * We also want to make sure the amount of IO required to do
433 * a random read is reasonably small, so we limit the size of
434 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400435 */
436 total_compressed = min(total_compressed, max_uncompressed);
Qu Wenruofda28322013-02-26 08:10:22 +0000437 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Masonbe20aa92007-12-17 20:14:01 -0500438 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400439 total_in = 0;
440 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400441
Chris Mason771ed682008-11-06 22:02:51 -0500442 /*
443 * we do compression for mount -o compress and when the
444 * inode has not been flagged as nocompress. This flag can
445 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400446 */
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200447 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) &&
Chris Mason1e701a32010-03-11 09:42:04 -0500448 (btrfs_test_opt(root, COMPRESS) ||
Liu Bo75e7cb72011-03-22 10:12:20 +0000449 (BTRFS_I(inode)->force_compress) ||
450 (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS))) {
Chris Masonc8b97812008-10-29 14:49:59 -0400451 WARN_ON(pages);
Chris Masoncfbc2462008-10-30 13:22:14 -0400452 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800453 if (!pages) {
454 /* just bail out to the uncompressed code */
455 goto cont;
456 }
Chris Mason179e29e2007-11-01 11:28:41 -0400457
Li Zefan261507a02010-12-17 14:21:50 +0800458 if (BTRFS_I(inode)->force_compress)
459 compress_type = BTRFS_I(inode)->force_compress;
460
Chris Mason4adaa612013-03-26 13:07:00 -0400461 /*
462 * we need to call clear_page_dirty_for_io on each
463 * page in the range. Otherwise applications with the file
464 * mmap'd can wander in and change the page contents while
465 * we are compressing them.
466 *
467 * If the compression fails for any reason, we set the pages
468 * dirty again later on.
469 */
470 extent_range_clear_dirty_for_io(inode, start, end);
471 redirty = 1;
Li Zefan261507a02010-12-17 14:21:50 +0800472 ret = btrfs_compress_pages(compress_type,
473 inode->i_mapping, start,
474 total_compressed, pages,
475 nr_pages, &nr_pages_ret,
476 &total_in,
477 &total_compressed,
478 max_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400479
480 if (!ret) {
481 unsigned long offset = total_compressed &
482 (PAGE_CACHE_SIZE - 1);
483 struct page *page = pages[nr_pages_ret - 1];
484 char *kaddr;
485
486 /* zero the tail end of the last page, we might be
487 * sending it down to disk
488 */
489 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800490 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400491 memset(kaddr + offset, 0,
492 PAGE_CACHE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800493 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400494 }
495 will_compress = 1;
496 }
497 }
Li Zefan560f7d72011-09-08 10:22:01 +0800498cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400499 if (start == 0) {
500 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500501 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400502 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500503 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400504 */
Josef Bacik00361582013-08-14 14:02:47 -0400505 ret = cow_file_range_inline(root, inode, start, end,
506 0, 0, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400507 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500508 /* try making a compressed inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400509 ret = cow_file_range_inline(root, inode, start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000510 total_compressed,
511 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400512 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100513 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400514 unsigned long clear_flags = EXTENT_DELALLOC |
515 EXTENT_DEFRAG;
516 clear_flags |= (ret < 0) ? EXTENT_DO_ACCOUNTING : 0;
517
Chris Mason771ed682008-11-06 22:02:51 -0500518 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100519 * inline extent creation worked or returned error,
520 * we don't need to create any more async work items.
521 * Unlock and free up our temp pages.
Chris Mason771ed682008-11-06 22:02:51 -0500522 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400523 extent_clear_unlock_delalloc(inode, start, end, NULL,
Josef Bacik151a41b2013-07-29 13:22:24 -0400524 clear_flags, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400525 PAGE_CLEAR_DIRTY |
526 PAGE_SET_WRITEBACK |
527 PAGE_END_WRITEBACK);
Chris Masonc8b97812008-10-29 14:49:59 -0400528 goto free_pages_out;
529 }
530 }
531
532 if (will_compress) {
533 /*
534 * we aren't doing an inline extent round the compressed size
535 * up to a block size boundary so the allocator does sane
536 * things
537 */
Qu Wenruofda28322013-02-26 08:10:22 +0000538 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400539
540 /*
541 * one last check to make sure the compression is really a
542 * win, compare the page count read with the blocks on disk
543 */
Qu Wenruofda28322013-02-26 08:10:22 +0000544 total_in = ALIGN(total_in, PAGE_CACHE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400545 if (total_compressed >= total_in) {
546 will_compress = 0;
547 } else {
Chris Masonc8b97812008-10-29 14:49:59 -0400548 num_bytes = total_in;
549 }
550 }
551 if (!will_compress && pages) {
552 /*
553 * the compression code ran but failed to make things smaller,
554 * free any pages it allocated and our page pointer array
555 */
556 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400557 WARN_ON(pages[i]->mapping);
Chris Masonc8b97812008-10-29 14:49:59 -0400558 page_cache_release(pages[i]);
559 }
560 kfree(pages);
561 pages = NULL;
562 total_compressed = 0;
563 nr_pages_ret = 0;
564
565 /* flag the file so we don't compress in the future */
Chris Mason1e701a32010-03-11 09:42:04 -0500566 if (!btrfs_test_opt(root, FORCE_COMPRESS) &&
567 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500568 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500569 }
Chris Masonc8b97812008-10-29 14:49:59 -0400570 }
Chris Mason771ed682008-11-06 22:02:51 -0500571 if (will_compress) {
572 *num_added += 1;
573
574 /* the async work queues will take care of doing actual
575 * allocation on disk for these compressed pages,
576 * and will submit them to the elevator.
577 */
578 add_async_extent(async_cow, start, num_bytes,
Li Zefan261507a02010-12-17 14:21:50 +0800579 total_compressed, pages, nr_pages_ret,
580 compress_type);
Chris Mason771ed682008-11-06 22:02:51 -0500581
Yan, Zheng24ae6362010-12-06 07:02:36 +0000582 if (start + num_bytes < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500583 start += num_bytes;
584 pages = NULL;
585 cond_resched();
586 goto again;
587 }
588 } else {
Chris Masonf03d9302009-02-04 09:31:06 -0500589cleanup_and_bail_uncompressed:
Chris Mason771ed682008-11-06 22:02:51 -0500590 /*
591 * No compression, but we still need to write the pages in
592 * the file we've been given so far. redirty the locked
593 * page if it corresponds to our extent and set things up
594 * for the async work queue to run cow_file_range to do
595 * the normal delalloc dance
596 */
597 if (page_offset(locked_page) >= start &&
598 page_offset(locked_page) <= end) {
599 __set_page_dirty_nobuffers(locked_page);
600 /* unlocked later on in the async handlers */
601 }
Chris Mason4adaa612013-03-26 13:07:00 -0400602 if (redirty)
603 extent_range_redirty_for_io(inode, start, end);
Li Zefan261507a02010-12-17 14:21:50 +0800604 add_async_extent(async_cow, start, end - start + 1,
605 0, NULL, 0, BTRFS_COMPRESS_NONE);
Chris Mason771ed682008-11-06 22:02:51 -0500606 *num_added += 1;
607 }
608
609out:
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100610 return ret;
Chris Mason771ed682008-11-06 22:02:51 -0500611
612free_pages_out:
613 for (i = 0; i < nr_pages_ret; i++) {
614 WARN_ON(pages[i]->mapping);
615 page_cache_release(pages[i]);
616 }
Chris Masond3977122009-01-05 21:25:51 -0500617 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500618
619 goto out;
620}
621
622/*
623 * phase two of compressed writeback. This is the ordered portion
624 * of the code, which only gets called in the order the work was
625 * queued. We walk all the async extents created by compress_file_range
626 * and send them down to the disk.
627 */
628static noinline int submit_compressed_extents(struct inode *inode,
629 struct async_cow *async_cow)
630{
631 struct async_extent *async_extent;
632 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500633 struct btrfs_key ins;
634 struct extent_map *em;
635 struct btrfs_root *root = BTRFS_I(inode)->root;
636 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
637 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500638 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500639
640 if (list_empty(&async_cow->extents))
641 return 0;
642
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500643again:
Chris Masond3977122009-01-05 21:25:51 -0500644 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500645 async_extent = list_entry(async_cow->extents.next,
646 struct async_extent, list);
647 list_del(&async_extent->list);
648
649 io_tree = &BTRFS_I(inode)->io_tree;
650
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500651retry:
Chris Mason771ed682008-11-06 22:02:51 -0500652 /* did the compression code fall back to uncompressed IO? */
653 if (!async_extent->pages) {
654 int page_started = 0;
655 unsigned long nr_written = 0;
656
657 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000658 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100659 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500660
661 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500662 ret = cow_file_range(inode, async_cow->locked_page,
663 async_extent->start,
664 async_extent->start +
665 async_extent->ram_size - 1,
666 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500667
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100668 /* JDM XXX */
669
Chris Mason771ed682008-11-06 22:02:51 -0500670 /*
671 * if page_started, cow_file_range inserted an
672 * inline extent and took care of all the unlocking
673 * and IO for us. Otherwise, we need to submit
674 * all those pages down to the drive.
675 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500676 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500677 extent_write_locked_range(io_tree,
678 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500679 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500680 async_extent->ram_size - 1,
681 btrfs_get_extent,
682 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500683 else if (ret)
684 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500685 kfree(async_extent);
686 cond_resched();
687 continue;
688 }
689
690 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100691 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500692
Josef Bacik00361582013-08-14 14:02:47 -0400693 ret = btrfs_reserve_extent(root,
Chris Mason771ed682008-11-06 22:02:51 -0500694 async_extent->compressed_size,
695 async_extent->compressed_size,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500696 0, alloc_hint, &ins, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500697 if (ret) {
698 int i;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500699
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500700 for (i = 0; i < async_extent->nr_pages; i++) {
701 WARN_ON(async_extent->pages[i]->mapping);
702 page_cache_release(async_extent->pages[i]);
703 }
704 kfree(async_extent->pages);
705 async_extent->nr_pages = 0;
706 async_extent->pages = NULL;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500707
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400708 if (ret == -ENOSPC) {
709 unlock_extent(io_tree, async_extent->start,
710 async_extent->start +
711 async_extent->ram_size - 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100712 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400713 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500714 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500715 }
716
Yan, Zhengc2167752009-11-12 09:34:21 +0000717 /*
718 * here we're doing allocation and writeback of the
719 * compressed pages
720 */
721 btrfs_drop_extent_cache(inode, async_extent->start,
722 async_extent->start +
723 async_extent->ram_size - 1, 0);
724
David Sterba172ddd62011-04-21 00:48:27 +0200725 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000726 if (!em) {
727 ret = -ENOMEM;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500728 goto out_free_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000729 }
Chris Mason771ed682008-11-06 22:02:51 -0500730 em->start = async_extent->start;
731 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500732 em->orig_start = em->start;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400733 em->mod_start = em->start;
734 em->mod_len = em->len;
Chris Mason771ed682008-11-06 22:02:51 -0500735
736 em->block_start = ins.objectid;
737 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500738 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400739 em->ram_bytes = async_extent->ram_size;
Chris Mason771ed682008-11-06 22:02:51 -0500740 em->bdev = root->fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800741 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500742 set_bit(EXTENT_FLAG_PINNED, &em->flags);
743 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400744 em->generation = -1;
Chris Mason771ed682008-11-06 22:02:51 -0500745
Chris Masond3977122009-01-05 21:25:51 -0500746 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400747 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400748 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400749 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500750 if (ret != -EEXIST) {
751 free_extent_map(em);
752 break;
753 }
754 btrfs_drop_extent_cache(inode, async_extent->start,
755 async_extent->start +
756 async_extent->ram_size - 1, 0);
757 }
758
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500759 if (ret)
760 goto out_free_reserve;
761
Li Zefan261507a02010-12-17 14:21:50 +0800762 ret = btrfs_add_ordered_extent_compress(inode,
763 async_extent->start,
764 ins.objectid,
765 async_extent->ram_size,
766 ins.offset,
767 BTRFS_ORDERED_COMPRESSED,
768 async_extent->compress_type);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500769 if (ret)
770 goto out_free_reserve;
Chris Mason771ed682008-11-06 22:02:51 -0500771
Chris Mason771ed682008-11-06 22:02:51 -0500772 /*
773 * clear dirty, set writeback and unlock the pages.
774 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400775 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400776 async_extent->start +
777 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400778 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
779 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400780 PAGE_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500781 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500782 async_extent->start,
783 async_extent->ram_size,
784 ins.objectid,
785 ins.offset, async_extent->pages,
786 async_extent->nr_pages);
Chris Mason771ed682008-11-06 22:02:51 -0500787 alloc_hint = ins.objectid + ins.offset;
788 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500789 if (ret)
790 goto out;
Chris Mason771ed682008-11-06 22:02:51 -0500791 cond_resched();
792 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100793 ret = 0;
794out:
795 return ret;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500796out_free_reserve:
797 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100798out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400799 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500800 async_extent->start +
801 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400802 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400803 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
804 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
805 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100806 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500807 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500808}
809
Josef Bacik4b46fce2010-05-23 11:00:55 -0400810static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
811 u64 num_bytes)
812{
813 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
814 struct extent_map *em;
815 u64 alloc_hint = 0;
816
817 read_lock(&em_tree->lock);
818 em = search_extent_mapping(em_tree, start, num_bytes);
819 if (em) {
820 /*
821 * if block start isn't an actual block number then find the
822 * first block in this inode and use that as a hint. If that
823 * block is also bogus then just don't worry about it.
824 */
825 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
826 free_extent_map(em);
827 em = search_extent_mapping(em_tree, 0, 0);
828 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
829 alloc_hint = em->block_start;
830 if (em)
831 free_extent_map(em);
832 } else {
833 alloc_hint = em->block_start;
834 free_extent_map(em);
835 }
836 }
837 read_unlock(&em_tree->lock);
838
839 return alloc_hint;
840}
841
Chris Mason771ed682008-11-06 22:02:51 -0500842/*
843 * when extent_io.c finds a delayed allocation range in the file,
844 * the call backs end up in this code. The basic idea is to
845 * allocate extents on disk for the range, and create ordered data structs
846 * in ram to track those extents.
847 *
848 * locked_page is the page that writepage had locked already. We use
849 * it to make sure we don't do extra locks or unlocks.
850 *
851 * *page_started is set to one if we unlock locked_page and do everything
852 * required to start IO on it. It may be clean and already done with
853 * IO when we return.
854 */
Josef Bacik00361582013-08-14 14:02:47 -0400855static noinline int cow_file_range(struct inode *inode,
856 struct page *locked_page,
857 u64 start, u64 end, int *page_started,
858 unsigned long *nr_written,
859 int unlock)
Chris Mason771ed682008-11-06 22:02:51 -0500860{
Josef Bacik00361582013-08-14 14:02:47 -0400861 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500862 u64 alloc_hint = 0;
863 u64 num_bytes;
864 unsigned long ram_size;
865 u64 disk_num_bytes;
866 u64 cur_alloc_size;
867 u64 blocksize = root->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500868 struct btrfs_key ins;
869 struct extent_map *em;
870 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
871 int ret = 0;
872
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400873 if (btrfs_is_free_space_inode(inode)) {
874 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500875 ret = -EINVAL;
876 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400877 }
Chris Mason771ed682008-11-06 22:02:51 -0500878
Qu Wenruofda28322013-02-26 08:10:22 +0000879 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500880 num_bytes = max(blocksize, num_bytes);
881 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500882
Chris Mason4cb53002011-05-24 15:35:30 -0400883 /* if this is a small write inside eof, kick off defrag */
Liu Bo4cb13e52012-03-29 09:57:45 -0400884 if (num_bytes < 64 * 1024 &&
885 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Josef Bacik00361582013-08-14 14:02:47 -0400886 btrfs_add_inode_defrag(NULL, inode);
Chris Mason4cb53002011-05-24 15:35:30 -0400887
Chris Mason771ed682008-11-06 22:02:51 -0500888 if (start == 0) {
889 /* lets try to make an inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400890 ret = cow_file_range_inline(root, inode, start, end, 0, 0,
891 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500892 if (ret == 0) {
Josef Bacikc2790a22013-07-29 11:20:47 -0400893 extent_clear_unlock_delalloc(inode, start, end, NULL,
894 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400895 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400896 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
897 PAGE_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000898
Chris Mason771ed682008-11-06 22:02:51 -0500899 *nr_written = *nr_written +
900 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
901 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500902 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100903 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100904 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500905 }
906 }
Chris Masonc8b97812008-10-29 14:49:59 -0400907
908 BUG_ON(disk_num_bytes >
David Sterba6c417612011-04-13 15:41:04 +0200909 btrfs_super_total_bytes(root->fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400910
Josef Bacik4b46fce2010-05-23 11:00:55 -0400911 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400912 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400913
Chris Masond3977122009-01-05 21:25:51 -0500914 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400915 unsigned long op;
916
Josef Bacik287a0ab2010-03-19 18:07:23 +0000917 cur_alloc_size = disk_num_bytes;
Josef Bacik00361582013-08-14 14:02:47 -0400918 ret = btrfs_reserve_extent(root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500919 root->sectorsize, 0, alloc_hint,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500920 &ins, 1);
Josef Bacik00361582013-08-14 14:02:47 -0400921 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100922 goto out_unlock;
Chris Masond3977122009-01-05 21:25:51 -0500923
David Sterba172ddd62011-04-21 00:48:27 +0200924 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000925 if (!em) {
926 ret = -ENOMEM;
Liu Boace68ba2013-04-22 10:53:47 +0000927 goto out_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000928 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400929 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500930 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500931 ram_size = ins.offset;
932 em->len = ins.offset;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400933 em->mod_start = em->start;
934 em->mod_len = em->len;
Chris Masonc8b97812008-10-29 14:49:59 -0400935
Chris Masone6dcd2d2008-07-17 12:53:50 -0400936 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400937 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500938 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400939 em->ram_bytes = ram_size;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400940 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400941 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400942 em->generation = -1;
Chris Masonc8b97812008-10-29 14:49:59 -0400943
Chris Masond3977122009-01-05 21:25:51 -0500944 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400945 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400946 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400947 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400948 if (ret != -EEXIST) {
949 free_extent_map(em);
950 break;
951 }
952 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400953 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400954 }
Liu Boace68ba2013-04-22 10:53:47 +0000955 if (ret)
956 goto out_reserve;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400957
Chris Mason98d20f62008-04-14 09:46:10 -0400958 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400959 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -0500960 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +0000961 if (ret)
962 goto out_reserve;
Chris Masonc8b97812008-10-29 14:49:59 -0400963
Yan Zheng17d217f2008-12-12 10:03:38 -0500964 if (root->root_key.objectid ==
965 BTRFS_DATA_RELOC_TREE_OBJECTID) {
966 ret = btrfs_reloc_clone_csums(inode, start,
967 cur_alloc_size);
Josef Bacik00361582013-08-14 14:02:47 -0400968 if (ret)
Liu Boace68ba2013-04-22 10:53:47 +0000969 goto out_reserve;
Yan Zheng17d217f2008-12-12 10:03:38 -0500970 }
971
Chris Masond3977122009-01-05 21:25:51 -0500972 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -0400973 break;
Chris Masond3977122009-01-05 21:25:51 -0500974
Chris Masonc8b97812008-10-29 14:49:59 -0400975 /* we're not doing compressed IO, don't unlock the first
976 * page (which the caller expects to stay locked), don't
977 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -0400978 *
979 * Do set the Private2 bit so we know this page was properly
980 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -0400981 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400982 op = unlock ? PAGE_UNLOCK : 0;
983 op |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -0400984
Josef Bacikc2790a22013-07-29 11:20:47 -0400985 extent_clear_unlock_delalloc(inode, start,
986 start + ram_size - 1, locked_page,
987 EXTENT_LOCKED | EXTENT_DELALLOC,
988 op);
Chris Masonc8b97812008-10-29 14:49:59 -0400989 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -0500990 num_bytes -= cur_alloc_size;
991 alloc_hint = ins.objectid + ins.offset;
992 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400993 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100994out:
Chris Masonbe20aa92007-12-17 20:14:01 -0500995 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +0000996
Liu Boace68ba2013-04-22 10:53:47 +0000997out_reserve:
998 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100999out_unlock:
Josef Bacikc2790a22013-07-29 11:20:47 -04001000 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Josef Bacik151a41b2013-07-29 13:22:24 -04001001 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
1002 EXTENT_DELALLOC | EXTENT_DEFRAG,
1003 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
1004 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001005 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001006}
Chris Masonc8b97812008-10-29 14:49:59 -04001007
Chris Mason771ed682008-11-06 22:02:51 -05001008/*
1009 * work queue call back to started compression on a file and pages
1010 */
1011static noinline void async_cow_start(struct btrfs_work *work)
1012{
1013 struct async_cow *async_cow;
1014 int num_added = 0;
1015 async_cow = container_of(work, struct async_cow, work);
1016
1017 compress_file_range(async_cow->inode, async_cow->locked_page,
1018 async_cow->start, async_cow->end, async_cow,
1019 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001020 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001021 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001022 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001023 }
Chris Mason771ed682008-11-06 22:02:51 -05001024}
1025
1026/*
1027 * work queue call back to submit previously compressed pages
1028 */
1029static noinline void async_cow_submit(struct btrfs_work *work)
1030{
1031 struct async_cow *async_cow;
1032 struct btrfs_root *root;
1033 unsigned long nr_pages;
1034
1035 async_cow = container_of(work, struct async_cow, work);
1036
1037 root = async_cow->root;
1038 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
1039 PAGE_CACHE_SHIFT;
1040
Josef Bacik66657b32012-08-01 15:36:24 -04001041 if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) <
Liu Bo287082b2012-06-28 04:02:24 -06001042 5 * 1024 * 1024 &&
Chris Mason771ed682008-11-06 22:02:51 -05001043 waitqueue_active(&root->fs_info->async_submit_wait))
1044 wake_up(&root->fs_info->async_submit_wait);
1045
Chris Masond3977122009-01-05 21:25:51 -05001046 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001047 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001048}
Chris Masonc8b97812008-10-29 14:49:59 -04001049
Chris Mason771ed682008-11-06 22:02:51 -05001050static noinline void async_cow_free(struct btrfs_work *work)
1051{
1052 struct async_cow *async_cow;
1053 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001054 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001055 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001056 kfree(async_cow);
1057}
1058
1059static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1060 u64 start, u64 end, int *page_started,
1061 unsigned long *nr_written)
1062{
1063 struct async_cow *async_cow;
1064 struct btrfs_root *root = BTRFS_I(inode)->root;
1065 unsigned long nr_pages;
1066 u64 cur_end;
Liu Bo287082b2012-06-28 04:02:24 -06001067 int limit = 10 * 1024 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -05001068
Chris Masona3429ab2009-10-08 12:30:20 -04001069 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1070 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001071 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001072 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001073 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001074 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001075 async_cow->root = root;
1076 async_cow->locked_page = locked_page;
1077 async_cow->start = start;
1078
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001079 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
Chris Mason771ed682008-11-06 22:02:51 -05001080 cur_end = end;
1081 else
1082 cur_end = min(end, start + 512 * 1024 - 1);
1083
1084 async_cow->end = cur_end;
1085 INIT_LIST_HEAD(&async_cow->extents);
1086
Qu Wenruoafe3d242014-02-28 10:46:07 +08001087 btrfs_init_work(&async_cow->work, async_cow_start,
1088 async_cow_submit, async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001089
Chris Mason771ed682008-11-06 22:02:51 -05001090 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
1091 PAGE_CACHE_SHIFT;
1092 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
1093
Qu Wenruoafe3d242014-02-28 10:46:07 +08001094 btrfs_queue_work(root->fs_info->delalloc_workers,
1095 &async_cow->work);
Chris Mason771ed682008-11-06 22:02:51 -05001096
1097 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
1098 wait_event(root->fs_info->async_submit_wait,
1099 (atomic_read(&root->fs_info->async_delalloc_pages) <
1100 limit));
1101 }
1102
Chris Masond3977122009-01-05 21:25:51 -05001103 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -05001104 atomic_read(&root->fs_info->async_delalloc_pages)) {
1105 wait_event(root->fs_info->async_submit_wait,
1106 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1107 0));
1108 }
1109
1110 *nr_written += nr_pages;
1111 start = cur_end + 1;
1112 }
1113 *page_started = 1;
1114 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001115}
1116
Chris Masond3977122009-01-05 21:25:51 -05001117static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001118 u64 bytenr, u64 num_bytes)
1119{
1120 int ret;
1121 struct btrfs_ordered_sum *sums;
1122 LIST_HEAD(list);
1123
Yan Zheng07d400a2009-01-06 11:42:00 -05001124 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001125 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001126 if (ret == 0 && list_empty(&list))
1127 return 0;
1128
1129 while (!list_empty(&list)) {
1130 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1131 list_del(&sums->list);
1132 kfree(sums);
1133 }
1134 return 1;
1135}
1136
Chris Masond352ac62008-09-29 15:18:18 -04001137/*
1138 * when nowcow writeback call back. This checks for snapshots or COW copies
1139 * of the extents that exist in the file, and COWs the file as required.
1140 *
1141 * If no cow copies or snapshots exist, we write directly to the existing
1142 * blocks on disk
1143 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001144static noinline int run_delalloc_nocow(struct inode *inode,
1145 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001146 u64 start, u64 end, int *page_started, int force,
1147 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001148{
Chris Masonbe20aa92007-12-17 20:14:01 -05001149 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001150 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001151 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001152 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001153 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001154 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001155 u64 cow_start;
1156 u64 cur_offset;
1157 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001158 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001159 u64 disk_bytenr;
1160 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001161 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001162 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001163 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001164 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001165 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001166 int nocow;
1167 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001168 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001169 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001170
1171 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001172 if (!path) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001173 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1174 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001175 EXTENT_DO_ACCOUNTING |
1176 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001177 PAGE_CLEAR_DIRTY |
1178 PAGE_SET_WRITEBACK |
1179 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001180 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001181 }
Li Zefan82d59022011-04-20 10:33:24 +08001182
Liu Bo83eea1f2012-07-10 05:28:39 -06001183 nolock = btrfs_is_free_space_inode(inode);
Li Zefan82d59022011-04-20 10:33:24 +08001184
1185 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001186 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001187 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001188 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001189
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001190 if (IS_ERR(trans)) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001191 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1192 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001193 EXTENT_DO_ACCOUNTING |
1194 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001195 PAGE_CLEAR_DIRTY |
1196 PAGE_SET_WRITEBACK |
1197 PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001198 btrfs_free_path(path);
1199 return PTR_ERR(trans);
1200 }
1201
Josef Bacik74b21072011-04-13 12:02:53 -04001202 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001203
Yan Zheng80ff3852008-10-30 14:20:02 -04001204 cow_start = (u64)-1;
1205 cur_offset = start;
1206 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001207 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001208 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001209 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001210 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001211 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1212 leaf = path->nodes[0];
1213 btrfs_item_key_to_cpu(leaf, &found_key,
1214 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001215 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001216 found_key.type == BTRFS_EXTENT_DATA_KEY)
1217 path->slots[0]--;
1218 }
1219 check_prev = 0;
1220next_slot:
1221 leaf = path->nodes[0];
1222 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1223 ret = btrfs_next_leaf(root, path);
Josef Bacikd788a342013-10-25 16:55:08 -04001224 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001225 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001226 if (ret > 0)
1227 break;
1228 leaf = path->nodes[0];
1229 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001230
Yan Zheng80ff3852008-10-30 14:20:02 -04001231 nocow = 0;
1232 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001233 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001234 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001235
Li Zefan33345d012011-04-20 10:31:50 +08001236 if (found_key.objectid > ino ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001237 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1238 found_key.offset > end)
1239 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001240
Yan Zheng80ff3852008-10-30 14:20:02 -04001241 if (found_key.offset > cur_offset) {
1242 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001243 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001244 goto out_check;
1245 }
Chris Masonc31f8832008-01-08 15:46:31 -05001246
Yan Zheng80ff3852008-10-30 14:20:02 -04001247 fi = btrfs_item_ptr(leaf, path->slots[0],
1248 struct btrfs_file_extent_item);
1249 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001250
Josef Bacikcc95bef2013-04-04 14:31:27 -04001251 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001252 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1253 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001254 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001255 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001256 extent_end = found_key.offset +
1257 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001258 disk_num_bytes =
1259 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001260 if (extent_end <= start) {
1261 path->slots[0]++;
1262 goto next_slot;
1263 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001264 if (disk_bytenr == 0)
1265 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001266 if (btrfs_file_extent_compression(leaf, fi) ||
1267 btrfs_file_extent_encryption(leaf, fi) ||
1268 btrfs_file_extent_other_encoding(leaf, fi))
1269 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001270 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1271 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001272 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001273 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001274 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001275 found_key.offset -
1276 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001277 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001278 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001279 disk_bytenr += cur_offset - found_key.offset;
1280 num_bytes = min(end + 1, extent_end) - cur_offset;
1281 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001282 * if there are pending snapshots for this root,
1283 * we fall into common COW way.
1284 */
1285 if (!nolock) {
1286 err = btrfs_start_nocow_write(root);
1287 if (!err)
1288 goto out_check;
1289 }
1290 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001291 * force cow if csum exists in the range.
1292 * this ensure that csum for a given extent are
1293 * either valid or do not exist.
1294 */
1295 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1296 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001297 nocow = 1;
1298 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1299 extent_end = found_key.offset +
Chris Mason514ac8a2014-01-03 21:07:00 -08001300 btrfs_file_extent_inline_len(leaf,
1301 path->slots[0], fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001302 extent_end = ALIGN(extent_end, root->sectorsize);
1303 } else {
1304 BUG_ON(1);
1305 }
1306out_check:
1307 if (extent_end <= start) {
1308 path->slots[0]++;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001309 if (!nolock && nocow)
1310 btrfs_end_nocow_write(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001311 goto next_slot;
1312 }
1313 if (!nocow) {
1314 if (cow_start == (u64)-1)
1315 cow_start = cur_offset;
1316 cur_offset = extent_end;
1317 if (cur_offset > end)
1318 break;
1319 path->slots[0]++;
1320 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001321 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001322
David Sterbab3b4aa72011-04-21 01:20:15 +02001323 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001324 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001325 ret = cow_file_range(inode, locked_page,
1326 cow_start, found_key.offset - 1,
1327 page_started, nr_written, 1);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001328 if (ret) {
1329 if (!nolock && nocow)
1330 btrfs_end_nocow_write(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001331 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001332 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001333 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001334 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001335
Yan Zhengd899e052008-10-30 14:25:28 -04001336 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1337 struct extent_map *em;
1338 struct extent_map_tree *em_tree;
1339 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001340 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001341 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001342 em->start = cur_offset;
Josef Bacik70c8a912012-10-11 16:54:30 -04001343 em->orig_start = found_key.offset - extent_offset;
Yan Zhengd899e052008-10-30 14:25:28 -04001344 em->len = num_bytes;
1345 em->block_len = num_bytes;
1346 em->block_start = disk_bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05001347 em->orig_block_len = disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001348 em->ram_bytes = ram_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04001349 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001350 em->mod_start = em->start;
1351 em->mod_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04001352 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacikb11e2342012-12-03 10:58:15 -05001353 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001354 em->generation = -1;
Yan Zhengd899e052008-10-30 14:25:28 -04001355 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001356 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001357 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001358 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001359 if (ret != -EEXIST) {
1360 free_extent_map(em);
1361 break;
1362 }
1363 btrfs_drop_extent_cache(inode, em->start,
1364 em->start + em->len - 1, 0);
1365 }
1366 type = BTRFS_ORDERED_PREALLOC;
1367 } else {
1368 type = BTRFS_ORDERED_NOCOW;
1369 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001370
1371 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001372 num_bytes, num_bytes, type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001373 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001374
Yan, Zhengefa56462010-05-16 10:49:59 -04001375 if (root->root_key.objectid ==
1376 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1377 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1378 num_bytes);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001379 if (ret) {
1380 if (!nolock && nocow)
1381 btrfs_end_nocow_write(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001382 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001383 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001384 }
1385
Josef Bacikc2790a22013-07-29 11:20:47 -04001386 extent_clear_unlock_delalloc(inode, cur_offset,
1387 cur_offset + num_bytes - 1,
1388 locked_page, EXTENT_LOCKED |
1389 EXTENT_DELALLOC, PAGE_UNLOCK |
1390 PAGE_SET_PRIVATE2);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001391 if (!nolock && nocow)
1392 btrfs_end_nocow_write(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001393 cur_offset = extent_end;
1394 if (cur_offset > end)
1395 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001396 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001397 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001398
Josef Bacik17ca04a2012-05-31 15:58:55 -04001399 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001400 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001401 cur_offset = end;
1402 }
1403
Yan Zheng80ff3852008-10-30 14:20:02 -04001404 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001405 ret = cow_file_range(inode, locked_page, cow_start, end,
1406 page_started, nr_written, 1);
Josef Bacikd788a342013-10-25 16:55:08 -04001407 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001408 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001409 }
1410
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001411error:
Miao Xiea698d0752012-09-20 01:51:59 -06001412 err = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001413 if (!ret)
1414 ret = err;
1415
Josef Bacik17ca04a2012-05-31 15:58:55 -04001416 if (ret && cur_offset < end)
Josef Bacikc2790a22013-07-29 11:20:47 -04001417 extent_clear_unlock_delalloc(inode, cur_offset, end,
1418 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001419 EXTENT_DELALLOC | EXTENT_DEFRAG |
1420 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1421 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001422 PAGE_SET_WRITEBACK |
1423 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001424 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001425 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001426}
1427
Chris Masond352ac62008-09-29 15:18:18 -04001428/*
1429 * extent_io.c call back to do delayed allocation processing
1430 */
Chris Masonc8b97812008-10-29 14:49:59 -04001431static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001432 u64 start, u64 end, int *page_started,
1433 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001434{
Chris Masonbe20aa92007-12-17 20:14:01 -05001435 int ret;
Chris Mason7f366cf2009-03-12 20:12:45 -04001436 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona2135012008-06-25 16:01:30 -04001437
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001438 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) {
Chris Masonc8b97812008-10-29 14:49:59 -04001439 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001440 page_started, 1, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001441 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001442 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001443 page_started, 0, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001444 } else if (!btrfs_test_opt(root, COMPRESS) &&
1445 !(BTRFS_I(inode)->force_compress) &&
1446 !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS)) {
Chris Mason7f366cf2009-03-12 20:12:45 -04001447 ret = cow_file_range(inode, locked_page, start, end,
1448 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001449 } else {
1450 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1451 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001452 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001453 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001454 }
Chris Masonb888db22007-08-27 16:49:44 -04001455 return ret;
1456}
1457
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001458static void btrfs_split_extent_hook(struct inode *inode,
1459 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001460{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001461 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001462 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001463 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001464
Josef Bacik9e0baf62011-07-15 15:16:44 +00001465 spin_lock(&BTRFS_I(inode)->lock);
1466 BTRFS_I(inode)->outstanding_extents++;
1467 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001468}
1469
1470/*
1471 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1472 * extents so we can keep track of new extents that are just merged onto old
1473 * extents, such as when we are doing sequential writes, so we can properly
1474 * account for the metadata space we'll need.
1475 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001476static void btrfs_merge_extent_hook(struct inode *inode,
1477 struct extent_state *new,
1478 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001479{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001480 /* not delalloc, ignore it */
1481 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001482 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001483
Josef Bacik9e0baf62011-07-15 15:16:44 +00001484 spin_lock(&BTRFS_I(inode)->lock);
1485 BTRFS_I(inode)->outstanding_extents--;
1486 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001487}
1488
Miao Xieeb73c1b2013-05-15 07:48:22 +00001489static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1490 struct inode *inode)
1491{
1492 spin_lock(&root->delalloc_lock);
1493 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1494 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1495 &root->delalloc_inodes);
1496 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1497 &BTRFS_I(inode)->runtime_flags);
1498 root->nr_delalloc_inodes++;
1499 if (root->nr_delalloc_inodes == 1) {
1500 spin_lock(&root->fs_info->delalloc_root_lock);
1501 BUG_ON(!list_empty(&root->delalloc_root));
1502 list_add_tail(&root->delalloc_root,
1503 &root->fs_info->delalloc_roots);
1504 spin_unlock(&root->fs_info->delalloc_root_lock);
1505 }
1506 }
1507 spin_unlock(&root->delalloc_lock);
1508}
1509
1510static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1511 struct inode *inode)
1512{
1513 spin_lock(&root->delalloc_lock);
1514 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1515 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1516 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1517 &BTRFS_I(inode)->runtime_flags);
1518 root->nr_delalloc_inodes--;
1519 if (!root->nr_delalloc_inodes) {
1520 spin_lock(&root->fs_info->delalloc_root_lock);
1521 BUG_ON(list_empty(&root->delalloc_root));
1522 list_del_init(&root->delalloc_root);
1523 spin_unlock(&root->fs_info->delalloc_root_lock);
1524 }
1525 }
1526 spin_unlock(&root->delalloc_lock);
1527}
1528
Chris Masond352ac62008-09-29 15:18:18 -04001529/*
1530 * extent_io.c set_bit_hook, used to track delayed allocation
1531 * bytes in this file, and to maintain the list of inodes that
1532 * have pending delalloc work to be done.
1533 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001534static void btrfs_set_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001535 struct extent_state *state, unsigned long *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001536{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001537
Chris Mason75eff682008-12-15 15:54:40 -05001538 /*
1539 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001540 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001541 * bit, which is only set or cleared with irqs on
1542 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001543 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001544 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001545 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001546 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001547
Josef Bacik9e0baf62011-07-15 15:16:44 +00001548 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001549 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001550 } else {
1551 spin_lock(&BTRFS_I(inode)->lock);
1552 BTRFS_I(inode)->outstanding_extents++;
1553 spin_unlock(&BTRFS_I(inode)->lock);
1554 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001555
Miao Xie963d6782013-01-29 10:10:51 +00001556 __percpu_counter_add(&root->fs_info->delalloc_bytes, len,
1557 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001558 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001559 BTRFS_I(inode)->delalloc_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001560 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001561 &BTRFS_I(inode)->runtime_flags))
1562 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001563 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001564 }
Chris Mason291d6732008-01-29 15:55:23 -05001565}
1566
Chris Masond352ac62008-09-29 15:18:18 -04001567/*
1568 * extent_io.c clear_bit_hook, see set_bit_hook for why
1569 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001570static void btrfs_clear_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001571 struct extent_state *state,
1572 unsigned long *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001573{
Chris Mason75eff682008-12-15 15:54:40 -05001574 /*
1575 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001576 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001577 * bit, which is only set or cleared with irqs on
1578 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001579 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001580 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001581 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001582 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001583
Josef Bacik9e0baf62011-07-15 15:16:44 +00001584 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001585 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001586 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1587 spin_lock(&BTRFS_I(inode)->lock);
1588 BTRFS_I(inode)->outstanding_extents--;
1589 spin_unlock(&BTRFS_I(inode)->lock);
1590 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001591
Josef Bacikb6d08f02013-09-27 14:57:43 -04001592 /*
1593 * We don't reserve metadata space for space cache inodes so we
1594 * don't need to call dellalloc_release_metadata if there is an
1595 * error.
1596 */
1597 if (*bits & EXTENT_DO_ACCOUNTING &&
1598 root != root->fs_info->tree_root)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001599 btrfs_delalloc_release_metadata(inode, len);
1600
Josef Bacik0cb59c92010-07-02 12:14:14 -04001601 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Josef Bacik7ee9e442013-06-21 16:37:03 -04001602 && do_list && !(state->state & EXTENT_NORESERVE))
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001603 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001604
Miao Xie963d6782013-01-29 10:10:51 +00001605 __percpu_counter_add(&root->fs_info->delalloc_bytes, -len,
1606 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001607 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001608 BTRFS_I(inode)->delalloc_bytes -= len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001609 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001610 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001611 &BTRFS_I(inode)->runtime_flags))
1612 btrfs_del_delalloc_inode(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001613 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001614 }
Chris Mason291d6732008-01-29 15:55:23 -05001615}
1616
Chris Masond352ac62008-09-29 15:18:18 -04001617/*
1618 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1619 * we don't create bios that span stripes or chunks
1620 */
David Woodhouse64a16702009-07-15 23:29:37 +01001621int btrfs_merge_bio_hook(int rw, struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001622 size_t size, struct bio *bio,
1623 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001624{
1625 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001626 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001627 u64 length = 0;
1628 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001629 int ret;
1630
Chris Mason771ed682008-11-06 22:02:51 -05001631 if (bio_flags & EXTENT_BIO_COMPRESSED)
1632 return 0;
1633
Kent Overstreet4f024f32013-10-11 15:44:27 -07001634 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001635 map_length = length;
David Woodhouse64a16702009-07-15 23:29:37 +01001636 ret = btrfs_map_block(root->fs_info, rw, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001637 &map_length, NULL, 0);
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001638 /* Will always return 0 with map_multi == NULL */
Jeff Mahoney3444a972011-10-03 23:23:13 -04001639 BUG_ON(ret < 0);
Chris Masond3977122009-01-05 21:25:51 -05001640 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001641 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001642 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001643}
1644
Chris Masond352ac62008-09-29 15:18:18 -04001645/*
1646 * in order to insert checksums into the metadata in large chunks,
1647 * we wait until bio submission time. All the pages in the bio are
1648 * checksummed and sums are attached onto the ordered extent record.
1649 *
1650 * At IO completion time the cums attached on the ordered extent record
1651 * are inserted into the btree
1652 */
Chris Masond3977122009-01-05 21:25:51 -05001653static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1654 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001655 unsigned long bio_flags,
1656 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001657{
Chris Mason065631f2008-02-20 12:07:25 -05001658 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001659 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001660
Chris Masond20f7042008-12-08 16:58:54 -05001661 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001662 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001663 return 0;
1664}
Chris Masone0156402008-04-16 11:15:20 -04001665
Chris Mason4a69a412008-11-06 22:03:00 -05001666/*
1667 * in order to insert checksums into the metadata in large chunks,
1668 * we wait until bio submission time. All the pages in the bio are
1669 * checksummed and sums are attached onto the ordered extent record.
1670 *
1671 * At IO completion time the cums attached on the ordered extent record
1672 * are inserted into the btree
1673 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001674static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001675 int mirror_num, unsigned long bio_flags,
1676 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001677{
1678 struct btrfs_root *root = BTRFS_I(inode)->root;
Stefan Behrens61891922012-11-05 18:51:52 +01001679 int ret;
1680
1681 ret = btrfs_map_bio(root, rw, bio, mirror_num, 1);
1682 if (ret)
1683 bio_endio(bio, ret);
1684 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001685}
1686
Chris Masond352ac62008-09-29 15:18:18 -04001687/*
Chris Masoncad321a2008-12-17 14:51:42 -05001688 * extent_io.c submission hook. This does the right thing for csum calculation
1689 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001690 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001691static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001692 int mirror_num, unsigned long bio_flags,
1693 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001694{
1695 struct btrfs_root *root = BTRFS_I(inode)->root;
1696 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001697 int skip_sum;
Jeff Mahoney04173412011-10-03 23:23:12 -04001698 int metadata = 0;
Josef Bacikb812ce22012-11-16 13:56:32 -05001699 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001700
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001701 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001702
Liu Bo83eea1f2012-07-10 05:28:39 -06001703 if (btrfs_is_free_space_inode(inode))
Jeff Mahoney04173412011-10-03 23:23:12 -04001704 metadata = 2;
1705
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001706 if (!(rw & REQ_WRITE)) {
Josef Bacik5fd02042012-05-02 14:00:54 -04001707 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1708 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001709 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001710
Chris Masond20f7042008-12-08 16:58:54 -05001711 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001712 ret = btrfs_submit_compressed_read(inode, bio,
1713 mirror_num,
1714 bio_flags);
1715 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001716 } else if (!skip_sum) {
1717 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1718 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001719 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001720 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001721 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001722 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001723 /* csum items have already been cloned */
1724 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1725 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001726 /* we're doing a write, do the async checksumming */
Stefan Behrens61891922012-11-05 18:51:52 +01001727 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001728 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001729 bio_flags, bio_offset,
1730 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001731 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001732 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001733 } else if (!skip_sum) {
1734 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1735 if (ret)
1736 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001737 }
1738
Chris Mason0b86a832008-03-24 15:01:56 -04001739mapit:
Stefan Behrens61891922012-11-05 18:51:52 +01001740 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
1741
1742out:
1743 if (ret < 0)
1744 bio_endio(bio, ret);
1745 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001746}
Chris Mason6885f302008-02-20 16:11:05 -05001747
Chris Masond352ac62008-09-29 15:18:18 -04001748/*
1749 * given a list of ordered sums record them in the inode. This happens
1750 * at IO completion time based on sums calculated at bio submission time.
1751 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001752static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001753 struct inode *inode, u64 file_offset,
1754 struct list_head *list)
1755{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001756 struct btrfs_ordered_sum *sum;
1757
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001758 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00001759 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05001760 btrfs_csum_file_blocks(trans,
1761 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00001762 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001763 }
1764 return 0;
1765}
1766
Josef Bacik2ac55d42010-02-03 19:33:23 +00001767int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1768 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001769{
Julia Lawall6c1500f2012-11-03 20:30:18 +00001770 WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04001771 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001772 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001773}
1774
Chris Masond352ac62008-09-29 15:18:18 -04001775/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001776struct btrfs_writepage_fixup {
1777 struct page *page;
1778 struct btrfs_work work;
1779};
1780
Christoph Hellwigb2950862008-12-02 09:54:17 -05001781static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001782{
1783 struct btrfs_writepage_fixup *fixup;
1784 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001785 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001786 struct page *page;
1787 struct inode *inode;
1788 u64 page_start;
1789 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001790 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001791
1792 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1793 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001794again:
Chris Mason247e7432008-07-17 12:53:51 -04001795 lock_page(page);
1796 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1797 ClearPageChecked(page);
1798 goto out_page;
1799 }
1800
1801 inode = page->mapping->host;
1802 page_start = page_offset(page);
1803 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1804
Josef Bacik2ac55d42010-02-03 19:33:23 +00001805 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001806 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001807
1808 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001809 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001810 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001811
1812 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1813 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001814 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1815 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001816 unlock_page(page);
1817 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001818 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04001819 goto again;
1820 }
Chris Mason247e7432008-07-17 12:53:51 -04001821
Jeff Mahoney87826df2012-02-15 16:23:57 +01001822 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
1823 if (ret) {
1824 mapping_set_error(page->mapping, ret);
1825 end_extent_writepage(page, ret, page_start, page_end);
1826 ClearPageChecked(page);
1827 goto out;
1828 }
1829
Josef Bacik2ac55d42010-02-03 19:33:23 +00001830 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001831 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001832 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04001833out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001834 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1835 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001836out_page:
1837 unlock_page(page);
1838 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08001839 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04001840}
1841
1842/*
1843 * There are a few paths in the higher layers of the kernel that directly
1844 * set the page dirty bit without asking the filesystem if it is a
1845 * good idea. This causes problems because we want to make sure COW
1846 * properly happens and the data=ordered rules are followed.
1847 *
Chris Masonc8b97812008-10-29 14:49:59 -04001848 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001849 * hasn't been properly setup for IO. We kick off an async process
1850 * to fix it up. The async helper will wait for ordered extents, set
1851 * the delalloc bit and make it safe to write the page.
1852 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001853static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001854{
1855 struct inode *inode = page->mapping->host;
1856 struct btrfs_writepage_fixup *fixup;
1857 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001858
Chris Mason8b62b722009-09-02 16:53:46 -04001859 /* this page is properly in the ordered list */
1860 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001861 return 0;
1862
1863 if (PageChecked(page))
1864 return -EAGAIN;
1865
1866 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1867 if (!fixup)
1868 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001869
Chris Mason247e7432008-07-17 12:53:51 -04001870 SetPageChecked(page);
1871 page_cache_get(page);
Qu Wenruodc6e3202014-02-28 10:46:14 +08001872 btrfs_init_work(&fixup->work, btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04001873 fixup->page = page;
Qu Wenruodc6e3202014-02-28 10:46:14 +08001874 btrfs_queue_work(root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001875 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04001876}
1877
Yan Zhengd899e052008-10-30 14:25:28 -04001878static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1879 struct inode *inode, u64 file_pos,
1880 u64 disk_bytenr, u64 disk_num_bytes,
1881 u64 num_bytes, u64 ram_bytes,
1882 u8 compression, u8 encryption,
1883 u16 other_encoding, int extent_type)
1884{
1885 struct btrfs_root *root = BTRFS_I(inode)->root;
1886 struct btrfs_file_extent_item *fi;
1887 struct btrfs_path *path;
1888 struct extent_buffer *leaf;
1889 struct btrfs_key ins;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00001890 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04001891 int ret;
1892
1893 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07001894 if (!path)
1895 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04001896
Chris Masona1ed8352009-09-11 12:27:37 -04001897 /*
1898 * we may be replacing one extent in the tree with another.
1899 * The new extent is pinned in the extent map, and we don't want
1900 * to drop it from the cache until it is completely in the btree.
1901 *
1902 * So, tell btrfs_drop_extents to leave this extent in the cache.
1903 * the caller is expected to unpin it and allow it to be merged
1904 * with the others.
1905 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00001906 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
1907 file_pos + num_bytes, NULL, 0,
1908 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001909 if (ret)
1910 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001911
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00001912 if (!extent_inserted) {
1913 ins.objectid = btrfs_ino(inode);
1914 ins.offset = file_pos;
1915 ins.type = BTRFS_EXTENT_DATA_KEY;
1916
1917 path->leave_spinning = 1;
1918 ret = btrfs_insert_empty_item(trans, root, path, &ins,
1919 sizeof(*fi));
1920 if (ret)
1921 goto out;
1922 }
Yan Zhengd899e052008-10-30 14:25:28 -04001923 leaf = path->nodes[0];
1924 fi = btrfs_item_ptr(leaf, path->slots[0],
1925 struct btrfs_file_extent_item);
1926 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1927 btrfs_set_file_extent_type(leaf, fi, extent_type);
1928 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1929 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1930 btrfs_set_file_extent_offset(leaf, fi, 0);
1931 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1932 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1933 btrfs_set_file_extent_compression(leaf, fi, compression);
1934 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1935 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04001936
Yan Zhengd899e052008-10-30 14:25:28 -04001937 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04001938 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04001939
1940 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04001941
1942 ins.objectid = disk_bytenr;
1943 ins.offset = disk_num_bytes;
1944 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001945 ret = btrfs_alloc_reserved_file_extent(trans, root,
1946 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08001947 btrfs_ino(inode), file_pos, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001948out:
Yan Zhengd899e052008-10-30 14:25:28 -04001949 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04001950
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001951 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04001952}
1953
Liu Bo38c227d2013-01-29 03:18:40 +00001954/* snapshot-aware defrag */
1955struct sa_defrag_extent_backref {
1956 struct rb_node node;
1957 struct old_sa_defrag_extent *old;
1958 u64 root_id;
1959 u64 inum;
1960 u64 file_pos;
1961 u64 extent_offset;
1962 u64 num_bytes;
1963 u64 generation;
1964};
1965
1966struct old_sa_defrag_extent {
1967 struct list_head list;
1968 struct new_sa_defrag_extent *new;
1969
1970 u64 extent_offset;
1971 u64 bytenr;
1972 u64 offset;
1973 u64 len;
1974 int count;
1975};
1976
1977struct new_sa_defrag_extent {
1978 struct rb_root root;
1979 struct list_head head;
1980 struct btrfs_path *path;
1981 struct inode *inode;
1982 u64 file_pos;
1983 u64 len;
1984 u64 bytenr;
1985 u64 disk_len;
1986 u8 compress_type;
1987};
1988
1989static int backref_comp(struct sa_defrag_extent_backref *b1,
1990 struct sa_defrag_extent_backref *b2)
1991{
1992 if (b1->root_id < b2->root_id)
1993 return -1;
1994 else if (b1->root_id > b2->root_id)
1995 return 1;
1996
1997 if (b1->inum < b2->inum)
1998 return -1;
1999 else if (b1->inum > b2->inum)
2000 return 1;
2001
2002 if (b1->file_pos < b2->file_pos)
2003 return -1;
2004 else if (b1->file_pos > b2->file_pos)
2005 return 1;
2006
2007 /*
2008 * [------------------------------] ===> (a range of space)
2009 * |<--->| |<---->| =============> (fs/file tree A)
2010 * |<---------------------------->| ===> (fs/file tree B)
2011 *
2012 * A range of space can refer to two file extents in one tree while
2013 * refer to only one file extent in another tree.
2014 *
2015 * So we may process a disk offset more than one time(two extents in A)
2016 * and locate at the same extent(one extent in B), then insert two same
2017 * backrefs(both refer to the extent in B).
2018 */
2019 return 0;
2020}
2021
2022static void backref_insert(struct rb_root *root,
2023 struct sa_defrag_extent_backref *backref)
2024{
2025 struct rb_node **p = &root->rb_node;
2026 struct rb_node *parent = NULL;
2027 struct sa_defrag_extent_backref *entry;
2028 int ret;
2029
2030 while (*p) {
2031 parent = *p;
2032 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2033
2034 ret = backref_comp(backref, entry);
2035 if (ret < 0)
2036 p = &(*p)->rb_left;
2037 else
2038 p = &(*p)->rb_right;
2039 }
2040
2041 rb_link_node(&backref->node, parent, p);
2042 rb_insert_color(&backref->node, root);
2043}
2044
2045/*
2046 * Note the backref might has changed, and in this case we just return 0.
2047 */
2048static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2049 void *ctx)
2050{
2051 struct btrfs_file_extent_item *extent;
2052 struct btrfs_fs_info *fs_info;
2053 struct old_sa_defrag_extent *old = ctx;
2054 struct new_sa_defrag_extent *new = old->new;
2055 struct btrfs_path *path = new->path;
2056 struct btrfs_key key;
2057 struct btrfs_root *root;
2058 struct sa_defrag_extent_backref *backref;
2059 struct extent_buffer *leaf;
2060 struct inode *inode = new->inode;
2061 int slot;
2062 int ret;
2063 u64 extent_offset;
2064 u64 num_bytes;
2065
2066 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
2067 inum == btrfs_ino(inode))
2068 return 0;
2069
2070 key.objectid = root_id;
2071 key.type = BTRFS_ROOT_ITEM_KEY;
2072 key.offset = (u64)-1;
2073
2074 fs_info = BTRFS_I(inode)->root->fs_info;
2075 root = btrfs_read_fs_root_no_name(fs_info, &key);
2076 if (IS_ERR(root)) {
2077 if (PTR_ERR(root) == -ENOENT)
2078 return 0;
2079 WARN_ON(1);
2080 pr_debug("inum=%llu, offset=%llu, root_id=%llu\n",
2081 inum, offset, root_id);
2082 return PTR_ERR(root);
2083 }
2084
2085 key.objectid = inum;
2086 key.type = BTRFS_EXTENT_DATA_KEY;
2087 if (offset > (u64)-1 << 32)
2088 key.offset = 0;
2089 else
2090 key.offset = offset;
2091
2092 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302093 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002094 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002095 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002096
2097 while (1) {
2098 cond_resched();
2099
2100 leaf = path->nodes[0];
2101 slot = path->slots[0];
2102
2103 if (slot >= btrfs_header_nritems(leaf)) {
2104 ret = btrfs_next_leaf(root, path);
2105 if (ret < 0) {
2106 goto out;
2107 } else if (ret > 0) {
2108 ret = 0;
2109 goto out;
2110 }
2111 continue;
2112 }
2113
2114 path->slots[0]++;
2115
2116 btrfs_item_key_to_cpu(leaf, &key, slot);
2117
2118 if (key.objectid > inum)
2119 goto out;
2120
2121 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2122 continue;
2123
2124 extent = btrfs_item_ptr(leaf, slot,
2125 struct btrfs_file_extent_item);
2126
2127 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2128 continue;
2129
Liu Boe68afa42013-07-01 22:13:26 +08002130 /*
2131 * 'offset' refers to the exact key.offset,
2132 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2133 * (key.offset - extent_offset).
2134 */
2135 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002136 continue;
2137
Liu Boe68afa42013-07-01 22:13:26 +08002138 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002139 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002140
Liu Bo38c227d2013-01-29 03:18:40 +00002141 if (extent_offset >= old->extent_offset + old->offset +
2142 old->len || extent_offset + num_bytes <=
2143 old->extent_offset + old->offset)
2144 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002145 break;
2146 }
2147
2148 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2149 if (!backref) {
2150 ret = -ENOENT;
2151 goto out;
2152 }
2153
2154 backref->root_id = root_id;
2155 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002156 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002157 backref->num_bytes = num_bytes;
2158 backref->extent_offset = extent_offset;
2159 backref->generation = btrfs_file_extent_generation(leaf, extent);
2160 backref->old = old;
2161 backref_insert(&new->root, backref);
2162 old->count++;
2163out:
2164 btrfs_release_path(path);
2165 WARN_ON(ret);
2166 return ret;
2167}
2168
2169static noinline bool record_extent_backrefs(struct btrfs_path *path,
2170 struct new_sa_defrag_extent *new)
2171{
2172 struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info;
2173 struct old_sa_defrag_extent *old, *tmp;
2174 int ret;
2175
2176 new->path = path;
2177
2178 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002179 ret = iterate_inodes_from_logical(old->bytenr +
2180 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002181 path, record_one_backref,
2182 old);
Josef Bacik4724b102013-11-05 11:11:40 -05002183 if (ret < 0 && ret != -ENOENT)
2184 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002185
2186 /* no backref to be processed for this extent */
2187 if (!old->count) {
2188 list_del(&old->list);
2189 kfree(old);
2190 }
2191 }
2192
2193 if (list_empty(&new->head))
2194 return false;
2195
2196 return true;
2197}
2198
2199static int relink_is_mergable(struct extent_buffer *leaf,
2200 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002201 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002202{
Liu Bo116e0022013-08-02 16:30:40 +08002203 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002204 return 0;
2205
2206 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2207 return 0;
2208
Liu Bo116e0022013-08-02 16:30:40 +08002209 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2210 return 0;
2211
2212 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002213 btrfs_file_extent_other_encoding(leaf, fi))
2214 return 0;
2215
2216 return 1;
2217}
2218
2219/*
2220 * Note the backref might has changed, and in this case we just return 0.
2221 */
2222static noinline int relink_extent_backref(struct btrfs_path *path,
2223 struct sa_defrag_extent_backref *prev,
2224 struct sa_defrag_extent_backref *backref)
2225{
2226 struct btrfs_file_extent_item *extent;
2227 struct btrfs_file_extent_item *item;
2228 struct btrfs_ordered_extent *ordered;
2229 struct btrfs_trans_handle *trans;
2230 struct btrfs_fs_info *fs_info;
2231 struct btrfs_root *root;
2232 struct btrfs_key key;
2233 struct extent_buffer *leaf;
2234 struct old_sa_defrag_extent *old = backref->old;
2235 struct new_sa_defrag_extent *new = old->new;
2236 struct inode *src_inode = new->inode;
2237 struct inode *inode;
2238 struct extent_state *cached = NULL;
2239 int ret = 0;
2240 u64 start;
2241 u64 len;
2242 u64 lock_start;
2243 u64 lock_end;
2244 bool merge = false;
2245 int index;
2246
2247 if (prev && prev->root_id == backref->root_id &&
2248 prev->inum == backref->inum &&
2249 prev->file_pos + prev->num_bytes == backref->file_pos)
2250 merge = true;
2251
2252 /* step 1: get root */
2253 key.objectid = backref->root_id;
2254 key.type = BTRFS_ROOT_ITEM_KEY;
2255 key.offset = (u64)-1;
2256
2257 fs_info = BTRFS_I(src_inode)->root->fs_info;
2258 index = srcu_read_lock(&fs_info->subvol_srcu);
2259
2260 root = btrfs_read_fs_root_no_name(fs_info, &key);
2261 if (IS_ERR(root)) {
2262 srcu_read_unlock(&fs_info->subvol_srcu, index);
2263 if (PTR_ERR(root) == -ENOENT)
2264 return 0;
2265 return PTR_ERR(root);
2266 }
Liu Bo38c227d2013-01-29 03:18:40 +00002267
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002268 if (btrfs_root_readonly(root)) {
2269 srcu_read_unlock(&fs_info->subvol_srcu, index);
2270 return 0;
2271 }
2272
Liu Bo38c227d2013-01-29 03:18:40 +00002273 /* step 2: get inode */
2274 key.objectid = backref->inum;
2275 key.type = BTRFS_INODE_ITEM_KEY;
2276 key.offset = 0;
2277
2278 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2279 if (IS_ERR(inode)) {
2280 srcu_read_unlock(&fs_info->subvol_srcu, index);
2281 return 0;
2282 }
2283
2284 srcu_read_unlock(&fs_info->subvol_srcu, index);
2285
2286 /* step 3: relink backref */
2287 lock_start = backref->file_pos;
2288 lock_end = backref->file_pos + backref->num_bytes - 1;
2289 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2290 0, &cached);
2291
2292 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2293 if (ordered) {
2294 btrfs_put_ordered_extent(ordered);
2295 goto out_unlock;
2296 }
2297
2298 trans = btrfs_join_transaction(root);
2299 if (IS_ERR(trans)) {
2300 ret = PTR_ERR(trans);
2301 goto out_unlock;
2302 }
2303
2304 key.objectid = backref->inum;
2305 key.type = BTRFS_EXTENT_DATA_KEY;
2306 key.offset = backref->file_pos;
2307
2308 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2309 if (ret < 0) {
2310 goto out_free_path;
2311 } else if (ret > 0) {
2312 ret = 0;
2313 goto out_free_path;
2314 }
2315
2316 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2317 struct btrfs_file_extent_item);
2318
2319 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2320 backref->generation)
2321 goto out_free_path;
2322
2323 btrfs_release_path(path);
2324
2325 start = backref->file_pos;
2326 if (backref->extent_offset < old->extent_offset + old->offset)
2327 start += old->extent_offset + old->offset -
2328 backref->extent_offset;
2329
2330 len = min(backref->extent_offset + backref->num_bytes,
2331 old->extent_offset + old->offset + old->len);
2332 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2333
2334 ret = btrfs_drop_extents(trans, root, inode, start,
2335 start + len, 1);
2336 if (ret)
2337 goto out_free_path;
2338again:
2339 key.objectid = btrfs_ino(inode);
2340 key.type = BTRFS_EXTENT_DATA_KEY;
2341 key.offset = start;
2342
Liu Boa09a0a72013-03-11 09:20:58 +00002343 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002344 if (merge) {
2345 struct btrfs_file_extent_item *fi;
2346 u64 extent_len;
2347 struct btrfs_key found_key;
2348
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002349 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002350 if (ret < 0)
2351 goto out_free_path;
2352
2353 path->slots[0]--;
2354 leaf = path->nodes[0];
2355 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2356
2357 fi = btrfs_item_ptr(leaf, path->slots[0],
2358 struct btrfs_file_extent_item);
2359 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2360
Liu Bo116e0022013-08-02 16:30:40 +08002361 if (extent_len + found_key.offset == start &&
2362 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002363 btrfs_set_file_extent_num_bytes(leaf, fi,
2364 extent_len + len);
2365 btrfs_mark_buffer_dirty(leaf);
2366 inode_add_bytes(inode, len);
2367
2368 ret = 1;
2369 goto out_free_path;
2370 } else {
2371 merge = false;
2372 btrfs_release_path(path);
2373 goto again;
2374 }
2375 }
2376
2377 ret = btrfs_insert_empty_item(trans, root, path, &key,
2378 sizeof(*extent));
2379 if (ret) {
2380 btrfs_abort_transaction(trans, root, ret);
2381 goto out_free_path;
2382 }
2383
2384 leaf = path->nodes[0];
2385 item = btrfs_item_ptr(leaf, path->slots[0],
2386 struct btrfs_file_extent_item);
2387 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2388 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2389 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2390 btrfs_set_file_extent_num_bytes(leaf, item, len);
2391 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2392 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2393 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2394 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2395 btrfs_set_file_extent_encryption(leaf, item, 0);
2396 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2397
2398 btrfs_mark_buffer_dirty(leaf);
2399 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002400 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002401
2402 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
2403 new->disk_len, 0,
2404 backref->root_id, backref->inum,
2405 new->file_pos, 0); /* start - extent_offset */
2406 if (ret) {
2407 btrfs_abort_transaction(trans, root, ret);
2408 goto out_free_path;
2409 }
2410
2411 ret = 1;
2412out_free_path:
2413 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002414 path->leave_spinning = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002415 btrfs_end_transaction(trans, root);
2416out_unlock:
2417 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2418 &cached, GFP_NOFS);
2419 iput(inode);
2420 return ret;
2421}
2422
Liu Bo6f519562013-10-29 10:45:05 +08002423static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2424{
2425 struct old_sa_defrag_extent *old, *tmp;
2426
2427 if (!new)
2428 return;
2429
2430 list_for_each_entry_safe(old, tmp, &new->head, list) {
2431 list_del(&old->list);
2432 kfree(old);
2433 }
2434 kfree(new);
2435}
2436
Liu Bo38c227d2013-01-29 03:18:40 +00002437static void relink_file_extents(struct new_sa_defrag_extent *new)
2438{
2439 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002440 struct sa_defrag_extent_backref *backref;
2441 struct sa_defrag_extent_backref *prev = NULL;
2442 struct inode *inode;
2443 struct btrfs_root *root;
2444 struct rb_node *node;
2445 int ret;
2446
2447 inode = new->inode;
2448 root = BTRFS_I(inode)->root;
2449
2450 path = btrfs_alloc_path();
2451 if (!path)
2452 return;
2453
2454 if (!record_extent_backrefs(path, new)) {
2455 btrfs_free_path(path);
2456 goto out;
2457 }
2458 btrfs_release_path(path);
2459
2460 while (1) {
2461 node = rb_first(&new->root);
2462 if (!node)
2463 break;
2464 rb_erase(node, &new->root);
2465
2466 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2467
2468 ret = relink_extent_backref(path, prev, backref);
2469 WARN_ON(ret < 0);
2470
2471 kfree(prev);
2472
2473 if (ret == 1)
2474 prev = backref;
2475 else
2476 prev = NULL;
2477 cond_resched();
2478 }
2479 kfree(prev);
2480
2481 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002482out:
Liu Bo6f519562013-10-29 10:45:05 +08002483 free_sa_defrag_extent(new);
2484
Liu Bo38c227d2013-01-29 03:18:40 +00002485 atomic_dec(&root->fs_info->defrag_running);
2486 wake_up(&root->fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002487}
2488
2489static struct new_sa_defrag_extent *
2490record_old_file_extents(struct inode *inode,
2491 struct btrfs_ordered_extent *ordered)
2492{
2493 struct btrfs_root *root = BTRFS_I(inode)->root;
2494 struct btrfs_path *path;
2495 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002496 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002497 struct new_sa_defrag_extent *new;
2498 int ret;
2499
2500 new = kmalloc(sizeof(*new), GFP_NOFS);
2501 if (!new)
2502 return NULL;
2503
2504 new->inode = inode;
2505 new->file_pos = ordered->file_offset;
2506 new->len = ordered->len;
2507 new->bytenr = ordered->start;
2508 new->disk_len = ordered->disk_len;
2509 new->compress_type = ordered->compress_type;
2510 new->root = RB_ROOT;
2511 INIT_LIST_HEAD(&new->head);
2512
2513 path = btrfs_alloc_path();
2514 if (!path)
2515 goto out_kfree;
2516
2517 key.objectid = btrfs_ino(inode);
2518 key.type = BTRFS_EXTENT_DATA_KEY;
2519 key.offset = new->file_pos;
2520
2521 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2522 if (ret < 0)
2523 goto out_free_path;
2524 if (ret > 0 && path->slots[0] > 0)
2525 path->slots[0]--;
2526
2527 /* find out all the old extents for the file range */
2528 while (1) {
2529 struct btrfs_file_extent_item *extent;
2530 struct extent_buffer *l;
2531 int slot;
2532 u64 num_bytes;
2533 u64 offset;
2534 u64 end;
2535 u64 disk_bytenr;
2536 u64 extent_offset;
2537
2538 l = path->nodes[0];
2539 slot = path->slots[0];
2540
2541 if (slot >= btrfs_header_nritems(l)) {
2542 ret = btrfs_next_leaf(root, path);
2543 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002544 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002545 else if (ret > 0)
2546 break;
2547 continue;
2548 }
2549
2550 btrfs_item_key_to_cpu(l, &key, slot);
2551
2552 if (key.objectid != btrfs_ino(inode))
2553 break;
2554 if (key.type != BTRFS_EXTENT_DATA_KEY)
2555 break;
2556 if (key.offset >= new->file_pos + new->len)
2557 break;
2558
2559 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2560
2561 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2562 if (key.offset + num_bytes < new->file_pos)
2563 goto next;
2564
2565 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2566 if (!disk_bytenr)
2567 goto next;
2568
2569 extent_offset = btrfs_file_extent_offset(l, extent);
2570
2571 old = kmalloc(sizeof(*old), GFP_NOFS);
2572 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002573 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002574
2575 offset = max(new->file_pos, key.offset);
2576 end = min(new->file_pos + new->len, key.offset + num_bytes);
2577
2578 old->bytenr = disk_bytenr;
2579 old->extent_offset = extent_offset;
2580 old->offset = offset - key.offset;
2581 old->len = end - offset;
2582 old->new = new;
2583 old->count = 0;
2584 list_add_tail(&old->list, &new->head);
2585next:
2586 path->slots[0]++;
2587 cond_resched();
2588 }
2589
2590 btrfs_free_path(path);
2591 atomic_inc(&root->fs_info->defrag_running);
2592
2593 return new;
2594
Liu Bo38c227d2013-01-29 03:18:40 +00002595out_free_path:
2596 btrfs_free_path(path);
2597out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002598 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002599 return NULL;
2600}
2601
Chris Masond352ac62008-09-29 15:18:18 -04002602/* as ordered data IO finishes, this gets called so we can finish
2603 * an ordered extent if the range of bytes in the file it covers are
2604 * fully written.
2605 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002606static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002607{
Josef Bacik5fd02042012-05-02 14:00:54 -04002608 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002609 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002610 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002611 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002612 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002613 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002614 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002615 int ret = 0;
2616 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002617 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002618 bool truncated = false;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002619
Liu Bo83eea1f2012-07-10 05:28:39 -06002620 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002621
Josef Bacik5fd02042012-05-02 14:00:54 -04002622 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2623 ret = -EIO;
2624 goto out;
2625 }
2626
Josef Bacik77cef2e2013-08-29 13:57:21 -04002627 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2628 truncated = true;
2629 logical_len = ordered_extent->truncated_len;
2630 /* Truncated the entire extent, don't bother adding */
2631 if (!logical_len)
2632 goto out;
2633 }
2634
Yan, Zhengc2167752009-11-12 09:34:21 +00002635 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002636 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Josef Bacik6c760c02012-11-09 10:53:21 -05002637 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2638 if (nolock)
2639 trans = btrfs_join_transaction_nolock(root);
2640 else
2641 trans = btrfs_join_transaction(root);
2642 if (IS_ERR(trans)) {
2643 ret = PTR_ERR(trans);
2644 trans = NULL;
2645 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002646 }
Josef Bacik6c760c02012-11-09 10:53:21 -05002647 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
2648 ret = btrfs_update_inode_fallback(trans, root, inode);
2649 if (ret) /* -ENOMEM or corruption */
2650 btrfs_abort_transaction(trans, root, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002651 goto out;
2652 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002653
Josef Bacik2ac55d42010-02-03 19:33:23 +00002654 lock_extent_bits(io_tree, ordered_extent->file_offset,
2655 ordered_extent->file_offset + ordered_extent->len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002656 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002657
Liu Bo38c227d2013-01-29 03:18:40 +00002658 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2659 ordered_extent->file_offset + ordered_extent->len - 1,
2660 EXTENT_DEFRAG, 1, cached_state);
2661 if (ret) {
2662 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05002663 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00002664 /* the inode is shared */
2665 new = record_old_file_extents(inode, ordered_extent);
2666
2667 clear_extent_bit(io_tree, ordered_extent->file_offset,
2668 ordered_extent->file_offset + ordered_extent->len - 1,
2669 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2670 }
2671
Josef Bacik0cb59c92010-07-02 12:14:14 -04002672 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002673 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002674 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002675 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002676 if (IS_ERR(trans)) {
2677 ret = PTR_ERR(trans);
2678 trans = NULL;
2679 goto out_unlock;
2680 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002681 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002682
Chris Masonc8b97812008-10-29 14:49:59 -04002683 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002684 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002685 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002686 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00002687 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002688 ordered_extent->file_offset,
2689 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002690 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002691 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04002692 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002693 ret = insert_reserved_file_extent(trans, inode,
2694 ordered_extent->file_offset,
2695 ordered_extent->start,
2696 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04002697 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08002698 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002699 BTRFS_FILE_EXTENT_REG);
Yan Zhengd899e052008-10-30 14:25:28 -04002700 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002701 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2702 ordered_extent->file_offset, ordered_extent->len,
2703 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002704 if (ret < 0) {
2705 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002706 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002707 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002708
Chris Masone6dcd2d2008-07-17 12:53:50 -04002709 add_pending_csums(trans, inode, ordered_extent->file_offset,
2710 &ordered_extent->list);
2711
Josef Bacik6c760c02012-11-09 10:53:21 -05002712 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2713 ret = btrfs_update_inode_fallback(trans, root, inode);
2714 if (ret) { /* -ENOMEM or corruption */
2715 btrfs_abort_transaction(trans, root, ret);
2716 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002717 }
2718 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002719out_unlock:
2720 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2721 ordered_extent->file_offset +
2722 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00002723out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002724 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04002725 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002726 if (trans)
2727 btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002728
Josef Bacik77cef2e2013-08-29 13:57:21 -04002729 if (ret || truncated) {
2730 u64 start, end;
2731
2732 if (truncated)
2733 start = ordered_extent->file_offset + logical_len;
2734 else
2735 start = ordered_extent->file_offset;
2736 end = ordered_extent->file_offset + ordered_extent->len - 1;
2737 clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS);
2738
2739 /* Drop the cache for the part of the extent we didn't write. */
2740 btrfs_drop_extent_cache(inode, start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04002741
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002742 /*
2743 * If the ordered extent had an IOERR or something else went
2744 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04002745 * back to the allocator. We only free the extent in the
2746 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002747 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04002748 if ((ret || !logical_len) &&
2749 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002750 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
2751 btrfs_free_reserved_extent(root, ordered_extent->start,
2752 ordered_extent->disk_len);
2753 }
2754
2755
Josef Bacik5fd02042012-05-02 14:00:54 -04002756 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002757 * This needs to be done to make sure anybody waiting knows we are done
2758 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002759 */
2760 btrfs_remove_ordered_extent(inode, ordered_extent);
2761
Liu Bo38c227d2013-01-29 03:18:40 +00002762 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08002763 if (new) {
2764 if (ret) {
2765 free_sa_defrag_extent(new);
2766 atomic_dec(&root->fs_info->defrag_running);
2767 } else {
2768 relink_file_extents(new);
2769 }
2770 }
Liu Bo38c227d2013-01-29 03:18:40 +00002771
Chris Masone6dcd2d2008-07-17 12:53:50 -04002772 /* once for us */
2773 btrfs_put_ordered_extent(ordered_extent);
2774 /* once for the tree */
2775 btrfs_put_ordered_extent(ordered_extent);
2776
Josef Bacik5fd02042012-05-02 14:00:54 -04002777 return ret;
2778}
2779
2780static void finish_ordered_fn(struct btrfs_work *work)
2781{
2782 struct btrfs_ordered_extent *ordered_extent;
2783 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2784 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002785}
2786
Christoph Hellwigb2950862008-12-02 09:54:17 -05002787static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04002788 struct extent_state *state, int uptodate)
2789{
Josef Bacik5fd02042012-05-02 14:00:54 -04002790 struct inode *inode = page->mapping->host;
2791 struct btrfs_root *root = BTRFS_I(inode)->root;
2792 struct btrfs_ordered_extent *ordered_extent = NULL;
Qu Wenruod458b052014-02-28 10:46:19 +08002793 struct btrfs_workqueue *workers;
Josef Bacik5fd02042012-05-02 14:00:54 -04002794
liubo1abe9b82011-03-24 11:18:59 +00002795 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2796
Chris Mason8b62b722009-09-02 16:53:46 -04002797 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002798 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2799 end - start + 1, uptodate))
2800 return 0;
2801
Qu Wenruofccb5d82014-02-28 10:46:10 +08002802 btrfs_init_work(&ordered_extent->work, finish_ordered_fn, NULL, NULL);
Josef Bacik5fd02042012-05-02 14:00:54 -04002803
Liu Bo83eea1f2012-07-10 05:28:39 -06002804 if (btrfs_is_free_space_inode(inode))
Qu Wenruofccb5d82014-02-28 10:46:10 +08002805 workers = root->fs_info->endio_freespace_worker;
Josef Bacik5fd02042012-05-02 14:00:54 -04002806 else
Qu Wenruofccb5d82014-02-28 10:46:10 +08002807 workers = root->fs_info->endio_write_workers;
2808 btrfs_queue_work(workers, &ordered_extent->work);
Josef Bacik5fd02042012-05-02 14:00:54 -04002809
2810 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04002811}
2812
Chris Masond352ac62008-09-29 15:18:18 -04002813/*
Chris Masond352ac62008-09-29 15:18:18 -04002814 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002815 * if there's a match, we allow the bio to finish. If not, the code in
2816 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002817 */
Miao Xiefacc8a22013-07-25 19:22:34 +08002818static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
2819 u64 phy_offset, struct page *page,
2820 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002821{
Miao Xie4eee4fa2012-12-21 09:17:45 +00002822 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04002823 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002824 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04002825 char *kaddr;
Chris Masonff79f812007-10-15 16:22:25 -04002826 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiefacc8a22013-07-25 19:22:34 +08002827 u32 csum_expected;
Chris Masonff79f812007-10-15 16:22:25 -04002828 u32 csum = ~(u32)0;
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002829 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
2830 DEFAULT_RATELIMIT_BURST);
Chris Masond1310b22008-01-24 16:13:08 -05002831
Chris Masond20f7042008-12-08 16:58:54 -05002832 if (PageChecked(page)) {
2833 ClearPageChecked(page);
2834 goto good;
2835 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002836
2837 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Jan Schmidt08d2f342011-05-04 16:18:50 +02002838 goto good;
Chris Masond20f7042008-12-08 16:58:54 -05002839
Yan Zheng17d217f2008-12-12 10:03:38 -05002840 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002841 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002842 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
2843 GFP_NOFS);
2844 return 0;
2845 }
2846
Miao Xiefacc8a22013-07-25 19:22:34 +08002847 phy_offset >>= inode->i_sb->s_blocksize_bits;
2848 csum_expected = *(((u32 *)io_bio->csum) + phy_offset);
Chris Masond3977122009-01-05 21:25:51 -05002849
Miao Xiefacc8a22013-07-25 19:22:34 +08002850 kaddr = kmap_atomic(page);
Liu Bob0496682013-03-14 14:57:45 +00002851 csum = btrfs_csum_data(kaddr + offset, csum, end - start + 1);
Chris Masonff79f812007-10-15 16:22:25 -04002852 btrfs_csum_final(csum, (char *)&csum);
Miao Xiefacc8a22013-07-25 19:22:34 +08002853 if (csum != csum_expected)
Chris Mason07157aa2007-08-30 08:50:51 -04002854 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002855
Cong Wang7ac687d2011-11-25 23:14:28 +08002856 kunmap_atomic(kaddr);
Chris Masond20f7042008-12-08 16:58:54 -05002857good:
Chris Mason07157aa2007-08-30 08:50:51 -04002858 return 0;
2859
2860zeroit:
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002861 if (__ratelimit(&_rs))
Miao Xiefacc8a22013-07-25 19:22:34 +08002862 btrfs_info(root->fs_info, "csum failed ino %llu off %llu csum %u expected csum %u",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002863 btrfs_ino(page->mapping->host), start, csum, csum_expected);
Chris Masondb945352007-10-15 16:15:53 -04002864 memset(kaddr + offset, 1, end - start + 1);
2865 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002866 kunmap_atomic(kaddr);
Miao Xiefacc8a22013-07-25 19:22:34 +08002867 if (csum_expected == 0)
Chris Mason3b951512008-04-17 11:29:12 -04002868 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04002869 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04002870}
Chris Masonb888db22007-08-27 16:49:44 -04002871
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002872struct delayed_iput {
2873 struct list_head list;
2874 struct inode *inode;
2875};
2876
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002877/* JDM: If this is fs-wide, why can't we add a pointer to
2878 * btrfs_inode instead and avoid the allocation? */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002879void btrfs_add_delayed_iput(struct inode *inode)
2880{
2881 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2882 struct delayed_iput *delayed;
2883
2884 if (atomic_add_unless(&inode->i_count, -1, 1))
2885 return;
2886
2887 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2888 delayed->inode = inode;
2889
2890 spin_lock(&fs_info->delayed_iput_lock);
2891 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2892 spin_unlock(&fs_info->delayed_iput_lock);
2893}
2894
2895void btrfs_run_delayed_iputs(struct btrfs_root *root)
2896{
2897 LIST_HEAD(list);
2898 struct btrfs_fs_info *fs_info = root->fs_info;
2899 struct delayed_iput *delayed;
2900 int empty;
2901
2902 spin_lock(&fs_info->delayed_iput_lock);
2903 empty = list_empty(&fs_info->delayed_iputs);
2904 spin_unlock(&fs_info->delayed_iput_lock);
2905 if (empty)
2906 return;
2907
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002908 spin_lock(&fs_info->delayed_iput_lock);
2909 list_splice_init(&fs_info->delayed_iputs, &list);
2910 spin_unlock(&fs_info->delayed_iput_lock);
2911
2912 while (!list_empty(&list)) {
2913 delayed = list_entry(list.next, struct delayed_iput, list);
2914 list_del(&delayed->list);
2915 iput(delayed->inode);
2916 kfree(delayed);
2917 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002918}
2919
Yan, Zhengd68fc572010-05-16 10:49:58 -04002920/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08002921 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04002922 * files in the subvolume, it removes orphan item and frees block_rsv
2923 * structure.
2924 */
2925void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
2926 struct btrfs_root *root)
2927{
Josef Bacik90290e12011-12-02 15:44:12 -05002928 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002929 int ret;
2930
Josef Bacik8a35d952012-05-23 14:26:42 -04002931 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04002932 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
2933 return;
2934
Josef Bacik90290e12011-12-02 15:44:12 -05002935 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04002936 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05002937 spin_unlock(&root->orphan_lock);
2938 return;
2939 }
2940
2941 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
2942 spin_unlock(&root->orphan_lock);
2943 return;
2944 }
2945
2946 block_rsv = root->orphan_block_rsv;
2947 root->orphan_block_rsv = NULL;
2948 spin_unlock(&root->orphan_lock);
2949
Yan, Zhengd68fc572010-05-16 10:49:58 -04002950 if (root->orphan_item_inserted &&
2951 btrfs_root_refs(&root->root_item) > 0) {
2952 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
2953 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04002954 if (ret)
2955 btrfs_abort_transaction(trans, root, ret);
2956 else
2957 root->orphan_item_inserted = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002958 }
2959
Josef Bacik90290e12011-12-02 15:44:12 -05002960 if (block_rsv) {
2961 WARN_ON(block_rsv->size > 0);
2962 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002963 }
2964}
2965
2966/*
Josef Bacik7b128762008-07-24 12:17:14 -04002967 * This creates an orphan entry for the given inode in case something goes
2968 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04002969 *
2970 * NOTE: caller of this function should reserve 5 units of metadata for
2971 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04002972 */
2973int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2974{
2975 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002976 struct btrfs_block_rsv *block_rsv = NULL;
2977 int reserve = 0;
2978 int insert = 0;
2979 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002980
Yan, Zhengd68fc572010-05-16 10:49:58 -04002981 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06002982 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00002983 if (!block_rsv)
2984 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04002985 }
2986
Yan, Zhengd68fc572010-05-16 10:49:58 -04002987 spin_lock(&root->orphan_lock);
2988 if (!root->orphan_block_rsv) {
2989 root->orphan_block_rsv = block_rsv;
2990 } else if (block_rsv) {
2991 btrfs_free_block_rsv(root, block_rsv);
2992 block_rsv = NULL;
2993 }
Josef Bacik7b128762008-07-24 12:17:14 -04002994
Josef Bacik8a35d952012-05-23 14:26:42 -04002995 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2996 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04002997#if 0
2998 /*
2999 * For proper ENOSPC handling, we should do orphan
3000 * cleanup when mounting. But this introduces backward
3001 * compatibility issue.
3002 */
3003 if (!xchg(&root->orphan_item_inserted, 1))
3004 insert = 2;
3005 else
3006 insert = 1;
3007#endif
3008 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003009 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003010 }
Josef Bacik7b128762008-07-24 12:17:14 -04003011
Josef Bacik72ac3c02012-05-23 14:13:11 -04003012 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3013 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003014 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003015 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003016
Yan, Zhengd68fc572010-05-16 10:49:58 -04003017 /* grab metadata reservation from transaction handle */
3018 if (reserve) {
3019 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003020 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04003021 }
3022
3023 /* insert an orphan item to track this unlinked/truncated file */
3024 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08003025 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003026 if (ret) {
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003027 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003028 if (reserve) {
3029 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3030 &BTRFS_I(inode)->runtime_flags);
3031 btrfs_orphan_release_metadata(inode);
3032 }
3033 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003034 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3035 &BTRFS_I(inode)->runtime_flags);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003036 btrfs_abort_transaction(trans, root, ret);
3037 return ret;
3038 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003039 }
3040 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003041 }
3042
3043 /* insert an orphan item to track subvolume contains orphan files */
3044 if (insert >= 2) {
3045 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
3046 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003047 if (ret && ret != -EEXIST) {
3048 btrfs_abort_transaction(trans, root, ret);
3049 return ret;
3050 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003051 }
3052 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003053}
3054
3055/*
3056 * We have done the truncate/delete so we can go ahead and remove the orphan
3057 * item for this particular inode.
3058 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003059static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3060 struct inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003061{
3062 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003063 int delete_item = 0;
3064 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003065 int ret = 0;
3066
Yan, Zhengd68fc572010-05-16 10:49:58 -04003067 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003068 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3069 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003070 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003071
Josef Bacik72ac3c02012-05-23 14:13:11 -04003072 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3073 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003074 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003075 spin_unlock(&root->orphan_lock);
3076
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003077 if (delete_item) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003078 atomic_dec(&root->orphan_inodes);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003079 if (trans)
3080 ret = btrfs_del_orphan_item(trans, root,
3081 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04003082 }
Josef Bacik7b128762008-07-24 12:17:14 -04003083
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003084 if (release_rsv)
3085 btrfs_orphan_release_metadata(inode);
3086
Josef Bacik4ef31a42013-08-13 14:10:08 -04003087 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003088}
3089
3090/*
3091 * this cleans up any orphans that may be left on the list from the last use
3092 * of this root.
3093 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003094int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003095{
3096 struct btrfs_path *path;
3097 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003098 struct btrfs_key key, found_key;
3099 struct btrfs_trans_handle *trans;
3100 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003101 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003102 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3103
Yan, Zhengd68fc572010-05-16 10:49:58 -04003104 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003105 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003106
3107 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003108 if (!path) {
3109 ret = -ENOMEM;
3110 goto out;
3111 }
Josef Bacik7b128762008-07-24 12:17:14 -04003112 path->reada = -1;
3113
3114 key.objectid = BTRFS_ORPHAN_OBJECTID;
3115 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
3116 key.offset = (u64)-1;
3117
Josef Bacik7b128762008-07-24 12:17:14 -04003118 while (1) {
3119 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003120 if (ret < 0)
3121 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003122
3123 /*
3124 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003125 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003126 * find the key and see if we have stuff that matches
3127 */
3128 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003129 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003130 if (path->slots[0] == 0)
3131 break;
3132 path->slots[0]--;
3133 }
3134
3135 /* pull out the item */
3136 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003137 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3138
3139 /* make sure the item matches what we want */
3140 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3141 break;
3142 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
3143 break;
3144
3145 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003146 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003147
3148 /*
3149 * this is where we are basically btrfs_lookup, without the
3150 * crossing root thing. we store the inode number in the
3151 * offset of the orphan item.
3152 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003153
3154 if (found_key.offset == last_objectid) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003155 btrfs_err(root->fs_info,
3156 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003157 ret = -EINVAL;
3158 goto out;
3159 }
3160
3161 last_objectid = found_key.offset;
3162
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003163 found_key.objectid = found_key.offset;
3164 found_key.type = BTRFS_INODE_ITEM_KEY;
3165 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00003166 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303167 ret = PTR_ERR_OR_ZERO(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003168 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003169 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003170
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003171 if (ret == -ESTALE && root == root->fs_info->tree_root) {
3172 struct btrfs_root *dead_root;
3173 struct btrfs_fs_info *fs_info = root->fs_info;
3174 int is_dead_root = 0;
3175
3176 /*
3177 * this is an orphan in the tree root. Currently these
3178 * could come from 2 sources:
3179 * a) a snapshot deletion in progress
3180 * b) a free space cache inode
3181 * We need to distinguish those two, as the snapshot
3182 * orphan must not get deleted.
3183 * find_dead_roots already ran before us, so if this
3184 * is a snapshot deletion, we should find the root
3185 * in the dead_roots list
3186 */
3187 spin_lock(&fs_info->trans_lock);
3188 list_for_each_entry(dead_root, &fs_info->dead_roots,
3189 root_list) {
3190 if (dead_root->root_key.objectid ==
3191 found_key.objectid) {
3192 is_dead_root = 1;
3193 break;
3194 }
3195 }
3196 spin_unlock(&fs_info->trans_lock);
3197 if (is_dead_root) {
3198 /* prevent this orphan from being found again */
3199 key.offset = found_key.objectid - 1;
3200 continue;
3201 }
3202 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003203 /*
3204 * Inode is already gone but the orphan item is still there,
3205 * kill the orphan item.
3206 */
3207 if (ret == -ESTALE) {
3208 trans = btrfs_start_transaction(root, 1);
3209 if (IS_ERR(trans)) {
3210 ret = PTR_ERR(trans);
3211 goto out;
3212 }
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003213 btrfs_debug(root->fs_info, "auto deleting %Lu",
3214 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003215 ret = btrfs_del_orphan_item(trans, root,
3216 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003217 btrfs_end_transaction(trans, root);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003218 if (ret)
3219 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003220 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003221 }
Josef Bacik7b128762008-07-24 12:17:14 -04003222
Josef Bacik7b128762008-07-24 12:17:14 -04003223 /*
3224 * add this inode to the orphan list so btrfs_orphan_del does
3225 * the proper thing when we hit it
3226 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003227 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3228 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003229 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003230
Josef Bacik7b128762008-07-24 12:17:14 -04003231 /* if we have links, this was a truncate, lets do that */
3232 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303233 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003234 iput(inode);
3235 continue;
3236 }
Josef Bacik7b128762008-07-24 12:17:14 -04003237 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003238
3239 /* 1 for the orphan item deletion. */
3240 trans = btrfs_start_transaction(root, 1);
3241 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003242 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003243 ret = PTR_ERR(trans);
3244 goto out;
3245 }
3246 ret = btrfs_orphan_add(trans, inode);
3247 btrfs_end_transaction(trans, root);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003248 if (ret) {
3249 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003250 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003251 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003252
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003253 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003254 if (ret)
3255 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003256 } else {
3257 nr_unlink++;
3258 }
3259
3260 /* this will do delete_inode and everything for us */
3261 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003262 if (ret)
3263 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003264 }
Miao Xie3254c872011-11-10 20:45:05 -05003265 /* release the path since we're done with it */
3266 btrfs_release_path(path);
3267
Yan, Zhengd68fc572010-05-16 10:49:58 -04003268 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3269
3270 if (root->orphan_block_rsv)
3271 btrfs_block_rsv_release(root, root->orphan_block_rsv,
3272 (u64)-1);
3273
3274 if (root->orphan_block_rsv || root->orphan_item_inserted) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003275 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003276 if (!IS_ERR(trans))
3277 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003278 }
Josef Bacik7b128762008-07-24 12:17:14 -04003279
3280 if (nr_unlink)
David Sterba4884b472013-03-20 13:31:27 +00003281 btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003282 if (nr_truncate)
David Sterba4884b472013-03-20 13:31:27 +00003283 btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003284
3285out:
3286 if (ret)
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003287 btrfs_crit(root->fs_info,
3288 "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003289 btrfs_free_path(path);
3290 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003291}
3292
Chris Masond352ac62008-09-29 15:18:18 -04003293/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003294 * very simple check to peek ahead in the leaf looking for xattrs. If we
3295 * don't find any xattrs, we know there can't be any acls.
3296 *
3297 * slot is the slot the inode is in, objectid is the objectid of the inode
3298 */
3299static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003300 int slot, u64 objectid,
3301 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003302{
3303 u32 nritems = btrfs_header_nritems(leaf);
3304 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003305 static u64 xattr_access = 0;
3306 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003307 int scanned = 0;
3308
Josef Bacikf23b5a52013-06-19 10:16:26 -04003309 if (!xattr_access) {
3310 xattr_access = btrfs_name_hash(POSIX_ACL_XATTR_ACCESS,
3311 strlen(POSIX_ACL_XATTR_ACCESS));
3312 xattr_default = btrfs_name_hash(POSIX_ACL_XATTR_DEFAULT,
3313 strlen(POSIX_ACL_XATTR_DEFAULT));
3314 }
3315
Chris Mason46a53cc2009-04-27 11:47:50 -04003316 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003317 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003318 while (slot < nritems) {
3319 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3320
3321 /* we found a different objectid, there must not be acls */
3322 if (found_key.objectid != objectid)
3323 return 0;
3324
3325 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003326 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003327 if (*first_xattr_slot == -1)
3328 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003329 if (found_key.offset == xattr_access ||
3330 found_key.offset == xattr_default)
3331 return 1;
3332 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003333
3334 /*
3335 * we found a key greater than an xattr key, there can't
3336 * be any acls later on
3337 */
3338 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3339 return 0;
3340
3341 slot++;
3342 scanned++;
3343
3344 /*
3345 * it goes inode, inode backrefs, xattrs, extents,
3346 * so if there are a ton of hard links to an inode there can
3347 * be a lot of backrefs. Don't waste time searching too hard,
3348 * this is just an optimization
3349 */
3350 if (scanned >= 8)
3351 break;
3352 }
3353 /* we hit the end of the leaf before we found an xattr or
3354 * something larger than an xattr. We have to assume the inode
3355 * has acls
3356 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003357 if (*first_xattr_slot == -1)
3358 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003359 return 1;
3360}
3361
3362/*
Chris Masond352ac62008-09-29 15:18:18 -04003363 * read an inode from the btree into the in-memory inode
3364 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003365static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003366{
3367 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003368 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003369 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04003370 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04003371 struct btrfs_root *root = BTRFS_I(inode)->root;
3372 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003373 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003374 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003375 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003376 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003377 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003378 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003379
3380 ret = btrfs_fill_inode(inode, &rdev);
3381 if (!ret)
3382 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003383
3384 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07003385 if (!path)
3386 goto make_bad;
3387
Chris Mason39279cc2007-06-12 06:35:45 -04003388 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003389
Chris Mason39279cc2007-06-12 06:35:45 -04003390 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003391 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04003392 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04003393
Chris Mason5f39d392007-10-15 16:14:19 -04003394 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003395
3396 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003397 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003398
Chris Mason5f39d392007-10-15 16:14:19 -04003399 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3400 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003401 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003402 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003403 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3404 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04003405 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003406
3407 tspec = btrfs_inode_atime(inode_item);
3408 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3409 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3410
3411 tspec = btrfs_inode_mtime(inode_item);
3412 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3413 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3414
3415 tspec = btrfs_inode_ctime(inode_item);
3416 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3417 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3418
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003419 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003420 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003421 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3422
3423 /*
3424 * If we were modified in the current generation and evicted from memory
3425 * and then re-read we need to do a full sync since we don't have any
3426 * idea about which extents were modified before we were evicted from
3427 * cache.
3428 */
3429 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
3430 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3431 &BTRFS_I(inode)->runtime_flags);
3432
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003433 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003434 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003435 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003436 rdev = btrfs_inode_rdev(leaf, inode_item);
3437
Josef Bacikaec74772008-07-24 12:12:38 -04003438 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003439 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003440
3441cache_index:
3442 path->slots[0]++;
3443 if (inode->i_nlink != 1 ||
3444 path->slots[0] >= btrfs_header_nritems(leaf))
3445 goto cache_acl;
3446
3447 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
3448 if (location.objectid != btrfs_ino(inode))
3449 goto cache_acl;
3450
3451 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3452 if (location.type == BTRFS_INODE_REF_KEY) {
3453 struct btrfs_inode_ref *ref;
3454
3455 ref = (struct btrfs_inode_ref *)ptr;
3456 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3457 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3458 struct btrfs_inode_extref *extref;
3459
3460 extref = (struct btrfs_inode_extref *)ptr;
3461 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3462 extref);
3463 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003464cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003465 /*
3466 * try to precache a NULL acl entry for files that don't have
3467 * any xattrs or acls
3468 */
Li Zefan33345d012011-04-20 10:31:50 +08003469 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003470 btrfs_ino(inode), &first_xattr_slot);
3471 if (first_xattr_slot != -1) {
3472 path->slots[0] = first_xattr_slot;
3473 ret = btrfs_load_inode_props(inode, path);
3474 if (ret)
3475 btrfs_err(root->fs_info,
3476 "error loading props for ino %llu (root %llu): %d\n",
3477 btrfs_ino(inode),
3478 root->root_key.objectid, ret);
3479 }
3480 btrfs_free_path(path);
3481
Al Viro72c04902009-06-24 16:58:48 -04003482 if (!maybe_acls)
3483 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003484
Chris Mason39279cc2007-06-12 06:35:45 -04003485 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003486 case S_IFREG:
3487 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003488 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05003489 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003490 inode->i_fop = &btrfs_file_operations;
3491 inode->i_op = &btrfs_file_inode_operations;
3492 break;
3493 case S_IFDIR:
3494 inode->i_fop = &btrfs_dir_file_operations;
3495 if (root == root->fs_info->tree_root)
3496 inode->i_op = &btrfs_dir_ro_inode_operations;
3497 else
3498 inode->i_op = &btrfs_dir_inode_operations;
3499 break;
3500 case S_IFLNK:
3501 inode->i_op = &btrfs_symlink_inode_operations;
3502 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04003503 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003504 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003505 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003506 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003507 init_special_inode(inode, inode->i_mode, rdev);
3508 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003509 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003510
3511 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003512 return;
3513
3514make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003515 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003516 make_bad_inode(inode);
3517}
3518
Chris Masond352ac62008-09-29 15:18:18 -04003519/*
3520 * given a leaf and an inode, copy the inode fields into the leaf
3521 */
Chris Masone02119d2008-09-05 16:13:11 -04003522static void fill_inode_item(struct btrfs_trans_handle *trans,
3523 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003524 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003525 struct inode *inode)
3526{
Liu Bo51fab692012-12-27 09:01:21 +00003527 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003528
Liu Bo51fab692012-12-27 09:01:21 +00003529 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003530
Liu Bo51fab692012-12-27 09:01:21 +00003531 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3532 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3533 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3534 &token);
3535 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3536 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003537
Liu Bo51fab692012-12-27 09:01:21 +00003538 btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item),
3539 inode->i_atime.tv_sec, &token);
3540 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item),
3541 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003542
Liu Bo51fab692012-12-27 09:01:21 +00003543 btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item),
3544 inode->i_mtime.tv_sec, &token);
3545 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item),
3546 inode->i_mtime.tv_nsec, &token);
3547
3548 btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item),
3549 inode->i_ctime.tv_sec, &token);
3550 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item),
3551 inode->i_ctime.tv_nsec, &token);
3552
3553 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3554 &token);
3555 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3556 &token);
3557 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3558 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3559 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3560 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3561 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003562}
3563
Chris Masond352ac62008-09-29 15:18:18 -04003564/*
3565 * copy everything in the in-memory inode into the btree.
3566 */
Chris Mason21151332011-11-10 20:39:08 -05003567static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003568 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003569{
3570 struct btrfs_inode_item *inode_item;
3571 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003572 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003573 int ret;
3574
3575 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003576 if (!path)
3577 return -ENOMEM;
3578
Chris Masonb9473432009-03-13 11:00:37 -04003579 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003580 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3581 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003582 if (ret) {
3583 if (ret > 0)
3584 ret = -ENOENT;
3585 goto failed;
3586 }
3587
Chris Mason5f39d392007-10-15 16:14:19 -04003588 leaf = path->nodes[0];
3589 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003590 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003591
Chris Masone02119d2008-09-05 16:13:11 -04003592 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003593 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003594 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003595 ret = 0;
3596failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003597 btrfs_free_path(path);
3598 return ret;
3599}
3600
Chris Masond352ac62008-09-29 15:18:18 -04003601/*
Chris Mason21151332011-11-10 20:39:08 -05003602 * copy everything in the in-memory inode into the btree.
3603 */
3604noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3605 struct btrfs_root *root, struct inode *inode)
3606{
3607 int ret;
3608
3609 /*
3610 * If the inode is a free space inode, we can deadlock during commit
3611 * if we put it into the delayed code.
3612 *
3613 * The data relocation inode should also be directly updated
3614 * without delay
3615 */
Liu Bo83eea1f2012-07-10 05:28:39 -06003616 if (!btrfs_is_free_space_inode(inode)
Chris Mason21151332011-11-10 20:39:08 -05003617 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003618 btrfs_update_root_times(trans, root);
3619
Chris Mason21151332011-11-10 20:39:08 -05003620 ret = btrfs_delayed_update_inode(trans, root, inode);
3621 if (!ret)
3622 btrfs_set_inode_last_trans(trans, inode);
3623 return ret;
3624 }
3625
3626 return btrfs_update_inode_item(trans, root, inode);
3627}
3628
Josef Bacikbe6aef62012-10-22 15:43:12 -04003629noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3630 struct btrfs_root *root,
3631 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003632{
3633 int ret;
3634
3635 ret = btrfs_update_inode(trans, root, inode);
3636 if (ret == -ENOSPC)
3637 return btrfs_update_inode_item(trans, root, inode);
3638 return ret;
3639}
3640
3641/*
Chris Masond352ac62008-09-29 15:18:18 -04003642 * unlink helper that gets used here in inode.c and in the tree logging
3643 * recovery code. It remove a link in a directory with a given name, and
3644 * also drops the back refs in the inode to the directory
3645 */
Al Viro92986792011-03-04 17:14:37 +00003646static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3647 struct btrfs_root *root,
3648 struct inode *dir, struct inode *inode,
3649 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003650{
3651 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003652 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003653 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003654 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003655 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003656 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003657 u64 ino = btrfs_ino(inode);
3658 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003659
3660 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003661 if (!path) {
3662 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003663 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003664 }
3665
Chris Masonb9473432009-03-13 11:00:37 -04003666 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003667 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003668 name, name_len, -1);
3669 if (IS_ERR(di)) {
3670 ret = PTR_ERR(di);
3671 goto err;
3672 }
3673 if (!di) {
3674 ret = -ENOENT;
3675 goto err;
3676 }
Chris Mason5f39d392007-10-15 16:14:19 -04003677 leaf = path->nodes[0];
3678 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003679 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003680 if (ret)
3681 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003682 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003683
Miao Xie67de1172013-12-26 13:07:06 +08003684 /*
3685 * If we don't have dir index, we have to get it by looking up
3686 * the inode ref, since we get the inode ref, remove it directly,
3687 * it is unnecessary to do delayed deletion.
3688 *
3689 * But if we have dir index, needn't search inode ref to get it.
3690 * Since the inode ref is close to the inode item, it is better
3691 * that we delay to delete it, and just do this deletion when
3692 * we update the inode item.
3693 */
3694 if (BTRFS_I(inode)->dir_index) {
3695 ret = btrfs_delayed_delete_inode_ref(inode);
3696 if (!ret) {
3697 index = BTRFS_I(inode)->dir_index;
3698 goto skip_backref;
3699 }
3700 }
3701
Li Zefan33345d012011-04-20 10:31:50 +08003702 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3703 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003704 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003705 btrfs_info(root->fs_info,
3706 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003707 name_len, name, ino, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003708 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003709 goto err;
3710 }
Miao Xie67de1172013-12-26 13:07:06 +08003711skip_backref:
Miao Xie16cdcec2011-04-22 18:12:22 +08003712 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003713 if (ret) {
3714 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003715 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003716 }
Chris Mason39279cc2007-06-12 06:35:45 -04003717
Chris Masone02119d2008-09-05 16:13:11 -04003718 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08003719 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003720 if (ret != 0 && ret != -ENOENT) {
3721 btrfs_abort_transaction(trans, root, ret);
3722 goto err;
3723 }
Chris Masone02119d2008-09-05 16:13:11 -04003724
3725 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
3726 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04003727 if (ret == -ENOENT)
3728 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003729 else if (ret)
3730 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003731err:
3732 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003733 if (ret)
3734 goto out;
3735
3736 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003737 inode_inc_iversion(inode);
3738 inode_inc_iversion(dir);
Chris Masone02119d2008-09-05 16:13:11 -04003739 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Tsutomu Itohb9959292012-06-25 21:25:22 -06003740 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04003741out:
Chris Mason39279cc2007-06-12 06:35:45 -04003742 return ret;
3743}
3744
Al Viro92986792011-03-04 17:14:37 +00003745int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3746 struct btrfs_root *root,
3747 struct inode *dir, struct inode *inode,
3748 const char *name, int name_len)
3749{
3750 int ret;
3751 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3752 if (!ret) {
Zach Brown8b558c52013-10-16 12:10:34 -07003753 drop_nlink(inode);
Al Viro92986792011-03-04 17:14:37 +00003754 ret = btrfs_update_inode(trans, root, inode);
3755 }
3756 return ret;
3757}
Yan, Zhenga22285a2010-05-16 10:48:46 -04003758
3759/*
3760 * helper to start transaction for unlink and rmdir.
3761 *
Josef Bacikd52be812013-05-29 14:54:47 -04003762 * unlink and rmdir are special in btrfs, they do not always free space, so
3763 * if we cannot make our reservations the normal way try and see if there is
3764 * plenty of slack room in the global reserve to migrate, otherwise we cannot
3765 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04003766 */
Josef Bacikd52be812013-05-29 14:54:47 -04003767static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003768{
3769 struct btrfs_trans_handle *trans;
3770 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003771 int ret;
3772
Josef Bacike70bea52011-10-11 14:18:24 -04003773 /*
3774 * 1 for the possible orphan item
3775 * 1 for the dir item
3776 * 1 for the dir index
3777 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04003778 * 1 for the inode
3779 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04003780 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003781 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
3782 return trans;
3783
Josef Bacikd52be812013-05-29 14:54:47 -04003784 if (PTR_ERR(trans) == -ENOSPC) {
3785 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003786
Josef Bacikd52be812013-05-29 14:54:47 -04003787 trans = btrfs_start_transaction(root, 0);
3788 if (IS_ERR(trans))
3789 return trans;
3790 ret = btrfs_cond_migrate_bytes(root->fs_info,
3791 &root->fs_info->trans_block_rsv,
3792 num_bytes, 5);
3793 if (ret) {
3794 btrfs_end_transaction(trans, root);
3795 return ERR_PTR(ret);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003796 }
Josef Bacik5a77d762011-11-01 14:32:23 -04003797 trans->block_rsv = &root->fs_info->trans_block_rsv;
Josef Bacikd52be812013-05-29 14:54:47 -04003798 trans->bytes_reserved = num_bytes;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003799 }
Josef Bacikd52be812013-05-29 14:54:47 -04003800 return trans;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003801}
3802
Chris Mason39279cc2007-06-12 06:35:45 -04003803static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3804{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003805 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003806 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04003807 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003808 int ret;
3809
Josef Bacikd52be812013-05-29 14:54:47 -04003810 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003811 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003812 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003813
Chris Mason12fcfd22009-03-24 10:24:20 -04003814 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
3815
Chris Masone02119d2008-09-05 16:13:11 -04003816 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3817 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003818 if (ret)
3819 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003820
Yan, Zhenga22285a2010-05-16 10:48:46 -04003821 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04003822 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003823 if (ret)
3824 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003825 }
Josef Bacik7b128762008-07-24 12:17:14 -04003826
Tsutomu Itohb5324022011-07-19 07:27:20 +00003827out:
Josef Bacikd52be812013-05-29 14:54:47 -04003828 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003829 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04003830 return ret;
3831}
3832
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003833int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3834 struct btrfs_root *root,
3835 struct inode *dir, u64 objectid,
3836 const char *name, int name_len)
3837{
3838 struct btrfs_path *path;
3839 struct extent_buffer *leaf;
3840 struct btrfs_dir_item *di;
3841 struct btrfs_key key;
3842 u64 index;
3843 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003844 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003845
3846 path = btrfs_alloc_path();
3847 if (!path)
3848 return -ENOMEM;
3849
Li Zefan33345d012011-04-20 10:31:50 +08003850 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003851 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003852 if (IS_ERR_OR_NULL(di)) {
3853 if (!di)
3854 ret = -ENOENT;
3855 else
3856 ret = PTR_ERR(di);
3857 goto out;
3858 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003859
3860 leaf = path->nodes[0];
3861 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3862 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3863 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003864 if (ret) {
3865 btrfs_abort_transaction(trans, root, ret);
3866 goto out;
3867 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003868 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003869
3870 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3871 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003872 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003873 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003874 if (ret != -ENOENT) {
3875 btrfs_abort_transaction(trans, root, ret);
3876 goto out;
3877 }
Li Zefan33345d012011-04-20 10:31:50 +08003878 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003879 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003880 if (IS_ERR_OR_NULL(di)) {
3881 if (!di)
3882 ret = -ENOENT;
3883 else
3884 ret = PTR_ERR(di);
3885 btrfs_abort_transaction(trans, root, ret);
3886 goto out;
3887 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003888
3889 leaf = path->nodes[0];
3890 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003891 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003892 index = key.offset;
3893 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003894 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003895
Miao Xie16cdcec2011-04-22 18:12:22 +08003896 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003897 if (ret) {
3898 btrfs_abort_transaction(trans, root, ret);
3899 goto out;
3900 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003901
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003902 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003903 inode_inc_iversion(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003904 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Josef Bacik5a24e842012-08-08 10:12:59 -06003905 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003906 if (ret)
3907 btrfs_abort_transaction(trans, root, ret);
3908out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04003909 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003910 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003911}
3912
Chris Mason39279cc2007-06-12 06:35:45 -04003913static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3914{
3915 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003916 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003917 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003918 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04003919
David Sterbab3ae2442012-09-13 16:04:34 -06003920 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04003921 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06003922 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
3923 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04003924
Josef Bacikd52be812013-05-29 14:54:47 -04003925 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003926 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003927 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003928
Li Zefan33345d012011-04-20 10:31:50 +08003929 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003930 err = btrfs_unlink_subvol(trans, root, dir,
3931 BTRFS_I(inode)->location.objectid,
3932 dentry->d_name.name,
3933 dentry->d_name.len);
3934 goto out;
3935 }
3936
Josef Bacik7b128762008-07-24 12:17:14 -04003937 err = btrfs_orphan_add(trans, inode);
3938 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003939 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003940
Chris Mason39279cc2007-06-12 06:35:45 -04003941 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04003942 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3943 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05003944 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04003945 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003946out:
Josef Bacikd52be812013-05-29 14:54:47 -04003947 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003948 btrfs_btree_balance_dirty(root);
Chris Mason39544012007-12-12 14:38:19 -05003949
Chris Mason39279cc2007-06-12 06:35:45 -04003950 return err;
3951}
3952
Chris Mason323ac952008-10-01 19:05:46 -04003953/*
Chris Mason39279cc2007-06-12 06:35:45 -04003954 * this can truncate away extent items, csum items and directory items.
3955 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04003956 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04003957 *
3958 * csum items that cross the new i_size are truncated to the new size
3959 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04003960 *
3961 * min_type is the minimum key type to truncate down to. If set to 0, this
3962 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04003963 */
Yan, Zheng80825102009-11-12 09:35:36 +00003964int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
3965 struct btrfs_root *root,
3966 struct inode *inode,
3967 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003968{
Chris Mason39279cc2007-06-12 06:35:45 -04003969 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003970 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003971 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00003972 struct btrfs_key key;
3973 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04003974 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04003975 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003976 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003977 u64 item_end = 0;
Josef Bacik7f4f6e02013-08-29 16:43:28 -04003978 u64 last_size = (u64)-1;
Yan, Zheng80825102009-11-12 09:35:36 +00003979 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04003980 int found_extent;
3981 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003982 int pending_del_nr = 0;
3983 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04003984 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00003985 int ret;
3986 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08003987 u64 ino = btrfs_ino(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003988
3989 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04003990
Mark Fasheh0eb0e192011-07-12 16:44:10 -07003991 path = btrfs_alloc_path();
3992 if (!path)
3993 return -ENOMEM;
3994 path->reada = -1;
3995
Josef Bacik5dc562c2012-08-17 13:14:17 -04003996 /*
3997 * We want to drop from the next block forward in case this new size is
3998 * not block aligned since we will be keeping the last block of the
3999 * extent just the way it is.
4000 */
Josef Bacik0af3d002010-06-21 14:48:16 -04004001 if (root->ref_cows || root == root->fs_info->tree_root)
Qu Wenruofda28322013-02-26 08:10:22 +00004002 btrfs_drop_extent_cache(inode, ALIGN(new_size,
4003 root->sectorsize), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004004
Miao Xie16cdcec2011-04-22 18:12:22 +08004005 /*
4006 * This function is also used to drop the items in the log tree before
4007 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4008 * it is used to drop the loged items. So we shouldn't kill the delayed
4009 * items.
4010 */
4011 if (min_type == 0 && root == BTRFS_I(inode)->root)
4012 btrfs_kill_delayed_inode_items(inode);
4013
Li Zefan33345d012011-04-20 10:31:50 +08004014 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004015 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004016 key.type = (u8)-1;
4017
Chris Mason85e21ba2008-01-29 15:11:36 -05004018search_again:
Chris Masonb9473432009-03-13 11:00:37 -04004019 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004020 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004021 if (ret < 0) {
4022 err = ret;
4023 goto out;
4024 }
Chris Masond3977122009-01-05 21:25:51 -05004025
Chris Mason85e21ba2008-01-29 15:11:36 -05004026 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004027 /* there are no items in the tree for us to truncate, we're
4028 * done
4029 */
Yan, Zheng80825102009-11-12 09:35:36 +00004030 if (path->slots[0] == 0)
4031 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004032 path->slots[0]--;
4033 }
4034
Chris Masond3977122009-01-05 21:25:51 -05004035 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004036 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004037 leaf = path->nodes[0];
4038 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4039 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04004040
Li Zefan33345d012011-04-20 10:31:50 +08004041 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004042 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004043
Chris Mason85e21ba2008-01-29 15:11:36 -05004044 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004045 break;
4046
Chris Mason5f39d392007-10-15 16:14:19 -04004047 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004048 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004049 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004050 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004051 extent_type = btrfs_file_extent_type(leaf, fi);
4052 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004053 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004054 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04004055 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004056 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004057 path->slots[0], fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004058 }
Yan008630c2007-11-07 13:31:09 -05004059 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004060 }
Yan, Zheng80825102009-11-12 09:35:36 +00004061 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004062 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004063 } else {
4064 if (item_end < new_size)
4065 break;
4066 if (found_key.offset >= new_size)
4067 del_item = 1;
4068 else
4069 del_item = 0;
4070 }
Chris Mason39279cc2007-06-12 06:35:45 -04004071 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004072 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004073 if (found_type != BTRFS_EXTENT_DATA_KEY)
4074 goto delete;
4075
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004076 if (del_item)
4077 last_size = found_key.offset;
4078 else
4079 last_size = new_size;
4080
Chris Mason179e29e2007-11-01 11:28:41 -04004081 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004082 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004083 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004084 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004085 u64 orig_num_bytes =
4086 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004087 extent_num_bytes = ALIGN(new_size -
4088 found_key.offset,
4089 root->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004090 btrfs_set_file_extent_num_bytes(leaf, fi,
4091 extent_num_bytes);
4092 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004093 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04004094 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004095 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004096 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004097 } else {
Chris Masondb945352007-10-15 16:15:53 -04004098 extent_num_bytes =
4099 btrfs_file_extent_disk_num_bytes(leaf,
4100 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004101 extent_offset = found_key.offset -
4102 btrfs_file_extent_offset(leaf, fi);
4103
Chris Mason39279cc2007-06-12 06:35:45 -04004104 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004105 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004106 if (extent_start != 0) {
4107 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04004108 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004109 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004110 }
4111 }
Chris Mason90692182008-02-08 13:49:28 -05004112 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004113 /*
4114 * we can't truncate inline items that have had
4115 * special encodings
4116 */
4117 if (!del_item &&
4118 btrfs_file_extent_compression(leaf, fi) == 0 &&
4119 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4120 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004121 u32 size = new_size - found_key.offset;
4122
4123 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004124 inode_sub_bytes(inode, item_end + 1 -
4125 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04004126 }
Chris Mason514ac8a2014-01-03 21:07:00 -08004127
4128 /*
4129 * update the ram bytes to properly reflect
4130 * the new size of our item
4131 */
4132 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
Chris Masone02119d2008-09-05 16:13:11 -04004133 size =
4134 btrfs_file_extent_calc_inline_size(size);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004135 btrfs_truncate_item(root, path, size, 1);
Chris Masone02119d2008-09-05 16:13:11 -04004136 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004137 inode_sub_bytes(inode, item_end + 1 -
4138 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05004139 }
Chris Mason39279cc2007-06-12 06:35:45 -04004140 }
Chris Mason179e29e2007-11-01 11:28:41 -04004141delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004142 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004143 if (!pending_del_nr) {
4144 /* no pending yet, add ourselves */
4145 pending_del_slot = path->slots[0];
4146 pending_del_nr = 1;
4147 } else if (pending_del_nr &&
4148 path->slots[0] + 1 == pending_del_slot) {
4149 /* hop on the pending chunk */
4150 pending_del_nr++;
4151 pending_del_slot = path->slots[0];
4152 } else {
Chris Masond3977122009-01-05 21:25:51 -05004153 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004154 }
Chris Mason39279cc2007-06-12 06:35:45 -04004155 } else {
4156 break;
4157 }
Josef Bacik0af3d002010-06-21 14:48:16 -04004158 if (found_extent && (root->ref_cows ||
4159 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004160 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004161 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004162 extent_num_bytes, 0,
4163 btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02004164 ino, extent_offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004165 BUG_ON(ret);
4166 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004167
Yan, Zheng80825102009-11-12 09:35:36 +00004168 if (found_type == BTRFS_INODE_ITEM_KEY)
4169 break;
4170
4171 if (path->slots[0] == 0 ||
4172 path->slots[0] != pending_del_slot) {
Yan, Zheng80825102009-11-12 09:35:36 +00004173 if (pending_del_nr) {
4174 ret = btrfs_del_items(trans, root, path,
4175 pending_del_slot,
4176 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004177 if (ret) {
4178 btrfs_abort_transaction(trans,
4179 root, ret);
4180 goto error;
4181 }
Yan, Zheng80825102009-11-12 09:35:36 +00004182 pending_del_nr = 0;
4183 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004184 btrfs_release_path(path);
Chris Mason85e21ba2008-01-29 15:11:36 -05004185 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004186 } else {
4187 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004188 }
Chris Mason39279cc2007-06-12 06:35:45 -04004189 }
Yan, Zheng80825102009-11-12 09:35:36 +00004190out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004191 if (pending_del_nr) {
4192 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4193 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004194 if (ret)
4195 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004196 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004197error:
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004198 if (last_size != (u64)-1)
4199 btrfs_ordered_update_i_size(inode, last_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004200 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00004201 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004202}
4203
Chris Masona52d9a82007-08-27 16:49:44 -04004204/*
Josef Bacik2aaa6652012-08-29 14:27:18 -04004205 * btrfs_truncate_page - read, zero a chunk and write a page
4206 * @inode - inode that we're zeroing
4207 * @from - the offset to start zeroing
4208 * @len - the length to zero, 0 to zero the entire range respective to the
4209 * offset
4210 * @front - zero up to the offset instead of from the offset on
4211 *
4212 * This will find the page for the "from" offset and cow the page and zero the
4213 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004214 */
Josef Bacik2aaa6652012-08-29 14:27:18 -04004215int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
4216 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004217{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004218 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04004219 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004220 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4221 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004222 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004223 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04004224 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04004225 pgoff_t index = from >> PAGE_CACHE_SHIFT;
4226 unsigned offset = from & (PAGE_CACHE_SIZE-1);
4227 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004228 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004229 int ret = 0;
4230 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004231 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004232
Josef Bacik2aaa6652012-08-29 14:27:18 -04004233 if ((offset & (blocksize - 1)) == 0 &&
4234 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004235 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004236 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004237 if (ret)
4238 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004239
Chris Mason211c17f2008-05-15 09:13:45 -04004240again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004241 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004242 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004243 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Miao Xieac6a2b32012-12-05 10:56:13 +00004244 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004245 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004246 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004247
4248 page_start = page_offset(page);
4249 page_end = page_start + PAGE_CACHE_SIZE - 1;
4250
Chris Masona52d9a82007-08-27 16:49:44 -04004251 if (!PageUptodate(page)) {
4252 ret = btrfs_readpage(NULL, page);
4253 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004254 if (page->mapping != mapping) {
4255 unlock_page(page);
4256 page_cache_release(page);
4257 goto again;
4258 }
Chris Masona52d9a82007-08-27 16:49:44 -04004259 if (!PageUptodate(page)) {
4260 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004261 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004262 }
4263 }
Chris Mason211c17f2008-05-15 09:13:45 -04004264 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004265
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004266 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004267 set_page_extent_mapped(page);
4268
4269 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4270 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004271 unlock_extent_cached(io_tree, page_start, page_end,
4272 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004273 unlock_page(page);
4274 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004275 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004276 btrfs_put_ordered_extent(ordered);
4277 goto again;
4278 }
4279
Josef Bacik2ac55d42010-02-03 19:33:23 +00004280 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004281 EXTENT_DIRTY | EXTENT_DELALLOC |
4282 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004283 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004284
Josef Bacik2ac55d42010-02-03 19:33:23 +00004285 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
4286 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004287 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004288 unlock_extent_cached(io_tree, page_start, page_end,
4289 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004290 goto out_unlock;
4291 }
4292
Chris Masone6dcd2d2008-07-17 12:53:50 -04004293 if (offset != PAGE_CACHE_SIZE) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004294 if (!len)
4295 len = PAGE_CACHE_SIZE - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004296 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004297 if (front)
4298 memset(kaddr, 0, offset);
4299 else
4300 memset(kaddr + offset, 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004301 flush_dcache_page(page);
4302 kunmap(page);
4303 }
Chris Mason247e7432008-07-17 12:53:51 -04004304 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004305 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004306 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
4307 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004308
Chris Mason89642222008-07-24 09:41:53 -04004309out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004310 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004311 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04004312 unlock_page(page);
4313 page_cache_release(page);
4314out:
4315 return ret;
4316}
4317
Josef Bacik16e75492013-10-22 12:18:51 -04004318static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4319 u64 offset, u64 len)
4320{
4321 struct btrfs_trans_handle *trans;
4322 int ret;
4323
4324 /*
4325 * Still need to make sure the inode looks like it's been updated so
4326 * that any holes get logged if we fsync.
4327 */
4328 if (btrfs_fs_incompat(root->fs_info, NO_HOLES)) {
4329 BTRFS_I(inode)->last_trans = root->fs_info->generation;
4330 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4331 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4332 return 0;
4333 }
4334
4335 /*
4336 * 1 - for the one we're dropping
4337 * 1 - for the one we're adding
4338 * 1 - for updating the inode.
4339 */
4340 trans = btrfs_start_transaction(root, 3);
4341 if (IS_ERR(trans))
4342 return PTR_ERR(trans);
4343
4344 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4345 if (ret) {
4346 btrfs_abort_transaction(trans, root, ret);
4347 btrfs_end_transaction(trans, root);
4348 return ret;
4349 }
4350
4351 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset,
4352 0, 0, len, 0, len, 0, 0, 0);
4353 if (ret)
4354 btrfs_abort_transaction(trans, root, ret);
4355 else
4356 btrfs_update_inode(trans, root, inode);
4357 btrfs_end_transaction(trans, root);
4358 return ret;
4359}
4360
Josef Bacik695a0d02011-03-04 15:46:53 -05004361/*
4362 * This function puts in dummy file extents for the area we're creating a hole
4363 * for. So if we are truncating this file to a larger size we need to insert
4364 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4365 * the range between oldsize and size
4366 */
Josef Bacika41ad392011-01-31 15:30:16 -05004367int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004368{
Yan Zheng9036c102008-10-30 14:19:41 -04004369 struct btrfs_root *root = BTRFS_I(inode)->root;
4370 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004371 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004372 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004373 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Qu Wenruofda28322013-02-26 08:10:22 +00004374 u64 hole_start = ALIGN(oldsize, root->sectorsize);
4375 u64 block_end = ALIGN(size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004376 u64 last_byte;
4377 u64 cur_offset;
4378 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004379 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004380
Josef Bacika71754f2013-06-17 17:14:39 -04004381 /*
4382 * If our size started in the middle of a page we need to zero out the
4383 * rest of the page before we expand the i_size, otherwise we could
4384 * expose stale data.
4385 */
4386 err = btrfs_truncate_page(inode, oldsize, 0, 0);
4387 if (err)
4388 return err;
4389
Yan Zheng9036c102008-10-30 14:19:41 -04004390 if (size <= hole_start)
4391 return 0;
4392
Yan Zheng9036c102008-10-30 14:19:41 -04004393 while (1) {
4394 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004395
Josef Bacik2ac55d42010-02-03 19:33:23 +00004396 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004397 &cached_state);
Miao Xiefa7c1492013-09-26 13:15:27 +08004398 ordered = btrfs_lookup_ordered_range(inode, hole_start,
4399 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004400 if (!ordered)
4401 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004402 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4403 &cached_state, GFP_NOFS);
Miao Xiefa7c1492013-09-26 13:15:27 +08004404 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004405 btrfs_put_ordered_extent(ordered);
4406 }
4407
Yan Zheng9036c102008-10-30 14:19:41 -04004408 cur_offset = hole_start;
4409 while (1) {
4410 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4411 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004412 if (IS_ERR(em)) {
4413 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004414 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004415 break;
4416 }
Yan Zheng9036c102008-10-30 14:19:41 -04004417 last_byte = min(extent_map_end(em), block_end);
Qu Wenruofda28322013-02-26 08:10:22 +00004418 last_byte = ALIGN(last_byte , root->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004419 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004420 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004421 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004422
Josef Bacik16e75492013-10-22 12:18:51 -04004423 err = maybe_insert_hole(root, inode, cur_offset,
4424 hole_size);
4425 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004426 break;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004427 btrfs_drop_extent_cache(inode, cur_offset,
4428 cur_offset + hole_size - 1, 0);
4429 hole_em = alloc_extent_map();
4430 if (!hole_em) {
4431 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4432 &BTRFS_I(inode)->runtime_flags);
4433 goto next;
4434 }
4435 hole_em->start = cur_offset;
4436 hole_em->len = hole_size;
4437 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004438
Josef Bacik5dc562c2012-08-17 13:14:17 -04004439 hole_em->block_start = EXTENT_MAP_HOLE;
4440 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004441 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004442 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004443 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
4444 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Josef Bacik16e75492013-10-22 12:18:51 -04004445 hole_em->generation = root->fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004446
4447 while (1) {
4448 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004449 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004450 write_unlock(&em_tree->lock);
4451 if (err != -EEXIST)
4452 break;
4453 btrfs_drop_extent_cache(inode, cur_offset,
4454 cur_offset +
4455 hole_size - 1, 0);
4456 }
4457 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04004458 }
Josef Bacik16e75492013-10-22 12:18:51 -04004459next:
Yan Zheng9036c102008-10-30 14:19:41 -04004460 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004461 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004462 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004463 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004464 break;
4465 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004466 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004467 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4468 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004469 return err;
4470}
4471
Eric Sandeen3972f262013-01-12 02:57:22 +00004472static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004473{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004474 struct btrfs_root *root = BTRFS_I(inode)->root;
4475 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004476 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004477 loff_t newsize = attr->ia_size;
4478 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004479 int ret;
4480
Eric Sandeen3972f262013-01-12 02:57:22 +00004481 /*
4482 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4483 * special case where we need to update the times despite not having
4484 * these flags set. For all other operations the VFS set these flags
4485 * explicitly if it wants a timestamp update.
4486 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004487 if (newsize != oldsize) {
4488 inode_inc_iversion(inode);
4489 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
4490 inode->i_ctime = inode->i_mtime =
4491 current_fs_time(inode->i_sb);
4492 }
Eric Sandeen3972f262013-01-12 02:57:22 +00004493
Josef Bacika41ad392011-01-31 15:30:16 -05004494 if (newsize > oldsize) {
Kirill A. Shutemov7caef262013-09-12 15:13:56 -07004495 truncate_pagecache(inode, newsize);
Josef Bacika41ad392011-01-31 15:30:16 -05004496 ret = btrfs_cont_expand(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004497 if (ret)
Yan, Zheng80825102009-11-12 09:35:36 +00004498 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004499
Miao Xief4a2f4c2011-12-14 20:12:01 -05004500 trans = btrfs_start_transaction(root, 1);
4501 if (IS_ERR(trans))
4502 return PTR_ERR(trans);
4503
4504 i_size_write(inode, newsize);
4505 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
4506 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004507 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05004508 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004509
Josef Bacika41ad392011-01-31 15:30:16 -05004510 /*
4511 * We're truncating a file that used to have good data down to
4512 * zero. Make sure it gets into the ordered flush list so that
4513 * any new writes get down to disk quickly.
4514 */
4515 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004516 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4517 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004518
Josef Bacikf3fe8202013-01-07 17:03:21 -05004519 /*
4520 * 1 for the orphan item we're going to add
4521 * 1 for the orphan item deletion.
4522 */
4523 trans = btrfs_start_transaction(root, 2);
4524 if (IS_ERR(trans))
4525 return PTR_ERR(trans);
4526
4527 /*
4528 * We need to do this in case we fail at _any_ point during the
4529 * actual truncate. Once we do the truncate_setsize we could
4530 * invalidate pages which forces any outstanding ordered io to
4531 * be instantly completed which will give us extents that need
4532 * to be truncated. If we fail to get an orphan inode down we
4533 * could have left over extents that were never meant to live,
4534 * so we need to garuntee from this point on that everything
4535 * will be consistent.
4536 */
4537 ret = btrfs_orphan_add(trans, inode);
4538 btrfs_end_transaction(trans, root);
4539 if (ret)
4540 return ret;
4541
Josef Bacika41ad392011-01-31 15:30:16 -05004542 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4543 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004544
4545 /* Disable nonlocked read DIO to avoid the end less truncate */
4546 btrfs_inode_block_unlocked_dio(inode);
4547 inode_dio_wait(inode);
4548 btrfs_inode_resume_unlocked_dio(inode);
4549
Josef Bacika41ad392011-01-31 15:30:16 -05004550 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004551 if (ret && inode->i_nlink) {
4552 int err;
4553
4554 /*
4555 * failed to truncate, disk_i_size is only adjusted down
4556 * as we remove extents, so it should represent the true
4557 * size of the inode, so reset the in memory size and
4558 * delete our orphan entry.
4559 */
4560 trans = btrfs_join_transaction(root);
4561 if (IS_ERR(trans)) {
4562 btrfs_orphan_del(NULL, inode);
4563 return ret;
4564 }
4565 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
4566 err = btrfs_orphan_del(trans, inode);
4567 if (err)
4568 btrfs_abort_transaction(trans, root, err);
4569 btrfs_end_transaction(trans, root);
4570 }
Yan, Zheng80825102009-11-12 09:35:36 +00004571 }
4572
Josef Bacika41ad392011-01-31 15:30:16 -05004573 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004574}
4575
Chris Mason39279cc2007-06-12 06:35:45 -04004576static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
4577{
4578 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08004579 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004580 int err;
4581
Li Zefanb83cc962010-12-20 16:04:08 +08004582 if (btrfs_root_readonly(root))
4583 return -EROFS;
4584
Chris Mason39279cc2007-06-12 06:35:45 -04004585 err = inode_change_ok(inode, attr);
4586 if (err)
4587 return err;
4588
Chris Mason5a3f23d2009-03-31 13:27:11 -04004589 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00004590 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00004591 if (err)
4592 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004593 }
Yan Zheng9036c102008-10-30 14:19:41 -04004594
Christoph Hellwig10257742010-06-04 11:30:02 +02004595 if (attr->ia_valid) {
4596 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004597 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004598 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04004599
Josef Bacik22c44fe2011-11-30 10:45:38 -05004600 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08004601 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02004602 }
4603
Chris Mason39279cc2007-06-12 06:35:45 -04004604 return err;
4605}
Chris Mason61295eb2008-01-14 16:24:38 -05004606
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004607/*
4608 * While truncating the inode pages during eviction, we get the VFS calling
4609 * btrfs_invalidatepage() against each page of the inode. This is slow because
4610 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
4611 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
4612 * extent_state structures over and over, wasting lots of time.
4613 *
4614 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
4615 * those expensive operations on a per page basis and do only the ordered io
4616 * finishing, while we release here the extent_map and extent_state structures,
4617 * without the excessive merging and splitting.
4618 */
4619static void evict_inode_truncate_pages(struct inode *inode)
4620{
4621 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4622 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
4623 struct rb_node *node;
4624
4625 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07004626 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004627
4628 write_lock(&map_tree->lock);
4629 while (!RB_EMPTY_ROOT(&map_tree->map)) {
4630 struct extent_map *em;
4631
4632 node = rb_first(&map_tree->map);
4633 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08004634 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
4635 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004636 remove_extent_mapping(map_tree, em);
4637 free_extent_map(em);
4638 }
4639 write_unlock(&map_tree->lock);
4640
4641 spin_lock(&io_tree->lock);
4642 while (!RB_EMPTY_ROOT(&io_tree->state)) {
4643 struct extent_state *state;
4644 struct extent_state *cached_state = NULL;
4645
4646 node = rb_first(&io_tree->state);
4647 state = rb_entry(node, struct extent_state, rb_node);
4648 atomic_inc(&state->refs);
4649 spin_unlock(&io_tree->lock);
4650
4651 lock_extent_bits(io_tree, state->start, state->end,
4652 0, &cached_state);
4653 clear_extent_bit(io_tree, state->start, state->end,
4654 EXTENT_LOCKED | EXTENT_DIRTY |
4655 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
4656 EXTENT_DEFRAG, 1, 1,
4657 &cached_state, GFP_NOFS);
4658 free_extent_state(state);
4659
4660 spin_lock(&io_tree->lock);
4661 }
4662 spin_unlock(&io_tree->lock);
4663}
4664
Al Virobd555972010-06-07 11:35:40 -04004665void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004666{
4667 struct btrfs_trans_handle *trans;
4668 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04004669 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik07127182011-08-19 10:29:59 -04004670 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04004671 int ret;
4672
liubo1abe9b82011-03-24 11:18:59 +00004673 trace_btrfs_inode_evict(inode);
4674
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004675 evict_inode_truncate_pages(inode);
4676
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02004677 if (inode->i_nlink &&
4678 ((btrfs_root_refs(&root->root_item) != 0 &&
4679 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
4680 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04004681 goto no_delete;
4682
Chris Mason39279cc2007-06-12 06:35:45 -04004683 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04004684 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004685 goto no_delete;
4686 }
Al Virobd555972010-06-07 11:35:40 -04004687 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04004688 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04004689
Yan, Zhengc71bf092009-11-12 09:34:40 +00004690 if (root->fs_info->log_root_recovering) {
Liu Bo6bf02312012-06-25 21:59:09 -06004691 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04004692 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00004693 goto no_delete;
4694 }
4695
Yan, Zheng76dda932009-09-21 16:00:26 -04004696 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02004697 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
4698 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04004699 goto no_delete;
4700 }
4701
Miao Xie0e8c36a2012-12-19 06:59:51 +00004702 ret = btrfs_commit_inode_delayed_inode(inode);
4703 if (ret) {
4704 btrfs_orphan_del(NULL, inode);
4705 goto no_delete;
4706 }
4707
Miao Xie66d8f3d2012-09-06 04:02:28 -06004708 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04004709 if (!rsv) {
4710 btrfs_orphan_del(NULL, inode);
4711 goto no_delete;
4712 }
Josef Bacik4a338542011-08-29 11:01:31 -04004713 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04004714 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04004715 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04004716
Chris Masondbe674a2008-07-17 12:54:05 -04004717 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04004718
Josef Bacik4289a662011-08-05 13:22:24 -04004719 /*
Miao Xie8407aa42012-09-07 01:43:32 -06004720 * This is a bit simpler than btrfs_truncate since we've already
4721 * reserved our space for our orphan item in the unlink, so we just
4722 * need to reserve some slack space in case we add bytes and update
4723 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04004724 */
Yan, Zheng80825102009-11-12 09:35:36 +00004725 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00004726 ret = btrfs_block_rsv_refill(root, rsv, min_size,
4727 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00004728
Josef Bacik726c35f2011-09-26 15:46:06 -04004729 /*
4730 * Try and steal from the global reserve since we will
4731 * likely not use this space anyway, we want to try as
4732 * hard as possible to get this to work.
4733 */
4734 if (ret)
4735 ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
4736
Yan, Zhengd68fc572010-05-16 10:49:58 -04004737 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004738 btrfs_warn(root->fs_info,
4739 "Could not get space for a delete, will truncate on mount %d",
4740 ret);
Josef Bacik4289a662011-08-05 13:22:24 -04004741 btrfs_orphan_del(NULL, inode);
4742 btrfs_free_block_rsv(root, rsv);
4743 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04004744 }
4745
Miao Xie0e8c36a2012-12-19 06:59:51 +00004746 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04004747 if (IS_ERR(trans)) {
4748 btrfs_orphan_del(NULL, inode);
4749 btrfs_free_block_rsv(root, rsv);
4750 goto no_delete;
4751 }
4752
4753 trans->block_rsv = rsv;
4754
Yan, Zhengd68fc572010-05-16 10:49:58 -04004755 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04004756 if (ret != -ENOSPC)
Yan, Zheng80825102009-11-12 09:35:36 +00004757 break;
4758
Miao Xie8407aa42012-09-07 01:43:32 -06004759 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004760 btrfs_end_transaction(trans, root);
4761 trans = NULL;
Liu Bob53d3f52012-11-14 14:34:34 +00004762 btrfs_btree_balance_dirty(root);
Josef Bacik7b128762008-07-24 12:17:14 -04004763 }
4764
Josef Bacik4289a662011-08-05 13:22:24 -04004765 btrfs_free_block_rsv(root, rsv);
4766
Josef Bacik4ef31a42013-08-13 14:10:08 -04004767 /*
4768 * Errors here aren't a big deal, it just means we leave orphan items
4769 * in the tree. They will be cleaned up on the next mount.
4770 */
Yan, Zheng80825102009-11-12 09:35:36 +00004771 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04004772 trans->block_rsv = root->orphan_block_rsv;
Josef Bacik4ef31a42013-08-13 14:10:08 -04004773 btrfs_orphan_del(trans, inode);
4774 } else {
4775 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004776 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004777
Josef Bacik4289a662011-08-05 13:22:24 -04004778 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08004779 if (!(root == root->fs_info->tree_root ||
4780 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08004781 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08004782
Chris Mason54aa1f42007-06-22 14:16:25 -04004783 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004784 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04004785no_delete:
Miao Xie89042e52013-05-15 07:48:15 +00004786 btrfs_remove_delayed_node(inode);
Jan Karadbd57682012-05-03 14:48:02 +02004787 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004788 return;
Chris Mason39279cc2007-06-12 06:35:45 -04004789}
4790
4791/*
4792 * this returns the key found in the dir entry in the location pointer.
4793 * If no dir entries were found, location->objectid is 0.
4794 */
4795static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
4796 struct btrfs_key *location)
4797{
4798 const char *name = dentry->d_name.name;
4799 int namelen = dentry->d_name.len;
4800 struct btrfs_dir_item *di;
4801 struct btrfs_path *path;
4802 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04004803 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004804
4805 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07004806 if (!path)
4807 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05004808
Li Zefan33345d012011-04-20 10:31:50 +08004809 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04004810 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04004811 if (IS_ERR(di))
4812 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05004813
David Sterbac7040052011-04-19 18:00:01 +02004814 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05004815 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05004816
Chris Mason5f39d392007-10-15 16:14:19 -04004817 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04004818out:
Chris Mason39279cc2007-06-12 06:35:45 -04004819 btrfs_free_path(path);
4820 return ret;
Chris Mason39544012007-12-12 14:38:19 -05004821out_err:
4822 location->objectid = 0;
4823 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04004824}
4825
4826/*
4827 * when we hit a tree root in a directory, the btrfs part of the inode
4828 * needs to be changed to reflect the root directory of the tree root. This
4829 * is kind of like crossing a mount point.
4830 */
4831static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004832 struct inode *dir,
4833 struct dentry *dentry,
4834 struct btrfs_key *location,
4835 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04004836{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004837 struct btrfs_path *path;
4838 struct btrfs_root *new_root;
4839 struct btrfs_root_ref *ref;
4840 struct extent_buffer *leaf;
4841 int ret;
4842 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004843
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004844 path = btrfs_alloc_path();
4845 if (!path) {
4846 err = -ENOMEM;
4847 goto out;
4848 }
Chris Mason39279cc2007-06-12 06:35:45 -04004849
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004850 err = -ENOENT;
Kelley Nielsen75ac2dd2013-11-04 19:35:58 -08004851 ret = btrfs_find_item(root->fs_info->tree_root, path,
4852 BTRFS_I(dir)->root->root_key.objectid,
4853 location->objectid, BTRFS_ROOT_REF_KEY, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004854 if (ret) {
4855 if (ret < 0)
4856 err = ret;
4857 goto out;
4858 }
Chris Mason39279cc2007-06-12 06:35:45 -04004859
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004860 leaf = path->nodes[0];
4861 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08004862 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004863 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
4864 goto out;
4865
4866 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
4867 (unsigned long)(ref + 1),
4868 dentry->d_name.len);
4869 if (ret)
4870 goto out;
4871
David Sterbab3b4aa72011-04-21 01:20:15 +02004872 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004873
4874 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
4875 if (IS_ERR(new_root)) {
4876 err = PTR_ERR(new_root);
4877 goto out;
4878 }
4879
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004880 *sub_root = new_root;
4881 location->objectid = btrfs_root_dirid(&new_root->root_item);
4882 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04004883 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004884 err = 0;
4885out:
4886 btrfs_free_path(path);
4887 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004888}
4889
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004890static void inode_tree_add(struct inode *inode)
4891{
4892 struct btrfs_root *root = BTRFS_I(inode)->root;
4893 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004894 struct rb_node **p;
4895 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01004896 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Li Zefan33345d012011-04-20 10:31:50 +08004897 u64 ino = btrfs_ino(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004898
Al Viro1d3382cb2010-10-23 15:19:20 -04004899 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004900 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00004901 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004902 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00004903 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004904 while (*p) {
4905 parent = *p;
4906 entry = rb_entry(parent, struct btrfs_inode, rb_node);
4907
Li Zefan33345d012011-04-20 10:31:50 +08004908 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004909 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004910 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004911 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004912 else {
4913 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04004914 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01004915 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004916 RB_CLEAR_NODE(parent);
4917 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01004918 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004919 }
4920 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01004921 rb_link_node(new, parent, p);
4922 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004923 spin_unlock(&root->inode_lock);
4924}
4925
4926static void inode_tree_del(struct inode *inode)
4927{
4928 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04004929 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004930
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004931 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004932 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004933 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004934 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04004935 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004936 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004937 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04004938
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02004939 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004940 synchronize_srcu(&root->fs_info->subvol_srcu);
4941 spin_lock(&root->inode_lock);
4942 empty = RB_EMPTY_ROOT(&root->inode_tree);
4943 spin_unlock(&root->inode_lock);
4944 if (empty)
4945 btrfs_add_dead_root(root);
4946 }
4947}
4948
Jeff Mahoney143bede2012-03-01 14:56:26 +01004949void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04004950{
4951 struct rb_node *node;
4952 struct rb_node *prev;
4953 struct btrfs_inode *entry;
4954 struct inode *inode;
4955 u64 objectid = 0;
4956
Liu Bo7813b3d2014-02-10 17:37:25 +08004957 if (!test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
4958 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04004959
4960 spin_lock(&root->inode_lock);
4961again:
4962 node = root->inode_tree.rb_node;
4963 prev = NULL;
4964 while (node) {
4965 prev = node;
4966 entry = rb_entry(node, struct btrfs_inode, rb_node);
4967
Li Zefan33345d012011-04-20 10:31:50 +08004968 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004969 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004970 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004971 node = node->rb_right;
4972 else
4973 break;
4974 }
4975 if (!node) {
4976 while (prev) {
4977 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004978 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004979 node = prev;
4980 break;
4981 }
4982 prev = rb_next(prev);
4983 }
4984 }
4985 while (node) {
4986 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004987 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04004988 inode = igrab(&entry->vfs_inode);
4989 if (inode) {
4990 spin_unlock(&root->inode_lock);
4991 if (atomic_read(&inode->i_count) > 1)
4992 d_prune_aliases(inode);
4993 /*
Al Viro45321ac2010-06-07 13:43:19 -04004994 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04004995 * the inode cache when its usage count
4996 * hits zero.
4997 */
4998 iput(inode);
4999 cond_resched();
5000 spin_lock(&root->inode_lock);
5001 goto again;
5002 }
5003
5004 if (cond_resched_lock(&root->inode_lock))
5005 goto again;
5006
5007 node = rb_next(node);
5008 }
5009 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005010}
5011
Chris Masone02119d2008-09-05 16:13:11 -04005012static int btrfs_init_locked_inode(struct inode *inode, void *p)
5013{
5014 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005015 inode->i_ino = args->location->objectid;
5016 memcpy(&BTRFS_I(inode)->location, args->location,
5017 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005018 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005019 return 0;
5020}
5021
5022static int btrfs_find_actor(struct inode *inode, void *opaque)
5023{
5024 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005025 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005026 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005027}
5028
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005029static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005030 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005031 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005032{
5033 struct inode *inode;
5034 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005035 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005036
Chris Mason90d3e592014-01-09 17:28:00 -08005037 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005038 args.root = root;
5039
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005040 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005041 btrfs_init_locked_inode,
5042 (void *)&args);
5043 return inode;
5044}
5045
Balaji Rao1a54ef82008-07-21 02:01:04 +05305046/* Get an inode object given its location and corresponding root.
5047 * Returns in *is_new if the inode was read from disk
5048 */
5049struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005050 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305051{
5052 struct inode *inode;
5053
Chris Mason90d3e592014-01-09 17:28:00 -08005054 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305055 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005056 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305057
5058 if (inode->i_state & I_NEW) {
Balaji Rao1a54ef82008-07-21 02:01:04 +05305059 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005060 if (!is_bad_inode(inode)) {
5061 inode_tree_add(inode);
5062 unlock_new_inode(inode);
5063 if (new)
5064 *new = 1;
5065 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005066 unlock_new_inode(inode);
5067 iput(inode);
5068 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005069 }
5070 }
5071
Balaji Rao1a54ef82008-07-21 02:01:04 +05305072 return inode;
5073}
5074
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005075static struct inode *new_simple_dir(struct super_block *s,
5076 struct btrfs_key *key,
5077 struct btrfs_root *root)
5078{
5079 struct inode *inode = new_inode(s);
5080
5081 if (!inode)
5082 return ERR_PTR(-ENOMEM);
5083
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005084 BTRFS_I(inode)->root = root;
5085 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005086 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005087
5088 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005089 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005090 inode->i_fop = &simple_dir_operations;
5091 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
5092 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
5093
5094 return inode;
5095}
5096
Chris Mason3de45862008-11-17 21:02:50 -05005097struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005098{
Chris Masond3977122009-01-05 21:25:51 -05005099 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005100 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005101 struct btrfs_root *sub_root = root;
5102 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005103 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005104 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005105
5106 if (dentry->d_name.len > BTRFS_NAME_LEN)
5107 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005108
Jeff Layton39e3c952012-11-28 11:30:53 -05005109 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005110 if (ret < 0)
5111 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005112
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005113 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005114 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005115
5116 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005117 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005118 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005119 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005120
5121 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5122
Yan, Zheng76dda932009-09-21 16:00:26 -04005123 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005124 ret = fixup_tree_root_location(root, dir, dentry,
5125 &location, &sub_root);
5126 if (ret < 0) {
5127 if (ret != -ENOENT)
5128 inode = ERR_PTR(ret);
5129 else
5130 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5131 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005132 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005133 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005134 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
5135
Julia Lawall34d19ba2011-01-24 19:55:19 +00005136 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00005137 down_read(&root->fs_info->cleanup_work_sem);
5138 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005139 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005140 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005141 if (ret) {
5142 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005143 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005144 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005145 }
5146
Chris Mason3de45862008-11-17 21:02:50 -05005147 return inode;
5148}
5149
Nick Pigginfe15ce42011-01-07 17:49:23 +11005150static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005151{
5152 struct btrfs_root *root;
Li Zefan848cce02012-02-21 17:04:28 +08005153 struct inode *inode = dentry->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005154
Li Zefan848cce02012-02-21 17:04:28 +08005155 if (!inode && !IS_ROOT(dentry))
5156 inode = dentry->d_parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005157
Li Zefan848cce02012-02-21 17:04:28 +08005158 if (inode) {
5159 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005160 if (btrfs_root_refs(&root->root_item) == 0)
5161 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005162
5163 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5164 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005165 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005166 return 0;
5167}
5168
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005169static void btrfs_dentry_release(struct dentry *dentry)
5170{
Daeseok Youn944a4512014-04-14 15:37:02 +09005171 kfree(dentry->d_fsdata);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005172}
5173
Chris Mason3de45862008-11-17 21:02:50 -05005174static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005175 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005176{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005177 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005178
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005179 inode = btrfs_lookup_dentry(dir, dentry);
5180 if (IS_ERR(inode)) {
5181 if (PTR_ERR(inode) == -ENOENT)
5182 inode = NULL;
5183 else
5184 return ERR_CAST(inode);
5185 }
5186
Josef Bacik3a0dfa62014-02-14 13:43:48 -05005187 return d_materialise_unique(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005188}
5189
Miao Xie16cdcec2011-04-22 18:12:22 +08005190unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005191 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5192};
5193
Al Viro9cdda8d2013-05-22 16:48:09 -04005194static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005195{
Al Viro9cdda8d2013-05-22 16:48:09 -04005196 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005197 struct btrfs_root *root = BTRFS_I(inode)->root;
5198 struct btrfs_item *item;
5199 struct btrfs_dir_item *di;
5200 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005201 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005202 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005203 struct list_head ins_list;
5204 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005205 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005206 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005207 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005208 unsigned char d_type;
5209 int over = 0;
5210 u32 di_cur;
5211 u32 di_total;
5212 u32 di_len;
5213 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005214 char tmp_name[32];
5215 char *name_ptr;
5216 int name_len;
Al Viro9cdda8d2013-05-22 16:48:09 -04005217 int is_curr = 0; /* ctx->pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04005218
5219 /* FIXME, use a real flag for deciding about the key type */
5220 if (root->fs_info->tree_root == root)
5221 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005222
Al Viro9cdda8d2013-05-22 16:48:09 -04005223 if (!dir_emit_dots(file, ctx))
5224 return 0;
5225
David Woodhouse49593bf2008-08-17 17:08:36 +01005226 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005227 if (!path)
5228 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005229
Josef Bacik026fd312011-05-13 10:32:11 -04005230 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005231
Miao Xie16cdcec2011-04-22 18:12:22 +08005232 if (key_type == BTRFS_DIR_INDEX_KEY) {
5233 INIT_LIST_HEAD(&ins_list);
5234 INIT_LIST_HEAD(&del_list);
5235 btrfs_get_delayed_items(inode, &ins_list, &del_list);
5236 }
5237
Chris Mason39279cc2007-06-12 06:35:45 -04005238 btrfs_set_key_type(&key, key_type);
Al Viro9cdda8d2013-05-22 16:48:09 -04005239 key.offset = ctx->pos;
Li Zefan33345d012011-04-20 10:31:50 +08005240 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005241
Chris Mason39279cc2007-06-12 06:35:45 -04005242 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5243 if (ret < 0)
5244 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005245
5246 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005247 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005248 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005249 if (slot >= btrfs_header_nritems(leaf)) {
5250 ret = btrfs_next_leaf(root, path);
5251 if (ret < 0)
5252 goto err;
5253 else if (ret > 0)
5254 break;
5255 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005256 }
Chris Mason3de45862008-11-17 21:02:50 -05005257
Ross Kirkdd3cc162013-09-16 15:58:09 +01005258 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04005259 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5260
5261 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005262 break;
Chris Mason5f39d392007-10-15 16:14:19 -04005263 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04005264 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005265 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005266 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08005267 if (key_type == BTRFS_DIR_INDEX_KEY &&
5268 btrfs_should_delete_dir_index(&del_list,
5269 found_key.offset))
5270 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005271
Al Viro9cdda8d2013-05-22 16:48:09 -04005272 ctx->pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08005273 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005274
Chris Mason39279cc2007-06-12 06:35:45 -04005275 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
5276 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005277 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01005278
5279 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04005280 struct btrfs_key location;
5281
Josef Bacik22a94d42011-03-16 16:47:17 -04005282 if (verify_dir_item(root, leaf, di))
5283 break;
5284
Chris Mason5f39d392007-10-15 16:14:19 -04005285 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01005286 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04005287 name_ptr = tmp_name;
5288 } else {
5289 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01005290 if (!name_ptr) {
5291 ret = -ENOMEM;
5292 goto err;
5293 }
Chris Mason5f39d392007-10-15 16:14:19 -04005294 }
5295 read_extent_buffer(leaf, name_ptr,
5296 (unsigned long)(di + 1), name_len);
5297
5298 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5299 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05005300
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005301
Chris Mason3de45862008-11-17 21:02:50 -05005302 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01005303 * skip it.
5304 *
5305 * In contrast to old kernels, we insert the snapshot's
5306 * dir item and dir index after it has been created, so
5307 * we won't find a reference to our own snapshot. We
5308 * still keep the following code for backward
5309 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05005310 */
5311 if (location.type == BTRFS_ROOT_ITEM_KEY &&
5312 location.objectid == root->root_key.objectid) {
5313 over = 0;
5314 goto skip;
5315 }
Al Viro9cdda8d2013-05-22 16:48:09 -04005316 over = !dir_emit(ctx, name_ptr, name_len,
5317 location.objectid, d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04005318
Chris Mason3de45862008-11-17 21:02:50 -05005319skip:
Chris Mason5f39d392007-10-15 16:14:19 -04005320 if (name_ptr != tmp_name)
5321 kfree(name_ptr);
5322
Chris Mason39279cc2007-06-12 06:35:45 -04005323 if (over)
5324 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05005325 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01005326 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04005327 di_cur += di_len;
5328 di = (struct btrfs_dir_item *)((char *)di + di_len);
5329 }
Li Zefanb9e03af2011-03-23 10:43:58 +08005330next:
5331 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005332 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005333
Miao Xie16cdcec2011-04-22 18:12:22 +08005334 if (key_type == BTRFS_DIR_INDEX_KEY) {
5335 if (is_curr)
Al Viro9cdda8d2013-05-22 16:48:09 -04005336 ctx->pos++;
5337 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005338 if (ret)
5339 goto nopos;
5340 }
5341
David Woodhouse49593bf2008-08-17 17:08:36 +01005342 /* Reached end of directory/root. Bump pos past the last item. */
Zach Browndb62efb2013-07-11 16:19:42 -07005343 ctx->pos++;
5344
5345 /*
5346 * Stop new entries from being returned after we return the last
5347 * entry.
5348 *
5349 * New directory entries are assigned a strictly increasing
5350 * offset. This means that new entries created during readdir
5351 * are *guaranteed* to be seen in the future by that readdir.
5352 * This has broken buggy programs which operate on names as
5353 * they're returned by readdir. Until we re-use freed offsets
5354 * we have this hack to stop new entries from being returned
5355 * under the assumption that they'll never reach this huge
5356 * offset.
5357 *
5358 * This is being careful not to overflow 32bit loff_t unless the
5359 * last entry requires it because doing so has broken 32bit apps
5360 * in the past.
5361 */
5362 if (key_type == BTRFS_DIR_INDEX_KEY) {
5363 if (ctx->pos >= INT_MAX)
5364 ctx->pos = LLONG_MAX;
5365 else
5366 ctx->pos = INT_MAX;
5367 }
Chris Mason39279cc2007-06-12 06:35:45 -04005368nopos:
5369 ret = 0;
5370err:
Miao Xie16cdcec2011-04-22 18:12:22 +08005371 if (key_type == BTRFS_DIR_INDEX_KEY)
5372 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005373 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005374 return ret;
5375}
5376
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005377int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005378{
5379 struct btrfs_root *root = BTRFS_I(inode)->root;
5380 struct btrfs_trans_handle *trans;
5381 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005382 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005383
Josef Bacik72ac3c02012-05-23 14:13:11 -04005384 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005385 return 0;
5386
Liu Bo83eea1f2012-07-10 05:28:39 -06005387 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08005388 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005389
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005390 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005391 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005392 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005393 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005394 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005395 if (IS_ERR(trans))
5396 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06005397 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005398 }
5399 return ret;
5400}
5401
5402/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005403 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005404 * inode changes. But, it is most likely to find the inode in cache.
5405 * FIXME, needs more benchmarking...there are no reasons other than performance
5406 * to keep or drop this code.
5407 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005408static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005409{
5410 struct btrfs_root *root = BTRFS_I(inode)->root;
5411 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005412 int ret;
5413
Josef Bacik72ac3c02012-05-23 14:13:11 -04005414 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005415 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005416
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005417 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005418 if (IS_ERR(trans))
5419 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005420
5421 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005422 if (ret && ret == -ENOSPC) {
5423 /* whoops, lets try again with the full transaction */
5424 btrfs_end_transaction(trans, root);
5425 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005426 if (IS_ERR(trans))
5427 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005428
Chris Mason94b60442010-05-26 11:02:00 -04005429 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005430 }
Chris Mason39279cc2007-06-12 06:35:45 -04005431 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08005432 if (BTRFS_I(inode)->delayed_node)
5433 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005434
5435 return ret;
5436}
5437
5438/*
5439 * This is a copy of file_update_time. We need this so we can return error on
5440 * ENOSPC for updating the inode in the case of file write and mmap writes.
5441 */
Josef Bacike41f9412012-03-26 09:46:47 -04005442static int btrfs_update_time(struct inode *inode, struct timespec *now,
5443 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005444{
Alexander Block2bc5565282012-06-15 09:49:33 +02005445 struct btrfs_root *root = BTRFS_I(inode)->root;
5446
5447 if (btrfs_root_readonly(root))
5448 return -EROFS;
5449
Josef Bacike41f9412012-03-26 09:46:47 -04005450 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005451 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04005452 if (flags & S_CTIME)
5453 inode->i_ctime = *now;
5454 if (flags & S_MTIME)
5455 inode->i_mtime = *now;
5456 if (flags & S_ATIME)
5457 inode->i_atime = *now;
5458 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005459}
5460
Chris Masond352ac62008-09-29 15:18:18 -04005461/*
5462 * find the highest existing sequence number in a directory
5463 * and then set the in-memory index_cnt variable to reflect
5464 * free sequence numbers
5465 */
Josef Bacikaec74772008-07-24 12:12:38 -04005466static int btrfs_set_inode_index_count(struct inode *inode)
5467{
5468 struct btrfs_root *root = BTRFS_I(inode)->root;
5469 struct btrfs_key key, found_key;
5470 struct btrfs_path *path;
5471 struct extent_buffer *leaf;
5472 int ret;
5473
Li Zefan33345d012011-04-20 10:31:50 +08005474 key.objectid = btrfs_ino(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005475 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
5476 key.offset = (u64)-1;
5477
5478 path = btrfs_alloc_path();
5479 if (!path)
5480 return -ENOMEM;
5481
5482 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5483 if (ret < 0)
5484 goto out;
5485 /* FIXME: we should be able to handle this */
5486 if (ret == 0)
5487 goto out;
5488 ret = 0;
5489
5490 /*
5491 * MAGIC NUMBER EXPLANATION:
5492 * since we search a directory based on f_pos we have to start at 2
5493 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5494 * else has to start at 2
5495 */
5496 if (path->slots[0] == 0) {
5497 BTRFS_I(inode)->index_cnt = 2;
5498 goto out;
5499 }
5500
5501 path->slots[0]--;
5502
5503 leaf = path->nodes[0];
5504 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5505
Li Zefan33345d012011-04-20 10:31:50 +08005506 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacikaec74772008-07-24 12:12:38 -04005507 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
5508 BTRFS_I(inode)->index_cnt = 2;
5509 goto out;
5510 }
5511
5512 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
5513out:
5514 btrfs_free_path(path);
5515 return ret;
5516}
5517
Chris Masond352ac62008-09-29 15:18:18 -04005518/*
5519 * helper to find a free sequence number in a given directory. This current
5520 * code is very simple, later versions will do smarter things in the btree
5521 */
Chris Mason3de45862008-11-17 21:02:50 -05005522int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04005523{
5524 int ret = 0;
5525
5526 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08005527 ret = btrfs_inode_delayed_dir_index_count(dir);
5528 if (ret) {
5529 ret = btrfs_set_inode_index_count(dir);
5530 if (ret)
5531 return ret;
5532 }
Josef Bacikaec74772008-07-24 12:12:38 -04005533 }
5534
Chris Mason00e4e6b2008-08-05 11:18:09 -04005535 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04005536 BTRFS_I(dir)->index_cnt++;
5537
5538 return ret;
5539}
5540
Chris Mason39279cc2007-06-12 06:35:45 -04005541static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5542 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04005543 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05005544 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04005545 u64 ref_objectid, u64 objectid,
5546 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04005547{
5548 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005549 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04005550 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04005551 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05005552 struct btrfs_inode_ref *ref;
5553 struct btrfs_key key[2];
5554 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005555 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05005556 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04005557 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005558
Chris Mason5f39d392007-10-15 16:14:19 -04005559 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005560 if (!path)
5561 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04005562
Chris Mason39279cc2007-06-12 06:35:45 -04005563 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005564 if (!inode) {
5565 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005566 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005567 }
Chris Mason39279cc2007-06-12 06:35:45 -04005568
Li Zefan581bb052011-04-20 10:06:11 +08005569 /*
5570 * we have to initialize this early, so we can reclaim the inode
5571 * number if we fail afterwards in this function.
5572 */
5573 inode->i_ino = objectid;
5574
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005575 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00005576 trace_btrfs_inode_request(dir);
5577
Chris Mason3de45862008-11-17 21:02:50 -05005578 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04005579 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005580 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005581 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005582 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04005583 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005584 } else if (dir) {
5585 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04005586 }
5587 /*
5588 * index_cnt is ignored for everything but a dir,
5589 * btrfs_get_inode_index_count has an explanation for the magic
5590 * number
5591 */
5592 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08005593 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04005594 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04005595 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00005596 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04005597
Josef Bacik5dc562c2012-08-17 13:14:17 -04005598 /*
5599 * We could have gotten an inode number from somebody who was fsynced
5600 * and then removed in this same transaction, so let's just set full
5601 * sync since it will be a full sync anyway and this will blow away the
5602 * old info in the log.
5603 */
5604 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
5605
Chris Mason9c583092008-01-29 15:15:18 -05005606 key[0].objectid = objectid;
5607 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
5608 key[0].offset = 0;
5609
Chris Mason9c583092008-01-29 15:15:18 -05005610 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005611
5612 if (name) {
5613 /*
5614 * Start new inodes with an inode_ref. This is slightly more
5615 * efficient for small numbers of hard links since they will
5616 * be packed into one item. Extended refs will kick in if we
5617 * add more hard links than can fit in the ref item.
5618 */
5619 key[1].objectid = objectid;
5620 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
5621 key[1].offset = ref_objectid;
5622
5623 sizes[1] = name_len + sizeof(*ref);
5624 }
Chris Mason9c583092008-01-29 15:15:18 -05005625
Chris Masonb9473432009-03-13 11:00:37 -04005626 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005627 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05005628 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04005629 goto fail;
5630
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03005631 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04005632 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005633 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04005634 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
5635 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06005636 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
5637 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04005638 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05005639
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005640 if (name) {
5641 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
5642 struct btrfs_inode_ref);
5643 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
5644 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
5645 ptr = (unsigned long)(ref + 1);
5646 write_extent_buffer(path->nodes[0], name, ptr, name_len);
5647 }
Chris Mason9c583092008-01-29 15:15:18 -05005648
Chris Mason5f39d392007-10-15 16:14:19 -04005649 btrfs_mark_buffer_dirty(path->nodes[0]);
5650 btrfs_free_path(path);
5651
Chris Mason39279cc2007-06-12 06:35:45 -04005652 location = &BTRFS_I(inode)->location;
5653 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04005654 location->offset = 0;
5655 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
5656
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005657 btrfs_inherit_iflags(inode, dir);
5658
Al Viro569254b2011-07-24 17:08:40 -04005659 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04005660 if (btrfs_test_opt(root, NODATASUM))
5661 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo213490b2012-09-11 08:33:50 -06005662 if (btrfs_test_opt(root, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05005663 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
5664 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04005665 }
5666
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005667 btrfs_insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005668 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00005669
5670 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04005671 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00005672
Alexander Block8ea05e32012-07-25 17:35:53 +02005673 btrfs_update_root_times(trans, root);
5674
Filipe David Borba Manana63541922014-01-07 11:47:46 +00005675 ret = btrfs_inode_inherit_props(trans, inode, dir);
5676 if (ret)
5677 btrfs_err(root->fs_info,
5678 "error inheriting props for ino %llu (root %llu): %d",
5679 btrfs_ino(inode), root->root_key.objectid, ret);
5680
Chris Mason39279cc2007-06-12 06:35:45 -04005681 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005682fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005683 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04005684 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04005685 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005686 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005687 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005688}
5689
5690static inline u8 btrfs_inode_type(struct inode *inode)
5691{
5692 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
5693}
5694
Chris Masond352ac62008-09-29 15:18:18 -04005695/*
5696 * utility function to add 'inode' into 'parent_inode' with
5697 * a give name and a given sequence number.
5698 * if 'add_backref' is true, also insert a backref from the
5699 * inode to the parent directory.
5700 */
Chris Masone02119d2008-09-05 16:13:11 -04005701int btrfs_add_link(struct btrfs_trans_handle *trans,
5702 struct inode *parent_inode, struct inode *inode,
5703 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005704{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005705 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005706 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04005707 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08005708 u64 ino = btrfs_ino(inode);
5709 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005710
Li Zefan33345d012011-04-20 10:31:50 +08005711 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005712 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
5713 } else {
Li Zefan33345d012011-04-20 10:31:50 +08005714 key.objectid = ino;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005715 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
5716 key.offset = 0;
5717 }
Chris Mason39279cc2007-06-12 06:35:45 -04005718
Li Zefan33345d012011-04-20 10:31:50 +08005719 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005720 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
5721 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08005722 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005723 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08005724 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
5725 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005726 }
5727
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005728 /* Nothing to clean up yet */
5729 if (ret)
5730 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005731
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005732 ret = btrfs_insert_dir_item(trans, root, name, name_len,
5733 parent_inode, &key,
5734 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05005735 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005736 goto fail_dir_item;
5737 else if (ret) {
5738 btrfs_abort_transaction(trans, root, ret);
5739 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005740 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005741
5742 btrfs_i_size_write(parent_inode, parent_inode->i_size +
5743 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005744 inode_inc_iversion(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005745 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
5746 ret = btrfs_update_inode(trans, root, parent_inode);
5747 if (ret)
5748 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005749 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05005750
5751fail_dir_item:
5752 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
5753 u64 local_index;
5754 int err;
5755 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
5756 key.objectid, root->root_key.objectid,
5757 parent_ino, &local_index, name, name_len);
5758
5759 } else if (add_backref) {
5760 u64 local_index;
5761 int err;
5762
5763 err = btrfs_del_inode_ref(trans, root, name, name_len,
5764 ino, parent_ino, &local_index);
5765 }
5766 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005767}
5768
5769static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00005770 struct inode *dir, struct dentry *dentry,
5771 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005772{
Josef Bacika1b075d2010-11-19 20:36:11 +00005773 int err = btrfs_add_link(trans, dir, inode,
5774 dentry->d_name.name, dentry->d_name.len,
5775 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005776 if (err > 0)
5777 err = -EEXIST;
5778 return err;
5779}
5780
Josef Bacik618e21d2007-07-11 10:18:17 -04005781static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04005782 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04005783{
5784 struct btrfs_trans_handle *trans;
5785 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005786 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04005787 int err;
5788 int drop_inode = 0;
5789 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005790 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04005791
5792 if (!new_valid_dev(rdev))
5793 return -EINVAL;
5794
Josef Bacik9ed74f22009-09-11 16:12:44 -04005795 /*
5796 * 2 for inode item and ref
5797 * 2 for dir items
5798 * 1 for xattr if selinux is on
5799 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005800 trans = btrfs_start_transaction(root, 5);
5801 if (IS_ERR(trans))
5802 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05005803
Li Zefan581bb052011-04-20 10:06:11 +08005804 err = btrfs_find_free_ino(root, &objectid);
5805 if (err)
5806 goto out_unlock;
5807
Josef Bacikaec74772008-07-24 12:12:38 -04005808 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005809 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005810 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005811 if (IS_ERR(inode)) {
5812 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005813 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005814 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005815
Eric Paris2a7dba32011-02-01 11:05:39 -05005816 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005817 if (err) {
5818 drop_inode = 1;
5819 goto out_unlock;
5820 }
5821
Casey Schauflerad19db72011-12-15 10:09:07 -05005822 /*
5823 * If the active LSM wants to access the inode during
5824 * d_instantiate it needs these. Smack checks to see
5825 * if the filesystem supports xattrs by looking at the
5826 * ops vector.
5827 */
5828
5829 inode->i_op = &btrfs_special_inode_operations;
Josef Bacika1b075d2010-11-19 20:36:11 +00005830 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04005831 if (err)
5832 drop_inode = 1;
5833 else {
Josef Bacik618e21d2007-07-11 10:18:17 -04005834 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04005835 btrfs_update_inode(trans, root, inode);
Al Viro08c422c2011-12-23 07:58:13 -05005836 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005837 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005838out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005839 btrfs_end_transaction(trans, root);
Josef Bacikc581afc2014-02-06 16:06:06 -05005840 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00005841 btrfs_btree_balance_dirty(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04005842 if (drop_inode) {
5843 inode_dec_link_count(inode);
5844 iput(inode);
5845 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005846 return err;
5847}
5848
Chris Mason39279cc2007-06-12 06:35:45 -04005849static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04005850 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04005851{
5852 struct btrfs_trans_handle *trans;
5853 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005854 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005855 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04005856 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04005857 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005858 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005859
Josef Bacik9ed74f22009-09-11 16:12:44 -04005860 /*
5861 * 2 for inode item and ref
5862 * 2 for dir items
5863 * 1 for xattr if selinux is on
5864 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005865 trans = btrfs_start_transaction(root, 5);
5866 if (IS_ERR(trans))
5867 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005868
Li Zefan581bb052011-04-20 10:06:11 +08005869 err = btrfs_find_free_ino(root, &objectid);
5870 if (err)
5871 goto out_unlock;
5872
Josef Bacikaec74772008-07-24 12:12:38 -04005873 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005874 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005875 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005876 if (IS_ERR(inode)) {
5877 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005878 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005879 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005880 drop_inode_on_err = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04005881
Eric Paris2a7dba32011-02-01 11:05:39 -05005882 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005883 if (err)
Josef Bacik33268ea2008-07-24 12:16:36 -04005884 goto out_unlock;
Josef Bacik33268ea2008-07-24 12:16:36 -04005885
Filipe Brandenburger9185aa52012-11-30 03:40:08 +00005886 err = btrfs_update_inode(trans, root, inode);
5887 if (err)
5888 goto out_unlock;
5889
Casey Schauflerad19db72011-12-15 10:09:07 -05005890 /*
5891 * If the active LSM wants to access the inode during
5892 * d_instantiate it needs these. Smack checks to see
5893 * if the filesystem supports xattrs by looking at the
5894 * ops vector.
5895 */
5896 inode->i_fop = &btrfs_file_operations;
5897 inode->i_op = &btrfs_file_inode_operations;
5898
Josef Bacika1b075d2010-11-19 20:36:11 +00005899 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005900 if (err)
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005901 goto out_unlock;
5902
5903 inode->i_mapping->a_ops = &btrfs_aops;
5904 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
5905 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
5906 d_instantiate(dentry, inode);
5907
Chris Mason39279cc2007-06-12 06:35:45 -04005908out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005909 btrfs_end_transaction(trans, root);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005910 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005911 inode_dec_link_count(inode);
5912 iput(inode);
5913 }
Josef Bacikc581afc2014-02-06 16:06:06 -05005914 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00005915 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005916 return err;
5917}
5918
5919static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
5920 struct dentry *dentry)
5921{
5922 struct btrfs_trans_handle *trans;
5923 struct btrfs_root *root = BTRFS_I(dir)->root;
5924 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005925 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04005926 int err;
5927 int drop_inode = 0;
5928
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005929 /* do not allow sys_link's with other subvols of the same device */
5930 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00005931 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005932
Mark Fashehf1863732012-08-08 11:32:27 -07005933 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00005934 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04005935
Chris Mason3de45862008-11-17 21:02:50 -05005936 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04005937 if (err)
5938 goto fail;
5939
Yan, Zhenga22285a2010-05-16 10:48:46 -04005940 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00005941 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04005942 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00005943 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04005944 */
Miao Xie7e6b6462011-02-18 09:21:17 +00005945 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005946 if (IS_ERR(trans)) {
5947 err = PTR_ERR(trans);
5948 goto fail;
5949 }
Chris Mason5f39d392007-10-15 16:14:19 -04005950
Miao Xie67de1172013-12-26 13:07:06 +08005951 /* There are several dir indexes for this inode, clear the cache. */
5952 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07005953 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005954 inode_inc_iversion(inode);
Miao Xie31534952011-04-13 13:19:21 +08005955 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6ee2010-10-23 11:11:40 -04005956 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04005957 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04005958
Josef Bacika1b075d2010-11-19 20:36:11 +00005959 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04005960
Yan, Zhenga5719522009-09-24 09:17:31 -04005961 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005962 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04005963 } else {
Al Viro10d9f302011-07-16 23:09:10 -04005964 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04005965 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005966 if (err)
5967 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005968 if (inode->i_nlink == 1) {
5969 /*
5970 * If new hard link count is 1, it's a file created
5971 * with open(2) O_TMPFILE flag.
5972 */
5973 err = btrfs_orphan_del(trans, inode);
5974 if (err)
5975 goto fail;
5976 }
Al Viro08c422c2011-12-23 07:58:13 -05005977 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00005978 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04005979 }
Chris Mason39279cc2007-06-12 06:35:45 -04005980
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005981 btrfs_end_transaction(trans, root);
Josef Bacikc581afc2014-02-06 16:06:06 -05005982 btrfs_balance_delayed_items(root);
Chris Mason1832a6d2007-12-21 16:27:21 -05005983fail:
Chris Mason39279cc2007-06-12 06:35:45 -04005984 if (drop_inode) {
5985 inode_dec_link_count(inode);
5986 iput(inode);
5987 }
Liu Bob53d3f52012-11-14 14:34:34 +00005988 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005989 return err;
5990}
5991
Al Viro18bb1db2011-07-26 01:41:39 -04005992static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04005993{
Chris Masonb9d86662008-05-02 16:13:49 -04005994 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04005995 struct btrfs_trans_handle *trans;
5996 struct btrfs_root *root = BTRFS_I(dir)->root;
5997 int err = 0;
5998 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04005999 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006000 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006001
Josef Bacik9ed74f22009-09-11 16:12:44 -04006002 /*
6003 * 2 items for inode and ref
6004 * 2 items for dir items
6005 * 1 for xattr if selinux is on
6006 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006007 trans = btrfs_start_transaction(root, 5);
6008 if (IS_ERR(trans))
6009 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006010
Li Zefan581bb052011-04-20 10:06:11 +08006011 err = btrfs_find_free_ino(root, &objectid);
6012 if (err)
6013 goto out_fail;
6014
Josef Bacikaec74772008-07-24 12:12:38 -04006015 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006016 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04006017 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006018 if (IS_ERR(inode)) {
6019 err = PTR_ERR(inode);
6020 goto out_fail;
6021 }
Chris Mason5f39d392007-10-15 16:14:19 -04006022
Chris Mason39279cc2007-06-12 06:35:45 -04006023 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04006024
Eric Paris2a7dba32011-02-01 11:05:39 -05006025 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006026 if (err)
6027 goto out_fail;
6028
Chris Mason39279cc2007-06-12 06:35:45 -04006029 inode->i_op = &btrfs_dir_inode_operations;
6030 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04006031
Chris Masondbe674a2008-07-17 12:54:05 -04006032 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006033 err = btrfs_update_inode(trans, root, inode);
6034 if (err)
6035 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006036
Josef Bacika1b075d2010-11-19 20:36:11 +00006037 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
6038 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006039 if (err)
6040 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006041
Chris Mason39279cc2007-06-12 06:35:45 -04006042 d_instantiate(dentry, inode);
6043 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006044
6045out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006046 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04006047 if (drop_on_err)
6048 iput(inode);
Josef Bacikc581afc2014-02-06 16:06:06 -05006049 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006050 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006051 return err;
6052}
6053
Chris Masond352ac62008-09-29 15:18:18 -04006054/* helper for btfs_get_extent. Given an existing extent in the tree,
6055 * and an extent that you want to insert, deal with overlap and insert
6056 * the new extent into the tree.
6057 */
Chris Mason3b951512008-04-17 11:29:12 -04006058static int merge_extent_mapping(struct extent_map_tree *em_tree,
6059 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006060 struct extent_map *em,
6061 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04006062{
6063 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04006064
Chris Masone6dcd2d2008-07-17 12:53:50 -04006065 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
6066 start_diff = map_start - em->start;
6067 em->start = map_start;
6068 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04006069 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
6070 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006071 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04006072 em->block_len -= start_diff;
6073 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006074 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006075}
6076
Chris Masonc8b97812008-10-29 14:49:59 -04006077static noinline int uncompress_inline(struct btrfs_path *path,
6078 struct inode *inode, struct page *page,
6079 size_t pg_offset, u64 extent_offset,
6080 struct btrfs_file_extent_item *item)
6081{
6082 int ret;
6083 struct extent_buffer *leaf = path->nodes[0];
6084 char *tmp;
6085 size_t max_size;
6086 unsigned long inline_size;
6087 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006088 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006089
6090 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006091 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006092 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6093 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006094 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006095 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006096 if (!tmp)
6097 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006098 ptr = btrfs_file_extent_inline_start(item);
6099
6100 read_extent_buffer(leaf, tmp, ptr, inline_size);
6101
Chris Mason5b050f02008-11-11 09:34:41 -05006102 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006103 ret = btrfs_decompress(compress_type, tmp, page,
6104 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006105 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006106 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006107}
6108
Chris Masond352ac62008-09-29 15:18:18 -04006109/*
6110 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006111 * the ugly parts come from merging extents from the disk with the in-ram
6112 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006113 * where the in-ram extents might be locked pending data=ordered completion.
6114 *
6115 * This also copies inline extents directly into the page.
6116 */
Chris Masond3977122009-01-05 21:25:51 -05006117
Chris Masona52d9a82007-08-27 16:49:44 -04006118struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05006119 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04006120 int create)
6121{
6122 int ret;
6123 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04006124 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04006125 u64 extent_start = 0;
6126 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08006127 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006128 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006129 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04006130 struct btrfs_root *root = BTRFS_I(inode)->root;
6131 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006132 struct extent_buffer *leaf;
6133 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006134 struct extent_map *em = NULL;
6135 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05006136 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006137 struct btrfs_trans_handle *trans = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08006138 int compress_type;
Chris Masona52d9a82007-08-27 16:49:44 -04006139
Chris Masona52d9a82007-08-27 16:49:44 -04006140again:
Chris Mason890871b2009-09-02 16:24:52 -04006141 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006142 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006143 if (em)
6144 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006145 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006146
Chris Masona52d9a82007-08-27 16:49:44 -04006147 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006148 if (em->start > start || em->start + em->len <= start)
6149 free_extent_map(em);
6150 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006151 free_extent_map(em);
6152 else
6153 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006154 }
David Sterba172ddd62011-04-21 00:48:27 +02006155 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006156 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006157 err = -ENOMEM;
6158 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006159 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006160 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006161 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006162 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006163 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006164 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006165
6166 if (!path) {
6167 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006168 if (!path) {
6169 err = -ENOMEM;
6170 goto out;
6171 }
6172 /*
6173 * Chances are we'll be called again, so go ahead and do
6174 * readahead
6175 */
6176 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04006177 }
6178
Chris Mason179e29e2007-11-01 11:28:41 -04006179 ret = btrfs_lookup_file_extent(trans, root, path,
6180 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006181 if (ret < 0) {
6182 err = ret;
6183 goto out;
6184 }
6185
6186 if (ret != 0) {
6187 if (path->slots[0] == 0)
6188 goto not_found;
6189 path->slots[0]--;
6190 }
6191
Chris Mason5f39d392007-10-15 16:14:19 -04006192 leaf = path->nodes[0];
6193 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006194 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006195 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006196 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6197 found_type = btrfs_key_type(&found_key);
6198 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006199 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006200 /*
6201 * If we backup past the first extent we want to move forward
6202 * and see if there is an extent in front of us, otherwise we'll
6203 * say there is a hole for our whole search range which can
6204 * cause problems.
6205 */
6206 extent_end = start;
6207 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006208 }
6209
Chris Mason5f39d392007-10-15 16:14:19 -04006210 found_type = btrfs_file_extent_type(leaf, item);
6211 extent_start = found_key.offset;
Li Zefan261507a02010-12-17 14:21:50 +08006212 compress_type = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04006213 if (found_type == BTRFS_FILE_EXTENT_REG ||
6214 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006215 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006216 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006217 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6218 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08006219 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Qu Wenruofda28322013-02-26 08:10:22 +00006220 extent_end = ALIGN(extent_start + size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04006221 }
Josef Bacik25a50342013-10-14 12:08:38 -04006222next:
Yan Zheng9036c102008-10-30 14:19:41 -04006223 if (start >= extent_end) {
6224 path->slots[0]++;
6225 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6226 ret = btrfs_next_leaf(root, path);
6227 if (ret < 0) {
6228 err = ret;
6229 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006230 }
Yan Zheng9036c102008-10-30 14:19:41 -04006231 if (ret > 0)
6232 goto not_found;
6233 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006234 }
Yan Zheng9036c102008-10-30 14:19:41 -04006235 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6236 if (found_key.objectid != objectid ||
6237 found_key.type != BTRFS_EXTENT_DATA_KEY)
6238 goto not_found;
6239 if (start + len <= found_key.offset)
6240 goto not_found;
6241 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006242 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006243 em->len = found_key.offset - start;
6244 goto not_found_em;
6245 }
6246
Josef Bacikcc95bef2013-04-04 14:31:27 -04006247 em->ram_bytes = btrfs_file_extent_ram_bytes(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04006248 if (found_type == BTRFS_FILE_EXTENT_REG ||
6249 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04006250 em->start = extent_start;
6251 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05006252 em->orig_start = extent_start -
6253 btrfs_file_extent_offset(leaf, item);
Josef Bacikb4939682012-12-03 10:31:19 -05006254 em->orig_block_len = btrfs_file_extent_disk_num_bytes(leaf,
6255 item);
Chris Masondb945352007-10-15 16:15:53 -04006256 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
6257 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04006258 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04006259 goto insert;
6260 }
Li Zefan261507a02010-12-17 14:21:50 +08006261 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006262 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08006263 em->compress_type = compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006264 em->block_start = bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05006265 em->block_len = em->orig_block_len;
Chris Masonc8b97812008-10-29 14:49:59 -04006266 } else {
6267 bytenr += btrfs_file_extent_offset(leaf, item);
6268 em->block_start = bytenr;
6269 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04006270 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
6271 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04006272 }
Chris Masona52d9a82007-08-27 16:49:44 -04006273 goto insert;
6274 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006275 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006276 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006277 size_t size;
6278 size_t extent_offset;
6279 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006280
Chris Masona52d9a82007-08-27 16:49:44 -04006281 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04006282 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04006283 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04006284 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04006285 goto out;
6286 }
6287
Chris Mason514ac8a2014-01-03 21:07:00 -08006288 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04006289 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05006290 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04006291 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006292 em->start = extent_start + extent_offset;
Qu Wenruofda28322013-02-26 08:10:22 +00006293 em->len = ALIGN(copy_size, root->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006294 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006295 em->orig_start = em->start;
Li Zefan261507a02010-12-17 14:21:50 +08006296 if (compress_type) {
Chris Masonc8b97812008-10-29 14:49:59 -04006297 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08006298 em->compress_type = compress_type;
6299 }
Yan689f9342007-10-29 11:41:07 -04006300 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006301 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006302 if (btrfs_file_extent_compression(leaf, item) !=
6303 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006304 ret = uncompress_inline(path, inode, page,
6305 pg_offset,
6306 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04006307 if (ret) {
6308 err = ret;
6309 goto out;
6310 }
Chris Masonc8b97812008-10-29 14:49:59 -04006311 } else {
6312 map = kmap(page);
6313 read_extent_buffer(leaf, map + pg_offset, ptr,
6314 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04006315 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
6316 memset(map + pg_offset + copy_size, 0,
6317 PAGE_CACHE_SIZE - pg_offset -
6318 copy_size);
6319 }
Chris Masonc8b97812008-10-29 14:49:59 -04006320 kunmap(page);
6321 }
Chris Mason179e29e2007-11-01 11:28:41 -04006322 flush_dcache_page(page);
6323 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006324 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006325 if (!trans) {
6326 kunmap(page);
6327 free_extent_map(em);
6328 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006329
David Sterbab3b4aa72011-04-21 01:20:15 +02006330 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006331 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006332
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006333 if (IS_ERR(trans))
6334 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006335 goto again;
6336 }
Chris Masonc8b97812008-10-29 14:49:59 -04006337 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006338 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006339 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006340 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006341 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006342 }
Chris Masond1310b22008-01-24 16:13:08 -05006343 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006344 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006345 goto insert;
6346 } else {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00006347 WARN(1, KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04006348 }
6349not_found:
6350 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006351 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006352 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006353not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04006354 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04006355 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006356insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006357 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006358 if (em->start > start || extent_map_end(em) <= start) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006359 btrfs_err(root->fs_info, "bad extent! em: [%llu %llu] passed [%llu %llu]",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006360 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04006361 err = -EIO;
6362 goto out;
6363 }
Chris Masond1310b22008-01-24 16:13:08 -05006364
6365 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006366 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006367 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006368 /* it is possible that someone inserted the extent into the tree
6369 * while we had the lock dropped. It is also possible that
6370 * an overlapping map exists in the tree
6371 */
Chris Masona52d9a82007-08-27 16:49:44 -04006372 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04006373 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006374
6375 ret = 0;
6376
Chris Mason3b951512008-04-17 11:29:12 -04006377 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04006378 if (existing && (existing->start > start ||
6379 existing->start + existing->len <= start)) {
6380 free_extent_map(existing);
6381 existing = NULL;
6382 }
Chris Mason3b951512008-04-17 11:29:12 -04006383 if (!existing) {
6384 existing = lookup_extent_mapping(em_tree, em->start,
6385 em->len);
6386 if (existing) {
6387 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006388 em, start,
6389 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04006390 free_extent_map(existing);
6391 if (err) {
6392 free_extent_map(em);
6393 em = NULL;
6394 }
6395 } else {
6396 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04006397 free_extent_map(em);
6398 em = NULL;
6399 }
6400 } else {
6401 free_extent_map(em);
6402 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006403 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006404 }
Chris Masona52d9a82007-08-27 16:49:44 -04006405 }
Chris Mason890871b2009-09-02 16:24:52 -04006406 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006407out:
liubo1abe9b82011-03-24 11:18:59 +00006408
Steven Rostedt4cd85872013-11-14 22:57:29 -05006409 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00006410
Chris Masonf4219502008-07-22 11:18:09 -04006411 if (path)
6412 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04006413 if (trans) {
6414 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05006415 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04006416 err = ret;
6417 }
Chris Masona52d9a82007-08-27 16:49:44 -04006418 if (err) {
6419 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04006420 return ERR_PTR(err);
6421 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006422 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04006423 return em;
6424}
6425
Chris Masonec29ed52011-02-23 16:23:20 -05006426struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
6427 size_t pg_offset, u64 start, u64 len,
6428 int create)
6429{
6430 struct extent_map *em;
6431 struct extent_map *hole_em = NULL;
6432 u64 range_start = start;
6433 u64 end;
6434 u64 found;
6435 u64 found_end;
6436 int err = 0;
6437
6438 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
6439 if (IS_ERR(em))
6440 return em;
6441 if (em) {
6442 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00006443 * if our em maps to
6444 * - a hole or
6445 * - a pre-alloc extent,
6446 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05006447 */
Liu Bof9e4fb52013-01-07 10:10:12 +00006448 if (em->block_start != EXTENT_MAP_HOLE &&
6449 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05006450 return em;
6451 else
6452 hole_em = em;
6453 }
6454
6455 /* check to see if we've wrapped (len == -1 or similar) */
6456 end = start + len;
6457 if (end < start)
6458 end = (u64)-1;
6459 else
6460 end -= 1;
6461
6462 em = NULL;
6463
6464 /* ok, we didn't find anything, lets look for delalloc */
6465 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
6466 end, len, EXTENT_DELALLOC, 1);
6467 found_end = range_start + found;
6468 if (found_end < range_start)
6469 found_end = (u64)-1;
6470
6471 /*
6472 * we didn't find anything useful, return
6473 * the original results from get_extent()
6474 */
6475 if (range_start > end || found_end <= start) {
6476 em = hole_em;
6477 hole_em = NULL;
6478 goto out;
6479 }
6480
6481 /* adjust the range_start to make sure it doesn't
6482 * go backwards from the start they passed in
6483 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05306484 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05006485 found = found_end - range_start;
6486
6487 if (found > 0) {
6488 u64 hole_start = start;
6489 u64 hole_len = len;
6490
David Sterba172ddd62011-04-21 00:48:27 +02006491 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05006492 if (!em) {
6493 err = -ENOMEM;
6494 goto out;
6495 }
6496 /*
6497 * when btrfs_get_extent can't find anything it
6498 * returns one huge hole
6499 *
6500 * make sure what it found really fits our range, and
6501 * adjust to make sure it is based on the start from
6502 * the caller
6503 */
6504 if (hole_em) {
6505 u64 calc_end = extent_map_end(hole_em);
6506
6507 if (calc_end <= start || (hole_em->start > end)) {
6508 free_extent_map(hole_em);
6509 hole_em = NULL;
6510 } else {
6511 hole_start = max(hole_em->start, start);
6512 hole_len = calc_end - hole_start;
6513 }
6514 }
6515 em->bdev = NULL;
6516 if (hole_em && range_start > hole_start) {
6517 /* our hole starts before our delalloc, so we
6518 * have to return just the parts of the hole
6519 * that go until the delalloc starts
6520 */
6521 em->len = min(hole_len,
6522 range_start - hole_start);
6523 em->start = hole_start;
6524 em->orig_start = hole_start;
6525 /*
6526 * don't adjust block start at all,
6527 * it is fixed at EXTENT_MAP_HOLE
6528 */
6529 em->block_start = hole_em->block_start;
6530 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00006531 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
6532 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05006533 } else {
6534 em->start = range_start;
6535 em->len = found;
6536 em->orig_start = range_start;
6537 em->block_start = EXTENT_MAP_DELALLOC;
6538 em->block_len = found;
6539 }
6540 } else if (hole_em) {
6541 return hole_em;
6542 }
6543out:
6544
6545 free_extent_map(hole_em);
6546 if (err) {
6547 free_extent_map(em);
6548 return ERR_PTR(err);
6549 }
6550 return em;
6551}
6552
Josef Bacik4b46fce2010-05-23 11:00:55 -04006553static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
6554 u64 start, u64 len)
6555{
6556 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04006557 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006558 struct btrfs_key ins;
6559 u64 alloc_hint;
6560 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006561
Josef Bacik4b46fce2010-05-23 11:00:55 -04006562 alloc_hint = get_extent_allocation_hint(inode, start, len);
Josef Bacik00361582013-08-14 14:02:47 -04006563 ret = btrfs_reserve_extent(root, len, root->sectorsize, 0,
Josef Bacik81c9ad22012-01-18 10:56:06 -05006564 alloc_hint, &ins, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006565 if (ret)
6566 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006567
Josef Bacik70c8a912012-10-11 16:54:30 -04006568 em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006569 ins.offset, ins.offset, ins.offset, 0);
Josef Bacik00361582013-08-14 14:02:47 -04006570 if (IS_ERR(em)) {
6571 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
6572 return em;
6573 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006574
6575 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
6576 ins.offset, ins.offset, 0);
6577 if (ret) {
6578 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
Josef Bacik00361582013-08-14 14:02:47 -04006579 free_extent_map(em);
6580 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006581 }
Josef Bacik00361582013-08-14 14:02:47 -04006582
Josef Bacik4b46fce2010-05-23 11:00:55 -04006583 return em;
6584}
6585
Chris Mason46bfbb52010-05-26 11:04:10 -04006586/*
6587 * returns 1 when the nocow is safe, < 1 on error, 0 if the
6588 * block must be cow'd
6589 */
Josef Bacik00361582013-08-14 14:02:47 -04006590noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04006591 u64 *orig_start, u64 *orig_block_len,
6592 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04006593{
Josef Bacik00361582013-08-14 14:02:47 -04006594 struct btrfs_trans_handle *trans;
Chris Mason46bfbb52010-05-26 11:04:10 -04006595 struct btrfs_path *path;
6596 int ret;
6597 struct extent_buffer *leaf;
6598 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08006599 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04006600 struct btrfs_file_extent_item *fi;
6601 struct btrfs_key key;
6602 u64 disk_bytenr;
6603 u64 backref_offset;
6604 u64 extent_end;
6605 u64 num_bytes;
6606 int slot;
6607 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04006608 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08006609
Chris Mason46bfbb52010-05-26 11:04:10 -04006610 path = btrfs_alloc_path();
6611 if (!path)
6612 return -ENOMEM;
6613
Josef Bacik00361582013-08-14 14:02:47 -04006614 ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04006615 offset, 0);
6616 if (ret < 0)
6617 goto out;
6618
6619 slot = path->slots[0];
6620 if (ret == 1) {
6621 if (slot == 0) {
6622 /* can't find the item, must cow */
6623 ret = 0;
6624 goto out;
6625 }
6626 slot--;
6627 }
6628 ret = 0;
6629 leaf = path->nodes[0];
6630 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08006631 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04006632 key.type != BTRFS_EXTENT_DATA_KEY) {
6633 /* not our file or wrong item type, must cow */
6634 goto out;
6635 }
6636
6637 if (key.offset > offset) {
6638 /* Wrong offset, must cow */
6639 goto out;
6640 }
6641
6642 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
6643 found_type = btrfs_file_extent_type(leaf, fi);
6644 if (found_type != BTRFS_FILE_EXTENT_REG &&
6645 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
6646 /* not a regular extent, must cow */
6647 goto out;
6648 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04006649
6650 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
6651 goto out;
6652
Miao Xiee77751a2013-12-27 21:11:50 +08006653 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
6654 if (extent_end <= offset)
6655 goto out;
6656
Chris Mason46bfbb52010-05-26 11:04:10 -04006657 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04006658 if (disk_bytenr == 0)
6659 goto out;
6660
6661 if (btrfs_file_extent_compression(leaf, fi) ||
6662 btrfs_file_extent_encryption(leaf, fi) ||
6663 btrfs_file_extent_other_encoding(leaf, fi))
6664 goto out;
6665
Chris Mason46bfbb52010-05-26 11:04:10 -04006666 backref_offset = btrfs_file_extent_offset(leaf, fi);
6667
Josef Bacik7ee9e442013-06-21 16:37:03 -04006668 if (orig_start) {
6669 *orig_start = key.offset - backref_offset;
6670 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
6671 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
6672 }
Josef Bacikeb384b52013-04-24 16:32:55 -04006673
Chris Mason46bfbb52010-05-26 11:04:10 -04006674 if (btrfs_extent_readonly(root, disk_bytenr))
6675 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08006676
6677 num_bytes = min(offset + *len, extent_end) - offset;
6678 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
6679 u64 range_end;
6680
6681 range_end = round_up(offset + num_bytes, root->sectorsize) - 1;
6682 ret = test_range_bit(io_tree, offset, range_end,
6683 EXTENT_DELALLOC, 0, NULL);
6684 if (ret) {
6685 ret = -EAGAIN;
6686 goto out;
6687 }
6688 }
6689
Josef Bacik1bda19e2013-10-18 12:10:36 -04006690 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04006691
6692 /*
6693 * look for other files referencing this extent, if we
6694 * find any we must cow
6695 */
Josef Bacik00361582013-08-14 14:02:47 -04006696 trans = btrfs_join_transaction(root);
6697 if (IS_ERR(trans)) {
6698 ret = 0;
Chris Mason46bfbb52010-05-26 11:04:10 -04006699 goto out;
Josef Bacik00361582013-08-14 14:02:47 -04006700 }
6701
6702 ret = btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
6703 key.offset - backref_offset, disk_bytenr);
6704 btrfs_end_transaction(trans, root);
6705 if (ret) {
6706 ret = 0;
6707 goto out;
6708 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006709
6710 /*
6711 * adjust disk_bytenr and num_bytes to cover just the bytes
6712 * in this extent we are about to write. If there
6713 * are any csums in that range we have to cow in order
6714 * to keep the csums correct
6715 */
6716 disk_bytenr += backref_offset;
6717 disk_bytenr += offset - key.offset;
Chris Mason46bfbb52010-05-26 11:04:10 -04006718 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
6719 goto out;
6720 /*
6721 * all of the above have passed, it is safe to overwrite this extent
6722 * without cow
6723 */
Josef Bacikeb384b52013-04-24 16:32:55 -04006724 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04006725 ret = 1;
6726out:
6727 btrfs_free_path(path);
6728 return ret;
6729}
6730
Josef Bacikeb838e72012-07-31 16:28:48 -04006731static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
6732 struct extent_state **cached_state, int writing)
6733{
6734 struct btrfs_ordered_extent *ordered;
6735 int ret = 0;
6736
6737 while (1) {
6738 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6739 0, cached_state);
6740 /*
6741 * We're concerned with the entire range that we're going to be
6742 * doing DIO to, so we need to make sure theres no ordered
6743 * extents in this range.
6744 */
6745 ordered = btrfs_lookup_ordered_range(inode, lockstart,
6746 lockend - lockstart + 1);
6747
6748 /*
6749 * We need to make sure there are no buffered pages in this
6750 * range either, we could have raced between the invalidate in
6751 * generic_file_direct_write and locking the extent. The
6752 * invalidate needs to happen so that reads after a write do not
6753 * get stale data.
6754 */
6755 if (!ordered && (!writing ||
6756 !test_range_bit(&BTRFS_I(inode)->io_tree,
6757 lockstart, lockend, EXTENT_UPTODATE, 0,
6758 *cached_state)))
6759 break;
6760
6761 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6762 cached_state, GFP_NOFS);
6763
6764 if (ordered) {
6765 btrfs_start_ordered_extent(inode, ordered, 1);
6766 btrfs_put_ordered_extent(ordered);
6767 } else {
6768 /* Screw you mmap */
6769 ret = filemap_write_and_wait_range(inode->i_mapping,
6770 lockstart,
6771 lockend);
6772 if (ret)
6773 break;
6774
6775 /*
6776 * If we found a page that couldn't be invalidated just
6777 * fall back to buffered.
6778 */
6779 ret = invalidate_inode_pages2_range(inode->i_mapping,
6780 lockstart >> PAGE_CACHE_SHIFT,
6781 lockend >> PAGE_CACHE_SHIFT);
6782 if (ret)
6783 break;
6784 }
6785
6786 cond_resched();
6787 }
6788
6789 return ret;
6790}
6791
Josef Bacik69ffb542012-09-11 15:40:07 -04006792static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
6793 u64 len, u64 orig_start,
6794 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006795 u64 orig_block_len, u64 ram_bytes,
6796 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04006797{
6798 struct extent_map_tree *em_tree;
6799 struct extent_map *em;
6800 struct btrfs_root *root = BTRFS_I(inode)->root;
6801 int ret;
6802
6803 em_tree = &BTRFS_I(inode)->extent_tree;
6804 em = alloc_extent_map();
6805 if (!em)
6806 return ERR_PTR(-ENOMEM);
6807
6808 em->start = start;
6809 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04006810 em->mod_start = start;
6811 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04006812 em->len = len;
6813 em->block_len = block_len;
6814 em->block_start = block_start;
6815 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05006816 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04006817 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04006818 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04006819 set_bit(EXTENT_FLAG_PINNED, &em->flags);
6820 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05006821 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04006822
6823 do {
6824 btrfs_drop_extent_cache(inode, em->start,
6825 em->start + em->len - 1, 0);
6826 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006827 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04006828 write_unlock(&em_tree->lock);
6829 } while (ret == -EEXIST);
6830
6831 if (ret) {
6832 free_extent_map(em);
6833 return ERR_PTR(ret);
6834 }
6835
6836 return em;
6837}
6838
6839
Josef Bacik4b46fce2010-05-23 11:00:55 -04006840static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
6841 struct buffer_head *bh_result, int create)
6842{
6843 struct extent_map *em;
6844 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04006845 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006846 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04006847 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006848 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04006849 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00006850 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006851
Miao Xie172a5042013-02-21 02:48:22 -07006852 if (create)
Josef Bacikeb838e72012-07-31 16:28:48 -04006853 unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07006854 else
Josef Bacikc3298612012-08-03 16:49:19 -04006855 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04006856
Josef Bacikc3298612012-08-03 16:49:19 -04006857 lockstart = start;
6858 lockend = start + len - 1;
6859
Josef Bacikeb838e72012-07-31 16:28:48 -04006860 /*
6861 * If this errors out it's because we couldn't invalidate pagecache for
6862 * this range and we need to fallback to buffered.
6863 */
6864 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
6865 return -ENOTBLK;
6866
Josef Bacik4b46fce2010-05-23 11:00:55 -04006867 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04006868 if (IS_ERR(em)) {
6869 ret = PTR_ERR(em);
6870 goto unlock_err;
6871 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006872
6873 /*
6874 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
6875 * io. INLINE is special, and we could probably kludge it in here, but
6876 * it's still buffered so for safety lets just fall back to the generic
6877 * buffered path.
6878 *
6879 * For COMPRESSED we _have_ to read the entire extent in so we can
6880 * decompress it, so there will be buffering required no matter what we
6881 * do, so go ahead and fallback to buffered.
6882 *
6883 * We return -ENOTBLK because thats what makes DIO go ahead and go back
6884 * to buffered IO. Don't blame me, this is the price we pay for using
6885 * the generic code.
6886 */
6887 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
6888 em->block_start == EXTENT_MAP_INLINE) {
6889 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006890 ret = -ENOTBLK;
6891 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006892 }
6893
6894 /* Just a good old fashioned hole, return */
6895 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
6896 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
6897 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006898 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006899 }
6900
6901 /*
6902 * We don't allocate a new extent in the following cases
6903 *
6904 * 1) The inode is marked as NODATACOW. In this case we'll just use the
6905 * existing extent.
6906 * 2) The extent is marked as PREALLOC. We're good to go here and can
6907 * just use the extent.
6908 *
6909 */
Chris Mason46bfbb52010-05-26 11:04:10 -04006910 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04006911 len = min(len, em->len - (start - em->start));
6912 lockstart = start + len;
6913 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04006914 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006915
6916 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
6917 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
6918 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04006919 int type;
6920 int ret;
Josef Bacikeb384b52013-04-24 16:32:55 -04006921 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006922
6923 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6924 type = BTRFS_ORDERED_PREALLOC;
6925 else
6926 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04006927 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04006928 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04006929
Josef Bacik00361582013-08-14 14:02:47 -04006930 if (can_nocow_extent(inode, start, &len, &orig_start,
Josef Bacik7ee9e442013-06-21 16:37:03 -04006931 &orig_block_len, &ram_bytes) == 1) {
Josef Bacik69ffb542012-09-11 15:40:07 -04006932 if (type == BTRFS_ORDERED_PREALLOC) {
6933 free_extent_map(em);
6934 em = create_pinned_em(inode, start, len,
6935 orig_start,
Josef Bacikb4939682012-12-03 10:31:19 -05006936 block_start, len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006937 orig_block_len,
6938 ram_bytes, type);
Josef Bacik00361582013-08-14 14:02:47 -04006939 if (IS_ERR(em))
Josef Bacik69ffb542012-09-11 15:40:07 -04006940 goto unlock_err;
Josef Bacik69ffb542012-09-11 15:40:07 -04006941 }
6942
Chris Mason46bfbb52010-05-26 11:04:10 -04006943 ret = btrfs_add_ordered_extent_dio(inode, start,
6944 block_start, len, len, type);
Chris Mason46bfbb52010-05-26 11:04:10 -04006945 if (ret) {
6946 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006947 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04006948 }
6949 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006950 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006951 }
Josef Bacik00361582013-08-14 14:02:47 -04006952
Chris Mason46bfbb52010-05-26 11:04:10 -04006953 /*
6954 * this will cow the extent, reset the len in case we changed
6955 * it above
6956 */
6957 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04006958 free_extent_map(em);
6959 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04006960 if (IS_ERR(em)) {
6961 ret = PTR_ERR(em);
6962 goto unlock_err;
6963 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006964 len = min(len, em->len - (start - em->start));
6965unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04006966 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
6967 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04006968 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006969 bh_result->b_bdev = em->bdev;
6970 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04006971 if (create) {
6972 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6973 set_buffer_new(bh_result);
6974
6975 /*
6976 * Need to update the i_size under the extent lock so buffered
6977 * readers will get the updated i_size when we unlock.
6978 */
6979 if (start + len > i_size_read(inode))
6980 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00006981
Miao Xie172a5042013-02-21 02:48:22 -07006982 spin_lock(&BTRFS_I(inode)->lock);
6983 BTRFS_I(inode)->outstanding_extents++;
6984 spin_unlock(&BTRFS_I(inode)->lock);
6985
Miao Xie09348562013-02-07 10:12:07 +00006986 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6987 lockstart + len - 1, EXTENT_DELALLOC, NULL,
6988 &cached_state, GFP_NOFS);
6989 BUG_ON(ret);
Josef Bacikc3473e82012-06-19 10:59:00 -04006990 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006991
Josef Bacikeb838e72012-07-31 16:28:48 -04006992 /*
6993 * In the case of write we need to clear and unlock the entire range,
6994 * in the case of read we need to unlock only the end area that we
6995 * aren't using if there is any left over space.
6996 */
Liu Bo24c03fa2012-08-22 20:10:38 -06006997 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00006998 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6999 lockend, unlock_bits, 1, 0,
7000 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06007001 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007002 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007003 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007004
Josef Bacik4b46fce2010-05-23 11:00:55 -04007005 free_extent_map(em);
7006
7007 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007008
7009unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007010 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7011 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
7012 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007013}
7014
Josef Bacik4b46fce2010-05-23 11:00:55 -04007015static void btrfs_endio_direct_read(struct bio *bio, int err)
7016{
Miao Xiee65e1532010-11-22 03:04:43 +00007017 struct btrfs_dio_private *dip = bio->bi_private;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007018 struct bio_vec *bvec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007019 struct inode *inode = dip->inode;
7020 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason9be33952013-05-17 18:30:14 -04007021 struct bio *dio_bio;
Miao Xiefacc8a22013-07-25 19:22:34 +08007022 u32 *csums = (u32 *)dip->csum;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007023 u64 start;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007024 int i;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007025
7026 start = dip->logical_offset;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007027 bio_for_each_segment_all(bvec, bio, i) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007028 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
7029 struct page *page = bvec->bv_page;
7030 char *kaddr;
7031 u32 csum = ~(u32)0;
7032 unsigned long flags;
7033
7034 local_irq_save(flags);
Cong Wang7ac687d2011-11-25 23:14:28 +08007035 kaddr = kmap_atomic(page);
Liu Bob0496682013-03-14 14:57:45 +00007036 csum = btrfs_csum_data(kaddr + bvec->bv_offset,
Josef Bacik4b46fce2010-05-23 11:00:55 -04007037 csum, bvec->bv_len);
7038 btrfs_csum_final(csum, (char *)&csum);
Cong Wang7ac687d2011-11-25 23:14:28 +08007039 kunmap_atomic(kaddr);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007040 local_irq_restore(flags);
7041
7042 flush_dcache_page(bvec->bv_page);
Kent Overstreet2c30c712013-11-07 12:20:26 -08007043 if (csum != csums[i]) {
Miao Xiefacc8a22013-07-25 19:22:34 +08007044 btrfs_err(root->fs_info, "csum failed ino %llu off %llu csum %u expected csum %u",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02007045 btrfs_ino(inode), start, csum,
Kent Overstreet2c30c712013-11-07 12:20:26 -08007046 csums[i]);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007047 err = -EIO;
7048 }
7049 }
7050
7051 start += bvec->bv_len;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007052 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007053
7054 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007055 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04007056 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007057
Josef Bacik4b46fce2010-05-23 11:00:55 -04007058 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04007059
7060 /* If we had a csum failure make sure to clear the uptodate flag */
7061 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04007062 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
7063 dio_end_io(dio_bio, err);
7064 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007065}
7066
7067static void btrfs_endio_direct_write(struct bio *bio, int err)
7068{
7069 struct btrfs_dio_private *dip = bio->bi_private;
7070 struct inode *inode = dip->inode;
7071 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007072 struct btrfs_ordered_extent *ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05007073 u64 ordered_offset = dip->logical_offset;
7074 u64 ordered_bytes = dip->bytes;
Chris Mason9be33952013-05-17 18:30:14 -04007075 struct bio *dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007076 int ret;
7077
7078 if (err)
7079 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05007080again:
7081 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
7082 &ordered_offset,
Josef Bacik5fd02042012-05-02 14:00:54 -04007083 ordered_bytes, !err);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007084 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05007085 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007086
Qu Wenruofccb5d82014-02-28 10:46:10 +08007087 btrfs_init_work(&ordered->work, finish_ordered_fn, NULL, NULL);
7088 btrfs_queue_work(root->fs_info->endio_write_workers,
7089 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05007090out_test:
7091 /*
7092 * our bio might span multiple ordered extents. If we haven't
7093 * completed the accounting for the whole dio, go back and try again
7094 */
7095 if (ordered_offset < dip->logical_offset + dip->bytes) {
7096 ordered_bytes = dip->logical_offset + dip->bytes -
7097 ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04007098 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05007099 goto again;
7100 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007101out_done:
Chris Mason9be33952013-05-17 18:30:14 -04007102 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007103
Josef Bacik4b46fce2010-05-23 11:00:55 -04007104 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04007105
7106 /* If we had an error make sure to clear the uptodate flag */
7107 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04007108 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
7109 dio_end_io(dio_bio, err);
7110 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007111}
7112
Chris Masoneaf25d92010-05-25 09:48:28 -04007113static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
7114 struct bio *bio, int mirror_num,
7115 unsigned long bio_flags, u64 offset)
7116{
7117 int ret;
7118 struct btrfs_root *root = BTRFS_I(inode)->root;
7119 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007120 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04007121 return 0;
7122}
7123
Miao Xiee65e1532010-11-22 03:04:43 +00007124static void btrfs_end_dio_bio(struct bio *bio, int err)
7125{
7126 struct btrfs_dio_private *dip = bio->bi_private;
7127
7128 if (err) {
Frank Holtonefe120a2013-12-20 11:37:06 -05007129 btrfs_err(BTRFS_I(dip->inode)->root->fs_info,
7130 "direct IO failed ino %llu rw %lu sector %#Lx len %u err no %d",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02007131 btrfs_ino(dip->inode), bio->bi_rw,
Kent Overstreet4f024f32013-10-11 15:44:27 -07007132 (unsigned long long)bio->bi_iter.bi_sector,
7133 bio->bi_iter.bi_size, err);
Miao Xiee65e1532010-11-22 03:04:43 +00007134 dip->errors = 1;
7135
7136 /*
7137 * before atomic variable goto zero, we must make sure
7138 * dip->errors is perceived to be set.
7139 */
7140 smp_mb__before_atomic_dec();
7141 }
7142
7143 /* if there are more bios still pending for this dio, just exit */
7144 if (!atomic_dec_and_test(&dip->pending_bios))
7145 goto out;
7146
Chris Mason9be33952013-05-17 18:30:14 -04007147 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00007148 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04007149 } else {
7150 set_bit(BIO_UPTODATE, &dip->dio_bio->bi_flags);
Miao Xiee65e1532010-11-22 03:04:43 +00007151 bio_endio(dip->orig_bio, 0);
7152 }
7153out:
7154 bio_put(bio);
7155}
7156
7157static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
7158 u64 first_sector, gfp_t gfp_flags)
7159{
7160 int nr_vecs = bio_get_nr_vecs(bdev);
7161 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
7162}
7163
7164static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
7165 int rw, u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007166 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00007167{
Miao Xiefacc8a22013-07-25 19:22:34 +08007168 struct btrfs_dio_private *dip = bio->bi_private;
Miao Xiee65e1532010-11-22 03:04:43 +00007169 int write = rw & REQ_WRITE;
7170 struct btrfs_root *root = BTRFS_I(inode)->root;
7171 int ret;
7172
Josef Bacikb812ce22012-11-16 13:56:32 -05007173 if (async_submit)
7174 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
7175
Miao Xiee65e1532010-11-22 03:04:43 +00007176 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04007177
7178 if (!write) {
7179 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
7180 if (ret)
7181 goto err;
7182 }
Miao Xiee65e1532010-11-22 03:04:43 +00007183
Josef Bacik1ae39932011-04-06 14:41:34 -04007184 if (skip_sum)
7185 goto map;
7186
7187 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00007188 ret = btrfs_wq_submit_bio(root->fs_info,
7189 inode, rw, bio, 0, 0,
7190 file_offset,
7191 __btrfs_submit_bio_start_direct_io,
7192 __btrfs_submit_bio_done);
7193 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04007194 } else if (write) {
7195 /*
7196 * If we aren't doing async submit, calculate the csum of the
7197 * bio now.
7198 */
7199 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
7200 if (ret)
7201 goto err;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007202 } else if (!skip_sum) {
Miao Xiefacc8a22013-07-25 19:22:34 +08007203 ret = btrfs_lookup_bio_sums_dio(root, inode, dip, bio,
7204 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007205 if (ret)
7206 goto err;
7207 }
Miao Xiee65e1532010-11-22 03:04:43 +00007208
Josef Bacik1ae39932011-04-06 14:41:34 -04007209map:
7210 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007211err:
7212 bio_put(bio);
7213 return ret;
7214}
7215
7216static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
7217 int skip_sum)
7218{
7219 struct inode *inode = dip->inode;
7220 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00007221 struct bio *bio;
7222 struct bio *orig_bio = dip->orig_bio;
7223 struct bio_vec *bvec = orig_bio->bi_io_vec;
Kent Overstreet4f024f32013-10-11 15:44:27 -07007224 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00007225 u64 file_offset = dip->logical_offset;
7226 u64 submit_len = 0;
7227 u64 map_length;
7228 int nr_pages = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007229 int ret = 0;
Josef Bacik1ae39932011-04-06 14:41:34 -04007230 int async_submit = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007231
Kent Overstreet4f024f32013-10-11 15:44:27 -07007232 map_length = orig_bio->bi_iter.bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007233 ret = btrfs_map_block(root->fs_info, rw, start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007234 &map_length, NULL, 0);
7235 if (ret) {
Josef Bacik64728bb2011-04-25 19:43:52 -04007236 bio_put(orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00007237 return -EIO;
7238 }
Miao Xiefacc8a22013-07-25 19:22:34 +08007239
Kent Overstreet4f024f32013-10-11 15:44:27 -07007240 if (map_length >= orig_bio->bi_iter.bi_size) {
Josef Bacik02f57c72011-04-06 14:25:44 -04007241 bio = orig_bio;
7242 goto submit;
7243 }
7244
David Woodhouse53b381b2013-01-29 18:40:14 -05007245 /* async crcs make it difficult to collect full stripe writes. */
7246 if (btrfs_get_alloc_profile(root, 1) &
7247 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6))
7248 async_submit = 0;
7249 else
7250 async_submit = 1;
7251
Josef Bacik02f57c72011-04-06 14:25:44 -04007252 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
7253 if (!bio)
7254 return -ENOMEM;
7255 bio->bi_private = dip;
7256 bio->bi_end_io = btrfs_end_dio_bio;
7257 atomic_inc(&dip->pending_bios);
7258
Miao Xiee65e1532010-11-22 03:04:43 +00007259 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
7260 if (unlikely(map_length < submit_len + bvec->bv_len ||
7261 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
7262 bvec->bv_offset) < bvec->bv_len)) {
7263 /*
7264 * inc the count before we submit the bio so
7265 * we know the end IO handler won't happen before
7266 * we inc the count. Otherwise, the dip might get freed
7267 * before we're done setting it up
7268 */
7269 atomic_inc(&dip->pending_bios);
7270 ret = __btrfs_submit_dio_bio(bio, inode, rw,
7271 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007272 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007273 if (ret) {
7274 bio_put(bio);
7275 atomic_dec(&dip->pending_bios);
7276 goto out_err;
7277 }
7278
Miao Xiee65e1532010-11-22 03:04:43 +00007279 start_sector += submit_len >> 9;
7280 file_offset += submit_len;
7281
7282 submit_len = 0;
7283 nr_pages = 0;
7284
7285 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
7286 start_sector, GFP_NOFS);
7287 if (!bio)
7288 goto out_err;
7289 bio->bi_private = dip;
7290 bio->bi_end_io = btrfs_end_dio_bio;
7291
Kent Overstreet4f024f32013-10-11 15:44:27 -07007292 map_length = orig_bio->bi_iter.bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007293 ret = btrfs_map_block(root->fs_info, rw,
Stefan Behrens3ec706c2012-11-05 15:46:42 +01007294 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007295 &map_length, NULL, 0);
7296 if (ret) {
7297 bio_put(bio);
7298 goto out_err;
7299 }
7300 } else {
7301 submit_len += bvec->bv_len;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307302 nr_pages++;
Miao Xiee65e1532010-11-22 03:04:43 +00007303 bvec++;
7304 }
7305 }
7306
Josef Bacik02f57c72011-04-06 14:25:44 -04007307submit:
Miao Xiee65e1532010-11-22 03:04:43 +00007308 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007309 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007310 if (!ret)
7311 return 0;
7312
7313 bio_put(bio);
7314out_err:
7315 dip->errors = 1;
7316 /*
7317 * before atomic variable goto zero, we must
7318 * make sure dip->errors is perceived to be set.
7319 */
7320 smp_mb__before_atomic_dec();
7321 if (atomic_dec_and_test(&dip->pending_bios))
7322 bio_io_error(dip->orig_bio);
7323
7324 /* bio_end_io() will handle error, so we needn't return it */
7325 return 0;
7326}
7327
Chris Mason9be33952013-05-17 18:30:14 -04007328static void btrfs_submit_direct(int rw, struct bio *dio_bio,
7329 struct inode *inode, loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007330{
7331 struct btrfs_root *root = BTRFS_I(inode)->root;
7332 struct btrfs_dio_private *dip;
Chris Mason9be33952013-05-17 18:30:14 -04007333 struct bio *io_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007334 int skip_sum;
Miao Xiefacc8a22013-07-25 19:22:34 +08007335 int sum_len;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02007336 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007337 int ret = 0;
Miao Xiefacc8a22013-07-25 19:22:34 +08007338 u16 csum_size;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007339
7340 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
7341
Chris Mason9be33952013-05-17 18:30:14 -04007342 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04007343 if (!io_bio) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007344 ret = -ENOMEM;
7345 goto free_ordered;
7346 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007347
Miao Xiefacc8a22013-07-25 19:22:34 +08007348 if (!skip_sum && !write) {
7349 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
Kent Overstreet4f024f32013-10-11 15:44:27 -07007350 sum_len = dio_bio->bi_iter.bi_size >>
7351 inode->i_sb->s_blocksize_bits;
Miao Xiefacc8a22013-07-25 19:22:34 +08007352 sum_len *= csum_size;
7353 } else {
7354 sum_len = 0;
7355 }
7356
7357 dip = kmalloc(sizeof(*dip) + sum_len, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04007358 if (!dip) {
7359 ret = -ENOMEM;
7360 goto free_io_bio;
7361 }
7362
7363 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007364 dip->inode = inode;
7365 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07007366 dip->bytes = dio_bio->bi_iter.bi_size;
7367 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Chris Mason9be33952013-05-17 18:30:14 -04007368 io_bio->bi_private = dip;
Miao Xiee65e1532010-11-22 03:04:43 +00007369 dip->errors = 0;
Chris Mason9be33952013-05-17 18:30:14 -04007370 dip->orig_bio = io_bio;
7371 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00007372 atomic_set(&dip->pending_bios, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007373
7374 if (write)
Chris Mason9be33952013-05-17 18:30:14 -04007375 io_bio->bi_end_io = btrfs_endio_direct_write;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007376 else
Chris Mason9be33952013-05-17 18:30:14 -04007377 io_bio->bi_end_io = btrfs_endio_direct_read;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007378
Miao Xiee65e1532010-11-22 03:04:43 +00007379 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
7380 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04007381 return;
Chris Mason9be33952013-05-17 18:30:14 -04007382
7383free_io_bio:
7384 bio_put(io_bio);
7385
Josef Bacik4b46fce2010-05-23 11:00:55 -04007386free_ordered:
7387 /*
7388 * If this is a write, we need to clean up the reserved space and kill
7389 * the ordered extent.
7390 */
7391 if (write) {
7392 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05007393 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007394 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
7395 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
7396 btrfs_free_reserved_extent(root, ordered->start,
7397 ordered->disk_len);
7398 btrfs_put_ordered_extent(ordered);
7399 btrfs_put_ordered_extent(ordered);
7400 }
Chris Mason9be33952013-05-17 18:30:14 -04007401 bio_endio(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007402}
7403
Chris Mason5a5f79b2010-05-26 21:33:37 -04007404static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
7405 const struct iovec *iov, loff_t offset,
7406 unsigned long nr_segs)
7407{
7408 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00007409 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007410 size_t size;
7411 unsigned long addr;
7412 unsigned blocksize_mask = root->sectorsize - 1;
7413 ssize_t retval = -EINVAL;
7414 loff_t end = offset;
7415
7416 if (offset & blocksize_mask)
7417 goto out;
7418
7419 /* Check the memory alignment. Blocks cannot straddle pages */
7420 for (seg = 0; seg < nr_segs; seg++) {
7421 addr = (unsigned long)iov[seg].iov_base;
7422 size = iov[seg].iov_len;
7423 end += size;
Josef Bacika1b75f72011-04-08 15:51:18 +00007424 if ((addr & blocksize_mask) || (size & blocksize_mask))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007425 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00007426
7427 /* If this is a write we don't need to check anymore */
7428 if (rw & WRITE)
7429 continue;
7430
7431 /*
7432 * Check to make sure we don't have duplicate iov_base's in this
7433 * iovec, if so return EINVAL, otherwise we'll get csum errors
7434 * when reading back.
7435 */
7436 for (i = seg + 1; i < nr_segs; i++) {
7437 if (iov[seg].iov_base == iov[i].iov_base)
7438 goto out;
7439 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04007440 }
7441 retval = 0;
7442out:
7443 return retval;
7444}
Josef Bacikeb838e72012-07-31 16:28:48 -04007445
Chris Mason16432982008-04-10 10:23:21 -04007446static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
7447 const struct iovec *iov, loff_t offset,
7448 unsigned long nr_segs)
7449{
Josef Bacik4b46fce2010-05-23 11:00:55 -04007450 struct file *file = iocb->ki_filp;
7451 struct inode *inode = file->f_mapping->host;
Miao Xie09348562013-02-07 10:12:07 +00007452 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00007453 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00007454 bool wakeup = true;
7455 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00007456 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007457
Chris Mason5a5f79b2010-05-26 21:33:37 -04007458 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
Josef Bacikeb838e72012-07-31 16:28:48 -04007459 offset, nr_segs))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007460 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007461
Miao Xie38851cc2013-02-08 07:04:11 +00007462 atomic_inc(&inode->i_dio_count);
7463 smp_mb__after_atomic_inc();
7464
Josef Bacik0e267c42013-07-02 10:38:02 -04007465 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08007466 * The generic stuff only does filemap_write_and_wait_range, which
7467 * isn't enough if we've written compressed pages to this area, so
7468 * we need to flush the dirty pages again to make absolutely sure
7469 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04007470 */
7471 count = iov_length(iov, nr_segs);
Miao Xie41bd9ca2014-03-06 13:54:57 +08007472 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
7473 &BTRFS_I(inode)->runtime_flags))
7474 filemap_fdatawrite_range(inode->i_mapping, offset, count);
Josef Bacik0e267c42013-07-02 10:38:02 -04007475
Miao Xie09348562013-02-07 10:12:07 +00007476 if (rw & WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00007477 /*
7478 * If the write DIO is beyond the EOF, we need update
7479 * the isize, but it is protected by i_mutex. So we can
7480 * not unlock the i_mutex at this case.
7481 */
7482 if (offset + count <= inode->i_size) {
7483 mutex_unlock(&inode->i_mutex);
7484 relock = true;
7485 }
Miao Xie09348562013-02-07 10:12:07 +00007486 ret = btrfs_delalloc_reserve_space(inode, count);
7487 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00007488 goto out;
7489 } else if (unlikely(test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
7490 &BTRFS_I(inode)->runtime_flags))) {
7491 inode_dio_done(inode);
7492 flags = DIO_LOCKING | DIO_SKIP_HOLES;
7493 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00007494 }
7495
7496 ret = __blockdev_direct_IO(rw, iocb, inode,
7497 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
7498 iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
Miao Xie2e60a512013-02-08 07:01:08 +00007499 btrfs_submit_direct, flags);
Miao Xie09348562013-02-07 10:12:07 +00007500 if (rw & WRITE) {
7501 if (ret < 0 && ret != -EIOCBQUEUED)
7502 btrfs_delalloc_release_space(inode, count);
Miao Xie172a5042013-02-21 02:48:22 -07007503 else if (ret >= 0 && (size_t)ret < count)
Miao Xie09348562013-02-07 10:12:07 +00007504 btrfs_delalloc_release_space(inode,
7505 count - (size_t)ret);
Miao Xie172a5042013-02-21 02:48:22 -07007506 else
7507 btrfs_delalloc_release_metadata(inode, 0);
Miao Xie09348562013-02-07 10:12:07 +00007508 }
Miao Xie38851cc2013-02-08 07:04:11 +00007509out:
Miao Xie2e60a512013-02-08 07:01:08 +00007510 if (wakeup)
7511 inode_dio_done(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00007512 if (relock)
7513 mutex_lock(&inode->i_mutex);
Miao Xie09348562013-02-07 10:12:07 +00007514
7515 return ret;
Chris Mason16432982008-04-10 10:23:21 -04007516}
7517
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007518#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
7519
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007520static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
7521 __u64 start, __u64 len)
7522{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007523 int ret;
7524
7525 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
7526 if (ret)
7527 return ret;
7528
Chris Masonec29ed52011-02-23 16:23:20 -05007529 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007530}
7531
Chris Mason9ebefb182007-06-15 13:50:00 -04007532int btrfs_readpage(struct file *file, struct page *page)
7533{
Chris Masond1310b22008-01-24 16:13:08 -05007534 struct extent_io_tree *tree;
7535 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02007536 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04007537}
Chris Mason1832a6d2007-12-21 16:27:21 -05007538
Chris Mason39279cc2007-06-12 06:35:45 -04007539static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
7540{
Chris Masond1310b22008-01-24 16:13:08 -05007541 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04007542
7543
7544 if (current->flags & PF_MEMALLOC) {
7545 redirty_page_for_writepage(wbc, page);
7546 unlock_page(page);
7547 return 0;
7548 }
Chris Masond1310b22008-01-24 16:13:08 -05007549 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04007550 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
7551}
Chris Mason39279cc2007-06-12 06:35:45 -04007552
Eric Sandeen48a3b632013-04-25 20:41:01 +00007553static int btrfs_writepages(struct address_space *mapping,
7554 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04007555{
Chris Masond1310b22008-01-24 16:13:08 -05007556 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05007557
Chris Masond1310b22008-01-24 16:13:08 -05007558 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04007559 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
7560}
7561
Chris Mason3ab2fb52007-11-08 10:59:22 -05007562static int
7563btrfs_readpages(struct file *file, struct address_space *mapping,
7564 struct list_head *pages, unsigned nr_pages)
7565{
Chris Masond1310b22008-01-24 16:13:08 -05007566 struct extent_io_tree *tree;
7567 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05007568 return extent_readpages(tree, mapping, pages, nr_pages,
7569 btrfs_get_extent);
7570}
Chris Masone6dcd2d2008-07-17 12:53:50 -04007571static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04007572{
Chris Masond1310b22008-01-24 16:13:08 -05007573 struct extent_io_tree *tree;
7574 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04007575 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04007576
Chris Masond1310b22008-01-24 16:13:08 -05007577 tree = &BTRFS_I(page->mapping->host)->io_tree;
7578 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05007579 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04007580 if (ret == 1) {
7581 ClearPagePrivate(page);
7582 set_page_private(page, 0);
7583 page_cache_release(page);
7584 }
7585 return ret;
7586}
Chris Mason39279cc2007-06-12 06:35:45 -04007587
Chris Masone6dcd2d2008-07-17 12:53:50 -04007588static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
7589{
Chris Mason98509cf2008-09-11 15:51:43 -04007590 if (PageWriteback(page) || PageDirty(page))
7591 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05007592 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007593}
7594
Lukas Czernerd47992f2013-05-21 23:17:23 -04007595static void btrfs_invalidatepage(struct page *page, unsigned int offset,
7596 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04007597{
Josef Bacik5fd02042012-05-02 14:00:54 -04007598 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05007599 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007600 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007601 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007602 u64 page_start = page_offset(page);
7603 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00007604 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04007605
Chris Mason8b62b722009-09-02 16:53:46 -04007606 /*
7607 * we have the page locked, so new writeback can't start,
7608 * and the dirty bit won't be cleared while we are here.
7609 *
7610 * Wait for IO on this page so that we can safely clear
7611 * the PagePrivate2 bit and do ordered accounting
7612 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007613 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04007614
Josef Bacik5fd02042012-05-02 14:00:54 -04007615 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007616 if (offset) {
7617 btrfs_releasepage(page, GFP_NOFS);
7618 return;
7619 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00007620
7621 if (!inode_evicting)
7622 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
7623 ordered = btrfs_lookup_ordered_extent(inode, page_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007624 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04007625 /*
7626 * IO on this page will never be started, so we need
7627 * to account for any ordered extents now
7628 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00007629 if (!inode_evicting)
7630 clear_extent_bit(tree, page_start, page_end,
7631 EXTENT_DIRTY | EXTENT_DELALLOC |
7632 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
7633 EXTENT_DEFRAG, 1, 0, &cached_state,
7634 GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04007635 /*
7636 * whoever cleared the private bit is responsible
7637 * for the finish_ordered_io
7638 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04007639 if (TestClearPagePrivate2(page)) {
7640 struct btrfs_ordered_inode_tree *tree;
7641 u64 new_len;
7642
7643 tree = &BTRFS_I(inode)->ordered_tree;
7644
7645 spin_lock_irq(&tree->lock);
7646 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
7647 new_len = page_start - ordered->file_offset;
7648 if (new_len < ordered->truncated_len)
7649 ordered->truncated_len = new_len;
7650 spin_unlock_irq(&tree->lock);
7651
7652 if (btrfs_dec_test_ordered_pending(inode, &ordered,
7653 page_start,
7654 PAGE_CACHE_SIZE, 1))
7655 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04007656 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007657 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00007658 if (!inode_evicting) {
7659 cached_state = NULL;
7660 lock_extent_bits(tree, page_start, page_end, 0,
7661 &cached_state);
7662 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007663 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00007664
7665 if (!inode_evicting) {
7666 clear_extent_bit(tree, page_start, page_end,
7667 EXTENT_LOCKED | EXTENT_DIRTY |
7668 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
7669 EXTENT_DEFRAG, 1, 1,
7670 &cached_state, GFP_NOFS);
7671
7672 __btrfs_releasepage(page, GFP_NOFS);
7673 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007674
Chris Mason4a096752008-07-21 10:29:44 -04007675 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04007676 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04007677 ClearPagePrivate(page);
7678 set_page_private(page, 0);
7679 page_cache_release(page);
7680 }
Chris Mason39279cc2007-06-12 06:35:45 -04007681}
7682
Chris Mason9ebefb182007-06-15 13:50:00 -04007683/*
7684 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
7685 * called from a page fault handler when a page is first dirtied. Hence we must
7686 * be careful to check for EOF conditions here. We set the page up correctly
7687 * for a written page which means we get ENOSPC checking when writing into
7688 * holes and correct delalloc and unwritten extent mapping on filesystems that
7689 * support these features.
7690 *
7691 * We are not allowed to take the i_mutex here so we have to play games to
7692 * protect against truncate races as the page could now be beyond EOF. Because
7693 * vmtruncate() writes the inode size before removing pages, once we have the
7694 * page lock we can determine safely if the page is beyond EOF. If it is not
7695 * beyond EOF, then the page is guaranteed safe against truncation until we
7696 * unlock the page.
7697 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07007698int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04007699{
Nick Pigginc2ec1752009-03-31 15:23:21 -07007700 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05007701 struct inode *inode = file_inode(vma->vm_file);
Chris Mason1832a6d2007-12-21 16:27:21 -05007702 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007703 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7704 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007705 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007706 char *kaddr;
7707 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04007708 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05007709 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05007710 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007711 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007712 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04007713
Jan Karab2b5ef52012-06-12 16:20:45 +02007714 sb_start_pagefault(inode->i_sb);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04007715 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007716 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04007717 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05007718 reserved = 1;
7719 }
Nick Piggin56a76f82009-03-31 15:23:23 -07007720 if (ret) {
7721 if (ret == -ENOMEM)
7722 ret = VM_FAULT_OOM;
7723 else /* -ENOSPC, -EIO, etc */
7724 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05007725 if (reserved)
7726 goto out;
7727 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07007728 }
Chris Mason1832a6d2007-12-21 16:27:21 -05007729
Nick Piggin56a76f82009-03-31 15:23:23 -07007730 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007731again:
Chris Mason9ebefb182007-06-15 13:50:00 -04007732 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04007733 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007734 page_start = page_offset(page);
7735 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04007736
Chris Mason9ebefb182007-06-15 13:50:00 -04007737 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04007738 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04007739 /* page got truncated out from underneath us */
7740 goto out_unlock;
7741 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007742 wait_on_page_writeback(page);
7743
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007744 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007745 set_page_extent_mapped(page);
7746
Chris Masoneb84ae02008-07-17 13:53:27 -04007747 /*
7748 * we can't set the delalloc bits if there are pending ordered
7749 * extents. Drop our locks and wait for them to finish
7750 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007751 ordered = btrfs_lookup_ordered_extent(inode, page_start);
7752 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007753 unlock_extent_cached(io_tree, page_start, page_end,
7754 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007755 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04007756 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007757 btrfs_put_ordered_extent(ordered);
7758 goto again;
7759 }
7760
Josef Bacikfbf19082009-10-01 17:10:23 -04007761 /*
7762 * XXX - page_mkwrite gets called every time the page is dirtied, even
7763 * if it was already dirty, so for space accounting reasons we need to
7764 * clear any delalloc bits for the range we are fixing to save. There
7765 * is probably a better way to do this, but for now keep consistent with
7766 * prepare_pages in the normal write path.
7767 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00007768 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06007769 EXTENT_DIRTY | EXTENT_DELALLOC |
7770 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00007771 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04007772
Josef Bacik2ac55d42010-02-03 19:33:23 +00007773 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
7774 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007775 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007776 unlock_extent_cached(io_tree, page_start, page_end,
7777 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007778 ret = VM_FAULT_SIGBUS;
7779 goto out_unlock;
7780 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007781 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04007782
7783 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04007784 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04007785 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04007786 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04007787 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04007788
Chris Masone6dcd2d2008-07-17 12:53:50 -04007789 if (zero_start != PAGE_CACHE_SIZE) {
7790 kaddr = kmap(page);
7791 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
7792 flush_dcache_page(page);
7793 kunmap(page);
7794 }
Chris Mason247e7432008-07-17 12:53:51 -04007795 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007796 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04007797 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007798
Chris Mason257c62e2009-10-13 13:21:08 -04007799 BTRFS_I(inode)->last_trans = root->fs_info->generation;
7800 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06007801 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04007802
Josef Bacik2ac55d42010-02-03 19:33:23 +00007803 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04007804
7805out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02007806 if (!ret) {
7807 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04007808 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02007809 }
Chris Mason9ebefb182007-06-15 13:50:00 -04007810 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05007811out:
Josef Bacikec39e182012-01-12 19:10:12 -05007812 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007813out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02007814 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04007815 return ret;
7816}
7817
Josef Bacika41ad392011-01-31 15:30:16 -05007818static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04007819{
7820 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04007821 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04007822 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05007823 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007824 struct btrfs_trans_handle *trans;
Chris Masondbe674a2008-07-17 12:54:05 -04007825 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04007826 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04007827
Josef Bacik0ef8b722013-10-25 16:13:35 -04007828 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
7829 (u64)-1);
7830 if (ret)
7831 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04007832
Josef Bacikfcb80c22011-05-03 10:40:22 -04007833 /*
7834 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
7835 * 3 things going on here
7836 *
7837 * 1) We need to reserve space for our orphan item and the space to
7838 * delete our orphan item. Lord knows we don't want to have a dangling
7839 * orphan item because we didn't reserve space to remove it.
7840 *
7841 * 2) We need to reserve space to update our inode.
7842 *
7843 * 3) We need to have something to cache all the space that is going to
7844 * be free'd up by the truncate operation, but also have some slack
7845 * space reserved in case it uses space during the truncate (thank you
7846 * very much snapshotting).
7847 *
7848 * And we need these to all be seperate. The fact is we can use alot of
7849 * space doing the truncate, and we have no earthly idea how much space
7850 * we will use, so we need the truncate reservation to be seperate so it
7851 * doesn't end up using space reserved for updating the inode or
7852 * removing the orphan item. We also need to be able to stop the
7853 * transaction and start a new one, which means we need to be able to
7854 * update the inode several times, and we have no idea of knowing how
7855 * many times that will be, so we can't just reserve 1 item for the
7856 * entirety of the opration, so that has to be done seperately as well.
7857 * Then there is the orphan item, which does indeed need to be held on
7858 * to for the whole operation, and we need nobody to touch this reserved
7859 * space except the orphan code.
7860 *
7861 * So that leaves us with
7862 *
7863 * 1) root->orphan_block_rsv - for the orphan deletion.
7864 * 2) rsv - for the truncate reservation, which we will steal from the
7865 * transaction reservation.
7866 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
7867 * updating the inode.
7868 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06007869 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007870 if (!rsv)
7871 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04007872 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04007873 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05007874
Josef Bacik907cbce2011-08-08 13:46:15 -04007875 /*
Josef Bacik07127182011-08-19 10:29:59 -04007876 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04007877 * 1 for updating the inode.
7878 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05007879 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007880 if (IS_ERR(trans)) {
7881 err = PTR_ERR(trans);
7882 goto out;
7883 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05007884
Josef Bacik907cbce2011-08-08 13:46:15 -04007885 /* Migrate the slack space for the truncate to our reserve */
7886 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
7887 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007888 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05007889
Chris Mason5a3f23d2009-03-31 13:27:11 -04007890 /*
7891 * setattr is responsible for setting the ordered_data_close flag,
7892 * but that is only tested during the last file release. That
7893 * could happen well after the next commit, leaving a great big
7894 * window where new writes may get lost if someone chooses to write
7895 * to this file after truncating to zero
7896 *
7897 * The inode doesn't have any dirty data here, and so if we commit
7898 * this is a noop. If someone immediately starts writing to the inode
7899 * it is very likely we'll catch some of their writes in this
7900 * transaction, and the commit will find this file on the ordered
7901 * data list with good things to send down.
7902 *
7903 * This is a best effort solution, there is still a window where
7904 * using truncate to replace the contents of the file will
7905 * end up with a zero length file after a crash.
7906 */
Josef Bacik72ac3c02012-05-23 14:13:11 -04007907 if (inode->i_size == 0 && test_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
7908 &BTRFS_I(inode)->runtime_flags))
Chris Mason5a3f23d2009-03-31 13:27:11 -04007909 btrfs_add_ordered_operation(trans, root, inode);
7910
Josef Bacik5dc562c2012-08-17 13:14:17 -04007911 /*
7912 * So if we truncate and then write and fsync we normally would just
7913 * write the extents that changed, which is a problem if we need to
7914 * first truncate that entire inode. So set this flag so we write out
7915 * all of the extents in the inode to the sync log so we're completely
7916 * safe.
7917 */
7918 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007919 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04007920
Yan, Zheng80825102009-11-12 09:35:36 +00007921 while (1) {
7922 ret = btrfs_truncate_inode_items(trans, root, inode,
7923 inode->i_size,
7924 BTRFS_EXTENT_DATA_KEY);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007925 if (ret != -ENOSPC) {
Josef Bacik3893e332011-01-31 16:03:11 -05007926 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007927 break;
Josef Bacik3893e332011-01-31 16:03:11 -05007928 }
Chris Mason39279cc2007-06-12 06:35:45 -04007929
Josef Bacikfcb80c22011-05-03 10:40:22 -04007930 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007931 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007932 if (ret) {
7933 err = ret;
7934 break;
7935 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04007936
Yan, Zheng80825102009-11-12 09:35:36 +00007937 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007938 btrfs_btree_balance_dirty(root);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007939
7940 trans = btrfs_start_transaction(root, 2);
7941 if (IS_ERR(trans)) {
7942 ret = err = PTR_ERR(trans);
7943 trans = NULL;
7944 break;
7945 }
7946
7947 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
7948 rsv, min_size);
7949 BUG_ON(ret); /* shouldn't happen */
7950 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007951 }
7952
7953 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04007954 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007955 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007956 if (ret)
7957 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007958 }
7959
Chris Mason917c16b2011-11-08 14:49:59 -05007960 if (trans) {
7961 trans->block_rsv = &root->fs_info->trans_block_rsv;
7962 ret = btrfs_update_inode(trans, root, inode);
7963 if (ret && !err)
7964 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04007965
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007966 ret = btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007967 btrfs_btree_balance_dirty(root);
Chris Mason917c16b2011-11-08 14:49:59 -05007968 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04007969
7970out:
7971 btrfs_free_block_rsv(root, rsv);
7972
Josef Bacik3893e332011-01-31 16:03:11 -05007973 if (ret && !err)
7974 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05007975
Josef Bacik3893e332011-01-31 16:03:11 -05007976 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007977}
7978
Sven Wegener3b963622008-06-09 21:57:42 -04007979/*
Chris Masond352ac62008-09-29 15:18:18 -04007980 * create a new subvolume directory/inode (helper for the ioctl).
7981 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05007982int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00007983 struct btrfs_root *new_root,
7984 struct btrfs_root *parent_root,
7985 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04007986{
Chris Mason39279cc2007-06-12 06:35:45 -04007987 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04007988 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007989 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007990
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01007991 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
7992 new_dirid, new_dirid,
7993 S_IFDIR | (~current_umask() & S_IRWXUGO),
7994 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04007995 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04007996 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007997 inode->i_op = &btrfs_dir_inode_operations;
7998 inode->i_fop = &btrfs_dir_file_operations;
7999
Miklos Szeredibfe86842011-10-28 14:13:29 +02008000 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04008001 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04008002
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008003 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
8004 if (err)
8005 btrfs_err(new_root->fs_info,
8006 "error inheriting subvolume %llu properties: %d\n",
8007 new_root->root_key.objectid, err);
8008
Yan, Zheng76dda932009-09-21 16:00:26 -04008009 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04008010
Yan, Zheng76dda932009-09-21 16:00:26 -04008011 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07008012 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04008013}
8014
Chris Mason39279cc2007-06-12 06:35:45 -04008015struct inode *btrfs_alloc_inode(struct super_block *sb)
8016{
8017 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008018 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008019
8020 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
8021 if (!ei)
8022 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008023
8024 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008025 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04008026 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04008027 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04008028 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008029 ei->delalloc_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008030 ei->disk_i_size = 0;
8031 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04008032 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008033 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08008034 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008035 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06008036 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008037
Josef Bacik9e0baf62011-07-15 15:16:44 +00008038 spin_lock_init(&ei->lock);
8039 ei->outstanding_extents = 0;
8040 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008041
Josef Bacik72ac3c02012-05-23 14:13:11 -04008042 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08008043 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008044
Miao Xie16cdcec2011-04-22 18:12:22 +08008045 ei->delayed_node = NULL;
8046
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008047 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02008048 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02008049 extent_io_tree_init(&ei->io_tree, &inode->i_data);
8050 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04008051 ei->io_tree.track_uptodate = 1;
8052 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05008053 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008054 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05008055 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008056 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008057 INIT_LIST_HEAD(&ei->delalloc_inodes);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008058 INIT_LIST_HEAD(&ei->ordered_operations);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008059 RB_CLEAR_NODE(&ei->rb_node);
8060
8061 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008062}
8063
Josef Bacikaaedb552013-10-11 14:44:09 -04008064#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
8065void btrfs_test_destroy_inode(struct inode *inode)
8066{
8067 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
8068 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8069}
8070#endif
8071
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008072static void btrfs_i_callback(struct rcu_head *head)
8073{
8074 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008075 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8076}
8077
Chris Mason39279cc2007-06-12 06:35:45 -04008078void btrfs_destroy_inode(struct inode *inode)
8079{
Chris Masone6dcd2d2008-07-17 12:53:50 -04008080 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008081 struct btrfs_root *root = BTRFS_I(inode)->root;
8082
Al Virob3d9b7a2012-06-09 13:51:19 -04008083 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04008084 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00008085 WARN_ON(BTRFS_I(inode)->outstanding_extents);
8086 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04008087 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
8088 WARN_ON(BTRFS_I(inode)->csum_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04008089
Chris Mason5a3f23d2009-03-31 13:27:11 -04008090 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05008091 * This can happen where we create an inode, but somebody else also
8092 * created the same inode and we need to destroy the one we already
8093 * created.
8094 */
8095 if (!root)
8096 goto free;
8097
8098 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04008099 * Make sure we're properly removed from the ordered operation
8100 * lists.
8101 */
8102 smp_mb();
8103 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
Miao Xie199c2a92013-05-15 07:48:23 +00008104 spin_lock(&root->fs_info->ordered_root_lock);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008105 list_del_init(&BTRFS_I(inode)->ordered_operations);
Miao Xie199c2a92013-05-15 07:48:23 +00008106 spin_unlock(&root->fs_info->ordered_root_lock);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008107 }
8108
Josef Bacik8a35d952012-05-23 14:26:42 -04008109 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
8110 &BTRFS_I(inode)->runtime_flags)) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008111 btrfs_info(root->fs_info, "inode %llu still on the orphan list",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02008112 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04008113 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04008114 }
Josef Bacik7b128762008-07-24 12:17:14 -04008115
Chris Masond3977122009-01-05 21:25:51 -05008116 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008117 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
8118 if (!ordered)
8119 break;
8120 else {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008121 btrfs_err(root->fs_info, "found ordered extent %llu %llu on inode cleanup",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02008122 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008123 btrfs_remove_ordered_extent(inode, ordered);
8124 btrfs_put_ordered_extent(ordered);
8125 btrfs_put_ordered_extent(ordered);
8126 }
8127 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008128 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008129 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05008130free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008131 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04008132}
8133
Al Viro45321ac2010-06-07 13:43:19 -04008134int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04008135{
8136 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04008137
Naohiro Aota6379ef92013-06-06 09:56:34 +00008138 if (root == NULL)
8139 return 1;
8140
Liu Bofa6ac872013-02-20 14:10:23 +00008141 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02008142 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04008143 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04008144 else
Al Viro45321ac2010-06-07 13:43:19 -04008145 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04008146}
8147
Sven Wegener0ee0fda2008-07-30 16:54:26 -04008148static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04008149{
8150 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
8151
8152 inode_init_once(&ei->vfs_inode);
8153}
8154
8155void btrfs_destroy_cachep(void)
8156{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10008157 /*
8158 * Make sure all delayed rcu free inodes are flushed before we
8159 * destroy cache.
8160 */
8161 rcu_barrier();
Chris Mason39279cc2007-06-12 06:35:45 -04008162 if (btrfs_inode_cachep)
8163 kmem_cache_destroy(btrfs_inode_cachep);
8164 if (btrfs_trans_handle_cachep)
8165 kmem_cache_destroy(btrfs_trans_handle_cachep);
8166 if (btrfs_transaction_cachep)
8167 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008168 if (btrfs_path_cachep)
8169 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05008170 if (btrfs_free_space_cachep)
8171 kmem_cache_destroy(btrfs_free_space_cachep);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008172 if (btrfs_delalloc_work_cachep)
8173 kmem_cache_destroy(btrfs_delalloc_work_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008174}
8175
8176int btrfs_init_cachep(void)
8177{
David Sterba837e1972012-09-07 03:00:48 -06008178 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008179 sizeof(struct btrfs_inode), 0,
8180 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04008181 if (!btrfs_inode_cachep)
8182 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008183
David Sterba837e1972012-09-07 03:00:48 -06008184 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008185 sizeof(struct btrfs_trans_handle), 0,
8186 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008187 if (!btrfs_trans_handle_cachep)
8188 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008189
David Sterba837e1972012-09-07 03:00:48 -06008190 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008191 sizeof(struct btrfs_transaction), 0,
8192 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008193 if (!btrfs_transaction_cachep)
8194 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008195
David Sterba837e1972012-09-07 03:00:48 -06008196 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008197 sizeof(struct btrfs_path), 0,
8198 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008199 if (!btrfs_path_cachep)
8200 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008201
David Sterba837e1972012-09-07 03:00:48 -06008202 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05008203 sizeof(struct btrfs_free_space), 0,
8204 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
8205 if (!btrfs_free_space_cachep)
8206 goto fail;
8207
Miao Xie8ccf6f192012-10-25 09:28:04 +00008208 btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
8209 sizeof(struct btrfs_delalloc_work), 0,
8210 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
8211 NULL);
8212 if (!btrfs_delalloc_work_cachep)
8213 goto fail;
8214
Chris Mason39279cc2007-06-12 06:35:45 -04008215 return 0;
8216fail:
8217 btrfs_destroy_cachep();
8218 return -ENOMEM;
8219}
8220
8221static int btrfs_getattr(struct vfsmount *mnt,
8222 struct dentry *dentry, struct kstat *stat)
8223{
Miao Xiedf0af1a2013-01-29 10:11:59 +00008224 u64 delalloc_bytes;
Chris Mason39279cc2007-06-12 06:35:45 -04008225 struct inode *inode = dentry->d_inode;
David Sterbafadc0d82011-11-20 07:33:38 -05008226 u32 blocksize = inode->i_sb->s_blocksize;
8227
Chris Mason39279cc2007-06-12 06:35:45 -04008228 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04008229 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05008230 stat->blksize = PAGE_CACHE_SIZE;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008231
8232 spin_lock(&BTRFS_I(inode)->lock);
8233 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
8234 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05008235 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00008236 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04008237 return 0;
8238}
8239
Chris Masond3977122009-01-05 21:25:51 -05008240static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
8241 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04008242{
8243 struct btrfs_trans_handle *trans;
8244 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008245 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04008246 struct inode *new_inode = new_dentry->d_inode;
8247 struct inode *old_inode = old_dentry->d_inode;
8248 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008249 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008250 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04008251 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08008252 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008253
Li Zefan33345d012011-04-20 10:31:50 +08008254 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04008255 return -EPERM;
8256
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008257 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08008258 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05008259 return -EXDEV;
8260
Li Zefan33345d012011-04-20 10:31:50 +08008261 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
8262 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008263 return -ENOTEMPTY;
8264
Chris Mason39279cc2007-06-12 06:35:45 -04008265 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008266 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04008267 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05008268
8269
8270 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04008271 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05008272 new_dentry->d_name.name,
8273 new_dentry->d_name.len);
8274
8275 if (ret) {
8276 if (ret == -EEXIST) {
8277 /* we shouldn't get
8278 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05308279 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05008280 return ret;
8281 }
8282 } else {
8283 /* maybe -EOVERFLOW */
8284 return ret;
8285 }
8286 }
8287 ret = 0;
8288
Chris Mason5a3f23d2009-03-31 13:27:11 -04008289 /*
8290 * we're using rename to replace one file with another.
8291 * and the replacement file is large. Start IO on it now so
8292 * we don't add too much work to the end of the transaction
8293 */
Bartlomiej Zolnierkiewicz4baf8c92009-08-07 13:47:08 -04008294 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
Chris Mason5a3f23d2009-03-31 13:27:11 -04008295 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
8296 filemap_flush(old_inode->i_mapping);
8297
Yan, Zheng76dda932009-09-21 16:00:26 -04008298 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08008299 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008300 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008301 /*
8302 * We want to reserve the absolute worst case amount of items. So if
8303 * both inodes are subvols and we need to unlink them then that would
8304 * require 4 item modifications, but if they are both normal inodes it
8305 * would require 5 item modifications, so we'll assume their normal
8306 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
8307 * should cover the worst case number of items we'll modify.
8308 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04008309 trans = btrfs_start_transaction(root, 11);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008310 if (IS_ERR(trans)) {
8311 ret = PTR_ERR(trans);
8312 goto out_notrans;
8313 }
Chris Mason5f39d392007-10-15 16:14:19 -04008314
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008315 if (dest != root)
8316 btrfs_record_root_in_trans(trans, dest);
8317
Yan, Zhenga5719522009-09-24 09:17:31 -04008318 ret = btrfs_set_inode_index(new_dir, &index);
8319 if (ret)
8320 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008321
Miao Xie67de1172013-12-26 13:07:06 +08008322 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08008323 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008324 /* force full log commit if subvolume involved. */
8325 root->fs_info->last_trans_log_full_commit = trans->transid;
8326 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04008327 ret = btrfs_insert_inode_ref(trans, dest,
8328 new_dentry->d_name.name,
8329 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08008330 old_ino,
8331 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04008332 if (ret)
8333 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008334 /*
8335 * this is an ugly little race, but the rename is required
8336 * to make sure that if we crash, the inode is either at the
8337 * old name or the new one. pinning the log transaction lets
8338 * us make sure we don't allow a log commit to come in after
8339 * we unlink the name but before we add the new name back in.
8340 */
8341 btrfs_pin_log_trans(root);
8342 }
Chris Mason12fcfd22009-03-24 10:24:20 -04008343 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04008344 * make sure the inode gets flushed if it is replacing
8345 * something.
8346 */
Li Zefan33345d012011-04-20 10:31:50 +08008347 if (new_inode && new_inode->i_size && S_ISREG(old_inode->i_mode))
Chris Mason5a3f23d2009-03-31 13:27:11 -04008348 btrfs_add_ordered_operation(trans, root, old_inode);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008349
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008350 inode_inc_iversion(old_dir);
8351 inode_inc_iversion(new_dir);
8352 inode_inc_iversion(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008353 old_dir->i_ctime = old_dir->i_mtime = ctime;
8354 new_dir->i_ctime = new_dir->i_mtime = ctime;
8355 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04008356
Chris Mason12fcfd22009-03-24 10:24:20 -04008357 if (old_dentry->d_parent != new_dentry->d_parent)
8358 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
8359
Li Zefan33345d012011-04-20 10:31:50 +08008360 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008361 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
8362 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
8363 old_dentry->d_name.name,
8364 old_dentry->d_name.len);
8365 } else {
Al Viro92986792011-03-04 17:14:37 +00008366 ret = __btrfs_unlink_inode(trans, root, old_dir,
8367 old_dentry->d_inode,
8368 old_dentry->d_name.name,
8369 old_dentry->d_name.len);
8370 if (!ret)
8371 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008372 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008373 if (ret) {
8374 btrfs_abort_transaction(trans, root, ret);
8375 goto out_fail;
8376 }
Chris Mason39279cc2007-06-12 06:35:45 -04008377
8378 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008379 inode_inc_iversion(new_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008380 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08008381 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008382 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
8383 root_objectid = BTRFS_I(new_inode)->location.objectid;
8384 ret = btrfs_unlink_subvol(trans, dest, new_dir,
8385 root_objectid,
8386 new_dentry->d_name.name,
8387 new_dentry->d_name.len);
8388 BUG_ON(new_inode->i_nlink == 0);
8389 } else {
8390 ret = btrfs_unlink_inode(trans, dest, new_dir,
8391 new_dentry->d_inode,
8392 new_dentry->d_name.name,
8393 new_dentry->d_name.len);
8394 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04008395 if (!ret && new_inode->i_nlink == 0)
Chris Masone02119d2008-09-05 16:13:11 -04008396 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008397 if (ret) {
8398 btrfs_abort_transaction(trans, root, ret);
8399 goto out_fail;
8400 }
Chris Mason39279cc2007-06-12 06:35:45 -04008401 }
Josef Bacikaec74772008-07-24 12:12:38 -04008402
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008403 ret = btrfs_add_link(trans, new_dir, old_inode,
8404 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04008405 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008406 if (ret) {
8407 btrfs_abort_transaction(trans, root, ret);
8408 goto out_fail;
8409 }
Chris Mason39279cc2007-06-12 06:35:45 -04008410
Miao Xie67de1172013-12-26 13:07:06 +08008411 if (old_inode->i_nlink == 1)
8412 BTRFS_I(old_inode)->dir_index = index;
8413
Li Zefan33345d012011-04-20 10:31:50 +08008414 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04008415 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00008416 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008417 btrfs_end_log_trans(root);
8418 }
Chris Mason39279cc2007-06-12 06:35:45 -04008419out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008420 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008421out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08008422 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008423 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008424
Chris Mason39279cc2007-06-12 06:35:45 -04008425 return ret;
8426}
8427
Miao Xie8ccf6f192012-10-25 09:28:04 +00008428static void btrfs_run_delalloc_work(struct btrfs_work *work)
8429{
8430 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04008431 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008432
8433 delalloc_work = container_of(work, struct btrfs_delalloc_work,
8434 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04008435 inode = delalloc_work->inode;
8436 if (delalloc_work->wait) {
8437 btrfs_wait_ordered_range(inode, 0, (u64)-1);
8438 } else {
8439 filemap_flush(inode->i_mapping);
8440 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8441 &BTRFS_I(inode)->runtime_flags))
8442 filemap_flush(inode->i_mapping);
8443 }
Miao Xie8ccf6f192012-10-25 09:28:04 +00008444
8445 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -04008446 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008447 else
Josef Bacik9f23e282013-10-28 15:03:41 -04008448 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008449 complete(&delalloc_work->completion);
8450}
8451
8452struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
8453 int wait, int delay_iput)
8454{
8455 struct btrfs_delalloc_work *work;
8456
8457 work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
8458 if (!work)
8459 return NULL;
8460
8461 init_completion(&work->completion);
8462 INIT_LIST_HEAD(&work->list);
8463 work->inode = inode;
8464 work->wait = wait;
8465 work->delay_iput = delay_iput;
Qu Wenruoa44903a2014-02-28 10:46:09 +08008466 btrfs_init_work(&work->work, btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008467
8468 return work;
8469}
8470
8471void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
8472{
8473 wait_for_completion(&work->completion);
8474 kmem_cache_free(btrfs_delalloc_work_cachep, work);
8475}
8476
Chris Masond352ac62008-09-29 15:18:18 -04008477/*
8478 * some fairly slow code that needs optimization. This walks the list
8479 * of all the inodes with pending delalloc and forces them to disk.
8480 */
Miao Xie6c255e62014-03-06 13:55:01 +08008481static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
8482 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -04008483{
Chris Masonea8c2812008-08-04 23:17:27 -04008484 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008485 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008486 struct btrfs_delalloc_work *work, *next;
8487 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00008488 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008489 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04008490
Miao Xie8ccf6f192012-10-25 09:28:04 +00008491 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008492 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00008493
Miao Xie573bfb72014-03-06 13:55:03 +08008494 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00008495 spin_lock(&root->delalloc_lock);
8496 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008497 while (!list_empty(&splice)) {
8498 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04008499 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008500
Miao Xieeb73c1b2013-05-15 07:48:22 +00008501 list_move_tail(&binode->delalloc_inodes,
8502 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008503 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00008504 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00008505 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008506 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008507 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008508 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008509
8510 work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
8511 if (unlikely(!work)) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -04008512 if (delay_iput)
8513 btrfs_add_delayed_iput(inode);
8514 else
8515 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008516 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +08008517 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008518 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008519 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +08008520 btrfs_queue_work(root->fs_info->flush_workers,
8521 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +08008522 ret++;
8523 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +08008524 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008525 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00008526 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04008527 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008528 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee1d2008-09-29 11:19:10 -04008529
Wang Shilonga1ecaab2014-04-02 19:53:32 +08008530out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00008531 list_for_each_entry_safe(work, next, &works, list) {
8532 list_del_init(&work->list);
8533 btrfs_wait_and_free_delalloc_work(work);
8534 }
8535
Miao Xieeb73c1b2013-05-15 07:48:22 +00008536 if (!list_empty_careful(&splice)) {
8537 spin_lock(&root->delalloc_lock);
8538 list_splice_tail(&splice, &root->delalloc_inodes);
8539 spin_unlock(&root->delalloc_lock);
8540 }
Miao Xie573bfb72014-03-06 13:55:03 +08008541 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00008542 return ret;
8543}
8544
8545int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
8546{
8547 int ret;
8548
Wang Shilong2c21b4d2014-01-14 19:42:20 +08008549 if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00008550 return -EROFS;
8551
Miao Xie6c255e62014-03-06 13:55:01 +08008552 ret = __start_delalloc_inodes(root, delay_iput, -1);
8553 if (ret > 0)
8554 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +00008555 /*
8556 * the filemap_flush will queue IO into the worker threads, but
Chris Mason8c8bee1d2008-09-29 11:19:10 -04008557 * we have to make sure the IO is actually started and that
8558 * ordered extents get created before we return
8559 */
8560 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05008561 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05008562 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee1d2008-09-29 11:19:10 -04008563 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05008564 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
8565 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee1d2008-09-29 11:19:10 -04008566 }
8567 atomic_dec(&root->fs_info->async_submit_draining);
Miao Xieeb73c1b2013-05-15 07:48:22 +00008568 return ret;
8569}
8570
Miao Xie6c255e62014-03-06 13:55:01 +08008571int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
8572 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +00008573{
8574 struct btrfs_root *root;
8575 struct list_head splice;
8576 int ret;
8577
Wang Shilong2c21b4d2014-01-14 19:42:20 +08008578 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00008579 return -EROFS;
8580
8581 INIT_LIST_HEAD(&splice);
8582
Miao Xie573bfb72014-03-06 13:55:03 +08008583 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00008584 spin_lock(&fs_info->delalloc_root_lock);
8585 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +08008586 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00008587 root = list_first_entry(&splice, struct btrfs_root,
8588 delalloc_root);
8589 root = btrfs_grab_fs_root(root);
8590 BUG_ON(!root);
8591 list_move_tail(&root->delalloc_root,
8592 &fs_info->delalloc_roots);
8593 spin_unlock(&fs_info->delalloc_root_lock);
8594
Miao Xie6c255e62014-03-06 13:55:01 +08008595 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +00008596 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +08008597 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +00008598 goto out;
8599
Miao Xie6c255e62014-03-06 13:55:01 +08008600 if (nr != -1) {
8601 nr -= ret;
8602 WARN_ON(nr < 0);
8603 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008604 spin_lock(&fs_info->delalloc_root_lock);
8605 }
8606 spin_unlock(&fs_info->delalloc_root_lock);
8607
Miao Xie6c255e62014-03-06 13:55:01 +08008608 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +00008609 atomic_inc(&fs_info->async_submit_draining);
8610 while (atomic_read(&fs_info->nr_async_submits) ||
8611 atomic_read(&fs_info->async_delalloc_pages)) {
8612 wait_event(fs_info->async_submit_wait,
8613 (atomic_read(&fs_info->nr_async_submits) == 0 &&
8614 atomic_read(&fs_info->async_delalloc_pages) == 0));
8615 }
8616 atomic_dec(&fs_info->async_submit_draining);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008617out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00008618 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00008619 spin_lock(&fs_info->delalloc_root_lock);
8620 list_splice_tail(&splice, &fs_info->delalloc_roots);
8621 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008622 }
Miao Xie573bfb72014-03-06 13:55:03 +08008623 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008624 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04008625}
8626
Chris Mason39279cc2007-06-12 06:35:45 -04008627static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
8628 const char *symname)
8629{
8630 struct btrfs_trans_handle *trans;
8631 struct btrfs_root *root = BTRFS_I(dir)->root;
8632 struct btrfs_path *path;
8633 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05008634 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04008635 int err;
8636 int drop_inode = 0;
8637 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05308638 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008639 int name_len;
8640 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04008641 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04008642 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04008643 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04008644
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01008645 name_len = strlen(symname);
Chris Mason39279cc2007-06-12 06:35:45 -04008646 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
8647 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05008648
Josef Bacik9ed74f22009-09-11 16:12:44 -04008649 /*
8650 * 2 items for inode item and ref
8651 * 2 items for dir items
8652 * 1 item for xattr if selinux is on
8653 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04008654 trans = btrfs_start_transaction(root, 5);
8655 if (IS_ERR(trans))
8656 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05008657
Li Zefan581bb052011-04-20 10:06:11 +08008658 err = btrfs_find_free_ino(root, &objectid);
8659 if (err)
8660 goto out_unlock;
8661
Josef Bacikaec74772008-07-24 12:12:38 -04008662 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08008663 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04008664 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008665 if (IS_ERR(inode)) {
8666 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008667 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008668 }
Chris Mason39279cc2007-06-12 06:35:45 -04008669
Eric Paris2a7dba32011-02-01 11:05:39 -05008670 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04008671 if (err) {
8672 drop_inode = 1;
8673 goto out_unlock;
8674 }
8675
Casey Schauflerad19db72011-12-15 10:09:07 -05008676 /*
8677 * If the active LSM wants to access the inode during
8678 * d_instantiate it needs these. Smack checks to see
8679 * if the filesystem supports xattrs by looking at the
8680 * ops vector.
8681 */
8682 inode->i_fop = &btrfs_file_operations;
8683 inode->i_op = &btrfs_file_inode_operations;
8684
Josef Bacika1b075d2010-11-19 20:36:11 +00008685 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04008686 if (err)
8687 drop_inode = 1;
8688 else {
8689 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04008690 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05008691 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04008692 }
Chris Mason39279cc2007-06-12 06:35:45 -04008693 if (drop_inode)
8694 goto out_unlock;
8695
8696 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07008697 if (!path) {
8698 err = -ENOMEM;
8699 drop_inode = 1;
8700 goto out_unlock;
8701 }
Li Zefan33345d012011-04-20 10:31:50 +08008702 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008703 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008704 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
8705 datasize = btrfs_file_extent_calc_inline_size(name_len);
8706 err = btrfs_insert_empty_item(trans, root, path, &key,
8707 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04008708 if (err) {
8709 drop_inode = 1;
Julia Lawallb0839162011-05-14 07:10:51 +00008710 btrfs_free_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04008711 goto out_unlock;
8712 }
Chris Mason5f39d392007-10-15 16:14:19 -04008713 leaf = path->nodes[0];
8714 ei = btrfs_item_ptr(leaf, path->slots[0],
8715 struct btrfs_file_extent_item);
8716 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
8717 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04008718 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04008719 btrfs_set_file_extent_encryption(leaf, ei, 0);
8720 btrfs_set_file_extent_compression(leaf, ei, 0);
8721 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
8722 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
8723
Chris Mason39279cc2007-06-12 06:35:45 -04008724 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04008725 write_extent_buffer(leaf, symname, ptr, name_len);
8726 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04008727 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04008728
Chris Mason39279cc2007-06-12 06:35:45 -04008729 inode->i_op = &btrfs_symlink_inode_operations;
8730 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04008731 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04008732 inode_set_bytes(inode, name_len);
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01008733 btrfs_i_size_write(inode, name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -04008734 err = btrfs_update_inode(trans, root, inode);
8735 if (err)
8736 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04008737
8738out_unlock:
Al Viro08c422c2011-12-23 07:58:13 -05008739 if (!err)
8740 d_instantiate(dentry, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008741 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04008742 if (drop_inode) {
8743 inode_dec_link_count(inode);
8744 iput(inode);
8745 }
Liu Bob53d3f52012-11-14 14:34:34 +00008746 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04008747 return err;
8748}
Chris Mason16432982008-04-10 10:23:21 -04008749
Josef Bacik0af3d002010-06-21 14:48:16 -04008750static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
8751 u64 start, u64 num_bytes, u64 min_size,
8752 loff_t actual_len, u64 *alloc_hint,
8753 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04008754{
Josef Bacik5dc562c2012-08-17 13:14:17 -04008755 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
8756 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04008757 struct btrfs_root *root = BTRFS_I(inode)->root;
8758 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04008759 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00008760 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05008761 u64 cur_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04008762 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04008763 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04008764
Josef Bacik0af3d002010-06-21 14:48:16 -04008765 if (trans)
8766 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04008767 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008768 if (own_trans) {
8769 trans = btrfs_start_transaction(root, 3);
8770 if (IS_ERR(trans)) {
8771 ret = PTR_ERR(trans);
8772 break;
8773 }
Yan Zhengd899e052008-10-30 14:25:28 -04008774 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00008775
Chris Mason154ea282013-03-05 11:11:26 -05008776 cur_bytes = min(num_bytes, 256ULL * 1024 * 1024);
8777 cur_bytes = max(cur_bytes, min_size);
Josef Bacik00361582013-08-14 14:02:47 -04008778 ret = btrfs_reserve_extent(root, cur_bytes, min_size, 0,
8779 *alloc_hint, &ins, 1);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008780 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008781 if (own_trans)
8782 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008783 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00008784 }
8785
Yan Zhengd899e052008-10-30 14:25:28 -04008786 ret = insert_reserved_file_extent(trans, inode,
8787 cur_offset, ins.objectid,
8788 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00008789 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04008790 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008791 if (ret) {
Josef Bacik857cc2f2013-10-07 15:21:08 -04008792 btrfs_free_reserved_extent(root, ins.objectid,
8793 ins.offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008794 btrfs_abort_transaction(trans, root, ret);
8795 if (own_trans)
8796 btrfs_end_transaction(trans, root);
8797 break;
8798 }
Chris Masona1ed8352009-09-11 12:27:37 -04008799 btrfs_drop_extent_cache(inode, cur_offset,
8800 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008801
Josef Bacik5dc562c2012-08-17 13:14:17 -04008802 em = alloc_extent_map();
8803 if (!em) {
8804 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
8805 &BTRFS_I(inode)->runtime_flags);
8806 goto next;
8807 }
8808
8809 em->start = cur_offset;
8810 em->orig_start = cur_offset;
8811 em->len = ins.offset;
8812 em->block_start = ins.objectid;
8813 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05008814 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04008815 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008816 em->bdev = root->fs_info->fs_devices->latest_bdev;
8817 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
8818 em->generation = trans->transid;
8819
8820 while (1) {
8821 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04008822 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04008823 write_unlock(&em_tree->lock);
8824 if (ret != -EEXIST)
8825 break;
8826 btrfs_drop_extent_cache(inode, cur_offset,
8827 cur_offset + ins.offset - 1,
8828 0);
8829 }
8830 free_extent_map(em);
8831next:
Yan Zhengd899e052008-10-30 14:25:28 -04008832 num_bytes -= ins.offset;
8833 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04008834 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00008835
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008836 inode_inc_iversion(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04008837 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02008838 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04008839 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04008840 (actual_len > inode->i_size) &&
8841 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00008842 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00008843 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00008844 else
Josef Bacik55a61d12010-11-22 18:50:32 +00008845 i_size = cur_offset;
8846 i_size_write(inode, i_size);
8847 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008848 }
8849
Yan Zhengd899e052008-10-30 14:25:28 -04008850 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008851
8852 if (ret) {
8853 btrfs_abort_transaction(trans, root, ret);
8854 if (own_trans)
8855 btrfs_end_transaction(trans, root);
8856 break;
8857 }
Yan Zhengd899e052008-10-30 14:25:28 -04008858
Josef Bacik0af3d002010-06-21 14:48:16 -04008859 if (own_trans)
8860 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008861 }
Yan Zhengd899e052008-10-30 14:25:28 -04008862 return ret;
8863}
8864
Josef Bacik0af3d002010-06-21 14:48:16 -04008865int btrfs_prealloc_file_range(struct inode *inode, int mode,
8866 u64 start, u64 num_bytes, u64 min_size,
8867 loff_t actual_len, u64 *alloc_hint)
8868{
8869 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
8870 min_size, actual_len, alloc_hint,
8871 NULL);
8872}
8873
8874int btrfs_prealloc_file_range_trans(struct inode *inode,
8875 struct btrfs_trans_handle *trans, int mode,
8876 u64 start, u64 num_bytes, u64 min_size,
8877 loff_t actual_len, u64 *alloc_hint)
8878{
8879 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
8880 min_size, actual_len, alloc_hint, trans);
8881}
8882
Chris Masone6dcd2d2008-07-17 12:53:50 -04008883static int btrfs_set_page_dirty(struct page *page)
8884{
Chris Masone6dcd2d2008-07-17 12:53:50 -04008885 return __set_page_dirty_nobuffers(page);
8886}
8887
Al Viro10556cb2011-06-20 19:28:19 -04008888static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05008889{
Li Zefanb83cc962010-12-20 16:04:08 +08008890 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00008891 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08008892
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00008893 if (mask & MAY_WRITE &&
8894 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
8895 if (btrfs_root_readonly(root))
8896 return -EROFS;
8897 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
8898 return -EACCES;
8899 }
Al Viro2830ba72011-06-20 19:16:29 -04008900 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05008901}
Chris Mason39279cc2007-06-12 06:35:45 -04008902
Filipe Mananaef3b9af2014-04-27 20:40:45 +01008903static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
8904{
8905 struct btrfs_trans_handle *trans;
8906 struct btrfs_root *root = BTRFS_I(dir)->root;
8907 struct inode *inode = NULL;
8908 u64 objectid;
8909 u64 index;
8910 int ret = 0;
8911
8912 /*
8913 * 5 units required for adding orphan entry
8914 */
8915 trans = btrfs_start_transaction(root, 5);
8916 if (IS_ERR(trans))
8917 return PTR_ERR(trans);
8918
8919 ret = btrfs_find_free_ino(root, &objectid);
8920 if (ret)
8921 goto out;
8922
8923 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
8924 btrfs_ino(dir), objectid, mode, &index);
8925 if (IS_ERR(inode)) {
8926 ret = PTR_ERR(inode);
8927 inode = NULL;
8928 goto out;
8929 }
8930
8931 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
8932 if (ret)
8933 goto out;
8934
8935 ret = btrfs_update_inode(trans, root, inode);
8936 if (ret)
8937 goto out;
8938
8939 inode->i_fop = &btrfs_file_operations;
8940 inode->i_op = &btrfs_file_inode_operations;
8941
8942 inode->i_mapping->a_ops = &btrfs_aops;
8943 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
8944 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
8945
8946 ret = btrfs_orphan_add(trans, inode);
8947 if (ret)
8948 goto out;
8949
8950 d_tmpfile(dentry, inode);
8951 mark_inode_dirty(inode);
8952
8953out:
8954 btrfs_end_transaction(trans, root);
8955 if (ret)
8956 iput(inode);
8957 btrfs_balance_delayed_items(root);
8958 btrfs_btree_balance_dirty(root);
8959
8960 return ret;
8961}
8962
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008963static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05008964 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04008965 .lookup = btrfs_lookup,
8966 .create = btrfs_create,
8967 .unlink = btrfs_unlink,
8968 .link = btrfs_link,
8969 .mkdir = btrfs_mkdir,
8970 .rmdir = btrfs_rmdir,
8971 .rename = btrfs_rename,
8972 .symlink = btrfs_symlink,
8973 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04008974 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008975 .setxattr = btrfs_setxattr,
8976 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05008977 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008978 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05008979 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008980 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08008981 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -07008982 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +01008983 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -04008984};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008985static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008986 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05008987 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008988 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08008989 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -07008990 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008991};
Yan, Zheng76dda932009-09-21 16:00:26 -04008992
Alexey Dobriyan828c0952009-10-01 15:43:56 -07008993static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008994 .llseek = generic_file_llseek,
8995 .read = generic_read_dir,
Al Viro9cdda8d2013-05-22 16:48:09 -04008996 .iterate = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04008997 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04008998#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04008999 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04009000#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04009001 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04009002 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04009003};
9004
Chris Masond1310b22008-01-24 16:13:08 -05009005static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04009006 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05009007 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04009008 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04009009 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04009010 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04009011 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05009012 .set_bit_hook = btrfs_set_bit_hook,
9013 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04009014 .merge_extent_hook = btrfs_merge_extent_hook,
9015 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04009016};
9017
Chris Mason35054392009-01-21 13:11:13 -05009018/*
9019 * btrfs doesn't support the bmap operation because swapfiles
9020 * use bmap to make a mapping of extents in the file. They assume
9021 * these extents won't change over the life of the file and they
9022 * use the bmap result to do IO directly to the drive.
9023 *
9024 * the btrfs bmap call would return logical addresses that aren't
9025 * suitable for IO and they also will change frequently as COW
9026 * operations happen. So, swapfile + btrfs == corruption.
9027 *
9028 * For now we're avoiding this by dropping bmap.
9029 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07009030static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04009031 .readpage = btrfs_readpage,
9032 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04009033 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05009034 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04009035 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04009036 .invalidatepage = btrfs_invalidatepage,
9037 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04009038 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02009039 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04009040};
9041
Alexey Dobriyan7f094102009-09-21 17:01:10 -07009042static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04009043 .readpage = btrfs_readpage,
9044 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04009045 .invalidatepage = btrfs_invalidatepage,
9046 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04009047};
9048
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009049static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009050 .getattr = btrfs_getattr,
9051 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009052 .setxattr = btrfs_setxattr,
9053 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05009054 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009055 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05009056 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05009057 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009058 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009059 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04009060 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04009061};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009062static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04009063 .getattr = btrfs_getattr,
9064 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05009065 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009066 .setxattr = btrfs_setxattr,
9067 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04009068 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009069 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009070 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009071 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04009072 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -04009073};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009074static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009075 .readlink = generic_readlink,
9076 .follow_link = page_follow_link_light,
9077 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00009078 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -05009079 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05009080 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05009081 .setxattr = btrfs_setxattr,
9082 .getxattr = btrfs_getxattr,
9083 .listxattr = btrfs_listxattr,
9084 .removexattr = btrfs_removexattr,
Josef Bacike41f9412012-03-26 09:46:47 -04009085 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04009086};
Yan, Zheng76dda932009-09-21 16:00:26 -04009087
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04009088const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04009089 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -04009090 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -04009091};