blob: 17800221978f0cf08c9a4b36fb350bef806be11f [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"
Chris Mason39279cc2007-06-12 06:35:45 -040061
62struct btrfs_iget_args {
63 u64 ino;
64 struct btrfs_root *root;
65};
66
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070067static const struct inode_operations btrfs_dir_inode_operations;
68static const struct inode_operations btrfs_symlink_inode_operations;
69static const struct inode_operations btrfs_dir_ro_inode_operations;
70static const struct inode_operations btrfs_special_inode_operations;
71static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070072static const struct address_space_operations btrfs_aops;
73static const struct address_space_operations btrfs_symlink_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070074static const struct file_operations btrfs_dir_file_operations;
Chris Masond1310b22008-01-24 16:13:08 -050075static struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040076
77static struct kmem_cache *btrfs_inode_cachep;
Miao Xie8ccf6f192012-10-25 09:28:04 +000078static struct kmem_cache *btrfs_delalloc_work_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040079struct kmem_cache *btrfs_trans_handle_cachep;
80struct kmem_cache *btrfs_transaction_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040081struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050082struct kmem_cache *btrfs_free_space_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040083
84#define S_SHIFT 12
85static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
86 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
87 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
88 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
89 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
90 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
91 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
92 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
93};
94
Eric Sandeen3972f262013-01-12 02:57:22 +000095static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Josef Bacika41ad392011-01-31 15:30:16 -050096static int btrfs_truncate(struct inode *inode);
Josef Bacik5fd02042012-05-02 14:00:54 -040097static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Chris Mason771ed682008-11-06 22:02:51 -050098static noinline int cow_file_range(struct inode *inode,
99 struct page *locked_page,
100 u64 start, u64 end, int *page_started,
101 unsigned long *nr_written, int unlock);
Josef Bacik70c8a912012-10-11 16:54:30 -0400102static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
103 u64 len, u64 orig_start,
104 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -0400105 u64 orig_block_len, u64 ram_bytes,
106 int type);
Josef Bacik7b128762008-07-24 12:17:14 -0400107
Eric Sandeen48a3b632013-04-25 20:41:01 +0000108static int btrfs_dirty_inode(struct inode *inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400109
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000110static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500111 struct inode *inode, struct inode *dir,
112 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500113{
114 int err;
115
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000116 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500117 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500118 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500119 return err;
120}
121
Chris Masond352ac62008-09-29 15:18:18 -0400122/*
Chris Masonc8b97812008-10-29 14:49:59 -0400123 * this does all the hard work for inserting an inline extent into
124 * the btree. The caller should have done a btrfs_drop_extents so that
125 * no overlapping inline items exist in the btree
126 */
Chris Masond3977122009-01-05 21:25:51 -0500127static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400128 struct btrfs_root *root, struct inode *inode,
129 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000130 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400131 struct page **compressed_pages)
132{
133 struct btrfs_key key;
134 struct btrfs_path *path;
135 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;
143 size_t datasize;
144 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400145
Li Zefanfe3f5662011-03-28 08:30:38 +0000146 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400147 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400148
Chris Masond3977122009-01-05 21:25:51 -0500149 path = btrfs_alloc_path();
150 if (!path)
Chris Masonc8b97812008-10-29 14:49:59 -0400151 return -ENOMEM;
152
Chris Masonb9473432009-03-13 11:00:37 -0400153 path->leave_spinning = 1;
Chris Masonc8b97812008-10-29 14:49:59 -0400154
Li Zefan33345d012011-04-20 10:31:50 +0800155 key.objectid = btrfs_ino(inode);
Chris Masonc8b97812008-10-29 14:49:59 -0400156 key.offset = start;
157 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
Chris Masonc8b97812008-10-29 14:49:59 -0400158 datasize = btrfs_file_extent_calc_inline_size(cur_size);
159
160 inode_add_bytes(inode, size);
161 ret = btrfs_insert_empty_item(trans, root, path, &key,
162 datasize);
Chris Masonc8b97812008-10-29 14:49:59 -0400163 if (ret) {
164 err = ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400165 goto fail;
166 }
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);
206 btrfs_free_path(path);
207
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:
222 btrfs_free_path(path);
223 return err;
224}
225
226
227/*
228 * conditionally insert an inline extent into the file. This
229 * does the checks required to make sure the data is small enough
230 * to fit as an inline extent.
231 */
Josef Bacik00361582013-08-14 14:02:47 -0400232static noinline int cow_file_range_inline(struct btrfs_root *root,
233 struct inode *inode, u64 start,
234 u64 end, size_t compressed_size,
235 int compress_type,
236 struct page **compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400237{
Josef Bacik00361582013-08-14 14:02:47 -0400238 struct btrfs_trans_handle *trans;
Chris Masonc8b97812008-10-29 14:49:59 -0400239 u64 isize = i_size_read(inode);
240 u64 actual_end = min(end + 1, isize);
241 u64 inline_len = actual_end - start;
Qu Wenruofda28322013-02-26 08:10:22 +0000242 u64 aligned_end = ALIGN(end, root->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400243 u64 data_len = inline_len;
244 int ret;
245
246 if (compressed_size)
247 data_len = compressed_size;
248
249 if (start > 0 ||
Chris Mason70b99e62008-10-31 12:46:39 -0400250 actual_end >= PAGE_CACHE_SIZE ||
Chris Masonc8b97812008-10-29 14:49:59 -0400251 data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
252 (!compressed_size &&
253 (actual_end & (root->sectorsize - 1)) == 0) ||
254 end + 1 < isize ||
255 data_len > root->fs_info->max_inline) {
256 return 1;
257 }
258
Josef Bacik00361582013-08-14 14:02:47 -0400259 trans = btrfs_join_transaction(root);
260 if (IS_ERR(trans))
261 return PTR_ERR(trans);
262 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
263
Josef Bacik26714852012-08-29 12:24:27 -0400264 ret = btrfs_drop_extents(trans, root, inode, start, aligned_end, 1);
Josef Bacik00361582013-08-14 14:02:47 -0400265 if (ret) {
266 btrfs_abort_transaction(trans, root, ret);
267 goto out;
268 }
Chris Masonc8b97812008-10-29 14:49:59 -0400269
270 if (isize > actual_end)
271 inline_len = min_t(u64, isize, actual_end);
272 ret = insert_inline_extent(trans, root, inode, start,
273 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000274 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400275 if (ret && ret != -ENOSPC) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100276 btrfs_abort_transaction(trans, root, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400277 goto out;
Josef Bacik2adcac12012-05-23 16:10:14 -0400278 } else if (ret == -ENOSPC) {
Josef Bacik00361582013-08-14 14:02:47 -0400279 ret = 1;
280 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100281 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400282
Josef Bacikbdc20e62013-02-28 13:23:38 -0500283 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400284 btrfs_delalloc_release_metadata(inode, end + 1 - start);
Chris Masona1ed8352009-09-11 12:27:37 -0400285 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
Josef Bacik00361582013-08-14 14:02:47 -0400286out:
287 btrfs_end_transaction(trans, root);
288 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400289}
290
Chris Mason771ed682008-11-06 22:02:51 -0500291struct async_extent {
292 u64 start;
293 u64 ram_size;
294 u64 compressed_size;
295 struct page **pages;
296 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800297 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500298 struct list_head list;
299};
300
301struct async_cow {
302 struct inode *inode;
303 struct btrfs_root *root;
304 struct page *locked_page;
305 u64 start;
306 u64 end;
307 struct list_head extents;
308 struct btrfs_work work;
309};
310
311static noinline int add_async_extent(struct async_cow *cow,
312 u64 start, u64 ram_size,
313 u64 compressed_size,
314 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800315 unsigned long nr_pages,
316 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500317{
318 struct async_extent *async_extent;
319
320 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100321 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500322 async_extent->start = start;
323 async_extent->ram_size = ram_size;
324 async_extent->compressed_size = compressed_size;
325 async_extent->pages = pages;
326 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800327 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500328 list_add_tail(&async_extent->list, &cow->extents);
329 return 0;
330}
331
Chris Masonc8b97812008-10-29 14:49:59 -0400332/*
Chris Mason771ed682008-11-06 22:02:51 -0500333 * we create compressed extents in two phases. The first
334 * phase compresses a range of pages that have already been
335 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400336 *
Chris Mason771ed682008-11-06 22:02:51 -0500337 * This is done inside an ordered work queue, and the compression
338 * is spread across many cpus. The actual IO submission is step
339 * two, and the ordered work queue takes care of making sure that
340 * happens in the same order things were put onto the queue by
341 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400342 *
Chris Mason771ed682008-11-06 22:02:51 -0500343 * If this code finds it can't get good compression, it puts an
344 * entry onto the work queue to write the uncompressed bytes. This
345 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300346 * are written in the same order that the flusher thread sent them
347 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400348 */
Chris Mason771ed682008-11-06 22:02:51 -0500349static noinline int compress_file_range(struct inode *inode,
350 struct page *locked_page,
351 u64 start, u64 end,
352 struct async_cow *async_cow,
353 int *num_added)
Chris Masonb888db22007-08-27 16:49:44 -0400354{
355 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masondb945352007-10-15 16:15:53 -0400356 u64 num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400357 u64 blocksize = root->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400358 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500359 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400360 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400361 struct page **pages = NULL;
362 unsigned long nr_pages;
363 unsigned long nr_pages_ret = 0;
364 unsigned long total_compressed = 0;
365 unsigned long total_in = 0;
366 unsigned long max_compressed = 128 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -0500367 unsigned long max_uncompressed = 128 * 1024;
Chris Masonc8b97812008-10-29 14:49:59 -0400368 int i;
369 int will_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800370 int compress_type = root->fs_info->compress_type;
Chris Mason4adaa612013-03-26 13:07:00 -0400371 int redirty = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400372
Liu Bo4cb13e52012-03-29 09:57:45 -0400373 /* if this is a small write inside eof, kick off a defrag */
374 if ((end - start + 1) < 16 * 1024 &&
375 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400376 btrfs_add_inode_defrag(NULL, inode);
377
Chris Mason42dc7ba2008-12-15 11:44:56 -0500378 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400379again:
380 will_compress = 0;
381 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
382 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
383
Chris Masonf03d9301f2009-02-04 09:31:06 -0500384 /*
385 * we don't want to send crud past the end of i_size through
386 * compression, that's just a waste of CPU time. So, if the
387 * end of the file is before the start of our current
388 * requested range of bytes, we bail out to the uncompressed
389 * cleanup code that can deal with all of this.
390 *
391 * It isn't really the fastest way to fix things, but this is a
392 * very uncommon corner.
393 */
394 if (actual_end <= start)
395 goto cleanup_and_bail_uncompressed;
396
Chris Masonc8b97812008-10-29 14:49:59 -0400397 total_compressed = actual_end - start;
398
399 /* we want to make sure that amount of ram required to uncompress
400 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500401 * of a compressed extent to 128k. This is a crucial number
402 * because it also controls how easily we can spread reads across
403 * cpus for decompression.
404 *
405 * We also want to make sure the amount of IO required to do
406 * a random read is reasonably small, so we limit the size of
407 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400408 */
409 total_compressed = min(total_compressed, max_uncompressed);
Qu Wenruofda28322013-02-26 08:10:22 +0000410 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Masonbe20aa92007-12-17 20:14:01 -0500411 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400412 total_in = 0;
413 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400414
Chris Mason771ed682008-11-06 22:02:51 -0500415 /*
416 * we do compression for mount -o compress and when the
417 * inode has not been flagged as nocompress. This flag can
418 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400419 */
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200420 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) &&
Chris Mason1e701a32010-03-11 09:42:04 -0500421 (btrfs_test_opt(root, COMPRESS) ||
Liu Bo75e7cb72011-03-22 10:12:20 +0000422 (BTRFS_I(inode)->force_compress) ||
423 (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS))) {
Chris Masonc8b97812008-10-29 14:49:59 -0400424 WARN_ON(pages);
Chris Masoncfbc2462008-10-30 13:22:14 -0400425 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800426 if (!pages) {
427 /* just bail out to the uncompressed code */
428 goto cont;
429 }
Chris Mason179e29e2007-11-01 11:28:41 -0400430
Li Zefan261507a02010-12-17 14:21:50 +0800431 if (BTRFS_I(inode)->force_compress)
432 compress_type = BTRFS_I(inode)->force_compress;
433
Chris Mason4adaa612013-03-26 13:07:00 -0400434 /*
435 * we need to call clear_page_dirty_for_io on each
436 * page in the range. Otherwise applications with the file
437 * mmap'd can wander in and change the page contents while
438 * we are compressing them.
439 *
440 * If the compression fails for any reason, we set the pages
441 * dirty again later on.
442 */
443 extent_range_clear_dirty_for_io(inode, start, end);
444 redirty = 1;
Li Zefan261507a02010-12-17 14:21:50 +0800445 ret = btrfs_compress_pages(compress_type,
446 inode->i_mapping, start,
447 total_compressed, pages,
448 nr_pages, &nr_pages_ret,
449 &total_in,
450 &total_compressed,
451 max_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400452
453 if (!ret) {
454 unsigned long offset = total_compressed &
455 (PAGE_CACHE_SIZE - 1);
456 struct page *page = pages[nr_pages_ret - 1];
457 char *kaddr;
458
459 /* zero the tail end of the last page, we might be
460 * sending it down to disk
461 */
462 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800463 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400464 memset(kaddr + offset, 0,
465 PAGE_CACHE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800466 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400467 }
468 will_compress = 1;
469 }
470 }
Li Zefan560f7d72011-09-08 10:22:01 +0800471cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400472 if (start == 0) {
473 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500474 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400475 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500476 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400477 */
Josef Bacik00361582013-08-14 14:02:47 -0400478 ret = cow_file_range_inline(root, inode, start, end,
479 0, 0, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400480 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500481 /* try making a compressed inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400482 ret = cow_file_range_inline(root, inode, start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000483 total_compressed,
484 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400485 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100486 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400487 unsigned long clear_flags = EXTENT_DELALLOC |
488 EXTENT_DEFRAG;
489 clear_flags |= (ret < 0) ? EXTENT_DO_ACCOUNTING : 0;
490
Chris Mason771ed682008-11-06 22:02:51 -0500491 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100492 * inline extent creation worked or returned error,
493 * we don't need to create any more async work items.
494 * Unlock and free up our temp pages.
Chris Mason771ed682008-11-06 22:02:51 -0500495 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400496 extent_clear_unlock_delalloc(inode, start, end, NULL,
Josef Bacik151a41b2013-07-29 13:22:24 -0400497 clear_flags, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400498 PAGE_CLEAR_DIRTY |
499 PAGE_SET_WRITEBACK |
500 PAGE_END_WRITEBACK);
Chris Masonc8b97812008-10-29 14:49:59 -0400501 goto free_pages_out;
502 }
503 }
504
505 if (will_compress) {
506 /*
507 * we aren't doing an inline extent round the compressed size
508 * up to a block size boundary so the allocator does sane
509 * things
510 */
Qu Wenruofda28322013-02-26 08:10:22 +0000511 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400512
513 /*
514 * one last check to make sure the compression is really a
515 * win, compare the page count read with the blocks on disk
516 */
Qu Wenruofda28322013-02-26 08:10:22 +0000517 total_in = ALIGN(total_in, PAGE_CACHE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400518 if (total_compressed >= total_in) {
519 will_compress = 0;
520 } else {
Chris Masonc8b97812008-10-29 14:49:59 -0400521 num_bytes = total_in;
522 }
523 }
524 if (!will_compress && pages) {
525 /*
526 * the compression code ran but failed to make things smaller,
527 * free any pages it allocated and our page pointer array
528 */
529 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400530 WARN_ON(pages[i]->mapping);
Chris Masonc8b97812008-10-29 14:49:59 -0400531 page_cache_release(pages[i]);
532 }
533 kfree(pages);
534 pages = NULL;
535 total_compressed = 0;
536 nr_pages_ret = 0;
537
538 /* flag the file so we don't compress in the future */
Chris Mason1e701a32010-03-11 09:42:04 -0500539 if (!btrfs_test_opt(root, FORCE_COMPRESS) &&
540 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500541 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500542 }
Chris Masonc8b97812008-10-29 14:49:59 -0400543 }
Chris Mason771ed682008-11-06 22:02:51 -0500544 if (will_compress) {
545 *num_added += 1;
546
547 /* the async work queues will take care of doing actual
548 * allocation on disk for these compressed pages,
549 * and will submit them to the elevator.
550 */
551 add_async_extent(async_cow, start, num_bytes,
Li Zefan261507a02010-12-17 14:21:50 +0800552 total_compressed, pages, nr_pages_ret,
553 compress_type);
Chris Mason771ed682008-11-06 22:02:51 -0500554
Yan, Zheng24ae6362010-12-06 07:02:36 +0000555 if (start + num_bytes < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500556 start += num_bytes;
557 pages = NULL;
558 cond_resched();
559 goto again;
560 }
561 } else {
Chris Masonf03d9301f2009-02-04 09:31:06 -0500562cleanup_and_bail_uncompressed:
Chris Mason771ed682008-11-06 22:02:51 -0500563 /*
564 * No compression, but we still need to write the pages in
565 * the file we've been given so far. redirty the locked
566 * page if it corresponds to our extent and set things up
567 * for the async work queue to run cow_file_range to do
568 * the normal delalloc dance
569 */
570 if (page_offset(locked_page) >= start &&
571 page_offset(locked_page) <= end) {
572 __set_page_dirty_nobuffers(locked_page);
573 /* unlocked later on in the async handlers */
574 }
Chris Mason4adaa612013-03-26 13:07:00 -0400575 if (redirty)
576 extent_range_redirty_for_io(inode, start, end);
Li Zefan261507a02010-12-17 14:21:50 +0800577 add_async_extent(async_cow, start, end - start + 1,
578 0, NULL, 0, BTRFS_COMPRESS_NONE);
Chris Mason771ed682008-11-06 22:02:51 -0500579 *num_added += 1;
580 }
581
582out:
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100583 return ret;
Chris Mason771ed682008-11-06 22:02:51 -0500584
585free_pages_out:
586 for (i = 0; i < nr_pages_ret; i++) {
587 WARN_ON(pages[i]->mapping);
588 page_cache_release(pages[i]);
589 }
Chris Masond3977122009-01-05 21:25:51 -0500590 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500591
592 goto out;
593}
594
595/*
596 * phase two of compressed writeback. This is the ordered portion
597 * of the code, which only gets called in the order the work was
598 * queued. We walk all the async extents created by compress_file_range
599 * and send them down to the disk.
600 */
601static noinline int submit_compressed_extents(struct inode *inode,
602 struct async_cow *async_cow)
603{
604 struct async_extent *async_extent;
605 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500606 struct btrfs_key ins;
607 struct extent_map *em;
608 struct btrfs_root *root = BTRFS_I(inode)->root;
609 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
610 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500611 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500612
613 if (list_empty(&async_cow->extents))
614 return 0;
615
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500616again:
Chris Masond3977122009-01-05 21:25:51 -0500617 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500618 async_extent = list_entry(async_cow->extents.next,
619 struct async_extent, list);
620 list_del(&async_extent->list);
621
622 io_tree = &BTRFS_I(inode)->io_tree;
623
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500624retry:
Chris Mason771ed682008-11-06 22:02:51 -0500625 /* did the compression code fall back to uncompressed IO? */
626 if (!async_extent->pages) {
627 int page_started = 0;
628 unsigned long nr_written = 0;
629
630 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000631 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100632 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500633
634 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500635 ret = cow_file_range(inode, async_cow->locked_page,
636 async_extent->start,
637 async_extent->start +
638 async_extent->ram_size - 1,
639 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500640
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100641 /* JDM XXX */
642
Chris Mason771ed682008-11-06 22:02:51 -0500643 /*
644 * if page_started, cow_file_range inserted an
645 * inline extent and took care of all the unlocking
646 * and IO for us. Otherwise, we need to submit
647 * all those pages down to the drive.
648 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500649 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500650 extent_write_locked_range(io_tree,
651 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500652 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500653 async_extent->ram_size - 1,
654 btrfs_get_extent,
655 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500656 else if (ret)
657 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500658 kfree(async_extent);
659 cond_resched();
660 continue;
661 }
662
663 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100664 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500665
Josef Bacik00361582013-08-14 14:02:47 -0400666 ret = btrfs_reserve_extent(root,
Chris Mason771ed682008-11-06 22:02:51 -0500667 async_extent->compressed_size,
668 async_extent->compressed_size,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500669 0, alloc_hint, &ins, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500670 if (ret) {
671 int i;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500672
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500673 for (i = 0; i < async_extent->nr_pages; i++) {
674 WARN_ON(async_extent->pages[i]->mapping);
675 page_cache_release(async_extent->pages[i]);
676 }
677 kfree(async_extent->pages);
678 async_extent->nr_pages = 0;
679 async_extent->pages = NULL;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500680
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400681 if (ret == -ENOSPC) {
682 unlock_extent(io_tree, async_extent->start,
683 async_extent->start +
684 async_extent->ram_size - 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100685 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400686 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500687 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500688 }
689
Yan, Zhengc2167752009-11-12 09:34:21 +0000690 /*
691 * here we're doing allocation and writeback of the
692 * compressed pages
693 */
694 btrfs_drop_extent_cache(inode, async_extent->start,
695 async_extent->start +
696 async_extent->ram_size - 1, 0);
697
David Sterba172ddd62011-04-21 00:48:27 +0200698 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000699 if (!em) {
700 ret = -ENOMEM;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500701 goto out_free_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000702 }
Chris Mason771ed682008-11-06 22:02:51 -0500703 em->start = async_extent->start;
704 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500705 em->orig_start = em->start;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400706 em->mod_start = em->start;
707 em->mod_len = em->len;
Chris Mason771ed682008-11-06 22:02:51 -0500708
709 em->block_start = ins.objectid;
710 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500711 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400712 em->ram_bytes = async_extent->ram_size;
Chris Mason771ed682008-11-06 22:02:51 -0500713 em->bdev = root->fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800714 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500715 set_bit(EXTENT_FLAG_PINNED, &em->flags);
716 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400717 em->generation = -1;
Chris Mason771ed682008-11-06 22:02:51 -0500718
Chris Masond3977122009-01-05 21:25:51 -0500719 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400720 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400721 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400722 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500723 if (ret != -EEXIST) {
724 free_extent_map(em);
725 break;
726 }
727 btrfs_drop_extent_cache(inode, async_extent->start,
728 async_extent->start +
729 async_extent->ram_size - 1, 0);
730 }
731
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500732 if (ret)
733 goto out_free_reserve;
734
Li Zefan261507a02010-12-17 14:21:50 +0800735 ret = btrfs_add_ordered_extent_compress(inode,
736 async_extent->start,
737 ins.objectid,
738 async_extent->ram_size,
739 ins.offset,
740 BTRFS_ORDERED_COMPRESSED,
741 async_extent->compress_type);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500742 if (ret)
743 goto out_free_reserve;
Chris Mason771ed682008-11-06 22:02:51 -0500744
Chris Mason771ed682008-11-06 22:02:51 -0500745 /*
746 * clear dirty, set writeback and unlock the pages.
747 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400748 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400749 async_extent->start +
750 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400751 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
752 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400753 PAGE_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500754 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500755 async_extent->start,
756 async_extent->ram_size,
757 ins.objectid,
758 ins.offset, async_extent->pages,
759 async_extent->nr_pages);
Chris Mason771ed682008-11-06 22:02:51 -0500760 alloc_hint = ins.objectid + ins.offset;
761 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500762 if (ret)
763 goto out;
Chris Mason771ed682008-11-06 22:02:51 -0500764 cond_resched();
765 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100766 ret = 0;
767out:
768 return ret;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500769out_free_reserve:
770 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100771out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400772 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500773 async_extent->start +
774 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400775 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400776 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
777 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
778 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100779 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500780 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500781}
782
Josef Bacik4b46fce2010-05-23 11:00:55 -0400783static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
784 u64 num_bytes)
785{
786 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
787 struct extent_map *em;
788 u64 alloc_hint = 0;
789
790 read_lock(&em_tree->lock);
791 em = search_extent_mapping(em_tree, start, num_bytes);
792 if (em) {
793 /*
794 * if block start isn't an actual block number then find the
795 * first block in this inode and use that as a hint. If that
796 * block is also bogus then just don't worry about it.
797 */
798 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
799 free_extent_map(em);
800 em = search_extent_mapping(em_tree, 0, 0);
801 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
802 alloc_hint = em->block_start;
803 if (em)
804 free_extent_map(em);
805 } else {
806 alloc_hint = em->block_start;
807 free_extent_map(em);
808 }
809 }
810 read_unlock(&em_tree->lock);
811
812 return alloc_hint;
813}
814
Chris Mason771ed682008-11-06 22:02:51 -0500815/*
816 * when extent_io.c finds a delayed allocation range in the file,
817 * the call backs end up in this code. The basic idea is to
818 * allocate extents on disk for the range, and create ordered data structs
819 * in ram to track those extents.
820 *
821 * locked_page is the page that writepage had locked already. We use
822 * it to make sure we don't do extra locks or unlocks.
823 *
824 * *page_started is set to one if we unlock locked_page and do everything
825 * required to start IO on it. It may be clean and already done with
826 * IO when we return.
827 */
Josef Bacik00361582013-08-14 14:02:47 -0400828static noinline int cow_file_range(struct inode *inode,
829 struct page *locked_page,
830 u64 start, u64 end, int *page_started,
831 unsigned long *nr_written,
832 int unlock)
Chris Mason771ed682008-11-06 22:02:51 -0500833{
Josef Bacik00361582013-08-14 14:02:47 -0400834 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500835 u64 alloc_hint = 0;
836 u64 num_bytes;
837 unsigned long ram_size;
838 u64 disk_num_bytes;
839 u64 cur_alloc_size;
840 u64 blocksize = root->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500841 struct btrfs_key ins;
842 struct extent_map *em;
843 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
844 int ret = 0;
845
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400846 if (btrfs_is_free_space_inode(inode)) {
847 WARN_ON_ONCE(1);
848 return -EINVAL;
849 }
Chris Mason771ed682008-11-06 22:02:51 -0500850
Qu Wenruofda28322013-02-26 08:10:22 +0000851 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500852 num_bytes = max(blocksize, num_bytes);
853 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500854
Chris Mason4cb53002011-05-24 15:35:30 -0400855 /* if this is a small write inside eof, kick off defrag */
Liu Bo4cb13e52012-03-29 09:57:45 -0400856 if (num_bytes < 64 * 1024 &&
857 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Josef Bacik00361582013-08-14 14:02:47 -0400858 btrfs_add_inode_defrag(NULL, inode);
Chris Mason4cb53002011-05-24 15:35:30 -0400859
Chris Mason771ed682008-11-06 22:02:51 -0500860 if (start == 0) {
861 /* lets try to make an inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400862 ret = cow_file_range_inline(root, inode, start, end, 0, 0,
863 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500864 if (ret == 0) {
Josef Bacikc2790a22013-07-29 11:20:47 -0400865 extent_clear_unlock_delalloc(inode, start, end, NULL,
866 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400867 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400868 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
869 PAGE_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000870
Chris Mason771ed682008-11-06 22:02:51 -0500871 *nr_written = *nr_written +
872 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
873 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500874 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100875 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100876 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500877 }
878 }
Chris Masonc8b97812008-10-29 14:49:59 -0400879
880 BUG_ON(disk_num_bytes >
David Sterba6c417612011-04-13 15:41:04 +0200881 btrfs_super_total_bytes(root->fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400882
Josef Bacik4b46fce2010-05-23 11:00:55 -0400883 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400884 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400885
Chris Masond3977122009-01-05 21:25:51 -0500886 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400887 unsigned long op;
888
Josef Bacik287a0ab2010-03-19 18:07:23 +0000889 cur_alloc_size = disk_num_bytes;
Josef Bacik00361582013-08-14 14:02:47 -0400890 ret = btrfs_reserve_extent(root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500891 root->sectorsize, 0, alloc_hint,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500892 &ins, 1);
Josef Bacik00361582013-08-14 14:02:47 -0400893 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100894 goto out_unlock;
Chris Masond3977122009-01-05 21:25:51 -0500895
David Sterba172ddd62011-04-21 00:48:27 +0200896 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000897 if (!em) {
898 ret = -ENOMEM;
Liu Boace68ba2013-04-22 10:53:47 +0000899 goto out_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000900 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400901 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500902 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500903 ram_size = ins.offset;
904 em->len = ins.offset;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400905 em->mod_start = em->start;
906 em->mod_len = em->len;
Chris Masonc8b97812008-10-29 14:49:59 -0400907
Chris Masone6dcd2d2008-07-17 12:53:50 -0400908 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400909 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500910 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400911 em->ram_bytes = ram_size;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400912 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400913 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400914 em->generation = -1;
Chris Masonc8b97812008-10-29 14:49:59 -0400915
Chris Masond3977122009-01-05 21:25:51 -0500916 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400917 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400918 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400919 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400920 if (ret != -EEXIST) {
921 free_extent_map(em);
922 break;
923 }
924 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400925 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400926 }
Liu Boace68ba2013-04-22 10:53:47 +0000927 if (ret)
928 goto out_reserve;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400929
Chris Mason98d20f62008-04-14 09:46:10 -0400930 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400931 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -0500932 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +0000933 if (ret)
934 goto out_reserve;
Chris Masonc8b97812008-10-29 14:49:59 -0400935
Yan Zheng17d217f2008-12-12 10:03:38 -0500936 if (root->root_key.objectid ==
937 BTRFS_DATA_RELOC_TREE_OBJECTID) {
938 ret = btrfs_reloc_clone_csums(inode, start,
939 cur_alloc_size);
Josef Bacik00361582013-08-14 14:02:47 -0400940 if (ret)
Liu Boace68ba2013-04-22 10:53:47 +0000941 goto out_reserve;
Yan Zheng17d217f2008-12-12 10:03:38 -0500942 }
943
Chris Masond3977122009-01-05 21:25:51 -0500944 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -0400945 break;
Chris Masond3977122009-01-05 21:25:51 -0500946
Chris Masonc8b97812008-10-29 14:49:59 -0400947 /* we're not doing compressed IO, don't unlock the first
948 * page (which the caller expects to stay locked), don't
949 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -0400950 *
951 * Do set the Private2 bit so we know this page was properly
952 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -0400953 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400954 op = unlock ? PAGE_UNLOCK : 0;
955 op |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -0400956
Josef Bacikc2790a22013-07-29 11:20:47 -0400957 extent_clear_unlock_delalloc(inode, start,
958 start + ram_size - 1, locked_page,
959 EXTENT_LOCKED | EXTENT_DELALLOC,
960 op);
Chris Masonc8b97812008-10-29 14:49:59 -0400961 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -0500962 num_bytes -= cur_alloc_size;
963 alloc_hint = ins.objectid + ins.offset;
964 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400965 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100966out:
Chris Masonbe20aa92007-12-17 20:14:01 -0500967 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +0000968
Liu Boace68ba2013-04-22 10:53:47 +0000969out_reserve:
970 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100971out_unlock:
Josef Bacikc2790a22013-07-29 11:20:47 -0400972 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Josef Bacik151a41b2013-07-29 13:22:24 -0400973 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
974 EXTENT_DELALLOC | EXTENT_DEFRAG,
975 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
976 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100977 goto out;
Chris Mason771ed682008-11-06 22:02:51 -0500978}
Chris Masonc8b97812008-10-29 14:49:59 -0400979
Chris Mason771ed682008-11-06 22:02:51 -0500980/*
981 * work queue call back to started compression on a file and pages
982 */
983static noinline void async_cow_start(struct btrfs_work *work)
984{
985 struct async_cow *async_cow;
986 int num_added = 0;
987 async_cow = container_of(work, struct async_cow, work);
988
989 compress_file_range(async_cow->inode, async_cow->locked_page,
990 async_cow->start, async_cow->end, async_cow,
991 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -0400992 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -0600993 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -0500994 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -0400995 }
Chris Mason771ed682008-11-06 22:02:51 -0500996}
997
998/*
999 * work queue call back to submit previously compressed pages
1000 */
1001static noinline void async_cow_submit(struct btrfs_work *work)
1002{
1003 struct async_cow *async_cow;
1004 struct btrfs_root *root;
1005 unsigned long nr_pages;
1006
1007 async_cow = container_of(work, struct async_cow, work);
1008
1009 root = async_cow->root;
1010 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
1011 PAGE_CACHE_SHIFT;
1012
Josef Bacik66657b32012-08-01 15:36:24 -04001013 if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) <
Liu Bo287082b2012-06-28 04:02:24 -06001014 5 * 1024 * 1024 &&
Chris Mason771ed682008-11-06 22:02:51 -05001015 waitqueue_active(&root->fs_info->async_submit_wait))
1016 wake_up(&root->fs_info->async_submit_wait);
1017
Chris Masond3977122009-01-05 21:25:51 -05001018 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001019 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001020}
Chris Masonc8b97812008-10-29 14:49:59 -04001021
Chris Mason771ed682008-11-06 22:02:51 -05001022static noinline void async_cow_free(struct btrfs_work *work)
1023{
1024 struct async_cow *async_cow;
1025 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001026 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001027 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001028 kfree(async_cow);
1029}
1030
1031static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1032 u64 start, u64 end, int *page_started,
1033 unsigned long *nr_written)
1034{
1035 struct async_cow *async_cow;
1036 struct btrfs_root *root = BTRFS_I(inode)->root;
1037 unsigned long nr_pages;
1038 u64 cur_end;
Liu Bo287082b2012-06-28 04:02:24 -06001039 int limit = 10 * 1024 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -05001040
Chris Masona3429ab2009-10-08 12:30:20 -04001041 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1042 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001043 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001044 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001045 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001046 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001047 async_cow->root = root;
1048 async_cow->locked_page = locked_page;
1049 async_cow->start = start;
1050
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001051 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
Chris Mason771ed682008-11-06 22:02:51 -05001052 cur_end = end;
1053 else
1054 cur_end = min(end, start + 512 * 1024 - 1);
1055
1056 async_cow->end = cur_end;
1057 INIT_LIST_HEAD(&async_cow->extents);
1058
1059 async_cow->work.func = async_cow_start;
1060 async_cow->work.ordered_func = async_cow_submit;
1061 async_cow->work.ordered_free = async_cow_free;
1062 async_cow->work.flags = 0;
1063
Chris Mason771ed682008-11-06 22:02:51 -05001064 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
1065 PAGE_CACHE_SHIFT;
1066 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
1067
1068 btrfs_queue_worker(&root->fs_info->delalloc_workers,
1069 &async_cow->work);
1070
1071 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
1072 wait_event(root->fs_info->async_submit_wait,
1073 (atomic_read(&root->fs_info->async_delalloc_pages) <
1074 limit));
1075 }
1076
Chris Masond3977122009-01-05 21:25:51 -05001077 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -05001078 atomic_read(&root->fs_info->async_delalloc_pages)) {
1079 wait_event(root->fs_info->async_submit_wait,
1080 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1081 0));
1082 }
1083
1084 *nr_written += nr_pages;
1085 start = cur_end + 1;
1086 }
1087 *page_started = 1;
1088 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001089}
1090
Chris Masond3977122009-01-05 21:25:51 -05001091static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001092 u64 bytenr, u64 num_bytes)
1093{
1094 int ret;
1095 struct btrfs_ordered_sum *sums;
1096 LIST_HEAD(list);
1097
Yan Zheng07d400a2009-01-06 11:42:00 -05001098 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001099 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001100 if (ret == 0 && list_empty(&list))
1101 return 0;
1102
1103 while (!list_empty(&list)) {
1104 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1105 list_del(&sums->list);
1106 kfree(sums);
1107 }
1108 return 1;
1109}
1110
Chris Masond352ac62008-09-29 15:18:18 -04001111/*
1112 * when nowcow writeback call back. This checks for snapshots or COW copies
1113 * of the extents that exist in the file, and COWs the file as required.
1114 *
1115 * If no cow copies or snapshots exist, we write directly to the existing
1116 * blocks on disk
1117 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001118static noinline int run_delalloc_nocow(struct inode *inode,
1119 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001120 u64 start, u64 end, int *page_started, int force,
1121 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001122{
Chris Masonbe20aa92007-12-17 20:14:01 -05001123 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001124 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001125 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001126 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001127 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001128 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001129 u64 cow_start;
1130 u64 cur_offset;
1131 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001132 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001133 u64 disk_bytenr;
1134 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001135 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001136 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001137 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001138 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001139 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001140 int nocow;
1141 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001142 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001143 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001144
1145 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001146 if (!path) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001147 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1148 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001149 EXTENT_DO_ACCOUNTING |
1150 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001151 PAGE_CLEAR_DIRTY |
1152 PAGE_SET_WRITEBACK |
1153 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001154 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001155 }
Li Zefan82d59022011-04-20 10:33:24 +08001156
Liu Bo83eea1f2012-07-10 05:28:39 -06001157 nolock = btrfs_is_free_space_inode(inode);
Li Zefan82d59022011-04-20 10:33:24 +08001158
1159 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001160 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001161 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001162 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001163
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001164 if (IS_ERR(trans)) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001165 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1166 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001167 EXTENT_DO_ACCOUNTING |
1168 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001169 PAGE_CLEAR_DIRTY |
1170 PAGE_SET_WRITEBACK |
1171 PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001172 btrfs_free_path(path);
1173 return PTR_ERR(trans);
1174 }
1175
Josef Bacik74b21072011-04-13 12:02:53 -04001176 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001177
Yan Zheng80ff3852008-10-30 14:20:02 -04001178 cow_start = (u64)-1;
1179 cur_offset = start;
1180 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001181 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001182 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001183 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001184 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001185 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1186 leaf = path->nodes[0];
1187 btrfs_item_key_to_cpu(leaf, &found_key,
1188 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001189 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001190 found_key.type == BTRFS_EXTENT_DATA_KEY)
1191 path->slots[0]--;
1192 }
1193 check_prev = 0;
1194next_slot:
1195 leaf = path->nodes[0];
1196 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1197 ret = btrfs_next_leaf(root, path);
Josef Bacikd788a342013-10-25 16:55:08 -04001198 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001199 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001200 if (ret > 0)
1201 break;
1202 leaf = path->nodes[0];
1203 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001204
Yan Zheng80ff3852008-10-30 14:20:02 -04001205 nocow = 0;
1206 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001207 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001208 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001209
Li Zefan33345d012011-04-20 10:31:50 +08001210 if (found_key.objectid > ino ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001211 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1212 found_key.offset > end)
1213 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001214
Yan Zheng80ff3852008-10-30 14:20:02 -04001215 if (found_key.offset > cur_offset) {
1216 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001217 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001218 goto out_check;
1219 }
Chris Masonc31f8832008-01-08 15:46:31 -05001220
Yan Zheng80ff3852008-10-30 14:20:02 -04001221 fi = btrfs_item_ptr(leaf, path->slots[0],
1222 struct btrfs_file_extent_item);
1223 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001224
Josef Bacikcc95bef2013-04-04 14:31:27 -04001225 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001226 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1227 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001228 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001229 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001230 extent_end = found_key.offset +
1231 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001232 disk_num_bytes =
1233 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001234 if (extent_end <= start) {
1235 path->slots[0]++;
1236 goto next_slot;
1237 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001238 if (disk_bytenr == 0)
1239 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001240 if (btrfs_file_extent_compression(leaf, fi) ||
1241 btrfs_file_extent_encryption(leaf, fi) ||
1242 btrfs_file_extent_other_encoding(leaf, fi))
1243 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001244 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1245 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001246 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001247 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001248 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001249 found_key.offset -
1250 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001251 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001252 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001253 disk_bytenr += cur_offset - found_key.offset;
1254 num_bytes = min(end + 1, extent_end) - cur_offset;
1255 /*
1256 * force cow if csum exists in the range.
1257 * this ensure that csum for a given extent are
1258 * either valid or do not exist.
1259 */
1260 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1261 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001262 nocow = 1;
1263 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1264 extent_end = found_key.offset +
1265 btrfs_file_extent_inline_len(leaf, fi);
1266 extent_end = ALIGN(extent_end, root->sectorsize);
1267 } else {
1268 BUG_ON(1);
1269 }
1270out_check:
1271 if (extent_end <= start) {
1272 path->slots[0]++;
1273 goto next_slot;
1274 }
1275 if (!nocow) {
1276 if (cow_start == (u64)-1)
1277 cow_start = cur_offset;
1278 cur_offset = extent_end;
1279 if (cur_offset > end)
1280 break;
1281 path->slots[0]++;
1282 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001283 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001284
David Sterbab3b4aa72011-04-21 01:20:15 +02001285 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001286 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001287 ret = cow_file_range(inode, locked_page,
1288 cow_start, found_key.offset - 1,
1289 page_started, nr_written, 1);
Josef Bacikd788a342013-10-25 16:55:08 -04001290 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001291 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001292 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001293 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001294
Yan Zhengd899e052008-10-30 14:25:28 -04001295 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1296 struct extent_map *em;
1297 struct extent_map_tree *em_tree;
1298 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001299 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001300 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001301 em->start = cur_offset;
Josef Bacik70c8a912012-10-11 16:54:30 -04001302 em->orig_start = found_key.offset - extent_offset;
Yan Zhengd899e052008-10-30 14:25:28 -04001303 em->len = num_bytes;
1304 em->block_len = num_bytes;
1305 em->block_start = disk_bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05001306 em->orig_block_len = disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001307 em->ram_bytes = ram_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04001308 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001309 em->mod_start = em->start;
1310 em->mod_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04001311 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacikb11e2342012-12-03 10:58:15 -05001312 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001313 em->generation = -1;
Yan Zhengd899e052008-10-30 14:25:28 -04001314 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001315 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001316 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001317 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001318 if (ret != -EEXIST) {
1319 free_extent_map(em);
1320 break;
1321 }
1322 btrfs_drop_extent_cache(inode, em->start,
1323 em->start + em->len - 1, 0);
1324 }
1325 type = BTRFS_ORDERED_PREALLOC;
1326 } else {
1327 type = BTRFS_ORDERED_NOCOW;
1328 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001329
1330 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001331 num_bytes, num_bytes, type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001332 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001333
Yan, Zhengefa56462010-05-16 10:49:59 -04001334 if (root->root_key.objectid ==
1335 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1336 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1337 num_bytes);
Josef Bacikd788a342013-10-25 16:55:08 -04001338 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001339 goto error;
Yan, Zhengefa56462010-05-16 10:49:59 -04001340 }
1341
Josef Bacikc2790a22013-07-29 11:20:47 -04001342 extent_clear_unlock_delalloc(inode, cur_offset,
1343 cur_offset + num_bytes - 1,
1344 locked_page, EXTENT_LOCKED |
1345 EXTENT_DELALLOC, PAGE_UNLOCK |
1346 PAGE_SET_PRIVATE2);
Yan Zheng80ff3852008-10-30 14:20:02 -04001347 cur_offset = extent_end;
1348 if (cur_offset > end)
1349 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001350 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001351 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001352
Josef Bacik17ca04a2012-05-31 15:58:55 -04001353 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001354 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001355 cur_offset = end;
1356 }
1357
Yan Zheng80ff3852008-10-30 14:20:02 -04001358 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001359 ret = cow_file_range(inode, locked_page, cow_start, end,
1360 page_started, nr_written, 1);
Josef Bacikd788a342013-10-25 16:55:08 -04001361 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001362 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001363 }
1364
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001365error:
Miao Xiea698d0752012-09-20 01:51:59 -06001366 err = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001367 if (!ret)
1368 ret = err;
1369
Josef Bacik17ca04a2012-05-31 15:58:55 -04001370 if (ret && cur_offset < end)
Josef Bacikc2790a22013-07-29 11:20:47 -04001371 extent_clear_unlock_delalloc(inode, cur_offset, end,
1372 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001373 EXTENT_DELALLOC | EXTENT_DEFRAG |
1374 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1375 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001376 PAGE_SET_WRITEBACK |
1377 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001378 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001379 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001380}
1381
Chris Masond352ac62008-09-29 15:18:18 -04001382/*
1383 * extent_io.c call back to do delayed allocation processing
1384 */
Chris Masonc8b97812008-10-29 14:49:59 -04001385static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001386 u64 start, u64 end, int *page_started,
1387 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001388{
Chris Masonbe20aa92007-12-17 20:14:01 -05001389 int ret;
Chris Mason7f366cf2009-03-12 20:12:45 -04001390 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona2135012008-06-25 16:01:30 -04001391
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001392 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) {
Chris Masonc8b97812008-10-29 14:49:59 -04001393 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001394 page_started, 1, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001395 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001396 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001397 page_started, 0, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001398 } else if (!btrfs_test_opt(root, COMPRESS) &&
1399 !(BTRFS_I(inode)->force_compress) &&
1400 !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS)) {
Chris Mason7f366cf2009-03-12 20:12:45 -04001401 ret = cow_file_range(inode, locked_page, start, end,
1402 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001403 } else {
1404 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1405 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001406 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001407 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001408 }
Chris Masonb888db22007-08-27 16:49:44 -04001409 return ret;
1410}
1411
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001412static void btrfs_split_extent_hook(struct inode *inode,
1413 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001414{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001415 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001416 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001417 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001418
Josef Bacik9e0baf62011-07-15 15:16:44 +00001419 spin_lock(&BTRFS_I(inode)->lock);
1420 BTRFS_I(inode)->outstanding_extents++;
1421 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001422}
1423
1424/*
1425 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1426 * extents so we can keep track of new extents that are just merged onto old
1427 * extents, such as when we are doing sequential writes, so we can properly
1428 * account for the metadata space we'll need.
1429 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001430static void btrfs_merge_extent_hook(struct inode *inode,
1431 struct extent_state *new,
1432 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001433{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001434 /* not delalloc, ignore it */
1435 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001436 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001437
Josef Bacik9e0baf62011-07-15 15:16:44 +00001438 spin_lock(&BTRFS_I(inode)->lock);
1439 BTRFS_I(inode)->outstanding_extents--;
1440 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001441}
1442
Miao Xieeb73c1b2013-05-15 07:48:22 +00001443static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1444 struct inode *inode)
1445{
1446 spin_lock(&root->delalloc_lock);
1447 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1448 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1449 &root->delalloc_inodes);
1450 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1451 &BTRFS_I(inode)->runtime_flags);
1452 root->nr_delalloc_inodes++;
1453 if (root->nr_delalloc_inodes == 1) {
1454 spin_lock(&root->fs_info->delalloc_root_lock);
1455 BUG_ON(!list_empty(&root->delalloc_root));
1456 list_add_tail(&root->delalloc_root,
1457 &root->fs_info->delalloc_roots);
1458 spin_unlock(&root->fs_info->delalloc_root_lock);
1459 }
1460 }
1461 spin_unlock(&root->delalloc_lock);
1462}
1463
1464static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1465 struct inode *inode)
1466{
1467 spin_lock(&root->delalloc_lock);
1468 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1469 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1470 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1471 &BTRFS_I(inode)->runtime_flags);
1472 root->nr_delalloc_inodes--;
1473 if (!root->nr_delalloc_inodes) {
1474 spin_lock(&root->fs_info->delalloc_root_lock);
1475 BUG_ON(list_empty(&root->delalloc_root));
1476 list_del_init(&root->delalloc_root);
1477 spin_unlock(&root->fs_info->delalloc_root_lock);
1478 }
1479 }
1480 spin_unlock(&root->delalloc_lock);
1481}
1482
Chris Masond352ac62008-09-29 15:18:18 -04001483/*
1484 * extent_io.c set_bit_hook, used to track delayed allocation
1485 * bytes in this file, and to maintain the list of inodes that
1486 * have pending delalloc work to be done.
1487 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001488static void btrfs_set_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001489 struct extent_state *state, unsigned long *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001490{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001491
Chris Mason75eff682008-12-15 15:54:40 -05001492 /*
1493 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001494 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001495 * bit, which is only set or cleared with irqs on
1496 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001497 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001498 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001499 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001500 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001501
Josef Bacik9e0baf62011-07-15 15:16:44 +00001502 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001503 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001504 } else {
1505 spin_lock(&BTRFS_I(inode)->lock);
1506 BTRFS_I(inode)->outstanding_extents++;
1507 spin_unlock(&BTRFS_I(inode)->lock);
1508 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001509
Miao Xie963d6782013-01-29 10:10:51 +00001510 __percpu_counter_add(&root->fs_info->delalloc_bytes, len,
1511 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001512 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001513 BTRFS_I(inode)->delalloc_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001514 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001515 &BTRFS_I(inode)->runtime_flags))
1516 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001517 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001518 }
Chris Mason291d6732008-01-29 15:55:23 -05001519}
1520
Chris Masond352ac62008-09-29 15:18:18 -04001521/*
1522 * extent_io.c clear_bit_hook, see set_bit_hook for why
1523 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001524static void btrfs_clear_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001525 struct extent_state *state,
1526 unsigned long *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001527{
Chris Mason75eff682008-12-15 15:54:40 -05001528 /*
1529 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001530 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001531 * bit, which is only set or cleared with irqs on
1532 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001533 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001534 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001535 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001536 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001537
Josef Bacik9e0baf62011-07-15 15:16:44 +00001538 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001539 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001540 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1541 spin_lock(&BTRFS_I(inode)->lock);
1542 BTRFS_I(inode)->outstanding_extents--;
1543 spin_unlock(&BTRFS_I(inode)->lock);
1544 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001545
Josef Bacikb6d08f02013-09-27 14:57:43 -04001546 /*
1547 * We don't reserve metadata space for space cache inodes so we
1548 * don't need to call dellalloc_release_metadata if there is an
1549 * error.
1550 */
1551 if (*bits & EXTENT_DO_ACCOUNTING &&
1552 root != root->fs_info->tree_root)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001553 btrfs_delalloc_release_metadata(inode, len);
1554
Josef Bacik0cb59c92010-07-02 12:14:14 -04001555 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Josef Bacik7ee9e442013-06-21 16:37:03 -04001556 && do_list && !(state->state & EXTENT_NORESERVE))
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001557 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001558
Miao Xie963d6782013-01-29 10:10:51 +00001559 __percpu_counter_add(&root->fs_info->delalloc_bytes, -len,
1560 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001561 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001562 BTRFS_I(inode)->delalloc_bytes -= len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001563 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001564 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001565 &BTRFS_I(inode)->runtime_flags))
1566 btrfs_del_delalloc_inode(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001567 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001568 }
Chris Mason291d6732008-01-29 15:55:23 -05001569}
1570
Chris Masond352ac62008-09-29 15:18:18 -04001571/*
1572 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1573 * we don't create bios that span stripes or chunks
1574 */
David Woodhouse64a16702009-07-15 23:29:37 +01001575int btrfs_merge_bio_hook(int rw, struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001576 size_t size, struct bio *bio,
1577 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001578{
1579 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Chris Masona62b9402008-10-03 16:31:08 -04001580 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001581 u64 length = 0;
1582 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001583 int ret;
1584
Chris Mason771ed682008-11-06 22:02:51 -05001585 if (bio_flags & EXTENT_BIO_COMPRESSED)
1586 return 0;
1587
Chris Masonf2d8d742008-04-21 10:03:05 -04001588 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001589 map_length = length;
David Woodhouse64a16702009-07-15 23:29:37 +01001590 ret = btrfs_map_block(root->fs_info, rw, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001591 &map_length, NULL, 0);
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001592 /* Will always return 0 with map_multi == NULL */
Jeff Mahoney3444a972011-10-03 23:23:13 -04001593 BUG_ON(ret < 0);
Chris Masond3977122009-01-05 21:25:51 -05001594 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001595 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001596 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001597}
1598
Chris Masond352ac62008-09-29 15:18:18 -04001599/*
1600 * in order to insert checksums into the metadata in large chunks,
1601 * we wait until bio submission time. All the pages in the bio are
1602 * checksummed and sums are attached onto the ordered extent record.
1603 *
1604 * At IO completion time the cums attached on the ordered extent record
1605 * are inserted into the btree
1606 */
Chris Masond3977122009-01-05 21:25:51 -05001607static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1608 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001609 unsigned long bio_flags,
1610 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001611{
Chris Mason065631f2008-02-20 12:07:25 -05001612 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001613 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001614
Chris Masond20f7042008-12-08 16:58:54 -05001615 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001616 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001617 return 0;
1618}
Chris Masone0156402008-04-16 11:15:20 -04001619
Chris Mason4a69a412008-11-06 22:03:00 -05001620/*
1621 * in order to insert checksums into the metadata in large chunks,
1622 * we wait until bio submission time. All the pages in the bio are
1623 * checksummed and sums are attached onto the ordered extent record.
1624 *
1625 * At IO completion time the cums attached on the ordered extent record
1626 * are inserted into the btree
1627 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001628static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001629 int mirror_num, unsigned long bio_flags,
1630 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001631{
1632 struct btrfs_root *root = BTRFS_I(inode)->root;
Stefan Behrens61891922012-11-05 18:51:52 +01001633 int ret;
1634
1635 ret = btrfs_map_bio(root, rw, bio, mirror_num, 1);
1636 if (ret)
1637 bio_endio(bio, ret);
1638 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001639}
1640
Chris Masond352ac62008-09-29 15:18:18 -04001641/*
Chris Masoncad321a2008-12-17 14:51:42 -05001642 * extent_io.c submission hook. This does the right thing for csum calculation
1643 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001644 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001645static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001646 int mirror_num, unsigned long bio_flags,
1647 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001648{
1649 struct btrfs_root *root = BTRFS_I(inode)->root;
1650 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001651 int skip_sum;
Jeff Mahoney04173412011-10-03 23:23:12 -04001652 int metadata = 0;
Josef Bacikb812ce22012-11-16 13:56:32 -05001653 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001654
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001655 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001656
Liu Bo83eea1f2012-07-10 05:28:39 -06001657 if (btrfs_is_free_space_inode(inode))
Jeff Mahoney04173412011-10-03 23:23:12 -04001658 metadata = 2;
1659
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001660 if (!(rw & REQ_WRITE)) {
Josef Bacik5fd02042012-05-02 14:00:54 -04001661 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1662 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001663 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001664
Chris Masond20f7042008-12-08 16:58:54 -05001665 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001666 ret = btrfs_submit_compressed_read(inode, bio,
1667 mirror_num,
1668 bio_flags);
1669 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001670 } else if (!skip_sum) {
1671 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1672 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001673 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001674 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001675 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001676 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001677 /* csum items have already been cloned */
1678 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1679 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001680 /* we're doing a write, do the async checksumming */
Stefan Behrens61891922012-11-05 18:51:52 +01001681 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001682 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001683 bio_flags, bio_offset,
1684 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001685 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001686 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001687 } else if (!skip_sum) {
1688 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1689 if (ret)
1690 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001691 }
1692
Chris Mason0b86a832008-03-24 15:01:56 -04001693mapit:
Stefan Behrens61891922012-11-05 18:51:52 +01001694 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
1695
1696out:
1697 if (ret < 0)
1698 bio_endio(bio, ret);
1699 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001700}
Chris Mason6885f302008-02-20 16:11:05 -05001701
Chris Masond352ac62008-09-29 15:18:18 -04001702/*
1703 * given a list of ordered sums record them in the inode. This happens
1704 * at IO completion time based on sums calculated at bio submission time.
1705 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001706static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001707 struct inode *inode, u64 file_offset,
1708 struct list_head *list)
1709{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001710 struct btrfs_ordered_sum *sum;
1711
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001712 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00001713 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05001714 btrfs_csum_file_blocks(trans,
1715 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00001716 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001717 }
1718 return 0;
1719}
1720
Josef Bacik2ac55d42010-02-03 19:33:23 +00001721int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1722 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001723{
Julia Lawall6c1500f2012-11-03 20:30:18 +00001724 WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04001725 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001726 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001727}
1728
Chris Masond352ac62008-09-29 15:18:18 -04001729/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001730struct btrfs_writepage_fixup {
1731 struct page *page;
1732 struct btrfs_work work;
1733};
1734
Christoph Hellwigb2950862008-12-02 09:54:17 -05001735static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001736{
1737 struct btrfs_writepage_fixup *fixup;
1738 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001739 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001740 struct page *page;
1741 struct inode *inode;
1742 u64 page_start;
1743 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001744 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001745
1746 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1747 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001748again:
Chris Mason247e7432008-07-17 12:53:51 -04001749 lock_page(page);
1750 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1751 ClearPageChecked(page);
1752 goto out_page;
1753 }
1754
1755 inode = page->mapping->host;
1756 page_start = page_offset(page);
1757 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1758
Josef Bacik2ac55d42010-02-03 19:33:23 +00001759 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001760 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001761
1762 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001763 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001764 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001765
1766 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1767 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001768 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1769 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001770 unlock_page(page);
1771 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001772 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04001773 goto again;
1774 }
Chris Mason247e7432008-07-17 12:53:51 -04001775
Jeff Mahoney87826df2012-02-15 16:23:57 +01001776 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
1777 if (ret) {
1778 mapping_set_error(page->mapping, ret);
1779 end_extent_writepage(page, ret, page_start, page_end);
1780 ClearPageChecked(page);
1781 goto out;
1782 }
1783
Josef Bacik2ac55d42010-02-03 19:33:23 +00001784 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001785 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001786 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04001787out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001788 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1789 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001790out_page:
1791 unlock_page(page);
1792 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08001793 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04001794}
1795
1796/*
1797 * There are a few paths in the higher layers of the kernel that directly
1798 * set the page dirty bit without asking the filesystem if it is a
1799 * good idea. This causes problems because we want to make sure COW
1800 * properly happens and the data=ordered rules are followed.
1801 *
Chris Masonc8b97812008-10-29 14:49:59 -04001802 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001803 * hasn't been properly setup for IO. We kick off an async process
1804 * to fix it up. The async helper will wait for ordered extents, set
1805 * the delalloc bit and make it safe to write the page.
1806 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001807static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001808{
1809 struct inode *inode = page->mapping->host;
1810 struct btrfs_writepage_fixup *fixup;
1811 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001812
Chris Mason8b62b722009-09-02 16:53:46 -04001813 /* this page is properly in the ordered list */
1814 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001815 return 0;
1816
1817 if (PageChecked(page))
1818 return -EAGAIN;
1819
1820 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1821 if (!fixup)
1822 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001823
Chris Mason247e7432008-07-17 12:53:51 -04001824 SetPageChecked(page);
1825 page_cache_get(page);
1826 fixup->work.func = btrfs_writepage_fixup_worker;
1827 fixup->page = page;
1828 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001829 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04001830}
1831
Yan Zhengd899e052008-10-30 14:25:28 -04001832static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1833 struct inode *inode, u64 file_pos,
1834 u64 disk_bytenr, u64 disk_num_bytes,
1835 u64 num_bytes, u64 ram_bytes,
1836 u8 compression, u8 encryption,
1837 u16 other_encoding, int extent_type)
1838{
1839 struct btrfs_root *root = BTRFS_I(inode)->root;
1840 struct btrfs_file_extent_item *fi;
1841 struct btrfs_path *path;
1842 struct extent_buffer *leaf;
1843 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04001844 int ret;
1845
1846 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07001847 if (!path)
1848 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04001849
Chris Masonb9473432009-03-13 11:00:37 -04001850 path->leave_spinning = 1;
Chris Masona1ed8352009-09-11 12:27:37 -04001851
1852 /*
1853 * we may be replacing one extent in the tree with another.
1854 * The new extent is pinned in the extent map, and we don't want
1855 * to drop it from the cache until it is completely in the btree.
1856 *
1857 * So, tell btrfs_drop_extents to leave this extent in the cache.
1858 * the caller is expected to unpin it and allow it to be merged
1859 * with the others.
1860 */
Josef Bacik5dc562c2012-08-17 13:14:17 -04001861 ret = btrfs_drop_extents(trans, root, inode, file_pos,
Josef Bacik26714852012-08-29 12:24:27 -04001862 file_pos + num_bytes, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001863 if (ret)
1864 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001865
Li Zefan33345d012011-04-20 10:31:50 +08001866 ins.objectid = btrfs_ino(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04001867 ins.offset = file_pos;
1868 ins.type = BTRFS_EXTENT_DATA_KEY;
1869 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001870 if (ret)
1871 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001872 leaf = path->nodes[0];
1873 fi = btrfs_item_ptr(leaf, path->slots[0],
1874 struct btrfs_file_extent_item);
1875 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1876 btrfs_set_file_extent_type(leaf, fi, extent_type);
1877 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1878 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1879 btrfs_set_file_extent_offset(leaf, fi, 0);
1880 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1881 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1882 btrfs_set_file_extent_compression(leaf, fi, compression);
1883 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1884 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04001885
Yan Zhengd899e052008-10-30 14:25:28 -04001886 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04001887 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04001888
1889 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04001890
1891 ins.objectid = disk_bytenr;
1892 ins.offset = disk_num_bytes;
1893 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001894 ret = btrfs_alloc_reserved_file_extent(trans, root,
1895 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08001896 btrfs_ino(inode), file_pos, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001897out:
Yan Zhengd899e052008-10-30 14:25:28 -04001898 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04001899
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001900 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04001901}
1902
Liu Bo38c227d2013-01-29 03:18:40 +00001903/* snapshot-aware defrag */
1904struct sa_defrag_extent_backref {
1905 struct rb_node node;
1906 struct old_sa_defrag_extent *old;
1907 u64 root_id;
1908 u64 inum;
1909 u64 file_pos;
1910 u64 extent_offset;
1911 u64 num_bytes;
1912 u64 generation;
1913};
1914
1915struct old_sa_defrag_extent {
1916 struct list_head list;
1917 struct new_sa_defrag_extent *new;
1918
1919 u64 extent_offset;
1920 u64 bytenr;
1921 u64 offset;
1922 u64 len;
1923 int count;
1924};
1925
1926struct new_sa_defrag_extent {
1927 struct rb_root root;
1928 struct list_head head;
1929 struct btrfs_path *path;
1930 struct inode *inode;
1931 u64 file_pos;
1932 u64 len;
1933 u64 bytenr;
1934 u64 disk_len;
1935 u8 compress_type;
1936};
1937
1938static int backref_comp(struct sa_defrag_extent_backref *b1,
1939 struct sa_defrag_extent_backref *b2)
1940{
1941 if (b1->root_id < b2->root_id)
1942 return -1;
1943 else if (b1->root_id > b2->root_id)
1944 return 1;
1945
1946 if (b1->inum < b2->inum)
1947 return -1;
1948 else if (b1->inum > b2->inum)
1949 return 1;
1950
1951 if (b1->file_pos < b2->file_pos)
1952 return -1;
1953 else if (b1->file_pos > b2->file_pos)
1954 return 1;
1955
1956 /*
1957 * [------------------------------] ===> (a range of space)
1958 * |<--->| |<---->| =============> (fs/file tree A)
1959 * |<---------------------------->| ===> (fs/file tree B)
1960 *
1961 * A range of space can refer to two file extents in one tree while
1962 * refer to only one file extent in another tree.
1963 *
1964 * So we may process a disk offset more than one time(two extents in A)
1965 * and locate at the same extent(one extent in B), then insert two same
1966 * backrefs(both refer to the extent in B).
1967 */
1968 return 0;
1969}
1970
1971static void backref_insert(struct rb_root *root,
1972 struct sa_defrag_extent_backref *backref)
1973{
1974 struct rb_node **p = &root->rb_node;
1975 struct rb_node *parent = NULL;
1976 struct sa_defrag_extent_backref *entry;
1977 int ret;
1978
1979 while (*p) {
1980 parent = *p;
1981 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
1982
1983 ret = backref_comp(backref, entry);
1984 if (ret < 0)
1985 p = &(*p)->rb_left;
1986 else
1987 p = &(*p)->rb_right;
1988 }
1989
1990 rb_link_node(&backref->node, parent, p);
1991 rb_insert_color(&backref->node, root);
1992}
1993
1994/*
1995 * Note the backref might has changed, and in this case we just return 0.
1996 */
1997static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
1998 void *ctx)
1999{
2000 struct btrfs_file_extent_item *extent;
2001 struct btrfs_fs_info *fs_info;
2002 struct old_sa_defrag_extent *old = ctx;
2003 struct new_sa_defrag_extent *new = old->new;
2004 struct btrfs_path *path = new->path;
2005 struct btrfs_key key;
2006 struct btrfs_root *root;
2007 struct sa_defrag_extent_backref *backref;
2008 struct extent_buffer *leaf;
2009 struct inode *inode = new->inode;
2010 int slot;
2011 int ret;
2012 u64 extent_offset;
2013 u64 num_bytes;
2014
2015 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
2016 inum == btrfs_ino(inode))
2017 return 0;
2018
2019 key.objectid = root_id;
2020 key.type = BTRFS_ROOT_ITEM_KEY;
2021 key.offset = (u64)-1;
2022
2023 fs_info = BTRFS_I(inode)->root->fs_info;
2024 root = btrfs_read_fs_root_no_name(fs_info, &key);
2025 if (IS_ERR(root)) {
2026 if (PTR_ERR(root) == -ENOENT)
2027 return 0;
2028 WARN_ON(1);
2029 pr_debug("inum=%llu, offset=%llu, root_id=%llu\n",
2030 inum, offset, root_id);
2031 return PTR_ERR(root);
2032 }
2033
2034 key.objectid = inum;
2035 key.type = BTRFS_EXTENT_DATA_KEY;
2036 if (offset > (u64)-1 << 32)
2037 key.offset = 0;
2038 else
2039 key.offset = offset;
2040
2041 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2042 if (ret < 0) {
2043 WARN_ON(1);
2044 return ret;
2045 }
Josef Bacik50f13192013-07-22 12:50:37 -04002046 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002047
2048 while (1) {
2049 cond_resched();
2050
2051 leaf = path->nodes[0];
2052 slot = path->slots[0];
2053
2054 if (slot >= btrfs_header_nritems(leaf)) {
2055 ret = btrfs_next_leaf(root, path);
2056 if (ret < 0) {
2057 goto out;
2058 } else if (ret > 0) {
2059 ret = 0;
2060 goto out;
2061 }
2062 continue;
2063 }
2064
2065 path->slots[0]++;
2066
2067 btrfs_item_key_to_cpu(leaf, &key, slot);
2068
2069 if (key.objectid > inum)
2070 goto out;
2071
2072 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2073 continue;
2074
2075 extent = btrfs_item_ptr(leaf, slot,
2076 struct btrfs_file_extent_item);
2077
2078 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2079 continue;
2080
Liu Boe68afa42013-07-01 22:13:26 +08002081 /*
2082 * 'offset' refers to the exact key.offset,
2083 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2084 * (key.offset - extent_offset).
2085 */
2086 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002087 continue;
2088
Liu Boe68afa42013-07-01 22:13:26 +08002089 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002090 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002091
Liu Bo38c227d2013-01-29 03:18:40 +00002092 if (extent_offset >= old->extent_offset + old->offset +
2093 old->len || extent_offset + num_bytes <=
2094 old->extent_offset + old->offset)
2095 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002096 break;
2097 }
2098
2099 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2100 if (!backref) {
2101 ret = -ENOENT;
2102 goto out;
2103 }
2104
2105 backref->root_id = root_id;
2106 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002107 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002108 backref->num_bytes = num_bytes;
2109 backref->extent_offset = extent_offset;
2110 backref->generation = btrfs_file_extent_generation(leaf, extent);
2111 backref->old = old;
2112 backref_insert(&new->root, backref);
2113 old->count++;
2114out:
2115 btrfs_release_path(path);
2116 WARN_ON(ret);
2117 return ret;
2118}
2119
2120static noinline bool record_extent_backrefs(struct btrfs_path *path,
2121 struct new_sa_defrag_extent *new)
2122{
2123 struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info;
2124 struct old_sa_defrag_extent *old, *tmp;
2125 int ret;
2126
2127 new->path = path;
2128
2129 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002130 ret = iterate_inodes_from_logical(old->bytenr +
2131 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002132 path, record_one_backref,
2133 old);
2134 BUG_ON(ret < 0 && ret != -ENOENT);
2135
2136 /* no backref to be processed for this extent */
2137 if (!old->count) {
2138 list_del(&old->list);
2139 kfree(old);
2140 }
2141 }
2142
2143 if (list_empty(&new->head))
2144 return false;
2145
2146 return true;
2147}
2148
2149static int relink_is_mergable(struct extent_buffer *leaf,
2150 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002151 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002152{
Liu Bo116e0022013-08-02 16:30:40 +08002153 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002154 return 0;
2155
2156 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2157 return 0;
2158
Liu Bo116e0022013-08-02 16:30:40 +08002159 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2160 return 0;
2161
2162 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002163 btrfs_file_extent_other_encoding(leaf, fi))
2164 return 0;
2165
2166 return 1;
2167}
2168
2169/*
2170 * Note the backref might has changed, and in this case we just return 0.
2171 */
2172static noinline int relink_extent_backref(struct btrfs_path *path,
2173 struct sa_defrag_extent_backref *prev,
2174 struct sa_defrag_extent_backref *backref)
2175{
2176 struct btrfs_file_extent_item *extent;
2177 struct btrfs_file_extent_item *item;
2178 struct btrfs_ordered_extent *ordered;
2179 struct btrfs_trans_handle *trans;
2180 struct btrfs_fs_info *fs_info;
2181 struct btrfs_root *root;
2182 struct btrfs_key key;
2183 struct extent_buffer *leaf;
2184 struct old_sa_defrag_extent *old = backref->old;
2185 struct new_sa_defrag_extent *new = old->new;
2186 struct inode *src_inode = new->inode;
2187 struct inode *inode;
2188 struct extent_state *cached = NULL;
2189 int ret = 0;
2190 u64 start;
2191 u64 len;
2192 u64 lock_start;
2193 u64 lock_end;
2194 bool merge = false;
2195 int index;
2196
2197 if (prev && prev->root_id == backref->root_id &&
2198 prev->inum == backref->inum &&
2199 prev->file_pos + prev->num_bytes == backref->file_pos)
2200 merge = true;
2201
2202 /* step 1: get root */
2203 key.objectid = backref->root_id;
2204 key.type = BTRFS_ROOT_ITEM_KEY;
2205 key.offset = (u64)-1;
2206
2207 fs_info = BTRFS_I(src_inode)->root->fs_info;
2208 index = srcu_read_lock(&fs_info->subvol_srcu);
2209
2210 root = btrfs_read_fs_root_no_name(fs_info, &key);
2211 if (IS_ERR(root)) {
2212 srcu_read_unlock(&fs_info->subvol_srcu, index);
2213 if (PTR_ERR(root) == -ENOENT)
2214 return 0;
2215 return PTR_ERR(root);
2216 }
Liu Bo38c227d2013-01-29 03:18:40 +00002217
2218 /* step 2: get inode */
2219 key.objectid = backref->inum;
2220 key.type = BTRFS_INODE_ITEM_KEY;
2221 key.offset = 0;
2222
2223 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2224 if (IS_ERR(inode)) {
2225 srcu_read_unlock(&fs_info->subvol_srcu, index);
2226 return 0;
2227 }
2228
2229 srcu_read_unlock(&fs_info->subvol_srcu, index);
2230
2231 /* step 3: relink backref */
2232 lock_start = backref->file_pos;
2233 lock_end = backref->file_pos + backref->num_bytes - 1;
2234 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2235 0, &cached);
2236
2237 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2238 if (ordered) {
2239 btrfs_put_ordered_extent(ordered);
2240 goto out_unlock;
2241 }
2242
2243 trans = btrfs_join_transaction(root);
2244 if (IS_ERR(trans)) {
2245 ret = PTR_ERR(trans);
2246 goto out_unlock;
2247 }
2248
2249 key.objectid = backref->inum;
2250 key.type = BTRFS_EXTENT_DATA_KEY;
2251 key.offset = backref->file_pos;
2252
2253 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2254 if (ret < 0) {
2255 goto out_free_path;
2256 } else if (ret > 0) {
2257 ret = 0;
2258 goto out_free_path;
2259 }
2260
2261 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2262 struct btrfs_file_extent_item);
2263
2264 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2265 backref->generation)
2266 goto out_free_path;
2267
2268 btrfs_release_path(path);
2269
2270 start = backref->file_pos;
2271 if (backref->extent_offset < old->extent_offset + old->offset)
2272 start += old->extent_offset + old->offset -
2273 backref->extent_offset;
2274
2275 len = min(backref->extent_offset + backref->num_bytes,
2276 old->extent_offset + old->offset + old->len);
2277 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2278
2279 ret = btrfs_drop_extents(trans, root, inode, start,
2280 start + len, 1);
2281 if (ret)
2282 goto out_free_path;
2283again:
2284 key.objectid = btrfs_ino(inode);
2285 key.type = BTRFS_EXTENT_DATA_KEY;
2286 key.offset = start;
2287
Liu Boa09a0a72013-03-11 09:20:58 +00002288 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002289 if (merge) {
2290 struct btrfs_file_extent_item *fi;
2291 u64 extent_len;
2292 struct btrfs_key found_key;
2293
2294 ret = btrfs_search_slot(trans, root, &key, path, 1, 1);
2295 if (ret < 0)
2296 goto out_free_path;
2297
2298 path->slots[0]--;
2299 leaf = path->nodes[0];
2300 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2301
2302 fi = btrfs_item_ptr(leaf, path->slots[0],
2303 struct btrfs_file_extent_item);
2304 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2305
Liu Bo116e0022013-08-02 16:30:40 +08002306 if (extent_len + found_key.offset == start &&
2307 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002308 btrfs_set_file_extent_num_bytes(leaf, fi,
2309 extent_len + len);
2310 btrfs_mark_buffer_dirty(leaf);
2311 inode_add_bytes(inode, len);
2312
2313 ret = 1;
2314 goto out_free_path;
2315 } else {
2316 merge = false;
2317 btrfs_release_path(path);
2318 goto again;
2319 }
2320 }
2321
2322 ret = btrfs_insert_empty_item(trans, root, path, &key,
2323 sizeof(*extent));
2324 if (ret) {
2325 btrfs_abort_transaction(trans, root, ret);
2326 goto out_free_path;
2327 }
2328
2329 leaf = path->nodes[0];
2330 item = btrfs_item_ptr(leaf, path->slots[0],
2331 struct btrfs_file_extent_item);
2332 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2333 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2334 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2335 btrfs_set_file_extent_num_bytes(leaf, item, len);
2336 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2337 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2338 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2339 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2340 btrfs_set_file_extent_encryption(leaf, item, 0);
2341 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2342
2343 btrfs_mark_buffer_dirty(leaf);
2344 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002345 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002346
2347 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
2348 new->disk_len, 0,
2349 backref->root_id, backref->inum,
2350 new->file_pos, 0); /* start - extent_offset */
2351 if (ret) {
2352 btrfs_abort_transaction(trans, root, ret);
2353 goto out_free_path;
2354 }
2355
2356 ret = 1;
2357out_free_path:
2358 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002359 path->leave_spinning = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002360 btrfs_end_transaction(trans, root);
2361out_unlock:
2362 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2363 &cached, GFP_NOFS);
2364 iput(inode);
2365 return ret;
2366}
2367
Liu Bo6f519562013-10-29 10:45:05 +08002368static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2369{
2370 struct old_sa_defrag_extent *old, *tmp;
2371
2372 if (!new)
2373 return;
2374
2375 list_for_each_entry_safe(old, tmp, &new->head, list) {
2376 list_del(&old->list);
2377 kfree(old);
2378 }
2379 kfree(new);
2380}
2381
Liu Bo38c227d2013-01-29 03:18:40 +00002382static void relink_file_extents(struct new_sa_defrag_extent *new)
2383{
2384 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002385 struct sa_defrag_extent_backref *backref;
2386 struct sa_defrag_extent_backref *prev = NULL;
2387 struct inode *inode;
2388 struct btrfs_root *root;
2389 struct rb_node *node;
2390 int ret;
2391
2392 inode = new->inode;
2393 root = BTRFS_I(inode)->root;
2394
2395 path = btrfs_alloc_path();
2396 if (!path)
2397 return;
2398
2399 if (!record_extent_backrefs(path, new)) {
2400 btrfs_free_path(path);
2401 goto out;
2402 }
2403 btrfs_release_path(path);
2404
2405 while (1) {
2406 node = rb_first(&new->root);
2407 if (!node)
2408 break;
2409 rb_erase(node, &new->root);
2410
2411 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2412
2413 ret = relink_extent_backref(path, prev, backref);
2414 WARN_ON(ret < 0);
2415
2416 kfree(prev);
2417
2418 if (ret == 1)
2419 prev = backref;
2420 else
2421 prev = NULL;
2422 cond_resched();
2423 }
2424 kfree(prev);
2425
2426 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002427out:
Liu Bo6f519562013-10-29 10:45:05 +08002428 free_sa_defrag_extent(new);
2429
Liu Bo38c227d2013-01-29 03:18:40 +00002430 atomic_dec(&root->fs_info->defrag_running);
2431 wake_up(&root->fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002432}
2433
2434static struct new_sa_defrag_extent *
2435record_old_file_extents(struct inode *inode,
2436 struct btrfs_ordered_extent *ordered)
2437{
2438 struct btrfs_root *root = BTRFS_I(inode)->root;
2439 struct btrfs_path *path;
2440 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002441 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002442 struct new_sa_defrag_extent *new;
2443 int ret;
2444
2445 new = kmalloc(sizeof(*new), GFP_NOFS);
2446 if (!new)
2447 return NULL;
2448
2449 new->inode = inode;
2450 new->file_pos = ordered->file_offset;
2451 new->len = ordered->len;
2452 new->bytenr = ordered->start;
2453 new->disk_len = ordered->disk_len;
2454 new->compress_type = ordered->compress_type;
2455 new->root = RB_ROOT;
2456 INIT_LIST_HEAD(&new->head);
2457
2458 path = btrfs_alloc_path();
2459 if (!path)
2460 goto out_kfree;
2461
2462 key.objectid = btrfs_ino(inode);
2463 key.type = BTRFS_EXTENT_DATA_KEY;
2464 key.offset = new->file_pos;
2465
2466 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2467 if (ret < 0)
2468 goto out_free_path;
2469 if (ret > 0 && path->slots[0] > 0)
2470 path->slots[0]--;
2471
2472 /* find out all the old extents for the file range */
2473 while (1) {
2474 struct btrfs_file_extent_item *extent;
2475 struct extent_buffer *l;
2476 int slot;
2477 u64 num_bytes;
2478 u64 offset;
2479 u64 end;
2480 u64 disk_bytenr;
2481 u64 extent_offset;
2482
2483 l = path->nodes[0];
2484 slot = path->slots[0];
2485
2486 if (slot >= btrfs_header_nritems(l)) {
2487 ret = btrfs_next_leaf(root, path);
2488 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002489 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002490 else if (ret > 0)
2491 break;
2492 continue;
2493 }
2494
2495 btrfs_item_key_to_cpu(l, &key, slot);
2496
2497 if (key.objectid != btrfs_ino(inode))
2498 break;
2499 if (key.type != BTRFS_EXTENT_DATA_KEY)
2500 break;
2501 if (key.offset >= new->file_pos + new->len)
2502 break;
2503
2504 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2505
2506 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2507 if (key.offset + num_bytes < new->file_pos)
2508 goto next;
2509
2510 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2511 if (!disk_bytenr)
2512 goto next;
2513
2514 extent_offset = btrfs_file_extent_offset(l, extent);
2515
2516 old = kmalloc(sizeof(*old), GFP_NOFS);
2517 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002518 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002519
2520 offset = max(new->file_pos, key.offset);
2521 end = min(new->file_pos + new->len, key.offset + num_bytes);
2522
2523 old->bytenr = disk_bytenr;
2524 old->extent_offset = extent_offset;
2525 old->offset = offset - key.offset;
2526 old->len = end - offset;
2527 old->new = new;
2528 old->count = 0;
2529 list_add_tail(&old->list, &new->head);
2530next:
2531 path->slots[0]++;
2532 cond_resched();
2533 }
2534
2535 btrfs_free_path(path);
2536 atomic_inc(&root->fs_info->defrag_running);
2537
2538 return new;
2539
Liu Bo38c227d2013-01-29 03:18:40 +00002540out_free_path:
2541 btrfs_free_path(path);
2542out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002543 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002544 return NULL;
2545}
2546
Chris Mason5d13a982009-03-13 11:41:46 -04002547/*
2548 * helper function for btrfs_finish_ordered_io, this
2549 * just reads in some of the csum leaves to prime them into ram
2550 * before we start the transaction. It limits the amount of btree
2551 * reads required while inside the transaction.
2552 */
Chris Masond352ac62008-09-29 15:18:18 -04002553/* as ordered data IO finishes, this gets called so we can finish
2554 * an ordered extent if the range of bytes in the file it covers are
2555 * fully written.
2556 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002557static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002558{
Josef Bacik5fd02042012-05-02 14:00:54 -04002559 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002560 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002561 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002562 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002563 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002564 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002565 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002566 int ret = 0;
2567 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002568 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002569 bool truncated = false;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002570
Liu Bo83eea1f2012-07-10 05:28:39 -06002571 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002572
Josef Bacik5fd02042012-05-02 14:00:54 -04002573 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2574 ret = -EIO;
2575 goto out;
2576 }
2577
Josef Bacik77cef2e2013-08-29 13:57:21 -04002578 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2579 truncated = true;
2580 logical_len = ordered_extent->truncated_len;
2581 /* Truncated the entire extent, don't bother adding */
2582 if (!logical_len)
2583 goto out;
2584 }
2585
Yan, Zhengc2167752009-11-12 09:34:21 +00002586 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002587 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Josef Bacik6c760c02012-11-09 10:53:21 -05002588 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2589 if (nolock)
2590 trans = btrfs_join_transaction_nolock(root);
2591 else
2592 trans = btrfs_join_transaction(root);
2593 if (IS_ERR(trans)) {
2594 ret = PTR_ERR(trans);
2595 trans = NULL;
2596 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002597 }
Josef Bacik6c760c02012-11-09 10:53:21 -05002598 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
2599 ret = btrfs_update_inode_fallback(trans, root, inode);
2600 if (ret) /* -ENOMEM or corruption */
2601 btrfs_abort_transaction(trans, root, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002602 goto out;
2603 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002604
Josef Bacik2ac55d42010-02-03 19:33:23 +00002605 lock_extent_bits(io_tree, ordered_extent->file_offset,
2606 ordered_extent->file_offset + ordered_extent->len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002607 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002608
Liu Bo38c227d2013-01-29 03:18:40 +00002609 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2610 ordered_extent->file_offset + ordered_extent->len - 1,
2611 EXTENT_DEFRAG, 1, cached_state);
2612 if (ret) {
2613 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
2614 if (last_snapshot >= BTRFS_I(inode)->generation)
2615 /* the inode is shared */
2616 new = record_old_file_extents(inode, ordered_extent);
2617
2618 clear_extent_bit(io_tree, ordered_extent->file_offset,
2619 ordered_extent->file_offset + ordered_extent->len - 1,
2620 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2621 }
2622
Josef Bacik0cb59c92010-07-02 12:14:14 -04002623 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002624 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002625 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002626 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002627 if (IS_ERR(trans)) {
2628 ret = PTR_ERR(trans);
2629 trans = NULL;
2630 goto out_unlock;
2631 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002632 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002633
Chris Masonc8b97812008-10-29 14:49:59 -04002634 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002635 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002636 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002637 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00002638 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002639 ordered_extent->file_offset,
2640 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002641 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002642 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04002643 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002644 ret = insert_reserved_file_extent(trans, inode,
2645 ordered_extent->file_offset,
2646 ordered_extent->start,
2647 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04002648 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08002649 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002650 BTRFS_FILE_EXTENT_REG);
Yan Zhengd899e052008-10-30 14:25:28 -04002651 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002652 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2653 ordered_extent->file_offset, ordered_extent->len,
2654 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002655 if (ret < 0) {
2656 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002657 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002658 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002659
Chris Masone6dcd2d2008-07-17 12:53:50 -04002660 add_pending_csums(trans, inode, ordered_extent->file_offset,
2661 &ordered_extent->list);
2662
Josef Bacik6c760c02012-11-09 10:53:21 -05002663 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2664 ret = btrfs_update_inode_fallback(trans, root, inode);
2665 if (ret) { /* -ENOMEM or corruption */
2666 btrfs_abort_transaction(trans, root, ret);
2667 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002668 }
2669 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002670out_unlock:
2671 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2672 ordered_extent->file_offset +
2673 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00002674out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002675 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04002676 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002677 if (trans)
2678 btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002679
Josef Bacik77cef2e2013-08-29 13:57:21 -04002680 if (ret || truncated) {
2681 u64 start, end;
2682
2683 if (truncated)
2684 start = ordered_extent->file_offset + logical_len;
2685 else
2686 start = ordered_extent->file_offset;
2687 end = ordered_extent->file_offset + ordered_extent->len - 1;
2688 clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS);
2689
2690 /* Drop the cache for the part of the extent we didn't write. */
2691 btrfs_drop_extent_cache(inode, start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04002692
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002693 /*
2694 * If the ordered extent had an IOERR or something else went
2695 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04002696 * back to the allocator. We only free the extent in the
2697 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002698 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04002699 if ((ret || !logical_len) &&
2700 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002701 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
2702 btrfs_free_reserved_extent(root, ordered_extent->start,
2703 ordered_extent->disk_len);
2704 }
2705
2706
Josef Bacik5fd02042012-05-02 14:00:54 -04002707 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002708 * This needs to be done to make sure anybody waiting knows we are done
2709 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002710 */
2711 btrfs_remove_ordered_extent(inode, ordered_extent);
2712
Liu Bo38c227d2013-01-29 03:18:40 +00002713 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08002714 if (new) {
2715 if (ret) {
2716 free_sa_defrag_extent(new);
2717 atomic_dec(&root->fs_info->defrag_running);
2718 } else {
2719 relink_file_extents(new);
2720 }
2721 }
Liu Bo38c227d2013-01-29 03:18:40 +00002722
Chris Masone6dcd2d2008-07-17 12:53:50 -04002723 /* once for us */
2724 btrfs_put_ordered_extent(ordered_extent);
2725 /* once for the tree */
2726 btrfs_put_ordered_extent(ordered_extent);
2727
Josef Bacik5fd02042012-05-02 14:00:54 -04002728 return ret;
2729}
2730
2731static void finish_ordered_fn(struct btrfs_work *work)
2732{
2733 struct btrfs_ordered_extent *ordered_extent;
2734 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2735 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002736}
2737
Christoph Hellwigb2950862008-12-02 09:54:17 -05002738static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04002739 struct extent_state *state, int uptodate)
2740{
Josef Bacik5fd02042012-05-02 14:00:54 -04002741 struct inode *inode = page->mapping->host;
2742 struct btrfs_root *root = BTRFS_I(inode)->root;
2743 struct btrfs_ordered_extent *ordered_extent = NULL;
2744 struct btrfs_workers *workers;
2745
liubo1abe9b82011-03-24 11:18:59 +00002746 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2747
Chris Mason8b62b722009-09-02 16:53:46 -04002748 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002749 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2750 end - start + 1, uptodate))
2751 return 0;
2752
2753 ordered_extent->work.func = finish_ordered_fn;
2754 ordered_extent->work.flags = 0;
2755
Liu Bo83eea1f2012-07-10 05:28:39 -06002756 if (btrfs_is_free_space_inode(inode))
Josef Bacik5fd02042012-05-02 14:00:54 -04002757 workers = &root->fs_info->endio_freespace_worker;
2758 else
2759 workers = &root->fs_info->endio_write_workers;
2760 btrfs_queue_worker(workers, &ordered_extent->work);
2761
2762 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04002763}
2764
Chris Masond352ac62008-09-29 15:18:18 -04002765/*
Chris Masond352ac62008-09-29 15:18:18 -04002766 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002767 * if there's a match, we allow the bio to finish. If not, the code in
2768 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002769 */
Miao Xiefacc8a222013-07-25 19:22:34 +08002770static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
2771 u64 phy_offset, struct page *page,
2772 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002773{
Miao Xie4eee4fa2012-12-21 09:17:45 +00002774 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04002775 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002776 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04002777 char *kaddr;
Chris Masonff79f812007-10-15 16:22:25 -04002778 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiefacc8a222013-07-25 19:22:34 +08002779 u32 csum_expected;
Chris Masonff79f812007-10-15 16:22:25 -04002780 u32 csum = ~(u32)0;
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002781 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
2782 DEFAULT_RATELIMIT_BURST);
Chris Masond1310b22008-01-24 16:13:08 -05002783
Chris Masond20f7042008-12-08 16:58:54 -05002784 if (PageChecked(page)) {
2785 ClearPageChecked(page);
2786 goto good;
2787 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002788
2789 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Jan Schmidt08d2f342011-05-04 16:18:50 +02002790 goto good;
Chris Masond20f7042008-12-08 16:58:54 -05002791
Yan Zheng17d217f2008-12-12 10:03:38 -05002792 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002793 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002794 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
2795 GFP_NOFS);
2796 return 0;
2797 }
2798
Miao Xiefacc8a222013-07-25 19:22:34 +08002799 phy_offset >>= inode->i_sb->s_blocksize_bits;
2800 csum_expected = *(((u32 *)io_bio->csum) + phy_offset);
Chris Masond3977122009-01-05 21:25:51 -05002801
Miao Xiefacc8a222013-07-25 19:22:34 +08002802 kaddr = kmap_atomic(page);
Liu Bob0496682013-03-14 14:57:45 +00002803 csum = btrfs_csum_data(kaddr + offset, csum, end - start + 1);
Chris Masonff79f812007-10-15 16:22:25 -04002804 btrfs_csum_final(csum, (char *)&csum);
Miao Xiefacc8a222013-07-25 19:22:34 +08002805 if (csum != csum_expected)
Chris Mason07157aa2007-08-30 08:50:51 -04002806 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002807
Cong Wang7ac687d2011-11-25 23:14:28 +08002808 kunmap_atomic(kaddr);
Chris Masond20f7042008-12-08 16:58:54 -05002809good:
Chris Mason07157aa2007-08-30 08:50:51 -04002810 return 0;
2811
2812zeroit:
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002813 if (__ratelimit(&_rs))
Miao Xiefacc8a222013-07-25 19:22:34 +08002814 btrfs_info(root->fs_info, "csum failed ino %llu off %llu csum %u expected csum %u",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002815 btrfs_ino(page->mapping->host), start, csum, csum_expected);
Chris Masondb945352007-10-15 16:15:53 -04002816 memset(kaddr + offset, 1, end - start + 1);
2817 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002818 kunmap_atomic(kaddr);
Miao Xiefacc8a222013-07-25 19:22:34 +08002819 if (csum_expected == 0)
Chris Mason3b951512008-04-17 11:29:12 -04002820 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04002821 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04002822}
Chris Masonb888db22007-08-27 16:49:44 -04002823
Yan, Zheng24bbcf042009-11-12 09:36:34 +00002824struct delayed_iput {
2825 struct list_head list;
2826 struct inode *inode;
2827};
2828
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002829/* JDM: If this is fs-wide, why can't we add a pointer to
2830 * btrfs_inode instead and avoid the allocation? */
Yan, Zheng24bbcf042009-11-12 09:36:34 +00002831void btrfs_add_delayed_iput(struct inode *inode)
2832{
2833 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2834 struct delayed_iput *delayed;
2835
2836 if (atomic_add_unless(&inode->i_count, -1, 1))
2837 return;
2838
2839 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2840 delayed->inode = inode;
2841
2842 spin_lock(&fs_info->delayed_iput_lock);
2843 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2844 spin_unlock(&fs_info->delayed_iput_lock);
2845}
2846
2847void btrfs_run_delayed_iputs(struct btrfs_root *root)
2848{
2849 LIST_HEAD(list);
2850 struct btrfs_fs_info *fs_info = root->fs_info;
2851 struct delayed_iput *delayed;
2852 int empty;
2853
2854 spin_lock(&fs_info->delayed_iput_lock);
2855 empty = list_empty(&fs_info->delayed_iputs);
2856 spin_unlock(&fs_info->delayed_iput_lock);
2857 if (empty)
2858 return;
2859
Yan, Zheng24bbcf042009-11-12 09:36:34 +00002860 spin_lock(&fs_info->delayed_iput_lock);
2861 list_splice_init(&fs_info->delayed_iputs, &list);
2862 spin_unlock(&fs_info->delayed_iput_lock);
2863
2864 while (!list_empty(&list)) {
2865 delayed = list_entry(list.next, struct delayed_iput, list);
2866 list_del(&delayed->list);
2867 iput(delayed->inode);
2868 kfree(delayed);
2869 }
Yan, Zheng24bbcf042009-11-12 09:36:34 +00002870}
2871
Yan, Zhengd68fc572010-05-16 10:49:58 -04002872/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08002873 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04002874 * files in the subvolume, it removes orphan item and frees block_rsv
2875 * structure.
2876 */
2877void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
2878 struct btrfs_root *root)
2879{
Josef Bacik90290e12011-12-02 15:44:12 -05002880 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002881 int ret;
2882
Josef Bacik8a35d952012-05-23 14:26:42 -04002883 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04002884 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
2885 return;
2886
Josef Bacik90290e12011-12-02 15:44:12 -05002887 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04002888 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05002889 spin_unlock(&root->orphan_lock);
2890 return;
2891 }
2892
2893 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
2894 spin_unlock(&root->orphan_lock);
2895 return;
2896 }
2897
2898 block_rsv = root->orphan_block_rsv;
2899 root->orphan_block_rsv = NULL;
2900 spin_unlock(&root->orphan_lock);
2901
Yan, Zhengd68fc572010-05-16 10:49:58 -04002902 if (root->orphan_item_inserted &&
2903 btrfs_root_refs(&root->root_item) > 0) {
2904 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
2905 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04002906 if (ret)
2907 btrfs_abort_transaction(trans, root, ret);
2908 else
2909 root->orphan_item_inserted = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002910 }
2911
Josef Bacik90290e12011-12-02 15:44:12 -05002912 if (block_rsv) {
2913 WARN_ON(block_rsv->size > 0);
2914 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002915 }
2916}
2917
2918/*
Josef Bacik7b128762008-07-24 12:17:14 -04002919 * This creates an orphan entry for the given inode in case something goes
2920 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04002921 *
2922 * NOTE: caller of this function should reserve 5 units of metadata for
2923 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04002924 */
2925int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2926{
2927 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002928 struct btrfs_block_rsv *block_rsv = NULL;
2929 int reserve = 0;
2930 int insert = 0;
2931 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002932
Yan, Zhengd68fc572010-05-16 10:49:58 -04002933 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06002934 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00002935 if (!block_rsv)
2936 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04002937 }
2938
Yan, Zhengd68fc572010-05-16 10:49:58 -04002939 spin_lock(&root->orphan_lock);
2940 if (!root->orphan_block_rsv) {
2941 root->orphan_block_rsv = block_rsv;
2942 } else if (block_rsv) {
2943 btrfs_free_block_rsv(root, block_rsv);
2944 block_rsv = NULL;
2945 }
Josef Bacik7b128762008-07-24 12:17:14 -04002946
Josef Bacik8a35d952012-05-23 14:26:42 -04002947 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2948 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04002949#if 0
2950 /*
2951 * For proper ENOSPC handling, we should do orphan
2952 * cleanup when mounting. But this introduces backward
2953 * compatibility issue.
2954 */
2955 if (!xchg(&root->orphan_item_inserted, 1))
2956 insert = 2;
2957 else
2958 insert = 1;
2959#endif
2960 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06002961 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002962 }
Josef Bacik7b128762008-07-24 12:17:14 -04002963
Josef Bacik72ac3c02012-05-23 14:13:11 -04002964 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
2965 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04002966 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002967 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002968
Yan, Zhengd68fc572010-05-16 10:49:58 -04002969 /* grab metadata reservation from transaction handle */
2970 if (reserve) {
2971 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002972 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04002973 }
2974
2975 /* insert an orphan item to track this unlinked/truncated file */
2976 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08002977 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04002978 if (ret) {
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01002979 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04002980 if (reserve) {
2981 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
2982 &BTRFS_I(inode)->runtime_flags);
2983 btrfs_orphan_release_metadata(inode);
2984 }
2985 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04002986 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2987 &BTRFS_I(inode)->runtime_flags);
Josef Bacik4ef31a42013-08-13 14:10:08 -04002988 btrfs_abort_transaction(trans, root, ret);
2989 return ret;
2990 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002991 }
2992 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002993 }
2994
2995 /* insert an orphan item to track subvolume contains orphan files */
2996 if (insert >= 2) {
2997 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
2998 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002999 if (ret && ret != -EEXIST) {
3000 btrfs_abort_transaction(trans, root, ret);
3001 return ret;
3002 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003003 }
3004 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003005}
3006
3007/*
3008 * We have done the truncate/delete so we can go ahead and remove the orphan
3009 * item for this particular inode.
3010 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003011static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3012 struct inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003013{
3014 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003015 int delete_item = 0;
3016 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003017 int ret = 0;
3018
Yan, Zhengd68fc572010-05-16 10:49:58 -04003019 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003020 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3021 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003022 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003023
Josef Bacik72ac3c02012-05-23 14:13:11 -04003024 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3025 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003026 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003027 spin_unlock(&root->orphan_lock);
3028
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003029 if (delete_item) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003030 atomic_dec(&root->orphan_inodes);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003031 if (trans)
3032 ret = btrfs_del_orphan_item(trans, root,
3033 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04003034 }
Josef Bacik7b128762008-07-24 12:17:14 -04003035
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003036 if (release_rsv)
3037 btrfs_orphan_release_metadata(inode);
3038
Josef Bacik4ef31a42013-08-13 14:10:08 -04003039 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003040}
3041
3042/*
3043 * this cleans up any orphans that may be left on the list from the last use
3044 * of this root.
3045 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003046int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003047{
3048 struct btrfs_path *path;
3049 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003050 struct btrfs_key key, found_key;
3051 struct btrfs_trans_handle *trans;
3052 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003053 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003054 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3055
Yan, Zhengd68fc572010-05-16 10:49:58 -04003056 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003057 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003058
3059 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003060 if (!path) {
3061 ret = -ENOMEM;
3062 goto out;
3063 }
Josef Bacik7b128762008-07-24 12:17:14 -04003064 path->reada = -1;
3065
3066 key.objectid = BTRFS_ORPHAN_OBJECTID;
3067 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
3068 key.offset = (u64)-1;
3069
Josef Bacik7b128762008-07-24 12:17:14 -04003070 while (1) {
3071 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003072 if (ret < 0)
3073 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003074
3075 /*
3076 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003077 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003078 * find the key and see if we have stuff that matches
3079 */
3080 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003081 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003082 if (path->slots[0] == 0)
3083 break;
3084 path->slots[0]--;
3085 }
3086
3087 /* pull out the item */
3088 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003089 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3090
3091 /* make sure the item matches what we want */
3092 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3093 break;
3094 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
3095 break;
3096
3097 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003098 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003099
3100 /*
3101 * this is where we are basically btrfs_lookup, without the
3102 * crossing root thing. we store the inode number in the
3103 * offset of the orphan item.
3104 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003105
3106 if (found_key.offset == last_objectid) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003107 btrfs_err(root->fs_info,
3108 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003109 ret = -EINVAL;
3110 goto out;
3111 }
3112
3113 last_objectid = found_key.offset;
3114
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003115 found_key.objectid = found_key.offset;
3116 found_key.type = BTRFS_INODE_ITEM_KEY;
3117 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00003118 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303119 ret = PTR_ERR_OR_ZERO(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003120 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003121 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003122
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003123 if (ret == -ESTALE && root == root->fs_info->tree_root) {
3124 struct btrfs_root *dead_root;
3125 struct btrfs_fs_info *fs_info = root->fs_info;
3126 int is_dead_root = 0;
3127
3128 /*
3129 * this is an orphan in the tree root. Currently these
3130 * could come from 2 sources:
3131 * a) a snapshot deletion in progress
3132 * b) a free space cache inode
3133 * We need to distinguish those two, as the snapshot
3134 * orphan must not get deleted.
3135 * find_dead_roots already ran before us, so if this
3136 * is a snapshot deletion, we should find the root
3137 * in the dead_roots list
3138 */
3139 spin_lock(&fs_info->trans_lock);
3140 list_for_each_entry(dead_root, &fs_info->dead_roots,
3141 root_list) {
3142 if (dead_root->root_key.objectid ==
3143 found_key.objectid) {
3144 is_dead_root = 1;
3145 break;
3146 }
3147 }
3148 spin_unlock(&fs_info->trans_lock);
3149 if (is_dead_root) {
3150 /* prevent this orphan from being found again */
3151 key.offset = found_key.objectid - 1;
3152 continue;
3153 }
3154 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003155 /*
3156 * Inode is already gone but the orphan item is still there,
3157 * kill the orphan item.
3158 */
3159 if (ret == -ESTALE) {
3160 trans = btrfs_start_transaction(root, 1);
3161 if (IS_ERR(trans)) {
3162 ret = PTR_ERR(trans);
3163 goto out;
3164 }
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003165 btrfs_debug(root->fs_info, "auto deleting %Lu",
3166 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003167 ret = btrfs_del_orphan_item(trans, root,
3168 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003169 btrfs_end_transaction(trans, root);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003170 if (ret)
3171 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003172 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003173 }
Josef Bacik7b128762008-07-24 12:17:14 -04003174
Josef Bacik7b128762008-07-24 12:17:14 -04003175 /*
3176 * add this inode to the orphan list so btrfs_orphan_del does
3177 * the proper thing when we hit it
3178 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003179 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3180 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003181 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003182
Josef Bacik7b128762008-07-24 12:17:14 -04003183 /* if we have links, this was a truncate, lets do that */
3184 if (inode->i_nlink) {
Josef Bacika41ad392011-01-31 15:30:16 -05003185 if (!S_ISREG(inode->i_mode)) {
3186 WARN_ON(1);
3187 iput(inode);
3188 continue;
3189 }
Josef Bacik7b128762008-07-24 12:17:14 -04003190 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003191
3192 /* 1 for the orphan item deletion. */
3193 trans = btrfs_start_transaction(root, 1);
3194 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003195 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003196 ret = PTR_ERR(trans);
3197 goto out;
3198 }
3199 ret = btrfs_orphan_add(trans, inode);
3200 btrfs_end_transaction(trans, root);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003201 if (ret) {
3202 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003203 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003204 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003205
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003206 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003207 if (ret)
3208 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003209 } else {
3210 nr_unlink++;
3211 }
3212
3213 /* this will do delete_inode and everything for us */
3214 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003215 if (ret)
3216 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003217 }
Miao Xie3254c872011-11-10 20:45:05 -05003218 /* release the path since we're done with it */
3219 btrfs_release_path(path);
3220
Yan, Zhengd68fc572010-05-16 10:49:58 -04003221 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3222
3223 if (root->orphan_block_rsv)
3224 btrfs_block_rsv_release(root, root->orphan_block_rsv,
3225 (u64)-1);
3226
3227 if (root->orphan_block_rsv || root->orphan_item_inserted) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003228 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003229 if (!IS_ERR(trans))
3230 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003231 }
Josef Bacik7b128762008-07-24 12:17:14 -04003232
3233 if (nr_unlink)
David Sterba4884b472013-03-20 13:31:27 +00003234 btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003235 if (nr_truncate)
David Sterba4884b472013-03-20 13:31:27 +00003236 btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003237
3238out:
3239 if (ret)
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003240 btrfs_crit(root->fs_info,
3241 "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003242 btrfs_free_path(path);
3243 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003244}
3245
Chris Masond352ac62008-09-29 15:18:18 -04003246/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003247 * very simple check to peek ahead in the leaf looking for xattrs. If we
3248 * don't find any xattrs, we know there can't be any acls.
3249 *
3250 * slot is the slot the inode is in, objectid is the objectid of the inode
3251 */
3252static noinline int acls_after_inode_item(struct extent_buffer *leaf,
3253 int slot, u64 objectid)
3254{
3255 u32 nritems = btrfs_header_nritems(leaf);
3256 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003257 static u64 xattr_access = 0;
3258 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003259 int scanned = 0;
3260
Josef Bacikf23b5a52013-06-19 10:16:26 -04003261 if (!xattr_access) {
3262 xattr_access = btrfs_name_hash(POSIX_ACL_XATTR_ACCESS,
3263 strlen(POSIX_ACL_XATTR_ACCESS));
3264 xattr_default = btrfs_name_hash(POSIX_ACL_XATTR_DEFAULT,
3265 strlen(POSIX_ACL_XATTR_DEFAULT));
3266 }
3267
Chris Mason46a53cc2009-04-27 11:47:50 -04003268 slot++;
3269 while (slot < nritems) {
3270 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3271
3272 /* we found a different objectid, there must not be acls */
3273 if (found_key.objectid != objectid)
3274 return 0;
3275
3276 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003277 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
3278 if (found_key.offset == xattr_access ||
3279 found_key.offset == xattr_default)
3280 return 1;
3281 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003282
3283 /*
3284 * we found a key greater than an xattr key, there can't
3285 * be any acls later on
3286 */
3287 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3288 return 0;
3289
3290 slot++;
3291 scanned++;
3292
3293 /*
3294 * it goes inode, inode backrefs, xattrs, extents,
3295 * so if there are a ton of hard links to an inode there can
3296 * be a lot of backrefs. Don't waste time searching too hard,
3297 * this is just an optimization
3298 */
3299 if (scanned >= 8)
3300 break;
3301 }
3302 /* we hit the end of the leaf before we found an xattr or
3303 * something larger than an xattr. We have to assume the inode
3304 * has acls
3305 */
3306 return 1;
3307}
3308
3309/*
Chris Masond352ac62008-09-29 15:18:18 -04003310 * read an inode from the btree into the in-memory inode
3311 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003312static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003313{
3314 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003315 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003316 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04003317 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04003318 struct btrfs_root *root = BTRFS_I(inode)->root;
3319 struct btrfs_key location;
Chris Mason46a53cc2009-04-27 11:47:50 -04003320 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003321 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003322 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003323 bool filled = false;
3324
3325 ret = btrfs_fill_inode(inode, &rdev);
3326 if (!ret)
3327 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003328
3329 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07003330 if (!path)
3331 goto make_bad;
3332
Josef Bacikd90c7322011-05-17 09:50:54 -04003333 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003334 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003335
Chris Mason39279cc2007-06-12 06:35:45 -04003336 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003337 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04003338 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04003339
Chris Mason5f39d392007-10-15 16:14:19 -04003340 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003341
3342 if (filled)
3343 goto cache_acl;
3344
Chris Mason5f39d392007-10-15 16:14:19 -04003345 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3346 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003347 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003348 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003349 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3350 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04003351 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003352
3353 tspec = btrfs_inode_atime(inode_item);
3354 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3355 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3356
3357 tspec = btrfs_inode_mtime(inode_item);
3358 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3359 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3360
3361 tspec = btrfs_inode_ctime(inode_item);
3362 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3363 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3364
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003365 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003366 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003367 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3368
3369 /*
3370 * If we were modified in the current generation and evicted from memory
3371 * and then re-read we need to do a full sync since we don't have any
3372 * idea about which extents were modified before we were evicted from
3373 * cache.
3374 */
3375 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
3376 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3377 &BTRFS_I(inode)->runtime_flags);
3378
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003379 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003380 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003381 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003382 rdev = btrfs_inode_rdev(leaf, inode_item);
3383
Josef Bacikaec74772008-07-24 12:12:38 -04003384 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003385 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie2f7e33d2011-06-23 07:27:13 +00003386cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003387 /*
3388 * try to precache a NULL acl entry for files that don't have
3389 * any xattrs or acls
3390 */
Li Zefan33345d012011-04-20 10:31:50 +08003391 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
3392 btrfs_ino(inode));
Al Viro72c04902009-06-24 16:58:48 -04003393 if (!maybe_acls)
3394 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003395
Chris Mason39279cc2007-06-12 06:35:45 -04003396 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003397
Chris Mason39279cc2007-06-12 06:35:45 -04003398 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003399 case S_IFREG:
3400 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003401 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05003402 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003403 inode->i_fop = &btrfs_file_operations;
3404 inode->i_op = &btrfs_file_inode_operations;
3405 break;
3406 case S_IFDIR:
3407 inode->i_fop = &btrfs_dir_file_operations;
3408 if (root == root->fs_info->tree_root)
3409 inode->i_op = &btrfs_dir_ro_inode_operations;
3410 else
3411 inode->i_op = &btrfs_dir_inode_operations;
3412 break;
3413 case S_IFLNK:
3414 inode->i_op = &btrfs_symlink_inode_operations;
3415 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04003416 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003417 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003418 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003419 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003420 init_special_inode(inode, inode->i_mode, rdev);
3421 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003422 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003423
3424 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003425 return;
3426
3427make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003428 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003429 make_bad_inode(inode);
3430}
3431
Chris Masond352ac62008-09-29 15:18:18 -04003432/*
3433 * given a leaf and an inode, copy the inode fields into the leaf
3434 */
Chris Masone02119d2008-09-05 16:13:11 -04003435static void fill_inode_item(struct btrfs_trans_handle *trans,
3436 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003437 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003438 struct inode *inode)
3439{
Liu Bo51fab692012-12-27 09:01:21 +00003440 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003441
Liu Bo51fab692012-12-27 09:01:21 +00003442 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003443
Liu Bo51fab692012-12-27 09:01:21 +00003444 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3445 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3446 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3447 &token);
3448 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3449 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003450
Liu Bo51fab692012-12-27 09:01:21 +00003451 btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item),
3452 inode->i_atime.tv_sec, &token);
3453 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item),
3454 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003455
Liu Bo51fab692012-12-27 09:01:21 +00003456 btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item),
3457 inode->i_mtime.tv_sec, &token);
3458 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item),
3459 inode->i_mtime.tv_nsec, &token);
3460
3461 btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item),
3462 inode->i_ctime.tv_sec, &token);
3463 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item),
3464 inode->i_ctime.tv_nsec, &token);
3465
3466 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3467 &token);
3468 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3469 &token);
3470 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3471 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3472 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3473 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3474 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003475}
3476
Chris Masond352ac62008-09-29 15:18:18 -04003477/*
3478 * copy everything in the in-memory inode into the btree.
3479 */
Chris Mason21151332011-11-10 20:39:08 -05003480static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003481 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003482{
3483 struct btrfs_inode_item *inode_item;
3484 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003485 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003486 int ret;
3487
3488 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003489 if (!path)
3490 return -ENOMEM;
3491
Chris Masonb9473432009-03-13 11:00:37 -04003492 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003493 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3494 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003495 if (ret) {
3496 if (ret > 0)
3497 ret = -ENOENT;
3498 goto failed;
3499 }
3500
Chris Masonb4ce94d2009-02-04 09:25:08 -05003501 btrfs_unlock_up_safe(path, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003502 leaf = path->nodes[0];
3503 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003504 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003505
Chris Masone02119d2008-09-05 16:13:11 -04003506 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003507 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003508 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003509 ret = 0;
3510failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003511 btrfs_free_path(path);
3512 return ret;
3513}
3514
Chris Masond352ac62008-09-29 15:18:18 -04003515/*
Chris Mason21151332011-11-10 20:39:08 -05003516 * copy everything in the in-memory inode into the btree.
3517 */
3518noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3519 struct btrfs_root *root, struct inode *inode)
3520{
3521 int ret;
3522
3523 /*
3524 * If the inode is a free space inode, we can deadlock during commit
3525 * if we put it into the delayed code.
3526 *
3527 * The data relocation inode should also be directly updated
3528 * without delay
3529 */
Liu Bo83eea1f2012-07-10 05:28:39 -06003530 if (!btrfs_is_free_space_inode(inode)
Chris Mason21151332011-11-10 20:39:08 -05003531 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003532 btrfs_update_root_times(trans, root);
3533
Chris Mason21151332011-11-10 20:39:08 -05003534 ret = btrfs_delayed_update_inode(trans, root, inode);
3535 if (!ret)
3536 btrfs_set_inode_last_trans(trans, inode);
3537 return ret;
3538 }
3539
3540 return btrfs_update_inode_item(trans, root, inode);
3541}
3542
Josef Bacikbe6aef62012-10-22 15:43:12 -04003543noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3544 struct btrfs_root *root,
3545 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003546{
3547 int ret;
3548
3549 ret = btrfs_update_inode(trans, root, inode);
3550 if (ret == -ENOSPC)
3551 return btrfs_update_inode_item(trans, root, inode);
3552 return ret;
3553}
3554
3555/*
Chris Masond352ac62008-09-29 15:18:18 -04003556 * unlink helper that gets used here in inode.c and in the tree logging
3557 * recovery code. It remove a link in a directory with a given name, and
3558 * also drops the back refs in the inode to the directory
3559 */
Al Viro92986792011-03-04 17:14:37 +00003560static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3561 struct btrfs_root *root,
3562 struct inode *dir, struct inode *inode,
3563 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003564{
3565 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003566 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003567 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003568 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003569 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003570 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003571 u64 ino = btrfs_ino(inode);
3572 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003573
3574 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003575 if (!path) {
3576 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003577 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003578 }
3579
Chris Masonb9473432009-03-13 11:00:37 -04003580 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003581 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003582 name, name_len, -1);
3583 if (IS_ERR(di)) {
3584 ret = PTR_ERR(di);
3585 goto err;
3586 }
3587 if (!di) {
3588 ret = -ENOENT;
3589 goto err;
3590 }
Chris Mason5f39d392007-10-15 16:14:19 -04003591 leaf = path->nodes[0];
3592 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003593 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003594 if (ret)
3595 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003596 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003597
Li Zefan33345d012011-04-20 10:31:50 +08003598 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3599 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003600 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003601 btrfs_info(root->fs_info,
3602 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003603 name_len, name, ino, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003604 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003605 goto err;
3606 }
3607
Miao Xie16cdcec2011-04-22 18:12:22 +08003608 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003609 if (ret) {
3610 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003611 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003612 }
Chris Mason39279cc2007-06-12 06:35:45 -04003613
Chris Masone02119d2008-09-05 16:13:11 -04003614 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08003615 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003616 if (ret != 0 && ret != -ENOENT) {
3617 btrfs_abort_transaction(trans, root, ret);
3618 goto err;
3619 }
Chris Masone02119d2008-09-05 16:13:11 -04003620
3621 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
3622 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04003623 if (ret == -ENOENT)
3624 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003625 else if (ret)
3626 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003627err:
3628 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003629 if (ret)
3630 goto out;
3631
3632 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003633 inode_inc_iversion(inode);
3634 inode_inc_iversion(dir);
Chris Masone02119d2008-09-05 16:13:11 -04003635 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Tsutomu Itohb9959292012-06-25 21:25:22 -06003636 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04003637out:
Chris Mason39279cc2007-06-12 06:35:45 -04003638 return ret;
3639}
3640
Al Viro92986792011-03-04 17:14:37 +00003641int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3642 struct btrfs_root *root,
3643 struct inode *dir, struct inode *inode,
3644 const char *name, int name_len)
3645{
3646 int ret;
3647 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3648 if (!ret) {
Zach Brown8b558c52013-10-16 12:10:34 -07003649 drop_nlink(inode);
Al Viro92986792011-03-04 17:14:37 +00003650 ret = btrfs_update_inode(trans, root, inode);
3651 }
3652 return ret;
3653}
Yan, Zhenga22285a2010-05-16 10:48:46 -04003654
3655/*
3656 * helper to start transaction for unlink and rmdir.
3657 *
Josef Bacikd52be812013-05-29 14:54:47 -04003658 * unlink and rmdir are special in btrfs, they do not always free space, so
3659 * if we cannot make our reservations the normal way try and see if there is
3660 * plenty of slack room in the global reserve to migrate, otherwise we cannot
3661 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04003662 */
Josef Bacikd52be812013-05-29 14:54:47 -04003663static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003664{
3665 struct btrfs_trans_handle *trans;
3666 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003667 int ret;
3668
Josef Bacike70bea52011-10-11 14:18:24 -04003669 /*
3670 * 1 for the possible orphan item
3671 * 1 for the dir item
3672 * 1 for the dir index
3673 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04003674 * 1 for the inode
3675 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04003676 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003677 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
3678 return trans;
3679
Josef Bacikd52be812013-05-29 14:54:47 -04003680 if (PTR_ERR(trans) == -ENOSPC) {
3681 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003682
Josef Bacikd52be812013-05-29 14:54:47 -04003683 trans = btrfs_start_transaction(root, 0);
3684 if (IS_ERR(trans))
3685 return trans;
3686 ret = btrfs_cond_migrate_bytes(root->fs_info,
3687 &root->fs_info->trans_block_rsv,
3688 num_bytes, 5);
3689 if (ret) {
3690 btrfs_end_transaction(trans, root);
3691 return ERR_PTR(ret);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003692 }
Josef Bacik5a77d762011-11-01 14:32:23 -04003693 trans->block_rsv = &root->fs_info->trans_block_rsv;
Josef Bacikd52be812013-05-29 14:54:47 -04003694 trans->bytes_reserved = num_bytes;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003695 }
Josef Bacikd52be812013-05-29 14:54:47 -04003696 return trans;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003697}
3698
Chris Mason39279cc2007-06-12 06:35:45 -04003699static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3700{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003701 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003702 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04003703 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003704 int ret;
3705
Josef Bacikd52be812013-05-29 14:54:47 -04003706 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003707 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003708 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003709
Chris Mason12fcfd22009-03-24 10:24:20 -04003710 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
3711
Chris Masone02119d2008-09-05 16:13:11 -04003712 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3713 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003714 if (ret)
3715 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003716
Yan, Zhenga22285a2010-05-16 10:48:46 -04003717 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04003718 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003719 if (ret)
3720 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003721 }
Josef Bacik7b128762008-07-24 12:17:14 -04003722
Tsutomu Itohb5324022011-07-19 07:27:20 +00003723out:
Josef Bacikd52be812013-05-29 14:54:47 -04003724 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003725 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04003726 return ret;
3727}
3728
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003729int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3730 struct btrfs_root *root,
3731 struct inode *dir, u64 objectid,
3732 const char *name, int name_len)
3733{
3734 struct btrfs_path *path;
3735 struct extent_buffer *leaf;
3736 struct btrfs_dir_item *di;
3737 struct btrfs_key key;
3738 u64 index;
3739 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003740 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003741
3742 path = btrfs_alloc_path();
3743 if (!path)
3744 return -ENOMEM;
3745
Li Zefan33345d012011-04-20 10:31:50 +08003746 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003747 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003748 if (IS_ERR_OR_NULL(di)) {
3749 if (!di)
3750 ret = -ENOENT;
3751 else
3752 ret = PTR_ERR(di);
3753 goto out;
3754 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003755
3756 leaf = path->nodes[0];
3757 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3758 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3759 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003760 if (ret) {
3761 btrfs_abort_transaction(trans, root, ret);
3762 goto out;
3763 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003764 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003765
3766 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3767 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003768 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003769 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003770 if (ret != -ENOENT) {
3771 btrfs_abort_transaction(trans, root, ret);
3772 goto out;
3773 }
Li Zefan33345d012011-04-20 10:31:50 +08003774 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003775 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003776 if (IS_ERR_OR_NULL(di)) {
3777 if (!di)
3778 ret = -ENOENT;
3779 else
3780 ret = PTR_ERR(di);
3781 btrfs_abort_transaction(trans, root, ret);
3782 goto out;
3783 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003784
3785 leaf = path->nodes[0];
3786 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003787 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003788 index = key.offset;
3789 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003790 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003791
Miao Xie16cdcec2011-04-22 18:12:22 +08003792 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003793 if (ret) {
3794 btrfs_abort_transaction(trans, root, ret);
3795 goto out;
3796 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003797
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003798 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003799 inode_inc_iversion(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003800 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Josef Bacik5a24e842012-08-08 10:12:59 -06003801 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003802 if (ret)
3803 btrfs_abort_transaction(trans, root, ret);
3804out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04003805 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003806 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003807}
3808
Chris Mason39279cc2007-06-12 06:35:45 -04003809static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3810{
3811 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003812 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003813 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003814 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04003815
David Sterbab3ae2442012-09-13 16:04:34 -06003816 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04003817 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06003818 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
3819 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04003820
Josef Bacikd52be812013-05-29 14:54:47 -04003821 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003822 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003823 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003824
Li Zefan33345d012011-04-20 10:31:50 +08003825 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003826 err = btrfs_unlink_subvol(trans, root, dir,
3827 BTRFS_I(inode)->location.objectid,
3828 dentry->d_name.name,
3829 dentry->d_name.len);
3830 goto out;
3831 }
3832
Josef Bacik7b128762008-07-24 12:17:14 -04003833 err = btrfs_orphan_add(trans, inode);
3834 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003835 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003836
Chris Mason39279cc2007-06-12 06:35:45 -04003837 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04003838 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3839 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05003840 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04003841 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003842out:
Josef Bacikd52be812013-05-29 14:54:47 -04003843 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003844 btrfs_btree_balance_dirty(root);
Chris Mason39544012007-12-12 14:38:19 -05003845
Chris Mason39279cc2007-06-12 06:35:45 -04003846 return err;
3847}
3848
Chris Mason323ac952008-10-01 19:05:46 -04003849/*
Chris Mason39279cc2007-06-12 06:35:45 -04003850 * this can truncate away extent items, csum items and directory items.
3851 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04003852 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04003853 *
3854 * csum items that cross the new i_size are truncated to the new size
3855 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04003856 *
3857 * min_type is the minimum key type to truncate down to. If set to 0, this
3858 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04003859 */
Yan, Zheng80825102009-11-12 09:35:36 +00003860int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
3861 struct btrfs_root *root,
3862 struct inode *inode,
3863 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003864{
Chris Mason39279cc2007-06-12 06:35:45 -04003865 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003866 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003867 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00003868 struct btrfs_key key;
3869 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04003870 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04003871 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003872 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003873 u64 item_end = 0;
Josef Bacik7f4f6e02013-08-29 16:43:28 -04003874 u64 last_size = (u64)-1;
Yan, Zheng80825102009-11-12 09:35:36 +00003875 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04003876 int found_extent;
3877 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003878 int pending_del_nr = 0;
3879 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04003880 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00003881 int ret;
3882 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08003883 u64 ino = btrfs_ino(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003884
3885 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04003886
Mark Fasheh0eb0e192011-07-12 16:44:10 -07003887 path = btrfs_alloc_path();
3888 if (!path)
3889 return -ENOMEM;
3890 path->reada = -1;
3891
Josef Bacik5dc562c2012-08-17 13:14:17 -04003892 /*
3893 * We want to drop from the next block forward in case this new size is
3894 * not block aligned since we will be keeping the last block of the
3895 * extent just the way it is.
3896 */
Josef Bacik0af3d002010-06-21 14:48:16 -04003897 if (root->ref_cows || root == root->fs_info->tree_root)
Qu Wenruofda28322013-02-26 08:10:22 +00003898 btrfs_drop_extent_cache(inode, ALIGN(new_size,
3899 root->sectorsize), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003900
Miao Xie16cdcec2011-04-22 18:12:22 +08003901 /*
3902 * This function is also used to drop the items in the log tree before
3903 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
3904 * it is used to drop the loged items. So we shouldn't kill the delayed
3905 * items.
3906 */
3907 if (min_type == 0 && root == BTRFS_I(inode)->root)
3908 btrfs_kill_delayed_inode_items(inode);
3909
Li Zefan33345d012011-04-20 10:31:50 +08003910 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04003911 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04003912 key.type = (u8)-1;
3913
Chris Mason85e21ba2008-01-29 15:11:36 -05003914search_again:
Chris Masonb9473432009-03-13 11:00:37 -04003915 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05003916 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00003917 if (ret < 0) {
3918 err = ret;
3919 goto out;
3920 }
Chris Masond3977122009-01-05 21:25:51 -05003921
Chris Mason85e21ba2008-01-29 15:11:36 -05003922 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003923 /* there are no items in the tree for us to truncate, we're
3924 * done
3925 */
Yan, Zheng80825102009-11-12 09:35:36 +00003926 if (path->slots[0] == 0)
3927 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05003928 path->slots[0]--;
3929 }
3930
Chris Masond3977122009-01-05 21:25:51 -05003931 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04003932 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04003933 leaf = path->nodes[0];
3934 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3935 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04003936
Li Zefan33345d012011-04-20 10:31:50 +08003937 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04003938 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003939
Chris Mason85e21ba2008-01-29 15:11:36 -05003940 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003941 break;
3942
Chris Mason5f39d392007-10-15 16:14:19 -04003943 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04003944 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04003945 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04003946 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04003947 extent_type = btrfs_file_extent_type(leaf, fi);
3948 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04003949 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04003950 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04003951 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04003952 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04003953 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003954 }
Yan008630c2007-11-07 13:31:09 -05003955 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04003956 }
Yan, Zheng80825102009-11-12 09:35:36 +00003957 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04003958 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00003959 } else {
3960 if (item_end < new_size)
3961 break;
3962 if (found_key.offset >= new_size)
3963 del_item = 1;
3964 else
3965 del_item = 0;
3966 }
Chris Mason39279cc2007-06-12 06:35:45 -04003967 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003968 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04003969 if (found_type != BTRFS_EXTENT_DATA_KEY)
3970 goto delete;
3971
Josef Bacik7f4f6e02013-08-29 16:43:28 -04003972 if (del_item)
3973 last_size = found_key.offset;
3974 else
3975 last_size = new_size;
3976
Chris Mason179e29e2007-11-01 11:28:41 -04003977 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04003978 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04003979 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05003980 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04003981 u64 orig_num_bytes =
3982 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00003983 extent_num_bytes = ALIGN(new_size -
3984 found_key.offset,
3985 root->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04003986 btrfs_set_file_extent_num_bytes(leaf, fi,
3987 extent_num_bytes);
3988 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05003989 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04003990 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003991 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04003992 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003993 } else {
Chris Masondb945352007-10-15 16:15:53 -04003994 extent_num_bytes =
3995 btrfs_file_extent_disk_num_bytes(leaf,
3996 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003997 extent_offset = found_key.offset -
3998 btrfs_file_extent_offset(leaf, fi);
3999
Chris Mason39279cc2007-06-12 06:35:45 -04004000 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004001 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004002 if (extent_start != 0) {
4003 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04004004 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004005 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004006 }
4007 }
Chris Mason90692182008-02-08 13:49:28 -05004008 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004009 /*
4010 * we can't truncate inline items that have had
4011 * special encodings
4012 */
4013 if (!del_item &&
4014 btrfs_file_extent_compression(leaf, fi) == 0 &&
4015 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4016 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004017 u32 size = new_size - found_key.offset;
4018
4019 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004020 inode_sub_bytes(inode, item_end + 1 -
4021 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04004022 }
4023 size =
4024 btrfs_file_extent_calc_inline_size(size);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004025 btrfs_truncate_item(root, path, size, 1);
Chris Masone02119d2008-09-05 16:13:11 -04004026 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004027 inode_sub_bytes(inode, item_end + 1 -
4028 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05004029 }
Chris Mason39279cc2007-06-12 06:35:45 -04004030 }
Chris Mason179e29e2007-11-01 11:28:41 -04004031delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004032 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004033 if (!pending_del_nr) {
4034 /* no pending yet, add ourselves */
4035 pending_del_slot = path->slots[0];
4036 pending_del_nr = 1;
4037 } else if (pending_del_nr &&
4038 path->slots[0] + 1 == pending_del_slot) {
4039 /* hop on the pending chunk */
4040 pending_del_nr++;
4041 pending_del_slot = path->slots[0];
4042 } else {
Chris Masond3977122009-01-05 21:25:51 -05004043 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004044 }
Chris Mason39279cc2007-06-12 06:35:45 -04004045 } else {
4046 break;
4047 }
Josef Bacik0af3d002010-06-21 14:48:16 -04004048 if (found_extent && (root->ref_cows ||
4049 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004050 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004051 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004052 extent_num_bytes, 0,
4053 btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02004054 ino, extent_offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004055 BUG_ON(ret);
4056 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004057
Yan, Zheng80825102009-11-12 09:35:36 +00004058 if (found_type == BTRFS_INODE_ITEM_KEY)
4059 break;
4060
4061 if (path->slots[0] == 0 ||
4062 path->slots[0] != pending_del_slot) {
Yan, Zheng80825102009-11-12 09:35:36 +00004063 if (pending_del_nr) {
4064 ret = btrfs_del_items(trans, root, path,
4065 pending_del_slot,
4066 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004067 if (ret) {
4068 btrfs_abort_transaction(trans,
4069 root, ret);
4070 goto error;
4071 }
Yan, Zheng80825102009-11-12 09:35:36 +00004072 pending_del_nr = 0;
4073 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004074 btrfs_release_path(path);
Chris Mason85e21ba2008-01-29 15:11:36 -05004075 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004076 } else {
4077 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004078 }
Chris Mason39279cc2007-06-12 06:35:45 -04004079 }
Yan, Zheng80825102009-11-12 09:35:36 +00004080out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004081 if (pending_del_nr) {
4082 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4083 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004084 if (ret)
4085 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004086 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004087error:
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004088 if (last_size != (u64)-1)
4089 btrfs_ordered_update_i_size(inode, last_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004090 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00004091 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004092}
4093
Chris Masona52d9a82007-08-27 16:49:44 -04004094/*
Josef Bacik2aaa6652012-08-29 14:27:18 -04004095 * btrfs_truncate_page - read, zero a chunk and write a page
4096 * @inode - inode that we're zeroing
4097 * @from - the offset to start zeroing
4098 * @len - the length to zero, 0 to zero the entire range respective to the
4099 * offset
4100 * @front - zero up to the offset instead of from the offset on
4101 *
4102 * This will find the page for the "from" offset and cow the page and zero the
4103 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004104 */
Josef Bacik2aaa6652012-08-29 14:27:18 -04004105int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
4106 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004107{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004108 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04004109 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004110 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4111 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004112 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004113 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04004114 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04004115 pgoff_t index = from >> PAGE_CACHE_SHIFT;
4116 unsigned offset = from & (PAGE_CACHE_SIZE-1);
4117 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004118 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004119 int ret = 0;
4120 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004121 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004122
Josef Bacik2aaa6652012-08-29 14:27:18 -04004123 if ((offset & (blocksize - 1)) == 0 &&
4124 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004125 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004126 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004127 if (ret)
4128 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004129
Chris Mason211c17f2008-05-15 09:13:45 -04004130again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004131 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004132 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004133 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Miao Xieac6a2b32012-12-05 10:56:13 +00004134 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004135 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004136 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004137
4138 page_start = page_offset(page);
4139 page_end = page_start + PAGE_CACHE_SIZE - 1;
4140
Chris Masona52d9a82007-08-27 16:49:44 -04004141 if (!PageUptodate(page)) {
4142 ret = btrfs_readpage(NULL, page);
4143 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004144 if (page->mapping != mapping) {
4145 unlock_page(page);
4146 page_cache_release(page);
4147 goto again;
4148 }
Chris Masona52d9a82007-08-27 16:49:44 -04004149 if (!PageUptodate(page)) {
4150 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004151 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004152 }
4153 }
Chris Mason211c17f2008-05-15 09:13:45 -04004154 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004155
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004156 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004157 set_page_extent_mapped(page);
4158
4159 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4160 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004161 unlock_extent_cached(io_tree, page_start, page_end,
4162 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004163 unlock_page(page);
4164 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004165 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004166 btrfs_put_ordered_extent(ordered);
4167 goto again;
4168 }
4169
Josef Bacik2ac55d42010-02-03 19:33:23 +00004170 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004171 EXTENT_DIRTY | EXTENT_DELALLOC |
4172 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004173 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004174
Josef Bacik2ac55d42010-02-03 19:33:23 +00004175 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
4176 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004177 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004178 unlock_extent_cached(io_tree, page_start, page_end,
4179 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004180 goto out_unlock;
4181 }
4182
Chris Masone6dcd2d2008-07-17 12:53:50 -04004183 if (offset != PAGE_CACHE_SIZE) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004184 if (!len)
4185 len = PAGE_CACHE_SIZE - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004186 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004187 if (front)
4188 memset(kaddr, 0, offset);
4189 else
4190 memset(kaddr + offset, 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004191 flush_dcache_page(page);
4192 kunmap(page);
4193 }
Chris Mason247e7432008-07-17 12:53:51 -04004194 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004195 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004196 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
4197 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004198
Chris Mason89642222008-07-24 09:41:53 -04004199out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004200 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004201 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04004202 unlock_page(page);
4203 page_cache_release(page);
4204out:
4205 return ret;
4206}
4207
Josef Bacik695a0d02011-03-04 15:46:53 -05004208/*
4209 * This function puts in dummy file extents for the area we're creating a hole
4210 * for. So if we are truncating this file to a larger size we need to insert
4211 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4212 * the range between oldsize and size
4213 */
Josef Bacika41ad392011-01-31 15:30:16 -05004214int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004215{
4216 struct btrfs_trans_handle *trans;
4217 struct btrfs_root *root = BTRFS_I(inode)->root;
4218 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004219 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004220 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004221 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Qu Wenruofda28322013-02-26 08:10:22 +00004222 u64 hole_start = ALIGN(oldsize, root->sectorsize);
4223 u64 block_end = ALIGN(size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004224 u64 last_byte;
4225 u64 cur_offset;
4226 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004227 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004228
Josef Bacika71754f2013-06-17 17:14:39 -04004229 /*
4230 * If our size started in the middle of a page we need to zero out the
4231 * rest of the page before we expand the i_size, otherwise we could
4232 * expose stale data.
4233 */
4234 err = btrfs_truncate_page(inode, oldsize, 0, 0);
4235 if (err)
4236 return err;
4237
Yan Zheng9036c102008-10-30 14:19:41 -04004238 if (size <= hole_start)
4239 return 0;
4240
Yan Zheng9036c102008-10-30 14:19:41 -04004241 while (1) {
4242 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004243
Josef Bacik2ac55d42010-02-03 19:33:23 +00004244 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004245 &cached_state);
Miao Xiefa7c1492013-09-26 13:15:27 +08004246 ordered = btrfs_lookup_ordered_range(inode, hole_start,
4247 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004248 if (!ordered)
4249 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004250 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4251 &cached_state, GFP_NOFS);
Miao Xiefa7c1492013-09-26 13:15:27 +08004252 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004253 btrfs_put_ordered_extent(ordered);
4254 }
4255
Yan Zheng9036c102008-10-30 14:19:41 -04004256 cur_offset = hole_start;
4257 while (1) {
4258 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4259 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004260 if (IS_ERR(em)) {
4261 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004262 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004263 break;
4264 }
Yan Zheng9036c102008-10-30 14:19:41 -04004265 last_byte = min(extent_map_end(em), block_end);
Qu Wenruofda28322013-02-26 08:10:22 +00004266 last_byte = ALIGN(last_byte , root->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004267 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004268 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004269 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004270
Miao Xie36423202011-12-14 20:12:02 -05004271 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004272 if (IS_ERR(trans)) {
4273 err = PTR_ERR(trans);
Chris Mason771ed682008-11-06 22:02:51 -05004274 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004275 }
Yan, Zheng80825102009-11-12 09:35:36 +00004276
Josef Bacik5dc562c2012-08-17 13:14:17 -04004277 err = btrfs_drop_extents(trans, root, inode,
4278 cur_offset,
Josef Bacik26714852012-08-29 12:24:27 -04004279 cur_offset + hole_size, 1);
Miao Xie5b397372011-09-11 10:52:24 -04004280 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004281 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04004282 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05004283 break;
Miao Xie5b397372011-09-11 10:52:24 -04004284 }
Josef Bacik9ed74f22009-09-11 16:12:44 -04004285
Yan Zheng9036c102008-10-30 14:19:41 -04004286 err = btrfs_insert_file_extent(trans, root,
Li Zefan33345d012011-04-20 10:31:50 +08004287 btrfs_ino(inode), cur_offset, 0,
Yan Zheng9036c102008-10-30 14:19:41 -04004288 0, hole_size, 0, hole_size,
4289 0, 0, 0);
Miao Xie5b397372011-09-11 10:52:24 -04004290 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004291 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04004292 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05004293 break;
Miao Xie5b397372011-09-11 10:52:24 -04004294 }
Yan, Zheng80825102009-11-12 09:35:36 +00004295
Josef Bacik5dc562c2012-08-17 13:14:17 -04004296 btrfs_drop_extent_cache(inode, cur_offset,
4297 cur_offset + hole_size - 1, 0);
4298 hole_em = alloc_extent_map();
4299 if (!hole_em) {
4300 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4301 &BTRFS_I(inode)->runtime_flags);
4302 goto next;
4303 }
4304 hole_em->start = cur_offset;
4305 hole_em->len = hole_size;
4306 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004307
Josef Bacik5dc562c2012-08-17 13:14:17 -04004308 hole_em->block_start = EXTENT_MAP_HOLE;
4309 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004310 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004311 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004312 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
4313 hole_em->compress_type = BTRFS_COMPRESS_NONE;
4314 hole_em->generation = trans->transid;
4315
4316 while (1) {
4317 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004318 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004319 write_unlock(&em_tree->lock);
4320 if (err != -EEXIST)
4321 break;
4322 btrfs_drop_extent_cache(inode, cur_offset,
4323 cur_offset +
4324 hole_size - 1, 0);
4325 }
4326 free_extent_map(hole_em);
4327next:
Miao Xie36423202011-12-14 20:12:02 -05004328 btrfs_update_inode(trans, root, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004329 btrfs_end_transaction(trans, root);
Yan Zheng9036c102008-10-30 14:19:41 -04004330 }
4331 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004332 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004333 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004334 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004335 break;
4336 }
4337
Yan, Zhenga22285a2010-05-16 10:48:46 -04004338 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004339 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4340 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004341 return err;
4342}
4343
Eric Sandeen3972f262013-01-12 02:57:22 +00004344static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004345{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004346 struct btrfs_root *root = BTRFS_I(inode)->root;
4347 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004348 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004349 loff_t newsize = attr->ia_size;
4350 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004351 int ret;
4352
Eric Sandeen3972f262013-01-12 02:57:22 +00004353 /*
4354 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4355 * special case where we need to update the times despite not having
4356 * these flags set. For all other operations the VFS set these flags
4357 * explicitly if it wants a timestamp update.
4358 */
4359 if (newsize != oldsize && (!(mask & (ATTR_CTIME | ATTR_MTIME))))
4360 inode->i_ctime = inode->i_mtime = current_fs_time(inode->i_sb);
4361
Josef Bacika41ad392011-01-31 15:30:16 -05004362 if (newsize > oldsize) {
Kirill A. Shutemov7caef262013-09-12 15:13:56 -07004363 truncate_pagecache(inode, newsize);
Josef Bacika41ad392011-01-31 15:30:16 -05004364 ret = btrfs_cont_expand(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004365 if (ret)
Yan, Zheng80825102009-11-12 09:35:36 +00004366 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004367
Miao Xief4a2f4c2011-12-14 20:12:01 -05004368 trans = btrfs_start_transaction(root, 1);
4369 if (IS_ERR(trans))
4370 return PTR_ERR(trans);
4371
4372 i_size_write(inode, newsize);
4373 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
4374 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004375 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05004376 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004377
Josef Bacika41ad392011-01-31 15:30:16 -05004378 /*
4379 * We're truncating a file that used to have good data down to
4380 * zero. Make sure it gets into the ordered flush list so that
4381 * any new writes get down to disk quickly.
4382 */
4383 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004384 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4385 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004386
Josef Bacikf3fe8202013-01-07 17:03:21 -05004387 /*
4388 * 1 for the orphan item we're going to add
4389 * 1 for the orphan item deletion.
4390 */
4391 trans = btrfs_start_transaction(root, 2);
4392 if (IS_ERR(trans))
4393 return PTR_ERR(trans);
4394
4395 /*
4396 * We need to do this in case we fail at _any_ point during the
4397 * actual truncate. Once we do the truncate_setsize we could
4398 * invalidate pages which forces any outstanding ordered io to
4399 * be instantly completed which will give us extents that need
4400 * to be truncated. If we fail to get an orphan inode down we
4401 * could have left over extents that were never meant to live,
4402 * so we need to garuntee from this point on that everything
4403 * will be consistent.
4404 */
4405 ret = btrfs_orphan_add(trans, inode);
4406 btrfs_end_transaction(trans, root);
4407 if (ret)
4408 return ret;
4409
Josef Bacika41ad392011-01-31 15:30:16 -05004410 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4411 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004412
4413 /* Disable nonlocked read DIO to avoid the end less truncate */
4414 btrfs_inode_block_unlocked_dio(inode);
4415 inode_dio_wait(inode);
4416 btrfs_inode_resume_unlocked_dio(inode);
4417
Josef Bacika41ad392011-01-31 15:30:16 -05004418 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004419 if (ret && inode->i_nlink) {
4420 int err;
4421
4422 /*
4423 * failed to truncate, disk_i_size is only adjusted down
4424 * as we remove extents, so it should represent the true
4425 * size of the inode, so reset the in memory size and
4426 * delete our orphan entry.
4427 */
4428 trans = btrfs_join_transaction(root);
4429 if (IS_ERR(trans)) {
4430 btrfs_orphan_del(NULL, inode);
4431 return ret;
4432 }
4433 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
4434 err = btrfs_orphan_del(trans, inode);
4435 if (err)
4436 btrfs_abort_transaction(trans, root, err);
4437 btrfs_end_transaction(trans, root);
4438 }
Yan, Zheng80825102009-11-12 09:35:36 +00004439 }
4440
Josef Bacika41ad392011-01-31 15:30:16 -05004441 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004442}
4443
Chris Mason39279cc2007-06-12 06:35:45 -04004444static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
4445{
4446 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08004447 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004448 int err;
4449
Li Zefanb83cc962010-12-20 16:04:08 +08004450 if (btrfs_root_readonly(root))
4451 return -EROFS;
4452
Chris Mason39279cc2007-06-12 06:35:45 -04004453 err = inode_change_ok(inode, attr);
4454 if (err)
4455 return err;
4456
Chris Mason5a3f23d2009-03-31 13:27:11 -04004457 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00004458 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00004459 if (err)
4460 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004461 }
Yan Zheng9036c102008-10-30 14:19:41 -04004462
Christoph Hellwig10257742010-06-04 11:30:02 +02004463 if (attr->ia_valid) {
4464 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004465 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004466 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04004467
Josef Bacik22c44fe2011-11-30 10:45:38 -05004468 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig10257742010-06-04 11:30:02 +02004469 err = btrfs_acl_chmod(inode);
4470 }
4471
Chris Mason39279cc2007-06-12 06:35:45 -04004472 return err;
4473}
Chris Mason61295eb2008-01-14 16:24:38 -05004474
Al Virobd555972010-06-07 11:35:40 -04004475void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004476{
4477 struct btrfs_trans_handle *trans;
4478 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04004479 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik07127182011-08-19 10:29:59 -04004480 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04004481 int ret;
4482
liubo1abe9b82011-03-24 11:18:59 +00004483 trace_btrfs_inode_evict(inode);
4484
Chris Mason39279cc2007-06-12 06:35:45 -04004485 truncate_inode_pages(&inode->i_data, 0);
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02004486 if (inode->i_nlink &&
4487 ((btrfs_root_refs(&root->root_item) != 0 &&
4488 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
4489 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04004490 goto no_delete;
4491
Chris Mason39279cc2007-06-12 06:35:45 -04004492 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04004493 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004494 goto no_delete;
4495 }
Al Virobd555972010-06-07 11:35:40 -04004496 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04004497 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04004498
Yan, Zhengc71bf092009-11-12 09:34:40 +00004499 if (root->fs_info->log_root_recovering) {
Liu Bo6bf02312012-06-25 21:59:09 -06004500 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04004501 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00004502 goto no_delete;
4503 }
4504
Yan, Zheng76dda932009-09-21 16:00:26 -04004505 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02004506 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
4507 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04004508 goto no_delete;
4509 }
4510
Miao Xie0e8c36a2012-12-19 06:59:51 +00004511 ret = btrfs_commit_inode_delayed_inode(inode);
4512 if (ret) {
4513 btrfs_orphan_del(NULL, inode);
4514 goto no_delete;
4515 }
4516
Miao Xie66d8f3d2012-09-06 04:02:28 -06004517 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04004518 if (!rsv) {
4519 btrfs_orphan_del(NULL, inode);
4520 goto no_delete;
4521 }
Josef Bacik4a338542011-08-29 11:01:31 -04004522 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04004523 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04004524 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04004525
Chris Masondbe674a2008-07-17 12:54:05 -04004526 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04004527
Josef Bacik4289a662011-08-05 13:22:24 -04004528 /*
Miao Xie8407aa42012-09-07 01:43:32 -06004529 * This is a bit simpler than btrfs_truncate since we've already
4530 * reserved our space for our orphan item in the unlink, so we just
4531 * need to reserve some slack space in case we add bytes and update
4532 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04004533 */
Yan, Zheng80825102009-11-12 09:35:36 +00004534 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00004535 ret = btrfs_block_rsv_refill(root, rsv, min_size,
4536 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00004537
Josef Bacik726c35f2011-09-26 15:46:06 -04004538 /*
4539 * Try and steal from the global reserve since we will
4540 * likely not use this space anyway, we want to try as
4541 * hard as possible to get this to work.
4542 */
4543 if (ret)
4544 ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
4545
Yan, Zhengd68fc572010-05-16 10:49:58 -04004546 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004547 btrfs_warn(root->fs_info,
4548 "Could not get space for a delete, will truncate on mount %d",
4549 ret);
Josef Bacik4289a662011-08-05 13:22:24 -04004550 btrfs_orphan_del(NULL, inode);
4551 btrfs_free_block_rsv(root, rsv);
4552 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04004553 }
4554
Miao Xie0e8c36a2012-12-19 06:59:51 +00004555 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04004556 if (IS_ERR(trans)) {
4557 btrfs_orphan_del(NULL, inode);
4558 btrfs_free_block_rsv(root, rsv);
4559 goto no_delete;
4560 }
4561
4562 trans->block_rsv = rsv;
4563
Yan, Zhengd68fc572010-05-16 10:49:58 -04004564 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04004565 if (ret != -ENOSPC)
Yan, Zheng80825102009-11-12 09:35:36 +00004566 break;
4567
Miao Xie8407aa42012-09-07 01:43:32 -06004568 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004569 btrfs_end_transaction(trans, root);
4570 trans = NULL;
Liu Bob53d3f52012-11-14 14:34:34 +00004571 btrfs_btree_balance_dirty(root);
Josef Bacik7b128762008-07-24 12:17:14 -04004572 }
4573
Josef Bacik4289a662011-08-05 13:22:24 -04004574 btrfs_free_block_rsv(root, rsv);
4575
Josef Bacik4ef31a42013-08-13 14:10:08 -04004576 /*
4577 * Errors here aren't a big deal, it just means we leave orphan items
4578 * in the tree. They will be cleaned up on the next mount.
4579 */
Yan, Zheng80825102009-11-12 09:35:36 +00004580 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04004581 trans->block_rsv = root->orphan_block_rsv;
Josef Bacik4ef31a42013-08-13 14:10:08 -04004582 btrfs_orphan_del(trans, inode);
4583 } else {
4584 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004585 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004586
Josef Bacik4289a662011-08-05 13:22:24 -04004587 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08004588 if (!(root == root->fs_info->tree_root ||
4589 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08004590 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08004591
Chris Mason54aa1f42007-06-22 14:16:25 -04004592 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004593 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04004594no_delete:
Miao Xie89042e52013-05-15 07:48:15 +00004595 btrfs_remove_delayed_node(inode);
Jan Karadbd57682012-05-03 14:48:02 +02004596 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004597 return;
Chris Mason39279cc2007-06-12 06:35:45 -04004598}
4599
4600/*
4601 * this returns the key found in the dir entry in the location pointer.
4602 * If no dir entries were found, location->objectid is 0.
4603 */
4604static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
4605 struct btrfs_key *location)
4606{
4607 const char *name = dentry->d_name.name;
4608 int namelen = dentry->d_name.len;
4609 struct btrfs_dir_item *di;
4610 struct btrfs_path *path;
4611 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04004612 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004613
4614 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07004615 if (!path)
4616 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05004617
Li Zefan33345d012011-04-20 10:31:50 +08004618 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04004619 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04004620 if (IS_ERR(di))
4621 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05004622
David Sterbac7040052011-04-19 18:00:01 +02004623 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05004624 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05004625
Chris Mason5f39d392007-10-15 16:14:19 -04004626 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04004627out:
Chris Mason39279cc2007-06-12 06:35:45 -04004628 btrfs_free_path(path);
4629 return ret;
Chris Mason39544012007-12-12 14:38:19 -05004630out_err:
4631 location->objectid = 0;
4632 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04004633}
4634
4635/*
4636 * when we hit a tree root in a directory, the btrfs part of the inode
4637 * needs to be changed to reflect the root directory of the tree root. This
4638 * is kind of like crossing a mount point.
4639 */
4640static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004641 struct inode *dir,
4642 struct dentry *dentry,
4643 struct btrfs_key *location,
4644 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04004645{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004646 struct btrfs_path *path;
4647 struct btrfs_root *new_root;
4648 struct btrfs_root_ref *ref;
4649 struct extent_buffer *leaf;
4650 int ret;
4651 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004652
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004653 path = btrfs_alloc_path();
4654 if (!path) {
4655 err = -ENOMEM;
4656 goto out;
4657 }
Chris Mason39279cc2007-06-12 06:35:45 -04004658
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004659 err = -ENOENT;
4660 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
4661 BTRFS_I(dir)->root->root_key.objectid,
4662 location->objectid);
4663 if (ret) {
4664 if (ret < 0)
4665 err = ret;
4666 goto out;
4667 }
Chris Mason39279cc2007-06-12 06:35:45 -04004668
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004669 leaf = path->nodes[0];
4670 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08004671 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004672 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
4673 goto out;
4674
4675 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
4676 (unsigned long)(ref + 1),
4677 dentry->d_name.len);
4678 if (ret)
4679 goto out;
4680
David Sterbab3b4aa72011-04-21 01:20:15 +02004681 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004682
4683 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
4684 if (IS_ERR(new_root)) {
4685 err = PTR_ERR(new_root);
4686 goto out;
4687 }
4688
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004689 *sub_root = new_root;
4690 location->objectid = btrfs_root_dirid(&new_root->root_item);
4691 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04004692 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004693 err = 0;
4694out:
4695 btrfs_free_path(path);
4696 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004697}
4698
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004699static void inode_tree_add(struct inode *inode)
4700{
4701 struct btrfs_root *root = BTRFS_I(inode)->root;
4702 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004703 struct rb_node **p;
4704 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01004705 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Li Zefan33345d012011-04-20 10:31:50 +08004706 u64 ino = btrfs_ino(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004707
Al Viro1d3382cb2010-10-23 15:19:20 -04004708 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004709 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00004710 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004711 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00004712 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004713 while (*p) {
4714 parent = *p;
4715 entry = rb_entry(parent, struct btrfs_inode, rb_node);
4716
Li Zefan33345d012011-04-20 10:31:50 +08004717 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004718 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004719 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004720 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004721 else {
4722 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04004723 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01004724 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004725 RB_CLEAR_NODE(parent);
4726 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01004727 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004728 }
4729 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01004730 rb_link_node(new, parent, p);
4731 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004732 spin_unlock(&root->inode_lock);
4733}
4734
4735static void inode_tree_del(struct inode *inode)
4736{
4737 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04004738 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004739
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004740 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004741 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004742 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004743 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04004744 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004745 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004746 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04004747
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02004748 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004749 synchronize_srcu(&root->fs_info->subvol_srcu);
4750 spin_lock(&root->inode_lock);
4751 empty = RB_EMPTY_ROOT(&root->inode_tree);
4752 spin_unlock(&root->inode_lock);
4753 if (empty)
4754 btrfs_add_dead_root(root);
4755 }
4756}
4757
Jeff Mahoney143bede2012-03-01 14:56:26 +01004758void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04004759{
4760 struct rb_node *node;
4761 struct rb_node *prev;
4762 struct btrfs_inode *entry;
4763 struct inode *inode;
4764 u64 objectid = 0;
4765
4766 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4767
4768 spin_lock(&root->inode_lock);
4769again:
4770 node = root->inode_tree.rb_node;
4771 prev = NULL;
4772 while (node) {
4773 prev = node;
4774 entry = rb_entry(node, struct btrfs_inode, rb_node);
4775
Li Zefan33345d012011-04-20 10:31:50 +08004776 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004777 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004778 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004779 node = node->rb_right;
4780 else
4781 break;
4782 }
4783 if (!node) {
4784 while (prev) {
4785 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004786 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004787 node = prev;
4788 break;
4789 }
4790 prev = rb_next(prev);
4791 }
4792 }
4793 while (node) {
4794 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004795 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04004796 inode = igrab(&entry->vfs_inode);
4797 if (inode) {
4798 spin_unlock(&root->inode_lock);
4799 if (atomic_read(&inode->i_count) > 1)
4800 d_prune_aliases(inode);
4801 /*
Al Viro45321ac2010-06-07 13:43:19 -04004802 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04004803 * the inode cache when its usage count
4804 * hits zero.
4805 */
4806 iput(inode);
4807 cond_resched();
4808 spin_lock(&root->inode_lock);
4809 goto again;
4810 }
4811
4812 if (cond_resched_lock(&root->inode_lock))
4813 goto again;
4814
4815 node = rb_next(node);
4816 }
4817 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004818}
4819
Chris Masone02119d2008-09-05 16:13:11 -04004820static int btrfs_init_locked_inode(struct inode *inode, void *p)
4821{
4822 struct btrfs_iget_args *args = p;
4823 inode->i_ino = args->ino;
Chris Masone02119d2008-09-05 16:13:11 -04004824 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004825 return 0;
4826}
4827
4828static int btrfs_find_actor(struct inode *inode, void *opaque)
4829{
4830 struct btrfs_iget_args *args = opaque;
Li Zefan33345d012011-04-20 10:31:50 +08004831 return args->ino == btrfs_ino(inode) &&
Chris Masond3977122009-01-05 21:25:51 -05004832 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004833}
4834
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004835static struct inode *btrfs_iget_locked(struct super_block *s,
4836 u64 objectid,
4837 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04004838{
4839 struct inode *inode;
4840 struct btrfs_iget_args args;
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01004841 unsigned long hashval = btrfs_inode_hash(objectid, root);
4842
Chris Mason39279cc2007-06-12 06:35:45 -04004843 args.ino = objectid;
4844 args.root = root;
4845
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01004846 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04004847 btrfs_init_locked_inode,
4848 (void *)&args);
4849 return inode;
4850}
4851
Balaji Rao1a54ef82008-07-21 02:01:04 +05304852/* Get an inode object given its location and corresponding root.
4853 * Returns in *is_new if the inode was read from disk
4854 */
4855struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00004856 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05304857{
4858 struct inode *inode;
4859
4860 inode = btrfs_iget_locked(s, location->objectid, root);
4861 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004862 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05304863
4864 if (inode->i_state & I_NEW) {
4865 BTRFS_I(inode)->root = root;
4866 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
4867 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07004868 if (!is_bad_inode(inode)) {
4869 inode_tree_add(inode);
4870 unlock_new_inode(inode);
4871 if (new)
4872 *new = 1;
4873 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04004874 unlock_new_inode(inode);
4875 iput(inode);
4876 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07004877 }
4878 }
4879
Balaji Rao1a54ef82008-07-21 02:01:04 +05304880 return inode;
4881}
4882
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004883static struct inode *new_simple_dir(struct super_block *s,
4884 struct btrfs_key *key,
4885 struct btrfs_root *root)
4886{
4887 struct inode *inode = new_inode(s);
4888
4889 if (!inode)
4890 return ERR_PTR(-ENOMEM);
4891
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004892 BTRFS_I(inode)->root = root;
4893 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04004894 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004895
4896 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08004897 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004898 inode->i_fop = &simple_dir_operations;
4899 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
4900 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
4901
4902 return inode;
4903}
4904
Chris Mason3de45862008-11-17 21:02:50 -05004905struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04004906{
Chris Masond3977122009-01-05 21:25:51 -05004907 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004908 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004909 struct btrfs_root *sub_root = root;
4910 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04004911 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004912 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004913
4914 if (dentry->d_name.len > BTRFS_NAME_LEN)
4915 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04004916
Jeff Layton39e3c952012-11-28 11:30:53 -05004917 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04004918 if (ret < 0)
4919 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04004920
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004921 if (location.objectid == 0)
4922 return NULL;
4923
4924 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00004925 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004926 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004927 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004928
4929 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
4930
Yan, Zheng76dda932009-09-21 16:00:26 -04004931 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004932 ret = fixup_tree_root_location(root, dir, dentry,
4933 &location, &sub_root);
4934 if (ret < 0) {
4935 if (ret != -ENOENT)
4936 inode = ERR_PTR(ret);
4937 else
4938 inode = new_simple_dir(dir->i_sb, &location, sub_root);
4939 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00004940 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004941 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004942 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
4943
Julia Lawall34d19ba2011-01-24 19:55:19 +00004944 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00004945 down_read(&root->fs_info->cleanup_work_sem);
4946 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004947 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00004948 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04004949 if (ret) {
4950 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004951 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04004952 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00004953 }
4954
Chris Mason3de45862008-11-17 21:02:50 -05004955 return inode;
4956}
4957
Nick Pigginfe15ce42011-01-07 17:49:23 +11004958static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04004959{
4960 struct btrfs_root *root;
Li Zefan848cce02012-02-21 17:04:28 +08004961 struct inode *inode = dentry->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04004962
Li Zefan848cce02012-02-21 17:04:28 +08004963 if (!inode && !IS_ROOT(dentry))
4964 inode = dentry->d_parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04004965
Li Zefan848cce02012-02-21 17:04:28 +08004966 if (inode) {
4967 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04004968 if (btrfs_root_refs(&root->root_item) == 0)
4969 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08004970
4971 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
4972 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04004973 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004974 return 0;
4975}
4976
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004977static void btrfs_dentry_release(struct dentry *dentry)
4978{
4979 if (dentry->d_fsdata)
4980 kfree(dentry->d_fsdata);
4981}
4982
Chris Mason3de45862008-11-17 21:02:50 -05004983static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04004984 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05004985{
Josef Bacika66e7cc2011-09-18 10:34:03 -04004986 struct dentry *ret;
4987
4988 ret = d_splice_alias(btrfs_lookup_dentry(dir, dentry), dentry);
Josef Bacika66e7cc2011-09-18 10:34:03 -04004989 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004990}
4991
Miao Xie16cdcec2011-04-22 18:12:22 +08004992unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04004993 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
4994};
4995
Al Viro9cdda8d2013-05-22 16:48:09 -04004996static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04004997{
Al Viro9cdda8d2013-05-22 16:48:09 -04004998 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04004999 struct btrfs_root *root = BTRFS_I(inode)->root;
5000 struct btrfs_item *item;
5001 struct btrfs_dir_item *di;
5002 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005003 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005004 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005005 struct list_head ins_list;
5006 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005007 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005008 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005009 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005010 unsigned char d_type;
5011 int over = 0;
5012 u32 di_cur;
5013 u32 di_total;
5014 u32 di_len;
5015 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005016 char tmp_name[32];
5017 char *name_ptr;
5018 int name_len;
Al Viro9cdda8d2013-05-22 16:48:09 -04005019 int is_curr = 0; /* ctx->pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04005020
5021 /* FIXME, use a real flag for deciding about the key type */
5022 if (root->fs_info->tree_root == root)
5023 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005024
Al Viro9cdda8d2013-05-22 16:48:09 -04005025 if (!dir_emit_dots(file, ctx))
5026 return 0;
5027
David Woodhouse49593bf2008-08-17 17:08:36 +01005028 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005029 if (!path)
5030 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005031
Josef Bacik026fd312011-05-13 10:32:11 -04005032 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005033
Miao Xie16cdcec2011-04-22 18:12:22 +08005034 if (key_type == BTRFS_DIR_INDEX_KEY) {
5035 INIT_LIST_HEAD(&ins_list);
5036 INIT_LIST_HEAD(&del_list);
5037 btrfs_get_delayed_items(inode, &ins_list, &del_list);
5038 }
5039
Chris Mason39279cc2007-06-12 06:35:45 -04005040 btrfs_set_key_type(&key, key_type);
Al Viro9cdda8d2013-05-22 16:48:09 -04005041 key.offset = ctx->pos;
Li Zefan33345d012011-04-20 10:31:50 +08005042 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005043
Chris Mason39279cc2007-06-12 06:35:45 -04005044 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5045 if (ret < 0)
5046 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005047
5048 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005049 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005050 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005051 if (slot >= btrfs_header_nritems(leaf)) {
5052 ret = btrfs_next_leaf(root, path);
5053 if (ret < 0)
5054 goto err;
5055 else if (ret > 0)
5056 break;
5057 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005058 }
Chris Mason3de45862008-11-17 21:02:50 -05005059
Ross Kirkdd3cc162013-09-16 15:58:09 +01005060 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04005061 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5062
5063 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005064 break;
Chris Mason5f39d392007-10-15 16:14:19 -04005065 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04005066 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005067 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005068 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08005069 if (key_type == BTRFS_DIR_INDEX_KEY &&
5070 btrfs_should_delete_dir_index(&del_list,
5071 found_key.offset))
5072 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005073
Al Viro9cdda8d2013-05-22 16:48:09 -04005074 ctx->pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08005075 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005076
Chris Mason39279cc2007-06-12 06:35:45 -04005077 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
5078 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005079 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01005080
5081 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04005082 struct btrfs_key location;
5083
Josef Bacik22a94d42011-03-16 16:47:17 -04005084 if (verify_dir_item(root, leaf, di))
5085 break;
5086
Chris Mason5f39d392007-10-15 16:14:19 -04005087 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01005088 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04005089 name_ptr = tmp_name;
5090 } else {
5091 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01005092 if (!name_ptr) {
5093 ret = -ENOMEM;
5094 goto err;
5095 }
Chris Mason5f39d392007-10-15 16:14:19 -04005096 }
5097 read_extent_buffer(leaf, name_ptr,
5098 (unsigned long)(di + 1), name_len);
5099
5100 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5101 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05005102
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005103
Chris Mason3de45862008-11-17 21:02:50 -05005104 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01005105 * skip it.
5106 *
5107 * In contrast to old kernels, we insert the snapshot's
5108 * dir item and dir index after it has been created, so
5109 * we won't find a reference to our own snapshot. We
5110 * still keep the following code for backward
5111 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05005112 */
5113 if (location.type == BTRFS_ROOT_ITEM_KEY &&
5114 location.objectid == root->root_key.objectid) {
5115 over = 0;
5116 goto skip;
5117 }
Al Viro9cdda8d2013-05-22 16:48:09 -04005118 over = !dir_emit(ctx, name_ptr, name_len,
5119 location.objectid, d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04005120
Chris Mason3de45862008-11-17 21:02:50 -05005121skip:
Chris Mason5f39d392007-10-15 16:14:19 -04005122 if (name_ptr != tmp_name)
5123 kfree(name_ptr);
5124
Chris Mason39279cc2007-06-12 06:35:45 -04005125 if (over)
5126 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05005127 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01005128 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04005129 di_cur += di_len;
5130 di = (struct btrfs_dir_item *)((char *)di + di_len);
5131 }
Li Zefanb9e03af2011-03-23 10:43:58 +08005132next:
5133 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005134 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005135
Miao Xie16cdcec2011-04-22 18:12:22 +08005136 if (key_type == BTRFS_DIR_INDEX_KEY) {
5137 if (is_curr)
Al Viro9cdda8d2013-05-22 16:48:09 -04005138 ctx->pos++;
5139 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005140 if (ret)
5141 goto nopos;
5142 }
5143
David Woodhouse49593bf2008-08-17 17:08:36 +01005144 /* Reached end of directory/root. Bump pos past the last item. */
Zach Browndb62efb2013-07-11 16:19:42 -07005145 ctx->pos++;
5146
5147 /*
5148 * Stop new entries from being returned after we return the last
5149 * entry.
5150 *
5151 * New directory entries are assigned a strictly increasing
5152 * offset. This means that new entries created during readdir
5153 * are *guaranteed* to be seen in the future by that readdir.
5154 * This has broken buggy programs which operate on names as
5155 * they're returned by readdir. Until we re-use freed offsets
5156 * we have this hack to stop new entries from being returned
5157 * under the assumption that they'll never reach this huge
5158 * offset.
5159 *
5160 * This is being careful not to overflow 32bit loff_t unless the
5161 * last entry requires it because doing so has broken 32bit apps
5162 * in the past.
5163 */
5164 if (key_type == BTRFS_DIR_INDEX_KEY) {
5165 if (ctx->pos >= INT_MAX)
5166 ctx->pos = LLONG_MAX;
5167 else
5168 ctx->pos = INT_MAX;
5169 }
Chris Mason39279cc2007-06-12 06:35:45 -04005170nopos:
5171 ret = 0;
5172err:
Miao Xie16cdcec2011-04-22 18:12:22 +08005173 if (key_type == BTRFS_DIR_INDEX_KEY)
5174 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005175 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005176 return ret;
5177}
5178
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005179int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005180{
5181 struct btrfs_root *root = BTRFS_I(inode)->root;
5182 struct btrfs_trans_handle *trans;
5183 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005184 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005185
Josef Bacik72ac3c02012-05-23 14:13:11 -04005186 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005187 return 0;
5188
Liu Bo83eea1f2012-07-10 05:28:39 -06005189 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08005190 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005191
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005192 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005193 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005194 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005195 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005196 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005197 if (IS_ERR(trans))
5198 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06005199 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005200 }
5201 return ret;
5202}
5203
5204/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005205 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005206 * inode changes. But, it is most likely to find the inode in cache.
5207 * FIXME, needs more benchmarking...there are no reasons other than performance
5208 * to keep or drop this code.
5209 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005210static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005211{
5212 struct btrfs_root *root = BTRFS_I(inode)->root;
5213 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005214 int ret;
5215
Josef Bacik72ac3c02012-05-23 14:13:11 -04005216 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005217 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005218
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005219 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005220 if (IS_ERR(trans))
5221 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005222
5223 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005224 if (ret && ret == -ENOSPC) {
5225 /* whoops, lets try again with the full transaction */
5226 btrfs_end_transaction(trans, root);
5227 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005228 if (IS_ERR(trans))
5229 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005230
Chris Mason94b60442010-05-26 11:02:00 -04005231 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005232 }
Chris Mason39279cc2007-06-12 06:35:45 -04005233 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08005234 if (BTRFS_I(inode)->delayed_node)
5235 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005236
5237 return ret;
5238}
5239
5240/*
5241 * This is a copy of file_update_time. We need this so we can return error on
5242 * ENOSPC for updating the inode in the case of file write and mmap writes.
5243 */
Josef Bacike41f9412012-03-26 09:46:47 -04005244static int btrfs_update_time(struct inode *inode, struct timespec *now,
5245 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005246{
Alexander Block2bc5565282012-06-15 09:49:33 +02005247 struct btrfs_root *root = BTRFS_I(inode)->root;
5248
5249 if (btrfs_root_readonly(root))
5250 return -EROFS;
5251
Josef Bacike41f9412012-03-26 09:46:47 -04005252 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005253 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04005254 if (flags & S_CTIME)
5255 inode->i_ctime = *now;
5256 if (flags & S_MTIME)
5257 inode->i_mtime = *now;
5258 if (flags & S_ATIME)
5259 inode->i_atime = *now;
5260 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005261}
5262
Chris Masond352ac62008-09-29 15:18:18 -04005263/*
5264 * find the highest existing sequence number in a directory
5265 * and then set the in-memory index_cnt variable to reflect
5266 * free sequence numbers
5267 */
Josef Bacikaec74772008-07-24 12:12:38 -04005268static int btrfs_set_inode_index_count(struct inode *inode)
5269{
5270 struct btrfs_root *root = BTRFS_I(inode)->root;
5271 struct btrfs_key key, found_key;
5272 struct btrfs_path *path;
5273 struct extent_buffer *leaf;
5274 int ret;
5275
Li Zefan33345d012011-04-20 10:31:50 +08005276 key.objectid = btrfs_ino(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005277 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
5278 key.offset = (u64)-1;
5279
5280 path = btrfs_alloc_path();
5281 if (!path)
5282 return -ENOMEM;
5283
5284 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5285 if (ret < 0)
5286 goto out;
5287 /* FIXME: we should be able to handle this */
5288 if (ret == 0)
5289 goto out;
5290 ret = 0;
5291
5292 /*
5293 * MAGIC NUMBER EXPLANATION:
5294 * since we search a directory based on f_pos we have to start at 2
5295 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5296 * else has to start at 2
5297 */
5298 if (path->slots[0] == 0) {
5299 BTRFS_I(inode)->index_cnt = 2;
5300 goto out;
5301 }
5302
5303 path->slots[0]--;
5304
5305 leaf = path->nodes[0];
5306 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5307
Li Zefan33345d012011-04-20 10:31:50 +08005308 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacikaec74772008-07-24 12:12:38 -04005309 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
5310 BTRFS_I(inode)->index_cnt = 2;
5311 goto out;
5312 }
5313
5314 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
5315out:
5316 btrfs_free_path(path);
5317 return ret;
5318}
5319
Chris Masond352ac62008-09-29 15:18:18 -04005320/*
5321 * helper to find a free sequence number in a given directory. This current
5322 * code is very simple, later versions will do smarter things in the btree
5323 */
Chris Mason3de45862008-11-17 21:02:50 -05005324int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04005325{
5326 int ret = 0;
5327
5328 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08005329 ret = btrfs_inode_delayed_dir_index_count(dir);
5330 if (ret) {
5331 ret = btrfs_set_inode_index_count(dir);
5332 if (ret)
5333 return ret;
5334 }
Josef Bacikaec74772008-07-24 12:12:38 -04005335 }
5336
Chris Mason00e4e6b2008-08-05 11:18:09 -04005337 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04005338 BTRFS_I(dir)->index_cnt++;
5339
5340 return ret;
5341}
5342
Chris Mason39279cc2007-06-12 06:35:45 -04005343static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5344 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04005345 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05005346 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04005347 u64 ref_objectid, u64 objectid,
5348 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04005349{
5350 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005351 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04005352 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04005353 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05005354 struct btrfs_inode_ref *ref;
5355 struct btrfs_key key[2];
5356 u32 sizes[2];
5357 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04005358 int ret;
5359 int owner;
5360
Chris Mason5f39d392007-10-15 16:14:19 -04005361 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005362 if (!path)
5363 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04005364
Chris Mason39279cc2007-06-12 06:35:45 -04005365 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005366 if (!inode) {
5367 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005368 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005369 }
Chris Mason39279cc2007-06-12 06:35:45 -04005370
Li Zefan581bb052011-04-20 10:06:11 +08005371 /*
5372 * we have to initialize this early, so we can reclaim the inode
5373 * number if we fail afterwards in this function.
5374 */
5375 inode->i_ino = objectid;
5376
Josef Bacikaec74772008-07-24 12:12:38 -04005377 if (dir) {
liubo1abe9b82011-03-24 11:18:59 +00005378 trace_btrfs_inode_request(dir);
5379
Chris Mason3de45862008-11-17 21:02:50 -05005380 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04005381 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005382 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005383 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005384 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04005385 }
Josef Bacikaec74772008-07-24 12:12:38 -04005386 }
5387 /*
5388 * index_cnt is ignored for everything but a dir,
5389 * btrfs_get_inode_index_count has an explanation for the magic
5390 * number
5391 */
5392 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04005393 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04005394 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00005395 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04005396
Josef Bacik5dc562c2012-08-17 13:14:17 -04005397 /*
5398 * We could have gotten an inode number from somebody who was fsynced
5399 * and then removed in this same transaction, so let's just set full
5400 * sync since it will be a full sync anyway and this will blow away the
5401 * old info in the log.
5402 */
5403 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
5404
Al Viro569254b2011-07-24 17:08:40 -04005405 if (S_ISDIR(mode))
Chris Mason39279cc2007-06-12 06:35:45 -04005406 owner = 0;
5407 else
5408 owner = 1;
Chris Mason9c583092008-01-29 15:15:18 -05005409
5410 key[0].objectid = objectid;
5411 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
5412 key[0].offset = 0;
5413
Mark Fashehf1863732012-08-08 11:32:27 -07005414 /*
5415 * Start new inodes with an inode_ref. This is slightly more
5416 * efficient for small numbers of hard links since they will
5417 * be packed into one item. Extended refs will kick in if we
5418 * add more hard links than can fit in the ref item.
5419 */
Chris Mason9c583092008-01-29 15:15:18 -05005420 key[1].objectid = objectid;
5421 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
5422 key[1].offset = ref_objectid;
5423
5424 sizes[0] = sizeof(struct btrfs_inode_item);
5425 sizes[1] = name_len + sizeof(*ref);
5426
Chris Masonb9473432009-03-13 11:00:37 -04005427 path->leave_spinning = 1;
Chris Mason9c583092008-01-29 15:15:18 -05005428 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
5429 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04005430 goto fail;
5431
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03005432 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04005433 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005434 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04005435 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
5436 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06005437 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
5438 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04005439 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05005440
5441 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
5442 struct btrfs_inode_ref);
5443 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04005444 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05005445 ptr = (unsigned long)(ref + 1);
5446 write_extent_buffer(path->nodes[0], name, ptr, name_len);
5447
Chris Mason5f39d392007-10-15 16:14:19 -04005448 btrfs_mark_buffer_dirty(path->nodes[0]);
5449 btrfs_free_path(path);
5450
Chris Mason39279cc2007-06-12 06:35:45 -04005451 location = &BTRFS_I(inode)->location;
5452 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04005453 location->offset = 0;
5454 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
5455
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005456 btrfs_inherit_iflags(inode, dir);
5457
Al Viro569254b2011-07-24 17:08:40 -04005458 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04005459 if (btrfs_test_opt(root, NODATASUM))
5460 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo213490b2012-09-11 08:33:50 -06005461 if (btrfs_test_opt(root, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05005462 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
5463 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04005464 }
5465
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005466 btrfs_insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005467 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00005468
5469 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04005470 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00005471
Alexander Block8ea05e32012-07-25 17:35:53 +02005472 btrfs_update_root_times(trans, root);
5473
Chris Mason39279cc2007-06-12 06:35:45 -04005474 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005475fail:
Josef Bacikaec74772008-07-24 12:12:38 -04005476 if (dir)
5477 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04005478 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005479 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005480 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005481}
5482
5483static inline u8 btrfs_inode_type(struct inode *inode)
5484{
5485 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
5486}
5487
Chris Masond352ac62008-09-29 15:18:18 -04005488/*
5489 * utility function to add 'inode' into 'parent_inode' with
5490 * a give name and a given sequence number.
5491 * if 'add_backref' is true, also insert a backref from the
5492 * inode to the parent directory.
5493 */
Chris Masone02119d2008-09-05 16:13:11 -04005494int btrfs_add_link(struct btrfs_trans_handle *trans,
5495 struct inode *parent_inode, struct inode *inode,
5496 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005497{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005498 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005499 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04005500 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08005501 u64 ino = btrfs_ino(inode);
5502 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005503
Li Zefan33345d012011-04-20 10:31:50 +08005504 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005505 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
5506 } else {
Li Zefan33345d012011-04-20 10:31:50 +08005507 key.objectid = ino;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005508 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
5509 key.offset = 0;
5510 }
Chris Mason39279cc2007-06-12 06:35:45 -04005511
Li Zefan33345d012011-04-20 10:31:50 +08005512 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005513 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
5514 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08005515 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005516 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08005517 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
5518 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005519 }
5520
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005521 /* Nothing to clean up yet */
5522 if (ret)
5523 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005524
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005525 ret = btrfs_insert_dir_item(trans, root, name, name_len,
5526 parent_inode, &key,
5527 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05005528 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005529 goto fail_dir_item;
5530 else if (ret) {
5531 btrfs_abort_transaction(trans, root, ret);
5532 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005533 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005534
5535 btrfs_i_size_write(parent_inode, parent_inode->i_size +
5536 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005537 inode_inc_iversion(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005538 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
5539 ret = btrfs_update_inode(trans, root, parent_inode);
5540 if (ret)
5541 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005542 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05005543
5544fail_dir_item:
5545 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
5546 u64 local_index;
5547 int err;
5548 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
5549 key.objectid, root->root_key.objectid,
5550 parent_ino, &local_index, name, name_len);
5551
5552 } else if (add_backref) {
5553 u64 local_index;
5554 int err;
5555
5556 err = btrfs_del_inode_ref(trans, root, name, name_len,
5557 ino, parent_ino, &local_index);
5558 }
5559 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005560}
5561
5562static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00005563 struct inode *dir, struct dentry *dentry,
5564 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005565{
Josef Bacika1b075d2010-11-19 20:36:11 +00005566 int err = btrfs_add_link(trans, dir, inode,
5567 dentry->d_name.name, dentry->d_name.len,
5568 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005569 if (err > 0)
5570 err = -EEXIST;
5571 return err;
5572}
5573
Josef Bacik618e21d2007-07-11 10:18:17 -04005574static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04005575 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04005576{
5577 struct btrfs_trans_handle *trans;
5578 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005579 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04005580 int err;
5581 int drop_inode = 0;
5582 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005583 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04005584
5585 if (!new_valid_dev(rdev))
5586 return -EINVAL;
5587
Josef Bacik9ed74f22009-09-11 16:12:44 -04005588 /*
5589 * 2 for inode item and ref
5590 * 2 for dir items
5591 * 1 for xattr if selinux is on
5592 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005593 trans = btrfs_start_transaction(root, 5);
5594 if (IS_ERR(trans))
5595 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05005596
Li Zefan581bb052011-04-20 10:06:11 +08005597 err = btrfs_find_free_ino(root, &objectid);
5598 if (err)
5599 goto out_unlock;
5600
Josef Bacikaec74772008-07-24 12:12:38 -04005601 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005602 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005603 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005604 if (IS_ERR(inode)) {
5605 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005606 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005607 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005608
Eric Paris2a7dba32011-02-01 11:05:39 -05005609 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005610 if (err) {
5611 drop_inode = 1;
5612 goto out_unlock;
5613 }
5614
Casey Schauflerad19db72011-12-15 10:09:07 -05005615 /*
5616 * If the active LSM wants to access the inode during
5617 * d_instantiate it needs these. Smack checks to see
5618 * if the filesystem supports xattrs by looking at the
5619 * ops vector.
5620 */
5621
5622 inode->i_op = &btrfs_special_inode_operations;
Josef Bacika1b075d2010-11-19 20:36:11 +00005623 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04005624 if (err)
5625 drop_inode = 1;
5626 else {
Josef Bacik618e21d2007-07-11 10:18:17 -04005627 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04005628 btrfs_update_inode(trans, root, inode);
Al Viro08c422c2011-12-23 07:58:13 -05005629 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005630 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005631out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005632 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00005633 btrfs_btree_balance_dirty(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04005634 if (drop_inode) {
5635 inode_dec_link_count(inode);
5636 iput(inode);
5637 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005638 return err;
5639}
5640
Chris Mason39279cc2007-06-12 06:35:45 -04005641static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04005642 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04005643{
5644 struct btrfs_trans_handle *trans;
5645 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005646 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005647 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04005648 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04005649 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005650 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005651
Josef Bacik9ed74f22009-09-11 16:12:44 -04005652 /*
5653 * 2 for inode item and ref
5654 * 2 for dir items
5655 * 1 for xattr if selinux is on
5656 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005657 trans = btrfs_start_transaction(root, 5);
5658 if (IS_ERR(trans))
5659 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005660
Li Zefan581bb052011-04-20 10:06:11 +08005661 err = btrfs_find_free_ino(root, &objectid);
5662 if (err)
5663 goto out_unlock;
5664
Josef Bacikaec74772008-07-24 12:12:38 -04005665 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005666 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005667 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005668 if (IS_ERR(inode)) {
5669 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005670 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005671 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005672 drop_inode_on_err = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04005673
Eric Paris2a7dba32011-02-01 11:05:39 -05005674 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005675 if (err)
Josef Bacik33268ea2008-07-24 12:16:36 -04005676 goto out_unlock;
Josef Bacik33268ea2008-07-24 12:16:36 -04005677
Filipe Brandenburger9185aa52012-11-30 03:40:08 +00005678 err = btrfs_update_inode(trans, root, inode);
5679 if (err)
5680 goto out_unlock;
5681
Casey Schauflerad19db72011-12-15 10:09:07 -05005682 /*
5683 * If the active LSM wants to access the inode during
5684 * d_instantiate it needs these. Smack checks to see
5685 * if the filesystem supports xattrs by looking at the
5686 * ops vector.
5687 */
5688 inode->i_fop = &btrfs_file_operations;
5689 inode->i_op = &btrfs_file_inode_operations;
5690
Josef Bacika1b075d2010-11-19 20:36:11 +00005691 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005692 if (err)
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005693 goto out_unlock;
5694
5695 inode->i_mapping->a_ops = &btrfs_aops;
5696 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
5697 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
5698 d_instantiate(dentry, inode);
5699
Chris Mason39279cc2007-06-12 06:35:45 -04005700out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005701 btrfs_end_transaction(trans, root);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005702 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005703 inode_dec_link_count(inode);
5704 iput(inode);
5705 }
Liu Bob53d3f52012-11-14 14:34:34 +00005706 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005707 return err;
5708}
5709
5710static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
5711 struct dentry *dentry)
5712{
5713 struct btrfs_trans_handle *trans;
5714 struct btrfs_root *root = BTRFS_I(dir)->root;
5715 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005716 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04005717 int err;
5718 int drop_inode = 0;
5719
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005720 /* do not allow sys_link's with other subvols of the same device */
5721 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00005722 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005723
Mark Fashehf1863732012-08-08 11:32:27 -07005724 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00005725 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04005726
Chris Mason3de45862008-11-17 21:02:50 -05005727 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04005728 if (err)
5729 goto fail;
5730
Yan, Zhenga22285a2010-05-16 10:48:46 -04005731 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00005732 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04005733 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00005734 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04005735 */
Miao Xie7e6b6462011-02-18 09:21:17 +00005736 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005737 if (IS_ERR(trans)) {
5738 err = PTR_ERR(trans);
5739 goto fail;
5740 }
Chris Mason5f39d392007-10-15 16:14:19 -04005741
Zach Brown8b558c52013-10-16 12:10:34 -07005742 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005743 inode_inc_iversion(inode);
Miao Xie31534952011-04-13 13:19:21 +08005744 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6ee2010-10-23 11:11:40 -04005745 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04005746 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04005747
Josef Bacika1b075d2010-11-19 20:36:11 +00005748 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04005749
Yan, Zhenga5719522009-09-24 09:17:31 -04005750 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005751 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04005752 } else {
Al Viro10d9f302011-07-16 23:09:10 -04005753 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04005754 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005755 if (err)
5756 goto fail;
Al Viro08c422c2011-12-23 07:58:13 -05005757 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00005758 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04005759 }
Chris Mason39279cc2007-06-12 06:35:45 -04005760
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005761 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05005762fail:
Chris Mason39279cc2007-06-12 06:35:45 -04005763 if (drop_inode) {
5764 inode_dec_link_count(inode);
5765 iput(inode);
5766 }
Liu Bob53d3f52012-11-14 14:34:34 +00005767 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005768 return err;
5769}
5770
Al Viro18bb1db2011-07-26 01:41:39 -04005771static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04005772{
Chris Masonb9d86662008-05-02 16:13:49 -04005773 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04005774 struct btrfs_trans_handle *trans;
5775 struct btrfs_root *root = BTRFS_I(dir)->root;
5776 int err = 0;
5777 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04005778 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005779 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005780
Josef Bacik9ed74f22009-09-11 16:12:44 -04005781 /*
5782 * 2 items for inode and ref
5783 * 2 items for dir items
5784 * 1 for xattr if selinux is on
5785 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005786 trans = btrfs_start_transaction(root, 5);
5787 if (IS_ERR(trans))
5788 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04005789
Li Zefan581bb052011-04-20 10:06:11 +08005790 err = btrfs_find_free_ino(root, &objectid);
5791 if (err)
5792 goto out_fail;
5793
Josef Bacikaec74772008-07-24 12:12:38 -04005794 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005795 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005796 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04005797 if (IS_ERR(inode)) {
5798 err = PTR_ERR(inode);
5799 goto out_fail;
5800 }
Chris Mason5f39d392007-10-15 16:14:19 -04005801
Chris Mason39279cc2007-06-12 06:35:45 -04005802 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04005803
Eric Paris2a7dba32011-02-01 11:05:39 -05005804 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005805 if (err)
5806 goto out_fail;
5807
Chris Mason39279cc2007-06-12 06:35:45 -04005808 inode->i_op = &btrfs_dir_inode_operations;
5809 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04005810
Chris Masondbe674a2008-07-17 12:54:05 -04005811 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005812 err = btrfs_update_inode(trans, root, inode);
5813 if (err)
5814 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005815
Josef Bacika1b075d2010-11-19 20:36:11 +00005816 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
5817 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005818 if (err)
5819 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005820
Chris Mason39279cc2007-06-12 06:35:45 -04005821 d_instantiate(dentry, inode);
5822 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005823
5824out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005825 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005826 if (drop_on_err)
5827 iput(inode);
Liu Bob53d3f52012-11-14 14:34:34 +00005828 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005829 return err;
5830}
5831
Chris Masond352ac62008-09-29 15:18:18 -04005832/* helper for btfs_get_extent. Given an existing extent in the tree,
5833 * and an extent that you want to insert, deal with overlap and insert
5834 * the new extent into the tree.
5835 */
Chris Mason3b951512008-04-17 11:29:12 -04005836static int merge_extent_mapping(struct extent_map_tree *em_tree,
5837 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005838 struct extent_map *em,
5839 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04005840{
5841 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04005842
Chris Masone6dcd2d2008-07-17 12:53:50 -04005843 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
5844 start_diff = map_start - em->start;
5845 em->start = map_start;
5846 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04005847 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
5848 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04005849 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04005850 em->block_len -= start_diff;
5851 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005852 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04005853}
5854
Chris Masonc8b97812008-10-29 14:49:59 -04005855static noinline int uncompress_inline(struct btrfs_path *path,
5856 struct inode *inode, struct page *page,
5857 size_t pg_offset, u64 extent_offset,
5858 struct btrfs_file_extent_item *item)
5859{
5860 int ret;
5861 struct extent_buffer *leaf = path->nodes[0];
5862 char *tmp;
5863 size_t max_size;
5864 unsigned long inline_size;
5865 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08005866 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04005867
5868 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08005869 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04005870 max_size = btrfs_file_extent_ram_bytes(leaf, item);
5871 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01005872 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04005873 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04005874 if (!tmp)
5875 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04005876 ptr = btrfs_file_extent_inline_start(item);
5877
5878 read_extent_buffer(leaf, tmp, ptr, inline_size);
5879
Chris Mason5b050f02008-11-11 09:34:41 -05005880 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08005881 ret = btrfs_decompress(compress_type, tmp, page,
5882 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04005883 if (ret) {
Cong Wang7ac687d2011-11-25 23:14:28 +08005884 char *kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04005885 unsigned long copy_size = min_t(u64,
5886 PAGE_CACHE_SIZE - pg_offset,
5887 max_size - extent_offset);
5888 memset(kaddr + pg_offset, 0, copy_size);
Cong Wang7ac687d2011-11-25 23:14:28 +08005889 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -04005890 }
5891 kfree(tmp);
5892 return 0;
5893}
5894
Chris Masond352ac62008-09-29 15:18:18 -04005895/*
5896 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05005897 * the ugly parts come from merging extents from the disk with the in-ram
5898 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04005899 * where the in-ram extents might be locked pending data=ordered completion.
5900 *
5901 * This also copies inline extents directly into the page.
5902 */
Chris Masond3977122009-01-05 21:25:51 -05005903
Chris Masona52d9a82007-08-27 16:49:44 -04005904struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05005905 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04005906 int create)
5907{
5908 int ret;
5909 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04005910 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04005911 u64 extent_start = 0;
5912 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08005913 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04005914 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04005915 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04005916 struct btrfs_root *root = BTRFS_I(inode)->root;
5917 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04005918 struct extent_buffer *leaf;
5919 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04005920 struct extent_map *em = NULL;
5921 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05005922 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04005923 struct btrfs_trans_handle *trans = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08005924 int compress_type;
Chris Masona52d9a82007-08-27 16:49:44 -04005925
Chris Masona52d9a82007-08-27 16:49:44 -04005926again:
Chris Mason890871b2009-09-02 16:24:52 -04005927 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005928 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04005929 if (em)
5930 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04005931 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005932
Chris Masona52d9a82007-08-27 16:49:44 -04005933 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04005934 if (em->start > start || em->start + em->len <= start)
5935 free_extent_map(em);
5936 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05005937 free_extent_map(em);
5938 else
5939 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005940 }
David Sterba172ddd62011-04-21 00:48:27 +02005941 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04005942 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05005943 err = -ENOMEM;
5944 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005945 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005946 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05005947 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05005948 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05005949 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04005950 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04005951
5952 if (!path) {
5953 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04005954 if (!path) {
5955 err = -ENOMEM;
5956 goto out;
5957 }
5958 /*
5959 * Chances are we'll be called again, so go ahead and do
5960 * readahead
5961 */
5962 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04005963 }
5964
Chris Mason179e29e2007-11-01 11:28:41 -04005965 ret = btrfs_lookup_file_extent(trans, root, path,
5966 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04005967 if (ret < 0) {
5968 err = ret;
5969 goto out;
5970 }
5971
5972 if (ret != 0) {
5973 if (path->slots[0] == 0)
5974 goto not_found;
5975 path->slots[0]--;
5976 }
5977
Chris Mason5f39d392007-10-15 16:14:19 -04005978 leaf = path->nodes[0];
5979 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04005980 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04005981 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04005982 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5983 found_type = btrfs_key_type(&found_key);
5984 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04005985 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04005986 /*
5987 * If we backup past the first extent we want to move forward
5988 * and see if there is an extent in front of us, otherwise we'll
5989 * say there is a hole for our whole search range which can
5990 * cause problems.
5991 */
5992 extent_end = start;
5993 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04005994 }
5995
Chris Mason5f39d392007-10-15 16:14:19 -04005996 found_type = btrfs_file_extent_type(leaf, item);
5997 extent_start = found_key.offset;
Li Zefan261507a02010-12-17 14:21:50 +08005998 compress_type = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04005999 if (found_type == BTRFS_FILE_EXTENT_REG ||
6000 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006001 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006002 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006003 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6004 size_t size;
6005 size = btrfs_file_extent_inline_len(leaf, item);
Qu Wenruofda28322013-02-26 08:10:22 +00006006 extent_end = ALIGN(extent_start + size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04006007 }
Josef Bacik25a50342013-10-14 12:08:38 -04006008next:
Yan Zheng9036c102008-10-30 14:19:41 -04006009 if (start >= extent_end) {
6010 path->slots[0]++;
6011 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6012 ret = btrfs_next_leaf(root, path);
6013 if (ret < 0) {
6014 err = ret;
6015 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006016 }
Yan Zheng9036c102008-10-30 14:19:41 -04006017 if (ret > 0)
6018 goto not_found;
6019 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006020 }
Yan Zheng9036c102008-10-30 14:19:41 -04006021 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6022 if (found_key.objectid != objectid ||
6023 found_key.type != BTRFS_EXTENT_DATA_KEY)
6024 goto not_found;
6025 if (start + len <= found_key.offset)
6026 goto not_found;
6027 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006028 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006029 em->len = found_key.offset - start;
6030 goto not_found_em;
6031 }
6032
Josef Bacikcc95bef2013-04-04 14:31:27 -04006033 em->ram_bytes = btrfs_file_extent_ram_bytes(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04006034 if (found_type == BTRFS_FILE_EXTENT_REG ||
6035 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04006036 em->start = extent_start;
6037 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05006038 em->orig_start = extent_start -
6039 btrfs_file_extent_offset(leaf, item);
Josef Bacikb4939682012-12-03 10:31:19 -05006040 em->orig_block_len = btrfs_file_extent_disk_num_bytes(leaf,
6041 item);
Chris Masondb945352007-10-15 16:15:53 -04006042 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
6043 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04006044 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04006045 goto insert;
6046 }
Li Zefan261507a02010-12-17 14:21:50 +08006047 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006048 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08006049 em->compress_type = compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006050 em->block_start = bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05006051 em->block_len = em->orig_block_len;
Chris Masonc8b97812008-10-29 14:49:59 -04006052 } else {
6053 bytenr += btrfs_file_extent_offset(leaf, item);
6054 em->block_start = bytenr;
6055 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04006056 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
6057 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04006058 }
Chris Masona52d9a82007-08-27 16:49:44 -04006059 goto insert;
6060 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006061 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006062 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006063 size_t size;
6064 size_t extent_offset;
6065 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006066
Chris Masona52d9a82007-08-27 16:49:44 -04006067 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04006068 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04006069 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04006070 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04006071 goto out;
6072 }
6073
Yan Zheng9036c102008-10-30 14:19:41 -04006074 size = btrfs_file_extent_inline_len(leaf, item);
6075 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05006076 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04006077 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006078 em->start = extent_start + extent_offset;
Qu Wenruofda28322013-02-26 08:10:22 +00006079 em->len = ALIGN(copy_size, root->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006080 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006081 em->orig_start = em->start;
Li Zefan261507a02010-12-17 14:21:50 +08006082 if (compress_type) {
Chris Masonc8b97812008-10-29 14:49:59 -04006083 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08006084 em->compress_type = compress_type;
6085 }
Yan689f9342007-10-29 11:41:07 -04006086 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006087 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006088 if (btrfs_file_extent_compression(leaf, item) !=
6089 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006090 ret = uncompress_inline(path, inode, page,
6091 pg_offset,
6092 extent_offset, item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006093 BUG_ON(ret); /* -ENOMEM */
Chris Masonc8b97812008-10-29 14:49:59 -04006094 } else {
6095 map = kmap(page);
6096 read_extent_buffer(leaf, map + pg_offset, ptr,
6097 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04006098 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
6099 memset(map + pg_offset + copy_size, 0,
6100 PAGE_CACHE_SIZE - pg_offset -
6101 copy_size);
6102 }
Chris Masonc8b97812008-10-29 14:49:59 -04006103 kunmap(page);
6104 }
Chris Mason179e29e2007-11-01 11:28:41 -04006105 flush_dcache_page(page);
6106 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006107 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006108 if (!trans) {
6109 kunmap(page);
6110 free_extent_map(em);
6111 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006112
David Sterbab3b4aa72011-04-21 01:20:15 +02006113 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006114 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006115
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006116 if (IS_ERR(trans))
6117 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006118 goto again;
6119 }
Chris Masonc8b97812008-10-29 14:49:59 -04006120 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006121 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006122 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006123 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006124 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006125 }
Chris Masond1310b22008-01-24 16:13:08 -05006126 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006127 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006128 goto insert;
6129 } else {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00006130 WARN(1, KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04006131 }
6132not_found:
6133 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006134 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006135 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006136not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04006137 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04006138 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006139insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006140 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006141 if (em->start > start || extent_map_end(em) <= start) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006142 btrfs_err(root->fs_info, "bad extent! em: [%llu %llu] passed [%llu %llu]",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006143 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04006144 err = -EIO;
6145 goto out;
6146 }
Chris Masond1310b22008-01-24 16:13:08 -05006147
6148 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006149 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006150 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006151 /* it is possible that someone inserted the extent into the tree
6152 * while we had the lock dropped. It is also possible that
6153 * an overlapping map exists in the tree
6154 */
Chris Masona52d9a82007-08-27 16:49:44 -04006155 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04006156 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006157
6158 ret = 0;
6159
Chris Mason3b951512008-04-17 11:29:12 -04006160 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04006161 if (existing && (existing->start > start ||
6162 existing->start + existing->len <= start)) {
6163 free_extent_map(existing);
6164 existing = NULL;
6165 }
Chris Mason3b951512008-04-17 11:29:12 -04006166 if (!existing) {
6167 existing = lookup_extent_mapping(em_tree, em->start,
6168 em->len);
6169 if (existing) {
6170 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006171 em, start,
6172 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04006173 free_extent_map(existing);
6174 if (err) {
6175 free_extent_map(em);
6176 em = NULL;
6177 }
6178 } else {
6179 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04006180 free_extent_map(em);
6181 em = NULL;
6182 }
6183 } else {
6184 free_extent_map(em);
6185 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006186 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006187 }
Chris Masona52d9a82007-08-27 16:49:44 -04006188 }
Chris Mason890871b2009-09-02 16:24:52 -04006189 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006190out:
liubo1abe9b82011-03-24 11:18:59 +00006191
Tsutomu Itohf0bd95e2012-10-01 03:08:37 -06006192 if (em)
6193 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00006194
Chris Masonf4219502008-07-22 11:18:09 -04006195 if (path)
6196 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04006197 if (trans) {
6198 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05006199 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04006200 err = ret;
6201 }
Chris Masona52d9a82007-08-27 16:49:44 -04006202 if (err) {
6203 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04006204 return ERR_PTR(err);
6205 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006206 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04006207 return em;
6208}
6209
Chris Masonec29ed52011-02-23 16:23:20 -05006210struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
6211 size_t pg_offset, u64 start, u64 len,
6212 int create)
6213{
6214 struct extent_map *em;
6215 struct extent_map *hole_em = NULL;
6216 u64 range_start = start;
6217 u64 end;
6218 u64 found;
6219 u64 found_end;
6220 int err = 0;
6221
6222 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
6223 if (IS_ERR(em))
6224 return em;
6225 if (em) {
6226 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00006227 * if our em maps to
6228 * - a hole or
6229 * - a pre-alloc extent,
6230 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05006231 */
Liu Bof9e4fb52013-01-07 10:10:12 +00006232 if (em->block_start != EXTENT_MAP_HOLE &&
6233 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05006234 return em;
6235 else
6236 hole_em = em;
6237 }
6238
6239 /* check to see if we've wrapped (len == -1 or similar) */
6240 end = start + len;
6241 if (end < start)
6242 end = (u64)-1;
6243 else
6244 end -= 1;
6245
6246 em = NULL;
6247
6248 /* ok, we didn't find anything, lets look for delalloc */
6249 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
6250 end, len, EXTENT_DELALLOC, 1);
6251 found_end = range_start + found;
6252 if (found_end < range_start)
6253 found_end = (u64)-1;
6254
6255 /*
6256 * we didn't find anything useful, return
6257 * the original results from get_extent()
6258 */
6259 if (range_start > end || found_end <= start) {
6260 em = hole_em;
6261 hole_em = NULL;
6262 goto out;
6263 }
6264
6265 /* adjust the range_start to make sure it doesn't
6266 * go backwards from the start they passed in
6267 */
6268 range_start = max(start,range_start);
6269 found = found_end - range_start;
6270
6271 if (found > 0) {
6272 u64 hole_start = start;
6273 u64 hole_len = len;
6274
David Sterba172ddd62011-04-21 00:48:27 +02006275 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05006276 if (!em) {
6277 err = -ENOMEM;
6278 goto out;
6279 }
6280 /*
6281 * when btrfs_get_extent can't find anything it
6282 * returns one huge hole
6283 *
6284 * make sure what it found really fits our range, and
6285 * adjust to make sure it is based on the start from
6286 * the caller
6287 */
6288 if (hole_em) {
6289 u64 calc_end = extent_map_end(hole_em);
6290
6291 if (calc_end <= start || (hole_em->start > end)) {
6292 free_extent_map(hole_em);
6293 hole_em = NULL;
6294 } else {
6295 hole_start = max(hole_em->start, start);
6296 hole_len = calc_end - hole_start;
6297 }
6298 }
6299 em->bdev = NULL;
6300 if (hole_em && range_start > hole_start) {
6301 /* our hole starts before our delalloc, so we
6302 * have to return just the parts of the hole
6303 * that go until the delalloc starts
6304 */
6305 em->len = min(hole_len,
6306 range_start - hole_start);
6307 em->start = hole_start;
6308 em->orig_start = hole_start;
6309 /*
6310 * don't adjust block start at all,
6311 * it is fixed at EXTENT_MAP_HOLE
6312 */
6313 em->block_start = hole_em->block_start;
6314 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00006315 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
6316 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05006317 } else {
6318 em->start = range_start;
6319 em->len = found;
6320 em->orig_start = range_start;
6321 em->block_start = EXTENT_MAP_DELALLOC;
6322 em->block_len = found;
6323 }
6324 } else if (hole_em) {
6325 return hole_em;
6326 }
6327out:
6328
6329 free_extent_map(hole_em);
6330 if (err) {
6331 free_extent_map(em);
6332 return ERR_PTR(err);
6333 }
6334 return em;
6335}
6336
Josef Bacik4b46fce2010-05-23 11:00:55 -04006337static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
6338 u64 start, u64 len)
6339{
6340 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04006341 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006342 struct btrfs_key ins;
6343 u64 alloc_hint;
6344 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006345
Josef Bacik4b46fce2010-05-23 11:00:55 -04006346 alloc_hint = get_extent_allocation_hint(inode, start, len);
Josef Bacik00361582013-08-14 14:02:47 -04006347 ret = btrfs_reserve_extent(root, len, root->sectorsize, 0,
Josef Bacik81c9ad22012-01-18 10:56:06 -05006348 alloc_hint, &ins, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006349 if (ret)
6350 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006351
Josef Bacik70c8a912012-10-11 16:54:30 -04006352 em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006353 ins.offset, ins.offset, ins.offset, 0);
Josef Bacik00361582013-08-14 14:02:47 -04006354 if (IS_ERR(em)) {
6355 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
6356 return em;
6357 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006358
6359 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
6360 ins.offset, ins.offset, 0);
6361 if (ret) {
6362 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
Josef Bacik00361582013-08-14 14:02:47 -04006363 free_extent_map(em);
6364 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006365 }
Josef Bacik00361582013-08-14 14:02:47 -04006366
Josef Bacik4b46fce2010-05-23 11:00:55 -04006367 return em;
6368}
6369
Chris Mason46bfbb52010-05-26 11:04:10 -04006370/*
6371 * returns 1 when the nocow is safe, < 1 on error, 0 if the
6372 * block must be cow'd
6373 */
Josef Bacik00361582013-08-14 14:02:47 -04006374noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04006375 u64 *orig_start, u64 *orig_block_len,
6376 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04006377{
Josef Bacik00361582013-08-14 14:02:47 -04006378 struct btrfs_trans_handle *trans;
Chris Mason46bfbb52010-05-26 11:04:10 -04006379 struct btrfs_path *path;
6380 int ret;
6381 struct extent_buffer *leaf;
6382 struct btrfs_root *root = BTRFS_I(inode)->root;
6383 struct btrfs_file_extent_item *fi;
6384 struct btrfs_key key;
6385 u64 disk_bytenr;
6386 u64 backref_offset;
6387 u64 extent_end;
6388 u64 num_bytes;
6389 int slot;
6390 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04006391 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Chris Mason46bfbb52010-05-26 11:04:10 -04006392 path = btrfs_alloc_path();
6393 if (!path)
6394 return -ENOMEM;
6395
Josef Bacik00361582013-08-14 14:02:47 -04006396 ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04006397 offset, 0);
6398 if (ret < 0)
6399 goto out;
6400
6401 slot = path->slots[0];
6402 if (ret == 1) {
6403 if (slot == 0) {
6404 /* can't find the item, must cow */
6405 ret = 0;
6406 goto out;
6407 }
6408 slot--;
6409 }
6410 ret = 0;
6411 leaf = path->nodes[0];
6412 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08006413 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04006414 key.type != BTRFS_EXTENT_DATA_KEY) {
6415 /* not our file or wrong item type, must cow */
6416 goto out;
6417 }
6418
6419 if (key.offset > offset) {
6420 /* Wrong offset, must cow */
6421 goto out;
6422 }
6423
6424 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
6425 found_type = btrfs_file_extent_type(leaf, fi);
6426 if (found_type != BTRFS_FILE_EXTENT_REG &&
6427 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
6428 /* not a regular extent, must cow */
6429 goto out;
6430 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04006431
6432 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
6433 goto out;
6434
Chris Mason46bfbb52010-05-26 11:04:10 -04006435 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04006436 if (disk_bytenr == 0)
6437 goto out;
6438
6439 if (btrfs_file_extent_compression(leaf, fi) ||
6440 btrfs_file_extent_encryption(leaf, fi) ||
6441 btrfs_file_extent_other_encoding(leaf, fi))
6442 goto out;
6443
Chris Mason46bfbb52010-05-26 11:04:10 -04006444 backref_offset = btrfs_file_extent_offset(leaf, fi);
6445
Josef Bacik7ee9e442013-06-21 16:37:03 -04006446 if (orig_start) {
6447 *orig_start = key.offset - backref_offset;
6448 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
6449 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
6450 }
Josef Bacikeb384b52013-04-24 16:32:55 -04006451
Chris Mason46bfbb52010-05-26 11:04:10 -04006452 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason46bfbb52010-05-26 11:04:10 -04006453
6454 if (btrfs_extent_readonly(root, disk_bytenr))
6455 goto out;
Josef Bacik1bda19e2013-10-18 12:10:36 -04006456 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04006457
6458 /*
6459 * look for other files referencing this extent, if we
6460 * find any we must cow
6461 */
Josef Bacik00361582013-08-14 14:02:47 -04006462 trans = btrfs_join_transaction(root);
6463 if (IS_ERR(trans)) {
6464 ret = 0;
Chris Mason46bfbb52010-05-26 11:04:10 -04006465 goto out;
Josef Bacik00361582013-08-14 14:02:47 -04006466 }
6467
6468 ret = btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
6469 key.offset - backref_offset, disk_bytenr);
6470 btrfs_end_transaction(trans, root);
6471 if (ret) {
6472 ret = 0;
6473 goto out;
6474 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006475
6476 /*
6477 * adjust disk_bytenr and num_bytes to cover just the bytes
6478 * in this extent we are about to write. If there
6479 * are any csums in that range we have to cow in order
6480 * to keep the csums correct
6481 */
6482 disk_bytenr += backref_offset;
6483 disk_bytenr += offset - key.offset;
Josef Bacikeb384b52013-04-24 16:32:55 -04006484 num_bytes = min(offset + *len, extent_end) - offset;
Chris Mason46bfbb52010-05-26 11:04:10 -04006485 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
6486 goto out;
6487 /*
6488 * all of the above have passed, it is safe to overwrite this extent
6489 * without cow
6490 */
Josef Bacikeb384b52013-04-24 16:32:55 -04006491 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04006492 ret = 1;
6493out:
6494 btrfs_free_path(path);
6495 return ret;
6496}
6497
Josef Bacikeb838e72012-07-31 16:28:48 -04006498static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
6499 struct extent_state **cached_state, int writing)
6500{
6501 struct btrfs_ordered_extent *ordered;
6502 int ret = 0;
6503
6504 while (1) {
6505 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6506 0, cached_state);
6507 /*
6508 * We're concerned with the entire range that we're going to be
6509 * doing DIO to, so we need to make sure theres no ordered
6510 * extents in this range.
6511 */
6512 ordered = btrfs_lookup_ordered_range(inode, lockstart,
6513 lockend - lockstart + 1);
6514
6515 /*
6516 * We need to make sure there are no buffered pages in this
6517 * range either, we could have raced between the invalidate in
6518 * generic_file_direct_write and locking the extent. The
6519 * invalidate needs to happen so that reads after a write do not
6520 * get stale data.
6521 */
6522 if (!ordered && (!writing ||
6523 !test_range_bit(&BTRFS_I(inode)->io_tree,
6524 lockstart, lockend, EXTENT_UPTODATE, 0,
6525 *cached_state)))
6526 break;
6527
6528 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6529 cached_state, GFP_NOFS);
6530
6531 if (ordered) {
6532 btrfs_start_ordered_extent(inode, ordered, 1);
6533 btrfs_put_ordered_extent(ordered);
6534 } else {
6535 /* Screw you mmap */
6536 ret = filemap_write_and_wait_range(inode->i_mapping,
6537 lockstart,
6538 lockend);
6539 if (ret)
6540 break;
6541
6542 /*
6543 * If we found a page that couldn't be invalidated just
6544 * fall back to buffered.
6545 */
6546 ret = invalidate_inode_pages2_range(inode->i_mapping,
6547 lockstart >> PAGE_CACHE_SHIFT,
6548 lockend >> PAGE_CACHE_SHIFT);
6549 if (ret)
6550 break;
6551 }
6552
6553 cond_resched();
6554 }
6555
6556 return ret;
6557}
6558
Josef Bacik69ffb542012-09-11 15:40:07 -04006559static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
6560 u64 len, u64 orig_start,
6561 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006562 u64 orig_block_len, u64 ram_bytes,
6563 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04006564{
6565 struct extent_map_tree *em_tree;
6566 struct extent_map *em;
6567 struct btrfs_root *root = BTRFS_I(inode)->root;
6568 int ret;
6569
6570 em_tree = &BTRFS_I(inode)->extent_tree;
6571 em = alloc_extent_map();
6572 if (!em)
6573 return ERR_PTR(-ENOMEM);
6574
6575 em->start = start;
6576 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04006577 em->mod_start = start;
6578 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04006579 em->len = len;
6580 em->block_len = block_len;
6581 em->block_start = block_start;
6582 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05006583 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04006584 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04006585 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04006586 set_bit(EXTENT_FLAG_PINNED, &em->flags);
6587 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05006588 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04006589
6590 do {
6591 btrfs_drop_extent_cache(inode, em->start,
6592 em->start + em->len - 1, 0);
6593 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006594 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04006595 write_unlock(&em_tree->lock);
6596 } while (ret == -EEXIST);
6597
6598 if (ret) {
6599 free_extent_map(em);
6600 return ERR_PTR(ret);
6601 }
6602
6603 return em;
6604}
6605
6606
Josef Bacik4b46fce2010-05-23 11:00:55 -04006607static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
6608 struct buffer_head *bh_result, int create)
6609{
6610 struct extent_map *em;
6611 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04006612 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006613 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04006614 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006615 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04006616 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00006617 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006618
Miao Xie172a5042013-02-21 02:48:22 -07006619 if (create)
Josef Bacikeb838e72012-07-31 16:28:48 -04006620 unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07006621 else
Josef Bacikc3298612012-08-03 16:49:19 -04006622 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04006623
Josef Bacikc3298612012-08-03 16:49:19 -04006624 lockstart = start;
6625 lockend = start + len - 1;
6626
Josef Bacikeb838e72012-07-31 16:28:48 -04006627 /*
6628 * If this errors out it's because we couldn't invalidate pagecache for
6629 * this range and we need to fallback to buffered.
6630 */
6631 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
6632 return -ENOTBLK;
6633
Josef Bacik4b46fce2010-05-23 11:00:55 -04006634 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04006635 if (IS_ERR(em)) {
6636 ret = PTR_ERR(em);
6637 goto unlock_err;
6638 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006639
6640 /*
6641 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
6642 * io. INLINE is special, and we could probably kludge it in here, but
6643 * it's still buffered so for safety lets just fall back to the generic
6644 * buffered path.
6645 *
6646 * For COMPRESSED we _have_ to read the entire extent in so we can
6647 * decompress it, so there will be buffering required no matter what we
6648 * do, so go ahead and fallback to buffered.
6649 *
6650 * We return -ENOTBLK because thats what makes DIO go ahead and go back
6651 * to buffered IO. Don't blame me, this is the price we pay for using
6652 * the generic code.
6653 */
6654 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
6655 em->block_start == EXTENT_MAP_INLINE) {
6656 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006657 ret = -ENOTBLK;
6658 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006659 }
6660
6661 /* Just a good old fashioned hole, return */
6662 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
6663 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
6664 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006665 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006666 }
6667
6668 /*
6669 * We don't allocate a new extent in the following cases
6670 *
6671 * 1) The inode is marked as NODATACOW. In this case we'll just use the
6672 * existing extent.
6673 * 2) The extent is marked as PREALLOC. We're good to go here and can
6674 * just use the extent.
6675 *
6676 */
Chris Mason46bfbb52010-05-26 11:04:10 -04006677 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04006678 len = min(len, em->len - (start - em->start));
6679 lockstart = start + len;
6680 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04006681 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006682
6683 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
6684 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
6685 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04006686 int type;
6687 int ret;
Josef Bacikeb384b52013-04-24 16:32:55 -04006688 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006689
6690 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6691 type = BTRFS_ORDERED_PREALLOC;
6692 else
6693 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04006694 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04006695 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04006696
Josef Bacik00361582013-08-14 14:02:47 -04006697 if (can_nocow_extent(inode, start, &len, &orig_start,
Josef Bacik7ee9e442013-06-21 16:37:03 -04006698 &orig_block_len, &ram_bytes) == 1) {
Josef Bacik69ffb542012-09-11 15:40:07 -04006699 if (type == BTRFS_ORDERED_PREALLOC) {
6700 free_extent_map(em);
6701 em = create_pinned_em(inode, start, len,
6702 orig_start,
Josef Bacikb4939682012-12-03 10:31:19 -05006703 block_start, len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006704 orig_block_len,
6705 ram_bytes, type);
Josef Bacik00361582013-08-14 14:02:47 -04006706 if (IS_ERR(em))
Josef Bacik69ffb542012-09-11 15:40:07 -04006707 goto unlock_err;
Josef Bacik69ffb542012-09-11 15:40:07 -04006708 }
6709
Chris Mason46bfbb52010-05-26 11:04:10 -04006710 ret = btrfs_add_ordered_extent_dio(inode, start,
6711 block_start, len, len, type);
Chris Mason46bfbb52010-05-26 11:04:10 -04006712 if (ret) {
6713 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006714 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04006715 }
6716 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006717 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006718 }
Josef Bacik00361582013-08-14 14:02:47 -04006719
Chris Mason46bfbb52010-05-26 11:04:10 -04006720 /*
6721 * this will cow the extent, reset the len in case we changed
6722 * it above
6723 */
6724 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04006725 free_extent_map(em);
6726 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04006727 if (IS_ERR(em)) {
6728 ret = PTR_ERR(em);
6729 goto unlock_err;
6730 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006731 len = min(len, em->len - (start - em->start));
6732unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04006733 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
6734 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04006735 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006736 bh_result->b_bdev = em->bdev;
6737 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04006738 if (create) {
6739 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6740 set_buffer_new(bh_result);
6741
6742 /*
6743 * Need to update the i_size under the extent lock so buffered
6744 * readers will get the updated i_size when we unlock.
6745 */
6746 if (start + len > i_size_read(inode))
6747 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00006748
Miao Xie172a5042013-02-21 02:48:22 -07006749 spin_lock(&BTRFS_I(inode)->lock);
6750 BTRFS_I(inode)->outstanding_extents++;
6751 spin_unlock(&BTRFS_I(inode)->lock);
6752
Miao Xie09348562013-02-07 10:12:07 +00006753 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6754 lockstart + len - 1, EXTENT_DELALLOC, NULL,
6755 &cached_state, GFP_NOFS);
6756 BUG_ON(ret);
Josef Bacikc3473e82012-06-19 10:59:00 -04006757 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006758
Josef Bacikeb838e72012-07-31 16:28:48 -04006759 /*
6760 * In the case of write we need to clear and unlock the entire range,
6761 * in the case of read we need to unlock only the end area that we
6762 * aren't using if there is any left over space.
6763 */
Liu Bo24c03fa2012-08-22 20:10:38 -06006764 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00006765 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6766 lockend, unlock_bits, 1, 0,
6767 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06006768 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04006769 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06006770 }
Josef Bacikeb838e72012-07-31 16:28:48 -04006771
Josef Bacik4b46fce2010-05-23 11:00:55 -04006772 free_extent_map(em);
6773
6774 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006775
6776unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04006777 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6778 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
6779 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006780}
6781
Josef Bacik4b46fce2010-05-23 11:00:55 -04006782static void btrfs_endio_direct_read(struct bio *bio, int err)
6783{
Miao Xiee65e1532010-11-22 03:04:43 +00006784 struct btrfs_dio_private *dip = bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006785 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
6786 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006787 struct inode *inode = dip->inode;
6788 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason9be33952013-05-17 18:30:14 -04006789 struct bio *dio_bio;
Miao Xiefacc8a222013-07-25 19:22:34 +08006790 u32 *csums = (u32 *)dip->csum;
6791 int index = 0;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006792 u64 start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006793
6794 start = dip->logical_offset;
6795 do {
6796 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
6797 struct page *page = bvec->bv_page;
6798 char *kaddr;
6799 u32 csum = ~(u32)0;
6800 unsigned long flags;
6801
6802 local_irq_save(flags);
Cong Wang7ac687d2011-11-25 23:14:28 +08006803 kaddr = kmap_atomic(page);
Liu Bob0496682013-03-14 14:57:45 +00006804 csum = btrfs_csum_data(kaddr + bvec->bv_offset,
Josef Bacik4b46fce2010-05-23 11:00:55 -04006805 csum, bvec->bv_len);
6806 btrfs_csum_final(csum, (char *)&csum);
Cong Wang7ac687d2011-11-25 23:14:28 +08006807 kunmap_atomic(kaddr);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006808 local_irq_restore(flags);
6809
6810 flush_dcache_page(bvec->bv_page);
Miao Xiefacc8a222013-07-25 19:22:34 +08006811 if (csum != csums[index]) {
6812 btrfs_err(root->fs_info, "csum failed ino %llu off %llu csum %u expected csum %u",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006813 btrfs_ino(inode), start, csum,
6814 csums[index]);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006815 err = -EIO;
6816 }
6817 }
6818
6819 start += bvec->bv_len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006820 bvec++;
Miao Xiefacc8a222013-07-25 19:22:34 +08006821 index++;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006822 } while (bvec <= bvec_end);
6823
6824 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006825 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04006826 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006827
Josef Bacik4b46fce2010-05-23 11:00:55 -04006828 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006829
6830 /* If we had a csum failure make sure to clear the uptodate flag */
6831 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04006832 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
6833 dio_end_io(dio_bio, err);
6834 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006835}
6836
6837static void btrfs_endio_direct_write(struct bio *bio, int err)
6838{
6839 struct btrfs_dio_private *dip = bio->bi_private;
6840 struct inode *inode = dip->inode;
6841 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006842 struct btrfs_ordered_extent *ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05006843 u64 ordered_offset = dip->logical_offset;
6844 u64 ordered_bytes = dip->bytes;
Chris Mason9be33952013-05-17 18:30:14 -04006845 struct bio *dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006846 int ret;
6847
6848 if (err)
6849 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05006850again:
6851 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
6852 &ordered_offset,
Josef Bacik5fd02042012-05-02 14:00:54 -04006853 ordered_bytes, !err);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006854 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05006855 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006856
Josef Bacik5fd02042012-05-02 14:00:54 -04006857 ordered->work.func = finish_ordered_fn;
6858 ordered->work.flags = 0;
6859 btrfs_queue_worker(&root->fs_info->endio_write_workers,
6860 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05006861out_test:
6862 /*
6863 * our bio might span multiple ordered extents. If we haven't
6864 * completed the accounting for the whole dio, go back and try again
6865 */
6866 if (ordered_offset < dip->logical_offset + dip->bytes) {
6867 ordered_bytes = dip->logical_offset + dip->bytes -
6868 ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04006869 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05006870 goto again;
6871 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006872out_done:
Chris Mason9be33952013-05-17 18:30:14 -04006873 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006874
Josef Bacik4b46fce2010-05-23 11:00:55 -04006875 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006876
6877 /* If we had an error make sure to clear the uptodate flag */
6878 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04006879 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
6880 dio_end_io(dio_bio, err);
6881 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006882}
6883
Chris Masoneaf25d92010-05-25 09:48:28 -04006884static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
6885 struct bio *bio, int mirror_num,
6886 unsigned long bio_flags, u64 offset)
6887{
6888 int ret;
6889 struct btrfs_root *root = BTRFS_I(inode)->root;
6890 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006891 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04006892 return 0;
6893}
6894
Miao Xiee65e1532010-11-22 03:04:43 +00006895static void btrfs_end_dio_bio(struct bio *bio, int err)
6896{
6897 struct btrfs_dio_private *dip = bio->bi_private;
6898
6899 if (err) {
Li Zefan33345d012011-04-20 10:31:50 +08006900 printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu "
Jan Beulich3dd14622010-12-07 14:54:09 +00006901 "sector %#Lx len %u err no %d\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006902 btrfs_ino(dip->inode), bio->bi_rw,
Jan Beulich3dd14622010-12-07 14:54:09 +00006903 (unsigned long long)bio->bi_sector, bio->bi_size, err);
Miao Xiee65e1532010-11-22 03:04:43 +00006904 dip->errors = 1;
6905
6906 /*
6907 * before atomic variable goto zero, we must make sure
6908 * dip->errors is perceived to be set.
6909 */
6910 smp_mb__before_atomic_dec();
6911 }
6912
6913 /* if there are more bios still pending for this dio, just exit */
6914 if (!atomic_dec_and_test(&dip->pending_bios))
6915 goto out;
6916
Chris Mason9be33952013-05-17 18:30:14 -04006917 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00006918 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04006919 } else {
6920 set_bit(BIO_UPTODATE, &dip->dio_bio->bi_flags);
Miao Xiee65e1532010-11-22 03:04:43 +00006921 bio_endio(dip->orig_bio, 0);
6922 }
6923out:
6924 bio_put(bio);
6925}
6926
6927static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
6928 u64 first_sector, gfp_t gfp_flags)
6929{
6930 int nr_vecs = bio_get_nr_vecs(bdev);
6931 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
6932}
6933
6934static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
6935 int rw, u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04006936 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00006937{
Miao Xiefacc8a222013-07-25 19:22:34 +08006938 struct btrfs_dio_private *dip = bio->bi_private;
Miao Xiee65e1532010-11-22 03:04:43 +00006939 int write = rw & REQ_WRITE;
6940 struct btrfs_root *root = BTRFS_I(inode)->root;
6941 int ret;
6942
Josef Bacikb812ce22012-11-16 13:56:32 -05006943 if (async_submit)
6944 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
6945
Miao Xiee65e1532010-11-22 03:04:43 +00006946 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04006947
6948 if (!write) {
6949 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
6950 if (ret)
6951 goto err;
6952 }
Miao Xiee65e1532010-11-22 03:04:43 +00006953
Josef Bacik1ae39932011-04-06 14:41:34 -04006954 if (skip_sum)
6955 goto map;
6956
6957 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00006958 ret = btrfs_wq_submit_bio(root->fs_info,
6959 inode, rw, bio, 0, 0,
6960 file_offset,
6961 __btrfs_submit_bio_start_direct_io,
6962 __btrfs_submit_bio_done);
6963 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04006964 } else if (write) {
6965 /*
6966 * If we aren't doing async submit, calculate the csum of the
6967 * bio now.
6968 */
6969 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
6970 if (ret)
6971 goto err;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00006972 } else if (!skip_sum) {
Miao Xiefacc8a222013-07-25 19:22:34 +08006973 ret = btrfs_lookup_bio_sums_dio(root, inode, dip, bio,
6974 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00006975 if (ret)
6976 goto err;
6977 }
Miao Xiee65e1532010-11-22 03:04:43 +00006978
Josef Bacik1ae39932011-04-06 14:41:34 -04006979map:
6980 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006981err:
6982 bio_put(bio);
6983 return ret;
6984}
6985
6986static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
6987 int skip_sum)
6988{
6989 struct inode *inode = dip->inode;
6990 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00006991 struct bio *bio;
6992 struct bio *orig_bio = dip->orig_bio;
6993 struct bio_vec *bvec = orig_bio->bi_io_vec;
6994 u64 start_sector = orig_bio->bi_sector;
6995 u64 file_offset = dip->logical_offset;
6996 u64 submit_len = 0;
6997 u64 map_length;
6998 int nr_pages = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00006999 int ret = 0;
Josef Bacik1ae39932011-04-06 14:41:34 -04007000 int async_submit = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007001
Miao Xiee65e1532010-11-22 03:04:43 +00007002 map_length = orig_bio->bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007003 ret = btrfs_map_block(root->fs_info, rw, start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007004 &map_length, NULL, 0);
7005 if (ret) {
Josef Bacik64728bb2011-04-25 19:43:52 -04007006 bio_put(orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00007007 return -EIO;
7008 }
Miao Xiefacc8a222013-07-25 19:22:34 +08007009
Josef Bacik02f57c72011-04-06 14:25:44 -04007010 if (map_length >= orig_bio->bi_size) {
7011 bio = orig_bio;
7012 goto submit;
7013 }
7014
David Woodhouse53b381b2013-01-29 18:40:14 -05007015 /* async crcs make it difficult to collect full stripe writes. */
7016 if (btrfs_get_alloc_profile(root, 1) &
7017 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6))
7018 async_submit = 0;
7019 else
7020 async_submit = 1;
7021
Josef Bacik02f57c72011-04-06 14:25:44 -04007022 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
7023 if (!bio)
7024 return -ENOMEM;
7025 bio->bi_private = dip;
7026 bio->bi_end_io = btrfs_end_dio_bio;
7027 atomic_inc(&dip->pending_bios);
7028
Miao Xiee65e1532010-11-22 03:04:43 +00007029 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
7030 if (unlikely(map_length < submit_len + bvec->bv_len ||
7031 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
7032 bvec->bv_offset) < bvec->bv_len)) {
7033 /*
7034 * inc the count before we submit the bio so
7035 * we know the end IO handler won't happen before
7036 * we inc the count. Otherwise, the dip might get freed
7037 * before we're done setting it up
7038 */
7039 atomic_inc(&dip->pending_bios);
7040 ret = __btrfs_submit_dio_bio(bio, inode, rw,
7041 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007042 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007043 if (ret) {
7044 bio_put(bio);
7045 atomic_dec(&dip->pending_bios);
7046 goto out_err;
7047 }
7048
Miao Xiee65e1532010-11-22 03:04:43 +00007049 start_sector += submit_len >> 9;
7050 file_offset += submit_len;
7051
7052 submit_len = 0;
7053 nr_pages = 0;
7054
7055 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
7056 start_sector, GFP_NOFS);
7057 if (!bio)
7058 goto out_err;
7059 bio->bi_private = dip;
7060 bio->bi_end_io = btrfs_end_dio_bio;
7061
7062 map_length = orig_bio->bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007063 ret = btrfs_map_block(root->fs_info, rw,
Stefan Behrens3ec706c2012-11-05 15:46:42 +01007064 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007065 &map_length, NULL, 0);
7066 if (ret) {
7067 bio_put(bio);
7068 goto out_err;
7069 }
7070 } else {
7071 submit_len += bvec->bv_len;
7072 nr_pages ++;
7073 bvec++;
7074 }
7075 }
7076
Josef Bacik02f57c72011-04-06 14:25:44 -04007077submit:
Miao Xiee65e1532010-11-22 03:04:43 +00007078 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007079 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007080 if (!ret)
7081 return 0;
7082
7083 bio_put(bio);
7084out_err:
7085 dip->errors = 1;
7086 /*
7087 * before atomic variable goto zero, we must
7088 * make sure dip->errors is perceived to be set.
7089 */
7090 smp_mb__before_atomic_dec();
7091 if (atomic_dec_and_test(&dip->pending_bios))
7092 bio_io_error(dip->orig_bio);
7093
7094 /* bio_end_io() will handle error, so we needn't return it */
7095 return 0;
7096}
7097
Chris Mason9be33952013-05-17 18:30:14 -04007098static void btrfs_submit_direct(int rw, struct bio *dio_bio,
7099 struct inode *inode, loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007100{
7101 struct btrfs_root *root = BTRFS_I(inode)->root;
7102 struct btrfs_dio_private *dip;
Chris Mason9be33952013-05-17 18:30:14 -04007103 struct bio *io_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007104 int skip_sum;
Miao Xiefacc8a222013-07-25 19:22:34 +08007105 int sum_len;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02007106 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007107 int ret = 0;
Miao Xiefacc8a222013-07-25 19:22:34 +08007108 u16 csum_size;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007109
7110 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
7111
Chris Mason9be33952013-05-17 18:30:14 -04007112 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04007113 if (!io_bio) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007114 ret = -ENOMEM;
7115 goto free_ordered;
7116 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007117
Miao Xiefacc8a222013-07-25 19:22:34 +08007118 if (!skip_sum && !write) {
7119 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
7120 sum_len = dio_bio->bi_size >> inode->i_sb->s_blocksize_bits;
7121 sum_len *= csum_size;
7122 } else {
7123 sum_len = 0;
7124 }
7125
7126 dip = kmalloc(sizeof(*dip) + sum_len, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04007127 if (!dip) {
7128 ret = -ENOMEM;
7129 goto free_io_bio;
7130 }
7131
7132 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007133 dip->inode = inode;
7134 dip->logical_offset = file_offset;
Miao Xiee6da5d22013-06-19 18:19:17 +08007135 dip->bytes = dio_bio->bi_size;
Chris Mason9be33952013-05-17 18:30:14 -04007136 dip->disk_bytenr = (u64)dio_bio->bi_sector << 9;
7137 io_bio->bi_private = dip;
Miao Xiee65e1532010-11-22 03:04:43 +00007138 dip->errors = 0;
Chris Mason9be33952013-05-17 18:30:14 -04007139 dip->orig_bio = io_bio;
7140 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00007141 atomic_set(&dip->pending_bios, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007142
7143 if (write)
Chris Mason9be33952013-05-17 18:30:14 -04007144 io_bio->bi_end_io = btrfs_endio_direct_write;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007145 else
Chris Mason9be33952013-05-17 18:30:14 -04007146 io_bio->bi_end_io = btrfs_endio_direct_read;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007147
Miao Xiee65e1532010-11-22 03:04:43 +00007148 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
7149 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04007150 return;
Chris Mason9be33952013-05-17 18:30:14 -04007151
7152free_io_bio:
7153 bio_put(io_bio);
7154
Josef Bacik4b46fce2010-05-23 11:00:55 -04007155free_ordered:
7156 /*
7157 * If this is a write, we need to clean up the reserved space and kill
7158 * the ordered extent.
7159 */
7160 if (write) {
7161 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05007162 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007163 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
7164 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
7165 btrfs_free_reserved_extent(root, ordered->start,
7166 ordered->disk_len);
7167 btrfs_put_ordered_extent(ordered);
7168 btrfs_put_ordered_extent(ordered);
7169 }
Chris Mason9be33952013-05-17 18:30:14 -04007170 bio_endio(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007171}
7172
Chris Mason5a5f79b2010-05-26 21:33:37 -04007173static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
7174 const struct iovec *iov, loff_t offset,
7175 unsigned long nr_segs)
7176{
7177 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00007178 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007179 size_t size;
7180 unsigned long addr;
7181 unsigned blocksize_mask = root->sectorsize - 1;
7182 ssize_t retval = -EINVAL;
7183 loff_t end = offset;
7184
7185 if (offset & blocksize_mask)
7186 goto out;
7187
7188 /* Check the memory alignment. Blocks cannot straddle pages */
7189 for (seg = 0; seg < nr_segs; seg++) {
7190 addr = (unsigned long)iov[seg].iov_base;
7191 size = iov[seg].iov_len;
7192 end += size;
Josef Bacika1b75f72011-04-08 15:51:18 +00007193 if ((addr & blocksize_mask) || (size & blocksize_mask))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007194 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00007195
7196 /* If this is a write we don't need to check anymore */
7197 if (rw & WRITE)
7198 continue;
7199
7200 /*
7201 * Check to make sure we don't have duplicate iov_base's in this
7202 * iovec, if so return EINVAL, otherwise we'll get csum errors
7203 * when reading back.
7204 */
7205 for (i = seg + 1; i < nr_segs; i++) {
7206 if (iov[seg].iov_base == iov[i].iov_base)
7207 goto out;
7208 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04007209 }
7210 retval = 0;
7211out:
7212 return retval;
7213}
Josef Bacikeb838e72012-07-31 16:28:48 -04007214
Chris Mason16432982008-04-10 10:23:21 -04007215static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
7216 const struct iovec *iov, loff_t offset,
7217 unsigned long nr_segs)
7218{
Josef Bacik4b46fce2010-05-23 11:00:55 -04007219 struct file *file = iocb->ki_filp;
7220 struct inode *inode = file->f_mapping->host;
Miao Xie09348562013-02-07 10:12:07 +00007221 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00007222 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00007223 bool wakeup = true;
7224 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00007225 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007226
Chris Mason5a5f79b2010-05-26 21:33:37 -04007227 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
Josef Bacikeb838e72012-07-31 16:28:48 -04007228 offset, nr_segs))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007229 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007230
Miao Xie38851cc2013-02-08 07:04:11 +00007231 atomic_inc(&inode->i_dio_count);
7232 smp_mb__after_atomic_inc();
7233
Josef Bacik0e267c42013-07-02 10:38:02 -04007234 /*
7235 * The generic stuff only does filemap_write_and_wait_range, which isn't
7236 * enough if we've written compressed pages to this area, so we need to
7237 * call btrfs_wait_ordered_range to make absolutely sure that any
7238 * outstanding dirty pages are on disk.
7239 */
7240 count = iov_length(iov, nr_segs);
Josef Bacik0ef8b722013-10-25 16:13:35 -04007241 ret = btrfs_wait_ordered_range(inode, offset, count);
7242 if (ret)
7243 return ret;
Josef Bacik0e267c42013-07-02 10:38:02 -04007244
Miao Xie09348562013-02-07 10:12:07 +00007245 if (rw & WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00007246 /*
7247 * If the write DIO is beyond the EOF, we need update
7248 * the isize, but it is protected by i_mutex. So we can
7249 * not unlock the i_mutex at this case.
7250 */
7251 if (offset + count <= inode->i_size) {
7252 mutex_unlock(&inode->i_mutex);
7253 relock = true;
7254 }
Miao Xie09348562013-02-07 10:12:07 +00007255 ret = btrfs_delalloc_reserve_space(inode, count);
7256 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00007257 goto out;
7258 } else if (unlikely(test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
7259 &BTRFS_I(inode)->runtime_flags))) {
7260 inode_dio_done(inode);
7261 flags = DIO_LOCKING | DIO_SKIP_HOLES;
7262 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00007263 }
7264
7265 ret = __blockdev_direct_IO(rw, iocb, inode,
7266 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
7267 iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
Miao Xie2e60a512013-02-08 07:01:08 +00007268 btrfs_submit_direct, flags);
Miao Xie09348562013-02-07 10:12:07 +00007269 if (rw & WRITE) {
7270 if (ret < 0 && ret != -EIOCBQUEUED)
7271 btrfs_delalloc_release_space(inode, count);
Miao Xie172a5042013-02-21 02:48:22 -07007272 else if (ret >= 0 && (size_t)ret < count)
Miao Xie09348562013-02-07 10:12:07 +00007273 btrfs_delalloc_release_space(inode,
7274 count - (size_t)ret);
Miao Xie172a5042013-02-21 02:48:22 -07007275 else
7276 btrfs_delalloc_release_metadata(inode, 0);
Miao Xie09348562013-02-07 10:12:07 +00007277 }
Miao Xie38851cc2013-02-08 07:04:11 +00007278out:
Miao Xie2e60a512013-02-08 07:01:08 +00007279 if (wakeup)
7280 inode_dio_done(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00007281 if (relock)
7282 mutex_lock(&inode->i_mutex);
Miao Xie09348562013-02-07 10:12:07 +00007283
7284 return ret;
Chris Mason16432982008-04-10 10:23:21 -04007285}
7286
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007287#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
7288
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007289static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
7290 __u64 start, __u64 len)
7291{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007292 int ret;
7293
7294 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
7295 if (ret)
7296 return ret;
7297
Chris Masonec29ed52011-02-23 16:23:20 -05007298 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007299}
7300
Chris Mason9ebefb182007-06-15 13:50:00 -04007301int btrfs_readpage(struct file *file, struct page *page)
7302{
Chris Masond1310b22008-01-24 16:13:08 -05007303 struct extent_io_tree *tree;
7304 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02007305 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04007306}
Chris Mason1832a6d2007-12-21 16:27:21 -05007307
Chris Mason39279cc2007-06-12 06:35:45 -04007308static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
7309{
Chris Masond1310b22008-01-24 16:13:08 -05007310 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04007311
7312
7313 if (current->flags & PF_MEMALLOC) {
7314 redirty_page_for_writepage(wbc, page);
7315 unlock_page(page);
7316 return 0;
7317 }
Chris Masond1310b22008-01-24 16:13:08 -05007318 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04007319 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
7320}
Chris Mason39279cc2007-06-12 06:35:45 -04007321
Eric Sandeen48a3b632013-04-25 20:41:01 +00007322static int btrfs_writepages(struct address_space *mapping,
7323 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04007324{
Chris Masond1310b22008-01-24 16:13:08 -05007325 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05007326
Chris Masond1310b22008-01-24 16:13:08 -05007327 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04007328 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
7329}
7330
Chris Mason3ab2fb52007-11-08 10:59:22 -05007331static int
7332btrfs_readpages(struct file *file, struct address_space *mapping,
7333 struct list_head *pages, unsigned nr_pages)
7334{
Chris Masond1310b22008-01-24 16:13:08 -05007335 struct extent_io_tree *tree;
7336 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05007337 return extent_readpages(tree, mapping, pages, nr_pages,
7338 btrfs_get_extent);
7339}
Chris Masone6dcd2d2008-07-17 12:53:50 -04007340static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04007341{
Chris Masond1310b22008-01-24 16:13:08 -05007342 struct extent_io_tree *tree;
7343 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04007344 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04007345
Chris Masond1310b22008-01-24 16:13:08 -05007346 tree = &BTRFS_I(page->mapping->host)->io_tree;
7347 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05007348 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04007349 if (ret == 1) {
7350 ClearPagePrivate(page);
7351 set_page_private(page, 0);
7352 page_cache_release(page);
7353 }
7354 return ret;
7355}
Chris Mason39279cc2007-06-12 06:35:45 -04007356
Chris Masone6dcd2d2008-07-17 12:53:50 -04007357static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
7358{
Chris Mason98509cfc2008-09-11 15:51:43 -04007359 if (PageWriteback(page) || PageDirty(page))
7360 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05007361 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007362}
7363
Lukas Czernerd47992f2013-05-21 23:17:23 -04007364static void btrfs_invalidatepage(struct page *page, unsigned int offset,
7365 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04007366{
Josef Bacik5fd02042012-05-02 14:00:54 -04007367 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05007368 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007369 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007370 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007371 u64 page_start = page_offset(page);
7372 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04007373
Chris Mason8b62b722009-09-02 16:53:46 -04007374 /*
7375 * we have the page locked, so new writeback can't start,
7376 * and the dirty bit won't be cleared while we are here.
7377 *
7378 * Wait for IO on this page so that we can safely clear
7379 * the PagePrivate2 bit and do ordered accounting
7380 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007381 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04007382
Josef Bacik5fd02042012-05-02 14:00:54 -04007383 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007384 if (offset) {
7385 btrfs_releasepage(page, GFP_NOFS);
7386 return;
7387 }
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007388 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Miao Xie4eee4fa2012-12-21 09:17:45 +00007389 ordered = btrfs_lookup_ordered_extent(inode, page_offset(page));
Chris Masone6dcd2d2008-07-17 12:53:50 -04007390 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04007391 /*
7392 * IO on this page will never be started, so we need
7393 * to account for any ordered extents now
7394 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007395 clear_extent_bit(tree, page_start, page_end,
7396 EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06007397 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
7398 EXTENT_DEFRAG, 1, 0, &cached_state, GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04007399 /*
7400 * whoever cleared the private bit is responsible
7401 * for the finish_ordered_io
7402 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04007403 if (TestClearPagePrivate2(page)) {
7404 struct btrfs_ordered_inode_tree *tree;
7405 u64 new_len;
7406
7407 tree = &BTRFS_I(inode)->ordered_tree;
7408
7409 spin_lock_irq(&tree->lock);
7410 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
7411 new_len = page_start - ordered->file_offset;
7412 if (new_len < ordered->truncated_len)
7413 ordered->truncated_len = new_len;
7414 spin_unlock_irq(&tree->lock);
7415
7416 if (btrfs_dec_test_ordered_pending(inode, &ordered,
7417 page_start,
7418 PAGE_CACHE_SIZE, 1))
7419 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04007420 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007421 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +00007422 cached_state = NULL;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007423 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007424 }
7425 clear_extent_bit(tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04007426 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06007427 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
7428 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007429 __btrfs_releasepage(page, GFP_NOFS);
7430
Chris Mason4a096752008-07-21 10:29:44 -04007431 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04007432 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04007433 ClearPagePrivate(page);
7434 set_page_private(page, 0);
7435 page_cache_release(page);
7436 }
Chris Mason39279cc2007-06-12 06:35:45 -04007437}
7438
Chris Mason9ebefb182007-06-15 13:50:00 -04007439/*
7440 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
7441 * called from a page fault handler when a page is first dirtied. Hence we must
7442 * be careful to check for EOF conditions here. We set the page up correctly
7443 * for a written page which means we get ENOSPC checking when writing into
7444 * holes and correct delalloc and unwritten extent mapping on filesystems that
7445 * support these features.
7446 *
7447 * We are not allowed to take the i_mutex here so we have to play games to
7448 * protect against truncate races as the page could now be beyond EOF. Because
7449 * vmtruncate() writes the inode size before removing pages, once we have the
7450 * page lock we can determine safely if the page is beyond EOF. If it is not
7451 * beyond EOF, then the page is guaranteed safe against truncation until we
7452 * unlock the page.
7453 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07007454int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04007455{
Nick Pigginc2ec1752009-03-31 15:23:21 -07007456 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05007457 struct inode *inode = file_inode(vma->vm_file);
Chris Mason1832a6d2007-12-21 16:27:21 -05007458 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007459 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7460 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007461 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007462 char *kaddr;
7463 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04007464 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05007465 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05007466 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007467 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007468 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04007469
Jan Karab2b5ef52012-06-12 16:20:45 +02007470 sb_start_pagefault(inode->i_sb);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04007471 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007472 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04007473 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05007474 reserved = 1;
7475 }
Nick Piggin56a76f82009-03-31 15:23:23 -07007476 if (ret) {
7477 if (ret == -ENOMEM)
7478 ret = VM_FAULT_OOM;
7479 else /* -ENOSPC, -EIO, etc */
7480 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05007481 if (reserved)
7482 goto out;
7483 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07007484 }
Chris Mason1832a6d2007-12-21 16:27:21 -05007485
Nick Piggin56a76f82009-03-31 15:23:23 -07007486 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007487again:
Chris Mason9ebefb182007-06-15 13:50:00 -04007488 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04007489 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007490 page_start = page_offset(page);
7491 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04007492
Chris Mason9ebefb182007-06-15 13:50:00 -04007493 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04007494 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04007495 /* page got truncated out from underneath us */
7496 goto out_unlock;
7497 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007498 wait_on_page_writeback(page);
7499
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007500 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007501 set_page_extent_mapped(page);
7502
Chris Masoneb84ae02008-07-17 13:53:27 -04007503 /*
7504 * we can't set the delalloc bits if there are pending ordered
7505 * extents. Drop our locks and wait for them to finish
7506 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007507 ordered = btrfs_lookup_ordered_extent(inode, page_start);
7508 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007509 unlock_extent_cached(io_tree, page_start, page_end,
7510 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007511 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04007512 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007513 btrfs_put_ordered_extent(ordered);
7514 goto again;
7515 }
7516
Josef Bacikfbf19082009-10-01 17:10:23 -04007517 /*
7518 * XXX - page_mkwrite gets called every time the page is dirtied, even
7519 * if it was already dirty, so for space accounting reasons we need to
7520 * clear any delalloc bits for the range we are fixing to save. There
7521 * is probably a better way to do this, but for now keep consistent with
7522 * prepare_pages in the normal write path.
7523 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00007524 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06007525 EXTENT_DIRTY | EXTENT_DELALLOC |
7526 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00007527 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04007528
Josef Bacik2ac55d42010-02-03 19:33:23 +00007529 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
7530 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007531 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007532 unlock_extent_cached(io_tree, page_start, page_end,
7533 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007534 ret = VM_FAULT_SIGBUS;
7535 goto out_unlock;
7536 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007537 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04007538
7539 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04007540 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04007541 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04007542 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04007543 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04007544
Chris Masone6dcd2d2008-07-17 12:53:50 -04007545 if (zero_start != PAGE_CACHE_SIZE) {
7546 kaddr = kmap(page);
7547 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
7548 flush_dcache_page(page);
7549 kunmap(page);
7550 }
Chris Mason247e7432008-07-17 12:53:51 -04007551 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007552 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04007553 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007554
Chris Mason257c62e2009-10-13 13:21:08 -04007555 BTRFS_I(inode)->last_trans = root->fs_info->generation;
7556 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06007557 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04007558
Josef Bacik2ac55d42010-02-03 19:33:23 +00007559 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04007560
7561out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02007562 if (!ret) {
7563 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04007564 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02007565 }
Chris Mason9ebefb182007-06-15 13:50:00 -04007566 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05007567out:
Josef Bacikec39e182012-01-12 19:10:12 -05007568 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007569out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02007570 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04007571 return ret;
7572}
7573
Josef Bacika41ad392011-01-31 15:30:16 -05007574static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04007575{
7576 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04007577 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04007578 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05007579 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007580 struct btrfs_trans_handle *trans;
Chris Masondbe674a2008-07-17 12:54:05 -04007581 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04007582 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04007583
Josef Bacik0ef8b722013-10-25 16:13:35 -04007584 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
7585 (u64)-1);
7586 if (ret)
7587 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04007588
Josef Bacikfcb80c22011-05-03 10:40:22 -04007589 /*
7590 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
7591 * 3 things going on here
7592 *
7593 * 1) We need to reserve space for our orphan item and the space to
7594 * delete our orphan item. Lord knows we don't want to have a dangling
7595 * orphan item because we didn't reserve space to remove it.
7596 *
7597 * 2) We need to reserve space to update our inode.
7598 *
7599 * 3) We need to have something to cache all the space that is going to
7600 * be free'd up by the truncate operation, but also have some slack
7601 * space reserved in case it uses space during the truncate (thank you
7602 * very much snapshotting).
7603 *
7604 * And we need these to all be seperate. The fact is we can use alot of
7605 * space doing the truncate, and we have no earthly idea how much space
7606 * we will use, so we need the truncate reservation to be seperate so it
7607 * doesn't end up using space reserved for updating the inode or
7608 * removing the orphan item. We also need to be able to stop the
7609 * transaction and start a new one, which means we need to be able to
7610 * update the inode several times, and we have no idea of knowing how
7611 * many times that will be, so we can't just reserve 1 item for the
7612 * entirety of the opration, so that has to be done seperately as well.
7613 * Then there is the orphan item, which does indeed need to be held on
7614 * to for the whole operation, and we need nobody to touch this reserved
7615 * space except the orphan code.
7616 *
7617 * So that leaves us with
7618 *
7619 * 1) root->orphan_block_rsv - for the orphan deletion.
7620 * 2) rsv - for the truncate reservation, which we will steal from the
7621 * transaction reservation.
7622 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
7623 * updating the inode.
7624 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06007625 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007626 if (!rsv)
7627 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04007628 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04007629 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05007630
Josef Bacik907cbce2011-08-08 13:46:15 -04007631 /*
Josef Bacik07127182011-08-19 10:29:59 -04007632 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04007633 * 1 for updating the inode.
7634 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05007635 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007636 if (IS_ERR(trans)) {
7637 err = PTR_ERR(trans);
7638 goto out;
7639 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05007640
Josef Bacik907cbce2011-08-08 13:46:15 -04007641 /* Migrate the slack space for the truncate to our reserve */
7642 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
7643 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007644 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05007645
Chris Mason5a3f23d2009-03-31 13:27:11 -04007646 /*
7647 * setattr is responsible for setting the ordered_data_close flag,
7648 * but that is only tested during the last file release. That
7649 * could happen well after the next commit, leaving a great big
7650 * window where new writes may get lost if someone chooses to write
7651 * to this file after truncating to zero
7652 *
7653 * The inode doesn't have any dirty data here, and so if we commit
7654 * this is a noop. If someone immediately starts writing to the inode
7655 * it is very likely we'll catch some of their writes in this
7656 * transaction, and the commit will find this file on the ordered
7657 * data list with good things to send down.
7658 *
7659 * This is a best effort solution, there is still a window where
7660 * using truncate to replace the contents of the file will
7661 * end up with a zero length file after a crash.
7662 */
Josef Bacik72ac3c02012-05-23 14:13:11 -04007663 if (inode->i_size == 0 && test_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
7664 &BTRFS_I(inode)->runtime_flags))
Chris Mason5a3f23d2009-03-31 13:27:11 -04007665 btrfs_add_ordered_operation(trans, root, inode);
7666
Josef Bacik5dc562c2012-08-17 13:14:17 -04007667 /*
7668 * So if we truncate and then write and fsync we normally would just
7669 * write the extents that changed, which is a problem if we need to
7670 * first truncate that entire inode. So set this flag so we write out
7671 * all of the extents in the inode to the sync log so we're completely
7672 * safe.
7673 */
7674 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007675 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04007676
Yan, Zheng80825102009-11-12 09:35:36 +00007677 while (1) {
7678 ret = btrfs_truncate_inode_items(trans, root, inode,
7679 inode->i_size,
7680 BTRFS_EXTENT_DATA_KEY);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007681 if (ret != -ENOSPC) {
Josef Bacik3893e332011-01-31 16:03:11 -05007682 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007683 break;
Josef Bacik3893e332011-01-31 16:03:11 -05007684 }
Chris Mason39279cc2007-06-12 06:35:45 -04007685
Josef Bacikfcb80c22011-05-03 10:40:22 -04007686 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007687 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007688 if (ret) {
7689 err = ret;
7690 break;
7691 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04007692
Yan, Zheng80825102009-11-12 09:35:36 +00007693 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007694 btrfs_btree_balance_dirty(root);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007695
7696 trans = btrfs_start_transaction(root, 2);
7697 if (IS_ERR(trans)) {
7698 ret = err = PTR_ERR(trans);
7699 trans = NULL;
7700 break;
7701 }
7702
7703 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
7704 rsv, min_size);
7705 BUG_ON(ret); /* shouldn't happen */
7706 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007707 }
7708
7709 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04007710 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007711 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007712 if (ret)
7713 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007714 }
7715
Chris Mason917c16b2011-11-08 14:49:59 -05007716 if (trans) {
7717 trans->block_rsv = &root->fs_info->trans_block_rsv;
7718 ret = btrfs_update_inode(trans, root, inode);
7719 if (ret && !err)
7720 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04007721
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007722 ret = btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007723 btrfs_btree_balance_dirty(root);
Chris Mason917c16b2011-11-08 14:49:59 -05007724 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04007725
7726out:
7727 btrfs_free_block_rsv(root, rsv);
7728
Josef Bacik3893e332011-01-31 16:03:11 -05007729 if (ret && !err)
7730 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05007731
Josef Bacik3893e332011-01-31 16:03:11 -05007732 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007733}
7734
Sven Wegener3b963622008-06-09 21:57:42 -04007735/*
Chris Masond352ac62008-09-29 15:18:18 -04007736 * create a new subvolume directory/inode (helper for the ioctl).
7737 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05007738int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Josef Bacikd82a6f12011-05-11 15:26:06 -04007739 struct btrfs_root *new_root, u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04007740{
Chris Mason39279cc2007-06-12 06:35:45 -04007741 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04007742 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007743 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007744
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01007745 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
7746 new_dirid, new_dirid,
7747 S_IFDIR | (~current_umask() & S_IRWXUGO),
7748 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04007749 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04007750 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007751 inode->i_op = &btrfs_dir_inode_operations;
7752 inode->i_fop = &btrfs_dir_file_operations;
7753
Miklos Szeredibfe86842011-10-28 14:13:29 +02007754 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04007755 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04007756
Yan, Zheng76dda932009-09-21 16:00:26 -04007757 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04007758
Yan, Zheng76dda932009-09-21 16:00:26 -04007759 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07007760 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007761}
7762
Chris Mason39279cc2007-06-12 06:35:45 -04007763struct inode *btrfs_alloc_inode(struct super_block *sb)
7764{
7765 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007766 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007767
7768 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
7769 if (!ei)
7770 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007771
7772 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007773 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04007774 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04007775 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04007776 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007777 ei->delalloc_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007778 ei->disk_i_size = 0;
7779 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04007780 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007781 ei->index_cnt = (u64)-1;
7782 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06007783 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007784
Josef Bacik9e0baf62011-07-15 15:16:44 +00007785 spin_lock_init(&ei->lock);
7786 ei->outstanding_extents = 0;
7787 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007788
Josef Bacik72ac3c02012-05-23 14:13:11 -04007789 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08007790 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007791
Miao Xie16cdcec2011-04-22 18:12:22 +08007792 ei->delayed_node = NULL;
7793
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007794 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02007795 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02007796 extent_io_tree_init(&ei->io_tree, &inode->i_data);
7797 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04007798 ei->io_tree.track_uptodate = 1;
7799 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05007800 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007801 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05007802 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007803 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007804 INIT_LIST_HEAD(&ei->delalloc_inodes);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007805 INIT_LIST_HEAD(&ei->ordered_operations);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007806 RB_CLEAR_NODE(&ei->rb_node);
7807
7808 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007809}
7810
Josef Bacikaaedb552013-10-11 14:44:09 -04007811#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
7812void btrfs_test_destroy_inode(struct inode *inode)
7813{
7814 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
7815 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
7816}
7817#endif
7818
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007819static void btrfs_i_callback(struct rcu_head *head)
7820{
7821 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007822 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
7823}
7824
Chris Mason39279cc2007-06-12 06:35:45 -04007825void btrfs_destroy_inode(struct inode *inode)
7826{
Chris Masone6dcd2d2008-07-17 12:53:50 -04007827 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007828 struct btrfs_root *root = BTRFS_I(inode)->root;
7829
Al Virob3d9b7a2012-06-09 13:51:19 -04007830 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04007831 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00007832 WARN_ON(BTRFS_I(inode)->outstanding_extents);
7833 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04007834 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
7835 WARN_ON(BTRFS_I(inode)->csum_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04007836
Chris Mason5a3f23d2009-03-31 13:27:11 -04007837 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05007838 * This can happen where we create an inode, but somebody else also
7839 * created the same inode and we need to destroy the one we already
7840 * created.
7841 */
7842 if (!root)
7843 goto free;
7844
7845 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04007846 * Make sure we're properly removed from the ordered operation
7847 * lists.
7848 */
7849 smp_mb();
7850 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
Miao Xie199c2a92013-05-15 07:48:23 +00007851 spin_lock(&root->fs_info->ordered_root_lock);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007852 list_del_init(&BTRFS_I(inode)->ordered_operations);
Miao Xie199c2a92013-05-15 07:48:23 +00007853 spin_unlock(&root->fs_info->ordered_root_lock);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007854 }
7855
Josef Bacik8a35d952012-05-23 14:26:42 -04007856 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
7857 &BTRFS_I(inode)->runtime_flags)) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00007858 btrfs_info(root->fs_info, "inode %llu still on the orphan list",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02007859 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04007860 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04007861 }
Josef Bacik7b128762008-07-24 12:17:14 -04007862
Chris Masond3977122009-01-05 21:25:51 -05007863 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04007864 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
7865 if (!ordered)
7866 break;
7867 else {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00007868 btrfs_err(root->fs_info, "found ordered extent %llu %llu on inode cleanup",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02007869 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007870 btrfs_remove_ordered_extent(inode, ordered);
7871 btrfs_put_ordered_extent(ordered);
7872 btrfs_put_ordered_extent(ordered);
7873 }
7874 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007875 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007876 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05007877free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007878 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04007879}
7880
Al Viro45321ac2010-06-07 13:43:19 -04007881int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04007882{
7883 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04007884
Naohiro Aota6379ef92013-06-06 09:56:34 +00007885 if (root == NULL)
7886 return 1;
7887
Liu Bofa6ac872013-02-20 14:10:23 +00007888 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02007889 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04007890 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04007891 else
Al Viro45321ac2010-06-07 13:43:19 -04007892 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04007893}
7894
Sven Wegener0ee0fda2008-07-30 16:54:26 -04007895static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04007896{
7897 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
7898
7899 inode_init_once(&ei->vfs_inode);
7900}
7901
7902void btrfs_destroy_cachep(void)
7903{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10007904 /*
7905 * Make sure all delayed rcu free inodes are flushed before we
7906 * destroy cache.
7907 */
7908 rcu_barrier();
Chris Mason39279cc2007-06-12 06:35:45 -04007909 if (btrfs_inode_cachep)
7910 kmem_cache_destroy(btrfs_inode_cachep);
7911 if (btrfs_trans_handle_cachep)
7912 kmem_cache_destroy(btrfs_trans_handle_cachep);
7913 if (btrfs_transaction_cachep)
7914 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04007915 if (btrfs_path_cachep)
7916 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05007917 if (btrfs_free_space_cachep)
7918 kmem_cache_destroy(btrfs_free_space_cachep);
Miao Xie8ccf6f192012-10-25 09:28:04 +00007919 if (btrfs_delalloc_work_cachep)
7920 kmem_cache_destroy(btrfs_delalloc_work_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04007921}
7922
7923int btrfs_init_cachep(void)
7924{
David Sterba837e1972012-09-07 03:00:48 -06007925 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007926 sizeof(struct btrfs_inode), 0,
7927 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04007928 if (!btrfs_inode_cachep)
7929 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007930
David Sterba837e1972012-09-07 03:00:48 -06007931 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007932 sizeof(struct btrfs_trans_handle), 0,
7933 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007934 if (!btrfs_trans_handle_cachep)
7935 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007936
David Sterba837e1972012-09-07 03:00:48 -06007937 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007938 sizeof(struct btrfs_transaction), 0,
7939 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007940 if (!btrfs_transaction_cachep)
7941 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007942
David Sterba837e1972012-09-07 03:00:48 -06007943 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007944 sizeof(struct btrfs_path), 0,
7945 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007946 if (!btrfs_path_cachep)
7947 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007948
David Sterba837e1972012-09-07 03:00:48 -06007949 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05007950 sizeof(struct btrfs_free_space), 0,
7951 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
7952 if (!btrfs_free_space_cachep)
7953 goto fail;
7954
Miao Xie8ccf6f192012-10-25 09:28:04 +00007955 btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
7956 sizeof(struct btrfs_delalloc_work), 0,
7957 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
7958 NULL);
7959 if (!btrfs_delalloc_work_cachep)
7960 goto fail;
7961
Chris Mason39279cc2007-06-12 06:35:45 -04007962 return 0;
7963fail:
7964 btrfs_destroy_cachep();
7965 return -ENOMEM;
7966}
7967
7968static int btrfs_getattr(struct vfsmount *mnt,
7969 struct dentry *dentry, struct kstat *stat)
7970{
Miao Xiedf0af1a2013-01-29 10:11:59 +00007971 u64 delalloc_bytes;
Chris Mason39279cc2007-06-12 06:35:45 -04007972 struct inode *inode = dentry->d_inode;
David Sterbafadc0d82011-11-20 07:33:38 -05007973 u32 blocksize = inode->i_sb->s_blocksize;
7974
Chris Mason39279cc2007-06-12 06:35:45 -04007975 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04007976 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05007977 stat->blksize = PAGE_CACHE_SIZE;
Miao Xiedf0af1a2013-01-29 10:11:59 +00007978
7979 spin_lock(&BTRFS_I(inode)->lock);
7980 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
7981 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05007982 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00007983 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04007984 return 0;
7985}
7986
Chris Masond3977122009-01-05 21:25:51 -05007987static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7988 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04007989{
7990 struct btrfs_trans_handle *trans;
7991 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007992 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04007993 struct inode *new_inode = new_dentry->d_inode;
7994 struct inode *old_inode = old_dentry->d_inode;
7995 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007996 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007997 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04007998 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08007999 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008000
Li Zefan33345d012011-04-20 10:31:50 +08008001 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04008002 return -EPERM;
8003
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008004 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08008005 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05008006 return -EXDEV;
8007
Li Zefan33345d012011-04-20 10:31:50 +08008008 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
8009 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008010 return -ENOTEMPTY;
8011
Chris Mason39279cc2007-06-12 06:35:45 -04008012 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008013 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04008014 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05008015
8016
8017 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04008018 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05008019 new_dentry->d_name.name,
8020 new_dentry->d_name.len);
8021
8022 if (ret) {
8023 if (ret == -EEXIST) {
8024 /* we shouldn't get
8025 * eexist without a new_inode */
8026 if (!new_inode) {
8027 WARN_ON(1);
8028 return ret;
8029 }
8030 } else {
8031 /* maybe -EOVERFLOW */
8032 return ret;
8033 }
8034 }
8035 ret = 0;
8036
Chris Mason5a3f23d2009-03-31 13:27:11 -04008037 /*
8038 * we're using rename to replace one file with another.
8039 * and the replacement file is large. Start IO on it now so
8040 * we don't add too much work to the end of the transaction
8041 */
Bartlomiej Zolnierkiewicz4baf8c92009-08-07 13:47:08 -04008042 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
Chris Mason5a3f23d2009-03-31 13:27:11 -04008043 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
8044 filemap_flush(old_inode->i_mapping);
8045
Yan, Zheng76dda932009-09-21 16:00:26 -04008046 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08008047 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008048 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008049 /*
8050 * We want to reserve the absolute worst case amount of items. So if
8051 * both inodes are subvols and we need to unlink them then that would
8052 * require 4 item modifications, but if they are both normal inodes it
8053 * would require 5 item modifications, so we'll assume their normal
8054 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
8055 * should cover the worst case number of items we'll modify.
8056 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04008057 trans = btrfs_start_transaction(root, 11);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008058 if (IS_ERR(trans)) {
8059 ret = PTR_ERR(trans);
8060 goto out_notrans;
8061 }
Chris Mason5f39d392007-10-15 16:14:19 -04008062
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008063 if (dest != root)
8064 btrfs_record_root_in_trans(trans, dest);
8065
Yan, Zhenga5719522009-09-24 09:17:31 -04008066 ret = btrfs_set_inode_index(new_dir, &index);
8067 if (ret)
8068 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008069
Li Zefan33345d012011-04-20 10:31:50 +08008070 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008071 /* force full log commit if subvolume involved. */
8072 root->fs_info->last_trans_log_full_commit = trans->transid;
8073 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04008074 ret = btrfs_insert_inode_ref(trans, dest,
8075 new_dentry->d_name.name,
8076 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08008077 old_ino,
8078 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04008079 if (ret)
8080 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008081 /*
8082 * this is an ugly little race, but the rename is required
8083 * to make sure that if we crash, the inode is either at the
8084 * old name or the new one. pinning the log transaction lets
8085 * us make sure we don't allow a log commit to come in after
8086 * we unlink the name but before we add the new name back in.
8087 */
8088 btrfs_pin_log_trans(root);
8089 }
Chris Mason12fcfd22009-03-24 10:24:20 -04008090 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04008091 * make sure the inode gets flushed if it is replacing
8092 * something.
8093 */
Li Zefan33345d012011-04-20 10:31:50 +08008094 if (new_inode && new_inode->i_size && S_ISREG(old_inode->i_mode))
Chris Mason5a3f23d2009-03-31 13:27:11 -04008095 btrfs_add_ordered_operation(trans, root, old_inode);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008096
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008097 inode_inc_iversion(old_dir);
8098 inode_inc_iversion(new_dir);
8099 inode_inc_iversion(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008100 old_dir->i_ctime = old_dir->i_mtime = ctime;
8101 new_dir->i_ctime = new_dir->i_mtime = ctime;
8102 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04008103
Chris Mason12fcfd22009-03-24 10:24:20 -04008104 if (old_dentry->d_parent != new_dentry->d_parent)
8105 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
8106
Li Zefan33345d012011-04-20 10:31:50 +08008107 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008108 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
8109 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
8110 old_dentry->d_name.name,
8111 old_dentry->d_name.len);
8112 } else {
Al Viro92986792011-03-04 17:14:37 +00008113 ret = __btrfs_unlink_inode(trans, root, old_dir,
8114 old_dentry->d_inode,
8115 old_dentry->d_name.name,
8116 old_dentry->d_name.len);
8117 if (!ret)
8118 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008119 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008120 if (ret) {
8121 btrfs_abort_transaction(trans, root, ret);
8122 goto out_fail;
8123 }
Chris Mason39279cc2007-06-12 06:35:45 -04008124
8125 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008126 inode_inc_iversion(new_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008127 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08008128 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008129 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
8130 root_objectid = BTRFS_I(new_inode)->location.objectid;
8131 ret = btrfs_unlink_subvol(trans, dest, new_dir,
8132 root_objectid,
8133 new_dentry->d_name.name,
8134 new_dentry->d_name.len);
8135 BUG_ON(new_inode->i_nlink == 0);
8136 } else {
8137 ret = btrfs_unlink_inode(trans, dest, new_dir,
8138 new_dentry->d_inode,
8139 new_dentry->d_name.name,
8140 new_dentry->d_name.len);
8141 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04008142 if (!ret && new_inode->i_nlink == 0)
Chris Masone02119d2008-09-05 16:13:11 -04008143 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008144 if (ret) {
8145 btrfs_abort_transaction(trans, root, ret);
8146 goto out_fail;
8147 }
Chris Mason39279cc2007-06-12 06:35:45 -04008148 }
Josef Bacikaec74772008-07-24 12:12:38 -04008149
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008150 ret = btrfs_add_link(trans, new_dir, old_inode,
8151 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04008152 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008153 if (ret) {
8154 btrfs_abort_transaction(trans, root, ret);
8155 goto out_fail;
8156 }
Chris Mason39279cc2007-06-12 06:35:45 -04008157
Li Zefan33345d012011-04-20 10:31:50 +08008158 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04008159 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00008160 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008161 btrfs_end_log_trans(root);
8162 }
Chris Mason39279cc2007-06-12 06:35:45 -04008163out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008164 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008165out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08008166 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008167 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008168
Chris Mason39279cc2007-06-12 06:35:45 -04008169 return ret;
8170}
8171
Miao Xie8ccf6f192012-10-25 09:28:04 +00008172static void btrfs_run_delalloc_work(struct btrfs_work *work)
8173{
8174 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04008175 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008176
8177 delalloc_work = container_of(work, struct btrfs_delalloc_work,
8178 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04008179 inode = delalloc_work->inode;
8180 if (delalloc_work->wait) {
8181 btrfs_wait_ordered_range(inode, 0, (u64)-1);
8182 } else {
8183 filemap_flush(inode->i_mapping);
8184 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8185 &BTRFS_I(inode)->runtime_flags))
8186 filemap_flush(inode->i_mapping);
8187 }
Miao Xie8ccf6f192012-10-25 09:28:04 +00008188
8189 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -04008190 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008191 else
Josef Bacik9f23e282013-10-28 15:03:41 -04008192 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008193 complete(&delalloc_work->completion);
8194}
8195
8196struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
8197 int wait, int delay_iput)
8198{
8199 struct btrfs_delalloc_work *work;
8200
8201 work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
8202 if (!work)
8203 return NULL;
8204
8205 init_completion(&work->completion);
8206 INIT_LIST_HEAD(&work->list);
8207 work->inode = inode;
8208 work->wait = wait;
8209 work->delay_iput = delay_iput;
8210 work->work.func = btrfs_run_delalloc_work;
8211
8212 return work;
8213}
8214
8215void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
8216{
8217 wait_for_completion(&work->completion);
8218 kmem_cache_free(btrfs_delalloc_work_cachep, work);
8219}
8220
Chris Masond352ac62008-09-29 15:18:18 -04008221/*
8222 * some fairly slow code that needs optimization. This walks the list
8223 * of all the inodes with pending delalloc and forces them to disk.
8224 */
Miao Xieeb73c1b2013-05-15 07:48:22 +00008225static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
Chris Masonea8c2812008-08-04 23:17:27 -04008226{
Chris Masonea8c2812008-08-04 23:17:27 -04008227 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008228 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008229 struct btrfs_delalloc_work *work, *next;
8230 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00008231 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008232 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04008233
Miao Xie8ccf6f192012-10-25 09:28:04 +00008234 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008235 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00008236
Miao Xieeb73c1b2013-05-15 07:48:22 +00008237 spin_lock(&root->delalloc_lock);
8238 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008239 while (!list_empty(&splice)) {
8240 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04008241 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008242
Miao Xieeb73c1b2013-05-15 07:48:22 +00008243 list_move_tail(&binode->delalloc_inodes,
8244 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008245 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00008246 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00008247 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008248 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008249 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008250 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008251
8252 work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
8253 if (unlikely(!work)) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -04008254 if (delay_iput)
8255 btrfs_add_delayed_iput(inode);
8256 else
8257 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008258 ret = -ENOMEM;
8259 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008260 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008261 list_add_tail(&work->list, &works);
8262 btrfs_queue_worker(&root->fs_info->flush_workers,
8263 &work->work);
8264
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008265 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00008266 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04008267 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008268 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04008269
Miao Xie1eafa6c2013-01-22 10:49:00 +00008270 list_for_each_entry_safe(work, next, &works, list) {
8271 list_del_init(&work->list);
8272 btrfs_wait_and_free_delalloc_work(work);
8273 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008274 return 0;
8275out:
8276 list_for_each_entry_safe(work, next, &works, list) {
8277 list_del_init(&work->list);
8278 btrfs_wait_and_free_delalloc_work(work);
8279 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008280
Miao Xieeb73c1b2013-05-15 07:48:22 +00008281 if (!list_empty_careful(&splice)) {
8282 spin_lock(&root->delalloc_lock);
8283 list_splice_tail(&splice, &root->delalloc_inodes);
8284 spin_unlock(&root->delalloc_lock);
8285 }
8286 return ret;
8287}
8288
8289int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
8290{
8291 int ret;
8292
8293 if (root->fs_info->sb->s_flags & MS_RDONLY)
8294 return -EROFS;
8295
8296 ret = __start_delalloc_inodes(root, delay_iput);
8297 /*
8298 * the filemap_flush will queue IO into the worker threads, but
Chris Mason8c8bee12008-09-29 11:19:10 -04008299 * we have to make sure the IO is actually started and that
8300 * ordered extents get created before we return
8301 */
8302 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05008303 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05008304 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04008305 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05008306 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
8307 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04008308 }
8309 atomic_dec(&root->fs_info->async_submit_draining);
Miao Xieeb73c1b2013-05-15 07:48:22 +00008310 return ret;
8311}
8312
8313int btrfs_start_all_delalloc_inodes(struct btrfs_fs_info *fs_info,
8314 int delay_iput)
8315{
8316 struct btrfs_root *root;
8317 struct list_head splice;
8318 int ret;
8319
8320 if (fs_info->sb->s_flags & MS_RDONLY)
8321 return -EROFS;
8322
8323 INIT_LIST_HEAD(&splice);
8324
8325 spin_lock(&fs_info->delalloc_root_lock);
8326 list_splice_init(&fs_info->delalloc_roots, &splice);
8327 while (!list_empty(&splice)) {
8328 root = list_first_entry(&splice, struct btrfs_root,
8329 delalloc_root);
8330 root = btrfs_grab_fs_root(root);
8331 BUG_ON(!root);
8332 list_move_tail(&root->delalloc_root,
8333 &fs_info->delalloc_roots);
8334 spin_unlock(&fs_info->delalloc_root_lock);
8335
8336 ret = __start_delalloc_inodes(root, delay_iput);
8337 btrfs_put_fs_root(root);
8338 if (ret)
8339 goto out;
8340
8341 spin_lock(&fs_info->delalloc_root_lock);
8342 }
8343 spin_unlock(&fs_info->delalloc_root_lock);
8344
8345 atomic_inc(&fs_info->async_submit_draining);
8346 while (atomic_read(&fs_info->nr_async_submits) ||
8347 atomic_read(&fs_info->async_delalloc_pages)) {
8348 wait_event(fs_info->async_submit_wait,
8349 (atomic_read(&fs_info->nr_async_submits) == 0 &&
8350 atomic_read(&fs_info->async_delalloc_pages) == 0));
8351 }
8352 atomic_dec(&fs_info->async_submit_draining);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008353 return 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008354out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00008355 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00008356 spin_lock(&fs_info->delalloc_root_lock);
8357 list_splice_tail(&splice, &fs_info->delalloc_roots);
8358 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008359 }
Miao Xie8ccf6f192012-10-25 09:28:04 +00008360 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04008361}
8362
Chris Mason39279cc2007-06-12 06:35:45 -04008363static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
8364 const char *symname)
8365{
8366 struct btrfs_trans_handle *trans;
8367 struct btrfs_root *root = BTRFS_I(dir)->root;
8368 struct btrfs_path *path;
8369 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05008370 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04008371 int err;
8372 int drop_inode = 0;
8373 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008374 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04008375 int name_len;
8376 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04008377 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04008378 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04008379 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04008380
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01008381 name_len = strlen(symname);
Chris Mason39279cc2007-06-12 06:35:45 -04008382 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
8383 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05008384
Josef Bacik9ed74f22009-09-11 16:12:44 -04008385 /*
8386 * 2 items for inode item and ref
8387 * 2 items for dir items
8388 * 1 item for xattr if selinux is on
8389 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04008390 trans = btrfs_start_transaction(root, 5);
8391 if (IS_ERR(trans))
8392 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05008393
Li Zefan581bb052011-04-20 10:06:11 +08008394 err = btrfs_find_free_ino(root, &objectid);
8395 if (err)
8396 goto out_unlock;
8397
Josef Bacikaec74772008-07-24 12:12:38 -04008398 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08008399 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04008400 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008401 if (IS_ERR(inode)) {
8402 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008403 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008404 }
Chris Mason39279cc2007-06-12 06:35:45 -04008405
Eric Paris2a7dba32011-02-01 11:05:39 -05008406 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04008407 if (err) {
8408 drop_inode = 1;
8409 goto out_unlock;
8410 }
8411
Casey Schauflerad19db72011-12-15 10:09:07 -05008412 /*
8413 * If the active LSM wants to access the inode during
8414 * d_instantiate it needs these. Smack checks to see
8415 * if the filesystem supports xattrs by looking at the
8416 * ops vector.
8417 */
8418 inode->i_fop = &btrfs_file_operations;
8419 inode->i_op = &btrfs_file_inode_operations;
8420
Josef Bacika1b075d2010-11-19 20:36:11 +00008421 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04008422 if (err)
8423 drop_inode = 1;
8424 else {
8425 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04008426 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05008427 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04008428 }
Chris Mason39279cc2007-06-12 06:35:45 -04008429 if (drop_inode)
8430 goto out_unlock;
8431
8432 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07008433 if (!path) {
8434 err = -ENOMEM;
8435 drop_inode = 1;
8436 goto out_unlock;
8437 }
Li Zefan33345d012011-04-20 10:31:50 +08008438 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008439 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008440 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
8441 datasize = btrfs_file_extent_calc_inline_size(name_len);
8442 err = btrfs_insert_empty_item(trans, root, path, &key,
8443 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04008444 if (err) {
8445 drop_inode = 1;
Julia Lawallb0839162011-05-14 07:10:51 +00008446 btrfs_free_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04008447 goto out_unlock;
8448 }
Chris Mason5f39d392007-10-15 16:14:19 -04008449 leaf = path->nodes[0];
8450 ei = btrfs_item_ptr(leaf, path->slots[0],
8451 struct btrfs_file_extent_item);
8452 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
8453 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04008454 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04008455 btrfs_set_file_extent_encryption(leaf, ei, 0);
8456 btrfs_set_file_extent_compression(leaf, ei, 0);
8457 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
8458 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
8459
Chris Mason39279cc2007-06-12 06:35:45 -04008460 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04008461 write_extent_buffer(leaf, symname, ptr, name_len);
8462 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04008463 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04008464
Chris Mason39279cc2007-06-12 06:35:45 -04008465 inode->i_op = &btrfs_symlink_inode_operations;
8466 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04008467 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04008468 inode_set_bytes(inode, name_len);
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01008469 btrfs_i_size_write(inode, name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -04008470 err = btrfs_update_inode(trans, root, inode);
8471 if (err)
8472 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04008473
8474out_unlock:
Al Viro08c422c2011-12-23 07:58:13 -05008475 if (!err)
8476 d_instantiate(dentry, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008477 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04008478 if (drop_inode) {
8479 inode_dec_link_count(inode);
8480 iput(inode);
8481 }
Liu Bob53d3f52012-11-14 14:34:34 +00008482 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04008483 return err;
8484}
Chris Mason16432982008-04-10 10:23:21 -04008485
Josef Bacik0af3d002010-06-21 14:48:16 -04008486static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
8487 u64 start, u64 num_bytes, u64 min_size,
8488 loff_t actual_len, u64 *alloc_hint,
8489 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04008490{
Josef Bacik5dc562c2012-08-17 13:14:17 -04008491 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
8492 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04008493 struct btrfs_root *root = BTRFS_I(inode)->root;
8494 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04008495 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00008496 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05008497 u64 cur_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04008498 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04008499 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04008500
Josef Bacik0af3d002010-06-21 14:48:16 -04008501 if (trans)
8502 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04008503 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008504 if (own_trans) {
8505 trans = btrfs_start_transaction(root, 3);
8506 if (IS_ERR(trans)) {
8507 ret = PTR_ERR(trans);
8508 break;
8509 }
Yan Zhengd899e052008-10-30 14:25:28 -04008510 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00008511
Chris Mason154ea282013-03-05 11:11:26 -05008512 cur_bytes = min(num_bytes, 256ULL * 1024 * 1024);
8513 cur_bytes = max(cur_bytes, min_size);
Josef Bacik00361582013-08-14 14:02:47 -04008514 ret = btrfs_reserve_extent(root, cur_bytes, min_size, 0,
8515 *alloc_hint, &ins, 1);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008516 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008517 if (own_trans)
8518 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008519 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00008520 }
8521
Yan Zhengd899e052008-10-30 14:25:28 -04008522 ret = insert_reserved_file_extent(trans, inode,
8523 cur_offset, ins.objectid,
8524 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00008525 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04008526 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008527 if (ret) {
Josef Bacik857cc2f2013-10-07 15:21:08 -04008528 btrfs_free_reserved_extent(root, ins.objectid,
8529 ins.offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008530 btrfs_abort_transaction(trans, root, ret);
8531 if (own_trans)
8532 btrfs_end_transaction(trans, root);
8533 break;
8534 }
Chris Masona1ed8352009-09-11 12:27:37 -04008535 btrfs_drop_extent_cache(inode, cur_offset,
8536 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008537
Josef Bacik5dc562c2012-08-17 13:14:17 -04008538 em = alloc_extent_map();
8539 if (!em) {
8540 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
8541 &BTRFS_I(inode)->runtime_flags);
8542 goto next;
8543 }
8544
8545 em->start = cur_offset;
8546 em->orig_start = cur_offset;
8547 em->len = ins.offset;
8548 em->block_start = ins.objectid;
8549 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05008550 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04008551 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008552 em->bdev = root->fs_info->fs_devices->latest_bdev;
8553 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
8554 em->generation = trans->transid;
8555
8556 while (1) {
8557 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04008558 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04008559 write_unlock(&em_tree->lock);
8560 if (ret != -EEXIST)
8561 break;
8562 btrfs_drop_extent_cache(inode, cur_offset,
8563 cur_offset + ins.offset - 1,
8564 0);
8565 }
8566 free_extent_map(em);
8567next:
Yan Zhengd899e052008-10-30 14:25:28 -04008568 num_bytes -= ins.offset;
8569 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04008570 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00008571
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008572 inode_inc_iversion(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04008573 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02008574 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04008575 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04008576 (actual_len > inode->i_size) &&
8577 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00008578 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00008579 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00008580 else
Josef Bacik55a61d12010-11-22 18:50:32 +00008581 i_size = cur_offset;
8582 i_size_write(inode, i_size);
8583 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008584 }
8585
Yan Zhengd899e052008-10-30 14:25:28 -04008586 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008587
8588 if (ret) {
8589 btrfs_abort_transaction(trans, root, ret);
8590 if (own_trans)
8591 btrfs_end_transaction(trans, root);
8592 break;
8593 }
Yan Zhengd899e052008-10-30 14:25:28 -04008594
Josef Bacik0af3d002010-06-21 14:48:16 -04008595 if (own_trans)
8596 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008597 }
Yan Zhengd899e052008-10-30 14:25:28 -04008598 return ret;
8599}
8600
Josef Bacik0af3d002010-06-21 14:48:16 -04008601int btrfs_prealloc_file_range(struct inode *inode, int mode,
8602 u64 start, u64 num_bytes, u64 min_size,
8603 loff_t actual_len, u64 *alloc_hint)
8604{
8605 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
8606 min_size, actual_len, alloc_hint,
8607 NULL);
8608}
8609
8610int btrfs_prealloc_file_range_trans(struct inode *inode,
8611 struct btrfs_trans_handle *trans, int mode,
8612 u64 start, u64 num_bytes, u64 min_size,
8613 loff_t actual_len, u64 *alloc_hint)
8614{
8615 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
8616 min_size, actual_len, alloc_hint, trans);
8617}
8618
Chris Masone6dcd2d2008-07-17 12:53:50 -04008619static int btrfs_set_page_dirty(struct page *page)
8620{
Chris Masone6dcd2d2008-07-17 12:53:50 -04008621 return __set_page_dirty_nobuffers(page);
8622}
8623
Al Viro10556cb2011-06-20 19:28:19 -04008624static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05008625{
Li Zefanb83cc962010-12-20 16:04:08 +08008626 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00008627 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08008628
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00008629 if (mask & MAY_WRITE &&
8630 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
8631 if (btrfs_root_readonly(root))
8632 return -EROFS;
8633 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
8634 return -EACCES;
8635 }
Al Viro2830ba72011-06-20 19:16:29 -04008636 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05008637}
Chris Mason39279cc2007-06-12 06:35:45 -04008638
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008639static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05008640 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04008641 .lookup = btrfs_lookup,
8642 .create = btrfs_create,
8643 .unlink = btrfs_unlink,
8644 .link = btrfs_link,
8645 .mkdir = btrfs_mkdir,
8646 .rmdir = btrfs_rmdir,
8647 .rename = btrfs_rename,
8648 .symlink = btrfs_symlink,
8649 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04008650 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008651 .setxattr = btrfs_setxattr,
8652 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05008653 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008654 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05008655 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008656 .get_acl = btrfs_get_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -07008657 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008658};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008659static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008660 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05008661 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008662 .get_acl = btrfs_get_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -07008663 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008664};
Yan, Zheng76dda932009-09-21 16:00:26 -04008665
Alexey Dobriyan828c0952009-10-01 15:43:56 -07008666static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008667 .llseek = generic_file_llseek,
8668 .read = generic_read_dir,
Al Viro9cdda8d2013-05-22 16:48:09 -04008669 .iterate = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04008670 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04008671#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04008672 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04008673#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04008674 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04008675 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04008676};
8677
Chris Masond1310b22008-01-24 16:13:08 -05008678static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04008679 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05008680 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04008681 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04008682 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008683 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04008684 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05008685 .set_bit_hook = btrfs_set_bit_hook,
8686 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04008687 .merge_extent_hook = btrfs_merge_extent_hook,
8688 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04008689};
8690
Chris Mason35054392009-01-21 13:11:13 -05008691/*
8692 * btrfs doesn't support the bmap operation because swapfiles
8693 * use bmap to make a mapping of extents in the file. They assume
8694 * these extents won't change over the life of the file and they
8695 * use the bmap result to do IO directly to the drive.
8696 *
8697 * the btrfs bmap call would return logical addresses that aren't
8698 * suitable for IO and they also will change frequently as COW
8699 * operations happen. So, swapfile + btrfs == corruption.
8700 *
8701 * For now we're avoiding this by dropping bmap.
8702 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008703static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008704 .readpage = btrfs_readpage,
8705 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04008706 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05008707 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04008708 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04008709 .invalidatepage = btrfs_invalidatepage,
8710 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008711 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02008712 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04008713};
8714
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008715static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008716 .readpage = btrfs_readpage,
8717 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04008718 .invalidatepage = btrfs_invalidatepage,
8719 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04008720};
8721
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008722static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008723 .getattr = btrfs_getattr,
8724 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008725 .setxattr = btrfs_setxattr,
8726 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05008727 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008728 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05008729 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008730 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008731 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008732 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008733};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008734static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04008735 .getattr = btrfs_getattr,
8736 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008737 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008738 .setxattr = btrfs_setxattr,
8739 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04008740 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008741 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008742 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008743 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -04008744};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008745static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008746 .readlink = generic_readlink,
8747 .follow_link = page_follow_link_light,
8748 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00008749 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -05008750 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008751 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05008752 .setxattr = btrfs_setxattr,
8753 .getxattr = btrfs_getxattr,
8754 .listxattr = btrfs_listxattr,
8755 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008756 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008757 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008758};
Yan, Zheng76dda932009-09-21 16:00:26 -04008759
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04008760const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04008761 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -04008762 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -04008763};