blob: de7085bc28f6b7ba14dff533e0412759154b786b [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 Mason4b4e25f2008-11-20 10:22:27 -050046#include "compat.h"
Chris Mason39279cc2007-06-12 06:35:45 -040047#include "ctree.h"
48#include "disk-io.h"
49#include "transaction.h"
50#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040051#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040052#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040053#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040054#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020055#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040056#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050057#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050058#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080059#include "inode-map.h"
Liu Bo38c227d2013-01-29 03:18:40 +000060#include "backref.h"
Josef Bacikf23b5a52013-06-19 10:16:26 -040061#include "hash.h"
Chris Mason39279cc2007-06-12 06:35:45 -040062
63struct btrfs_iget_args {
64 u64 ino;
65 struct btrfs_root *root;
66};
67
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070068static const struct inode_operations btrfs_dir_inode_operations;
69static const struct inode_operations btrfs_symlink_inode_operations;
70static const struct inode_operations btrfs_dir_ro_inode_operations;
71static const struct inode_operations btrfs_special_inode_operations;
72static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070073static const struct address_space_operations btrfs_aops;
74static const struct address_space_operations btrfs_symlink_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070075static const struct file_operations btrfs_dir_file_operations;
Chris Masond1310b22008-01-24 16:13:08 -050076static struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040077
78static struct kmem_cache *btrfs_inode_cachep;
Miao Xie8ccf6f192012-10-25 09:28:04 +000079static struct kmem_cache *btrfs_delalloc_work_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040080struct kmem_cache *btrfs_trans_handle_cachep;
81struct kmem_cache *btrfs_transaction_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040082struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050083struct kmem_cache *btrfs_free_space_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040084
85#define S_SHIFT 12
86static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
87 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
88 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
89 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
90 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
91 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
92 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
93 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
94};
95
Eric Sandeen3972f262013-01-12 02:57:22 +000096static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Josef Bacika41ad392011-01-31 15:30:16 -050097static int btrfs_truncate(struct inode *inode);
Josef Bacik5fd02042012-05-02 14:00:54 -040098static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Chris Mason771ed682008-11-06 22:02:51 -050099static noinline int cow_file_range(struct inode *inode,
100 struct page *locked_page,
101 u64 start, u64 end, int *page_started,
102 unsigned long *nr_written, int unlock);
Josef Bacik70c8a912012-10-11 16:54:30 -0400103static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
104 u64 len, u64 orig_start,
105 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -0400106 u64 orig_block_len, u64 ram_bytes,
107 int type);
Josef Bacik7b128762008-07-24 12:17:14 -0400108
Eric Sandeen48a3b632013-04-25 20:41:01 +0000109static int btrfs_dirty_inode(struct inode *inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400110
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000111static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500112 struct inode *inode, struct inode *dir,
113 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500114{
115 int err;
116
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000117 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500118 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500119 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500120 return err;
121}
122
Chris Masond352ac62008-09-29 15:18:18 -0400123/*
Chris Masonc8b97812008-10-29 14:49:59 -0400124 * this does all the hard work for inserting an inline extent into
125 * the btree. The caller should have done a btrfs_drop_extents so that
126 * no overlapping inline items exist in the btree
127 */
Chris Masond3977122009-01-05 21:25:51 -0500128static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400129 struct btrfs_root *root, struct inode *inode,
130 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000131 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400132 struct page **compressed_pages)
133{
134 struct btrfs_key key;
135 struct btrfs_path *path;
136 struct extent_buffer *leaf;
137 struct page *page = NULL;
138 char *kaddr;
139 unsigned long ptr;
140 struct btrfs_file_extent_item *ei;
141 int err = 0;
142 int ret;
143 size_t cur_size = size;
144 size_t datasize;
145 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400146
Li Zefanfe3f5662011-03-28 08:30:38 +0000147 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400148 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400149
Chris Masond3977122009-01-05 21:25:51 -0500150 path = btrfs_alloc_path();
151 if (!path)
Chris Masonc8b97812008-10-29 14:49:59 -0400152 return -ENOMEM;
153
Chris Masonb9473432009-03-13 11:00:37 -0400154 path->leave_spinning = 1;
Chris Masonc8b97812008-10-29 14:49:59 -0400155
Li Zefan33345d012011-04-20 10:31:50 +0800156 key.objectid = btrfs_ino(inode);
Chris Masonc8b97812008-10-29 14:49:59 -0400157 key.offset = start;
158 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
Chris Masonc8b97812008-10-29 14:49:59 -0400159 datasize = btrfs_file_extent_calc_inline_size(cur_size);
160
161 inode_add_bytes(inode, size);
162 ret = btrfs_insert_empty_item(trans, root, path, &key,
163 datasize);
Chris Masonc8b97812008-10-29 14:49:59 -0400164 if (ret) {
165 err = ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400166 goto fail;
167 }
168 leaf = path->nodes[0];
169 ei = btrfs_item_ptr(leaf, path->slots[0],
170 struct btrfs_file_extent_item);
171 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
172 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
173 btrfs_set_file_extent_encryption(leaf, ei, 0);
174 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
175 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
176 ptr = btrfs_file_extent_inline_start(ei);
177
Li Zefan261507a02010-12-17 14:21:50 +0800178 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400179 struct page *cpage;
180 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500181 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400182 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500183 cur_size = min_t(unsigned long, compressed_size,
Chris Masonc8b97812008-10-29 14:49:59 -0400184 PAGE_CACHE_SIZE);
185
Cong Wang7ac687d2011-11-25 23:14:28 +0800186 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400187 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800188 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400189
190 i++;
191 ptr += cur_size;
192 compressed_size -= cur_size;
193 }
194 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800195 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400196 } else {
197 page = find_get_page(inode->i_mapping,
198 start >> PAGE_CACHE_SHIFT);
199 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800200 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400201 offset = start & (PAGE_CACHE_SIZE - 1);
202 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800203 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400204 page_cache_release(page);
205 }
206 btrfs_mark_buffer_dirty(leaf);
207 btrfs_free_path(path);
208
Yan, Zhengc2167752009-11-12 09:34:21 +0000209 /*
210 * we're an inline extent, so nobody can
211 * extend the file past i_size without locking
212 * a page we already have locked.
213 *
214 * We must do any isize and inode updates
215 * before we unlock the pages. Otherwise we
216 * could end up racing with unlink.
217 */
Chris Masonc8b97812008-10-29 14:49:59 -0400218 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100219 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000220
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100221 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400222fail:
223 btrfs_free_path(path);
224 return err;
225}
226
227
228/*
229 * conditionally insert an inline extent into the file. This
230 * does the checks required to make sure the data is small enough
231 * to fit as an inline extent.
232 */
Chris Mason7f366cf2009-03-12 20:12:45 -0400233static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400234 struct btrfs_root *root,
235 struct inode *inode, u64 start, u64 end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000236 size_t compressed_size, int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400237 struct page **compressed_pages)
238{
239 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 Bacik26714852012-08-29 12:24:27 -0400259 ret = btrfs_drop_extents(trans, root, inode, start, aligned_end, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100260 if (ret)
261 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400262
263 if (isize > actual_end)
264 inline_len = min_t(u64, isize, actual_end);
265 ret = insert_inline_extent(trans, root, inode, start,
266 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000267 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400268 if (ret && ret != -ENOSPC) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100269 btrfs_abort_transaction(trans, root, ret);
270 return ret;
Josef Bacik2adcac12012-05-23 16:10:14 -0400271 } else if (ret == -ENOSPC) {
272 return 1;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100273 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400274
Josef Bacikbdc20e62013-02-28 13:23:38 -0500275 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400276 btrfs_delalloc_release_metadata(inode, end + 1 - start);
Chris Masona1ed8352009-09-11 12:27:37 -0400277 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
Chris Masonc8b97812008-10-29 14:49:59 -0400278 return 0;
279}
280
Chris Mason771ed682008-11-06 22:02:51 -0500281struct async_extent {
282 u64 start;
283 u64 ram_size;
284 u64 compressed_size;
285 struct page **pages;
286 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800287 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500288 struct list_head list;
289};
290
291struct async_cow {
292 struct inode *inode;
293 struct btrfs_root *root;
294 struct page *locked_page;
295 u64 start;
296 u64 end;
297 struct list_head extents;
298 struct btrfs_work work;
299};
300
301static noinline int add_async_extent(struct async_cow *cow,
302 u64 start, u64 ram_size,
303 u64 compressed_size,
304 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800305 unsigned long nr_pages,
306 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500307{
308 struct async_extent *async_extent;
309
310 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100311 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500312 async_extent->start = start;
313 async_extent->ram_size = ram_size;
314 async_extent->compressed_size = compressed_size;
315 async_extent->pages = pages;
316 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800317 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500318 list_add_tail(&async_extent->list, &cow->extents);
319 return 0;
320}
321
Chris Masonc8b97812008-10-29 14:49:59 -0400322/*
Chris Mason771ed682008-11-06 22:02:51 -0500323 * we create compressed extents in two phases. The first
324 * phase compresses a range of pages that have already been
325 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400326 *
Chris Mason771ed682008-11-06 22:02:51 -0500327 * This is done inside an ordered work queue, and the compression
328 * is spread across many cpus. The actual IO submission is step
329 * two, and the ordered work queue takes care of making sure that
330 * happens in the same order things were put onto the queue by
331 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400332 *
Chris Mason771ed682008-11-06 22:02:51 -0500333 * If this code finds it can't get good compression, it puts an
334 * entry onto the work queue to write the uncompressed bytes. This
335 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300336 * are written in the same order that the flusher thread sent them
337 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400338 */
Chris Mason771ed682008-11-06 22:02:51 -0500339static noinline int compress_file_range(struct inode *inode,
340 struct page *locked_page,
341 u64 start, u64 end,
342 struct async_cow *async_cow,
343 int *num_added)
Chris Masonb888db22007-08-27 16:49:44 -0400344{
345 struct btrfs_root *root = BTRFS_I(inode)->root;
346 struct btrfs_trans_handle *trans;
Chris Masondb945352007-10-15 16:15:53 -0400347 u64 num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400348 u64 blocksize = root->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400349 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500350 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400351 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400352 struct page **pages = NULL;
353 unsigned long nr_pages;
354 unsigned long nr_pages_ret = 0;
355 unsigned long total_compressed = 0;
356 unsigned long total_in = 0;
357 unsigned long max_compressed = 128 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -0500358 unsigned long max_uncompressed = 128 * 1024;
Chris Masonc8b97812008-10-29 14:49:59 -0400359 int i;
360 int will_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800361 int compress_type = root->fs_info->compress_type;
Chris Mason4adaa612013-03-26 13:07:00 -0400362 int redirty = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400363
Liu Bo4cb13e52012-03-29 09:57:45 -0400364 /* if this is a small write inside eof, kick off a defrag */
365 if ((end - start + 1) < 16 * 1024 &&
366 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400367 btrfs_add_inode_defrag(NULL, inode);
368
Chris Mason42dc7ba2008-12-15 11:44:56 -0500369 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400370again:
371 will_compress = 0;
372 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
373 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
374
Chris Masonf03d9301f2009-02-04 09:31:06 -0500375 /*
376 * we don't want to send crud past the end of i_size through
377 * compression, that's just a waste of CPU time. So, if the
378 * end of the file is before the start of our current
379 * requested range of bytes, we bail out to the uncompressed
380 * cleanup code that can deal with all of this.
381 *
382 * It isn't really the fastest way to fix things, but this is a
383 * very uncommon corner.
384 */
385 if (actual_end <= start)
386 goto cleanup_and_bail_uncompressed;
387
Chris Masonc8b97812008-10-29 14:49:59 -0400388 total_compressed = actual_end - start;
389
390 /* we want to make sure that amount of ram required to uncompress
391 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500392 * of a compressed extent to 128k. This is a crucial number
393 * because it also controls how easily we can spread reads across
394 * cpus for decompression.
395 *
396 * We also want to make sure the amount of IO required to do
397 * a random read is reasonably small, so we limit the size of
398 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400399 */
400 total_compressed = min(total_compressed, max_uncompressed);
Qu Wenruofda28322013-02-26 08:10:22 +0000401 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Masonbe20aa92007-12-17 20:14:01 -0500402 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400403 total_in = 0;
404 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400405
Chris Mason771ed682008-11-06 22:02:51 -0500406 /*
407 * we do compression for mount -o compress and when the
408 * inode has not been flagged as nocompress. This flag can
409 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400410 */
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200411 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) &&
Chris Mason1e701a32010-03-11 09:42:04 -0500412 (btrfs_test_opt(root, COMPRESS) ||
Liu Bo75e7cb72011-03-22 10:12:20 +0000413 (BTRFS_I(inode)->force_compress) ||
414 (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS))) {
Chris Masonc8b97812008-10-29 14:49:59 -0400415 WARN_ON(pages);
Chris Masoncfbc2462008-10-30 13:22:14 -0400416 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800417 if (!pages) {
418 /* just bail out to the uncompressed code */
419 goto cont;
420 }
Chris Mason179e29e2007-11-01 11:28:41 -0400421
Li Zefan261507a02010-12-17 14:21:50 +0800422 if (BTRFS_I(inode)->force_compress)
423 compress_type = BTRFS_I(inode)->force_compress;
424
Chris Mason4adaa612013-03-26 13:07:00 -0400425 /*
426 * we need to call clear_page_dirty_for_io on each
427 * page in the range. Otherwise applications with the file
428 * mmap'd can wander in and change the page contents while
429 * we are compressing them.
430 *
431 * If the compression fails for any reason, we set the pages
432 * dirty again later on.
433 */
434 extent_range_clear_dirty_for_io(inode, start, end);
435 redirty = 1;
Li Zefan261507a02010-12-17 14:21:50 +0800436 ret = btrfs_compress_pages(compress_type,
437 inode->i_mapping, start,
438 total_compressed, pages,
439 nr_pages, &nr_pages_ret,
440 &total_in,
441 &total_compressed,
442 max_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400443
444 if (!ret) {
445 unsigned long offset = total_compressed &
446 (PAGE_CACHE_SIZE - 1);
447 struct page *page = pages[nr_pages_ret - 1];
448 char *kaddr;
449
450 /* zero the tail end of the last page, we might be
451 * sending it down to disk
452 */
453 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800454 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400455 memset(kaddr + offset, 0,
456 PAGE_CACHE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800457 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400458 }
459 will_compress = 1;
460 }
461 }
Li Zefan560f7d72011-09-08 10:22:01 +0800462cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400463 if (start == 0) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -0400464 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100465 if (IS_ERR(trans)) {
466 ret = PTR_ERR(trans);
467 trans = NULL;
468 goto cleanup_and_out;
469 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400470 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Mason771ed682008-11-06 22:02:51 -0500471
Chris Masonc8b97812008-10-29 14:49:59 -0400472 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500473 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400474 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500475 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400476 */
477 ret = cow_file_range_inline(trans, root, inode,
Li Zefanfe3f5662011-03-28 08:30:38 +0000478 start, end, 0, 0, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400479 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500480 /* try making a compressed inline extent */
Chris Masonc8b97812008-10-29 14:49:59 -0400481 ret = cow_file_range_inline(trans, root, inode,
482 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);
Yan, Zhengc2167752009-11-12 09:34:21 +0000501 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400502 goto free_pages_out;
503 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000504 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400505 }
506
507 if (will_compress) {
508 /*
509 * we aren't doing an inline extent round the compressed size
510 * up to a block size boundary so the allocator does sane
511 * things
512 */
Qu Wenruofda28322013-02-26 08:10:22 +0000513 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400514
515 /*
516 * one last check to make sure the compression is really a
517 * win, compare the page count read with the blocks on disk
518 */
Qu Wenruofda28322013-02-26 08:10:22 +0000519 total_in = ALIGN(total_in, PAGE_CACHE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400520 if (total_compressed >= total_in) {
521 will_compress = 0;
522 } else {
Chris Masonc8b97812008-10-29 14:49:59 -0400523 num_bytes = total_in;
524 }
525 }
526 if (!will_compress && pages) {
527 /*
528 * the compression code ran but failed to make things smaller,
529 * free any pages it allocated and our page pointer array
530 */
531 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400532 WARN_ON(pages[i]->mapping);
Chris Masonc8b97812008-10-29 14:49:59 -0400533 page_cache_release(pages[i]);
534 }
535 kfree(pages);
536 pages = NULL;
537 total_compressed = 0;
538 nr_pages_ret = 0;
539
540 /* flag the file so we don't compress in the future */
Chris Mason1e701a32010-03-11 09:42:04 -0500541 if (!btrfs_test_opt(root, FORCE_COMPRESS) &&
542 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500543 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500544 }
Chris Masonc8b97812008-10-29 14:49:59 -0400545 }
Chris Mason771ed682008-11-06 22:02:51 -0500546 if (will_compress) {
547 *num_added += 1;
548
549 /* the async work queues will take care of doing actual
550 * allocation on disk for these compressed pages,
551 * and will submit them to the elevator.
552 */
553 add_async_extent(async_cow, start, num_bytes,
Li Zefan261507a02010-12-17 14:21:50 +0800554 total_compressed, pages, nr_pages_ret,
555 compress_type);
Chris Mason771ed682008-11-06 22:02:51 -0500556
Yan, Zheng24ae6362010-12-06 07:02:36 +0000557 if (start + num_bytes < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500558 start += num_bytes;
559 pages = NULL;
560 cond_resched();
561 goto again;
562 }
563 } else {
Chris Masonf03d9301f2009-02-04 09:31:06 -0500564cleanup_and_bail_uncompressed:
Chris Mason771ed682008-11-06 22:02:51 -0500565 /*
566 * No compression, but we still need to write the pages in
567 * the file we've been given so far. redirty the locked
568 * page if it corresponds to our extent and set things up
569 * for the async work queue to run cow_file_range to do
570 * the normal delalloc dance
571 */
572 if (page_offset(locked_page) >= start &&
573 page_offset(locked_page) <= end) {
574 __set_page_dirty_nobuffers(locked_page);
575 /* unlocked later on in the async handlers */
576 }
Chris Mason4adaa612013-03-26 13:07:00 -0400577 if (redirty)
578 extent_range_redirty_for_io(inode, start, end);
Li Zefan261507a02010-12-17 14:21:50 +0800579 add_async_extent(async_cow, start, end - start + 1,
580 0, NULL, 0, BTRFS_COMPRESS_NONE);
Chris Mason771ed682008-11-06 22:02:51 -0500581 *num_added += 1;
582 }
583
584out:
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100585 return ret;
Chris Mason771ed682008-11-06 22:02:51 -0500586
587free_pages_out:
588 for (i = 0; i < nr_pages_ret; i++) {
589 WARN_ON(pages[i]->mapping);
590 page_cache_release(pages[i]);
591 }
Chris Masond3977122009-01-05 21:25:51 -0500592 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500593
594 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100595
596cleanup_and_out:
Josef Bacikc2790a22013-07-29 11:20:47 -0400597 extent_clear_unlock_delalloc(inode, start, end, NULL,
Josef Bacik151a41b2013-07-29 13:22:24 -0400598 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
599 EXTENT_DEFRAG, PAGE_UNLOCK |
600 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
601 PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100602 if (!trans || IS_ERR(trans))
603 btrfs_error(root->fs_info, ret, "Failed to join transaction");
604 else
605 btrfs_abort_transaction(trans, root, ret);
606 goto free_pages_out;
Chris Mason771ed682008-11-06 22:02:51 -0500607}
608
609/*
610 * phase two of compressed writeback. This is the ordered portion
611 * of the code, which only gets called in the order the work was
612 * queued. We walk all the async extents created by compress_file_range
613 * and send them down to the disk.
614 */
615static noinline int submit_compressed_extents(struct inode *inode,
616 struct async_cow *async_cow)
617{
618 struct async_extent *async_extent;
619 u64 alloc_hint = 0;
620 struct btrfs_trans_handle *trans;
621 struct btrfs_key ins;
622 struct extent_map *em;
623 struct btrfs_root *root = BTRFS_I(inode)->root;
624 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
625 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500626 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500627
628 if (list_empty(&async_cow->extents))
629 return 0;
630
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500631again:
Chris Masond3977122009-01-05 21:25:51 -0500632 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500633 async_extent = list_entry(async_cow->extents.next,
634 struct async_extent, list);
635 list_del(&async_extent->list);
636
637 io_tree = &BTRFS_I(inode)->io_tree;
638
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500639retry:
Chris Mason771ed682008-11-06 22:02:51 -0500640 /* did the compression code fall back to uncompressed IO? */
641 if (!async_extent->pages) {
642 int page_started = 0;
643 unsigned long nr_written = 0;
644
645 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000646 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100647 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500648
649 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500650 ret = cow_file_range(inode, async_cow->locked_page,
651 async_extent->start,
652 async_extent->start +
653 async_extent->ram_size - 1,
654 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500655
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100656 /* JDM XXX */
657
Chris Mason771ed682008-11-06 22:02:51 -0500658 /*
659 * if page_started, cow_file_range inserted an
660 * inline extent and took care of all the unlocking
661 * and IO for us. Otherwise, we need to submit
662 * all those pages down to the drive.
663 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500664 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500665 extent_write_locked_range(io_tree,
666 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500667 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500668 async_extent->ram_size - 1,
669 btrfs_get_extent,
670 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500671 else if (ret)
672 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500673 kfree(async_extent);
674 cond_resched();
675 continue;
676 }
677
678 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100679 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500680
Josef Bacik7a7eaa42011-04-13 12:54:33 -0400681 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100682 if (IS_ERR(trans)) {
683 ret = PTR_ERR(trans);
684 } else {
685 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
686 ret = btrfs_reserve_extent(trans, root,
Chris Mason771ed682008-11-06 22:02:51 -0500687 async_extent->compressed_size,
688 async_extent->compressed_size,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500689 0, alloc_hint, &ins, 1);
Miao Xie962197b2012-09-11 23:27:35 -0600690 if (ret && ret != -ENOSPC)
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100691 btrfs_abort_transaction(trans, root, ret);
692 btrfs_end_transaction(trans, root);
693 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000694
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500695 if (ret) {
696 int i;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500697
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500698 for (i = 0; i < async_extent->nr_pages; i++) {
699 WARN_ON(async_extent->pages[i]->mapping);
700 page_cache_release(async_extent->pages[i]);
701 }
702 kfree(async_extent->pages);
703 async_extent->nr_pages = 0;
704 async_extent->pages = NULL;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500705
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400706 if (ret == -ENOSPC) {
707 unlock_extent(io_tree, async_extent->start,
708 async_extent->start +
709 async_extent->ram_size - 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100710 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400711 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500712 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500713 }
714
Yan, Zhengc2167752009-11-12 09:34:21 +0000715 /*
716 * here we're doing allocation and writeback of the
717 * compressed pages
718 */
719 btrfs_drop_extent_cache(inode, async_extent->start,
720 async_extent->start +
721 async_extent->ram_size - 1, 0);
722
David Sterba172ddd62011-04-21 00:48:27 +0200723 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000724 if (!em) {
725 ret = -ENOMEM;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500726 goto out_free_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000727 }
Chris Mason771ed682008-11-06 22:02:51 -0500728 em->start = async_extent->start;
729 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500730 em->orig_start = em->start;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400731 em->mod_start = em->start;
732 em->mod_len = em->len;
Chris Mason771ed682008-11-06 22:02:51 -0500733
734 em->block_start = ins.objectid;
735 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500736 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400737 em->ram_bytes = async_extent->ram_size;
Chris Mason771ed682008-11-06 22:02:51 -0500738 em->bdev = root->fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800739 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500740 set_bit(EXTENT_FLAG_PINNED, &em->flags);
741 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400742 em->generation = -1;
Chris Mason771ed682008-11-06 22:02:51 -0500743
Chris Masond3977122009-01-05 21:25:51 -0500744 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400745 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400746 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400747 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500748 if (ret != -EEXIST) {
749 free_extent_map(em);
750 break;
751 }
752 btrfs_drop_extent_cache(inode, async_extent->start,
753 async_extent->start +
754 async_extent->ram_size - 1, 0);
755 }
756
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500757 if (ret)
758 goto out_free_reserve;
759
Li Zefan261507a02010-12-17 14:21:50 +0800760 ret = btrfs_add_ordered_extent_compress(inode,
761 async_extent->start,
762 ins.objectid,
763 async_extent->ram_size,
764 ins.offset,
765 BTRFS_ORDERED_COMPRESSED,
766 async_extent->compress_type);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500767 if (ret)
768 goto out_free_reserve;
Chris Mason771ed682008-11-06 22:02:51 -0500769
Chris Mason771ed682008-11-06 22:02:51 -0500770 /*
771 * clear dirty, set writeback and unlock the pages.
772 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400773 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400774 async_extent->start +
775 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400776 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
777 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400778 PAGE_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500779 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500780 async_extent->start,
781 async_extent->ram_size,
782 ins.objectid,
783 ins.offset, async_extent->pages,
784 async_extent->nr_pages);
Chris Mason771ed682008-11-06 22:02:51 -0500785 alloc_hint = ins.objectid + ins.offset;
786 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500787 if (ret)
788 goto out;
Chris Mason771ed682008-11-06 22:02:51 -0500789 cond_resched();
790 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100791 ret = 0;
792out:
793 return ret;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500794out_free_reserve:
795 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100796out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400797 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500798 async_extent->start +
799 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400800 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400801 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
802 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
803 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100804 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500805 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500806}
807
Josef Bacik4b46fce2010-05-23 11:00:55 -0400808static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
809 u64 num_bytes)
810{
811 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
812 struct extent_map *em;
813 u64 alloc_hint = 0;
814
815 read_lock(&em_tree->lock);
816 em = search_extent_mapping(em_tree, start, num_bytes);
817 if (em) {
818 /*
819 * if block start isn't an actual block number then find the
820 * first block in this inode and use that as a hint. If that
821 * block is also bogus then just don't worry about it.
822 */
823 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
824 free_extent_map(em);
825 em = search_extent_mapping(em_tree, 0, 0);
826 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
827 alloc_hint = em->block_start;
828 if (em)
829 free_extent_map(em);
830 } else {
831 alloc_hint = em->block_start;
832 free_extent_map(em);
833 }
834 }
835 read_unlock(&em_tree->lock);
836
837 return alloc_hint;
838}
839
Chris Mason771ed682008-11-06 22:02:51 -0500840/*
841 * when extent_io.c finds a delayed allocation range in the file,
842 * the call backs end up in this code. The basic idea is to
843 * allocate extents on disk for the range, and create ordered data structs
844 * in ram to track those extents.
845 *
846 * locked_page is the page that writepage had locked already. We use
847 * it to make sure we don't do extra locks or unlocks.
848 *
849 * *page_started is set to one if we unlock locked_page and do everything
850 * required to start IO on it. It may be clean and already done with
851 * IO when we return.
852 */
Miao Xieb7d5b0a2012-11-01 07:32:18 +0000853static noinline int __cow_file_range(struct btrfs_trans_handle *trans,
854 struct inode *inode,
855 struct btrfs_root *root,
856 struct page *locked_page,
857 u64 start, u64 end, int *page_started,
858 unsigned long *nr_written,
859 int unlock)
Chris Mason771ed682008-11-06 22:02:51 -0500860{
Chris Mason771ed682008-11-06 22:02:51 -0500861 u64 alloc_hint = 0;
862 u64 num_bytes;
863 unsigned long ram_size;
864 u64 disk_num_bytes;
865 u64 cur_alloc_size;
866 u64 blocksize = root->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500867 struct btrfs_key ins;
868 struct extent_map *em;
869 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
870 int ret = 0;
871
Liu Bo83eea1f2012-07-10 05:28:39 -0600872 BUG_ON(btrfs_is_free_space_inode(inode));
Chris Mason771ed682008-11-06 22:02:51 -0500873
Qu Wenruofda28322013-02-26 08:10:22 +0000874 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500875 num_bytes = max(blocksize, num_bytes);
876 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500877
Chris Mason4cb53002011-05-24 15:35:30 -0400878 /* if this is a small write inside eof, kick off defrag */
Liu Bo4cb13e52012-03-29 09:57:45 -0400879 if (num_bytes < 64 * 1024 &&
880 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400881 btrfs_add_inode_defrag(trans, inode);
882
Chris Mason771ed682008-11-06 22:02:51 -0500883 if (start == 0) {
884 /* lets try to make an inline extent */
885 ret = cow_file_range_inline(trans, root, inode,
Li Zefanfe3f5662011-03-28 08:30:38 +0000886 start, end, 0, 0, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500887 if (ret == 0) {
Josef Bacikc2790a22013-07-29 11:20:47 -0400888 extent_clear_unlock_delalloc(inode, start, end, NULL,
889 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400890 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400891 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
892 PAGE_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000893
Chris Mason771ed682008-11-06 22:02:51 -0500894 *nr_written = *nr_written +
895 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
896 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500897 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100898 } else if (ret < 0) {
899 btrfs_abort_transaction(trans, root, ret);
900 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500901 }
902 }
Chris Masonc8b97812008-10-29 14:49:59 -0400903
904 BUG_ON(disk_num_bytes >
David Sterba6c417612011-04-13 15:41:04 +0200905 btrfs_super_total_bytes(root->fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400906
Josef Bacik4b46fce2010-05-23 11:00:55 -0400907 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400908 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400909
Chris Masond3977122009-01-05 21:25:51 -0500910 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400911 unsigned long op;
912
Josef Bacik287a0ab2010-03-19 18:07:23 +0000913 cur_alloc_size = disk_num_bytes;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400914 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500915 root->sectorsize, 0, alloc_hint,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500916 &ins, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100917 if (ret < 0) {
918 btrfs_abort_transaction(trans, root, ret);
919 goto out_unlock;
920 }
Chris Masond3977122009-01-05 21:25:51 -0500921
David Sterba172ddd62011-04-21 00:48:27 +0200922 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000923 if (!em) {
924 ret = -ENOMEM;
Liu Boace68ba2013-04-22 10:53:47 +0000925 goto out_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000926 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400927 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500928 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500929 ram_size = ins.offset;
930 em->len = ins.offset;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400931 em->mod_start = em->start;
932 em->mod_len = em->len;
Chris Masonc8b97812008-10-29 14:49:59 -0400933
Chris Masone6dcd2d2008-07-17 12:53:50 -0400934 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400935 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500936 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400937 em->ram_bytes = ram_size;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400938 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400939 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400940 em->generation = -1;
Chris Masonc8b97812008-10-29 14:49:59 -0400941
Chris Masond3977122009-01-05 21:25:51 -0500942 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400943 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400944 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400945 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400946 if (ret != -EEXIST) {
947 free_extent_map(em);
948 break;
949 }
950 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400951 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400952 }
Liu Boace68ba2013-04-22 10:53:47 +0000953 if (ret)
954 goto out_reserve;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400955
Chris Mason98d20f62008-04-14 09:46:10 -0400956 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400957 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -0500958 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +0000959 if (ret)
960 goto out_reserve;
Chris Masonc8b97812008-10-29 14:49:59 -0400961
Yan Zheng17d217f2008-12-12 10:03:38 -0500962 if (root->root_key.objectid ==
963 BTRFS_DATA_RELOC_TREE_OBJECTID) {
964 ret = btrfs_reloc_clone_csums(inode, start,
965 cur_alloc_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100966 if (ret) {
967 btrfs_abort_transaction(trans, root, ret);
Liu Boace68ba2013-04-22 10:53:47 +0000968 goto out_reserve;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100969 }
Yan Zheng17d217f2008-12-12 10:03:38 -0500970 }
971
Chris Masond3977122009-01-05 21:25:51 -0500972 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -0400973 break;
Chris Masond3977122009-01-05 21:25:51 -0500974
Chris Masonc8b97812008-10-29 14:49:59 -0400975 /* we're not doing compressed IO, don't unlock the first
976 * page (which the caller expects to stay locked), don't
977 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -0400978 *
979 * Do set the Private2 bit so we know this page was properly
980 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -0400981 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400982 op = unlock ? PAGE_UNLOCK : 0;
983 op |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -0400984
Josef Bacikc2790a22013-07-29 11:20:47 -0400985 extent_clear_unlock_delalloc(inode, start,
986 start + ram_size - 1, locked_page,
987 EXTENT_LOCKED | EXTENT_DELALLOC,
988 op);
Chris Masonc8b97812008-10-29 14:49:59 -0400989 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -0500990 num_bytes -= cur_alloc_size;
991 alloc_hint = ins.objectid + ins.offset;
992 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400993 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100994out:
Chris Masonbe20aa92007-12-17 20:14:01 -0500995 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +0000996
Liu Boace68ba2013-04-22 10:53:47 +0000997out_reserve:
998 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100999out_unlock:
Josef Bacikc2790a22013-07-29 11:20:47 -04001000 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Josef Bacik151a41b2013-07-29 13:22:24 -04001001 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
1002 EXTENT_DELALLOC | EXTENT_DEFRAG,
1003 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
1004 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001005 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001006}
Chris Masonc8b97812008-10-29 14:49:59 -04001007
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001008static noinline int cow_file_range(struct inode *inode,
1009 struct page *locked_page,
1010 u64 start, u64 end, int *page_started,
1011 unsigned long *nr_written,
1012 int unlock)
1013{
1014 struct btrfs_trans_handle *trans;
1015 struct btrfs_root *root = BTRFS_I(inode)->root;
1016 int ret;
1017
1018 trans = btrfs_join_transaction(root);
1019 if (IS_ERR(trans)) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001020 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1021 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001022 EXTENT_DO_ACCOUNTING |
1023 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001024 PAGE_CLEAR_DIRTY |
1025 PAGE_SET_WRITEBACK |
1026 PAGE_END_WRITEBACK);
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001027 return PTR_ERR(trans);
1028 }
1029 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
1030
1031 ret = __cow_file_range(trans, inode, root, locked_page, start, end,
1032 page_started, nr_written, unlock);
1033
1034 btrfs_end_transaction(trans, root);
1035
1036 return ret;
1037}
1038
Chris Mason771ed682008-11-06 22:02:51 -05001039/*
1040 * work queue call back to started compression on a file and pages
1041 */
1042static noinline void async_cow_start(struct btrfs_work *work)
1043{
1044 struct async_cow *async_cow;
1045 int num_added = 0;
1046 async_cow = container_of(work, struct async_cow, work);
1047
1048 compress_file_range(async_cow->inode, async_cow->locked_page,
1049 async_cow->start, async_cow->end, async_cow,
1050 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001051 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001052 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001053 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001054 }
Chris Mason771ed682008-11-06 22:02:51 -05001055}
1056
1057/*
1058 * work queue call back to submit previously compressed pages
1059 */
1060static noinline void async_cow_submit(struct btrfs_work *work)
1061{
1062 struct async_cow *async_cow;
1063 struct btrfs_root *root;
1064 unsigned long nr_pages;
1065
1066 async_cow = container_of(work, struct async_cow, work);
1067
1068 root = async_cow->root;
1069 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
1070 PAGE_CACHE_SHIFT;
1071
Josef Bacik66657b32012-08-01 15:36:24 -04001072 if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) <
Liu Bo287082b2012-06-28 04:02:24 -06001073 5 * 1024 * 1024 &&
Chris Mason771ed682008-11-06 22:02:51 -05001074 waitqueue_active(&root->fs_info->async_submit_wait))
1075 wake_up(&root->fs_info->async_submit_wait);
1076
Chris Masond3977122009-01-05 21:25:51 -05001077 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001078 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001079}
Chris Masonc8b97812008-10-29 14:49:59 -04001080
Chris Mason771ed682008-11-06 22:02:51 -05001081static noinline void async_cow_free(struct btrfs_work *work)
1082{
1083 struct async_cow *async_cow;
1084 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001085 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001086 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001087 kfree(async_cow);
1088}
1089
1090static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1091 u64 start, u64 end, int *page_started,
1092 unsigned long *nr_written)
1093{
1094 struct async_cow *async_cow;
1095 struct btrfs_root *root = BTRFS_I(inode)->root;
1096 unsigned long nr_pages;
1097 u64 cur_end;
Liu Bo287082b2012-06-28 04:02:24 -06001098 int limit = 10 * 1024 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -05001099
Chris Masona3429ab2009-10-08 12:30:20 -04001100 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1101 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001102 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001103 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001104 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001105 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001106 async_cow->root = root;
1107 async_cow->locked_page = locked_page;
1108 async_cow->start = start;
1109
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001110 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
Chris Mason771ed682008-11-06 22:02:51 -05001111 cur_end = end;
1112 else
1113 cur_end = min(end, start + 512 * 1024 - 1);
1114
1115 async_cow->end = cur_end;
1116 INIT_LIST_HEAD(&async_cow->extents);
1117
1118 async_cow->work.func = async_cow_start;
1119 async_cow->work.ordered_func = async_cow_submit;
1120 async_cow->work.ordered_free = async_cow_free;
1121 async_cow->work.flags = 0;
1122
Chris Mason771ed682008-11-06 22:02:51 -05001123 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
1124 PAGE_CACHE_SHIFT;
1125 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
1126
1127 btrfs_queue_worker(&root->fs_info->delalloc_workers,
1128 &async_cow->work);
1129
1130 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
1131 wait_event(root->fs_info->async_submit_wait,
1132 (atomic_read(&root->fs_info->async_delalloc_pages) <
1133 limit));
1134 }
1135
Chris Masond3977122009-01-05 21:25:51 -05001136 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -05001137 atomic_read(&root->fs_info->async_delalloc_pages)) {
1138 wait_event(root->fs_info->async_submit_wait,
1139 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1140 0));
1141 }
1142
1143 *nr_written += nr_pages;
1144 start = cur_end + 1;
1145 }
1146 *page_started = 1;
1147 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001148}
1149
Chris Masond3977122009-01-05 21:25:51 -05001150static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001151 u64 bytenr, u64 num_bytes)
1152{
1153 int ret;
1154 struct btrfs_ordered_sum *sums;
1155 LIST_HEAD(list);
1156
Yan Zheng07d400a2009-01-06 11:42:00 -05001157 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001158 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001159 if (ret == 0 && list_empty(&list))
1160 return 0;
1161
1162 while (!list_empty(&list)) {
1163 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1164 list_del(&sums->list);
1165 kfree(sums);
1166 }
1167 return 1;
1168}
1169
Chris Masond352ac62008-09-29 15:18:18 -04001170/*
1171 * when nowcow writeback call back. This checks for snapshots or COW copies
1172 * of the extents that exist in the file, and COWs the file as required.
1173 *
1174 * If no cow copies or snapshots exist, we write directly to the existing
1175 * blocks on disk
1176 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001177static noinline int run_delalloc_nocow(struct inode *inode,
1178 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001179 u64 start, u64 end, int *page_started, int force,
1180 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001181{
Chris Masonbe20aa92007-12-17 20:14:01 -05001182 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001183 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001184 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001185 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001186 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001187 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001188 u64 cow_start;
1189 u64 cur_offset;
1190 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001191 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001192 u64 disk_bytenr;
1193 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001194 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001195 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001196 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001197 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001198 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001199 int nocow;
1200 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001201 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001202 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001203
1204 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001205 if (!path) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001206 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1207 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001208 EXTENT_DO_ACCOUNTING |
1209 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001210 PAGE_CLEAR_DIRTY |
1211 PAGE_SET_WRITEBACK |
1212 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001213 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001214 }
Li Zefan82d59022011-04-20 10:33:24 +08001215
Liu Bo83eea1f2012-07-10 05:28:39 -06001216 nolock = btrfs_is_free_space_inode(inode);
Li Zefan82d59022011-04-20 10:33:24 +08001217
1218 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001219 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001220 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001221 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001222
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001223 if (IS_ERR(trans)) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001224 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1225 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001226 EXTENT_DO_ACCOUNTING |
1227 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001228 PAGE_CLEAR_DIRTY |
1229 PAGE_SET_WRITEBACK |
1230 PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001231 btrfs_free_path(path);
1232 return PTR_ERR(trans);
1233 }
1234
Josef Bacik74b21072011-04-13 12:02:53 -04001235 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001236
Yan Zheng80ff3852008-10-30 14:20:02 -04001237 cow_start = (u64)-1;
1238 cur_offset = start;
1239 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001240 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001241 cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001242 if (ret < 0) {
1243 btrfs_abort_transaction(trans, root, ret);
1244 goto error;
1245 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001246 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1247 leaf = path->nodes[0];
1248 btrfs_item_key_to_cpu(leaf, &found_key,
1249 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001250 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001251 found_key.type == BTRFS_EXTENT_DATA_KEY)
1252 path->slots[0]--;
1253 }
1254 check_prev = 0;
1255next_slot:
1256 leaf = path->nodes[0];
1257 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1258 ret = btrfs_next_leaf(root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001259 if (ret < 0) {
1260 btrfs_abort_transaction(trans, root, ret);
1261 goto error;
1262 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001263 if (ret > 0)
1264 break;
1265 leaf = path->nodes[0];
1266 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001267
Yan Zheng80ff3852008-10-30 14:20:02 -04001268 nocow = 0;
1269 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001270 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001271 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001272
Li Zefan33345d012011-04-20 10:31:50 +08001273 if (found_key.objectid > ino ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001274 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1275 found_key.offset > end)
1276 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001277
Yan Zheng80ff3852008-10-30 14:20:02 -04001278 if (found_key.offset > cur_offset) {
1279 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001280 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001281 goto out_check;
1282 }
Chris Masonc31f8832008-01-08 15:46:31 -05001283
Yan Zheng80ff3852008-10-30 14:20:02 -04001284 fi = btrfs_item_ptr(leaf, path->slots[0],
1285 struct btrfs_file_extent_item);
1286 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001287
Josef Bacikcc95bef2013-04-04 14:31:27 -04001288 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001289 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1290 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001291 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001292 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001293 extent_end = found_key.offset +
1294 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001295 disk_num_bytes =
1296 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001297 if (extent_end <= start) {
1298 path->slots[0]++;
1299 goto next_slot;
1300 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001301 if (disk_bytenr == 0)
1302 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001303 if (btrfs_file_extent_compression(leaf, fi) ||
1304 btrfs_file_extent_encryption(leaf, fi) ||
1305 btrfs_file_extent_other_encoding(leaf, fi))
1306 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001307 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1308 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001309 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001310 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001311 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001312 found_key.offset -
1313 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001314 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001315 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001316 disk_bytenr += cur_offset - found_key.offset;
1317 num_bytes = min(end + 1, extent_end) - cur_offset;
1318 /*
1319 * force cow if csum exists in the range.
1320 * this ensure that csum for a given extent are
1321 * either valid or do not exist.
1322 */
1323 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1324 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001325 nocow = 1;
1326 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1327 extent_end = found_key.offset +
1328 btrfs_file_extent_inline_len(leaf, fi);
1329 extent_end = ALIGN(extent_end, root->sectorsize);
1330 } else {
1331 BUG_ON(1);
1332 }
1333out_check:
1334 if (extent_end <= start) {
1335 path->slots[0]++;
1336 goto next_slot;
1337 }
1338 if (!nocow) {
1339 if (cow_start == (u64)-1)
1340 cow_start = cur_offset;
1341 cur_offset = extent_end;
1342 if (cur_offset > end)
1343 break;
1344 path->slots[0]++;
1345 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001346 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001347
David Sterbab3b4aa72011-04-21 01:20:15 +02001348 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001349 if (cow_start != (u64)-1) {
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001350 ret = __cow_file_range(trans, inode, root, locked_page,
1351 cow_start, found_key.offset - 1,
1352 page_started, nr_written, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001353 if (ret) {
1354 btrfs_abort_transaction(trans, root, ret);
1355 goto error;
1356 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001357 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001358 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001359
Yan Zhengd899e052008-10-30 14:25:28 -04001360 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1361 struct extent_map *em;
1362 struct extent_map_tree *em_tree;
1363 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001364 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001365 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001366 em->start = cur_offset;
Josef Bacik70c8a912012-10-11 16:54:30 -04001367 em->orig_start = found_key.offset - extent_offset;
Yan Zhengd899e052008-10-30 14:25:28 -04001368 em->len = num_bytes;
1369 em->block_len = num_bytes;
1370 em->block_start = disk_bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05001371 em->orig_block_len = disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001372 em->ram_bytes = ram_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04001373 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001374 em->mod_start = em->start;
1375 em->mod_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04001376 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacikb11e2342012-12-03 10:58:15 -05001377 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001378 em->generation = -1;
Yan Zhengd899e052008-10-30 14:25:28 -04001379 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001380 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001381 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001382 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001383 if (ret != -EEXIST) {
1384 free_extent_map(em);
1385 break;
1386 }
1387 btrfs_drop_extent_cache(inode, em->start,
1388 em->start + em->len - 1, 0);
1389 }
1390 type = BTRFS_ORDERED_PREALLOC;
1391 } else {
1392 type = BTRFS_ORDERED_NOCOW;
1393 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001394
1395 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001396 num_bytes, num_bytes, type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001397 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001398
Yan, Zhengefa56462010-05-16 10:49:59 -04001399 if (root->root_key.objectid ==
1400 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1401 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1402 num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001403 if (ret) {
1404 btrfs_abort_transaction(trans, root, ret);
1405 goto error;
1406 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001407 }
1408
Josef Bacikc2790a22013-07-29 11:20:47 -04001409 extent_clear_unlock_delalloc(inode, cur_offset,
1410 cur_offset + num_bytes - 1,
1411 locked_page, EXTENT_LOCKED |
1412 EXTENT_DELALLOC, PAGE_UNLOCK |
1413 PAGE_SET_PRIVATE2);
Yan Zheng80ff3852008-10-30 14:20:02 -04001414 cur_offset = extent_end;
1415 if (cur_offset > end)
1416 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001417 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001418 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001419
Josef Bacik17ca04a2012-05-31 15:58:55 -04001420 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001421 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001422 cur_offset = end;
1423 }
1424
Yan Zheng80ff3852008-10-30 14:20:02 -04001425 if (cow_start != (u64)-1) {
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001426 ret = __cow_file_range(trans, inode, root, locked_page,
1427 cow_start, end,
1428 page_started, nr_written, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001429 if (ret) {
1430 btrfs_abort_transaction(trans, root, ret);
1431 goto error;
1432 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001433 }
1434
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001435error:
Miao Xiea698d0752012-09-20 01:51:59 -06001436 err = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001437 if (!ret)
1438 ret = err;
1439
Josef Bacik17ca04a2012-05-31 15:58:55 -04001440 if (ret && cur_offset < end)
Josef Bacikc2790a22013-07-29 11:20:47 -04001441 extent_clear_unlock_delalloc(inode, cur_offset, end,
1442 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001443 EXTENT_DELALLOC | EXTENT_DEFRAG |
1444 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1445 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001446 PAGE_SET_WRITEBACK |
1447 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001448 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001449 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001450}
1451
Chris Masond352ac62008-09-29 15:18:18 -04001452/*
1453 * extent_io.c call back to do delayed allocation processing
1454 */
Chris Masonc8b97812008-10-29 14:49:59 -04001455static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001456 u64 start, u64 end, int *page_started,
1457 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001458{
Chris Masonbe20aa92007-12-17 20:14:01 -05001459 int ret;
Chris Mason7f366cf2009-03-12 20:12:45 -04001460 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona2135012008-06-25 16:01:30 -04001461
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001462 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) {
Chris Masonc8b97812008-10-29 14:49:59 -04001463 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001464 page_started, 1, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001465 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001466 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001467 page_started, 0, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001468 } else if (!btrfs_test_opt(root, COMPRESS) &&
1469 !(BTRFS_I(inode)->force_compress) &&
1470 !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS)) {
Chris Mason7f366cf2009-03-12 20:12:45 -04001471 ret = cow_file_range(inode, locked_page, start, end,
1472 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001473 } else {
1474 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1475 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001476 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001477 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001478 }
Chris Masonb888db22007-08-27 16:49:44 -04001479 return ret;
1480}
1481
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001482static void btrfs_split_extent_hook(struct inode *inode,
1483 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001484{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001485 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001486 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001487 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001488
Josef Bacik9e0baf62011-07-15 15:16:44 +00001489 spin_lock(&BTRFS_I(inode)->lock);
1490 BTRFS_I(inode)->outstanding_extents++;
1491 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001492}
1493
1494/*
1495 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1496 * extents so we can keep track of new extents that are just merged onto old
1497 * extents, such as when we are doing sequential writes, so we can properly
1498 * account for the metadata space we'll need.
1499 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001500static void btrfs_merge_extent_hook(struct inode *inode,
1501 struct extent_state *new,
1502 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001503{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001504 /* not delalloc, ignore it */
1505 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001506 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001507
Josef Bacik9e0baf62011-07-15 15:16:44 +00001508 spin_lock(&BTRFS_I(inode)->lock);
1509 BTRFS_I(inode)->outstanding_extents--;
1510 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001511}
1512
Miao Xieeb73c1b2013-05-15 07:48:22 +00001513static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1514 struct inode *inode)
1515{
1516 spin_lock(&root->delalloc_lock);
1517 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1518 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1519 &root->delalloc_inodes);
1520 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1521 &BTRFS_I(inode)->runtime_flags);
1522 root->nr_delalloc_inodes++;
1523 if (root->nr_delalloc_inodes == 1) {
1524 spin_lock(&root->fs_info->delalloc_root_lock);
1525 BUG_ON(!list_empty(&root->delalloc_root));
1526 list_add_tail(&root->delalloc_root,
1527 &root->fs_info->delalloc_roots);
1528 spin_unlock(&root->fs_info->delalloc_root_lock);
1529 }
1530 }
1531 spin_unlock(&root->delalloc_lock);
1532}
1533
1534static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1535 struct inode *inode)
1536{
1537 spin_lock(&root->delalloc_lock);
1538 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1539 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1540 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1541 &BTRFS_I(inode)->runtime_flags);
1542 root->nr_delalloc_inodes--;
1543 if (!root->nr_delalloc_inodes) {
1544 spin_lock(&root->fs_info->delalloc_root_lock);
1545 BUG_ON(list_empty(&root->delalloc_root));
1546 list_del_init(&root->delalloc_root);
1547 spin_unlock(&root->fs_info->delalloc_root_lock);
1548 }
1549 }
1550 spin_unlock(&root->delalloc_lock);
1551}
1552
Chris Masond352ac62008-09-29 15:18:18 -04001553/*
1554 * extent_io.c set_bit_hook, used to track delayed allocation
1555 * bytes in this file, and to maintain the list of inodes that
1556 * have pending delalloc work to be done.
1557 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001558static void btrfs_set_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001559 struct extent_state *state, unsigned long *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001560{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001561
Chris Mason75eff682008-12-15 15:54:40 -05001562 /*
1563 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001564 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001565 * bit, which is only set or cleared with irqs on
1566 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001567 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001568 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001569 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001570 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001571
Josef Bacik9e0baf62011-07-15 15:16:44 +00001572 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001573 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001574 } else {
1575 spin_lock(&BTRFS_I(inode)->lock);
1576 BTRFS_I(inode)->outstanding_extents++;
1577 spin_unlock(&BTRFS_I(inode)->lock);
1578 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001579
Miao Xie963d6782013-01-29 10:10:51 +00001580 __percpu_counter_add(&root->fs_info->delalloc_bytes, len,
1581 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001582 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001583 BTRFS_I(inode)->delalloc_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001584 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001585 &BTRFS_I(inode)->runtime_flags))
1586 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001587 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001588 }
Chris Mason291d6732008-01-29 15:55:23 -05001589}
1590
Chris Masond352ac62008-09-29 15:18:18 -04001591/*
1592 * extent_io.c clear_bit_hook, see set_bit_hook for why
1593 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001594static void btrfs_clear_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001595 struct extent_state *state,
1596 unsigned long *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001597{
Chris Mason75eff682008-12-15 15:54:40 -05001598 /*
1599 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001600 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001601 * bit, which is only set or cleared with irqs on
1602 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001603 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001604 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001605 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001606 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001607
Josef Bacik9e0baf62011-07-15 15:16:44 +00001608 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001609 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001610 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1611 spin_lock(&BTRFS_I(inode)->lock);
1612 BTRFS_I(inode)->outstanding_extents--;
1613 spin_unlock(&BTRFS_I(inode)->lock);
1614 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001615
1616 if (*bits & EXTENT_DO_ACCOUNTING)
1617 btrfs_delalloc_release_metadata(inode, len);
1618
Josef Bacik0cb59c92010-07-02 12:14:14 -04001619 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Josef Bacik7ee9e442013-06-21 16:37:03 -04001620 && do_list && !(state->state & EXTENT_NORESERVE))
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001621 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001622
Miao Xie963d6782013-01-29 10:10:51 +00001623 __percpu_counter_add(&root->fs_info->delalloc_bytes, -len,
1624 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001625 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001626 BTRFS_I(inode)->delalloc_bytes -= len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001627 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001628 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001629 &BTRFS_I(inode)->runtime_flags))
1630 btrfs_del_delalloc_inode(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001631 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001632 }
Chris Mason291d6732008-01-29 15:55:23 -05001633}
1634
Chris Masond352ac62008-09-29 15:18:18 -04001635/*
1636 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1637 * we don't create bios that span stripes or chunks
1638 */
David Woodhouse64a16702009-07-15 23:29:37 +01001639int btrfs_merge_bio_hook(int rw, struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001640 size_t size, struct bio *bio,
1641 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001642{
1643 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Chris Masona62b9402008-10-03 16:31:08 -04001644 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001645 u64 length = 0;
1646 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001647 int ret;
1648
Chris Mason771ed682008-11-06 22:02:51 -05001649 if (bio_flags & EXTENT_BIO_COMPRESSED)
1650 return 0;
1651
Chris Masonf2d8d742008-04-21 10:03:05 -04001652 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001653 map_length = length;
David Woodhouse64a16702009-07-15 23:29:37 +01001654 ret = btrfs_map_block(root->fs_info, rw, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001655 &map_length, NULL, 0);
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001656 /* Will always return 0 with map_multi == NULL */
Jeff Mahoney3444a972011-10-03 23:23:13 -04001657 BUG_ON(ret < 0);
Chris Masond3977122009-01-05 21:25:51 -05001658 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001659 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001660 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001661}
1662
Chris Masond352ac62008-09-29 15:18:18 -04001663/*
1664 * in order to insert checksums into the metadata in large chunks,
1665 * we wait until bio submission time. All the pages in the bio are
1666 * checksummed and sums are attached onto the ordered extent record.
1667 *
1668 * At IO completion time the cums attached on the ordered extent record
1669 * are inserted into the btree
1670 */
Chris Masond3977122009-01-05 21:25:51 -05001671static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1672 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001673 unsigned long bio_flags,
1674 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001675{
Chris Mason065631f2008-02-20 12:07:25 -05001676 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001677 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001678
Chris Masond20f7042008-12-08 16:58:54 -05001679 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001680 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001681 return 0;
1682}
Chris Masone0156402008-04-16 11:15:20 -04001683
Chris Mason4a69a412008-11-06 22:03:00 -05001684/*
1685 * in order to insert checksums into the metadata in large chunks,
1686 * we wait until bio submission time. All the pages in the bio are
1687 * checksummed and sums are attached onto the ordered extent record.
1688 *
1689 * At IO completion time the cums attached on the ordered extent record
1690 * are inserted into the btree
1691 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001692static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001693 int mirror_num, unsigned long bio_flags,
1694 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001695{
1696 struct btrfs_root *root = BTRFS_I(inode)->root;
Stefan Behrens61891922012-11-05 18:51:52 +01001697 int ret;
1698
1699 ret = btrfs_map_bio(root, rw, bio, mirror_num, 1);
1700 if (ret)
1701 bio_endio(bio, ret);
1702 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001703}
1704
Chris Masond352ac62008-09-29 15:18:18 -04001705/*
Chris Masoncad321a2008-12-17 14:51:42 -05001706 * extent_io.c submission hook. This does the right thing for csum calculation
1707 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001708 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001709static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001710 int mirror_num, unsigned long bio_flags,
1711 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001712{
1713 struct btrfs_root *root = BTRFS_I(inode)->root;
1714 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001715 int skip_sum;
Jeff Mahoney04173412011-10-03 23:23:12 -04001716 int metadata = 0;
Josef Bacikb812ce22012-11-16 13:56:32 -05001717 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001718
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001719 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001720
Liu Bo83eea1f2012-07-10 05:28:39 -06001721 if (btrfs_is_free_space_inode(inode))
Jeff Mahoney04173412011-10-03 23:23:12 -04001722 metadata = 2;
1723
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001724 if (!(rw & REQ_WRITE)) {
Josef Bacik5fd02042012-05-02 14:00:54 -04001725 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1726 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001727 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001728
Chris Masond20f7042008-12-08 16:58:54 -05001729 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001730 ret = btrfs_submit_compressed_read(inode, bio,
1731 mirror_num,
1732 bio_flags);
1733 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001734 } else if (!skip_sum) {
1735 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1736 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001737 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001738 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001739 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001740 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001741 /* csum items have already been cloned */
1742 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1743 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001744 /* we're doing a write, do the async checksumming */
Stefan Behrens61891922012-11-05 18:51:52 +01001745 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001746 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001747 bio_flags, bio_offset,
1748 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001749 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001750 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001751 } else if (!skip_sum) {
1752 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1753 if (ret)
1754 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001755 }
1756
Chris Mason0b86a832008-03-24 15:01:56 -04001757mapit:
Stefan Behrens61891922012-11-05 18:51:52 +01001758 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
1759
1760out:
1761 if (ret < 0)
1762 bio_endio(bio, ret);
1763 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001764}
Chris Mason6885f302008-02-20 16:11:05 -05001765
Chris Masond352ac62008-09-29 15:18:18 -04001766/*
1767 * given a list of ordered sums record them in the inode. This happens
1768 * at IO completion time based on sums calculated at bio submission time.
1769 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001770static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001771 struct inode *inode, u64 file_offset,
1772 struct list_head *list)
1773{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001774 struct btrfs_ordered_sum *sum;
1775
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001776 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00001777 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05001778 btrfs_csum_file_blocks(trans,
1779 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00001780 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001781 }
1782 return 0;
1783}
1784
Josef Bacik2ac55d42010-02-03 19:33:23 +00001785int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1786 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001787{
Julia Lawall6c1500f2012-11-03 20:30:18 +00001788 WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04001789 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001790 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001791}
1792
Chris Masond352ac62008-09-29 15:18:18 -04001793/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001794struct btrfs_writepage_fixup {
1795 struct page *page;
1796 struct btrfs_work work;
1797};
1798
Christoph Hellwigb2950862008-12-02 09:54:17 -05001799static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001800{
1801 struct btrfs_writepage_fixup *fixup;
1802 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001803 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001804 struct page *page;
1805 struct inode *inode;
1806 u64 page_start;
1807 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001808 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001809
1810 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1811 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001812again:
Chris Mason247e7432008-07-17 12:53:51 -04001813 lock_page(page);
1814 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1815 ClearPageChecked(page);
1816 goto out_page;
1817 }
1818
1819 inode = page->mapping->host;
1820 page_start = page_offset(page);
1821 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1822
Josef Bacik2ac55d42010-02-03 19:33:23 +00001823 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001824 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001825
1826 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001827 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001828 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001829
1830 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1831 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001832 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1833 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001834 unlock_page(page);
1835 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001836 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04001837 goto again;
1838 }
Chris Mason247e7432008-07-17 12:53:51 -04001839
Jeff Mahoney87826df2012-02-15 16:23:57 +01001840 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
1841 if (ret) {
1842 mapping_set_error(page->mapping, ret);
1843 end_extent_writepage(page, ret, page_start, page_end);
1844 ClearPageChecked(page);
1845 goto out;
1846 }
1847
Josef Bacik2ac55d42010-02-03 19:33:23 +00001848 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001849 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001850 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04001851out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001852 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1853 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001854out_page:
1855 unlock_page(page);
1856 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08001857 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04001858}
1859
1860/*
1861 * There are a few paths in the higher layers of the kernel that directly
1862 * set the page dirty bit without asking the filesystem if it is a
1863 * good idea. This causes problems because we want to make sure COW
1864 * properly happens and the data=ordered rules are followed.
1865 *
Chris Masonc8b97812008-10-29 14:49:59 -04001866 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001867 * hasn't been properly setup for IO. We kick off an async process
1868 * to fix it up. The async helper will wait for ordered extents, set
1869 * the delalloc bit and make it safe to write the page.
1870 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001871static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001872{
1873 struct inode *inode = page->mapping->host;
1874 struct btrfs_writepage_fixup *fixup;
1875 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001876
Chris Mason8b62b722009-09-02 16:53:46 -04001877 /* this page is properly in the ordered list */
1878 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001879 return 0;
1880
1881 if (PageChecked(page))
1882 return -EAGAIN;
1883
1884 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1885 if (!fixup)
1886 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001887
Chris Mason247e7432008-07-17 12:53:51 -04001888 SetPageChecked(page);
1889 page_cache_get(page);
1890 fixup->work.func = btrfs_writepage_fixup_worker;
1891 fixup->page = page;
1892 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001893 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04001894}
1895
Yan Zhengd899e052008-10-30 14:25:28 -04001896static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1897 struct inode *inode, u64 file_pos,
1898 u64 disk_bytenr, u64 disk_num_bytes,
1899 u64 num_bytes, u64 ram_bytes,
1900 u8 compression, u8 encryption,
1901 u16 other_encoding, int extent_type)
1902{
1903 struct btrfs_root *root = BTRFS_I(inode)->root;
1904 struct btrfs_file_extent_item *fi;
1905 struct btrfs_path *path;
1906 struct extent_buffer *leaf;
1907 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04001908 int ret;
1909
1910 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07001911 if (!path)
1912 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04001913
Chris Masonb9473432009-03-13 11:00:37 -04001914 path->leave_spinning = 1;
Chris Masona1ed8352009-09-11 12:27:37 -04001915
1916 /*
1917 * we may be replacing one extent in the tree with another.
1918 * The new extent is pinned in the extent map, and we don't want
1919 * to drop it from the cache until it is completely in the btree.
1920 *
1921 * So, tell btrfs_drop_extents to leave this extent in the cache.
1922 * the caller is expected to unpin it and allow it to be merged
1923 * with the others.
1924 */
Josef Bacik5dc562c2012-08-17 13:14:17 -04001925 ret = btrfs_drop_extents(trans, root, inode, file_pos,
Josef Bacik26714852012-08-29 12:24:27 -04001926 file_pos + num_bytes, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001927 if (ret)
1928 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001929
Li Zefan33345d012011-04-20 10:31:50 +08001930 ins.objectid = btrfs_ino(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04001931 ins.offset = file_pos;
1932 ins.type = BTRFS_EXTENT_DATA_KEY;
1933 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001934 if (ret)
1935 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001936 leaf = path->nodes[0];
1937 fi = btrfs_item_ptr(leaf, path->slots[0],
1938 struct btrfs_file_extent_item);
1939 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1940 btrfs_set_file_extent_type(leaf, fi, extent_type);
1941 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1942 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1943 btrfs_set_file_extent_offset(leaf, fi, 0);
1944 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1945 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1946 btrfs_set_file_extent_compression(leaf, fi, compression);
1947 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1948 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04001949
Yan Zhengd899e052008-10-30 14:25:28 -04001950 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04001951 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04001952
1953 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04001954
1955 ins.objectid = disk_bytenr;
1956 ins.offset = disk_num_bytes;
1957 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001958 ret = btrfs_alloc_reserved_file_extent(trans, root,
1959 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08001960 btrfs_ino(inode), file_pos, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001961out:
Yan Zhengd899e052008-10-30 14:25:28 -04001962 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04001963
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001964 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04001965}
1966
Liu Bo38c227d2013-01-29 03:18:40 +00001967/* snapshot-aware defrag */
1968struct sa_defrag_extent_backref {
1969 struct rb_node node;
1970 struct old_sa_defrag_extent *old;
1971 u64 root_id;
1972 u64 inum;
1973 u64 file_pos;
1974 u64 extent_offset;
1975 u64 num_bytes;
1976 u64 generation;
1977};
1978
1979struct old_sa_defrag_extent {
1980 struct list_head list;
1981 struct new_sa_defrag_extent *new;
1982
1983 u64 extent_offset;
1984 u64 bytenr;
1985 u64 offset;
1986 u64 len;
1987 int count;
1988};
1989
1990struct new_sa_defrag_extent {
1991 struct rb_root root;
1992 struct list_head head;
1993 struct btrfs_path *path;
1994 struct inode *inode;
1995 u64 file_pos;
1996 u64 len;
1997 u64 bytenr;
1998 u64 disk_len;
1999 u8 compress_type;
2000};
2001
2002static int backref_comp(struct sa_defrag_extent_backref *b1,
2003 struct sa_defrag_extent_backref *b2)
2004{
2005 if (b1->root_id < b2->root_id)
2006 return -1;
2007 else if (b1->root_id > b2->root_id)
2008 return 1;
2009
2010 if (b1->inum < b2->inum)
2011 return -1;
2012 else if (b1->inum > b2->inum)
2013 return 1;
2014
2015 if (b1->file_pos < b2->file_pos)
2016 return -1;
2017 else if (b1->file_pos > b2->file_pos)
2018 return 1;
2019
2020 /*
2021 * [------------------------------] ===> (a range of space)
2022 * |<--->| |<---->| =============> (fs/file tree A)
2023 * |<---------------------------->| ===> (fs/file tree B)
2024 *
2025 * A range of space can refer to two file extents in one tree while
2026 * refer to only one file extent in another tree.
2027 *
2028 * So we may process a disk offset more than one time(two extents in A)
2029 * and locate at the same extent(one extent in B), then insert two same
2030 * backrefs(both refer to the extent in B).
2031 */
2032 return 0;
2033}
2034
2035static void backref_insert(struct rb_root *root,
2036 struct sa_defrag_extent_backref *backref)
2037{
2038 struct rb_node **p = &root->rb_node;
2039 struct rb_node *parent = NULL;
2040 struct sa_defrag_extent_backref *entry;
2041 int ret;
2042
2043 while (*p) {
2044 parent = *p;
2045 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2046
2047 ret = backref_comp(backref, entry);
2048 if (ret < 0)
2049 p = &(*p)->rb_left;
2050 else
2051 p = &(*p)->rb_right;
2052 }
2053
2054 rb_link_node(&backref->node, parent, p);
2055 rb_insert_color(&backref->node, root);
2056}
2057
2058/*
2059 * Note the backref might has changed, and in this case we just return 0.
2060 */
2061static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2062 void *ctx)
2063{
2064 struct btrfs_file_extent_item *extent;
2065 struct btrfs_fs_info *fs_info;
2066 struct old_sa_defrag_extent *old = ctx;
2067 struct new_sa_defrag_extent *new = old->new;
2068 struct btrfs_path *path = new->path;
2069 struct btrfs_key key;
2070 struct btrfs_root *root;
2071 struct sa_defrag_extent_backref *backref;
2072 struct extent_buffer *leaf;
2073 struct inode *inode = new->inode;
2074 int slot;
2075 int ret;
2076 u64 extent_offset;
2077 u64 num_bytes;
2078
2079 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
2080 inum == btrfs_ino(inode))
2081 return 0;
2082
2083 key.objectid = root_id;
2084 key.type = BTRFS_ROOT_ITEM_KEY;
2085 key.offset = (u64)-1;
2086
2087 fs_info = BTRFS_I(inode)->root->fs_info;
2088 root = btrfs_read_fs_root_no_name(fs_info, &key);
2089 if (IS_ERR(root)) {
2090 if (PTR_ERR(root) == -ENOENT)
2091 return 0;
2092 WARN_ON(1);
2093 pr_debug("inum=%llu, offset=%llu, root_id=%llu\n",
2094 inum, offset, root_id);
2095 return PTR_ERR(root);
2096 }
2097
2098 key.objectid = inum;
2099 key.type = BTRFS_EXTENT_DATA_KEY;
2100 if (offset > (u64)-1 << 32)
2101 key.offset = 0;
2102 else
2103 key.offset = offset;
2104
2105 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2106 if (ret < 0) {
2107 WARN_ON(1);
2108 return ret;
2109 }
Josef Bacik50f13192013-07-22 12:50:37 -04002110 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002111
2112 while (1) {
2113 cond_resched();
2114
2115 leaf = path->nodes[0];
2116 slot = path->slots[0];
2117
2118 if (slot >= btrfs_header_nritems(leaf)) {
2119 ret = btrfs_next_leaf(root, path);
2120 if (ret < 0) {
2121 goto out;
2122 } else if (ret > 0) {
2123 ret = 0;
2124 goto out;
2125 }
2126 continue;
2127 }
2128
2129 path->slots[0]++;
2130
2131 btrfs_item_key_to_cpu(leaf, &key, slot);
2132
2133 if (key.objectid > inum)
2134 goto out;
2135
2136 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2137 continue;
2138
2139 extent = btrfs_item_ptr(leaf, slot,
2140 struct btrfs_file_extent_item);
2141
2142 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2143 continue;
2144
Liu Boe68afa42013-07-01 22:13:26 +08002145 /*
2146 * 'offset' refers to the exact key.offset,
2147 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2148 * (key.offset - extent_offset).
2149 */
2150 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002151 continue;
2152
Liu Boe68afa42013-07-01 22:13:26 +08002153 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002154 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002155
Liu Bo38c227d2013-01-29 03:18:40 +00002156 if (extent_offset >= old->extent_offset + old->offset +
2157 old->len || extent_offset + num_bytes <=
2158 old->extent_offset + old->offset)
2159 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002160 break;
2161 }
2162
2163 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2164 if (!backref) {
2165 ret = -ENOENT;
2166 goto out;
2167 }
2168
2169 backref->root_id = root_id;
2170 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002171 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002172 backref->num_bytes = num_bytes;
2173 backref->extent_offset = extent_offset;
2174 backref->generation = btrfs_file_extent_generation(leaf, extent);
2175 backref->old = old;
2176 backref_insert(&new->root, backref);
2177 old->count++;
2178out:
2179 btrfs_release_path(path);
2180 WARN_ON(ret);
2181 return ret;
2182}
2183
2184static noinline bool record_extent_backrefs(struct btrfs_path *path,
2185 struct new_sa_defrag_extent *new)
2186{
2187 struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info;
2188 struct old_sa_defrag_extent *old, *tmp;
2189 int ret;
2190
2191 new->path = path;
2192
2193 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002194 ret = iterate_inodes_from_logical(old->bytenr +
2195 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002196 path, record_one_backref,
2197 old);
2198 BUG_ON(ret < 0 && ret != -ENOENT);
2199
2200 /* no backref to be processed for this extent */
2201 if (!old->count) {
2202 list_del(&old->list);
2203 kfree(old);
2204 }
2205 }
2206
2207 if (list_empty(&new->head))
2208 return false;
2209
2210 return true;
2211}
2212
2213static int relink_is_mergable(struct extent_buffer *leaf,
2214 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002215 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002216{
Liu Bo116e0022013-08-02 16:30:40 +08002217 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002218 return 0;
2219
2220 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2221 return 0;
2222
Liu Bo116e0022013-08-02 16:30:40 +08002223 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2224 return 0;
2225
2226 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002227 btrfs_file_extent_other_encoding(leaf, fi))
2228 return 0;
2229
2230 return 1;
2231}
2232
2233/*
2234 * Note the backref might has changed, and in this case we just return 0.
2235 */
2236static noinline int relink_extent_backref(struct btrfs_path *path,
2237 struct sa_defrag_extent_backref *prev,
2238 struct sa_defrag_extent_backref *backref)
2239{
2240 struct btrfs_file_extent_item *extent;
2241 struct btrfs_file_extent_item *item;
2242 struct btrfs_ordered_extent *ordered;
2243 struct btrfs_trans_handle *trans;
2244 struct btrfs_fs_info *fs_info;
2245 struct btrfs_root *root;
2246 struct btrfs_key key;
2247 struct extent_buffer *leaf;
2248 struct old_sa_defrag_extent *old = backref->old;
2249 struct new_sa_defrag_extent *new = old->new;
2250 struct inode *src_inode = new->inode;
2251 struct inode *inode;
2252 struct extent_state *cached = NULL;
2253 int ret = 0;
2254 u64 start;
2255 u64 len;
2256 u64 lock_start;
2257 u64 lock_end;
2258 bool merge = false;
2259 int index;
2260
2261 if (prev && prev->root_id == backref->root_id &&
2262 prev->inum == backref->inum &&
2263 prev->file_pos + prev->num_bytes == backref->file_pos)
2264 merge = true;
2265
2266 /* step 1: get root */
2267 key.objectid = backref->root_id;
2268 key.type = BTRFS_ROOT_ITEM_KEY;
2269 key.offset = (u64)-1;
2270
2271 fs_info = BTRFS_I(src_inode)->root->fs_info;
2272 index = srcu_read_lock(&fs_info->subvol_srcu);
2273
2274 root = btrfs_read_fs_root_no_name(fs_info, &key);
2275 if (IS_ERR(root)) {
2276 srcu_read_unlock(&fs_info->subvol_srcu, index);
2277 if (PTR_ERR(root) == -ENOENT)
2278 return 0;
2279 return PTR_ERR(root);
2280 }
Liu Bo38c227d2013-01-29 03:18:40 +00002281
2282 /* step 2: get inode */
2283 key.objectid = backref->inum;
2284 key.type = BTRFS_INODE_ITEM_KEY;
2285 key.offset = 0;
2286
2287 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2288 if (IS_ERR(inode)) {
2289 srcu_read_unlock(&fs_info->subvol_srcu, index);
2290 return 0;
2291 }
2292
2293 srcu_read_unlock(&fs_info->subvol_srcu, index);
2294
2295 /* step 3: relink backref */
2296 lock_start = backref->file_pos;
2297 lock_end = backref->file_pos + backref->num_bytes - 1;
2298 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2299 0, &cached);
2300
2301 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2302 if (ordered) {
2303 btrfs_put_ordered_extent(ordered);
2304 goto out_unlock;
2305 }
2306
2307 trans = btrfs_join_transaction(root);
2308 if (IS_ERR(trans)) {
2309 ret = PTR_ERR(trans);
2310 goto out_unlock;
2311 }
2312
2313 key.objectid = backref->inum;
2314 key.type = BTRFS_EXTENT_DATA_KEY;
2315 key.offset = backref->file_pos;
2316
2317 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2318 if (ret < 0) {
2319 goto out_free_path;
2320 } else if (ret > 0) {
2321 ret = 0;
2322 goto out_free_path;
2323 }
2324
2325 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2326 struct btrfs_file_extent_item);
2327
2328 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2329 backref->generation)
2330 goto out_free_path;
2331
2332 btrfs_release_path(path);
2333
2334 start = backref->file_pos;
2335 if (backref->extent_offset < old->extent_offset + old->offset)
2336 start += old->extent_offset + old->offset -
2337 backref->extent_offset;
2338
2339 len = min(backref->extent_offset + backref->num_bytes,
2340 old->extent_offset + old->offset + old->len);
2341 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2342
2343 ret = btrfs_drop_extents(trans, root, inode, start,
2344 start + len, 1);
2345 if (ret)
2346 goto out_free_path;
2347again:
2348 key.objectid = btrfs_ino(inode);
2349 key.type = BTRFS_EXTENT_DATA_KEY;
2350 key.offset = start;
2351
Liu Boa09a0a72013-03-11 09:20:58 +00002352 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002353 if (merge) {
2354 struct btrfs_file_extent_item *fi;
2355 u64 extent_len;
2356 struct btrfs_key found_key;
2357
2358 ret = btrfs_search_slot(trans, root, &key, path, 1, 1);
2359 if (ret < 0)
2360 goto out_free_path;
2361
2362 path->slots[0]--;
2363 leaf = path->nodes[0];
2364 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2365
2366 fi = btrfs_item_ptr(leaf, path->slots[0],
2367 struct btrfs_file_extent_item);
2368 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2369
Liu Bo116e0022013-08-02 16:30:40 +08002370 if (extent_len + found_key.offset == start &&
2371 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002372 btrfs_set_file_extent_num_bytes(leaf, fi,
2373 extent_len + len);
2374 btrfs_mark_buffer_dirty(leaf);
2375 inode_add_bytes(inode, len);
2376
2377 ret = 1;
2378 goto out_free_path;
2379 } else {
2380 merge = false;
2381 btrfs_release_path(path);
2382 goto again;
2383 }
2384 }
2385
2386 ret = btrfs_insert_empty_item(trans, root, path, &key,
2387 sizeof(*extent));
2388 if (ret) {
2389 btrfs_abort_transaction(trans, root, ret);
2390 goto out_free_path;
2391 }
2392
2393 leaf = path->nodes[0];
2394 item = btrfs_item_ptr(leaf, path->slots[0],
2395 struct btrfs_file_extent_item);
2396 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2397 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2398 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2399 btrfs_set_file_extent_num_bytes(leaf, item, len);
2400 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2401 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2402 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2403 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2404 btrfs_set_file_extent_encryption(leaf, item, 0);
2405 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2406
2407 btrfs_mark_buffer_dirty(leaf);
2408 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002409 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002410
2411 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
2412 new->disk_len, 0,
2413 backref->root_id, backref->inum,
2414 new->file_pos, 0); /* start - extent_offset */
2415 if (ret) {
2416 btrfs_abort_transaction(trans, root, ret);
2417 goto out_free_path;
2418 }
2419
2420 ret = 1;
2421out_free_path:
2422 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002423 path->leave_spinning = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002424 btrfs_end_transaction(trans, root);
2425out_unlock:
2426 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2427 &cached, GFP_NOFS);
2428 iput(inode);
2429 return ret;
2430}
2431
2432static void relink_file_extents(struct new_sa_defrag_extent *new)
2433{
2434 struct btrfs_path *path;
2435 struct old_sa_defrag_extent *old, *tmp;
2436 struct sa_defrag_extent_backref *backref;
2437 struct sa_defrag_extent_backref *prev = NULL;
2438 struct inode *inode;
2439 struct btrfs_root *root;
2440 struct rb_node *node;
2441 int ret;
2442
2443 inode = new->inode;
2444 root = BTRFS_I(inode)->root;
2445
2446 path = btrfs_alloc_path();
2447 if (!path)
2448 return;
2449
2450 if (!record_extent_backrefs(path, new)) {
2451 btrfs_free_path(path);
2452 goto out;
2453 }
2454 btrfs_release_path(path);
2455
2456 while (1) {
2457 node = rb_first(&new->root);
2458 if (!node)
2459 break;
2460 rb_erase(node, &new->root);
2461
2462 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2463
2464 ret = relink_extent_backref(path, prev, backref);
2465 WARN_ON(ret < 0);
2466
2467 kfree(prev);
2468
2469 if (ret == 1)
2470 prev = backref;
2471 else
2472 prev = NULL;
2473 cond_resched();
2474 }
2475 kfree(prev);
2476
2477 btrfs_free_path(path);
2478
2479 list_for_each_entry_safe(old, tmp, &new->head, list) {
2480 list_del(&old->list);
2481 kfree(old);
2482 }
2483out:
2484 atomic_dec(&root->fs_info->defrag_running);
2485 wake_up(&root->fs_info->transaction_wait);
2486
2487 kfree(new);
2488}
2489
2490static struct new_sa_defrag_extent *
2491record_old_file_extents(struct inode *inode,
2492 struct btrfs_ordered_extent *ordered)
2493{
2494 struct btrfs_root *root = BTRFS_I(inode)->root;
2495 struct btrfs_path *path;
2496 struct btrfs_key key;
2497 struct old_sa_defrag_extent *old, *tmp;
2498 struct new_sa_defrag_extent *new;
2499 int ret;
2500
2501 new = kmalloc(sizeof(*new), GFP_NOFS);
2502 if (!new)
2503 return NULL;
2504
2505 new->inode = inode;
2506 new->file_pos = ordered->file_offset;
2507 new->len = ordered->len;
2508 new->bytenr = ordered->start;
2509 new->disk_len = ordered->disk_len;
2510 new->compress_type = ordered->compress_type;
2511 new->root = RB_ROOT;
2512 INIT_LIST_HEAD(&new->head);
2513
2514 path = btrfs_alloc_path();
2515 if (!path)
2516 goto out_kfree;
2517
2518 key.objectid = btrfs_ino(inode);
2519 key.type = BTRFS_EXTENT_DATA_KEY;
2520 key.offset = new->file_pos;
2521
2522 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2523 if (ret < 0)
2524 goto out_free_path;
2525 if (ret > 0 && path->slots[0] > 0)
2526 path->slots[0]--;
2527
2528 /* find out all the old extents for the file range */
2529 while (1) {
2530 struct btrfs_file_extent_item *extent;
2531 struct extent_buffer *l;
2532 int slot;
2533 u64 num_bytes;
2534 u64 offset;
2535 u64 end;
2536 u64 disk_bytenr;
2537 u64 extent_offset;
2538
2539 l = path->nodes[0];
2540 slot = path->slots[0];
2541
2542 if (slot >= btrfs_header_nritems(l)) {
2543 ret = btrfs_next_leaf(root, path);
2544 if (ret < 0)
2545 goto out_free_list;
2546 else if (ret > 0)
2547 break;
2548 continue;
2549 }
2550
2551 btrfs_item_key_to_cpu(l, &key, slot);
2552
2553 if (key.objectid != btrfs_ino(inode))
2554 break;
2555 if (key.type != BTRFS_EXTENT_DATA_KEY)
2556 break;
2557 if (key.offset >= new->file_pos + new->len)
2558 break;
2559
2560 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2561
2562 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2563 if (key.offset + num_bytes < new->file_pos)
2564 goto next;
2565
2566 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2567 if (!disk_bytenr)
2568 goto next;
2569
2570 extent_offset = btrfs_file_extent_offset(l, extent);
2571
2572 old = kmalloc(sizeof(*old), GFP_NOFS);
2573 if (!old)
2574 goto out_free_list;
2575
2576 offset = max(new->file_pos, key.offset);
2577 end = min(new->file_pos + new->len, key.offset + num_bytes);
2578
2579 old->bytenr = disk_bytenr;
2580 old->extent_offset = extent_offset;
2581 old->offset = offset - key.offset;
2582 old->len = end - offset;
2583 old->new = new;
2584 old->count = 0;
2585 list_add_tail(&old->list, &new->head);
2586next:
2587 path->slots[0]++;
2588 cond_resched();
2589 }
2590
2591 btrfs_free_path(path);
2592 atomic_inc(&root->fs_info->defrag_running);
2593
2594 return new;
2595
2596out_free_list:
2597 list_for_each_entry_safe(old, tmp, &new->head, list) {
2598 list_del(&old->list);
2599 kfree(old);
2600 }
2601out_free_path:
2602 btrfs_free_path(path);
2603out_kfree:
2604 kfree(new);
2605 return NULL;
2606}
2607
Chris Mason5d13a982009-03-13 11:41:46 -04002608/*
2609 * helper function for btrfs_finish_ordered_io, this
2610 * just reads in some of the csum leaves to prime them into ram
2611 * before we start the transaction. It limits the amount of btree
2612 * reads required while inside the transaction.
2613 */
Chris Masond352ac62008-09-29 15:18:18 -04002614/* as ordered data IO finishes, this gets called so we can finish
2615 * an ordered extent if the range of bytes in the file it covers are
2616 * fully written.
2617 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002618static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002619{
Josef Bacik5fd02042012-05-02 14:00:54 -04002620 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002621 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002622 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002623 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002624 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002625 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002626 int compress_type = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002627 int ret;
Li Zefan82d59022011-04-20 10:33:24 +08002628 bool nolock;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002629
Liu Bo83eea1f2012-07-10 05:28:39 -06002630 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002631
Josef Bacik5fd02042012-05-02 14:00:54 -04002632 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2633 ret = -EIO;
2634 goto out;
2635 }
2636
Yan, Zhengc2167752009-11-12 09:34:21 +00002637 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002638 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Josef Bacik6c760c02012-11-09 10:53:21 -05002639 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2640 if (nolock)
2641 trans = btrfs_join_transaction_nolock(root);
2642 else
2643 trans = btrfs_join_transaction(root);
2644 if (IS_ERR(trans)) {
2645 ret = PTR_ERR(trans);
2646 trans = NULL;
2647 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002648 }
Josef Bacik6c760c02012-11-09 10:53:21 -05002649 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
2650 ret = btrfs_update_inode_fallback(trans, root, inode);
2651 if (ret) /* -ENOMEM or corruption */
2652 btrfs_abort_transaction(trans, root, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002653 goto out;
2654 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002655
Josef Bacik2ac55d42010-02-03 19:33:23 +00002656 lock_extent_bits(io_tree, ordered_extent->file_offset,
2657 ordered_extent->file_offset + ordered_extent->len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002658 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002659
Liu Bo38c227d2013-01-29 03:18:40 +00002660 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2661 ordered_extent->file_offset + ordered_extent->len - 1,
2662 EXTENT_DEFRAG, 1, cached_state);
2663 if (ret) {
2664 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
2665 if (last_snapshot >= BTRFS_I(inode)->generation)
2666 /* the inode is shared */
2667 new = record_old_file_extents(inode, ordered_extent);
2668
2669 clear_extent_bit(io_tree, ordered_extent->file_offset,
2670 ordered_extent->file_offset + ordered_extent->len - 1,
2671 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2672 }
2673
Josef Bacik0cb59c92010-07-02 12:14:14 -04002674 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002675 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002676 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002677 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002678 if (IS_ERR(trans)) {
2679 ret = PTR_ERR(trans);
2680 trans = NULL;
2681 goto out_unlock;
2682 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002683 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002684
Chris Masonc8b97812008-10-29 14:49:59 -04002685 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002686 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002687 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002688 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00002689 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002690 ordered_extent->file_offset,
2691 ordered_extent->file_offset +
2692 ordered_extent->len);
Yan Zhengd899e052008-10-30 14:25:28 -04002693 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04002694 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002695 ret = insert_reserved_file_extent(trans, inode,
2696 ordered_extent->file_offset,
2697 ordered_extent->start,
2698 ordered_extent->disk_len,
2699 ordered_extent->len,
2700 ordered_extent->len,
Li Zefan261507a02010-12-17 14:21:50 +08002701 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002702 BTRFS_FILE_EXTENT_REG);
Yan Zhengd899e052008-10-30 14:25:28 -04002703 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002704 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2705 ordered_extent->file_offset, ordered_extent->len,
2706 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002707 if (ret < 0) {
2708 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002709 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002710 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002711
Chris Masone6dcd2d2008-07-17 12:53:50 -04002712 add_pending_csums(trans, inode, ordered_extent->file_offset,
2713 &ordered_extent->list);
2714
Josef Bacik6c760c02012-11-09 10:53:21 -05002715 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2716 ret = btrfs_update_inode_fallback(trans, root, inode);
2717 if (ret) { /* -ENOMEM or corruption */
2718 btrfs_abort_transaction(trans, root, ret);
2719 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002720 }
2721 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002722out_unlock:
2723 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2724 ordered_extent->file_offset +
2725 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00002726out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002727 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04002728 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002729 if (trans)
2730 btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002731
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002732 if (ret) {
Josef Bacik5fd02042012-05-02 14:00:54 -04002733 clear_extent_uptodate(io_tree, ordered_extent->file_offset,
2734 ordered_extent->file_offset +
2735 ordered_extent->len - 1, NULL, GFP_NOFS);
2736
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002737 /*
2738 * If the ordered extent had an IOERR or something else went
2739 * wrong we need to return the space for this ordered extent
2740 * back to the allocator.
2741 */
2742 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2743 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
2744 btrfs_free_reserved_extent(root, ordered_extent->start,
2745 ordered_extent->disk_len);
2746 }
2747
2748
Josef Bacik5fd02042012-05-02 14:00:54 -04002749 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002750 * This needs to be done to make sure anybody waiting knows we are done
2751 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002752 */
2753 btrfs_remove_ordered_extent(inode, ordered_extent);
2754
Liu Bo38c227d2013-01-29 03:18:40 +00002755 /* for snapshot-aware defrag */
2756 if (new)
2757 relink_file_extents(new);
2758
Chris Masone6dcd2d2008-07-17 12:53:50 -04002759 /* once for us */
2760 btrfs_put_ordered_extent(ordered_extent);
2761 /* once for the tree */
2762 btrfs_put_ordered_extent(ordered_extent);
2763
Josef Bacik5fd02042012-05-02 14:00:54 -04002764 return ret;
2765}
2766
2767static void finish_ordered_fn(struct btrfs_work *work)
2768{
2769 struct btrfs_ordered_extent *ordered_extent;
2770 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2771 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002772}
2773
Christoph Hellwigb2950862008-12-02 09:54:17 -05002774static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04002775 struct extent_state *state, int uptodate)
2776{
Josef Bacik5fd02042012-05-02 14:00:54 -04002777 struct inode *inode = page->mapping->host;
2778 struct btrfs_root *root = BTRFS_I(inode)->root;
2779 struct btrfs_ordered_extent *ordered_extent = NULL;
2780 struct btrfs_workers *workers;
2781
liubo1abe9b82011-03-24 11:18:59 +00002782 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2783
Chris Mason8b62b722009-09-02 16:53:46 -04002784 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002785 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2786 end - start + 1, uptodate))
2787 return 0;
2788
2789 ordered_extent->work.func = finish_ordered_fn;
2790 ordered_extent->work.flags = 0;
2791
Liu Bo83eea1f2012-07-10 05:28:39 -06002792 if (btrfs_is_free_space_inode(inode))
Josef Bacik5fd02042012-05-02 14:00:54 -04002793 workers = &root->fs_info->endio_freespace_worker;
2794 else
2795 workers = &root->fs_info->endio_write_workers;
2796 btrfs_queue_worker(workers, &ordered_extent->work);
2797
2798 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04002799}
2800
Chris Masond352ac62008-09-29 15:18:18 -04002801/*
Chris Masond352ac62008-09-29 15:18:18 -04002802 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002803 * if there's a match, we allow the bio to finish. If not, the code in
2804 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002805 */
Miao Xiefacc8a222013-07-25 19:22:34 +08002806static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
2807 u64 phy_offset, struct page *page,
2808 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002809{
Miao Xie4eee4fa2012-12-21 09:17:45 +00002810 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04002811 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002812 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04002813 char *kaddr;
Chris Masonff79f812007-10-15 16:22:25 -04002814 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiefacc8a222013-07-25 19:22:34 +08002815 u32 csum_expected;
Chris Masonff79f812007-10-15 16:22:25 -04002816 u32 csum = ~(u32)0;
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002817 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
2818 DEFAULT_RATELIMIT_BURST);
Chris Masond1310b22008-01-24 16:13:08 -05002819
Chris Masond20f7042008-12-08 16:58:54 -05002820 if (PageChecked(page)) {
2821 ClearPageChecked(page);
2822 goto good;
2823 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002824
2825 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Jan Schmidt08d2f342011-05-04 16:18:50 +02002826 goto good;
Chris Masond20f7042008-12-08 16:58:54 -05002827
Yan Zheng17d217f2008-12-12 10:03:38 -05002828 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002829 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002830 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
2831 GFP_NOFS);
2832 return 0;
2833 }
2834
Miao Xiefacc8a222013-07-25 19:22:34 +08002835 phy_offset >>= inode->i_sb->s_blocksize_bits;
2836 csum_expected = *(((u32 *)io_bio->csum) + phy_offset);
Chris Masond3977122009-01-05 21:25:51 -05002837
Miao Xiefacc8a222013-07-25 19:22:34 +08002838 kaddr = kmap_atomic(page);
Liu Bob0496682013-03-14 14:57:45 +00002839 csum = btrfs_csum_data(kaddr + offset, csum, end - start + 1);
Chris Masonff79f812007-10-15 16:22:25 -04002840 btrfs_csum_final(csum, (char *)&csum);
Miao Xiefacc8a222013-07-25 19:22:34 +08002841 if (csum != csum_expected)
Chris Mason07157aa2007-08-30 08:50:51 -04002842 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002843
Cong Wang7ac687d2011-11-25 23:14:28 +08002844 kunmap_atomic(kaddr);
Chris Masond20f7042008-12-08 16:58:54 -05002845good:
Chris Mason07157aa2007-08-30 08:50:51 -04002846 return 0;
2847
2848zeroit:
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002849 if (__ratelimit(&_rs))
Miao Xiefacc8a222013-07-25 19:22:34 +08002850 btrfs_info(root->fs_info, "csum failed ino %llu off %llu csum %u expected csum %u",
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002851 (unsigned long long)btrfs_ino(page->mapping->host),
Miao Xiefacc8a222013-07-25 19:22:34 +08002852 (unsigned long long)start, csum, csum_expected);
Chris Masondb945352007-10-15 16:15:53 -04002853 memset(kaddr + offset, 1, end - start + 1);
2854 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002855 kunmap_atomic(kaddr);
Miao Xiefacc8a222013-07-25 19:22:34 +08002856 if (csum_expected == 0)
Chris Mason3b951512008-04-17 11:29:12 -04002857 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04002858 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04002859}
Chris Masonb888db22007-08-27 16:49:44 -04002860
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002861struct delayed_iput {
2862 struct list_head list;
2863 struct inode *inode;
2864};
2865
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002866/* JDM: If this is fs-wide, why can't we add a pointer to
2867 * btrfs_inode instead and avoid the allocation? */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002868void btrfs_add_delayed_iput(struct inode *inode)
2869{
2870 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2871 struct delayed_iput *delayed;
2872
2873 if (atomic_add_unless(&inode->i_count, -1, 1))
2874 return;
2875
2876 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2877 delayed->inode = inode;
2878
2879 spin_lock(&fs_info->delayed_iput_lock);
2880 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2881 spin_unlock(&fs_info->delayed_iput_lock);
2882}
2883
2884void btrfs_run_delayed_iputs(struct btrfs_root *root)
2885{
2886 LIST_HEAD(list);
2887 struct btrfs_fs_info *fs_info = root->fs_info;
2888 struct delayed_iput *delayed;
2889 int empty;
2890
2891 spin_lock(&fs_info->delayed_iput_lock);
2892 empty = list_empty(&fs_info->delayed_iputs);
2893 spin_unlock(&fs_info->delayed_iput_lock);
2894 if (empty)
2895 return;
2896
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002897 spin_lock(&fs_info->delayed_iput_lock);
2898 list_splice_init(&fs_info->delayed_iputs, &list);
2899 spin_unlock(&fs_info->delayed_iput_lock);
2900
2901 while (!list_empty(&list)) {
2902 delayed = list_entry(list.next, struct delayed_iput, list);
2903 list_del(&delayed->list);
2904 iput(delayed->inode);
2905 kfree(delayed);
2906 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002907}
2908
Yan, Zhengd68fc572010-05-16 10:49:58 -04002909/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08002910 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04002911 * files in the subvolume, it removes orphan item and frees block_rsv
2912 * structure.
2913 */
2914void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
2915 struct btrfs_root *root)
2916{
Josef Bacik90290e12011-12-02 15:44:12 -05002917 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002918 int ret;
2919
Josef Bacik8a35d952012-05-23 14:26:42 -04002920 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04002921 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
2922 return;
2923
Josef Bacik90290e12011-12-02 15:44:12 -05002924 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04002925 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05002926 spin_unlock(&root->orphan_lock);
2927 return;
2928 }
2929
2930 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
2931 spin_unlock(&root->orphan_lock);
2932 return;
2933 }
2934
2935 block_rsv = root->orphan_block_rsv;
2936 root->orphan_block_rsv = NULL;
2937 spin_unlock(&root->orphan_lock);
2938
Yan, Zhengd68fc572010-05-16 10:49:58 -04002939 if (root->orphan_item_inserted &&
2940 btrfs_root_refs(&root->root_item) > 0) {
2941 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
2942 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04002943 if (ret)
2944 btrfs_abort_transaction(trans, root, ret);
2945 else
2946 root->orphan_item_inserted = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002947 }
2948
Josef Bacik90290e12011-12-02 15:44:12 -05002949 if (block_rsv) {
2950 WARN_ON(block_rsv->size > 0);
2951 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002952 }
2953}
2954
2955/*
Josef Bacik7b128762008-07-24 12:17:14 -04002956 * This creates an orphan entry for the given inode in case something goes
2957 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04002958 *
2959 * NOTE: caller of this function should reserve 5 units of metadata for
2960 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04002961 */
2962int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2963{
2964 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002965 struct btrfs_block_rsv *block_rsv = NULL;
2966 int reserve = 0;
2967 int insert = 0;
2968 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002969
Yan, Zhengd68fc572010-05-16 10:49:58 -04002970 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06002971 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00002972 if (!block_rsv)
2973 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04002974 }
2975
Yan, Zhengd68fc572010-05-16 10:49:58 -04002976 spin_lock(&root->orphan_lock);
2977 if (!root->orphan_block_rsv) {
2978 root->orphan_block_rsv = block_rsv;
2979 } else if (block_rsv) {
2980 btrfs_free_block_rsv(root, block_rsv);
2981 block_rsv = NULL;
2982 }
Josef Bacik7b128762008-07-24 12:17:14 -04002983
Josef Bacik8a35d952012-05-23 14:26:42 -04002984 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2985 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04002986#if 0
2987 /*
2988 * For proper ENOSPC handling, we should do orphan
2989 * cleanup when mounting. But this introduces backward
2990 * compatibility issue.
2991 */
2992 if (!xchg(&root->orphan_item_inserted, 1))
2993 insert = 2;
2994 else
2995 insert = 1;
2996#endif
2997 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06002998 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002999 }
Josef Bacik7b128762008-07-24 12:17:14 -04003000
Josef Bacik72ac3c02012-05-23 14:13:11 -04003001 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3002 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003003 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003004 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003005
Yan, Zhengd68fc572010-05-16 10:49:58 -04003006 /* grab metadata reservation from transaction handle */
3007 if (reserve) {
3008 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003009 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04003010 }
3011
3012 /* insert an orphan item to track this unlinked/truncated file */
3013 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08003014 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003015 if (ret) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003016 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3017 &BTRFS_I(inode)->runtime_flags);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003018 if (reserve) {
3019 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3020 &BTRFS_I(inode)->runtime_flags);
3021 btrfs_orphan_release_metadata(inode);
3022 }
3023 if (ret != -EEXIST) {
3024 btrfs_abort_transaction(trans, root, ret);
3025 return ret;
3026 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003027 }
3028 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003029 }
3030
3031 /* insert an orphan item to track subvolume contains orphan files */
3032 if (insert >= 2) {
3033 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
3034 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003035 if (ret && ret != -EEXIST) {
3036 btrfs_abort_transaction(trans, root, ret);
3037 return ret;
3038 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003039 }
3040 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003041}
3042
3043/*
3044 * We have done the truncate/delete so we can go ahead and remove the orphan
3045 * item for this particular inode.
3046 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003047static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3048 struct inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003049{
3050 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003051 int delete_item = 0;
3052 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003053 int ret = 0;
3054
Yan, Zhengd68fc572010-05-16 10:49:58 -04003055 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003056 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3057 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003058 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003059
Josef Bacik72ac3c02012-05-23 14:13:11 -04003060 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3061 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003062 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003063 spin_unlock(&root->orphan_lock);
3064
Josef Bacik4ef31a42013-08-13 14:10:08 -04003065 if (trans && delete_item)
Li Zefan33345d012011-04-20 10:31:50 +08003066 ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04003067
Josef Bacik8a35d952012-05-23 14:26:42 -04003068 if (release_rsv) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003069 btrfs_orphan_release_metadata(inode);
Josef Bacik8a35d952012-05-23 14:26:42 -04003070 atomic_dec(&root->orphan_inodes);
3071 }
Josef Bacik7b128762008-07-24 12:17:14 -04003072
Josef Bacik4ef31a42013-08-13 14:10:08 -04003073 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003074}
3075
3076/*
3077 * this cleans up any orphans that may be left on the list from the last use
3078 * of this root.
3079 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003080int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003081{
3082 struct btrfs_path *path;
3083 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003084 struct btrfs_key key, found_key;
3085 struct btrfs_trans_handle *trans;
3086 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003087 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003088 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3089
Yan, Zhengd68fc572010-05-16 10:49:58 -04003090 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003091 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003092
3093 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003094 if (!path) {
3095 ret = -ENOMEM;
3096 goto out;
3097 }
Josef Bacik7b128762008-07-24 12:17:14 -04003098 path->reada = -1;
3099
3100 key.objectid = BTRFS_ORPHAN_OBJECTID;
3101 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
3102 key.offset = (u64)-1;
3103
Josef Bacik7b128762008-07-24 12:17:14 -04003104 while (1) {
3105 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003106 if (ret < 0)
3107 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003108
3109 /*
3110 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003111 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003112 * find the key and see if we have stuff that matches
3113 */
3114 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003115 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003116 if (path->slots[0] == 0)
3117 break;
3118 path->slots[0]--;
3119 }
3120
3121 /* pull out the item */
3122 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003123 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3124
3125 /* make sure the item matches what we want */
3126 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3127 break;
3128 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
3129 break;
3130
3131 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003132 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003133
3134 /*
3135 * this is where we are basically btrfs_lookup, without the
3136 * crossing root thing. we store the inode number in the
3137 * offset of the orphan item.
3138 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003139
3140 if (found_key.offset == last_objectid) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003141 btrfs_err(root->fs_info,
3142 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003143 ret = -EINVAL;
3144 goto out;
3145 }
3146
3147 last_objectid = found_key.offset;
3148
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003149 found_key.objectid = found_key.offset;
3150 found_key.type = BTRFS_INODE_ITEM_KEY;
3151 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00003152 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Josef Bacika8c9e572011-09-21 16:55:59 -04003153 ret = PTR_RET(inode);
3154 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003155 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003156
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003157 if (ret == -ESTALE && root == root->fs_info->tree_root) {
3158 struct btrfs_root *dead_root;
3159 struct btrfs_fs_info *fs_info = root->fs_info;
3160 int is_dead_root = 0;
3161
3162 /*
3163 * this is an orphan in the tree root. Currently these
3164 * could come from 2 sources:
3165 * a) a snapshot deletion in progress
3166 * b) a free space cache inode
3167 * We need to distinguish those two, as the snapshot
3168 * orphan must not get deleted.
3169 * find_dead_roots already ran before us, so if this
3170 * is a snapshot deletion, we should find the root
3171 * in the dead_roots list
3172 */
3173 spin_lock(&fs_info->trans_lock);
3174 list_for_each_entry(dead_root, &fs_info->dead_roots,
3175 root_list) {
3176 if (dead_root->root_key.objectid ==
3177 found_key.objectid) {
3178 is_dead_root = 1;
3179 break;
3180 }
3181 }
3182 spin_unlock(&fs_info->trans_lock);
3183 if (is_dead_root) {
3184 /* prevent this orphan from being found again */
3185 key.offset = found_key.objectid - 1;
3186 continue;
3187 }
3188 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003189 /*
3190 * Inode is already gone but the orphan item is still there,
3191 * kill the orphan item.
3192 */
3193 if (ret == -ESTALE) {
3194 trans = btrfs_start_transaction(root, 1);
3195 if (IS_ERR(trans)) {
3196 ret = PTR_ERR(trans);
3197 goto out;
3198 }
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003199 btrfs_debug(root->fs_info, "auto deleting %Lu",
3200 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003201 ret = btrfs_del_orphan_item(trans, root,
3202 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003203 btrfs_end_transaction(trans, root);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003204 if (ret)
3205 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003206 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003207 }
Josef Bacik7b128762008-07-24 12:17:14 -04003208
Josef Bacik7b128762008-07-24 12:17:14 -04003209 /*
3210 * add this inode to the orphan list so btrfs_orphan_del does
3211 * the proper thing when we hit it
3212 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003213 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3214 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003215 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003216
Josef Bacik7b128762008-07-24 12:17:14 -04003217 /* if we have links, this was a truncate, lets do that */
3218 if (inode->i_nlink) {
Josef Bacika41ad392011-01-31 15:30:16 -05003219 if (!S_ISREG(inode->i_mode)) {
3220 WARN_ON(1);
3221 iput(inode);
3222 continue;
3223 }
Josef Bacik7b128762008-07-24 12:17:14 -04003224 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003225
3226 /* 1 for the orphan item deletion. */
3227 trans = btrfs_start_transaction(root, 1);
3228 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003229 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003230 ret = PTR_ERR(trans);
3231 goto out;
3232 }
3233 ret = btrfs_orphan_add(trans, inode);
3234 btrfs_end_transaction(trans, root);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003235 if (ret) {
3236 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003237 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003238 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003239
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003240 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003241 if (ret)
3242 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003243 } else {
3244 nr_unlink++;
3245 }
3246
3247 /* this will do delete_inode and everything for us */
3248 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003249 if (ret)
3250 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003251 }
Miao Xie3254c872011-11-10 20:45:05 -05003252 /* release the path since we're done with it */
3253 btrfs_release_path(path);
3254
Yan, Zhengd68fc572010-05-16 10:49:58 -04003255 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3256
3257 if (root->orphan_block_rsv)
3258 btrfs_block_rsv_release(root, root->orphan_block_rsv,
3259 (u64)-1);
3260
3261 if (root->orphan_block_rsv || root->orphan_item_inserted) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003262 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003263 if (!IS_ERR(trans))
3264 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003265 }
Josef Bacik7b128762008-07-24 12:17:14 -04003266
3267 if (nr_unlink)
David Sterba4884b472013-03-20 13:31:27 +00003268 btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003269 if (nr_truncate)
David Sterba4884b472013-03-20 13:31:27 +00003270 btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003271
3272out:
3273 if (ret)
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003274 btrfs_crit(root->fs_info,
3275 "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003276 btrfs_free_path(path);
3277 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003278}
3279
Chris Masond352ac62008-09-29 15:18:18 -04003280/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003281 * very simple check to peek ahead in the leaf looking for xattrs. If we
3282 * don't find any xattrs, we know there can't be any acls.
3283 *
3284 * slot is the slot the inode is in, objectid is the objectid of the inode
3285 */
3286static noinline int acls_after_inode_item(struct extent_buffer *leaf,
3287 int slot, u64 objectid)
3288{
3289 u32 nritems = btrfs_header_nritems(leaf);
3290 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003291 static u64 xattr_access = 0;
3292 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003293 int scanned = 0;
3294
Josef Bacikf23b5a52013-06-19 10:16:26 -04003295 if (!xattr_access) {
3296 xattr_access = btrfs_name_hash(POSIX_ACL_XATTR_ACCESS,
3297 strlen(POSIX_ACL_XATTR_ACCESS));
3298 xattr_default = btrfs_name_hash(POSIX_ACL_XATTR_DEFAULT,
3299 strlen(POSIX_ACL_XATTR_DEFAULT));
3300 }
3301
Chris Mason46a53cc2009-04-27 11:47:50 -04003302 slot++;
3303 while (slot < nritems) {
3304 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3305
3306 /* we found a different objectid, there must not be acls */
3307 if (found_key.objectid != objectid)
3308 return 0;
3309
3310 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003311 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
3312 if (found_key.offset == xattr_access ||
3313 found_key.offset == xattr_default)
3314 return 1;
3315 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003316
3317 /*
3318 * we found a key greater than an xattr key, there can't
3319 * be any acls later on
3320 */
3321 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3322 return 0;
3323
3324 slot++;
3325 scanned++;
3326
3327 /*
3328 * it goes inode, inode backrefs, xattrs, extents,
3329 * so if there are a ton of hard links to an inode there can
3330 * be a lot of backrefs. Don't waste time searching too hard,
3331 * this is just an optimization
3332 */
3333 if (scanned >= 8)
3334 break;
3335 }
3336 /* we hit the end of the leaf before we found an xattr or
3337 * something larger than an xattr. We have to assume the inode
3338 * has acls
3339 */
3340 return 1;
3341}
3342
3343/*
Chris Masond352ac62008-09-29 15:18:18 -04003344 * read an inode from the btree into the in-memory inode
3345 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003346static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003347{
3348 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003349 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003350 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04003351 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04003352 struct btrfs_root *root = BTRFS_I(inode)->root;
3353 struct btrfs_key location;
Chris Mason46a53cc2009-04-27 11:47:50 -04003354 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003355 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003356 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003357 bool filled = false;
3358
3359 ret = btrfs_fill_inode(inode, &rdev);
3360 if (!ret)
3361 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003362
3363 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07003364 if (!path)
3365 goto make_bad;
3366
Josef Bacikd90c7322011-05-17 09:50:54 -04003367 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003368 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003369
Chris Mason39279cc2007-06-12 06:35:45 -04003370 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003371 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04003372 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04003373
Chris Mason5f39d392007-10-15 16:14:19 -04003374 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003375
3376 if (filled)
3377 goto cache_acl;
3378
Chris Mason5f39d392007-10-15 16:14:19 -04003379 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3380 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003381 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003382 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003383 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3384 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04003385 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003386
3387 tspec = btrfs_inode_atime(inode_item);
3388 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3389 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3390
3391 tspec = btrfs_inode_mtime(inode_item);
3392 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3393 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3394
3395 tspec = btrfs_inode_ctime(inode_item);
3396 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3397 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3398
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003399 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003400 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003401 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3402
3403 /*
3404 * If we were modified in the current generation and evicted from memory
3405 * and then re-read we need to do a full sync since we don't have any
3406 * idea about which extents were modified before we were evicted from
3407 * cache.
3408 */
3409 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
3410 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3411 &BTRFS_I(inode)->runtime_flags);
3412
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003413 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003414 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003415 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003416 rdev = btrfs_inode_rdev(leaf, inode_item);
3417
Josef Bacikaec74772008-07-24 12:12:38 -04003418 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003419 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie2f7e33d2011-06-23 07:27:13 +00003420cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003421 /*
3422 * try to precache a NULL acl entry for files that don't have
3423 * any xattrs or acls
3424 */
Li Zefan33345d012011-04-20 10:31:50 +08003425 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
3426 btrfs_ino(inode));
Al Viro72c04902009-06-24 16:58:48 -04003427 if (!maybe_acls)
3428 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003429
Chris Mason39279cc2007-06-12 06:35:45 -04003430 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003431
Chris Mason39279cc2007-06-12 06:35:45 -04003432 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003433 case S_IFREG:
3434 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003435 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05003436 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003437 inode->i_fop = &btrfs_file_operations;
3438 inode->i_op = &btrfs_file_inode_operations;
3439 break;
3440 case S_IFDIR:
3441 inode->i_fop = &btrfs_dir_file_operations;
3442 if (root == root->fs_info->tree_root)
3443 inode->i_op = &btrfs_dir_ro_inode_operations;
3444 else
3445 inode->i_op = &btrfs_dir_inode_operations;
3446 break;
3447 case S_IFLNK:
3448 inode->i_op = &btrfs_symlink_inode_operations;
3449 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04003450 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003451 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003452 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003453 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003454 init_special_inode(inode, inode->i_mode, rdev);
3455 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003456 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003457
3458 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003459 return;
3460
3461make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003462 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003463 make_bad_inode(inode);
3464}
3465
Chris Masond352ac62008-09-29 15:18:18 -04003466/*
3467 * given a leaf and an inode, copy the inode fields into the leaf
3468 */
Chris Masone02119d2008-09-05 16:13:11 -04003469static void fill_inode_item(struct btrfs_trans_handle *trans,
3470 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003471 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003472 struct inode *inode)
3473{
Liu Bo51fab692012-12-27 09:01:21 +00003474 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003475
Liu Bo51fab692012-12-27 09:01:21 +00003476 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003477
Liu Bo51fab692012-12-27 09:01:21 +00003478 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3479 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3480 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3481 &token);
3482 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3483 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003484
Liu Bo51fab692012-12-27 09:01:21 +00003485 btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item),
3486 inode->i_atime.tv_sec, &token);
3487 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item),
3488 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003489
Liu Bo51fab692012-12-27 09:01:21 +00003490 btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item),
3491 inode->i_mtime.tv_sec, &token);
3492 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item),
3493 inode->i_mtime.tv_nsec, &token);
3494
3495 btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item),
3496 inode->i_ctime.tv_sec, &token);
3497 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item),
3498 inode->i_ctime.tv_nsec, &token);
3499
3500 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3501 &token);
3502 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3503 &token);
3504 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3505 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3506 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3507 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3508 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003509}
3510
Chris Masond352ac62008-09-29 15:18:18 -04003511/*
3512 * copy everything in the in-memory inode into the btree.
3513 */
Chris Mason21151332011-11-10 20:39:08 -05003514static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003515 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003516{
3517 struct btrfs_inode_item *inode_item;
3518 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003519 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003520 int ret;
3521
3522 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003523 if (!path)
3524 return -ENOMEM;
3525
Chris Masonb9473432009-03-13 11:00:37 -04003526 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003527 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3528 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003529 if (ret) {
3530 if (ret > 0)
3531 ret = -ENOENT;
3532 goto failed;
3533 }
3534
Chris Masonb4ce94d2009-02-04 09:25:08 -05003535 btrfs_unlock_up_safe(path, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003536 leaf = path->nodes[0];
3537 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003538 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003539
Chris Masone02119d2008-09-05 16:13:11 -04003540 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003541 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003542 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003543 ret = 0;
3544failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003545 btrfs_free_path(path);
3546 return ret;
3547}
3548
Chris Masond352ac62008-09-29 15:18:18 -04003549/*
Chris Mason21151332011-11-10 20:39:08 -05003550 * copy everything in the in-memory inode into the btree.
3551 */
3552noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3553 struct btrfs_root *root, struct inode *inode)
3554{
3555 int ret;
3556
3557 /*
3558 * If the inode is a free space inode, we can deadlock during commit
3559 * if we put it into the delayed code.
3560 *
3561 * The data relocation inode should also be directly updated
3562 * without delay
3563 */
Liu Bo83eea1f2012-07-10 05:28:39 -06003564 if (!btrfs_is_free_space_inode(inode)
Chris Mason21151332011-11-10 20:39:08 -05003565 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003566 btrfs_update_root_times(trans, root);
3567
Chris Mason21151332011-11-10 20:39:08 -05003568 ret = btrfs_delayed_update_inode(trans, root, inode);
3569 if (!ret)
3570 btrfs_set_inode_last_trans(trans, inode);
3571 return ret;
3572 }
3573
3574 return btrfs_update_inode_item(trans, root, inode);
3575}
3576
Josef Bacikbe6aef62012-10-22 15:43:12 -04003577noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3578 struct btrfs_root *root,
3579 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003580{
3581 int ret;
3582
3583 ret = btrfs_update_inode(trans, root, inode);
3584 if (ret == -ENOSPC)
3585 return btrfs_update_inode_item(trans, root, inode);
3586 return ret;
3587}
3588
3589/*
Chris Masond352ac62008-09-29 15:18:18 -04003590 * unlink helper that gets used here in inode.c and in the tree logging
3591 * recovery code. It remove a link in a directory with a given name, and
3592 * also drops the back refs in the inode to the directory
3593 */
Al Viro92986792011-03-04 17:14:37 +00003594static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3595 struct btrfs_root *root,
3596 struct inode *dir, struct inode *inode,
3597 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003598{
3599 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003600 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003601 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003602 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003603 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003604 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003605 u64 ino = btrfs_ino(inode);
3606 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003607
3608 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003609 if (!path) {
3610 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003611 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003612 }
3613
Chris Masonb9473432009-03-13 11:00:37 -04003614 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003615 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003616 name, name_len, -1);
3617 if (IS_ERR(di)) {
3618 ret = PTR_ERR(di);
3619 goto err;
3620 }
3621 if (!di) {
3622 ret = -ENOENT;
3623 goto err;
3624 }
Chris Mason5f39d392007-10-15 16:14:19 -04003625 leaf = path->nodes[0];
3626 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003627 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003628 if (ret)
3629 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003630 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003631
Li Zefan33345d012011-04-20 10:31:50 +08003632 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3633 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003634 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003635 btrfs_info(root->fs_info,
3636 "failed to delete reference to %.*s, inode %llu parent %llu",
3637 name_len, name,
3638 (unsigned long long)ino, (unsigned long long)dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003639 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003640 goto err;
3641 }
3642
Miao Xie16cdcec2011-04-22 18:12:22 +08003643 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003644 if (ret) {
3645 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003646 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003647 }
Chris Mason39279cc2007-06-12 06:35:45 -04003648
Chris Masone02119d2008-09-05 16:13:11 -04003649 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08003650 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003651 if (ret != 0 && ret != -ENOENT) {
3652 btrfs_abort_transaction(trans, root, ret);
3653 goto err;
3654 }
Chris Masone02119d2008-09-05 16:13:11 -04003655
3656 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
3657 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04003658 if (ret == -ENOENT)
3659 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003660 else if (ret)
3661 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003662err:
3663 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003664 if (ret)
3665 goto out;
3666
3667 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003668 inode_inc_iversion(inode);
3669 inode_inc_iversion(dir);
Chris Masone02119d2008-09-05 16:13:11 -04003670 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Tsutomu Itohb9959292012-06-25 21:25:22 -06003671 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04003672out:
Chris Mason39279cc2007-06-12 06:35:45 -04003673 return ret;
3674}
3675
Al Viro92986792011-03-04 17:14:37 +00003676int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3677 struct btrfs_root *root,
3678 struct inode *dir, struct inode *inode,
3679 const char *name, int name_len)
3680{
3681 int ret;
3682 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3683 if (!ret) {
3684 btrfs_drop_nlink(inode);
3685 ret = btrfs_update_inode(trans, root, inode);
3686 }
3687 return ret;
3688}
Yan, Zhenga22285a2010-05-16 10:48:46 -04003689
3690/*
3691 * helper to start transaction for unlink and rmdir.
3692 *
Josef Bacikd52be812013-05-29 14:54:47 -04003693 * unlink and rmdir are special in btrfs, they do not always free space, so
3694 * if we cannot make our reservations the normal way try and see if there is
3695 * plenty of slack room in the global reserve to migrate, otherwise we cannot
3696 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04003697 */
Josef Bacikd52be812013-05-29 14:54:47 -04003698static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003699{
3700 struct btrfs_trans_handle *trans;
3701 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003702 int ret;
3703
Josef Bacike70bea52011-10-11 14:18:24 -04003704 /*
3705 * 1 for the possible orphan item
3706 * 1 for the dir item
3707 * 1 for the dir index
3708 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04003709 * 1 for the inode
3710 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04003711 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003712 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
3713 return trans;
3714
Josef Bacikd52be812013-05-29 14:54:47 -04003715 if (PTR_ERR(trans) == -ENOSPC) {
3716 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003717
Josef Bacikd52be812013-05-29 14:54:47 -04003718 trans = btrfs_start_transaction(root, 0);
3719 if (IS_ERR(trans))
3720 return trans;
3721 ret = btrfs_cond_migrate_bytes(root->fs_info,
3722 &root->fs_info->trans_block_rsv,
3723 num_bytes, 5);
3724 if (ret) {
3725 btrfs_end_transaction(trans, root);
3726 return ERR_PTR(ret);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003727 }
Josef Bacik5a77d762011-11-01 14:32:23 -04003728 trans->block_rsv = &root->fs_info->trans_block_rsv;
Josef Bacikd52be812013-05-29 14:54:47 -04003729 trans->bytes_reserved = num_bytes;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003730 }
Josef Bacikd52be812013-05-29 14:54:47 -04003731 return trans;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003732}
3733
Chris Mason39279cc2007-06-12 06:35:45 -04003734static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3735{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003736 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003737 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04003738 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003739 int ret;
3740
Josef Bacikd52be812013-05-29 14:54:47 -04003741 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003742 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003743 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003744
Chris Mason12fcfd22009-03-24 10:24:20 -04003745 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
3746
Chris Masone02119d2008-09-05 16:13:11 -04003747 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3748 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003749 if (ret)
3750 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003751
Yan, Zhenga22285a2010-05-16 10:48:46 -04003752 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04003753 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003754 if (ret)
3755 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003756 }
Josef Bacik7b128762008-07-24 12:17:14 -04003757
Tsutomu Itohb5324022011-07-19 07:27:20 +00003758out:
Josef Bacikd52be812013-05-29 14:54:47 -04003759 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003760 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04003761 return ret;
3762}
3763
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003764int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3765 struct btrfs_root *root,
3766 struct inode *dir, u64 objectid,
3767 const char *name, int name_len)
3768{
3769 struct btrfs_path *path;
3770 struct extent_buffer *leaf;
3771 struct btrfs_dir_item *di;
3772 struct btrfs_key key;
3773 u64 index;
3774 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003775 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003776
3777 path = btrfs_alloc_path();
3778 if (!path)
3779 return -ENOMEM;
3780
Li Zefan33345d012011-04-20 10:31:50 +08003781 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003782 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003783 if (IS_ERR_OR_NULL(di)) {
3784 if (!di)
3785 ret = -ENOENT;
3786 else
3787 ret = PTR_ERR(di);
3788 goto out;
3789 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003790
3791 leaf = path->nodes[0];
3792 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3793 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3794 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003795 if (ret) {
3796 btrfs_abort_transaction(trans, root, ret);
3797 goto out;
3798 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003799 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003800
3801 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3802 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003803 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003804 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003805 if (ret != -ENOENT) {
3806 btrfs_abort_transaction(trans, root, ret);
3807 goto out;
3808 }
Li Zefan33345d012011-04-20 10:31:50 +08003809 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003810 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003811 if (IS_ERR_OR_NULL(di)) {
3812 if (!di)
3813 ret = -ENOENT;
3814 else
3815 ret = PTR_ERR(di);
3816 btrfs_abort_transaction(trans, root, ret);
3817 goto out;
3818 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003819
3820 leaf = path->nodes[0];
3821 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003822 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003823 index = key.offset;
3824 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003825 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003826
Miao Xie16cdcec2011-04-22 18:12:22 +08003827 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003828 if (ret) {
3829 btrfs_abort_transaction(trans, root, ret);
3830 goto out;
3831 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003832
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003833 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003834 inode_inc_iversion(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003835 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Josef Bacik5a24e842012-08-08 10:12:59 -06003836 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003837 if (ret)
3838 btrfs_abort_transaction(trans, root, ret);
3839out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04003840 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003841 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003842}
3843
Chris Mason39279cc2007-06-12 06:35:45 -04003844static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3845{
3846 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003847 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003848 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003849 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04003850
David Sterbab3ae2442012-09-13 16:04:34 -06003851 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04003852 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06003853 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
3854 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04003855
Josef Bacikd52be812013-05-29 14:54:47 -04003856 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003857 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003858 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003859
Li Zefan33345d012011-04-20 10:31:50 +08003860 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003861 err = btrfs_unlink_subvol(trans, root, dir,
3862 BTRFS_I(inode)->location.objectid,
3863 dentry->d_name.name,
3864 dentry->d_name.len);
3865 goto out;
3866 }
3867
Josef Bacik7b128762008-07-24 12:17:14 -04003868 err = btrfs_orphan_add(trans, inode);
3869 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003870 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003871
Chris Mason39279cc2007-06-12 06:35:45 -04003872 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04003873 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3874 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05003875 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04003876 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003877out:
Josef Bacikd52be812013-05-29 14:54:47 -04003878 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003879 btrfs_btree_balance_dirty(root);
Chris Mason39544012007-12-12 14:38:19 -05003880
Chris Mason39279cc2007-06-12 06:35:45 -04003881 return err;
3882}
3883
Chris Mason323ac952008-10-01 19:05:46 -04003884/*
Chris Mason39279cc2007-06-12 06:35:45 -04003885 * this can truncate away extent items, csum items and directory items.
3886 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04003887 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04003888 *
3889 * csum items that cross the new i_size are truncated to the new size
3890 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04003891 *
3892 * min_type is the minimum key type to truncate down to. If set to 0, this
3893 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04003894 */
Yan, Zheng80825102009-11-12 09:35:36 +00003895int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
3896 struct btrfs_root *root,
3897 struct inode *inode,
3898 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003899{
Chris Mason39279cc2007-06-12 06:35:45 -04003900 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003901 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003902 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00003903 struct btrfs_key key;
3904 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04003905 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04003906 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003907 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003908 u64 item_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00003909 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04003910 int found_extent;
3911 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003912 int pending_del_nr = 0;
3913 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04003914 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00003915 int ret;
3916 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08003917 u64 ino = btrfs_ino(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003918
3919 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04003920
Mark Fasheh0eb0e192011-07-12 16:44:10 -07003921 path = btrfs_alloc_path();
3922 if (!path)
3923 return -ENOMEM;
3924 path->reada = -1;
3925
Josef Bacik5dc562c2012-08-17 13:14:17 -04003926 /*
3927 * We want to drop from the next block forward in case this new size is
3928 * not block aligned since we will be keeping the last block of the
3929 * extent just the way it is.
3930 */
Josef Bacik0af3d002010-06-21 14:48:16 -04003931 if (root->ref_cows || root == root->fs_info->tree_root)
Qu Wenruofda28322013-02-26 08:10:22 +00003932 btrfs_drop_extent_cache(inode, ALIGN(new_size,
3933 root->sectorsize), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003934
Miao Xie16cdcec2011-04-22 18:12:22 +08003935 /*
3936 * This function is also used to drop the items in the log tree before
3937 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
3938 * it is used to drop the loged items. So we shouldn't kill the delayed
3939 * items.
3940 */
3941 if (min_type == 0 && root == BTRFS_I(inode)->root)
3942 btrfs_kill_delayed_inode_items(inode);
3943
Li Zefan33345d012011-04-20 10:31:50 +08003944 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04003945 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04003946 key.type = (u8)-1;
3947
Chris Mason85e21ba2008-01-29 15:11:36 -05003948search_again:
Chris Masonb9473432009-03-13 11:00:37 -04003949 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05003950 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00003951 if (ret < 0) {
3952 err = ret;
3953 goto out;
3954 }
Chris Masond3977122009-01-05 21:25:51 -05003955
Chris Mason85e21ba2008-01-29 15:11:36 -05003956 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003957 /* there are no items in the tree for us to truncate, we're
3958 * done
3959 */
Yan, Zheng80825102009-11-12 09:35:36 +00003960 if (path->slots[0] == 0)
3961 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05003962 path->slots[0]--;
3963 }
3964
Chris Masond3977122009-01-05 21:25:51 -05003965 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04003966 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04003967 leaf = path->nodes[0];
3968 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3969 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04003970
Li Zefan33345d012011-04-20 10:31:50 +08003971 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04003972 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003973
Chris Mason85e21ba2008-01-29 15:11:36 -05003974 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003975 break;
3976
Chris Mason5f39d392007-10-15 16:14:19 -04003977 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04003978 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04003979 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04003980 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04003981 extent_type = btrfs_file_extent_type(leaf, fi);
3982 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04003983 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04003984 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04003985 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04003986 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04003987 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003988 }
Yan008630c2007-11-07 13:31:09 -05003989 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04003990 }
Yan, Zheng80825102009-11-12 09:35:36 +00003991 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04003992 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00003993 } else {
3994 if (item_end < new_size)
3995 break;
3996 if (found_key.offset >= new_size)
3997 del_item = 1;
3998 else
3999 del_item = 0;
4000 }
Chris Mason39279cc2007-06-12 06:35:45 -04004001 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004002 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004003 if (found_type != BTRFS_EXTENT_DATA_KEY)
4004 goto delete;
4005
4006 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004007 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004008 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004009 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004010 u64 orig_num_bytes =
4011 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004012 extent_num_bytes = ALIGN(new_size -
4013 found_key.offset,
4014 root->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004015 btrfs_set_file_extent_num_bytes(leaf, fi,
4016 extent_num_bytes);
4017 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004018 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04004019 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004020 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004021 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004022 } else {
Chris Masondb945352007-10-15 16:15:53 -04004023 extent_num_bytes =
4024 btrfs_file_extent_disk_num_bytes(leaf,
4025 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004026 extent_offset = found_key.offset -
4027 btrfs_file_extent_offset(leaf, fi);
4028
Chris Mason39279cc2007-06-12 06:35:45 -04004029 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004030 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004031 if (extent_start != 0) {
4032 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04004033 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004034 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004035 }
4036 }
Chris Mason90692182008-02-08 13:49:28 -05004037 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004038 /*
4039 * we can't truncate inline items that have had
4040 * special encodings
4041 */
4042 if (!del_item &&
4043 btrfs_file_extent_compression(leaf, fi) == 0 &&
4044 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4045 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004046 u32 size = new_size - found_key.offset;
4047
4048 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004049 inode_sub_bytes(inode, item_end + 1 -
4050 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04004051 }
4052 size =
4053 btrfs_file_extent_calc_inline_size(size);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004054 btrfs_truncate_item(root, path, size, 1);
Chris Masone02119d2008-09-05 16:13:11 -04004055 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004056 inode_sub_bytes(inode, item_end + 1 -
4057 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05004058 }
Chris Mason39279cc2007-06-12 06:35:45 -04004059 }
Chris Mason179e29e2007-11-01 11:28:41 -04004060delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004061 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004062 if (!pending_del_nr) {
4063 /* no pending yet, add ourselves */
4064 pending_del_slot = path->slots[0];
4065 pending_del_nr = 1;
4066 } else if (pending_del_nr &&
4067 path->slots[0] + 1 == pending_del_slot) {
4068 /* hop on the pending chunk */
4069 pending_del_nr++;
4070 pending_del_slot = path->slots[0];
4071 } else {
Chris Masond3977122009-01-05 21:25:51 -05004072 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004073 }
Chris Mason39279cc2007-06-12 06:35:45 -04004074 } else {
4075 break;
4076 }
Josef Bacik0af3d002010-06-21 14:48:16 -04004077 if (found_extent && (root->ref_cows ||
4078 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004079 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004080 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004081 extent_num_bytes, 0,
4082 btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02004083 ino, extent_offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004084 BUG_ON(ret);
4085 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004086
Yan, Zheng80825102009-11-12 09:35:36 +00004087 if (found_type == BTRFS_INODE_ITEM_KEY)
4088 break;
4089
4090 if (path->slots[0] == 0 ||
4091 path->slots[0] != pending_del_slot) {
Yan, Zheng80825102009-11-12 09:35:36 +00004092 if (pending_del_nr) {
4093 ret = btrfs_del_items(trans, root, path,
4094 pending_del_slot,
4095 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004096 if (ret) {
4097 btrfs_abort_transaction(trans,
4098 root, ret);
4099 goto error;
4100 }
Yan, Zheng80825102009-11-12 09:35:36 +00004101 pending_del_nr = 0;
4102 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004103 btrfs_release_path(path);
Chris Mason85e21ba2008-01-29 15:11:36 -05004104 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004105 } else {
4106 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004107 }
Chris Mason39279cc2007-06-12 06:35:45 -04004108 }
Yan, Zheng80825102009-11-12 09:35:36 +00004109out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004110 if (pending_del_nr) {
4111 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4112 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004113 if (ret)
4114 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004115 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004116error:
Chris Mason39279cc2007-06-12 06:35:45 -04004117 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00004118 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004119}
4120
Chris Masona52d9a82007-08-27 16:49:44 -04004121/*
Josef Bacik2aaa6652012-08-29 14:27:18 -04004122 * btrfs_truncate_page - read, zero a chunk and write a page
4123 * @inode - inode that we're zeroing
4124 * @from - the offset to start zeroing
4125 * @len - the length to zero, 0 to zero the entire range respective to the
4126 * offset
4127 * @front - zero up to the offset instead of from the offset on
4128 *
4129 * This will find the page for the "from" offset and cow the page and zero the
4130 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004131 */
Josef Bacik2aaa6652012-08-29 14:27:18 -04004132int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
4133 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004134{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004135 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04004136 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004137 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4138 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004139 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004140 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04004141 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04004142 pgoff_t index = from >> PAGE_CACHE_SHIFT;
4143 unsigned offset = from & (PAGE_CACHE_SIZE-1);
4144 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004145 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004146 int ret = 0;
4147 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004148 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004149
Josef Bacik2aaa6652012-08-29 14:27:18 -04004150 if ((offset & (blocksize - 1)) == 0 &&
4151 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004152 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004153 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004154 if (ret)
4155 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004156
Chris Mason211c17f2008-05-15 09:13:45 -04004157again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004158 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004159 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004160 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Miao Xieac6a2b32012-12-05 10:56:13 +00004161 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004162 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004163 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004164
4165 page_start = page_offset(page);
4166 page_end = page_start + PAGE_CACHE_SIZE - 1;
4167
Chris Masona52d9a82007-08-27 16:49:44 -04004168 if (!PageUptodate(page)) {
4169 ret = btrfs_readpage(NULL, page);
4170 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004171 if (page->mapping != mapping) {
4172 unlock_page(page);
4173 page_cache_release(page);
4174 goto again;
4175 }
Chris Masona52d9a82007-08-27 16:49:44 -04004176 if (!PageUptodate(page)) {
4177 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004178 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004179 }
4180 }
Chris Mason211c17f2008-05-15 09:13:45 -04004181 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004182
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004183 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004184 set_page_extent_mapped(page);
4185
4186 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4187 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004188 unlock_extent_cached(io_tree, page_start, page_end,
4189 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004190 unlock_page(page);
4191 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004192 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004193 btrfs_put_ordered_extent(ordered);
4194 goto again;
4195 }
4196
Josef Bacik2ac55d42010-02-03 19:33:23 +00004197 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004198 EXTENT_DIRTY | EXTENT_DELALLOC |
4199 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004200 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004201
Josef Bacik2ac55d42010-02-03 19:33:23 +00004202 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
4203 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004204 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004205 unlock_extent_cached(io_tree, page_start, page_end,
4206 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004207 goto out_unlock;
4208 }
4209
Chris Masone6dcd2d2008-07-17 12:53:50 -04004210 if (offset != PAGE_CACHE_SIZE) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004211 if (!len)
4212 len = PAGE_CACHE_SIZE - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004213 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004214 if (front)
4215 memset(kaddr, 0, offset);
4216 else
4217 memset(kaddr + offset, 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004218 flush_dcache_page(page);
4219 kunmap(page);
4220 }
Chris Mason247e7432008-07-17 12:53:51 -04004221 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004222 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004223 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
4224 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004225
Chris Mason89642222008-07-24 09:41:53 -04004226out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004227 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004228 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04004229 unlock_page(page);
4230 page_cache_release(page);
4231out:
4232 return ret;
4233}
4234
Josef Bacik695a0d02011-03-04 15:46:53 -05004235/*
4236 * This function puts in dummy file extents for the area we're creating a hole
4237 * for. So if we are truncating this file to a larger size we need to insert
4238 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4239 * the range between oldsize and size
4240 */
Josef Bacika41ad392011-01-31 15:30:16 -05004241int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004242{
4243 struct btrfs_trans_handle *trans;
4244 struct btrfs_root *root = BTRFS_I(inode)->root;
4245 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004246 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004247 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004248 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Qu Wenruofda28322013-02-26 08:10:22 +00004249 u64 hole_start = ALIGN(oldsize, root->sectorsize);
4250 u64 block_end = ALIGN(size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004251 u64 last_byte;
4252 u64 cur_offset;
4253 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004254 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004255
Josef Bacika71754f2013-06-17 17:14:39 -04004256 /*
4257 * If our size started in the middle of a page we need to zero out the
4258 * rest of the page before we expand the i_size, otherwise we could
4259 * expose stale data.
4260 */
4261 err = btrfs_truncate_page(inode, oldsize, 0, 0);
4262 if (err)
4263 return err;
4264
Yan Zheng9036c102008-10-30 14:19:41 -04004265 if (size <= hole_start)
4266 return 0;
4267
Yan Zheng9036c102008-10-30 14:19:41 -04004268 while (1) {
4269 struct btrfs_ordered_extent *ordered;
4270 btrfs_wait_ordered_range(inode, hole_start,
4271 block_end - hole_start);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004272 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004273 &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04004274 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
4275 if (!ordered)
4276 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004277 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4278 &cached_state, GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004279 btrfs_put_ordered_extent(ordered);
4280 }
4281
Yan Zheng9036c102008-10-30 14:19:41 -04004282 cur_offset = hole_start;
4283 while (1) {
4284 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4285 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004286 if (IS_ERR(em)) {
4287 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004288 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004289 break;
4290 }
Yan Zheng9036c102008-10-30 14:19:41 -04004291 last_byte = min(extent_map_end(em), block_end);
Qu Wenruofda28322013-02-26 08:10:22 +00004292 last_byte = ALIGN(last_byte , root->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004293 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004294 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004295 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004296
Miao Xie36423202011-12-14 20:12:02 -05004297 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004298 if (IS_ERR(trans)) {
4299 err = PTR_ERR(trans);
Chris Mason771ed682008-11-06 22:02:51 -05004300 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004301 }
Yan, Zheng80825102009-11-12 09:35:36 +00004302
Josef Bacik5dc562c2012-08-17 13:14:17 -04004303 err = btrfs_drop_extents(trans, root, inode,
4304 cur_offset,
Josef Bacik26714852012-08-29 12:24:27 -04004305 cur_offset + hole_size, 1);
Miao Xie5b397372011-09-11 10:52:24 -04004306 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004307 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04004308 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05004309 break;
Miao Xie5b397372011-09-11 10:52:24 -04004310 }
Josef Bacik9ed74f22009-09-11 16:12:44 -04004311
Yan Zheng9036c102008-10-30 14:19:41 -04004312 err = btrfs_insert_file_extent(trans, root,
Li Zefan33345d012011-04-20 10:31:50 +08004313 btrfs_ino(inode), cur_offset, 0,
Yan Zheng9036c102008-10-30 14:19:41 -04004314 0, hole_size, 0, hole_size,
4315 0, 0, 0);
Miao Xie5b397372011-09-11 10:52:24 -04004316 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004317 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04004318 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05004319 break;
Miao Xie5b397372011-09-11 10:52:24 -04004320 }
Yan, Zheng80825102009-11-12 09:35:36 +00004321
Josef Bacik5dc562c2012-08-17 13:14:17 -04004322 btrfs_drop_extent_cache(inode, cur_offset,
4323 cur_offset + hole_size - 1, 0);
4324 hole_em = alloc_extent_map();
4325 if (!hole_em) {
4326 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4327 &BTRFS_I(inode)->runtime_flags);
4328 goto next;
4329 }
4330 hole_em->start = cur_offset;
4331 hole_em->len = hole_size;
4332 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004333
Josef Bacik5dc562c2012-08-17 13:14:17 -04004334 hole_em->block_start = EXTENT_MAP_HOLE;
4335 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004336 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004337 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004338 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
4339 hole_em->compress_type = BTRFS_COMPRESS_NONE;
4340 hole_em->generation = trans->transid;
4341
4342 while (1) {
4343 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004344 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004345 write_unlock(&em_tree->lock);
4346 if (err != -EEXIST)
4347 break;
4348 btrfs_drop_extent_cache(inode, cur_offset,
4349 cur_offset +
4350 hole_size - 1, 0);
4351 }
4352 free_extent_map(hole_em);
4353next:
Miao Xie36423202011-12-14 20:12:02 -05004354 btrfs_update_inode(trans, root, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004355 btrfs_end_transaction(trans, root);
Yan Zheng9036c102008-10-30 14:19:41 -04004356 }
4357 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004358 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004359 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004360 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004361 break;
4362 }
4363
Yan, Zhenga22285a2010-05-16 10:48:46 -04004364 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004365 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4366 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004367 return err;
4368}
4369
Eric Sandeen3972f262013-01-12 02:57:22 +00004370static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004371{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004372 struct btrfs_root *root = BTRFS_I(inode)->root;
4373 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004374 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004375 loff_t newsize = attr->ia_size;
4376 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004377 int ret;
4378
Eric Sandeen3972f262013-01-12 02:57:22 +00004379 /*
4380 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4381 * special case where we need to update the times despite not having
4382 * these flags set. For all other operations the VFS set these flags
4383 * explicitly if it wants a timestamp update.
4384 */
4385 if (newsize != oldsize && (!(mask & (ATTR_CTIME | ATTR_MTIME))))
4386 inode->i_ctime = inode->i_mtime = current_fs_time(inode->i_sb);
4387
Josef Bacika41ad392011-01-31 15:30:16 -05004388 if (newsize > oldsize) {
Josef Bacika41ad392011-01-31 15:30:16 -05004389 truncate_pagecache(inode, oldsize, newsize);
4390 ret = btrfs_cont_expand(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004391 if (ret)
Yan, Zheng80825102009-11-12 09:35:36 +00004392 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004393
Miao Xief4a2f4c2011-12-14 20:12:01 -05004394 trans = btrfs_start_transaction(root, 1);
4395 if (IS_ERR(trans))
4396 return PTR_ERR(trans);
4397
4398 i_size_write(inode, newsize);
4399 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
4400 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004401 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05004402 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004403
Josef Bacika41ad392011-01-31 15:30:16 -05004404 /*
4405 * We're truncating a file that used to have good data down to
4406 * zero. Make sure it gets into the ordered flush list so that
4407 * any new writes get down to disk quickly.
4408 */
4409 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004410 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4411 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004412
Josef Bacikf3fe8202013-01-07 17:03:21 -05004413 /*
4414 * 1 for the orphan item we're going to add
4415 * 1 for the orphan item deletion.
4416 */
4417 trans = btrfs_start_transaction(root, 2);
4418 if (IS_ERR(trans))
4419 return PTR_ERR(trans);
4420
4421 /*
4422 * We need to do this in case we fail at _any_ point during the
4423 * actual truncate. Once we do the truncate_setsize we could
4424 * invalidate pages which forces any outstanding ordered io to
4425 * be instantly completed which will give us extents that need
4426 * to be truncated. If we fail to get an orphan inode down we
4427 * could have left over extents that were never meant to live,
4428 * so we need to garuntee from this point on that everything
4429 * will be consistent.
4430 */
4431 ret = btrfs_orphan_add(trans, inode);
4432 btrfs_end_transaction(trans, root);
4433 if (ret)
4434 return ret;
4435
Josef Bacika41ad392011-01-31 15:30:16 -05004436 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4437 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004438
4439 /* Disable nonlocked read DIO to avoid the end less truncate */
4440 btrfs_inode_block_unlocked_dio(inode);
4441 inode_dio_wait(inode);
4442 btrfs_inode_resume_unlocked_dio(inode);
4443
Josef Bacika41ad392011-01-31 15:30:16 -05004444 ret = btrfs_truncate(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05004445 if (ret && inode->i_nlink)
4446 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004447 }
4448
Josef Bacika41ad392011-01-31 15:30:16 -05004449 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004450}
4451
Chris Mason39279cc2007-06-12 06:35:45 -04004452static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
4453{
4454 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08004455 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004456 int err;
4457
Li Zefanb83cc962010-12-20 16:04:08 +08004458 if (btrfs_root_readonly(root))
4459 return -EROFS;
4460
Chris Mason39279cc2007-06-12 06:35:45 -04004461 err = inode_change_ok(inode, attr);
4462 if (err)
4463 return err;
4464
Chris Mason5a3f23d2009-03-31 13:27:11 -04004465 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00004466 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00004467 if (err)
4468 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004469 }
Yan Zheng9036c102008-10-30 14:19:41 -04004470
Christoph Hellwig10257742010-06-04 11:30:02 +02004471 if (attr->ia_valid) {
4472 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004473 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004474 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04004475
Josef Bacik22c44fe2011-11-30 10:45:38 -05004476 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig10257742010-06-04 11:30:02 +02004477 err = btrfs_acl_chmod(inode);
4478 }
4479
Chris Mason39279cc2007-06-12 06:35:45 -04004480 return err;
4481}
Chris Mason61295eb2008-01-14 16:24:38 -05004482
Al Virobd555972010-06-07 11:35:40 -04004483void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004484{
4485 struct btrfs_trans_handle *trans;
4486 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04004487 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik07127182011-08-19 10:29:59 -04004488 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04004489 int ret;
4490
liubo1abe9b82011-03-24 11:18:59 +00004491 trace_btrfs_inode_evict(inode);
4492
Chris Mason39279cc2007-06-12 06:35:45 -04004493 truncate_inode_pages(&inode->i_data, 0);
Josef Bacik0af3d002010-06-21 14:48:16 -04004494 if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 ||
Liu Bo83eea1f2012-07-10 05:28:39 -06004495 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04004496 goto no_delete;
4497
Chris Mason39279cc2007-06-12 06:35:45 -04004498 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04004499 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004500 goto no_delete;
4501 }
Al Virobd555972010-06-07 11:35:40 -04004502 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04004503 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04004504
Yan, Zhengc71bf092009-11-12 09:34:40 +00004505 if (root->fs_info->log_root_recovering) {
Liu Bo6bf02312012-06-25 21:59:09 -06004506 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04004507 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00004508 goto no_delete;
4509 }
4510
Yan, Zheng76dda932009-09-21 16:00:26 -04004511 if (inode->i_nlink > 0) {
4512 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
4513 goto no_delete;
4514 }
4515
Miao Xie0e8c36a2012-12-19 06:59:51 +00004516 ret = btrfs_commit_inode_delayed_inode(inode);
4517 if (ret) {
4518 btrfs_orphan_del(NULL, inode);
4519 goto no_delete;
4520 }
4521
Miao Xie66d8f3d2012-09-06 04:02:28 -06004522 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04004523 if (!rsv) {
4524 btrfs_orphan_del(NULL, inode);
4525 goto no_delete;
4526 }
Josef Bacik4a338542011-08-29 11:01:31 -04004527 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04004528 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04004529 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04004530
Chris Masondbe674a2008-07-17 12:54:05 -04004531 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04004532
Josef Bacik4289a662011-08-05 13:22:24 -04004533 /*
Miao Xie8407aa42012-09-07 01:43:32 -06004534 * This is a bit simpler than btrfs_truncate since we've already
4535 * reserved our space for our orphan item in the unlink, so we just
4536 * need to reserve some slack space in case we add bytes and update
4537 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04004538 */
Yan, Zheng80825102009-11-12 09:35:36 +00004539 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00004540 ret = btrfs_block_rsv_refill(root, rsv, min_size,
4541 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00004542
Josef Bacik726c35f2011-09-26 15:46:06 -04004543 /*
4544 * Try and steal from the global reserve since we will
4545 * likely not use this space anyway, we want to try as
4546 * hard as possible to get this to work.
4547 */
4548 if (ret)
4549 ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
4550
Yan, Zhengd68fc572010-05-16 10:49:58 -04004551 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004552 btrfs_warn(root->fs_info,
4553 "Could not get space for a delete, will truncate on mount %d",
4554 ret);
Josef Bacik4289a662011-08-05 13:22:24 -04004555 btrfs_orphan_del(NULL, inode);
4556 btrfs_free_block_rsv(root, rsv);
4557 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04004558 }
4559
Miao Xie0e8c36a2012-12-19 06:59:51 +00004560 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04004561 if (IS_ERR(trans)) {
4562 btrfs_orphan_del(NULL, inode);
4563 btrfs_free_block_rsv(root, rsv);
4564 goto no_delete;
4565 }
4566
4567 trans->block_rsv = rsv;
4568
Yan, Zhengd68fc572010-05-16 10:49:58 -04004569 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04004570 if (ret != -ENOSPC)
Yan, Zheng80825102009-11-12 09:35:36 +00004571 break;
4572
Miao Xie8407aa42012-09-07 01:43:32 -06004573 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004574 btrfs_end_transaction(trans, root);
4575 trans = NULL;
Liu Bob53d3f52012-11-14 14:34:34 +00004576 btrfs_btree_balance_dirty(root);
Josef Bacik7b128762008-07-24 12:17:14 -04004577 }
4578
Josef Bacik4289a662011-08-05 13:22:24 -04004579 btrfs_free_block_rsv(root, rsv);
4580
Josef Bacik4ef31a42013-08-13 14:10:08 -04004581 /*
4582 * Errors here aren't a big deal, it just means we leave orphan items
4583 * in the tree. They will be cleaned up on the next mount.
4584 */
Yan, Zheng80825102009-11-12 09:35:36 +00004585 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04004586 trans->block_rsv = root->orphan_block_rsv;
Josef Bacik4ef31a42013-08-13 14:10:08 -04004587 btrfs_orphan_del(trans, inode);
4588 } else {
4589 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004590 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004591
Josef Bacik4289a662011-08-05 13:22:24 -04004592 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08004593 if (!(root == root->fs_info->tree_root ||
4594 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08004595 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08004596
Chris Mason54aa1f42007-06-22 14:16:25 -04004597 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004598 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04004599no_delete:
Miao Xie89042e52013-05-15 07:48:15 +00004600 btrfs_remove_delayed_node(inode);
Jan Karadbd57682012-05-03 14:48:02 +02004601 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004602 return;
Chris Mason39279cc2007-06-12 06:35:45 -04004603}
4604
4605/*
4606 * this returns the key found in the dir entry in the location pointer.
4607 * If no dir entries were found, location->objectid is 0.
4608 */
4609static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
4610 struct btrfs_key *location)
4611{
4612 const char *name = dentry->d_name.name;
4613 int namelen = dentry->d_name.len;
4614 struct btrfs_dir_item *di;
4615 struct btrfs_path *path;
4616 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04004617 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004618
4619 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07004620 if (!path)
4621 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05004622
Li Zefan33345d012011-04-20 10:31:50 +08004623 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04004624 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04004625 if (IS_ERR(di))
4626 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05004627
David Sterbac7040052011-04-19 18:00:01 +02004628 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05004629 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05004630
Chris Mason5f39d392007-10-15 16:14:19 -04004631 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04004632out:
Chris Mason39279cc2007-06-12 06:35:45 -04004633 btrfs_free_path(path);
4634 return ret;
Chris Mason39544012007-12-12 14:38:19 -05004635out_err:
4636 location->objectid = 0;
4637 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04004638}
4639
4640/*
4641 * when we hit a tree root in a directory, the btrfs part of the inode
4642 * needs to be changed to reflect the root directory of the tree root. This
4643 * is kind of like crossing a mount point.
4644 */
4645static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004646 struct inode *dir,
4647 struct dentry *dentry,
4648 struct btrfs_key *location,
4649 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04004650{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004651 struct btrfs_path *path;
4652 struct btrfs_root *new_root;
4653 struct btrfs_root_ref *ref;
4654 struct extent_buffer *leaf;
4655 int ret;
4656 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004657
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004658 path = btrfs_alloc_path();
4659 if (!path) {
4660 err = -ENOMEM;
4661 goto out;
4662 }
Chris Mason39279cc2007-06-12 06:35:45 -04004663
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004664 err = -ENOENT;
4665 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
4666 BTRFS_I(dir)->root->root_key.objectid,
4667 location->objectid);
4668 if (ret) {
4669 if (ret < 0)
4670 err = ret;
4671 goto out;
4672 }
Chris Mason39279cc2007-06-12 06:35:45 -04004673
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004674 leaf = path->nodes[0];
4675 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08004676 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004677 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
4678 goto out;
4679
4680 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
4681 (unsigned long)(ref + 1),
4682 dentry->d_name.len);
4683 if (ret)
4684 goto out;
4685
David Sterbab3b4aa72011-04-21 01:20:15 +02004686 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004687
4688 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
4689 if (IS_ERR(new_root)) {
4690 err = PTR_ERR(new_root);
4691 goto out;
4692 }
4693
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004694 *sub_root = new_root;
4695 location->objectid = btrfs_root_dirid(&new_root->root_item);
4696 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04004697 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004698 err = 0;
4699out:
4700 btrfs_free_path(path);
4701 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004702}
4703
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004704static void inode_tree_add(struct inode *inode)
4705{
4706 struct btrfs_root *root = BTRFS_I(inode)->root;
4707 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004708 struct rb_node **p;
4709 struct rb_node *parent;
Li Zefan33345d012011-04-20 10:31:50 +08004710 u64 ino = btrfs_ino(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004711
Al Viro1d3382cb2010-10-23 15:19:20 -04004712 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004713 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00004714again:
4715 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004716 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00004717 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004718 while (*p) {
4719 parent = *p;
4720 entry = rb_entry(parent, struct btrfs_inode, rb_node);
4721
Li Zefan33345d012011-04-20 10:31:50 +08004722 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004723 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004724 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004725 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004726 else {
4727 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04004728 (I_WILL_FREE | I_FREEING)));
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004729 rb_erase(parent, &root->inode_tree);
4730 RB_CLEAR_NODE(parent);
4731 spin_unlock(&root->inode_lock);
4732 goto again;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004733 }
4734 }
4735 rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
4736 rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
4737 spin_unlock(&root->inode_lock);
4738}
4739
4740static void inode_tree_del(struct inode *inode)
4741{
4742 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04004743 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004744
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004745 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004746 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004747 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004748 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04004749 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004750 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004751 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04004752
Josef Bacik0af3d002010-06-21 14:48:16 -04004753 /*
4754 * Free space cache has inodes in the tree root, but the tree root has a
4755 * root_refs of 0, so this could end up dropping the tree root as a
4756 * snapshot, so we need the extra !root->fs_info->tree_root check to
4757 * make sure we don't drop it.
4758 */
4759 if (empty && btrfs_root_refs(&root->root_item) == 0 &&
4760 root != root->fs_info->tree_root) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004761 synchronize_srcu(&root->fs_info->subvol_srcu);
4762 spin_lock(&root->inode_lock);
4763 empty = RB_EMPTY_ROOT(&root->inode_tree);
4764 spin_unlock(&root->inode_lock);
4765 if (empty)
4766 btrfs_add_dead_root(root);
4767 }
4768}
4769
Jeff Mahoney143bede2012-03-01 14:56:26 +01004770void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04004771{
4772 struct rb_node *node;
4773 struct rb_node *prev;
4774 struct btrfs_inode *entry;
4775 struct inode *inode;
4776 u64 objectid = 0;
4777
4778 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4779
4780 spin_lock(&root->inode_lock);
4781again:
4782 node = root->inode_tree.rb_node;
4783 prev = NULL;
4784 while (node) {
4785 prev = node;
4786 entry = rb_entry(node, struct btrfs_inode, rb_node);
4787
Li Zefan33345d012011-04-20 10:31:50 +08004788 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004789 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004790 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004791 node = node->rb_right;
4792 else
4793 break;
4794 }
4795 if (!node) {
4796 while (prev) {
4797 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004798 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004799 node = prev;
4800 break;
4801 }
4802 prev = rb_next(prev);
4803 }
4804 }
4805 while (node) {
4806 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004807 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04004808 inode = igrab(&entry->vfs_inode);
4809 if (inode) {
4810 spin_unlock(&root->inode_lock);
4811 if (atomic_read(&inode->i_count) > 1)
4812 d_prune_aliases(inode);
4813 /*
Al Viro45321ac2010-06-07 13:43:19 -04004814 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04004815 * the inode cache when its usage count
4816 * hits zero.
4817 */
4818 iput(inode);
4819 cond_resched();
4820 spin_lock(&root->inode_lock);
4821 goto again;
4822 }
4823
4824 if (cond_resched_lock(&root->inode_lock))
4825 goto again;
4826
4827 node = rb_next(node);
4828 }
4829 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004830}
4831
Chris Masone02119d2008-09-05 16:13:11 -04004832static int btrfs_init_locked_inode(struct inode *inode, void *p)
4833{
4834 struct btrfs_iget_args *args = p;
4835 inode->i_ino = args->ino;
Chris Masone02119d2008-09-05 16:13:11 -04004836 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004837 return 0;
4838}
4839
4840static int btrfs_find_actor(struct inode *inode, void *opaque)
4841{
4842 struct btrfs_iget_args *args = opaque;
Li Zefan33345d012011-04-20 10:31:50 +08004843 return args->ino == btrfs_ino(inode) &&
Chris Masond3977122009-01-05 21:25:51 -05004844 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004845}
4846
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004847static struct inode *btrfs_iget_locked(struct super_block *s,
4848 u64 objectid,
4849 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04004850{
4851 struct inode *inode;
4852 struct btrfs_iget_args args;
4853 args.ino = objectid;
4854 args.root = root;
4855
4856 inode = iget5_locked(s, objectid, btrfs_find_actor,
4857 btrfs_init_locked_inode,
4858 (void *)&args);
4859 return inode;
4860}
4861
Balaji Rao1a54ef82008-07-21 02:01:04 +05304862/* Get an inode object given its location and corresponding root.
4863 * Returns in *is_new if the inode was read from disk
4864 */
4865struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00004866 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05304867{
4868 struct inode *inode;
4869
4870 inode = btrfs_iget_locked(s, location->objectid, root);
4871 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004872 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05304873
4874 if (inode->i_state & I_NEW) {
4875 BTRFS_I(inode)->root = root;
4876 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
4877 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07004878 if (!is_bad_inode(inode)) {
4879 inode_tree_add(inode);
4880 unlock_new_inode(inode);
4881 if (new)
4882 *new = 1;
4883 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04004884 unlock_new_inode(inode);
4885 iput(inode);
4886 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07004887 }
4888 }
4889
Balaji Rao1a54ef82008-07-21 02:01:04 +05304890 return inode;
4891}
4892
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004893static struct inode *new_simple_dir(struct super_block *s,
4894 struct btrfs_key *key,
4895 struct btrfs_root *root)
4896{
4897 struct inode *inode = new_inode(s);
4898
4899 if (!inode)
4900 return ERR_PTR(-ENOMEM);
4901
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004902 BTRFS_I(inode)->root = root;
4903 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04004904 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004905
4906 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08004907 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004908 inode->i_fop = &simple_dir_operations;
4909 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
4910 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
4911
4912 return inode;
4913}
4914
Chris Mason3de45862008-11-17 21:02:50 -05004915struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04004916{
Chris Masond3977122009-01-05 21:25:51 -05004917 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004918 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004919 struct btrfs_root *sub_root = root;
4920 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04004921 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004922 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004923
4924 if (dentry->d_name.len > BTRFS_NAME_LEN)
4925 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04004926
Jeff Layton39e3c952012-11-28 11:30:53 -05004927 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04004928 if (ret < 0)
4929 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04004930
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004931 if (location.objectid == 0)
4932 return NULL;
4933
4934 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00004935 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004936 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004937 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004938
4939 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
4940
Yan, Zheng76dda932009-09-21 16:00:26 -04004941 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004942 ret = fixup_tree_root_location(root, dir, dentry,
4943 &location, &sub_root);
4944 if (ret < 0) {
4945 if (ret != -ENOENT)
4946 inode = ERR_PTR(ret);
4947 else
4948 inode = new_simple_dir(dir->i_sb, &location, sub_root);
4949 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00004950 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004951 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004952 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
4953
Julia Lawall34d19ba2011-01-24 19:55:19 +00004954 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00004955 down_read(&root->fs_info->cleanup_work_sem);
4956 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004957 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00004958 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04004959 if (ret) {
4960 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004961 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04004962 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00004963 }
4964
Chris Mason3de45862008-11-17 21:02:50 -05004965 return inode;
4966}
4967
Nick Pigginfe15ce42011-01-07 17:49:23 +11004968static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04004969{
4970 struct btrfs_root *root;
Li Zefan848cce02012-02-21 17:04:28 +08004971 struct inode *inode = dentry->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04004972
Li Zefan848cce02012-02-21 17:04:28 +08004973 if (!inode && !IS_ROOT(dentry))
4974 inode = dentry->d_parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04004975
Li Zefan848cce02012-02-21 17:04:28 +08004976 if (inode) {
4977 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04004978 if (btrfs_root_refs(&root->root_item) == 0)
4979 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08004980
4981 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
4982 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04004983 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004984 return 0;
4985}
4986
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004987static void btrfs_dentry_release(struct dentry *dentry)
4988{
4989 if (dentry->d_fsdata)
4990 kfree(dentry->d_fsdata);
4991}
4992
Chris Mason3de45862008-11-17 21:02:50 -05004993static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04004994 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05004995{
Josef Bacika66e7cc2011-09-18 10:34:03 -04004996 struct dentry *ret;
4997
4998 ret = d_splice_alias(btrfs_lookup_dentry(dir, dentry), dentry);
Josef Bacika66e7cc2011-09-18 10:34:03 -04004999 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005000}
5001
Miao Xie16cdcec2011-04-22 18:12:22 +08005002unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005003 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5004};
5005
Al Viro9cdda8d2013-05-22 16:48:09 -04005006static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005007{
Al Viro9cdda8d2013-05-22 16:48:09 -04005008 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005009 struct btrfs_root *root = BTRFS_I(inode)->root;
5010 struct btrfs_item *item;
5011 struct btrfs_dir_item *di;
5012 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005013 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005014 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005015 struct list_head ins_list;
5016 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005017 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005018 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005019 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005020 unsigned char d_type;
5021 int over = 0;
5022 u32 di_cur;
5023 u32 di_total;
5024 u32 di_len;
5025 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005026 char tmp_name[32];
5027 char *name_ptr;
5028 int name_len;
Al Viro9cdda8d2013-05-22 16:48:09 -04005029 int is_curr = 0; /* ctx->pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04005030
5031 /* FIXME, use a real flag for deciding about the key type */
5032 if (root->fs_info->tree_root == root)
5033 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005034
Al Viro9cdda8d2013-05-22 16:48:09 -04005035 if (!dir_emit_dots(file, ctx))
5036 return 0;
5037
David Woodhouse49593bf2008-08-17 17:08:36 +01005038 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005039 if (!path)
5040 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005041
Josef Bacik026fd312011-05-13 10:32:11 -04005042 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005043
Miao Xie16cdcec2011-04-22 18:12:22 +08005044 if (key_type == BTRFS_DIR_INDEX_KEY) {
5045 INIT_LIST_HEAD(&ins_list);
5046 INIT_LIST_HEAD(&del_list);
5047 btrfs_get_delayed_items(inode, &ins_list, &del_list);
5048 }
5049
Chris Mason39279cc2007-06-12 06:35:45 -04005050 btrfs_set_key_type(&key, key_type);
Al Viro9cdda8d2013-05-22 16:48:09 -04005051 key.offset = ctx->pos;
Li Zefan33345d012011-04-20 10:31:50 +08005052 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005053
Chris Mason39279cc2007-06-12 06:35:45 -04005054 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5055 if (ret < 0)
5056 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005057
5058 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005059 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005060 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005061 if (slot >= btrfs_header_nritems(leaf)) {
5062 ret = btrfs_next_leaf(root, path);
5063 if (ret < 0)
5064 goto err;
5065 else if (ret > 0)
5066 break;
5067 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005068 }
Chris Mason3de45862008-11-17 21:02:50 -05005069
Chris Mason5f39d392007-10-15 16:14:19 -04005070 item = btrfs_item_nr(leaf, slot);
5071 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5072
5073 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005074 break;
Chris Mason5f39d392007-10-15 16:14:19 -04005075 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04005076 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005077 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005078 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08005079 if (key_type == BTRFS_DIR_INDEX_KEY &&
5080 btrfs_should_delete_dir_index(&del_list,
5081 found_key.offset))
5082 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005083
Al Viro9cdda8d2013-05-22 16:48:09 -04005084 ctx->pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08005085 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005086
Chris Mason39279cc2007-06-12 06:35:45 -04005087 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
5088 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005089 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01005090
5091 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04005092 struct btrfs_key location;
5093
Josef Bacik22a94d42011-03-16 16:47:17 -04005094 if (verify_dir_item(root, leaf, di))
5095 break;
5096
Chris Mason5f39d392007-10-15 16:14:19 -04005097 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01005098 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04005099 name_ptr = tmp_name;
5100 } else {
5101 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01005102 if (!name_ptr) {
5103 ret = -ENOMEM;
5104 goto err;
5105 }
Chris Mason5f39d392007-10-15 16:14:19 -04005106 }
5107 read_extent_buffer(leaf, name_ptr,
5108 (unsigned long)(di + 1), name_len);
5109
5110 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5111 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05005112
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005113
Chris Mason3de45862008-11-17 21:02:50 -05005114 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01005115 * skip it.
5116 *
5117 * In contrast to old kernels, we insert the snapshot's
5118 * dir item and dir index after it has been created, so
5119 * we won't find a reference to our own snapshot. We
5120 * still keep the following code for backward
5121 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05005122 */
5123 if (location.type == BTRFS_ROOT_ITEM_KEY &&
5124 location.objectid == root->root_key.objectid) {
5125 over = 0;
5126 goto skip;
5127 }
Al Viro9cdda8d2013-05-22 16:48:09 -04005128 over = !dir_emit(ctx, name_ptr, name_len,
5129 location.objectid, d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04005130
Chris Mason3de45862008-11-17 21:02:50 -05005131skip:
Chris Mason5f39d392007-10-15 16:14:19 -04005132 if (name_ptr != tmp_name)
5133 kfree(name_ptr);
5134
Chris Mason39279cc2007-06-12 06:35:45 -04005135 if (over)
5136 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05005137 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01005138 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04005139 di_cur += di_len;
5140 di = (struct btrfs_dir_item *)((char *)di + di_len);
5141 }
Li Zefanb9e03af2011-03-23 10:43:58 +08005142next:
5143 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005144 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005145
Miao Xie16cdcec2011-04-22 18:12:22 +08005146 if (key_type == BTRFS_DIR_INDEX_KEY) {
5147 if (is_curr)
Al Viro9cdda8d2013-05-22 16:48:09 -04005148 ctx->pos++;
5149 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005150 if (ret)
5151 goto nopos;
5152 }
5153
David Woodhouse49593bf2008-08-17 17:08:36 +01005154 /* Reached end of directory/root. Bump pos past the last item. */
Zach Browndb62efb2013-07-11 16:19:42 -07005155 ctx->pos++;
5156
5157 /*
5158 * Stop new entries from being returned after we return the last
5159 * entry.
5160 *
5161 * New directory entries are assigned a strictly increasing
5162 * offset. This means that new entries created during readdir
5163 * are *guaranteed* to be seen in the future by that readdir.
5164 * This has broken buggy programs which operate on names as
5165 * they're returned by readdir. Until we re-use freed offsets
5166 * we have this hack to stop new entries from being returned
5167 * under the assumption that they'll never reach this huge
5168 * offset.
5169 *
5170 * This is being careful not to overflow 32bit loff_t unless the
5171 * last entry requires it because doing so has broken 32bit apps
5172 * in the past.
5173 */
5174 if (key_type == BTRFS_DIR_INDEX_KEY) {
5175 if (ctx->pos >= INT_MAX)
5176 ctx->pos = LLONG_MAX;
5177 else
5178 ctx->pos = INT_MAX;
5179 }
Chris Mason39279cc2007-06-12 06:35:45 -04005180nopos:
5181 ret = 0;
5182err:
Miao Xie16cdcec2011-04-22 18:12:22 +08005183 if (key_type == BTRFS_DIR_INDEX_KEY)
5184 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005185 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005186 return ret;
5187}
5188
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005189int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005190{
5191 struct btrfs_root *root = BTRFS_I(inode)->root;
5192 struct btrfs_trans_handle *trans;
5193 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005194 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005195
Josef Bacik72ac3c02012-05-23 14:13:11 -04005196 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005197 return 0;
5198
Liu Bo83eea1f2012-07-10 05:28:39 -06005199 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08005200 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005201
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005202 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005203 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005204 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005205 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005206 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005207 if (IS_ERR(trans))
5208 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06005209 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005210 }
5211 return ret;
5212}
5213
5214/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005215 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005216 * inode changes. But, it is most likely to find the inode in cache.
5217 * FIXME, needs more benchmarking...there are no reasons other than performance
5218 * to keep or drop this code.
5219 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005220static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005221{
5222 struct btrfs_root *root = BTRFS_I(inode)->root;
5223 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005224 int ret;
5225
Josef Bacik72ac3c02012-05-23 14:13:11 -04005226 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005227 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005228
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005229 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005230 if (IS_ERR(trans))
5231 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005232
5233 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005234 if (ret && ret == -ENOSPC) {
5235 /* whoops, lets try again with the full transaction */
5236 btrfs_end_transaction(trans, root);
5237 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005238 if (IS_ERR(trans))
5239 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005240
Chris Mason94b60442010-05-26 11:02:00 -04005241 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005242 }
Chris Mason39279cc2007-06-12 06:35:45 -04005243 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08005244 if (BTRFS_I(inode)->delayed_node)
5245 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005246
5247 return ret;
5248}
5249
5250/*
5251 * This is a copy of file_update_time. We need this so we can return error on
5252 * ENOSPC for updating the inode in the case of file write and mmap writes.
5253 */
Josef Bacike41f9412012-03-26 09:46:47 -04005254static int btrfs_update_time(struct inode *inode, struct timespec *now,
5255 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005256{
Alexander Block2bc5565282012-06-15 09:49:33 +02005257 struct btrfs_root *root = BTRFS_I(inode)->root;
5258
5259 if (btrfs_root_readonly(root))
5260 return -EROFS;
5261
Josef Bacike41f9412012-03-26 09:46:47 -04005262 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005263 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04005264 if (flags & S_CTIME)
5265 inode->i_ctime = *now;
5266 if (flags & S_MTIME)
5267 inode->i_mtime = *now;
5268 if (flags & S_ATIME)
5269 inode->i_atime = *now;
5270 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005271}
5272
Chris Masond352ac62008-09-29 15:18:18 -04005273/*
5274 * find the highest existing sequence number in a directory
5275 * and then set the in-memory index_cnt variable to reflect
5276 * free sequence numbers
5277 */
Josef Bacikaec74772008-07-24 12:12:38 -04005278static int btrfs_set_inode_index_count(struct inode *inode)
5279{
5280 struct btrfs_root *root = BTRFS_I(inode)->root;
5281 struct btrfs_key key, found_key;
5282 struct btrfs_path *path;
5283 struct extent_buffer *leaf;
5284 int ret;
5285
Li Zefan33345d012011-04-20 10:31:50 +08005286 key.objectid = btrfs_ino(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005287 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
5288 key.offset = (u64)-1;
5289
5290 path = btrfs_alloc_path();
5291 if (!path)
5292 return -ENOMEM;
5293
5294 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5295 if (ret < 0)
5296 goto out;
5297 /* FIXME: we should be able to handle this */
5298 if (ret == 0)
5299 goto out;
5300 ret = 0;
5301
5302 /*
5303 * MAGIC NUMBER EXPLANATION:
5304 * since we search a directory based on f_pos we have to start at 2
5305 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5306 * else has to start at 2
5307 */
5308 if (path->slots[0] == 0) {
5309 BTRFS_I(inode)->index_cnt = 2;
5310 goto out;
5311 }
5312
5313 path->slots[0]--;
5314
5315 leaf = path->nodes[0];
5316 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5317
Li Zefan33345d012011-04-20 10:31:50 +08005318 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacikaec74772008-07-24 12:12:38 -04005319 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
5320 BTRFS_I(inode)->index_cnt = 2;
5321 goto out;
5322 }
5323
5324 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
5325out:
5326 btrfs_free_path(path);
5327 return ret;
5328}
5329
Chris Masond352ac62008-09-29 15:18:18 -04005330/*
5331 * helper to find a free sequence number in a given directory. This current
5332 * code is very simple, later versions will do smarter things in the btree
5333 */
Chris Mason3de45862008-11-17 21:02:50 -05005334int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04005335{
5336 int ret = 0;
5337
5338 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08005339 ret = btrfs_inode_delayed_dir_index_count(dir);
5340 if (ret) {
5341 ret = btrfs_set_inode_index_count(dir);
5342 if (ret)
5343 return ret;
5344 }
Josef Bacikaec74772008-07-24 12:12:38 -04005345 }
5346
Chris Mason00e4e6b2008-08-05 11:18:09 -04005347 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04005348 BTRFS_I(dir)->index_cnt++;
5349
5350 return ret;
5351}
5352
Chris Mason39279cc2007-06-12 06:35:45 -04005353static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5354 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04005355 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05005356 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04005357 u64 ref_objectid, u64 objectid,
5358 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04005359{
5360 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005361 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04005362 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04005363 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05005364 struct btrfs_inode_ref *ref;
5365 struct btrfs_key key[2];
5366 u32 sizes[2];
5367 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04005368 int ret;
5369 int owner;
5370
Chris Mason5f39d392007-10-15 16:14:19 -04005371 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005372 if (!path)
5373 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04005374
Chris Mason39279cc2007-06-12 06:35:45 -04005375 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005376 if (!inode) {
5377 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005378 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005379 }
Chris Mason39279cc2007-06-12 06:35:45 -04005380
Li Zefan581bb052011-04-20 10:06:11 +08005381 /*
5382 * we have to initialize this early, so we can reclaim the inode
5383 * number if we fail afterwards in this function.
5384 */
5385 inode->i_ino = objectid;
5386
Josef Bacikaec74772008-07-24 12:12:38 -04005387 if (dir) {
liubo1abe9b82011-03-24 11:18:59 +00005388 trace_btrfs_inode_request(dir);
5389
Chris Mason3de45862008-11-17 21:02:50 -05005390 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04005391 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005392 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005393 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005394 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04005395 }
Josef Bacikaec74772008-07-24 12:12:38 -04005396 }
5397 /*
5398 * index_cnt is ignored for everything but a dir,
5399 * btrfs_get_inode_index_count has an explanation for the magic
5400 * number
5401 */
5402 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04005403 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04005404 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00005405 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04005406
Josef Bacik5dc562c2012-08-17 13:14:17 -04005407 /*
5408 * We could have gotten an inode number from somebody who was fsynced
5409 * and then removed in this same transaction, so let's just set full
5410 * sync since it will be a full sync anyway and this will blow away the
5411 * old info in the log.
5412 */
5413 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
5414
Al Viro569254b2011-07-24 17:08:40 -04005415 if (S_ISDIR(mode))
Chris Mason39279cc2007-06-12 06:35:45 -04005416 owner = 0;
5417 else
5418 owner = 1;
Chris Mason9c583092008-01-29 15:15:18 -05005419
5420 key[0].objectid = objectid;
5421 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
5422 key[0].offset = 0;
5423
Mark Fashehf1863732012-08-08 11:32:27 -07005424 /*
5425 * Start new inodes with an inode_ref. This is slightly more
5426 * efficient for small numbers of hard links since they will
5427 * be packed into one item. Extended refs will kick in if we
5428 * add more hard links than can fit in the ref item.
5429 */
Chris Mason9c583092008-01-29 15:15:18 -05005430 key[1].objectid = objectid;
5431 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
5432 key[1].offset = ref_objectid;
5433
5434 sizes[0] = sizeof(struct btrfs_inode_item);
5435 sizes[1] = name_len + sizeof(*ref);
5436
Chris Masonb9473432009-03-13 11:00:37 -04005437 path->leave_spinning = 1;
Chris Mason9c583092008-01-29 15:15:18 -05005438 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
5439 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04005440 goto fail;
5441
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03005442 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04005443 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005444 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04005445 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
5446 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06005447 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
5448 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04005449 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05005450
5451 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
5452 struct btrfs_inode_ref);
5453 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04005454 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05005455 ptr = (unsigned long)(ref + 1);
5456 write_extent_buffer(path->nodes[0], name, ptr, name_len);
5457
Chris Mason5f39d392007-10-15 16:14:19 -04005458 btrfs_mark_buffer_dirty(path->nodes[0]);
5459 btrfs_free_path(path);
5460
Chris Mason39279cc2007-06-12 06:35:45 -04005461 location = &BTRFS_I(inode)->location;
5462 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04005463 location->offset = 0;
5464 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
5465
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005466 btrfs_inherit_iflags(inode, dir);
5467
Al Viro569254b2011-07-24 17:08:40 -04005468 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04005469 if (btrfs_test_opt(root, NODATASUM))
5470 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo213490b2012-09-11 08:33:50 -06005471 if (btrfs_test_opt(root, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05005472 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
5473 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04005474 }
5475
Chris Mason39279cc2007-06-12 06:35:45 -04005476 insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005477 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00005478
5479 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04005480 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00005481
Alexander Block8ea05e32012-07-25 17:35:53 +02005482 btrfs_update_root_times(trans, root);
5483
Chris Mason39279cc2007-06-12 06:35:45 -04005484 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005485fail:
Josef Bacikaec74772008-07-24 12:12:38 -04005486 if (dir)
5487 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04005488 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005489 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005490 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005491}
5492
5493static inline u8 btrfs_inode_type(struct inode *inode)
5494{
5495 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
5496}
5497
Chris Masond352ac62008-09-29 15:18:18 -04005498/*
5499 * utility function to add 'inode' into 'parent_inode' with
5500 * a give name and a given sequence number.
5501 * if 'add_backref' is true, also insert a backref from the
5502 * inode to the parent directory.
5503 */
Chris Masone02119d2008-09-05 16:13:11 -04005504int btrfs_add_link(struct btrfs_trans_handle *trans,
5505 struct inode *parent_inode, struct inode *inode,
5506 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005507{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005508 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005509 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04005510 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08005511 u64 ino = btrfs_ino(inode);
5512 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005513
Li Zefan33345d012011-04-20 10:31:50 +08005514 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005515 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
5516 } else {
Li Zefan33345d012011-04-20 10:31:50 +08005517 key.objectid = ino;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005518 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
5519 key.offset = 0;
5520 }
Chris Mason39279cc2007-06-12 06:35:45 -04005521
Li Zefan33345d012011-04-20 10:31:50 +08005522 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005523 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
5524 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08005525 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005526 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08005527 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
5528 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005529 }
5530
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005531 /* Nothing to clean up yet */
5532 if (ret)
5533 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005534
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005535 ret = btrfs_insert_dir_item(trans, root, name, name_len,
5536 parent_inode, &key,
5537 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05005538 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005539 goto fail_dir_item;
5540 else if (ret) {
5541 btrfs_abort_transaction(trans, root, ret);
5542 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005543 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005544
5545 btrfs_i_size_write(parent_inode, parent_inode->i_size +
5546 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005547 inode_inc_iversion(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005548 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
5549 ret = btrfs_update_inode(trans, root, parent_inode);
5550 if (ret)
5551 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005552 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05005553
5554fail_dir_item:
5555 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
5556 u64 local_index;
5557 int err;
5558 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
5559 key.objectid, root->root_key.objectid,
5560 parent_ino, &local_index, name, name_len);
5561
5562 } else if (add_backref) {
5563 u64 local_index;
5564 int err;
5565
5566 err = btrfs_del_inode_ref(trans, root, name, name_len,
5567 ino, parent_ino, &local_index);
5568 }
5569 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005570}
5571
5572static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00005573 struct inode *dir, struct dentry *dentry,
5574 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005575{
Josef Bacika1b075d2010-11-19 20:36:11 +00005576 int err = btrfs_add_link(trans, dir, inode,
5577 dentry->d_name.name, dentry->d_name.len,
5578 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005579 if (err > 0)
5580 err = -EEXIST;
5581 return err;
5582}
5583
Josef Bacik618e21d2007-07-11 10:18:17 -04005584static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04005585 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04005586{
5587 struct btrfs_trans_handle *trans;
5588 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005589 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04005590 int err;
5591 int drop_inode = 0;
5592 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005593 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04005594
5595 if (!new_valid_dev(rdev))
5596 return -EINVAL;
5597
Josef Bacik9ed74f22009-09-11 16:12:44 -04005598 /*
5599 * 2 for inode item and ref
5600 * 2 for dir items
5601 * 1 for xattr if selinux is on
5602 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005603 trans = btrfs_start_transaction(root, 5);
5604 if (IS_ERR(trans))
5605 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05005606
Li Zefan581bb052011-04-20 10:06:11 +08005607 err = btrfs_find_free_ino(root, &objectid);
5608 if (err)
5609 goto out_unlock;
5610
Josef Bacikaec74772008-07-24 12:12:38 -04005611 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005612 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005613 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005614 if (IS_ERR(inode)) {
5615 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005616 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005617 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005618
Eric Paris2a7dba32011-02-01 11:05:39 -05005619 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005620 if (err) {
5621 drop_inode = 1;
5622 goto out_unlock;
5623 }
5624
Casey Schauflerad19db72011-12-15 10:09:07 -05005625 /*
5626 * If the active LSM wants to access the inode during
5627 * d_instantiate it needs these. Smack checks to see
5628 * if the filesystem supports xattrs by looking at the
5629 * ops vector.
5630 */
5631
5632 inode->i_op = &btrfs_special_inode_operations;
Josef Bacika1b075d2010-11-19 20:36:11 +00005633 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04005634 if (err)
5635 drop_inode = 1;
5636 else {
Josef Bacik618e21d2007-07-11 10:18:17 -04005637 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04005638 btrfs_update_inode(trans, root, inode);
Al Viro08c422c2011-12-23 07:58:13 -05005639 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005640 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005641out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005642 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00005643 btrfs_btree_balance_dirty(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04005644 if (drop_inode) {
5645 inode_dec_link_count(inode);
5646 iput(inode);
5647 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005648 return err;
5649}
5650
Chris Mason39279cc2007-06-12 06:35:45 -04005651static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04005652 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04005653{
5654 struct btrfs_trans_handle *trans;
5655 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005656 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005657 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04005658 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04005659 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005660 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005661
Josef Bacik9ed74f22009-09-11 16:12:44 -04005662 /*
5663 * 2 for inode item and ref
5664 * 2 for dir items
5665 * 1 for xattr if selinux is on
5666 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005667 trans = btrfs_start_transaction(root, 5);
5668 if (IS_ERR(trans))
5669 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005670
Li Zefan581bb052011-04-20 10:06:11 +08005671 err = btrfs_find_free_ino(root, &objectid);
5672 if (err)
5673 goto out_unlock;
5674
Josef Bacikaec74772008-07-24 12:12:38 -04005675 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005676 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005677 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005678 if (IS_ERR(inode)) {
5679 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005680 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005681 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005682 drop_inode_on_err = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04005683
Eric Paris2a7dba32011-02-01 11:05:39 -05005684 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005685 if (err)
Josef Bacik33268ea2008-07-24 12:16:36 -04005686 goto out_unlock;
Josef Bacik33268ea2008-07-24 12:16:36 -04005687
Filipe Brandenburger9185aa52012-11-30 03:40:08 +00005688 err = btrfs_update_inode(trans, root, inode);
5689 if (err)
5690 goto out_unlock;
5691
Casey Schauflerad19db72011-12-15 10:09:07 -05005692 /*
5693 * If the active LSM wants to access the inode during
5694 * d_instantiate it needs these. Smack checks to see
5695 * if the filesystem supports xattrs by looking at the
5696 * ops vector.
5697 */
5698 inode->i_fop = &btrfs_file_operations;
5699 inode->i_op = &btrfs_file_inode_operations;
5700
Josef Bacika1b075d2010-11-19 20:36:11 +00005701 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005702 if (err)
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005703 goto out_unlock;
5704
5705 inode->i_mapping->a_ops = &btrfs_aops;
5706 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
5707 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
5708 d_instantiate(dentry, inode);
5709
Chris Mason39279cc2007-06-12 06:35:45 -04005710out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005711 btrfs_end_transaction(trans, root);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005712 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005713 inode_dec_link_count(inode);
5714 iput(inode);
5715 }
Liu Bob53d3f52012-11-14 14:34:34 +00005716 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005717 return err;
5718}
5719
5720static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
5721 struct dentry *dentry)
5722{
5723 struct btrfs_trans_handle *trans;
5724 struct btrfs_root *root = BTRFS_I(dir)->root;
5725 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005726 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04005727 int err;
5728 int drop_inode = 0;
5729
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005730 /* do not allow sys_link's with other subvols of the same device */
5731 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00005732 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005733
Mark Fashehf1863732012-08-08 11:32:27 -07005734 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00005735 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04005736
Chris Mason3de45862008-11-17 21:02:50 -05005737 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04005738 if (err)
5739 goto fail;
5740
Yan, Zhenga22285a2010-05-16 10:48:46 -04005741 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00005742 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04005743 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00005744 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04005745 */
Miao Xie7e6b6462011-02-18 09:21:17 +00005746 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005747 if (IS_ERR(trans)) {
5748 err = PTR_ERR(trans);
5749 goto fail;
5750 }
Chris Mason5f39d392007-10-15 16:14:19 -04005751
Miao Xie31534952011-04-13 13:19:21 +08005752 btrfs_inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005753 inode_inc_iversion(inode);
Miao Xie31534952011-04-13 13:19:21 +08005754 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6ee2010-10-23 11:11:40 -04005755 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04005756 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04005757
Josef Bacika1b075d2010-11-19 20:36:11 +00005758 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04005759
Yan, Zhenga5719522009-09-24 09:17:31 -04005760 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005761 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04005762 } else {
Al Viro10d9f302011-07-16 23:09:10 -04005763 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04005764 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005765 if (err)
5766 goto fail;
Al Viro08c422c2011-12-23 07:58:13 -05005767 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00005768 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04005769 }
Chris Mason39279cc2007-06-12 06:35:45 -04005770
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005771 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05005772fail:
Chris Mason39279cc2007-06-12 06:35:45 -04005773 if (drop_inode) {
5774 inode_dec_link_count(inode);
5775 iput(inode);
5776 }
Liu Bob53d3f52012-11-14 14:34:34 +00005777 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005778 return err;
5779}
5780
Al Viro18bb1db2011-07-26 01:41:39 -04005781static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04005782{
Chris Masonb9d86662008-05-02 16:13:49 -04005783 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04005784 struct btrfs_trans_handle *trans;
5785 struct btrfs_root *root = BTRFS_I(dir)->root;
5786 int err = 0;
5787 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04005788 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005789 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005790
Josef Bacik9ed74f22009-09-11 16:12:44 -04005791 /*
5792 * 2 items for inode and ref
5793 * 2 items for dir items
5794 * 1 for xattr if selinux is on
5795 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005796 trans = btrfs_start_transaction(root, 5);
5797 if (IS_ERR(trans))
5798 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04005799
Li Zefan581bb052011-04-20 10:06:11 +08005800 err = btrfs_find_free_ino(root, &objectid);
5801 if (err)
5802 goto out_fail;
5803
Josef Bacikaec74772008-07-24 12:12:38 -04005804 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005805 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005806 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04005807 if (IS_ERR(inode)) {
5808 err = PTR_ERR(inode);
5809 goto out_fail;
5810 }
Chris Mason5f39d392007-10-15 16:14:19 -04005811
Chris Mason39279cc2007-06-12 06:35:45 -04005812 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04005813
Eric Paris2a7dba32011-02-01 11:05:39 -05005814 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005815 if (err)
5816 goto out_fail;
5817
Chris Mason39279cc2007-06-12 06:35:45 -04005818 inode->i_op = &btrfs_dir_inode_operations;
5819 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04005820
Chris Masondbe674a2008-07-17 12:54:05 -04005821 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005822 err = btrfs_update_inode(trans, root, inode);
5823 if (err)
5824 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005825
Josef Bacika1b075d2010-11-19 20:36:11 +00005826 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
5827 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005828 if (err)
5829 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005830
Chris Mason39279cc2007-06-12 06:35:45 -04005831 d_instantiate(dentry, inode);
5832 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005833
5834out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005835 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005836 if (drop_on_err)
5837 iput(inode);
Liu Bob53d3f52012-11-14 14:34:34 +00005838 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005839 return err;
5840}
5841
Chris Masond352ac62008-09-29 15:18:18 -04005842/* helper for btfs_get_extent. Given an existing extent in the tree,
5843 * and an extent that you want to insert, deal with overlap and insert
5844 * the new extent into the tree.
5845 */
Chris Mason3b951512008-04-17 11:29:12 -04005846static int merge_extent_mapping(struct extent_map_tree *em_tree,
5847 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005848 struct extent_map *em,
5849 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04005850{
5851 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04005852
Chris Masone6dcd2d2008-07-17 12:53:50 -04005853 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
5854 start_diff = map_start - em->start;
5855 em->start = map_start;
5856 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04005857 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
5858 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04005859 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04005860 em->block_len -= start_diff;
5861 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005862 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04005863}
5864
Chris Masonc8b97812008-10-29 14:49:59 -04005865static noinline int uncompress_inline(struct btrfs_path *path,
5866 struct inode *inode, struct page *page,
5867 size_t pg_offset, u64 extent_offset,
5868 struct btrfs_file_extent_item *item)
5869{
5870 int ret;
5871 struct extent_buffer *leaf = path->nodes[0];
5872 char *tmp;
5873 size_t max_size;
5874 unsigned long inline_size;
5875 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08005876 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04005877
5878 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08005879 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04005880 max_size = btrfs_file_extent_ram_bytes(leaf, item);
5881 inline_size = btrfs_file_extent_inline_item_len(leaf,
5882 btrfs_item_nr(leaf, path->slots[0]));
5883 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04005884 if (!tmp)
5885 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04005886 ptr = btrfs_file_extent_inline_start(item);
5887
5888 read_extent_buffer(leaf, tmp, ptr, inline_size);
5889
Chris Mason5b050f02008-11-11 09:34:41 -05005890 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08005891 ret = btrfs_decompress(compress_type, tmp, page,
5892 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04005893 if (ret) {
Cong Wang7ac687d2011-11-25 23:14:28 +08005894 char *kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04005895 unsigned long copy_size = min_t(u64,
5896 PAGE_CACHE_SIZE - pg_offset,
5897 max_size - extent_offset);
5898 memset(kaddr + pg_offset, 0, copy_size);
Cong Wang7ac687d2011-11-25 23:14:28 +08005899 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -04005900 }
5901 kfree(tmp);
5902 return 0;
5903}
5904
Chris Masond352ac62008-09-29 15:18:18 -04005905/*
5906 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05005907 * the ugly parts come from merging extents from the disk with the in-ram
5908 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04005909 * where the in-ram extents might be locked pending data=ordered completion.
5910 *
5911 * This also copies inline extents directly into the page.
5912 */
Chris Masond3977122009-01-05 21:25:51 -05005913
Chris Masona52d9a82007-08-27 16:49:44 -04005914struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05005915 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04005916 int create)
5917{
5918 int ret;
5919 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04005920 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04005921 u64 extent_start = 0;
5922 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08005923 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04005924 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04005925 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04005926 struct btrfs_root *root = BTRFS_I(inode)->root;
5927 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04005928 struct extent_buffer *leaf;
5929 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04005930 struct extent_map *em = NULL;
5931 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05005932 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04005933 struct btrfs_trans_handle *trans = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08005934 int compress_type;
Chris Masona52d9a82007-08-27 16:49:44 -04005935
Chris Masona52d9a82007-08-27 16:49:44 -04005936again:
Chris Mason890871b2009-09-02 16:24:52 -04005937 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005938 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04005939 if (em)
5940 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04005941 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005942
Chris Masona52d9a82007-08-27 16:49:44 -04005943 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04005944 if (em->start > start || em->start + em->len <= start)
5945 free_extent_map(em);
5946 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05005947 free_extent_map(em);
5948 else
5949 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005950 }
David Sterba172ddd62011-04-21 00:48:27 +02005951 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04005952 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05005953 err = -ENOMEM;
5954 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005955 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005956 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05005957 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05005958 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05005959 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04005960 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04005961
5962 if (!path) {
5963 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04005964 if (!path) {
5965 err = -ENOMEM;
5966 goto out;
5967 }
5968 /*
5969 * Chances are we'll be called again, so go ahead and do
5970 * readahead
5971 */
5972 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04005973 }
5974
Chris Mason179e29e2007-11-01 11:28:41 -04005975 ret = btrfs_lookup_file_extent(trans, root, path,
5976 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04005977 if (ret < 0) {
5978 err = ret;
5979 goto out;
5980 }
5981
5982 if (ret != 0) {
5983 if (path->slots[0] == 0)
5984 goto not_found;
5985 path->slots[0]--;
5986 }
5987
Chris Mason5f39d392007-10-15 16:14:19 -04005988 leaf = path->nodes[0];
5989 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04005990 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04005991 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04005992 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5993 found_type = btrfs_key_type(&found_key);
5994 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04005995 found_type != BTRFS_EXTENT_DATA_KEY) {
5996 goto not_found;
5997 }
5998
Chris Mason5f39d392007-10-15 16:14:19 -04005999 found_type = btrfs_file_extent_type(leaf, item);
6000 extent_start = found_key.offset;
Li Zefan261507a02010-12-17 14:21:50 +08006001 compress_type = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04006002 if (found_type == BTRFS_FILE_EXTENT_REG ||
6003 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006004 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006005 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006006 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6007 size_t size;
6008 size = btrfs_file_extent_inline_len(leaf, item);
Qu Wenruofda28322013-02-26 08:10:22 +00006009 extent_end = ALIGN(extent_start + size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04006010 }
6011
6012 if (start >= extent_end) {
6013 path->slots[0]++;
6014 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6015 ret = btrfs_next_leaf(root, path);
6016 if (ret < 0) {
6017 err = ret;
6018 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006019 }
Yan Zheng9036c102008-10-30 14:19:41 -04006020 if (ret > 0)
6021 goto not_found;
6022 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006023 }
Yan Zheng9036c102008-10-30 14:19:41 -04006024 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6025 if (found_key.objectid != objectid ||
6026 found_key.type != BTRFS_EXTENT_DATA_KEY)
6027 goto not_found;
6028 if (start + len <= found_key.offset)
6029 goto not_found;
6030 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006031 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006032 em->len = found_key.offset - start;
6033 goto not_found_em;
6034 }
6035
Josef Bacikcc95bef2013-04-04 14:31:27 -04006036 em->ram_bytes = btrfs_file_extent_ram_bytes(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04006037 if (found_type == BTRFS_FILE_EXTENT_REG ||
6038 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04006039 em->start = extent_start;
6040 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05006041 em->orig_start = extent_start -
6042 btrfs_file_extent_offset(leaf, item);
Josef Bacikb4939682012-12-03 10:31:19 -05006043 em->orig_block_len = btrfs_file_extent_disk_num_bytes(leaf,
6044 item);
Chris Masondb945352007-10-15 16:15:53 -04006045 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
6046 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04006047 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04006048 goto insert;
6049 }
Li Zefan261507a02010-12-17 14:21:50 +08006050 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006051 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08006052 em->compress_type = compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006053 em->block_start = bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05006054 em->block_len = em->orig_block_len;
Chris Masonc8b97812008-10-29 14:49:59 -04006055 } else {
6056 bytenr += btrfs_file_extent_offset(leaf, item);
6057 em->block_start = bytenr;
6058 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04006059 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
6060 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04006061 }
Chris Masona52d9a82007-08-27 16:49:44 -04006062 goto insert;
6063 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006064 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006065 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006066 size_t size;
6067 size_t extent_offset;
6068 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006069
Chris Masona52d9a82007-08-27 16:49:44 -04006070 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04006071 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04006072 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04006073 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04006074 goto out;
6075 }
6076
Yan Zheng9036c102008-10-30 14:19:41 -04006077 size = btrfs_file_extent_inline_len(leaf, item);
6078 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05006079 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04006080 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006081 em->start = extent_start + extent_offset;
Qu Wenruofda28322013-02-26 08:10:22 +00006082 em->len = ALIGN(copy_size, root->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006083 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006084 em->orig_start = em->start;
Li Zefan261507a02010-12-17 14:21:50 +08006085 if (compress_type) {
Chris Masonc8b97812008-10-29 14:49:59 -04006086 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08006087 em->compress_type = compress_type;
6088 }
Yan689f9342007-10-29 11:41:07 -04006089 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006090 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006091 if (btrfs_file_extent_compression(leaf, item) !=
6092 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006093 ret = uncompress_inline(path, inode, page,
6094 pg_offset,
6095 extent_offset, item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006096 BUG_ON(ret); /* -ENOMEM */
Chris Masonc8b97812008-10-29 14:49:59 -04006097 } else {
6098 map = kmap(page);
6099 read_extent_buffer(leaf, map + pg_offset, ptr,
6100 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04006101 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
6102 memset(map + pg_offset + copy_size, 0,
6103 PAGE_CACHE_SIZE - pg_offset -
6104 copy_size);
6105 }
Chris Masonc8b97812008-10-29 14:49:59 -04006106 kunmap(page);
6107 }
Chris Mason179e29e2007-11-01 11:28:41 -04006108 flush_dcache_page(page);
6109 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006110 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006111 if (!trans) {
6112 kunmap(page);
6113 free_extent_map(em);
6114 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006115
David Sterbab3b4aa72011-04-21 01:20:15 +02006116 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006117 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006118
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006119 if (IS_ERR(trans))
6120 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006121 goto again;
6122 }
Chris Masonc8b97812008-10-29 14:49:59 -04006123 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006124 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006125 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006126 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006127 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006128 }
Chris Masond1310b22008-01-24 16:13:08 -05006129 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006130 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006131 goto insert;
6132 } else {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00006133 WARN(1, KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04006134 }
6135not_found:
6136 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006137 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006138 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006139not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04006140 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04006141 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006142insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006143 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006144 if (em->start > start || extent_map_end(em) <= start) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006145 btrfs_err(root->fs_info, "bad extent! em: [%llu %llu] passed [%llu %llu]",
6146 (unsigned long long)em->start,
6147 (unsigned long long)em->len,
6148 (unsigned long long)start,
6149 (unsigned long long)len);
Chris Masona52d9a82007-08-27 16:49:44 -04006150 err = -EIO;
6151 goto out;
6152 }
Chris Masond1310b22008-01-24 16:13:08 -05006153
6154 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006155 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006156 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006157 /* it is possible that someone inserted the extent into the tree
6158 * while we had the lock dropped. It is also possible that
6159 * an overlapping map exists in the tree
6160 */
Chris Masona52d9a82007-08-27 16:49:44 -04006161 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04006162 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006163
6164 ret = 0;
6165
Chris Mason3b951512008-04-17 11:29:12 -04006166 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04006167 if (existing && (existing->start > start ||
6168 existing->start + existing->len <= start)) {
6169 free_extent_map(existing);
6170 existing = NULL;
6171 }
Chris Mason3b951512008-04-17 11:29:12 -04006172 if (!existing) {
6173 existing = lookup_extent_mapping(em_tree, em->start,
6174 em->len);
6175 if (existing) {
6176 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006177 em, start,
6178 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04006179 free_extent_map(existing);
6180 if (err) {
6181 free_extent_map(em);
6182 em = NULL;
6183 }
6184 } else {
6185 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04006186 free_extent_map(em);
6187 em = NULL;
6188 }
6189 } else {
6190 free_extent_map(em);
6191 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006192 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006193 }
Chris Masona52d9a82007-08-27 16:49:44 -04006194 }
Chris Mason890871b2009-09-02 16:24:52 -04006195 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006196out:
liubo1abe9b82011-03-24 11:18:59 +00006197
Tsutomu Itohf0bd95e2012-10-01 03:08:37 -06006198 if (em)
6199 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00006200
Chris Masonf4219502008-07-22 11:18:09 -04006201 if (path)
6202 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04006203 if (trans) {
6204 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05006205 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04006206 err = ret;
6207 }
Chris Masona52d9a82007-08-27 16:49:44 -04006208 if (err) {
6209 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04006210 return ERR_PTR(err);
6211 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006212 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04006213 return em;
6214}
6215
Chris Masonec29ed52011-02-23 16:23:20 -05006216struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
6217 size_t pg_offset, u64 start, u64 len,
6218 int create)
6219{
6220 struct extent_map *em;
6221 struct extent_map *hole_em = NULL;
6222 u64 range_start = start;
6223 u64 end;
6224 u64 found;
6225 u64 found_end;
6226 int err = 0;
6227
6228 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
6229 if (IS_ERR(em))
6230 return em;
6231 if (em) {
6232 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00006233 * if our em maps to
6234 * - a hole or
6235 * - a pre-alloc extent,
6236 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05006237 */
Liu Bof9e4fb52013-01-07 10:10:12 +00006238 if (em->block_start != EXTENT_MAP_HOLE &&
6239 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05006240 return em;
6241 else
6242 hole_em = em;
6243 }
6244
6245 /* check to see if we've wrapped (len == -1 or similar) */
6246 end = start + len;
6247 if (end < start)
6248 end = (u64)-1;
6249 else
6250 end -= 1;
6251
6252 em = NULL;
6253
6254 /* ok, we didn't find anything, lets look for delalloc */
6255 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
6256 end, len, EXTENT_DELALLOC, 1);
6257 found_end = range_start + found;
6258 if (found_end < range_start)
6259 found_end = (u64)-1;
6260
6261 /*
6262 * we didn't find anything useful, return
6263 * the original results from get_extent()
6264 */
6265 if (range_start > end || found_end <= start) {
6266 em = hole_em;
6267 hole_em = NULL;
6268 goto out;
6269 }
6270
6271 /* adjust the range_start to make sure it doesn't
6272 * go backwards from the start they passed in
6273 */
6274 range_start = max(start,range_start);
6275 found = found_end - range_start;
6276
6277 if (found > 0) {
6278 u64 hole_start = start;
6279 u64 hole_len = len;
6280
David Sterba172ddd62011-04-21 00:48:27 +02006281 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05006282 if (!em) {
6283 err = -ENOMEM;
6284 goto out;
6285 }
6286 /*
6287 * when btrfs_get_extent can't find anything it
6288 * returns one huge hole
6289 *
6290 * make sure what it found really fits our range, and
6291 * adjust to make sure it is based on the start from
6292 * the caller
6293 */
6294 if (hole_em) {
6295 u64 calc_end = extent_map_end(hole_em);
6296
6297 if (calc_end <= start || (hole_em->start > end)) {
6298 free_extent_map(hole_em);
6299 hole_em = NULL;
6300 } else {
6301 hole_start = max(hole_em->start, start);
6302 hole_len = calc_end - hole_start;
6303 }
6304 }
6305 em->bdev = NULL;
6306 if (hole_em && range_start > hole_start) {
6307 /* our hole starts before our delalloc, so we
6308 * have to return just the parts of the hole
6309 * that go until the delalloc starts
6310 */
6311 em->len = min(hole_len,
6312 range_start - hole_start);
6313 em->start = hole_start;
6314 em->orig_start = hole_start;
6315 /*
6316 * don't adjust block start at all,
6317 * it is fixed at EXTENT_MAP_HOLE
6318 */
6319 em->block_start = hole_em->block_start;
6320 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00006321 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
6322 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05006323 } else {
6324 em->start = range_start;
6325 em->len = found;
6326 em->orig_start = range_start;
6327 em->block_start = EXTENT_MAP_DELALLOC;
6328 em->block_len = found;
6329 }
6330 } else if (hole_em) {
6331 return hole_em;
6332 }
6333out:
6334
6335 free_extent_map(hole_em);
6336 if (err) {
6337 free_extent_map(em);
6338 return ERR_PTR(err);
6339 }
6340 return em;
6341}
6342
Josef Bacik4b46fce2010-05-23 11:00:55 -04006343static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
6344 u64 start, u64 len)
6345{
6346 struct btrfs_root *root = BTRFS_I(inode)->root;
6347 struct btrfs_trans_handle *trans;
Josef Bacik70c8a912012-10-11 16:54:30 -04006348 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006349 struct btrfs_key ins;
6350 u64 alloc_hint;
6351 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006352
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006353 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006354 if (IS_ERR(trans))
6355 return ERR_CAST(trans);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006356
6357 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
6358
6359 alloc_hint = get_extent_allocation_hint(inode, start, len);
6360 ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0,
Josef Bacik81c9ad22012-01-18 10:56:06 -05006361 alloc_hint, &ins, 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006362 if (ret) {
6363 em = ERR_PTR(ret);
6364 goto out;
6365 }
6366
Josef Bacik70c8a912012-10-11 16:54:30 -04006367 em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006368 ins.offset, ins.offset, ins.offset, 0);
Josef Bacik70c8a912012-10-11 16:54:30 -04006369 if (IS_ERR(em))
6370 goto out;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006371
6372 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
6373 ins.offset, ins.offset, 0);
6374 if (ret) {
6375 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
6376 em = ERR_PTR(ret);
6377 }
6378out:
6379 btrfs_end_transaction(trans, root);
6380 return em;
6381}
6382
Chris Mason46bfbb52010-05-26 11:04:10 -04006383/*
6384 * returns 1 when the nocow is safe, < 1 on error, 0 if the
6385 * block must be cow'd
6386 */
Josef Bacik7ee9e442013-06-21 16:37:03 -04006387noinline int can_nocow_extent(struct btrfs_trans_handle *trans,
6388 struct inode *inode, u64 offset, u64 *len,
6389 u64 *orig_start, u64 *orig_block_len,
6390 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04006391{
6392 struct btrfs_path *path;
6393 int ret;
6394 struct extent_buffer *leaf;
6395 struct btrfs_root *root = BTRFS_I(inode)->root;
6396 struct btrfs_file_extent_item *fi;
6397 struct btrfs_key key;
6398 u64 disk_bytenr;
6399 u64 backref_offset;
6400 u64 extent_end;
6401 u64 num_bytes;
6402 int slot;
6403 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04006404 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Chris Mason46bfbb52010-05-26 11:04:10 -04006405 path = btrfs_alloc_path();
6406 if (!path)
6407 return -ENOMEM;
6408
Li Zefan33345d012011-04-20 10:31:50 +08006409 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04006410 offset, 0);
6411 if (ret < 0)
6412 goto out;
6413
6414 slot = path->slots[0];
6415 if (ret == 1) {
6416 if (slot == 0) {
6417 /* can't find the item, must cow */
6418 ret = 0;
6419 goto out;
6420 }
6421 slot--;
6422 }
6423 ret = 0;
6424 leaf = path->nodes[0];
6425 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08006426 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04006427 key.type != BTRFS_EXTENT_DATA_KEY) {
6428 /* not our file or wrong item type, must cow */
6429 goto out;
6430 }
6431
6432 if (key.offset > offset) {
6433 /* Wrong offset, must cow */
6434 goto out;
6435 }
6436
6437 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
6438 found_type = btrfs_file_extent_type(leaf, fi);
6439 if (found_type != BTRFS_FILE_EXTENT_REG &&
6440 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
6441 /* not a regular extent, must cow */
6442 goto out;
6443 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04006444
6445 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
6446 goto out;
6447
Chris Mason46bfbb52010-05-26 11:04:10 -04006448 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04006449 if (disk_bytenr == 0)
6450 goto out;
6451
6452 if (btrfs_file_extent_compression(leaf, fi) ||
6453 btrfs_file_extent_encryption(leaf, fi) ||
6454 btrfs_file_extent_other_encoding(leaf, fi))
6455 goto out;
6456
Chris Mason46bfbb52010-05-26 11:04:10 -04006457 backref_offset = btrfs_file_extent_offset(leaf, fi);
6458
Josef Bacik7ee9e442013-06-21 16:37:03 -04006459 if (orig_start) {
6460 *orig_start = key.offset - backref_offset;
6461 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
6462 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
6463 }
Josef Bacikeb384b52013-04-24 16:32:55 -04006464
Chris Mason46bfbb52010-05-26 11:04:10 -04006465 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason46bfbb52010-05-26 11:04:10 -04006466
6467 if (btrfs_extent_readonly(root, disk_bytenr))
6468 goto out;
6469
6470 /*
6471 * look for other files referencing this extent, if we
6472 * find any we must cow
6473 */
Li Zefan33345d012011-04-20 10:31:50 +08006474 if (btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04006475 key.offset - backref_offset, disk_bytenr))
6476 goto out;
6477
6478 /*
6479 * adjust disk_bytenr and num_bytes to cover just the bytes
6480 * in this extent we are about to write. If there
6481 * are any csums in that range we have to cow in order
6482 * to keep the csums correct
6483 */
6484 disk_bytenr += backref_offset;
6485 disk_bytenr += offset - key.offset;
Josef Bacikeb384b52013-04-24 16:32:55 -04006486 num_bytes = min(offset + *len, extent_end) - offset;
Chris Mason46bfbb52010-05-26 11:04:10 -04006487 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
6488 goto out;
6489 /*
6490 * all of the above have passed, it is safe to overwrite this extent
6491 * without cow
6492 */
Josef Bacikeb384b52013-04-24 16:32:55 -04006493 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04006494 ret = 1;
6495out:
6496 btrfs_free_path(path);
6497 return ret;
6498}
6499
Josef Bacikeb838e72012-07-31 16:28:48 -04006500static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
6501 struct extent_state **cached_state, int writing)
6502{
6503 struct btrfs_ordered_extent *ordered;
6504 int ret = 0;
6505
6506 while (1) {
6507 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6508 0, cached_state);
6509 /*
6510 * We're concerned with the entire range that we're going to be
6511 * doing DIO to, so we need to make sure theres no ordered
6512 * extents in this range.
6513 */
6514 ordered = btrfs_lookup_ordered_range(inode, lockstart,
6515 lockend - lockstart + 1);
6516
6517 /*
6518 * We need to make sure there are no buffered pages in this
6519 * range either, we could have raced between the invalidate in
6520 * generic_file_direct_write and locking the extent. The
6521 * invalidate needs to happen so that reads after a write do not
6522 * get stale data.
6523 */
6524 if (!ordered && (!writing ||
6525 !test_range_bit(&BTRFS_I(inode)->io_tree,
6526 lockstart, lockend, EXTENT_UPTODATE, 0,
6527 *cached_state)))
6528 break;
6529
6530 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6531 cached_state, GFP_NOFS);
6532
6533 if (ordered) {
6534 btrfs_start_ordered_extent(inode, ordered, 1);
6535 btrfs_put_ordered_extent(ordered);
6536 } else {
6537 /* Screw you mmap */
6538 ret = filemap_write_and_wait_range(inode->i_mapping,
6539 lockstart,
6540 lockend);
6541 if (ret)
6542 break;
6543
6544 /*
6545 * If we found a page that couldn't be invalidated just
6546 * fall back to buffered.
6547 */
6548 ret = invalidate_inode_pages2_range(inode->i_mapping,
6549 lockstart >> PAGE_CACHE_SHIFT,
6550 lockend >> PAGE_CACHE_SHIFT);
6551 if (ret)
6552 break;
6553 }
6554
6555 cond_resched();
6556 }
6557
6558 return ret;
6559}
6560
Josef Bacik69ffb542012-09-11 15:40:07 -04006561static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
6562 u64 len, u64 orig_start,
6563 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006564 u64 orig_block_len, u64 ram_bytes,
6565 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04006566{
6567 struct extent_map_tree *em_tree;
6568 struct extent_map *em;
6569 struct btrfs_root *root = BTRFS_I(inode)->root;
6570 int ret;
6571
6572 em_tree = &BTRFS_I(inode)->extent_tree;
6573 em = alloc_extent_map();
6574 if (!em)
6575 return ERR_PTR(-ENOMEM);
6576
6577 em->start = start;
6578 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04006579 em->mod_start = start;
6580 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04006581 em->len = len;
6582 em->block_len = block_len;
6583 em->block_start = block_start;
6584 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05006585 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04006586 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04006587 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04006588 set_bit(EXTENT_FLAG_PINNED, &em->flags);
6589 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05006590 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04006591
6592 do {
6593 btrfs_drop_extent_cache(inode, em->start,
6594 em->start + em->len - 1, 0);
6595 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006596 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04006597 write_unlock(&em_tree->lock);
6598 } while (ret == -EEXIST);
6599
6600 if (ret) {
6601 free_extent_map(em);
6602 return ERR_PTR(ret);
6603 }
6604
6605 return em;
6606}
6607
6608
Josef Bacik4b46fce2010-05-23 11:00:55 -04006609static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
6610 struct buffer_head *bh_result, int create)
6611{
6612 struct extent_map *em;
6613 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04006614 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006615 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04006616 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006617 u64 len = bh_result->b_size;
Chris Mason46bfbb52010-05-26 11:04:10 -04006618 struct btrfs_trans_handle *trans;
Josef Bacikeb838e72012-07-31 16:28:48 -04006619 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00006620 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006621
Miao Xie172a5042013-02-21 02:48:22 -07006622 if (create)
Josef Bacikeb838e72012-07-31 16:28:48 -04006623 unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07006624 else
Josef Bacikc3298612012-08-03 16:49:19 -04006625 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04006626
Josef Bacikc3298612012-08-03 16:49:19 -04006627 lockstart = start;
6628 lockend = start + len - 1;
6629
Josef Bacikeb838e72012-07-31 16:28:48 -04006630 /*
6631 * If this errors out it's because we couldn't invalidate pagecache for
6632 * this range and we need to fallback to buffered.
6633 */
6634 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
6635 return -ENOTBLK;
6636
Josef Bacik4b46fce2010-05-23 11:00:55 -04006637 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04006638 if (IS_ERR(em)) {
6639 ret = PTR_ERR(em);
6640 goto unlock_err;
6641 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006642
6643 /*
6644 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
6645 * io. INLINE is special, and we could probably kludge it in here, but
6646 * it's still buffered so for safety lets just fall back to the generic
6647 * buffered path.
6648 *
6649 * For COMPRESSED we _have_ to read the entire extent in so we can
6650 * decompress it, so there will be buffering required no matter what we
6651 * do, so go ahead and fallback to buffered.
6652 *
6653 * We return -ENOTBLK because thats what makes DIO go ahead and go back
6654 * to buffered IO. Don't blame me, this is the price we pay for using
6655 * the generic code.
6656 */
6657 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
6658 em->block_start == EXTENT_MAP_INLINE) {
6659 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006660 ret = -ENOTBLK;
6661 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006662 }
6663
6664 /* Just a good old fashioned hole, return */
6665 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
6666 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
6667 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006668 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006669 }
6670
6671 /*
6672 * We don't allocate a new extent in the following cases
6673 *
6674 * 1) The inode is marked as NODATACOW. In this case we'll just use the
6675 * existing extent.
6676 * 2) The extent is marked as PREALLOC. We're good to go here and can
6677 * just use the extent.
6678 *
6679 */
Chris Mason46bfbb52010-05-26 11:04:10 -04006680 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04006681 len = min(len, em->len - (start - em->start));
6682 lockstart = start + len;
6683 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04006684 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006685
6686 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
6687 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
6688 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04006689 int type;
6690 int ret;
Josef Bacikeb384b52013-04-24 16:32:55 -04006691 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006692
6693 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6694 type = BTRFS_ORDERED_PREALLOC;
6695 else
6696 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04006697 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04006698 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04006699
6700 /*
6701 * we're not going to log anything, but we do need
6702 * to make sure the current transaction stays open
6703 * while we look for nocow cross refs
6704 */
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006705 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006706 if (IS_ERR(trans))
Chris Mason46bfbb52010-05-26 11:04:10 -04006707 goto must_cow;
6708
Josef Bacik7ee9e442013-06-21 16:37:03 -04006709 if (can_nocow_extent(trans, inode, start, &len, &orig_start,
6710 &orig_block_len, &ram_bytes) == 1) {
Josef Bacik69ffb542012-09-11 15:40:07 -04006711 if (type == BTRFS_ORDERED_PREALLOC) {
6712 free_extent_map(em);
6713 em = create_pinned_em(inode, start, len,
6714 orig_start,
Josef Bacikb4939682012-12-03 10:31:19 -05006715 block_start, len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006716 orig_block_len,
6717 ram_bytes, type);
Josef Bacik69ffb542012-09-11 15:40:07 -04006718 if (IS_ERR(em)) {
6719 btrfs_end_transaction(trans, root);
6720 goto unlock_err;
6721 }
6722 }
6723
Chris Mason46bfbb52010-05-26 11:04:10 -04006724 ret = btrfs_add_ordered_extent_dio(inode, start,
6725 block_start, len, len, type);
6726 btrfs_end_transaction(trans, root);
6727 if (ret) {
6728 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006729 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04006730 }
6731 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006732 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006733 btrfs_end_transaction(trans, root);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006734 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006735must_cow:
6736 /*
6737 * this will cow the extent, reset the len in case we changed
6738 * it above
6739 */
6740 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04006741 free_extent_map(em);
6742 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04006743 if (IS_ERR(em)) {
6744 ret = PTR_ERR(em);
6745 goto unlock_err;
6746 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006747 len = min(len, em->len - (start - em->start));
6748unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04006749 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
6750 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04006751 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006752 bh_result->b_bdev = em->bdev;
6753 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04006754 if (create) {
6755 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6756 set_buffer_new(bh_result);
6757
6758 /*
6759 * Need to update the i_size under the extent lock so buffered
6760 * readers will get the updated i_size when we unlock.
6761 */
6762 if (start + len > i_size_read(inode))
6763 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00006764
Miao Xie172a5042013-02-21 02:48:22 -07006765 spin_lock(&BTRFS_I(inode)->lock);
6766 BTRFS_I(inode)->outstanding_extents++;
6767 spin_unlock(&BTRFS_I(inode)->lock);
6768
Miao Xie09348562013-02-07 10:12:07 +00006769 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6770 lockstart + len - 1, EXTENT_DELALLOC, NULL,
6771 &cached_state, GFP_NOFS);
6772 BUG_ON(ret);
Josef Bacikc3473e82012-06-19 10:59:00 -04006773 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006774
Josef Bacikeb838e72012-07-31 16:28:48 -04006775 /*
6776 * In the case of write we need to clear and unlock the entire range,
6777 * in the case of read we need to unlock only the end area that we
6778 * aren't using if there is any left over space.
6779 */
Liu Bo24c03fa2012-08-22 20:10:38 -06006780 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00006781 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6782 lockend, unlock_bits, 1, 0,
6783 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06006784 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04006785 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06006786 }
Josef Bacikeb838e72012-07-31 16:28:48 -04006787
Josef Bacik4b46fce2010-05-23 11:00:55 -04006788 free_extent_map(em);
6789
6790 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006791
6792unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04006793 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6794 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
6795 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006796}
6797
Josef Bacik4b46fce2010-05-23 11:00:55 -04006798static void btrfs_endio_direct_read(struct bio *bio, int err)
6799{
Miao Xiee65e1532010-11-22 03:04:43 +00006800 struct btrfs_dio_private *dip = bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006801 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
6802 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006803 struct inode *inode = dip->inode;
6804 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason9be33952013-05-17 18:30:14 -04006805 struct bio *dio_bio;
Miao Xiefacc8a222013-07-25 19:22:34 +08006806 u32 *csums = (u32 *)dip->csum;
6807 int index = 0;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006808 u64 start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006809
6810 start = dip->logical_offset;
6811 do {
6812 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
6813 struct page *page = bvec->bv_page;
6814 char *kaddr;
6815 u32 csum = ~(u32)0;
6816 unsigned long flags;
6817
6818 local_irq_save(flags);
Cong Wang7ac687d2011-11-25 23:14:28 +08006819 kaddr = kmap_atomic(page);
Liu Bob0496682013-03-14 14:57:45 +00006820 csum = btrfs_csum_data(kaddr + bvec->bv_offset,
Josef Bacik4b46fce2010-05-23 11:00:55 -04006821 csum, bvec->bv_len);
6822 btrfs_csum_final(csum, (char *)&csum);
Cong Wang7ac687d2011-11-25 23:14:28 +08006823 kunmap_atomic(kaddr);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006824 local_irq_restore(flags);
6825
6826 flush_dcache_page(bvec->bv_page);
Miao Xiefacc8a222013-07-25 19:22:34 +08006827 if (csum != csums[index]) {
6828 btrfs_err(root->fs_info, "csum failed ino %llu off %llu csum %u expected csum %u",
6829 (unsigned long long)btrfs_ino(inode),
6830 (unsigned long long)start,
6831 csum, csums[index]);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006832 err = -EIO;
6833 }
6834 }
6835
6836 start += bvec->bv_len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006837 bvec++;
Miao Xiefacc8a222013-07-25 19:22:34 +08006838 index++;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006839 } while (bvec <= bvec_end);
6840
6841 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006842 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04006843 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006844
Josef Bacik4b46fce2010-05-23 11:00:55 -04006845 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006846
6847 /* If we had a csum failure make sure to clear the uptodate flag */
6848 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04006849 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
6850 dio_end_io(dio_bio, err);
6851 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006852}
6853
6854static void btrfs_endio_direct_write(struct bio *bio, int err)
6855{
6856 struct btrfs_dio_private *dip = bio->bi_private;
6857 struct inode *inode = dip->inode;
6858 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006859 struct btrfs_ordered_extent *ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05006860 u64 ordered_offset = dip->logical_offset;
6861 u64 ordered_bytes = dip->bytes;
Chris Mason9be33952013-05-17 18:30:14 -04006862 struct bio *dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006863 int ret;
6864
6865 if (err)
6866 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05006867again:
6868 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
6869 &ordered_offset,
Josef Bacik5fd02042012-05-02 14:00:54 -04006870 ordered_bytes, !err);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006871 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05006872 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006873
Josef Bacik5fd02042012-05-02 14:00:54 -04006874 ordered->work.func = finish_ordered_fn;
6875 ordered->work.flags = 0;
6876 btrfs_queue_worker(&root->fs_info->endio_write_workers,
6877 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05006878out_test:
6879 /*
6880 * our bio might span multiple ordered extents. If we haven't
6881 * completed the accounting for the whole dio, go back and try again
6882 */
6883 if (ordered_offset < dip->logical_offset + dip->bytes) {
6884 ordered_bytes = dip->logical_offset + dip->bytes -
6885 ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04006886 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05006887 goto again;
6888 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006889out_done:
Chris Mason9be33952013-05-17 18:30:14 -04006890 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006891
Josef Bacik4b46fce2010-05-23 11:00:55 -04006892 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006893
6894 /* If we had an error make sure to clear the uptodate flag */
6895 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04006896 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
6897 dio_end_io(dio_bio, err);
6898 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006899}
6900
Chris Masoneaf25d92010-05-25 09:48:28 -04006901static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
6902 struct bio *bio, int mirror_num,
6903 unsigned long bio_flags, u64 offset)
6904{
6905 int ret;
6906 struct btrfs_root *root = BTRFS_I(inode)->root;
6907 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006908 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04006909 return 0;
6910}
6911
Miao Xiee65e1532010-11-22 03:04:43 +00006912static void btrfs_end_dio_bio(struct bio *bio, int err)
6913{
6914 struct btrfs_dio_private *dip = bio->bi_private;
6915
6916 if (err) {
Li Zefan33345d012011-04-20 10:31:50 +08006917 printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu "
Jan Beulich3dd14622010-12-07 14:54:09 +00006918 "sector %#Lx len %u err no %d\n",
Li Zefan33345d012011-04-20 10:31:50 +08006919 (unsigned long long)btrfs_ino(dip->inode), bio->bi_rw,
Jan Beulich3dd14622010-12-07 14:54:09 +00006920 (unsigned long long)bio->bi_sector, bio->bi_size, err);
Miao Xiee65e1532010-11-22 03:04:43 +00006921 dip->errors = 1;
6922
6923 /*
6924 * before atomic variable goto zero, we must make sure
6925 * dip->errors is perceived to be set.
6926 */
6927 smp_mb__before_atomic_dec();
6928 }
6929
6930 /* if there are more bios still pending for this dio, just exit */
6931 if (!atomic_dec_and_test(&dip->pending_bios))
6932 goto out;
6933
Chris Mason9be33952013-05-17 18:30:14 -04006934 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00006935 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04006936 } else {
6937 set_bit(BIO_UPTODATE, &dip->dio_bio->bi_flags);
Miao Xiee65e1532010-11-22 03:04:43 +00006938 bio_endio(dip->orig_bio, 0);
6939 }
6940out:
6941 bio_put(bio);
6942}
6943
6944static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
6945 u64 first_sector, gfp_t gfp_flags)
6946{
6947 int nr_vecs = bio_get_nr_vecs(bdev);
6948 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
6949}
6950
6951static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
6952 int rw, u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04006953 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00006954{
Miao Xiefacc8a222013-07-25 19:22:34 +08006955 struct btrfs_dio_private *dip = bio->bi_private;
Miao Xiee65e1532010-11-22 03:04:43 +00006956 int write = rw & REQ_WRITE;
6957 struct btrfs_root *root = BTRFS_I(inode)->root;
6958 int ret;
6959
Josef Bacikb812ce22012-11-16 13:56:32 -05006960 if (async_submit)
6961 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
6962
Miao Xiee65e1532010-11-22 03:04:43 +00006963 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04006964
6965 if (!write) {
6966 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
6967 if (ret)
6968 goto err;
6969 }
Miao Xiee65e1532010-11-22 03:04:43 +00006970
Josef Bacik1ae39932011-04-06 14:41:34 -04006971 if (skip_sum)
6972 goto map;
6973
6974 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00006975 ret = btrfs_wq_submit_bio(root->fs_info,
6976 inode, rw, bio, 0, 0,
6977 file_offset,
6978 __btrfs_submit_bio_start_direct_io,
6979 __btrfs_submit_bio_done);
6980 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04006981 } else if (write) {
6982 /*
6983 * If we aren't doing async submit, calculate the csum of the
6984 * bio now.
6985 */
6986 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
6987 if (ret)
6988 goto err;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00006989 } else if (!skip_sum) {
Miao Xiefacc8a222013-07-25 19:22:34 +08006990 ret = btrfs_lookup_bio_sums_dio(root, inode, dip, bio,
6991 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00006992 if (ret)
6993 goto err;
6994 }
Miao Xiee65e1532010-11-22 03:04:43 +00006995
Josef Bacik1ae39932011-04-06 14:41:34 -04006996map:
6997 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006998err:
6999 bio_put(bio);
7000 return ret;
7001}
7002
7003static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
7004 int skip_sum)
7005{
7006 struct inode *inode = dip->inode;
7007 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00007008 struct bio *bio;
7009 struct bio *orig_bio = dip->orig_bio;
7010 struct bio_vec *bvec = orig_bio->bi_io_vec;
7011 u64 start_sector = orig_bio->bi_sector;
7012 u64 file_offset = dip->logical_offset;
7013 u64 submit_len = 0;
7014 u64 map_length;
7015 int nr_pages = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007016 int ret = 0;
Josef Bacik1ae39932011-04-06 14:41:34 -04007017 int async_submit = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007018
Miao Xiee65e1532010-11-22 03:04:43 +00007019 map_length = orig_bio->bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007020 ret = btrfs_map_block(root->fs_info, rw, start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007021 &map_length, NULL, 0);
7022 if (ret) {
Josef Bacik64728bb2011-04-25 19:43:52 -04007023 bio_put(orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00007024 return -EIO;
7025 }
Miao Xiefacc8a222013-07-25 19:22:34 +08007026
Josef Bacik02f57c72011-04-06 14:25:44 -04007027 if (map_length >= orig_bio->bi_size) {
7028 bio = orig_bio;
7029 goto submit;
7030 }
7031
David Woodhouse53b381b2013-01-29 18:40:14 -05007032 /* async crcs make it difficult to collect full stripe writes. */
7033 if (btrfs_get_alloc_profile(root, 1) &
7034 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6))
7035 async_submit = 0;
7036 else
7037 async_submit = 1;
7038
Josef Bacik02f57c72011-04-06 14:25:44 -04007039 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
7040 if (!bio)
7041 return -ENOMEM;
7042 bio->bi_private = dip;
7043 bio->bi_end_io = btrfs_end_dio_bio;
7044 atomic_inc(&dip->pending_bios);
7045
Miao Xiee65e1532010-11-22 03:04:43 +00007046 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
7047 if (unlikely(map_length < submit_len + bvec->bv_len ||
7048 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
7049 bvec->bv_offset) < bvec->bv_len)) {
7050 /*
7051 * inc the count before we submit the bio so
7052 * we know the end IO handler won't happen before
7053 * we inc the count. Otherwise, the dip might get freed
7054 * before we're done setting it up
7055 */
7056 atomic_inc(&dip->pending_bios);
7057 ret = __btrfs_submit_dio_bio(bio, inode, rw,
7058 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007059 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007060 if (ret) {
7061 bio_put(bio);
7062 atomic_dec(&dip->pending_bios);
7063 goto out_err;
7064 }
7065
Miao Xiee65e1532010-11-22 03:04:43 +00007066 start_sector += submit_len >> 9;
7067 file_offset += submit_len;
7068
7069 submit_len = 0;
7070 nr_pages = 0;
7071
7072 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
7073 start_sector, GFP_NOFS);
7074 if (!bio)
7075 goto out_err;
7076 bio->bi_private = dip;
7077 bio->bi_end_io = btrfs_end_dio_bio;
7078
7079 map_length = orig_bio->bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007080 ret = btrfs_map_block(root->fs_info, rw,
Stefan Behrens3ec706c2012-11-05 15:46:42 +01007081 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007082 &map_length, NULL, 0);
7083 if (ret) {
7084 bio_put(bio);
7085 goto out_err;
7086 }
7087 } else {
7088 submit_len += bvec->bv_len;
7089 nr_pages ++;
7090 bvec++;
7091 }
7092 }
7093
Josef Bacik02f57c72011-04-06 14:25:44 -04007094submit:
Miao Xiee65e1532010-11-22 03:04:43 +00007095 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007096 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007097 if (!ret)
7098 return 0;
7099
7100 bio_put(bio);
7101out_err:
7102 dip->errors = 1;
7103 /*
7104 * before atomic variable goto zero, we must
7105 * make sure dip->errors is perceived to be set.
7106 */
7107 smp_mb__before_atomic_dec();
7108 if (atomic_dec_and_test(&dip->pending_bios))
7109 bio_io_error(dip->orig_bio);
7110
7111 /* bio_end_io() will handle error, so we needn't return it */
7112 return 0;
7113}
7114
Chris Mason9be33952013-05-17 18:30:14 -04007115static void btrfs_submit_direct(int rw, struct bio *dio_bio,
7116 struct inode *inode, loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007117{
7118 struct btrfs_root *root = BTRFS_I(inode)->root;
7119 struct btrfs_dio_private *dip;
Chris Mason9be33952013-05-17 18:30:14 -04007120 struct bio *io_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007121 int skip_sum;
Miao Xiefacc8a222013-07-25 19:22:34 +08007122 int sum_len;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02007123 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007124 int ret = 0;
Miao Xiefacc8a222013-07-25 19:22:34 +08007125 u16 csum_size;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007126
7127 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
7128
Chris Mason9be33952013-05-17 18:30:14 -04007129 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04007130 if (!io_bio) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007131 ret = -ENOMEM;
7132 goto free_ordered;
7133 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007134
Miao Xiefacc8a222013-07-25 19:22:34 +08007135 if (!skip_sum && !write) {
7136 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
7137 sum_len = dio_bio->bi_size >> inode->i_sb->s_blocksize_bits;
7138 sum_len *= csum_size;
7139 } else {
7140 sum_len = 0;
7141 }
7142
7143 dip = kmalloc(sizeof(*dip) + sum_len, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04007144 if (!dip) {
7145 ret = -ENOMEM;
7146 goto free_io_bio;
7147 }
7148
7149 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007150 dip->inode = inode;
7151 dip->logical_offset = file_offset;
Miao Xiee6da5d22013-06-19 18:19:17 +08007152 dip->bytes = dio_bio->bi_size;
Chris Mason9be33952013-05-17 18:30:14 -04007153 dip->disk_bytenr = (u64)dio_bio->bi_sector << 9;
7154 io_bio->bi_private = dip;
Miao Xiee65e1532010-11-22 03:04:43 +00007155 dip->errors = 0;
Chris Mason9be33952013-05-17 18:30:14 -04007156 dip->orig_bio = io_bio;
7157 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00007158 atomic_set(&dip->pending_bios, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007159
7160 if (write)
Chris Mason9be33952013-05-17 18:30:14 -04007161 io_bio->bi_end_io = btrfs_endio_direct_write;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007162 else
Chris Mason9be33952013-05-17 18:30:14 -04007163 io_bio->bi_end_io = btrfs_endio_direct_read;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007164
Miao Xiee65e1532010-11-22 03:04:43 +00007165 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
7166 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04007167 return;
Chris Mason9be33952013-05-17 18:30:14 -04007168
7169free_io_bio:
7170 bio_put(io_bio);
7171
Josef Bacik4b46fce2010-05-23 11:00:55 -04007172free_ordered:
7173 /*
7174 * If this is a write, we need to clean up the reserved space and kill
7175 * the ordered extent.
7176 */
7177 if (write) {
7178 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05007179 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007180 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
7181 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
7182 btrfs_free_reserved_extent(root, ordered->start,
7183 ordered->disk_len);
7184 btrfs_put_ordered_extent(ordered);
7185 btrfs_put_ordered_extent(ordered);
7186 }
Chris Mason9be33952013-05-17 18:30:14 -04007187 bio_endio(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007188}
7189
Chris Mason5a5f79b2010-05-26 21:33:37 -04007190static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
7191 const struct iovec *iov, loff_t offset,
7192 unsigned long nr_segs)
7193{
7194 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00007195 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007196 size_t size;
7197 unsigned long addr;
7198 unsigned blocksize_mask = root->sectorsize - 1;
7199 ssize_t retval = -EINVAL;
7200 loff_t end = offset;
7201
7202 if (offset & blocksize_mask)
7203 goto out;
7204
7205 /* Check the memory alignment. Blocks cannot straddle pages */
7206 for (seg = 0; seg < nr_segs; seg++) {
7207 addr = (unsigned long)iov[seg].iov_base;
7208 size = iov[seg].iov_len;
7209 end += size;
Josef Bacika1b75f72011-04-08 15:51:18 +00007210 if ((addr & blocksize_mask) || (size & blocksize_mask))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007211 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00007212
7213 /* If this is a write we don't need to check anymore */
7214 if (rw & WRITE)
7215 continue;
7216
7217 /*
7218 * Check to make sure we don't have duplicate iov_base's in this
7219 * iovec, if so return EINVAL, otherwise we'll get csum errors
7220 * when reading back.
7221 */
7222 for (i = seg + 1; i < nr_segs; i++) {
7223 if (iov[seg].iov_base == iov[i].iov_base)
7224 goto out;
7225 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04007226 }
7227 retval = 0;
7228out:
7229 return retval;
7230}
Josef Bacikeb838e72012-07-31 16:28:48 -04007231
Chris Mason16432982008-04-10 10:23:21 -04007232static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
7233 const struct iovec *iov, loff_t offset,
7234 unsigned long nr_segs)
7235{
Josef Bacik4b46fce2010-05-23 11:00:55 -04007236 struct file *file = iocb->ki_filp;
7237 struct inode *inode = file->f_mapping->host;
Miao Xie09348562013-02-07 10:12:07 +00007238 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00007239 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00007240 bool wakeup = true;
7241 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00007242 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007243
Chris Mason5a5f79b2010-05-26 21:33:37 -04007244 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
Josef Bacikeb838e72012-07-31 16:28:48 -04007245 offset, nr_segs))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007246 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007247
Miao Xie38851cc2013-02-08 07:04:11 +00007248 atomic_inc(&inode->i_dio_count);
7249 smp_mb__after_atomic_inc();
7250
Josef Bacik0e267c42013-07-02 10:38:02 -04007251 /*
7252 * The generic stuff only does filemap_write_and_wait_range, which isn't
7253 * enough if we've written compressed pages to this area, so we need to
7254 * call btrfs_wait_ordered_range to make absolutely sure that any
7255 * outstanding dirty pages are on disk.
7256 */
7257 count = iov_length(iov, nr_segs);
7258 btrfs_wait_ordered_range(inode, offset, count);
7259
Miao Xie09348562013-02-07 10:12:07 +00007260 if (rw & WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00007261 /*
7262 * If the write DIO is beyond the EOF, we need update
7263 * the isize, but it is protected by i_mutex. So we can
7264 * not unlock the i_mutex at this case.
7265 */
7266 if (offset + count <= inode->i_size) {
7267 mutex_unlock(&inode->i_mutex);
7268 relock = true;
7269 }
Miao Xie09348562013-02-07 10:12:07 +00007270 ret = btrfs_delalloc_reserve_space(inode, count);
7271 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00007272 goto out;
7273 } else if (unlikely(test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
7274 &BTRFS_I(inode)->runtime_flags))) {
7275 inode_dio_done(inode);
7276 flags = DIO_LOCKING | DIO_SKIP_HOLES;
7277 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00007278 }
7279
7280 ret = __blockdev_direct_IO(rw, iocb, inode,
7281 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
7282 iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
Miao Xie2e60a512013-02-08 07:01:08 +00007283 btrfs_submit_direct, flags);
Miao Xie09348562013-02-07 10:12:07 +00007284 if (rw & WRITE) {
7285 if (ret < 0 && ret != -EIOCBQUEUED)
7286 btrfs_delalloc_release_space(inode, count);
Miao Xie172a5042013-02-21 02:48:22 -07007287 else if (ret >= 0 && (size_t)ret < count)
Miao Xie09348562013-02-07 10:12:07 +00007288 btrfs_delalloc_release_space(inode,
7289 count - (size_t)ret);
Miao Xie172a5042013-02-21 02:48:22 -07007290 else
7291 btrfs_delalloc_release_metadata(inode, 0);
Miao Xie09348562013-02-07 10:12:07 +00007292 }
Miao Xie38851cc2013-02-08 07:04:11 +00007293out:
Miao Xie2e60a512013-02-08 07:01:08 +00007294 if (wakeup)
7295 inode_dio_done(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00007296 if (relock)
7297 mutex_lock(&inode->i_mutex);
Miao Xie09348562013-02-07 10:12:07 +00007298
7299 return ret;
Chris Mason16432982008-04-10 10:23:21 -04007300}
7301
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007302#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
7303
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007304static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
7305 __u64 start, __u64 len)
7306{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007307 int ret;
7308
7309 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
7310 if (ret)
7311 return ret;
7312
Chris Masonec29ed52011-02-23 16:23:20 -05007313 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007314}
7315
Chris Mason9ebefb182007-06-15 13:50:00 -04007316int btrfs_readpage(struct file *file, struct page *page)
7317{
Chris Masond1310b22008-01-24 16:13:08 -05007318 struct extent_io_tree *tree;
7319 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02007320 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04007321}
Chris Mason1832a6d2007-12-21 16:27:21 -05007322
Chris Mason39279cc2007-06-12 06:35:45 -04007323static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
7324{
Chris Masond1310b22008-01-24 16:13:08 -05007325 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04007326
7327
7328 if (current->flags & PF_MEMALLOC) {
7329 redirty_page_for_writepage(wbc, page);
7330 unlock_page(page);
7331 return 0;
7332 }
Chris Masond1310b22008-01-24 16:13:08 -05007333 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04007334 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
7335}
Chris Mason39279cc2007-06-12 06:35:45 -04007336
Eric Sandeen48a3b632013-04-25 20:41:01 +00007337static int btrfs_writepages(struct address_space *mapping,
7338 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04007339{
Chris Masond1310b22008-01-24 16:13:08 -05007340 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05007341
Chris Masond1310b22008-01-24 16:13:08 -05007342 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04007343 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
7344}
7345
Chris Mason3ab2fb52007-11-08 10:59:22 -05007346static int
7347btrfs_readpages(struct file *file, struct address_space *mapping,
7348 struct list_head *pages, unsigned nr_pages)
7349{
Chris Masond1310b22008-01-24 16:13:08 -05007350 struct extent_io_tree *tree;
7351 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05007352 return extent_readpages(tree, mapping, pages, nr_pages,
7353 btrfs_get_extent);
7354}
Chris Masone6dcd2d2008-07-17 12:53:50 -04007355static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04007356{
Chris Masond1310b22008-01-24 16:13:08 -05007357 struct extent_io_tree *tree;
7358 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04007359 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04007360
Chris Masond1310b22008-01-24 16:13:08 -05007361 tree = &BTRFS_I(page->mapping->host)->io_tree;
7362 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05007363 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04007364 if (ret == 1) {
7365 ClearPagePrivate(page);
7366 set_page_private(page, 0);
7367 page_cache_release(page);
7368 }
7369 return ret;
7370}
Chris Mason39279cc2007-06-12 06:35:45 -04007371
Chris Masone6dcd2d2008-07-17 12:53:50 -04007372static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
7373{
Chris Mason98509cf2008-09-11 15:51:43 -04007374 if (PageWriteback(page) || PageDirty(page))
7375 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05007376 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007377}
7378
Lukas Czernerd47992f2013-05-21 23:17:23 -04007379static void btrfs_invalidatepage(struct page *page, unsigned int offset,
7380 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04007381{
Josef Bacik5fd02042012-05-02 14:00:54 -04007382 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05007383 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007384 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007385 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007386 u64 page_start = page_offset(page);
7387 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04007388
Chris Mason8b62b722009-09-02 16:53:46 -04007389 /*
7390 * we have the page locked, so new writeback can't start,
7391 * and the dirty bit won't be cleared while we are here.
7392 *
7393 * Wait for IO on this page so that we can safely clear
7394 * the PagePrivate2 bit and do ordered accounting
7395 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007396 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04007397
Josef Bacik5fd02042012-05-02 14:00:54 -04007398 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007399 if (offset) {
7400 btrfs_releasepage(page, GFP_NOFS);
7401 return;
7402 }
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007403 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Miao Xie4eee4fa2012-12-21 09:17:45 +00007404 ordered = btrfs_lookup_ordered_extent(inode, page_offset(page));
Chris Masone6dcd2d2008-07-17 12:53:50 -04007405 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04007406 /*
7407 * IO on this page will never be started, so we need
7408 * to account for any ordered extents now
7409 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007410 clear_extent_bit(tree, page_start, page_end,
7411 EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06007412 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
7413 EXTENT_DEFRAG, 1, 0, &cached_state, GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04007414 /*
7415 * whoever cleared the private bit is responsible
7416 * for the finish_ordered_io
7417 */
Josef Bacik5fd02042012-05-02 14:00:54 -04007418 if (TestClearPagePrivate2(page) &&
7419 btrfs_dec_test_ordered_pending(inode, &ordered, page_start,
7420 PAGE_CACHE_SIZE, 1)) {
7421 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04007422 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007423 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +00007424 cached_state = NULL;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007425 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007426 }
7427 clear_extent_bit(tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04007428 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06007429 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
7430 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007431 __btrfs_releasepage(page, GFP_NOFS);
7432
Chris Mason4a096752008-07-21 10:29:44 -04007433 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04007434 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04007435 ClearPagePrivate(page);
7436 set_page_private(page, 0);
7437 page_cache_release(page);
7438 }
Chris Mason39279cc2007-06-12 06:35:45 -04007439}
7440
Chris Mason9ebefb182007-06-15 13:50:00 -04007441/*
7442 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
7443 * called from a page fault handler when a page is first dirtied. Hence we must
7444 * be careful to check for EOF conditions here. We set the page up correctly
7445 * for a written page which means we get ENOSPC checking when writing into
7446 * holes and correct delalloc and unwritten extent mapping on filesystems that
7447 * support these features.
7448 *
7449 * We are not allowed to take the i_mutex here so we have to play games to
7450 * protect against truncate races as the page could now be beyond EOF. Because
7451 * vmtruncate() writes the inode size before removing pages, once we have the
7452 * page lock we can determine safely if the page is beyond EOF. If it is not
7453 * beyond EOF, then the page is guaranteed safe against truncation until we
7454 * unlock the page.
7455 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07007456int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04007457{
Nick Pigginc2ec1752009-03-31 15:23:21 -07007458 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05007459 struct inode *inode = file_inode(vma->vm_file);
Chris Mason1832a6d2007-12-21 16:27:21 -05007460 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007461 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7462 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007463 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007464 char *kaddr;
7465 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04007466 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05007467 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05007468 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007469 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007470 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04007471
Jan Karab2b5ef52012-06-12 16:20:45 +02007472 sb_start_pagefault(inode->i_sb);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04007473 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007474 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04007475 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05007476 reserved = 1;
7477 }
Nick Piggin56a76f82009-03-31 15:23:23 -07007478 if (ret) {
7479 if (ret == -ENOMEM)
7480 ret = VM_FAULT_OOM;
7481 else /* -ENOSPC, -EIO, etc */
7482 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05007483 if (reserved)
7484 goto out;
7485 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07007486 }
Chris Mason1832a6d2007-12-21 16:27:21 -05007487
Nick Piggin56a76f82009-03-31 15:23:23 -07007488 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007489again:
Chris Mason9ebefb182007-06-15 13:50:00 -04007490 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04007491 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007492 page_start = page_offset(page);
7493 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04007494
Chris Mason9ebefb182007-06-15 13:50:00 -04007495 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04007496 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04007497 /* page got truncated out from underneath us */
7498 goto out_unlock;
7499 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007500 wait_on_page_writeback(page);
7501
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007502 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007503 set_page_extent_mapped(page);
7504
Chris Masoneb84ae02008-07-17 13:53:27 -04007505 /*
7506 * we can't set the delalloc bits if there are pending ordered
7507 * extents. Drop our locks and wait for them to finish
7508 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007509 ordered = btrfs_lookup_ordered_extent(inode, page_start);
7510 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007511 unlock_extent_cached(io_tree, page_start, page_end,
7512 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007513 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04007514 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007515 btrfs_put_ordered_extent(ordered);
7516 goto again;
7517 }
7518
Josef Bacikfbf19082009-10-01 17:10:23 -04007519 /*
7520 * XXX - page_mkwrite gets called every time the page is dirtied, even
7521 * if it was already dirty, so for space accounting reasons we need to
7522 * clear any delalloc bits for the range we are fixing to save. There
7523 * is probably a better way to do this, but for now keep consistent with
7524 * prepare_pages in the normal write path.
7525 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00007526 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06007527 EXTENT_DIRTY | EXTENT_DELALLOC |
7528 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00007529 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04007530
Josef Bacik2ac55d42010-02-03 19:33:23 +00007531 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
7532 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007533 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007534 unlock_extent_cached(io_tree, page_start, page_end,
7535 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007536 ret = VM_FAULT_SIGBUS;
7537 goto out_unlock;
7538 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007539 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04007540
7541 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04007542 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04007543 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04007544 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04007545 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04007546
Chris Masone6dcd2d2008-07-17 12:53:50 -04007547 if (zero_start != PAGE_CACHE_SIZE) {
7548 kaddr = kmap(page);
7549 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
7550 flush_dcache_page(page);
7551 kunmap(page);
7552 }
Chris Mason247e7432008-07-17 12:53:51 -04007553 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007554 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04007555 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007556
Chris Mason257c62e2009-10-13 13:21:08 -04007557 BTRFS_I(inode)->last_trans = root->fs_info->generation;
7558 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06007559 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04007560
Josef Bacik2ac55d42010-02-03 19:33:23 +00007561 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04007562
7563out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02007564 if (!ret) {
7565 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04007566 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02007567 }
Chris Mason9ebefb182007-06-15 13:50:00 -04007568 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05007569out:
Josef Bacikec39e182012-01-12 19:10:12 -05007570 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007571out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02007572 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04007573 return ret;
7574}
7575
Josef Bacika41ad392011-01-31 15:30:16 -05007576static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04007577{
7578 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04007579 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04007580 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05007581 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007582 struct btrfs_trans_handle *trans;
Chris Masondbe674a2008-07-17 12:54:05 -04007583 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04007584 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04007585
Chris Mason4a096752008-07-21 10:29:44 -04007586 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Yan, Zheng80825102009-11-12 09:35:36 +00007587 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007588
Josef Bacikfcb80c22011-05-03 10:40:22 -04007589 /*
7590 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
7591 * 3 things going on here
7592 *
7593 * 1) We need to reserve space for our orphan item and the space to
7594 * delete our orphan item. Lord knows we don't want to have a dangling
7595 * orphan item because we didn't reserve space to remove it.
7596 *
7597 * 2) We need to reserve space to update our inode.
7598 *
7599 * 3) We need to have something to cache all the space that is going to
7600 * be free'd up by the truncate operation, but also have some slack
7601 * space reserved in case it uses space during the truncate (thank you
7602 * very much snapshotting).
7603 *
7604 * And we need these to all be seperate. The fact is we can use alot of
7605 * space doing the truncate, and we have no earthly idea how much space
7606 * we will use, so we need the truncate reservation to be seperate so it
7607 * doesn't end up using space reserved for updating the inode or
7608 * removing the orphan item. We also need to be able to stop the
7609 * transaction and start a new one, which means we need to be able to
7610 * update the inode several times, and we have no idea of knowing how
7611 * many times that will be, so we can't just reserve 1 item for the
7612 * entirety of the opration, so that has to be done seperately as well.
7613 * Then there is the orphan item, which does indeed need to be held on
7614 * to for the whole operation, and we need nobody to touch this reserved
7615 * space except the orphan code.
7616 *
7617 * So that leaves us with
7618 *
7619 * 1) root->orphan_block_rsv - for the orphan deletion.
7620 * 2) rsv - for the truncate reservation, which we will steal from the
7621 * transaction reservation.
7622 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
7623 * updating the inode.
7624 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06007625 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007626 if (!rsv)
7627 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04007628 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04007629 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05007630
Josef Bacik907cbce2011-08-08 13:46:15 -04007631 /*
Josef Bacik07127182011-08-19 10:29:59 -04007632 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04007633 * 1 for updating the inode.
7634 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05007635 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007636 if (IS_ERR(trans)) {
7637 err = PTR_ERR(trans);
7638 goto out;
7639 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05007640
Josef Bacik907cbce2011-08-08 13:46:15 -04007641 /* Migrate the slack space for the truncate to our reserve */
7642 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
7643 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007644 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05007645
Chris Mason5a3f23d2009-03-31 13:27:11 -04007646 /*
7647 * setattr is responsible for setting the ordered_data_close flag,
7648 * but that is only tested during the last file release. That
7649 * could happen well after the next commit, leaving a great big
7650 * window where new writes may get lost if someone chooses to write
7651 * to this file after truncating to zero
7652 *
7653 * The inode doesn't have any dirty data here, and so if we commit
7654 * this is a noop. If someone immediately starts writing to the inode
7655 * it is very likely we'll catch some of their writes in this
7656 * transaction, and the commit will find this file on the ordered
7657 * data list with good things to send down.
7658 *
7659 * This is a best effort solution, there is still a window where
7660 * using truncate to replace the contents of the file will
7661 * end up with a zero length file after a crash.
7662 */
Josef Bacik72ac3c02012-05-23 14:13:11 -04007663 if (inode->i_size == 0 && test_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
7664 &BTRFS_I(inode)->runtime_flags))
Chris Mason5a3f23d2009-03-31 13:27:11 -04007665 btrfs_add_ordered_operation(trans, root, inode);
7666
Josef Bacik5dc562c2012-08-17 13:14:17 -04007667 /*
7668 * So if we truncate and then write and fsync we normally would just
7669 * write the extents that changed, which is a problem if we need to
7670 * first truncate that entire inode. So set this flag so we write out
7671 * all of the extents in the inode to the sync log so we're completely
7672 * safe.
7673 */
7674 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007675 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04007676
Yan, Zheng80825102009-11-12 09:35:36 +00007677 while (1) {
7678 ret = btrfs_truncate_inode_items(trans, root, inode,
7679 inode->i_size,
7680 BTRFS_EXTENT_DATA_KEY);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007681 if (ret != -ENOSPC) {
Josef Bacik3893e332011-01-31 16:03:11 -05007682 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007683 break;
Josef Bacik3893e332011-01-31 16:03:11 -05007684 }
Chris Mason39279cc2007-06-12 06:35:45 -04007685
Josef Bacikfcb80c22011-05-03 10:40:22 -04007686 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007687 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007688 if (ret) {
7689 err = ret;
7690 break;
7691 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04007692
Yan, Zheng80825102009-11-12 09:35:36 +00007693 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007694 btrfs_btree_balance_dirty(root);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007695
7696 trans = btrfs_start_transaction(root, 2);
7697 if (IS_ERR(trans)) {
7698 ret = err = PTR_ERR(trans);
7699 trans = NULL;
7700 break;
7701 }
7702
7703 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
7704 rsv, min_size);
7705 BUG_ON(ret); /* shouldn't happen */
7706 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007707 }
7708
7709 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04007710 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007711 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007712 if (ret)
7713 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007714 }
7715
Chris Mason917c16b2011-11-08 14:49:59 -05007716 if (trans) {
7717 trans->block_rsv = &root->fs_info->trans_block_rsv;
7718 ret = btrfs_update_inode(trans, root, inode);
7719 if (ret && !err)
7720 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04007721
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007722 ret = btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007723 btrfs_btree_balance_dirty(root);
Chris Mason917c16b2011-11-08 14:49:59 -05007724 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04007725
7726out:
7727 btrfs_free_block_rsv(root, rsv);
7728
Josef Bacik3893e332011-01-31 16:03:11 -05007729 if (ret && !err)
7730 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05007731
Josef Bacik3893e332011-01-31 16:03:11 -05007732 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007733}
7734
Sven Wegener3b963622008-06-09 21:57:42 -04007735/*
Chris Masond352ac62008-09-29 15:18:18 -04007736 * create a new subvolume directory/inode (helper for the ioctl).
7737 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05007738int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Josef Bacikd82a6f12011-05-11 15:26:06 -04007739 struct btrfs_root *new_root, u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04007740{
Chris Mason39279cc2007-06-12 06:35:45 -04007741 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04007742 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007743 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007744
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01007745 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
7746 new_dirid, new_dirid,
7747 S_IFDIR | (~current_umask() & S_IRWXUGO),
7748 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04007749 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04007750 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007751 inode->i_op = &btrfs_dir_inode_operations;
7752 inode->i_fop = &btrfs_dir_file_operations;
7753
Miklos Szeredibfe86842011-10-28 14:13:29 +02007754 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04007755 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04007756
Yan, Zheng76dda932009-09-21 16:00:26 -04007757 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04007758
Yan, Zheng76dda932009-09-21 16:00:26 -04007759 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07007760 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007761}
7762
Chris Mason39279cc2007-06-12 06:35:45 -04007763struct inode *btrfs_alloc_inode(struct super_block *sb)
7764{
7765 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007766 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007767
7768 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
7769 if (!ei)
7770 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007771
7772 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007773 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04007774 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04007775 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04007776 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007777 ei->delalloc_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007778 ei->disk_i_size = 0;
7779 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04007780 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007781 ei->index_cnt = (u64)-1;
7782 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06007783 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007784
Josef Bacik9e0baf62011-07-15 15:16:44 +00007785 spin_lock_init(&ei->lock);
7786 ei->outstanding_extents = 0;
7787 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007788
Josef Bacik72ac3c02012-05-23 14:13:11 -04007789 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08007790 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007791
Miao Xie16cdcec2011-04-22 18:12:22 +08007792 ei->delayed_node = NULL;
7793
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007794 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02007795 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02007796 extent_io_tree_init(&ei->io_tree, &inode->i_data);
7797 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04007798 ei->io_tree.track_uptodate = 1;
7799 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05007800 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007801 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05007802 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007803 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007804 INIT_LIST_HEAD(&ei->delalloc_inodes);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007805 INIT_LIST_HEAD(&ei->ordered_operations);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007806 RB_CLEAR_NODE(&ei->rb_node);
7807
7808 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007809}
7810
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007811static void btrfs_i_callback(struct rcu_head *head)
7812{
7813 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007814 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
7815}
7816
Chris Mason39279cc2007-06-12 06:35:45 -04007817void btrfs_destroy_inode(struct inode *inode)
7818{
Chris Masone6dcd2d2008-07-17 12:53:50 -04007819 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007820 struct btrfs_root *root = BTRFS_I(inode)->root;
7821
Al Virob3d9b7a2012-06-09 13:51:19 -04007822 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04007823 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00007824 WARN_ON(BTRFS_I(inode)->outstanding_extents);
7825 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04007826 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
7827 WARN_ON(BTRFS_I(inode)->csum_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04007828
Chris Mason5a3f23d2009-03-31 13:27:11 -04007829 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05007830 * This can happen where we create an inode, but somebody else also
7831 * created the same inode and we need to destroy the one we already
7832 * created.
7833 */
7834 if (!root)
7835 goto free;
7836
7837 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04007838 * Make sure we're properly removed from the ordered operation
7839 * lists.
7840 */
7841 smp_mb();
7842 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
Miao Xie199c2a92013-05-15 07:48:23 +00007843 spin_lock(&root->fs_info->ordered_root_lock);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007844 list_del_init(&BTRFS_I(inode)->ordered_operations);
Miao Xie199c2a92013-05-15 07:48:23 +00007845 spin_unlock(&root->fs_info->ordered_root_lock);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007846 }
7847
Josef Bacik8a35d952012-05-23 14:26:42 -04007848 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
7849 &BTRFS_I(inode)->runtime_flags)) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00007850 btrfs_info(root->fs_info, "inode %llu still on the orphan list",
7851 (unsigned long long)btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04007852 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04007853 }
Josef Bacik7b128762008-07-24 12:17:14 -04007854
Chris Masond3977122009-01-05 21:25:51 -05007855 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04007856 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
7857 if (!ordered)
7858 break;
7859 else {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00007860 btrfs_err(root->fs_info, "found ordered extent %llu %llu on inode cleanup",
7861 (unsigned long long)ordered->file_offset,
7862 (unsigned long long)ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007863 btrfs_remove_ordered_extent(inode, ordered);
7864 btrfs_put_ordered_extent(ordered);
7865 btrfs_put_ordered_extent(ordered);
7866 }
7867 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007868 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007869 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05007870free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007871 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04007872}
7873
Al Viro45321ac2010-06-07 13:43:19 -04007874int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04007875{
7876 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04007877
Naohiro Aota6379ef92013-06-06 09:56:34 +00007878 if (root == NULL)
7879 return 1;
7880
Liu Bofa6ac872013-02-20 14:10:23 +00007881 /* the snap/subvol tree is on deleting */
Josef Bacik0af3d002010-06-21 14:48:16 -04007882 if (btrfs_root_refs(&root->root_item) == 0 &&
Liu Bofa6ac872013-02-20 14:10:23 +00007883 root != root->fs_info->tree_root)
Al Viro45321ac2010-06-07 13:43:19 -04007884 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04007885 else
Al Viro45321ac2010-06-07 13:43:19 -04007886 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04007887}
7888
Sven Wegener0ee0fda2008-07-30 16:54:26 -04007889static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04007890{
7891 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
7892
7893 inode_init_once(&ei->vfs_inode);
7894}
7895
7896void btrfs_destroy_cachep(void)
7897{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10007898 /*
7899 * Make sure all delayed rcu free inodes are flushed before we
7900 * destroy cache.
7901 */
7902 rcu_barrier();
Chris Mason39279cc2007-06-12 06:35:45 -04007903 if (btrfs_inode_cachep)
7904 kmem_cache_destroy(btrfs_inode_cachep);
7905 if (btrfs_trans_handle_cachep)
7906 kmem_cache_destroy(btrfs_trans_handle_cachep);
7907 if (btrfs_transaction_cachep)
7908 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04007909 if (btrfs_path_cachep)
7910 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05007911 if (btrfs_free_space_cachep)
7912 kmem_cache_destroy(btrfs_free_space_cachep);
Miao Xie8ccf6f192012-10-25 09:28:04 +00007913 if (btrfs_delalloc_work_cachep)
7914 kmem_cache_destroy(btrfs_delalloc_work_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04007915}
7916
7917int btrfs_init_cachep(void)
7918{
David Sterba837e1972012-09-07 03:00:48 -06007919 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007920 sizeof(struct btrfs_inode), 0,
7921 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04007922 if (!btrfs_inode_cachep)
7923 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007924
David Sterba837e1972012-09-07 03:00:48 -06007925 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007926 sizeof(struct btrfs_trans_handle), 0,
7927 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007928 if (!btrfs_trans_handle_cachep)
7929 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007930
David Sterba837e1972012-09-07 03:00:48 -06007931 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007932 sizeof(struct btrfs_transaction), 0,
7933 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007934 if (!btrfs_transaction_cachep)
7935 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007936
David Sterba837e1972012-09-07 03:00:48 -06007937 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007938 sizeof(struct btrfs_path), 0,
7939 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007940 if (!btrfs_path_cachep)
7941 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007942
David Sterba837e1972012-09-07 03:00:48 -06007943 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05007944 sizeof(struct btrfs_free_space), 0,
7945 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
7946 if (!btrfs_free_space_cachep)
7947 goto fail;
7948
Miao Xie8ccf6f192012-10-25 09:28:04 +00007949 btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
7950 sizeof(struct btrfs_delalloc_work), 0,
7951 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
7952 NULL);
7953 if (!btrfs_delalloc_work_cachep)
7954 goto fail;
7955
Chris Mason39279cc2007-06-12 06:35:45 -04007956 return 0;
7957fail:
7958 btrfs_destroy_cachep();
7959 return -ENOMEM;
7960}
7961
7962static int btrfs_getattr(struct vfsmount *mnt,
7963 struct dentry *dentry, struct kstat *stat)
7964{
Miao Xiedf0af1a2013-01-29 10:11:59 +00007965 u64 delalloc_bytes;
Chris Mason39279cc2007-06-12 06:35:45 -04007966 struct inode *inode = dentry->d_inode;
David Sterbafadc0d82011-11-20 07:33:38 -05007967 u32 blocksize = inode->i_sb->s_blocksize;
7968
Chris Mason39279cc2007-06-12 06:35:45 -04007969 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04007970 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05007971 stat->blksize = PAGE_CACHE_SIZE;
Miao Xiedf0af1a2013-01-29 10:11:59 +00007972
7973 spin_lock(&BTRFS_I(inode)->lock);
7974 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
7975 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05007976 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00007977 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04007978 return 0;
7979}
7980
Chris Masond3977122009-01-05 21:25:51 -05007981static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7982 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04007983{
7984 struct btrfs_trans_handle *trans;
7985 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007986 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04007987 struct inode *new_inode = new_dentry->d_inode;
7988 struct inode *old_inode = old_dentry->d_inode;
7989 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007990 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007991 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04007992 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08007993 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007994
Li Zefan33345d012011-04-20 10:31:50 +08007995 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04007996 return -EPERM;
7997
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007998 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08007999 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05008000 return -EXDEV;
8001
Li Zefan33345d012011-04-20 10:31:50 +08008002 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
8003 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008004 return -ENOTEMPTY;
8005
Chris Mason39279cc2007-06-12 06:35:45 -04008006 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008007 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04008008 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05008009
8010
8011 /* check for collisions, even if the name isn't there */
8012 ret = btrfs_check_dir_item_collision(root, new_dir->i_ino,
8013 new_dentry->d_name.name,
8014 new_dentry->d_name.len);
8015
8016 if (ret) {
8017 if (ret == -EEXIST) {
8018 /* we shouldn't get
8019 * eexist without a new_inode */
8020 if (!new_inode) {
8021 WARN_ON(1);
8022 return ret;
8023 }
8024 } else {
8025 /* maybe -EOVERFLOW */
8026 return ret;
8027 }
8028 }
8029 ret = 0;
8030
Chris Mason5a3f23d2009-03-31 13:27:11 -04008031 /*
8032 * we're using rename to replace one file with another.
8033 * and the replacement file is large. Start IO on it now so
8034 * we don't add too much work to the end of the transaction
8035 */
Bartlomiej Zolnierkiewicz4baf8c92009-08-07 13:47:08 -04008036 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
Chris Mason5a3f23d2009-03-31 13:27:11 -04008037 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
8038 filemap_flush(old_inode->i_mapping);
8039
Yan, Zheng76dda932009-09-21 16:00:26 -04008040 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08008041 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008042 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008043 /*
8044 * We want to reserve the absolute worst case amount of items. So if
8045 * both inodes are subvols and we need to unlink them then that would
8046 * require 4 item modifications, but if they are both normal inodes it
8047 * would require 5 item modifications, so we'll assume their normal
8048 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
8049 * should cover the worst case number of items we'll modify.
8050 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04008051 trans = btrfs_start_transaction(root, 11);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008052 if (IS_ERR(trans)) {
8053 ret = PTR_ERR(trans);
8054 goto out_notrans;
8055 }
Chris Mason5f39d392007-10-15 16:14:19 -04008056
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008057 if (dest != root)
8058 btrfs_record_root_in_trans(trans, dest);
8059
Yan, Zhenga5719522009-09-24 09:17:31 -04008060 ret = btrfs_set_inode_index(new_dir, &index);
8061 if (ret)
8062 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008063
Li Zefan33345d012011-04-20 10:31:50 +08008064 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008065 /* force full log commit if subvolume involved. */
8066 root->fs_info->last_trans_log_full_commit = trans->transid;
8067 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04008068 ret = btrfs_insert_inode_ref(trans, dest,
8069 new_dentry->d_name.name,
8070 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08008071 old_ino,
8072 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04008073 if (ret)
8074 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008075 /*
8076 * this is an ugly little race, but the rename is required
8077 * to make sure that if we crash, the inode is either at the
8078 * old name or the new one. pinning the log transaction lets
8079 * us make sure we don't allow a log commit to come in after
8080 * we unlink the name but before we add the new name back in.
8081 */
8082 btrfs_pin_log_trans(root);
8083 }
Chris Mason12fcfd22009-03-24 10:24:20 -04008084 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04008085 * make sure the inode gets flushed if it is replacing
8086 * something.
8087 */
Li Zefan33345d012011-04-20 10:31:50 +08008088 if (new_inode && new_inode->i_size && S_ISREG(old_inode->i_mode))
Chris Mason5a3f23d2009-03-31 13:27:11 -04008089 btrfs_add_ordered_operation(trans, root, old_inode);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008090
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008091 inode_inc_iversion(old_dir);
8092 inode_inc_iversion(new_dir);
8093 inode_inc_iversion(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008094 old_dir->i_ctime = old_dir->i_mtime = ctime;
8095 new_dir->i_ctime = new_dir->i_mtime = ctime;
8096 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04008097
Chris Mason12fcfd22009-03-24 10:24:20 -04008098 if (old_dentry->d_parent != new_dentry->d_parent)
8099 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
8100
Li Zefan33345d012011-04-20 10:31:50 +08008101 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008102 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
8103 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
8104 old_dentry->d_name.name,
8105 old_dentry->d_name.len);
8106 } else {
Al Viro92986792011-03-04 17:14:37 +00008107 ret = __btrfs_unlink_inode(trans, root, old_dir,
8108 old_dentry->d_inode,
8109 old_dentry->d_name.name,
8110 old_dentry->d_name.len);
8111 if (!ret)
8112 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008113 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008114 if (ret) {
8115 btrfs_abort_transaction(trans, root, ret);
8116 goto out_fail;
8117 }
Chris Mason39279cc2007-06-12 06:35:45 -04008118
8119 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008120 inode_inc_iversion(new_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008121 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08008122 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008123 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
8124 root_objectid = BTRFS_I(new_inode)->location.objectid;
8125 ret = btrfs_unlink_subvol(trans, dest, new_dir,
8126 root_objectid,
8127 new_dentry->d_name.name,
8128 new_dentry->d_name.len);
8129 BUG_ON(new_inode->i_nlink == 0);
8130 } else {
8131 ret = btrfs_unlink_inode(trans, dest, new_dir,
8132 new_dentry->d_inode,
8133 new_dentry->d_name.name,
8134 new_dentry->d_name.len);
8135 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04008136 if (!ret && new_inode->i_nlink == 0)
Chris Masone02119d2008-09-05 16:13:11 -04008137 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008138 if (ret) {
8139 btrfs_abort_transaction(trans, root, ret);
8140 goto out_fail;
8141 }
Chris Mason39279cc2007-06-12 06:35:45 -04008142 }
Josef Bacikaec74772008-07-24 12:12:38 -04008143
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008144 ret = btrfs_add_link(trans, new_dir, old_inode,
8145 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04008146 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008147 if (ret) {
8148 btrfs_abort_transaction(trans, root, ret);
8149 goto out_fail;
8150 }
Chris Mason39279cc2007-06-12 06:35:45 -04008151
Li Zefan33345d012011-04-20 10:31:50 +08008152 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04008153 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00008154 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008155 btrfs_end_log_trans(root);
8156 }
Chris Mason39279cc2007-06-12 06:35:45 -04008157out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008158 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008159out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08008160 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008161 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008162
Chris Mason39279cc2007-06-12 06:35:45 -04008163 return ret;
8164}
8165
Miao Xie8ccf6f192012-10-25 09:28:04 +00008166static void btrfs_run_delalloc_work(struct btrfs_work *work)
8167{
8168 struct btrfs_delalloc_work *delalloc_work;
8169
8170 delalloc_work = container_of(work, struct btrfs_delalloc_work,
8171 work);
8172 if (delalloc_work->wait)
8173 btrfs_wait_ordered_range(delalloc_work->inode, 0, (u64)-1);
8174 else
8175 filemap_flush(delalloc_work->inode->i_mapping);
8176
8177 if (delalloc_work->delay_iput)
8178 btrfs_add_delayed_iput(delalloc_work->inode);
8179 else
8180 iput(delalloc_work->inode);
8181 complete(&delalloc_work->completion);
8182}
8183
8184struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
8185 int wait, int delay_iput)
8186{
8187 struct btrfs_delalloc_work *work;
8188
8189 work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
8190 if (!work)
8191 return NULL;
8192
8193 init_completion(&work->completion);
8194 INIT_LIST_HEAD(&work->list);
8195 work->inode = inode;
8196 work->wait = wait;
8197 work->delay_iput = delay_iput;
8198 work->work.func = btrfs_run_delalloc_work;
8199
8200 return work;
8201}
8202
8203void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
8204{
8205 wait_for_completion(&work->completion);
8206 kmem_cache_free(btrfs_delalloc_work_cachep, work);
8207}
8208
Chris Masond352ac62008-09-29 15:18:18 -04008209/*
8210 * some fairly slow code that needs optimization. This walks the list
8211 * of all the inodes with pending delalloc and forces them to disk.
8212 */
Miao Xieeb73c1b2013-05-15 07:48:22 +00008213static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
Chris Masonea8c2812008-08-04 23:17:27 -04008214{
Chris Masonea8c2812008-08-04 23:17:27 -04008215 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008216 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008217 struct btrfs_delalloc_work *work, *next;
8218 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00008219 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008220 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04008221
Miao Xie8ccf6f192012-10-25 09:28:04 +00008222 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008223 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00008224
Miao Xieeb73c1b2013-05-15 07:48:22 +00008225 spin_lock(&root->delalloc_lock);
8226 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008227 while (!list_empty(&splice)) {
8228 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04008229 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008230
Miao Xieeb73c1b2013-05-15 07:48:22 +00008231 list_move_tail(&binode->delalloc_inodes,
8232 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008233 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00008234 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00008235 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008236 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008237 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008238 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008239
8240 work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
8241 if (unlikely(!work)) {
8242 ret = -ENOMEM;
8243 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008244 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008245 list_add_tail(&work->list, &works);
8246 btrfs_queue_worker(&root->fs_info->flush_workers,
8247 &work->work);
8248
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008249 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00008250 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04008251 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008252 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04008253
Miao Xie1eafa6c2013-01-22 10:49:00 +00008254 list_for_each_entry_safe(work, next, &works, list) {
8255 list_del_init(&work->list);
8256 btrfs_wait_and_free_delalloc_work(work);
8257 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008258 return 0;
8259out:
8260 list_for_each_entry_safe(work, next, &works, list) {
8261 list_del_init(&work->list);
8262 btrfs_wait_and_free_delalloc_work(work);
8263 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008264
Miao Xieeb73c1b2013-05-15 07:48:22 +00008265 if (!list_empty_careful(&splice)) {
8266 spin_lock(&root->delalloc_lock);
8267 list_splice_tail(&splice, &root->delalloc_inodes);
8268 spin_unlock(&root->delalloc_lock);
8269 }
8270 return ret;
8271}
8272
8273int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
8274{
8275 int ret;
8276
8277 if (root->fs_info->sb->s_flags & MS_RDONLY)
8278 return -EROFS;
8279
8280 ret = __start_delalloc_inodes(root, delay_iput);
8281 /*
8282 * the filemap_flush will queue IO into the worker threads, but
Chris Mason8c8bee12008-09-29 11:19:10 -04008283 * we have to make sure the IO is actually started and that
8284 * ordered extents get created before we return
8285 */
8286 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05008287 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05008288 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04008289 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05008290 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
8291 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04008292 }
8293 atomic_dec(&root->fs_info->async_submit_draining);
Miao Xieeb73c1b2013-05-15 07:48:22 +00008294 return ret;
8295}
8296
8297int btrfs_start_all_delalloc_inodes(struct btrfs_fs_info *fs_info,
8298 int delay_iput)
8299{
8300 struct btrfs_root *root;
8301 struct list_head splice;
8302 int ret;
8303
8304 if (fs_info->sb->s_flags & MS_RDONLY)
8305 return -EROFS;
8306
8307 INIT_LIST_HEAD(&splice);
8308
8309 spin_lock(&fs_info->delalloc_root_lock);
8310 list_splice_init(&fs_info->delalloc_roots, &splice);
8311 while (!list_empty(&splice)) {
8312 root = list_first_entry(&splice, struct btrfs_root,
8313 delalloc_root);
8314 root = btrfs_grab_fs_root(root);
8315 BUG_ON(!root);
8316 list_move_tail(&root->delalloc_root,
8317 &fs_info->delalloc_roots);
8318 spin_unlock(&fs_info->delalloc_root_lock);
8319
8320 ret = __start_delalloc_inodes(root, delay_iput);
8321 btrfs_put_fs_root(root);
8322 if (ret)
8323 goto out;
8324
8325 spin_lock(&fs_info->delalloc_root_lock);
8326 }
8327 spin_unlock(&fs_info->delalloc_root_lock);
8328
8329 atomic_inc(&fs_info->async_submit_draining);
8330 while (atomic_read(&fs_info->nr_async_submits) ||
8331 atomic_read(&fs_info->async_delalloc_pages)) {
8332 wait_event(fs_info->async_submit_wait,
8333 (atomic_read(&fs_info->nr_async_submits) == 0 &&
8334 atomic_read(&fs_info->async_delalloc_pages) == 0));
8335 }
8336 atomic_dec(&fs_info->async_submit_draining);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008337 return 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008338out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00008339 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00008340 spin_lock(&fs_info->delalloc_root_lock);
8341 list_splice_tail(&splice, &fs_info->delalloc_roots);
8342 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008343 }
Miao Xie8ccf6f192012-10-25 09:28:04 +00008344 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04008345}
8346
Chris Mason39279cc2007-06-12 06:35:45 -04008347static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
8348 const char *symname)
8349{
8350 struct btrfs_trans_handle *trans;
8351 struct btrfs_root *root = BTRFS_I(dir)->root;
8352 struct btrfs_path *path;
8353 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05008354 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04008355 int err;
8356 int drop_inode = 0;
8357 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008358 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04008359 int name_len;
8360 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04008361 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04008362 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04008363 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04008364
8365 name_len = strlen(symname) + 1;
8366 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
8367 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05008368
Josef Bacik9ed74f22009-09-11 16:12:44 -04008369 /*
8370 * 2 items for inode item and ref
8371 * 2 items for dir items
8372 * 1 item for xattr if selinux is on
8373 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04008374 trans = btrfs_start_transaction(root, 5);
8375 if (IS_ERR(trans))
8376 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05008377
Li Zefan581bb052011-04-20 10:06:11 +08008378 err = btrfs_find_free_ino(root, &objectid);
8379 if (err)
8380 goto out_unlock;
8381
Josef Bacikaec74772008-07-24 12:12:38 -04008382 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08008383 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04008384 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008385 if (IS_ERR(inode)) {
8386 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008387 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008388 }
Chris Mason39279cc2007-06-12 06:35:45 -04008389
Eric Paris2a7dba32011-02-01 11:05:39 -05008390 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04008391 if (err) {
8392 drop_inode = 1;
8393 goto out_unlock;
8394 }
8395
Casey Schauflerad19db72011-12-15 10:09:07 -05008396 /*
8397 * If the active LSM wants to access the inode during
8398 * d_instantiate it needs these. Smack checks to see
8399 * if the filesystem supports xattrs by looking at the
8400 * ops vector.
8401 */
8402 inode->i_fop = &btrfs_file_operations;
8403 inode->i_op = &btrfs_file_inode_operations;
8404
Josef Bacika1b075d2010-11-19 20:36:11 +00008405 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04008406 if (err)
8407 drop_inode = 1;
8408 else {
8409 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04008410 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05008411 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04008412 }
Chris Mason39279cc2007-06-12 06:35:45 -04008413 if (drop_inode)
8414 goto out_unlock;
8415
8416 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07008417 if (!path) {
8418 err = -ENOMEM;
8419 drop_inode = 1;
8420 goto out_unlock;
8421 }
Li Zefan33345d012011-04-20 10:31:50 +08008422 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008423 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008424 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
8425 datasize = btrfs_file_extent_calc_inline_size(name_len);
8426 err = btrfs_insert_empty_item(trans, root, path, &key,
8427 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04008428 if (err) {
8429 drop_inode = 1;
Julia Lawallb0839162011-05-14 07:10:51 +00008430 btrfs_free_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04008431 goto out_unlock;
8432 }
Chris Mason5f39d392007-10-15 16:14:19 -04008433 leaf = path->nodes[0];
8434 ei = btrfs_item_ptr(leaf, path->slots[0],
8435 struct btrfs_file_extent_item);
8436 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
8437 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04008438 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04008439 btrfs_set_file_extent_encryption(leaf, ei, 0);
8440 btrfs_set_file_extent_compression(leaf, ei, 0);
8441 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
8442 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
8443
Chris Mason39279cc2007-06-12 06:35:45 -04008444 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04008445 write_extent_buffer(leaf, symname, ptr, name_len);
8446 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04008447 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04008448
Chris Mason39279cc2007-06-12 06:35:45 -04008449 inode->i_op = &btrfs_symlink_inode_operations;
8450 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04008451 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04008452 inode_set_bytes(inode, name_len);
Chris Masondbe674a2008-07-17 12:54:05 -04008453 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04008454 err = btrfs_update_inode(trans, root, inode);
8455 if (err)
8456 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04008457
8458out_unlock:
Al Viro08c422c2011-12-23 07:58:13 -05008459 if (!err)
8460 d_instantiate(dentry, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008461 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04008462 if (drop_inode) {
8463 inode_dec_link_count(inode);
8464 iput(inode);
8465 }
Liu Bob53d3f52012-11-14 14:34:34 +00008466 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04008467 return err;
8468}
Chris Mason16432982008-04-10 10:23:21 -04008469
Josef Bacik0af3d002010-06-21 14:48:16 -04008470static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
8471 u64 start, u64 num_bytes, u64 min_size,
8472 loff_t actual_len, u64 *alloc_hint,
8473 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04008474{
Josef Bacik5dc562c2012-08-17 13:14:17 -04008475 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
8476 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04008477 struct btrfs_root *root = BTRFS_I(inode)->root;
8478 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04008479 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00008480 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05008481 u64 cur_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04008482 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04008483 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04008484
Josef Bacik0af3d002010-06-21 14:48:16 -04008485 if (trans)
8486 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04008487 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008488 if (own_trans) {
8489 trans = btrfs_start_transaction(root, 3);
8490 if (IS_ERR(trans)) {
8491 ret = PTR_ERR(trans);
8492 break;
8493 }
Yan Zhengd899e052008-10-30 14:25:28 -04008494 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00008495
Chris Mason154ea282013-03-05 11:11:26 -05008496 cur_bytes = min(num_bytes, 256ULL * 1024 * 1024);
8497 cur_bytes = max(cur_bytes, min_size);
8498 ret = btrfs_reserve_extent(trans, root, cur_bytes,
Zach Brown24542bf2012-11-16 00:04:43 +00008499 min_size, 0, *alloc_hint, &ins, 1);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008500 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008501 if (own_trans)
8502 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008503 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00008504 }
8505
Yan Zhengd899e052008-10-30 14:25:28 -04008506 ret = insert_reserved_file_extent(trans, inode,
8507 cur_offset, ins.objectid,
8508 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00008509 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04008510 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008511 if (ret) {
8512 btrfs_abort_transaction(trans, root, ret);
8513 if (own_trans)
8514 btrfs_end_transaction(trans, root);
8515 break;
8516 }
Chris Masona1ed8352009-09-11 12:27:37 -04008517 btrfs_drop_extent_cache(inode, cur_offset,
8518 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008519
Josef Bacik5dc562c2012-08-17 13:14:17 -04008520 em = alloc_extent_map();
8521 if (!em) {
8522 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
8523 &BTRFS_I(inode)->runtime_flags);
8524 goto next;
8525 }
8526
8527 em->start = cur_offset;
8528 em->orig_start = cur_offset;
8529 em->len = ins.offset;
8530 em->block_start = ins.objectid;
8531 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05008532 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04008533 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008534 em->bdev = root->fs_info->fs_devices->latest_bdev;
8535 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
8536 em->generation = trans->transid;
8537
8538 while (1) {
8539 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04008540 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04008541 write_unlock(&em_tree->lock);
8542 if (ret != -EEXIST)
8543 break;
8544 btrfs_drop_extent_cache(inode, cur_offset,
8545 cur_offset + ins.offset - 1,
8546 0);
8547 }
8548 free_extent_map(em);
8549next:
Yan Zhengd899e052008-10-30 14:25:28 -04008550 num_bytes -= ins.offset;
8551 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04008552 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00008553
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008554 inode_inc_iversion(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04008555 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02008556 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04008557 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04008558 (actual_len > inode->i_size) &&
8559 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00008560 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00008561 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00008562 else
Josef Bacik55a61d12010-11-22 18:50:32 +00008563 i_size = cur_offset;
8564 i_size_write(inode, i_size);
8565 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008566 }
8567
Yan Zhengd899e052008-10-30 14:25:28 -04008568 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008569
8570 if (ret) {
8571 btrfs_abort_transaction(trans, root, ret);
8572 if (own_trans)
8573 btrfs_end_transaction(trans, root);
8574 break;
8575 }
Yan Zhengd899e052008-10-30 14:25:28 -04008576
Josef Bacik0af3d002010-06-21 14:48:16 -04008577 if (own_trans)
8578 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008579 }
Yan Zhengd899e052008-10-30 14:25:28 -04008580 return ret;
8581}
8582
Josef Bacik0af3d002010-06-21 14:48:16 -04008583int btrfs_prealloc_file_range(struct inode *inode, int mode,
8584 u64 start, u64 num_bytes, u64 min_size,
8585 loff_t actual_len, u64 *alloc_hint)
8586{
8587 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
8588 min_size, actual_len, alloc_hint,
8589 NULL);
8590}
8591
8592int btrfs_prealloc_file_range_trans(struct inode *inode,
8593 struct btrfs_trans_handle *trans, int mode,
8594 u64 start, u64 num_bytes, u64 min_size,
8595 loff_t actual_len, u64 *alloc_hint)
8596{
8597 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
8598 min_size, actual_len, alloc_hint, trans);
8599}
8600
Chris Masone6dcd2d2008-07-17 12:53:50 -04008601static int btrfs_set_page_dirty(struct page *page)
8602{
Chris Masone6dcd2d2008-07-17 12:53:50 -04008603 return __set_page_dirty_nobuffers(page);
8604}
8605
Al Viro10556cb2011-06-20 19:28:19 -04008606static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05008607{
Li Zefanb83cc962010-12-20 16:04:08 +08008608 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00008609 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08008610
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00008611 if (mask & MAY_WRITE &&
8612 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
8613 if (btrfs_root_readonly(root))
8614 return -EROFS;
8615 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
8616 return -EACCES;
8617 }
Al Viro2830ba72011-06-20 19:16:29 -04008618 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05008619}
Chris Mason39279cc2007-06-12 06:35:45 -04008620
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008621static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05008622 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04008623 .lookup = btrfs_lookup,
8624 .create = btrfs_create,
8625 .unlink = btrfs_unlink,
8626 .link = btrfs_link,
8627 .mkdir = btrfs_mkdir,
8628 .rmdir = btrfs_rmdir,
8629 .rename = btrfs_rename,
8630 .symlink = btrfs_symlink,
8631 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04008632 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008633 .setxattr = btrfs_setxattr,
8634 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05008635 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008636 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05008637 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008638 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04008639};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008640static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008641 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05008642 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008643 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04008644};
Yan, Zheng76dda932009-09-21 16:00:26 -04008645
Alexey Dobriyan828c0952009-10-01 15:43:56 -07008646static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008647 .llseek = generic_file_llseek,
8648 .read = generic_read_dir,
Al Viro9cdda8d2013-05-22 16:48:09 -04008649 .iterate = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04008650 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04008651#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04008652 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04008653#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04008654 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04008655 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04008656};
8657
Chris Masond1310b22008-01-24 16:13:08 -05008658static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04008659 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05008660 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04008661 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04008662 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008663 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04008664 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05008665 .set_bit_hook = btrfs_set_bit_hook,
8666 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04008667 .merge_extent_hook = btrfs_merge_extent_hook,
8668 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04008669};
8670
Chris Mason35054392009-01-21 13:11:13 -05008671/*
8672 * btrfs doesn't support the bmap operation because swapfiles
8673 * use bmap to make a mapping of extents in the file. They assume
8674 * these extents won't change over the life of the file and they
8675 * use the bmap result to do IO directly to the drive.
8676 *
8677 * the btrfs bmap call would return logical addresses that aren't
8678 * suitable for IO and they also will change frequently as COW
8679 * operations happen. So, swapfile + btrfs == corruption.
8680 *
8681 * For now we're avoiding this by dropping bmap.
8682 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008683static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008684 .readpage = btrfs_readpage,
8685 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04008686 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05008687 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04008688 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04008689 .invalidatepage = btrfs_invalidatepage,
8690 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008691 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02008692 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04008693};
8694
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008695static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008696 .readpage = btrfs_readpage,
8697 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04008698 .invalidatepage = btrfs_invalidatepage,
8699 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04008700};
8701
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008702static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008703 .getattr = btrfs_getattr,
8704 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008705 .setxattr = btrfs_setxattr,
8706 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05008707 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008708 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05008709 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008710 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008711 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008712 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008713};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008714static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04008715 .getattr = btrfs_getattr,
8716 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008717 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008718 .setxattr = btrfs_setxattr,
8719 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04008720 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008721 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008722 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008723 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -04008724};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008725static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008726 .readlink = generic_readlink,
8727 .follow_link = page_follow_link_light,
8728 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00008729 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -05008730 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008731 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05008732 .setxattr = btrfs_setxattr,
8733 .getxattr = btrfs_getxattr,
8734 .listxattr = btrfs_listxattr,
8735 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008736 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008737 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008738};
Yan, Zheng76dda932009-09-21 16:00:26 -04008739
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04008740const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04008741 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -04008742 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -04008743};