blob: a44ca6a4feffc1af73ef74813b38c849934a5fbf [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);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001183 if (ret < 0) {
1184 btrfs_abort_transaction(trans, root, ret);
1185 goto error;
1186 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001187 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1188 leaf = path->nodes[0];
1189 btrfs_item_key_to_cpu(leaf, &found_key,
1190 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001191 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001192 found_key.type == BTRFS_EXTENT_DATA_KEY)
1193 path->slots[0]--;
1194 }
1195 check_prev = 0;
1196next_slot:
1197 leaf = path->nodes[0];
1198 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1199 ret = btrfs_next_leaf(root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001200 if (ret < 0) {
1201 btrfs_abort_transaction(trans, root, ret);
1202 goto error;
1203 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001204 if (ret > 0)
1205 break;
1206 leaf = path->nodes[0];
1207 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001208
Yan Zheng80ff3852008-10-30 14:20:02 -04001209 nocow = 0;
1210 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001211 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001212 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001213
Li Zefan33345d012011-04-20 10:31:50 +08001214 if (found_key.objectid > ino ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001215 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1216 found_key.offset > end)
1217 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001218
Yan Zheng80ff3852008-10-30 14:20:02 -04001219 if (found_key.offset > cur_offset) {
1220 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001221 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001222 goto out_check;
1223 }
Chris Masonc31f8832008-01-08 15:46:31 -05001224
Yan Zheng80ff3852008-10-30 14:20:02 -04001225 fi = btrfs_item_ptr(leaf, path->slots[0],
1226 struct btrfs_file_extent_item);
1227 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001228
Josef Bacikcc95bef2013-04-04 14:31:27 -04001229 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001230 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1231 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001232 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001233 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001234 extent_end = found_key.offset +
1235 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001236 disk_num_bytes =
1237 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001238 if (extent_end <= start) {
1239 path->slots[0]++;
1240 goto next_slot;
1241 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001242 if (disk_bytenr == 0)
1243 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001244 if (btrfs_file_extent_compression(leaf, fi) ||
1245 btrfs_file_extent_encryption(leaf, fi) ||
1246 btrfs_file_extent_other_encoding(leaf, fi))
1247 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001248 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1249 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001250 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001251 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001252 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001253 found_key.offset -
1254 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001255 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001256 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001257 disk_bytenr += cur_offset - found_key.offset;
1258 num_bytes = min(end + 1, extent_end) - cur_offset;
1259 /*
1260 * force cow if csum exists in the range.
1261 * this ensure that csum for a given extent are
1262 * either valid or do not exist.
1263 */
1264 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1265 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001266 nocow = 1;
1267 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1268 extent_end = found_key.offset +
1269 btrfs_file_extent_inline_len(leaf, fi);
1270 extent_end = ALIGN(extent_end, root->sectorsize);
1271 } else {
1272 BUG_ON(1);
1273 }
1274out_check:
1275 if (extent_end <= start) {
1276 path->slots[0]++;
1277 goto next_slot;
1278 }
1279 if (!nocow) {
1280 if (cow_start == (u64)-1)
1281 cow_start = cur_offset;
1282 cur_offset = extent_end;
1283 if (cur_offset > end)
1284 break;
1285 path->slots[0]++;
1286 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001287 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001288
David Sterbab3b4aa72011-04-21 01:20:15 +02001289 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001290 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001291 ret = cow_file_range(inode, locked_page,
1292 cow_start, found_key.offset - 1,
1293 page_started, nr_written, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001294 if (ret) {
1295 btrfs_abort_transaction(trans, root, ret);
1296 goto error;
1297 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001298 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001299 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001300
Yan Zhengd899e052008-10-30 14:25:28 -04001301 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1302 struct extent_map *em;
1303 struct extent_map_tree *em_tree;
1304 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001305 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001306 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001307 em->start = cur_offset;
Josef Bacik70c8a912012-10-11 16:54:30 -04001308 em->orig_start = found_key.offset - extent_offset;
Yan Zhengd899e052008-10-30 14:25:28 -04001309 em->len = num_bytes;
1310 em->block_len = num_bytes;
1311 em->block_start = disk_bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05001312 em->orig_block_len = disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001313 em->ram_bytes = ram_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04001314 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001315 em->mod_start = em->start;
1316 em->mod_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04001317 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacikb11e2342012-12-03 10:58:15 -05001318 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001319 em->generation = -1;
Yan Zhengd899e052008-10-30 14:25:28 -04001320 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001321 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001322 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001323 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001324 if (ret != -EEXIST) {
1325 free_extent_map(em);
1326 break;
1327 }
1328 btrfs_drop_extent_cache(inode, em->start,
1329 em->start + em->len - 1, 0);
1330 }
1331 type = BTRFS_ORDERED_PREALLOC;
1332 } else {
1333 type = BTRFS_ORDERED_NOCOW;
1334 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001335
1336 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001337 num_bytes, num_bytes, type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001338 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001339
Yan, Zhengefa56462010-05-16 10:49:59 -04001340 if (root->root_key.objectid ==
1341 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1342 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1343 num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001344 if (ret) {
1345 btrfs_abort_transaction(trans, root, ret);
1346 goto error;
1347 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001348 }
1349
Josef Bacikc2790a22013-07-29 11:20:47 -04001350 extent_clear_unlock_delalloc(inode, cur_offset,
1351 cur_offset + num_bytes - 1,
1352 locked_page, EXTENT_LOCKED |
1353 EXTENT_DELALLOC, PAGE_UNLOCK |
1354 PAGE_SET_PRIVATE2);
Yan Zheng80ff3852008-10-30 14:20:02 -04001355 cur_offset = extent_end;
1356 if (cur_offset > end)
1357 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001358 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001359 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001360
Josef Bacik17ca04a2012-05-31 15:58:55 -04001361 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001362 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001363 cur_offset = end;
1364 }
1365
Yan Zheng80ff3852008-10-30 14:20:02 -04001366 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001367 ret = cow_file_range(inode, locked_page, cow_start, end,
1368 page_started, nr_written, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001369 if (ret) {
1370 btrfs_abort_transaction(trans, root, ret);
1371 goto error;
1372 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001373 }
1374
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001375error:
Miao Xiea698d0752012-09-20 01:51:59 -06001376 err = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001377 if (!ret)
1378 ret = err;
1379
Josef Bacik17ca04a2012-05-31 15:58:55 -04001380 if (ret && cur_offset < end)
Josef Bacikc2790a22013-07-29 11:20:47 -04001381 extent_clear_unlock_delalloc(inode, cur_offset, end,
1382 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001383 EXTENT_DELALLOC | EXTENT_DEFRAG |
1384 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1385 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001386 PAGE_SET_WRITEBACK |
1387 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001388 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001389 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001390}
1391
Chris Masond352ac62008-09-29 15:18:18 -04001392/*
1393 * extent_io.c call back to do delayed allocation processing
1394 */
Chris Masonc8b97812008-10-29 14:49:59 -04001395static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001396 u64 start, u64 end, int *page_started,
1397 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001398{
Chris Masonbe20aa92007-12-17 20:14:01 -05001399 int ret;
Chris Mason7f366cf2009-03-12 20:12:45 -04001400 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona2135012008-06-25 16:01:30 -04001401
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001402 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) {
Chris Masonc8b97812008-10-29 14:49:59 -04001403 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001404 page_started, 1, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001405 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001406 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001407 page_started, 0, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001408 } else if (!btrfs_test_opt(root, COMPRESS) &&
1409 !(BTRFS_I(inode)->force_compress) &&
1410 !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS)) {
Chris Mason7f366cf2009-03-12 20:12:45 -04001411 ret = cow_file_range(inode, locked_page, start, end,
1412 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001413 } else {
1414 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1415 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001416 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001417 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001418 }
Chris Masonb888db22007-08-27 16:49:44 -04001419 return ret;
1420}
1421
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001422static void btrfs_split_extent_hook(struct inode *inode,
1423 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001424{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001425 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001426 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001427 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001428
Josef Bacik9e0baf62011-07-15 15:16:44 +00001429 spin_lock(&BTRFS_I(inode)->lock);
1430 BTRFS_I(inode)->outstanding_extents++;
1431 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001432}
1433
1434/*
1435 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1436 * extents so we can keep track of new extents that are just merged onto old
1437 * extents, such as when we are doing sequential writes, so we can properly
1438 * account for the metadata space we'll need.
1439 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001440static void btrfs_merge_extent_hook(struct inode *inode,
1441 struct extent_state *new,
1442 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001443{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001444 /* not delalloc, ignore it */
1445 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001446 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001447
Josef Bacik9e0baf62011-07-15 15:16:44 +00001448 spin_lock(&BTRFS_I(inode)->lock);
1449 BTRFS_I(inode)->outstanding_extents--;
1450 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001451}
1452
Miao Xieeb73c1b2013-05-15 07:48:22 +00001453static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1454 struct inode *inode)
1455{
1456 spin_lock(&root->delalloc_lock);
1457 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1458 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1459 &root->delalloc_inodes);
1460 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1461 &BTRFS_I(inode)->runtime_flags);
1462 root->nr_delalloc_inodes++;
1463 if (root->nr_delalloc_inodes == 1) {
1464 spin_lock(&root->fs_info->delalloc_root_lock);
1465 BUG_ON(!list_empty(&root->delalloc_root));
1466 list_add_tail(&root->delalloc_root,
1467 &root->fs_info->delalloc_roots);
1468 spin_unlock(&root->fs_info->delalloc_root_lock);
1469 }
1470 }
1471 spin_unlock(&root->delalloc_lock);
1472}
1473
1474static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1475 struct inode *inode)
1476{
1477 spin_lock(&root->delalloc_lock);
1478 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1479 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1480 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1481 &BTRFS_I(inode)->runtime_flags);
1482 root->nr_delalloc_inodes--;
1483 if (!root->nr_delalloc_inodes) {
1484 spin_lock(&root->fs_info->delalloc_root_lock);
1485 BUG_ON(list_empty(&root->delalloc_root));
1486 list_del_init(&root->delalloc_root);
1487 spin_unlock(&root->fs_info->delalloc_root_lock);
1488 }
1489 }
1490 spin_unlock(&root->delalloc_lock);
1491}
1492
Chris Masond352ac62008-09-29 15:18:18 -04001493/*
1494 * extent_io.c set_bit_hook, used to track delayed allocation
1495 * bytes in this file, and to maintain the list of inodes that
1496 * have pending delalloc work to be done.
1497 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001498static void btrfs_set_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001499 struct extent_state *state, unsigned long *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001500{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001501
Chris Mason75eff682008-12-15 15:54:40 -05001502 /*
1503 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001504 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001505 * bit, which is only set or cleared with irqs on
1506 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001507 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001508 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001509 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001510 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001511
Josef Bacik9e0baf62011-07-15 15:16:44 +00001512 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001513 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001514 } else {
1515 spin_lock(&BTRFS_I(inode)->lock);
1516 BTRFS_I(inode)->outstanding_extents++;
1517 spin_unlock(&BTRFS_I(inode)->lock);
1518 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001519
Miao Xie963d6782013-01-29 10:10:51 +00001520 __percpu_counter_add(&root->fs_info->delalloc_bytes, len,
1521 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001522 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001523 BTRFS_I(inode)->delalloc_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001524 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001525 &BTRFS_I(inode)->runtime_flags))
1526 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001527 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001528 }
Chris Mason291d6732008-01-29 15:55:23 -05001529}
1530
Chris Masond352ac62008-09-29 15:18:18 -04001531/*
1532 * extent_io.c clear_bit_hook, see set_bit_hook for why
1533 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001534static void btrfs_clear_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001535 struct extent_state *state,
1536 unsigned long *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001537{
Chris Mason75eff682008-12-15 15:54:40 -05001538 /*
1539 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001540 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001541 * bit, which is only set or cleared with irqs on
1542 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001543 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001544 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001545 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001546 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001547
Josef Bacik9e0baf62011-07-15 15:16:44 +00001548 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001549 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001550 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1551 spin_lock(&BTRFS_I(inode)->lock);
1552 BTRFS_I(inode)->outstanding_extents--;
1553 spin_unlock(&BTRFS_I(inode)->lock);
1554 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001555
Josef Bacikb6d08f02013-09-27 14:57:43 -04001556 /*
1557 * We don't reserve metadata space for space cache inodes so we
1558 * don't need to call dellalloc_release_metadata if there is an
1559 * error.
1560 */
1561 if (*bits & EXTENT_DO_ACCOUNTING &&
1562 root != root->fs_info->tree_root)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001563 btrfs_delalloc_release_metadata(inode, len);
1564
Josef Bacik0cb59c92010-07-02 12:14:14 -04001565 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Josef Bacik7ee9e442013-06-21 16:37:03 -04001566 && do_list && !(state->state & EXTENT_NORESERVE))
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001567 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001568
Miao Xie963d6782013-01-29 10:10:51 +00001569 __percpu_counter_add(&root->fs_info->delalloc_bytes, -len,
1570 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001571 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001572 BTRFS_I(inode)->delalloc_bytes -= len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001573 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001574 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001575 &BTRFS_I(inode)->runtime_flags))
1576 btrfs_del_delalloc_inode(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001577 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001578 }
Chris Mason291d6732008-01-29 15:55:23 -05001579}
1580
Chris Masond352ac62008-09-29 15:18:18 -04001581/*
1582 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1583 * we don't create bios that span stripes or chunks
1584 */
David Woodhouse64a16702009-07-15 23:29:37 +01001585int btrfs_merge_bio_hook(int rw, struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001586 size_t size, struct bio *bio,
1587 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001588{
1589 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Chris Masona62b9402008-10-03 16:31:08 -04001590 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001591 u64 length = 0;
1592 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001593 int ret;
1594
Chris Mason771ed682008-11-06 22:02:51 -05001595 if (bio_flags & EXTENT_BIO_COMPRESSED)
1596 return 0;
1597
Chris Masonf2d8d742008-04-21 10:03:05 -04001598 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001599 map_length = length;
David Woodhouse64a16702009-07-15 23:29:37 +01001600 ret = btrfs_map_block(root->fs_info, rw, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001601 &map_length, NULL, 0);
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001602 /* Will always return 0 with map_multi == NULL */
Jeff Mahoney3444a972011-10-03 23:23:13 -04001603 BUG_ON(ret < 0);
Chris Masond3977122009-01-05 21:25:51 -05001604 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001605 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001606 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001607}
1608
Chris Masond352ac62008-09-29 15:18:18 -04001609/*
1610 * in order to insert checksums into the metadata in large chunks,
1611 * we wait until bio submission time. All the pages in the bio are
1612 * checksummed and sums are attached onto the ordered extent record.
1613 *
1614 * At IO completion time the cums attached on the ordered extent record
1615 * are inserted into the btree
1616 */
Chris Masond3977122009-01-05 21:25:51 -05001617static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1618 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001619 unsigned long bio_flags,
1620 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001621{
Chris Mason065631f2008-02-20 12:07:25 -05001622 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001623 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001624
Chris Masond20f7042008-12-08 16:58:54 -05001625 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001626 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001627 return 0;
1628}
Chris Masone0156402008-04-16 11:15:20 -04001629
Chris Mason4a69a412008-11-06 22:03:00 -05001630/*
1631 * in order to insert checksums into the metadata in large chunks,
1632 * we wait until bio submission time. All the pages in the bio are
1633 * checksummed and sums are attached onto the ordered extent record.
1634 *
1635 * At IO completion time the cums attached on the ordered extent record
1636 * are inserted into the btree
1637 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001638static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001639 int mirror_num, unsigned long bio_flags,
1640 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001641{
1642 struct btrfs_root *root = BTRFS_I(inode)->root;
Stefan Behrens61891922012-11-05 18:51:52 +01001643 int ret;
1644
1645 ret = btrfs_map_bio(root, rw, bio, mirror_num, 1);
1646 if (ret)
1647 bio_endio(bio, ret);
1648 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001649}
1650
Chris Masond352ac62008-09-29 15:18:18 -04001651/*
Chris Masoncad321a2008-12-17 14:51:42 -05001652 * extent_io.c submission hook. This does the right thing for csum calculation
1653 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001654 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001655static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001656 int mirror_num, unsigned long bio_flags,
1657 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001658{
1659 struct btrfs_root *root = BTRFS_I(inode)->root;
1660 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001661 int skip_sum;
Jeff Mahoney04173412011-10-03 23:23:12 -04001662 int metadata = 0;
Josef Bacikb812ce22012-11-16 13:56:32 -05001663 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001664
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001665 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001666
Liu Bo83eea1f2012-07-10 05:28:39 -06001667 if (btrfs_is_free_space_inode(inode))
Jeff Mahoney04173412011-10-03 23:23:12 -04001668 metadata = 2;
1669
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001670 if (!(rw & REQ_WRITE)) {
Josef Bacik5fd02042012-05-02 14:00:54 -04001671 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1672 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001673 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001674
Chris Masond20f7042008-12-08 16:58:54 -05001675 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001676 ret = btrfs_submit_compressed_read(inode, bio,
1677 mirror_num,
1678 bio_flags);
1679 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001680 } else if (!skip_sum) {
1681 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1682 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001683 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001684 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001685 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001686 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001687 /* csum items have already been cloned */
1688 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1689 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001690 /* we're doing a write, do the async checksumming */
Stefan Behrens61891922012-11-05 18:51:52 +01001691 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001692 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001693 bio_flags, bio_offset,
1694 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001695 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001696 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001697 } else if (!skip_sum) {
1698 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1699 if (ret)
1700 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001701 }
1702
Chris Mason0b86a832008-03-24 15:01:56 -04001703mapit:
Stefan Behrens61891922012-11-05 18:51:52 +01001704 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
1705
1706out:
1707 if (ret < 0)
1708 bio_endio(bio, ret);
1709 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001710}
Chris Mason6885f302008-02-20 16:11:05 -05001711
Chris Masond352ac62008-09-29 15:18:18 -04001712/*
1713 * given a list of ordered sums record them in the inode. This happens
1714 * at IO completion time based on sums calculated at bio submission time.
1715 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001716static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001717 struct inode *inode, u64 file_offset,
1718 struct list_head *list)
1719{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001720 struct btrfs_ordered_sum *sum;
1721
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001722 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00001723 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05001724 btrfs_csum_file_blocks(trans,
1725 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00001726 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001727 }
1728 return 0;
1729}
1730
Josef Bacik2ac55d42010-02-03 19:33:23 +00001731int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1732 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001733{
Julia Lawall6c1500f2012-11-03 20:30:18 +00001734 WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04001735 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001736 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001737}
1738
Chris Masond352ac62008-09-29 15:18:18 -04001739/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001740struct btrfs_writepage_fixup {
1741 struct page *page;
1742 struct btrfs_work work;
1743};
1744
Christoph Hellwigb2950862008-12-02 09:54:17 -05001745static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001746{
1747 struct btrfs_writepage_fixup *fixup;
1748 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001749 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001750 struct page *page;
1751 struct inode *inode;
1752 u64 page_start;
1753 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001754 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001755
1756 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1757 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001758again:
Chris Mason247e7432008-07-17 12:53:51 -04001759 lock_page(page);
1760 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1761 ClearPageChecked(page);
1762 goto out_page;
1763 }
1764
1765 inode = page->mapping->host;
1766 page_start = page_offset(page);
1767 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1768
Josef Bacik2ac55d42010-02-03 19:33:23 +00001769 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001770 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001771
1772 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001773 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001774 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001775
1776 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1777 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001778 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1779 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001780 unlock_page(page);
1781 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001782 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04001783 goto again;
1784 }
Chris Mason247e7432008-07-17 12:53:51 -04001785
Jeff Mahoney87826df2012-02-15 16:23:57 +01001786 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
1787 if (ret) {
1788 mapping_set_error(page->mapping, ret);
1789 end_extent_writepage(page, ret, page_start, page_end);
1790 ClearPageChecked(page);
1791 goto out;
1792 }
1793
Josef Bacik2ac55d42010-02-03 19:33:23 +00001794 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001795 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001796 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04001797out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001798 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1799 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001800out_page:
1801 unlock_page(page);
1802 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08001803 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04001804}
1805
1806/*
1807 * There are a few paths in the higher layers of the kernel that directly
1808 * set the page dirty bit without asking the filesystem if it is a
1809 * good idea. This causes problems because we want to make sure COW
1810 * properly happens and the data=ordered rules are followed.
1811 *
Chris Masonc8b97812008-10-29 14:49:59 -04001812 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001813 * hasn't been properly setup for IO. We kick off an async process
1814 * to fix it up. The async helper will wait for ordered extents, set
1815 * the delalloc bit and make it safe to write the page.
1816 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001817static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001818{
1819 struct inode *inode = page->mapping->host;
1820 struct btrfs_writepage_fixup *fixup;
1821 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001822
Chris Mason8b62b722009-09-02 16:53:46 -04001823 /* this page is properly in the ordered list */
1824 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001825 return 0;
1826
1827 if (PageChecked(page))
1828 return -EAGAIN;
1829
1830 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1831 if (!fixup)
1832 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001833
Chris Mason247e7432008-07-17 12:53:51 -04001834 SetPageChecked(page);
1835 page_cache_get(page);
1836 fixup->work.func = btrfs_writepage_fixup_worker;
1837 fixup->page = page;
1838 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001839 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04001840}
1841
Yan Zhengd899e052008-10-30 14:25:28 -04001842static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1843 struct inode *inode, u64 file_pos,
1844 u64 disk_bytenr, u64 disk_num_bytes,
1845 u64 num_bytes, u64 ram_bytes,
1846 u8 compression, u8 encryption,
1847 u16 other_encoding, int extent_type)
1848{
1849 struct btrfs_root *root = BTRFS_I(inode)->root;
1850 struct btrfs_file_extent_item *fi;
1851 struct btrfs_path *path;
1852 struct extent_buffer *leaf;
1853 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04001854 int ret;
1855
1856 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07001857 if (!path)
1858 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04001859
Chris Masonb9473432009-03-13 11:00:37 -04001860 path->leave_spinning = 1;
Chris Masona1ed8352009-09-11 12:27:37 -04001861
1862 /*
1863 * we may be replacing one extent in the tree with another.
1864 * The new extent is pinned in the extent map, and we don't want
1865 * to drop it from the cache until it is completely in the btree.
1866 *
1867 * So, tell btrfs_drop_extents to leave this extent in the cache.
1868 * the caller is expected to unpin it and allow it to be merged
1869 * with the others.
1870 */
Josef Bacik5dc562c2012-08-17 13:14:17 -04001871 ret = btrfs_drop_extents(trans, root, inode, file_pos,
Josef Bacik26714852012-08-29 12:24:27 -04001872 file_pos + num_bytes, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001873 if (ret)
1874 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001875
Li Zefan33345d012011-04-20 10:31:50 +08001876 ins.objectid = btrfs_ino(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04001877 ins.offset = file_pos;
1878 ins.type = BTRFS_EXTENT_DATA_KEY;
1879 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001880 if (ret)
1881 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001882 leaf = path->nodes[0];
1883 fi = btrfs_item_ptr(leaf, path->slots[0],
1884 struct btrfs_file_extent_item);
1885 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1886 btrfs_set_file_extent_type(leaf, fi, extent_type);
1887 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1888 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1889 btrfs_set_file_extent_offset(leaf, fi, 0);
1890 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1891 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1892 btrfs_set_file_extent_compression(leaf, fi, compression);
1893 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1894 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04001895
Yan Zhengd899e052008-10-30 14:25:28 -04001896 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04001897 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04001898
1899 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04001900
1901 ins.objectid = disk_bytenr;
1902 ins.offset = disk_num_bytes;
1903 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001904 ret = btrfs_alloc_reserved_file_extent(trans, root,
1905 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08001906 btrfs_ino(inode), file_pos, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001907out:
Yan Zhengd899e052008-10-30 14:25:28 -04001908 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04001909
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001910 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04001911}
1912
Liu Bo38c227d2013-01-29 03:18:40 +00001913/* snapshot-aware defrag */
1914struct sa_defrag_extent_backref {
1915 struct rb_node node;
1916 struct old_sa_defrag_extent *old;
1917 u64 root_id;
1918 u64 inum;
1919 u64 file_pos;
1920 u64 extent_offset;
1921 u64 num_bytes;
1922 u64 generation;
1923};
1924
1925struct old_sa_defrag_extent {
1926 struct list_head list;
1927 struct new_sa_defrag_extent *new;
1928
1929 u64 extent_offset;
1930 u64 bytenr;
1931 u64 offset;
1932 u64 len;
1933 int count;
1934};
1935
1936struct new_sa_defrag_extent {
1937 struct rb_root root;
1938 struct list_head head;
1939 struct btrfs_path *path;
1940 struct inode *inode;
1941 u64 file_pos;
1942 u64 len;
1943 u64 bytenr;
1944 u64 disk_len;
1945 u8 compress_type;
1946};
1947
1948static int backref_comp(struct sa_defrag_extent_backref *b1,
1949 struct sa_defrag_extent_backref *b2)
1950{
1951 if (b1->root_id < b2->root_id)
1952 return -1;
1953 else if (b1->root_id > b2->root_id)
1954 return 1;
1955
1956 if (b1->inum < b2->inum)
1957 return -1;
1958 else if (b1->inum > b2->inum)
1959 return 1;
1960
1961 if (b1->file_pos < b2->file_pos)
1962 return -1;
1963 else if (b1->file_pos > b2->file_pos)
1964 return 1;
1965
1966 /*
1967 * [------------------------------] ===> (a range of space)
1968 * |<--->| |<---->| =============> (fs/file tree A)
1969 * |<---------------------------->| ===> (fs/file tree B)
1970 *
1971 * A range of space can refer to two file extents in one tree while
1972 * refer to only one file extent in another tree.
1973 *
1974 * So we may process a disk offset more than one time(two extents in A)
1975 * and locate at the same extent(one extent in B), then insert two same
1976 * backrefs(both refer to the extent in B).
1977 */
1978 return 0;
1979}
1980
1981static void backref_insert(struct rb_root *root,
1982 struct sa_defrag_extent_backref *backref)
1983{
1984 struct rb_node **p = &root->rb_node;
1985 struct rb_node *parent = NULL;
1986 struct sa_defrag_extent_backref *entry;
1987 int ret;
1988
1989 while (*p) {
1990 parent = *p;
1991 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
1992
1993 ret = backref_comp(backref, entry);
1994 if (ret < 0)
1995 p = &(*p)->rb_left;
1996 else
1997 p = &(*p)->rb_right;
1998 }
1999
2000 rb_link_node(&backref->node, parent, p);
2001 rb_insert_color(&backref->node, root);
2002}
2003
2004/*
2005 * Note the backref might has changed, and in this case we just return 0.
2006 */
2007static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2008 void *ctx)
2009{
2010 struct btrfs_file_extent_item *extent;
2011 struct btrfs_fs_info *fs_info;
2012 struct old_sa_defrag_extent *old = ctx;
2013 struct new_sa_defrag_extent *new = old->new;
2014 struct btrfs_path *path = new->path;
2015 struct btrfs_key key;
2016 struct btrfs_root *root;
2017 struct sa_defrag_extent_backref *backref;
2018 struct extent_buffer *leaf;
2019 struct inode *inode = new->inode;
2020 int slot;
2021 int ret;
2022 u64 extent_offset;
2023 u64 num_bytes;
2024
2025 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
2026 inum == btrfs_ino(inode))
2027 return 0;
2028
2029 key.objectid = root_id;
2030 key.type = BTRFS_ROOT_ITEM_KEY;
2031 key.offset = (u64)-1;
2032
2033 fs_info = BTRFS_I(inode)->root->fs_info;
2034 root = btrfs_read_fs_root_no_name(fs_info, &key);
2035 if (IS_ERR(root)) {
2036 if (PTR_ERR(root) == -ENOENT)
2037 return 0;
2038 WARN_ON(1);
2039 pr_debug("inum=%llu, offset=%llu, root_id=%llu\n",
2040 inum, offset, root_id);
2041 return PTR_ERR(root);
2042 }
2043
2044 key.objectid = inum;
2045 key.type = BTRFS_EXTENT_DATA_KEY;
2046 if (offset > (u64)-1 << 32)
2047 key.offset = 0;
2048 else
2049 key.offset = offset;
2050
2051 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2052 if (ret < 0) {
2053 WARN_ON(1);
2054 return ret;
2055 }
Josef Bacik50f13192013-07-22 12:50:37 -04002056 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002057
2058 while (1) {
2059 cond_resched();
2060
2061 leaf = path->nodes[0];
2062 slot = path->slots[0];
2063
2064 if (slot >= btrfs_header_nritems(leaf)) {
2065 ret = btrfs_next_leaf(root, path);
2066 if (ret < 0) {
2067 goto out;
2068 } else if (ret > 0) {
2069 ret = 0;
2070 goto out;
2071 }
2072 continue;
2073 }
2074
2075 path->slots[0]++;
2076
2077 btrfs_item_key_to_cpu(leaf, &key, slot);
2078
2079 if (key.objectid > inum)
2080 goto out;
2081
2082 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2083 continue;
2084
2085 extent = btrfs_item_ptr(leaf, slot,
2086 struct btrfs_file_extent_item);
2087
2088 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2089 continue;
2090
Liu Boe68afa42013-07-01 22:13:26 +08002091 /*
2092 * 'offset' refers to the exact key.offset,
2093 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2094 * (key.offset - extent_offset).
2095 */
2096 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002097 continue;
2098
Liu Boe68afa42013-07-01 22:13:26 +08002099 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002100 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002101
Liu Bo38c227d2013-01-29 03:18:40 +00002102 if (extent_offset >= old->extent_offset + old->offset +
2103 old->len || extent_offset + num_bytes <=
2104 old->extent_offset + old->offset)
2105 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002106 break;
2107 }
2108
2109 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2110 if (!backref) {
2111 ret = -ENOENT;
2112 goto out;
2113 }
2114
2115 backref->root_id = root_id;
2116 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002117 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002118 backref->num_bytes = num_bytes;
2119 backref->extent_offset = extent_offset;
2120 backref->generation = btrfs_file_extent_generation(leaf, extent);
2121 backref->old = old;
2122 backref_insert(&new->root, backref);
2123 old->count++;
2124out:
2125 btrfs_release_path(path);
2126 WARN_ON(ret);
2127 return ret;
2128}
2129
2130static noinline bool record_extent_backrefs(struct btrfs_path *path,
2131 struct new_sa_defrag_extent *new)
2132{
2133 struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info;
2134 struct old_sa_defrag_extent *old, *tmp;
2135 int ret;
2136
2137 new->path = path;
2138
2139 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002140 ret = iterate_inodes_from_logical(old->bytenr +
2141 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002142 path, record_one_backref,
2143 old);
2144 BUG_ON(ret < 0 && ret != -ENOENT);
2145
2146 /* no backref to be processed for this extent */
2147 if (!old->count) {
2148 list_del(&old->list);
2149 kfree(old);
2150 }
2151 }
2152
2153 if (list_empty(&new->head))
2154 return false;
2155
2156 return true;
2157}
2158
2159static int relink_is_mergable(struct extent_buffer *leaf,
2160 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002161 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002162{
Liu Bo116e0022013-08-02 16:30:40 +08002163 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002164 return 0;
2165
2166 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2167 return 0;
2168
Liu Bo116e0022013-08-02 16:30:40 +08002169 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2170 return 0;
2171
2172 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002173 btrfs_file_extent_other_encoding(leaf, fi))
2174 return 0;
2175
2176 return 1;
2177}
2178
2179/*
2180 * Note the backref might has changed, and in this case we just return 0.
2181 */
2182static noinline int relink_extent_backref(struct btrfs_path *path,
2183 struct sa_defrag_extent_backref *prev,
2184 struct sa_defrag_extent_backref *backref)
2185{
2186 struct btrfs_file_extent_item *extent;
2187 struct btrfs_file_extent_item *item;
2188 struct btrfs_ordered_extent *ordered;
2189 struct btrfs_trans_handle *trans;
2190 struct btrfs_fs_info *fs_info;
2191 struct btrfs_root *root;
2192 struct btrfs_key key;
2193 struct extent_buffer *leaf;
2194 struct old_sa_defrag_extent *old = backref->old;
2195 struct new_sa_defrag_extent *new = old->new;
2196 struct inode *src_inode = new->inode;
2197 struct inode *inode;
2198 struct extent_state *cached = NULL;
2199 int ret = 0;
2200 u64 start;
2201 u64 len;
2202 u64 lock_start;
2203 u64 lock_end;
2204 bool merge = false;
2205 int index;
2206
2207 if (prev && prev->root_id == backref->root_id &&
2208 prev->inum == backref->inum &&
2209 prev->file_pos + prev->num_bytes == backref->file_pos)
2210 merge = true;
2211
2212 /* step 1: get root */
2213 key.objectid = backref->root_id;
2214 key.type = BTRFS_ROOT_ITEM_KEY;
2215 key.offset = (u64)-1;
2216
2217 fs_info = BTRFS_I(src_inode)->root->fs_info;
2218 index = srcu_read_lock(&fs_info->subvol_srcu);
2219
2220 root = btrfs_read_fs_root_no_name(fs_info, &key);
2221 if (IS_ERR(root)) {
2222 srcu_read_unlock(&fs_info->subvol_srcu, index);
2223 if (PTR_ERR(root) == -ENOENT)
2224 return 0;
2225 return PTR_ERR(root);
2226 }
Liu Bo38c227d2013-01-29 03:18:40 +00002227
2228 /* step 2: get inode */
2229 key.objectid = backref->inum;
2230 key.type = BTRFS_INODE_ITEM_KEY;
2231 key.offset = 0;
2232
2233 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2234 if (IS_ERR(inode)) {
2235 srcu_read_unlock(&fs_info->subvol_srcu, index);
2236 return 0;
2237 }
2238
2239 srcu_read_unlock(&fs_info->subvol_srcu, index);
2240
2241 /* step 3: relink backref */
2242 lock_start = backref->file_pos;
2243 lock_end = backref->file_pos + backref->num_bytes - 1;
2244 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2245 0, &cached);
2246
2247 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2248 if (ordered) {
2249 btrfs_put_ordered_extent(ordered);
2250 goto out_unlock;
2251 }
2252
2253 trans = btrfs_join_transaction(root);
2254 if (IS_ERR(trans)) {
2255 ret = PTR_ERR(trans);
2256 goto out_unlock;
2257 }
2258
2259 key.objectid = backref->inum;
2260 key.type = BTRFS_EXTENT_DATA_KEY;
2261 key.offset = backref->file_pos;
2262
2263 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2264 if (ret < 0) {
2265 goto out_free_path;
2266 } else if (ret > 0) {
2267 ret = 0;
2268 goto out_free_path;
2269 }
2270
2271 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2272 struct btrfs_file_extent_item);
2273
2274 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2275 backref->generation)
2276 goto out_free_path;
2277
2278 btrfs_release_path(path);
2279
2280 start = backref->file_pos;
2281 if (backref->extent_offset < old->extent_offset + old->offset)
2282 start += old->extent_offset + old->offset -
2283 backref->extent_offset;
2284
2285 len = min(backref->extent_offset + backref->num_bytes,
2286 old->extent_offset + old->offset + old->len);
2287 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2288
2289 ret = btrfs_drop_extents(trans, root, inode, start,
2290 start + len, 1);
2291 if (ret)
2292 goto out_free_path;
2293again:
2294 key.objectid = btrfs_ino(inode);
2295 key.type = BTRFS_EXTENT_DATA_KEY;
2296 key.offset = start;
2297
Liu Boa09a0a72013-03-11 09:20:58 +00002298 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002299 if (merge) {
2300 struct btrfs_file_extent_item *fi;
2301 u64 extent_len;
2302 struct btrfs_key found_key;
2303
2304 ret = btrfs_search_slot(trans, root, &key, path, 1, 1);
2305 if (ret < 0)
2306 goto out_free_path;
2307
2308 path->slots[0]--;
2309 leaf = path->nodes[0];
2310 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2311
2312 fi = btrfs_item_ptr(leaf, path->slots[0],
2313 struct btrfs_file_extent_item);
2314 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2315
Liu Bo116e0022013-08-02 16:30:40 +08002316 if (extent_len + found_key.offset == start &&
2317 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002318 btrfs_set_file_extent_num_bytes(leaf, fi,
2319 extent_len + len);
2320 btrfs_mark_buffer_dirty(leaf);
2321 inode_add_bytes(inode, len);
2322
2323 ret = 1;
2324 goto out_free_path;
2325 } else {
2326 merge = false;
2327 btrfs_release_path(path);
2328 goto again;
2329 }
2330 }
2331
2332 ret = btrfs_insert_empty_item(trans, root, path, &key,
2333 sizeof(*extent));
2334 if (ret) {
2335 btrfs_abort_transaction(trans, root, ret);
2336 goto out_free_path;
2337 }
2338
2339 leaf = path->nodes[0];
2340 item = btrfs_item_ptr(leaf, path->slots[0],
2341 struct btrfs_file_extent_item);
2342 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2343 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2344 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2345 btrfs_set_file_extent_num_bytes(leaf, item, len);
2346 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2347 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2348 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2349 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2350 btrfs_set_file_extent_encryption(leaf, item, 0);
2351 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2352
2353 btrfs_mark_buffer_dirty(leaf);
2354 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002355 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002356
2357 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
2358 new->disk_len, 0,
2359 backref->root_id, backref->inum,
2360 new->file_pos, 0); /* start - extent_offset */
2361 if (ret) {
2362 btrfs_abort_transaction(trans, root, ret);
2363 goto out_free_path;
2364 }
2365
2366 ret = 1;
2367out_free_path:
2368 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002369 path->leave_spinning = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002370 btrfs_end_transaction(trans, root);
2371out_unlock:
2372 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2373 &cached, GFP_NOFS);
2374 iput(inode);
2375 return ret;
2376}
2377
2378static void relink_file_extents(struct new_sa_defrag_extent *new)
2379{
2380 struct btrfs_path *path;
2381 struct old_sa_defrag_extent *old, *tmp;
2382 struct sa_defrag_extent_backref *backref;
2383 struct sa_defrag_extent_backref *prev = NULL;
2384 struct inode *inode;
2385 struct btrfs_root *root;
2386 struct rb_node *node;
2387 int ret;
2388
2389 inode = new->inode;
2390 root = BTRFS_I(inode)->root;
2391
2392 path = btrfs_alloc_path();
2393 if (!path)
2394 return;
2395
2396 if (!record_extent_backrefs(path, new)) {
2397 btrfs_free_path(path);
2398 goto out;
2399 }
2400 btrfs_release_path(path);
2401
2402 while (1) {
2403 node = rb_first(&new->root);
2404 if (!node)
2405 break;
2406 rb_erase(node, &new->root);
2407
2408 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2409
2410 ret = relink_extent_backref(path, prev, backref);
2411 WARN_ON(ret < 0);
2412
2413 kfree(prev);
2414
2415 if (ret == 1)
2416 prev = backref;
2417 else
2418 prev = NULL;
2419 cond_resched();
2420 }
2421 kfree(prev);
2422
2423 btrfs_free_path(path);
2424
2425 list_for_each_entry_safe(old, tmp, &new->head, list) {
2426 list_del(&old->list);
2427 kfree(old);
2428 }
2429out:
2430 atomic_dec(&root->fs_info->defrag_running);
2431 wake_up(&root->fs_info->transaction_wait);
2432
2433 kfree(new);
2434}
2435
2436static struct new_sa_defrag_extent *
2437record_old_file_extents(struct inode *inode,
2438 struct btrfs_ordered_extent *ordered)
2439{
2440 struct btrfs_root *root = BTRFS_I(inode)->root;
2441 struct btrfs_path *path;
2442 struct btrfs_key key;
2443 struct old_sa_defrag_extent *old, *tmp;
2444 struct new_sa_defrag_extent *new;
2445 int ret;
2446
2447 new = kmalloc(sizeof(*new), GFP_NOFS);
2448 if (!new)
2449 return NULL;
2450
2451 new->inode = inode;
2452 new->file_pos = ordered->file_offset;
2453 new->len = ordered->len;
2454 new->bytenr = ordered->start;
2455 new->disk_len = ordered->disk_len;
2456 new->compress_type = ordered->compress_type;
2457 new->root = RB_ROOT;
2458 INIT_LIST_HEAD(&new->head);
2459
2460 path = btrfs_alloc_path();
2461 if (!path)
2462 goto out_kfree;
2463
2464 key.objectid = btrfs_ino(inode);
2465 key.type = BTRFS_EXTENT_DATA_KEY;
2466 key.offset = new->file_pos;
2467
2468 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2469 if (ret < 0)
2470 goto out_free_path;
2471 if (ret > 0 && path->slots[0] > 0)
2472 path->slots[0]--;
2473
2474 /* find out all the old extents for the file range */
2475 while (1) {
2476 struct btrfs_file_extent_item *extent;
2477 struct extent_buffer *l;
2478 int slot;
2479 u64 num_bytes;
2480 u64 offset;
2481 u64 end;
2482 u64 disk_bytenr;
2483 u64 extent_offset;
2484
2485 l = path->nodes[0];
2486 slot = path->slots[0];
2487
2488 if (slot >= btrfs_header_nritems(l)) {
2489 ret = btrfs_next_leaf(root, path);
2490 if (ret < 0)
2491 goto out_free_list;
2492 else if (ret > 0)
2493 break;
2494 continue;
2495 }
2496
2497 btrfs_item_key_to_cpu(l, &key, slot);
2498
2499 if (key.objectid != btrfs_ino(inode))
2500 break;
2501 if (key.type != BTRFS_EXTENT_DATA_KEY)
2502 break;
2503 if (key.offset >= new->file_pos + new->len)
2504 break;
2505
2506 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2507
2508 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2509 if (key.offset + num_bytes < new->file_pos)
2510 goto next;
2511
2512 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2513 if (!disk_bytenr)
2514 goto next;
2515
2516 extent_offset = btrfs_file_extent_offset(l, extent);
2517
2518 old = kmalloc(sizeof(*old), GFP_NOFS);
2519 if (!old)
2520 goto out_free_list;
2521
2522 offset = max(new->file_pos, key.offset);
2523 end = min(new->file_pos + new->len, key.offset + num_bytes);
2524
2525 old->bytenr = disk_bytenr;
2526 old->extent_offset = extent_offset;
2527 old->offset = offset - key.offset;
2528 old->len = end - offset;
2529 old->new = new;
2530 old->count = 0;
2531 list_add_tail(&old->list, &new->head);
2532next:
2533 path->slots[0]++;
2534 cond_resched();
2535 }
2536
2537 btrfs_free_path(path);
2538 atomic_inc(&root->fs_info->defrag_running);
2539
2540 return new;
2541
2542out_free_list:
2543 list_for_each_entry_safe(old, tmp, &new->head, list) {
2544 list_del(&old->list);
2545 kfree(old);
2546 }
2547out_free_path:
2548 btrfs_free_path(path);
2549out_kfree:
2550 kfree(new);
2551 return NULL;
2552}
2553
Chris Mason5d13a982009-03-13 11:41:46 -04002554/*
2555 * helper function for btrfs_finish_ordered_io, this
2556 * just reads in some of the csum leaves to prime them into ram
2557 * before we start the transaction. It limits the amount of btree
2558 * reads required while inside the transaction.
2559 */
Chris Masond352ac62008-09-29 15:18:18 -04002560/* as ordered data IO finishes, this gets called so we can finish
2561 * an ordered extent if the range of bytes in the file it covers are
2562 * fully written.
2563 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002564static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002565{
Josef Bacik5fd02042012-05-02 14:00:54 -04002566 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002567 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002568 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002569 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002570 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002571 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002572 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002573 int ret = 0;
2574 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002575 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002576 bool truncated = false;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002577
Liu Bo83eea1f2012-07-10 05:28:39 -06002578 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002579
Josef Bacik5fd02042012-05-02 14:00:54 -04002580 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2581 ret = -EIO;
2582 goto out;
2583 }
2584
Josef Bacik77cef2e2013-08-29 13:57:21 -04002585 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2586 truncated = true;
2587 logical_len = ordered_extent->truncated_len;
2588 /* Truncated the entire extent, don't bother adding */
2589 if (!logical_len)
2590 goto out;
2591 }
2592
Yan, Zhengc2167752009-11-12 09:34:21 +00002593 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002594 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Josef Bacik6c760c02012-11-09 10:53:21 -05002595 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2596 if (nolock)
2597 trans = btrfs_join_transaction_nolock(root);
2598 else
2599 trans = btrfs_join_transaction(root);
2600 if (IS_ERR(trans)) {
2601 ret = PTR_ERR(trans);
2602 trans = NULL;
2603 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002604 }
Josef Bacik6c760c02012-11-09 10:53:21 -05002605 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
2606 ret = btrfs_update_inode_fallback(trans, root, inode);
2607 if (ret) /* -ENOMEM or corruption */
2608 btrfs_abort_transaction(trans, root, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002609 goto out;
2610 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002611
Josef Bacik2ac55d42010-02-03 19:33:23 +00002612 lock_extent_bits(io_tree, ordered_extent->file_offset,
2613 ordered_extent->file_offset + ordered_extent->len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002614 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002615
Liu Bo38c227d2013-01-29 03:18:40 +00002616 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2617 ordered_extent->file_offset + ordered_extent->len - 1,
2618 EXTENT_DEFRAG, 1, cached_state);
2619 if (ret) {
2620 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
2621 if (last_snapshot >= BTRFS_I(inode)->generation)
2622 /* the inode is shared */
2623 new = record_old_file_extents(inode, ordered_extent);
2624
2625 clear_extent_bit(io_tree, ordered_extent->file_offset,
2626 ordered_extent->file_offset + ordered_extent->len - 1,
2627 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2628 }
2629
Josef Bacik0cb59c92010-07-02 12:14:14 -04002630 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002631 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002632 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002633 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002634 if (IS_ERR(trans)) {
2635 ret = PTR_ERR(trans);
2636 trans = NULL;
2637 goto out_unlock;
2638 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002639 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002640
Chris Masonc8b97812008-10-29 14:49:59 -04002641 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002642 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002643 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002644 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00002645 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002646 ordered_extent->file_offset,
2647 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002648 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002649 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04002650 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002651 ret = insert_reserved_file_extent(trans, inode,
2652 ordered_extent->file_offset,
2653 ordered_extent->start,
2654 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04002655 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08002656 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002657 BTRFS_FILE_EXTENT_REG);
Yan Zhengd899e052008-10-30 14:25:28 -04002658 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002659 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2660 ordered_extent->file_offset, ordered_extent->len,
2661 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002662 if (ret < 0) {
2663 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002664 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002665 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002666
Chris Masone6dcd2d2008-07-17 12:53:50 -04002667 add_pending_csums(trans, inode, ordered_extent->file_offset,
2668 &ordered_extent->list);
2669
Josef Bacik6c760c02012-11-09 10:53:21 -05002670 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2671 ret = btrfs_update_inode_fallback(trans, root, inode);
2672 if (ret) { /* -ENOMEM or corruption */
2673 btrfs_abort_transaction(trans, root, ret);
2674 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002675 }
2676 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002677out_unlock:
2678 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2679 ordered_extent->file_offset +
2680 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00002681out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002682 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04002683 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002684 if (trans)
2685 btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002686
Josef Bacik77cef2e2013-08-29 13:57:21 -04002687 if (ret || truncated) {
2688 u64 start, end;
2689
2690 if (truncated)
2691 start = ordered_extent->file_offset + logical_len;
2692 else
2693 start = ordered_extent->file_offset;
2694 end = ordered_extent->file_offset + ordered_extent->len - 1;
2695 clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS);
2696
2697 /* Drop the cache for the part of the extent we didn't write. */
2698 btrfs_drop_extent_cache(inode, start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04002699
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002700 /*
2701 * If the ordered extent had an IOERR or something else went
2702 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04002703 * back to the allocator. We only free the extent in the
2704 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002705 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04002706 if ((ret || !logical_len) &&
2707 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002708 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
2709 btrfs_free_reserved_extent(root, ordered_extent->start,
2710 ordered_extent->disk_len);
2711 }
2712
2713
Josef Bacik5fd02042012-05-02 14:00:54 -04002714 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002715 * This needs to be done to make sure anybody waiting knows we are done
2716 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002717 */
2718 btrfs_remove_ordered_extent(inode, ordered_extent);
2719
Liu Bo38c227d2013-01-29 03:18:40 +00002720 /* for snapshot-aware defrag */
2721 if (new)
2722 relink_file_extents(new);
2723
Chris Masone6dcd2d2008-07-17 12:53:50 -04002724 /* once for us */
2725 btrfs_put_ordered_extent(ordered_extent);
2726 /* once for the tree */
2727 btrfs_put_ordered_extent(ordered_extent);
2728
Josef Bacik5fd02042012-05-02 14:00:54 -04002729 return ret;
2730}
2731
2732static void finish_ordered_fn(struct btrfs_work *work)
2733{
2734 struct btrfs_ordered_extent *ordered_extent;
2735 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2736 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002737}
2738
Christoph Hellwigb2950862008-12-02 09:54:17 -05002739static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04002740 struct extent_state *state, int uptodate)
2741{
Josef Bacik5fd02042012-05-02 14:00:54 -04002742 struct inode *inode = page->mapping->host;
2743 struct btrfs_root *root = BTRFS_I(inode)->root;
2744 struct btrfs_ordered_extent *ordered_extent = NULL;
2745 struct btrfs_workers *workers;
2746
liubo1abe9b82011-03-24 11:18:59 +00002747 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2748
Chris Mason8b62b722009-09-02 16:53:46 -04002749 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002750 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2751 end - start + 1, uptodate))
2752 return 0;
2753
2754 ordered_extent->work.func = finish_ordered_fn;
2755 ordered_extent->work.flags = 0;
2756
Liu Bo83eea1f2012-07-10 05:28:39 -06002757 if (btrfs_is_free_space_inode(inode))
Josef Bacik5fd02042012-05-02 14:00:54 -04002758 workers = &root->fs_info->endio_freespace_worker;
2759 else
2760 workers = &root->fs_info->endio_write_workers;
2761 btrfs_queue_worker(workers, &ordered_extent->work);
2762
2763 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04002764}
2765
Chris Masond352ac62008-09-29 15:18:18 -04002766/*
Chris Masond352ac62008-09-29 15:18:18 -04002767 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002768 * if there's a match, we allow the bio to finish. If not, the code in
2769 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002770 */
Miao Xiefacc8a222013-07-25 19:22:34 +08002771static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
2772 u64 phy_offset, struct page *page,
2773 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002774{
Miao Xie4eee4fa2012-12-21 09:17:45 +00002775 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04002776 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002777 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04002778 char *kaddr;
Chris Masonff79f812007-10-15 16:22:25 -04002779 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiefacc8a222013-07-25 19:22:34 +08002780 u32 csum_expected;
Chris Masonff79f812007-10-15 16:22:25 -04002781 u32 csum = ~(u32)0;
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002782 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
2783 DEFAULT_RATELIMIT_BURST);
Chris Masond1310b22008-01-24 16:13:08 -05002784
Chris Masond20f7042008-12-08 16:58:54 -05002785 if (PageChecked(page)) {
2786 ClearPageChecked(page);
2787 goto good;
2788 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002789
2790 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Jan Schmidt08d2f342011-05-04 16:18:50 +02002791 goto good;
Chris Masond20f7042008-12-08 16:58:54 -05002792
Yan Zheng17d217f2008-12-12 10:03:38 -05002793 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002794 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002795 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
2796 GFP_NOFS);
2797 return 0;
2798 }
2799
Miao Xiefacc8a222013-07-25 19:22:34 +08002800 phy_offset >>= inode->i_sb->s_blocksize_bits;
2801 csum_expected = *(((u32 *)io_bio->csum) + phy_offset);
Chris Masond3977122009-01-05 21:25:51 -05002802
Miao Xiefacc8a222013-07-25 19:22:34 +08002803 kaddr = kmap_atomic(page);
Liu Bob0496682013-03-14 14:57:45 +00002804 csum = btrfs_csum_data(kaddr + offset, csum, end - start + 1);
Chris Masonff79f812007-10-15 16:22:25 -04002805 btrfs_csum_final(csum, (char *)&csum);
Miao Xiefacc8a222013-07-25 19:22:34 +08002806 if (csum != csum_expected)
Chris Mason07157aa2007-08-30 08:50:51 -04002807 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002808
Cong Wang7ac687d2011-11-25 23:14:28 +08002809 kunmap_atomic(kaddr);
Chris Masond20f7042008-12-08 16:58:54 -05002810good:
Chris Mason07157aa2007-08-30 08:50:51 -04002811 return 0;
2812
2813zeroit:
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002814 if (__ratelimit(&_rs))
Miao Xiefacc8a222013-07-25 19:22:34 +08002815 btrfs_info(root->fs_info, "csum failed ino %llu off %llu csum %u expected csum %u",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002816 btrfs_ino(page->mapping->host), start, csum, csum_expected);
Chris Masondb945352007-10-15 16:15:53 -04002817 memset(kaddr + offset, 1, end - start + 1);
2818 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002819 kunmap_atomic(kaddr);
Miao Xiefacc8a222013-07-25 19:22:34 +08002820 if (csum_expected == 0)
Chris Mason3b951512008-04-17 11:29:12 -04002821 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04002822 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04002823}
Chris Masonb888db22007-08-27 16:49:44 -04002824
Yan, Zheng24bbcf042009-11-12 09:36:34 +00002825struct delayed_iput {
2826 struct list_head list;
2827 struct inode *inode;
2828};
2829
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002830/* JDM: If this is fs-wide, why can't we add a pointer to
2831 * btrfs_inode instead and avoid the allocation? */
Yan, Zheng24bbcf042009-11-12 09:36:34 +00002832void btrfs_add_delayed_iput(struct inode *inode)
2833{
2834 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2835 struct delayed_iput *delayed;
2836
2837 if (atomic_add_unless(&inode->i_count, -1, 1))
2838 return;
2839
2840 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2841 delayed->inode = inode;
2842
2843 spin_lock(&fs_info->delayed_iput_lock);
2844 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2845 spin_unlock(&fs_info->delayed_iput_lock);
2846}
2847
2848void btrfs_run_delayed_iputs(struct btrfs_root *root)
2849{
2850 LIST_HEAD(list);
2851 struct btrfs_fs_info *fs_info = root->fs_info;
2852 struct delayed_iput *delayed;
2853 int empty;
2854
2855 spin_lock(&fs_info->delayed_iput_lock);
2856 empty = list_empty(&fs_info->delayed_iputs);
2857 spin_unlock(&fs_info->delayed_iput_lock);
2858 if (empty)
2859 return;
2860
Yan, Zheng24bbcf042009-11-12 09:36:34 +00002861 spin_lock(&fs_info->delayed_iput_lock);
2862 list_splice_init(&fs_info->delayed_iputs, &list);
2863 spin_unlock(&fs_info->delayed_iput_lock);
2864
2865 while (!list_empty(&list)) {
2866 delayed = list_entry(list.next, struct delayed_iput, list);
2867 list_del(&delayed->list);
2868 iput(delayed->inode);
2869 kfree(delayed);
2870 }
Yan, Zheng24bbcf042009-11-12 09:36:34 +00002871}
2872
Yan, Zhengd68fc572010-05-16 10:49:58 -04002873/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08002874 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04002875 * files in the subvolume, it removes orphan item and frees block_rsv
2876 * structure.
2877 */
2878void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
2879 struct btrfs_root *root)
2880{
Josef Bacik90290e12011-12-02 15:44:12 -05002881 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002882 int ret;
2883
Josef Bacik8a35d952012-05-23 14:26:42 -04002884 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04002885 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
2886 return;
2887
Josef Bacik90290e12011-12-02 15:44:12 -05002888 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04002889 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05002890 spin_unlock(&root->orphan_lock);
2891 return;
2892 }
2893
2894 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
2895 spin_unlock(&root->orphan_lock);
2896 return;
2897 }
2898
2899 block_rsv = root->orphan_block_rsv;
2900 root->orphan_block_rsv = NULL;
2901 spin_unlock(&root->orphan_lock);
2902
Yan, Zhengd68fc572010-05-16 10:49:58 -04002903 if (root->orphan_item_inserted &&
2904 btrfs_root_refs(&root->root_item) > 0) {
2905 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
2906 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04002907 if (ret)
2908 btrfs_abort_transaction(trans, root, ret);
2909 else
2910 root->orphan_item_inserted = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002911 }
2912
Josef Bacik90290e12011-12-02 15:44:12 -05002913 if (block_rsv) {
2914 WARN_ON(block_rsv->size > 0);
2915 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002916 }
2917}
2918
2919/*
Josef Bacik7b128762008-07-24 12:17:14 -04002920 * This creates an orphan entry for the given inode in case something goes
2921 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04002922 *
2923 * NOTE: caller of this function should reserve 5 units of metadata for
2924 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04002925 */
2926int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2927{
2928 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002929 struct btrfs_block_rsv *block_rsv = NULL;
2930 int reserve = 0;
2931 int insert = 0;
2932 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002933
Yan, Zhengd68fc572010-05-16 10:49:58 -04002934 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06002935 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00002936 if (!block_rsv)
2937 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04002938 }
2939
Yan, Zhengd68fc572010-05-16 10:49:58 -04002940 spin_lock(&root->orphan_lock);
2941 if (!root->orphan_block_rsv) {
2942 root->orphan_block_rsv = block_rsv;
2943 } else if (block_rsv) {
2944 btrfs_free_block_rsv(root, block_rsv);
2945 block_rsv = NULL;
2946 }
Josef Bacik7b128762008-07-24 12:17:14 -04002947
Josef Bacik8a35d952012-05-23 14:26:42 -04002948 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2949 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04002950#if 0
2951 /*
2952 * For proper ENOSPC handling, we should do orphan
2953 * cleanup when mounting. But this introduces backward
2954 * compatibility issue.
2955 */
2956 if (!xchg(&root->orphan_item_inserted, 1))
2957 insert = 2;
2958 else
2959 insert = 1;
2960#endif
2961 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06002962 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002963 }
Josef Bacik7b128762008-07-24 12:17:14 -04002964
Josef Bacik72ac3c02012-05-23 14:13:11 -04002965 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
2966 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04002967 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002968 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002969
Yan, Zhengd68fc572010-05-16 10:49:58 -04002970 /* grab metadata reservation from transaction handle */
2971 if (reserve) {
2972 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002973 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04002974 }
2975
2976 /* insert an orphan item to track this unlinked/truncated file */
2977 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08002978 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04002979 if (ret) {
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01002980 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04002981 if (reserve) {
2982 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
2983 &BTRFS_I(inode)->runtime_flags);
2984 btrfs_orphan_release_metadata(inode);
2985 }
2986 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04002987 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2988 &BTRFS_I(inode)->runtime_flags);
Josef Bacik4ef31a42013-08-13 14:10:08 -04002989 btrfs_abort_transaction(trans, root, ret);
2990 return ret;
2991 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002992 }
2993 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002994 }
2995
2996 /* insert an orphan item to track subvolume contains orphan files */
2997 if (insert >= 2) {
2998 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
2999 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003000 if (ret && ret != -EEXIST) {
3001 btrfs_abort_transaction(trans, root, ret);
3002 return ret;
3003 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003004 }
3005 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003006}
3007
3008/*
3009 * We have done the truncate/delete so we can go ahead and remove the orphan
3010 * item for this particular inode.
3011 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003012static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3013 struct inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003014{
3015 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003016 int delete_item = 0;
3017 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003018 int ret = 0;
3019
Yan, Zhengd68fc572010-05-16 10:49:58 -04003020 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003021 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3022 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003023 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003024
Josef Bacik72ac3c02012-05-23 14:13:11 -04003025 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3026 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003027 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003028 spin_unlock(&root->orphan_lock);
3029
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003030 if (delete_item) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003031 atomic_dec(&root->orphan_inodes);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003032 if (trans)
3033 ret = btrfs_del_orphan_item(trans, root,
3034 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04003035 }
Josef Bacik7b128762008-07-24 12:17:14 -04003036
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003037 if (release_rsv)
3038 btrfs_orphan_release_metadata(inode);
3039
Josef Bacik4ef31a42013-08-13 14:10:08 -04003040 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003041}
3042
3043/*
3044 * this cleans up any orphans that may be left on the list from the last use
3045 * of this root.
3046 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003047int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003048{
3049 struct btrfs_path *path;
3050 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003051 struct btrfs_key key, found_key;
3052 struct btrfs_trans_handle *trans;
3053 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003054 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003055 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3056
Yan, Zhengd68fc572010-05-16 10:49:58 -04003057 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003058 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003059
3060 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003061 if (!path) {
3062 ret = -ENOMEM;
3063 goto out;
3064 }
Josef Bacik7b128762008-07-24 12:17:14 -04003065 path->reada = -1;
3066
3067 key.objectid = BTRFS_ORPHAN_OBJECTID;
3068 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
3069 key.offset = (u64)-1;
3070
Josef Bacik7b128762008-07-24 12:17:14 -04003071 while (1) {
3072 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003073 if (ret < 0)
3074 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003075
3076 /*
3077 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003078 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003079 * find the key and see if we have stuff that matches
3080 */
3081 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003082 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003083 if (path->slots[0] == 0)
3084 break;
3085 path->slots[0]--;
3086 }
3087
3088 /* pull out the item */
3089 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003090 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3091
3092 /* make sure the item matches what we want */
3093 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3094 break;
3095 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
3096 break;
3097
3098 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003099 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003100
3101 /*
3102 * this is where we are basically btrfs_lookup, without the
3103 * crossing root thing. we store the inode number in the
3104 * offset of the orphan item.
3105 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003106
3107 if (found_key.offset == last_objectid) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003108 btrfs_err(root->fs_info,
3109 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003110 ret = -EINVAL;
3111 goto out;
3112 }
3113
3114 last_objectid = found_key.offset;
3115
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003116 found_key.objectid = found_key.offset;
3117 found_key.type = BTRFS_INODE_ITEM_KEY;
3118 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00003119 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303120 ret = PTR_ERR_OR_ZERO(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003121 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003122 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003123
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003124 if (ret == -ESTALE && root == root->fs_info->tree_root) {
3125 struct btrfs_root *dead_root;
3126 struct btrfs_fs_info *fs_info = root->fs_info;
3127 int is_dead_root = 0;
3128
3129 /*
3130 * this is an orphan in the tree root. Currently these
3131 * could come from 2 sources:
3132 * a) a snapshot deletion in progress
3133 * b) a free space cache inode
3134 * We need to distinguish those two, as the snapshot
3135 * orphan must not get deleted.
3136 * find_dead_roots already ran before us, so if this
3137 * is a snapshot deletion, we should find the root
3138 * in the dead_roots list
3139 */
3140 spin_lock(&fs_info->trans_lock);
3141 list_for_each_entry(dead_root, &fs_info->dead_roots,
3142 root_list) {
3143 if (dead_root->root_key.objectid ==
3144 found_key.objectid) {
3145 is_dead_root = 1;
3146 break;
3147 }
3148 }
3149 spin_unlock(&fs_info->trans_lock);
3150 if (is_dead_root) {
3151 /* prevent this orphan from being found again */
3152 key.offset = found_key.objectid - 1;
3153 continue;
3154 }
3155 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003156 /*
3157 * Inode is already gone but the orphan item is still there,
3158 * kill the orphan item.
3159 */
3160 if (ret == -ESTALE) {
3161 trans = btrfs_start_transaction(root, 1);
3162 if (IS_ERR(trans)) {
3163 ret = PTR_ERR(trans);
3164 goto out;
3165 }
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003166 btrfs_debug(root->fs_info, "auto deleting %Lu",
3167 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003168 ret = btrfs_del_orphan_item(trans, root,
3169 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003170 btrfs_end_transaction(trans, root);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003171 if (ret)
3172 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003173 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003174 }
Josef Bacik7b128762008-07-24 12:17:14 -04003175
Josef Bacik7b128762008-07-24 12:17:14 -04003176 /*
3177 * add this inode to the orphan list so btrfs_orphan_del does
3178 * the proper thing when we hit it
3179 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003180 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3181 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003182 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003183
Josef Bacik7b128762008-07-24 12:17:14 -04003184 /* if we have links, this was a truncate, lets do that */
3185 if (inode->i_nlink) {
Josef Bacika41ad392011-01-31 15:30:16 -05003186 if (!S_ISREG(inode->i_mode)) {
3187 WARN_ON(1);
3188 iput(inode);
3189 continue;
3190 }
Josef Bacik7b128762008-07-24 12:17:14 -04003191 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003192
3193 /* 1 for the orphan item deletion. */
3194 trans = btrfs_start_transaction(root, 1);
3195 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003196 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003197 ret = PTR_ERR(trans);
3198 goto out;
3199 }
3200 ret = btrfs_orphan_add(trans, inode);
3201 btrfs_end_transaction(trans, root);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003202 if (ret) {
3203 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003204 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003205 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003206
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003207 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003208 if (ret)
3209 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003210 } else {
3211 nr_unlink++;
3212 }
3213
3214 /* this will do delete_inode and everything for us */
3215 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003216 if (ret)
3217 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003218 }
Miao Xie3254c872011-11-10 20:45:05 -05003219 /* release the path since we're done with it */
3220 btrfs_release_path(path);
3221
Yan, Zhengd68fc572010-05-16 10:49:58 -04003222 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3223
3224 if (root->orphan_block_rsv)
3225 btrfs_block_rsv_release(root, root->orphan_block_rsv,
3226 (u64)-1);
3227
3228 if (root->orphan_block_rsv || root->orphan_item_inserted) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003229 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003230 if (!IS_ERR(trans))
3231 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003232 }
Josef Bacik7b128762008-07-24 12:17:14 -04003233
3234 if (nr_unlink)
David Sterba4884b472013-03-20 13:31:27 +00003235 btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003236 if (nr_truncate)
David Sterba4884b472013-03-20 13:31:27 +00003237 btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003238
3239out:
3240 if (ret)
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003241 btrfs_crit(root->fs_info,
3242 "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003243 btrfs_free_path(path);
3244 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003245}
3246
Chris Masond352ac62008-09-29 15:18:18 -04003247/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003248 * very simple check to peek ahead in the leaf looking for xattrs. If we
3249 * don't find any xattrs, we know there can't be any acls.
3250 *
3251 * slot is the slot the inode is in, objectid is the objectid of the inode
3252 */
3253static noinline int acls_after_inode_item(struct extent_buffer *leaf,
3254 int slot, u64 objectid)
3255{
3256 u32 nritems = btrfs_header_nritems(leaf);
3257 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003258 static u64 xattr_access = 0;
3259 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003260 int scanned = 0;
3261
Josef Bacikf23b5a52013-06-19 10:16:26 -04003262 if (!xattr_access) {
3263 xattr_access = btrfs_name_hash(POSIX_ACL_XATTR_ACCESS,
3264 strlen(POSIX_ACL_XATTR_ACCESS));
3265 xattr_default = btrfs_name_hash(POSIX_ACL_XATTR_DEFAULT,
3266 strlen(POSIX_ACL_XATTR_DEFAULT));
3267 }
3268
Chris Mason46a53cc2009-04-27 11:47:50 -04003269 slot++;
3270 while (slot < nritems) {
3271 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3272
3273 /* we found a different objectid, there must not be acls */
3274 if (found_key.objectid != objectid)
3275 return 0;
3276
3277 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003278 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
3279 if (found_key.offset == xattr_access ||
3280 found_key.offset == xattr_default)
3281 return 1;
3282 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003283
3284 /*
3285 * we found a key greater than an xattr key, there can't
3286 * be any acls later on
3287 */
3288 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3289 return 0;
3290
3291 slot++;
3292 scanned++;
3293
3294 /*
3295 * it goes inode, inode backrefs, xattrs, extents,
3296 * so if there are a ton of hard links to an inode there can
3297 * be a lot of backrefs. Don't waste time searching too hard,
3298 * this is just an optimization
3299 */
3300 if (scanned >= 8)
3301 break;
3302 }
3303 /* we hit the end of the leaf before we found an xattr or
3304 * something larger than an xattr. We have to assume the inode
3305 * has acls
3306 */
3307 return 1;
3308}
3309
3310/*
Chris Masond352ac62008-09-29 15:18:18 -04003311 * read an inode from the btree into the in-memory inode
3312 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003313static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003314{
3315 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003316 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003317 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04003318 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04003319 struct btrfs_root *root = BTRFS_I(inode)->root;
3320 struct btrfs_key location;
Chris Mason46a53cc2009-04-27 11:47:50 -04003321 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003322 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003323 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003324 bool filled = false;
3325
3326 ret = btrfs_fill_inode(inode, &rdev);
3327 if (!ret)
3328 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003329
3330 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07003331 if (!path)
3332 goto make_bad;
3333
Josef Bacikd90c7322011-05-17 09:50:54 -04003334 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003335 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003336
Chris Mason39279cc2007-06-12 06:35:45 -04003337 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003338 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04003339 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04003340
Chris Mason5f39d392007-10-15 16:14:19 -04003341 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003342
3343 if (filled)
3344 goto cache_acl;
3345
Chris Mason5f39d392007-10-15 16:14:19 -04003346 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3347 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003348 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003349 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003350 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3351 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04003352 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003353
3354 tspec = btrfs_inode_atime(inode_item);
3355 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3356 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3357
3358 tspec = btrfs_inode_mtime(inode_item);
3359 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3360 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3361
3362 tspec = btrfs_inode_ctime(inode_item);
3363 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3364 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3365
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003366 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003367 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003368 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3369
3370 /*
3371 * If we were modified in the current generation and evicted from memory
3372 * and then re-read we need to do a full sync since we don't have any
3373 * idea about which extents were modified before we were evicted from
3374 * cache.
3375 */
3376 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
3377 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3378 &BTRFS_I(inode)->runtime_flags);
3379
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003380 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003381 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003382 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003383 rdev = btrfs_inode_rdev(leaf, inode_item);
3384
Josef Bacikaec74772008-07-24 12:12:38 -04003385 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003386 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie2f7e33d2011-06-23 07:27:13 +00003387cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003388 /*
3389 * try to precache a NULL acl entry for files that don't have
3390 * any xattrs or acls
3391 */
Li Zefan33345d012011-04-20 10:31:50 +08003392 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
3393 btrfs_ino(inode));
Al Viro72c04902009-06-24 16:58:48 -04003394 if (!maybe_acls)
3395 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003396
Chris Mason39279cc2007-06-12 06:35:45 -04003397 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003398
Chris Mason39279cc2007-06-12 06:35:45 -04003399 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003400 case S_IFREG:
3401 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003402 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05003403 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003404 inode->i_fop = &btrfs_file_operations;
3405 inode->i_op = &btrfs_file_inode_operations;
3406 break;
3407 case S_IFDIR:
3408 inode->i_fop = &btrfs_dir_file_operations;
3409 if (root == root->fs_info->tree_root)
3410 inode->i_op = &btrfs_dir_ro_inode_operations;
3411 else
3412 inode->i_op = &btrfs_dir_inode_operations;
3413 break;
3414 case S_IFLNK:
3415 inode->i_op = &btrfs_symlink_inode_operations;
3416 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04003417 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003418 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003419 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003420 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003421 init_special_inode(inode, inode->i_mode, rdev);
3422 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003423 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003424
3425 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003426 return;
3427
3428make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003429 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003430 make_bad_inode(inode);
3431}
3432
Chris Masond352ac62008-09-29 15:18:18 -04003433/*
3434 * given a leaf and an inode, copy the inode fields into the leaf
3435 */
Chris Masone02119d2008-09-05 16:13:11 -04003436static void fill_inode_item(struct btrfs_trans_handle *trans,
3437 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003438 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003439 struct inode *inode)
3440{
Liu Bo51fab692012-12-27 09:01:21 +00003441 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003442
Liu Bo51fab692012-12-27 09:01:21 +00003443 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003444
Liu Bo51fab692012-12-27 09:01:21 +00003445 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3446 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3447 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3448 &token);
3449 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3450 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003451
Liu Bo51fab692012-12-27 09:01:21 +00003452 btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item),
3453 inode->i_atime.tv_sec, &token);
3454 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item),
3455 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003456
Liu Bo51fab692012-12-27 09:01:21 +00003457 btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item),
3458 inode->i_mtime.tv_sec, &token);
3459 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item),
3460 inode->i_mtime.tv_nsec, &token);
3461
3462 btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item),
3463 inode->i_ctime.tv_sec, &token);
3464 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item),
3465 inode->i_ctime.tv_nsec, &token);
3466
3467 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3468 &token);
3469 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3470 &token);
3471 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3472 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3473 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3474 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3475 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003476}
3477
Chris Masond352ac62008-09-29 15:18:18 -04003478/*
3479 * copy everything in the in-memory inode into the btree.
3480 */
Chris Mason21151332011-11-10 20:39:08 -05003481static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003482 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003483{
3484 struct btrfs_inode_item *inode_item;
3485 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003486 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003487 int ret;
3488
3489 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003490 if (!path)
3491 return -ENOMEM;
3492
Chris Masonb9473432009-03-13 11:00:37 -04003493 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003494 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3495 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003496 if (ret) {
3497 if (ret > 0)
3498 ret = -ENOENT;
3499 goto failed;
3500 }
3501
Chris Masonb4ce94d2009-02-04 09:25:08 -05003502 btrfs_unlock_up_safe(path, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003503 leaf = path->nodes[0];
3504 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003505 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003506
Chris Masone02119d2008-09-05 16:13:11 -04003507 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003508 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003509 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003510 ret = 0;
3511failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003512 btrfs_free_path(path);
3513 return ret;
3514}
3515
Chris Masond352ac62008-09-29 15:18:18 -04003516/*
Chris Mason21151332011-11-10 20:39:08 -05003517 * copy everything in the in-memory inode into the btree.
3518 */
3519noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3520 struct btrfs_root *root, struct inode *inode)
3521{
3522 int ret;
3523
3524 /*
3525 * If the inode is a free space inode, we can deadlock during commit
3526 * if we put it into the delayed code.
3527 *
3528 * The data relocation inode should also be directly updated
3529 * without delay
3530 */
Liu Bo83eea1f2012-07-10 05:28:39 -06003531 if (!btrfs_is_free_space_inode(inode)
Chris Mason21151332011-11-10 20:39:08 -05003532 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003533 btrfs_update_root_times(trans, root);
3534
Chris Mason21151332011-11-10 20:39:08 -05003535 ret = btrfs_delayed_update_inode(trans, root, inode);
3536 if (!ret)
3537 btrfs_set_inode_last_trans(trans, inode);
3538 return ret;
3539 }
3540
3541 return btrfs_update_inode_item(trans, root, inode);
3542}
3543
Josef Bacikbe6aef62012-10-22 15:43:12 -04003544noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3545 struct btrfs_root *root,
3546 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003547{
3548 int ret;
3549
3550 ret = btrfs_update_inode(trans, root, inode);
3551 if (ret == -ENOSPC)
3552 return btrfs_update_inode_item(trans, root, inode);
3553 return ret;
3554}
3555
3556/*
Chris Masond352ac62008-09-29 15:18:18 -04003557 * unlink helper that gets used here in inode.c and in the tree logging
3558 * recovery code. It remove a link in a directory with a given name, and
3559 * also drops the back refs in the inode to the directory
3560 */
Al Viro92986792011-03-04 17:14:37 +00003561static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3562 struct btrfs_root *root,
3563 struct inode *dir, struct inode *inode,
3564 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003565{
3566 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003567 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003568 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003569 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003570 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003571 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003572 u64 ino = btrfs_ino(inode);
3573 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003574
3575 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003576 if (!path) {
3577 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003578 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003579 }
3580
Chris Masonb9473432009-03-13 11:00:37 -04003581 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003582 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003583 name, name_len, -1);
3584 if (IS_ERR(di)) {
3585 ret = PTR_ERR(di);
3586 goto err;
3587 }
3588 if (!di) {
3589 ret = -ENOENT;
3590 goto err;
3591 }
Chris Mason5f39d392007-10-15 16:14:19 -04003592 leaf = path->nodes[0];
3593 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003594 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003595 if (ret)
3596 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003597 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003598
Li Zefan33345d012011-04-20 10:31:50 +08003599 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3600 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003601 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003602 btrfs_info(root->fs_info,
3603 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003604 name_len, name, ino, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003605 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003606 goto err;
3607 }
3608
Miao Xie16cdcec2011-04-22 18:12:22 +08003609 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003610 if (ret) {
3611 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003612 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003613 }
Chris Mason39279cc2007-06-12 06:35:45 -04003614
Chris Masone02119d2008-09-05 16:13:11 -04003615 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08003616 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003617 if (ret != 0 && ret != -ENOENT) {
3618 btrfs_abort_transaction(trans, root, ret);
3619 goto err;
3620 }
Chris Masone02119d2008-09-05 16:13:11 -04003621
3622 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
3623 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04003624 if (ret == -ENOENT)
3625 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003626 else if (ret)
3627 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003628err:
3629 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003630 if (ret)
3631 goto out;
3632
3633 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003634 inode_inc_iversion(inode);
3635 inode_inc_iversion(dir);
Chris Masone02119d2008-09-05 16:13:11 -04003636 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Tsutomu Itohb9959292012-06-25 21:25:22 -06003637 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04003638out:
Chris Mason39279cc2007-06-12 06:35:45 -04003639 return ret;
3640}
3641
Al Viro92986792011-03-04 17:14:37 +00003642int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3643 struct btrfs_root *root,
3644 struct inode *dir, struct inode *inode,
3645 const char *name, int name_len)
3646{
3647 int ret;
3648 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3649 if (!ret) {
Zach Brown8b558c52013-10-16 12:10:34 -07003650 drop_nlink(inode);
Al Viro92986792011-03-04 17:14:37 +00003651 ret = btrfs_update_inode(trans, root, inode);
3652 }
3653 return ret;
3654}
Yan, Zhenga22285a2010-05-16 10:48:46 -04003655
3656/*
3657 * helper to start transaction for unlink and rmdir.
3658 *
Josef Bacikd52be812013-05-29 14:54:47 -04003659 * unlink and rmdir are special in btrfs, they do not always free space, so
3660 * if we cannot make our reservations the normal way try and see if there is
3661 * plenty of slack room in the global reserve to migrate, otherwise we cannot
3662 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04003663 */
Josef Bacikd52be812013-05-29 14:54:47 -04003664static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003665{
3666 struct btrfs_trans_handle *trans;
3667 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003668 int ret;
3669
Josef Bacike70bea52011-10-11 14:18:24 -04003670 /*
3671 * 1 for the possible orphan item
3672 * 1 for the dir item
3673 * 1 for the dir index
3674 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04003675 * 1 for the inode
3676 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04003677 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003678 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
3679 return trans;
3680
Josef Bacikd52be812013-05-29 14:54:47 -04003681 if (PTR_ERR(trans) == -ENOSPC) {
3682 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003683
Josef Bacikd52be812013-05-29 14:54:47 -04003684 trans = btrfs_start_transaction(root, 0);
3685 if (IS_ERR(trans))
3686 return trans;
3687 ret = btrfs_cond_migrate_bytes(root->fs_info,
3688 &root->fs_info->trans_block_rsv,
3689 num_bytes, 5);
3690 if (ret) {
3691 btrfs_end_transaction(trans, root);
3692 return ERR_PTR(ret);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003693 }
Josef Bacik5a77d762011-11-01 14:32:23 -04003694 trans->block_rsv = &root->fs_info->trans_block_rsv;
Josef Bacikd52be812013-05-29 14:54:47 -04003695 trans->bytes_reserved = num_bytes;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003696 }
Josef Bacikd52be812013-05-29 14:54:47 -04003697 return trans;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003698}
3699
Chris Mason39279cc2007-06-12 06:35:45 -04003700static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3701{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003702 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003703 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04003704 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003705 int ret;
3706
Josef Bacikd52be812013-05-29 14:54:47 -04003707 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003708 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003709 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003710
Chris Mason12fcfd22009-03-24 10:24:20 -04003711 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
3712
Chris Masone02119d2008-09-05 16:13:11 -04003713 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3714 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003715 if (ret)
3716 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003717
Yan, Zhenga22285a2010-05-16 10:48:46 -04003718 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04003719 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003720 if (ret)
3721 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003722 }
Josef Bacik7b128762008-07-24 12:17:14 -04003723
Tsutomu Itohb5324022011-07-19 07:27:20 +00003724out:
Josef Bacikd52be812013-05-29 14:54:47 -04003725 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003726 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04003727 return ret;
3728}
3729
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003730int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3731 struct btrfs_root *root,
3732 struct inode *dir, u64 objectid,
3733 const char *name, int name_len)
3734{
3735 struct btrfs_path *path;
3736 struct extent_buffer *leaf;
3737 struct btrfs_dir_item *di;
3738 struct btrfs_key key;
3739 u64 index;
3740 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003741 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003742
3743 path = btrfs_alloc_path();
3744 if (!path)
3745 return -ENOMEM;
3746
Li Zefan33345d012011-04-20 10:31:50 +08003747 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003748 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003749 if (IS_ERR_OR_NULL(di)) {
3750 if (!di)
3751 ret = -ENOENT;
3752 else
3753 ret = PTR_ERR(di);
3754 goto out;
3755 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003756
3757 leaf = path->nodes[0];
3758 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3759 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3760 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003761 if (ret) {
3762 btrfs_abort_transaction(trans, root, ret);
3763 goto out;
3764 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003765 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003766
3767 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3768 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003769 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003770 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003771 if (ret != -ENOENT) {
3772 btrfs_abort_transaction(trans, root, ret);
3773 goto out;
3774 }
Li Zefan33345d012011-04-20 10:31:50 +08003775 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003776 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003777 if (IS_ERR_OR_NULL(di)) {
3778 if (!di)
3779 ret = -ENOENT;
3780 else
3781 ret = PTR_ERR(di);
3782 btrfs_abort_transaction(trans, root, ret);
3783 goto out;
3784 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003785
3786 leaf = path->nodes[0];
3787 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003788 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003789 index = key.offset;
3790 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003791 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003792
Miao Xie16cdcec2011-04-22 18:12:22 +08003793 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003794 if (ret) {
3795 btrfs_abort_transaction(trans, root, ret);
3796 goto out;
3797 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003798
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003799 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003800 inode_inc_iversion(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003801 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Josef Bacik5a24e842012-08-08 10:12:59 -06003802 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003803 if (ret)
3804 btrfs_abort_transaction(trans, root, ret);
3805out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04003806 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003807 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003808}
3809
Chris Mason39279cc2007-06-12 06:35:45 -04003810static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3811{
3812 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003813 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003814 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003815 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04003816
David Sterbab3ae2442012-09-13 16:04:34 -06003817 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04003818 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06003819 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
3820 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04003821
Josef Bacikd52be812013-05-29 14:54:47 -04003822 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003823 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003824 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003825
Li Zefan33345d012011-04-20 10:31:50 +08003826 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003827 err = btrfs_unlink_subvol(trans, root, dir,
3828 BTRFS_I(inode)->location.objectid,
3829 dentry->d_name.name,
3830 dentry->d_name.len);
3831 goto out;
3832 }
3833
Josef Bacik7b128762008-07-24 12:17:14 -04003834 err = btrfs_orphan_add(trans, inode);
3835 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003836 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003837
Chris Mason39279cc2007-06-12 06:35:45 -04003838 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04003839 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3840 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05003841 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04003842 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003843out:
Josef Bacikd52be812013-05-29 14:54:47 -04003844 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003845 btrfs_btree_balance_dirty(root);
Chris Mason39544012007-12-12 14:38:19 -05003846
Chris Mason39279cc2007-06-12 06:35:45 -04003847 return err;
3848}
3849
Chris Mason323ac952008-10-01 19:05:46 -04003850/*
Chris Mason39279cc2007-06-12 06:35:45 -04003851 * this can truncate away extent items, csum items and directory items.
3852 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04003853 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04003854 *
3855 * csum items that cross the new i_size are truncated to the new size
3856 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04003857 *
3858 * min_type is the minimum key type to truncate down to. If set to 0, this
3859 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04003860 */
Yan, Zheng80825102009-11-12 09:35:36 +00003861int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
3862 struct btrfs_root *root,
3863 struct inode *inode,
3864 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003865{
Chris Mason39279cc2007-06-12 06:35:45 -04003866 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003867 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003868 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00003869 struct btrfs_key key;
3870 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04003871 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04003872 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003873 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003874 u64 item_end = 0;
Josef Bacik7f4f6e02013-08-29 16:43:28 -04003875 u64 last_size = (u64)-1;
Yan, Zheng80825102009-11-12 09:35:36 +00003876 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04003877 int found_extent;
3878 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003879 int pending_del_nr = 0;
3880 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04003881 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00003882 int ret;
3883 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08003884 u64 ino = btrfs_ino(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003885
3886 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04003887
Mark Fasheh0eb0e192011-07-12 16:44:10 -07003888 path = btrfs_alloc_path();
3889 if (!path)
3890 return -ENOMEM;
3891 path->reada = -1;
3892
Josef Bacik5dc562c2012-08-17 13:14:17 -04003893 /*
3894 * We want to drop from the next block forward in case this new size is
3895 * not block aligned since we will be keeping the last block of the
3896 * extent just the way it is.
3897 */
Josef Bacik0af3d002010-06-21 14:48:16 -04003898 if (root->ref_cows || root == root->fs_info->tree_root)
Qu Wenruofda28322013-02-26 08:10:22 +00003899 btrfs_drop_extent_cache(inode, ALIGN(new_size,
3900 root->sectorsize), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003901
Miao Xie16cdcec2011-04-22 18:12:22 +08003902 /*
3903 * This function is also used to drop the items in the log tree before
3904 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
3905 * it is used to drop the loged items. So we shouldn't kill the delayed
3906 * items.
3907 */
3908 if (min_type == 0 && root == BTRFS_I(inode)->root)
3909 btrfs_kill_delayed_inode_items(inode);
3910
Li Zefan33345d012011-04-20 10:31:50 +08003911 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04003912 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04003913 key.type = (u8)-1;
3914
Chris Mason85e21ba2008-01-29 15:11:36 -05003915search_again:
Chris Masonb9473432009-03-13 11:00:37 -04003916 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05003917 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00003918 if (ret < 0) {
3919 err = ret;
3920 goto out;
3921 }
Chris Masond3977122009-01-05 21:25:51 -05003922
Chris Mason85e21ba2008-01-29 15:11:36 -05003923 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003924 /* there are no items in the tree for us to truncate, we're
3925 * done
3926 */
Yan, Zheng80825102009-11-12 09:35:36 +00003927 if (path->slots[0] == 0)
3928 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05003929 path->slots[0]--;
3930 }
3931
Chris Masond3977122009-01-05 21:25:51 -05003932 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04003933 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04003934 leaf = path->nodes[0];
3935 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3936 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04003937
Li Zefan33345d012011-04-20 10:31:50 +08003938 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04003939 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003940
Chris Mason85e21ba2008-01-29 15:11:36 -05003941 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003942 break;
3943
Chris Mason5f39d392007-10-15 16:14:19 -04003944 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04003945 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04003946 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04003947 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04003948 extent_type = btrfs_file_extent_type(leaf, fi);
3949 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04003950 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04003951 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04003952 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04003953 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04003954 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003955 }
Yan008630c2007-11-07 13:31:09 -05003956 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04003957 }
Yan, Zheng80825102009-11-12 09:35:36 +00003958 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04003959 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00003960 } else {
3961 if (item_end < new_size)
3962 break;
3963 if (found_key.offset >= new_size)
3964 del_item = 1;
3965 else
3966 del_item = 0;
3967 }
Chris Mason39279cc2007-06-12 06:35:45 -04003968 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003969 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04003970 if (found_type != BTRFS_EXTENT_DATA_KEY)
3971 goto delete;
3972
Josef Bacik7f4f6e02013-08-29 16:43:28 -04003973 if (del_item)
3974 last_size = found_key.offset;
3975 else
3976 last_size = new_size;
3977
Chris Mason179e29e2007-11-01 11:28:41 -04003978 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04003979 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04003980 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05003981 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04003982 u64 orig_num_bytes =
3983 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00003984 extent_num_bytes = ALIGN(new_size -
3985 found_key.offset,
3986 root->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04003987 btrfs_set_file_extent_num_bytes(leaf, fi,
3988 extent_num_bytes);
3989 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05003990 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04003991 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003992 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04003993 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003994 } else {
Chris Masondb945352007-10-15 16:15:53 -04003995 extent_num_bytes =
3996 btrfs_file_extent_disk_num_bytes(leaf,
3997 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003998 extent_offset = found_key.offset -
3999 btrfs_file_extent_offset(leaf, fi);
4000
Chris Mason39279cc2007-06-12 06:35:45 -04004001 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004002 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004003 if (extent_start != 0) {
4004 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04004005 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004006 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004007 }
4008 }
Chris Mason90692182008-02-08 13:49:28 -05004009 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004010 /*
4011 * we can't truncate inline items that have had
4012 * special encodings
4013 */
4014 if (!del_item &&
4015 btrfs_file_extent_compression(leaf, fi) == 0 &&
4016 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4017 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004018 u32 size = new_size - found_key.offset;
4019
4020 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004021 inode_sub_bytes(inode, item_end + 1 -
4022 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04004023 }
4024 size =
4025 btrfs_file_extent_calc_inline_size(size);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004026 btrfs_truncate_item(root, path, size, 1);
Chris Masone02119d2008-09-05 16:13:11 -04004027 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004028 inode_sub_bytes(inode, item_end + 1 -
4029 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05004030 }
Chris Mason39279cc2007-06-12 06:35:45 -04004031 }
Chris Mason179e29e2007-11-01 11:28:41 -04004032delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004033 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004034 if (!pending_del_nr) {
4035 /* no pending yet, add ourselves */
4036 pending_del_slot = path->slots[0];
4037 pending_del_nr = 1;
4038 } else if (pending_del_nr &&
4039 path->slots[0] + 1 == pending_del_slot) {
4040 /* hop on the pending chunk */
4041 pending_del_nr++;
4042 pending_del_slot = path->slots[0];
4043 } else {
Chris Masond3977122009-01-05 21:25:51 -05004044 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004045 }
Chris Mason39279cc2007-06-12 06:35:45 -04004046 } else {
4047 break;
4048 }
Josef Bacik0af3d002010-06-21 14:48:16 -04004049 if (found_extent && (root->ref_cows ||
4050 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004051 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004052 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004053 extent_num_bytes, 0,
4054 btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02004055 ino, extent_offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004056 BUG_ON(ret);
4057 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004058
Yan, Zheng80825102009-11-12 09:35:36 +00004059 if (found_type == BTRFS_INODE_ITEM_KEY)
4060 break;
4061
4062 if (path->slots[0] == 0 ||
4063 path->slots[0] != pending_del_slot) {
Yan, Zheng80825102009-11-12 09:35:36 +00004064 if (pending_del_nr) {
4065 ret = btrfs_del_items(trans, root, path,
4066 pending_del_slot,
4067 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004068 if (ret) {
4069 btrfs_abort_transaction(trans,
4070 root, ret);
4071 goto error;
4072 }
Yan, Zheng80825102009-11-12 09:35:36 +00004073 pending_del_nr = 0;
4074 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004075 btrfs_release_path(path);
Chris Mason85e21ba2008-01-29 15:11:36 -05004076 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004077 } else {
4078 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004079 }
Chris Mason39279cc2007-06-12 06:35:45 -04004080 }
Yan, Zheng80825102009-11-12 09:35:36 +00004081out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004082 if (pending_del_nr) {
4083 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4084 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004085 if (ret)
4086 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004087 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004088error:
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004089 if (last_size != (u64)-1)
4090 btrfs_ordered_update_i_size(inode, last_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004091 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00004092 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004093}
4094
Chris Masona52d9a82007-08-27 16:49:44 -04004095/*
Josef Bacik2aaa6652012-08-29 14:27:18 -04004096 * btrfs_truncate_page - read, zero a chunk and write a page
4097 * @inode - inode that we're zeroing
4098 * @from - the offset to start zeroing
4099 * @len - the length to zero, 0 to zero the entire range respective to the
4100 * offset
4101 * @front - zero up to the offset instead of from the offset on
4102 *
4103 * This will find the page for the "from" offset and cow the page and zero the
4104 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004105 */
Josef Bacik2aaa6652012-08-29 14:27:18 -04004106int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
4107 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004108{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004109 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04004110 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004111 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4112 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004113 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004114 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04004115 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04004116 pgoff_t index = from >> PAGE_CACHE_SHIFT;
4117 unsigned offset = from & (PAGE_CACHE_SIZE-1);
4118 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004119 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004120 int ret = 0;
4121 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004122 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004123
Josef Bacik2aaa6652012-08-29 14:27:18 -04004124 if ((offset & (blocksize - 1)) == 0 &&
4125 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004126 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004127 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004128 if (ret)
4129 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004130
Chris Mason211c17f2008-05-15 09:13:45 -04004131again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004132 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004133 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004134 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Miao Xieac6a2b32012-12-05 10:56:13 +00004135 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004136 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004137 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004138
4139 page_start = page_offset(page);
4140 page_end = page_start + PAGE_CACHE_SIZE - 1;
4141
Chris Masona52d9a82007-08-27 16:49:44 -04004142 if (!PageUptodate(page)) {
4143 ret = btrfs_readpage(NULL, page);
4144 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004145 if (page->mapping != mapping) {
4146 unlock_page(page);
4147 page_cache_release(page);
4148 goto again;
4149 }
Chris Masona52d9a82007-08-27 16:49:44 -04004150 if (!PageUptodate(page)) {
4151 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004152 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004153 }
4154 }
Chris Mason211c17f2008-05-15 09:13:45 -04004155 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004156
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004157 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004158 set_page_extent_mapped(page);
4159
4160 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4161 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004162 unlock_extent_cached(io_tree, page_start, page_end,
4163 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004164 unlock_page(page);
4165 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004166 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004167 btrfs_put_ordered_extent(ordered);
4168 goto again;
4169 }
4170
Josef Bacik2ac55d42010-02-03 19:33:23 +00004171 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004172 EXTENT_DIRTY | EXTENT_DELALLOC |
4173 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004174 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004175
Josef Bacik2ac55d42010-02-03 19:33:23 +00004176 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
4177 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004178 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004179 unlock_extent_cached(io_tree, page_start, page_end,
4180 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004181 goto out_unlock;
4182 }
4183
Chris Masone6dcd2d2008-07-17 12:53:50 -04004184 if (offset != PAGE_CACHE_SIZE) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004185 if (!len)
4186 len = PAGE_CACHE_SIZE - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004187 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004188 if (front)
4189 memset(kaddr, 0, offset);
4190 else
4191 memset(kaddr + offset, 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004192 flush_dcache_page(page);
4193 kunmap(page);
4194 }
Chris Mason247e7432008-07-17 12:53:51 -04004195 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004196 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004197 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
4198 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004199
Chris Mason89642222008-07-24 09:41:53 -04004200out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004201 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004202 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04004203 unlock_page(page);
4204 page_cache_release(page);
4205out:
4206 return ret;
4207}
4208
Josef Bacik695a0d02011-03-04 15:46:53 -05004209/*
4210 * This function puts in dummy file extents for the area we're creating a hole
4211 * for. So if we are truncating this file to a larger size we need to insert
4212 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4213 * the range between oldsize and size
4214 */
Josef Bacika41ad392011-01-31 15:30:16 -05004215int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004216{
4217 struct btrfs_trans_handle *trans;
4218 struct btrfs_root *root = BTRFS_I(inode)->root;
4219 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004220 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004221 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004222 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Qu Wenruofda28322013-02-26 08:10:22 +00004223 u64 hole_start = ALIGN(oldsize, root->sectorsize);
4224 u64 block_end = ALIGN(size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004225 u64 last_byte;
4226 u64 cur_offset;
4227 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004228 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004229
Josef Bacika71754f2013-06-17 17:14:39 -04004230 /*
4231 * If our size started in the middle of a page we need to zero out the
4232 * rest of the page before we expand the i_size, otherwise we could
4233 * expose stale data.
4234 */
4235 err = btrfs_truncate_page(inode, oldsize, 0, 0);
4236 if (err)
4237 return err;
4238
Yan Zheng9036c102008-10-30 14:19:41 -04004239 if (size <= hole_start)
4240 return 0;
4241
Yan Zheng9036c102008-10-30 14:19:41 -04004242 while (1) {
4243 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004244
Josef Bacik2ac55d42010-02-03 19:33:23 +00004245 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004246 &cached_state);
Miao Xiefa7c1492013-09-26 13:15:27 +08004247 ordered = btrfs_lookup_ordered_range(inode, hole_start,
4248 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004249 if (!ordered)
4250 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004251 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4252 &cached_state, GFP_NOFS);
Miao Xiefa7c1492013-09-26 13:15:27 +08004253 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004254 btrfs_put_ordered_extent(ordered);
4255 }
4256
Yan Zheng9036c102008-10-30 14:19:41 -04004257 cur_offset = hole_start;
4258 while (1) {
4259 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4260 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004261 if (IS_ERR(em)) {
4262 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004263 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004264 break;
4265 }
Yan Zheng9036c102008-10-30 14:19:41 -04004266 last_byte = min(extent_map_end(em), block_end);
Qu Wenruofda28322013-02-26 08:10:22 +00004267 last_byte = ALIGN(last_byte , root->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004268 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004269 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004270 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004271
Miao Xie36423202011-12-14 20:12:02 -05004272 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004273 if (IS_ERR(trans)) {
4274 err = PTR_ERR(trans);
Chris Mason771ed682008-11-06 22:02:51 -05004275 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004276 }
Yan, Zheng80825102009-11-12 09:35:36 +00004277
Josef Bacik5dc562c2012-08-17 13:14:17 -04004278 err = btrfs_drop_extents(trans, root, inode,
4279 cur_offset,
Josef Bacik26714852012-08-29 12:24:27 -04004280 cur_offset + hole_size, 1);
Miao Xie5b397372011-09-11 10:52:24 -04004281 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004282 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04004283 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05004284 break;
Miao Xie5b397372011-09-11 10:52:24 -04004285 }
Josef Bacik9ed74f22009-09-11 16:12:44 -04004286
Yan Zheng9036c102008-10-30 14:19:41 -04004287 err = btrfs_insert_file_extent(trans, root,
Li Zefan33345d012011-04-20 10:31:50 +08004288 btrfs_ino(inode), cur_offset, 0,
Yan Zheng9036c102008-10-30 14:19:41 -04004289 0, hole_size, 0, hole_size,
4290 0, 0, 0);
Miao Xie5b397372011-09-11 10:52:24 -04004291 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004292 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04004293 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05004294 break;
Miao Xie5b397372011-09-11 10:52:24 -04004295 }
Yan, Zheng80825102009-11-12 09:35:36 +00004296
Josef Bacik5dc562c2012-08-17 13:14:17 -04004297 btrfs_drop_extent_cache(inode, cur_offset,
4298 cur_offset + hole_size - 1, 0);
4299 hole_em = alloc_extent_map();
4300 if (!hole_em) {
4301 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4302 &BTRFS_I(inode)->runtime_flags);
4303 goto next;
4304 }
4305 hole_em->start = cur_offset;
4306 hole_em->len = hole_size;
4307 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004308
Josef Bacik5dc562c2012-08-17 13:14:17 -04004309 hole_em->block_start = EXTENT_MAP_HOLE;
4310 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004311 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004312 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004313 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
4314 hole_em->compress_type = BTRFS_COMPRESS_NONE;
4315 hole_em->generation = trans->transid;
4316
4317 while (1) {
4318 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004319 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004320 write_unlock(&em_tree->lock);
4321 if (err != -EEXIST)
4322 break;
4323 btrfs_drop_extent_cache(inode, cur_offset,
4324 cur_offset +
4325 hole_size - 1, 0);
4326 }
4327 free_extent_map(hole_em);
4328next:
Miao Xie36423202011-12-14 20:12:02 -05004329 btrfs_update_inode(trans, root, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004330 btrfs_end_transaction(trans, root);
Yan Zheng9036c102008-10-30 14:19:41 -04004331 }
4332 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004333 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004334 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004335 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004336 break;
4337 }
4338
Yan, Zhenga22285a2010-05-16 10:48:46 -04004339 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004340 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4341 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004342 return err;
4343}
4344
Eric Sandeen3972f262013-01-12 02:57:22 +00004345static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004346{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004347 struct btrfs_root *root = BTRFS_I(inode)->root;
4348 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004349 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004350 loff_t newsize = attr->ia_size;
4351 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004352 int ret;
4353
Eric Sandeen3972f262013-01-12 02:57:22 +00004354 /*
4355 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4356 * special case where we need to update the times despite not having
4357 * these flags set. For all other operations the VFS set these flags
4358 * explicitly if it wants a timestamp update.
4359 */
4360 if (newsize != oldsize && (!(mask & (ATTR_CTIME | ATTR_MTIME))))
4361 inode->i_ctime = inode->i_mtime = current_fs_time(inode->i_sb);
4362
Josef Bacika41ad392011-01-31 15:30:16 -05004363 if (newsize > oldsize) {
Kirill A. Shutemov7caef262013-09-12 15:13:56 -07004364 truncate_pagecache(inode, newsize);
Josef Bacika41ad392011-01-31 15:30:16 -05004365 ret = btrfs_cont_expand(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004366 if (ret)
Yan, Zheng80825102009-11-12 09:35:36 +00004367 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004368
Miao Xief4a2f4c2011-12-14 20:12:01 -05004369 trans = btrfs_start_transaction(root, 1);
4370 if (IS_ERR(trans))
4371 return PTR_ERR(trans);
4372
4373 i_size_write(inode, newsize);
4374 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
4375 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004376 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05004377 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004378
Josef Bacika41ad392011-01-31 15:30:16 -05004379 /*
4380 * We're truncating a file that used to have good data down to
4381 * zero. Make sure it gets into the ordered flush list so that
4382 * any new writes get down to disk quickly.
4383 */
4384 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004385 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4386 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004387
Josef Bacikf3fe8202013-01-07 17:03:21 -05004388 /*
4389 * 1 for the orphan item we're going to add
4390 * 1 for the orphan item deletion.
4391 */
4392 trans = btrfs_start_transaction(root, 2);
4393 if (IS_ERR(trans))
4394 return PTR_ERR(trans);
4395
4396 /*
4397 * We need to do this in case we fail at _any_ point during the
4398 * actual truncate. Once we do the truncate_setsize we could
4399 * invalidate pages which forces any outstanding ordered io to
4400 * be instantly completed which will give us extents that need
4401 * to be truncated. If we fail to get an orphan inode down we
4402 * could have left over extents that were never meant to live,
4403 * so we need to garuntee from this point on that everything
4404 * will be consistent.
4405 */
4406 ret = btrfs_orphan_add(trans, inode);
4407 btrfs_end_transaction(trans, root);
4408 if (ret)
4409 return ret;
4410
Josef Bacika41ad392011-01-31 15:30:16 -05004411 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4412 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004413
4414 /* Disable nonlocked read DIO to avoid the end less truncate */
4415 btrfs_inode_block_unlocked_dio(inode);
4416 inode_dio_wait(inode);
4417 btrfs_inode_resume_unlocked_dio(inode);
4418
Josef Bacika41ad392011-01-31 15:30:16 -05004419 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004420 if (ret && inode->i_nlink) {
4421 int err;
4422
4423 /*
4424 * failed to truncate, disk_i_size is only adjusted down
4425 * as we remove extents, so it should represent the true
4426 * size of the inode, so reset the in memory size and
4427 * delete our orphan entry.
4428 */
4429 trans = btrfs_join_transaction(root);
4430 if (IS_ERR(trans)) {
4431 btrfs_orphan_del(NULL, inode);
4432 return ret;
4433 }
4434 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
4435 err = btrfs_orphan_del(trans, inode);
4436 if (err)
4437 btrfs_abort_transaction(trans, root, err);
4438 btrfs_end_transaction(trans, root);
4439 }
Yan, Zheng80825102009-11-12 09:35:36 +00004440 }
4441
Josef Bacika41ad392011-01-31 15:30:16 -05004442 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004443}
4444
Chris Mason39279cc2007-06-12 06:35:45 -04004445static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
4446{
4447 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08004448 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004449 int err;
4450
Li Zefanb83cc962010-12-20 16:04:08 +08004451 if (btrfs_root_readonly(root))
4452 return -EROFS;
4453
Chris Mason39279cc2007-06-12 06:35:45 -04004454 err = inode_change_ok(inode, attr);
4455 if (err)
4456 return err;
4457
Chris Mason5a3f23d2009-03-31 13:27:11 -04004458 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00004459 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00004460 if (err)
4461 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004462 }
Yan Zheng9036c102008-10-30 14:19:41 -04004463
Christoph Hellwig10257742010-06-04 11:30:02 +02004464 if (attr->ia_valid) {
4465 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004466 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004467 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04004468
Josef Bacik22c44fe2011-11-30 10:45:38 -05004469 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig10257742010-06-04 11:30:02 +02004470 err = btrfs_acl_chmod(inode);
4471 }
4472
Chris Mason39279cc2007-06-12 06:35:45 -04004473 return err;
4474}
Chris Mason61295eb2008-01-14 16:24:38 -05004475
Al Virobd555972010-06-07 11:35:40 -04004476void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004477{
4478 struct btrfs_trans_handle *trans;
4479 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04004480 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik07127182011-08-19 10:29:59 -04004481 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04004482 int ret;
4483
liubo1abe9b82011-03-24 11:18:59 +00004484 trace_btrfs_inode_evict(inode);
4485
Chris Mason39279cc2007-06-12 06:35:45 -04004486 truncate_inode_pages(&inode->i_data, 0);
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02004487 if (inode->i_nlink &&
4488 ((btrfs_root_refs(&root->root_item) != 0 &&
4489 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
4490 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04004491 goto no_delete;
4492
Chris Mason39279cc2007-06-12 06:35:45 -04004493 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04004494 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004495 goto no_delete;
4496 }
Al Virobd555972010-06-07 11:35:40 -04004497 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04004498 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04004499
Yan, Zhengc71bf092009-11-12 09:34:40 +00004500 if (root->fs_info->log_root_recovering) {
Liu Bo6bf02312012-06-25 21:59:09 -06004501 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04004502 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00004503 goto no_delete;
4504 }
4505
Yan, Zheng76dda932009-09-21 16:00:26 -04004506 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02004507 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
4508 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04004509 goto no_delete;
4510 }
4511
Miao Xie0e8c36a2012-12-19 06:59:51 +00004512 ret = btrfs_commit_inode_delayed_inode(inode);
4513 if (ret) {
4514 btrfs_orphan_del(NULL, inode);
4515 goto no_delete;
4516 }
4517
Miao Xie66d8f3d2012-09-06 04:02:28 -06004518 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04004519 if (!rsv) {
4520 btrfs_orphan_del(NULL, inode);
4521 goto no_delete;
4522 }
Josef Bacik4a338542011-08-29 11:01:31 -04004523 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04004524 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04004525 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04004526
Chris Masondbe674a2008-07-17 12:54:05 -04004527 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04004528
Josef Bacik4289a662011-08-05 13:22:24 -04004529 /*
Miao Xie8407aa42012-09-07 01:43:32 -06004530 * This is a bit simpler than btrfs_truncate since we've already
4531 * reserved our space for our orphan item in the unlink, so we just
4532 * need to reserve some slack space in case we add bytes and update
4533 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04004534 */
Yan, Zheng80825102009-11-12 09:35:36 +00004535 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00004536 ret = btrfs_block_rsv_refill(root, rsv, min_size,
4537 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00004538
Josef Bacik726c35f2011-09-26 15:46:06 -04004539 /*
4540 * Try and steal from the global reserve since we will
4541 * likely not use this space anyway, we want to try as
4542 * hard as possible to get this to work.
4543 */
4544 if (ret)
4545 ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
4546
Yan, Zhengd68fc572010-05-16 10:49:58 -04004547 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004548 btrfs_warn(root->fs_info,
4549 "Could not get space for a delete, will truncate on mount %d",
4550 ret);
Josef Bacik4289a662011-08-05 13:22:24 -04004551 btrfs_orphan_del(NULL, inode);
4552 btrfs_free_block_rsv(root, rsv);
4553 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04004554 }
4555
Miao Xie0e8c36a2012-12-19 06:59:51 +00004556 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04004557 if (IS_ERR(trans)) {
4558 btrfs_orphan_del(NULL, inode);
4559 btrfs_free_block_rsv(root, rsv);
4560 goto no_delete;
4561 }
4562
4563 trans->block_rsv = rsv;
4564
Yan, Zhengd68fc572010-05-16 10:49:58 -04004565 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04004566 if (ret != -ENOSPC)
Yan, Zheng80825102009-11-12 09:35:36 +00004567 break;
4568
Miao Xie8407aa42012-09-07 01:43:32 -06004569 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004570 btrfs_end_transaction(trans, root);
4571 trans = NULL;
Liu Bob53d3f52012-11-14 14:34:34 +00004572 btrfs_btree_balance_dirty(root);
Josef Bacik7b128762008-07-24 12:17:14 -04004573 }
4574
Josef Bacik4289a662011-08-05 13:22:24 -04004575 btrfs_free_block_rsv(root, rsv);
4576
Josef Bacik4ef31a42013-08-13 14:10:08 -04004577 /*
4578 * Errors here aren't a big deal, it just means we leave orphan items
4579 * in the tree. They will be cleaned up on the next mount.
4580 */
Yan, Zheng80825102009-11-12 09:35:36 +00004581 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04004582 trans->block_rsv = root->orphan_block_rsv;
Josef Bacik4ef31a42013-08-13 14:10:08 -04004583 btrfs_orphan_del(trans, inode);
4584 } else {
4585 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004586 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004587
Josef Bacik4289a662011-08-05 13:22:24 -04004588 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08004589 if (!(root == root->fs_info->tree_root ||
4590 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08004591 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08004592
Chris Mason54aa1f42007-06-22 14:16:25 -04004593 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004594 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04004595no_delete:
Miao Xie89042e52013-05-15 07:48:15 +00004596 btrfs_remove_delayed_node(inode);
Jan Karadbd57682012-05-03 14:48:02 +02004597 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004598 return;
Chris Mason39279cc2007-06-12 06:35:45 -04004599}
4600
4601/*
4602 * this returns the key found in the dir entry in the location pointer.
4603 * If no dir entries were found, location->objectid is 0.
4604 */
4605static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
4606 struct btrfs_key *location)
4607{
4608 const char *name = dentry->d_name.name;
4609 int namelen = dentry->d_name.len;
4610 struct btrfs_dir_item *di;
4611 struct btrfs_path *path;
4612 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04004613 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004614
4615 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07004616 if (!path)
4617 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05004618
Li Zefan33345d012011-04-20 10:31:50 +08004619 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04004620 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04004621 if (IS_ERR(di))
4622 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05004623
David Sterbac7040052011-04-19 18:00:01 +02004624 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05004625 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05004626
Chris Mason5f39d392007-10-15 16:14:19 -04004627 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04004628out:
Chris Mason39279cc2007-06-12 06:35:45 -04004629 btrfs_free_path(path);
4630 return ret;
Chris Mason39544012007-12-12 14:38:19 -05004631out_err:
4632 location->objectid = 0;
4633 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04004634}
4635
4636/*
4637 * when we hit a tree root in a directory, the btrfs part of the inode
4638 * needs to be changed to reflect the root directory of the tree root. This
4639 * is kind of like crossing a mount point.
4640 */
4641static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004642 struct inode *dir,
4643 struct dentry *dentry,
4644 struct btrfs_key *location,
4645 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04004646{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004647 struct btrfs_path *path;
4648 struct btrfs_root *new_root;
4649 struct btrfs_root_ref *ref;
4650 struct extent_buffer *leaf;
4651 int ret;
4652 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004653
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004654 path = btrfs_alloc_path();
4655 if (!path) {
4656 err = -ENOMEM;
4657 goto out;
4658 }
Chris Mason39279cc2007-06-12 06:35:45 -04004659
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004660 err = -ENOENT;
4661 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
4662 BTRFS_I(dir)->root->root_key.objectid,
4663 location->objectid);
4664 if (ret) {
4665 if (ret < 0)
4666 err = ret;
4667 goto out;
4668 }
Chris Mason39279cc2007-06-12 06:35:45 -04004669
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004670 leaf = path->nodes[0];
4671 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08004672 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004673 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
4674 goto out;
4675
4676 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
4677 (unsigned long)(ref + 1),
4678 dentry->d_name.len);
4679 if (ret)
4680 goto out;
4681
David Sterbab3b4aa72011-04-21 01:20:15 +02004682 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004683
4684 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
4685 if (IS_ERR(new_root)) {
4686 err = PTR_ERR(new_root);
4687 goto out;
4688 }
4689
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004690 *sub_root = new_root;
4691 location->objectid = btrfs_root_dirid(&new_root->root_item);
4692 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04004693 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004694 err = 0;
4695out:
4696 btrfs_free_path(path);
4697 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004698}
4699
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004700static void inode_tree_add(struct inode *inode)
4701{
4702 struct btrfs_root *root = BTRFS_I(inode)->root;
4703 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004704 struct rb_node **p;
4705 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01004706 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Li Zefan33345d012011-04-20 10:31:50 +08004707 u64 ino = btrfs_ino(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004708
Al Viro1d3382cb2010-10-23 15:19:20 -04004709 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004710 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00004711 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004712 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00004713 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004714 while (*p) {
4715 parent = *p;
4716 entry = rb_entry(parent, struct btrfs_inode, rb_node);
4717
Li Zefan33345d012011-04-20 10:31:50 +08004718 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004719 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004720 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004721 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004722 else {
4723 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04004724 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01004725 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004726 RB_CLEAR_NODE(parent);
4727 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01004728 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004729 }
4730 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01004731 rb_link_node(new, parent, p);
4732 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004733 spin_unlock(&root->inode_lock);
4734}
4735
4736static void inode_tree_del(struct inode *inode)
4737{
4738 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04004739 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004740
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004741 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004742 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004743 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004744 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04004745 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004746 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004747 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04004748
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02004749 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004750 synchronize_srcu(&root->fs_info->subvol_srcu);
4751 spin_lock(&root->inode_lock);
4752 empty = RB_EMPTY_ROOT(&root->inode_tree);
4753 spin_unlock(&root->inode_lock);
4754 if (empty)
4755 btrfs_add_dead_root(root);
4756 }
4757}
4758
Jeff Mahoney143bede2012-03-01 14:56:26 +01004759void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04004760{
4761 struct rb_node *node;
4762 struct rb_node *prev;
4763 struct btrfs_inode *entry;
4764 struct inode *inode;
4765 u64 objectid = 0;
4766
4767 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4768
4769 spin_lock(&root->inode_lock);
4770again:
4771 node = root->inode_tree.rb_node;
4772 prev = NULL;
4773 while (node) {
4774 prev = node;
4775 entry = rb_entry(node, struct btrfs_inode, rb_node);
4776
Li Zefan33345d012011-04-20 10:31:50 +08004777 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004778 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004779 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004780 node = node->rb_right;
4781 else
4782 break;
4783 }
4784 if (!node) {
4785 while (prev) {
4786 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004787 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004788 node = prev;
4789 break;
4790 }
4791 prev = rb_next(prev);
4792 }
4793 }
4794 while (node) {
4795 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004796 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04004797 inode = igrab(&entry->vfs_inode);
4798 if (inode) {
4799 spin_unlock(&root->inode_lock);
4800 if (atomic_read(&inode->i_count) > 1)
4801 d_prune_aliases(inode);
4802 /*
Al Viro45321ac2010-06-07 13:43:19 -04004803 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04004804 * the inode cache when its usage count
4805 * hits zero.
4806 */
4807 iput(inode);
4808 cond_resched();
4809 spin_lock(&root->inode_lock);
4810 goto again;
4811 }
4812
4813 if (cond_resched_lock(&root->inode_lock))
4814 goto again;
4815
4816 node = rb_next(node);
4817 }
4818 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004819}
4820
Chris Masone02119d2008-09-05 16:13:11 -04004821static int btrfs_init_locked_inode(struct inode *inode, void *p)
4822{
4823 struct btrfs_iget_args *args = p;
4824 inode->i_ino = args->ino;
Chris Masone02119d2008-09-05 16:13:11 -04004825 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004826 return 0;
4827}
4828
4829static int btrfs_find_actor(struct inode *inode, void *opaque)
4830{
4831 struct btrfs_iget_args *args = opaque;
Li Zefan33345d012011-04-20 10:31:50 +08004832 return args->ino == btrfs_ino(inode) &&
Chris Masond3977122009-01-05 21:25:51 -05004833 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004834}
4835
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004836static struct inode *btrfs_iget_locked(struct super_block *s,
4837 u64 objectid,
4838 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04004839{
4840 struct inode *inode;
4841 struct btrfs_iget_args args;
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01004842 unsigned long hashval = btrfs_inode_hash(objectid, root);
4843
Chris Mason39279cc2007-06-12 06:35:45 -04004844 args.ino = objectid;
4845 args.root = root;
4846
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01004847 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04004848 btrfs_init_locked_inode,
4849 (void *)&args);
4850 return inode;
4851}
4852
Balaji Rao1a54ef82008-07-21 02:01:04 +05304853/* Get an inode object given its location and corresponding root.
4854 * Returns in *is_new if the inode was read from disk
4855 */
4856struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00004857 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05304858{
4859 struct inode *inode;
4860
4861 inode = btrfs_iget_locked(s, location->objectid, root);
4862 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004863 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05304864
4865 if (inode->i_state & I_NEW) {
4866 BTRFS_I(inode)->root = root;
4867 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
4868 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07004869 if (!is_bad_inode(inode)) {
4870 inode_tree_add(inode);
4871 unlock_new_inode(inode);
4872 if (new)
4873 *new = 1;
4874 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04004875 unlock_new_inode(inode);
4876 iput(inode);
4877 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07004878 }
4879 }
4880
Balaji Rao1a54ef82008-07-21 02:01:04 +05304881 return inode;
4882}
4883
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004884static struct inode *new_simple_dir(struct super_block *s,
4885 struct btrfs_key *key,
4886 struct btrfs_root *root)
4887{
4888 struct inode *inode = new_inode(s);
4889
4890 if (!inode)
4891 return ERR_PTR(-ENOMEM);
4892
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004893 BTRFS_I(inode)->root = root;
4894 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04004895 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004896
4897 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08004898 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004899 inode->i_fop = &simple_dir_operations;
4900 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
4901 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
4902
4903 return inode;
4904}
4905
Chris Mason3de45862008-11-17 21:02:50 -05004906struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04004907{
Chris Masond3977122009-01-05 21:25:51 -05004908 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004909 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004910 struct btrfs_root *sub_root = root;
4911 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04004912 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004913 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004914
4915 if (dentry->d_name.len > BTRFS_NAME_LEN)
4916 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04004917
Jeff Layton39e3c952012-11-28 11:30:53 -05004918 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04004919 if (ret < 0)
4920 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04004921
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004922 if (location.objectid == 0)
4923 return NULL;
4924
4925 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00004926 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004927 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004928 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004929
4930 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
4931
Yan, Zheng76dda932009-09-21 16:00:26 -04004932 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004933 ret = fixup_tree_root_location(root, dir, dentry,
4934 &location, &sub_root);
4935 if (ret < 0) {
4936 if (ret != -ENOENT)
4937 inode = ERR_PTR(ret);
4938 else
4939 inode = new_simple_dir(dir->i_sb, &location, sub_root);
4940 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00004941 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004942 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004943 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
4944
Julia Lawall34d19ba2011-01-24 19:55:19 +00004945 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00004946 down_read(&root->fs_info->cleanup_work_sem);
4947 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004948 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00004949 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04004950 if (ret) {
4951 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004952 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04004953 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00004954 }
4955
Chris Mason3de45862008-11-17 21:02:50 -05004956 return inode;
4957}
4958
Nick Pigginfe15ce42011-01-07 17:49:23 +11004959static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04004960{
4961 struct btrfs_root *root;
Li Zefan848cce02012-02-21 17:04:28 +08004962 struct inode *inode = dentry->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04004963
Li Zefan848cce02012-02-21 17:04:28 +08004964 if (!inode && !IS_ROOT(dentry))
4965 inode = dentry->d_parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04004966
Li Zefan848cce02012-02-21 17:04:28 +08004967 if (inode) {
4968 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04004969 if (btrfs_root_refs(&root->root_item) == 0)
4970 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08004971
4972 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
4973 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04004974 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004975 return 0;
4976}
4977
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004978static void btrfs_dentry_release(struct dentry *dentry)
4979{
4980 if (dentry->d_fsdata)
4981 kfree(dentry->d_fsdata);
4982}
4983
Chris Mason3de45862008-11-17 21:02:50 -05004984static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04004985 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05004986{
Josef Bacika66e7cc2011-09-18 10:34:03 -04004987 struct dentry *ret;
4988
4989 ret = d_splice_alias(btrfs_lookup_dentry(dir, dentry), dentry);
Josef Bacika66e7cc2011-09-18 10:34:03 -04004990 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004991}
4992
Miao Xie16cdcec2011-04-22 18:12:22 +08004993unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04004994 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
4995};
4996
Al Viro9cdda8d2013-05-22 16:48:09 -04004997static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04004998{
Al Viro9cdda8d2013-05-22 16:48:09 -04004999 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005000 struct btrfs_root *root = BTRFS_I(inode)->root;
5001 struct btrfs_item *item;
5002 struct btrfs_dir_item *di;
5003 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005004 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005005 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005006 struct list_head ins_list;
5007 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005008 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005009 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005010 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005011 unsigned char d_type;
5012 int over = 0;
5013 u32 di_cur;
5014 u32 di_total;
5015 u32 di_len;
5016 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005017 char tmp_name[32];
5018 char *name_ptr;
5019 int name_len;
Al Viro9cdda8d2013-05-22 16:48:09 -04005020 int is_curr = 0; /* ctx->pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04005021
5022 /* FIXME, use a real flag for deciding about the key type */
5023 if (root->fs_info->tree_root == root)
5024 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005025
Al Viro9cdda8d2013-05-22 16:48:09 -04005026 if (!dir_emit_dots(file, ctx))
5027 return 0;
5028
David Woodhouse49593bf2008-08-17 17:08:36 +01005029 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005030 if (!path)
5031 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005032
Josef Bacik026fd312011-05-13 10:32:11 -04005033 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005034
Miao Xie16cdcec2011-04-22 18:12:22 +08005035 if (key_type == BTRFS_DIR_INDEX_KEY) {
5036 INIT_LIST_HEAD(&ins_list);
5037 INIT_LIST_HEAD(&del_list);
5038 btrfs_get_delayed_items(inode, &ins_list, &del_list);
5039 }
5040
Chris Mason39279cc2007-06-12 06:35:45 -04005041 btrfs_set_key_type(&key, key_type);
Al Viro9cdda8d2013-05-22 16:48:09 -04005042 key.offset = ctx->pos;
Li Zefan33345d012011-04-20 10:31:50 +08005043 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005044
Chris Mason39279cc2007-06-12 06:35:45 -04005045 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5046 if (ret < 0)
5047 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005048
5049 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005050 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005051 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005052 if (slot >= btrfs_header_nritems(leaf)) {
5053 ret = btrfs_next_leaf(root, path);
5054 if (ret < 0)
5055 goto err;
5056 else if (ret > 0)
5057 break;
5058 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005059 }
Chris Mason3de45862008-11-17 21:02:50 -05005060
Ross Kirkdd3cc162013-09-16 15:58:09 +01005061 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04005062 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5063
5064 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005065 break;
Chris Mason5f39d392007-10-15 16:14:19 -04005066 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04005067 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005068 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005069 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08005070 if (key_type == BTRFS_DIR_INDEX_KEY &&
5071 btrfs_should_delete_dir_index(&del_list,
5072 found_key.offset))
5073 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005074
Al Viro9cdda8d2013-05-22 16:48:09 -04005075 ctx->pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08005076 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005077
Chris Mason39279cc2007-06-12 06:35:45 -04005078 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
5079 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005080 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01005081
5082 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04005083 struct btrfs_key location;
5084
Josef Bacik22a94d42011-03-16 16:47:17 -04005085 if (verify_dir_item(root, leaf, di))
5086 break;
5087
Chris Mason5f39d392007-10-15 16:14:19 -04005088 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01005089 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04005090 name_ptr = tmp_name;
5091 } else {
5092 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01005093 if (!name_ptr) {
5094 ret = -ENOMEM;
5095 goto err;
5096 }
Chris Mason5f39d392007-10-15 16:14:19 -04005097 }
5098 read_extent_buffer(leaf, name_ptr,
5099 (unsigned long)(di + 1), name_len);
5100
5101 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5102 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05005103
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005104
Chris Mason3de45862008-11-17 21:02:50 -05005105 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01005106 * skip it.
5107 *
5108 * In contrast to old kernels, we insert the snapshot's
5109 * dir item and dir index after it has been created, so
5110 * we won't find a reference to our own snapshot. We
5111 * still keep the following code for backward
5112 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05005113 */
5114 if (location.type == BTRFS_ROOT_ITEM_KEY &&
5115 location.objectid == root->root_key.objectid) {
5116 over = 0;
5117 goto skip;
5118 }
Al Viro9cdda8d2013-05-22 16:48:09 -04005119 over = !dir_emit(ctx, name_ptr, name_len,
5120 location.objectid, d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04005121
Chris Mason3de45862008-11-17 21:02:50 -05005122skip:
Chris Mason5f39d392007-10-15 16:14:19 -04005123 if (name_ptr != tmp_name)
5124 kfree(name_ptr);
5125
Chris Mason39279cc2007-06-12 06:35:45 -04005126 if (over)
5127 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05005128 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01005129 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04005130 di_cur += di_len;
5131 di = (struct btrfs_dir_item *)((char *)di + di_len);
5132 }
Li Zefanb9e03af2011-03-23 10:43:58 +08005133next:
5134 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005135 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005136
Miao Xie16cdcec2011-04-22 18:12:22 +08005137 if (key_type == BTRFS_DIR_INDEX_KEY) {
5138 if (is_curr)
Al Viro9cdda8d2013-05-22 16:48:09 -04005139 ctx->pos++;
5140 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005141 if (ret)
5142 goto nopos;
5143 }
5144
David Woodhouse49593bf2008-08-17 17:08:36 +01005145 /* Reached end of directory/root. Bump pos past the last item. */
Zach Browndb62efb2013-07-11 16:19:42 -07005146 ctx->pos++;
5147
5148 /*
5149 * Stop new entries from being returned after we return the last
5150 * entry.
5151 *
5152 * New directory entries are assigned a strictly increasing
5153 * offset. This means that new entries created during readdir
5154 * are *guaranteed* to be seen in the future by that readdir.
5155 * This has broken buggy programs which operate on names as
5156 * they're returned by readdir. Until we re-use freed offsets
5157 * we have this hack to stop new entries from being returned
5158 * under the assumption that they'll never reach this huge
5159 * offset.
5160 *
5161 * This is being careful not to overflow 32bit loff_t unless the
5162 * last entry requires it because doing so has broken 32bit apps
5163 * in the past.
5164 */
5165 if (key_type == BTRFS_DIR_INDEX_KEY) {
5166 if (ctx->pos >= INT_MAX)
5167 ctx->pos = LLONG_MAX;
5168 else
5169 ctx->pos = INT_MAX;
5170 }
Chris Mason39279cc2007-06-12 06:35:45 -04005171nopos:
5172 ret = 0;
5173err:
Miao Xie16cdcec2011-04-22 18:12:22 +08005174 if (key_type == BTRFS_DIR_INDEX_KEY)
5175 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005176 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005177 return ret;
5178}
5179
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005180int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005181{
5182 struct btrfs_root *root = BTRFS_I(inode)->root;
5183 struct btrfs_trans_handle *trans;
5184 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005185 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005186
Josef Bacik72ac3c02012-05-23 14:13:11 -04005187 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005188 return 0;
5189
Liu Bo83eea1f2012-07-10 05:28:39 -06005190 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08005191 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005192
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005193 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005194 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005195 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005196 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005197 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005198 if (IS_ERR(trans))
5199 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06005200 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005201 }
5202 return ret;
5203}
5204
5205/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005206 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005207 * inode changes. But, it is most likely to find the inode in cache.
5208 * FIXME, needs more benchmarking...there are no reasons other than performance
5209 * to keep or drop this code.
5210 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005211static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005212{
5213 struct btrfs_root *root = BTRFS_I(inode)->root;
5214 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005215 int ret;
5216
Josef Bacik72ac3c02012-05-23 14:13:11 -04005217 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005218 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005219
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005220 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005221 if (IS_ERR(trans))
5222 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005223
5224 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005225 if (ret && ret == -ENOSPC) {
5226 /* whoops, lets try again with the full transaction */
5227 btrfs_end_transaction(trans, root);
5228 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005229 if (IS_ERR(trans))
5230 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005231
Chris Mason94b60442010-05-26 11:02:00 -04005232 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005233 }
Chris Mason39279cc2007-06-12 06:35:45 -04005234 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08005235 if (BTRFS_I(inode)->delayed_node)
5236 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005237
5238 return ret;
5239}
5240
5241/*
5242 * This is a copy of file_update_time. We need this so we can return error on
5243 * ENOSPC for updating the inode in the case of file write and mmap writes.
5244 */
Josef Bacike41f9412012-03-26 09:46:47 -04005245static int btrfs_update_time(struct inode *inode, struct timespec *now,
5246 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005247{
Alexander Block2bc5565282012-06-15 09:49:33 +02005248 struct btrfs_root *root = BTRFS_I(inode)->root;
5249
5250 if (btrfs_root_readonly(root))
5251 return -EROFS;
5252
Josef Bacike41f9412012-03-26 09:46:47 -04005253 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005254 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04005255 if (flags & S_CTIME)
5256 inode->i_ctime = *now;
5257 if (flags & S_MTIME)
5258 inode->i_mtime = *now;
5259 if (flags & S_ATIME)
5260 inode->i_atime = *now;
5261 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005262}
5263
Chris Masond352ac62008-09-29 15:18:18 -04005264/*
5265 * find the highest existing sequence number in a directory
5266 * and then set the in-memory index_cnt variable to reflect
5267 * free sequence numbers
5268 */
Josef Bacikaec74772008-07-24 12:12:38 -04005269static int btrfs_set_inode_index_count(struct inode *inode)
5270{
5271 struct btrfs_root *root = BTRFS_I(inode)->root;
5272 struct btrfs_key key, found_key;
5273 struct btrfs_path *path;
5274 struct extent_buffer *leaf;
5275 int ret;
5276
Li Zefan33345d012011-04-20 10:31:50 +08005277 key.objectid = btrfs_ino(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005278 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
5279 key.offset = (u64)-1;
5280
5281 path = btrfs_alloc_path();
5282 if (!path)
5283 return -ENOMEM;
5284
5285 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5286 if (ret < 0)
5287 goto out;
5288 /* FIXME: we should be able to handle this */
5289 if (ret == 0)
5290 goto out;
5291 ret = 0;
5292
5293 /*
5294 * MAGIC NUMBER EXPLANATION:
5295 * since we search a directory based on f_pos we have to start at 2
5296 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5297 * else has to start at 2
5298 */
5299 if (path->slots[0] == 0) {
5300 BTRFS_I(inode)->index_cnt = 2;
5301 goto out;
5302 }
5303
5304 path->slots[0]--;
5305
5306 leaf = path->nodes[0];
5307 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5308
Li Zefan33345d012011-04-20 10:31:50 +08005309 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacikaec74772008-07-24 12:12:38 -04005310 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
5311 BTRFS_I(inode)->index_cnt = 2;
5312 goto out;
5313 }
5314
5315 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
5316out:
5317 btrfs_free_path(path);
5318 return ret;
5319}
5320
Chris Masond352ac62008-09-29 15:18:18 -04005321/*
5322 * helper to find a free sequence number in a given directory. This current
5323 * code is very simple, later versions will do smarter things in the btree
5324 */
Chris Mason3de45862008-11-17 21:02:50 -05005325int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04005326{
5327 int ret = 0;
5328
5329 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08005330 ret = btrfs_inode_delayed_dir_index_count(dir);
5331 if (ret) {
5332 ret = btrfs_set_inode_index_count(dir);
5333 if (ret)
5334 return ret;
5335 }
Josef Bacikaec74772008-07-24 12:12:38 -04005336 }
5337
Chris Mason00e4e6b2008-08-05 11:18:09 -04005338 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04005339 BTRFS_I(dir)->index_cnt++;
5340
5341 return ret;
5342}
5343
Chris Mason39279cc2007-06-12 06:35:45 -04005344static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5345 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04005346 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05005347 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04005348 u64 ref_objectid, u64 objectid,
5349 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04005350{
5351 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005352 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04005353 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04005354 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05005355 struct btrfs_inode_ref *ref;
5356 struct btrfs_key key[2];
5357 u32 sizes[2];
5358 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04005359 int ret;
5360 int owner;
5361
Chris Mason5f39d392007-10-15 16:14:19 -04005362 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005363 if (!path)
5364 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04005365
Chris Mason39279cc2007-06-12 06:35:45 -04005366 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005367 if (!inode) {
5368 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005369 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005370 }
Chris Mason39279cc2007-06-12 06:35:45 -04005371
Li Zefan581bb052011-04-20 10:06:11 +08005372 /*
5373 * we have to initialize this early, so we can reclaim the inode
5374 * number if we fail afterwards in this function.
5375 */
5376 inode->i_ino = objectid;
5377
Josef Bacikaec74772008-07-24 12:12:38 -04005378 if (dir) {
liubo1abe9b82011-03-24 11:18:59 +00005379 trace_btrfs_inode_request(dir);
5380
Chris Mason3de45862008-11-17 21:02:50 -05005381 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04005382 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005383 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005384 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005385 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04005386 }
Josef Bacikaec74772008-07-24 12:12:38 -04005387 }
5388 /*
5389 * index_cnt is ignored for everything but a dir,
5390 * btrfs_get_inode_index_count has an explanation for the magic
5391 * number
5392 */
5393 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04005394 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04005395 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00005396 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04005397
Josef Bacik5dc562c2012-08-17 13:14:17 -04005398 /*
5399 * We could have gotten an inode number from somebody who was fsynced
5400 * and then removed in this same transaction, so let's just set full
5401 * sync since it will be a full sync anyway and this will blow away the
5402 * old info in the log.
5403 */
5404 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
5405
Al Viro569254b2011-07-24 17:08:40 -04005406 if (S_ISDIR(mode))
Chris Mason39279cc2007-06-12 06:35:45 -04005407 owner = 0;
5408 else
5409 owner = 1;
Chris Mason9c583092008-01-29 15:15:18 -05005410
5411 key[0].objectid = objectid;
5412 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
5413 key[0].offset = 0;
5414
Mark Fashehf1863732012-08-08 11:32:27 -07005415 /*
5416 * Start new inodes with an inode_ref. This is slightly more
5417 * efficient for small numbers of hard links since they will
5418 * be packed into one item. Extended refs will kick in if we
5419 * add more hard links than can fit in the ref item.
5420 */
Chris Mason9c583092008-01-29 15:15:18 -05005421 key[1].objectid = objectid;
5422 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
5423 key[1].offset = ref_objectid;
5424
5425 sizes[0] = sizeof(struct btrfs_inode_item);
5426 sizes[1] = name_len + sizeof(*ref);
5427
Chris Masonb9473432009-03-13 11:00:37 -04005428 path->leave_spinning = 1;
Chris Mason9c583092008-01-29 15:15:18 -05005429 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
5430 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04005431 goto fail;
5432
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03005433 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04005434 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005435 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04005436 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
5437 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06005438 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
5439 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04005440 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05005441
5442 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
5443 struct btrfs_inode_ref);
5444 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04005445 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05005446 ptr = (unsigned long)(ref + 1);
5447 write_extent_buffer(path->nodes[0], name, ptr, name_len);
5448
Chris Mason5f39d392007-10-15 16:14:19 -04005449 btrfs_mark_buffer_dirty(path->nodes[0]);
5450 btrfs_free_path(path);
5451
Chris Mason39279cc2007-06-12 06:35:45 -04005452 location = &BTRFS_I(inode)->location;
5453 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04005454 location->offset = 0;
5455 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
5456
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005457 btrfs_inherit_iflags(inode, dir);
5458
Al Viro569254b2011-07-24 17:08:40 -04005459 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04005460 if (btrfs_test_opt(root, NODATASUM))
5461 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo213490b2012-09-11 08:33:50 -06005462 if (btrfs_test_opt(root, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05005463 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
5464 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04005465 }
5466
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005467 btrfs_insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005468 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00005469
5470 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04005471 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00005472
Alexander Block8ea05e32012-07-25 17:35:53 +02005473 btrfs_update_root_times(trans, root);
5474
Chris Mason39279cc2007-06-12 06:35:45 -04005475 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005476fail:
Josef Bacikaec74772008-07-24 12:12:38 -04005477 if (dir)
5478 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04005479 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005480 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005481 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005482}
5483
5484static inline u8 btrfs_inode_type(struct inode *inode)
5485{
5486 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
5487}
5488
Chris Masond352ac62008-09-29 15:18:18 -04005489/*
5490 * utility function to add 'inode' into 'parent_inode' with
5491 * a give name and a given sequence number.
5492 * if 'add_backref' is true, also insert a backref from the
5493 * inode to the parent directory.
5494 */
Chris Masone02119d2008-09-05 16:13:11 -04005495int btrfs_add_link(struct btrfs_trans_handle *trans,
5496 struct inode *parent_inode, struct inode *inode,
5497 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005498{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005499 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005500 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04005501 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08005502 u64 ino = btrfs_ino(inode);
5503 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005504
Li Zefan33345d012011-04-20 10:31:50 +08005505 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005506 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
5507 } else {
Li Zefan33345d012011-04-20 10:31:50 +08005508 key.objectid = ino;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005509 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
5510 key.offset = 0;
5511 }
Chris Mason39279cc2007-06-12 06:35:45 -04005512
Li Zefan33345d012011-04-20 10:31:50 +08005513 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005514 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
5515 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08005516 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005517 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08005518 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
5519 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005520 }
5521
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005522 /* Nothing to clean up yet */
5523 if (ret)
5524 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005525
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005526 ret = btrfs_insert_dir_item(trans, root, name, name_len,
5527 parent_inode, &key,
5528 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05005529 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005530 goto fail_dir_item;
5531 else if (ret) {
5532 btrfs_abort_transaction(trans, root, ret);
5533 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005534 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005535
5536 btrfs_i_size_write(parent_inode, parent_inode->i_size +
5537 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005538 inode_inc_iversion(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005539 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
5540 ret = btrfs_update_inode(trans, root, parent_inode);
5541 if (ret)
5542 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005543 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05005544
5545fail_dir_item:
5546 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
5547 u64 local_index;
5548 int err;
5549 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
5550 key.objectid, root->root_key.objectid,
5551 parent_ino, &local_index, name, name_len);
5552
5553 } else if (add_backref) {
5554 u64 local_index;
5555 int err;
5556
5557 err = btrfs_del_inode_ref(trans, root, name, name_len,
5558 ino, parent_ino, &local_index);
5559 }
5560 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005561}
5562
5563static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00005564 struct inode *dir, struct dentry *dentry,
5565 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005566{
Josef Bacika1b075d2010-11-19 20:36:11 +00005567 int err = btrfs_add_link(trans, dir, inode,
5568 dentry->d_name.name, dentry->d_name.len,
5569 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005570 if (err > 0)
5571 err = -EEXIST;
5572 return err;
5573}
5574
Josef Bacik618e21d2007-07-11 10:18:17 -04005575static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04005576 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04005577{
5578 struct btrfs_trans_handle *trans;
5579 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005580 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04005581 int err;
5582 int drop_inode = 0;
5583 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005584 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04005585
5586 if (!new_valid_dev(rdev))
5587 return -EINVAL;
5588
Josef Bacik9ed74f22009-09-11 16:12:44 -04005589 /*
5590 * 2 for inode item and ref
5591 * 2 for dir items
5592 * 1 for xattr if selinux is on
5593 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005594 trans = btrfs_start_transaction(root, 5);
5595 if (IS_ERR(trans))
5596 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05005597
Li Zefan581bb052011-04-20 10:06:11 +08005598 err = btrfs_find_free_ino(root, &objectid);
5599 if (err)
5600 goto out_unlock;
5601
Josef Bacikaec74772008-07-24 12:12:38 -04005602 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005603 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005604 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005605 if (IS_ERR(inode)) {
5606 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005607 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005608 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005609
Eric Paris2a7dba32011-02-01 11:05:39 -05005610 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005611 if (err) {
5612 drop_inode = 1;
5613 goto out_unlock;
5614 }
5615
Casey Schauflerad19db72011-12-15 10:09:07 -05005616 /*
5617 * If the active LSM wants to access the inode during
5618 * d_instantiate it needs these. Smack checks to see
5619 * if the filesystem supports xattrs by looking at the
5620 * ops vector.
5621 */
5622
5623 inode->i_op = &btrfs_special_inode_operations;
Josef Bacika1b075d2010-11-19 20:36:11 +00005624 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04005625 if (err)
5626 drop_inode = 1;
5627 else {
Josef Bacik618e21d2007-07-11 10:18:17 -04005628 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04005629 btrfs_update_inode(trans, root, inode);
Al Viro08c422c2011-12-23 07:58:13 -05005630 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005631 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005632out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005633 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00005634 btrfs_btree_balance_dirty(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04005635 if (drop_inode) {
5636 inode_dec_link_count(inode);
5637 iput(inode);
5638 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005639 return err;
5640}
5641
Chris Mason39279cc2007-06-12 06:35:45 -04005642static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04005643 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04005644{
5645 struct btrfs_trans_handle *trans;
5646 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005647 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005648 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04005649 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04005650 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005651 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005652
Josef Bacik9ed74f22009-09-11 16:12:44 -04005653 /*
5654 * 2 for inode item and ref
5655 * 2 for dir items
5656 * 1 for xattr if selinux is on
5657 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005658 trans = btrfs_start_transaction(root, 5);
5659 if (IS_ERR(trans))
5660 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005661
Li Zefan581bb052011-04-20 10:06:11 +08005662 err = btrfs_find_free_ino(root, &objectid);
5663 if (err)
5664 goto out_unlock;
5665
Josef Bacikaec74772008-07-24 12:12:38 -04005666 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005667 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005668 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005669 if (IS_ERR(inode)) {
5670 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005671 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005672 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005673 drop_inode_on_err = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04005674
Eric Paris2a7dba32011-02-01 11:05:39 -05005675 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005676 if (err)
Josef Bacik33268ea2008-07-24 12:16:36 -04005677 goto out_unlock;
Josef Bacik33268ea2008-07-24 12:16:36 -04005678
Filipe Brandenburger9185aa52012-11-30 03:40:08 +00005679 err = btrfs_update_inode(trans, root, inode);
5680 if (err)
5681 goto out_unlock;
5682
Casey Schauflerad19db72011-12-15 10:09:07 -05005683 /*
5684 * If the active LSM wants to access the inode during
5685 * d_instantiate it needs these. Smack checks to see
5686 * if the filesystem supports xattrs by looking at the
5687 * ops vector.
5688 */
5689 inode->i_fop = &btrfs_file_operations;
5690 inode->i_op = &btrfs_file_inode_operations;
5691
Josef Bacika1b075d2010-11-19 20:36:11 +00005692 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005693 if (err)
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005694 goto out_unlock;
5695
5696 inode->i_mapping->a_ops = &btrfs_aops;
5697 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
5698 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
5699 d_instantiate(dentry, inode);
5700
Chris Mason39279cc2007-06-12 06:35:45 -04005701out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005702 btrfs_end_transaction(trans, root);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005703 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005704 inode_dec_link_count(inode);
5705 iput(inode);
5706 }
Liu Bob53d3f52012-11-14 14:34:34 +00005707 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005708 return err;
5709}
5710
5711static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
5712 struct dentry *dentry)
5713{
5714 struct btrfs_trans_handle *trans;
5715 struct btrfs_root *root = BTRFS_I(dir)->root;
5716 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005717 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04005718 int err;
5719 int drop_inode = 0;
5720
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005721 /* do not allow sys_link's with other subvols of the same device */
5722 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00005723 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005724
Mark Fashehf1863732012-08-08 11:32:27 -07005725 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00005726 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04005727
Chris Mason3de45862008-11-17 21:02:50 -05005728 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04005729 if (err)
5730 goto fail;
5731
Yan, Zhenga22285a2010-05-16 10:48:46 -04005732 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00005733 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04005734 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00005735 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04005736 */
Miao Xie7e6b6462011-02-18 09:21:17 +00005737 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005738 if (IS_ERR(trans)) {
5739 err = PTR_ERR(trans);
5740 goto fail;
5741 }
Chris Mason5f39d392007-10-15 16:14:19 -04005742
Zach Brown8b558c52013-10-16 12:10:34 -07005743 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005744 inode_inc_iversion(inode);
Miao Xie31534952011-04-13 13:19:21 +08005745 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6ee2010-10-23 11:11:40 -04005746 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04005747 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04005748
Josef Bacika1b075d2010-11-19 20:36:11 +00005749 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04005750
Yan, Zhenga5719522009-09-24 09:17:31 -04005751 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005752 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04005753 } else {
Al Viro10d9f302011-07-16 23:09:10 -04005754 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04005755 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005756 if (err)
5757 goto fail;
Al Viro08c422c2011-12-23 07:58:13 -05005758 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00005759 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04005760 }
Chris Mason39279cc2007-06-12 06:35:45 -04005761
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005762 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05005763fail:
Chris Mason39279cc2007-06-12 06:35:45 -04005764 if (drop_inode) {
5765 inode_dec_link_count(inode);
5766 iput(inode);
5767 }
Liu Bob53d3f52012-11-14 14:34:34 +00005768 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005769 return err;
5770}
5771
Al Viro18bb1db2011-07-26 01:41:39 -04005772static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04005773{
Chris Masonb9d86662008-05-02 16:13:49 -04005774 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04005775 struct btrfs_trans_handle *trans;
5776 struct btrfs_root *root = BTRFS_I(dir)->root;
5777 int err = 0;
5778 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04005779 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005780 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005781
Josef Bacik9ed74f22009-09-11 16:12:44 -04005782 /*
5783 * 2 items for inode and ref
5784 * 2 items for dir items
5785 * 1 for xattr if selinux is on
5786 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005787 trans = btrfs_start_transaction(root, 5);
5788 if (IS_ERR(trans))
5789 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04005790
Li Zefan581bb052011-04-20 10:06:11 +08005791 err = btrfs_find_free_ino(root, &objectid);
5792 if (err)
5793 goto out_fail;
5794
Josef Bacikaec74772008-07-24 12:12:38 -04005795 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005796 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005797 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04005798 if (IS_ERR(inode)) {
5799 err = PTR_ERR(inode);
5800 goto out_fail;
5801 }
Chris Mason5f39d392007-10-15 16:14:19 -04005802
Chris Mason39279cc2007-06-12 06:35:45 -04005803 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04005804
Eric Paris2a7dba32011-02-01 11:05:39 -05005805 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005806 if (err)
5807 goto out_fail;
5808
Chris Mason39279cc2007-06-12 06:35:45 -04005809 inode->i_op = &btrfs_dir_inode_operations;
5810 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04005811
Chris Masondbe674a2008-07-17 12:54:05 -04005812 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005813 err = btrfs_update_inode(trans, root, inode);
5814 if (err)
5815 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005816
Josef Bacika1b075d2010-11-19 20:36:11 +00005817 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
5818 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005819 if (err)
5820 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005821
Chris Mason39279cc2007-06-12 06:35:45 -04005822 d_instantiate(dentry, inode);
5823 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005824
5825out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005826 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005827 if (drop_on_err)
5828 iput(inode);
Liu Bob53d3f52012-11-14 14:34:34 +00005829 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005830 return err;
5831}
5832
Chris Masond352ac62008-09-29 15:18:18 -04005833/* helper for btfs_get_extent. Given an existing extent in the tree,
5834 * and an extent that you want to insert, deal with overlap and insert
5835 * the new extent into the tree.
5836 */
Chris Mason3b951512008-04-17 11:29:12 -04005837static int merge_extent_mapping(struct extent_map_tree *em_tree,
5838 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005839 struct extent_map *em,
5840 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04005841{
5842 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04005843
Chris Masone6dcd2d2008-07-17 12:53:50 -04005844 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
5845 start_diff = map_start - em->start;
5846 em->start = map_start;
5847 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04005848 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
5849 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04005850 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04005851 em->block_len -= start_diff;
5852 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005853 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04005854}
5855
Chris Masonc8b97812008-10-29 14:49:59 -04005856static noinline int uncompress_inline(struct btrfs_path *path,
5857 struct inode *inode, struct page *page,
5858 size_t pg_offset, u64 extent_offset,
5859 struct btrfs_file_extent_item *item)
5860{
5861 int ret;
5862 struct extent_buffer *leaf = path->nodes[0];
5863 char *tmp;
5864 size_t max_size;
5865 unsigned long inline_size;
5866 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08005867 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04005868
5869 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08005870 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04005871 max_size = btrfs_file_extent_ram_bytes(leaf, item);
5872 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01005873 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04005874 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04005875 if (!tmp)
5876 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04005877 ptr = btrfs_file_extent_inline_start(item);
5878
5879 read_extent_buffer(leaf, tmp, ptr, inline_size);
5880
Chris Mason5b050f02008-11-11 09:34:41 -05005881 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08005882 ret = btrfs_decompress(compress_type, tmp, page,
5883 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04005884 if (ret) {
Cong Wang7ac687d2011-11-25 23:14:28 +08005885 char *kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04005886 unsigned long copy_size = min_t(u64,
5887 PAGE_CACHE_SIZE - pg_offset,
5888 max_size - extent_offset);
5889 memset(kaddr + pg_offset, 0, copy_size);
Cong Wang7ac687d2011-11-25 23:14:28 +08005890 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -04005891 }
5892 kfree(tmp);
5893 return 0;
5894}
5895
Chris Masond352ac62008-09-29 15:18:18 -04005896/*
5897 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05005898 * the ugly parts come from merging extents from the disk with the in-ram
5899 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04005900 * where the in-ram extents might be locked pending data=ordered completion.
5901 *
5902 * This also copies inline extents directly into the page.
5903 */
Chris Masond3977122009-01-05 21:25:51 -05005904
Chris Masona52d9a82007-08-27 16:49:44 -04005905struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05005906 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04005907 int create)
5908{
5909 int ret;
5910 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04005911 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04005912 u64 extent_start = 0;
5913 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08005914 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04005915 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04005916 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04005917 struct btrfs_root *root = BTRFS_I(inode)->root;
5918 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04005919 struct extent_buffer *leaf;
5920 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04005921 struct extent_map *em = NULL;
5922 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05005923 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04005924 struct btrfs_trans_handle *trans = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08005925 int compress_type;
Chris Masona52d9a82007-08-27 16:49:44 -04005926
Chris Masona52d9a82007-08-27 16:49:44 -04005927again:
Chris Mason890871b2009-09-02 16:24:52 -04005928 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005929 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04005930 if (em)
5931 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04005932 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005933
Chris Masona52d9a82007-08-27 16:49:44 -04005934 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04005935 if (em->start > start || em->start + em->len <= start)
5936 free_extent_map(em);
5937 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05005938 free_extent_map(em);
5939 else
5940 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005941 }
David Sterba172ddd62011-04-21 00:48:27 +02005942 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04005943 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05005944 err = -ENOMEM;
5945 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005946 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005947 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05005948 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05005949 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05005950 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04005951 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04005952
5953 if (!path) {
5954 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04005955 if (!path) {
5956 err = -ENOMEM;
5957 goto out;
5958 }
5959 /*
5960 * Chances are we'll be called again, so go ahead and do
5961 * readahead
5962 */
5963 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04005964 }
5965
Chris Mason179e29e2007-11-01 11:28:41 -04005966 ret = btrfs_lookup_file_extent(trans, root, path,
5967 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04005968 if (ret < 0) {
5969 err = ret;
5970 goto out;
5971 }
5972
5973 if (ret != 0) {
5974 if (path->slots[0] == 0)
5975 goto not_found;
5976 path->slots[0]--;
5977 }
5978
Chris Mason5f39d392007-10-15 16:14:19 -04005979 leaf = path->nodes[0];
5980 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04005981 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04005982 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04005983 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5984 found_type = btrfs_key_type(&found_key);
5985 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04005986 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04005987 /*
5988 * If we backup past the first extent we want to move forward
5989 * and see if there is an extent in front of us, otherwise we'll
5990 * say there is a hole for our whole search range which can
5991 * cause problems.
5992 */
5993 extent_end = start;
5994 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04005995 }
5996
Chris Mason5f39d392007-10-15 16:14:19 -04005997 found_type = btrfs_file_extent_type(leaf, item);
5998 extent_start = found_key.offset;
Li Zefan261507a02010-12-17 14:21:50 +08005999 compress_type = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04006000 if (found_type == BTRFS_FILE_EXTENT_REG ||
6001 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006002 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006003 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006004 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6005 size_t size;
6006 size = btrfs_file_extent_inline_len(leaf, item);
Qu Wenruofda28322013-02-26 08:10:22 +00006007 extent_end = ALIGN(extent_start + size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04006008 }
Josef Bacik25a50342013-10-14 12:08:38 -04006009next:
Yan Zheng9036c102008-10-30 14:19:41 -04006010 if (start >= extent_end) {
6011 path->slots[0]++;
6012 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6013 ret = btrfs_next_leaf(root, path);
6014 if (ret < 0) {
6015 err = ret;
6016 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006017 }
Yan Zheng9036c102008-10-30 14:19:41 -04006018 if (ret > 0)
6019 goto not_found;
6020 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006021 }
Yan Zheng9036c102008-10-30 14:19:41 -04006022 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6023 if (found_key.objectid != objectid ||
6024 found_key.type != BTRFS_EXTENT_DATA_KEY)
6025 goto not_found;
6026 if (start + len <= found_key.offset)
6027 goto not_found;
6028 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006029 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006030 em->len = found_key.offset - start;
6031 goto not_found_em;
6032 }
6033
Josef Bacikcc95bef2013-04-04 14:31:27 -04006034 em->ram_bytes = btrfs_file_extent_ram_bytes(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04006035 if (found_type == BTRFS_FILE_EXTENT_REG ||
6036 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04006037 em->start = extent_start;
6038 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05006039 em->orig_start = extent_start -
6040 btrfs_file_extent_offset(leaf, item);
Josef Bacikb4939682012-12-03 10:31:19 -05006041 em->orig_block_len = btrfs_file_extent_disk_num_bytes(leaf,
6042 item);
Chris Masondb945352007-10-15 16:15:53 -04006043 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
6044 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04006045 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04006046 goto insert;
6047 }
Li Zefan261507a02010-12-17 14:21:50 +08006048 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006049 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08006050 em->compress_type = compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006051 em->block_start = bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05006052 em->block_len = em->orig_block_len;
Chris Masonc8b97812008-10-29 14:49:59 -04006053 } else {
6054 bytenr += btrfs_file_extent_offset(leaf, item);
6055 em->block_start = bytenr;
6056 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04006057 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
6058 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04006059 }
Chris Masona52d9a82007-08-27 16:49:44 -04006060 goto insert;
6061 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006062 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006063 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006064 size_t size;
6065 size_t extent_offset;
6066 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006067
Chris Masona52d9a82007-08-27 16:49:44 -04006068 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04006069 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04006070 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04006071 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04006072 goto out;
6073 }
6074
Yan Zheng9036c102008-10-30 14:19:41 -04006075 size = btrfs_file_extent_inline_len(leaf, item);
6076 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05006077 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04006078 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006079 em->start = extent_start + extent_offset;
Qu Wenruofda28322013-02-26 08:10:22 +00006080 em->len = ALIGN(copy_size, root->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006081 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006082 em->orig_start = em->start;
Li Zefan261507a02010-12-17 14:21:50 +08006083 if (compress_type) {
Chris Masonc8b97812008-10-29 14:49:59 -04006084 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08006085 em->compress_type = compress_type;
6086 }
Yan689f9342007-10-29 11:41:07 -04006087 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006088 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006089 if (btrfs_file_extent_compression(leaf, item) !=
6090 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006091 ret = uncompress_inline(path, inode, page,
6092 pg_offset,
6093 extent_offset, item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006094 BUG_ON(ret); /* -ENOMEM */
Chris Masonc8b97812008-10-29 14:49:59 -04006095 } else {
6096 map = kmap(page);
6097 read_extent_buffer(leaf, map + pg_offset, ptr,
6098 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04006099 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
6100 memset(map + pg_offset + copy_size, 0,
6101 PAGE_CACHE_SIZE - pg_offset -
6102 copy_size);
6103 }
Chris Masonc8b97812008-10-29 14:49:59 -04006104 kunmap(page);
6105 }
Chris Mason179e29e2007-11-01 11:28:41 -04006106 flush_dcache_page(page);
6107 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006108 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006109 if (!trans) {
6110 kunmap(page);
6111 free_extent_map(em);
6112 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006113
David Sterbab3b4aa72011-04-21 01:20:15 +02006114 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006115 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006116
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006117 if (IS_ERR(trans))
6118 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006119 goto again;
6120 }
Chris Masonc8b97812008-10-29 14:49:59 -04006121 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006122 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006123 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006124 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006125 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006126 }
Chris Masond1310b22008-01-24 16:13:08 -05006127 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006128 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006129 goto insert;
6130 } else {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00006131 WARN(1, KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04006132 }
6133not_found:
6134 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006135 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006136 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006137not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04006138 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04006139 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006140insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006141 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006142 if (em->start > start || extent_map_end(em) <= start) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006143 btrfs_err(root->fs_info, "bad extent! em: [%llu %llu] passed [%llu %llu]",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006144 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04006145 err = -EIO;
6146 goto out;
6147 }
Chris Masond1310b22008-01-24 16:13:08 -05006148
6149 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006150 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006151 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006152 /* it is possible that someone inserted the extent into the tree
6153 * while we had the lock dropped. It is also possible that
6154 * an overlapping map exists in the tree
6155 */
Chris Masona52d9a82007-08-27 16:49:44 -04006156 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04006157 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006158
6159 ret = 0;
6160
Chris Mason3b951512008-04-17 11:29:12 -04006161 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04006162 if (existing && (existing->start > start ||
6163 existing->start + existing->len <= start)) {
6164 free_extent_map(existing);
6165 existing = NULL;
6166 }
Chris Mason3b951512008-04-17 11:29:12 -04006167 if (!existing) {
6168 existing = lookup_extent_mapping(em_tree, em->start,
6169 em->len);
6170 if (existing) {
6171 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006172 em, start,
6173 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04006174 free_extent_map(existing);
6175 if (err) {
6176 free_extent_map(em);
6177 em = NULL;
6178 }
6179 } else {
6180 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04006181 free_extent_map(em);
6182 em = NULL;
6183 }
6184 } else {
6185 free_extent_map(em);
6186 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006187 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006188 }
Chris Masona52d9a82007-08-27 16:49:44 -04006189 }
Chris Mason890871b2009-09-02 16:24:52 -04006190 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006191out:
liubo1abe9b82011-03-24 11:18:59 +00006192
Tsutomu Itohf0bd95e2012-10-01 03:08:37 -06006193 if (em)
6194 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00006195
Chris Masonf4219502008-07-22 11:18:09 -04006196 if (path)
6197 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04006198 if (trans) {
6199 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05006200 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04006201 err = ret;
6202 }
Chris Masona52d9a82007-08-27 16:49:44 -04006203 if (err) {
6204 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04006205 return ERR_PTR(err);
6206 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006207 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04006208 return em;
6209}
6210
Chris Masonec29ed52011-02-23 16:23:20 -05006211struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
6212 size_t pg_offset, u64 start, u64 len,
6213 int create)
6214{
6215 struct extent_map *em;
6216 struct extent_map *hole_em = NULL;
6217 u64 range_start = start;
6218 u64 end;
6219 u64 found;
6220 u64 found_end;
6221 int err = 0;
6222
6223 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
6224 if (IS_ERR(em))
6225 return em;
6226 if (em) {
6227 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00006228 * if our em maps to
6229 * - a hole or
6230 * - a pre-alloc extent,
6231 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05006232 */
Liu Bof9e4fb52013-01-07 10:10:12 +00006233 if (em->block_start != EXTENT_MAP_HOLE &&
6234 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05006235 return em;
6236 else
6237 hole_em = em;
6238 }
6239
6240 /* check to see if we've wrapped (len == -1 or similar) */
6241 end = start + len;
6242 if (end < start)
6243 end = (u64)-1;
6244 else
6245 end -= 1;
6246
6247 em = NULL;
6248
6249 /* ok, we didn't find anything, lets look for delalloc */
6250 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
6251 end, len, EXTENT_DELALLOC, 1);
6252 found_end = range_start + found;
6253 if (found_end < range_start)
6254 found_end = (u64)-1;
6255
6256 /*
6257 * we didn't find anything useful, return
6258 * the original results from get_extent()
6259 */
6260 if (range_start > end || found_end <= start) {
6261 em = hole_em;
6262 hole_em = NULL;
6263 goto out;
6264 }
6265
6266 /* adjust the range_start to make sure it doesn't
6267 * go backwards from the start they passed in
6268 */
6269 range_start = max(start,range_start);
6270 found = found_end - range_start;
6271
6272 if (found > 0) {
6273 u64 hole_start = start;
6274 u64 hole_len = len;
6275
David Sterba172ddd62011-04-21 00:48:27 +02006276 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05006277 if (!em) {
6278 err = -ENOMEM;
6279 goto out;
6280 }
6281 /*
6282 * when btrfs_get_extent can't find anything it
6283 * returns one huge hole
6284 *
6285 * make sure what it found really fits our range, and
6286 * adjust to make sure it is based on the start from
6287 * the caller
6288 */
6289 if (hole_em) {
6290 u64 calc_end = extent_map_end(hole_em);
6291
6292 if (calc_end <= start || (hole_em->start > end)) {
6293 free_extent_map(hole_em);
6294 hole_em = NULL;
6295 } else {
6296 hole_start = max(hole_em->start, start);
6297 hole_len = calc_end - hole_start;
6298 }
6299 }
6300 em->bdev = NULL;
6301 if (hole_em && range_start > hole_start) {
6302 /* our hole starts before our delalloc, so we
6303 * have to return just the parts of the hole
6304 * that go until the delalloc starts
6305 */
6306 em->len = min(hole_len,
6307 range_start - hole_start);
6308 em->start = hole_start;
6309 em->orig_start = hole_start;
6310 /*
6311 * don't adjust block start at all,
6312 * it is fixed at EXTENT_MAP_HOLE
6313 */
6314 em->block_start = hole_em->block_start;
6315 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00006316 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
6317 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05006318 } else {
6319 em->start = range_start;
6320 em->len = found;
6321 em->orig_start = range_start;
6322 em->block_start = EXTENT_MAP_DELALLOC;
6323 em->block_len = found;
6324 }
6325 } else if (hole_em) {
6326 return hole_em;
6327 }
6328out:
6329
6330 free_extent_map(hole_em);
6331 if (err) {
6332 free_extent_map(em);
6333 return ERR_PTR(err);
6334 }
6335 return em;
6336}
6337
Josef Bacik4b46fce2010-05-23 11:00:55 -04006338static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
6339 u64 start, u64 len)
6340{
6341 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04006342 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006343 struct btrfs_key ins;
6344 u64 alloc_hint;
6345 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006346
Josef Bacik4b46fce2010-05-23 11:00:55 -04006347 alloc_hint = get_extent_allocation_hint(inode, start, len);
Josef Bacik00361582013-08-14 14:02:47 -04006348 ret = btrfs_reserve_extent(root, len, root->sectorsize, 0,
Josef Bacik81c9ad22012-01-18 10:56:06 -05006349 alloc_hint, &ins, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006350 if (ret)
6351 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006352
Josef Bacik70c8a912012-10-11 16:54:30 -04006353 em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006354 ins.offset, ins.offset, ins.offset, 0);
Josef Bacik00361582013-08-14 14:02:47 -04006355 if (IS_ERR(em)) {
6356 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
6357 return em;
6358 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006359
6360 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
6361 ins.offset, ins.offset, 0);
6362 if (ret) {
6363 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
Josef Bacik00361582013-08-14 14:02:47 -04006364 free_extent_map(em);
6365 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006366 }
Josef Bacik00361582013-08-14 14:02:47 -04006367
Josef Bacik4b46fce2010-05-23 11:00:55 -04006368 return em;
6369}
6370
Chris Mason46bfbb52010-05-26 11:04:10 -04006371/*
6372 * returns 1 when the nocow is safe, < 1 on error, 0 if the
6373 * block must be cow'd
6374 */
Josef Bacik00361582013-08-14 14:02:47 -04006375noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04006376 u64 *orig_start, u64 *orig_block_len,
6377 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04006378{
Josef Bacik00361582013-08-14 14:02:47 -04006379 struct btrfs_trans_handle *trans;
Chris Mason46bfbb52010-05-26 11:04:10 -04006380 struct btrfs_path *path;
6381 int ret;
6382 struct extent_buffer *leaf;
6383 struct btrfs_root *root = BTRFS_I(inode)->root;
6384 struct btrfs_file_extent_item *fi;
6385 struct btrfs_key key;
6386 u64 disk_bytenr;
6387 u64 backref_offset;
6388 u64 extent_end;
6389 u64 num_bytes;
6390 int slot;
6391 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04006392 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Chris Mason46bfbb52010-05-26 11:04:10 -04006393 path = btrfs_alloc_path();
6394 if (!path)
6395 return -ENOMEM;
6396
Josef Bacik00361582013-08-14 14:02:47 -04006397 ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04006398 offset, 0);
6399 if (ret < 0)
6400 goto out;
6401
6402 slot = path->slots[0];
6403 if (ret == 1) {
6404 if (slot == 0) {
6405 /* can't find the item, must cow */
6406 ret = 0;
6407 goto out;
6408 }
6409 slot--;
6410 }
6411 ret = 0;
6412 leaf = path->nodes[0];
6413 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08006414 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04006415 key.type != BTRFS_EXTENT_DATA_KEY) {
6416 /* not our file or wrong item type, must cow */
6417 goto out;
6418 }
6419
6420 if (key.offset > offset) {
6421 /* Wrong offset, must cow */
6422 goto out;
6423 }
6424
6425 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
6426 found_type = btrfs_file_extent_type(leaf, fi);
6427 if (found_type != BTRFS_FILE_EXTENT_REG &&
6428 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
6429 /* not a regular extent, must cow */
6430 goto out;
6431 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04006432
6433 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
6434 goto out;
6435
Chris Mason46bfbb52010-05-26 11:04:10 -04006436 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04006437 if (disk_bytenr == 0)
6438 goto out;
6439
6440 if (btrfs_file_extent_compression(leaf, fi) ||
6441 btrfs_file_extent_encryption(leaf, fi) ||
6442 btrfs_file_extent_other_encoding(leaf, fi))
6443 goto out;
6444
Chris Mason46bfbb52010-05-26 11:04:10 -04006445 backref_offset = btrfs_file_extent_offset(leaf, fi);
6446
Josef Bacik7ee9e442013-06-21 16:37:03 -04006447 if (orig_start) {
6448 *orig_start = key.offset - backref_offset;
6449 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
6450 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
6451 }
Josef Bacikeb384b52013-04-24 16:32:55 -04006452
Chris Mason46bfbb52010-05-26 11:04:10 -04006453 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason46bfbb52010-05-26 11:04:10 -04006454
6455 if (btrfs_extent_readonly(root, disk_bytenr))
6456 goto out;
Josef Bacik1bda19e2013-10-18 12:10:36 -04006457 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04006458
6459 /*
6460 * look for other files referencing this extent, if we
6461 * find any we must cow
6462 */
Josef Bacik00361582013-08-14 14:02:47 -04006463 trans = btrfs_join_transaction(root);
6464 if (IS_ERR(trans)) {
6465 ret = 0;
Chris Mason46bfbb52010-05-26 11:04:10 -04006466 goto out;
Josef Bacik00361582013-08-14 14:02:47 -04006467 }
6468
6469 ret = btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
6470 key.offset - backref_offset, disk_bytenr);
6471 btrfs_end_transaction(trans, root);
6472 if (ret) {
6473 ret = 0;
6474 goto out;
6475 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006476
6477 /*
6478 * adjust disk_bytenr and num_bytes to cover just the bytes
6479 * in this extent we are about to write. If there
6480 * are any csums in that range we have to cow in order
6481 * to keep the csums correct
6482 */
6483 disk_bytenr += backref_offset;
6484 disk_bytenr += offset - key.offset;
Josef Bacikeb384b52013-04-24 16:32:55 -04006485 num_bytes = min(offset + *len, extent_end) - offset;
Chris Mason46bfbb52010-05-26 11:04:10 -04006486 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
6487 goto out;
6488 /*
6489 * all of the above have passed, it is safe to overwrite this extent
6490 * without cow
6491 */
Josef Bacikeb384b52013-04-24 16:32:55 -04006492 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04006493 ret = 1;
6494out:
6495 btrfs_free_path(path);
6496 return ret;
6497}
6498
Josef Bacikeb838e72012-07-31 16:28:48 -04006499static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
6500 struct extent_state **cached_state, int writing)
6501{
6502 struct btrfs_ordered_extent *ordered;
6503 int ret = 0;
6504
6505 while (1) {
6506 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6507 0, cached_state);
6508 /*
6509 * We're concerned with the entire range that we're going to be
6510 * doing DIO to, so we need to make sure theres no ordered
6511 * extents in this range.
6512 */
6513 ordered = btrfs_lookup_ordered_range(inode, lockstart,
6514 lockend - lockstart + 1);
6515
6516 /*
6517 * We need to make sure there are no buffered pages in this
6518 * range either, we could have raced between the invalidate in
6519 * generic_file_direct_write and locking the extent. The
6520 * invalidate needs to happen so that reads after a write do not
6521 * get stale data.
6522 */
6523 if (!ordered && (!writing ||
6524 !test_range_bit(&BTRFS_I(inode)->io_tree,
6525 lockstart, lockend, EXTENT_UPTODATE, 0,
6526 *cached_state)))
6527 break;
6528
6529 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6530 cached_state, GFP_NOFS);
6531
6532 if (ordered) {
6533 btrfs_start_ordered_extent(inode, ordered, 1);
6534 btrfs_put_ordered_extent(ordered);
6535 } else {
6536 /* Screw you mmap */
6537 ret = filemap_write_and_wait_range(inode->i_mapping,
6538 lockstart,
6539 lockend);
6540 if (ret)
6541 break;
6542
6543 /*
6544 * If we found a page that couldn't be invalidated just
6545 * fall back to buffered.
6546 */
6547 ret = invalidate_inode_pages2_range(inode->i_mapping,
6548 lockstart >> PAGE_CACHE_SHIFT,
6549 lockend >> PAGE_CACHE_SHIFT);
6550 if (ret)
6551 break;
6552 }
6553
6554 cond_resched();
6555 }
6556
6557 return ret;
6558}
6559
Josef Bacik69ffb542012-09-11 15:40:07 -04006560static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
6561 u64 len, u64 orig_start,
6562 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006563 u64 orig_block_len, u64 ram_bytes,
6564 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04006565{
6566 struct extent_map_tree *em_tree;
6567 struct extent_map *em;
6568 struct btrfs_root *root = BTRFS_I(inode)->root;
6569 int ret;
6570
6571 em_tree = &BTRFS_I(inode)->extent_tree;
6572 em = alloc_extent_map();
6573 if (!em)
6574 return ERR_PTR(-ENOMEM);
6575
6576 em->start = start;
6577 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04006578 em->mod_start = start;
6579 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04006580 em->len = len;
6581 em->block_len = block_len;
6582 em->block_start = block_start;
6583 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05006584 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04006585 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04006586 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04006587 set_bit(EXTENT_FLAG_PINNED, &em->flags);
6588 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05006589 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04006590
6591 do {
6592 btrfs_drop_extent_cache(inode, em->start,
6593 em->start + em->len - 1, 0);
6594 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006595 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04006596 write_unlock(&em_tree->lock);
6597 } while (ret == -EEXIST);
6598
6599 if (ret) {
6600 free_extent_map(em);
6601 return ERR_PTR(ret);
6602 }
6603
6604 return em;
6605}
6606
6607
Josef Bacik4b46fce2010-05-23 11:00:55 -04006608static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
6609 struct buffer_head *bh_result, int create)
6610{
6611 struct extent_map *em;
6612 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04006613 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006614 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04006615 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006616 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04006617 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00006618 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006619
Miao Xie172a5042013-02-21 02:48:22 -07006620 if (create)
Josef Bacikeb838e72012-07-31 16:28:48 -04006621 unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07006622 else
Josef Bacikc3298612012-08-03 16:49:19 -04006623 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04006624
Josef Bacikc3298612012-08-03 16:49:19 -04006625 lockstart = start;
6626 lockend = start + len - 1;
6627
Josef Bacikeb838e72012-07-31 16:28:48 -04006628 /*
6629 * If this errors out it's because we couldn't invalidate pagecache for
6630 * this range and we need to fallback to buffered.
6631 */
6632 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
6633 return -ENOTBLK;
6634
Josef Bacik4b46fce2010-05-23 11:00:55 -04006635 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04006636 if (IS_ERR(em)) {
6637 ret = PTR_ERR(em);
6638 goto unlock_err;
6639 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006640
6641 /*
6642 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
6643 * io. INLINE is special, and we could probably kludge it in here, but
6644 * it's still buffered so for safety lets just fall back to the generic
6645 * buffered path.
6646 *
6647 * For COMPRESSED we _have_ to read the entire extent in so we can
6648 * decompress it, so there will be buffering required no matter what we
6649 * do, so go ahead and fallback to buffered.
6650 *
6651 * We return -ENOTBLK because thats what makes DIO go ahead and go back
6652 * to buffered IO. Don't blame me, this is the price we pay for using
6653 * the generic code.
6654 */
6655 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
6656 em->block_start == EXTENT_MAP_INLINE) {
6657 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006658 ret = -ENOTBLK;
6659 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006660 }
6661
6662 /* Just a good old fashioned hole, return */
6663 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
6664 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
6665 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006666 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006667 }
6668
6669 /*
6670 * We don't allocate a new extent in the following cases
6671 *
6672 * 1) The inode is marked as NODATACOW. In this case we'll just use the
6673 * existing extent.
6674 * 2) The extent is marked as PREALLOC. We're good to go here and can
6675 * just use the extent.
6676 *
6677 */
Chris Mason46bfbb52010-05-26 11:04:10 -04006678 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04006679 len = min(len, em->len - (start - em->start));
6680 lockstart = start + len;
6681 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04006682 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006683
6684 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
6685 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
6686 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04006687 int type;
6688 int ret;
Josef Bacikeb384b52013-04-24 16:32:55 -04006689 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006690
6691 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6692 type = BTRFS_ORDERED_PREALLOC;
6693 else
6694 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04006695 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04006696 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04006697
Josef Bacik00361582013-08-14 14:02:47 -04006698 if (can_nocow_extent(inode, start, &len, &orig_start,
Josef Bacik7ee9e442013-06-21 16:37:03 -04006699 &orig_block_len, &ram_bytes) == 1) {
Josef Bacik69ffb542012-09-11 15:40:07 -04006700 if (type == BTRFS_ORDERED_PREALLOC) {
6701 free_extent_map(em);
6702 em = create_pinned_em(inode, start, len,
6703 orig_start,
Josef Bacikb4939682012-12-03 10:31:19 -05006704 block_start, len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006705 orig_block_len,
6706 ram_bytes, type);
Josef Bacik00361582013-08-14 14:02:47 -04006707 if (IS_ERR(em))
Josef Bacik69ffb542012-09-11 15:40:07 -04006708 goto unlock_err;
Josef Bacik69ffb542012-09-11 15:40:07 -04006709 }
6710
Chris Mason46bfbb52010-05-26 11:04:10 -04006711 ret = btrfs_add_ordered_extent_dio(inode, start,
6712 block_start, len, len, type);
Chris Mason46bfbb52010-05-26 11:04:10 -04006713 if (ret) {
6714 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006715 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04006716 }
6717 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006718 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006719 }
Josef Bacik00361582013-08-14 14:02:47 -04006720
Chris Mason46bfbb52010-05-26 11:04:10 -04006721 /*
6722 * this will cow the extent, reset the len in case we changed
6723 * it above
6724 */
6725 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04006726 free_extent_map(em);
6727 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04006728 if (IS_ERR(em)) {
6729 ret = PTR_ERR(em);
6730 goto unlock_err;
6731 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006732 len = min(len, em->len - (start - em->start));
6733unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04006734 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
6735 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04006736 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006737 bh_result->b_bdev = em->bdev;
6738 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04006739 if (create) {
6740 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6741 set_buffer_new(bh_result);
6742
6743 /*
6744 * Need to update the i_size under the extent lock so buffered
6745 * readers will get the updated i_size when we unlock.
6746 */
6747 if (start + len > i_size_read(inode))
6748 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00006749
Miao Xie172a5042013-02-21 02:48:22 -07006750 spin_lock(&BTRFS_I(inode)->lock);
6751 BTRFS_I(inode)->outstanding_extents++;
6752 spin_unlock(&BTRFS_I(inode)->lock);
6753
Miao Xie09348562013-02-07 10:12:07 +00006754 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6755 lockstart + len - 1, EXTENT_DELALLOC, NULL,
6756 &cached_state, GFP_NOFS);
6757 BUG_ON(ret);
Josef Bacikc3473e82012-06-19 10:59:00 -04006758 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006759
Josef Bacikeb838e72012-07-31 16:28:48 -04006760 /*
6761 * In the case of write we need to clear and unlock the entire range,
6762 * in the case of read we need to unlock only the end area that we
6763 * aren't using if there is any left over space.
6764 */
Liu Bo24c03fa2012-08-22 20:10:38 -06006765 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00006766 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6767 lockend, unlock_bits, 1, 0,
6768 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06006769 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04006770 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06006771 }
Josef Bacikeb838e72012-07-31 16:28:48 -04006772
Josef Bacik4b46fce2010-05-23 11:00:55 -04006773 free_extent_map(em);
6774
6775 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006776
6777unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04006778 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6779 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
6780 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006781}
6782
Josef Bacik4b46fce2010-05-23 11:00:55 -04006783static void btrfs_endio_direct_read(struct bio *bio, int err)
6784{
Miao Xiee65e1532010-11-22 03:04:43 +00006785 struct btrfs_dio_private *dip = bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006786 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
6787 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006788 struct inode *inode = dip->inode;
6789 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason9be33952013-05-17 18:30:14 -04006790 struct bio *dio_bio;
Miao Xiefacc8a222013-07-25 19:22:34 +08006791 u32 *csums = (u32 *)dip->csum;
6792 int index = 0;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006793 u64 start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006794
6795 start = dip->logical_offset;
6796 do {
6797 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
6798 struct page *page = bvec->bv_page;
6799 char *kaddr;
6800 u32 csum = ~(u32)0;
6801 unsigned long flags;
6802
6803 local_irq_save(flags);
Cong Wang7ac687d2011-11-25 23:14:28 +08006804 kaddr = kmap_atomic(page);
Liu Bob0496682013-03-14 14:57:45 +00006805 csum = btrfs_csum_data(kaddr + bvec->bv_offset,
Josef Bacik4b46fce2010-05-23 11:00:55 -04006806 csum, bvec->bv_len);
6807 btrfs_csum_final(csum, (char *)&csum);
Cong Wang7ac687d2011-11-25 23:14:28 +08006808 kunmap_atomic(kaddr);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006809 local_irq_restore(flags);
6810
6811 flush_dcache_page(bvec->bv_page);
Miao Xiefacc8a222013-07-25 19:22:34 +08006812 if (csum != csums[index]) {
6813 btrfs_err(root->fs_info, "csum failed ino %llu off %llu csum %u expected csum %u",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006814 btrfs_ino(inode), start, csum,
6815 csums[index]);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006816 err = -EIO;
6817 }
6818 }
6819
6820 start += bvec->bv_len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006821 bvec++;
Miao Xiefacc8a222013-07-25 19:22:34 +08006822 index++;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006823 } while (bvec <= bvec_end);
6824
6825 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006826 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04006827 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006828
Josef Bacik4b46fce2010-05-23 11:00:55 -04006829 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006830
6831 /* If we had a csum failure make sure to clear the uptodate flag */
6832 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04006833 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
6834 dio_end_io(dio_bio, err);
6835 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006836}
6837
6838static void btrfs_endio_direct_write(struct bio *bio, int err)
6839{
6840 struct btrfs_dio_private *dip = bio->bi_private;
6841 struct inode *inode = dip->inode;
6842 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006843 struct btrfs_ordered_extent *ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05006844 u64 ordered_offset = dip->logical_offset;
6845 u64 ordered_bytes = dip->bytes;
Chris Mason9be33952013-05-17 18:30:14 -04006846 struct bio *dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006847 int ret;
6848
6849 if (err)
6850 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05006851again:
6852 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
6853 &ordered_offset,
Josef Bacik5fd02042012-05-02 14:00:54 -04006854 ordered_bytes, !err);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006855 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05006856 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006857
Josef Bacik5fd02042012-05-02 14:00:54 -04006858 ordered->work.func = finish_ordered_fn;
6859 ordered->work.flags = 0;
6860 btrfs_queue_worker(&root->fs_info->endio_write_workers,
6861 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05006862out_test:
6863 /*
6864 * our bio might span multiple ordered extents. If we haven't
6865 * completed the accounting for the whole dio, go back and try again
6866 */
6867 if (ordered_offset < dip->logical_offset + dip->bytes) {
6868 ordered_bytes = dip->logical_offset + dip->bytes -
6869 ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04006870 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05006871 goto again;
6872 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006873out_done:
Chris Mason9be33952013-05-17 18:30:14 -04006874 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006875
Josef Bacik4b46fce2010-05-23 11:00:55 -04006876 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006877
6878 /* If we had an error make sure to clear the uptodate flag */
6879 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04006880 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
6881 dio_end_io(dio_bio, err);
6882 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006883}
6884
Chris Masoneaf25d92010-05-25 09:48:28 -04006885static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
6886 struct bio *bio, int mirror_num,
6887 unsigned long bio_flags, u64 offset)
6888{
6889 int ret;
6890 struct btrfs_root *root = BTRFS_I(inode)->root;
6891 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006892 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04006893 return 0;
6894}
6895
Miao Xiee65e1532010-11-22 03:04:43 +00006896static void btrfs_end_dio_bio(struct bio *bio, int err)
6897{
6898 struct btrfs_dio_private *dip = bio->bi_private;
6899
6900 if (err) {
Li Zefan33345d012011-04-20 10:31:50 +08006901 printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu "
Jan Beulich3dd14622010-12-07 14:54:09 +00006902 "sector %#Lx len %u err no %d\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006903 btrfs_ino(dip->inode), bio->bi_rw,
Jan Beulich3dd14622010-12-07 14:54:09 +00006904 (unsigned long long)bio->bi_sector, bio->bi_size, err);
Miao Xiee65e1532010-11-22 03:04:43 +00006905 dip->errors = 1;
6906
6907 /*
6908 * before atomic variable goto zero, we must make sure
6909 * dip->errors is perceived to be set.
6910 */
6911 smp_mb__before_atomic_dec();
6912 }
6913
6914 /* if there are more bios still pending for this dio, just exit */
6915 if (!atomic_dec_and_test(&dip->pending_bios))
6916 goto out;
6917
Chris Mason9be33952013-05-17 18:30:14 -04006918 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00006919 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04006920 } else {
6921 set_bit(BIO_UPTODATE, &dip->dio_bio->bi_flags);
Miao Xiee65e1532010-11-22 03:04:43 +00006922 bio_endio(dip->orig_bio, 0);
6923 }
6924out:
6925 bio_put(bio);
6926}
6927
6928static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
6929 u64 first_sector, gfp_t gfp_flags)
6930{
6931 int nr_vecs = bio_get_nr_vecs(bdev);
6932 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
6933}
6934
6935static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
6936 int rw, u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04006937 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00006938{
Miao Xiefacc8a222013-07-25 19:22:34 +08006939 struct btrfs_dio_private *dip = bio->bi_private;
Miao Xiee65e1532010-11-22 03:04:43 +00006940 int write = rw & REQ_WRITE;
6941 struct btrfs_root *root = BTRFS_I(inode)->root;
6942 int ret;
6943
Josef Bacikb812ce22012-11-16 13:56:32 -05006944 if (async_submit)
6945 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
6946
Miao Xiee65e1532010-11-22 03:04:43 +00006947 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04006948
6949 if (!write) {
6950 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
6951 if (ret)
6952 goto err;
6953 }
Miao Xiee65e1532010-11-22 03:04:43 +00006954
Josef Bacik1ae39932011-04-06 14:41:34 -04006955 if (skip_sum)
6956 goto map;
6957
6958 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00006959 ret = btrfs_wq_submit_bio(root->fs_info,
6960 inode, rw, bio, 0, 0,
6961 file_offset,
6962 __btrfs_submit_bio_start_direct_io,
6963 __btrfs_submit_bio_done);
6964 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04006965 } else if (write) {
6966 /*
6967 * If we aren't doing async submit, calculate the csum of the
6968 * bio now.
6969 */
6970 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
6971 if (ret)
6972 goto err;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00006973 } else if (!skip_sum) {
Miao Xiefacc8a222013-07-25 19:22:34 +08006974 ret = btrfs_lookup_bio_sums_dio(root, inode, dip, bio,
6975 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00006976 if (ret)
6977 goto err;
6978 }
Miao Xiee65e1532010-11-22 03:04:43 +00006979
Josef Bacik1ae39932011-04-06 14:41:34 -04006980map:
6981 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006982err:
6983 bio_put(bio);
6984 return ret;
6985}
6986
6987static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
6988 int skip_sum)
6989{
6990 struct inode *inode = dip->inode;
6991 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00006992 struct bio *bio;
6993 struct bio *orig_bio = dip->orig_bio;
6994 struct bio_vec *bvec = orig_bio->bi_io_vec;
6995 u64 start_sector = orig_bio->bi_sector;
6996 u64 file_offset = dip->logical_offset;
6997 u64 submit_len = 0;
6998 u64 map_length;
6999 int nr_pages = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007000 int ret = 0;
Josef Bacik1ae39932011-04-06 14:41:34 -04007001 int async_submit = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007002
Miao Xiee65e1532010-11-22 03:04:43 +00007003 map_length = orig_bio->bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007004 ret = btrfs_map_block(root->fs_info, rw, start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007005 &map_length, NULL, 0);
7006 if (ret) {
Josef Bacik64728bb2011-04-25 19:43:52 -04007007 bio_put(orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00007008 return -EIO;
7009 }
Miao Xiefacc8a222013-07-25 19:22:34 +08007010
Josef Bacik02f57c72011-04-06 14:25:44 -04007011 if (map_length >= orig_bio->bi_size) {
7012 bio = orig_bio;
7013 goto submit;
7014 }
7015
David Woodhouse53b381b2013-01-29 18:40:14 -05007016 /* async crcs make it difficult to collect full stripe writes. */
7017 if (btrfs_get_alloc_profile(root, 1) &
7018 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6))
7019 async_submit = 0;
7020 else
7021 async_submit = 1;
7022
Josef Bacik02f57c72011-04-06 14:25:44 -04007023 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
7024 if (!bio)
7025 return -ENOMEM;
7026 bio->bi_private = dip;
7027 bio->bi_end_io = btrfs_end_dio_bio;
7028 atomic_inc(&dip->pending_bios);
7029
Miao Xiee65e1532010-11-22 03:04:43 +00007030 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
7031 if (unlikely(map_length < submit_len + bvec->bv_len ||
7032 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
7033 bvec->bv_offset) < bvec->bv_len)) {
7034 /*
7035 * inc the count before we submit the bio so
7036 * we know the end IO handler won't happen before
7037 * we inc the count. Otherwise, the dip might get freed
7038 * before we're done setting it up
7039 */
7040 atomic_inc(&dip->pending_bios);
7041 ret = __btrfs_submit_dio_bio(bio, inode, rw,
7042 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007043 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007044 if (ret) {
7045 bio_put(bio);
7046 atomic_dec(&dip->pending_bios);
7047 goto out_err;
7048 }
7049
Miao Xiee65e1532010-11-22 03:04:43 +00007050 start_sector += submit_len >> 9;
7051 file_offset += submit_len;
7052
7053 submit_len = 0;
7054 nr_pages = 0;
7055
7056 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
7057 start_sector, GFP_NOFS);
7058 if (!bio)
7059 goto out_err;
7060 bio->bi_private = dip;
7061 bio->bi_end_io = btrfs_end_dio_bio;
7062
7063 map_length = orig_bio->bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007064 ret = btrfs_map_block(root->fs_info, rw,
Stefan Behrens3ec706c2012-11-05 15:46:42 +01007065 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007066 &map_length, NULL, 0);
7067 if (ret) {
7068 bio_put(bio);
7069 goto out_err;
7070 }
7071 } else {
7072 submit_len += bvec->bv_len;
7073 nr_pages ++;
7074 bvec++;
7075 }
7076 }
7077
Josef Bacik02f57c72011-04-06 14:25:44 -04007078submit:
Miao Xiee65e1532010-11-22 03:04:43 +00007079 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007080 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007081 if (!ret)
7082 return 0;
7083
7084 bio_put(bio);
7085out_err:
7086 dip->errors = 1;
7087 /*
7088 * before atomic variable goto zero, we must
7089 * make sure dip->errors is perceived to be set.
7090 */
7091 smp_mb__before_atomic_dec();
7092 if (atomic_dec_and_test(&dip->pending_bios))
7093 bio_io_error(dip->orig_bio);
7094
7095 /* bio_end_io() will handle error, so we needn't return it */
7096 return 0;
7097}
7098
Chris Mason9be33952013-05-17 18:30:14 -04007099static void btrfs_submit_direct(int rw, struct bio *dio_bio,
7100 struct inode *inode, loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007101{
7102 struct btrfs_root *root = BTRFS_I(inode)->root;
7103 struct btrfs_dio_private *dip;
Chris Mason9be33952013-05-17 18:30:14 -04007104 struct bio *io_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007105 int skip_sum;
Miao Xiefacc8a222013-07-25 19:22:34 +08007106 int sum_len;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02007107 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007108 int ret = 0;
Miao Xiefacc8a222013-07-25 19:22:34 +08007109 u16 csum_size;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007110
7111 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
7112
Chris Mason9be33952013-05-17 18:30:14 -04007113 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04007114 if (!io_bio) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007115 ret = -ENOMEM;
7116 goto free_ordered;
7117 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007118
Miao Xiefacc8a222013-07-25 19:22:34 +08007119 if (!skip_sum && !write) {
7120 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
7121 sum_len = dio_bio->bi_size >> inode->i_sb->s_blocksize_bits;
7122 sum_len *= csum_size;
7123 } else {
7124 sum_len = 0;
7125 }
7126
7127 dip = kmalloc(sizeof(*dip) + sum_len, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04007128 if (!dip) {
7129 ret = -ENOMEM;
7130 goto free_io_bio;
7131 }
7132
7133 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007134 dip->inode = inode;
7135 dip->logical_offset = file_offset;
Miao Xiee6da5d22013-06-19 18:19:17 +08007136 dip->bytes = dio_bio->bi_size;
Chris Mason9be33952013-05-17 18:30:14 -04007137 dip->disk_bytenr = (u64)dio_bio->bi_sector << 9;
7138 io_bio->bi_private = dip;
Miao Xiee65e1532010-11-22 03:04:43 +00007139 dip->errors = 0;
Chris Mason9be33952013-05-17 18:30:14 -04007140 dip->orig_bio = io_bio;
7141 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00007142 atomic_set(&dip->pending_bios, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007143
7144 if (write)
Chris Mason9be33952013-05-17 18:30:14 -04007145 io_bio->bi_end_io = btrfs_endio_direct_write;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007146 else
Chris Mason9be33952013-05-17 18:30:14 -04007147 io_bio->bi_end_io = btrfs_endio_direct_read;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007148
Miao Xiee65e1532010-11-22 03:04:43 +00007149 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
7150 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04007151 return;
Chris Mason9be33952013-05-17 18:30:14 -04007152
7153free_io_bio:
7154 bio_put(io_bio);
7155
Josef Bacik4b46fce2010-05-23 11:00:55 -04007156free_ordered:
7157 /*
7158 * If this is a write, we need to clean up the reserved space and kill
7159 * the ordered extent.
7160 */
7161 if (write) {
7162 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05007163 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007164 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
7165 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
7166 btrfs_free_reserved_extent(root, ordered->start,
7167 ordered->disk_len);
7168 btrfs_put_ordered_extent(ordered);
7169 btrfs_put_ordered_extent(ordered);
7170 }
Chris Mason9be33952013-05-17 18:30:14 -04007171 bio_endio(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007172}
7173
Chris Mason5a5f79b2010-05-26 21:33:37 -04007174static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
7175 const struct iovec *iov, loff_t offset,
7176 unsigned long nr_segs)
7177{
7178 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00007179 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007180 size_t size;
7181 unsigned long addr;
7182 unsigned blocksize_mask = root->sectorsize - 1;
7183 ssize_t retval = -EINVAL;
7184 loff_t end = offset;
7185
7186 if (offset & blocksize_mask)
7187 goto out;
7188
7189 /* Check the memory alignment. Blocks cannot straddle pages */
7190 for (seg = 0; seg < nr_segs; seg++) {
7191 addr = (unsigned long)iov[seg].iov_base;
7192 size = iov[seg].iov_len;
7193 end += size;
Josef Bacika1b75f72011-04-08 15:51:18 +00007194 if ((addr & blocksize_mask) || (size & blocksize_mask))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007195 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00007196
7197 /* If this is a write we don't need to check anymore */
7198 if (rw & WRITE)
7199 continue;
7200
7201 /*
7202 * Check to make sure we don't have duplicate iov_base's in this
7203 * iovec, if so return EINVAL, otherwise we'll get csum errors
7204 * when reading back.
7205 */
7206 for (i = seg + 1; i < nr_segs; i++) {
7207 if (iov[seg].iov_base == iov[i].iov_base)
7208 goto out;
7209 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04007210 }
7211 retval = 0;
7212out:
7213 return retval;
7214}
Josef Bacikeb838e72012-07-31 16:28:48 -04007215
Chris Mason16432982008-04-10 10:23:21 -04007216static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
7217 const struct iovec *iov, loff_t offset,
7218 unsigned long nr_segs)
7219{
Josef Bacik4b46fce2010-05-23 11:00:55 -04007220 struct file *file = iocb->ki_filp;
7221 struct inode *inode = file->f_mapping->host;
Miao Xie09348562013-02-07 10:12:07 +00007222 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00007223 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00007224 bool wakeup = true;
7225 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00007226 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007227
Chris Mason5a5f79b2010-05-26 21:33:37 -04007228 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
Josef Bacikeb838e72012-07-31 16:28:48 -04007229 offset, nr_segs))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007230 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007231
Miao Xie38851cc2013-02-08 07:04:11 +00007232 atomic_inc(&inode->i_dio_count);
7233 smp_mb__after_atomic_inc();
7234
Josef Bacik0e267c42013-07-02 10:38:02 -04007235 /*
7236 * The generic stuff only does filemap_write_and_wait_range, which isn't
7237 * enough if we've written compressed pages to this area, so we need to
7238 * call btrfs_wait_ordered_range to make absolutely sure that any
7239 * outstanding dirty pages are on disk.
7240 */
7241 count = iov_length(iov, nr_segs);
Josef Bacik0ef8b722013-10-25 16:13:35 -04007242 ret = btrfs_wait_ordered_range(inode, offset, count);
7243 if (ret)
7244 return ret;
Josef Bacik0e267c42013-07-02 10:38:02 -04007245
Miao Xie09348562013-02-07 10:12:07 +00007246 if (rw & WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00007247 /*
7248 * If the write DIO is beyond the EOF, we need update
7249 * the isize, but it is protected by i_mutex. So we can
7250 * not unlock the i_mutex at this case.
7251 */
7252 if (offset + count <= inode->i_size) {
7253 mutex_unlock(&inode->i_mutex);
7254 relock = true;
7255 }
Miao Xie09348562013-02-07 10:12:07 +00007256 ret = btrfs_delalloc_reserve_space(inode, count);
7257 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00007258 goto out;
7259 } else if (unlikely(test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
7260 &BTRFS_I(inode)->runtime_flags))) {
7261 inode_dio_done(inode);
7262 flags = DIO_LOCKING | DIO_SKIP_HOLES;
7263 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00007264 }
7265
7266 ret = __blockdev_direct_IO(rw, iocb, inode,
7267 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
7268 iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
Miao Xie2e60a512013-02-08 07:01:08 +00007269 btrfs_submit_direct, flags);
Miao Xie09348562013-02-07 10:12:07 +00007270 if (rw & WRITE) {
7271 if (ret < 0 && ret != -EIOCBQUEUED)
7272 btrfs_delalloc_release_space(inode, count);
Miao Xie172a5042013-02-21 02:48:22 -07007273 else if (ret >= 0 && (size_t)ret < count)
Miao Xie09348562013-02-07 10:12:07 +00007274 btrfs_delalloc_release_space(inode,
7275 count - (size_t)ret);
Miao Xie172a5042013-02-21 02:48:22 -07007276 else
7277 btrfs_delalloc_release_metadata(inode, 0);
Miao Xie09348562013-02-07 10:12:07 +00007278 }
Miao Xie38851cc2013-02-08 07:04:11 +00007279out:
Miao Xie2e60a512013-02-08 07:01:08 +00007280 if (wakeup)
7281 inode_dio_done(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00007282 if (relock)
7283 mutex_lock(&inode->i_mutex);
Miao Xie09348562013-02-07 10:12:07 +00007284
7285 return ret;
Chris Mason16432982008-04-10 10:23:21 -04007286}
7287
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007288#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
7289
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007290static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
7291 __u64 start, __u64 len)
7292{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007293 int ret;
7294
7295 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
7296 if (ret)
7297 return ret;
7298
Chris Masonec29ed52011-02-23 16:23:20 -05007299 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007300}
7301
Chris Mason9ebefb182007-06-15 13:50:00 -04007302int btrfs_readpage(struct file *file, struct page *page)
7303{
Chris Masond1310b22008-01-24 16:13:08 -05007304 struct extent_io_tree *tree;
7305 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02007306 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04007307}
Chris Mason1832a6d2007-12-21 16:27:21 -05007308
Chris Mason39279cc2007-06-12 06:35:45 -04007309static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
7310{
Chris Masond1310b22008-01-24 16:13:08 -05007311 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04007312
7313
7314 if (current->flags & PF_MEMALLOC) {
7315 redirty_page_for_writepage(wbc, page);
7316 unlock_page(page);
7317 return 0;
7318 }
Chris Masond1310b22008-01-24 16:13:08 -05007319 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04007320 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
7321}
Chris Mason39279cc2007-06-12 06:35:45 -04007322
Eric Sandeen48a3b632013-04-25 20:41:01 +00007323static int btrfs_writepages(struct address_space *mapping,
7324 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04007325{
Chris Masond1310b22008-01-24 16:13:08 -05007326 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05007327
Chris Masond1310b22008-01-24 16:13:08 -05007328 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04007329 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
7330}
7331
Chris Mason3ab2fb52007-11-08 10:59:22 -05007332static int
7333btrfs_readpages(struct file *file, struct address_space *mapping,
7334 struct list_head *pages, unsigned nr_pages)
7335{
Chris Masond1310b22008-01-24 16:13:08 -05007336 struct extent_io_tree *tree;
7337 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05007338 return extent_readpages(tree, mapping, pages, nr_pages,
7339 btrfs_get_extent);
7340}
Chris Masone6dcd2d2008-07-17 12:53:50 -04007341static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04007342{
Chris Masond1310b22008-01-24 16:13:08 -05007343 struct extent_io_tree *tree;
7344 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04007345 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04007346
Chris Masond1310b22008-01-24 16:13:08 -05007347 tree = &BTRFS_I(page->mapping->host)->io_tree;
7348 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05007349 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04007350 if (ret == 1) {
7351 ClearPagePrivate(page);
7352 set_page_private(page, 0);
7353 page_cache_release(page);
7354 }
7355 return ret;
7356}
Chris Mason39279cc2007-06-12 06:35:45 -04007357
Chris Masone6dcd2d2008-07-17 12:53:50 -04007358static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
7359{
Chris Mason98509cfc2008-09-11 15:51:43 -04007360 if (PageWriteback(page) || PageDirty(page))
7361 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05007362 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007363}
7364
Lukas Czernerd47992f2013-05-21 23:17:23 -04007365static void btrfs_invalidatepage(struct page *page, unsigned int offset,
7366 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04007367{
Josef Bacik5fd02042012-05-02 14:00:54 -04007368 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05007369 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007370 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007371 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007372 u64 page_start = page_offset(page);
7373 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04007374
Chris Mason8b62b722009-09-02 16:53:46 -04007375 /*
7376 * we have the page locked, so new writeback can't start,
7377 * and the dirty bit won't be cleared while we are here.
7378 *
7379 * Wait for IO on this page so that we can safely clear
7380 * the PagePrivate2 bit and do ordered accounting
7381 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007382 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04007383
Josef Bacik5fd02042012-05-02 14:00:54 -04007384 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007385 if (offset) {
7386 btrfs_releasepage(page, GFP_NOFS);
7387 return;
7388 }
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007389 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Miao Xie4eee4fa2012-12-21 09:17:45 +00007390 ordered = btrfs_lookup_ordered_extent(inode, page_offset(page));
Chris Masone6dcd2d2008-07-17 12:53:50 -04007391 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04007392 /*
7393 * IO on this page will never be started, so we need
7394 * to account for any ordered extents now
7395 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007396 clear_extent_bit(tree, page_start, page_end,
7397 EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06007398 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
7399 EXTENT_DEFRAG, 1, 0, &cached_state, GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04007400 /*
7401 * whoever cleared the private bit is responsible
7402 * for the finish_ordered_io
7403 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04007404 if (TestClearPagePrivate2(page)) {
7405 struct btrfs_ordered_inode_tree *tree;
7406 u64 new_len;
7407
7408 tree = &BTRFS_I(inode)->ordered_tree;
7409
7410 spin_lock_irq(&tree->lock);
7411 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
7412 new_len = page_start - ordered->file_offset;
7413 if (new_len < ordered->truncated_len)
7414 ordered->truncated_len = new_len;
7415 spin_unlock_irq(&tree->lock);
7416
7417 if (btrfs_dec_test_ordered_pending(inode, &ordered,
7418 page_start,
7419 PAGE_CACHE_SIZE, 1))
7420 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04007421 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007422 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +00007423 cached_state = NULL;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007424 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007425 }
7426 clear_extent_bit(tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04007427 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06007428 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
7429 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007430 __btrfs_releasepage(page, GFP_NOFS);
7431
Chris Mason4a096752008-07-21 10:29:44 -04007432 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04007433 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04007434 ClearPagePrivate(page);
7435 set_page_private(page, 0);
7436 page_cache_release(page);
7437 }
Chris Mason39279cc2007-06-12 06:35:45 -04007438}
7439
Chris Mason9ebefb182007-06-15 13:50:00 -04007440/*
7441 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
7442 * called from a page fault handler when a page is first dirtied. Hence we must
7443 * be careful to check for EOF conditions here. We set the page up correctly
7444 * for a written page which means we get ENOSPC checking when writing into
7445 * holes and correct delalloc and unwritten extent mapping on filesystems that
7446 * support these features.
7447 *
7448 * We are not allowed to take the i_mutex here so we have to play games to
7449 * protect against truncate races as the page could now be beyond EOF. Because
7450 * vmtruncate() writes the inode size before removing pages, once we have the
7451 * page lock we can determine safely if the page is beyond EOF. If it is not
7452 * beyond EOF, then the page is guaranteed safe against truncation until we
7453 * unlock the page.
7454 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07007455int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04007456{
Nick Pigginc2ec1752009-03-31 15:23:21 -07007457 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05007458 struct inode *inode = file_inode(vma->vm_file);
Chris Mason1832a6d2007-12-21 16:27:21 -05007459 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007460 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7461 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007462 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007463 char *kaddr;
7464 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04007465 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05007466 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05007467 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007468 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007469 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04007470
Jan Karab2b5ef52012-06-12 16:20:45 +02007471 sb_start_pagefault(inode->i_sb);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04007472 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007473 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04007474 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05007475 reserved = 1;
7476 }
Nick Piggin56a76f82009-03-31 15:23:23 -07007477 if (ret) {
7478 if (ret == -ENOMEM)
7479 ret = VM_FAULT_OOM;
7480 else /* -ENOSPC, -EIO, etc */
7481 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05007482 if (reserved)
7483 goto out;
7484 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07007485 }
Chris Mason1832a6d2007-12-21 16:27:21 -05007486
Nick Piggin56a76f82009-03-31 15:23:23 -07007487 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007488again:
Chris Mason9ebefb182007-06-15 13:50:00 -04007489 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04007490 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007491 page_start = page_offset(page);
7492 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04007493
Chris Mason9ebefb182007-06-15 13:50:00 -04007494 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04007495 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04007496 /* page got truncated out from underneath us */
7497 goto out_unlock;
7498 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007499 wait_on_page_writeback(page);
7500
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007501 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007502 set_page_extent_mapped(page);
7503
Chris Masoneb84ae02008-07-17 13:53:27 -04007504 /*
7505 * we can't set the delalloc bits if there are pending ordered
7506 * extents. Drop our locks and wait for them to finish
7507 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007508 ordered = btrfs_lookup_ordered_extent(inode, page_start);
7509 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007510 unlock_extent_cached(io_tree, page_start, page_end,
7511 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007512 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04007513 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007514 btrfs_put_ordered_extent(ordered);
7515 goto again;
7516 }
7517
Josef Bacikfbf19082009-10-01 17:10:23 -04007518 /*
7519 * XXX - page_mkwrite gets called every time the page is dirtied, even
7520 * if it was already dirty, so for space accounting reasons we need to
7521 * clear any delalloc bits for the range we are fixing to save. There
7522 * is probably a better way to do this, but for now keep consistent with
7523 * prepare_pages in the normal write path.
7524 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00007525 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06007526 EXTENT_DIRTY | EXTENT_DELALLOC |
7527 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00007528 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04007529
Josef Bacik2ac55d42010-02-03 19:33:23 +00007530 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
7531 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007532 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007533 unlock_extent_cached(io_tree, page_start, page_end,
7534 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007535 ret = VM_FAULT_SIGBUS;
7536 goto out_unlock;
7537 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007538 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04007539
7540 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04007541 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04007542 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04007543 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04007544 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04007545
Chris Masone6dcd2d2008-07-17 12:53:50 -04007546 if (zero_start != PAGE_CACHE_SIZE) {
7547 kaddr = kmap(page);
7548 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
7549 flush_dcache_page(page);
7550 kunmap(page);
7551 }
Chris Mason247e7432008-07-17 12:53:51 -04007552 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007553 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04007554 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007555
Chris Mason257c62e2009-10-13 13:21:08 -04007556 BTRFS_I(inode)->last_trans = root->fs_info->generation;
7557 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06007558 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04007559
Josef Bacik2ac55d42010-02-03 19:33:23 +00007560 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04007561
7562out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02007563 if (!ret) {
7564 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04007565 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02007566 }
Chris Mason9ebefb182007-06-15 13:50:00 -04007567 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05007568out:
Josef Bacikec39e182012-01-12 19:10:12 -05007569 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007570out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02007571 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04007572 return ret;
7573}
7574
Josef Bacika41ad392011-01-31 15:30:16 -05007575static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04007576{
7577 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04007578 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04007579 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05007580 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007581 struct btrfs_trans_handle *trans;
Chris Masondbe674a2008-07-17 12:54:05 -04007582 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04007583 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04007584
Josef Bacik0ef8b722013-10-25 16:13:35 -04007585 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
7586 (u64)-1);
7587 if (ret)
7588 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04007589
Josef Bacikfcb80c22011-05-03 10:40:22 -04007590 /*
7591 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
7592 * 3 things going on here
7593 *
7594 * 1) We need to reserve space for our orphan item and the space to
7595 * delete our orphan item. Lord knows we don't want to have a dangling
7596 * orphan item because we didn't reserve space to remove it.
7597 *
7598 * 2) We need to reserve space to update our inode.
7599 *
7600 * 3) We need to have something to cache all the space that is going to
7601 * be free'd up by the truncate operation, but also have some slack
7602 * space reserved in case it uses space during the truncate (thank you
7603 * very much snapshotting).
7604 *
7605 * And we need these to all be seperate. The fact is we can use alot of
7606 * space doing the truncate, and we have no earthly idea how much space
7607 * we will use, so we need the truncate reservation to be seperate so it
7608 * doesn't end up using space reserved for updating the inode or
7609 * removing the orphan item. We also need to be able to stop the
7610 * transaction and start a new one, which means we need to be able to
7611 * update the inode several times, and we have no idea of knowing how
7612 * many times that will be, so we can't just reserve 1 item for the
7613 * entirety of the opration, so that has to be done seperately as well.
7614 * Then there is the orphan item, which does indeed need to be held on
7615 * to for the whole operation, and we need nobody to touch this reserved
7616 * space except the orphan code.
7617 *
7618 * So that leaves us with
7619 *
7620 * 1) root->orphan_block_rsv - for the orphan deletion.
7621 * 2) rsv - for the truncate reservation, which we will steal from the
7622 * transaction reservation.
7623 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
7624 * updating the inode.
7625 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06007626 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007627 if (!rsv)
7628 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04007629 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04007630 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05007631
Josef Bacik907cbce2011-08-08 13:46:15 -04007632 /*
Josef Bacik07127182011-08-19 10:29:59 -04007633 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04007634 * 1 for updating the inode.
7635 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05007636 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007637 if (IS_ERR(trans)) {
7638 err = PTR_ERR(trans);
7639 goto out;
7640 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05007641
Josef Bacik907cbce2011-08-08 13:46:15 -04007642 /* Migrate the slack space for the truncate to our reserve */
7643 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
7644 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007645 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05007646
Chris Mason5a3f23d2009-03-31 13:27:11 -04007647 /*
7648 * setattr is responsible for setting the ordered_data_close flag,
7649 * but that is only tested during the last file release. That
7650 * could happen well after the next commit, leaving a great big
7651 * window where new writes may get lost if someone chooses to write
7652 * to this file after truncating to zero
7653 *
7654 * The inode doesn't have any dirty data here, and so if we commit
7655 * this is a noop. If someone immediately starts writing to the inode
7656 * it is very likely we'll catch some of their writes in this
7657 * transaction, and the commit will find this file on the ordered
7658 * data list with good things to send down.
7659 *
7660 * This is a best effort solution, there is still a window where
7661 * using truncate to replace the contents of the file will
7662 * end up with a zero length file after a crash.
7663 */
Josef Bacik72ac3c02012-05-23 14:13:11 -04007664 if (inode->i_size == 0 && test_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
7665 &BTRFS_I(inode)->runtime_flags))
Chris Mason5a3f23d2009-03-31 13:27:11 -04007666 btrfs_add_ordered_operation(trans, root, inode);
7667
Josef Bacik5dc562c2012-08-17 13:14:17 -04007668 /*
7669 * So if we truncate and then write and fsync we normally would just
7670 * write the extents that changed, which is a problem if we need to
7671 * first truncate that entire inode. So set this flag so we write out
7672 * all of the extents in the inode to the sync log so we're completely
7673 * safe.
7674 */
7675 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007676 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04007677
Yan, Zheng80825102009-11-12 09:35:36 +00007678 while (1) {
7679 ret = btrfs_truncate_inode_items(trans, root, inode,
7680 inode->i_size,
7681 BTRFS_EXTENT_DATA_KEY);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007682 if (ret != -ENOSPC) {
Josef Bacik3893e332011-01-31 16:03:11 -05007683 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007684 break;
Josef Bacik3893e332011-01-31 16:03:11 -05007685 }
Chris Mason39279cc2007-06-12 06:35:45 -04007686
Josef Bacikfcb80c22011-05-03 10:40:22 -04007687 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007688 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007689 if (ret) {
7690 err = ret;
7691 break;
7692 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04007693
Yan, Zheng80825102009-11-12 09:35:36 +00007694 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007695 btrfs_btree_balance_dirty(root);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007696
7697 trans = btrfs_start_transaction(root, 2);
7698 if (IS_ERR(trans)) {
7699 ret = err = PTR_ERR(trans);
7700 trans = NULL;
7701 break;
7702 }
7703
7704 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
7705 rsv, min_size);
7706 BUG_ON(ret); /* shouldn't happen */
7707 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007708 }
7709
7710 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04007711 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007712 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007713 if (ret)
7714 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007715 }
7716
Chris Mason917c16b2011-11-08 14:49:59 -05007717 if (trans) {
7718 trans->block_rsv = &root->fs_info->trans_block_rsv;
7719 ret = btrfs_update_inode(trans, root, inode);
7720 if (ret && !err)
7721 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04007722
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007723 ret = btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007724 btrfs_btree_balance_dirty(root);
Chris Mason917c16b2011-11-08 14:49:59 -05007725 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04007726
7727out:
7728 btrfs_free_block_rsv(root, rsv);
7729
Josef Bacik3893e332011-01-31 16:03:11 -05007730 if (ret && !err)
7731 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05007732
Josef Bacik3893e332011-01-31 16:03:11 -05007733 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007734}
7735
Sven Wegener3b963622008-06-09 21:57:42 -04007736/*
Chris Masond352ac62008-09-29 15:18:18 -04007737 * create a new subvolume directory/inode (helper for the ioctl).
7738 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05007739int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Josef Bacikd82a6f12011-05-11 15:26:06 -04007740 struct btrfs_root *new_root, u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04007741{
Chris Mason39279cc2007-06-12 06:35:45 -04007742 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04007743 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007744 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007745
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01007746 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
7747 new_dirid, new_dirid,
7748 S_IFDIR | (~current_umask() & S_IRWXUGO),
7749 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04007750 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04007751 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007752 inode->i_op = &btrfs_dir_inode_operations;
7753 inode->i_fop = &btrfs_dir_file_operations;
7754
Miklos Szeredibfe86842011-10-28 14:13:29 +02007755 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04007756 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04007757
Yan, Zheng76dda932009-09-21 16:00:26 -04007758 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04007759
Yan, Zheng76dda932009-09-21 16:00:26 -04007760 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07007761 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007762}
7763
Chris Mason39279cc2007-06-12 06:35:45 -04007764struct inode *btrfs_alloc_inode(struct super_block *sb)
7765{
7766 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007767 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007768
7769 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
7770 if (!ei)
7771 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007772
7773 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007774 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04007775 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04007776 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04007777 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007778 ei->delalloc_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007779 ei->disk_i_size = 0;
7780 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04007781 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007782 ei->index_cnt = (u64)-1;
7783 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06007784 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007785
Josef Bacik9e0baf62011-07-15 15:16:44 +00007786 spin_lock_init(&ei->lock);
7787 ei->outstanding_extents = 0;
7788 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007789
Josef Bacik72ac3c02012-05-23 14:13:11 -04007790 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08007791 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007792
Miao Xie16cdcec2011-04-22 18:12:22 +08007793 ei->delayed_node = NULL;
7794
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007795 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02007796 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02007797 extent_io_tree_init(&ei->io_tree, &inode->i_data);
7798 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04007799 ei->io_tree.track_uptodate = 1;
7800 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05007801 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007802 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05007803 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007804 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007805 INIT_LIST_HEAD(&ei->delalloc_inodes);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007806 INIT_LIST_HEAD(&ei->ordered_operations);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007807 RB_CLEAR_NODE(&ei->rb_node);
7808
7809 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007810}
7811
Josef Bacikaaedb552013-10-11 14:44:09 -04007812#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
7813void btrfs_test_destroy_inode(struct inode *inode)
7814{
7815 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
7816 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
7817}
7818#endif
7819
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007820static void btrfs_i_callback(struct rcu_head *head)
7821{
7822 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007823 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
7824}
7825
Chris Mason39279cc2007-06-12 06:35:45 -04007826void btrfs_destroy_inode(struct inode *inode)
7827{
Chris Masone6dcd2d2008-07-17 12:53:50 -04007828 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007829 struct btrfs_root *root = BTRFS_I(inode)->root;
7830
Al Virob3d9b7a2012-06-09 13:51:19 -04007831 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04007832 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00007833 WARN_ON(BTRFS_I(inode)->outstanding_extents);
7834 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04007835 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
7836 WARN_ON(BTRFS_I(inode)->csum_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04007837
Chris Mason5a3f23d2009-03-31 13:27:11 -04007838 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05007839 * This can happen where we create an inode, but somebody else also
7840 * created the same inode and we need to destroy the one we already
7841 * created.
7842 */
7843 if (!root)
7844 goto free;
7845
7846 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04007847 * Make sure we're properly removed from the ordered operation
7848 * lists.
7849 */
7850 smp_mb();
7851 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
Miao Xie199c2a92013-05-15 07:48:23 +00007852 spin_lock(&root->fs_info->ordered_root_lock);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007853 list_del_init(&BTRFS_I(inode)->ordered_operations);
Miao Xie199c2a92013-05-15 07:48:23 +00007854 spin_unlock(&root->fs_info->ordered_root_lock);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007855 }
7856
Josef Bacik8a35d952012-05-23 14:26:42 -04007857 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
7858 &BTRFS_I(inode)->runtime_flags)) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00007859 btrfs_info(root->fs_info, "inode %llu still on the orphan list",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02007860 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04007861 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04007862 }
Josef Bacik7b128762008-07-24 12:17:14 -04007863
Chris Masond3977122009-01-05 21:25:51 -05007864 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04007865 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
7866 if (!ordered)
7867 break;
7868 else {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00007869 btrfs_err(root->fs_info, "found ordered extent %llu %llu on inode cleanup",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02007870 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007871 btrfs_remove_ordered_extent(inode, ordered);
7872 btrfs_put_ordered_extent(ordered);
7873 btrfs_put_ordered_extent(ordered);
7874 }
7875 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007876 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007877 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05007878free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007879 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04007880}
7881
Al Viro45321ac2010-06-07 13:43:19 -04007882int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04007883{
7884 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04007885
Naohiro Aota6379ef92013-06-06 09:56:34 +00007886 if (root == NULL)
7887 return 1;
7888
Liu Bofa6ac872013-02-20 14:10:23 +00007889 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02007890 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04007891 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04007892 else
Al Viro45321ac2010-06-07 13:43:19 -04007893 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04007894}
7895
Sven Wegener0ee0fda2008-07-30 16:54:26 -04007896static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04007897{
7898 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
7899
7900 inode_init_once(&ei->vfs_inode);
7901}
7902
7903void btrfs_destroy_cachep(void)
7904{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10007905 /*
7906 * Make sure all delayed rcu free inodes are flushed before we
7907 * destroy cache.
7908 */
7909 rcu_barrier();
Chris Mason39279cc2007-06-12 06:35:45 -04007910 if (btrfs_inode_cachep)
7911 kmem_cache_destroy(btrfs_inode_cachep);
7912 if (btrfs_trans_handle_cachep)
7913 kmem_cache_destroy(btrfs_trans_handle_cachep);
7914 if (btrfs_transaction_cachep)
7915 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04007916 if (btrfs_path_cachep)
7917 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05007918 if (btrfs_free_space_cachep)
7919 kmem_cache_destroy(btrfs_free_space_cachep);
Miao Xie8ccf6f192012-10-25 09:28:04 +00007920 if (btrfs_delalloc_work_cachep)
7921 kmem_cache_destroy(btrfs_delalloc_work_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04007922}
7923
7924int btrfs_init_cachep(void)
7925{
David Sterba837e1972012-09-07 03:00:48 -06007926 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007927 sizeof(struct btrfs_inode), 0,
7928 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04007929 if (!btrfs_inode_cachep)
7930 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007931
David Sterba837e1972012-09-07 03:00:48 -06007932 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007933 sizeof(struct btrfs_trans_handle), 0,
7934 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007935 if (!btrfs_trans_handle_cachep)
7936 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007937
David Sterba837e1972012-09-07 03:00:48 -06007938 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007939 sizeof(struct btrfs_transaction), 0,
7940 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007941 if (!btrfs_transaction_cachep)
7942 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007943
David Sterba837e1972012-09-07 03:00:48 -06007944 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007945 sizeof(struct btrfs_path), 0,
7946 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007947 if (!btrfs_path_cachep)
7948 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007949
David Sterba837e1972012-09-07 03:00:48 -06007950 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05007951 sizeof(struct btrfs_free_space), 0,
7952 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
7953 if (!btrfs_free_space_cachep)
7954 goto fail;
7955
Miao Xie8ccf6f192012-10-25 09:28:04 +00007956 btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
7957 sizeof(struct btrfs_delalloc_work), 0,
7958 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
7959 NULL);
7960 if (!btrfs_delalloc_work_cachep)
7961 goto fail;
7962
Chris Mason39279cc2007-06-12 06:35:45 -04007963 return 0;
7964fail:
7965 btrfs_destroy_cachep();
7966 return -ENOMEM;
7967}
7968
7969static int btrfs_getattr(struct vfsmount *mnt,
7970 struct dentry *dentry, struct kstat *stat)
7971{
Miao Xiedf0af1a2013-01-29 10:11:59 +00007972 u64 delalloc_bytes;
Chris Mason39279cc2007-06-12 06:35:45 -04007973 struct inode *inode = dentry->d_inode;
David Sterbafadc0d82011-11-20 07:33:38 -05007974 u32 blocksize = inode->i_sb->s_blocksize;
7975
Chris Mason39279cc2007-06-12 06:35:45 -04007976 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04007977 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05007978 stat->blksize = PAGE_CACHE_SIZE;
Miao Xiedf0af1a2013-01-29 10:11:59 +00007979
7980 spin_lock(&BTRFS_I(inode)->lock);
7981 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
7982 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05007983 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00007984 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04007985 return 0;
7986}
7987
Chris Masond3977122009-01-05 21:25:51 -05007988static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7989 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04007990{
7991 struct btrfs_trans_handle *trans;
7992 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007993 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04007994 struct inode *new_inode = new_dentry->d_inode;
7995 struct inode *old_inode = old_dentry->d_inode;
7996 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007997 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007998 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04007999 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08008000 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008001
Li Zefan33345d012011-04-20 10:31:50 +08008002 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04008003 return -EPERM;
8004
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008005 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08008006 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05008007 return -EXDEV;
8008
Li Zefan33345d012011-04-20 10:31:50 +08008009 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
8010 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008011 return -ENOTEMPTY;
8012
Chris Mason39279cc2007-06-12 06:35:45 -04008013 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008014 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04008015 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05008016
8017
8018 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04008019 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05008020 new_dentry->d_name.name,
8021 new_dentry->d_name.len);
8022
8023 if (ret) {
8024 if (ret == -EEXIST) {
8025 /* we shouldn't get
8026 * eexist without a new_inode */
8027 if (!new_inode) {
8028 WARN_ON(1);
8029 return ret;
8030 }
8031 } else {
8032 /* maybe -EOVERFLOW */
8033 return ret;
8034 }
8035 }
8036 ret = 0;
8037
Chris Mason5a3f23d2009-03-31 13:27:11 -04008038 /*
8039 * we're using rename to replace one file with another.
8040 * and the replacement file is large. Start IO on it now so
8041 * we don't add too much work to the end of the transaction
8042 */
Bartlomiej Zolnierkiewicz4baf8c92009-08-07 13:47:08 -04008043 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
Chris Mason5a3f23d2009-03-31 13:27:11 -04008044 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
8045 filemap_flush(old_inode->i_mapping);
8046
Yan, Zheng76dda932009-09-21 16:00:26 -04008047 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08008048 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008049 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008050 /*
8051 * We want to reserve the absolute worst case amount of items. So if
8052 * both inodes are subvols and we need to unlink them then that would
8053 * require 4 item modifications, but if they are both normal inodes it
8054 * would require 5 item modifications, so we'll assume their normal
8055 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
8056 * should cover the worst case number of items we'll modify.
8057 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04008058 trans = btrfs_start_transaction(root, 11);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008059 if (IS_ERR(trans)) {
8060 ret = PTR_ERR(trans);
8061 goto out_notrans;
8062 }
Chris Mason5f39d392007-10-15 16:14:19 -04008063
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008064 if (dest != root)
8065 btrfs_record_root_in_trans(trans, dest);
8066
Yan, Zhenga5719522009-09-24 09:17:31 -04008067 ret = btrfs_set_inode_index(new_dir, &index);
8068 if (ret)
8069 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008070
Li Zefan33345d012011-04-20 10:31:50 +08008071 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008072 /* force full log commit if subvolume involved. */
8073 root->fs_info->last_trans_log_full_commit = trans->transid;
8074 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04008075 ret = btrfs_insert_inode_ref(trans, dest,
8076 new_dentry->d_name.name,
8077 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08008078 old_ino,
8079 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04008080 if (ret)
8081 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008082 /*
8083 * this is an ugly little race, but the rename is required
8084 * to make sure that if we crash, the inode is either at the
8085 * old name or the new one. pinning the log transaction lets
8086 * us make sure we don't allow a log commit to come in after
8087 * we unlink the name but before we add the new name back in.
8088 */
8089 btrfs_pin_log_trans(root);
8090 }
Chris Mason12fcfd22009-03-24 10:24:20 -04008091 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04008092 * make sure the inode gets flushed if it is replacing
8093 * something.
8094 */
Li Zefan33345d012011-04-20 10:31:50 +08008095 if (new_inode && new_inode->i_size && S_ISREG(old_inode->i_mode))
Chris Mason5a3f23d2009-03-31 13:27:11 -04008096 btrfs_add_ordered_operation(trans, root, old_inode);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008097
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008098 inode_inc_iversion(old_dir);
8099 inode_inc_iversion(new_dir);
8100 inode_inc_iversion(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008101 old_dir->i_ctime = old_dir->i_mtime = ctime;
8102 new_dir->i_ctime = new_dir->i_mtime = ctime;
8103 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04008104
Chris Mason12fcfd22009-03-24 10:24:20 -04008105 if (old_dentry->d_parent != new_dentry->d_parent)
8106 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
8107
Li Zefan33345d012011-04-20 10:31:50 +08008108 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008109 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
8110 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
8111 old_dentry->d_name.name,
8112 old_dentry->d_name.len);
8113 } else {
Al Viro92986792011-03-04 17:14:37 +00008114 ret = __btrfs_unlink_inode(trans, root, old_dir,
8115 old_dentry->d_inode,
8116 old_dentry->d_name.name,
8117 old_dentry->d_name.len);
8118 if (!ret)
8119 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008120 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008121 if (ret) {
8122 btrfs_abort_transaction(trans, root, ret);
8123 goto out_fail;
8124 }
Chris Mason39279cc2007-06-12 06:35:45 -04008125
8126 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008127 inode_inc_iversion(new_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008128 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08008129 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008130 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
8131 root_objectid = BTRFS_I(new_inode)->location.objectid;
8132 ret = btrfs_unlink_subvol(trans, dest, new_dir,
8133 root_objectid,
8134 new_dentry->d_name.name,
8135 new_dentry->d_name.len);
8136 BUG_ON(new_inode->i_nlink == 0);
8137 } else {
8138 ret = btrfs_unlink_inode(trans, dest, new_dir,
8139 new_dentry->d_inode,
8140 new_dentry->d_name.name,
8141 new_dentry->d_name.len);
8142 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04008143 if (!ret && new_inode->i_nlink == 0)
Chris Masone02119d2008-09-05 16:13:11 -04008144 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008145 if (ret) {
8146 btrfs_abort_transaction(trans, root, ret);
8147 goto out_fail;
8148 }
Chris Mason39279cc2007-06-12 06:35:45 -04008149 }
Josef Bacikaec74772008-07-24 12:12:38 -04008150
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008151 ret = btrfs_add_link(trans, new_dir, old_inode,
8152 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04008153 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008154 if (ret) {
8155 btrfs_abort_transaction(trans, root, ret);
8156 goto out_fail;
8157 }
Chris Mason39279cc2007-06-12 06:35:45 -04008158
Li Zefan33345d012011-04-20 10:31:50 +08008159 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04008160 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00008161 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008162 btrfs_end_log_trans(root);
8163 }
Chris Mason39279cc2007-06-12 06:35:45 -04008164out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008165 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008166out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08008167 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008168 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008169
Chris Mason39279cc2007-06-12 06:35:45 -04008170 return ret;
8171}
8172
Miao Xie8ccf6f192012-10-25 09:28:04 +00008173static void btrfs_run_delalloc_work(struct btrfs_work *work)
8174{
8175 struct btrfs_delalloc_work *delalloc_work;
8176
8177 delalloc_work = container_of(work, struct btrfs_delalloc_work,
8178 work);
8179 if (delalloc_work->wait)
8180 btrfs_wait_ordered_range(delalloc_work->inode, 0, (u64)-1);
8181 else
8182 filemap_flush(delalloc_work->inode->i_mapping);
8183
8184 if (delalloc_work->delay_iput)
8185 btrfs_add_delayed_iput(delalloc_work->inode);
8186 else
8187 iput(delalloc_work->inode);
8188 complete(&delalloc_work->completion);
8189}
8190
8191struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
8192 int wait, int delay_iput)
8193{
8194 struct btrfs_delalloc_work *work;
8195
8196 work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
8197 if (!work)
8198 return NULL;
8199
8200 init_completion(&work->completion);
8201 INIT_LIST_HEAD(&work->list);
8202 work->inode = inode;
8203 work->wait = wait;
8204 work->delay_iput = delay_iput;
8205 work->work.func = btrfs_run_delalloc_work;
8206
8207 return work;
8208}
8209
8210void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
8211{
8212 wait_for_completion(&work->completion);
8213 kmem_cache_free(btrfs_delalloc_work_cachep, work);
8214}
8215
Chris Masond352ac62008-09-29 15:18:18 -04008216/*
8217 * some fairly slow code that needs optimization. This walks the list
8218 * of all the inodes with pending delalloc and forces them to disk.
8219 */
Miao Xieeb73c1b2013-05-15 07:48:22 +00008220static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
Chris Masonea8c2812008-08-04 23:17:27 -04008221{
Chris Masonea8c2812008-08-04 23:17:27 -04008222 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008223 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008224 struct btrfs_delalloc_work *work, *next;
8225 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00008226 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008227 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04008228
Miao Xie8ccf6f192012-10-25 09:28:04 +00008229 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008230 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00008231
Miao Xieeb73c1b2013-05-15 07:48:22 +00008232 spin_lock(&root->delalloc_lock);
8233 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008234 while (!list_empty(&splice)) {
8235 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04008236 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008237
Miao Xieeb73c1b2013-05-15 07:48:22 +00008238 list_move_tail(&binode->delalloc_inodes,
8239 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008240 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00008241 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00008242 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008243 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008244 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008245 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008246
8247 work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
8248 if (unlikely(!work)) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -04008249 if (delay_iput)
8250 btrfs_add_delayed_iput(inode);
8251 else
8252 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008253 ret = -ENOMEM;
8254 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008255 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008256 list_add_tail(&work->list, &works);
8257 btrfs_queue_worker(&root->fs_info->flush_workers,
8258 &work->work);
8259
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008260 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00008261 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04008262 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008263 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04008264
Miao Xie1eafa6c2013-01-22 10:49:00 +00008265 list_for_each_entry_safe(work, next, &works, list) {
8266 list_del_init(&work->list);
8267 btrfs_wait_and_free_delalloc_work(work);
8268 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008269 return 0;
8270out:
8271 list_for_each_entry_safe(work, next, &works, list) {
8272 list_del_init(&work->list);
8273 btrfs_wait_and_free_delalloc_work(work);
8274 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008275
Miao Xieeb73c1b2013-05-15 07:48:22 +00008276 if (!list_empty_careful(&splice)) {
8277 spin_lock(&root->delalloc_lock);
8278 list_splice_tail(&splice, &root->delalloc_inodes);
8279 spin_unlock(&root->delalloc_lock);
8280 }
8281 return ret;
8282}
8283
8284int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
8285{
8286 int ret;
8287
8288 if (root->fs_info->sb->s_flags & MS_RDONLY)
8289 return -EROFS;
8290
8291 ret = __start_delalloc_inodes(root, delay_iput);
8292 /*
8293 * the filemap_flush will queue IO into the worker threads, but
Chris Mason8c8bee12008-09-29 11:19:10 -04008294 * we have to make sure the IO is actually started and that
8295 * ordered extents get created before we return
8296 */
8297 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05008298 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05008299 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04008300 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05008301 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
8302 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04008303 }
8304 atomic_dec(&root->fs_info->async_submit_draining);
Miao Xieeb73c1b2013-05-15 07:48:22 +00008305 return ret;
8306}
8307
8308int btrfs_start_all_delalloc_inodes(struct btrfs_fs_info *fs_info,
8309 int delay_iput)
8310{
8311 struct btrfs_root *root;
8312 struct list_head splice;
8313 int ret;
8314
8315 if (fs_info->sb->s_flags & MS_RDONLY)
8316 return -EROFS;
8317
8318 INIT_LIST_HEAD(&splice);
8319
8320 spin_lock(&fs_info->delalloc_root_lock);
8321 list_splice_init(&fs_info->delalloc_roots, &splice);
8322 while (!list_empty(&splice)) {
8323 root = list_first_entry(&splice, struct btrfs_root,
8324 delalloc_root);
8325 root = btrfs_grab_fs_root(root);
8326 BUG_ON(!root);
8327 list_move_tail(&root->delalloc_root,
8328 &fs_info->delalloc_roots);
8329 spin_unlock(&fs_info->delalloc_root_lock);
8330
8331 ret = __start_delalloc_inodes(root, delay_iput);
8332 btrfs_put_fs_root(root);
8333 if (ret)
8334 goto out;
8335
8336 spin_lock(&fs_info->delalloc_root_lock);
8337 }
8338 spin_unlock(&fs_info->delalloc_root_lock);
8339
8340 atomic_inc(&fs_info->async_submit_draining);
8341 while (atomic_read(&fs_info->nr_async_submits) ||
8342 atomic_read(&fs_info->async_delalloc_pages)) {
8343 wait_event(fs_info->async_submit_wait,
8344 (atomic_read(&fs_info->nr_async_submits) == 0 &&
8345 atomic_read(&fs_info->async_delalloc_pages) == 0));
8346 }
8347 atomic_dec(&fs_info->async_submit_draining);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008348 return 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008349out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00008350 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00008351 spin_lock(&fs_info->delalloc_root_lock);
8352 list_splice_tail(&splice, &fs_info->delalloc_roots);
8353 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008354 }
Miao Xie8ccf6f192012-10-25 09:28:04 +00008355 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04008356}
8357
Chris Mason39279cc2007-06-12 06:35:45 -04008358static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
8359 const char *symname)
8360{
8361 struct btrfs_trans_handle *trans;
8362 struct btrfs_root *root = BTRFS_I(dir)->root;
8363 struct btrfs_path *path;
8364 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05008365 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04008366 int err;
8367 int drop_inode = 0;
8368 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008369 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04008370 int name_len;
8371 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04008372 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04008373 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04008374 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04008375
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01008376 name_len = strlen(symname);
Chris Mason39279cc2007-06-12 06:35:45 -04008377 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
8378 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05008379
Josef Bacik9ed74f22009-09-11 16:12:44 -04008380 /*
8381 * 2 items for inode item and ref
8382 * 2 items for dir items
8383 * 1 item for xattr if selinux is on
8384 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04008385 trans = btrfs_start_transaction(root, 5);
8386 if (IS_ERR(trans))
8387 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05008388
Li Zefan581bb052011-04-20 10:06:11 +08008389 err = btrfs_find_free_ino(root, &objectid);
8390 if (err)
8391 goto out_unlock;
8392
Josef Bacikaec74772008-07-24 12:12:38 -04008393 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08008394 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04008395 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008396 if (IS_ERR(inode)) {
8397 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008398 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008399 }
Chris Mason39279cc2007-06-12 06:35:45 -04008400
Eric Paris2a7dba32011-02-01 11:05:39 -05008401 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04008402 if (err) {
8403 drop_inode = 1;
8404 goto out_unlock;
8405 }
8406
Casey Schauflerad19db72011-12-15 10:09:07 -05008407 /*
8408 * If the active LSM wants to access the inode during
8409 * d_instantiate it needs these. Smack checks to see
8410 * if the filesystem supports xattrs by looking at the
8411 * ops vector.
8412 */
8413 inode->i_fop = &btrfs_file_operations;
8414 inode->i_op = &btrfs_file_inode_operations;
8415
Josef Bacika1b075d2010-11-19 20:36:11 +00008416 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04008417 if (err)
8418 drop_inode = 1;
8419 else {
8420 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04008421 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05008422 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04008423 }
Chris Mason39279cc2007-06-12 06:35:45 -04008424 if (drop_inode)
8425 goto out_unlock;
8426
8427 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07008428 if (!path) {
8429 err = -ENOMEM;
8430 drop_inode = 1;
8431 goto out_unlock;
8432 }
Li Zefan33345d012011-04-20 10:31:50 +08008433 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008434 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008435 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
8436 datasize = btrfs_file_extent_calc_inline_size(name_len);
8437 err = btrfs_insert_empty_item(trans, root, path, &key,
8438 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04008439 if (err) {
8440 drop_inode = 1;
Julia Lawallb0839162011-05-14 07:10:51 +00008441 btrfs_free_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04008442 goto out_unlock;
8443 }
Chris Mason5f39d392007-10-15 16:14:19 -04008444 leaf = path->nodes[0];
8445 ei = btrfs_item_ptr(leaf, path->slots[0],
8446 struct btrfs_file_extent_item);
8447 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
8448 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04008449 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04008450 btrfs_set_file_extent_encryption(leaf, ei, 0);
8451 btrfs_set_file_extent_compression(leaf, ei, 0);
8452 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
8453 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
8454
Chris Mason39279cc2007-06-12 06:35:45 -04008455 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04008456 write_extent_buffer(leaf, symname, ptr, name_len);
8457 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04008458 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04008459
Chris Mason39279cc2007-06-12 06:35:45 -04008460 inode->i_op = &btrfs_symlink_inode_operations;
8461 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04008462 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04008463 inode_set_bytes(inode, name_len);
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01008464 btrfs_i_size_write(inode, name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -04008465 err = btrfs_update_inode(trans, root, inode);
8466 if (err)
8467 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04008468
8469out_unlock:
Al Viro08c422c2011-12-23 07:58:13 -05008470 if (!err)
8471 d_instantiate(dentry, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008472 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04008473 if (drop_inode) {
8474 inode_dec_link_count(inode);
8475 iput(inode);
8476 }
Liu Bob53d3f52012-11-14 14:34:34 +00008477 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04008478 return err;
8479}
Chris Mason16432982008-04-10 10:23:21 -04008480
Josef Bacik0af3d002010-06-21 14:48:16 -04008481static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
8482 u64 start, u64 num_bytes, u64 min_size,
8483 loff_t actual_len, u64 *alloc_hint,
8484 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04008485{
Josef Bacik5dc562c2012-08-17 13:14:17 -04008486 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
8487 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04008488 struct btrfs_root *root = BTRFS_I(inode)->root;
8489 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04008490 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00008491 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05008492 u64 cur_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04008493 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04008494 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04008495
Josef Bacik0af3d002010-06-21 14:48:16 -04008496 if (trans)
8497 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04008498 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008499 if (own_trans) {
8500 trans = btrfs_start_transaction(root, 3);
8501 if (IS_ERR(trans)) {
8502 ret = PTR_ERR(trans);
8503 break;
8504 }
Yan Zhengd899e052008-10-30 14:25:28 -04008505 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00008506
Chris Mason154ea282013-03-05 11:11:26 -05008507 cur_bytes = min(num_bytes, 256ULL * 1024 * 1024);
8508 cur_bytes = max(cur_bytes, min_size);
Josef Bacik00361582013-08-14 14:02:47 -04008509 ret = btrfs_reserve_extent(root, cur_bytes, min_size, 0,
8510 *alloc_hint, &ins, 1);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008511 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008512 if (own_trans)
8513 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008514 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00008515 }
8516
Yan Zhengd899e052008-10-30 14:25:28 -04008517 ret = insert_reserved_file_extent(trans, inode,
8518 cur_offset, ins.objectid,
8519 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00008520 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04008521 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008522 if (ret) {
Josef Bacik857cc2f2013-10-07 15:21:08 -04008523 btrfs_free_reserved_extent(root, ins.objectid,
8524 ins.offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008525 btrfs_abort_transaction(trans, root, ret);
8526 if (own_trans)
8527 btrfs_end_transaction(trans, root);
8528 break;
8529 }
Chris Masona1ed8352009-09-11 12:27:37 -04008530 btrfs_drop_extent_cache(inode, cur_offset,
8531 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008532
Josef Bacik5dc562c2012-08-17 13:14:17 -04008533 em = alloc_extent_map();
8534 if (!em) {
8535 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
8536 &BTRFS_I(inode)->runtime_flags);
8537 goto next;
8538 }
8539
8540 em->start = cur_offset;
8541 em->orig_start = cur_offset;
8542 em->len = ins.offset;
8543 em->block_start = ins.objectid;
8544 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05008545 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04008546 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008547 em->bdev = root->fs_info->fs_devices->latest_bdev;
8548 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
8549 em->generation = trans->transid;
8550
8551 while (1) {
8552 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04008553 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04008554 write_unlock(&em_tree->lock);
8555 if (ret != -EEXIST)
8556 break;
8557 btrfs_drop_extent_cache(inode, cur_offset,
8558 cur_offset + ins.offset - 1,
8559 0);
8560 }
8561 free_extent_map(em);
8562next:
Yan Zhengd899e052008-10-30 14:25:28 -04008563 num_bytes -= ins.offset;
8564 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04008565 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00008566
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008567 inode_inc_iversion(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04008568 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02008569 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04008570 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04008571 (actual_len > inode->i_size) &&
8572 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00008573 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00008574 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00008575 else
Josef Bacik55a61d12010-11-22 18:50:32 +00008576 i_size = cur_offset;
8577 i_size_write(inode, i_size);
8578 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008579 }
8580
Yan Zhengd899e052008-10-30 14:25:28 -04008581 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008582
8583 if (ret) {
8584 btrfs_abort_transaction(trans, root, ret);
8585 if (own_trans)
8586 btrfs_end_transaction(trans, root);
8587 break;
8588 }
Yan Zhengd899e052008-10-30 14:25:28 -04008589
Josef Bacik0af3d002010-06-21 14:48:16 -04008590 if (own_trans)
8591 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008592 }
Yan Zhengd899e052008-10-30 14:25:28 -04008593 return ret;
8594}
8595
Josef Bacik0af3d002010-06-21 14:48:16 -04008596int btrfs_prealloc_file_range(struct inode *inode, int mode,
8597 u64 start, u64 num_bytes, u64 min_size,
8598 loff_t actual_len, u64 *alloc_hint)
8599{
8600 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
8601 min_size, actual_len, alloc_hint,
8602 NULL);
8603}
8604
8605int btrfs_prealloc_file_range_trans(struct inode *inode,
8606 struct btrfs_trans_handle *trans, int mode,
8607 u64 start, u64 num_bytes, u64 min_size,
8608 loff_t actual_len, u64 *alloc_hint)
8609{
8610 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
8611 min_size, actual_len, alloc_hint, trans);
8612}
8613
Chris Masone6dcd2d2008-07-17 12:53:50 -04008614static int btrfs_set_page_dirty(struct page *page)
8615{
Chris Masone6dcd2d2008-07-17 12:53:50 -04008616 return __set_page_dirty_nobuffers(page);
8617}
8618
Al Viro10556cb2011-06-20 19:28:19 -04008619static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05008620{
Li Zefanb83cc962010-12-20 16:04:08 +08008621 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00008622 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08008623
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00008624 if (mask & MAY_WRITE &&
8625 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
8626 if (btrfs_root_readonly(root))
8627 return -EROFS;
8628 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
8629 return -EACCES;
8630 }
Al Viro2830ba72011-06-20 19:16:29 -04008631 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05008632}
Chris Mason39279cc2007-06-12 06:35:45 -04008633
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008634static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05008635 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04008636 .lookup = btrfs_lookup,
8637 .create = btrfs_create,
8638 .unlink = btrfs_unlink,
8639 .link = btrfs_link,
8640 .mkdir = btrfs_mkdir,
8641 .rmdir = btrfs_rmdir,
8642 .rename = btrfs_rename,
8643 .symlink = btrfs_symlink,
8644 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04008645 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008646 .setxattr = btrfs_setxattr,
8647 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05008648 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008649 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05008650 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008651 .get_acl = btrfs_get_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -07008652 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008653};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008654static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008655 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05008656 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008657 .get_acl = btrfs_get_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -07008658 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008659};
Yan, Zheng76dda932009-09-21 16:00:26 -04008660
Alexey Dobriyan828c0952009-10-01 15:43:56 -07008661static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008662 .llseek = generic_file_llseek,
8663 .read = generic_read_dir,
Al Viro9cdda8d2013-05-22 16:48:09 -04008664 .iterate = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04008665 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04008666#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04008667 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04008668#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04008669 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04008670 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04008671};
8672
Chris Masond1310b22008-01-24 16:13:08 -05008673static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04008674 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05008675 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04008676 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04008677 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008678 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04008679 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05008680 .set_bit_hook = btrfs_set_bit_hook,
8681 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04008682 .merge_extent_hook = btrfs_merge_extent_hook,
8683 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04008684};
8685
Chris Mason35054392009-01-21 13:11:13 -05008686/*
8687 * btrfs doesn't support the bmap operation because swapfiles
8688 * use bmap to make a mapping of extents in the file. They assume
8689 * these extents won't change over the life of the file and they
8690 * use the bmap result to do IO directly to the drive.
8691 *
8692 * the btrfs bmap call would return logical addresses that aren't
8693 * suitable for IO and they also will change frequently as COW
8694 * operations happen. So, swapfile + btrfs == corruption.
8695 *
8696 * For now we're avoiding this by dropping bmap.
8697 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008698static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008699 .readpage = btrfs_readpage,
8700 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04008701 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05008702 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04008703 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04008704 .invalidatepage = btrfs_invalidatepage,
8705 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008706 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02008707 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04008708};
8709
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008710static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008711 .readpage = btrfs_readpage,
8712 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04008713 .invalidatepage = btrfs_invalidatepage,
8714 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04008715};
8716
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008717static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008718 .getattr = btrfs_getattr,
8719 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008720 .setxattr = btrfs_setxattr,
8721 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05008722 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008723 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05008724 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008725 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008726 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008727 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008728};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008729static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04008730 .getattr = btrfs_getattr,
8731 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008732 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008733 .setxattr = btrfs_setxattr,
8734 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04008735 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008736 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008737 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008738 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -04008739};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008740static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008741 .readlink = generic_readlink,
8742 .follow_link = page_follow_link_light,
8743 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00008744 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -05008745 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008746 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05008747 .setxattr = btrfs_setxattr,
8748 .getxattr = btrfs_getxattr,
8749 .listxattr = btrfs_listxattr,
8750 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008751 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008752 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008753};
Yan, Zheng76dda932009-09-21 16:00:26 -04008754
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04008755const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04008756 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -04008757 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -04008758};